smp.c 12 KB

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