smp.c 12 KB

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