1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* |
3 | * Support routines for initializing a PCI subsystem |
4 | * |
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) |
9 | * |
10 | * Fixed for multiple PCI buses, 1999 Andrea Arcangeli <andrea@suse.de> |
11 | * |
12 | * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru> |
13 | * Resource sorting |
14 | */ |
15 | |
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> |
23 | #include "pci.h" |
24 | |
25 | static void pci_std_update_resource(struct pci_dev *dev, int resno) |
26 | { |
27 | struct pci_bus_region region; |
28 | bool disable; |
29 | u16 cmd; |
30 | u32 new, check, mask; |
31 | int reg; |
32 | struct resource *res = pci_resource_n(dev, resno); |
33 | const char *res_name = pci_resource_name(dev, i: resno); |
34 | |
35 | /* Per SR-IOV spec 3.4.1.11, VF BARs are RO zero */ |
36 | if (dev->is_virtfn) |
37 | return; |
38 | |
39 | /* |
40 | * Ignore resources for unimplemented BARs and unused resource slots |
41 | * for 64 bit BARs. |
42 | */ |
43 | if (!res->flags) |
44 | return; |
45 | |
46 | if (res->flags & IORESOURCE_UNSET) |
47 | return; |
48 | |
49 | /* |
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. |
53 | */ |
54 | if (res->flags & IORESOURCE_PCI_FIXED) |
55 | return; |
56 | |
57 | pcibios_resource_to_bus(bus: dev->bus, region: ®ion, res); |
58 | new = region.start; |
59 | |
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; |
65 | } else { |
66 | mask = (u32)PCI_BASE_ADDRESS_MEM_MASK; |
67 | new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK; |
68 | } |
69 | |
70 | if (resno < PCI_ROM_RESOURCE) { |
71 | reg = PCI_BASE_ADDRESS_0 + 4 * resno; |
72 | } else if (resno == PCI_ROM_RESOURCE) { |
73 | |
74 | /* |
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. |
81 | */ |
82 | if (!(res->flags & IORESOURCE_ROM_ENABLE) && |
83 | !dev->rom_bar_overlap) |
84 | return; |
85 | |
86 | reg = dev->rom_base_reg; |
87 | if (res->flags & IORESOURCE_ROM_ENABLE) |
88 | new |= PCI_ROM_ADDRESS_ENABLE; |
89 | } else |
90 | return; |
91 | |
92 | /* |
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. |
96 | */ |
97 | disable = (res->flags & IORESOURCE_MEM_64) && !dev->mmio_always_on; |
98 | if (disable) { |
99 | pci_read_config_word(dev, PCI_COMMAND, val: &cmd); |
100 | pci_write_config_word(dev, PCI_COMMAND, |
101 | val: cmd & ~PCI_COMMAND_MEMORY); |
102 | } |
103 | |
104 | pci_write_config_dword(dev, where: reg, val: new); |
105 | pci_read_config_dword(dev, where: reg, val: &check); |
106 | |
107 | if ((new ^ check) & mask) { |
108 | pci_err(dev, "%s: error updating (%#010x != %#010x)\n" , |
109 | res_name, new, check); |
110 | } |
111 | |
112 | if (res->flags & IORESOURCE_MEM_64) { |
113 | new = region.start >> 16 >> 16; |
114 | pci_write_config_dword(dev, where: reg + 4, val: new); |
115 | pci_read_config_dword(dev, where: reg + 4, val: &check); |
116 | if (check != new) { |
117 | pci_err(dev, "%s: error updating (high %#010x != %#010x)\n" , |
118 | res_name, new, check); |
119 | } |
120 | } |
121 | |
122 | if (disable) |
123 | pci_write_config_word(dev, PCI_COMMAND, val: cmd); |
124 | } |
125 | |
126 | void pci_update_resource(struct pci_dev *dev, int resno) |
127 | { |
128 | if (resno <= PCI_ROM_RESOURCE) |
129 | pci_std_update_resource(dev, resno); |
130 | else if (pci_resource_is_iov(resno)) |
131 | pci_iov_update_resource(dev, resno); |
132 | } |
133 | |
134 | int pci_claim_resource(struct pci_dev *dev, int resource) |
135 | { |
136 | struct resource *res = &dev->resource[resource]; |
137 | const char *res_name = pci_resource_name(dev, i: resource); |
138 | struct resource *root, *conflict; |
139 | |
140 | if (res->flags & IORESOURCE_UNSET) { |
141 | pci_info(dev, "%s %pR: can't claim; no address assigned\n" , |
142 | res_name, res); |
143 | return -EINVAL; |
144 | } |
145 | |
146 | /* |
147 | * If we have a shadow copy in RAM, the PCI device doesn't respond |
148 | * to the shadow range, so we don't need to claim it, and upstream |
149 | * bridges don't need to route the range to the device. |
150 | */ |
151 | if (res->flags & IORESOURCE_ROM_SHADOW) |
152 | return 0; |
153 | |
154 | root = pci_find_parent_resource(dev, res); |
155 | if (!root) { |
156 | pci_info(dev, "%s %pR: can't claim; no compatible bridge window\n" , |
157 | res_name, res); |
158 | res->flags |= IORESOURCE_UNSET; |
159 | return -EINVAL; |
160 | } |
161 | |
162 | conflict = request_resource_conflict(root, new: res); |
163 | if (conflict) { |
164 | pci_info(dev, "%s %pR: can't claim; address conflict with %s %pR\n" , |
165 | res_name, res, conflict->name, conflict); |
166 | res->flags |= IORESOURCE_UNSET; |
167 | return -EBUSY; |
168 | } |
169 | |
170 | return 0; |
171 | } |
172 | EXPORT_SYMBOL(pci_claim_resource); |
173 | |
174 | void pci_disable_bridge_window(struct pci_dev *dev) |
175 | { |
176 | /* MMIO Base/Limit */ |
177 | pci_write_config_dword(dev, PCI_MEMORY_BASE, val: 0x0000fff0); |
178 | |
179 | /* Prefetchable MMIO Base/Limit */ |
180 | pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, val: 0); |
181 | pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, val: 0x0000fff0); |
182 | pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, val: 0xffffffff); |
183 | } |
184 | |
185 | /* |
186 | * Generic function that returns a value indicating that the device's |
187 | * original BIOS BAR address was not saved and so is not available for |
188 | * reinstatement. |
189 | * |
190 | * Can be over-ridden by architecture specific code that implements |
191 | * reinstatement functionality rather than leaving it disabled when |
192 | * normal allocation attempts fail. |
193 | */ |
194 | resource_size_t __weak pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx) |
195 | { |
196 | return 0; |
197 | } |
198 | |
199 | static int pci_revert_fw_address(struct resource *res, struct pci_dev *dev, |
200 | int resno, resource_size_t size) |
201 | { |
202 | struct resource *root, *conflict; |
203 | resource_size_t fw_addr, start, end; |
204 | const char *res_name = pci_resource_name(dev, i: resno); |
205 | |
206 | fw_addr = pcibios_retrieve_fw_addr(dev, idx: resno); |
207 | if (!fw_addr) |
208 | return -ENOMEM; |
209 | |
210 | start = res->start; |
211 | end = res->end; |
212 | resource_set_range(res, start: fw_addr, size); |
213 | res->flags &= ~IORESOURCE_UNSET; |
214 | |
215 | root = pci_find_parent_resource(dev, res); |
216 | if (!root) { |
217 | /* |
218 | * If dev is behind a bridge, accesses will only reach it |
219 | * if res is inside the relevant bridge window. |
220 | */ |
221 | if (pci_upstream_bridge(dev)) |
222 | return -ENXIO; |
223 | |
224 | /* |
225 | * On the root bus, assume the host bridge will forward |
226 | * everything. |
227 | */ |
228 | if (res->flags & IORESOURCE_IO) |
229 | root = &ioport_resource; |
230 | else |
231 | root = &iomem_resource; |
232 | } |
233 | |
234 | pci_info(dev, "%s: trying firmware assignment %pR\n" , res_name, res); |
235 | conflict = request_resource_conflict(root, new: res); |
236 | if (conflict) { |
237 | pci_info(dev, "%s %pR: conflicts with %s %pR\n" , res_name, res, |
238 | conflict->name, conflict); |
239 | res->start = start; |
240 | res->end = end; |
241 | res->flags |= IORESOURCE_UNSET; |
242 | return -EBUSY; |
243 | } |
244 | return 0; |
245 | } |
246 | |
247 | /* |
248 | * We don't have to worry about legacy ISA devices, so nothing to do here. |
249 | * This is marked as __weak because multiple architectures define it; it should |
250 | * eventually go away. |
251 | */ |
252 | resource_size_t __weak pcibios_align_resource(void *data, |
253 | const struct resource *res, |
254 | resource_size_t size, |
255 | resource_size_t align) |
256 | { |
257 | return res->start; |
258 | } |
259 | |
260 | static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev, |
261 | int resno, resource_size_t size, resource_size_t align) |
262 | { |
263 | struct resource *res = pci_resource_n(dev, resno); |
264 | resource_size_t min; |
265 | int ret; |
266 | |
267 | min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM; |
268 | |
269 | /* |
270 | * First, try exact prefetching match. Even if a 64-bit |
271 | * prefetchable bridge window is below 4GB, we can't put a 32-bit |
272 | * prefetchable resource in it because pbus_size_mem() assumes a |
273 | * 64-bit window will contain no 32-bit resources. If we assign |
274 | * things differently than they were sized, not everything will fit. |
275 | */ |
276 | ret = pci_bus_alloc_resource(bus, res, size, align, min, |
277 | IORESOURCE_PREFETCH | IORESOURCE_MEM_64, |
278 | alignf: pcibios_align_resource, alignf_data: dev); |
279 | if (ret == 0) |
280 | return 0; |
281 | |
282 | /* |
283 | * If the prefetchable window is only 32 bits wide, we can put |
284 | * 64-bit prefetchable resources in it. |
285 | */ |
286 | if ((res->flags & (IORESOURCE_PREFETCH | IORESOURCE_MEM_64)) == |
287 | (IORESOURCE_PREFETCH | IORESOURCE_MEM_64)) { |
288 | ret = pci_bus_alloc_resource(bus, res, size, align, min, |
289 | IORESOURCE_PREFETCH, |
290 | alignf: pcibios_align_resource, alignf_data: dev); |
291 | if (ret == 0) |
292 | return 0; |
293 | } |
294 | |
295 | /* |
296 | * If we didn't find a better match, we can put any memory resource |
297 | * in a non-prefetchable window. If this resource is 32 bits and |
298 | * non-prefetchable, the first call already tried the only possibility |
299 | * so we don't need to try again. |
300 | */ |
301 | if (res->flags & (IORESOURCE_PREFETCH | IORESOURCE_MEM_64)) |
302 | ret = pci_bus_alloc_resource(bus, res, size, align, min, type_mask: 0, |
303 | alignf: pcibios_align_resource, alignf_data: dev); |
304 | |
305 | return ret; |
306 | } |
307 | |
308 | static int _pci_assign_resource(struct pci_dev *dev, int resno, |
309 | resource_size_t size, resource_size_t min_align) |
310 | { |
311 | struct pci_bus *bus; |
312 | int ret; |
313 | |
314 | bus = dev->bus; |
315 | while ((ret = __pci_assign_resource(bus, dev, resno, size, align: min_align))) { |
316 | if (!bus->parent || !bus->self->transparent) |
317 | break; |
318 | bus = bus->parent; |
319 | } |
320 | |
321 | return ret; |
322 | } |
323 | |
324 | int pci_assign_resource(struct pci_dev *dev, int resno) |
325 | { |
326 | struct resource *res = pci_resource_n(dev, resno); |
327 | const char *res_name = pci_resource_name(dev, i: resno); |
328 | resource_size_t align, size; |
329 | int ret; |
330 | |
331 | if (res->flags & IORESOURCE_PCI_FIXED) |
332 | return 0; |
333 | |
334 | res->flags |= IORESOURCE_UNSET; |
335 | align = pci_resource_alignment(dev, res); |
336 | if (!align) { |
337 | pci_info(dev, "%s %pR: can't assign; bogus alignment\n" , |
338 | res_name, res); |
339 | return -EINVAL; |
340 | } |
341 | |
342 | size = resource_size(res); |
343 | ret = _pci_assign_resource(dev, resno, size, min_align: align); |
344 | |
345 | /* |
346 | * If we failed to assign anything, let's try the address |
347 | * where firmware left it. That at least has a chance of |
348 | * working, which is better than just leaving it disabled. |
349 | */ |
350 | if (ret < 0) { |
351 | pci_info(dev, "%s %pR: can't assign; no space\n" , res_name, res); |
352 | ret = pci_revert_fw_address(res, dev, resno, size); |
353 | } |
354 | |
355 | if (ret < 0) { |
356 | pci_info(dev, "%s %pR: failed to assign\n" , res_name, res); |
357 | return ret; |
358 | } |
359 | |
360 | res->flags &= ~IORESOURCE_UNSET; |
361 | res->flags &= ~IORESOURCE_STARTALIGN; |
362 | pci_info(dev, "%s %pR: assigned\n" , res_name, res); |
363 | if (resno < PCI_BRIDGE_RESOURCES) |
364 | pci_update_resource(dev, resno); |
365 | |
366 | return 0; |
367 | } |
368 | EXPORT_SYMBOL(pci_assign_resource); |
369 | |
370 | int pci_reassign_resource(struct pci_dev *dev, int resno, |
371 | resource_size_t addsize, resource_size_t min_align) |
372 | { |
373 | struct resource *res = pci_resource_n(dev, resno); |
374 | const char *res_name = pci_resource_name(dev, i: resno); |
375 | unsigned long flags; |
376 | resource_size_t new_size; |
377 | int ret; |
378 | |
379 | if (res->flags & IORESOURCE_PCI_FIXED) |
380 | return 0; |
381 | |
382 | flags = res->flags; |
383 | res->flags |= IORESOURCE_UNSET; |
384 | if (!res->parent) { |
385 | pci_info(dev, "%s %pR: can't reassign; unassigned resource\n" , |
386 | res_name, res); |
387 | return -EINVAL; |
388 | } |
389 | |
390 | new_size = resource_size(res) + addsize; |
391 | ret = _pci_assign_resource(dev, resno, size: new_size, min_align); |
392 | if (ret) { |
393 | res->flags = flags; |
394 | pci_info(dev, "%s %pR: failed to expand by %#llx\n" , |
395 | res_name, res, (unsigned long long) addsize); |
396 | return ret; |
397 | } |
398 | |
399 | res->flags &= ~IORESOURCE_UNSET; |
400 | res->flags &= ~IORESOURCE_STARTALIGN; |
401 | pci_info(dev, "%s %pR: reassigned; expanded by %#llx\n" , |
402 | res_name, res, (unsigned long long) addsize); |
403 | if (resno < PCI_BRIDGE_RESOURCES) |
404 | pci_update_resource(dev, resno); |
405 | |
406 | return 0; |
407 | } |
408 | |
409 | void pci_release_resource(struct pci_dev *dev, int resno) |
410 | { |
411 | struct resource *res = pci_resource_n(dev, resno); |
412 | const char *res_name = pci_resource_name(dev, i: resno); |
413 | |
414 | if (!res->parent) |
415 | return; |
416 | |
417 | pci_info(dev, "%s %pR: releasing\n" , res_name, res); |
418 | |
419 | release_resource(new: res); |
420 | res->end = resource_size(res) - 1; |
421 | res->start = 0; |
422 | res->flags |= IORESOURCE_UNSET; |
423 | } |
424 | EXPORT_SYMBOL(pci_release_resource); |
425 | |
426 | int pci_resize_resource(struct pci_dev *dev, int resno, int size) |
427 | { |
428 | struct resource *res = pci_resource_n(dev, resno); |
429 | struct pci_host_bridge *host; |
430 | int old, ret; |
431 | u32 sizes; |
432 | u16 cmd; |
433 | |
434 | /* Check if we must preserve the firmware's resource assignment */ |
435 | host = pci_find_host_bridge(bus: dev->bus); |
436 | if (host->preserve_config) |
437 | return -ENOTSUPP; |
438 | |
439 | /* Make sure the resource isn't assigned before resizing it. */ |
440 | if (!(res->flags & IORESOURCE_UNSET)) |
441 | return -EBUSY; |
442 | |
443 | pci_read_config_word(dev, PCI_COMMAND, val: &cmd); |
444 | if (cmd & PCI_COMMAND_MEMORY) |
445 | return -EBUSY; |
446 | |
447 | sizes = pci_rebar_get_possible_sizes(pdev: dev, bar: resno); |
448 | if (!sizes) |
449 | return -ENOTSUPP; |
450 | |
451 | if (!(sizes & BIT(size))) |
452 | return -EINVAL; |
453 | |
454 | old = pci_rebar_get_current_size(pdev: dev, bar: resno); |
455 | if (old < 0) |
456 | return old; |
457 | |
458 | ret = pci_rebar_set_size(pdev: dev, bar: resno, size); |
459 | if (ret) |
460 | return ret; |
461 | |
462 | resource_set_size(res, size: pci_rebar_size_to_bytes(size)); |
463 | |
464 | /* Check if the new config works by trying to assign everything. */ |
465 | if (dev->bus->self) { |
466 | ret = pci_reassign_bridge_resources(bridge: dev->bus->self, type: res->flags); |
467 | if (ret) |
468 | goto error_resize; |
469 | } |
470 | return 0; |
471 | |
472 | error_resize: |
473 | pci_rebar_set_size(pdev: dev, bar: resno, size: old); |
474 | resource_set_size(res, size: pci_rebar_size_to_bytes(size: old)); |
475 | return ret; |
476 | } |
477 | EXPORT_SYMBOL(pci_resize_resource); |
478 | |
479 | int pci_enable_resources(struct pci_dev *dev, int mask) |
480 | { |
481 | u16 cmd, old_cmd; |
482 | int i; |
483 | struct resource *r; |
484 | const char *r_name; |
485 | |
486 | pci_read_config_word(dev, PCI_COMMAND, val: &cmd); |
487 | old_cmd = cmd; |
488 | |
489 | pci_dev_for_each_resource(dev, r, i) { |
490 | if (!(mask & (1 << i))) |
491 | continue; |
492 | |
493 | r_name = pci_resource_name(dev, i); |
494 | |
495 | if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM))) |
496 | continue; |
497 | if (pci_resource_is_optional(dev, resno: i)) |
498 | continue; |
499 | |
500 | if (r->flags & IORESOURCE_UNSET) { |
501 | pci_err(dev, "%s %pR: not assigned; can't enable device\n" , |
502 | r_name, r); |
503 | return -EINVAL; |
504 | } |
505 | |
506 | if (!r->parent) { |
507 | pci_err(dev, "%s %pR: not claimed; can't enable device\n" , |
508 | r_name, r); |
509 | return -EINVAL; |
510 | } |
511 | |
512 | if (r->flags & IORESOURCE_IO) |
513 | cmd |= PCI_COMMAND_IO; |
514 | if (r->flags & IORESOURCE_MEM) |
515 | cmd |= PCI_COMMAND_MEMORY; |
516 | } |
517 | |
518 | if (cmd != old_cmd) { |
519 | pci_info(dev, "enabling device (%04x -> %04x)\n" , old_cmd, cmd); |
520 | pci_write_config_word(dev, PCI_COMMAND, val: cmd); |
521 | } |
522 | return 0; |
523 | } |
524 | |