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