blob: 3c5203ff53c7381e79249e940b54a2006b3cff27 [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
Yu Zhao14add802008-11-22 02:38:52 +080029void pci_update_resource(struct pci_dev *dev, int resno)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 struct pci_bus_region region;
32 u32 new, check, mask;
33 int reg;
Yu Zhao14add802008-11-22 02:38:52 +080034 struct resource *res = dev->resource + resno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
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
Bjorn Helgaas80ccba12008-06-13 10:52:11 -060046 * system resource we shouldn't move around.
Ralf Baechlefb0f2b42006-12-19 13:12:08 -080047 */
48 if (res->flags & IORESOURCE_PCI_FIXED)
49 return;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 pcibios_resource_to_bus(dev, &region, res);
52
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +110053 dev_dbg(&dev->dev, "BAR %d: got res %pR bus [%#llx-%#llx] "
54 "flags %#lx\n", resno, res,
Benjamin Herrenschmidt6015fbe2007-12-10 17:32:16 +110055 (unsigned long long)region.start,
56 (unsigned long long)region.end,
Bjorn Helgaas80ccba12008-06-13 10:52:11 -060057 (unsigned long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
60 if (res->flags & IORESOURCE_IO)
61 mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
62 else
63 mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
64
65 if (resno < 6) {
66 reg = PCI_BASE_ADDRESS_0 + 4 * resno;
67 } else if (resno == PCI_ROM_RESOURCE) {
Linus Torvalds755528c2005-08-26 10:49:22 -070068 if (!(res->flags & IORESOURCE_ROM_ENABLE))
69 return;
70 new |= PCI_ROM_ADDRESS_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 reg = dev->rom_base_reg;
72 } else {
73 /* Hmm, non-standard resource. */
74
75 return; /* kill uninitialised var warning */
76 }
77
78 pci_write_config_dword(dev, reg, new);
79 pci_read_config_dword(dev, reg, &check);
80
81 if ((new ^ check) & mask) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -060082 dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n",
83 resno, new, check);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
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)) {
Ivan Kokshayskycf7bee52005-08-07 13:49:59 +040088 new = region.start >> 16 >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 pci_write_config_dword(dev, reg + 4, new);
90 pci_read_config_dword(dev, reg + 4, &check);
91 if (check != new) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -060092 dev_err(&dev->dev, "BAR %d: error updating "
93 "(high %#08x != %#08x)\n", resno, new, check);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95 }
96 res->flags &= ~IORESOURCE_UNSET;
Bjorn Helgaas80ccba12008-06-13 10:52:11 -060097 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Sam Ravnborg96bde062007-03-26 21:53:30 -0800102int pci_claim_resource(struct pci_dev *dev, int resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 struct resource *res = &dev->resource[resource];
105 struct resource *root = NULL;
106 char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
107 int err;
108
David S. Miller085ae412005-08-08 13:19:08 -0700109 root = pcibios_select_root(dev, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 err = -EINVAL;
112 if (root != NULL)
113 err = insert_resource(root, res);
114
115 if (err) {
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +1100116 dev_err(&dev->dev, "BAR %d: %s of %s %pR\n",
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600117 resource,
118 root ? "address space collision on" :
119 "no parent found for",
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +1100120 dtype, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
123 return err;
124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126int pci_assign_resource(struct pci_dev *dev, int resno)
127{
128 struct pci_bus *bus = dev->bus;
129 struct resource *res = dev->resource + resno;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700130 resource_size_t size, min, align;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int ret;
132
Zhao, Yu022edd82008-10-13 19:24:28 +0800133 size = resource_size(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400135
136 align = resource_alignment(res);
137 if (!align) {
Ingo Molnar104bafcf2008-12-12 06:49:40 +0100138 dev_info(&dev->dev, "BAR %d: can't allocate resource (bogus "
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +1100139 "alignment) %pR flags %#lx\n",
140 resno, res, res->flags);
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400141 return -EINVAL;
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /* First, try exact prefetching match.. */
145 ret = pci_bus_alloc_resource(bus, res, size, align, min,
146 IORESOURCE_PREFETCH,
147 pcibios_align_resource, dev);
148
149 if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
150 /*
151 * That failed.
152 *
153 * But a prefetching area can handle a non-prefetching
154 * window (it will just not perform as well).
155 */
156 ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
157 pcibios_align_resource, dev);
158 }
159
160 if (ret) {
Ingo Molnar104bafcf2008-12-12 06:49:40 +0100161 dev_info(&dev->dev, "BAR %d: can't allocate %s resource %pR\n",
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +1100162 resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res);
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400163 } else {
164 res->flags &= ~IORESOURCE_STARTALIGN;
165 if (resno < PCI_BRIDGE_RESOURCES)
Yu Zhao14add802008-11-22 02:38:52 +0800166 pci_update_resource(dev, resno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168
169 return ret;
170}
171
Adrian Bunk2baad5f2008-02-13 23:30:12 +0200172#if 0
Kumar Gala75acfeca2006-05-01 10:43:46 -0500173int pci_assign_resource_fixed(struct pci_dev *dev, int resno)
174{
175 struct pci_bus *bus = dev->bus;
176 struct resource *res = dev->resource + resno;
177 unsigned int type_mask;
178 int i, ret = -EBUSY;
179
180 type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
181
182 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
183 struct resource *r = bus->resource[i];
184 if (!r)
185 continue;
186
187 /* type_mask must match */
188 if ((res->flags ^ r->flags) & type_mask)
189 continue;
190
191 ret = request_resource(r, res);
192
193 if (ret == 0)
194 break;
195 }
196
197 if (ret) {
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +1100198 dev_err(&dev->dev, "BAR %d: can't allocate %s resource %pR\n",
199 resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res);
Kumar Gala75acfeca2006-05-01 10:43:46 -0500200 } else if (resno < PCI_BRIDGE_RESOURCES) {
Yu Zhao14add802008-11-22 02:38:52 +0800201 pci_update_resource(dev, resno);
Kumar Gala75acfeca2006-05-01 10:43:46 -0500202 }
203
204 return ret;
205}
206EXPORT_SYMBOL_GPL(pci_assign_resource_fixed);
207#endif
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/* Sort resources by alignment */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800210void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 int i;
213
214 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
215 struct resource *r;
216 struct resource_list *list, *tmp;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700217 resource_size_t r_align;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 r = &dev->resource[i];
Ralf Baechlefb0f2b42006-12-19 13:12:08 -0800220
221 if (r->flags & IORESOURCE_PCI_FIXED)
222 continue;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (!(r->flags) || r->parent)
225 continue;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400226
227 r_align = resource_alignment(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (!r_align) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600229 dev_warn(&dev->dev, "BAR %d: bogus alignment "
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +1100230 "%pR flags %#lx\n",
231 i, r, r->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 continue;
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 for (list = head; ; list = list->next) {
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700235 resource_size_t align = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 struct resource_list *ln = list->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400238 if (ln)
239 align = resource_alignment(ln->res);
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (r_align > align) {
242 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
243 if (!tmp)
244 panic("pdev_sort_resources(): "
245 "kmalloc() failed!\n");
246 tmp->next = ln;
247 tmp->res = r;
248 tmp->dev = dev;
249 list->next = tmp;
250 break;
251 }
252 }
253 }
254}
Bjorn Helgaas842de402008-03-04 11:56:47 -0700255
256int pci_enable_resources(struct pci_dev *dev, int mask)
257{
258 u16 cmd, old_cmd;
259 int i;
260 struct resource *r;
261
262 pci_read_config_word(dev, PCI_COMMAND, &cmd);
263 old_cmd = cmd;
264
265 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
266 if (!(mask & (1 << i)))
267 continue;
268
269 r = &dev->resource[i];
270
271 if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
272 continue;
273 if ((i == PCI_ROM_RESOURCE) &&
274 (!(r->flags & IORESOURCE_ROM_ENABLE)))
275 continue;
276
277 if (!r->parent) {
278 dev_err(&dev->dev, "device not available because of "
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +1100279 "BAR %d %pR collisions\n", i, r);
Bjorn Helgaas842de402008-03-04 11:56:47 -0700280 return -EINVAL;
281 }
282
283 if (r->flags & IORESOURCE_IO)
284 cmd |= PCI_COMMAND_IO;
285 if (r->flags & IORESOURCE_MEM)
286 cmd |= PCI_COMMAND_MEMORY;
287 }
288
289 if (cmd != old_cmd) {
290 dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
291 old_cmd, cmd);
292 pci_write_config_word(dev, PCI_COMMAND, cmd);
293 }
294 return 0;
295}