Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * SMP support for ppc. | |
3 | * | |
4 | * Written by Cort Dougan (cort@cs.nmt.edu) borrowing a great | |
5 | * deal of code from the sparc and intel versions. | |
6 | * | |
7 | * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu> | |
8 | * | |
9 | * PowerPC-64 Support added by Dave Engebretsen, Peter Bergner, and | |
10 | * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU General Public License | |
14 | * as published by the Free Software Foundation; either version | |
15 | * 2 of the License, or (at your option) any later version. | |
16 | */ | |
17 | ||
18 | #undef DEBUG | |
19 | ||
20 | #include <linux/config.h> | |
21 | #include <linux/kernel.h> | |
22 | #include <linux/module.h> | |
23 | #include <linux/sched.h> | |
24 | #include <linux/smp.h> | |
25 | #include <linux/interrupt.h> | |
26 | #include <linux/delay.h> | |
27 | #include <linux/init.h> | |
28 | #include <linux/spinlock.h> | |
29 | #include <linux/cache.h> | |
30 | #include <linux/err.h> | |
31 | #include <linux/sysdev.h> | |
32 | #include <linux/cpu.h> | |
33 | #include <linux/notifier.h> | |
34 | ||
35 | #include <asm/ptrace.h> | |
36 | #include <asm/atomic.h> | |
37 | #include <asm/irq.h> | |
38 | #include <asm/page.h> | |
39 | #include <asm/pgtable.h> | |
40 | #include <asm/prom.h> | |
41 | #include <asm/smp.h> | |
42 | #include <asm/paca.h> | |
43 | #include <asm/time.h> | |
44 | #include <asm/machdep.h> | |
45 | #include <asm/cputable.h> | |
46 | #include <asm/system.h> | |
47 | #include <asm/abs_addr.h> | |
48 | ||
49 | #include "mpic.h" | |
50 | ||
51 | #ifdef DEBUG | |
52 | #define DBG(fmt...) udbg_printf(fmt) | |
53 | #else | |
54 | #define DBG(fmt...) | |
55 | #endif | |
56 | ||
57 | cpumask_t cpu_possible_map = CPU_MASK_NONE; | |
58 | cpumask_t cpu_online_map = CPU_MASK_NONE; | |
59 | cpumask_t cpu_sibling_map[NR_CPUS] = { [0 ... NR_CPUS-1] = CPU_MASK_NONE }; | |
60 | ||
61 | EXPORT_SYMBOL(cpu_online_map); | |
62 | EXPORT_SYMBOL(cpu_possible_map); | |
63 | ||
64 | struct smp_ops_t *smp_ops; | |
65 | ||
66 | static volatile unsigned int cpu_callin_map[NR_CPUS]; | |
67 | ||
1da177e4 LT |
68 | void smp_call_function_interrupt(void); |
69 | ||
70 | int smt_enabled_at_boot = 1; | |
71 | ||
cebf589c | 72 | #ifdef CONFIG_MPIC |
1da177e4 LT |
73 | void smp_mpic_message_pass(int target, int msg) |
74 | { | |
75 | /* make sure we're sending something that translates to an IPI */ | |
76 | if ( msg > 0x3 ){ | |
77 | printk("SMP %d: smp_message_pass: unknown msg %d\n", | |
78 | smp_processor_id(), msg); | |
79 | return; | |
80 | } | |
81 | switch ( target ) | |
82 | { | |
83 | case MSG_ALL: | |
84 | mpic_send_ipi(msg, 0xffffffff); | |
85 | break; | |
86 | case MSG_ALL_BUT_SELF: | |
87 | mpic_send_ipi(msg, 0xffffffff & ~(1 << smp_processor_id())); | |
88 | break; | |
89 | default: | |
90 | mpic_send_ipi(msg, 1 << target); | |
91 | break; | |
92 | } | |
93 | } | |
94 | ||
95 | int __init smp_mpic_probe(void) | |
96 | { | |
97 | int nr_cpus; | |
98 | ||
99 | DBG("smp_mpic_probe()...\n"); | |
100 | ||
101 | nr_cpus = cpus_weight(cpu_possible_map); | |
102 | ||
103 | DBG("nr_cpus: %d\n", nr_cpus); | |
104 | ||
105 | if (nr_cpus > 1) | |
106 | mpic_request_ipis(); | |
107 | ||
108 | return nr_cpus; | |
109 | } | |
110 | ||
111 | void __devinit smp_mpic_setup_cpu(int cpu) | |
112 | { | |
113 | mpic_setup_this_cpu(); | |
114 | } | |
115 | ||
116 | void __devinit smp_generic_kick_cpu(int nr) | |
117 | { | |
118 | BUG_ON(nr < 0 || nr >= NR_CPUS); | |
119 | ||
120 | /* | |
121 | * The processor is currently spinning, waiting for the | |
122 | * cpu_start field to become non-zero After we set cpu_start, | |
123 | * the processor will continue on to secondary_start | |
124 | */ | |
125 | paca[nr].cpu_start = 1; | |
0d8d4d42 | 126 | smp_mb(); |
1da177e4 LT |
127 | } |
128 | ||
cebf589c | 129 | #endif /* CONFIG_MPIC */ |
1da177e4 LT |
130 | |
131 | static void __init smp_space_timers(unsigned int max_cpus) | |
132 | { | |
133 | int i; | |
134 | unsigned long offset = tb_ticks_per_jiffy / max_cpus; | |
135 | unsigned long previous_tb = paca[boot_cpuid].next_jiffy_update_tb; | |
136 | ||
137 | for_each_cpu(i) { | |
138 | if (i != boot_cpuid) { | |
139 | paca[i].next_jiffy_update_tb = | |
140 | previous_tb + offset; | |
141 | previous_tb = paca[i].next_jiffy_update_tb; | |
142 | } | |
143 | } | |
144 | } | |
145 | ||
146 | void smp_message_recv(int msg, struct pt_regs *regs) | |
147 | { | |
148 | switch(msg) { | |
149 | case PPC_MSG_CALL_FUNCTION: | |
150 | smp_call_function_interrupt(); | |
151 | break; | |
152 | case PPC_MSG_RESCHEDULE: | |
153 | /* XXX Do we have to do this? */ | |
154 | set_need_resched(); | |
155 | break; | |
156 | #if 0 | |
157 | case PPC_MSG_MIGRATE_TASK: | |
158 | /* spare */ | |
159 | break; | |
160 | #endif | |
161 | #ifdef CONFIG_DEBUGGER | |
162 | case PPC_MSG_DEBUGGER_BREAK: | |
163 | debugger_ipi(regs); | |
164 | break; | |
165 | #endif | |
166 | default: | |
167 | printk("SMP %d: smp_message_recv(): unknown msg %d\n", | |
168 | smp_processor_id(), msg); | |
169 | break; | |
170 | } | |
171 | } | |
172 | ||
173 | void smp_send_reschedule(int cpu) | |
174 | { | |
175 | smp_ops->message_pass(cpu, PPC_MSG_RESCHEDULE); | |
176 | } | |
177 | ||
178 | #ifdef CONFIG_DEBUGGER | |
179 | void smp_send_debugger_break(int cpu) | |
180 | { | |
181 | smp_ops->message_pass(cpu, PPC_MSG_DEBUGGER_BREAK); | |
182 | } | |
183 | #endif | |
184 | ||
185 | static void stop_this_cpu(void *dummy) | |
186 | { | |
187 | local_irq_disable(); | |
188 | while (1) | |
189 | ; | |
190 | } | |
191 | ||
192 | void smp_send_stop(void) | |
193 | { | |
194 | smp_call_function(stop_this_cpu, NULL, 1, 0); | |
195 | } | |
196 | ||
197 | /* | |
198 | * Structure and data for smp_call_function(). This is designed to minimise | |
199 | * static memory requirements. It also looks cleaner. | |
200 | * Stolen from the i386 version. | |
201 | */ | |
202 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_lock); | |
203 | ||
204 | static struct call_data_struct { | |
205 | void (*func) (void *info); | |
206 | void *info; | |
207 | atomic_t started; | |
208 | atomic_t finished; | |
209 | int wait; | |
210 | } *call_data; | |
211 | ||
212 | /* delay of at least 8 seconds on 1GHz cpu */ | |
213 | #define SMP_CALL_TIMEOUT (1UL << (30 + 3)) | |
214 | ||
215 | /* | |
216 | * This function sends a 'generic call function' IPI to all other CPUs | |
217 | * in the system. | |
218 | * | |
219 | * [SUMMARY] Run a function on all other CPUs. | |
220 | * <func> The function to run. This must be fast and non-blocking. | |
221 | * <info> An arbitrary pointer to pass to the function. | |
222 | * <nonatomic> currently unused. | |
223 | * <wait> If true, wait (atomically) until function has completed on other CPUs. | |
224 | * [RETURNS] 0 on success, else a negative status code. Does not return until | |
225 | * remote CPUs are nearly ready to execute <<func>> or are or have executed. | |
226 | * | |
227 | * You must not call this function with disabled interrupts or from a | |
228 | * hardware interrupt handler or from a bottom half handler. | |
229 | */ | |
230 | int smp_call_function (void (*func) (void *info), void *info, int nonatomic, | |
231 | int wait) | |
232 | { | |
233 | struct call_data_struct data; | |
234 | int ret = -1, cpus; | |
235 | unsigned long timeout; | |
236 | ||
237 | /* Can deadlock when called with interrupts disabled */ | |
238 | WARN_ON(irqs_disabled()); | |
239 | ||
240 | data.func = func; | |
241 | data.info = info; | |
242 | atomic_set(&data.started, 0); | |
243 | data.wait = wait; | |
244 | if (wait) | |
245 | atomic_set(&data.finished, 0); | |
246 | ||
247 | spin_lock(&call_lock); | |
248 | /* Must grab online cpu count with preempt disabled, otherwise | |
249 | * it can change. */ | |
250 | cpus = num_online_cpus() - 1; | |
251 | if (!cpus) { | |
252 | ret = 0; | |
253 | goto out; | |
254 | } | |
255 | ||
256 | call_data = &data; | |
0d8d4d42 | 257 | smp_wmb(); |
1da177e4 LT |
258 | /* Send a message to all other CPUs and wait for them to respond */ |
259 | smp_ops->message_pass(MSG_ALL_BUT_SELF, PPC_MSG_CALL_FUNCTION); | |
260 | ||
261 | /* Wait for response */ | |
262 | timeout = SMP_CALL_TIMEOUT; | |
263 | while (atomic_read(&data.started) != cpus) { | |
264 | HMT_low(); | |
265 | if (--timeout == 0) { | |
266 | printk("smp_call_function on cpu %d: other cpus not " | |
267 | "responding (%d)\n", smp_processor_id(), | |
268 | atomic_read(&data.started)); | |
269 | debugger(NULL); | |
270 | goto out; | |
271 | } | |
272 | } | |
273 | ||
274 | if (wait) { | |
275 | timeout = SMP_CALL_TIMEOUT; | |
276 | while (atomic_read(&data.finished) != cpus) { | |
277 | HMT_low(); | |
278 | if (--timeout == 0) { | |
279 | printk("smp_call_function on cpu %d: other " | |
280 | "cpus not finishing (%d/%d)\n", | |
281 | smp_processor_id(), | |
282 | atomic_read(&data.finished), | |
283 | atomic_read(&data.started)); | |
284 | debugger(NULL); | |
285 | goto out; | |
286 | } | |
287 | } | |
288 | } | |
289 | ||
290 | ret = 0; | |
291 | ||
292 | out: | |
293 | call_data = NULL; | |
294 | HMT_medium(); | |
295 | spin_unlock(&call_lock); | |
296 | return ret; | |
297 | } | |
298 | ||
299 | EXPORT_SYMBOL(smp_call_function); | |
300 | ||
301 | void smp_call_function_interrupt(void) | |
302 | { | |
303 | void (*func) (void *info); | |
304 | void *info; | |
305 | int wait; | |
306 | ||
307 | /* call_data will be NULL if the sender timed out while | |
308 | * waiting on us to receive the call. | |
309 | */ | |
310 | if (!call_data) | |
311 | return; | |
312 | ||
313 | func = call_data->func; | |
314 | info = call_data->info; | |
315 | wait = call_data->wait; | |
316 | ||
317 | if (!wait) | |
318 | smp_mb__before_atomic_inc(); | |
319 | ||
320 | /* | |
321 | * Notify initiating CPU that I've grabbed the data and am | |
322 | * about to execute the function | |
323 | */ | |
324 | atomic_inc(&call_data->started); | |
325 | /* | |
326 | * At this point the info structure may be out of scope unless wait==1 | |
327 | */ | |
328 | (*func)(info); | |
329 | if (wait) { | |
330 | smp_mb__before_atomic_inc(); | |
331 | atomic_inc(&call_data->finished); | |
332 | } | |
333 | } | |
334 | ||
1da177e4 LT |
335 | extern struct gettimeofday_struct do_gtod; |
336 | ||
337 | struct thread_info *current_set[NR_CPUS]; | |
338 | ||
339 | DECLARE_PER_CPU(unsigned int, pvr); | |
340 | ||
341 | static void __devinit smp_store_cpu_info(int id) | |
342 | { | |
343 | per_cpu(pvr, id) = mfspr(SPRN_PVR); | |
344 | } | |
345 | ||
346 | static void __init smp_create_idle(unsigned int cpu) | |
347 | { | |
348 | struct task_struct *p; | |
349 | ||
350 | /* create a process for the processor */ | |
351 | p = fork_idle(cpu); | |
352 | if (IS_ERR(p)) | |
353 | panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p)); | |
354 | paca[cpu].__current = p; | |
355 | current_set[cpu] = p->thread_info; | |
356 | } | |
357 | ||
358 | void __init smp_prepare_cpus(unsigned int max_cpus) | |
359 | { | |
360 | unsigned int cpu; | |
361 | ||
362 | DBG("smp_prepare_cpus\n"); | |
363 | ||
364 | /* | |
365 | * setup_cpu may need to be called on the boot cpu. We havent | |
366 | * spun any cpus up but lets be paranoid. | |
367 | */ | |
368 | BUG_ON(boot_cpuid != smp_processor_id()); | |
369 | ||
370 | /* Fixup boot cpu */ | |
371 | smp_store_cpu_info(boot_cpuid); | |
372 | cpu_callin_map[boot_cpuid] = 1; | |
373 | ||
374 | #ifndef CONFIG_PPC_ISERIES | |
375 | paca[boot_cpuid].next_jiffy_update_tb = tb_last_stamp = get_tb(); | |
376 | ||
377 | /* | |
378 | * Should update do_gtod.stamp_xsec. | |
379 | * For now we leave it which means the time can be some | |
380 | * number of msecs off until someone does a settimeofday() | |
381 | */ | |
382 | do_gtod.varp->tb_orig_stamp = tb_last_stamp; | |
383 | systemcfg->tb_orig_stamp = tb_last_stamp; | |
384 | #endif | |
385 | ||
386 | max_cpus = smp_ops->probe(); | |
387 | ||
388 | smp_space_timers(max_cpus); | |
389 | ||
390 | for_each_cpu(cpu) | |
391 | if (cpu != boot_cpuid) | |
392 | smp_create_idle(cpu); | |
393 | } | |
394 | ||
395 | void __devinit smp_prepare_boot_cpu(void) | |
396 | { | |
397 | BUG_ON(smp_processor_id() != boot_cpuid); | |
398 | ||
399 | cpu_set(boot_cpuid, cpu_online_map); | |
400 | ||
401 | paca[boot_cpuid].__current = current; | |
402 | current_set[boot_cpuid] = current->thread_info; | |
403 | } | |
404 | ||
405 | #ifdef CONFIG_HOTPLUG_CPU | |
406 | /* State of each CPU during hotplug phases */ | |
407 | DEFINE_PER_CPU(int, cpu_state) = { 0 }; | |
408 | ||
409 | int generic_cpu_disable(void) | |
410 | { | |
411 | unsigned int cpu = smp_processor_id(); | |
412 | ||
413 | if (cpu == boot_cpuid) | |
414 | return -EBUSY; | |
415 | ||
416 | systemcfg->processorCount--; | |
417 | cpu_clear(cpu, cpu_online_map); | |
418 | fixup_irqs(cpu_online_map); | |
419 | return 0; | |
420 | } | |
421 | ||
422 | int generic_cpu_enable(unsigned int cpu) | |
423 | { | |
424 | /* Do the normal bootup if we haven't | |
425 | * already bootstrapped. */ | |
426 | if (system_state != SYSTEM_RUNNING) | |
427 | return -ENOSYS; | |
428 | ||
429 | /* get the target out of it's holding state */ | |
430 | per_cpu(cpu_state, cpu) = CPU_UP_PREPARE; | |
0d8d4d42 | 431 | smp_wmb(); |
1da177e4 LT |
432 | |
433 | while (!cpu_online(cpu)) | |
434 | cpu_relax(); | |
435 | ||
436 | fixup_irqs(cpu_online_map); | |
437 | /* counter the irq disable in fixup_irqs */ | |
438 | local_irq_enable(); | |
439 | return 0; | |
440 | } | |
441 | ||
442 | void generic_cpu_die(unsigned int cpu) | |
443 | { | |
444 | int i; | |
445 | ||
446 | for (i = 0; i < 100; i++) { | |
0d8d4d42 | 447 | smp_rmb(); |
1da177e4 LT |
448 | if (per_cpu(cpu_state, cpu) == CPU_DEAD) |
449 | return; | |
450 | msleep(100); | |
451 | } | |
452 | printk(KERN_ERR "CPU%d didn't die...\n", cpu); | |
453 | } | |
454 | ||
455 | void generic_mach_cpu_die(void) | |
456 | { | |
457 | unsigned int cpu; | |
458 | ||
459 | local_irq_disable(); | |
460 | cpu = smp_processor_id(); | |
461 | printk(KERN_DEBUG "CPU%d offline\n", cpu); | |
462 | __get_cpu_var(cpu_state) = CPU_DEAD; | |
0d8d4d42 | 463 | smp_wmb(); |
1da177e4 LT |
464 | while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE) |
465 | cpu_relax(); | |
466 | ||
467 | flush_tlb_pending(); | |
468 | cpu_set(cpu, cpu_online_map); | |
469 | local_irq_enable(); | |
470 | } | |
471 | #endif | |
472 | ||
473 | static int __devinit cpu_enable(unsigned int cpu) | |
474 | { | |
475 | if (smp_ops->cpu_enable) | |
476 | return smp_ops->cpu_enable(cpu); | |
477 | ||
478 | return -ENOSYS; | |
479 | } | |
480 | ||
481 | int __devinit __cpu_up(unsigned int cpu) | |
482 | { | |
483 | int c; | |
484 | ||
485 | if (!cpu_enable(cpu)) | |
486 | return 0; | |
487 | ||
488 | if (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu)) | |
489 | return -EINVAL; | |
490 | ||
c4eb2a93 | 491 | paca[cpu].default_decr = tb_ticks_per_jiffy; |
1da177e4 | 492 | |
1da177e4 LT |
493 | /* Make sure callin-map entry is 0 (can be leftover a CPU |
494 | * hotplug | |
495 | */ | |
496 | cpu_callin_map[cpu] = 0; | |
497 | ||
498 | /* The information for processor bringup must | |
499 | * be written out to main store before we release | |
500 | * the processor. | |
501 | */ | |
0d8d4d42 | 502 | smp_mb(); |
1da177e4 LT |
503 | |
504 | /* wake up cpus */ | |
505 | DBG("smp: kicking cpu %d\n", cpu); | |
506 | smp_ops->kick_cpu(cpu); | |
507 | ||
508 | /* | |
509 | * wait to see if the cpu made a callin (is actually up). | |
510 | * use this value that I found through experimentation. | |
511 | * -- Cort | |
512 | */ | |
513 | if (system_state < SYSTEM_RUNNING) | |
514 | for (c = 5000; c && !cpu_callin_map[cpu]; c--) | |
515 | udelay(100); | |
516 | #ifdef CONFIG_HOTPLUG_CPU | |
517 | else | |
518 | /* | |
519 | * CPUs can take much longer to come up in the | |
520 | * hotplug case. Wait five seconds. | |
521 | */ | |
522 | for (c = 25; c && !cpu_callin_map[cpu]; c--) { | |
523 | msleep(200); | |
524 | } | |
525 | #endif | |
526 | ||
527 | if (!cpu_callin_map[cpu]) { | |
528 | printk("Processor %u is stuck.\n", cpu); | |
529 | return -ENOENT; | |
530 | } | |
531 | ||
532 | printk("Processor %u found.\n", cpu); | |
533 | ||
534 | if (smp_ops->give_timebase) | |
535 | smp_ops->give_timebase(); | |
536 | ||
537 | /* Wait until cpu puts itself in the online map */ | |
538 | while (!cpu_online(cpu)) | |
539 | cpu_relax(); | |
540 | ||
541 | return 0; | |
542 | } | |
543 | ||
544 | ||
545 | /* Activate a secondary processor. */ | |
546 | int __devinit start_secondary(void *unused) | |
547 | { | |
548 | unsigned int cpu = smp_processor_id(); | |
549 | ||
550 | atomic_inc(&init_mm.mm_count); | |
551 | current->active_mm = &init_mm; | |
552 | ||
553 | smp_store_cpu_info(cpu); | |
554 | set_dec(paca[cpu].default_decr); | |
555 | cpu_callin_map[cpu] = 1; | |
556 | ||
557 | smp_ops->setup_cpu(cpu); | |
558 | if (smp_ops->take_timebase) | |
559 | smp_ops->take_timebase(); | |
560 | ||
561 | spin_lock(&call_lock); | |
562 | cpu_set(cpu, cpu_online_map); | |
563 | spin_unlock(&call_lock); | |
564 | ||
565 | local_irq_enable(); | |
566 | ||
567 | cpu_idle(); | |
568 | return 0; | |
569 | } | |
570 | ||
571 | int setup_profiling_timer(unsigned int multiplier) | |
572 | { | |
573 | return 0; | |
574 | } | |
575 | ||
576 | void __init smp_cpus_done(unsigned int max_cpus) | |
577 | { | |
578 | cpumask_t old_mask; | |
579 | ||
580 | /* We want the setup_cpu() here to be called from CPU 0, but our | |
581 | * init thread may have been "borrowed" by another CPU in the meantime | |
582 | * se we pin us down to CPU 0 for a short while | |
583 | */ | |
584 | old_mask = current->cpus_allowed; | |
585 | set_cpus_allowed(current, cpumask_of_cpu(boot_cpuid)); | |
586 | ||
587 | smp_ops->setup_cpu(boot_cpuid); | |
588 | ||
589 | set_cpus_allowed(current, old_mask); | |
590 | } | |
591 | ||
592 | #ifdef CONFIG_HOTPLUG_CPU | |
593 | int __cpu_disable(void) | |
594 | { | |
595 | if (smp_ops->cpu_disable) | |
596 | return smp_ops->cpu_disable(); | |
597 | ||
598 | return -ENOSYS; | |
599 | } | |
600 | ||
601 | void __cpu_die(unsigned int cpu) | |
602 | { | |
603 | if (smp_ops->cpu_die) | |
604 | smp_ops->cpu_die(cpu); | |
605 | } | |
606 | #endif |