2 * BK Id: SCCS/s.smp.c 1.50 04/16/02 12:02:07 hozer
7 * Written by Cort Dougan (cort@cs.nmt.edu) borrowing a great
8 * deal of code from the sparc and intel versions.
10 * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
14 #include <linux/config.h>
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/smp.h>
18 #include <linux/smp_lock.h>
19 #include <linux/interrupt.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/delay.h>
22 #define __KERNEL_SYSCALLS__
23 #include <linux/unistd.h>
24 #include <linux/init.h>
25 #include <linux/spinlock.h>
26 #include <linux/cache.h>
28 #include <asm/ptrace.h>
29 #include <asm/atomic.h>
32 #include <asm/pgtable.h>
33 #include <asm/hardirq.h>
34 #include <asm/softirq.h>
38 #include <asm/residual.h>
41 int smp_threads_ready;
42 volatile int smp_commenced;
44 int smp_tb_synchronized;
45 struct cpuinfo_PPC cpu_data[NR_CPUS];
46 struct klock_info_struct klock_info = { KLOCK_CLEAR, 0 };
49 spinlock_t kernel_flag __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
50 unsigned int prof_multiplier[NR_CPUS];
51 unsigned int prof_counter[NR_CPUS];
52 cycles_t cacheflush_time;
53 static int max_cpus __initdata = NR_CPUS;
54 unsigned long cpu_online_map;
55 int smp_hw_index[NR_CPUS];
56 static struct smp_ops_t *smp_ops;
58 /* all cpu mappings are 1-1 -- Cort */
59 volatile unsigned long cpu_callin_map[NR_CPUS];
61 #define TB_SYNC_PASSES 4
62 volatile unsigned long __initdata tb_sync_flag = 0;
63 volatile unsigned long __initdata tb_offset = 0;
65 int start_secondary(void *);
66 extern int cpu_idle(void *unused);
67 void smp_call_function_interrupt(void);
69 #ifdef CONFIG_PPC_ISERIES
70 extern void smp_iSeries_space_timers( unsigned nr );
73 /* Since OpenPIC has only 4 IPIs, we use slightly different message numbers.
75 * Make sure this matches openpic_request_IPIs in open_pic.c, or what shows up
76 * in /proc/interrupts will be wrong!!! --Troy */
77 #define PPC_MSG_CALL_FUNCTION 0
78 #define PPC_MSG_RESCHEDULE 1
79 #define PPC_MSG_INVALIDATE_TLB 2
80 #define PPC_MSG_XMON_BREAK 3
83 smp_message_pass(int target, int msg, unsigned long data, int wait)
86 atomic_inc(&ipi_sent);
87 smp_ops->message_pass(target,msg,data,wait);
94 void smp_local_timer_interrupt(struct pt_regs * regs)
96 int cpu = smp_processor_id();
98 if (!--prof_counter[cpu]) {
99 update_process_times(user_mode(regs));
100 prof_counter[cpu]=prof_multiplier[cpu];
104 void smp_message_recv(int msg, struct pt_regs *regs)
106 atomic_inc(&ipi_recv);
109 case PPC_MSG_CALL_FUNCTION:
110 smp_call_function_interrupt();
112 case PPC_MSG_RESCHEDULE:
113 current->need_resched = 1;
115 case PPC_MSG_INVALIDATE_TLB:
119 case PPC_MSG_XMON_BREAK:
122 #endif /* CONFIG_XMON */
124 printk("SMP %d: smp_message_recv(): unknown msg %d\n",
125 smp_processor_id(), msg);
130 #ifdef CONFIG_750_SMP
132 * 750's don't broadcast tlb invalidates so
133 * we have to emulate that behavior.
136 void smp_ppc750_send_tlb_invalidate(int cpu)
138 if ( PVR_VER(mfspr(PVR)) == 8 )
139 smp_message_pass(MSG_ALL_BUT_SELF, PPC_MSG_INVALIDATE_TLB, 0, 0);
143 void smp_send_reschedule(int cpu)
146 * This is only used if `cpu' is running an idle task,
147 * so it will reschedule itself anyway...
149 * This isn't the case anymore since the other CPU could be
150 * sleeping and won't reschedule until the next interrupt (such
154 /* This is only used if `cpu' is running an idle task,
155 so it will reschedule itself anyway... */
156 smp_message_pass(cpu, PPC_MSG_RESCHEDULE, 0, 0);
160 void smp_send_xmon_break(int cpu)
162 smp_message_pass(cpu, PPC_MSG_XMON_BREAK, 0, 0);
164 #endif /* CONFIG_XMON */
166 static void stop_this_cpu(void *dummy)
173 void smp_send_stop(void)
175 smp_call_function(stop_this_cpu, NULL, 1, 0);
180 * Structure and data for smp_call_function(). This is designed to minimise
181 * static memory requirements. It also looks cleaner.
182 * Stolen from the i386 version.
184 static spinlock_t call_lock = SPIN_LOCK_UNLOCKED;
186 static struct call_data_struct {
187 void (*func) (void *info);
195 * this function sends a 'generic call function' IPI to all other CPUs
199 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
202 * [SUMMARY] Run a function on all other CPUs.
203 * <func> The function to run. This must be fast and non-blocking.
204 * <info> An arbitrary pointer to pass to the function.
205 * <nonatomic> currently unused.
206 * <wait> If true, wait (atomically) until function has completed on other CPUs.
207 * [RETURNS] 0 on success, else a negative status code. Does not return until
208 * remote CPUs are nearly ready to execute <<func>> or are or have executed.
210 * You must not call this function with disabled interrupts or from a
211 * hardware interrupt handler, you may call it from a bottom half handler.
214 struct call_data_struct data;
215 int ret = -1, cpus = smp_num_cpus-1;
223 atomic_set(&data.started, 0);
226 atomic_set(&data.finished, 0);
228 spin_lock_bh(&call_lock);
230 /* Send a message to all other CPUs and wait for them to respond */
231 smp_message_pass(MSG_ALL_BUT_SELF, PPC_MSG_CALL_FUNCTION, 0, 0);
233 /* Wait for response */
235 while (atomic_read(&data.started) != cpus) {
236 if (--timeout == 0) {
237 printk("smp_call_function on cpu %d: other cpus not responding (%d)\n",
238 smp_processor_id(), atomic_read(&data.started));
247 while (atomic_read(&data.finished) != cpus) {
248 if (--timeout == 0) {
249 printk("smp_call_function on cpu %d: other cpus not finishing (%d/%d)\n",
250 smp_processor_id(), atomic_read(&data.finished), atomic_read(&data.started));
260 spin_unlock_bh(&call_lock);
264 void smp_call_function_interrupt(void)
266 void (*func) (void *info) = call_data->func;
267 void *info = call_data->info;
268 int wait = call_data->wait;
271 * Notify initiating CPU that I've grabbed the data and am
272 * about to execute the function
274 atomic_inc(&call_data->started);
276 * At this point the info structure may be out of scope unless wait==1
280 atomic_inc(&call_data->finished);
283 void __init smp_boot_cpus(void)
285 extern struct task_struct *current_set[NR_CPUS];
287 struct task_struct *p;
289 printk("Entering SMP Mode...\n");
291 smp_store_cpu_info(0);
292 cpu_online_map = 1UL;
295 * assume for now that the first cpu booted is
296 * cpu 0, the master -- Cort
298 cpu_callin_map[0] = 1;
299 current->processor = 0;
303 for (i = 0; i < NR_CPUS; i++) {
305 prof_multiplier[i] = 1;
309 * XXX very rough, assumes 20 bus cycles to read a cache line,
310 * timebase increments every 4 bus cycles, 32kB L1 data cache.
312 cacheflush_time = 5 * 1024;
314 smp_ops = ppc_md.smp_ops;
315 if (smp_ops == NULL) {
316 printk("SMP not supported on this machine.\n");
320 #ifndef CONFIG_750_SMP
321 /* check for 750's, they just don't work with linux SMP.
322 * If you actually have 750 SMP hardware and want to try to get
323 * it to work, send me a patch to make it work and
324 * I'll make CONFIG_750_SMP a config option. -- Troy (hozer@drgw.net)
326 if ( PVR_VER(mfspr(PVR)) == 8 ){
327 printk("SMP not supported on 750 cpus. %s line %d\n",
334 /* Probe arch for CPUs */
335 cpu_nr = smp_ops->probe();
338 * only check for cpus we know exist. We keep the callin map
339 * with cpus at the bottom -- Cort
341 if (cpu_nr > max_cpus)
343 #ifdef CONFIG_PPC_ISERIES
344 smp_iSeries_space_timers( cpu_nr );
346 for (i = 1; i < cpu_nr; i++) {
350 /* create a process for the processor */
351 /* only regs.msr is actually used, and 0 is OK for it */
352 memset(®s, 0, sizeof(struct pt_regs));
353 if (do_fork(CLONE_VM|CLONE_PID, 0, ®s, 0) < 0)
354 panic("failed fork for CPU %d", i);
355 p = init_task.prev_task;
357 panic("No idle task for CPU %d", i);
358 del_from_runqueue(p);
363 p->cpus_runnable = 1 << i; /* we schedule the first task manually */
367 * There was a cache flush loop here to flush the cache
368 * to memory for the first 8MB of RAM. The cache flush
369 * has been pushed into the kick_cpu function for those
370 * platforms that need it.
374 smp_ops->kick_cpu(i);
377 * wait to see if the cpu made a callin (is actually up).
378 * use this value that I found through experimentation.
381 for ( c = 1000; c && !cpu_callin_map[i] ; c-- )
384 if ( cpu_callin_map[i] )
387 sprintf(buf, "found cpu %d", i);
388 if (ppc_md.progress) ppc_md.progress(buf, 0x350+i);
389 printk("Processor %d found.\n", i);
393 sprintf(buf, "didn't find cpu %d", i);
394 if (ppc_md.progress) ppc_md.progress(buf, 0x360+i);
395 printk("Processor %d is stuck.\n", i);
399 /* Setup CPU 0 last (important) */
400 smp_ops->setup_cpu(0);
402 if (smp_num_cpus < 2)
403 smp_tb_synchronized = 1;
406 void __init smp_software_tb_sync(int cpu)
408 #define PASSES 4 /* 4 passes.. */
412 /* stop - start will be the number of timebase ticks it takes for cpu0
413 * to send a message to all others and the first reponse to show up.
415 * ASSUMPTION: this time is similiar for all cpus
416 * ASSUMPTION: the time to send a one-way message is ping/2
418 register unsigned long start = 0;
419 register unsigned long stop = 0;
420 register unsigned long temp = 0;
424 /* multiple passes to get in l1 cache.. */
425 for (pass = 2; pass < 2+PASSES; pass++){
428 for (i = j = 1; i < smp_num_cpus; i++, j++){
429 /* skip stuck cpus */
430 while (!cpu_callin_map[j])
432 while (cpu_callin_map[j] != pass)
437 start = get_tbl(); /* start timing */
440 stop = get_tbl(); /* end timing */
441 /* theoretically, the divisor should be 2, but
442 * I get better results on my dual mtx. someone
443 * please report results on other smp machines..
445 tb_offset = (stop-start)/4;
455 cpu_callin_map[cpu] = pass;
457 while (!tb_sync_flag)
458 mb(); /* wait for cpu0 */
460 tb_sync_flag = 0; /* send response for timing */
462 while (!tb_sync_flag)
464 temp = tb_offset; /* make sure offset is loaded */
467 set_tb(0,temp); /* now, set the timebase */
472 smp_tb_synchronized = 1;
473 printk("smp_software_tb_sync: %d passes, final offset: %ld\n",
476 /* so time.c doesn't get confused */
477 set_dec(tb_ticks_per_jiffy);
478 last_jiffy_stamp(cpu) = 0;
481 void __init smp_commence(void)
484 * Lets the callin's below out of their loop.
486 if (ppc_md.progress) ppc_md.progress("smp_commence", 0x370);
490 /* if the smp_ops->setup_cpu function has not already synched the
491 * timebases with a nicer hardware-based method, do so now
493 * I am open to suggestions for improvements to this method
494 * -- Troy <hozer@drgw.net>
496 * NOTE: if you are debugging, set smp_tb_synchronized for now
497 * since if this code runs pretty early and needs all cpus that
498 * reported in in smp_callin_map to be working
500 * NOTE2: this code doesn't seem to work on > 2 cpus. -- paulus/BenH
502 if (!smp_tb_synchronized && smp_num_cpus == 2) {
504 __save_and_cli(flags);
505 smp_software_tb_sync(0);
506 __restore_flags(flags);
510 void __init smp_callin(void)
512 int cpu = current->processor;
514 smp_store_cpu_info(cpu);
515 set_dec(tb_ticks_per_jiffy);
516 cpu_callin_map[cpu] = 1;
518 smp_ops->setup_cpu(cpu);
521 * This cpu is now "online". Only set them online
522 * before they enter the loop below since write access
523 * to the below variable is _not_ guaranteed to be
525 * -- Cort <cort@fsmlabs.com>
527 cpu_online_map |= 1UL << smp_processor_id();
529 while(!smp_commenced)
532 /* see smp_commence for more info */
533 if (!smp_tb_synchronized && smp_num_cpus == 2) {
534 smp_software_tb_sync(cpu);
539 /* intel needs this */
540 void __init initialize_secondary(void)
544 /* Activate a secondary processor. */
545 int __init start_secondary(void *unused)
547 atomic_inc(&init_mm.mm_count);
548 current->active_mm = &init_mm;
550 return cpu_idle(NULL);
553 void __init smp_setup(char *str, int *ints)
557 int __init setup_profiling_timer(unsigned int multiplier)
562 void __init smp_store_cpu_info(int id)
564 struct cpuinfo_PPC *c = &cpu_data[id];
566 /* assume bogomips are same for everything */
567 c->loops_per_jiffy = loops_per_jiffy;
571 static int __init maxcpus(char *str)
573 get_option(&str, &max_cpus);
577 __setup("maxcpus=", maxcpus);