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>
29 void pci_update_resource(struct pci_dev *dev, int resno)
31 struct pci_bus_region region;
34 struct resource *res = dev->resource + resno;
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 shouldn't move around.
48 if (res->flags & IORESOURCE_PCI_FIXED)
51 pcibios_resource_to_bus(dev, ®ion, res);
53 dev_dbg(&dev->dev, "BAR %d: got res %pR bus [%#llx-%#llx] "
54 "flags %#lx\n", resno, res,
55 (unsigned long long)region.start,
56 (unsigned long long)region.end,
57 (unsigned long)res->flags);
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 dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n",
86 if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
87 (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) {
88 new = region.start >> 16 >> 16;
89 pci_write_config_dword(dev, reg + 4, new);
90 pci_read_config_dword(dev, reg + 4, &check);
92 dev_err(&dev->dev, "BAR %d: error updating "
93 "(high %#08x != %#08x)\n", resno, new, check);
96 res->flags &= ~IORESOURCE_UNSET;
97 dev_dbg(&dev->dev, "BAR %d: moved to bus [%#llx-%#llx] flags %#lx\n",
98 resno, (unsigned long long)region.start,
99 (unsigned long long)region.end, res->flags);
102 int pci_claim_resource(struct pci_dev *dev, int resource)
104 struct resource *res = &dev->resource[resource];
105 struct resource *root = NULL;
106 char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
109 root = pcibios_select_root(dev, res);
113 err = insert_resource(root, res);
116 dev_err(&dev->dev, "BAR %d: %s of %s %pR\n",
118 root ? "address space collision on" :
119 "no parent found for",
126 int pci_assign_resource(struct pci_dev *dev, int resno)
128 struct pci_bus *bus = dev->bus;
129 struct resource *res = dev->resource + resno;
130 resource_size_t size, min, align;
133 size = resource_size(res);
134 min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
136 align = resource_alignment(res);
138 dev_info(&dev->dev, "BAR %d: can't allocate resource (bogus "
139 "alignment) %pR flags %#lx\n",
140 resno, res, res->flags);
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 dev_info(&dev->dev, "BAR %d: can't allocate %s resource %pR\n",
162 resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res);
164 res->flags &= ~IORESOURCE_STARTALIGN;
165 if (resno < PCI_BRIDGE_RESOURCES)
166 pci_update_resource(dev, resno);
173 int pci_assign_resource_fixed(struct pci_dev *dev, int resno)
175 struct pci_bus *bus = dev->bus;
176 struct resource *res = dev->resource + resno;
177 unsigned int type_mask;
180 type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
182 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
183 struct resource *r = bus->resource[i];
187 /* type_mask must match */
188 if ((res->flags ^ r->flags) & type_mask)
191 ret = request_resource(r, res);
198 dev_err(&dev->dev, "BAR %d: can't allocate %s resource %pR\n",
199 resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res);
200 } else if (resno < PCI_BRIDGE_RESOURCES) {
201 pci_update_resource(dev, resno);
206 EXPORT_SYMBOL_GPL(pci_assign_resource_fixed);
209 /* Sort resources by alignment */
210 void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
214 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
216 struct resource_list *list, *tmp;
217 resource_size_t r_align;
219 r = &dev->resource[i];
221 if (r->flags & IORESOURCE_PCI_FIXED)
224 if (!(r->flags) || r->parent)
227 r_align = resource_alignment(r);
229 dev_warn(&dev->dev, "BAR %d: bogus alignment "
234 for (list = head; ; list = list->next) {
235 resource_size_t align = 0;
236 struct resource_list *ln = list->next;
239 align = resource_alignment(ln->res);
241 if (r_align > align) {
242 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
244 panic("pdev_sort_resources(): "
245 "kmalloc() failed!\n");
256 int pci_enable_resources(struct pci_dev *dev, int mask)
262 pci_read_config_word(dev, PCI_COMMAND, &cmd);
265 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
266 if (!(mask & (1 << i)))
269 r = &dev->resource[i];
271 if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
273 if ((i == PCI_ROM_RESOURCE) &&
274 (!(r->flags & IORESOURCE_ROM_ENABLE)))
278 dev_err(&dev->dev, "device not available because of "
279 "BAR %d %pR collisions\n", i, r);
283 if (r->flags & IORESOURCE_IO)
284 cmd |= PCI_COMMAND_IO;
285 if (r->flags & IORESOURCE_MEM)
286 cmd |= PCI_COMMAND_MEMORY;
289 if (cmd != old_cmd) {
290 dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
292 pci_write_config_word(dev, PCI_COMMAND, cmd);