smp.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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/mm.h>
  14. #include <linux/sched/task_stack.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/cache.h>
  17. #include <linux/clockchips.h>
  18. #include <linux/profile.h>
  19. #include <linux/errno.h>
  20. #include <linux/mm.h>
  21. #include <linux/cpu.h>
  22. #include <linux/smp.h>
  23. #include <linux/cpumask.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/irq.h>
  26. #include <linux/slab.h>
  27. #include <linux/atomic.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/irq_handler.h>
  30. #include <asm/mmu_context.h>
  31. #include <asm/pgtable.h>
  32. #include <asm/pgalloc.h>
  33. #include <asm/processor.h>
  34. #include <asm/ptrace.h>
  35. #include <asm/cpu.h>
  36. #include <asm/time.h>
  37. #include <linux/err.h>
  38. /*
  39. * Anomaly notes:
  40. * 05000120 - we always define corelock as 32-bit integer in L2
  41. */
  42. struct corelock_slot corelock __attribute__ ((__section__(".l2.bss")));
  43. #ifdef CONFIG_ICACHE_FLUSH_L1
  44. unsigned long blackfin_iflush_l1_entry[NR_CPUS];
  45. #endif
  46. struct blackfin_initial_pda initial_pda_coreb;
  47. enum ipi_message_type {
  48. BFIN_IPI_NONE,
  49. BFIN_IPI_TIMER,
  50. BFIN_IPI_RESCHEDULE,
  51. BFIN_IPI_CALL_FUNC,
  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 = this_cpu_ptr(&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_CPU_STOP:
  140. ipi_cpu_stop(cpu);
  141. break;
  142. default:
  143. goto out;
  144. }
  145. atomic_dec(&bfin_ipi_data->count);
  146. } while (msg < BITS_PER_LONG);
  147. }
  148. out:
  149. return IRQ_HANDLED;
  150. }
  151. static void bfin_ipi_init(void)
  152. {
  153. unsigned int cpu;
  154. struct ipi_data *bfin_ipi_data;
  155. for_each_possible_cpu(cpu) {
  156. bfin_ipi_data = &per_cpu(bfin_ipi, cpu);
  157. atomic_set(&bfin_ipi_data->bits, 0);
  158. atomic_set(&bfin_ipi_data->count, 0);
  159. }
  160. }
  161. void send_ipi(const struct cpumask *cpumask, enum ipi_message_type msg)
  162. {
  163. unsigned int cpu;
  164. struct ipi_data *bfin_ipi_data;
  165. unsigned long flags;
  166. local_irq_save(flags);
  167. for_each_cpu(cpu, cpumask) {
  168. bfin_ipi_data = &per_cpu(bfin_ipi, cpu);
  169. atomic_or((1 << msg), &bfin_ipi_data->bits);
  170. atomic_inc(&bfin_ipi_data->count);
  171. }
  172. local_irq_restore(flags);
  173. smp_wmb();
  174. for_each_cpu(cpu, cpumask)
  175. platform_send_ipi_cpu(cpu, IRQ_SUPPLE_1);
  176. }
  177. void arch_send_call_function_single_ipi(int cpu)
  178. {
  179. send_ipi(cpumask_of(cpu), BFIN_IPI_CALL_FUNC);
  180. }
  181. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  182. {
  183. send_ipi(mask, BFIN_IPI_CALL_FUNC);
  184. }
  185. void smp_send_reschedule(int cpu)
  186. {
  187. send_ipi(cpumask_of(cpu), BFIN_IPI_RESCHEDULE);
  188. return;
  189. }
  190. void smp_send_msg(const struct cpumask *mask, unsigned long type)
  191. {
  192. send_ipi(mask, type);
  193. }
  194. void smp_timer_broadcast(const struct cpumask *mask)
  195. {
  196. smp_send_msg(mask, BFIN_IPI_TIMER);
  197. }
  198. void smp_send_stop(void)
  199. {
  200. cpumask_t callmap;
  201. preempt_disable();
  202. cpumask_copy(&callmap, cpu_online_mask);
  203. cpumask_clear_cpu(smp_processor_id(), &callmap);
  204. if (!cpumask_empty(&callmap))
  205. send_ipi(&callmap, BFIN_IPI_CPU_STOP);
  206. preempt_enable();
  207. return;
  208. }
  209. int __cpu_up(unsigned int cpu, struct task_struct *idle)
  210. {
  211. int ret;
  212. secondary_stack = task_stack_page(idle) + THREAD_SIZE;
  213. ret = platform_boot_secondary(cpu, idle);
  214. secondary_stack = NULL;
  215. return ret;
  216. }
  217. static void setup_secondary(unsigned int cpu)
  218. {
  219. unsigned long ilat;
  220. bfin_write_IMASK(0);
  221. CSYNC();
  222. ilat = bfin_read_ILAT();
  223. CSYNC();
  224. bfin_write_ILAT(ilat);
  225. CSYNC();
  226. /* Enable interrupt levels IVG7-15. IARs have been already
  227. * programmed by the boot CPU. */
  228. bfin_irq_flags |= IMASK_IVG15 |
  229. IMASK_IVG14 | IMASK_IVG13 | IMASK_IVG12 | IMASK_IVG11 |
  230. IMASK_IVG10 | IMASK_IVG9 | IMASK_IVG8 | IMASK_IVG7 | IMASK_IVGHW;
  231. }
  232. void secondary_start_kernel(void)
  233. {
  234. unsigned int cpu = smp_processor_id();
  235. struct mm_struct *mm = &init_mm;
  236. if (_bfin_swrst & SWRST_DBL_FAULT_B) {
  237. printk(KERN_EMERG "CoreB Recovering from DOUBLE FAULT event\n");
  238. #ifdef CONFIG_DEBUG_DOUBLEFAULT
  239. printk(KERN_EMERG " While handling exception (EXCAUSE = %#x) at %pF\n",
  240. initial_pda_coreb.seqstat_doublefault & SEQSTAT_EXCAUSE,
  241. initial_pda_coreb.retx_doublefault);
  242. printk(KERN_NOTICE " DCPLB_FAULT_ADDR: %pF\n",
  243. initial_pda_coreb.dcplb_doublefault_addr);
  244. printk(KERN_NOTICE " ICPLB_FAULT_ADDR: %pF\n",
  245. initial_pda_coreb.icplb_doublefault_addr);
  246. #endif
  247. printk(KERN_NOTICE " The instruction at %pF caused a double exception\n",
  248. initial_pda_coreb.retx);
  249. }
  250. /*
  251. * We want the D-cache to be enabled early, in case the atomic
  252. * support code emulates cache coherence (see
  253. * __ARCH_SYNC_CORE_DCACHE).
  254. */
  255. init_exception_vectors();
  256. local_irq_disable();
  257. /* Attach the new idle task to the global mm. */
  258. mmget(mm);
  259. mmgrab(mm);
  260. current->active_mm = mm;
  261. preempt_disable();
  262. setup_secondary(cpu);
  263. platform_secondary_init(cpu);
  264. /* setup local core timer */
  265. bfin_local_timer_setup();
  266. local_irq_enable();
  267. bfin_setup_caches(cpu);
  268. notify_cpu_starting(cpu);
  269. /*
  270. * Calibrate loops per jiffy value.
  271. * IRQs need to be enabled here - D-cache can be invalidated
  272. * in timer irq handler, so core B can read correct jiffies.
  273. */
  274. calibrate_delay();
  275. /* We are done with local CPU inits, unblock the boot CPU. */
  276. set_cpu_online(cpu, true);
  277. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  278. }
  279. void __init smp_prepare_boot_cpu(void)
  280. {
  281. }
  282. void __init smp_prepare_cpus(unsigned int max_cpus)
  283. {
  284. platform_prepare_cpus(max_cpus);
  285. bfin_ipi_init();
  286. platform_request_ipi(IRQ_SUPPLE_0, ipi_handler_int0);
  287. platform_request_ipi(IRQ_SUPPLE_1, ipi_handler_int1);
  288. }
  289. void __init smp_cpus_done(unsigned int max_cpus)
  290. {
  291. unsigned long bogosum = 0;
  292. unsigned int cpu;
  293. for_each_online_cpu(cpu)
  294. bogosum += loops_per_jiffy;
  295. printk(KERN_INFO "SMP: Total of %d processors activated "
  296. "(%lu.%02lu BogoMIPS).\n",
  297. num_online_cpus(),
  298. bogosum / (500000/HZ),
  299. (bogosum / (5000/HZ)) % 100);
  300. }
  301. void smp_icache_flush_range_others(unsigned long start, unsigned long end)
  302. {
  303. smp_flush_data.start = start;
  304. smp_flush_data.end = end;
  305. preempt_disable();
  306. if (smp_call_function(&ipi_flush_icache, &smp_flush_data, 1))
  307. printk(KERN_WARNING "SMP: failed to run I-cache flush request on other CPUs\n");
  308. preempt_enable();
  309. }
  310. EXPORT_SYMBOL_GPL(smp_icache_flush_range_others);
  311. #ifdef __ARCH_SYNC_CORE_ICACHE
  312. unsigned long icache_invld_count[NR_CPUS];
  313. void resync_core_icache(void)
  314. {
  315. unsigned int cpu = get_cpu();
  316. blackfin_invalidate_entire_icache();
  317. icache_invld_count[cpu]++;
  318. put_cpu();
  319. }
  320. EXPORT_SYMBOL(resync_core_icache);
  321. #endif
  322. #ifdef __ARCH_SYNC_CORE_DCACHE
  323. unsigned long dcache_invld_count[NR_CPUS];
  324. unsigned long barrier_mask __attribute__ ((__section__(".l2.bss")));
  325. void resync_core_dcache(void)
  326. {
  327. unsigned int cpu = get_cpu();
  328. blackfin_invalidate_entire_dcache();
  329. dcache_invld_count[cpu]++;
  330. put_cpu();
  331. }
  332. EXPORT_SYMBOL(resync_core_dcache);
  333. #endif
  334. #ifdef CONFIG_HOTPLUG_CPU
  335. int __cpu_disable(void)
  336. {
  337. unsigned int cpu = smp_processor_id();
  338. if (cpu == 0)
  339. return -EPERM;
  340. set_cpu_online(cpu, false);
  341. return 0;
  342. }
  343. int __cpu_die(unsigned int cpu)
  344. {
  345. return cpu_wait_death(cpu, 5);
  346. }
  347. void cpu_die(void)
  348. {
  349. (void)cpu_report_death();
  350. atomic_dec(&init_mm.mm_users);
  351. atomic_dec(&init_mm.mm_count);
  352. local_irq_disable();
  353. platform_cpu_die();
  354. }
  355. #endif