⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 apic.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *	Local APIC handling, local APIC timers * *	(c) 1999, 2000 Ingo Molnar <mingo@redhat.com> * *	Fixes *	Maciej W. Rozycki	:	Bits for genuine 82489DX APICs; *					thanks to Eric Gilmore *					and Rolf G. Tews *					for testing these extensively. *	Maciej W. Rozycki	:	Various updates and fixes. */#include <linux/config.h>#include <linux/init.h>#include <linux/mm.h>#include <linux/irq.h>#include <linux/delay.h>#include <linux/bootmem.h>#include <linux/smp_lock.h>#include <linux/interrupt.h>#include <linux/mc146818rtc.h>#include <linux/kernel_stat.h>#include <asm/smp.h>#include <asm/mtrr.h>#include <asm/mpspec.h>#include <asm/pgalloc.h>int prof_multiplier[NR_CPUS] = { 1, };int prof_old_multiplier[NR_CPUS] = { 1, };int prof_counter[NR_CPUS] = { 1, };int get_maxlvt(void){	unsigned int v, ver, maxlvt;	v = apic_read(APIC_LVR);	ver = GET_APIC_VERSION(v);	/* 82489DXs do not report # of LVT entries. */	maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;	return maxlvt;}static void clear_local_APIC(void){	int maxlvt;	unsigned long v;	maxlvt = get_maxlvt();	/*	 * Careful: we have to set masks only first to deassert	 * any level-triggered sources.	 */	v = apic_read(APIC_LVTT);	apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);	v = apic_read(APIC_LVT0);	apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);	v = apic_read(APIC_LVT1);	apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);	if (maxlvt >= 3) {		v = apic_read(APIC_LVTERR);		apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);	}	if (maxlvt >= 4) {		v = apic_read(APIC_LVTPC);		apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);	}	/*	 * Clean APIC state for other OSs:	 */	apic_write_around(APIC_LVTT, APIC_LVT_MASKED);	apic_write_around(APIC_LVT0, APIC_LVT_MASKED);	apic_write_around(APIC_LVT1, APIC_LVT_MASKED);	if (maxlvt >= 3)		apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);	if (maxlvt >= 4)		apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);}void __init connect_bsp_APIC(void){	if (pic_mode) {		/*		 * Do not trust the local APIC being empty at bootup.		 */		clear_local_APIC();		/*		 * PIC mode, enable symmetric IO mode in the IMCR,		 * i.e. connect BSP's local APIC to INT and NMI lines.		 */		printk("leaving PIC mode, enabling symmetric IO mode.\n");		outb(0x70, 0x22);		outb(0x01, 0x23);	}}void disconnect_bsp_APIC(void){	if (pic_mode) {		/*		 * Put the board back into PIC mode (has an effect		 * only on certain older boards).  Note that APIC		 * interrupts, including IPIs, won't work beyond		 * this point!  The only exception are INIT IPIs.		 */		printk("disabling symmetric IO mode, entering PIC mode.\n");		outb(0x70, 0x22);		outb(0x00, 0x23);	}}void disable_local_APIC(void){	unsigned long value;	clear_local_APIC();	/*	 * Disable APIC (implies clearing of registers	 * for 82489DX!).	 */	value = apic_read(APIC_SPIV);	value &= ~(1<<8);	apic_write_around(APIC_SPIV, value);}/* * This is to verify that we're looking at a real local APIC. * Check these against your board if the CPUs aren't getting * started for no apparent reason. */int __init verify_local_APIC(void){	unsigned int reg0, reg1;	/*	 * The version register is read-only in a real APIC.	 */	reg0 = apic_read(APIC_LVR);	Dprintk("Getting VERSION: %x\n", reg0);	apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);	reg1 = apic_read(APIC_LVR);	Dprintk("Getting VERSION: %x\n", reg1);	/*	 * The two version reads above should print the same	 * numbers.  If the second one is different, then we	 * poke at a non-APIC.	 */	if (reg1 != reg0)		return 0;	/*	 * Check if the version looks reasonably.	 */	reg1 = GET_APIC_VERSION(reg0);	if (reg1 == 0x00 || reg1 == 0xff)		return 0;	reg1 = get_maxlvt();	if (reg1 < 0x02 || reg1 == 0xff)		return 0;	/*	 * The ID register is read/write in a real APIC.	 */	reg0 = apic_read(APIC_ID);	Dprintk("Getting ID: %x\n", reg0);	apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);	reg1 = apic_read(APIC_ID);	Dprintk("Getting ID: %x\n", reg1);	apic_write(APIC_ID, reg0);	if (reg1 != (reg0 ^ APIC_ID_MASK))		return 0;	/*	 * The next two are just to see if we have sane values.	 * They're only really relevant if we're in Virtual Wire	 * compatibility mode, but most boxes are anymore.	 */	reg0 = apic_read(APIC_LVT0);	Dprintk("Getting LVT0: %x\n", reg0);	reg1 = apic_read(APIC_LVT1);	Dprintk("Getting LVT1: %x\n", reg1);	return 1;}void __init sync_Arb_IDs(void){	/*	 * Wait for idle.	 */	apic_wait_icr_idle();	Dprintk("Synchronizing Arb IDs.\n");	apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG				| APIC_DM_INIT);}extern void __error_in_apic_c (void);void __init setup_local_APIC (void){	unsigned long value, ver, maxlvt;	value = apic_read(APIC_LVR);	ver = GET_APIC_VERSION(value);	if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)		__error_in_apic_c();	/*	 * Double-check wether this APIC is really registered.	 */	if (!test_bit(GET_APIC_ID(apic_read(APIC_ID)), &phys_cpu_present_map))		BUG();	/*	 * Intel recommends to set DFR, LDR and TPR before enabling	 * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel	 * document number 292116).  So here it goes...	 */	/*	 * Put the APIC into flat delivery mode.	 * Must be "all ones" explicitly for 82489DX.	 */	apic_write_around(APIC_DFR, 0xffffffff);	/*	 * Set up the logical destination ID.	 */	value = apic_read(APIC_LDR);	value &= ~APIC_LDR_MASK;	value |= (1<<(smp_processor_id()+24));	apic_write_around(APIC_LDR, value);	/*	 * Set Task Priority to 'accept all'. We never change this	 * later on.	 */	value = apic_read(APIC_TASKPRI);	value &= ~APIC_TPRI_MASK;	apic_write_around(APIC_TASKPRI, value);	/*	 * Now that we are all set up, enable the APIC	 */	value = apic_read(APIC_SPIV);	value &= ~APIC_VECTOR_MASK;	/*	 * Enable APIC	 */	value |= (1<<8);	/*	 * Some unknown Intel IO/APIC (or APIC) errata is biting us with	 * certain networking cards. If high frequency interrupts are	 * happening on a particular IOAPIC pin, plus the IOAPIC routing	 * entry is masked/unmasked at a high rate as well then sooner or	 * later IOAPIC line gets 'stuck', no more interrupts are received	 * from the device. If focus CPU is disabled then the hang goes	 * away, oh well :-(	 *	 * [ This bug can be reproduced easily with a level-triggered	 *   PCI Ne2000 networking cards and PII/PIII processors, dual	 *   BX chipset. ]	 */#if 0	/* Enable focus processor (bit==0) */	value &= ~(1<<9);#else	/* Disable focus processor (bit==1) */	value |= (1<<9);#endif	/*	 * Set spurious IRQ vector	 */	value |= SPURIOUS_APIC_VECTOR;	apic_write_around(APIC_SPIV, value);	/*	 * Set up LVT0, LVT1:	 *	 * set up through-local-APIC on the BP's LINT0. This is not	 * strictly necessery in pure symmetric-IO mode, but sometimes	 * we delegate interrupts to the 8259A.	 */	/*	 * TODO: set up through-local-APIC from through-I/O-APIC? --macro	 */	value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;	if (!smp_processor_id() && (pic_mode || !value)) {		value = APIC_DM_EXTINT;		printk("enabled ExtINT on CPU#%d\n", smp_processor_id());	} else {		value = APIC_DM_EXTINT | APIC_LVT_MASKED;		printk("masked ExtINT on CPU#%d\n", smp_processor_id());	}	apic_write_around(APIC_LVT0, value);	/*	 * only the BP should see the LINT1 NMI signal, obviously.	 */	if (!smp_processor_id())		value = APIC_DM_NMI;	else		value = APIC_DM_NMI | APIC_LVT_MASKED;	if (!APIC_INTEGRATED(ver))		/* 82489DX */		value |= APIC_LVT_LEVEL_TRIGGER;	apic_write_around(APIC_LVT1, value);	if (APIC_INTEGRATED(ver)) {		/* !82489DX */		maxlvt = get_maxlvt();		if (maxlvt > 3)		/* Due to the Pentium erratum 3AP. */			apic_write(APIC_ESR, 0);		value = apic_read(APIC_ESR);		printk("ESR value before enabling vector: %08lx\n", value);		value = ERROR_APIC_VECTOR;      // enables sending errors		apic_write_around(APIC_LVTERR, value);		/*		 * spec says clear errors after enabling vector.		 */		if (maxlvt > 3)			apic_write(APIC_ESR, 0);		value = apic_read(APIC_ESR);		printk("ESR value after enabling vector: %08lx\n", value);	} else		printk("No ESR for 82489DX.\n");}void __init init_apic_mappings(void){	unsigned long apic_phys;	if (smp_found_config) {		apic_phys = mp_lapic_addr;	} else {		/*		 * set up a fake all zeroes page to simulate the		 * local APIC and another one for the IO-APIC. We		 * could use the real zero-page, but it's safer		 * this way if some buggy code writes to this page ...		 */		apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);		apic_phys = __pa(apic_phys);	}	set_fixmap_nocache(FIX_APIC_BASE, apic_phys);	Dprintk("mapped APIC to %08lx (%08lx)\n", APIC_BASE, apic_phys);	/*	 * Fetch the APIC ID of the BSP in case we have a	 * default configuration (or the MP table is broken).	 */	if (boot_cpu_id == -1U)		boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));#ifdef CONFIG_X86_IO_APIC	{		unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;		int i;		for (i = 0; i < nr_ioapics; i++) {			if (smp_found_config) {				ioapic_phys = mp_ioapics[i].mpc_apicaddr;			} else {				ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);				ioapic_phys = __pa(ioapic_phys);			}			set_fixmap_nocache(idx, ioapic_phys);			Dprintk("mapped IOAPIC to %08lx (%08lx)\n",					__fix_to_virt(idx), ioapic_phys);			idx++;		}	}#endif}/* * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts * per second. We assume that the caller has already set up the local * APIC. * * The APIC timer is not exactly sync with the external timer chip, it * closely follows bus clocks. */

⌨️ 快捷键说明

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