leon_smp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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/smp_lock.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel_stat.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/delay.h>
  30. #include <asm/irq.h>
  31. #include <asm/page.h>
  32. #include <asm/pgalloc.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/oplib.h>
  35. #include <asm/cpudata.h>
  36. #include <asm/asi.h>
  37. #include <asm/leon.h>
  38. #include <asm/leon_amba.h>
  39. #ifdef CONFIG_SPARC_LEON
  40. #include "irq.h"
  41. extern ctxd_t *srmmu_ctx_table_phys;
  42. static int smp_processors_ready;
  43. extern volatile unsigned long cpu_callin_map[NR_CPUS];
  44. extern unsigned char boot_cpu_id;
  45. extern cpumask_t smp_commenced_mask;
  46. void __init leon_configure_cache_smp(void);
  47. static inline unsigned long do_swap(volatile unsigned long *ptr,
  48. unsigned long val)
  49. {
  50. __asm__ __volatile__("swapa [%1] %2, %0\n\t" : "=&r"(val)
  51. : "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
  52. : "memory");
  53. return val;
  54. }
  55. static void smp_setup_percpu_timer(void);
  56. void __cpuinit leon_callin(void)
  57. {
  58. int cpuid = hard_smpleon_processor_id();
  59. local_flush_cache_all();
  60. local_flush_tlb_all();
  61. leon_configure_cache_smp();
  62. /* Get our local ticker going. */
  63. smp_setup_percpu_timer();
  64. calibrate_delay();
  65. smp_store_cpu_info(cpuid);
  66. local_flush_cache_all();
  67. local_flush_tlb_all();
  68. /*
  69. * Unblock the master CPU _only_ when the scheduler state
  70. * of all secondary CPUs will be up-to-date, so after
  71. * the SMP initialization the master will be just allowed
  72. * to call the scheduler code.
  73. * Allow master to continue.
  74. */
  75. do_swap(&cpu_callin_map[cpuid], 1);
  76. local_flush_cache_all();
  77. local_flush_tlb_all();
  78. cpu_probe();
  79. /* Fix idle thread fields. */
  80. __asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(&current_set[cpuid])
  81. : "memory" /* paranoid */);
  82. /* Attach to the address space of init_task. */
  83. atomic_inc(&init_mm.mm_count);
  84. current->active_mm = &init_mm;
  85. while (!cpu_isset(cpuid, smp_commenced_mask))
  86. mb();
  87. local_irq_enable();
  88. cpu_set(cpuid, cpu_online_map);
  89. }
  90. /*
  91. * Cycle through the processors asking the PROM to start each one.
  92. */
  93. extern struct linux_prom_registers smp_penguin_ctable;
  94. void __init leon_configure_cache_smp(void)
  95. {
  96. unsigned long cfg = sparc_leon3_get_dcachecfg();
  97. int me = smp_processor_id();
  98. if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg) > 4) {
  99. printk(KERN_INFO "Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n",
  100. (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg),
  101. (unsigned int)cfg, (unsigned int)me);
  102. sparc_leon3_disable_cache();
  103. } else {
  104. if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) {
  105. sparc_leon3_enable_snooping();
  106. } else {
  107. printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
  108. me);
  109. sparc_leon3_disable_cache();
  110. }
  111. }
  112. local_flush_cache_all();
  113. local_flush_tlb_all();
  114. }
  115. void leon_smp_setbroadcast(unsigned int mask)
  116. {
  117. int broadcast =
  118. ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
  119. LEON3_IRQMPSTATUS_BROADCAST) & 1);
  120. if (!broadcast) {
  121. prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
  122. leon_smp_nrcpus());
  123. if (leon_smp_nrcpus() > 1) {
  124. BUG();
  125. } else {
  126. prom_printf("continue anyway\n");
  127. return;
  128. }
  129. }
  130. LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask);
  131. }
  132. unsigned int leon_smp_getbroadcast(void)
  133. {
  134. unsigned int mask;
  135. mask = LEON_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpbroadcast));
  136. return mask;
  137. }
  138. int leon_smp_nrcpus(void)
  139. {
  140. int nrcpu =
  141. ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
  142. LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
  143. return nrcpu;
  144. }
  145. void __init leon_boot_cpus(void)
  146. {
  147. int nrcpu = leon_smp_nrcpus();
  148. int me = smp_processor_id();
  149. printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me,
  150. (unsigned int)nrcpu, (unsigned int)NR_CPUS,
  151. (unsigned int)&(leon3_irqctrl_regs->mpstatus));
  152. leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me);
  153. leon_enable_irq_cpu(LEON3_IRQ_TICKER, me);
  154. leon_enable_irq_cpu(LEON3_IRQ_RESCHEDULE, me);
  155. leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
  156. leon_configure_cache_smp();
  157. smp_setup_percpu_timer();
  158. local_flush_cache_all();
  159. }
  160. int __cpuinit leon_boot_one_cpu(int i)
  161. {
  162. struct task_struct *p;
  163. int timeout;
  164. /* Cook up an idler for this guy. */
  165. p = fork_idle(i);
  166. current_set[i] = task_thread_info(p);
  167. /* See trampoline.S:leon_smp_cpu_startup for details...
  168. * Initialize the contexts table
  169. * Since the call to prom_startcpu() trashes the structure,
  170. * we need to re-initialize it for each cpu
  171. */
  172. smp_penguin_ctable.which_io = 0;
  173. smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys;
  174. smp_penguin_ctable.reg_size = 0;
  175. /* whirrr, whirrr, whirrrrrrrrr... */
  176. printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i,
  177. (unsigned int)&leon3_irqctrl_regs->mpstatus);
  178. local_flush_cache_all();
  179. LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
  180. /* wheee... it's going... */
  181. for (timeout = 0; timeout < 10000; timeout++) {
  182. if (cpu_callin_map[i])
  183. break;
  184. udelay(200);
  185. }
  186. printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
  187. if (!(cpu_callin_map[i])) {
  188. printk(KERN_ERR "Processor %d is stuck.\n", i);
  189. return -ENODEV;
  190. } else {
  191. leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i);
  192. leon_enable_irq_cpu(LEON3_IRQ_TICKER, i);
  193. leon_enable_irq_cpu(LEON3_IRQ_RESCHEDULE, i);
  194. }
  195. local_flush_cache_all();
  196. return 0;
  197. }
  198. void __init leon_smp_done(void)
  199. {
  200. int i, first;
  201. int *prev;
  202. /* setup cpu list for irq rotation */
  203. first = 0;
  204. prev = &first;
  205. for (i = 0; i < NR_CPUS; i++) {
  206. if (cpu_online(i)) {
  207. *prev = i;
  208. prev = &cpu_data(i).next;
  209. }
  210. }
  211. *prev = first;
  212. local_flush_cache_all();
  213. /* Free unneeded trap tables */
  214. if (!cpu_isset(1, cpu_present_map)) {
  215. ClearPageReserved(virt_to_page(trapbase_cpu1));
  216. init_page_count(virt_to_page(trapbase_cpu1));
  217. free_page((unsigned long)trapbase_cpu1);
  218. totalram_pages++;
  219. num_physpages++;
  220. }
  221. if (!cpu_isset(2, cpu_present_map)) {
  222. ClearPageReserved(virt_to_page(trapbase_cpu2));
  223. init_page_count(virt_to_page(trapbase_cpu2));
  224. free_page((unsigned long)trapbase_cpu2);
  225. totalram_pages++;
  226. num_physpages++;
  227. }
  228. if (!cpu_isset(3, cpu_present_map)) {
  229. ClearPageReserved(virt_to_page(trapbase_cpu3));
  230. init_page_count(virt_to_page(trapbase_cpu3));
  231. free_page((unsigned long)trapbase_cpu3);
  232. totalram_pages++;
  233. num_physpages++;
  234. }
  235. /* Ok, they are spinning and ready to go. */
  236. smp_processors_ready = 1;
  237. }
  238. void leon_irq_rotate(int cpu)
  239. {
  240. }
  241. static struct smp_funcall {
  242. smpfunc_t func;
  243. unsigned long arg1;
  244. unsigned long arg2;
  245. unsigned long arg3;
  246. unsigned long arg4;
  247. unsigned long arg5;
  248. unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
  249. unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
  250. } ccall_info;
  251. static DEFINE_SPINLOCK(cross_call_lock);
  252. /* Cross calls must be serialized, at least currently. */
  253. static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
  254. unsigned long arg2, unsigned long arg3,
  255. unsigned long arg4)
  256. {
  257. if (smp_processors_ready) {
  258. register int high = NR_CPUS - 1;
  259. unsigned long flags;
  260. spin_lock_irqsave(&cross_call_lock, flags);
  261. {
  262. /* If you make changes here, make sure gcc generates proper code... */
  263. register smpfunc_t f asm("i0") = func;
  264. register unsigned long a1 asm("i1") = arg1;
  265. register unsigned long a2 asm("i2") = arg2;
  266. register unsigned long a3 asm("i3") = arg3;
  267. register unsigned long a4 asm("i4") = arg4;
  268. register unsigned long a5 asm("i5") = 0;
  269. __asm__ __volatile__("std %0, [%6]\n\t"
  270. "std %2, [%6 + 8]\n\t"
  271. "std %4, [%6 + 16]\n\t" : :
  272. "r"(f), "r"(a1), "r"(a2), "r"(a3),
  273. "r"(a4), "r"(a5),
  274. "r"(&ccall_info.func));
  275. }
  276. /* Init receive/complete mapping, plus fire the IPI's off. */
  277. {
  278. register int i;
  279. cpu_clear(smp_processor_id(), mask);
  280. cpus_and(mask, cpu_online_map, mask);
  281. for (i = 0; i <= high; i++) {
  282. if (cpu_isset(i, mask)) {
  283. ccall_info.processors_in[i] = 0;
  284. ccall_info.processors_out[i] = 0;
  285. set_cpu_int(i, LEON3_IRQ_CROSS_CALL);
  286. }
  287. }
  288. }
  289. {
  290. register int i;
  291. i = 0;
  292. do {
  293. if (!cpu_isset(i, mask))
  294. continue;
  295. while (!ccall_info.processors_in[i])
  296. barrier();
  297. } while (++i <= high);
  298. i = 0;
  299. do {
  300. if (!cpu_isset(i, mask))
  301. continue;
  302. while (!ccall_info.processors_out[i])
  303. barrier();
  304. } while (++i <= high);
  305. }
  306. spin_unlock_irqrestore(&cross_call_lock, flags);
  307. }
  308. }
  309. /* Running cross calls. */
  310. void leon_cross_call_irq(void)
  311. {
  312. int i = smp_processor_id();
  313. ccall_info.processors_in[i] = 1;
  314. ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
  315. ccall_info.arg4, ccall_info.arg5);
  316. ccall_info.processors_out[i] = 1;
  317. }
  318. void leon_percpu_timer_interrupt(struct pt_regs *regs)
  319. {
  320. struct pt_regs *old_regs;
  321. int cpu = smp_processor_id();
  322. old_regs = set_irq_regs(regs);
  323. leon_clear_profile_irq(cpu);
  324. profile_tick(CPU_PROFILING);
  325. if (!--prof_counter(cpu)) {
  326. int user = user_mode(regs);
  327. irq_enter();
  328. update_process_times(user);
  329. irq_exit();
  330. prof_counter(cpu) = prof_multiplier(cpu);
  331. }
  332. set_irq_regs(old_regs);
  333. }
  334. static void __init smp_setup_percpu_timer(void)
  335. {
  336. int cpu = smp_processor_id();
  337. prof_counter(cpu) = prof_multiplier(cpu) = 1;
  338. }
  339. void __init leon_blackbox_id(unsigned *addr)
  340. {
  341. int rd = *addr & 0x3e000000;
  342. int rs1 = rd >> 11;
  343. /* patch places where ___b_hard_smp_processor_id appears */
  344. addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
  345. addr[1] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
  346. addr[2] = 0x01000000; /* nop */
  347. }
  348. void __init leon_blackbox_current(unsigned *addr)
  349. {
  350. int rd = *addr & 0x3e000000;
  351. int rs1 = rd >> 11;
  352. /* patch LOAD_CURRENT macro where ___b_load_current appears */
  353. addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
  354. addr[2] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
  355. addr[4] = 0x81282002 | rd | rs1; /* sll reg, 0x2, reg */
  356. }
  357. /*
  358. * CPU idle callback function
  359. * See .../arch/sparc/kernel/process.c
  360. */
  361. void pmc_leon_idle(void)
  362. {
  363. __asm__ volatile ("mov %g0, %asr19");
  364. }
  365. void __init leon_init_smp(void)
  366. {
  367. /* Patch ipi15 trap table */
  368. t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
  369. BTFIXUPSET_BLACKBOX(hard_smp_processor_id, leon_blackbox_id);
  370. BTFIXUPSET_BLACKBOX(load_current, leon_blackbox_current);
  371. BTFIXUPSET_CALL(smp_cross_call, leon_cross_call, BTFIXUPCALL_NORM);
  372. BTFIXUPSET_CALL(__hard_smp_processor_id, __leon_processor_id,
  373. BTFIXUPCALL_NORM);
  374. #ifndef PMC_NO_IDLE
  375. /* Assign power management IDLE handler */
  376. pm_idle = pmc_leon_idle;
  377. printk(KERN_INFO "leon: power management initialized\n");
  378. #endif
  379. }
  380. #endif /* CONFIG_SPARC_LEON */