smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * File: arch/blackfin/kernel/smp.c
  3. * Author: Philippe Gerum <rpm@xenomai.org>
  4. * IPI management based on arch/arm/kernel/smp.c.
  5. *
  6. * Copyright 2007 Analog Devices Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, see the file COPYING, or write
  20. * to the Free Software Foundation, Inc.,
  21. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <linux/module.h>
  24. #include <linux/delay.h>
  25. #include <linux/init.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/sched.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/cache.h>
  30. #include <linux/profile.h>
  31. #include <linux/errno.h>
  32. #include <linux/mm.h>
  33. #include <linux/cpu.h>
  34. #include <linux/smp.h>
  35. #include <linux/seq_file.h>
  36. #include <linux/irq.h>
  37. #include <asm/atomic.h>
  38. #include <asm/cacheflush.h>
  39. #include <asm/mmu_context.h>
  40. #include <asm/pgtable.h>
  41. #include <asm/pgalloc.h>
  42. #include <asm/processor.h>
  43. #include <asm/ptrace.h>
  44. #include <asm/cpu.h>
  45. #include <asm/time.h>
  46. #include <linux/err.h>
  47. /*
  48. * Anomaly notes:
  49. * 05000120 - we always define corelock as 32-bit integer in L2
  50. */
  51. struct corelock_slot corelock __attribute__ ((__section__(".l2.bss")));
  52. void __cpuinitdata *init_retx_coreb, *init_saved_retx_coreb,
  53. *init_saved_seqstat_coreb, *init_saved_icplb_fault_addr_coreb,
  54. *init_saved_dcplb_fault_addr_coreb;
  55. cpumask_t cpu_possible_map;
  56. EXPORT_SYMBOL(cpu_possible_map);
  57. cpumask_t cpu_online_map;
  58. EXPORT_SYMBOL(cpu_online_map);
  59. #define BFIN_IPI_RESCHEDULE 0
  60. #define BFIN_IPI_CALL_FUNC 1
  61. #define BFIN_IPI_CPU_STOP 2
  62. struct blackfin_flush_data {
  63. unsigned long start;
  64. unsigned long end;
  65. };
  66. void *secondary_stack;
  67. struct smp_call_struct {
  68. void (*func)(void *info);
  69. void *info;
  70. int wait;
  71. cpumask_t pending;
  72. cpumask_t waitmask;
  73. };
  74. static struct blackfin_flush_data smp_flush_data;
  75. static DEFINE_SPINLOCK(stop_lock);
  76. struct ipi_message {
  77. struct list_head list;
  78. unsigned long type;
  79. struct smp_call_struct call_struct;
  80. };
  81. struct ipi_message_queue {
  82. struct list_head head;
  83. spinlock_t lock;
  84. unsigned long count;
  85. };
  86. static DEFINE_PER_CPU(struct ipi_message_queue, ipi_msg_queue);
  87. static void ipi_cpu_stop(unsigned int cpu)
  88. {
  89. spin_lock(&stop_lock);
  90. printk(KERN_CRIT "CPU%u: stopping\n", cpu);
  91. dump_stack();
  92. spin_unlock(&stop_lock);
  93. cpu_clear(cpu, cpu_online_map);
  94. local_irq_disable();
  95. while (1)
  96. SSYNC();
  97. }
  98. static void ipi_flush_icache(void *info)
  99. {
  100. struct blackfin_flush_data *fdata = info;
  101. /* Invalidate the memory holding the bounds of the flushed region. */
  102. blackfin_dcache_invalidate_range((unsigned long)fdata,
  103. (unsigned long)fdata + sizeof(*fdata));
  104. blackfin_icache_flush_range(fdata->start, fdata->end);
  105. }
  106. static void ipi_call_function(unsigned int cpu, struct ipi_message *msg)
  107. {
  108. int wait;
  109. void (*func)(void *info);
  110. void *info;
  111. func = msg->call_struct.func;
  112. info = msg->call_struct.info;
  113. wait = msg->call_struct.wait;
  114. cpu_clear(cpu, msg->call_struct.pending);
  115. func(info);
  116. if (wait)
  117. cpu_clear(cpu, msg->call_struct.waitmask);
  118. else
  119. kfree(msg);
  120. }
  121. static irqreturn_t ipi_handler(int irq, void *dev_instance)
  122. {
  123. struct ipi_message *msg;
  124. struct ipi_message_queue *msg_queue;
  125. unsigned int cpu = smp_processor_id();
  126. platform_clear_ipi(cpu);
  127. msg_queue = &__get_cpu_var(ipi_msg_queue);
  128. msg_queue->count++;
  129. spin_lock(&msg_queue->lock);
  130. while (!list_empty(&msg_queue->head)) {
  131. msg = list_entry(msg_queue->head.next, typeof(*msg), list);
  132. list_del(&msg->list);
  133. switch (msg->type) {
  134. case BFIN_IPI_RESCHEDULE:
  135. /* That's the easiest one; leave it to
  136. * return_from_int. */
  137. kfree(msg);
  138. break;
  139. case BFIN_IPI_CALL_FUNC:
  140. spin_unlock(&msg_queue->lock);
  141. ipi_call_function(cpu, msg);
  142. spin_lock(&msg_queue->lock);
  143. break;
  144. case BFIN_IPI_CPU_STOP:
  145. spin_unlock(&msg_queue->lock);
  146. ipi_cpu_stop(cpu);
  147. spin_lock(&msg_queue->lock);
  148. kfree(msg);
  149. break;
  150. default:
  151. printk(KERN_CRIT "CPU%u: Unknown IPI message \
  152. 0x%lx\n", cpu, msg->type);
  153. kfree(msg);
  154. break;
  155. }
  156. }
  157. spin_unlock(&msg_queue->lock);
  158. return IRQ_HANDLED;
  159. }
  160. static void ipi_queue_init(void)
  161. {
  162. unsigned int cpu;
  163. struct ipi_message_queue *msg_queue;
  164. for_each_possible_cpu(cpu) {
  165. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  166. INIT_LIST_HEAD(&msg_queue->head);
  167. spin_lock_init(&msg_queue->lock);
  168. msg_queue->count = 0;
  169. }
  170. }
  171. int smp_call_function(void (*func)(void *info), void *info, int wait)
  172. {
  173. unsigned int cpu;
  174. cpumask_t callmap;
  175. unsigned long flags;
  176. struct ipi_message_queue *msg_queue;
  177. struct ipi_message *msg;
  178. callmap = cpu_online_map;
  179. cpu_clear(smp_processor_id(), callmap);
  180. if (cpus_empty(callmap))
  181. return 0;
  182. msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
  183. if (!msg)
  184. return -ENOMEM;
  185. INIT_LIST_HEAD(&msg->list);
  186. msg->call_struct.func = func;
  187. msg->call_struct.info = info;
  188. msg->call_struct.wait = wait;
  189. msg->call_struct.pending = callmap;
  190. msg->call_struct.waitmask = callmap;
  191. msg->type = BFIN_IPI_CALL_FUNC;
  192. for_each_cpu_mask(cpu, callmap) {
  193. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  194. spin_lock_irqsave(&msg_queue->lock, flags);
  195. list_add_tail(&msg->list, &msg_queue->head);
  196. spin_unlock_irqrestore(&msg_queue->lock, flags);
  197. platform_send_ipi_cpu(cpu);
  198. }
  199. if (wait) {
  200. while (!cpus_empty(msg->call_struct.waitmask))
  201. blackfin_dcache_invalidate_range(
  202. (unsigned long)(&msg->call_struct.waitmask),
  203. (unsigned long)(&msg->call_struct.waitmask));
  204. kfree(msg);
  205. }
  206. return 0;
  207. }
  208. EXPORT_SYMBOL_GPL(smp_call_function);
  209. int smp_call_function_single(int cpuid, void (*func) (void *info), void *info,
  210. int wait)
  211. {
  212. unsigned int cpu = cpuid;
  213. cpumask_t callmap;
  214. unsigned long flags;
  215. struct ipi_message_queue *msg_queue;
  216. struct ipi_message *msg;
  217. if (cpu_is_offline(cpu))
  218. return 0;
  219. cpus_clear(callmap);
  220. cpu_set(cpu, callmap);
  221. msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
  222. if (!msg)
  223. return -ENOMEM;
  224. INIT_LIST_HEAD(&msg->list);
  225. msg->call_struct.func = func;
  226. msg->call_struct.info = info;
  227. msg->call_struct.wait = wait;
  228. msg->call_struct.pending = callmap;
  229. msg->call_struct.waitmask = callmap;
  230. msg->type = BFIN_IPI_CALL_FUNC;
  231. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  232. spin_lock_irqsave(&msg_queue->lock, flags);
  233. list_add_tail(&msg->list, &msg_queue->head);
  234. spin_unlock_irqrestore(&msg_queue->lock, flags);
  235. platform_send_ipi_cpu(cpu);
  236. if (wait) {
  237. while (!cpus_empty(msg->call_struct.waitmask))
  238. blackfin_dcache_invalidate_range(
  239. (unsigned long)(&msg->call_struct.waitmask),
  240. (unsigned long)(&msg->call_struct.waitmask));
  241. kfree(msg);
  242. }
  243. return 0;
  244. }
  245. EXPORT_SYMBOL_GPL(smp_call_function_single);
  246. void smp_send_reschedule(int cpu)
  247. {
  248. unsigned long flags;
  249. struct ipi_message_queue *msg_queue;
  250. struct ipi_message *msg;
  251. if (cpu_is_offline(cpu))
  252. return;
  253. msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
  254. if (!msg)
  255. return;
  256. memset(msg, 0, sizeof(msg));
  257. INIT_LIST_HEAD(&msg->list);
  258. msg->type = BFIN_IPI_RESCHEDULE;
  259. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  260. spin_lock_irqsave(&msg_queue->lock, flags);
  261. list_add_tail(&msg->list, &msg_queue->head);
  262. spin_unlock_irqrestore(&msg_queue->lock, flags);
  263. platform_send_ipi_cpu(cpu);
  264. return;
  265. }
  266. void smp_send_stop(void)
  267. {
  268. unsigned int cpu;
  269. cpumask_t callmap;
  270. unsigned long flags;
  271. struct ipi_message_queue *msg_queue;
  272. struct ipi_message *msg;
  273. callmap = cpu_online_map;
  274. cpu_clear(smp_processor_id(), callmap);
  275. if (cpus_empty(callmap))
  276. return;
  277. msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
  278. if (!msg)
  279. return;
  280. memset(msg, 0, sizeof(msg));
  281. INIT_LIST_HEAD(&msg->list);
  282. msg->type = BFIN_IPI_CPU_STOP;
  283. for_each_cpu_mask(cpu, callmap) {
  284. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  285. spin_lock_irqsave(&msg_queue->lock, flags);
  286. list_add_tail(&msg->list, &msg_queue->head);
  287. spin_unlock_irqrestore(&msg_queue->lock, flags);
  288. platform_send_ipi_cpu(cpu);
  289. }
  290. return;
  291. }
  292. int __cpuinit __cpu_up(unsigned int cpu)
  293. {
  294. struct task_struct *idle;
  295. int ret;
  296. idle = fork_idle(cpu);
  297. if (IS_ERR(idle)) {
  298. printk(KERN_ERR "CPU%u: fork() failed\n", cpu);
  299. return PTR_ERR(idle);
  300. }
  301. secondary_stack = task_stack_page(idle) + THREAD_SIZE;
  302. smp_wmb();
  303. ret = platform_boot_secondary(cpu, idle);
  304. if (ret) {
  305. cpu_clear(cpu, cpu_present_map);
  306. printk(KERN_CRIT "CPU%u: processor failed to boot (%d)\n", cpu, ret);
  307. free_task(idle);
  308. } else
  309. cpu_set(cpu, cpu_online_map);
  310. secondary_stack = NULL;
  311. return ret;
  312. }
  313. static void __cpuinit setup_secondary(unsigned int cpu)
  314. {
  315. #if !defined(CONFIG_TICKSOURCE_GPTMR0)
  316. struct irq_desc *timer_desc;
  317. #endif
  318. unsigned long ilat;
  319. bfin_write_IMASK(0);
  320. CSYNC();
  321. ilat = bfin_read_ILAT();
  322. CSYNC();
  323. bfin_write_ILAT(ilat);
  324. CSYNC();
  325. /* Enable interrupt levels IVG7-15. IARs have been already
  326. * programmed by the boot CPU. */
  327. bfin_irq_flags |= IMASK_IVG15 |
  328. IMASK_IVG14 | IMASK_IVG13 | IMASK_IVG12 | IMASK_IVG11 |
  329. IMASK_IVG10 | IMASK_IVG9 | IMASK_IVG8 | IMASK_IVG7 | IMASK_IVGHW;
  330. #if defined(CONFIG_TICKSOURCE_GPTMR0)
  331. /* Power down the core timer, just to play safe. */
  332. bfin_write_TCNTL(0);
  333. /* system timer0 has been setup by CoreA. */
  334. #else
  335. timer_desc = irq_desc + IRQ_CORETMR;
  336. setup_core_timer();
  337. timer_desc->chip->enable(IRQ_CORETMR);
  338. #endif
  339. }
  340. void __cpuinit secondary_start_kernel(void)
  341. {
  342. unsigned int cpu = smp_processor_id();
  343. struct mm_struct *mm = &init_mm;
  344. if (_bfin_swrst & SWRST_DBL_FAULT_B) {
  345. printk(KERN_EMERG "CoreB Recovering from DOUBLE FAULT event\n");
  346. #ifdef CONFIG_DEBUG_DOUBLEFAULT
  347. printk(KERN_EMERG " While handling exception (EXCAUSE = 0x%x) at %pF\n",
  348. (int)init_saved_seqstat_coreb & SEQSTAT_EXCAUSE, init_saved_retx_coreb);
  349. printk(KERN_NOTICE " DCPLB_FAULT_ADDR: %pF\n", init_saved_dcplb_fault_addr_coreb);
  350. printk(KERN_NOTICE " ICPLB_FAULT_ADDR: %pF\n", init_saved_icplb_fault_addr_coreb);
  351. #endif
  352. printk(KERN_NOTICE " The instruction at %pF caused a double exception\n",
  353. init_retx_coreb);
  354. }
  355. /*
  356. * We want the D-cache to be enabled early, in case the atomic
  357. * support code emulates cache coherence (see
  358. * __ARCH_SYNC_CORE_DCACHE).
  359. */
  360. init_exception_vectors();
  361. bfin_setup_caches(cpu);
  362. local_irq_disable();
  363. /* Attach the new idle task to the global mm. */
  364. atomic_inc(&mm->mm_users);
  365. atomic_inc(&mm->mm_count);
  366. current->active_mm = mm;
  367. BUG_ON(current->mm); /* Can't be, but better be safe than sorry. */
  368. preempt_disable();
  369. setup_secondary(cpu);
  370. local_irq_enable();
  371. platform_secondary_init(cpu);
  372. cpu_idle();
  373. }
  374. void __init smp_prepare_boot_cpu(void)
  375. {
  376. }
  377. void __init smp_prepare_cpus(unsigned int max_cpus)
  378. {
  379. platform_prepare_cpus(max_cpus);
  380. ipi_queue_init();
  381. platform_request_ipi(&ipi_handler);
  382. }
  383. void __init smp_cpus_done(unsigned int max_cpus)
  384. {
  385. unsigned long bogosum = 0;
  386. unsigned int cpu;
  387. for_each_online_cpu(cpu)
  388. bogosum += loops_per_jiffy;
  389. printk(KERN_INFO "SMP: Total of %d processors activated "
  390. "(%lu.%02lu BogoMIPS).\n",
  391. num_online_cpus(),
  392. bogosum / (500000/HZ),
  393. (bogosum / (5000/HZ)) % 100);
  394. }
  395. void smp_icache_flush_range_others(unsigned long start, unsigned long end)
  396. {
  397. smp_flush_data.start = start;
  398. smp_flush_data.end = end;
  399. if (smp_call_function(&ipi_flush_icache, &smp_flush_data, 0))
  400. printk(KERN_WARNING "SMP: failed to run I-cache flush request on other CPUs\n");
  401. }
  402. EXPORT_SYMBOL_GPL(smp_icache_flush_range_others);
  403. #ifdef __ARCH_SYNC_CORE_ICACHE
  404. void resync_core_icache(void)
  405. {
  406. unsigned int cpu = get_cpu();
  407. blackfin_invalidate_entire_icache();
  408. ++per_cpu(cpu_data, cpu).icache_invld_count;
  409. put_cpu();
  410. }
  411. EXPORT_SYMBOL(resync_core_icache);
  412. #endif
  413. #ifdef __ARCH_SYNC_CORE_DCACHE
  414. unsigned long barrier_mask __attribute__ ((__section__(".l2.bss")));
  415. void resync_core_dcache(void)
  416. {
  417. unsigned int cpu = get_cpu();
  418. blackfin_invalidate_entire_dcache();
  419. ++per_cpu(cpu_data, cpu).dcache_invld_count;
  420. put_cpu();
  421. }
  422. EXPORT_SYMBOL(resync_core_dcache);
  423. #endif