probe.c

来自「linux 内核源代码」· C语言 代码 · 共 1,257 行 · 第 1/3 页

C
1,257
字号
/* * probe.c - PCI detection and setup code */#include <linux/kernel.h>#include <linux/delay.h>#include <linux/init.h>#include <linux/pci.h>#include <linux/slab.h>#include <linux/module.h>#include <linux/cpumask.h>#include "pci.h"#define CARDBUS_LATENCY_TIMER	176	/* secondary latency timer */#define CARDBUS_RESERVE_BUSNR	3#define PCI_CFG_SPACE_SIZE	256#define PCI_CFG_SPACE_EXP_SIZE	4096/* Ugh.  Need to stop exporting this to modules. */LIST_HEAD(pci_root_buses);EXPORT_SYMBOL(pci_root_buses);LIST_HEAD(pci_devices);/* * Some device drivers need know if pci is initiated. * Basically, we think pci is not initiated when there * is no device in list of pci_devices. */int no_pci_devices(void){	return list_empty(&pci_devices);}EXPORT_SYMBOL(no_pci_devices);#ifdef HAVE_PCI_LEGACY/** * pci_create_legacy_files - create legacy I/O port and memory files * @b: bus to create files under * * Some platforms allow access to legacy I/O port and ISA memory space on * a per-bus basis.  This routine creates the files and ties them into * their associated read, write and mmap files from pci-sysfs.c */static void pci_create_legacy_files(struct pci_bus *b){	b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,			       GFP_ATOMIC);	if (b->legacy_io) {		b->legacy_io->attr.name = "legacy_io";		b->legacy_io->size = 0xffff;		b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;		b->legacy_io->read = pci_read_legacy_io;		b->legacy_io->write = pci_write_legacy_io;		class_device_create_bin_file(&b->class_dev, b->legacy_io);		/* Allocated above after the legacy_io struct */		b->legacy_mem = b->legacy_io + 1;		b->legacy_mem->attr.name = "legacy_mem";		b->legacy_mem->size = 1024*1024;		b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;		b->legacy_mem->mmap = pci_mmap_legacy_mem;		class_device_create_bin_file(&b->class_dev, b->legacy_mem);	}}void pci_remove_legacy_files(struct pci_bus *b){	if (b->legacy_io) {		class_device_remove_bin_file(&b->class_dev, b->legacy_io);		class_device_remove_bin_file(&b->class_dev, b->legacy_mem);		kfree(b->legacy_io); /* both are allocated here */	}}#else /* !HAVE_PCI_LEGACY */static inline void pci_create_legacy_files(struct pci_bus *bus) { return; }void pci_remove_legacy_files(struct pci_bus *bus) { return; }#endif /* HAVE_PCI_LEGACY *//* * PCI Bus Class Devices */static ssize_t pci_bus_show_cpuaffinity(struct class_device *class_dev,					char *buf){	int ret;	cpumask_t cpumask;	cpumask = pcibus_to_cpumask(to_pci_bus(class_dev));	ret = cpumask_scnprintf(buf, PAGE_SIZE, cpumask);	if (ret < PAGE_SIZE)		buf[ret++] = '\n';	return ret;}CLASS_DEVICE_ATTR(cpuaffinity, S_IRUGO, pci_bus_show_cpuaffinity, NULL);/* * PCI Bus Class */static void release_pcibus_dev(struct class_device *class_dev){	struct pci_bus *pci_bus = to_pci_bus(class_dev);	if (pci_bus->bridge)		put_device(pci_bus->bridge);	kfree(pci_bus);}static struct class pcibus_class = {	.name		= "pci_bus",	.release	= &release_pcibus_dev,};static int __init pcibus_class_init(void){	return class_register(&pcibus_class);}postcore_initcall(pcibus_class_init);/* * Translate the low bits of the PCI base * to the resource type */static inline unsigned int pci_calc_resource_flags(unsigned int flags){	if (flags & PCI_BASE_ADDRESS_SPACE_IO)		return IORESOURCE_IO;	if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)		return IORESOURCE_MEM | IORESOURCE_PREFETCH;	return IORESOURCE_MEM;}/* * Find the extent of a PCI decode.. */static u32 pci_size(u32 base, u32 maxbase, u32 mask){	u32 size = mask & maxbase;	/* Find the significant bits */	if (!size)		return 0;	/* Get the lowest of them to find the decode size, and	   from that the extent.  */	size = (size & ~(size-1)) - 1;	/* base == maxbase can be valid only if the BAR has	   already been programmed with all 1s.  */	if (base == maxbase && ((base | size) & mask) != mask)		return 0;	return size;}static u64 pci_size64(u64 base, u64 maxbase, u64 mask){	u64 size = mask & maxbase;	/* Find the significant bits */	if (!size)		return 0;	/* Get the lowest of them to find the decode size, and	   from that the extent.  */	size = (size & ~(size-1)) - 1;	/* base == maxbase can be valid only if the BAR has	   already been programmed with all 1s.  */	if (base == maxbase && ((base | size) & mask) != mask)		return 0;	return size;}static inline int is_64bit_memory(u32 mask){	if ((mask & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==	    (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64))		return 1;	return 0;}static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom){	unsigned int pos, reg, next;	u32 l, sz;	struct resource *res;	for(pos=0; pos<howmany; pos = next) {		u64 l64;		u64 sz64;		u32 raw_sz;		next = pos+1;		res = &dev->resource[pos];		res->name = pci_name(dev);		reg = PCI_BASE_ADDRESS_0 + (pos << 2);		pci_read_config_dword(dev, reg, &l);		pci_write_config_dword(dev, reg, ~0);		pci_read_config_dword(dev, reg, &sz);		pci_write_config_dword(dev, reg, l);		if (!sz || sz == 0xffffffff)			continue;		if (l == 0xffffffff)			l = 0;		raw_sz = sz;		if ((l & PCI_BASE_ADDRESS_SPACE) ==				PCI_BASE_ADDRESS_SPACE_MEMORY) {			sz = pci_size(l, sz, (u32)PCI_BASE_ADDRESS_MEM_MASK);			/*			 * For 64bit prefetchable memory sz could be 0, if the			 * real size is bigger than 4G, so we need to check			 * szhi for that.			 */			if (!is_64bit_memory(l) && !sz)				continue;			res->start = l & PCI_BASE_ADDRESS_MEM_MASK;			res->flags |= l & ~PCI_BASE_ADDRESS_MEM_MASK;		} else {			sz = pci_size(l, sz, PCI_BASE_ADDRESS_IO_MASK & 0xffff);			if (!sz)				continue;			res->start = l & PCI_BASE_ADDRESS_IO_MASK;			res->flags |= l & ~PCI_BASE_ADDRESS_IO_MASK;		}		res->end = res->start + (unsigned long) sz;		res->flags |= pci_calc_resource_flags(l);		if (is_64bit_memory(l)) {			u32 szhi, lhi;			pci_read_config_dword(dev, reg+4, &lhi);			pci_write_config_dword(dev, reg+4, ~0);			pci_read_config_dword(dev, reg+4, &szhi);			pci_write_config_dword(dev, reg+4, lhi);			sz64 = ((u64)szhi << 32) | raw_sz;			l64 = ((u64)lhi << 32) | l;			sz64 = pci_size64(l64, sz64, PCI_BASE_ADDRESS_MEM_MASK);			next++;#if BITS_PER_LONG == 64			if (!sz64) {				res->start = 0;				res->end = 0;				res->flags = 0;				continue;			}			res->start = l64 & PCI_BASE_ADDRESS_MEM_MASK;			res->end = res->start + sz64;#else			if (sz64 > 0x100000000ULL) {				printk(KERN_ERR "PCI: Unable to handle 64-bit "					"BAR for device %s\n", pci_name(dev));				res->start = 0;				res->flags = 0;			} else if (lhi) {				/* 64-bit wide address, treat as disabled */				pci_write_config_dword(dev, reg,					l & ~(u32)PCI_BASE_ADDRESS_MEM_MASK);				pci_write_config_dword(dev, reg+4, 0);				res->start = 0;				res->end = sz;			}#endif		}	}	if (rom) {		dev->rom_base_reg = rom;		res = &dev->resource[PCI_ROM_RESOURCE];		res->name = pci_name(dev);		pci_read_config_dword(dev, rom, &l);		pci_write_config_dword(dev, rom, ~PCI_ROM_ADDRESS_ENABLE);		pci_read_config_dword(dev, rom, &sz);		pci_write_config_dword(dev, rom, l);		if (l == 0xffffffff)			l = 0;		if (sz && sz != 0xffffffff) {			sz = pci_size(l, sz, (u32)PCI_ROM_ADDRESS_MASK);			if (sz) {				res->flags = (l & IORESOURCE_ROM_ENABLE) |				  IORESOURCE_MEM | IORESOURCE_PREFETCH |				  IORESOURCE_READONLY | IORESOURCE_CACHEABLE;				res->start = l & PCI_ROM_ADDRESS_MASK;				res->end = res->start + (unsigned long) sz;			}		}	}}void pci_read_bridge_bases(struct pci_bus *child){	struct pci_dev *dev = child->self;	u8 io_base_lo, io_limit_lo;	u16 mem_base_lo, mem_limit_lo;	unsigned long base, limit;	struct resource *res;	int i;	if (!dev)		/* It's a host bus, nothing to read */		return;	if (dev->transparent) {		printk(KERN_INFO "PCI: Transparent bridge - %s\n", pci_name(dev));		for(i = 3; i < PCI_BUS_NUM_RESOURCES; i++)			child->resource[i] = child->parent->resource[i - 3];	}	for(i=0; i<3; i++)		child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];	res = child->resource[0];	pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);	pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);	base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;	limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;	if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {		u16 io_base_hi, io_limit_hi;		pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);		pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);		base |= (io_base_hi << 16);		limit |= (io_limit_hi << 16);	}	if (base <= limit) {		res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;		if (!res->start)			res->start = base;		if (!res->end)			res->end = limit + 0xfff;	}	res = child->resource[1];	pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);	pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);	base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;	limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;	if (base <= limit) {		res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;		res->start = base;		res->end = limit + 0xfffff;	}	res = child->resource[2];	pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);	pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);	base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;	limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;	if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {		u32 mem_base_hi, mem_limit_hi;		pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);		pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);		/*		 * Some bridges set the base > limit by default, and some		 * (broken) BIOSes do not initialize them.  If we find		 * this, just assume they are not being used.		 */		if (mem_base_hi <= mem_limit_hi) {#if BITS_PER_LONG == 64			base |= ((long) mem_base_hi) << 32;			limit |= ((long) mem_limit_hi) << 32;#else			if (mem_base_hi || mem_limit_hi) {				printk(KERN_ERR "PCI: Unable to handle 64-bit address space for bridge %s\n", pci_name(dev));				return;			}#endif		}	}	if (base <= limit) {		res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM | IORESOURCE_PREFETCH;		res->start = base;		res->end = limit + 0xfffff;	}}static struct pci_bus * pci_alloc_bus(void){	struct pci_bus *b;	b = kzalloc(sizeof(*b), GFP_KERNEL);	if (b) {		INIT_LIST_HEAD(&b->node);		INIT_LIST_HEAD(&b->children);		INIT_LIST_HEAD(&b->devices);	}	return b;}static struct pci_bus * __devinitpci_alloc_child_bus(struct pci_bus *parent, struct pci_dev *bridge, int busnr){	struct pci_bus *child;	int i;	int retval;	/*	 * Allocate a new bus, and inherit stuff from the parent..	 */	child = pci_alloc_bus();	if (!child)		return NULL;	child->self = bridge;	child->parent = parent;	child->ops = parent->ops;	child->sysdata = parent->sysdata;	child->bus_flags = parent->bus_flags;	child->bridge = get_device(&bridge->dev);	child->class_dev.class = &pcibus_class;	sprintf(child->class_dev.class_id, "%04x:%02x", pci_domain_nr(child), busnr);	retval = class_device_register(&child->class_dev);	if (retval)		goto error_register;	retval = class_device_create_file(&child->class_dev,					  &class_device_attr_cpuaffinity);	if (retval)		goto error_file_create;

⌨️ 快捷键说明

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