4 * Written by Cort Dougan (cort@cs.nmt.edu) borrowing a great
5 * deal of code from the sparc and intel versions.
7 * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/smp.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel_stat.h>
17 #include <linux/delay.h>
18 #include <linux/init.h>
19 #include <linux/spinlock.h>
20 #include <linux/cache.h>
22 #include <asm/ptrace.h>
23 #include <asm/atomic.h>
26 #include <asm/pgtable.h>
30 #include <asm/residual.h>
32 #include <asm/thread_info.h>
33 #include <asm/tlbflush.h>
35 #include <asm/machdep.h>
37 volatile int smp_commenced;
38 int smp_tb_synchronized;
39 struct cpuinfo_PPC cpu_data[NR_CPUS];
42 cpumask_t cpu_online_map;
43 cpumask_t cpu_possible_map;
44 int smp_hw_index[NR_CPUS];
45 struct thread_info *secondary_ti;
46 static struct task_struct *idle_tasks[NR_CPUS];
48 EXPORT_SYMBOL(cpu_online_map);
49 EXPORT_SYMBOL(cpu_possible_map);
51 /* SMP operations for this machine */
52 struct smp_ops_t *smp_ops;
54 /* all cpu mappings are 1-1 -- Cort */
55 volatile unsigned long cpu_callin_map[NR_CPUS];
57 int start_secondary(void *);
58 void smp_call_function_interrupt(void);
59 static int __smp_call_function(void (*func) (void *info), void *info,
60 int wait, int target);
62 /* Low level assembly function used to backup CPU 0 state */
63 extern void __save_cpu_setup(void);
65 /* Since OpenPIC has only 4 IPIs, we use slightly different message numbers.
67 * Make sure this matches openpic_request_IPIs in open_pic.c, or what shows up
68 * in /proc/interrupts will be wrong!!! --Troy */
69 #define PPC_MSG_CALL_FUNCTION 0
70 #define PPC_MSG_RESCHEDULE 1
71 #define PPC_MSG_INVALIDATE_TLB 2
72 #define PPC_MSG_XMON_BREAK 3
75 smp_message_pass(int target, int msg)
78 atomic_inc(&ipi_sent);
79 smp_ops->message_pass(target, msg);
86 void smp_message_recv(int msg)
88 atomic_inc(&ipi_recv);
91 case PPC_MSG_CALL_FUNCTION:
92 smp_call_function_interrupt();
94 case PPC_MSG_RESCHEDULE:
97 case PPC_MSG_INVALIDATE_TLB:
101 case PPC_MSG_XMON_BREAK:
102 xmon(get_irq_regs());
104 #endif /* CONFIG_XMON */
106 printk("SMP %d: smp_message_recv(): unknown msg %d\n",
107 smp_processor_id(), msg);
113 * 750's don't broadcast tlb invalidates so
114 * we have to emulate that behavior.
117 void smp_send_tlb_invalidate(int cpu)
119 if ( PVR_VER(mfspr(SPRN_PVR)) == 8 )
120 smp_message_pass(MSG_ALL_BUT_SELF, PPC_MSG_INVALIDATE_TLB);
123 void smp_send_reschedule(int cpu)
126 * This is only used if `cpu' is running an idle task,
127 * so it will reschedule itself anyway...
129 * This isn't the case anymore since the other CPU could be
130 * sleeping and won't reschedule until the next interrupt (such
134 /* This is only used if `cpu' is running an idle task,
135 so it will reschedule itself anyway... */
136 smp_message_pass(cpu, PPC_MSG_RESCHEDULE);
140 void smp_send_xmon_break(int cpu)
142 smp_message_pass(cpu, PPC_MSG_XMON_BREAK);
144 #endif /* CONFIG_XMON */
146 static void stop_this_cpu(void *dummy)
153 void smp_send_stop(void)
155 smp_call_function(stop_this_cpu, NULL, 1, 0);
159 * Structure and data for smp_call_function(). This is designed to minimise
160 * static memory requirements. It also looks cleaner.
161 * Stolen from the i386 version.
163 static DEFINE_SPINLOCK(call_lock);
165 static struct call_data_struct {
166 void (*func) (void *info);
174 * this function sends a 'generic call function' IPI to all other CPUs
178 int smp_call_function(void (*func) (void *info), void *info, int nonatomic,
181 * [SUMMARY] Run a function on all other CPUs.
182 * <func> The function to run. This must be fast and non-blocking.
183 * <info> An arbitrary pointer to pass to the function.
184 * <nonatomic> currently unused.
185 * <wait> If true, wait (atomically) until function has completed on other CPUs.
186 * [RETURNS] 0 on success, else a negative status code. Does not return until
187 * remote CPUs are nearly ready to execute <<func>> or are or have executed.
189 * You must not call this function with disabled interrupts or from a
190 * hardware interrupt handler or from a bottom half handler.
193 /* FIXME: get cpu lock with hotplug cpus, or change this to
195 if (num_online_cpus() <= 1)
197 /* Can deadlock when called with interrupts disabled */
198 WARN_ON(irqs_disabled());
199 return __smp_call_function(func, info, wait, MSG_ALL_BUT_SELF);
202 static int __smp_call_function(void (*func) (void *info), void *info,
203 int wait, int target)
205 struct call_data_struct data;
210 if (target == MSG_ALL_BUT_SELF)
211 ncpus = num_online_cpus() - 1;
212 else if (target == MSG_ALL)
213 ncpus = num_online_cpus();
217 atomic_set(&data.started, 0);
220 atomic_set(&data.finished, 0);
222 spin_lock(&call_lock);
224 /* Send a message to all other CPUs and wait for them to respond */
225 smp_message_pass(target, PPC_MSG_CALL_FUNCTION);
227 /* Wait for response */
229 while (atomic_read(&data.started) != ncpus) {
230 if (--timeout == 0) {
231 printk("smp_call_function on cpu %d: other cpus not responding (%d)\n",
232 smp_processor_id(), atomic_read(&data.started));
241 while (atomic_read(&data.finished) != ncpus) {
242 if (--timeout == 0) {
243 printk("smp_call_function on cpu %d: other cpus not finishing (%d/%d)\n",
244 smp_processor_id(), atomic_read(&data.finished), atomic_read(&data.started));
254 spin_unlock(&call_lock);
258 void smp_call_function_interrupt(void)
260 void (*func) (void *info) = call_data->func;
261 void *info = call_data->info;
262 int wait = call_data->wait;
265 * Notify initiating CPU that I've grabbed the data and am
266 * about to execute the function
268 atomic_inc(&call_data->started);
270 * At this point the info structure may be out of scope unless wait==1
274 atomic_inc(&call_data->finished);
277 static void __devinit smp_store_cpu_info(int id)
279 struct cpuinfo_PPC *c = &cpu_data[id];
281 /* assume bogomips are same for everything */
282 c->loops_per_jiffy = loops_per_jiffy;
283 c->pvr = mfspr(SPRN_PVR);
286 void __init smp_prepare_cpus(unsigned int max_cpus)
288 int num_cpus, i, cpu;
289 struct task_struct *p;
292 smp_store_cpu_info(smp_processor_id());
293 cpu_callin_map[smp_processor_id()] = 1;
295 if (smp_ops == NULL) {
296 printk("SMP not supported on this machine.\n");
300 /* Probe platform for CPUs: always linear. */
301 num_cpus = smp_ops->probe();
304 smp_tb_synchronized = 1;
306 for (i = 0; i < num_cpus; ++i)
307 cpu_set(i, cpu_possible_map);
309 /* Backup CPU 0 state */
312 for_each_possible_cpu(cpu) {
313 if (cpu == smp_processor_id())
315 /* create a process for the processor */
318 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
319 task_thread_info(p)->cpu = cpu;
324 void __devinit smp_prepare_boot_cpu(void)
326 cpu_set(smp_processor_id(), cpu_online_map);
327 cpu_set(smp_processor_id(), cpu_possible_map);
330 int __init setup_profiling_timer(unsigned int multiplier)
335 /* Processor coming up starts here */
336 int __devinit start_secondary(void *unused)
340 atomic_inc(&init_mm.mm_count);
341 current->active_mm = &init_mm;
343 cpu = smp_processor_id();
344 smp_store_cpu_info(cpu);
345 set_dec(tb_ticks_per_jiffy);
347 cpu_callin_map[cpu] = 1;
349 printk("CPU %d done callin...\n", cpu);
350 smp_ops->setup_cpu(cpu);
351 printk("CPU %d done setup...\n", cpu);
352 smp_ops->take_timebase();
353 printk("CPU %d done timebase take...\n", cpu);
355 spin_lock(&call_lock);
356 cpu_set(cpu, cpu_online_map);
357 spin_unlock(&call_lock);
365 int __cpu_up(unsigned int cpu)
370 secondary_ti = task_thread_info(idle_tasks[cpu]);
374 * There was a cache flush loop here to flush the cache
375 * to memory for the first 8MB of RAM. The cache flush
376 * has been pushed into the kick_cpu function for those
377 * platforms that need it.
381 smp_ops->kick_cpu(cpu);
384 * wait to see if the cpu made a callin (is actually up).
385 * use this value that I found through experimentation.
388 for (c = 1000; c && !cpu_callin_map[cpu]; c--)
391 if (!cpu_callin_map[cpu]) {
392 sprintf(buf, "didn't find cpu %u", cpu);
393 if (ppc_md.progress) ppc_md.progress(buf, 0x360+cpu);
394 printk("Processor %u is stuck.\n", cpu);
398 sprintf(buf, "found cpu %u", cpu);
399 if (ppc_md.progress) ppc_md.progress(buf, 0x350+cpu);
400 printk("Processor %d found.\n", cpu);
402 smp_ops->give_timebase();
404 /* Wait until cpu puts itself in the online map */
405 while (!cpu_online(cpu))
411 void smp_cpus_done(unsigned int max_cpus)
413 smp_ops->setup_cpu(0);