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, struct resource *res, int resno)
31 struct pci_bus_region region;
36 * Ignore resources for unimplemented BARs and unused resource slots
43 * Ignore non-moveable resources. This might be legacy resources for
44 * which no functional BAR register exists or another important
45 * system resource we shouldn't move around.
47 if (res->flags & IORESOURCE_PCI_FIXED)
50 pcibios_resource_to_bus(dev, ®ion, res);
52 dev_dbg(&dev->dev, "BAR %d: got res %pR bus [%#llx-%#llx] "
53 "flags %#lx\n", resno, res,
54 (unsigned long long)region.start,
55 (unsigned long long)region.end,
56 (unsigned long)res->flags);
58 new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
59 if (res->flags & IORESOURCE_IO)
60 mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
62 mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
65 reg = PCI_BASE_ADDRESS_0 + 4 * resno;
66 } else if (resno == PCI_ROM_RESOURCE) {
67 if (!(res->flags & IORESOURCE_ROM_ENABLE))
69 new |= PCI_ROM_ADDRESS_ENABLE;
70 reg = dev->rom_base_reg;
72 /* Hmm, non-standard resource. */
74 return; /* kill uninitialised var warning */
77 pci_write_config_dword(dev, reg, new);
78 pci_read_config_dword(dev, reg, &check);
80 if ((new ^ check) & mask) {
81 dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n",
85 if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
86 (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) {
87 new = region.start >> 16 >> 16;
88 pci_write_config_dword(dev, reg + 4, new);
89 pci_read_config_dword(dev, reg + 4, &check);
91 dev_err(&dev->dev, "BAR %d: error updating "
92 "(high %#08x != %#08x)\n", resno, new, check);
95 res->flags &= ~IORESOURCE_UNSET;
96 dev_dbg(&dev->dev, "BAR %d: moved to bus [%#llx-%#llx] flags %#lx\n",
97 resno, (unsigned long long)region.start,
98 (unsigned long long)region.end, res->flags);
101 int pci_claim_resource(struct pci_dev *dev, int resource)
103 struct resource *res = &dev->resource[resource];
104 struct resource *root = NULL;
105 char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
108 root = pcibios_select_root(dev, res);
112 err = insert_resource(root, res);
115 dev_err(&dev->dev, "BAR %d: %s of %s %pR\n",
117 root ? "address space collision on" :
118 "no parent found for",
125 int pci_assign_resource(struct pci_dev *dev, int resno)
127 struct pci_bus *bus = dev->bus;
128 struct resource *res = dev->resource + resno;
129 resource_size_t size, min, align;
132 size = res->end - res->start + 1;
133 min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
135 align = resource_alignment(res);
137 dev_err(&dev->dev, "BAR %d: can't allocate resource (bogus "
138 "alignment) %pR flags %#lx\n",
139 resno, res, res->flags);
143 /* First, try exact prefetching match.. */
144 ret = pci_bus_alloc_resource(bus, res, size, align, min,
146 pcibios_align_resource, dev);
148 if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
152 * But a prefetching area can handle a non-prefetching
153 * window (it will just not perform as well).
155 ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
156 pcibios_align_resource, dev);
160 dev_err(&dev->dev, "BAR %d: can't allocate %s resource %pR\n",
161 resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res);
163 res->flags &= ~IORESOURCE_STARTALIGN;
164 if (resno < PCI_BRIDGE_RESOURCES)
165 pci_update_resource(dev, res, resno);
172 int pci_assign_resource_fixed(struct pci_dev *dev, int resno)
174 struct pci_bus *bus = dev->bus;
175 struct resource *res = dev->resource + resno;
176 unsigned int type_mask;
179 type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
181 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
182 struct resource *r = bus->resource[i];
186 /* type_mask must match */
187 if ((res->flags ^ r->flags) & type_mask)
190 ret = request_resource(r, res);
197 dev_err(&dev->dev, "BAR %d: can't allocate %s resource %pR\n",
198 resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res);
199 } else if (resno < PCI_BRIDGE_RESOURCES) {
200 pci_update_resource(dev, res, resno);
205 EXPORT_SYMBOL_GPL(pci_assign_resource_fixed);
208 /* Sort resources by alignment */
209 void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
213 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
215 struct resource_list *list, *tmp;
216 resource_size_t r_align;
218 r = &dev->resource[i];
220 if (r->flags & IORESOURCE_PCI_FIXED)
223 if (!(r->flags) || r->parent)
226 r_align = resource_alignment(r);
228 dev_warn(&dev->dev, "BAR %d: bogus alignment "
233 for (list = head; ; list = list->next) {
234 resource_size_t align = 0;
235 struct resource_list *ln = list->next;
238 align = resource_alignment(ln->res);
240 if (r_align > align) {
241 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
243 panic("pdev_sort_resources(): "
244 "kmalloc() failed!\n");
255 int pci_enable_resources(struct pci_dev *dev, int mask)
261 pci_read_config_word(dev, PCI_COMMAND, &cmd);
264 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
265 if (!(mask & (1 << i)))
268 r = &dev->resource[i];
270 if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
272 if ((i == PCI_ROM_RESOURCE) &&
273 (!(r->flags & IORESOURCE_ROM_ENABLE)))
277 dev_err(&dev->dev, "device not available because of "
278 "BAR %d %pR collisions\n", i, r);
282 if (r->flags & IORESOURCE_IO)
283 cmd |= PCI_COMMAND_IO;
284 if (r->flags & IORESOURCE_MEM)
285 cmd |= PCI_COMMAND_MEMORY;
288 if (cmd != old_cmd) {
289 dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
291 pci_write_config_word(dev, PCI_COMMAND, cmd);