smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * File: arch/blackfin/kernel/smp.c
  3. * Author: Philippe Gerum <rpm@xenomai.org>
  4. * IPI management based on arch/arm/kernel/smp.c.
  5. *
  6. * Copyright 2007 Analog Devices Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, see the file COPYING, or write
  20. * to the Free Software Foundation, Inc.,
  21. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <linux/module.h>
  24. #include <linux/delay.h>
  25. #include <linux/init.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/sched.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/cache.h>
  30. #include <linux/profile.h>
  31. #include <linux/errno.h>
  32. #include <linux/mm.h>
  33. #include <linux/cpu.h>
  34. #include <linux/smp.h>
  35. #include <linux/seq_file.h>
  36. #include <linux/irq.h>
  37. #include <asm/atomic.h>
  38. #include <asm/cacheflush.h>
  39. #include <asm/mmu_context.h>
  40. #include <asm/pgtable.h>
  41. #include <asm/pgalloc.h>
  42. #include <asm/processor.h>
  43. #include <asm/ptrace.h>
  44. #include <asm/cpu.h>
  45. #include <linux/err.h>
  46. struct corelock_slot corelock __attribute__ ((__section__(".l2.bss")));
  47. void __cpuinitdata *init_retx_coreb, *init_saved_retx_coreb,
  48. *init_saved_seqstat_coreb, *init_saved_icplb_fault_addr_coreb,
  49. *init_saved_dcplb_fault_addr_coreb;
  50. cpumask_t cpu_possible_map;
  51. EXPORT_SYMBOL(cpu_possible_map);
  52. cpumask_t cpu_online_map;
  53. EXPORT_SYMBOL(cpu_online_map);
  54. #define BFIN_IPI_RESCHEDULE 0
  55. #define BFIN_IPI_CALL_FUNC 1
  56. #define BFIN_IPI_CPU_STOP 2
  57. struct blackfin_flush_data {
  58. unsigned long start;
  59. unsigned long end;
  60. };
  61. void *secondary_stack;
  62. struct smp_call_struct {
  63. void (*func)(void *info);
  64. void *info;
  65. int wait;
  66. cpumask_t pending;
  67. cpumask_t waitmask;
  68. };
  69. static struct blackfin_flush_data smp_flush_data;
  70. static DEFINE_SPINLOCK(stop_lock);
  71. struct ipi_message {
  72. struct list_head list;
  73. unsigned long type;
  74. struct smp_call_struct call_struct;
  75. };
  76. struct ipi_message_queue {
  77. struct list_head head;
  78. spinlock_t lock;
  79. unsigned long count;
  80. };
  81. static DEFINE_PER_CPU(struct ipi_message_queue, ipi_msg_queue);
  82. static void ipi_cpu_stop(unsigned int cpu)
  83. {
  84. spin_lock(&stop_lock);
  85. printk(KERN_CRIT "CPU%u: stopping\n", cpu);
  86. dump_stack();
  87. spin_unlock(&stop_lock);
  88. cpu_clear(cpu, cpu_online_map);
  89. local_irq_disable();
  90. while (1)
  91. SSYNC();
  92. }
  93. static void ipi_flush_icache(void *info)
  94. {
  95. struct blackfin_flush_data *fdata = info;
  96. /* Invalidate the memory holding the bounds of the flushed region. */
  97. blackfin_dcache_invalidate_range((unsigned long)fdata,
  98. (unsigned long)fdata + sizeof(*fdata));
  99. blackfin_icache_flush_range(fdata->start, fdata->end);
  100. }
  101. static void ipi_call_function(unsigned int cpu, struct ipi_message *msg)
  102. {
  103. int wait;
  104. void (*func)(void *info);
  105. void *info;
  106. func = msg->call_struct.func;
  107. info = msg->call_struct.info;
  108. wait = msg->call_struct.wait;
  109. cpu_clear(cpu, msg->call_struct.pending);
  110. func(info);
  111. if (wait)
  112. cpu_clear(cpu, msg->call_struct.waitmask);
  113. else
  114. kfree(msg);
  115. }
  116. static irqreturn_t ipi_handler(int irq, void *dev_instance)
  117. {
  118. struct ipi_message *msg, *mg;
  119. struct ipi_message_queue *msg_queue;
  120. unsigned int cpu = smp_processor_id();
  121. platform_clear_ipi(cpu);
  122. msg_queue = &__get_cpu_var(ipi_msg_queue);
  123. msg_queue->count++;
  124. spin_lock(&msg_queue->lock);
  125. list_for_each_entry_safe(msg, mg, &msg_queue->head, list) {
  126. list_del(&msg->list);
  127. switch (msg->type) {
  128. case BFIN_IPI_RESCHEDULE:
  129. /* That's the easiest one; leave it to
  130. * return_from_int. */
  131. kfree(msg);
  132. break;
  133. case BFIN_IPI_CALL_FUNC:
  134. spin_unlock(&msg_queue->lock);
  135. ipi_call_function(cpu, msg);
  136. spin_lock(&msg_queue->lock);
  137. break;
  138. case BFIN_IPI_CPU_STOP:
  139. spin_unlock(&msg_queue->lock);
  140. ipi_cpu_stop(cpu);
  141. spin_lock(&msg_queue->lock);
  142. kfree(msg);
  143. break;
  144. default:
  145. printk(KERN_CRIT "CPU%u: Unknown IPI message \
  146. 0x%lx\n", cpu, msg->type);
  147. kfree(msg);
  148. break;
  149. }
  150. }
  151. spin_unlock(&msg_queue->lock);
  152. return IRQ_HANDLED;
  153. }
  154. static void ipi_queue_init(void)
  155. {
  156. unsigned int cpu;
  157. struct ipi_message_queue *msg_queue;
  158. for_each_possible_cpu(cpu) {
  159. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  160. INIT_LIST_HEAD(&msg_queue->head);
  161. spin_lock_init(&msg_queue->lock);
  162. msg_queue->count = 0;
  163. }
  164. }
  165. int smp_call_function(void (*func)(void *info), void *info, int wait)
  166. {
  167. unsigned int cpu;
  168. cpumask_t callmap;
  169. unsigned long flags;
  170. struct ipi_message_queue *msg_queue;
  171. struct ipi_message *msg;
  172. callmap = cpu_online_map;
  173. cpu_clear(smp_processor_id(), callmap);
  174. if (cpus_empty(callmap))
  175. return 0;
  176. msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
  177. INIT_LIST_HEAD(&msg->list);
  178. msg->call_struct.func = func;
  179. msg->call_struct.info = info;
  180. msg->call_struct.wait = wait;
  181. msg->call_struct.pending = callmap;
  182. msg->call_struct.waitmask = callmap;
  183. msg->type = BFIN_IPI_CALL_FUNC;
  184. for_each_cpu_mask(cpu, callmap) {
  185. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  186. spin_lock_irqsave(&msg_queue->lock, flags);
  187. list_add(&msg->list, &msg_queue->head);
  188. spin_unlock_irqrestore(&msg_queue->lock, flags);
  189. platform_send_ipi_cpu(cpu);
  190. }
  191. if (wait) {
  192. while (!cpus_empty(msg->call_struct.waitmask))
  193. blackfin_dcache_invalidate_range(
  194. (unsigned long)(&msg->call_struct.waitmask),
  195. (unsigned long)(&msg->call_struct.waitmask));
  196. kfree(msg);
  197. }
  198. return 0;
  199. }
  200. EXPORT_SYMBOL_GPL(smp_call_function);
  201. int smp_call_function_single(int cpuid, void (*func) (void *info), void *info,
  202. int wait)
  203. {
  204. unsigned int cpu = cpuid;
  205. cpumask_t callmap;
  206. unsigned long flags;
  207. struct ipi_message_queue *msg_queue;
  208. struct ipi_message *msg;
  209. if (cpu_is_offline(cpu))
  210. return 0;
  211. cpus_clear(callmap);
  212. cpu_set(cpu, callmap);
  213. msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
  214. INIT_LIST_HEAD(&msg->list);
  215. msg->call_struct.func = func;
  216. msg->call_struct.info = info;
  217. msg->call_struct.wait = wait;
  218. msg->call_struct.pending = callmap;
  219. msg->call_struct.waitmask = callmap;
  220. msg->type = BFIN_IPI_CALL_FUNC;
  221. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  222. spin_lock_irqsave(&msg_queue->lock, flags);
  223. list_add(&msg->list, &msg_queue->head);
  224. spin_unlock_irqrestore(&msg_queue->lock, flags);
  225. platform_send_ipi_cpu(cpu);
  226. if (wait) {
  227. while (!cpus_empty(msg->call_struct.waitmask))
  228. blackfin_dcache_invalidate_range(
  229. (unsigned long)(&msg->call_struct.waitmask),
  230. (unsigned long)(&msg->call_struct.waitmask));
  231. kfree(msg);
  232. }
  233. return 0;
  234. }
  235. EXPORT_SYMBOL_GPL(smp_call_function_single);
  236. void smp_send_reschedule(int cpu)
  237. {
  238. unsigned long flags;
  239. struct ipi_message_queue *msg_queue;
  240. struct ipi_message *msg;
  241. if (cpu_is_offline(cpu))
  242. return;
  243. msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
  244. memset(msg, 0, sizeof(msg));
  245. INIT_LIST_HEAD(&msg->list);
  246. msg->type = BFIN_IPI_RESCHEDULE;
  247. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  248. spin_lock_irqsave(&msg_queue->lock, flags);
  249. list_add(&msg->list, &msg_queue->head);
  250. spin_unlock_irqrestore(&msg_queue->lock, flags);
  251. platform_send_ipi_cpu(cpu);
  252. return;
  253. }
  254. void smp_send_stop(void)
  255. {
  256. unsigned int cpu;
  257. cpumask_t callmap;
  258. unsigned long flags;
  259. struct ipi_message_queue *msg_queue;
  260. struct ipi_message *msg;
  261. callmap = cpu_online_map;
  262. cpu_clear(smp_processor_id(), callmap);
  263. if (cpus_empty(callmap))
  264. return;
  265. msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
  266. memset(msg, 0, sizeof(msg));
  267. INIT_LIST_HEAD(&msg->list);
  268. msg->type = BFIN_IPI_CPU_STOP;
  269. for_each_cpu_mask(cpu, callmap) {
  270. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  271. spin_lock_irqsave(&msg_queue->lock, flags);
  272. list_add(&msg->list, &msg_queue->head);
  273. spin_unlock_irqrestore(&msg_queue->lock, flags);
  274. platform_send_ipi_cpu(cpu);
  275. }
  276. return;
  277. }
  278. int __cpuinit __cpu_up(unsigned int cpu)
  279. {
  280. struct task_struct *idle;
  281. int ret;
  282. idle = fork_idle(cpu);
  283. if (IS_ERR(idle)) {
  284. printk(KERN_ERR "CPU%u: fork() failed\n", cpu);
  285. return PTR_ERR(idle);
  286. }
  287. secondary_stack = task_stack_page(idle) + THREAD_SIZE;
  288. smp_wmb();
  289. ret = platform_boot_secondary(cpu, idle);
  290. if (ret) {
  291. cpu_clear(cpu, cpu_present_map);
  292. printk(KERN_CRIT "CPU%u: processor failed to boot (%d)\n", cpu, ret);
  293. free_task(idle);
  294. } else
  295. cpu_set(cpu, cpu_online_map);
  296. secondary_stack = NULL;
  297. return ret;
  298. }
  299. static void __cpuinit setup_secondary(unsigned int cpu)
  300. {
  301. #if !(defined(CONFIG_TICK_SOURCE_SYSTMR0) || defined(CONFIG_IPIPE))
  302. struct irq_desc *timer_desc;
  303. #endif
  304. unsigned long ilat;
  305. bfin_write_IMASK(0);
  306. CSYNC();
  307. ilat = bfin_read_ILAT();
  308. CSYNC();
  309. bfin_write_ILAT(ilat);
  310. CSYNC();
  311. /* Reserve the PDA space for the secondary CPU. */
  312. reserve_pda();
  313. /* Enable interrupt levels IVG7-15. IARs have been already
  314. * programmed by the boot CPU. */
  315. bfin_irq_flags |= IMASK_IVG15 |
  316. IMASK_IVG14 | IMASK_IVG13 | IMASK_IVG12 | IMASK_IVG11 |
  317. IMASK_IVG10 | IMASK_IVG9 | IMASK_IVG8 | IMASK_IVG7 | IMASK_IVGHW;
  318. #if defined(CONFIG_TICK_SOURCE_SYSTMR0) || defined(CONFIG_IPIPE)
  319. /* Power down the core timer, just to play safe. */
  320. bfin_write_TCNTL(0);
  321. /* system timer0 has been setup by CoreA. */
  322. #else
  323. timer_desc = irq_desc + IRQ_CORETMR;
  324. setup_core_timer();
  325. timer_desc->chip->enable(IRQ_CORETMR);
  326. #endif
  327. }
  328. void __cpuinit secondary_start_kernel(void)
  329. {
  330. unsigned int cpu = smp_processor_id();
  331. struct mm_struct *mm = &init_mm;
  332. if (_bfin_swrst & SWRST_DBL_FAULT_B) {
  333. printk(KERN_EMERG "CoreB Recovering from DOUBLE FAULT event\n");
  334. #ifdef CONFIG_DEBUG_DOUBLEFAULT
  335. printk(KERN_EMERG " While handling exception (EXCAUSE = 0x%x) at %pF\n",
  336. (int)init_saved_seqstat_coreb & SEQSTAT_EXCAUSE, init_saved_retx_coreb);
  337. printk(KERN_NOTICE " DCPLB_FAULT_ADDR: %pF\n", init_saved_dcplb_fault_addr_coreb);
  338. printk(KERN_NOTICE " ICPLB_FAULT_ADDR: %pF\n", init_saved_icplb_fault_addr_coreb);
  339. #endif
  340. printk(KERN_NOTICE " The instruction at %pF caused a double exception\n",
  341. init_retx_coreb);
  342. }
  343. /*
  344. * We want the D-cache to be enabled early, in case the atomic
  345. * support code emulates cache coherence (see
  346. * __ARCH_SYNC_CORE_DCACHE).
  347. */
  348. init_exception_vectors();
  349. bfin_setup_caches(cpu);
  350. local_irq_disable();
  351. /* Attach the new idle task to the global mm. */
  352. atomic_inc(&mm->mm_users);
  353. atomic_inc(&mm->mm_count);
  354. current->active_mm = mm;
  355. BUG_ON(current->mm); /* Can't be, but better be safe than sorry. */
  356. preempt_disable();
  357. setup_secondary(cpu);
  358. local_irq_enable();
  359. platform_secondary_init(cpu);
  360. cpu_idle();
  361. }
  362. void __init smp_prepare_boot_cpu(void)
  363. {
  364. }
  365. void __init smp_prepare_cpus(unsigned int max_cpus)
  366. {
  367. platform_prepare_cpus(max_cpus);
  368. ipi_queue_init();
  369. platform_request_ipi(&ipi_handler);
  370. }
  371. void __init smp_cpus_done(unsigned int max_cpus)
  372. {
  373. unsigned long bogosum = 0;
  374. unsigned int cpu;
  375. for_each_online_cpu(cpu)
  376. bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
  377. printk(KERN_INFO "SMP: Total of %d processors activated "
  378. "(%lu.%02lu BogoMIPS).\n",
  379. num_online_cpus(),
  380. bogosum / (500000/HZ),
  381. (bogosum / (5000/HZ)) % 100);
  382. }
  383. void smp_icache_flush_range_others(unsigned long start, unsigned long end)
  384. {
  385. smp_flush_data.start = start;
  386. smp_flush_data.end = end;
  387. if (smp_call_function(&ipi_flush_icache, &smp_flush_data, 0))
  388. printk(KERN_WARNING "SMP: failed to run I-cache flush request on other CPUs\n");
  389. }
  390. EXPORT_SYMBOL_GPL(smp_icache_flush_range_others);
  391. #ifdef __ARCH_SYNC_CORE_DCACHE
  392. unsigned long barrier_mask __attribute__ ((__section__(".l2.bss")));
  393. void resync_core_dcache(void)
  394. {
  395. unsigned int cpu = get_cpu();
  396. blackfin_invalidate_entire_dcache();
  397. ++per_cpu(cpu_data, cpu).dcache_invld_count;
  398. put_cpu();
  399. }
  400. EXPORT_SYMBOL(resync_core_dcache);
  401. #endif