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

📄 pci-pc.c

📁 讲述linux的初始化过程
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *	Low-Level PCI Support for PC * *	(c) 1999--2000 Martin Mares <mj@suse.cz> */#include <linux/config.h>#include <linux/types.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/pci.h>#include <linux/init.h>#include <linux/ioport.h>#include <asm/segment.h>#include <asm/io.h>#include "pci-i386.h"unsigned int pci_probe = PCI_PROBE_BIOS | PCI_PROBE_CONF1 | PCI_PROBE_CONF2;int pcibios_last_bus = -1;struct pci_bus *pci_root_bus;struct pci_ops *pci_root_ops;/* * Direct access to PCI hardware... */#ifdef CONFIG_PCI_DIRECT/* * Functions for accessing PCI configuration space with type 1 accesses */#define CONFIG_CMD(dev, where)   (0x80000000 | (dev->bus->number << 16) | (dev->devfn << 8) | (where & ~3))static int pci_conf1_read_config_byte(struct pci_dev *dev, int where, u8 *value){	outl(CONFIG_CMD(dev,where), 0xCF8);	*value = inb(0xCFC + (where&3));	return PCIBIOS_SUCCESSFUL;}static int pci_conf1_read_config_word(struct pci_dev *dev, int where, u16 *value){	outl(CONFIG_CMD(dev,where), 0xCF8);    	*value = inw(0xCFC + (where&2));	return PCIBIOS_SUCCESSFUL;    }static int pci_conf1_read_config_dword(struct pci_dev *dev, int where, u32 *value){	outl(CONFIG_CMD(dev,where), 0xCF8);	*value = inl(0xCFC);	return PCIBIOS_SUCCESSFUL;    }static int pci_conf1_write_config_byte(struct pci_dev *dev, int where, u8 value){	outl(CONFIG_CMD(dev,where), 0xCF8);    	outb(value, 0xCFC + (where&3));	return PCIBIOS_SUCCESSFUL;}static int pci_conf1_write_config_word(struct pci_dev *dev, int where, u16 value){	outl(CONFIG_CMD(dev,where), 0xCF8);	outw(value, 0xCFC + (where&2));	return PCIBIOS_SUCCESSFUL;}static int pci_conf1_write_config_dword(struct pci_dev *dev, int where, u32 value){	outl(CONFIG_CMD(dev,where), 0xCF8);	outl(value, 0xCFC);	return PCIBIOS_SUCCESSFUL;}#undef CONFIG_CMDstatic struct pci_ops pci_direct_conf1 = {	pci_conf1_read_config_byte,	pci_conf1_read_config_word,	pci_conf1_read_config_dword,	pci_conf1_write_config_byte,	pci_conf1_write_config_word,	pci_conf1_write_config_dword};/* * Functions for accessing PCI configuration space with type 2 accesses */#define IOADDR(devfn, where)	((0xC000 | ((devfn & 0x78) << 5)) + where)#define FUNC(devfn)		(((devfn & 7) << 1) | 0xf0)#define SET(dev)		if (dev->devfn & 0x80) return PCIBIOS_DEVICE_NOT_FOUND;		\				outb(FUNC(dev->devfn), 0xCF8);					\				outb(dev->bus->number, 0xCFA);static int pci_conf2_read_config_byte(struct pci_dev *dev, int where, u8 *value){	SET(dev);	*value = inb(IOADDR(dev->devfn,where));	outb (0, 0xCF8);	return PCIBIOS_SUCCESSFUL;}static int pci_conf2_read_config_word(struct pci_dev *dev, int where, u16 *value){	SET(dev);	*value = inw(IOADDR(dev->devfn,where));	outb (0, 0xCF8);	return PCIBIOS_SUCCESSFUL;}static int pci_conf2_read_config_dword(struct pci_dev *dev, int where, u32 *value){	SET(dev);	*value = inl (IOADDR(dev->devfn,where));    	outb (0, 0xCF8);    	return PCIBIOS_SUCCESSFUL;}static int pci_conf2_write_config_byte(struct pci_dev *dev, int where, u8 value){	SET(dev);	outb (value, IOADDR(dev->devfn,where));	outb (0, 0xCF8);    	return PCIBIOS_SUCCESSFUL;}static int pci_conf2_write_config_word(struct pci_dev *dev, int where, u16 value){	SET(dev);	outw (value, IOADDR(dev->devfn,where));	outb (0, 0xCF8);    	return PCIBIOS_SUCCESSFUL;}static int pci_conf2_write_config_dword(struct pci_dev *dev, int where, u32 value){	SET(dev);	outl (value, IOADDR(dev->devfn,where));    	outb (0, 0xCF8);    	return PCIBIOS_SUCCESSFUL;}#undef SET#undef IOADDR#undef FUNCstatic struct pci_ops pci_direct_conf2 = {	pci_conf2_read_config_byte,	pci_conf2_read_config_word,	pci_conf2_read_config_dword,	pci_conf2_write_config_byte,	pci_conf2_write_config_word,	pci_conf2_write_config_dword};/* * Before we decide to use direct hardware access mechanisms, we try to do some * trivial checks to ensure it at least _seems_ to be working -- we just test * whether bus 00 contains a host bridge (this is similar to checking * techniques used in XFree86, but ours should be more reliable since we * attempt to make use of direct access hints provided by the PCI BIOS). * * This should be close to trivial, but it isn't, because there are buggy * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID. */static int __init pci_sanity_check(struct pci_ops *o){	u16 x;	struct pci_bus bus;		/* Fake bus and device */	struct pci_dev dev;	if (pci_probe & PCI_NO_CHECKS)		return 1;	bus.number = 0;	dev.bus = &bus;	for(dev.devfn=0; dev.devfn < 0x100; dev.devfn++)		if ((!o->read_word(&dev, PCI_CLASS_DEVICE, &x) &&		     (x == PCI_CLASS_BRIDGE_HOST || x == PCI_CLASS_DISPLAY_VGA)) ||		    (!o->read_word(&dev, PCI_VENDOR_ID, &x) &&		     (x == PCI_VENDOR_ID_INTEL || x == PCI_VENDOR_ID_COMPAQ)))			return 1;	DBG("PCI: Sanity check failed\n");	return 0;}static struct pci_ops * __init pci_check_direct(void){	unsigned int tmp;	unsigned long flags;	__save_flags(flags); __cli();	/*	 * Check if configuration type 1 works.	 */	if (pci_probe & PCI_PROBE_CONF1) {		outb (0x01, 0xCFB);		tmp = inl (0xCF8);		outl (0x80000000, 0xCF8);		if (inl (0xCF8) == 0x80000000 &&		    pci_sanity_check(&pci_direct_conf1)) {			outl (tmp, 0xCF8);			__restore_flags(flags);			printk("PCI: Using configuration type 1\n");			request_region(0xCF8, 8, "PCI conf1");			return &pci_direct_conf1;		}		outl (tmp, 0xCF8);	}	/*	 * Check if configuration type 2 works.	 */	if (pci_probe & PCI_PROBE_CONF2) {		outb (0x00, 0xCFB);		outb (0x00, 0xCF8);		outb (0x00, 0xCFA);		if (inb (0xCF8) == 0x00 && inb (0xCFA) == 0x00 &&		    pci_sanity_check(&pci_direct_conf2)) {			__restore_flags(flags);			printk("PCI: Using configuration type 2\n");			request_region(0xCF8, 4, "PCI conf2");			return &pci_direct_conf2;		}	}	__restore_flags(flags);	return NULL;}#endif/* * BIOS32 and PCI BIOS handling. */#ifdef CONFIG_PCI_BIOS#define PCIBIOS_PCI_FUNCTION_ID 	0xb1XX#define PCIBIOS_PCI_BIOS_PRESENT 	0xb101#define PCIBIOS_FIND_PCI_DEVICE		0xb102#define PCIBIOS_FIND_PCI_CLASS_CODE	0xb103#define PCIBIOS_GENERATE_SPECIAL_CYCLE	0xb106#define PCIBIOS_READ_CONFIG_BYTE	0xb108#define PCIBIOS_READ_CONFIG_WORD	0xb109#define PCIBIOS_READ_CONFIG_DWORD	0xb10a#define PCIBIOS_WRITE_CONFIG_BYTE	0xb10b#define PCIBIOS_WRITE_CONFIG_WORD	0xb10c#define PCIBIOS_WRITE_CONFIG_DWORD	0xb10d#define PCIBIOS_GET_ROUTING_OPTIONS	0xb10e#define PCIBIOS_SET_PCI_HW_INT		0xb10f/* BIOS32 signature: "_32_" */#define BIOS32_SIGNATURE	(('_' << 0) + ('3' << 8) + ('2' << 16) + ('_' << 24))/* PCI signature: "PCI " */#define PCI_SIGNATURE		(('P' << 0) + ('C' << 8) + ('I' << 16) + (' ' << 24))/* PCI service signature: "$PCI" */#define PCI_SERVICE		(('$' << 0) + ('P' << 8) + ('C' << 16) + ('I' << 24))/* PCI BIOS hardware mechanism flags */#define PCIBIOS_HW_TYPE1		0x01#define PCIBIOS_HW_TYPE2		0x02#define PCIBIOS_HW_TYPE1_SPEC		0x10#define PCIBIOS_HW_TYPE2_SPEC		0x20/* * This is the standard structure used to identify the entry point * to the BIOS32 Service Directory, as documented in * 	Standard BIOS 32-bit Service Directory Proposal * 	Revision 0.4 May 24, 1993 * 	Phoenix Technologies Ltd. *	Norwood, MA * and the PCI BIOS specification. */union bios32 {	struct {		unsigned long signature;	/* _32_ */		unsigned long entry;		/* 32 bit physical address */		unsigned char revision;		/* Revision level, 0 */		unsigned char length;		/* Length in paragraphs should be 01 */		unsigned char checksum;		/* All bytes must add up to zero */		unsigned char reserved[5]; 	/* Must be zero */	} fields;	char chars[16];};/* * Physical address of the service directory.  I don't know if we're * allowed to have more than one of these or not, so just in case * we'll make pcibios_present() take a memory start parameter and store * the array there. */static struct {	unsigned long address;	unsigned short segment;} bios32_indirect = { 0, __KERNEL_CS };/* * Returns the entry point for the given service, NULL on error */static unsigned long bios32_service(unsigned long service){	unsigned char return_code;	/* %al */	unsigned long address;		/* %ebx */	unsigned long length;		/* %ecx */	unsigned long entry;		/* %edx */	unsigned long flags;	__save_flags(flags); __cli();	__asm__("lcall (%%edi); cld"		: "=a" (return_code),		  "=b" (address),		  "=c" (length),		  "=d" (entry)		: "0" (service),		  "1" (0),		  "D" (&bios32_indirect));	__restore_flags(flags);	switch (return_code) {		case 0:			return address + entry;		case 0x80:	/* Not present */			printk("bios32_service(0x%lx): not present\n", service);			return 0;		default: /* Shouldn't happen */			printk("bios32_service(0x%lx): returned 0x%x -- BIOS bug!\n",				service, return_code);			return 0;	}}static struct {	unsigned long address;	unsigned short segment;} pci_indirect = { 0, __KERNEL_CS };static int pci_bios_present;static int __init check_pcibios(void){	u32 signature, eax, ebx, ecx;	u8 status, major_ver, minor_ver, hw_mech;	unsigned long flags, pcibios_entry;	if ((pcibios_entry = bios32_service(PCI_SERVICE))) {		pci_indirect.address = pcibios_entry + PAGE_OFFSET;		__save_flags(flags); __cli();		__asm__(			"lcall (%%edi); cld\n\t"			"jc 1f\n\t"			"xor %%ah, %%ah\n"			"1:"			: "=d" (signature),			  "=a" (eax),			  "=b" (ebx),			  "=c" (ecx)			: "1" (PCIBIOS_PCI_BIOS_PRESENT),			  "D" (&pci_indirect)			: "memory");		__restore_flags(flags);		status = (eax >> 8) & 0xff;		hw_mech = eax & 0xff;		major_ver = (ebx >> 8) & 0xff;		minor_ver = ebx & 0xff;		if (pcibios_last_bus < 0)			pcibios_last_bus = ecx & 0xff;		DBG("PCI: BIOS probe returned s=%02x hw=%02x ver=%02x.%02x l=%02x\n",			status, hw_mech, major_ver, minor_ver, pcibios_last_bus);		if (status || signature != PCI_SIGNATURE) {			printk (KERN_ERR "PCI: BIOS BUG #%x[%08x] found, report to <mj@suse.cz>\n",				status, signature);			return 0;		}		printk("PCI: PCI BIOS revision %x.%02x entry at 0x%lx, last bus=%d\n",			major_ver, minor_ver, pcibios_entry, pcibios_last_bus);#ifdef CONFIG_PCI_DIRECT		if (!(hw_mech & PCIBIOS_HW_TYPE1))			pci_probe &= ~PCI_PROBE_CONF1;		if (!(hw_mech & PCIBIOS_HW_TYPE2))			pci_probe &= ~PCI_PROBE_CONF2;#endif		return 1;	}	return 0;}static int __init pci_bios_find_device (unsigned short vendor, unsigned short device_id,					unsigned short index, unsigned char *bus, unsigned char *device_fn){	unsigned short bx;	unsigned short ret;	__asm__("lcall (%%edi); cld\n\t"		"jc 1f\n\t"		"xor %%ah, %%ah\n"		"1:"		: "=b" (bx),		  "=a" (ret)		: "1" (PCIBIOS_FIND_PCI_DEVICE),		  "c" (device_id),		  "d" (vendor),		  "S" ((int) index),		  "D" (&pci_indirect));	*bus = (bx >> 8) & 0xff;	*device_fn = bx & 0xff;	return (int) (ret & 0xff00) >> 8;}static int pci_bios_read_config_byte(struct pci_dev *dev, int where, u8 *value){	unsigned long ret;	unsigned long bx = (dev->bus->number << 8) | dev->devfn;	__asm__("lcall (%%esi); cld\n\t"		"jc 1f\n\t"		"xor %%ah, %%ah\n"		"1:"		: "=c" (*value),		  "=a" (ret)		: "1" (PCIBIOS_READ_CONFIG_BYTE),		  "b" (bx),		  "D" ((long) where),		  "S" (&pci_indirect));	return (int) (ret & 0xff00) >> 8;}static int pci_bios_read_config_word(struct pci_dev *dev, int where, u16 *value){	unsigned long ret;	unsigned long bx = (dev->bus->number << 8) | dev->devfn;	__asm__("lcall (%%esi); cld\n\t"		"jc 1f\n\t"		"xor %%ah, %%ah\n"		"1:"		: "=c" (*value),		  "=a" (ret)		: "1" (PCIBIOS_READ_CONFIG_WORD),		  "b" (bx),		  "D" ((long) where),		  "S" (&pci_indirect));	return (int) (ret & 0xff00) >> 8;}static int pci_bios_read_config_dword(struct pci_dev *dev, int where, u32 *value){	unsigned long ret;	unsigned long bx = (dev->bus->number << 8) | dev->devfn;	__asm__("lcall (%%esi); cld\n\t"		"jc 1f\n\t"		"xor %%ah, %%ah\n"		"1:"		: "=c" (*value),		  "=a" (ret)		: "1" (PCIBIOS_READ_CONFIG_DWORD),		  "b" (bx),		  "D" ((long) where),		  "S" (&pci_indirect));	return (int) (ret & 0xff00) >> 8;}static int pci_bios_write_config_byte(struct pci_dev *dev, int where, u8 value){	unsigned long ret;	unsigned long bx = (dev->bus->number << 8) | dev->devfn;	__asm__("lcall (%%esi); cld\n\t"		"jc 1f\n\t"		"xor %%ah, %%ah\n"		"1:"		: "=a" (ret)		: "0" (PCIBIOS_WRITE_CONFIG_BYTE),		  "c" (value),		  "b" (bx),		  "D" ((long) where),		  "S" (&pci_indirect));	return (int) (ret & 0xff00) >> 8;}static int pci_bios_write_config_word(struct pci_dev *dev, int where, u16 value){	unsigned long ret;	unsigned long bx = (dev->bus->number << 8) | dev->devfn;	__asm__("lcall (%%esi); cld\n\t"		"jc 1f\n\t"		"xor %%ah, %%ah\n"		"1:"		: "=a" (ret)		: "0" (PCIBIOS_WRITE_CONFIG_WORD),		  "c" (value),		  "b" (bx),		  "D" ((long) where),		  "S" (&pci_indirect));	return (int) (ret & 0xff00) >> 8;}static int pci_bios_write_config_dword(struct pci_dev *dev, int where, u32 value){	unsigned long ret;	unsigned long bx = (dev->bus->number << 8) | dev->devfn;	__asm__("lcall (%%esi); cld\n\t"		"jc 1f\n\t"		"xor %%ah, %%ah\n"		"1:"		: "=a" (ret)		: "0" (PCIBIOS_WRITE_CONFIG_DWORD),		  "c" (value),		  "b" (bx),		  "D" ((long) where),

⌨️ 快捷键说明

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