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/config.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/smp.h>
16 #include <linux/smp_lock.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/delay.h>
20 #include <linux/init.h>
21 #include <linux/spinlock.h>
22 #include <linux/cache.h>
24 #include <asm/ptrace.h>
25 #include <asm/atomic.h>
28 #include <asm/pgtable.h>
29 #include <asm/hardirq.h>
33 #include <asm/residual.h>
35 #include <asm/thread_info.h>
36 #include <asm/tlbflush.h>
39 int smp_threads_ready;
40 volatile int smp_commenced;
41 int smp_tb_synchronized;
42 struct cpuinfo_PPC cpu_data[NR_CPUS];
43 struct klock_info_struct klock_info = { KLOCK_CLEAR, 0 };
46 DEFINE_PER_CPU(unsigned int, prof_multiplier);
47 DEFINE_PER_CPU(unsigned int, prof_counter);
48 unsigned long cache_decay_ticks = HZ/100;
49 cpumask_t cpu_online_map;
50 cpumask_t cpu_possible_map;
51 int smp_hw_index[NR_CPUS];
52 struct thread_info *secondary_ti;
54 EXPORT_SYMBOL(cpu_online_map);
55 EXPORT_SYMBOL(cpu_possible_map);
57 /* SMP operations for this machine */
58 static struct smp_ops_t *smp_ops;
60 /* all cpu mappings are 1-1 -- Cort */
61 volatile unsigned long cpu_callin_map[NR_CPUS];
63 int start_secondary(void *);
64 extern int cpu_idle(void *unused);
65 void smp_call_function_interrupt(void);
66 static int __smp_call_function(void (*func) (void *info), void *info,
67 int wait, int target);
69 /* Low level assembly function used to backup CPU 0 state */
70 extern void __save_cpu_setup(void);
72 /* Since OpenPIC has only 4 IPIs, we use slightly different message numbers.
74 * Make sure this matches openpic_request_IPIs in open_pic.c, or what shows up
75 * in /proc/interrupts will be wrong!!! --Troy */
76 #define PPC_MSG_CALL_FUNCTION 0
77 #define PPC_MSG_RESCHEDULE 1
78 #define PPC_MSG_INVALIDATE_TLB 2
79 #define PPC_MSG_XMON_BREAK 3
82 smp_message_pass(int target, int msg, unsigned long data, int wait)
85 atomic_inc(&ipi_sent);
86 smp_ops->message_pass(target,msg,data,wait);
93 void smp_local_timer_interrupt(struct pt_regs * regs)
95 int cpu = smp_processor_id();
97 if (!--per_cpu(prof_counter, cpu)) {
98 update_process_times(user_mode(regs));
99 per_cpu(prof_counter, cpu) = per_cpu(prof_multiplier, cpu);
103 void smp_message_recv(int msg, struct pt_regs *regs)
105 atomic_inc(&ipi_recv);
108 case PPC_MSG_CALL_FUNCTION:
109 smp_call_function_interrupt();
111 case PPC_MSG_RESCHEDULE:
114 case PPC_MSG_INVALIDATE_TLB:
118 case PPC_MSG_XMON_BREAK:
121 #endif /* CONFIG_XMON */
123 printk("SMP %d: smp_message_recv(): unknown msg %d\n",
124 smp_processor_id(), msg);
130 * 750's don't broadcast tlb invalidates so
131 * we have to emulate that behavior.
134 void smp_send_tlb_invalidate(int cpu)
136 if ( PVR_VER(mfspr(PVR)) == 8 )
137 smp_message_pass(MSG_ALL_BUT_SELF, PPC_MSG_INVALIDATE_TLB, 0, 0);
140 void smp_send_reschedule(int cpu)
143 * This is only used if `cpu' is running an idle task,
144 * so it will reschedule itself anyway...
146 * This isn't the case anymore since the other CPU could be
147 * sleeping and won't reschedule until the next interrupt (such
151 /* This is only used if `cpu' is running an idle task,
152 so it will reschedule itself anyway... */
153 smp_message_pass(cpu, PPC_MSG_RESCHEDULE, 0, 0);
157 void smp_send_xmon_break(int cpu)
159 smp_message_pass(cpu, PPC_MSG_XMON_BREAK, 0, 0);
161 #endif /* CONFIG_XMON */
163 static void stop_this_cpu(void *dummy)
170 void smp_send_stop(void)
172 smp_call_function(stop_this_cpu, NULL, 1, 0);
176 * Structure and data for smp_call_function(). This is designed to minimise
177 * static memory requirements. It also looks cleaner.
178 * Stolen from the i386 version.
180 static spinlock_t call_lock = SPIN_LOCK_UNLOCKED;
182 static struct call_data_struct {
183 void (*func) (void *info);
191 * this function sends a 'generic call function' IPI to all other CPUs
195 int smp_call_function(void (*func) (void *info), void *info, int nonatomic,
198 * [SUMMARY] Run a function on all other CPUs.
199 * <func> The function to run. This must be fast and non-blocking.
200 * <info> An arbitrary pointer to pass to the function.
201 * <nonatomic> currently unused.
202 * <wait> If true, wait (atomically) until function has completed on other CPUs.
203 * [RETURNS] 0 on success, else a negative status code. Does not return until
204 * remote CPUs are nearly ready to execute <<func>> or are or have executed.
206 * You must not call this function with disabled interrupts or from a
207 * hardware interrupt handler or from a bottom half handler.
210 /* FIXME: get cpu lock with hotplug cpus, or change this to
212 if (num_online_cpus() <= 1)
214 /* Can deadlock when called with interrupts disabled */
215 WARN_ON(irqs_disabled());
216 return __smp_call_function(func, info, wait, MSG_ALL_BUT_SELF);
219 static int __smp_call_function(void (*func) (void *info), void *info,
220 int wait, int target)
222 struct call_data_struct data;
227 if (target == MSG_ALL_BUT_SELF)
228 ncpus = num_online_cpus() - 1;
229 else if (target == MSG_ALL)
230 ncpus = num_online_cpus();
234 atomic_set(&data.started, 0);
237 atomic_set(&data.finished, 0);
239 spin_lock(&call_lock);
241 /* Send a message to all other CPUs and wait for them to respond */
242 smp_message_pass(target, PPC_MSG_CALL_FUNCTION, 0, 0);
244 /* Wait for response */
246 while (atomic_read(&data.started) != ncpus) {
247 if (--timeout == 0) {
248 printk("smp_call_function on cpu %d: other cpus not responding (%d)\n",
249 smp_processor_id(), atomic_read(&data.started));
258 while (atomic_read(&data.finished) != ncpus) {
259 if (--timeout == 0) {
260 printk("smp_call_function on cpu %d: other cpus not finishing (%d/%d)\n",
261 smp_processor_id(), atomic_read(&data.finished), atomic_read(&data.started));
271 spin_unlock(&call_lock);
275 void smp_call_function_interrupt(void)
277 void (*func) (void *info) = call_data->func;
278 void *info = call_data->info;
279 int wait = call_data->wait;
282 * Notify initiating CPU that I've grabbed the data and am
283 * about to execute the function
285 atomic_inc(&call_data->started);
287 * At this point the info structure may be out of scope unless wait==1
291 atomic_inc(&call_data->finished);
294 static void __devinit smp_store_cpu_info(int id)
296 struct cpuinfo_PPC *c = &cpu_data[id];
298 /* assume bogomips are same for everything */
299 c->loops_per_jiffy = loops_per_jiffy;
301 per_cpu(prof_counter, id) = 1;
302 per_cpu(prof_multiplier, id) = 1;
305 void __init smp_prepare_cpus(unsigned int max_cpus)
310 smp_store_cpu_info(smp_processor_id());
311 cpu_callin_map[smp_processor_id()] = 1;
313 smp_ops = ppc_md.smp_ops;
314 if (smp_ops == NULL) {
315 printk("SMP not supported on this machine.\n");
319 /* Probe platform for CPUs: always linear. */
320 num_cpus = smp_ops->probe();
321 for (i = 0; i < num_cpus; ++i)
322 cpu_set(i, cpu_possible_map);
324 /* Backup CPU 0 state */
327 if (smp_ops->space_timers)
328 smp_ops->space_timers(num_cpus);
331 void __devinit smp_prepare_boot_cpu(void)
333 cpu_set(smp_processor_id(), cpu_online_map);
334 cpu_set(smp_processor_id(), cpu_possible_map);
337 int __init setup_profiling_timer(unsigned int multiplier)
342 /* Processor coming up starts here */
343 int __devinit start_secondary(void *unused)
347 atomic_inc(&init_mm.mm_count);
348 current->active_mm = &init_mm;
350 cpu = smp_processor_id();
351 smp_store_cpu_info(cpu);
352 set_dec(tb_ticks_per_jiffy);
353 cpu_callin_map[cpu] = 1;
355 printk("CPU %i done callin...\n", cpu);
356 smp_ops->setup_cpu(cpu);
357 printk("CPU %i done setup...\n", cpu);
359 smp_ops->take_timebase();
360 printk("CPU %i done timebase take...\n", cpu);
362 return cpu_idle(NULL);
365 int __cpu_up(unsigned int cpu)
368 struct task_struct *p;
372 /* create a process for the processor */
373 /* only regs.msr is actually used, and 0 is OK for it */
374 memset(®s, 0, sizeof(struct pt_regs));
375 p = copy_process(CLONE_VM|CLONE_IDLETASK, 0, ®s, 0, NULL, NULL);
377 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
378 wake_up_forked_process(p);
383 secondary_ti = p->thread_info;
384 p->thread_info->cpu = cpu;
387 * There was a cache flush loop here to flush the cache
388 * to memory for the first 8MB of RAM. The cache flush
389 * has been pushed into the kick_cpu function for those
390 * platforms that need it.
394 smp_ops->kick_cpu(cpu);
397 * wait to see if the cpu made a callin (is actually up).
398 * use this value that I found through experimentation.
401 for (c = 1000; c && !cpu_callin_map[cpu]; c--)
404 if (!cpu_callin_map[cpu]) {
405 sprintf(buf, "didn't find cpu %u", cpu);
406 if (ppc_md.progress) ppc_md.progress(buf, 0x360+cpu);
407 printk("Processor %u is stuck.\n", cpu);
411 sprintf(buf, "found cpu %u", cpu);
412 if (ppc_md.progress) ppc_md.progress(buf, 0x350+cpu);
413 printk("Processor %d found.\n", cpu);
415 smp_ops->give_timebase();
416 cpu_set(cpu, cpu_online_map);
420 void smp_cpus_done(unsigned int max_cpus)
422 smp_ops->setup_cpu(0);