smpboot.c

来自「底层驱动开发」· C语言 代码 · 共 1,170 行 · 第 1/2 页

C
1,170
字号
/* *	x86 SMP booting functions * *	(c) 1995 Alan Cox, Building #3 <alan@redhat.com> *	(c) 1998, 1999, 2000 Ingo Molnar <mingo@redhat.com> *	Copyright 2001 Andi Kleen, SuSE Labs. * *	Much of the core SMP work is based on previous work by Thomas Radke, to *	whom a great many thanks are extended. * *	Thanks to Intel for making available several different Pentium, *	Pentium Pro and Pentium-II/Xeon MP machines. *	Original development of Linux SMP code supported by Caldera. * *	This code is released under the GNU General Public License version 2 * *	Fixes *		Felix Koop	:	NR_CPUS used properly *		Jose Renau	:	Handle single CPU case. *		Alan Cox	:	By repeated request 8) - Total BogoMIP report. *		Greg Wright	:	Fix for kernel stacks panic. *		Erich Boleyn	:	MP v1.4 and additional changes. *	Matthias Sattler	:	Changes for 2.1 kernel map. *	Michel Lespinasse	:	Changes for 2.1 kernel map. *	Michael Chastain	:	Change trampoline.S to gnu as. *		Alan Cox	:	Dumb bug: 'B' step PPro's are fine *		Ingo Molnar	:	Added APIC timers, based on code *					from Jose Renau *		Ingo Molnar	:	various cleanups and rewrites *		Tigran Aivazian	:	fixed "0.00 in /proc/uptime on SMP" bug. *	Maciej W. Rozycki	:	Bits for genuine 82489DX APICs *	Andi Kleen		:	Changed for SMP boot into long mode. *		Rusty Russell	:	Hacked into shape for new "hotplug" boot process. *      Andi Kleen              :       Converted to new state machine. *					Various cleanups. *					Probably mostly hotplug CPU ready now. *	Ashok Raj			: CPU hotplug support */#include <linux/config.h>#include <linux/init.h>#include <linux/mm.h>#include <linux/kernel_stat.h>#include <linux/smp_lock.h>#include <linux/bootmem.h>#include <linux/thread_info.h>#include <linux/module.h>#include <linux/delay.h>#include <linux/mc146818rtc.h>#include <asm/mtrr.h>#include <asm/pgalloc.h>#include <asm/desc.h>#include <asm/kdebug.h>#include <asm/tlbflush.h>#include <asm/proto.h>#include <asm/nmi.h>#include <asm/irq.h>#include <asm/hw_irq.h>/* Number of siblings per CPU package */int smp_num_siblings = 1;/* Package ID of each logical CPU */u8 phys_proc_id[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = BAD_APICID };u8 cpu_core_id[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = BAD_APICID };EXPORT_SYMBOL(phys_proc_id);EXPORT_SYMBOL(cpu_core_id);/* Bitmask of currently online CPUs */cpumask_t cpu_online_map __read_mostly;EXPORT_SYMBOL(cpu_online_map);/* * Private maps to synchronize booting between AP and BP. * Probably not needed anymore, but it makes for easier debugging. -AK */cpumask_t cpu_callin_map;cpumask_t cpu_callout_map;cpumask_t cpu_possible_map;EXPORT_SYMBOL(cpu_possible_map);/* Per CPU bogomips and other parameters */struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;/* Set when the idlers are all forked */int smp_threads_ready;cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;cpumask_t cpu_core_map[NR_CPUS] __read_mostly;EXPORT_SYMBOL(cpu_core_map);/* * Trampoline 80x86 program as an array. */extern unsigned char trampoline_data[];extern unsigned char trampoline_end[];/* State of each CPU */DEFINE_PER_CPU(int, cpu_state) = { 0 };/* * 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] __cpuinitdata ;#define get_idle_for_cpu(x)     (idle_thread_array[(x)])#define set_idle_for_cpu(x,p)   (idle_thread_array[(x)] = (p))/* * Currently trivial. Write the real->protected mode * bootstrap into the page concerned. The caller * has made sure it's suitably aligned. */static unsigned long __cpuinit setup_trampoline(void){	void *tramp = __va(SMP_TRAMPOLINE_BASE); 	memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);	return virt_to_phys(tramp);}/* * The bootstrap kernel entry code has set these up. Save them for * a given CPU */static void __cpuinit smp_store_cpu_info(int id){	struct cpuinfo_x86 *c = cpu_data + id;	*c = boot_cpu_data;	identify_cpu(c);	print_cpu_info(c);}/* * New Funky TSC sync algorithm borrowed from IA64. * Main advantage is that it doesn't reset the TSCs fully and * in general looks more robust and it works better than my earlier * attempts. I believe it was written by David Mosberger. Some minor * adjustments for x86-64 by me -AK * * Original comment reproduced below. * * Synchronize TSC of the current (slave) CPU with the TSC 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 TSC 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 TSC 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 TSCs * are usually almost perfectly synchronized, but we shouldn't assume * that the accuracy is much better than half a micro second or so. * * [there are other errors like the latency of RDTSC and of the * WRMSR. These can also account to hundreds of cycles. So it's * probably worse. It claims 153 cycles error on a dual Opteron, * but I suspect the numbers are actually somewhat worse -AK] */#define MASTER	0#define SLAVE	(SMP_CACHE_BYTES/8)/* Intentionally don't use cpu_relax() while TSC synchronization   because we don't want to go into funky power save modi or cause   hypervisors to schedule us away.  Going to sleep would likely affect   latency and low latency is the primary objective here. -AK */#define no_cpu_relax() barrier()static __cpuinitdata DEFINE_SPINLOCK(tsc_sync_lock);static volatile __cpuinitdata unsigned long go[SLAVE + 1];static int notscsync __cpuinitdata;#undef DEBUG_TSC_SYNC#define NUM_ROUNDS	64	/* magic value */#define NUM_ITERS	5	/* likewise *//* Callback on boot CPU */static __cpuinit void sync_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])				no_cpu_relax();			go[MASTER] = 0;			rdtscll(go[SLAVE]);		}	}	local_irq_restore(flags);}/* * Return the number of cycles by which our tsc differs from the tsc * on the master (time-keeper) CPU.  A positive number indicates our * tsc is ahead of the master, negative that it is behind. */static inline longget_delta(long *rt, long *master){	unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;	unsigned long tcenter, t0, t1, tm;	int i;	for (i = 0; i < NUM_ITERS; ++i) {		rdtscll(t0);		go[MASTER] = 1;		while (!(tm = go[SLAVE]))			no_cpu_relax();		go[SLAVE] = 0;		rdtscll(t1);		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;}static __cpuinit void sync_tsc(unsigned int master){	int i, done = 0;	long delta, adj, adjust_latency = 0;	unsigned long flags, rt, master_time_stamp, bound;#ifdef DEBUG_TSC_SYNC	static struct syncdebug {		long rt;	/* roundtrip time */		long master;	/* master's timestamp */		long diff;	/* difference between midpoint and master's timestamp */		long lat;	/* estimate of tsc adjustment latency */	} t[NUM_ROUNDS] __cpuinitdata;#endif	printk(KERN_INFO "CPU %d: Syncing TSC to CPU %u.\n",		smp_processor_id(), master);	go[MASTER] = 1;	/* It is dangerous to broadcast IPI as cpus are coming up,	 * as they may not be ready to accept them.  So since	 * we only need to send the ipi to the boot cpu direct	 * the message, and avoid the race.	 */	smp_call_function_single(master, sync_master, NULL, 1, 0);	while (go[MASTER])	/* wait for master to be ready */		no_cpu_relax();	spin_lock_irqsave(&tsc_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) {				unsigned long t;				if (i > 0) {					adjust_latency += -delta;					adj = -delta + adjust_latency/4;				} else					adj = -delta;				rdtscll(t);				wrmsrl(MSR_IA32_TSC, t + adj);			}#ifdef DEBUG_TSC_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(&tsc_sync_lock, flags);#ifdef DEBUG_TSC_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 TSC with CPU %u (last diff %ld cycles, "	       "maxerr %lu cycles)\n",	       smp_processor_id(), master, delta, rt);}static void __cpuinit tsc_sync_wait(void){	if (notscsync || !cpu_has_tsc)		return;	sync_tsc(0);}static __init int notscsync_setup(char *s){	notscsync = 1;	return 0;}__setup("notscsync", notscsync_setup);static atomic_t init_deasserted __cpuinitdata;/* * Report back to the Boot Processor. * Running on AP. */void __cpuinit smp_callin(void){	int cpuid, phys_id;	unsigned long timeout;	/*	 * If waken up by an INIT in an 82489DX configuration	 * we may get here before an INIT-deassert IPI reaches	 * our local APIC.  We have to wait for the IPI or we'll	 * lock up on an APIC access.	 */	while (!atomic_read(&init_deasserted))		cpu_relax();	/*	 * (This works even if the APIC is not enabled.)	 */	phys_id = GET_APIC_ID(apic_read(APIC_ID));	cpuid = smp_processor_id();	if (cpu_isset(cpuid, cpu_callin_map)) {		panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",					phys_id, cpuid);	}	Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);	/*	 * STARTUP IPIs are fragile beasts as they might sometimes	 * trigger some glue motherboard logic. Complete APIC bus	 * silence for 1 second, this overestimates the time the	 * boot CPU is spending to send the up to 2 STARTUP IPIs	 * by a factor of two. This should be enough.	 */	/*	 * Waiting 2s total for startup (udelay is not yet working)	 */	timeout = jiffies + 2*HZ;	while (time_before(jiffies, timeout)) {		/*		 * Has the boot CPU finished it's STARTUP sequence?		 */		if (cpu_isset(cpuid, cpu_callout_map))			break;		cpu_relax();	}	if (!time_before(jiffies, timeout)) {		panic("smp_callin: CPU%d started up but did not get a callout!\n",			cpuid);	}	/*	 * the boot CPU has finished the init stage and is spinning	 * on callin_map until we finish. We are free to set up this	 * CPU, first the APIC. (this is probably redundant on most	 * boards)	 */	Dprintk("CALLIN, before setup_local_APIC().\n");	setup_local_APIC();	/*	 * Get our bogomips. 	 * 	 * Need to enable IRQs because it can take longer and then	 * the NMI watchdog might kill us.	 */	local_irq_enable();	calibrate_delay();	local_irq_disable();	Dprintk("Stack at about %p\n",&cpuid);	disable_APIC_timer();	/*	 * Save our processor parameters	 */ 	smp_store_cpu_info(cpuid);	/*	 * Allow the master to continue.	 */	cpu_set(cpuid, cpu_callin_map);}static inline void set_cpu_sibling_map(int cpu){	int i;	if (smp_num_siblings > 1) {		for_each_cpu(i) {			if (cpu_core_id[cpu] == cpu_core_id[i]) {				cpu_set(i, cpu_sibling_map[cpu]);				cpu_set(cpu, cpu_sibling_map[i]);			}		}	} else {		cpu_set(cpu, cpu_sibling_map[cpu]);	}	if (current_cpu_data.x86_num_cores > 1) {		for_each_cpu(i) {			if (phys_proc_id[cpu] == phys_proc_id[i]) {				cpu_set(i, cpu_core_map[cpu]);				cpu_set(cpu, cpu_core_map[i]);			}		}	} else {		cpu_core_map[cpu] = cpu_sibling_map[cpu];	}}/* * Setup code on secondary processor (after comming out of the trampoline) */void __cpuinit start_secondary(void){	/*	 * Dont put anything before smp_callin(), SMP	 * booting is too fragile that we want to limit the	 * things done here to the most necessary things.	 */	cpu_init();	smp_callin();	/* otherwise gcc will move up the smp_processor_id before the cpu_init */	barrier();	Dprintk("cpu %d: setting up apic clock\n", smp_processor_id()); 		setup_secondary_APIC_clock();	Dprintk("cpu %d: enabling apic timer\n", smp_processor_id());	if (nmi_watchdog == NMI_IO_APIC) {		disable_8259A_irq(0);		enable_NMI_through_LVT0(NULL);		enable_8259A_irq(0);	}	enable_APIC_timer();	/*	 * The sibling maps must be set before turing the online map on for	 * this cpu	 */	set_cpu_sibling_map(smp_processor_id());	/*   	 * Wait for TSC sync to not schedule things before.	 * We still process interrupts, which could see an inconsistent	 * time in that window unfortunately. 	 * Do this here because TSC sync has global unprotected state. 	 */	tsc_sync_wait();	/*	 * We need to hold call_lock, so there is no inconsistency	 * between the time smp_call_function() determines number of	 * IPI receipients, and the time when the determination is made	 * for which cpus receive the IPI in genapic_flat.c. Holding this	 * lock helps us to not include this cpu in a currently in progress	 * smp_call_function().	 */	lock_ipi_call_lock();	/*	 * Allow the master to continue.	 */	cpu_set(smp_processor_id(), cpu_online_map);	per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;	unlock_ipi_call_lock();	cpu_idle();}extern volatile unsigned long init_rsp;extern void (*initial_code)(void);#ifdef APIC_DEBUGstatic void inquire_remote_apic(int apicid){	unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };	char *names[] = { "ID", "VERSION", "SPIV" };	int timeout, status;	printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);	for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {		printk("... APIC #%d %s: ", apicid, names[i]);		/*		 * Wait for idle.		 */		apic_wait_icr_idle();		apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));		apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]);		timeout = 0;		do {			udelay(100);			status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;		} while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);		switch (status) {		case APIC_ICR_RR_VALID:			status = apic_read(APIC_RRR);			printk("%08x\n", status);			break;		default:			printk("failed\n");		}	}}#endif/* * Kick the secondary to wake up. */static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip){	unsigned long send_status = 0, accept_status = 0;	int maxlvt, timeout, num_starts, j;	Dprintk("Asserting INIT.\n");	/*	 * Turn INIT on target chip	 */	apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));	/*

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?