2 * drivers/pci/setup-res.c
4 * Extruded from code written by
5 * Dave Rusling (david.rusling@reo.mts.dec.com)
6 * David Mosberger (davidm@cs.arizona.edu)
7 * David Miller (davem@redhat.com)
9 * Support routines for initializing a PCI subsystem.
12 /* fixed for multiple pci buses, 1999 Andrea Arcangeli <andrea@suse.de> */
15 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/pci.h>
22 #include <linux/errno.h>
23 #include <linux/ioport.h>
24 #include <linux/cache.h>
25 #include <linux/slab.h>
30 pci_update_resource(struct pci_dev *dev, struct resource *res, int resno)
32 struct pci_bus_region region;
36 pcibios_resource_to_bus(dev, ®ion, res);
38 pr_debug(" got res [%lx:%lx] bus [%lx:%lx] flags %lx for "
39 "BAR %d of %s\n", res->start, res->end,
40 region.start, region.end, res->flags, resno, pci_name(dev));
42 new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
43 if (res->flags & IORESOURCE_IO)
44 mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
46 mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
49 reg = PCI_BASE_ADDRESS_0 + 4 * resno;
50 } else if (resno == PCI_ROM_RESOURCE) {
51 new |= res->flags & IORESOURCE_ROM_ENABLE;
52 reg = dev->rom_base_reg;
54 /* Hmm, non-standard resource. */
56 return; /* kill uninitialised var warning */
59 pci_write_config_dword(dev, reg, new);
60 pci_read_config_dword(dev, reg, &check);
62 if ((new ^ check) & mask) {
63 printk(KERN_ERR "PCI: Error while updating region "
64 "%s/%d (%08x != %08x)\n", pci_name(dev), resno,
68 if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
69 (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) {
70 new = 0; /* currently everyone zeros the high address */
71 pci_write_config_dword(dev, reg + 4, new);
72 pci_read_config_dword(dev, reg + 4, &check);
74 printk(KERN_ERR "PCI: Error updating region "
75 "%s/%d (high %08x != %08x)\n",
76 pci_name(dev), resno, new, check);
79 res->flags &= ~IORESOURCE_UNSET;
80 pr_debug("PCI: moved device %s resource %d (%lx) to %x\n",
81 pci_name(dev), resno, res->flags,
82 new & ~PCI_REGION_FLAG_MASK);
86 pci_claim_resource(struct pci_dev *dev, int resource)
88 struct resource *res = &dev->resource[resource];
89 struct resource *root = NULL;
90 char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
93 if (res->flags & IORESOURCE_IO)
94 root = &ioport_resource;
95 if (res->flags & IORESOURCE_MEM)
96 root = &iomem_resource;
100 err = insert_resource(root, res);
103 printk(KERN_ERR "PCI: %s region %d of %s %s [%lx:%lx]\n",
104 root ? "Address space collision on" :
105 "No parent found for",
106 resource, dtype, pci_name(dev), res->start, res->end);
112 int pci_assign_resource(struct pci_dev *dev, int resno)
114 struct pci_bus *bus = dev->bus;
115 struct resource *res = dev->resource + resno;
116 unsigned long size, min, align;
119 size = res->end - res->start + 1;
120 min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
121 /* The bridge resources are special, as their
122 size != alignment. Sizing routines return
123 required alignment in the "start" field. */
124 align = (resno < PCI_BRIDGE_RESOURCES) ? size : res->start;
126 /* First, try exact prefetching match.. */
127 ret = pci_bus_alloc_resource(bus, res, size, align, min,
129 pcibios_align_resource, dev);
131 if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
135 * But a prefetching area can handle a non-prefetching
136 * window (it will just not perform as well).
138 ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
139 pcibios_align_resource, dev);
143 printk(KERN_ERR "PCI: Failed to allocate %s resource #%d:%lx@%lx for %s\n",
144 res->flags & IORESOURCE_IO ? "I/O" : "mem",
145 resno, size, res->start, pci_name(dev));
146 } else if (resno < PCI_BRIDGE_RESOURCES) {
147 pci_update_resource(dev, res, resno);
153 /* Sort resources by alignment */
155 pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
159 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
161 struct resource_list *list, *tmp;
162 unsigned long r_align;
164 r = &dev->resource[i];
165 r_align = r->end - r->start;
167 if (!(r->flags) || r->parent)
170 printk(KERN_WARNING "PCI: Ignore bogus resource %d "
172 i, r->start, r->end, pci_name(dev));
175 r_align = (i < PCI_BRIDGE_RESOURCES) ? r_align + 1 : r->start;
176 for (list = head; ; list = list->next) {
177 unsigned long align = 0;
178 struct resource_list *ln = list->next;
182 idx = ln->res - &ln->dev->resource[0];
183 align = (idx < PCI_BRIDGE_RESOURCES) ?
184 ln->res->end - ln->res->start + 1 :
187 if (r_align > align) {
188 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
190 panic("pdev_sort_resources(): "
191 "kmalloc() failed!\n");