leon_smp.c 13 KB

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