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