smp.c 12 KB

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