leon_smp.c 11 KB

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