blob: 0b8e8ee84bbcec6c04ada4e7def1bab4cd5becdd [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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * PCI Bus Services, see include/linux/pci.h for further explanation.
4 *
5 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
6 * David Mosberger-Tang
7 *
8 * Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz>
9 */
10
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>
Shaohua Li7d715a62008-02-25 09:46:41 +080025#include <linux/pci-aspm.h>
Stephen Rothwellc300bd2fb2008-07-10 02:16:44 +020026#include <linux/pm_wakeup.h>
Sheng Yang8dd7f802008-10-21 17:38:25 +080027#include <linux/interrupt.h>
Yuji Shimada32a9a6822009-03-16 17:13:39 +090028#include <linux/device.h>
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010029#include <linux/pm_runtime.h>
Alex Williamson608c3882013-08-08 14:09:43 -060030#include <linux/pci_hotplug.h>
Sinan Kaya4d3f1382016-06-10 21:55:11 +020031#include <linux/vmalloc.h>
CQ Tang4ebeb1e2017-05-30 09:25:49 -070032#include <linux/pci-ats.h>
Yuji Shimada32a9a6822009-03-16 17:13:39 +090033#include <asm/setup.h>
Ben Dooks2a2aca32016-06-17 16:05:13 +010034#include <asm/dma.h>
Taku Izumib07461a2015-09-17 10:09:37 -050035#include <linux/aer.h>
Greg KHbc56b9e2005-04-08 14:53:31 +090036#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Alan Stern00240c32009-04-27 13:33:16 -040038const char *pci_power_names[] = {
39 "error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown",
40};
41EXPORT_SYMBOL_GPL(pci_power_names);
42
Rafael J. Wysocki93177a72010-01-02 22:57:24 +010043int isa_dma_bridge_buggy;
44EXPORT_SYMBOL(isa_dma_bridge_buggy);
45
46int pci_pci_problems;
47EXPORT_SYMBOL(pci_pci_problems);
48
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +010049unsigned int pci_pm_d3_delay;
50
Matthew Garrettdf17e622010-10-04 14:22:29 -040051static void pci_pme_list_scan(struct work_struct *work);
52
53static LIST_HEAD(pci_pme_list);
54static DEFINE_MUTEX(pci_pme_list_mutex);
55static DECLARE_DELAYED_WORK(pci_pme_work, pci_pme_list_scan);
56
57struct pci_pme_device {
58 struct list_head list;
59 struct pci_dev *dev;
60};
61
62#define PME_TIMEOUT 1000 /* How long between PME checks */
63
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +010064static void pci_dev_d3_sleep(struct pci_dev *dev)
65{
66 unsigned int delay = dev->d3_delay;
67
68 if (delay < pci_pm_d3_delay)
69 delay = pci_pm_d3_delay;
70
Adrian Hunter50b2b542017-03-14 15:21:58 +020071 if (delay)
72 msleep(delay);
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +010073}
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Jeff Garzik32a2eea2007-10-11 16:57:27 -040075#ifdef CONFIG_PCI_DOMAINS
76int pci_domains_supported = 1;
77#endif
78
Atsushi Nemoto4516a612007-02-05 16:36:06 -080079#define DEFAULT_CARDBUS_IO_SIZE (256)
80#define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
81/* pci=cbmemsize=nnM,cbiosize=nn can override this */
82unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
83unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
84
Eric W. Biederman28760482009-09-09 14:09:24 -070085#define DEFAULT_HOTPLUG_IO_SIZE (256)
86#define DEFAULT_HOTPLUG_MEM_SIZE (2*1024*1024)
87/* pci=hpmemsize=nnM,hpiosize=nn can override this */
88unsigned long pci_hotplug_io_size = DEFAULT_HOTPLUG_IO_SIZE;
89unsigned long pci_hotplug_mem_size = DEFAULT_HOTPLUG_MEM_SIZE;
90
Keith Busche16b4662016-07-21 21:40:28 -060091#define DEFAULT_HOTPLUG_BUS_SIZE 1
92unsigned long pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;
93
Keith Busch27d868b2015-08-24 08:48:16 -050094enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_DEFAULT;
Jon Masonb03e7492011-07-20 15:20:54 -050095
Jesse Barnesac1aa472009-10-26 13:20:44 -070096/*
97 * The default CLS is used if arch didn't set CLS explicitly and not
98 * all pci devices agree on the same value. Arch can override either
99 * the dfl or actual value as it sees fit. Don't forget this is
100 * measured in 32-bit words, not bytes.
101 */
Bill Pemberton15856ad2012-11-21 15:35:00 -0500102u8 pci_dfl_cache_line_size = L1_CACHE_BYTES >> 2;
Jesse Barnesac1aa472009-10-26 13:20:44 -0700103u8 pci_cache_line_size;
104
Myron Stowe96c55902011-10-28 15:48:38 -0600105/*
106 * If we set up a device for bus mastering, we need to check the latency
107 * timer as certain BIOSes forget to set it properly.
108 */
109unsigned int pcibios_max_latency = 255;
110
Rafael J. Wysocki6748dcc2012-03-01 00:06:33 +0100111/* If set, the PCIe ARI capability will not be used. */
112static bool pcie_ari_disabled;
113
Mika Westerberg9d26d3a2016-06-02 11:17:12 +0300114/* Disable bridge_d3 for all PCIe ports */
115static bool pci_bridge_d3_disable;
116/* Force bridge_d3 for all PCIe ports */
117static bool pci_bridge_d3_force;
118
119static int __init pcie_port_pm_setup(char *str)
120{
121 if (!strcmp(str, "off"))
122 pci_bridge_d3_disable = true;
123 else if (!strcmp(str, "force"))
124 pci_bridge_d3_force = true;
125 return 1;
126}
127__setup("pcie_port_pm=", pcie_port_pm_setup);
128
Sinan Kayaa2758b62018-02-27 14:14:10 -0600129/* Time to wait after a reset for device to become responsive */
130#define PCIE_RESET_READY_POLL_MS 60000
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/**
133 * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
134 * @bus: pointer to PCI bus structure to search
135 *
136 * Given a PCI bus, returns the highest PCI bus number present in the set
137 * including the given PCI bus and its list of child PCI buses.
138 */
Ryan Desfosses07656d83082014-04-11 01:01:53 -0400139unsigned char pci_bus_max_busnr(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Yijing Wang94e6a9b2014-02-13 21:14:03 +0800141 struct pci_bus *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 unsigned char max, n;
143
Yinghai Lub918c622012-05-17 18:51:11 -0700144 max = bus->busn_res.end;
Yijing Wang94e6a9b2014-02-13 21:14:03 +0800145 list_for_each_entry(tmp, &bus->children, node) {
146 n = pci_bus_max_busnr(tmp);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400147 if (n > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 max = n;
149 }
150 return max;
151}
Kristen Accardib82db5c2006-01-17 16:56:56 -0800152EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Andrew Morton1684f5d2008-12-01 14:30:30 -0800154#ifdef CONFIG_HAS_IOMEM
155void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
156{
Bjorn Helgaas1f7bf3bf2015-03-12 12:30:11 -0500157 struct resource *res = &pdev->resource[bar];
158
Andrew Morton1684f5d2008-12-01 14:30:30 -0800159 /*
160 * Make sure the BAR is actually a memory resource, not an IO resource
161 */
Bjorn Helgaas646c0282015-03-12 12:30:15 -0500162 if (res->flags & IORESOURCE_UNSET || !(res->flags & IORESOURCE_MEM)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600163 pci_warn(pdev, "can't ioremap BAR %d: %pR\n", bar, res);
Andrew Morton1684f5d2008-12-01 14:30:30 -0800164 return NULL;
165 }
Bjorn Helgaas1f7bf3bf2015-03-12 12:30:11 -0500166 return ioremap_nocache(res->start, resource_size(res));
Andrew Morton1684f5d2008-12-01 14:30:30 -0800167}
168EXPORT_SYMBOL_GPL(pci_ioremap_bar);
Luis R. Rodriguezc43996f2015-08-24 12:13:23 -0700169
170void __iomem *pci_ioremap_wc_bar(struct pci_dev *pdev, int bar)
171{
172 /*
173 * Make sure the BAR is actually a memory resource, not an IO resource
174 */
175 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
176 WARN_ON(1);
177 return NULL;
178 }
179 return ioremap_wc(pci_resource_start(pdev, bar),
180 pci_resource_len(pdev, bar));
181}
182EXPORT_SYMBOL_GPL(pci_ioremap_wc_bar);
Andrew Morton1684f5d2008-12-01 14:30:30 -0800183#endif
184
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100185
186static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
187 u8 pos, int cap, int *ttl)
Roland Dreier24a4e372005-10-28 17:35:34 -0700188{
189 u8 id;
Sean O. Stalley55db3202015-04-02 14:10:19 -0700190 u16 ent;
191
192 pci_bus_read_config_byte(bus, devfn, pos, &pos);
Roland Dreier24a4e372005-10-28 17:35:34 -0700193
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100194 while ((*ttl)--) {
Roland Dreier24a4e372005-10-28 17:35:34 -0700195 if (pos < 0x40)
196 break;
197 pos &= ~3;
Sean O. Stalley55db3202015-04-02 14:10:19 -0700198 pci_bus_read_config_word(bus, devfn, pos, &ent);
199
200 id = ent & 0xff;
Roland Dreier24a4e372005-10-28 17:35:34 -0700201 if (id == 0xff)
202 break;
203 if (id == cap)
204 return pos;
Sean O. Stalley55db3202015-04-02 14:10:19 -0700205 pos = (ent >> 8);
Roland Dreier24a4e372005-10-28 17:35:34 -0700206 }
207 return 0;
208}
209
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100210static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
211 u8 pos, int cap)
212{
213 int ttl = PCI_FIND_CAP_TTL;
214
215 return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
216}
217
Roland Dreier24a4e372005-10-28 17:35:34 -0700218int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
219{
220 return __pci_find_next_cap(dev->bus, dev->devfn,
221 pos + PCI_CAP_LIST_NEXT, cap);
222}
223EXPORT_SYMBOL_GPL(pci_find_next_capability);
224
Michael Ellermand3bac112006-11-22 18:26:16 +1100225static int __pci_bus_find_cap_start(struct pci_bus *bus,
226 unsigned int devfn, u8 hdr_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
231 if (!(status & PCI_STATUS_CAP_LIST))
232 return 0;
233
234 switch (hdr_type) {
235 case PCI_HEADER_TYPE_NORMAL:
236 case PCI_HEADER_TYPE_BRIDGE:
Michael Ellermand3bac112006-11-22 18:26:16 +1100237 return PCI_CAPABILITY_LIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 case PCI_HEADER_TYPE_CARDBUS:
Michael Ellermand3bac112006-11-22 18:26:16 +1100239 return PCI_CB_CAPABILITY_LIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Michael Ellermand3bac112006-11-22 18:26:16 +1100241
242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245/**
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700246 * pci_find_capability - query for devices' capabilities
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * @dev: PCI device to query
248 * @cap: capability code
249 *
250 * Tell if a device supports a given PCI capability.
251 * Returns the address of the requested capability structure within the
252 * device's PCI configuration space or 0 in case the device does not
253 * support it. Possible values for @cap:
254 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700255 * %PCI_CAP_ID_PM Power Management
256 * %PCI_CAP_ID_AGP Accelerated Graphics Port
257 * %PCI_CAP_ID_VPD Vital Product Data
258 * %PCI_CAP_ID_SLOTID Slot Identification
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * %PCI_CAP_ID_MSI Message Signalled Interrupts
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700260 * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * %PCI_CAP_ID_PCIX PCI-X
262 * %PCI_CAP_ID_EXP PCI Express
263 */
264int pci_find_capability(struct pci_dev *dev, int cap)
265{
Michael Ellermand3bac112006-11-22 18:26:16 +1100266 int pos;
267
268 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
269 if (pos)
270 pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
271
272 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600274EXPORT_SYMBOL(pci_find_capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276/**
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700277 * pci_bus_find_capability - query for devices' capabilities
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * @bus: the PCI bus to query
279 * @devfn: PCI device to query
280 * @cap: capability code
281 *
282 * Like pci_find_capability() but works for pci devices that do not have a
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700283 * pci_dev structure set up yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 *
285 * Returns the address of the requested capability structure within the
286 * device's PCI configuration space or 0 in case the device does not
287 * support it.
288 */
289int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
290{
Michael Ellermand3bac112006-11-22 18:26:16 +1100291 int pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 u8 hdr_type;
293
294 pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
295
Michael Ellermand3bac112006-11-22 18:26:16 +1100296 pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
297 if (pos)
298 pos = __pci_find_next_cap(bus, devfn, pos, cap);
299
300 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600302EXPORT_SYMBOL(pci_bus_find_capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304/**
Bjorn Helgaas44a9a362012-07-13 14:24:59 -0600305 * pci_find_next_ext_capability - Find an extended capability
306 * @dev: PCI device to query
307 * @start: address at which to start looking (0 to start at beginning of list)
308 * @cap: capability code
309 *
310 * Returns the address of the next matching extended capability structure
311 * within the device's PCI configuration space or 0 if the device does
312 * not support it. Some capabilities can occur several times, e.g., the
313 * vendor-specific capability, and this provides a way to find them all.
314 */
315int pci_find_next_ext_capability(struct pci_dev *dev, int start, int cap)
316{
317 u32 header;
318 int ttl;
319 int pos = PCI_CFG_SPACE_SIZE;
320
321 /* minimum 8 bytes per capability */
322 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
323
324 if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
325 return 0;
326
327 if (start)
328 pos = start;
329
330 if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
331 return 0;
332
333 /*
334 * If we have no capabilities, this is indicated by cap ID,
335 * cap version and next pointer all being 0.
336 */
337 if (header == 0)
338 return 0;
339
340 while (ttl-- > 0) {
341 if (PCI_EXT_CAP_ID(header) == cap && pos != start)
342 return pos;
343
344 pos = PCI_EXT_CAP_NEXT(header);
345 if (pos < PCI_CFG_SPACE_SIZE)
346 break;
347
348 if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
349 break;
350 }
351
352 return 0;
353}
354EXPORT_SYMBOL_GPL(pci_find_next_ext_capability);
355
356/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * pci_find_ext_capability - Find an extended capability
358 * @dev: PCI device to query
359 * @cap: capability code
360 *
361 * Returns the address of the requested extended capability structure
362 * within the device's PCI configuration space or 0 if the device does
363 * not support it. Possible values for @cap:
364 *
365 * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
366 * %PCI_EXT_CAP_ID_VC Virtual Channel
367 * %PCI_EXT_CAP_ID_DSN Device Serial Number
368 * %PCI_EXT_CAP_ID_PWR Power Budgeting
369 */
370int pci_find_ext_capability(struct pci_dev *dev, int cap)
371{
Bjorn Helgaas44a9a362012-07-13 14:24:59 -0600372 return pci_find_next_ext_capability(dev, 0, cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
Brice Goglin3a720d72006-05-23 06:10:01 -0400374EXPORT_SYMBOL_GPL(pci_find_ext_capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100376static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
377{
378 int rc, ttl = PCI_FIND_CAP_TTL;
379 u8 cap, mask;
380
381 if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
382 mask = HT_3BIT_CAP_MASK;
383 else
384 mask = HT_5BIT_CAP_MASK;
385
386 pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
387 PCI_CAP_ID_HT, &ttl);
388 while (pos) {
389 rc = pci_read_config_byte(dev, pos + 3, &cap);
390 if (rc != PCIBIOS_SUCCESSFUL)
391 return 0;
392
393 if ((cap & mask) == ht_cap)
394 return pos;
395
Brice Goglin47a4d5b2007-01-10 23:15:29 -0800396 pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
397 pos + PCI_CAP_LIST_NEXT,
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100398 PCI_CAP_ID_HT, &ttl);
399 }
400
401 return 0;
402}
403/**
404 * pci_find_next_ht_capability - query a device's Hypertransport capabilities
405 * @dev: PCI device to query
406 * @pos: Position from which to continue searching
407 * @ht_cap: Hypertransport capability code
408 *
409 * To be used in conjunction with pci_find_ht_capability() to search for
410 * all capabilities matching @ht_cap. @pos should always be a value returned
411 * from pci_find_ht_capability().
412 *
413 * NB. To be 100% safe against broken PCI devices, the caller should take
414 * steps to avoid an infinite loop.
415 */
416int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
417{
418 return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
419}
420EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
421
422/**
423 * pci_find_ht_capability - query a device's Hypertransport capabilities
424 * @dev: PCI device to query
425 * @ht_cap: Hypertransport capability code
426 *
427 * Tell if a device supports a given Hypertransport capability.
428 * Returns an address within the device's PCI configuration space
429 * or 0 in case the device does not support the request capability.
430 * The address points to the PCI capability, of type PCI_CAP_ID_HT,
431 * which has a Hypertransport capability matching @ht_cap.
432 */
433int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
434{
435 int pos;
436
437 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
438 if (pos)
439 pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
440
441 return pos;
442}
443EXPORT_SYMBOL_GPL(pci_find_ht_capability);
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445/**
446 * pci_find_parent_resource - return resource region of parent bus of given region
447 * @dev: PCI device structure contains resources to be searched
448 * @res: child resource record for which parent is sought
449 *
450 * For given resource region of given device, return the resource
Bjorn Helgaasf44116a2014-02-26 11:25:58 -0700451 * region of parent bus the given region is contained in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400453struct resource *pci_find_parent_resource(const struct pci_dev *dev,
454 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 const struct pci_bus *bus = dev->bus;
Bjorn Helgaasf44116a2014-02-26 11:25:58 -0700457 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700460 pci_bus_for_each_resource(bus, r, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (!r)
462 continue;
Ard Biesheuvel31342332017-04-11 17:33:12 +0100463 if (resource_contains(r, res)) {
Bjorn Helgaasf44116a2014-02-26 11:25:58 -0700464
465 /*
466 * If the window is prefetchable but the BAR is
467 * not, the allocator made a mistake.
468 */
469 if (r->flags & IORESOURCE_PREFETCH &&
470 !(res->flags & IORESOURCE_PREFETCH))
471 return NULL;
472
473 /*
474 * If we're below a transparent bridge, there may
475 * be both a positively-decoded aperture and a
476 * subtractively-decoded region that contain the BAR.
477 * We want the positively-decoded one, so this depends
478 * on pci_bus_for_each_resource() giving us those
479 * first.
480 */
481 return r;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Bjorn Helgaasf44116a2014-02-26 11:25:58 -0700484 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600486EXPORT_SYMBOL(pci_find_parent_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488/**
Mika Westerbergafd29f92016-09-15 11:07:03 +0300489 * pci_find_resource - Return matching PCI device resource
490 * @dev: PCI device to query
491 * @res: Resource to look for
492 *
493 * Goes over standard PCI resources (BARs) and checks if the given resource
494 * is partially or fully contained in any of them. In that case the
495 * matching resource is returned, %NULL otherwise.
496 */
497struct resource *pci_find_resource(struct pci_dev *dev, struct resource *res)
498{
499 int i;
500
501 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
502 struct resource *r = &dev->resource[i];
503
504 if (r->start && resource_contains(r, res))
505 return r;
506 }
507
508 return NULL;
509}
510EXPORT_SYMBOL(pci_find_resource);
511
512/**
Hariprasad Shenaic56d4452015-10-18 19:55:04 +0530513 * pci_find_pcie_root_port - return PCIe Root Port
514 * @dev: PCI device to query
515 *
516 * Traverse up the parent chain and return the PCIe Root Port PCI Device
517 * for a given PCI Device.
518 */
519struct pci_dev *pci_find_pcie_root_port(struct pci_dev *dev)
520{
Thierry Redingb6f6d562017-08-17 13:06:14 +0200521 struct pci_dev *bridge, *highest_pcie_bridge = dev;
Hariprasad Shenaic56d4452015-10-18 19:55:04 +0530522
523 bridge = pci_upstream_bridge(dev);
524 while (bridge && pci_is_pcie(bridge)) {
525 highest_pcie_bridge = bridge;
526 bridge = pci_upstream_bridge(bridge);
527 }
528
Thierry Redingb6f6d562017-08-17 13:06:14 +0200529 if (pci_pcie_type(highest_pcie_bridge) != PCI_EXP_TYPE_ROOT_PORT)
530 return NULL;
Hariprasad Shenaic56d4452015-10-18 19:55:04 +0530531
Thierry Redingb6f6d562017-08-17 13:06:14 +0200532 return highest_pcie_bridge;
Hariprasad Shenaic56d4452015-10-18 19:55:04 +0530533}
534EXPORT_SYMBOL(pci_find_pcie_root_port);
535
536/**
Alex Williamson157e8762013-12-17 16:43:39 -0700537 * pci_wait_for_pending - wait for @mask bit(s) to clear in status word @pos
538 * @dev: the PCI device to operate on
539 * @pos: config space offset of status word
540 * @mask: mask of bit(s) to care about in status word
541 *
542 * Return 1 when mask bit(s) in status word clear, 0 otherwise.
543 */
544int pci_wait_for_pending(struct pci_dev *dev, int pos, u16 mask)
545{
546 int i;
547
548 /* Wait for Transaction Pending bit clean */
549 for (i = 0; i < 4; i++) {
550 u16 status;
551 if (i)
552 msleep((1 << (i - 1)) * 100);
553
554 pci_read_config_word(dev, pos, &status);
555 if (!(status & mask))
556 return 1;
557 }
558
559 return 0;
560}
561
562/**
Wei Yang70675e02015-07-29 16:52:58 +0800563 * pci_restore_bars - restore a device's BAR values (e.g. after wake-up)
John W. Linville064b53db2005-07-27 10:19:44 -0400564 * @dev: PCI device to have its BARs restored
565 *
566 * Restore the BAR values for a given device, so as to make it
567 * accessible by its driver.
568 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400569static void pci_restore_bars(struct pci_dev *dev)
John W. Linville064b53db2005-07-27 10:19:44 -0400570{
Yu Zhaobc5f5a82008-11-22 02:40:00 +0800571 int i;
John W. Linville064b53db2005-07-27 10:19:44 -0400572
Yu Zhaobc5f5a82008-11-22 02:40:00 +0800573 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
Yu Zhao14add802008-11-22 02:38:52 +0800574 pci_update_resource(dev, i);
John W. Linville064b53db2005-07-27 10:19:44 -0400575}
576
Julia Lawall299f2ff2015-12-06 17:33:45 +0100577static const struct pci_platform_pm_ops *pci_platform_pm;
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200578
Julia Lawall299f2ff2015-12-06 17:33:45 +0100579int pci_set_platform_pm(const struct pci_platform_pm_ops *ops)
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200580{
Lukas Wunnercc7cc022016-09-18 05:39:20 +0200581 if (!ops->is_manageable || !ops->set_state || !ops->get_state ||
Rafael J. Wysocki08476842017-06-24 01:57:35 +0200582 !ops->choose_state || !ops->set_wakeup || !ops->need_resume)
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200583 return -EINVAL;
584 pci_platform_pm = ops;
585 return 0;
586}
587
588static inline bool platform_pci_power_manageable(struct pci_dev *dev)
589{
590 return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
591}
592
593static inline int platform_pci_set_power_state(struct pci_dev *dev,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400594 pci_power_t t)
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200595{
596 return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
597}
598
Lukas Wunnercc7cc022016-09-18 05:39:20 +0200599static inline pci_power_t platform_pci_get_power_state(struct pci_dev *dev)
600{
601 return pci_platform_pm ? pci_platform_pm->get_state(dev) : PCI_UNKNOWN;
602}
603
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200604static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
605{
606 return pci_platform_pm ?
607 pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
608}
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700609
Rafael J. Wysocki08476842017-06-24 01:57:35 +0200610static inline int platform_pci_set_wakeup(struct pci_dev *dev, bool enable)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200611{
612 return pci_platform_pm ?
Rafael J. Wysocki08476842017-06-24 01:57:35 +0200613 pci_platform_pm->set_wakeup(dev, enable) : -ENODEV;
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100614}
615
Rafael J. Wysockibac2a902015-01-21 02:17:42 +0100616static inline bool platform_pci_need_resume(struct pci_dev *dev)
617{
618 return pci_platform_pm ? pci_platform_pm->need_resume(dev) : false;
619}
620
John W. Linville064b53db2005-07-27 10:19:44 -0400621/**
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200622 * pci_raw_set_power_state - Use PCI PM registers to set the power state of
623 * given PCI device
624 * @dev: PCI device to handle.
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200625 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 *
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200627 * RETURN VALUE:
628 * -EINVAL if the requested state is invalid.
629 * -EIO if device does not support PCI PM or its PM capabilities register has a
630 * wrong version, or device doesn't support the requested state.
631 * 0 if device already is in the requested state.
632 * 0 if device's power state has been successfully changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
Rafael J. Wysockif00a20e2009-03-16 22:40:08 +0100634static int pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200636 u16 pmcsr;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200637 bool need_restore = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Rafael J. Wysocki4a865902009-03-16 22:40:36 +0100639 /* Check if we're already there */
640 if (dev->current_state == state)
641 return 0;
642
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200643 if (!dev->pm_cap)
Andrew Lunncca03de2007-07-09 11:55:58 -0700644 return -EIO;
645
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200646 if (state < PCI_D0 || state > PCI_D3hot)
647 return -EINVAL;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* Validate current state:
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700650 * Can enter D0 from any state, but if we can only go deeper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 * to sleep if we're already in a low power state
652 */
Rafael J. Wysocki4a865902009-03-16 22:40:36 +0100653 if (state != PCI_D0 && dev->current_state <= PCI_D3cold
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200654 && dev->current_state > state) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600655 pci_err(dev, "invalid power transition (from state %d to %d)\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -0400656 dev->current_state, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return -EINVAL;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /* check if this device supports the desired state */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200661 if ((state == PCI_D1 && !dev->d1_support)
662 || (state == PCI_D2 && !dev->d2_support))
Daniel Ritz3fe9d192005-08-17 15:32:19 -0700663 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200665 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
John W. Linville064b53db2005-07-27 10:19:44 -0400666
John W. Linville32a36582005-09-14 09:52:42 -0400667 /* If we're (effectively) in D3, force entire word to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 * This doesn't affect PME_Status, disables PME_En, and
669 * sets PowerState to 0.
670 */
John W. Linville32a36582005-09-14 09:52:42 -0400671 switch (dev->current_state) {
John W. Linvilled3535fb2005-09-28 17:50:51 -0400672 case PCI_D0:
673 case PCI_D1:
674 case PCI_D2:
675 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
676 pmcsr |= state;
677 break;
Rafael J. Wysockif62795f2009-05-18 22:51:12 +0200678 case PCI_D3hot:
679 case PCI_D3cold:
John W. Linville32a36582005-09-14 09:52:42 -0400680 case PCI_UNKNOWN: /* Boot-up */
681 if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
Rafael J. Wysockif00a20e2009-03-16 22:40:08 +0100682 && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200683 need_restore = true;
John W. Linville32a36582005-09-14 09:52:42 -0400684 /* Fall-through: force to D0 */
John W. Linville32a36582005-09-14 09:52:42 -0400685 default:
John W. Linvilled3535fb2005-09-28 17:50:51 -0400686 pmcsr = 0;
John W. Linville32a36582005-09-14 09:52:42 -0400687 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
690 /* enter specified state */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200691 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /* Mandatory power management transition delays */
694 /* see PCI PM 1.1 5.6.1 table 18 */
695 if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +0100696 pci_dev_d3_sleep(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 else if (state == PCI_D2 || dev->current_state == PCI_D2)
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100698 udelay(PCI_PM_D2_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Rafael J. Wysockie13cdbd2009-10-05 00:48:40 +0200700 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
701 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
702 if (dev->current_state != state && printk_ratelimit())
Frederick Lawler7506dc72018-01-18 12:55:24 -0600703 pci_info(dev, "Refused to change power state, currently in D%d\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -0400704 dev->current_state);
John W. Linville064b53db2005-07-27 10:19:44 -0400705
Huang Ying448bd852012-06-23 10:23:51 +0800706 /*
707 * According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
John W. Linville064b53db2005-07-27 10:19:44 -0400708 * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
709 * from D3hot to D0 _may_ perform an internal reset, thereby
710 * going to "D0 Uninitialized" rather than "D0 Initialized".
711 * For example, at least some versions of the 3c905B and the
712 * 3c556B exhibit this behaviour.
713 *
714 * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
715 * devices in a D3hot state at boot. Consequently, we need to
716 * restore at least the BARs so that the device will be
717 * accessible to its driver.
718 */
719 if (need_restore)
720 pci_restore_bars(dev);
721
Rafael J. Wysockif00a20e2009-03-16 22:40:08 +0100722 if (dev->bus->self)
Shaohua Li7d715a62008-02-25 09:46:41 +0800723 pcie_aspm_pm_state_change(dev->bus->self);
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
726}
727
728/**
Lukas Wunnera6a64022016-09-18 05:39:20 +0200729 * pci_update_current_state - Read power state of given device and cache it
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200730 * @dev: PCI device to handle.
Rafael J. Wysockif06fc0b2008-12-27 16:30:52 +0100731 * @state: State to cache in case the device doesn't have the PM capability
Lukas Wunnera6a64022016-09-18 05:39:20 +0200732 *
733 * The power state is read from the PMCSR register, which however is
734 * inaccessible in D3cold. The platform firmware is therefore queried first
735 * to detect accessibility of the register. In case the platform firmware
736 * reports an incorrect state or the device isn't power manageable by the
737 * platform at all, we try to detect D3cold by testing accessibility of the
738 * vendor ID in config space.
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200739 */
Rafael J. Wysocki734104292009-01-07 13:07:15 +0100740void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200741{
Lukas Wunnera6a64022016-09-18 05:39:20 +0200742 if (platform_pci_get_power_state(dev) == PCI_D3cold ||
743 !pci_device_is_present(dev)) {
744 dev->current_state = PCI_D3cold;
745 } else if (dev->pm_cap) {
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200746 u16 pmcsr;
747
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200748 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200749 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
Rafael J. Wysockif06fc0b2008-12-27 16:30:52 +0100750 } else {
751 dev->current_state = state;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200752 }
753}
754
755/**
Rafael J. Wysockidb288c92012-07-05 15:20:00 -0600756 * pci_power_up - Put the given device into D0 forcibly
757 * @dev: PCI device to power up
758 */
759void pci_power_up(struct pci_dev *dev)
760{
761 if (platform_pci_power_manageable(dev))
762 platform_pci_set_power_state(dev, PCI_D0);
763
764 pci_raw_set_power_state(dev, PCI_D0);
765 pci_update_current_state(dev, PCI_D0);
766}
767
768/**
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100769 * pci_platform_power_transition - Use platform to change device power state
770 * @dev: PCI device to handle.
771 * @state: State to put the device into.
772 */
773static int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
774{
775 int error;
776
777 if (platform_pci_power_manageable(dev)) {
778 error = platform_pci_set_power_state(dev, state);
779 if (!error)
780 pci_update_current_state(dev, state);
Rafael J. Wysocki769ba722013-04-12 13:58:17 +0000781 } else
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100782 error = -ENODEV;
Rafael J. Wysocki769ba722013-04-12 13:58:17 +0000783
784 if (error && !dev->pm_cap) /* Fall back to PCI_D0 */
785 dev->current_state = PCI_D0;
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100786
787 return error;
788}
789
790/**
Stephen Hemminger0b950f02014-01-10 17:14:48 -0700791 * pci_wakeup - Wake up a PCI device
792 * @pci_dev: Device to handle.
793 * @ign: ignored parameter
794 */
795static int pci_wakeup(struct pci_dev *pci_dev, void *ign)
796{
797 pci_wakeup_event(pci_dev);
798 pm_request_resume(&pci_dev->dev);
799 return 0;
800}
801
802/**
803 * pci_wakeup_bus - Walk given bus and wake up devices on it
804 * @bus: Top bus of the subtree to walk.
805 */
806static void pci_wakeup_bus(struct pci_bus *bus)
807{
808 if (bus)
809 pci_walk_bus(bus, pci_wakeup, NULL);
810}
811
812/**
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100813 * __pci_start_power_transition - Start power transition of a PCI device
814 * @dev: PCI device to handle.
815 * @state: State to put the device into.
816 */
817static void __pci_start_power_transition(struct pci_dev *dev, pci_power_t state)
818{
Huang Ying448bd852012-06-23 10:23:51 +0800819 if (state == PCI_D0) {
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100820 pci_platform_power_transition(dev, PCI_D0);
Huang Ying448bd852012-06-23 10:23:51 +0800821 /*
822 * Mandatory power management transition delays, see
823 * PCI Express Base Specification Revision 2.0 Section
824 * 6.6.1: Conventional Reset. Do not delay for
825 * devices powered on/off by corresponding bridge,
826 * because have already delayed for the bridge.
827 */
828 if (dev->runtime_d3cold) {
Adrian Hunter50b2b542017-03-14 15:21:58 +0200829 if (dev->d3cold_delay)
830 msleep(dev->d3cold_delay);
Huang Ying448bd852012-06-23 10:23:51 +0800831 /*
832 * When powering on a bridge from D3cold, the
833 * whole hierarchy may be powered on into
834 * D0uninitialized state, resume them to give
835 * them a chance to suspend again
836 */
837 pci_wakeup_bus(dev->subordinate);
838 }
839 }
840}
841
842/**
843 * __pci_dev_set_current_state - Set current state of a PCI device
844 * @dev: Device to handle
845 * @data: pointer to state to be set
846 */
847static int __pci_dev_set_current_state(struct pci_dev *dev, void *data)
848{
849 pci_power_t state = *(pci_power_t *)data;
850
851 dev->current_state = state;
852 return 0;
853}
854
855/**
856 * __pci_bus_set_current_state - Walk given bus and set current state of devices
857 * @bus: Top bus of the subtree to walk.
858 * @state: state to be set
859 */
860static void __pci_bus_set_current_state(struct pci_bus *bus, pci_power_t state)
861{
862 if (bus)
863 pci_walk_bus(bus, __pci_dev_set_current_state, &state);
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100864}
865
866/**
867 * __pci_complete_power_transition - Complete power transition of a PCI device
868 * @dev: PCI device to handle.
869 * @state: State to put the device into.
870 *
871 * This function should not be called directly by device drivers.
872 */
873int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
874{
Huang Ying448bd852012-06-23 10:23:51 +0800875 int ret;
876
Rafael J. Wysockidb288c92012-07-05 15:20:00 -0600877 if (state <= PCI_D0)
Huang Ying448bd852012-06-23 10:23:51 +0800878 return -EINVAL;
879 ret = pci_platform_power_transition(dev, state);
880 /* Power off the bridge may power off the whole hierarchy */
881 if (!ret && state == PCI_D3cold)
882 __pci_bus_set_current_state(dev->subordinate, PCI_D3cold);
883 return ret;
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100884}
885EXPORT_SYMBOL_GPL(__pci_complete_power_transition);
886
887/**
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200888 * pci_set_power_state - Set the power state of a PCI device
889 * @dev: PCI device to handle.
890 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
891 *
Nick Andrew877d0312009-01-26 11:06:57 +0100892 * Transition a device to a new power state, using the platform firmware and/or
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200893 * the device's PCI PM registers.
894 *
895 * RETURN VALUE:
896 * -EINVAL if the requested state is invalid.
897 * -EIO if device does not support PCI PM or its PM capabilities register has a
898 * wrong version, or device doesn't support the requested state.
Piotr Gregorab4b8a42017-08-02 20:42:18 +0100899 * 0 if the transition is to D1 or D2 but D1 and D2 are not supported.
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200900 * 0 if device already is in the requested state.
Piotr Gregorab4b8a42017-08-02 20:42:18 +0100901 * 0 if the transition is to D3 but D3 is not supported.
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200902 * 0 if device's power state has been successfully changed.
903 */
904int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
905{
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200906 int error;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200907
908 /* bound the state we're entering */
Huang Ying448bd852012-06-23 10:23:51 +0800909 if (state > PCI_D3cold)
910 state = PCI_D3cold;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200911 else if (state < PCI_D0)
912 state = PCI_D0;
913 else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
914 /*
915 * If the device or the parent bridge do not support PCI PM,
916 * ignore the request if we're doing anything other than putting
917 * it into D0 (which would only happen on boot).
918 */
919 return 0;
920
Rafael J. Wysockidb288c92012-07-05 15:20:00 -0600921 /* Check if we're already there */
922 if (dev->current_state == state)
923 return 0;
924
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100925 __pci_start_power_transition(dev, state);
926
Alan Cox979b1792008-07-24 17:18:38 +0100927 /* This device is quirked not to be put into D3, so
928 don't put it in D3 */
Huang Ying448bd852012-06-23 10:23:51 +0800929 if (state >= PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
Alan Cox979b1792008-07-24 17:18:38 +0100930 return 0;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200931
Huang Ying448bd852012-06-23 10:23:51 +0800932 /*
933 * To put device in D3cold, we put device into D3hot in native
934 * way, then put device into D3cold with platform ops
935 */
936 error = pci_raw_set_power_state(dev, state > PCI_D3hot ?
937 PCI_D3hot : state);
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200938
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100939 if (!__pci_complete_power_transition(dev, state))
940 error = 0;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200941
942 return error;
943}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600944EXPORT_SYMBOL(pci_set_power_state);
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200945
946/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 * pci_choose_state - Choose the power state of a PCI device
948 * @dev: PCI device to be suspended
949 * @state: target sleep state for the whole system. This is the value
950 * that is passed to suspend() function.
951 *
952 * Returns PCI power state suitable for given device and given system
953 * message.
954 */
955
956pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
957{
Shaohua Liab826ca2007-07-20 10:03:22 +0800958 pci_power_t ret;
David Shaohua Li0f644742005-03-19 00:15:48 -0500959
Yijing Wang728cdb72013-06-18 16:22:14 +0800960 if (!dev->pm_cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return PCI_D0;
962
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200963 ret = platform_pci_choose_state(dev);
964 if (ret != PCI_POWER_ERROR)
965 return ret;
Pavel Machekca078ba2005-09-03 15:56:57 -0700966
967 switch (state.event) {
968 case PM_EVENT_ON:
969 return PCI_D0;
970 case PM_EVENT_FREEZE:
David Brownellb887d2e2006-08-14 23:11:05 -0700971 case PM_EVENT_PRETHAW:
972 /* REVISIT both freeze and pre-thaw "should" use D0 */
Pavel Machekca078ba2005-09-03 15:56:57 -0700973 case PM_EVENT_SUSPEND:
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100974 case PM_EVENT_HIBERNATE:
Pavel Machekca078ba2005-09-03 15:56:57 -0700975 return PCI_D3hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 default:
Frederick Lawler7506dc72018-01-18 12:55:24 -0600977 pci_info(dev, "unrecognized suspend event %d\n",
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600978 state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 BUG();
980 }
981 return PCI_D0;
982}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983EXPORT_SYMBOL(pci_choose_state);
984
Yu Zhao89858512009-02-16 02:55:47 +0800985#define PCI_EXP_SAVE_REGS 7
986
Alex Williamsonfd0f7f72013-12-17 16:43:45 -0700987static struct pci_cap_saved_state *_pci_find_saved_cap(struct pci_dev *pci_dev,
988 u16 cap, bool extended)
Yinghai Lu34a48762012-02-11 00:18:41 -0800989{
990 struct pci_cap_saved_state *tmp;
Yinghai Lu34a48762012-02-11 00:18:41 -0800991
Sasha Levinb67bfe02013-02-27 17:06:00 -0800992 hlist_for_each_entry(tmp, &pci_dev->saved_cap_space, next) {
Alex Williamsonfd0f7f72013-12-17 16:43:45 -0700993 if (tmp->cap.cap_extended == extended && tmp->cap.cap_nr == cap)
Yinghai Lu34a48762012-02-11 00:18:41 -0800994 return tmp;
995 }
996 return NULL;
997}
998
Alex Williamsonfd0f7f72013-12-17 16:43:45 -0700999struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap)
1000{
1001 return _pci_find_saved_cap(dev, cap, false);
1002}
1003
1004struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap)
1005{
1006 return _pci_find_saved_cap(dev, cap, true);
1007}
1008
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001009static int pci_save_pcie_state(struct pci_dev *dev)
1010{
Jiang Liu59875ae2012-07-24 17:20:06 +08001011 int i = 0;
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001012 struct pci_cap_saved_state *save_state;
1013 u16 *cap;
1014
Jiang Liu59875ae2012-07-24 17:20:06 +08001015 if (!pci_is_pcie(dev))
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001016 return 0;
1017
Eric W. Biederman9f355752007-03-08 13:06:13 -07001018 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001019 if (!save_state) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001020 pci_err(dev, "buffer not found in %s\n", __func__);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001021 return -ENOMEM;
1022 }
Jiang Liu59875ae2012-07-24 17:20:06 +08001023
Alex Williamson24a4742f2011-05-10 10:02:11 -06001024 cap = (u16 *)&save_state->cap.data[0];
Jiang Liu59875ae2012-07-24 17:20:06 +08001025 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &cap[i++]);
1026 pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]);
1027 pcie_capability_read_word(dev, PCI_EXP_SLTCTL, &cap[i++]);
1028 pcie_capability_read_word(dev, PCI_EXP_RTCTL, &cap[i++]);
1029 pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &cap[i++]);
1030 pcie_capability_read_word(dev, PCI_EXP_LNKCTL2, &cap[i++]);
1031 pcie_capability_read_word(dev, PCI_EXP_SLTCTL2, &cap[i++]);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001032
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001033 return 0;
1034}
1035
1036static void pci_restore_pcie_state(struct pci_dev *dev)
1037{
Jiang Liu59875ae2012-07-24 17:20:06 +08001038 int i = 0;
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001039 struct pci_cap_saved_state *save_state;
1040 u16 *cap;
1041
1042 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
Jiang Liu59875ae2012-07-24 17:20:06 +08001043 if (!save_state)
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001044 return;
Jiang Liu59875ae2012-07-24 17:20:06 +08001045
Alex Williamson24a4742f2011-05-10 10:02:11 -06001046 cap = (u16 *)&save_state->cap.data[0];
Jiang Liu59875ae2012-07-24 17:20:06 +08001047 pcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]);
1048 pcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]);
1049 pcie_capability_write_word(dev, PCI_EXP_SLTCTL, cap[i++]);
1050 pcie_capability_write_word(dev, PCI_EXP_RTCTL, cap[i++]);
1051 pcie_capability_write_word(dev, PCI_EXP_DEVCTL2, cap[i++]);
1052 pcie_capability_write_word(dev, PCI_EXP_LNKCTL2, cap[i++]);
1053 pcie_capability_write_word(dev, PCI_EXP_SLTCTL2, cap[i++]);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001054}
1055
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001056
1057static int pci_save_pcix_state(struct pci_dev *dev)
1058{
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01001059 int pos;
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001060 struct pci_cap_saved_state *save_state;
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001061
1062 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
Wei Yang0a1a9b42015-06-30 09:16:44 +08001063 if (!pos)
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001064 return 0;
1065
Shaohua Lif34303d2007-12-18 09:56:47 +08001066 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001067 if (!save_state) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001068 pci_err(dev, "buffer not found in %s\n", __func__);
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001069 return -ENOMEM;
1070 }
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001071
Alex Williamson24a4742f2011-05-10 10:02:11 -06001072 pci_read_config_word(dev, pos + PCI_X_CMD,
1073 (u16 *)save_state->cap.data);
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01001074
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001075 return 0;
1076}
1077
1078static void pci_restore_pcix_state(struct pci_dev *dev)
1079{
1080 int i = 0, pos;
1081 struct pci_cap_saved_state *save_state;
1082 u16 *cap;
1083
1084 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
1085 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
Wei Yang0a1a9b42015-06-30 09:16:44 +08001086 if (!save_state || !pos)
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001087 return;
Alex Williamson24a4742f2011-05-10 10:02:11 -06001088 cap = (u16 *)&save_state->cap.data[0];
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001089
1090 pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001091}
1092
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094/**
1095 * pci_save_state - save the PCI configuration space of a device before suspending
1096 * @dev: - PCI device that we're dealing with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001098int pci_save_state(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 int i;
1101 /* XXX: 100% dword access ok here? */
1102 for (i = 0; i < 16; i++)
Kleber Sacilotto de Souza9e0b5b22009-11-25 00:55:51 -02001103 pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]);
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +01001104 dev->state_saved = true;
Quentin Lambert79e50e72014-09-07 20:03:32 +02001105
1106 i = pci_save_pcie_state(dev);
1107 if (i != 0)
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001108 return i;
Quentin Lambert79e50e72014-09-07 20:03:32 +02001109
1110 i = pci_save_pcix_state(dev);
1111 if (i != 0)
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001112 return i;
Quentin Lambert79e50e72014-09-07 20:03:32 +02001113
Quentin Lambert754834b2014-11-06 17:45:55 +01001114 return pci_save_vc_state(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001116EXPORT_SYMBOL(pci_save_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Rafael J. Wysockiebfc5b82012-04-15 21:40:40 +02001118static void pci_restore_config_dword(struct pci_dev *pdev, int offset,
1119 u32 saved_val, int retry)
1120{
1121 u32 val;
1122
1123 pci_read_config_dword(pdev, offset, &val);
1124 if (val == saved_val)
1125 return;
1126
1127 for (;;) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001128 pci_dbg(pdev, "restoring config space at offset %#x (was %#x, writing %#x)\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -04001129 offset, val, saved_val);
Rafael J. Wysockiebfc5b82012-04-15 21:40:40 +02001130 pci_write_config_dword(pdev, offset, saved_val);
1131 if (retry-- <= 0)
1132 return;
1133
1134 pci_read_config_dword(pdev, offset, &val);
1135 if (val == saved_val)
1136 return;
1137
1138 mdelay(1);
1139 }
1140}
1141
Rafael J. Wysockia6cb9ee2012-04-16 23:07:50 +02001142static void pci_restore_config_space_range(struct pci_dev *pdev,
1143 int start, int end, int retry)
Rafael J. Wysockiebfc5b82012-04-15 21:40:40 +02001144{
1145 int index;
1146
1147 for (index = end; index >= start; index--)
1148 pci_restore_config_dword(pdev, 4 * index,
1149 pdev->saved_config_space[index],
1150 retry);
1151}
1152
Rafael J. Wysockia6cb9ee2012-04-16 23:07:50 +02001153static void pci_restore_config_space(struct pci_dev *pdev)
1154{
1155 if (pdev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
1156 pci_restore_config_space_range(pdev, 10, 15, 0);
1157 /* Restore BARs before the command register. */
1158 pci_restore_config_space_range(pdev, 4, 9, 10);
1159 pci_restore_config_space_range(pdev, 0, 3, 0);
1160 } else {
1161 pci_restore_config_space_range(pdev, 0, 15, 0);
1162 }
1163}
1164
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001165/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 * pci_restore_state - Restore the saved state of a PCI device
1167 * @dev: - PCI device that we're dealing with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 */
Jon Mason1d3c16a2010-11-30 17:43:26 -06001169void pci_restore_state(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Alek Duc82f63e2009-08-08 08:46:19 +08001171 if (!dev->state_saved)
Jon Mason1d3c16a2010-11-30 17:43:26 -06001172 return;
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +02001173
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001174 /* PCI Express register must be restored first */
1175 pci_restore_pcie_state(dev);
CQ Tang4ebeb1e2017-05-30 09:25:49 -07001176 pci_restore_pasid_state(dev);
1177 pci_restore_pri_state(dev);
Hao, Xudong1900ca12011-12-17 21:24:40 +08001178 pci_restore_ats_state(dev);
Alex Williamson425c1b22013-12-17 16:43:51 -07001179 pci_restore_vc_state(dev);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001180
Taku Izumib07461a2015-09-17 10:09:37 -05001181 pci_cleanup_aer_error_status_regs(dev);
1182
Rafael J. Wysockia6cb9ee2012-04-16 23:07:50 +02001183 pci_restore_config_space(dev);
Rafael J. Wysockiebfc5b82012-04-15 21:40:40 +02001184
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001185 pci_restore_pcix_state(dev);
Shaohua Li41017f02006-02-08 17:11:38 +08001186 pci_restore_msi_state(dev);
Alexander Duyckccbc1752015-07-07 12:24:35 -07001187
1188 /* Restore ACS and IOV configuration state */
1189 pci_enable_acs(dev);
Yu Zhao8c5cdb62009-03-20 11:25:12 +08001190 pci_restore_iov_state(dev);
Michael Ellerman8fed4b62007-01-25 19:34:08 +11001191
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +02001192 dev->state_saved = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001194EXPORT_SYMBOL(pci_restore_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001196struct pci_saved_state {
1197 u32 config_space[16];
1198 struct pci_cap_saved_data cap[0];
1199};
1200
1201/**
1202 * pci_store_saved_state - Allocate and return an opaque struct containing
1203 * the device saved state.
1204 * @dev: PCI device that we're dealing with
1205 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001206 * Return NULL if no state or error.
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001207 */
1208struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev)
1209{
1210 struct pci_saved_state *state;
1211 struct pci_cap_saved_state *tmp;
1212 struct pci_cap_saved_data *cap;
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001213 size_t size;
1214
1215 if (!dev->state_saved)
1216 return NULL;
1217
1218 size = sizeof(*state) + sizeof(struct pci_cap_saved_data);
1219
Sasha Levinb67bfe02013-02-27 17:06:00 -08001220 hlist_for_each_entry(tmp, &dev->saved_cap_space, next)
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001221 size += sizeof(struct pci_cap_saved_data) + tmp->cap.size;
1222
1223 state = kzalloc(size, GFP_KERNEL);
1224 if (!state)
1225 return NULL;
1226
1227 memcpy(state->config_space, dev->saved_config_space,
1228 sizeof(state->config_space));
1229
1230 cap = state->cap;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001231 hlist_for_each_entry(tmp, &dev->saved_cap_space, next) {
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001232 size_t len = sizeof(struct pci_cap_saved_data) + tmp->cap.size;
1233 memcpy(cap, &tmp->cap, len);
1234 cap = (struct pci_cap_saved_data *)((u8 *)cap + len);
1235 }
1236 /* Empty cap_save terminates list */
1237
1238 return state;
1239}
1240EXPORT_SYMBOL_GPL(pci_store_saved_state);
1241
1242/**
1243 * pci_load_saved_state - Reload the provided save state into struct pci_dev.
1244 * @dev: PCI device that we're dealing with
1245 * @state: Saved state returned from pci_store_saved_state()
1246 */
Konrad Rzeszutek Wilk98d9b272014-12-03 16:40:31 -05001247int pci_load_saved_state(struct pci_dev *dev,
1248 struct pci_saved_state *state)
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001249{
1250 struct pci_cap_saved_data *cap;
1251
1252 dev->state_saved = false;
1253
1254 if (!state)
1255 return 0;
1256
1257 memcpy(dev->saved_config_space, state->config_space,
1258 sizeof(state->config_space));
1259
1260 cap = state->cap;
1261 while (cap->size) {
1262 struct pci_cap_saved_state *tmp;
1263
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07001264 tmp = _pci_find_saved_cap(dev, cap->cap_nr, cap->cap_extended);
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001265 if (!tmp || tmp->cap.size != cap->size)
1266 return -EINVAL;
1267
1268 memcpy(tmp->cap.data, cap->data, tmp->cap.size);
1269 cap = (struct pci_cap_saved_data *)((u8 *)cap +
1270 sizeof(struct pci_cap_saved_data) + cap->size);
1271 }
1272
1273 dev->state_saved = true;
1274 return 0;
1275}
Konrad Rzeszutek Wilk98d9b272014-12-03 16:40:31 -05001276EXPORT_SYMBOL_GPL(pci_load_saved_state);
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001277
1278/**
1279 * pci_load_and_free_saved_state - Reload the save state pointed to by state,
1280 * and free the memory allocated for it.
1281 * @dev: PCI device that we're dealing with
1282 * @state: Pointer to saved state returned from pci_store_saved_state()
1283 */
1284int pci_load_and_free_saved_state(struct pci_dev *dev,
1285 struct pci_saved_state **state)
1286{
1287 int ret = pci_load_saved_state(dev, *state);
1288 kfree(*state);
1289 *state = NULL;
1290 return ret;
1291}
1292EXPORT_SYMBOL_GPL(pci_load_and_free_saved_state);
1293
Bjorn Helgaas8a9d5602014-02-26 11:26:00 -07001294int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
1295{
1296 return pci_enable_resources(dev, bars);
1297}
1298
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001299static int do_pci_enable_device(struct pci_dev *dev, int bars)
1300{
1301 int err;
Vidya Sagar1f6ae472014-07-16 15:33:42 +05301302 struct pci_dev *bridge;
Bjorn Helgaas1e2571a2014-01-29 16:13:51 -07001303 u16 cmd;
1304 u8 pin;
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001305
1306 err = pci_set_power_state(dev, PCI_D0);
1307 if (err < 0 && err != -EIO)
1308 return err;
Vidya Sagar1f6ae472014-07-16 15:33:42 +05301309
1310 bridge = pci_upstream_bridge(dev);
1311 if (bridge)
1312 pcie_aspm_powersave_config_link(bridge);
1313
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001314 err = pcibios_enable_device(dev, bars);
1315 if (err < 0)
1316 return err;
1317 pci_fixup_device(pci_fixup_enable, dev);
1318
Bjorn Helgaas866d5412014-03-07 16:06:05 -07001319 if (dev->msi_enabled || dev->msix_enabled)
1320 return 0;
1321
Bjorn Helgaas1e2571a2014-01-29 16:13:51 -07001322 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
1323 if (pin) {
1324 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1325 if (cmd & PCI_COMMAND_INTX_DISABLE)
1326 pci_write_config_word(dev, PCI_COMMAND,
1327 cmd & ~PCI_COMMAND_INTX_DISABLE);
1328 }
1329
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001330 return 0;
1331}
1332
1333/**
Tejun Heo0b62e132007-07-27 14:43:35 +09001334 * pci_reenable_device - Resume abandoned device
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001335 * @dev: PCI device to be resumed
1336 *
1337 * Note this function is a backend of pci_default_resume and is not supposed
1338 * to be called by normal code, write proper resume handler and use it instead.
1339 */
Tejun Heo0b62e132007-07-27 14:43:35 +09001340int pci_reenable_device(struct pci_dev *dev)
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001341{
Yuji Shimada296ccb02009-04-03 16:41:46 +09001342 if (pci_is_enabled(dev))
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001343 return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
1344 return 0;
1345}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001346EXPORT_SYMBOL(pci_reenable_device);
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001347
Yinghai Lu928bea92013-07-22 14:37:17 -07001348static void pci_enable_bridge(struct pci_dev *dev)
1349{
Bjorn Helgaas79272132013-11-06 10:00:51 -07001350 struct pci_dev *bridge;
Yinghai Lu928bea92013-07-22 14:37:17 -07001351 int retval;
1352
Bjorn Helgaas79272132013-11-06 10:00:51 -07001353 bridge = pci_upstream_bridge(dev);
1354 if (bridge)
1355 pci_enable_bridge(bridge);
Yinghai Lu928bea92013-07-22 14:37:17 -07001356
Yinghai Lucf3e1fe2013-11-05 13:34:38 -07001357 if (pci_is_enabled(dev)) {
Bjorn Helgaasfbeeb822013-11-05 13:34:51 -07001358 if (!dev->is_busmaster)
Yinghai Lucf3e1fe2013-11-05 13:34:38 -07001359 pci_set_master(dev);
Bjorn Helgaas0f50a492017-09-15 01:33:51 -05001360 return;
Yinghai Lucf3e1fe2013-11-05 13:34:38 -07001361 }
1362
Yinghai Lu928bea92013-07-22 14:37:17 -07001363 retval = pci_enable_device(dev);
1364 if (retval)
Frederick Lawler7506dc72018-01-18 12:55:24 -06001365 pci_err(dev, "Error enabling bridge (%d), continuing\n",
Yinghai Lu928bea92013-07-22 14:37:17 -07001366 retval);
1367 pci_set_master(dev);
1368}
1369
Bjorn Helgaasb4b4fbb2013-01-04 12:12:55 -07001370static int pci_enable_device_flags(struct pci_dev *dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Bjorn Helgaas79272132013-11-06 10:00:51 -07001372 struct pci_dev *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 int err;
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001374 int i, bars = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Jesse Barnes97c145f2010-11-05 15:16:36 -04001376 /*
1377 * Power state could be unknown at this point, either due to a fresh
1378 * boot or a device removal call. So get the current power state
1379 * so that things like MSI message writing will behave as expected
1380 * (e.g. if the device really is in D0 at enable time).
1381 */
1382 if (dev->pm_cap) {
1383 u16 pmcsr;
1384 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
1385 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
1386 }
1387
Bjorn Helgaascc7ba392013-02-11 16:47:01 -07001388 if (atomic_inc_return(&dev->enable_cnt) > 1)
Hidetoshi Seto9fb625c2006-12-18 10:28:43 +09001389 return 0; /* already enabled */
1390
Bjorn Helgaas79272132013-11-06 10:00:51 -07001391 bridge = pci_upstream_bridge(dev);
Bjorn Helgaas0f50a492017-09-15 01:33:51 -05001392 if (bridge)
Bjorn Helgaas79272132013-11-06 10:00:51 -07001393 pci_enable_bridge(bridge);
Yinghai Lu928bea92013-07-22 14:37:17 -07001394
Yinghai Lu497f16f2011-12-17 18:33:37 -08001395 /* only skip sriov related */
1396 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
1397 if (dev->resource[i].flags & flags)
1398 bars |= (1 << i);
1399 for (i = PCI_BRIDGE_RESOURCES; i < DEVICE_COUNT_RESOURCE; i++)
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001400 if (dev->resource[i].flags & flags)
1401 bars |= (1 << i);
1402
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001403 err = do_pci_enable_device(dev, bars);
Greg Kroah-Hartman95a62962005-07-28 11:37:33 -07001404 if (err < 0)
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001405 atomic_dec(&dev->enable_cnt);
Hidetoshi Seto9fb625c2006-12-18 10:28:43 +09001406 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
1408
1409/**
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001410 * pci_enable_device_io - Initialize a device for use with IO space
1411 * @dev: PCI device to be initialized
1412 *
1413 * Initialize device before it's used by a driver. Ask low-level code
1414 * to enable I/O resources. Wake up the device if it was suspended.
1415 * Beware, this function can fail.
1416 */
1417int pci_enable_device_io(struct pci_dev *dev)
1418{
Bjorn Helgaasb4b4fbb2013-01-04 12:12:55 -07001419 return pci_enable_device_flags(dev, IORESOURCE_IO);
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001420}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001421EXPORT_SYMBOL(pci_enable_device_io);
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001422
1423/**
1424 * pci_enable_device_mem - Initialize a device for use with Memory space
1425 * @dev: PCI device to be initialized
1426 *
1427 * Initialize device before it's used by a driver. Ask low-level code
1428 * to enable Memory resources. Wake up the device if it was suspended.
1429 * Beware, this function can fail.
1430 */
1431int pci_enable_device_mem(struct pci_dev *dev)
1432{
Bjorn Helgaasb4b4fbb2013-01-04 12:12:55 -07001433 return pci_enable_device_flags(dev, IORESOURCE_MEM);
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001434}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001435EXPORT_SYMBOL(pci_enable_device_mem);
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/**
1438 * pci_enable_device - Initialize device before it's used by a driver.
1439 * @dev: PCI device to be initialized
1440 *
1441 * Initialize device before it's used by a driver. Ask low-level code
1442 * to enable I/O and memory. Wake up the device if it was suspended.
1443 * Beware, this function can fail.
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001444 *
1445 * Note we don't actually enable the device many times if we call
1446 * this function repeatedly (we just increment the count).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001448int pci_enable_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Bjorn Helgaasb4b4fbb2013-01-04 12:12:55 -07001450 return pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001452EXPORT_SYMBOL(pci_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Tejun Heo9ac78492007-01-20 16:00:26 +09001454/*
1455 * Managed PCI resources. This manages device on/off, intx/msi/msix
1456 * on/off and BAR regions. pci_dev itself records msi/msix status, so
1457 * there's no need to track it separately. pci_devres is initialized
1458 * when a device is enabled using managed PCI device enable interface.
1459 */
1460struct pci_devres {
Tejun Heo7f375f32007-02-25 04:36:01 -08001461 unsigned int enabled:1;
1462 unsigned int pinned:1;
Tejun Heo9ac78492007-01-20 16:00:26 +09001463 unsigned int orig_intx:1;
1464 unsigned int restore_intx:1;
Heiner Kallweitfc0f9f42017-12-12 07:40:56 +01001465 unsigned int mwi:1;
Tejun Heo9ac78492007-01-20 16:00:26 +09001466 u32 region_mask;
1467};
1468
1469static void pcim_release(struct device *gendev, void *res)
1470{
Geliang Tangf3d2f1652016-01-08 12:05:39 -06001471 struct pci_dev *dev = to_pci_dev(gendev);
Tejun Heo9ac78492007-01-20 16:00:26 +09001472 struct pci_devres *this = res;
1473 int i;
1474
1475 if (dev->msi_enabled)
1476 pci_disable_msi(dev);
1477 if (dev->msix_enabled)
1478 pci_disable_msix(dev);
1479
1480 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
1481 if (this->region_mask & (1 << i))
1482 pci_release_region(dev, i);
1483
Heiner Kallweitfc0f9f42017-12-12 07:40:56 +01001484 if (this->mwi)
1485 pci_clear_mwi(dev);
1486
Tejun Heo9ac78492007-01-20 16:00:26 +09001487 if (this->restore_intx)
1488 pci_intx(dev, this->orig_intx);
1489
Tejun Heo7f375f32007-02-25 04:36:01 -08001490 if (this->enabled && !this->pinned)
Tejun Heo9ac78492007-01-20 16:00:26 +09001491 pci_disable_device(dev);
1492}
1493
Ryan Desfosses07656d83082014-04-11 01:01:53 -04001494static struct pci_devres *get_pci_dr(struct pci_dev *pdev)
Tejun Heo9ac78492007-01-20 16:00:26 +09001495{
1496 struct pci_devres *dr, *new_dr;
1497
1498 dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
1499 if (dr)
1500 return dr;
1501
1502 new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
1503 if (!new_dr)
1504 return NULL;
1505 return devres_get(&pdev->dev, new_dr, NULL, NULL);
1506}
1507
Ryan Desfosses07656d83082014-04-11 01:01:53 -04001508static struct pci_devres *find_pci_dr(struct pci_dev *pdev)
Tejun Heo9ac78492007-01-20 16:00:26 +09001509{
1510 if (pci_is_managed(pdev))
1511 return devres_find(&pdev->dev, pcim_release, NULL, NULL);
1512 return NULL;
1513}
1514
1515/**
1516 * pcim_enable_device - Managed pci_enable_device()
1517 * @pdev: PCI device to be initialized
1518 *
1519 * Managed pci_enable_device().
1520 */
1521int pcim_enable_device(struct pci_dev *pdev)
1522{
1523 struct pci_devres *dr;
1524 int rc;
1525
1526 dr = get_pci_dr(pdev);
1527 if (unlikely(!dr))
1528 return -ENOMEM;
Tejun Heob95d58e2008-01-30 18:20:04 +09001529 if (dr->enabled)
1530 return 0;
Tejun Heo9ac78492007-01-20 16:00:26 +09001531
1532 rc = pci_enable_device(pdev);
1533 if (!rc) {
1534 pdev->is_managed = 1;
Tejun Heo7f375f32007-02-25 04:36:01 -08001535 dr->enabled = 1;
Tejun Heo9ac78492007-01-20 16:00:26 +09001536 }
1537 return rc;
1538}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001539EXPORT_SYMBOL(pcim_enable_device);
Tejun Heo9ac78492007-01-20 16:00:26 +09001540
1541/**
1542 * pcim_pin_device - Pin managed PCI device
1543 * @pdev: PCI device to pin
1544 *
1545 * Pin managed PCI device @pdev. Pinned device won't be disabled on
1546 * driver detach. @pdev must have been enabled with
1547 * pcim_enable_device().
1548 */
1549void pcim_pin_device(struct pci_dev *pdev)
1550{
1551 struct pci_devres *dr;
1552
1553 dr = find_pci_dr(pdev);
Tejun Heo7f375f32007-02-25 04:36:01 -08001554 WARN_ON(!dr || !dr->enabled);
Tejun Heo9ac78492007-01-20 16:00:26 +09001555 if (dr)
Tejun Heo7f375f32007-02-25 04:36:01 -08001556 dr->pinned = 1;
Tejun Heo9ac78492007-01-20 16:00:26 +09001557}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001558EXPORT_SYMBOL(pcim_pin_device);
Tejun Heo9ac78492007-01-20 16:00:26 +09001559
Matthew Garretteca0d4672012-12-05 14:33:27 -07001560/*
1561 * pcibios_add_device - provide arch specific hooks when adding device dev
1562 * @dev: the PCI device being added
1563 *
1564 * Permits the platform to provide architecture specific functionality when
1565 * devices are added. This is the default implementation. Architecture
1566 * implementations can override this.
1567 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001568int __weak pcibios_add_device(struct pci_dev *dev)
Matthew Garretteca0d4672012-12-05 14:33:27 -07001569{
1570 return 0;
1571}
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573/**
Sebastian Ott6ae32c52013-06-04 19:18:14 +02001574 * pcibios_release_device - provide arch specific hooks when releasing device dev
1575 * @dev: the PCI device being released
1576 *
1577 * Permits the platform to provide architecture specific functionality when
1578 * devices are released. This is the default implementation. Architecture
1579 * implementations can override this.
1580 */
1581void __weak pcibios_release_device(struct pci_dev *dev) {}
1582
1583/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 * pcibios_disable_device - disable arch specific PCI resources for device dev
1585 * @dev: the PCI device to disable
1586 *
1587 * Disables architecture specific PCI resources for the device. This
1588 * is the default implementation. Architecture implementations can
1589 * override this.
1590 */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001591void __weak pcibios_disable_device(struct pci_dev *dev) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Hanjun Guoa43ae582014-05-06 11:29:52 +08001593/**
1594 * pcibios_penalize_isa_irq - penalize an ISA IRQ
1595 * @irq: ISA IRQ to penalize
1596 * @active: IRQ active or not
1597 *
1598 * Permits the platform to provide architecture-specific functionality when
1599 * penalizing ISA IRQs. This is the default implementation. Architecture
1600 * implementations can override this.
1601 */
1602void __weak pcibios_penalize_isa_irq(int irq, int active) {}
1603
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001604static void do_pci_disable_device(struct pci_dev *dev)
1605{
1606 u16 pci_command;
1607
1608 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
1609 if (pci_command & PCI_COMMAND_MASTER) {
1610 pci_command &= ~PCI_COMMAND_MASTER;
1611 pci_write_config_word(dev, PCI_COMMAND, pci_command);
1612 }
1613
1614 pcibios_disable_device(dev);
1615}
1616
1617/**
1618 * pci_disable_enabled_device - Disable device without updating enable_cnt
1619 * @dev: PCI device to disable
1620 *
1621 * NOTE: This function is a backend of PCI power management routines and is
1622 * not supposed to be called drivers.
1623 */
1624void pci_disable_enabled_device(struct pci_dev *dev)
1625{
Yuji Shimada296ccb02009-04-03 16:41:46 +09001626 if (pci_is_enabled(dev))
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001627 do_pci_disable_device(dev);
1628}
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630/**
1631 * pci_disable_device - Disable PCI device after use
1632 * @dev: PCI device to be disabled
1633 *
1634 * Signal to the system that the PCI device is not in use by the system
1635 * anymore. This only involves disabling PCI bus-mastering, if active.
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001636 *
1637 * Note we don't actually disable the device until all callers of
Roman Fietzeee6583f2010-05-18 14:45:47 +02001638 * pci_enable_device() have called pci_disable_device().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001640void pci_disable_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
Tejun Heo9ac78492007-01-20 16:00:26 +09001642 struct pci_devres *dr;
Shaohua Li99dc8042006-05-26 10:58:27 +08001643
Tejun Heo9ac78492007-01-20 16:00:26 +09001644 dr = find_pci_dr(dev);
1645 if (dr)
Tejun Heo7f375f32007-02-25 04:36:01 -08001646 dr->enabled = 0;
Tejun Heo9ac78492007-01-20 16:00:26 +09001647
Konstantin Khlebnikovfd6dcea2013-02-04 15:56:01 +04001648 dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0,
1649 "disabling already-disabled device");
1650
Bjorn Helgaascc7ba392013-02-11 16:47:01 -07001651 if (atomic_dec_return(&dev->enable_cnt) != 0)
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001652 return;
1653
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001654 do_pci_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001656 dev->is_busmaster = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001658EXPORT_SYMBOL(pci_disable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660/**
Brian Kingf7bdd122007-04-06 16:39:36 -05001661 * pcibios_set_pcie_reset_state - set reset state for device dev
Stefan Assmann45e829e2009-12-03 06:49:24 -05001662 * @dev: the PCIe device reset
Brian Kingf7bdd122007-04-06 16:39:36 -05001663 * @state: Reset state to enter into
1664 *
1665 *
Stefan Assmann45e829e2009-12-03 06:49:24 -05001666 * Sets the PCIe reset state for the device. This is the default
Brian Kingf7bdd122007-04-06 16:39:36 -05001667 * implementation. Architecture implementations can override this.
1668 */
Bjorn Helgaasd6d88c82012-06-19 06:54:49 -06001669int __weak pcibios_set_pcie_reset_state(struct pci_dev *dev,
1670 enum pcie_reset_state state)
Brian Kingf7bdd122007-04-06 16:39:36 -05001671{
1672 return -EINVAL;
1673}
1674
1675/**
1676 * pci_set_pcie_reset_state - set reset state for device dev
Stefan Assmann45e829e2009-12-03 06:49:24 -05001677 * @dev: the PCIe device reset
Brian Kingf7bdd122007-04-06 16:39:36 -05001678 * @state: Reset state to enter into
1679 *
1680 *
1681 * Sets the PCI reset state for the device.
1682 */
1683int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
1684{
1685 return pcibios_set_pcie_reset_state(dev, state);
1686}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001687EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);
Brian Kingf7bdd122007-04-06 16:39:36 -05001688
1689/**
Rafael J. Wysocki58ff4632010-02-17 23:36:58 +01001690 * pci_check_pme_status - Check if given device has generated PME.
1691 * @dev: Device to check.
1692 *
1693 * Check the PME status of the device and if set, clear it and clear PME enable
1694 * (if set). Return 'true' if PME status and PME enable were both set or
1695 * 'false' otherwise.
1696 */
1697bool pci_check_pme_status(struct pci_dev *dev)
1698{
1699 int pmcsr_pos;
1700 u16 pmcsr;
1701 bool ret = false;
1702
1703 if (!dev->pm_cap)
1704 return false;
1705
1706 pmcsr_pos = dev->pm_cap + PCI_PM_CTRL;
1707 pci_read_config_word(dev, pmcsr_pos, &pmcsr);
1708 if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
1709 return false;
1710
1711 /* Clear PME status. */
1712 pmcsr |= PCI_PM_CTRL_PME_STATUS;
1713 if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
1714 /* Disable PME to avoid interrupt flood. */
1715 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
1716 ret = true;
1717 }
1718
1719 pci_write_config_word(dev, pmcsr_pos, pmcsr);
1720
1721 return ret;
1722}
1723
1724/**
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001725 * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set.
1726 * @dev: Device to handle.
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001727 * @pme_poll_reset: Whether or not to reset the device's pme_poll flag.
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001728 *
1729 * Check if @dev has generated PME and queue a resume request for it in that
1730 * case.
1731 */
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001732static int pci_pme_wakeup(struct pci_dev *dev, void *pme_poll_reset)
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001733{
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001734 if (pme_poll_reset && dev->pme_poll)
1735 dev->pme_poll = false;
1736
Rafael J. Wysockic125e962010-07-05 22:43:53 +02001737 if (pci_check_pme_status(dev)) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +02001738 pci_wakeup_event(dev);
Rafael J. Wysocki0f953bf2010-12-29 13:22:08 +01001739 pm_request_resume(&dev->dev);
Rafael J. Wysockic125e962010-07-05 22:43:53 +02001740 }
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001741 return 0;
1742}
1743
1744/**
1745 * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary.
1746 * @bus: Top bus of the subtree to walk.
1747 */
1748void pci_pme_wakeup_bus(struct pci_bus *bus)
1749{
1750 if (bus)
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001751 pci_walk_bus(bus, pci_pme_wakeup, (void *)true);
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001752}
1753
Huang Ying448bd852012-06-23 10:23:51 +08001754
1755/**
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001756 * pci_pme_capable - check the capability of PCI device to generate PME#
1757 * @dev: PCI device to handle.
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001758 * @state: PCI state from which device will issue PME#.
1759 */
Rafael J. Wysockie5899e12008-07-19 14:39:24 +02001760bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001761{
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001762 if (!dev->pm_cap)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001763 return false;
1764
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001765 return !!(dev->pme_support & (1 << state));
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001766}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001767EXPORT_SYMBOL(pci_pme_capable);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001768
Matthew Garrettdf17e622010-10-04 14:22:29 -04001769static void pci_pme_list_scan(struct work_struct *work)
1770{
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001771 struct pci_pme_device *pme_dev, *n;
Matthew Garrettdf17e622010-10-04 14:22:29 -04001772
1773 mutex_lock(&pci_pme_list_mutex);
Bjorn Helgaasce300002014-01-24 09:51:06 -07001774 list_for_each_entry_safe(pme_dev, n, &pci_pme_list, list) {
1775 if (pme_dev->dev->pme_poll) {
1776 struct pci_dev *bridge;
Zheng Yan71a83bd2012-06-23 10:23:49 +08001777
Bjorn Helgaasce300002014-01-24 09:51:06 -07001778 bridge = pme_dev->dev->bus->self;
1779 /*
1780 * If bridge is in low power state, the
1781 * configuration space of subordinate devices
1782 * may be not accessible
1783 */
1784 if (bridge && bridge->current_state != PCI_D0)
1785 continue;
1786 pci_pme_wakeup(pme_dev->dev, NULL);
1787 } else {
1788 list_del(&pme_dev->list);
1789 kfree(pme_dev);
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001790 }
Matthew Garrettdf17e622010-10-04 14:22:29 -04001791 }
Bjorn Helgaasce300002014-01-24 09:51:06 -07001792 if (!list_empty(&pci_pme_list))
Lukas Wunnerea003532017-04-18 20:44:30 +02001793 queue_delayed_work(system_freezable_wq, &pci_pme_work,
1794 msecs_to_jiffies(PME_TIMEOUT));
Matthew Garrettdf17e622010-10-04 14:22:29 -04001795 mutex_unlock(&pci_pme_list_mutex);
1796}
1797
Rafael J. Wysocki2cef5482015-09-30 01:10:24 +02001798static void __pci_pme_active(struct pci_dev *dev, bool enable)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001799{
1800 u16 pmcsr;
1801
Rafael J. Wysockiffaddbe2013-04-10 10:32:51 +00001802 if (!dev->pme_support)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001803 return;
1804
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001805 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001806 /* Clear PME_Status by writing 1 to it and enable PME# */
1807 pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
1808 if (!enable)
1809 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
1810
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001811 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
Rafael J. Wysocki2cef5482015-09-30 01:10:24 +02001812}
1813
Rafael J. Wysocki0ce3fca2017-07-12 03:05:39 +02001814/**
1815 * pci_pme_restore - Restore PME configuration after config space restore.
1816 * @dev: PCI device to update.
1817 */
1818void pci_pme_restore(struct pci_dev *dev)
Rafael J. Wysockidc15e712017-06-12 22:53:36 +02001819{
1820 u16 pmcsr;
1821
1822 if (!dev->pme_support)
1823 return;
1824
1825 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
1826 if (dev->wakeup_prepared) {
1827 pmcsr |= PCI_PM_CTRL_PME_ENABLE;
Rafael J. Wysocki0ce3fca2017-07-12 03:05:39 +02001828 pmcsr &= ~PCI_PM_CTRL_PME_STATUS;
Rafael J. Wysockidc15e712017-06-12 22:53:36 +02001829 } else {
1830 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
1831 pmcsr |= PCI_PM_CTRL_PME_STATUS;
1832 }
1833 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
1834}
1835
Rafael J. Wysocki2cef5482015-09-30 01:10:24 +02001836/**
1837 * pci_pme_active - enable or disable PCI device's PME# function
1838 * @dev: PCI device to handle.
1839 * @enable: 'true' to enable PME# generation; 'false' to disable it.
1840 *
1841 * The caller must verify that the device is capable of generating PME# before
1842 * calling this function with @enable equal to 'true'.
1843 */
1844void pci_pme_active(struct pci_dev *dev, bool enable)
1845{
1846 __pci_pme_active(dev, enable);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001847
Huang Ying6e965e02012-10-26 13:07:51 +08001848 /*
1849 * PCI (as opposed to PCIe) PME requires that the device have
1850 * its PME# line hooked up correctly. Not all hardware vendors
1851 * do this, so the PME never gets delivered and the device
1852 * remains asleep. The easiest way around this is to
1853 * periodically walk the list of suspended devices and check
1854 * whether any have their PME flag set. The assumption is that
1855 * we'll wake up often enough anyway that this won't be a huge
1856 * hit, and the power savings from the devices will still be a
1857 * win.
1858 *
1859 * Although PCIe uses in-band PME message instead of PME# line
1860 * to report PME, PME does not work for some PCIe devices in
1861 * reality. For example, there are devices that set their PME
1862 * status bits, but don't really bother to send a PME message;
1863 * there are PCI Express Root Ports that don't bother to
1864 * trigger interrupts when they receive PME messages from the
1865 * devices below. So PME poll is used for PCIe devices too.
1866 */
Matthew Garrettdf17e622010-10-04 14:22:29 -04001867
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001868 if (dev->pme_poll) {
Matthew Garrettdf17e622010-10-04 14:22:29 -04001869 struct pci_pme_device *pme_dev;
1870 if (enable) {
1871 pme_dev = kmalloc(sizeof(struct pci_pme_device),
1872 GFP_KERNEL);
Bjorn Helgaas0394cb12013-10-16 12:32:53 -06001873 if (!pme_dev) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001874 pci_warn(dev, "can't enable PME#\n");
Bjorn Helgaas0394cb12013-10-16 12:32:53 -06001875 return;
1876 }
Matthew Garrettdf17e622010-10-04 14:22:29 -04001877 pme_dev->dev = dev;
1878 mutex_lock(&pci_pme_list_mutex);
1879 list_add(&pme_dev->list, &pci_pme_list);
1880 if (list_is_singular(&pci_pme_list))
Lukas Wunnerea003532017-04-18 20:44:30 +02001881 queue_delayed_work(system_freezable_wq,
1882 &pci_pme_work,
1883 msecs_to_jiffies(PME_TIMEOUT));
Matthew Garrettdf17e622010-10-04 14:22:29 -04001884 mutex_unlock(&pci_pme_list_mutex);
1885 } else {
1886 mutex_lock(&pci_pme_list_mutex);
1887 list_for_each_entry(pme_dev, &pci_pme_list, list) {
1888 if (pme_dev->dev == dev) {
1889 list_del(&pme_dev->list);
1890 kfree(pme_dev);
1891 break;
1892 }
1893 }
1894 mutex_unlock(&pci_pme_list_mutex);
1895 }
1896 }
1897
Frederick Lawler7506dc72018-01-18 12:55:24 -06001898 pci_dbg(dev, "PME# %s\n", enable ? "enabled" : "disabled");
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001899}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001900EXPORT_SYMBOL(pci_pme_active);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001901
1902/**
Rafael J. Wysocki08476842017-06-24 01:57:35 +02001903 * pci_enable_wake - enable PCI device as wakeup event source
David Brownell075c1772007-04-26 00:12:06 -07001904 * @dev: PCI device affected
1905 * @state: PCI state from which device will issue wakeup events
1906 * @enable: True to enable event generation; false to disable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 *
David Brownell075c1772007-04-26 00:12:06 -07001908 * This enables the device as a wakeup event source, or disables it.
1909 * When such events involves platform-specific hooks, those hooks are
1910 * called automatically by this routine.
1911 *
1912 * Devices with legacy power management (no standard PCI PM capabilities)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001913 * always require such platform hooks.
David Brownell075c1772007-04-26 00:12:06 -07001914 *
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001915 * RETURN VALUE:
1916 * 0 is returned on success
1917 * -EINVAL is returned if device is not supposed to wake up the system
1918 * Error code depending on the platform is returned if both the platform and
1919 * the native mechanism fail to enable the generation of wake-up events
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 */
Rafael J. Wysocki08476842017-06-24 01:57:35 +02001921int pci_enable_wake(struct pci_dev *dev, pci_power_t state, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001923 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Rafael J. Wysockibaecc472017-07-21 14:38:08 +02001925 /*
1926 * Bridges can only signal wakeup on behalf of subordinate devices,
1927 * but that is set up elsewhere, so skip them.
1928 */
1929 if (pci_has_subordinate(dev))
1930 return 0;
1931
Rafael J. Wysocki0ce3fca2017-07-12 03:05:39 +02001932 /* Don't do the same thing twice in a row for one device. */
1933 if (!!enable == !!dev->wakeup_prepared)
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02001934 return 0;
1935
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001936 /*
1937 * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
1938 * Anderson we should be doing PME# wake enable followed by ACPI wake
1939 * enable. To disable wake-up we call the platform first, for symmetry.
David Brownell075c1772007-04-26 00:12:06 -07001940 */
1941
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001942 if (enable) {
1943 int error;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001944
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001945 if (pci_pme_capable(dev, state))
1946 pci_pme_active(dev, true);
1947 else
1948 ret = 1;
Rafael J. Wysocki08476842017-06-24 01:57:35 +02001949 error = platform_pci_set_wakeup(dev, true);
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001950 if (ret)
1951 ret = error;
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02001952 if (!ret)
1953 dev->wakeup_prepared = true;
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001954 } else {
Rafael J. Wysocki08476842017-06-24 01:57:35 +02001955 platform_pci_set_wakeup(dev, false);
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001956 pci_pme_active(dev, false);
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02001957 dev->wakeup_prepared = false;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001958 }
1959
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001960 return ret;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001961}
Rafael J. Wysocki08476842017-06-24 01:57:35 +02001962EXPORT_SYMBOL(pci_enable_wake);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001963
1964/**
Rafael J. Wysocki0235c4f2008-08-18 21:38:00 +02001965 * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
1966 * @dev: PCI device to prepare
1967 * @enable: True to enable wake-up event generation; false to disable
1968 *
1969 * Many drivers want the device to wake up the system from D3_hot or D3_cold
1970 * and this function allows them to set that up cleanly - pci_enable_wake()
1971 * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
1972 * ordering constraints.
1973 *
1974 * This function only returns error code if the device is not capable of
1975 * generating PME# from both D3_hot and D3_cold, and the platform is unable to
1976 * enable wake-up power for it.
1977 */
1978int pci_wake_from_d3(struct pci_dev *dev, bool enable)
1979{
1980 return pci_pme_capable(dev, PCI_D3cold) ?
1981 pci_enable_wake(dev, PCI_D3cold, enable) :
1982 pci_enable_wake(dev, PCI_D3hot, enable);
1983}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001984EXPORT_SYMBOL(pci_wake_from_d3);
Rafael J. Wysocki0235c4f2008-08-18 21:38:00 +02001985
1986/**
Jesse Barnes37139072008-07-28 11:49:26 -07001987 * pci_target_state - find an appropriate low power state for a given PCI dev
1988 * @dev: PCI device
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02001989 * @wakeup: Whether or not wakeup functionality will be enabled for the device.
Jesse Barnes37139072008-07-28 11:49:26 -07001990 *
1991 * Use underlying platform code to find a supported low power state for @dev.
1992 * If the platform can't manage @dev, return the deepest state from which it
1993 * can generate wake events, based on any available PME info.
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001994 */
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02001995static pci_power_t pci_target_state(struct pci_dev *dev, bool wakeup)
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001996{
1997 pci_power_t target_state = PCI_D3hot;
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001998
1999 if (platform_pci_power_manageable(dev)) {
2000 /*
2001 * Call the platform to choose the target state of the device
2002 * and enable wake-up from this state if supported.
2003 */
2004 pci_power_t state = platform_pci_choose_state(dev);
2005
2006 switch (state) {
2007 case PCI_POWER_ERROR:
2008 case PCI_UNKNOWN:
2009 break;
2010 case PCI_D1:
2011 case PCI_D2:
2012 if (pci_no_d1d2(dev))
2013 break;
2014 default:
2015 target_state = state;
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002016 }
Lukas Wunner4132a572016-09-18 05:39:20 +02002017
2018 return target_state;
2019 }
2020
2021 if (!dev->pm_cap)
Rafael J. Wysockid2abdf62009-06-14 21:25:02 +02002022 target_state = PCI_D0;
Lukas Wunner4132a572016-09-18 05:39:20 +02002023
2024 /*
2025 * If the device is in D3cold even though it's not power-manageable by
2026 * the platform, it may have been powered down by non-standard means.
2027 * Best to let it slumber.
2028 */
2029 if (dev->current_state == PCI_D3cold)
2030 target_state = PCI_D3cold;
2031
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002032 if (wakeup) {
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002033 /*
2034 * Find the deepest state from which the device can generate
2035 * wake-up events, make it the target state and enable device
2036 * to generate PME#.
2037 */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002038 if (dev->pme_support) {
2039 while (target_state
2040 && !(dev->pme_support & (1 << target_state)))
2041 target_state--;
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002042 }
2043 }
2044
Rafael J. Wysockie5899e12008-07-19 14:39:24 +02002045 return target_state;
2046}
2047
2048/**
2049 * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
2050 * @dev: Device to handle.
2051 *
2052 * Choose the power state appropriate for the device depending on whether
2053 * it can wake up the system and/or is power manageable by the platform
2054 * (PCI_D3hot is the default) and put the device into that state.
2055 */
2056int pci_prepare_to_sleep(struct pci_dev *dev)
2057{
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002058 bool wakeup = device_may_wakeup(&dev->dev);
2059 pci_power_t target_state = pci_target_state(dev, wakeup);
Rafael J. Wysockie5899e12008-07-19 14:39:24 +02002060 int error;
2061
2062 if (target_state == PCI_POWER_ERROR)
2063 return -EIO;
2064
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002065 pci_enable_wake(dev, target_state, wakeup);
Rafael J. Wysockic157dfa2008-07-13 22:45:06 +02002066
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002067 error = pci_set_power_state(dev, target_state);
2068
2069 if (error)
2070 pci_enable_wake(dev, target_state, false);
2071
2072 return error;
2073}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002074EXPORT_SYMBOL(pci_prepare_to_sleep);
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002075
2076/**
Randy Dunlap443bd1c2008-07-21 09:27:18 -07002077 * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002078 * @dev: Device to handle.
2079 *
Thomas Weber88393162010-03-16 11:47:56 +01002080 * Disable device's system wake-up capability and put it into D0.
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002081 */
2082int pci_back_from_sleep(struct pci_dev *dev)
2083{
2084 pci_enable_wake(dev, PCI_D0, false);
2085 return pci_set_power_state(dev, PCI_D0);
2086}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002087EXPORT_SYMBOL(pci_back_from_sleep);
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02002088
2089/**
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01002090 * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend.
2091 * @dev: PCI device being suspended.
2092 *
2093 * Prepare @dev to generate wake-up events at run time and put it into a low
2094 * power state.
2095 */
2096int pci_finish_runtime_suspend(struct pci_dev *dev)
2097{
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002098 pci_power_t target_state;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01002099 int error;
2100
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002101 target_state = pci_target_state(dev, device_can_wakeup(&dev->dev));
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01002102 if (target_state == PCI_POWER_ERROR)
2103 return -EIO;
2104
Huang Ying448bd852012-06-23 10:23:51 +08002105 dev->runtime_d3cold = target_state == PCI_D3cold;
2106
Rafael J. Wysocki08476842017-06-24 01:57:35 +02002107 pci_enable_wake(dev, target_state, pci_dev_run_wake(dev));
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01002108
2109 error = pci_set_power_state(dev, target_state);
2110
Huang Ying448bd852012-06-23 10:23:51 +08002111 if (error) {
Rafael J. Wysocki08476842017-06-24 01:57:35 +02002112 pci_enable_wake(dev, target_state, false);
Huang Ying448bd852012-06-23 10:23:51 +08002113 dev->runtime_d3cold = false;
2114 }
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01002115
2116 return error;
2117}
2118
2119/**
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01002120 * pci_dev_run_wake - Check if device can generate run-time wake-up events.
2121 * @dev: Device to check.
2122 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002123 * Return true if the device itself is capable of generating wake-up events
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01002124 * (through the platform or using the native PCIe PME) or if the device supports
2125 * PME and one of its upstream bridges can generate wake-up events.
2126 */
2127bool pci_dev_run_wake(struct pci_dev *dev)
2128{
2129 struct pci_bus *bus = dev->bus;
2130
Rafael J. Wysockide3ef1e2017-06-24 01:58:53 +02002131 if (device_can_wakeup(&dev->dev))
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01002132 return true;
2133
2134 if (!dev->pme_support)
2135 return false;
2136
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002137 /* PME-capable in principle, but not from the target power state */
2138 if (!pci_pme_capable(dev, pci_target_state(dev, false)))
Alan Stern6496ebd2016-10-21 16:45:38 -04002139 return false;
2140
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01002141 while (bus->parent) {
2142 struct pci_dev *bridge = bus->self;
2143
Rafael J. Wysockide3ef1e2017-06-24 01:58:53 +02002144 if (device_can_wakeup(&bridge->dev))
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01002145 return true;
2146
2147 bus = bus->parent;
2148 }
2149
2150 /* We have reached the root bus. */
2151 if (bus->bridge)
Rafael J. Wysockide3ef1e2017-06-24 01:58:53 +02002152 return device_can_wakeup(bus->bridge);
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01002153
2154 return false;
2155}
2156EXPORT_SYMBOL_GPL(pci_dev_run_wake);
2157
Rafael J. Wysockibac2a902015-01-21 02:17:42 +01002158/**
2159 * pci_dev_keep_suspended - Check if the device can stay in the suspended state.
2160 * @pci_dev: Device to check.
2161 *
2162 * Return 'true' if the device is runtime-suspended, it doesn't have to be
2163 * reconfigured due to wakeup settings difference between system and runtime
2164 * suspend and the current power state of it is suitable for the upcoming
2165 * (system) transition.
Rafael J. Wysocki2cef5482015-09-30 01:10:24 +02002166 *
2167 * If the device is not configured for system wakeup, disable PME for it before
2168 * returning 'true' to prevent it from waking up the system unnecessarily.
Rafael J. Wysockibac2a902015-01-21 02:17:42 +01002169 */
2170bool pci_dev_keep_suspended(struct pci_dev *pci_dev)
2171{
2172 struct device *dev = &pci_dev->dev;
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002173 bool wakeup = device_may_wakeup(dev);
Rafael J. Wysockibac2a902015-01-21 02:17:42 +01002174
2175 if (!pm_runtime_suspended(dev)
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002176 || pci_target_state(pci_dev, wakeup) != pci_dev->current_state
Rafael J. Wysockic2eac4d2017-10-25 14:16:46 +02002177 || platform_pci_need_resume(pci_dev))
Rafael J. Wysockibac2a902015-01-21 02:17:42 +01002178 return false;
2179
Rafael J. Wysocki2cef5482015-09-30 01:10:24 +02002180 /*
2181 * At this point the device is good to go unless it's been configured
2182 * to generate PME at the runtime suspend time, but it is not supposed
2183 * to wake up the system. In that case, simply disable PME for it
2184 * (it will have to be re-enabled on exit from system resume).
2185 *
2186 * If the device's power state is D3cold and the platform check above
2187 * hasn't triggered, the device's configuration is suitable and we don't
2188 * need to manipulate it at all.
2189 */
2190 spin_lock_irq(&dev->power.lock);
2191
2192 if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold &&
Rafael J. Wysocki666ff6f2017-06-23 14:58:11 +02002193 !wakeup)
Rafael J. Wysocki2cef5482015-09-30 01:10:24 +02002194 __pci_pme_active(pci_dev, false);
2195
2196 spin_unlock_irq(&dev->power.lock);
2197 return true;
2198}
2199
2200/**
2201 * pci_dev_complete_resume - Finalize resume from system sleep for a device.
2202 * @pci_dev: Device to handle.
2203 *
2204 * If the device is runtime suspended and wakeup-capable, enable PME for it as
2205 * it might have been disabled during the prepare phase of system suspend if
2206 * the device was not configured for system wakeup.
2207 */
2208void pci_dev_complete_resume(struct pci_dev *pci_dev)
2209{
2210 struct device *dev = &pci_dev->dev;
2211
2212 if (!pci_dev_run_wake(pci_dev))
2213 return;
2214
2215 spin_lock_irq(&dev->power.lock);
2216
2217 if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold)
2218 __pci_pme_active(pci_dev, true);
2219
2220 spin_unlock_irq(&dev->power.lock);
Rafael J. Wysockibac2a902015-01-21 02:17:42 +01002221}
2222
Huang Yingb3c32c42012-10-25 09:36:03 +08002223void pci_config_pm_runtime_get(struct pci_dev *pdev)
2224{
2225 struct device *dev = &pdev->dev;
2226 struct device *parent = dev->parent;
2227
2228 if (parent)
2229 pm_runtime_get_sync(parent);
2230 pm_runtime_get_noresume(dev);
2231 /*
2232 * pdev->current_state is set to PCI_D3cold during suspending,
2233 * so wait until suspending completes
2234 */
2235 pm_runtime_barrier(dev);
2236 /*
2237 * Only need to resume devices in D3cold, because config
2238 * registers are still accessible for devices suspended but
2239 * not in D3cold.
2240 */
2241 if (pdev->current_state == PCI_D3cold)
2242 pm_runtime_resume(dev);
2243}
2244
2245void pci_config_pm_runtime_put(struct pci_dev *pdev)
2246{
2247 struct device *dev = &pdev->dev;
2248 struct device *parent = dev->parent;
2249
2250 pm_runtime_put(dev);
2251 if (parent)
2252 pm_runtime_put_sync(parent);
2253}
2254
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01002255/**
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002256 * pci_bridge_d3_possible - Is it possible to put the bridge into D3
2257 * @bridge: Bridge to check
2258 *
2259 * This function checks if it is possible to move the bridge to D3.
2260 * Currently we only allow D3 for recent enough PCIe ports.
2261 */
Lukas Wunnerc6a63302016-10-28 10:52:06 +02002262bool pci_bridge_d3_possible(struct pci_dev *bridge)
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002263{
2264 unsigned int year;
2265
2266 if (!pci_is_pcie(bridge))
2267 return false;
2268
2269 switch (pci_pcie_type(bridge)) {
2270 case PCI_EXP_TYPE_ROOT_PORT:
2271 case PCI_EXP_TYPE_UPSTREAM:
2272 case PCI_EXP_TYPE_DOWNSTREAM:
2273 if (pci_bridge_d3_disable)
2274 return false;
Lukas Wunner97a90ae2016-10-28 10:52:06 +02002275
2276 /*
Bjorn Helgaasd98e0922017-02-03 08:53:51 -06002277 * Hotplug interrupts cannot be delivered if the link is down,
2278 * so parents of a hotplug port must stay awake. In addition,
2279 * hotplug ports handled by firmware in System Management Mode
Lukas Wunner97a90ae2016-10-28 10:52:06 +02002280 * may not be put into D3 by the OS (Thunderbolt on non-Macs).
Bjorn Helgaasd98e0922017-02-03 08:53:51 -06002281 * For simplicity, disallow in general for now.
Lukas Wunner97a90ae2016-10-28 10:52:06 +02002282 */
Bjorn Helgaasd98e0922017-02-03 08:53:51 -06002283 if (bridge->is_hotplug_bridge)
Lukas Wunner97a90ae2016-10-28 10:52:06 +02002284 return false;
2285
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002286 if (pci_bridge_d3_force)
2287 return true;
2288
2289 /*
2290 * It should be safe to put PCIe ports from 2015 or newer
2291 * to D3.
2292 */
2293 if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) &&
2294 year >= 2015) {
2295 return true;
2296 }
2297 break;
2298 }
2299
2300 return false;
2301}
2302
2303static int pci_dev_check_d3cold(struct pci_dev *dev, void *data)
2304{
2305 bool *d3cold_ok = data;
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002306
Lukas Wunner718a0602016-10-28 10:52:06 +02002307 if (/* The device needs to be allowed to go D3cold ... */
2308 dev->no_d3cold || !dev->d3cold_allowed ||
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002309
Lukas Wunner718a0602016-10-28 10:52:06 +02002310 /* ... and if it is wakeup capable to do so from D3cold. */
2311 (device_may_wakeup(&dev->dev) &&
2312 !pci_pme_capable(dev, PCI_D3cold)) ||
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002313
Lukas Wunner718a0602016-10-28 10:52:06 +02002314 /* If it is a bridge it must be allowed to go to D3. */
Bjorn Helgaasd98e0922017-02-03 08:53:51 -06002315 !pci_power_manageable(dev))
Lukas Wunner718a0602016-10-28 10:52:06 +02002316
2317 *d3cold_ok = false;
2318
2319 return !*d3cold_ok;
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002320}
2321
2322/*
2323 * pci_bridge_d3_update - Update bridge D3 capabilities
2324 * @dev: PCI device which is changed
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002325 *
2326 * Update upstream bridge PM capabilities accordingly depending on if the
2327 * device PM configuration was changed or the device is being removed. The
2328 * change is also propagated upstream.
2329 */
Lukas Wunner1ed276a2016-10-28 10:52:06 +02002330void pci_bridge_d3_update(struct pci_dev *dev)
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002331{
Lukas Wunner1ed276a2016-10-28 10:52:06 +02002332 bool remove = !device_is_registered(&dev->dev);
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002333 struct pci_dev *bridge;
2334 bool d3cold_ok = true;
2335
2336 bridge = pci_upstream_bridge(dev);
2337 if (!bridge || !pci_bridge_d3_possible(bridge))
2338 return;
2339
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002340 /*
Lukas Wunnere8559b712016-10-28 10:52:06 +02002341 * If D3 is currently allowed for the bridge, removing one of its
2342 * children won't change that.
2343 */
2344 if (remove && bridge->bridge_d3)
2345 return;
2346
2347 /*
2348 * If D3 is currently allowed for the bridge and a child is added or
2349 * changed, disallowance of D3 can only be caused by that child, so
2350 * we only need to check that single device, not any of its siblings.
2351 *
2352 * If D3 is currently not allowed for the bridge, checking the device
2353 * first may allow us to skip checking its siblings.
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002354 */
2355 if (!remove)
2356 pci_dev_check_d3cold(dev, &d3cold_ok);
2357
Lukas Wunnere8559b712016-10-28 10:52:06 +02002358 /*
2359 * If D3 is currently not allowed for the bridge, this may be caused
2360 * either by the device being changed/removed or any of its siblings,
2361 * so we need to go through all children to find out if one of them
2362 * continues to block D3.
2363 */
2364 if (d3cold_ok && !bridge->bridge_d3)
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002365 pci_walk_bus(bridge->subordinate, pci_dev_check_d3cold,
2366 &d3cold_ok);
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002367
2368 if (bridge->bridge_d3 != d3cold_ok) {
2369 bridge->bridge_d3 = d3cold_ok;
2370 /* Propagate change to upstream bridges */
Lukas Wunner1ed276a2016-10-28 10:52:06 +02002371 pci_bridge_d3_update(bridge);
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002372 }
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002373}
2374
2375/**
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002376 * pci_d3cold_enable - Enable D3cold for device
2377 * @dev: PCI device to handle
2378 *
2379 * This function can be used in drivers to enable D3cold from the device
2380 * they handle. It also updates upstream PCI bridge PM capabilities
2381 * accordingly.
2382 */
2383void pci_d3cold_enable(struct pci_dev *dev)
2384{
2385 if (dev->no_d3cold) {
2386 dev->no_d3cold = false;
Lukas Wunner1ed276a2016-10-28 10:52:06 +02002387 pci_bridge_d3_update(dev);
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002388 }
2389}
2390EXPORT_SYMBOL_GPL(pci_d3cold_enable);
2391
2392/**
2393 * pci_d3cold_disable - Disable D3cold for device
2394 * @dev: PCI device to handle
2395 *
2396 * This function can be used in drivers to disable D3cold from the device
2397 * they handle. It also updates upstream PCI bridge PM capabilities
2398 * accordingly.
2399 */
2400void pci_d3cold_disable(struct pci_dev *dev)
2401{
2402 if (!dev->no_d3cold) {
2403 dev->no_d3cold = true;
Lukas Wunner1ed276a2016-10-28 10:52:06 +02002404 pci_bridge_d3_update(dev);
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002405 }
2406}
2407EXPORT_SYMBOL_GPL(pci_d3cold_disable);
2408
2409/**
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002410 * pci_pm_init - Initialize PM functions of given PCI device
2411 * @dev: PCI device to handle.
2412 */
2413void pci_pm_init(struct pci_dev *dev)
2414{
2415 int pm;
2416 u16 pmc;
David Brownell075c1772007-04-26 00:12:06 -07002417
Rafael J. Wysockibb910a72010-02-27 21:37:37 +01002418 pm_runtime_forbid(&dev->dev);
Huang Ying967577b2012-11-20 16:08:22 +08002419 pm_runtime_set_active(&dev->dev);
2420 pm_runtime_enable(&dev->dev);
Rafael J. Wysockia1e4d722010-02-08 19:16:33 +01002421 device_enable_async_suspend(&dev->dev);
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02002422 dev->wakeup_prepared = false;
Rafael J. Wysockibb910a72010-02-27 21:37:37 +01002423
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002424 dev->pm_cap = 0;
Rafael J. Wysockiffaddbe2013-04-10 10:32:51 +00002425 dev->pme_support = 0;
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 /* find PCI PM capability in list */
2428 pm = pci_find_capability(dev, PCI_CAP_ID_PM);
David Brownell075c1772007-04-26 00:12:06 -07002429 if (!pm)
Linus Torvalds50246dd2009-01-16 08:14:51 -08002430 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 /* Check device's ability to generate PME# */
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002432 pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002434 if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06002435 pci_err(dev, "unsupported PM cap regs version (%u)\n",
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002436 pmc & PCI_PM_CAP_VER_MASK);
Linus Torvalds50246dd2009-01-16 08:14:51 -08002437 return;
David Brownell075c1772007-04-26 00:12:06 -07002438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002440 dev->pm_cap = pm;
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +01002441 dev->d3_delay = PCI_PM_D3_WAIT;
Huang Ying448bd852012-06-23 10:23:51 +08002442 dev->d3cold_delay = PCI_PM_D3COLD_WAIT;
Mika Westerberg9d26d3a2016-06-02 11:17:12 +03002443 dev->bridge_d3 = pci_bridge_d3_possible(dev);
Huang Ying4f9c1392012-08-08 09:07:38 +08002444 dev->d3cold_allowed = true;
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002445
2446 dev->d1_support = false;
2447 dev->d2_support = false;
2448 if (!pci_no_d1d2(dev)) {
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06002449 if (pmc & PCI_PM_CAP_D1)
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002450 dev->d1_support = true;
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06002451 if (pmc & PCI_PM_CAP_D2)
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002452 dev->d2_support = true;
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06002453
2454 if (dev->d1_support || dev->d2_support)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002455 pci_printk(KERN_DEBUG, dev, "supports%s%s\n",
Jesse Barnesec84f122008-09-23 11:43:34 -07002456 dev->d1_support ? " D1" : "",
2457 dev->d2_support ? " D2" : "");
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002458 }
2459
2460 pmc &= PCI_PM_CAP_PME_MASK;
2461 if (pmc) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06002462 pci_printk(KERN_DEBUG, dev, "PME# supported from%s%s%s%s%s\n",
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06002463 (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
2464 (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
2465 (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
2466 (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
2467 (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002468 dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02002469 dev->pme_poll = true;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002470 /*
2471 * Make device's PM flags reflect the wake-up capability, but
2472 * let the user space enable it to wake up the system as needed.
2473 */
2474 device_set_wakeup_capable(&dev->dev, true);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002475 /* Disable the PME# generation functionality */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002476 pci_pme_active(dev, false);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478}
2479
Sean O. Stalley938174e2015-10-29 17:35:39 -05002480static unsigned long pci_ea_flags(struct pci_dev *dev, u8 prop)
2481{
Alex Williamson92efb1b2016-05-16 15:12:02 -05002482 unsigned long flags = IORESOURCE_PCI_FIXED | IORESOURCE_PCI_EA_BEI;
Sean O. Stalley938174e2015-10-29 17:35:39 -05002483
2484 switch (prop) {
2485 case PCI_EA_P_MEM:
2486 case PCI_EA_P_VF_MEM:
2487 flags |= IORESOURCE_MEM;
2488 break;
2489 case PCI_EA_P_MEM_PREFETCH:
2490 case PCI_EA_P_VF_MEM_PREFETCH:
2491 flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
2492 break;
2493 case PCI_EA_P_IO:
2494 flags |= IORESOURCE_IO;
2495 break;
2496 default:
2497 return 0;
2498 }
2499
2500 return flags;
2501}
2502
2503static struct resource *pci_ea_get_resource(struct pci_dev *dev, u8 bei,
2504 u8 prop)
2505{
2506 if (bei <= PCI_EA_BEI_BAR5 && prop <= PCI_EA_P_IO)
2507 return &dev->resource[bei];
David Daney11183992015-10-29 17:35:40 -05002508#ifdef CONFIG_PCI_IOV
2509 else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5 &&
2510 (prop == PCI_EA_P_VF_MEM || prop == PCI_EA_P_VF_MEM_PREFETCH))
2511 return &dev->resource[PCI_IOV_RESOURCES +
2512 bei - PCI_EA_BEI_VF_BAR0];
2513#endif
Sean O. Stalley938174e2015-10-29 17:35:39 -05002514 else if (bei == PCI_EA_BEI_ROM)
2515 return &dev->resource[PCI_ROM_RESOURCE];
2516 else
2517 return NULL;
2518}
2519
2520/* Read an Enhanced Allocation (EA) entry */
2521static int pci_ea_read(struct pci_dev *dev, int offset)
2522{
2523 struct resource *res;
2524 int ent_size, ent_offset = offset;
2525 resource_size_t start, end;
2526 unsigned long flags;
Bjorn Helgaas26635112015-10-29 17:35:40 -05002527 u32 dw0, bei, base, max_offset;
Sean O. Stalley938174e2015-10-29 17:35:39 -05002528 u8 prop;
2529 bool support_64 = (sizeof(resource_size_t) >= 8);
2530
2531 pci_read_config_dword(dev, ent_offset, &dw0);
2532 ent_offset += 4;
2533
2534 /* Entry size field indicates DWORDs after 1st */
2535 ent_size = ((dw0 & PCI_EA_ES) + 1) << 2;
2536
2537 if (!(dw0 & PCI_EA_ENABLE)) /* Entry not enabled */
2538 goto out;
2539
Bjorn Helgaas26635112015-10-29 17:35:40 -05002540 bei = (dw0 & PCI_EA_BEI) >> 4;
2541 prop = (dw0 & PCI_EA_PP) >> 8;
2542
Sean O. Stalley938174e2015-10-29 17:35:39 -05002543 /*
2544 * If the Property is in the reserved range, try the Secondary
2545 * Property instead.
2546 */
2547 if (prop > PCI_EA_P_BRIDGE_IO && prop < PCI_EA_P_MEM_RESERVED)
Bjorn Helgaas26635112015-10-29 17:35:40 -05002548 prop = (dw0 & PCI_EA_SP) >> 16;
Sean O. Stalley938174e2015-10-29 17:35:39 -05002549 if (prop > PCI_EA_P_BRIDGE_IO)
2550 goto out;
2551
Bjorn Helgaas26635112015-10-29 17:35:40 -05002552 res = pci_ea_get_resource(dev, bei, prop);
Sean O. Stalley938174e2015-10-29 17:35:39 -05002553 if (!res) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06002554 pci_err(dev, "Unsupported EA entry BEI: %u\n", bei);
Sean O. Stalley938174e2015-10-29 17:35:39 -05002555 goto out;
2556 }
2557
2558 flags = pci_ea_flags(dev, prop);
2559 if (!flags) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06002560 pci_err(dev, "Unsupported EA properties: %#x\n", prop);
Sean O. Stalley938174e2015-10-29 17:35:39 -05002561 goto out;
2562 }
2563
2564 /* Read Base */
2565 pci_read_config_dword(dev, ent_offset, &base);
2566 start = (base & PCI_EA_FIELD_MASK);
2567 ent_offset += 4;
2568
2569 /* Read MaxOffset */
2570 pci_read_config_dword(dev, ent_offset, &max_offset);
2571 ent_offset += 4;
2572
2573 /* Read Base MSBs (if 64-bit entry) */
2574 if (base & PCI_EA_IS_64) {
2575 u32 base_upper;
2576
2577 pci_read_config_dword(dev, ent_offset, &base_upper);
2578 ent_offset += 4;
2579
2580 flags |= IORESOURCE_MEM_64;
2581
2582 /* entry starts above 32-bit boundary, can't use */
2583 if (!support_64 && base_upper)
2584 goto out;
2585
2586 if (support_64)
2587 start |= ((u64)base_upper << 32);
2588 }
2589
2590 end = start + (max_offset | 0x03);
2591
2592 /* Read MaxOffset MSBs (if 64-bit entry) */
2593 if (max_offset & PCI_EA_IS_64) {
2594 u32 max_offset_upper;
2595
2596 pci_read_config_dword(dev, ent_offset, &max_offset_upper);
2597 ent_offset += 4;
2598
2599 flags |= IORESOURCE_MEM_64;
2600
2601 /* entry too big, can't use */
2602 if (!support_64 && max_offset_upper)
2603 goto out;
2604
2605 if (support_64)
2606 end += ((u64)max_offset_upper << 32);
2607 }
2608
2609 if (end < start) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06002610 pci_err(dev, "EA Entry crosses address boundary\n");
Sean O. Stalley938174e2015-10-29 17:35:39 -05002611 goto out;
2612 }
2613
2614 if (ent_size != ent_offset - offset) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06002615 pci_err(dev, "EA Entry Size (%d) does not match length read (%d)\n",
Sean O. Stalley938174e2015-10-29 17:35:39 -05002616 ent_size, ent_offset - offset);
2617 goto out;
2618 }
2619
2620 res->name = pci_name(dev);
2621 res->start = start;
2622 res->end = end;
2623 res->flags = flags;
Bjorn Helgaas597becb2015-10-29 17:35:40 -05002624
2625 if (bei <= PCI_EA_BEI_BAR5)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002626 pci_printk(KERN_DEBUG, dev, "BAR %d: %pR (from Enhanced Allocation, properties %#02x)\n",
Bjorn Helgaas597becb2015-10-29 17:35:40 -05002627 bei, res, prop);
2628 else if (bei == PCI_EA_BEI_ROM)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002629 pci_printk(KERN_DEBUG, dev, "ROM: %pR (from Enhanced Allocation, properties %#02x)\n",
Bjorn Helgaas597becb2015-10-29 17:35:40 -05002630 res, prop);
2631 else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002632 pci_printk(KERN_DEBUG, dev, "VF BAR %d: %pR (from Enhanced Allocation, properties %#02x)\n",
Bjorn Helgaas597becb2015-10-29 17:35:40 -05002633 bei - PCI_EA_BEI_VF_BAR0, res, prop);
2634 else
Frederick Lawler7506dc72018-01-18 12:55:24 -06002635 pci_printk(KERN_DEBUG, dev, "BEI %d res: %pR (from Enhanced Allocation, properties %#02x)\n",
Bjorn Helgaas597becb2015-10-29 17:35:40 -05002636 bei, res, prop);
2637
Sean O. Stalley938174e2015-10-29 17:35:39 -05002638out:
2639 return offset + ent_size;
2640}
2641
Colin Ian Kingdcbb4082016-04-05 12:12:45 -05002642/* Enhanced Allocation Initialization */
Sean O. Stalley938174e2015-10-29 17:35:39 -05002643void pci_ea_init(struct pci_dev *dev)
2644{
2645 int ea;
2646 u8 num_ent;
2647 int offset;
2648 int i;
2649
2650 /* find PCI EA capability in list */
2651 ea = pci_find_capability(dev, PCI_CAP_ID_EA);
2652 if (!ea)
2653 return;
2654
2655 /* determine the number of entries */
2656 pci_bus_read_config_byte(dev->bus, dev->devfn, ea + PCI_EA_NUM_ENT,
2657 &num_ent);
2658 num_ent &= PCI_EA_NUM_ENT_MASK;
2659
2660 offset = ea + PCI_EA_FIRST_ENT;
2661
2662 /* Skip DWORD 2 for type 1 functions */
2663 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
2664 offset += 4;
2665
2666 /* parse each EA entry */
2667 for (i = 0; i < num_ent; ++i)
2668 offset = pci_ea_read(dev, offset);
2669}
2670
Yinghai Lu34a48762012-02-11 00:18:41 -08002671static void pci_add_saved_cap(struct pci_dev *pci_dev,
2672 struct pci_cap_saved_state *new_cap)
2673{
2674 hlist_add_head(&new_cap->next, &pci_dev->saved_cap_space);
2675}
2676
Jesse Barneseb9c39d2008-12-17 12:10:05 -08002677/**
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002678 * _pci_add_cap_save_buffer - allocate buffer for saving given
2679 * capability registers
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002680 * @dev: the PCI device
2681 * @cap: the capability to allocate the buffer for
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002682 * @extended: Standard or Extended capability ID
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002683 * @size: requested size of the buffer
2684 */
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002685static int _pci_add_cap_save_buffer(struct pci_dev *dev, u16 cap,
2686 bool extended, unsigned int size)
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002687{
2688 int pos;
2689 struct pci_cap_saved_state *save_state;
2690
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002691 if (extended)
2692 pos = pci_find_ext_capability(dev, cap);
2693 else
2694 pos = pci_find_capability(dev, cap);
2695
Wei Yang0a1a9b42015-06-30 09:16:44 +08002696 if (!pos)
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002697 return 0;
2698
2699 save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
2700 if (!save_state)
2701 return -ENOMEM;
2702
Alex Williamson24a4742f2011-05-10 10:02:11 -06002703 save_state->cap.cap_nr = cap;
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002704 save_state->cap.cap_extended = extended;
Alex Williamson24a4742f2011-05-10 10:02:11 -06002705 save_state->cap.size = size;
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002706 pci_add_saved_cap(dev, save_state);
2707
2708 return 0;
2709}
2710
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002711int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size)
2712{
2713 return _pci_add_cap_save_buffer(dev, cap, false, size);
2714}
2715
2716int pci_add_ext_cap_save_buffer(struct pci_dev *dev, u16 cap, unsigned int size)
2717{
2718 return _pci_add_cap_save_buffer(dev, cap, true, size);
2719}
2720
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002721/**
2722 * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
2723 * @dev: the PCI device
2724 */
2725void pci_allocate_cap_save_buffers(struct pci_dev *dev)
2726{
2727 int error;
2728
Yu Zhao89858512009-02-16 02:55:47 +08002729 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP,
2730 PCI_EXP_SAVE_REGS * sizeof(u16));
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002731 if (error)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002732 pci_err(dev, "unable to preallocate PCI Express save buffer\n");
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002733
2734 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
2735 if (error)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002736 pci_err(dev, "unable to preallocate PCI-X save buffer\n");
Alex Williamson425c1b22013-12-17 16:43:51 -07002737
2738 pci_allocate_vc_save_buffers(dev);
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002739}
2740
Yinghai Luf7968412012-02-11 00:18:30 -08002741void pci_free_cap_save_buffers(struct pci_dev *dev)
2742{
2743 struct pci_cap_saved_state *tmp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002744 struct hlist_node *n;
Yinghai Luf7968412012-02-11 00:18:30 -08002745
Sasha Levinb67bfe02013-02-27 17:06:00 -08002746 hlist_for_each_entry_safe(tmp, n, &dev->saved_cap_space, next)
Yinghai Luf7968412012-02-11 00:18:30 -08002747 kfree(tmp);
2748}
2749
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002750/**
Yijing Wang31ab2472013-01-15 11:12:17 +08002751 * pci_configure_ari - enable or disable ARI forwarding
Yu Zhao58c3a722008-10-14 14:02:53 +08002752 * @dev: the PCI device
Yijing Wangb0cc6022013-01-15 11:12:16 +08002753 *
2754 * If @dev and its upstream bridge both support ARI, enable ARI in the
2755 * bridge. Otherwise, disable ARI in the bridge.
Yu Zhao58c3a722008-10-14 14:02:53 +08002756 */
Yijing Wang31ab2472013-01-15 11:12:17 +08002757void pci_configure_ari(struct pci_dev *dev)
Yu Zhao58c3a722008-10-14 14:02:53 +08002758{
Yu Zhao58c3a722008-10-14 14:02:53 +08002759 u32 cap;
Zhao, Yu81135872008-10-23 13:15:39 +08002760 struct pci_dev *bridge;
Yu Zhao58c3a722008-10-14 14:02:53 +08002761
Rafael J. Wysocki6748dcc2012-03-01 00:06:33 +01002762 if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
Yu Zhao58c3a722008-10-14 14:02:53 +08002763 return;
2764
Zhao, Yu81135872008-10-23 13:15:39 +08002765 bridge = dev->bus->self;
Myron Stowecb97ae32012-06-01 15:16:31 -06002766 if (!bridge)
Zhao, Yu81135872008-10-23 13:15:39 +08002767 return;
2768
Jiang Liu59875ae2012-07-24 17:20:06 +08002769 pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
Yu Zhao58c3a722008-10-14 14:02:53 +08002770 if (!(cap & PCI_EXP_DEVCAP2_ARI))
2771 return;
2772
Yijing Wangb0cc6022013-01-15 11:12:16 +08002773 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
2774 pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
2775 PCI_EXP_DEVCTL2_ARI);
2776 bridge->ari_enabled = 1;
2777 } else {
2778 pcie_capability_clear_word(bridge, PCI_EXP_DEVCTL2,
2779 PCI_EXP_DEVCTL2_ARI);
2780 bridge->ari_enabled = 0;
2781 }
Yu Zhao58c3a722008-10-14 14:02:53 +08002782}
2783
Chris Wright5d990b62009-12-04 12:15:21 -08002784static int pci_acs_enable;
2785
2786/**
2787 * pci_request_acs - ask for ACS to be enabled if supported
2788 */
2789void pci_request_acs(void)
2790{
2791 pci_acs_enable = 1;
2792}
2793
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07002794/**
Alex Williamson2c744242014-02-03 14:27:33 -07002795 * pci_std_enable_acs - enable ACS on devices using standard ACS capabilites
Allen Kayae21ee62009-10-07 10:27:17 -07002796 * @dev: the PCI device
2797 */
Alex Williamsonc1d61c92016-03-31 16:34:32 -06002798static void pci_std_enable_acs(struct pci_dev *dev)
Allen Kayae21ee62009-10-07 10:27:17 -07002799{
2800 int pos;
2801 u16 cap;
2802 u16 ctrl;
2803
Allen Kayae21ee62009-10-07 10:27:17 -07002804 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
2805 if (!pos)
Alex Williamsonc1d61c92016-03-31 16:34:32 -06002806 return;
Allen Kayae21ee62009-10-07 10:27:17 -07002807
2808 pci_read_config_word(dev, pos + PCI_ACS_CAP, &cap);
2809 pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
2810
2811 /* Source Validation */
2812 ctrl |= (cap & PCI_ACS_SV);
2813
2814 /* P2P Request Redirect */
2815 ctrl |= (cap & PCI_ACS_RR);
2816
2817 /* P2P Completion Redirect */
2818 ctrl |= (cap & PCI_ACS_CR);
2819
2820 /* Upstream Forwarding */
2821 ctrl |= (cap & PCI_ACS_UF);
2822
2823 pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
Alex Williamson2c744242014-02-03 14:27:33 -07002824}
2825
2826/**
2827 * pci_enable_acs - enable ACS if hardware support it
2828 * @dev: the PCI device
2829 */
2830void pci_enable_acs(struct pci_dev *dev)
2831{
2832 if (!pci_acs_enable)
2833 return;
2834
Alex Williamsonc1d61c92016-03-31 16:34:32 -06002835 if (!pci_dev_specific_enable_acs(dev))
Alex Williamson2c744242014-02-03 14:27:33 -07002836 return;
2837
Alex Williamsonc1d61c92016-03-31 16:34:32 -06002838 pci_std_enable_acs(dev);
Allen Kayae21ee62009-10-07 10:27:17 -07002839}
2840
Alex Williamson0a671192013-06-27 16:39:48 -06002841static bool pci_acs_flags_enabled(struct pci_dev *pdev, u16 acs_flags)
2842{
2843 int pos;
Alex Williamson83db7e02013-06-27 16:39:54 -06002844 u16 cap, ctrl;
Alex Williamson0a671192013-06-27 16:39:48 -06002845
2846 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ACS);
2847 if (!pos)
2848 return false;
2849
Alex Williamson83db7e02013-06-27 16:39:54 -06002850 /*
2851 * Except for egress control, capabilities are either required
2852 * or only required if controllable. Features missing from the
2853 * capability field can therefore be assumed as hard-wired enabled.
2854 */
2855 pci_read_config_word(pdev, pos + PCI_ACS_CAP, &cap);
2856 acs_flags &= (cap | PCI_ACS_EC);
2857
Alex Williamson0a671192013-06-27 16:39:48 -06002858 pci_read_config_word(pdev, pos + PCI_ACS_CTRL, &ctrl);
2859 return (ctrl & acs_flags) == acs_flags;
2860}
2861
Allen Kayae21ee62009-10-07 10:27:17 -07002862/**
Alex Williamsonad805752012-06-11 05:27:07 +00002863 * pci_acs_enabled - test ACS against required flags for a given device
2864 * @pdev: device to test
2865 * @acs_flags: required PCI ACS flags
2866 *
2867 * Return true if the device supports the provided flags. Automatically
2868 * filters out flags that are not implemented on multifunction devices.
Alex Williamson0a671192013-06-27 16:39:48 -06002869 *
2870 * Note that this interface checks the effective ACS capabilities of the
2871 * device rather than the actual capabilities. For instance, most single
2872 * function endpoints are not required to support ACS because they have no
2873 * opportunity for peer-to-peer access. We therefore return 'true'
2874 * regardless of whether the device exposes an ACS capability. This makes
2875 * it much easier for callers of this function to ignore the actual type
2876 * or topology of the device when testing ACS support.
Alex Williamsonad805752012-06-11 05:27:07 +00002877 */
2878bool pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags)
2879{
Alex Williamson0a671192013-06-27 16:39:48 -06002880 int ret;
Alex Williamsonad805752012-06-11 05:27:07 +00002881
2882 ret = pci_dev_specific_acs_enabled(pdev, acs_flags);
2883 if (ret >= 0)
2884 return ret > 0;
2885
Alex Williamson0a671192013-06-27 16:39:48 -06002886 /*
2887 * Conventional PCI and PCI-X devices never support ACS, either
2888 * effectively or actually. The shared bus topology implies that
2889 * any device on the bus can receive or snoop DMA.
2890 */
Alex Williamsonad805752012-06-11 05:27:07 +00002891 if (!pci_is_pcie(pdev))
2892 return false;
2893
Alex Williamson0a671192013-06-27 16:39:48 -06002894 switch (pci_pcie_type(pdev)) {
2895 /*
2896 * PCI/X-to-PCIe bridges are not specifically mentioned by the spec,
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002897 * but since their primary interface is PCI/X, we conservatively
Alex Williamson0a671192013-06-27 16:39:48 -06002898 * handle them as we would a non-PCIe device.
2899 */
2900 case PCI_EXP_TYPE_PCIE_BRIDGE:
2901 /*
2902 * PCIe 3.0, 6.12.1 excludes ACS on these devices. "ACS is never
2903 * applicable... must never implement an ACS Extended Capability...".
2904 * This seems arbitrary, but we take a conservative interpretation
2905 * of this statement.
2906 */
2907 case PCI_EXP_TYPE_PCI_BRIDGE:
2908 case PCI_EXP_TYPE_RC_EC:
2909 return false;
2910 /*
2911 * PCIe 3.0, 6.12.1.1 specifies that downstream and root ports should
2912 * implement ACS in order to indicate their peer-to-peer capabilities,
2913 * regardless of whether they are single- or multi-function devices.
2914 */
2915 case PCI_EXP_TYPE_DOWNSTREAM:
2916 case PCI_EXP_TYPE_ROOT_PORT:
2917 return pci_acs_flags_enabled(pdev, acs_flags);
2918 /*
2919 * PCIe 3.0, 6.12.1.2 specifies ACS capabilities that should be
2920 * implemented by the remaining PCIe types to indicate peer-to-peer
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002921 * capabilities, but only when they are part of a multifunction
Alex Williamson0a671192013-06-27 16:39:48 -06002922 * device. The footnote for section 6.12 indicates the specific
2923 * PCIe types included here.
2924 */
2925 case PCI_EXP_TYPE_ENDPOINT:
2926 case PCI_EXP_TYPE_UPSTREAM:
2927 case PCI_EXP_TYPE_LEG_END:
2928 case PCI_EXP_TYPE_RC_END:
2929 if (!pdev->multifunction)
2930 break;
2931
Alex Williamson0a671192013-06-27 16:39:48 -06002932 return pci_acs_flags_enabled(pdev, acs_flags);
Alex Williamsonad805752012-06-11 05:27:07 +00002933 }
2934
Alex Williamson0a671192013-06-27 16:39:48 -06002935 /*
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002936 * PCIe 3.0, 6.12.1.3 specifies no ACS capabilities are applicable
Alex Williamson0a671192013-06-27 16:39:48 -06002937 * to single function devices with the exception of downstream ports.
2938 */
Alex Williamsonad805752012-06-11 05:27:07 +00002939 return true;
2940}
2941
2942/**
2943 * pci_acs_path_enable - test ACS flags from start to end in a hierarchy
2944 * @start: starting downstream device
2945 * @end: ending upstream device or NULL to search to the root bus
2946 * @acs_flags: required flags
2947 *
2948 * Walk up a device tree from start to end testing PCI ACS support. If
2949 * any step along the way does not support the required flags, return false.
2950 */
2951bool pci_acs_path_enabled(struct pci_dev *start,
2952 struct pci_dev *end, u16 acs_flags)
2953{
2954 struct pci_dev *pdev, *parent = start;
2955
2956 do {
2957 pdev = parent;
2958
2959 if (!pci_acs_enabled(pdev, acs_flags))
2960 return false;
2961
2962 if (pci_is_root_bus(pdev->bus))
2963 return (end == NULL);
2964
2965 parent = pdev->bus->self;
2966 } while (pdev != end);
2967
2968 return true;
2969}
2970
2971/**
Christian König276b7382017-10-24 14:40:20 -05002972 * pci_rebar_find_pos - find position of resize ctrl reg for BAR
2973 * @pdev: PCI device
2974 * @bar: BAR to find
2975 *
2976 * Helper to find the position of the ctrl register for a BAR.
2977 * Returns -ENOTSUPP if resizable BARs are not supported at all.
2978 * Returns -ENOENT if no ctrl register for the BAR could be found.
2979 */
2980static int pci_rebar_find_pos(struct pci_dev *pdev, int bar)
2981{
2982 unsigned int pos, nbars, i;
2983 u32 ctrl;
2984
2985 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_REBAR);
2986 if (!pos)
2987 return -ENOTSUPP;
2988
2989 pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
2990 nbars = (ctrl & PCI_REBAR_CTRL_NBAR_MASK) >>
2991 PCI_REBAR_CTRL_NBAR_SHIFT;
2992
2993 for (i = 0; i < nbars; i++, pos += 8) {
2994 int bar_idx;
2995
2996 pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
2997 bar_idx = ctrl & PCI_REBAR_CTRL_BAR_IDX;
2998 if (bar_idx == bar)
2999 return pos;
3000 }
3001
3002 return -ENOENT;
3003}
3004
3005/**
3006 * pci_rebar_get_possible_sizes - get possible sizes for BAR
3007 * @pdev: PCI device
3008 * @bar: BAR to query
3009 *
3010 * Get the possible sizes of a resizable BAR as bitmask defined in the spec
3011 * (bit 0=1MB, bit 19=512GB). Returns 0 if BAR isn't resizable.
3012 */
3013u32 pci_rebar_get_possible_sizes(struct pci_dev *pdev, int bar)
3014{
3015 int pos;
3016 u32 cap;
3017
3018 pos = pci_rebar_find_pos(pdev, bar);
3019 if (pos < 0)
3020 return 0;
3021
3022 pci_read_config_dword(pdev, pos + PCI_REBAR_CAP, &cap);
3023 return (cap & PCI_REBAR_CAP_SIZES) >> 4;
3024}
3025
3026/**
3027 * pci_rebar_get_current_size - get the current size of a BAR
3028 * @pdev: PCI device
3029 * @bar: BAR to set size to
3030 *
3031 * Read the size of a BAR from the resizable BAR config.
3032 * Returns size if found or negative error code.
3033 */
3034int pci_rebar_get_current_size(struct pci_dev *pdev, int bar)
3035{
3036 int pos;
3037 u32 ctrl;
3038
3039 pos = pci_rebar_find_pos(pdev, bar);
3040 if (pos < 0)
3041 return pos;
3042
3043 pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
3044 return (ctrl & PCI_REBAR_CTRL_BAR_SIZE) >> 8;
3045}
3046
3047/**
3048 * pci_rebar_set_size - set a new size for a BAR
3049 * @pdev: PCI device
3050 * @bar: BAR to set size to
3051 * @size: new size as defined in the spec (0=1MB, 19=512GB)
3052 *
3053 * Set the new size of a BAR as defined in the spec.
3054 * Returns zero if resizing was successful, error code otherwise.
3055 */
3056int pci_rebar_set_size(struct pci_dev *pdev, int bar, int size)
3057{
3058 int pos;
3059 u32 ctrl;
3060
3061 pos = pci_rebar_find_pos(pdev, bar);
3062 if (pos < 0)
3063 return pos;
3064
3065 pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
3066 ctrl &= ~PCI_REBAR_CTRL_BAR_SIZE;
3067 ctrl |= size << 8;
3068 pci_write_config_dword(pdev, pos + PCI_REBAR_CTRL, ctrl);
3069 return 0;
3070}
3071
3072/**
Jay Cornwall430a2362018-01-04 19:44:59 -05003073 * pci_enable_atomic_ops_to_root - enable AtomicOp requests to root port
3074 * @dev: the PCI device
3075 * @cap_mask: mask of desired AtomicOp sizes, including one or more of:
3076 * PCI_EXP_DEVCAP2_ATOMIC_COMP32
3077 * PCI_EXP_DEVCAP2_ATOMIC_COMP64
3078 * PCI_EXP_DEVCAP2_ATOMIC_COMP128
3079 *
3080 * Return 0 if all upstream bridges support AtomicOp routing, egress
3081 * blocking is disabled on all upstream ports, and the root port supports
3082 * the requested completion capabilities (32-bit, 64-bit and/or 128-bit
3083 * AtomicOp completion), or negative otherwise.
3084 */
3085int pci_enable_atomic_ops_to_root(struct pci_dev *dev, u32 cap_mask)
3086{
3087 struct pci_bus *bus = dev->bus;
3088 struct pci_dev *bridge;
3089 u32 cap, ctl2;
3090
3091 if (!pci_is_pcie(dev))
3092 return -EINVAL;
3093
3094 /*
3095 * Per PCIe r4.0, sec 6.15, endpoints and root ports may be
3096 * AtomicOp requesters. For now, we only support endpoints as
3097 * requesters and root ports as completers. No endpoints as
3098 * completers, and no peer-to-peer.
3099 */
3100
3101 switch (pci_pcie_type(dev)) {
3102 case PCI_EXP_TYPE_ENDPOINT:
3103 case PCI_EXP_TYPE_LEG_END:
3104 case PCI_EXP_TYPE_RC_END:
3105 break;
3106 default:
3107 return -EINVAL;
3108 }
3109
3110 while (bus->parent) {
3111 bridge = bus->self;
3112
3113 pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
3114
3115 switch (pci_pcie_type(bridge)) {
3116 /* Ensure switch ports support AtomicOp routing */
3117 case PCI_EXP_TYPE_UPSTREAM:
3118 case PCI_EXP_TYPE_DOWNSTREAM:
3119 if (!(cap & PCI_EXP_DEVCAP2_ATOMIC_ROUTE))
3120 return -EINVAL;
3121 break;
3122
3123 /* Ensure root port supports all the sizes we care about */
3124 case PCI_EXP_TYPE_ROOT_PORT:
3125 if ((cap & cap_mask) != cap_mask)
3126 return -EINVAL;
3127 break;
3128 }
3129
3130 /* Ensure upstream ports don't block AtomicOps on egress */
3131 if (!bridge->has_secondary_link) {
3132 pcie_capability_read_dword(bridge, PCI_EXP_DEVCTL2,
3133 &ctl2);
3134 if (ctl2 & PCI_EXP_DEVCTL2_ATOMIC_EGRESS_BLOCK)
3135 return -EINVAL;
3136 }
3137
3138 bus = bus->parent;
3139 }
3140
3141 pcie_capability_set_word(dev, PCI_EXP_DEVCTL2,
3142 PCI_EXP_DEVCTL2_ATOMIC_REQ);
3143 return 0;
3144}
3145EXPORT_SYMBOL(pci_enable_atomic_ops_to_root);
3146
3147/**
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07003148 * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
3149 * @dev: the PCI device
Wang Sheng-Huibb5c2de2013-05-28 11:17:41 +08003150 * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD)
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07003151 *
3152 * Perform INTx swizzling for a device behind one level of bridge. This is
3153 * required by section 9.1 of the PCI-to-PCI bridge specification for devices
Matthew Wilcox46b952a2009-07-01 14:24:30 -07003154 * behind bridges on add-in cards. For devices with ARI enabled, the slot
3155 * number is always 0 (see the Implementation Note in section 2.2.8.1 of
3156 * the PCI Express Base Specification, Revision 2.1)
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07003157 */
John Crispin3df425f2012-04-12 17:33:07 +02003158u8 pci_swizzle_interrupt_pin(const struct pci_dev *dev, u8 pin)
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07003159{
Matthew Wilcox46b952a2009-07-01 14:24:30 -07003160 int slot;
3161
3162 if (pci_ari_enabled(dev->bus))
3163 slot = 0;
3164 else
3165 slot = PCI_SLOT(dev->devfn);
3166
3167 return (((pin - 1) + slot) % 4) + 1;
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07003168}
3169
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003170int pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
3172 u8 pin;
3173
Kristen Accardi514d2072005-11-02 16:24:39 -08003174 pin = dev->pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 if (!pin)
3176 return -1;
Bjorn Helgaas878f2e52008-12-09 16:11:46 -07003177
Kenji Kaneshige8784fd42009-05-26 16:07:33 +09003178 while (!pci_is_root_bus(dev->bus)) {
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07003179 pin = pci_swizzle_interrupt_pin(dev, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 dev = dev->bus->self;
3181 }
3182 *bridge = dev;
3183 return pin;
3184}
3185
3186/**
Bjorn Helgaas68feac82008-12-16 21:36:55 -07003187 * pci_common_swizzle - swizzle INTx all the way to root bridge
3188 * @dev: the PCI device
3189 * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
3190 *
3191 * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
3192 * bridges all the way up to a PCI root bus.
3193 */
3194u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
3195{
3196 u8 pin = *pinp;
3197
Kenji Kaneshige1eb39482009-05-26 16:08:36 +09003198 while (!pci_is_root_bus(dev->bus)) {
Bjorn Helgaas68feac82008-12-16 21:36:55 -07003199 pin = pci_swizzle_interrupt_pin(dev, pin);
3200 dev = dev->bus->self;
3201 }
3202 *pinp = pin;
3203 return PCI_SLOT(dev->devfn);
3204}
Ray Juie6b29de2015-04-08 11:21:33 -07003205EXPORT_SYMBOL_GPL(pci_common_swizzle);
Bjorn Helgaas68feac82008-12-16 21:36:55 -07003206
3207/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 * pci_release_region - Release a PCI bar
3209 * @pdev: PCI device whose resources were previously reserved by pci_request_region
3210 * @bar: BAR to release
3211 *
3212 * Releases the PCI I/O and memory resources previously reserved by a
3213 * successful call to pci_request_region. Call this function only
3214 * after all use of the PCI regions has ceased.
3215 */
3216void pci_release_region(struct pci_dev *pdev, int bar)
3217{
Tejun Heo9ac78492007-01-20 16:00:26 +09003218 struct pci_devres *dr;
3219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 if (pci_resource_len(pdev, bar) == 0)
3221 return;
3222 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
3223 release_region(pci_resource_start(pdev, bar),
3224 pci_resource_len(pdev, bar));
3225 else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
3226 release_mem_region(pci_resource_start(pdev, bar),
3227 pci_resource_len(pdev, bar));
Tejun Heo9ac78492007-01-20 16:00:26 +09003228
3229 dr = find_pci_dr(pdev);
3230 if (dr)
3231 dr->region_mask &= ~(1 << bar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003233EXPORT_SYMBOL(pci_release_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
3235/**
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08003236 * __pci_request_region - Reserved PCI I/O and memory resource
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 * @pdev: PCI device whose resources are to be reserved
3238 * @bar: BAR to be reserved
3239 * @res_name: Name to be associated with resource.
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08003240 * @exclusive: whether the region access is exclusive or not
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 *
3242 * Mark the PCI region associated with PCI device @pdev BR @bar as
3243 * being reserved by owner @res_name. Do not access any
3244 * address inside the PCI regions unless this call returns
3245 * successfully.
3246 *
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08003247 * If @exclusive is set, then the region is marked so that userspace
3248 * is explicitly not allowed to map the resource via /dev/mem or
Bjorn Helgaasf7625982013-11-14 11:28:18 -07003249 * sysfs MMIO access.
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08003250 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 * Returns 0 on success, or %EBUSY on error. A warning
3252 * message is also printed on failure.
3253 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003254static int __pci_request_region(struct pci_dev *pdev, int bar,
3255 const char *res_name, int exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256{
Tejun Heo9ac78492007-01-20 16:00:26 +09003257 struct pci_devres *dr;
3258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 if (pci_resource_len(pdev, bar) == 0)
3260 return 0;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07003261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
3263 if (!request_region(pci_resource_start(pdev, bar),
3264 pci_resource_len(pdev, bar), res_name))
3265 goto err_out;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003266 } else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
Arjan van de Vene8de1482008-10-22 19:55:31 -07003267 if (!__request_mem_region(pci_resource_start(pdev, bar),
3268 pci_resource_len(pdev, bar), res_name,
3269 exclusive))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 goto err_out;
3271 }
Tejun Heo9ac78492007-01-20 16:00:26 +09003272
3273 dr = find_pci_dr(pdev);
3274 if (dr)
3275 dr->region_mask |= 1 << bar;
3276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 return 0;
3278
3279err_out:
Frederick Lawler7506dc72018-01-18 12:55:24 -06003280 pci_warn(pdev, "BAR %d: can't reserve %pR\n", bar,
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +11003281 &pdev->resource[bar]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 return -EBUSY;
3283}
3284
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003285/**
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08003286 * pci_request_region - Reserve PCI I/O and memory resource
Arjan van de Vene8de1482008-10-22 19:55:31 -07003287 * @pdev: PCI device whose resources are to be reserved
3288 * @bar: BAR to be reserved
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08003289 * @res_name: Name to be associated with resource
Arjan van de Vene8de1482008-10-22 19:55:31 -07003290 *
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08003291 * Mark the PCI region associated with PCI device @pdev BAR @bar as
Arjan van de Vene8de1482008-10-22 19:55:31 -07003292 * being reserved by owner @res_name. Do not access any
3293 * address inside the PCI regions unless this call returns
3294 * successfully.
3295 *
3296 * Returns 0 on success, or %EBUSY on error. A warning
3297 * message is also printed on failure.
3298 */
3299int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
3300{
3301 return __pci_request_region(pdev, bar, res_name, 0);
3302}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003303EXPORT_SYMBOL(pci_request_region);
Arjan van de Vene8de1482008-10-22 19:55:31 -07003304
3305/**
3306 * pci_request_region_exclusive - Reserved PCI I/O and memory resource
3307 * @pdev: PCI device whose resources are to be reserved
3308 * @bar: BAR to be reserved
3309 * @res_name: Name to be associated with resource.
3310 *
3311 * Mark the PCI region associated with PCI device @pdev BR @bar as
3312 * being reserved by owner @res_name. Do not access any
3313 * address inside the PCI regions unless this call returns
3314 * successfully.
3315 *
3316 * Returns 0 on success, or %EBUSY on error. A warning
3317 * message is also printed on failure.
3318 *
3319 * The key difference that _exclusive makes it that userspace is
3320 * explicitly not allowed to map the resource via /dev/mem or
Bjorn Helgaasf7625982013-11-14 11:28:18 -07003321 * sysfs.
Arjan van de Vene8de1482008-10-22 19:55:31 -07003322 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003323int pci_request_region_exclusive(struct pci_dev *pdev, int bar,
3324 const char *res_name)
Arjan van de Vene8de1482008-10-22 19:55:31 -07003325{
3326 return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
3327}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003328EXPORT_SYMBOL(pci_request_region_exclusive);
3329
Arjan van de Vene8de1482008-10-22 19:55:31 -07003330/**
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003331 * pci_release_selected_regions - Release selected PCI I/O and memory resources
3332 * @pdev: PCI device whose resources were previously reserved
3333 * @bars: Bitmask of BARs to be released
3334 *
3335 * Release selected PCI I/O and memory resources previously reserved.
3336 * Call this function only after all use of the PCI regions has ceased.
3337 */
3338void pci_release_selected_regions(struct pci_dev *pdev, int bars)
3339{
3340 int i;
3341
3342 for (i = 0; i < 6; i++)
3343 if (bars & (1 << i))
3344 pci_release_region(pdev, i);
3345}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003346EXPORT_SYMBOL(pci_release_selected_regions);
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003347
Bjorn Helgaas9738abe2013-04-12 11:20:03 -06003348static int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003349 const char *res_name, int excl)
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003350{
3351 int i;
3352
3353 for (i = 0; i < 6; i++)
3354 if (bars & (1 << i))
Arjan van de Vene8de1482008-10-22 19:55:31 -07003355 if (__pci_request_region(pdev, i, res_name, excl))
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003356 goto err_out;
3357 return 0;
3358
3359err_out:
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003360 while (--i >= 0)
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003361 if (bars & (1 << i))
3362 pci_release_region(pdev, i);
3363
3364 return -EBUSY;
3365}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Arjan van de Vene8de1482008-10-22 19:55:31 -07003367
3368/**
3369 * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
3370 * @pdev: PCI device whose resources are to be reserved
3371 * @bars: Bitmask of BARs to be requested
3372 * @res_name: Name to be associated with resource
3373 */
3374int pci_request_selected_regions(struct pci_dev *pdev, int bars,
3375 const char *res_name)
3376{
3377 return __pci_request_selected_regions(pdev, bars, res_name, 0);
3378}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003379EXPORT_SYMBOL(pci_request_selected_regions);
Arjan van de Vene8de1482008-10-22 19:55:31 -07003380
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003381int pci_request_selected_regions_exclusive(struct pci_dev *pdev, int bars,
3382 const char *res_name)
Arjan van de Vene8de1482008-10-22 19:55:31 -07003383{
3384 return __pci_request_selected_regions(pdev, bars, res_name,
3385 IORESOURCE_EXCLUSIVE);
3386}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003387EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
Arjan van de Vene8de1482008-10-22 19:55:31 -07003388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389/**
3390 * pci_release_regions - Release reserved PCI I/O and memory resources
3391 * @pdev: PCI device whose resources were previously reserved by pci_request_regions
3392 *
3393 * Releases all PCI I/O and memory resources previously reserved by a
3394 * successful call to pci_request_regions. Call this function only
3395 * after all use of the PCI regions has ceased.
3396 */
3397
3398void pci_release_regions(struct pci_dev *pdev)
3399{
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003400 pci_release_selected_regions(pdev, (1 << 6) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003402EXPORT_SYMBOL(pci_release_regions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
3404/**
3405 * pci_request_regions - Reserved PCI I/O and memory resources
3406 * @pdev: PCI device whose resources are to be reserved
3407 * @res_name: Name to be associated with resource.
3408 *
3409 * Mark all PCI regions associated with PCI device @pdev as
3410 * being reserved by owner @res_name. Do not access any
3411 * address inside the PCI regions unless this call returns
3412 * successfully.
3413 *
3414 * Returns 0 on success, or %EBUSY on error. A warning
3415 * message is also printed on failure.
3416 */
Jeff Garzik3c990e92006-03-04 21:52:42 -05003417int pci_request_regions(struct pci_dev *pdev, const char *res_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418{
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003419 return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003421EXPORT_SYMBOL(pci_request_regions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
3423/**
Arjan van de Vene8de1482008-10-22 19:55:31 -07003424 * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
3425 * @pdev: PCI device whose resources are to be reserved
3426 * @res_name: Name to be associated with resource.
3427 *
3428 * Mark all PCI regions associated with PCI device @pdev as
3429 * being reserved by owner @res_name. Do not access any
3430 * address inside the PCI regions unless this call returns
3431 * successfully.
3432 *
3433 * pci_request_regions_exclusive() will mark the region so that
Bjorn Helgaasf7625982013-11-14 11:28:18 -07003434 * /dev/mem and the sysfs MMIO access will not be allowed.
Arjan van de Vene8de1482008-10-22 19:55:31 -07003435 *
3436 * Returns 0 on success, or %EBUSY on error. A warning
3437 * message is also printed on failure.
3438 */
3439int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
3440{
3441 return pci_request_selected_regions_exclusive(pdev,
3442 ((1 << 6) - 1), res_name);
3443}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003444EXPORT_SYMBOL(pci_request_regions_exclusive);
Arjan van de Vene8de1482008-10-22 19:55:31 -07003445
Tomasz Nowickic5076cf2016-05-11 17:34:51 -05003446#ifdef PCI_IOBASE
3447struct io_range {
3448 struct list_head list;
3449 phys_addr_t start;
3450 resource_size_t size;
3451};
3452
3453static LIST_HEAD(io_range_list);
3454static DEFINE_SPINLOCK(io_range_lock);
3455#endif
3456
3457/*
3458 * Record the PCI IO range (expressed as CPU physical address + size).
3459 * Return a negative value if an error has occured, zero otherwise
3460 */
3461int __weak pci_register_io_range(phys_addr_t addr, resource_size_t size)
3462{
3463 int err = 0;
3464
3465#ifdef PCI_IOBASE
3466 struct io_range *range;
3467 resource_size_t allocated_size = 0;
3468
3469 /* check if the range hasn't been previously recorded */
3470 spin_lock(&io_range_lock);
3471 list_for_each_entry(range, &io_range_list, list) {
3472 if (addr >= range->start && addr + size <= range->start + size) {
3473 /* range already registered, bail out */
3474 goto end_register;
3475 }
3476 allocated_size += range->size;
3477 }
3478
3479 /* range not registed yet, check for available space */
3480 if (allocated_size + size - 1 > IO_SPACE_LIMIT) {
3481 /* if it's too big check if 64K space can be reserved */
3482 if (allocated_size + SZ_64K - 1 > IO_SPACE_LIMIT) {
3483 err = -E2BIG;
3484 goto end_register;
3485 }
3486
3487 size = SZ_64K;
3488 pr_warn("Requested IO range too big, new size set to 64K\n");
3489 }
3490
3491 /* add the range to the list */
3492 range = kzalloc(sizeof(*range), GFP_ATOMIC);
3493 if (!range) {
3494 err = -ENOMEM;
3495 goto end_register;
3496 }
3497
3498 range->start = addr;
3499 range->size = size;
3500
3501 list_add_tail(&range->list, &io_range_list);
3502
3503end_register:
3504 spin_unlock(&io_range_lock);
3505#endif
3506
3507 return err;
3508}
3509
3510phys_addr_t pci_pio_to_address(unsigned long pio)
3511{
3512 phys_addr_t address = (phys_addr_t)OF_BAD_ADDR;
3513
3514#ifdef PCI_IOBASE
3515 struct io_range *range;
3516 resource_size_t allocated_size = 0;
3517
3518 if (pio > IO_SPACE_LIMIT)
3519 return address;
3520
3521 spin_lock(&io_range_lock);
3522 list_for_each_entry(range, &io_range_list, list) {
3523 if (pio >= allocated_size && pio < allocated_size + range->size) {
3524 address = range->start + pio - allocated_size;
3525 break;
3526 }
3527 allocated_size += range->size;
3528 }
3529 spin_unlock(&io_range_lock);
3530#endif
3531
3532 return address;
3533}
3534
3535unsigned long __weak pci_address_to_pio(phys_addr_t address)
3536{
3537#ifdef PCI_IOBASE
3538 struct io_range *res;
3539 resource_size_t offset = 0;
3540 unsigned long addr = -1;
3541
3542 spin_lock(&io_range_lock);
3543 list_for_each_entry(res, &io_range_list, list) {
3544 if (address >= res->start && address < res->start + res->size) {
3545 addr = address - res->start + offset;
3546 break;
3547 }
3548 offset += res->size;
3549 }
3550 spin_unlock(&io_range_lock);
3551
3552 return addr;
3553#else
3554 if (address > IO_SPACE_LIMIT)
3555 return (unsigned long)-1;
3556
3557 return (unsigned long) address;
3558#endif
3559}
3560
Liviu Dudau8b921ac2014-09-29 15:29:30 +01003561/**
3562 * pci_remap_iospace - Remap the memory mapped I/O space
3563 * @res: Resource describing the I/O space
3564 * @phys_addr: physical address of range to be mapped
3565 *
3566 * Remap the memory mapped I/O space described by the @res
3567 * and the CPU physical address @phys_addr into virtual address space.
3568 * Only architectures that have memory mapped IO functions defined
3569 * (and the PCI_IOBASE value defined) should call this function.
3570 */
Lorenzo Pieralisi7b309ae2017-04-19 17:48:50 +01003571int pci_remap_iospace(const struct resource *res, phys_addr_t phys_addr)
Liviu Dudau8b921ac2014-09-29 15:29:30 +01003572{
3573#if defined(PCI_IOBASE) && defined(CONFIG_MMU)
3574 unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start;
3575
3576 if (!(res->flags & IORESOURCE_IO))
3577 return -EINVAL;
3578
3579 if (res->end > IO_SPACE_LIMIT)
3580 return -EINVAL;
3581
3582 return ioremap_page_range(vaddr, vaddr + resource_size(res), phys_addr,
3583 pgprot_device(PAGE_KERNEL));
3584#else
3585 /* this architecture does not have memory mapped I/O space,
3586 so this function should never be called */
3587 WARN_ONCE(1, "This architecture does not support memory mapped I/O\n");
3588 return -ENODEV;
3589#endif
3590}
Brian Norrisf90b0872017-03-09 18:46:16 -08003591EXPORT_SYMBOL(pci_remap_iospace);
Liviu Dudau8b921ac2014-09-29 15:29:30 +01003592
Sinan Kaya4d3f1382016-06-10 21:55:11 +02003593/**
3594 * pci_unmap_iospace - Unmap the memory mapped I/O space
3595 * @res: resource to be unmapped
3596 *
3597 * Unmap the CPU virtual address @res from virtual address space.
3598 * Only architectures that have memory mapped IO functions defined
3599 * (and the PCI_IOBASE value defined) should call this function.
3600 */
3601void pci_unmap_iospace(struct resource *res)
3602{
3603#if defined(PCI_IOBASE) && defined(CONFIG_MMU)
3604 unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start;
3605
3606 unmap_kernel_range(vaddr, resource_size(res));
3607#endif
3608}
Brian Norrisf90b0872017-03-09 18:46:16 -08003609EXPORT_SYMBOL(pci_unmap_iospace);
Sinan Kaya4d3f1382016-06-10 21:55:11 +02003610
Lorenzo Pieralisi490cb6d2017-04-19 17:48:55 +01003611/**
3612 * devm_pci_remap_cfgspace - Managed pci_remap_cfgspace()
3613 * @dev: Generic device to remap IO address for
3614 * @offset: Resource address to map
3615 * @size: Size of map
3616 *
3617 * Managed pci_remap_cfgspace(). Map is automatically unmapped on driver
3618 * detach.
3619 */
3620void __iomem *devm_pci_remap_cfgspace(struct device *dev,
3621 resource_size_t offset,
3622 resource_size_t size)
3623{
3624 void __iomem **ptr, *addr;
3625
3626 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
3627 if (!ptr)
3628 return NULL;
3629
3630 addr = pci_remap_cfgspace(offset, size);
3631 if (addr) {
3632 *ptr = addr;
3633 devres_add(dev, ptr);
3634 } else
3635 devres_free(ptr);
3636
3637 return addr;
3638}
3639EXPORT_SYMBOL(devm_pci_remap_cfgspace);
3640
3641/**
3642 * devm_pci_remap_cfg_resource - check, request region and ioremap cfg resource
3643 * @dev: generic device to handle the resource for
3644 * @res: configuration space resource to be handled
3645 *
3646 * Checks that a resource is a valid memory region, requests the memory
3647 * region and ioremaps with pci_remap_cfgspace() API that ensures the
3648 * proper PCI configuration space memory attributes are guaranteed.
3649 *
3650 * All operations are managed and will be undone on driver detach.
3651 *
3652 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
Randy Dunlap505fb742017-10-29 17:07:11 -07003653 * on failure. Usage example::
Lorenzo Pieralisi490cb6d2017-04-19 17:48:55 +01003654 *
3655 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3656 * base = devm_pci_remap_cfg_resource(&pdev->dev, res);
3657 * if (IS_ERR(base))
3658 * return PTR_ERR(base);
3659 */
3660void __iomem *devm_pci_remap_cfg_resource(struct device *dev,
3661 struct resource *res)
3662{
3663 resource_size_t size;
3664 const char *name;
3665 void __iomem *dest_ptr;
3666
3667 BUG_ON(!dev);
3668
3669 if (!res || resource_type(res) != IORESOURCE_MEM) {
3670 dev_err(dev, "invalid resource\n");
3671 return IOMEM_ERR_PTR(-EINVAL);
3672 }
3673
3674 size = resource_size(res);
3675 name = res->name ?: dev_name(dev);
3676
3677 if (!devm_request_mem_region(dev, res->start, size, name)) {
3678 dev_err(dev, "can't request region for resource %pR\n", res);
3679 return IOMEM_ERR_PTR(-EBUSY);
3680 }
3681
3682 dest_ptr = devm_pci_remap_cfgspace(dev, res->start, size);
3683 if (!dest_ptr) {
3684 dev_err(dev, "ioremap failed for resource %pR\n", res);
3685 devm_release_mem_region(dev, res->start, size);
3686 dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
3687 }
3688
3689 return dest_ptr;
3690}
3691EXPORT_SYMBOL(devm_pci_remap_cfg_resource);
3692
Ben Hutchings6a479072008-12-23 03:08:29 +00003693static void __pci_set_master(struct pci_dev *dev, bool enable)
3694{
3695 u16 old_cmd, cmd;
3696
3697 pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
3698 if (enable)
3699 cmd = old_cmd | PCI_COMMAND_MASTER;
3700 else
3701 cmd = old_cmd & ~PCI_COMMAND_MASTER;
3702 if (cmd != old_cmd) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06003703 pci_dbg(dev, "%s bus mastering\n",
Ben Hutchings6a479072008-12-23 03:08:29 +00003704 enable ? "enabling" : "disabling");
3705 pci_write_config_word(dev, PCI_COMMAND, cmd);
3706 }
3707 dev->is_busmaster = enable;
3708}
Arjan van de Vene8de1482008-10-22 19:55:31 -07003709
3710/**
Myron Stowe2b6f2c32012-06-25 21:30:57 -06003711 * pcibios_setup - process "pci=" kernel boot arguments
3712 * @str: string used to pass in "pci=" kernel boot arguments
3713 *
3714 * Process kernel boot arguments. This is the default implementation.
3715 * Architecture specific implementations can override this as necessary.
3716 */
3717char * __weak __init pcibios_setup(char *str)
3718{
3719 return str;
3720}
3721
3722/**
Myron Stowe96c55902011-10-28 15:48:38 -06003723 * pcibios_set_master - enable PCI bus-mastering for device dev
3724 * @dev: the PCI device to enable
3725 *
3726 * Enables PCI bus-mastering for the device. This is the default
3727 * implementation. Architecture specific implementations can override
3728 * this if necessary.
3729 */
3730void __weak pcibios_set_master(struct pci_dev *dev)
3731{
3732 u8 lat;
3733
Myron Stowef6766782011-10-28 15:49:20 -06003734 /* The latency timer doesn't apply to PCIe (either Type 0 or Type 1) */
3735 if (pci_is_pcie(dev))
3736 return;
3737
Myron Stowe96c55902011-10-28 15:48:38 -06003738 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3739 if (lat < 16)
3740 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
3741 else if (lat > pcibios_max_latency)
3742 lat = pcibios_max_latency;
3743 else
3744 return;
Bjorn Helgaasa0064822013-09-23 15:25:26 -06003745
Myron Stowe96c55902011-10-28 15:48:38 -06003746 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
3747}
3748
3749/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 * pci_set_master - enables bus-mastering for device dev
3751 * @dev: the PCI device to enable
3752 *
3753 * Enables bus-mastering on the device and calls pcibios_set_master()
3754 * to do the needed arch specific settings.
3755 */
Ben Hutchings6a479072008-12-23 03:08:29 +00003756void pci_set_master(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
Ben Hutchings6a479072008-12-23 03:08:29 +00003758 __pci_set_master(dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 pcibios_set_master(dev);
3760}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003761EXPORT_SYMBOL(pci_set_master);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
Ben Hutchings6a479072008-12-23 03:08:29 +00003763/**
3764 * pci_clear_master - disables bus-mastering for device dev
3765 * @dev: the PCI device to disable
3766 */
3767void pci_clear_master(struct pci_dev *dev)
3768{
3769 __pci_set_master(dev, false);
3770}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003771EXPORT_SYMBOL(pci_clear_master);
Ben Hutchings6a479072008-12-23 03:08:29 +00003772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773/**
Matthew Wilcoxedb2d972006-10-10 08:01:21 -06003774 * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
3775 * @dev: the PCI device for which MWI is to be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 *
Matthew Wilcoxedb2d972006-10-10 08:01:21 -06003777 * Helper function for pci_set_mwi.
3778 * Originally copied from drivers/net/acenic.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
3780 *
3781 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
3782 */
Tejun Heo15ea76d2009-09-22 17:34:48 +09003783int pci_set_cacheline_size(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
3785 u8 cacheline_size;
3786
3787 if (!pci_cache_line_size)
Tejun Heo15ea76d2009-09-22 17:34:48 +09003788 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
3790 /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
3791 equal to or multiple of the right value. */
3792 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
3793 if (cacheline_size >= pci_cache_line_size &&
3794 (cacheline_size % pci_cache_line_size) == 0)
3795 return 0;
3796
3797 /* Write the correct value. */
3798 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
3799 /* Read it back. */
3800 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
3801 if (cacheline_size == pci_cache_line_size)
3802 return 0;
3803
Frederick Lawler7506dc72018-01-18 12:55:24 -06003804 pci_printk(KERN_DEBUG, dev, "cache line size of %d is not supported\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -04003805 pci_cache_line_size << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
3807 return -EINVAL;
3808}
Tejun Heo15ea76d2009-09-22 17:34:48 +09003809EXPORT_SYMBOL_GPL(pci_set_cacheline_size);
3810
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811/**
3812 * pci_set_mwi - enables memory-write-invalidate PCI transaction
3813 * @dev: the PCI device for which MWI is enabled
3814 *
Randy Dunlap694625c2007-07-09 11:55:54 -07003815 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 *
3817 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
3818 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003819int pci_set_mwi(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820{
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003821#ifdef PCI_DISABLE_MWI
3822 return 0;
3823#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 int rc;
3825 u16 cmd;
3826
Matthew Wilcoxedb2d972006-10-10 08:01:21 -06003827 rc = pci_set_cacheline_size(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 if (rc)
3829 return rc;
3830
3831 pci_read_config_word(dev, PCI_COMMAND, &cmd);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003832 if (!(cmd & PCI_COMMAND_INVALIDATE)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06003833 pci_dbg(dev, "enabling Mem-Wr-Inval\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 cmd |= PCI_COMMAND_INVALIDATE;
3835 pci_write_config_word(dev, PCI_COMMAND, cmd);
3836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 return 0;
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003838#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003840EXPORT_SYMBOL(pci_set_mwi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
3842/**
Heiner Kallweitfc0f9f42017-12-12 07:40:56 +01003843 * pcim_set_mwi - a device-managed pci_set_mwi()
3844 * @dev: the PCI device for which MWI is enabled
3845 *
3846 * Managed pci_set_mwi().
3847 *
3848 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
3849 */
3850int pcim_set_mwi(struct pci_dev *dev)
3851{
3852 struct pci_devres *dr;
3853
3854 dr = find_pci_dr(dev);
3855 if (!dr)
3856 return -ENOMEM;
3857
3858 dr->mwi = 1;
3859 return pci_set_mwi(dev);
3860}
3861EXPORT_SYMBOL(pcim_set_mwi);
3862
3863/**
Randy Dunlap694625c2007-07-09 11:55:54 -07003864 * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
3865 * @dev: the PCI device for which MWI is enabled
3866 *
3867 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
3868 * Callers are not required to check the return value.
3869 *
3870 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
3871 */
3872int pci_try_set_mwi(struct pci_dev *dev)
3873{
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003874#ifdef PCI_DISABLE_MWI
3875 return 0;
3876#else
3877 return pci_set_mwi(dev);
3878#endif
Randy Dunlap694625c2007-07-09 11:55:54 -07003879}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003880EXPORT_SYMBOL(pci_try_set_mwi);
Randy Dunlap694625c2007-07-09 11:55:54 -07003881
3882/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
3884 * @dev: the PCI device to disable
3885 *
3886 * Disables PCI Memory-Write-Invalidate transaction on the device
3887 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003888void pci_clear_mwi(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889{
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003890#ifndef PCI_DISABLE_MWI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 u16 cmd;
3892
3893 pci_read_config_word(dev, PCI_COMMAND, &cmd);
3894 if (cmd & PCI_COMMAND_INVALIDATE) {
3895 cmd &= ~PCI_COMMAND_INVALIDATE;
3896 pci_write_config_word(dev, PCI_COMMAND, cmd);
3897 }
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003898#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003900EXPORT_SYMBOL(pci_clear_mwi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
Brett M Russa04ce0f2005-08-15 15:23:41 -04003902/**
3903 * pci_intx - enables/disables PCI INTx for device dev
Randy Dunlap8f7020d2005-10-23 11:57:38 -07003904 * @pdev: the PCI device to operate on
3905 * @enable: boolean: whether to enable or disable PCI INTx
Brett M Russa04ce0f2005-08-15 15:23:41 -04003906 *
3907 * Enables/disables PCI INTx for device dev
3908 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003909void pci_intx(struct pci_dev *pdev, int enable)
Brett M Russa04ce0f2005-08-15 15:23:41 -04003910{
3911 u16 pci_command, new;
3912
3913 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
3914
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003915 if (enable)
Brett M Russa04ce0f2005-08-15 15:23:41 -04003916 new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04003917 else
Brett M Russa04ce0f2005-08-15 15:23:41 -04003918 new = pci_command | PCI_COMMAND_INTX_DISABLE;
Brett M Russa04ce0f2005-08-15 15:23:41 -04003919
3920 if (new != pci_command) {
Tejun Heo9ac78492007-01-20 16:00:26 +09003921 struct pci_devres *dr;
3922
Brett M Russ2fd9d742005-09-09 10:02:22 -07003923 pci_write_config_word(pdev, PCI_COMMAND, new);
Tejun Heo9ac78492007-01-20 16:00:26 +09003924
3925 dr = find_pci_dr(pdev);
3926 if (dr && !dr->restore_intx) {
3927 dr->restore_intx = 1;
3928 dr->orig_intx = !enable;
3929 }
Brett M Russa04ce0f2005-08-15 15:23:41 -04003930 }
3931}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06003932EXPORT_SYMBOL_GPL(pci_intx);
Brett M Russa04ce0f2005-08-15 15:23:41 -04003933
Jan Kiszkaa2e27782011-11-04 09:46:00 +01003934static bool pci_check_and_set_intx_mask(struct pci_dev *dev, bool mask)
3935{
3936 struct pci_bus *bus = dev->bus;
3937 bool mask_updated = true;
3938 u32 cmd_status_dword;
3939 u16 origcmd, newcmd;
3940 unsigned long flags;
3941 bool irq_pending;
3942
3943 /*
3944 * We do a single dword read to retrieve both command and status.
3945 * Document assumptions that make this possible.
3946 */
3947 BUILD_BUG_ON(PCI_COMMAND % 4);
3948 BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
3949
3950 raw_spin_lock_irqsave(&pci_lock, flags);
3951
3952 bus->ops->read(bus, dev->devfn, PCI_COMMAND, 4, &cmd_status_dword);
3953
3954 irq_pending = (cmd_status_dword >> 16) & PCI_STATUS_INTERRUPT;
3955
3956 /*
3957 * Check interrupt status register to see whether our device
3958 * triggered the interrupt (when masking) or the next IRQ is
3959 * already pending (when unmasking).
3960 */
3961 if (mask != irq_pending) {
3962 mask_updated = false;
3963 goto done;
3964 }
3965
3966 origcmd = cmd_status_dword;
3967 newcmd = origcmd & ~PCI_COMMAND_INTX_DISABLE;
3968 if (mask)
3969 newcmd |= PCI_COMMAND_INTX_DISABLE;
3970 if (newcmd != origcmd)
3971 bus->ops->write(bus, dev->devfn, PCI_COMMAND, 2, newcmd);
3972
3973done:
3974 raw_spin_unlock_irqrestore(&pci_lock, flags);
3975
3976 return mask_updated;
3977}
3978
3979/**
3980 * pci_check_and_mask_intx - mask INTx on pending interrupt
Randy Dunlap6e9292c2012-01-21 11:02:35 -08003981 * @dev: the PCI device to operate on
Jan Kiszkaa2e27782011-11-04 09:46:00 +01003982 *
3983 * Check if the device dev has its INTx line asserted, mask it and
Piotr Gregor99b3c582017-05-26 22:02:25 +01003984 * return true in that case. False is returned if no interrupt was
Jan Kiszkaa2e27782011-11-04 09:46:00 +01003985 * pending.
3986 */
3987bool pci_check_and_mask_intx(struct pci_dev *dev)
3988{
3989 return pci_check_and_set_intx_mask(dev, true);
3990}
3991EXPORT_SYMBOL_GPL(pci_check_and_mask_intx);
3992
3993/**
Bjorn Helgaasebd50b92014-01-14 17:10:39 -07003994 * pci_check_and_unmask_intx - unmask INTx if no interrupt is pending
Randy Dunlap6e9292c2012-01-21 11:02:35 -08003995 * @dev: the PCI device to operate on
Jan Kiszkaa2e27782011-11-04 09:46:00 +01003996 *
3997 * Check if the device dev has its INTx line asserted, unmask it if not
3998 * and return true. False is returned and the mask remains active if
3999 * there was still an interrupt pending.
4000 */
4001bool pci_check_and_unmask_intx(struct pci_dev *dev)
4002{
4003 return pci_check_and_set_intx_mask(dev, false);
4004}
4005EXPORT_SYMBOL_GPL(pci_check_and_unmask_intx);
4006
Casey Leedom3775a202013-08-06 15:48:36 +05304007/**
4008 * pci_wait_for_pending_transaction - waits for pending transaction
4009 * @dev: the PCI device to operate on
4010 *
4011 * Return 0 if transaction is pending 1 otherwise.
4012 */
4013int pci_wait_for_pending_transaction(struct pci_dev *dev)
Sheng Yang8dd7f802008-10-21 17:38:25 +08004014{
Alex Williamson157e8762013-12-17 16:43:39 -07004015 if (!pci_is_pcie(dev))
4016 return 1;
Sheng Yang8dd7f802008-10-21 17:38:25 +08004017
Gavin Shand0b4cc42014-05-19 13:06:46 +10004018 return pci_wait_for_pending(dev, pci_pcie_cap(dev) + PCI_EXP_DEVSTA,
4019 PCI_EXP_DEVSTA_TRPND);
Casey Leedom3775a202013-08-06 15:48:36 +05304020}
4021EXPORT_SYMBOL(pci_wait_for_pending_transaction);
Sheng Yang5fe5db02009-02-09 14:53:47 +08004022
Sinan Kayaa2758b62018-02-27 14:14:10 -06004023static int pci_dev_wait(struct pci_dev *dev, char *reset_type, int timeout)
Alex Williamson5adecf82016-02-22 13:05:48 -07004024{
Sinan Kayaa2758b62018-02-27 14:14:10 -06004025 int delay = 1;
Alex Williamson5adecf82016-02-22 13:05:48 -07004026 u32 id;
4027
Sinan Kaya821cdad2017-08-29 14:45:45 -05004028 /*
Sinan Kayaa2758b62018-02-27 14:14:10 -06004029 * After reset, the device should not silently discard config
Sinan Kaya821cdad2017-08-29 14:45:45 -05004030 * requests, but it may still indicate that it needs more time by
4031 * responding to them with CRS completions. The Root Port will
4032 * generally synthesize ~0 data to complete the read (except when
4033 * CRS SV is enabled and the read was for the Vendor ID; in that
4034 * case it synthesizes 0x0001 data).
4035 *
4036 * Wait for the device to return a non-CRS completion. Read the
4037 * Command register instead of Vendor ID so we don't have to
4038 * contend with the CRS SV value.
4039 */
4040 pci_read_config_dword(dev, PCI_COMMAND, &id);
4041 while (id == ~0) {
4042 if (delay > timeout) {
Sinan Kayaa2758b62018-02-27 14:14:10 -06004043 pci_warn(dev, "not ready %dms after %s; giving up\n",
4044 delay - 1, reset_type);
Sinan Kaya91295d72018-02-27 14:14:08 -06004045 return -ENOTTY;
Sinan Kaya821cdad2017-08-29 14:45:45 -05004046 }
4047
4048 if (delay > 1000)
Sinan Kayaa2758b62018-02-27 14:14:10 -06004049 pci_info(dev, "not ready %dms after %s; waiting\n",
4050 delay - 1, reset_type);
Sinan Kaya821cdad2017-08-29 14:45:45 -05004051
4052 msleep(delay);
4053 delay *= 2;
4054 pci_read_config_dword(dev, PCI_COMMAND, &id);
4055 }
4056
4057 if (delay > 1000)
Sinan Kayaa2758b62018-02-27 14:14:10 -06004058 pci_info(dev, "ready %dms after %s\n", delay - 1,
4059 reset_type);
Sinan Kaya91295d72018-02-27 14:14:08 -06004060
4061 return 0;
Alex Williamson5adecf82016-02-22 13:05:48 -07004062}
4063
Christoph Hellwiga60a2b72017-04-14 21:11:25 +02004064/**
4065 * pcie_has_flr - check if a device supports function level resets
4066 * @dev: device to check
4067 *
4068 * Returns true if the device advertises support for PCIe function level
4069 * resets.
4070 */
4071static bool pcie_has_flr(struct pci_dev *dev)
Casey Leedom3775a202013-08-06 15:48:36 +05304072{
4073 u32 cap;
4074
Sasha Neftinf65fd1a2017-04-03 16:02:50 -05004075 if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET)
Christoph Hellwiga60a2b72017-04-14 21:11:25 +02004076 return false;
Sasha Neftinf65fd1a2017-04-03 16:02:50 -05004077
Casey Leedom3775a202013-08-06 15:48:36 +05304078 pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
Christoph Hellwiga60a2b72017-04-14 21:11:25 +02004079 return cap & PCI_EXP_DEVCAP_FLR;
4080}
Casey Leedom3775a202013-08-06 15:48:36 +05304081
Christoph Hellwiga60a2b72017-04-14 21:11:25 +02004082/**
4083 * pcie_flr - initiate a PCIe function level reset
4084 * @dev: device to reset
4085 *
4086 * Initiate a function level reset on @dev. The caller should ensure the
4087 * device supports FLR before calling this function, e.g. by using the
4088 * pcie_has_flr() helper.
4089 */
Sinan Kaya91295d72018-02-27 14:14:08 -06004090int pcie_flr(struct pci_dev *dev)
Christoph Hellwiga60a2b72017-04-14 21:11:25 +02004091{
Casey Leedom3775a202013-08-06 15:48:36 +05304092 if (!pci_wait_for_pending_transaction(dev))
Frederick Lawler7506dc72018-01-18 12:55:24 -06004093 pci_err(dev, "timed out waiting for pending transaction; performing function level reset anyway\n");
Casey Leedom3775a202013-08-06 15:48:36 +05304094
Jiang Liu59875ae2012-07-24 17:20:06 +08004095 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
Sinan Kayaa2758b62018-02-27 14:14:10 -06004096
4097 /*
4098 * Per PCIe r4.0, sec 6.6.2, a device must complete an FLR within
4099 * 100ms, but may silently discard requests while the FLR is in
4100 * progress. Wait 100ms before trying to access the device.
4101 */
4102 msleep(100);
4103
4104 return pci_dev_wait(dev, "FLR", PCIE_RESET_READY_POLL_MS);
Sheng Yang8dd7f802008-10-21 17:38:25 +08004105}
Christoph Hellwiga60a2b72017-04-14 21:11:25 +02004106EXPORT_SYMBOL_GPL(pcie_flr);
Sheng Yangd91cdc72008-11-11 17:17:47 +08004107
Yu Zhao8c1c6992009-06-13 15:52:13 +08004108static int pci_af_flr(struct pci_dev *dev, int probe)
Sheng Yang1ca88792008-11-11 17:17:48 +08004109{
Yu Zhao8c1c6992009-06-13 15:52:13 +08004110 int pos;
Sheng Yang1ca88792008-11-11 17:17:48 +08004111 u8 cap;
4112
Yu Zhao8c1c6992009-06-13 15:52:13 +08004113 pos = pci_find_capability(dev, PCI_CAP_ID_AF);
4114 if (!pos)
Sheng Yang1ca88792008-11-11 17:17:48 +08004115 return -ENOTTY;
Yu Zhao8c1c6992009-06-13 15:52:13 +08004116
Sasha Neftinf65fd1a2017-04-03 16:02:50 -05004117 if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET)
4118 return -ENOTTY;
4119
Yu Zhao8c1c6992009-06-13 15:52:13 +08004120 pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap);
Sheng Yang1ca88792008-11-11 17:17:48 +08004121 if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
4122 return -ENOTTY;
4123
4124 if (probe)
4125 return 0;
4126
Alex Williamsond066c942014-06-17 15:40:13 -06004127 /*
4128 * Wait for Transaction Pending bit to clear. A word-aligned test
4129 * is used, so we use the conrol offset rather than status and shift
4130 * the test bit to match.
4131 */
Gavin Shanbb383e22014-11-12 13:41:51 +11004132 if (!pci_wait_for_pending(dev, pos + PCI_AF_CTRL,
Alex Williamsond066c942014-06-17 15:40:13 -06004133 PCI_AF_STATUS_TP << 8))
Frederick Lawler7506dc72018-01-18 12:55:24 -06004134 pci_err(dev, "timed out waiting for pending transaction; performing AF function level reset anyway\n");
Yu Zhao8c1c6992009-06-13 15:52:13 +08004135
Yu Zhao8c1c6992009-06-13 15:52:13 +08004136 pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
Sinan Kayaa2758b62018-02-27 14:14:10 -06004137
4138 /*
4139 * Per Advanced Capabilities for Conventional PCI ECN, 13 April 2006,
4140 * updated 27 July 2006; a device must complete an FLR within
4141 * 100ms, but may silently discard requests while the FLR is in
4142 * progress. Wait 100ms before trying to access the device.
4143 */
4144 msleep(100);
4145
4146 return pci_dev_wait(dev, "AF_FLR", PCIE_RESET_READY_POLL_MS);
Sheng Yang1ca88792008-11-11 17:17:48 +08004147}
4148
Rafael J. Wysocki83d74e02011-03-05 21:48:44 +01004149/**
4150 * pci_pm_reset - Put device into PCI_D3 and back into PCI_D0.
4151 * @dev: Device to reset.
4152 * @probe: If set, only check if the device can be reset this way.
4153 *
4154 * If @dev supports native PCI PM and its PCI_PM_CTRL_NO_SOFT_RESET flag is
4155 * unset, it will be reinitialized internally when going from PCI_D3hot to
4156 * PCI_D0. If that's the case and the device is not in a low-power state
4157 * already, force it into PCI_D3hot and back to PCI_D0, causing it to be reset.
4158 *
4159 * NOTE: This causes the caller to sleep for twice the device power transition
4160 * cooldown period, which for the D0->D3hot and D3hot->D0 transitions is 10 ms
Bjorn Helgaasf7625982013-11-14 11:28:18 -07004161 * by default (i.e. unless the @dev's d3_delay field has a different value).
Rafael J. Wysocki83d74e02011-03-05 21:48:44 +01004162 * Moreover, only devices in D0 can be reset by this function.
4163 */
Yu Zhaof85876b2009-06-13 15:52:14 +08004164static int pci_pm_reset(struct pci_dev *dev, int probe)
Sheng Yangd91cdc72008-11-11 17:17:47 +08004165{
Yu Zhaof85876b2009-06-13 15:52:14 +08004166 u16 csr;
Sheng Yangd91cdc72008-11-11 17:17:47 +08004167
Alex Williamson51e53732014-11-21 11:24:08 -07004168 if (!dev->pm_cap || dev->dev_flags & PCI_DEV_FLAGS_NO_PM_RESET)
Yu Zhaof85876b2009-06-13 15:52:14 +08004169 return -ENOTTY;
Sheng Yangd91cdc72008-11-11 17:17:47 +08004170
Yu Zhaof85876b2009-06-13 15:52:14 +08004171 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr);
4172 if (csr & PCI_PM_CTRL_NO_SOFT_RESET)
4173 return -ENOTTY;
Sheng Yang1ca88792008-11-11 17:17:48 +08004174
Yu Zhaof85876b2009-06-13 15:52:14 +08004175 if (probe)
4176 return 0;
4177
4178 if (dev->current_state != PCI_D0)
4179 return -EINVAL;
4180
4181 csr &= ~PCI_PM_CTRL_STATE_MASK;
4182 csr |= PCI_D3hot;
4183 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +01004184 pci_dev_d3_sleep(dev);
Yu Zhaof85876b2009-06-13 15:52:14 +08004185
4186 csr &= ~PCI_PM_CTRL_STATE_MASK;
4187 csr |= PCI_D0;
4188 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +01004189 pci_dev_d3_sleep(dev);
Yu Zhaof85876b2009-06-13 15:52:14 +08004190
Sinan Kayaabbcf0e2018-02-27 14:14:10 -06004191 return pci_dev_wait(dev, "PM D3->D0", PCIE_RESET_READY_POLL_MS);
Yu Zhaof85876b2009-06-13 15:52:14 +08004192}
4193
Gavin Shan9e330022014-06-19 17:22:44 +10004194void pci_reset_secondary_bus(struct pci_dev *dev)
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08004195{
4196 u16 ctrl;
Alex Williamson64e86742013-08-08 14:09:24 -06004197
4198 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &ctrl);
4199 ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
4200 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
Alex Williamsonde0c5482013-08-08 14:10:13 -06004201 /*
4202 * PCI spec v3.0 7.6.4.2 requires minimum Trst of 1ms. Double
Bjorn Helgaasf7625982013-11-14 11:28:18 -07004203 * this to 2ms to ensure that we meet the minimum requirement.
Alex Williamsonde0c5482013-08-08 14:10:13 -06004204 */
4205 msleep(2);
Alex Williamson64e86742013-08-08 14:09:24 -06004206
4207 ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
4208 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
Alex Williamsonde0c5482013-08-08 14:10:13 -06004209
4210 /*
4211 * Trhfa for conventional PCI is 2^25 clock cycles.
4212 * Assuming a minimum 33MHz clock this results in a 1s
4213 * delay before we can consider subordinate devices to
4214 * be re-initialized. PCIe has some ways to shorten this,
4215 * but we don't make use of them yet.
4216 */
4217 ssleep(1);
Alex Williamson64e86742013-08-08 14:09:24 -06004218}
Gavin Shand92a2082014-04-24 18:00:24 +10004219
Gavin Shan9e330022014-06-19 17:22:44 +10004220void __weak pcibios_reset_secondary_bus(struct pci_dev *dev)
4221{
4222 pci_reset_secondary_bus(dev);
4223}
4224
Gavin Shand92a2082014-04-24 18:00:24 +10004225/**
4226 * pci_reset_bridge_secondary_bus - Reset the secondary bus on a PCI bridge.
4227 * @dev: Bridge device
4228 *
4229 * Use the bridge control register to assert reset on the secondary bus.
4230 * Devices on the secondary bus are left in power-on state.
4231 */
Sinan Kaya01fd61c2018-02-27 14:14:11 -06004232int pci_reset_bridge_secondary_bus(struct pci_dev *dev)
Gavin Shand92a2082014-04-24 18:00:24 +10004233{
4234 pcibios_reset_secondary_bus(dev);
Sinan Kaya01fd61c2018-02-27 14:14:11 -06004235
Sinan Kaya6b2f13512018-02-27 14:14:12 -06004236 return pci_dev_wait(dev, "bus reset", PCIE_RESET_READY_POLL_MS);
Gavin Shand92a2082014-04-24 18:00:24 +10004237}
Alex Williamson64e86742013-08-08 14:09:24 -06004238EXPORT_SYMBOL_GPL(pci_reset_bridge_secondary_bus);
4239
4240static int pci_parent_bus_reset(struct pci_dev *dev, int probe)
4241{
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08004242 struct pci_dev *pdev;
4243
Alex Williamsonf331a852015-01-15 18:16:04 -06004244 if (pci_is_root_bus(dev->bus) || dev->subordinate ||
4245 !dev->bus->self || dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08004246 return -ENOTTY;
4247
4248 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
4249 if (pdev != dev)
4250 return -ENOTTY;
4251
4252 if (probe)
4253 return 0;
4254
Alex Williamson64e86742013-08-08 14:09:24 -06004255 pci_reset_bridge_secondary_bus(dev->bus->self);
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08004256
4257 return 0;
4258}
4259
Alex Williamson608c3882013-08-08 14:09:43 -06004260static int pci_reset_hotplug_slot(struct hotplug_slot *hotplug, int probe)
4261{
4262 int rc = -ENOTTY;
4263
4264 if (!hotplug || !try_module_get(hotplug->ops->owner))
4265 return rc;
4266
4267 if (hotplug->ops->reset_slot)
4268 rc = hotplug->ops->reset_slot(hotplug, probe);
4269
4270 module_put(hotplug->ops->owner);
4271
4272 return rc;
4273}
4274
4275static int pci_dev_reset_slot_function(struct pci_dev *dev, int probe)
4276{
4277 struct pci_dev *pdev;
4278
Alex Williamsonf331a852015-01-15 18:16:04 -06004279 if (dev->subordinate || !dev->slot ||
4280 dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)
Alex Williamson608c3882013-08-08 14:09:43 -06004281 return -ENOTTY;
4282
4283 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
4284 if (pdev != dev && pdev->slot == dev->slot)
4285 return -ENOTTY;
4286
4287 return pci_reset_hotplug_slot(dev->slot->hotplug, probe);
4288}
4289
Alex Williamson77cb9852013-08-08 14:09:49 -06004290static void pci_dev_lock(struct pci_dev *dev)
4291{
4292 pci_cfg_access_lock(dev);
4293 /* block PM suspend, driver probe, etc. */
4294 device_lock(&dev->dev);
4295}
4296
Alex Williamson61cf16d2013-12-16 15:14:31 -07004297/* Return 1 on successful lock, 0 on contention */
4298static int pci_dev_trylock(struct pci_dev *dev)
4299{
4300 if (pci_cfg_access_trylock(dev)) {
4301 if (device_trylock(&dev->dev))
4302 return 1;
4303 pci_cfg_access_unlock(dev);
4304 }
4305
4306 return 0;
4307}
4308
Alex Williamson77cb9852013-08-08 14:09:49 -06004309static void pci_dev_unlock(struct pci_dev *dev)
4310{
4311 device_unlock(&dev->dev);
4312 pci_cfg_access_unlock(dev);
4313}
4314
Christoph Hellwig775755e2017-06-01 13:10:38 +02004315static void pci_dev_save_and_disable(struct pci_dev *dev)
Keith Busch3ebe7f92014-05-02 10:40:42 -06004316{
4317 const struct pci_error_handlers *err_handler =
4318 dev->driver ? dev->driver->err_handler : NULL;
Keith Busch3ebe7f92014-05-02 10:40:42 -06004319
Christoph Hellwigb014e962017-06-01 13:10:37 +02004320 /*
Christoph Hellwig775755e2017-06-01 13:10:38 +02004321 * dev->driver->err_handler->reset_prepare() is protected against
Christoph Hellwigb014e962017-06-01 13:10:37 +02004322 * races with ->remove() by the device lock, which must be held by
4323 * the caller.
4324 */
Christoph Hellwig775755e2017-06-01 13:10:38 +02004325 if (err_handler && err_handler->reset_prepare)
4326 err_handler->reset_prepare(dev);
Keith Busch3ebe7f92014-05-02 10:40:42 -06004327
Alex Williamsona6cbaad2013-08-08 14:10:02 -06004328 /*
4329 * Wake-up device prior to save. PM registers default to D0 after
4330 * reset and a simple register restore doesn't reliably return
4331 * to a non-D0 state anyway.
4332 */
4333 pci_set_power_state(dev, PCI_D0);
4334
Alex Williamson77cb9852013-08-08 14:09:49 -06004335 pci_save_state(dev);
4336 /*
4337 * Disable the device by clearing the Command register, except for
4338 * INTx-disable which is set. This not only disables MMIO and I/O port
4339 * BARs, but also prevents the device from being Bus Master, preventing
4340 * DMA from the device including MSI/MSI-X interrupts. For PCI 2.3
4341 * compliant devices, INTx-disable prevents legacy interrupts.
4342 */
4343 pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
4344}
4345
4346static void pci_dev_restore(struct pci_dev *dev)
4347{
Christoph Hellwig775755e2017-06-01 13:10:38 +02004348 const struct pci_error_handlers *err_handler =
4349 dev->driver ? dev->driver->err_handler : NULL;
4350
Alex Williamson77cb9852013-08-08 14:09:49 -06004351 pci_restore_state(dev);
Alex Williamson77cb9852013-08-08 14:09:49 -06004352
Christoph Hellwig775755e2017-06-01 13:10:38 +02004353 /*
4354 * dev->driver->err_handler->reset_done() is protected against
4355 * races with ->remove() by the device lock, which must be held by
4356 * the caller.
4357 */
4358 if (err_handler && err_handler->reset_done)
4359 err_handler->reset_done(dev);
Sheng Yangd91cdc72008-11-11 17:17:47 +08004360}
Keith Busch3ebe7f92014-05-02 10:40:42 -06004361
Sheng Yangd91cdc72008-11-11 17:17:47 +08004362/**
Konrad Rzeszutek Wilk6fbf9e72012-01-12 12:06:46 -05004363 * __pci_reset_function_locked - reset a PCI device function while holding
4364 * the @dev mutex lock.
4365 * @dev: PCI device to reset
4366 *
4367 * Some devices allow an individual function to be reset without affecting
4368 * other functions in the same device. The PCI device must be responsive
4369 * to PCI config space in order to use this function.
4370 *
4371 * The device function is presumed to be unused and the caller is holding
4372 * the device mutex lock when this function is called.
4373 * Resetting the device will make the contents of PCI configuration space
4374 * random, so any caller of this must be prepared to reinitialise the
4375 * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
4376 * etc.
4377 *
4378 * Returns 0 if the device function was successfully reset or negative if the
4379 * device doesn't support resetting a single function.
4380 */
4381int __pci_reset_function_locked(struct pci_dev *dev)
4382{
Christoph Hellwig52354b92017-06-01 13:10:39 +02004383 int rc;
4384
4385 might_sleep();
4386
Bjorn Helgaas832c418a2017-10-25 17:09:24 -05004387 /*
4388 * A reset method returns -ENOTTY if it doesn't support this device
4389 * and we should try the next method.
4390 *
4391 * If it returns 0 (success), we're finished. If it returns any
4392 * other error, we're also finished: this indicates that further
4393 * reset mechanisms might be broken on the device.
4394 */
Christoph Hellwig52354b92017-06-01 13:10:39 +02004395 rc = pci_dev_specific_reset(dev, 0);
4396 if (rc != -ENOTTY)
4397 return rc;
4398 if (pcie_has_flr(dev)) {
Sinan Kaya91295d72018-02-27 14:14:08 -06004399 rc = pcie_flr(dev);
4400 if (rc != -ENOTTY)
4401 return rc;
Christoph Hellwig52354b92017-06-01 13:10:39 +02004402 }
4403 rc = pci_af_flr(dev, 0);
4404 if (rc != -ENOTTY)
4405 return rc;
4406 rc = pci_pm_reset(dev, 0);
4407 if (rc != -ENOTTY)
4408 return rc;
4409 rc = pci_dev_reset_slot_function(dev, 0);
4410 if (rc != -ENOTTY)
4411 return rc;
4412 return pci_parent_bus_reset(dev, 0);
Konrad Rzeszutek Wilk6fbf9e72012-01-12 12:06:46 -05004413}
4414EXPORT_SYMBOL_GPL(__pci_reset_function_locked);
4415
4416/**
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03004417 * pci_probe_reset_function - check whether the device can be safely reset
4418 * @dev: PCI device to reset
4419 *
4420 * Some devices allow an individual function to be reset without affecting
4421 * other functions in the same device. The PCI device must be responsive
4422 * to PCI config space in order to use this function.
4423 *
4424 * Returns 0 if the device function can be reset or negative if the
4425 * device doesn't support resetting a single function.
4426 */
4427int pci_probe_reset_function(struct pci_dev *dev)
4428{
Christoph Hellwig52354b92017-06-01 13:10:39 +02004429 int rc;
4430
4431 might_sleep();
4432
4433 rc = pci_dev_specific_reset(dev, 1);
4434 if (rc != -ENOTTY)
4435 return rc;
4436 if (pcie_has_flr(dev))
4437 return 0;
4438 rc = pci_af_flr(dev, 1);
4439 if (rc != -ENOTTY)
4440 return rc;
4441 rc = pci_pm_reset(dev, 1);
4442 if (rc != -ENOTTY)
4443 return rc;
4444 rc = pci_dev_reset_slot_function(dev, 1);
4445 if (rc != -ENOTTY)
4446 return rc;
4447
4448 return pci_parent_bus_reset(dev, 1);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03004449}
4450
4451/**
Yu Zhao8c1c6992009-06-13 15:52:13 +08004452 * pci_reset_function - quiesce and reset a PCI device function
4453 * @dev: PCI device to reset
Sheng Yang8dd7f802008-10-21 17:38:25 +08004454 *
4455 * Some devices allow an individual function to be reset without affecting
4456 * other functions in the same device. The PCI device must be responsive
4457 * to PCI config space in order to use this function.
4458 *
4459 * This function does not just reset the PCI portion of a device, but
4460 * clears all the state associated with the device. This function differs
Jan H. Schönherr79e699b2017-09-06 01:21:23 +02004461 * from __pci_reset_function_locked() in that it saves and restores device state
4462 * over the reset and takes the PCI device lock.
Sheng Yang8dd7f802008-10-21 17:38:25 +08004463 *
Yu Zhao8c1c6992009-06-13 15:52:13 +08004464 * Returns 0 if the device function was successfully reset or negative if the
Sheng Yang8dd7f802008-10-21 17:38:25 +08004465 * device doesn't support resetting a single function.
4466 */
4467int pci_reset_function(struct pci_dev *dev)
4468{
Yu Zhao8c1c6992009-06-13 15:52:13 +08004469 int rc;
Sheng Yang8dd7f802008-10-21 17:38:25 +08004470
Bjorn Helgaas204f4af2018-02-16 15:22:39 -06004471 if (!dev->reset_fn)
4472 return -ENOTTY;
Sheng Yang8dd7f802008-10-21 17:38:25 +08004473
Christoph Hellwigb014e962017-06-01 13:10:37 +02004474 pci_dev_lock(dev);
Alex Williamson77cb9852013-08-08 14:09:49 -06004475 pci_dev_save_and_disable(dev);
Sheng Yang8dd7f802008-10-21 17:38:25 +08004476
Christoph Hellwig52354b92017-06-01 13:10:39 +02004477 rc = __pci_reset_function_locked(dev);
Sheng Yang8dd7f802008-10-21 17:38:25 +08004478
Alex Williamson77cb9852013-08-08 14:09:49 -06004479 pci_dev_restore(dev);
Christoph Hellwigb014e962017-06-01 13:10:37 +02004480 pci_dev_unlock(dev);
Sheng Yang8dd7f802008-10-21 17:38:25 +08004481
Yu Zhao8c1c6992009-06-13 15:52:13 +08004482 return rc;
Sheng Yang8dd7f802008-10-21 17:38:25 +08004483}
4484EXPORT_SYMBOL_GPL(pci_reset_function);
4485
Alex Williamson61cf16d2013-12-16 15:14:31 -07004486/**
Marc Zyngiera477b9c2017-08-01 20:11:02 -05004487 * pci_reset_function_locked - quiesce and reset a PCI device function
4488 * @dev: PCI device to reset
4489 *
4490 * Some devices allow an individual function to be reset without affecting
4491 * other functions in the same device. The PCI device must be responsive
4492 * to PCI config space in order to use this function.
4493 *
4494 * This function does not just reset the PCI portion of a device, but
4495 * clears all the state associated with the device. This function differs
Jan H. Schönherr79e699b2017-09-06 01:21:23 +02004496 * from __pci_reset_function_locked() in that it saves and restores device state
Marc Zyngiera477b9c2017-08-01 20:11:02 -05004497 * over the reset. It also differs from pci_reset_function() in that it
4498 * requires the PCI device lock to be held.
4499 *
4500 * Returns 0 if the device function was successfully reset or negative if the
4501 * device doesn't support resetting a single function.
4502 */
4503int pci_reset_function_locked(struct pci_dev *dev)
4504{
4505 int rc;
4506
Bjorn Helgaas204f4af2018-02-16 15:22:39 -06004507 if (!dev->reset_fn)
4508 return -ENOTTY;
Marc Zyngiera477b9c2017-08-01 20:11:02 -05004509
4510 pci_dev_save_and_disable(dev);
4511
4512 rc = __pci_reset_function_locked(dev);
4513
4514 pci_dev_restore(dev);
4515
4516 return rc;
4517}
4518EXPORT_SYMBOL_GPL(pci_reset_function_locked);
4519
4520/**
Alex Williamson61cf16d2013-12-16 15:14:31 -07004521 * pci_try_reset_function - quiesce and reset a PCI device function
4522 * @dev: PCI device to reset
4523 *
4524 * Same as above, except return -EAGAIN if unable to lock device.
4525 */
4526int pci_try_reset_function(struct pci_dev *dev)
4527{
4528 int rc;
4529
Bjorn Helgaas204f4af2018-02-16 15:22:39 -06004530 if (!dev->reset_fn)
4531 return -ENOTTY;
Alex Williamson61cf16d2013-12-16 15:14:31 -07004532
Christoph Hellwigb014e962017-06-01 13:10:37 +02004533 if (!pci_dev_trylock(dev))
4534 return -EAGAIN;
Alex Williamson61cf16d2013-12-16 15:14:31 -07004535
Christoph Hellwigb014e962017-06-01 13:10:37 +02004536 pci_dev_save_and_disable(dev);
Christoph Hellwig52354b92017-06-01 13:10:39 +02004537 rc = __pci_reset_function_locked(dev);
Sinan Kayacb5e0d02018-02-27 14:14:08 -06004538 pci_dev_restore(dev);
Christoph Hellwigb014e962017-06-01 13:10:37 +02004539 pci_dev_unlock(dev);
Alex Williamson61cf16d2013-12-16 15:14:31 -07004540
Alex Williamson61cf16d2013-12-16 15:14:31 -07004541 return rc;
4542}
4543EXPORT_SYMBOL_GPL(pci_try_reset_function);
4544
Alex Williamsonf331a852015-01-15 18:16:04 -06004545/* Do any devices on or below this bus prevent a bus reset? */
4546static bool pci_bus_resetable(struct pci_bus *bus)
4547{
4548 struct pci_dev *dev;
4549
David Daney35702772017-09-08 10:10:31 +02004550
4551 if (bus->self && (bus->self->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET))
4552 return false;
4553
Alex Williamsonf331a852015-01-15 18:16:04 -06004554 list_for_each_entry(dev, &bus->devices, bus_list) {
4555 if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET ||
4556 (dev->subordinate && !pci_bus_resetable(dev->subordinate)))
4557 return false;
4558 }
4559
4560 return true;
4561}
4562
Alex Williamson090a3c52013-08-08 14:09:55 -06004563/* Lock devices from the top of the tree down */
4564static void pci_bus_lock(struct pci_bus *bus)
4565{
4566 struct pci_dev *dev;
4567
4568 list_for_each_entry(dev, &bus->devices, bus_list) {
4569 pci_dev_lock(dev);
4570 if (dev->subordinate)
4571 pci_bus_lock(dev->subordinate);
4572 }
4573}
4574
4575/* Unlock devices from the bottom of the tree up */
4576static void pci_bus_unlock(struct pci_bus *bus)
4577{
4578 struct pci_dev *dev;
4579
4580 list_for_each_entry(dev, &bus->devices, bus_list) {
4581 if (dev->subordinate)
4582 pci_bus_unlock(dev->subordinate);
4583 pci_dev_unlock(dev);
4584 }
4585}
4586
Alex Williamson61cf16d2013-12-16 15:14:31 -07004587/* Return 1 on successful lock, 0 on contention */
4588static int pci_bus_trylock(struct pci_bus *bus)
4589{
4590 struct pci_dev *dev;
4591
4592 list_for_each_entry(dev, &bus->devices, bus_list) {
4593 if (!pci_dev_trylock(dev))
4594 goto unlock;
4595 if (dev->subordinate) {
4596 if (!pci_bus_trylock(dev->subordinate)) {
4597 pci_dev_unlock(dev);
4598 goto unlock;
4599 }
4600 }
4601 }
4602 return 1;
4603
4604unlock:
4605 list_for_each_entry_continue_reverse(dev, &bus->devices, bus_list) {
4606 if (dev->subordinate)
4607 pci_bus_unlock(dev->subordinate);
4608 pci_dev_unlock(dev);
4609 }
4610 return 0;
4611}
4612
Alex Williamsonf331a852015-01-15 18:16:04 -06004613/* Do any devices on or below this slot prevent a bus reset? */
4614static bool pci_slot_resetable(struct pci_slot *slot)
4615{
4616 struct pci_dev *dev;
4617
Jan Glauber33ba90a2017-09-08 10:10:33 +02004618 if (slot->bus->self &&
4619 (slot->bus->self->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET))
4620 return false;
4621
Alex Williamsonf331a852015-01-15 18:16:04 -06004622 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4623 if (!dev->slot || dev->slot != slot)
4624 continue;
4625 if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET ||
4626 (dev->subordinate && !pci_bus_resetable(dev->subordinate)))
4627 return false;
4628 }
4629
4630 return true;
4631}
4632
Alex Williamson090a3c52013-08-08 14:09:55 -06004633/* Lock devices from the top of the tree down */
4634static void pci_slot_lock(struct pci_slot *slot)
4635{
4636 struct pci_dev *dev;
4637
4638 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4639 if (!dev->slot || dev->slot != slot)
4640 continue;
4641 pci_dev_lock(dev);
4642 if (dev->subordinate)
4643 pci_bus_lock(dev->subordinate);
4644 }
4645}
4646
4647/* Unlock devices from the bottom of the tree up */
4648static void pci_slot_unlock(struct pci_slot *slot)
4649{
4650 struct pci_dev *dev;
4651
4652 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4653 if (!dev->slot || dev->slot != slot)
4654 continue;
4655 if (dev->subordinate)
4656 pci_bus_unlock(dev->subordinate);
4657 pci_dev_unlock(dev);
4658 }
4659}
4660
Alex Williamson61cf16d2013-12-16 15:14:31 -07004661/* Return 1 on successful lock, 0 on contention */
4662static int pci_slot_trylock(struct pci_slot *slot)
4663{
4664 struct pci_dev *dev;
4665
4666 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4667 if (!dev->slot || dev->slot != slot)
4668 continue;
4669 if (!pci_dev_trylock(dev))
4670 goto unlock;
4671 if (dev->subordinate) {
4672 if (!pci_bus_trylock(dev->subordinate)) {
4673 pci_dev_unlock(dev);
4674 goto unlock;
4675 }
4676 }
4677 }
4678 return 1;
4679
4680unlock:
4681 list_for_each_entry_continue_reverse(dev,
4682 &slot->bus->devices, bus_list) {
4683 if (!dev->slot || dev->slot != slot)
4684 continue;
4685 if (dev->subordinate)
4686 pci_bus_unlock(dev->subordinate);
4687 pci_dev_unlock(dev);
4688 }
4689 return 0;
4690}
4691
Alex Williamson090a3c52013-08-08 14:09:55 -06004692/* Save and disable devices from the top of the tree down */
4693static void pci_bus_save_and_disable(struct pci_bus *bus)
4694{
4695 struct pci_dev *dev;
4696
4697 list_for_each_entry(dev, &bus->devices, bus_list) {
Christoph Hellwigb014e962017-06-01 13:10:37 +02004698 pci_dev_lock(dev);
Alex Williamson090a3c52013-08-08 14:09:55 -06004699 pci_dev_save_and_disable(dev);
Christoph Hellwigb014e962017-06-01 13:10:37 +02004700 pci_dev_unlock(dev);
Alex Williamson090a3c52013-08-08 14:09:55 -06004701 if (dev->subordinate)
4702 pci_bus_save_and_disable(dev->subordinate);
4703 }
4704}
4705
4706/*
4707 * Restore devices from top of the tree down - parent bridges need to be
4708 * restored before we can get to subordinate devices.
4709 */
4710static void pci_bus_restore(struct pci_bus *bus)
4711{
4712 struct pci_dev *dev;
4713
4714 list_for_each_entry(dev, &bus->devices, bus_list) {
Christoph Hellwigb014e962017-06-01 13:10:37 +02004715 pci_dev_lock(dev);
Alex Williamson090a3c52013-08-08 14:09:55 -06004716 pci_dev_restore(dev);
Christoph Hellwigb014e962017-06-01 13:10:37 +02004717 pci_dev_unlock(dev);
Alex Williamson090a3c52013-08-08 14:09:55 -06004718 if (dev->subordinate)
4719 pci_bus_restore(dev->subordinate);
4720 }
4721}
4722
4723/* Save and disable devices from the top of the tree down */
4724static void pci_slot_save_and_disable(struct pci_slot *slot)
4725{
4726 struct pci_dev *dev;
4727
4728 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4729 if (!dev->slot || dev->slot != slot)
4730 continue;
4731 pci_dev_save_and_disable(dev);
4732 if (dev->subordinate)
4733 pci_bus_save_and_disable(dev->subordinate);
4734 }
4735}
4736
4737/*
4738 * Restore devices from top of the tree down - parent bridges need to be
4739 * restored before we can get to subordinate devices.
4740 */
4741static void pci_slot_restore(struct pci_slot *slot)
4742{
4743 struct pci_dev *dev;
4744
4745 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4746 if (!dev->slot || dev->slot != slot)
4747 continue;
Sinan Kayacb5e0d02018-02-27 14:14:08 -06004748 pci_dev_lock(dev);
Alex Williamson090a3c52013-08-08 14:09:55 -06004749 pci_dev_restore(dev);
Sinan Kayacb5e0d02018-02-27 14:14:08 -06004750 pci_dev_unlock(dev);
Alex Williamson090a3c52013-08-08 14:09:55 -06004751 if (dev->subordinate)
4752 pci_bus_restore(dev->subordinate);
4753 }
4754}
4755
4756static int pci_slot_reset(struct pci_slot *slot, int probe)
4757{
4758 int rc;
4759
Alex Williamsonf331a852015-01-15 18:16:04 -06004760 if (!slot || !pci_slot_resetable(slot))
Alex Williamson090a3c52013-08-08 14:09:55 -06004761 return -ENOTTY;
4762
4763 if (!probe)
4764 pci_slot_lock(slot);
4765
4766 might_sleep();
4767
4768 rc = pci_reset_hotplug_slot(slot->hotplug, probe);
4769
4770 if (!probe)
4771 pci_slot_unlock(slot);
4772
4773 return rc;
4774}
4775
4776/**
Alex Williamson9a3d2b92013-08-14 14:06:05 -06004777 * pci_probe_reset_slot - probe whether a PCI slot can be reset
4778 * @slot: PCI slot to probe
4779 *
4780 * Return 0 if slot can be reset, negative if a slot reset is not supported.
4781 */
4782int pci_probe_reset_slot(struct pci_slot *slot)
4783{
4784 return pci_slot_reset(slot, 1);
4785}
4786EXPORT_SYMBOL_GPL(pci_probe_reset_slot);
4787
4788/**
Alex Williamson090a3c52013-08-08 14:09:55 -06004789 * pci_reset_slot - reset a PCI slot
4790 * @slot: PCI slot to reset
4791 *
4792 * A PCI bus may host multiple slots, each slot may support a reset mechanism
4793 * independent of other slots. For instance, some slots may support slot power
4794 * control. In the case of a 1:1 bus to slot architecture, this function may
4795 * wrap the bus reset to avoid spurious slot related events such as hotplug.
4796 * Generally a slot reset should be attempted before a bus reset. All of the
4797 * function of the slot and any subordinate buses behind the slot are reset
4798 * through this function. PCI config space of all devices in the slot and
4799 * behind the slot is saved before and restored after reset.
4800 *
4801 * Return 0 on success, non-zero on error.
4802 */
4803int pci_reset_slot(struct pci_slot *slot)
4804{
4805 int rc;
4806
4807 rc = pci_slot_reset(slot, 1);
4808 if (rc)
4809 return rc;
4810
4811 pci_slot_save_and_disable(slot);
4812
4813 rc = pci_slot_reset(slot, 0);
4814
4815 pci_slot_restore(slot);
4816
4817 return rc;
4818}
4819EXPORT_SYMBOL_GPL(pci_reset_slot);
4820
Alex Williamson61cf16d2013-12-16 15:14:31 -07004821/**
4822 * pci_try_reset_slot - Try to reset a PCI slot
4823 * @slot: PCI slot to reset
4824 *
4825 * Same as above except return -EAGAIN if the slot cannot be locked
4826 */
4827int pci_try_reset_slot(struct pci_slot *slot)
4828{
4829 int rc;
4830
4831 rc = pci_slot_reset(slot, 1);
4832 if (rc)
4833 return rc;
4834
4835 pci_slot_save_and_disable(slot);
4836
4837 if (pci_slot_trylock(slot)) {
4838 might_sleep();
4839 rc = pci_reset_hotplug_slot(slot->hotplug, 0);
4840 pci_slot_unlock(slot);
4841 } else
4842 rc = -EAGAIN;
4843
4844 pci_slot_restore(slot);
4845
4846 return rc;
4847}
4848EXPORT_SYMBOL_GPL(pci_try_reset_slot);
4849
Alex Williamson090a3c52013-08-08 14:09:55 -06004850static int pci_bus_reset(struct pci_bus *bus, int probe)
4851{
Alex Williamsonf331a852015-01-15 18:16:04 -06004852 if (!bus->self || !pci_bus_resetable(bus))
Alex Williamson090a3c52013-08-08 14:09:55 -06004853 return -ENOTTY;
4854
4855 if (probe)
4856 return 0;
4857
4858 pci_bus_lock(bus);
4859
4860 might_sleep();
4861
4862 pci_reset_bridge_secondary_bus(bus->self);
4863
4864 pci_bus_unlock(bus);
4865
4866 return 0;
4867}
4868
4869/**
Alex Williamson9a3d2b92013-08-14 14:06:05 -06004870 * pci_probe_reset_bus - probe whether a PCI bus can be reset
4871 * @bus: PCI bus to probe
4872 *
4873 * Return 0 if bus can be reset, negative if a bus reset is not supported.
4874 */
4875int pci_probe_reset_bus(struct pci_bus *bus)
4876{
4877 return pci_bus_reset(bus, 1);
4878}
4879EXPORT_SYMBOL_GPL(pci_probe_reset_bus);
4880
4881/**
Alex Williamson090a3c52013-08-08 14:09:55 -06004882 * pci_reset_bus - reset a PCI bus
4883 * @bus: top level PCI bus to reset
4884 *
4885 * Do a bus reset on the given bus and any subordinate buses, saving
4886 * and restoring state of all devices.
4887 *
4888 * Return 0 on success, non-zero on error.
4889 */
4890int pci_reset_bus(struct pci_bus *bus)
4891{
4892 int rc;
4893
4894 rc = pci_bus_reset(bus, 1);
4895 if (rc)
4896 return rc;
4897
4898 pci_bus_save_and_disable(bus);
4899
4900 rc = pci_bus_reset(bus, 0);
4901
4902 pci_bus_restore(bus);
4903
4904 return rc;
4905}
4906EXPORT_SYMBOL_GPL(pci_reset_bus);
4907
Sheng Yang8dd7f802008-10-21 17:38:25 +08004908/**
Alex Williamson61cf16d2013-12-16 15:14:31 -07004909 * pci_try_reset_bus - Try to reset a PCI bus
4910 * @bus: top level PCI bus to reset
4911 *
4912 * Same as above except return -EAGAIN if the bus cannot be locked
4913 */
4914int pci_try_reset_bus(struct pci_bus *bus)
4915{
4916 int rc;
4917
4918 rc = pci_bus_reset(bus, 1);
4919 if (rc)
4920 return rc;
4921
4922 pci_bus_save_and_disable(bus);
4923
4924 if (pci_bus_trylock(bus)) {
4925 might_sleep();
4926 pci_reset_bridge_secondary_bus(bus->self);
4927 pci_bus_unlock(bus);
4928 } else
4929 rc = -EAGAIN;
4930
4931 pci_bus_restore(bus);
4932
4933 return rc;
4934}
4935EXPORT_SYMBOL_GPL(pci_try_reset_bus);
4936
4937/**
Peter Orubad556ad42007-05-15 13:59:13 +02004938 * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
4939 * @dev: PCI device to query
4940 *
4941 * Returns mmrbc: maximum designed memory read count in bytes
4942 * or appropriate error value.
4943 */
4944int pcix_get_max_mmrbc(struct pci_dev *dev)
4945{
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004946 int cap;
Peter Orubad556ad42007-05-15 13:59:13 +02004947 u32 stat;
4948
4949 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
4950 if (!cap)
4951 return -EINVAL;
4952
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004953 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
Peter Orubad556ad42007-05-15 13:59:13 +02004954 return -EINVAL;
4955
Dean Nelson25daeb52010-03-09 22:26:40 -05004956 return 512 << ((stat & PCI_X_STATUS_MAX_READ) >> 21);
Peter Orubad556ad42007-05-15 13:59:13 +02004957}
4958EXPORT_SYMBOL(pcix_get_max_mmrbc);
4959
4960/**
4961 * pcix_get_mmrbc - get PCI-X maximum memory read byte count
4962 * @dev: PCI device to query
4963 *
4964 * Returns mmrbc: maximum memory read count in bytes
4965 * or appropriate error value.
4966 */
4967int pcix_get_mmrbc(struct pci_dev *dev)
4968{
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004969 int cap;
Dean Nelsonbdc2bda2010-03-09 22:26:48 -05004970 u16 cmd;
Peter Orubad556ad42007-05-15 13:59:13 +02004971
4972 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
4973 if (!cap)
4974 return -EINVAL;
4975
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004976 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
4977 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02004978
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004979 return 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
Peter Orubad556ad42007-05-15 13:59:13 +02004980}
4981EXPORT_SYMBOL(pcix_get_mmrbc);
4982
4983/**
4984 * pcix_set_mmrbc - set PCI-X maximum memory read byte count
4985 * @dev: PCI device to query
4986 * @mmrbc: maximum memory read count in bytes
4987 * valid values are 512, 1024, 2048, 4096
4988 *
4989 * If possible sets maximum memory read byte count, some bridges have erratas
4990 * that prevent this.
4991 */
4992int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
4993{
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004994 int cap;
Dean Nelsonbdc2bda2010-03-09 22:26:48 -05004995 u32 stat, v, o;
4996 u16 cmd;
Peter Orubad556ad42007-05-15 13:59:13 +02004997
vignesh babu229f5af2007-08-13 18:23:14 +05304998 if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004999 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02005000
5001 v = ffs(mmrbc) - 10;
5002
5003 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
5004 if (!cap)
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005005 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02005006
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005007 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
5008 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02005009
5010 if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
5011 return -E2BIG;
5012
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005013 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
5014 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02005015
5016 o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
5017 if (o != v) {
Bjorn Helgaas809a3bf2012-06-20 16:41:16 -06005018 if (v > o && (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
Peter Orubad556ad42007-05-15 13:59:13 +02005019 return -EIO;
5020
5021 cmd &= ~PCI_X_CMD_MAX_READ;
5022 cmd |= v << 2;
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005023 if (pci_write_config_word(dev, cap + PCI_X_CMD, cmd))
5024 return -EIO;
Peter Orubad556ad42007-05-15 13:59:13 +02005025 }
Dean Nelson7c9e2b12010-03-09 22:26:55 -05005026 return 0;
Peter Orubad556ad42007-05-15 13:59:13 +02005027}
5028EXPORT_SYMBOL(pcix_set_mmrbc);
5029
5030/**
5031 * pcie_get_readrq - get PCI Express read request size
5032 * @dev: PCI device to query
5033 *
5034 * Returns maximum memory read request in bytes
5035 * or appropriate error value.
5036 */
5037int pcie_get_readrq(struct pci_dev *dev)
5038{
Peter Orubad556ad42007-05-15 13:59:13 +02005039 u16 ctl;
5040
Jiang Liu59875ae2012-07-24 17:20:06 +08005041 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
Peter Orubad556ad42007-05-15 13:59:13 +02005042
Jiang Liu59875ae2012-07-24 17:20:06 +08005043 return 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
Peter Orubad556ad42007-05-15 13:59:13 +02005044}
5045EXPORT_SYMBOL(pcie_get_readrq);
5046
5047/**
5048 * pcie_set_readrq - set PCI Express maximum memory read request
5049 * @dev: PCI device to query
Randy Dunlap42e61f4a2007-07-23 21:42:11 -07005050 * @rq: maximum memory read count in bytes
Peter Orubad556ad42007-05-15 13:59:13 +02005051 * valid values are 128, 256, 512, 1024, 2048, 4096
5052 *
Jon Masonc9b378c2011-06-28 18:26:25 -05005053 * If possible sets maximum memory read request in bytes
Peter Orubad556ad42007-05-15 13:59:13 +02005054 */
5055int pcie_set_readrq(struct pci_dev *dev, int rq)
5056{
Jiang Liu59875ae2012-07-24 17:20:06 +08005057 u16 v;
Peter Orubad556ad42007-05-15 13:59:13 +02005058
vignesh babu229f5af2007-08-13 18:23:14 +05305059 if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
Jiang Liu59875ae2012-07-24 17:20:06 +08005060 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02005061
Benjamin Herrenschmidta1c473a2011-10-14 14:56:15 -05005062 /*
5063 * If using the "performance" PCIe config, we clamp the
5064 * read rq size to the max packet size to prevent the
5065 * host bridge generating requests larger than we can
5066 * cope with
5067 */
5068 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
5069 int mps = pcie_get_mps(dev);
5070
Benjamin Herrenschmidta1c473a2011-10-14 14:56:15 -05005071 if (mps < rq)
5072 rq = mps;
5073 }
5074
5075 v = (ffs(rq) - 8) << 12;
Peter Orubad556ad42007-05-15 13:59:13 +02005076
Jiang Liu59875ae2012-07-24 17:20:06 +08005077 return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
5078 PCI_EXP_DEVCTL_READRQ, v);
Peter Orubad556ad42007-05-15 13:59:13 +02005079}
5080EXPORT_SYMBOL(pcie_set_readrq);
5081
5082/**
Jon Masonb03e7492011-07-20 15:20:54 -05005083 * pcie_get_mps - get PCI Express maximum payload size
5084 * @dev: PCI device to query
5085 *
5086 * Returns maximum payload size in bytes
Jon Masonb03e7492011-07-20 15:20:54 -05005087 */
5088int pcie_get_mps(struct pci_dev *dev)
5089{
Jon Masonb03e7492011-07-20 15:20:54 -05005090 u16 ctl;
5091
Jiang Liu59875ae2012-07-24 17:20:06 +08005092 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
Jon Masonb03e7492011-07-20 15:20:54 -05005093
Jiang Liu59875ae2012-07-24 17:20:06 +08005094 return 128 << ((ctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
Jon Masonb03e7492011-07-20 15:20:54 -05005095}
Yijing Wangf1c66c42013-09-24 12:08:06 -06005096EXPORT_SYMBOL(pcie_get_mps);
Jon Masonb03e7492011-07-20 15:20:54 -05005097
5098/**
5099 * pcie_set_mps - set PCI Express maximum payload size
5100 * @dev: PCI device to query
Randy Dunlap47c08f32011-08-20 11:49:43 -07005101 * @mps: maximum payload size in bytes
Jon Masonb03e7492011-07-20 15:20:54 -05005102 * valid values are 128, 256, 512, 1024, 2048, 4096
5103 *
5104 * If possible sets maximum payload size
5105 */
5106int pcie_set_mps(struct pci_dev *dev, int mps)
5107{
Jiang Liu59875ae2012-07-24 17:20:06 +08005108 u16 v;
Jon Masonb03e7492011-07-20 15:20:54 -05005109
5110 if (mps < 128 || mps > 4096 || !is_power_of_2(mps))
Jiang Liu59875ae2012-07-24 17:20:06 +08005111 return -EINVAL;
Jon Masonb03e7492011-07-20 15:20:54 -05005112
5113 v = ffs(mps) - 8;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07005114 if (v > dev->pcie_mpss)
Jiang Liu59875ae2012-07-24 17:20:06 +08005115 return -EINVAL;
Jon Masonb03e7492011-07-20 15:20:54 -05005116 v <<= 5;
5117
Jiang Liu59875ae2012-07-24 17:20:06 +08005118 return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
5119 PCI_EXP_DEVCTL_PAYLOAD, v);
Jon Masonb03e7492011-07-20 15:20:54 -05005120}
Yijing Wangf1c66c42013-09-24 12:08:06 -06005121EXPORT_SYMBOL(pcie_set_mps);
Jon Masonb03e7492011-07-20 15:20:54 -05005122
5123/**
Jacob Keller81377c82013-07-31 06:53:26 +00005124 * pcie_get_minimum_link - determine minimum link settings of a PCI device
5125 * @dev: PCI device to query
5126 * @speed: storage for minimum speed
5127 * @width: storage for minimum width
5128 *
5129 * This function will walk up the PCI device chain and determine the minimum
5130 * link width and speed of the device.
5131 */
5132int pcie_get_minimum_link(struct pci_dev *dev, enum pci_bus_speed *speed,
5133 enum pcie_link_width *width)
5134{
5135 int ret;
5136
5137 *speed = PCI_SPEED_UNKNOWN;
5138 *width = PCIE_LNK_WIDTH_UNKNOWN;
5139
5140 while (dev) {
5141 u16 lnksta;
5142 enum pci_bus_speed next_speed;
5143 enum pcie_link_width next_width;
5144
5145 ret = pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
5146 if (ret)
5147 return ret;
5148
5149 next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
5150 next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >>
5151 PCI_EXP_LNKSTA_NLW_SHIFT;
5152
5153 if (next_speed < *speed)
5154 *speed = next_speed;
5155
5156 if (next_width < *width)
5157 *width = next_width;
5158
5159 dev = dev->bus->self;
5160 }
5161
5162 return 0;
5163}
5164EXPORT_SYMBOL(pcie_get_minimum_link);
5165
5166/**
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09005167 * pci_select_bars - Make BAR mask from the type of resource
Randy Dunlapf95d8822007-02-10 14:41:56 -08005168 * @dev: the PCI device for which BAR mask is made
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09005169 * @flags: resource type mask to be selected
5170 *
5171 * This helper routine makes bar mask from the type of resource.
5172 */
5173int pci_select_bars(struct pci_dev *dev, unsigned long flags)
5174{
5175 int i, bars = 0;
5176 for (i = 0; i < PCI_NUM_RESOURCES; i++)
5177 if (pci_resource_flags(dev, i) & flags)
5178 bars |= (1 << i);
5179 return bars;
5180}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06005181EXPORT_SYMBOL(pci_select_bars);
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09005182
Mike Travis95a8b6e2010-02-02 14:38:13 -08005183/* Some architectures require additional programming to enable VGA */
5184static arch_set_vga_state_t arch_set_vga_state;
5185
5186void __init pci_register_set_vga_state(arch_set_vga_state_t func)
5187{
5188 arch_set_vga_state = func; /* NULL disables */
5189}
5190
5191static int pci_set_vga_state_arch(struct pci_dev *dev, bool decode,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04005192 unsigned int command_bits, u32 flags)
Mike Travis95a8b6e2010-02-02 14:38:13 -08005193{
5194 if (arch_set_vga_state)
5195 return arch_set_vga_state(dev, decode, command_bits,
Dave Airlie7ad35cf2011-05-25 14:00:49 +10005196 flags);
Mike Travis95a8b6e2010-02-02 14:38:13 -08005197 return 0;
5198}
5199
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10005200/**
5201 * pci_set_vga_state - set VGA decode state on device and parents if requested
Randy Dunlap19eea632009-09-17 15:28:22 -07005202 * @dev: the PCI device
5203 * @decode: true = enable decoding, false = disable decoding
5204 * @command_bits: PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY
Randy Dunlap3f37d622011-05-25 19:21:25 -07005205 * @flags: traverse ancestors and change bridges
Dave Airlie3448a192010-06-01 15:32:24 +10005206 * CHANGE_BRIDGE_ONLY / CHANGE_BRIDGE
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10005207 */
5208int pci_set_vga_state(struct pci_dev *dev, bool decode,
Dave Airlie3448a192010-06-01 15:32:24 +10005209 unsigned int command_bits, u32 flags)
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10005210{
5211 struct pci_bus *bus;
5212 struct pci_dev *bridge;
5213 u16 cmd;
Mike Travis95a8b6e2010-02-02 14:38:13 -08005214 int rc;
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10005215
Bjorn Helgaas67ebd812014-04-05 15:14:22 -06005216 WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) && (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY)));
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10005217
Mike Travis95a8b6e2010-02-02 14:38:13 -08005218 /* ARCH specific VGA enables */
Dave Airlie3448a192010-06-01 15:32:24 +10005219 rc = pci_set_vga_state_arch(dev, decode, command_bits, flags);
Mike Travis95a8b6e2010-02-02 14:38:13 -08005220 if (rc)
5221 return rc;
5222
Dave Airlie3448a192010-06-01 15:32:24 +10005223 if (flags & PCI_VGA_STATE_CHANGE_DECODES) {
5224 pci_read_config_word(dev, PCI_COMMAND, &cmd);
5225 if (decode == true)
5226 cmd |= command_bits;
5227 else
5228 cmd &= ~command_bits;
5229 pci_write_config_word(dev, PCI_COMMAND, cmd);
5230 }
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10005231
Dave Airlie3448a192010-06-01 15:32:24 +10005232 if (!(flags & PCI_VGA_STATE_CHANGE_BRIDGE))
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10005233 return 0;
5234
5235 bus = dev->bus;
5236 while (bus) {
5237 bridge = bus->self;
5238 if (bridge) {
5239 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
5240 &cmd);
5241 if (decode == true)
5242 cmd |= PCI_BRIDGE_CTL_VGA;
5243 else
5244 cmd &= ~PCI_BRIDGE_CTL_VGA;
5245 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL,
5246 cmd);
5247 }
5248 bus = bus->parent;
5249 }
5250 return 0;
5251}
5252
Bjorn Helgaasf0af9592016-02-24 13:43:45 -06005253/**
5254 * pci_add_dma_alias - Add a DMA devfn alias for a device
5255 * @dev: the PCI device for which alias is added
5256 * @devfn: alias slot and function
5257 *
5258 * This helper encodes 8-bit devfn as bit number in dma_alias_mask.
5259 * It should be called early, preferably as PCI fixup header quirk.
5260 */
5261void pci_add_dma_alias(struct pci_dev *dev, u8 devfn)
5262{
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01005263 if (!dev->dma_alias_mask)
5264 dev->dma_alias_mask = kcalloc(BITS_TO_LONGS(U8_MAX),
5265 sizeof(long), GFP_KERNEL);
5266 if (!dev->dma_alias_mask) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06005267 pci_warn(dev, "Unable to allocate DMA alias mask\n");
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01005268 return;
5269 }
5270
5271 set_bit(devfn, dev->dma_alias_mask);
Frederick Lawler7506dc72018-01-18 12:55:24 -06005272 pci_info(dev, "Enabling fixed DMA alias to %02x.%d\n",
Bjorn Helgaas48c83082016-02-24 13:43:54 -06005273 PCI_SLOT(devfn), PCI_FUNC(devfn));
Bjorn Helgaasf0af9592016-02-24 13:43:45 -06005274}
5275
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01005276bool pci_devs_are_dma_aliases(struct pci_dev *dev1, struct pci_dev *dev2)
5277{
5278 return (dev1->dma_alias_mask &&
5279 test_bit(dev2->devfn, dev1->dma_alias_mask)) ||
5280 (dev2->dma_alias_mask &&
5281 test_bit(dev1->devfn, dev2->dma_alias_mask));
5282}
5283
Rafael J. Wysocki8496e852013-12-01 02:34:37 +01005284bool pci_device_is_present(struct pci_dev *pdev)
5285{
5286 u32 v;
5287
Keith Buschfe2bd752017-03-29 22:49:17 -05005288 if (pci_dev_is_disconnected(pdev))
5289 return false;
Rafael J. Wysocki8496e852013-12-01 02:34:37 +01005290 return pci_bus_read_dev_vendor_id(pdev->bus, pdev->devfn, &v, 0);
5291}
5292EXPORT_SYMBOL_GPL(pci_device_is_present);
5293
Rafael J. Wysocki08249652015-04-13 16:23:36 +02005294void pci_ignore_hotplug(struct pci_dev *dev)
5295{
5296 struct pci_dev *bridge = dev->bus->self;
5297
5298 dev->ignore_hotplug = 1;
5299 /* Propagate the "ignore hotplug" setting to the parent bridge. */
5300 if (bridge)
5301 bridge->ignore_hotplug = 1;
5302}
5303EXPORT_SYMBOL_GPL(pci_ignore_hotplug);
5304
Yongji Xie0a701aa2017-04-10 19:58:12 +08005305resource_size_t __weak pcibios_default_alignment(void)
5306{
5307 return 0;
5308}
5309
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005310#define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZE
5311static char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE] = {0};
Thomas Gleixnere9d1e492009-11-06 22:41:23 +00005312static DEFINE_SPINLOCK(resource_alignment_lock);
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005313
5314/**
5315 * pci_specified_resource_alignment - get resource alignment specified by user.
5316 * @dev: the PCI device to get
Yongji Xiee3adec72017-04-10 19:58:14 +08005317 * @resize: whether or not to change resources' size when reassigning alignment
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005318 *
5319 * RETURNS: Resource alignment if it is specified.
5320 * Zero if it is not specified.
5321 */
Yongji Xiee3adec72017-04-10 19:58:14 +08005322static resource_size_t pci_specified_resource_alignment(struct pci_dev *dev,
5323 bool *resize)
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005324{
5325 int seg, bus, slot, func, align_order, count;
Koehrer Mathias (ETAS/ESW5)644a5442016-06-07 14:24:17 +00005326 unsigned short vendor, device, subsystem_vendor, subsystem_device;
Yongji Xie0a701aa2017-04-10 19:58:12 +08005327 resource_size_t align = pcibios_default_alignment();
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005328 char *p;
5329
5330 spin_lock(&resource_alignment_lock);
5331 p = resource_alignment_param;
Yongji Xie0a701aa2017-04-10 19:58:12 +08005332 if (!*p && !align)
Yongji Xief0b99f72016-09-13 17:00:31 +08005333 goto out;
5334 if (pci_has_flag(PCI_PROBE_ONLY)) {
Yongji Xie0a701aa2017-04-10 19:58:12 +08005335 align = 0;
Yongji Xief0b99f72016-09-13 17:00:31 +08005336 pr_info_once("PCI: Ignoring requested alignments (PCI_PROBE_ONLY)\n");
5337 goto out;
5338 }
5339
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005340 while (*p) {
5341 count = 0;
5342 if (sscanf(p, "%d%n", &align_order, &count) == 1 &&
5343 p[count] == '@') {
5344 p += count + 1;
5345 } else {
5346 align_order = -1;
5347 }
Koehrer Mathias (ETAS/ESW5)644a5442016-06-07 14:24:17 +00005348 if (strncmp(p, "pci:", 4) == 0) {
5349 /* PCI vendor/device (subvendor/subdevice) ids are specified */
5350 p += 4;
5351 if (sscanf(p, "%hx:%hx:%hx:%hx%n",
5352 &vendor, &device, &subsystem_vendor, &subsystem_device, &count) != 4) {
5353 if (sscanf(p, "%hx:%hx%n", &vendor, &device, &count) != 2) {
5354 printk(KERN_ERR "PCI: Can't parse resource_alignment parameter: pci:%s\n",
5355 p);
5356 break;
5357 }
5358 subsystem_vendor = subsystem_device = 0;
5359 }
5360 p += count;
5361 if ((!vendor || (vendor == dev->vendor)) &&
5362 (!device || (device == dev->device)) &&
5363 (!subsystem_vendor || (subsystem_vendor == dev->subsystem_vendor)) &&
5364 (!subsystem_device || (subsystem_device == dev->subsystem_device))) {
Yongji Xiee3adec72017-04-10 19:58:14 +08005365 *resize = true;
Koehrer Mathias (ETAS/ESW5)644a5442016-06-07 14:24:17 +00005366 if (align_order == -1)
5367 align = PAGE_SIZE;
5368 else
5369 align = 1 << align_order;
5370 /* Found */
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005371 break;
5372 }
5373 }
Koehrer Mathias (ETAS/ESW5)644a5442016-06-07 14:24:17 +00005374 else {
5375 if (sscanf(p, "%x:%x:%x.%x%n",
5376 &seg, &bus, &slot, &func, &count) != 4) {
5377 seg = 0;
5378 if (sscanf(p, "%x:%x.%x%n",
5379 &bus, &slot, &func, &count) != 3) {
5380 /* Invalid format */
5381 printk(KERN_ERR "PCI: Can't parse resource_alignment parameter: %s\n",
5382 p);
5383 break;
5384 }
5385 }
5386 p += count;
5387 if (seg == pci_domain_nr(dev->bus) &&
5388 bus == dev->bus->number &&
5389 slot == PCI_SLOT(dev->devfn) &&
5390 func == PCI_FUNC(dev->devfn)) {
Yongji Xiee3adec72017-04-10 19:58:14 +08005391 *resize = true;
Koehrer Mathias (ETAS/ESW5)644a5442016-06-07 14:24:17 +00005392 if (align_order == -1)
5393 align = PAGE_SIZE;
5394 else
5395 align = 1 << align_order;
5396 /* Found */
5397 break;
5398 }
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005399 }
5400 if (*p != ';' && *p != ',') {
5401 /* End of param or invalid format */
5402 break;
5403 }
5404 p++;
5405 }
Yongji Xief0b99f72016-09-13 17:00:31 +08005406out:
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005407 spin_unlock(&resource_alignment_lock);
5408 return align;
5409}
5410
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005411static void pci_request_resource_alignment(struct pci_dev *dev, int bar,
Yongji Xiee3adec72017-04-10 19:58:14 +08005412 resource_size_t align, bool resize)
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005413{
5414 struct resource *r = &dev->resource[bar];
5415 resource_size_t size;
5416
5417 if (!(r->flags & IORESOURCE_MEM))
5418 return;
5419
5420 if (r->flags & IORESOURCE_PCI_FIXED) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06005421 pci_info(dev, "BAR%d %pR: ignoring requested alignment %#llx\n",
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005422 bar, r, (unsigned long long)align);
5423 return;
5424 }
5425
5426 size = resource_size(r);
Bjorn Helgaas0dde1c02017-04-17 15:20:58 -05005427 if (size >= align)
5428 return;
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005429
Bjorn Helgaas0dde1c02017-04-17 15:20:58 -05005430 /*
Yongji Xiee3adec72017-04-10 19:58:14 +08005431 * Increase the alignment of the resource. There are two ways we
5432 * can do this:
Bjorn Helgaas0dde1c02017-04-17 15:20:58 -05005433 *
Yongji Xiee3adec72017-04-10 19:58:14 +08005434 * 1) Increase the size of the resource. BARs are aligned on their
5435 * size, so when we reallocate space for this resource, we'll
5436 * allocate it with the larger alignment. This also prevents
5437 * assignment of any other BARs inside the alignment region, so
5438 * if we're requesting page alignment, this means no other BARs
5439 * will share the page.
5440 *
5441 * The disadvantage is that this makes the resource larger than
5442 * the hardware BAR, which may break drivers that compute things
5443 * based on the resource size, e.g., to find registers at a
5444 * fixed offset before the end of the BAR.
5445 *
5446 * 2) Retain the resource size, but use IORESOURCE_STARTALIGN and
5447 * set r->start to the desired alignment. By itself this
5448 * doesn't prevent other BARs being put inside the alignment
5449 * region, but if we realign *every* resource of every device in
5450 * the system, none of them will share an alignment region.
5451 *
5452 * When the user has requested alignment for only some devices via
5453 * the "pci=resource_alignment" argument, "resize" is true and we
5454 * use the first method. Otherwise we assume we're aligning all
5455 * devices and we use the second.
Bjorn Helgaas0dde1c02017-04-17 15:20:58 -05005456 */
Yongji Xiee3adec72017-04-10 19:58:14 +08005457
Frederick Lawler7506dc72018-01-18 12:55:24 -06005458 pci_info(dev, "BAR%d %pR: requesting alignment to %#llx\n",
Bjorn Helgaas0dde1c02017-04-17 15:20:58 -05005459 bar, r, (unsigned long long)align);
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005460
Yongji Xiee3adec72017-04-10 19:58:14 +08005461 if (resize) {
5462 r->start = 0;
5463 r->end = align - 1;
5464 } else {
5465 r->flags &= ~IORESOURCE_SIZEALIGN;
5466 r->flags |= IORESOURCE_STARTALIGN;
5467 r->start = align;
5468 r->end = r->start + size - 1;
5469 }
Bjorn Helgaas0dde1c02017-04-17 15:20:58 -05005470 r->flags |= IORESOURCE_UNSET;
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005471}
5472
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005473/*
5474 * This function disables memory decoding and releases memory resources
5475 * of the device specified by kernel's boot parameter 'pci=resource_alignment='.
5476 * It also rounds up size to specified alignment.
5477 * Later on, the kernel will assign page-aligned memory resource back
5478 * to the device.
5479 */
5480void pci_reassigndev_resource_alignment(struct pci_dev *dev)
5481{
5482 int i;
5483 struct resource *r;
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005484 resource_size_t align;
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005485 u16 command;
Yongji Xiee3adec72017-04-10 19:58:14 +08005486 bool resize = false;
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005487
Yongji Xie62d9a782016-09-13 17:00:32 +08005488 /*
5489 * VF BARs are read-only zero according to SR-IOV spec r1.1, sec
5490 * 3.4.1.11. Their resources are allocated from the space
5491 * described by the VF BARx register in the PF's SR-IOV capability.
5492 * We can't influence their alignment here.
5493 */
5494 if (dev->is_virtfn)
5495 return;
5496
Yinghai Lu10c463a2012-03-18 22:46:26 -07005497 /* check if specified PCI is target device to reassign */
Yongji Xiee3adec72017-04-10 19:58:14 +08005498 align = pci_specified_resource_alignment(dev, &resize);
Yinghai Lu10c463a2012-03-18 22:46:26 -07005499 if (!align)
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005500 return;
5501
5502 if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL &&
5503 (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06005504 pci_warn(dev, "Can't reassign resources to host bridge\n");
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005505 return;
5506 }
5507
Frederick Lawler7506dc72018-01-18 12:55:24 -06005508 pci_info(dev, "Disabling memory decoding and releasing memory resources\n");
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005509 pci_read_config_word(dev, PCI_COMMAND, &command);
5510 command &= ~PCI_COMMAND_MEMORY;
5511 pci_write_config_word(dev, PCI_COMMAND, command);
5512
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005513 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
Yongji Xiee3adec72017-04-10 19:58:14 +08005514 pci_request_resource_alignment(dev, i, align, resize);
Yongji Xief0b99f72016-09-13 17:00:31 +08005515
Bjorn Helgaas81a5e702017-04-14 14:12:06 -05005516 /*
5517 * Need to disable bridge's resource window,
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005518 * to enable the kernel to reassign new resource
5519 * window later on.
5520 */
5521 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
5522 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
5523 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
5524 r = &dev->resource[i];
5525 if (!(r->flags & IORESOURCE_MEM))
5526 continue;
Bjorn Helgaasbd064f02014-02-26 11:25:58 -07005527 r->flags |= IORESOURCE_UNSET;
Yinghai Lu2069ecf2012-02-15 21:40:31 -08005528 r->end = resource_size(r) - 1;
5529 r->start = 0;
5530 }
5531 pci_disable_bridge_window(dev);
5532 }
5533}
5534
Bjorn Helgaas9738abe2013-04-12 11:20:03 -06005535static ssize_t pci_set_resource_alignment_param(const char *buf, size_t count)
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005536{
5537 if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1)
5538 count = RESOURCE_ALIGNMENT_PARAM_SIZE - 1;
5539 spin_lock(&resource_alignment_lock);
5540 strncpy(resource_alignment_param, buf, count);
5541 resource_alignment_param[count] = '\0';
5542 spin_unlock(&resource_alignment_lock);
5543 return count;
5544}
5545
Bjorn Helgaas9738abe2013-04-12 11:20:03 -06005546static ssize_t pci_get_resource_alignment_param(char *buf, size_t size)
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005547{
5548 size_t count;
5549 spin_lock(&resource_alignment_lock);
5550 count = snprintf(buf, size, "%s", resource_alignment_param);
5551 spin_unlock(&resource_alignment_lock);
5552 return count;
5553}
5554
5555static ssize_t pci_resource_alignment_show(struct bus_type *bus, char *buf)
5556{
5557 return pci_get_resource_alignment_param(buf, PAGE_SIZE);
5558}
5559
5560static ssize_t pci_resource_alignment_store(struct bus_type *bus,
5561 const char *buf, size_t count)
5562{
5563 return pci_set_resource_alignment_param(buf, count);
5564}
5565
Ben Dooks21751a92016-06-09 11:42:13 +01005566static BUS_ATTR(resource_alignment, 0644, pci_resource_alignment_show,
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005567 pci_resource_alignment_store);
5568
5569static int __init pci_resource_alignment_sysfs_init(void)
5570{
5571 return bus_create_file(&pci_bus_type,
5572 &bus_attr_resource_alignment);
5573}
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005574late_initcall(pci_resource_alignment_sysfs_init);
5575
Bill Pemberton15856ad2012-11-21 15:35:00 -05005576static void pci_no_domains(void)
Jeff Garzik32a2eea2007-10-11 16:57:27 -04005577{
5578#ifdef CONFIG_PCI_DOMAINS
5579 pci_domains_supported = 0;
5580#endif
5581}
5582
Liviu Dudau41e5c0f2014-09-29 15:29:27 +01005583#ifdef CONFIG_PCI_DOMAINS
5584static atomic_t __domain_nr = ATOMIC_INIT(-1);
5585
5586int pci_get_new_domain_nr(void)
5587{
5588 return atomic_inc_return(&__domain_nr);
5589}
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -07005590
5591#ifdef CONFIG_PCI_DOMAINS_GENERIC
Tomasz Nowicki1a4f93f2016-06-10 21:55:15 +02005592static int of_pci_bus_find_domain_nr(struct device *parent)
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -07005593{
5594 static int use_dt_domains = -1;
Krzysztof =?utf-8?Q?Ha=C5=82asa?=54c6e2d2016-03-01 07:07:18 +01005595 int domain = -1;
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -07005596
Krzysztof =?utf-8?Q?Ha=C5=82asa?=54c6e2d2016-03-01 07:07:18 +01005597 if (parent)
5598 domain = of_get_pci_domain_nr(parent->of_node);
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -07005599 /*
5600 * Check DT domain and use_dt_domains values.
5601 *
5602 * If DT domain property is valid (domain >= 0) and
5603 * use_dt_domains != 0, the DT assignment is valid since this means
5604 * we have not previously allocated a domain number by using
5605 * pci_get_new_domain_nr(); we should also update use_dt_domains to
5606 * 1, to indicate that we have just assigned a domain number from
5607 * DT.
5608 *
5609 * If DT domain property value is not valid (ie domain < 0), and we
5610 * have not previously assigned a domain number from DT
5611 * (use_dt_domains != 1) we should assign a domain number by
5612 * using the:
5613 *
5614 * pci_get_new_domain_nr()
5615 *
5616 * API and update the use_dt_domains value to keep track of method we
5617 * are using to assign domain numbers (use_dt_domains = 0).
5618 *
5619 * All other combinations imply we have a platform that is trying
5620 * to mix domain numbers obtained from DT and pci_get_new_domain_nr(),
5621 * which is a recipe for domain mishandling and it is prevented by
5622 * invalidating the domain value (domain = -1) and printing a
5623 * corresponding error.
5624 */
5625 if (domain >= 0 && use_dt_domains) {
5626 use_dt_domains = 1;
5627 } else if (domain < 0 && use_dt_domains != 1) {
5628 use_dt_domains = 0;
5629 domain = pci_get_new_domain_nr();
5630 } else {
Rob Herringb63773a2017-07-18 16:43:21 -05005631 dev_err(parent, "Node %pOF has inconsistent \"linux,pci-domain\" property in DT\n",
5632 parent->of_node);
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -07005633 domain = -1;
5634 }
5635
Tomasz Nowicki9c7cb892016-06-10 21:55:14 +02005636 return domain;
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -07005637}
Tomasz Nowicki1a4f93f2016-06-10 21:55:15 +02005638
5639int pci_bus_find_domain_nr(struct pci_bus *bus, struct device *parent)
5640{
Tomasz Nowicki2ab51dd2016-06-10 15:36:26 -05005641 return acpi_disabled ? of_pci_bus_find_domain_nr(parent) :
5642 acpi_pci_bus_find_domain_nr(bus);
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -07005643}
5644#endif
Liviu Dudau41e5c0f2014-09-29 15:29:27 +01005645#endif
5646
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -07005647/**
Taku Izumi642c92d2012-10-30 15:26:18 +09005648 * pci_ext_cfg_avail - can we access extended PCI config space?
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -07005649 *
5650 * Returns 1 if we can access PCI extended config space (offsets
5651 * greater than 0xff). This is the default implementation. Architecture
5652 * implementations can override this.
5653 */
Taku Izumi642c92d2012-10-30 15:26:18 +09005654int __weak pci_ext_cfg_avail(void)
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -07005655{
5656 return 1;
5657}
5658
Benjamin Herrenschmidt2d1c8612009-12-09 17:52:13 +11005659void __weak pci_fixup_cardbus(struct pci_bus *bus)
5660{
5661}
5662EXPORT_SYMBOL(pci_fixup_cardbus);
5663
Al Viroad04d312008-11-22 17:37:14 +00005664static int __init pci_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665{
5666 while (str) {
5667 char *k = strchr(str, ',');
5668 if (k)
5669 *k++ = 0;
5670 if (*str && (str = pcibios_setup(str)) && *str) {
Matthew Wilcox309e57d2006-03-05 22:33:34 -07005671 if (!strcmp(str, "nomsi")) {
5672 pci_no_msi();
Randy Dunlap7f785762007-10-05 13:17:58 -07005673 } else if (!strcmp(str, "noaer")) {
5674 pci_no_aer();
Yinghai Lub55438f2012-02-23 19:23:30 -08005675 } else if (!strncmp(str, "realloc=", 8)) {
5676 pci_realloc_get_opt(str + 8);
Ram Paif483d392011-07-07 11:19:10 -07005677 } else if (!strncmp(str, "realloc", 7)) {
Yinghai Lub55438f2012-02-23 19:23:30 -08005678 pci_realloc_get_opt("on");
Jeff Garzik32a2eea2007-10-11 16:57:27 -04005679 } else if (!strcmp(str, "nodomains")) {
5680 pci_no_domains();
Rafael J. Wysocki6748dcc2012-03-01 00:06:33 +01005681 } else if (!strncmp(str, "noari", 5)) {
5682 pcie_ari_disabled = true;
Atsushi Nemoto4516a612007-02-05 16:36:06 -08005683 } else if (!strncmp(str, "cbiosize=", 9)) {
5684 pci_cardbus_io_size = memparse(str + 9, &str);
5685 } else if (!strncmp(str, "cbmemsize=", 10)) {
5686 pci_cardbus_mem_size = memparse(str + 10, &str);
Yuji Shimada32a9a6822009-03-16 17:13:39 +09005687 } else if (!strncmp(str, "resource_alignment=", 19)) {
5688 pci_set_resource_alignment_param(str + 19,
5689 strlen(str + 19));
Andrew Patterson43c16402009-04-22 16:52:09 -06005690 } else if (!strncmp(str, "ecrc=", 5)) {
5691 pcie_ecrc_get_policy(str + 5);
Eric W. Biederman28760482009-09-09 14:09:24 -07005692 } else if (!strncmp(str, "hpiosize=", 9)) {
5693 pci_hotplug_io_size = memparse(str + 9, &str);
5694 } else if (!strncmp(str, "hpmemsize=", 10)) {
5695 pci_hotplug_mem_size = memparse(str + 10, &str);
Keith Busche16b4662016-07-21 21:40:28 -06005696 } else if (!strncmp(str, "hpbussize=", 10)) {
5697 pci_hotplug_bus_size =
5698 simple_strtoul(str + 10, &str, 0);
5699 if (pci_hotplug_bus_size > 0xff)
5700 pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;
Jon Mason5f39e672011-10-03 09:50:20 -05005701 } else if (!strncmp(str, "pcie_bus_tune_off", 17)) {
5702 pcie_bus_config = PCIE_BUS_TUNE_OFF;
Jon Masonb03e7492011-07-20 15:20:54 -05005703 } else if (!strncmp(str, "pcie_bus_safe", 13)) {
5704 pcie_bus_config = PCIE_BUS_SAFE;
5705 } else if (!strncmp(str, "pcie_bus_perf", 13)) {
5706 pcie_bus_config = PCIE_BUS_PERFORMANCE;
Jon Mason5f39e672011-10-03 09:50:20 -05005707 } else if (!strncmp(str, "pcie_bus_peer2peer", 18)) {
5708 pcie_bus_config = PCIE_BUS_PEER2PEER;
Bjorn Helgaas284f5f92012-04-30 15:21:02 -06005709 } else if (!strncmp(str, "pcie_scan_all", 13)) {
5710 pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS);
Matthew Wilcox309e57d2006-03-05 22:33:34 -07005711 } else {
5712 printk(KERN_ERR "PCI: Unknown option `%s'\n",
5713 str);
5714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 }
5716 str = k;
5717 }
Andi Kleen0637a702006-09-26 10:52:41 +02005718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719}
Andi Kleen0637a702006-09-26 10:52:41 +02005720early_param("pci", pci_setup);