smp.c 9.7 KB

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