sun4m_smp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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] = p->thread_info;
  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(i = 0; i < NR_CPUS; i++) {
  181. if (cpu_isset(i, cpu_present_map))
  182. bogosum += cpu_data(i).udelay_val;
  183. }
  184. printk("Total of %d Processors activated (%lu.%02lu BogoMIPS).\n",
  185. cpucount + 1,
  186. bogosum/(500000/HZ),
  187. (bogosum/(5000/HZ))%100);
  188. smp_activated = 1;
  189. smp_num_cpus = cpucount + 1;
  190. }
  191. /* Free unneeded trap tables */
  192. if (!cpu_isset(i, cpu_present_map)) {
  193. ClearPageReserved(virt_to_page(trapbase_cpu1));
  194. set_page_count(virt_to_page(trapbase_cpu1), 1);
  195. free_page((unsigned long)trapbase_cpu1);
  196. totalram_pages++;
  197. num_physpages++;
  198. }
  199. if (!cpu_isset(2, cpu_present_map)) {
  200. ClearPageReserved(virt_to_page(trapbase_cpu2));
  201. set_page_count(virt_to_page(trapbase_cpu2), 1);
  202. free_page((unsigned long)trapbase_cpu2);
  203. totalram_pages++;
  204. num_physpages++;
  205. }
  206. if (!cpu_isset(3, cpu_present_map)) {
  207. ClearPageReserved(virt_to_page(trapbase_cpu3));
  208. set_page_count(virt_to_page(trapbase_cpu3), 1);
  209. free_page((unsigned long)trapbase_cpu3);
  210. totalram_pages++;
  211. num_physpages++;
  212. }
  213. /* Ok, they are spinning and ready to go. */
  214. smp_processors_ready = 1;
  215. }
  216. /* At each hardware IRQ, we get this called to forward IRQ reception
  217. * to the next processor. The caller must disable the IRQ level being
  218. * serviced globally so that there are no double interrupts received.
  219. *
  220. * XXX See sparc64 irq.c.
  221. */
  222. void smp4m_irq_rotate(int cpu)
  223. {
  224. }
  225. /* Cross calls, in order to work efficiently and atomically do all
  226. * the message passing work themselves, only stopcpu and reschedule
  227. * messages come through here.
  228. */
  229. void smp4m_message_pass(int target, int msg, unsigned long data, int wait)
  230. {
  231. static unsigned long smp_cpu_in_msg[NR_CPUS];
  232. cpumask_t mask;
  233. int me = smp_processor_id();
  234. int irq, i;
  235. if(msg == MSG_RESCHEDULE) {
  236. irq = IRQ_RESCHEDULE;
  237. if(smp_cpu_in_msg[me])
  238. return;
  239. } else if(msg == MSG_STOP_CPU) {
  240. irq = IRQ_STOP_CPU;
  241. } else {
  242. goto barf;
  243. }
  244. smp_cpu_in_msg[me]++;
  245. if(target == MSG_ALL_BUT_SELF || target == MSG_ALL) {
  246. mask = cpu_present_map;
  247. if(target == MSG_ALL_BUT_SELF)
  248. cpu_clear(me, mask);
  249. for(i = 0; i < 4; i++) {
  250. if (cpu_isset(i, mask))
  251. set_cpu_int(i, irq);
  252. }
  253. } else {
  254. set_cpu_int(target, irq);
  255. }
  256. smp_cpu_in_msg[me]--;
  257. return;
  258. barf:
  259. printk("Yeeee, trying to send SMP msg(%d) on cpu %d\n", msg, me);
  260. panic("Bogon SMP message pass.");
  261. }
  262. static struct smp_funcall {
  263. smpfunc_t func;
  264. unsigned long arg1;
  265. unsigned long arg2;
  266. unsigned long arg3;
  267. unsigned long arg4;
  268. unsigned long arg5;
  269. unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
  270. unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
  271. } ccall_info;
  272. static DEFINE_SPINLOCK(cross_call_lock);
  273. /* Cross calls must be serialized, at least currently. */
  274. void smp4m_cross_call(smpfunc_t func, unsigned long arg1, unsigned long arg2,
  275. unsigned long arg3, unsigned long arg4, unsigned long arg5)
  276. {
  277. if(smp_processors_ready) {
  278. register int ncpus = smp_num_cpus;
  279. unsigned long flags;
  280. spin_lock_irqsave(&cross_call_lock, flags);
  281. /* Init function glue. */
  282. ccall_info.func = func;
  283. ccall_info.arg1 = arg1;
  284. ccall_info.arg2 = arg2;
  285. ccall_info.arg3 = arg3;
  286. ccall_info.arg4 = arg4;
  287. ccall_info.arg5 = arg5;
  288. /* Init receive/complete mapping, plus fire the IPI's off. */
  289. {
  290. cpumask_t mask = cpu_present_map;
  291. register int i;
  292. cpu_clear(smp_processor_id(), mask);
  293. for(i = 0; i < ncpus; i++) {
  294. if (cpu_isset(i, mask)) {
  295. ccall_info.processors_in[i] = 0;
  296. ccall_info.processors_out[i] = 0;
  297. set_cpu_int(i, IRQ_CROSS_CALL);
  298. } else {
  299. ccall_info.processors_in[i] = 1;
  300. ccall_info.processors_out[i] = 1;
  301. }
  302. }
  303. }
  304. {
  305. register int i;
  306. i = 0;
  307. do {
  308. while(!ccall_info.processors_in[i])
  309. barrier();
  310. } while(++i < ncpus);
  311. i = 0;
  312. do {
  313. while(!ccall_info.processors_out[i])
  314. barrier();
  315. } while(++i < ncpus);
  316. }
  317. spin_unlock_irqrestore(&cross_call_lock, flags);
  318. }
  319. }
  320. /* Running cross calls. */
  321. void smp4m_cross_call_irq(void)
  322. {
  323. int i = smp_processor_id();
  324. ccall_info.processors_in[i] = 1;
  325. ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
  326. ccall_info.arg4, ccall_info.arg5);
  327. ccall_info.processors_out[i] = 1;
  328. }
  329. void smp4m_percpu_timer_interrupt(struct pt_regs *regs)
  330. {
  331. int cpu = smp_processor_id();
  332. clear_profile_irq(cpu);
  333. profile_tick(CPU_PROFILING, regs);
  334. if(!--prof_counter(cpu)) {
  335. int user = user_mode(regs);
  336. irq_enter();
  337. update_process_times(user);
  338. irq_exit();
  339. prof_counter(cpu) = prof_multiplier(cpu);
  340. }
  341. }
  342. extern unsigned int lvl14_resolution;
  343. static void __init smp_setup_percpu_timer(void)
  344. {
  345. int cpu = smp_processor_id();
  346. prof_counter(cpu) = prof_multiplier(cpu) = 1;
  347. load_profile_irq(cpu, lvl14_resolution);
  348. if(cpu == boot_cpu_id)
  349. enable_pil_irq(14);
  350. }
  351. void __init smp4m_blackbox_id(unsigned *addr)
  352. {
  353. int rd = *addr & 0x3e000000;
  354. int rs1 = rd >> 11;
  355. addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
  356. addr[1] = 0x8130200c | rd | rs1; /* srl reg, 0xc, reg */
  357. addr[2] = 0x80082003 | rd | rs1; /* and reg, 3, reg */
  358. }
  359. void __init smp4m_blackbox_current(unsigned *addr)
  360. {
  361. int rd = *addr & 0x3e000000;
  362. int rs1 = rd >> 11;
  363. addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
  364. addr[2] = 0x8130200a | rd | rs1; /* srl reg, 0xa, reg */
  365. addr[4] = 0x8008200c | rd | rs1; /* and reg, 3, reg */
  366. }
  367. void __init sun4m_init_smp(void)
  368. {
  369. BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4m_blackbox_id);
  370. BTFIXUPSET_BLACKBOX(load_current, smp4m_blackbox_current);
  371. BTFIXUPSET_CALL(smp_cross_call, smp4m_cross_call, BTFIXUPCALL_NORM);
  372. BTFIXUPSET_CALL(smp_message_pass, smp4m_message_pass, BTFIXUPCALL_NORM);
  373. BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4m_processor_id, BTFIXUPCALL_NORM);
  374. }