1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/pci/setup-res.c
5 * Extruded from code written by
6 * Dave Rusling (david.rusling@reo.mts.dec.com)
7 * David Mosberger (davidm@cs.arizona.edu)
8 * David Miller (davem@redhat.com)
10 * Support routines for initializing a PCI subsystem.
13 /* fixed for multiple pci buses, 1999 Andrea Arcangeli <andrea@suse.de> */
16 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
20 #include <linux/kernel.h>
21 #include <linux/export.h>
22 #include <linux/pci.h>
23 #include <linux/errno.h>
24 #include <linux/ioport.h>
25 #include <linux/cache.h>
26 #include <linux/slab.h>
29 static void pci_std_update_resource(struct pci_dev
*dev
, int resno
)
31 struct pci_bus_region region
;
36 struct resource
*res
= dev
->resource
+ resno
;
38 /* Per SR-IOV spec 3.4.1.11, VF BARs are RO zero */
43 * Ignore resources for unimplemented BARs and unused resource slots
49 if (res
->flags
& IORESOURCE_UNSET
)
53 * Ignore non-moveable resources. This might be legacy resources for
54 * which no functional BAR register exists or another important
55 * system resource we shouldn't move around.
57 if (res
->flags
& IORESOURCE_PCI_FIXED
)
60 pcibios_resource_to_bus(dev
->bus
, ®ion
, res
);
63 if (res
->flags
& IORESOURCE_IO
) {
64 mask
= (u32
)PCI_BASE_ADDRESS_IO_MASK
;
65 new |= res
->flags
& ~PCI_BASE_ADDRESS_IO_MASK
;
66 } else if (resno
== PCI_ROM_RESOURCE
) {
67 mask
= PCI_ROM_ADDRESS_MASK
;
69 mask
= (u32
)PCI_BASE_ADDRESS_MEM_MASK
;
70 new |= res
->flags
& ~PCI_BASE_ADDRESS_MEM_MASK
;
73 if (resno
< PCI_ROM_RESOURCE
) {
74 reg
= PCI_BASE_ADDRESS_0
+ 4 * resno
;
75 } else if (resno
== PCI_ROM_RESOURCE
) {
78 * Apparently some Matrox devices have ROM BARs that read
79 * as zero when disabled, so don't update ROM BARs unless
80 * they're enabled. See https://lkml.org/lkml/2005/8/30/138.
82 if (!(res
->flags
& IORESOURCE_ROM_ENABLE
))
85 reg
= dev
->rom_base_reg
;
86 new |= PCI_ROM_ADDRESS_ENABLE
;
91 * We can't update a 64-bit BAR atomically, so when possible,
92 * disable decoding so that a half-updated BAR won't conflict
93 * with another device.
95 disable
= (res
->flags
& IORESOURCE_MEM_64
) && !dev
->mmio_always_on
;
97 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
98 pci_write_config_word(dev
, PCI_COMMAND
,
99 cmd
& ~PCI_COMMAND_MEMORY
);
102 pci_write_config_dword(dev
, reg
, new);
103 pci_read_config_dword(dev
, reg
, &check
);
105 if ((new ^ check
) & mask
) {
106 pci_err(dev
, "BAR %d: error updating (%#08x != %#08x)\n",
110 if (res
->flags
& IORESOURCE_MEM_64
) {
111 new = region
.start
>> 16 >> 16;
112 pci_write_config_dword(dev
, reg
+ 4, new);
113 pci_read_config_dword(dev
, reg
+ 4, &check
);
115 pci_err(dev
, "BAR %d: error updating (high %#08x != %#08x)\n",
121 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
124 void pci_update_resource(struct pci_dev
*dev
, int resno
)
126 if (resno
<= PCI_ROM_RESOURCE
)
127 pci_std_update_resource(dev
, resno
);
128 #ifdef CONFIG_PCI_IOV
129 else if (resno
>= PCI_IOV_RESOURCES
&& resno
<= PCI_IOV_RESOURCE_END
)
130 pci_iov_update_resource(dev
, resno
);
134 int pci_claim_resource(struct pci_dev
*dev
, int resource
)
136 struct resource
*res
= &dev
->resource
[resource
];
137 struct resource
*root
, *conflict
;
139 if (res
->flags
& IORESOURCE_UNSET
) {
140 pci_info(dev
, "can't claim BAR %d %pR: no address assigned\n",
146 * If we have a shadow copy in RAM, the PCI device doesn't respond
147 * to the shadow range, so we don't need to claim it, and upstream
148 * bridges don't need to route the range to the device.
150 if (res
->flags
& IORESOURCE_ROM_SHADOW
)
153 root
= pci_find_parent_resource(dev
, res
);
155 pci_info(dev
, "can't claim BAR %d %pR: no compatible bridge window\n",
157 res
->flags
|= IORESOURCE_UNSET
;
161 conflict
= request_resource_conflict(root
, res
);
163 pci_info(dev
, "can't claim BAR %d %pR: address conflict with %s %pR\n",
164 resource
, res
, conflict
->name
, conflict
);
165 res
->flags
|= IORESOURCE_UNSET
;
171 EXPORT_SYMBOL(pci_claim_resource
);
173 void pci_disable_bridge_window(struct pci_dev
*dev
)
175 pci_info(dev
, "disabling bridge mem windows\n");
177 /* MMIO Base/Limit */
178 pci_write_config_dword(dev
, PCI_MEMORY_BASE
, 0x0000fff0);
180 /* Prefetchable MMIO Base/Limit */
181 pci_write_config_dword(dev
, PCI_PREF_LIMIT_UPPER32
, 0);
182 pci_write_config_dword(dev
, PCI_PREF_MEMORY_BASE
, 0x0000fff0);
183 pci_write_config_dword(dev
, PCI_PREF_BASE_UPPER32
, 0xffffffff);
187 * Generic function that returns a value indicating that the device's
188 * original BIOS BAR address was not saved and so is not available for
191 * Can be over-ridden by architecture specific code that implements
192 * reinstatement functionality rather than leaving it disabled when
193 * normal allocation attempts fail.
195 resource_size_t __weak
pcibios_retrieve_fw_addr(struct pci_dev
*dev
, int idx
)
200 static int pci_revert_fw_address(struct resource
*res
, struct pci_dev
*dev
,
201 int resno
, resource_size_t size
)
203 struct resource
*root
, *conflict
;
204 resource_size_t fw_addr
, start
, end
;
206 fw_addr
= pcibios_retrieve_fw_addr(dev
, resno
);
212 res
->start
= fw_addr
;
213 res
->end
= res
->start
+ size
- 1;
214 res
->flags
&= ~IORESOURCE_UNSET
;
216 root
= pci_find_parent_resource(dev
, res
);
218 if (res
->flags
& IORESOURCE_IO
)
219 root
= &ioport_resource
;
221 root
= &iomem_resource
;
224 pci_info(dev
, "BAR %d: trying firmware assignment %pR\n",
226 conflict
= request_resource_conflict(root
, res
);
228 pci_info(dev
, "BAR %d: %pR conflicts with %s %pR\n",
229 resno
, res
, conflict
->name
, conflict
);
232 res
->flags
|= IORESOURCE_UNSET
;
239 * We don't have to worry about legacy ISA devices, so nothing to do here.
240 * This is marked as __weak because multiple architectures define it; it should
241 * eventually go away.
243 resource_size_t __weak
pcibios_align_resource(void *data
,
244 const struct resource
*res
,
245 resource_size_t size
,
246 resource_size_t align
)
251 static int __pci_assign_resource(struct pci_bus
*bus
, struct pci_dev
*dev
,
252 int resno
, resource_size_t size
, resource_size_t align
)
254 struct resource
*res
= dev
->resource
+ resno
;
258 min
= (res
->flags
& IORESOURCE_IO
) ? PCIBIOS_MIN_IO
: PCIBIOS_MIN_MEM
;
261 * First, try exact prefetching match. Even if a 64-bit
262 * prefetchable bridge window is below 4GB, we can't put a 32-bit
263 * prefetchable resource in it because pbus_size_mem() assumes a
264 * 64-bit window will contain no 32-bit resources. If we assign
265 * things differently than they were sized, not everything will fit.
267 ret
= pci_bus_alloc_resource(bus
, res
, size
, align
, min
,
268 IORESOURCE_PREFETCH
| IORESOURCE_MEM_64
,
269 pcibios_align_resource
, dev
);
274 * If the prefetchable window is only 32 bits wide, we can put
275 * 64-bit prefetchable resources in it.
277 if ((res
->flags
& (IORESOURCE_PREFETCH
| IORESOURCE_MEM_64
)) ==
278 (IORESOURCE_PREFETCH
| IORESOURCE_MEM_64
)) {
279 ret
= pci_bus_alloc_resource(bus
, res
, size
, align
, min
,
281 pcibios_align_resource
, dev
);
287 * If we didn't find a better match, we can put any memory resource
288 * in a non-prefetchable window. If this resource is 32 bits and
289 * non-prefetchable, the first call already tried the only possibility
290 * so we don't need to try again.
292 if (res
->flags
& (IORESOURCE_PREFETCH
| IORESOURCE_MEM_64
))
293 ret
= pci_bus_alloc_resource(bus
, res
, size
, align
, min
, 0,
294 pcibios_align_resource
, dev
);
299 static int _pci_assign_resource(struct pci_dev
*dev
, int resno
,
300 resource_size_t size
, resource_size_t min_align
)
306 while ((ret
= __pci_assign_resource(bus
, dev
, resno
, size
, min_align
))) {
307 if (!bus
->parent
|| !bus
->self
->transparent
)
315 int pci_assign_resource(struct pci_dev
*dev
, int resno
)
317 struct resource
*res
= dev
->resource
+ resno
;
318 resource_size_t align
, size
;
321 if (res
->flags
& IORESOURCE_PCI_FIXED
)
324 res
->flags
|= IORESOURCE_UNSET
;
325 align
= pci_resource_alignment(dev
, res
);
327 pci_info(dev
, "BAR %d: can't assign %pR (bogus alignment)\n",
332 size
= resource_size(res
);
333 ret
= _pci_assign_resource(dev
, resno
, size
, align
);
336 * If we failed to assign anything, let's try the address
337 * where firmware left it. That at least has a chance of
338 * working, which is better than just leaving it disabled.
341 pci_info(dev
, "BAR %d: no space for %pR\n", resno
, res
);
342 ret
= pci_revert_fw_address(res
, dev
, resno
, size
);
346 pci_info(dev
, "BAR %d: failed to assign %pR\n", resno
, res
);
350 res
->flags
&= ~IORESOURCE_UNSET
;
351 res
->flags
&= ~IORESOURCE_STARTALIGN
;
352 pci_info(dev
, "BAR %d: assigned %pR\n", resno
, res
);
353 if (resno
< PCI_BRIDGE_RESOURCES
)
354 pci_update_resource(dev
, resno
);
358 EXPORT_SYMBOL(pci_assign_resource
);
360 int pci_reassign_resource(struct pci_dev
*dev
, int resno
, resource_size_t addsize
,
361 resource_size_t min_align
)
363 struct resource
*res
= dev
->resource
+ resno
;
365 resource_size_t new_size
;
368 if (res
->flags
& IORESOURCE_PCI_FIXED
)
372 res
->flags
|= IORESOURCE_UNSET
;
374 pci_info(dev
, "BAR %d: can't reassign an unassigned resource %pR\n",
379 /* already aligned with min_align */
380 new_size
= resource_size(res
) + addsize
;
381 ret
= _pci_assign_resource(dev
, resno
, new_size
, min_align
);
384 pci_info(dev
, "BAR %d: %pR (failed to expand by %#llx)\n",
385 resno
, res
, (unsigned long long) addsize
);
389 res
->flags
&= ~IORESOURCE_UNSET
;
390 res
->flags
&= ~IORESOURCE_STARTALIGN
;
391 pci_info(dev
, "BAR %d: reassigned %pR (expanded by %#llx)\n",
392 resno
, res
, (unsigned long long) addsize
);
393 if (resno
< PCI_BRIDGE_RESOURCES
)
394 pci_update_resource(dev
, resno
);
399 void pci_release_resource(struct pci_dev
*dev
, int resno
)
401 struct resource
*res
= dev
->resource
+ resno
;
403 pci_info(dev
, "BAR %d: releasing %pR\n", resno
, res
);
408 release_resource(res
);
409 res
->end
= resource_size(res
) - 1;
411 res
->flags
|= IORESOURCE_UNSET
;
413 EXPORT_SYMBOL(pci_release_resource
);
415 int pci_resize_resource(struct pci_dev
*dev
, int resno
, int size
)
417 struct resource
*res
= dev
->resource
+ resno
;
422 /* Make sure the resource isn't assigned before resizing it. */
423 if (!(res
->flags
& IORESOURCE_UNSET
))
426 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
427 if (cmd
& PCI_COMMAND_MEMORY
)
430 sizes
= pci_rebar_get_possible_sizes(dev
, resno
);
434 if (!(sizes
& BIT(size
)))
437 old
= pci_rebar_get_current_size(dev
, resno
);
441 ret
= pci_rebar_set_size(dev
, resno
, size
);
445 res
->end
= res
->start
+ pci_rebar_size_to_bytes(size
) - 1;
447 /* Check if the new config works by trying to assign everything. */
448 ret
= pci_reassign_bridge_resources(dev
->bus
->self
, res
->flags
);
455 pci_rebar_set_size(dev
, resno
, old
);
456 res
->end
= res
->start
+ pci_rebar_size_to_bytes(old
) - 1;
459 EXPORT_SYMBOL(pci_resize_resource
);
461 int pci_enable_resources(struct pci_dev
*dev
, int mask
)
467 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
470 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
471 if (!(mask
& (1 << i
)))
474 r
= &dev
->resource
[i
];
476 if (!(r
->flags
& (IORESOURCE_IO
| IORESOURCE_MEM
)))
478 if ((i
== PCI_ROM_RESOURCE
) &&
479 (!(r
->flags
& IORESOURCE_ROM_ENABLE
)))
482 if (r
->flags
& IORESOURCE_UNSET
) {
483 pci_err(dev
, "can't enable device: BAR %d %pR not assigned\n",
489 pci_err(dev
, "can't enable device: BAR %d %pR not claimed\n",
494 if (r
->flags
& IORESOURCE_IO
)
495 cmd
|= PCI_COMMAND_IO
;
496 if (r
->flags
& IORESOURCE_MEM
)
497 cmd
|= PCI_COMMAND_MEMORY
;
500 if (cmd
!= old_cmd
) {
501 pci_info(dev
, "enabling device (%04x -> %04x)\n", old_cmd
, cmd
);
502 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);