blob: bad509e40fbc7f8f01920025cd5f8528aad2b59f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/pci/setup-res.c
3 *
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)
8 *
9 * Support routines for initializing a PCI subsystem.
10 */
11
12/* fixed for multiple pci buses, 1999 Andrea Arcangeli <andrea@suse.de> */
13
14/*
15 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
16 * Resource sorting
17 */
18
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>
26#include "pci.h"
27
28
John W. Linville064b53d2005-07-27 10:19:44 -040029void
Linus Torvalds1da177e2005-04-16 15:20:36 -070030pci_update_resource(struct pci_dev *dev, struct resource *res, int resno)
31{
32 struct pci_bus_region region;
33 u32 new, check, mask;
34 int reg;
35
Ralf Baechlefb0f2b42006-12-19 13:12:08 -080036 /*
37 * Ignore resources for unimplemented BARs and unused resource slots
38 * for 64 bit BARs.
39 */
Ivan Kokshayskycf7bee52005-08-07 13:49:59 +040040 if (!res->flags)
41 return;
42
Ralf Baechlefb0f2b42006-12-19 13:12:08 -080043 /*
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
47 * space.
48 */
49 if (res->flags & IORESOURCE_PCI_FIXED)
50 return;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 pcibios_resource_to_bus(dev, &region, res);
53
Benjamin Herrenschmidt6015fbe2007-12-10 17:32:16 +110054 pr_debug(" got res [%llx:%llx] bus [%llx:%llx] flags %lx for "
Greg Kroah-Hartman1396a8c2006-06-12 15:14:29 -070055 "BAR %d of %s\n", (unsigned long long)res->start,
56 (unsigned long long)res->end,
Benjamin Herrenschmidt6015fbe2007-12-10 17:32:16 +110057 (unsigned long long)region.start,
58 (unsigned long long)region.end,
59 (unsigned long)res->flags, resno, pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
62 if (res->flags & IORESOURCE_IO)
63 mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
64 else
65 mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
66
67 if (resno < 6) {
68 reg = PCI_BASE_ADDRESS_0 + 4 * resno;
69 } else if (resno == PCI_ROM_RESOURCE) {
Linus Torvalds755528c2005-08-26 10:49:22 -070070 if (!(res->flags & IORESOURCE_ROM_ENABLE))
71 return;
72 new |= PCI_ROM_ADDRESS_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 reg = dev->rom_base_reg;
74 } else {
75 /* Hmm, non-standard resource. */
76
77 return; /* kill uninitialised var warning */
78 }
79
80 pci_write_config_dword(dev, reg, new);
81 pci_read_config_dword(dev, reg, &check);
82
83 if ((new ^ check) & mask) {
84 printk(KERN_ERR "PCI: Error while updating region "
85 "%s/%d (%08x != %08x)\n", pci_name(dev), resno,
86 new, check);
87 }
88
89 if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
90 (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) {
Ivan Kokshayskycf7bee52005-08-07 13:49:59 +040091 new = region.start >> 16 >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 pci_write_config_dword(dev, reg + 4, new);
93 pci_read_config_dword(dev, reg + 4, &check);
94 if (check != new) {
95 printk(KERN_ERR "PCI: Error updating region "
96 "%s/%d (high %08x != %08x)\n",
97 pci_name(dev), resno, new, check);
98 }
99 }
100 res->flags &= ~IORESOURCE_UNSET;
101 pr_debug("PCI: moved device %s resource %d (%lx) to %x\n",
102 pci_name(dev), resno, res->flags,
103 new & ~PCI_REGION_FLAG_MASK);
104}
105
Sam Ravnborg96bde062007-03-26 21:53:30 -0800106int pci_claim_resource(struct pci_dev *dev, int resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 struct resource *res = &dev->resource[resource];
109 struct resource *root = NULL;
110 char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
111 int err;
112
David S. Miller085ae412005-08-08 13:19:08 -0700113 root = pcibios_select_root(dev, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 err = -EINVAL;
116 if (root != NULL)
117 err = insert_resource(root, res);
118
119 if (err) {
Greg Kroah-Hartman1396a8c2006-06-12 15:14:29 -0700120 printk(KERN_ERR "PCI: %s region %d of %s %s [%llx:%llx]\n",
121 root ? "Address space collision on" :
122 "No parent found for",
123 resource, dtype, pci_name(dev),
124 (unsigned long long)res->start,
125 (unsigned long long)res->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127
128 return err;
129}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131int pci_assign_resource(struct pci_dev *dev, int resno)
132{
133 struct pci_bus *bus = dev->bus;
134 struct resource *res = dev->resource + resno;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700135 resource_size_t size, min, align;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int ret;
137
138 size = res->end - res->start + 1;
139 min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
140 /* The bridge resources are special, as their
141 size != alignment. Sizing routines return
142 required alignment in the "start" field. */
143 align = (resno < PCI_BRIDGE_RESOURCES) ? size : res->start;
144
145 /* First, try exact prefetching match.. */
146 ret = pci_bus_alloc_resource(bus, res, size, align, min,
147 IORESOURCE_PREFETCH,
148 pcibios_align_resource, dev);
149
150 if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
151 /*
152 * That failed.
153 *
154 * But a prefetching area can handle a non-prefetching
155 * window (it will just not perform as well).
156 */
157 ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
158 pcibios_align_resource, dev);
159 }
160
161 if (ret) {
Greg Kroah-Hartman1396a8c2006-06-12 15:14:29 -0700162 printk(KERN_ERR "PCI: Failed to allocate %s resource "
163 "#%d:%llx@%llx for %s\n",
164 res->flags & IORESOURCE_IO ? "I/O" : "mem",
165 resno, (unsigned long long)size,
166 (unsigned long long)res->start, pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 } else if (resno < PCI_BRIDGE_RESOURCES) {
168 pci_update_resource(dev, res, resno);
169 }
170
171 return ret;
172}
173
Adrian Bunk2baad5f2008-02-13 23:30:12 +0200174#if 0
Kumar Gala75acfeca2006-05-01 10:43:46 -0500175int pci_assign_resource_fixed(struct pci_dev *dev, int resno)
176{
177 struct pci_bus *bus = dev->bus;
178 struct resource *res = dev->resource + resno;
179 unsigned int type_mask;
180 int i, ret = -EBUSY;
181
182 type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
183
184 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
185 struct resource *r = bus->resource[i];
186 if (!r)
187 continue;
188
189 /* type_mask must match */
190 if ((res->flags ^ r->flags) & type_mask)
191 continue;
192
193 ret = request_resource(r, res);
194
195 if (ret == 0)
196 break;
197 }
198
199 if (ret) {
200 printk(KERN_ERR "PCI: Failed to allocate %s resource "
201 "#%d:%llx@%llx for %s\n",
202 res->flags & IORESOURCE_IO ? "I/O" : "mem",
203 resno, (unsigned long long)(res->end - res->start + 1),
204 (unsigned long long)res->start, pci_name(dev));
205 } else if (resno < PCI_BRIDGE_RESOURCES) {
206 pci_update_resource(dev, res, resno);
207 }
208
209 return ret;
210}
211EXPORT_SYMBOL_GPL(pci_assign_resource_fixed);
212#endif
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/* Sort resources by alignment */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800215void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 int i;
218
219 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
220 struct resource *r;
221 struct resource_list *list, *tmp;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700222 resource_size_t r_align;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 r = &dev->resource[i];
Ralf Baechlefb0f2b42006-12-19 13:12:08 -0800225
226 if (r->flags & IORESOURCE_PCI_FIXED)
227 continue;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 r_align = r->end - r->start;
230
231 if (!(r->flags) || r->parent)
232 continue;
233 if (!r_align) {
234 printk(KERN_WARNING "PCI: Ignore bogus resource %d "
Greg Kroah-Hartman1396a8c2006-06-12 15:14:29 -0700235 "[%llx:%llx] of %s\n",
236 i, (unsigned long long)r->start,
237 (unsigned long long)r->end, pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 continue;
239 }
240 r_align = (i < PCI_BRIDGE_RESOURCES) ? r_align + 1 : r->start;
241 for (list = head; ; list = list->next) {
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700242 resource_size_t align = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 struct resource_list *ln = list->next;
244 int idx;
245
246 if (ln) {
247 idx = ln->res - &ln->dev->resource[0];
248 align = (idx < PCI_BRIDGE_RESOURCES) ?
249 ln->res->end - ln->res->start + 1 :
250 ln->res->start;
251 }
252 if (r_align > align) {
253 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
254 if (!tmp)
255 panic("pdev_sort_resources(): "
256 "kmalloc() failed!\n");
257 tmp->next = ln;
258 tmp->res = r;
259 tmp->dev = dev;
260 list->next = tmp;
261 break;
262 }
263 }
264 }
265}
Bjorn Helgaas842de402008-03-04 11:56:47 -0700266
267int pci_enable_resources(struct pci_dev *dev, int mask)
268{
269 u16 cmd, old_cmd;
270 int i;
271 struct resource *r;
272
273 pci_read_config_word(dev, PCI_COMMAND, &cmd);
274 old_cmd = cmd;
275
276 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
277 if (!(mask & (1 << i)))
278 continue;
279
280 r = &dev->resource[i];
281
282 if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
283 continue;
284 if ((i == PCI_ROM_RESOURCE) &&
285 (!(r->flags & IORESOURCE_ROM_ENABLE)))
286 continue;
287
288 if (!r->parent) {
289 dev_err(&dev->dev, "device not available because of "
290 "BAR %d [%llx:%llx] collisions\n", i,
291 (unsigned long long) r->start,
292 (unsigned long long) r->end);
293 return -EINVAL;
294 }
295
296 if (r->flags & IORESOURCE_IO)
297 cmd |= PCI_COMMAND_IO;
298 if (r->flags & IORESOURCE_MEM)
299 cmd |= PCI_COMMAND_MEMORY;
300 }
301
302 if (cmd != old_cmd) {
303 dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
304 old_cmd, cmd);
305 pci_write_config_word(dev, PCI_COMMAND, cmd);
306 }
307 return 0;
308}