leon_smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* leon_smp.c: Sparc-Leon SMP support.
  2. *
  3. * based on sun4m_smp.c
  4. * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 2009 Daniel Hellstrom (daniel@gaisler.com) Aeroflex Gaisler AB
  6. * Copyright (C) 2009 Konrad Eisele (konrad@gaisler.com) Aeroflex Gaisler AB
  7. */
  8. #include <asm/head.h>
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/threads.h>
  12. #include <linux/smp.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel_stat.h>
  15. #include <linux/of.h>
  16. #include <linux/init.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/mm.h>
  19. #include <linux/swap.h>
  20. #include <linux/profile.h>
  21. #include <linux/pm.h>
  22. #include <linux/delay.h>
  23. #include <linux/gfp.h>
  24. #include <linux/cpu.h>
  25. #include <linux/clockchips.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/tlbflush.h>
  28. #include <asm/ptrace.h>
  29. #include <linux/atomic.h>
  30. #include <asm/irq_regs.h>
  31. #include <asm/traps.h>
  32. #include <asm/delay.h>
  33. #include <asm/irq.h>
  34. #include <asm/page.h>
  35. #include <asm/pgalloc.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/oplib.h>
  38. #include <asm/cpudata.h>
  39. #include <asm/asi.h>
  40. #include <asm/leon.h>
  41. #include <asm/leon_amba.h>
  42. #include <asm/timer.h>
  43. #include "kernel.h"
  44. #include "irq.h"
  45. extern ctxd_t *srmmu_ctx_table_phys;
  46. static int smp_processors_ready;
  47. extern volatile unsigned long cpu_callin_map[NR_CPUS];
  48. extern cpumask_t smp_commenced_mask;
  49. void __cpuinit leon_configure_cache_smp(void);
  50. static void leon_ipi_init(void);
  51. /* IRQ number of LEON IPIs */
  52. int leon_ipi_irq = LEON3_IRQ_IPI_DEFAULT;
  53. static inline unsigned long do_swap(volatile unsigned long *ptr,
  54. unsigned long val)
  55. {
  56. __asm__ __volatile__("swapa [%2] %3, %0\n\t" : "=&r"(val)
  57. : "0"(val), "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
  58. : "memory");
  59. return val;
  60. }
  61. void __cpuinit leon_cpu_pre_starting(void *arg)
  62. {
  63. leon_configure_cache_smp();
  64. }
  65. void __cpuinit leon_cpu_pre_online(void *arg)
  66. {
  67. int cpuid = hard_smp_processor_id();
  68. /* Allow master to continue. The master will then give us the
  69. * go-ahead by setting the smp_commenced_mask and will wait without
  70. * timeouts until our setup is completed fully (signified by
  71. * our bit being set in the cpu_online_mask).
  72. */
  73. do_swap(&cpu_callin_map[cpuid], 1);
  74. local_ops->cache_all();
  75. local_ops->tlb_all();
  76. /* Fix idle thread fields. */
  77. __asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(&current_set[cpuid])
  78. : "memory" /* paranoid */);
  79. /* Attach to the address space of init_task. */
  80. atomic_inc(&init_mm.mm_count);
  81. current->active_mm = &init_mm;
  82. while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
  83. mb();
  84. }
  85. /*
  86. * Cycle through the processors asking the PROM to start each one.
  87. */
  88. extern struct linux_prom_registers smp_penguin_ctable;
  89. void __cpuinit leon_configure_cache_smp(void)
  90. {
  91. unsigned long cfg = sparc_leon3_get_dcachecfg();
  92. int me = smp_processor_id();
  93. if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg) > 4) {
  94. printk(KERN_INFO "Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n",
  95. (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg),
  96. (unsigned int)cfg, (unsigned int)me);
  97. sparc_leon3_disable_cache();
  98. } else {
  99. if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) {
  100. sparc_leon3_enable_snooping();
  101. } else {
  102. printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
  103. me);
  104. sparc_leon3_disable_cache();
  105. }
  106. }
  107. local_ops->cache_all();
  108. local_ops->tlb_all();
  109. }
  110. void leon_smp_setbroadcast(unsigned int mask)
  111. {
  112. int broadcast =
  113. ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
  114. LEON3_IRQMPSTATUS_BROADCAST) & 1);
  115. if (!broadcast) {
  116. prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
  117. leon_smp_nrcpus());
  118. if (leon_smp_nrcpus() > 1) {
  119. BUG();
  120. } else {
  121. prom_printf("continue anyway\n");
  122. return;
  123. }
  124. }
  125. LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask);
  126. }
  127. unsigned int leon_smp_getbroadcast(void)
  128. {
  129. unsigned int mask;
  130. mask = LEON_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpbroadcast));
  131. return mask;
  132. }
  133. int leon_smp_nrcpus(void)
  134. {
  135. int nrcpu =
  136. ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
  137. LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
  138. return nrcpu;
  139. }
  140. void __init leon_boot_cpus(void)
  141. {
  142. int nrcpu = leon_smp_nrcpus();
  143. int me = smp_processor_id();
  144. /* Setup IPI */
  145. leon_ipi_init();
  146. printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me,
  147. (unsigned int)nrcpu, (unsigned int)NR_CPUS,
  148. (unsigned int)&(leon3_irqctrl_regs->mpstatus));
  149. leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me);
  150. leon_enable_irq_cpu(LEON3_IRQ_TICKER, me);
  151. leon_enable_irq_cpu(leon_ipi_irq, me);
  152. leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
  153. leon_configure_cache_smp();
  154. local_ops->cache_all();
  155. }
  156. int __cpuinit leon_boot_one_cpu(int i, struct task_struct *idle)
  157. {
  158. int timeout;
  159. current_set[i] = task_thread_info(idle);
  160. /* See trampoline.S:leon_smp_cpu_startup for details...
  161. * Initialize the contexts table
  162. * Since the call to prom_startcpu() trashes the structure,
  163. * we need to re-initialize it for each cpu
  164. */
  165. smp_penguin_ctable.which_io = 0;
  166. smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys;
  167. smp_penguin_ctable.reg_size = 0;
  168. /* whirrr, whirrr, whirrrrrrrrr... */
  169. printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i,
  170. (unsigned int)&leon3_irqctrl_regs->mpstatus);
  171. local_ops->cache_all();
  172. /* Make sure all IRQs are of from the start for this new CPU */
  173. LEON_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[i], 0);
  174. /* Wake one CPU */
  175. LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
  176. /* wheee... it's going... */
  177. for (timeout = 0; timeout < 10000; timeout++) {
  178. if (cpu_callin_map[i])
  179. break;
  180. udelay(200);
  181. }
  182. printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
  183. if (!(cpu_callin_map[i])) {
  184. printk(KERN_ERR "Processor %d is stuck.\n", i);
  185. return -ENODEV;
  186. } else {
  187. leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i);
  188. leon_enable_irq_cpu(LEON3_IRQ_TICKER, i);
  189. leon_enable_irq_cpu(leon_ipi_irq, i);
  190. }
  191. local_ops->cache_all();
  192. return 0;
  193. }
  194. void __init leon_smp_done(void)
  195. {
  196. int i, first;
  197. int *prev;
  198. /* setup cpu list for irq rotation */
  199. first = 0;
  200. prev = &first;
  201. for (i = 0; i < NR_CPUS; i++) {
  202. if (cpu_online(i)) {
  203. *prev = i;
  204. prev = &cpu_data(i).next;
  205. }
  206. }
  207. *prev = first;
  208. local_ops->cache_all();
  209. /* Free unneeded trap tables */
  210. if (!cpu_present(1)) {
  211. ClearPageReserved(virt_to_page(&trapbase_cpu1));
  212. init_page_count(virt_to_page(&trapbase_cpu1));
  213. free_page((unsigned long)&trapbase_cpu1);
  214. totalram_pages++;
  215. num_physpages++;
  216. }
  217. if (!cpu_present(2)) {
  218. ClearPageReserved(virt_to_page(&trapbase_cpu2));
  219. init_page_count(virt_to_page(&trapbase_cpu2));
  220. free_page((unsigned long)&trapbase_cpu2);
  221. totalram_pages++;
  222. num_physpages++;
  223. }
  224. if (!cpu_present(3)) {
  225. ClearPageReserved(virt_to_page(&trapbase_cpu3));
  226. init_page_count(virt_to_page(&trapbase_cpu3));
  227. free_page((unsigned long)&trapbase_cpu3);
  228. totalram_pages++;
  229. num_physpages++;
  230. }
  231. /* Ok, they are spinning and ready to go. */
  232. smp_processors_ready = 1;
  233. }
  234. void leon_irq_rotate(int cpu)
  235. {
  236. }
  237. struct leon_ipi_work {
  238. int single;
  239. int msk;
  240. int resched;
  241. };
  242. static DEFINE_PER_CPU_SHARED_ALIGNED(struct leon_ipi_work, leon_ipi_work);
  243. /* Initialize IPIs on the LEON, in order to save IRQ resources only one IRQ
  244. * is used for all three types of IPIs.
  245. */
  246. static void __init leon_ipi_init(void)
  247. {
  248. int cpu, len;
  249. struct leon_ipi_work *work;
  250. struct property *pp;
  251. struct device_node *rootnp;
  252. struct tt_entry *trap_table;
  253. unsigned long flags;
  254. /* Find IPI IRQ or stick with default value */
  255. rootnp = of_find_node_by_path("/ambapp0");
  256. if (rootnp) {
  257. pp = of_find_property(rootnp, "ipi_num", &len);
  258. if (pp && (*(int *)pp->value))
  259. leon_ipi_irq = *(int *)pp->value;
  260. }
  261. printk(KERN_INFO "leon: SMP IPIs at IRQ %d\n", leon_ipi_irq);
  262. /* Adjust so that we jump directly to smpleon_ipi */
  263. local_irq_save(flags);
  264. trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_ipi_irq - 1)];
  265. trap_table->inst_three += smpleon_ipi - real_irq_entry;
  266. local_ops->cache_all();
  267. local_irq_restore(flags);
  268. for_each_possible_cpu(cpu) {
  269. work = &per_cpu(leon_ipi_work, cpu);
  270. work->single = work->msk = work->resched = 0;
  271. }
  272. }
  273. static void leon_send_ipi(int cpu, int level)
  274. {
  275. unsigned long mask;
  276. mask = leon_get_irqmask(level);
  277. LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
  278. }
  279. static void leon_ipi_single(int cpu)
  280. {
  281. struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
  282. /* Mark work */
  283. work->single = 1;
  284. /* Generate IRQ on the CPU */
  285. leon_send_ipi(cpu, leon_ipi_irq);
  286. }
  287. static void leon_ipi_mask_one(int cpu)
  288. {
  289. struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
  290. /* Mark work */
  291. work->msk = 1;
  292. /* Generate IRQ on the CPU */
  293. leon_send_ipi(cpu, leon_ipi_irq);
  294. }
  295. static void leon_ipi_resched(int cpu)
  296. {
  297. struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
  298. /* Mark work */
  299. work->resched = 1;
  300. /* Generate IRQ on the CPU (any IRQ will cause resched) */
  301. leon_send_ipi(cpu, leon_ipi_irq);
  302. }
  303. void leonsmp_ipi_interrupt(void)
  304. {
  305. struct leon_ipi_work *work = &__get_cpu_var(leon_ipi_work);
  306. if (work->single) {
  307. work->single = 0;
  308. smp_call_function_single_interrupt();
  309. }
  310. if (work->msk) {
  311. work->msk = 0;
  312. smp_call_function_interrupt();
  313. }
  314. if (work->resched) {
  315. work->resched = 0;
  316. smp_resched_interrupt();
  317. }
  318. }
  319. static struct smp_funcall {
  320. smpfunc_t func;
  321. unsigned long arg1;
  322. unsigned long arg2;
  323. unsigned long arg3;
  324. unsigned long arg4;
  325. unsigned long arg5;
  326. unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
  327. unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
  328. } ccall_info;
  329. static DEFINE_SPINLOCK(cross_call_lock);
  330. /* Cross calls must be serialized, at least currently. */
  331. static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
  332. unsigned long arg2, unsigned long arg3,
  333. unsigned long arg4)
  334. {
  335. if (smp_processors_ready) {
  336. register int high = NR_CPUS - 1;
  337. unsigned long flags;
  338. spin_lock_irqsave(&cross_call_lock, flags);
  339. {
  340. /* If you make changes here, make sure gcc generates proper code... */
  341. register smpfunc_t f asm("i0") = func;
  342. register unsigned long a1 asm("i1") = arg1;
  343. register unsigned long a2 asm("i2") = arg2;
  344. register unsigned long a3 asm("i3") = arg3;
  345. register unsigned long a4 asm("i4") = arg4;
  346. register unsigned long a5 asm("i5") = 0;
  347. __asm__ __volatile__("std %0, [%6]\n\t"
  348. "std %2, [%6 + 8]\n\t"
  349. "std %4, [%6 + 16]\n\t" : :
  350. "r"(f), "r"(a1), "r"(a2), "r"(a3),
  351. "r"(a4), "r"(a5),
  352. "r"(&ccall_info.func));
  353. }
  354. /* Init receive/complete mapping, plus fire the IPI's off. */
  355. {
  356. register int i;
  357. cpumask_clear_cpu(smp_processor_id(), &mask);
  358. cpumask_and(&mask, cpu_online_mask, &mask);
  359. for (i = 0; i <= high; i++) {
  360. if (cpumask_test_cpu(i, &mask)) {
  361. ccall_info.processors_in[i] = 0;
  362. ccall_info.processors_out[i] = 0;
  363. leon_send_ipi(i, LEON3_IRQ_CROSS_CALL);
  364. }
  365. }
  366. }
  367. {
  368. register int i;
  369. i = 0;
  370. do {
  371. if (!cpumask_test_cpu(i, &mask))
  372. continue;
  373. while (!ccall_info.processors_in[i])
  374. barrier();
  375. } while (++i <= high);
  376. i = 0;
  377. do {
  378. if (!cpumask_test_cpu(i, &mask))
  379. continue;
  380. while (!ccall_info.processors_out[i])
  381. barrier();
  382. } while (++i <= high);
  383. }
  384. spin_unlock_irqrestore(&cross_call_lock, flags);
  385. }
  386. }
  387. /* Running cross calls. */
  388. void leon_cross_call_irq(void)
  389. {
  390. int i = smp_processor_id();
  391. ccall_info.processors_in[i] = 1;
  392. ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
  393. ccall_info.arg4, ccall_info.arg5);
  394. ccall_info.processors_out[i] = 1;
  395. }
  396. static const struct sparc32_ipi_ops leon_ipi_ops = {
  397. .cross_call = leon_cross_call,
  398. .resched = leon_ipi_resched,
  399. .single = leon_ipi_single,
  400. .mask_one = leon_ipi_mask_one,
  401. };
  402. void __init leon_init_smp(void)
  403. {
  404. /* Patch ipi15 trap table */
  405. t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
  406. sparc32_ipi_ops = &leon_ipi_ops;
  407. }