2 * SMP boot-related support
4 * Copyright (C) 1998-2003 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().
15 #define __KERNEL_SYSCALLS__
17 #include <linux/config.h>
19 #include <linux/acpi.h>
20 #include <linux/bootmem.h>
21 #include <linux/delay.h>
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/irq.h>
25 #include <linux/kernel.h>
26 #include <linux/kernel_stat.h>
28 #include <linux/smp.h>
29 #include <linux/smp_lock.h>
30 #include <linux/spinlock.h>
31 #include <linux/efi.h>
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>
40 #include <asm/machvec.h>
43 #include <asm/pgalloc.h>
44 #include <asm/pgtable.h>
45 #include <asm/processor.h>
46 #include <asm/ptrace.h>
48 #include <asm/system.h>
49 #include <asm/unistd.h>
54 #define Dprintk(x...) printk(x)
61 * ITC synchronization related stuff:
64 #define SLAVE (SMP_CACHE_BYTES/8)
66 #define NUM_ROUNDS 64 /* magic value */
67 #define NUM_ITERS 5 /* likewise */
69 static spinlock_t itc_sync_lock = SPIN_LOCK_UNLOCKED;
70 static volatile unsigned long go[SLAVE + 1];
72 #define DEBUG_ITC_SYNC 0
74 extern void __init calibrate_delay (void);
75 extern void start_ap (void);
76 extern unsigned long ia64_iobase;
79 task_t *task_for_booting_cpu;
81 /* Bitmask of currently online CPUs */
82 volatile unsigned long cpu_online_map;
83 unsigned long phys_cpu_present_map;
85 /* which logical CPU number maps to which CPU (physical APIC ID) */
86 volatile int ia64_cpu_to_sapicid[NR_CPUS];
88 static volatile unsigned long cpu_callin_map;
90 struct smp_boot_data smp_boot_data __initdata;
92 unsigned long ap_wakeup_vector = -1; /* External Int use to wakeup APs */
94 char __initdata no_int_routing;
96 unsigned char smp_int_redirect; /* are INT and IPI redirectable by the chipset? */
99 nointroute (char *str)
105 __setup("nointroute", nointroute);
108 sync_master (void *arg)
110 unsigned long flags, i;
114 local_irq_save(flags);
116 for (i = 0; i < NUM_ROUNDS*NUM_ITERS; ++i) {
119 go[SLAVE] = ia64_get_itc();
122 local_irq_restore(flags);
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.
131 get_delta (long *rt, long *master)
133 unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;
134 unsigned long tcenter, t0, t1, tm;
137 for (i = 0; i < NUM_ITERS; ++i) {
140 while (!(tm = go[SLAVE]));
144 if (t1 - t0 < best_t1 - best_t0)
145 best_t0 = t0, best_t1 = t1, best_tm = tm;
148 *rt = best_t1 - best_t0;
149 *master = best_tm - best_t0;
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)
155 return tcenter - best_tm;
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
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).
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.
191 ia64_sync_itc (unsigned int master)
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);
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 */
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);
212 while (go[MASTER]); /* wait for master to be ready */
214 spin_lock_irqsave(&itc_sync_lock, flags);
216 for (i = 0; i < NUM_ROUNDS; ++i) {
217 delta = get_delta(&rt, &master_time_stamp);
219 done = 1; /* let's lock on to this... */
225 adjust_latency += -delta;
226 adj = -delta + adjust_latency/4;
230 ia64_set_itc(ia64_get_itc() + adj);
234 t[i].master = master_time_stamp;
236 t[i].lat = adjust_latency/4;
240 spin_unlock_irqrestore(&itc_sync_lock, flags);
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);
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);
252 * Check whether we sync'd the itc ahead of the next timer interrupt. If so, just
255 if (time_after(ia64_get_itc(), local_cpu_data->itm_next)) {
256 Dprintk("CPU %d: oops, jumped a timer tick; resetting timer.\n",
258 ia64_cpu_local_tick();
263 * Ideally sets up per-cpu profiling hooks. Doesn't do much now...
265 static inline void __init
266 smp_setup_percpu_timer (void)
268 local_cpu_data->prof_counter = 1;
269 local_cpu_data->prof_multiplier = 1;
276 extern void ia64_init_itm(void);
278 #ifdef CONFIG_PERFMON
279 extern void pfm_init_percpu(void);
282 cpuid = smp_processor_id();
283 phys_id = hard_smp_processor_id();
285 if (test_and_set_bit(cpuid, &cpu_online_map)) {
286 printk(KERN_ERR "huh, phys CPU#0x%x, CPU#0x%x already present??\n",
291 smp_setup_percpu_timer();
299 * Set I/O port base per CPU
301 ia64_set_kr(IA64_KR_IO_BASE, __pa(ia64_iobase));
303 #ifdef CONFIG_IA64_MCA
304 ia64_mca_cmc_vector_setup(); /* Setup vector on AP & enable */
305 ia64_mca_check_errors(); /* For post-failure MCA error logging */
308 #ifdef CONFIG_PERFMON
314 local_cpu_data->loops_per_jiffy = loops_per_jiffy;
316 if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
318 * Synchronize the ITC with the BP. Need to do this after irqs are
319 * enabled because ia64_sync_itc() calls smp_call_function_single(), which
320 * calls spin_unlock_bh(), which calls spin_unlock_bh(), which calls
321 * local_bh_enable(), which bugs out if irqs are not enabled...
323 Dprintk("Going to syncup ITC with BP.\n");
328 * Allow the master to continue.
330 set_bit(cpuid, &cpu_callin_map);
331 Dprintk("Stack on CPU %d at about %p\n",cpuid, &cpuid);
336 * Activate a secondary processor. head.S calls this.
339 start_secondary (void *unused)
341 extern int cpu_idle (void);
343 Dprintk("start_secondary: starting CPU 0x%x\n", hard_smp_processor_id());
351 static struct task_struct * __init
355 * don't care about the eip and regs settings since we'll never reschedule the
358 return do_fork(CLONE_VM|CLONE_IDLETASK, 0, 0, 0, NULL, NULL);
362 do_boot_cpu (int sapicid, int cpu)
364 struct task_struct *idle;
368 * We can't use kernel_thread since we must avoid to reschedule the child.
370 idle = fork_by_hand();
372 panic("failed fork for CPU %d", cpu);
375 * We remove it from the pidhash and the runqueue
376 * once we got the process:
378 init_idle(idle, cpu);
380 unhash_process(idle);
382 task_for_booting_cpu = idle;
384 Dprintk("Sending wakeup vector %lu to AP 0x%x/0x%x.\n", ap_wakeup_vector, cpu, sapicid);
386 platform_send_ipi(cpu, ap_wakeup_vector, IA64_IPI_DM_INT, 0);
389 * Wait 10s total for the AP to start
391 Dprintk("Waiting on callin_map ...");
392 for (timeout = 0; timeout < 100000; timeout++) {
393 if (test_bit(cpu, &cpu_callin_map))
394 break; /* It has booted */
399 if (test_bit(cpu, &cpu_callin_map)) {
400 /* number CPUs logically, starting from 1 (BSP is 0) */
401 printk(KERN_INFO "CPU%d: CPU has booted.\n", cpu);
403 printk(KERN_ERR "Processor 0x%x/0x%x is stuck.\n", cpu, sapicid);
404 ia64_cpu_to_sapicid[cpu] = -1;
405 clear_bit(cpu, &cpu_online_map); /* was set in smp_callin() */
415 get_option (&str, &ticks);
416 cache_decay_ticks = ticks;
420 __setup("decay=", decay);
423 * # of ticks an idle task is considered cache-hot. Highly application-dependent. There
424 * are apps out there which are known to suffer significantly with values >= 4.
426 unsigned long cache_decay_ticks = 10; /* equal to MIN_TIMESLICE */
429 smp_tune_scheduling (void)
431 printk(KERN_INFO "task migration cache decay timeout: %ld msecs.\n",
432 (cache_decay_ticks + 1) * 1000 / HZ);
436 * Initialize the logical CPU number to SAPICID mapping
439 smp_build_cpu_map (void)
442 int boot_cpu_id = hard_smp_processor_id();
444 for (cpu = 0; cpu < NR_CPUS; cpu++)
445 ia64_cpu_to_sapicid[cpu] = -1;
447 ia64_cpu_to_sapicid[0] = boot_cpu_id;
448 phys_cpu_present_map = 1;
450 for (cpu = 1, i = 0; i < smp_boot_data.cpu_count; i++) {
451 sapicid = smp_boot_data.cpu_phys_id[i];
452 if (sapicid == -1 || sapicid == boot_cpu_id)
454 phys_cpu_present_map |= (1 << cpu);
455 ia64_cpu_to_sapicid[cpu] = sapicid;
462 /* on which node is each logical CPU (one cacheline even for 64 CPUs) */
463 volatile char cpu_to_node_map[NR_CPUS] __cacheline_aligned;
464 /* which logical CPUs are on which nodes */
465 volatile unsigned long node_to_cpu_mask[MAX_NUMNODES] __cacheline_aligned;
468 * Build cpu to node mapping and initialize the per node cpu masks.
471 build_cpu_to_node_map (void)
475 for(node=0; node<MAX_NUMNODES; node++)
476 node_to_cpu_mask[node] = 0;
477 for(cpu = 0; cpu < NR_CPUS; ++cpu) {
479 * All Itanium NUMA platforms I know use ACPI, so maybe we
480 * can drop this ifdef completely. [EF]
482 #ifdef CONFIG_ACPI_NUMA
484 for (i = 0; i < NR_CPUS; ++i)
485 if (cpu_physical_id(cpu) == node_cpuid[i].phys_id) {
486 node = node_cpuid[i].nid;
490 # error Fixme: Dunno how to build CPU-to-node map.
492 cpu_to_node_map[cpu] = node;
494 node_to_cpu_mask[node] |= (1UL << cpu);
498 #endif /* CONFIG_NUMA */
501 * Cycle through the APs sending Wakeup IPIs to boot each.
504 smp_prepare_cpus (unsigned int max_cpus)
506 int boot_cpu_id = hard_smp_processor_id();
509 * Initialize the per-CPU profiling counter/multiplier
512 smp_setup_percpu_timer();
515 * We have the boot CPU online for sure.
517 set_bit(0, &cpu_online_map);
518 set_bit(0, &cpu_callin_map);
520 local_cpu_data->loops_per_jiffy = loops_per_jiffy;
521 ia64_cpu_to_sapicid[0] = boot_cpu_id;
523 printk(KERN_INFO "Boot processor id 0x%x/0x%x\n", 0, boot_cpu_id);
525 current_thread_info()->cpu = 0;
526 smp_tune_scheduling();
529 * If SMP should be disabled, then really disable it!
532 printk(KERN_INFO "SMP mode deactivated.\n");
533 cpu_online_map = phys_cpu_present_map = 1;
538 void __devinit smp_prepare_boot_cpu(void)
540 set_bit(smp_processor_id(), &cpu_online_map);
541 set_bit(smp_processor_id(), &cpu_callin_map);
545 smp_cpus_done (unsigned int dummy)
548 unsigned long bogosum = 0;
551 * Allow the user to impress friends.
554 for (cpu = 0; cpu < NR_CPUS; cpu++)
556 bogosum += cpu_data(cpu)->loops_per_jiffy;
558 printk(KERN_INFO "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
559 num_online_cpus(), bogosum/(500000/HZ), (bogosum/(5000/HZ))%100);
563 __cpu_up (unsigned int cpu)
568 sapicid = ia64_cpu_to_sapicid[cpu];
572 printk(KERN_INFO "Processor %d/%d is spinning up...\n", sapicid, cpu);
574 /* Processor goes to start_secondary(), sets online flag */
575 ret = do_boot_cpu(sapicid, cpu);
579 printk(KERN_INFO "Processor %d has spun up...\n", cpu);
584 * Assume that CPU's have been discovered by some platform-dependent interface. For
585 * SoftSDV/Lion, that would be ACPI.
587 * Setup of the IPI irq handler is done in irq.c:init_IRQ_SMP().
590 init_smp_config(void)
598 /* Tell SAL where to drop the AP's. */
599 ap_startup = (struct fptr *) start_ap;
600 sal_ret = ia64_sal_set_vectors(SAL_VECTOR_OS_BOOT_RENDEZ,
601 __pa(ap_startup->fp), __pa(ap_startup->gp), 0, 0, 0, 0);
603 printk(KERN_ERR "SMP: Can't set SAL AP Boot Rendezvous: %s\n",
604 ia64_sal_strerror(sal_ret));