smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 <linux/err.h>
  46. /*
  47. * Anomaly notes:
  48. * 05000120 - we always define corelock as 32-bit integer in L2
  49. */
  50. struct corelock_slot corelock __attribute__ ((__section__(".l2.bss")));
  51. void __cpuinitdata *init_retx_coreb, *init_saved_retx_coreb,
  52. *init_saved_seqstat_coreb, *init_saved_icplb_fault_addr_coreb,
  53. *init_saved_dcplb_fault_addr_coreb;
  54. cpumask_t cpu_possible_map;
  55. EXPORT_SYMBOL(cpu_possible_map);
  56. cpumask_t cpu_online_map;
  57. EXPORT_SYMBOL(cpu_online_map);
  58. #define BFIN_IPI_RESCHEDULE 0
  59. #define BFIN_IPI_CALL_FUNC 1
  60. #define BFIN_IPI_CPU_STOP 2
  61. struct blackfin_flush_data {
  62. unsigned long start;
  63. unsigned long end;
  64. };
  65. void *secondary_stack;
  66. struct smp_call_struct {
  67. void (*func)(void *info);
  68. void *info;
  69. int wait;
  70. cpumask_t pending;
  71. cpumask_t waitmask;
  72. };
  73. static struct blackfin_flush_data smp_flush_data;
  74. static DEFINE_SPINLOCK(stop_lock);
  75. struct ipi_message {
  76. struct list_head list;
  77. unsigned long type;
  78. struct smp_call_struct call_struct;
  79. };
  80. struct ipi_message_queue {
  81. struct list_head head;
  82. spinlock_t lock;
  83. unsigned long count;
  84. };
  85. static DEFINE_PER_CPU(struct ipi_message_queue, ipi_msg_queue);
  86. static void ipi_cpu_stop(unsigned int cpu)
  87. {
  88. spin_lock(&stop_lock);
  89. printk(KERN_CRIT "CPU%u: stopping\n", cpu);
  90. dump_stack();
  91. spin_unlock(&stop_lock);
  92. cpu_clear(cpu, cpu_online_map);
  93. local_irq_disable();
  94. while (1)
  95. SSYNC();
  96. }
  97. static void ipi_flush_icache(void *info)
  98. {
  99. struct blackfin_flush_data *fdata = info;
  100. /* Invalidate the memory holding the bounds of the flushed region. */
  101. blackfin_dcache_invalidate_range((unsigned long)fdata,
  102. (unsigned long)fdata + sizeof(*fdata));
  103. blackfin_icache_flush_range(fdata->start, fdata->end);
  104. }
  105. static void ipi_call_function(unsigned int cpu, struct ipi_message *msg)
  106. {
  107. int wait;
  108. void (*func)(void *info);
  109. void *info;
  110. func = msg->call_struct.func;
  111. info = msg->call_struct.info;
  112. wait = msg->call_struct.wait;
  113. cpu_clear(cpu, msg->call_struct.pending);
  114. func(info);
  115. if (wait)
  116. cpu_clear(cpu, msg->call_struct.waitmask);
  117. else
  118. kfree(msg);
  119. }
  120. static irqreturn_t ipi_handler(int irq, void *dev_instance)
  121. {
  122. struct ipi_message *msg, *mg;
  123. struct ipi_message_queue *msg_queue;
  124. unsigned int cpu = smp_processor_id();
  125. platform_clear_ipi(cpu);
  126. msg_queue = &__get_cpu_var(ipi_msg_queue);
  127. msg_queue->count++;
  128. spin_lock(&msg_queue->lock);
  129. list_for_each_entry_safe(msg, mg, &msg_queue->head, list) {
  130. list_del(&msg->list);
  131. switch (msg->type) {
  132. case BFIN_IPI_RESCHEDULE:
  133. /* That's the easiest one; leave it to
  134. * return_from_int. */
  135. kfree(msg);
  136. break;
  137. case BFIN_IPI_CALL_FUNC:
  138. spin_unlock(&msg_queue->lock);
  139. ipi_call_function(cpu, msg);
  140. spin_lock(&msg_queue->lock);
  141. break;
  142. case BFIN_IPI_CPU_STOP:
  143. spin_unlock(&msg_queue->lock);
  144. ipi_cpu_stop(cpu);
  145. spin_lock(&msg_queue->lock);
  146. kfree(msg);
  147. break;
  148. default:
  149. printk(KERN_CRIT "CPU%u: Unknown IPI message \
  150. 0x%lx\n", cpu, msg->type);
  151. kfree(msg);
  152. break;
  153. }
  154. }
  155. spin_unlock(&msg_queue->lock);
  156. return IRQ_HANDLED;
  157. }
  158. static void ipi_queue_init(void)
  159. {
  160. unsigned int cpu;
  161. struct ipi_message_queue *msg_queue;
  162. for_each_possible_cpu(cpu) {
  163. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  164. INIT_LIST_HEAD(&msg_queue->head);
  165. spin_lock_init(&msg_queue->lock);
  166. msg_queue->count = 0;
  167. }
  168. }
  169. int smp_call_function(void (*func)(void *info), void *info, int wait)
  170. {
  171. unsigned int cpu;
  172. cpumask_t callmap;
  173. unsigned long flags;
  174. struct ipi_message_queue *msg_queue;
  175. struct ipi_message *msg;
  176. callmap = cpu_online_map;
  177. cpu_clear(smp_processor_id(), callmap);
  178. if (cpus_empty(callmap))
  179. return 0;
  180. msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
  181. INIT_LIST_HEAD(&msg->list);
  182. msg->call_struct.func = func;
  183. msg->call_struct.info = info;
  184. msg->call_struct.wait = wait;
  185. msg->call_struct.pending = callmap;
  186. msg->call_struct.waitmask = callmap;
  187. msg->type = BFIN_IPI_CALL_FUNC;
  188. for_each_cpu_mask(cpu, callmap) {
  189. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  190. spin_lock_irqsave(&msg_queue->lock, flags);
  191. list_add(&msg->list, &msg_queue->head);
  192. spin_unlock_irqrestore(&msg_queue->lock, flags);
  193. platform_send_ipi_cpu(cpu);
  194. }
  195. if (wait) {
  196. while (!cpus_empty(msg->call_struct.waitmask))
  197. blackfin_dcache_invalidate_range(
  198. (unsigned long)(&msg->call_struct.waitmask),
  199. (unsigned long)(&msg->call_struct.waitmask));
  200. kfree(msg);
  201. }
  202. return 0;
  203. }
  204. EXPORT_SYMBOL_GPL(smp_call_function);
  205. int smp_call_function_single(int cpuid, void (*func) (void *info), void *info,
  206. int wait)
  207. {
  208. unsigned int cpu = cpuid;
  209. cpumask_t callmap;
  210. unsigned long flags;
  211. struct ipi_message_queue *msg_queue;
  212. struct ipi_message *msg;
  213. if (cpu_is_offline(cpu))
  214. return 0;
  215. cpus_clear(callmap);
  216. cpu_set(cpu, callmap);
  217. msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
  218. INIT_LIST_HEAD(&msg->list);
  219. msg->call_struct.func = func;
  220. msg->call_struct.info = info;
  221. msg->call_struct.wait = wait;
  222. msg->call_struct.pending = callmap;
  223. msg->call_struct.waitmask = callmap;
  224. msg->type = BFIN_IPI_CALL_FUNC;
  225. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  226. spin_lock_irqsave(&msg_queue->lock, flags);
  227. list_add(&msg->list, &msg_queue->head);
  228. spin_unlock_irqrestore(&msg_queue->lock, flags);
  229. platform_send_ipi_cpu(cpu);
  230. if (wait) {
  231. while (!cpus_empty(msg->call_struct.waitmask))
  232. blackfin_dcache_invalidate_range(
  233. (unsigned long)(&msg->call_struct.waitmask),
  234. (unsigned long)(&msg->call_struct.waitmask));
  235. kfree(msg);
  236. }
  237. return 0;
  238. }
  239. EXPORT_SYMBOL_GPL(smp_call_function_single);
  240. void smp_send_reschedule(int cpu)
  241. {
  242. unsigned long flags;
  243. struct ipi_message_queue *msg_queue;
  244. struct ipi_message *msg;
  245. if (cpu_is_offline(cpu))
  246. return;
  247. msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
  248. memset(msg, 0, sizeof(msg));
  249. INIT_LIST_HEAD(&msg->list);
  250. msg->type = BFIN_IPI_RESCHEDULE;
  251. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  252. spin_lock_irqsave(&msg_queue->lock, flags);
  253. list_add(&msg->list, &msg_queue->head);
  254. spin_unlock_irqrestore(&msg_queue->lock, flags);
  255. platform_send_ipi_cpu(cpu);
  256. return;
  257. }
  258. void smp_send_stop(void)
  259. {
  260. unsigned int cpu;
  261. cpumask_t callmap;
  262. unsigned long flags;
  263. struct ipi_message_queue *msg_queue;
  264. struct ipi_message *msg;
  265. callmap = cpu_online_map;
  266. cpu_clear(smp_processor_id(), callmap);
  267. if (cpus_empty(callmap))
  268. return;
  269. msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
  270. memset(msg, 0, sizeof(msg));
  271. INIT_LIST_HEAD(&msg->list);
  272. msg->type = BFIN_IPI_CPU_STOP;
  273. for_each_cpu_mask(cpu, callmap) {
  274. msg_queue = &per_cpu(ipi_msg_queue, cpu);
  275. spin_lock_irqsave(&msg_queue->lock, flags);
  276. list_add(&msg->list, &msg_queue->head);
  277. spin_unlock_irqrestore(&msg_queue->lock, flags);
  278. platform_send_ipi_cpu(cpu);
  279. }
  280. return;
  281. }
  282. int __cpuinit __cpu_up(unsigned int cpu)
  283. {
  284. struct task_struct *idle;
  285. int ret;
  286. idle = fork_idle(cpu);
  287. if (IS_ERR(idle)) {
  288. printk(KERN_ERR "CPU%u: fork() failed\n", cpu);
  289. return PTR_ERR(idle);
  290. }
  291. secondary_stack = task_stack_page(idle) + THREAD_SIZE;
  292. smp_wmb();
  293. ret = platform_boot_secondary(cpu, idle);
  294. if (ret) {
  295. cpu_clear(cpu, cpu_present_map);
  296. printk(KERN_CRIT "CPU%u: processor failed to boot (%d)\n", cpu, ret);
  297. free_task(idle);
  298. } else
  299. cpu_set(cpu, cpu_online_map);
  300. secondary_stack = NULL;
  301. return ret;
  302. }
  303. static void __cpuinit setup_secondary(unsigned int cpu)
  304. {
  305. #if !(defined(CONFIG_TICK_SOURCE_SYSTMR0) || defined(CONFIG_IPIPE))
  306. struct irq_desc *timer_desc;
  307. #endif
  308. unsigned long ilat;
  309. bfin_write_IMASK(0);
  310. CSYNC();
  311. ilat = bfin_read_ILAT();
  312. CSYNC();
  313. bfin_write_ILAT(ilat);
  314. CSYNC();
  315. /* Reserve the PDA space for the secondary CPU. */
  316. reserve_pda();
  317. /* Enable interrupt levels IVG7-15. IARs have been already
  318. * programmed by the boot CPU. */
  319. bfin_irq_flags |= IMASK_IVG15 |
  320. IMASK_IVG14 | IMASK_IVG13 | IMASK_IVG12 | IMASK_IVG11 |
  321. IMASK_IVG10 | IMASK_IVG9 | IMASK_IVG8 | IMASK_IVG7 | IMASK_IVGHW;
  322. #if defined(CONFIG_TICK_SOURCE_SYSTMR0) || defined(CONFIG_IPIPE)
  323. /* Power down the core timer, just to play safe. */
  324. bfin_write_TCNTL(0);
  325. /* system timer0 has been setup by CoreA. */
  326. #else
  327. timer_desc = irq_desc + IRQ_CORETMR;
  328. setup_core_timer();
  329. timer_desc->chip->enable(IRQ_CORETMR);
  330. #endif
  331. }
  332. void __cpuinit secondary_start_kernel(void)
  333. {
  334. unsigned int cpu = smp_processor_id();
  335. struct mm_struct *mm = &init_mm;
  336. if (_bfin_swrst & SWRST_DBL_FAULT_B) {
  337. printk(KERN_EMERG "CoreB Recovering from DOUBLE FAULT event\n");
  338. #ifdef CONFIG_DEBUG_DOUBLEFAULT
  339. printk(KERN_EMERG " While handling exception (EXCAUSE = 0x%x) at %pF\n",
  340. (int)init_saved_seqstat_coreb & SEQSTAT_EXCAUSE, init_saved_retx_coreb);
  341. printk(KERN_NOTICE " DCPLB_FAULT_ADDR: %pF\n", init_saved_dcplb_fault_addr_coreb);
  342. printk(KERN_NOTICE " ICPLB_FAULT_ADDR: %pF\n", init_saved_icplb_fault_addr_coreb);
  343. #endif
  344. printk(KERN_NOTICE " The instruction at %pF caused a double exception\n",
  345. init_retx_coreb);
  346. }
  347. /*
  348. * We want the D-cache to be enabled early, in case the atomic
  349. * support code emulates cache coherence (see
  350. * __ARCH_SYNC_CORE_DCACHE).
  351. */
  352. init_exception_vectors();
  353. bfin_setup_caches(cpu);
  354. local_irq_disable();
  355. /* Attach the new idle task to the global mm. */
  356. atomic_inc(&mm->mm_users);
  357. atomic_inc(&mm->mm_count);
  358. current->active_mm = mm;
  359. BUG_ON(current->mm); /* Can't be, but better be safe than sorry. */
  360. preempt_disable();
  361. setup_secondary(cpu);
  362. local_irq_enable();
  363. platform_secondary_init(cpu);
  364. cpu_idle();
  365. }
  366. void __init smp_prepare_boot_cpu(void)
  367. {
  368. }
  369. void __init smp_prepare_cpus(unsigned int max_cpus)
  370. {
  371. platform_prepare_cpus(max_cpus);
  372. ipi_queue_init();
  373. platform_request_ipi(&ipi_handler);
  374. }
  375. void __init smp_cpus_done(unsigned int max_cpus)
  376. {
  377. unsigned long bogosum = 0;
  378. unsigned int cpu;
  379. for_each_online_cpu(cpu)
  380. bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
  381. printk(KERN_INFO "SMP: Total of %d processors activated "
  382. "(%lu.%02lu BogoMIPS).\n",
  383. num_online_cpus(),
  384. bogosum / (500000/HZ),
  385. (bogosum / (5000/HZ)) % 100);
  386. }
  387. void smp_icache_flush_range_others(unsigned long start, unsigned long end)
  388. {
  389. smp_flush_data.start = start;
  390. smp_flush_data.end = end;
  391. if (smp_call_function(&ipi_flush_icache, &smp_flush_data, 0))
  392. printk(KERN_WARNING "SMP: failed to run I-cache flush request on other CPUs\n");
  393. }
  394. EXPORT_SYMBOL_GPL(smp_icache_flush_range_others);
  395. #ifdef __ARCH_SYNC_CORE_DCACHE
  396. unsigned long barrier_mask __attribute__ ((__section__(".l2.bss")));
  397. void resync_core_dcache(void)
  398. {
  399. unsigned int cpu = get_cpu();
  400. blackfin_invalidate_entire_dcache();
  401. ++per_cpu(cpu_data, cpu).dcache_invld_count;
  402. put_cpu();
  403. }
  404. EXPORT_SYMBOL(resync_core_dcache);
  405. #endif