smp.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * IPI management based on arch/arm/kernel/smp.c (Copyright 2002 ARM Limited)
  3. *
  4. * Copyright 2007-2009 Analog Devices Inc.
  5. * Philippe Gerum <rpm@xenomai.org>
  6. *
  7. * Licensed under the GPL-2.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/delay.h>
  11. #include <linux/init.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/sched.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/cache.h>
  16. #include <linux/clockchips.h>
  17. #include <linux/profile.h>
  18. #include <linux/errno.h>
  19. #include <linux/mm.h>
  20. #include <linux/cpu.h>
  21. #include <linux/smp.h>
  22. #include <linux/cpumask.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/irq.h>
  25. #include <linux/slab.h>
  26. #include <linux/atomic.h>
  27. #include <asm/cacheflush.h>
  28. #include <asm/irq_handler.h>
  29. #include <asm/mmu_context.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/pgalloc.h>
  32. #include <asm/processor.h>
  33. #include <asm/ptrace.h>
  34. #include <asm/cpu.h>
  35. #include <asm/time.h>
  36. #include <linux/err.h>
  37. /*
  38. * Anomaly notes:
  39. * 05000120 - we always define corelock as 32-bit integer in L2
  40. */
  41. struct corelock_slot corelock __attribute__ ((__section__(".l2.bss")));
  42. #ifdef CONFIG_ICACHE_FLUSH_L1
  43. unsigned long blackfin_iflush_l1_entry[NR_CPUS];
  44. #endif
  45. struct blackfin_initial_pda initial_pda_coreb;
  46. enum ipi_message_type {
  47. BFIN_IPI_NONE,
  48. BFIN_IPI_TIMER,
  49. BFIN_IPI_RESCHEDULE,
  50. BFIN_IPI_CALL_FUNC,
  51. BFIN_IPI_CALL_FUNC_SINGLE,
  52. BFIN_IPI_CPU_STOP,
  53. };
  54. struct blackfin_flush_data {
  55. unsigned long start;
  56. unsigned long end;
  57. };
  58. void *secondary_stack;
  59. static struct blackfin_flush_data smp_flush_data;
  60. static DEFINE_SPINLOCK(stop_lock);
  61. /* A magic number - stress test shows this is safe for common cases */
  62. #define BFIN_IPI_MSGQ_LEN 5
  63. /* Simple FIFO buffer, overflow leads to panic */
  64. struct ipi_data {
  65. atomic_t count;
  66. atomic_t bits;
  67. };
  68. static DEFINE_PER_CPU(struct ipi_data, bfin_ipi);
  69. static void ipi_cpu_stop(unsigned int cpu)
  70. {
  71. spin_lock(&stop_lock);
  72. printk(KERN_CRIT "CPU%u: stopping\n", cpu);
  73. dump_stack();
  74. spin_unlock(&stop_lock);
  75. set_cpu_online(cpu, false);
  76. local_irq_disable();
  77. while (1)
  78. SSYNC();
  79. }
  80. static void ipi_flush_icache(void *info)
  81. {
  82. struct blackfin_flush_data *fdata = info;
  83. /* Invalidate the memory holding the bounds of the flushed region. */
  84. blackfin_dcache_invalidate_range((unsigned long)fdata,
  85. (unsigned long)fdata + sizeof(*fdata));
  86. /* Make sure all write buffers in the data side of the core
  87. * are flushed before trying to invalidate the icache. This
  88. * needs to be after the data flush and before the icache
  89. * flush so that the SSYNC does the right thing in preventing
  90. * the instruction prefetcher from hitting things in cached
  91. * memory at the wrong time -- it runs much further ahead than
  92. * the pipeline.
  93. */
  94. SSYNC();
  95. /* ipi_flaush_icache is invoked by generic flush_icache_range,
  96. * so call blackfin arch icache flush directly here.
  97. */
  98. blackfin_icache_flush_range(fdata->start, fdata->end);
  99. }
  100. /* Use IRQ_SUPPLE_0 to request reschedule.
  101. * When returning from interrupt to user space,
  102. * there is chance to reschedule */
  103. static irqreturn_t ipi_handler_int0(int irq, void *dev_instance)
  104. {
  105. unsigned int cpu = smp_processor_id();
  106. platform_clear_ipi(cpu, IRQ_SUPPLE_0);
  107. return IRQ_HANDLED;
  108. }
  109. DECLARE_PER_CPU(struct clock_event_device, coretmr_events);
  110. void ipi_timer(void)
  111. {
  112. int cpu = smp_processor_id();
  113. struct clock_event_device *evt = &per_cpu(coretmr_events, cpu);
  114. evt->event_handler(evt);
  115. }
  116. static irqreturn_t ipi_handler_int1(int irq, void *dev_instance)
  117. {
  118. struct ipi_data *bfin_ipi_data;
  119. unsigned int cpu = smp_processor_id();
  120. unsigned long pending;
  121. unsigned long msg;
  122. platform_clear_ipi(cpu, IRQ_SUPPLE_1);
  123. smp_rmb();
  124. bfin_ipi_data = &__get_cpu_var(bfin_ipi);
  125. while ((pending = atomic_xchg(&bfin_ipi_data->bits, 0)) != 0) {
  126. msg = 0;
  127. do {
  128. msg = find_next_bit(&pending, BITS_PER_LONG, msg + 1);
  129. switch (msg) {
  130. case BFIN_IPI_TIMER:
  131. ipi_timer();
  132. break;
  133. case BFIN_IPI_RESCHEDULE:
  134. scheduler_ipi();
  135. break;
  136. case BFIN_IPI_CALL_FUNC:
  137. generic_smp_call_function_interrupt();
  138. break;
  139. case BFIN_IPI_CALL_FUNC_SINGLE:
  140. generic_smp_call_function_single_interrupt();
  141. break;
  142. case BFIN_IPI_CPU_STOP:
  143. ipi_cpu_stop(cpu);
  144. break;
  145. default:
  146. goto out;
  147. }
  148. atomic_dec(&bfin_ipi_data->count);
  149. } while (msg < BITS_PER_LONG);
  150. }
  151. out:
  152. return IRQ_HANDLED;
  153. }
  154. static void bfin_ipi_init(void)
  155. {
  156. unsigned int cpu;
  157. struct ipi_data *bfin_ipi_data;
  158. for_each_possible_cpu(cpu) {
  159. bfin_ipi_data = &per_cpu(bfin_ipi, cpu);
  160. atomic_set(&bfin_ipi_data->bits, 0);
  161. atomic_set(&bfin_ipi_data->count, 0);
  162. }
  163. }
  164. void send_ipi(const struct cpumask *cpumask, enum ipi_message_type msg)
  165. {
  166. unsigned int cpu;
  167. struct ipi_data *bfin_ipi_data;
  168. unsigned long flags;
  169. local_irq_save(flags);
  170. for_each_cpu(cpu, cpumask) {
  171. bfin_ipi_data = &per_cpu(bfin_ipi, cpu);
  172. atomic_set_mask((1 << msg), &bfin_ipi_data->bits);
  173. atomic_inc(&bfin_ipi_data->count);
  174. }
  175. local_irq_restore(flags);
  176. smp_wmb();
  177. for_each_cpu(cpu, cpumask)
  178. platform_send_ipi_cpu(cpu, IRQ_SUPPLE_1);
  179. }
  180. void arch_send_call_function_single_ipi(int cpu)
  181. {
  182. send_ipi(cpumask_of(cpu), BFIN_IPI_CALL_FUNC_SINGLE);
  183. }
  184. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  185. {
  186. send_ipi(mask, BFIN_IPI_CALL_FUNC);
  187. }
  188. void smp_send_reschedule(int cpu)
  189. {
  190. send_ipi(cpumask_of(cpu), BFIN_IPI_RESCHEDULE);
  191. return;
  192. }
  193. void smp_send_msg(const struct cpumask *mask, unsigned long type)
  194. {
  195. send_ipi(mask, type);
  196. }
  197. void smp_timer_broadcast(const struct cpumask *mask)
  198. {
  199. smp_send_msg(mask, BFIN_IPI_TIMER);
  200. }
  201. void smp_send_stop(void)
  202. {
  203. cpumask_t callmap;
  204. preempt_disable();
  205. cpumask_copy(&callmap, cpu_online_mask);
  206. cpumask_clear_cpu(smp_processor_id(), &callmap);
  207. if (!cpumask_empty(&callmap))
  208. send_ipi(&callmap, BFIN_IPI_CPU_STOP);
  209. preempt_enable();
  210. return;
  211. }
  212. int __cpu_up(unsigned int cpu, struct task_struct *idle)
  213. {
  214. int ret;
  215. secondary_stack = task_stack_page(idle) + THREAD_SIZE;
  216. ret = platform_boot_secondary(cpu, idle);
  217. secondary_stack = NULL;
  218. return ret;
  219. }
  220. static void setup_secondary(unsigned int cpu)
  221. {
  222. unsigned long ilat;
  223. bfin_write_IMASK(0);
  224. CSYNC();
  225. ilat = bfin_read_ILAT();
  226. CSYNC();
  227. bfin_write_ILAT(ilat);
  228. CSYNC();
  229. /* Enable interrupt levels IVG7-15. IARs have been already
  230. * programmed by the boot CPU. */
  231. bfin_irq_flags |= IMASK_IVG15 |
  232. IMASK_IVG14 | IMASK_IVG13 | IMASK_IVG12 | IMASK_IVG11 |
  233. IMASK_IVG10 | IMASK_IVG9 | IMASK_IVG8 | IMASK_IVG7 | IMASK_IVGHW;
  234. }
  235. void secondary_start_kernel(void)
  236. {
  237. unsigned int cpu = smp_processor_id();
  238. struct mm_struct *mm = &init_mm;
  239. if (_bfin_swrst & SWRST_DBL_FAULT_B) {
  240. printk(KERN_EMERG "CoreB Recovering from DOUBLE FAULT event\n");
  241. #ifdef CONFIG_DEBUG_DOUBLEFAULT
  242. printk(KERN_EMERG " While handling exception (EXCAUSE = %#x) at %pF\n",
  243. initial_pda_coreb.seqstat_doublefault & SEQSTAT_EXCAUSE,
  244. initial_pda_coreb.retx_doublefault);
  245. printk(KERN_NOTICE " DCPLB_FAULT_ADDR: %pF\n",
  246. initial_pda_coreb.dcplb_doublefault_addr);
  247. printk(KERN_NOTICE " ICPLB_FAULT_ADDR: %pF\n",
  248. initial_pda_coreb.icplb_doublefault_addr);
  249. #endif
  250. printk(KERN_NOTICE " The instruction at %pF caused a double exception\n",
  251. initial_pda_coreb.retx);
  252. }
  253. /*
  254. * We want the D-cache to be enabled early, in case the atomic
  255. * support code emulates cache coherence (see
  256. * __ARCH_SYNC_CORE_DCACHE).
  257. */
  258. init_exception_vectors();
  259. local_irq_disable();
  260. /* Attach the new idle task to the global mm. */
  261. atomic_inc(&mm->mm_users);
  262. atomic_inc(&mm->mm_count);
  263. current->active_mm = mm;
  264. preempt_disable();
  265. setup_secondary(cpu);
  266. platform_secondary_init(cpu);
  267. /* setup local core timer */
  268. bfin_local_timer_setup();
  269. local_irq_enable();
  270. bfin_setup_caches(cpu);
  271. notify_cpu_starting(cpu);
  272. /*
  273. * Calibrate loops per jiffy value.
  274. * IRQs need to be enabled here - D-cache can be invalidated
  275. * in timer irq handler, so core B can read correct jiffies.
  276. */
  277. calibrate_delay();
  278. /* We are done with local CPU inits, unblock the boot CPU. */
  279. set_cpu_online(cpu, true);
  280. cpu_startup_entry(CPUHP_ONLINE);
  281. }
  282. void __init smp_prepare_boot_cpu(void)
  283. {
  284. }
  285. void __init smp_prepare_cpus(unsigned int max_cpus)
  286. {
  287. platform_prepare_cpus(max_cpus);
  288. bfin_ipi_init();
  289. platform_request_ipi(IRQ_SUPPLE_0, ipi_handler_int0);
  290. platform_request_ipi(IRQ_SUPPLE_1, ipi_handler_int1);
  291. }
  292. void __init smp_cpus_done(unsigned int max_cpus)
  293. {
  294. unsigned long bogosum = 0;
  295. unsigned int cpu;
  296. for_each_online_cpu(cpu)
  297. bogosum += loops_per_jiffy;
  298. printk(KERN_INFO "SMP: Total of %d processors activated "
  299. "(%lu.%02lu BogoMIPS).\n",
  300. num_online_cpus(),
  301. bogosum / (500000/HZ),
  302. (bogosum / (5000/HZ)) % 100);
  303. }
  304. void smp_icache_flush_range_others(unsigned long start, unsigned long end)
  305. {
  306. smp_flush_data.start = start;
  307. smp_flush_data.end = end;
  308. preempt_disable();
  309. if (smp_call_function(&ipi_flush_icache, &smp_flush_data, 1))
  310. printk(KERN_WARNING "SMP: failed to run I-cache flush request on other CPUs\n");
  311. preempt_enable();
  312. }
  313. EXPORT_SYMBOL_GPL(smp_icache_flush_range_others);
  314. #ifdef __ARCH_SYNC_CORE_ICACHE
  315. unsigned long icache_invld_count[NR_CPUS];
  316. void resync_core_icache(void)
  317. {
  318. unsigned int cpu = get_cpu();
  319. blackfin_invalidate_entire_icache();
  320. icache_invld_count[cpu]++;
  321. put_cpu();
  322. }
  323. EXPORT_SYMBOL(resync_core_icache);
  324. #endif
  325. #ifdef __ARCH_SYNC_CORE_DCACHE
  326. unsigned long dcache_invld_count[NR_CPUS];
  327. unsigned long barrier_mask __attribute__ ((__section__(".l2.bss")));
  328. void resync_core_dcache(void)
  329. {
  330. unsigned int cpu = get_cpu();
  331. blackfin_invalidate_entire_dcache();
  332. dcache_invld_count[cpu]++;
  333. put_cpu();
  334. }
  335. EXPORT_SYMBOL(resync_core_dcache);
  336. #endif
  337. #ifdef CONFIG_HOTPLUG_CPU
  338. int __cpu_disable(void)
  339. {
  340. unsigned int cpu = smp_processor_id();
  341. if (cpu == 0)
  342. return -EPERM;
  343. set_cpu_online(cpu, false);
  344. return 0;
  345. }
  346. static DECLARE_COMPLETION(cpu_killed);
  347. int __cpu_die(unsigned int cpu)
  348. {
  349. return wait_for_completion_timeout(&cpu_killed, 5000);
  350. }
  351. void cpu_die(void)
  352. {
  353. complete(&cpu_killed);
  354. atomic_dec(&init_mm.mm_users);
  355. atomic_dec(&init_mm.mm_count);
  356. local_irq_disable();
  357. platform_cpu_die();
  358. }
  359. #endif