sun4d_smp.c 9.6 KB

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