leon_smp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 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 leon_cpu_pre_starting(void *arg)
  62. {
  63. leon_configure_cache_smp();
  64. }
  65. void 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 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. static 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. int leon_smp_nrcpus(void)
  128. {
  129. int nrcpu =
  130. ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
  131. LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
  132. return nrcpu;
  133. }
  134. void __init leon_boot_cpus(void)
  135. {
  136. int nrcpu = leon_smp_nrcpus();
  137. int me = smp_processor_id();
  138. /* Setup IPI */
  139. leon_ipi_init();
  140. printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me,
  141. (unsigned int)nrcpu, (unsigned int)NR_CPUS,
  142. (unsigned int)&(leon3_irqctrl_regs->mpstatus));
  143. leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me);
  144. leon_enable_irq_cpu(LEON3_IRQ_TICKER, me);
  145. leon_enable_irq_cpu(leon_ipi_irq, me);
  146. leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
  147. leon_configure_cache_smp();
  148. local_ops->cache_all();
  149. }
  150. int leon_boot_one_cpu(int i, struct task_struct *idle)
  151. {
  152. int timeout;
  153. current_set[i] = task_thread_info(idle);
  154. /* See trampoline.S:leon_smp_cpu_startup for details...
  155. * Initialize the contexts table
  156. * Since the call to prom_startcpu() trashes the structure,
  157. * we need to re-initialize it for each cpu
  158. */
  159. smp_penguin_ctable.which_io = 0;
  160. smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys;
  161. smp_penguin_ctable.reg_size = 0;
  162. /* whirrr, whirrr, whirrrrrrrrr... */
  163. printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i,
  164. (unsigned int)&leon3_irqctrl_regs->mpstatus);
  165. local_ops->cache_all();
  166. /* Make sure all IRQs are of from the start for this new CPU */
  167. LEON_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[i], 0);
  168. /* Wake one CPU */
  169. LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
  170. /* wheee... it's going... */
  171. for (timeout = 0; timeout < 10000; timeout++) {
  172. if (cpu_callin_map[i])
  173. break;
  174. udelay(200);
  175. }
  176. printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
  177. if (!(cpu_callin_map[i])) {
  178. printk(KERN_ERR "Processor %d is stuck.\n", i);
  179. return -ENODEV;
  180. } else {
  181. leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i);
  182. leon_enable_irq_cpu(LEON3_IRQ_TICKER, i);
  183. leon_enable_irq_cpu(leon_ipi_irq, i);
  184. }
  185. local_ops->cache_all();
  186. return 0;
  187. }
  188. void __init leon_smp_done(void)
  189. {
  190. int i, first;
  191. int *prev;
  192. /* setup cpu list for irq rotation */
  193. first = 0;
  194. prev = &first;
  195. for (i = 0; i < NR_CPUS; i++) {
  196. if (cpu_online(i)) {
  197. *prev = i;
  198. prev = &cpu_data(i).next;
  199. }
  200. }
  201. *prev = first;
  202. local_ops->cache_all();
  203. /* Free unneeded trap tables */
  204. if (!cpu_present(1)) {
  205. free_reserved_page(virt_to_page(&trapbase_cpu1));
  206. }
  207. if (!cpu_present(2)) {
  208. free_reserved_page(virt_to_page(&trapbase_cpu2));
  209. }
  210. if (!cpu_present(3)) {
  211. free_reserved_page(virt_to_page(&trapbase_cpu3));
  212. }
  213. /* Ok, they are spinning and ready to go. */
  214. smp_processors_ready = 1;
  215. }
  216. struct leon_ipi_work {
  217. int single;
  218. int msk;
  219. int resched;
  220. };
  221. static DEFINE_PER_CPU_SHARED_ALIGNED(struct leon_ipi_work, leon_ipi_work);
  222. /* Initialize IPIs on the LEON, in order to save IRQ resources only one IRQ
  223. * is used for all three types of IPIs.
  224. */
  225. static void __init leon_ipi_init(void)
  226. {
  227. int cpu, len;
  228. struct leon_ipi_work *work;
  229. struct property *pp;
  230. struct device_node *rootnp;
  231. struct tt_entry *trap_table;
  232. unsigned long flags;
  233. /* Find IPI IRQ or stick with default value */
  234. rootnp = of_find_node_by_path("/ambapp0");
  235. if (rootnp) {
  236. pp = of_find_property(rootnp, "ipi_num", &len);
  237. if (pp && (*(int *)pp->value))
  238. leon_ipi_irq = *(int *)pp->value;
  239. }
  240. printk(KERN_INFO "leon: SMP IPIs at IRQ %d\n", leon_ipi_irq);
  241. /* Adjust so that we jump directly to smpleon_ipi */
  242. local_irq_save(flags);
  243. trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_ipi_irq - 1)];
  244. trap_table->inst_three += smpleon_ipi - real_irq_entry;
  245. local_ops->cache_all();
  246. local_irq_restore(flags);
  247. for_each_possible_cpu(cpu) {
  248. work = &per_cpu(leon_ipi_work, cpu);
  249. work->single = work->msk = work->resched = 0;
  250. }
  251. }
  252. static void leon_send_ipi(int cpu, int level)
  253. {
  254. unsigned long mask;
  255. mask = leon_get_irqmask(level);
  256. LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
  257. }
  258. static void leon_ipi_single(int cpu)
  259. {
  260. struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
  261. /* Mark work */
  262. work->single = 1;
  263. /* Generate IRQ on the CPU */
  264. leon_send_ipi(cpu, leon_ipi_irq);
  265. }
  266. static void leon_ipi_mask_one(int cpu)
  267. {
  268. struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
  269. /* Mark work */
  270. work->msk = 1;
  271. /* Generate IRQ on the CPU */
  272. leon_send_ipi(cpu, leon_ipi_irq);
  273. }
  274. static void leon_ipi_resched(int cpu)
  275. {
  276. struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
  277. /* Mark work */
  278. work->resched = 1;
  279. /* Generate IRQ on the CPU (any IRQ will cause resched) */
  280. leon_send_ipi(cpu, leon_ipi_irq);
  281. }
  282. void leonsmp_ipi_interrupt(void)
  283. {
  284. struct leon_ipi_work *work = this_cpu_ptr(&leon_ipi_work);
  285. if (work->single) {
  286. work->single = 0;
  287. smp_call_function_single_interrupt();
  288. }
  289. if (work->msk) {
  290. work->msk = 0;
  291. smp_call_function_interrupt();
  292. }
  293. if (work->resched) {
  294. work->resched = 0;
  295. smp_resched_interrupt();
  296. }
  297. }
  298. static struct smp_funcall {
  299. smpfunc_t func;
  300. unsigned long arg1;
  301. unsigned long arg2;
  302. unsigned long arg3;
  303. unsigned long arg4;
  304. unsigned long arg5;
  305. unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
  306. unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
  307. } ccall_info __attribute__((aligned(8)));
  308. static DEFINE_SPINLOCK(cross_call_lock);
  309. /* Cross calls must be serialized, at least currently. */
  310. static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
  311. unsigned long arg2, unsigned long arg3,
  312. unsigned long arg4)
  313. {
  314. if (smp_processors_ready) {
  315. register int high = NR_CPUS - 1;
  316. unsigned long flags;
  317. spin_lock_irqsave(&cross_call_lock, flags);
  318. {
  319. /* If you make changes here, make sure gcc generates proper code... */
  320. register smpfunc_t f asm("i0") = func;
  321. register unsigned long a1 asm("i1") = arg1;
  322. register unsigned long a2 asm("i2") = arg2;
  323. register unsigned long a3 asm("i3") = arg3;
  324. register unsigned long a4 asm("i4") = arg4;
  325. register unsigned long a5 asm("i5") = 0;
  326. __asm__ __volatile__("std %0, [%6]\n\t"
  327. "std %2, [%6 + 8]\n\t"
  328. "std %4, [%6 + 16]\n\t" : :
  329. "r"(f), "r"(a1), "r"(a2), "r"(a3),
  330. "r"(a4), "r"(a5),
  331. "r"(&ccall_info.func));
  332. }
  333. /* Init receive/complete mapping, plus fire the IPI's off. */
  334. {
  335. register int i;
  336. cpumask_clear_cpu(smp_processor_id(), &mask);
  337. cpumask_and(&mask, cpu_online_mask, &mask);
  338. for (i = 0; i <= high; i++) {
  339. if (cpumask_test_cpu(i, &mask)) {
  340. ccall_info.processors_in[i] = 0;
  341. ccall_info.processors_out[i] = 0;
  342. leon_send_ipi(i, LEON3_IRQ_CROSS_CALL);
  343. }
  344. }
  345. }
  346. {
  347. register int i;
  348. i = 0;
  349. do {
  350. if (!cpumask_test_cpu(i, &mask))
  351. continue;
  352. while (!ccall_info.processors_in[i])
  353. barrier();
  354. } while (++i <= high);
  355. i = 0;
  356. do {
  357. if (!cpumask_test_cpu(i, &mask))
  358. continue;
  359. while (!ccall_info.processors_out[i])
  360. barrier();
  361. } while (++i <= high);
  362. }
  363. spin_unlock_irqrestore(&cross_call_lock, flags);
  364. }
  365. }
  366. /* Running cross calls. */
  367. void leon_cross_call_irq(void)
  368. {
  369. int i = smp_processor_id();
  370. ccall_info.processors_in[i] = 1;
  371. ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
  372. ccall_info.arg4, ccall_info.arg5);
  373. ccall_info.processors_out[i] = 1;
  374. }
  375. static const struct sparc32_ipi_ops leon_ipi_ops = {
  376. .cross_call = leon_cross_call,
  377. .resched = leon_ipi_resched,
  378. .single = leon_ipi_single,
  379. .mask_one = leon_ipi_mask_one,
  380. };
  381. void __init leon_init_smp(void)
  382. {
  383. /* Patch ipi15 trap table */
  384. t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
  385. sparc32_ipi_ops = &leon_ipi_ops;
  386. }