ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ia64 / kernel / smpboot.c
1 /*
2  * SMP boot-related support
3  *
4  * Copyright (C) 1998-2003 Hewlett-Packard Co
5  *      David Mosberger-Tang <davidm@hpl.hp.com>
6  *
7  * 01/05/16 Rohit Seth <rohit.seth@intel.com>   Moved SMP booting functions from smp.c to here.
8  * 01/04/27 David Mosberger <davidm@hpl.hp.com> Added ITC synching code.
9  * 02/07/31 David Mosberger <davidm@hpl.hp.com> Switch over to hotplug-CPU boot-sequence.
10  *                                              smp_boot_cpus()/smp_commence() is replaced by
11  *                                              smp_prepare_cpus()/__cpu_up()/smp_cpus_done().
12  */
13 #include <linux/config.h>
14
15 #include <linux/module.h>
16 #include <linux/acpi.h>
17 #include <linux/bootmem.h>
18 #include <linux/delay.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/kernel.h>
23 #include <linux/kernel_stat.h>
24 #include <linux/mm.h>
25 #include <linux/smp.h>
26 #include <linux/smp_lock.h>
27 #include <linux/spinlock.h>
28 #include <linux/efi.h>
29
30 #include <asm/atomic.h>
31 #include <asm/bitops.h>
32 #include <asm/cache.h>
33 #include <asm/current.h>
34 #include <asm/delay.h>
35 #include <asm/ia32.h>
36 #include <asm/io.h>
37 #include <asm/irq.h>
38 #include <asm/machvec.h>
39 #include <asm/mca.h>
40 #include <asm/page.h>
41 #include <asm/pgalloc.h>
42 #include <asm/pgtable.h>
43 #include <asm/processor.h>
44 #include <asm/ptrace.h>
45 #include <asm/sal.h>
46 #include <asm/system.h>
47 #include <asm/unistd.h>
48
49 #define SMP_DEBUG 0
50
51 #if SMP_DEBUG
52 #define Dprintk(x...)  printk(x)
53 #else
54 #define Dprintk(x...)
55 #endif
56
57
58 /*
59  * ITC synchronization related stuff:
60  */
61 #define MASTER  0
62 #define SLAVE   (SMP_CACHE_BYTES/8)
63
64 #define NUM_ROUNDS      64      /* magic value */
65 #define NUM_ITERS       5       /* likewise */
66
67 static spinlock_t itc_sync_lock = SPIN_LOCK_UNLOCKED;
68 static volatile unsigned long go[SLAVE + 1];
69
70 #define DEBUG_ITC_SYNC  0
71
72 extern void __init calibrate_delay (void);
73 extern void start_ap (void);
74 extern unsigned long ia64_iobase;
75
76 task_t *task_for_booting_cpu;
77
78 /* Bitmask of currently online CPUs */
79 cpumask_t cpu_online_map;
80 EXPORT_SYMBOL(cpu_online_map);
81 cpumask_t phys_cpu_present_map;
82 EXPORT_SYMBOL(phys_cpu_present_map);
83
84 /* which logical CPU number maps to which CPU (physical APIC ID) */
85 volatile int ia64_cpu_to_sapicid[NR_CPUS];
86 EXPORT_SYMBOL(ia64_cpu_to_sapicid);
87
88 static volatile cpumask_t cpu_callin_map;
89
90 struct smp_boot_data smp_boot_data __initdata;
91
92 unsigned long ap_wakeup_vector = -1; /* External Int use to wakeup APs */
93
94 char __initdata no_int_routing;
95
96 unsigned char smp_int_redirect; /* are INT and IPI redirectable by the chipset? */
97
98 static int __init
99 nointroute (char *str)
100 {
101         no_int_routing = 1;
102         return 1;
103 }
104
105 __setup("nointroute", nointroute);
106
107 void
108 sync_master (void *arg)
109 {
110         unsigned long flags, i;
111
112         go[MASTER] = 0;
113
114         local_irq_save(flags);
115         {
116                 for (i = 0; i < NUM_ROUNDS*NUM_ITERS; ++i) {
117                         while (!go[MASTER]);
118                         go[MASTER] = 0;
119                         go[SLAVE] = ia64_get_itc();
120                 }
121         }
122         local_irq_restore(flags);
123 }
124
125 /*
126  * Return the number of cycles by which our itc differs from the itc on the master
127  * (time-keeper) CPU.  A positive number indicates our itc is ahead of the master,
128  * negative that it is behind.
129  */
130 static inline long
131 get_delta (long *rt, long *master)
132 {
133         unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;
134         unsigned long tcenter, t0, t1, tm;
135         long i;
136
137         for (i = 0; i < NUM_ITERS; ++i) {
138                 t0 = ia64_get_itc();
139                 go[MASTER] = 1;
140                 while (!(tm = go[SLAVE]));
141                 go[SLAVE] = 0;
142                 t1 = ia64_get_itc();
143
144                 if (t1 - t0 < best_t1 - best_t0)
145                         best_t0 = t0, best_t1 = t1, best_tm = tm;
146         }
147
148         *rt = best_t1 - best_t0;
149         *master = best_tm - best_t0;
150
151         /* average best_t0 and best_t1 without overflow: */
152         tcenter = (best_t0/2 + best_t1/2);
153         if (best_t0 % 2 + best_t1 % 2 == 2)
154                 ++tcenter;
155         return tcenter - best_tm;
156 }
157
158 /*
159  * Synchronize ar.itc of the current (slave) CPU with the ar.itc of the MASTER CPU
160  * (normally the time-keeper CPU).  We use a closed loop to eliminate the possibility of
161  * unaccounted-for errors (such as getting a machine check in the middle of a calibration
162  * step).  The basic idea is for the slave to ask the master what itc value it has and to
163  * read its own itc before and after the master responds.  Each iteration gives us three
164  * timestamps:
165  *
166  *      slave           master
167  *
168  *      t0 ---\
169  *             ---\
170  *                 --->
171  *                      tm
172  *                 /---
173  *             /---
174  *      t1 <---
175  *
176  *
177  * The goal is to adjust the slave's ar.itc such that tm falls exactly half-way between t0
178  * and t1.  If we achieve this, the clocks are synchronized provided the interconnect
179  * between the slave and the master is symmetric.  Even if the interconnect were
180  * asymmetric, we would still know that the synchronization error is smaller than the
181  * roundtrip latency (t0 - t1).
182  *
183  * When the interconnect is quiet and symmetric, this lets us synchronize the itc to
184  * within one or two cycles.  However, we can only *guarantee* that the synchronization is
185  * accurate to within a round-trip time, which is typically in the range of several
186  * hundred cycles (e.g., ~500 cycles).  In practice, this means that the itc's are usually
187  * almost perfectly synchronized, but we shouldn't assume that the accuracy is much better
188  * than half a micro second or so.
189  */
190 void
191 ia64_sync_itc (unsigned int master)
192 {
193         long i, delta, adj, adjust_latency = 0, done = 0;
194         unsigned long flags, rt, master_time_stamp, bound;
195         extern void ia64_cpu_local_tick (void);
196 #if DEBUG_ITC_SYNC
197         struct {
198                 long rt;        /* roundtrip time */
199                 long master;    /* master's timestamp */
200                 long diff;      /* difference between midpoint and master's timestamp */
201                 long lat;       /* estimate of itc adjustment latency */
202         } t[NUM_ROUNDS];
203 #endif
204
205         go[MASTER] = 1;
206
207         if (smp_call_function_single(master, sync_master, NULL, 1, 0) < 0) {
208                 printk(KERN_ERR "sync_itc: failed to get attention of CPU %u!\n", master);
209                 return;
210         }
211
212         while (go[MASTER]);     /* wait for master to be ready */
213
214         spin_lock_irqsave(&itc_sync_lock, flags);
215         {
216                 for (i = 0; i < NUM_ROUNDS; ++i) {
217                         delta = get_delta(&rt, &master_time_stamp);
218                         if (delta == 0) {
219                                 done = 1;       /* let's lock on to this... */
220                                 bound = rt;
221                         }
222
223                         if (!done) {
224                                 if (i > 0) {
225                                         adjust_latency += -delta;
226                                         adj = -delta + adjust_latency/4;
227                                 } else
228                                         adj = -delta;
229
230                                 ia64_set_itc(ia64_get_itc() + adj);
231                         }
232 #if DEBUG_ITC_SYNC
233                         t[i].rt = rt;
234                         t[i].master = master_time_stamp;
235                         t[i].diff = delta;
236                         t[i].lat = adjust_latency/4;
237 #endif
238                 }
239         }
240         spin_unlock_irqrestore(&itc_sync_lock, flags);
241
242 #if DEBUG_ITC_SYNC
243         for (i = 0; i < NUM_ROUNDS; ++i)
244                 printk("rt=%5ld master=%5ld diff=%5ld adjlat=%5ld\n",
245                        t[i].rt, t[i].master, t[i].diff, t[i].lat);
246 #endif
247
248         printk(KERN_INFO "CPU %d: synchronized ITC with CPU %u (last diff %ld cycles, "
249                "maxerr %lu cycles)\n", smp_processor_id(), master, delta, rt);
250
251         /*
252          * Check whether we sync'd the itc ahead of the next timer interrupt.  If so, just
253          * reset it.
254          */
255         if (time_after(ia64_get_itc(), local_cpu_data->itm_next)) {
256                 Dprintk("CPU %d: oops, jumped a timer tick; resetting timer.\n",
257                         smp_processor_id());
258                 ia64_cpu_local_tick();
259         }
260 }
261
262 /*
263  * Ideally sets up per-cpu profiling hooks.  Doesn't do much now...
264  */
265 static inline void __init
266 smp_setup_percpu_timer (void)
267 {
268 }
269
270 static void __init
271 smp_callin (void)
272 {
273         int cpuid, phys_id;
274         extern void ia64_init_itm(void);
275
276 #ifdef CONFIG_PERFMON
277         extern void pfm_init_percpu(void);
278 #endif
279
280         cpuid = smp_processor_id();
281         phys_id = hard_smp_processor_id();
282
283         if (cpu_test_and_set(cpuid, cpu_online_map)) {
284                 printk(KERN_ERR "huh, phys CPU#0x%x, CPU#0x%x already present??\n",
285                        phys_id, cpuid);
286                 BUG();
287         }
288
289         smp_setup_percpu_timer();
290
291         /*
292          * Get our bogomips.
293          */
294         ia64_init_itm();
295
296         /*
297          * Set I/O port base per CPU
298          */
299         ia64_set_kr(IA64_KR_IO_BASE, __pa(ia64_iobase));
300
301         ia64_mca_cmc_vector_setup();    /* Setup vector on AP & enable */
302
303 #ifdef CONFIG_PERFMON
304         pfm_init_percpu();
305 #endif
306
307         local_irq_enable();
308         calibrate_delay();
309         local_cpu_data->loops_per_jiffy = loops_per_jiffy;
310 #ifdef CONFIG_IA32_SUPPORT
311         ia32_gdt_init();
312 #endif
313
314         if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
315                 /*
316                  * Synchronize the ITC with the BP.  Need to do this after irqs are
317                  * enabled because ia64_sync_itc() calls smp_call_function_single(), which
318                  * calls spin_unlock_bh(), which calls spin_unlock_bh(), which calls
319                  * local_bh_enable(), which bugs out if irqs are not enabled...
320                  */
321                 Dprintk("Going to syncup ITC with BP.\n");
322                 ia64_sync_itc(0);
323         }
324
325         /*
326          * Allow the master to continue.
327          */
328         cpu_set(cpuid, cpu_callin_map);
329         Dprintk("Stack on CPU %d at about %p\n",cpuid, &cpuid);
330 }
331
332
333 /*
334  * Activate a secondary processor.  head.S calls this.
335  */
336 int __init
337 start_secondary (void *unused)
338 {
339         extern int cpu_idle (void);
340
341         Dprintk("start_secondary: starting CPU 0x%x\n", hard_smp_processor_id());
342         efi_map_pal_code();
343         cpu_init();
344         smp_callin();
345
346         return cpu_idle();
347 }
348
349 static struct task_struct * __init
350 fork_by_hand (void)
351 {
352         /*
353          * Don't care about the IP and regs settings since we'll never reschedule the
354          * forked task.
355          */
356         return copy_process(CLONE_VM|CLONE_IDLETASK, 0, 0, 0, NULL, NULL);
357 }
358
359 static int __init
360 do_boot_cpu (int sapicid, int cpu)
361 {
362         struct task_struct *idle;
363         int timeout;
364
365         /*
366          * We can't use kernel_thread since we must avoid to reschedule the child.
367          */
368         idle = fork_by_hand();
369         if (IS_ERR(idle))
370                 panic("failed fork for CPU %d", cpu);
371         wake_up_forked_process(idle);
372
373         /*
374          * We remove it from the pidhash and the runqueue
375          * once we got the process:
376          */
377         init_idle(idle, cpu);
378
379         unhash_process(idle);
380
381         task_for_booting_cpu = idle;
382
383         Dprintk("Sending wakeup vector %lu to AP 0x%x/0x%x.\n", ap_wakeup_vector, cpu, sapicid);
384
385         platform_send_ipi(cpu, ap_wakeup_vector, IA64_IPI_DM_INT, 0);
386
387         /*
388          * Wait 10s total for the AP to start
389          */
390         Dprintk("Waiting on callin_map ...");
391         for (timeout = 0; timeout < 100000; timeout++) {
392                 if (cpu_isset(cpu, cpu_callin_map))
393                         break;  /* It has booted */
394                 udelay(100);
395         }
396         Dprintk("\n");
397
398         if (!cpu_isset(cpu, cpu_callin_map)) {
399                 printk(KERN_ERR "Processor 0x%x/0x%x is stuck.\n", cpu, sapicid);
400                 ia64_cpu_to_sapicid[cpu] = -1;
401                 cpu_clear(cpu, cpu_online_map);  /* was set in smp_callin() */
402                 return -EINVAL;
403         }
404         return 0;
405 }
406
407 static int __init
408 decay (char *str)
409 {
410         int ticks;
411         get_option (&str, &ticks);
412         cache_decay_ticks = ticks;
413         return 1;
414 }
415
416 __setup("decay=", decay);
417
418 /*
419  * # of ticks an idle task is considered cache-hot.  Highly application-dependent.  There
420  * are apps out there which are known to suffer significantly with values >= 4.
421  */
422 unsigned long cache_decay_ticks = 10;   /* equal to MIN_TIMESLICE */
423
424 static void
425 smp_tune_scheduling (void)
426 {
427         printk(KERN_INFO "task migration cache decay timeout: %ld msecs.\n",
428                (cache_decay_ticks + 1) * 1000 / HZ);
429 }
430
431 /*
432  * Initialize the logical CPU number to SAPICID mapping
433  */
434 void __init
435 smp_build_cpu_map (void)
436 {
437         int sapicid, cpu, i;
438         int boot_cpu_id = hard_smp_processor_id();
439
440         for (cpu = 0; cpu < NR_CPUS; cpu++)
441                 ia64_cpu_to_sapicid[cpu] = -1;
442
443         ia64_cpu_to_sapicid[0] = boot_cpu_id;
444         cpus_clear(phys_cpu_present_map);
445         cpu_set(0, phys_cpu_present_map);
446
447         for (cpu = 1, i = 0; i < smp_boot_data.cpu_count; i++) {
448                 sapicid = smp_boot_data.cpu_phys_id[i];
449                 if (sapicid == boot_cpu_id)
450                         continue;
451                 cpu_set(cpu, phys_cpu_present_map);
452                 ia64_cpu_to_sapicid[cpu] = sapicid;
453                 cpu++;
454         }
455 }
456
457 #ifdef CONFIG_NUMA
458
459 /* on which node is each logical CPU (one cacheline even for 64 CPUs) */
460 u8 cpu_to_node_map[NR_CPUS] __cacheline_aligned;
461 EXPORT_SYMBOL(cpu_to_node_map);
462 /* which logical CPUs are on which nodes */
463 cpumask_t node_to_cpu_mask[MAX_NUMNODES] __cacheline_aligned;
464
465 /*
466  * Build cpu to node mapping and initialize the per node cpu masks.
467  */
468 void __init
469 build_cpu_to_node_map (void)
470 {
471         int cpu, i, node;
472
473         for(node=0; node<MAX_NUMNODES; node++)
474                 cpus_clear(node_to_cpu_mask[node]);
475         for(cpu = 0; cpu < NR_CPUS; ++cpu) {
476                 /*
477                  * All Itanium NUMA platforms I know use ACPI, so maybe we
478                  * can drop this ifdef completely.                    [EF]
479                  */
480 #ifdef CONFIG_ACPI_NUMA
481                 node = -1;
482                 for (i = 0; i < NR_CPUS; ++i)
483                         if (cpu_physical_id(cpu) == node_cpuid[i].phys_id) {
484                                 node = node_cpuid[i].nid;
485                                 break;
486                         }
487 #else
488 #               error Fixme: Dunno how to build CPU-to-node map.
489 #endif
490                 cpu_to_node_map[cpu] = node;
491                 if (node >= 0)
492                         cpu_set(cpu, node_to_cpu_mask[node]);
493         }
494 }
495
496 #endif /* CONFIG_NUMA */
497
498 /*
499  * Cycle through the APs sending Wakeup IPIs to boot each.
500  */
501 void __init
502 smp_prepare_cpus (unsigned int max_cpus)
503 {
504         int boot_cpu_id = hard_smp_processor_id();
505
506         /*
507          * Initialize the per-CPU profiling counter/multiplier
508          */
509
510         smp_setup_percpu_timer();
511
512         /*
513          * We have the boot CPU online for sure.
514          */
515         cpu_set(0, cpu_online_map);
516         cpu_set(0, cpu_callin_map);
517
518         local_cpu_data->loops_per_jiffy = loops_per_jiffy;
519         ia64_cpu_to_sapicid[0] = boot_cpu_id;
520
521         printk(KERN_INFO "Boot processor id 0x%x/0x%x\n", 0, boot_cpu_id);
522
523         current_thread_info()->cpu = 0;
524         smp_tune_scheduling();
525
526         /*
527          * If SMP should be disabled, then really disable it!
528          */
529         if (!max_cpus) {
530                 printk(KERN_INFO "SMP mode deactivated.\n");
531                 cpus_clear(cpu_online_map);
532                 cpus_clear(phys_cpu_present_map);
533                 cpu_set(0, cpu_online_map);
534                 cpu_set(0, phys_cpu_present_map);
535                 return;
536         }
537 }
538
539 void __devinit smp_prepare_boot_cpu(void)
540 {
541         cpu_set(smp_processor_id(), cpu_online_map);
542         cpu_set(smp_processor_id(), cpu_callin_map);
543 }
544
545 void
546 smp_cpus_done (unsigned int dummy)
547 {
548         int cpu;
549         unsigned long bogosum = 0;
550
551         /*
552          * Allow the user to impress friends.
553          */
554
555         for (cpu = 0; cpu < NR_CPUS; cpu++)
556                 if (cpu_online(cpu))
557                         bogosum += cpu_data(cpu)->loops_per_jiffy;
558
559         printk(KERN_INFO "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
560                (int)num_online_cpus(), bogosum/(500000/HZ), (bogosum/(5000/HZ))%100);
561 }
562
563 int __devinit
564 __cpu_up (unsigned int cpu)
565 {
566         int ret;
567         int sapicid;
568
569         sapicid = ia64_cpu_to_sapicid[cpu];
570         if (sapicid == -1)
571                 return -EINVAL;
572
573         /* Processor goes to start_secondary(), sets online flag */
574         ret = do_boot_cpu(sapicid, cpu);
575         if (ret < 0)
576                 return ret;
577
578         return 0;
579 }
580
581 /*
582  * Assume that CPU's have been discovered by some platform-dependent interface.  For
583  * SoftSDV/Lion, that would be ACPI.
584  *
585  * Setup of the IPI irq handler is done in irq.c:init_IRQ_SMP().
586  */
587 void __init
588 init_smp_config(void)
589 {
590         struct fptr {
591                 unsigned long fp;
592                 unsigned long gp;
593         } *ap_startup;
594         long sal_ret;
595
596         /* Tell SAL where to drop the AP's.  */
597         ap_startup = (struct fptr *) start_ap;
598         sal_ret = ia64_sal_set_vectors(SAL_VECTOR_OS_BOOT_RENDEZ,
599                                        ia64_tpa(ap_startup->fp), ia64_tpa(ap_startup->gp), 0, 0, 0, 0);
600         if (sal_ret < 0)
601                 printk(KERN_ERR "SMP: Can't set SAL AP Boot Rendezvous: %s\n",
602                        ia64_sal_strerror(sal_ret));
603 }