smp.c 9.8 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.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. bfin_ipi_data = &__get_cpu_var(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_CALL_FUNC_SINGLE:
  139. generic_smp_call_function_single_interrupt();
  140. break;
  141. case BFIN_IPI_CPU_STOP:
  142. ipi_cpu_stop(cpu);
  143. break;
  144. }
  145. atomic_dec(&bfin_ipi_data->count);
  146. } while (msg < BITS_PER_LONG);
  147. }
  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_set_mask((1 << msg), &bfin_ipi_data->bits);
  169. atomic_inc(&bfin_ipi_data->count);
  170. platform_send_ipi_cpu(cpu, IRQ_SUPPLE_1);
  171. }
  172. local_irq_restore(flags);
  173. }
  174. void arch_send_call_function_single_ipi(int cpu)
  175. {
  176. send_ipi(cpumask_of(cpu), BFIN_IPI_CALL_FUNC_SINGLE);
  177. }
  178. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  179. {
  180. send_ipi(mask, BFIN_IPI_CALL_FUNC);
  181. }
  182. void smp_send_reschedule(int cpu)
  183. {
  184. send_ipi(cpumask_of(cpu), BFIN_IPI_RESCHEDULE);
  185. return;
  186. }
  187. void smp_send_msg(const struct cpumask *mask, unsigned long type)
  188. {
  189. send_ipi(mask, type);
  190. }
  191. void smp_timer_broadcast(const struct cpumask *mask)
  192. {
  193. smp_send_msg(mask, BFIN_IPI_TIMER);
  194. }
  195. void smp_send_stop(void)
  196. {
  197. cpumask_t callmap;
  198. preempt_disable();
  199. cpumask_copy(&callmap, cpu_online_mask);
  200. cpumask_clear_cpu(smp_processor_id(), &callmap);
  201. if (!cpumask_empty(&callmap))
  202. send_ipi(&callmap, BFIN_IPI_CPU_STOP);
  203. preempt_enable();
  204. return;
  205. }
  206. int __cpu_up(unsigned int cpu, struct task_struct *idle)
  207. {
  208. int ret;
  209. secondary_stack = task_stack_page(idle) + THREAD_SIZE;
  210. ret = platform_boot_secondary(cpu, idle);
  211. secondary_stack = NULL;
  212. return ret;
  213. }
  214. static void setup_secondary(unsigned int cpu)
  215. {
  216. unsigned long ilat;
  217. bfin_write_IMASK(0);
  218. CSYNC();
  219. ilat = bfin_read_ILAT();
  220. CSYNC();
  221. bfin_write_ILAT(ilat);
  222. CSYNC();
  223. /* Enable interrupt levels IVG7-15. IARs have been already
  224. * programmed by the boot CPU. */
  225. bfin_irq_flags |= IMASK_IVG15 |
  226. IMASK_IVG14 | IMASK_IVG13 | IMASK_IVG12 | IMASK_IVG11 |
  227. IMASK_IVG10 | IMASK_IVG9 | IMASK_IVG8 | IMASK_IVG7 | IMASK_IVGHW;
  228. }
  229. void secondary_start_kernel(void)
  230. {
  231. unsigned int cpu = smp_processor_id();
  232. struct mm_struct *mm = &init_mm;
  233. if (_bfin_swrst & SWRST_DBL_FAULT_B) {
  234. printk(KERN_EMERG "CoreB Recovering from DOUBLE FAULT event\n");
  235. #ifdef CONFIG_DEBUG_DOUBLEFAULT
  236. printk(KERN_EMERG " While handling exception (EXCAUSE = %#x) at %pF\n",
  237. initial_pda_coreb.seqstat_doublefault & SEQSTAT_EXCAUSE,
  238. initial_pda_coreb.retx_doublefault);
  239. printk(KERN_NOTICE " DCPLB_FAULT_ADDR: %pF\n",
  240. initial_pda_coreb.dcplb_doublefault_addr);
  241. printk(KERN_NOTICE " ICPLB_FAULT_ADDR: %pF\n",
  242. initial_pda_coreb.icplb_doublefault_addr);
  243. #endif
  244. printk(KERN_NOTICE " The instruction at %pF caused a double exception\n",
  245. initial_pda_coreb.retx);
  246. }
  247. /*
  248. * We want the D-cache to be enabled early, in case the atomic
  249. * support code emulates cache coherence (see
  250. * __ARCH_SYNC_CORE_DCACHE).
  251. */
  252. init_exception_vectors();
  253. local_irq_disable();
  254. /* Attach the new idle task to the global mm. */
  255. atomic_inc(&mm->mm_users);
  256. atomic_inc(&mm->mm_count);
  257. current->active_mm = mm;
  258. preempt_disable();
  259. setup_secondary(cpu);
  260. platform_secondary_init(cpu);
  261. /* setup local core timer */
  262. bfin_local_timer_setup();
  263. local_irq_enable();
  264. bfin_setup_caches(cpu);
  265. notify_cpu_starting(cpu);
  266. /*
  267. * Calibrate loops per jiffy value.
  268. * IRQs need to be enabled here - D-cache can be invalidated
  269. * in timer irq handler, so core B can read correct jiffies.
  270. */
  271. calibrate_delay();
  272. /* We are done with local CPU inits, unblock the boot CPU. */
  273. set_cpu_online(cpu, true);
  274. cpu_startup_entry(CPUHP_ONLINE);
  275. }
  276. void __init smp_prepare_boot_cpu(void)
  277. {
  278. }
  279. void __init smp_prepare_cpus(unsigned int max_cpus)
  280. {
  281. platform_prepare_cpus(max_cpus);
  282. bfin_ipi_init();
  283. platform_request_ipi(IRQ_SUPPLE_0, ipi_handler_int0);
  284. platform_request_ipi(IRQ_SUPPLE_1, ipi_handler_int1);
  285. }
  286. void __init smp_cpus_done(unsigned int max_cpus)
  287. {
  288. unsigned long bogosum = 0;
  289. unsigned int cpu;
  290. for_each_online_cpu(cpu)
  291. bogosum += loops_per_jiffy;
  292. printk(KERN_INFO "SMP: Total of %d processors activated "
  293. "(%lu.%02lu BogoMIPS).\n",
  294. num_online_cpus(),
  295. bogosum / (500000/HZ),
  296. (bogosum / (5000/HZ)) % 100);
  297. }
  298. void smp_icache_flush_range_others(unsigned long start, unsigned long end)
  299. {
  300. smp_flush_data.start = start;
  301. smp_flush_data.end = end;
  302. preempt_disable();
  303. if (smp_call_function(&ipi_flush_icache, &smp_flush_data, 1))
  304. printk(KERN_WARNING "SMP: failed to run I-cache flush request on other CPUs\n");
  305. preempt_enable();
  306. }
  307. EXPORT_SYMBOL_GPL(smp_icache_flush_range_others);
  308. #ifdef __ARCH_SYNC_CORE_ICACHE
  309. unsigned long icache_invld_count[NR_CPUS];
  310. void resync_core_icache(void)
  311. {
  312. unsigned int cpu = get_cpu();
  313. blackfin_invalidate_entire_icache();
  314. icache_invld_count[cpu]++;
  315. put_cpu();
  316. }
  317. EXPORT_SYMBOL(resync_core_icache);
  318. #endif
  319. #ifdef __ARCH_SYNC_CORE_DCACHE
  320. unsigned long dcache_invld_count[NR_CPUS];
  321. unsigned long barrier_mask __attribute__ ((__section__(".l2.bss")));
  322. void resync_core_dcache(void)
  323. {
  324. unsigned int cpu = get_cpu();
  325. blackfin_invalidate_entire_dcache();
  326. dcache_invld_count[cpu]++;
  327. put_cpu();
  328. }
  329. EXPORT_SYMBOL(resync_core_dcache);
  330. #endif
  331. #ifdef CONFIG_HOTPLUG_CPU
  332. int __cpu_disable(void)
  333. {
  334. unsigned int cpu = smp_processor_id();
  335. if (cpu == 0)
  336. return -EPERM;
  337. set_cpu_online(cpu, false);
  338. return 0;
  339. }
  340. static DECLARE_COMPLETION(cpu_killed);
  341. int __cpu_die(unsigned int cpu)
  342. {
  343. return wait_for_completion_timeout(&cpu_killed, 5000);
  344. }
  345. void cpu_die(void)
  346. {
  347. complete(&cpu_killed);
  348. atomic_dec(&init_mm.mm_users);
  349. atomic_dec(&init_mm.mm_count);
  350. local_irq_disable();
  351. platform_cpu_die();
  352. }
  353. #endif