sun4d_smp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /* 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 <linux/interrupt.h>
  9. #include <linux/profile.h>
  10. #include <linux/delay.h>
  11. #include <linux/cpu.h>
  12. #include <asm/sbi.h>
  13. #include <asm/mmu.h>
  14. #include <asm/tlbflush.h>
  15. #include <asm/switch_to.h>
  16. #include <asm/cacheflush.h>
  17. #include "kernel.h"
  18. #include "irq.h"
  19. #define IRQ_CROSS_CALL 15
  20. static volatile int smp_processors_ready;
  21. static int smp_highest_cpu;
  22. static inline unsigned long sun4d_swap(volatile unsigned long *ptr, unsigned long val)
  23. {
  24. __asm__ __volatile__("swap [%1], %0\n\t" :
  25. "=&r" (val), "=&r" (ptr) :
  26. "0" (val), "1" (ptr));
  27. return val;
  28. }
  29. static void smp4d_ipi_init(void);
  30. static void smp_setup_percpu_timer(void);
  31. static unsigned char cpu_leds[32];
  32. static inline void show_leds(int cpuid)
  33. {
  34. cpuid &= 0x1e;
  35. __asm__ __volatile__ ("stba %0, [%1] %2" : :
  36. "r" ((cpu_leds[cpuid] << 4) | cpu_leds[cpuid+1]),
  37. "r" (ECSR_BASE(cpuid) | BB_LEDS),
  38. "i" (ASI_M_CTL));
  39. }
  40. void __cpuinit smp4d_callin(void)
  41. {
  42. int cpuid = hard_smp4d_processor_id();
  43. unsigned long flags;
  44. /* Show we are alive */
  45. cpu_leds[cpuid] = 0x6;
  46. show_leds(cpuid);
  47. /* Enable level15 interrupt, disable level14 interrupt for now */
  48. cc_set_imsk((cc_get_imsk() & ~0x8000) | 0x4000);
  49. local_flush_cache_all();
  50. local_flush_tlb_all();
  51. notify_cpu_starting(cpuid);
  52. /*
  53. * Unblock the master CPU _only_ when the scheduler state
  54. * of all secondary CPUs will be up-to-date, so after
  55. * the SMP initialization the master will be just allowed
  56. * to call the scheduler code.
  57. */
  58. /* Get our local ticker going. */
  59. smp_setup_percpu_timer();
  60. calibrate_delay();
  61. smp_store_cpu_info(cpuid);
  62. local_flush_cache_all();
  63. local_flush_tlb_all();
  64. /* Allow master to continue. */
  65. sun4d_swap((unsigned long *)&cpu_callin_map[cpuid], 1);
  66. local_flush_cache_all();
  67. local_flush_tlb_all();
  68. while ((unsigned long)current_set[cpuid] < PAGE_OFFSET)
  69. barrier();
  70. while (current_set[cpuid]->cpu != cpuid)
  71. barrier();
  72. /* Fix idle thread fields. */
  73. __asm__ __volatile__("ld [%0], %%g6\n\t"
  74. : : "r" (&current_set[cpuid])
  75. : "memory" /* paranoid */);
  76. cpu_leds[cpuid] = 0x9;
  77. show_leds(cpuid);
  78. /* Attach to the address space of init_task. */
  79. atomic_inc(&init_mm.mm_count);
  80. current->active_mm = &init_mm;
  81. local_flush_cache_all();
  82. local_flush_tlb_all();
  83. local_irq_enable(); /* We don't allow PIL 14 yet */
  84. while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
  85. barrier();
  86. spin_lock_irqsave(&sun4d_imsk_lock, flags);
  87. cc_set_imsk(cc_get_imsk() & ~0x4000); /* Allow PIL 14 as well */
  88. spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
  89. set_cpu_online(cpuid, true);
  90. }
  91. /*
  92. * Cycle through the processors asking the PROM to start each one.
  93. */
  94. void __init smp4d_boot_cpus(void)
  95. {
  96. smp4d_ipi_init();
  97. if (boot_cpu_id)
  98. current_set[0] = NULL;
  99. smp_setup_percpu_timer();
  100. local_flush_cache_all();
  101. }
  102. int __cpuinit smp4d_boot_one_cpu(int i)
  103. {
  104. unsigned long *entry = &sun4d_cpu_startup;
  105. struct task_struct *p;
  106. int timeout;
  107. int cpu_node;
  108. cpu_find_by_instance(i, &cpu_node, NULL);
  109. /* Cook up an idler for this guy. */
  110. p = fork_idle(i);
  111. current_set[i] = task_thread_info(p);
  112. /*
  113. * Initialize the contexts table
  114. * Since the call to prom_startcpu() trashes the structure,
  115. * we need to re-initialize it for each cpu
  116. */
  117. smp_penguin_ctable.which_io = 0;
  118. smp_penguin_ctable.phys_addr = (unsigned int) srmmu_ctx_table_phys;
  119. smp_penguin_ctable.reg_size = 0;
  120. /* whirrr, whirrr, whirrrrrrrrr... */
  121. printk(KERN_INFO "Starting CPU %d at %p\n", i, entry);
  122. local_flush_cache_all();
  123. prom_startcpu(cpu_node,
  124. &smp_penguin_ctable, 0, (char *)entry);
  125. printk(KERN_INFO "prom_startcpu returned :)\n");
  126. /* wheee... it's going... */
  127. for (timeout = 0; timeout < 10000; timeout++) {
  128. if (cpu_callin_map[i])
  129. break;
  130. udelay(200);
  131. }
  132. if (!(cpu_callin_map[i])) {
  133. printk(KERN_ERR "Processor %d is stuck.\n", i);
  134. return -ENODEV;
  135. }
  136. local_flush_cache_all();
  137. return 0;
  138. }
  139. void __init smp4d_smp_done(void)
  140. {
  141. int i, first;
  142. int *prev;
  143. /* setup cpu list for irq rotation */
  144. first = 0;
  145. prev = &first;
  146. for_each_online_cpu(i) {
  147. *prev = i;
  148. prev = &cpu_data(i).next;
  149. }
  150. *prev = first;
  151. local_flush_cache_all();
  152. /* Ok, they are spinning and ready to go. */
  153. smp_processors_ready = 1;
  154. sun4d_distribute_irqs();
  155. }
  156. /* Memory structure giving interrupt handler information about IPI generated */
  157. struct sun4d_ipi_work {
  158. int single;
  159. int msk;
  160. int resched;
  161. };
  162. static DEFINE_PER_CPU_SHARED_ALIGNED(struct sun4d_ipi_work, sun4d_ipi_work);
  163. /* Initialize IPIs on the SUN4D SMP machine */
  164. static void __init smp4d_ipi_init(void)
  165. {
  166. int cpu;
  167. struct sun4d_ipi_work *work;
  168. printk(KERN_INFO "smp4d: setup IPI at IRQ %d\n", SUN4D_IPI_IRQ);
  169. for_each_possible_cpu(cpu) {
  170. work = &per_cpu(sun4d_ipi_work, cpu);
  171. work->single = work->msk = work->resched = 0;
  172. }
  173. }
  174. void sun4d_ipi_interrupt(void)
  175. {
  176. struct sun4d_ipi_work *work = &__get_cpu_var(sun4d_ipi_work);
  177. if (work->single) {
  178. work->single = 0;
  179. smp_call_function_single_interrupt();
  180. }
  181. if (work->msk) {
  182. work->msk = 0;
  183. smp_call_function_interrupt();
  184. }
  185. if (work->resched) {
  186. work->resched = 0;
  187. smp_resched_interrupt();
  188. }
  189. }
  190. static void smp4d_ipi_single(int cpu)
  191. {
  192. struct sun4d_ipi_work *work = &per_cpu(sun4d_ipi_work, cpu);
  193. /* Mark work */
  194. work->single = 1;
  195. /* Generate IRQ on the CPU */
  196. sun4d_send_ipi(cpu, SUN4D_IPI_IRQ);
  197. }
  198. static void smp4d_ipi_mask_one(int cpu)
  199. {
  200. struct sun4d_ipi_work *work = &per_cpu(sun4d_ipi_work, cpu);
  201. /* Mark work */
  202. work->msk = 1;
  203. /* Generate IRQ on the CPU */
  204. sun4d_send_ipi(cpu, SUN4D_IPI_IRQ);
  205. }
  206. static void smp4d_ipi_resched(int cpu)
  207. {
  208. struct sun4d_ipi_work *work = &per_cpu(sun4d_ipi_work, cpu);
  209. /* Mark work */
  210. work->resched = 1;
  211. /* Generate IRQ on the CPU (any IRQ will cause resched) */
  212. sun4d_send_ipi(cpu, SUN4D_IPI_IRQ);
  213. }
  214. static struct smp_funcall {
  215. smpfunc_t func;
  216. unsigned long arg1;
  217. unsigned long arg2;
  218. unsigned long arg3;
  219. unsigned long arg4;
  220. unsigned long arg5;
  221. unsigned char processors_in[NR_CPUS]; /* Set when ipi entered. */
  222. unsigned char processors_out[NR_CPUS]; /* Set when ipi exited. */
  223. } ccall_info __attribute__((aligned(8)));
  224. static DEFINE_SPINLOCK(cross_call_lock);
  225. /* Cross calls must be serialized, at least currently. */
  226. static void smp4d_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
  227. unsigned long arg2, unsigned long arg3,
  228. unsigned long arg4)
  229. {
  230. if (smp_processors_ready) {
  231. register int high = smp_highest_cpu;
  232. unsigned long flags;
  233. spin_lock_irqsave(&cross_call_lock, flags);
  234. {
  235. /*
  236. * If you make changes here, make sure
  237. * gcc generates proper code...
  238. */
  239. register smpfunc_t f asm("i0") = func;
  240. register unsigned long a1 asm("i1") = arg1;
  241. register unsigned long a2 asm("i2") = arg2;
  242. register unsigned long a3 asm("i3") = arg3;
  243. register unsigned long a4 asm("i4") = arg4;
  244. register unsigned long a5 asm("i5") = 0;
  245. __asm__ __volatile__(
  246. "std %0, [%6]\n\t"
  247. "std %2, [%6 + 8]\n\t"
  248. "std %4, [%6 + 16]\n\t" : :
  249. "r"(f), "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5),
  250. "r" (&ccall_info.func));
  251. }
  252. /* Init receive/complete mapping, plus fire the IPI's off. */
  253. {
  254. register int i;
  255. cpumask_clear_cpu(smp_processor_id(), &mask);
  256. cpumask_and(&mask, cpu_online_mask, &mask);
  257. for (i = 0; i <= high; i++) {
  258. if (cpumask_test_cpu(i, &mask)) {
  259. ccall_info.processors_in[i] = 0;
  260. ccall_info.processors_out[i] = 0;
  261. sun4d_send_ipi(i, IRQ_CROSS_CALL);
  262. }
  263. }
  264. }
  265. {
  266. register int i;
  267. i = 0;
  268. do {
  269. if (!cpumask_test_cpu(i, &mask))
  270. continue;
  271. while (!ccall_info.processors_in[i])
  272. barrier();
  273. } while (++i <= high);
  274. i = 0;
  275. do {
  276. if (!cpumask_test_cpu(i, &mask))
  277. continue;
  278. while (!ccall_info.processors_out[i])
  279. barrier();
  280. } while (++i <= high);
  281. }
  282. spin_unlock_irqrestore(&cross_call_lock, flags);
  283. }
  284. }
  285. /* Running cross calls. */
  286. void smp4d_cross_call_irq(void)
  287. {
  288. int i = hard_smp4d_processor_id();
  289. ccall_info.processors_in[i] = 1;
  290. ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
  291. ccall_info.arg4, ccall_info.arg5);
  292. ccall_info.processors_out[i] = 1;
  293. }
  294. void smp4d_percpu_timer_interrupt(struct pt_regs *regs)
  295. {
  296. struct pt_regs *old_regs;
  297. int cpu = hard_smp4d_processor_id();
  298. static int cpu_tick[NR_CPUS];
  299. static char led_mask[] = { 0xe, 0xd, 0xb, 0x7, 0xb, 0xd };
  300. old_regs = set_irq_regs(regs);
  301. bw_get_prof_limit(cpu);
  302. bw_clear_intr_mask(0, 1); /* INTR_TABLE[0] & 1 is Profile IRQ */
  303. cpu_tick[cpu]++;
  304. if (!(cpu_tick[cpu] & 15)) {
  305. if (cpu_tick[cpu] == 0x60)
  306. cpu_tick[cpu] = 0;
  307. cpu_leds[cpu] = led_mask[cpu_tick[cpu] >> 4];
  308. show_leds(cpu);
  309. }
  310. profile_tick(CPU_PROFILING);
  311. if (!--prof_counter(cpu)) {
  312. int user = user_mode(regs);
  313. irq_enter();
  314. update_process_times(user);
  315. irq_exit();
  316. prof_counter(cpu) = prof_multiplier(cpu);
  317. }
  318. set_irq_regs(old_regs);
  319. }
  320. static void __cpuinit smp_setup_percpu_timer(void)
  321. {
  322. int cpu = hard_smp4d_processor_id();
  323. prof_counter(cpu) = prof_multiplier(cpu) = 1;
  324. load_profile_irq(cpu, lvl14_resolution);
  325. }
  326. void __init smp4d_blackbox_id(unsigned *addr)
  327. {
  328. int rd = *addr & 0x3e000000;
  329. addr[0] = 0xc0800800 | rd; /* lda [%g0] ASI_M_VIKING_TMP1, reg */
  330. addr[1] = 0x01000000; /* nop */
  331. addr[2] = 0x01000000; /* nop */
  332. }
  333. void __init smp4d_blackbox_current(unsigned *addr)
  334. {
  335. int rd = *addr & 0x3e000000;
  336. addr[0] = 0xc0800800 | rd; /* lda [%g0] ASI_M_VIKING_TMP1, reg */
  337. addr[2] = 0x81282002 | rd | (rd >> 11); /* sll reg, 2, reg */
  338. addr[4] = 0x01000000; /* nop */
  339. }
  340. void __init sun4d_init_smp(void)
  341. {
  342. int i;
  343. /* Patch ipi15 trap table */
  344. t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_sun4d - linux_trap_ipi15_sun4m);
  345. /* And set btfixup... */
  346. BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4d_blackbox_id);
  347. BTFIXUPSET_BLACKBOX(load_current, smp4d_blackbox_current);
  348. BTFIXUPSET_CALL(smp_cross_call, smp4d_cross_call, BTFIXUPCALL_NORM);
  349. BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4d_processor_id, BTFIXUPCALL_NORM);
  350. BTFIXUPSET_CALL(smp_ipi_resched, smp4d_ipi_resched, BTFIXUPCALL_NORM);
  351. BTFIXUPSET_CALL(smp_ipi_single, smp4d_ipi_single, BTFIXUPCALL_NORM);
  352. BTFIXUPSET_CALL(smp_ipi_mask_one, smp4d_ipi_mask_one, BTFIXUPCALL_NORM);
  353. for (i = 0; i < NR_CPUS; i++) {
  354. ccall_info.processors_in[i] = 1;
  355. ccall_info.processors_out[i] = 1;
  356. }
  357. }