1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PCI Bus Services, see include/linux/pci.h for further explanation.
4 *
5 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
6 * David Mosberger-Tang
7 *
8 * Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz>
9 */
10
11 #include <linux/acpi.h>
12 #include <linux/kernel.h>
13 #include <linux/delay.h>
14 #include <linux/dmi.h>
15 #include <linux/init.h>
16 #include <linux/msi.h>
17 #include <linux/of.h>
18 #include <linux/pci.h>
19 #include <linux/pm.h>
20 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/spinlock.h>
23 #include <linux/string.h>
24 #include <linux/log2.h>
25 #include <linux/logic_pio.h>
26 #include <linux/pm_wakeup.h>
27 #include <linux/interrupt.h>
28 #include <linux/device.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/pci_hotplug.h>
31 #include <linux/vmalloc.h>
32 #include <asm/dma.h>
33 #include <linux/aer.h>
34 #include <linux/bitfield.h>
35 #include "pci.h"
36
37 DEFINE_MUTEX(pci_slot_mutex);
38
39 const char *pci_power_names[] = {
40 "error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown",
41 };
42 EXPORT_SYMBOL_GPL(pci_power_names);
43
44 int isa_dma_bridge_buggy;
45 EXPORT_SYMBOL(isa_dma_bridge_buggy);
46
47 int pci_pci_problems;
48 EXPORT_SYMBOL(pci_pci_problems);
49
50 unsigned int pci_pm_d3hot_delay;
51
52 static void pci_pme_list_scan(struct work_struct *work);
53
54 static LIST_HEAD(pci_pme_list);
55 static DEFINE_MUTEX(pci_pme_list_mutex);
56 static DECLARE_DELAYED_WORK(pci_pme_work, pci_pme_list_scan);
57
58 struct pci_pme_device {
59 struct list_head list;
60 struct pci_dev *dev;
61 };
62
63 #define PME_TIMEOUT 1000 /* How long between PME checks */
64
pci_dev_d3_sleep(struct pci_dev * dev)65 static void pci_dev_d3_sleep(struct pci_dev *dev)
66 {
67 unsigned int delay_ms = max(dev->d3hot_delay, pci_pm_d3hot_delay);
68 unsigned int upper;
69
70 if (delay_ms) {
71 /* Use a 20% upper bound, 1ms minimum */
72 upper = max(DIV_ROUND_CLOSEST(delay_ms, 5), 1U);
73 usleep_range(delay_ms * USEC_PER_MSEC,
74 (delay_ms + upper) * USEC_PER_MSEC);
75 }
76 }
77
pci_reset_supported(struct pci_dev * dev)78 bool pci_reset_supported(struct pci_dev *dev)
79 {
80 return dev->reset_methods[0] != 0;
81 }
82
83 #ifdef CONFIG_PCI_DOMAINS
84 int pci_domains_supported = 1;
85 #endif
86
87 #define DEFAULT_CARDBUS_IO_SIZE (256)
88 #define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
89 /* pci=cbmemsize=nnM,cbiosize=nn can override this */
90 unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
91 unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
92
93 #define DEFAULT_HOTPLUG_IO_SIZE (256)
94 #define DEFAULT_HOTPLUG_MMIO_SIZE (2*1024*1024)
95 #define DEFAULT_HOTPLUG_MMIO_PREF_SIZE (2*1024*1024)
96 /* hpiosize=nn can override this */
97 unsigned long pci_hotplug_io_size = DEFAULT_HOTPLUG_IO_SIZE;
98 /*
99 * pci=hpmmiosize=nnM overrides non-prefetchable MMIO size,
100 * pci=hpmmioprefsize=nnM overrides prefetchable MMIO size;
101 * pci=hpmemsize=nnM overrides both
102 */
103 unsigned long pci_hotplug_mmio_size = DEFAULT_HOTPLUG_MMIO_SIZE;
104 unsigned long pci_hotplug_mmio_pref_size = DEFAULT_HOTPLUG_MMIO_PREF_SIZE;
105
106 #define DEFAULT_HOTPLUG_BUS_SIZE 1
107 unsigned long pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;
108
109
110 /* PCIe MPS/MRRS strategy; can be overridden by kernel command-line param */
111 #ifdef CONFIG_PCIE_BUS_TUNE_OFF
112 enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_TUNE_OFF;
113 #elif defined CONFIG_PCIE_BUS_SAFE
114 enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_SAFE;
115 #elif defined CONFIG_PCIE_BUS_PERFORMANCE
116 enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_PERFORMANCE;
117 #elif defined CONFIG_PCIE_BUS_PEER2PEER
118 enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_PEER2PEER;
119 #else
120 enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_DEFAULT;
121 #endif
122
123 /*
124 * The default CLS is used if arch didn't set CLS explicitly and not
125 * all pci devices agree on the same value. Arch can override either
126 * the dfl or actual value as it sees fit. Don't forget this is
127 * measured in 32-bit words, not bytes.
128 */
129 u8 pci_dfl_cache_line_size = L1_CACHE_BYTES >> 2;
130 u8 pci_cache_line_size;
131
132 /*
133 * If we set up a device for bus mastering, we need to check the latency
134 * timer as certain BIOSes forget to set it properly.
135 */
136 unsigned int pcibios_max_latency = 255;
137
138 /* If set, the PCIe ARI capability will not be used. */
139 static bool pcie_ari_disabled;
140
141 /* If set, the PCIe ATS capability will not be used. */
142 static bool pcie_ats_disabled;
143
144 /* If set, the PCI config space of each device is printed during boot. */
145 bool pci_early_dump;
146
pci_ats_disabled(void)147 bool pci_ats_disabled(void)
148 {
149 return pcie_ats_disabled;
150 }
151 EXPORT_SYMBOL_GPL(pci_ats_disabled);
152
153 /* Disable bridge_d3 for all PCIe ports */
154 static bool pci_bridge_d3_disable;
155 /* Force bridge_d3 for all PCIe ports */
156 static bool pci_bridge_d3_force;
157
pcie_port_pm_setup(char * str)158 static int __init pcie_port_pm_setup(char *str)
159 {
160 if (!strcmp(str, "off"))
161 pci_bridge_d3_disable = true;
162 else if (!strcmp(str, "force"))
163 pci_bridge_d3_force = true;
164 return 1;
165 }
166 __setup("pcie_port_pm=", pcie_port_pm_setup);
167
168 /**
169 * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
170 * @bus: pointer to PCI bus structure to search
171 *
172 * Given a PCI bus, returns the highest PCI bus number present in the set
173 * including the given PCI bus and its list of child PCI buses.
174 */
pci_bus_max_busnr(struct pci_bus * bus)175 unsigned char pci_bus_max_busnr(struct pci_bus *bus)
176 {
177 struct pci_bus *tmp;
178 unsigned char max, n;
179
180 max = bus->busn_res.end;
181 list_for_each_entry(tmp, &bus->children, node) {
182 n = pci_bus_max_busnr(tmp);
183 if (n > max)
184 max = n;
185 }
186 return max;
187 }
188 EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
189
190 /**
191 * pci_status_get_and_clear_errors - return and clear error bits in PCI_STATUS
192 * @pdev: the PCI device
193 *
194 * Returns error bits set in PCI_STATUS and clears them.
195 */
pci_status_get_and_clear_errors(struct pci_dev * pdev)196 int pci_status_get_and_clear_errors(struct pci_dev *pdev)
197 {
198 u16 status;
199 int ret;
200
201 ret = pci_read_config_word(pdev, PCI_STATUS, &status);
202 if (ret != PCIBIOS_SUCCESSFUL)
203 return -EIO;
204
205 status &= PCI_STATUS_ERROR_BITS;
206 if (status)
207 pci_write_config_word(pdev, PCI_STATUS, status);
208
209 return status;
210 }
211 EXPORT_SYMBOL_GPL(pci_status_get_and_clear_errors);
212
213 #ifdef CONFIG_HAS_IOMEM
__pci_ioremap_resource(struct pci_dev * pdev,int bar,bool write_combine)214 static void __iomem *__pci_ioremap_resource(struct pci_dev *pdev, int bar,
215 bool write_combine)
216 {
217 struct resource *res = &pdev->resource[bar];
218 resource_size_t start = res->start;
219 resource_size_t size = resource_size(res);
220
221 /*
222 * Make sure the BAR is actually a memory resource, not an IO resource
223 */
224 if (res->flags & IORESOURCE_UNSET || !(res->flags & IORESOURCE_MEM)) {
225 pci_err(pdev, "can't ioremap BAR %d: %pR\n", bar, res);
226 return NULL;
227 }
228
229 if (write_combine)
230 return ioremap_wc(start, size);
231
232 return ioremap(start, size);
233 }
234
pci_ioremap_bar(struct pci_dev * pdev,int bar)235 void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
236 {
237 return __pci_ioremap_resource(pdev, bar, false);
238 }
239 EXPORT_SYMBOL_GPL(pci_ioremap_bar);
240
pci_ioremap_wc_bar(struct pci_dev * pdev,int bar)241 void __iomem *pci_ioremap_wc_bar(struct pci_dev *pdev, int bar)
242 {
243 return __pci_ioremap_resource(pdev, bar, true);
244 }
245 EXPORT_SYMBOL_GPL(pci_ioremap_wc_bar);
246 #endif
247
248 /**
249 * pci_dev_str_match_path - test if a path string matches a device
250 * @dev: the PCI device to test
251 * @path: string to match the device against
252 * @endptr: pointer to the string after the match
253 *
254 * Test if a string (typically from a kernel parameter) formatted as a
255 * path of device/function addresses matches a PCI device. The string must
256 * be of the form:
257 *
258 * [<domain>:]<bus>:<device>.<func>[/<device>.<func>]*
259 *
260 * A path for a device can be obtained using 'lspci -t'. Using a path
261 * is more robust against bus renumbering than using only a single bus,
262 * device and function address.
263 *
264 * Returns 1 if the string matches the device, 0 if it does not and
265 * a negative error code if it fails to parse the string.
266 */
pci_dev_str_match_path(struct pci_dev * dev,const char * path,const char ** endptr)267 static int pci_dev_str_match_path(struct pci_dev *dev, const char *path,
268 const char **endptr)
269 {
270 int ret;
271 int seg, bus, slot, func;
272 char *wpath, *p;
273 char end;
274
275 *endptr = strchrnul(path, ';');
276
277 wpath = kmemdup_nul(path, *endptr - path, GFP_ATOMIC);
278 if (!wpath)
279 return -ENOMEM;
280
281 while (1) {
282 p = strrchr(wpath, '/');
283 if (!p)
284 break;
285 ret = sscanf(p, "/%x.%x%c", &slot, &func, &end);
286 if (ret != 2) {
287 ret = -EINVAL;
288 goto free_and_exit;
289 }
290
291 if (dev->devfn != PCI_DEVFN(slot, func)) {
292 ret = 0;
293 goto free_and_exit;
294 }
295
296 /*
297 * Note: we don't need to get a reference to the upstream
298 * bridge because we hold a reference to the top level
299 * device which should hold a reference to the bridge,
300 * and so on.
301 */
302 dev = pci_upstream_bridge(dev);
303 if (!dev) {
304 ret = 0;
305 goto free_and_exit;
306 }
307
308 *p = 0;
309 }
310
311 ret = sscanf(wpath, "%x:%x:%x.%x%c", &seg, &bus, &slot,
312 &func, &end);
313 if (ret != 4) {
314 seg = 0;
315 ret = sscanf(wpath, "%x:%x.%x%c", &bus, &slot, &func, &end);
316 if (ret != 3) {
317 ret = -EINVAL;
318 goto free_and_exit;
319 }
320 }
321
322 ret = (seg == pci_domain_nr(dev->bus) &&
323 bus == dev->bus->number &&
324 dev->devfn == PCI_DEVFN(slot, func));
325
326 free_and_exit:
327 kfree(wpath);
328 return ret;
329 }
330
331 /**
332 * pci_dev_str_match - test if a string matches a device
333 * @dev: the PCI device to test
334 * @p: string to match the device against
335 * @endptr: pointer to the string after the match
336 *
337 * Test if a string (typically from a kernel parameter) matches a specified
338 * PCI device. The string may be of one of the following formats:
339 *
340 * [<domain>:]<bus>:<device>.<func>[/<device>.<func>]*
341 * pci:<vendor>:<device>[:<subvendor>:<subdevice>]
342 *
343 * The first format specifies a PCI bus/device/function address which
344 * may change if new hardware is inserted, if motherboard firmware changes,
345 * or due to changes caused in kernel parameters. If the domain is
346 * left unspecified, it is taken to be 0. In order to be robust against
347 * bus renumbering issues, a path of PCI device/function numbers may be used
348 * to address the specific device. The path for a device can be determined
349 * through the use of 'lspci -t'.
350 *
351 * The second format matches devices using IDs in the configuration
352 * space which may match multiple devices in the system. A value of 0
353 * for any field will match all devices. (Note: this differs from
354 * in-kernel code that uses PCI_ANY_ID which is ~0; this is for
355 * legacy reasons and convenience so users don't have to specify
356 * FFFFFFFFs on the command line.)
357 *
358 * Returns 1 if the string matches the device, 0 if it does not and
359 * a negative error code if the string cannot be parsed.
360 */
pci_dev_str_match(struct pci_dev * dev,const char * p,const char ** endptr)361 static int pci_dev_str_match(struct pci_dev *dev, const char *p,
362 const char **endptr)
363 {
364 int ret;
365 int count;
366 unsigned short vendor, device, subsystem_vendor, subsystem_device;
367
368 if (strncmp(p, "pci:", 4) == 0) {
369 /* PCI vendor/device (subvendor/subdevice) IDs are specified */
370 p += 4;
371 ret = sscanf(p, "%hx:%hx:%hx:%hx%n", &vendor, &device,
372 &subsystem_vendor, &subsystem_device, &count);
373 if (ret != 4) {
374 ret = sscanf(p, "%hx:%hx%n", &vendor, &device, &count);
375 if (ret != 2)
376 return -EINVAL;
377
378 subsystem_vendor = 0;
379 subsystem_device = 0;
380 }
381
382 p += count;
383
384 if ((!vendor || vendor == dev->vendor) &&
385 (!device || device == dev->device) &&
386 (!subsystem_vendor ||
387 subsystem_vendor == dev->subsystem_vendor) &&
388 (!subsystem_device ||
389 subsystem_device == dev->subsystem_device))
390 goto found;
391 } else {
392 /*
393 * PCI Bus, Device, Function IDs are specified
394 * (optionally, may include a path of devfns following it)
395 */
396 ret = pci_dev_str_match_path(dev, p, &p);
397 if (ret < 0)
398 return ret;
399 else if (ret)
400 goto found;
401 }
402
403 *endptr = p;
404 return 0;
405
406 found:
407 *endptr = p;
408 return 1;
409 }
410
__pci_find_next_cap_ttl(struct pci_bus * bus,unsigned int devfn,u8 pos,int cap,int * ttl)411 static u8 __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
412 u8 pos, int cap, int *ttl)
413 {
414 u8 id;
415 u16 ent;
416
417 pci_bus_read_config_byte(bus, devfn, pos, &pos);
418
419 while ((*ttl)--) {
420 if (pos < 0x40)
421 break;
422 pos &= ~3;
423 pci_bus_read_config_word(bus, devfn, pos, &ent);
424
425 id = ent & 0xff;
426 if (id == 0xff)
427 break;
428 if (id == cap)
429 return pos;
430 pos = (ent >> 8);
431 }
432 return 0;
433 }
434
__pci_find_next_cap(struct pci_bus * bus,unsigned int devfn,u8 pos,int cap)435 static u8 __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
436 u8 pos, int cap)
437 {
438 int ttl = PCI_FIND_CAP_TTL;
439
440 return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
441 }
442
pci_find_next_capability(struct pci_dev * dev,u8 pos,int cap)443 u8 pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
444 {
445 return __pci_find_next_cap(dev->bus, dev->devfn,
446 pos + PCI_CAP_LIST_NEXT, cap);
447 }
448 EXPORT_SYMBOL_GPL(pci_find_next_capability);
449
__pci_bus_find_cap_start(struct pci_bus * bus,unsigned int devfn,u8 hdr_type)450 static u8 __pci_bus_find_cap_start(struct pci_bus *bus,
451 unsigned int devfn, u8 hdr_type)
452 {
453 u16 status;
454
455 pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
456 if (!(status & PCI_STATUS_CAP_LIST))
457 return 0;
458
459 switch (hdr_type) {
460 case PCI_HEADER_TYPE_NORMAL:
461 case PCI_HEADER_TYPE_BRIDGE:
462 return PCI_CAPABILITY_LIST;
463 case PCI_HEADER_TYPE_CARDBUS:
464 return PCI_CB_CAPABILITY_LIST;
465 }
466
467 return 0;
468 }
469
470 /**
471 * pci_find_capability - query for devices' capabilities
472 * @dev: PCI device to query
473 * @cap: capability code
474 *
475 * Tell if a device supports a given PCI capability.
476 * Returns the address of the requested capability structure within the
477 * device's PCI configuration space or 0 in case the device does not
478 * support it. Possible values for @cap include:
479 *
480 * %PCI_CAP_ID_PM Power Management
481 * %PCI_CAP_ID_AGP Accelerated Graphics Port
482 * %PCI_CAP_ID_VPD Vital Product Data
483 * %PCI_CAP_ID_SLOTID Slot Identification
484 * %PCI_CAP_ID_MSI Message Signalled Interrupts
485 * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
486 * %PCI_CAP_ID_PCIX PCI-X
487 * %PCI_CAP_ID_EXP PCI Express
488 */
pci_find_capability(struct pci_dev * dev,int cap)489 u8 pci_find_capability(struct pci_dev *dev, int cap)
490 {
491 u8 pos;
492
493 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
494 if (pos)
495 pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
496
497 return pos;
498 }
499 EXPORT_SYMBOL(pci_find_capability);
500
501 /**
502 * pci_bus_find_capability - query for devices' capabilities
503 * @bus: the PCI bus to query
504 * @devfn: PCI device to query
505 * @cap: capability code
506 *
507 * Like pci_find_capability() but works for PCI devices that do not have a
508 * pci_dev structure set up yet.
509 *
510 * Returns the address of the requested capability structure within the
511 * device's PCI configuration space or 0 in case the device does not
512 * support it.
513 */
pci_bus_find_capability(struct pci_bus * bus,unsigned int devfn,int cap)514 u8 pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
515 {
516 u8 hdr_type, pos;
517
518 pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
519
520 pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
521 if (pos)
522 pos = __pci_find_next_cap(bus, devfn, pos, cap);
523
524 return pos;
525 }
526 EXPORT_SYMBOL(pci_bus_find_capability);
527
528 /**
529 * pci_find_next_ext_capability - Find an extended capability
530 * @dev: PCI device to query
531 * @start: address at which to start looking (0 to start at beginning of list)
532 * @cap: capability code
533 *
534 * Returns the address of the next matching extended capability structure
535 * within the device's PCI configuration space or 0 if the device does
536 * not support it. Some capabilities can occur several times, e.g., the
537 * vendor-specific capability, and this provides a way to find them all.
538 */
pci_find_next_ext_capability(struct pci_dev * dev,u16 start,int cap)539 u16 pci_find_next_ext_capability(struct pci_dev *dev, u16 start, int cap)
540 {
541 u32 header;
542 int ttl;
543 u16 pos = PCI_CFG_SPACE_SIZE;
544
545 /* minimum 8 bytes per capability */
546 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
547
548 if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
549 return 0;
550
551 if (start)
552 pos = start;
553
554 if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
555 return 0;
556
557 /*
558 * If we have no capabilities, this is indicated by cap ID,
559 * cap version and next pointer all being 0.
560 */
561 if (header == 0)
562 return 0;
563
564 while (ttl-- > 0) {
565 if (PCI_EXT_CAP_ID(header) == cap && pos != start)
566 return pos;
567
568 pos = PCI_EXT_CAP_NEXT(header);
569 if (pos < PCI_CFG_SPACE_SIZE)
570 break;
571
572 if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
573 break;
574 }
575
576 return 0;
577 }
578 EXPORT_SYMBOL_GPL(pci_find_next_ext_capability);
579
580 /**
581 * pci_find_ext_capability - Find an extended capability
582 * @dev: PCI device to query
583 * @cap: capability code
584 *
585 * Returns the address of the requested extended capability structure
586 * within the device's PCI configuration space or 0 if the device does
587 * not support it. Possible values for @cap include:
588 *
589 * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
590 * %PCI_EXT_CAP_ID_VC Virtual Channel
591 * %PCI_EXT_CAP_ID_DSN Device Serial Number
592 * %PCI_EXT_CAP_ID_PWR Power Budgeting
593 */
pci_find_ext_capability(struct pci_dev * dev,int cap)594 u16 pci_find_ext_capability(struct pci_dev *dev, int cap)
595 {
596 return pci_find_next_ext_capability(dev, 0, cap);
597 }
598 EXPORT_SYMBOL_GPL(pci_find_ext_capability);
599
600 /**
601 * pci_get_dsn - Read and return the 8-byte Device Serial Number
602 * @dev: PCI device to query
603 *
604 * Looks up the PCI_EXT_CAP_ID_DSN and reads the 8 bytes of the Device Serial
605 * Number.
606 *
607 * Returns the DSN, or zero if the capability does not exist.
608 */
pci_get_dsn(struct pci_dev * dev)609 u64 pci_get_dsn(struct pci_dev *dev)
610 {
611 u32 dword;
612 u64 dsn;
613 int pos;
614
615 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DSN);
616 if (!pos)
617 return 0;
618
619 /*
620 * The Device Serial Number is two dwords offset 4 bytes from the
621 * capability position. The specification says that the first dword is
622 * the lower half, and the second dword is the upper half.
623 */
624 pos += 4;
625 pci_read_config_dword(dev, pos, &dword);
626 dsn = (u64)dword;
627 pci_read_config_dword(dev, pos + 4, &dword);
628 dsn |= ((u64)dword) << 32;
629
630 return dsn;
631 }
632 EXPORT_SYMBOL_GPL(pci_get_dsn);
633
__pci_find_next_ht_cap(struct pci_dev * dev,u8 pos,int ht_cap)634 static u8 __pci_find_next_ht_cap(struct pci_dev *dev, u8 pos, int ht_cap)
635 {
636 int rc, ttl = PCI_FIND_CAP_TTL;
637 u8 cap, mask;
638
639 if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
640 mask = HT_3BIT_CAP_MASK;
641 else
642 mask = HT_5BIT_CAP_MASK;
643
644 pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
645 PCI_CAP_ID_HT, &ttl);
646 while (pos) {
647 rc = pci_read_config_byte(dev, pos + 3, &cap);
648 if (rc != PCIBIOS_SUCCESSFUL)
649 return 0;
650
651 if ((cap & mask) == ht_cap)
652 return pos;
653
654 pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
655 pos + PCI_CAP_LIST_NEXT,
656 PCI_CAP_ID_HT, &ttl);
657 }
658
659 return 0;
660 }
661
662 /**
663 * pci_find_next_ht_capability - query a device's HyperTransport capabilities
664 * @dev: PCI device to query
665 * @pos: Position from which to continue searching
666 * @ht_cap: HyperTransport capability code
667 *
668 * To be used in conjunction with pci_find_ht_capability() to search for
669 * all capabilities matching @ht_cap. @pos should always be a value returned
670 * from pci_find_ht_capability().
671 *
672 * NB. To be 100% safe against broken PCI devices, the caller should take
673 * steps to avoid an infinite loop.
674 */
pci_find_next_ht_capability(struct pci_dev * dev,u8 pos,int ht_cap)675 u8 pci_find_next_ht_capability(struct pci_dev *dev, u8 pos, int ht_cap)
676 {
677 return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
678 }
679 EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
680
681 /**
682 * pci_find_ht_capability - query a device's HyperTransport capabilities
683 * @dev: PCI device to query
684 * @ht_cap: HyperTransport capability code
685 *
686 * Tell if a device supports a given HyperTransport capability.
687 * Returns an address within the device's PCI configuration space
688 * or 0 in case the device does not support the request capability.
689 * The address points to the PCI capability, of type PCI_CAP_ID_HT,
690 * which has a HyperTransport capability matching @ht_cap.
691 */
pci_find_ht_capability(struct pci_dev * dev,int ht_cap)692 u8 pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
693 {
694 u8 pos;
695
696 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
697 if (pos)
698 pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
699
700 return pos;
701 }
702 EXPORT_SYMBOL_GPL(pci_find_ht_capability);
703
704 /**
705 * pci_find_vsec_capability - Find a vendor-specific extended capability
706 * @dev: PCI device to query
707 * @vendor: Vendor ID for which capability is defined
708 * @cap: Vendor-specific capability ID
709 *
710 * If @dev has Vendor ID @vendor, search for a VSEC capability with
711 * VSEC ID @cap. If found, return the capability offset in
712 * config space; otherwise return 0.
713 */
pci_find_vsec_capability(struct pci_dev * dev,u16 vendor,int cap)714 u16 pci_find_vsec_capability(struct pci_dev *dev, u16 vendor, int cap)
715 {
716 u16 vsec = 0;
717 u32 header;
718
719 if (vendor != dev->vendor)
720 return 0;
721
722 while ((vsec = pci_find_next_ext_capability(dev, vsec,
723 PCI_EXT_CAP_ID_VNDR))) {
724 if (pci_read_config_dword(dev, vsec + PCI_VNDR_HEADER,
725 &header) == PCIBIOS_SUCCESSFUL &&
726 PCI_VNDR_HEADER_ID(header) == cap)
727 return vsec;
728 }
729
730 return 0;
731 }
732 EXPORT_SYMBOL_GPL(pci_find_vsec_capability);
733
734 /**
735 * pci_find_parent_resource - return resource region of parent bus of given
736 * region
737 * @dev: PCI device structure contains resources to be searched
738 * @res: child resource record for which parent is sought
739 *
740 * For given resource region of given device, return the resource region of
741 * parent bus the given region is contained in.
742 */
pci_find_parent_resource(const struct pci_dev * dev,struct resource * res)743 struct resource *pci_find_parent_resource(const struct pci_dev *dev,
744 struct resource *res)
745 {
746 const struct pci_bus *bus = dev->bus;
747 struct resource *r;
748 int i;
749
750 pci_bus_for_each_resource(bus, r, i) {
751 if (!r)
752 continue;
753 if (resource_contains(r, res)) {
754
755 /*
756 * If the window is prefetchable but the BAR is
757 * not, the allocator made a mistake.
758 */
759 if (r->flags & IORESOURCE_PREFETCH &&
760 !(res->flags & IORESOURCE_PREFETCH))
761 return NULL;
762
763 /*
764 * If we're below a transparent bridge, there may
765 * be both a positively-decoded aperture and a
766 * subtractively-decoded region that contain the BAR.
767 * We want the positively-decoded one, so this depends
768 * on pci_bus_for_each_resource() giving us those
769 * first.
770 */
771 return r;
772 }
773 }
774 return NULL;
775 }
776 EXPORT_SYMBOL(pci_find_parent_resource);
777
778 /**
779 * pci_find_resource - Return matching PCI device resource
780 * @dev: PCI device to query
781 * @res: Resource to look for
782 *
783 * Goes over standard PCI resources (BARs) and checks if the given resource
784 * is partially or fully contained in any of them. In that case the
785 * matching resource is returned, %NULL otherwise.
786 */
pci_find_resource(struct pci_dev * dev,struct resource * res)787 struct resource *pci_find_resource(struct pci_dev *dev, struct resource *res)
788 {
789 int i;
790
791 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
792 struct resource *r = &dev->resource[i];
793
794 if (r->start && resource_contains(r, res))
795 return r;
796 }
797
798 return NULL;
799 }
800 EXPORT_SYMBOL(pci_find_resource);
801
802 /**
803 * pci_wait_for_pending - wait for @mask bit(s) to clear in status word @pos
804 * @dev: the PCI device to operate on
805 * @pos: config space offset of status word
806 * @mask: mask of bit(s) to care about in status word
807 *
808 * Return 1 when mask bit(s) in status word clear, 0 otherwise.
809 */
pci_wait_for_pending(struct pci_dev * dev,int pos,u16 mask)810 int pci_wait_for_pending(struct pci_dev *dev, int pos, u16 mask)
811 {
812 int i;
813
814 /* Wait for Transaction Pending bit clean */
815 for (i = 0; i < 4; i++) {
816 u16 status;
817 if (i)
818 msleep((1 << (i - 1)) * 100);
819
820 pci_read_config_word(dev, pos, &status);
821 if (!(status & mask))
822 return 1;
823 }
824
825 if (dev->bus->self)
826 pcie_aspm_pm_state_change(dev->bus->self);
827
828 return 0;
829 }
830
831 static int pci_acs_enable;
832
833 /**
834 * pci_request_acs - ask for ACS to be enabled if supported
835 */
pci_request_acs(void)836 void pci_request_acs(void)
837 {
838 pci_acs_enable = 1;
839 }
840
841 static const char *disable_acs_redir_param;
842
843 /**
844 * pci_disable_acs_redir - disable ACS redirect capabilities
845 * @dev: the PCI device
846 *
847 * For only devices specified in the disable_acs_redir parameter.
848 */
pci_disable_acs_redir(struct pci_dev * dev)849 static void pci_disable_acs_redir(struct pci_dev *dev)
850 {
851 int ret = 0;
852 const char *p;
853 int pos;
854 u16 ctrl;
855
856 if (!disable_acs_redir_param)
857 return;
858
859 p = disable_acs_redir_param;
860 while (*p) {
861 ret = pci_dev_str_match(dev, p, &p);
862 if (ret < 0) {
863 pr_info_once("PCI: Can't parse disable_acs_redir parameter: %s\n",
864 disable_acs_redir_param);
865
866 break;
867 } else if (ret == 1) {
868 /* Found a match */
869 break;
870 }
871
872 if (*p != ';' && *p != ',') {
873 /* End of param or invalid format */
874 break;
875 }
876 p++;
877 }
878
879 if (ret != 1)
880 return;
881
882 if (!pci_dev_specific_disable_acs_redir(dev))
883 return;
884
885 pos = dev->acs_cap;
886 if (!pos) {
887 pci_warn(dev, "cannot disable ACS redirect for this hardware as it does not have ACS capabilities\n");
888 return;
889 }
890
891 pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
892
893 /* P2P Request & Completion Redirect */
894 ctrl &= ~(PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_EC);
895
896 pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
897
898 pci_info(dev, "disabled ACS redirect\n");
899 }
900
901 /**
902 * pci_std_enable_acs - enable ACS on devices using standard ACS capabilities
903 * @dev: the PCI device
904 */
pci_std_enable_acs(struct pci_dev * dev)905 static void pci_std_enable_acs(struct pci_dev *dev)
906 {
907 int pos;
908 u16 cap;
909 u16 ctrl;
910
911 pos = dev->acs_cap;
912 if (!pos)
913 return;
914
915 pci_read_config_word(dev, pos + PCI_ACS_CAP, &cap);
916 pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
917
918 /* Source Validation */
919 ctrl |= (cap & PCI_ACS_SV);
920
921 /* P2P Request Redirect */
922 ctrl |= (cap & PCI_ACS_RR);
923
924 /* P2P Completion Redirect */
925 ctrl |= (cap & PCI_ACS_CR);
926
927 /* Upstream Forwarding */
928 ctrl |= (cap & PCI_ACS_UF);
929
930 /* Enable Translation Blocking for external devices and noats */
931 if (pci_ats_disabled() || dev->external_facing || dev->untrusted)
932 ctrl |= (cap & PCI_ACS_TB);
933
934 pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
935 }
936
937 /**
938 * pci_enable_acs - enable ACS if hardware support it
939 * @dev: the PCI device
940 */
pci_enable_acs(struct pci_dev * dev)941 static void pci_enable_acs(struct pci_dev *dev)
942 {
943 if (!pci_acs_enable)
944 goto disable_acs_redir;
945
946 if (!pci_dev_specific_enable_acs(dev))
947 goto disable_acs_redir;
948
949 pci_std_enable_acs(dev);
950
951 disable_acs_redir:
952 /*
953 * Note: pci_disable_acs_redir() must be called even if ACS was not
954 * enabled by the kernel because it may have been enabled by
955 * platform firmware. So if we are told to disable it, we should
956 * always disable it after setting the kernel's default
957 * preferences.
958 */
959 pci_disable_acs_redir(dev);
960 }
961
962 /**
963 * pci_restore_bars - restore a device's BAR values (e.g. after wake-up)
964 * @dev: PCI device to have its BARs restored
965 *
966 * Restore the BAR values for a given device, so as to make it
967 * accessible by its driver.
968 */
pci_restore_bars(struct pci_dev * dev)969 static void pci_restore_bars(struct pci_dev *dev)
970 {
971 int i;
972
973 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
974 pci_update_resource(dev, i);
975 }
976
977 static const struct pci_platform_pm_ops *pci_platform_pm;
978
pci_set_platform_pm(const struct pci_platform_pm_ops * ops)979 int pci_set_platform_pm(const struct pci_platform_pm_ops *ops)
980 {
981 if (!ops->is_manageable || !ops->set_state || !ops->get_state ||
982 !ops->choose_state || !ops->set_wakeup || !ops->need_resume)
983 return -EINVAL;
984 pci_platform_pm = ops;
985 return 0;
986 }
987
platform_pci_power_manageable(struct pci_dev * dev)988 static inline bool platform_pci_power_manageable(struct pci_dev *dev)
989 {
990 return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
991 }
992
platform_pci_set_power_state(struct pci_dev * dev,pci_power_t t)993 static inline int platform_pci_set_power_state(struct pci_dev *dev,
994 pci_power_t t)
995 {
996 return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
997 }
998
platform_pci_get_power_state(struct pci_dev * dev)999 static inline pci_power_t platform_pci_get_power_state(struct pci_dev *dev)
1000 {
1001 return pci_platform_pm ? pci_platform_pm->get_state(dev) : PCI_UNKNOWN;
1002 }
1003
platform_pci_refresh_power_state(struct pci_dev * dev)1004 static inline void platform_pci_refresh_power_state(struct pci_dev *dev)
1005 {
1006 if (pci_platform_pm && pci_platform_pm->refresh_state)
1007 pci_platform_pm->refresh_state(dev);
1008 }
1009
platform_pci_choose_state(struct pci_dev * dev)1010 static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
1011 {
1012 return pci_platform_pm ?
1013 pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
1014 }
1015
platform_pci_set_wakeup(struct pci_dev * dev,bool enable)1016 static inline int platform_pci_set_wakeup(struct pci_dev *dev, bool enable)
1017 {
1018 return pci_platform_pm ?
1019 pci_platform_pm->set_wakeup(dev, enable) : -ENODEV;
1020 }
1021
platform_pci_need_resume(struct pci_dev * dev)1022 static inline bool platform_pci_need_resume(struct pci_dev *dev)
1023 {
1024 return pci_platform_pm ? pci_platform_pm->need_resume(dev) : false;
1025 }
1026
platform_pci_bridge_d3(struct pci_dev * dev)1027 static inline bool platform_pci_bridge_d3(struct pci_dev *dev)
1028 {
1029 if (pci_platform_pm && pci_platform_pm->bridge_d3)
1030 return pci_platform_pm->bridge_d3(dev);
1031 return false;
1032 }
1033
1034 /**
1035 * pci_raw_set_power_state - Use PCI PM registers to set the power state of
1036 * given PCI device
1037 * @dev: PCI device to handle.
1038 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
1039 *
1040 * RETURN VALUE:
1041 * -EINVAL if the requested state is invalid.
1042 * -EIO if device does not support PCI PM or its PM capabilities register has a
1043 * wrong version, or device doesn't support the requested state.
1044 * 0 if device already is in the requested state.
1045 * 0 if device's power state has been successfully changed.
1046 */
pci_raw_set_power_state(struct pci_dev * dev,pci_power_t state)1047 static int pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
1048 {
1049 u16 pmcsr;
1050 bool need_restore = false;
1051
1052 /* Check if we're already there */
1053 if (dev->current_state == state)
1054 return 0;
1055
1056 if (!dev->pm_cap)
1057 return -EIO;
1058
1059 if (state < PCI_D0 || state > PCI_D3hot)
1060 return -EINVAL;
1061
1062 /*
1063 * Validate transition: We can enter D0 from any state, but if
1064 * we're already in a low-power state, we can only go deeper. E.g.,
1065 * we can go from D1 to D3, but we can't go directly from D3 to D1;
1066 * we'd have to go from D3 to D0, then to D1.
1067 */
1068 if (state != PCI_D0 && dev->current_state <= PCI_D3cold
1069 && dev->current_state > state) {
1070 pci_err(dev, "invalid power transition (from %s to %s)\n",
1071 pci_power_name(dev->current_state),
1072 pci_power_name(state));
1073 return -EINVAL;
1074 }
1075
1076 /* Check if this device supports the desired state */
1077 if ((state == PCI_D1 && !dev->d1_support)
1078 || (state == PCI_D2 && !dev->d2_support))
1079 return -EIO;
1080
1081 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
1082 if (pmcsr == (u16) ~0) {
1083 pci_err(dev, "can't change power state from %s to %s (config space inaccessible)\n",
1084 pci_power_name(dev->current_state),
1085 pci_power_name(state));
1086 return -EIO;
1087 }
1088
1089 /*
1090 * If we're (effectively) in D3, force entire word to 0.
1091 * This doesn't affect PME_Status, disables PME_En, and
1092 * sets PowerState to 0.
1093 */
1094 switch (dev->current_state) {
1095 case PCI_D0:
1096 case PCI_D1:
1097 case PCI_D2:
1098 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
1099 pmcsr |= state;
1100 break;
1101 case PCI_D3hot:
1102 case PCI_D3cold:
1103 case PCI_UNKNOWN: /* Boot-up */
1104 if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
1105 && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
1106 need_restore = true;
1107 fallthrough; /* force to D0 */
1108 default:
1109 pmcsr = 0;
1110 break;
1111 }
1112
1113 /* Enter specified state */
1114 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
1115
1116 /*
1117 * Mandatory power management transition delays; see PCI PM 1.1
1118 * 5.6.1 table 18
1119 */
1120 if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
1121 pci_dev_d3_sleep(dev);
1122 else if (state == PCI_D2 || dev->current_state == PCI_D2)
1123 udelay(PCI_PM_D2_DELAY);
1124
1125 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
1126 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
1127 if (dev->current_state != state)
1128 pci_info_ratelimited(dev, "refused to change power state from %s to %s\n",
1129 pci_power_name(dev->current_state),
1130 pci_power_name(state));
1131
1132 /*
1133 * According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
1134 * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
1135 * from D3hot to D0 _may_ perform an internal reset, thereby
1136 * going to "D0 Uninitialized" rather than "D0 Initialized".
1137 * For example, at least some versions of the 3c905B and the
1138 * 3c556B exhibit this behaviour.
1139 *
1140 * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
1141 * devices in a D3hot state at boot. Consequently, we need to
1142 * restore at least the BARs so that the device will be
1143 * accessible to its driver.
1144 */
1145 if (need_restore)
1146 pci_restore_bars(dev);
1147
1148 if (dev->bus->self)
1149 pcie_aspm_pm_state_change(dev->bus->self);
1150
1151 return 0;
1152 }
1153
1154 /**
1155 * pci_update_current_state - Read power state of given device and cache it
1156 * @dev: PCI device to handle.
1157 * @state: State to cache in case the device doesn't have the PM capability
1158 *
1159 * The power state is read from the PMCSR register, which however is
1160 * inaccessible in D3cold. The platform firmware is therefore queried first
1161 * to detect accessibility of the register. In case the platform firmware
1162 * reports an incorrect state or the device isn't power manageable by the
1163 * platform at all, we try to detect D3cold by testing accessibility of the
1164 * vendor ID in config space.
1165 */
pci_update_current_state(struct pci_dev * dev,pci_power_t state)1166 void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
1167 {
1168 if (platform_pci_get_power_state(dev) == PCI_D3cold ||
1169 !pci_device_is_present(dev)) {
1170 dev->current_state = PCI_D3cold;
1171 } else if (dev->pm_cap) {
1172 u16 pmcsr;
1173
1174 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
1175 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
1176 } else {
1177 dev->current_state = state;
1178 }
1179 }
1180
1181 /**
1182 * pci_refresh_power_state - Refresh the given device's power state data
1183 * @dev: Target PCI device.
1184 *
1185 * Ask the platform to refresh the devices power state information and invoke
1186 * pci_update_current_state() to update its current PCI power state.
1187 */
pci_refresh_power_state(struct pci_dev * dev)1188 void pci_refresh_power_state(struct pci_dev *dev)
1189 {
1190 if (platform_pci_power_manageable(dev))
1191 platform_pci_refresh_power_state(dev);
1192
1193 pci_update_current_state(dev, dev->current_state);
1194 }
1195
1196 /**
1197 * pci_platform_power_transition - Use platform to change device power state
1198 * @dev: PCI device to handle.
1199 * @state: State to put the device into.
1200 */
pci_platform_power_transition(struct pci_dev * dev,pci_power_t state)1201 int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
1202 {
1203 int error;
1204
1205 if (platform_pci_power_manageable(dev)) {
1206 error = platform_pci_set_power_state(dev, state);
1207 if (!error)
1208 pci_update_current_state(dev, state);
1209 } else
1210 error = -ENODEV;
1211
1212 if (error && !dev->pm_cap) /* Fall back to PCI_D0 */
1213 dev->current_state = PCI_D0;
1214
1215 return error;
1216 }
1217 EXPORT_SYMBOL_GPL(pci_platform_power_transition);
1218
pci_resume_one(struct pci_dev * pci_dev,void * ign)1219 static int pci_resume_one(struct pci_dev *pci_dev, void *ign)
1220 {
1221 pm_request_resume(&pci_dev->dev);
1222 return 0;
1223 }
1224
1225 /**
1226 * pci_resume_bus - Walk given bus and runtime resume devices on it
1227 * @bus: Top bus of the subtree to walk.
1228 */
pci_resume_bus(struct pci_bus * bus)1229 void pci_resume_bus(struct pci_bus *bus)
1230 {
1231 if (bus)
1232 pci_walk_bus(bus, pci_resume_one, NULL);
1233 }
1234
pci_dev_wait(struct pci_dev * dev,char * reset_type,int timeout)1235 static int pci_dev_wait(struct pci_dev *dev, char *reset_type, int timeout)
1236 {
1237 int delay = 1;
1238 u32 id;
1239
1240 /*
1241 * After reset, the device should not silently discard config
1242 * requests, but it may still indicate that it needs more time by
1243 * responding to them with CRS completions. The Root Port will
1244 * generally synthesize ~0 data to complete the read (except when
1245 * CRS SV is enabled and the read was for the Vendor ID; in that
1246 * case it synthesizes 0x0001 data).
1247 *
1248 * Wait for the device to return a non-CRS completion. Read the
1249 * Command register instead of Vendor ID so we don't have to
1250 * contend with the CRS SV value.
1251 */
1252 pci_read_config_dword(dev, PCI_COMMAND, &id);
1253 while (id == ~0) {
1254 if (delay > timeout) {
1255 pci_warn(dev, "not ready %dms after %s; giving up\n",
1256 delay - 1, reset_type);
1257 return -ENOTTY;
1258 }
1259
1260 if (delay > PCI_RESET_WAIT)
1261 pci_info(dev, "not ready %dms after %s; waiting\n",
1262 delay - 1, reset_type);
1263
1264 msleep(delay);
1265 delay *= 2;
1266 pci_read_config_dword(dev, PCI_COMMAND, &id);
1267 }
1268
1269 if (delay > PCI_RESET_WAIT)
1270 pci_info(dev, "ready %dms after %s\n", delay - 1,
1271 reset_type);
1272
1273 return 0;
1274 }
1275
1276 /**
1277 * pci_power_up - Put the given device into D0
1278 * @dev: PCI device to power up
1279 */
pci_power_up(struct pci_dev * dev)1280 int pci_power_up(struct pci_dev *dev)
1281 {
1282 pci_platform_power_transition(dev, PCI_D0);
1283
1284 /*
1285 * Mandatory power management transition delays are handled in
1286 * pci_pm_resume_noirq() and pci_pm_runtime_resume() of the
1287 * corresponding bridge.
1288 */
1289 if (dev->runtime_d3cold) {
1290 /*
1291 * When powering on a bridge from D3cold, the whole hierarchy
1292 * may be powered on into D0uninitialized state, resume them to
1293 * give them a chance to suspend again
1294 */
1295 pci_resume_bus(dev->subordinate);
1296 }
1297
1298 return pci_raw_set_power_state(dev, PCI_D0);
1299 }
1300
1301 /**
1302 * __pci_dev_set_current_state - Set current state of a PCI device
1303 * @dev: Device to handle
1304 * @data: pointer to state to be set
1305 */
__pci_dev_set_current_state(struct pci_dev * dev,void * data)1306 static int __pci_dev_set_current_state(struct pci_dev *dev, void *data)
1307 {
1308 pci_power_t state = *(pci_power_t *)data;
1309
1310 dev->current_state = state;
1311 return 0;
1312 }
1313
1314 /**
1315 * pci_bus_set_current_state - Walk given bus and set current state of devices
1316 * @bus: Top bus of the subtree to walk.
1317 * @state: state to be set
1318 */
pci_bus_set_current_state(struct pci_bus * bus,pci_power_t state)1319 void pci_bus_set_current_state(struct pci_bus *bus, pci_power_t state)
1320 {
1321 if (bus)
1322 pci_walk_bus(bus, __pci_dev_set_current_state, &state);
1323 }
1324
1325 /**
1326 * pci_set_power_state - Set the power state of a PCI device
1327 * @dev: PCI device to handle.
1328 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
1329 *
1330 * Transition a device to a new power state, using the platform firmware and/or
1331 * the device's PCI PM registers.
1332 *
1333 * RETURN VALUE:
1334 * -EINVAL if the requested state is invalid.
1335 * -EIO if device does not support PCI PM or its PM capabilities register has a
1336 * wrong version, or device doesn't support the requested state.
1337 * 0 if the transition is to D1 or D2 but D1 and D2 are not supported.
1338 * 0 if device already is in the requested state.
1339 * 0 if the transition is to D3 but D3 is not supported.
1340 * 0 if device's power state has been successfully changed.
1341 */
pci_set_power_state(struct pci_dev * dev,pci_power_t state)1342 int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
1343 {
1344 int error;
1345
1346 /* Bound the state we're entering */
1347 if (state > PCI_D3cold)
1348 state = PCI_D3cold;
1349 else if (state < PCI_D0)
1350 state = PCI_D0;
1351 else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
1352
1353 /*
1354 * If the device or the parent bridge do not support PCI
1355 * PM, ignore the request if we're doing anything other
1356 * than putting it into D0 (which would only happen on
1357 * boot).
1358 */
1359 return 0;
1360
1361 /* Check if we're already there */
1362 if (dev->current_state == state)
1363 return 0;
1364
1365 if (state == PCI_D0)
1366 return pci_power_up(dev);
1367
1368 /*
1369 * This device is quirked not to be put into D3, so don't put it in
1370 * D3
1371 */
1372 if (state >= PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
1373 return 0;
1374
1375 /*
1376 * To put device in D3cold, we put device into D3hot in native
1377 * way, then put device into D3cold with platform ops
1378 */
1379 error = pci_raw_set_power_state(dev, state > PCI_D3hot ?
1380 PCI_D3hot : state);
1381
1382 if (pci_platform_power_transition(dev, state))
1383 return error;
1384
1385 /* Powering off a bridge may power off the whole hierarchy */
1386 if (state == PCI_D3cold)
1387 pci_bus_set_current_state(dev->subordinate, PCI_D3cold);
1388
1389 return 0;
1390 }
1391 EXPORT_SYMBOL(pci_set_power_state);
1392
1393 /**
1394 * pci_choose_state - Choose the power state of a PCI device
1395 * @dev: PCI device to be suspended
1396 * @state: target sleep state for the whole system. This is the value
1397 * that is passed to suspend() function.
1398 *
1399 * Returns PCI power state suitable for given device and given system
1400 * message.
1401 */
pci_choose_state(struct pci_dev * dev,pm_message_t state)1402 pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
1403 {
1404 pci_power_t ret;
1405
1406 if (!dev->pm_cap)
1407 return PCI_D0;
1408
1409 ret = platform_pci_choose_state(dev);
1410 if (ret != PCI_POWER_ERROR)
1411 return ret;
1412
1413 switch (state.event) {
1414 case PM_EVENT_ON:
1415 return PCI_D0;
1416 case PM_EVENT_FREEZE:
1417 case PM_EVENT_PRETHAW:
1418 /* REVISIT both freeze and pre-thaw "should" use D0 */
1419 case PM_EVENT_SUSPEND:
1420 case PM_EVENT_HIBERNATE:
1421 return PCI_D3hot;
1422 default:
1423 pci_info(dev, "unrecognized suspend event %d\n",
1424 state.event);
1425 BUG();
1426 }
1427 return PCI_D0;
1428 }
1429 EXPORT_SYMBOL(pci_choose_state);
1430
1431 #define PCI_EXP_SAVE_REGS 7
1432
_pci_find_saved_cap(struct pci_dev * pci_dev,u16 cap,bool extended)1433 static struct pci_cap_saved_state *_pci_find_saved_cap(struct pci_dev *pci_dev,
1434 u16 cap, bool extended)
1435 {
1436 struct pci_cap_saved_state *tmp;
1437
1438 hlist_for_each_entry(tmp, &pci_dev->saved_cap_space, next) {
1439 if (tmp->cap.cap_extended == extended && tmp->cap.cap_nr == cap)
1440 return tmp;
1441 }
1442 return NULL;
1443 }
1444
pci_find_saved_cap(struct pci_dev * dev,char cap)1445 struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap)
1446 {
1447 return _pci_find_saved_cap(dev, cap, false);
1448 }
1449
pci_find_saved_ext_cap(struct pci_dev * dev,u16 cap)1450 struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap)
1451 {
1452 return _pci_find_saved_cap(dev, cap, true);
1453 }
1454
pci_save_pcie_state(struct pci_dev * dev)1455 static int pci_save_pcie_state(struct pci_dev *dev)
1456 {
1457 int i = 0;
1458 struct pci_cap_saved_state *save_state;
1459 u16 *cap;
1460
1461 if (!pci_is_pcie(dev))
1462 return 0;
1463
1464 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
1465 if (!save_state) {
1466 pci_err(dev, "buffer not found in %s\n", __func__);
1467 return -ENOMEM;
1468 }
1469
1470 cap = (u16 *)&save_state->cap.data[0];
1471 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &cap[i++]);
1472 pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]);
1473 pcie_capability_read_word(dev, PCI_EXP_SLTCTL, &cap[i++]);
1474 pcie_capability_read_word(dev, PCI_EXP_RTCTL, &cap[i++]);
1475 pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &cap[i++]);
1476 pcie_capability_read_word(dev, PCI_EXP_LNKCTL2, &cap[i++]);
1477 pcie_capability_read_word(dev, PCI_EXP_SLTCTL2, &cap[i++]);
1478
1479 return 0;
1480 }
1481
pci_restore_pcie_state(struct pci_dev * dev)1482 static void pci_restore_pcie_state(struct pci_dev *dev)
1483 {
1484 int i = 0;
1485 struct pci_cap_saved_state *save_state;
1486 u16 *cap;
1487
1488 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
1489 if (!save_state)
1490 return;
1491
1492 cap = (u16 *)&save_state->cap.data[0];
1493 pcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]);
1494 pcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]);
1495 pcie_capability_write_word(dev, PCI_EXP_SLTCTL, cap[i++]);
1496 pcie_capability_write_word(dev, PCI_EXP_RTCTL, cap[i++]);
1497 pcie_capability_write_word(dev, PCI_EXP_DEVCTL2, cap[i++]);
1498 pcie_capability_write_word(dev, PCI_EXP_LNKCTL2, cap[i++]);
1499 pcie_capability_write_word(dev, PCI_EXP_SLTCTL2, cap[i++]);
1500 }
1501
pci_save_pcix_state(struct pci_dev * dev)1502 static int pci_save_pcix_state(struct pci_dev *dev)
1503 {
1504 int pos;
1505 struct pci_cap_saved_state *save_state;
1506
1507 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1508 if (!pos)
1509 return 0;
1510
1511 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
1512 if (!save_state) {
1513 pci_err(dev, "buffer not found in %s\n", __func__);
1514 return -ENOMEM;
1515 }
1516
1517 pci_read_config_word(dev, pos + PCI_X_CMD,
1518 (u16 *)save_state->cap.data);
1519
1520 return 0;
1521 }
1522
pci_restore_pcix_state(struct pci_dev * dev)1523 static void pci_restore_pcix_state(struct pci_dev *dev)
1524 {
1525 int i = 0, pos;
1526 struct pci_cap_saved_state *save_state;
1527 u16 *cap;
1528
1529 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
1530 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1531 if (!save_state || !pos)
1532 return;
1533 cap = (u16 *)&save_state->cap.data[0];
1534
1535 pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
1536 }
1537
pci_save_ltr_state(struct pci_dev * dev)1538 static void pci_save_ltr_state(struct pci_dev *dev)
1539 {
1540 int ltr;
1541 struct pci_cap_saved_state *save_state;
1542 u16 *cap;
1543
1544 if (!pci_is_pcie(dev))
1545 return;
1546
1547 ltr = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_LTR);
1548 if (!ltr)
1549 return;
1550
1551 save_state = pci_find_saved_ext_cap(dev, PCI_EXT_CAP_ID_LTR);
1552 if (!save_state) {
1553 pci_err(dev, "no suspend buffer for LTR; ASPM issues possible after resume\n");
1554 return;
1555 }
1556
1557 cap = (u16 *)&save_state->cap.data[0];
1558 pci_read_config_word(dev, ltr + PCI_LTR_MAX_SNOOP_LAT, cap++);
1559 pci_read_config_word(dev, ltr + PCI_LTR_MAX_NOSNOOP_LAT, cap++);
1560 }
1561
pci_restore_ltr_state(struct pci_dev * dev)1562 static void pci_restore_ltr_state(struct pci_dev *dev)
1563 {
1564 struct pci_cap_saved_state *save_state;
1565 int ltr;
1566 u16 *cap;
1567
1568 save_state = pci_find_saved_ext_cap(dev, PCI_EXT_CAP_ID_LTR);
1569 ltr = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_LTR);
1570 if (!save_state || !ltr)
1571 return;
1572
1573 cap = (u16 *)&save_state->cap.data[0];
1574 pci_write_config_word(dev, ltr + PCI_LTR_MAX_SNOOP_LAT, *cap++);
1575 pci_write_config_word(dev, ltr + PCI_LTR_MAX_NOSNOOP_LAT, *cap++);
1576 }
1577
1578 /**
1579 * pci_save_state - save the PCI configuration space of a device before
1580 * suspending
1581 * @dev: PCI device that we're dealing with
1582 */
pci_save_state(struct pci_dev * dev)1583 int pci_save_state(struct pci_dev *dev)
1584 {
1585 int i;
1586 /* XXX: 100% dword access ok here? */
1587 for (i = 0; i < 16; i++) {
1588 pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]);
1589 pci_dbg(dev, "saving config space at offset %#x (reading %#x)\n",
1590 i * 4, dev->saved_config_space[i]);
1591 }
1592 dev->state_saved = true;
1593
1594 i = pci_save_pcie_state(dev);
1595 if (i != 0)
1596 return i;
1597
1598 i = pci_save_pcix_state(dev);
1599 if (i != 0)
1600 return i;
1601
1602 pci_save_ltr_state(dev);
1603 pci_save_dpc_state(dev);
1604 pci_save_aer_state(dev);
1605 pci_save_ptm_state(dev);
1606 return pci_save_vc_state(dev);
1607 }
1608 EXPORT_SYMBOL(pci_save_state);
1609
pci_restore_config_dword(struct pci_dev * pdev,int offset,u32 saved_val,int retry,bool force)1610 static void pci_restore_config_dword(struct pci_dev *pdev, int offset,
1611 u32 saved_val, int retry, bool force)
1612 {
1613 u32 val;
1614
1615 pci_read_config_dword(pdev, offset, &val);
1616 if (!force && val == saved_val)
1617 return;
1618
1619 for (;;) {
1620 pci_dbg(pdev, "restoring config space at offset %#x (was %#x, writing %#x)\n",
1621 offset, val, saved_val);
1622 pci_write_config_dword(pdev, offset, saved_val);
1623 if (retry-- <= 0)
1624 return;
1625
1626 pci_read_config_dword(pdev, offset, &val);
1627 if (val == saved_val)
1628 return;
1629
1630 mdelay(1);
1631 }
1632 }
1633
pci_restore_config_space_range(struct pci_dev * pdev,int start,int end,int retry,bool force)1634 static void pci_restore_config_space_range(struct pci_dev *pdev,
1635 int start, int end, int retry,
1636 bool force)
1637 {
1638 int index;
1639
1640 for (index = end; index >= start; index--)
1641 pci_restore_config_dword(pdev, 4 * index,
1642 pdev->saved_config_space[index],
1643 retry, force);
1644 }
1645
pci_restore_config_space(struct pci_dev * pdev)1646 static void pci_restore_config_space(struct pci_dev *pdev)
1647 {
1648 if (pdev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
1649 pci_restore_config_space_range(pdev, 10, 15, 0, false);
1650 /* Restore BARs before the command register. */
1651 pci_restore_config_space_range(pdev, 4, 9, 10, false);
1652 pci_restore_config_space_range(pdev, 0, 3, 0, false);
1653 } else if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1654 pci_restore_config_space_range(pdev, 12, 15, 0, false);
1655
1656 /*
1657 * Force rewriting of prefetch registers to avoid S3 resume
1658 * issues on Intel PCI bridges that occur when these
1659 * registers are not explicitly written.
1660 */
1661 pci_restore_config_space_range(pdev, 9, 11, 0, true);
1662 pci_restore_config_space_range(pdev, 0, 8, 0, false);
1663 } else {
1664 pci_restore_config_space_range(pdev, 0, 15, 0, false);
1665 }
1666 }
1667
pci_restore_rebar_state(struct pci_dev * pdev)1668 static void pci_restore_rebar_state(struct pci_dev *pdev)
1669 {
1670 unsigned int pos, nbars, i;
1671 u32 ctrl;
1672
1673 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_REBAR);
1674 if (!pos)
1675 return;
1676
1677 pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
1678 nbars = (ctrl & PCI_REBAR_CTRL_NBAR_MASK) >>
1679 PCI_REBAR_CTRL_NBAR_SHIFT;
1680
1681 for (i = 0; i < nbars; i++, pos += 8) {
1682 struct resource *res;
1683 int bar_idx, size;
1684
1685 pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
1686 bar_idx = ctrl & PCI_REBAR_CTRL_BAR_IDX;
1687 res = pdev->resource + bar_idx;
1688 size = pci_rebar_bytes_to_size(resource_size(res));
1689 ctrl &= ~PCI_REBAR_CTRL_BAR_SIZE;
1690 ctrl |= size << PCI_REBAR_CTRL_BAR_SHIFT;
1691 pci_write_config_dword(pdev, pos + PCI_REBAR_CTRL, ctrl);
1692 }
1693 }
1694
1695 /**
1696 * pci_restore_state - Restore the saved state of a PCI device
1697 * @dev: PCI device that we're dealing with
1698 */
pci_restore_state(struct pci_dev * dev)1699 void pci_restore_state(struct pci_dev *dev)
1700 {
1701 if (!dev->state_saved)
1702 return;
1703
1704 /*
1705 * Restore max latencies (in the LTR capability) before enabling
1706 * LTR itself (in the PCIe capability).
1707 */
1708 pci_restore_ltr_state(dev);
1709
1710 pci_restore_pcie_state(dev);
1711 pci_restore_pasid_state(dev);
1712 pci_restore_pri_state(dev);
1713 pci_restore_ats_state(dev);
1714 pci_restore_vc_state(dev);
1715 pci_restore_rebar_state(dev);
1716 pci_restore_dpc_state(dev);
1717 pci_restore_ptm_state(dev);
1718
1719 pci_aer_clear_status(dev);
1720 pci_restore_aer_state(dev);
1721
1722 pci_restore_config_space(dev);
1723
1724 pci_restore_pcix_state(dev);
1725 pci_restore_msi_state(dev);
1726
1727 /* Restore ACS and IOV configuration state */
1728 pci_enable_acs(dev);
1729 pci_restore_iov_state(dev);
1730
1731 dev->state_saved = false;
1732 }
1733 EXPORT_SYMBOL(pci_restore_state);
1734
1735 struct pci_saved_state {
1736 u32 config_space[16];
1737 struct pci_cap_saved_data cap[];
1738 };
1739
1740 /**
1741 * pci_store_saved_state - Allocate and return an opaque struct containing
1742 * the device saved state.
1743 * @dev: PCI device that we're dealing with
1744 *
1745 * Return NULL if no state or error.
1746 */
pci_store_saved_state(struct pci_dev * dev)1747 struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev)
1748 {
1749 struct pci_saved_state *state;
1750 struct pci_cap_saved_state *tmp;
1751 struct pci_cap_saved_data *cap;
1752 size_t size;
1753
1754 if (!dev->state_saved)
1755 return NULL;
1756
1757 size = sizeof(*state) + sizeof(struct pci_cap_saved_data);
1758
1759 hlist_for_each_entry(tmp, &dev->saved_cap_space, next)
1760 size += sizeof(struct pci_cap_saved_data) + tmp->cap.size;
1761
1762 state = kzalloc(size, GFP_KERNEL);
1763 if (!state)
1764 return NULL;
1765
1766 memcpy(state->config_space, dev->saved_config_space,
1767 sizeof(state->config_space));
1768
1769 cap = state->cap;
1770 hlist_for_each_entry(tmp, &dev->saved_cap_space, next) {
1771 size_t len = sizeof(struct pci_cap_saved_data) + tmp->cap.size;
1772 memcpy(cap, &tmp->cap, len);
1773 cap = (struct pci_cap_saved_data *)((u8 *)cap + len);
1774 }
1775 /* Empty cap_save terminates list */
1776
1777 return state;
1778 }
1779 EXPORT_SYMBOL_GPL(pci_store_saved_state);
1780
1781 /**
1782 * pci_load_saved_state - Reload the provided save state into struct pci_dev.
1783 * @dev: PCI device that we're dealing with
1784 * @state: Saved state returned from pci_store_saved_state()
1785 */
pci_load_saved_state(struct pci_dev * dev,struct pci_saved_state * state)1786 int pci_load_saved_state(struct pci_dev *dev,
1787 struct pci_saved_state *state)
1788 {
1789 struct pci_cap_saved_data *cap;
1790
1791 dev->state_saved = false;
1792
1793 if (!state)
1794 return 0;
1795
1796 memcpy(dev->saved_config_space, state->config_space,
1797 sizeof(state->config_space));
1798
1799 cap = state->cap;
1800 while (cap->size) {
1801 struct pci_cap_saved_state *tmp;
1802
1803 tmp = _pci_find_saved_cap(dev, cap->cap_nr, cap->cap_extended);
1804 if (!tmp || tmp->cap.size != cap->size)
1805 return -EINVAL;
1806
1807 memcpy(tmp->cap.data, cap->data, tmp->cap.size);
1808 cap = (struct pci_cap_saved_data *)((u8 *)cap +
1809 sizeof(struct pci_cap_saved_data) + cap->size);
1810 }
1811
1812 dev->state_saved = true;
1813 return 0;
1814 }
1815 EXPORT_SYMBOL_GPL(pci_load_saved_state);
1816
1817 /**
1818 * pci_load_and_free_saved_state - Reload the save state pointed to by state,
1819 * and free the memory allocated for it.
1820 * @dev: PCI device that we're dealing with
1821 * @state: Pointer to saved state returned from pci_store_saved_state()
1822 */
pci_load_and_free_saved_state(struct pci_dev * dev,struct pci_saved_state ** state)1823 int pci_load_and_free_saved_state(struct pci_dev *dev,
1824 struct pci_saved_state **state)
1825 {
1826 int ret = pci_load_saved_state(dev, *state);
1827 kfree(*state);
1828 *state = NULL;
1829 return ret;
1830 }
1831 EXPORT_SYMBOL_GPL(pci_load_and_free_saved_state);
1832
pcibios_enable_device(struct pci_dev * dev,int bars)1833 int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
1834 {
1835 return pci_enable_resources(dev, bars);
1836 }
1837
do_pci_enable_device(struct pci_dev * dev,int bars)1838 static int do_pci_enable_device(struct pci_dev *dev, int bars)
1839 {
1840 int err;
1841 struct pci_dev *bridge;
1842 u16 cmd;
1843 u8 pin;
1844
1845 err = pci_set_power_state(dev, PCI_D0);
1846 if (err < 0 && err != -EIO)
1847 return err;
1848
1849 bridge = pci_upstream_bridge(dev);
1850 if (bridge)
1851 pcie_aspm_powersave_config_link(bridge);
1852
1853 err = pcibios_enable_device(dev, bars);
1854 if (err < 0)
1855 return err;
1856 pci_fixup_device(pci_fixup_enable, dev);
1857
1858 if (dev->msi_enabled || dev->msix_enabled)
1859 return 0;
1860
1861 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
1862 if (pin) {
1863 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1864 if (cmd & PCI_COMMAND_INTX_DISABLE)
1865 pci_write_config_word(dev, PCI_COMMAND,
1866 cmd & ~PCI_COMMAND_INTX_DISABLE);
1867 }
1868
1869 return 0;
1870 }
1871
1872 /**
1873 * pci_reenable_device - Resume abandoned device
1874 * @dev: PCI device to be resumed
1875 *
1876 * NOTE: This function is a backend of pci_default_resume() and is not supposed
1877 * to be called by normal code, write proper resume handler and use it instead.
1878 */
pci_reenable_device(struct pci_dev * dev)1879 int pci_reenable_device(struct pci_dev *dev)
1880 {
1881 if (pci_is_enabled(dev))
1882 return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
1883 return 0;
1884 }
1885 EXPORT_SYMBOL(pci_reenable_device);
1886
pci_enable_bridge(struct pci_dev * dev)1887 static void pci_enable_bridge(struct pci_dev *dev)
1888 {
1889 struct pci_dev *bridge;
1890 int retval;
1891
1892 bridge = pci_upstream_bridge(dev);
1893 if (bridge)
1894 pci_enable_bridge(bridge);
1895
1896 if (pci_is_enabled(dev)) {
1897 if (!dev->is_busmaster)
1898 pci_set_master(dev);
1899 return;
1900 }
1901
1902 retval = pci_enable_device(dev);
1903 if (retval)
1904 pci_err(dev, "Error enabling bridge (%d), continuing\n",
1905 retval);
1906 pci_set_master(dev);
1907 }
1908
pci_enable_device_flags(struct pci_dev * dev,unsigned long flags)1909 static int pci_enable_device_flags(struct pci_dev *dev, unsigned long flags)
1910 {
1911 struct pci_dev *bridge;
1912 int err;
1913 int i, bars = 0;
1914
1915 /*
1916 * Power state could be unknown at this point, either due to a fresh
1917 * boot or a device removal call. So get the current power state
1918 * so that things like MSI message writing will behave as expected
1919 * (e.g. if the device really is in D0 at enable time).
1920 */
1921 pci_update_current_state(dev, dev->current_state);
1922
1923 if (atomic_inc_return(&dev->enable_cnt) > 1)
1924 return 0; /* already enabled */
1925
1926 bridge = pci_upstream_bridge(dev);
1927 if (bridge)
1928 pci_enable_bridge(bridge);
1929
1930 /* only skip sriov related */
1931 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
1932 if (dev->resource[i].flags & flags)
1933 bars |= (1 << i);
1934 for (i = PCI_BRIDGE_RESOURCES; i < DEVICE_COUNT_RESOURCE; i++)
1935 if (dev->resource[i].flags & flags)
1936 bars |= (1 << i);
1937
1938 err = do_pci_enable_device(dev, bars);
1939 if (err < 0)
1940 atomic_dec(&dev->enable_cnt);
1941 return err;
1942 }
1943
1944 /**
1945 * pci_enable_device_io - Initialize a device for use with IO space
1946 * @dev: PCI device to be initialized
1947 *
1948 * Initialize device before it's used by a driver. Ask low-level code
1949 * to enable I/O resources. Wake up the device if it was suspended.
1950 * Beware, this function can fail.
1951 */
pci_enable_device_io(struct pci_dev * dev)1952 int pci_enable_device_io(struct pci_dev *dev)
1953 {
1954 return pci_enable_device_flags(dev, IORESOURCE_IO);
1955 }
1956 EXPORT_SYMBOL(pci_enable_device_io);
1957
1958 /**
1959 * pci_enable_device_mem - Initialize a device for use with Memory space
1960 * @dev: PCI device to be initialized
1961 *
1962 * Initialize device before it's used by a driver. Ask low-level code
1963 * to enable Memory resources. Wake up the device if it was suspended.
1964 * Beware, this function can fail.
1965 */
pci_enable_device_mem(struct pci_dev * dev)1966 int pci_enable_device_mem(struct pci_dev *dev)
1967 {
1968 return pci_enable_device_flags(dev, IORESOURCE_MEM);
1969 }
1970 EXPORT_SYMBOL(pci_enable_device_mem);
1971
1972 /**
1973 * pci_enable_device - Initialize device before it's used by a driver.
1974 * @dev: PCI device to be initialized
1975 *
1976 * Initialize device before it's used by a driver. Ask low-level code
1977 * to enable I/O and memory. Wake up the device if it was suspended.
1978 * Beware, this function can fail.
1979 *
1980 * Note we don't actually enable the device many times if we call
1981 * this function repeatedly (we just increment the count).
1982 */
pci_enable_device(struct pci_dev * dev)1983 int pci_enable_device(struct pci_dev *dev)
1984 {
1985 return pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
1986 }
1987 EXPORT_SYMBOL(pci_enable_device);
1988
1989 /*
1990 * Managed PCI resources. This manages device on/off, INTx/MSI/MSI-X
1991 * on/off and BAR regions. pci_dev itself records MSI/MSI-X status, so
1992 * there's no need to track it separately. pci_devres is initialized
1993 * when a device is enabled using managed PCI device enable interface.
1994 */
1995 struct pci_devres {
1996 unsigned int enabled:1;
1997 unsigned int pinned:1;
1998 unsigned int orig_intx:1;
1999 unsigned int restore_intx:1;
2000 unsigned int mwi:1;
2001 u32 region_mask;
2002 };
2003
pcim_release(struct device * gendev,void * res)2004 static void pcim_release(struct device *gendev, void *res)
2005 {
2006 struct pci_dev *dev = to_pci_dev(gendev);
2007 struct pci_devres *this = res;
2008 int i;
2009
2010 if (dev->msi_enabled)
2011 pci_disable_msi(dev);
2012 if (dev->msix_enabled)
2013 pci_disable_msix(dev);
2014
2015 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
2016 if (this->region_mask & (1 << i))
2017 pci_release_region(dev, i);
2018
2019 if (this->mwi)
2020 pci_clear_mwi(dev);
2021
2022 if (this->restore_intx)
2023 pci_intx(dev, this->orig_intx);
2024
2025 if (this->enabled && !this->pinned)
2026 pci_disable_device(dev);
2027 }
2028
get_pci_dr(struct pci_dev * pdev)2029 static struct pci_devres *get_pci_dr(struct pci_dev *pdev)
2030 {
2031 struct pci_devres *dr, *new_dr;
2032
2033 dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
2034 if (dr)
2035 return dr;
2036
2037 new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
2038 if (!new_dr)
2039 return NULL;
2040 return devres_get(&pdev->dev, new_dr, NULL, NULL);
2041 }
2042
find_pci_dr(struct pci_dev * pdev)2043 static struct pci_devres *find_pci_dr(struct pci_dev *pdev)
2044 {
2045 if (pci_is_managed(pdev))
2046 return devres_find(&pdev->dev, pcim_release, NULL, NULL);
2047 return NULL;
2048 }
2049
2050 /**
2051 * pcim_enable_device - Managed pci_enable_device()
2052 * @pdev: PCI device to be initialized
2053 *
2054 * Managed pci_enable_device().
2055 */
pcim_enable_device(struct pci_dev * pdev)2056 int pcim_enable_device(struct pci_dev *pdev)
2057 {
2058 struct pci_devres *dr;
2059 int rc;
2060
2061 dr = get_pci_dr(pdev);
2062 if (unlikely(!dr))
2063 return -ENOMEM;
2064 if (dr->enabled)
2065 return 0;
2066
2067 rc = pci_enable_device(pdev);
2068 if (!rc) {
2069 pdev->is_managed = 1;
2070 dr->enabled = 1;
2071 }
2072 return rc;
2073 }
2074 EXPORT_SYMBOL(pcim_enable_device);
2075
2076 /**
2077 * pcim_pin_device - Pin managed PCI device
2078 * @pdev: PCI device to pin
2079 *
2080 * Pin managed PCI device @pdev. Pinned device won't be disabled on
2081 * driver detach. @pdev must have been enabled with
2082 * pcim_enable_device().
2083 */
pcim_pin_device(struct pci_dev * pdev)2084 void pcim_pin_device(struct pci_dev *pdev)
2085 {
2086 struct pci_devres *dr;
2087
2088 dr = find_pci_dr(pdev);
2089 WARN_ON(!dr || !dr->enabled);
2090 if (dr)
2091 dr->pinned = 1;
2092 }
2093 EXPORT_SYMBOL(pcim_pin_device);
2094
2095 /*
2096 * pcibios_add_device - provide arch specific hooks when adding device dev
2097 * @dev: the PCI device being added
2098 *
2099 * Permits the platform to provide architecture specific functionality when
2100 * devices are added. This is the default implementation. Architecture
2101 * implementations can override this.
2102 */
pcibios_add_device(struct pci_dev * dev)2103 int __weak pcibios_add_device(struct pci_dev *dev)
2104 {
2105 return 0;
2106 }
2107
2108 /**
2109 * pcibios_release_device - provide arch specific hooks when releasing
2110 * device dev
2111 * @dev: the PCI device being released
2112 *
2113 * Permits the platform to provide architecture specific functionality when
2114 * devices are released. This is the default implementation. Architecture
2115 * implementations can override this.
2116 */
pcibios_release_device(struct pci_dev * dev)2117 void __weak pcibios_release_device(struct pci_dev *dev) {}
2118
2119 /**
2120 * pcibios_disable_device - disable arch specific PCI resources for device dev
2121 * @dev: the PCI device to disable
2122 *
2123 * Disables architecture specific PCI resources for the device. This
2124 * is the default implementation. Architecture implementations can
2125 * override this.
2126 */
pcibios_disable_device(struct pci_dev * dev)2127 void __weak pcibios_disable_device(struct pci_dev *dev) {}
2128
2129 /**
2130 * pcibios_penalize_isa_irq - penalize an ISA IRQ
2131 * @irq: ISA IRQ to penalize
2132 * @active: IRQ active or not
2133 *
2134 * Permits the platform to provide architecture-specific functionality when
2135 * penalizing ISA IRQs. This is the default implementation. Architecture
2136 * implementations can override this.
2137 */
pcibios_penalize_isa_irq(int irq,int active)2138 void __weak pcibios_penalize_isa_irq(int irq, int active) {}
2139
do_pci_disable_device(struct pci_dev * dev)2140 static void do_pci_disable_device(struct pci_dev *dev)
2141 {
2142 u16 pci_command;
2143
2144 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
2145 if (pci_command & PCI_COMMAND_MASTER) {
2146 pci_command &= ~PCI_COMMAND_MASTER;
2147 pci_write_config_word(dev, PCI_COMMAND, pci_command);
2148 }
2149
2150 pcibios_disable_device(dev);
2151 }
2152
2153 /**
2154 * pci_disable_enabled_device - Disable device without updating enable_cnt
2155 * @dev: PCI device to disable
2156 *
2157 * NOTE: This function is a backend of PCI power management routines and is
2158 * not supposed to be called drivers.
2159 */
pci_disable_enabled_device(struct pci_dev * dev)2160 void pci_disable_enabled_device(struct pci_dev *dev)
2161 {
2162 if (pci_is_enabled(dev))
2163 do_pci_disable_device(dev);
2164 }
2165
2166 /**
2167 * pci_disable_device - Disable PCI device after use
2168 * @dev: PCI device to be disabled
2169 *
2170 * Signal to the system that the PCI device is not in use by the system
2171 * anymore. This only involves disabling PCI bus-mastering, if active.
2172 *
2173 * Note we don't actually disable the device until all callers of
2174 * pci_enable_device() have called pci_disable_device().
2175 */
pci_disable_device(struct pci_dev * dev)2176 void pci_disable_device(struct pci_dev *dev)
2177 {
2178 struct pci_devres *dr;
2179
2180 dr = find_pci_dr(dev);
2181 if (dr)
2182 dr->enabled = 0;
2183
2184 dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0,
2185 "disabling already-disabled device");
2186
2187 if (atomic_dec_return(&dev->enable_cnt) != 0)
2188 return;
2189
2190 do_pci_disable_device(dev);
2191
2192 dev->is_busmaster = 0;
2193 }
2194 EXPORT_SYMBOL(pci_disable_device);
2195
2196 /**
2197 * pcibios_set_pcie_reset_state - set reset state for device dev
2198 * @dev: the PCIe device reset
2199 * @state: Reset state to enter into
2200 *
2201 * Set the PCIe reset state for the device. This is the default
2202 * implementation. Architecture implementations can override this.
2203 */
pcibios_set_pcie_reset_state(struct pci_dev * dev,enum pcie_reset_state state)2204 int __weak pcibios_set_pcie_reset_state(struct pci_dev *dev,
2205 enum pcie_reset_state state)
2206 {
2207 return -EINVAL;
2208 }
2209
2210 /**
2211 * pci_set_pcie_reset_state - set reset state for device dev
2212 * @dev: the PCIe device reset
2213 * @state: Reset state to enter into
2214 *
2215 * Sets the PCI reset state for the device.
2216 */
pci_set_pcie_reset_state(struct pci_dev * dev,enum pcie_reset_state state)2217 int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
2218 {
2219 return pcibios_set_pcie_reset_state(dev, state);
2220 }
2221 EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);
2222
pcie_clear_device_status(struct pci_dev * dev)2223 void pcie_clear_device_status(struct pci_dev *dev)
2224 {
2225 u16 sta;
2226
2227 pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &sta);
2228 pcie_capability_write_word(dev, PCI_EXP_DEVSTA, sta);
2229 }
2230
2231 /**
2232 * pcie_clear_root_pme_status - Clear root port PME interrupt status.
2233 * @dev: PCIe root port or event collector.
2234 */
pcie_clear_root_pme_status(struct pci_dev * dev)2235 void pcie_clear_root_pme_status(struct pci_dev *dev)
2236 {
2237 pcie_capability_set_dword(dev, PCI_EXP_RTSTA, PCI_EXP_RTSTA_PME);
2238 }
2239
2240 /**
2241 * pci_check_pme_status - Check if given device has generated PME.
2242 * @dev: Device to check.
2243 *
2244 * Check the PME status of the device and if set, clear it and clear PME enable
2245 * (if set). Return 'true' if PME status and PME enable were both set or
2246 * 'false' otherwise.
2247 */
pci_check_pme_status(struct pci_dev * dev)2248 bool pci_check_pme_status(struct pci_dev *dev)
2249 {
2250 int pmcsr_pos;
2251 u16 pmcsr;
2252 bool ret = false;
2253
2254 if (!dev->pm_cap)
2255 return false;
2256
2257 pmcsr_pos = dev->pm_cap + PCI_PM_CTRL;
2258 pci_read_config_word(dev, pmcsr_pos, &pmcsr);
2259 if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
2260 return false;
2261
2262 /* Clear PME status. */
2263 pmcsr |= PCI_PM_CTRL_PME_STATUS;
2264 if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
2265 /* Disable PME to avoid interrupt flood. */
2266 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
2267 ret = true;
2268 }
2269
2270 pci_write_config_word(dev, pmcsr_pos, pmcsr);
2271
2272 return ret;
2273 }
2274
2275 /**
2276 * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set.
2277 * @dev: Device to handle.
2278 * @pme_poll_reset: Whether or not to reset the device's pme_poll flag.
2279 *
2280 * Check if @dev has generated PME and queue a resume request for it in that
2281 * case.
2282 */
pci_pme_wakeup(struct pci_dev * dev,void * pme_poll_reset)2283 static int pci_pme_wakeup(struct pci_dev *dev, void *pme_poll_reset)
2284 {
2285 if (pme_poll_reset && dev->pme_poll)
2286 dev->pme_poll = false;
2287
2288 if (pci_check_pme_status(dev)) {
2289 pci_wakeup_event(dev);
2290 pm_request_resume(&dev->dev);
2291 }
2292 return 0;
2293 }
2294
2295 /**
2296 * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary.
2297 * @bus: Top bus of the subtree to walk.
2298 */
pci_pme_wakeup_bus(struct pci_bus * bus)2299 void pci_pme_wakeup_bus(struct pci_bus *bus)
2300 {
2301 if (bus)
2302 pci_walk_bus(bus, pci_pme_wakeup, (void *)true);
2303 }
2304
2305
2306 /**
2307 * pci_pme_capable - check the capability of PCI device to generate PME#
2308 * @dev: PCI device to handle.
2309 * @state: PCI state from which device will issue PME#.
2310 */
pci_pme_capable(struct pci_dev * dev,pci_power_t state)2311 bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
2312 {
2313 if (!dev->pm_cap)
2314 return false;
2315
2316 return !!(dev->pme_support & (1 << state));
2317 }
2318 EXPORT_SYMBOL(pci_pme_capable);
2319
pci_pme_list_scan(struct work_struct * work)2320 static void pci_pme_list_scan(struct work_struct *work)
2321 {
2322 struct pci_pme_device *pme_dev, *n;
2323
2324 mutex_lock(&pci_pme_list_mutex);
2325 list_for_each_entry_safe(pme_dev, n, &pci_pme_list, list) {
2326 if (pme_dev->dev->pme_poll) {
2327 struct pci_dev *bridge;
2328
2329 bridge = pme_dev->dev->bus->self;
2330 /*
2331 * If bridge is in low power state, the
2332 * configuration space of subordinate devices
2333 * may be not accessible
2334 */
2335 if (bridge && bridge->current_state != PCI_D0)
2336 continue;
2337 /*
2338 * If the device is in D3cold it should not be
2339 * polled either.
2340 */
2341 if (pme_dev->dev->current_state == PCI_D3cold)
2342 continue;
2343
2344 pci_pme_wakeup(pme_dev->dev, NULL);
2345 } else {
2346 list_del(&pme_dev->list);
2347 kfree(pme_dev);
2348 }
2349 }
2350 if (!list_empty(&pci_pme_list))
2351 queue_delayed_work(system_freezable_wq, &pci_pme_work,
2352 msecs_to_jiffies(PME_TIMEOUT));
2353 mutex_unlock(&pci_pme_list_mutex);
2354 }
2355
__pci_pme_active(struct pci_dev * dev,bool enable)2356 static void __pci_pme_active(struct pci_dev *dev, bool enable)
2357 {
2358 u16 pmcsr;
2359
2360 if (!dev->pme_support)
2361 return;
2362
2363 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
2364 /* Clear PME_Status by writing 1 to it and enable PME# */
2365 pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
2366 if (!enable)
2367 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
2368
2369 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
2370 }
2371
2372 /**
2373 * pci_pme_restore - Restore PME configuration after config space restore.
2374 * @dev: PCI device to update.
2375 */
pci_pme_restore(struct pci_dev * dev)2376 void pci_pme_restore(struct pci_dev *dev)
2377 {
2378 u16 pmcsr;
2379
2380 if (!dev->pme_support)
2381 return;
2382
2383 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
2384 if (dev->wakeup_prepared) {
2385 pmcsr |= PCI_PM_CTRL_PME_ENABLE;
2386 pmcsr &= ~PCI_PM_CTRL_PME_STATUS;
2387 } else {
2388 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
2389 pmcsr |= PCI_PM_CTRL_PME_STATUS;
2390 }
2391 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
2392 }
2393
2394 /**
2395 * pci_pme_active - enable or disable PCI device's PME# function
2396 * @dev: PCI device to handle.
2397 * @enable: 'true' to enable PME# generation; 'false' to disable it.
2398 *
2399 * The caller must verify that the device is capable of generating PME# before
2400 * calling this function with @enable equal to 'true'.
2401 */
pci_pme_active(struct pci_dev * dev,bool enable)2402 void pci_pme_active(struct pci_dev *dev, bool enable)
2403 {
2404 __pci_pme_active(dev, enable);
2405
2406 /*
2407 * PCI (as opposed to PCIe) PME requires that the device have
2408 * its PME# line hooked up correctly. Not all hardware vendors
2409 * do this, so the PME never gets delivered and the device
2410 * remains asleep. The easiest way around this is to
2411 * periodically walk the list of suspended devices and check
2412 * whether any have their PME flag set. The assumption is that
2413 * we'll wake up often enough anyway that this won't be a huge
2414 * hit, and the power savings from the devices will still be a
2415 * win.
2416 *
2417 * Although PCIe uses in-band PME message instead of PME# line
2418 * to report PME, PME does not work for some PCIe devices in
2419 * reality. For example, there are devices that set their PME
2420 * status bits, but don't really bother to send a PME message;
2421 * there are PCI Express Root Ports that don't bother to
2422 * trigger interrupts when they receive PME messages from the
2423 * devices below. So PME poll is used for PCIe devices too.
2424 */
2425
2426 if (dev->pme_poll) {
2427 struct pci_pme_device *pme_dev;
2428 if (enable) {
2429 pme_dev = kmalloc(sizeof(struct pci_pme_device),
2430 GFP_KERNEL);
2431 if (!pme_dev) {
2432 pci_warn(dev, "can't enable PME#\n");
2433 return;
2434 }
2435 pme_dev->dev = dev;
2436 mutex_lock(&pci_pme_list_mutex);
2437 list_add(&pme_dev->list, &pci_pme_list);
2438 if (list_is_singular(&pci_pme_list))
2439 queue_delayed_work(system_freezable_wq,
2440 &pci_pme_work,
2441 msecs_to_jiffies(PME_TIMEOUT));
2442 mutex_unlock(&pci_pme_list_mutex);
2443 } else {
2444 mutex_lock(&pci_pme_list_mutex);
2445 list_for_each_entry(pme_dev, &pci_pme_list, list) {
2446 if (pme_dev->dev == dev) {
2447 list_del(&pme_dev->list);
2448 kfree(pme_dev);
2449 break;
2450 }
2451 }
2452 mutex_unlock(&pci_pme_list_mutex);
2453 }
2454 }
2455
2456 pci_dbg(dev, "PME# %s\n", enable ? "enabled" : "disabled");
2457 }
2458 EXPORT_SYMBOL(pci_pme_active);
2459
2460 /**
2461 * __pci_enable_wake - enable PCI device as wakeup event source
2462 * @dev: PCI device affected
2463 * @state: PCI state from which device will issue wakeup events
2464 * @enable: True to enable event generation; false to disable
2465 *
2466 * This enables the device as a wakeup event source, or disables it.
2467 * When such events involves platform-specific hooks, those hooks are
2468 * called automatically by this routine.
2469 *
2470 * Devices with legacy power management (no standard PCI PM capabilities)
2471 * always require such platform hooks.
2472 *
2473 * RETURN VALUE:
2474 * 0 is returned on success
2475 * -EINVAL is returned if device is not supposed to wake up the system
2476 * Error code depending on the platform is returned if both the platform and
2477 * the native mechanism fail to enable the generation of wake-up events
2478 */
__pci_enable_wake(struct pci_dev * dev,pci_power_t state,bool enable)2479 static int __pci_enable_wake(struct pci_dev *dev, pci_power_t state, bool enable)
2480 {
2481 int ret = 0;
2482
2483 /*
2484 * Bridges that are not power-manageable directly only signal
2485 * wakeup on behalf of subordinate devices which is set up
2486 * elsewhere, so skip them. However, bridges that are
2487 * power-manageable may signal wakeup for themselves (for example,
2488 * on a hotplug event) and they need to be covered here.
2489 */
2490 if (!pci_power_manageable(dev))
2491 return 0;
2492
2493 /* Don't do the same thing twice in a row for one device. */
2494 if (!!enable == !!dev->wakeup_prepared)
2495 return 0;
2496
2497 /*
2498 * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
2499 * Anderson we should be doing PME# wake enable followed by ACPI wake
2500 * enable. To disable wake-up we call the platform first, for symmetry.
2501 */
2502
2503 if (enable) {
2504 int error;
2505
2506 /*
2507 * Enable PME signaling if the device can signal PME from
2508 * D3cold regardless of whether or not it can signal PME from
2509 * the current target state, because that will allow it to
2510 * signal PME when the hierarchy above it goes into D3cold and
2511 * the device itself ends up in D3cold as a result of that.
2512 */
2513 if (pci_pme_capable(dev, state) || pci_pme_capable(dev, PCI_D3cold))
2514 pci_pme_active(dev, true);
2515 else
2516 ret = 1;
2517 error = platform_pci_set_wakeup(dev, true);
2518 if (ret)
2519 ret = error;
2520 if (!ret)
2521 dev->wakeup_prepared = true;
2522 } else {
2523 platform_pci_set_wakeup(dev, false);
2524 pci_pme_active(dev, false);
2525 dev->wakeup_prepared = false;
2526 }
2527
2528 return ret;
2529 }
2530
2531 /**
2532 * pci_enable_wake - change wakeup settings for a PCI device
2533 * @pci_dev: Target device
2534 * @state: PCI state from which device will issue wakeup events
2535 * @enable: Whether or not to enable event generation
2536 *
2537 * If @enable is set, check device_may_wakeup() for the device before calling
2538 * __pci_enable_wake() for it.
2539 */
pci_enable_wake(struct pci_dev * pci_dev,pci_power_t state,bool enable)2540 int pci_enable_wake(struct pci_dev *pci_dev, pci_power_t state, bool enable)
2541 {
2542 if (enable && !device_may_wakeup(&pci_dev->dev))
2543 return -EINVAL;
2544
2545 return __pci_enable_wake(pci_dev, state, enable);
2546 }
2547 EXPORT_SYMBOL(pci_enable_wake);
2548
2549 /**
2550 * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
2551 * @dev: PCI device to prepare
2552 * @enable: True to enable wake-up event generation; false to disable
2553 *
2554 * Many drivers want the device to wake up the system from D3_hot or D3_cold
2555 * and this function allows them to set that up cleanly - pci_enable_wake()
2556 * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
2557 * ordering constraints.
2558 *
2559 * This function only returns error code if the device is not allowed to wake
2560 * up the system from sleep or it is not capable of generating PME# from both
2561 * D3_hot and D3_cold and the platform is unable to enable wake-up power for it.
2562 */
pci_wake_from_d3(struct pci_dev * dev,bool enable)2563 int pci_wake_from_d3(struct pci_dev *dev, bool enable)
2564 {
2565 return pci_pme_capable(dev, PCI_D3cold) ?
2566 pci_enable_wake(dev, PCI_D3cold, enable) :
2567 pci_enable_wake(dev, PCI_D3hot, enable);
2568 }
2569 EXPORT_SYMBOL(pci_wake_from_d3);
2570
2571 /**
2572 * pci_target_state - find an appropriate low power state for a given PCI dev
2573 * @dev: PCI device
2574 * @wakeup: Whether or not wakeup functionality will be enabled for the device.
2575 *
2576 * Use underlying platform code to find a supported low power state for @dev.
2577 * If the platform can't manage @dev, return the deepest state from which it
2578 * can generate wake events, based on any available PME info.
2579 */
pci_target_state(struct pci_dev * dev,bool wakeup)2580 static pci_power_t pci_target_state(struct pci_dev *dev, bool wakeup)
2581 {
2582 pci_power_t target_state = PCI_D3hot;
2583
2584 if (platform_pci_power_manageable(dev)) {
2585 /*
2586 * Call the platform to find the target state for the device.
2587 */
2588 pci_power_t state = platform_pci_choose_state(dev);
2589
2590 switch (state) {
2591 case PCI_POWER_ERROR:
2592 case PCI_UNKNOWN:
2593 break;
2594 case PCI_D1:
2595 case PCI_D2:
2596 if (pci_no_d1d2(dev))
2597 break;
2598 fallthrough;
2599 default:
2600 target_state = state;
2601 }
2602
2603 return target_state;
2604 }
2605
2606 if (!dev->pm_cap)
2607 target_state = PCI_D0;
2608
2609 /*
2610 * If the device is in D3cold even though it's not power-manageable by
2611 * the platform, it may have been powered down by non-standard means.
2612 * Best to let it slumber.
2613 */
2614 if (dev->current_state == PCI_D3cold)
2615 target_state = PCI_D3cold;
2616
2617 if (wakeup && dev->pme_support) {
2618 pci_power_t state = target_state;
2619
2620 /*
2621 * Find the deepest state from which the device can generate
2622 * PME#.
2623 */
2624 while (state && !(dev->pme_support & (1 << state)))
2625 state--;
2626
2627 if (state)
2628 return state;
2629 else if (dev->pme_support & 1)
2630 return PCI_D0;
2631 }
2632
2633 return target_state;
2634 }
2635
2636 /**
2637 * pci_prepare_to_sleep - prepare PCI device for system-wide transition
2638 * into a sleep state
2639 * @dev: Device to handle.
2640 *
2641 * Choose the power state appropriate for the device depending on whether
2642 * it can wake up the system and/or is power manageable by the platform
2643 * (PCI_D3hot is the default) and put the device into that state.
2644 */
pci_prepare_to_sleep(struct pci_dev * dev)2645 int pci_prepare_to_sleep(struct pci_dev *dev)
2646 {
2647 bool wakeup = device_may_wakeup(&dev->dev);
2648 pci_power_t target_state = pci_target_state(dev, wakeup);
2649 int error;
2650
2651 if (target_state == PCI_POWER_ERROR)
2652 return -EIO;
2653
2654 /*
2655 * There are systems (for example, Intel mobile chips since Coffee
2656 * Lake) where the power drawn while suspended can be significantly
2657 * reduced by disabling PTM on PCIe root ports as this allows the
2658 * port to enter a lower-power PM state and the SoC to reach a
2659 * lower-power idle state as a whole.
2660 */
2661 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT)
2662 pci_disable_ptm(dev);
2663
2664 pci_enable_wake(dev, target_state, wakeup);
2665
2666 error = pci_set_power_state(dev, target_state);
2667
2668 if (error) {
2669 pci_enable_wake(dev, target_state, false);
2670 pci_restore_ptm_state(dev);
2671 }
2672
2673 return error;
2674 }
2675 EXPORT_SYMBOL(pci_prepare_to_sleep);
2676
2677 /**
2678 * pci_back_from_sleep - turn PCI device on during system-wide transition
2679 * into working state
2680 * @dev: Device to handle.
2681 *
2682 * Disable device's system wake-up capability and put it into D0.
2683 */
pci_back_from_sleep(struct pci_dev * dev)2684 int pci_back_from_sleep(struct pci_dev *dev)
2685 {
2686 pci_enable_wake(dev, PCI_D0, false);
2687 return pci_set_power_state(dev, PCI_D0);
2688 }
2689 EXPORT_SYMBOL(pci_back_from_sleep);
2690
2691 /**
2692 * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend.
2693 * @dev: PCI device being suspended.
2694 *
2695 * Prepare @dev to generate wake-up events at run time and put it into a low
2696 * power state.
2697 */
pci_finish_runtime_suspend(struct pci_dev * dev)2698 int pci_finish_runtime_suspend(struct pci_dev *dev)
2699 {
2700 pci_power_t target_state;
2701 int error;
2702
2703 target_state = pci_target_state(dev, device_can_wakeup(&dev->dev));
2704 if (target_state == PCI_POWER_ERROR)
2705 return -EIO;
2706
2707 dev->runtime_d3cold = target_state == PCI_D3cold;
2708
2709 /*
2710 * There are systems (for example, Intel mobile chips since Coffee
2711 * Lake) where the power drawn while suspended can be significantly
2712 * reduced by disabling PTM on PCIe root ports as this allows the
2713 * port to enter a lower-power PM state and the SoC to reach a
2714 * lower-power idle state as a whole.
2715 */
2716 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT)
2717 pci_disable_ptm(dev);
2718
2719 __pci_enable_wake(dev, target_state, pci_dev_run_wake(dev));
2720
2721 error = pci_set_power_state(dev, target_state);
2722
2723 if (error) {
2724 pci_enable_wake(dev, target_state, false);
2725 pci_restore_ptm_state(dev);
2726 dev->runtime_d3cold = false;
2727 }
2728
2729 return error;
2730 }
2731
2732 /**
2733 * pci_dev_run_wake - Check if device can generate run-time wake-up events.
2734 * @dev: Device to check.
2735 *
2736 * Return true if the device itself is capable of generating wake-up events
2737 * (through the platform or using the native PCIe PME) or if the device supports
2738 * PME and one of its upstream bridges can generate wake-up events.
2739 */
pci_dev_run_wake(struct pci_dev * dev)2740 bool pci_dev_run_wake(struct pci_dev *dev)
2741 {
2742 struct pci_bus *bus = dev->bus;
2743
2744 if (!dev->pme_support)
2745 return false;
2746
2747 /* PME-capable in principle, but not from the target power state */
2748 if (!pci_pme_capable(dev, pci_target_state(dev, true)))
2749 return false;
2750
2751 if (device_can_wakeup(&dev->dev))
2752 return true;
2753
2754 while (bus->parent) {
2755 struct pci_dev *bridge = bus->self;
2756
2757 if (device_can_wakeup(&bridge->dev))
2758 return true;
2759
2760 bus = bus->parent;
2761 }
2762
2763 /* We have reached the root bus. */
2764 if (bus->bridge)
2765 return device_can_wakeup(bus->bridge);
2766
2767 return false;
2768 }
2769 EXPORT_SYMBOL_GPL(pci_dev_run_wake);
2770
2771 /**
2772 * pci_dev_need_resume - Check if it is necessary to resume the device.
2773 * @pci_dev: Device to check.
2774 *
2775 * Return 'true' if the device is not runtime-suspended or it has to be
2776 * reconfigured due to wakeup settings difference between system and runtime
2777 * suspend, or the current power state of it is not suitable for the upcoming
2778 * (system-wide) transition.
2779 */
pci_dev_need_resume(struct pci_dev * pci_dev)2780 bool pci_dev_need_resume(struct pci_dev *pci_dev)
2781 {
2782 struct device *dev = &pci_dev->dev;
2783 pci_power_t target_state;
2784
2785 if (!pm_runtime_suspended(dev) || platform_pci_need_resume(pci_dev))
2786 return true;
2787
2788 target_state = pci_target_state(pci_dev, device_may_wakeup(dev));
2789
2790 /*
2791 * If the earlier platform check has not triggered, D3cold is just power
2792 * removal on top of D3hot, so no need to resume the device in that
2793 * case.
2794 */
2795 return target_state != pci_dev->current_state &&
2796 target_state != PCI_D3cold &&
2797 pci_dev->current_state != PCI_D3hot;
2798 }
2799
2800 /**
2801 * pci_dev_adjust_pme - Adjust PME setting for a suspended device.
2802 * @pci_dev: Device to check.
2803 *
2804 * If the device is suspended and it is not configured for system wakeup,
2805 * disable PME for it to prevent it from waking up the system unnecessarily.
2806 *
2807 * Note that if the device's power state is D3cold and the platform check in
2808 * pci_dev_need_resume() has not triggered, the device's configuration need not
2809 * be changed.
2810 */
pci_dev_adjust_pme(struct pci_dev * pci_dev)2811 void pci_dev_adjust_pme(struct pci_dev *pci_dev)
2812 {
2813 struct device *dev = &pci_dev->dev;
2814
2815 spin_lock_irq(&dev->power.lock);
2816
2817 if (pm_runtime_suspended(dev) && !device_may_wakeup(dev) &&
2818 pci_dev->current_state < PCI_D3cold)
2819 __pci_pme_active(pci_dev, false);
2820
2821 spin_unlock_irq(&dev->power.lock);
2822 }
2823
2824 /**
2825 * pci_dev_complete_resume - Finalize resume from system sleep for a device.
2826 * @pci_dev: Device to handle.
2827 *
2828 * If the device is runtime suspended and wakeup-capable, enable PME for it as
2829 * it might have been disabled during the prepare phase of system suspend if
2830 * the device was not configured for system wakeup.
2831 */
pci_dev_complete_resume(struct pci_dev * pci_dev)2832 void pci_dev_complete_resume(struct pci_dev *pci_dev)
2833 {
2834 struct device *dev = &pci_dev->dev;
2835
2836 if (!pci_dev_run_wake(pci_dev))
2837 return;
2838
2839 spin_lock_irq(&dev->power.lock);
2840
2841 if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold)
2842 __pci_pme_active(pci_dev, true);
2843
2844 spin_unlock_irq(&dev->power.lock);
2845 }
2846
pci_config_pm_runtime_get(struct pci_dev * pdev)2847 void pci_config_pm_runtime_get(struct pci_dev *pdev)
2848 {
2849 struct device *dev = &pdev->dev;
2850 struct device *parent = dev->parent;
2851
2852 if (parent)
2853 pm_runtime_get_sync(parent);
2854 pm_runtime_get_noresume(dev);
2855 /*
2856 * pdev->current_state is set to PCI_D3cold during suspending,
2857 * so wait until suspending completes
2858 */
2859 pm_runtime_barrier(dev);
2860 /*
2861 * Only need to resume devices in D3cold, because config
2862 * registers are still accessible for devices suspended but
2863 * not in D3cold.
2864 */
2865 if (pdev->current_state == PCI_D3cold)
2866 pm_runtime_resume(dev);
2867 }
2868
pci_config_pm_runtime_put(struct pci_dev * pdev)2869 void pci_config_pm_runtime_put(struct pci_dev *pdev)
2870 {
2871 struct device *dev = &pdev->dev;
2872 struct device *parent = dev->parent;
2873
2874 pm_runtime_put(dev);
2875 if (parent)
2876 pm_runtime_put_sync(parent);
2877 }
2878
2879 static const struct dmi_system_id bridge_d3_blacklist[] = {
2880 #ifdef CONFIG_X86
2881 {
2882 /*
2883 * Gigabyte X299 root port is not marked as hotplug capable
2884 * which allows Linux to power manage it. However, this
2885 * confuses the BIOS SMI handler so don't power manage root
2886 * ports on that system.
2887 */
2888 .ident = "X299 DESIGNARE EX-CF",
2889 .matches = {
2890 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
2891 DMI_MATCH(DMI_BOARD_NAME, "X299 DESIGNARE EX-CF"),
2892 },
2893 },
2894 {
2895 /*
2896 * Downstream device is not accessible after putting a root port
2897 * into D3cold and back into D0 on Elo Continental Z2 board
2898 */
2899 .ident = "Elo Continental Z2",
2900 .matches = {
2901 DMI_MATCH(DMI_BOARD_VENDOR, "Elo Touch Solutions"),
2902 DMI_MATCH(DMI_BOARD_NAME, "Geminilake"),
2903 DMI_MATCH(DMI_BOARD_VERSION, "Continental Z2"),
2904 },
2905 },
2906 #endif
2907 { }
2908 };
2909
2910 /**
2911 * pci_bridge_d3_possible - Is it possible to put the bridge into D3
2912 * @bridge: Bridge to check
2913 *
2914 * This function checks if it is possible to move the bridge to D3.
2915 * Currently we only allow D3 for recent enough PCIe ports and Thunderbolt.
2916 */
pci_bridge_d3_possible(struct pci_dev * bridge)2917 bool pci_bridge_d3_possible(struct pci_dev *bridge)
2918 {
2919 if (!pci_is_pcie(bridge))
2920 return false;
2921
2922 switch (pci_pcie_type(bridge)) {
2923 case PCI_EXP_TYPE_ROOT_PORT:
2924 case PCI_EXP_TYPE_UPSTREAM:
2925 case PCI_EXP_TYPE_DOWNSTREAM:
2926 if (pci_bridge_d3_disable)
2927 return false;
2928
2929 /*
2930 * Hotplug ports handled by firmware in System Management Mode
2931 * may not be put into D3 by the OS (Thunderbolt on non-Macs).
2932 */
2933 if (bridge->is_hotplug_bridge && !pciehp_is_native(bridge))
2934 return false;
2935
2936 if (pci_bridge_d3_force)
2937 return true;
2938
2939 /* Even the oldest 2010 Thunderbolt controller supports D3. */
2940 if (bridge->is_thunderbolt)
2941 return true;
2942
2943 /* Platform might know better if the bridge supports D3 */
2944 if (platform_pci_bridge_d3(bridge))
2945 return true;
2946
2947 /*
2948 * Hotplug ports handled natively by the OS were not validated
2949 * by vendors for runtime D3 at least until 2018 because there
2950 * was no OS support.
2951 */
2952 if (bridge->is_hotplug_bridge)
2953 return false;
2954
2955 if (dmi_check_system(bridge_d3_blacklist))
2956 return false;
2957
2958 /*
2959 * It should be safe to put PCIe ports from 2015 or newer
2960 * to D3.
2961 */
2962 if (dmi_get_bios_year() >= 2015)
2963 return true;
2964 break;
2965 }
2966
2967 return false;
2968 }
2969
pci_dev_check_d3cold(struct pci_dev * dev,void * data)2970 static int pci_dev_check_d3cold(struct pci_dev *dev, void *data)
2971 {
2972 bool *d3cold_ok = data;
2973
2974 if (/* The device needs to be allowed to go D3cold ... */
2975 dev->no_d3cold || !dev->d3cold_allowed ||
2976
2977 /* ... and if it is wakeup capable to do so from D3cold. */
2978 (device_may_wakeup(&dev->dev) &&
2979 !pci_pme_capable(dev, PCI_D3cold)) ||
2980
2981 /* If it is a bridge it must be allowed to go to D3. */
2982 !pci_power_manageable(dev))
2983
2984 *d3cold_ok = false;
2985
2986 return !*d3cold_ok;
2987 }
2988
2989 /*
2990 * pci_bridge_d3_update - Update bridge D3 capabilities
2991 * @dev: PCI device which is changed
2992 *
2993 * Update upstream bridge PM capabilities accordingly depending on if the
2994 * device PM configuration was changed or the device is being removed. The
2995 * change is also propagated upstream.
2996 */
pci_bridge_d3_update(struct pci_dev * dev)2997 void pci_bridge_d3_update(struct pci_dev *dev)
2998 {
2999 bool remove = !device_is_registered(&dev->dev);
3000 struct pci_dev *bridge;
3001 bool d3cold_ok = true;
3002
3003 bridge = pci_upstream_bridge(dev);
3004 if (!bridge || !pci_bridge_d3_possible(bridge))
3005 return;
3006
3007 /*
3008 * If D3 is currently allowed for the bridge, removing one of its
3009 * children won't change that.
3010 */
3011 if (remove && bridge->bridge_d3)
3012 return;
3013
3014 /*
3015 * If D3 is currently allowed for the bridge and a child is added or
3016 * changed, disallowance of D3 can only be caused by that child, so
3017 * we only need to check that single device, not any of its siblings.
3018 *
3019 * If D3 is currently not allowed for the bridge, checking the device
3020 * first may allow us to skip checking its siblings.
3021 */
3022 if (!remove)
3023 pci_dev_check_d3cold(dev, &d3cold_ok);
3024
3025 /*
3026 * If D3 is currently not allowed for the bridge, this may be caused
3027 * either by the device being changed/removed or any of its siblings,
3028 * so we need to go through all children to find out if one of them
3029 * continues to block D3.
3030 */
3031 if (d3cold_ok && !bridge->bridge_d3)
3032 pci_walk_bus(bridge->subordinate, pci_dev_check_d3cold,
3033 &d3cold_ok);
3034
3035 if (bridge->bridge_d3 != d3cold_ok) {
3036 bridge->bridge_d3 = d3cold_ok;
3037 /* Propagate change to upstream bridges */
3038 pci_bridge_d3_update(bridge);
3039 }
3040 }
3041
3042 /**
3043 * pci_d3cold_enable - Enable D3cold for device
3044 * @dev: PCI device to handle
3045 *
3046 * This function can be used in drivers to enable D3cold from the device
3047 * they handle. It also updates upstream PCI bridge PM capabilities
3048 * accordingly.
3049 */
pci_d3cold_enable(struct pci_dev * dev)3050 void pci_d3cold_enable(struct pci_dev *dev)
3051 {
3052 if (dev->no_d3cold) {
3053 dev->no_d3cold = false;
3054 pci_bridge_d3_update(dev);
3055 }
3056 }
3057 EXPORT_SYMBOL_GPL(pci_d3cold_enable);
3058
3059 /**
3060 * pci_d3cold_disable - Disable D3cold for device
3061 * @dev: PCI device to handle
3062 *
3063 * This function can be used in drivers to disable D3cold from the device
3064 * they handle. It also updates upstream PCI bridge PM capabilities
3065 * accordingly.
3066 */
pci_d3cold_disable(struct pci_dev * dev)3067 void pci_d3cold_disable(struct pci_dev *dev)
3068 {
3069 if (!dev->no_d3cold) {
3070 dev->no_d3cold = true;
3071 pci_bridge_d3_update(dev);
3072 }
3073 }
3074 EXPORT_SYMBOL_GPL(pci_d3cold_disable);
3075
3076 /**
3077 * pci_pm_init - Initialize PM functions of given PCI device
3078 * @dev: PCI device to handle.
3079 */
pci_pm_init(struct pci_dev * dev)3080 void pci_pm_init(struct pci_dev *dev)
3081 {
3082 int pm;
3083 u16 status;
3084 u16 pmc;
3085
3086 pm_runtime_forbid(&dev->dev);
3087 pm_runtime_set_active(&dev->dev);
3088 pm_runtime_enable(&dev->dev);
3089 device_enable_async_suspend(&dev->dev);
3090 dev->wakeup_prepared = false;
3091
3092 dev->pm_cap = 0;
3093 dev->pme_support = 0;
3094
3095 /* find PCI PM capability in list */
3096 pm = pci_find_capability(dev, PCI_CAP_ID_PM);
3097 if (!pm)
3098 return;
3099 /* Check device's ability to generate PME# */
3100 pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
3101
3102 if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
3103 pci_err(dev, "unsupported PM cap regs version (%u)\n",
3104 pmc & PCI_PM_CAP_VER_MASK);
3105 return;
3106 }
3107
3108 dev->pm_cap = pm;
3109 dev->d3hot_delay = PCI_PM_D3HOT_WAIT;
3110 dev->d3cold_delay = PCI_PM_D3COLD_WAIT;
3111 dev->bridge_d3 = pci_bridge_d3_possible(dev);
3112 dev->d3cold_allowed = true;
3113
3114 dev->d1_support = false;
3115 dev->d2_support = false;
3116 if (!pci_no_d1d2(dev)) {
3117 if (pmc & PCI_PM_CAP_D1)
3118 dev->d1_support = true;
3119 if (pmc & PCI_PM_CAP_D2)
3120 dev->d2_support = true;
3121
3122 if (dev->d1_support || dev->d2_support)
3123 pci_info(dev, "supports%s%s\n",
3124 dev->d1_support ? " D1" : "",
3125 dev->d2_support ? " D2" : "");
3126 }
3127
3128 pmc &= PCI_PM_CAP_PME_MASK;
3129 if (pmc) {
3130 pci_info(dev, "PME# supported from%s%s%s%s%s\n",
3131 (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
3132 (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
3133 (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
3134 (pmc & PCI_PM_CAP_PME_D3hot) ? " D3hot" : "",
3135 (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
3136 dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
3137 dev->pme_poll = true;
3138 /*
3139 * Make device's PM flags reflect the wake-up capability, but
3140 * let the user space enable it to wake up the system as needed.
3141 */
3142 device_set_wakeup_capable(&dev->dev, true);
3143 /* Disable the PME# generation functionality */
3144 pci_pme_active(dev, false);
3145 }
3146
3147 pci_read_config_word(dev, PCI_STATUS, &status);
3148 if (status & PCI_STATUS_IMM_READY)
3149 dev->imm_ready = 1;
3150 }
3151
pci_ea_flags(struct pci_dev * dev,u8 prop)3152 static unsigned long pci_ea_flags(struct pci_dev *dev, u8 prop)
3153 {
3154 unsigned long flags = IORESOURCE_PCI_FIXED | IORESOURCE_PCI_EA_BEI;
3155
3156 switch (prop) {
3157 case PCI_EA_P_MEM:
3158 case PCI_EA_P_VF_MEM:
3159 flags |= IORESOURCE_MEM;
3160 break;
3161 case PCI_EA_P_MEM_PREFETCH:
3162 case PCI_EA_P_VF_MEM_PREFETCH:
3163 flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
3164 break;
3165 case PCI_EA_P_IO:
3166 flags |= IORESOURCE_IO;
3167 break;
3168 default:
3169 return 0;
3170 }
3171
3172 return flags;
3173 }
3174
pci_ea_get_resource(struct pci_dev * dev,u8 bei,u8 prop)3175 static struct resource *pci_ea_get_resource(struct pci_dev *dev, u8 bei,
3176 u8 prop)
3177 {
3178 if (bei <= PCI_EA_BEI_BAR5 && prop <= PCI_EA_P_IO)
3179 return &dev->resource[bei];
3180 #ifdef CONFIG_PCI_IOV
3181 else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5 &&
3182 (prop == PCI_EA_P_VF_MEM || prop == PCI_EA_P_VF_MEM_PREFETCH))
3183 return &dev->resource[PCI_IOV_RESOURCES +
3184 bei - PCI_EA_BEI_VF_BAR0];
3185 #endif
3186 else if (bei == PCI_EA_BEI_ROM)
3187 return &dev->resource[PCI_ROM_RESOURCE];
3188 else
3189 return NULL;
3190 }
3191
3192 /* Read an Enhanced Allocation (EA) entry */
pci_ea_read(struct pci_dev * dev,int offset)3193 static int pci_ea_read(struct pci_dev *dev, int offset)
3194 {
3195 struct resource *res;
3196 int ent_size, ent_offset = offset;
3197 resource_size_t start, end;
3198 unsigned long flags;
3199 u32 dw0, bei, base, max_offset;
3200 u8 prop;
3201 bool support_64 = (sizeof(resource_size_t) >= 8);
3202
3203 pci_read_config_dword(dev, ent_offset, &dw0);
3204 ent_offset += 4;
3205
3206 /* Entry size field indicates DWORDs after 1st */
3207 ent_size = ((dw0 & PCI_EA_ES) + 1) << 2;
3208
3209 if (!(dw0 & PCI_EA_ENABLE)) /* Entry not enabled */
3210 goto out;
3211
3212 bei = (dw0 & PCI_EA_BEI) >> 4;
3213 prop = (dw0 & PCI_EA_PP) >> 8;
3214
3215 /*
3216 * If the Property is in the reserved range, try the Secondary
3217 * Property instead.
3218 */
3219 if (prop > PCI_EA_P_BRIDGE_IO && prop < PCI_EA_P_MEM_RESERVED)
3220 prop = (dw0 & PCI_EA_SP) >> 16;
3221 if (prop > PCI_EA_P_BRIDGE_IO)
3222 goto out;
3223
3224 res = pci_ea_get_resource(dev, bei, prop);
3225 if (!res) {
3226 pci_err(dev, "Unsupported EA entry BEI: %u\n", bei);
3227 goto out;
3228 }
3229
3230 flags = pci_ea_flags(dev, prop);
3231 if (!flags) {
3232 pci_err(dev, "Unsupported EA properties: %#x\n", prop);
3233 goto out;
3234 }
3235
3236 /* Read Base */
3237 pci_read_config_dword(dev, ent_offset, &base);
3238 start = (base & PCI_EA_FIELD_MASK);
3239 ent_offset += 4;
3240
3241 /* Read MaxOffset */
3242 pci_read_config_dword(dev, ent_offset, &max_offset);
3243 ent_offset += 4;
3244
3245 /* Read Base MSBs (if 64-bit entry) */
3246 if (base & PCI_EA_IS_64) {
3247 u32 base_upper;
3248
3249 pci_read_config_dword(dev, ent_offset, &base_upper);
3250 ent_offset += 4;
3251
3252 flags |= IORESOURCE_MEM_64;
3253
3254 /* entry starts above 32-bit boundary, can't use */
3255 if (!support_64 && base_upper)
3256 goto out;
3257
3258 if (support_64)
3259 start |= ((u64)base_upper << 32);
3260 }
3261
3262 end = start + (max_offset | 0x03);
3263
3264 /* Read MaxOffset MSBs (if 64-bit entry) */
3265 if (max_offset & PCI_EA_IS_64) {
3266 u32 max_offset_upper;
3267
3268 pci_read_config_dword(dev, ent_offset, &max_offset_upper);
3269 ent_offset += 4;
3270
3271 flags |= IORESOURCE_MEM_64;
3272
3273 /* entry too big, can't use */
3274 if (!support_64 && max_offset_upper)
3275 goto out;
3276
3277 if (support_64)
3278 end += ((u64)max_offset_upper << 32);
3279 }
3280
3281 if (end < start) {
3282 pci_err(dev, "EA Entry crosses address boundary\n");
3283 goto out;
3284 }
3285
3286 if (ent_size != ent_offset - offset) {
3287 pci_err(dev, "EA Entry Size (%d) does not match length read (%d)\n",
3288 ent_size, ent_offset - offset);
3289 goto out;
3290 }
3291
3292 res->name = pci_name(dev);
3293 res->start = start;
3294 res->end = end;
3295 res->flags = flags;
3296
3297 if (bei <= PCI_EA_BEI_BAR5)
3298 pci_info(dev, "BAR %d: %pR (from Enhanced Allocation, properties %#02x)\n",
3299 bei, res, prop);
3300 else if (bei == PCI_EA_BEI_ROM)
3301 pci_info(dev, "ROM: %pR (from Enhanced Allocation, properties %#02x)\n",
3302 res, prop);
3303 else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5)
3304 pci_info(dev, "VF BAR %d: %pR (from Enhanced Allocation, properties %#02x)\n",
3305 bei - PCI_EA_BEI_VF_BAR0, res, prop);
3306 else
3307 pci_info(dev, "BEI %d res: %pR (from Enhanced Allocation, properties %#02x)\n",
3308 bei, res, prop);
3309
3310 out:
3311 return offset + ent_size;
3312 }
3313
3314 /* Enhanced Allocation Initialization */
pci_ea_init(struct pci_dev * dev)3315 void pci_ea_init(struct pci_dev *dev)
3316 {
3317 int ea;
3318 u8 num_ent;
3319 int offset;
3320 int i;
3321
3322 /* find PCI EA capability in list */
3323 ea = pci_find_capability(dev, PCI_CAP_ID_EA);
3324 if (!ea)
3325 return;
3326
3327 /* determine the number of entries */
3328 pci_bus_read_config_byte(dev->bus, dev->devfn, ea + PCI_EA_NUM_ENT,
3329 &num_ent);
3330 num_ent &= PCI_EA_NUM_ENT_MASK;
3331
3332 offset = ea + PCI_EA_FIRST_ENT;
3333
3334 /* Skip DWORD 2 for type 1 functions */
3335 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
3336 offset += 4;
3337
3338 /* parse each EA entry */
3339 for (i = 0; i < num_ent; ++i)
3340 offset = pci_ea_read(dev, offset);
3341 }
3342
pci_add_saved_cap(struct pci_dev * pci_dev,struct pci_cap_saved_state * new_cap)3343 static void pci_add_saved_cap(struct pci_dev *pci_dev,
3344 struct pci_cap_saved_state *new_cap)
3345 {
3346 hlist_add_head(&new_cap->next, &pci_dev->saved_cap_space);
3347 }
3348
3349 /**
3350 * _pci_add_cap_save_buffer - allocate buffer for saving given
3351 * capability registers
3352 * @dev: the PCI device
3353 * @cap: the capability to allocate the buffer for
3354 * @extended: Standard or Extended capability ID
3355 * @size: requested size of the buffer
3356 */
_pci_add_cap_save_buffer(struct pci_dev * dev,u16 cap,bool extended,unsigned int size)3357 static int _pci_add_cap_save_buffer(struct pci_dev *dev, u16 cap,
3358 bool extended, unsigned int size)
3359 {
3360 int pos;
3361 struct pci_cap_saved_state *save_state;
3362
3363 if (extended)
3364 pos = pci_find_ext_capability(dev, cap);
3365 else
3366 pos = pci_find_capability(dev, cap);
3367
3368 if (!pos)
3369 return 0;
3370
3371 save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
3372 if (!save_state)
3373 return -ENOMEM;
3374
3375 save_state->cap.cap_nr = cap;
3376 save_state->cap.cap_extended = extended;
3377 save_state->cap.size = size;
3378 pci_add_saved_cap(dev, save_state);
3379
3380 return 0;
3381 }
3382
pci_add_cap_save_buffer(struct pci_dev * dev,char cap,unsigned int size)3383 int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size)
3384 {
3385 return _pci_add_cap_save_buffer(dev, cap, false, size);
3386 }
3387
pci_add_ext_cap_save_buffer(struct pci_dev * dev,u16 cap,unsigned int size)3388 int pci_add_ext_cap_save_buffer(struct pci_dev *dev, u16 cap, unsigned int size)
3389 {
3390 return _pci_add_cap_save_buffer(dev, cap, true, size);
3391 }
3392
3393 /**
3394 * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
3395 * @dev: the PCI device
3396 */
pci_allocate_cap_save_buffers(struct pci_dev * dev)3397 void pci_allocate_cap_save_buffers(struct pci_dev *dev)
3398 {
3399 int error;
3400
3401 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP,
3402 PCI_EXP_SAVE_REGS * sizeof(u16));
3403 if (error)
3404 pci_err(dev, "unable to preallocate PCI Express save buffer\n");
3405
3406 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
3407 if (error)
3408 pci_err(dev, "unable to preallocate PCI-X save buffer\n");
3409
3410 error = pci_add_ext_cap_save_buffer(dev, PCI_EXT_CAP_ID_LTR,
3411 2 * sizeof(u16));
3412 if (error)
3413 pci_err(dev, "unable to allocate suspend buffer for LTR\n");
3414
3415 pci_allocate_vc_save_buffers(dev);
3416 }
3417
pci_free_cap_save_buffers(struct pci_dev * dev)3418 void pci_free_cap_save_buffers(struct pci_dev *dev)
3419 {
3420 struct pci_cap_saved_state *tmp;
3421 struct hlist_node *n;
3422
3423 hlist_for_each_entry_safe(tmp, n, &dev->saved_cap_space, next)
3424 kfree(tmp);
3425 }
3426
3427 /**
3428 * pci_configure_ari - enable or disable ARI forwarding
3429 * @dev: the PCI device
3430 *
3431 * If @dev and its upstream bridge both support ARI, enable ARI in the
3432 * bridge. Otherwise, disable ARI in the bridge.
3433 */
pci_configure_ari(struct pci_dev * dev)3434 void pci_configure_ari(struct pci_dev *dev)
3435 {
3436 u32 cap;
3437 struct pci_dev *bridge;
3438
3439 if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
3440 return;
3441
3442 bridge = dev->bus->self;
3443 if (!bridge)
3444 return;
3445
3446 pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
3447 if (!(cap & PCI_EXP_DEVCAP2_ARI))
3448 return;
3449
3450 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
3451 pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
3452 PCI_EXP_DEVCTL2_ARI);
3453 bridge->ari_enabled = 1;
3454 } else {
3455 pcie_capability_clear_word(bridge, PCI_EXP_DEVCTL2,
3456 PCI_EXP_DEVCTL2_ARI);
3457 bridge->ari_enabled = 0;
3458 }
3459 }
3460
pci_acs_flags_enabled(struct pci_dev * pdev,u16 acs_flags)3461 static bool pci_acs_flags_enabled(struct pci_dev *pdev, u16 acs_flags)
3462 {
3463 int pos;
3464 u16 cap, ctrl;
3465
3466 pos = pdev->acs_cap;
3467 if (!pos)
3468 return false;
3469
3470 /*
3471 * Except for egress control, capabilities are either required
3472 * or only required if controllable. Features missing from the
3473 * capability field can therefore be assumed as hard-wired enabled.
3474 */
3475 pci_read_config_word(pdev, pos + PCI_ACS_CAP, &cap);
3476 acs_flags &= (cap | PCI_ACS_EC);
3477
3478 pci_read_config_word(pdev, pos + PCI_ACS_CTRL, &ctrl);
3479 return (ctrl & acs_flags) == acs_flags;
3480 }
3481
3482 /**
3483 * pci_acs_enabled - test ACS against required flags for a given device
3484 * @pdev: device to test
3485 * @acs_flags: required PCI ACS flags
3486 *
3487 * Return true if the device supports the provided flags. Automatically
3488 * filters out flags that are not implemented on multifunction devices.
3489 *
3490 * Note that this interface checks the effective ACS capabilities of the
3491 * device rather than the actual capabilities. For instance, most single
3492 * function endpoints are not required to support ACS because they have no
3493 * opportunity for peer-to-peer access. We therefore return 'true'
3494 * regardless of whether the device exposes an ACS capability. This makes
3495 * it much easier for callers of this function to ignore the actual type
3496 * or topology of the device when testing ACS support.
3497 */
pci_acs_enabled(struct pci_dev * pdev,u16 acs_flags)3498 bool pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags)
3499 {
3500 int ret;
3501
3502 ret = pci_dev_specific_acs_enabled(pdev, acs_flags);
3503 if (ret >= 0)
3504 return ret > 0;
3505
3506 /*
3507 * Conventional PCI and PCI-X devices never support ACS, either
3508 * effectively or actually. The shared bus topology implies that
3509 * any device on the bus can receive or snoop DMA.
3510 */
3511 if (!pci_is_pcie(pdev))
3512 return false;
3513
3514 switch (pci_pcie_type(pdev)) {
3515 /*
3516 * PCI/X-to-PCIe bridges are not specifically mentioned by the spec,
3517 * but since their primary interface is PCI/X, we conservatively
3518 * handle them as we would a non-PCIe device.
3519 */
3520 case PCI_EXP_TYPE_PCIE_BRIDGE:
3521 /*
3522 * PCIe 3.0, 6.12.1 excludes ACS on these devices. "ACS is never
3523 * applicable... must never implement an ACS Extended Capability...".
3524 * This seems arbitrary, but we take a conservative interpretation
3525 * of this statement.
3526 */
3527 case PCI_EXP_TYPE_PCI_BRIDGE:
3528 case PCI_EXP_TYPE_RC_EC:
3529 return false;
3530 /*
3531 * PCIe 3.0, 6.12.1.1 specifies that downstream and root ports should
3532 * implement ACS in order to indicate their peer-to-peer capabilities,
3533 * regardless of whether they are single- or multi-function devices.
3534 */
3535 case PCI_EXP_TYPE_DOWNSTREAM:
3536 case PCI_EXP_TYPE_ROOT_PORT:
3537 return pci_acs_flags_enabled(pdev, acs_flags);
3538 /*
3539 * PCIe 3.0, 6.12.1.2 specifies ACS capabilities that should be
3540 * implemented by the remaining PCIe types to indicate peer-to-peer
3541 * capabilities, but only when they are part of a multifunction
3542 * device. The footnote for section 6.12 indicates the specific
3543 * PCIe types included here.
3544 */
3545 case PCI_EXP_TYPE_ENDPOINT:
3546 case PCI_EXP_TYPE_UPSTREAM:
3547 case PCI_EXP_TYPE_LEG_END:
3548 case PCI_EXP_TYPE_RC_END:
3549 if (!pdev->multifunction)
3550 break;
3551
3552 return pci_acs_flags_enabled(pdev, acs_flags);
3553 }
3554
3555 /*
3556 * PCIe 3.0, 6.12.1.3 specifies no ACS capabilities are applicable
3557 * to single function devices with the exception of downstream ports.
3558 */
3559 return true;
3560 }
3561
3562 /**
3563 * pci_acs_path_enabled - test ACS flags from start to end in a hierarchy
3564 * @start: starting downstream device
3565 * @end: ending upstream device or NULL to search to the root bus
3566 * @acs_flags: required flags
3567 *
3568 * Walk up a device tree from start to end testing PCI ACS support. If
3569 * any step along the way does not support the required flags, return false.
3570 */
pci_acs_path_enabled(struct pci_dev * start,struct pci_dev * end,u16 acs_flags)3571 bool pci_acs_path_enabled(struct pci_dev *start,
3572 struct pci_dev *end, u16 acs_flags)
3573 {
3574 struct pci_dev *pdev, *parent = start;
3575
3576 do {
3577 pdev = parent;
3578
3579 if (!pci_acs_enabled(pdev, acs_flags))
3580 return false;
3581
3582 if (pci_is_root_bus(pdev->bus))
3583 return (end == NULL);
3584
3585 parent = pdev->bus->self;
3586 } while (pdev != end);
3587
3588 return true;
3589 }
3590
3591 /**
3592 * pci_acs_init - Initialize ACS if hardware supports it
3593 * @dev: the PCI device
3594 */
pci_acs_init(struct pci_dev * dev)3595 void pci_acs_init(struct pci_dev *dev)
3596 {
3597 dev->acs_cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
3598
3599 /*
3600 * Attempt to enable ACS regardless of capability because some Root
3601 * Ports (e.g. those quirked with *_intel_pch_acs_*) do not have
3602 * the standard ACS capability but still support ACS via those
3603 * quirks.
3604 */
3605 pci_enable_acs(dev);
3606 }
3607
3608 /**
3609 * pci_rebar_find_pos - find position of resize ctrl reg for BAR
3610 * @pdev: PCI device
3611 * @bar: BAR to find
3612 *
3613 * Helper to find the position of the ctrl register for a BAR.
3614 * Returns -ENOTSUPP if resizable BARs are not supported at all.
3615 * Returns -ENOENT if no ctrl register for the BAR could be found.
3616 */
pci_rebar_find_pos(struct pci_dev * pdev,int bar)3617 static int pci_rebar_find_pos(struct pci_dev *pdev, int bar)
3618 {
3619 unsigned int pos, nbars, i;
3620 u32 ctrl;
3621
3622 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_REBAR);
3623 if (!pos)
3624 return -ENOTSUPP;
3625
3626 pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
3627 nbars = (ctrl & PCI_REBAR_CTRL_NBAR_MASK) >>
3628 PCI_REBAR_CTRL_NBAR_SHIFT;
3629
3630 for (i = 0; i < nbars; i++, pos += 8) {
3631 int bar_idx;
3632
3633 pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
3634 bar_idx = ctrl & PCI_REBAR_CTRL_BAR_IDX;
3635 if (bar_idx == bar)
3636 return pos;
3637 }
3638
3639 return -ENOENT;
3640 }
3641
3642 /**
3643 * pci_rebar_get_possible_sizes - get possible sizes for BAR
3644 * @pdev: PCI device
3645 * @bar: BAR to query
3646 *
3647 * Get the possible sizes of a resizable BAR as bitmask defined in the spec
3648 * (bit 0=1MB, bit 19=512GB). Returns 0 if BAR isn't resizable.
3649 */
pci_rebar_get_possible_sizes(struct pci_dev * pdev,int bar)3650 u32 pci_rebar_get_possible_sizes(struct pci_dev *pdev, int bar)
3651 {
3652 int pos;
3653 u32 cap;
3654
3655 pos = pci_rebar_find_pos(pdev, bar);
3656 if (pos < 0)
3657 return 0;
3658
3659 pci_read_config_dword(pdev, pos + PCI_REBAR_CAP, &cap);
3660 cap = FIELD_GET(PCI_REBAR_CAP_SIZES, cap);
3661
3662 /* Sapphire RX 5600 XT Pulse has an invalid cap dword for BAR 0 */
3663 if (pdev->vendor == PCI_VENDOR_ID_ATI && pdev->device == 0x731f &&
3664 bar == 0 && cap == 0x700)
3665 return 0x3f00;
3666
3667 return cap;
3668 }
3669 EXPORT_SYMBOL(pci_rebar_get_possible_sizes);
3670
3671 /**
3672 * pci_rebar_get_current_size - get the current size of a BAR
3673 * @pdev: PCI device
3674 * @bar: BAR to set size to
3675 *
3676 * Read the size of a BAR from the resizable BAR config.
3677 * Returns size if found or negative error code.
3678 */
pci_rebar_get_current_size(struct pci_dev * pdev,int bar)3679 int pci_rebar_get_current_size(struct pci_dev *pdev, int bar)
3680 {
3681 int pos;
3682 u32 ctrl;
3683
3684 pos = pci_rebar_find_pos(pdev, bar);
3685 if (pos < 0)
3686 return pos;
3687
3688 pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
3689 return (ctrl & PCI_REBAR_CTRL_BAR_SIZE) >> PCI_REBAR_CTRL_BAR_SHIFT;
3690 }
3691
3692 /**
3693 * pci_rebar_set_size - set a new size for a BAR
3694 * @pdev: PCI device
3695 * @bar: BAR to set size to
3696 * @size: new size as defined in the spec (0=1MB, 19=512GB)
3697 *
3698 * Set the new size of a BAR as defined in the spec.
3699 * Returns zero if resizing was successful, error code otherwise.
3700 */
pci_rebar_set_size(struct pci_dev * pdev,int bar,int size)3701 int pci_rebar_set_size(struct pci_dev *pdev, int bar, int size)
3702 {
3703 int pos;
3704 u32 ctrl;
3705
3706 pos = pci_rebar_find_pos(pdev, bar);
3707 if (pos < 0)
3708 return pos;
3709
3710 pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
3711 ctrl &= ~PCI_REBAR_CTRL_BAR_SIZE;
3712 ctrl |= size << PCI_REBAR_CTRL_BAR_SHIFT;
3713 pci_write_config_dword(pdev, pos + PCI_REBAR_CTRL, ctrl);
3714 return 0;
3715 }
3716
3717 /**
3718 * pci_enable_atomic_ops_to_root - enable AtomicOp requests to root port
3719 * @dev: the PCI device
3720 * @cap_mask: mask of desired AtomicOp sizes, including one or more of:
3721 * PCI_EXP_DEVCAP2_ATOMIC_COMP32
3722 * PCI_EXP_DEVCAP2_ATOMIC_COMP64
3723 * PCI_EXP_DEVCAP2_ATOMIC_COMP128
3724 *
3725 * Return 0 if all upstream bridges support AtomicOp routing, egress
3726 * blocking is disabled on all upstream ports, and the root port supports
3727 * the requested completion capabilities (32-bit, 64-bit and/or 128-bit
3728 * AtomicOp completion), or negative otherwise.
3729 */
pci_enable_atomic_ops_to_root(struct pci_dev * dev,u32 cap_mask)3730 int pci_enable_atomic_ops_to_root(struct pci_dev *dev, u32 cap_mask)
3731 {
3732 struct pci_bus *bus = dev->bus;
3733 struct pci_dev *bridge;
3734 u32 cap, ctl2;
3735
3736 /*
3737 * Per PCIe r5.0, sec 9.3.5.10, the AtomicOp Requester Enable bit
3738 * in Device Control 2 is reserved in VFs and the PF value applies
3739 * to all associated VFs.
3740 */
3741 if (dev->is_virtfn)
3742 return -EINVAL;
3743
3744 if (!pci_is_pcie(dev))
3745 return -EINVAL;
3746
3747 /*
3748 * Per PCIe r4.0, sec 6.15, endpoints and root ports may be
3749 * AtomicOp requesters. For now, we only support endpoints as
3750 * requesters and root ports as completers. No endpoints as
3751 * completers, and no peer-to-peer.
3752 */
3753
3754 switch (pci_pcie_type(dev)) {
3755 case PCI_EXP_TYPE_ENDPOINT:
3756 case PCI_EXP_TYPE_LEG_END:
3757 case PCI_EXP_TYPE_RC_END:
3758 break;
3759 default:
3760 return -EINVAL;
3761 }
3762
3763 while (bus->parent) {
3764 bridge = bus->self;
3765
3766 pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
3767
3768 switch (pci_pcie_type(bridge)) {
3769 /* Ensure switch ports support AtomicOp routing */
3770 case PCI_EXP_TYPE_UPSTREAM:
3771 case PCI_EXP_TYPE_DOWNSTREAM:
3772 if (!(cap & PCI_EXP_DEVCAP2_ATOMIC_ROUTE))
3773 return -EINVAL;
3774 break;
3775
3776 /* Ensure root port supports all the sizes we care about */
3777 case PCI_EXP_TYPE_ROOT_PORT:
3778 if ((cap & cap_mask) != cap_mask)
3779 return -EINVAL;
3780 break;
3781 }
3782
3783 /* Ensure upstream ports don't block AtomicOps on egress */
3784 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_UPSTREAM) {
3785 pcie_capability_read_dword(bridge, PCI_EXP_DEVCTL2,
3786 &ctl2);
3787 if (ctl2 & PCI_EXP_DEVCTL2_ATOMIC_EGRESS_BLOCK)
3788 return -EINVAL;
3789 }
3790
3791 bus = bus->parent;
3792 }
3793
3794 pcie_capability_set_word(dev, PCI_EXP_DEVCTL2,
3795 PCI_EXP_DEVCTL2_ATOMIC_REQ);
3796 return 0;
3797 }
3798 EXPORT_SYMBOL(pci_enable_atomic_ops_to_root);
3799
3800 /**
3801 * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
3802 * @dev: the PCI device
3803 * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD)
3804 *
3805 * Perform INTx swizzling for a device behind one level of bridge. This is
3806 * required by section 9.1 of the PCI-to-PCI bridge specification for devices
3807 * behind bridges on add-in cards. For devices with ARI enabled, the slot
3808 * number is always 0 (see the Implementation Note in section 2.2.8.1 of
3809 * the PCI Express Base Specification, Revision 2.1)
3810 */
pci_swizzle_interrupt_pin(const struct pci_dev * dev,u8 pin)3811 u8 pci_swizzle_interrupt_pin(const struct pci_dev *dev, u8 pin)
3812 {
3813 int slot;
3814
3815 if (pci_ari_enabled(dev->bus))
3816 slot = 0;
3817 else
3818 slot = PCI_SLOT(dev->devfn);
3819
3820 return (((pin - 1) + slot) % 4) + 1;
3821 }
3822
pci_get_interrupt_pin(struct pci_dev * dev,struct pci_dev ** bridge)3823 int pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
3824 {
3825 u8 pin;
3826
3827 pin = dev->pin;
3828 if (!pin)
3829 return -1;
3830
3831 while (!pci_is_root_bus(dev->bus)) {
3832 pin = pci_swizzle_interrupt_pin(dev, pin);
3833 dev = dev->bus->self;
3834 }
3835 *bridge = dev;
3836 return pin;
3837 }
3838
3839 /**
3840 * pci_common_swizzle - swizzle INTx all the way to root bridge
3841 * @dev: the PCI device
3842 * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
3843 *
3844 * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
3845 * bridges all the way up to a PCI root bus.
3846 */
pci_common_swizzle(struct pci_dev * dev,u8 * pinp)3847 u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
3848 {
3849 u8 pin = *pinp;
3850
3851 while (!pci_is_root_bus(dev->bus)) {
3852 pin = pci_swizzle_interrupt_pin(dev, pin);
3853 dev = dev->bus->self;
3854 }
3855 *pinp = pin;
3856 return PCI_SLOT(dev->devfn);
3857 }
3858 EXPORT_SYMBOL_GPL(pci_common_swizzle);
3859
3860 /**
3861 * pci_release_region - Release a PCI bar
3862 * @pdev: PCI device whose resources were previously reserved by
3863 * pci_request_region()
3864 * @bar: BAR to release
3865 *
3866 * Releases the PCI I/O and memory resources previously reserved by a
3867 * successful call to pci_request_region(). Call this function only
3868 * after all use of the PCI regions has ceased.
3869 */
pci_release_region(struct pci_dev * pdev,int bar)3870 void pci_release_region(struct pci_dev *pdev, int bar)
3871 {
3872 struct pci_devres *dr;
3873
3874 if (pci_resource_len(pdev, bar) == 0)
3875 return;
3876 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
3877 release_region(pci_resource_start(pdev, bar),
3878 pci_resource_len(pdev, bar));
3879 else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
3880 release_mem_region(pci_resource_start(pdev, bar),
3881 pci_resource_len(pdev, bar));
3882
3883 dr = find_pci_dr(pdev);
3884 if (dr)
3885 dr->region_mask &= ~(1 << bar);
3886 }
3887 EXPORT_SYMBOL(pci_release_region);
3888
3889 /**
3890 * __pci_request_region - Reserved PCI I/O and memory resource
3891 * @pdev: PCI device whose resources are to be reserved
3892 * @bar: BAR to be reserved
3893 * @res_name: Name to be associated with resource.
3894 * @exclusive: whether the region access is exclusive or not
3895 *
3896 * Mark the PCI region associated with PCI device @pdev BAR @bar as
3897 * being reserved by owner @res_name. Do not access any
3898 * address inside the PCI regions unless this call returns
3899 * successfully.
3900 *
3901 * If @exclusive is set, then the region is marked so that userspace
3902 * is explicitly not allowed to map the resource via /dev/mem or
3903 * sysfs MMIO access.
3904 *
3905 * Returns 0 on success, or %EBUSY on error. A warning
3906 * message is also printed on failure.
3907 */
__pci_request_region(struct pci_dev * pdev,int bar,const char * res_name,int exclusive)3908 static int __pci_request_region(struct pci_dev *pdev, int bar,
3909 const char *res_name, int exclusive)
3910 {
3911 struct pci_devres *dr;
3912
3913 if (pci_resource_len(pdev, bar) == 0)
3914 return 0;
3915
3916 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
3917 if (!request_region(pci_resource_start(pdev, bar),
3918 pci_resource_len(pdev, bar), res_name))
3919 goto err_out;
3920 } else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
3921 if (!__request_mem_region(pci_resource_start(pdev, bar),
3922 pci_resource_len(pdev, bar), res_name,
3923 exclusive))
3924 goto err_out;
3925 }
3926
3927 dr = find_pci_dr(pdev);
3928 if (dr)
3929 dr->region_mask |= 1 << bar;
3930
3931 return 0;
3932
3933 err_out:
3934 pci_warn(pdev, "BAR %d: can't reserve %pR\n", bar,
3935 &pdev->resource[bar]);
3936 return -EBUSY;
3937 }
3938
3939 /**
3940 * pci_request_region - Reserve PCI I/O and memory resource
3941 * @pdev: PCI device whose resources are to be reserved
3942 * @bar: BAR to be reserved
3943 * @res_name: Name to be associated with resource
3944 *
3945 * Mark the PCI region associated with PCI device @pdev BAR @bar as
3946 * being reserved by owner @res_name. Do not access any
3947 * address inside the PCI regions unless this call returns
3948 * successfully.
3949 *
3950 * Returns 0 on success, or %EBUSY on error. A warning
3951 * message is also printed on failure.
3952 */
pci_request_region(struct pci_dev * pdev,int bar,const char * res_name)3953 int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
3954 {
3955 return __pci_request_region(pdev, bar, res_name, 0);
3956 }
3957 EXPORT_SYMBOL(pci_request_region);
3958
3959 /**
3960 * pci_release_selected_regions - Release selected PCI I/O and memory resources
3961 * @pdev: PCI device whose resources were previously reserved
3962 * @bars: Bitmask of BARs to be released
3963 *
3964 * Release selected PCI I/O and memory resources previously reserved.
3965 * Call this function only after all use of the PCI regions has ceased.
3966 */
pci_release_selected_regions(struct pci_dev * pdev,int bars)3967 void pci_release_selected_regions(struct pci_dev *pdev, int bars)
3968 {
3969 int i;
3970
3971 for (i = 0; i < PCI_STD_NUM_BARS; i++)
3972 if (bars & (1 << i))
3973 pci_release_region(pdev, i);
3974 }
3975 EXPORT_SYMBOL(pci_release_selected_regions);
3976
__pci_request_selected_regions(struct pci_dev * pdev,int bars,const char * res_name,int excl)3977 static int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
3978 const char *res_name, int excl)
3979 {
3980 int i;
3981
3982 for (i = 0; i < PCI_STD_NUM_BARS; i++)
3983 if (bars & (1 << i))
3984 if (__pci_request_region(pdev, i, res_name, excl))
3985 goto err_out;
3986 return 0;
3987
3988 err_out:
3989 while (--i >= 0)
3990 if (bars & (1 << i))
3991 pci_release_region(pdev, i);
3992
3993 return -EBUSY;
3994 }
3995
3996
3997 /**
3998 * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
3999 * @pdev: PCI device whose resources are to be reserved
4000 * @bars: Bitmask of BARs to be requested
4001 * @res_name: Name to be associated with resource
4002 */
pci_request_selected_regions(struct pci_dev * pdev,int bars,const char * res_name)4003 int pci_request_selected_regions(struct pci_dev *pdev, int bars,
4004 const char *res_name)
4005 {
4006 return __pci_request_selected_regions(pdev, bars, res_name, 0);
4007 }
4008 EXPORT_SYMBOL(pci_request_selected_regions);
4009
pci_request_selected_regions_exclusive(struct pci_dev * pdev,int bars,const char * res_name)4010 int pci_request_selected_regions_exclusive(struct pci_dev *pdev, int bars,
4011 const char *res_name)
4012 {
4013 return __pci_request_selected_regions(pdev, bars, res_name,
4014 IORESOURCE_EXCLUSIVE);
4015 }
4016 EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
4017
4018 /**
4019 * pci_release_regions - Release reserved PCI I/O and memory resources
4020 * @pdev: PCI device whose resources were previously reserved by
4021 * pci_request_regions()
4022 *
4023 * Releases all PCI I/O and memory resources previously reserved by a
4024 * successful call to pci_request_regions(). Call this function only
4025 * after all use of the PCI regions has ceased.
4026 */
4027
pci_release_regions(struct pci_dev * pdev)4028 void pci_release_regions(struct pci_dev *pdev)
4029 {
4030 pci_release_selected_regions(pdev, (1 << PCI_STD_NUM_BARS) - 1);
4031 }
4032 EXPORT_SYMBOL(pci_release_regions);
4033
4034 /**
4035 * pci_request_regions - Reserve PCI I/O and memory resources
4036 * @pdev: PCI device whose resources are to be reserved
4037 * @res_name: Name to be associated with resource.
4038 *
4039 * Mark all PCI regions associated with PCI device @pdev as
4040 * being reserved by owner @res_name. Do not access any
4041 * address inside the PCI regions unless this call returns
4042 * successfully.
4043 *
4044 * Returns 0 on success, or %EBUSY on error. A warning
4045 * message is also printed on failure.
4046 */
pci_request_regions(struct pci_dev * pdev,const char * res_name)4047 int pci_request_regions(struct pci_dev *pdev, const char *res_name)
4048 {
4049 return pci_request_selected_regions(pdev,
4050 ((1 << PCI_STD_NUM_BARS) - 1), res_name);
4051 }
4052 EXPORT_SYMBOL(pci_request_regions);
4053
4054 /**
4055 * pci_request_regions_exclusive - Reserve PCI I/O and memory resources
4056 * @pdev: PCI device whose resources are to be reserved
4057 * @res_name: Name to be associated with resource.
4058 *
4059 * Mark all PCI regions associated with PCI device @pdev as being reserved
4060 * by owner @res_name. Do not access any address inside the PCI regions
4061 * unless this call returns successfully.
4062 *
4063 * pci_request_regions_exclusive() will mark the region so that /dev/mem
4064 * and the sysfs MMIO access will not be allowed.
4065 *
4066 * Returns 0 on success, or %EBUSY on error. A warning message is also
4067 * printed on failure.
4068 */
pci_request_regions_exclusive(struct pci_dev * pdev,const char * res_name)4069 int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
4070 {
4071 return pci_request_selected_regions_exclusive(pdev,
4072 ((1 << PCI_STD_NUM_BARS) - 1), res_name);
4073 }
4074 EXPORT_SYMBOL(pci_request_regions_exclusive);
4075
4076 /*
4077 * Record the PCI IO range (expressed as CPU physical address + size).
4078 * Return a negative value if an error has occurred, zero otherwise
4079 */
pci_register_io_range(struct fwnode_handle * fwnode,phys_addr_t addr,resource_size_t size)4080 int pci_register_io_range(struct fwnode_handle *fwnode, phys_addr_t addr,
4081 resource_size_t size)
4082 {
4083 int ret = 0;
4084 #ifdef PCI_IOBASE
4085 struct logic_pio_hwaddr *range;
4086
4087 if (!size || addr + size < addr)
4088 return -EINVAL;
4089
4090 range = kzalloc(sizeof(*range), GFP_ATOMIC);
4091 if (!range)
4092 return -ENOMEM;
4093
4094 range->fwnode = fwnode;
4095 range->size = size;
4096 range->hw_start = addr;
4097 range->flags = LOGIC_PIO_CPU_MMIO;
4098
4099 ret = logic_pio_register_range(range);
4100 if (ret)
4101 kfree(range);
4102
4103 /* Ignore duplicates due to deferred probing */
4104 if (ret == -EEXIST)
4105 ret = 0;
4106 #endif
4107
4108 return ret;
4109 }
4110
pci_pio_to_address(unsigned long pio)4111 phys_addr_t pci_pio_to_address(unsigned long pio)
4112 {
4113 phys_addr_t address = (phys_addr_t)OF_BAD_ADDR;
4114
4115 #ifdef PCI_IOBASE
4116 if (pio >= MMIO_UPPER_LIMIT)
4117 return address;
4118
4119 address = logic_pio_to_hwaddr(pio);
4120 #endif
4121
4122 return address;
4123 }
4124 EXPORT_SYMBOL_GPL(pci_pio_to_address);
4125
pci_address_to_pio(phys_addr_t address)4126 unsigned long __weak pci_address_to_pio(phys_addr_t address)
4127 {
4128 #ifdef PCI_IOBASE
4129 return logic_pio_trans_cpuaddr(address);
4130 #else
4131 if (address > IO_SPACE_LIMIT)
4132 return (unsigned long)-1;
4133
4134 return (unsigned long) address;
4135 #endif
4136 }
4137
4138 /**
4139 * pci_remap_iospace - Remap the memory mapped I/O space
4140 * @res: Resource describing the I/O space
4141 * @phys_addr: physical address of range to be mapped
4142 *
4143 * Remap the memory mapped I/O space described by the @res and the CPU
4144 * physical address @phys_addr into virtual address space. Only
4145 * architectures that have memory mapped IO functions defined (and the
4146 * PCI_IOBASE value defined) should call this function.
4147 */
pci_remap_iospace(const struct resource * res,phys_addr_t phys_addr)4148 int pci_remap_iospace(const struct resource *res, phys_addr_t phys_addr)
4149 {
4150 #if defined(PCI_IOBASE) && defined(CONFIG_MMU)
4151 unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start;
4152
4153 if (!(res->flags & IORESOURCE_IO))
4154 return -EINVAL;
4155
4156 if (res->end > IO_SPACE_LIMIT)
4157 return -EINVAL;
4158
4159 return ioremap_page_range(vaddr, vaddr + resource_size(res), phys_addr,
4160 pgprot_device(PAGE_KERNEL));
4161 #else
4162 /*
4163 * This architecture does not have memory mapped I/O space,
4164 * so this function should never be called
4165 */
4166 WARN_ONCE(1, "This architecture does not support memory mapped I/O\n");
4167 return -ENODEV;
4168 #endif
4169 }
4170 EXPORT_SYMBOL(pci_remap_iospace);
4171
4172 /**
4173 * pci_unmap_iospace - Unmap the memory mapped I/O space
4174 * @res: resource to be unmapped
4175 *
4176 * Unmap the CPU virtual address @res from virtual address space. Only
4177 * architectures that have memory mapped IO functions defined (and the
4178 * PCI_IOBASE value defined) should call this function.
4179 */
pci_unmap_iospace(struct resource * res)4180 void pci_unmap_iospace(struct resource *res)
4181 {
4182 #if defined(PCI_IOBASE) && defined(CONFIG_MMU)
4183 unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start;
4184
4185 vunmap_range(vaddr, vaddr + resource_size(res));
4186 #endif
4187 }
4188 EXPORT_SYMBOL(pci_unmap_iospace);
4189
devm_pci_unmap_iospace(struct device * dev,void * ptr)4190 static void devm_pci_unmap_iospace(struct device *dev, void *ptr)
4191 {
4192 struct resource **res = ptr;
4193
4194 pci_unmap_iospace(*res);
4195 }
4196
4197 /**
4198 * devm_pci_remap_iospace - Managed pci_remap_iospace()
4199 * @dev: Generic device to remap IO address for
4200 * @res: Resource describing the I/O space
4201 * @phys_addr: physical address of range to be mapped
4202 *
4203 * Managed pci_remap_iospace(). Map is automatically unmapped on driver
4204 * detach.
4205 */
devm_pci_remap_iospace(struct device * dev,const struct resource * res,phys_addr_t phys_addr)4206 int devm_pci_remap_iospace(struct device *dev, const struct resource *res,
4207 phys_addr_t phys_addr)
4208 {
4209 const struct resource **ptr;
4210 int error;
4211
4212 ptr = devres_alloc(devm_pci_unmap_iospace, sizeof(*ptr), GFP_KERNEL);
4213 if (!ptr)
4214 return -ENOMEM;
4215
4216 error = pci_remap_iospace(res, phys_addr);
4217 if (error) {
4218 devres_free(ptr);
4219 } else {
4220 *ptr = res;
4221 devres_add(dev, ptr);
4222 }
4223
4224 return error;
4225 }
4226 EXPORT_SYMBOL(devm_pci_remap_iospace);
4227
4228 /**
4229 * devm_pci_remap_cfgspace - Managed pci_remap_cfgspace()
4230 * @dev: Generic device to remap IO address for
4231 * @offset: Resource address to map
4232 * @size: Size of map
4233 *
4234 * Managed pci_remap_cfgspace(). Map is automatically unmapped on driver
4235 * detach.
4236 */
devm_pci_remap_cfgspace(struct device * dev,resource_size_t offset,resource_size_t size)4237 void __iomem *devm_pci_remap_cfgspace(struct device *dev,
4238 resource_size_t offset,
4239 resource_size_t size)
4240 {
4241 void __iomem **ptr, *addr;
4242
4243 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
4244 if (!ptr)
4245 return NULL;
4246
4247 addr = pci_remap_cfgspace(offset, size);
4248 if (addr) {
4249 *ptr = addr;
4250 devres_add(dev, ptr);
4251 } else
4252 devres_free(ptr);
4253
4254 return addr;
4255 }
4256 EXPORT_SYMBOL(devm_pci_remap_cfgspace);
4257
4258 /**
4259 * devm_pci_remap_cfg_resource - check, request region and ioremap cfg resource
4260 * @dev: generic device to handle the resource for
4261 * @res: configuration space resource to be handled
4262 *
4263 * Checks that a resource is a valid memory region, requests the memory
4264 * region and ioremaps with pci_remap_cfgspace() API that ensures the
4265 * proper PCI configuration space memory attributes are guaranteed.
4266 *
4267 * All operations are managed and will be undone on driver detach.
4268 *
4269 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
4270 * on failure. Usage example::
4271 *
4272 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4273 * base = devm_pci_remap_cfg_resource(&pdev->dev, res);
4274 * if (IS_ERR(base))
4275 * return PTR_ERR(base);
4276 */
devm_pci_remap_cfg_resource(struct device * dev,struct resource * res)4277 void __iomem *devm_pci_remap_cfg_resource(struct device *dev,
4278 struct resource *res)
4279 {
4280 resource_size_t size;
4281 const char *name;
4282 void __iomem *dest_ptr;
4283
4284 BUG_ON(!dev);
4285
4286 if (!res || resource_type(res) != IORESOURCE_MEM) {
4287 dev_err(dev, "invalid resource\n");
4288 return IOMEM_ERR_PTR(-EINVAL);
4289 }
4290
4291 size = resource_size(res);
4292
4293 if (res->name)
4294 name = devm_kasprintf(dev, GFP_KERNEL, "%s %s", dev_name(dev),
4295 res->name);
4296 else
4297 name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
4298 if (!name)
4299 return IOMEM_ERR_PTR(-ENOMEM);
4300
4301 if (!devm_request_mem_region(dev, res->start, size, name)) {
4302 dev_err(dev, "can't request region for resource %pR\n", res);
4303 return IOMEM_ERR_PTR(-EBUSY);
4304 }
4305
4306 dest_ptr = devm_pci_remap_cfgspace(dev, res->start, size);
4307 if (!dest_ptr) {
4308 dev_err(dev, "ioremap failed for resource %pR\n", res);
4309 devm_release_mem_region(dev, res->start, size);
4310 dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
4311 }
4312
4313 return dest_ptr;
4314 }
4315 EXPORT_SYMBOL(devm_pci_remap_cfg_resource);
4316
__pci_set_master(struct pci_dev * dev,bool enable)4317 static void __pci_set_master(struct pci_dev *dev, bool enable)
4318 {
4319 u16 old_cmd, cmd;
4320
4321 pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
4322 if (enable)
4323 cmd = old_cmd | PCI_COMMAND_MASTER;
4324 else
4325 cmd = old_cmd & ~PCI_COMMAND_MASTER;
4326 if (cmd != old_cmd) {
4327 pci_dbg(dev, "%s bus mastering\n",
4328 enable ? "enabling" : "disabling");
4329 pci_write_config_word(dev, PCI_COMMAND, cmd);
4330 }
4331 dev->is_busmaster = enable;
4332 }
4333
4334 /**
4335 * pcibios_setup - process "pci=" kernel boot arguments
4336 * @str: string used to pass in "pci=" kernel boot arguments
4337 *
4338 * Process kernel boot arguments. This is the default implementation.
4339 * Architecture specific implementations can override this as necessary.
4340 */
pcibios_setup(char * str)4341 char * __weak __init pcibios_setup(char *str)
4342 {
4343 return str;
4344 }
4345
4346 /**
4347 * pcibios_set_master - enable PCI bus-mastering for device dev
4348 * @dev: the PCI device to enable
4349 *
4350 * Enables PCI bus-mastering for the device. This is the default
4351 * implementation. Architecture specific implementations can override
4352 * this if necessary.
4353 */
pcibios_set_master(struct pci_dev * dev)4354 void __weak pcibios_set_master(struct pci_dev *dev)
4355 {
4356 u8 lat;
4357
4358 /* The latency timer doesn't apply to PCIe (either Type 0 or Type 1) */
4359 if (pci_is_pcie(dev))
4360 return;
4361
4362 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4363 if (lat < 16)
4364 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
4365 else if (lat > pcibios_max_latency)
4366 lat = pcibios_max_latency;
4367 else
4368 return;
4369
4370 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
4371 }
4372
4373 /**
4374 * pci_set_master - enables bus-mastering for device dev
4375 * @dev: the PCI device to enable
4376 *
4377 * Enables bus-mastering on the device and calls pcibios_set_master()
4378 * to do the needed arch specific settings.
4379 */
pci_set_master(struct pci_dev * dev)4380 void pci_set_master(struct pci_dev *dev)
4381 {
4382 __pci_set_master(dev, true);
4383 pcibios_set_master(dev);
4384 }
4385 EXPORT_SYMBOL(pci_set_master);
4386
4387 /**
4388 * pci_clear_master - disables bus-mastering for device dev
4389 * @dev: the PCI device to disable
4390 */
pci_clear_master(struct pci_dev * dev)4391 void pci_clear_master(struct pci_dev *dev)
4392 {
4393 __pci_set_master(dev, false);
4394 }
4395 EXPORT_SYMBOL(pci_clear_master);
4396
4397 /**
4398 * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
4399 * @dev: the PCI device for which MWI is to be enabled
4400 *
4401 * Helper function for pci_set_mwi.
4402 * Originally copied from drivers/net/acenic.c.
4403 * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
4404 *
4405 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
4406 */
pci_set_cacheline_size(struct pci_dev * dev)4407 int pci_set_cacheline_size(struct pci_dev *dev)
4408 {
4409 u8 cacheline_size;
4410
4411 if (!pci_cache_line_size)
4412 return -EINVAL;
4413
4414 /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
4415 equal to or multiple of the right value. */
4416 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
4417 if (cacheline_size >= pci_cache_line_size &&
4418 (cacheline_size % pci_cache_line_size) == 0)
4419 return 0;
4420
4421 /* Write the correct value. */
4422 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
4423 /* Read it back. */
4424 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
4425 if (cacheline_size == pci_cache_line_size)
4426 return 0;
4427
4428 pci_dbg(dev, "cache line size of %d is not supported\n",
4429 pci_cache_line_size << 2);
4430
4431 return -EINVAL;
4432 }
4433 EXPORT_SYMBOL_GPL(pci_set_cacheline_size);
4434
4435 /**
4436 * pci_set_mwi - enables memory-write-invalidate PCI transaction
4437 * @dev: the PCI device for which MWI is enabled
4438 *
4439 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
4440 *
4441 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
4442 */
pci_set_mwi(struct pci_dev * dev)4443 int pci_set_mwi(struct pci_dev *dev)
4444 {
4445 #ifdef PCI_DISABLE_MWI
4446 return 0;
4447 #else
4448 int rc;
4449 u16 cmd;
4450
4451 rc = pci_set_cacheline_size(dev);
4452 if (rc)
4453 return rc;
4454
4455 pci_read_config_word(dev, PCI_COMMAND, &cmd);
4456 if (!(cmd & PCI_COMMAND_INVALIDATE)) {
4457 pci_dbg(dev, "enabling Mem-Wr-Inval\n");
4458 cmd |= PCI_COMMAND_INVALIDATE;
4459 pci_write_config_word(dev, PCI_COMMAND, cmd);
4460 }
4461 return 0;
4462 #endif
4463 }
4464 EXPORT_SYMBOL(pci_set_mwi);
4465
4466 /**
4467 * pcim_set_mwi - a device-managed pci_set_mwi()
4468 * @dev: the PCI device for which MWI is enabled
4469 *
4470 * Managed pci_set_mwi().
4471 *
4472 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
4473 */
pcim_set_mwi(struct pci_dev * dev)4474 int pcim_set_mwi(struct pci_dev *dev)
4475 {
4476 struct pci_devres *dr;
4477
4478 dr = find_pci_dr(dev);
4479 if (!dr)
4480 return -ENOMEM;
4481
4482 dr->mwi = 1;
4483 return pci_set_mwi(dev);
4484 }
4485 EXPORT_SYMBOL(pcim_set_mwi);
4486
4487 /**
4488 * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
4489 * @dev: the PCI device for which MWI is enabled
4490 *
4491 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
4492 * Callers are not required to check the return value.
4493 *
4494 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
4495 */
pci_try_set_mwi(struct pci_dev * dev)4496 int pci_try_set_mwi(struct pci_dev *dev)
4497 {
4498 #ifdef PCI_DISABLE_MWI
4499 return 0;
4500 #else
4501 return pci_set_mwi(dev);
4502 #endif
4503 }
4504 EXPORT_SYMBOL(pci_try_set_mwi);
4505
4506 /**
4507 * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
4508 * @dev: the PCI device to disable
4509 *
4510 * Disables PCI Memory-Write-Invalidate transaction on the device
4511 */
pci_clear_mwi(struct pci_dev * dev)4512 void pci_clear_mwi(struct pci_dev *dev)
4513 {
4514 #ifndef PCI_DISABLE_MWI
4515 u16 cmd;
4516
4517 pci_read_config_word(dev, PCI_COMMAND, &cmd);
4518 if (cmd & PCI_COMMAND_INVALIDATE) {
4519 cmd &= ~PCI_COMMAND_INVALIDATE;
4520 pci_write_config_word(dev, PCI_COMMAND, cmd);
4521 }
4522 #endif
4523 }
4524 EXPORT_SYMBOL(pci_clear_mwi);
4525
4526 /**
4527 * pci_disable_parity - disable parity checking for device
4528 * @dev: the PCI device to operate on
4529 *
4530 * Disable parity checking for device @dev
4531 */
pci_disable_parity(struct pci_dev * dev)4532 void pci_disable_parity(struct pci_dev *dev)
4533 {
4534 u16 cmd;
4535
4536 pci_read_config_word(dev, PCI_COMMAND, &cmd);
4537 if (cmd & PCI_COMMAND_PARITY) {
4538 cmd &= ~PCI_COMMAND_PARITY;
4539 pci_write_config_word(dev, PCI_COMMAND, cmd);
4540 }
4541 }
4542
4543 /**
4544 * pci_intx - enables/disables PCI INTx for device dev
4545 * @pdev: the PCI device to operate on
4546 * @enable: boolean: whether to enable or disable PCI INTx
4547 *
4548 * Enables/disables PCI INTx for device @pdev
4549 */
pci_intx(struct pci_dev * pdev,int enable)4550 void pci_intx(struct pci_dev *pdev, int enable)
4551 {
4552 u16 pci_command, new;
4553
4554 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
4555
4556 if (enable)
4557 new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
4558 else
4559 new = pci_command | PCI_COMMAND_INTX_DISABLE;
4560
4561 if (new != pci_command) {
4562 struct pci_devres *dr;
4563
4564 pci_write_config_word(pdev, PCI_COMMAND, new);
4565
4566 dr = find_pci_dr(pdev);
4567 if (dr && !dr->restore_intx) {
4568 dr->restore_intx = 1;
4569 dr->orig_intx = !enable;
4570 }
4571 }
4572 }
4573 EXPORT_SYMBOL_GPL(pci_intx);
4574
pci_check_and_set_intx_mask(struct pci_dev * dev,bool mask)4575 static bool pci_check_and_set_intx_mask(struct pci_dev *dev, bool mask)
4576 {
4577 struct pci_bus *bus = dev->bus;
4578 bool mask_updated = true;
4579 u32 cmd_status_dword;
4580 u16 origcmd, newcmd;
4581 unsigned long flags;
4582 bool irq_pending;
4583
4584 /*
4585 * We do a single dword read to retrieve both command and status.
4586 * Document assumptions that make this possible.
4587 */
4588 BUILD_BUG_ON(PCI_COMMAND % 4);
4589 BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
4590
4591 raw_spin_lock_irqsave(&pci_lock, flags);
4592
4593 bus->ops->read(bus, dev->devfn, PCI_COMMAND, 4, &cmd_status_dword);
4594
4595 irq_pending = (cmd_status_dword >> 16) & PCI_STATUS_INTERRUPT;
4596
4597 /*
4598 * Check interrupt status register to see whether our device
4599 * triggered the interrupt (when masking) or the next IRQ is
4600 * already pending (when unmasking).
4601 */
4602 if (mask != irq_pending) {
4603 mask_updated = false;
4604 goto done;
4605 }
4606
4607 origcmd = cmd_status_dword;
4608 newcmd = origcmd & ~PCI_COMMAND_INTX_DISABLE;
4609 if (mask)
4610 newcmd |= PCI_COMMAND_INTX_DISABLE;
4611 if (newcmd != origcmd)
4612 bus->ops->write(bus, dev->devfn, PCI_COMMAND, 2, newcmd);
4613
4614 done:
4615 raw_spin_unlock_irqrestore(&pci_lock, flags);
4616
4617 return mask_updated;
4618 }
4619
4620 /**
4621 * pci_check_and_mask_intx - mask INTx on pending interrupt
4622 * @dev: the PCI device to operate on
4623 *
4624 * Check if the device dev has its INTx line asserted, mask it and return
4625 * true in that case. False is returned if no interrupt was pending.
4626 */
pci_check_and_mask_intx(struct pci_dev * dev)4627 bool pci_check_and_mask_intx(struct pci_dev *dev)
4628 {
4629 return pci_check_and_set_intx_mask(dev, true);
4630 }
4631 EXPORT_SYMBOL_GPL(pci_check_and_mask_intx);
4632
4633 /**
4634 * pci_check_and_unmask_intx - unmask INTx if no interrupt is pending
4635 * @dev: the PCI device to operate on
4636 *
4637 * Check if the device dev has its INTx line asserted, unmask it if not and
4638 * return true. False is returned and the mask remains active if there was
4639 * still an interrupt pending.
4640 */
pci_check_and_unmask_intx(struct pci_dev * dev)4641 bool pci_check_and_unmask_intx(struct pci_dev *dev)
4642 {
4643 return pci_check_and_set_intx_mask(dev, false);
4644 }
4645 EXPORT_SYMBOL_GPL(pci_check_and_unmask_intx);
4646
4647 /**
4648 * pci_wait_for_pending_transaction - wait for pending transaction
4649 * @dev: the PCI device to operate on
4650 *
4651 * Return 0 if transaction is pending 1 otherwise.
4652 */
pci_wait_for_pending_transaction(struct pci_dev * dev)4653 int pci_wait_for_pending_transaction(struct pci_dev *dev)
4654 {
4655 if (!pci_is_pcie(dev))
4656 return 1;
4657
4658 return pci_wait_for_pending(dev, pci_pcie_cap(dev) + PCI_EXP_DEVSTA,
4659 PCI_EXP_DEVSTA_TRPND);
4660 }
4661 EXPORT_SYMBOL(pci_wait_for_pending_transaction);
4662
4663 /**
4664 * pcie_flr - initiate a PCIe function level reset
4665 * @dev: device to reset
4666 *
4667 * Initiate a function level reset unconditionally on @dev without
4668 * checking any flags and DEVCAP
4669 */
pcie_flr(struct pci_dev * dev)4670 int pcie_flr(struct pci_dev *dev)
4671 {
4672 if (!pci_wait_for_pending_transaction(dev))
4673 pci_err(dev, "timed out waiting for pending transaction; performing function level reset anyway\n");
4674
4675 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
4676
4677 if (dev->imm_ready)
4678 return 0;
4679
4680 /*
4681 * Per PCIe r4.0, sec 6.6.2, a device must complete an FLR within
4682 * 100ms, but may silently discard requests while the FLR is in
4683 * progress. Wait 100ms before trying to access the device.
4684 */
4685 msleep(100);
4686
4687 return pci_dev_wait(dev, "FLR", PCIE_RESET_READY_POLL_MS);
4688 }
4689 EXPORT_SYMBOL_GPL(pcie_flr);
4690
4691 /**
4692 * pcie_reset_flr - initiate a PCIe function level reset
4693 * @dev: device to reset
4694 * @probe: if true, return 0 if device can be reset this way
4695 *
4696 * Initiate a function level reset on @dev.
4697 */
pcie_reset_flr(struct pci_dev * dev,bool probe)4698 int pcie_reset_flr(struct pci_dev *dev, bool probe)
4699 {
4700 if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET)
4701 return -ENOTTY;
4702
4703 if (!(dev->devcap & PCI_EXP_DEVCAP_FLR))
4704 return -ENOTTY;
4705
4706 if (probe)
4707 return 0;
4708
4709 return pcie_flr(dev);
4710 }
4711 EXPORT_SYMBOL_GPL(pcie_reset_flr);
4712
pci_af_flr(struct pci_dev * dev,bool probe)4713 static int pci_af_flr(struct pci_dev *dev, bool probe)
4714 {
4715 int pos;
4716 u8 cap;
4717
4718 pos = pci_find_capability(dev, PCI_CAP_ID_AF);
4719 if (!pos)
4720 return -ENOTTY;
4721
4722 if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET)
4723 return -ENOTTY;
4724
4725 pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap);
4726 if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
4727 return -ENOTTY;
4728
4729 if (probe)
4730 return 0;
4731
4732 /*
4733 * Wait for Transaction Pending bit to clear. A word-aligned test
4734 * is used, so we use the control offset rather than status and shift
4735 * the test bit to match.
4736 */
4737 if (!pci_wait_for_pending(dev, pos + PCI_AF_CTRL,
4738 PCI_AF_STATUS_TP << 8))
4739 pci_err(dev, "timed out waiting for pending transaction; performing AF function level reset anyway\n");
4740
4741 pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
4742
4743 if (dev->imm_ready)
4744 return 0;
4745
4746 /*
4747 * Per Advanced Capabilities for Conventional PCI ECN, 13 April 2006,
4748 * updated 27 July 2006; a device must complete an FLR within
4749 * 100ms, but may silently discard requests while the FLR is in
4750 * progress. Wait 100ms before trying to access the device.
4751 */
4752 msleep(100);
4753
4754 return pci_dev_wait(dev, "AF_FLR", PCIE_RESET_READY_POLL_MS);
4755 }
4756
4757 /**
4758 * pci_pm_reset - Put device into PCI_D3 and back into PCI_D0.
4759 * @dev: Device to reset.
4760 * @probe: if true, return 0 if the device can be reset this way.
4761 *
4762 * If @dev supports native PCI PM and its PCI_PM_CTRL_NO_SOFT_RESET flag is
4763 * unset, it will be reinitialized internally when going from PCI_D3hot to
4764 * PCI_D0. If that's the case and the device is not in a low-power state
4765 * already, force it into PCI_D3hot and back to PCI_D0, causing it to be reset.
4766 *
4767 * NOTE: This causes the caller to sleep for twice the device power transition
4768 * cooldown period, which for the D0->D3hot and D3hot->D0 transitions is 10 ms
4769 * by default (i.e. unless the @dev's d3hot_delay field has a different value).
4770 * Moreover, only devices in D0 can be reset by this function.
4771 */
pci_pm_reset(struct pci_dev * dev,bool probe)4772 static int pci_pm_reset(struct pci_dev *dev, bool probe)
4773 {
4774 u16 csr;
4775
4776 if (!dev->pm_cap || dev->dev_flags & PCI_DEV_FLAGS_NO_PM_RESET)
4777 return -ENOTTY;
4778
4779 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr);
4780 if (csr & PCI_PM_CTRL_NO_SOFT_RESET)
4781 return -ENOTTY;
4782
4783 if (probe)
4784 return 0;
4785
4786 if (dev->current_state != PCI_D0)
4787 return -EINVAL;
4788
4789 csr &= ~PCI_PM_CTRL_STATE_MASK;
4790 csr |= PCI_D3hot;
4791 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
4792 pci_dev_d3_sleep(dev);
4793
4794 csr &= ~PCI_PM_CTRL_STATE_MASK;
4795 csr |= PCI_D0;
4796 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
4797 pci_dev_d3_sleep(dev);
4798
4799 return pci_dev_wait(dev, "PM D3hot->D0", PCIE_RESET_READY_POLL_MS);
4800 }
4801
4802 /**
4803 * pcie_wait_for_link_delay - Wait until link is active or inactive
4804 * @pdev: Bridge device
4805 * @active: waiting for active or inactive?
4806 * @delay: Delay to wait after link has become active (in ms)
4807 *
4808 * Use this to wait till link becomes active or inactive.
4809 */
pcie_wait_for_link_delay(struct pci_dev * pdev,bool active,int delay)4810 static bool pcie_wait_for_link_delay(struct pci_dev *pdev, bool active,
4811 int delay)
4812 {
4813 int timeout = 1000;
4814 bool ret;
4815 u16 lnk_status;
4816
4817 /*
4818 * Some controllers might not implement link active reporting. In this
4819 * case, we wait for 1000 ms + any delay requested by the caller.
4820 */
4821 if (!pdev->link_active_reporting) {
4822 msleep(timeout + delay);
4823 return true;
4824 }
4825
4826 /*
4827 * PCIe r4.0 sec 6.6.1, a component must enter LTSSM Detect within 20ms,
4828 * after which we should expect an link active if the reset was
4829 * successful. If so, software must wait a minimum 100ms before sending
4830 * configuration requests to devices downstream this port.
4831 *
4832 * If the link fails to activate, either the device was physically
4833 * removed or the link is permanently failed.
4834 */
4835 if (active)
4836 msleep(20);
4837 for (;;) {
4838 pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
4839 ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
4840 if (ret == active)
4841 break;
4842 if (timeout <= 0)
4843 break;
4844 msleep(10);
4845 timeout -= 10;
4846 }
4847 if (active && ret)
4848 msleep(delay);
4849
4850 return ret == active;
4851 }
4852
4853 /**
4854 * pcie_wait_for_link - Wait until link is active or inactive
4855 * @pdev: Bridge device
4856 * @active: waiting for active or inactive?
4857 *
4858 * Use this to wait till link becomes active or inactive.
4859 */
pcie_wait_for_link(struct pci_dev * pdev,bool active)4860 bool pcie_wait_for_link(struct pci_dev *pdev, bool active)
4861 {
4862 return pcie_wait_for_link_delay(pdev, active, 100);
4863 }
4864
4865 /*
4866 * Find maximum D3cold delay required by all the devices on the bus. The
4867 * spec says 100 ms, but firmware can lower it and we allow drivers to
4868 * increase it as well.
4869 *
4870 * Called with @pci_bus_sem locked for reading.
4871 */
pci_bus_max_d3cold_delay(const struct pci_bus * bus)4872 static int pci_bus_max_d3cold_delay(const struct pci_bus *bus)
4873 {
4874 const struct pci_dev *pdev;
4875 int min_delay = 100;
4876 int max_delay = 0;
4877
4878 list_for_each_entry(pdev, &bus->devices, bus_list) {
4879 if (pdev->d3cold_delay < min_delay)
4880 min_delay = pdev->d3cold_delay;
4881 if (pdev->d3cold_delay > max_delay)
4882 max_delay = pdev->d3cold_delay;
4883 }
4884
4885 return max(min_delay, max_delay);
4886 }
4887
4888 /**
4889 * pci_bridge_wait_for_secondary_bus - Wait for secondary bus to be accessible
4890 * @dev: PCI bridge
4891 * @reset_type: reset type in human-readable form
4892 * @timeout: maximum time to wait for devices on secondary bus (milliseconds)
4893 *
4894 * Handle necessary delays before access to the devices on the secondary
4895 * side of the bridge are permitted after D3cold to D0 transition
4896 * or Conventional Reset.
4897 *
4898 * For PCIe this means the delays in PCIe 5.0 section 6.6.1. For
4899 * conventional PCI it means Tpvrh + Trhfa specified in PCI 3.0 section
4900 * 4.3.2.
4901 *
4902 * Return 0 on success or -ENOTTY if the first device on the secondary bus
4903 * failed to become accessible.
4904 */
pci_bridge_wait_for_secondary_bus(struct pci_dev * dev,char * reset_type,int timeout)4905 int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type,
4906 int timeout)
4907 {
4908 struct pci_dev *child;
4909 int delay;
4910
4911 if (pci_dev_is_disconnected(dev))
4912 return 0;
4913
4914 if (!pci_is_bridge(dev))
4915 return 0;
4916
4917 down_read(&pci_bus_sem);
4918
4919 /*
4920 * We only deal with devices that are present currently on the bus.
4921 * For any hot-added devices the access delay is handled in pciehp
4922 * board_added(). In case of ACPI hotplug the firmware is expected
4923 * to configure the devices before OS is notified.
4924 */
4925 if (!dev->subordinate || list_empty(&dev->subordinate->devices)) {
4926 up_read(&pci_bus_sem);
4927 return 0;
4928 }
4929
4930 /* Take d3cold_delay requirements into account */
4931 delay = pci_bus_max_d3cold_delay(dev->subordinate);
4932 if (!delay) {
4933 up_read(&pci_bus_sem);
4934 return 0;
4935 }
4936
4937 child = list_first_entry(&dev->subordinate->devices, struct pci_dev,
4938 bus_list);
4939 up_read(&pci_bus_sem);
4940
4941 /*
4942 * Conventional PCI and PCI-X we need to wait Tpvrh + Trhfa before
4943 * accessing the device after reset (that is 1000 ms + 100 ms).
4944 */
4945 if (!pci_is_pcie(dev)) {
4946 pci_dbg(dev, "waiting %d ms for secondary bus\n", 1000 + delay);
4947 msleep(1000 + delay);
4948 return 0;
4949 }
4950
4951 /*
4952 * For PCIe downstream and root ports that do not support speeds
4953 * greater than 5 GT/s need to wait minimum 100 ms. For higher
4954 * speeds (gen3) we need to wait first for the data link layer to
4955 * become active.
4956 *
4957 * However, 100 ms is the minimum and the PCIe spec says the
4958 * software must allow at least 1s before it can determine that the
4959 * device that did not respond is a broken device. There is
4960 * evidence that 100 ms is not always enough, for example certain
4961 * Titan Ridge xHCI controller does not always respond to
4962 * configuration requests if we only wait for 100 ms (see
4963 * https://bugzilla.kernel.org/show_bug.cgi?id=203885).
4964 *
4965 * Therefore we wait for 100 ms and check for the device presence
4966 * until the timeout expires.
4967 */
4968 if (!pcie_downstream_port(dev))
4969 return 0;
4970
4971 if (pcie_get_speed_cap(dev) <= PCIE_SPEED_5_0GT) {
4972 pci_dbg(dev, "waiting %d ms for downstream link\n", delay);
4973 msleep(delay);
4974 } else {
4975 pci_dbg(dev, "waiting %d ms for downstream link, after activation\n",
4976 delay);
4977 if (!pcie_wait_for_link_delay(dev, true, delay)) {
4978 /* Did not train, no need to wait any further */
4979 pci_info(dev, "Data Link Layer Link Active not set in 1000 msec\n");
4980 return -ENOTTY;
4981 }
4982 }
4983
4984 return pci_dev_wait(child, reset_type, timeout - delay);
4985 }
4986
pci_reset_secondary_bus(struct pci_dev * dev)4987 void pci_reset_secondary_bus(struct pci_dev *dev)
4988 {
4989 u16 ctrl;
4990
4991 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &ctrl);
4992 ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
4993 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
4994
4995 /*
4996 * PCI spec v3.0 7.6.4.2 requires minimum Trst of 1ms. Double
4997 * this to 2ms to ensure that we meet the minimum requirement.
4998 */
4999 msleep(2);
5000
5001 ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
5002 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
5003 }
5004
pcibios_reset_secondary_bus(struct pci_dev * dev)5005 void __weak pcibios_reset_secondary_bus(struct pci_dev *dev)
5006 {
5007 pci_reset_secondary_bus(dev);
5008 }
5009
5010 /**
5011 * pci_bridge_secondary_bus_reset - Reset the secondary bus on a PCI bridge.
5012 * @dev: Bridge device
5013 *
5014 * Use the bridge control register to assert reset on the secondary bus.
5015 * Devices on the secondary bus are left in power-on state.
5016 */
pci_bridge_secondary_bus_reset(struct pci_dev * dev)5017 int pci_bridge_secondary_bus_reset(struct pci_dev *dev)
5018 {
5019 pcibios_reset_secondary_bus(dev);
5020
5021 return pci_bridge_wait_for_secondary_bus(dev, "bus reset",
5022 PCIE_RESET_READY_POLL_MS);
5023 }
5024 EXPORT_SYMBOL_GPL(pci_bridge_secondary_bus_reset);
5025
pci_parent_bus_reset(struct pci_dev * dev,bool probe)5026 static int pci_parent_bus_reset(struct pci_dev *dev, bool probe)
5027 {
5028 struct pci_dev *pdev;
5029
5030 if (pci_is_root_bus(dev->bus) || dev->subordinate ||
5031 !dev->bus->self || dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)
5032 return -ENOTTY;
5033
5034 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
5035 if (pdev != dev)
5036 return -ENOTTY;
5037
5038 if (probe)
5039 return 0;
5040
5041 return pci_bridge_secondary_bus_reset(dev->bus->self);
5042 }
5043
pci_reset_hotplug_slot(struct hotplug_slot * hotplug,bool probe)5044 static int pci_reset_hotplug_slot(struct hotplug_slot *hotplug, bool probe)
5045 {
5046 int rc = -ENOTTY;
5047
5048 if (!hotplug || !try_module_get(hotplug->owner))
5049 return rc;
5050
5051 if (hotplug->ops->reset_slot)
5052 rc = hotplug->ops->reset_slot(hotplug, probe);
5053
5054 module_put(hotplug->owner);
5055
5056 return rc;
5057 }
5058
pci_dev_reset_slot_function(struct pci_dev * dev,bool probe)5059 static int pci_dev_reset_slot_function(struct pci_dev *dev, bool probe)
5060 {
5061 if (dev->multifunction || dev->subordinate || !dev->slot ||
5062 dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)
5063 return -ENOTTY;
5064
5065 return pci_reset_hotplug_slot(dev->slot->hotplug, probe);
5066 }
5067
pci_reset_bus_function(struct pci_dev * dev,bool probe)5068 static int pci_reset_bus_function(struct pci_dev *dev, bool probe)
5069 {
5070 int rc;
5071
5072 rc = pci_dev_reset_slot_function(dev, probe);
5073 if (rc != -ENOTTY)
5074 return rc;
5075 return pci_parent_bus_reset(dev, probe);
5076 }
5077
pci_dev_lock(struct pci_dev * dev)5078 static void pci_dev_lock(struct pci_dev *dev)
5079 {
5080 /* block PM suspend, driver probe, etc. */
5081 device_lock(&dev->dev);
5082 pci_cfg_access_lock(dev);
5083 }
5084
5085 /* Return 1 on successful lock, 0 on contention */
pci_dev_trylock(struct pci_dev * dev)5086 int pci_dev_trylock(struct pci_dev *dev)
5087 {
5088 if (device_trylock(&dev->dev)) {
5089 if (pci_cfg_access_trylock(dev))
5090 return 1;
5091 device_unlock(&dev->dev);
5092 }
5093
5094 return 0;
5095 }
5096 EXPORT_SYMBOL_GPL(pci_dev_trylock);
5097
pci_dev_unlock(struct pci_dev * dev)5098 void pci_dev_unlock(struct pci_dev *dev)
5099 {
5100 pci_cfg_access_unlock(dev);
5101 device_unlock(&dev->dev);
5102 }
5103 EXPORT_SYMBOL_GPL(pci_dev_unlock);
5104
pci_dev_save_and_disable(struct pci_dev * dev)5105 static void pci_dev_save_and_disable(struct pci_dev *dev)
5106 {
5107 const struct pci_error_handlers *err_handler =
5108 dev->driver ? dev->driver->err_handler : NULL;
5109
5110 /*
5111 * dev->driver->err_handler->reset_prepare() is protected against
5112 * races with ->remove() by the device lock, which must be held by
5113 * the caller.
5114 */
5115 if (err_handler && err_handler->reset_prepare)
5116 err_handler->reset_prepare(dev);
5117
5118 /*
5119 * Wake-up device prior to save. PM registers default to D0 after
5120 * reset and a simple register restore doesn't reliably return
5121 * to a non-D0 state anyway.
5122 */
5123 pci_set_power_state(dev, PCI_D0);
5124
5125 pci_save_state(dev);
5126 /*
5127 * Disable the device by clearing the Command register, except for
5128 * INTx-disable which is set. This not only disables MMIO and I/O port
5129 * BARs, but also prevents the device from being Bus Master, preventing
5130 * DMA from the device including MSI/MSI-X interrupts. For PCI 2.3
5131 * compliant devices, INTx-disable prevents legacy interrupts.
5132 */
5133 pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
5134 }
5135
pci_dev_restore(struct pci_dev * dev)5136 static void pci_dev_restore(struct pci_dev *dev)
5137 {
5138 const struct pci_error_handlers *err_handler =
5139 dev->driver ? dev->driver->err_handler : NULL;
5140
5141 pci_restore_state(dev);
5142
5143 /*
5144 * dev->driver->err_handler->reset_done() is protected against
5145 * races with ->remove() by the device lock, which must be held by
5146 * the caller.
5147 */
5148 if (err_handler && err_handler->reset_done)
5149 err_handler->reset_done(dev);
5150 }
5151
5152 /* dev->reset_methods[] is a 0-terminated list of indices into this array */
5153 static const struct pci_reset_fn_method pci_reset_fn_methods[] = {
5154 { },
5155 { pci_dev_specific_reset, .name = "device_specific" },
5156 { pci_dev_acpi_reset, .name = "acpi" },
5157 { pcie_reset_flr, .name = "flr" },
5158 { pci_af_flr, .name = "af_flr" },
5159 { pci_pm_reset, .name = "pm" },
5160 { pci_reset_bus_function, .name = "bus" },
5161 };
5162
reset_method_show(struct device * dev,struct device_attribute * attr,char * buf)5163 static ssize_t reset_method_show(struct device *dev,
5164 struct device_attribute *attr, char *buf)
5165 {
5166 struct pci_dev *pdev = to_pci_dev(dev);
5167 ssize_t len = 0;
5168 int i, m;
5169
5170 for (i = 0; i < PCI_NUM_RESET_METHODS; i++) {
5171 m = pdev->reset_methods[i];
5172 if (!m)
5173 break;
5174
5175 len += sysfs_emit_at(buf, len, "%s%s", len ? " " : "",
5176 pci_reset_fn_methods[m].name);
5177 }
5178
5179 if (len)
5180 len += sysfs_emit_at(buf, len, "\n");
5181
5182 return len;
5183 }
5184
reset_method_lookup(const char * name)5185 static int reset_method_lookup(const char *name)
5186 {
5187 int m;
5188
5189 for (m = 1; m < PCI_NUM_RESET_METHODS; m++) {
5190 if (sysfs_streq(name, pci_reset_fn_methods[m].name))
5191 return m;
5192 }
5193
5194 return 0; /* not found */
5195 }
5196
reset_method_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)5197 static ssize_t reset_method_store(struct device *dev,
5198 struct device_attribute *attr,
5199 const char *buf, size_t count)
5200 {
5201 struct pci_dev *pdev = to_pci_dev(dev);
5202 char *options, *name;
5203 int m, n;
5204 u8 reset_methods[PCI_NUM_RESET_METHODS] = { 0 };
5205
5206 if (sysfs_streq(buf, "")) {
5207 pdev->reset_methods[0] = 0;
5208 pci_warn(pdev, "All device reset methods disabled by user");
5209 return count;
5210 }
5211
5212 if (sysfs_streq(buf, "default")) {
5213 pci_init_reset_methods(pdev);
5214 return count;
5215 }
5216
5217 options = kstrndup(buf, count, GFP_KERNEL);
5218 if (!options)
5219 return -ENOMEM;
5220
5221 n = 0;
5222 while ((name = strsep(&options, " ")) != NULL) {
5223 if (sysfs_streq(name, ""))
5224 continue;
5225
5226 name = strim(name);
5227
5228 m = reset_method_lookup(name);
5229 if (!m) {
5230 pci_err(pdev, "Invalid reset method '%s'", name);
5231 goto error;
5232 }
5233
5234 if (pci_reset_fn_methods[m].reset_fn(pdev, PCI_RESET_PROBE)) {
5235 pci_err(pdev, "Unsupported reset method '%s'", name);
5236 goto error;
5237 }
5238
5239 if (n == PCI_NUM_RESET_METHODS - 1) {
5240 pci_err(pdev, "Too many reset methods\n");
5241 goto error;
5242 }
5243
5244 reset_methods[n++] = m;
5245 }
5246
5247 reset_methods[n] = 0;
5248
5249 /* Warn if dev-specific supported but not highest priority */
5250 if (pci_reset_fn_methods[1].reset_fn(pdev, PCI_RESET_PROBE) == 0 &&
5251 reset_methods[0] != 1)
5252 pci_warn(pdev, "Device-specific reset disabled/de-prioritized by user");
5253 memcpy(pdev->reset_methods, reset_methods, sizeof(pdev->reset_methods));
5254 kfree(options);
5255 return count;
5256
5257 error:
5258 /* Leave previous methods unchanged */
5259 kfree(options);
5260 return -EINVAL;
5261 }
5262 static DEVICE_ATTR_RW(reset_method);
5263
5264 static struct attribute *pci_dev_reset_method_attrs[] = {
5265 &dev_attr_reset_method.attr,
5266 NULL,
5267 };
5268
pci_dev_reset_method_attr_is_visible(struct kobject * kobj,struct attribute * a,int n)5269 static umode_t pci_dev_reset_method_attr_is_visible(struct kobject *kobj,
5270 struct attribute *a, int n)
5271 {
5272 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
5273
5274 if (!pci_reset_supported(pdev))
5275 return 0;
5276
5277 return a->mode;
5278 }
5279
5280 const struct attribute_group pci_dev_reset_method_attr_group = {
5281 .attrs = pci_dev_reset_method_attrs,
5282 .is_visible = pci_dev_reset_method_attr_is_visible,
5283 };
5284
5285 /**
5286 * __pci_reset_function_locked - reset a PCI device function while holding
5287 * the @dev mutex lock.
5288 * @dev: PCI device to reset
5289 *
5290 * Some devices allow an individual function to be reset without affecting
5291 * other functions in the same device. The PCI device must be responsive
5292 * to PCI config space in order to use this function.
5293 *
5294 * The device function is presumed to be unused and the caller is holding
5295 * the device mutex lock when this function is called.
5296 *
5297 * Resetting the device will make the contents of PCI configuration space
5298 * random, so any caller of this must be prepared to reinitialise the
5299 * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
5300 * etc.
5301 *
5302 * Returns 0 if the device function was successfully reset or negative if the
5303 * device doesn't support resetting a single function.
5304 */
__pci_reset_function_locked(struct pci_dev * dev)5305 int __pci_reset_function_locked(struct pci_dev *dev)
5306 {
5307 int i, m, rc = -ENOTTY;
5308
5309 might_sleep();
5310
5311 /*
5312 * A reset method returns -ENOTTY if it doesn't support this device and
5313 * we should try the next method.
5314 *
5315 * If it returns 0 (success), we're finished. If it returns any other
5316 * error, we're also finished: this indicates that further reset
5317 * mechanisms might be broken on the device.
5318 */
5319 for (i = 0; i < PCI_NUM_RESET_METHODS; i++) {
5320 m = dev->reset_methods[i];
5321 if (!m)
5322 return -ENOTTY;
5323
5324 rc = pci_reset_fn_methods[m].reset_fn(dev, PCI_RESET_DO_RESET);
5325 if (!rc)
5326 return 0;
5327 if (rc != -ENOTTY)
5328 return rc;
5329 }
5330
5331 return -ENOTTY;
5332 }
5333 EXPORT_SYMBOL_GPL(__pci_reset_function_locked);
5334
5335 /**
5336 * pci_init_reset_methods - check whether device can be safely reset
5337 * and store supported reset mechanisms.
5338 * @dev: PCI device to check for reset mechanisms
5339 *
5340 * Some devices allow an individual function to be reset without affecting
5341 * other functions in the same device. The PCI device must be in D0-D3hot
5342 * state.
5343 *
5344 * Stores reset mechanisms supported by device in reset_methods byte array
5345 * which is a member of struct pci_dev.
5346 */
pci_init_reset_methods(struct pci_dev * dev)5347 void pci_init_reset_methods(struct pci_dev *dev)
5348 {
5349 int m, i, rc;
5350
5351 BUILD_BUG_ON(ARRAY_SIZE(pci_reset_fn_methods) != PCI_NUM_RESET_METHODS);
5352
5353 might_sleep();
5354
5355 i = 0;
5356 for (m = 1; m < PCI_NUM_RESET_METHODS; m++) {
5357 rc = pci_reset_fn_methods[m].reset_fn(dev, PCI_RESET_PROBE);
5358 if (!rc)
5359 dev->reset_methods[i++] = m;
5360 else if (rc != -ENOTTY)
5361 break;
5362 }
5363
5364 dev->reset_methods[i] = 0;
5365 }
5366
5367 /**
5368 * pci_reset_function - quiesce and reset a PCI device function
5369 * @dev: PCI device to reset
5370 *
5371 * Some devices allow an individual function to be reset without affecting
5372 * other functions in the same device. The PCI device must be responsive
5373 * to PCI config space in order to use this function.
5374 *
5375 * This function does not just reset the PCI portion of a device, but
5376 * clears all the state associated with the device. This function differs
5377 * from __pci_reset_function_locked() in that it saves and restores device state
5378 * over the reset and takes the PCI device lock.
5379 *
5380 * Returns 0 if the device function was successfully reset or negative if the
5381 * device doesn't support resetting a single function.
5382 */
pci_reset_function(struct pci_dev * dev)5383 int pci_reset_function(struct pci_dev *dev)
5384 {
5385 int rc;
5386
5387 if (!pci_reset_supported(dev))
5388 return -ENOTTY;
5389
5390 pci_dev_lock(dev);
5391 pci_dev_save_and_disable(dev);
5392
5393 rc = __pci_reset_function_locked(dev);
5394
5395 pci_dev_restore(dev);
5396 pci_dev_unlock(dev);
5397
5398 return rc;
5399 }
5400 EXPORT_SYMBOL_GPL(pci_reset_function);
5401
5402 /**
5403 * pci_reset_function_locked - quiesce and reset a PCI device function
5404 * @dev: PCI device to reset
5405 *
5406 * Some devices allow an individual function to be reset without affecting
5407 * other functions in the same device. The PCI device must be responsive
5408 * to PCI config space in order to use this function.
5409 *
5410 * This function does not just reset the PCI portion of a device, but
5411 * clears all the state associated with the device. This function differs
5412 * from __pci_reset_function_locked() in that it saves and restores device state
5413 * over the reset. It also differs from pci_reset_function() in that it
5414 * requires the PCI device lock to be held.
5415 *
5416 * Returns 0 if the device function was successfully reset or negative if the
5417 * device doesn't support resetting a single function.
5418 */
pci_reset_function_locked(struct pci_dev * dev)5419 int pci_reset_function_locked(struct pci_dev *dev)
5420 {
5421 int rc;
5422
5423 if (!pci_reset_supported(dev))
5424 return -ENOTTY;
5425
5426 pci_dev_save_and_disable(dev);
5427
5428 rc = __pci_reset_function_locked(dev);
5429
5430 pci_dev_restore(dev);
5431
5432 return rc;
5433 }
5434 EXPORT_SYMBOL_GPL(pci_reset_function_locked);
5435
5436 /**
5437 * pci_try_reset_function - quiesce and reset a PCI device function
5438 * @dev: PCI device to reset
5439 *
5440 * Same as above, except return -EAGAIN if unable to lock device.
5441 */
pci_try_reset_function(struct pci_dev * dev)5442 int pci_try_reset_function(struct pci_dev *dev)
5443 {
5444 int rc;
5445
5446 if (!pci_reset_supported(dev))
5447 return -ENOTTY;
5448
5449 if (!pci_dev_trylock(dev))
5450 return -EAGAIN;
5451
5452 pci_dev_save_and_disable(dev);
5453 rc = __pci_reset_function_locked(dev);
5454 pci_dev_restore(dev);
5455 pci_dev_unlock(dev);
5456
5457 return rc;
5458 }
5459 EXPORT_SYMBOL_GPL(pci_try_reset_function);
5460
5461 /* Do any devices on or below this bus prevent a bus reset? */
pci_bus_resetable(struct pci_bus * bus)5462 static bool pci_bus_resetable(struct pci_bus *bus)
5463 {
5464 struct pci_dev *dev;
5465
5466
5467 if (bus->self && (bus->self->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET))
5468 return false;
5469
5470 list_for_each_entry(dev, &bus->devices, bus_list) {
5471 if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET ||
5472 (dev->subordinate && !pci_bus_resetable(dev->subordinate)))
5473 return false;
5474 }
5475
5476 return true;
5477 }
5478
5479 /* Lock devices from the top of the tree down */
pci_bus_lock(struct pci_bus * bus)5480 static void pci_bus_lock(struct pci_bus *bus)
5481 {
5482 struct pci_dev *dev;
5483
5484 list_for_each_entry(dev, &bus->devices, bus_list) {
5485 pci_dev_lock(dev);
5486 if (dev->subordinate)
5487 pci_bus_lock(dev->subordinate);
5488 }
5489 }
5490
5491 /* Unlock devices from the bottom of the tree up */
pci_bus_unlock(struct pci_bus * bus)5492 static void pci_bus_unlock(struct pci_bus *bus)
5493 {
5494 struct pci_dev *dev;
5495
5496 list_for_each_entry(dev, &bus->devices, bus_list) {
5497 if (dev->subordinate)
5498 pci_bus_unlock(dev->subordinate);
5499 pci_dev_unlock(dev);
5500 }
5501 }
5502
5503 /* Return 1 on successful lock, 0 on contention */
pci_bus_trylock(struct pci_bus * bus)5504 static int pci_bus_trylock(struct pci_bus *bus)
5505 {
5506 struct pci_dev *dev;
5507
5508 list_for_each_entry(dev, &bus->devices, bus_list) {
5509 if (!pci_dev_trylock(dev))
5510 goto unlock;
5511 if (dev->subordinate) {
5512 if (!pci_bus_trylock(dev->subordinate)) {
5513 pci_dev_unlock(dev);
5514 goto unlock;
5515 }
5516 }
5517 }
5518 return 1;
5519
5520 unlock:
5521 list_for_each_entry_continue_reverse(dev, &bus->devices, bus_list) {
5522 if (dev->subordinate)
5523 pci_bus_unlock(dev->subordinate);
5524 pci_dev_unlock(dev);
5525 }
5526 return 0;
5527 }
5528
5529 /* Do any devices on or below this slot prevent a bus reset? */
pci_slot_resetable(struct pci_slot * slot)5530 static bool pci_slot_resetable(struct pci_slot *slot)
5531 {
5532 struct pci_dev *dev;
5533
5534 if (slot->bus->self &&
5535 (slot->bus->self->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET))
5536 return false;
5537
5538 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
5539 if (!dev->slot || dev->slot != slot)
5540 continue;
5541 if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET ||
5542 (dev->subordinate && !pci_bus_resetable(dev->subordinate)))
5543 return false;
5544 }
5545
5546 return true;
5547 }
5548
5549 /* Lock devices from the top of the tree down */
pci_slot_lock(struct pci_slot * slot)5550 static void pci_slot_lock(struct pci_slot *slot)
5551 {
5552 struct pci_dev *dev;
5553
5554 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
5555 if (!dev->slot || dev->slot != slot)
5556 continue;
5557 pci_dev_lock(dev);
5558 if (dev->subordinate)
5559 pci_bus_lock(dev->subordinate);
5560 }
5561 }
5562
5563 /* Unlock devices from the bottom of the tree up */
pci_slot_unlock(struct pci_slot * slot)5564 static void pci_slot_unlock(struct pci_slot *slot)
5565 {
5566 struct pci_dev *dev;
5567
5568 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
5569 if (!dev->slot || dev->slot != slot)
5570 continue;
5571 if (dev->subordinate)
5572 pci_bus_unlock(dev->subordinate);
5573 pci_dev_unlock(dev);
5574 }
5575 }
5576
5577 /* Return 1 on successful lock, 0 on contention */
pci_slot_trylock(struct pci_slot * slot)5578 static int pci_slot_trylock(struct pci_slot *slot)
5579 {
5580 struct pci_dev *dev;
5581
5582 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
5583 if (!dev->slot || dev->slot != slot)
5584 continue;
5585 if (!pci_dev_trylock(dev))
5586 goto unlock;
5587 if (dev->subordinate) {
5588 if (!pci_bus_trylock(dev->subordinate)) {
5589 pci_dev_unlock(dev);
5590 goto unlock;
5591 }
5592 }
5593 }
5594 return 1;
5595
5596 unlock:
5597 list_for_each_entry_continue_reverse(dev,
5598 &slot->bus->devices, bus_list) {
5599 if (!dev->slot || dev->slot != slot)
5600 continue;
5601 if (dev->subordinate)
5602 pci_bus_unlock(dev->subordinate);
5603 pci_dev_unlock(dev);
5604 }
5605 return 0;
5606 }
5607
5608 /*
5609 * Save and disable devices from the top of the tree down while holding
5610 * the @dev mutex lock for the entire tree.
5611 */
pci_bus_save_and_disable_locked(struct pci_bus * bus)5612 static void pci_bus_save_and_disable_locked(struct pci_bus *bus)
5613 {
5614 struct pci_dev *dev;
5615
5616 list_for_each_entry(dev, &bus->devices, bus_list) {
5617 pci_dev_save_and_disable(dev);
5618 if (dev->subordinate)
5619 pci_bus_save_and_disable_locked(dev->subordinate);
5620 }
5621 }
5622
5623 /*
5624 * Restore devices from top of the tree down while holding @dev mutex lock
5625 * for the entire tree. Parent bridges need to be restored before we can
5626 * get to subordinate devices.
5627 */
pci_bus_restore_locked(struct pci_bus * bus)5628 static void pci_bus_restore_locked(struct pci_bus *bus)
5629 {
5630 struct pci_dev *dev;
5631
5632 list_for_each_entry(dev, &bus->devices, bus_list) {
5633 pci_dev_restore(dev);
5634 if (dev->subordinate)
5635 pci_bus_restore_locked(dev->subordinate);
5636 }
5637 }
5638
5639 /*
5640 * Save and disable devices from the top of the tree down while holding
5641 * the @dev mutex lock for the entire tree.
5642 */
pci_slot_save_and_disable_locked(struct pci_slot * slot)5643 static void pci_slot_save_and_disable_locked(struct pci_slot *slot)
5644 {
5645 struct pci_dev *dev;
5646
5647 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
5648 if (!dev->slot || dev->slot != slot)
5649 continue;
5650 pci_dev_save_and_disable(dev);
5651 if (dev->subordinate)
5652 pci_bus_save_and_disable_locked(dev->subordinate);
5653 }
5654 }
5655
5656 /*
5657 * Restore devices from top of the tree down while holding @dev mutex lock
5658 * for the entire tree. Parent bridges need to be restored before we can
5659 * get to subordinate devices.
5660 */
pci_slot_restore_locked(struct pci_slot * slot)5661 static void pci_slot_restore_locked(struct pci_slot *slot)
5662 {
5663 struct pci_dev *dev;
5664
5665 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
5666 if (!dev->slot || dev->slot != slot)
5667 continue;
5668 pci_dev_restore(dev);
5669 if (dev->subordinate)
5670 pci_bus_restore_locked(dev->subordinate);
5671 }
5672 }
5673
pci_slot_reset(struct pci_slot * slot,bool probe)5674 static int pci_slot_reset(struct pci_slot *slot, bool probe)
5675 {
5676 int rc;
5677
5678 if (!slot || !pci_slot_resetable(slot))
5679 return -ENOTTY;
5680
5681 if (!probe)
5682 pci_slot_lock(slot);
5683
5684 might_sleep();
5685
5686 rc = pci_reset_hotplug_slot(slot->hotplug, probe);
5687
5688 if (!probe)
5689 pci_slot_unlock(slot);
5690
5691 return rc;
5692 }
5693
5694 /**
5695 * pci_probe_reset_slot - probe whether a PCI slot can be reset
5696 * @slot: PCI slot to probe
5697 *
5698 * Return 0 if slot can be reset, negative if a slot reset is not supported.
5699 */
pci_probe_reset_slot(struct pci_slot * slot)5700 int pci_probe_reset_slot(struct pci_slot *slot)
5701 {
5702 return pci_slot_reset(slot, PCI_RESET_PROBE);
5703 }
5704 EXPORT_SYMBOL_GPL(pci_probe_reset_slot);
5705
5706 /**
5707 * __pci_reset_slot - Try to reset a PCI slot
5708 * @slot: PCI slot to reset
5709 *
5710 * A PCI bus may host multiple slots, each slot may support a reset mechanism
5711 * independent of other slots. For instance, some slots may support slot power
5712 * control. In the case of a 1:1 bus to slot architecture, this function may
5713 * wrap the bus reset to avoid spurious slot related events such as hotplug.
5714 * Generally a slot reset should be attempted before a bus reset. All of the
5715 * function of the slot and any subordinate buses behind the slot are reset
5716 * through this function. PCI config space of all devices in the slot and
5717 * behind the slot is saved before and restored after reset.
5718 *
5719 * Same as above except return -EAGAIN if the slot cannot be locked
5720 */
__pci_reset_slot(struct pci_slot * slot)5721 static int __pci_reset_slot(struct pci_slot *slot)
5722 {
5723 int rc;
5724
5725 rc = pci_slot_reset(slot, PCI_RESET_PROBE);
5726 if (rc)
5727 return rc;
5728
5729 if (pci_slot_trylock(slot)) {
5730 pci_slot_save_and_disable_locked(slot);
5731 might_sleep();
5732 rc = pci_reset_hotplug_slot(slot->hotplug, PCI_RESET_DO_RESET);
5733 pci_slot_restore_locked(slot);
5734 pci_slot_unlock(slot);
5735 } else
5736 rc = -EAGAIN;
5737
5738 return rc;
5739 }
5740
pci_bus_reset(struct pci_bus * bus,bool probe)5741 static int pci_bus_reset(struct pci_bus *bus, bool probe)
5742 {
5743 int ret;
5744
5745 if (!bus->self || !pci_bus_resetable(bus))
5746 return -ENOTTY;
5747
5748 if (probe)
5749 return 0;
5750
5751 pci_bus_lock(bus);
5752
5753 might_sleep();
5754
5755 ret = pci_bridge_secondary_bus_reset(bus->self);
5756
5757 pci_bus_unlock(bus);
5758
5759 return ret;
5760 }
5761
5762 /**
5763 * pci_bus_error_reset - reset the bridge's subordinate bus
5764 * @bridge: The parent device that connects to the bus to reset
5765 *
5766 * This function will first try to reset the slots on this bus if the method is
5767 * available. If slot reset fails or is not available, this will fall back to a
5768 * secondary bus reset.
5769 */
pci_bus_error_reset(struct pci_dev * bridge)5770 int pci_bus_error_reset(struct pci_dev *bridge)
5771 {
5772 struct pci_bus *bus = bridge->subordinate;
5773 struct pci_slot *slot;
5774
5775 if (!bus)
5776 return -ENOTTY;
5777
5778 mutex_lock(&pci_slot_mutex);
5779 if (list_empty(&bus->slots))
5780 goto bus_reset;
5781
5782 list_for_each_entry(slot, &bus->slots, list)
5783 if (pci_probe_reset_slot(slot))
5784 goto bus_reset;
5785
5786 list_for_each_entry(slot, &bus->slots, list)
5787 if (pci_slot_reset(slot, PCI_RESET_DO_RESET))
5788 goto bus_reset;
5789
5790 mutex_unlock(&pci_slot_mutex);
5791 return 0;
5792 bus_reset:
5793 mutex_unlock(&pci_slot_mutex);
5794 return pci_bus_reset(bridge->subordinate, PCI_RESET_DO_RESET);
5795 }
5796
5797 /**
5798 * pci_probe_reset_bus - probe whether a PCI bus can be reset
5799 * @bus: PCI bus to probe
5800 *
5801 * Return 0 if bus can be reset, negative if a bus reset is not supported.
5802 */
pci_probe_reset_bus(struct pci_bus * bus)5803 int pci_probe_reset_bus(struct pci_bus *bus)
5804 {
5805 return pci_bus_reset(bus, PCI_RESET_PROBE);
5806 }
5807 EXPORT_SYMBOL_GPL(pci_probe_reset_bus);
5808
5809 /**
5810 * __pci_reset_bus - Try to reset a PCI bus
5811 * @bus: top level PCI bus to reset
5812 *
5813 * Same as above except return -EAGAIN if the bus cannot be locked
5814 */
__pci_reset_bus(struct pci_bus * bus)5815 static int __pci_reset_bus(struct pci_bus *bus)
5816 {
5817 int rc;
5818
5819 rc = pci_bus_reset(bus, PCI_RESET_PROBE);
5820 if (rc)
5821 return rc;
5822
5823 if (pci_bus_trylock(bus)) {
5824 pci_bus_save_and_disable_locked(bus);
5825 might_sleep();
5826 rc = pci_bridge_secondary_bus_reset(bus->self);
5827 pci_bus_restore_locked(bus);
5828 pci_bus_unlock(bus);
5829 } else
5830 rc = -EAGAIN;
5831
5832 return rc;
5833 }
5834
5835 /**
5836 * pci_reset_bus - Try to reset a PCI bus
5837 * @pdev: top level PCI device to reset via slot/bus
5838 *
5839 * Same as above except return -EAGAIN if the bus cannot be locked
5840 */
pci_reset_bus(struct pci_dev * pdev)5841 int pci_reset_bus(struct pci_dev *pdev)
5842 {
5843 return (!pci_probe_reset_slot(pdev->slot)) ?
5844 __pci_reset_slot(pdev->slot) : __pci_reset_bus(pdev->bus);
5845 }
5846 EXPORT_SYMBOL_GPL(pci_reset_bus);
5847
5848 /**
5849 * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
5850 * @dev: PCI device to query
5851 *
5852 * Returns mmrbc: maximum designed memory read count in bytes or
5853 * appropriate error value.
5854 */
pcix_get_max_mmrbc(struct pci_dev * dev)5855 int pcix_get_max_mmrbc(struct pci_dev *dev)
5856 {
5857 int cap;
5858 u32 stat;
5859
5860 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
5861 if (!cap)
5862 return -EINVAL;
5863
5864 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
5865 return -EINVAL;
5866
5867 return 512 << ((stat & PCI_X_STATUS_MAX_READ) >> 21);
5868 }
5869 EXPORT_SYMBOL(pcix_get_max_mmrbc);
5870
5871 /**
5872 * pcix_get_mmrbc - get PCI-X maximum memory read byte count
5873 * @dev: PCI device to query
5874 *
5875 * Returns mmrbc: maximum memory read count in bytes or appropriate error
5876 * value.
5877 */
pcix_get_mmrbc(struct pci_dev * dev)5878 int pcix_get_mmrbc(struct pci_dev *dev)
5879 {
5880 int cap;
5881 u16 cmd;
5882
5883 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
5884 if (!cap)
5885 return -EINVAL;
5886
5887 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
5888 return -EINVAL;
5889
5890 return 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
5891 }
5892 EXPORT_SYMBOL(pcix_get_mmrbc);
5893
5894 /**
5895 * pcix_set_mmrbc - set PCI-X maximum memory read byte count
5896 * @dev: PCI device to query
5897 * @mmrbc: maximum memory read count in bytes
5898 * valid values are 512, 1024, 2048, 4096
5899 *
5900 * If possible sets maximum memory read byte count, some bridges have errata
5901 * that prevent this.
5902 */
pcix_set_mmrbc(struct pci_dev * dev,int mmrbc)5903 int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
5904 {
5905 int cap;
5906 u32 stat, v, o;
5907 u16 cmd;
5908
5909 if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
5910 return -EINVAL;
5911
5912 v = ffs(mmrbc) - 10;
5913
5914 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
5915 if (!cap)
5916 return -EINVAL;
5917
5918 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
5919 return -EINVAL;
5920
5921 if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
5922 return -E2BIG;
5923
5924 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
5925 return -EINVAL;
5926
5927 o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
5928 if (o != v) {
5929 if (v > o && (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
5930 return -EIO;
5931
5932 cmd &= ~PCI_X_CMD_MAX_READ;
5933 cmd |= v << 2;
5934 if (pci_write_config_word(dev, cap + PCI_X_CMD, cmd))
5935 return -EIO;
5936 }
5937 return 0;
5938 }
5939 EXPORT_SYMBOL(pcix_set_mmrbc);
5940
5941 /**
5942 * pcie_get_readrq - get PCI Express read request size
5943 * @dev: PCI device to query
5944 *
5945 * Returns maximum memory read request in bytes or appropriate error value.
5946 */
pcie_get_readrq(struct pci_dev * dev)5947 int pcie_get_readrq(struct pci_dev *dev)
5948 {
5949 u16 ctl;
5950
5951 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
5952
5953 return 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
5954 }
5955 EXPORT_SYMBOL(pcie_get_readrq);
5956
5957 /**
5958 * pcie_set_readrq - set PCI Express maximum memory read request
5959 * @dev: PCI device to query
5960 * @rq: maximum memory read count in bytes
5961 * valid values are 128, 256, 512, 1024, 2048, 4096
5962 *
5963 * If possible sets maximum memory read request in bytes
5964 */
pcie_set_readrq(struct pci_dev * dev,int rq)5965 int pcie_set_readrq(struct pci_dev *dev, int rq)
5966 {
5967 u16 v;
5968 int ret;
5969 struct pci_host_bridge *bridge = pci_find_host_bridge(dev->bus);
5970
5971 if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
5972 return -EINVAL;
5973
5974 /*
5975 * If using the "performance" PCIe config, we clamp the read rq
5976 * size to the max packet size to keep the host bridge from
5977 * generating requests larger than we can cope with.
5978 */
5979 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
5980 int mps = pcie_get_mps(dev);
5981
5982 if (mps < rq)
5983 rq = mps;
5984 }
5985
5986 v = (ffs(rq) - 8) << 12;
5987
5988 if (bridge->no_inc_mrrs) {
5989 int max_mrrs = pcie_get_readrq(dev);
5990
5991 if (rq > max_mrrs) {
5992 pci_info(dev, "can't set Max_Read_Request_Size to %d; max is %d\n", rq, max_mrrs);
5993 return -EINVAL;
5994 }
5995 }
5996
5997 ret = pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
5998 PCI_EXP_DEVCTL_READRQ, v);
5999
6000 return pcibios_err_to_errno(ret);
6001 }
6002 EXPORT_SYMBOL(pcie_set_readrq);
6003
6004 /**
6005 * pcie_get_mps - get PCI Express maximum payload size
6006 * @dev: PCI device to query
6007 *
6008 * Returns maximum payload size in bytes
6009 */
pcie_get_mps(struct pci_dev * dev)6010 int pcie_get_mps(struct pci_dev *dev)
6011 {
6012 u16 ctl;
6013
6014 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
6015
6016 return 128 << ((ctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
6017 }
6018 EXPORT_SYMBOL(pcie_get_mps);
6019
6020 /**
6021 * pcie_set_mps - set PCI Express maximum payload size
6022 * @dev: PCI device to query
6023 * @mps: maximum payload size in bytes
6024 * valid values are 128, 256, 512, 1024, 2048, 4096
6025 *
6026 * If possible sets maximum payload size
6027 */
pcie_set_mps(struct pci_dev * dev,int mps)6028 int pcie_set_mps(struct pci_dev *dev, int mps)
6029 {
6030 u16 v;
6031 int ret;
6032
6033 if (mps < 128 || mps > 4096 || !is_power_of_2(mps))
6034 return -EINVAL;
6035
6036 v = ffs(mps) - 8;
6037 if (v > dev->pcie_mpss)
6038 return -EINVAL;
6039 v <<= 5;
6040
6041 ret = pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
6042 PCI_EXP_DEVCTL_PAYLOAD, v);
6043
6044 return pcibios_err_to_errno(ret);
6045 }
6046 EXPORT_SYMBOL(pcie_set_mps);
6047
6048 /**
6049 * pcie_bandwidth_available - determine minimum link settings of a PCIe
6050 * device and its bandwidth limitation
6051 * @dev: PCI device to query
6052 * @limiting_dev: storage for device causing the bandwidth limitation
6053 * @speed: storage for speed of limiting device
6054 * @width: storage for width of limiting device
6055 *
6056 * Walk up the PCI device chain and find the point where the minimum
6057 * bandwidth is available. Return the bandwidth available there and (if
6058 * limiting_dev, speed, and width pointers are supplied) information about
6059 * that point. The bandwidth returned is in Mb/s, i.e., megabits/second of
6060 * raw bandwidth.
6061 */
pcie_bandwidth_available(struct pci_dev * dev,struct pci_dev ** limiting_dev,enum pci_bus_speed * speed,enum pcie_link_width * width)6062 u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,
6063 enum pci_bus_speed *speed,
6064 enum pcie_link_width *width)
6065 {
6066 u16 lnksta;
6067 enum pci_bus_speed next_speed;
6068 enum pcie_link_width next_width;
6069 u32 bw, next_bw;
6070
6071 if (speed)
6072 *speed = PCI_SPEED_UNKNOWN;
6073 if (width)
6074 *width = PCIE_LNK_WIDTH_UNKNOWN;
6075
6076 bw = 0;
6077
6078 while (dev) {
6079 pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
6080
6081 next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
6082 next_width = FIELD_GET(PCI_EXP_LNKSTA_NLW, lnksta);
6083
6084 next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
6085
6086 /* Check if current device limits the total bandwidth */
6087 if (!bw || next_bw <= bw) {
6088 bw = next_bw;
6089
6090 if (limiting_dev)
6091 *limiting_dev = dev;
6092 if (speed)
6093 *speed = next_speed;
6094 if (width)
6095 *width = next_width;
6096 }
6097
6098 dev = pci_upstream_bridge(dev);
6099 }
6100
6101 return bw;
6102 }
6103 EXPORT_SYMBOL(pcie_bandwidth_available);
6104
6105 /**
6106 * pcie_get_speed_cap - query for the PCI device's link speed capability
6107 * @dev: PCI device to query
6108 *
6109 * Query the PCI device speed capability. Return the maximum link speed
6110 * supported by the device.
6111 */
pcie_get_speed_cap(struct pci_dev * dev)6112 enum pci_bus_speed pcie_get_speed_cap(struct pci_dev *dev)
6113 {
6114 u32 lnkcap2, lnkcap;
6115
6116 /*
6117 * Link Capabilities 2 was added in PCIe r3.0, sec 7.8.18. The
6118 * implementation note there recommends using the Supported Link
6119 * Speeds Vector in Link Capabilities 2 when supported.
6120 *
6121 * Without Link Capabilities 2, i.e., prior to PCIe r3.0, software
6122 * should use the Supported Link Speeds field in Link Capabilities,
6123 * where only 2.5 GT/s and 5.0 GT/s speeds were defined.
6124 */
6125 pcie_capability_read_dword(dev, PCI_EXP_LNKCAP2, &lnkcap2);
6126
6127 /* PCIe r3.0-compliant */
6128 if (lnkcap2)
6129 return PCIE_LNKCAP2_SLS2SPEED(lnkcap2);
6130
6131 pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
6132 if ((lnkcap & PCI_EXP_LNKCAP_SLS) == PCI_EXP_LNKCAP_SLS_5_0GB)
6133 return PCIE_SPEED_5_0GT;
6134 else if ((lnkcap & PCI_EXP_LNKCAP_SLS) == PCI_EXP_LNKCAP_SLS_2_5GB)
6135 return PCIE_SPEED_2_5GT;
6136
6137 return PCI_SPEED_UNKNOWN;
6138 }
6139 EXPORT_SYMBOL(pcie_get_speed_cap);
6140
6141 /**
6142 * pcie_get_width_cap - query for the PCI device's link width capability
6143 * @dev: PCI device to query
6144 *
6145 * Query the PCI device width capability. Return the maximum link width
6146 * supported by the device.
6147 */
pcie_get_width_cap(struct pci_dev * dev)6148 enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev)
6149 {
6150 u32 lnkcap;
6151
6152 pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
6153 if (lnkcap)
6154 return FIELD_GET(PCI_EXP_LNKCAP_MLW, lnkcap);
6155
6156 return PCIE_LNK_WIDTH_UNKNOWN;
6157 }
6158 EXPORT_SYMBOL(pcie_get_width_cap);
6159
6160 /**
6161 * pcie_bandwidth_capable - calculate a PCI device's link bandwidth capability
6162 * @dev: PCI device
6163 * @speed: storage for link speed
6164 * @width: storage for link width
6165 *
6166 * Calculate a PCI device's link bandwidth by querying for its link speed
6167 * and width, multiplying them, and applying encoding overhead. The result
6168 * is in Mb/s, i.e., megabits/second of raw bandwidth.
6169 */
pcie_bandwidth_capable(struct pci_dev * dev,enum pci_bus_speed * speed,enum pcie_link_width * width)6170 u32 pcie_bandwidth_capable(struct pci_dev *dev, enum pci_bus_speed *speed,
6171 enum pcie_link_width *width)
6172 {
6173 *speed = pcie_get_speed_cap(dev);
6174 *width = pcie_get_width_cap(dev);
6175
6176 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
6177 return 0;
6178
6179 return *width * PCIE_SPEED2MBS_ENC(*speed);
6180 }
6181
6182 /**
6183 * __pcie_print_link_status - Report the PCI device's link speed and width
6184 * @dev: PCI device to query
6185 * @verbose: Print info even when enough bandwidth is available
6186 *
6187 * If the available bandwidth at the device is less than the device is
6188 * capable of, report the device's maximum possible bandwidth and the
6189 * upstream link that limits its performance. If @verbose, always print
6190 * the available bandwidth, even if the device isn't constrained.
6191 */
__pcie_print_link_status(struct pci_dev * dev,bool verbose)6192 void __pcie_print_link_status(struct pci_dev *dev, bool verbose)
6193 {
6194 enum pcie_link_width width, width_cap;
6195 enum pci_bus_speed speed, speed_cap;
6196 struct pci_dev *limiting_dev = NULL;
6197 u32 bw_avail, bw_cap;
6198
6199 bw_cap = pcie_bandwidth_capable(dev, &speed_cap, &width_cap);
6200 bw_avail = pcie_bandwidth_available(dev, &limiting_dev, &speed, &width);
6201
6202 if (bw_avail >= bw_cap && verbose)
6203 pci_info(dev, "%u.%03u Gb/s available PCIe bandwidth (%s x%d link)\n",
6204 bw_cap / 1000, bw_cap % 1000,
6205 pci_speed_string(speed_cap), width_cap);
6206 else if (bw_avail < bw_cap)
6207 pci_info(dev, "%u.%03u Gb/s available PCIe bandwidth, limited by %s x%d link at %s (capable of %u.%03u Gb/s with %s x%d link)\n",
6208 bw_avail / 1000, bw_avail % 1000,
6209 pci_speed_string(speed), width,
6210 limiting_dev ? pci_name(limiting_dev) : "<unknown>",
6211 bw_cap / 1000, bw_cap % 1000,
6212 pci_speed_string(speed_cap), width_cap);
6213 }
6214
6215 /**
6216 * pcie_print_link_status - Report the PCI device's link speed and width
6217 * @dev: PCI device to query
6218 *
6219 * Report the available bandwidth at the device.
6220 */
pcie_print_link_status(struct pci_dev * dev)6221 void pcie_print_link_status(struct pci_dev *dev)
6222 {
6223 __pcie_print_link_status(dev, true);
6224 }
6225 EXPORT_SYMBOL(pcie_print_link_status);
6226
6227 /**
6228 * pci_select_bars - Make BAR mask from the type of resource
6229 * @dev: the PCI device for which BAR mask is made
6230 * @flags: resource type mask to be selected
6231 *
6232 * This helper routine makes bar mask from the type of resource.
6233 */
pci_select_bars(struct pci_dev * dev,unsigned long flags)6234 int pci_select_bars(struct pci_dev *dev, unsigned long flags)
6235 {
6236 int i, bars = 0;
6237 for (i = 0; i < PCI_NUM_RESOURCES; i++)
6238 if (pci_resource_flags(dev, i) & flags)
6239 bars |= (1 << i);
6240 return bars;
6241 }
6242 EXPORT_SYMBOL(pci_select_bars);
6243
6244 /* Some architectures require additional programming to enable VGA */
6245 static arch_set_vga_state_t arch_set_vga_state;
6246
pci_register_set_vga_state(arch_set_vga_state_t func)6247 void __init pci_register_set_vga_state(arch_set_vga_state_t func)
6248 {
6249 arch_set_vga_state = func; /* NULL disables */
6250 }
6251
pci_set_vga_state_arch(struct pci_dev * dev,bool decode,unsigned int command_bits,u32 flags)6252 static int pci_set_vga_state_arch(struct pci_dev *dev, bool decode,
6253 unsigned int command_bits, u32 flags)
6254 {
6255 if (arch_set_vga_state)
6256 return arch_set_vga_state(dev, decode, command_bits,
6257 flags);
6258 return 0;
6259 }
6260
6261 /**
6262 * pci_set_vga_state - set VGA decode state on device and parents if requested
6263 * @dev: the PCI device
6264 * @decode: true = enable decoding, false = disable decoding
6265 * @command_bits: PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY
6266 * @flags: traverse ancestors and change bridges
6267 * CHANGE_BRIDGE_ONLY / CHANGE_BRIDGE
6268 */
pci_set_vga_state(struct pci_dev * dev,bool decode,unsigned int command_bits,u32 flags)6269 int pci_set_vga_state(struct pci_dev *dev, bool decode,
6270 unsigned int command_bits, u32 flags)
6271 {
6272 struct pci_bus *bus;
6273 struct pci_dev *bridge;
6274 u16 cmd;
6275 int rc;
6276
6277 WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) && (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY)));
6278
6279 /* ARCH specific VGA enables */
6280 rc = pci_set_vga_state_arch(dev, decode, command_bits, flags);
6281 if (rc)
6282 return rc;
6283
6284 if (flags & PCI_VGA_STATE_CHANGE_DECODES) {
6285 pci_read_config_word(dev, PCI_COMMAND, &cmd);
6286 if (decode)
6287 cmd |= command_bits;
6288 else
6289 cmd &= ~command_bits;
6290 pci_write_config_word(dev, PCI_COMMAND, cmd);
6291 }
6292
6293 if (!(flags & PCI_VGA_STATE_CHANGE_BRIDGE))
6294 return 0;
6295
6296 bus = dev->bus;
6297 while (bus) {
6298 bridge = bus->self;
6299 if (bridge) {
6300 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
6301 &cmd);
6302 if (decode)
6303 cmd |= PCI_BRIDGE_CTL_VGA;
6304 else
6305 cmd &= ~PCI_BRIDGE_CTL_VGA;
6306 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL,
6307 cmd);
6308 }
6309 bus = bus->parent;
6310 }
6311 return 0;
6312 }
6313
6314 #ifdef CONFIG_ACPI
pci_pr3_present(struct pci_dev * pdev)6315 bool pci_pr3_present(struct pci_dev *pdev)
6316 {
6317 struct acpi_device *adev;
6318
6319 if (acpi_disabled)
6320 return false;
6321
6322 adev = ACPI_COMPANION(&pdev->dev);
6323 if (!adev)
6324 return false;
6325
6326 return adev->power.flags.power_resources &&
6327 acpi_has_method(adev->handle, "_PR3");
6328 }
6329 EXPORT_SYMBOL_GPL(pci_pr3_present);
6330 #endif
6331
6332 /**
6333 * pci_add_dma_alias - Add a DMA devfn alias for a device
6334 * @dev: the PCI device for which alias is added
6335 * @devfn_from: alias slot and function
6336 * @nr_devfns: number of subsequent devfns to alias
6337 *
6338 * This helper encodes an 8-bit devfn as a bit number in dma_alias_mask
6339 * which is used to program permissible bus-devfn source addresses for DMA
6340 * requests in an IOMMU. These aliases factor into IOMMU group creation
6341 * and are useful for devices generating DMA requests beyond or different
6342 * from their logical bus-devfn. Examples include device quirks where the
6343 * device simply uses the wrong devfn, as well as non-transparent bridges
6344 * where the alias may be a proxy for devices in another domain.
6345 *
6346 * IOMMU group creation is performed during device discovery or addition,
6347 * prior to any potential DMA mapping and therefore prior to driver probing
6348 * (especially for userspace assigned devices where IOMMU group definition
6349 * cannot be left as a userspace activity). DMA aliases should therefore
6350 * be configured via quirks, such as the PCI fixup header quirk.
6351 */
pci_add_dma_alias(struct pci_dev * dev,u8 devfn_from,unsigned nr_devfns)6352 void pci_add_dma_alias(struct pci_dev *dev, u8 devfn_from, unsigned nr_devfns)
6353 {
6354 int devfn_to;
6355
6356 nr_devfns = min(nr_devfns, (unsigned) MAX_NR_DEVFNS - devfn_from);
6357 devfn_to = devfn_from + nr_devfns - 1;
6358
6359 if (!dev->dma_alias_mask)
6360 dev->dma_alias_mask = bitmap_zalloc(MAX_NR_DEVFNS, GFP_KERNEL);
6361 if (!dev->dma_alias_mask) {
6362 pci_warn(dev, "Unable to allocate DMA alias mask\n");
6363 return;
6364 }
6365
6366 bitmap_set(dev->dma_alias_mask, devfn_from, nr_devfns);
6367
6368 if (nr_devfns == 1)
6369 pci_info(dev, "Enabling fixed DMA alias to %02x.%d\n",
6370 PCI_SLOT(devfn_from), PCI_FUNC(devfn_from));
6371 else if (nr_devfns > 1)
6372 pci_info(dev, "Enabling fixed DMA alias for devfn range from %02x.%d to %02x.%d\n",
6373 PCI_SLOT(devfn_from), PCI_FUNC(devfn_from),
6374 PCI_SLOT(devfn_to), PCI_FUNC(devfn_to));
6375 }
6376
pci_devs_are_dma_aliases(struct pci_dev * dev1,struct pci_dev * dev2)6377 bool pci_devs_are_dma_aliases(struct pci_dev *dev1, struct pci_dev *dev2)
6378 {
6379 return (dev1->dma_alias_mask &&
6380 test_bit(dev2->devfn, dev1->dma_alias_mask)) ||
6381 (dev2->dma_alias_mask &&
6382 test_bit(dev1->devfn, dev2->dma_alias_mask)) ||
6383 pci_real_dma_dev(dev1) == dev2 ||
6384 pci_real_dma_dev(dev2) == dev1;
6385 }
6386
pci_device_is_present(struct pci_dev * pdev)6387 bool pci_device_is_present(struct pci_dev *pdev)
6388 {
6389 u32 v;
6390
6391 /* Check PF if pdev is a VF, since VF Vendor/Device IDs are 0xffff */
6392 pdev = pci_physfn(pdev);
6393 if (pci_dev_is_disconnected(pdev))
6394 return false;
6395 return pci_bus_read_dev_vendor_id(pdev->bus, pdev->devfn, &v, 0);
6396 }
6397 EXPORT_SYMBOL_GPL(pci_device_is_present);
6398
pci_ignore_hotplug(struct pci_dev * dev)6399 void pci_ignore_hotplug(struct pci_dev *dev)
6400 {
6401 struct pci_dev *bridge = dev->bus->self;
6402
6403 dev->ignore_hotplug = 1;
6404 /* Propagate the "ignore hotplug" setting to the parent bridge. */
6405 if (bridge)
6406 bridge->ignore_hotplug = 1;
6407 }
6408 EXPORT_SYMBOL_GPL(pci_ignore_hotplug);
6409
6410 /**
6411 * pci_real_dma_dev - Get PCI DMA device for PCI device
6412 * @dev: the PCI device that may have a PCI DMA alias
6413 *
6414 * Permits the platform to provide architecture-specific functionality to
6415 * devices needing to alias DMA to another PCI device on another PCI bus. If
6416 * the PCI device is on the same bus, it is recommended to use
6417 * pci_add_dma_alias(). This is the default implementation. Architecture
6418 * implementations can override this.
6419 */
pci_real_dma_dev(struct pci_dev * dev)6420 struct pci_dev __weak *pci_real_dma_dev(struct pci_dev *dev)
6421 {
6422 return dev;
6423 }
6424
pcibios_default_alignment(void)6425 resource_size_t __weak pcibios_default_alignment(void)
6426 {
6427 return 0;
6428 }
6429
6430 /*
6431 * Arches that don't want to expose struct resource to userland as-is in
6432 * sysfs and /proc can implement their own pci_resource_to_user().
6433 */
pci_resource_to_user(const struct pci_dev * dev,int bar,const struct resource * rsrc,resource_size_t * start,resource_size_t * end)6434 void __weak pci_resource_to_user(const struct pci_dev *dev, int bar,
6435 const struct resource *rsrc,
6436 resource_size_t *start, resource_size_t *end)
6437 {
6438 *start = rsrc->start;
6439 *end = rsrc->end;
6440 }
6441
6442 static char *resource_alignment_param;
6443 static DEFINE_SPINLOCK(resource_alignment_lock);
6444
6445 /**
6446 * pci_specified_resource_alignment - get resource alignment specified by user.
6447 * @dev: the PCI device to get
6448 * @resize: whether or not to change resources' size when reassigning alignment
6449 *
6450 * RETURNS: Resource alignment if it is specified.
6451 * Zero if it is not specified.
6452 */
pci_specified_resource_alignment(struct pci_dev * dev,bool * resize)6453 static resource_size_t pci_specified_resource_alignment(struct pci_dev *dev,
6454 bool *resize)
6455 {
6456 int align_order, count;
6457 resource_size_t align = pcibios_default_alignment();
6458 const char *p;
6459 int ret;
6460
6461 spin_lock(&resource_alignment_lock);
6462 p = resource_alignment_param;
6463 if (!p || !*p)
6464 goto out;
6465 if (pci_has_flag(PCI_PROBE_ONLY)) {
6466 align = 0;
6467 pr_info_once("PCI: Ignoring requested alignments (PCI_PROBE_ONLY)\n");
6468 goto out;
6469 }
6470
6471 while (*p) {
6472 count = 0;
6473 if (sscanf(p, "%d%n", &align_order, &count) == 1 &&
6474 p[count] == '@') {
6475 p += count + 1;
6476 if (align_order > 63) {
6477 pr_err("PCI: Invalid requested alignment (order %d)\n",
6478 align_order);
6479 align_order = PAGE_SHIFT;
6480 }
6481 } else {
6482 align_order = PAGE_SHIFT;
6483 }
6484
6485 ret = pci_dev_str_match(dev, p, &p);
6486 if (ret == 1) {
6487 *resize = true;
6488 align = 1ULL << align_order;
6489 break;
6490 } else if (ret < 0) {
6491 pr_err("PCI: Can't parse resource_alignment parameter: %s\n",
6492 p);
6493 break;
6494 }
6495
6496 if (*p != ';' && *p != ',') {
6497 /* End of param or invalid format */
6498 break;
6499 }
6500 p++;
6501 }
6502 out:
6503 spin_unlock(&resource_alignment_lock);
6504 return align;
6505 }
6506
pci_request_resource_alignment(struct pci_dev * dev,int bar,resource_size_t align,bool resize)6507 static void pci_request_resource_alignment(struct pci_dev *dev, int bar,
6508 resource_size_t align, bool resize)
6509 {
6510 struct resource *r = &dev->resource[bar];
6511 resource_size_t size;
6512
6513 if (!(r->flags & IORESOURCE_MEM))
6514 return;
6515
6516 if (r->flags & IORESOURCE_PCI_FIXED) {
6517 pci_info(dev, "BAR%d %pR: ignoring requested alignment %#llx\n",
6518 bar, r, (unsigned long long)align);
6519 return;
6520 }
6521
6522 size = resource_size(r);
6523 if (size >= align)
6524 return;
6525
6526 /*
6527 * Increase the alignment of the resource. There are two ways we
6528 * can do this:
6529 *
6530 * 1) Increase the size of the resource. BARs are aligned on their
6531 * size, so when we reallocate space for this resource, we'll
6532 * allocate it with the larger alignment. This also prevents
6533 * assignment of any other BARs inside the alignment region, so
6534 * if we're requesting page alignment, this means no other BARs
6535 * will share the page.
6536 *
6537 * The disadvantage is that this makes the resource larger than
6538 * the hardware BAR, which may break drivers that compute things
6539 * based on the resource size, e.g., to find registers at a
6540 * fixed offset before the end of the BAR.
6541 *
6542 * 2) Retain the resource size, but use IORESOURCE_STARTALIGN and
6543 * set r->start to the desired alignment. By itself this
6544 * doesn't prevent other BARs being put inside the alignment
6545 * region, but if we realign *every* resource of every device in
6546 * the system, none of them will share an alignment region.
6547 *
6548 * When the user has requested alignment for only some devices via
6549 * the "pci=resource_alignment" argument, "resize" is true and we
6550 * use the first method. Otherwise we assume we're aligning all
6551 * devices and we use the second.
6552 */
6553
6554 pci_info(dev, "BAR%d %pR: requesting alignment to %#llx\n",
6555 bar, r, (unsigned long long)align);
6556
6557 if (resize) {
6558 r->start = 0;
6559 r->end = align - 1;
6560 } else {
6561 r->flags &= ~IORESOURCE_SIZEALIGN;
6562 r->flags |= IORESOURCE_STARTALIGN;
6563 r->start = align;
6564 r->end = r->start + size - 1;
6565 }
6566 r->flags |= IORESOURCE_UNSET;
6567 }
6568
6569 /*
6570 * This function disables memory decoding and releases memory resources
6571 * of the device specified by kernel's boot parameter 'pci=resource_alignment='.
6572 * It also rounds up size to specified alignment.
6573 * Later on, the kernel will assign page-aligned memory resource back
6574 * to the device.
6575 */
pci_reassigndev_resource_alignment(struct pci_dev * dev)6576 void pci_reassigndev_resource_alignment(struct pci_dev *dev)
6577 {
6578 int i;
6579 struct resource *r;
6580 resource_size_t align;
6581 u16 command;
6582 bool resize = false;
6583
6584 /*
6585 * VF BARs are read-only zero according to SR-IOV spec r1.1, sec
6586 * 3.4.1.11. Their resources are allocated from the space
6587 * described by the VF BARx register in the PF's SR-IOV capability.
6588 * We can't influence their alignment here.
6589 */
6590 if (dev->is_virtfn)
6591 return;
6592
6593 /* check if specified PCI is target device to reassign */
6594 align = pci_specified_resource_alignment(dev, &resize);
6595 if (!align)
6596 return;
6597
6598 if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL &&
6599 (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) {
6600 pci_warn(dev, "Can't reassign resources to host bridge\n");
6601 return;
6602 }
6603
6604 pci_read_config_word(dev, PCI_COMMAND, &command);
6605 command &= ~PCI_COMMAND_MEMORY;
6606 pci_write_config_word(dev, PCI_COMMAND, command);
6607
6608 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
6609 pci_request_resource_alignment(dev, i, align, resize);
6610
6611 /*
6612 * Need to disable bridge's resource window,
6613 * to enable the kernel to reassign new resource
6614 * window later on.
6615 */
6616 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
6617 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
6618 r = &dev->resource[i];
6619 if (!(r->flags & IORESOURCE_MEM))
6620 continue;
6621 r->flags |= IORESOURCE_UNSET;
6622 r->end = resource_size(r) - 1;
6623 r->start = 0;
6624 }
6625 pci_disable_bridge_window(dev);
6626 }
6627 }
6628
resource_alignment_show(struct bus_type * bus,char * buf)6629 static ssize_t resource_alignment_show(struct bus_type *bus, char *buf)
6630 {
6631 size_t count = 0;
6632
6633 spin_lock(&resource_alignment_lock);
6634 if (resource_alignment_param)
6635 count = sysfs_emit(buf, "%s\n", resource_alignment_param);
6636 spin_unlock(&resource_alignment_lock);
6637
6638 return count;
6639 }
6640
resource_alignment_store(struct bus_type * bus,const char * buf,size_t count)6641 static ssize_t resource_alignment_store(struct bus_type *bus,
6642 const char *buf, size_t count)
6643 {
6644 char *param, *old, *end;
6645
6646 if (count >= (PAGE_SIZE - 1))
6647 return -EINVAL;
6648
6649 param = kstrndup(buf, count, GFP_KERNEL);
6650 if (!param)
6651 return -ENOMEM;
6652
6653 end = strchr(param, '\n');
6654 if (end)
6655 *end = '\0';
6656
6657 spin_lock(&resource_alignment_lock);
6658 old = resource_alignment_param;
6659 if (strlen(param)) {
6660 resource_alignment_param = param;
6661 } else {
6662 kfree(param);
6663 resource_alignment_param = NULL;
6664 }
6665 spin_unlock(&resource_alignment_lock);
6666
6667 kfree(old);
6668
6669 return count;
6670 }
6671
6672 static BUS_ATTR_RW(resource_alignment);
6673
pci_resource_alignment_sysfs_init(void)6674 static int __init pci_resource_alignment_sysfs_init(void)
6675 {
6676 return bus_create_file(&pci_bus_type,
6677 &bus_attr_resource_alignment);
6678 }
6679 late_initcall(pci_resource_alignment_sysfs_init);
6680
pci_no_domains(void)6681 static void pci_no_domains(void)
6682 {
6683 #ifdef CONFIG_PCI_DOMAINS
6684 pci_domains_supported = 0;
6685 #endif
6686 }
6687
6688 #ifdef CONFIG_PCI_DOMAINS_GENERIC
6689 static atomic_t __domain_nr = ATOMIC_INIT(-1);
6690
pci_get_new_domain_nr(void)6691 static int pci_get_new_domain_nr(void)
6692 {
6693 return atomic_inc_return(&__domain_nr);
6694 }
6695
of_pci_bus_find_domain_nr(struct device * parent)6696 static int of_pci_bus_find_domain_nr(struct device *parent)
6697 {
6698 static int use_dt_domains = -1;
6699 int domain = -1;
6700
6701 if (parent)
6702 domain = of_get_pci_domain_nr(parent->of_node);
6703
6704 /*
6705 * Check DT domain and use_dt_domains values.
6706 *
6707 * If DT domain property is valid (domain >= 0) and
6708 * use_dt_domains != 0, the DT assignment is valid since this means
6709 * we have not previously allocated a domain number by using
6710 * pci_get_new_domain_nr(); we should also update use_dt_domains to
6711 * 1, to indicate that we have just assigned a domain number from
6712 * DT.
6713 *
6714 * If DT domain property value is not valid (ie domain < 0), and we
6715 * have not previously assigned a domain number from DT
6716 * (use_dt_domains != 1) we should assign a domain number by
6717 * using the:
6718 *
6719 * pci_get_new_domain_nr()
6720 *
6721 * API and update the use_dt_domains value to keep track of method we
6722 * are using to assign domain numbers (use_dt_domains = 0).
6723 *
6724 * All other combinations imply we have a platform that is trying
6725 * to mix domain numbers obtained from DT and pci_get_new_domain_nr(),
6726 * which is a recipe for domain mishandling and it is prevented by
6727 * invalidating the domain value (domain = -1) and printing a
6728 * corresponding error.
6729 */
6730 if (domain >= 0 && use_dt_domains) {
6731 use_dt_domains = 1;
6732 } else if (domain < 0 && use_dt_domains != 1) {
6733 use_dt_domains = 0;
6734 domain = pci_get_new_domain_nr();
6735 } else {
6736 if (parent)
6737 pr_err("Node %pOF has ", parent->of_node);
6738 pr_err("Inconsistent \"linux,pci-domain\" property in DT\n");
6739 domain = -1;
6740 }
6741
6742 return domain;
6743 }
6744
pci_bus_find_domain_nr(struct pci_bus * bus,struct device * parent)6745 int pci_bus_find_domain_nr(struct pci_bus *bus, struct device *parent)
6746 {
6747 return acpi_disabled ? of_pci_bus_find_domain_nr(parent) :
6748 acpi_pci_bus_find_domain_nr(bus);
6749 }
6750 #endif
6751
6752 /**
6753 * pci_ext_cfg_avail - can we access extended PCI config space?
6754 *
6755 * Returns 1 if we can access PCI extended config space (offsets
6756 * greater than 0xff). This is the default implementation. Architecture
6757 * implementations can override this.
6758 */
pci_ext_cfg_avail(void)6759 int __weak pci_ext_cfg_avail(void)
6760 {
6761 return 1;
6762 }
6763
pci_fixup_cardbus(struct pci_bus * bus)6764 void __weak pci_fixup_cardbus(struct pci_bus *bus)
6765 {
6766 }
6767 EXPORT_SYMBOL(pci_fixup_cardbus);
6768
pci_setup(char * str)6769 static int __init pci_setup(char *str)
6770 {
6771 while (str) {
6772 char *k = strchr(str, ',');
6773 if (k)
6774 *k++ = 0;
6775 if (*str && (str = pcibios_setup(str)) && *str) {
6776 if (!strcmp(str, "nomsi")) {
6777 pci_no_msi();
6778 } else if (!strncmp(str, "noats", 5)) {
6779 pr_info("PCIe: ATS is disabled\n");
6780 pcie_ats_disabled = true;
6781 } else if (!strcmp(str, "noaer")) {
6782 pci_no_aer();
6783 } else if (!strcmp(str, "earlydump")) {
6784 pci_early_dump = true;
6785 } else if (!strncmp(str, "realloc=", 8)) {
6786 pci_realloc_get_opt(str + 8);
6787 } else if (!strncmp(str, "realloc", 7)) {
6788 pci_realloc_get_opt("on");
6789 } else if (!strcmp(str, "nodomains")) {
6790 pci_no_domains();
6791 } else if (!strncmp(str, "noari", 5)) {
6792 pcie_ari_disabled = true;
6793 } else if (!strncmp(str, "cbiosize=", 9)) {
6794 pci_cardbus_io_size = memparse(str + 9, &str);
6795 } else if (!strncmp(str, "cbmemsize=", 10)) {
6796 pci_cardbus_mem_size = memparse(str + 10, &str);
6797 } else if (!strncmp(str, "resource_alignment=", 19)) {
6798 resource_alignment_param = str + 19;
6799 } else if (!strncmp(str, "ecrc=", 5)) {
6800 pcie_ecrc_get_policy(str + 5);
6801 } else if (!strncmp(str, "hpiosize=", 9)) {
6802 pci_hotplug_io_size = memparse(str + 9, &str);
6803 } else if (!strncmp(str, "hpmmiosize=", 11)) {
6804 pci_hotplug_mmio_size = memparse(str + 11, &str);
6805 } else if (!strncmp(str, "hpmmioprefsize=", 15)) {
6806 pci_hotplug_mmio_pref_size = memparse(str + 15, &str);
6807 } else if (!strncmp(str, "hpmemsize=", 10)) {
6808 pci_hotplug_mmio_size = memparse(str + 10, &str);
6809 pci_hotplug_mmio_pref_size = pci_hotplug_mmio_size;
6810 } else if (!strncmp(str, "hpbussize=", 10)) {
6811 pci_hotplug_bus_size =
6812 simple_strtoul(str + 10, &str, 0);
6813 if (pci_hotplug_bus_size > 0xff)
6814 pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;
6815 } else if (!strncmp(str, "pcie_bus_tune_off", 17)) {
6816 pcie_bus_config = PCIE_BUS_TUNE_OFF;
6817 } else if (!strncmp(str, "pcie_bus_safe", 13)) {
6818 pcie_bus_config = PCIE_BUS_SAFE;
6819 } else if (!strncmp(str, "pcie_bus_perf", 13)) {
6820 pcie_bus_config = PCIE_BUS_PERFORMANCE;
6821 } else if (!strncmp(str, "pcie_bus_peer2peer", 18)) {
6822 pcie_bus_config = PCIE_BUS_PEER2PEER;
6823 } else if (!strncmp(str, "pcie_scan_all", 13)) {
6824 pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS);
6825 } else if (!strncmp(str, "disable_acs_redir=", 18)) {
6826 disable_acs_redir_param = str + 18;
6827 } else {
6828 pr_err("PCI: Unknown option `%s'\n", str);
6829 }
6830 }
6831 str = k;
6832 }
6833 return 0;
6834 }
6835 early_param("pci", pci_setup);
6836
6837 /*
6838 * 'resource_alignment_param' and 'disable_acs_redir_param' are initialized
6839 * in pci_setup(), above, to point to data in the __initdata section which
6840 * will be freed after the init sequence is complete. We can't allocate memory
6841 * in pci_setup() because some architectures do not have any memory allocation
6842 * service available during an early_param() call. So we allocate memory and
6843 * copy the variable here before the init section is freed.
6844 *
6845 */
pci_realloc_setup_params(void)6846 static int __init pci_realloc_setup_params(void)
6847 {
6848 resource_alignment_param = kstrdup(resource_alignment_param,
6849 GFP_KERNEL);
6850 disable_acs_redir_param = kstrdup(disable_acs_redir_param, GFP_KERNEL);
6851
6852 return 0;
6853 }
6854 pure_initcall(pci_realloc_setup_params);
6855