1 // SPDX-License-Identifier: GPL-2.0
3 * Support routines for initializing a PCI subsystem
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 * Fixed for multiple PCI buses, 1999 Andrea Arcangeli <andrea@suse.de>
12 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
16 #include <linux/kernel.h>
17 #include <linux/export.h>
18 #include <linux/pci.h>
19 #include <linux/errno.h>
20 #include <linux/ioport.h>
21 #include <linux/cache.h>
22 #include <linux/slab.h>
25 static void pci_std_update_resource(struct pci_dev
*dev
, int resno
)
27 struct pci_bus_region region
;
32 struct resource
*res
= dev
->resource
+ resno
;
33 const char *res_name
= pci_resource_name(dev
, resno
);
35 /* Per SR-IOV spec 3.4.1.11, VF BARs are RO zero */
40 * Ignore resources for unimplemented BARs and unused resource slots
46 if (res
->flags
& IORESOURCE_UNSET
)
50 * Ignore non-moveable resources. This might be legacy resources for
51 * which no functional BAR register exists or another important
52 * system resource we shouldn't move around.
54 if (res
->flags
& IORESOURCE_PCI_FIXED
)
57 pcibios_resource_to_bus(dev
->bus
, ®ion
, res
);
60 if (res
->flags
& IORESOURCE_IO
) {
61 mask
= (u32
)PCI_BASE_ADDRESS_IO_MASK
;
62 new |= res
->flags
& ~PCI_BASE_ADDRESS_IO_MASK
;
63 } else if (resno
== PCI_ROM_RESOURCE
) {
64 mask
= PCI_ROM_ADDRESS_MASK
;
66 mask
= (u32
)PCI_BASE_ADDRESS_MEM_MASK
;
67 new |= res
->flags
& ~PCI_BASE_ADDRESS_MEM_MASK
;
70 if (resno
< PCI_ROM_RESOURCE
) {
71 reg
= PCI_BASE_ADDRESS_0
+ 4 * resno
;
72 } else if (resno
== PCI_ROM_RESOURCE
) {
75 * Apparently some Matrox devices have ROM BARs that read
76 * as zero when disabled, so don't update ROM BARs unless
77 * they're enabled. See
78 * https://lore.kernel.org/r/43147B3D.1030309@vc.cvut.cz/
79 * But we must update ROM BAR for buggy devices where even a
80 * disabled ROM can conflict with other BARs.
82 if (!(res
->flags
& IORESOURCE_ROM_ENABLE
) &&
83 !dev
->rom_bar_overlap
)
86 reg
= dev
->rom_base_reg
;
87 if (res
->flags
& IORESOURCE_ROM_ENABLE
)
88 new |= PCI_ROM_ADDRESS_ENABLE
;
93 * We can't update a 64-bit BAR atomically, so when possible,
94 * disable decoding so that a half-updated BAR won't conflict
95 * with another device.
97 disable
= (res
->flags
& IORESOURCE_MEM_64
) && !dev
->mmio_always_on
;
99 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
100 pci_write_config_word(dev
, PCI_COMMAND
,
101 cmd
& ~PCI_COMMAND_MEMORY
);
104 pci_write_config_dword(dev
, reg
, new);
105 pci_read_config_dword(dev
, reg
, &check
);
107 if ((new ^ check
) & mask
) {
108 pci_err(dev
, "%s: error updating (%#010x != %#010x)\n",
109 res_name
, new, check
);
112 if (res
->flags
& IORESOURCE_MEM_64
) {
113 new = region
.start
>> 16 >> 16;
114 pci_write_config_dword(dev
, reg
+ 4, new);
115 pci_read_config_dword(dev
, reg
+ 4, &check
);
117 pci_err(dev
, "%s: error updating (high %#010x != %#010x)\n",
118 res_name
, new, check
);
123 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
126 void pci_update_resource(struct pci_dev
*dev
, int resno
)
128 if (resno
<= PCI_ROM_RESOURCE
)
129 pci_std_update_resource(dev
, resno
);
130 #ifdef CONFIG_PCI_IOV
131 else if (resno
>= PCI_IOV_RESOURCES
&& resno
<= PCI_IOV_RESOURCE_END
)
132 pci_iov_update_resource(dev
, resno
);
136 int pci_claim_resource(struct pci_dev
*dev
, int resource
)
138 struct resource
*res
= &dev
->resource
[resource
];
139 const char *res_name
= pci_resource_name(dev
, resource
);
140 struct resource
*root
, *conflict
;
142 if (res
->flags
& IORESOURCE_UNSET
) {
143 pci_info(dev
, "%s %pR: can't claim; no address assigned\n",
149 * If we have a shadow copy in RAM, the PCI device doesn't respond
150 * to the shadow range, so we don't need to claim it, and upstream
151 * bridges don't need to route the range to the device.
153 if (res
->flags
& IORESOURCE_ROM_SHADOW
)
156 root
= pci_find_parent_resource(dev
, res
);
158 pci_info(dev
, "%s %pR: can't claim; no compatible bridge window\n",
160 res
->flags
|= IORESOURCE_UNSET
;
164 conflict
= request_resource_conflict(root
, res
);
166 pci_info(dev
, "%s %pR: can't claim; address conflict with %s %pR\n",
167 res_name
, res
, conflict
->name
, conflict
);
168 res
->flags
|= IORESOURCE_UNSET
;
174 EXPORT_SYMBOL(pci_claim_resource
);
176 void pci_disable_bridge_window(struct pci_dev
*dev
)
178 /* MMIO Base/Limit */
179 pci_write_config_dword(dev
, PCI_MEMORY_BASE
, 0x0000fff0);
181 /* Prefetchable MMIO Base/Limit */
182 pci_write_config_dword(dev
, PCI_PREF_LIMIT_UPPER32
, 0);
183 pci_write_config_dword(dev
, PCI_PREF_MEMORY_BASE
, 0x0000fff0);
184 pci_write_config_dword(dev
, PCI_PREF_BASE_UPPER32
, 0xffffffff);
188 * Generic function that returns a value indicating that the device's
189 * original BIOS BAR address was not saved and so is not available for
192 * Can be over-ridden by architecture specific code that implements
193 * reinstatement functionality rather than leaving it disabled when
194 * normal allocation attempts fail.
196 resource_size_t __weak
pcibios_retrieve_fw_addr(struct pci_dev
*dev
, int idx
)
201 static int pci_revert_fw_address(struct resource
*res
, struct pci_dev
*dev
,
202 int resno
, resource_size_t size
)
204 struct resource
*root
, *conflict
;
205 resource_size_t fw_addr
, start
, end
;
206 const char *res_name
= pci_resource_name(dev
, resno
);
208 fw_addr
= pcibios_retrieve_fw_addr(dev
, resno
);
214 resource_set_range(res
, fw_addr
, size
);
215 res
->flags
&= ~IORESOURCE_UNSET
;
217 root
= pci_find_parent_resource(dev
, res
);
220 * If dev is behind a bridge, accesses will only reach it
221 * if res is inside the relevant bridge window.
223 if (pci_upstream_bridge(dev
))
227 * On the root bus, assume the host bridge will forward
230 if (res
->flags
& IORESOURCE_IO
)
231 root
= &ioport_resource
;
233 root
= &iomem_resource
;
236 pci_info(dev
, "%s: trying firmware assignment %pR\n", res_name
, res
);
237 conflict
= request_resource_conflict(root
, res
);
239 pci_info(dev
, "%s %pR: conflicts with %s %pR\n", res_name
, res
,
240 conflict
->name
, conflict
);
243 res
->flags
|= IORESOURCE_UNSET
;
250 * We don't have to worry about legacy ISA devices, so nothing to do here.
251 * This is marked as __weak because multiple architectures define it; it should
252 * eventually go away.
254 resource_size_t __weak
pcibios_align_resource(void *data
,
255 const struct resource
*res
,
256 resource_size_t size
,
257 resource_size_t align
)
262 static int __pci_assign_resource(struct pci_bus
*bus
, struct pci_dev
*dev
,
263 int resno
, resource_size_t size
, resource_size_t align
)
265 struct resource
*res
= dev
->resource
+ resno
;
269 min
= (res
->flags
& IORESOURCE_IO
) ? PCIBIOS_MIN_IO
: PCIBIOS_MIN_MEM
;
272 * First, try exact prefetching match. Even if a 64-bit
273 * prefetchable bridge window is below 4GB, we can't put a 32-bit
274 * prefetchable resource in it because pbus_size_mem() assumes a
275 * 64-bit window will contain no 32-bit resources. If we assign
276 * things differently than they were sized, not everything will fit.
278 ret
= pci_bus_alloc_resource(bus
, res
, size
, align
, min
,
279 IORESOURCE_PREFETCH
| IORESOURCE_MEM_64
,
280 pcibios_align_resource
, dev
);
285 * If the prefetchable window is only 32 bits wide, we can put
286 * 64-bit prefetchable resources in it.
288 if ((res
->flags
& (IORESOURCE_PREFETCH
| IORESOURCE_MEM_64
)) ==
289 (IORESOURCE_PREFETCH
| IORESOURCE_MEM_64
)) {
290 ret
= pci_bus_alloc_resource(bus
, res
, size
, align
, min
,
292 pcibios_align_resource
, dev
);
298 * If we didn't find a better match, we can put any memory resource
299 * in a non-prefetchable window. If this resource is 32 bits and
300 * non-prefetchable, the first call already tried the only possibility
301 * so we don't need to try again.
303 if (res
->flags
& (IORESOURCE_PREFETCH
| IORESOURCE_MEM_64
))
304 ret
= pci_bus_alloc_resource(bus
, res
, size
, align
, min
, 0,
305 pcibios_align_resource
, dev
);
310 static int _pci_assign_resource(struct pci_dev
*dev
, int resno
,
311 resource_size_t size
, resource_size_t min_align
)
317 while ((ret
= __pci_assign_resource(bus
, dev
, resno
, size
, min_align
))) {
318 if (!bus
->parent
|| !bus
->self
->transparent
)
326 int pci_assign_resource(struct pci_dev
*dev
, int resno
)
328 struct resource
*res
= dev
->resource
+ resno
;
329 const char *res_name
= pci_resource_name(dev
, resno
);
330 resource_size_t align
, size
;
333 if (res
->flags
& IORESOURCE_PCI_FIXED
)
336 res
->flags
|= IORESOURCE_UNSET
;
337 align
= pci_resource_alignment(dev
, res
);
339 pci_info(dev
, "%s %pR: can't assign; bogus alignment\n",
344 size
= resource_size(res
);
345 ret
= _pci_assign_resource(dev
, resno
, size
, align
);
348 * If we failed to assign anything, let's try the address
349 * where firmware left it. That at least has a chance of
350 * working, which is better than just leaving it disabled.
353 pci_info(dev
, "%s %pR: can't assign; no space\n", res_name
, res
);
354 ret
= pci_revert_fw_address(res
, dev
, resno
, size
);
358 pci_info(dev
, "%s %pR: failed to assign\n", res_name
, res
);
362 res
->flags
&= ~IORESOURCE_UNSET
;
363 res
->flags
&= ~IORESOURCE_STARTALIGN
;
364 pci_info(dev
, "%s %pR: assigned\n", res_name
, res
);
365 if (resno
< PCI_BRIDGE_RESOURCES
)
366 pci_update_resource(dev
, resno
);
370 EXPORT_SYMBOL(pci_assign_resource
);
372 int pci_reassign_resource(struct pci_dev
*dev
, int resno
,
373 resource_size_t addsize
, resource_size_t min_align
)
375 struct resource
*res
= dev
->resource
+ resno
;
376 const char *res_name
= pci_resource_name(dev
, resno
);
378 resource_size_t new_size
;
381 if (res
->flags
& IORESOURCE_PCI_FIXED
)
385 res
->flags
|= IORESOURCE_UNSET
;
387 pci_info(dev
, "%s %pR: can't reassign; unassigned resource\n",
392 /* already aligned with min_align */
393 new_size
= resource_size(res
) + addsize
;
394 ret
= _pci_assign_resource(dev
, resno
, new_size
, min_align
);
397 pci_info(dev
, "%s %pR: failed to expand by %#llx\n",
398 res_name
, res
, (unsigned long long) addsize
);
402 res
->flags
&= ~IORESOURCE_UNSET
;
403 res
->flags
&= ~IORESOURCE_STARTALIGN
;
404 pci_info(dev
, "%s %pR: reassigned; expanded by %#llx\n",
405 res_name
, res
, (unsigned long long) addsize
);
406 if (resno
< PCI_BRIDGE_RESOURCES
)
407 pci_update_resource(dev
, resno
);
412 void pci_release_resource(struct pci_dev
*dev
, int resno
)
414 struct resource
*res
= dev
->resource
+ resno
;
415 const char *res_name
= pci_resource_name(dev
, resno
);
417 pci_info(dev
, "%s %pR: releasing\n", res_name
, res
);
422 release_resource(res
);
423 res
->end
= resource_size(res
) - 1;
425 res
->flags
|= IORESOURCE_UNSET
;
427 EXPORT_SYMBOL(pci_release_resource
);
429 int pci_resize_resource(struct pci_dev
*dev
, int resno
, int size
)
431 struct resource
*res
= dev
->resource
+ resno
;
432 struct pci_host_bridge
*host
;
437 /* Check if we must preserve the firmware's resource assignment */
438 host
= pci_find_host_bridge(dev
->bus
);
439 if (host
->preserve_config
)
442 /* Make sure the resource isn't assigned before resizing it. */
443 if (!(res
->flags
& IORESOURCE_UNSET
))
446 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
447 if (cmd
& PCI_COMMAND_MEMORY
)
450 sizes
= pci_rebar_get_possible_sizes(dev
, resno
);
454 if (!(sizes
& BIT(size
)))
457 old
= pci_rebar_get_current_size(dev
, resno
);
461 ret
= pci_rebar_set_size(dev
, resno
, size
);
465 resource_set_size(res
, pci_rebar_size_to_bytes(size
));
467 /* Check if the new config works by trying to assign everything. */
468 if (dev
->bus
->self
) {
469 ret
= pci_reassign_bridge_resources(dev
->bus
->self
, res
->flags
);
476 pci_rebar_set_size(dev
, resno
, old
);
477 resource_set_size(res
, pci_rebar_size_to_bytes(old
));
480 EXPORT_SYMBOL(pci_resize_resource
);
482 int pci_enable_resources(struct pci_dev
*dev
, int mask
)
489 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
492 pci_dev_for_each_resource(dev
, r
, i
) {
493 if (!(mask
& (1 << i
)))
496 r_name
= pci_resource_name(dev
, i
);
498 if (!(r
->flags
& (IORESOURCE_IO
| IORESOURCE_MEM
)))
500 if ((i
== PCI_ROM_RESOURCE
) &&
501 (!(r
->flags
& IORESOURCE_ROM_ENABLE
)))
504 if (r
->flags
& IORESOURCE_UNSET
) {
505 pci_err(dev
, "%s %pR: not assigned; can't enable device\n",
511 pci_err(dev
, "%s %pR: not claimed; can't enable device\n",
516 if (r
->flags
& IORESOURCE_IO
)
517 cmd
|= PCI_COMMAND_IO
;
518 if (r
->flags
& IORESOURCE_MEM
)
519 cmd
|= PCI_COMMAND_MEMORY
;
522 if (cmd
!= old_cmd
) {
523 pci_info(dev
, "enabling device (%04x -> %04x)\n", old_cmd
, cmd
);
524 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);