smp.c 12 KB

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