sun4m_smp.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 <asm/cacheflush.h>
  19. #include <asm/tlbflush.h>
  20. #include <asm/irq_regs.h>
  21. #include <asm/ptrace.h>
  22. #include <asm/atomic.h>
  23. #include <asm/irq.h>
  24. #include <asm/page.h>
  25. #include <asm/pgalloc.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/oplib.h>
  28. #include <asm/cpudata.h>
  29. #include "irq.h"
  30. #define IRQ_CROSS_CALL 15
  31. extern ctxd_t *srmmu_ctx_table_phys;
  32. extern volatile unsigned long cpu_callin_map[NR_CPUS];
  33. extern unsigned char boot_cpu_id;
  34. extern cpumask_t smp_commenced_mask;
  35. extern int __smp4m_processor_id(void);
  36. /*#define SMP_DEBUG*/
  37. #ifdef SMP_DEBUG
  38. #define SMP_PRINTK(x) printk x
  39. #else
  40. #define SMP_PRINTK(x)
  41. #endif
  42. static inline unsigned long swap(volatile unsigned long *ptr, unsigned long val)
  43. {
  44. __asm__ __volatile__("swap [%1], %0\n\t" :
  45. "=&r" (val), "=&r" (ptr) :
  46. "0" (val), "1" (ptr));
  47. return val;
  48. }
  49. static void smp_setup_percpu_timer(void);
  50. extern void cpu_probe(void);
  51. void __cpuinit smp4m_callin(void)
  52. {
  53. int cpuid = hard_smp_processor_id();
  54. local_flush_cache_all();
  55. local_flush_tlb_all();
  56. notify_cpu_starting(cpuid);
  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. /*
  64. * Unblock the master CPU _only_ when the scheduler state
  65. * of all secondary CPUs will be up-to-date, so after
  66. * the SMP initialization the master will be just allowed
  67. * to call the scheduler code.
  68. */
  69. /* Allow master to continue. */
  70. swap(&cpu_callin_map[cpuid], 1);
  71. /* XXX: What's up with all the flushes? */
  72. local_flush_cache_all();
  73. local_flush_tlb_all();
  74. cpu_probe();
  75. /* Fix idle thread fields. */
  76. __asm__ __volatile__("ld [%0], %%g6\n\t"
  77. : : "r" (&current_set[cpuid])
  78. : "memory" /* paranoid */);
  79. /* Attach to the address space of init_task. */
  80. atomic_inc(&init_mm.mm_count);
  81. current->active_mm = &init_mm;
  82. while (!cpu_isset(cpuid, smp_commenced_mask))
  83. mb();
  84. local_irq_enable();
  85. cpu_set(cpuid, cpu_online_map);
  86. }
  87. /*
  88. * Cycle through the processors asking the PROM to start each one.
  89. */
  90. extern struct linux_prom_registers smp_penguin_ctable;
  91. extern unsigned long trapbase_cpu1[];
  92. extern unsigned long trapbase_cpu2[];
  93. extern unsigned long trapbase_cpu3[];
  94. void __init smp4m_boot_cpus(void)
  95. {
  96. smp_setup_percpu_timer();
  97. local_flush_cache_all();
  98. }
  99. int __cpuinit smp4m_boot_one_cpu(int i)
  100. {
  101. extern unsigned long sun4m_cpu_startup;
  102. unsigned long *entry = &sun4m_cpu_startup;
  103. struct task_struct *p;
  104. int timeout;
  105. int cpu_node;
  106. cpu_find_by_mid(i, &cpu_node);
  107. /* Cook up an idler for this guy. */
  108. p = fork_idle(i);
  109. current_set[i] = task_thread_info(p);
  110. /* See trampoline.S for details... */
  111. entry += ((i-1) * 3);
  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("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. /* 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("Processor %d is stuck.\n", i);
  133. return -ENODEV;
  134. }
  135. local_flush_cache_all();
  136. return 0;
  137. }
  138. void __init smp4m_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 (i = 0; i < NR_CPUS; i++) {
  146. if (cpu_online(i)) {
  147. *prev = i;
  148. prev = &cpu_data(i).next;
  149. }
  150. }
  151. *prev = first;
  152. local_flush_cache_all();
  153. /* Free unneeded trap tables */
  154. if (!cpu_isset(1, cpu_present_map)) {
  155. ClearPageReserved(virt_to_page(trapbase_cpu1));
  156. init_page_count(virt_to_page(trapbase_cpu1));
  157. free_page((unsigned long)trapbase_cpu1);
  158. totalram_pages++;
  159. num_physpages++;
  160. }
  161. if (!cpu_isset(2, cpu_present_map)) {
  162. ClearPageReserved(virt_to_page(trapbase_cpu2));
  163. init_page_count(virt_to_page(trapbase_cpu2));
  164. free_page((unsigned long)trapbase_cpu2);
  165. totalram_pages++;
  166. num_physpages++;
  167. }
  168. if (!cpu_isset(3, cpu_present_map)) {
  169. ClearPageReserved(virt_to_page(trapbase_cpu3));
  170. init_page_count(virt_to_page(trapbase_cpu3));
  171. free_page((unsigned long)trapbase_cpu3);
  172. totalram_pages++;
  173. num_physpages++;
  174. }
  175. /* Ok, they are spinning and ready to go. */
  176. }
  177. /* At each hardware IRQ, we get this called to forward IRQ reception
  178. * to the next processor. The caller must disable the IRQ level being
  179. * serviced globally so that there are no double interrupts received.
  180. *
  181. * XXX See sparc64 irq.c.
  182. */
  183. void smp4m_irq_rotate(int cpu)
  184. {
  185. int next = cpu_data(cpu).next;
  186. if (next != cpu)
  187. set_irq_udt(next);
  188. }
  189. static struct smp_funcall {
  190. smpfunc_t func;
  191. unsigned long arg1;
  192. unsigned long arg2;
  193. unsigned long arg3;
  194. unsigned long arg4;
  195. unsigned long arg5;
  196. unsigned long processors_in[SUN4M_NCPUS]; /* Set when ipi entered. */
  197. unsigned long processors_out[SUN4M_NCPUS]; /* Set when ipi exited. */
  198. } ccall_info;
  199. static DEFINE_SPINLOCK(cross_call_lock);
  200. /* Cross calls must be serialized, at least currently. */
  201. static void smp4m_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
  202. unsigned long arg2, unsigned long arg3,
  203. unsigned long arg4)
  204. {
  205. register int ncpus = SUN4M_NCPUS;
  206. unsigned long flags;
  207. spin_lock_irqsave(&cross_call_lock, flags);
  208. /* Init function glue. */
  209. ccall_info.func = func;
  210. ccall_info.arg1 = arg1;
  211. ccall_info.arg2 = arg2;
  212. ccall_info.arg3 = arg3;
  213. ccall_info.arg4 = arg4;
  214. ccall_info.arg5 = 0;
  215. /* Init receive/complete mapping, plus fire the IPI's off. */
  216. {
  217. register int i;
  218. cpu_clear(smp_processor_id(), mask);
  219. cpus_and(mask, cpu_online_map, mask);
  220. for(i = 0; i < ncpus; i++) {
  221. if (cpu_isset(i, mask)) {
  222. ccall_info.processors_in[i] = 0;
  223. ccall_info.processors_out[i] = 0;
  224. set_cpu_int(i, IRQ_CROSS_CALL);
  225. } else {
  226. ccall_info.processors_in[i] = 1;
  227. ccall_info.processors_out[i] = 1;
  228. }
  229. }
  230. }
  231. {
  232. register int i;
  233. i = 0;
  234. do {
  235. if (!cpu_isset(i, mask))
  236. continue;
  237. while(!ccall_info.processors_in[i])
  238. barrier();
  239. } while(++i < ncpus);
  240. i = 0;
  241. do {
  242. if (!cpu_isset(i, mask))
  243. continue;
  244. while(!ccall_info.processors_out[i])
  245. barrier();
  246. } while(++i < ncpus);
  247. }
  248. spin_unlock_irqrestore(&cross_call_lock, flags);
  249. }
  250. /* Running cross calls. */
  251. void smp4m_cross_call_irq(void)
  252. {
  253. int i = smp_processor_id();
  254. ccall_info.processors_in[i] = 1;
  255. ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
  256. ccall_info.arg4, ccall_info.arg5);
  257. ccall_info.processors_out[i] = 1;
  258. }
  259. void smp4m_percpu_timer_interrupt(struct pt_regs *regs)
  260. {
  261. struct pt_regs *old_regs;
  262. int cpu = smp_processor_id();
  263. old_regs = set_irq_regs(regs);
  264. clear_profile_irq(cpu);
  265. profile_tick(CPU_PROFILING);
  266. if(!--prof_counter(cpu)) {
  267. int user = user_mode(regs);
  268. irq_enter();
  269. update_process_times(user);
  270. irq_exit();
  271. prof_counter(cpu) = prof_multiplier(cpu);
  272. }
  273. set_irq_regs(old_regs);
  274. }
  275. extern unsigned int lvl14_resolution;
  276. static void __init smp_setup_percpu_timer(void)
  277. {
  278. int cpu = smp_processor_id();
  279. prof_counter(cpu) = prof_multiplier(cpu) = 1;
  280. load_profile_irq(cpu, lvl14_resolution);
  281. if(cpu == boot_cpu_id)
  282. enable_pil_irq(14);
  283. }
  284. static void __init smp4m_blackbox_id(unsigned *addr)
  285. {
  286. int rd = *addr & 0x3e000000;
  287. int rs1 = rd >> 11;
  288. addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
  289. addr[1] = 0x8130200c | rd | rs1; /* srl reg, 0xc, reg */
  290. addr[2] = 0x80082003 | rd | rs1; /* and reg, 3, reg */
  291. }
  292. static void __init smp4m_blackbox_current(unsigned *addr)
  293. {
  294. int rd = *addr & 0x3e000000;
  295. int rs1 = rd >> 11;
  296. addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
  297. addr[2] = 0x8130200a | rd | rs1; /* srl reg, 0xa, reg */
  298. addr[4] = 0x8008200c | rd | rs1; /* and reg, 0xc, reg */
  299. }
  300. void __init sun4m_init_smp(void)
  301. {
  302. BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4m_blackbox_id);
  303. BTFIXUPSET_BLACKBOX(load_current, smp4m_blackbox_current);
  304. BTFIXUPSET_CALL(smp_cross_call, smp4m_cross_call, BTFIXUPCALL_NORM);
  305. BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4m_processor_id, BTFIXUPCALL_NORM);
  306. }