leon_smp.c 11 KB

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