blob: 706f82d8111fe621f8ad163f43cc417a1a743fba [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 Zhao613e7ed2008-11-22 02:41:27 +080034 enum pci_bar_type type;
Yu Zhao14add802008-11-22 02:38:52 +080035 struct resource *res = dev->resource + resno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Ralf Baechlefb0f2b42006-12-19 13:12:08 -080037 /*
38 * Ignore resources for unimplemented BARs and unused resource slots
39 * for 64 bit BARs.
40 */
Ivan Kokshayskycf7bee52005-08-07 13:49:59 +040041 if (!res->flags)
42 return;
43
Ralf Baechlefb0f2b42006-12-19 13:12:08 -080044 /*
45 * Ignore non-moveable resources. This might be legacy resources for
46 * which no functional BAR register exists or another important
Bjorn Helgaas80ccba12008-06-13 10:52:11 -060047 * system resource we shouldn't move around.
Ralf Baechlefb0f2b42006-12-19 13:12:08 -080048 */
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 Herrenschmidt096e6f62008-10-20 15:07:37 +110054 dev_dbg(&dev->dev, "BAR %d: got res %pR bus [%#llx-%#llx] "
55 "flags %#lx\n", resno, res,
Benjamin Herrenschmidt6015fbe2007-12-10 17:32:16 +110056 (unsigned long long)region.start,
57 (unsigned long long)region.end,
Bjorn Helgaas80ccba12008-06-13 10:52:11 -060058 (unsigned long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
61 if (res->flags & IORESOURCE_IO)
62 mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
63 else
64 mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
65
Yu Zhao613e7ed2008-11-22 02:41:27 +080066 reg = pci_resource_bar(dev, resno, &type);
67 if (!reg)
68 return;
69 if (type != pci_bar_unknown) {
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 }
74
75 pci_write_config_dword(dev, reg, new);
76 pci_read_config_dword(dev, reg, &check);
77
78 if ((new ^ check) & mask) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -060079 dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n",
80 resno, new, check);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82
83 if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
84 (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) {
Ivan Kokshayskycf7bee52005-08-07 13:49:59 +040085 new = region.start >> 16 >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 pci_write_config_dword(dev, reg + 4, new);
87 pci_read_config_dword(dev, reg + 4, &check);
88 if (check != new) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -060089 dev_err(&dev->dev, "BAR %d: error updating "
90 "(high %#08x != %#08x)\n", resno, new, check);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92 }
93 res->flags &= ~IORESOURCE_UNSET;
Bjorn Helgaas80ccba12008-06-13 10:52:11 -060094 dev_dbg(&dev->dev, "BAR %d: moved to bus [%#llx-%#llx] flags %#lx\n",
95 resno, (unsigned long long)region.start,
96 (unsigned long long)region.end, res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Sam Ravnborg96bde062007-03-26 21:53:30 -080099int pci_claim_resource(struct pci_dev *dev, int resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 struct resource *res = &dev->resource[resource];
Matthew Wilcoxcebd78a2009-06-17 16:33:33 -0400102 struct resource *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 int err;
104
Matthew Wilcoxcebd78a2009-06-17 16:33:33 -0400105 root = pci_find_parent_resource(dev, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 err = -EINVAL;
108 if (root != NULL)
Linus Torvalds79896cf2009-08-02 14:04:19 -0700109 err = request_resource(root, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 if (err) {
Linus Torvalds79896cf2009-08-02 14:04:19 -0700112 const char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +1100113 dev_err(&dev->dev, "BAR %d: %s of %s %pR\n",
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600114 resource,
115 root ? "address space collision on" :
116 "no parent found for",
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +1100117 dtype, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119
120 return err;
121}
Jesse Barneseaa959d2009-06-30 21:45:44 -0700122EXPORT_SYMBOL(pci_claim_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Yuji Shimada32a9a6822009-03-16 17:13:39 +0900124#ifdef CONFIG_PCI_QUIRKS
125void pci_disable_bridge_window(struct pci_dev *dev)
126{
127 dev_dbg(&dev->dev, "Disabling bridge window.\n");
128
129 /* MMIO Base/Limit */
130 pci_write_config_dword(dev, PCI_MEMORY_BASE, 0x0000fff0);
131
132 /* Prefetchable MMIO Base/Limit */
133 pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
134 pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0x0000fff0);
135 pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0xffffffff);
136}
137#endif /* CONFIG_PCI_QUIRKS */
138
Yinghai Lud09ee962009-04-23 20:49:25 -0700139static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,
140 int resno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct resource *res = dev->resource + resno;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700143 resource_size_t size, min, align;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int ret;
145
Zhao, Yu022edd82008-10-13 19:24:28 +0800146 size = resource_size(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
Chris Wright6faf17f2009-08-28 13:00:06 -0700148 align = pci_resource_alignment(dev, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 /* First, try exact prefetching match.. */
151 ret = pci_bus_alloc_resource(bus, res, size, align, min,
152 IORESOURCE_PREFETCH,
153 pcibios_align_resource, dev);
154
155 if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
156 /*
157 * That failed.
158 *
159 * But a prefetching area can handle a non-prefetching
160 * window (it will just not perform as well).
161 */
162 ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
163 pcibios_align_resource, dev);
164 }
165
Yinghai Lud09ee962009-04-23 20:49:25 -0700166 if (!ret) {
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400167 res->flags &= ~IORESOURCE_STARTALIGN;
168 if (resno < PCI_BRIDGE_RESOURCES)
Yu Zhao14add802008-11-22 02:38:52 +0800169 pci_update_resource(dev, resno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171
172 return ret;
173}
174
Yinghai Lud09ee962009-04-23 20:49:25 -0700175int pci_assign_resource(struct pci_dev *dev, int resno)
176{
177 struct resource *res = dev->resource + resno;
178 resource_size_t align;
179 struct pci_bus *bus;
180 int ret;
181
Chris Wright6faf17f2009-08-28 13:00:06 -0700182 align = pci_resource_alignment(dev, res);
Yinghai Lud09ee962009-04-23 20:49:25 -0700183 if (!align) {
184 dev_info(&dev->dev, "BAR %d: can't allocate resource (bogus "
185 "alignment) %pR flags %#lx\n",
186 resno, res, res->flags);
187 return -EINVAL;
188 }
189
190 bus = dev->bus;
191 while ((ret = __pci_assign_resource(bus, dev, resno))) {
192 if (bus->parent && bus->self->transparent)
193 bus = bus->parent;
194 else
195 bus = NULL;
196 if (bus)
197 continue;
198 break;
199 }
200
201 if (ret)
202 dev_info(&dev->dev, "BAR %d: can't allocate %s resource %pR\n",
203 resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res);
204
205 return ret;
206}
207
Adrian Bunk2baad5f2008-02-13 23:30:12 +0200208#if 0
Kumar Gala75acfeca2006-05-01 10:43:46 -0500209int pci_assign_resource_fixed(struct pci_dev *dev, int resno)
210{
211 struct pci_bus *bus = dev->bus;
212 struct resource *res = dev->resource + resno;
213 unsigned int type_mask;
214 int i, ret = -EBUSY;
215
216 type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
217
218 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
219 struct resource *r = bus->resource[i];
220 if (!r)
221 continue;
222
223 /* type_mask must match */
224 if ((res->flags ^ r->flags) & type_mask)
225 continue;
226
227 ret = request_resource(r, res);
228
229 if (ret == 0)
230 break;
231 }
232
233 if (ret) {
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +1100234 dev_err(&dev->dev, "BAR %d: can't allocate %s resource %pR\n",
235 resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res);
Kumar Gala75acfeca2006-05-01 10:43:46 -0500236 } else if (resno < PCI_BRIDGE_RESOURCES) {
Yu Zhao14add802008-11-22 02:38:52 +0800237 pci_update_resource(dev, resno);
Kumar Gala75acfeca2006-05-01 10:43:46 -0500238 }
239
240 return ret;
241}
242EXPORT_SYMBOL_GPL(pci_assign_resource_fixed);
243#endif
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/* Sort resources by alignment */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800246void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 int i;
249
250 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
251 struct resource *r;
252 struct resource_list *list, *tmp;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700253 resource_size_t r_align;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 r = &dev->resource[i];
Ralf Baechlefb0f2b42006-12-19 13:12:08 -0800256
257 if (r->flags & IORESOURCE_PCI_FIXED)
258 continue;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!(r->flags) || r->parent)
261 continue;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400262
Chris Wright6faf17f2009-08-28 13:00:06 -0700263 r_align = pci_resource_alignment(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (!r_align) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600265 dev_warn(&dev->dev, "BAR %d: bogus alignment "
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +1100266 "%pR flags %#lx\n",
267 i, r, r->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 continue;
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 for (list = head; ; list = list->next) {
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700271 resource_size_t align = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 struct resource_list *ln = list->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400274 if (ln)
Chris Wright6faf17f2009-08-28 13:00:06 -0700275 align = pci_resource_alignment(ln->dev, ln->res);
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (r_align > align) {
278 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
279 if (!tmp)
280 panic("pdev_sort_resources(): "
281 "kmalloc() failed!\n");
282 tmp->next = ln;
283 tmp->res = r;
284 tmp->dev = dev;
285 list->next = tmp;
286 break;
287 }
288 }
289 }
290}
Bjorn Helgaas842de402008-03-04 11:56:47 -0700291
292int pci_enable_resources(struct pci_dev *dev, int mask)
293{
294 u16 cmd, old_cmd;
295 int i;
296 struct resource *r;
297
298 pci_read_config_word(dev, PCI_COMMAND, &cmd);
299 old_cmd = cmd;
300
301 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
302 if (!(mask & (1 << i)))
303 continue;
304
305 r = &dev->resource[i];
306
307 if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
308 continue;
309 if ((i == PCI_ROM_RESOURCE) &&
310 (!(r->flags & IORESOURCE_ROM_ENABLE)))
311 continue;
312
313 if (!r->parent) {
314 dev_err(&dev->dev, "device not available because of "
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +1100315 "BAR %d %pR collisions\n", i, r);
Bjorn Helgaas842de402008-03-04 11:56:47 -0700316 return -EINVAL;
317 }
318
319 if (r->flags & IORESOURCE_IO)
320 cmd |= PCI_COMMAND_IO;
321 if (r->flags & IORESOURCE_MEM)
322 cmd |= PCI_COMMAND_MEMORY;
323 }
324
325 if (cmd != old_cmd) {
326 dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
327 old_cmd, cmd);
328 pci_write_config_word(dev, PCI_COMMAND, cmd);
329 }
330 return 0;
331}