2 * $Id: smp.c,v 1.68 1999/09/17 19:38:05 cort Exp $
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>
33 #include <asm/pgtable.h>
34 #include <asm/hardirq.h>
35 #include <asm/softirq.h>
40 #include <asm/gemini.h>
44 int smp_threads_ready
;
45 volatile int smp_commenced
;
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
)
107 case PPC_MSG_CALL_FUNCTION
:
108 smp_call_function_interrupt();
110 case PPC_MSG_RESCHEDULE
:
111 current
->need_resched
= 1;
113 case PPC_MSG_INVALIDATE_TLB
:
117 case PPC_MSG_XMON_BREAK
:
120 #endif /* CONFIG_XMON */
122 printk("SMP %d: smp_message_recv(): unknown msg %d\n",
123 smp_processor_id(), msg
);
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
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.
139 int pmac_smp_message
[NR_CPUS
];
140 void pmac_smp_message_recv(struct pt_regs
*regs
)
142 int cpu
= smp_processor_id();
145 /* clear interrupt */
147 out_be32(psurge_sec_intr
, ~0);
149 if (smp_num_cpus
< 2)
152 /* make sure there is a message there */
153 msg
= pmac_smp_message
[cpu
];
158 pmac_smp_message
[cpu
] = 0;
160 smp_message_recv(msg
- 1, regs
);
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.
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
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);
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
)
210 void smp_send_stop(void)
212 smp_call_function(stop_this_cpu
, NULL
, 1, 0);
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
);
232 * this function sends a 'generic call function' IPI to all other CPUs
236 int smp_call_function (void (*func
) (void *info
), void *info
, int nonatomic
,
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;
260 atomic_set(&data
.started
, 0);
263 atomic_set(&data
.finished
, 0);
265 spin_lock_bh(&call_lock
);
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 */
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
));
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
));
297 spin_unlock_bh(&call_lock
);
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
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
)) )
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
335 if (smp_processor_id() == 0) {
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);
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
);
362 #ifndef CONFIG_POWER4
363 /* make sure we're sending something that translates to an IPI */
369 openpic_cause_IPI(smp_processor_id(), msg
, 0xffffffff);
371 case MSG_ALL_BUT_SELF
:
372 openpic_cause_IPI(smp_processor_id(), msg
,
373 0xffffffff & ~(1 << smp_processor_id()));
376 openpic_cause_IPI(smp_processor_id(), msg
, 1<<target
);
379 #else /* CONFIG_POWER4 */
380 /* for now, only do reschedule messages
381 since we only have one IPI */
382 if (msg
!= PPC_MSG_RESCHEDULE
)
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()))
390 #endif /* CONFIG_POWER4 */
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);
402 struct task_struct
*p
;
405 printk("Entering SMP Mode...\n");
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;
419 for (i
= 0; i
< NR_CPUS
; i
++) {
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");
439 /* assume powersurge board - 2 processors -- Cort */
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);
447 for ( i
= 0; i
< 4 ; i
++ )
448 openpic_enable_IPI(i
);
449 cpu_nr
= smp_chrp_cpu_nr
;
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
;
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
++ )
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, ®s
, 0) < 0)
472 panic("failed fork for CPU %d", i
);
473 p
= init_task
.prev_task
;
475 panic("No idle task for CPU %d", i
);
476 del_from_runqueue(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");
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);
498 out_be32(psurge_sec_intr
, 0);
501 *(unsigned long *)KERNELBASE
= i
;
502 asm volatile("dcbf 0,%0"::"r"(KERNELBASE
):"memory");
505 openpic_init_processor( 1<<i
);
506 openpic_init_processor( 0 );
511 * wait to see if the cpu made a callin (is actually up).
512 * use this value that I found through experimentation.
515 for ( c
= 1000; c
&& !cpu_callin_map
[i
] ; c
-- )
518 if ( cpu_callin_map
[i
] )
520 printk("Processor %d found.\n", i
);
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
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);
552 out_be32(psurge_sec_intr
, 0);
556 void __init
smp_commence(void)
559 * Lets the callin's below out of their loop.
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
;
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;
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.
596 if (OpenPIC
&& _machine
& (_MACH_gemini
|_MACH_chrp
|_MACH_prep
))
597 do_openpic_setup_cpu();
600 #endif /* CONFIG_POWER4 */
602 if ( _machine
== _MACH_gemini
)
605 while(!smp_commenced
)
610 void __init
smp_setup(char *str
, int *ints
)
614 int __init
setup_profiling_timer(unsigned int multiplier
)
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
;