sun4m_smp.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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/smp_lock.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel_stat.h>
  13. #include <linux/init.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/mm.h>
  16. #include <linux/swap.h>
  17. #include <linux/profile.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/tlbflush.h>
  20. #include <asm/ptrace.h>
  21. #include <asm/atomic.h>
  22. #include <asm/delay.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. #define IRQ_RESCHEDULE 13
  30. #define IRQ_STOP_CPU 14
  31. #define IRQ_CROSS_CALL 15
  32. extern ctxd_t *srmmu_ctx_table_phys;
  33. extern void calibrate_delay(void);
  34. extern volatile int smp_processors_ready;
  35. extern volatile unsigned long cpu_callin_map[NR_CPUS];
  36. extern unsigned char boot_cpu_id;
  37. extern cpumask_t smp_commenced_mask;
  38. extern int __smp4m_processor_id(void);
  39. /*#define SMP_DEBUG*/
  40. #ifdef SMP_DEBUG
  41. #define SMP_PRINTK(x) printk x
  42. #else
  43. #define SMP_PRINTK(x)
  44. #endif
  45. static inline unsigned long swap(volatile unsigned long *ptr, unsigned long val)
  46. {
  47. __asm__ __volatile__("swap [%1], %0\n\t" :
  48. "=&r" (val), "=&r" (ptr) :
  49. "0" (val), "1" (ptr));
  50. return val;
  51. }
  52. static void smp_setup_percpu_timer(void);
  53. extern void cpu_probe(void);
  54. void __init smp4m_callin(void)
  55. {
  56. int cpuid = hard_smp_processor_id();
  57. local_flush_cache_all();
  58. local_flush_tlb_all();
  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(&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. cpu_set(cpuid, cpu_online_map);
  88. /* last one in gets all the interrupts (for testing) */
  89. set_irq_udt(boot_cpu_id);
  90. }
  91. extern void init_IRQ(void);
  92. extern void cpu_panic(void);
  93. /*
  94. * Cycle through the processors asking the PROM to start each one.
  95. */
  96. extern struct linux_prom_registers smp_penguin_ctable;
  97. extern unsigned long trapbase_cpu1[];
  98. extern unsigned long trapbase_cpu2[];
  99. extern unsigned long trapbase_cpu3[];
  100. void __init smp4m_boot_cpus(void)
  101. {
  102. smp_setup_percpu_timer();
  103. local_flush_cache_all();
  104. }
  105. int smp4m_boot_one_cpu(int i)
  106. {
  107. extern unsigned long sun4m_cpu_startup;
  108. unsigned long *entry = &sun4m_cpu_startup;
  109. struct task_struct *p;
  110. int timeout;
  111. int cpu_node;
  112. cpu_find_by_mid(i, &cpu_node);
  113. /* Cook up an idler for this guy. */
  114. p = fork_idle(i);
  115. current_set[i] = task_thread_info(p);
  116. /* See trampoline.S for details... */
  117. entry += ((i-1) * 3);
  118. /*
  119. * Initialize the contexts table
  120. * Since the call to prom_startcpu() trashes the structure,
  121. * we need to re-initialize it for each cpu
  122. */
  123. smp_penguin_ctable.which_io = 0;
  124. smp_penguin_ctable.phys_addr = (unsigned int) srmmu_ctx_table_phys;
  125. smp_penguin_ctable.reg_size = 0;
  126. /* whirrr, whirrr, whirrrrrrrrr... */
  127. printk("Starting CPU %d at %p\n", i, entry);
  128. local_flush_cache_all();
  129. prom_startcpu(cpu_node,
  130. &smp_penguin_ctable, 0, (char *)entry);
  131. /* wheee... it's going... */
  132. for(timeout = 0; timeout < 10000; timeout++) {
  133. if(cpu_callin_map[i])
  134. break;
  135. udelay(200);
  136. }
  137. if (!(cpu_callin_map[i])) {
  138. printk("Processor %d is stuck.\n", i);
  139. return -ENODEV;
  140. }
  141. local_flush_cache_all();
  142. return 0;
  143. }
  144. void __init smp4m_smp_done(void)
  145. {
  146. int i, first;
  147. int *prev;
  148. /* setup cpu list for irq rotation */
  149. first = 0;
  150. prev = &first;
  151. for (i = 0; i < NR_CPUS; i++) {
  152. if (cpu_online(i)) {
  153. *prev = i;
  154. prev = &cpu_data(i).next;
  155. }
  156. }
  157. *prev = first;
  158. local_flush_cache_all();
  159. /* Free unneeded trap tables */
  160. if (!cpu_isset(1, cpu_present_map)) {
  161. ClearPageReserved(virt_to_page(trapbase_cpu1));
  162. init_page_count(virt_to_page(trapbase_cpu1));
  163. free_page((unsigned long)trapbase_cpu1);
  164. totalram_pages++;
  165. num_physpages++;
  166. }
  167. if (!cpu_isset(2, cpu_present_map)) {
  168. ClearPageReserved(virt_to_page(trapbase_cpu2));
  169. init_page_count(virt_to_page(trapbase_cpu2));
  170. free_page((unsigned long)trapbase_cpu2);
  171. totalram_pages++;
  172. num_physpages++;
  173. }
  174. if (!cpu_isset(3, cpu_present_map)) {
  175. ClearPageReserved(virt_to_page(trapbase_cpu3));
  176. init_page_count(virt_to_page(trapbase_cpu3));
  177. free_page((unsigned long)trapbase_cpu3);
  178. totalram_pages++;
  179. num_physpages++;
  180. }
  181. /* Ok, they are spinning and ready to go. */
  182. smp_processors_ready = 1;
  183. }
  184. /* At each hardware IRQ, we get this called to forward IRQ reception
  185. * to the next processor. The caller must disable the IRQ level being
  186. * serviced globally so that there are no double interrupts received.
  187. *
  188. * XXX See sparc64 irq.c.
  189. */
  190. void smp4m_irq_rotate(int cpu)
  191. {
  192. int next = cpu_data(cpu).next;
  193. if (next != cpu)
  194. set_irq_udt(next);
  195. }
  196. /* Cross calls, in order to work efficiently and atomically do all
  197. * the message passing work themselves, only stopcpu and reschedule
  198. * messages come through here.
  199. */
  200. void smp4m_message_pass(int target, int msg, unsigned long data, int wait)
  201. {
  202. static unsigned long smp_cpu_in_msg[NR_CPUS];
  203. cpumask_t mask;
  204. int me = smp_processor_id();
  205. int irq, i;
  206. if(msg == MSG_RESCHEDULE) {
  207. irq = IRQ_RESCHEDULE;
  208. if(smp_cpu_in_msg[me])
  209. return;
  210. } else if(msg == MSG_STOP_CPU) {
  211. irq = IRQ_STOP_CPU;
  212. } else {
  213. goto barf;
  214. }
  215. smp_cpu_in_msg[me]++;
  216. if(target == MSG_ALL_BUT_SELF || target == MSG_ALL) {
  217. mask = cpu_online_map;
  218. if(target == MSG_ALL_BUT_SELF)
  219. cpu_clear(me, mask);
  220. for(i = 0; i < 4; i++) {
  221. if (cpu_isset(i, mask))
  222. set_cpu_int(i, irq);
  223. }
  224. } else {
  225. set_cpu_int(target, irq);
  226. }
  227. smp_cpu_in_msg[me]--;
  228. return;
  229. barf:
  230. printk("Yeeee, trying to send SMP msg(%d) on cpu %d\n", msg, me);
  231. panic("Bogon SMP message pass.");
  232. }
  233. static struct smp_funcall {
  234. smpfunc_t func;
  235. unsigned long arg1;
  236. unsigned long arg2;
  237. unsigned long arg3;
  238. unsigned long arg4;
  239. unsigned long arg5;
  240. unsigned long processors_in[SUN4M_NCPUS]; /* Set when ipi entered. */
  241. unsigned long processors_out[SUN4M_NCPUS]; /* Set when ipi exited. */
  242. } ccall_info;
  243. static DEFINE_SPINLOCK(cross_call_lock);
  244. /* Cross calls must be serialized, at least currently. */
  245. void smp4m_cross_call(smpfunc_t func, unsigned long arg1, unsigned long arg2,
  246. unsigned long arg3, unsigned long arg4, unsigned long arg5)
  247. {
  248. register int ncpus = SUN4M_NCPUS;
  249. unsigned long flags;
  250. spin_lock_irqsave(&cross_call_lock, flags);
  251. /* Init function glue. */
  252. ccall_info.func = func;
  253. ccall_info.arg1 = arg1;
  254. ccall_info.arg2 = arg2;
  255. ccall_info.arg3 = arg3;
  256. ccall_info.arg4 = arg4;
  257. ccall_info.arg5 = arg5;
  258. /* Init receive/complete mapping, plus fire the IPI's off. */
  259. {
  260. cpumask_t mask = cpu_online_map;
  261. register int i;
  262. cpu_clear(smp_processor_id(), mask);
  263. for(i = 0; i < ncpus; i++) {
  264. if (cpu_isset(i, mask)) {
  265. ccall_info.processors_in[i] = 0;
  266. ccall_info.processors_out[i] = 0;
  267. set_cpu_int(i, IRQ_CROSS_CALL);
  268. } else {
  269. ccall_info.processors_in[i] = 1;
  270. ccall_info.processors_out[i] = 1;
  271. }
  272. }
  273. }
  274. {
  275. register int i;
  276. i = 0;
  277. do {
  278. while(!ccall_info.processors_in[i])
  279. barrier();
  280. } while(++i < ncpus);
  281. i = 0;
  282. do {
  283. while(!ccall_info.processors_out[i])
  284. barrier();
  285. } while(++i < ncpus);
  286. }
  287. spin_unlock_irqrestore(&cross_call_lock, flags);
  288. }
  289. /* Running cross calls. */
  290. void smp4m_cross_call_irq(void)
  291. {
  292. int i = smp_processor_id();
  293. ccall_info.processors_in[i] = 1;
  294. ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
  295. ccall_info.arg4, ccall_info.arg5);
  296. ccall_info.processors_out[i] = 1;
  297. }
  298. void smp4m_percpu_timer_interrupt(struct pt_regs *regs)
  299. {
  300. int cpu = smp_processor_id();
  301. clear_profile_irq(cpu);
  302. profile_tick(CPU_PROFILING, regs);
  303. if(!--prof_counter(cpu)) {
  304. int user = user_mode(regs);
  305. irq_enter();
  306. update_process_times(user);
  307. irq_exit();
  308. prof_counter(cpu) = prof_multiplier(cpu);
  309. }
  310. }
  311. extern unsigned int lvl14_resolution;
  312. static void __init smp_setup_percpu_timer(void)
  313. {
  314. int cpu = smp_processor_id();
  315. prof_counter(cpu) = prof_multiplier(cpu) = 1;
  316. load_profile_irq(cpu, lvl14_resolution);
  317. if(cpu == boot_cpu_id)
  318. enable_pil_irq(14);
  319. }
  320. void __init smp4m_blackbox_id(unsigned *addr)
  321. {
  322. int rd = *addr & 0x3e000000;
  323. int rs1 = rd >> 11;
  324. addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
  325. addr[1] = 0x8130200c | rd | rs1; /* srl reg, 0xc, reg */
  326. addr[2] = 0x80082003 | rd | rs1; /* and reg, 3, reg */
  327. }
  328. void __init smp4m_blackbox_current(unsigned *addr)
  329. {
  330. int rd = *addr & 0x3e000000;
  331. int rs1 = rd >> 11;
  332. addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
  333. addr[2] = 0x8130200a | rd | rs1; /* srl reg, 0xa, reg */
  334. addr[4] = 0x8008200c | rd | rs1; /* and reg, 3, reg */
  335. }
  336. void __init sun4m_init_smp(void)
  337. {
  338. BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4m_blackbox_id);
  339. BTFIXUPSET_BLACKBOX(load_current, smp4m_blackbox_current);
  340. BTFIXUPSET_CALL(smp_cross_call, smp4m_cross_call, BTFIXUPCALL_NORM);
  341. BTFIXUPSET_CALL(smp_message_pass, smp4m_message_pass, BTFIXUPCALL_NORM);
  342. BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4m_processor_id, BTFIXUPCALL_NORM);
  343. }