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/smp_lock.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel_stat.h>
15 #include <linux/init.h>
16 #include <linux/spinlock.h>
18 #include <asm/ptrace.h>
19 #include <asm/atomic.h>
21 #include <asm/delay.h>
24 #include <asm/pgtable.h>
25 #include <asm/oplib.h>
26 #include <asm/atops.h>
27 #include <asm/hardirq.h>
28 #include <asm/softirq.h>
30 #define __KERNEL_SYSCALLS__
31 #include <linux/unistd.h>
33 #define IRQ_RESCHEDULE 13
34 #define IRQ_STOP_CPU 14
35 #define IRQ_CROSS_CALL 15
37 extern ctxd_t
*srmmu_ctx_table_phys
;
38 extern int linux_num_cpus
;
40 extern void calibrate_delay(void);
42 extern struct task_struct
*current_set
[NR_CPUS
];
43 extern volatile int smp_processors_ready
;
44 extern unsigned long cpu_present_map
;
45 extern int smp_num_cpus
;
46 extern int smp_threads_ready
;
47 extern unsigned char mid_xlate
[NR_CPUS
];
48 extern volatile unsigned long cpu_callin_map
[NR_CPUS
];
49 extern unsigned long smp_proc_in_lock
[NR_CPUS
];
50 extern struct cpuinfo_sparc cpu_data
[NR_CPUS
];
51 extern unsigned long cpu_offset
[NR_CPUS
];
52 extern unsigned char boot_cpu_id
;
53 extern int smp_activated
;
54 extern volatile int cpu_number_map
[NR_CPUS
];
55 extern volatile int __cpu_logical_map
[NR_CPUS
];
56 extern volatile unsigned long ipi_count
;
57 extern volatile int smp_process_available
;
58 extern volatile int smp_commenced
;
59 extern int __smp4m_processor_id(void);
64 #define SMP_PRINTK(x) printk x
69 static inline unsigned long swap(volatile unsigned long *ptr
, unsigned long val
)
71 __asm__
__volatile__("swap [%1], %0\n\t" :
72 "=&r" (val
), "=&r" (ptr
) :
73 "0" (val
), "1" (ptr
));
77 static void smp_setup_percpu_timer(void);
78 extern void cpu_probe(void);
80 void __init
smp4m_callin(void)
82 int cpuid
= hard_smp_processor_id();
84 local_flush_cache_all();
85 local_flush_tlb_all();
86 set_irq_udt(mid_xlate
[boot_cpu_id
]);
88 /* Get our local ticker going. */
89 smp_setup_percpu_timer();
92 smp_store_cpu_info(cpuid
);
93 local_flush_cache_all();
94 local_flush_tlb_all();
96 /* Allow master to continue. */
97 swap((unsigned long *)&cpu_callin_map
[cpuid
], 1);
98 local_flush_cache_all();
99 local_flush_tlb_all();
103 while(!task
[cpuid
] || current_set
[cpuid
] != task
[cpuid
])
106 /* Fix idle thread fields. */
107 __asm__
__volatile__("ld [%0], %%g6\n\t"
108 : : "r" (¤t_set
[cpuid
])
109 : "memory" /* paranoid */);
110 current
->mm
->mmap
->vm_page_prot
= PAGE_SHARED
;
111 current
->mm
->mmap
->vm_start
= PAGE_OFFSET
;
112 current
->mm
->mmap
->vm_end
= init_mm
.mmap
->vm_end
;
114 while(!smp_commenced
)
117 local_flush_cache_all();
118 local_flush_tlb_all();
123 extern int cpu_idle(void *unused
);
124 extern void init_IRQ(void);
125 extern void cpu_panic(void);
126 extern int start_secondary(void *unused
);
129 * Cycle through the processors asking the PROM to start each one.
132 extern struct prom_cpuinfo linux_cpus
[NR_CPUS
];
133 extern struct linux_prom_registers smp_penguin_ctable
;
134 extern unsigned long trapbase_cpu1
[];
135 extern unsigned long trapbase_cpu2
[];
136 extern unsigned long trapbase_cpu3
[];
138 void __init
smp4m_boot_cpus(void)
144 printk("Entering SMP Mode...\n");
146 for (i
= 0; i
< NR_CPUS
; i
++)
147 cpu_offset
[i
] = (char *)&cpu_data
[i
] - (char *)&cpu_data
;
151 for(i
=0; i
< linux_num_cpus
; i
++)
152 cpu_present_map
|= (1<<i
);
153 for(i
=0; i
< NR_CPUS
; i
++)
154 cpu_number_map
[i
] = -1;
155 for(i
=0; i
< NR_CPUS
; i
++)
156 __cpu_logical_map
[i
] = -1;
157 mid_xlate
[boot_cpu_id
] = (linux_cpus
[boot_cpu_id
].mid
& ~8);
158 cpu_number_map
[boot_cpu_id
] = 0;
159 __cpu_logical_map
[0] = boot_cpu_id
;
160 current
->processor
= boot_cpu_id
;
161 smp_store_cpu_info(boot_cpu_id
);
162 set_irq_udt(mid_xlate
[boot_cpu_id
]);
163 smp_setup_percpu_timer();
165 local_flush_cache_all();
166 if(linux_num_cpus
== 1)
167 return; /* Not an MP box. */
168 for(i
= 0; i
< NR_CPUS
; i
++) {
172 if(cpu_present_map
& (1 << i
)) {
173 extern unsigned long sun4m_cpu_startup
;
174 unsigned long *entry
= &sun4m_cpu_startup
;
175 struct task_struct
*p
;
178 /* Cook up an idler for this guy. */
179 kernel_thread(start_secondary
, NULL
, CLONE_PID
);
181 p
= task
[++cpucount
];
184 p
->has_cpu
= 1; /* we schedule the first task manually */
187 /* See trampoline.S for details... */
188 entry
+= ((i
-1) * 3);
191 * Initialize the contexts table
192 * Since the call to prom_startcpu() trashes the structure,
193 * we need to re-initialize it for each cpu
195 smp_penguin_ctable
.which_io
= 0;
196 smp_penguin_ctable
.phys_addr
= (unsigned int) srmmu_ctx_table_phys
;
197 smp_penguin_ctable
.reg_size
= 0;
199 /* whirrr, whirrr, whirrrrrrrrr... */
200 printk("Starting CPU %d at %p\n", i
, entry
);
201 mid_xlate
[i
] = (linux_cpus
[i
].mid
& ~8);
202 local_flush_cache_all();
203 prom_startcpu(linux_cpus
[i
].prom_node
,
204 &smp_penguin_ctable
, 0, (char *)entry
);
206 /* wheee... it's going... */
207 for(timeout
= 0; timeout
< 10000; timeout
++) {
208 if(cpu_callin_map
[i
])
212 if(cpu_callin_map
[i
]) {
213 /* Another "Red Snapper". */
214 cpu_number_map
[i
] = i
;
215 __cpu_logical_map
[i
] = i
;
218 printk("Processor %d is stuck.\n", i
);
221 if(!(cpu_callin_map
[i
])) {
222 cpu_present_map
&= ~(1 << i
);
223 cpu_number_map
[i
] = -1;
226 local_flush_cache_all();
228 printk("Error: only one Processor found.\n");
229 cpu_present_map
= (1 << smp_processor_id());
231 unsigned long bogosum
= 0;
232 for(i
= 0; i
< NR_CPUS
; i
++) {
233 if(cpu_present_map
& (1 << i
))
234 bogosum
+= cpu_data
[i
].udelay_val
;
236 printk("Total of %d Processors activated (%lu.%02lu BogoMIPS).\n",
238 (bogosum
+ 2500)/500000,
239 ((bogosum
+ 2500)/5000)%100);
241 smp_num_cpus
= cpucount
+ 1;
244 /* Setup CPU list for IRQ distribution scheme. */
246 for(i
= 0; i
< NR_CPUS
; i
++) {
247 if(cpu_present_map
& (1 << i
)) {
251 cpu_data
[prev
].next
= i
;
252 cpu_data
[i
].mid
= mid_xlate
[i
];
256 cpu_data
[prev
].next
= first
;
258 /* Free unneeded trap tables */
260 if (!(cpu_present_map
& (1 << 1))) {
261 mem_map
[MAP_NR((unsigned long)trapbase_cpu1
)].flags
&= ~(1 << PG_reserved
);
262 free_page((unsigned long)trapbase_cpu1
);
264 if (!(cpu_present_map
& (1 << 2))) {
265 mem_map
[MAP_NR((unsigned long)trapbase_cpu2
)].flags
&= ~(1 << PG_reserved
);
266 free_page((unsigned long)trapbase_cpu2
);
268 if (!(cpu_present_map
& (1 << 3))) {
269 mem_map
[MAP_NR((unsigned long)trapbase_cpu3
)].flags
&= ~(1 << PG_reserved
);
270 free_page((unsigned long)trapbase_cpu3
);
273 /* Ok, they are spinning and ready to go. */
274 smp_processors_ready
= 1;
277 /* At each hardware IRQ, we get this called to forward IRQ reception
278 * to the next processor. The caller must disable the IRQ level being
279 * serviced globally so that there are no double interrupts received.
281 void smp4m_irq_rotate(int cpu
)
283 if(smp_processors_ready
)
284 set_irq_udt(cpu_data
[cpu_data
[cpu
].next
].mid
);
287 /* Cross calls, in order to work efficiently and atomically do all
288 * the message passing work themselves, only stopcpu and reschedule
289 * messages come through here.
291 void smp4m_message_pass(int target
, int msg
, unsigned long data
, int wait
)
293 static unsigned long smp_cpu_in_msg
[NR_CPUS
];
295 int me
= smp_processor_id();
298 if(msg
== MSG_RESCHEDULE
) {
299 irq
= IRQ_RESCHEDULE
;
301 if(smp_cpu_in_msg
[me
])
303 } else if(msg
== MSG_STOP_CPU
) {
309 smp_cpu_in_msg
[me
]++;
310 if(target
== MSG_ALL_BUT_SELF
|| target
== MSG_ALL
) {
311 mask
= cpu_present_map
;
312 if(target
== MSG_ALL_BUT_SELF
)
314 for(i
= 0; i
< 4; i
++) {
316 set_cpu_int(mid_xlate
[i
], irq
);
319 set_cpu_int(mid_xlate
[target
], irq
);
321 smp_cpu_in_msg
[me
]--;
325 printk("Yeeee, trying to send SMP msg(%d) on cpu %d\n", msg
, me
);
326 panic("Bogon SMP message pass.");
329 static struct smp_funcall
{
336 unsigned long processors_in
[NR_CPUS
]; /* Set when ipi entered. */
337 unsigned long processors_out
[NR_CPUS
]; /* Set when ipi exited. */
340 static spinlock_t cross_call_lock
= SPIN_LOCK_UNLOCKED
;
342 /* Cross calls must be serialized, at least currently. */
343 void smp4m_cross_call(smpfunc_t func
, unsigned long arg1
, unsigned long arg2
,
344 unsigned long arg3
, unsigned long arg4
, unsigned long arg5
)
346 if(smp_processors_ready
) {
347 register int ncpus
= smp_num_cpus
;
350 spin_lock_irqsave(&cross_call_lock
, flags
);
352 /* Init function glue. */
353 ccall_info
.func
= func
;
354 ccall_info
.arg1
= arg1
;
355 ccall_info
.arg2
= arg2
;
356 ccall_info
.arg3
= arg3
;
357 ccall_info
.arg4
= arg4
;
358 ccall_info
.arg5
= arg5
;
360 /* Init receive/complete mapping, plus fire the IPI's off. */
362 register unsigned long mask
;
365 mask
= (cpu_present_map
& ~(1 << smp_processor_id()));
366 for(i
= 0; i
< ncpus
; i
++) {
367 if(mask
& (1 << i
)) {
368 ccall_info
.processors_in
[i
] = 0;
369 ccall_info
.processors_out
[i
] = 0;
370 set_cpu_int(mid_xlate
[i
], IRQ_CROSS_CALL
);
372 ccall_info
.processors_in
[i
] = 1;
373 ccall_info
.processors_out
[i
] = 1;
378 /* First, run local copy. */
379 func(arg1
, arg2
, arg3
, arg4
, arg5
);
386 while(!ccall_info
.processors_in
[i
])
388 } while(++i
< ncpus
);
392 while(!ccall_info
.processors_out
[i
])
394 } while(++i
< ncpus
);
397 spin_unlock_irqrestore(&cross_call_lock
, flags
);
399 func(arg1
, arg2
, arg3
, arg4
, arg5
); /* Just need to run local copy. */
402 /* Running cross calls. */
403 void smp4m_cross_call_irq(void)
405 int i
= smp_processor_id();
407 ccall_info
.processors_in
[i
] = 1;
408 ccall_info
.func(ccall_info
.arg1
, ccall_info
.arg2
, ccall_info
.arg3
,
409 ccall_info
.arg4
, ccall_info
.arg5
);
410 ccall_info
.processors_out
[i
] = 1;
413 /* Protects counters touched during level14 ticker */
414 static spinlock_t ticker_lock
= SPIN_LOCK_UNLOCKED
;
416 /* 32-bit Sparc specific profiling function. */
417 static inline void sparc_do_profile(unsigned long pc
)
419 if(prof_buffer
&& current
->pid
) {
422 pc
-= (unsigned long) &_stext
;
425 spin_lock(&ticker_lock
);
429 prof_buffer
[prof_len
- 1]++;
430 spin_unlock(&ticker_lock
);
434 extern unsigned int prof_multiplier
[NR_CPUS
];
435 extern unsigned int prof_counter
[NR_CPUS
];
437 extern void update_one_process(struct task_struct
*p
, unsigned long ticks
,
438 unsigned long user
, unsigned long system
,
441 void smp4m_percpu_timer_interrupt(struct pt_regs
*regs
)
443 int cpu
= smp_processor_id();
445 clear_profile_irq(mid_xlate
[cpu
]);
448 sparc_do_profile(regs
->pc
);
450 if(!--prof_counter
[cpu
]) {
451 int user
= user_mode(regs
);
455 update_one_process(current
, 1, user
, !user
, cpu
);
457 if(--current
->counter
<= 0) {
458 current
->counter
= 0;
459 current
->need_resched
= 1;
463 if(current
->priority
< DEF_PRIORITY
) {
465 kstat
.per_cpu_nice
[cpu
]++;
468 kstat
.per_cpu_user
[cpu
]++;
472 kstat
.per_cpu_system
[cpu
]++;
475 prof_counter
[cpu
] = prof_multiplier
[cpu
];
480 extern unsigned int lvl14_resolution
;
482 static void __init
smp_setup_percpu_timer(void)
484 int cpu
= smp_processor_id();
486 prof_counter
[cpu
] = prof_multiplier
[cpu
] = 1;
487 load_profile_irq(mid_xlate
[cpu
], lvl14_resolution
);
489 if(cpu
== boot_cpu_id
)
493 void __init
smp4m_blackbox_id(unsigned *addr
)
495 int rd
= *addr
& 0x3e000000;
498 addr
[0] = 0x81580000 | rd
; /* rd %tbr, reg */
499 addr
[1] = 0x8130200c | rd
| rs1
; /* srl reg, 0xc, reg */
500 addr
[2] = 0x80082003 | rd
| rs1
; /* and reg, 3, reg */
503 void __init
smp4m_blackbox_current(unsigned *addr
)
505 int rd
= *addr
& 0x3e000000;
508 addr
[0] = 0x81580000 | rd
; /* rd %tbr, reg */
509 addr
[2] = 0x8130200a | rd
| rs1
; /* srl reg, 0xa, reg */
510 addr
[4] = 0x8008200c | rd
| rs1
; /* and reg, 3, reg */
513 void __init
sun4m_init_smp(void)
515 BTFIXUPSET_BLACKBOX(smp_processor_id
, smp4m_blackbox_id
);
516 BTFIXUPSET_BLACKBOX(load_current
, smp4m_blackbox_current
);
517 BTFIXUPSET_CALL(smp_cross_call
, smp4m_cross_call
, BTFIXUPCALL_NORM
);
518 BTFIXUPSET_CALL(smp_message_pass
, smp4m_message_pass
, BTFIXUPCALL_NORM
);
519 BTFIXUPSET_CALL(__smp_processor_id
, __smp4m_processor_id
, BTFIXUPCALL_NORM
);