leon_smp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 <asm/cacheflush.h>
  26. #include <asm/tlbflush.h>
  27. #include <asm/ptrace.h>
  28. #include <linux/atomic.h>
  29. #include <asm/irq_regs.h>
  30. #include <asm/traps.h>
  31. #include <asm/delay.h>
  32. #include <asm/irq.h>
  33. #include <asm/page.h>
  34. #include <asm/pgalloc.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/oplib.h>
  37. #include <asm/cpudata.h>
  38. #include <asm/asi.h>
  39. #include <asm/leon.h>
  40. #include <asm/leon_amba.h>
  41. #include "kernel.h"
  42. #ifdef CONFIG_SPARC_LEON
  43. #include "irq.h"
  44. extern ctxd_t *srmmu_ctx_table_phys;
  45. static int smp_processors_ready;
  46. extern volatile unsigned long cpu_callin_map[NR_CPUS];
  47. extern cpumask_t smp_commenced_mask;
  48. void __init leon_configure_cache_smp(void);
  49. static void leon_ipi_init(void);
  50. /* IRQ number of LEON IPIs */
  51. int leon_ipi_irq = LEON3_IRQ_IPI_DEFAULT;
  52. static inline unsigned long do_swap(volatile unsigned long *ptr,
  53. unsigned long val)
  54. {
  55. __asm__ __volatile__("swapa [%2] %3, %0\n\t" : "=&r"(val)
  56. : "0"(val), "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
  57. : "memory");
  58. return val;
  59. }
  60. static void smp_setup_percpu_timer(void);
  61. void __cpuinit leon_callin(void)
  62. {
  63. int cpuid = hard_smpleon_processor_id();
  64. local_flush_cache_all();
  65. local_flush_tlb_all();
  66. leon_configure_cache_smp();
  67. notify_cpu_starting(cpuid);
  68. /* Get our local ticker going. */
  69. smp_setup_percpu_timer();
  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. smp_setup_percpu_timer();
  165. local_flush_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_flush_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_flush_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_flush_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_flush_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_ipi_single(int cpu)
  288. {
  289. struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
  290. /* Mark work */
  291. work->single = 1;
  292. /* Generate IRQ on the CPU */
  293. set_cpu_int(cpu, leon_ipi_irq);
  294. }
  295. static void leon_ipi_mask_one(int cpu)
  296. {
  297. struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
  298. /* Mark work */
  299. work->msk = 1;
  300. /* Generate IRQ on the CPU */
  301. set_cpu_int(cpu, leon_ipi_irq);
  302. }
  303. static void leon_ipi_resched(int cpu)
  304. {
  305. struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
  306. /* Mark work */
  307. work->resched = 1;
  308. /* Generate IRQ on the CPU (any IRQ will cause resched) */
  309. set_cpu_int(cpu, leon_ipi_irq);
  310. }
  311. void leonsmp_ipi_interrupt(void)
  312. {
  313. struct leon_ipi_work *work = &__get_cpu_var(leon_ipi_work);
  314. if (work->single) {
  315. work->single = 0;
  316. smp_call_function_single_interrupt();
  317. }
  318. if (work->msk) {
  319. work->msk = 0;
  320. smp_call_function_interrupt();
  321. }
  322. if (work->resched) {
  323. work->resched = 0;
  324. smp_resched_interrupt();
  325. }
  326. }
  327. static struct smp_funcall {
  328. smpfunc_t func;
  329. unsigned long arg1;
  330. unsigned long arg2;
  331. unsigned long arg3;
  332. unsigned long arg4;
  333. unsigned long arg5;
  334. unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
  335. unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
  336. } ccall_info;
  337. static DEFINE_SPINLOCK(cross_call_lock);
  338. /* Cross calls must be serialized, at least currently. */
  339. static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
  340. unsigned long arg2, unsigned long arg3,
  341. unsigned long arg4)
  342. {
  343. if (smp_processors_ready) {
  344. register int high = NR_CPUS - 1;
  345. unsigned long flags;
  346. spin_lock_irqsave(&cross_call_lock, flags);
  347. {
  348. /* If you make changes here, make sure gcc generates proper code... */
  349. register smpfunc_t f asm("i0") = func;
  350. register unsigned long a1 asm("i1") = arg1;
  351. register unsigned long a2 asm("i2") = arg2;
  352. register unsigned long a3 asm("i3") = arg3;
  353. register unsigned long a4 asm("i4") = arg4;
  354. register unsigned long a5 asm("i5") = 0;
  355. __asm__ __volatile__("std %0, [%6]\n\t"
  356. "std %2, [%6 + 8]\n\t"
  357. "std %4, [%6 + 16]\n\t" : :
  358. "r"(f), "r"(a1), "r"(a2), "r"(a3),
  359. "r"(a4), "r"(a5),
  360. "r"(&ccall_info.func));
  361. }
  362. /* Init receive/complete mapping, plus fire the IPI's off. */
  363. {
  364. register int i;
  365. cpumask_clear_cpu(smp_processor_id(), &mask);
  366. cpumask_and(&mask, cpu_online_mask, &mask);
  367. for (i = 0; i <= high; i++) {
  368. if (cpumask_test_cpu(i, &mask)) {
  369. ccall_info.processors_in[i] = 0;
  370. ccall_info.processors_out[i] = 0;
  371. set_cpu_int(i, LEON3_IRQ_CROSS_CALL);
  372. }
  373. }
  374. }
  375. {
  376. register int i;
  377. i = 0;
  378. do {
  379. if (!cpumask_test_cpu(i, &mask))
  380. continue;
  381. while (!ccall_info.processors_in[i])
  382. barrier();
  383. } while (++i <= high);
  384. i = 0;
  385. do {
  386. if (!cpumask_test_cpu(i, &mask))
  387. continue;
  388. while (!ccall_info.processors_out[i])
  389. barrier();
  390. } while (++i <= high);
  391. }
  392. spin_unlock_irqrestore(&cross_call_lock, flags);
  393. }
  394. }
  395. /* Running cross calls. */
  396. void leon_cross_call_irq(void)
  397. {
  398. int i = smp_processor_id();
  399. ccall_info.processors_in[i] = 1;
  400. ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
  401. ccall_info.arg4, ccall_info.arg5);
  402. ccall_info.processors_out[i] = 1;
  403. }
  404. irqreturn_t leon_percpu_timer_interrupt(int irq, void *unused)
  405. {
  406. int cpu = smp_processor_id();
  407. leon_clear_profile_irq(cpu);
  408. profile_tick(CPU_PROFILING);
  409. if (!--prof_counter(cpu)) {
  410. int user = user_mode(get_irq_regs());
  411. update_process_times(user);
  412. prof_counter(cpu) = prof_multiplier(cpu);
  413. }
  414. return IRQ_HANDLED;
  415. }
  416. static void __init smp_setup_percpu_timer(void)
  417. {
  418. int cpu = smp_processor_id();
  419. prof_counter(cpu) = prof_multiplier(cpu) = 1;
  420. }
  421. void __init leon_blackbox_id(unsigned *addr)
  422. {
  423. int rd = *addr & 0x3e000000;
  424. int rs1 = rd >> 11;
  425. /* patch places where ___b_hard_smp_processor_id appears */
  426. addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
  427. addr[1] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
  428. addr[2] = 0x01000000; /* nop */
  429. }
  430. void __init leon_blackbox_current(unsigned *addr)
  431. {
  432. int rd = *addr & 0x3e000000;
  433. int rs1 = rd >> 11;
  434. /* patch LOAD_CURRENT macro where ___b_load_current appears */
  435. addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
  436. addr[2] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
  437. addr[4] = 0x81282002 | rd | rs1; /* sll reg, 0x2, reg */
  438. }
  439. void __init leon_init_smp(void)
  440. {
  441. /* Patch ipi15 trap table */
  442. t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
  443. BTFIXUPSET_BLACKBOX(hard_smp_processor_id, leon_blackbox_id);
  444. BTFIXUPSET_BLACKBOX(load_current, leon_blackbox_current);
  445. BTFIXUPSET_CALL(smp_cross_call, leon_cross_call, BTFIXUPCALL_NORM);
  446. BTFIXUPSET_CALL(__hard_smp_processor_id, __leon_processor_id,
  447. BTFIXUPCALL_NORM);
  448. BTFIXUPSET_CALL(smp_ipi_resched, leon_ipi_resched, BTFIXUPCALL_NORM);
  449. BTFIXUPSET_CALL(smp_ipi_single, leon_ipi_single, BTFIXUPCALL_NORM);
  450. BTFIXUPSET_CALL(smp_ipi_mask_one, leon_ipi_mask_one, BTFIXUPCALL_NORM);
  451. }
  452. #endif /* CONFIG_SPARC_LEON */