sun4d_smp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /* sun4d_smp.c: Sparc SS1000/SC2000 SMP support.
  2. *
  3. * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  4. *
  5. * Based on sun4m's smp.c, which is:
  6. * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
  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/init.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/mm.h>
  18. #include <linux/swap.h>
  19. #include <linux/profile.h>
  20. #include <asm/ptrace.h>
  21. #include <asm/atomic.h>
  22. #include <asm/irq_regs.h>
  23. #include <asm/delay.h>
  24. #include <asm/irq.h>
  25. #include <asm/page.h>
  26. #include <asm/pgalloc.h>
  27. #include <asm/pgtable.h>
  28. #include <asm/oplib.h>
  29. #include <asm/sbus.h>
  30. #include <asm/sbi.h>
  31. #include <asm/tlbflush.h>
  32. #include <asm/cacheflush.h>
  33. #include <asm/cpudata.h>
  34. #include "irq.h"
  35. #define IRQ_CROSS_CALL 15
  36. extern ctxd_t *srmmu_ctx_table_phys;
  37. extern void calibrate_delay(void);
  38. static volatile int smp_processors_ready = 0;
  39. static int smp_highest_cpu;
  40. extern volatile unsigned long cpu_callin_map[NR_CPUS];
  41. extern cpuinfo_sparc cpu_data[NR_CPUS];
  42. extern unsigned char boot_cpu_id;
  43. extern volatile int smp_process_available;
  44. extern cpumask_t smp_commenced_mask;
  45. extern int __smp4d_processor_id(void);
  46. /* #define SMP_DEBUG */
  47. #ifdef SMP_DEBUG
  48. #define SMP_PRINTK(x) printk x
  49. #else
  50. #define SMP_PRINTK(x)
  51. #endif
  52. static inline unsigned long swap(volatile unsigned long *ptr, unsigned long val)
  53. {
  54. __asm__ __volatile__("swap [%1], %0\n\t" :
  55. "=&r" (val), "=&r" (ptr) :
  56. "0" (val), "1" (ptr));
  57. return val;
  58. }
  59. static void smp_setup_percpu_timer(void);
  60. extern void cpu_probe(void);
  61. extern void sun4d_distribute_irqs(void);
  62. void __init smp4d_callin(void)
  63. {
  64. int cpuid = hard_smp4d_processor_id();
  65. extern spinlock_t sun4d_imsk_lock;
  66. unsigned long flags;
  67. /* Show we are alive */
  68. cpu_leds[cpuid] = 0x6;
  69. show_leds(cpuid);
  70. /* Enable level15 interrupt, disable level14 interrupt for now */
  71. cc_set_imsk((cc_get_imsk() & ~0x8000) | 0x4000);
  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. */
  80. /* Get our local ticker going. */
  81. smp_setup_percpu_timer();
  82. calibrate_delay();
  83. smp_store_cpu_info(cpuid);
  84. local_flush_cache_all();
  85. local_flush_tlb_all();
  86. /* Allow master to continue. */
  87. swap((unsigned long *)&cpu_callin_map[cpuid], 1);
  88. local_flush_cache_all();
  89. local_flush_tlb_all();
  90. cpu_probe();
  91. while((unsigned long)current_set[cpuid] < PAGE_OFFSET)
  92. barrier();
  93. while(current_set[cpuid]->cpu != cpuid)
  94. barrier();
  95. /* Fix idle thread fields. */
  96. __asm__ __volatile__("ld [%0], %%g6\n\t"
  97. : : "r" (&current_set[cpuid])
  98. : "memory" /* paranoid */);
  99. cpu_leds[cpuid] = 0x9;
  100. show_leds(cpuid);
  101. /* Attach to the address space of init_task. */
  102. atomic_inc(&init_mm.mm_count);
  103. current->active_mm = &init_mm;
  104. local_flush_cache_all();
  105. local_flush_tlb_all();
  106. local_irq_enable(); /* We don't allow PIL 14 yet */
  107. while (!cpu_isset(cpuid, smp_commenced_mask))
  108. barrier();
  109. spin_lock_irqsave(&sun4d_imsk_lock, flags);
  110. cc_set_imsk(cc_get_imsk() & ~0x4000); /* Allow PIL 14 as well */
  111. spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
  112. cpu_set(cpuid, cpu_online_map);
  113. }
  114. extern void init_IRQ(void);
  115. extern void cpu_panic(void);
  116. /*
  117. * Cycle through the processors asking the PROM to start each one.
  118. */
  119. extern struct linux_prom_registers smp_penguin_ctable;
  120. extern unsigned long trapbase_cpu1[];
  121. extern unsigned long trapbase_cpu2[];
  122. extern unsigned long trapbase_cpu3[];
  123. void __init smp4d_boot_cpus(void)
  124. {
  125. if (boot_cpu_id)
  126. current_set[0] = NULL;
  127. smp_setup_percpu_timer();
  128. local_flush_cache_all();
  129. }
  130. int __cpuinit smp4d_boot_one_cpu(int i)
  131. {
  132. extern unsigned long sun4d_cpu_startup;
  133. unsigned long *entry = &sun4d_cpu_startup;
  134. struct task_struct *p;
  135. int timeout;
  136. int cpu_node;
  137. cpu_find_by_instance(i, &cpu_node,NULL);
  138. /* Cook up an idler for this guy. */
  139. p = fork_idle(i);
  140. current_set[i] = task_thread_info(p);
  141. /*
  142. * Initialize the contexts table
  143. * Since the call to prom_startcpu() trashes the structure,
  144. * we need to re-initialize it for each cpu
  145. */
  146. smp_penguin_ctable.which_io = 0;
  147. smp_penguin_ctable.phys_addr = (unsigned int) srmmu_ctx_table_phys;
  148. smp_penguin_ctable.reg_size = 0;
  149. /* whirrr, whirrr, whirrrrrrrrr... */
  150. SMP_PRINTK(("Starting CPU %d at %p \n", i, entry));
  151. local_flush_cache_all();
  152. prom_startcpu(cpu_node,
  153. &smp_penguin_ctable, 0, (char *)entry);
  154. SMP_PRINTK(("prom_startcpu returned :)\n"));
  155. /* wheee... it's going... */
  156. for(timeout = 0; timeout < 10000; timeout++) {
  157. if(cpu_callin_map[i])
  158. break;
  159. udelay(200);
  160. }
  161. if (!(cpu_callin_map[i])) {
  162. printk("Processor %d is stuck.\n", i);
  163. return -ENODEV;
  164. }
  165. local_flush_cache_all();
  166. return 0;
  167. }
  168. void __init smp4d_smp_done(void)
  169. {
  170. int i, first;
  171. int *prev;
  172. /* setup cpu list for irq rotation */
  173. first = 0;
  174. prev = &first;
  175. for (i = 0; i < NR_CPUS; i++)
  176. if (cpu_online(i)) {
  177. *prev = i;
  178. prev = &cpu_data(i).next;
  179. }
  180. *prev = first;
  181. local_flush_cache_all();
  182. /* Free unneeded trap tables */
  183. ClearPageReserved(virt_to_page(trapbase_cpu1));
  184. init_page_count(virt_to_page(trapbase_cpu1));
  185. free_page((unsigned long)trapbase_cpu1);
  186. totalram_pages++;
  187. num_physpages++;
  188. ClearPageReserved(virt_to_page(trapbase_cpu2));
  189. init_page_count(virt_to_page(trapbase_cpu2));
  190. free_page((unsigned long)trapbase_cpu2);
  191. totalram_pages++;
  192. num_physpages++;
  193. ClearPageReserved(virt_to_page(trapbase_cpu3));
  194. init_page_count(virt_to_page(trapbase_cpu3));
  195. free_page((unsigned long)trapbase_cpu3);
  196. totalram_pages++;
  197. num_physpages++;
  198. /* Ok, they are spinning and ready to go. */
  199. smp_processors_ready = 1;
  200. sun4d_distribute_irqs();
  201. }
  202. static struct smp_funcall {
  203. smpfunc_t func;
  204. unsigned long arg1;
  205. unsigned long arg2;
  206. unsigned long arg3;
  207. unsigned long arg4;
  208. unsigned long arg5;
  209. unsigned char processors_in[NR_CPUS]; /* Set when ipi entered. */
  210. unsigned char processors_out[NR_CPUS]; /* Set when ipi exited. */
  211. } ccall_info __attribute__((aligned(8)));
  212. static DEFINE_SPINLOCK(cross_call_lock);
  213. /* Cross calls must be serialized, at least currently. */
  214. void smp4d_cross_call(smpfunc_t func, unsigned long arg1, unsigned long arg2,
  215. unsigned long arg3, unsigned long arg4, unsigned long arg5)
  216. {
  217. if(smp_processors_ready) {
  218. register int high = smp_highest_cpu;
  219. unsigned long flags;
  220. spin_lock_irqsave(&cross_call_lock, flags);
  221. {
  222. /* If you make changes here, make sure gcc generates proper code... */
  223. register smpfunc_t f asm("i0") = func;
  224. register unsigned long a1 asm("i1") = arg1;
  225. register unsigned long a2 asm("i2") = arg2;
  226. register unsigned long a3 asm("i3") = arg3;
  227. register unsigned long a4 asm("i4") = arg4;
  228. register unsigned long a5 asm("i5") = arg5;
  229. __asm__ __volatile__(
  230. "std %0, [%6]\n\t"
  231. "std %2, [%6 + 8]\n\t"
  232. "std %4, [%6 + 16]\n\t" : :
  233. "r"(f), "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5),
  234. "r" (&ccall_info.func));
  235. }
  236. /* Init receive/complete mapping, plus fire the IPI's off. */
  237. {
  238. cpumask_t mask;
  239. register int i;
  240. mask = cpumask_of_cpu(hard_smp4d_processor_id());
  241. cpus_andnot(mask, cpu_online_map, mask);
  242. for(i = 0; i <= high; i++) {
  243. if (cpu_isset(i, mask)) {
  244. ccall_info.processors_in[i] = 0;
  245. ccall_info.processors_out[i] = 0;
  246. sun4d_send_ipi(i, IRQ_CROSS_CALL);
  247. }
  248. }
  249. }
  250. {
  251. register int i;
  252. i = 0;
  253. do {
  254. while(!ccall_info.processors_in[i])
  255. barrier();
  256. } while(++i <= high);
  257. i = 0;
  258. do {
  259. while(!ccall_info.processors_out[i])
  260. barrier();
  261. } while(++i <= high);
  262. }
  263. spin_unlock_irqrestore(&cross_call_lock, flags);
  264. }
  265. }
  266. /* Running cross calls. */
  267. void smp4d_cross_call_irq(void)
  268. {
  269. int i = hard_smp4d_processor_id();
  270. ccall_info.processors_in[i] = 1;
  271. ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
  272. ccall_info.arg4, ccall_info.arg5);
  273. ccall_info.processors_out[i] = 1;
  274. }
  275. static int smp4d_stop_cpu_sender;
  276. static void smp4d_stop_cpu(void)
  277. {
  278. int me = hard_smp4d_processor_id();
  279. if (me != smp4d_stop_cpu_sender)
  280. while(1) barrier();
  281. }
  282. /* Cross calls, in order to work efficiently and atomically do all
  283. * the message passing work themselves, only stopcpu and reschedule
  284. * messages come through here.
  285. */
  286. void smp4d_message_pass(int target, int msg, unsigned long data, int wait)
  287. {
  288. int me = hard_smp4d_processor_id();
  289. SMP_PRINTK(("smp4d_message_pass %d %d %08lx %d\n", target, msg, data, wait));
  290. if (msg == MSG_STOP_CPU && target == MSG_ALL_BUT_SELF) {
  291. unsigned long flags;
  292. static DEFINE_SPINLOCK(stop_cpu_lock);
  293. spin_lock_irqsave(&stop_cpu_lock, flags);
  294. smp4d_stop_cpu_sender = me;
  295. smp4d_cross_call((smpfunc_t)smp4d_stop_cpu, 0, 0, 0, 0, 0);
  296. spin_unlock_irqrestore(&stop_cpu_lock, flags);
  297. }
  298. printk("Yeeee, trying to send SMP msg(%d) to %d on cpu %d\n", msg, target, me);
  299. panic("Bogon SMP message pass.");
  300. }
  301. void smp4d_percpu_timer_interrupt(struct pt_regs *regs)
  302. {
  303. struct pt_regs *old_regs;
  304. int cpu = hard_smp4d_processor_id();
  305. static int cpu_tick[NR_CPUS];
  306. static char led_mask[] = { 0xe, 0xd, 0xb, 0x7, 0xb, 0xd };
  307. old_regs = set_irq_regs(regs);
  308. bw_get_prof_limit(cpu);
  309. bw_clear_intr_mask(0, 1); /* INTR_TABLE[0] & 1 is Profile IRQ */
  310. cpu_tick[cpu]++;
  311. if (!(cpu_tick[cpu] & 15)) {
  312. if (cpu_tick[cpu] == 0x60)
  313. cpu_tick[cpu] = 0;
  314. cpu_leds[cpu] = led_mask[cpu_tick[cpu] >> 4];
  315. show_leds(cpu);
  316. }
  317. profile_tick(CPU_PROFILING);
  318. if(!--prof_counter(cpu)) {
  319. int user = user_mode(regs);
  320. irq_enter();
  321. update_process_times(user);
  322. irq_exit();
  323. prof_counter(cpu) = prof_multiplier(cpu);
  324. }
  325. set_irq_regs(old_regs);
  326. }
  327. extern unsigned int lvl14_resolution;
  328. static void __init smp_setup_percpu_timer(void)
  329. {
  330. int cpu = hard_smp4d_processor_id();
  331. prof_counter(cpu) = prof_multiplier(cpu) = 1;
  332. load_profile_irq(cpu, lvl14_resolution);
  333. }
  334. void __init smp4d_blackbox_id(unsigned *addr)
  335. {
  336. int rd = *addr & 0x3e000000;
  337. addr[0] = 0xc0800800 | rd; /* lda [%g0] ASI_M_VIKING_TMP1, reg */
  338. addr[1] = 0x01000000; /* nop */
  339. addr[2] = 0x01000000; /* nop */
  340. }
  341. void __init smp4d_blackbox_current(unsigned *addr)
  342. {
  343. int rd = *addr & 0x3e000000;
  344. addr[0] = 0xc0800800 | rd; /* lda [%g0] ASI_M_VIKING_TMP1, reg */
  345. addr[2] = 0x81282002 | rd | (rd >> 11); /* sll reg, 2, reg */
  346. addr[4] = 0x01000000; /* nop */
  347. }
  348. void __init sun4d_init_smp(void)
  349. {
  350. int i;
  351. extern unsigned int t_nmi[], linux_trap_ipi15_sun4d[], linux_trap_ipi15_sun4m[];
  352. /* Patch ipi15 trap table */
  353. t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_sun4d - linux_trap_ipi15_sun4m);
  354. /* And set btfixup... */
  355. BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4d_blackbox_id);
  356. BTFIXUPSET_BLACKBOX(load_current, smp4d_blackbox_current);
  357. BTFIXUPSET_CALL(smp_cross_call, smp4d_cross_call, BTFIXUPCALL_NORM);
  358. BTFIXUPSET_CALL(smp_message_pass, smp4d_message_pass, BTFIXUPCALL_NORM);
  359. BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4d_processor_id, BTFIXUPCALL_NORM);
  360. for (i = 0; i < NR_CPUS; i++) {
  361. ccall_info.processors_in[i] = 1;
  362. ccall_info.processors_out[i] = 1;
  363. }
  364. }