1 /* sun4m_smp.c: Sparc SUN4M SMP support.
3 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/threads.h>
11 #include <linux/smp.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/init.h>
15 #include <linux/spinlock.h>
17 #include <linux/swap.h>
18 #include <linux/profile.h>
19 #include <asm/cacheflush.h>
20 #include <asm/tlbflush.h>
21 #include <asm/irq_regs.h>
23 #include <asm/ptrace.h>
24 #include <asm/atomic.h>
26 #include <asm/delay.h>
29 #include <asm/pgalloc.h>
30 #include <asm/pgtable.h>
31 #include <asm/oplib.h>
32 #include <asm/cpudata.h>
36 #define IRQ_RESCHEDULE 13
37 #define IRQ_STOP_CPU 14
38 #define IRQ_CROSS_CALL 15
40 extern ctxd_t *srmmu_ctx_table_phys;
42 extern void calibrate_delay(void);
44 extern volatile unsigned long cpu_callin_map[NR_CPUS];
45 extern unsigned char boot_cpu_id;
47 extern cpumask_t smp_commenced_mask;
49 extern int __smp4m_processor_id(void);
54 #define SMP_PRINTK(x) printk x
59 static inline unsigned long swap(volatile unsigned long *ptr, unsigned long val)
61 __asm__ __volatile__("swap [%1], %0\n\t" :
62 "=&r" (val), "=&r" (ptr) :
63 "0" (val), "1" (ptr));
67 static void smp_setup_percpu_timer(void);
68 extern void cpu_probe(void);
70 void __cpuinit smp4m_callin(void)
72 int cpuid = hard_smp_processor_id();
74 local_flush_cache_all();
75 local_flush_tlb_all();
77 /* Get our local ticker going. */
78 smp_setup_percpu_timer();
81 smp_store_cpu_info(cpuid);
83 local_flush_cache_all();
84 local_flush_tlb_all();
87 * Unblock the master CPU _only_ when the scheduler state
88 * of all secondary CPUs will be up-to-date, so after
89 * the SMP initialization the master will be just allowed
90 * to call the scheduler code.
92 /* Allow master to continue. */
93 swap(&cpu_callin_map[cpuid], 1);
95 /* XXX: What's up with all the flushes? */
96 local_flush_cache_all();
97 local_flush_tlb_all();
101 /* Fix idle thread fields. */
102 __asm__ __volatile__("ld [%0], %%g6\n\t"
103 : : "r" (¤t_set[cpuid])
104 : "memory" /* paranoid */);
106 /* Attach to the address space of init_task. */
107 atomic_inc(&init_mm.mm_count);
108 current->active_mm = &init_mm;
110 while (!cpu_isset(cpuid, smp_commenced_mask))
115 cpu_set(cpuid, cpu_online_map);
119 * Cycle through the processors asking the PROM to start each one.
122 extern struct linux_prom_registers smp_penguin_ctable;
123 extern unsigned long trapbase_cpu1[];
124 extern unsigned long trapbase_cpu2[];
125 extern unsigned long trapbase_cpu3[];
127 void __init smp4m_boot_cpus(void)
129 smp_setup_percpu_timer();
130 local_flush_cache_all();
133 int __cpuinit smp4m_boot_one_cpu(int i)
135 extern unsigned long sun4m_cpu_startup;
136 unsigned long *entry = &sun4m_cpu_startup;
137 struct task_struct *p;
141 cpu_find_by_mid(i, &cpu_node);
143 /* Cook up an idler for this guy. */
145 current_set[i] = task_thread_info(p);
146 /* See trampoline.S for details... */
147 entry += ((i-1) * 3);
150 * Initialize the contexts table
151 * Since the call to prom_startcpu() trashes the structure,
152 * we need to re-initialize it for each cpu
154 smp_penguin_ctable.which_io = 0;
155 smp_penguin_ctable.phys_addr = (unsigned int) srmmu_ctx_table_phys;
156 smp_penguin_ctable.reg_size = 0;
158 /* whirrr, whirrr, whirrrrrrrrr... */
159 printk("Starting CPU %d at %p\n", i, entry);
160 local_flush_cache_all();
161 prom_startcpu(cpu_node,
162 &smp_penguin_ctable, 0, (char *)entry);
164 /* wheee... it's going... */
165 for(timeout = 0; timeout < 10000; timeout++) {
166 if(cpu_callin_map[i])
171 if (!(cpu_callin_map[i])) {
172 printk("Processor %d is stuck.\n", i);
176 local_flush_cache_all();
180 void __init smp4m_smp_done(void)
185 /* setup cpu list for irq rotation */
188 for (i = 0; i < NR_CPUS; i++) {
191 prev = &cpu_data(i).next;
195 local_flush_cache_all();
197 /* Free unneeded trap tables */
198 if (!cpu_isset(1, cpu_present_map)) {
199 ClearPageReserved(virt_to_page(trapbase_cpu1));
200 init_page_count(virt_to_page(trapbase_cpu1));
201 free_page((unsigned long)trapbase_cpu1);
205 if (!cpu_isset(2, cpu_present_map)) {
206 ClearPageReserved(virt_to_page(trapbase_cpu2));
207 init_page_count(virt_to_page(trapbase_cpu2));
208 free_page((unsigned long)trapbase_cpu2);
212 if (!cpu_isset(3, cpu_present_map)) {
213 ClearPageReserved(virt_to_page(trapbase_cpu3));
214 init_page_count(virt_to_page(trapbase_cpu3));
215 free_page((unsigned long)trapbase_cpu3);
220 /* Ok, they are spinning and ready to go. */
223 /* At each hardware IRQ, we get this called to forward IRQ reception
224 * to the next processor. The caller must disable the IRQ level being
225 * serviced globally so that there are no double interrupts received.
227 * XXX See sparc64 irq.c.
229 void smp4m_irq_rotate(int cpu)
231 int next = cpu_data(cpu).next;
236 /* Cross calls, in order to work efficiently and atomically do all
237 * the message passing work themselves, only stopcpu and reschedule
238 * messages come through here.
240 void smp4m_message_pass(int target, int msg, unsigned long data, int wait)
242 static unsigned long smp_cpu_in_msg[NR_CPUS];
244 int me = smp_processor_id();
247 if(msg == MSG_RESCHEDULE) {
248 irq = IRQ_RESCHEDULE;
250 if(smp_cpu_in_msg[me])
252 } else if(msg == MSG_STOP_CPU) {
258 smp_cpu_in_msg[me]++;
259 if(target == MSG_ALL_BUT_SELF || target == MSG_ALL) {
260 mask = cpu_online_map;
261 if(target == MSG_ALL_BUT_SELF)
263 for(i = 0; i < 4; i++) {
264 if (cpu_isset(i, mask))
268 set_cpu_int(target, irq);
270 smp_cpu_in_msg[me]--;
274 printk("Yeeee, trying to send SMP msg(%d) on cpu %d\n", msg, me);
275 panic("Bogon SMP message pass.");
278 static struct smp_funcall {
285 unsigned long processors_in[SUN4M_NCPUS]; /* Set when ipi entered. */
286 unsigned long processors_out[SUN4M_NCPUS]; /* Set when ipi exited. */
289 static DEFINE_SPINLOCK(cross_call_lock);
291 /* Cross calls must be serialized, at least currently. */
292 void smp4m_cross_call(smpfunc_t func, unsigned long arg1, unsigned long arg2,
293 unsigned long arg3, unsigned long arg4, unsigned long arg5)
295 register int ncpus = SUN4M_NCPUS;
298 spin_lock_irqsave(&cross_call_lock, flags);
300 /* Init function glue. */
301 ccall_info.func = func;
302 ccall_info.arg1 = arg1;
303 ccall_info.arg2 = arg2;
304 ccall_info.arg3 = arg3;
305 ccall_info.arg4 = arg4;
306 ccall_info.arg5 = arg5;
308 /* Init receive/complete mapping, plus fire the IPI's off. */
310 cpumask_t mask = cpu_online_map;
313 cpu_clear(smp_processor_id(), mask);
314 for(i = 0; i < ncpus; i++) {
315 if (cpu_isset(i, mask)) {
316 ccall_info.processors_in[i] = 0;
317 ccall_info.processors_out[i] = 0;
318 set_cpu_int(i, IRQ_CROSS_CALL);
320 ccall_info.processors_in[i] = 1;
321 ccall_info.processors_out[i] = 1;
331 while(!ccall_info.processors_in[i])
333 } while(++i < ncpus);
337 while(!ccall_info.processors_out[i])
339 } while(++i < ncpus);
342 spin_unlock_irqrestore(&cross_call_lock, flags);
345 /* Running cross calls. */
346 void smp4m_cross_call_irq(void)
348 int i = smp_processor_id();
350 ccall_info.processors_in[i] = 1;
351 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
352 ccall_info.arg4, ccall_info.arg5);
353 ccall_info.processors_out[i] = 1;
356 void smp4m_percpu_timer_interrupt(struct pt_regs *regs)
358 struct pt_regs *old_regs;
359 int cpu = smp_processor_id();
361 old_regs = set_irq_regs(regs);
363 clear_profile_irq(cpu);
365 profile_tick(CPU_PROFILING);
367 if(!--prof_counter(cpu)) {
368 int user = user_mode(regs);
371 update_process_times(user);
374 prof_counter(cpu) = prof_multiplier(cpu);
376 set_irq_regs(old_regs);
379 extern unsigned int lvl14_resolution;
381 static void __init smp_setup_percpu_timer(void)
383 int cpu = smp_processor_id();
385 prof_counter(cpu) = prof_multiplier(cpu) = 1;
386 load_profile_irq(cpu, lvl14_resolution);
388 if(cpu == boot_cpu_id)
392 void __init smp4m_blackbox_id(unsigned *addr)
394 int rd = *addr & 0x3e000000;
397 addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
398 addr[1] = 0x8130200c | rd | rs1; /* srl reg, 0xc, reg */
399 addr[2] = 0x80082003 | rd | rs1; /* and reg, 3, reg */
402 void __init smp4m_blackbox_current(unsigned *addr)
404 int rd = *addr & 0x3e000000;
407 addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
408 addr[2] = 0x8130200a | rd | rs1; /* srl reg, 0xa, reg */
409 addr[4] = 0x8008200c | rd | rs1; /* and reg, 0xc, reg */
412 void __init sun4m_init_smp(void)
414 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4m_blackbox_id);
415 BTFIXUPSET_BLACKBOX(load_current, smp4m_blackbox_current);
416 BTFIXUPSET_CALL(smp_cross_call, smp4m_cross_call, BTFIXUPCALL_NORM);
417 BTFIXUPSET_CALL(smp_message_pass, smp4m_message_pass, BTFIXUPCALL_NORM);
418 BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4m_processor_id, BTFIXUPCALL_NORM);