leon_smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. #ifdef CONFIG_SPARC_LEON
  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 __init 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 __cpuinit leon_callin(void)
  63. {
  64. int cpuid = hard_smp_processor_id();
  65. local_ops->cache_all();
  66. local_ops->tlb_all();
  67. leon_configure_cache_smp();
  68. notify_cpu_starting(cpuid);
  69. /* Get our local ticker going. */
  70. register_percpu_ce(cpuid);
  71. calibrate_delay();
  72. smp_store_cpu_info(cpuid);
  73. local_ops->cache_all();
  74. local_ops->tlb_all();
  75. /*
  76. * Unblock the master CPU _only_ when the scheduler state
  77. * of all secondary CPUs will be up-to-date, so after
  78. * the SMP initialization the master will be just allowed
  79. * to call the scheduler code.
  80. * Allow master to continue.
  81. */
  82. do_swap(&cpu_callin_map[cpuid], 1);
  83. local_ops->cache_all();
  84. local_ops->tlb_all();
  85. /* Fix idle thread fields. */
  86. __asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(&current_set[cpuid])
  87. : "memory" /* paranoid */);
  88. /* Attach to the address space of init_task. */
  89. atomic_inc(&init_mm.mm_count);
  90. current->active_mm = &init_mm;
  91. while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
  92. mb();
  93. local_irq_enable();
  94. set_cpu_online(cpuid, true);
  95. }
  96. /*
  97. * Cycle through the processors asking the PROM to start each one.
  98. */
  99. extern struct linux_prom_registers smp_penguin_ctable;
  100. void __init leon_configure_cache_smp(void)
  101. {
  102. unsigned long cfg = sparc_leon3_get_dcachecfg();
  103. int me = smp_processor_id();
  104. if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg) > 4) {
  105. printk(KERN_INFO "Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n",
  106. (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg),
  107. (unsigned int)cfg, (unsigned int)me);
  108. sparc_leon3_disable_cache();
  109. } else {
  110. if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) {
  111. sparc_leon3_enable_snooping();
  112. } else {
  113. printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
  114. me);
  115. sparc_leon3_disable_cache();
  116. }
  117. }
  118. local_ops->cache_all();
  119. local_ops->tlb_all();
  120. }
  121. void leon_smp_setbroadcast(unsigned int mask)
  122. {
  123. int broadcast =
  124. ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
  125. LEON3_IRQMPSTATUS_BROADCAST) & 1);
  126. if (!broadcast) {
  127. prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
  128. leon_smp_nrcpus());
  129. if (leon_smp_nrcpus() > 1) {
  130. BUG();
  131. } else {
  132. prom_printf("continue anyway\n");
  133. return;
  134. }
  135. }
  136. LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask);
  137. }
  138. unsigned int leon_smp_getbroadcast(void)
  139. {
  140. unsigned int mask;
  141. mask = LEON_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpbroadcast));
  142. return mask;
  143. }
  144. int leon_smp_nrcpus(void)
  145. {
  146. int nrcpu =
  147. ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
  148. LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
  149. return nrcpu;
  150. }
  151. void __init leon_boot_cpus(void)
  152. {
  153. int nrcpu = leon_smp_nrcpus();
  154. int me = smp_processor_id();
  155. /* Setup IPI */
  156. leon_ipi_init();
  157. printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me,
  158. (unsigned int)nrcpu, (unsigned int)NR_CPUS,
  159. (unsigned int)&(leon3_irqctrl_regs->mpstatus));
  160. leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me);
  161. leon_enable_irq_cpu(LEON3_IRQ_TICKER, me);
  162. leon_enable_irq_cpu(leon_ipi_irq, me);
  163. leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
  164. leon_configure_cache_smp();
  165. local_ops->cache_all();
  166. }
  167. int __cpuinit leon_boot_one_cpu(int i)
  168. {
  169. struct task_struct *p;
  170. int timeout;
  171. /* Cook up an idler for this guy. */
  172. p = fork_idle(i);
  173. current_set[i] = task_thread_info(p);
  174. /* See trampoline.S:leon_smp_cpu_startup for details...
  175. * Initialize the contexts table
  176. * Since the call to prom_startcpu() trashes the structure,
  177. * we need to re-initialize it for each cpu
  178. */
  179. smp_penguin_ctable.which_io = 0;
  180. smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys;
  181. smp_penguin_ctable.reg_size = 0;
  182. /* whirrr, whirrr, whirrrrrrrrr... */
  183. printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i,
  184. (unsigned int)&leon3_irqctrl_regs->mpstatus);
  185. local_ops->cache_all();
  186. /* Make sure all IRQs are of from the start for this new CPU */
  187. LEON_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[i], 0);
  188. /* Wake one CPU */
  189. LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
  190. /* wheee... it's going... */
  191. for (timeout = 0; timeout < 10000; timeout++) {
  192. if (cpu_callin_map[i])
  193. break;
  194. udelay(200);
  195. }
  196. printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
  197. if (!(cpu_callin_map[i])) {
  198. printk(KERN_ERR "Processor %d is stuck.\n", i);
  199. return -ENODEV;
  200. } else {
  201. leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i);
  202. leon_enable_irq_cpu(LEON3_IRQ_TICKER, i);
  203. leon_enable_irq_cpu(leon_ipi_irq, i);
  204. }
  205. local_ops->cache_all();
  206. return 0;
  207. }
  208. void __init leon_smp_done(void)
  209. {
  210. int i, first;
  211. int *prev;
  212. /* setup cpu list for irq rotation */
  213. first = 0;
  214. prev = &first;
  215. for (i = 0; i < NR_CPUS; i++) {
  216. if (cpu_online(i)) {
  217. *prev = i;
  218. prev = &cpu_data(i).next;
  219. }
  220. }
  221. *prev = first;
  222. local_ops->cache_all();
  223. /* Free unneeded trap tables */
  224. if (!cpu_present(1)) {
  225. ClearPageReserved(virt_to_page(&trapbase_cpu1));
  226. init_page_count(virt_to_page(&trapbase_cpu1));
  227. free_page((unsigned long)&trapbase_cpu1);
  228. totalram_pages++;
  229. num_physpages++;
  230. }
  231. if (!cpu_present(2)) {
  232. ClearPageReserved(virt_to_page(&trapbase_cpu2));
  233. init_page_count(virt_to_page(&trapbase_cpu2));
  234. free_page((unsigned long)&trapbase_cpu2);
  235. totalram_pages++;
  236. num_physpages++;
  237. }
  238. if (!cpu_present(3)) {
  239. ClearPageReserved(virt_to_page(&trapbase_cpu3));
  240. init_page_count(virt_to_page(&trapbase_cpu3));
  241. free_page((unsigned long)&trapbase_cpu3);
  242. totalram_pages++;
  243. num_physpages++;
  244. }
  245. /* Ok, they are spinning and ready to go. */
  246. smp_processors_ready = 1;
  247. }
  248. void leon_irq_rotate(int cpu)
  249. {
  250. }
  251. struct leon_ipi_work {
  252. int single;
  253. int msk;
  254. int resched;
  255. };
  256. static DEFINE_PER_CPU_SHARED_ALIGNED(struct leon_ipi_work, leon_ipi_work);
  257. /* Initialize IPIs on the LEON, in order to save IRQ resources only one IRQ
  258. * is used for all three types of IPIs.
  259. */
  260. static void __init leon_ipi_init(void)
  261. {
  262. int cpu, len;
  263. struct leon_ipi_work *work;
  264. struct property *pp;
  265. struct device_node *rootnp;
  266. struct tt_entry *trap_table;
  267. unsigned long flags;
  268. /* Find IPI IRQ or stick with default value */
  269. rootnp = of_find_node_by_path("/ambapp0");
  270. if (rootnp) {
  271. pp = of_find_property(rootnp, "ipi_num", &len);
  272. if (pp && (*(int *)pp->value))
  273. leon_ipi_irq = *(int *)pp->value;
  274. }
  275. printk(KERN_INFO "leon: SMP IPIs at IRQ %d\n", leon_ipi_irq);
  276. /* Adjust so that we jump directly to smpleon_ipi */
  277. local_irq_save(flags);
  278. trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_ipi_irq - 1)];
  279. trap_table->inst_three += smpleon_ipi - real_irq_entry;
  280. local_ops->cache_all();
  281. local_irq_restore(flags);
  282. for_each_possible_cpu(cpu) {
  283. work = &per_cpu(leon_ipi_work, cpu);
  284. work->single = work->msk = work->resched = 0;
  285. }
  286. }
  287. static void leon_send_ipi(int cpu, int level)
  288. {
  289. unsigned long mask;
  290. mask = leon_get_irqmask(level);
  291. LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
  292. }
  293. static void leon_ipi_single(int cpu)
  294. {
  295. struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
  296. /* Mark work */
  297. work->single = 1;
  298. /* Generate IRQ on the CPU */
  299. leon_send_ipi(cpu, leon_ipi_irq);
  300. }
  301. static void leon_ipi_mask_one(int cpu)
  302. {
  303. struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
  304. /* Mark work */
  305. work->msk = 1;
  306. /* Generate IRQ on the CPU */
  307. leon_send_ipi(cpu, leon_ipi_irq);
  308. }
  309. static void leon_ipi_resched(int cpu)
  310. {
  311. struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
  312. /* Mark work */
  313. work->resched = 1;
  314. /* Generate IRQ on the CPU (any IRQ will cause resched) */
  315. leon_send_ipi(cpu, leon_ipi_irq);
  316. }
  317. void leonsmp_ipi_interrupt(void)
  318. {
  319. struct leon_ipi_work *work = &__get_cpu_var(leon_ipi_work);
  320. if (work->single) {
  321. work->single = 0;
  322. smp_call_function_single_interrupt();
  323. }
  324. if (work->msk) {
  325. work->msk = 0;
  326. smp_call_function_interrupt();
  327. }
  328. if (work->resched) {
  329. work->resched = 0;
  330. smp_resched_interrupt();
  331. }
  332. }
  333. static struct smp_funcall {
  334. smpfunc_t func;
  335. unsigned long arg1;
  336. unsigned long arg2;
  337. unsigned long arg3;
  338. unsigned long arg4;
  339. unsigned long arg5;
  340. unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
  341. unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
  342. } ccall_info;
  343. static DEFINE_SPINLOCK(cross_call_lock);
  344. /* Cross calls must be serialized, at least currently. */
  345. static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
  346. unsigned long arg2, unsigned long arg3,
  347. unsigned long arg4)
  348. {
  349. if (smp_processors_ready) {
  350. register int high = NR_CPUS - 1;
  351. unsigned long flags;
  352. spin_lock_irqsave(&cross_call_lock, flags);
  353. {
  354. /* If you make changes here, make sure gcc generates proper code... */
  355. register smpfunc_t f asm("i0") = func;
  356. register unsigned long a1 asm("i1") = arg1;
  357. register unsigned long a2 asm("i2") = arg2;
  358. register unsigned long a3 asm("i3") = arg3;
  359. register unsigned long a4 asm("i4") = arg4;
  360. register unsigned long a5 asm("i5") = 0;
  361. __asm__ __volatile__("std %0, [%6]\n\t"
  362. "std %2, [%6 + 8]\n\t"
  363. "std %4, [%6 + 16]\n\t" : :
  364. "r"(f), "r"(a1), "r"(a2), "r"(a3),
  365. "r"(a4), "r"(a5),
  366. "r"(&ccall_info.func));
  367. }
  368. /* Init receive/complete mapping, plus fire the IPI's off. */
  369. {
  370. register int i;
  371. cpumask_clear_cpu(smp_processor_id(), &mask);
  372. cpumask_and(&mask, cpu_online_mask, &mask);
  373. for (i = 0; i <= high; i++) {
  374. if (cpumask_test_cpu(i, &mask)) {
  375. ccall_info.processors_in[i] = 0;
  376. ccall_info.processors_out[i] = 0;
  377. leon_send_ipi(i, LEON3_IRQ_CROSS_CALL);
  378. }
  379. }
  380. }
  381. {
  382. register int i;
  383. i = 0;
  384. do {
  385. if (!cpumask_test_cpu(i, &mask))
  386. continue;
  387. while (!ccall_info.processors_in[i])
  388. barrier();
  389. } while (++i <= high);
  390. i = 0;
  391. do {
  392. if (!cpumask_test_cpu(i, &mask))
  393. continue;
  394. while (!ccall_info.processors_out[i])
  395. barrier();
  396. } while (++i <= high);
  397. }
  398. spin_unlock_irqrestore(&cross_call_lock, flags);
  399. }
  400. }
  401. /* Running cross calls. */
  402. void leon_cross_call_irq(void)
  403. {
  404. int i = smp_processor_id();
  405. ccall_info.processors_in[i] = 1;
  406. ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
  407. ccall_info.arg4, ccall_info.arg5);
  408. ccall_info.processors_out[i] = 1;
  409. }
  410. static const struct sparc32_ipi_ops leon_ipi_ops = {
  411. .cross_call = leon_cross_call,
  412. .resched = leon_ipi_resched,
  413. .single = leon_ipi_single,
  414. .mask_one = leon_ipi_mask_one,
  415. };
  416. void __init leon_init_smp(void)
  417. {
  418. /* Patch ipi15 trap table */
  419. t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
  420. sparc32_ipi_ops = &leon_ipi_ops;
  421. }
  422. #endif /* CONFIG_SPARC_LEON */