blob: d2a32d88d2ae8161a6ee4a5677bc6b7311c28dea [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
Paul Gortmaker363c75d2011-05-27 09:37:25 -040020#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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;
Bjorn Helgaas9aac5372012-07-09 19:49:37 -060032 bool disable;
33 u16 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 u32 new, check, mask;
35 int reg;
Yu Zhao613e7ed2008-11-22 02:41:27 +080036 enum pci_bar_type type;
Yu Zhao14add802008-11-22 02:38:52 +080037 struct resource *res = dev->resource + resno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Bjorn Helgaas63880b232016-11-28 11:19:27 -060039 /* Per SR-IOV spec 3.4.1.11, VF BARs are RO zero */
40 if (dev->is_virtfn)
Wei Yang70675e02015-07-29 16:52:58 +080041 return;
Wei Yang70675e02015-07-29 16:52:58 +080042
Ralf Baechlefb0f2b42006-12-19 13:12:08 -080043 /*
44 * Ignore resources for unimplemented BARs and unused resource slots
45 * for 64 bit BARs.
46 */
Ivan Kokshayskycf7bee52005-08-07 13:49:59 +040047 if (!res->flags)
48 return;
49
Bjorn Helgaascd8a4d32014-02-26 11:25:59 -070050 if (res->flags & IORESOURCE_UNSET)
51 return;
52
Ralf Baechlefb0f2b42006-12-19 13:12:08 -080053 /*
54 * Ignore non-moveable resources. This might be legacy resources for
55 * which no functional BAR register exists or another important
Bjorn Helgaas80ccba12008-06-13 10:52:11 -060056 * system resource we shouldn't move around.
Ralf Baechlefb0f2b42006-12-19 13:12:08 -080057 */
58 if (res->flags & IORESOURCE_PCI_FIXED)
59 return;
60
Yinghai Lufc279852013-12-09 22:54:40 -080061 pcibios_resource_to_bus(dev->bus, &region, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
64 if (res->flags & IORESOURCE_IO)
65 mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
66 else
67 mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
68
Yu Zhao613e7ed2008-11-22 02:41:27 +080069 reg = pci_resource_bar(dev, resno, &type);
70 if (!reg)
71 return;
72 if (type != pci_bar_unknown) {
Linus Torvalds755528c2005-08-26 10:49:22 -070073 if (!(res->flags & IORESOURCE_ROM_ENABLE))
74 return;
75 new |= PCI_ROM_ADDRESS_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77
Bjorn Helgaas9aac5372012-07-09 19:49:37 -060078 /*
79 * We can't update a 64-bit BAR atomically, so when possible,
80 * disable decoding so that a half-updated BAR won't conflict
81 * with another device.
82 */
83 disable = (res->flags & IORESOURCE_MEM_64) && !dev->mmio_always_on;
84 if (disable) {
85 pci_read_config_word(dev, PCI_COMMAND, &cmd);
86 pci_write_config_word(dev, PCI_COMMAND,
87 cmd & ~PCI_COMMAND_MEMORY);
88 }
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 pci_write_config_dword(dev, reg, new);
91 pci_read_config_dword(dev, reg, &check);
92
93 if ((new ^ check) & mask) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -060094 dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n",
95 resno, new, check);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97
Bjorn Helgaas28c68212011-06-14 13:04:35 -060098 if (res->flags & IORESOURCE_MEM_64) {
Ivan Kokshayskycf7bee52005-08-07 13:49:59 +040099 new = region.start >> 16 >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 pci_write_config_dword(dev, reg + 4, new);
101 pci_read_config_dword(dev, reg + 4, &check);
102 if (check != new) {
Ryan Desfosses227f0642014-04-18 20:13:50 -0400103 dev_err(&dev->dev, "BAR %d: error updating (high %#08x != %#08x)\n",
104 resno, new, check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106 }
Bjorn Helgaas9aac5372012-07-09 19:49:37 -0600107
108 if (disable)
109 pci_write_config_word(dev, PCI_COMMAND, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Sam Ravnborg96bde062007-03-26 21:53:30 -0800112int pci_claim_resource(struct pci_dev *dev, int resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 struct resource *res = &dev->resource[resource];
Bjorn Helgaas966f3a72010-03-11 17:01:19 -0700115 struct resource *root, *conflict;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Bjorn Helgaas29003be2014-02-26 11:25:59 -0700117 if (res->flags & IORESOURCE_UNSET) {
118 dev_info(&dev->dev, "can't claim BAR %d %pR: no address assigned\n",
119 resource, res);
120 return -EINVAL;
121 }
122
Matthew Wilcoxcebd78a2009-06-17 16:33:33 -0400123 root = pci_find_parent_resource(dev, res);
Bjorn Helgaas865df572009-11-04 10:32:57 -0700124 if (!root) {
Bjorn Helgaas29003be2014-02-26 11:25:59 -0700125 dev_info(&dev->dev, "can't claim BAR %d %pR: no compatible bridge window\n",
126 resource, res);
Bjorn Helgaasc770cb42015-03-12 12:30:06 -0500127 res->flags |= IORESOURCE_UNSET;
Bjorn Helgaas865df572009-11-04 10:32:57 -0700128 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130
Bjorn Helgaas966f3a72010-03-11 17:01:19 -0700131 conflict = request_resource_conflict(root, res);
132 if (conflict) {
Bjorn Helgaas29003be2014-02-26 11:25:59 -0700133 dev_info(&dev->dev, "can't claim BAR %d %pR: address conflict with %s %pR\n",
134 resource, res, conflict->name, conflict);
Bjorn Helgaasc770cb42015-03-12 12:30:06 -0500135 res->flags |= IORESOURCE_UNSET;
Bjorn Helgaas966f3a72010-03-11 17:01:19 -0700136 return -EBUSY;
137 }
Bjorn Helgaas865df572009-11-04 10:32:57 -0700138
Bjorn Helgaas966f3a72010-03-11 17:01:19 -0700139 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
Jesse Barneseaa959d2009-06-30 21:45:44 -0700141EXPORT_SYMBOL(pci_claim_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Yuji Shimada32a9a6822009-03-16 17:13:39 +0900143void pci_disable_bridge_window(struct pci_dev *dev)
144{
Bjorn Helgaas865df572009-11-04 10:32:57 -0700145 dev_info(&dev->dev, "disabling bridge mem windows\n");
Yuji Shimada32a9a6822009-03-16 17:13:39 +0900146
147 /* MMIO Base/Limit */
148 pci_write_config_dword(dev, PCI_MEMORY_BASE, 0x0000fff0);
149
150 /* Prefetchable MMIO Base/Limit */
151 pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
152 pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0x0000fff0);
153 pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0xffffffff);
154}
Ram Pai2bbc6942011-07-25 13:08:39 -0700155
Myron Stowe6535943f2011-11-21 11:54:19 -0700156/*
157 * Generic function that returns a value indicating that the device's
158 * original BIOS BAR address was not saved and so is not available for
159 * reinstatement.
160 *
161 * Can be over-ridden by architecture specific code that implements
162 * reinstatement functionality rather than leaving it disabled when
163 * normal allocation attempts fail.
164 */
165resource_size_t __weak pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx)
166{
167 return 0;
168}
169
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700170static int pci_revert_fw_address(struct resource *res, struct pci_dev *dev,
Ram Pai2bbc6942011-07-25 13:08:39 -0700171 int resno, resource_size_t size)
172{
173 struct resource *root, *conflict;
Myron Stowe6535943f2011-11-21 11:54:19 -0700174 resource_size_t fw_addr, start, end;
Ram Pai2bbc6942011-07-25 13:08:39 -0700175
Myron Stowe6535943f2011-11-21 11:54:19 -0700176 fw_addr = pcibios_retrieve_fw_addr(dev, resno);
177 if (!fw_addr)
Bjorn Helgaas94778832014-07-08 16:00:42 -0600178 return -ENOMEM;
Myron Stowe6535943f2011-11-21 11:54:19 -0700179
Ram Pai2bbc6942011-07-25 13:08:39 -0700180 start = res->start;
181 end = res->end;
Myron Stowe6535943f2011-11-21 11:54:19 -0700182 res->start = fw_addr;
Ram Pai2bbc6942011-07-25 13:08:39 -0700183 res->end = res->start + size - 1;
Bjorn Helgaas0b26cd62015-09-21 18:26:45 -0500184 res->flags &= ~IORESOURCE_UNSET;
Myron Stowe351fc6d2011-11-21 11:54:07 -0700185
186 root = pci_find_parent_resource(dev, res);
187 if (!root) {
188 if (res->flags & IORESOURCE_IO)
189 root = &ioport_resource;
190 else
191 root = &iomem_resource;
192 }
193
Ram Pai2bbc6942011-07-25 13:08:39 -0700194 dev_info(&dev->dev, "BAR %d: trying firmware assignment %pR\n",
195 resno, res);
196 conflict = request_resource_conflict(root, res);
197 if (conflict) {
Bjorn Helgaas94778832014-07-08 16:00:42 -0600198 dev_info(&dev->dev, "BAR %d: %pR conflicts with %s %pR\n",
199 resno, res, conflict->name, conflict);
Ram Pai2bbc6942011-07-25 13:08:39 -0700200 res->start = start;
201 res->end = end;
Bjorn Helgaas0b26cd62015-09-21 18:26:45 -0500202 res->flags |= IORESOURCE_UNSET;
Bjorn Helgaas94778832014-07-08 16:00:42 -0600203 return -EBUSY;
Ram Pai2bbc6942011-07-25 13:08:39 -0700204 }
Bjorn Helgaas94778832014-07-08 16:00:42 -0600205 return 0;
Ram Pai2bbc6942011-07-25 13:08:39 -0700206}
207
Bjorn Helgaasfe6dacd2012-07-11 17:05:43 -0600208static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,
209 int resno, resource_size_t size, resource_size_t align)
210{
211 struct resource *res = dev->resource + resno;
212 resource_size_t min;
213 int ret;
214
215 min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
216
Bjorn Helgaas67d29b52014-05-19 18:32:18 -0600217 /*
218 * First, try exact prefetching match. Even if a 64-bit
219 * prefetchable bridge window is below 4GB, we can't put a 32-bit
220 * prefetchable resource in it because pbus_size_mem() assumes a
221 * 64-bit window will contain no 32-bit resources. If we assign
222 * things differently than they were sized, not everything will fit.
223 */
Bjorn Helgaasfe6dacd2012-07-11 17:05:43 -0600224 ret = pci_bus_alloc_resource(bus, res, size, align, min,
Yinghai Lu5b285412014-05-19 17:01:55 -0600225 IORESOURCE_PREFETCH | IORESOURCE_MEM_64,
Bjorn Helgaasfe6dacd2012-07-11 17:05:43 -0600226 pcibios_align_resource, dev);
Bjorn Helgaasd3689df2014-05-19 18:39:07 -0600227 if (ret == 0)
228 return 0;
Bjorn Helgaasfe6dacd2012-07-11 17:05:43 -0600229
Bjorn Helgaas67d29b52014-05-19 18:32:18 -0600230 /*
231 * If the prefetchable window is only 32 bits wide, we can put
232 * 64-bit prefetchable resources in it.
233 */
Bjorn Helgaasd3689df2014-05-19 18:39:07 -0600234 if ((res->flags & (IORESOURCE_PREFETCH | IORESOURCE_MEM_64)) ==
Yinghai Lu5b285412014-05-19 17:01:55 -0600235 (IORESOURCE_PREFETCH | IORESOURCE_MEM_64)) {
Yinghai Lu5b285412014-05-19 17:01:55 -0600236 ret = pci_bus_alloc_resource(bus, res, size, align, min,
237 IORESOURCE_PREFETCH,
238 pcibios_align_resource, dev);
Bjorn Helgaasd3689df2014-05-19 18:39:07 -0600239 if (ret == 0)
240 return 0;
Yinghai Lu5b285412014-05-19 17:01:55 -0600241 }
242
Bjorn Helgaas67d29b52014-05-19 18:32:18 -0600243 /*
244 * If we didn't find a better match, we can put any memory resource
245 * in a non-prefetchable window. If this resource is 32 bits and
246 * non-prefetchable, the first call already tried the only possibility
247 * so we don't need to try again.
248 */
249 if (res->flags & (IORESOURCE_PREFETCH | IORESOURCE_MEM_64))
Bjorn Helgaasfe6dacd2012-07-11 17:05:43 -0600250 ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
251 pcibios_align_resource, dev);
Bjorn Helgaas67d29b52014-05-19 18:32:18 -0600252
Bjorn Helgaasfe6dacd2012-07-11 17:05:43 -0600253 return ret;
254}
255
Nikhil P Raod6776e62012-06-20 12:56:00 -0700256static int _pci_assign_resource(struct pci_dev *dev, int resno,
257 resource_size_t size, resource_size_t min_align)
Yinghai Lud09ee9682009-04-23 20:49:25 -0700258{
Yinghai Lud09ee9682009-04-23 20:49:25 -0700259 struct pci_bus *bus;
260 int ret;
261
Yinghai Lud09ee9682009-04-23 20:49:25 -0700262 bus = dev->bus;
Ram Pai2bbc6942011-07-25 13:08:39 -0700263 while ((ret = __pci_assign_resource(bus, dev, resno, size, min_align))) {
264 if (!bus->parent || !bus->self->transparent)
265 break;
266 bus = bus->parent;
Yinghai Lud09ee9682009-04-23 20:49:25 -0700267 }
268
Yinghai Lud09ee9682009-04-23 20:49:25 -0700269 return ret;
270}
271
Ram Pai2bbc6942011-07-25 13:08:39 -0700272int pci_assign_resource(struct pci_dev *dev, int resno)
273{
274 struct resource *res = dev->resource + resno;
275 resource_size_t align, size;
Ram Pai2bbc6942011-07-25 13:08:39 -0700276 int ret;
277
Bjorn Helgaas2ea4adf2016-03-01 10:58:04 -0600278 if (res->flags & IORESOURCE_PCI_FIXED)
279 return 0;
280
Bjorn Helgaasbd064f02014-02-26 11:25:58 -0700281 res->flags |= IORESOURCE_UNSET;
Ram Pai2bbc6942011-07-25 13:08:39 -0700282 align = pci_resource_alignment(dev, res);
283 if (!align) {
Ryan Desfosses227f0642014-04-18 20:13:50 -0400284 dev_info(&dev->dev, "BAR %d: can't assign %pR (bogus alignment)\n",
285 resno, res);
Ram Pai2bbc6942011-07-25 13:08:39 -0700286 return -EINVAL;
287 }
288
Ram Pai2bbc6942011-07-25 13:08:39 -0700289 size = resource_size(res);
290 ret = _pci_assign_resource(dev, resno, size, align);
291
292 /*
293 * If we failed to assign anything, let's try the address
294 * where firmware left it. That at least has a chance of
295 * working, which is better than just leaving it disabled.
296 */
Bjorn Helgaas64da4652014-07-08 16:04:22 -0600297 if (ret < 0) {
298 dev_info(&dev->dev, "BAR %d: no space for %pR\n", resno, res);
Ram Pai2bbc6942011-07-25 13:08:39 -0700299 ret = pci_revert_fw_address(res, dev, resno, size);
Bjorn Helgaas64da4652014-07-08 16:04:22 -0600300 }
Ram Pai2bbc6942011-07-25 13:08:39 -0700301
Bjorn Helgaas64da4652014-07-08 16:04:22 -0600302 if (ret < 0) {
303 dev_info(&dev->dev, "BAR %d: failed to assign %pR\n", resno,
304 res);
Bjorn Helgaas28f6dbe2014-07-04 15:58:15 -0600305 return ret;
Bjorn Helgaas64da4652014-07-08 16:04:22 -0600306 }
Bjorn Helgaas28f6dbe2014-07-04 15:58:15 -0600307
308 res->flags &= ~IORESOURCE_UNSET;
309 res->flags &= ~IORESOURCE_STARTALIGN;
310 dev_info(&dev->dev, "BAR %d: assigned %pR\n", resno, res);
311 if (resno < PCI_BRIDGE_RESOURCES)
312 pci_update_resource(dev, resno);
313
314 return 0;
Ram Pai2bbc6942011-07-25 13:08:39 -0700315}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600316EXPORT_SYMBOL(pci_assign_resource);
Ram Pai2bbc6942011-07-25 13:08:39 -0700317
Bjorn Helgaasfe6dacd2012-07-11 17:05:43 -0600318int pci_reassign_resource(struct pci_dev *dev, int resno, resource_size_t addsize,
319 resource_size_t min_align)
320{
321 struct resource *res = dev->resource + resno;
Guo Chaoc3337702014-07-03 18:30:29 -0600322 unsigned long flags;
Bjorn Helgaasfe6dacd2012-07-11 17:05:43 -0600323 resource_size_t new_size;
324 int ret;
325
Bjorn Helgaas2ea4adf2016-03-01 10:58:04 -0600326 if (res->flags & IORESOURCE_PCI_FIXED)
327 return 0;
328
Guo Chaoc3337702014-07-03 18:30:29 -0600329 flags = res->flags;
Bjorn Helgaasbd064f02014-02-26 11:25:58 -0700330 res->flags |= IORESOURCE_UNSET;
Bjorn Helgaasfe6dacd2012-07-11 17:05:43 -0600331 if (!res->parent) {
Ryan Desfosses227f0642014-04-18 20:13:50 -0400332 dev_info(&dev->dev, "BAR %d: can't reassign an unassigned resource %pR\n",
333 resno, res);
Bjorn Helgaasfe6dacd2012-07-11 17:05:43 -0600334 return -EINVAL;
335 }
336
337 /* already aligned with min_align */
338 new_size = resource_size(res) + addsize;
339 ret = _pci_assign_resource(dev, resno, new_size, min_align);
Bjorn Helgaas28f6dbe2014-07-04 15:58:15 -0600340 if (ret) {
Guo Chaoc3337702014-07-03 18:30:29 -0600341 res->flags = flags;
342 dev_info(&dev->dev, "BAR %d: %pR (failed to expand by %#llx)\n",
343 resno, res, (unsigned long long) addsize);
Bjorn Helgaas28f6dbe2014-07-04 15:58:15 -0600344 return ret;
Bjorn Helgaasfe6dacd2012-07-11 17:05:43 -0600345 }
Guo Chaoc3337702014-07-03 18:30:29 -0600346
Bjorn Helgaas28f6dbe2014-07-04 15:58:15 -0600347 res->flags &= ~IORESOURCE_UNSET;
348 res->flags &= ~IORESOURCE_STARTALIGN;
Bjorn Helgaas64da4652014-07-08 16:04:22 -0600349 dev_info(&dev->dev, "BAR %d: reassigned %pR (expanded by %#llx)\n",
350 resno, res, (unsigned long long) addsize);
Bjorn Helgaas28f6dbe2014-07-04 15:58:15 -0600351 if (resno < PCI_BRIDGE_RESOURCES)
352 pci_update_resource(dev, resno);
353
354 return 0;
Bjorn Helgaasfe6dacd2012-07-11 17:05:43 -0600355}
356
Bjorn Helgaas842de402008-03-04 11:56:47 -0700357int pci_enable_resources(struct pci_dev *dev, int mask)
358{
359 u16 cmd, old_cmd;
360 int i;
361 struct resource *r;
362
363 pci_read_config_word(dev, PCI_COMMAND, &cmd);
364 old_cmd = cmd;
365
366 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
367 if (!(mask & (1 << i)))
368 continue;
369
370 r = &dev->resource[i];
371
372 if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
373 continue;
374 if ((i == PCI_ROM_RESOURCE) &&
375 (!(r->flags & IORESOURCE_ROM_ENABLE)))
376 continue;
377
Bjorn Helgaas3cedcc32014-02-26 11:26:00 -0700378 if (r->flags & IORESOURCE_UNSET) {
379 dev_err(&dev->dev, "can't enable device: BAR %d %pR not assigned\n",
380 i, r);
381 return -EINVAL;
382 }
383
Bjorn Helgaas842de402008-03-04 11:56:47 -0700384 if (!r->parent) {
Bjorn Helgaas3cedcc32014-02-26 11:26:00 -0700385 dev_err(&dev->dev, "can't enable device: BAR %d %pR not claimed\n",
386 i, r);
Bjorn Helgaas842de402008-03-04 11:56:47 -0700387 return -EINVAL;
388 }
389
390 if (r->flags & IORESOURCE_IO)
391 cmd |= PCI_COMMAND_IO;
392 if (r->flags & IORESOURCE_MEM)
393 cmd |= PCI_COMMAND_MEMORY;
394 }
395
396 if (cmd != old_cmd) {
397 dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
398 old_cmd, cmd);
399 pci_write_config_word(dev, PCI_COMMAND, cmd);
400 }
401 return 0;
402}