📄 smpboot.c
字号:
/* * SMP boot-related support * * Copyright (C) 1998-2003, 2005 Hewlett-Packard Co * David Mosberger-Tang <davidm@hpl.hp.com> * Copyright (C) 2001, 2004-2005 Intel Corp * Rohit Seth <rohit.seth@intel.com> * Suresh Siddha <suresh.b.siddha@intel.com> * Gordon Jin <gordon.jin@intel.com> * Ashok Raj <ashok.raj@intel.com> * * 01/05/16 Rohit Seth <rohit.seth@intel.com> Moved SMP booting functions from smp.c to here. * 01/04/27 David Mosberger <davidm@hpl.hp.com> Added ITC synching code. * 02/07/31 David Mosberger <davidm@hpl.hp.com> Switch over to hotplug-CPU boot-sequence. * smp_boot_cpus()/smp_commence() is replaced by * smp_prepare_cpus()/__cpu_up()/smp_cpus_done(). * 04/06/21 Ashok Raj <ashok.raj@intel.com> Added CPU Hotplug Support * 04/12/26 Jin Gordon <gordon.jin@intel.com> * 04/12/26 Rohit Seth <rohit.seth@intel.com> * Add multi-threading and multi-core detection * 05/01/30 Suresh Siddha <suresh.b.siddha@intel.com> * Setup cpu_sibling_map and cpu_core_map */#include <linux/config.h>#include <linux/module.h>#include <linux/acpi.h>#include <linux/bootmem.h>#include <linux/cpu.h>#include <linux/delay.h>#include <linux/init.h>#include <linux/interrupt.h>#include <linux/irq.h>#include <linux/kernel.h>#include <linux/kernel_stat.h>#include <linux/mm.h>#include <linux/notifier.h> /* hg add me */#include <linux/smp.h>#include <linux/smp_lock.h>#include <linux/spinlock.h>#include <linux/efi.h>#include <linux/percpu.h>#include <linux/bitops.h>#include <asm/atomic.h>#include <asm/cache.h>#include <asm/current.h>#include <asm/delay.h>#include <asm/ia32.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/machvec.h>#include <asm/mca.h>#include <asm/page.h>#include <asm/pgalloc.h>#include <asm/pgtable.h>#include <asm/processor.h>#include <asm/ptrace.h>#include <asm/sal.h>#include <asm/system.h>#include <asm/tlbflush.h>#include <asm/unistd.h>#ifdef XEN#include <xen/domain.h>#include <asm/hw_irq.h>#include <asm/vmx.h>#ifndef CONFIG_SMPcpumask_t cpu_online_map = CPU_MASK_CPU0;EXPORT_SYMBOL(cpu_online_map);#endif#endif#ifdef CONFIG_SMP /* ifdef XEN */#define SMP_DEBUG 0#if SMP_DEBUG#define Dprintk(x...) printk(x)#else#define Dprintk(x...)#endif#ifdef CONFIG_HOTPLUG_CPU/* * Store all idle threads, this can be reused instead of creating * a new thread. Also avoids complicated thread destroy functionality * for idle threads. */struct task_struct *idle_thread_array[NR_CPUS];/* * Global array allocated for NR_CPUS at boot time */struct sal_to_os_boot sal_boot_rendez_state[NR_CPUS];/* * start_ap in head.S uses this to store current booting cpu * info. */struct sal_to_os_boot *sal_state_for_booting_cpu = &sal_boot_rendez_state[0];#define set_brendez_area(x) (sal_state_for_booting_cpu = &sal_boot_rendez_state[(x)]);#define get_idle_for_cpu(x) (idle_thread_array[(x)])#define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))#else#define get_idle_for_cpu(x) (NULL)#define set_idle_for_cpu(x,p)#define set_brendez_area(x)#endif/* * ITC synchronization related stuff: */#define MASTER 0#define SLAVE (SMP_CACHE_BYTES/8)#define NUM_ROUNDS 64 /* magic value */#define NUM_ITERS 5 /* likewise */static DEFINE_SPINLOCK(itc_sync_lock);static volatile unsigned long go[SLAVE + 1];#define DEBUG_ITC_SYNC 0extern void __devinit calibrate_delay (void);extern void start_ap (void);extern unsigned long ia64_iobase;task_t *task_for_booting_cpu;/* * State for each CPU */DEFINE_PER_CPU(int, cpu_state);/* Bitmasks of currently online, and possible CPUs */cpumask_t cpu_online_map;EXPORT_SYMBOL(cpu_online_map);cpumask_t cpu_possible_map;EXPORT_SYMBOL(cpu_possible_map);cpumask_t cpu_core_map[NR_CPUS] __cacheline_aligned;cpumask_t cpu_sibling_map[NR_CPUS] __cacheline_aligned;int smp_num_siblings = 1;int smp_num_cpucores = 1;/* which logical CPU number maps to which CPU (physical APIC ID) */volatile int ia64_cpu_to_sapicid[NR_CPUS];EXPORT_SYMBOL(ia64_cpu_to_sapicid);static volatile cpumask_t cpu_callin_map;struct smp_boot_data smp_boot_data __initdata;unsigned long ap_wakeup_vector = -1; /* External Int use to wakeup APs */char __initdata no_int_routing;unsigned char smp_int_redirect; /* are INT and IPI redirectable by the chipset? */static int __initnointroute (char *str){ no_int_routing = 1; printk ("no_int_routing on\n"); return 1;}__setup("nointroute", nointroute);static void fix_b0_for_bsp(void){#ifdef CONFIG_HOTPLUG_CPU int cpuid; static int fix_bsp_b0 = 1; cpuid = smp_processor_id(); /* * Cache the b0 value on the first AP that comes up */ if (!(fix_bsp_b0 && cpuid)) return; sal_boot_rendez_state[0].br[0] = sal_boot_rendez_state[cpuid].br[0]; printk ("Fixed BSP b0 value from CPU %d\n", cpuid); fix_bsp_b0 = 0;#endif}voidsync_master (void *arg){ unsigned long flags, i; go[MASTER] = 0; local_irq_save(flags); { for (i = 0; i < NUM_ROUNDS*NUM_ITERS; ++i) { while (!go[MASTER]) cpu_relax(); go[MASTER] = 0; go[SLAVE] = ia64_get_itc(); } } local_irq_restore(flags);}/* * Return the number of cycles by which our itc differs from the itc on the master * (time-keeper) CPU. A positive number indicates our itc is ahead of the master, * negative that it is behind. */static inline long#ifdef XEN /* warning cleanup */get_delta (unsigned long *rt, unsigned long *master)#elseget_delta (long *rt, long *master)#endif{ unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0; unsigned long tcenter, t0, t1, tm; long i; for (i = 0; i < NUM_ITERS; ++i) { t0 = ia64_get_itc(); go[MASTER] = 1; while (!(tm = go[SLAVE])) cpu_relax(); go[SLAVE] = 0; t1 = ia64_get_itc(); if (t1 - t0 < best_t1 - best_t0) best_t0 = t0, best_t1 = t1, best_tm = tm; } *rt = best_t1 - best_t0; *master = best_tm - best_t0; /* average best_t0 and best_t1 without overflow: */ tcenter = (best_t0/2 + best_t1/2); if (best_t0 % 2 + best_t1 % 2 == 2) ++tcenter; return tcenter - best_tm;}/* * Synchronize ar.itc of the current (slave) CPU with the ar.itc of the MASTER CPU * (normally the time-keeper CPU). We use a closed loop to eliminate the possibility of * unaccounted-for errors (such as getting a machine check in the middle of a calibration * step). The basic idea is for the slave to ask the master what itc value it has and to * read its own itc before and after the master responds. Each iteration gives us three * timestamps: * * slave master * * t0 ---\ * ---\ * ---> * tm * /--- * /--- * t1 <--- * * * The goal is to adjust the slave's ar.itc such that tm falls exactly half-way between t0 * and t1. If we achieve this, the clocks are synchronized provided the interconnect * between the slave and the master is symmetric. Even if the interconnect were * asymmetric, we would still know that the synchronization error is smaller than the * roundtrip latency (t0 - t1). * * When the interconnect is quiet and symmetric, this lets us synchronize the itc to * within one or two cycles. However, we can only *guarantee* that the synchronization is * accurate to within a round-trip time, which is typically in the range of several * hundred cycles (e.g., ~500 cycles). In practice, this means that the itc's are usually * almost perfectly synchronized, but we shouldn't assume that the accuracy is much better * than half a micro second or so. */voidia64_sync_itc (unsigned int master){ long i, delta, adj, adjust_latency = 0, done = 0; unsigned long flags, rt, master_time_stamp, bound;#if DEBUG_ITC_SYNC struct { long rt; /* roundtrip time */ long master; /* master's timestamp */ long diff; /* difference between midpoint and master's timestamp */ long lat; /* estimate of itc adjustment latency */ } t[NUM_ROUNDS];#endif /* * Make sure local timer ticks are disabled while we sync. If * they were enabled, we'd have to worry about nasty issues * like setting the ITC ahead of (or a long time before) the * next scheduled tick. */ BUG_ON((ia64_get_itv() & (1 << 16)) == 0); go[MASTER] = 1; if (smp_call_function_single(master, sync_master, NULL, 1, 0) < 0) { printk(KERN_ERR "sync_itc: failed to get attention of CPU %u!\n", master); return; } while (go[MASTER]) cpu_relax(); /* wait for master to be ready */ spin_lock_irqsave(&itc_sync_lock, flags); { for (i = 0; i < NUM_ROUNDS; ++i) { delta = get_delta(&rt, &master_time_stamp); if (delta == 0) { done = 1; /* let's lock on to this... */ bound = rt; } if (!done) { if (i > 0) { adjust_latency += -delta; adj = -delta + adjust_latency/4; } else adj = -delta; ia64_set_itc(ia64_get_itc() + adj); }#if DEBUG_ITC_SYNC t[i].rt = rt; t[i].master = master_time_stamp; t[i].diff = delta; t[i].lat = adjust_latency/4;#endif } } spin_unlock_irqrestore(&itc_sync_lock, flags);#if DEBUG_ITC_SYNC for (i = 0; i < NUM_ROUNDS; ++i) printk("rt=%5ld master=%5ld diff=%5ld adjlat=%5ld\n", t[i].rt, t[i].master, t[i].diff, t[i].lat);#endif printk(KERN_INFO "CPU %d: synchronized ITC with CPU %u (last diff %ld cycles, " "maxerr %lu cycles)\n", smp_processor_id(), master, delta, rt);}/* * Ideally sets up per-cpu profiling hooks. Doesn't do much now... */static inline void __devinitsmp_setup_percpu_timer (void){}static void __devinitsmp_callin (void){ int cpuid, phys_id; extern void ia64_init_itm(void);#ifdef CONFIG_PERFMON extern void pfm_init_percpu(void);#endif cpuid = smp_processor_id(); phys_id = hard_smp_processor_id(); if (cpu_online(cpuid)) { printk(KERN_ERR "huh, phys CPU#0x%x, CPU#0x%x already present??\n", phys_id, cpuid); BUG(); } fix_b0_for_bsp(); lock_ipi_calllock(); cpu_set(cpuid, cpu_online_map); unlock_ipi_calllock(); per_cpu(cpu_state, cpuid) = CPU_ONLINE; smp_setup_percpu_timer(); ia64_mca_cmc_vector_setup(); /* Setup vector on AP */#ifdef CONFIG_PERFMON pfm_init_percpu();#endif local_irq_enable(); if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) { /* * Synchronize the ITC with the BP. Need to do this after irqs are * enabled because ia64_sync_itc() calls smp_call_function_single(), which * calls spin_unlock_bh(), which calls spin_unlock_bh(), which calls * local_bh_enable(), which bugs out if irqs are not enabled... */ Dprintk("Going to syncup ITC with BP.\n"); ia64_sync_itc(0); } /* * Get our bogomips. */ ia64_init_itm();#ifndef XEN calibrate_delay();#endif local_cpu_data->loops_per_jiffy = loops_per_jiffy;#ifdef CONFIG_IA32_SUPPORT ia32_gdt_init();#endif /* * Allow the master to continue. */ cpu_set(cpuid, cpu_callin_map); Dprintk("Stack on CPU %d at about %p\n",cpuid, &cpuid);}/* * Activate a secondary processor. head.S calls this. */int __devinitstart_secondary (void *unused){ /* Early console may use I/O ports */ ia64_set_kr(IA64_KR_IO_BASE, __pa(ia64_iobase)); Dprintk("start_secondary: starting CPU 0x%x\n", hard_smp_processor_id());#ifndef XEN efi_map_pal_code();#endif cpu_init(); smp_callin();#ifdef XEN if (vmx_enabled) vmx_init_env(0, 0); startup_cpu_idle_loop();#else cpu_idle();#endif return 0;}struct pt_regs * __devinit idle_regs(struct pt_regs *regs){ return NULL;}#ifndef XENstruct create_idle { struct task_struct *idle; struct completion done; int cpu;};voiddo_fork_idle(void *_c_idle){ struct create_idle *c_idle = _c_idle; c_idle->idle = fork_idle(c_idle->cpu); complete(&c_idle->done);}#endifstatic int __devinitdo_boot_cpu (int sapicid, int cpu){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -