probe.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 815 行 · 第 1/2 页

C
815
字号
/* * probe.c - PCI detection and setup code */#include <linux/init.h>#include <linux/pci.h>#include <linux/slab.h>#include <linux/module.h>#include <linux/cpumask.h>#undef DEBUG#ifdef DEBUG#define DBG(x...) printk(x)#else#define DBG(x...)#endif#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);/* * 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);/* * PCI Bus Class Devices */static ssize_t pci_bus_show_cpuaffinity(struct class_device *class_dev, char *buf){	cpumask_t cpumask = pcibus_to_cpumask((to_pci_bus(class_dev))->number);	int ret;	ret = cpumask_scnprintf(buf, PAGE_SIZE, cpumask);	if (ret < PAGE_SIZE)		buf[ret++] = '\n';	return ret;}static CLASS_DEVICE_ATTR(cpuaffinity, S_IRUGO, pci_bus_show_cpuaffinity, NULL);/* * 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, unsigned long 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 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) {		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;		if ((l & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY) {			sz = pci_size(l, sz, PCI_BASE_ADDRESS_MEM_MASK);			if (!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 ((l & (PCI_BASE_ADDRESS_SPACE | PCI_BASE_ADDRESS_MEM_TYPE_MASK))		    == (PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_64)) {			pci_read_config_dword(dev, reg+4, &l);			next++;#if BITS_PER_LONG == 64			res->start |= ((unsigned long) l) << 32;			res->end = res->start + sz;			pci_write_config_dword(dev, reg+4, ~0);			pci_read_config_dword(dev, reg+4, &sz);			pci_write_config_dword(dev, reg+4, l);			if (~sz)				res->end = res->start + 0xffffffff +						(((unsigned long) ~sz) << 32);#else			if (l) {				printk(KERN_ERR "PCI: Unable to handle 64-bit address for device %s\n", pci_name(dev));				res->start = 0;				res->flags = 0;				continue;			}#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, PCI_ROM_ADDRESS_MASK);			if (sz) {				res->flags = (l & PCI_ROM_ADDRESS_ENABLE) |				  IORESOURCE_MEM | IORESOURCE_PREFETCH |				  IORESOURCE_READONLY | IORESOURCE_CACHEABLE;				res->start = l & PCI_ROM_ADDRESS_MASK;				res->end = res->start + (unsigned long) sz;			}		}	}}void __devinit 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 = 0; i < PCI_BUS_NUM_RESOURCES; i++)			child->resource[i] = child->parent->resource[i];		return;	}	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;		res->start = base;		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);#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 %s\n", child->name);			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 * __devinit pci_alloc_bus(void){	struct pci_bus *b;	b = kmalloc(sizeof(*b), GFP_KERNEL);	if (b) {		memset(b, 0, sizeof(*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;	/*	 * 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->bridge = get_device(&bridge->dev);	child->class_dev.class = &pcibus_class;	sprintf(child->class_dev.class_id, "%04x:%02x", pci_domain_nr(child), busnr);	class_device_register(&child->class_dev);	class_device_create_file(&child->class_dev, &class_device_attr_cpuaffinity);	/*	 * Set up the primary, secondary and subordinate	 * bus numbers.	 */	child->number = child->secondary = busnr;	child->primary = parent->secondary;	child->subordinate = 0xff;	/* Set up default resource pointers and names.. */	for (i = 0; i < 4; i++) {		child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];		child->resource[i]->name = child->name;	}	bridge->subordinate = child;	return child;}struct pci_bus * __devinit pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr){	struct pci_bus *child;	child = pci_alloc_child_bus(parent, dev, busnr);	if (child)		list_add_tail(&child->node, &parent->children);	return child;}unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus);/* * If it's a bridge, configure it and scan the bus behind it. * For CardBus bridges, we don't scan behind as the devices will * be handled by the bridge driver itself. * * We need to process bridges in two passes -- first we scan those * already configured by the BIOS and after we are done with all of * them, we proceed to assigning numbers to the remaining buses in * order to avoid overlaps between old and new bus numbers. */int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev * dev, int max, int pass){	struct pci_bus *child;	int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);	u32 buses;	u16 bctl;	pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);	DBG("Scanning behind PCI bridge %s, config %06x, pass %d\n",	    pci_name(dev), buses & 0xffffff, pass);	/* Disable MasterAbortMode during probing to avoid reporting	   of bus errors (in some architectures) */ 	pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);	pci_write_config_word(dev, PCI_BRIDGE_CONTROL,			      bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);	if ((buses & 0xffff00) && !pcibios_assign_all_busses() && !is_cardbus) {		unsigned int cmax, busnr;		/*		 * Bus already configured by firmware, process it in the first		 * pass and just note the configuration.		 */		if (pass)			return max;		busnr = (buses >> 8) & 0xFF;		child = pci_alloc_child_bus(bus, dev, busnr);		if (!child)			return max;		child->primary = buses & 0xFF;		child->subordinate = (buses >> 16) & 0xFF;		child->bridge_ctl = bctl;		cmax = pci_scan_child_bus(child);		if (cmax > max)			max = cmax;		if (child->subordinate > max)			max = child->subordinate;	} else {		/*		 * We need to assign a number to this bus which we always		 * do in the second pass.		 */		if (!pass)			return max;		/* Clear errors */		pci_write_config_word(dev, PCI_STATUS, 0xffff);		child = pci_alloc_child_bus(bus, dev, ++max);		buses = (buses & 0xff000000)		      | ((unsigned int)(child->primary)     <<  0)		      | ((unsigned int)(child->secondary)   <<  8)		      | ((unsigned int)(child->subordinate) << 16);		/*		 * yenta.c forces a secondary latency timer of 176.		 * Copy that behaviour here.		 */		if (is_cardbus) {			buses &= ~0xff000000;			buses |= CARDBUS_LATENCY_TIMER << 24;		}					/*		 * We need to blast all three values with a single write.		 */

⌨️ 快捷键说明

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