smp.c 11 KB

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