smp.c 11 KB

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