leon_smp.c 13 KB

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