blob: 3c84cc6bc75a965e64a56ecc57da29bd261d85e4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * PCI Bus Services, see include/linux/pci.h for further explanation.
3 *
4 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
5 * David Mosberger-Tang
6 *
7 * Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz>
8 */
9
10#include <linux/kernel.h>
11#include <linux/delay.h>
12#include <linux/init.h>
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -070013#include <linux/of.h>
14#include <linux/of_pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/pci.h>
David Brownell075c1772007-04-26 00:12:06 -070016#include <linux/pm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/spinlock.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080020#include <linux/string.h>
vignesh babu229f5af2007-08-13 18:23:14 +053021#include <linux/log2.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080022#include <linux/pci-aspm.h>
Stephen Rothwellc300bd2fb2008-07-10 02:16:44 +020023#include <linux/pm_wakeup.h>
Sheng Yang8dd7f802008-10-21 17:38:25 +080024#include <linux/interrupt.h>
Yuji Shimada32a9a6822009-03-16 17:13:39 +090025#include <linux/device.h>
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010026#include <linux/pm_runtime.h>
Alex Williamson608c3882013-08-08 14:09:43 -060027#include <linux/pci_hotplug.h>
Bjorn Helgaas284f5f92012-04-30 15:21:02 -060028#include <asm-generic/pci-bridge.h>
Yuji Shimada32a9a6822009-03-16 17:13:39 +090029#include <asm/setup.h>
Greg KHbc56b9e2005-04-08 14:53:31 +090030#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Alan Stern00240c32009-04-27 13:33:16 -040032const char *pci_power_names[] = {
33 "error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown",
34};
35EXPORT_SYMBOL_GPL(pci_power_names);
36
Rafael J. Wysocki93177a72010-01-02 22:57:24 +010037int isa_dma_bridge_buggy;
38EXPORT_SYMBOL(isa_dma_bridge_buggy);
39
40int pci_pci_problems;
41EXPORT_SYMBOL(pci_pci_problems);
42
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +010043unsigned int pci_pm_d3_delay;
44
Matthew Garrettdf17e622010-10-04 14:22:29 -040045static void pci_pme_list_scan(struct work_struct *work);
46
47static LIST_HEAD(pci_pme_list);
48static DEFINE_MUTEX(pci_pme_list_mutex);
49static DECLARE_DELAYED_WORK(pci_pme_work, pci_pme_list_scan);
50
51struct pci_pme_device {
52 struct list_head list;
53 struct pci_dev *dev;
54};
55
56#define PME_TIMEOUT 1000 /* How long between PME checks */
57
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +010058static void pci_dev_d3_sleep(struct pci_dev *dev)
59{
60 unsigned int delay = dev->d3_delay;
61
62 if (delay < pci_pm_d3_delay)
63 delay = pci_pm_d3_delay;
64
65 msleep(delay);
66}
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Jeff Garzik32a2eea2007-10-11 16:57:27 -040068#ifdef CONFIG_PCI_DOMAINS
69int pci_domains_supported = 1;
70#endif
71
Atsushi Nemoto4516a612007-02-05 16:36:06 -080072#define DEFAULT_CARDBUS_IO_SIZE (256)
73#define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
74/* pci=cbmemsize=nnM,cbiosize=nn can override this */
75unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
76unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
77
Eric W. Biederman28760482009-09-09 14:09:24 -070078#define DEFAULT_HOTPLUG_IO_SIZE (256)
79#define DEFAULT_HOTPLUG_MEM_SIZE (2*1024*1024)
80/* pci=hpmemsize=nnM,hpiosize=nn can override this */
81unsigned long pci_hotplug_io_size = DEFAULT_HOTPLUG_IO_SIZE;
82unsigned long pci_hotplug_mem_size = DEFAULT_HOTPLUG_MEM_SIZE;
83
Jon Mason5f39e672011-10-03 09:50:20 -050084enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_TUNE_OFF;
Jon Masonb03e7492011-07-20 15:20:54 -050085
Jesse Barnesac1aa472009-10-26 13:20:44 -070086/*
87 * The default CLS is used if arch didn't set CLS explicitly and not
88 * all pci devices agree on the same value. Arch can override either
89 * the dfl or actual value as it sees fit. Don't forget this is
90 * measured in 32-bit words, not bytes.
91 */
Bill Pemberton15856ad2012-11-21 15:35:00 -050092u8 pci_dfl_cache_line_size = L1_CACHE_BYTES >> 2;
Jesse Barnesac1aa472009-10-26 13:20:44 -070093u8 pci_cache_line_size;
94
Myron Stowe96c55902011-10-28 15:48:38 -060095/*
96 * If we set up a device for bus mastering, we need to check the latency
97 * timer as certain BIOSes forget to set it properly.
98 */
99unsigned int pcibios_max_latency = 255;
100
Rafael J. Wysocki6748dcc2012-03-01 00:06:33 +0100101/* If set, the PCIe ARI capability will not be used. */
102static bool pcie_ari_disabled;
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/**
105 * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
106 * @bus: pointer to PCI bus structure to search
107 *
108 * Given a PCI bus, returns the highest PCI bus number present in the set
109 * including the given PCI bus and its list of child PCI buses.
110 */
Ryan Desfosses07656d83082014-04-11 01:01:53 -0400111unsigned char pci_bus_max_busnr(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Yijing Wang94e6a9b2014-02-13 21:14:03 +0800113 struct pci_bus *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned char max, n;
115
Yinghai Lub918c622012-05-17 18:51:11 -0700116 max = bus->busn_res.end;
Yijing Wang94e6a9b2014-02-13 21:14:03 +0800117 list_for_each_entry(tmp, &bus->children, node) {
118 n = pci_bus_max_busnr(tmp);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400119 if (n > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 max = n;
121 }
122 return max;
123}
Kristen Accardib82db5c2006-01-17 16:56:56 -0800124EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Andrew Morton1684f5d2008-12-01 14:30:30 -0800126#ifdef CONFIG_HAS_IOMEM
127void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
128{
129 /*
130 * Make sure the BAR is actually a memory resource, not an IO resource
131 */
132 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
133 WARN_ON(1);
134 return NULL;
135 }
136 return ioremap_nocache(pci_resource_start(pdev, bar),
137 pci_resource_len(pdev, bar));
138}
139EXPORT_SYMBOL_GPL(pci_ioremap_bar);
140#endif
141
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100142#define PCI_FIND_CAP_TTL 48
143
144static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
145 u8 pos, int cap, int *ttl)
Roland Dreier24a4e372005-10-28 17:35:34 -0700146{
147 u8 id;
Sean O. Stalley55db3202015-04-02 14:10:19 -0700148 u16 ent;
149
150 pci_bus_read_config_byte(bus, devfn, pos, &pos);
Roland Dreier24a4e372005-10-28 17:35:34 -0700151
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100152 while ((*ttl)--) {
Roland Dreier24a4e372005-10-28 17:35:34 -0700153 if (pos < 0x40)
154 break;
155 pos &= ~3;
Sean O. Stalley55db3202015-04-02 14:10:19 -0700156 pci_bus_read_config_word(bus, devfn, pos, &ent);
157
158 id = ent & 0xff;
Roland Dreier24a4e372005-10-28 17:35:34 -0700159 if (id == 0xff)
160 break;
161 if (id == cap)
162 return pos;
Sean O. Stalley55db3202015-04-02 14:10:19 -0700163 pos = (ent >> 8);
Roland Dreier24a4e372005-10-28 17:35:34 -0700164 }
165 return 0;
166}
167
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100168static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
169 u8 pos, int cap)
170{
171 int ttl = PCI_FIND_CAP_TTL;
172
173 return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
174}
175
Roland Dreier24a4e372005-10-28 17:35:34 -0700176int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
177{
178 return __pci_find_next_cap(dev->bus, dev->devfn,
179 pos + PCI_CAP_LIST_NEXT, cap);
180}
181EXPORT_SYMBOL_GPL(pci_find_next_capability);
182
Michael Ellermand3bac112006-11-22 18:26:16 +1100183static int __pci_bus_find_cap_start(struct pci_bus *bus,
184 unsigned int devfn, u8 hdr_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
189 if (!(status & PCI_STATUS_CAP_LIST))
190 return 0;
191
192 switch (hdr_type) {
193 case PCI_HEADER_TYPE_NORMAL:
194 case PCI_HEADER_TYPE_BRIDGE:
Michael Ellermand3bac112006-11-22 18:26:16 +1100195 return PCI_CAPABILITY_LIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 case PCI_HEADER_TYPE_CARDBUS:
Michael Ellermand3bac112006-11-22 18:26:16 +1100197 return PCI_CB_CAPABILITY_LIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 default:
199 return 0;
200 }
Michael Ellermand3bac112006-11-22 18:26:16 +1100201
202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205/**
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700206 * pci_find_capability - query for devices' capabilities
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * @dev: PCI device to query
208 * @cap: capability code
209 *
210 * Tell if a device supports a given PCI capability.
211 * Returns the address of the requested capability structure within the
212 * device's PCI configuration space or 0 in case the device does not
213 * support it. Possible values for @cap:
214 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700215 * %PCI_CAP_ID_PM Power Management
216 * %PCI_CAP_ID_AGP Accelerated Graphics Port
217 * %PCI_CAP_ID_VPD Vital Product Data
218 * %PCI_CAP_ID_SLOTID Slot Identification
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * %PCI_CAP_ID_MSI Message Signalled Interrupts
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700220 * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * %PCI_CAP_ID_PCIX PCI-X
222 * %PCI_CAP_ID_EXP PCI Express
223 */
224int pci_find_capability(struct pci_dev *dev, int cap)
225{
Michael Ellermand3bac112006-11-22 18:26:16 +1100226 int pos;
227
228 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
229 if (pos)
230 pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
231
232 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600234EXPORT_SYMBOL(pci_find_capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236/**
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700237 * pci_bus_find_capability - query for devices' capabilities
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * @bus: the PCI bus to query
239 * @devfn: PCI device to query
240 * @cap: capability code
241 *
242 * Like pci_find_capability() but works for pci devices that do not have a
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700243 * pci_dev structure set up yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 *
245 * Returns the address of the requested capability structure within the
246 * device's PCI configuration space or 0 in case the device does not
247 * support it.
248 */
249int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
250{
Michael Ellermand3bac112006-11-22 18:26:16 +1100251 int pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 u8 hdr_type;
253
254 pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
255
Michael Ellermand3bac112006-11-22 18:26:16 +1100256 pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
257 if (pos)
258 pos = __pci_find_next_cap(bus, devfn, pos, cap);
259
260 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600262EXPORT_SYMBOL(pci_bus_find_capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264/**
Bjorn Helgaas44a9a362012-07-13 14:24:59 -0600265 * pci_find_next_ext_capability - Find an extended capability
266 * @dev: PCI device to query
267 * @start: address at which to start looking (0 to start at beginning of list)
268 * @cap: capability code
269 *
270 * Returns the address of the next matching extended capability structure
271 * within the device's PCI configuration space or 0 if the device does
272 * not support it. Some capabilities can occur several times, e.g., the
273 * vendor-specific capability, and this provides a way to find them all.
274 */
275int pci_find_next_ext_capability(struct pci_dev *dev, int start, int cap)
276{
277 u32 header;
278 int ttl;
279 int pos = PCI_CFG_SPACE_SIZE;
280
281 /* minimum 8 bytes per capability */
282 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
283
284 if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
285 return 0;
286
287 if (start)
288 pos = start;
289
290 if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
291 return 0;
292
293 /*
294 * If we have no capabilities, this is indicated by cap ID,
295 * cap version and next pointer all being 0.
296 */
297 if (header == 0)
298 return 0;
299
300 while (ttl-- > 0) {
301 if (PCI_EXT_CAP_ID(header) == cap && pos != start)
302 return pos;
303
304 pos = PCI_EXT_CAP_NEXT(header);
305 if (pos < PCI_CFG_SPACE_SIZE)
306 break;
307
308 if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
309 break;
310 }
311
312 return 0;
313}
314EXPORT_SYMBOL_GPL(pci_find_next_ext_capability);
315
316/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 * pci_find_ext_capability - Find an extended capability
318 * @dev: PCI device to query
319 * @cap: capability code
320 *
321 * Returns the address of the requested extended capability structure
322 * within the device's PCI configuration space or 0 if the device does
323 * not support it. Possible values for @cap:
324 *
325 * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
326 * %PCI_EXT_CAP_ID_VC Virtual Channel
327 * %PCI_EXT_CAP_ID_DSN Device Serial Number
328 * %PCI_EXT_CAP_ID_PWR Power Budgeting
329 */
330int pci_find_ext_capability(struct pci_dev *dev, int cap)
331{
Bjorn Helgaas44a9a362012-07-13 14:24:59 -0600332 return pci_find_next_ext_capability(dev, 0, cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
Brice Goglin3a720d72006-05-23 06:10:01 -0400334EXPORT_SYMBOL_GPL(pci_find_ext_capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100336static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
337{
338 int rc, ttl = PCI_FIND_CAP_TTL;
339 u8 cap, mask;
340
341 if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
342 mask = HT_3BIT_CAP_MASK;
343 else
344 mask = HT_5BIT_CAP_MASK;
345
346 pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
347 PCI_CAP_ID_HT, &ttl);
348 while (pos) {
349 rc = pci_read_config_byte(dev, pos + 3, &cap);
350 if (rc != PCIBIOS_SUCCESSFUL)
351 return 0;
352
353 if ((cap & mask) == ht_cap)
354 return pos;
355
Brice Goglin47a4d5b2007-01-10 23:15:29 -0800356 pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
357 pos + PCI_CAP_LIST_NEXT,
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100358 PCI_CAP_ID_HT, &ttl);
359 }
360
361 return 0;
362}
363/**
364 * pci_find_next_ht_capability - query a device's Hypertransport capabilities
365 * @dev: PCI device to query
366 * @pos: Position from which to continue searching
367 * @ht_cap: Hypertransport capability code
368 *
369 * To be used in conjunction with pci_find_ht_capability() to search for
370 * all capabilities matching @ht_cap. @pos should always be a value returned
371 * from pci_find_ht_capability().
372 *
373 * NB. To be 100% safe against broken PCI devices, the caller should take
374 * steps to avoid an infinite loop.
375 */
376int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
377{
378 return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
379}
380EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
381
382/**
383 * pci_find_ht_capability - query a device's Hypertransport capabilities
384 * @dev: PCI device to query
385 * @ht_cap: Hypertransport capability code
386 *
387 * Tell if a device supports a given Hypertransport capability.
388 * Returns an address within the device's PCI configuration space
389 * or 0 in case the device does not support the request capability.
390 * The address points to the PCI capability, of type PCI_CAP_ID_HT,
391 * which has a Hypertransport capability matching @ht_cap.
392 */
393int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
394{
395 int pos;
396
397 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
398 if (pos)
399 pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
400
401 return pos;
402}
403EXPORT_SYMBOL_GPL(pci_find_ht_capability);
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405/**
406 * pci_find_parent_resource - return resource region of parent bus of given region
407 * @dev: PCI device structure contains resources to be searched
408 * @res: child resource record for which parent is sought
409 *
410 * For given resource region of given device, return the resource
Bjorn Helgaasf44116a2014-02-26 11:25:58 -0700411 * region of parent bus the given region is contained in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400413struct resource *pci_find_parent_resource(const struct pci_dev *dev,
414 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 const struct pci_bus *bus = dev->bus;
Bjorn Helgaasf44116a2014-02-26 11:25:58 -0700417 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700420 pci_bus_for_each_resource(bus, r, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (!r)
422 continue;
Bjorn Helgaasf44116a2014-02-26 11:25:58 -0700423 if (res->start && resource_contains(r, res)) {
424
425 /*
426 * If the window is prefetchable but the BAR is
427 * not, the allocator made a mistake.
428 */
429 if (r->flags & IORESOURCE_PREFETCH &&
430 !(res->flags & IORESOURCE_PREFETCH))
431 return NULL;
432
433 /*
434 * If we're below a transparent bridge, there may
435 * be both a positively-decoded aperture and a
436 * subtractively-decoded region that contain the BAR.
437 * We want the positively-decoded one, so this depends
438 * on pci_bus_for_each_resource() giving us those
439 * first.
440 */
441 return r;
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
Bjorn Helgaasf44116a2014-02-26 11:25:58 -0700444 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600446EXPORT_SYMBOL(pci_find_parent_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448/**
Alex Williamson157e8762013-12-17 16:43:39 -0700449 * pci_wait_for_pending - wait for @mask bit(s) to clear in status word @pos
450 * @dev: the PCI device to operate on
451 * @pos: config space offset of status word
452 * @mask: mask of bit(s) to care about in status word
453 *
454 * Return 1 when mask bit(s) in status word clear, 0 otherwise.
455 */
456int pci_wait_for_pending(struct pci_dev *dev, int pos, u16 mask)
457{
458 int i;
459
460 /* Wait for Transaction Pending bit clean */
461 for (i = 0; i < 4; i++) {
462 u16 status;
463 if (i)
464 msleep((1 << (i - 1)) * 100);
465
466 pci_read_config_word(dev, pos, &status);
467 if (!(status & mask))
468 return 1;
469 }
470
471 return 0;
472}
473
474/**
John W. Linville064b53db2005-07-27 10:19:44 -0400475 * pci_restore_bars - restore a devices BAR values (e.g. after wake-up)
476 * @dev: PCI device to have its BARs restored
477 *
478 * Restore the BAR values for a given device, so as to make it
479 * accessible by its driver.
480 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400481static void pci_restore_bars(struct pci_dev *dev)
John W. Linville064b53db2005-07-27 10:19:44 -0400482{
Yu Zhaobc5f5a82008-11-22 02:40:00 +0800483 int i;
John W. Linville064b53db2005-07-27 10:19:44 -0400484
Yu Zhaobc5f5a82008-11-22 02:40:00 +0800485 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
Yu Zhao14add802008-11-22 02:38:52 +0800486 pci_update_resource(dev, i);
John W. Linville064b53db2005-07-27 10:19:44 -0400487}
488
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200489static struct pci_platform_pm_ops *pci_platform_pm;
490
491int pci_set_platform_pm(struct pci_platform_pm_ops *ops)
492{
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200493 if (!ops->is_manageable || !ops->set_state || !ops->choose_state
Rafael J. Wysockid2e5f0c2012-12-23 00:02:44 +0100494 || !ops->sleep_wake)
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200495 return -EINVAL;
496 pci_platform_pm = ops;
497 return 0;
498}
499
500static inline bool platform_pci_power_manageable(struct pci_dev *dev)
501{
502 return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
503}
504
505static inline int platform_pci_set_power_state(struct pci_dev *dev,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400506 pci_power_t t)
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200507{
508 return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
509}
510
511static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
512{
513 return pci_platform_pm ?
514 pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
515}
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700516
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200517static inline int platform_pci_sleep_wake(struct pci_dev *dev, bool enable)
518{
519 return pci_platform_pm ?
520 pci_platform_pm->sleep_wake(dev, enable) : -ENODEV;
521}
522
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100523static inline int platform_pci_run_wake(struct pci_dev *dev, bool enable)
524{
525 return pci_platform_pm ?
526 pci_platform_pm->run_wake(dev, enable) : -ENODEV;
527}
528
Rafael J. Wysockibac2a902015-01-21 02:17:42 +0100529static inline bool platform_pci_need_resume(struct pci_dev *dev)
530{
531 return pci_platform_pm ? pci_platform_pm->need_resume(dev) : false;
532}
533
John W. Linville064b53db2005-07-27 10:19:44 -0400534/**
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200535 * pci_raw_set_power_state - Use PCI PM registers to set the power state of
536 * given PCI device
537 * @dev: PCI device to handle.
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200538 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 *
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200540 * RETURN VALUE:
541 * -EINVAL if the requested state is invalid.
542 * -EIO if device does not support PCI PM or its PM capabilities register has a
543 * wrong version, or device doesn't support the requested state.
544 * 0 if device already is in the requested state.
545 * 0 if device's power state has been successfully changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 */
Rafael J. Wysockif00a20e2009-03-16 22:40:08 +0100547static int pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200549 u16 pmcsr;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200550 bool need_restore = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Rafael J. Wysocki4a865902009-03-16 22:40:36 +0100552 /* Check if we're already there */
553 if (dev->current_state == state)
554 return 0;
555
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200556 if (!dev->pm_cap)
Andrew Lunncca03de2007-07-09 11:55:58 -0700557 return -EIO;
558
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200559 if (state < PCI_D0 || state > PCI_D3hot)
560 return -EINVAL;
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* Validate current state:
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700563 * Can enter D0 from any state, but if we can only go deeper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 * to sleep if we're already in a low power state
565 */
Rafael J. Wysocki4a865902009-03-16 22:40:36 +0100566 if (state != PCI_D0 && dev->current_state <= PCI_D3cold
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200567 && dev->current_state > state) {
Ryan Desfosses227f0642014-04-18 20:13:50 -0400568 dev_err(&dev->dev, "invalid power transition (from state %d to %d)\n",
569 dev->current_state, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return -EINVAL;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* check if this device supports the desired state */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200574 if ((state == PCI_D1 && !dev->d1_support)
575 || (state == PCI_D2 && !dev->d2_support))
Daniel Ritz3fe9d192005-08-17 15:32:19 -0700576 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200578 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
John W. Linville064b53db2005-07-27 10:19:44 -0400579
John W. Linville32a36582005-09-14 09:52:42 -0400580 /* If we're (effectively) in D3, force entire word to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 * This doesn't affect PME_Status, disables PME_En, and
582 * sets PowerState to 0.
583 */
John W. Linville32a36582005-09-14 09:52:42 -0400584 switch (dev->current_state) {
John W. Linvilled3535fb2005-09-28 17:50:51 -0400585 case PCI_D0:
586 case PCI_D1:
587 case PCI_D2:
588 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
589 pmcsr |= state;
590 break;
Rafael J. Wysockif62795f2009-05-18 22:51:12 +0200591 case PCI_D3hot:
592 case PCI_D3cold:
John W. Linville32a36582005-09-14 09:52:42 -0400593 case PCI_UNKNOWN: /* Boot-up */
594 if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
Rafael J. Wysockif00a20e2009-03-16 22:40:08 +0100595 && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200596 need_restore = true;
John W. Linville32a36582005-09-14 09:52:42 -0400597 /* Fall-through: force to D0 */
John W. Linville32a36582005-09-14 09:52:42 -0400598 default:
John W. Linvilled3535fb2005-09-28 17:50:51 -0400599 pmcsr = 0;
John W. Linville32a36582005-09-14 09:52:42 -0400600 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602
603 /* enter specified state */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200604 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /* Mandatory power management transition delays */
607 /* see PCI PM 1.1 5.6.1 table 18 */
608 if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +0100609 pci_dev_d3_sleep(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 else if (state == PCI_D2 || dev->current_state == PCI_D2)
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100611 udelay(PCI_PM_D2_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Rafael J. Wysockie13cdbd2009-10-05 00:48:40 +0200613 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
614 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
615 if (dev->current_state != state && printk_ratelimit())
Ryan Desfosses227f0642014-04-18 20:13:50 -0400616 dev_info(&dev->dev, "Refused to change power state, currently in D%d\n",
617 dev->current_state);
John W. Linville064b53db2005-07-27 10:19:44 -0400618
Huang Ying448bd852012-06-23 10:23:51 +0800619 /*
620 * According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
John W. Linville064b53db2005-07-27 10:19:44 -0400621 * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
622 * from D3hot to D0 _may_ perform an internal reset, thereby
623 * going to "D0 Uninitialized" rather than "D0 Initialized".
624 * For example, at least some versions of the 3c905B and the
625 * 3c556B exhibit this behaviour.
626 *
627 * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
628 * devices in a D3hot state at boot. Consequently, we need to
629 * restore at least the BARs so that the device will be
630 * accessible to its driver.
631 */
632 if (need_restore)
633 pci_restore_bars(dev);
634
Rafael J. Wysockif00a20e2009-03-16 22:40:08 +0100635 if (dev->bus->self)
Shaohua Li7d715a62008-02-25 09:46:41 +0800636 pcie_aspm_pm_state_change(dev->bus->self);
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return 0;
639}
640
641/**
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200642 * pci_update_current_state - Read PCI power state of given device from its
643 * PCI PM registers and cache it
644 * @dev: PCI device to handle.
Rafael J. Wysockif06fc0b2008-12-27 16:30:52 +0100645 * @state: State to cache in case the device doesn't have the PM capability
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200646 */
Rafael J. Wysocki734104292009-01-07 13:07:15 +0100647void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200648{
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200649 if (dev->pm_cap) {
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200650 u16 pmcsr;
651
Huang Ying448bd852012-06-23 10:23:51 +0800652 /*
653 * Configuration space is not accessible for device in
654 * D3cold, so just keep or set D3cold for safety
655 */
656 if (dev->current_state == PCI_D3cold)
657 return;
658 if (state == PCI_D3cold) {
659 dev->current_state = PCI_D3cold;
660 return;
661 }
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200662 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200663 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
Rafael J. Wysockif06fc0b2008-12-27 16:30:52 +0100664 } else {
665 dev->current_state = state;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200666 }
667}
668
669/**
Rafael J. Wysockidb288c92012-07-05 15:20:00 -0600670 * pci_power_up - Put the given device into D0 forcibly
671 * @dev: PCI device to power up
672 */
673void pci_power_up(struct pci_dev *dev)
674{
675 if (platform_pci_power_manageable(dev))
676 platform_pci_set_power_state(dev, PCI_D0);
677
678 pci_raw_set_power_state(dev, PCI_D0);
679 pci_update_current_state(dev, PCI_D0);
680}
681
682/**
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100683 * pci_platform_power_transition - Use platform to change device power state
684 * @dev: PCI device to handle.
685 * @state: State to put the device into.
686 */
687static int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
688{
689 int error;
690
691 if (platform_pci_power_manageable(dev)) {
692 error = platform_pci_set_power_state(dev, state);
693 if (!error)
694 pci_update_current_state(dev, state);
Rafael J. Wysocki769ba722013-04-12 13:58:17 +0000695 } else
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100696 error = -ENODEV;
Rafael J. Wysocki769ba722013-04-12 13:58:17 +0000697
698 if (error && !dev->pm_cap) /* Fall back to PCI_D0 */
699 dev->current_state = PCI_D0;
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100700
701 return error;
702}
703
704/**
Stephen Hemminger0b950f02014-01-10 17:14:48 -0700705 * pci_wakeup - Wake up a PCI device
706 * @pci_dev: Device to handle.
707 * @ign: ignored parameter
708 */
709static int pci_wakeup(struct pci_dev *pci_dev, void *ign)
710{
711 pci_wakeup_event(pci_dev);
712 pm_request_resume(&pci_dev->dev);
713 return 0;
714}
715
716/**
717 * pci_wakeup_bus - Walk given bus and wake up devices on it
718 * @bus: Top bus of the subtree to walk.
719 */
720static void pci_wakeup_bus(struct pci_bus *bus)
721{
722 if (bus)
723 pci_walk_bus(bus, pci_wakeup, NULL);
724}
725
726/**
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100727 * __pci_start_power_transition - Start power transition of a PCI device
728 * @dev: PCI device to handle.
729 * @state: State to put the device into.
730 */
731static void __pci_start_power_transition(struct pci_dev *dev, pci_power_t state)
732{
Huang Ying448bd852012-06-23 10:23:51 +0800733 if (state == PCI_D0) {
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100734 pci_platform_power_transition(dev, PCI_D0);
Huang Ying448bd852012-06-23 10:23:51 +0800735 /*
736 * Mandatory power management transition delays, see
737 * PCI Express Base Specification Revision 2.0 Section
738 * 6.6.1: Conventional Reset. Do not delay for
739 * devices powered on/off by corresponding bridge,
740 * because have already delayed for the bridge.
741 */
742 if (dev->runtime_d3cold) {
743 msleep(dev->d3cold_delay);
744 /*
745 * When powering on a bridge from D3cold, the
746 * whole hierarchy may be powered on into
747 * D0uninitialized state, resume them to give
748 * them a chance to suspend again
749 */
750 pci_wakeup_bus(dev->subordinate);
751 }
752 }
753}
754
755/**
756 * __pci_dev_set_current_state - Set current state of a PCI device
757 * @dev: Device to handle
758 * @data: pointer to state to be set
759 */
760static int __pci_dev_set_current_state(struct pci_dev *dev, void *data)
761{
762 pci_power_t state = *(pci_power_t *)data;
763
764 dev->current_state = state;
765 return 0;
766}
767
768/**
769 * __pci_bus_set_current_state - Walk given bus and set current state of devices
770 * @bus: Top bus of the subtree to walk.
771 * @state: state to be set
772 */
773static void __pci_bus_set_current_state(struct pci_bus *bus, pci_power_t state)
774{
775 if (bus)
776 pci_walk_bus(bus, __pci_dev_set_current_state, &state);
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100777}
778
779/**
780 * __pci_complete_power_transition - Complete power transition of a PCI device
781 * @dev: PCI device to handle.
782 * @state: State to put the device into.
783 *
784 * This function should not be called directly by device drivers.
785 */
786int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
787{
Huang Ying448bd852012-06-23 10:23:51 +0800788 int ret;
789
Rafael J. Wysockidb288c92012-07-05 15:20:00 -0600790 if (state <= PCI_D0)
Huang Ying448bd852012-06-23 10:23:51 +0800791 return -EINVAL;
792 ret = pci_platform_power_transition(dev, state);
793 /* Power off the bridge may power off the whole hierarchy */
794 if (!ret && state == PCI_D3cold)
795 __pci_bus_set_current_state(dev->subordinate, PCI_D3cold);
796 return ret;
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100797}
798EXPORT_SYMBOL_GPL(__pci_complete_power_transition);
799
800/**
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200801 * pci_set_power_state - Set the power state of a PCI device
802 * @dev: PCI device to handle.
803 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
804 *
Nick Andrew877d0312009-01-26 11:06:57 +0100805 * Transition a device to a new power state, using the platform firmware and/or
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200806 * the device's PCI PM registers.
807 *
808 * RETURN VALUE:
809 * -EINVAL if the requested state is invalid.
810 * -EIO if device does not support PCI PM or its PM capabilities register has a
811 * wrong version, or device doesn't support the requested state.
812 * 0 if device already is in the requested state.
813 * 0 if device's power state has been successfully changed.
814 */
815int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
816{
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200817 int error;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200818
819 /* bound the state we're entering */
Huang Ying448bd852012-06-23 10:23:51 +0800820 if (state > PCI_D3cold)
821 state = PCI_D3cold;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200822 else if (state < PCI_D0)
823 state = PCI_D0;
824 else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
825 /*
826 * If the device or the parent bridge do not support PCI PM,
827 * ignore the request if we're doing anything other than putting
828 * it into D0 (which would only happen on boot).
829 */
830 return 0;
831
Rafael J. Wysockidb288c92012-07-05 15:20:00 -0600832 /* Check if we're already there */
833 if (dev->current_state == state)
834 return 0;
835
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100836 __pci_start_power_transition(dev, state);
837
Alan Cox979b1792008-07-24 17:18:38 +0100838 /* This device is quirked not to be put into D3, so
839 don't put it in D3 */
Huang Ying448bd852012-06-23 10:23:51 +0800840 if (state >= PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
Alan Cox979b1792008-07-24 17:18:38 +0100841 return 0;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200842
Huang Ying448bd852012-06-23 10:23:51 +0800843 /*
844 * To put device in D3cold, we put device into D3hot in native
845 * way, then put device into D3cold with platform ops
846 */
847 error = pci_raw_set_power_state(dev, state > PCI_D3hot ?
848 PCI_D3hot : state);
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200849
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100850 if (!__pci_complete_power_transition(dev, state))
851 error = 0;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200852
853 return error;
854}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600855EXPORT_SYMBOL(pci_set_power_state);
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200856
857/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * pci_choose_state - Choose the power state of a PCI device
859 * @dev: PCI device to be suspended
860 * @state: target sleep state for the whole system. This is the value
861 * that is passed to suspend() function.
862 *
863 * Returns PCI power state suitable for given device and given system
864 * message.
865 */
866
867pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
868{
Shaohua Liab826ca2007-07-20 10:03:22 +0800869 pci_power_t ret;
David Shaohua Li0f644742005-03-19 00:15:48 -0500870
Yijing Wang728cdb72013-06-18 16:22:14 +0800871 if (!dev->pm_cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return PCI_D0;
873
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200874 ret = platform_pci_choose_state(dev);
875 if (ret != PCI_POWER_ERROR)
876 return ret;
Pavel Machekca078ba2005-09-03 15:56:57 -0700877
878 switch (state.event) {
879 case PM_EVENT_ON:
880 return PCI_D0;
881 case PM_EVENT_FREEZE:
David Brownellb887d2e2006-08-14 23:11:05 -0700882 case PM_EVENT_PRETHAW:
883 /* REVISIT both freeze and pre-thaw "should" use D0 */
Pavel Machekca078ba2005-09-03 15:56:57 -0700884 case PM_EVENT_SUSPEND:
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100885 case PM_EVENT_HIBERNATE:
Pavel Machekca078ba2005-09-03 15:56:57 -0700886 return PCI_D3hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 default:
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600888 dev_info(&dev->dev, "unrecognized suspend event %d\n",
889 state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 BUG();
891 }
892 return PCI_D0;
893}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894EXPORT_SYMBOL(pci_choose_state);
895
Yu Zhao89858512009-02-16 02:55:47 +0800896#define PCI_EXP_SAVE_REGS 7
897
Alex Williamsonfd0f7f72013-12-17 16:43:45 -0700898static struct pci_cap_saved_state *_pci_find_saved_cap(struct pci_dev *pci_dev,
899 u16 cap, bool extended)
Yinghai Lu34a48762012-02-11 00:18:41 -0800900{
901 struct pci_cap_saved_state *tmp;
Yinghai Lu34a48762012-02-11 00:18:41 -0800902
Sasha Levinb67bfe02013-02-27 17:06:00 -0800903 hlist_for_each_entry(tmp, &pci_dev->saved_cap_space, next) {
Alex Williamsonfd0f7f72013-12-17 16:43:45 -0700904 if (tmp->cap.cap_extended == extended && tmp->cap.cap_nr == cap)
Yinghai Lu34a48762012-02-11 00:18:41 -0800905 return tmp;
906 }
907 return NULL;
908}
909
Alex Williamsonfd0f7f72013-12-17 16:43:45 -0700910struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap)
911{
912 return _pci_find_saved_cap(dev, cap, false);
913}
914
915struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap)
916{
917 return _pci_find_saved_cap(dev, cap, true);
918}
919
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300920static int pci_save_pcie_state(struct pci_dev *dev)
921{
Jiang Liu59875ae2012-07-24 17:20:06 +0800922 int i = 0;
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300923 struct pci_cap_saved_state *save_state;
924 u16 *cap;
925
Jiang Liu59875ae2012-07-24 17:20:06 +0800926 if (!pci_is_pcie(dev))
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300927 return 0;
928
Eric W. Biederman9f355752007-03-08 13:06:13 -0700929 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300930 if (!save_state) {
Harvey Harrisone496b612009-01-07 16:22:37 -0800931 dev_err(&dev->dev, "buffer not found in %s\n", __func__);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300932 return -ENOMEM;
933 }
Jiang Liu59875ae2012-07-24 17:20:06 +0800934
Alex Williamson24a4742f2011-05-10 10:02:11 -0600935 cap = (u16 *)&save_state->cap.data[0];
Jiang Liu59875ae2012-07-24 17:20:06 +0800936 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &cap[i++]);
937 pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]);
938 pcie_capability_read_word(dev, PCI_EXP_SLTCTL, &cap[i++]);
939 pcie_capability_read_word(dev, PCI_EXP_RTCTL, &cap[i++]);
940 pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &cap[i++]);
941 pcie_capability_read_word(dev, PCI_EXP_LNKCTL2, &cap[i++]);
942 pcie_capability_read_word(dev, PCI_EXP_SLTCTL2, &cap[i++]);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300943
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300944 return 0;
945}
946
947static void pci_restore_pcie_state(struct pci_dev *dev)
948{
Jiang Liu59875ae2012-07-24 17:20:06 +0800949 int i = 0;
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300950 struct pci_cap_saved_state *save_state;
951 u16 *cap;
952
953 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
Jiang Liu59875ae2012-07-24 17:20:06 +0800954 if (!save_state)
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300955 return;
Jiang Liu59875ae2012-07-24 17:20:06 +0800956
Alex Williamson24a4742f2011-05-10 10:02:11 -0600957 cap = (u16 *)&save_state->cap.data[0];
Jiang Liu59875ae2012-07-24 17:20:06 +0800958 pcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]);
959 pcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]);
960 pcie_capability_write_word(dev, PCI_EXP_SLTCTL, cap[i++]);
961 pcie_capability_write_word(dev, PCI_EXP_RTCTL, cap[i++]);
962 pcie_capability_write_word(dev, PCI_EXP_DEVCTL2, cap[i++]);
963 pcie_capability_write_word(dev, PCI_EXP_LNKCTL2, cap[i++]);
964 pcie_capability_write_word(dev, PCI_EXP_SLTCTL2, cap[i++]);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300965}
966
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800967
968static int pci_save_pcix_state(struct pci_dev *dev)
969{
Rafael J. Wysocki63f48982008-12-07 22:02:58 +0100970 int pos;
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800971 struct pci_cap_saved_state *save_state;
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800972
973 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
974 if (pos <= 0)
975 return 0;
976
Shaohua Lif34303d2007-12-18 09:56:47 +0800977 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800978 if (!save_state) {
Harvey Harrisone496b612009-01-07 16:22:37 -0800979 dev_err(&dev->dev, "buffer not found in %s\n", __func__);
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800980 return -ENOMEM;
981 }
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800982
Alex Williamson24a4742f2011-05-10 10:02:11 -0600983 pci_read_config_word(dev, pos + PCI_X_CMD,
984 (u16 *)save_state->cap.data);
Rafael J. Wysocki63f48982008-12-07 22:02:58 +0100985
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800986 return 0;
987}
988
989static void pci_restore_pcix_state(struct pci_dev *dev)
990{
991 int i = 0, pos;
992 struct pci_cap_saved_state *save_state;
993 u16 *cap;
994
995 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
996 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
997 if (!save_state || pos <= 0)
998 return;
Alex Williamson24a4742f2011-05-10 10:02:11 -0600999 cap = (u16 *)&save_state->cap.data[0];
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001000
1001 pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001002}
1003
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005/**
1006 * pci_save_state - save the PCI configuration space of a device before suspending
1007 * @dev: - PCI device that we're dealing with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001009int pci_save_state(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 int i;
1012 /* XXX: 100% dword access ok here? */
1013 for (i = 0; i < 16; i++)
Kleber Sacilotto de Souza9e0b5b22009-11-25 00:55:51 -02001014 pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]);
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +01001015 dev->state_saved = true;
Quentin Lambert79e50e72014-09-07 20:03:32 +02001016
1017 i = pci_save_pcie_state(dev);
1018 if (i != 0)
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001019 return i;
Quentin Lambert79e50e72014-09-07 20:03:32 +02001020
1021 i = pci_save_pcix_state(dev);
1022 if (i != 0)
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001023 return i;
Quentin Lambert79e50e72014-09-07 20:03:32 +02001024
Quentin Lambert754834b2014-11-06 17:45:55 +01001025 return pci_save_vc_state(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001027EXPORT_SYMBOL(pci_save_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Rafael J. Wysockiebfc5b82012-04-15 21:40:40 +02001029static void pci_restore_config_dword(struct pci_dev *pdev, int offset,
1030 u32 saved_val, int retry)
1031{
1032 u32 val;
1033
1034 pci_read_config_dword(pdev, offset, &val);
1035 if (val == saved_val)
1036 return;
1037
1038 for (;;) {
Ryan Desfosses227f0642014-04-18 20:13:50 -04001039 dev_dbg(&pdev->dev, "restoring config space at offset %#x (was %#x, writing %#x)\n",
1040 offset, val, saved_val);
Rafael J. Wysockiebfc5b82012-04-15 21:40:40 +02001041 pci_write_config_dword(pdev, offset, saved_val);
1042 if (retry-- <= 0)
1043 return;
1044
1045 pci_read_config_dword(pdev, offset, &val);
1046 if (val == saved_val)
1047 return;
1048
1049 mdelay(1);
1050 }
1051}
1052
Rafael J. Wysockia6cb9ee2012-04-16 23:07:50 +02001053static void pci_restore_config_space_range(struct pci_dev *pdev,
1054 int start, int end, int retry)
Rafael J. Wysockiebfc5b82012-04-15 21:40:40 +02001055{
1056 int index;
1057
1058 for (index = end; index >= start; index--)
1059 pci_restore_config_dword(pdev, 4 * index,
1060 pdev->saved_config_space[index],
1061 retry);
1062}
1063
Rafael J. Wysockia6cb9ee2012-04-16 23:07:50 +02001064static void pci_restore_config_space(struct pci_dev *pdev)
1065{
1066 if (pdev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
1067 pci_restore_config_space_range(pdev, 10, 15, 0);
1068 /* Restore BARs before the command register. */
1069 pci_restore_config_space_range(pdev, 4, 9, 10);
1070 pci_restore_config_space_range(pdev, 0, 3, 0);
1071 } else {
1072 pci_restore_config_space_range(pdev, 0, 15, 0);
1073 }
1074}
1075
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001076/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 * pci_restore_state - Restore the saved state of a PCI device
1078 * @dev: - PCI device that we're dealing with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 */
Jon Mason1d3c16a2010-11-30 17:43:26 -06001080void pci_restore_state(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Alek Duc82f63e2009-08-08 08:46:19 +08001082 if (!dev->state_saved)
Jon Mason1d3c16a2010-11-30 17:43:26 -06001083 return;
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +02001084
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001085 /* PCI Express register must be restored first */
1086 pci_restore_pcie_state(dev);
Hao, Xudong1900ca12011-12-17 21:24:40 +08001087 pci_restore_ats_state(dev);
Alex Williamson425c1b22013-12-17 16:43:51 -07001088 pci_restore_vc_state(dev);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +03001089
Rafael J. Wysockia6cb9ee2012-04-16 23:07:50 +02001090 pci_restore_config_space(dev);
Rafael J. Wysockiebfc5b82012-04-15 21:40:40 +02001091
Stephen Hemmingercc692a52006-11-08 16:17:15 -08001092 pci_restore_pcix_state(dev);
Shaohua Li41017f02006-02-08 17:11:38 +08001093 pci_restore_msi_state(dev);
Yu Zhao8c5cdb62009-03-20 11:25:12 +08001094 pci_restore_iov_state(dev);
Michael Ellerman8fed4b62007-01-25 19:34:08 +11001095
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +02001096 dev->state_saved = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001098EXPORT_SYMBOL(pci_restore_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001100struct pci_saved_state {
1101 u32 config_space[16];
1102 struct pci_cap_saved_data cap[0];
1103};
1104
1105/**
1106 * pci_store_saved_state - Allocate and return an opaque struct containing
1107 * the device saved state.
1108 * @dev: PCI device that we're dealing with
1109 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001110 * Return NULL if no state or error.
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001111 */
1112struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev)
1113{
1114 struct pci_saved_state *state;
1115 struct pci_cap_saved_state *tmp;
1116 struct pci_cap_saved_data *cap;
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001117 size_t size;
1118
1119 if (!dev->state_saved)
1120 return NULL;
1121
1122 size = sizeof(*state) + sizeof(struct pci_cap_saved_data);
1123
Sasha Levinb67bfe02013-02-27 17:06:00 -08001124 hlist_for_each_entry(tmp, &dev->saved_cap_space, next)
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001125 size += sizeof(struct pci_cap_saved_data) + tmp->cap.size;
1126
1127 state = kzalloc(size, GFP_KERNEL);
1128 if (!state)
1129 return NULL;
1130
1131 memcpy(state->config_space, dev->saved_config_space,
1132 sizeof(state->config_space));
1133
1134 cap = state->cap;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001135 hlist_for_each_entry(tmp, &dev->saved_cap_space, next) {
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001136 size_t len = sizeof(struct pci_cap_saved_data) + tmp->cap.size;
1137 memcpy(cap, &tmp->cap, len);
1138 cap = (struct pci_cap_saved_data *)((u8 *)cap + len);
1139 }
1140 /* Empty cap_save terminates list */
1141
1142 return state;
1143}
1144EXPORT_SYMBOL_GPL(pci_store_saved_state);
1145
1146/**
1147 * pci_load_saved_state - Reload the provided save state into struct pci_dev.
1148 * @dev: PCI device that we're dealing with
1149 * @state: Saved state returned from pci_store_saved_state()
1150 */
Konrad Rzeszutek Wilk98d9b272014-12-03 16:40:31 -05001151int pci_load_saved_state(struct pci_dev *dev,
1152 struct pci_saved_state *state)
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001153{
1154 struct pci_cap_saved_data *cap;
1155
1156 dev->state_saved = false;
1157
1158 if (!state)
1159 return 0;
1160
1161 memcpy(dev->saved_config_space, state->config_space,
1162 sizeof(state->config_space));
1163
1164 cap = state->cap;
1165 while (cap->size) {
1166 struct pci_cap_saved_state *tmp;
1167
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07001168 tmp = _pci_find_saved_cap(dev, cap->cap_nr, cap->cap_extended);
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001169 if (!tmp || tmp->cap.size != cap->size)
1170 return -EINVAL;
1171
1172 memcpy(tmp->cap.data, cap->data, tmp->cap.size);
1173 cap = (struct pci_cap_saved_data *)((u8 *)cap +
1174 sizeof(struct pci_cap_saved_data) + cap->size);
1175 }
1176
1177 dev->state_saved = true;
1178 return 0;
1179}
Konrad Rzeszutek Wilk98d9b272014-12-03 16:40:31 -05001180EXPORT_SYMBOL_GPL(pci_load_saved_state);
Alex Williamsonffbdd3f2011-05-10 10:02:27 -06001181
1182/**
1183 * pci_load_and_free_saved_state - Reload the save state pointed to by state,
1184 * and free the memory allocated for it.
1185 * @dev: PCI device that we're dealing with
1186 * @state: Pointer to saved state returned from pci_store_saved_state()
1187 */
1188int pci_load_and_free_saved_state(struct pci_dev *dev,
1189 struct pci_saved_state **state)
1190{
1191 int ret = pci_load_saved_state(dev, *state);
1192 kfree(*state);
1193 *state = NULL;
1194 return ret;
1195}
1196EXPORT_SYMBOL_GPL(pci_load_and_free_saved_state);
1197
Bjorn Helgaas8a9d5602014-02-26 11:26:00 -07001198int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
1199{
1200 return pci_enable_resources(dev, bars);
1201}
1202
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001203static int do_pci_enable_device(struct pci_dev *dev, int bars)
1204{
1205 int err;
Vidya Sagar1f6ae472014-07-16 15:33:42 +05301206 struct pci_dev *bridge;
Bjorn Helgaas1e2571a2014-01-29 16:13:51 -07001207 u16 cmd;
1208 u8 pin;
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001209
1210 err = pci_set_power_state(dev, PCI_D0);
1211 if (err < 0 && err != -EIO)
1212 return err;
Vidya Sagar1f6ae472014-07-16 15:33:42 +05301213
1214 bridge = pci_upstream_bridge(dev);
1215 if (bridge)
1216 pcie_aspm_powersave_config_link(bridge);
1217
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001218 err = pcibios_enable_device(dev, bars);
1219 if (err < 0)
1220 return err;
1221 pci_fixup_device(pci_fixup_enable, dev);
1222
Bjorn Helgaas866d5412014-03-07 16:06:05 -07001223 if (dev->msi_enabled || dev->msix_enabled)
1224 return 0;
1225
Bjorn Helgaas1e2571a2014-01-29 16:13:51 -07001226 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
1227 if (pin) {
1228 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1229 if (cmd & PCI_COMMAND_INTX_DISABLE)
1230 pci_write_config_word(dev, PCI_COMMAND,
1231 cmd & ~PCI_COMMAND_INTX_DISABLE);
1232 }
1233
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001234 return 0;
1235}
1236
1237/**
Tejun Heo0b62e132007-07-27 14:43:35 +09001238 * pci_reenable_device - Resume abandoned device
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001239 * @dev: PCI device to be resumed
1240 *
1241 * Note this function is a backend of pci_default_resume and is not supposed
1242 * to be called by normal code, write proper resume handler and use it instead.
1243 */
Tejun Heo0b62e132007-07-27 14:43:35 +09001244int pci_reenable_device(struct pci_dev *dev)
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001245{
Yuji Shimada296ccb02009-04-03 16:41:46 +09001246 if (pci_is_enabled(dev))
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001247 return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
1248 return 0;
1249}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001250EXPORT_SYMBOL(pci_reenable_device);
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001251
Yinghai Lu928bea92013-07-22 14:37:17 -07001252static void pci_enable_bridge(struct pci_dev *dev)
1253{
Bjorn Helgaas79272132013-11-06 10:00:51 -07001254 struct pci_dev *bridge;
Yinghai Lu928bea92013-07-22 14:37:17 -07001255 int retval;
1256
Bjorn Helgaas79272132013-11-06 10:00:51 -07001257 bridge = pci_upstream_bridge(dev);
1258 if (bridge)
1259 pci_enable_bridge(bridge);
Yinghai Lu928bea92013-07-22 14:37:17 -07001260
Yinghai Lucf3e1fe2013-11-05 13:34:38 -07001261 if (pci_is_enabled(dev)) {
Bjorn Helgaasfbeeb822013-11-05 13:34:51 -07001262 if (!dev->is_busmaster)
Yinghai Lucf3e1fe2013-11-05 13:34:38 -07001263 pci_set_master(dev);
Yinghai Lu928bea92013-07-22 14:37:17 -07001264 return;
Yinghai Lucf3e1fe2013-11-05 13:34:38 -07001265 }
1266
Yinghai Lu928bea92013-07-22 14:37:17 -07001267 retval = pci_enable_device(dev);
1268 if (retval)
1269 dev_err(&dev->dev, "Error enabling bridge (%d), continuing\n",
1270 retval);
1271 pci_set_master(dev);
1272}
1273
Bjorn Helgaasb4b4fbb2013-01-04 12:12:55 -07001274static int pci_enable_device_flags(struct pci_dev *dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Bjorn Helgaas79272132013-11-06 10:00:51 -07001276 struct pci_dev *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int err;
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001278 int i, bars = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Jesse Barnes97c145f2010-11-05 15:16:36 -04001280 /*
1281 * Power state could be unknown at this point, either due to a fresh
1282 * boot or a device removal call. So get the current power state
1283 * so that things like MSI message writing will behave as expected
1284 * (e.g. if the device really is in D0 at enable time).
1285 */
1286 if (dev->pm_cap) {
1287 u16 pmcsr;
1288 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
1289 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
1290 }
1291
Bjorn Helgaascc7ba392013-02-11 16:47:01 -07001292 if (atomic_inc_return(&dev->enable_cnt) > 1)
Hidetoshi Seto9fb625c2006-12-18 10:28:43 +09001293 return 0; /* already enabled */
1294
Bjorn Helgaas79272132013-11-06 10:00:51 -07001295 bridge = pci_upstream_bridge(dev);
1296 if (bridge)
1297 pci_enable_bridge(bridge);
Yinghai Lu928bea92013-07-22 14:37:17 -07001298
Yinghai Lu497f16f2011-12-17 18:33:37 -08001299 /* only skip sriov related */
1300 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
1301 if (dev->resource[i].flags & flags)
1302 bars |= (1 << i);
1303 for (i = PCI_BRIDGE_RESOURCES; i < DEVICE_COUNT_RESOURCE; i++)
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001304 if (dev->resource[i].flags & flags)
1305 bars |= (1 << i);
1306
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001307 err = do_pci_enable_device(dev, bars);
Greg Kroah-Hartman95a62962005-07-28 11:37:33 -07001308 if (err < 0)
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001309 atomic_dec(&dev->enable_cnt);
Hidetoshi Seto9fb625c2006-12-18 10:28:43 +09001310 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313/**
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001314 * pci_enable_device_io - Initialize a device for use with IO space
1315 * @dev: PCI device to be initialized
1316 *
1317 * Initialize device before it's used by a driver. Ask low-level code
1318 * to enable I/O resources. Wake up the device if it was suspended.
1319 * Beware, this function can fail.
1320 */
1321int pci_enable_device_io(struct pci_dev *dev)
1322{
Bjorn Helgaasb4b4fbb2013-01-04 12:12:55 -07001323 return pci_enable_device_flags(dev, IORESOURCE_IO);
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001324}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001325EXPORT_SYMBOL(pci_enable_device_io);
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001326
1327/**
1328 * pci_enable_device_mem - Initialize a device for use with Memory space
1329 * @dev: PCI device to be initialized
1330 *
1331 * Initialize device before it's used by a driver. Ask low-level code
1332 * to enable Memory resources. Wake up the device if it was suspended.
1333 * Beware, this function can fail.
1334 */
1335int pci_enable_device_mem(struct pci_dev *dev)
1336{
Bjorn Helgaasb4b4fbb2013-01-04 12:12:55 -07001337 return pci_enable_device_flags(dev, IORESOURCE_MEM);
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001338}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001339EXPORT_SYMBOL(pci_enable_device_mem);
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341/**
1342 * pci_enable_device - Initialize device before it's used by a driver.
1343 * @dev: PCI device to be initialized
1344 *
1345 * Initialize device before it's used by a driver. Ask low-level code
1346 * to enable I/O and memory. Wake up the device if it was suspended.
1347 * Beware, this function can fail.
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001348 *
1349 * Note we don't actually enable the device many times if we call
1350 * this function repeatedly (we just increment the count).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 */
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001352int pci_enable_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Bjorn Helgaasb4b4fbb2013-01-04 12:12:55 -07001354 return pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001356EXPORT_SYMBOL(pci_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Tejun Heo9ac78492007-01-20 16:00:26 +09001358/*
1359 * Managed PCI resources. This manages device on/off, intx/msi/msix
1360 * on/off and BAR regions. pci_dev itself records msi/msix status, so
1361 * there's no need to track it separately. pci_devres is initialized
1362 * when a device is enabled using managed PCI device enable interface.
1363 */
1364struct pci_devres {
Tejun Heo7f375f32007-02-25 04:36:01 -08001365 unsigned int enabled:1;
1366 unsigned int pinned:1;
Tejun Heo9ac78492007-01-20 16:00:26 +09001367 unsigned int orig_intx:1;
1368 unsigned int restore_intx:1;
1369 u32 region_mask;
1370};
1371
1372static void pcim_release(struct device *gendev, void *res)
1373{
1374 struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
1375 struct pci_devres *this = res;
1376 int i;
1377
1378 if (dev->msi_enabled)
1379 pci_disable_msi(dev);
1380 if (dev->msix_enabled)
1381 pci_disable_msix(dev);
1382
1383 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
1384 if (this->region_mask & (1 << i))
1385 pci_release_region(dev, i);
1386
1387 if (this->restore_intx)
1388 pci_intx(dev, this->orig_intx);
1389
Tejun Heo7f375f32007-02-25 04:36:01 -08001390 if (this->enabled && !this->pinned)
Tejun Heo9ac78492007-01-20 16:00:26 +09001391 pci_disable_device(dev);
1392}
1393
Ryan Desfosses07656d83082014-04-11 01:01:53 -04001394static struct pci_devres *get_pci_dr(struct pci_dev *pdev)
Tejun Heo9ac78492007-01-20 16:00:26 +09001395{
1396 struct pci_devres *dr, *new_dr;
1397
1398 dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
1399 if (dr)
1400 return dr;
1401
1402 new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
1403 if (!new_dr)
1404 return NULL;
1405 return devres_get(&pdev->dev, new_dr, NULL, NULL);
1406}
1407
Ryan Desfosses07656d83082014-04-11 01:01:53 -04001408static struct pci_devres *find_pci_dr(struct pci_dev *pdev)
Tejun Heo9ac78492007-01-20 16:00:26 +09001409{
1410 if (pci_is_managed(pdev))
1411 return devres_find(&pdev->dev, pcim_release, NULL, NULL);
1412 return NULL;
1413}
1414
1415/**
1416 * pcim_enable_device - Managed pci_enable_device()
1417 * @pdev: PCI device to be initialized
1418 *
1419 * Managed pci_enable_device().
1420 */
1421int pcim_enable_device(struct pci_dev *pdev)
1422{
1423 struct pci_devres *dr;
1424 int rc;
1425
1426 dr = get_pci_dr(pdev);
1427 if (unlikely(!dr))
1428 return -ENOMEM;
Tejun Heob95d58e2008-01-30 18:20:04 +09001429 if (dr->enabled)
1430 return 0;
Tejun Heo9ac78492007-01-20 16:00:26 +09001431
1432 rc = pci_enable_device(pdev);
1433 if (!rc) {
1434 pdev->is_managed = 1;
Tejun Heo7f375f32007-02-25 04:36:01 -08001435 dr->enabled = 1;
Tejun Heo9ac78492007-01-20 16:00:26 +09001436 }
1437 return rc;
1438}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001439EXPORT_SYMBOL(pcim_enable_device);
Tejun Heo9ac78492007-01-20 16:00:26 +09001440
1441/**
1442 * pcim_pin_device - Pin managed PCI device
1443 * @pdev: PCI device to pin
1444 *
1445 * Pin managed PCI device @pdev. Pinned device won't be disabled on
1446 * driver detach. @pdev must have been enabled with
1447 * pcim_enable_device().
1448 */
1449void pcim_pin_device(struct pci_dev *pdev)
1450{
1451 struct pci_devres *dr;
1452
1453 dr = find_pci_dr(pdev);
Tejun Heo7f375f32007-02-25 04:36:01 -08001454 WARN_ON(!dr || !dr->enabled);
Tejun Heo9ac78492007-01-20 16:00:26 +09001455 if (dr)
Tejun Heo7f375f32007-02-25 04:36:01 -08001456 dr->pinned = 1;
Tejun Heo9ac78492007-01-20 16:00:26 +09001457}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001458EXPORT_SYMBOL(pcim_pin_device);
Tejun Heo9ac78492007-01-20 16:00:26 +09001459
Matthew Garretteca0d462012-12-05 14:33:27 -07001460/*
1461 * pcibios_add_device - provide arch specific hooks when adding device dev
1462 * @dev: the PCI device being added
1463 *
1464 * Permits the platform to provide architecture specific functionality when
1465 * devices are added. This is the default implementation. Architecture
1466 * implementations can override this.
1467 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001468int __weak pcibios_add_device(struct pci_dev *dev)
Matthew Garretteca0d462012-12-05 14:33:27 -07001469{
1470 return 0;
1471}
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473/**
Sebastian Ott6ae32c52013-06-04 19:18:14 +02001474 * pcibios_release_device - provide arch specific hooks when releasing device dev
1475 * @dev: the PCI device being released
1476 *
1477 * Permits the platform to provide architecture specific functionality when
1478 * devices are released. This is the default implementation. Architecture
1479 * implementations can override this.
1480 */
1481void __weak pcibios_release_device(struct pci_dev *dev) {}
1482
1483/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 * pcibios_disable_device - disable arch specific PCI resources for device dev
1485 * @dev: the PCI device to disable
1486 *
1487 * Disables architecture specific PCI resources for the device. This
1488 * is the default implementation. Architecture implementations can
1489 * override this.
1490 */
Bjorn Helgaasd6d88c82012-06-19 06:54:49 -06001491void __weak pcibios_disable_device (struct pci_dev *dev) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Hanjun Guoa43ae582014-05-06 11:29:52 +08001493/**
1494 * pcibios_penalize_isa_irq - penalize an ISA IRQ
1495 * @irq: ISA IRQ to penalize
1496 * @active: IRQ active or not
1497 *
1498 * Permits the platform to provide architecture-specific functionality when
1499 * penalizing ISA IRQs. This is the default implementation. Architecture
1500 * implementations can override this.
1501 */
1502void __weak pcibios_penalize_isa_irq(int irq, int active) {}
1503
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001504static void do_pci_disable_device(struct pci_dev *dev)
1505{
1506 u16 pci_command;
1507
1508 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
1509 if (pci_command & PCI_COMMAND_MASTER) {
1510 pci_command &= ~PCI_COMMAND_MASTER;
1511 pci_write_config_word(dev, PCI_COMMAND, pci_command);
1512 }
1513
1514 pcibios_disable_device(dev);
1515}
1516
1517/**
1518 * pci_disable_enabled_device - Disable device without updating enable_cnt
1519 * @dev: PCI device to disable
1520 *
1521 * NOTE: This function is a backend of PCI power management routines and is
1522 * not supposed to be called drivers.
1523 */
1524void pci_disable_enabled_device(struct pci_dev *dev)
1525{
Yuji Shimada296ccb02009-04-03 16:41:46 +09001526 if (pci_is_enabled(dev))
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001527 do_pci_disable_device(dev);
1528}
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530/**
1531 * pci_disable_device - Disable PCI device after use
1532 * @dev: PCI device to be disabled
1533 *
1534 * Signal to the system that the PCI device is not in use by the system
1535 * anymore. This only involves disabling PCI bus-mastering, if active.
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001536 *
1537 * Note we don't actually disable the device until all callers of
Roman Fietzeee6583f2010-05-18 14:45:47 +02001538 * pci_enable_device() have called pci_disable_device().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001540void pci_disable_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Tejun Heo9ac78492007-01-20 16:00:26 +09001542 struct pci_devres *dr;
Shaohua Li99dc8042006-05-26 10:58:27 +08001543
Tejun Heo9ac78492007-01-20 16:00:26 +09001544 dr = find_pci_dr(dev);
1545 if (dr)
Tejun Heo7f375f32007-02-25 04:36:01 -08001546 dr->enabled = 0;
Tejun Heo9ac78492007-01-20 16:00:26 +09001547
Konstantin Khlebnikovfd6dcea2013-02-04 15:56:01 +04001548 dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0,
1549 "disabling already-disabled device");
1550
Bjorn Helgaascc7ba392013-02-11 16:47:01 -07001551 if (atomic_dec_return(&dev->enable_cnt) != 0)
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001552 return;
1553
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001554 do_pci_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001556 dev->is_busmaster = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001558EXPORT_SYMBOL(pci_disable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560/**
Brian Kingf7bdd122007-04-06 16:39:36 -05001561 * pcibios_set_pcie_reset_state - set reset state for device dev
Stefan Assmann45e829e2009-12-03 06:49:24 -05001562 * @dev: the PCIe device reset
Brian Kingf7bdd122007-04-06 16:39:36 -05001563 * @state: Reset state to enter into
1564 *
1565 *
Stefan Assmann45e829e2009-12-03 06:49:24 -05001566 * Sets the PCIe reset state for the device. This is the default
Brian Kingf7bdd122007-04-06 16:39:36 -05001567 * implementation. Architecture implementations can override this.
1568 */
Bjorn Helgaasd6d88c82012-06-19 06:54:49 -06001569int __weak pcibios_set_pcie_reset_state(struct pci_dev *dev,
1570 enum pcie_reset_state state)
Brian Kingf7bdd122007-04-06 16:39:36 -05001571{
1572 return -EINVAL;
1573}
1574
1575/**
1576 * pci_set_pcie_reset_state - set reset state for device dev
Stefan Assmann45e829e2009-12-03 06:49:24 -05001577 * @dev: the PCIe device reset
Brian Kingf7bdd122007-04-06 16:39:36 -05001578 * @state: Reset state to enter into
1579 *
1580 *
1581 * Sets the PCI reset state for the device.
1582 */
1583int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
1584{
1585 return pcibios_set_pcie_reset_state(dev, state);
1586}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001587EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);
Brian Kingf7bdd122007-04-06 16:39:36 -05001588
1589/**
Rafael J. Wysocki58ff4632010-02-17 23:36:58 +01001590 * pci_check_pme_status - Check if given device has generated PME.
1591 * @dev: Device to check.
1592 *
1593 * Check the PME status of the device and if set, clear it and clear PME enable
1594 * (if set). Return 'true' if PME status and PME enable were both set or
1595 * 'false' otherwise.
1596 */
1597bool pci_check_pme_status(struct pci_dev *dev)
1598{
1599 int pmcsr_pos;
1600 u16 pmcsr;
1601 bool ret = false;
1602
1603 if (!dev->pm_cap)
1604 return false;
1605
1606 pmcsr_pos = dev->pm_cap + PCI_PM_CTRL;
1607 pci_read_config_word(dev, pmcsr_pos, &pmcsr);
1608 if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
1609 return false;
1610
1611 /* Clear PME status. */
1612 pmcsr |= PCI_PM_CTRL_PME_STATUS;
1613 if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
1614 /* Disable PME to avoid interrupt flood. */
1615 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
1616 ret = true;
1617 }
1618
1619 pci_write_config_word(dev, pmcsr_pos, pmcsr);
1620
1621 return ret;
1622}
1623
1624/**
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001625 * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set.
1626 * @dev: Device to handle.
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001627 * @pme_poll_reset: Whether or not to reset the device's pme_poll flag.
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001628 *
1629 * Check if @dev has generated PME and queue a resume request for it in that
1630 * case.
1631 */
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001632static int pci_pme_wakeup(struct pci_dev *dev, void *pme_poll_reset)
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001633{
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001634 if (pme_poll_reset && dev->pme_poll)
1635 dev->pme_poll = false;
1636
Rafael J. Wysockic125e962010-07-05 22:43:53 +02001637 if (pci_check_pme_status(dev)) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +02001638 pci_wakeup_event(dev);
Rafael J. Wysocki0f953bf2010-12-29 13:22:08 +01001639 pm_request_resume(&dev->dev);
Rafael J. Wysockic125e962010-07-05 22:43:53 +02001640 }
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001641 return 0;
1642}
1643
1644/**
1645 * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary.
1646 * @bus: Top bus of the subtree to walk.
1647 */
1648void pci_pme_wakeup_bus(struct pci_bus *bus)
1649{
1650 if (bus)
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001651 pci_walk_bus(bus, pci_pme_wakeup, (void *)true);
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001652}
1653
Huang Ying448bd852012-06-23 10:23:51 +08001654
1655/**
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001656 * pci_pme_capable - check the capability of PCI device to generate PME#
1657 * @dev: PCI device to handle.
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001658 * @state: PCI state from which device will issue PME#.
1659 */
Rafael J. Wysockie5899e12008-07-19 14:39:24 +02001660bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001661{
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001662 if (!dev->pm_cap)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001663 return false;
1664
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001665 return !!(dev->pme_support & (1 << state));
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001666}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001667EXPORT_SYMBOL(pci_pme_capable);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001668
Matthew Garrettdf17e622010-10-04 14:22:29 -04001669static void pci_pme_list_scan(struct work_struct *work)
1670{
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001671 struct pci_pme_device *pme_dev, *n;
Matthew Garrettdf17e622010-10-04 14:22:29 -04001672
1673 mutex_lock(&pci_pme_list_mutex);
Bjorn Helgaasce300002014-01-24 09:51:06 -07001674 list_for_each_entry_safe(pme_dev, n, &pci_pme_list, list) {
1675 if (pme_dev->dev->pme_poll) {
1676 struct pci_dev *bridge;
Zheng Yan71a83bd2012-06-23 10:23:49 +08001677
Bjorn Helgaasce300002014-01-24 09:51:06 -07001678 bridge = pme_dev->dev->bus->self;
1679 /*
1680 * If bridge is in low power state, the
1681 * configuration space of subordinate devices
1682 * may be not accessible
1683 */
1684 if (bridge && bridge->current_state != PCI_D0)
1685 continue;
1686 pci_pme_wakeup(pme_dev->dev, NULL);
1687 } else {
1688 list_del(&pme_dev->list);
1689 kfree(pme_dev);
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001690 }
Matthew Garrettdf17e622010-10-04 14:22:29 -04001691 }
Bjorn Helgaasce300002014-01-24 09:51:06 -07001692 if (!list_empty(&pci_pme_list))
1693 schedule_delayed_work(&pci_pme_work,
1694 msecs_to_jiffies(PME_TIMEOUT));
Matthew Garrettdf17e622010-10-04 14:22:29 -04001695 mutex_unlock(&pci_pme_list_mutex);
1696}
1697
1698/**
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001699 * pci_pme_active - enable or disable PCI device's PME# function
1700 * @dev: PCI device to handle.
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001701 * @enable: 'true' to enable PME# generation; 'false' to disable it.
1702 *
1703 * The caller must verify that the device is capable of generating PME# before
1704 * calling this function with @enable equal to 'true'.
1705 */
Rafael J. Wysocki5a6c9b62008-08-08 00:14:24 +02001706void pci_pme_active(struct pci_dev *dev, bool enable)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001707{
1708 u16 pmcsr;
1709
Rafael J. Wysockiffaddbe2013-04-10 10:32:51 +00001710 if (!dev->pme_support)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001711 return;
1712
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001713 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001714 /* Clear PME_Status by writing 1 to it and enable PME# */
1715 pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
1716 if (!enable)
1717 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
1718
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001719 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001720
Huang Ying6e965e02012-10-26 13:07:51 +08001721 /*
1722 * PCI (as opposed to PCIe) PME requires that the device have
1723 * its PME# line hooked up correctly. Not all hardware vendors
1724 * do this, so the PME never gets delivered and the device
1725 * remains asleep. The easiest way around this is to
1726 * periodically walk the list of suspended devices and check
1727 * whether any have their PME flag set. The assumption is that
1728 * we'll wake up often enough anyway that this won't be a huge
1729 * hit, and the power savings from the devices will still be a
1730 * win.
1731 *
1732 * Although PCIe uses in-band PME message instead of PME# line
1733 * to report PME, PME does not work for some PCIe devices in
1734 * reality. For example, there are devices that set their PME
1735 * status bits, but don't really bother to send a PME message;
1736 * there are PCI Express Root Ports that don't bother to
1737 * trigger interrupts when they receive PME messages from the
1738 * devices below. So PME poll is used for PCIe devices too.
1739 */
Matthew Garrettdf17e622010-10-04 14:22:29 -04001740
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001741 if (dev->pme_poll) {
Matthew Garrettdf17e622010-10-04 14:22:29 -04001742 struct pci_pme_device *pme_dev;
1743 if (enable) {
1744 pme_dev = kmalloc(sizeof(struct pci_pme_device),
1745 GFP_KERNEL);
Bjorn Helgaas0394cb12013-10-16 12:32:53 -06001746 if (!pme_dev) {
1747 dev_warn(&dev->dev, "can't enable PME#\n");
1748 return;
1749 }
Matthew Garrettdf17e622010-10-04 14:22:29 -04001750 pme_dev->dev = dev;
1751 mutex_lock(&pci_pme_list_mutex);
1752 list_add(&pme_dev->list, &pci_pme_list);
1753 if (list_is_singular(&pci_pme_list))
1754 schedule_delayed_work(&pci_pme_work,
1755 msecs_to_jiffies(PME_TIMEOUT));
1756 mutex_unlock(&pci_pme_list_mutex);
1757 } else {
1758 mutex_lock(&pci_pme_list_mutex);
1759 list_for_each_entry(pme_dev, &pci_pme_list, list) {
1760 if (pme_dev->dev == dev) {
1761 list_del(&pme_dev->list);
1762 kfree(pme_dev);
1763 break;
1764 }
1765 }
1766 mutex_unlock(&pci_pme_list_mutex);
1767 }
1768 }
1769
Vincent Palatin85b85822011-12-05 11:51:18 -08001770 dev_dbg(&dev->dev, "PME# %s\n", enable ? "enabled" : "disabled");
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001771}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001772EXPORT_SYMBOL(pci_pme_active);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001773
1774/**
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001775 * __pci_enable_wake - enable PCI device as wakeup event source
David Brownell075c1772007-04-26 00:12:06 -07001776 * @dev: PCI device affected
1777 * @state: PCI state from which device will issue wakeup events
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001778 * @runtime: True if the events are to be generated at run time
David Brownell075c1772007-04-26 00:12:06 -07001779 * @enable: True to enable event generation; false to disable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 *
David Brownell075c1772007-04-26 00:12:06 -07001781 * This enables the device as a wakeup event source, or disables it.
1782 * When such events involves platform-specific hooks, those hooks are
1783 * called automatically by this routine.
1784 *
1785 * Devices with legacy power management (no standard PCI PM capabilities)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001786 * always require such platform hooks.
David Brownell075c1772007-04-26 00:12:06 -07001787 *
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001788 * RETURN VALUE:
1789 * 0 is returned on success
1790 * -EINVAL is returned if device is not supposed to wake up the system
1791 * Error code depending on the platform is returned if both the platform and
1792 * the native mechanism fail to enable the generation of wake-up events
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 */
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001794int __pci_enable_wake(struct pci_dev *dev, pci_power_t state,
1795 bool runtime, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001797 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001799 if (enable && !runtime && !device_may_wakeup(&dev->dev))
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001800 return -EINVAL;
1801
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02001802 /* Don't do the same thing twice in a row for one device. */
1803 if (!!enable == !!dev->wakeup_prepared)
1804 return 0;
1805
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001806 /*
1807 * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
1808 * Anderson we should be doing PME# wake enable followed by ACPI wake
1809 * enable. To disable wake-up we call the platform first, for symmetry.
David Brownell075c1772007-04-26 00:12:06 -07001810 */
1811
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001812 if (enable) {
1813 int error;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001814
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001815 if (pci_pme_capable(dev, state))
1816 pci_pme_active(dev, true);
1817 else
1818 ret = 1;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001819 error = runtime ? platform_pci_run_wake(dev, true) :
1820 platform_pci_sleep_wake(dev, true);
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001821 if (ret)
1822 ret = error;
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02001823 if (!ret)
1824 dev->wakeup_prepared = true;
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001825 } else {
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001826 if (runtime)
1827 platform_pci_run_wake(dev, false);
1828 else
1829 platform_pci_sleep_wake(dev, false);
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001830 pci_pme_active(dev, false);
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02001831 dev->wakeup_prepared = false;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001832 }
1833
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001834 return ret;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001835}
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001836EXPORT_SYMBOL(__pci_enable_wake);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001837
1838/**
Rafael J. Wysocki0235c4f2008-08-18 21:38:00 +02001839 * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
1840 * @dev: PCI device to prepare
1841 * @enable: True to enable wake-up event generation; false to disable
1842 *
1843 * Many drivers want the device to wake up the system from D3_hot or D3_cold
1844 * and this function allows them to set that up cleanly - pci_enable_wake()
1845 * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
1846 * ordering constraints.
1847 *
1848 * This function only returns error code if the device is not capable of
1849 * generating PME# from both D3_hot and D3_cold, and the platform is unable to
1850 * enable wake-up power for it.
1851 */
1852int pci_wake_from_d3(struct pci_dev *dev, bool enable)
1853{
1854 return pci_pme_capable(dev, PCI_D3cold) ?
1855 pci_enable_wake(dev, PCI_D3cold, enable) :
1856 pci_enable_wake(dev, PCI_D3hot, enable);
1857}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001858EXPORT_SYMBOL(pci_wake_from_d3);
Rafael J. Wysocki0235c4f2008-08-18 21:38:00 +02001859
1860/**
Jesse Barnes37139072008-07-28 11:49:26 -07001861 * pci_target_state - find an appropriate low power state for a given PCI dev
1862 * @dev: PCI device
1863 *
1864 * Use underlying platform code to find a supported low power state for @dev.
1865 * If the platform can't manage @dev, return the deepest state from which it
1866 * can generate wake events, based on any available PME info.
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001867 */
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001868static pci_power_t pci_target_state(struct pci_dev *dev)
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001869{
1870 pci_power_t target_state = PCI_D3hot;
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001871
1872 if (platform_pci_power_manageable(dev)) {
1873 /*
1874 * Call the platform to choose the target state of the device
1875 * and enable wake-up from this state if supported.
1876 */
1877 pci_power_t state = platform_pci_choose_state(dev);
1878
1879 switch (state) {
1880 case PCI_POWER_ERROR:
1881 case PCI_UNKNOWN:
1882 break;
1883 case PCI_D1:
1884 case PCI_D2:
1885 if (pci_no_d1d2(dev))
1886 break;
1887 default:
1888 target_state = state;
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001889 }
Rafael J. Wysockid2abdf62009-06-14 21:25:02 +02001890 } else if (!dev->pm_cap) {
1891 target_state = PCI_D0;
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001892 } else if (device_may_wakeup(&dev->dev)) {
1893 /*
1894 * Find the deepest state from which the device can generate
1895 * wake-up events, make it the target state and enable device
1896 * to generate PME#.
1897 */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001898 if (dev->pme_support) {
1899 while (target_state
1900 && !(dev->pme_support & (1 << target_state)))
1901 target_state--;
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001902 }
1903 }
1904
Rafael J. Wysockie5899e12008-07-19 14:39:24 +02001905 return target_state;
1906}
1907
1908/**
1909 * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
1910 * @dev: Device to handle.
1911 *
1912 * Choose the power state appropriate for the device depending on whether
1913 * it can wake up the system and/or is power manageable by the platform
1914 * (PCI_D3hot is the default) and put the device into that state.
1915 */
1916int pci_prepare_to_sleep(struct pci_dev *dev)
1917{
1918 pci_power_t target_state = pci_target_state(dev);
1919 int error;
1920
1921 if (target_state == PCI_POWER_ERROR)
1922 return -EIO;
1923
Rafael J. Wysocki8efb8c72009-03-30 21:46:27 +02001924 pci_enable_wake(dev, target_state, device_may_wakeup(&dev->dev));
Rafael J. Wysockic157dfa2008-07-13 22:45:06 +02001925
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001926 error = pci_set_power_state(dev, target_state);
1927
1928 if (error)
1929 pci_enable_wake(dev, target_state, false);
1930
1931 return error;
1932}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001933EXPORT_SYMBOL(pci_prepare_to_sleep);
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001934
1935/**
Randy Dunlap443bd1c2008-07-21 09:27:18 -07001936 * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001937 * @dev: Device to handle.
1938 *
Thomas Weber88393162010-03-16 11:47:56 +01001939 * Disable device's system wake-up capability and put it into D0.
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001940 */
1941int pci_back_from_sleep(struct pci_dev *dev)
1942{
1943 pci_enable_wake(dev, PCI_D0, false);
1944 return pci_set_power_state(dev, PCI_D0);
1945}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001946EXPORT_SYMBOL(pci_back_from_sleep);
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001947
1948/**
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001949 * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend.
1950 * @dev: PCI device being suspended.
1951 *
1952 * Prepare @dev to generate wake-up events at run time and put it into a low
1953 * power state.
1954 */
1955int pci_finish_runtime_suspend(struct pci_dev *dev)
1956{
1957 pci_power_t target_state = pci_target_state(dev);
1958 int error;
1959
1960 if (target_state == PCI_POWER_ERROR)
1961 return -EIO;
1962
Huang Ying448bd852012-06-23 10:23:51 +08001963 dev->runtime_d3cold = target_state == PCI_D3cold;
1964
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001965 __pci_enable_wake(dev, target_state, true, pci_dev_run_wake(dev));
1966
1967 error = pci_set_power_state(dev, target_state);
1968
Huang Ying448bd852012-06-23 10:23:51 +08001969 if (error) {
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001970 __pci_enable_wake(dev, target_state, true, false);
Huang Ying448bd852012-06-23 10:23:51 +08001971 dev->runtime_d3cold = false;
1972 }
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001973
1974 return error;
1975}
1976
1977/**
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001978 * pci_dev_run_wake - Check if device can generate run-time wake-up events.
1979 * @dev: Device to check.
1980 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001981 * Return true if the device itself is capable of generating wake-up events
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001982 * (through the platform or using the native PCIe PME) or if the device supports
1983 * PME and one of its upstream bridges can generate wake-up events.
1984 */
1985bool pci_dev_run_wake(struct pci_dev *dev)
1986{
1987 struct pci_bus *bus = dev->bus;
1988
1989 if (device_run_wake(&dev->dev))
1990 return true;
1991
1992 if (!dev->pme_support)
1993 return false;
1994
1995 while (bus->parent) {
1996 struct pci_dev *bridge = bus->self;
1997
1998 if (device_run_wake(&bridge->dev))
1999 return true;
2000
2001 bus = bus->parent;
2002 }
2003
2004 /* We have reached the root bus. */
2005 if (bus->bridge)
2006 return device_run_wake(bus->bridge);
2007
2008 return false;
2009}
2010EXPORT_SYMBOL_GPL(pci_dev_run_wake);
2011
Rafael J. Wysockibac2a902015-01-21 02:17:42 +01002012/**
2013 * pci_dev_keep_suspended - Check if the device can stay in the suspended state.
2014 * @pci_dev: Device to check.
2015 *
2016 * Return 'true' if the device is runtime-suspended, it doesn't have to be
2017 * reconfigured due to wakeup settings difference between system and runtime
2018 * suspend and the current power state of it is suitable for the upcoming
2019 * (system) transition.
2020 */
2021bool pci_dev_keep_suspended(struct pci_dev *pci_dev)
2022{
2023 struct device *dev = &pci_dev->dev;
2024
2025 if (!pm_runtime_suspended(dev)
2026 || (device_can_wakeup(dev) && !device_may_wakeup(dev))
2027 || platform_pci_need_resume(pci_dev))
2028 return false;
2029
2030 return pci_target_state(pci_dev) == pci_dev->current_state;
2031}
2032
Huang Yingb3c32c42012-10-25 09:36:03 +08002033void pci_config_pm_runtime_get(struct pci_dev *pdev)
2034{
2035 struct device *dev = &pdev->dev;
2036 struct device *parent = dev->parent;
2037
2038 if (parent)
2039 pm_runtime_get_sync(parent);
2040 pm_runtime_get_noresume(dev);
2041 /*
2042 * pdev->current_state is set to PCI_D3cold during suspending,
2043 * so wait until suspending completes
2044 */
2045 pm_runtime_barrier(dev);
2046 /*
2047 * Only need to resume devices in D3cold, because config
2048 * registers are still accessible for devices suspended but
2049 * not in D3cold.
2050 */
2051 if (pdev->current_state == PCI_D3cold)
2052 pm_runtime_resume(dev);
2053}
2054
2055void pci_config_pm_runtime_put(struct pci_dev *pdev)
2056{
2057 struct device *dev = &pdev->dev;
2058 struct device *parent = dev->parent;
2059
2060 pm_runtime_put(dev);
2061 if (parent)
2062 pm_runtime_put_sync(parent);
2063}
2064
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01002065/**
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002066 * pci_pm_init - Initialize PM functions of given PCI device
2067 * @dev: PCI device to handle.
2068 */
2069void pci_pm_init(struct pci_dev *dev)
2070{
2071 int pm;
2072 u16 pmc;
David Brownell075c1772007-04-26 00:12:06 -07002073
Rafael J. Wysockibb910a72010-02-27 21:37:37 +01002074 pm_runtime_forbid(&dev->dev);
Huang Ying967577b2012-11-20 16:08:22 +08002075 pm_runtime_set_active(&dev->dev);
2076 pm_runtime_enable(&dev->dev);
Rafael J. Wysockia1e4d722010-02-08 19:16:33 +01002077 device_enable_async_suspend(&dev->dev);
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02002078 dev->wakeup_prepared = false;
Rafael J. Wysockibb910a72010-02-27 21:37:37 +01002079
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002080 dev->pm_cap = 0;
Rafael J. Wysockiffaddbe2013-04-10 10:32:51 +00002081 dev->pme_support = 0;
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /* find PCI PM capability in list */
2084 pm = pci_find_capability(dev, PCI_CAP_ID_PM);
David Brownell075c1772007-04-26 00:12:06 -07002085 if (!pm)
Linus Torvalds50246dd2009-01-16 08:14:51 -08002086 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 /* Check device's ability to generate PME# */
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002088 pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002090 if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
2091 dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n",
2092 pmc & PCI_PM_CAP_VER_MASK);
Linus Torvalds50246dd2009-01-16 08:14:51 -08002093 return;
David Brownell075c1772007-04-26 00:12:06 -07002094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002096 dev->pm_cap = pm;
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +01002097 dev->d3_delay = PCI_PM_D3_WAIT;
Huang Ying448bd852012-06-23 10:23:51 +08002098 dev->d3cold_delay = PCI_PM_D3COLD_WAIT;
Huang Ying4f9c1392012-08-08 09:07:38 +08002099 dev->d3cold_allowed = true;
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002100
2101 dev->d1_support = false;
2102 dev->d2_support = false;
2103 if (!pci_no_d1d2(dev)) {
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06002104 if (pmc & PCI_PM_CAP_D1)
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002105 dev->d1_support = true;
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06002106 if (pmc & PCI_PM_CAP_D2)
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002107 dev->d2_support = true;
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06002108
2109 if (dev->d1_support || dev->d2_support)
2110 dev_printk(KERN_DEBUG, &dev->dev, "supports%s%s\n",
Jesse Barnesec84f122008-09-23 11:43:34 -07002111 dev->d1_support ? " D1" : "",
2112 dev->d2_support ? " D2" : "");
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002113 }
2114
2115 pmc &= PCI_PM_CAP_PME_MASK;
2116 if (pmc) {
Bjorn Helgaas10c3d712009-11-04 10:32:42 -07002117 dev_printk(KERN_DEBUG, &dev->dev,
2118 "PME# supported from%s%s%s%s%s\n",
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06002119 (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
2120 (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
2121 (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
2122 (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
2123 (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002124 dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02002125 dev->pme_poll = true;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002126 /*
2127 * Make device's PM flags reflect the wake-up capability, but
2128 * let the user space enable it to wake up the system as needed.
2129 */
2130 device_set_wakeup_capable(&dev->dev, true);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002131 /* Disable the PME# generation functionality */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02002132 pci_pme_active(dev, false);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Yinghai Lu34a48762012-02-11 00:18:41 -08002136static void pci_add_saved_cap(struct pci_dev *pci_dev,
2137 struct pci_cap_saved_state *new_cap)
2138{
2139 hlist_add_head(&new_cap->next, &pci_dev->saved_cap_space);
2140}
2141
Jesse Barneseb9c39d2008-12-17 12:10:05 -08002142/**
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002143 * _pci_add_cap_save_buffer - allocate buffer for saving given
2144 * capability registers
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002145 * @dev: the PCI device
2146 * @cap: the capability to allocate the buffer for
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002147 * @extended: Standard or Extended capability ID
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002148 * @size: requested size of the buffer
2149 */
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002150static int _pci_add_cap_save_buffer(struct pci_dev *dev, u16 cap,
2151 bool extended, unsigned int size)
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002152{
2153 int pos;
2154 struct pci_cap_saved_state *save_state;
2155
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002156 if (extended)
2157 pos = pci_find_ext_capability(dev, cap);
2158 else
2159 pos = pci_find_capability(dev, cap);
2160
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002161 if (pos <= 0)
2162 return 0;
2163
2164 save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
2165 if (!save_state)
2166 return -ENOMEM;
2167
Alex Williamson24a4742f2011-05-10 10:02:11 -06002168 save_state->cap.cap_nr = cap;
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002169 save_state->cap.cap_extended = extended;
Alex Williamson24a4742f2011-05-10 10:02:11 -06002170 save_state->cap.size = size;
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002171 pci_add_saved_cap(dev, save_state);
2172
2173 return 0;
2174}
2175
Alex Williamsonfd0f7f72013-12-17 16:43:45 -07002176int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size)
2177{
2178 return _pci_add_cap_save_buffer(dev, cap, false, size);
2179}
2180
2181int pci_add_ext_cap_save_buffer(struct pci_dev *dev, u16 cap, unsigned int size)
2182{
2183 return _pci_add_cap_save_buffer(dev, cap, true, size);
2184}
2185
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002186/**
2187 * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
2188 * @dev: the PCI device
2189 */
2190void pci_allocate_cap_save_buffers(struct pci_dev *dev)
2191{
2192 int error;
2193
Yu Zhao89858512009-02-16 02:55:47 +08002194 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP,
2195 PCI_EXP_SAVE_REGS * sizeof(u16));
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002196 if (error)
2197 dev_err(&dev->dev,
2198 "unable to preallocate PCI Express save buffer\n");
2199
2200 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
2201 if (error)
2202 dev_err(&dev->dev,
2203 "unable to preallocate PCI-X save buffer\n");
Alex Williamson425c1b22013-12-17 16:43:51 -07002204
2205 pci_allocate_vc_save_buffers(dev);
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002206}
2207
Yinghai Luf7968412012-02-11 00:18:30 -08002208void pci_free_cap_save_buffers(struct pci_dev *dev)
2209{
2210 struct pci_cap_saved_state *tmp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002211 struct hlist_node *n;
Yinghai Luf7968412012-02-11 00:18:30 -08002212
Sasha Levinb67bfe02013-02-27 17:06:00 -08002213 hlist_for_each_entry_safe(tmp, n, &dev->saved_cap_space, next)
Yinghai Luf7968412012-02-11 00:18:30 -08002214 kfree(tmp);
2215}
2216
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002217/**
Yijing Wang31ab2472013-01-15 11:12:17 +08002218 * pci_configure_ari - enable or disable ARI forwarding
Yu Zhao58c3a722008-10-14 14:02:53 +08002219 * @dev: the PCI device
Yijing Wangb0cc6022013-01-15 11:12:16 +08002220 *
2221 * If @dev and its upstream bridge both support ARI, enable ARI in the
2222 * bridge. Otherwise, disable ARI in the bridge.
Yu Zhao58c3a722008-10-14 14:02:53 +08002223 */
Yijing Wang31ab2472013-01-15 11:12:17 +08002224void pci_configure_ari(struct pci_dev *dev)
Yu Zhao58c3a722008-10-14 14:02:53 +08002225{
Yu Zhao58c3a722008-10-14 14:02:53 +08002226 u32 cap;
Zhao, Yu81135872008-10-23 13:15:39 +08002227 struct pci_dev *bridge;
Yu Zhao58c3a722008-10-14 14:02:53 +08002228
Rafael J. Wysocki6748dcc2012-03-01 00:06:33 +01002229 if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
Yu Zhao58c3a722008-10-14 14:02:53 +08002230 return;
2231
Zhao, Yu81135872008-10-23 13:15:39 +08002232 bridge = dev->bus->self;
Myron Stowecb97ae32012-06-01 15:16:31 -06002233 if (!bridge)
Zhao, Yu81135872008-10-23 13:15:39 +08002234 return;
2235
Jiang Liu59875ae2012-07-24 17:20:06 +08002236 pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
Yu Zhao58c3a722008-10-14 14:02:53 +08002237 if (!(cap & PCI_EXP_DEVCAP2_ARI))
2238 return;
2239
Yijing Wangb0cc6022013-01-15 11:12:16 +08002240 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
2241 pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
2242 PCI_EXP_DEVCTL2_ARI);
2243 bridge->ari_enabled = 1;
2244 } else {
2245 pcie_capability_clear_word(bridge, PCI_EXP_DEVCTL2,
2246 PCI_EXP_DEVCTL2_ARI);
2247 bridge->ari_enabled = 0;
2248 }
Yu Zhao58c3a722008-10-14 14:02:53 +08002249}
2250
Chris Wright5d990b62009-12-04 12:15:21 -08002251static int pci_acs_enable;
2252
2253/**
2254 * pci_request_acs - ask for ACS to be enabled if supported
2255 */
2256void pci_request_acs(void)
2257{
2258 pci_acs_enable = 1;
2259}
2260
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07002261/**
Alex Williamson2c744242014-02-03 14:27:33 -07002262 * pci_std_enable_acs - enable ACS on devices using standard ACS capabilites
Allen Kayae21ee62009-10-07 10:27:17 -07002263 * @dev: the PCI device
2264 */
Alex Williamson2c744242014-02-03 14:27:33 -07002265static int pci_std_enable_acs(struct pci_dev *dev)
Allen Kayae21ee62009-10-07 10:27:17 -07002266{
2267 int pos;
2268 u16 cap;
2269 u16 ctrl;
2270
Allen Kayae21ee62009-10-07 10:27:17 -07002271 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
2272 if (!pos)
Alex Williamson2c744242014-02-03 14:27:33 -07002273 return -ENODEV;
Allen Kayae21ee62009-10-07 10:27:17 -07002274
2275 pci_read_config_word(dev, pos + PCI_ACS_CAP, &cap);
2276 pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
2277
2278 /* Source Validation */
2279 ctrl |= (cap & PCI_ACS_SV);
2280
2281 /* P2P Request Redirect */
2282 ctrl |= (cap & PCI_ACS_RR);
2283
2284 /* P2P Completion Redirect */
2285 ctrl |= (cap & PCI_ACS_CR);
2286
2287 /* Upstream Forwarding */
2288 ctrl |= (cap & PCI_ACS_UF);
2289
2290 pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
Alex Williamson2c744242014-02-03 14:27:33 -07002291
2292 return 0;
2293}
2294
2295/**
2296 * pci_enable_acs - enable ACS if hardware support it
2297 * @dev: the PCI device
2298 */
2299void pci_enable_acs(struct pci_dev *dev)
2300{
2301 if (!pci_acs_enable)
2302 return;
2303
2304 if (!pci_std_enable_acs(dev))
2305 return;
2306
2307 pci_dev_specific_enable_acs(dev);
Allen Kayae21ee62009-10-07 10:27:17 -07002308}
2309
Alex Williamson0a671192013-06-27 16:39:48 -06002310static bool pci_acs_flags_enabled(struct pci_dev *pdev, u16 acs_flags)
2311{
2312 int pos;
Alex Williamson83db7e02013-06-27 16:39:54 -06002313 u16 cap, ctrl;
Alex Williamson0a671192013-06-27 16:39:48 -06002314
2315 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ACS);
2316 if (!pos)
2317 return false;
2318
Alex Williamson83db7e02013-06-27 16:39:54 -06002319 /*
2320 * Except for egress control, capabilities are either required
2321 * or only required if controllable. Features missing from the
2322 * capability field can therefore be assumed as hard-wired enabled.
2323 */
2324 pci_read_config_word(pdev, pos + PCI_ACS_CAP, &cap);
2325 acs_flags &= (cap | PCI_ACS_EC);
2326
Alex Williamson0a671192013-06-27 16:39:48 -06002327 pci_read_config_word(pdev, pos + PCI_ACS_CTRL, &ctrl);
2328 return (ctrl & acs_flags) == acs_flags;
2329}
2330
Allen Kayae21ee62009-10-07 10:27:17 -07002331/**
Alex Williamsonad805752012-06-11 05:27:07 +00002332 * pci_acs_enabled - test ACS against required flags for a given device
2333 * @pdev: device to test
2334 * @acs_flags: required PCI ACS flags
2335 *
2336 * Return true if the device supports the provided flags. Automatically
2337 * filters out flags that are not implemented on multifunction devices.
Alex Williamson0a671192013-06-27 16:39:48 -06002338 *
2339 * Note that this interface checks the effective ACS capabilities of the
2340 * device rather than the actual capabilities. For instance, most single
2341 * function endpoints are not required to support ACS because they have no
2342 * opportunity for peer-to-peer access. We therefore return 'true'
2343 * regardless of whether the device exposes an ACS capability. This makes
2344 * it much easier for callers of this function to ignore the actual type
2345 * or topology of the device when testing ACS support.
Alex Williamsonad805752012-06-11 05:27:07 +00002346 */
2347bool pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags)
2348{
Alex Williamson0a671192013-06-27 16:39:48 -06002349 int ret;
Alex Williamsonad805752012-06-11 05:27:07 +00002350
2351 ret = pci_dev_specific_acs_enabled(pdev, acs_flags);
2352 if (ret >= 0)
2353 return ret > 0;
2354
Alex Williamson0a671192013-06-27 16:39:48 -06002355 /*
2356 * Conventional PCI and PCI-X devices never support ACS, either
2357 * effectively or actually. The shared bus topology implies that
2358 * any device on the bus can receive or snoop DMA.
2359 */
Alex Williamsonad805752012-06-11 05:27:07 +00002360 if (!pci_is_pcie(pdev))
2361 return false;
2362
Alex Williamson0a671192013-06-27 16:39:48 -06002363 switch (pci_pcie_type(pdev)) {
2364 /*
2365 * PCI/X-to-PCIe bridges are not specifically mentioned by the spec,
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002366 * but since their primary interface is PCI/X, we conservatively
Alex Williamson0a671192013-06-27 16:39:48 -06002367 * handle them as we would a non-PCIe device.
2368 */
2369 case PCI_EXP_TYPE_PCIE_BRIDGE:
2370 /*
2371 * PCIe 3.0, 6.12.1 excludes ACS on these devices. "ACS is never
2372 * applicable... must never implement an ACS Extended Capability...".
2373 * This seems arbitrary, but we take a conservative interpretation
2374 * of this statement.
2375 */
2376 case PCI_EXP_TYPE_PCI_BRIDGE:
2377 case PCI_EXP_TYPE_RC_EC:
2378 return false;
2379 /*
2380 * PCIe 3.0, 6.12.1.1 specifies that downstream and root ports should
2381 * implement ACS in order to indicate their peer-to-peer capabilities,
2382 * regardless of whether they are single- or multi-function devices.
2383 */
2384 case PCI_EXP_TYPE_DOWNSTREAM:
2385 case PCI_EXP_TYPE_ROOT_PORT:
2386 return pci_acs_flags_enabled(pdev, acs_flags);
2387 /*
2388 * PCIe 3.0, 6.12.1.2 specifies ACS capabilities that should be
2389 * implemented by the remaining PCIe types to indicate peer-to-peer
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002390 * capabilities, but only when they are part of a multifunction
Alex Williamson0a671192013-06-27 16:39:48 -06002391 * device. The footnote for section 6.12 indicates the specific
2392 * PCIe types included here.
2393 */
2394 case PCI_EXP_TYPE_ENDPOINT:
2395 case PCI_EXP_TYPE_UPSTREAM:
2396 case PCI_EXP_TYPE_LEG_END:
2397 case PCI_EXP_TYPE_RC_END:
2398 if (!pdev->multifunction)
2399 break;
2400
Alex Williamson0a671192013-06-27 16:39:48 -06002401 return pci_acs_flags_enabled(pdev, acs_flags);
Alex Williamsonad805752012-06-11 05:27:07 +00002402 }
2403
Alex Williamson0a671192013-06-27 16:39:48 -06002404 /*
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002405 * PCIe 3.0, 6.12.1.3 specifies no ACS capabilities are applicable
Alex Williamson0a671192013-06-27 16:39:48 -06002406 * to single function devices with the exception of downstream ports.
2407 */
Alex Williamsonad805752012-06-11 05:27:07 +00002408 return true;
2409}
2410
2411/**
2412 * pci_acs_path_enable - test ACS flags from start to end in a hierarchy
2413 * @start: starting downstream device
2414 * @end: ending upstream device or NULL to search to the root bus
2415 * @acs_flags: required flags
2416 *
2417 * Walk up a device tree from start to end testing PCI ACS support. If
2418 * any step along the way does not support the required flags, return false.
2419 */
2420bool pci_acs_path_enabled(struct pci_dev *start,
2421 struct pci_dev *end, u16 acs_flags)
2422{
2423 struct pci_dev *pdev, *parent = start;
2424
2425 do {
2426 pdev = parent;
2427
2428 if (!pci_acs_enabled(pdev, acs_flags))
2429 return false;
2430
2431 if (pci_is_root_bus(pdev->bus))
2432 return (end == NULL);
2433
2434 parent = pdev->bus->self;
2435 } while (pdev != end);
2436
2437 return true;
2438}
2439
2440/**
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07002441 * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
2442 * @dev: the PCI device
Wang Sheng-Huibb5c2de2013-05-28 11:17:41 +08002443 * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD)
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07002444 *
2445 * Perform INTx swizzling for a device behind one level of bridge. This is
2446 * required by section 9.1 of the PCI-to-PCI bridge specification for devices
Matthew Wilcox46b952a2009-07-01 14:24:30 -07002447 * behind bridges on add-in cards. For devices with ARI enabled, the slot
2448 * number is always 0 (see the Implementation Note in section 2.2.8.1 of
2449 * the PCI Express Base Specification, Revision 2.1)
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07002450 */
John Crispin3df425f2012-04-12 17:33:07 +02002451u8 pci_swizzle_interrupt_pin(const struct pci_dev *dev, u8 pin)
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07002452{
Matthew Wilcox46b952a2009-07-01 14:24:30 -07002453 int slot;
2454
2455 if (pci_ari_enabled(dev->bus))
2456 slot = 0;
2457 else
2458 slot = PCI_SLOT(dev->devfn);
2459
2460 return (((pin - 1) + slot) % 4) + 1;
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07002461}
2462
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002463int pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
2465 u8 pin;
2466
Kristen Accardi514d2072005-11-02 16:24:39 -08002467 pin = dev->pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 if (!pin)
2469 return -1;
Bjorn Helgaas878f2e52008-12-09 16:11:46 -07002470
Kenji Kaneshige8784fd42009-05-26 16:07:33 +09002471 while (!pci_is_root_bus(dev->bus)) {
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07002472 pin = pci_swizzle_interrupt_pin(dev, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 dev = dev->bus->self;
2474 }
2475 *bridge = dev;
2476 return pin;
2477}
2478
2479/**
Bjorn Helgaas68feac82008-12-16 21:36:55 -07002480 * pci_common_swizzle - swizzle INTx all the way to root bridge
2481 * @dev: the PCI device
2482 * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
2483 *
2484 * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
2485 * bridges all the way up to a PCI root bus.
2486 */
2487u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
2488{
2489 u8 pin = *pinp;
2490
Kenji Kaneshige1eb39482009-05-26 16:08:36 +09002491 while (!pci_is_root_bus(dev->bus)) {
Bjorn Helgaas68feac82008-12-16 21:36:55 -07002492 pin = pci_swizzle_interrupt_pin(dev, pin);
2493 dev = dev->bus->self;
2494 }
2495 *pinp = pin;
2496 return PCI_SLOT(dev->devfn);
2497}
2498
2499/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 * pci_release_region - Release a PCI bar
2501 * @pdev: PCI device whose resources were previously reserved by pci_request_region
2502 * @bar: BAR to release
2503 *
2504 * Releases the PCI I/O and memory resources previously reserved by a
2505 * successful call to pci_request_region. Call this function only
2506 * after all use of the PCI regions has ceased.
2507 */
2508void pci_release_region(struct pci_dev *pdev, int bar)
2509{
Tejun Heo9ac78492007-01-20 16:00:26 +09002510 struct pci_devres *dr;
2511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 if (pci_resource_len(pdev, bar) == 0)
2513 return;
2514 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
2515 release_region(pci_resource_start(pdev, bar),
2516 pci_resource_len(pdev, bar));
2517 else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
2518 release_mem_region(pci_resource_start(pdev, bar),
2519 pci_resource_len(pdev, bar));
Tejun Heo9ac78492007-01-20 16:00:26 +09002520
2521 dr = find_pci_dr(pdev);
2522 if (dr)
2523 dr->region_mask &= ~(1 << bar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002525EXPORT_SYMBOL(pci_release_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527/**
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08002528 * __pci_request_region - Reserved PCI I/O and memory resource
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 * @pdev: PCI device whose resources are to be reserved
2530 * @bar: BAR to be reserved
2531 * @res_name: Name to be associated with resource.
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08002532 * @exclusive: whether the region access is exclusive or not
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 *
2534 * Mark the PCI region associated with PCI device @pdev BR @bar as
2535 * being reserved by owner @res_name. Do not access any
2536 * address inside the PCI regions unless this call returns
2537 * successfully.
2538 *
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08002539 * If @exclusive is set, then the region is marked so that userspace
2540 * is explicitly not allowed to map the resource via /dev/mem or
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002541 * sysfs MMIO access.
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08002542 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 * Returns 0 on success, or %EBUSY on error. A warning
2544 * message is also printed on failure.
2545 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002546static int __pci_request_region(struct pci_dev *pdev, int bar,
2547 const char *res_name, int exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
Tejun Heo9ac78492007-01-20 16:00:26 +09002549 struct pci_devres *dr;
2550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if (pci_resource_len(pdev, bar) == 0)
2552 return 0;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
2555 if (!request_region(pci_resource_start(pdev, bar),
2556 pci_resource_len(pdev, bar), res_name))
2557 goto err_out;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002558 } else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
Arjan van de Vene8de1482008-10-22 19:55:31 -07002559 if (!__request_mem_region(pci_resource_start(pdev, bar),
2560 pci_resource_len(pdev, bar), res_name,
2561 exclusive))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 goto err_out;
2563 }
Tejun Heo9ac78492007-01-20 16:00:26 +09002564
2565 dr = find_pci_dr(pdev);
2566 if (dr)
2567 dr->region_mask |= 1 << bar;
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return 0;
2570
2571err_out:
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002572 dev_warn(&pdev->dev, "BAR %d: can't reserve %pR\n", bar,
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +11002573 &pdev->resource[bar]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return -EBUSY;
2575}
2576
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09002577/**
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08002578 * pci_request_region - Reserve PCI I/O and memory resource
Arjan van de Vene8de1482008-10-22 19:55:31 -07002579 * @pdev: PCI device whose resources are to be reserved
2580 * @bar: BAR to be reserved
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08002581 * @res_name: Name to be associated with resource
Arjan van de Vene8de1482008-10-22 19:55:31 -07002582 *
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08002583 * Mark the PCI region associated with PCI device @pdev BAR @bar as
Arjan van de Vene8de1482008-10-22 19:55:31 -07002584 * being reserved by owner @res_name. Do not access any
2585 * address inside the PCI regions unless this call returns
2586 * successfully.
2587 *
2588 * Returns 0 on success, or %EBUSY on error. A warning
2589 * message is also printed on failure.
2590 */
2591int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
2592{
2593 return __pci_request_region(pdev, bar, res_name, 0);
2594}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002595EXPORT_SYMBOL(pci_request_region);
Arjan van de Vene8de1482008-10-22 19:55:31 -07002596
2597/**
2598 * pci_request_region_exclusive - Reserved PCI I/O and memory resource
2599 * @pdev: PCI device whose resources are to be reserved
2600 * @bar: BAR to be reserved
2601 * @res_name: Name to be associated with resource.
2602 *
2603 * Mark the PCI region associated with PCI device @pdev BR @bar as
2604 * being reserved by owner @res_name. Do not access any
2605 * address inside the PCI regions unless this call returns
2606 * successfully.
2607 *
2608 * Returns 0 on success, or %EBUSY on error. A warning
2609 * message is also printed on failure.
2610 *
2611 * The key difference that _exclusive makes it that userspace is
2612 * explicitly not allowed to map the resource via /dev/mem or
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002613 * sysfs.
Arjan van de Vene8de1482008-10-22 19:55:31 -07002614 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002615int pci_request_region_exclusive(struct pci_dev *pdev, int bar,
2616 const char *res_name)
Arjan van de Vene8de1482008-10-22 19:55:31 -07002617{
2618 return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
2619}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002620EXPORT_SYMBOL(pci_request_region_exclusive);
2621
Arjan van de Vene8de1482008-10-22 19:55:31 -07002622/**
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09002623 * pci_release_selected_regions - Release selected PCI I/O and memory resources
2624 * @pdev: PCI device whose resources were previously reserved
2625 * @bars: Bitmask of BARs to be released
2626 *
2627 * Release selected PCI I/O and memory resources previously reserved.
2628 * Call this function only after all use of the PCI regions has ceased.
2629 */
2630void pci_release_selected_regions(struct pci_dev *pdev, int bars)
2631{
2632 int i;
2633
2634 for (i = 0; i < 6; i++)
2635 if (bars & (1 << i))
2636 pci_release_region(pdev, i);
2637}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002638EXPORT_SYMBOL(pci_release_selected_regions);
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09002639
Bjorn Helgaas9738abe2013-04-12 11:20:03 -06002640static int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002641 const char *res_name, int excl)
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09002642{
2643 int i;
2644
2645 for (i = 0; i < 6; i++)
2646 if (bars & (1 << i))
Arjan van de Vene8de1482008-10-22 19:55:31 -07002647 if (__pci_request_region(pdev, i, res_name, excl))
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09002648 goto err_out;
2649 return 0;
2650
2651err_out:
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002652 while (--i >= 0)
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09002653 if (bars & (1 << i))
2654 pci_release_region(pdev, i);
2655
2656 return -EBUSY;
2657}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Arjan van de Vene8de1482008-10-22 19:55:31 -07002659
2660/**
2661 * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
2662 * @pdev: PCI device whose resources are to be reserved
2663 * @bars: Bitmask of BARs to be requested
2664 * @res_name: Name to be associated with resource
2665 */
2666int pci_request_selected_regions(struct pci_dev *pdev, int bars,
2667 const char *res_name)
2668{
2669 return __pci_request_selected_regions(pdev, bars, res_name, 0);
2670}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002671EXPORT_SYMBOL(pci_request_selected_regions);
Arjan van de Vene8de1482008-10-22 19:55:31 -07002672
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002673int pci_request_selected_regions_exclusive(struct pci_dev *pdev, int bars,
2674 const char *res_name)
Arjan van de Vene8de1482008-10-22 19:55:31 -07002675{
2676 return __pci_request_selected_regions(pdev, bars, res_name,
2677 IORESOURCE_EXCLUSIVE);
2678}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002679EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
Arjan van de Vene8de1482008-10-22 19:55:31 -07002680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681/**
2682 * pci_release_regions - Release reserved PCI I/O and memory resources
2683 * @pdev: PCI device whose resources were previously reserved by pci_request_regions
2684 *
2685 * Releases all PCI I/O and memory resources previously reserved by a
2686 * successful call to pci_request_regions. Call this function only
2687 * after all use of the PCI regions has ceased.
2688 */
2689
2690void pci_release_regions(struct pci_dev *pdev)
2691{
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09002692 pci_release_selected_regions(pdev, (1 << 6) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002694EXPORT_SYMBOL(pci_release_regions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
2696/**
2697 * pci_request_regions - Reserved PCI I/O and memory resources
2698 * @pdev: PCI device whose resources are to be reserved
2699 * @res_name: Name to be associated with resource.
2700 *
2701 * Mark all PCI regions associated with PCI device @pdev as
2702 * being reserved by owner @res_name. Do not access any
2703 * address inside the PCI regions unless this call returns
2704 * successfully.
2705 *
2706 * Returns 0 on success, or %EBUSY on error. A warning
2707 * message is also printed on failure.
2708 */
Jeff Garzik3c990e92006-03-04 21:52:42 -05002709int pci_request_regions(struct pci_dev *pdev, const char *res_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09002711 return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002713EXPORT_SYMBOL(pci_request_regions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715/**
Arjan van de Vene8de1482008-10-22 19:55:31 -07002716 * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
2717 * @pdev: PCI device whose resources are to be reserved
2718 * @res_name: Name to be associated with resource.
2719 *
2720 * Mark all PCI regions associated with PCI device @pdev as
2721 * being reserved by owner @res_name. Do not access any
2722 * address inside the PCI regions unless this call returns
2723 * successfully.
2724 *
2725 * pci_request_regions_exclusive() will mark the region so that
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002726 * /dev/mem and the sysfs MMIO access will not be allowed.
Arjan van de Vene8de1482008-10-22 19:55:31 -07002727 *
2728 * Returns 0 on success, or %EBUSY on error. A warning
2729 * message is also printed on failure.
2730 */
2731int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
2732{
2733 return pci_request_selected_regions_exclusive(pdev,
2734 ((1 << 6) - 1), res_name);
2735}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002736EXPORT_SYMBOL(pci_request_regions_exclusive);
Arjan van de Vene8de1482008-10-22 19:55:31 -07002737
Liviu Dudau8b921ac2014-09-29 15:29:30 +01002738/**
2739 * pci_remap_iospace - Remap the memory mapped I/O space
2740 * @res: Resource describing the I/O space
2741 * @phys_addr: physical address of range to be mapped
2742 *
2743 * Remap the memory mapped I/O space described by the @res
2744 * and the CPU physical address @phys_addr into virtual address space.
2745 * Only architectures that have memory mapped IO functions defined
2746 * (and the PCI_IOBASE value defined) should call this function.
2747 */
2748int __weak pci_remap_iospace(const struct resource *res, phys_addr_t phys_addr)
2749{
2750#if defined(PCI_IOBASE) && defined(CONFIG_MMU)
2751 unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start;
2752
2753 if (!(res->flags & IORESOURCE_IO))
2754 return -EINVAL;
2755
2756 if (res->end > IO_SPACE_LIMIT)
2757 return -EINVAL;
2758
2759 return ioremap_page_range(vaddr, vaddr + resource_size(res), phys_addr,
2760 pgprot_device(PAGE_KERNEL));
2761#else
2762 /* this architecture does not have memory mapped I/O space,
2763 so this function should never be called */
2764 WARN_ONCE(1, "This architecture does not support memory mapped I/O\n");
2765 return -ENODEV;
2766#endif
2767}
2768
Ben Hutchings6a479072008-12-23 03:08:29 +00002769static void __pci_set_master(struct pci_dev *dev, bool enable)
2770{
2771 u16 old_cmd, cmd;
2772
2773 pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
2774 if (enable)
2775 cmd = old_cmd | PCI_COMMAND_MASTER;
2776 else
2777 cmd = old_cmd & ~PCI_COMMAND_MASTER;
2778 if (cmd != old_cmd) {
2779 dev_dbg(&dev->dev, "%s bus mastering\n",
2780 enable ? "enabling" : "disabling");
2781 pci_write_config_word(dev, PCI_COMMAND, cmd);
2782 }
2783 dev->is_busmaster = enable;
2784}
Arjan van de Vene8de1482008-10-22 19:55:31 -07002785
2786/**
Myron Stowe2b6f2c32012-06-25 21:30:57 -06002787 * pcibios_setup - process "pci=" kernel boot arguments
2788 * @str: string used to pass in "pci=" kernel boot arguments
2789 *
2790 * Process kernel boot arguments. This is the default implementation.
2791 * Architecture specific implementations can override this as necessary.
2792 */
2793char * __weak __init pcibios_setup(char *str)
2794{
2795 return str;
2796}
2797
2798/**
Myron Stowe96c55902011-10-28 15:48:38 -06002799 * pcibios_set_master - enable PCI bus-mastering for device dev
2800 * @dev: the PCI device to enable
2801 *
2802 * Enables PCI bus-mastering for the device. This is the default
2803 * implementation. Architecture specific implementations can override
2804 * this if necessary.
2805 */
2806void __weak pcibios_set_master(struct pci_dev *dev)
2807{
2808 u8 lat;
2809
Myron Stowef6766782011-10-28 15:49:20 -06002810 /* The latency timer doesn't apply to PCIe (either Type 0 or Type 1) */
2811 if (pci_is_pcie(dev))
2812 return;
2813
Myron Stowe96c55902011-10-28 15:48:38 -06002814 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
2815 if (lat < 16)
2816 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
2817 else if (lat > pcibios_max_latency)
2818 lat = pcibios_max_latency;
2819 else
2820 return;
Bjorn Helgaasa0064822013-09-23 15:25:26 -06002821
Myron Stowe96c55902011-10-28 15:48:38 -06002822 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
2823}
2824
2825/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 * pci_set_master - enables bus-mastering for device dev
2827 * @dev: the PCI device to enable
2828 *
2829 * Enables bus-mastering on the device and calls pcibios_set_master()
2830 * to do the needed arch specific settings.
2831 */
Ben Hutchings6a479072008-12-23 03:08:29 +00002832void pci_set_master(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
Ben Hutchings6a479072008-12-23 03:08:29 +00002834 __pci_set_master(dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 pcibios_set_master(dev);
2836}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002837EXPORT_SYMBOL(pci_set_master);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Ben Hutchings6a479072008-12-23 03:08:29 +00002839/**
2840 * pci_clear_master - disables bus-mastering for device dev
2841 * @dev: the PCI device to disable
2842 */
2843void pci_clear_master(struct pci_dev *dev)
2844{
2845 __pci_set_master(dev, false);
2846}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002847EXPORT_SYMBOL(pci_clear_master);
Ben Hutchings6a479072008-12-23 03:08:29 +00002848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849/**
Matthew Wilcoxedb2d972006-10-10 08:01:21 -06002850 * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
2851 * @dev: the PCI device for which MWI is to be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 *
Matthew Wilcoxedb2d972006-10-10 08:01:21 -06002853 * Helper function for pci_set_mwi.
2854 * Originally copied from drivers/net/acenic.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
2856 *
2857 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2858 */
Tejun Heo15ea76d2009-09-22 17:34:48 +09002859int pci_set_cacheline_size(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
2861 u8 cacheline_size;
2862
2863 if (!pci_cache_line_size)
Tejun Heo15ea76d2009-09-22 17:34:48 +09002864 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
2867 equal to or multiple of the right value. */
2868 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
2869 if (cacheline_size >= pci_cache_line_size &&
2870 (cacheline_size % pci_cache_line_size) == 0)
2871 return 0;
2872
2873 /* Write the correct value. */
2874 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
2875 /* Read it back. */
2876 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
2877 if (cacheline_size == pci_cache_line_size)
2878 return 0;
2879
Ryan Desfosses227f0642014-04-18 20:13:50 -04002880 dev_printk(KERN_DEBUG, &dev->dev, "cache line size of %d is not supported\n",
2881 pci_cache_line_size << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 return -EINVAL;
2884}
Tejun Heo15ea76d2009-09-22 17:34:48 +09002885EXPORT_SYMBOL_GPL(pci_set_cacheline_size);
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887/**
2888 * pci_set_mwi - enables memory-write-invalidate PCI transaction
2889 * @dev: the PCI device for which MWI is enabled
2890 *
Randy Dunlap694625c2007-07-09 11:55:54 -07002891 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 *
2893 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2894 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002895int pci_set_mwi(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002897#ifdef PCI_DISABLE_MWI
2898 return 0;
2899#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 int rc;
2901 u16 cmd;
2902
Matthew Wilcoxedb2d972006-10-10 08:01:21 -06002903 rc = pci_set_cacheline_size(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 if (rc)
2905 return rc;
2906
2907 pci_read_config_word(dev, PCI_COMMAND, &cmd);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002908 if (!(cmd & PCI_COMMAND_INVALIDATE)) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -06002909 dev_dbg(&dev->dev, "enabling Mem-Wr-Inval\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 cmd |= PCI_COMMAND_INVALIDATE;
2911 pci_write_config_word(dev, PCI_COMMAND, cmd);
2912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 return 0;
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002914#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002916EXPORT_SYMBOL(pci_set_mwi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918/**
Randy Dunlap694625c2007-07-09 11:55:54 -07002919 * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
2920 * @dev: the PCI device for which MWI is enabled
2921 *
2922 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
2923 * Callers are not required to check the return value.
2924 *
2925 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2926 */
2927int pci_try_set_mwi(struct pci_dev *dev)
2928{
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002929#ifdef PCI_DISABLE_MWI
2930 return 0;
2931#else
2932 return pci_set_mwi(dev);
2933#endif
Randy Dunlap694625c2007-07-09 11:55:54 -07002934}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002935EXPORT_SYMBOL(pci_try_set_mwi);
Randy Dunlap694625c2007-07-09 11:55:54 -07002936
2937/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
2939 * @dev: the PCI device to disable
2940 *
2941 * Disables PCI Memory-Write-Invalidate transaction on the device
2942 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002943void pci_clear_mwi(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944{
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002945#ifndef PCI_DISABLE_MWI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 u16 cmd;
2947
2948 pci_read_config_word(dev, PCI_COMMAND, &cmd);
2949 if (cmd & PCI_COMMAND_INVALIDATE) {
2950 cmd &= ~PCI_COMMAND_INVALIDATE;
2951 pci_write_config_word(dev, PCI_COMMAND, cmd);
2952 }
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002953#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002955EXPORT_SYMBOL(pci_clear_mwi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Brett M Russa04ce0f2005-08-15 15:23:41 -04002957/**
2958 * pci_intx - enables/disables PCI INTx for device dev
Randy Dunlap8f7020d2005-10-23 11:57:38 -07002959 * @pdev: the PCI device to operate on
2960 * @enable: boolean: whether to enable or disable PCI INTx
Brett M Russa04ce0f2005-08-15 15:23:41 -04002961 *
2962 * Enables/disables PCI INTx for device dev
2963 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002964void pci_intx(struct pci_dev *pdev, int enable)
Brett M Russa04ce0f2005-08-15 15:23:41 -04002965{
2966 u16 pci_command, new;
2967
2968 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
2969
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002970 if (enable)
Brett M Russa04ce0f2005-08-15 15:23:41 -04002971 new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002972 else
Brett M Russa04ce0f2005-08-15 15:23:41 -04002973 new = pci_command | PCI_COMMAND_INTX_DISABLE;
Brett M Russa04ce0f2005-08-15 15:23:41 -04002974
2975 if (new != pci_command) {
Tejun Heo9ac78492007-01-20 16:00:26 +09002976 struct pci_devres *dr;
2977
Brett M Russ2fd9d742005-09-09 10:02:22 -07002978 pci_write_config_word(pdev, PCI_COMMAND, new);
Tejun Heo9ac78492007-01-20 16:00:26 +09002979
2980 dr = find_pci_dr(pdev);
2981 if (dr && !dr->restore_intx) {
2982 dr->restore_intx = 1;
2983 dr->orig_intx = !enable;
2984 }
Brett M Russa04ce0f2005-08-15 15:23:41 -04002985 }
2986}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002987EXPORT_SYMBOL_GPL(pci_intx);
Brett M Russa04ce0f2005-08-15 15:23:41 -04002988
Eric W. Biedermanf5f2b132007-03-05 00:30:07 -08002989/**
Jan Kiszkaa2e27782011-11-04 09:46:00 +01002990 * pci_intx_mask_supported - probe for INTx masking support
Randy Dunlap6e9292c2012-01-21 11:02:35 -08002991 * @dev: the PCI device to operate on
Jan Kiszkaa2e27782011-11-04 09:46:00 +01002992 *
2993 * Check if the device dev support INTx masking via the config space
2994 * command word.
2995 */
2996bool pci_intx_mask_supported(struct pci_dev *dev)
2997{
2998 bool mask_supported = false;
2999 u16 orig, new;
3000
Bjorn Helgaasfbebb9f2012-06-16 14:40:22 -06003001 if (dev->broken_intx_masking)
3002 return false;
3003
Jan Kiszkaa2e27782011-11-04 09:46:00 +01003004 pci_cfg_access_lock(dev);
3005
3006 pci_read_config_word(dev, PCI_COMMAND, &orig);
3007 pci_write_config_word(dev, PCI_COMMAND,
3008 orig ^ PCI_COMMAND_INTX_DISABLE);
3009 pci_read_config_word(dev, PCI_COMMAND, &new);
3010
3011 /*
3012 * There's no way to protect against hardware bugs or detect them
3013 * reliably, but as long as we know what the value should be, let's
3014 * go ahead and check it.
3015 */
3016 if ((new ^ orig) & ~PCI_COMMAND_INTX_DISABLE) {
Ryan Desfosses227f0642014-04-18 20:13:50 -04003017 dev_err(&dev->dev, "Command register changed from 0x%x to 0x%x: driver or hardware bug?\n",
3018 orig, new);
Jan Kiszkaa2e27782011-11-04 09:46:00 +01003019 } else if ((new ^ orig) & PCI_COMMAND_INTX_DISABLE) {
3020 mask_supported = true;
3021 pci_write_config_word(dev, PCI_COMMAND, orig);
3022 }
3023
3024 pci_cfg_access_unlock(dev);
3025 return mask_supported;
3026}
3027EXPORT_SYMBOL_GPL(pci_intx_mask_supported);
3028
3029static bool pci_check_and_set_intx_mask(struct pci_dev *dev, bool mask)
3030{
3031 struct pci_bus *bus = dev->bus;
3032 bool mask_updated = true;
3033 u32 cmd_status_dword;
3034 u16 origcmd, newcmd;
3035 unsigned long flags;
3036 bool irq_pending;
3037
3038 /*
3039 * We do a single dword read to retrieve both command and status.
3040 * Document assumptions that make this possible.
3041 */
3042 BUILD_BUG_ON(PCI_COMMAND % 4);
3043 BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
3044
3045 raw_spin_lock_irqsave(&pci_lock, flags);
3046
3047 bus->ops->read(bus, dev->devfn, PCI_COMMAND, 4, &cmd_status_dword);
3048
3049 irq_pending = (cmd_status_dword >> 16) & PCI_STATUS_INTERRUPT;
3050
3051 /*
3052 * Check interrupt status register to see whether our device
3053 * triggered the interrupt (when masking) or the next IRQ is
3054 * already pending (when unmasking).
3055 */
3056 if (mask != irq_pending) {
3057 mask_updated = false;
3058 goto done;
3059 }
3060
3061 origcmd = cmd_status_dword;
3062 newcmd = origcmd & ~PCI_COMMAND_INTX_DISABLE;
3063 if (mask)
3064 newcmd |= PCI_COMMAND_INTX_DISABLE;
3065 if (newcmd != origcmd)
3066 bus->ops->write(bus, dev->devfn, PCI_COMMAND, 2, newcmd);
3067
3068done:
3069 raw_spin_unlock_irqrestore(&pci_lock, flags);
3070
3071 return mask_updated;
3072}
3073
3074/**
3075 * pci_check_and_mask_intx - mask INTx on pending interrupt
Randy Dunlap6e9292c2012-01-21 11:02:35 -08003076 * @dev: the PCI device to operate on
Jan Kiszkaa2e27782011-11-04 09:46:00 +01003077 *
3078 * Check if the device dev has its INTx line asserted, mask it and
3079 * return true in that case. False is returned if not interrupt was
3080 * pending.
3081 */
3082bool pci_check_and_mask_intx(struct pci_dev *dev)
3083{
3084 return pci_check_and_set_intx_mask(dev, true);
3085}
3086EXPORT_SYMBOL_GPL(pci_check_and_mask_intx);
3087
3088/**
Bjorn Helgaasebd50b92014-01-14 17:10:39 -07003089 * pci_check_and_unmask_intx - unmask INTx if no interrupt is pending
Randy Dunlap6e9292c2012-01-21 11:02:35 -08003090 * @dev: the PCI device to operate on
Jan Kiszkaa2e27782011-11-04 09:46:00 +01003091 *
3092 * Check if the device dev has its INTx line asserted, unmask it if not
3093 * and return true. False is returned and the mask remains active if
3094 * there was still an interrupt pending.
3095 */
3096bool pci_check_and_unmask_intx(struct pci_dev *dev)
3097{
3098 return pci_check_and_set_intx_mask(dev, false);
3099}
3100EXPORT_SYMBOL_GPL(pci_check_and_unmask_intx);
3101
3102/**
Bjorn Helgaasda27f4b2013-08-22 14:45:21 -06003103 * pci_msi_off - disables any MSI or MSI-X capabilities
Randy Dunlap8d7d86e2007-03-16 19:55:52 -07003104 * @dev: the PCI device to operate on
Eric W. Biedermanf5f2b132007-03-05 00:30:07 -08003105 *
Bjorn Helgaasda27f4b2013-08-22 14:45:21 -06003106 * If you want to use MSI, see pci_enable_msi() and friends.
3107 * This is a lower-level primitive that allows us to disable
3108 * MSI operation at the device level.
Eric W. Biedermanf5f2b132007-03-05 00:30:07 -08003109 */
3110void pci_msi_off(struct pci_dev *dev)
3111{
3112 int pos;
3113 u16 control;
3114
Bjorn Helgaasda27f4b2013-08-22 14:45:21 -06003115 /*
3116 * This looks like it could go in msi.c, but we need it even when
3117 * CONFIG_PCI_MSI=n. For the same reason, we can't use
3118 * dev->msi_cap or dev->msix_cap here.
3119 */
Eric W. Biedermanf5f2b132007-03-05 00:30:07 -08003120 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
3121 if (pos) {
3122 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
3123 control &= ~PCI_MSI_FLAGS_ENABLE;
3124 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
3125 }
3126 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
3127 if (pos) {
3128 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
3129 control &= ~PCI_MSIX_FLAGS_ENABLE;
3130 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
3131 }
3132}
Michael S. Tsirkinb03214d2010-06-23 22:49:06 -06003133EXPORT_SYMBOL_GPL(pci_msi_off);
Eric W. Biedermanf5f2b132007-03-05 00:30:07 -08003134
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08003135int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size)
3136{
3137 return dma_set_max_seg_size(&dev->dev, size);
3138}
3139EXPORT_SYMBOL(pci_set_dma_max_seg_size);
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08003140
FUJITA Tomonori59fc67d2008-02-04 22:28:14 -08003141int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask)
3142{
3143 return dma_set_seg_boundary(&dev->dev, mask);
3144}
3145EXPORT_SYMBOL(pci_set_dma_seg_boundary);
FUJITA Tomonori59fc67d2008-02-04 22:28:14 -08003146
Casey Leedom3775a202013-08-06 15:48:36 +05303147/**
3148 * pci_wait_for_pending_transaction - waits for pending transaction
3149 * @dev: the PCI device to operate on
3150 *
3151 * Return 0 if transaction is pending 1 otherwise.
3152 */
3153int pci_wait_for_pending_transaction(struct pci_dev *dev)
Sheng Yang8dd7f802008-10-21 17:38:25 +08003154{
Alex Williamson157e8762013-12-17 16:43:39 -07003155 if (!pci_is_pcie(dev))
3156 return 1;
Sheng Yang8dd7f802008-10-21 17:38:25 +08003157
Gavin Shand0b4cc42014-05-19 13:06:46 +10003158 return pci_wait_for_pending(dev, pci_pcie_cap(dev) + PCI_EXP_DEVSTA,
3159 PCI_EXP_DEVSTA_TRPND);
Casey Leedom3775a202013-08-06 15:48:36 +05303160}
3161EXPORT_SYMBOL(pci_wait_for_pending_transaction);
Sheng Yang5fe5db02009-02-09 14:53:47 +08003162
Casey Leedom3775a202013-08-06 15:48:36 +05303163static int pcie_flr(struct pci_dev *dev, int probe)
3164{
3165 u32 cap;
3166
3167 pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
3168 if (!(cap & PCI_EXP_DEVCAP_FLR))
3169 return -ENOTTY;
3170
3171 if (probe)
3172 return 0;
3173
3174 if (!pci_wait_for_pending_transaction(dev))
Gavin Shanbb383e22014-11-12 13:41:51 +11003175 dev_err(&dev->dev, "timed out waiting for pending transaction; performing function level reset anyway\n");
Casey Leedom3775a202013-08-06 15:48:36 +05303176
Jiang Liu59875ae2012-07-24 17:20:06 +08003177 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
Yu Zhao8c1c6992009-06-13 15:52:13 +08003178 msleep(100);
Sheng Yang8dd7f802008-10-21 17:38:25 +08003179 return 0;
3180}
Sheng Yangd91cdc72008-11-11 17:17:47 +08003181
Yu Zhao8c1c6992009-06-13 15:52:13 +08003182static int pci_af_flr(struct pci_dev *dev, int probe)
Sheng Yang1ca88792008-11-11 17:17:48 +08003183{
Yu Zhao8c1c6992009-06-13 15:52:13 +08003184 int pos;
Sheng Yang1ca88792008-11-11 17:17:48 +08003185 u8 cap;
3186
Yu Zhao8c1c6992009-06-13 15:52:13 +08003187 pos = pci_find_capability(dev, PCI_CAP_ID_AF);
3188 if (!pos)
Sheng Yang1ca88792008-11-11 17:17:48 +08003189 return -ENOTTY;
Yu Zhao8c1c6992009-06-13 15:52:13 +08003190
3191 pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap);
Sheng Yang1ca88792008-11-11 17:17:48 +08003192 if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
3193 return -ENOTTY;
3194
3195 if (probe)
3196 return 0;
3197
Alex Williamsond066c942014-06-17 15:40:13 -06003198 /*
3199 * Wait for Transaction Pending bit to clear. A word-aligned test
3200 * is used, so we use the conrol offset rather than status and shift
3201 * the test bit to match.
3202 */
Gavin Shanbb383e22014-11-12 13:41:51 +11003203 if (!pci_wait_for_pending(dev, pos + PCI_AF_CTRL,
Alex Williamsond066c942014-06-17 15:40:13 -06003204 PCI_AF_STATUS_TP << 8))
Gavin Shanbb383e22014-11-12 13:41:51 +11003205 dev_err(&dev->dev, "timed out waiting for pending transaction; performing AF function level reset anyway\n");
Yu Zhao8c1c6992009-06-13 15:52:13 +08003206
Yu Zhao8c1c6992009-06-13 15:52:13 +08003207 pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
Sheng Yang1ca88792008-11-11 17:17:48 +08003208 msleep(100);
Sheng Yang1ca88792008-11-11 17:17:48 +08003209 return 0;
3210}
3211
Rafael J. Wysocki83d74e02011-03-05 21:48:44 +01003212/**
3213 * pci_pm_reset - Put device into PCI_D3 and back into PCI_D0.
3214 * @dev: Device to reset.
3215 * @probe: If set, only check if the device can be reset this way.
3216 *
3217 * If @dev supports native PCI PM and its PCI_PM_CTRL_NO_SOFT_RESET flag is
3218 * unset, it will be reinitialized internally when going from PCI_D3hot to
3219 * PCI_D0. If that's the case and the device is not in a low-power state
3220 * already, force it into PCI_D3hot and back to PCI_D0, causing it to be reset.
3221 *
3222 * NOTE: This causes the caller to sleep for twice the device power transition
3223 * cooldown period, which for the D0->D3hot and D3hot->D0 transitions is 10 ms
Bjorn Helgaasf7625982013-11-14 11:28:18 -07003224 * by default (i.e. unless the @dev's d3_delay field has a different value).
Rafael J. Wysocki83d74e02011-03-05 21:48:44 +01003225 * Moreover, only devices in D0 can be reset by this function.
3226 */
Yu Zhaof85876b2009-06-13 15:52:14 +08003227static int pci_pm_reset(struct pci_dev *dev, int probe)
Sheng Yangd91cdc72008-11-11 17:17:47 +08003228{
Yu Zhaof85876b2009-06-13 15:52:14 +08003229 u16 csr;
Sheng Yangd91cdc72008-11-11 17:17:47 +08003230
Alex Williamson51e53732014-11-21 11:24:08 -07003231 if (!dev->pm_cap || dev->dev_flags & PCI_DEV_FLAGS_NO_PM_RESET)
Yu Zhaof85876b2009-06-13 15:52:14 +08003232 return -ENOTTY;
Sheng Yangd91cdc72008-11-11 17:17:47 +08003233
Yu Zhaof85876b2009-06-13 15:52:14 +08003234 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr);
3235 if (csr & PCI_PM_CTRL_NO_SOFT_RESET)
3236 return -ENOTTY;
Sheng Yang1ca88792008-11-11 17:17:48 +08003237
Yu Zhaof85876b2009-06-13 15:52:14 +08003238 if (probe)
3239 return 0;
3240
3241 if (dev->current_state != PCI_D0)
3242 return -EINVAL;
3243
3244 csr &= ~PCI_PM_CTRL_STATE_MASK;
3245 csr |= PCI_D3hot;
3246 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +01003247 pci_dev_d3_sleep(dev);
Yu Zhaof85876b2009-06-13 15:52:14 +08003248
3249 csr &= ~PCI_PM_CTRL_STATE_MASK;
3250 csr |= PCI_D0;
3251 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +01003252 pci_dev_d3_sleep(dev);
Yu Zhaof85876b2009-06-13 15:52:14 +08003253
3254 return 0;
3255}
3256
Gavin Shan9e330022014-06-19 17:22:44 +10003257void pci_reset_secondary_bus(struct pci_dev *dev)
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08003258{
3259 u16 ctrl;
Alex Williamson64e86742013-08-08 14:09:24 -06003260
3261 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &ctrl);
3262 ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
3263 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
Alex Williamsonde0c5482013-08-08 14:10:13 -06003264 /*
3265 * PCI spec v3.0 7.6.4.2 requires minimum Trst of 1ms. Double
Bjorn Helgaasf7625982013-11-14 11:28:18 -07003266 * this to 2ms to ensure that we meet the minimum requirement.
Alex Williamsonde0c5482013-08-08 14:10:13 -06003267 */
3268 msleep(2);
Alex Williamson64e86742013-08-08 14:09:24 -06003269
3270 ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
3271 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
Alex Williamsonde0c5482013-08-08 14:10:13 -06003272
3273 /*
3274 * Trhfa for conventional PCI is 2^25 clock cycles.
3275 * Assuming a minimum 33MHz clock this results in a 1s
3276 * delay before we can consider subordinate devices to
3277 * be re-initialized. PCIe has some ways to shorten this,
3278 * but we don't make use of them yet.
3279 */
3280 ssleep(1);
Alex Williamson64e86742013-08-08 14:09:24 -06003281}
Gavin Shand92a2082014-04-24 18:00:24 +10003282
Gavin Shan9e330022014-06-19 17:22:44 +10003283void __weak pcibios_reset_secondary_bus(struct pci_dev *dev)
3284{
3285 pci_reset_secondary_bus(dev);
3286}
3287
Gavin Shand92a2082014-04-24 18:00:24 +10003288/**
3289 * pci_reset_bridge_secondary_bus - Reset the secondary bus on a PCI bridge.
3290 * @dev: Bridge device
3291 *
3292 * Use the bridge control register to assert reset on the secondary bus.
3293 * Devices on the secondary bus are left in power-on state.
3294 */
3295void pci_reset_bridge_secondary_bus(struct pci_dev *dev)
3296{
3297 pcibios_reset_secondary_bus(dev);
3298}
Alex Williamson64e86742013-08-08 14:09:24 -06003299EXPORT_SYMBOL_GPL(pci_reset_bridge_secondary_bus);
3300
3301static int pci_parent_bus_reset(struct pci_dev *dev, int probe)
3302{
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08003303 struct pci_dev *pdev;
3304
Alex Williamsonf331a852015-01-15 18:16:04 -06003305 if (pci_is_root_bus(dev->bus) || dev->subordinate ||
3306 !dev->bus->self || dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08003307 return -ENOTTY;
3308
3309 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
3310 if (pdev != dev)
3311 return -ENOTTY;
3312
3313 if (probe)
3314 return 0;
3315
Alex Williamson64e86742013-08-08 14:09:24 -06003316 pci_reset_bridge_secondary_bus(dev->bus->self);
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08003317
3318 return 0;
3319}
3320
Alex Williamson608c3882013-08-08 14:09:43 -06003321static int pci_reset_hotplug_slot(struct hotplug_slot *hotplug, int probe)
3322{
3323 int rc = -ENOTTY;
3324
3325 if (!hotplug || !try_module_get(hotplug->ops->owner))
3326 return rc;
3327
3328 if (hotplug->ops->reset_slot)
3329 rc = hotplug->ops->reset_slot(hotplug, probe);
3330
3331 module_put(hotplug->ops->owner);
3332
3333 return rc;
3334}
3335
3336static int pci_dev_reset_slot_function(struct pci_dev *dev, int probe)
3337{
3338 struct pci_dev *pdev;
3339
Alex Williamsonf331a852015-01-15 18:16:04 -06003340 if (dev->subordinate || !dev->slot ||
3341 dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)
Alex Williamson608c3882013-08-08 14:09:43 -06003342 return -ENOTTY;
3343
3344 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
3345 if (pdev != dev && pdev->slot == dev->slot)
3346 return -ENOTTY;
3347
3348 return pci_reset_hotplug_slot(dev->slot->hotplug, probe);
3349}
3350
Konrad Rzeszutek Wilk977f8572012-04-24 13:15:18 -06003351static int __pci_dev_reset(struct pci_dev *dev, int probe)
Sheng Yang8dd7f802008-10-21 17:38:25 +08003352{
Yu Zhao8c1c6992009-06-13 15:52:13 +08003353 int rc;
Sheng Yang8dd7f802008-10-21 17:38:25 +08003354
Yu Zhao8c1c6992009-06-13 15:52:13 +08003355 might_sleep();
Sheng Yang8dd7f802008-10-21 17:38:25 +08003356
Dexuan Cuib9c3b262009-12-07 13:03:21 +08003357 rc = pci_dev_specific_reset(dev, probe);
3358 if (rc != -ENOTTY)
3359 goto done;
3360
Yu Zhao8c1c6992009-06-13 15:52:13 +08003361 rc = pcie_flr(dev, probe);
3362 if (rc != -ENOTTY)
3363 goto done;
3364
3365 rc = pci_af_flr(dev, probe);
Yu Zhaof85876b2009-06-13 15:52:14 +08003366 if (rc != -ENOTTY)
3367 goto done;
3368
3369 rc = pci_pm_reset(dev, probe);
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08003370 if (rc != -ENOTTY)
3371 goto done;
3372
Alex Williamson608c3882013-08-08 14:09:43 -06003373 rc = pci_dev_reset_slot_function(dev, probe);
3374 if (rc != -ENOTTY)
3375 goto done;
3376
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08003377 rc = pci_parent_bus_reset(dev, probe);
Yu Zhao8c1c6992009-06-13 15:52:13 +08003378done:
Konrad Rzeszutek Wilk977f8572012-04-24 13:15:18 -06003379 return rc;
3380}
3381
Alex Williamson77cb9852013-08-08 14:09:49 -06003382static void pci_dev_lock(struct pci_dev *dev)
3383{
3384 pci_cfg_access_lock(dev);
3385 /* block PM suspend, driver probe, etc. */
3386 device_lock(&dev->dev);
3387}
3388
Alex Williamson61cf16d2013-12-16 15:14:31 -07003389/* Return 1 on successful lock, 0 on contention */
3390static int pci_dev_trylock(struct pci_dev *dev)
3391{
3392 if (pci_cfg_access_trylock(dev)) {
3393 if (device_trylock(&dev->dev))
3394 return 1;
3395 pci_cfg_access_unlock(dev);
3396 }
3397
3398 return 0;
3399}
3400
Alex Williamson77cb9852013-08-08 14:09:49 -06003401static void pci_dev_unlock(struct pci_dev *dev)
3402{
3403 device_unlock(&dev->dev);
3404 pci_cfg_access_unlock(dev);
3405}
3406
Keith Busch3ebe7f92014-05-02 10:40:42 -06003407/**
3408 * pci_reset_notify - notify device driver of reset
3409 * @dev: device to be notified of reset
3410 * @prepare: 'true' if device is about to be reset; 'false' if reset attempt
3411 * completed
3412 *
3413 * Must be called prior to device access being disabled and after device
3414 * access is restored.
3415 */
3416static void pci_reset_notify(struct pci_dev *dev, bool prepare)
3417{
3418 const struct pci_error_handlers *err_handler =
3419 dev->driver ? dev->driver->err_handler : NULL;
3420 if (err_handler && err_handler->reset_notify)
3421 err_handler->reset_notify(dev, prepare);
3422}
3423
Alex Williamson77cb9852013-08-08 14:09:49 -06003424static void pci_dev_save_and_disable(struct pci_dev *dev)
3425{
Keith Busch3ebe7f92014-05-02 10:40:42 -06003426 pci_reset_notify(dev, true);
3427
Alex Williamsona6cbaad2013-08-08 14:10:02 -06003428 /*
3429 * Wake-up device prior to save. PM registers default to D0 after
3430 * reset and a simple register restore doesn't reliably return
3431 * to a non-D0 state anyway.
3432 */
3433 pci_set_power_state(dev, PCI_D0);
3434
Alex Williamson77cb9852013-08-08 14:09:49 -06003435 pci_save_state(dev);
3436 /*
3437 * Disable the device by clearing the Command register, except for
3438 * INTx-disable which is set. This not only disables MMIO and I/O port
3439 * BARs, but also prevents the device from being Bus Master, preventing
3440 * DMA from the device including MSI/MSI-X interrupts. For PCI 2.3
3441 * compliant devices, INTx-disable prevents legacy interrupts.
3442 */
3443 pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
3444}
3445
3446static void pci_dev_restore(struct pci_dev *dev)
3447{
3448 pci_restore_state(dev);
Keith Busch3ebe7f92014-05-02 10:40:42 -06003449 pci_reset_notify(dev, false);
Alex Williamson77cb9852013-08-08 14:09:49 -06003450}
3451
Konrad Rzeszutek Wilk977f8572012-04-24 13:15:18 -06003452static int pci_dev_reset(struct pci_dev *dev, int probe)
3453{
3454 int rc;
3455
Alex Williamson77cb9852013-08-08 14:09:49 -06003456 if (!probe)
3457 pci_dev_lock(dev);
Konrad Rzeszutek Wilk977f8572012-04-24 13:15:18 -06003458
3459 rc = __pci_dev_reset(dev, probe);
3460
Alex Williamson77cb9852013-08-08 14:09:49 -06003461 if (!probe)
3462 pci_dev_unlock(dev);
3463
Yu Zhao8c1c6992009-06-13 15:52:13 +08003464 return rc;
Sheng Yang8dd7f802008-10-21 17:38:25 +08003465}
Keith Busch3ebe7f92014-05-02 10:40:42 -06003466
Sheng Yang8dd7f802008-10-21 17:38:25 +08003467/**
Yu Zhao8c1c6992009-06-13 15:52:13 +08003468 * __pci_reset_function - reset a PCI device function
3469 * @dev: PCI device to reset
Sheng Yang8dd7f802008-10-21 17:38:25 +08003470 *
3471 * Some devices allow an individual function to be reset without affecting
3472 * other functions in the same device. The PCI device must be responsive
3473 * to PCI config space in order to use this function.
3474 *
3475 * The device function is presumed to be unused when this function is called.
3476 * Resetting the device will make the contents of PCI configuration space
3477 * random, so any caller of this must be prepared to reinitialise the
3478 * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
3479 * etc.
3480 *
Yu Zhao8c1c6992009-06-13 15:52:13 +08003481 * Returns 0 if the device function was successfully reset or negative if the
Sheng Yang8dd7f802008-10-21 17:38:25 +08003482 * device doesn't support resetting a single function.
3483 */
Yu Zhao8c1c6992009-06-13 15:52:13 +08003484int __pci_reset_function(struct pci_dev *dev)
Sheng Yang8dd7f802008-10-21 17:38:25 +08003485{
Yu Zhao8c1c6992009-06-13 15:52:13 +08003486 return pci_dev_reset(dev, 0);
Sheng Yang8dd7f802008-10-21 17:38:25 +08003487}
Yu Zhao8c1c6992009-06-13 15:52:13 +08003488EXPORT_SYMBOL_GPL(__pci_reset_function);
Sheng Yang8dd7f802008-10-21 17:38:25 +08003489
3490/**
Konrad Rzeszutek Wilk6fbf9e72012-01-12 12:06:46 -05003491 * __pci_reset_function_locked - reset a PCI device function while holding
3492 * the @dev mutex lock.
3493 * @dev: PCI device to reset
3494 *
3495 * Some devices allow an individual function to be reset without affecting
3496 * other functions in the same device. The PCI device must be responsive
3497 * to PCI config space in order to use this function.
3498 *
3499 * The device function is presumed to be unused and the caller is holding
3500 * the device mutex lock when this function is called.
3501 * Resetting the device will make the contents of PCI configuration space
3502 * random, so any caller of this must be prepared to reinitialise the
3503 * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
3504 * etc.
3505 *
3506 * Returns 0 if the device function was successfully reset or negative if the
3507 * device doesn't support resetting a single function.
3508 */
3509int __pci_reset_function_locked(struct pci_dev *dev)
3510{
Konrad Rzeszutek Wilk977f8572012-04-24 13:15:18 -06003511 return __pci_dev_reset(dev, 0);
Konrad Rzeszutek Wilk6fbf9e72012-01-12 12:06:46 -05003512}
3513EXPORT_SYMBOL_GPL(__pci_reset_function_locked);
3514
3515/**
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03003516 * pci_probe_reset_function - check whether the device can be safely reset
3517 * @dev: PCI device to reset
3518 *
3519 * Some devices allow an individual function to be reset without affecting
3520 * other functions in the same device. The PCI device must be responsive
3521 * to PCI config space in order to use this function.
3522 *
3523 * Returns 0 if the device function can be reset or negative if the
3524 * device doesn't support resetting a single function.
3525 */
3526int pci_probe_reset_function(struct pci_dev *dev)
3527{
3528 return pci_dev_reset(dev, 1);
3529}
3530
3531/**
Yu Zhao8c1c6992009-06-13 15:52:13 +08003532 * pci_reset_function - quiesce and reset a PCI device function
3533 * @dev: PCI device to reset
Sheng Yang8dd7f802008-10-21 17:38:25 +08003534 *
3535 * Some devices allow an individual function to be reset without affecting
3536 * other functions in the same device. The PCI device must be responsive
3537 * to PCI config space in order to use this function.
3538 *
3539 * This function does not just reset the PCI portion of a device, but
3540 * clears all the state associated with the device. This function differs
Yu Zhao8c1c6992009-06-13 15:52:13 +08003541 * from __pci_reset_function in that it saves and restores device state
Sheng Yang8dd7f802008-10-21 17:38:25 +08003542 * over the reset.
3543 *
Yu Zhao8c1c6992009-06-13 15:52:13 +08003544 * Returns 0 if the device function was successfully reset or negative if the
Sheng Yang8dd7f802008-10-21 17:38:25 +08003545 * device doesn't support resetting a single function.
3546 */
3547int pci_reset_function(struct pci_dev *dev)
3548{
Yu Zhao8c1c6992009-06-13 15:52:13 +08003549 int rc;
Sheng Yang8dd7f802008-10-21 17:38:25 +08003550
Yu Zhao8c1c6992009-06-13 15:52:13 +08003551 rc = pci_dev_reset(dev, 1);
3552 if (rc)
3553 return rc;
Sheng Yang8dd7f802008-10-21 17:38:25 +08003554
Alex Williamson77cb9852013-08-08 14:09:49 -06003555 pci_dev_save_and_disable(dev);
Sheng Yang8dd7f802008-10-21 17:38:25 +08003556
Yu Zhao8c1c6992009-06-13 15:52:13 +08003557 rc = pci_dev_reset(dev, 0);
Sheng Yang8dd7f802008-10-21 17:38:25 +08003558
Alex Williamson77cb9852013-08-08 14:09:49 -06003559 pci_dev_restore(dev);
Sheng Yang8dd7f802008-10-21 17:38:25 +08003560
Yu Zhao8c1c6992009-06-13 15:52:13 +08003561 return rc;
Sheng Yang8dd7f802008-10-21 17:38:25 +08003562}
3563EXPORT_SYMBOL_GPL(pci_reset_function);
3564
Alex Williamson61cf16d2013-12-16 15:14:31 -07003565/**
3566 * pci_try_reset_function - quiesce and reset a PCI device function
3567 * @dev: PCI device to reset
3568 *
3569 * Same as above, except return -EAGAIN if unable to lock device.
3570 */
3571int pci_try_reset_function(struct pci_dev *dev)
3572{
3573 int rc;
3574
3575 rc = pci_dev_reset(dev, 1);
3576 if (rc)
3577 return rc;
3578
3579 pci_dev_save_and_disable(dev);
3580
3581 if (pci_dev_trylock(dev)) {
3582 rc = __pci_dev_reset(dev, 0);
3583 pci_dev_unlock(dev);
3584 } else
3585 rc = -EAGAIN;
3586
3587 pci_dev_restore(dev);
3588
3589 return rc;
3590}
3591EXPORT_SYMBOL_GPL(pci_try_reset_function);
3592
Alex Williamsonf331a852015-01-15 18:16:04 -06003593/* Do any devices on or below this bus prevent a bus reset? */
3594static bool pci_bus_resetable(struct pci_bus *bus)
3595{
3596 struct pci_dev *dev;
3597
3598 list_for_each_entry(dev, &bus->devices, bus_list) {
3599 if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET ||
3600 (dev->subordinate && !pci_bus_resetable(dev->subordinate)))
3601 return false;
3602 }
3603
3604 return true;
3605}
3606
Alex Williamson090a3c52013-08-08 14:09:55 -06003607/* Lock devices from the top of the tree down */
3608static void pci_bus_lock(struct pci_bus *bus)
3609{
3610 struct pci_dev *dev;
3611
3612 list_for_each_entry(dev, &bus->devices, bus_list) {
3613 pci_dev_lock(dev);
3614 if (dev->subordinate)
3615 pci_bus_lock(dev->subordinate);
3616 }
3617}
3618
3619/* Unlock devices from the bottom of the tree up */
3620static void pci_bus_unlock(struct pci_bus *bus)
3621{
3622 struct pci_dev *dev;
3623
3624 list_for_each_entry(dev, &bus->devices, bus_list) {
3625 if (dev->subordinate)
3626 pci_bus_unlock(dev->subordinate);
3627 pci_dev_unlock(dev);
3628 }
3629}
3630
Alex Williamson61cf16d2013-12-16 15:14:31 -07003631/* Return 1 on successful lock, 0 on contention */
3632static int pci_bus_trylock(struct pci_bus *bus)
3633{
3634 struct pci_dev *dev;
3635
3636 list_for_each_entry(dev, &bus->devices, bus_list) {
3637 if (!pci_dev_trylock(dev))
3638 goto unlock;
3639 if (dev->subordinate) {
3640 if (!pci_bus_trylock(dev->subordinate)) {
3641 pci_dev_unlock(dev);
3642 goto unlock;
3643 }
3644 }
3645 }
3646 return 1;
3647
3648unlock:
3649 list_for_each_entry_continue_reverse(dev, &bus->devices, bus_list) {
3650 if (dev->subordinate)
3651 pci_bus_unlock(dev->subordinate);
3652 pci_dev_unlock(dev);
3653 }
3654 return 0;
3655}
3656
Alex Williamsonf331a852015-01-15 18:16:04 -06003657/* Do any devices on or below this slot prevent a bus reset? */
3658static bool pci_slot_resetable(struct pci_slot *slot)
3659{
3660 struct pci_dev *dev;
3661
3662 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
3663 if (!dev->slot || dev->slot != slot)
3664 continue;
3665 if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET ||
3666 (dev->subordinate && !pci_bus_resetable(dev->subordinate)))
3667 return false;
3668 }
3669
3670 return true;
3671}
3672
Alex Williamson090a3c52013-08-08 14:09:55 -06003673/* Lock devices from the top of the tree down */
3674static void pci_slot_lock(struct pci_slot *slot)
3675{
3676 struct pci_dev *dev;
3677
3678 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
3679 if (!dev->slot || dev->slot != slot)
3680 continue;
3681 pci_dev_lock(dev);
3682 if (dev->subordinate)
3683 pci_bus_lock(dev->subordinate);
3684 }
3685}
3686
3687/* Unlock devices from the bottom of the tree up */
3688static void pci_slot_unlock(struct pci_slot *slot)
3689{
3690 struct pci_dev *dev;
3691
3692 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
3693 if (!dev->slot || dev->slot != slot)
3694 continue;
3695 if (dev->subordinate)
3696 pci_bus_unlock(dev->subordinate);
3697 pci_dev_unlock(dev);
3698 }
3699}
3700
Alex Williamson61cf16d2013-12-16 15:14:31 -07003701/* Return 1 on successful lock, 0 on contention */
3702static int pci_slot_trylock(struct pci_slot *slot)
3703{
3704 struct pci_dev *dev;
3705
3706 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
3707 if (!dev->slot || dev->slot != slot)
3708 continue;
3709 if (!pci_dev_trylock(dev))
3710 goto unlock;
3711 if (dev->subordinate) {
3712 if (!pci_bus_trylock(dev->subordinate)) {
3713 pci_dev_unlock(dev);
3714 goto unlock;
3715 }
3716 }
3717 }
3718 return 1;
3719
3720unlock:
3721 list_for_each_entry_continue_reverse(dev,
3722 &slot->bus->devices, bus_list) {
3723 if (!dev->slot || dev->slot != slot)
3724 continue;
3725 if (dev->subordinate)
3726 pci_bus_unlock(dev->subordinate);
3727 pci_dev_unlock(dev);
3728 }
3729 return 0;
3730}
3731
Alex Williamson090a3c52013-08-08 14:09:55 -06003732/* Save and disable devices from the top of the tree down */
3733static void pci_bus_save_and_disable(struct pci_bus *bus)
3734{
3735 struct pci_dev *dev;
3736
3737 list_for_each_entry(dev, &bus->devices, bus_list) {
3738 pci_dev_save_and_disable(dev);
3739 if (dev->subordinate)
3740 pci_bus_save_and_disable(dev->subordinate);
3741 }
3742}
3743
3744/*
3745 * Restore devices from top of the tree down - parent bridges need to be
3746 * restored before we can get to subordinate devices.
3747 */
3748static void pci_bus_restore(struct pci_bus *bus)
3749{
3750 struct pci_dev *dev;
3751
3752 list_for_each_entry(dev, &bus->devices, bus_list) {
3753 pci_dev_restore(dev);
3754 if (dev->subordinate)
3755 pci_bus_restore(dev->subordinate);
3756 }
3757}
3758
3759/* Save and disable devices from the top of the tree down */
3760static void pci_slot_save_and_disable(struct pci_slot *slot)
3761{
3762 struct pci_dev *dev;
3763
3764 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
3765 if (!dev->slot || dev->slot != slot)
3766 continue;
3767 pci_dev_save_and_disable(dev);
3768 if (dev->subordinate)
3769 pci_bus_save_and_disable(dev->subordinate);
3770 }
3771}
3772
3773/*
3774 * Restore devices from top of the tree down - parent bridges need to be
3775 * restored before we can get to subordinate devices.
3776 */
3777static void pci_slot_restore(struct pci_slot *slot)
3778{
3779 struct pci_dev *dev;
3780
3781 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
3782 if (!dev->slot || dev->slot != slot)
3783 continue;
3784 pci_dev_restore(dev);
3785 if (dev->subordinate)
3786 pci_bus_restore(dev->subordinate);
3787 }
3788}
3789
3790static int pci_slot_reset(struct pci_slot *slot, int probe)
3791{
3792 int rc;
3793
Alex Williamsonf331a852015-01-15 18:16:04 -06003794 if (!slot || !pci_slot_resetable(slot))
Alex Williamson090a3c52013-08-08 14:09:55 -06003795 return -ENOTTY;
3796
3797 if (!probe)
3798 pci_slot_lock(slot);
3799
3800 might_sleep();
3801
3802 rc = pci_reset_hotplug_slot(slot->hotplug, probe);
3803
3804 if (!probe)
3805 pci_slot_unlock(slot);
3806
3807 return rc;
3808}
3809
3810/**
Alex Williamson9a3d2b92013-08-14 14:06:05 -06003811 * pci_probe_reset_slot - probe whether a PCI slot can be reset
3812 * @slot: PCI slot to probe
3813 *
3814 * Return 0 if slot can be reset, negative if a slot reset is not supported.
3815 */
3816int pci_probe_reset_slot(struct pci_slot *slot)
3817{
3818 return pci_slot_reset(slot, 1);
3819}
3820EXPORT_SYMBOL_GPL(pci_probe_reset_slot);
3821
3822/**
Alex Williamson090a3c52013-08-08 14:09:55 -06003823 * pci_reset_slot - reset a PCI slot
3824 * @slot: PCI slot to reset
3825 *
3826 * A PCI bus may host multiple slots, each slot may support a reset mechanism
3827 * independent of other slots. For instance, some slots may support slot power
3828 * control. In the case of a 1:1 bus to slot architecture, this function may
3829 * wrap the bus reset to avoid spurious slot related events such as hotplug.
3830 * Generally a slot reset should be attempted before a bus reset. All of the
3831 * function of the slot and any subordinate buses behind the slot are reset
3832 * through this function. PCI config space of all devices in the slot and
3833 * behind the slot is saved before and restored after reset.
3834 *
3835 * Return 0 on success, non-zero on error.
3836 */
3837int pci_reset_slot(struct pci_slot *slot)
3838{
3839 int rc;
3840
3841 rc = pci_slot_reset(slot, 1);
3842 if (rc)
3843 return rc;
3844
3845 pci_slot_save_and_disable(slot);
3846
3847 rc = pci_slot_reset(slot, 0);
3848
3849 pci_slot_restore(slot);
3850
3851 return rc;
3852}
3853EXPORT_SYMBOL_GPL(pci_reset_slot);
3854
Alex Williamson61cf16d2013-12-16 15:14:31 -07003855/**
3856 * pci_try_reset_slot - Try to reset a PCI slot
3857 * @slot: PCI slot to reset
3858 *
3859 * Same as above except return -EAGAIN if the slot cannot be locked
3860 */
3861int pci_try_reset_slot(struct pci_slot *slot)
3862{
3863 int rc;
3864
3865 rc = pci_slot_reset(slot, 1);
3866 if (rc)
3867 return rc;
3868
3869 pci_slot_save_and_disable(slot);
3870
3871 if (pci_slot_trylock(slot)) {
3872 might_sleep();
3873 rc = pci_reset_hotplug_slot(slot->hotplug, 0);
3874 pci_slot_unlock(slot);
3875 } else
3876 rc = -EAGAIN;
3877
3878 pci_slot_restore(slot);
3879
3880 return rc;
3881}
3882EXPORT_SYMBOL_GPL(pci_try_reset_slot);
3883
Alex Williamson090a3c52013-08-08 14:09:55 -06003884static int pci_bus_reset(struct pci_bus *bus, int probe)
3885{
Alex Williamsonf331a852015-01-15 18:16:04 -06003886 if (!bus->self || !pci_bus_resetable(bus))
Alex Williamson090a3c52013-08-08 14:09:55 -06003887 return -ENOTTY;
3888
3889 if (probe)
3890 return 0;
3891
3892 pci_bus_lock(bus);
3893
3894 might_sleep();
3895
3896 pci_reset_bridge_secondary_bus(bus->self);
3897
3898 pci_bus_unlock(bus);
3899
3900 return 0;
3901}
3902
3903/**
Alex Williamson9a3d2b92013-08-14 14:06:05 -06003904 * pci_probe_reset_bus - probe whether a PCI bus can be reset
3905 * @bus: PCI bus to probe
3906 *
3907 * Return 0 if bus can be reset, negative if a bus reset is not supported.
3908 */
3909int pci_probe_reset_bus(struct pci_bus *bus)
3910{
3911 return pci_bus_reset(bus, 1);
3912}
3913EXPORT_SYMBOL_GPL(pci_probe_reset_bus);
3914
3915/**
Alex Williamson090a3c52013-08-08 14:09:55 -06003916 * pci_reset_bus - reset a PCI bus
3917 * @bus: top level PCI bus to reset
3918 *
3919 * Do a bus reset on the given bus and any subordinate buses, saving
3920 * and restoring state of all devices.
3921 *
3922 * Return 0 on success, non-zero on error.
3923 */
3924int pci_reset_bus(struct pci_bus *bus)
3925{
3926 int rc;
3927
3928 rc = pci_bus_reset(bus, 1);
3929 if (rc)
3930 return rc;
3931
3932 pci_bus_save_and_disable(bus);
3933
3934 rc = pci_bus_reset(bus, 0);
3935
3936 pci_bus_restore(bus);
3937
3938 return rc;
3939}
3940EXPORT_SYMBOL_GPL(pci_reset_bus);
3941
Sheng Yang8dd7f802008-10-21 17:38:25 +08003942/**
Alex Williamson61cf16d2013-12-16 15:14:31 -07003943 * pci_try_reset_bus - Try to reset a PCI bus
3944 * @bus: top level PCI bus to reset
3945 *
3946 * Same as above except return -EAGAIN if the bus cannot be locked
3947 */
3948int pci_try_reset_bus(struct pci_bus *bus)
3949{
3950 int rc;
3951
3952 rc = pci_bus_reset(bus, 1);
3953 if (rc)
3954 return rc;
3955
3956 pci_bus_save_and_disable(bus);
3957
3958 if (pci_bus_trylock(bus)) {
3959 might_sleep();
3960 pci_reset_bridge_secondary_bus(bus->self);
3961 pci_bus_unlock(bus);
3962 } else
3963 rc = -EAGAIN;
3964
3965 pci_bus_restore(bus);
3966
3967 return rc;
3968}
3969EXPORT_SYMBOL_GPL(pci_try_reset_bus);
3970
3971/**
Peter Orubad556ad42007-05-15 13:59:13 +02003972 * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
3973 * @dev: PCI device to query
3974 *
3975 * Returns mmrbc: maximum designed memory read count in bytes
3976 * or appropriate error value.
3977 */
3978int pcix_get_max_mmrbc(struct pci_dev *dev)
3979{
Dean Nelson7c9e2b12010-03-09 22:26:55 -05003980 int cap;
Peter Orubad556ad42007-05-15 13:59:13 +02003981 u32 stat;
3982
3983 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
3984 if (!cap)
3985 return -EINVAL;
3986
Dean Nelson7c9e2b12010-03-09 22:26:55 -05003987 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
Peter Orubad556ad42007-05-15 13:59:13 +02003988 return -EINVAL;
3989
Dean Nelson25daeb52010-03-09 22:26:40 -05003990 return 512 << ((stat & PCI_X_STATUS_MAX_READ) >> 21);
Peter Orubad556ad42007-05-15 13:59:13 +02003991}
3992EXPORT_SYMBOL(pcix_get_max_mmrbc);
3993
3994/**
3995 * pcix_get_mmrbc - get PCI-X maximum memory read byte count
3996 * @dev: PCI device to query
3997 *
3998 * Returns mmrbc: maximum memory read count in bytes
3999 * or appropriate error value.
4000 */
4001int pcix_get_mmrbc(struct pci_dev *dev)
4002{
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004003 int cap;
Dean Nelsonbdc2bda2010-03-09 22:26:48 -05004004 u16 cmd;
Peter Orubad556ad42007-05-15 13:59:13 +02004005
4006 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
4007 if (!cap)
4008 return -EINVAL;
4009
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004010 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
4011 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02004012
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004013 return 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
Peter Orubad556ad42007-05-15 13:59:13 +02004014}
4015EXPORT_SYMBOL(pcix_get_mmrbc);
4016
4017/**
4018 * pcix_set_mmrbc - set PCI-X maximum memory read byte count
4019 * @dev: PCI device to query
4020 * @mmrbc: maximum memory read count in bytes
4021 * valid values are 512, 1024, 2048, 4096
4022 *
4023 * If possible sets maximum memory read byte count, some bridges have erratas
4024 * that prevent this.
4025 */
4026int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
4027{
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004028 int cap;
Dean Nelsonbdc2bda2010-03-09 22:26:48 -05004029 u32 stat, v, o;
4030 u16 cmd;
Peter Orubad556ad42007-05-15 13:59:13 +02004031
vignesh babu229f5af2007-08-13 18:23:14 +05304032 if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004033 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02004034
4035 v = ffs(mmrbc) - 10;
4036
4037 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
4038 if (!cap)
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004039 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02004040
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004041 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
4042 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02004043
4044 if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
4045 return -E2BIG;
4046
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004047 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
4048 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02004049
4050 o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
4051 if (o != v) {
Bjorn Helgaas809a3bf2012-06-20 16:41:16 -06004052 if (v > o && (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
Peter Orubad556ad42007-05-15 13:59:13 +02004053 return -EIO;
4054
4055 cmd &= ~PCI_X_CMD_MAX_READ;
4056 cmd |= v << 2;
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004057 if (pci_write_config_word(dev, cap + PCI_X_CMD, cmd))
4058 return -EIO;
Peter Orubad556ad42007-05-15 13:59:13 +02004059 }
Dean Nelson7c9e2b12010-03-09 22:26:55 -05004060 return 0;
Peter Orubad556ad42007-05-15 13:59:13 +02004061}
4062EXPORT_SYMBOL(pcix_set_mmrbc);
4063
4064/**
4065 * pcie_get_readrq - get PCI Express read request size
4066 * @dev: PCI device to query
4067 *
4068 * Returns maximum memory read request in bytes
4069 * or appropriate error value.
4070 */
4071int pcie_get_readrq(struct pci_dev *dev)
4072{
Peter Orubad556ad42007-05-15 13:59:13 +02004073 u16 ctl;
4074
Jiang Liu59875ae2012-07-24 17:20:06 +08004075 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
Peter Orubad556ad42007-05-15 13:59:13 +02004076
Jiang Liu59875ae2012-07-24 17:20:06 +08004077 return 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
Peter Orubad556ad42007-05-15 13:59:13 +02004078}
4079EXPORT_SYMBOL(pcie_get_readrq);
4080
4081/**
4082 * pcie_set_readrq - set PCI Express maximum memory read request
4083 * @dev: PCI device to query
Randy Dunlap42e61f42007-07-23 21:42:11 -07004084 * @rq: maximum memory read count in bytes
Peter Orubad556ad42007-05-15 13:59:13 +02004085 * valid values are 128, 256, 512, 1024, 2048, 4096
4086 *
Jon Masonc9b378c2011-06-28 18:26:25 -05004087 * If possible sets maximum memory read request in bytes
Peter Orubad556ad42007-05-15 13:59:13 +02004088 */
4089int pcie_set_readrq(struct pci_dev *dev, int rq)
4090{
Jiang Liu59875ae2012-07-24 17:20:06 +08004091 u16 v;
Peter Orubad556ad42007-05-15 13:59:13 +02004092
vignesh babu229f5af2007-08-13 18:23:14 +05304093 if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
Jiang Liu59875ae2012-07-24 17:20:06 +08004094 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02004095
Benjamin Herrenschmidta1c473a2011-10-14 14:56:15 -05004096 /*
4097 * If using the "performance" PCIe config, we clamp the
4098 * read rq size to the max packet size to prevent the
4099 * host bridge generating requests larger than we can
4100 * cope with
4101 */
4102 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
4103 int mps = pcie_get_mps(dev);
4104
Benjamin Herrenschmidta1c473a2011-10-14 14:56:15 -05004105 if (mps < rq)
4106 rq = mps;
4107 }
4108
4109 v = (ffs(rq) - 8) << 12;
Peter Orubad556ad42007-05-15 13:59:13 +02004110
Jiang Liu59875ae2012-07-24 17:20:06 +08004111 return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
4112 PCI_EXP_DEVCTL_READRQ, v);
Peter Orubad556ad42007-05-15 13:59:13 +02004113}
4114EXPORT_SYMBOL(pcie_set_readrq);
4115
4116/**
Jon Masonb03e7492011-07-20 15:20:54 -05004117 * pcie_get_mps - get PCI Express maximum payload size
4118 * @dev: PCI device to query
4119 *
4120 * Returns maximum payload size in bytes
Jon Masonb03e7492011-07-20 15:20:54 -05004121 */
4122int pcie_get_mps(struct pci_dev *dev)
4123{
Jon Masonb03e7492011-07-20 15:20:54 -05004124 u16 ctl;
4125
Jiang Liu59875ae2012-07-24 17:20:06 +08004126 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
Jon Masonb03e7492011-07-20 15:20:54 -05004127
Jiang Liu59875ae2012-07-24 17:20:06 +08004128 return 128 << ((ctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
Jon Masonb03e7492011-07-20 15:20:54 -05004129}
Yijing Wangf1c66c42013-09-24 12:08:06 -06004130EXPORT_SYMBOL(pcie_get_mps);
Jon Masonb03e7492011-07-20 15:20:54 -05004131
4132/**
4133 * pcie_set_mps - set PCI Express maximum payload size
4134 * @dev: PCI device to query
Randy Dunlap47c08f32011-08-20 11:49:43 -07004135 * @mps: maximum payload size in bytes
Jon Masonb03e7492011-07-20 15:20:54 -05004136 * valid values are 128, 256, 512, 1024, 2048, 4096
4137 *
4138 * If possible sets maximum payload size
4139 */
4140int pcie_set_mps(struct pci_dev *dev, int mps)
4141{
Jiang Liu59875ae2012-07-24 17:20:06 +08004142 u16 v;
Jon Masonb03e7492011-07-20 15:20:54 -05004143
4144 if (mps < 128 || mps > 4096 || !is_power_of_2(mps))
Jiang Liu59875ae2012-07-24 17:20:06 +08004145 return -EINVAL;
Jon Masonb03e7492011-07-20 15:20:54 -05004146
4147 v = ffs(mps) - 8;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07004148 if (v > dev->pcie_mpss)
Jiang Liu59875ae2012-07-24 17:20:06 +08004149 return -EINVAL;
Jon Masonb03e7492011-07-20 15:20:54 -05004150 v <<= 5;
4151
Jiang Liu59875ae2012-07-24 17:20:06 +08004152 return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
4153 PCI_EXP_DEVCTL_PAYLOAD, v);
Jon Masonb03e7492011-07-20 15:20:54 -05004154}
Yijing Wangf1c66c42013-09-24 12:08:06 -06004155EXPORT_SYMBOL(pcie_set_mps);
Jon Masonb03e7492011-07-20 15:20:54 -05004156
4157/**
Jacob Keller81377c82013-07-31 06:53:26 +00004158 * pcie_get_minimum_link - determine minimum link settings of a PCI device
4159 * @dev: PCI device to query
4160 * @speed: storage for minimum speed
4161 * @width: storage for minimum width
4162 *
4163 * This function will walk up the PCI device chain and determine the minimum
4164 * link width and speed of the device.
4165 */
4166int pcie_get_minimum_link(struct pci_dev *dev, enum pci_bus_speed *speed,
4167 enum pcie_link_width *width)
4168{
4169 int ret;
4170
4171 *speed = PCI_SPEED_UNKNOWN;
4172 *width = PCIE_LNK_WIDTH_UNKNOWN;
4173
4174 while (dev) {
4175 u16 lnksta;
4176 enum pci_bus_speed next_speed;
4177 enum pcie_link_width next_width;
4178
4179 ret = pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
4180 if (ret)
4181 return ret;
4182
4183 next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
4184 next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >>
4185 PCI_EXP_LNKSTA_NLW_SHIFT;
4186
4187 if (next_speed < *speed)
4188 *speed = next_speed;
4189
4190 if (next_width < *width)
4191 *width = next_width;
4192
4193 dev = dev->bus->self;
4194 }
4195
4196 return 0;
4197}
4198EXPORT_SYMBOL(pcie_get_minimum_link);
4199
4200/**
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09004201 * pci_select_bars - Make BAR mask from the type of resource
Randy Dunlapf95d8822007-02-10 14:41:56 -08004202 * @dev: the PCI device for which BAR mask is made
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09004203 * @flags: resource type mask to be selected
4204 *
4205 * This helper routine makes bar mask from the type of resource.
4206 */
4207int pci_select_bars(struct pci_dev *dev, unsigned long flags)
4208{
4209 int i, bars = 0;
4210 for (i = 0; i < PCI_NUM_RESOURCES; i++)
4211 if (pci_resource_flags(dev, i) & flags)
4212 bars |= (1 << i);
4213 return bars;
4214}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06004215EXPORT_SYMBOL(pci_select_bars);
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09004216
Yu Zhao613e7ed2008-11-22 02:41:27 +08004217/**
4218 * pci_resource_bar - get position of the BAR associated with a resource
4219 * @dev: the PCI device
4220 * @resno: the resource number
4221 * @type: the BAR type to be filled in
4222 *
4223 * Returns BAR position in config space, or 0 if the BAR is invalid.
4224 */
4225int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)
4226{
Yu Zhaod1b054d2009-03-20 11:25:11 +08004227 int reg;
4228
Yu Zhao613e7ed2008-11-22 02:41:27 +08004229 if (resno < PCI_ROM_RESOURCE) {
4230 *type = pci_bar_unknown;
4231 return PCI_BASE_ADDRESS_0 + 4 * resno;
4232 } else if (resno == PCI_ROM_RESOURCE) {
4233 *type = pci_bar_mem32;
4234 return dev->rom_base_reg;
Yu Zhaod1b054d2009-03-20 11:25:11 +08004235 } else if (resno < PCI_BRIDGE_RESOURCES) {
4236 /* device specific resource */
Myron Stowe26ff46c2014-11-11 08:04:50 -07004237 *type = pci_bar_unknown;
4238 reg = pci_iov_resource_bar(dev, resno);
Yu Zhaod1b054d2009-03-20 11:25:11 +08004239 if (reg)
4240 return reg;
Yu Zhao613e7ed2008-11-22 02:41:27 +08004241 }
4242
Bjorn Helgaas865df572009-11-04 10:32:57 -07004243 dev_err(&dev->dev, "BAR %d: invalid resource\n", resno);
Yu Zhao613e7ed2008-11-22 02:41:27 +08004244 return 0;
4245}
4246
Mike Travis95a8b6e2010-02-02 14:38:13 -08004247/* Some architectures require additional programming to enable VGA */
4248static arch_set_vga_state_t arch_set_vga_state;
4249
4250void __init pci_register_set_vga_state(arch_set_vga_state_t func)
4251{
4252 arch_set_vga_state = func; /* NULL disables */
4253}
4254
4255static int pci_set_vga_state_arch(struct pci_dev *dev, bool decode,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04004256 unsigned int command_bits, u32 flags)
Mike Travis95a8b6e2010-02-02 14:38:13 -08004257{
4258 if (arch_set_vga_state)
4259 return arch_set_vga_state(dev, decode, command_bits,
Dave Airlie7ad35cf2011-05-25 14:00:49 +10004260 flags);
Mike Travis95a8b6e2010-02-02 14:38:13 -08004261 return 0;
4262}
4263
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10004264/**
4265 * pci_set_vga_state - set VGA decode state on device and parents if requested
Randy Dunlap19eea632009-09-17 15:28:22 -07004266 * @dev: the PCI device
4267 * @decode: true = enable decoding, false = disable decoding
4268 * @command_bits: PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY
Randy Dunlap3f37d622011-05-25 19:21:25 -07004269 * @flags: traverse ancestors and change bridges
Dave Airlie3448a192010-06-01 15:32:24 +10004270 * CHANGE_BRIDGE_ONLY / CHANGE_BRIDGE
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10004271 */
4272int pci_set_vga_state(struct pci_dev *dev, bool decode,
Dave Airlie3448a192010-06-01 15:32:24 +10004273 unsigned int command_bits, u32 flags)
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10004274{
4275 struct pci_bus *bus;
4276 struct pci_dev *bridge;
4277 u16 cmd;
Mike Travis95a8b6e2010-02-02 14:38:13 -08004278 int rc;
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10004279
Bjorn Helgaas67ebd812014-04-05 15:14:22 -06004280 WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) && (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY)));
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10004281
Mike Travis95a8b6e2010-02-02 14:38:13 -08004282 /* ARCH specific VGA enables */
Dave Airlie3448a192010-06-01 15:32:24 +10004283 rc = pci_set_vga_state_arch(dev, decode, command_bits, flags);
Mike Travis95a8b6e2010-02-02 14:38:13 -08004284 if (rc)
4285 return rc;
4286
Dave Airlie3448a192010-06-01 15:32:24 +10004287 if (flags & PCI_VGA_STATE_CHANGE_DECODES) {
4288 pci_read_config_word(dev, PCI_COMMAND, &cmd);
4289 if (decode == true)
4290 cmd |= command_bits;
4291 else
4292 cmd &= ~command_bits;
4293 pci_write_config_word(dev, PCI_COMMAND, cmd);
4294 }
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10004295
Dave Airlie3448a192010-06-01 15:32:24 +10004296 if (!(flags & PCI_VGA_STATE_CHANGE_BRIDGE))
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10004297 return 0;
4298
4299 bus = dev->bus;
4300 while (bus) {
4301 bridge = bus->self;
4302 if (bridge) {
4303 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
4304 &cmd);
4305 if (decode == true)
4306 cmd |= PCI_BRIDGE_CTL_VGA;
4307 else
4308 cmd &= ~PCI_BRIDGE_CTL_VGA;
4309 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL,
4310 cmd);
4311 }
4312 bus = bus->parent;
4313 }
4314 return 0;
4315}
4316
Rafael J. Wysocki8496e852013-12-01 02:34:37 +01004317bool pci_device_is_present(struct pci_dev *pdev)
4318{
4319 u32 v;
4320
4321 return pci_bus_read_dev_vendor_id(pdev->bus, pdev->devfn, &v, 0);
4322}
4323EXPORT_SYMBOL_GPL(pci_device_is_present);
4324
Yuji Shimada32a9a6822009-03-16 17:13:39 +09004325#define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZE
4326static char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE] = {0};
Thomas Gleixnere9d1e492009-11-06 22:41:23 +00004327static DEFINE_SPINLOCK(resource_alignment_lock);
Yuji Shimada32a9a6822009-03-16 17:13:39 +09004328
4329/**
4330 * pci_specified_resource_alignment - get resource alignment specified by user.
4331 * @dev: the PCI device to get
4332 *
4333 * RETURNS: Resource alignment if it is specified.
4334 * Zero if it is not specified.
4335 */
Bjorn Helgaas9738abe2013-04-12 11:20:03 -06004336static resource_size_t pci_specified_resource_alignment(struct pci_dev *dev)
Yuji Shimada32a9a6822009-03-16 17:13:39 +09004337{
4338 int seg, bus, slot, func, align_order, count;
4339 resource_size_t align = 0;
4340 char *p;
4341
4342 spin_lock(&resource_alignment_lock);
4343 p = resource_alignment_param;
4344 while (*p) {
4345 count = 0;
4346 if (sscanf(p, "%d%n", &align_order, &count) == 1 &&
4347 p[count] == '@') {
4348 p += count + 1;
4349 } else {
4350 align_order = -1;
4351 }
4352 if (sscanf(p, "%x:%x:%x.%x%n",
4353 &seg, &bus, &slot, &func, &count) != 4) {
4354 seg = 0;
4355 if (sscanf(p, "%x:%x.%x%n",
4356 &bus, &slot, &func, &count) != 3) {
4357 /* Invalid format */
4358 printk(KERN_ERR "PCI: Can't parse resource_alignment parameter: %s\n",
4359 p);
4360 break;
4361 }
4362 }
4363 p += count;
4364 if (seg == pci_domain_nr(dev->bus) &&
4365 bus == dev->bus->number &&
4366 slot == PCI_SLOT(dev->devfn) &&
4367 func == PCI_FUNC(dev->devfn)) {
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04004368 if (align_order == -1)
Yuji Shimada32a9a6822009-03-16 17:13:39 +09004369 align = PAGE_SIZE;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04004370 else
Yuji Shimada32a9a6822009-03-16 17:13:39 +09004371 align = 1 << align_order;
Yuji Shimada32a9a6822009-03-16 17:13:39 +09004372 /* Found */
4373 break;
4374 }
4375 if (*p != ';' && *p != ',') {
4376 /* End of param or invalid format */
4377 break;
4378 }
4379 p++;
4380 }
4381 spin_unlock(&resource_alignment_lock);
4382 return align;
4383}
4384
Yinghai Lu2069ecf2012-02-15 21:40:31 -08004385/*
4386 * This function disables memory decoding and releases memory resources
4387 * of the device specified by kernel's boot parameter 'pci=resource_alignment='.
4388 * It also rounds up size to specified alignment.
4389 * Later on, the kernel will assign page-aligned memory resource back
4390 * to the device.
4391 */
4392void pci_reassigndev_resource_alignment(struct pci_dev *dev)
4393{
4394 int i;
4395 struct resource *r;
4396 resource_size_t align, size;
4397 u16 command;
4398
Yinghai Lu10c463a2012-03-18 22:46:26 -07004399 /* check if specified PCI is target device to reassign */
4400 align = pci_specified_resource_alignment(dev);
4401 if (!align)
Yinghai Lu2069ecf2012-02-15 21:40:31 -08004402 return;
4403
4404 if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL &&
4405 (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) {
4406 dev_warn(&dev->dev,
4407 "Can't reassign resources to host bridge.\n");
4408 return;
4409 }
4410
4411 dev_info(&dev->dev,
4412 "Disabling memory decoding and releasing memory resources.\n");
4413 pci_read_config_word(dev, PCI_COMMAND, &command);
4414 command &= ~PCI_COMMAND_MEMORY;
4415 pci_write_config_word(dev, PCI_COMMAND, command);
4416
Yinghai Lu2069ecf2012-02-15 21:40:31 -08004417 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
4418 r = &dev->resource[i];
4419 if (!(r->flags & IORESOURCE_MEM))
4420 continue;
4421 size = resource_size(r);
4422 if (size < align) {
4423 size = align;
4424 dev_info(&dev->dev,
4425 "Rounding up size of resource #%d to %#llx.\n",
4426 i, (unsigned long long)size);
4427 }
Bjorn Helgaasbd064f02014-02-26 11:25:58 -07004428 r->flags |= IORESOURCE_UNSET;
Yinghai Lu2069ecf2012-02-15 21:40:31 -08004429 r->end = size - 1;
4430 r->start = 0;
4431 }
4432 /* Need to disable bridge's resource window,
4433 * to enable the kernel to reassign new resource
4434 * window later on.
4435 */
4436 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
4437 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
4438 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
4439 r = &dev->resource[i];
4440 if (!(r->flags & IORESOURCE_MEM))
4441 continue;
Bjorn Helgaasbd064f02014-02-26 11:25:58 -07004442 r->flags |= IORESOURCE_UNSET;
Yinghai Lu2069ecf2012-02-15 21:40:31 -08004443 r->end = resource_size(r) - 1;
4444 r->start = 0;
4445 }
4446 pci_disable_bridge_window(dev);
4447 }
4448}
4449
Bjorn Helgaas9738abe2013-04-12 11:20:03 -06004450static ssize_t pci_set_resource_alignment_param(const char *buf, size_t count)
Yuji Shimada32a9a6822009-03-16 17:13:39 +09004451{
4452 if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1)
4453 count = RESOURCE_ALIGNMENT_PARAM_SIZE - 1;
4454 spin_lock(&resource_alignment_lock);
4455 strncpy(resource_alignment_param, buf, count);
4456 resource_alignment_param[count] = '\0';
4457 spin_unlock(&resource_alignment_lock);
4458 return count;
4459}
4460
Bjorn Helgaas9738abe2013-04-12 11:20:03 -06004461static ssize_t pci_get_resource_alignment_param(char *buf, size_t size)
Yuji Shimada32a9a6822009-03-16 17:13:39 +09004462{
4463 size_t count;
4464 spin_lock(&resource_alignment_lock);
4465 count = snprintf(buf, size, "%s", resource_alignment_param);
4466 spin_unlock(&resource_alignment_lock);
4467 return count;
4468}
4469
4470static ssize_t pci_resource_alignment_show(struct bus_type *bus, char *buf)
4471{
4472 return pci_get_resource_alignment_param(buf, PAGE_SIZE);
4473}
4474
4475static ssize_t pci_resource_alignment_store(struct bus_type *bus,
4476 const char *buf, size_t count)
4477{
4478 return pci_set_resource_alignment_param(buf, count);
4479}
4480
4481BUS_ATTR(resource_alignment, 0644, pci_resource_alignment_show,
4482 pci_resource_alignment_store);
4483
4484static int __init pci_resource_alignment_sysfs_init(void)
4485{
4486 return bus_create_file(&pci_bus_type,
4487 &bus_attr_resource_alignment);
4488}
Yuji Shimada32a9a6822009-03-16 17:13:39 +09004489late_initcall(pci_resource_alignment_sysfs_init);
4490
Bill Pemberton15856ad2012-11-21 15:35:00 -05004491static void pci_no_domains(void)
Jeff Garzik32a2eea2007-10-11 16:57:27 -04004492{
4493#ifdef CONFIG_PCI_DOMAINS
4494 pci_domains_supported = 0;
4495#endif
4496}
4497
Liviu Dudau41e5c0f2014-09-29 15:29:27 +01004498#ifdef CONFIG_PCI_DOMAINS
4499static atomic_t __domain_nr = ATOMIC_INIT(-1);
4500
4501int pci_get_new_domain_nr(void)
4502{
4503 return atomic_inc_return(&__domain_nr);
4504}
Lorenzo Pieralisi7c674702014-12-27 18:19:12 -07004505
4506#ifdef CONFIG_PCI_DOMAINS_GENERIC
4507void pci_bus_assign_domain_nr(struct pci_bus *bus, struct device *parent)
4508{
4509 static int use_dt_domains = -1;
4510 int domain = of_get_pci_domain_nr(parent->of_node);
4511
4512 /*
4513 * Check DT domain and use_dt_domains values.
4514 *
4515 * If DT domain property is valid (domain >= 0) and
4516 * use_dt_domains != 0, the DT assignment is valid since this means
4517 * we have not previously allocated a domain number by using
4518 * pci_get_new_domain_nr(); we should also update use_dt_domains to
4519 * 1, to indicate that we have just assigned a domain number from
4520 * DT.
4521 *
4522 * If DT domain property value is not valid (ie domain < 0), and we
4523 * have not previously assigned a domain number from DT
4524 * (use_dt_domains != 1) we should assign a domain number by
4525 * using the:
4526 *
4527 * pci_get_new_domain_nr()
4528 *
4529 * API and update the use_dt_domains value to keep track of method we
4530 * are using to assign domain numbers (use_dt_domains = 0).
4531 *
4532 * All other combinations imply we have a platform that is trying
4533 * to mix domain numbers obtained from DT and pci_get_new_domain_nr(),
4534 * which is a recipe for domain mishandling and it is prevented by
4535 * invalidating the domain value (domain = -1) and printing a
4536 * corresponding error.
4537 */
4538 if (domain >= 0 && use_dt_domains) {
4539 use_dt_domains = 1;
4540 } else if (domain < 0 && use_dt_domains != 1) {
4541 use_dt_domains = 0;
4542 domain = pci_get_new_domain_nr();
4543 } else {
4544 dev_err(parent, "Node %s has inconsistent \"linux,pci-domain\" property in DT\n",
4545 parent->of_node->full_name);
4546 domain = -1;
4547 }
4548
4549 bus->domain_nr = domain;
4550}
4551#endif
Liviu Dudau41e5c0f2014-09-29 15:29:27 +01004552#endif
4553
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -07004554/**
Taku Izumi642c92d2012-10-30 15:26:18 +09004555 * pci_ext_cfg_avail - can we access extended PCI config space?
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -07004556 *
4557 * Returns 1 if we can access PCI extended config space (offsets
4558 * greater than 0xff). This is the default implementation. Architecture
4559 * implementations can override this.
4560 */
Taku Izumi642c92d2012-10-30 15:26:18 +09004561int __weak pci_ext_cfg_avail(void)
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -07004562{
4563 return 1;
4564}
4565
Benjamin Herrenschmidt2d1c8612009-12-09 17:52:13 +11004566void __weak pci_fixup_cardbus(struct pci_bus *bus)
4567{
4568}
4569EXPORT_SYMBOL(pci_fixup_cardbus);
4570
Al Viroad04d312008-11-22 17:37:14 +00004571static int __init pci_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572{
4573 while (str) {
4574 char *k = strchr(str, ',');
4575 if (k)
4576 *k++ = 0;
4577 if (*str && (str = pcibios_setup(str)) && *str) {
Matthew Wilcox309e57d2006-03-05 22:33:34 -07004578 if (!strcmp(str, "nomsi")) {
4579 pci_no_msi();
Randy Dunlap7f785762007-10-05 13:17:58 -07004580 } else if (!strcmp(str, "noaer")) {
4581 pci_no_aer();
Yinghai Lub55438f2012-02-23 19:23:30 -08004582 } else if (!strncmp(str, "realloc=", 8)) {
4583 pci_realloc_get_opt(str + 8);
Ram Paif483d392011-07-07 11:19:10 -07004584 } else if (!strncmp(str, "realloc", 7)) {
Yinghai Lub55438f2012-02-23 19:23:30 -08004585 pci_realloc_get_opt("on");
Jeff Garzik32a2eea2007-10-11 16:57:27 -04004586 } else if (!strcmp(str, "nodomains")) {
4587 pci_no_domains();
Rafael J. Wysocki6748dcc2012-03-01 00:06:33 +01004588 } else if (!strncmp(str, "noari", 5)) {
4589 pcie_ari_disabled = true;
Atsushi Nemoto4516a612007-02-05 16:36:06 -08004590 } else if (!strncmp(str, "cbiosize=", 9)) {
4591 pci_cardbus_io_size = memparse(str + 9, &str);
4592 } else if (!strncmp(str, "cbmemsize=", 10)) {
4593 pci_cardbus_mem_size = memparse(str + 10, &str);
Yuji Shimada32a9a6822009-03-16 17:13:39 +09004594 } else if (!strncmp(str, "resource_alignment=", 19)) {
4595 pci_set_resource_alignment_param(str + 19,
4596 strlen(str + 19));
Andrew Patterson43c16402009-04-22 16:52:09 -06004597 } else if (!strncmp(str, "ecrc=", 5)) {
4598 pcie_ecrc_get_policy(str + 5);
Eric W. Biederman28760482009-09-09 14:09:24 -07004599 } else if (!strncmp(str, "hpiosize=", 9)) {
4600 pci_hotplug_io_size = memparse(str + 9, &str);
4601 } else if (!strncmp(str, "hpmemsize=", 10)) {
4602 pci_hotplug_mem_size = memparse(str + 10, &str);
Jon Mason5f39e672011-10-03 09:50:20 -05004603 } else if (!strncmp(str, "pcie_bus_tune_off", 17)) {
4604 pcie_bus_config = PCIE_BUS_TUNE_OFF;
Jon Masonb03e7492011-07-20 15:20:54 -05004605 } else if (!strncmp(str, "pcie_bus_safe", 13)) {
4606 pcie_bus_config = PCIE_BUS_SAFE;
4607 } else if (!strncmp(str, "pcie_bus_perf", 13)) {
4608 pcie_bus_config = PCIE_BUS_PERFORMANCE;
Jon Mason5f39e672011-10-03 09:50:20 -05004609 } else if (!strncmp(str, "pcie_bus_peer2peer", 18)) {
4610 pcie_bus_config = PCIE_BUS_PEER2PEER;
Bjorn Helgaas284f5f92012-04-30 15:21:02 -06004611 } else if (!strncmp(str, "pcie_scan_all", 13)) {
4612 pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS);
Matthew Wilcox309e57d2006-03-05 22:33:34 -07004613 } else {
4614 printk(KERN_ERR "PCI: Unknown option `%s'\n",
4615 str);
4616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 }
4618 str = k;
4619 }
Andi Kleen0637a702006-09-26 10:52:41 +02004620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621}
Andi Kleen0637a702006-09-26 10:52:41 +02004622early_param("pci", pci_setup);