Linux 2.4.0-test10pre4
[davej-history.git] / arch / ppc / kernel / smp.c
blobfb7f384440c815772d8fd4dd0c53f0d26e2ea26d
1 /*
2 * $Id: smp.c,v 1.68 1999/09/17 19:38:05 cort Exp $
4 * Smp support for ppc.
6 * Written by Cort Dougan (cort@cs.nmt.edu) borrowing a great
7 * deal of code from the sparc and intel versions.
9 * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
11 * Support for PReP (Motorola MTX/MVME) SMP by Troy Benjegerdes
12 * (troy@microux.com, hozer@drgw.net)
15 #include <linux/config.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/delay.h>
23 #define __KERNEL_SYSCALLS__
24 #include <linux/unistd.h>
25 #include <linux/init.h>
26 #include <linux/openpic.h>
27 #include <linux/spinlock.h>
29 #include <asm/ptrace.h>
30 #include <asm/atomic.h>
31 #include <asm/irq.h>
32 #include <asm/page.h>
33 #include <asm/pgtable.h>
34 #include <asm/hardirq.h>
35 #include <asm/softirq.h>
36 #include <asm/init.h>
37 #include <asm/io.h>
38 #include <asm/prom.h>
39 #include <asm/smp.h>
40 #include <asm/gemini.h>
42 #include <asm/time.h>
43 #include "open_pic.h"
44 int smp_threads_ready;
45 volatile int smp_commenced;
46 int smp_num_cpus = 1;
47 struct cpuinfo_PPC cpu_data[NR_CPUS];
48 struct klock_info_struct klock_info = { KLOCK_CLEAR, 0 };
49 volatile unsigned char active_kernel_processor = NO_PROC_ID; /* Processor holding kernel spinlock */
50 volatile unsigned long ipi_count;
51 spinlock_t kernel_flag = SPIN_LOCK_UNLOCKED;
52 unsigned int prof_multiplier[NR_CPUS];
53 unsigned int prof_counter[NR_CPUS];
54 cycles_t cacheflush_time;
56 /* this has to go in the data section because it is accessed from prom_init */
57 int smp_hw_index[NR_CPUS];
59 /* all cpu mappings are 1-1 -- Cort */
60 volatile unsigned long cpu_callin_map[NR_CPUS];
62 int start_secondary(void *);
63 extern int cpu_idle(void *unused);
64 u_int openpic_read(volatile u_int *addr);
65 void smp_call_function_interrupt(void);
66 void smp_message_pass(int target, int msg, unsigned long data, int wait);
68 /* register for interrupting the primary processor on the powersurge */
69 /* N.B. this is actually the ethernet ROM! */
70 #define PSURGE_PRI_INTR 0xf3019000
71 /* register for interrupting the secondary processor on the powersurge */
72 #define PSURGE_SEC_INTR 0xf80000c0
73 /* register for storing the start address for the secondary processor */
74 #define PSURGE_START 0xf2800000
75 /* virtual addresses for the above */
76 volatile u32 *psurge_pri_intr;
77 volatile u32 *psurge_sec_intr;
78 volatile u32 *psurge_start;
80 /* Since OpenPIC has only 4 IPIs, we use slightly different message numbers. */
81 #define PPC_MSG_CALL_FUNCTION 0
82 #define PPC_MSG_RESCHEDULE 1
83 #define PPC_MSG_INVALIDATE_TLB 2
84 #define PPC_MSG_XMON_BREAK 3
86 static inline void set_tb(unsigned int upper, unsigned int lower)
88 mtspr(SPRN_TBWU, upper);
89 mtspr(SPRN_TBWL, lower);
92 void smp_local_timer_interrupt(struct pt_regs * regs)
94 int cpu = smp_processor_id();
96 if (!--prof_counter[cpu]) {
97 update_process_times(user_mode(regs));
98 prof_counter[cpu]=prof_multiplier[cpu];
102 void smp_message_recv(int msg, struct pt_regs *regs)
104 ipi_count++;
106 switch( msg ) {
107 case PPC_MSG_CALL_FUNCTION:
108 smp_call_function_interrupt();
109 break;
110 case PPC_MSG_RESCHEDULE:
111 current->need_resched = 1;
112 break;
113 case PPC_MSG_INVALIDATE_TLB:
114 _tlbia();
115 break;
116 #ifdef CONFIG_XMON
117 case PPC_MSG_XMON_BREAK:
118 xmon(regs);
119 break;
120 #endif /* CONFIG_XMON */
121 default:
122 printk("SMP %d: smp_message_recv(): unknown msg %d\n",
123 smp_processor_id(), msg);
124 break;
129 * As it is now, if we're sending two message at the same time
130 * we have race conditions on Pmac. The PowerSurge doesn't easily
131 * allow us to send IPI messages so we put the messages in
132 * smp_message[].
134 * This is because don't have several IPI's on the PowerSurge even though
135 * we do on the chrp. It would be nice to use actual IPI's such as with
136 * openpic rather than this.
137 * -- Cort
139 int pmac_smp_message[NR_CPUS];
140 void pmac_smp_message_recv(struct pt_regs *regs)
142 int cpu = smp_processor_id();
143 int msg;
145 /* clear interrupt */
146 if (cpu == 1)
147 out_be32(psurge_sec_intr, ~0);
149 if (smp_num_cpus < 2)
150 return;
152 /* make sure there is a message there */
153 msg = pmac_smp_message[cpu];
154 if (msg == 0)
155 return;
157 /* reset message */
158 pmac_smp_message[cpu] = 0;
160 smp_message_recv(msg - 1, regs);
163 void
164 pmac_primary_intr(int irq, void *d, struct pt_regs *regs)
166 pmac_smp_message_recv(regs);
170 * 750's don't broadcast tlb invalidates so
171 * we have to emulate that behavior.
172 * -- Cort
174 void smp_send_tlb_invalidate(int cpu)
176 if ( (_get_PVR()>>16) == 8 )
177 smp_message_pass(MSG_ALL_BUT_SELF, PPC_MSG_INVALIDATE_TLB, 0, 0);
180 void smp_send_reschedule(int cpu)
183 * This is only used if `cpu' is running an idle task,
184 * so it will reschedule itself anyway...
186 * This isn't the case anymore since the other CPU could be
187 * sleeping and won't reschedule until the next interrupt (such
188 * as the timer).
189 * -- Cort
191 /* This is only used if `cpu' is running an idle task,
192 so it will reschedule itself anyway... */
193 smp_message_pass(cpu, PPC_MSG_RESCHEDULE, 0, 0);
196 #ifdef CONFIG_XMON
197 void smp_send_xmon_break(int cpu)
199 smp_message_pass(cpu, PPC_MSG_XMON_BREAK, 0, 0);
201 #endif /* CONFIG_XMON */
203 static void stop_this_cpu(void *dummy)
205 __cli();
206 while (1)
210 void smp_send_stop(void)
212 smp_call_function(stop_this_cpu, NULL, 1, 0);
213 smp_num_cpus = 1;
217 * Structure and data for smp_call_function(). This is designed to minimise
218 * static memory requirements. It also looks cleaner.
219 * Stolen from the i386 version.
221 static spinlock_t call_lock = SPIN_LOCK_UNLOCKED;
223 static volatile struct call_data_struct {
224 void (*func) (void *info);
225 void *info;
226 atomic_t started;
227 atomic_t finished;
228 int wait;
229 } *call_data;
232 * this function sends a 'generic call function' IPI to all other CPUs
233 * in the system.
236 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
237 int wait)
239 * [SUMMARY] Run a function on all other CPUs.
240 * <func> The function to run. This must be fast and non-blocking.
241 * <info> An arbitrary pointer to pass to the function.
242 * <nonatomic> currently unused.
243 * <wait> If true, wait (atomically) until function has completed on other CPUs.
244 * [RETURNS] 0 on success, else a negative status code. Does not return until
245 * remote CPUs are nearly ready to execute <<func>> or are or have executed.
247 * You must not call this function with disabled interrupts or from a
248 * hardware interrupt handler, you may call it from a bottom half handler.
251 struct call_data_struct data;
252 int ret = -1, cpus = smp_num_cpus-1;
253 int timeout;
255 if (!cpus)
256 return 0;
258 data.func = func;
259 data.info = info;
260 atomic_set(&data.started, 0);
261 data.wait = wait;
262 if (wait)
263 atomic_set(&data.finished, 0);
265 spin_lock_bh(&call_lock);
266 call_data = &data;
267 /* Send a message to all other CPUs and wait for them to respond */
268 smp_message_pass(MSG_ALL_BUT_SELF, PPC_MSG_CALL_FUNCTION, 0, 0);
270 /* Wait for response */
271 timeout = 1000000;
272 while (atomic_read(&data.started) != cpus) {
273 if (--timeout == 0) {
274 printk("smp_call_function on cpu %d: other cpus not responding (%d)\n",
275 smp_processor_id(), atomic_read(&data.started));
276 goto out;
278 barrier();
279 udelay(1);
282 if (wait) {
283 timeout = 1000000;
284 while (atomic_read(&data.finished) != cpus) {
285 if (--timeout == 0) {
286 printk("smp_call_function on cpu %d: other cpus not finishing (%d/%d)\n",
287 smp_processor_id(), atomic_read(&data.finished), atomic_read(&data.started));
288 goto out;
290 barrier();
291 udelay(1);
294 ret = 0;
296 out:
297 spin_unlock_bh(&call_lock);
298 return ret;
301 void smp_call_function_interrupt(void)
303 void (*func) (void *info) = call_data->func;
304 void *info = call_data->info;
305 int wait = call_data->wait;
308 * Notify initiating CPU that I've grabbed the data and am
309 * about to execute the function
311 atomic_inc(&call_data->started);
313 * At this point the info structure may be out of scope unless wait==1
315 (*func)(info);
316 if (wait)
317 atomic_inc(&call_data->finished);
320 void smp_message_pass(int target, int msg, unsigned long data, int wait)
322 if ( !(_machine & (_MACH_Pmac|_MACH_chrp|_MACH_prep|_MACH_gemini)) )
323 return;
325 switch (_machine) {
326 case _MACH_Pmac:
328 * IPI's on the Pmac are a hack but without reasonable
329 * IPI hardware SMP on Pmac is a hack.
331 * We assume here that the msg is not -1. If it is,
332 * the recipient won't know the message was destined
333 * for it. -- Cort
335 if (smp_processor_id() == 0) {
336 /* primary cpu */
337 if (target == 1 || target == MSG_ALL_BUT_SELF
338 || target == MSG_ALL) {
339 pmac_smp_message[1] = msg + 1;
340 /* interrupt secondary processor */
341 out_be32(psurge_sec_intr, ~0);
342 out_be32(psurge_sec_intr, 0);
344 } else {
345 /* secondary cpu */
346 if (target == 0 || target == MSG_ALL_BUT_SELF
347 || target == MSG_ALL) {
348 pmac_smp_message[0] = msg + 1;
349 /* interrupt primary processor */
350 in_be32(psurge_pri_intr);
353 if (target == smp_processor_id() || target == MSG_ALL) {
354 /* sending a message to ourself */
355 /* XXX maybe we shouldn't do this if ints are off */
356 smp_message_recv(msg, NULL);
358 break;
359 case _MACH_chrp:
360 case _MACH_prep:
361 case _MACH_gemini:
362 #ifndef CONFIG_POWER4
363 /* make sure we're sending something that translates to an IPI */
364 if ( msg > 0x3 )
365 break;
366 switch ( target )
368 case MSG_ALL:
369 openpic_cause_IPI(smp_processor_id(), msg, 0xffffffff);
370 break;
371 case MSG_ALL_BUT_SELF:
372 openpic_cause_IPI(smp_processor_id(), msg,
373 0xffffffff & ~(1 << smp_processor_id()));
374 break;
375 default:
376 openpic_cause_IPI(smp_processor_id(), msg, 1<<target);
377 break;
379 #else /* CONFIG_POWER4 */
380 /* for now, only do reschedule messages
381 since we only have one IPI */
382 if (msg != PPC_MSG_RESCHEDULE)
383 break;
384 for (i = 0; i < smp_num_cpus; ++i) {
385 if (target == MSG_ALL || target == i
386 || (target == MSG_ALL_BUT_SELF
387 && i != smp_processor_id()))
388 xics_cause_IPI(i);
390 #endif /* CONFIG_POWER4 */
391 break;
395 void __init smp_boot_cpus(void)
397 extern struct task_struct *current_set[NR_CPUS];
398 extern unsigned long smp_chrp_cpu_nr;
399 extern void __secondary_start_psurge(void);
400 extern void __secondary_start_chrp(void);
401 int i, cpu_nr;
402 struct task_struct *p;
403 unsigned long a;
405 printk("Entering SMP Mode...\n");
406 smp_num_cpus = 1;
407 smp_store_cpu_info(0);
410 * assume for now that the first cpu booted is
411 * cpu 0, the master -- Cort
413 cpu_callin_map[0] = 1;
414 active_kernel_processor = 0;
415 current->processor = 0;
417 init_idle();
419 for (i = 0; i < NR_CPUS; i++) {
420 prof_counter[i] = 1;
421 prof_multiplier[i] = 1;
425 * XXX very rough, assumes 20 bus cycles to read a cache line,
426 * timebase increments every 4 bus cycles, 32kB L1 data cache.
428 cacheflush_time = 5 * 1024;
430 if ( !(_machine & (_MACH_Pmac|_MACH_chrp|_MACH_gemini)) )
432 printk("SMP not supported on this machine.\n");
433 return;
436 switch ( _machine )
438 case _MACH_Pmac:
439 /* assume powersurge board - 2 processors -- Cort */
440 cpu_nr = 2;
441 psurge_pri_intr = ioremap(PSURGE_PRI_INTR, 4);
442 psurge_sec_intr = ioremap(PSURGE_SEC_INTR, 4);
443 psurge_start = ioremap(PSURGE_START, 4);
444 break;
445 case _MACH_chrp:
446 if (OpenPIC)
447 for ( i = 0; i < 4 ; i++ )
448 openpic_enable_IPI(i);
449 cpu_nr = smp_chrp_cpu_nr;
450 break;
451 case _MACH_gemini:
452 for ( i = 0; i < 4 ; i++ )
453 openpic_enable_IPI(i);
454 cpu_nr = (readb(GEMINI_CPUSTAT) & GEMINI_CPU_COUNT_MASK)>>2;
455 cpu_nr = (cpu_nr == 0) ? 4 : cpu_nr;
456 break;
460 * only check for cpus we know exist. We keep the callin map
461 * with cpus at the bottom -- Cort
463 for ( i = 1 ; i < cpu_nr; i++ )
465 int c;
466 struct pt_regs regs;
468 /* create a process for the processor */
469 /* we don't care about the values in regs since we'll
470 never reschedule the forked task. */
471 if (do_fork(CLONE_VM|CLONE_PID, 0, &regs, 0) < 0)
472 panic("failed fork for CPU %d", i);
473 p = init_task.prev_task;
474 if (!p)
475 panic("No idle task for CPU %d", i);
476 del_from_runqueue(p);
477 unhash_process(p);
478 init_tasks[i] = p;
480 p->processor = i;
481 p->has_cpu = 1;
482 current_set[i] = p;
484 /* need to flush here since secondary bats aren't setup */
485 for (a = KERNELBASE; a < KERNELBASE + 0x800000; a += 32)
486 asm volatile("dcbf 0,%0" : : "r" (a) : "memory");
487 asm volatile("sync");
489 /* wake up cpus */
490 switch ( _machine )
492 case _MACH_Pmac:
493 /* setup entry point of secondary processor */
494 out_be32(psurge_start, __pa(__secondary_start_psurge));
495 /* interrupt secondary to begin executing code */
496 out_be32(psurge_sec_intr, ~0);
497 udelay(1);
498 out_be32(psurge_sec_intr, 0);
499 break;
500 case _MACH_chrp:
501 *(unsigned long *)KERNELBASE = i;
502 asm volatile("dcbf 0,%0"::"r"(KERNELBASE):"memory");
503 break;
504 case _MACH_gemini:
505 openpic_init_processor( 1<<i );
506 openpic_init_processor( 0 );
507 break;
511 * wait to see if the cpu made a callin (is actually up).
512 * use this value that I found through experimentation.
513 * -- Cort
515 for ( c = 1000; c && !cpu_callin_map[i] ; c-- )
516 udelay(100);
518 if ( cpu_callin_map[i] )
520 printk("Processor %d found.\n", i);
521 smp_num_cpus++;
522 } else {
523 printk("Processor %d is stuck.\n", i);
527 if (OpenPIC && (_machine & (_MACH_gemini|_MACH_chrp|_MACH_prep)))
528 do_openpic_setup_cpu();
530 if ( _machine == _MACH_Pmac )
532 /* reset the entry point so if we get another intr we won't
533 * try to startup again */
534 out_be32(psurge_start, 0x100);
535 if (request_irq(30, pmac_primary_intr, 0, "primary IPI", 0))
536 printk(KERN_ERR "Couldn't get primary IPI interrupt");
538 * The decrementers of both cpus are frozen at this point
539 * until we give the secondary cpu another interrupt.
540 * We set them both to decrementer_count and then send
541 * the interrupt. This should get the decrementers
542 * synchronized.
543 * -- paulus.
545 set_dec(tb_ticks_per_jiffy);
546 if ((_get_PVR() >> 16) != 1) {
547 set_tb(0, 0); /* set timebase if not 601 */
548 last_jiffy_stamp(0) = 0;
550 out_be32(psurge_sec_intr, ~0);
551 udelay(1);
552 out_be32(psurge_sec_intr, 0);
556 void __init smp_commence(void)
559 * Lets the callin's below out of their loop.
561 wmb();
562 smp_commenced = 1;
565 /* intel needs this */
566 void __init initialize_secondary(void)
570 /* Activate a secondary processor. */
571 int __init start_secondary(void *unused)
573 atomic_inc(&init_mm.mm_count);
574 current->active_mm = &init_mm;
575 smp_callin();
576 return cpu_idle(NULL);
579 void __init smp_callin(void)
581 smp_store_cpu_info(current->processor);
582 set_dec(tb_ticks_per_jiffy);
583 if (_machine == _MACH_Pmac && (_get_PVR() >> 16) != 1) {
584 set_tb(0, 0); /* set timebase if not 601 */
585 last_jiffy_stamp(current->processor) = 0;
587 init_idle();
588 cpu_callin_map[current->processor] = 1;
590 #ifndef CONFIG_POWER4
592 * Each processor has to do this and this is the best
593 * place to stick it for now.
594 * -- Cort
596 if (OpenPIC && _machine & (_MACH_gemini|_MACH_chrp|_MACH_prep))
597 do_openpic_setup_cpu();
598 #else
599 xics_setup_cpu();
600 #endif /* CONFIG_POWER4 */
601 #ifdef CONFIG_GEMINI
602 if ( _machine == _MACH_gemini )
603 gemini_init_l2();
604 #endif
605 while(!smp_commenced)
606 barrier();
607 __sti();
610 void __init smp_setup(char *str, int *ints)
614 int __init setup_profiling_timer(unsigned int multiplier)
616 return 0;
619 void __init smp_store_cpu_info(int id)
621 struct cpuinfo_PPC *c = &cpu_data[id];
623 /* assume bogomips are same for everything */
624 c->loops_per_sec = loops_per_sec;
625 c->pvr = _get_PVR();