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