smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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/profile.h>
  17. #include <linux/errno.h>
  18. #include <linux/mm.h>
  19. #include <linux/cpu.h>
  20. #include <linux/smp.h>
  21. #include <linux/cpumask.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/irq.h>
  24. #include <linux/slab.h>
  25. #include <linux/atomic.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/irq_handler.h>
  28. #include <asm/mmu_context.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/pgalloc.h>
  31. #include <asm/processor.h>
  32. #include <asm/ptrace.h>
  33. #include <asm/cpu.h>
  34. #include <asm/time.h>
  35. #include <linux/err.h>
  36. /*
  37. * Anomaly notes:
  38. * 05000120 - we always define corelock as 32-bit integer in L2
  39. */
  40. struct corelock_slot corelock __attribute__ ((__section__(".l2.bss")));
  41. #ifdef CONFIG_ICACHE_FLUSH_L1
  42. unsigned long blackfin_iflush_l1_entry[NR_CPUS];
  43. #endif
  44. struct blackfin_initial_pda __cpuinitdata initial_pda_coreb;
  45. #define BFIN_IPI_RESCHEDULE 0
  46. #define BFIN_IPI_CALL_FUNC 1
  47. #define BFIN_IPI_CPU_STOP 2
  48. struct blackfin_flush_data {
  49. unsigned long start;
  50. unsigned long end;
  51. };
  52. void *secondary_stack;
  53. struct smp_call_struct {
  54. void (*func)(void *info);
  55. void *info;
  56. int wait;
  57. cpumask_t *waitmask;
  58. };
  59. static struct blackfin_flush_data smp_flush_data;
  60. static DEFINE_SPINLOCK(stop_lock);
  61. struct ipi_message {
  62. unsigned long type;
  63. struct smp_call_struct call_struct;
  64. };
  65. /* A magic number - stress test shows this is safe for common cases */
  66. #define BFIN_IPI_MSGQ_LEN 5
  67. /* Simple FIFO buffer, overflow leads to panic */
  68. struct ipi_message_queue {
  69. spinlock_t lock;
  70. unsigned long count;
  71. unsigned long head; /* head of the queue */
  72. struct ipi_message ipi_message[BFIN_IPI_MSGQ_LEN];
  73. };
  74. static DEFINE_PER_CPU(struct ipi_message_queue, ipi_msg_queue);
  75. static void ipi_cpu_stop(unsigned int cpu)
  76. {
  77. spin_lock(&stop_lock);
  78. printk(KERN_CRIT "CPU%u: stopping\n", cpu);
  79. dump_stack();
  80. spin_unlock(&stop_lock);
  81. set_cpu_online(cpu, false);
  82. local_irq_disable();
  83. while (1)
  84. SSYNC();
  85. }
  86. static void ipi_flush_icache(void *info)
  87. {
  88. struct blackfin_flush_data *fdata = info;
  89. /* Invalidate the memory holding the bounds of the flushed region. */
  90. blackfin_dcache_invalidate_range((unsigned long)fdata,
  91. (unsigned long)fdata + sizeof(*fdata));
  92. /* Make sure all write buffers in the data side of the core
  93. * are flushed before trying to invalidate the icache. This
  94. * needs to be after the data flush and before the icache
  95. * flush so that the SSYNC does the right thing in preventing
  96. * the instruction prefetcher from hitting things in cached
  97. * memory at the wrong time -- it runs much further ahead than
  98. * the pipeline.
  99. */
  100. SSYNC();
  101. /* ipi_flaush_icache is invoked by generic flush_icache_range,
  102. * so call blackfin arch icache flush directly here.
  103. */
  104. blackfin_icache_flush_range(fdata->start, fdata->end);
  105. }
  106. static void ipi_call_function(unsigned int cpu, struct ipi_message *msg)
  107. {
  108. int wait;
  109. void (*func)(void *info);
  110. void *info;
  111. func = msg->call_struct.func;
  112. info = msg->call_struct.info;
  113. wait = msg->call_struct.wait;
  114. func(info);
  115. if (wait) {
  116. #ifdef __ARCH_SYNC_CORE_DCACHE
  117. /*
  118. * 'wait' usually means synchronization between CPUs.
  119. * Invalidate D cache in case shared data was changed
  120. * by func() to ensure cache coherence.
  121. */
  122. resync_core_dcache();
  123. #endif
  124. cpumask_clear_cpu(cpu, msg->call_struct.waitmask);
  125. }
  126. }
  127. /* Use IRQ_SUPPLE_0 to request reschedule.
  128. * When returning from interrupt to user space,
  129. * there is chance to reschedule */
  130. static irqreturn_t ipi_handler_int0(int irq, void *dev_instance)
  131. {
  132. unsigned int cpu = smp_processor_id();
  133. platform_clear_ipi(cpu, IRQ_SUPPLE_0);
  134. return IRQ_HANDLED;
  135. }
  136. static irqreturn_t ipi_handler_int1(int irq, void *dev_instance)
  137. {
  138. struct ipi_message *msg;
  139. struct ipi_message_queue *msg_queue;
  140. unsigned int cpu = smp_processor_id();
  141. unsigned long flags;
  142. platform_clear_ipi(cpu, IRQ_SUPPLE_1);
  143. msg_queue = &__get_cpu_var(ipi_msg_queue);
  144. spin_lock_irqsave(&msg_queue->lock, flags);
  145. while (msg_queue->count) {
  146. msg = &msg_queue->ipi_message[msg_queue->head];
  147. switch (msg->type) {
  148. case BFIN_IPI_RESCHEDULE:
  149. scheduler_ipi();
  150. break;
  151. case BFIN_IPI_CALL_FUNC:
  152. spin_unlock_irqrestore(&msg_queue->lock, flags);
  153. ipi_call_function(cpu, msg);
  154. spin_lock_irqsave(&msg_queue->lock, flags);
  155. break;
  156. case BFIN_IPI_CPU_STOP:
  157. spin_unlock_irqrestore(&msg_queue->lock, flags);
  158. ipi_cpu_stop(cpu);
  159. spin_lock_irqsave(&msg_queue->lock, flags);
  160. break;
  161. default:
  162. printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%lx\n",
  163. cpu, msg->type);
  164. break;
  165. }
  166. msg_queue->head++;
  167. msg_queue->head %= BFIN_IPI_MSGQ_LEN;
  168. msg_queue->count--;
  169. }
  170. spin_unlock_irqrestore(&msg_queue->lock, flags);
  171. return IRQ_HANDLED;
  172. }
  173. static void ipi_queue_init(void)
  174. {
  175. unsigned int cpu;
  176. struct ipi_message_queue *msg_queue;
  177. for_each_possible_cpu(cpu) {
  178. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  179. spin_lock_init(&msg_queue->lock);
  180. msg_queue->count = 0;
  181. msg_queue->head = 0;
  182. }
  183. }
  184. static inline void smp_send_message(cpumask_t callmap, unsigned long type,
  185. void (*func) (void *info), void *info, int wait)
  186. {
  187. unsigned int cpu;
  188. struct ipi_message_queue *msg_queue;
  189. struct ipi_message *msg;
  190. unsigned long flags, next_msg;
  191. cpumask_t waitmask; /* waitmask is shared by all cpus */
  192. cpumask_copy(&waitmask, &callmap);
  193. for_each_cpu(cpu, &callmap) {
  194. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  195. spin_lock_irqsave(&msg_queue->lock, flags);
  196. if (msg_queue->count < BFIN_IPI_MSGQ_LEN) {
  197. next_msg = (msg_queue->head + msg_queue->count)
  198. % BFIN_IPI_MSGQ_LEN;
  199. msg = &msg_queue->ipi_message[next_msg];
  200. msg->type = type;
  201. if (type == BFIN_IPI_CALL_FUNC) {
  202. msg->call_struct.func = func;
  203. msg->call_struct.info = info;
  204. msg->call_struct.wait = wait;
  205. msg->call_struct.waitmask = &waitmask;
  206. }
  207. msg_queue->count++;
  208. } else
  209. panic("IPI message queue overflow\n");
  210. spin_unlock_irqrestore(&msg_queue->lock, flags);
  211. platform_send_ipi_cpu(cpu, IRQ_SUPPLE_1);
  212. }
  213. if (wait) {
  214. while (!cpumask_empty(&waitmask))
  215. blackfin_dcache_invalidate_range(
  216. (unsigned long)(&waitmask),
  217. (unsigned long)(&waitmask));
  218. #ifdef __ARCH_SYNC_CORE_DCACHE
  219. /*
  220. * Invalidate D cache in case shared data was changed by
  221. * other processors to ensure cache coherence.
  222. */
  223. resync_core_dcache();
  224. #endif
  225. }
  226. }
  227. int smp_call_function(void (*func)(void *info), void *info, int wait)
  228. {
  229. cpumask_t callmap;
  230. preempt_disable();
  231. cpumask_copy(&callmap, cpu_online_mask);
  232. cpumask_clear_cpu(smp_processor_id(), &callmap);
  233. if (!cpumask_empty(&callmap))
  234. smp_send_message(callmap, BFIN_IPI_CALL_FUNC, func, info, wait);
  235. preempt_enable();
  236. return 0;
  237. }
  238. EXPORT_SYMBOL_GPL(smp_call_function);
  239. int smp_call_function_single(int cpuid, void (*func) (void *info), void *info,
  240. int wait)
  241. {
  242. unsigned int cpu = cpuid;
  243. cpumask_t callmap;
  244. if (cpu_is_offline(cpu))
  245. return 0;
  246. cpumask_clear(&callmap);
  247. cpumask_set_cpu(cpu, &callmap);
  248. smp_send_message(callmap, BFIN_IPI_CALL_FUNC, func, info, wait);
  249. return 0;
  250. }
  251. EXPORT_SYMBOL_GPL(smp_call_function_single);
  252. void smp_send_reschedule(int cpu)
  253. {
  254. cpumask_t callmap;
  255. /* simply trigger an ipi */
  256. if (cpu_is_offline(cpu))
  257. return;
  258. cpumask_clear(&callmap);
  259. cpumask_set_cpu(cpu, &callmap);
  260. smp_send_message(callmap, BFIN_IPI_RESCHEDULE, NULL, NULL, 0);
  261. return;
  262. }
  263. void smp_send_stop(void)
  264. {
  265. cpumask_t callmap;
  266. preempt_disable();
  267. cpumask_copy(&callmap, cpu_online_mask);
  268. cpumask_clear_cpu(smp_processor_id(), &callmap);
  269. if (!cpumask_empty(&callmap))
  270. smp_send_message(callmap, BFIN_IPI_CPU_STOP, NULL, NULL, 0);
  271. preempt_enable();
  272. return;
  273. }
  274. int __cpuinit __cpu_up(unsigned int cpu)
  275. {
  276. int ret;
  277. static struct task_struct *idle;
  278. if (idle)
  279. free_task(idle);
  280. idle = fork_idle(cpu);
  281. if (IS_ERR(idle)) {
  282. printk(KERN_ERR "CPU%u: fork() failed\n", cpu);
  283. return PTR_ERR(idle);
  284. }
  285. secondary_stack = task_stack_page(idle) + THREAD_SIZE;
  286. ret = platform_boot_secondary(cpu, idle);
  287. secondary_stack = NULL;
  288. return ret;
  289. }
  290. static void __cpuinit setup_secondary(unsigned int cpu)
  291. {
  292. unsigned long ilat;
  293. bfin_write_IMASK(0);
  294. CSYNC();
  295. ilat = bfin_read_ILAT();
  296. CSYNC();
  297. bfin_write_ILAT(ilat);
  298. CSYNC();
  299. /* Enable interrupt levels IVG7-15. IARs have been already
  300. * programmed by the boot CPU. */
  301. bfin_irq_flags |= IMASK_IVG15 |
  302. IMASK_IVG14 | IMASK_IVG13 | IMASK_IVG12 | IMASK_IVG11 |
  303. IMASK_IVG10 | IMASK_IVG9 | IMASK_IVG8 | IMASK_IVG7 | IMASK_IVGHW;
  304. }
  305. void __cpuinit secondary_start_kernel(void)
  306. {
  307. unsigned int cpu = smp_processor_id();
  308. struct mm_struct *mm = &init_mm;
  309. if (_bfin_swrst & SWRST_DBL_FAULT_B) {
  310. printk(KERN_EMERG "CoreB Recovering from DOUBLE FAULT event\n");
  311. #ifdef CONFIG_DEBUG_DOUBLEFAULT
  312. printk(KERN_EMERG " While handling exception (EXCAUSE = %#x) at %pF\n",
  313. initial_pda_coreb.seqstat_doublefault & SEQSTAT_EXCAUSE,
  314. initial_pda_coreb.retx_doublefault);
  315. printk(KERN_NOTICE " DCPLB_FAULT_ADDR: %pF\n",
  316. initial_pda_coreb.dcplb_doublefault_addr);
  317. printk(KERN_NOTICE " ICPLB_FAULT_ADDR: %pF\n",
  318. initial_pda_coreb.icplb_doublefault_addr);
  319. #endif
  320. printk(KERN_NOTICE " The instruction at %pF caused a double exception\n",
  321. initial_pda_coreb.retx);
  322. }
  323. /*
  324. * We want the D-cache to be enabled early, in case the atomic
  325. * support code emulates cache coherence (see
  326. * __ARCH_SYNC_CORE_DCACHE).
  327. */
  328. init_exception_vectors();
  329. local_irq_disable();
  330. /* Attach the new idle task to the global mm. */
  331. atomic_inc(&mm->mm_users);
  332. atomic_inc(&mm->mm_count);
  333. current->active_mm = mm;
  334. preempt_disable();
  335. setup_secondary(cpu);
  336. platform_secondary_init(cpu);
  337. /* setup local core timer */
  338. bfin_local_timer_setup();
  339. local_irq_enable();
  340. bfin_setup_caches(cpu);
  341. /*
  342. * Calibrate loops per jiffy value.
  343. * IRQs need to be enabled here - D-cache can be invalidated
  344. * in timer irq handler, so core B can read correct jiffies.
  345. */
  346. calibrate_delay();
  347. cpu_idle();
  348. }
  349. void __init smp_prepare_boot_cpu(void)
  350. {
  351. }
  352. void __init smp_prepare_cpus(unsigned int max_cpus)
  353. {
  354. platform_prepare_cpus(max_cpus);
  355. ipi_queue_init();
  356. platform_request_ipi(IRQ_SUPPLE_0, ipi_handler_int0);
  357. platform_request_ipi(IRQ_SUPPLE_1, ipi_handler_int1);
  358. }
  359. void __init smp_cpus_done(unsigned int max_cpus)
  360. {
  361. unsigned long bogosum = 0;
  362. unsigned int cpu;
  363. for_each_online_cpu(cpu)
  364. bogosum += loops_per_jiffy;
  365. printk(KERN_INFO "SMP: Total of %d processors activated "
  366. "(%lu.%02lu BogoMIPS).\n",
  367. num_online_cpus(),
  368. bogosum / (500000/HZ),
  369. (bogosum / (5000/HZ)) % 100);
  370. }
  371. void smp_icache_flush_range_others(unsigned long start, unsigned long end)
  372. {
  373. smp_flush_data.start = start;
  374. smp_flush_data.end = end;
  375. if (smp_call_function(&ipi_flush_icache, &smp_flush_data, 0))
  376. printk(KERN_WARNING "SMP: failed to run I-cache flush request on other CPUs\n");
  377. }
  378. EXPORT_SYMBOL_GPL(smp_icache_flush_range_others);
  379. #ifdef __ARCH_SYNC_CORE_ICACHE
  380. unsigned long icache_invld_count[NR_CPUS];
  381. void resync_core_icache(void)
  382. {
  383. unsigned int cpu = get_cpu();
  384. blackfin_invalidate_entire_icache();
  385. icache_invld_count[cpu]++;
  386. put_cpu();
  387. }
  388. EXPORT_SYMBOL(resync_core_icache);
  389. #endif
  390. #ifdef __ARCH_SYNC_CORE_DCACHE
  391. unsigned long dcache_invld_count[NR_CPUS];
  392. unsigned long barrier_mask __attribute__ ((__section__(".l2.bss")));
  393. void resync_core_dcache(void)
  394. {
  395. unsigned int cpu = get_cpu();
  396. blackfin_invalidate_entire_dcache();
  397. dcache_invld_count[cpu]++;
  398. put_cpu();
  399. }
  400. EXPORT_SYMBOL(resync_core_dcache);
  401. #endif
  402. #ifdef CONFIG_HOTPLUG_CPU
  403. int __cpuexit __cpu_disable(void)
  404. {
  405. unsigned int cpu = smp_processor_id();
  406. if (cpu == 0)
  407. return -EPERM;
  408. set_cpu_online(cpu, false);
  409. return 0;
  410. }
  411. static DECLARE_COMPLETION(cpu_killed);
  412. int __cpuexit __cpu_die(unsigned int cpu)
  413. {
  414. return wait_for_completion_timeout(&cpu_killed, 5000);
  415. }
  416. void cpu_die(void)
  417. {
  418. complete(&cpu_killed);
  419. atomic_dec(&init_mm.mm_users);
  420. atomic_dec(&init_mm.mm_count);
  421. local_irq_disable();
  422. platform_cpu_die();
  423. }
  424. #endif