blob: 17d8de109556fe169fd880ef5ca9228ce7b7b030 [file] [log] [blame]
Bjorn Helgaas7328c8f2018-01-26 11:45:16 -06001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06003 * PCI Bus Services, see include/linux/pci.h for further explanation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06005 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
6 * David Mosberger-Tang
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06008 * Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Tomasz Nowicki2ab51dd2016-06-10 15:36:26 -050011#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
13#include <linux/delay.h>
Mika Westerberg9d26d3a2016-06-02 11:17:12 +030014#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -070016#include <linux/of.h>
17#include <linux/of_pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/pci.h>
David Brownell075c1772007-04-26 00:12:06 -070019#include <linux/pm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/spinlock.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080023#include <linux/string.h>
vignesh babu229f5af2007-08-13 18:23:14 +053024#include <linux/log2.h>
Zhichang Yuan57453922018-03-15 02:15:53 +080025#include <linux/logic_pio.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080026#include <linux/pci-aspm.h>
Stephen Rothwellc300bd2fb2008-07-10 02:16:44 +020027#include <linux/pm_wakeup.h>
Sheng Yang8dd7f802008-10-21 17:38:25 +080028#include <linux/interrupt.h>
Yuji Shimada32a9a6822009-03-16 17:13:39 +090029#include <linux/device.h>
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010030#include <linux/pm_runtime.h>
Alex Williamson608c3882013-08-08 14:09:43 -060031#include <linux/pci_hotplug.h>
Sinan Kaya4d3f1382016-06-10 21:55:11 +020032#include <linux/vmalloc.h>
CQ Tang4ebeb1e2017-05-30 09:25:49 -070033#include <linux/pci-ats.h>
Yuji Shimada32a9a6822009-03-16 17:13:39 +090034#include <asm/setup.h>
Ben Dooks2a2aca32016-06-17 16:05:13 +010035#include <asm/dma.h>
Taku Izumib07461a2015-09-17 10:09:37 -050036#include <linux/aer.h>
Greg KHbc56b9e2005-04-08 14:53:31 +090037#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Alan Stern00240c32009-04-27 13:33:16 -040039const char *pci_power_names[] = {
40 "error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown",
41};
42EXPORT_SYMBOL_GPL(pci_power_names);
43
Rafael J. Wysocki93177a72010-01-02 22:57:24 +010044int isa_dma_bridge_buggy;
45EXPORT_SYMBOL(isa_dma_bridge_buggy);
46
47int pci_pci_problems;
48EXPORT_SYMBOL(pci_pci_problems);
49
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +010050unsigned int pci_pm_d3_delay;
51
Matthew Garrettdf17e622010-10-04 14:22:29 -040052static void pci_pme_list_scan(struct work_struct *work);
53
54static LIST_HEAD(pci_pme_list);
55static DEFINE_MUTEX(pci_pme_list_mutex);
56static DECLARE_DELAYED_WORK(pci_pme_work, pci_pme_list_scan);
57
58struct pci_pme_device {
59 struct list_head list;
60 struct pci_dev *dev;
61};
62
63#define PME_TIMEOUT 1000 /* How long between PME checks */
64
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +010065static void pci_dev_d3_sleep(struct pci_dev *dev)
66{
67 unsigned int delay = dev->d3_delay;
68
69 if (delay < pci_pm_d3_delay)
70 delay = pci_pm_d3_delay;
71
Adrian Hunter50b2b542017-03-14 15:21:58 +020072 if (delay)
73 msleep(delay);
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +010074}
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Jeff Garzik32a2eea2007-10-11 16:57:27 -040076#ifdef CONFIG_PCI_DOMAINS
77int pci_domains_supported = 1;
78#endif
79
Atsushi Nemoto4516a612007-02-05 16:36:06 -080080#define DEFAULT_CARDBUS_IO_SIZE (256)
81#define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
82/* pci=cbmemsize=nnM,cbiosize=nn can override this */
83unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
84unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
85
Eric W. Biederman28760482009-09-09 14:09:24 -070086#define DEFAULT_HOTPLUG_IO_SIZE (256)
87#define DEFAULT_HOTPLUG_MEM_SIZE (2*1024*1024)
88/* pci=hpmemsize=nnM,hpiosize=nn can override this */
89unsigned long pci_hotplug_io_size = DEFAULT_HOTPLUG_IO_SIZE;
90unsigned long pci_hotplug_mem_size = DEFAULT_HOTPLUG_MEM_SIZE;
91
Keith Busche16b4662016-07-21 21:40:28 -060092#define DEFAULT_HOTPLUG_BUS_SIZE 1
93unsigned long pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;
94
Keith Busch27d868b2015-08-24 08:48:16 -050095enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_DEFAULT;
Jon Masonb03e7492011-07-20 15:20:54 -050096
Jesse Barnesac1aa472009-10-26 13:20:44 -070097/*
98 * The default CLS is used if arch didn't set CLS explicitly and not
99 * all pci devices agree on the same value. Arch can override either
100 * the dfl or actual value as it sees fit. Don't forget this is
101 * measured in 32-bit words, not bytes.
102 */
Bill Pemberton15856ad2012-11-21 15:35:00 -0500103u8 pci_dfl_cache_line_size = L1_CACHE_BYTES >> 2;
Jesse Barnesac1aa472009-10-26 13:20:44 -0700104u8 pci_cache_line_size;
105
Myron Stowe96c55902011-10-28 15:48:38 -0600106/*
107 * If we set up a device for bus mastering, we need to check the latency
108 * timer as certain BIOSes forget to set it properly.
109 */
110unsigned int pcibios_max_latency = 255;
111
Rafael J. Wysocki6748dcc2012-03-01 00:06:33 +0100112/* If set, the PCIe ARI capability will not be used. */
113static bool pcie_ari_disabled;
114
Gil Kupfercef74402018-05-10 17:56:02 -0500115/* If set, the PCIe ATS capability will not be used. */
116static bool pcie_ats_disabled;
117
118bool pci_ats_disabled(void)
119{
120 return pcie_ats_disabled;
121}
122
Mika Westerberg9d26d3a2016-06-02 11:17:12 +0300123/* Disable bridge_d3 for all PCIe ports */
124static bool pci_bridge_d3_disable;
125/* Force bridge_d3 for all PCIe ports */
126static bool pci_bridge_d3_force;
127
128static int __init pcie_port_pm_setup(char *str)
129{
130 if (!strcmp(str, "off"))
131 pci_bridge_d3_disable = true;
132 else if (!strcmp(str, "force"))
133 pci_bridge_d3_force = true;
134 return 1;
135}
136__setup("pcie_port_pm=", pcie_port_pm_setup);
137
Sinan Kayaa2758b62018-02-27 14:14:10 -0600138/* Time to wait after a reset for device to become responsive */
139#define PCIE_RESET_READY_POLL_MS 60000
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/**
142 * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
143 * @bus: pointer to PCI bus structure to search
144 *
145 * Given a PCI bus, returns the highest PCI bus number present in the set
146 * including the given PCI bus and its list of child PCI buses.
147 */
Ryan Desfosses07656d83082014-04-11 01:01:53 -0400148unsigned char pci_bus_max_busnr(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Yijing Wang94e6a9b2014-02-13 21:14:03 +0800150 struct pci_bus *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 unsigned char max, n;
152
Yinghai Lub918c622012-05-17 18:51:11 -0700153 max = bus->busn_res.end;
Yijing Wang94e6a9b2014-02-13 21:14:03 +0800154 list_for_each_entry(tmp, &bus->children, node) {
155 n = pci_bus_max_busnr(tmp);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400156 if (n > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 max = n;
158 }
159 return max;
160}
Kristen Accardib82db5c2006-01-17 16:56:56 -0800161EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Andrew Morton1684f5d2008-12-01 14:30:30 -0800163#ifdef CONFIG_HAS_IOMEM
164void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
165{
Bjorn Helgaas1f7bf3bf2015-03-12 12:30:11 -0500166 struct resource *res = &pdev->resource[bar];
167
Andrew Morton1684f5d2008-12-01 14:30:30 -0800168 /*
169 * Make sure the BAR is actually a memory resource, not an IO resource
170 */
Bjorn Helgaas646c0282015-03-12 12:30:15 -0500171 if (res->flags & IORESOURCE_UNSET || !(res->flags & IORESOURCE_MEM)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600172 pci_warn(pdev, "can't ioremap BAR %d: %pR\n", bar, res);
Andrew Morton1684f5d2008-12-01 14:30:30 -0800173 return NULL;
174 }
Bjorn Helgaas1f7bf3bf2015-03-12 12:30:11 -0500175 return ioremap_nocache(res->start, resource_size(res));
Andrew Morton1684f5d2008-12-01 14:30:30 -0800176}
177EXPORT_SYMBOL_GPL(pci_ioremap_bar);
Luis R. Rodriguezc43996f2015-08-24 12:13:23 -0700178
179void __iomem *pci_ioremap_wc_bar(struct pci_dev *pdev, int bar)
180{
181 /*
182 * Make sure the BAR is actually a memory resource, not an IO resource
183 */
184 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
185 WARN_ON(1);
186 return NULL;
187 }
188 return ioremap_wc(pci_resource_start(pdev, bar),
189 pci_resource_len(pdev, bar));
190}
191EXPORT_SYMBOL_GPL(pci_ioremap_wc_bar);
Andrew Morton1684f5d2008-12-01 14:30:30 -0800192#endif
193
Logan Gunthorpe07d8d7e2018-07-30 10:18:37 -0600194/**
Logan Gunthorpe45db3372018-07-30 10:18:38 -0600195 * pci_dev_str_match_path - test if a path string matches a device
196 * @dev: the PCI device to test
197 * @p: string to match the device against
198 * @endptr: pointer to the string after the match
199 *
200 * Test if a string (typically from a kernel parameter) formatted as a
201 * path of device/function addresses matches a PCI device. The string must
202 * be of the form:
203 *
204 * [<domain>:]<bus>:<device>.<func>[/<device>.<func>]*
205 *
206 * A path for a device can be obtained using 'lspci -t'. Using a path
207 * is more robust against bus renumbering than using only a single bus,
208 * device and function address.
209 *
210 * Returns 1 if the string matches the device, 0 if it does not and
211 * a negative error code if it fails to parse the string.
212 */
213static int pci_dev_str_match_path(struct pci_dev *dev, const char *path,
214 const char **endptr)
215{
216 int ret;
217 int seg, bus, slot, func;
218 char *wpath, *p;
219 char end;
220
221 *endptr = strchrnul(path, ';');
222
223 wpath = kmemdup_nul(path, *endptr - path, GFP_KERNEL);
224 if (!wpath)
225 return -ENOMEM;
226
227 while (1) {
228 p = strrchr(wpath, '/');
229 if (!p)
230 break;
231 ret = sscanf(p, "/%x.%x%c", &slot, &func, &end);
232 if (ret != 2) {
233 ret = -EINVAL;
234 goto free_and_exit;
235 }
236
237 if (dev->devfn != PCI_DEVFN(slot, func)) {
238 ret = 0;
239 goto free_and_exit;
240 }
241
242 /*
243 * Note: we don't need to get a reference to the upstream
244 * bridge because we hold a reference to the top level
245 * device which should hold a reference to the bridge,
246 * and so on.
247 */
248 dev = pci_upstream_bridge(dev);
249 if (!dev) {
250 ret = 0;
251 goto free_and_exit;
252 }
253
254 *p = 0;
255 }
256
257 ret = sscanf(wpath, "%x:%x:%x.%x%c", &seg, &bus, &slot,
258 &func, &end);
259 if (ret != 4) {
260 seg = 0;
261 ret = sscanf(wpath, "%x:%x.%x%c", &bus, &slot, &func, &end);
262 if (ret != 3) {
263 ret = -EINVAL;
264 goto free_and_exit;
265 }
266 }
267
268 ret = (seg == pci_domain_nr(dev->bus) &&
269 bus == dev->bus->number &&
270 dev->devfn == PCI_DEVFN(slot, func));
271
272free_and_exit:
273 kfree(wpath);
274 return ret;
275}
276
277/**
Logan Gunthorpe07d8d7e2018-07-30 10:18:37 -0600278 * pci_dev_str_match - test if a string matches a device
279 * @dev: the PCI device to test
280 * @p: string to match the device against
281 * @endptr: pointer to the string after the match
282 *
283 * Test if a string (typically from a kernel parameter) matches a specified
284 * PCI device. The string may be of one of the following formats:
285 *
Logan Gunthorpe45db3372018-07-30 10:18:38 -0600286 * [<domain>:]<bus>:<device>.<func>[/<device>.<func>]*
Logan Gunthorpe07d8d7e2018-07-30 10:18:37 -0600287 * pci:<vendor>:<device>[:<subvendor>:<subdevice>]
288 *
289 * The first format specifies a PCI bus/device/function address which
290 * may change if new hardware is inserted, if motherboard firmware changes,
291 * or due to changes caused in kernel parameters. If the domain is
Logan Gunthorpe45db3372018-07-30 10:18:38 -0600292 * left unspecified, it is taken to be 0. In order to be robust against
293 * bus renumbering issues, a path of PCI device/function numbers may be used
294 * to address the specific device. The path for a device can be determined
295 * through the use of 'lspci -t'.
Logan Gunthorpe07d8d7e2018-07-30 10:18:37 -0600296 *
297 * The second format matches devices using IDs in the configuration
298 * space which may match multiple devices in the system. A value of 0
299 * for any field will match all devices. (Note: this differs from
300 * in-kernel code that uses PCI_ANY_ID which is ~0; this is for
301 * legacy reasons and convenience so users don't have to specify
302 * FFFFFFFFs on the command line.)
303 *
304 * Returns 1 if the string matches the device, 0 if it does not and
305 * a negative error code if the string cannot be parsed.
306 */
307static int pci_dev_str_match(struct pci_dev *dev, const char *p,
308 const char **endptr)
309{
310 int ret;
Logan Gunthorpe45db3372018-07-30 10:18:38 -0600311 int count;
Logan Gunthorpe07d8d7e2018-07-30 10:18:37 -0600312 unsigned short vendor, device, subsystem_vendor, subsystem_device;
313
314 if (strncmp(p, "pci:", 4) == 0) {
315 /* PCI vendor/device (subvendor/subdevice) IDs are specified */
316 p += 4;
317 ret = sscanf(p, "%hx:%hx:%hx:%hx%n", &vendor, &device,
318 &subsystem_vendor, &subsystem_device, &count);
319 if (ret != 4) {
320 ret = sscanf(p, "%hx:%hx%n", &vendor, &device, &count);
321 if (ret != 2)
322 return -EINVAL;
323
324 subsystem_vendor = 0;
325 subsystem_device = 0;
326 }
327
328 p += count;
329
330 if ((!vendor || vendor == dev->vendor) &&
331 (!device || device == dev->device) &&
332 (!subsystem_vendor ||
333 subsystem_vendor == dev->subsystem_vendor) &&
334 (!subsystem_device ||
335 subsystem_device == dev->subsystem_device))
336 goto found;
Logan Gunthorpe07d8d7e2018-07-30 10:18:37 -0600337 } else {
Logan Gunthorpe45db3372018-07-30 10:18:38 -0600338 /*
339 * PCI Bus, Device, Function IDs are specified
340 * (optionally, may include a path of devfns following it)
341 */
342 ret = pci_dev_str_match_path(dev, p, &p);
343 if (ret < 0)
344 return ret;
345 else if (ret)
Logan Gunthorpe07d8d7e2018-07-30 10:18:37 -0600346 goto found;
347 }
348
349 *endptr = p;
350 return 0;
351
352found:
353 *endptr = p;
354 return 1;
355}
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100356
357static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
358 u8 pos, int cap, int *ttl)
Roland Dreier24a4e372005-10-28 17:35:34 -0700359{
360 u8 id;
Sean O. Stalley55db3202015-04-02 14:10:19 -0700361 u16 ent;
362
363 pci_bus_read_config_byte(bus, devfn, pos, &pos);
Roland Dreier24a4e372005-10-28 17:35:34 -0700364
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100365 while ((*ttl)--) {
Roland Dreier24a4e372005-10-28 17:35:34 -0700366 if (pos < 0x40)
367 break;
368 pos &= ~3;
Sean O. Stalley55db3202015-04-02 14:10:19 -0700369 pci_bus_read_config_word(bus, devfn, pos, &ent);
370
371 id = ent & 0xff;
Roland Dreier24a4e372005-10-28 17:35:34 -0700372 if (id == 0xff)
373 break;
374 if (id == cap)
375 return pos;
Sean O. Stalley55db3202015-04-02 14:10:19 -0700376 pos = (ent >> 8);
Roland Dreier24a4e372005-10-28 17:35:34 -0700377 }
378 return 0;
379}
380
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100381static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
382 u8 pos, int cap)
383{
384 int ttl = PCI_FIND_CAP_TTL;
385
386 return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
387}
388
Roland Dreier24a4e372005-10-28 17:35:34 -0700389int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
390{
391 return __pci_find_next_cap(dev->bus, dev->devfn,
392 pos + PCI_CAP_LIST_NEXT, cap);
393}
394EXPORT_SYMBOL_GPL(pci_find_next_capability);
395
Michael Ellermand3bac112006-11-22 18:26:16 +1100396static int __pci_bus_find_cap_start(struct pci_bus *bus,
397 unsigned int devfn, u8 hdr_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
402 if (!(status & PCI_STATUS_CAP_LIST))
403 return 0;
404
405 switch (hdr_type) {
406 case PCI_HEADER_TYPE_NORMAL:
407 case PCI_HEADER_TYPE_BRIDGE:
Michael Ellermand3bac112006-11-22 18:26:16 +1100408 return PCI_CAPABILITY_LIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 case PCI_HEADER_TYPE_CARDBUS:
Michael Ellermand3bac112006-11-22 18:26:16 +1100410 return PCI_CB_CAPABILITY_LIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Michael Ellermand3bac112006-11-22 18:26:16 +1100412
413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
416/**
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700417 * pci_find_capability - query for devices' capabilities
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 * @dev: PCI device to query
419 * @cap: capability code
420 *
421 * Tell if a device supports a given PCI capability.
422 * Returns the address of the requested capability structure within the
423 * device's PCI configuration space or 0 in case the device does not
424 * support it. Possible values for @cap:
425 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700426 * %PCI_CAP_ID_PM Power Management
427 * %PCI_CAP_ID_AGP Accelerated Graphics Port
428 * %PCI_CAP_ID_VPD Vital Product Data
429 * %PCI_CAP_ID_SLOTID Slot Identification
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * %PCI_CAP_ID_MSI Message Signalled Interrupts
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700431 * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * %PCI_CAP_ID_PCIX PCI-X
433 * %PCI_CAP_ID_EXP PCI Express
434 */
435int pci_find_capability(struct pci_dev *dev, int cap)
436{
Michael Ellermand3bac112006-11-22 18:26:16 +1100437 int pos;
438
439 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
440 if (pos)
441 pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
442
443 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600445EXPORT_SYMBOL(pci_find_capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447/**
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700448 * pci_bus_find_capability - query for devices' capabilities
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * @bus: the PCI bus to query
450 * @devfn: PCI device to query
451 * @cap: capability code
452 *
453 * Like pci_find_capability() but works for pci devices that do not have a
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700454 * pci_dev structure set up yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 *
456 * Returns the address of the requested capability structure within the
457 * device's PCI configuration space or 0 in case the device does not
458 * support it.
459 */
460int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
461{
Michael Ellermand3bac112006-11-22 18:26:16 +1100462 int pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 u8 hdr_type;
464
465 pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
466
Michael Ellermand3bac112006-11-22 18:26:16 +1100467 pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
468 if (pos)
469 pos = __pci_find_next_cap(bus, devfn, pos, cap);
470
471 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600473EXPORT_SYMBOL(pci_bus_find_capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475/**
Bjorn Helgaas44a9a362012-07-13 14:24:59 -0600476 * pci_find_next_ext_capability - Find an extended capability
477 * @dev: PCI device to query
478 * @start: address at which to start looking (0 to start at beginning of list)
479 * @cap: capability code
480 *
481 * Returns the address of the next matching extended capability structure
482 * within the device's PCI configuration space or 0 if the device does
483 * not support it. Some capabilities can occur several times, e.g., the
484 * vendor-specific capability, and this provides a way to find them all.
485 */
486int pci_find_next_ext_capability(struct pci_dev *dev, int start, int cap)
487{
488 u32 header;
489 int ttl;
490 int pos = PCI_CFG_SPACE_SIZE;
491
492 /* minimum 8 bytes per capability */
493 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
494
495 if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
496 return 0;
497
498 if (start)
499 pos = start;
500
501 if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
502 return 0;
503
504 /*
505 * If we have no capabilities, this is indicated by cap ID,
506 * cap version and next pointer all being 0.
507 */
508 if (header == 0)
509 return 0;
510
511 while (ttl-- > 0) {
512 if (PCI_EXT_CAP_ID(header) == cap && pos != start)
513 return pos;
514
515 pos = PCI_EXT_CAP_NEXT(header);
516 if (pos < PCI_CFG_SPACE_SIZE)
517 break;
518
519 if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
520 break;
521 }
522
523 return 0;
524}
525EXPORT_SYMBOL_GPL(pci_find_next_ext_capability);
526
527/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * pci_find_ext_capability - Find an extended capability
529 * @dev: PCI device to query
530 * @cap: capability code
531 *
532 * Returns the address of the requested extended capability structure
533 * within the device's PCI configuration space or 0 if the device does
534 * not support it. Possible values for @cap:
535 *
536 * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
537 * %PCI_EXT_CAP_ID_VC Virtual Channel
538 * %PCI_EXT_CAP_ID_DSN Device Serial Number
539 * %PCI_EXT_CAP_ID_PWR Power Budgeting
540 */
541int pci_find_ext_capability(struct pci_dev *dev, int cap)
542{
Bjorn Helgaas44a9a362012-07-13 14:24:59 -0600543 return pci_find_next_ext_capability(dev, 0, cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
Brice Goglin3a720d72006-05-23 06:10:01 -0400545EXPORT_SYMBOL_GPL(pci_find_ext_capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100547static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
548{
549 int rc, ttl = PCI_FIND_CAP_TTL;
550 u8 cap, mask;
551
552 if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
553 mask = HT_3BIT_CAP_MASK;
554 else
555 mask = HT_5BIT_CAP_MASK;
556
557 pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
558 PCI_CAP_ID_HT, &ttl);
559 while (pos) {
560 rc = pci_read_config_byte(dev, pos + 3, &cap);
561 if (rc != PCIBIOS_SUCCESSFUL)
562 return 0;
563
564 if ((cap & mask) == ht_cap)
565 return pos;
566
Brice Goglin47a4d5b2007-01-10 23:15:29 -0800567 pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
568 pos + PCI_CAP_LIST_NEXT,
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100569 PCI_CAP_ID_HT, &ttl);
570 }
571
572 return 0;
573}
574/**
575 * pci_find_next_ht_capability - query a device's Hypertransport capabilities
576 * @dev: PCI device to query
577 * @pos: Position from which to continue searching
578 * @ht_cap: Hypertransport capability code
579 *
580 * To be used in conjunction with pci_find_ht_capability() to search for
581 * all capabilities matching @ht_cap. @pos should always be a value returned
582 * from pci_find_ht_capability().
583 *
584 * NB. To be 100% safe against broken PCI devices, the caller should take
585 * steps to avoid an infinite loop.
586 */
587int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
588{
589 return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
590}
591EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
592
593/**
594 * pci_find_ht_capability - query a device's Hypertransport capabilities
595 * @dev: PCI device to query
596 * @ht_cap: Hypertransport capability code
597 *
598 * Tell if a device supports a given Hypertransport capability.
599 * Returns an address within the device's PCI configuration space
600 * or 0 in case the device does not support the request capability.
601 * The address points to the PCI capability, of type PCI_CAP_ID_HT,
602 * which has a Hypertransport capability matching @ht_cap.
603 */
604int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
605{
606 int pos;
607
608 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
609 if (pos)
610 pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
611
612 return pos;
613}
614EXPORT_SYMBOL_GPL(pci_find_ht_capability);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/**
617 * pci_find_parent_resource - return resource region of parent bus of given region
618 * @dev: PCI device structure contains resources to be searched
619 * @res: child resource record for which parent is sought
620 *
621 * For given resource region of given device, return the resource
Bjorn Helgaasf44116a2014-02-26 11:25:58 -0700622 * region of parent bus the given region is contained in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400624struct resource *pci_find_parent_resource(const struct pci_dev *dev,
625 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 const struct pci_bus *bus = dev->bus;
Bjorn Helgaasf44116a2014-02-26 11:25:58 -0700628 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700631 pci_bus_for_each_resource(bus, r, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (!r)
633 continue;
Ard Biesheuvel31342332017-04-11 17:33:12 +0100634 if (resource_contains(r, res)) {
Bjorn Helgaasf44116a2014-02-26 11:25:58 -0700635
636 /*
637 * If the window is prefetchable but the BAR is
638 * not, the allocator made a mistake.
639 */
640 if (r->flags & IORESOURCE_PREFETCH &&
641 !(res->flags & IORESOURCE_PREFETCH))
642 return NULL;
643
644 /*
645 * If we're below a transparent bridge, there may
646 * be both a positively-decoded aperture and a
647 * subtractively-decoded region that contain the BAR.
648 * We want the positively-decoded one, so this depends
649 * on pci_bus_for_each_resource() giving us those
650 * first.
651 */
652 return r;
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Bjorn Helgaasf44116a2014-02-26 11:25:58 -0700655 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600657EXPORT_SYMBOL(pci_find_parent_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659/**
Mika Westerbergafd29f92016-09-15 11:07:03 +0300660 * pci_find_resource - Return matching PCI device resource
661 * @dev: PCI device to query
662 * @res: Resource to look for
663 *
664 * Goes over standard PCI resources (BARs) and checks if the given resource
665 * is partially or fully contained in any of them. In that case the
666 * matching resource is returned, %NULL otherwise.
667 */
668struct resource *pci_find_resource(struct pci_dev *dev, struct resource *res)
669{
670 int i;
671
672 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
673 struct resource *r = &dev->resource[i];
674
675 if (r->start && resource_contains(r, res))
676 return r;
677 }
678
679 return NULL;
680}
681EXPORT_SYMBOL(pci_find_resource);
682
683/**
Hariprasad Shenaic56d4452015-10-18 19:55:04 +0530684 * pci_find_pcie_root_port - return PCIe Root Port
685 * @dev: PCI device to query
686 *
687 * Traverse up the parent chain and return the PCIe Root Port PCI Device
688 * for a given PCI Device.
689 */
690struct pci_dev *pci_find_pcie_root_port(struct pci_dev *dev)
691{
Thierry Redingb6f6d562017-08-17 13:06:14 +0200692 struct pci_dev *bridge, *highest_pcie_bridge = dev;
Hariprasad Shenaic56d4452015-10-18 19:55:04 +0530693
694 bridge = pci_upstream_bridge(dev);
695 while (bridge && pci_is_pcie(bridge)) {
696 highest_pcie_bridge = bridge;
697 bridge = pci_upstream_bridge(bridge);
698 }
699
Thierry Redingb6f6d562017-08-17 13:06:14 +0200700 if (pci_pcie_type(highest_pcie_bridge) != PCI_EXP_TYPE_ROOT_PORT)
701 return NULL;
Hariprasad Shenaic56d4452015-10-18 19:55:04 +0530702
Thierry Redingb6f6d562017-08-17 13:06:14 +0200703 return highest_pcie_bridge;
Hariprasad Shenaic56d4452015-10-18 19:55:04 +0530704}
705EXPORT_SYMBOL(pci_find_pcie_root_port);
706
707/**
Alex Williamson157e8762013-12-17 16:43:39 -0700708 * pci_wait_for_pending - wait for @mask bit(s) to clear in status word @pos
709 * @dev: the PCI device to operate on
710 * @pos: config space offset of status word
711 * @mask: mask of bit(s) to care about in status word
712 *
713 * Return 1 when mask bit(s) in status word clear, 0 otherwise.
714 */
715int pci_wait_for_pending(struct pci_dev *dev, int pos, u16 mask)
716{
717 int i;
718
719 /* Wait for Transaction Pending bit clean */
720 for (i = 0; i < 4; i++) {
721 u16 status;
722 if (i)
723 msleep((1 << (i - 1)) * 100);
724
725 pci_read_config_word(dev, pos, &status);
726 if (!(status & mask))
727 return 1;
728 }
729
730 return 0;
731}
732
733/**
Wei Yang70675e02015-07-29 16:52:58 +0800734 * pci_restore_bars - restore a device's BAR values (e.g. after wake-up)
John W. Linville064b53db2005-07-27 10:19:44 -0400735 * @dev: PCI device to have its BARs restored
736 *
737 * Restore the BAR values for a given device, so as to make it
738 * accessible by its driver.
739 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400740static void pci_restore_bars(struct pci_dev *dev)
John W. Linville064b53db2005-07-27 10:19:44 -0400741{
Yu Zhaobc5f5a82008-11-22 02:40:00 +0800742 int i;
John W. Linville064b53db2005-07-27 10:19:44 -0400743
Yu Zhaobc5f5a82008-11-22 02:40:00 +0800744 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
Yu Zhao14add802008-11-22 02:38:52 +0800745 pci_update_resource(dev, i);
John W. Linville064b53db2005-07-27 10:19:44 -0400746}
747
Julia Lawall299f2ff2015-12-06 17:33:45 +0100748static const struct pci_platform_pm_ops *pci_platform_pm;
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200749
Julia Lawall299f2ff2015-12-06 17:33:45 +0100750int pci_set_platform_pm(const struct pci_platform_pm_ops *ops)
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200751{
Lukas Wunnercc7cc022016-09-18 05:39:20 +0200752 if (!ops->is_manageable || !ops->set_state || !ops->get_state ||
Rafael J. Wysocki08476842017-06-24 01:57:35 +0200753 !ops->choose_state || !ops->set_wakeup || !ops->need_resume)
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200754 return -EINVAL;
755 pci_platform_pm = ops;
756 return 0;
757}
758
759static inline bool platform_pci_power_manageable(struct pci_dev *dev)
760{
761 return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
762}
763
764static inline int platform_pci_set_power_state(struct pci_dev *dev,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400765 pci_power_t t)
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200766{
767 return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
768}
769
Lukas Wunnercc7cc022016-09-18 05:39:20 +0200770static inline pci_power_t platform_pci_get_power_state(struct pci_dev *dev)
771{
772 return pci_platform_pm ? pci_platform_pm->get_state(dev) : PCI_UNKNOWN;
773}
774
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200775static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
776{
777 return pci_platform_pm ?
778 pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
779}
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700780
Rafael J. Wysocki08476842017-06-24 01:57:35 +0200781static inline int platform_pci_set_wakeup(struct pci_dev *dev, bool enable)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200782{
783 return pci_platform_pm ?
Rafael J. Wysocki08476842017-06-24 01:57:35 +0200784 pci_platform_pm->set_wakeup(dev, enable) : -ENODEV;
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100785}
786
Rafael J. Wysockibac2a902015-01-21 02:17:42 +0100787static inline bool platform_pci_need_resume(struct pci_dev *dev)
788{
789 return pci_platform_pm ? pci_platform_pm->need_resume(dev) : false;
790}
791
John W. Linville064b53db2005-07-27 10:19:44 -0400792/**
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200793 * pci_raw_set_power_state - Use PCI PM registers to set the power state of
794 * given PCI device
795 * @dev: PCI device to handle.
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200796 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 *
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200798 * RETURN VALUE:
799 * -EINVAL if the requested state is invalid.
800 * -EIO if device does not support PCI PM or its PM capabilities register has a
801 * wrong version, or device doesn't support the requested state.
802 * 0 if device already is in the requested state.
803 * 0 if device's power state has been successfully changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 */
Rafael J. Wysockif00a20e2009-03-16 22:40:08 +0100805static int pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200807 u16 pmcsr;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200808 bool need_restore = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Rafael J. Wysocki4a865902009-03-16 22:40:36 +0100810 /* Check if we're already there */
811 if (dev->current_state == state)
812 return 0;
813
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200814 if (!dev->pm_cap)
Andrew Lunncca03de2007-07-09 11:55:58 -0700815 return -EIO;
816
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200817 if (state < PCI_D0 || state > PCI_D3hot)
818 return -EINVAL;
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /* Validate current state:
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700821 * Can enter D0 from any state, but if we can only go deeper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 * to sleep if we're already in a low power state
823 */
Rafael J. Wysocki4a865902009-03-16 22:40:36 +0100824 if (state != PCI_D0 && dev->current_state <= PCI_D3cold
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200825 && dev->current_state > state) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600826 pci_err(dev, "invalid power transition (from state %d to %d)\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -0400827 dev->current_state, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return -EINVAL;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* check if this device supports the desired state */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200832 if ((state == PCI_D1 && !dev->d1_support)
833 || (state == PCI_D2 && !dev->d2_support))
Daniel Ritz3fe9d192005-08-17 15:32:19 -0700834 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200836 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
John W. Linville064b53db2005-07-27 10:19:44 -0400837
John W. Linville32a36582005-09-14 09:52:42 -0400838 /* If we're (effectively) in D3, force entire word to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 * This doesn't affect PME_Status, disables PME_En, and
840 * sets PowerState to 0.
841 */
John W. Linville32a36582005-09-14 09:52:42 -0400842 switch (dev->current_state) {
John W. Linvilled3535fb2005-09-28 17:50:51 -0400843 case PCI_D0:
844 case PCI_D1:
845 case PCI_D2:
846 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
847 pmcsr |= state;
848 break;
Rafael J. Wysockif62795f2009-05-18 22:51:12 +0200849 case PCI_D3hot:
850 case PCI_D3cold:
John W. Linville32a36582005-09-14 09:52:42 -0400851 case PCI_UNKNOWN: /* Boot-up */
852 if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
Rafael J. Wysockif00a20e2009-03-16 22:40:08 +0100853 && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200854 need_restore = true;
John W. Linville32a36582005-09-14 09:52:42 -0400855 /* Fall-through: force to D0 */
John W. Linville32a36582005-09-14 09:52:42 -0400856 default:
John W. Linvilled3535fb2005-09-28 17:50:51 -0400857 pmcsr = 0;
John W. Linville32a36582005-09-14 09:52:42 -0400858 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
861 /* enter specified state */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200862 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 /* Mandatory power management transition delays */
865 /* see PCI PM 1.1 5.6.1 table 18 */
866 if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +0100867 pci_dev_d3_sleep(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 else if (state == PCI_D2 || dev->current_state == PCI_D2)
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100869 udelay(PCI_PM_D2_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Rafael J. Wysockie13cdbd2009-10-05 00:48:40 +0200871 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
872 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
873 if (dev->current_state != state && printk_ratelimit())
Frederick Lawler7506dc72018-01-18 12:55:24 -0600874 pci_info(dev, "Refused to change power state, currently in D%d\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -0400875 dev->current_state);
John W. Linville064b53db2005-07-27 10:19:44 -0400876
Huang Ying448bd852012-06-23 10:23:51 +0800877 /*
878 * According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
John W. Linville064b53db2005-07-27 10:19:44 -0400879 * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
880 * from D3hot to D0 _may_ perform an internal reset, thereby
881 * going to "D0 Uninitialized" rather than "D0 Initialized".
882 * For example, at least some versions of the 3c905B and the
883 * 3c556B exhibit this behaviour.
884 *
885 * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
886 * devices in a D3hot state at boot. Consequently, we need to
887 * restore at least the BARs so that the device will be
888 * accessible to its driver.
889 */
890 if (need_restore)
891 pci_restore_bars(dev);
892
Rafael J. Wysockif00a20e2009-03-16 22:40:08 +0100893 if (dev->bus->self)
Shaohua Li7d715a62008-02-25 09:46:41 +0800894 pcie_aspm_pm_state_change(dev->bus->self);
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return 0;
897}
898
899/**
Lukas Wunnera6a64022016-09-18 05:39:20 +0200900 * pci_update_current_state - Read power state of given device and cache it
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200901 * @dev: PCI device to handle.
Rafael J. Wysockif06fc0b2008-12-27 16:30:52 +0100902 * @state: State to cache in case the device doesn't have the PM capability
Lukas Wunnera6a64022016-09-18 05:39:20 +0200903 *
904 * The power state is read from the PMCSR register, which however is
905 * inaccessible in D3cold. The platform firmware is therefore queried first
906 * to detect accessibility of the register. In case the platform firmware
907 * reports an incorrect state or the device isn't power manageable by the
908 * platform at all, we try to detect D3cold by testing accessibility of the
909 * vendor ID in config space.
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200910 */
Rafael J. Wysocki734104292009-01-07 13:07:15 +0100911void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200912{
Lukas Wunnera6a64022016-09-18 05:39:20 +0200913 if (platform_pci_get_power_state(dev) == PCI_D3cold ||
914 !pci_device_is_present(dev)) {
915 dev->current_state = PCI_D3cold;
916 } else if (dev->pm_cap) {
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200917 u16 pmcsr;
918
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200919 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200920 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
Rafael J. Wysockif06fc0b2008-12-27 16:30:52 +0100921 } else {
922 dev->current_state = state;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200923 }
924}
925
926/**
Rafael J. Wysockidb288c92012-07-05 15:20:00 -0600927 * pci_power_up - Put the given device into D0 forcibly
928 * @dev: PCI device to power up
929 */
930void pci_power_up(struct pci_dev *dev)
931{
932 if (platform_pci_power_manageable(dev))
933 platform_pci_set_power_state(dev, PCI_D0);
934
935 pci_raw_set_power_state(dev, PCI_D0);
936 pci_update_current_state(dev, PCI_D0);
937}
938
939/**
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100940 * pci_platform_power_transition - Use platform to change device power state
941 * @dev: PCI device to handle.
942 * @state: State to put the device into.
943 */
944static int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
945{
946 int error;
947
948 if (platform_pci_power_manageable(dev)) {
949 error = platform_pci_set_power_state(dev, state);
950 if (!error)
951 pci_update_current_state(dev, state);
Rafael J. Wysocki769ba722013-04-12 13:58:17 +0000952 } else
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100953 error = -ENODEV;
Rafael J. Wysocki769ba722013-04-12 13:58:17 +0000954
955 if (error && !dev->pm_cap) /* Fall back to PCI_D0 */
956 dev->current_state = PCI_D0;
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100957
958 return error;
959}
960
961/**
Stephen Hemminger0b950f02014-01-10 17:14:48 -0700962 * pci_wakeup - Wake up a PCI device
963 * @pci_dev: Device to handle.
964 * @ign: ignored parameter
965 */
966static int pci_wakeup(struct pci_dev *pci_dev, void *ign)
967{
968 pci_wakeup_event(pci_dev);
969 pm_request_resume(&pci_dev->dev);
970 return 0;
971}
972
973/**
974 * pci_wakeup_bus - Walk given bus and wake up devices on it
975 * @bus: Top bus of the subtree to walk.
976 */
Lukas Wunner2a4d2c42018-03-03 10:53:24 +0100977void pci_wakeup_bus(struct pci_bus *bus)
Stephen Hemminger0b950f02014-01-10 17:14:48 -0700978{
979 if (bus)
980 pci_walk_bus(bus, pci_wakeup, NULL);
981}
982
983/**
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100984 * __pci_start_power_transition - Start power transition of a PCI device
985 * @dev: PCI device to handle.
986 * @state: State to put the device into.
987 */
988static void __pci_start_power_transition(struct pci_dev *dev, pci_power_t state)
989{
Huang Ying448bd852012-06-23 10:23:51 +0800990 if (state == PCI_D0) {
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100991 pci_platform_power_transition(dev, PCI_D0);
Huang Ying448bd852012-06-23 10:23:51 +0800992 /*
993 * Mandatory power management transition delays, see
994 * PCI Express Base Specification Revision 2.0 Section
995 * 6.6.1: Conventional Reset. Do not delay for
996 * devices powered on/off by corresponding bridge,
997 * because have already delayed for the bridge.
998 */
999 if (dev->runtime_d3cold) {
Adrian Hunter50b2b542017-03-14 15:21:58 +02001000 if (dev->d3cold_delay)
1001 msleep(dev->d3cold_delay);
Huang Ying448bd852012-06-23 10:23:51 +08001002 /*
1003 * When powering on a bridge from D3cold, the
1004 * whole hierarchy may be powered on into
1005 * D0uninitialized state, resume them to give
1006 * them a chance to suspend again
1007 */
1008 pci_wakeup_bus(dev->subordinate);
1009 }
1010 }
1011}
1012
1013/**
1014 * __pci_dev_set_current_state - Set current state of a PCI device
1015 * @dev: Device to handle
1016 * @data: pointer to state to be set
1017 */
1018static int __pci_dev_set_current_state(struct pci_dev *dev, void *data)
1019{
1020 pci_power_t state = *(pci_power_t *)data;
1021
1022 dev->current_state = state;
1023 return 0;
1024}
1025
1026/**
Lukas Wunner2a4d2c42018-03-03 10:53:24 +01001027 * pci_bus_set_current_state - Walk given bus and set current state of devices
Huang Ying448bd852012-06-23 10:23:51 +08001028 * @bus: Top bus of the subtree to walk.
1029 * @state: state to be set
1030 */
Lukas Wunner2a4d2c42018-03-03 10:53:24 +01001031void pci_bus_set_current_state(struct pci_bus *bus, pci_power_t state)
Huang Ying448bd852012-06-23 10:23:51 +08001032{
1033 if (bus)
1034 pci_walk_bus(bus, __pci_dev_set_current_state, &state);
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +01001035}
1036
1037/**
1038 * __pci_complete_power_transition - Complete power transition of a PCI device
1039 * @dev: PCI device to handle.
1040 * @state: State to put the device into.
1041 *
1042 * This function should not be called directly by device drivers.
1043 */
1044int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
1045{
Huang Ying448bd852012-06-23 10:23:51 +08001046 int ret;
1047
Rafael J. Wysockidb288c92012-07-05 15:20:00 -06001048 if (state <= PCI_D0)
Huang Ying448bd852012-06-23 10:23:51 +08001049 return -EINVAL;
1050 ret = pci_platform_power_transition(dev, state);
1051 /* Power off the bridge may power off the whole hierarchy */
1052 if (!ret && state == PCI_D3cold)
Lukas Wunner2a4d2c42018-03-03 10:53:24 +01001053 pci_bus_set_current_state(dev->subordinate, PCI_D3cold);
Huang Ying448bd852012-06-23 10:23:51 +08001054 return ret;
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +01001055}
1056EXPORT_SYMBOL_GPL(__pci_complete_power_transition);
1057
1058/**
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +02001059 * pci_set_power_state - Set the power state of a PCI device
1060 * @dev: PCI device to handle.
1061 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
1062 *
Nick Andrew877d0312009-01-26 11:06:57 +01001063 * Transition a device to a new power state, using the platform firmware and/or
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +02001064 * the device's PCI PM registers.
1065 *
1066 * RETURN VALUE:
1067 * -EINVAL if the requested state is invalid.
1068 * -EIO if device does not support PCI PM or its PM capabilities register has a
1069 * wrong version, or device doesn't support the requested state.
Piotr Gregorab4b8a42017-08-02 20:42:18 +01001070 * 0 if the transition is to D1 or D2 but D1 and D2 are not supported.
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +02001071 * 0 if device already is in the requested state.
Piotr Gregorab4b8a42017-08-02 20:42:18 +01001072 * 0 if the transition is to D3 but D3 is not supported.
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +02001073 * 0 if device's power state has been successfully changed.
1074 */
1075int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
1076{
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001077 int error;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +02001078
1079 /* bound the state we're entering */
Huang Ying448bd852012-06-23 10:23:51 +08001080 if (state > PCI_D3cold)
1081 state = PCI_D3cold;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +02001082 else if (state < PCI_D0)
1083 state = PCI_D0;
1084 else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
1085 /*
1086 * If the device or the parent bridge do not support PCI PM,
1087 * ignore the request if we're doing anything other than putting
1088 * it into D0 (which would only happen on boot).
1089 */
1090 return 0;
1091
Rafael J. Wysockidb288c92012-07-05 15:20:00 -06001092 /* Check if we're already there */
1093 if (dev->current_state == state)
1094 return 0;
1095
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +01001096 __pci_start_power_transition(dev, state);
1097
Alan Cox979b1792008-07-24 17:18:38 +01001098 /* This device is quirked not to be put into D3, so
1099 don't put it in D3 */
Huang Ying448bd852012-06-23 10:23:51 +08001100 if (state >= PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
Alan Cox979b1792008-07-24 17:18:38 +01001101 return 0;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +02001102
Huang Ying448bd852012-06-23 10:23:51 +08001103 /*
1104 * To put device in D3cold, we put device into D3hot in native
1105 * way, then put device into D3cold with platform ops
1106 */
1107 error = pci_raw_set_power_state(dev, state > PCI_D3hot ?
1108 PCI_D3hot : state);
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +02001109
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +01001110 if (!__pci_complete_power_transition(dev, state))
1111 error = 0;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +02001112
1113 return error;
1114}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001115EXPORT_SYMBOL(pci_set_power_state);
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +02001116
1117/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 * pci_choose_state - Choose the power state of a PCI device
1119 * @dev: PCI device to be suspended
1120 * @state: target sleep state for the whole system. This is the value
1121 * that is passed to suspend() function.
1122 *
1123 * Returns PCI power state suitable for given device and given system
1124 * message.
1125 */
1126
1127pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
1128{
Shaohua Liab826ca2007-07-20 10:03:22 +08001129 pci_power_t ret;
David Shaohua Li0f644742005-03-19 00:15:48 -05001130
Yijing Wang728cdb72013-06-18 16:22:14 +08001131 if (!dev->pm_cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return PCI_D0;
1133
Rafael J. Wysocki961d9122008-07-07 03:32:02 +02001134 ret = platform_pci_choose_state(dev);
1135 if (ret != PCI_POWER_ERROR)
1136 return ret;
Pavel Machekca078ba2005-09-03 15:56:57 -07001137
1138 switch (state.event) {
1139 case PM_EVENT_ON:
1140 return PCI_D0;
1141 case PM_EVENT_FREEZE:
David Brownellb887d2e2006-08-14 23:11:05 -07001142 case PM_EVENT_PRETHAW:
1143 /* REVISIT both freeze and pre-thaw "should" use D0 */
Pavel Machekca078ba2005-09-03 15:56:57 -07001144 case PM_EVENT_SUSPEND:
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01001145 case PM_EVENT_HIBERNATE:
Pavel Machekca078ba2005-09-03 15:56:57 -07001146 return PCI_D3hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 default:
Frederick Lawler7506dc72018-01-18 12:55:24 -06001148 pci_info(dev, "unrecognized suspend event %d\n",
Bjorn Helgaas80ccba12008-06-13 10:52:11 -06001149 state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 BUG();
1151 }
1152 return PCI_D0;
1153}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154EXPORT_SYMBOL(pci_choose_state);
1155
Yu Zhao89858512009-02-16 02:55:47 +08001156#define PCI_EXP_SAVE_REGS 7
1157
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07001158static struct pci_cap_saved_state *_pci_find_saved_cap(struct pci_dev *pci_dev,
1159 u16 cap, bool extended)
Yinghai Lu34a48762012-02-11 00:18:41 -08001160{
1161 struct pci_cap_saved_state *tmp;
Yinghai Lu34a48762012-02-11 00:18:41 -08001162
Sasha Levinb67bfe02013-02-27 17:06:00 -08001163 hlist_for_each_entry(tmp, &pci_dev->saved_cap_space, next) {
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07001164 if (tmp->cap.cap_extended == extended && tmp->cap.cap_nr == cap)
Yinghai Lu34a48762012-02-11 00:18:41 -08001165 return tmp;
1166 }
1167 return NULL;
1168}
1169
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07001170struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap)
1171{
1172 return _pci_find_saved_cap(dev, cap, false);
1173}
1174
1175struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap)
1176{
1177 return _pci_find_saved_cap(dev, cap, true);
1178}
1179
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001180static int pci_save_pcie_state(struct pci_dev *dev)
1181{
Jiang Liu59875ae2012-07-24 17:20:06 +08001182 int i = 0;
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001183 struct pci_cap_saved_state *save_state;
1184 u16 *cap;
1185
Jiang Liu59875ae2012-07-24 17:20:06 +08001186 if (!pci_is_pcie(dev))
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001187 return 0;
1188
Eric W. Biederman9f355752007-03-08 13:06:13 -07001189 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001190 if (!save_state) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001191 pci_err(dev, "buffer not found in %s\n", __func__);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001192 return -ENOMEM;
1193 }
Jiang Liu59875ae2012-07-24 17:20:06 +08001194
Alex Williamson24a4742f2011-05-10 10:02:11 -06001195 cap = (u16 *)&save_state->cap.data[0];
Jiang Liu59875ae2012-07-24 17:20:06 +08001196 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &cap[i++]);
1197 pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]);
1198 pcie_capability_read_word(dev, PCI_EXP_SLTCTL, &cap[i++]);
1199 pcie_capability_read_word(dev, PCI_EXP_RTCTL, &cap[i++]);
1200 pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &cap[i++]);
1201 pcie_capability_read_word(dev, PCI_EXP_LNKCTL2, &cap[i++]);
1202 pcie_capability_read_word(dev, PCI_EXP_SLTCTL2, &cap[i++]);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001203
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001204 return 0;
1205}
1206
1207static void pci_restore_pcie_state(struct pci_dev *dev)
1208{
Jiang Liu59875ae2012-07-24 17:20:06 +08001209 int i = 0;
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001210 struct pci_cap_saved_state *save_state;
1211 u16 *cap;
1212
1213 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
Jiang Liu59875ae2012-07-24 17:20:06 +08001214 if (!save_state)
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001215 return;
Jiang Liu59875ae2012-07-24 17:20:06 +08001216
Alex Williamson24a4742f2011-05-10 10:02:11 -06001217 cap = (u16 *)&save_state->cap.data[0];
Jiang Liu59875ae2012-07-24 17:20:06 +08001218 pcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]);
1219 pcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]);
1220 pcie_capability_write_word(dev, PCI_EXP_SLTCTL, cap[i++]);
1221 pcie_capability_write_word(dev, PCI_EXP_RTCTL, cap[i++]);
1222 pcie_capability_write_word(dev, PCI_EXP_DEVCTL2, cap[i++]);
1223 pcie_capability_write_word(dev, PCI_EXP_LNKCTL2, cap[i++]);
1224 pcie_capability_write_word(dev, PCI_EXP_SLTCTL2, cap[i++]);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001225}
1226
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001227
1228static int pci_save_pcix_state(struct pci_dev *dev)
1229{
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01001230 int pos;
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001231 struct pci_cap_saved_state *save_state;
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001232
1233 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
Wei Yang0a1a9b42015-06-30 09:16:44 +08001234 if (!pos)
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001235 return 0;
1236
Shaohua Lif34303d2007-12-18 09:56:47 +08001237 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001238 if (!save_state) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001239 pci_err(dev, "buffer not found in %s\n", __func__);
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001240 return -ENOMEM;
1241 }
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001242
Alex Williamson24a4742f2011-05-10 10:02:11 -06001243 pci_read_config_word(dev, pos + PCI_X_CMD,
1244 (u16 *)save_state->cap.data);
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01001245
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001246 return 0;
1247}
1248
1249static void pci_restore_pcix_state(struct pci_dev *dev)
1250{
1251 int i = 0, pos;
1252 struct pci_cap_saved_state *save_state;
1253 u16 *cap;
1254
1255 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
1256 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
Wei Yang0a1a9b42015-06-30 09:16:44 +08001257 if (!save_state || !pos)
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001258 return;
Alex Williamson24a4742f2011-05-10 10:02:11 -06001259 cap = (u16 *)&save_state->cap.data[0];
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001260
1261 pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001262}
1263
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265/**
1266 * pci_save_state - save the PCI configuration space of a device before suspending
1267 * @dev: - PCI device that we're dealing with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001269int pci_save_state(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 int i;
1272 /* XXX: 100% dword access ok here? */
1273 for (i = 0; i < 16; i++)
Kleber Sacilotto de Souza9e0b5b22009-11-25 00:55:51 -02001274 pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]);
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +01001275 dev->state_saved = true;
Quentin Lambert79e50e72014-09-07 20:03:32 +02001276
1277 i = pci_save_pcie_state(dev);
1278 if (i != 0)
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001279 return i;
Quentin Lambert79e50e72014-09-07 20:03:32 +02001280
1281 i = pci_save_pcix_state(dev);
1282 if (i != 0)
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001283 return i;
Quentin Lambert79e50e72014-09-07 20:03:32 +02001284
Quentin Lambert754834b2014-11-06 17:45:55 +01001285 return pci_save_vc_state(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001287EXPORT_SYMBOL(pci_save_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Rafael J. Wysockiebfc5b82012-04-15 21:40:40 +02001289static void pci_restore_config_dword(struct pci_dev *pdev, int offset,
1290 u32 saved_val, int retry)
1291{
1292 u32 val;
1293
1294 pci_read_config_dword(pdev, offset, &val);
1295 if (val == saved_val)
1296 return;
1297
1298 for (;;) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001299 pci_dbg(pdev, "restoring config space at offset %#x (was %#x, writing %#x)\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -04001300 offset, val, saved_val);
Rafael J. Wysockiebfc5b82012-04-15 21:40:40 +02001301 pci_write_config_dword(pdev, offset, saved_val);
1302 if (retry-- <= 0)
1303 return;
1304
1305 pci_read_config_dword(pdev, offset, &val);
1306 if (val == saved_val)
1307 return;
1308
1309 mdelay(1);
1310 }
1311}
1312
Rafael J. Wysockia6cb9ee2012-04-16 23:07:50 +02001313static void pci_restore_config_space_range(struct pci_dev *pdev,
1314 int start, int end, int retry)
Rafael J. Wysockiebfc5b82012-04-15 21:40:40 +02001315{
1316 int index;
1317
1318 for (index = end; index >= start; index--)
1319 pci_restore_config_dword(pdev, 4 * index,
1320 pdev->saved_config_space[index],
1321 retry);
1322}
1323
Rafael J. Wysockia6cb9ee2012-04-16 23:07:50 +02001324static void pci_restore_config_space(struct pci_dev *pdev)
1325{
1326 if (pdev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
1327 pci_restore_config_space_range(pdev, 10, 15, 0);
1328 /* Restore BARs before the command register. */
1329 pci_restore_config_space_range(pdev, 4, 9, 10);
1330 pci_restore_config_space_range(pdev, 0, 3, 0);
1331 } else {
1332 pci_restore_config_space_range(pdev, 0, 15, 0);
1333 }
1334}
1335
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001336/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * pci_restore_state - Restore the saved state of a PCI device
1338 * @dev: - PCI device that we're dealing with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
Jon Mason1d3c16a2010-11-30 17:43:26 -06001340void pci_restore_state(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Alek Duc82f63e2009-08-08 08:46:19 +08001342 if (!dev->state_saved)
Jon Mason1d3c16a2010-11-30 17:43:26 -06001343 return;
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +02001344
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001345 /* PCI Express register must be restored first */
1346 pci_restore_pcie_state(dev);
CQ Tang4ebeb1e2017-05-30 09:25:49 -07001347 pci_restore_pasid_state(dev);
1348 pci_restore_pri_state(dev);
Hao, Xudong1900ca12011-12-17 21:24:40 +08001349 pci_restore_ats_state(dev);
Alex Williamson425c1b22013-12-17 16:43:51 -07001350 pci_restore_vc_state(dev);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001351
Taku Izumib07461a2015-09-17 10:09:37 -05001352 pci_cleanup_aer_error_status_regs(dev);
1353
Rafael J. Wysockia6cb9ee2012-04-16 23:07:50 +02001354 pci_restore_config_space(dev);
Rafael J. Wysockiebfc5b82012-04-15 21:40:40 +02001355
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001356 pci_restore_pcix_state(dev);
Shaohua Li41017f02006-02-08 17:11:38 +08001357 pci_restore_msi_state(dev);
Alexander Duyckccbc1752015-07-07 12:24:35 -07001358
1359 /* Restore ACS and IOV configuration state */
1360 pci_enable_acs(dev);
Yu Zhao8c5cdb62009-03-20 11:25:12 +08001361 pci_restore_iov_state(dev);
Michael Ellerman8fed4b62007-01-25 19:34:08 +11001362
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +02001363 dev->state_saved = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001365EXPORT_SYMBOL(pci_restore_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001367struct pci_saved_state {
1368 u32 config_space[16];
1369 struct pci_cap_saved_data cap[0];
1370};
1371
1372/**
1373 * pci_store_saved_state - Allocate and return an opaque struct containing
1374 * the device saved state.
1375 * @dev: PCI device that we're dealing with
1376 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001377 * Return NULL if no state or error.
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001378 */
1379struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev)
1380{
1381 struct pci_saved_state *state;
1382 struct pci_cap_saved_state *tmp;
1383 struct pci_cap_saved_data *cap;
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001384 size_t size;
1385
1386 if (!dev->state_saved)
1387 return NULL;
1388
1389 size = sizeof(*state) + sizeof(struct pci_cap_saved_data);
1390
Sasha Levinb67bfe02013-02-27 17:06:00 -08001391 hlist_for_each_entry(tmp, &dev->saved_cap_space, next)
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001392 size += sizeof(struct pci_cap_saved_data) + tmp->cap.size;
1393
1394 state = kzalloc(size, GFP_KERNEL);
1395 if (!state)
1396 return NULL;
1397
1398 memcpy(state->config_space, dev->saved_config_space,
1399 sizeof(state->config_space));
1400
1401 cap = state->cap;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001402 hlist_for_each_entry(tmp, &dev->saved_cap_space, next) {
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001403 size_t len = sizeof(struct pci_cap_saved_data) + tmp->cap.size;
1404 memcpy(cap, &tmp->cap, len);
1405 cap = (struct pci_cap_saved_data *)((u8 *)cap + len);
1406 }
1407 /* Empty cap_save terminates list */
1408
1409 return state;
1410}
1411EXPORT_SYMBOL_GPL(pci_store_saved_state);
1412
1413/**
1414 * pci_load_saved_state - Reload the provided save state into struct pci_dev.
1415 * @dev: PCI device that we're dealing with
1416 * @state: Saved state returned from pci_store_saved_state()
1417 */
Konrad Rzeszutek Wilk98d9b272014-12-03 16:40:31 -05001418int pci_load_saved_state(struct pci_dev *dev,
1419 struct pci_saved_state *state)
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001420{
1421 struct pci_cap_saved_data *cap;
1422
1423 dev->state_saved = false;
1424
1425 if (!state)
1426 return 0;
1427
1428 memcpy(dev->saved_config_space, state->config_space,
1429 sizeof(state->config_space));
1430
1431 cap = state->cap;
1432 while (cap->size) {
1433 struct pci_cap_saved_state *tmp;
1434
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07001435 tmp = _pci_find_saved_cap(dev, cap->cap_nr, cap->cap_extended);
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001436 if (!tmp || tmp->cap.size != cap->size)
1437 return -EINVAL;
1438
1439 memcpy(tmp->cap.data, cap->data, tmp->cap.size);
1440 cap = (struct pci_cap_saved_data *)((u8 *)cap +
1441 sizeof(struct pci_cap_saved_data) + cap->size);
1442 }
1443
1444 dev->state_saved = true;
1445 return 0;
1446}
Konrad Rzeszutek Wilk98d9b272014-12-03 16:40:31 -05001447EXPORT_SYMBOL_GPL(pci_load_saved_state);
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001448
1449/**
1450 * pci_load_and_free_saved_state - Reload the save state pointed to by state,
1451 * and free the memory allocated for it.
1452 * @dev: PCI device that we're dealing with
1453 * @state: Pointer to saved state returned from pci_store_saved_state()
1454 */
1455int pci_load_and_free_saved_state(struct pci_dev *dev,
1456 struct pci_saved_state **state)
1457{
1458 int ret = pci_load_saved_state(dev, *state);
1459 kfree(*state);
1460 *state = NULL;
1461 return ret;
1462}
1463EXPORT_SYMBOL_GPL(pci_load_and_free_saved_state);
1464
Bjorn Helgaas8a9d5602014-02-26 11:26:00 -07001465int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
1466{
1467 return pci_enable_resources(dev, bars);
1468}
1469
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001470static int do_pci_enable_device(struct pci_dev *dev, int bars)
1471{
1472 int err;
Vidya Sagar1f6ae472014-07-16 15:33:42 +05301473 struct pci_dev *bridge;
Bjorn Helgaas1e2571a2014-01-29 16:13:51 -07001474 u16 cmd;
1475 u8 pin;
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001476
1477 err = pci_set_power_state(dev, PCI_D0);
1478 if (err < 0 && err != -EIO)
1479 return err;
Vidya Sagar1f6ae472014-07-16 15:33:42 +05301480
1481 bridge = pci_upstream_bridge(dev);
1482 if (bridge)
1483 pcie_aspm_powersave_config_link(bridge);
1484
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001485 err = pcibios_enable_device(dev, bars);
1486 if (err < 0)
1487 return err;
1488 pci_fixup_device(pci_fixup_enable, dev);
1489
Bjorn Helgaas866d5412014-03-07 16:06:05 -07001490 if (dev->msi_enabled || dev->msix_enabled)
1491 return 0;
1492
Bjorn Helgaas1e2571a2014-01-29 16:13:51 -07001493 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
1494 if (pin) {
1495 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1496 if (cmd & PCI_COMMAND_INTX_DISABLE)
1497 pci_write_config_word(dev, PCI_COMMAND,
1498 cmd & ~PCI_COMMAND_INTX_DISABLE);
1499 }
1500
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001501 return 0;
1502}
1503
1504/**
Tejun Heo0b62e132007-07-27 14:43:35 +09001505 * pci_reenable_device - Resume abandoned device
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001506 * @dev: PCI device to be resumed
1507 *
1508 * Note this function is a backend of pci_default_resume and is not supposed
1509 * to be called by normal code, write proper resume handler and use it instead.
1510 */
Tejun Heo0b62e132007-07-27 14:43:35 +09001511int pci_reenable_device(struct pci_dev *dev)
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001512{
Yuji Shimada296ccb02009-04-03 16:41:46 +09001513 if (pci_is_enabled(dev))
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001514 return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
1515 return 0;
1516}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001517EXPORT_SYMBOL(pci_reenable_device);
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001518
Yinghai Lu928bea92013-07-22 14:37:17 -07001519static void pci_enable_bridge(struct pci_dev *dev)
1520{
Bjorn Helgaas79272132013-11-06 10:00:51 -07001521 struct pci_dev *bridge;
Yinghai Lu928bea92013-07-22 14:37:17 -07001522 int retval;
1523
Bjorn Helgaas79272132013-11-06 10:00:51 -07001524 bridge = pci_upstream_bridge(dev);
1525 if (bridge)
1526 pci_enable_bridge(bridge);
Yinghai Lu928bea92013-07-22 14:37:17 -07001527
Yinghai Lucf3e1fe2013-11-05 13:34:38 -07001528 if (pci_is_enabled(dev)) {
Bjorn Helgaasfbeeb822013-11-05 13:34:51 -07001529 if (!dev->is_busmaster)
Yinghai Lucf3e1fe2013-11-05 13:34:38 -07001530 pci_set_master(dev);
Bjorn Helgaas0f50a492017-09-15 01:33:51 -05001531 return;
Yinghai Lucf3e1fe2013-11-05 13:34:38 -07001532 }
1533
Yinghai Lu928bea92013-07-22 14:37:17 -07001534 retval = pci_enable_device(dev);
1535 if (retval)
Frederick Lawler7506dc72018-01-18 12:55:24 -06001536 pci_err(dev, "Error enabling bridge (%d), continuing\n",
Yinghai Lu928bea92013-07-22 14:37:17 -07001537 retval);
1538 pci_set_master(dev);
1539}
1540
Bjorn Helgaasb4b4fbb2013-01-04 12:12:55 -07001541static int pci_enable_device_flags(struct pci_dev *dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Bjorn Helgaas79272132013-11-06 10:00:51 -07001543 struct pci_dev *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 int err;
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001545 int i, bars = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Jesse Barnes97c145f2010-11-05 15:16:36 -04001547 /*
1548 * Power state could be unknown at this point, either due to a fresh
1549 * boot or a device removal call. So get the current power state
1550 * so that things like MSI message writing will behave as expected
1551 * (e.g. if the device really is in D0 at enable time).
1552 */
1553 if (dev->pm_cap) {
1554 u16 pmcsr;
1555 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
1556 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
1557 }
1558
Bjorn Helgaascc7ba392013-02-11 16:47:01 -07001559 if (atomic_inc_return(&dev->enable_cnt) > 1)
Hidetoshi Seto9fb625c2006-12-18 10:28:43 +09001560 return 0; /* already enabled */
1561
Bjorn Helgaas79272132013-11-06 10:00:51 -07001562 bridge = pci_upstream_bridge(dev);
Bjorn Helgaas0f50a492017-09-15 01:33:51 -05001563 if (bridge)
Bjorn Helgaas79272132013-11-06 10:00:51 -07001564 pci_enable_bridge(bridge);
Yinghai Lu928bea92013-07-22 14:37:17 -07001565
Yinghai Lu497f16f2011-12-17 18:33:37 -08001566 /* only skip sriov related */
1567 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
1568 if (dev->resource[i].flags & flags)
1569 bars |= (1 << i);
1570 for (i = PCI_BRIDGE_RESOURCES; i < DEVICE_COUNT_RESOURCE; i++)
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001571 if (dev->resource[i].flags & flags)
1572 bars |= (1 << i);
1573
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001574 err = do_pci_enable_device(dev, bars);
Greg Kroah-Hartman95a62962005-07-28 11:37:33 -07001575 if (err < 0)
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001576 atomic_dec(&dev->enable_cnt);
Hidetoshi Seto9fb625c2006-12-18 10:28:43 +09001577 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
1580/**
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001581 * pci_enable_device_io - Initialize a device for use with IO space
1582 * @dev: PCI device to be initialized
1583 *
1584 * Initialize device before it's used by a driver. Ask low-level code
1585 * to enable I/O resources. Wake up the device if it was suspended.
1586 * Beware, this function can fail.
1587 */
1588int pci_enable_device_io(struct pci_dev *dev)
1589{
Bjorn Helgaasb4b4fbb2013-01-04 12:12:55 -07001590 return pci_enable_device_flags(dev, IORESOURCE_IO);
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001591}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001592EXPORT_SYMBOL(pci_enable_device_io);
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001593
1594/**
1595 * pci_enable_device_mem - Initialize a device for use with Memory space
1596 * @dev: PCI device to be initialized
1597 *
1598 * Initialize device before it's used by a driver. Ask low-level code
1599 * to enable Memory resources. Wake up the device if it was suspended.
1600 * Beware, this function can fail.
1601 */
1602int pci_enable_device_mem(struct pci_dev *dev)
1603{
Bjorn Helgaasb4b4fbb2013-01-04 12:12:55 -07001604 return pci_enable_device_flags(dev, IORESOURCE_MEM);
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001605}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001606EXPORT_SYMBOL(pci_enable_device_mem);
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608/**
1609 * pci_enable_device - Initialize device before it's used by a driver.
1610 * @dev: PCI device to be initialized
1611 *
1612 * Initialize device before it's used by a driver. Ask low-level code
1613 * to enable I/O and memory. Wake up the device if it was suspended.
1614 * Beware, this function can fail.
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001615 *
1616 * Note we don't actually enable the device many times if we call
1617 * this function repeatedly (we just increment the count).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 */
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001619int pci_enable_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Bjorn Helgaasb4b4fbb2013-01-04 12:12:55 -07001621 return pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001623EXPORT_SYMBOL(pci_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Tejun Heo9ac78492007-01-20 16:00:26 +09001625/*
1626 * Managed PCI resources. This manages device on/off, intx/msi/msix
1627 * on/off and BAR regions. pci_dev itself records msi/msix status, so
1628 * there's no need to track it separately. pci_devres is initialized
1629 * when a device is enabled using managed PCI device enable interface.
1630 */
1631struct pci_devres {
Tejun Heo7f375f32007-02-25 04:36:01 -08001632 unsigned int enabled:1;
1633 unsigned int pinned:1;
Tejun Heo9ac78492007-01-20 16:00:26 +09001634 unsigned int orig_intx:1;
1635 unsigned int restore_intx:1;
Heiner Kallweitfc0f9f42017-12-12 07:40:56 +01001636 unsigned int mwi:1;
Tejun Heo9ac78492007-01-20 16:00:26 +09001637 u32 region_mask;
1638};
1639
1640static void pcim_release(struct device *gendev, void *res)
1641{
Geliang Tangf3d2f1652016-01-08 12:05:39 -06001642 struct pci_dev *dev = to_pci_dev(gendev);
Tejun Heo9ac78492007-01-20 16:00:26 +09001643 struct pci_devres *this = res;
1644 int i;
1645
1646 if (dev->msi_enabled)
1647 pci_disable_msi(dev);
1648 if (dev->msix_enabled)
1649 pci_disable_msix(dev);
1650
1651 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
1652 if (this->region_mask & (1 << i))
1653 pci_release_region(dev, i);
1654
Heiner Kallweitfc0f9f42017-12-12 07:40:56 +01001655 if (this->mwi)
1656 pci_clear_mwi(dev);
1657
Tejun Heo9ac78492007-01-20 16:00:26 +09001658 if (this->restore_intx)
1659 pci_intx(dev, this->orig_intx);
1660
Tejun Heo7f375f32007-02-25 04:36:01 -08001661 if (this->enabled && !this->pinned)
Tejun Heo9ac78492007-01-20 16:00:26 +09001662 pci_disable_device(dev);
1663}
1664
Ryan Desfosses07656d83082014-04-11 01:01:53 -04001665static struct pci_devres *get_pci_dr(struct pci_dev *pdev)
Tejun Heo9ac78492007-01-20 16:00:26 +09001666{
1667 struct pci_devres *dr, *new_dr;
1668
1669 dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
1670 if (dr)
1671 return dr;
1672
1673 new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
1674 if (!new_dr)
1675 return NULL;
1676 return devres_get(&pdev->dev, new_dr, NULL, NULL);
1677}
1678
Ryan Desfosses07656d83082014-04-11 01:01:53 -04001679static struct pci_devres *find_pci_dr(struct pci_dev *pdev)
Tejun Heo9ac78492007-01-20 16:00:26 +09001680{
1681 if (pci_is_managed(pdev))
1682 return devres_find(&pdev->dev, pcim_release, NULL, NULL);
1683 return NULL;
1684}
1685
1686/**
1687 * pcim_enable_device - Managed pci_enable_device()
1688 * @pdev: PCI device to be initialized
1689 *
1690 * Managed pci_enable_device().
1691 */
1692int pcim_enable_device(struct pci_dev *pdev)
1693{
1694 struct pci_devres *dr;
1695 int rc;
1696
1697 dr = get_pci_dr(pdev);
1698 if (unlikely(!dr))
1699 return -ENOMEM;
Tejun Heob95d58e2008-01-30 18:20:04 +09001700 if (dr->enabled)
1701 return 0;
Tejun Heo9ac78492007-01-20 16:00:26 +09001702
1703 rc = pci_enable_device(pdev);
1704 if (!rc) {
1705 pdev->is_managed = 1;
Tejun Heo7f375f32007-02-25 04:36:01 -08001706 dr->enabled = 1;
Tejun Heo9ac78492007-01-20 16:00:26 +09001707 }
1708 return rc;
1709}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001710EXPORT_SYMBOL(pcim_enable_device);
Tejun Heo9ac78492007-01-20 16:00:26 +09001711
1712/**
1713 * pcim_pin_device - Pin managed PCI device
1714 * @pdev: PCI device to pin
1715 *
1716 * Pin managed PCI device @pdev. Pinned device won't be disabled on
1717 * driver detach. @pdev must have been enabled with
1718 * pcim_enable_device().
1719 */
1720void pcim_pin_device(struct pci_dev *pdev)
1721{
1722 struct pci_devres *dr;
1723
1724 dr = find_pci_dr(pdev);
Tejun Heo7f375f32007-02-25 04:36:01 -08001725 WARN_ON(!dr || !dr->enabled);
Tejun Heo9ac78492007-01-20 16:00:26 +09001726 if (dr)
Tejun Heo7f375f32007-02-25 04:36:01 -08001727 dr->pinned = 1;
Tejun Heo9ac78492007-01-20 16:00:26 +09001728}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001729EXPORT_SYMBOL(pcim_pin_device);
Tejun Heo9ac78492007-01-20 16:00:26 +09001730
Matthew Garretteca0d4672012-12-05 14:33:27 -07001731/*
1732 * pcibios_add_device - provide arch specific hooks when adding device dev
1733 * @dev: the PCI device being added
1734 *
1735 * Permits the platform to provide architecture specific functionality when
1736 * devices are added. This is the default implementation. Architecture
1737 * implementations can override this.
1738 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001739int __weak pcibios_add_device(struct pci_dev *dev)
Matthew Garretteca0d4672012-12-05 14:33:27 -07001740{
1741 return 0;
1742}
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744/**
Sebastian Ott6ae32c52013-06-04 19:18:14 +02001745 * pcibios_release_device - provide arch specific hooks when releasing device dev
1746 * @dev: the PCI device being released
1747 *
1748 * Permits the platform to provide architecture specific functionality when
1749 * devices are released. This is the default implementation. Architecture
1750 * implementations can override this.
1751 */
1752void __weak pcibios_release_device(struct pci_dev *dev) {}
1753
1754/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 * pcibios_disable_device - disable arch specific PCI resources for device dev
1756 * @dev: the PCI device to disable
1757 *
1758 * Disables architecture specific PCI resources for the device. This
1759 * is the default implementation. Architecture implementations can
1760 * override this.
1761 */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001762void __weak pcibios_disable_device(struct pci_dev *dev) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Hanjun Guoa43ae582014-05-06 11:29:52 +08001764/**
1765 * pcibios_penalize_isa_irq - penalize an ISA IRQ
1766 * @irq: ISA IRQ to penalize
1767 * @active: IRQ active or not
1768 *
1769 * Permits the platform to provide architecture-specific functionality when
1770 * penalizing ISA IRQs. This is the default implementation. Architecture
1771 * implementations can override this.
1772 */
1773void __weak pcibios_penalize_isa_irq(int irq, int active) {}
1774
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001775static void do_pci_disable_device(struct pci_dev *dev)
1776{
1777 u16 pci_command;
1778
1779 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
1780 if (pci_command & PCI_COMMAND_MASTER) {
1781 pci_command &= ~PCI_COMMAND_MASTER;
1782 pci_write_config_word(dev, PCI_COMMAND, pci_command);
1783 }
1784
1785 pcibios_disable_device(dev);
1786}
1787
1788/**
1789 * pci_disable_enabled_device - Disable device without updating enable_cnt
1790 * @dev: PCI device to disable
1791 *
1792 * NOTE: This function is a backend of PCI power management routines and is
1793 * not supposed to be called drivers.
1794 */
1795void pci_disable_enabled_device(struct pci_dev *dev)
1796{
Yuji Shimada296ccb02009-04-03 16:41:46 +09001797 if (pci_is_enabled(dev))
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001798 do_pci_disable_device(dev);
1799}
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801/**
1802 * pci_disable_device - Disable PCI device after use
1803 * @dev: PCI device to be disabled
1804 *
1805 * Signal to the system that the PCI device is not in use by the system
1806 * anymore. This only involves disabling PCI bus-mastering, if active.
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001807 *
1808 * Note we don't actually disable the device until all callers of
Roman Fietzeee6583f2010-05-18 14:45:47 +02001809 * pci_enable_device() have called pci_disable_device().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001811void pci_disable_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Tejun Heo9ac78492007-01-20 16:00:26 +09001813 struct pci_devres *dr;
Shaohua Li99dc8042006-05-26 10:58:27 +08001814
Tejun Heo9ac78492007-01-20 16:00:26 +09001815 dr = find_pci_dr(dev);
1816 if (dr)
Tejun Heo7f375f32007-02-25 04:36:01 -08001817 dr->enabled = 0;
Tejun Heo9ac78492007-01-20 16:00:26 +09001818
Konstantin Khlebnikovfd6dcea2013-02-04 15:56:01 +04001819 dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0,
1820 "disabling already-disabled device");
1821
Bjorn Helgaascc7ba392013-02-11 16:47:01 -07001822 if (atomic_dec_return(&dev->enable_cnt) != 0)
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001823 return;
1824
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001825 do_pci_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001827 dev->is_busmaster = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001829EXPORT_SYMBOL(pci_disable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831/**
Brian Kingf7bdd122007-04-06 16:39:36 -05001832 * pcibios_set_pcie_reset_state - set reset state for device dev
Stefan Assmann45e829e2009-12-03 06:49:24 -05001833 * @dev: the PCIe device reset
Brian Kingf7bdd122007-04-06 16:39:36 -05001834 * @state: Reset state to enter into
1835 *
1836 *
Stefan Assmann45e829e2009-12-03 06:49:24 -05001837 * Sets the PCIe reset state for the device. This is the default
Brian Kingf7bdd122007-04-06 16:39:36 -05001838 * implementation. Architecture implementations can override this.
1839 */
Bjorn Helgaasd6d88c82012-06-19 06:54:49 -06001840int __weak pcibios_set_pcie_reset_state(struct pci_dev *dev,
1841 enum pcie_reset_state state)
Brian Kingf7bdd122007-04-06 16:39:36 -05001842{
1843 return -EINVAL;
1844}
1845
1846/**
1847 * pci_set_pcie_reset_state - set reset state for device dev
Stefan Assmann45e829e2009-12-03 06:49:24 -05001848 * @dev: the PCIe device reset
Brian Kingf7bdd122007-04-06 16:39:36 -05001849 * @state: Reset state to enter into
1850 *
1851 *
1852 * Sets the PCI reset state for the device.
1853 */
1854int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
1855{
1856 return pcibios_set_pcie_reset_state(dev, state);
1857}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001858EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);
Brian Kingf7bdd122007-04-06 16:39:36 -05001859
1860/**
Bjorn Helgaasdcb04532018-03-09 11:06:53 -06001861 * pcie_clear_root_pme_status - Clear root port PME interrupt status.
1862 * @dev: PCIe root port or event collector.
1863 */
1864void pcie_clear_root_pme_status(struct pci_dev *dev)
1865{
1866 pcie_capability_set_dword(dev, PCI_EXP_RTSTA, PCI_EXP_RTSTA_PME);
1867}
1868
1869/**
Rafael J. Wysocki58ff4632010-02-17 23:36:58 +01001870 * pci_check_pme_status - Check if given device has generated PME.
1871 * @dev: Device to check.
1872 *
1873 * Check the PME status of the device and if set, clear it and clear PME enable
1874 * (if set). Return 'true' if PME status and PME enable were both set or
1875 * 'false' otherwise.
1876 */
1877bool pci_check_pme_status(struct pci_dev *dev)
1878{
1879 int pmcsr_pos;
1880 u16 pmcsr;
1881 bool ret = false;
1882
1883 if (!dev->pm_cap)
1884 return false;
1885
1886 pmcsr_pos = dev->pm_cap + PCI_PM_CTRL;
1887 pci_read_config_word(dev, pmcsr_pos, &pmcsr);
1888 if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
1889 return false;
1890
1891 /* Clear PME status. */
1892 pmcsr |= PCI_PM_CTRL_PME_STATUS;
1893 if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
1894 /* Disable PME to avoid interrupt flood. */
1895 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
1896 ret = true;
1897 }
1898
1899 pci_write_config_word(dev, pmcsr_pos, pmcsr);
1900
1901 return ret;
1902}
1903
1904/**
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001905 * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set.
1906 * @dev: Device to handle.
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001907 * @pme_poll_reset: Whether or not to reset the device's pme_poll flag.
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001908 *
1909 * Check if @dev has generated PME and queue a resume request for it in that
1910 * case.
1911 */
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001912static int pci_pme_wakeup(struct pci_dev *dev, void *pme_poll_reset)
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001913{
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001914 if (pme_poll_reset && dev->pme_poll)
1915 dev->pme_poll = false;
1916
Rafael J. Wysockic125e962010-07-05 22:43:53 +02001917 if (pci_check_pme_status(dev)) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +02001918 pci_wakeup_event(dev);
Rafael J. Wysocki0f953bf2010-12-29 13:22:08 +01001919 pm_request_resume(&dev->dev);
Rafael J. Wysockic125e962010-07-05 22:43:53 +02001920 }
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001921 return 0;
1922}
1923
1924/**
1925 * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary.
1926 * @bus: Top bus of the subtree to walk.
1927 */
1928void pci_pme_wakeup_bus(struct pci_bus *bus)
1929{
1930 if (bus)
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001931 pci_walk_bus(bus, pci_pme_wakeup, (void *)true);
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001932}
1933
Huang Ying448bd852012-06-23 10:23:51 +08001934
1935/**
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001936 * pci_pme_capable - check the capability of PCI device to generate PME#
1937 * @dev: PCI device to handle.
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001938 * @state: PCI state from which device will issue PME#.
1939 */
Rafael J. Wysockie5899e12008-07-19 14:39:24 +02001940bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001941{
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001942 if (!dev->pm_cap)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001943 return false;
1944
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001945 return !!(dev->pme_support & (1 << state));
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001946}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001947EXPORT_SYMBOL(pci_pme_capable);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001948
Matthew Garrettdf17e622010-10-04 14:22:29 -04001949static void pci_pme_list_scan(struct work_struct *work)
1950{
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001951 struct pci_pme_device *pme_dev, *n;
Matthew Garrettdf17e622010-10-04 14:22:29 -04001952
1953 mutex_lock(&pci_pme_list_mutex);
Bjorn Helgaasce300002014-01-24 09:51:06 -07001954 list_for_each_entry_safe(pme_dev, n, &pci_pme_list, list) {
1955 if (pme_dev->dev->pme_poll) {
1956 struct pci_dev *bridge;
Zheng Yan71a83bd2012-06-23 10:23:49 +08001957
Bjorn Helgaasce300002014-01-24 09:51:06 -07001958 bridge = pme_dev->dev->bus->self;
1959 /*
1960 * If bridge is in low power state, the
1961 * configuration space of subordinate devices
1962 * may be not accessible
1963 */
1964 if (bridge && bridge->current_state != PCI_D0)
1965 continue;
1966 pci_pme_wakeup(pme_dev->dev, NULL);
1967 } else {
1968 list_del(&pme_dev->list);
1969 kfree(pme_dev);
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001970 }
Matthew Garrettdf17e622010-10-04 14:22:29 -04001971 }
Bjorn Helgaasce300002014-01-24 09:51:06 -07001972 if (!list_empty(&pci_pme_list))
Lukas Wunnerea003532017-04-18 20:44:30 +02001973 queue_delayed_work(system_freezable_wq, &pci_pme_work,
1974 msecs_to_jiffies(PME_TIMEOUT));
Matthew Garrettdf17e622010-10-04 14:22:29 -04001975 mutex_unlock(&pci_pme_list_mutex);
1976}
1977
Rafael J. Wysocki2cef5482015-09-30 01:10:24 +02001978static void __pci_pme_active(struct pci_dev *dev, bool enable)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001979{
1980 u16 pmcsr;
1981
Rafael J. Wysockiffaddbe2013-04-10 10:32:51 +00001982 if (!dev->pme_support)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001983 return;
1984
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001985 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001986 /* Clear PME_Status by writing 1 to it and enable PME# */
1987 pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
1988 if (!enable)
1989 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
1990
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001991 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
Rafael J. Wysocki2cef5482015-09-30 01:10:24 +02001992}
1993
Rafael J. Wysocki0ce3fca2017-07-12 03:05:39 +02001994/**
1995 * pci_pme_restore - Restore PME configuration after config space restore.
1996 * @dev: PCI device to update.
1997 */
1998void pci_pme_restore(struct pci_dev *dev)
Rafael J. Wysockidc15e712017-06-12 22:53:36 +02001999{
2000 u16 pmcsr;
2001
2002 if (!dev->pme_support)
2003 return;
2004
2005 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
2006 if (dev->wakeup_prepared) {
2007 pmcsr |= PCI_PM_CTRL_PME_ENABLE;
Rafael J. Wysocki0ce3fca2017-07-12 03:05:39 +02002008 pmcsr &= ~PCI_PM_CTRL_PME_STATUS;
Rafael J. Wysockidc15e712017-06-12 22:53:36 +02002009 } else {
2010 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
2011 pmcsr |= PCI_PM_CTRL_PME_STATUS;
2012 }
2013 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
2014}
2015
Rafael J. Wysocki2cef5482015-09-30 01:10:24 +02002016/**
2017 * pci_pme_active - enable or disable PCI device's PME# function
2018 * @dev: PCI device to handle.
2019 * @enable: 'true' to enable PME# generation; 'false' to disable it.
2020 *
2021 * The caller must verify that the device is capable of generating PME# before
2022 * calling this function with @enable equal to 'true'.
2023 */
2024void pci_pme_active(struct pci_dev *dev, bool enable)
2025{
2026 __pci_pme_active(dev, enable);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002027
Huang Ying6e965e02012-10-26 13:07:51 +08002028 /*
2029 * PCI (as opposed to PCIe) PME requires that the device have
2030 * its PME# line hooked up correctly. Not all hardware vendors
2031 * do this, so the PME never gets delivered and the device
2032 * remains asleep. The easiest way around this is to
2033 * periodically walk the list of suspended devices and check
2034 * whether any have their PME flag set. The assumption is that
2035 * we'll wake up often enough anyway that this won't be a huge
2036 * hit, and the power savings from the devices will still be a
2037 * win.
2038 *
2039 * Although PCIe uses in-band PME message instead of PME# line
2040 * to report PME, PME does not work for some PCIe devices in
2041 * reality. For example, there are devices that set their PME
2042 * status bits, but don't really bother to send a PME message;
2043 * there are PCI Express Root Ports that don't bother to
2044 * trigger interrupts when they receive PME messages from the
2045 * devices below. So PME poll is used for PCIe devices too.
2046 */
Matthew Garrettdf17e622010-10-04 14:22:29 -04002047
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02002048 if (dev->pme_poll) {
Matthew Garrettdf17e622010-10-04 14:22:29 -04002049 struct pci_pme_device *pme_dev;
2050 if (enable) {
2051 pme_dev = kmalloc(sizeof(struct pci_pme_device),
2052 GFP_KERNEL);
Bjorn Helgaas0394cb12013-10-16 12:32:53 -06002053 if (!pme_dev) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06002054 pci_warn(dev, "can't enable PME#\n");
Bjorn Helgaas0394cb12013-10-16 12:32:53 -06002055 return;
2056 }
Matthew Garrettdf17e622010-10-04 14:22:29 -04002057 pme_dev->dev = dev;
2058 mutex_lock(&pci_pme_list_mutex);
2059 list_add(&pme_dev->list, &pci_pme_list);
2060 if (list_is_singular(&pci_pme_list))
Lukas Wunnerea003532017-04-18 20:44:30 +02002061 queue_delayed_work(system_freezable_wq,
2062 &pci_pme_work,
2063 msecs_to_jiffies(PME_TIMEOUT));
Matthew Garrettdf17e622010-10-04 14:22:29 -04002064 mutex_unlock(&pci_pme_list_mutex);
2065 } else {
2066 mutex_lock(&pci_pme_list_mutex);
2067 list_for_each_entry(pme_dev, &pci_pme_list, list) {
2068 if (pme_dev->dev == dev) {
2069 list_del(&pme_dev->list);
2070 kfree(pme_dev);
2071 break;
2072 }
2073 }
2074 mutex_unlock(&pci_pme_list_mutex);
2075 }
2076 }
2077
Frederick Lawler7506dc72018-01-18 12:55:24 -06002078 pci_dbg(dev, "PME# %s\n", enable ? "enabled" : "disabled");
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002079}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002080EXPORT_SYMBOL(pci_pme_active);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002081
2082/**
Rafael J. Wysockicfcadfa2018-05-09 00:18:32 +02002083 * __pci_enable_wake - enable PCI device as wakeup event source
David Brownell075c1772007-04-26 00:12:06 -07002084 * @dev: PCI device affected
2085 * @state: PCI state from which device will issue wakeup events
2086 * @enable: True to enable event generation; false to disable
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 *
David Brownell075c1772007-04-26 00:12:06 -07002088 * This enables the device as a wakeup event source, or disables it.
2089 * When such events involves platform-specific hooks, those hooks are
2090 * called automatically by this routine.
2091 *
2092 * Devices with legacy power management (no standard PCI PM capabilities)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002093 * always require such platform hooks.
David Brownell075c1772007-04-26 00:12:06 -07002094 *
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002095 * RETURN VALUE:
2096 * 0 is returned on success
2097 * -EINVAL is returned if device is not supposed to wake up the system
2098 * Error code depending on the platform is returned if both the platform and
2099 * the native mechanism fail to enable the generation of wake-up events
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 */
Rafael J. Wysockicfcadfa2018-05-09 00:18:32 +02002101static int __pci_enable_wake(struct pci_dev *dev, pci_power_t state, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02002103 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Rafael J. Wysockibaecc472017-07-21 14:38:08 +02002105 /*
2106 * Bridges can only signal wakeup on behalf of subordinate devices,
2107 * but that is set up elsewhere, so skip them.
2108 */
2109 if (pci_has_subordinate(dev))
2110 return 0;
2111
Rafael J. Wysocki0ce3fca2017-07-12 03:05:39 +02002112 /* Don't do the same thing twice in a row for one device. */
2113 if (!!enable == !!dev->wakeup_prepared)
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02002114 return 0;
2115
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002116 /*
2117 * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
2118 * Anderson we should be doing PME# wake enable followed by ACPI wake
2119 * enable. To disable wake-up we call the platform first, for symmetry.
David Brownell075c1772007-04-26 00:12:06 -07002120 */
2121
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02002122 if (enable) {
2123 int error;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002124
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02002125 if (pci_pme_capable(dev, state))
2126 pci_pme_active(dev, true);
2127 else
2128 ret = 1;
Rafael J. Wysocki08476842017-06-24 01:57:35 +02002129 error = platform_pci_set_wakeup(dev, true);
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02002130 if (ret)
2131 ret = error;
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02002132 if (!ret)
2133 dev->wakeup_prepared = true;
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02002134 } else {
Rafael J. Wysocki08476842017-06-24 01:57:35 +02002135 platform_pci_set_wakeup(dev, false);
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02002136 pci_pme_active(dev, false);
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02002137 dev->wakeup_prepared = false;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002138 }
2139
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02002140 return ret;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002141}
Rafael J. Wysockicfcadfa2018-05-09 00:18:32 +02002142
2143/**
2144 * pci_enable_wake - change wakeup settings for a PCI device
2145 * @pci_dev: Target device
2146 * @state: PCI state from which device will issue wakeup events
2147 * @enable: Whether or not to enable event generation
2148 *
2149 * If @enable is set, check device_may_wakeup() for the device before calling
2150 * __pci_enable_wake() for it.
2151 */
2152int pci_enable_wake(struct pci_dev *pci_dev, pci_power_t state, bool enable)
2153{
2154 if (enable && !device_may_wakeup(&pci_dev->dev))
2155 return -EINVAL;
2156
2157 return __pci_enable_wake(pci_dev, state, enable);
2158}
Rafael J. Wysocki08476842017-06-24 01:57:35 +02002159EXPORT_SYMBOL(pci_enable_wake);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002160
2161/**
Rafael J. Wysocki0235c4f2008-08-18 21:38:00 +02002162 * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
2163 * @dev: PCI device to prepare
2164 * @enable: True to enable wake-up event generation; false to disable
2165 *
2166 * Many drivers want the device to wake up the system from D3_hot or D3_cold
2167 * and this function allows them to set that up cleanly - pci_enable_wake()
2168 * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
2169 * ordering constraints.
2170 *
Rafael J. Wysockicfcadfa2018-05-09 00:18:32 +02002171 * This function only returns error code if the device is not allowed to wake
2172 * up the system from sleep or it is not capable of generating PME# from both
2173 * D3_hot and D3_cold and the platform is unable to enable wake-up power for it.
Rafael J. Wysocki0235c4f2008-08-18 21:38:00 +02002174 */
2175int pci_wake_from_d3(struct pci_dev *dev, bool enable)
2176{
2177 return pci_pme_capable(dev, PCI_D3cold) ?
2178 pci_enable_wake(dev, PCI_D3cold, enable) :
2179 pci_enable_wake(dev, PCI_D3hot, enable);
2180}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002181EXPORT_SYMBOL(pci_wake_from_d3);
Rafael J. Wysocki0235c4f2008-08-18 21:38:00 +02002182
2183/**
Jesse Barnes37139072008-07-28 11:49:26 -07002184 * pci_target_state - find an appropriate low power state for a given PCI dev
2185 * @dev: PCI device
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002186 * @wakeup: Whether or not wakeup functionality will be enabled for the device.
Jesse Barnes37139072008-07-28 11:49:26 -07002187 *
2188 * Use underlying platform code to find a supported low power state for @dev.
2189 * If the platform can't manage @dev, return the deepest state from which it
2190 * can generate wake events, based on any available PME info.
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002191 */
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002192static pci_power_t pci_target_state(struct pci_dev *dev, bool wakeup)
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002193{
2194 pci_power_t target_state = PCI_D3hot;
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002195
2196 if (platform_pci_power_manageable(dev)) {
2197 /*
Rafael J. Wysocki60ee031a2018-05-21 13:11:12 +02002198 * Call the platform to find the target state for the device.
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002199 */
2200 pci_power_t state = platform_pci_choose_state(dev);
2201
2202 switch (state) {
2203 case PCI_POWER_ERROR:
2204 case PCI_UNKNOWN:
2205 break;
2206 case PCI_D1:
2207 case PCI_D2:
2208 if (pci_no_d1d2(dev))
2209 break;
2210 default:
2211 target_state = state;
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002212 }
Lukas Wunner4132a572016-09-18 05:39:20 +02002213
2214 return target_state;
2215 }
2216
2217 if (!dev->pm_cap)
Rafael J. Wysockid2abdf62009-06-14 21:25:02 +02002218 target_state = PCI_D0;
Lukas Wunner4132a572016-09-18 05:39:20 +02002219
2220 /*
2221 * If the device is in D3cold even though it's not power-manageable by
2222 * the platform, it may have been powered down by non-standard means.
2223 * Best to let it slumber.
2224 */
2225 if (dev->current_state == PCI_D3cold)
2226 target_state = PCI_D3cold;
2227
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002228 if (wakeup) {
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002229 /*
2230 * Find the deepest state from which the device can generate
Rafael J. Wysocki60ee031a2018-05-21 13:11:12 +02002231 * PME#.
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002232 */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002233 if (dev->pme_support) {
2234 while (target_state
2235 && !(dev->pme_support & (1 << target_state)))
2236 target_state--;
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002237 }
2238 }
2239
Rafael J. Wysockie5899e12008-07-19 14:39:24 +02002240 return target_state;
2241}
2242
2243/**
2244 * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
2245 * @dev: Device to handle.
2246 *
2247 * Choose the power state appropriate for the device depending on whether
2248 * it can wake up the system and/or is power manageable by the platform
2249 * (PCI_D3hot is the default) and put the device into that state.
2250 */
2251int pci_prepare_to_sleep(struct pci_dev *dev)
2252{
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002253 bool wakeup = device_may_wakeup(&dev->dev);
2254 pci_power_t target_state = pci_target_state(dev, wakeup);
Rafael J. Wysockie5899e12008-07-19 14:39:24 +02002255 int error;
2256
2257 if (target_state == PCI_POWER_ERROR)
2258 return -EIO;
2259
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002260 pci_enable_wake(dev, target_state, wakeup);
Rafael J. Wysockic157dfa2008-07-13 22:45:06 +02002261
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002262 error = pci_set_power_state(dev, target_state);
2263
2264 if (error)
2265 pci_enable_wake(dev, target_state, false);
2266
2267 return error;
2268}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002269EXPORT_SYMBOL(pci_prepare_to_sleep);
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002270
2271/**
Randy Dunlap443bd1c2008-07-21 09:27:18 -07002272 * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002273 * @dev: Device to handle.
2274 *
Thomas Weber88393162010-03-16 11:47:56 +01002275 * Disable device's system wake-up capability and put it into D0.
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002276 */
2277int pci_back_from_sleep(struct pci_dev *dev)
2278{
2279 pci_enable_wake(dev, PCI_D0, false);
2280 return pci_set_power_state(dev, PCI_D0);
2281}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002282EXPORT_SYMBOL(pci_back_from_sleep);
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002283
2284/**
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01002285 * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend.
2286 * @dev: PCI device being suspended.
2287 *
2288 * Prepare @dev to generate wake-up events at run time and put it into a low
2289 * power state.
2290 */
2291int pci_finish_runtime_suspend(struct pci_dev *dev)
2292{
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002293 pci_power_t target_state;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01002294 int error;
2295
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002296 target_state = pci_target_state(dev, device_can_wakeup(&dev->dev));
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01002297 if (target_state == PCI_POWER_ERROR)
2298 return -EIO;
2299
Huang Ying448bd852012-06-23 10:23:51 +08002300 dev->runtime_d3cold = target_state == PCI_D3cold;
2301
Rafael J. Wysockicfcadfa2018-05-09 00:18:32 +02002302 __pci_enable_wake(dev, target_state, pci_dev_run_wake(dev));
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01002303
2304 error = pci_set_power_state(dev, target_state);
2305
Huang Ying448bd852012-06-23 10:23:51 +08002306 if (error) {
Rafael J. Wysocki08476842017-06-24 01:57:35 +02002307 pci_enable_wake(dev, target_state, false);
Huang Ying448bd852012-06-23 10:23:51 +08002308 dev->runtime_d3cold = false;
2309 }
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01002310
2311 return error;
2312}
2313
2314/**
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01002315 * pci_dev_run_wake - Check if device can generate run-time wake-up events.
2316 * @dev: Device to check.
2317 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002318 * Return true if the device itself is capable of generating wake-up events
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01002319 * (through the platform or using the native PCIe PME) or if the device supports
2320 * PME and one of its upstream bridges can generate wake-up events.
2321 */
2322bool pci_dev_run_wake(struct pci_dev *dev)
2323{
2324 struct pci_bus *bus = dev->bus;
2325
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01002326 if (!dev->pme_support)
2327 return false;
2328
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002329 /* PME-capable in principle, but not from the target power state */
Kai Heng Feng8feaec32018-05-07 14:11:20 +08002330 if (!pci_pme_capable(dev, pci_target_state(dev, true)))
Alan Stern6496ebd2016-10-21 16:45:38 -04002331 return false;
2332
Kai Heng Feng8feaec32018-05-07 14:11:20 +08002333 if (device_can_wakeup(&dev->dev))
2334 return true;
2335
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01002336 while (bus->parent) {
2337 struct pci_dev *bridge = bus->self;
2338
Rafael J. Wysockide3ef1e2017-06-24 01:58:53 +02002339 if (device_can_wakeup(&bridge->dev))
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01002340 return true;
2341
2342 bus = bus->parent;
2343 }
2344
2345 /* We have reached the root bus. */
2346 if (bus->bridge)
Rafael J. Wysockide3ef1e2017-06-24 01:58:53 +02002347 return device_can_wakeup(bus->bridge);
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01002348
2349 return false;
2350}
2351EXPORT_SYMBOL_GPL(pci_dev_run_wake);
2352
Rafael J. Wysockibac2a902015-01-21 02:17:42 +01002353/**
2354 * pci_dev_keep_suspended - Check if the device can stay in the suspended state.
2355 * @pci_dev: Device to check.
2356 *
2357 * Return 'true' if the device is runtime-suspended, it doesn't have to be
2358 * reconfigured due to wakeup settings difference between system and runtime
2359 * suspend and the current power state of it is suitable for the upcoming
2360 * (system) transition.
Rafael J. Wysocki2cef5482015-09-30 01:10:24 +02002361 *
2362 * If the device is not configured for system wakeup, disable PME for it before
2363 * returning 'true' to prevent it from waking up the system unnecessarily.
Rafael J. Wysockibac2a902015-01-21 02:17:42 +01002364 */
2365bool pci_dev_keep_suspended(struct pci_dev *pci_dev)
2366{
2367 struct device *dev = &pci_dev->dev;
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002368 bool wakeup = device_may_wakeup(dev);
Rafael J. Wysockibac2a902015-01-21 02:17:42 +01002369
2370 if (!pm_runtime_suspended(dev)
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002371 || pci_target_state(pci_dev, wakeup) != pci_dev->current_state
Rafael J. Wysockic2eac4d2017-10-25 14:16:46 +02002372 || platform_pci_need_resume(pci_dev))
Rafael J. Wysockibac2a902015-01-21 02:17:42 +01002373 return false;
2374
Rafael J. Wysocki2cef5482015-09-30 01:10:24 +02002375 /*
2376 * At this point the device is good to go unless it's been configured
2377 * to generate PME at the runtime suspend time, but it is not supposed
2378 * to wake up the system. In that case, simply disable PME for it
2379 * (it will have to be re-enabled on exit from system resume).
2380 *
2381 * If the device's power state is D3cold and the platform check above
2382 * hasn't triggered, the device's configuration is suitable and we don't
2383 * need to manipulate it at all.
2384 */
2385 spin_lock_irq(&dev->power.lock);
2386
2387 if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold &&
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002388 !wakeup)
Rafael J. Wysocki2cef5482015-09-30 01:10:24 +02002389 __pci_pme_active(pci_dev, false);
2390
2391 spin_unlock_irq(&dev->power.lock);
2392 return true;
2393}
2394
2395/**
2396 * pci_dev_complete_resume - Finalize resume from system sleep for a device.
2397 * @pci_dev: Device to handle.
2398 *
2399 * If the device is runtime suspended and wakeup-capable, enable PME for it as
2400 * it might have been disabled during the prepare phase of system suspend if
2401 * the device was not configured for system wakeup.
2402 */
2403void pci_dev_complete_resume(struct pci_dev *pci_dev)
2404{
2405 struct device *dev = &pci_dev->dev;
2406
2407 if (!pci_dev_run_wake(pci_dev))
2408 return;
2409
2410 spin_lock_irq(&dev->power.lock);
2411
2412 if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold)
2413 __pci_pme_active(pci_dev, true);
2414
2415 spin_unlock_irq(&dev->power.lock);
Rafael J. Wysockibac2a902015-01-21 02:17:42 +01002416}
2417
Huang Yingb3c32c42012-10-25 09:36:03 +08002418void pci_config_pm_runtime_get(struct pci_dev *pdev)
2419{
2420 struct device *dev = &pdev->dev;
2421 struct device *parent = dev->parent;
2422
2423 if (parent)
2424 pm_runtime_get_sync(parent);
2425 pm_runtime_get_noresume(dev);
2426 /*
2427 * pdev->current_state is set to PCI_D3cold during suspending,
2428 * so wait until suspending completes
2429 */
2430 pm_runtime_barrier(dev);
2431 /*
2432 * Only need to resume devices in D3cold, because config
2433 * registers are still accessible for devices suspended but
2434 * not in D3cold.
2435 */
2436 if (pdev->current_state == PCI_D3cold)
2437 pm_runtime_resume(dev);
2438}
2439
2440void pci_config_pm_runtime_put(struct pci_dev *pdev)
2441{
2442 struct device *dev = &pdev->dev;
2443 struct device *parent = dev->parent;
2444
2445 pm_runtime_put(dev);
2446 if (parent)
2447 pm_runtime_put_sync(parent);
2448}
2449
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01002450/**
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002451 * pci_bridge_d3_possible - Is it possible to put the bridge into D3
2452 * @bridge: Bridge to check
2453 *
2454 * This function checks if it is possible to move the bridge to D3.
2455 * Currently we only allow D3 for recent enough PCIe ports.
2456 */
Lukas Wunnerc6a63302016-10-28 10:52:06 +02002457bool pci_bridge_d3_possible(struct pci_dev *bridge)
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002458{
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002459 if (!pci_is_pcie(bridge))
2460 return false;
2461
2462 switch (pci_pcie_type(bridge)) {
2463 case PCI_EXP_TYPE_ROOT_PORT:
2464 case PCI_EXP_TYPE_UPSTREAM:
2465 case PCI_EXP_TYPE_DOWNSTREAM:
2466 if (pci_bridge_d3_disable)
2467 return false;
Lukas Wunner97a90ae2016-10-28 10:52:06 +02002468
2469 /*
Bjorn Helgaasd98e0922017-02-03 08:53:51 -06002470 * Hotplug interrupts cannot be delivered if the link is down,
2471 * so parents of a hotplug port must stay awake. In addition,
2472 * hotplug ports handled by firmware in System Management Mode
Lukas Wunner97a90ae2016-10-28 10:52:06 +02002473 * may not be put into D3 by the OS (Thunderbolt on non-Macs).
Bjorn Helgaasd98e0922017-02-03 08:53:51 -06002474 * For simplicity, disallow in general for now.
Lukas Wunner97a90ae2016-10-28 10:52:06 +02002475 */
Bjorn Helgaasd98e0922017-02-03 08:53:51 -06002476 if (bridge->is_hotplug_bridge)
Lukas Wunner97a90ae2016-10-28 10:52:06 +02002477 return false;
2478
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002479 if (pci_bridge_d3_force)
2480 return true;
2481
2482 /*
2483 * It should be safe to put PCIe ports from 2015 or newer
2484 * to D3.
2485 */
Andy Shevchenkoac950902018-02-22 14:59:23 +02002486 if (dmi_get_bios_year() >= 2015)
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002487 return true;
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002488 break;
2489 }
2490
2491 return false;
2492}
2493
2494static int pci_dev_check_d3cold(struct pci_dev *dev, void *data)
2495{
2496 bool *d3cold_ok = data;
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002497
Lukas Wunner718a0602016-10-28 10:52:06 +02002498 if (/* The device needs to be allowed to go D3cold ... */
2499 dev->no_d3cold || !dev->d3cold_allowed ||
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002500
Lukas Wunner718a0602016-10-28 10:52:06 +02002501 /* ... and if it is wakeup capable to do so from D3cold. */
2502 (device_may_wakeup(&dev->dev) &&
2503 !pci_pme_capable(dev, PCI_D3cold)) ||
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002504
Lukas Wunner718a0602016-10-28 10:52:06 +02002505 /* If it is a bridge it must be allowed to go to D3. */
Bjorn Helgaasd98e0922017-02-03 08:53:51 -06002506 !pci_power_manageable(dev))
Lukas Wunner718a0602016-10-28 10:52:06 +02002507
2508 *d3cold_ok = false;
2509
2510 return !*d3cold_ok;
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002511}
2512
2513/*
2514 * pci_bridge_d3_update - Update bridge D3 capabilities
2515 * @dev: PCI device which is changed
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002516 *
2517 * Update upstream bridge PM capabilities accordingly depending on if the
2518 * device PM configuration was changed or the device is being removed. The
2519 * change is also propagated upstream.
2520 */
Lukas Wunner1ed276a2016-10-28 10:52:06 +02002521void pci_bridge_d3_update(struct pci_dev *dev)
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002522{
Lukas Wunner1ed276a2016-10-28 10:52:06 +02002523 bool remove = !device_is_registered(&dev->dev);
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002524 struct pci_dev *bridge;
2525 bool d3cold_ok = true;
2526
2527 bridge = pci_upstream_bridge(dev);
2528 if (!bridge || !pci_bridge_d3_possible(bridge))
2529 return;
2530
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002531 /*
Lukas Wunnere8559b712016-10-28 10:52:06 +02002532 * If D3 is currently allowed for the bridge, removing one of its
2533 * children won't change that.
2534 */
2535 if (remove && bridge->bridge_d3)
2536 return;
2537
2538 /*
2539 * If D3 is currently allowed for the bridge and a child is added or
2540 * changed, disallowance of D3 can only be caused by that child, so
2541 * we only need to check that single device, not any of its siblings.
2542 *
2543 * If D3 is currently not allowed for the bridge, checking the device
2544 * first may allow us to skip checking its siblings.
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002545 */
2546 if (!remove)
2547 pci_dev_check_d3cold(dev, &d3cold_ok);
2548
Lukas Wunnere8559b712016-10-28 10:52:06 +02002549 /*
2550 * If D3 is currently not allowed for the bridge, this may be caused
2551 * either by the device being changed/removed or any of its siblings,
2552 * so we need to go through all children to find out if one of them
2553 * continues to block D3.
2554 */
2555 if (d3cold_ok && !bridge->bridge_d3)
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002556 pci_walk_bus(bridge->subordinate, pci_dev_check_d3cold,
2557 &d3cold_ok);
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002558
2559 if (bridge->bridge_d3 != d3cold_ok) {
2560 bridge->bridge_d3 = d3cold_ok;
2561 /* Propagate change to upstream bridges */
Lukas Wunner1ed276a2016-10-28 10:52:06 +02002562 pci_bridge_d3_update(bridge);
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002563 }
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002564}
2565
2566/**
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002567 * pci_d3cold_enable - Enable D3cold for device
2568 * @dev: PCI device to handle
2569 *
2570 * This function can be used in drivers to enable D3cold from the device
2571 * they handle. It also updates upstream PCI bridge PM capabilities
2572 * accordingly.
2573 */
2574void pci_d3cold_enable(struct pci_dev *dev)
2575{
2576 if (dev->no_d3cold) {
2577 dev->no_d3cold = false;
Lukas Wunner1ed276a2016-10-28 10:52:06 +02002578 pci_bridge_d3_update(dev);
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002579 }
2580}
2581EXPORT_SYMBOL_GPL(pci_d3cold_enable);
2582
2583/**
2584 * pci_d3cold_disable - Disable D3cold for device
2585 * @dev: PCI device to handle
2586 *
2587 * This function can be used in drivers to disable D3cold from the device
2588 * they handle. It also updates upstream PCI bridge PM capabilities
2589 * accordingly.
2590 */
2591void pci_d3cold_disable(struct pci_dev *dev)
2592{
2593 if (!dev->no_d3cold) {
2594 dev->no_d3cold = true;
Lukas Wunner1ed276a2016-10-28 10:52:06 +02002595 pci_bridge_d3_update(dev);
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002596 }
2597}
2598EXPORT_SYMBOL_GPL(pci_d3cold_disable);
2599
2600/**
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002601 * pci_pm_init - Initialize PM functions of given PCI device
2602 * @dev: PCI device to handle.
2603 */
2604void pci_pm_init(struct pci_dev *dev)
2605{
2606 int pm;
2607 u16 pmc;
David Brownell075c1772007-04-26 00:12:06 -07002608
Rafael J. Wysockibb910a72010-02-27 21:37:37 +01002609 pm_runtime_forbid(&dev->dev);
Huang Ying967577b2012-11-20 16:08:22 +08002610 pm_runtime_set_active(&dev->dev);
2611 pm_runtime_enable(&dev->dev);
Rafael J. Wysockia1e4d722010-02-08 19:16:33 +01002612 device_enable_async_suspend(&dev->dev);
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02002613 dev->wakeup_prepared = false;
Rafael J. Wysockibb910a72010-02-27 21:37:37 +01002614
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002615 dev->pm_cap = 0;
Rafael J. Wysockiffaddbe2013-04-10 10:32:51 +00002616 dev->pme_support = 0;
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 /* find PCI PM capability in list */
2619 pm = pci_find_capability(dev, PCI_CAP_ID_PM);
David Brownell075c1772007-04-26 00:12:06 -07002620 if (!pm)
Linus Torvalds50246dd2009-01-16 08:14:51 -08002621 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 /* Check device's ability to generate PME# */
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002623 pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002625 if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06002626 pci_err(dev, "unsupported PM cap regs version (%u)\n",
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002627 pmc & PCI_PM_CAP_VER_MASK);
Linus Torvalds50246dd2009-01-16 08:14:51 -08002628 return;
David Brownell075c1772007-04-26 00:12:06 -07002629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002631 dev->pm_cap = pm;
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +01002632 dev->d3_delay = PCI_PM_D3_WAIT;
Huang Ying448bd852012-06-23 10:23:51 +08002633 dev->d3cold_delay = PCI_PM_D3COLD_WAIT;
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002634 dev->bridge_d3 = pci_bridge_d3_possible(dev);
Huang Ying4f9c1392012-08-08 09:07:38 +08002635 dev->d3cold_allowed = true;
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002636
2637 dev->d1_support = false;
2638 dev->d2_support = false;
2639 if (!pci_no_d1d2(dev)) {
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06002640 if (pmc & PCI_PM_CAP_D1)
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002641 dev->d1_support = true;
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06002642 if (pmc & PCI_PM_CAP_D2)
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002643 dev->d2_support = true;
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06002644
2645 if (dev->d1_support || dev->d2_support)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002646 pci_printk(KERN_DEBUG, dev, "supports%s%s\n",
Jesse Barnesec84f122008-09-23 11:43:34 -07002647 dev->d1_support ? " D1" : "",
2648 dev->d2_support ? " D2" : "");
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002649 }
2650
2651 pmc &= PCI_PM_CAP_PME_MASK;
2652 if (pmc) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06002653 pci_printk(KERN_DEBUG, dev, "PME# supported from%s%s%s%s%s\n",
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06002654 (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
2655 (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
2656 (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
2657 (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
2658 (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002659 dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02002660 dev->pme_poll = true;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002661 /*
2662 * Make device's PM flags reflect the wake-up capability, but
2663 * let the user space enable it to wake up the system as needed.
2664 */
2665 device_set_wakeup_capable(&dev->dev, true);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002666 /* Disable the PME# generation functionality */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002667 pci_pme_active(dev, false);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
Sean O. Stalley938174e2015-10-29 17:35:39 -05002671static unsigned long pci_ea_flags(struct pci_dev *dev, u8 prop)
2672{
Alex Williamson92efb1b2016-05-16 15:12:02 -05002673 unsigned long flags = IORESOURCE_PCI_FIXED | IORESOURCE_PCI_EA_BEI;
Sean O. Stalley938174e2015-10-29 17:35:39 -05002674
2675 switch (prop) {
2676 case PCI_EA_P_MEM:
2677 case PCI_EA_P_VF_MEM:
2678 flags |= IORESOURCE_MEM;
2679 break;
2680 case PCI_EA_P_MEM_PREFETCH:
2681 case PCI_EA_P_VF_MEM_PREFETCH:
2682 flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
2683 break;
2684 case PCI_EA_P_IO:
2685 flags |= IORESOURCE_IO;
2686 break;
2687 default:
2688 return 0;
2689 }
2690
2691 return flags;
2692}
2693
2694static struct resource *pci_ea_get_resource(struct pci_dev *dev, u8 bei,
2695 u8 prop)
2696{
2697 if (bei <= PCI_EA_BEI_BAR5 && prop <= PCI_EA_P_IO)
2698 return &dev->resource[bei];
David Daney11183992015-10-29 17:35:40 -05002699#ifdef CONFIG_PCI_IOV
2700 else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5 &&
2701 (prop == PCI_EA_P_VF_MEM || prop == PCI_EA_P_VF_MEM_PREFETCH))
2702 return &dev->resource[PCI_IOV_RESOURCES +
2703 bei - PCI_EA_BEI_VF_BAR0];
2704#endif
Sean O. Stalley938174e2015-10-29 17:35:39 -05002705 else if (bei == PCI_EA_BEI_ROM)
2706 return &dev->resource[PCI_ROM_RESOURCE];
2707 else
2708 return NULL;
2709}
2710
2711/* Read an Enhanced Allocation (EA) entry */
2712static int pci_ea_read(struct pci_dev *dev, int offset)
2713{
2714 struct resource *res;
2715 int ent_size, ent_offset = offset;
2716 resource_size_t start, end;
2717 unsigned long flags;
Bjorn Helgaas26635112015-10-29 17:35:40 -05002718 u32 dw0, bei, base, max_offset;
Sean O. Stalley938174e2015-10-29 17:35:39 -05002719 u8 prop;
2720 bool support_64 = (sizeof(resource_size_t) >= 8);
2721
2722 pci_read_config_dword(dev, ent_offset, &dw0);
2723 ent_offset += 4;
2724
2725 /* Entry size field indicates DWORDs after 1st */
2726 ent_size = ((dw0 & PCI_EA_ES) + 1) << 2;
2727
2728 if (!(dw0 & PCI_EA_ENABLE)) /* Entry not enabled */
2729 goto out;
2730
Bjorn Helgaas26635112015-10-29 17:35:40 -05002731 bei = (dw0 & PCI_EA_BEI) >> 4;
2732 prop = (dw0 & PCI_EA_PP) >> 8;
2733
Sean O. Stalley938174e2015-10-29 17:35:39 -05002734 /*
2735 * If the Property is in the reserved range, try the Secondary
2736 * Property instead.
2737 */
2738 if (prop > PCI_EA_P_BRIDGE_IO && prop < PCI_EA_P_MEM_RESERVED)
Bjorn Helgaas26635112015-10-29 17:35:40 -05002739 prop = (dw0 & PCI_EA_SP) >> 16;
Sean O. Stalley938174e2015-10-29 17:35:39 -05002740 if (prop > PCI_EA_P_BRIDGE_IO)
2741 goto out;
2742
Bjorn Helgaas26635112015-10-29 17:35:40 -05002743 res = pci_ea_get_resource(dev, bei, prop);
Sean O. Stalley938174e2015-10-29 17:35:39 -05002744 if (!res) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06002745 pci_err(dev, "Unsupported EA entry BEI: %u\n", bei);
Sean O. Stalley938174e2015-10-29 17:35:39 -05002746 goto out;
2747 }
2748
2749 flags = pci_ea_flags(dev, prop);
2750 if (!flags) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06002751 pci_err(dev, "Unsupported EA properties: %#x\n", prop);
Sean O. Stalley938174e2015-10-29 17:35:39 -05002752 goto out;
2753 }
2754
2755 /* Read Base */
2756 pci_read_config_dword(dev, ent_offset, &base);
2757 start = (base & PCI_EA_FIELD_MASK);
2758 ent_offset += 4;
2759
2760 /* Read MaxOffset */
2761 pci_read_config_dword(dev, ent_offset, &max_offset);
2762 ent_offset += 4;
2763
2764 /* Read Base MSBs (if 64-bit entry) */
2765 if (base & PCI_EA_IS_64) {
2766 u32 base_upper;
2767
2768 pci_read_config_dword(dev, ent_offset, &base_upper);
2769 ent_offset += 4;
2770
2771 flags |= IORESOURCE_MEM_64;
2772
2773 /* entry starts above 32-bit boundary, can't use */
2774 if (!support_64 && base_upper)
2775 goto out;
2776
2777 if (support_64)
2778 start |= ((u64)base_upper << 32);
2779 }
2780
2781 end = start + (max_offset | 0x03);
2782
2783 /* Read MaxOffset MSBs (if 64-bit entry) */
2784 if (max_offset & PCI_EA_IS_64) {
2785 u32 max_offset_upper;
2786
2787 pci_read_config_dword(dev, ent_offset, &max_offset_upper);
2788 ent_offset += 4;
2789
2790 flags |= IORESOURCE_MEM_64;
2791
2792 /* entry too big, can't use */
2793 if (!support_64 && max_offset_upper)
2794 goto out;
2795
2796 if (support_64)
2797 end += ((u64)max_offset_upper << 32);
2798 }
2799
2800 if (end < start) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06002801 pci_err(dev, "EA Entry crosses address boundary\n");
Sean O. Stalley938174e2015-10-29 17:35:39 -05002802 goto out;
2803 }
2804
2805 if (ent_size != ent_offset - offset) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06002806 pci_err(dev, "EA Entry Size (%d) does not match length read (%d)\n",
Sean O. Stalley938174e2015-10-29 17:35:39 -05002807 ent_size, ent_offset - offset);
2808 goto out;
2809 }
2810
2811 res->name = pci_name(dev);
2812 res->start = start;
2813 res->end = end;
2814 res->flags = flags;
Bjorn Helgaas597becb2015-10-29 17:35:40 -05002815
2816 if (bei <= PCI_EA_BEI_BAR5)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002817 pci_printk(KERN_DEBUG, dev, "BAR %d: %pR (from Enhanced Allocation, properties %#02x)\n",
Bjorn Helgaas597becb2015-10-29 17:35:40 -05002818 bei, res, prop);
2819 else if (bei == PCI_EA_BEI_ROM)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002820 pci_printk(KERN_DEBUG, dev, "ROM: %pR (from Enhanced Allocation, properties %#02x)\n",
Bjorn Helgaas597becb2015-10-29 17:35:40 -05002821 res, prop);
2822 else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002823 pci_printk(KERN_DEBUG, dev, "VF BAR %d: %pR (from Enhanced Allocation, properties %#02x)\n",
Bjorn Helgaas597becb2015-10-29 17:35:40 -05002824 bei - PCI_EA_BEI_VF_BAR0, res, prop);
2825 else
Frederick Lawler7506dc72018-01-18 12:55:24 -06002826 pci_printk(KERN_DEBUG, dev, "BEI %d res: %pR (from Enhanced Allocation, properties %#02x)\n",
Bjorn Helgaas597becb2015-10-29 17:35:40 -05002827 bei, res, prop);
2828
Sean O. Stalley938174e2015-10-29 17:35:39 -05002829out:
2830 return offset + ent_size;
2831}
2832
Colin Ian Kingdcbb4082016-04-05 12:12:45 -05002833/* Enhanced Allocation Initialization */
Sean O. Stalley938174e2015-10-29 17:35:39 -05002834void pci_ea_init(struct pci_dev *dev)
2835{
2836 int ea;
2837 u8 num_ent;
2838 int offset;
2839 int i;
2840
2841 /* find PCI EA capability in list */
2842 ea = pci_find_capability(dev, PCI_CAP_ID_EA);
2843 if (!ea)
2844 return;
2845
2846 /* determine the number of entries */
2847 pci_bus_read_config_byte(dev->bus, dev->devfn, ea + PCI_EA_NUM_ENT,
2848 &num_ent);
2849 num_ent &= PCI_EA_NUM_ENT_MASK;
2850
2851 offset = ea + PCI_EA_FIRST_ENT;
2852
2853 /* Skip DWORD 2 for type 1 functions */
2854 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
2855 offset += 4;
2856
2857 /* parse each EA entry */
2858 for (i = 0; i < num_ent; ++i)
2859 offset = pci_ea_read(dev, offset);
2860}
2861
Yinghai Lu34a48762012-02-11 00:18:41 -08002862static void pci_add_saved_cap(struct pci_dev *pci_dev,
2863 struct pci_cap_saved_state *new_cap)
2864{
2865 hlist_add_head(&new_cap->next, &pci_dev->saved_cap_space);
2866}
2867
Jesse Barneseb9c39d2008-12-17 12:10:05 -08002868/**
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002869 * _pci_add_cap_save_buffer - allocate buffer for saving given
2870 * capability registers
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002871 * @dev: the PCI device
2872 * @cap: the capability to allocate the buffer for
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002873 * @extended: Standard or Extended capability ID
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002874 * @size: requested size of the buffer
2875 */
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002876static int _pci_add_cap_save_buffer(struct pci_dev *dev, u16 cap,
2877 bool extended, unsigned int size)
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002878{
2879 int pos;
2880 struct pci_cap_saved_state *save_state;
2881
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002882 if (extended)
2883 pos = pci_find_ext_capability(dev, cap);
2884 else
2885 pos = pci_find_capability(dev, cap);
2886
Wei Yang0a1a9b42015-06-30 09:16:44 +08002887 if (!pos)
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002888 return 0;
2889
2890 save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
2891 if (!save_state)
2892 return -ENOMEM;
2893
Alex Williamson24a4742f2011-05-10 10:02:11 -06002894 save_state->cap.cap_nr = cap;
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002895 save_state->cap.cap_extended = extended;
Alex Williamson24a4742f2011-05-10 10:02:11 -06002896 save_state->cap.size = size;
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002897 pci_add_saved_cap(dev, save_state);
2898
2899 return 0;
2900}
2901
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002902int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size)
2903{
2904 return _pci_add_cap_save_buffer(dev, cap, false, size);
2905}
2906
2907int pci_add_ext_cap_save_buffer(struct pci_dev *dev, u16 cap, unsigned int size)
2908{
2909 return _pci_add_cap_save_buffer(dev, cap, true, size);
2910}
2911
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002912/**
2913 * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
2914 * @dev: the PCI device
2915 */
2916void pci_allocate_cap_save_buffers(struct pci_dev *dev)
2917{
2918 int error;
2919
Yu Zhao89858512009-02-16 02:55:47 +08002920 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP,
2921 PCI_EXP_SAVE_REGS * sizeof(u16));
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002922 if (error)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002923 pci_err(dev, "unable to preallocate PCI Express save buffer\n");
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002924
2925 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
2926 if (error)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002927 pci_err(dev, "unable to preallocate PCI-X save buffer\n");
Alex Williamson425c1b22013-12-17 16:43:51 -07002928
2929 pci_allocate_vc_save_buffers(dev);
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002930}
2931
Yinghai Luf7968412012-02-11 00:18:30 -08002932void pci_free_cap_save_buffers(struct pci_dev *dev)
2933{
2934 struct pci_cap_saved_state *tmp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002935 struct hlist_node *n;
Yinghai Luf7968412012-02-11 00:18:30 -08002936
Sasha Levinb67bfe02013-02-27 17:06:00 -08002937 hlist_for_each_entry_safe(tmp, n, &dev->saved_cap_space, next)
Yinghai Luf7968412012-02-11 00:18:30 -08002938 kfree(tmp);
2939}
2940
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002941/**
Yijing Wang31ab2472013-01-15 11:12:17 +08002942 * pci_configure_ari - enable or disable ARI forwarding
Yu Zhao58c3a722008-10-14 14:02:53 +08002943 * @dev: the PCI device
Yijing Wangb0cc6022013-01-15 11:12:16 +08002944 *
2945 * If @dev and its upstream bridge both support ARI, enable ARI in the
2946 * bridge. Otherwise, disable ARI in the bridge.
Yu Zhao58c3a722008-10-14 14:02:53 +08002947 */
Yijing Wang31ab2472013-01-15 11:12:17 +08002948void pci_configure_ari(struct pci_dev *dev)
Yu Zhao58c3a722008-10-14 14:02:53 +08002949{
Yu Zhao58c3a722008-10-14 14:02:53 +08002950 u32 cap;
Zhao, Yu81135872008-10-23 13:15:39 +08002951 struct pci_dev *bridge;
Yu Zhao58c3a722008-10-14 14:02:53 +08002952
Rafael J. Wysocki6748dcc2012-03-01 00:06:33 +01002953 if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
Yu Zhao58c3a722008-10-14 14:02:53 +08002954 return;
2955
Zhao, Yu81135872008-10-23 13:15:39 +08002956 bridge = dev->bus->self;
Myron Stowecb97ae32012-06-01 15:16:31 -06002957 if (!bridge)
Zhao, Yu81135872008-10-23 13:15:39 +08002958 return;
2959
Jiang Liu59875ae2012-07-24 17:20:06 +08002960 pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
Yu Zhao58c3a722008-10-14 14:02:53 +08002961 if (!(cap & PCI_EXP_DEVCAP2_ARI))
2962 return;
2963
Yijing Wangb0cc6022013-01-15 11:12:16 +08002964 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
2965 pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
2966 PCI_EXP_DEVCTL2_ARI);
2967 bridge->ari_enabled = 1;
2968 } else {
2969 pcie_capability_clear_word(bridge, PCI_EXP_DEVCTL2,
2970 PCI_EXP_DEVCTL2_ARI);
2971 bridge->ari_enabled = 0;
2972 }
Yu Zhao58c3a722008-10-14 14:02:53 +08002973}
2974
Chris Wright5d990b62009-12-04 12:15:21 -08002975static int pci_acs_enable;
2976
2977/**
2978 * pci_request_acs - ask for ACS to be enabled if supported
2979 */
2980void pci_request_acs(void)
2981{
2982 pci_acs_enable = 1;
2983}
2984
Logan Gunthorpeaaca43f2018-07-30 10:18:40 -06002985static const char *disable_acs_redir_param;
2986
2987/**
2988 * pci_disable_acs_redir - disable ACS redirect capabilities
2989 * @dev: the PCI device
2990 *
2991 * For only devices specified in the disable_acs_redir parameter.
2992 */
2993static void pci_disable_acs_redir(struct pci_dev *dev)
2994{
2995 int ret = 0;
2996 const char *p;
2997 int pos;
2998 u16 ctrl;
2999
3000 if (!disable_acs_redir_param)
3001 return;
3002
3003 p = disable_acs_redir_param;
3004 while (*p) {
3005 ret = pci_dev_str_match(dev, p, &p);
3006 if (ret < 0) {
3007 pr_info_once("PCI: Can't parse disable_acs_redir parameter: %s\n",
3008 disable_acs_redir_param);
3009
3010 break;
3011 } else if (ret == 1) {
3012 /* Found a match */
3013 break;
3014 }
3015
3016 if (*p != ';' && *p != ',') {
3017 /* End of param or invalid format */
3018 break;
3019 }
3020 p++;
3021 }
3022
3023 if (ret != 1)
3024 return;
3025
Logan Gunthorpe73c47dde2018-08-09 16:51:43 -05003026 if (!pci_dev_specific_disable_acs_redir(dev))
3027 return;
3028
Logan Gunthorpeaaca43f2018-07-30 10:18:40 -06003029 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
3030 if (!pos) {
3031 pci_warn(dev, "cannot disable ACS redirect for this hardware as it does not have ACS capabilities\n");
3032 return;
3033 }
3034
3035 pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
3036
3037 /* P2P Request & Completion Redirect */
3038 ctrl &= ~(PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_EC);
3039
3040 pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
3041
3042 pci_info(dev, "disabled ACS redirect\n");
3043}
3044
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07003045/**
Alex Williamson2c744242014-02-03 14:27:33 -07003046 * pci_std_enable_acs - enable ACS on devices using standard ACS capabilites
Allen Kayae21ee62009-10-07 10:27:17 -07003047 * @dev: the PCI device
3048 */
Alex Williamsonc1d61c92016-03-31 16:34:32 -06003049static void pci_std_enable_acs(struct pci_dev *dev)
Allen Kayae21ee62009-10-07 10:27:17 -07003050{
3051 int pos;
3052 u16 cap;
3053 u16 ctrl;
3054
Allen Kayae21ee62009-10-07 10:27:17 -07003055 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
3056 if (!pos)
Alex Williamsonc1d61c92016-03-31 16:34:32 -06003057 return;
Allen Kayae21ee62009-10-07 10:27:17 -07003058
3059 pci_read_config_word(dev, pos + PCI_ACS_CAP, &cap);
3060 pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
3061
3062 /* Source Validation */
3063 ctrl |= (cap & PCI_ACS_SV);
3064
3065 /* P2P Request Redirect */
3066 ctrl |= (cap & PCI_ACS_RR);
3067
3068 /* P2P Completion Redirect */
3069 ctrl |= (cap & PCI_ACS_CR);
3070
3071 /* Upstream Forwarding */
3072 ctrl |= (cap & PCI_ACS_UF);
3073
3074 pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
Alex Williamson2c744242014-02-03 14:27:33 -07003075}
3076
3077/**
3078 * pci_enable_acs - enable ACS if hardware support it
3079 * @dev: the PCI device
3080 */
3081void pci_enable_acs(struct pci_dev *dev)
3082{
3083 if (!pci_acs_enable)
Logan Gunthorpeaaca43f2018-07-30 10:18:40 -06003084 goto disable_acs_redir;
Alex Williamson2c744242014-02-03 14:27:33 -07003085
Alex Williamsonc1d61c92016-03-31 16:34:32 -06003086 if (!pci_dev_specific_enable_acs(dev))
Logan Gunthorpeaaca43f2018-07-30 10:18:40 -06003087 goto disable_acs_redir;
Alex Williamson2c744242014-02-03 14:27:33 -07003088
Alex Williamsonc1d61c92016-03-31 16:34:32 -06003089 pci_std_enable_acs(dev);
Logan Gunthorpeaaca43f2018-07-30 10:18:40 -06003090
3091disable_acs_redir:
3092 /*
3093 * Note: pci_disable_acs_redir() must be called even if ACS was not
3094 * enabled by the kernel because it may have been enabled by
3095 * platform firmware. So if we are told to disable it, we should
3096 * always disable it after setting the kernel's default
3097 * preferences.
3098 */
3099 pci_disable_acs_redir(dev);
Allen Kayae21ee62009-10-07 10:27:17 -07003100}
3101
Alex Williamson0a671192013-06-27 16:39:48 -06003102static bool pci_acs_flags_enabled(struct pci_dev *pdev, u16 acs_flags)
3103{
3104 int pos;
Alex Williamson83db7e02013-06-27 16:39:54 -06003105 u16 cap, ctrl;
Alex Williamson0a671192013-06-27 16:39:48 -06003106
3107 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ACS);
3108 if (!pos)
3109 return false;
3110
Alex Williamson83db7e02013-06-27 16:39:54 -06003111 /*
3112 * Except for egress control, capabilities are either required
3113 * or only required if controllable. Features missing from the
3114 * capability field can therefore be assumed as hard-wired enabled.
3115 */
3116 pci_read_config_word(pdev, pos + PCI_ACS_CAP, &cap);
3117 acs_flags &= (cap | PCI_ACS_EC);
3118
Alex Williamson0a671192013-06-27 16:39:48 -06003119 pci_read_config_word(pdev, pos + PCI_ACS_CTRL, &ctrl);
3120 return (ctrl & acs_flags) == acs_flags;
3121}
3122
Allen Kayae21ee62009-10-07 10:27:17 -07003123/**
Alex Williamsonad805752012-06-11 05:27:07 +00003124 * pci_acs_enabled - test ACS against required flags for a given device
3125 * @pdev: device to test
3126 * @acs_flags: required PCI ACS flags
3127 *
3128 * Return true if the device supports the provided flags. Automatically
3129 * filters out flags that are not implemented on multifunction devices.
Alex Williamson0a671192013-06-27 16:39:48 -06003130 *
3131 * Note that this interface checks the effective ACS capabilities of the
3132 * device rather than the actual capabilities. For instance, most single
3133 * function endpoints are not required to support ACS because they have no
3134 * opportunity for peer-to-peer access. We therefore return 'true'
3135 * regardless of whether the device exposes an ACS capability. This makes
3136 * it much easier for callers of this function to ignore the actual type
3137 * or topology of the device when testing ACS support.
Alex Williamsonad805752012-06-11 05:27:07 +00003138 */
3139bool pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags)
3140{
Alex Williamson0a671192013-06-27 16:39:48 -06003141 int ret;
Alex Williamsonad805752012-06-11 05:27:07 +00003142
3143 ret = pci_dev_specific_acs_enabled(pdev, acs_flags);
3144 if (ret >= 0)
3145 return ret > 0;
3146
Alex Williamson0a671192013-06-27 16:39:48 -06003147 /*
3148 * Conventional PCI and PCI-X devices never support ACS, either
3149 * effectively or actually. The shared bus topology implies that
3150 * any device on the bus can receive or snoop DMA.
3151 */
Alex Williamsonad805752012-06-11 05:27:07 +00003152 if (!pci_is_pcie(pdev))
3153 return false;
3154
Alex Williamson0a671192013-06-27 16:39:48 -06003155 switch (pci_pcie_type(pdev)) {
3156 /*
3157 * PCI/X-to-PCIe bridges are not specifically mentioned by the spec,
Bjorn Helgaasf7625982013-11-14 11:28:18 -07003158 * but since their primary interface is PCI/X, we conservatively
Alex Williamson0a671192013-06-27 16:39:48 -06003159 * handle them as we would a non-PCIe device.
3160 */
3161 case PCI_EXP_TYPE_PCIE_BRIDGE:
3162 /*
3163 * PCIe 3.0, 6.12.1 excludes ACS on these devices. "ACS is never
3164 * applicable... must never implement an ACS Extended Capability...".
3165 * This seems arbitrary, but we take a conservative interpretation
3166 * of this statement.
3167 */
3168 case PCI_EXP_TYPE_PCI_BRIDGE:
3169 case PCI_EXP_TYPE_RC_EC:
3170 return false;
3171 /*
3172 * PCIe 3.0, 6.12.1.1 specifies that downstream and root ports should
3173 * implement ACS in order to indicate their peer-to-peer capabilities,
3174 * regardless of whether they are single- or multi-function devices.
3175 */
3176 case PCI_EXP_TYPE_DOWNSTREAM:
3177 case PCI_EXP_TYPE_ROOT_PORT:
3178 return pci_acs_flags_enabled(pdev, acs_flags);
3179 /*
3180 * PCIe 3.0, 6.12.1.2 specifies ACS capabilities that should be
3181 * implemented by the remaining PCIe types to indicate peer-to-peer
Bjorn Helgaasf7625982013-11-14 11:28:18 -07003182 * capabilities, but only when they are part of a multifunction
Alex Williamson0a671192013-06-27 16:39:48 -06003183 * device. The footnote for section 6.12 indicates the specific
3184 * PCIe types included here.
3185 */
3186 case PCI_EXP_TYPE_ENDPOINT:
3187 case PCI_EXP_TYPE_UPSTREAM:
3188 case PCI_EXP_TYPE_LEG_END:
3189 case PCI_EXP_TYPE_RC_END:
3190 if (!pdev->multifunction)
3191 break;
3192
Alex Williamson0a671192013-06-27 16:39:48 -06003193 return pci_acs_flags_enabled(pdev, acs_flags);
Alex Williamsonad805752012-06-11 05:27:07 +00003194 }
3195
Alex Williamson0a671192013-06-27 16:39:48 -06003196 /*
Bjorn Helgaasf7625982013-11-14 11:28:18 -07003197 * PCIe 3.0, 6.12.1.3 specifies no ACS capabilities are applicable
Alex Williamson0a671192013-06-27 16:39:48 -06003198 * to single function devices with the exception of downstream ports.
3199 */
Alex Williamsonad805752012-06-11 05:27:07 +00003200 return true;
3201}
3202
3203/**
3204 * pci_acs_path_enable - test ACS flags from start to end in a hierarchy
3205 * @start: starting downstream device
3206 * @end: ending upstream device or NULL to search to the root bus
3207 * @acs_flags: required flags
3208 *
3209 * Walk up a device tree from start to end testing PCI ACS support. If
3210 * any step along the way does not support the required flags, return false.
3211 */
3212bool pci_acs_path_enabled(struct pci_dev *start,
3213 struct pci_dev *end, u16 acs_flags)
3214{
3215 struct pci_dev *pdev, *parent = start;
3216
3217 do {
3218 pdev = parent;
3219
3220 if (!pci_acs_enabled(pdev, acs_flags))
3221 return false;
3222
3223 if (pci_is_root_bus(pdev->bus))
3224 return (end == NULL);
3225
3226 parent = pdev->bus->self;
3227 } while (pdev != end);
3228
3229 return true;
3230}
3231
3232/**
Christian König276b7382017-10-24 14:40:20 -05003233 * pci_rebar_find_pos - find position of resize ctrl reg for BAR
3234 * @pdev: PCI device
3235 * @bar: BAR to find
3236 *
3237 * Helper to find the position of the ctrl register for a BAR.
3238 * Returns -ENOTSUPP if resizable BARs are not supported at all.
3239 * Returns -ENOENT if no ctrl register for the BAR could be found.
3240 */
3241static int pci_rebar_find_pos(struct pci_dev *pdev, int bar)
3242{
3243 unsigned int pos, nbars, i;
3244 u32 ctrl;
3245
3246 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_REBAR);
3247 if (!pos)
3248 return -ENOTSUPP;
3249
3250 pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
3251 nbars = (ctrl & PCI_REBAR_CTRL_NBAR_MASK) >>
3252 PCI_REBAR_CTRL_NBAR_SHIFT;
3253
3254 for (i = 0; i < nbars; i++, pos += 8) {
3255 int bar_idx;
3256
3257 pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
3258 bar_idx = ctrl & PCI_REBAR_CTRL_BAR_IDX;
3259 if (bar_idx == bar)
3260 return pos;
3261 }
3262
3263 return -ENOENT;
3264}
3265
3266/**
3267 * pci_rebar_get_possible_sizes - get possible sizes for BAR
3268 * @pdev: PCI device
3269 * @bar: BAR to query
3270 *
3271 * Get the possible sizes of a resizable BAR as bitmask defined in the spec
3272 * (bit 0=1MB, bit 19=512GB). Returns 0 if BAR isn't resizable.
3273 */
3274u32 pci_rebar_get_possible_sizes(struct pci_dev *pdev, int bar)
3275{
3276 int pos;
3277 u32 cap;
3278
3279 pos = pci_rebar_find_pos(pdev, bar);
3280 if (pos < 0)
3281 return 0;
3282
3283 pci_read_config_dword(pdev, pos + PCI_REBAR_CAP, &cap);
3284 return (cap & PCI_REBAR_CAP_SIZES) >> 4;
3285}
3286
3287/**
3288 * pci_rebar_get_current_size - get the current size of a BAR
3289 * @pdev: PCI device
3290 * @bar: BAR to set size to
3291 *
3292 * Read the size of a BAR from the resizable BAR config.
3293 * Returns size if found or negative error code.
3294 */
3295int pci_rebar_get_current_size(struct pci_dev *pdev, int bar)
3296{
3297 int pos;
3298 u32 ctrl;
3299
3300 pos = pci_rebar_find_pos(pdev, bar);
3301 if (pos < 0)
3302 return pos;
3303
3304 pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
3305 return (ctrl & PCI_REBAR_CTRL_BAR_SIZE) >> 8;
3306}
3307
3308/**
3309 * pci_rebar_set_size - set a new size for a BAR
3310 * @pdev: PCI device
3311 * @bar: BAR to set size to
3312 * @size: new size as defined in the spec (0=1MB, 19=512GB)
3313 *
3314 * Set the new size of a BAR as defined in the spec.
3315 * Returns zero if resizing was successful, error code otherwise.
3316 */
3317int pci_rebar_set_size(struct pci_dev *pdev, int bar, int size)
3318{
3319 int pos;
3320 u32 ctrl;
3321
3322 pos = pci_rebar_find_pos(pdev, bar);
3323 if (pos < 0)
3324 return pos;
3325
3326 pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
3327 ctrl &= ~PCI_REBAR_CTRL_BAR_SIZE;
3328 ctrl |= size << 8;
3329 pci_write_config_dword(pdev, pos + PCI_REBAR_CTRL, ctrl);
3330 return 0;
3331}
3332
3333/**
Jay Cornwall430a2362018-01-04 19:44:59 -05003334 * pci_enable_atomic_ops_to_root - enable AtomicOp requests to root port
3335 * @dev: the PCI device
3336 * @cap_mask: mask of desired AtomicOp sizes, including one or more of:
3337 * PCI_EXP_DEVCAP2_ATOMIC_COMP32
3338 * PCI_EXP_DEVCAP2_ATOMIC_COMP64
3339 * PCI_EXP_DEVCAP2_ATOMIC_COMP128
3340 *
3341 * Return 0 if all upstream bridges support AtomicOp routing, egress
3342 * blocking is disabled on all upstream ports, and the root port supports
3343 * the requested completion capabilities (32-bit, 64-bit and/or 128-bit
3344 * AtomicOp completion), or negative otherwise.
3345 */
3346int pci_enable_atomic_ops_to_root(struct pci_dev *dev, u32 cap_mask)
3347{
3348 struct pci_bus *bus = dev->bus;
3349 struct pci_dev *bridge;
3350 u32 cap, ctl2;
3351
3352 if (!pci_is_pcie(dev))
3353 return -EINVAL;
3354
3355 /*
3356 * Per PCIe r4.0, sec 6.15, endpoints and root ports may be
3357 * AtomicOp requesters. For now, we only support endpoints as
3358 * requesters and root ports as completers. No endpoints as
3359 * completers, and no peer-to-peer.
3360 */
3361
3362 switch (pci_pcie_type(dev)) {
3363 case PCI_EXP_TYPE_ENDPOINT:
3364 case PCI_EXP_TYPE_LEG_END:
3365 case PCI_EXP_TYPE_RC_END:
3366 break;
3367 default:
3368 return -EINVAL;
3369 }
3370
3371 while (bus->parent) {
3372 bridge = bus->self;
3373
3374 pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
3375
3376 switch (pci_pcie_type(bridge)) {
3377 /* Ensure switch ports support AtomicOp routing */
3378 case PCI_EXP_TYPE_UPSTREAM:
3379 case PCI_EXP_TYPE_DOWNSTREAM:
3380 if (!(cap & PCI_EXP_DEVCAP2_ATOMIC_ROUTE))
3381 return -EINVAL;
3382 break;
3383
3384 /* Ensure root port supports all the sizes we care about */
3385 case PCI_EXP_TYPE_ROOT_PORT:
3386 if ((cap & cap_mask) != cap_mask)
3387 return -EINVAL;
3388 break;
3389 }
3390
3391 /* Ensure upstream ports don't block AtomicOps on egress */
3392 if (!bridge->has_secondary_link) {
3393 pcie_capability_read_dword(bridge, PCI_EXP_DEVCTL2,
3394 &ctl2);
3395 if (ctl2 & PCI_EXP_DEVCTL2_ATOMIC_EGRESS_BLOCK)
3396 return -EINVAL;
3397 }
3398
3399 bus = bus->parent;
3400 }
3401
3402 pcie_capability_set_word(dev, PCI_EXP_DEVCTL2,
3403 PCI_EXP_DEVCTL2_ATOMIC_REQ);
3404 return 0;
3405}
3406EXPORT_SYMBOL(pci_enable_atomic_ops_to_root);
3407
3408/**
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07003409 * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
3410 * @dev: the PCI device
Wang Sheng-Huibb5c2de2013-05-28 11:17:41 +08003411 * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD)
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07003412 *
3413 * Perform INTx swizzling for a device behind one level of bridge. This is
3414 * required by section 9.1 of the PCI-to-PCI bridge specification for devices
Matthew Wilcox46b952a2009-07-01 14:24:30 -07003415 * behind bridges on add-in cards. For devices with ARI enabled, the slot
3416 * number is always 0 (see the Implementation Note in section 2.2.8.1 of
3417 * the PCI Express Base Specification, Revision 2.1)
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07003418 */
John Crispin3df425f2012-04-12 17:33:07 +02003419u8 pci_swizzle_interrupt_pin(const struct pci_dev *dev, u8 pin)
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07003420{
Matthew Wilcox46b952a2009-07-01 14:24:30 -07003421 int slot;
3422
3423 if (pci_ari_enabled(dev->bus))
3424 slot = 0;
3425 else
3426 slot = PCI_SLOT(dev->devfn);
3427
3428 return (((pin - 1) + slot) % 4) + 1;
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07003429}
3430
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003431int pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432{
3433 u8 pin;
3434
Kristen Accardi514d2072005-11-02 16:24:39 -08003435 pin = dev->pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 if (!pin)
3437 return -1;
Bjorn Helgaas878f2e52008-12-09 16:11:46 -07003438
Kenji Kaneshige8784fd42009-05-26 16:07:33 +09003439 while (!pci_is_root_bus(dev->bus)) {
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07003440 pin = pci_swizzle_interrupt_pin(dev, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 dev = dev->bus->self;
3442 }
3443 *bridge = dev;
3444 return pin;
3445}
3446
3447/**
Bjorn Helgaas68feac82008-12-16 21:36:55 -07003448 * pci_common_swizzle - swizzle INTx all the way to root bridge
3449 * @dev: the PCI device
3450 * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
3451 *
3452 * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
3453 * bridges all the way up to a PCI root bus.
3454 */
3455u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
3456{
3457 u8 pin = *pinp;
3458
Kenji Kaneshige1eb39482009-05-26 16:08:36 +09003459 while (!pci_is_root_bus(dev->bus)) {
Bjorn Helgaas68feac82008-12-16 21:36:55 -07003460 pin = pci_swizzle_interrupt_pin(dev, pin);
3461 dev = dev->bus->self;
3462 }
3463 *pinp = pin;
3464 return PCI_SLOT(dev->devfn);
3465}
Ray Juie6b29de2015-04-08 11:21:33 -07003466EXPORT_SYMBOL_GPL(pci_common_swizzle);
Bjorn Helgaas68feac82008-12-16 21:36:55 -07003467
3468/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 * pci_release_region - Release a PCI bar
3470 * @pdev: PCI device whose resources were previously reserved by pci_request_region
3471 * @bar: BAR to release
3472 *
3473 * Releases the PCI I/O and memory resources previously reserved by a
3474 * successful call to pci_request_region. Call this function only
3475 * after all use of the PCI regions has ceased.
3476 */
3477void pci_release_region(struct pci_dev *pdev, int bar)
3478{
Tejun Heo9ac78492007-01-20 16:00:26 +09003479 struct pci_devres *dr;
3480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 if (pci_resource_len(pdev, bar) == 0)
3482 return;
3483 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
3484 release_region(pci_resource_start(pdev, bar),
3485 pci_resource_len(pdev, bar));
3486 else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
3487 release_mem_region(pci_resource_start(pdev, bar),
3488 pci_resource_len(pdev, bar));
Tejun Heo9ac78492007-01-20 16:00:26 +09003489
3490 dr = find_pci_dr(pdev);
3491 if (dr)
3492 dr->region_mask &= ~(1 << bar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003494EXPORT_SYMBOL(pci_release_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
3496/**
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08003497 * __pci_request_region - Reserved PCI I/O and memory resource
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 * @pdev: PCI device whose resources are to be reserved
3499 * @bar: BAR to be reserved
3500 * @res_name: Name to be associated with resource.
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08003501 * @exclusive: whether the region access is exclusive or not
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 *
3503 * Mark the PCI region associated with PCI device @pdev BR @bar as
3504 * being reserved by owner @res_name. Do not access any
3505 * address inside the PCI regions unless this call returns
3506 * successfully.
3507 *
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08003508 * If @exclusive is set, then the region is marked so that userspace
3509 * is explicitly not allowed to map the resource via /dev/mem or
Bjorn Helgaasf7625982013-11-14 11:28:18 -07003510 * sysfs MMIO access.
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08003511 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 * Returns 0 on success, or %EBUSY on error. A warning
3513 * message is also printed on failure.
3514 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003515static int __pci_request_region(struct pci_dev *pdev, int bar,
3516 const char *res_name, int exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517{
Tejun Heo9ac78492007-01-20 16:00:26 +09003518 struct pci_devres *dr;
3519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 if (pci_resource_len(pdev, bar) == 0)
3521 return 0;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07003522
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
3524 if (!request_region(pci_resource_start(pdev, bar),
3525 pci_resource_len(pdev, bar), res_name))
3526 goto err_out;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003527 } else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
Arjan van de Vene8de1482008-10-22 19:55:31 -07003528 if (!__request_mem_region(pci_resource_start(pdev, bar),
3529 pci_resource_len(pdev, bar), res_name,
3530 exclusive))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 goto err_out;
3532 }
Tejun Heo9ac78492007-01-20 16:00:26 +09003533
3534 dr = find_pci_dr(pdev);
3535 if (dr)
3536 dr->region_mask |= 1 << bar;
3537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 return 0;
3539
3540err_out:
Frederick Lawler7506dc72018-01-18 12:55:24 -06003541 pci_warn(pdev, "BAR %d: can't reserve %pR\n", bar,
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +11003542 &pdev->resource[bar]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 return -EBUSY;
3544}
3545
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003546/**
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08003547 * pci_request_region - Reserve PCI I/O and memory resource
Arjan van de Vene8de1482008-10-22 19:55:31 -07003548 * @pdev: PCI device whose resources are to be reserved
3549 * @bar: BAR to be reserved
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08003550 * @res_name: Name to be associated with resource
Arjan van de Vene8de1482008-10-22 19:55:31 -07003551 *
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08003552 * Mark the PCI region associated with PCI device @pdev BAR @bar as
Arjan van de Vene8de1482008-10-22 19:55:31 -07003553 * being reserved by owner @res_name. Do not access any
3554 * address inside the PCI regions unless this call returns
3555 * successfully.
3556 *
3557 * Returns 0 on success, or %EBUSY on error. A warning
3558 * message is also printed on failure.
3559 */
3560int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
3561{
3562 return __pci_request_region(pdev, bar, res_name, 0);
3563}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003564EXPORT_SYMBOL(pci_request_region);
Arjan van de Vene8de1482008-10-22 19:55:31 -07003565
3566/**
3567 * pci_request_region_exclusive - Reserved PCI I/O and memory resource
3568 * @pdev: PCI device whose resources are to be reserved
3569 * @bar: BAR to be reserved
3570 * @res_name: Name to be associated with resource.
3571 *
3572 * Mark the PCI region associated with PCI device @pdev BR @bar as
3573 * being reserved by owner @res_name. Do not access any
3574 * address inside the PCI regions unless this call returns
3575 * successfully.
3576 *
3577 * Returns 0 on success, or %EBUSY on error. A warning
3578 * message is also printed on failure.
3579 *
3580 * The key difference that _exclusive makes it that userspace is
3581 * explicitly not allowed to map the resource via /dev/mem or
Bjorn Helgaasf7625982013-11-14 11:28:18 -07003582 * sysfs.
Arjan van de Vene8de1482008-10-22 19:55:31 -07003583 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003584int pci_request_region_exclusive(struct pci_dev *pdev, int bar,
3585 const char *res_name)
Arjan van de Vene8de1482008-10-22 19:55:31 -07003586{
3587 return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
3588}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003589EXPORT_SYMBOL(pci_request_region_exclusive);
3590
Arjan van de Vene8de1482008-10-22 19:55:31 -07003591/**
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003592 * pci_release_selected_regions - Release selected PCI I/O and memory resources
3593 * @pdev: PCI device whose resources were previously reserved
3594 * @bars: Bitmask of BARs to be released
3595 *
3596 * Release selected PCI I/O and memory resources previously reserved.
3597 * Call this function only after all use of the PCI regions has ceased.
3598 */
3599void pci_release_selected_regions(struct pci_dev *pdev, int bars)
3600{
3601 int i;
3602
3603 for (i = 0; i < 6; i++)
3604 if (bars & (1 << i))
3605 pci_release_region(pdev, i);
3606}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003607EXPORT_SYMBOL(pci_release_selected_regions);
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003608
Bjorn Helgaas9738abe2013-04-12 11:20:03 -06003609static int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003610 const char *res_name, int excl)
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003611{
3612 int i;
3613
3614 for (i = 0; i < 6; i++)
3615 if (bars & (1 << i))
Arjan van de Vene8de1482008-10-22 19:55:31 -07003616 if (__pci_request_region(pdev, i, res_name, excl))
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003617 goto err_out;
3618 return 0;
3619
3620err_out:
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003621 while (--i >= 0)
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003622 if (bars & (1 << i))
3623 pci_release_region(pdev, i);
3624
3625 return -EBUSY;
3626}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Arjan van de Vene8de1482008-10-22 19:55:31 -07003628
3629/**
3630 * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
3631 * @pdev: PCI device whose resources are to be reserved
3632 * @bars: Bitmask of BARs to be requested
3633 * @res_name: Name to be associated with resource
3634 */
3635int pci_request_selected_regions(struct pci_dev *pdev, int bars,
3636 const char *res_name)
3637{
3638 return __pci_request_selected_regions(pdev, bars, res_name, 0);
3639}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003640EXPORT_SYMBOL(pci_request_selected_regions);
Arjan van de Vene8de1482008-10-22 19:55:31 -07003641
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003642int pci_request_selected_regions_exclusive(struct pci_dev *pdev, int bars,
3643 const char *res_name)
Arjan van de Vene8de1482008-10-22 19:55:31 -07003644{
3645 return __pci_request_selected_regions(pdev, bars, res_name,
3646 IORESOURCE_EXCLUSIVE);
3647}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003648EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
Arjan van de Vene8de1482008-10-22 19:55:31 -07003649
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650/**
3651 * pci_release_regions - Release reserved PCI I/O and memory resources
3652 * @pdev: PCI device whose resources were previously reserved by pci_request_regions
3653 *
3654 * Releases all PCI I/O and memory resources previously reserved by a
3655 * successful call to pci_request_regions. Call this function only
3656 * after all use of the PCI regions has ceased.
3657 */
3658
3659void pci_release_regions(struct pci_dev *pdev)
3660{
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003661 pci_release_selected_regions(pdev, (1 << 6) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003663EXPORT_SYMBOL(pci_release_regions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
3665/**
3666 * pci_request_regions - Reserved PCI I/O and memory resources
3667 * @pdev: PCI device whose resources are to be reserved
3668 * @res_name: Name to be associated with resource.
3669 *
3670 * Mark all PCI regions associated with PCI device @pdev as
3671 * being reserved by owner @res_name. Do not access any
3672 * address inside the PCI regions unless this call returns
3673 * successfully.
3674 *
3675 * Returns 0 on success, or %EBUSY on error. A warning
3676 * message is also printed on failure.
3677 */
Jeff Garzik3c990e92006-03-04 21:52:42 -05003678int pci_request_regions(struct pci_dev *pdev, const char *res_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003680 return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003682EXPORT_SYMBOL(pci_request_regions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
3684/**
Arjan van de Vene8de1482008-10-22 19:55:31 -07003685 * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
3686 * @pdev: PCI device whose resources are to be reserved
3687 * @res_name: Name to be associated with resource.
3688 *
3689 * Mark all PCI regions associated with PCI device @pdev as
3690 * being reserved by owner @res_name. Do not access any
3691 * address inside the PCI regions unless this call returns
3692 * successfully.
3693 *
3694 * pci_request_regions_exclusive() will mark the region so that
Bjorn Helgaasf7625982013-11-14 11:28:18 -07003695 * /dev/mem and the sysfs MMIO access will not be allowed.
Arjan van de Vene8de1482008-10-22 19:55:31 -07003696 *
3697 * Returns 0 on success, or %EBUSY on error. A warning
3698 * message is also printed on failure.
3699 */
3700int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
3701{
3702 return pci_request_selected_regions_exclusive(pdev,
3703 ((1 << 6) - 1), res_name);
3704}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003705EXPORT_SYMBOL(pci_request_regions_exclusive);
Arjan van de Vene8de1482008-10-22 19:55:31 -07003706
Tomasz Nowickic5076cf2016-05-11 17:34:51 -05003707/*
3708 * Record the PCI IO range (expressed as CPU physical address + size).
3709 * Return a negative value if an error has occured, zero otherwise
3710 */
Gabriele Paolonifcfaab32018-03-15 02:15:52 +08003711int pci_register_io_range(struct fwnode_handle *fwnode, phys_addr_t addr,
3712 resource_size_t size)
Tomasz Nowickic5076cf2016-05-11 17:34:51 -05003713{
Zhichang Yuan57453922018-03-15 02:15:53 +08003714 int ret = 0;
Tomasz Nowickic5076cf2016-05-11 17:34:51 -05003715#ifdef PCI_IOBASE
Zhichang Yuan57453922018-03-15 02:15:53 +08003716 struct logic_pio_hwaddr *range;
Tomasz Nowickic5076cf2016-05-11 17:34:51 -05003717
Zhichang Yuan57453922018-03-15 02:15:53 +08003718 if (!size || addr + size < addr)
3719 return -EINVAL;
Tomasz Nowickic5076cf2016-05-11 17:34:51 -05003720
Tomasz Nowickic5076cf2016-05-11 17:34:51 -05003721 range = kzalloc(sizeof(*range), GFP_ATOMIC);
Zhichang Yuan57453922018-03-15 02:15:53 +08003722 if (!range)
3723 return -ENOMEM;
Tomasz Nowickic5076cf2016-05-11 17:34:51 -05003724
Zhichang Yuan57453922018-03-15 02:15:53 +08003725 range->fwnode = fwnode;
Tomasz Nowickic5076cf2016-05-11 17:34:51 -05003726 range->size = size;
Zhichang Yuan57453922018-03-15 02:15:53 +08003727 range->hw_start = addr;
3728 range->flags = LOGIC_PIO_CPU_MMIO;
Tomasz Nowickic5076cf2016-05-11 17:34:51 -05003729
Zhichang Yuan57453922018-03-15 02:15:53 +08003730 ret = logic_pio_register_range(range);
3731 if (ret)
3732 kfree(range);
Tomasz Nowickic5076cf2016-05-11 17:34:51 -05003733#endif
3734
Zhichang Yuan57453922018-03-15 02:15:53 +08003735 return ret;
Tomasz Nowickic5076cf2016-05-11 17:34:51 -05003736}
3737
3738phys_addr_t pci_pio_to_address(unsigned long pio)
3739{
3740 phys_addr_t address = (phys_addr_t)OF_BAD_ADDR;
3741
3742#ifdef PCI_IOBASE
Zhichang Yuan57453922018-03-15 02:15:53 +08003743 if (pio >= MMIO_UPPER_LIMIT)
Tomasz Nowickic5076cf2016-05-11 17:34:51 -05003744 return address;
3745
Zhichang Yuan57453922018-03-15 02:15:53 +08003746 address = logic_pio_to_hwaddr(pio);
Tomasz Nowickic5076cf2016-05-11 17:34:51 -05003747#endif
3748
3749 return address;
3750}
3751
3752unsigned long __weak pci_address_to_pio(phys_addr_t address)
3753{
3754#ifdef PCI_IOBASE
Zhichang Yuan57453922018-03-15 02:15:53 +08003755 return logic_pio_trans_cpuaddr(address);
Tomasz Nowickic5076cf2016-05-11 17:34:51 -05003756#else
3757 if (address > IO_SPACE_LIMIT)
3758 return (unsigned long)-1;
3759
3760 return (unsigned long) address;
3761#endif
3762}
3763
Liviu Dudau8b921ac2014-09-29 15:29:30 +01003764/**
3765 * pci_remap_iospace - Remap the memory mapped I/O space
3766 * @res: Resource describing the I/O space
3767 * @phys_addr: physical address of range to be mapped
3768 *
3769 * Remap the memory mapped I/O space described by the @res
3770 * and the CPU physical address @phys_addr into virtual address space.
3771 * Only architectures that have memory mapped IO functions defined
3772 * (and the PCI_IOBASE value defined) should call this function.
3773 */
Lorenzo Pieralisi7b309ae2017-04-19 17:48:50 +01003774int pci_remap_iospace(const struct resource *res, phys_addr_t phys_addr)
Liviu Dudau8b921ac2014-09-29 15:29:30 +01003775{
3776#if defined(PCI_IOBASE) && defined(CONFIG_MMU)
3777 unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start;
3778
3779 if (!(res->flags & IORESOURCE_IO))
3780 return -EINVAL;
3781
3782 if (res->end > IO_SPACE_LIMIT)
3783 return -EINVAL;
3784
3785 return ioremap_page_range(vaddr, vaddr + resource_size(res), phys_addr,
3786 pgprot_device(PAGE_KERNEL));
3787#else
3788 /* this architecture does not have memory mapped I/O space,
3789 so this function should never be called */
3790 WARN_ONCE(1, "This architecture does not support memory mapped I/O\n");
3791 return -ENODEV;
3792#endif
3793}
Brian Norrisf90b0872017-03-09 18:46:16 -08003794EXPORT_SYMBOL(pci_remap_iospace);
Liviu Dudau8b921ac2014-09-29 15:29:30 +01003795
Sinan Kaya4d3f1382016-06-10 21:55:11 +02003796/**
3797 * pci_unmap_iospace - Unmap the memory mapped I/O space
3798 * @res: resource to be unmapped
3799 *
3800 * Unmap the CPU virtual address @res from virtual address space.
3801 * Only architectures that have memory mapped IO functions defined
3802 * (and the PCI_IOBASE value defined) should call this function.
3803 */
3804void pci_unmap_iospace(struct resource *res)
3805{
3806#if defined(PCI_IOBASE) && defined(CONFIG_MMU)
3807 unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start;
3808
3809 unmap_kernel_range(vaddr, resource_size(res));
3810#endif
3811}
Brian Norrisf90b0872017-03-09 18:46:16 -08003812EXPORT_SYMBOL(pci_unmap_iospace);
Sinan Kaya4d3f1382016-06-10 21:55:11 +02003813
Lorenzo Pieralisi490cb6d2017-04-19 17:48:55 +01003814/**
3815 * devm_pci_remap_cfgspace - Managed pci_remap_cfgspace()
3816 * @dev: Generic device to remap IO address for
3817 * @offset: Resource address to map
3818 * @size: Size of map
3819 *
3820 * Managed pci_remap_cfgspace(). Map is automatically unmapped on driver
3821 * detach.
3822 */
3823void __iomem *devm_pci_remap_cfgspace(struct device *dev,
3824 resource_size_t offset,
3825 resource_size_t size)
3826{
3827 void __iomem **ptr, *addr;
3828
3829 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
3830 if (!ptr)
3831 return NULL;
3832
3833 addr = pci_remap_cfgspace(offset, size);
3834 if (addr) {
3835 *ptr = addr;
3836 devres_add(dev, ptr);
3837 } else
3838 devres_free(ptr);
3839
3840 return addr;
3841}
3842EXPORT_SYMBOL(devm_pci_remap_cfgspace);
3843
3844/**
3845 * devm_pci_remap_cfg_resource - check, request region and ioremap cfg resource
3846 * @dev: generic device to handle the resource for
3847 * @res: configuration space resource to be handled
3848 *
3849 * Checks that a resource is a valid memory region, requests the memory
3850 * region and ioremaps with pci_remap_cfgspace() API that ensures the
3851 * proper PCI configuration space memory attributes are guaranteed.
3852 *
3853 * All operations are managed and will be undone on driver detach.
3854 *
3855 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
Randy Dunlap505fb742017-10-29 17:07:11 -07003856 * on failure. Usage example::
Lorenzo Pieralisi490cb6d2017-04-19 17:48:55 +01003857 *
3858 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3859 * base = devm_pci_remap_cfg_resource(&pdev->dev, res);
3860 * if (IS_ERR(base))
3861 * return PTR_ERR(base);
3862 */
3863void __iomem *devm_pci_remap_cfg_resource(struct device *dev,
3864 struct resource *res)
3865{
3866 resource_size_t size;
3867 const char *name;
3868 void __iomem *dest_ptr;
3869
3870 BUG_ON(!dev);
3871
3872 if (!res || resource_type(res) != IORESOURCE_MEM) {
3873 dev_err(dev, "invalid resource\n");
3874 return IOMEM_ERR_PTR(-EINVAL);
3875 }
3876
3877 size = resource_size(res);
3878 name = res->name ?: dev_name(dev);
3879
3880 if (!devm_request_mem_region(dev, res->start, size, name)) {
3881 dev_err(dev, "can't request region for resource %pR\n", res);
3882 return IOMEM_ERR_PTR(-EBUSY);
3883 }
3884
3885 dest_ptr = devm_pci_remap_cfgspace(dev, res->start, size);
3886 if (!dest_ptr) {
3887 dev_err(dev, "ioremap failed for resource %pR\n", res);
3888 devm_release_mem_region(dev, res->start, size);
3889 dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
3890 }
3891
3892 return dest_ptr;
3893}
3894EXPORT_SYMBOL(devm_pci_remap_cfg_resource);
3895
Ben Hutchings6a479072008-12-23 03:08:29 +00003896static void __pci_set_master(struct pci_dev *dev, bool enable)
3897{
3898 u16 old_cmd, cmd;
3899
3900 pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
3901 if (enable)
3902 cmd = old_cmd | PCI_COMMAND_MASTER;
3903 else
3904 cmd = old_cmd & ~PCI_COMMAND_MASTER;
3905 if (cmd != old_cmd) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06003906 pci_dbg(dev, "%s bus mastering\n",
Ben Hutchings6a479072008-12-23 03:08:29 +00003907 enable ? "enabling" : "disabling");
3908 pci_write_config_word(dev, PCI_COMMAND, cmd);
3909 }
3910 dev->is_busmaster = enable;
3911}
Arjan van de Vene8de1482008-10-22 19:55:31 -07003912
3913/**
Myron Stowe2b6f2c32012-06-25 21:30:57 -06003914 * pcibios_setup - process "pci=" kernel boot arguments
3915 * @str: string used to pass in "pci=" kernel boot arguments
3916 *
3917 * Process kernel boot arguments. This is the default implementation.
3918 * Architecture specific implementations can override this as necessary.
3919 */
3920char * __weak __init pcibios_setup(char *str)
3921{
3922 return str;
3923}
3924
3925/**
Myron Stowe96c55902011-10-28 15:48:38 -06003926 * pcibios_set_master - enable PCI bus-mastering for device dev
3927 * @dev: the PCI device to enable
3928 *
3929 * Enables PCI bus-mastering for the device. This is the default
3930 * implementation. Architecture specific implementations can override
3931 * this if necessary.
3932 */
3933void __weak pcibios_set_master(struct pci_dev *dev)
3934{
3935 u8 lat;
3936
Myron Stowef6766782011-10-28 15:49:20 -06003937 /* The latency timer doesn't apply to PCIe (either Type 0 or Type 1) */
3938 if (pci_is_pcie(dev))
3939 return;
3940
Myron Stowe96c55902011-10-28 15:48:38 -06003941 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3942 if (lat < 16)
3943 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
3944 else if (lat > pcibios_max_latency)
3945 lat = pcibios_max_latency;
3946 else
3947 return;
Bjorn Helgaasa0064822013-09-23 15:25:26 -06003948
Myron Stowe96c55902011-10-28 15:48:38 -06003949 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
3950}
3951
3952/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 * pci_set_master - enables bus-mastering for device dev
3954 * @dev: the PCI device to enable
3955 *
3956 * Enables bus-mastering on the device and calls pcibios_set_master()
3957 * to do the needed arch specific settings.
3958 */
Ben Hutchings6a479072008-12-23 03:08:29 +00003959void pci_set_master(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
Ben Hutchings6a479072008-12-23 03:08:29 +00003961 __pci_set_master(dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 pcibios_set_master(dev);
3963}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003964EXPORT_SYMBOL(pci_set_master);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Ben Hutchings6a479072008-12-23 03:08:29 +00003966/**
3967 * pci_clear_master - disables bus-mastering for device dev
3968 * @dev: the PCI device to disable
3969 */
3970void pci_clear_master(struct pci_dev *dev)
3971{
3972 __pci_set_master(dev, false);
3973}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003974EXPORT_SYMBOL(pci_clear_master);
Ben Hutchings6a479072008-12-23 03:08:29 +00003975
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976/**
Matthew Wilcoxedb2d972006-10-10 08:01:21 -06003977 * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
3978 * @dev: the PCI device for which MWI is to be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 *
Matthew Wilcoxedb2d972006-10-10 08:01:21 -06003980 * Helper function for pci_set_mwi.
3981 * Originally copied from drivers/net/acenic.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
3983 *
3984 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
3985 */
Tejun Heo15ea76d2009-09-22 17:34:48 +09003986int pci_set_cacheline_size(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987{
3988 u8 cacheline_size;
3989
3990 if (!pci_cache_line_size)
Tejun Heo15ea76d2009-09-22 17:34:48 +09003991 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
3993 /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
3994 equal to or multiple of the right value. */
3995 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
3996 if (cacheline_size >= pci_cache_line_size &&
3997 (cacheline_size % pci_cache_line_size) == 0)
3998 return 0;
3999
4000 /* Write the correct value. */
4001 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
4002 /* Read it back. */
4003 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
4004 if (cacheline_size == pci_cache_line_size)
4005 return 0;
4006
Frederick Lawler7506dc72018-01-18 12:55:24 -06004007 pci_printk(KERN_DEBUG, dev, "cache line size of %d is not supported\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -04004008 pci_cache_line_size << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009
4010 return -EINVAL;
4011}
Tejun Heo15ea76d2009-09-22 17:34:48 +09004012EXPORT_SYMBOL_GPL(pci_set_cacheline_size);
4013
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014/**
4015 * pci_set_mwi - enables memory-write-invalidate PCI transaction
4016 * @dev: the PCI device for which MWI is enabled
4017 *
Randy Dunlap694625c2007-07-09 11:55:54 -07004018 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 *
4020 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
4021 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04004022int pci_set_mwi(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06004024#ifdef PCI_DISABLE_MWI
4025 return 0;
4026#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 int rc;
4028 u16 cmd;
4029
Matthew Wilcoxedb2d972006-10-10 08:01:21 -06004030 rc = pci_set_cacheline_size(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 if (rc)
4032 return rc;
4033
4034 pci_read_config_word(dev, PCI_COMMAND, &cmd);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04004035 if (!(cmd & PCI_COMMAND_INVALIDATE)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06004036 pci_dbg(dev, "enabling Mem-Wr-Inval\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 cmd |= PCI_COMMAND_INVALIDATE;
4038 pci_write_config_word(dev, PCI_COMMAND, cmd);
4039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 return 0;
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06004041#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06004043EXPORT_SYMBOL(pci_set_mwi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
4045/**
Heiner Kallweitfc0f9f42017-12-12 07:40:56 +01004046 * pcim_set_mwi - a device-managed pci_set_mwi()
4047 * @dev: the PCI device for which MWI is enabled
4048 *
4049 * Managed pci_set_mwi().
4050 *
4051 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
4052 */
4053int pcim_set_mwi(struct pci_dev *dev)
4054{
4055 struct pci_devres *dr;
4056
4057 dr = find_pci_dr(dev);
4058 if (!dr)
4059 return -ENOMEM;
4060
4061 dr->mwi = 1;
4062 return pci_set_mwi(dev);
4063}
4064EXPORT_SYMBOL(pcim_set_mwi);
4065
4066/**
Randy Dunlap694625c2007-07-09 11:55:54 -07004067 * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
4068 * @dev: the PCI device for which MWI is enabled
4069 *
4070 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
4071 * Callers are not required to check the return value.
4072 *
4073 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
4074 */
4075int pci_try_set_mwi(struct pci_dev *dev)
4076{
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06004077#ifdef PCI_DISABLE_MWI
4078 return 0;
4079#else
4080 return pci_set_mwi(dev);
4081#endif
Randy Dunlap694625c2007-07-09 11:55:54 -07004082}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06004083EXPORT_SYMBOL(pci_try_set_mwi);
Randy Dunlap694625c2007-07-09 11:55:54 -07004084
4085/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
4087 * @dev: the PCI device to disable
4088 *
4089 * Disables PCI Memory-Write-Invalidate transaction on the device
4090 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04004091void pci_clear_mwi(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092{
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06004093#ifndef PCI_DISABLE_MWI
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 u16 cmd;
4095
4096 pci_read_config_word(dev, PCI_COMMAND, &cmd);
4097 if (cmd & PCI_COMMAND_INVALIDATE) {
4098 cmd &= ~PCI_COMMAND_INVALIDATE;
4099 pci_write_config_word(dev, PCI_COMMAND, cmd);
4100 }
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06004101#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06004103EXPORT_SYMBOL(pci_clear_mwi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
Brett M Russa04ce0f2005-08-15 15:23:41 -04004105/**
4106 * pci_intx - enables/disables PCI INTx for device dev
Randy Dunlap8f7020d2005-10-23 11:57:38 -07004107 * @pdev: the PCI device to operate on
4108 * @enable: boolean: whether to enable or disable PCI INTx
Brett M Russa04ce0f2005-08-15 15:23:41 -04004109 *
4110 * Enables/disables PCI INTx for device dev
4111 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04004112void pci_intx(struct pci_dev *pdev, int enable)
Brett M Russa04ce0f2005-08-15 15:23:41 -04004113{
4114 u16 pci_command, new;
4115
4116 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
4117
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04004118 if (enable)
Brett M Russa04ce0f2005-08-15 15:23:41 -04004119 new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04004120 else
Brett M Russa04ce0f2005-08-15 15:23:41 -04004121 new = pci_command | PCI_COMMAND_INTX_DISABLE;
Brett M Russa04ce0f2005-08-15 15:23:41 -04004122
4123 if (new != pci_command) {
Tejun Heo9ac78492007-01-20 16:00:26 +09004124 struct pci_devres *dr;
4125
Brett M Russ2fd9d742005-09-09 10:02:22 -07004126 pci_write_config_word(pdev, PCI_COMMAND, new);
Tejun Heo9ac78492007-01-20 16:00:26 +09004127
4128 dr = find_pci_dr(pdev);
4129 if (dr && !dr->restore_intx) {
4130 dr->restore_intx = 1;
4131 dr->orig_intx = !enable;
4132 }
Brett M Russa04ce0f2005-08-15 15:23:41 -04004133 }
4134}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06004135EXPORT_SYMBOL_GPL(pci_intx);
Brett M Russa04ce0f2005-08-15 15:23:41 -04004136
Jan Kiszkaa2e27782011-11-04 09:46:00 +01004137static bool pci_check_and_set_intx_mask(struct pci_dev *dev, bool mask)
4138{
4139 struct pci_bus *bus = dev->bus;
4140 bool mask_updated = true;
4141 u32 cmd_status_dword;
4142 u16 origcmd, newcmd;
4143 unsigned long flags;
4144 bool irq_pending;
4145
4146 /*
4147 * We do a single dword read to retrieve both command and status.
4148 * Document assumptions that make this possible.
4149 */
4150 BUILD_BUG_ON(PCI_COMMAND % 4);
4151 BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
4152
4153 raw_spin_lock_irqsave(&pci_lock, flags);
4154
4155 bus->ops->read(bus, dev->devfn, PCI_COMMAND, 4, &cmd_status_dword);
4156
4157 irq_pending = (cmd_status_dword >> 16) & PCI_STATUS_INTERRUPT;
4158
4159 /*
4160 * Check interrupt status register to see whether our device
4161 * triggered the interrupt (when masking) or the next IRQ is
4162 * already pending (when unmasking).
4163 */
4164 if (mask != irq_pending) {
4165 mask_updated = false;
4166 goto done;
4167 }
4168
4169 origcmd = cmd_status_dword;
4170 newcmd = origcmd & ~PCI_COMMAND_INTX_DISABLE;
4171 if (mask)
4172 newcmd |= PCI_COMMAND_INTX_DISABLE;
4173 if (newcmd != origcmd)
4174 bus->ops->write(bus, dev->devfn, PCI_COMMAND, 2, newcmd);
4175
4176done:
4177 raw_spin_unlock_irqrestore(&pci_lock, flags);
4178
4179 return mask_updated;
4180}
4181
4182/**
4183 * pci_check_and_mask_intx - mask INTx on pending interrupt
Randy Dunlap6e9292c2012-01-21 11:02:35 -08004184 * @dev: the PCI device to operate on
Jan Kiszkaa2e27782011-11-04 09:46:00 +01004185 *
4186 * Check if the device dev has its INTx line asserted, mask it and
Piotr Gregor99b3c582017-05-26 22:02:25 +01004187 * return true in that case. False is returned if no interrupt was
Jan Kiszkaa2e27782011-11-04 09:46:00 +01004188 * pending.
4189 */
4190bool pci_check_and_mask_intx(struct pci_dev *dev)
4191{
4192 return pci_check_and_set_intx_mask(dev, true);
4193}
4194EXPORT_SYMBOL_GPL(pci_check_and_mask_intx);
4195
4196/**
Bjorn Helgaasebd50b92014-01-14 17:10:39 -07004197 * pci_check_and_unmask_intx - unmask INTx if no interrupt is pending
Randy Dunlap6e9292c2012-01-21 11:02:35 -08004198 * @dev: the PCI device to operate on
Jan Kiszkaa2e27782011-11-04 09:46:00 +01004199 *
4200 * Check if the device dev has its INTx line asserted, unmask it if not
4201 * and return true. False is returned and the mask remains active if
4202 * there was still an interrupt pending.
4203 */
4204bool pci_check_and_unmask_intx(struct pci_dev *dev)
4205{
4206 return pci_check_and_set_intx_mask(dev, false);
4207}
4208EXPORT_SYMBOL_GPL(pci_check_and_unmask_intx);
4209
Casey Leedom3775a202013-08-06 15:48:36 +05304210/**
4211 * pci_wait_for_pending_transaction - waits for pending transaction
4212 * @dev: the PCI device to operate on
4213 *
4214 * Return 0 if transaction is pending 1 otherwise.
4215 */
4216int pci_wait_for_pending_transaction(struct pci_dev *dev)
Sheng Yang8dd7f802008-10-21 17:38:25 +08004217{
Alex Williamson157e8762013-12-17 16:43:39 -07004218 if (!pci_is_pcie(dev))
4219 return 1;
Sheng Yang8dd7f802008-10-21 17:38:25 +08004220
Gavin Shand0b4cc42014-05-19 13:06:46 +10004221 return pci_wait_for_pending(dev, pci_pcie_cap(dev) + PCI_EXP_DEVSTA,
4222 PCI_EXP_DEVSTA_TRPND);
Casey Leedom3775a202013-08-06 15:48:36 +05304223}
4224EXPORT_SYMBOL(pci_wait_for_pending_transaction);
Sheng Yang5fe5db02009-02-09 14:53:47 +08004225
Sinan Kayaa2758b62018-02-27 14:14:10 -06004226static int pci_dev_wait(struct pci_dev *dev, char *reset_type, int timeout)
Alex Williamson5adecf82016-02-22 13:05:48 -07004227{
Sinan Kayaa2758b62018-02-27 14:14:10 -06004228 int delay = 1;
Alex Williamson5adecf82016-02-22 13:05:48 -07004229 u32 id;
4230
Sinan Kaya821cdad2017-08-29 14:45:45 -05004231 /*
Sinan Kayaa2758b62018-02-27 14:14:10 -06004232 * After reset, the device should not silently discard config
Sinan Kaya821cdad2017-08-29 14:45:45 -05004233 * requests, but it may still indicate that it needs more time by
4234 * responding to them with CRS completions. The Root Port will
4235 * generally synthesize ~0 data to complete the read (except when
4236 * CRS SV is enabled and the read was for the Vendor ID; in that
4237 * case it synthesizes 0x0001 data).
4238 *
4239 * Wait for the device to return a non-CRS completion. Read the
4240 * Command register instead of Vendor ID so we don't have to
4241 * contend with the CRS SV value.
4242 */
4243 pci_read_config_dword(dev, PCI_COMMAND, &id);
4244 while (id == ~0) {
4245 if (delay > timeout) {
Sinan Kayaa2758b62018-02-27 14:14:10 -06004246 pci_warn(dev, "not ready %dms after %s; giving up\n",
4247 delay - 1, reset_type);
Sinan Kaya91295d72018-02-27 14:14:08 -06004248 return -ENOTTY;
Sinan Kaya821cdad2017-08-29 14:45:45 -05004249 }
4250
4251 if (delay > 1000)
Sinan Kayaa2758b62018-02-27 14:14:10 -06004252 pci_info(dev, "not ready %dms after %s; waiting\n",
4253 delay - 1, reset_type);
Sinan Kaya821cdad2017-08-29 14:45:45 -05004254
4255 msleep(delay);
4256 delay *= 2;
4257 pci_read_config_dword(dev, PCI_COMMAND, &id);
4258 }
4259
4260 if (delay > 1000)
Sinan Kayaa2758b62018-02-27 14:14:10 -06004261 pci_info(dev, "ready %dms after %s\n", delay - 1,
4262 reset_type);
Sinan Kaya91295d72018-02-27 14:14:08 -06004263
4264 return 0;
Alex Williamson5adecf82016-02-22 13:05:48 -07004265}
4266
Christoph Hellwiga60a2b72017-04-14 21:11:25 +02004267/**
4268 * pcie_has_flr - check if a device supports function level resets
4269 * @dev: device to check
4270 *
4271 * Returns true if the device advertises support for PCIe function level
4272 * resets.
4273 */
4274static bool pcie_has_flr(struct pci_dev *dev)
Casey Leedom3775a202013-08-06 15:48:36 +05304275{
4276 u32 cap;
4277
Sasha Neftinf65fd1a2017-04-03 16:02:50 -05004278 if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET)
Christoph Hellwiga60a2b72017-04-14 21:11:25 +02004279 return false;
Sasha Neftinf65fd1a2017-04-03 16:02:50 -05004280
Casey Leedom3775a202013-08-06 15:48:36 +05304281 pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
Christoph Hellwiga60a2b72017-04-14 21:11:25 +02004282 return cap & PCI_EXP_DEVCAP_FLR;
4283}
Casey Leedom3775a202013-08-06 15:48:36 +05304284
Christoph Hellwiga60a2b72017-04-14 21:11:25 +02004285/**
4286 * pcie_flr - initiate a PCIe function level reset
4287 * @dev: device to reset
4288 *
4289 * Initiate a function level reset on @dev. The caller should ensure the
4290 * device supports FLR before calling this function, e.g. by using the
4291 * pcie_has_flr() helper.
4292 */
Sinan Kaya91295d72018-02-27 14:14:08 -06004293int pcie_flr(struct pci_dev *dev)
Christoph Hellwiga60a2b72017-04-14 21:11:25 +02004294{
Casey Leedom3775a202013-08-06 15:48:36 +05304295 if (!pci_wait_for_pending_transaction(dev))
Frederick Lawler7506dc72018-01-18 12:55:24 -06004296 pci_err(dev, "timed out waiting for pending transaction; performing function level reset anyway\n");
Casey Leedom3775a202013-08-06 15:48:36 +05304297
Jiang Liu59875ae2012-07-24 17:20:06 +08004298 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
Sinan Kayaa2758b62018-02-27 14:14:10 -06004299
4300 /*
4301 * Per PCIe r4.0, sec 6.6.2, a device must complete an FLR within
4302 * 100ms, but may silently discard requests while the FLR is in
4303 * progress. Wait 100ms before trying to access the device.
4304 */
4305 msleep(100);
4306
4307 return pci_dev_wait(dev, "FLR", PCIE_RESET_READY_POLL_MS);
Sheng Yang8dd7f802008-10-21 17:38:25 +08004308}
Christoph Hellwiga60a2b72017-04-14 21:11:25 +02004309EXPORT_SYMBOL_GPL(pcie_flr);
Sheng Yangd91cdc72008-11-11 17:17:47 +08004310
Yu Zhao8c1c6992009-06-13 15:52:13 +08004311static int pci_af_flr(struct pci_dev *dev, int probe)
Sheng Yang1ca88792008-11-11 17:17:48 +08004312{
Yu Zhao8c1c6992009-06-13 15:52:13 +08004313 int pos;
Sheng Yang1ca88792008-11-11 17:17:48 +08004314 u8 cap;
4315
Yu Zhao8c1c6992009-06-13 15:52:13 +08004316 pos = pci_find_capability(dev, PCI_CAP_ID_AF);
4317 if (!pos)
Sheng Yang1ca88792008-11-11 17:17:48 +08004318 return -ENOTTY;
Yu Zhao8c1c6992009-06-13 15:52:13 +08004319
Sasha Neftinf65fd1a2017-04-03 16:02:50 -05004320 if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET)
4321 return -ENOTTY;
4322
Yu Zhao8c1c6992009-06-13 15:52:13 +08004323 pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap);
Sheng Yang1ca88792008-11-11 17:17:48 +08004324 if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
4325 return -ENOTTY;
4326
4327 if (probe)
4328 return 0;
4329
Alex Williamsond066c942014-06-17 15:40:13 -06004330 /*
4331 * Wait for Transaction Pending bit to clear. A word-aligned test
4332 * is used, so we use the conrol offset rather than status and shift
4333 * the test bit to match.
4334 */
Gavin Shanbb383e22014-11-12 13:41:51 +11004335 if (!pci_wait_for_pending(dev, pos + PCI_AF_CTRL,
Alex Williamsond066c942014-06-17 15:40:13 -06004336 PCI_AF_STATUS_TP << 8))
Frederick Lawler7506dc72018-01-18 12:55:24 -06004337 pci_err(dev, "timed out waiting for pending transaction; performing AF function level reset anyway\n");
Yu Zhao8c1c6992009-06-13 15:52:13 +08004338
Yu Zhao8c1c6992009-06-13 15:52:13 +08004339 pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
Sinan Kayaa2758b62018-02-27 14:14:10 -06004340
4341 /*
4342 * Per Advanced Capabilities for Conventional PCI ECN, 13 April 2006,
4343 * updated 27 July 2006; a device must complete an FLR within
4344 * 100ms, but may silently discard requests while the FLR is in
4345 * progress. Wait 100ms before trying to access the device.
4346 */
4347 msleep(100);
4348
4349 return pci_dev_wait(dev, "AF_FLR", PCIE_RESET_READY_POLL_MS);
Sheng Yang1ca88792008-11-11 17:17:48 +08004350}
4351
Rafael J. Wysocki83d74e02011-03-05 21:48:44 +01004352/**
4353 * pci_pm_reset - Put device into PCI_D3 and back into PCI_D0.
4354 * @dev: Device to reset.
4355 * @probe: If set, only check if the device can be reset this way.
4356 *
4357 * If @dev supports native PCI PM and its PCI_PM_CTRL_NO_SOFT_RESET flag is
4358 * unset, it will be reinitialized internally when going from PCI_D3hot to
4359 * PCI_D0. If that's the case and the device is not in a low-power state
4360 * already, force it into PCI_D3hot and back to PCI_D0, causing it to be reset.
4361 *
4362 * NOTE: This causes the caller to sleep for twice the device power transition
4363 * cooldown period, which for the D0->D3hot and D3hot->D0 transitions is 10 ms
Bjorn Helgaasf7625982013-11-14 11:28:18 -07004364 * by default (i.e. unless the @dev's d3_delay field has a different value).
Rafael J. Wysocki83d74e02011-03-05 21:48:44 +01004365 * Moreover, only devices in D0 can be reset by this function.
4366 */
Yu Zhaof85876b2009-06-13 15:52:14 +08004367static int pci_pm_reset(struct pci_dev *dev, int probe)
Sheng Yangd91cdc72008-11-11 17:17:47 +08004368{
Yu Zhaof85876b2009-06-13 15:52:14 +08004369 u16 csr;
Sheng Yangd91cdc72008-11-11 17:17:47 +08004370
Alex Williamson51e53732014-11-21 11:24:08 -07004371 if (!dev->pm_cap || dev->dev_flags & PCI_DEV_FLAGS_NO_PM_RESET)
Yu Zhaof85876b2009-06-13 15:52:14 +08004372 return -ENOTTY;
Sheng Yangd91cdc72008-11-11 17:17:47 +08004373
Yu Zhaof85876b2009-06-13 15:52:14 +08004374 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr);
4375 if (csr & PCI_PM_CTRL_NO_SOFT_RESET)
4376 return -ENOTTY;
Sheng Yang1ca88792008-11-11 17:17:48 +08004377
Yu Zhaof85876b2009-06-13 15:52:14 +08004378 if (probe)
4379 return 0;
4380
4381 if (dev->current_state != PCI_D0)
4382 return -EINVAL;
4383
4384 csr &= ~PCI_PM_CTRL_STATE_MASK;
4385 csr |= PCI_D3hot;
4386 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +01004387 pci_dev_d3_sleep(dev);
Yu Zhaof85876b2009-06-13 15:52:14 +08004388
4389 csr &= ~PCI_PM_CTRL_STATE_MASK;
4390 csr |= PCI_D0;
4391 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +01004392 pci_dev_d3_sleep(dev);
Yu Zhaof85876b2009-06-13 15:52:14 +08004393
Sinan Kayaabbcf0e2018-02-27 14:14:10 -06004394 return pci_dev_wait(dev, "PM D3->D0", PCIE_RESET_READY_POLL_MS);
Yu Zhaof85876b2009-06-13 15:52:14 +08004395}
Oza Pawandeep9f5a70f12018-05-17 16:44:11 -05004396/**
4397 * pcie_wait_for_link - Wait until link is active or inactive
4398 * @pdev: Bridge device
4399 * @active: waiting for active or inactive?
4400 *
4401 * Use this to wait till link becomes active or inactive.
4402 */
4403bool pcie_wait_for_link(struct pci_dev *pdev, bool active)
4404{
4405 int timeout = 1000;
4406 bool ret;
4407 u16 lnk_status;
4408
4409 for (;;) {
4410 pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
4411 ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
4412 if (ret == active)
4413 return true;
4414 if (timeout <= 0)
4415 break;
4416 msleep(10);
4417 timeout -= 10;
4418 }
4419
4420 pci_info(pdev, "Data Link Layer Link Active not %s in 1000 msec\n",
4421 active ? "set" : "cleared");
4422
4423 return false;
4424}
Yu Zhaof85876b2009-06-13 15:52:14 +08004425
Gavin Shan9e330022014-06-19 17:22:44 +10004426void pci_reset_secondary_bus(struct pci_dev *dev)
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08004427{
4428 u16 ctrl;
Alex Williamson64e86742013-08-08 14:09:24 -06004429
4430 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &ctrl);
4431 ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
4432 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06004433
Alex Williamsonde0c5482013-08-08 14:10:13 -06004434 /*
4435 * PCI spec v3.0 7.6.4.2 requires minimum Trst of 1ms. Double
Bjorn Helgaasf7625982013-11-14 11:28:18 -07004436 * this to 2ms to ensure that we meet the minimum requirement.
Alex Williamsonde0c5482013-08-08 14:10:13 -06004437 */
4438 msleep(2);
Alex Williamson64e86742013-08-08 14:09:24 -06004439
4440 ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
4441 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
Alex Williamsonde0c5482013-08-08 14:10:13 -06004442
4443 /*
4444 * Trhfa for conventional PCI is 2^25 clock cycles.
4445 * Assuming a minimum 33MHz clock this results in a 1s
4446 * delay before we can consider subordinate devices to
4447 * be re-initialized. PCIe has some ways to shorten this,
4448 * but we don't make use of them yet.
4449 */
4450 ssleep(1);
Alex Williamson64e86742013-08-08 14:09:24 -06004451}
Gavin Shand92a2082014-04-24 18:00:24 +10004452
Gavin Shan9e330022014-06-19 17:22:44 +10004453void __weak pcibios_reset_secondary_bus(struct pci_dev *dev)
4454{
4455 pci_reset_secondary_bus(dev);
4456}
4457
Gavin Shand92a2082014-04-24 18:00:24 +10004458/**
4459 * pci_reset_bridge_secondary_bus - Reset the secondary bus on a PCI bridge.
4460 * @dev: Bridge device
4461 *
4462 * Use the bridge control register to assert reset on the secondary bus.
4463 * Devices on the secondary bus are left in power-on state.
4464 */
Sinan Kaya01fd61c2018-02-27 14:14:11 -06004465int pci_reset_bridge_secondary_bus(struct pci_dev *dev)
Gavin Shand92a2082014-04-24 18:00:24 +10004466{
4467 pcibios_reset_secondary_bus(dev);
Sinan Kaya01fd61c2018-02-27 14:14:11 -06004468
Sinan Kaya6b2f13512018-02-27 14:14:12 -06004469 return pci_dev_wait(dev, "bus reset", PCIE_RESET_READY_POLL_MS);
Gavin Shand92a2082014-04-24 18:00:24 +10004470}
Alex Williamson64e86742013-08-08 14:09:24 -06004471EXPORT_SYMBOL_GPL(pci_reset_bridge_secondary_bus);
4472
4473static int pci_parent_bus_reset(struct pci_dev *dev, int probe)
4474{
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08004475 struct pci_dev *pdev;
4476
Alex Williamsonf331a852015-01-15 18:16:04 -06004477 if (pci_is_root_bus(dev->bus) || dev->subordinate ||
4478 !dev->bus->self || dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08004479 return -ENOTTY;
4480
4481 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
4482 if (pdev != dev)
4483 return -ENOTTY;
4484
4485 if (probe)
4486 return 0;
4487
Alex Williamson64e86742013-08-08 14:09:24 -06004488 pci_reset_bridge_secondary_bus(dev->bus->self);
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08004489
4490 return 0;
4491}
4492
Alex Williamson608c3882013-08-08 14:09:43 -06004493static int pci_reset_hotplug_slot(struct hotplug_slot *hotplug, int probe)
4494{
4495 int rc = -ENOTTY;
4496
4497 if (!hotplug || !try_module_get(hotplug->ops->owner))
4498 return rc;
4499
4500 if (hotplug->ops->reset_slot)
4501 rc = hotplug->ops->reset_slot(hotplug, probe);
4502
4503 module_put(hotplug->ops->owner);
4504
4505 return rc;
4506}
4507
4508static int pci_dev_reset_slot_function(struct pci_dev *dev, int probe)
4509{
4510 struct pci_dev *pdev;
4511
Alex Williamsonf331a852015-01-15 18:16:04 -06004512 if (dev->subordinate || !dev->slot ||
4513 dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)
Alex Williamson608c3882013-08-08 14:09:43 -06004514 return -ENOTTY;
4515
4516 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
4517 if (pdev != dev && pdev->slot == dev->slot)
4518 return -ENOTTY;
4519
4520 return pci_reset_hotplug_slot(dev->slot->hotplug, probe);
4521}
4522
Alex Williamson77cb9852013-08-08 14:09:49 -06004523static void pci_dev_lock(struct pci_dev *dev)
4524{
4525 pci_cfg_access_lock(dev);
4526 /* block PM suspend, driver probe, etc. */
4527 device_lock(&dev->dev);
4528}
4529
Alex Williamson61cf16d2013-12-16 15:14:31 -07004530/* Return 1 on successful lock, 0 on contention */
4531static int pci_dev_trylock(struct pci_dev *dev)
4532{
4533 if (pci_cfg_access_trylock(dev)) {
4534 if (device_trylock(&dev->dev))
4535 return 1;
4536 pci_cfg_access_unlock(dev);
4537 }
4538
4539 return 0;
4540}
4541
Alex Williamson77cb9852013-08-08 14:09:49 -06004542static void pci_dev_unlock(struct pci_dev *dev)
4543{
4544 device_unlock(&dev->dev);
4545 pci_cfg_access_unlock(dev);
4546}
4547
Christoph Hellwig775755e2017-06-01 13:10:38 +02004548static void pci_dev_save_and_disable(struct pci_dev *dev)
Keith Busch3ebe7f92014-05-02 10:40:42 -06004549{
4550 const struct pci_error_handlers *err_handler =
4551 dev->driver ? dev->driver->err_handler : NULL;
Keith Busch3ebe7f92014-05-02 10:40:42 -06004552
Christoph Hellwigb014e962017-06-01 13:10:37 +02004553 /*
Christoph Hellwig775755e2017-06-01 13:10:38 +02004554 * dev->driver->err_handler->reset_prepare() is protected against
Christoph Hellwigb014e962017-06-01 13:10:37 +02004555 * races with ->remove() by the device lock, which must be held by
4556 * the caller.
4557 */
Christoph Hellwig775755e2017-06-01 13:10:38 +02004558 if (err_handler && err_handler->reset_prepare)
4559 err_handler->reset_prepare(dev);
Keith Busch3ebe7f92014-05-02 10:40:42 -06004560
Alex Williamsona6cbaad2013-08-08 14:10:02 -06004561 /*
4562 * Wake-up device prior to save. PM registers default to D0 after
4563 * reset and a simple register restore doesn't reliably return
4564 * to a non-D0 state anyway.
4565 */
4566 pci_set_power_state(dev, PCI_D0);
4567
Alex Williamson77cb9852013-08-08 14:09:49 -06004568 pci_save_state(dev);
4569 /*
4570 * Disable the device by clearing the Command register, except for
4571 * INTx-disable which is set. This not only disables MMIO and I/O port
4572 * BARs, but also prevents the device from being Bus Master, preventing
4573 * DMA from the device including MSI/MSI-X interrupts. For PCI 2.3
4574 * compliant devices, INTx-disable prevents legacy interrupts.
4575 */
4576 pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
4577}
4578
4579static void pci_dev_restore(struct pci_dev *dev)
4580{
Christoph Hellwig775755e2017-06-01 13:10:38 +02004581 const struct pci_error_handlers *err_handler =
4582 dev->driver ? dev->driver->err_handler : NULL;
4583
Alex Williamson77cb9852013-08-08 14:09:49 -06004584 pci_restore_state(dev);
Alex Williamson77cb9852013-08-08 14:09:49 -06004585
Christoph Hellwig775755e2017-06-01 13:10:38 +02004586 /*
4587 * dev->driver->err_handler->reset_done() is protected against
4588 * races with ->remove() by the device lock, which must be held by
4589 * the caller.
4590 */
4591 if (err_handler && err_handler->reset_done)
4592 err_handler->reset_done(dev);
Sheng Yangd91cdc72008-11-11 17:17:47 +08004593}
Keith Busch3ebe7f92014-05-02 10:40:42 -06004594
Sheng Yangd91cdc72008-11-11 17:17:47 +08004595/**
Konrad Rzeszutek Wilk6fbf9e72012-01-12 12:06:46 -05004596 * __pci_reset_function_locked - reset a PCI device function while holding
4597 * the @dev mutex lock.
4598 * @dev: PCI device to reset
4599 *
4600 * Some devices allow an individual function to be reset without affecting
4601 * other functions in the same device. The PCI device must be responsive
4602 * to PCI config space in order to use this function.
4603 *
4604 * The device function is presumed to be unused and the caller is holding
4605 * the device mutex lock when this function is called.
4606 * Resetting the device will make the contents of PCI configuration space
4607 * random, so any caller of this must be prepared to reinitialise the
4608 * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
4609 * etc.
4610 *
4611 * Returns 0 if the device function was successfully reset or negative if the
4612 * device doesn't support resetting a single function.
4613 */
4614int __pci_reset_function_locked(struct pci_dev *dev)
4615{
Christoph Hellwig52354b92017-06-01 13:10:39 +02004616 int rc;
4617
4618 might_sleep();
4619
Bjorn Helgaas832c418a2017-10-25 17:09:24 -05004620 /*
4621 * A reset method returns -ENOTTY if it doesn't support this device
4622 * and we should try the next method.
4623 *
4624 * If it returns 0 (success), we're finished. If it returns any
4625 * other error, we're also finished: this indicates that further
4626 * reset mechanisms might be broken on the device.
4627 */
Christoph Hellwig52354b92017-06-01 13:10:39 +02004628 rc = pci_dev_specific_reset(dev, 0);
4629 if (rc != -ENOTTY)
4630 return rc;
4631 if (pcie_has_flr(dev)) {
Sinan Kaya91295d72018-02-27 14:14:08 -06004632 rc = pcie_flr(dev);
4633 if (rc != -ENOTTY)
4634 return rc;
Christoph Hellwig52354b92017-06-01 13:10:39 +02004635 }
4636 rc = pci_af_flr(dev, 0);
4637 if (rc != -ENOTTY)
4638 return rc;
4639 rc = pci_pm_reset(dev, 0);
4640 if (rc != -ENOTTY)
4641 return rc;
4642 rc = pci_dev_reset_slot_function(dev, 0);
4643 if (rc != -ENOTTY)
4644 return rc;
4645 return pci_parent_bus_reset(dev, 0);
Konrad Rzeszutek Wilk6fbf9e72012-01-12 12:06:46 -05004646}
4647EXPORT_SYMBOL_GPL(__pci_reset_function_locked);
4648
4649/**
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03004650 * pci_probe_reset_function - check whether the device can be safely reset
4651 * @dev: PCI device to reset
4652 *
4653 * Some devices allow an individual function to be reset without affecting
4654 * other functions in the same device. The PCI device must be responsive
4655 * to PCI config space in order to use this function.
4656 *
4657 * Returns 0 if the device function can be reset or negative if the
4658 * device doesn't support resetting a single function.
4659 */
4660int pci_probe_reset_function(struct pci_dev *dev)
4661{
Christoph Hellwig52354b92017-06-01 13:10:39 +02004662 int rc;
4663
4664 might_sleep();
4665
4666 rc = pci_dev_specific_reset(dev, 1);
4667 if (rc != -ENOTTY)
4668 return rc;
4669 if (pcie_has_flr(dev))
4670 return 0;
4671 rc = pci_af_flr(dev, 1);
4672 if (rc != -ENOTTY)
4673 return rc;
4674 rc = pci_pm_reset(dev, 1);
4675 if (rc != -ENOTTY)
4676 return rc;
4677 rc = pci_dev_reset_slot_function(dev, 1);
4678 if (rc != -ENOTTY)
4679 return rc;
4680
4681 return pci_parent_bus_reset(dev, 1);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03004682}
4683
4684/**
Yu Zhao8c1c6992009-06-13 15:52:13 +08004685 * pci_reset_function - quiesce and reset a PCI device function
4686 * @dev: PCI device to reset
Sheng Yang8dd7f802008-10-21 17:38:25 +08004687 *
4688 * Some devices allow an individual function to be reset without affecting
4689 * other functions in the same device. The PCI device must be responsive
4690 * to PCI config space in order to use this function.
4691 *
4692 * This function does not just reset the PCI portion of a device, but
4693 * clears all the state associated with the device. This function differs
Jan H. Schönherr79e699b2017-09-06 01:21:23 +02004694 * from __pci_reset_function_locked() in that it saves and restores device state
4695 * over the reset and takes the PCI device lock.
Sheng Yang8dd7f802008-10-21 17:38:25 +08004696 *
Yu Zhao8c1c6992009-06-13 15:52:13 +08004697 * Returns 0 if the device function was successfully reset or negative if the
Sheng Yang8dd7f802008-10-21 17:38:25 +08004698 * device doesn't support resetting a single function.
4699 */
4700int pci_reset_function(struct pci_dev *dev)
4701{
Yu Zhao8c1c6992009-06-13 15:52:13 +08004702 int rc;
Sheng Yang8dd7f802008-10-21 17:38:25 +08004703
Bjorn Helgaas204f4af2018-02-16 15:22:39 -06004704 if (!dev->reset_fn)
4705 return -ENOTTY;
Sheng Yang8dd7f802008-10-21 17:38:25 +08004706
Christoph Hellwigb014e962017-06-01 13:10:37 +02004707 pci_dev_lock(dev);
Alex Williamson77cb9852013-08-08 14:09:49 -06004708 pci_dev_save_and_disable(dev);
Sheng Yang8dd7f802008-10-21 17:38:25 +08004709
Christoph Hellwig52354b92017-06-01 13:10:39 +02004710 rc = __pci_reset_function_locked(dev);
Sheng Yang8dd7f802008-10-21 17:38:25 +08004711
Alex Williamson77cb9852013-08-08 14:09:49 -06004712 pci_dev_restore(dev);
Christoph Hellwigb014e962017-06-01 13:10:37 +02004713 pci_dev_unlock(dev);
Sheng Yang8dd7f802008-10-21 17:38:25 +08004714
Yu Zhao8c1c6992009-06-13 15:52:13 +08004715 return rc;
Sheng Yang8dd7f802008-10-21 17:38:25 +08004716}
4717EXPORT_SYMBOL_GPL(pci_reset_function);
4718
Alex Williamson61cf16d2013-12-16 15:14:31 -07004719/**
Marc Zyngiera477b9c2017-08-01 20:11:02 -05004720 * pci_reset_function_locked - quiesce and reset a PCI device function
4721 * @dev: PCI device to reset
4722 *
4723 * Some devices allow an individual function to be reset without affecting
4724 * other functions in the same device. The PCI device must be responsive
4725 * to PCI config space in order to use this function.
4726 *
4727 * This function does not just reset the PCI portion of a device, but
4728 * clears all the state associated with the device. This function differs
Jan H. Schönherr79e699b2017-09-06 01:21:23 +02004729 * from __pci_reset_function_locked() in that it saves and restores device state
Marc Zyngiera477b9c2017-08-01 20:11:02 -05004730 * over the reset. It also differs from pci_reset_function() in that it
4731 * requires the PCI device lock to be held.
4732 *
4733 * Returns 0 if the device function was successfully reset or negative if the
4734 * device doesn't support resetting a single function.
4735 */
4736int pci_reset_function_locked(struct pci_dev *dev)
4737{
4738 int rc;
4739
Bjorn Helgaas204f4af2018-02-16 15:22:39 -06004740 if (!dev->reset_fn)
4741 return -ENOTTY;
Marc Zyngiera477b9c2017-08-01 20:11:02 -05004742
4743 pci_dev_save_and_disable(dev);
4744
4745 rc = __pci_reset_function_locked(dev);
4746
4747 pci_dev_restore(dev);
4748
4749 return rc;
4750}
4751EXPORT_SYMBOL_GPL(pci_reset_function_locked);
4752
4753/**
Alex Williamson61cf16d2013-12-16 15:14:31 -07004754 * pci_try_reset_function - quiesce and reset a PCI device function
4755 * @dev: PCI device to reset
4756 *
4757 * Same as above, except return -EAGAIN if unable to lock device.
4758 */
4759int pci_try_reset_function(struct pci_dev *dev)
4760{
4761 int rc;
4762
Bjorn Helgaas204f4af2018-02-16 15:22:39 -06004763 if (!dev->reset_fn)
4764 return -ENOTTY;
Alex Williamson61cf16d2013-12-16 15:14:31 -07004765
Christoph Hellwigb014e962017-06-01 13:10:37 +02004766 if (!pci_dev_trylock(dev))
4767 return -EAGAIN;
Alex Williamson61cf16d2013-12-16 15:14:31 -07004768
Christoph Hellwigb014e962017-06-01 13:10:37 +02004769 pci_dev_save_and_disable(dev);
Christoph Hellwig52354b92017-06-01 13:10:39 +02004770 rc = __pci_reset_function_locked(dev);
Sinan Kayacb5e0d02018-02-27 14:14:08 -06004771 pci_dev_restore(dev);
Christoph Hellwigb014e962017-06-01 13:10:37 +02004772 pci_dev_unlock(dev);
Alex Williamson61cf16d2013-12-16 15:14:31 -07004773
Alex Williamson61cf16d2013-12-16 15:14:31 -07004774 return rc;
4775}
4776EXPORT_SYMBOL_GPL(pci_try_reset_function);
4777
Alex Williamsonf331a852015-01-15 18:16:04 -06004778/* Do any devices on or below this bus prevent a bus reset? */
4779static bool pci_bus_resetable(struct pci_bus *bus)
4780{
4781 struct pci_dev *dev;
4782
David Daney35702772017-09-08 10:10:31 +02004783
4784 if (bus->self && (bus->self->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET))
4785 return false;
4786
Alex Williamsonf331a852015-01-15 18:16:04 -06004787 list_for_each_entry(dev, &bus->devices, bus_list) {
4788 if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET ||
4789 (dev->subordinate && !pci_bus_resetable(dev->subordinate)))
4790 return false;
4791 }
4792
4793 return true;
4794}
4795
Alex Williamson090a3c52013-08-08 14:09:55 -06004796/* Lock devices from the top of the tree down */
4797static void pci_bus_lock(struct pci_bus *bus)
4798{
4799 struct pci_dev *dev;
4800
4801 list_for_each_entry(dev, &bus->devices, bus_list) {
4802 pci_dev_lock(dev);
4803 if (dev->subordinate)
4804 pci_bus_lock(dev->subordinate);
4805 }
4806}
4807
4808/* Unlock devices from the bottom of the tree up */
4809static void pci_bus_unlock(struct pci_bus *bus)
4810{
4811 struct pci_dev *dev;
4812
4813 list_for_each_entry(dev, &bus->devices, bus_list) {
4814 if (dev->subordinate)
4815 pci_bus_unlock(dev->subordinate);
4816 pci_dev_unlock(dev);
4817 }
4818}
4819
Alex Williamson61cf16d2013-12-16 15:14:31 -07004820/* Return 1 on successful lock, 0 on contention */
4821static int pci_bus_trylock(struct pci_bus *bus)
4822{
4823 struct pci_dev *dev;
4824
4825 list_for_each_entry(dev, &bus->devices, bus_list) {
4826 if (!pci_dev_trylock(dev))
4827 goto unlock;
4828 if (dev->subordinate) {
4829 if (!pci_bus_trylock(dev->subordinate)) {
4830 pci_dev_unlock(dev);
4831 goto unlock;
4832 }
4833 }
4834 }
4835 return 1;
4836
4837unlock:
4838 list_for_each_entry_continue_reverse(dev, &bus->devices, bus_list) {
4839 if (dev->subordinate)
4840 pci_bus_unlock(dev->subordinate);
4841 pci_dev_unlock(dev);
4842 }
4843 return 0;
4844}
4845
Alex Williamsonf331a852015-01-15 18:16:04 -06004846/* Do any devices on or below this slot prevent a bus reset? */
4847static bool pci_slot_resetable(struct pci_slot *slot)
4848{
4849 struct pci_dev *dev;
4850
Jan Glauber33ba90a2017-09-08 10:10:33 +02004851 if (slot->bus->self &&
4852 (slot->bus->self->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET))
4853 return false;
4854
Alex Williamsonf331a852015-01-15 18:16:04 -06004855 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4856 if (!dev->slot || dev->slot != slot)
4857 continue;
4858 if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET ||
4859 (dev->subordinate && !pci_bus_resetable(dev->subordinate)))
4860 return false;
4861 }
4862
4863 return true;
4864}
4865
Alex Williamson090a3c52013-08-08 14:09:55 -06004866/* Lock devices from the top of the tree down */
4867static void pci_slot_lock(struct pci_slot *slot)
4868{
4869 struct pci_dev *dev;
4870
4871 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4872 if (!dev->slot || dev->slot != slot)
4873 continue;
4874 pci_dev_lock(dev);
4875 if (dev->subordinate)
4876 pci_bus_lock(dev->subordinate);
4877 }
4878}
4879
4880/* Unlock devices from the bottom of the tree up */
4881static void pci_slot_unlock(struct pci_slot *slot)
4882{
4883 struct pci_dev *dev;
4884
4885 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4886 if (!dev->slot || dev->slot != slot)
4887 continue;
4888 if (dev->subordinate)
4889 pci_bus_unlock(dev->subordinate);
4890 pci_dev_unlock(dev);
4891 }
4892}
4893
Alex Williamson61cf16d2013-12-16 15:14:31 -07004894/* Return 1 on successful lock, 0 on contention */
4895static int pci_slot_trylock(struct pci_slot *slot)
4896{
4897 struct pci_dev *dev;
4898
4899 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4900 if (!dev->slot || dev->slot != slot)
4901 continue;
4902 if (!pci_dev_trylock(dev))
4903 goto unlock;
4904 if (dev->subordinate) {
4905 if (!pci_bus_trylock(dev->subordinate)) {
4906 pci_dev_unlock(dev);
4907 goto unlock;
4908 }
4909 }
4910 }
4911 return 1;
4912
4913unlock:
4914 list_for_each_entry_continue_reverse(dev,
4915 &slot->bus->devices, bus_list) {
4916 if (!dev->slot || dev->slot != slot)
4917 continue;
4918 if (dev->subordinate)
4919 pci_bus_unlock(dev->subordinate);
4920 pci_dev_unlock(dev);
4921 }
4922 return 0;
4923}
4924
Alex Williamson090a3c52013-08-08 14:09:55 -06004925/* Save and disable devices from the top of the tree down */
4926static void pci_bus_save_and_disable(struct pci_bus *bus)
4927{
4928 struct pci_dev *dev;
4929
4930 list_for_each_entry(dev, &bus->devices, bus_list) {
Christoph Hellwigb014e962017-06-01 13:10:37 +02004931 pci_dev_lock(dev);
Alex Williamson090a3c52013-08-08 14:09:55 -06004932 pci_dev_save_and_disable(dev);
Christoph Hellwigb014e962017-06-01 13:10:37 +02004933 pci_dev_unlock(dev);
Alex Williamson090a3c52013-08-08 14:09:55 -06004934 if (dev->subordinate)
4935 pci_bus_save_and_disable(dev->subordinate);
4936 }
4937}
4938
4939/*
4940 * Restore devices from top of the tree down - parent bridges need to be
4941 * restored before we can get to subordinate devices.
4942 */
4943static void pci_bus_restore(struct pci_bus *bus)
4944{
4945 struct pci_dev *dev;
4946
4947 list_for_each_entry(dev, &bus->devices, bus_list) {
Christoph Hellwigb014e962017-06-01 13:10:37 +02004948 pci_dev_lock(dev);
Alex Williamson090a3c52013-08-08 14:09:55 -06004949 pci_dev_restore(dev);
Christoph Hellwigb014e962017-06-01 13:10:37 +02004950 pci_dev_unlock(dev);
Alex Williamson090a3c52013-08-08 14:09:55 -06004951 if (dev->subordinate)
4952 pci_bus_restore(dev->subordinate);
4953 }
4954}
4955
4956/* Save and disable devices from the top of the tree down */
4957static void pci_slot_save_and_disable(struct pci_slot *slot)
4958{
4959 struct pci_dev *dev;
4960
4961 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4962 if (!dev->slot || dev->slot != slot)
4963 continue;
4964 pci_dev_save_and_disable(dev);
4965 if (dev->subordinate)
4966 pci_bus_save_and_disable(dev->subordinate);
4967 }
4968}
4969
4970/*
4971 * Restore devices from top of the tree down - parent bridges need to be
4972 * restored before we can get to subordinate devices.
4973 */
4974static void pci_slot_restore(struct pci_slot *slot)
4975{
4976 struct pci_dev *dev;
4977
4978 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4979 if (!dev->slot || dev->slot != slot)
4980 continue;
Sinan Kayacb5e0d02018-02-27 14:14:08 -06004981 pci_dev_lock(dev);
Alex Williamson090a3c52013-08-08 14:09:55 -06004982 pci_dev_restore(dev);
Sinan Kayacb5e0d02018-02-27 14:14:08 -06004983 pci_dev_unlock(dev);
Alex Williamson090a3c52013-08-08 14:09:55 -06004984 if (dev->subordinate)
4985 pci_bus_restore(dev->subordinate);
4986 }
4987}
4988
4989static int pci_slot_reset(struct pci_slot *slot, int probe)
4990{
4991 int rc;
4992
Alex Williamsonf331a852015-01-15 18:16:04 -06004993 if (!slot || !pci_slot_resetable(slot))
Alex Williamson090a3c52013-08-08 14:09:55 -06004994 return -ENOTTY;
4995
4996 if (!probe)
4997 pci_slot_lock(slot);
4998
4999 might_sleep();
5000
5001 rc = pci_reset_hotplug_slot(slot->hotplug, probe);
5002
5003 if (!probe)
5004 pci_slot_unlock(slot);
5005
5006 return rc;
5007}
5008
5009/**
Alex Williamson9a3d2b92013-08-14 14:06:05 -06005010 * pci_probe_reset_slot - probe whether a PCI slot can be reset
5011 * @slot: PCI slot to probe
5012 *
5013 * Return 0 if slot can be reset, negative if a slot reset is not supported.
5014 */
5015int pci_probe_reset_slot(struct pci_slot *slot)
5016{
5017 return pci_slot_reset(slot, 1);
5018}
5019EXPORT_SYMBOL_GPL(pci_probe_reset_slot);
5020
5021/**
Alex Williamson090a3c52013-08-08 14:09:55 -06005022 * pci_reset_slot - reset a PCI slot
5023 * @slot: PCI slot to reset
5024 *
5025 * A PCI bus may host multiple slots, each slot may support a reset mechanism
5026 * independent of other slots. For instance, some slots may support slot power
5027 * control. In the case of a 1:1 bus to slot architecture, this function may
5028 * wrap the bus reset to avoid spurious slot related events such as hotplug.
5029 * Generally a slot reset should be attempted before a bus reset. All of the
5030 * function of the slot and any subordinate buses behind the slot are reset
5031 * through this function. PCI config space of all devices in the slot and
5032 * behind the slot is saved before and restored after reset.
5033 *
5034 * Return 0 on success, non-zero on error.
5035 */
5036int pci_reset_slot(struct pci_slot *slot)
5037{
5038 int rc;
5039
5040 rc = pci_slot_reset(slot, 1);
5041 if (rc)
5042 return rc;
5043
5044 pci_slot_save_and_disable(slot);
5045
5046 rc = pci_slot_reset(slot, 0);
5047
5048 pci_slot_restore(slot);
5049
5050 return rc;
5051}
5052EXPORT_SYMBOL_GPL(pci_reset_slot);
5053
Alex Williamson61cf16d2013-12-16 15:14:31 -07005054/**
5055 * pci_try_reset_slot - Try to reset a PCI slot
5056 * @slot: PCI slot to reset
5057 *
5058 * Same as above except return -EAGAIN if the slot cannot be locked
5059 */
5060int pci_try_reset_slot(struct pci_slot *slot)
5061{
5062 int rc;
5063
5064 rc = pci_slot_reset(slot, 1);
5065 if (rc)
5066 return rc;
5067
5068 pci_slot_save_and_disable(slot);
5069
5070 if (pci_slot_trylock(slot)) {
5071 might_sleep();
5072 rc = pci_reset_hotplug_slot(slot->hotplug, 0);
5073 pci_slot_unlock(slot);
5074 } else
5075 rc = -EAGAIN;
5076
5077 pci_slot_restore(slot);
5078
5079 return rc;
5080}
5081EXPORT_SYMBOL_GPL(pci_try_reset_slot);
5082
Alex Williamson090a3c52013-08-08 14:09:55 -06005083static int pci_bus_reset(struct pci_bus *bus, int probe)
5084{
Alex Williamsonf331a852015-01-15 18:16:04 -06005085 if (!bus->self || !pci_bus_resetable(bus))
Alex Williamson090a3c52013-08-08 14:09:55 -06005086 return -ENOTTY;
5087
5088 if (probe)
5089 return 0;
5090
5091 pci_bus_lock(bus);
5092
5093 might_sleep();
5094
5095 pci_reset_bridge_secondary_bus(bus->self);
5096
5097 pci_bus_unlock(bus);
5098
5099 return 0;
5100}
5101
5102/**
Alex Williamson9a3d2b92013-08-14 14:06:05 -06005103 * pci_probe_reset_bus - probe whether a PCI bus can be reset
5104 * @bus: PCI bus to probe
5105 *
5106 * Return 0 if bus can be reset, negative if a bus reset is not supported.
5107 */
5108int pci_probe_reset_bus(struct pci_bus *bus)
5109{
5110 return pci_bus_reset(bus, 1);
5111}
5112EXPORT_SYMBOL_GPL(pci_probe_reset_bus);
5113
5114/**
Alex Williamson090a3c52013-08-08 14:09:55 -06005115 * pci_reset_bus - reset a PCI bus
5116 * @bus: top level PCI bus to reset
5117 *
5118 * Do a bus reset on the given bus and any subordinate buses, saving
5119 * and restoring state of all devices.
5120 *
5121 * Return 0 on success, non-zero on error.
5122 */
5123int pci_reset_bus(struct pci_bus *bus)
5124{
5125 int rc;
5126
5127 rc = pci_bus_reset(bus, 1);
5128 if (rc)
5129 return rc;
5130
5131 pci_bus_save_and_disable(bus);
5132
5133 rc = pci_bus_reset(bus, 0);
5134
5135 pci_bus_restore(bus);
5136
5137 return rc;
5138}
5139EXPORT_SYMBOL_GPL(pci_reset_bus);
5140
Sheng Yang8dd7f802008-10-21 17:38:25 +08005141/**
Alex Williamson61cf16d2013-12-16 15:14:31 -07005142 * pci_try_reset_bus - Try to reset a PCI bus
5143 * @bus: top level PCI bus to reset
5144 *
5145 * Same as above except return -EAGAIN if the bus cannot be locked
5146 */
5147int pci_try_reset_bus(struct pci_bus *bus)
5148{
5149 int rc;
5150
5151 rc = pci_bus_reset(bus, 1);
5152 if (rc)
5153 return rc;
5154
5155 pci_bus_save_and_disable(bus);
5156
5157 if (pci_bus_trylock(bus)) {
5158 might_sleep();
5159 pci_reset_bridge_secondary_bus(bus->self);
5160 pci_bus_unlock(bus);
5161 } else
5162 rc = -EAGAIN;
5163
5164 pci_bus_restore(bus);
5165
5166 return rc;
5167}
5168EXPORT_SYMBOL_GPL(pci_try_reset_bus);
5169
5170/**
Peter Orubad556ad42007-05-15 13:59:13 +02005171 * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
5172 * @dev: PCI device to query
5173 *
5174 * Returns mmrbc: maximum designed memory read count in bytes
5175 * or appropriate error value.
5176 */
5177int pcix_get_max_mmrbc(struct pci_dev *dev)
5178{
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005179 int cap;
Peter Orubad556ad42007-05-15 13:59:13 +02005180 u32 stat;
5181
5182 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
5183 if (!cap)
5184 return -EINVAL;
5185
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005186 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
Peter Orubad556ad42007-05-15 13:59:13 +02005187 return -EINVAL;
5188
Dean Nelson25daeb52010-03-09 22:26:40 -05005189 return 512 << ((stat & PCI_X_STATUS_MAX_READ) >> 21);
Peter Orubad556ad42007-05-15 13:59:13 +02005190}
5191EXPORT_SYMBOL(pcix_get_max_mmrbc);
5192
5193/**
5194 * pcix_get_mmrbc - get PCI-X maximum memory read byte count
5195 * @dev: PCI device to query
5196 *
5197 * Returns mmrbc: maximum memory read count in bytes
5198 * or appropriate error value.
5199 */
5200int pcix_get_mmrbc(struct pci_dev *dev)
5201{
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005202 int cap;
Dean Nelsonbdc2bda2010-03-09 22:26:48 -05005203 u16 cmd;
Peter Orubad556ad42007-05-15 13:59:13 +02005204
5205 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
5206 if (!cap)
5207 return -EINVAL;
5208
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005209 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
5210 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02005211
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005212 return 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
Peter Orubad556ad42007-05-15 13:59:13 +02005213}
5214EXPORT_SYMBOL(pcix_get_mmrbc);
5215
5216/**
5217 * pcix_set_mmrbc - set PCI-X maximum memory read byte count
5218 * @dev: PCI device to query
5219 * @mmrbc: maximum memory read count in bytes
5220 * valid values are 512, 1024, 2048, 4096
5221 *
5222 * If possible sets maximum memory read byte count, some bridges have erratas
5223 * that prevent this.
5224 */
5225int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
5226{
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005227 int cap;
Dean Nelsonbdc2bda2010-03-09 22:26:48 -05005228 u32 stat, v, o;
5229 u16 cmd;
Peter Orubad556ad42007-05-15 13:59:13 +02005230
vignesh babu229f5af2007-08-13 18:23:14 +05305231 if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005232 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02005233
5234 v = ffs(mmrbc) - 10;
5235
5236 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
5237 if (!cap)
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005238 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02005239
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005240 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
5241 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02005242
5243 if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
5244 return -E2BIG;
5245
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005246 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
5247 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02005248
5249 o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
5250 if (o != v) {
Bjorn Helgaas809a3bf2012-06-20 16:41:16 -06005251 if (v > o && (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
Peter Orubad556ad42007-05-15 13:59:13 +02005252 return -EIO;
5253
5254 cmd &= ~PCI_X_CMD_MAX_READ;
5255 cmd |= v << 2;
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005256 if (pci_write_config_word(dev, cap + PCI_X_CMD, cmd))
5257 return -EIO;
Peter Orubad556ad42007-05-15 13:59:13 +02005258 }
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005259 return 0;
Peter Orubad556ad42007-05-15 13:59:13 +02005260}
5261EXPORT_SYMBOL(pcix_set_mmrbc);
5262
5263/**
5264 * pcie_get_readrq - get PCI Express read request size
5265 * @dev: PCI device to query
5266 *
5267 * Returns maximum memory read request in bytes
5268 * or appropriate error value.
5269 */
5270int pcie_get_readrq(struct pci_dev *dev)
5271{
Peter Orubad556ad42007-05-15 13:59:13 +02005272 u16 ctl;
5273
Jiang Liu59875ae2012-07-24 17:20:06 +08005274 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
Peter Orubad556ad42007-05-15 13:59:13 +02005275
Jiang Liu59875ae2012-07-24 17:20:06 +08005276 return 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
Peter Orubad556ad42007-05-15 13:59:13 +02005277}
5278EXPORT_SYMBOL(pcie_get_readrq);
5279
5280/**
5281 * pcie_set_readrq - set PCI Express maximum memory read request
5282 * @dev: PCI device to query
Randy Dunlap42e61f4a2007-07-23 21:42:11 -07005283 * @rq: maximum memory read count in bytes
Peter Orubad556ad42007-05-15 13:59:13 +02005284 * valid values are 128, 256, 512, 1024, 2048, 4096
5285 *
Jon Masonc9b378c2011-06-28 18:26:25 -05005286 * If possible sets maximum memory read request in bytes
Peter Orubad556ad42007-05-15 13:59:13 +02005287 */
5288int pcie_set_readrq(struct pci_dev *dev, int rq)
5289{
Jiang Liu59875ae2012-07-24 17:20:06 +08005290 u16 v;
Peter Orubad556ad42007-05-15 13:59:13 +02005291
vignesh babu229f5af2007-08-13 18:23:14 +05305292 if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
Jiang Liu59875ae2012-07-24 17:20:06 +08005293 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02005294
Benjamin Herrenschmidta1c473a2011-10-14 14:56:15 -05005295 /*
5296 * If using the "performance" PCIe config, we clamp the
5297 * read rq size to the max packet size to prevent the
5298 * host bridge generating requests larger than we can
5299 * cope with
5300 */
5301 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
5302 int mps = pcie_get_mps(dev);
5303
Benjamin Herrenschmidta1c473a2011-10-14 14:56:15 -05005304 if (mps < rq)
5305 rq = mps;
5306 }
5307
5308 v = (ffs(rq) - 8) << 12;
Peter Orubad556ad42007-05-15 13:59:13 +02005309
Jiang Liu59875ae2012-07-24 17:20:06 +08005310 return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
5311 PCI_EXP_DEVCTL_READRQ, v);
Peter Orubad556ad42007-05-15 13:59:13 +02005312}
5313EXPORT_SYMBOL(pcie_set_readrq);
5314
5315/**
Jon Masonb03e7492011-07-20 15:20:54 -05005316 * pcie_get_mps - get PCI Express maximum payload size
5317 * @dev: PCI device to query
5318 *
5319 * Returns maximum payload size in bytes
Jon Masonb03e7492011-07-20 15:20:54 -05005320 */
5321int pcie_get_mps(struct pci_dev *dev)
5322{
Jon Masonb03e7492011-07-20 15:20:54 -05005323 u16 ctl;
5324
Jiang Liu59875ae2012-07-24 17:20:06 +08005325 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
Jon Masonb03e7492011-07-20 15:20:54 -05005326
Jiang Liu59875ae2012-07-24 17:20:06 +08005327 return 128 << ((ctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
Jon Masonb03e7492011-07-20 15:20:54 -05005328}
Yijing Wangf1c66c42013-09-24 12:08:06 -06005329EXPORT_SYMBOL(pcie_get_mps);
Jon Masonb03e7492011-07-20 15:20:54 -05005330
5331/**
5332 * pcie_set_mps - set PCI Express maximum payload size
5333 * @dev: PCI device to query
Randy Dunlap47c08f32011-08-20 11:49:43 -07005334 * @mps: maximum payload size in bytes
Jon Masonb03e7492011-07-20 15:20:54 -05005335 * valid values are 128, 256, 512, 1024, 2048, 4096
5336 *
5337 * If possible sets maximum payload size
5338 */
5339int pcie_set_mps(struct pci_dev *dev, int mps)
5340{
Jiang Liu59875ae2012-07-24 17:20:06 +08005341 u16 v;
Jon Masonb03e7492011-07-20 15:20:54 -05005342
5343 if (mps < 128 || mps > 4096 || !is_power_of_2(mps))
Jiang Liu59875ae2012-07-24 17:20:06 +08005344 return -EINVAL;
Jon Masonb03e7492011-07-20 15:20:54 -05005345
5346 v = ffs(mps) - 8;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07005347 if (v > dev->pcie_mpss)
Jiang Liu59875ae2012-07-24 17:20:06 +08005348 return -EINVAL;
Jon Masonb03e7492011-07-20 15:20:54 -05005349 v <<= 5;
5350
Jiang Liu59875ae2012-07-24 17:20:06 +08005351 return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
5352 PCI_EXP_DEVCTL_PAYLOAD, v);
Jon Masonb03e7492011-07-20 15:20:54 -05005353}
Yijing Wangf1c66c42013-09-24 12:08:06 -06005354EXPORT_SYMBOL(pcie_set_mps);
Jon Masonb03e7492011-07-20 15:20:54 -05005355
5356/**
Tal Gilboa6db79a82018-03-30 08:37:44 -05005357 * pcie_bandwidth_available - determine minimum link settings of a PCIe
5358 * device and its bandwidth limitation
5359 * @dev: PCI device to query
5360 * @limiting_dev: storage for device causing the bandwidth limitation
5361 * @speed: storage for speed of limiting device
5362 * @width: storage for width of limiting device
5363 *
5364 * Walk up the PCI device chain and find the point where the minimum
5365 * bandwidth is available. Return the bandwidth available there and (if
5366 * limiting_dev, speed, and width pointers are supplied) information about
5367 * that point. The bandwidth returned is in Mb/s, i.e., megabits/second of
5368 * raw bandwidth.
5369 */
5370u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,
5371 enum pci_bus_speed *speed,
5372 enum pcie_link_width *width)
5373{
5374 u16 lnksta;
5375 enum pci_bus_speed next_speed;
5376 enum pcie_link_width next_width;
5377 u32 bw, next_bw;
5378
5379 if (speed)
5380 *speed = PCI_SPEED_UNKNOWN;
5381 if (width)
5382 *width = PCIE_LNK_WIDTH_UNKNOWN;
5383
5384 bw = 0;
5385
5386 while (dev) {
5387 pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
5388
5389 next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
5390 next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >>
5391 PCI_EXP_LNKSTA_NLW_SHIFT;
5392
5393 next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
5394
5395 /* Check if current device limits the total bandwidth */
5396 if (!bw || next_bw <= bw) {
5397 bw = next_bw;
5398
5399 if (limiting_dev)
5400 *limiting_dev = dev;
5401 if (speed)
5402 *speed = next_speed;
5403 if (width)
5404 *width = next_width;
5405 }
5406
5407 dev = pci_upstream_bridge(dev);
5408 }
5409
5410 return bw;
5411}
5412EXPORT_SYMBOL(pcie_bandwidth_available);
5413
5414/**
Tal Gilboa6cf57be2018-03-30 07:44:05 -05005415 * pcie_get_speed_cap - query for the PCI device's link speed capability
5416 * @dev: PCI device to query
5417 *
5418 * Query the PCI device speed capability. Return the maximum link speed
5419 * supported by the device.
5420 */
5421enum pci_bus_speed pcie_get_speed_cap(struct pci_dev *dev)
5422{
5423 u32 lnkcap2, lnkcap;
5424
5425 /*
5426 * PCIe r4.0 sec 7.5.3.18 recommends using the Supported Link
5427 * Speeds Vector in Link Capabilities 2 when supported, falling
5428 * back to Max Link Speed in Link Capabilities otherwise.
5429 */
5430 pcie_capability_read_dword(dev, PCI_EXP_LNKCAP2, &lnkcap2);
5431 if (lnkcap2) { /* PCIe r3.0-compliant */
5432 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_16_0GB)
5433 return PCIE_SPEED_16_0GT;
5434 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
5435 return PCIE_SPEED_8_0GT;
5436 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
5437 return PCIE_SPEED_5_0GT;
5438 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
5439 return PCIE_SPEED_2_5GT;
5440 return PCI_SPEED_UNKNOWN;
5441 }
5442
5443 pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
5444 if (lnkcap) {
5445 if (lnkcap & PCI_EXP_LNKCAP_SLS_16_0GB)
5446 return PCIE_SPEED_16_0GT;
5447 else if (lnkcap & PCI_EXP_LNKCAP_SLS_8_0GB)
5448 return PCIE_SPEED_8_0GT;
5449 else if (lnkcap & PCI_EXP_LNKCAP_SLS_5_0GB)
5450 return PCIE_SPEED_5_0GT;
5451 else if (lnkcap & PCI_EXP_LNKCAP_SLS_2_5GB)
5452 return PCIE_SPEED_2_5GT;
5453 }
5454
5455 return PCI_SPEED_UNKNOWN;
5456}
5457
5458/**
Tal Gilboac70b65f2018-03-30 08:24:36 -05005459 * pcie_get_width_cap - query for the PCI device's link width capability
5460 * @dev: PCI device to query
5461 *
5462 * Query the PCI device width capability. Return the maximum link width
5463 * supported by the device.
5464 */
5465enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev)
5466{
5467 u32 lnkcap;
5468
5469 pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
5470 if (lnkcap)
5471 return (lnkcap & PCI_EXP_LNKCAP_MLW) >> 4;
5472
5473 return PCIE_LNK_WIDTH_UNKNOWN;
5474}
5475
5476/**
Tal Gilboab852f632018-03-30 08:32:03 -05005477 * pcie_bandwidth_capable - calculate a PCI device's link bandwidth capability
5478 * @dev: PCI device
5479 * @speed: storage for link speed
5480 * @width: storage for link width
5481 *
5482 * Calculate a PCI device's link bandwidth by querying for its link speed
5483 * and width, multiplying them, and applying encoding overhead. The result
5484 * is in Mb/s, i.e., megabits/second of raw bandwidth.
5485 */
5486u32 pcie_bandwidth_capable(struct pci_dev *dev, enum pci_bus_speed *speed,
5487 enum pcie_link_width *width)
5488{
5489 *speed = pcie_get_speed_cap(dev);
5490 *width = pcie_get_width_cap(dev);
5491
5492 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
5493 return 0;
5494
5495 return *width * PCIE_SPEED2MBS_ENC(*speed);
5496}
5497
5498/**
Tal Gilboa9e506a72018-03-30 08:56:47 -05005499 * pcie_print_link_status - Report the PCI device's link speed and width
5500 * @dev: PCI device to query
5501 *
5502 * Report the available bandwidth at the device. If this is less than the
5503 * device is capable of, report the device's maximum possible bandwidth and
5504 * the upstream link that limits its performance to less than that.
5505 */
5506void pcie_print_link_status(struct pci_dev *dev)
5507{
5508 enum pcie_link_width width, width_cap;
5509 enum pci_bus_speed speed, speed_cap;
5510 struct pci_dev *limiting_dev = NULL;
5511 u32 bw_avail, bw_cap;
5512
5513 bw_cap = pcie_bandwidth_capable(dev, &speed_cap, &width_cap);
5514 bw_avail = pcie_bandwidth_available(dev, &limiting_dev, &speed, &width);
5515
5516 if (bw_avail >= bw_cap)
Jakub Kicinski0cf22d62018-04-20 12:56:36 -05005517 pci_info(dev, "%u.%03u Gb/s available PCIe bandwidth (%s x%d link)\n",
Tal Gilboa9e506a72018-03-30 08:56:47 -05005518 bw_cap / 1000, bw_cap % 1000,
5519 PCIE_SPEED2STR(speed_cap), width_cap);
5520 else
Jakub Kicinski0cf22d62018-04-20 12:56:36 -05005521 pci_info(dev, "%u.%03u Gb/s available PCIe bandwidth, limited by %s x%d link at %s (capable of %u.%03u Gb/s with %s x%d link)\n",
Tal Gilboa9e506a72018-03-30 08:56:47 -05005522 bw_avail / 1000, bw_avail % 1000,
5523 PCIE_SPEED2STR(speed), width,
5524 limiting_dev ? pci_name(limiting_dev) : "<unknown>",
5525 bw_cap / 1000, bw_cap % 1000,
5526 PCIE_SPEED2STR(speed_cap), width_cap);
5527}
5528EXPORT_SYMBOL(pcie_print_link_status);
5529
5530/**
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09005531 * pci_select_bars - Make BAR mask from the type of resource
Randy Dunlapf95d8822007-02-10 14:41:56 -08005532 * @dev: the PCI device for which BAR mask is made
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09005533 * @flags: resource type mask to be selected
5534 *
5535 * This helper routine makes bar mask from the type of resource.
5536 */
5537int pci_select_bars(struct pci_dev *dev, unsigned long flags)
5538{
5539 int i, bars = 0;
5540 for (i = 0; i < PCI_NUM_RESOURCES; i++)
5541 if (pci_resource_flags(dev, i) & flags)
5542 bars |= (1 << i);
5543 return bars;
5544}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06005545EXPORT_SYMBOL(pci_select_bars);
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09005546
Mike Travis95a8b6e2010-02-02 14:38:13 -08005547/* Some architectures require additional programming to enable VGA */
5548static arch_set_vga_state_t arch_set_vga_state;
5549
5550void __init pci_register_set_vga_state(arch_set_vga_state_t func)
5551{
5552 arch_set_vga_state = func; /* NULL disables */
5553}
5554
5555static int pci_set_vga_state_arch(struct pci_dev *dev, bool decode,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04005556 unsigned int command_bits, u32 flags)
Mike Travis95a8b6e2010-02-02 14:38:13 -08005557{
5558 if (arch_set_vga_state)
5559 return arch_set_vga_state(dev, decode, command_bits,
Dave Airlie7ad35cf2011-05-25 14:00:49 +10005560 flags);
Mike Travis95a8b6e2010-02-02 14:38:13 -08005561 return 0;
5562}
5563
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10005564/**
5565 * pci_set_vga_state - set VGA decode state on device and parents if requested
Randy Dunlap19eea632009-09-17 15:28:22 -07005566 * @dev: the PCI device
5567 * @decode: true = enable decoding, false = disable decoding
5568 * @command_bits: PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY
Randy Dunlap3f37d622011-05-25 19:21:25 -07005569 * @flags: traverse ancestors and change bridges
Dave Airlie3448a192010-06-01 15:32:24 +10005570 * CHANGE_BRIDGE_ONLY / CHANGE_BRIDGE
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10005571 */
5572int pci_set_vga_state(struct pci_dev *dev, bool decode,
Dave Airlie3448a192010-06-01 15:32:24 +10005573 unsigned int command_bits, u32 flags)
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10005574{
5575 struct pci_bus *bus;
5576 struct pci_dev *bridge;
5577 u16 cmd;
Mike Travis95a8b6e2010-02-02 14:38:13 -08005578 int rc;
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10005579
Bjorn Helgaas67ebd812014-04-05 15:14:22 -06005580 WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) && (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY)));
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10005581
Mike Travis95a8b6e2010-02-02 14:38:13 -08005582 /* ARCH specific VGA enables */
Dave Airlie3448a192010-06-01 15:32:24 +10005583 rc = pci_set_vga_state_arch(dev, decode, command_bits, flags);
Mike Travis95a8b6e2010-02-02 14:38:13 -08005584 if (rc)
5585 return rc;
5586
Dave Airlie3448a192010-06-01 15:32:24 +10005587 if (flags & PCI_VGA_STATE_CHANGE_DECODES) {
5588 pci_read_config_word(dev, PCI_COMMAND, &cmd);
5589 if (decode == true)
5590 cmd |= command_bits;
5591 else
5592 cmd &= ~command_bits;
5593 pci_write_config_word(dev, PCI_COMMAND, cmd);
5594 }
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10005595
Dave Airlie3448a192010-06-01 15:32:24 +10005596 if (!(flags & PCI_VGA_STATE_CHANGE_BRIDGE))
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10005597 return 0;
5598
5599 bus = dev->bus;
5600 while (bus) {
5601 bridge = bus->self;
5602 if (bridge) {
5603 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
5604 &cmd);
5605 if (decode == true)
5606 cmd |= PCI_BRIDGE_CTL_VGA;
5607 else
5608 cmd &= ~PCI_BRIDGE_CTL_VGA;
5609 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL,
5610 cmd);
5611 }
5612 bus = bus->parent;
5613 }
5614 return 0;
5615}
5616
Bjorn Helgaasf0af9592016-02-24 13:43:45 -06005617/**
5618 * pci_add_dma_alias - Add a DMA devfn alias for a device
5619 * @dev: the PCI device for which alias is added
5620 * @devfn: alias slot and function
5621 *
5622 * This helper encodes 8-bit devfn as bit number in dma_alias_mask.
5623 * It should be called early, preferably as PCI fixup header quirk.
5624 */
5625void pci_add_dma_alias(struct pci_dev *dev, u8 devfn)
5626{
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01005627 if (!dev->dma_alias_mask)
5628 dev->dma_alias_mask = kcalloc(BITS_TO_LONGS(U8_MAX),
5629 sizeof(long), GFP_KERNEL);
5630 if (!dev->dma_alias_mask) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06005631 pci_warn(dev, "Unable to allocate DMA alias mask\n");
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01005632 return;
5633 }
5634
5635 set_bit(devfn, dev->dma_alias_mask);
Frederick Lawler7506dc72018-01-18 12:55:24 -06005636 pci_info(dev, "Enabling fixed DMA alias to %02x.%d\n",
Bjorn Helgaas48c83082016-02-24 13:43:54 -06005637 PCI_SLOT(devfn), PCI_FUNC(devfn));
Bjorn Helgaasf0af9592016-02-24 13:43:45 -06005638}
5639
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01005640bool pci_devs_are_dma_aliases(struct pci_dev *dev1, struct pci_dev *dev2)
5641{
5642 return (dev1->dma_alias_mask &&
5643 test_bit(dev2->devfn, dev1->dma_alias_mask)) ||
5644 (dev2->dma_alias_mask &&
5645 test_bit(dev1->devfn, dev2->dma_alias_mask));
5646}
5647
Rafael J. Wysocki8496e852013-12-01 02:34:37 +01005648bool pci_device_is_present(struct pci_dev *pdev)
5649{
5650 u32 v;
5651
Keith Buschfe2bd752017-03-29 22:49:17 -05005652 if (pci_dev_is_disconnected(pdev))
5653 return false;
Rafael J. Wysocki8496e852013-12-01 02:34:37 +01005654 return pci_bus_read_dev_vendor_id(pdev->bus, pdev->devfn, &v, 0);
5655}
5656EXPORT_SYMBOL_GPL(pci_device_is_present);
5657
Rafael J. Wysocki08249652015-04-13 16:23:36 +02005658void pci_ignore_hotplug(struct pci_dev *dev)
5659{
5660 struct pci_dev *bridge = dev->bus->self;
5661
5662 dev->ignore_hotplug = 1;
5663 /* Propagate the "ignore hotplug" setting to the parent bridge. */
5664 if (bridge)
5665 bridge->ignore_hotplug = 1;
5666}
5667EXPORT_SYMBOL_GPL(pci_ignore_hotplug);
5668
Yongji Xie0a701aa2017-04-10 19:58:12 +08005669resource_size_t __weak pcibios_default_alignment(void)
5670{
5671 return 0;
5672}
5673
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005674#define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZE
5675static char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE] = {0};
Thomas Gleixnere9d1e492009-11-06 22:41:23 +00005676static DEFINE_SPINLOCK(resource_alignment_lock);
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005677
5678/**
5679 * pci_specified_resource_alignment - get resource alignment specified by user.
5680 * @dev: the PCI device to get
Yongji Xiee3adec72017-04-10 19:58:14 +08005681 * @resize: whether or not to change resources' size when reassigning alignment
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005682 *
5683 * RETURNS: Resource alignment if it is specified.
5684 * Zero if it is not specified.
5685 */
Yongji Xiee3adec72017-04-10 19:58:14 +08005686static resource_size_t pci_specified_resource_alignment(struct pci_dev *dev,
5687 bool *resize)
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005688{
Logan Gunthorpe07d8d7e2018-07-30 10:18:37 -06005689 int align_order, count;
Yongji Xie0a701aa2017-04-10 19:58:12 +08005690 resource_size_t align = pcibios_default_alignment();
Logan Gunthorpe07d8d7e2018-07-30 10:18:37 -06005691 const char *p;
5692 int ret;
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005693
5694 spin_lock(&resource_alignment_lock);
5695 p = resource_alignment_param;
Yongji Xie0a701aa2017-04-10 19:58:12 +08005696 if (!*p && !align)
Yongji Xief0b99f72016-09-13 17:00:31 +08005697 goto out;
5698 if (pci_has_flag(PCI_PROBE_ONLY)) {
Yongji Xie0a701aa2017-04-10 19:58:12 +08005699 align = 0;
Yongji Xief0b99f72016-09-13 17:00:31 +08005700 pr_info_once("PCI: Ignoring requested alignments (PCI_PROBE_ONLY)\n");
5701 goto out;
5702 }
5703
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005704 while (*p) {
5705 count = 0;
5706 if (sscanf(p, "%d%n", &align_order, &count) == 1 &&
5707 p[count] == '@') {
5708 p += count + 1;
5709 } else {
5710 align_order = -1;
5711 }
Logan Gunthorpe07d8d7e2018-07-30 10:18:37 -06005712
5713 ret = pci_dev_str_match(dev, p, &p);
5714 if (ret == 1) {
5715 *resize = true;
5716 if (align_order == -1)
5717 align = PAGE_SIZE;
5718 else
5719 align = 1 << align_order;
5720 break;
5721 } else if (ret < 0) {
5722 pr_err("PCI: Can't parse resource_alignment parameter: %s\n",
5723 p);
5724 break;
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005725 }
Logan Gunthorpe07d8d7e2018-07-30 10:18:37 -06005726
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005727 if (*p != ';' && *p != ',') {
5728 /* End of param or invalid format */
5729 break;
5730 }
5731 p++;
5732 }
Yongji Xief0b99f72016-09-13 17:00:31 +08005733out:
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005734 spin_unlock(&resource_alignment_lock);
5735 return align;
5736}
5737
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005738static void pci_request_resource_alignment(struct pci_dev *dev, int bar,
Yongji Xiee3adec72017-04-10 19:58:14 +08005739 resource_size_t align, bool resize)
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005740{
5741 struct resource *r = &dev->resource[bar];
5742 resource_size_t size;
5743
5744 if (!(r->flags & IORESOURCE_MEM))
5745 return;
5746
5747 if (r->flags & IORESOURCE_PCI_FIXED) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06005748 pci_info(dev, "BAR%d %pR: ignoring requested alignment %#llx\n",
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005749 bar, r, (unsigned long long)align);
5750 return;
5751 }
5752
5753 size = resource_size(r);
Bjorn Helgaas0dde1c02017-04-17 15:20:58 -05005754 if (size >= align)
5755 return;
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005756
Bjorn Helgaas0dde1c02017-04-17 15:20:58 -05005757 /*
Yongji Xiee3adec72017-04-10 19:58:14 +08005758 * Increase the alignment of the resource. There are two ways we
5759 * can do this:
Bjorn Helgaas0dde1c02017-04-17 15:20:58 -05005760 *
Yongji Xiee3adec72017-04-10 19:58:14 +08005761 * 1) Increase the size of the resource. BARs are aligned on their
5762 * size, so when we reallocate space for this resource, we'll
5763 * allocate it with the larger alignment. This also prevents
5764 * assignment of any other BARs inside the alignment region, so
5765 * if we're requesting page alignment, this means no other BARs
5766 * will share the page.
5767 *
5768 * The disadvantage is that this makes the resource larger than
5769 * the hardware BAR, which may break drivers that compute things
5770 * based on the resource size, e.g., to find registers at a
5771 * fixed offset before the end of the BAR.
5772 *
5773 * 2) Retain the resource size, but use IORESOURCE_STARTALIGN and
5774 * set r->start to the desired alignment. By itself this
5775 * doesn't prevent other BARs being put inside the alignment
5776 * region, but if we realign *every* resource of every device in
5777 * the system, none of them will share an alignment region.
5778 *
5779 * When the user has requested alignment for only some devices via
5780 * the "pci=resource_alignment" argument, "resize" is true and we
5781 * use the first method. Otherwise we assume we're aligning all
5782 * devices and we use the second.
Bjorn Helgaas0dde1c02017-04-17 15:20:58 -05005783 */
Yongji Xiee3adec72017-04-10 19:58:14 +08005784
Frederick Lawler7506dc72018-01-18 12:55:24 -06005785 pci_info(dev, "BAR%d %pR: requesting alignment to %#llx\n",
Bjorn Helgaas0dde1c02017-04-17 15:20:58 -05005786 bar, r, (unsigned long long)align);
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005787
Yongji Xiee3adec72017-04-10 19:58:14 +08005788 if (resize) {
5789 r->start = 0;
5790 r->end = align - 1;
5791 } else {
5792 r->flags &= ~IORESOURCE_SIZEALIGN;
5793 r->flags |= IORESOURCE_STARTALIGN;
5794 r->start = align;
5795 r->end = r->start + size - 1;
5796 }
Bjorn Helgaas0dde1c02017-04-17 15:20:58 -05005797 r->flags |= IORESOURCE_UNSET;
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005798}
5799
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005800/*
5801 * This function disables memory decoding and releases memory resources
5802 * of the device specified by kernel's boot parameter 'pci=resource_alignment='.
5803 * It also rounds up size to specified alignment.
5804 * Later on, the kernel will assign page-aligned memory resource back
5805 * to the device.
5806 */
5807void pci_reassigndev_resource_alignment(struct pci_dev *dev)
5808{
5809 int i;
5810 struct resource *r;
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005811 resource_size_t align;
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005812 u16 command;
Yongji Xiee3adec72017-04-10 19:58:14 +08005813 bool resize = false;
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005814
Yongji Xie62d9a782016-09-13 17:00:32 +08005815 /*
5816 * VF BARs are read-only zero according to SR-IOV spec r1.1, sec
5817 * 3.4.1.11. Their resources are allocated from the space
5818 * described by the VF BARx register in the PF's SR-IOV capability.
5819 * We can't influence their alignment here.
5820 */
5821 if (dev->is_virtfn)
5822 return;
5823
Yinghai Lu10c463a2012-03-18 22:46:26 -07005824 /* check if specified PCI is target device to reassign */
Yongji Xiee3adec72017-04-10 19:58:14 +08005825 align = pci_specified_resource_alignment(dev, &resize);
Yinghai Lu10c463a2012-03-18 22:46:26 -07005826 if (!align)
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005827 return;
5828
5829 if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL &&
5830 (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06005831 pci_warn(dev, "Can't reassign resources to host bridge\n");
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005832 return;
5833 }
5834
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005835 pci_read_config_word(dev, PCI_COMMAND, &command);
5836 command &= ~PCI_COMMAND_MEMORY;
5837 pci_write_config_word(dev, PCI_COMMAND, command);
5838
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005839 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
Yongji Xiee3adec72017-04-10 19:58:14 +08005840 pci_request_resource_alignment(dev, i, align, resize);
Yongji Xief0b99f72016-09-13 17:00:31 +08005841
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005842 /*
5843 * Need to disable bridge's resource window,
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005844 * to enable the kernel to reassign new resource
5845 * window later on.
5846 */
5847 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
5848 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
5849 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
5850 r = &dev->resource[i];
5851 if (!(r->flags & IORESOURCE_MEM))
5852 continue;
Bjorn Helgaasbd064f02014-02-26 11:25:58 -07005853 r->flags |= IORESOURCE_UNSET;
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005854 r->end = resource_size(r) - 1;
5855 r->start = 0;
5856 }
5857 pci_disable_bridge_window(dev);
5858 }
5859}
5860
Bjorn Helgaas9738abe2013-04-12 11:20:03 -06005861static ssize_t pci_set_resource_alignment_param(const char *buf, size_t count)
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005862{
5863 if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1)
5864 count = RESOURCE_ALIGNMENT_PARAM_SIZE - 1;
5865 spin_lock(&resource_alignment_lock);
5866 strncpy(resource_alignment_param, buf, count);
5867 resource_alignment_param[count] = '\0';
5868 spin_unlock(&resource_alignment_lock);
5869 return count;
5870}
5871
Bjorn Helgaas9738abe2013-04-12 11:20:03 -06005872static ssize_t pci_get_resource_alignment_param(char *buf, size_t size)
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005873{
5874 size_t count;
5875 spin_lock(&resource_alignment_lock);
5876 count = snprintf(buf, size, "%s", resource_alignment_param);
5877 spin_unlock(&resource_alignment_lock);
5878 return count;
5879}
5880
5881static ssize_t pci_resource_alignment_show(struct bus_type *bus, char *buf)
5882{
5883 return pci_get_resource_alignment_param(buf, PAGE_SIZE);
5884}
5885
5886static ssize_t pci_resource_alignment_store(struct bus_type *bus,
5887 const char *buf, size_t count)
5888{
5889 return pci_set_resource_alignment_param(buf, count);
5890}
5891
Ben Dooks21751a92016-06-09 11:42:13 +01005892static BUS_ATTR(resource_alignment, 0644, pci_resource_alignment_show,
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005893 pci_resource_alignment_store);
5894
5895static int __init pci_resource_alignment_sysfs_init(void)
5896{
5897 return bus_create_file(&pci_bus_type,
5898 &bus_attr_resource_alignment);
5899}
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005900late_initcall(pci_resource_alignment_sysfs_init);
5901
Bill Pemberton15856ad2012-11-21 15:35:00 -05005902static void pci_no_domains(void)
Jeff Garzik32a2eea2007-10-11 16:57:27 -04005903{
5904#ifdef CONFIG_PCI_DOMAINS
5905 pci_domains_supported = 0;
5906#endif
5907}
5908
Jan Kiszkaae07b782018-05-15 11:07:00 +02005909#ifdef CONFIG_PCI_DOMAINS_GENERIC
Liviu Dudau41e5c0f2014-09-29 15:29:27 +01005910static atomic_t __domain_nr = ATOMIC_INIT(-1);
5911
Jan Kiszkaae07b782018-05-15 11:07:00 +02005912static int pci_get_new_domain_nr(void)
Liviu Dudau41e5c0f2014-09-29 15:29:27 +01005913{
5914 return atomic_inc_return(&__domain_nr);
5915}
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -07005916
Tomasz Nowicki1a4f93f2016-06-10 21:55:15 +02005917static int of_pci_bus_find_domain_nr(struct device *parent)
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -07005918{
5919 static int use_dt_domains = -1;
Krzysztof =?utf-8?Q?Ha=C5=82asa?=54c6e2d2016-03-01 07:07:18 +01005920 int domain = -1;
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -07005921
Krzysztof =?utf-8?Q?Ha=C5=82asa?=54c6e2d2016-03-01 07:07:18 +01005922 if (parent)
5923 domain = of_get_pci_domain_nr(parent->of_node);
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -07005924 /*
5925 * Check DT domain and use_dt_domains values.
5926 *
5927 * If DT domain property is valid (domain >= 0) and
5928 * use_dt_domains != 0, the DT assignment is valid since this means
5929 * we have not previously allocated a domain number by using
5930 * pci_get_new_domain_nr(); we should also update use_dt_domains to
5931 * 1, to indicate that we have just assigned a domain number from
5932 * DT.
5933 *
5934 * If DT domain property value is not valid (ie domain < 0), and we
5935 * have not previously assigned a domain number from DT
5936 * (use_dt_domains != 1) we should assign a domain number by
5937 * using the:
5938 *
5939 * pci_get_new_domain_nr()
5940 *
5941 * API and update the use_dt_domains value to keep track of method we
5942 * are using to assign domain numbers (use_dt_domains = 0).
5943 *
5944 * All other combinations imply we have a platform that is trying
5945 * to mix domain numbers obtained from DT and pci_get_new_domain_nr(),
5946 * which is a recipe for domain mishandling and it is prevented by
5947 * invalidating the domain value (domain = -1) and printing a
5948 * corresponding error.
5949 */
5950 if (domain >= 0 && use_dt_domains) {
5951 use_dt_domains = 1;
5952 } else if (domain < 0 && use_dt_domains != 1) {
5953 use_dt_domains = 0;
5954 domain = pci_get_new_domain_nr();
5955 } else {
Shawn Lin9df1c6e2018-03-01 09:26:55 +08005956 if (parent)
5957 pr_err("Node %pOF has ", parent->of_node);
5958 pr_err("Inconsistent \"linux,pci-domain\" property in DT\n");
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -07005959 domain = -1;
5960 }
5961
Tomasz Nowicki9c7cb892016-06-10 21:55:14 +02005962 return domain;
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -07005963}
Tomasz Nowicki1a4f93f2016-06-10 21:55:15 +02005964
5965int pci_bus_find_domain_nr(struct pci_bus *bus, struct device *parent)
5966{
Tomasz Nowicki2ab51dd2016-06-10 15:36:26 -05005967 return acpi_disabled ? of_pci_bus_find_domain_nr(parent) :
5968 acpi_pci_bus_find_domain_nr(bus);
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -07005969}
5970#endif
Liviu Dudau41e5c0f2014-09-29 15:29:27 +01005971
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -07005972/**
Taku Izumi642c92d2012-10-30 15:26:18 +09005973 * pci_ext_cfg_avail - can we access extended PCI config space?
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -07005974 *
5975 * Returns 1 if we can access PCI extended config space (offsets
5976 * greater than 0xff). This is the default implementation. Architecture
5977 * implementations can override this.
5978 */
Taku Izumi642c92d2012-10-30 15:26:18 +09005979int __weak pci_ext_cfg_avail(void)
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -07005980{
5981 return 1;
5982}
5983
Benjamin Herrenschmidt2d1c8612009-12-09 17:52:13 +11005984void __weak pci_fixup_cardbus(struct pci_bus *bus)
5985{
5986}
5987EXPORT_SYMBOL(pci_fixup_cardbus);
5988
Al Viroad04d312008-11-22 17:37:14 +00005989static int __init pci_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990{
5991 while (str) {
5992 char *k = strchr(str, ',');
5993 if (k)
5994 *k++ = 0;
5995 if (*str && (str = pcibios_setup(str)) && *str) {
Matthew Wilcox309e57d2006-03-05 22:33:34 -07005996 if (!strcmp(str, "nomsi")) {
5997 pci_no_msi();
Gil Kupfercef74402018-05-10 17:56:02 -05005998 } else if (!strncmp(str, "noats", 5)) {
5999 pr_info("PCIe: ATS is disabled\n");
6000 pcie_ats_disabled = true;
Randy Dunlap7f785762007-10-05 13:17:58 -07006001 } else if (!strcmp(str, "noaer")) {
6002 pci_no_aer();
Yinghai Lub55438f2012-02-23 19:23:30 -08006003 } else if (!strncmp(str, "realloc=", 8)) {
6004 pci_realloc_get_opt(str + 8);
Ram Paif483d392011-07-07 11:19:10 -07006005 } else if (!strncmp(str, "realloc", 7)) {
Yinghai Lub55438f2012-02-23 19:23:30 -08006006 pci_realloc_get_opt("on");
Jeff Garzik32a2eea2007-10-11 16:57:27 -04006007 } else if (!strcmp(str, "nodomains")) {
6008 pci_no_domains();
Rafael J. Wysocki6748dcc2012-03-01 00:06:33 +01006009 } else if (!strncmp(str, "noari", 5)) {
6010 pcie_ari_disabled = true;
Atsushi Nemoto4516a612007-02-05 16:36:06 -08006011 } else if (!strncmp(str, "cbiosize=", 9)) {
6012 pci_cardbus_io_size = memparse(str + 9, &str);
6013 } else if (!strncmp(str, "cbmemsize=", 10)) {
6014 pci_cardbus_mem_size = memparse(str + 10, &str);
Yuji Shimada32a9a6822009-03-16 17:13:39 +09006015 } else if (!strncmp(str, "resource_alignment=", 19)) {
6016 pci_set_resource_alignment_param(str + 19,
6017 strlen(str + 19));
Andrew Patterson43c16402009-04-22 16:52:09 -06006018 } else if (!strncmp(str, "ecrc=", 5)) {
6019 pcie_ecrc_get_policy(str + 5);
Eric W. Biederman28760482009-09-09 14:09:24 -07006020 } else if (!strncmp(str, "hpiosize=", 9)) {
6021 pci_hotplug_io_size = memparse(str + 9, &str);
6022 } else if (!strncmp(str, "hpmemsize=", 10)) {
6023 pci_hotplug_mem_size = memparse(str + 10, &str);
Keith Busche16b4662016-07-21 21:40:28 -06006024 } else if (!strncmp(str, "hpbussize=", 10)) {
6025 pci_hotplug_bus_size =
6026 simple_strtoul(str + 10, &str, 0);
6027 if (pci_hotplug_bus_size > 0xff)
6028 pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;
Jon Mason5f39e672011-10-03 09:50:20 -05006029 } else if (!strncmp(str, "pcie_bus_tune_off", 17)) {
6030 pcie_bus_config = PCIE_BUS_TUNE_OFF;
Jon Masonb03e7492011-07-20 15:20:54 -05006031 } else if (!strncmp(str, "pcie_bus_safe", 13)) {
6032 pcie_bus_config = PCIE_BUS_SAFE;
6033 } else if (!strncmp(str, "pcie_bus_perf", 13)) {
6034 pcie_bus_config = PCIE_BUS_PERFORMANCE;
Jon Mason5f39e672011-10-03 09:50:20 -05006035 } else if (!strncmp(str, "pcie_bus_peer2peer", 18)) {
6036 pcie_bus_config = PCIE_BUS_PEER2PEER;
Bjorn Helgaas284f5f92012-04-30 15:21:02 -06006037 } else if (!strncmp(str, "pcie_scan_all", 13)) {
6038 pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS);
Logan Gunthorpeaaca43f2018-07-30 10:18:40 -06006039 } else if (!strncmp(str, "disable_acs_redir=", 18)) {
6040 disable_acs_redir_param = str + 18;
Matthew Wilcox309e57d2006-03-05 22:33:34 -07006041 } else {
6042 printk(KERN_ERR "PCI: Unknown option `%s'\n",
6043 str);
6044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 }
6046 str = k;
6047 }
Andi Kleen0637a702006-09-26 10:52:41 +02006048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049}
Andi Kleen0637a702006-09-26 10:52:41 +02006050early_param("pci", pci_setup);