smp.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 __cpuinitdata initial_pda_coreb;
  46. enum ipi_message_type {
  47. BFIN_IPI_TIMER,
  48. BFIN_IPI_RESCHEDULE,
  49. BFIN_IPI_CALL_FUNC,
  50. BFIN_IPI_CALL_FUNC_SINGLE,
  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. unsigned long count;
  65. unsigned long 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. bfin_ipi_data = &__get_cpu_var(bfin_ipi);
  123. while ((pending = xchg(&bfin_ipi_data->bits, 0)) != 0) {
  124. msg = 0;
  125. do {
  126. msg = find_next_bit(&pending, BITS_PER_LONG, msg + 1);
  127. switch (msg) {
  128. case BFIN_IPI_TIMER:
  129. ipi_timer();
  130. break;
  131. case BFIN_IPI_RESCHEDULE:
  132. scheduler_ipi();
  133. break;
  134. case BFIN_IPI_CALL_FUNC:
  135. generic_smp_call_function_interrupt();
  136. break;
  137. case BFIN_IPI_CALL_FUNC_SINGLE:
  138. generic_smp_call_function_single_interrupt();
  139. break;
  140. case BFIN_IPI_CPU_STOP:
  141. ipi_cpu_stop(cpu);
  142. break;
  143. }
  144. } while (msg < BITS_PER_LONG);
  145. smp_mb();
  146. }
  147. return IRQ_HANDLED;
  148. }
  149. static void bfin_ipi_init(void)
  150. {
  151. unsigned int cpu;
  152. struct ipi_data *bfin_ipi_data;
  153. for_each_possible_cpu(cpu) {
  154. bfin_ipi_data = &per_cpu(bfin_ipi, cpu);
  155. bfin_ipi_data->bits = 0;
  156. bfin_ipi_data->count = 0;
  157. }
  158. }
  159. void send_ipi(const struct cpumask *cpumask, enum ipi_message_type msg)
  160. {
  161. unsigned int cpu;
  162. struct ipi_data *bfin_ipi_data;
  163. unsigned long flags;
  164. local_irq_save(flags);
  165. for_each_cpu(cpu, cpumask) {
  166. bfin_ipi_data = &per_cpu(bfin_ipi, cpu);
  167. smp_mb();
  168. set_bit(msg, &bfin_ipi_data->bits);
  169. 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 __cpuinit __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 __cpuinit 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 __cpuinit 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. cpu_idle();
  273. }
  274. void __init smp_prepare_boot_cpu(void)
  275. {
  276. }
  277. void __init smp_prepare_cpus(unsigned int max_cpus)
  278. {
  279. platform_prepare_cpus(max_cpus);
  280. bfin_ipi_init();
  281. platform_request_ipi(IRQ_SUPPLE_0, ipi_handler_int0);
  282. platform_request_ipi(IRQ_SUPPLE_1, ipi_handler_int1);
  283. }
  284. void __init smp_cpus_done(unsigned int max_cpus)
  285. {
  286. unsigned long bogosum = 0;
  287. unsigned int cpu;
  288. for_each_online_cpu(cpu)
  289. bogosum += loops_per_jiffy;
  290. printk(KERN_INFO "SMP: Total of %d processors activated "
  291. "(%lu.%02lu BogoMIPS).\n",
  292. num_online_cpus(),
  293. bogosum / (500000/HZ),
  294. (bogosum / (5000/HZ)) % 100);
  295. }
  296. void smp_icache_flush_range_others(unsigned long start, unsigned long end)
  297. {
  298. smp_flush_data.start = start;
  299. smp_flush_data.end = end;
  300. preempt_disable();
  301. if (smp_call_function(&ipi_flush_icache, &smp_flush_data, 1))
  302. printk(KERN_WARNING "SMP: failed to run I-cache flush request on other CPUs\n");
  303. preempt_enable();
  304. }
  305. EXPORT_SYMBOL_GPL(smp_icache_flush_range_others);
  306. #ifdef __ARCH_SYNC_CORE_ICACHE
  307. unsigned long icache_invld_count[NR_CPUS];
  308. void resync_core_icache(void)
  309. {
  310. unsigned int cpu = get_cpu();
  311. blackfin_invalidate_entire_icache();
  312. icache_invld_count[cpu]++;
  313. put_cpu();
  314. }
  315. EXPORT_SYMBOL(resync_core_icache);
  316. #endif
  317. #ifdef __ARCH_SYNC_CORE_DCACHE
  318. unsigned long dcache_invld_count[NR_CPUS];
  319. unsigned long barrier_mask __attribute__ ((__section__(".l2.bss")));
  320. void resync_core_dcache(void)
  321. {
  322. unsigned int cpu = get_cpu();
  323. blackfin_invalidate_entire_dcache();
  324. dcache_invld_count[cpu]++;
  325. put_cpu();
  326. }
  327. EXPORT_SYMBOL(resync_core_dcache);
  328. #endif
  329. #ifdef CONFIG_HOTPLUG_CPU
  330. int __cpuexit __cpu_disable(void)
  331. {
  332. unsigned int cpu = smp_processor_id();
  333. if (cpu == 0)
  334. return -EPERM;
  335. set_cpu_online(cpu, false);
  336. return 0;
  337. }
  338. static DECLARE_COMPLETION(cpu_killed);
  339. int __cpuexit __cpu_die(unsigned int cpu)
  340. {
  341. return wait_for_completion_timeout(&cpu_killed, 5000);
  342. }
  343. void cpu_die(void)
  344. {
  345. complete(&cpu_killed);
  346. atomic_dec(&init_mm.mm_users);
  347. atomic_dec(&init_mm.mm_count);
  348. local_irq_disable();
  349. platform_cpu_die();
  350. }
  351. #endif