smp.c 12 KB

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