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;
37 * Ignore resources for unimplemented BARs and unused resource slots
44 * Ignore non-moveable resources. This might be legacy resources for
45 * which no functional BAR register exists or another important
46 * system resource we should better not move around in system address
49 if (res->flags & IORESOURCE_PCI_FIXED)
52 pcibios_resource_to_bus(dev, ®ion, res);
54 pr_debug(" got res [%llx:%llx] bus [%lx:%lx] flags %lx for "
55 "BAR %d of %s\n", (unsigned long long)res->start,
56 (unsigned long long)res->end,
57 region.start, region.end, res->flags, resno, pci_name(dev));
59 new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
60 if (res->flags & IORESOURCE_IO)
61 mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
63 mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
66 reg = PCI_BASE_ADDRESS_0 + 4 * resno;
67 } else if (resno == PCI_ROM_RESOURCE) {
68 if (!(res->flags & IORESOURCE_ROM_ENABLE))
70 new |= PCI_ROM_ADDRESS_ENABLE;
71 reg = dev->rom_base_reg;
73 /* Hmm, non-standard resource. */
75 return; /* kill uninitialised var warning */
78 pci_write_config_dword(dev, reg, new);
79 pci_read_config_dword(dev, reg, &check);
81 if ((new ^ check) & mask) {
82 printk(KERN_ERR "PCI: Error while updating region "
83 "%s/%d (%08x != %08x)\n", pci_name(dev), resno,
87 if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
88 (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) {
89 new = region.start >> 16 >> 16;
90 pci_write_config_dword(dev, reg + 4, new);
91 pci_read_config_dword(dev, reg + 4, &check);
93 printk(KERN_ERR "PCI: Error updating region "
94 "%s/%d (high %08x != %08x)\n",
95 pci_name(dev), resno, new, check);
98 res->flags &= ~IORESOURCE_UNSET;
99 pr_debug("PCI: moved device %s resource %d (%lx) to %x\n",
100 pci_name(dev), resno, res->flags,
101 new & ~PCI_REGION_FLAG_MASK);
104 int pci_claim_resource(struct pci_dev *dev, int resource)
106 struct resource *res = &dev->resource[resource];
107 struct resource *root = NULL;
108 char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
111 root = pcibios_select_root(dev, res);
115 err = insert_resource(root, res);
118 printk(KERN_ERR "PCI: %s region %d of %s %s [%llx:%llx]\n",
119 root ? "Address space collision on" :
120 "No parent found for",
121 resource, dtype, pci_name(dev),
122 (unsigned long long)res->start,
123 (unsigned long long)res->end);
128 EXPORT_SYMBOL_GPL(pci_claim_resource);
130 int pci_assign_resource(struct pci_dev *dev, int resno)
132 struct pci_bus *bus = dev->bus;
133 struct resource *res = dev->resource + resno;
134 resource_size_t size, min, align;
137 size = res->end - res->start + 1;
138 min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
139 /* The bridge resources are special, as their
140 size != alignment. Sizing routines return
141 required alignment in the "start" field. */
142 align = (resno < PCI_BRIDGE_RESOURCES) ? size : res->start;
144 /* First, try exact prefetching match.. */
145 ret = pci_bus_alloc_resource(bus, res, size, align, min,
147 pcibios_align_resource, dev);
149 if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
153 * But a prefetching area can handle a non-prefetching
154 * window (it will just not perform as well).
156 ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
157 pcibios_align_resource, dev);
161 printk(KERN_ERR "PCI: Failed to allocate %s resource "
162 "#%d:%llx@%llx for %s\n",
163 res->flags & IORESOURCE_IO ? "I/O" : "mem",
164 resno, (unsigned long long)size,
165 (unsigned long long)res->start, pci_name(dev));
166 } else if (resno < PCI_BRIDGE_RESOURCES) {
167 pci_update_resource(dev, res, resno);
173 #ifdef CONFIG_EMBEDDED
174 int pci_assign_resource_fixed(struct pci_dev *dev, int resno)
176 struct pci_bus *bus = dev->bus;
177 struct resource *res = dev->resource + resno;
178 unsigned int type_mask;
181 type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
183 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
184 struct resource *r = bus->resource[i];
188 /* type_mask must match */
189 if ((res->flags ^ r->flags) & type_mask)
192 ret = request_resource(r, res);
199 printk(KERN_ERR "PCI: Failed to allocate %s resource "
200 "#%d:%llx@%llx for %s\n",
201 res->flags & IORESOURCE_IO ? "I/O" : "mem",
202 resno, (unsigned long long)(res->end - res->start + 1),
203 (unsigned long long)res->start, pci_name(dev));
204 } else if (resno < PCI_BRIDGE_RESOURCES) {
205 pci_update_resource(dev, res, resno);
210 EXPORT_SYMBOL_GPL(pci_assign_resource_fixed);
213 /* Sort resources by alignment */
214 void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
218 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
220 struct resource_list *list, *tmp;
221 resource_size_t r_align;
223 r = &dev->resource[i];
225 if (r->flags & IORESOURCE_PCI_FIXED)
228 r_align = r->end - r->start;
230 if (!(r->flags) || r->parent)
233 printk(KERN_WARNING "PCI: Ignore bogus resource %d "
234 "[%llx:%llx] of %s\n",
235 i, (unsigned long long)r->start,
236 (unsigned long long)r->end, pci_name(dev));
239 r_align = (i < PCI_BRIDGE_RESOURCES) ? r_align + 1 : r->start;
240 for (list = head; ; list = list->next) {
241 resource_size_t align = 0;
242 struct resource_list *ln = list->next;
246 idx = ln->res - &ln->dev->resource[0];
247 align = (idx < PCI_BRIDGE_RESOURCES) ?
248 ln->res->end - ln->res->start + 1 :
251 if (r_align > align) {
252 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
254 panic("pdev_sort_resources(): "
255 "kmalloc() failed!\n");