sun4d_smp.c 10 KB

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