ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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 #include <linux/config.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/sched.h>
22 #include <linux/smp.h>
23 #include <linux/smp_lock.h>
24 #include <linux/interrupt.h>
25 #include <linux/kernel_stat.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
34 #include <asm/ptrace.h>
35 #include <asm/atomic.h>
36 #include <asm/irq.h>
37 #include <asm/page.h>
38 #include <asm/pgtable.h>
39 #include <asm/hardirq.h>
40 #include <asm/io.h>
41 #include <asm/prom.h>
42 #include <asm/smp.h>
43 #include <asm/naca.h>
44 #include <asm/paca.h>
45 #include <asm/iSeries/LparData.h>
46 #include <asm/iSeries/HvCall.h>
47 #include <asm/iSeries/HvCallCfg.h>
48 #include <asm/time.h>
49 #include <asm/ppcdebug.h>
50 #include "open_pic.h"
51 #include <asm/machdep.h>
52 #include <asm/xics.h>
53 #include <asm/cputable.h>
54 #include <asm/system.h>
55
56 int smp_threads_ready;
57 unsigned long cache_decay_ticks;
58
59 cpumask_t cpu_possible_map = CPU_MASK_NONE;
60 cpumask_t cpu_online_map = CPU_MASK_NONE;
61 cpumask_t cpu_available_map = CPU_MASK_NONE;
62 cpumask_t cpu_present_at_boot = CPU_MASK_NONE;
63
64 EXPORT_SYMBOL(cpu_online_map);
65 EXPORT_SYMBOL(cpu_possible_map);
66
67 struct smp_ops_t *smp_ops;
68
69 static volatile unsigned int cpu_callin_map[NR_CPUS];
70
71 extern unsigned char stab_array[];
72
73 extern int cpu_idle(void *unused);
74 void smp_call_function_interrupt(void);
75 extern long register_vpa(unsigned long flags, unsigned long proc,
76                          unsigned long vpa);
77
78 /* Low level assembly function used to backup CPU 0 state */
79 extern void __save_cpu_setup(void);
80
81 #ifdef CONFIG_PPC_ISERIES
82 static unsigned long iSeries_smp_message[NR_CPUS];
83
84 void iSeries_smp_message_recv( struct pt_regs * regs )
85 {
86         int cpu = smp_processor_id();
87         int msg;
88
89         if ( num_online_cpus() < 2 )
90                 return;
91
92         for ( msg = 0; msg < 4; ++msg )
93                 if ( test_and_clear_bit( msg, &iSeries_smp_message[cpu] ) )
94                         smp_message_recv( msg, regs );
95 }
96
97 static inline void smp_iSeries_do_message(int cpu, int msg)
98 {
99         set_bit(msg, &iSeries_smp_message[cpu]);
100         HvCall_sendIPI(&(paca[cpu]));
101 }
102
103 static void smp_iSeries_message_pass(int target, int msg)
104 {
105         int i;
106
107         if (target < NR_CPUS)
108                 smp_iSeries_do_message(target, msg);
109         else {
110                 for_each_online_cpu(i) {
111                         if (target == MSG_ALL_BUT_SELF
112                             && i == smp_processor_id())
113                                 continue;
114                         smp_iSeries_do_message(i, msg);
115                 }
116         }
117 }
118
119 static int smp_iSeries_numProcs(void)
120 {
121         unsigned np, i;
122         struct ItLpPaca * lpPaca;
123
124         np = 0;
125         for (i=0; i < NR_CPUS; ++i) {
126                 lpPaca = paca[i].xLpPacaPtr;
127                 if ( lpPaca->xDynProcStatus < 2 ) {
128                         cpu_set(i, cpu_available_map);
129                         cpu_set(i, cpu_possible_map);
130                         cpu_set(i, cpu_present_at_boot);
131                         ++np;
132                 }
133         }
134         return np;
135 }
136
137 static int smp_iSeries_probe(void)
138 {
139         unsigned i;
140         unsigned np = 0;
141         struct ItLpPaca *lpPaca;
142
143         for (i=0; i < NR_CPUS; ++i) {
144                 lpPaca = paca[i].xLpPacaPtr;
145                 if (lpPaca->xDynProcStatus < 2) {
146                         /*paca[i].active = 1;*/
147                         ++np;
148                 }
149         }
150
151         return np;
152 }
153
154 static void smp_iSeries_kick_cpu(int nr)
155 {
156         struct ItLpPaca *lpPaca;
157
158         BUG_ON(nr < 0 || nr >= NR_CPUS);
159
160         /* Verify that our partition has a processor nr */
161         lpPaca = paca[nr].xLpPacaPtr;
162         if (lpPaca->xDynProcStatus >= 2)
163                 return;
164
165         /* The processor is currently spinning, waiting
166          * for the xProcStart field to become non-zero
167          * After we set xProcStart, the processor will
168          * continue on to secondary_start in iSeries_head.S
169          */
170         paca[nr].xProcStart = 1;
171 }
172
173 static void __devinit smp_iSeries_setup_cpu(int nr)
174 {
175 }
176
177 static struct smp_ops_t iSeries_smp_ops = {
178         .message_pass = smp_iSeries_message_pass,
179         .probe        = smp_iSeries_probe,
180         .kick_cpu     = smp_iSeries_kick_cpu,
181         .setup_cpu    = smp_iSeries_setup_cpu,
182 };
183
184 /* This is called very early. */
185 void __init smp_init_iSeries(void)
186 {
187         smp_ops = &iSeries_smp_ops;
188         systemcfg->processorCount       = smp_iSeries_numProcs();
189 }
190 #endif
191
192 #ifdef CONFIG_PPC_PSERIES
193 void smp_openpic_message_pass(int target, int msg)
194 {
195         /* make sure we're sending something that translates to an IPI */
196         if ( msg > 0x3 ){
197                 printk("SMP %d: smp_message_pass: unknown msg %d\n",
198                        smp_processor_id(), msg);
199                 return;
200         }
201         switch ( target )
202         {
203         case MSG_ALL:
204                 openpic_cause_IPI(msg, 0xffffffff);
205                 break;
206         case MSG_ALL_BUT_SELF:
207                 openpic_cause_IPI(msg,
208                                   0xffffffff & ~(1 << smp_processor_id()));
209                 break;
210         default:
211                 openpic_cause_IPI(msg, 1<<target);
212                 break;
213         }
214 }
215
216 static int __init smp_openpic_probe(void)
217 {
218         int nr_cpus;
219
220         nr_cpus = cpus_weight(cpu_possible_map);
221
222         if (nr_cpus > 1)
223                 openpic_request_IPIs();
224
225         return nr_cpus;
226 }
227
228 static void __devinit smp_openpic_setup_cpu(int cpu)
229 {
230         do_openpic_setup_cpu();
231 }
232
233 #ifdef CONFIG_HOTPLUG_CPU
234 /* Get state of physical CPU.
235  * Return codes:
236  *      0       - The processor is in the RTAS stopped state
237  *      1       - stop-self is in progress
238  *      2       - The processor is not in the RTAS stopped state
239  *      -1      - Hardware Error
240  *      -2      - Hardware Busy, Try again later.
241  */
242 static int query_cpu_stopped(unsigned int pcpu)
243 {
244         long cpu_status;
245         int status, qcss_tok;
246
247         qcss_tok = rtas_token("query-cpu-stopped-state");
248         BUG_ON(qcss_tok == RTAS_UNKNOWN_SERVICE);
249         status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
250         if (status != 0) {
251                 printk(KERN_ERR
252                        "RTAS query-cpu-stopped-state failed: %i\n", status);
253                 return status;
254         }
255
256         return cpu_status;
257 }
258
259 int __cpu_disable(void)
260 {
261         /* FIXME: go put this in a header somewhere */
262         extern void xics_migrate_irqs_away(void);
263
264         systemcfg->processorCount--;
265
266         /*fix boot_cpuid here*/
267         if (smp_processor_id() == boot_cpuid)
268                 boot_cpuid = any_online_cpu(cpu_online_map);
269
270         /* FIXME: abstract this to not be platform specific later on */
271         xics_migrate_irqs_away();
272         return 0;
273 }
274
275 void __cpu_die(unsigned int cpu)
276 {
277         int tries;
278         int cpu_status;
279         unsigned int pcpu = get_hard_smp_processor_id(cpu);
280
281         for (tries = 0; tries < 5; tries++) {
282                 cpu_status = query_cpu_stopped(pcpu);
283
284                 if (cpu_status == 0)
285                         break;
286                 set_current_state(TASK_UNINTERRUPTIBLE);
287                 schedule_timeout(HZ);
288         }
289         if (cpu_status != 0) {
290                 printk("Querying DEAD? cpu %i (%i) shows %i\n",
291                        cpu, pcpu, cpu_status);
292         }
293
294         /* Isolation and deallocation are definatly done by
295          * drslot_chrp_cpu.  If they were not they would be
296          * done here.  Change isolate state to Isolate and
297          * change allocation-state to Unusable.
298          */
299         paca[cpu].xProcStart = 0;
300
301         /* So we can recognize if it fails to come up next time. */
302         cpu_callin_map[cpu] = 0;
303 }
304
305 /* Kill this cpu */
306 void cpu_die(void)
307 {
308         local_irq_disable();
309         rtas_stop_self();
310         /* Should never get here... */
311         BUG();
312         for(;;);
313 }
314
315 /* Search all cpu device nodes for an offline logical cpu.  If a
316  * device node has a "ibm,my-drc-index" property (meaning this is an
317  * LPAR), paranoid-check whether we own the cpu.  For each "thread"
318  * of a cpu, if it is offline and has the same hw index as before,
319  * grab that in preference.
320  */
321 static unsigned int find_physical_cpu_to_start(unsigned int old_hwindex)
322 {
323         struct device_node *np = NULL;
324         unsigned int best = -1U;
325
326         while ((np = of_find_node_by_type(np, "cpu"))) {
327                 int nr_threads, len;
328                 u32 *index = (u32 *)get_property(np, "ibm,my-drc-index", NULL);
329                 u32 *tid = (u32 *)
330                         get_property(np, "ibm,ppc-interrupt-server#s", &len);
331
332                 if (!tid)
333                         tid = (u32 *)get_property(np, "reg", &len);
334
335                 if (!tid)
336                         continue;
337
338                 /* If there is a drc-index, make sure that we own
339                  * the cpu.
340                  */
341                 if (index) {
342                         int state;
343                         int rc = rtas_get_sensor(9003, *index, &state);
344                         if (rc != 0 || state != 1)
345                                 continue;
346                 }
347
348                 nr_threads = len / sizeof(u32);
349
350                 while (nr_threads--) {
351                         if (0 == query_cpu_stopped(tid[nr_threads])) {
352                                 best = tid[nr_threads];
353                                 if (best == old_hwindex)
354                                         goto out;
355                         }
356                 }
357         }
358 out:
359         of_node_put(np);
360         return best;
361 }
362
363 /**
364  * smp_startup_cpu() - start the given cpu
365  *
366  * At boot time, there is nothing to do.  At run-time, call RTAS with
367  * the appropriate start location, if the cpu is in the RTAS stopped
368  * state.
369  *
370  * Returns:
371  *      0       - failure
372  *      1       - success
373  */
374 static inline int __devinit smp_startup_cpu(unsigned int lcpu)
375 {
376         int status;
377         extern void (*pseries_secondary_smp_init)(unsigned int cpu);
378         unsigned long start_here = __pa(pseries_secondary_smp_init);
379         unsigned int pcpu;
380
381         /* At boot time the cpus are already spinning in hold
382          * loops, so nothing to do. */
383         if (system_state == SYSTEM_BOOTING)
384                 return 1;
385
386         pcpu = find_physical_cpu_to_start(get_hard_smp_processor_id(lcpu));
387         if (pcpu == -1U) {
388                 printk(KERN_INFO "No more cpus available, failing\n");
389                 return 0;
390         }
391
392         /* Fixup atomic count: it exited inside IRQ handler. */
393         ((struct task_struct *)paca[lcpu].xCurrent)->thread_info->preempt_count
394                 = 0;
395         /* Fixup SLB round-robin so next segment (kernel) goes in segment 0 */
396         paca[lcpu].xStab_data.next_round_robin = 0;
397
398         /* At boot this is done in prom.c. */
399         paca[lcpu].xHwProcNum = pcpu;
400
401         status = rtas_call(rtas_token("start-cpu"), 3, 1, NULL,
402                            pcpu, start_here, lcpu);
403         if (status != 0) {
404                 printk(KERN_ERR "start-cpu failed: %i\n", status);
405                 return 0;
406         }
407         return 1;
408 }
409
410 static inline void look_for_more_cpus(void)
411 {
412         int num_addr_cell, num_size_cell, len, i, maxcpus;
413         struct device_node *np;
414         unsigned int *ireg;
415
416         /* Find the property which will tell us about how many CPUs
417          * we're allowed to have. */
418         if ((np = find_path_device("/rtas")) == NULL) {
419                 printk(KERN_ERR "Could not find /rtas in device tree!");
420                 return;
421         }
422         num_addr_cell = prom_n_addr_cells(np);
423         num_size_cell = prom_n_size_cells(np);
424
425         ireg = (unsigned int *)get_property(np, "ibm,lrdr-capacity", &len);
426         if (ireg == NULL) {
427                 /* FIXME: make sure not marked as lrdr_capable() */
428                 return;
429         }
430
431         maxcpus = ireg[num_addr_cell + num_size_cell];
432         /* DRENG need to account for threads here too */
433
434         if (maxcpus > NR_CPUS) {
435                 printk(KERN_WARNING
436                        "Partition configured for %d cpus, "
437                        "operating system maximum is %d.\n", maxcpus, NR_CPUS);
438                 maxcpus = NR_CPUS;
439         } else
440                 printk(KERN_INFO "Partition configured for %d cpus.\n",
441                        maxcpus);
442
443         /* Make those cpus (which might appear later) possible too. */
444         for (i = 0; i < maxcpus; i++)
445                 cpu_set(i, cpu_possible_map);
446 }
447 #else /* ... CONFIG_HOTPLUG_CPU */
448 static inline int __devinit smp_startup_cpu(unsigned int lcpu)
449 {
450         return 1;
451 }
452 static inline void look_for_more_cpus(void)
453 {
454 }
455 #endif /* CONFIG_HOTPLUG_CPU */
456
457 static void smp_pSeries_kick_cpu(int nr)
458 {
459         BUG_ON(nr < 0 || nr >= NR_CPUS);
460
461         if (!smp_startup_cpu(nr))
462                 return;
463
464         /* The processor is currently spinning, waiting
465          * for the xProcStart field to become non-zero
466          * After we set xProcStart, the processor will
467          * continue on to secondary_start
468          */
469         paca[nr].xProcStart = 1;
470 }
471 #endif /* CONFIG_PPC_PSERIES */
472
473 static void __init smp_space_timers(unsigned int max_cpus)
474 {
475         int i;
476         unsigned long offset = tb_ticks_per_jiffy / max_cpus;
477         unsigned long previous_tb = paca[boot_cpuid].next_jiffy_update_tb;
478
479         for_each_cpu(i) {
480                 if (i != boot_cpuid) {
481                         paca[i].next_jiffy_update_tb =
482                                 previous_tb + offset;
483                         previous_tb = paca[i].next_jiffy_update_tb;
484                 }
485         }
486 }
487
488 #ifdef CONFIG_PPC_PSERIES
489 void vpa_init(int cpu)
490 {
491         unsigned long flags;
492
493         /* Register the Virtual Processor Area (VPA) */
494         printk(KERN_INFO "register_vpa: cpu 0x%x\n", cpu);
495         flags = 1UL << (63 - 18);
496         paca[cpu].xLpPaca.xSLBCount = 64; /* SLB restore highwater mark */
497         register_vpa(flags, cpu, __pa((unsigned long)&(paca[cpu].xLpPaca))); 
498 }
499
500 static inline void smp_xics_do_message(int cpu, int msg)
501 {
502         set_bit(msg, &xics_ipi_message[cpu].value);
503         mb();
504         xics_cause_IPI(cpu);
505 }
506
507 static void smp_xics_message_pass(int target, int msg)
508 {
509         unsigned int i;
510
511         if (target < NR_CPUS) {
512                 smp_xics_do_message(target, msg);
513         } else {
514                 for_each_online_cpu(i) {
515                         if (target == MSG_ALL_BUT_SELF
516                             && i == smp_processor_id())
517                                 continue;
518                         smp_xics_do_message(i, msg);
519                 }
520         }
521 }
522
523 extern void xics_request_IPIs(void);
524
525 static int __init smp_xics_probe(void)
526 {
527 #ifdef CONFIG_SMP
528         xics_request_IPIs();
529 #endif
530
531         return cpus_weight(cpu_possible_map);
532 }
533
534 static void __devinit smp_xics_setup_cpu(int cpu)
535 {
536         if (cpu != boot_cpuid)
537                 xics_setup_cpu();
538 }
539
540 static spinlock_t timebase_lock = SPIN_LOCK_UNLOCKED;
541 static unsigned long timebase = 0;
542
543 static void __devinit pSeries_give_timebase(void)
544 {
545         spin_lock(&timebase_lock);
546         rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
547         timebase = get_tb();
548         spin_unlock(&timebase_lock);
549
550         while (timebase)
551                 barrier();
552         rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
553 }
554
555 static void __devinit pSeries_take_timebase(void)
556 {
557         while (!timebase)
558                 barrier();
559         spin_lock(&timebase_lock);
560         set_tb(timebase >> 32, timebase & 0xffffffff);
561         timebase = 0;
562         spin_unlock(&timebase_lock);
563 }
564
565 static struct smp_ops_t pSeries_openpic_smp_ops = {
566         .message_pass   = smp_openpic_message_pass,
567         .probe          = smp_openpic_probe,
568         .kick_cpu       = smp_pSeries_kick_cpu,
569         .setup_cpu      = smp_openpic_setup_cpu,
570 };
571
572 static struct smp_ops_t pSeries_xics_smp_ops = {
573         .message_pass   = smp_xics_message_pass,
574         .probe          = smp_xics_probe,
575         .kick_cpu       = smp_pSeries_kick_cpu,
576         .setup_cpu      = smp_xics_setup_cpu,
577 };
578
579 /* This is called very early */
580 void __init smp_init_pSeries(void)
581 {
582
583         if (naca->interrupt_controller == IC_OPEN_PIC)
584                 smp_ops = &pSeries_openpic_smp_ops;
585         else
586                 smp_ops = &pSeries_xics_smp_ops;
587
588         /* Non-lpar has additional take/give timebase */
589         if (systemcfg->platform == PLATFORM_PSERIES) {
590                 smp_ops->give_timebase = pSeries_give_timebase;
591                 smp_ops->take_timebase = pSeries_take_timebase;
592         }
593 }
594 #endif
595
596 void smp_local_timer_interrupt(struct pt_regs * regs)
597 {
598         if (!--(get_paca()->prof_counter)) {
599                 update_process_times(user_mode(regs));
600                 (get_paca()->prof_counter)=get_paca()->prof_multiplier;
601         }
602 }
603
604 void smp_message_recv(int msg, struct pt_regs *regs)
605 {
606         switch(msg) {
607         case PPC_MSG_CALL_FUNCTION:
608                 smp_call_function_interrupt();
609                 break;
610         case PPC_MSG_RESCHEDULE: 
611                 /* XXX Do we have to do this? */
612                 set_need_resched();
613                 break;
614 #if 0
615         case PPC_MSG_MIGRATE_TASK:
616                 /* spare */
617                 break;
618 #endif
619 #ifdef CONFIG_DEBUGGER
620         case PPC_MSG_DEBUGGER_BREAK:
621                 debugger(regs);
622                 break;
623 #endif
624         default:
625                 printk("SMP %d: smp_message_recv(): unknown msg %d\n",
626                        smp_processor_id(), msg);
627                 break;
628         }
629 }
630
631 void smp_send_reschedule(int cpu)
632 {
633         smp_ops->message_pass(cpu, PPC_MSG_RESCHEDULE);
634 }
635
636 #ifdef CONFIG_DEBUGGER
637 void smp_send_debugger_break(int cpu)
638 {
639         smp_ops->message_pass(cpu, PPC_MSG_DEBUGGER_BREAK);
640 }
641 #endif
642
643 static void stop_this_cpu(void *dummy)
644 {
645         local_irq_disable();
646         while (1)
647                 ;
648 }
649
650 void smp_send_stop(void)
651 {
652         smp_call_function(stop_this_cpu, NULL, 1, 0);
653 }
654
655 /*
656  * Structure and data for smp_call_function(). This is designed to minimise
657  * static memory requirements. It also looks cleaner.
658  * Stolen from the i386 version.
659  */
660 static spinlock_t call_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
661
662 static struct call_data_struct {
663         void (*func) (void *info);
664         void *info;
665         atomic_t started;
666         atomic_t finished;
667         int wait;
668 } *call_data;
669
670 /* delay of at least 8 seconds on 1GHz cpu */
671 #define SMP_CALL_TIMEOUT (1UL << (30 + 3))
672
673 /*
674  * This function sends a 'generic call function' IPI to all other CPUs
675  * in the system.
676  *
677  * [SUMMARY] Run a function on all other CPUs.
678  * <func> The function to run. This must be fast and non-blocking.
679  * <info> An arbitrary pointer to pass to the function.
680  * <nonatomic> currently unused.
681  * <wait> If true, wait (atomically) until function has completed on other CPUs.
682  * [RETURNS] 0 on success, else a negative status code. Does not return until
683  * remote CPUs are nearly ready to execute <<func>> or are or have executed.
684  *
685  * You must not call this function with disabled interrupts or from a
686  * hardware interrupt handler or from a bottom half handler.
687  */
688 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
689                        int wait)
690
691         struct call_data_struct data;
692         int ret = -1, cpus;
693         unsigned long timeout;
694
695         data.func = func;
696         data.info = info;
697         atomic_set(&data.started, 0);
698         data.wait = wait;
699         if (wait)
700                 atomic_set(&data.finished, 0);
701
702         spin_lock(&call_lock);
703         /* Must grab online cpu count with preempt disabled, otherwise
704          * it can change. */
705         cpus = num_online_cpus() - 1;
706         if (!cpus) {
707                 ret = 0;
708                 goto out;
709         }
710
711         call_data = &data;
712         wmb();
713         /* Send a message to all other CPUs and wait for them to respond */
714         smp_ops->message_pass(MSG_ALL_BUT_SELF, PPC_MSG_CALL_FUNCTION);
715
716         /* Wait for response */
717         timeout = SMP_CALL_TIMEOUT;
718         while (atomic_read(&data.started) != cpus) {
719                 HMT_low();
720                 if (--timeout == 0) {
721                         printk("smp_call_function on cpu %d: other cpus not "
722                                "responding (%d)\n", smp_processor_id(),
723                                atomic_read(&data.started));
724                         debugger(0);
725                         goto out;
726                 }
727         }
728
729         if (wait) {
730                 timeout = SMP_CALL_TIMEOUT;
731                 while (atomic_read(&data.finished) != cpus) {
732                         HMT_low();
733                         if (--timeout == 0) {
734                                 printk("smp_call_function on cpu %d: other "
735                                        "cpus not finishing (%d/%d)\n",
736                                        smp_processor_id(),
737                                        atomic_read(&data.finished),
738                                        atomic_read(&data.started));
739                                 debugger(0);
740                                 goto out;
741                         }
742                 }
743         }
744
745         ret = 0;
746
747 out:
748         call_data = NULL;
749         HMT_medium();
750         spin_unlock(&call_lock);
751         return ret;
752 }
753
754 void smp_call_function_interrupt(void)
755 {
756         void (*func) (void *info);
757         void *info;
758         int wait;
759
760         /* call_data will be NULL if the sender timed out while
761          * waiting on us to receive the call.
762          */
763         if (!call_data)
764                 return;
765
766         func = call_data->func;
767         info = call_data->info;
768         wait = call_data->wait;
769
770         if (!wait)
771                 smp_mb__before_atomic_inc();
772
773         /*
774          * Notify initiating CPU that I've grabbed the data and am
775          * about to execute the function
776          */
777         atomic_inc(&call_data->started);
778         /*
779          * At this point the info structure may be out of scope unless wait==1
780          */
781         (*func)(info);
782         if (wait) {
783                 smp_mb__before_atomic_inc();
784                 atomic_inc(&call_data->finished);
785         }
786 }
787
788 extern unsigned long decr_overclock;
789 extern struct gettimeofday_struct do_gtod;
790
791 struct thread_info *current_set[NR_CPUS];
792
793 DECLARE_PER_CPU(unsigned int, pvr);
794
795 static void __devinit smp_store_cpu_info(int id)
796 {
797         per_cpu(pvr, id) = _get_PVR();
798 }
799
800 static void __init smp_create_idle(unsigned int cpu)
801 {
802         struct pt_regs regs;
803         struct task_struct *p;
804
805         /* create a process for the processor */
806         /* only regs.msr is actually used, and 0 is OK for it */
807         memset(&regs, 0, sizeof(struct pt_regs));
808         p = copy_process(CLONE_VM | CLONE_IDLETASK,
809                          0, &regs, 0, NULL, NULL);
810         if (IS_ERR(p))
811                 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
812
813         wake_up_forked_process(p);
814         init_idle(p, cpu);
815         unhash_process(p);
816
817         paca[cpu].xCurrent = (u64)p;
818         current_set[cpu] = p->thread_info;
819 }
820
821 void __init smp_prepare_cpus(unsigned int max_cpus)
822 {
823         unsigned int cpu;
824
825         /* 
826          * setup_cpu may need to be called on the boot cpu. We havent
827          * spun any cpus up but lets be paranoid.
828          */
829         BUG_ON(boot_cpuid != smp_processor_id());
830
831         /* Fixup boot cpu */
832         smp_store_cpu_info(boot_cpuid);
833         cpu_callin_map[boot_cpuid] = 1;
834         paca[boot_cpuid].prof_counter = 1;
835         paca[boot_cpuid].prof_multiplier = 1;
836
837         /*
838          * XXX very rough. 
839          */
840         cache_decay_ticks = HZ/100;
841
842 #ifndef CONFIG_PPC_ISERIES
843         paca[boot_cpuid].next_jiffy_update_tb = tb_last_stamp = get_tb();
844
845         /*
846          * Should update do_gtod.stamp_xsec.
847          * For now we leave it which means the time can be some
848          * number of msecs off until someone does a settimeofday()
849          */
850         do_gtod.tb_orig_stamp = tb_last_stamp;
851
852         look_for_more_cpus();
853 #endif
854
855         max_cpus = smp_ops->probe();
856  
857         /* Backup CPU 0 state if necessary */
858         __save_cpu_setup();
859
860         smp_space_timers(max_cpus);
861
862         for_each_cpu(cpu)
863                 if (cpu != boot_cpuid)
864                         smp_create_idle(cpu);
865 }
866
867 void __devinit smp_prepare_boot_cpu(void)
868 {
869         BUG_ON(smp_processor_id() != boot_cpuid);
870
871         /* cpu_possible is set up in prom.c */
872         cpu_set(boot_cpuid, cpu_online_map);
873
874         paca[boot_cpuid].xCurrent = (u64)current;
875         current_set[boot_cpuid] = current->thread_info;
876 }
877
878 int __devinit __cpu_up(unsigned int cpu)
879 {
880         int c;
881
882         /* At boot, don't bother with non-present cpus -JSCHOPP */
883         if (system_state == SYSTEM_BOOTING && !cpu_present_at_boot(cpu))
884                 return -ENOENT;
885
886         paca[cpu].prof_counter = 1;
887         paca[cpu].prof_multiplier = 1;
888         paca[cpu].default_decr = tb_ticks_per_jiffy / decr_overclock;
889
890         if (!(cur_cpu_spec->cpu_features & CPU_FTR_SLB)) {
891                 void *tmp;
892
893                 /* maximum of 48 CPUs on machines with a segment table */
894                 if (cpu >= 48)
895                         BUG();
896
897                 tmp = &stab_array[PAGE_SIZE * cpu];
898                 memset(tmp, 0, PAGE_SIZE); 
899                 paca[cpu].xStab_data.virt = (unsigned long)tmp;
900                 paca[cpu].xStab_data.real = virt_to_abs(tmp);
901         }
902
903         /* The information for processor bringup must
904          * be written out to main store before we release
905          * the processor.
906          */
907         mb();
908
909         /* wake up cpus */
910         smp_ops->kick_cpu(cpu);
911
912         /*
913          * wait to see if the cpu made a callin (is actually up).
914          * use this value that I found through experimentation.
915          * -- Cort
916          */
917         for (c = 5000; c && !cpu_callin_map[cpu]; c--)
918                 udelay(100);
919
920         if (!cpu_callin_map[cpu]) {
921                 printk("Processor %u is stuck.\n", cpu);
922                 return -ENOENT;
923         }
924
925         printk("Processor %u found.\n", cpu);
926
927         if (smp_ops->give_timebase)
928                 smp_ops->give_timebase();
929         cpu_set(cpu, cpu_online_map);
930         return 0;
931 }
932
933 extern unsigned int default_distrib_server;
934 /* Activate a secondary processor. */
935 int __devinit start_secondary(void *unused)
936 {
937         unsigned int cpu = smp_processor_id();
938
939         atomic_inc(&init_mm.mm_count);
940         current->active_mm = &init_mm;
941
942         smp_store_cpu_info(cpu);
943         set_dec(paca[cpu].default_decr);
944         cpu_callin_map[cpu] = 1;
945
946         smp_ops->setup_cpu(cpu);
947         if (smp_ops->take_timebase)
948                 smp_ops->take_timebase();
949
950         get_paca()->yielded = 0;
951
952 #ifdef CONFIG_PPC_PSERIES
953         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
954                 vpa_init(cpu); 
955         }
956
957 #ifdef CONFIG_IRQ_ALL_CPUS
958         /* Put the calling processor into the GIQ.  This is really only
959          * necessary from a secondary thread as the OF start-cpu interface
960          * performs this function for us on primary threads.
961          */
962         /* TODO: 9005 is #defined in rtas-proc.c -- move to a header */
963         rtas_set_indicator(9005, default_distrib_server, 1);
964 #endif
965 #endif
966
967         local_irq_enable();
968
969         return cpu_idle(NULL);
970 }
971
972 int setup_profiling_timer(unsigned int multiplier)
973 {
974         return 0;
975 }
976
977 void __init smp_cpus_done(unsigned int max_cpus)
978 {
979         cpumask_t old_mask;
980
981         /* We want the setup_cpu() here to be called from CPU 0, but our
982          * init thread may have been "borrowed" by another CPU in the meantime
983          * se we pin us down to CPU 0 for a short while
984          */
985         old_mask = current->cpus_allowed;
986         set_cpus_allowed(current, cpumask_of_cpu(boot_cpuid));
987         
988         smp_ops->setup_cpu(boot_cpuid);
989
990         /* XXX fix this, xics currently relies on it - Anton */
991         smp_threads_ready = 1;
992
993         set_cpus_allowed(current, old_mask);
994 }