sun4m_smp.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* sun4m_smp.c: Sparc SUN4M SMP support.
  2. *
  3. * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
  4. */
  5. #include <asm/head.h>
  6. #include <linux/kernel.h>
  7. #include <linux/sched.h>
  8. #include <linux/threads.h>
  9. #include <linux/smp.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel_stat.h>
  12. #include <linux/init.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/mm.h>
  15. #include <linux/swap.h>
  16. #include <linux/profile.h>
  17. #include <linux/delay.h>
  18. #include <linux/cpu.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/irq_regs.h>
  22. #include <asm/ptrace.h>
  23. #include <asm/atomic.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/cpudata.h>
  30. #include "irq.h"
  31. #define IRQ_CROSS_CALL 15
  32. extern ctxd_t *srmmu_ctx_table_phys;
  33. extern volatile unsigned long cpu_callin_map[NR_CPUS];
  34. extern unsigned char boot_cpu_id;
  35. extern cpumask_t smp_commenced_mask;
  36. extern int __smp4m_processor_id(void);
  37. /*#define SMP_DEBUG*/
  38. #ifdef SMP_DEBUG
  39. #define SMP_PRINTK(x) printk x
  40. #else
  41. #define SMP_PRINTK(x)
  42. #endif
  43. static inline unsigned long
  44. swap_ulong(volatile unsigned long *ptr, unsigned long val)
  45. {
  46. __asm__ __volatile__("swap [%1], %0\n\t" :
  47. "=&r" (val), "=&r" (ptr) :
  48. "0" (val), "1" (ptr));
  49. return val;
  50. }
  51. static void smp_setup_percpu_timer(void);
  52. extern void cpu_probe(void);
  53. void __cpuinit smp4m_callin(void)
  54. {
  55. int cpuid = hard_smp_processor_id();
  56. local_flush_cache_all();
  57. local_flush_tlb_all();
  58. notify_cpu_starting(cpuid);
  59. /* Get our local ticker going. */
  60. smp_setup_percpu_timer();
  61. calibrate_delay();
  62. smp_store_cpu_info(cpuid);
  63. local_flush_cache_all();
  64. local_flush_tlb_all();
  65. /*
  66. * Unblock the master CPU _only_ when the scheduler state
  67. * of all secondary CPUs will be up-to-date, so after
  68. * the SMP initialization the master will be just allowed
  69. * to call the scheduler code.
  70. */
  71. /* Allow master to continue. */
  72. swap_ulong(&cpu_callin_map[cpuid], 1);
  73. /* XXX: What's up with all the flushes? */
  74. local_flush_cache_all();
  75. local_flush_tlb_all();
  76. cpu_probe();
  77. /* Fix idle thread fields. */
  78. __asm__ __volatile__("ld [%0], %%g6\n\t"
  79. : : "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. set_cpu_online(cpuid, true);
  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 smp4m_boot_cpus(void)
  94. {
  95. smp_setup_percpu_timer();
  96. local_flush_cache_all();
  97. }
  98. int __cpuinit smp4m_boot_one_cpu(int i)
  99. {
  100. extern unsigned long sun4m_cpu_startup;
  101. unsigned long *entry = &sun4m_cpu_startup;
  102. struct task_struct *p;
  103. int timeout;
  104. int cpu_node;
  105. cpu_find_by_mid(i, &cpu_node);
  106. /* Cook up an idler for this guy. */
  107. p = fork_idle(i);
  108. current_set[i] = task_thread_info(p);
  109. /* See trampoline.S for details... */
  110. entry += ((i-1) * 3);
  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("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. /* wheee... it's going... */
  125. for(timeout = 0; timeout < 10000; timeout++) {
  126. if(cpu_callin_map[i])
  127. break;
  128. udelay(200);
  129. }
  130. if (!(cpu_callin_map[i])) {
  131. printk("Processor %d is stuck.\n", i);
  132. return -ENODEV;
  133. }
  134. local_flush_cache_all();
  135. return 0;
  136. }
  137. void __init smp4m_smp_done(void)
  138. {
  139. int i, first;
  140. int *prev;
  141. /* setup cpu list for irq rotation */
  142. first = 0;
  143. prev = &first;
  144. for_each_online_cpu(i) {
  145. *prev = i;
  146. prev = &cpu_data(i).next;
  147. }
  148. *prev = first;
  149. local_flush_cache_all();
  150. /* Ok, they are spinning and ready to go. */
  151. }
  152. /* At each hardware IRQ, we get this called to forward IRQ reception
  153. * to the next processor. The caller must disable the IRQ level being
  154. * serviced globally so that there are no double interrupts received.
  155. *
  156. * XXX See sparc64 irq.c.
  157. */
  158. void smp4m_irq_rotate(int cpu)
  159. {
  160. int next = cpu_data(cpu).next;
  161. if (next != cpu)
  162. set_irq_udt(next);
  163. }
  164. static struct smp_funcall {
  165. smpfunc_t func;
  166. unsigned long arg1;
  167. unsigned long arg2;
  168. unsigned long arg3;
  169. unsigned long arg4;
  170. unsigned long arg5;
  171. unsigned long processors_in[SUN4M_NCPUS]; /* Set when ipi entered. */
  172. unsigned long processors_out[SUN4M_NCPUS]; /* Set when ipi exited. */
  173. } ccall_info;
  174. static DEFINE_SPINLOCK(cross_call_lock);
  175. /* Cross calls must be serialized, at least currently. */
  176. static void smp4m_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
  177. unsigned long arg2, unsigned long arg3,
  178. unsigned long arg4)
  179. {
  180. register int ncpus = SUN4M_NCPUS;
  181. unsigned long flags;
  182. spin_lock_irqsave(&cross_call_lock, flags);
  183. /* Init function glue. */
  184. ccall_info.func = func;
  185. ccall_info.arg1 = arg1;
  186. ccall_info.arg2 = arg2;
  187. ccall_info.arg3 = arg3;
  188. ccall_info.arg4 = arg4;
  189. ccall_info.arg5 = 0;
  190. /* Init receive/complete mapping, plus fire the IPI's off. */
  191. {
  192. register int i;
  193. cpu_clear(smp_processor_id(), mask);
  194. cpus_and(mask, cpu_online_map, mask);
  195. for(i = 0; i < ncpus; i++) {
  196. if (cpu_isset(i, mask)) {
  197. ccall_info.processors_in[i] = 0;
  198. ccall_info.processors_out[i] = 0;
  199. set_cpu_int(i, IRQ_CROSS_CALL);
  200. } else {
  201. ccall_info.processors_in[i] = 1;
  202. ccall_info.processors_out[i] = 1;
  203. }
  204. }
  205. }
  206. {
  207. register int i;
  208. i = 0;
  209. do {
  210. if (!cpu_isset(i, mask))
  211. continue;
  212. while(!ccall_info.processors_in[i])
  213. barrier();
  214. } while(++i < ncpus);
  215. i = 0;
  216. do {
  217. if (!cpu_isset(i, mask))
  218. continue;
  219. while(!ccall_info.processors_out[i])
  220. barrier();
  221. } while(++i < ncpus);
  222. }
  223. spin_unlock_irqrestore(&cross_call_lock, flags);
  224. }
  225. /* Running cross calls. */
  226. void smp4m_cross_call_irq(void)
  227. {
  228. int i = smp_processor_id();
  229. ccall_info.processors_in[i] = 1;
  230. ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
  231. ccall_info.arg4, ccall_info.arg5);
  232. ccall_info.processors_out[i] = 1;
  233. }
  234. extern void sun4m_clear_profile_irq(int cpu);
  235. void smp4m_percpu_timer_interrupt(struct pt_regs *regs)
  236. {
  237. struct pt_regs *old_regs;
  238. int cpu = smp_processor_id();
  239. old_regs = set_irq_regs(regs);
  240. sun4m_clear_profile_irq(cpu);
  241. profile_tick(CPU_PROFILING);
  242. if(!--prof_counter(cpu)) {
  243. int user = user_mode(regs);
  244. irq_enter();
  245. update_process_times(user);
  246. irq_exit();
  247. prof_counter(cpu) = prof_multiplier(cpu);
  248. }
  249. set_irq_regs(old_regs);
  250. }
  251. extern unsigned int lvl14_resolution;
  252. static void __cpuinit smp_setup_percpu_timer(void)
  253. {
  254. int cpu = smp_processor_id();
  255. prof_counter(cpu) = prof_multiplier(cpu) = 1;
  256. load_profile_irq(cpu, lvl14_resolution);
  257. if(cpu == boot_cpu_id)
  258. enable_pil_irq(14);
  259. }
  260. static void __init smp4m_blackbox_id(unsigned *addr)
  261. {
  262. int rd = *addr & 0x3e000000;
  263. int rs1 = rd >> 11;
  264. addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
  265. addr[1] = 0x8130200c | rd | rs1; /* srl reg, 0xc, reg */
  266. addr[2] = 0x80082003 | rd | rs1; /* and reg, 3, reg */
  267. }
  268. static void __init smp4m_blackbox_current(unsigned *addr)
  269. {
  270. int rd = *addr & 0x3e000000;
  271. int rs1 = rd >> 11;
  272. addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
  273. addr[2] = 0x8130200a | rd | rs1; /* srl reg, 0xa, reg */
  274. addr[4] = 0x8008200c | rd | rs1; /* and reg, 0xc, reg */
  275. }
  276. void __init sun4m_init_smp(void)
  277. {
  278. BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4m_blackbox_id);
  279. BTFIXUPSET_BLACKBOX(load_current, smp4m_blackbox_current);
  280. BTFIXUPSET_CALL(smp_cross_call, smp4m_cross_call, BTFIXUPCALL_NORM);
  281. BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4m_processor_id, BTFIXUPCALL_NORM);
  282. }