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