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/smp_lock.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel_stat.h>
18 #include <linux/delay.h>
19 #include <linux/init.h>
20 #include <linux/spinlock.h>
21 #include <linux/cache.h>
23 #include <asm/ptrace.h>
24 #include <asm/atomic.h>
27 #include <asm/pgtable.h>
31 #include <asm/residual.h>
33 #include <asm/thread_info.h>
34 #include <asm/tlbflush.h>
36 #include <asm/machdep.h>
38 volatile int smp_commenced
;
39 int smp_tb_synchronized
;
40 struct cpuinfo_PPC cpu_data
[NR_CPUS
];
43 cpumask_t cpu_online_map
;
44 cpumask_t cpu_possible_map
;
45 int smp_hw_index
[NR_CPUS
];
46 struct thread_info
*secondary_ti
;
47 static struct task_struct
*idle_tasks
[NR_CPUS
];
49 EXPORT_SYMBOL(cpu_online_map
);
50 EXPORT_SYMBOL(cpu_possible_map
);
52 /* SMP operations for this machine */
53 struct smp_ops_t
*smp_ops
;
55 /* all cpu mappings are 1-1 -- Cort */
56 volatile unsigned long cpu_callin_map
[NR_CPUS
];
58 int start_secondary(void *);
59 void smp_call_function_interrupt(void);
60 static int __smp_call_function(void (*func
) (void *info
), void *info
,
61 int wait
, int target
);
63 /* Low level assembly function used to backup CPU 0 state */
64 extern void __save_cpu_setup(void);
66 /* Since OpenPIC has only 4 IPIs, we use slightly different message numbers.
68 * Make sure this matches openpic_request_IPIs in open_pic.c, or what shows up
69 * in /proc/interrupts will be wrong!!! --Troy */
70 #define PPC_MSG_CALL_FUNCTION 0
71 #define PPC_MSG_RESCHEDULE 1
72 #define PPC_MSG_INVALIDATE_TLB 2
73 #define PPC_MSG_XMON_BREAK 3
76 smp_message_pass(int target
, int msg
)
79 atomic_inc(&ipi_sent
);
80 smp_ops
->message_pass(target
, msg
);
87 void smp_message_recv(int msg
)
89 atomic_inc(&ipi_recv
);
92 case PPC_MSG_CALL_FUNCTION
:
93 smp_call_function_interrupt();
95 case PPC_MSG_RESCHEDULE
:
98 case PPC_MSG_INVALIDATE_TLB
:
102 case PPC_MSG_XMON_BREAK
:
103 xmon(get_irq_regs());
105 #endif /* CONFIG_XMON */
107 printk("SMP %d: smp_message_recv(): unknown msg %d\n",
108 smp_processor_id(), msg
);
114 * 750's don't broadcast tlb invalidates so
115 * we have to emulate that behavior.
118 void smp_send_tlb_invalidate(int cpu
)
120 if ( PVR_VER(mfspr(SPRN_PVR
)) == 8 )
121 smp_message_pass(MSG_ALL_BUT_SELF
, PPC_MSG_INVALIDATE_TLB
);
124 void smp_send_reschedule(int cpu
)
127 * This is only used if `cpu' is running an idle task,
128 * so it will reschedule itself anyway...
130 * This isn't the case anymore since the other CPU could be
131 * sleeping and won't reschedule until the next interrupt (such
135 /* This is only used if `cpu' is running an idle task,
136 so it will reschedule itself anyway... */
137 smp_message_pass(cpu
, PPC_MSG_RESCHEDULE
);
141 void smp_send_xmon_break(int cpu
)
143 smp_message_pass(cpu
, PPC_MSG_XMON_BREAK
);
145 #endif /* CONFIG_XMON */
147 static void stop_this_cpu(void *dummy
)
154 void smp_send_stop(void)
156 smp_call_function(stop_this_cpu
, NULL
, 1, 0);
160 * Structure and data for smp_call_function(). This is designed to minimise
161 * static memory requirements. It also looks cleaner.
162 * Stolen from the i386 version.
164 static DEFINE_SPINLOCK(call_lock
);
166 static struct call_data_struct
{
167 void (*func
) (void *info
);
175 * this function sends a 'generic call function' IPI to all other CPUs
179 int smp_call_function(void (*func
) (void *info
), void *info
, int nonatomic
,
182 * [SUMMARY] Run a function on all other CPUs.
183 * <func> The function to run. This must be fast and non-blocking.
184 * <info> An arbitrary pointer to pass to the function.
185 * <nonatomic> currently unused.
186 * <wait> If true, wait (atomically) until function has completed on other CPUs.
187 * [RETURNS] 0 on success, else a negative status code. Does not return until
188 * remote CPUs are nearly ready to execute <<func>> or are or have executed.
190 * You must not call this function with disabled interrupts or from a
191 * hardware interrupt handler or from a bottom half handler.
194 /* FIXME: get cpu lock with hotplug cpus, or change this to
196 if (num_online_cpus() <= 1)
198 /* Can deadlock when called with interrupts disabled */
199 WARN_ON(irqs_disabled());
200 return __smp_call_function(func
, info
, wait
, MSG_ALL_BUT_SELF
);
203 static int __smp_call_function(void (*func
) (void *info
), void *info
,
204 int wait
, int target
)
206 struct call_data_struct data
;
211 if (target
== MSG_ALL_BUT_SELF
)
212 ncpus
= num_online_cpus() - 1;
213 else if (target
== MSG_ALL
)
214 ncpus
= num_online_cpus();
218 atomic_set(&data
.started
, 0);
221 atomic_set(&data
.finished
, 0);
223 spin_lock(&call_lock
);
225 /* Send a message to all other CPUs and wait for them to respond */
226 smp_message_pass(target
, PPC_MSG_CALL_FUNCTION
);
228 /* Wait for response */
230 while (atomic_read(&data
.started
) != ncpus
) {
231 if (--timeout
== 0) {
232 printk("smp_call_function on cpu %d: other cpus not responding (%d)\n",
233 smp_processor_id(), atomic_read(&data
.started
));
242 while (atomic_read(&data
.finished
) != ncpus
) {
243 if (--timeout
== 0) {
244 printk("smp_call_function on cpu %d: other cpus not finishing (%d/%d)\n",
245 smp_processor_id(), atomic_read(&data
.finished
), atomic_read(&data
.started
));
255 spin_unlock(&call_lock
);
259 void smp_call_function_interrupt(void)
261 void (*func
) (void *info
) = call_data
->func
;
262 void *info
= call_data
->info
;
263 int wait
= call_data
->wait
;
266 * Notify initiating CPU that I've grabbed the data and am
267 * about to execute the function
269 atomic_inc(&call_data
->started
);
271 * At this point the info structure may be out of scope unless wait==1
275 atomic_inc(&call_data
->finished
);
278 static void __devinit
smp_store_cpu_info(int id
)
280 struct cpuinfo_PPC
*c
= &cpu_data
[id
];
282 /* assume bogomips are same for everything */
283 c
->loops_per_jiffy
= loops_per_jiffy
;
284 c
->pvr
= mfspr(SPRN_PVR
);
287 void __init
smp_prepare_cpus(unsigned int max_cpus
)
289 int num_cpus
, i
, cpu
;
290 struct task_struct
*p
;
293 smp_store_cpu_info(smp_processor_id());
294 cpu_callin_map
[smp_processor_id()] = 1;
296 if (smp_ops
== NULL
) {
297 printk("SMP not supported on this machine.\n");
301 /* Probe platform for CPUs: always linear. */
302 num_cpus
= smp_ops
->probe();
305 smp_tb_synchronized
= 1;
307 for (i
= 0; i
< num_cpus
; ++i
)
308 cpu_set(i
, cpu_possible_map
);
310 /* Backup CPU 0 state */
313 for_each_possible_cpu(cpu
) {
314 if (cpu
== smp_processor_id())
316 /* create a process for the processor */
319 panic("failed fork for CPU %u: %li", cpu
, PTR_ERR(p
));
320 task_thread_info(p
)->cpu
= cpu
;
325 void __devinit
smp_prepare_boot_cpu(void)
327 cpu_set(smp_processor_id(), cpu_online_map
);
328 cpu_set(smp_processor_id(), cpu_possible_map
);
331 int __init
setup_profiling_timer(unsigned int multiplier
)
336 /* Processor coming up starts here */
337 int __devinit
start_secondary(void *unused
)
341 atomic_inc(&init_mm
.mm_count
);
342 current
->active_mm
= &init_mm
;
344 cpu
= smp_processor_id();
345 smp_store_cpu_info(cpu
);
346 set_dec(tb_ticks_per_jiffy
);
348 cpu_callin_map
[cpu
] = 1;
350 printk("CPU %d done callin...\n", cpu
);
351 smp_ops
->setup_cpu(cpu
);
352 printk("CPU %d done setup...\n", cpu
);
353 smp_ops
->take_timebase();
354 printk("CPU %d done timebase take...\n", cpu
);
356 spin_lock(&call_lock
);
357 cpu_set(cpu
, cpu_online_map
);
358 spin_unlock(&call_lock
);
366 int __cpu_up(unsigned int cpu
)
371 secondary_ti
= task_thread_info(idle_tasks
[cpu
]);
375 * There was a cache flush loop here to flush the cache
376 * to memory for the first 8MB of RAM. The cache flush
377 * has been pushed into the kick_cpu function for those
378 * platforms that need it.
382 smp_ops
->kick_cpu(cpu
);
385 * wait to see if the cpu made a callin (is actually up).
386 * use this value that I found through experimentation.
389 for (c
= 1000; c
&& !cpu_callin_map
[cpu
]; c
--)
392 if (!cpu_callin_map
[cpu
]) {
393 sprintf(buf
, "didn't find cpu %u", cpu
);
394 if (ppc_md
.progress
) ppc_md
.progress(buf
, 0x360+cpu
);
395 printk("Processor %u is stuck.\n", cpu
);
399 sprintf(buf
, "found cpu %u", cpu
);
400 if (ppc_md
.progress
) ppc_md
.progress(buf
, 0x350+cpu
);
401 printk("Processor %d found.\n", cpu
);
403 smp_ops
->give_timebase();
405 /* Wait until cpu puts itself in the online map */
406 while (!cpu_online(cpu
))
412 void smp_cpus_done(unsigned int max_cpus
)
414 smp_ops
->setup_cpu(0);