sun4m_smp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 int smp_num_cpus;
  36. extern volatile unsigned long cpu_callin_map[NR_CPUS];
  37. extern unsigned char boot_cpu_id;
  38. extern int smp_activated;
  39. extern volatile int __cpu_number_map[NR_CPUS];
  40. extern volatile int __cpu_logical_map[NR_CPUS];
  41. extern volatile unsigned long ipi_count;
  42. extern volatile int smp_process_available;
  43. extern volatile int smp_commenced;
  44. extern int __smp4m_processor_id(void);
  45. /*#define SMP_DEBUG*/
  46. #ifdef SMP_DEBUG
  47. #define SMP_PRINTK(x) printk x
  48. #else
  49. #define SMP_PRINTK(x)
  50. #endif
  51. static inline unsigned long swap(volatile unsigned long *ptr, unsigned long val)
  52. {
  53. __asm__ __volatile__("swap [%1], %0\n\t" :
  54. "=&r" (val), "=&r" (ptr) :
  55. "0" (val), "1" (ptr));
  56. return val;
  57. }
  58. static void smp_setup_percpu_timer(void);
  59. extern void cpu_probe(void);
  60. void __init smp4m_callin(void)
  61. {
  62. int cpuid = hard_smp_processor_id();
  63. local_flush_cache_all();
  64. local_flush_tlb_all();
  65. set_irq_udt(boot_cpu_id);
  66. /* Get our local ticker going. */
  67. smp_setup_percpu_timer();
  68. calibrate_delay();
  69. smp_store_cpu_info(cpuid);
  70. local_flush_cache_all();
  71. local_flush_tlb_all();
  72. /*
  73. * Unblock the master CPU _only_ when the scheduler state
  74. * of all secondary CPUs will be up-to-date, so after
  75. * the SMP initialization the master will be just allowed
  76. * to call the scheduler code.
  77. */
  78. /* Allow master to continue. */
  79. swap((unsigned long *)&cpu_callin_map[cpuid], 1);
  80. local_flush_cache_all();
  81. local_flush_tlb_all();
  82. cpu_probe();
  83. /* Fix idle thread fields. */
  84. __asm__ __volatile__("ld [%0], %%g6\n\t"
  85. : : "r" (&current_set[cpuid])
  86. : "memory" /* paranoid */);
  87. /* Attach to the address space of init_task. */
  88. atomic_inc(&init_mm.mm_count);
  89. current->active_mm = &init_mm;
  90. while(!smp_commenced)
  91. barrier();
  92. local_flush_cache_all();
  93. local_flush_tlb_all();
  94. local_irq_enable();
  95. }
  96. extern void init_IRQ(void);
  97. extern void cpu_panic(void);
  98. /*
  99. * Cycle through the processors asking the PROM to start each one.
  100. */
  101. extern struct linux_prom_registers smp_penguin_ctable;
  102. extern unsigned long trapbase_cpu1[];
  103. extern unsigned long trapbase_cpu2[];
  104. extern unsigned long trapbase_cpu3[];
  105. void __init smp4m_boot_cpus(void)
  106. {
  107. int cpucount = 0;
  108. int i, mid;
  109. printk("Entering SMP Mode...\n");
  110. local_irq_enable();
  111. cpus_clear(cpu_present_map);
  112. for (i = 0; !cpu_find_by_instance(i, NULL, &mid); i++)
  113. cpu_set(mid, cpu_present_map);
  114. for(i=0; i < NR_CPUS; i++) {
  115. __cpu_number_map[i] = -1;
  116. __cpu_logical_map[i] = -1;
  117. }
  118. __cpu_number_map[boot_cpu_id] = 0;
  119. __cpu_logical_map[0] = boot_cpu_id;
  120. current_thread_info()->cpu = boot_cpu_id;
  121. smp_store_cpu_info(boot_cpu_id);
  122. set_irq_udt(boot_cpu_id);
  123. smp_setup_percpu_timer();
  124. local_flush_cache_all();
  125. if(cpu_find_by_instance(1, NULL, NULL))
  126. return; /* Not an MP box. */
  127. for(i = 0; i < NR_CPUS; i++) {
  128. if(i == boot_cpu_id)
  129. continue;
  130. if (cpu_isset(i, cpu_present_map)) {
  131. extern unsigned long sun4m_cpu_startup;
  132. unsigned long *entry = &sun4m_cpu_startup;
  133. struct task_struct *p;
  134. int timeout;
  135. /* Cook up an idler for this guy. */
  136. p = fork_idle(i);
  137. cpucount++;
  138. current_set[i] = task_thread_info(p);
  139. /* See trampoline.S for details... */
  140. entry += ((i-1) * 3);
  141. /*
  142. * Initialize the contexts table
  143. * Since the call to prom_startcpu() trashes the structure,
  144. * we need to re-initialize it for each cpu
  145. */
  146. smp_penguin_ctable.which_io = 0;
  147. smp_penguin_ctable.phys_addr = (unsigned int) srmmu_ctx_table_phys;
  148. smp_penguin_ctable.reg_size = 0;
  149. /* whirrr, whirrr, whirrrrrrrrr... */
  150. printk("Starting CPU %d at %p\n", i, entry);
  151. local_flush_cache_all();
  152. prom_startcpu(cpu_data(i).prom_node,
  153. &smp_penguin_ctable, 0, (char *)entry);
  154. /* wheee... it's going... */
  155. for(timeout = 0; timeout < 10000; timeout++) {
  156. if(cpu_callin_map[i])
  157. break;
  158. udelay(200);
  159. }
  160. if(cpu_callin_map[i]) {
  161. /* Another "Red Snapper". */
  162. __cpu_number_map[i] = i;
  163. __cpu_logical_map[i] = i;
  164. } else {
  165. cpucount--;
  166. printk("Processor %d is stuck.\n", i);
  167. }
  168. }
  169. if(!(cpu_callin_map[i])) {
  170. cpu_clear(i, cpu_present_map);
  171. __cpu_number_map[i] = -1;
  172. }
  173. }
  174. local_flush_cache_all();
  175. if(cpucount == 0) {
  176. printk("Error: only one Processor found.\n");
  177. cpu_present_map = cpumask_of_cpu(smp_processor_id());
  178. } else {
  179. unsigned long bogosum = 0;
  180. for_each_present_cpu(i)
  181. bogosum += cpu_data(i).udelay_val;
  182. printk("Total of %d Processors activated (%lu.%02lu BogoMIPS).\n",
  183. cpucount + 1,
  184. bogosum/(500000/HZ),
  185. (bogosum/(5000/HZ))%100);
  186. smp_activated = 1;
  187. smp_num_cpus = cpucount + 1;
  188. }
  189. /* Free unneeded trap tables */
  190. if (!cpu_isset(i, cpu_present_map)) {
  191. ClearPageReserved(virt_to_page(trapbase_cpu1));
  192. init_page_count(virt_to_page(trapbase_cpu1));
  193. free_page((unsigned long)trapbase_cpu1);
  194. totalram_pages++;
  195. num_physpages++;
  196. }
  197. if (!cpu_isset(2, cpu_present_map)) {
  198. ClearPageReserved(virt_to_page(trapbase_cpu2));
  199. init_page_count(virt_to_page(trapbase_cpu2));
  200. free_page((unsigned long)trapbase_cpu2);
  201. totalram_pages++;
  202. num_physpages++;
  203. }
  204. if (!cpu_isset(3, cpu_present_map)) {
  205. ClearPageReserved(virt_to_page(trapbase_cpu3));
  206. init_page_count(virt_to_page(trapbase_cpu3));
  207. free_page((unsigned long)trapbase_cpu3);
  208. totalram_pages++;
  209. num_physpages++;
  210. }
  211. /* Ok, they are spinning and ready to go. */
  212. smp_processors_ready = 1;
  213. }
  214. /* At each hardware IRQ, we get this called to forward IRQ reception
  215. * to the next processor. The caller must disable the IRQ level being
  216. * serviced globally so that there are no double interrupts received.
  217. *
  218. * XXX See sparc64 irq.c.
  219. */
  220. void smp4m_irq_rotate(int cpu)
  221. {
  222. }
  223. /* Cross calls, in order to work efficiently and atomically do all
  224. * the message passing work themselves, only stopcpu and reschedule
  225. * messages come through here.
  226. */
  227. void smp4m_message_pass(int target, int msg, unsigned long data, int wait)
  228. {
  229. static unsigned long smp_cpu_in_msg[NR_CPUS];
  230. cpumask_t mask;
  231. int me = smp_processor_id();
  232. int irq, i;
  233. if(msg == MSG_RESCHEDULE) {
  234. irq = IRQ_RESCHEDULE;
  235. if(smp_cpu_in_msg[me])
  236. return;
  237. } else if(msg == MSG_STOP_CPU) {
  238. irq = IRQ_STOP_CPU;
  239. } else {
  240. goto barf;
  241. }
  242. smp_cpu_in_msg[me]++;
  243. if(target == MSG_ALL_BUT_SELF || target == MSG_ALL) {
  244. mask = cpu_present_map;
  245. if(target == MSG_ALL_BUT_SELF)
  246. cpu_clear(me, mask);
  247. for(i = 0; i < 4; i++) {
  248. if (cpu_isset(i, mask))
  249. set_cpu_int(i, irq);
  250. }
  251. } else {
  252. set_cpu_int(target, irq);
  253. }
  254. smp_cpu_in_msg[me]--;
  255. return;
  256. barf:
  257. printk("Yeeee, trying to send SMP msg(%d) on cpu %d\n", msg, me);
  258. panic("Bogon SMP message pass.");
  259. }
  260. static struct smp_funcall {
  261. smpfunc_t func;
  262. unsigned long arg1;
  263. unsigned long arg2;
  264. unsigned long arg3;
  265. unsigned long arg4;
  266. unsigned long arg5;
  267. unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
  268. unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
  269. } ccall_info;
  270. static DEFINE_SPINLOCK(cross_call_lock);
  271. /* Cross calls must be serialized, at least currently. */
  272. void smp4m_cross_call(smpfunc_t func, unsigned long arg1, unsigned long arg2,
  273. unsigned long arg3, unsigned long arg4, unsigned long arg5)
  274. {
  275. if(smp_processors_ready) {
  276. register int ncpus = smp_num_cpus;
  277. unsigned long flags;
  278. spin_lock_irqsave(&cross_call_lock, flags);
  279. /* Init function glue. */
  280. ccall_info.func = func;
  281. ccall_info.arg1 = arg1;
  282. ccall_info.arg2 = arg2;
  283. ccall_info.arg3 = arg3;
  284. ccall_info.arg4 = arg4;
  285. ccall_info.arg5 = arg5;
  286. /* Init receive/complete mapping, plus fire the IPI's off. */
  287. {
  288. cpumask_t mask = cpu_present_map;
  289. register int i;
  290. cpu_clear(smp_processor_id(), mask);
  291. for(i = 0; i < ncpus; i++) {
  292. if (cpu_isset(i, mask)) {
  293. ccall_info.processors_in[i] = 0;
  294. ccall_info.processors_out[i] = 0;
  295. set_cpu_int(i, IRQ_CROSS_CALL);
  296. } else {
  297. ccall_info.processors_in[i] = 1;
  298. ccall_info.processors_out[i] = 1;
  299. }
  300. }
  301. }
  302. {
  303. register int i;
  304. i = 0;
  305. do {
  306. while(!ccall_info.processors_in[i])
  307. barrier();
  308. } while(++i < ncpus);
  309. i = 0;
  310. do {
  311. while(!ccall_info.processors_out[i])
  312. barrier();
  313. } while(++i < ncpus);
  314. }
  315. spin_unlock_irqrestore(&cross_call_lock, flags);
  316. }
  317. }
  318. /* Running cross calls. */
  319. void smp4m_cross_call_irq(void)
  320. {
  321. int i = smp_processor_id();
  322. ccall_info.processors_in[i] = 1;
  323. ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
  324. ccall_info.arg4, ccall_info.arg5);
  325. ccall_info.processors_out[i] = 1;
  326. }
  327. void smp4m_percpu_timer_interrupt(struct pt_regs *regs)
  328. {
  329. int cpu = smp_processor_id();
  330. clear_profile_irq(cpu);
  331. profile_tick(CPU_PROFILING, regs);
  332. if(!--prof_counter(cpu)) {
  333. int user = user_mode(regs);
  334. irq_enter();
  335. update_process_times(user);
  336. irq_exit();
  337. prof_counter(cpu) = prof_multiplier(cpu);
  338. }
  339. }
  340. extern unsigned int lvl14_resolution;
  341. static void __init smp_setup_percpu_timer(void)
  342. {
  343. int cpu = smp_processor_id();
  344. prof_counter(cpu) = prof_multiplier(cpu) = 1;
  345. load_profile_irq(cpu, lvl14_resolution);
  346. if(cpu == boot_cpu_id)
  347. enable_pil_irq(14);
  348. }
  349. void __init smp4m_blackbox_id(unsigned *addr)
  350. {
  351. int rd = *addr & 0x3e000000;
  352. int rs1 = rd >> 11;
  353. addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
  354. addr[1] = 0x8130200c | rd | rs1; /* srl reg, 0xc, reg */
  355. addr[2] = 0x80082003 | rd | rs1; /* and reg, 3, reg */
  356. }
  357. void __init smp4m_blackbox_current(unsigned *addr)
  358. {
  359. int rd = *addr & 0x3e000000;
  360. int rs1 = rd >> 11;
  361. addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
  362. addr[2] = 0x8130200a | rd | rs1; /* srl reg, 0xa, reg */
  363. addr[4] = 0x8008200c | rd | rs1; /* and reg, 3, reg */
  364. }
  365. void __init sun4m_init_smp(void)
  366. {
  367. BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4m_blackbox_id);
  368. BTFIXUPSET_BLACKBOX(load_current, smp4m_blackbox_current);
  369. BTFIXUPSET_CALL(smp_cross_call, smp4m_cross_call, BTFIXUPCALL_NORM);
  370. BTFIXUPSET_CALL(smp_message_pass, smp4m_message_pass, BTFIXUPCALL_NORM);
  371. BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4m_processor_id, BTFIXUPCALL_NORM);
  372. }