smp.c 12 KB

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