2 * SMP boot-related support
4 * Copyright (C) 1998-2003, 2005 Hewlett-Packard Co
5 * David Mosberger-Tang <davidm@hpl.hp.com>
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 * 04/06/21 Ashok Raj <ashok.raj@intel.com> Added CPU Hotplug Support
14 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/acpi.h>
18 #include <linux/bootmem.h>
19 #include <linux/cpu.h>
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/kernel.h>
25 #include <linux/kernel_stat.h>
27 #include <linux/notifier.h>
28 #include <linux/smp.h>
29 #include <linux/smp_lock.h>
30 #include <linux/spinlock.h>
31 #include <linux/efi.h>
32 #include <linux/percpu.h>
33 #include <linux/bitops.h>
35 #include <asm/atomic.h>
36 #include <asm/cache.h>
37 #include <asm/current.h>
38 #include <asm/delay.h>
42 #include <asm/machvec.h>
45 #include <asm/pgalloc.h>
46 #include <asm/pgtable.h>
47 #include <asm/processor.h>
48 #include <asm/ptrace.h>
50 #include <asm/system.h>
51 #include <asm/tlbflush.h>
52 #include <asm/unistd.h>
57 #define Dprintk(x...) printk(x)
62 #ifdef CONFIG_HOTPLUG_CPU
64 * Store all idle threads, this can be reused instead of creating
65 * a new thread. Also avoids complicated thread destroy functionality
68 struct task_struct *idle_thread_array[NR_CPUS];
71 * Global array allocated for NR_CPUS at boot time
73 struct sal_to_os_boot sal_boot_rendez_state[NR_CPUS];
76 * start_ap in head.S uses this to store current booting cpu
79 struct sal_to_os_boot *sal_state_for_booting_cpu = &sal_boot_rendez_state[0];
81 #define set_brendez_area(x) (sal_state_for_booting_cpu = &sal_boot_rendez_state[(x)]);
83 #define get_idle_for_cpu(x) (idle_thread_array[(x)])
84 #define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
88 #define get_idle_for_cpu(x) (NULL)
89 #define set_idle_for_cpu(x,p)
90 #define set_brendez_area(x)
95 * ITC synchronization related stuff:
98 #define SLAVE (SMP_CACHE_BYTES/8)
100 #define NUM_ROUNDS 64 /* magic value */
101 #define NUM_ITERS 5 /* likewise */
103 static DEFINE_SPINLOCK(itc_sync_lock);
104 static volatile unsigned long go[SLAVE + 1];
106 #define DEBUG_ITC_SYNC 0
108 extern void __devinit calibrate_delay (void);
109 extern void start_ap (void);
110 extern unsigned long ia64_iobase;
112 task_t *task_for_booting_cpu;
117 DEFINE_PER_CPU(int, cpu_state);
119 /* Bitmasks of currently online, and possible CPUs */
120 cpumask_t cpu_online_map;
121 EXPORT_SYMBOL(cpu_online_map);
122 cpumask_t cpu_possible_map;
123 EXPORT_SYMBOL(cpu_possible_map);
125 /* which logical CPU number maps to which CPU (physical APIC ID) */
126 volatile int ia64_cpu_to_sapicid[NR_CPUS];
127 EXPORT_SYMBOL(ia64_cpu_to_sapicid);
129 static volatile cpumask_t cpu_callin_map;
131 struct smp_boot_data smp_boot_data __initdata;
133 unsigned long ap_wakeup_vector = -1; /* External Int use to wakeup APs */
135 char __initdata no_int_routing;
137 unsigned char smp_int_redirect; /* are INT and IPI redirectable by the chipset? */
140 nointroute (char *str)
143 printk ("no_int_routing on\n");
147 __setup("nointroute", nointroute);
150 sync_master (void *arg)
152 unsigned long flags, i;
156 local_irq_save(flags);
158 for (i = 0; i < NUM_ROUNDS*NUM_ITERS; ++i) {
162 go[SLAVE] = ia64_get_itc();
165 local_irq_restore(flags);
169 * Return the number of cycles by which our itc differs from the itc on the master
170 * (time-keeper) CPU. A positive number indicates our itc is ahead of the master,
171 * negative that it is behind.
174 get_delta (long *rt, long *master)
176 unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;
177 unsigned long tcenter, t0, t1, tm;
180 for (i = 0; i < NUM_ITERS; ++i) {
183 while (!(tm = go[SLAVE]))
188 if (t1 - t0 < best_t1 - best_t0)
189 best_t0 = t0, best_t1 = t1, best_tm = tm;
192 *rt = best_t1 - best_t0;
193 *master = best_tm - best_t0;
195 /* average best_t0 and best_t1 without overflow: */
196 tcenter = (best_t0/2 + best_t1/2);
197 if (best_t0 % 2 + best_t1 % 2 == 2)
199 return tcenter - best_tm;
203 * Synchronize ar.itc of the current (slave) CPU with the ar.itc of the MASTER CPU
204 * (normally the time-keeper CPU). We use a closed loop to eliminate the possibility of
205 * unaccounted-for errors (such as getting a machine check in the middle of a calibration
206 * step). The basic idea is for the slave to ask the master what itc value it has and to
207 * read its own itc before and after the master responds. Each iteration gives us three
221 * The goal is to adjust the slave's ar.itc such that tm falls exactly half-way between t0
222 * and t1. If we achieve this, the clocks are synchronized provided the interconnect
223 * between the slave and the master is symmetric. Even if the interconnect were
224 * asymmetric, we would still know that the synchronization error is smaller than the
225 * roundtrip latency (t0 - t1).
227 * When the interconnect is quiet and symmetric, this lets us synchronize the itc to
228 * within one or two cycles. However, we can only *guarantee* that the synchronization is
229 * accurate to within a round-trip time, which is typically in the range of several
230 * hundred cycles (e.g., ~500 cycles). In practice, this means that the itc's are usually
231 * almost perfectly synchronized, but we shouldn't assume that the accuracy is much better
232 * than half a micro second or so.
235 ia64_sync_itc (unsigned int master)
237 long i, delta, adj, adjust_latency = 0, done = 0;
238 unsigned long flags, rt, master_time_stamp, bound;
241 long rt; /* roundtrip time */
242 long master; /* master's timestamp */
243 long diff; /* difference between midpoint and master's timestamp */
244 long lat; /* estimate of itc adjustment latency */
249 * Make sure local timer ticks are disabled while we sync. If
250 * they were enabled, we'd have to worry about nasty issues
251 * like setting the ITC ahead of (or a long time before) the
252 * next scheduled tick.
254 BUG_ON((ia64_get_itv() & (1 << 16)) == 0);
258 if (smp_call_function_single(master, sync_master, NULL, 1, 0) < 0) {
259 printk(KERN_ERR "sync_itc: failed to get attention of CPU %u!\n", master);
264 cpu_relax(); /* wait for master to be ready */
266 spin_lock_irqsave(&itc_sync_lock, flags);
268 for (i = 0; i < NUM_ROUNDS; ++i) {
269 delta = get_delta(&rt, &master_time_stamp);
271 done = 1; /* let's lock on to this... */
277 adjust_latency += -delta;
278 adj = -delta + adjust_latency/4;
282 ia64_set_itc(ia64_get_itc() + adj);
286 t[i].master = master_time_stamp;
288 t[i].lat = adjust_latency/4;
292 spin_unlock_irqrestore(&itc_sync_lock, flags);
295 for (i = 0; i < NUM_ROUNDS; ++i)
296 printk("rt=%5ld master=%5ld diff=%5ld adjlat=%5ld\n",
297 t[i].rt, t[i].master, t[i].diff, t[i].lat);
300 printk(KERN_INFO "CPU %d: synchronized ITC with CPU %u (last diff %ld cycles, "
301 "maxerr %lu cycles)\n", smp_processor_id(), master, delta, rt);
305 * Ideally sets up per-cpu profiling hooks. Doesn't do much now...
307 static inline void __devinit
308 smp_setup_percpu_timer (void)
312 static void __devinit
316 extern void ia64_init_itm(void);
318 #ifdef CONFIG_PERFMON
319 extern void pfm_init_percpu(void);
322 cpuid = smp_processor_id();
323 phys_id = hard_smp_processor_id();
325 if (cpu_online(cpuid)) {
326 printk(KERN_ERR "huh, phys CPU#0x%x, CPU#0x%x already present??\n",
332 cpu_set(cpuid, cpu_online_map);
333 unlock_ipi_calllock();
335 smp_setup_percpu_timer();
337 ia64_mca_cmc_vector_setup(); /* Setup vector on AP */
339 #ifdef CONFIG_PERFMON
345 if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
347 * Synchronize the ITC with the BP. Need to do this after irqs are
348 * enabled because ia64_sync_itc() calls smp_call_function_single(), which
349 * calls spin_unlock_bh(), which calls spin_unlock_bh(), which calls
350 * local_bh_enable(), which bugs out if irqs are not enabled...
352 Dprintk("Going to syncup ITC with BP.\n");
361 local_cpu_data->loops_per_jiffy = loops_per_jiffy;
363 #ifdef CONFIG_IA32_SUPPORT
368 * Allow the master to continue.
370 cpu_set(cpuid, cpu_callin_map);
371 Dprintk("Stack on CPU %d at about %p\n",cpuid, &cpuid);
376 * Activate a secondary processor. head.S calls this.
379 start_secondary (void *unused)
381 /* Early console may use I/O ports */
382 ia64_set_kr(IA64_KR_IO_BASE, __pa(ia64_iobase));
383 Dprintk("start_secondary: starting CPU 0x%x\n", hard_smp_processor_id());
392 struct pt_regs * __devinit idle_regs(struct pt_regs *regs)
398 struct task_struct *idle;
399 struct completion done;
404 do_fork_idle(void *_c_idle)
406 struct create_idle *c_idle = _c_idle;
408 c_idle->idle = fork_idle(c_idle->cpu);
409 complete(&c_idle->done);
413 do_boot_cpu (int sapicid, int cpu)
416 struct create_idle c_idle = {
418 .done = COMPLETION_INITIALIZER(c_idle.done),
420 DECLARE_WORK(work, do_fork_idle, &c_idle);
422 c_idle.idle = get_idle_for_cpu(cpu);
424 init_idle(c_idle.idle, cpu);
429 * We can't use kernel_thread since we must avoid to reschedule the child.
431 if (!keventd_up() || current_is_keventd())
432 work.func(work.data);
434 schedule_work(&work);
435 wait_for_completion(&c_idle.done);
438 if (IS_ERR(c_idle.idle))
439 panic("failed fork for CPU %d", cpu);
441 set_idle_for_cpu(cpu, c_idle.idle);
444 task_for_booting_cpu = c_idle.idle;
446 Dprintk("Sending wakeup vector %lu to AP 0x%x/0x%x.\n", ap_wakeup_vector, cpu, sapicid);
448 set_brendez_area(cpu);
449 platform_send_ipi(cpu, ap_wakeup_vector, IA64_IPI_DM_INT, 0);
452 * Wait 10s total for the AP to start
454 Dprintk("Waiting on callin_map ...");
455 for (timeout = 0; timeout < 100000; timeout++) {
456 if (cpu_isset(cpu, cpu_callin_map))
457 break; /* It has booted */
462 if (!cpu_isset(cpu, cpu_callin_map)) {
463 printk(KERN_ERR "Processor 0x%x/0x%x is stuck.\n", cpu, sapicid);
464 ia64_cpu_to_sapicid[cpu] = -1;
465 cpu_clear(cpu, cpu_online_map); /* was set in smp_callin() */
475 get_option (&str, &ticks);
479 __setup("decay=", decay);
482 * Initialize the logical CPU number to SAPICID mapping
485 smp_build_cpu_map (void)
488 int boot_cpu_id = hard_smp_processor_id();
490 for (cpu = 0; cpu < NR_CPUS; cpu++) {
491 ia64_cpu_to_sapicid[cpu] = -1;
492 #ifdef CONFIG_HOTPLUG_CPU
493 cpu_set(cpu, cpu_possible_map);
497 ia64_cpu_to_sapicid[0] = boot_cpu_id;
498 cpus_clear(cpu_present_map);
499 cpu_set(0, cpu_present_map);
500 cpu_set(0, cpu_possible_map);
501 for (cpu = 1, i = 0; i < smp_boot_data.cpu_count; i++) {
502 sapicid = smp_boot_data.cpu_phys_id[i];
503 if (sapicid == boot_cpu_id)
505 cpu_set(cpu, cpu_present_map);
506 cpu_set(cpu, cpu_possible_map);
507 ia64_cpu_to_sapicid[cpu] = sapicid;
514 /* on which node is each logical CPU (one cacheline even for 64 CPUs) */
515 u8 cpu_to_node_map[NR_CPUS] __cacheline_aligned;
516 EXPORT_SYMBOL(cpu_to_node_map);
517 /* which logical CPUs are on which nodes */
518 cpumask_t node_to_cpu_mask[MAX_NUMNODES] __cacheline_aligned;
521 * Build cpu to node mapping and initialize the per node cpu masks.
524 build_cpu_to_node_map (void)
528 for(node=0; node<MAX_NUMNODES; node++)
529 cpus_clear(node_to_cpu_mask[node]);
530 for(cpu = 0; cpu < NR_CPUS; ++cpu) {
532 * All Itanium NUMA platforms I know use ACPI, so maybe we
533 * can drop this ifdef completely. [EF]
535 #ifdef CONFIG_ACPI_NUMA
537 for (i = 0; i < NR_CPUS; ++i)
538 if (cpu_physical_id(cpu) == node_cpuid[i].phys_id) {
539 node = node_cpuid[i].nid;
543 # error Fixme: Dunno how to build CPU-to-node map.
545 cpu_to_node_map[cpu] = (node >= 0) ? node : 0;
547 cpu_set(cpu, node_to_cpu_mask[node]);
551 #endif /* CONFIG_NUMA */
554 * Cycle through the APs sending Wakeup IPIs to boot each.
557 smp_prepare_cpus (unsigned int max_cpus)
559 int boot_cpu_id = hard_smp_processor_id();
562 * Initialize the per-CPU profiling counter/multiplier
565 smp_setup_percpu_timer();
568 * We have the boot CPU online for sure.
570 cpu_set(0, cpu_online_map);
571 cpu_set(0, cpu_callin_map);
573 local_cpu_data->loops_per_jiffy = loops_per_jiffy;
574 ia64_cpu_to_sapicid[0] = boot_cpu_id;
576 printk(KERN_INFO "Boot processor id 0x%x/0x%x\n", 0, boot_cpu_id);
578 current_thread_info()->cpu = 0;
581 * If SMP should be disabled, then really disable it!
584 printk(KERN_INFO "SMP mode deactivated.\n");
585 cpus_clear(cpu_online_map);
586 cpus_clear(cpu_present_map);
587 cpus_clear(cpu_possible_map);
588 cpu_set(0, cpu_online_map);
589 cpu_set(0, cpu_present_map);
590 cpu_set(0, cpu_possible_map);
595 void __devinit smp_prepare_boot_cpu(void)
597 cpu_set(smp_processor_id(), cpu_online_map);
598 cpu_set(smp_processor_id(), cpu_callin_map);
601 #ifdef CONFIG_HOTPLUG_CPU
602 extern void fixup_irqs(void);
603 /* must be called with cpucontrol mutex held */
604 int __cpu_disable(void)
606 int cpu = smp_processor_id();
609 * dont permit boot processor for now
615 local_flush_tlb_all();
616 cpu_clear(cpu, cpu_callin_map);
620 void __cpu_die(unsigned int cpu)
624 for (i = 0; i < 100; i++) {
625 /* They ack this in play_dead by setting CPU_DEAD */
626 if (per_cpu(cpu_state, cpu) == CPU_DEAD)
628 printk ("CPU %d is now offline\n", cpu);
633 printk(KERN_ERR "CPU %u didn't die...\n", cpu);
635 #else /* !CONFIG_HOTPLUG_CPU */
636 int __cpu_disable(void)
641 void __cpu_die(unsigned int cpu)
643 /* We said "no" in __cpu_disable */
646 #endif /* CONFIG_HOTPLUG_CPU */
649 smp_cpus_done (unsigned int dummy)
652 unsigned long bogosum = 0;
655 * Allow the user to impress friends.
658 for (cpu = 0; cpu < NR_CPUS; cpu++)
660 bogosum += cpu_data(cpu)->loops_per_jiffy;
662 printk(KERN_INFO "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
663 (int)num_online_cpus(), bogosum/(500000/HZ), (bogosum/(5000/HZ))%100);
667 __cpu_up (unsigned int cpu)
672 sapicid = ia64_cpu_to_sapicid[cpu];
677 * Already booted cpu? not valid anymore since we dont
678 * do idle loop tightspin anymore.
680 if (cpu_isset(cpu, cpu_callin_map))
683 /* Processor goes to start_secondary(), sets online flag */
684 ret = do_boot_cpu(sapicid, cpu);
692 * Assume that CPU's have been discovered by some platform-dependent interface. For
693 * SoftSDV/Lion, that would be ACPI.
695 * Setup of the IPI irq handler is done in irq.c:init_IRQ_SMP().
698 init_smp_config(void)
706 /* Tell SAL where to drop the AP's. */
707 ap_startup = (struct fptr *) start_ap;
708 sal_ret = ia64_sal_set_vectors(SAL_VECTOR_OS_BOOT_RENDEZ,
709 ia64_tpa(ap_startup->fp), ia64_tpa(ap_startup->gp), 0, 0, 0, 0);
711 printk(KERN_ERR "SMP: Can't set SAL AP Boot Rendezvous: %s\n",
712 ia64_sal_strerror(sal_ret));