blob: de9386da2eb2386140fbc268f99628326de12e6c [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>
13#include <linux/pci.h>
David Brownell075c1772007-04-26 00:12:06 -070014#include <linux/pm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/spinlock.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080018#include <linux/string.h>
vignesh babu229f5af2007-08-13 18:23:14 +053019#include <linux/log2.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080020#include <linux/pci-aspm.h>
Stephen Rothwellc300bd2fb2008-07-10 02:16:44 +020021#include <linux/pm_wakeup.h>
Sheng Yang8dd7f802008-10-21 17:38:25 +080022#include <linux/interrupt.h>
Yuji Shimada32a9a6822009-03-16 17:13:39 +090023#include <linux/device.h>
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010024#include <linux/pm_runtime.h>
Bjorn Helgaas284f5f92012-04-30 15:21:02 -060025#include <asm-generic/pci-bridge.h>
Yuji Shimada32a9a6822009-03-16 17:13:39 +090026#include <asm/setup.h>
Greg KHbc56b9e2005-04-08 14:53:31 +090027#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Alan Stern00240c32009-04-27 13:33:16 -040029const char *pci_power_names[] = {
30 "error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown",
31};
32EXPORT_SYMBOL_GPL(pci_power_names);
33
Rafael J. Wysocki93177a72010-01-02 22:57:24 +010034int isa_dma_bridge_buggy;
35EXPORT_SYMBOL(isa_dma_bridge_buggy);
36
37int pci_pci_problems;
38EXPORT_SYMBOL(pci_pci_problems);
39
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +010040unsigned int pci_pm_d3_delay;
41
Matthew Garrettdf17e622010-10-04 14:22:29 -040042static void pci_pme_list_scan(struct work_struct *work);
43
44static LIST_HEAD(pci_pme_list);
45static DEFINE_MUTEX(pci_pme_list_mutex);
46static DECLARE_DELAYED_WORK(pci_pme_work, pci_pme_list_scan);
47
48struct pci_pme_device {
49 struct list_head list;
50 struct pci_dev *dev;
51};
52
53#define PME_TIMEOUT 1000 /* How long between PME checks */
54
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +010055static void pci_dev_d3_sleep(struct pci_dev *dev)
56{
57 unsigned int delay = dev->d3_delay;
58
59 if (delay < pci_pm_d3_delay)
60 delay = pci_pm_d3_delay;
61
62 msleep(delay);
63}
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Jeff Garzik32a2eea2007-10-11 16:57:27 -040065#ifdef CONFIG_PCI_DOMAINS
66int pci_domains_supported = 1;
67#endif
68
Atsushi Nemoto4516a612007-02-05 16:36:06 -080069#define DEFAULT_CARDBUS_IO_SIZE (256)
70#define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
71/* pci=cbmemsize=nnM,cbiosize=nn can override this */
72unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
73unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
74
Eric W. Biederman28760482009-09-09 14:09:24 -070075#define DEFAULT_HOTPLUG_IO_SIZE (256)
76#define DEFAULT_HOTPLUG_MEM_SIZE (2*1024*1024)
77/* pci=hpmemsize=nnM,hpiosize=nn can override this */
78unsigned long pci_hotplug_io_size = DEFAULT_HOTPLUG_IO_SIZE;
79unsigned long pci_hotplug_mem_size = DEFAULT_HOTPLUG_MEM_SIZE;
80
Jon Mason5f39e672011-10-03 09:50:20 -050081enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_TUNE_OFF;
Jon Masonb03e7492011-07-20 15:20:54 -050082
Jesse Barnesac1aa472009-10-26 13:20:44 -070083/*
84 * The default CLS is used if arch didn't set CLS explicitly and not
85 * all pci devices agree on the same value. Arch can override either
86 * the dfl or actual value as it sees fit. Don't forget this is
87 * measured in 32-bit words, not bytes.
88 */
Tejun Heo98e724c2009-10-08 18:59:53 +090089u8 pci_dfl_cache_line_size __devinitdata = L1_CACHE_BYTES >> 2;
Jesse Barnesac1aa472009-10-26 13:20:44 -070090u8 pci_cache_line_size;
91
Myron Stowe96c55902011-10-28 15:48:38 -060092/*
93 * If we set up a device for bus mastering, we need to check the latency
94 * timer as certain BIOSes forget to set it properly.
95 */
96unsigned int pcibios_max_latency = 255;
97
Rafael J. Wysocki6748dcc2012-03-01 00:06:33 +010098/* If set, the PCIe ARI capability will not be used. */
99static bool pcie_ari_disabled;
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/**
102 * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
103 * @bus: pointer to PCI bus structure to search
104 *
105 * Given a PCI bus, returns the highest PCI bus number present in the set
106 * including the given PCI bus and its list of child PCI buses.
107 */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800108unsigned char pci_bus_max_busnr(struct pci_bus* bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 struct list_head *tmp;
111 unsigned char max, n;
112
Kristen Accardib82db5c2006-01-17 16:56:56 -0800113 max = bus->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 list_for_each(tmp, &bus->children) {
115 n = pci_bus_max_busnr(pci_bus_b(tmp));
116 if(n > max)
117 max = n;
118 }
119 return max;
120}
Kristen Accardib82db5c2006-01-17 16:56:56 -0800121EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Andrew Morton1684f5d2008-12-01 14:30:30 -0800123#ifdef CONFIG_HAS_IOMEM
124void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
125{
126 /*
127 * Make sure the BAR is actually a memory resource, not an IO resource
128 */
129 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
130 WARN_ON(1);
131 return NULL;
132 }
133 return ioremap_nocache(pci_resource_start(pdev, bar),
134 pci_resource_len(pdev, bar));
135}
136EXPORT_SYMBOL_GPL(pci_ioremap_bar);
137#endif
138
Kristen Accardib82db5c2006-01-17 16:56:56 -0800139#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/**
141 * pci_max_busnr - returns maximum PCI bus number
142 *
143 * Returns the highest PCI bus number present in the system global list of
144 * PCI buses.
145 */
146unsigned char __devinit
147pci_max_busnr(void)
148{
149 struct pci_bus *bus = NULL;
150 unsigned char max, n;
151
152 max = 0;
153 while ((bus = pci_find_next_bus(bus)) != NULL) {
154 n = pci_bus_max_busnr(bus);
155 if(n > max)
156 max = n;
157 }
158 return max;
159}
160
Adrian Bunk54c762f2005-12-22 01:08:52 +0100161#endif /* 0 */
162
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100163#define PCI_FIND_CAP_TTL 48
164
165static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
166 u8 pos, int cap, int *ttl)
Roland Dreier24a4e372005-10-28 17:35:34 -0700167{
168 u8 id;
Roland Dreier24a4e372005-10-28 17:35:34 -0700169
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100170 while ((*ttl)--) {
Roland Dreier24a4e372005-10-28 17:35:34 -0700171 pci_bus_read_config_byte(bus, devfn, pos, &pos);
172 if (pos < 0x40)
173 break;
174 pos &= ~3;
175 pci_bus_read_config_byte(bus, devfn, pos + PCI_CAP_LIST_ID,
176 &id);
177 if (id == 0xff)
178 break;
179 if (id == cap)
180 return pos;
181 pos += PCI_CAP_LIST_NEXT;
182 }
183 return 0;
184}
185
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100186static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
187 u8 pos, int cap)
188{
189 int ttl = PCI_FIND_CAP_TTL;
190
191 return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
192}
193
Roland Dreier24a4e372005-10-28 17:35:34 -0700194int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
195{
196 return __pci_find_next_cap(dev->bus, dev->devfn,
197 pos + PCI_CAP_LIST_NEXT, cap);
198}
199EXPORT_SYMBOL_GPL(pci_find_next_capability);
200
Michael Ellermand3bac112006-11-22 18:26:16 +1100201static int __pci_bus_find_cap_start(struct pci_bus *bus,
202 unsigned int devfn, u8 hdr_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
207 if (!(status & PCI_STATUS_CAP_LIST))
208 return 0;
209
210 switch (hdr_type) {
211 case PCI_HEADER_TYPE_NORMAL:
212 case PCI_HEADER_TYPE_BRIDGE:
Michael Ellermand3bac112006-11-22 18:26:16 +1100213 return PCI_CAPABILITY_LIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 case PCI_HEADER_TYPE_CARDBUS:
Michael Ellermand3bac112006-11-22 18:26:16 +1100215 return PCI_CB_CAPABILITY_LIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 default:
217 return 0;
218 }
Michael Ellermand3bac112006-11-22 18:26:16 +1100219
220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223/**
224 * pci_find_capability - query for devices' capabilities
225 * @dev: PCI device to query
226 * @cap: capability code
227 *
228 * Tell if a device supports a given PCI capability.
229 * Returns the address of the requested capability structure within the
230 * device's PCI configuration space or 0 in case the device does not
231 * support it. Possible values for @cap:
232 *
233 * %PCI_CAP_ID_PM Power Management
234 * %PCI_CAP_ID_AGP Accelerated Graphics Port
235 * %PCI_CAP_ID_VPD Vital Product Data
236 * %PCI_CAP_ID_SLOTID Slot Identification
237 * %PCI_CAP_ID_MSI Message Signalled Interrupts
238 * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
239 * %PCI_CAP_ID_PCIX PCI-X
240 * %PCI_CAP_ID_EXP PCI Express
241 */
242int pci_find_capability(struct pci_dev *dev, int cap)
243{
Michael Ellermand3bac112006-11-22 18:26:16 +1100244 int pos;
245
246 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
247 if (pos)
248 pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
249
250 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/**
254 * pci_bus_find_capability - query for devices' capabilities
255 * @bus: the PCI bus to query
256 * @devfn: PCI device to query
257 * @cap: capability code
258 *
259 * Like pci_find_capability() but works for pci devices that do not have a
260 * pci_dev structure set up yet.
261 *
262 * Returns the address of the requested capability structure within the
263 * device's PCI configuration space or 0 in case the device does not
264 * support it.
265 */
266int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
267{
Michael Ellermand3bac112006-11-22 18:26:16 +1100268 int pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 u8 hdr_type;
270
271 pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
272
Michael Ellermand3bac112006-11-22 18:26:16 +1100273 pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
274 if (pos)
275 pos = __pci_find_next_cap(bus, devfn, pos, cap);
276
277 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/**
281 * pci_find_ext_capability - Find an extended capability
282 * @dev: PCI device to query
283 * @cap: capability code
284 *
285 * Returns the address of the requested extended capability structure
286 * within the device's PCI configuration space or 0 if the device does
287 * not support it. Possible values for @cap:
288 *
289 * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
290 * %PCI_EXT_CAP_ID_VC Virtual Channel
291 * %PCI_EXT_CAP_ID_DSN Device Serial Number
292 * %PCI_EXT_CAP_ID_PWR Power Budgeting
293 */
294int pci_find_ext_capability(struct pci_dev *dev, int cap)
295{
296 u32 header;
Zhao, Yu557848c2008-10-13 19:18:07 +0800297 int ttl;
298 int pos = PCI_CFG_SPACE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Zhao, Yu557848c2008-10-13 19:18:07 +0800300 /* minimum 8 bytes per capability */
301 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
302
303 if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
305
306 if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
307 return 0;
308
309 /*
310 * If we have no capabilities, this is indicated by cap ID,
311 * cap version and next pointer all being 0.
312 */
313 if (header == 0)
314 return 0;
315
316 while (ttl-- > 0) {
317 if (PCI_EXT_CAP_ID(header) == cap)
318 return pos;
319
320 pos = PCI_EXT_CAP_NEXT(header);
Zhao, Yu557848c2008-10-13 19:18:07 +0800321 if (pos < PCI_CFG_SPACE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 break;
323
324 if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
325 break;
326 }
327
328 return 0;
329}
Brice Goglin3a720d72006-05-23 06:10:01 -0400330EXPORT_SYMBOL_GPL(pci_find_ext_capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100332static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
333{
334 int rc, ttl = PCI_FIND_CAP_TTL;
335 u8 cap, mask;
336
337 if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
338 mask = HT_3BIT_CAP_MASK;
339 else
340 mask = HT_5BIT_CAP_MASK;
341
342 pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
343 PCI_CAP_ID_HT, &ttl);
344 while (pos) {
345 rc = pci_read_config_byte(dev, pos + 3, &cap);
346 if (rc != PCIBIOS_SUCCESSFUL)
347 return 0;
348
349 if ((cap & mask) == ht_cap)
350 return pos;
351
Brice Goglin47a4d5b2007-01-10 23:15:29 -0800352 pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
353 pos + PCI_CAP_LIST_NEXT,
Michael Ellerman687d5fe2006-11-22 18:26:18 +1100354 PCI_CAP_ID_HT, &ttl);
355 }
356
357 return 0;
358}
359/**
360 * pci_find_next_ht_capability - query a device's Hypertransport capabilities
361 * @dev: PCI device to query
362 * @pos: Position from which to continue searching
363 * @ht_cap: Hypertransport capability code
364 *
365 * To be used in conjunction with pci_find_ht_capability() to search for
366 * all capabilities matching @ht_cap. @pos should always be a value returned
367 * from pci_find_ht_capability().
368 *
369 * NB. To be 100% safe against broken PCI devices, the caller should take
370 * steps to avoid an infinite loop.
371 */
372int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
373{
374 return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
375}
376EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
377
378/**
379 * pci_find_ht_capability - query a device's Hypertransport capabilities
380 * @dev: PCI device to query
381 * @ht_cap: Hypertransport capability code
382 *
383 * Tell if a device supports a given Hypertransport capability.
384 * Returns an address within the device's PCI configuration space
385 * or 0 in case the device does not support the request capability.
386 * The address points to the PCI capability, of type PCI_CAP_ID_HT,
387 * which has a Hypertransport capability matching @ht_cap.
388 */
389int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
390{
391 int pos;
392
393 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
394 if (pos)
395 pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
396
397 return pos;
398}
399EXPORT_SYMBOL_GPL(pci_find_ht_capability);
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/**
402 * pci_find_parent_resource - return resource region of parent bus of given region
403 * @dev: PCI device structure contains resources to be searched
404 * @res: child resource record for which parent is sought
405 *
406 * For given resource region of given device, return the resource
407 * region of parent bus the given region is contained in or where
408 * it should be allocated from.
409 */
410struct resource *
411pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
412{
413 const struct pci_bus *bus = dev->bus;
414 int i;
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700415 struct resource *best = NULL, *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700417 pci_bus_for_each_resource(bus, r, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (!r)
419 continue;
420 if (res->start && !(res->start >= r->start && res->end <= r->end))
421 continue; /* Not contained */
422 if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
423 continue; /* Wrong type */
424 if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH))
425 return r; /* Exact match */
Linus Torvalds8c8def22009-11-09 12:04:32 -0800426 /* We can't insert a non-prefetch resource inside a prefetchable parent .. */
427 if (r->flags & IORESOURCE_PREFETCH)
428 continue;
429 /* .. but we can put a prefetchable resource inside a non-prefetchable one */
430 if (!best)
431 best = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433 return best;
434}
435
436/**
John W. Linville064b53db2005-07-27 10:19:44 -0400437 * pci_restore_bars - restore a devices BAR values (e.g. after wake-up)
438 * @dev: PCI device to have its BARs restored
439 *
440 * Restore the BAR values for a given device, so as to make it
441 * accessible by its driver.
442 */
Adrian Bunkad6685992007-10-27 03:06:22 +0200443static void
John W. Linville064b53db2005-07-27 10:19:44 -0400444pci_restore_bars(struct pci_dev *dev)
445{
Yu Zhaobc5f5a82008-11-22 02:40:00 +0800446 int i;
John W. Linville064b53db2005-07-27 10:19:44 -0400447
Yu Zhaobc5f5a82008-11-22 02:40:00 +0800448 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
Yu Zhao14add802008-11-22 02:38:52 +0800449 pci_update_resource(dev, i);
John W. Linville064b53db2005-07-27 10:19:44 -0400450}
451
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200452static struct pci_platform_pm_ops *pci_platform_pm;
453
454int pci_set_platform_pm(struct pci_platform_pm_ops *ops)
455{
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200456 if (!ops->is_manageable || !ops->set_state || !ops->choose_state
457 || !ops->sleep_wake || !ops->can_wakeup)
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200458 return -EINVAL;
459 pci_platform_pm = ops;
460 return 0;
461}
462
463static inline bool platform_pci_power_manageable(struct pci_dev *dev)
464{
465 return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
466}
467
468static inline int platform_pci_set_power_state(struct pci_dev *dev,
469 pci_power_t t)
470{
471 return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
472}
473
474static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
475{
476 return pci_platform_pm ?
477 pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
478}
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700479
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200480static inline bool platform_pci_can_wakeup(struct pci_dev *dev)
481{
482 return pci_platform_pm ? pci_platform_pm->can_wakeup(dev) : false;
483}
484
485static inline int platform_pci_sleep_wake(struct pci_dev *dev, bool enable)
486{
487 return pci_platform_pm ?
488 pci_platform_pm->sleep_wake(dev, enable) : -ENODEV;
489}
490
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100491static inline int platform_pci_run_wake(struct pci_dev *dev, bool enable)
492{
493 return pci_platform_pm ?
494 pci_platform_pm->run_wake(dev, enable) : -ENODEV;
495}
496
John W. Linville064b53db2005-07-27 10:19:44 -0400497/**
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200498 * pci_raw_set_power_state - Use PCI PM registers to set the power state of
499 * given PCI device
500 * @dev: PCI device to handle.
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200501 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 *
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200503 * RETURN VALUE:
504 * -EINVAL if the requested state is invalid.
505 * -EIO if device does not support PCI PM or its PM capabilities register has a
506 * wrong version, or device doesn't support the requested state.
507 * 0 if device already is in the requested state.
508 * 0 if device's power state has been successfully changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
Rafael J. Wysockif00a20e2009-03-16 22:40:08 +0100510static int pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200512 u16 pmcsr;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200513 bool need_restore = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Rafael J. Wysocki4a865902009-03-16 22:40:36 +0100515 /* Check if we're already there */
516 if (dev->current_state == state)
517 return 0;
518
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200519 if (!dev->pm_cap)
Andrew Lunncca03de2007-07-09 11:55:58 -0700520 return -EIO;
521
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200522 if (state < PCI_D0 || state > PCI_D3hot)
523 return -EINVAL;
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* Validate current state:
526 * Can enter D0 from any state, but if we can only go deeper
527 * to sleep if we're already in a low power state
528 */
Rafael J. Wysocki4a865902009-03-16 22:40:36 +0100529 if (state != PCI_D0 && dev->current_state <= PCI_D3cold
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200530 && dev->current_state > state) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600531 dev_err(&dev->dev, "invalid power transition "
532 "(from state %d to %d)\n", dev->current_state, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return -EINVAL;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* check if this device supports the desired state */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200537 if ((state == PCI_D1 && !dev->d1_support)
538 || (state == PCI_D2 && !dev->d2_support))
Daniel Ritz3fe9d192005-08-17 15:32:19 -0700539 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200541 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
John W. Linville064b53db2005-07-27 10:19:44 -0400542
John W. Linville32a36582005-09-14 09:52:42 -0400543 /* If we're (effectively) in D3, force entire word to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 * This doesn't affect PME_Status, disables PME_En, and
545 * sets PowerState to 0.
546 */
John W. Linville32a36582005-09-14 09:52:42 -0400547 switch (dev->current_state) {
John W. Linvilled3535fb2005-09-28 17:50:51 -0400548 case PCI_D0:
549 case PCI_D1:
550 case PCI_D2:
551 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
552 pmcsr |= state;
553 break;
Rafael J. Wysockif62795f2009-05-18 22:51:12 +0200554 case PCI_D3hot:
555 case PCI_D3cold:
John W. Linville32a36582005-09-14 09:52:42 -0400556 case PCI_UNKNOWN: /* Boot-up */
557 if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
Rafael J. Wysockif00a20e2009-03-16 22:40:08 +0100558 && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200559 need_restore = true;
John W. Linville32a36582005-09-14 09:52:42 -0400560 /* Fall-through: force to D0 */
John W. Linville32a36582005-09-14 09:52:42 -0400561 default:
John W. Linvilled3535fb2005-09-28 17:50:51 -0400562 pmcsr = 0;
John W. Linville32a36582005-09-14 09:52:42 -0400563 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
566 /* enter specified state */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200567 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* Mandatory power management transition delays */
570 /* see PCI PM 1.1 5.6.1 table 18 */
571 if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +0100572 pci_dev_d3_sleep(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 else if (state == PCI_D2 || dev->current_state == PCI_D2)
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100574 udelay(PCI_PM_D2_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Rafael J. Wysockie13cdbd2009-10-05 00:48:40 +0200576 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
577 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
578 if (dev->current_state != state && printk_ratelimit())
579 dev_info(&dev->dev, "Refused to change power state, "
580 "currently in D%d\n", dev->current_state);
John W. Linville064b53db2005-07-27 10:19:44 -0400581
582 /* According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
583 * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
584 * from D3hot to D0 _may_ perform an internal reset, thereby
585 * going to "D0 Uninitialized" rather than "D0 Initialized".
586 * For example, at least some versions of the 3c905B and the
587 * 3c556B exhibit this behaviour.
588 *
589 * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
590 * devices in a D3hot state at boot. Consequently, we need to
591 * restore at least the BARs so that the device will be
592 * accessible to its driver.
593 */
594 if (need_restore)
595 pci_restore_bars(dev);
596
Rafael J. Wysockif00a20e2009-03-16 22:40:08 +0100597 if (dev->bus->self)
Shaohua Li7d715a62008-02-25 09:46:41 +0800598 pcie_aspm_pm_state_change(dev->bus->self);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return 0;
601}
602
603/**
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200604 * pci_update_current_state - Read PCI power state of given device from its
605 * PCI PM registers and cache it
606 * @dev: PCI device to handle.
Rafael J. Wysockif06fc0b2008-12-27 16:30:52 +0100607 * @state: State to cache in case the device doesn't have the PM capability
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200608 */
Rafael J. Wysocki734104292009-01-07 13:07:15 +0100609void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200610{
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200611 if (dev->pm_cap) {
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200612 u16 pmcsr;
613
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200614 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200615 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
Rafael J. Wysockif06fc0b2008-12-27 16:30:52 +0100616 } else {
617 dev->current_state = state;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200618 }
619}
620
621/**
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100622 * pci_platform_power_transition - Use platform to change device power state
623 * @dev: PCI device to handle.
624 * @state: State to put the device into.
625 */
626static int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
627{
628 int error;
629
630 if (platform_pci_power_manageable(dev)) {
631 error = platform_pci_set_power_state(dev, state);
632 if (!error)
633 pci_update_current_state(dev, state);
Ajaykumar Hotchandanib51306c2011-12-12 13:57:36 +0530634 /* Fall back to PCI_D0 if native PM is not supported */
635 if (!dev->pm_cap)
636 dev->current_state = PCI_D0;
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100637 } else {
638 error = -ENODEV;
639 /* Fall back to PCI_D0 if native PM is not supported */
Rafael J. Wysockib3bad722009-05-17 20:17:06 +0200640 if (!dev->pm_cap)
641 dev->current_state = PCI_D0;
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100642 }
643
644 return error;
645}
646
647/**
648 * __pci_start_power_transition - Start power transition of a PCI device
649 * @dev: PCI device to handle.
650 * @state: State to put the device into.
651 */
652static void __pci_start_power_transition(struct pci_dev *dev, pci_power_t state)
653{
654 if (state == PCI_D0)
655 pci_platform_power_transition(dev, PCI_D0);
656}
657
658/**
659 * __pci_complete_power_transition - Complete power transition of a PCI device
660 * @dev: PCI device to handle.
661 * @state: State to put the device into.
662 *
663 * This function should not be called directly by device drivers.
664 */
665int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
666{
Matthew Garrettcc2893b2010-04-22 09:30:51 -0400667 return state >= PCI_D0 ?
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100668 pci_platform_power_transition(dev, state) : -EINVAL;
669}
670EXPORT_SYMBOL_GPL(__pci_complete_power_transition);
671
672/**
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200673 * pci_set_power_state - Set the power state of a PCI device
674 * @dev: PCI device to handle.
675 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
676 *
Nick Andrew877d0312009-01-26 11:06:57 +0100677 * Transition a device to a new power state, using the platform firmware and/or
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200678 * the device's PCI PM registers.
679 *
680 * RETURN VALUE:
681 * -EINVAL if the requested state is invalid.
682 * -EIO if device does not support PCI PM or its PM capabilities register has a
683 * wrong version, or device doesn't support the requested state.
684 * 0 if device already is in the requested state.
685 * 0 if device's power state has been successfully changed.
686 */
687int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
688{
Rafael J. Wysocki337001b2008-07-07 03:36:24 +0200689 int error;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200690
691 /* bound the state we're entering */
692 if (state > PCI_D3hot)
693 state = PCI_D3hot;
694 else if (state < PCI_D0)
695 state = PCI_D0;
696 else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
697 /*
698 * If the device or the parent bridge do not support PCI PM,
699 * ignore the request if we're doing anything other than putting
700 * it into D0 (which would only happen on boot).
701 */
702 return 0;
703
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100704 __pci_start_power_transition(dev, state);
705
Alan Cox979b1792008-07-24 17:18:38 +0100706 /* This device is quirked not to be put into D3, so
707 don't put it in D3 */
708 if (state == PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
709 return 0;
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200710
Rafael J. Wysockif00a20e2009-03-16 22:40:08 +0100711 error = pci_raw_set_power_state(dev, state);
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200712
Rafael J. Wysocki0e5dd462009-03-26 22:51:40 +0100713 if (!__pci_complete_power_transition(dev, state))
714 error = 0;
Naga Chumbalkar1a680b72011-03-21 03:29:08 +0000715 /*
716 * When aspm_policy is "powersave" this call ensures
717 * that ASPM is configured.
718 */
719 if (!error && dev->bus->self)
720 pcie_aspm_powersave_config_link(dev->bus->self);
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200721
722 return error;
723}
724
725/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 * pci_choose_state - Choose the power state of a PCI device
727 * @dev: PCI device to be suspended
728 * @state: target sleep state for the whole system. This is the value
729 * that is passed to suspend() function.
730 *
731 * Returns PCI power state suitable for given device and given system
732 * message.
733 */
734
735pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
736{
Shaohua Liab826ca2007-07-20 10:03:22 +0800737 pci_power_t ret;
David Shaohua Li0f644742005-03-19 00:15:48 -0500738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (!pci_find_capability(dev, PCI_CAP_ID_PM))
740 return PCI_D0;
741
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200742 ret = platform_pci_choose_state(dev);
743 if (ret != PCI_POWER_ERROR)
744 return ret;
Pavel Machekca078ba2005-09-03 15:56:57 -0700745
746 switch (state.event) {
747 case PM_EVENT_ON:
748 return PCI_D0;
749 case PM_EVENT_FREEZE:
David Brownellb887d2e2006-08-14 23:11:05 -0700750 case PM_EVENT_PRETHAW:
751 /* REVISIT both freeze and pre-thaw "should" use D0 */
Pavel Machekca078ba2005-09-03 15:56:57 -0700752 case PM_EVENT_SUSPEND:
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100753 case PM_EVENT_HIBERNATE:
Pavel Machekca078ba2005-09-03 15:56:57 -0700754 return PCI_D3hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 default:
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600756 dev_info(&dev->dev, "unrecognized suspend event %d\n",
757 state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 BUG();
759 }
760 return PCI_D0;
761}
762
763EXPORT_SYMBOL(pci_choose_state);
764
Yu Zhao89858512009-02-16 02:55:47 +0800765#define PCI_EXP_SAVE_REGS 7
766
Yu Zhao1b6b8ce2009-04-09 14:57:39 +0800767#define pcie_cap_has_devctl(type, flags) 1
768#define pcie_cap_has_lnkctl(type, flags) \
769 ((flags & PCI_EXP_FLAGS_VERS) > 1 || \
770 (type == PCI_EXP_TYPE_ROOT_PORT || \
771 type == PCI_EXP_TYPE_ENDPOINT || \
772 type == PCI_EXP_TYPE_LEG_END))
773#define pcie_cap_has_sltctl(type, flags) \
774 ((flags & PCI_EXP_FLAGS_VERS) > 1 || \
775 ((type == PCI_EXP_TYPE_ROOT_PORT) || \
776 (type == PCI_EXP_TYPE_DOWNSTREAM && \
777 (flags & PCI_EXP_FLAGS_SLOT))))
778#define pcie_cap_has_rtctl(type, flags) \
779 ((flags & PCI_EXP_FLAGS_VERS) > 1 || \
780 (type == PCI_EXP_TYPE_ROOT_PORT || \
781 type == PCI_EXP_TYPE_RC_EC))
782#define pcie_cap_has_devctl2(type, flags) \
783 ((flags & PCI_EXP_FLAGS_VERS) > 1)
784#define pcie_cap_has_lnkctl2(type, flags) \
785 ((flags & PCI_EXP_FLAGS_VERS) > 1)
786#define pcie_cap_has_sltctl2(type, flags) \
787 ((flags & PCI_EXP_FLAGS_VERS) > 1)
788
Yinghai Lu34a48762012-02-11 00:18:41 -0800789static struct pci_cap_saved_state *pci_find_saved_cap(
790 struct pci_dev *pci_dev, char cap)
791{
792 struct pci_cap_saved_state *tmp;
793 struct hlist_node *pos;
794
795 hlist_for_each_entry(tmp, pos, &pci_dev->saved_cap_space, next) {
796 if (tmp->cap.cap_nr == cap)
797 return tmp;
798 }
799 return NULL;
800}
801
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300802static int pci_save_pcie_state(struct pci_dev *dev)
803{
804 int pos, i = 0;
805 struct pci_cap_saved_state *save_state;
806 u16 *cap;
Yu Zhao1b6b8ce2009-04-09 14:57:39 +0800807 u16 flags;
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300808
Kenji Kaneshige06a1cba2009-11-11 14:30:56 +0900809 pos = pci_pcie_cap(dev);
810 if (!pos)
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300811 return 0;
812
Eric W. Biederman9f355752007-03-08 13:06:13 -0700813 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300814 if (!save_state) {
Harvey Harrisone496b612009-01-07 16:22:37 -0800815 dev_err(&dev->dev, "buffer not found in %s\n", __func__);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300816 return -ENOMEM;
817 }
Alex Williamson24a4742f2011-05-10 10:02:11 -0600818 cap = (u16 *)&save_state->cap.data[0];
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300819
Yu Zhao1b6b8ce2009-04-09 14:57:39 +0800820 pci_read_config_word(dev, pos + PCI_EXP_FLAGS, &flags);
821
822 if (pcie_cap_has_devctl(dev->pcie_type, flags))
823 pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &cap[i++]);
824 if (pcie_cap_has_lnkctl(dev->pcie_type, flags))
825 pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &cap[i++]);
826 if (pcie_cap_has_sltctl(dev->pcie_type, flags))
827 pci_read_config_word(dev, pos + PCI_EXP_SLTCTL, &cap[i++]);
828 if (pcie_cap_has_rtctl(dev->pcie_type, flags))
829 pci_read_config_word(dev, pos + PCI_EXP_RTCTL, &cap[i++]);
830 if (pcie_cap_has_devctl2(dev->pcie_type, flags))
831 pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &cap[i++]);
832 if (pcie_cap_has_lnkctl2(dev->pcie_type, flags))
833 pci_read_config_word(dev, pos + PCI_EXP_LNKCTL2, &cap[i++]);
834 if (pcie_cap_has_sltctl2(dev->pcie_type, flags))
835 pci_read_config_word(dev, pos + PCI_EXP_SLTCTL2, &cap[i++]);
Rafael J. Wysocki63f48982008-12-07 22:02:58 +0100836
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300837 return 0;
838}
839
840static void pci_restore_pcie_state(struct pci_dev *dev)
841{
842 int i = 0, pos;
843 struct pci_cap_saved_state *save_state;
844 u16 *cap;
Yu Zhao1b6b8ce2009-04-09 14:57:39 +0800845 u16 flags;
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300846
847 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
848 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
849 if (!save_state || pos <= 0)
850 return;
Alex Williamson24a4742f2011-05-10 10:02:11 -0600851 cap = (u16 *)&save_state->cap.data[0];
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300852
Yu Zhao1b6b8ce2009-04-09 14:57:39 +0800853 pci_read_config_word(dev, pos + PCI_EXP_FLAGS, &flags);
854
855 if (pcie_cap_has_devctl(dev->pcie_type, flags))
856 pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, cap[i++]);
857 if (pcie_cap_has_lnkctl(dev->pcie_type, flags))
858 pci_write_config_word(dev, pos + PCI_EXP_LNKCTL, cap[i++]);
859 if (pcie_cap_has_sltctl(dev->pcie_type, flags))
860 pci_write_config_word(dev, pos + PCI_EXP_SLTCTL, cap[i++]);
861 if (pcie_cap_has_rtctl(dev->pcie_type, flags))
862 pci_write_config_word(dev, pos + PCI_EXP_RTCTL, cap[i++]);
863 if (pcie_cap_has_devctl2(dev->pcie_type, flags))
864 pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, cap[i++]);
865 if (pcie_cap_has_lnkctl2(dev->pcie_type, flags))
866 pci_write_config_word(dev, pos + PCI_EXP_LNKCTL2, cap[i++]);
867 if (pcie_cap_has_sltctl2(dev->pcie_type, flags))
868 pci_write_config_word(dev, pos + PCI_EXP_SLTCTL2, cap[i++]);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300869}
870
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800871
872static int pci_save_pcix_state(struct pci_dev *dev)
873{
Rafael J. Wysocki63f48982008-12-07 22:02:58 +0100874 int pos;
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800875 struct pci_cap_saved_state *save_state;
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800876
877 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
878 if (pos <= 0)
879 return 0;
880
Shaohua Lif34303d2007-12-18 09:56:47 +0800881 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800882 if (!save_state) {
Harvey Harrisone496b612009-01-07 16:22:37 -0800883 dev_err(&dev->dev, "buffer not found in %s\n", __func__);
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800884 return -ENOMEM;
885 }
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800886
Alex Williamson24a4742f2011-05-10 10:02:11 -0600887 pci_read_config_word(dev, pos + PCI_X_CMD,
888 (u16 *)save_state->cap.data);
Rafael J. Wysocki63f48982008-12-07 22:02:58 +0100889
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800890 return 0;
891}
892
893static void pci_restore_pcix_state(struct pci_dev *dev)
894{
895 int i = 0, pos;
896 struct pci_cap_saved_state *save_state;
897 u16 *cap;
898
899 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
900 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
901 if (!save_state || pos <= 0)
902 return;
Alex Williamson24a4742f2011-05-10 10:02:11 -0600903 cap = (u16 *)&save_state->cap.data[0];
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800904
905 pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800906}
907
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909/**
910 * pci_save_state - save the PCI configuration space of a device before suspending
911 * @dev: - PCI device that we're dealing with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 */
913int
914pci_save_state(struct pci_dev *dev)
915{
916 int i;
917 /* XXX: 100% dword access ok here? */
918 for (i = 0; i < 16; i++)
Kleber Sacilotto de Souza9e0b5b22009-11-25 00:55:51 -0200919 pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]);
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100920 dev->state_saved = true;
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300921 if ((i = pci_save_pcie_state(dev)) != 0)
922 return i;
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800923 if ((i = pci_save_pcix_state(dev)) != 0)
924 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 0;
926}
927
Rafael J. Wysockiebfc5b82012-04-15 21:40:40 +0200928static void pci_restore_config_dword(struct pci_dev *pdev, int offset,
929 u32 saved_val, int retry)
930{
931 u32 val;
932
933 pci_read_config_dword(pdev, offset, &val);
934 if (val == saved_val)
935 return;
936
937 for (;;) {
938 dev_dbg(&pdev->dev, "restoring config space at offset "
939 "%#x (was %#x, writing %#x)\n", offset, val, saved_val);
940 pci_write_config_dword(pdev, offset, saved_val);
941 if (retry-- <= 0)
942 return;
943
944 pci_read_config_dword(pdev, offset, &val);
945 if (val == saved_val)
946 return;
947
948 mdelay(1);
949 }
950}
951
Rafael J. Wysockia6cb9ee2012-04-16 23:07:50 +0200952static void pci_restore_config_space_range(struct pci_dev *pdev,
953 int start, int end, int retry)
Rafael J. Wysockiebfc5b82012-04-15 21:40:40 +0200954{
955 int index;
956
957 for (index = end; index >= start; index--)
958 pci_restore_config_dword(pdev, 4 * index,
959 pdev->saved_config_space[index],
960 retry);
961}
962
Rafael J. Wysockia6cb9ee2012-04-16 23:07:50 +0200963static void pci_restore_config_space(struct pci_dev *pdev)
964{
965 if (pdev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
966 pci_restore_config_space_range(pdev, 10, 15, 0);
967 /* Restore BARs before the command register. */
968 pci_restore_config_space_range(pdev, 4, 9, 10);
969 pci_restore_config_space_range(pdev, 0, 3, 0);
970 } else {
971 pci_restore_config_space_range(pdev, 0, 15, 0);
972 }
973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975/**
976 * pci_restore_state - Restore the saved state of a PCI device
977 * @dev: - PCI device that we're dealing with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 */
Jon Mason1d3c16a2010-11-30 17:43:26 -0600979void pci_restore_state(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Alek Duc82f63e2009-08-08 08:46:19 +0800981 if (!dev->state_saved)
Jon Mason1d3c16a2010-11-30 17:43:26 -0600982 return;
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +0200983
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300984 /* PCI Express register must be restored first */
985 pci_restore_pcie_state(dev);
Hao, Xudong1900ca12011-12-17 21:24:40 +0800986 pci_restore_ats_state(dev);
Michael S. Tsirkinb56a5a22006-08-21 16:22:22 +0300987
Rafael J. Wysockia6cb9ee2012-04-16 23:07:50 +0200988 pci_restore_config_space(dev);
Rafael J. Wysockiebfc5b82012-04-15 21:40:40 +0200989
Stephen Hemmingercc692a52006-11-08 16:17:15 -0800990 pci_restore_pcix_state(dev);
Shaohua Li41017f02006-02-08 17:11:38 +0800991 pci_restore_msi_state(dev);
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800992 pci_restore_iov_state(dev);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100993
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +0200994 dev->state_saved = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
Alex Williamsonffbdd3f2011-05-10 10:02:27 -0600997struct pci_saved_state {
998 u32 config_space[16];
999 struct pci_cap_saved_data cap[0];
1000};
1001
1002/**
1003 * pci_store_saved_state - Allocate and return an opaque struct containing
1004 * the device saved state.
1005 * @dev: PCI device that we're dealing with
1006 *
1007 * Rerturn NULL if no state or error.
1008 */
1009struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev)
1010{
1011 struct pci_saved_state *state;
1012 struct pci_cap_saved_state *tmp;
1013 struct pci_cap_saved_data *cap;
1014 struct hlist_node *pos;
1015 size_t size;
1016
1017 if (!dev->state_saved)
1018 return NULL;
1019
1020 size = sizeof(*state) + sizeof(struct pci_cap_saved_data);
1021
1022 hlist_for_each_entry(tmp, pos, &dev->saved_cap_space, next)
1023 size += sizeof(struct pci_cap_saved_data) + tmp->cap.size;
1024
1025 state = kzalloc(size, GFP_KERNEL);
1026 if (!state)
1027 return NULL;
1028
1029 memcpy(state->config_space, dev->saved_config_space,
1030 sizeof(state->config_space));
1031
1032 cap = state->cap;
1033 hlist_for_each_entry(tmp, pos, &dev->saved_cap_space, next) {
1034 size_t len = sizeof(struct pci_cap_saved_data) + tmp->cap.size;
1035 memcpy(cap, &tmp->cap, len);
1036 cap = (struct pci_cap_saved_data *)((u8 *)cap + len);
1037 }
1038 /* Empty cap_save terminates list */
1039
1040 return state;
1041}
1042EXPORT_SYMBOL_GPL(pci_store_saved_state);
1043
1044/**
1045 * pci_load_saved_state - Reload the provided save state into struct pci_dev.
1046 * @dev: PCI device that we're dealing with
1047 * @state: Saved state returned from pci_store_saved_state()
1048 */
1049int pci_load_saved_state(struct pci_dev *dev, struct pci_saved_state *state)
1050{
1051 struct pci_cap_saved_data *cap;
1052
1053 dev->state_saved = false;
1054
1055 if (!state)
1056 return 0;
1057
1058 memcpy(dev->saved_config_space, state->config_space,
1059 sizeof(state->config_space));
1060
1061 cap = state->cap;
1062 while (cap->size) {
1063 struct pci_cap_saved_state *tmp;
1064
1065 tmp = pci_find_saved_cap(dev, cap->cap_nr);
1066 if (!tmp || tmp->cap.size != cap->size)
1067 return -EINVAL;
1068
1069 memcpy(tmp->cap.data, cap->data, tmp->cap.size);
1070 cap = (struct pci_cap_saved_data *)((u8 *)cap +
1071 sizeof(struct pci_cap_saved_data) + cap->size);
1072 }
1073
1074 dev->state_saved = true;
1075 return 0;
1076}
1077EXPORT_SYMBOL_GPL(pci_load_saved_state);
1078
1079/**
1080 * pci_load_and_free_saved_state - Reload the save state pointed to by state,
1081 * and free the memory allocated for it.
1082 * @dev: PCI device that we're dealing with
1083 * @state: Pointer to saved state returned from pci_store_saved_state()
1084 */
1085int pci_load_and_free_saved_state(struct pci_dev *dev,
1086 struct pci_saved_state **state)
1087{
1088 int ret = pci_load_saved_state(dev, *state);
1089 kfree(*state);
1090 *state = NULL;
1091 return ret;
1092}
1093EXPORT_SYMBOL_GPL(pci_load_and_free_saved_state);
1094
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001095static int do_pci_enable_device(struct pci_dev *dev, int bars)
1096{
1097 int err;
1098
1099 err = pci_set_power_state(dev, PCI_D0);
1100 if (err < 0 && err != -EIO)
1101 return err;
1102 err = pcibios_enable_device(dev, bars);
1103 if (err < 0)
1104 return err;
1105 pci_fixup_device(pci_fixup_enable, dev);
1106
1107 return 0;
1108}
1109
1110/**
Tejun Heo0b62e132007-07-27 14:43:35 +09001111 * pci_reenable_device - Resume abandoned device
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001112 * @dev: PCI device to be resumed
1113 *
1114 * Note this function is a backend of pci_default_resume and is not supposed
1115 * to be called by normal code, write proper resume handler and use it instead.
1116 */
Tejun Heo0b62e132007-07-27 14:43:35 +09001117int pci_reenable_device(struct pci_dev *dev)
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001118{
Yuji Shimada296ccb02009-04-03 16:41:46 +09001119 if (pci_is_enabled(dev))
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001120 return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
1121 return 0;
1122}
1123
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001124static int __pci_enable_device_flags(struct pci_dev *dev,
1125 resource_size_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 int err;
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001128 int i, bars = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Jesse Barnes97c145f2010-11-05 15:16:36 -04001130 /*
1131 * Power state could be unknown at this point, either due to a fresh
1132 * boot or a device removal call. So get the current power state
1133 * so that things like MSI message writing will behave as expected
1134 * (e.g. if the device really is in D0 at enable time).
1135 */
1136 if (dev->pm_cap) {
1137 u16 pmcsr;
1138 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
1139 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
1140 }
1141
Hidetoshi Seto9fb625c2006-12-18 10:28:43 +09001142 if (atomic_add_return(1, &dev->enable_cnt) > 1)
1143 return 0; /* already enabled */
1144
Yinghai Lu497f16f2011-12-17 18:33:37 -08001145 /* only skip sriov related */
1146 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
1147 if (dev->resource[i].flags & flags)
1148 bars |= (1 << i);
1149 for (i = PCI_BRIDGE_RESOURCES; i < DEVICE_COUNT_RESOURCE; i++)
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001150 if (dev->resource[i].flags & flags)
1151 bars |= (1 << i);
1152
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001153 err = do_pci_enable_device(dev, bars);
Greg Kroah-Hartman95a62962005-07-28 11:37:33 -07001154 if (err < 0)
Hidetoshi Seto38cc1302006-12-18 10:30:00 +09001155 atomic_dec(&dev->enable_cnt);
Hidetoshi Seto9fb625c2006-12-18 10:28:43 +09001156 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159/**
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001160 * pci_enable_device_io - Initialize a device for use with IO space
1161 * @dev: PCI device to be initialized
1162 *
1163 * Initialize device before it's used by a driver. Ask low-level code
1164 * to enable I/O resources. Wake up the device if it was suspended.
1165 * Beware, this function can fail.
1166 */
1167int pci_enable_device_io(struct pci_dev *dev)
1168{
1169 return __pci_enable_device_flags(dev, IORESOURCE_IO);
1170}
1171
1172/**
1173 * pci_enable_device_mem - Initialize a device for use with Memory space
1174 * @dev: PCI device to be initialized
1175 *
1176 * Initialize device before it's used by a driver. Ask low-level code
1177 * to enable Memory resources. Wake up the device if it was suspended.
1178 * Beware, this function can fail.
1179 */
1180int pci_enable_device_mem(struct pci_dev *dev)
1181{
1182 return __pci_enable_device_flags(dev, IORESOURCE_MEM);
1183}
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185/**
1186 * pci_enable_device - Initialize device before it's used by a driver.
1187 * @dev: PCI device to be initialized
1188 *
1189 * Initialize device before it's used by a driver. Ask low-level code
1190 * to enable I/O and memory. Wake up the device if it was suspended.
1191 * Beware, this function can fail.
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001192 *
1193 * Note we don't actually enable the device many times if we call
1194 * this function repeatedly (we just increment the count).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001196int pci_enable_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11001198 return __pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Tejun Heo9ac78492007-01-20 16:00:26 +09001201/*
1202 * Managed PCI resources. This manages device on/off, intx/msi/msix
1203 * on/off and BAR regions. pci_dev itself records msi/msix status, so
1204 * there's no need to track it separately. pci_devres is initialized
1205 * when a device is enabled using managed PCI device enable interface.
1206 */
1207struct pci_devres {
Tejun Heo7f375f32007-02-25 04:36:01 -08001208 unsigned int enabled:1;
1209 unsigned int pinned:1;
Tejun Heo9ac78492007-01-20 16:00:26 +09001210 unsigned int orig_intx:1;
1211 unsigned int restore_intx:1;
1212 u32 region_mask;
1213};
1214
1215static void pcim_release(struct device *gendev, void *res)
1216{
1217 struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
1218 struct pci_devres *this = res;
1219 int i;
1220
1221 if (dev->msi_enabled)
1222 pci_disable_msi(dev);
1223 if (dev->msix_enabled)
1224 pci_disable_msix(dev);
1225
1226 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
1227 if (this->region_mask & (1 << i))
1228 pci_release_region(dev, i);
1229
1230 if (this->restore_intx)
1231 pci_intx(dev, this->orig_intx);
1232
Tejun Heo7f375f32007-02-25 04:36:01 -08001233 if (this->enabled && !this->pinned)
Tejun Heo9ac78492007-01-20 16:00:26 +09001234 pci_disable_device(dev);
1235}
1236
1237static struct pci_devres * get_pci_dr(struct pci_dev *pdev)
1238{
1239 struct pci_devres *dr, *new_dr;
1240
1241 dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
1242 if (dr)
1243 return dr;
1244
1245 new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
1246 if (!new_dr)
1247 return NULL;
1248 return devres_get(&pdev->dev, new_dr, NULL, NULL);
1249}
1250
1251static struct pci_devres * find_pci_dr(struct pci_dev *pdev)
1252{
1253 if (pci_is_managed(pdev))
1254 return devres_find(&pdev->dev, pcim_release, NULL, NULL);
1255 return NULL;
1256}
1257
1258/**
1259 * pcim_enable_device - Managed pci_enable_device()
1260 * @pdev: PCI device to be initialized
1261 *
1262 * Managed pci_enable_device().
1263 */
1264int pcim_enable_device(struct pci_dev *pdev)
1265{
1266 struct pci_devres *dr;
1267 int rc;
1268
1269 dr = get_pci_dr(pdev);
1270 if (unlikely(!dr))
1271 return -ENOMEM;
Tejun Heob95d58e2008-01-30 18:20:04 +09001272 if (dr->enabled)
1273 return 0;
Tejun Heo9ac78492007-01-20 16:00:26 +09001274
1275 rc = pci_enable_device(pdev);
1276 if (!rc) {
1277 pdev->is_managed = 1;
Tejun Heo7f375f32007-02-25 04:36:01 -08001278 dr->enabled = 1;
Tejun Heo9ac78492007-01-20 16:00:26 +09001279 }
1280 return rc;
1281}
1282
1283/**
1284 * pcim_pin_device - Pin managed PCI device
1285 * @pdev: PCI device to pin
1286 *
1287 * Pin managed PCI device @pdev. Pinned device won't be disabled on
1288 * driver detach. @pdev must have been enabled with
1289 * pcim_enable_device().
1290 */
1291void pcim_pin_device(struct pci_dev *pdev)
1292{
1293 struct pci_devres *dr;
1294
1295 dr = find_pci_dr(pdev);
Tejun Heo7f375f32007-02-25 04:36:01 -08001296 WARN_ON(!dr || !dr->enabled);
Tejun Heo9ac78492007-01-20 16:00:26 +09001297 if (dr)
Tejun Heo7f375f32007-02-25 04:36:01 -08001298 dr->pinned = 1;
Tejun Heo9ac78492007-01-20 16:00:26 +09001299}
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301/**
1302 * pcibios_disable_device - disable arch specific PCI resources for device dev
1303 * @dev: the PCI device to disable
1304 *
1305 * Disables architecture specific PCI resources for the device. This
1306 * is the default implementation. Architecture implementations can
1307 * override this.
1308 */
1309void __attribute__ ((weak)) pcibios_disable_device (struct pci_dev *dev) {}
1310
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001311static void do_pci_disable_device(struct pci_dev *dev)
1312{
1313 u16 pci_command;
1314
1315 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
1316 if (pci_command & PCI_COMMAND_MASTER) {
1317 pci_command &= ~PCI_COMMAND_MASTER;
1318 pci_write_config_word(dev, PCI_COMMAND, pci_command);
1319 }
1320
1321 pcibios_disable_device(dev);
1322}
1323
1324/**
1325 * pci_disable_enabled_device - Disable device without updating enable_cnt
1326 * @dev: PCI device to disable
1327 *
1328 * NOTE: This function is a backend of PCI power management routines and is
1329 * not supposed to be called drivers.
1330 */
1331void pci_disable_enabled_device(struct pci_dev *dev)
1332{
Yuji Shimada296ccb02009-04-03 16:41:46 +09001333 if (pci_is_enabled(dev))
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001334 do_pci_disable_device(dev);
1335}
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337/**
1338 * pci_disable_device - Disable PCI device after use
1339 * @dev: PCI device to be disabled
1340 *
1341 * Signal to the system that the PCI device is not in use by the system
1342 * anymore. This only involves disabling PCI bus-mastering, if active.
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001343 *
1344 * Note we don't actually disable the device until all callers of
Roman Fietzeee6583f2010-05-18 14:45:47 +02001345 * pci_enable_device() have called pci_disable_device().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 */
1347void
1348pci_disable_device(struct pci_dev *dev)
1349{
Tejun Heo9ac78492007-01-20 16:00:26 +09001350 struct pci_devres *dr;
Shaohua Li99dc8042006-05-26 10:58:27 +08001351
Tejun Heo9ac78492007-01-20 16:00:26 +09001352 dr = find_pci_dr(dev);
1353 if (dr)
Tejun Heo7f375f32007-02-25 04:36:01 -08001354 dr->enabled = 0;
Tejun Heo9ac78492007-01-20 16:00:26 +09001355
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -08001356 if (atomic_sub_return(1, &dev->enable_cnt) != 0)
1357 return;
1358
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001359 do_pci_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Rafael J. Wysockifa58d302009-01-07 13:03:42 +01001361 dev->is_busmaster = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
1364/**
Brian Kingf7bdd122007-04-06 16:39:36 -05001365 * pcibios_set_pcie_reset_state - set reset state for device dev
Stefan Assmann45e829e2009-12-03 06:49:24 -05001366 * @dev: the PCIe device reset
Brian Kingf7bdd122007-04-06 16:39:36 -05001367 * @state: Reset state to enter into
1368 *
1369 *
Stefan Assmann45e829e2009-12-03 06:49:24 -05001370 * Sets the PCIe reset state for the device. This is the default
Brian Kingf7bdd122007-04-06 16:39:36 -05001371 * implementation. Architecture implementations can override this.
1372 */
1373int __attribute__ ((weak)) pcibios_set_pcie_reset_state(struct pci_dev *dev,
1374 enum pcie_reset_state state)
1375{
1376 return -EINVAL;
1377}
1378
1379/**
1380 * pci_set_pcie_reset_state - set reset state for device dev
Stefan Assmann45e829e2009-12-03 06:49:24 -05001381 * @dev: the PCIe device reset
Brian Kingf7bdd122007-04-06 16:39:36 -05001382 * @state: Reset state to enter into
1383 *
1384 *
1385 * Sets the PCI reset state for the device.
1386 */
1387int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
1388{
1389 return pcibios_set_pcie_reset_state(dev, state);
1390}
1391
1392/**
Rafael J. Wysocki58ff4632010-02-17 23:36:58 +01001393 * pci_check_pme_status - Check if given device has generated PME.
1394 * @dev: Device to check.
1395 *
1396 * Check the PME status of the device and if set, clear it and clear PME enable
1397 * (if set). Return 'true' if PME status and PME enable were both set or
1398 * 'false' otherwise.
1399 */
1400bool pci_check_pme_status(struct pci_dev *dev)
1401{
1402 int pmcsr_pos;
1403 u16 pmcsr;
1404 bool ret = false;
1405
1406 if (!dev->pm_cap)
1407 return false;
1408
1409 pmcsr_pos = dev->pm_cap + PCI_PM_CTRL;
1410 pci_read_config_word(dev, pmcsr_pos, &pmcsr);
1411 if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
1412 return false;
1413
1414 /* Clear PME status. */
1415 pmcsr |= PCI_PM_CTRL_PME_STATUS;
1416 if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
1417 /* Disable PME to avoid interrupt flood. */
1418 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
1419 ret = true;
1420 }
1421
1422 pci_write_config_word(dev, pmcsr_pos, pmcsr);
1423
1424 return ret;
1425}
1426
1427/**
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001428 * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set.
1429 * @dev: Device to handle.
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001430 * @pme_poll_reset: Whether or not to reset the device's pme_poll flag.
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001431 *
1432 * Check if @dev has generated PME and queue a resume request for it in that
1433 * case.
1434 */
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001435static int pci_pme_wakeup(struct pci_dev *dev, void *pme_poll_reset)
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001436{
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001437 if (pme_poll_reset && dev->pme_poll)
1438 dev->pme_poll = false;
1439
Rafael J. Wysockic125e962010-07-05 22:43:53 +02001440 if (pci_check_pme_status(dev)) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +02001441 pci_wakeup_event(dev);
Rafael J. Wysocki0f953bf2010-12-29 13:22:08 +01001442 pm_request_resume(&dev->dev);
Rafael J. Wysockic125e962010-07-05 22:43:53 +02001443 }
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001444 return 0;
1445}
1446
1447/**
1448 * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary.
1449 * @bus: Top bus of the subtree to walk.
1450 */
1451void pci_pme_wakeup_bus(struct pci_bus *bus)
1452{
1453 if (bus)
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001454 pci_walk_bus(bus, pci_pme_wakeup, (void *)true);
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001455}
1456
1457/**
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001458 * pci_pme_capable - check the capability of PCI device to generate PME#
1459 * @dev: PCI device to handle.
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001460 * @state: PCI state from which device will issue PME#.
1461 */
Rafael J. Wysockie5899e12008-07-19 14:39:24 +02001462bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001463{
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001464 if (!dev->pm_cap)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001465 return false;
1466
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001467 return !!(dev->pme_support & (1 << state));
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001468}
1469
Matthew Garrettdf17e622010-10-04 14:22:29 -04001470static void pci_pme_list_scan(struct work_struct *work)
1471{
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001472 struct pci_pme_device *pme_dev, *n;
Matthew Garrettdf17e622010-10-04 14:22:29 -04001473
1474 mutex_lock(&pci_pme_list_mutex);
1475 if (!list_empty(&pci_pme_list)) {
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001476 list_for_each_entry_safe(pme_dev, n, &pci_pme_list, list) {
1477 if (pme_dev->dev->pme_poll) {
1478 pci_pme_wakeup(pme_dev->dev, NULL);
1479 } else {
1480 list_del(&pme_dev->list);
1481 kfree(pme_dev);
1482 }
1483 }
1484 if (!list_empty(&pci_pme_list))
1485 schedule_delayed_work(&pci_pme_work,
1486 msecs_to_jiffies(PME_TIMEOUT));
Matthew Garrettdf17e622010-10-04 14:22:29 -04001487 }
1488 mutex_unlock(&pci_pme_list_mutex);
1489}
1490
1491/**
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001492 * pci_pme_active - enable or disable PCI device's PME# function
1493 * @dev: PCI device to handle.
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001494 * @enable: 'true' to enable PME# generation; 'false' to disable it.
1495 *
1496 * The caller must verify that the device is capable of generating PME# before
1497 * calling this function with @enable equal to 'true'.
1498 */
Rafael J. Wysocki5a6c9b62008-08-08 00:14:24 +02001499void pci_pme_active(struct pci_dev *dev, bool enable)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001500{
1501 u16 pmcsr;
1502
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001503 if (!dev->pm_cap)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001504 return;
1505
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001506 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001507 /* Clear PME_Status by writing 1 to it and enable PME# */
1508 pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
1509 if (!enable)
1510 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
1511
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001512 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001513
Matthew Garrettdf17e622010-10-04 14:22:29 -04001514 /* PCI (as opposed to PCIe) PME requires that the device have
1515 its PME# line hooked up correctly. Not all hardware vendors
1516 do this, so the PME never gets delivered and the device
1517 remains asleep. The easiest way around this is to
1518 periodically walk the list of suspended devices and check
1519 whether any have their PME flag set. The assumption is that
1520 we'll wake up often enough anyway that this won't be a huge
1521 hit, and the power savings from the devices will still be a
1522 win. */
1523
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001524 if (dev->pme_poll) {
Matthew Garrettdf17e622010-10-04 14:22:29 -04001525 struct pci_pme_device *pme_dev;
1526 if (enable) {
1527 pme_dev = kmalloc(sizeof(struct pci_pme_device),
1528 GFP_KERNEL);
1529 if (!pme_dev)
1530 goto out;
1531 pme_dev->dev = dev;
1532 mutex_lock(&pci_pme_list_mutex);
1533 list_add(&pme_dev->list, &pci_pme_list);
1534 if (list_is_singular(&pci_pme_list))
1535 schedule_delayed_work(&pci_pme_work,
1536 msecs_to_jiffies(PME_TIMEOUT));
1537 mutex_unlock(&pci_pme_list_mutex);
1538 } else {
1539 mutex_lock(&pci_pme_list_mutex);
1540 list_for_each_entry(pme_dev, &pci_pme_list, list) {
1541 if (pme_dev->dev == dev) {
1542 list_del(&pme_dev->list);
1543 kfree(pme_dev);
1544 break;
1545 }
1546 }
1547 mutex_unlock(&pci_pme_list_mutex);
1548 }
1549 }
1550
1551out:
Vincent Palatin85b85822011-12-05 11:51:18 -08001552 dev_dbg(&dev->dev, "PME# %s\n", enable ? "enabled" : "disabled");
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001553}
1554
1555/**
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001556 * __pci_enable_wake - enable PCI device as wakeup event source
David Brownell075c1772007-04-26 00:12:06 -07001557 * @dev: PCI device affected
1558 * @state: PCI state from which device will issue wakeup events
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001559 * @runtime: True if the events are to be generated at run time
David Brownell075c1772007-04-26 00:12:06 -07001560 * @enable: True to enable event generation; false to disable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 *
David Brownell075c1772007-04-26 00:12:06 -07001562 * This enables the device as a wakeup event source, or disables it.
1563 * When such events involves platform-specific hooks, those hooks are
1564 * called automatically by this routine.
1565 *
1566 * Devices with legacy power management (no standard PCI PM capabilities)
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001567 * always require such platform hooks.
David Brownell075c1772007-04-26 00:12:06 -07001568 *
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001569 * RETURN VALUE:
1570 * 0 is returned on success
1571 * -EINVAL is returned if device is not supposed to wake up the system
1572 * Error code depending on the platform is returned if both the platform and
1573 * the native mechanism fail to enable the generation of wake-up events
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 */
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001575int __pci_enable_wake(struct pci_dev *dev, pci_power_t state,
1576 bool runtime, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001578 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001580 if (enable && !runtime && !device_may_wakeup(&dev->dev))
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001581 return -EINVAL;
1582
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02001583 /* Don't do the same thing twice in a row for one device. */
1584 if (!!enable == !!dev->wakeup_prepared)
1585 return 0;
1586
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001587 /*
1588 * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
1589 * Anderson we should be doing PME# wake enable followed by ACPI wake
1590 * enable. To disable wake-up we call the platform first, for symmetry.
David Brownell075c1772007-04-26 00:12:06 -07001591 */
1592
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001593 if (enable) {
1594 int error;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001595
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001596 if (pci_pme_capable(dev, state))
1597 pci_pme_active(dev, true);
1598 else
1599 ret = 1;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001600 error = runtime ? platform_pci_run_wake(dev, true) :
1601 platform_pci_sleep_wake(dev, true);
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001602 if (ret)
1603 ret = error;
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02001604 if (!ret)
1605 dev->wakeup_prepared = true;
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001606 } else {
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001607 if (runtime)
1608 platform_pci_run_wake(dev, false);
1609 else
1610 platform_pci_sleep_wake(dev, false);
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001611 pci_pme_active(dev, false);
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02001612 dev->wakeup_prepared = false;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001613 }
1614
Rafael J. Wysocki5bcc2fb2009-09-08 23:12:59 +02001615 return ret;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001616}
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001617EXPORT_SYMBOL(__pci_enable_wake);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001618
1619/**
Rafael J. Wysocki0235c4f2008-08-18 21:38:00 +02001620 * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
1621 * @dev: PCI device to prepare
1622 * @enable: True to enable wake-up event generation; false to disable
1623 *
1624 * Many drivers want the device to wake up the system from D3_hot or D3_cold
1625 * and this function allows them to set that up cleanly - pci_enable_wake()
1626 * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
1627 * ordering constraints.
1628 *
1629 * This function only returns error code if the device is not capable of
1630 * generating PME# from both D3_hot and D3_cold, and the platform is unable to
1631 * enable wake-up power for it.
1632 */
1633int pci_wake_from_d3(struct pci_dev *dev, bool enable)
1634{
1635 return pci_pme_capable(dev, PCI_D3cold) ?
1636 pci_enable_wake(dev, PCI_D3cold, enable) :
1637 pci_enable_wake(dev, PCI_D3hot, enable);
1638}
1639
1640/**
Jesse Barnes37139072008-07-28 11:49:26 -07001641 * pci_target_state - find an appropriate low power state for a given PCI dev
1642 * @dev: PCI device
1643 *
1644 * Use underlying platform code to find a supported low power state for @dev.
1645 * If the platform can't manage @dev, return the deepest state from which it
1646 * can generate wake events, based on any available PME info.
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001647 */
Rafael J. Wysockie5899e12008-07-19 14:39:24 +02001648pci_power_t pci_target_state(struct pci_dev *dev)
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001649{
1650 pci_power_t target_state = PCI_D3hot;
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001651
1652 if (platform_pci_power_manageable(dev)) {
1653 /*
1654 * Call the platform to choose the target state of the device
1655 * and enable wake-up from this state if supported.
1656 */
1657 pci_power_t state = platform_pci_choose_state(dev);
1658
1659 switch (state) {
1660 case PCI_POWER_ERROR:
1661 case PCI_UNKNOWN:
1662 break;
1663 case PCI_D1:
1664 case PCI_D2:
1665 if (pci_no_d1d2(dev))
1666 break;
1667 default:
1668 target_state = state;
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001669 }
Rafael J. Wysockid2abdf62009-06-14 21:25:02 +02001670 } else if (!dev->pm_cap) {
1671 target_state = PCI_D0;
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001672 } else if (device_may_wakeup(&dev->dev)) {
1673 /*
1674 * Find the deepest state from which the device can generate
1675 * wake-up events, make it the target state and enable device
1676 * to generate PME#.
1677 */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001678 if (dev->pme_support) {
1679 while (target_state
1680 && !(dev->pme_support & (1 << target_state)))
1681 target_state--;
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001682 }
1683 }
1684
Rafael J. Wysockie5899e12008-07-19 14:39:24 +02001685 return target_state;
1686}
1687
1688/**
1689 * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
1690 * @dev: Device to handle.
1691 *
1692 * Choose the power state appropriate for the device depending on whether
1693 * it can wake up the system and/or is power manageable by the platform
1694 * (PCI_D3hot is the default) and put the device into that state.
1695 */
1696int pci_prepare_to_sleep(struct pci_dev *dev)
1697{
1698 pci_power_t target_state = pci_target_state(dev);
1699 int error;
1700
1701 if (target_state == PCI_POWER_ERROR)
1702 return -EIO;
1703
Rafael J. Wysocki8efb8c72009-03-30 21:46:27 +02001704 pci_enable_wake(dev, target_state, device_may_wakeup(&dev->dev));
Rafael J. Wysockic157dfa2008-07-13 22:45:06 +02001705
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001706 error = pci_set_power_state(dev, target_state);
1707
1708 if (error)
1709 pci_enable_wake(dev, target_state, false);
1710
1711 return error;
1712}
1713
1714/**
Randy Dunlap443bd1c2008-07-21 09:27:18 -07001715 * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001716 * @dev: Device to handle.
1717 *
Thomas Weber88393162010-03-16 11:47:56 +01001718 * Disable device's system wake-up capability and put it into D0.
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02001719 */
1720int pci_back_from_sleep(struct pci_dev *dev)
1721{
1722 pci_enable_wake(dev, PCI_D0, false);
1723 return pci_set_power_state(dev, PCI_D0);
1724}
1725
1726/**
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001727 * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend.
1728 * @dev: PCI device being suspended.
1729 *
1730 * Prepare @dev to generate wake-up events at run time and put it into a low
1731 * power state.
1732 */
1733int pci_finish_runtime_suspend(struct pci_dev *dev)
1734{
1735 pci_power_t target_state = pci_target_state(dev);
1736 int error;
1737
1738 if (target_state == PCI_POWER_ERROR)
1739 return -EIO;
1740
1741 __pci_enable_wake(dev, target_state, true, pci_dev_run_wake(dev));
1742
1743 error = pci_set_power_state(dev, target_state);
1744
1745 if (error)
1746 __pci_enable_wake(dev, target_state, true, false);
1747
1748 return error;
1749}
1750
1751/**
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001752 * pci_dev_run_wake - Check if device can generate run-time wake-up events.
1753 * @dev: Device to check.
1754 *
1755 * Return true if the device itself is cabable of generating wake-up events
1756 * (through the platform or using the native PCIe PME) or if the device supports
1757 * PME and one of its upstream bridges can generate wake-up events.
1758 */
1759bool pci_dev_run_wake(struct pci_dev *dev)
1760{
1761 struct pci_bus *bus = dev->bus;
1762
1763 if (device_run_wake(&dev->dev))
1764 return true;
1765
1766 if (!dev->pme_support)
1767 return false;
1768
1769 while (bus->parent) {
1770 struct pci_dev *bridge = bus->self;
1771
1772 if (device_run_wake(&bridge->dev))
1773 return true;
1774
1775 bus = bus->parent;
1776 }
1777
1778 /* We have reached the root bus. */
1779 if (bus->bridge)
1780 return device_run_wake(bus->bridge);
1781
1782 return false;
1783}
1784EXPORT_SYMBOL_GPL(pci_dev_run_wake);
1785
1786/**
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001787 * pci_pm_init - Initialize PM functions of given PCI device
1788 * @dev: PCI device to handle.
1789 */
1790void pci_pm_init(struct pci_dev *dev)
1791{
1792 int pm;
1793 u16 pmc;
David Brownell075c1772007-04-26 00:12:06 -07001794
Rafael J. Wysockibb910a72010-02-27 21:37:37 +01001795 pm_runtime_forbid(&dev->dev);
Rafael J. Wysockia1e4d722010-02-08 19:16:33 +01001796 device_enable_async_suspend(&dev->dev);
Rafael J. Wysockie80bb092009-09-08 23:14:49 +02001797 dev->wakeup_prepared = false;
Rafael J. Wysockibb910a72010-02-27 21:37:37 +01001798
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001799 dev->pm_cap = 0;
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 /* find PCI PM capability in list */
1802 pm = pci_find_capability(dev, PCI_CAP_ID_PM);
David Brownell075c1772007-04-26 00:12:06 -07001803 if (!pm)
Linus Torvalds50246dd2009-01-16 08:14:51 -08001804 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /* Check device's ability to generate PME# */
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001806 pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001808 if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
1809 dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n",
1810 pmc & PCI_PM_CAP_VER_MASK);
Linus Torvalds50246dd2009-01-16 08:14:51 -08001811 return;
David Brownell075c1772007-04-26 00:12:06 -07001812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001814 dev->pm_cap = pm;
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +01001815 dev->d3_delay = PCI_PM_D3_WAIT;
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001816
1817 dev->d1_support = false;
1818 dev->d2_support = false;
1819 if (!pci_no_d1d2(dev)) {
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06001820 if (pmc & PCI_PM_CAP_D1)
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001821 dev->d1_support = true;
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06001822 if (pmc & PCI_PM_CAP_D2)
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001823 dev->d2_support = true;
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06001824
1825 if (dev->d1_support || dev->d2_support)
1826 dev_printk(KERN_DEBUG, &dev->dev, "supports%s%s\n",
Jesse Barnesec84f122008-09-23 11:43:34 -07001827 dev->d1_support ? " D1" : "",
1828 dev->d2_support ? " D2" : "");
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001829 }
1830
1831 pmc &= PCI_PM_CAP_PME_MASK;
1832 if (pmc) {
Bjorn Helgaas10c3d712009-11-04 10:32:42 -07001833 dev_printk(KERN_DEBUG, &dev->dev,
1834 "PME# supported from%s%s%s%s%s\n",
Bjorn Helgaasc9ed77e2008-08-22 09:37:02 -06001835 (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
1836 (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
1837 (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
1838 (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
1839 (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001840 dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
Rafael J. Wysocki379021d2011-10-03 23:16:33 +02001841 dev->pme_poll = true;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001842 /*
1843 * Make device's PM flags reflect the wake-up capability, but
1844 * let the user space enable it to wake up the system as needed.
1845 */
1846 device_set_wakeup_capable(&dev->dev, true);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001847 /* Disable the PME# generation functionality */
Rafael J. Wysocki337001b2008-07-07 03:36:24 +02001848 pci_pme_active(dev, false);
1849 } else {
1850 dev->pme_support = 0;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
Yu Zhao58c3a722008-10-14 14:02:53 +08001854/**
Jesse Barneseb9c39d2008-12-17 12:10:05 -08001855 * platform_pci_wakeup_init - init platform wakeup if present
1856 * @dev: PCI device
1857 *
1858 * Some devices don't have PCI PM caps but can still generate wakeup
1859 * events through platform methods (like ACPI events). If @dev supports
1860 * platform wakeup events, set the device flag to indicate as much. This
1861 * may be redundant if the device also supports PCI PM caps, but double
1862 * initialization should be safe in that case.
1863 */
1864void platform_pci_wakeup_init(struct pci_dev *dev)
1865{
1866 if (!platform_pci_can_wakeup(dev))
1867 return;
1868
1869 device_set_wakeup_capable(&dev->dev, true);
Jesse Barneseb9c39d2008-12-17 12:10:05 -08001870 platform_pci_sleep_wake(dev, false);
1871}
1872
Yinghai Lu34a48762012-02-11 00:18:41 -08001873static void pci_add_saved_cap(struct pci_dev *pci_dev,
1874 struct pci_cap_saved_state *new_cap)
1875{
1876 hlist_add_head(&new_cap->next, &pci_dev->saved_cap_space);
1877}
1878
Jesse Barneseb9c39d2008-12-17 12:10:05 -08001879/**
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01001880 * pci_add_save_buffer - allocate buffer for saving given capability registers
1881 * @dev: the PCI device
1882 * @cap: the capability to allocate the buffer for
1883 * @size: requested size of the buffer
1884 */
1885static int pci_add_cap_save_buffer(
1886 struct pci_dev *dev, char cap, unsigned int size)
1887{
1888 int pos;
1889 struct pci_cap_saved_state *save_state;
1890
1891 pos = pci_find_capability(dev, cap);
1892 if (pos <= 0)
1893 return 0;
1894
1895 save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
1896 if (!save_state)
1897 return -ENOMEM;
1898
Alex Williamson24a4742f2011-05-10 10:02:11 -06001899 save_state->cap.cap_nr = cap;
1900 save_state->cap.size = size;
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01001901 pci_add_saved_cap(dev, save_state);
1902
1903 return 0;
1904}
1905
1906/**
1907 * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
1908 * @dev: the PCI device
1909 */
1910void pci_allocate_cap_save_buffers(struct pci_dev *dev)
1911{
1912 int error;
1913
Yu Zhao89858512009-02-16 02:55:47 +08001914 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP,
1915 PCI_EXP_SAVE_REGS * sizeof(u16));
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01001916 if (error)
1917 dev_err(&dev->dev,
1918 "unable to preallocate PCI Express save buffer\n");
1919
1920 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
1921 if (error)
1922 dev_err(&dev->dev,
1923 "unable to preallocate PCI-X save buffer\n");
1924}
1925
Yinghai Luf7968412012-02-11 00:18:30 -08001926void pci_free_cap_save_buffers(struct pci_dev *dev)
1927{
1928 struct pci_cap_saved_state *tmp;
1929 struct hlist_node *pos, *n;
1930
1931 hlist_for_each_entry_safe(tmp, pos, n, &dev->saved_cap_space, next)
1932 kfree(tmp);
1933}
1934
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01001935/**
Yu Zhao58c3a722008-10-14 14:02:53 +08001936 * pci_enable_ari - enable ARI forwarding if hardware support it
1937 * @dev: the PCI device
1938 */
1939void pci_enable_ari(struct pci_dev *dev)
1940{
1941 int pos;
1942 u32 cap;
Chris Wright864d2962011-07-13 10:14:33 -07001943 u16 flags, ctrl;
Zhao, Yu81135872008-10-23 13:15:39 +08001944 struct pci_dev *bridge;
Yu Zhao58c3a722008-10-14 14:02:53 +08001945
Rafael J. Wysocki6748dcc2012-03-01 00:06:33 +01001946 if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
Yu Zhao58c3a722008-10-14 14:02:53 +08001947 return;
1948
Zhao, Yu81135872008-10-23 13:15:39 +08001949 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
Yu Zhao58c3a722008-10-14 14:02:53 +08001950 if (!pos)
1951 return;
1952
Zhao, Yu81135872008-10-23 13:15:39 +08001953 bridge = dev->bus->self;
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +09001954 if (!bridge || !pci_is_pcie(bridge))
Zhao, Yu81135872008-10-23 13:15:39 +08001955 return;
1956
Kenji Kaneshige06a1cba2009-11-11 14:30:56 +09001957 pos = pci_pcie_cap(bridge);
Zhao, Yu81135872008-10-23 13:15:39 +08001958 if (!pos)
1959 return;
1960
Chris Wright864d2962011-07-13 10:14:33 -07001961 /* ARI is a PCIe v2 feature */
1962 pci_read_config_word(bridge, pos + PCI_EXP_FLAGS, &flags);
1963 if ((flags & PCI_EXP_FLAGS_VERS) < 2)
1964 return;
1965
Zhao, Yu81135872008-10-23 13:15:39 +08001966 pci_read_config_dword(bridge, pos + PCI_EXP_DEVCAP2, &cap);
Yu Zhao58c3a722008-10-14 14:02:53 +08001967 if (!(cap & PCI_EXP_DEVCAP2_ARI))
1968 return;
1969
Zhao, Yu81135872008-10-23 13:15:39 +08001970 pci_read_config_word(bridge, pos + PCI_EXP_DEVCTL2, &ctrl);
Yu Zhao58c3a722008-10-14 14:02:53 +08001971 ctrl |= PCI_EXP_DEVCTL2_ARI;
Zhao, Yu81135872008-10-23 13:15:39 +08001972 pci_write_config_word(bridge, pos + PCI_EXP_DEVCTL2, ctrl);
Yu Zhao58c3a722008-10-14 14:02:53 +08001973
Zhao, Yu81135872008-10-23 13:15:39 +08001974 bridge->ari_enabled = 1;
Yu Zhao58c3a722008-10-14 14:02:53 +08001975}
1976
Jesse Barnesb48d4422010-10-19 13:07:57 -07001977/**
1978 * pci_enable_ido - enable ID-based ordering on a device
1979 * @dev: the PCI device
1980 * @type: which types of IDO to enable
1981 *
1982 * Enable ID-based ordering on @dev. @type can contain the bits
1983 * %PCI_EXP_IDO_REQUEST and/or %PCI_EXP_IDO_COMPLETION to indicate
1984 * which types of transactions are allowed to be re-ordered.
1985 */
1986void pci_enable_ido(struct pci_dev *dev, unsigned long type)
1987{
1988 int pos;
1989 u16 ctrl;
1990
1991 pos = pci_pcie_cap(dev);
1992 if (!pos)
1993 return;
1994
1995 pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &ctrl);
1996 if (type & PCI_EXP_IDO_REQUEST)
1997 ctrl |= PCI_EXP_IDO_REQ_EN;
1998 if (type & PCI_EXP_IDO_COMPLETION)
1999 ctrl |= PCI_EXP_IDO_CMP_EN;
2000 pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, ctrl);
2001}
2002EXPORT_SYMBOL(pci_enable_ido);
2003
2004/**
2005 * pci_disable_ido - disable ID-based ordering on a device
2006 * @dev: the PCI device
2007 * @type: which types of IDO to disable
2008 */
2009void pci_disable_ido(struct pci_dev *dev, unsigned long type)
2010{
2011 int pos;
2012 u16 ctrl;
2013
2014 if (!pci_is_pcie(dev))
2015 return;
2016
2017 pos = pci_pcie_cap(dev);
2018 if (!pos)
2019 return;
2020
2021 pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &ctrl);
2022 if (type & PCI_EXP_IDO_REQUEST)
2023 ctrl &= ~PCI_EXP_IDO_REQ_EN;
2024 if (type & PCI_EXP_IDO_COMPLETION)
2025 ctrl &= ~PCI_EXP_IDO_CMP_EN;
2026 pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, ctrl);
2027}
2028EXPORT_SYMBOL(pci_disable_ido);
2029
Jesse Barnes48a92a82011-01-10 12:46:36 -08002030/**
2031 * pci_enable_obff - enable optimized buffer flush/fill
2032 * @dev: PCI device
2033 * @type: type of signaling to use
2034 *
2035 * Try to enable @type OBFF signaling on @dev. It will try using WAKE#
2036 * signaling if possible, falling back to message signaling only if
2037 * WAKE# isn't supported. @type should indicate whether the PCIe link
2038 * be brought out of L0s or L1 to send the message. It should be either
2039 * %PCI_EXP_OBFF_SIGNAL_ALWAYS or %PCI_OBFF_SIGNAL_L0.
2040 *
2041 * If your device can benefit from receiving all messages, even at the
2042 * power cost of bringing the link back up from a low power state, use
2043 * %PCI_EXP_OBFF_SIGNAL_ALWAYS. Otherwise, use %PCI_OBFF_SIGNAL_L0 (the
2044 * preferred type).
2045 *
2046 * RETURNS:
2047 * Zero on success, appropriate error number on failure.
2048 */
2049int pci_enable_obff(struct pci_dev *dev, enum pci_obff_signal_type type)
2050{
2051 int pos;
2052 u32 cap;
2053 u16 ctrl;
2054 int ret;
2055
2056 if (!pci_is_pcie(dev))
2057 return -ENOTSUPP;
2058
2059 pos = pci_pcie_cap(dev);
2060 if (!pos)
2061 return -ENOTSUPP;
2062
2063 pci_read_config_dword(dev, pos + PCI_EXP_DEVCAP2, &cap);
2064 if (!(cap & PCI_EXP_OBFF_MASK))
2065 return -ENOTSUPP; /* no OBFF support at all */
2066
2067 /* Make sure the topology supports OBFF as well */
2068 if (dev->bus) {
2069 ret = pci_enable_obff(dev->bus->self, type);
2070 if (ret)
2071 return ret;
2072 }
2073
2074 pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &ctrl);
2075 if (cap & PCI_EXP_OBFF_WAKE)
2076 ctrl |= PCI_EXP_OBFF_WAKE_EN;
2077 else {
2078 switch (type) {
2079 case PCI_EXP_OBFF_SIGNAL_L0:
2080 if (!(ctrl & PCI_EXP_OBFF_WAKE_EN))
2081 ctrl |= PCI_EXP_OBFF_MSGA_EN;
2082 break;
2083 case PCI_EXP_OBFF_SIGNAL_ALWAYS:
2084 ctrl &= ~PCI_EXP_OBFF_WAKE_EN;
2085 ctrl |= PCI_EXP_OBFF_MSGB_EN;
2086 break;
2087 default:
2088 WARN(1, "bad OBFF signal type\n");
2089 return -ENOTSUPP;
2090 }
2091 }
2092 pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, ctrl);
2093
2094 return 0;
2095}
2096EXPORT_SYMBOL(pci_enable_obff);
2097
2098/**
2099 * pci_disable_obff - disable optimized buffer flush/fill
2100 * @dev: PCI device
2101 *
2102 * Disable OBFF on @dev.
2103 */
2104void pci_disable_obff(struct pci_dev *dev)
2105{
2106 int pos;
2107 u16 ctrl;
2108
2109 if (!pci_is_pcie(dev))
2110 return;
2111
2112 pos = pci_pcie_cap(dev);
2113 if (!pos)
2114 return;
2115
2116 pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &ctrl);
2117 ctrl &= ~PCI_EXP_OBFF_WAKE_EN;
2118 pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, ctrl);
2119}
2120EXPORT_SYMBOL(pci_disable_obff);
2121
Jesse Barnes51c2e0a2011-01-14 08:53:04 -08002122/**
2123 * pci_ltr_supported - check whether a device supports LTR
2124 * @dev: PCI device
2125 *
2126 * RETURNS:
2127 * True if @dev supports latency tolerance reporting, false otherwise.
2128 */
2129bool pci_ltr_supported(struct pci_dev *dev)
2130{
2131 int pos;
2132 u32 cap;
2133
2134 if (!pci_is_pcie(dev))
2135 return false;
2136
2137 pos = pci_pcie_cap(dev);
2138 if (!pos)
2139 return false;
2140
2141 pci_read_config_dword(dev, pos + PCI_EXP_DEVCAP2, &cap);
2142
2143 return cap & PCI_EXP_DEVCAP2_LTR;
2144}
2145EXPORT_SYMBOL(pci_ltr_supported);
2146
2147/**
2148 * pci_enable_ltr - enable latency tolerance reporting
2149 * @dev: PCI device
2150 *
2151 * Enable LTR on @dev if possible, which means enabling it first on
2152 * upstream ports.
2153 *
2154 * RETURNS:
2155 * Zero on success, errno on failure.
2156 */
2157int pci_enable_ltr(struct pci_dev *dev)
2158{
2159 int pos;
2160 u16 ctrl;
2161 int ret;
2162
2163 if (!pci_ltr_supported(dev))
2164 return -ENOTSUPP;
2165
2166 pos = pci_pcie_cap(dev);
2167 if (!pos)
2168 return -ENOTSUPP;
2169
2170 /* Only primary function can enable/disable LTR */
2171 if (PCI_FUNC(dev->devfn) != 0)
2172 return -EINVAL;
2173
2174 /* Enable upstream ports first */
2175 if (dev->bus) {
2176 ret = pci_enable_ltr(dev->bus->self);
2177 if (ret)
2178 return ret;
2179 }
2180
2181 pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &ctrl);
2182 ctrl |= PCI_EXP_LTR_EN;
2183 pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, ctrl);
2184
2185 return 0;
2186}
2187EXPORT_SYMBOL(pci_enable_ltr);
2188
2189/**
2190 * pci_disable_ltr - disable latency tolerance reporting
2191 * @dev: PCI device
2192 */
2193void pci_disable_ltr(struct pci_dev *dev)
2194{
2195 int pos;
2196 u16 ctrl;
2197
2198 if (!pci_ltr_supported(dev))
2199 return;
2200
2201 pos = pci_pcie_cap(dev);
2202 if (!pos)
2203 return;
2204
2205 /* Only primary function can enable/disable LTR */
2206 if (PCI_FUNC(dev->devfn) != 0)
2207 return;
2208
2209 pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &ctrl);
2210 ctrl &= ~PCI_EXP_LTR_EN;
2211 pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, ctrl);
2212}
2213EXPORT_SYMBOL(pci_disable_ltr);
2214
2215static int __pci_ltr_scale(int *val)
2216{
2217 int scale = 0;
2218
2219 while (*val > 1023) {
2220 *val = (*val + 31) / 32;
2221 scale++;
2222 }
2223 return scale;
2224}
2225
2226/**
2227 * pci_set_ltr - set LTR latency values
2228 * @dev: PCI device
2229 * @snoop_lat_ns: snoop latency in nanoseconds
2230 * @nosnoop_lat_ns: nosnoop latency in nanoseconds
2231 *
2232 * Figure out the scale and set the LTR values accordingly.
2233 */
2234int pci_set_ltr(struct pci_dev *dev, int snoop_lat_ns, int nosnoop_lat_ns)
2235{
2236 int pos, ret, snoop_scale, nosnoop_scale;
2237 u16 val;
2238
2239 if (!pci_ltr_supported(dev))
2240 return -ENOTSUPP;
2241
2242 snoop_scale = __pci_ltr_scale(&snoop_lat_ns);
2243 nosnoop_scale = __pci_ltr_scale(&nosnoop_lat_ns);
2244
2245 if (snoop_lat_ns > PCI_LTR_VALUE_MASK ||
2246 nosnoop_lat_ns > PCI_LTR_VALUE_MASK)
2247 return -EINVAL;
2248
2249 if ((snoop_scale > (PCI_LTR_SCALE_MASK >> PCI_LTR_SCALE_SHIFT)) ||
2250 (nosnoop_scale > (PCI_LTR_SCALE_MASK >> PCI_LTR_SCALE_SHIFT)))
2251 return -EINVAL;
2252
2253 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_LTR);
2254 if (!pos)
2255 return -ENOTSUPP;
2256
2257 val = (snoop_scale << PCI_LTR_SCALE_SHIFT) | snoop_lat_ns;
2258 ret = pci_write_config_word(dev, pos + PCI_LTR_MAX_SNOOP_LAT, val);
2259 if (ret != 4)
2260 return -EIO;
2261
2262 val = (nosnoop_scale << PCI_LTR_SCALE_SHIFT) | nosnoop_lat_ns;
2263 ret = pci_write_config_word(dev, pos + PCI_LTR_MAX_NOSNOOP_LAT, val);
2264 if (ret != 4)
2265 return -EIO;
2266
2267 return 0;
2268}
2269EXPORT_SYMBOL(pci_set_ltr);
2270
Chris Wright5d990b62009-12-04 12:15:21 -08002271static int pci_acs_enable;
2272
2273/**
2274 * pci_request_acs - ask for ACS to be enabled if supported
2275 */
2276void pci_request_acs(void)
2277{
2278 pci_acs_enable = 1;
2279}
2280
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07002281/**
Allen Kayae21ee62009-10-07 10:27:17 -07002282 * pci_enable_acs - enable ACS if hardware support it
2283 * @dev: the PCI device
2284 */
2285void pci_enable_acs(struct pci_dev *dev)
2286{
2287 int pos;
2288 u16 cap;
2289 u16 ctrl;
2290
Chris Wright5d990b62009-12-04 12:15:21 -08002291 if (!pci_acs_enable)
2292 return;
2293
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +09002294 if (!pci_is_pcie(dev))
Allen Kayae21ee62009-10-07 10:27:17 -07002295 return;
2296
2297 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
2298 if (!pos)
2299 return;
2300
2301 pci_read_config_word(dev, pos + PCI_ACS_CAP, &cap);
2302 pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
2303
2304 /* Source Validation */
2305 ctrl |= (cap & PCI_ACS_SV);
2306
2307 /* P2P Request Redirect */
2308 ctrl |= (cap & PCI_ACS_RR);
2309
2310 /* P2P Completion Redirect */
2311 ctrl |= (cap & PCI_ACS_CR);
2312
2313 /* Upstream Forwarding */
2314 ctrl |= (cap & PCI_ACS_UF);
2315
2316 pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
2317}
2318
2319/**
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07002320 * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
2321 * @dev: the PCI device
2322 * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTD, 4=INTD)
2323 *
2324 * Perform INTx swizzling for a device behind one level of bridge. This is
2325 * required by section 9.1 of the PCI-to-PCI bridge specification for devices
Matthew Wilcox46b952a2009-07-01 14:24:30 -07002326 * behind bridges on add-in cards. For devices with ARI enabled, the slot
2327 * number is always 0 (see the Implementation Note in section 2.2.8.1 of
2328 * the PCI Express Base Specification, Revision 2.1)
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07002329 */
John Crispin3df425f2012-04-12 17:33:07 +02002330u8 pci_swizzle_interrupt_pin(const struct pci_dev *dev, u8 pin)
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07002331{
Matthew Wilcox46b952a2009-07-01 14:24:30 -07002332 int slot;
2333
2334 if (pci_ari_enabled(dev->bus))
2335 slot = 0;
2336 else
2337 slot = PCI_SLOT(dev->devfn);
2338
2339 return (((pin - 1) + slot) % 4) + 1;
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07002340}
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342int
2343pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
2344{
2345 u8 pin;
2346
Kristen Accardi514d2072005-11-02 16:24:39 -08002347 pin = dev->pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 if (!pin)
2349 return -1;
Bjorn Helgaas878f2e52008-12-09 16:11:46 -07002350
Kenji Kaneshige8784fd42009-05-26 16:07:33 +09002351 while (!pci_is_root_bus(dev->bus)) {
Bjorn Helgaas57c2cf72008-12-11 11:24:23 -07002352 pin = pci_swizzle_interrupt_pin(dev, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 dev = dev->bus->self;
2354 }
2355 *bridge = dev;
2356 return pin;
2357}
2358
2359/**
Bjorn Helgaas68feac82008-12-16 21:36:55 -07002360 * pci_common_swizzle - swizzle INTx all the way to root bridge
2361 * @dev: the PCI device
2362 * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
2363 *
2364 * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
2365 * bridges all the way up to a PCI root bus.
2366 */
2367u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
2368{
2369 u8 pin = *pinp;
2370
Kenji Kaneshige1eb39482009-05-26 16:08:36 +09002371 while (!pci_is_root_bus(dev->bus)) {
Bjorn Helgaas68feac82008-12-16 21:36:55 -07002372 pin = pci_swizzle_interrupt_pin(dev, pin);
2373 dev = dev->bus->self;
2374 }
2375 *pinp = pin;
2376 return PCI_SLOT(dev->devfn);
2377}
2378
2379/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 * pci_release_region - Release a PCI bar
2381 * @pdev: PCI device whose resources were previously reserved by pci_request_region
2382 * @bar: BAR to release
2383 *
2384 * Releases the PCI I/O and memory resources previously reserved by a
2385 * successful call to pci_request_region. Call this function only
2386 * after all use of the PCI regions has ceased.
2387 */
2388void pci_release_region(struct pci_dev *pdev, int bar)
2389{
Tejun Heo9ac78492007-01-20 16:00:26 +09002390 struct pci_devres *dr;
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (pci_resource_len(pdev, bar) == 0)
2393 return;
2394 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
2395 release_region(pci_resource_start(pdev, bar),
2396 pci_resource_len(pdev, bar));
2397 else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
2398 release_mem_region(pci_resource_start(pdev, bar),
2399 pci_resource_len(pdev, bar));
Tejun Heo9ac78492007-01-20 16:00:26 +09002400
2401 dr = find_pci_dr(pdev);
2402 if (dr)
2403 dr->region_mask &= ~(1 << bar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
2406/**
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08002407 * __pci_request_region - Reserved PCI I/O and memory resource
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 * @pdev: PCI device whose resources are to be reserved
2409 * @bar: BAR to be reserved
2410 * @res_name: Name to be associated with resource.
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08002411 * @exclusive: whether the region access is exclusive or not
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 *
2413 * Mark the PCI region associated with PCI device @pdev BR @bar as
2414 * being reserved by owner @res_name. Do not access any
2415 * address inside the PCI regions unless this call returns
2416 * successfully.
2417 *
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08002418 * If @exclusive is set, then the region is marked so that userspace
2419 * is explicitly not allowed to map the resource via /dev/mem or
2420 * sysfs MMIO access.
2421 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 * Returns 0 on success, or %EBUSY on error. A warning
2423 * message is also printed on failure.
2424 */
Arjan van de Vene8de1482008-10-22 19:55:31 -07002425static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name,
2426 int exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
Tejun Heo9ac78492007-01-20 16:00:26 +09002428 struct pci_devres *dr;
2429
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (pci_resource_len(pdev, bar) == 0)
2431 return 0;
2432
2433 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
2434 if (!request_region(pci_resource_start(pdev, bar),
2435 pci_resource_len(pdev, bar), res_name))
2436 goto err_out;
2437 }
2438 else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
Arjan van de Vene8de1482008-10-22 19:55:31 -07002439 if (!__request_mem_region(pci_resource_start(pdev, bar),
2440 pci_resource_len(pdev, bar), res_name,
2441 exclusive))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 goto err_out;
2443 }
Tejun Heo9ac78492007-01-20 16:00:26 +09002444
2445 dr = find_pci_dr(pdev);
2446 if (dr)
2447 dr->region_mask |= 1 << bar;
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 return 0;
2450
2451err_out:
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002452 dev_warn(&pdev->dev, "BAR %d: can't reserve %pR\n", bar,
Benjamin Herrenschmidt096e6f62008-10-20 15:07:37 +11002453 &pdev->resource[bar]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return -EBUSY;
2455}
2456
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09002457/**
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08002458 * pci_request_region - Reserve PCI I/O and memory resource
Arjan van de Vene8de1482008-10-22 19:55:31 -07002459 * @pdev: PCI device whose resources are to be reserved
2460 * @bar: BAR to be reserved
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08002461 * @res_name: Name to be associated with resource
Arjan van de Vene8de1482008-10-22 19:55:31 -07002462 *
Randy Dunlapf5ddcac2009-01-09 17:03:20 -08002463 * Mark the PCI region associated with PCI device @pdev BAR @bar as
Arjan van de Vene8de1482008-10-22 19:55:31 -07002464 * being reserved by owner @res_name. Do not access any
2465 * address inside the PCI regions unless this call returns
2466 * successfully.
2467 *
2468 * Returns 0 on success, or %EBUSY on error. A warning
2469 * message is also printed on failure.
2470 */
2471int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
2472{
2473 return __pci_request_region(pdev, bar, res_name, 0);
2474}
2475
2476/**
2477 * pci_request_region_exclusive - Reserved PCI I/O and memory resource
2478 * @pdev: PCI device whose resources are to be reserved
2479 * @bar: BAR to be reserved
2480 * @res_name: Name to be associated with resource.
2481 *
2482 * Mark the PCI region associated with PCI device @pdev BR @bar as
2483 * being reserved by owner @res_name. Do not access any
2484 * address inside the PCI regions unless this call returns
2485 * successfully.
2486 *
2487 * Returns 0 on success, or %EBUSY on error. A warning
2488 * message is also printed on failure.
2489 *
2490 * The key difference that _exclusive makes it that userspace is
2491 * explicitly not allowed to map the resource via /dev/mem or
2492 * sysfs.
2493 */
2494int pci_request_region_exclusive(struct pci_dev *pdev, int bar, const char *res_name)
2495{
2496 return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
2497}
2498/**
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09002499 * pci_release_selected_regions - Release selected PCI I/O and memory resources
2500 * @pdev: PCI device whose resources were previously reserved
2501 * @bars: Bitmask of BARs to be released
2502 *
2503 * Release selected PCI I/O and memory resources previously reserved.
2504 * Call this function only after all use of the PCI regions has ceased.
2505 */
2506void pci_release_selected_regions(struct pci_dev *pdev, int bars)
2507{
2508 int i;
2509
2510 for (i = 0; i < 6; i++)
2511 if (bars & (1 << i))
2512 pci_release_region(pdev, i);
2513}
2514
Arjan van de Vene8de1482008-10-22 19:55:31 -07002515int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
2516 const char *res_name, int excl)
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09002517{
2518 int i;
2519
2520 for (i = 0; i < 6; i++)
2521 if (bars & (1 << i))
Arjan van de Vene8de1482008-10-22 19:55:31 -07002522 if (__pci_request_region(pdev, i, res_name, excl))
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09002523 goto err_out;
2524 return 0;
2525
2526err_out:
2527 while(--i >= 0)
2528 if (bars & (1 << i))
2529 pci_release_region(pdev, i);
2530
2531 return -EBUSY;
2532}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Arjan van de Vene8de1482008-10-22 19:55:31 -07002534
2535/**
2536 * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
2537 * @pdev: PCI device whose resources are to be reserved
2538 * @bars: Bitmask of BARs to be requested
2539 * @res_name: Name to be associated with resource
2540 */
2541int pci_request_selected_regions(struct pci_dev *pdev, int bars,
2542 const char *res_name)
2543{
2544 return __pci_request_selected_regions(pdev, bars, res_name, 0);
2545}
2546
2547int pci_request_selected_regions_exclusive(struct pci_dev *pdev,
2548 int bars, const char *res_name)
2549{
2550 return __pci_request_selected_regions(pdev, bars, res_name,
2551 IORESOURCE_EXCLUSIVE);
2552}
2553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554/**
2555 * pci_release_regions - Release reserved PCI I/O and memory resources
2556 * @pdev: PCI device whose resources were previously reserved by pci_request_regions
2557 *
2558 * Releases all PCI I/O and memory resources previously reserved by a
2559 * successful call to pci_request_regions. Call this function only
2560 * after all use of the PCI regions has ceased.
2561 */
2562
2563void pci_release_regions(struct pci_dev *pdev)
2564{
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09002565 pci_release_selected_regions(pdev, (1 << 6) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
2568/**
2569 * pci_request_regions - Reserved PCI I/O and memory resources
2570 * @pdev: PCI device whose resources are to be reserved
2571 * @res_name: Name to be associated with resource.
2572 *
2573 * Mark all PCI regions associated with PCI device @pdev as
2574 * being reserved by owner @res_name. Do not access any
2575 * address inside the PCI regions unless this call returns
2576 * successfully.
2577 *
2578 * Returns 0 on success, or %EBUSY on error. A warning
2579 * message is also printed on failure.
2580 */
Jeff Garzik3c990e92006-03-04 21:52:42 -05002581int pci_request_regions(struct pci_dev *pdev, const char *res_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09002583 return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
2586/**
Arjan van de Vene8de1482008-10-22 19:55:31 -07002587 * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
2588 * @pdev: PCI device whose resources are to be reserved
2589 * @res_name: Name to be associated with resource.
2590 *
2591 * Mark all PCI regions associated with PCI device @pdev as
2592 * being reserved by owner @res_name. Do not access any
2593 * address inside the PCI regions unless this call returns
2594 * successfully.
2595 *
2596 * pci_request_regions_exclusive() will mark the region so that
2597 * /dev/mem and the sysfs MMIO access will not be allowed.
2598 *
2599 * Returns 0 on success, or %EBUSY on error. A warning
2600 * message is also printed on failure.
2601 */
2602int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
2603{
2604 return pci_request_selected_regions_exclusive(pdev,
2605 ((1 << 6) - 1), res_name);
2606}
2607
Ben Hutchings6a479072008-12-23 03:08:29 +00002608static void __pci_set_master(struct pci_dev *dev, bool enable)
2609{
2610 u16 old_cmd, cmd;
2611
2612 pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
2613 if (enable)
2614 cmd = old_cmd | PCI_COMMAND_MASTER;
2615 else
2616 cmd = old_cmd & ~PCI_COMMAND_MASTER;
2617 if (cmd != old_cmd) {
2618 dev_dbg(&dev->dev, "%s bus mastering\n",
2619 enable ? "enabling" : "disabling");
2620 pci_write_config_word(dev, PCI_COMMAND, cmd);
2621 }
2622 dev->is_busmaster = enable;
2623}
Arjan van de Vene8de1482008-10-22 19:55:31 -07002624
2625/**
Myron Stowe96c55902011-10-28 15:48:38 -06002626 * pcibios_set_master - enable PCI bus-mastering for device dev
2627 * @dev: the PCI device to enable
2628 *
2629 * Enables PCI bus-mastering for the device. This is the default
2630 * implementation. Architecture specific implementations can override
2631 * this if necessary.
2632 */
2633void __weak pcibios_set_master(struct pci_dev *dev)
2634{
2635 u8 lat;
2636
Myron Stowef6766782011-10-28 15:49:20 -06002637 /* The latency timer doesn't apply to PCIe (either Type 0 or Type 1) */
2638 if (pci_is_pcie(dev))
2639 return;
2640
Myron Stowe96c55902011-10-28 15:48:38 -06002641 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
2642 if (lat < 16)
2643 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
2644 else if (lat > pcibios_max_latency)
2645 lat = pcibios_max_latency;
2646 else
2647 return;
2648 dev_printk(KERN_DEBUG, &dev->dev, "setting latency timer to %d\n", lat);
2649 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
2650}
2651
2652/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 * pci_set_master - enables bus-mastering for device dev
2654 * @dev: the PCI device to enable
2655 *
2656 * Enables bus-mastering on the device and calls pcibios_set_master()
2657 * to do the needed arch specific settings.
2658 */
Ben Hutchings6a479072008-12-23 03:08:29 +00002659void pci_set_master(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
Ben Hutchings6a479072008-12-23 03:08:29 +00002661 __pci_set_master(dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 pcibios_set_master(dev);
2663}
2664
Ben Hutchings6a479072008-12-23 03:08:29 +00002665/**
2666 * pci_clear_master - disables bus-mastering for device dev
2667 * @dev: the PCI device to disable
2668 */
2669void pci_clear_master(struct pci_dev *dev)
2670{
2671 __pci_set_master(dev, false);
2672}
2673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674/**
Matthew Wilcoxedb2d972006-10-10 08:01:21 -06002675 * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
2676 * @dev: the PCI device for which MWI is to be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 *
Matthew Wilcoxedb2d972006-10-10 08:01:21 -06002678 * Helper function for pci_set_mwi.
2679 * Originally copied from drivers/net/acenic.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
2681 *
2682 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2683 */
Tejun Heo15ea76d2009-09-22 17:34:48 +09002684int pci_set_cacheline_size(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
2686 u8 cacheline_size;
2687
2688 if (!pci_cache_line_size)
Tejun Heo15ea76d2009-09-22 17:34:48 +09002689 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691 /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
2692 equal to or multiple of the right value. */
2693 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
2694 if (cacheline_size >= pci_cache_line_size &&
2695 (cacheline_size % pci_cache_line_size) == 0)
2696 return 0;
2697
2698 /* Write the correct value. */
2699 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
2700 /* Read it back. */
2701 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
2702 if (cacheline_size == pci_cache_line_size)
2703 return 0;
2704
Bjorn Helgaas80ccba12008-06-13 10:52:11 -06002705 dev_printk(KERN_DEBUG, &dev->dev, "cache line size of %d is not "
2706 "supported\n", pci_cache_line_size << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 return -EINVAL;
2709}
Tejun Heo15ea76d2009-09-22 17:34:48 +09002710EXPORT_SYMBOL_GPL(pci_set_cacheline_size);
2711
2712#ifdef PCI_DISABLE_MWI
2713int pci_set_mwi(struct pci_dev *dev)
2714{
2715 return 0;
2716}
2717
2718int pci_try_set_mwi(struct pci_dev *dev)
2719{
2720 return 0;
2721}
2722
2723void pci_clear_mwi(struct pci_dev *dev)
2724{
2725}
2726
2727#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729/**
2730 * pci_set_mwi - enables memory-write-invalidate PCI transaction
2731 * @dev: the PCI device for which MWI is enabled
2732 *
Randy Dunlap694625c2007-07-09 11:55:54 -07002733 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 *
2735 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2736 */
2737int
2738pci_set_mwi(struct pci_dev *dev)
2739{
2740 int rc;
2741 u16 cmd;
2742
Matthew Wilcoxedb2d972006-10-10 08:01:21 -06002743 rc = pci_set_cacheline_size(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 if (rc)
2745 return rc;
2746
2747 pci_read_config_word(dev, PCI_COMMAND, &cmd);
2748 if (! (cmd & PCI_COMMAND_INVALIDATE)) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -06002749 dev_dbg(&dev->dev, "enabling Mem-Wr-Inval\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 cmd |= PCI_COMMAND_INVALIDATE;
2751 pci_write_config_word(dev, PCI_COMMAND, cmd);
2752 }
2753
2754 return 0;
2755}
2756
2757/**
Randy Dunlap694625c2007-07-09 11:55:54 -07002758 * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
2759 * @dev: the PCI device for which MWI is enabled
2760 *
2761 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
2762 * Callers are not required to check the return value.
2763 *
2764 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2765 */
2766int pci_try_set_mwi(struct pci_dev *dev)
2767{
2768 int rc = pci_set_mwi(dev);
2769 return rc;
2770}
2771
2772/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
2774 * @dev: the PCI device to disable
2775 *
2776 * Disables PCI Memory-Write-Invalidate transaction on the device
2777 */
2778void
2779pci_clear_mwi(struct pci_dev *dev)
2780{
2781 u16 cmd;
2782
2783 pci_read_config_word(dev, PCI_COMMAND, &cmd);
2784 if (cmd & PCI_COMMAND_INVALIDATE) {
2785 cmd &= ~PCI_COMMAND_INVALIDATE;
2786 pci_write_config_word(dev, PCI_COMMAND, cmd);
2787 }
2788}
Matthew Wilcoxedb2d972006-10-10 08:01:21 -06002789#endif /* ! PCI_DISABLE_MWI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Brett M Russa04ce0f2005-08-15 15:23:41 -04002791/**
2792 * pci_intx - enables/disables PCI INTx for device dev
Randy Dunlap8f7020d2005-10-23 11:57:38 -07002793 * @pdev: the PCI device to operate on
2794 * @enable: boolean: whether to enable or disable PCI INTx
Brett M Russa04ce0f2005-08-15 15:23:41 -04002795 *
2796 * Enables/disables PCI INTx for device dev
2797 */
2798void
2799pci_intx(struct pci_dev *pdev, int enable)
2800{
2801 u16 pci_command, new;
2802
2803 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
2804
2805 if (enable) {
2806 new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
2807 } else {
2808 new = pci_command | PCI_COMMAND_INTX_DISABLE;
2809 }
2810
2811 if (new != pci_command) {
Tejun Heo9ac78492007-01-20 16:00:26 +09002812 struct pci_devres *dr;
2813
Brett M Russ2fd9d742005-09-09 10:02:22 -07002814 pci_write_config_word(pdev, PCI_COMMAND, new);
Tejun Heo9ac78492007-01-20 16:00:26 +09002815
2816 dr = find_pci_dr(pdev);
2817 if (dr && !dr->restore_intx) {
2818 dr->restore_intx = 1;
2819 dr->orig_intx = !enable;
2820 }
Brett M Russa04ce0f2005-08-15 15:23:41 -04002821 }
2822}
2823
Eric W. Biedermanf5f2b132007-03-05 00:30:07 -08002824/**
Jan Kiszkaa2e27782011-11-04 09:46:00 +01002825 * pci_intx_mask_supported - probe for INTx masking support
Randy Dunlap6e9292c2012-01-21 11:02:35 -08002826 * @dev: the PCI device to operate on
Jan Kiszkaa2e27782011-11-04 09:46:00 +01002827 *
2828 * Check if the device dev support INTx masking via the config space
2829 * command word.
2830 */
2831bool pci_intx_mask_supported(struct pci_dev *dev)
2832{
2833 bool mask_supported = false;
2834 u16 orig, new;
2835
2836 pci_cfg_access_lock(dev);
2837
2838 pci_read_config_word(dev, PCI_COMMAND, &orig);
2839 pci_write_config_word(dev, PCI_COMMAND,
2840 orig ^ PCI_COMMAND_INTX_DISABLE);
2841 pci_read_config_word(dev, PCI_COMMAND, &new);
2842
2843 /*
2844 * There's no way to protect against hardware bugs or detect them
2845 * reliably, but as long as we know what the value should be, let's
2846 * go ahead and check it.
2847 */
2848 if ((new ^ orig) & ~PCI_COMMAND_INTX_DISABLE) {
2849 dev_err(&dev->dev, "Command register changed from "
2850 "0x%x to 0x%x: driver or hardware bug?\n", orig, new);
2851 } else if ((new ^ orig) & PCI_COMMAND_INTX_DISABLE) {
2852 mask_supported = true;
2853 pci_write_config_word(dev, PCI_COMMAND, orig);
2854 }
2855
2856 pci_cfg_access_unlock(dev);
2857 return mask_supported;
2858}
2859EXPORT_SYMBOL_GPL(pci_intx_mask_supported);
2860
2861static bool pci_check_and_set_intx_mask(struct pci_dev *dev, bool mask)
2862{
2863 struct pci_bus *bus = dev->bus;
2864 bool mask_updated = true;
2865 u32 cmd_status_dword;
2866 u16 origcmd, newcmd;
2867 unsigned long flags;
2868 bool irq_pending;
2869
2870 /*
2871 * We do a single dword read to retrieve both command and status.
2872 * Document assumptions that make this possible.
2873 */
2874 BUILD_BUG_ON(PCI_COMMAND % 4);
2875 BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
2876
2877 raw_spin_lock_irqsave(&pci_lock, flags);
2878
2879 bus->ops->read(bus, dev->devfn, PCI_COMMAND, 4, &cmd_status_dword);
2880
2881 irq_pending = (cmd_status_dword >> 16) & PCI_STATUS_INTERRUPT;
2882
2883 /*
2884 * Check interrupt status register to see whether our device
2885 * triggered the interrupt (when masking) or the next IRQ is
2886 * already pending (when unmasking).
2887 */
2888 if (mask != irq_pending) {
2889 mask_updated = false;
2890 goto done;
2891 }
2892
2893 origcmd = cmd_status_dword;
2894 newcmd = origcmd & ~PCI_COMMAND_INTX_DISABLE;
2895 if (mask)
2896 newcmd |= PCI_COMMAND_INTX_DISABLE;
2897 if (newcmd != origcmd)
2898 bus->ops->write(bus, dev->devfn, PCI_COMMAND, 2, newcmd);
2899
2900done:
2901 raw_spin_unlock_irqrestore(&pci_lock, flags);
2902
2903 return mask_updated;
2904}
2905
2906/**
2907 * pci_check_and_mask_intx - mask INTx on pending interrupt
Randy Dunlap6e9292c2012-01-21 11:02:35 -08002908 * @dev: the PCI device to operate on
Jan Kiszkaa2e27782011-11-04 09:46:00 +01002909 *
2910 * Check if the device dev has its INTx line asserted, mask it and
2911 * return true in that case. False is returned if not interrupt was
2912 * pending.
2913 */
2914bool pci_check_and_mask_intx(struct pci_dev *dev)
2915{
2916 return pci_check_and_set_intx_mask(dev, true);
2917}
2918EXPORT_SYMBOL_GPL(pci_check_and_mask_intx);
2919
2920/**
2921 * pci_check_and_mask_intx - unmask INTx of no interrupt is pending
Randy Dunlap6e9292c2012-01-21 11:02:35 -08002922 * @dev: the PCI device to operate on
Jan Kiszkaa2e27782011-11-04 09:46:00 +01002923 *
2924 * Check if the device dev has its INTx line asserted, unmask it if not
2925 * and return true. False is returned and the mask remains active if
2926 * there was still an interrupt pending.
2927 */
2928bool pci_check_and_unmask_intx(struct pci_dev *dev)
2929{
2930 return pci_check_and_set_intx_mask(dev, false);
2931}
2932EXPORT_SYMBOL_GPL(pci_check_and_unmask_intx);
2933
2934/**
Eric W. Biedermanf5f2b132007-03-05 00:30:07 -08002935 * pci_msi_off - disables any msi or msix capabilities
Randy Dunlap8d7d86e2007-03-16 19:55:52 -07002936 * @dev: the PCI device to operate on
Eric W. Biedermanf5f2b132007-03-05 00:30:07 -08002937 *
2938 * If you want to use msi see pci_enable_msi and friends.
2939 * This is a lower level primitive that allows us to disable
2940 * msi operation at the device level.
2941 */
2942void pci_msi_off(struct pci_dev *dev)
2943{
2944 int pos;
2945 u16 control;
2946
2947 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
2948 if (pos) {
2949 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
2950 control &= ~PCI_MSI_FLAGS_ENABLE;
2951 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
2952 }
2953 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
2954 if (pos) {
2955 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
2956 control &= ~PCI_MSIX_FLAGS_ENABLE;
2957 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
2958 }
2959}
Michael S. Tsirkinb03214d2010-06-23 22:49:06 -06002960EXPORT_SYMBOL_GPL(pci_msi_off);
Eric W. Biedermanf5f2b132007-03-05 00:30:07 -08002961
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08002962int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size)
2963{
2964 return dma_set_max_seg_size(&dev->dev, size);
2965}
2966EXPORT_SYMBOL(pci_set_dma_max_seg_size);
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08002967
FUJITA Tomonori59fc67d2008-02-04 22:28:14 -08002968int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask)
2969{
2970 return dma_set_seg_boundary(&dev->dev, mask);
2971}
2972EXPORT_SYMBOL(pci_set_dma_seg_boundary);
FUJITA Tomonori59fc67d2008-02-04 22:28:14 -08002973
Yu Zhao8c1c6992009-06-13 15:52:13 +08002974static int pcie_flr(struct pci_dev *dev, int probe)
Sheng Yang8dd7f802008-10-21 17:38:25 +08002975{
Yu Zhao8c1c6992009-06-13 15:52:13 +08002976 int i;
2977 int pos;
Sheng Yang8dd7f802008-10-21 17:38:25 +08002978 u32 cap;
Shmulik Ravid04b55c42009-12-03 22:27:51 +02002979 u16 status, control;
Sheng Yang8dd7f802008-10-21 17:38:25 +08002980
Kenji Kaneshige06a1cba2009-11-11 14:30:56 +09002981 pos = pci_pcie_cap(dev);
Yu Zhao8c1c6992009-06-13 15:52:13 +08002982 if (!pos)
Sheng Yang8dd7f802008-10-21 17:38:25 +08002983 return -ENOTTY;
Yu Zhao8c1c6992009-06-13 15:52:13 +08002984
2985 pci_read_config_dword(dev, pos + PCI_EXP_DEVCAP, &cap);
Sheng Yang8dd7f802008-10-21 17:38:25 +08002986 if (!(cap & PCI_EXP_DEVCAP_FLR))
2987 return -ENOTTY;
2988
Sheng Yangd91cdc72008-11-11 17:17:47 +08002989 if (probe)
2990 return 0;
2991
Sheng Yang8dd7f802008-10-21 17:38:25 +08002992 /* Wait for Transaction Pending bit clean */
Yu Zhao8c1c6992009-06-13 15:52:13 +08002993 for (i = 0; i < 4; i++) {
2994 if (i)
2995 msleep((1 << (i - 1)) * 100);
Sheng Yang5fe5db02009-02-09 14:53:47 +08002996
Yu Zhao8c1c6992009-06-13 15:52:13 +08002997 pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &status);
2998 if (!(status & PCI_EXP_DEVSTA_TRPND))
2999 goto clear;
3000 }
Sheng Yang8dd7f802008-10-21 17:38:25 +08003001
Yu Zhao8c1c6992009-06-13 15:52:13 +08003002 dev_err(&dev->dev, "transaction is not cleared; "
3003 "proceeding with reset anyway\n");
Sheng Yang5fe5db02009-02-09 14:53:47 +08003004
Yu Zhao8c1c6992009-06-13 15:52:13 +08003005clear:
Shmulik Ravid04b55c42009-12-03 22:27:51 +02003006 pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &control);
3007 control |= PCI_EXP_DEVCTL_BCR_FLR;
3008 pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, control);
3009
Yu Zhao8c1c6992009-06-13 15:52:13 +08003010 msleep(100);
Sheng Yang8dd7f802008-10-21 17:38:25 +08003011
Sheng Yang8dd7f802008-10-21 17:38:25 +08003012 return 0;
3013}
Sheng Yangd91cdc72008-11-11 17:17:47 +08003014
Yu Zhao8c1c6992009-06-13 15:52:13 +08003015static int pci_af_flr(struct pci_dev *dev, int probe)
Sheng Yang1ca88792008-11-11 17:17:48 +08003016{
Yu Zhao8c1c6992009-06-13 15:52:13 +08003017 int i;
3018 int pos;
Sheng Yang1ca88792008-11-11 17:17:48 +08003019 u8 cap;
Yu Zhao8c1c6992009-06-13 15:52:13 +08003020 u8 status;
Sheng Yang1ca88792008-11-11 17:17:48 +08003021
Yu Zhao8c1c6992009-06-13 15:52:13 +08003022 pos = pci_find_capability(dev, PCI_CAP_ID_AF);
3023 if (!pos)
Sheng Yang1ca88792008-11-11 17:17:48 +08003024 return -ENOTTY;
Yu Zhao8c1c6992009-06-13 15:52:13 +08003025
3026 pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap);
Sheng Yang1ca88792008-11-11 17:17:48 +08003027 if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
3028 return -ENOTTY;
3029
3030 if (probe)
3031 return 0;
3032
Sheng Yang1ca88792008-11-11 17:17:48 +08003033 /* Wait for Transaction Pending bit clean */
Yu Zhao8c1c6992009-06-13 15:52:13 +08003034 for (i = 0; i < 4; i++) {
3035 if (i)
3036 msleep((1 << (i - 1)) * 100);
Sheng Yang5fe5db02009-02-09 14:53:47 +08003037
Yu Zhao8c1c6992009-06-13 15:52:13 +08003038 pci_read_config_byte(dev, pos + PCI_AF_STATUS, &status);
3039 if (!(status & PCI_AF_STATUS_TP))
3040 goto clear;
3041 }
3042
3043 dev_err(&dev->dev, "transaction is not cleared; "
3044 "proceeding with reset anyway\n");
3045
3046clear:
3047 pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
Sheng Yang1ca88792008-11-11 17:17:48 +08003048 msleep(100);
Sheng Yang5fe5db02009-02-09 14:53:47 +08003049
Sheng Yang1ca88792008-11-11 17:17:48 +08003050 return 0;
3051}
3052
Rafael J. Wysocki83d74e02011-03-05 21:48:44 +01003053/**
3054 * pci_pm_reset - Put device into PCI_D3 and back into PCI_D0.
3055 * @dev: Device to reset.
3056 * @probe: If set, only check if the device can be reset this way.
3057 *
3058 * If @dev supports native PCI PM and its PCI_PM_CTRL_NO_SOFT_RESET flag is
3059 * unset, it will be reinitialized internally when going from PCI_D3hot to
3060 * PCI_D0. If that's the case and the device is not in a low-power state
3061 * already, force it into PCI_D3hot and back to PCI_D0, causing it to be reset.
3062 *
3063 * NOTE: This causes the caller to sleep for twice the device power transition
3064 * cooldown period, which for the D0->D3hot and D3hot->D0 transitions is 10 ms
3065 * by devault (i.e. unless the @dev's d3_delay field has a different value).
3066 * Moreover, only devices in D0 can be reset by this function.
3067 */
Yu Zhaof85876b2009-06-13 15:52:14 +08003068static int pci_pm_reset(struct pci_dev *dev, int probe)
Sheng Yangd91cdc72008-11-11 17:17:47 +08003069{
Yu Zhaof85876b2009-06-13 15:52:14 +08003070 u16 csr;
Sheng Yangd91cdc72008-11-11 17:17:47 +08003071
Yu Zhaof85876b2009-06-13 15:52:14 +08003072 if (!dev->pm_cap)
3073 return -ENOTTY;
Sheng Yangd91cdc72008-11-11 17:17:47 +08003074
Yu Zhaof85876b2009-06-13 15:52:14 +08003075 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr);
3076 if (csr & PCI_PM_CTRL_NO_SOFT_RESET)
3077 return -ENOTTY;
Sheng Yang1ca88792008-11-11 17:17:48 +08003078
Yu Zhaof85876b2009-06-13 15:52:14 +08003079 if (probe)
3080 return 0;
3081
3082 if (dev->current_state != PCI_D0)
3083 return -EINVAL;
3084
3085 csr &= ~PCI_PM_CTRL_STATE_MASK;
3086 csr |= PCI_D3hot;
3087 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +01003088 pci_dev_d3_sleep(dev);
Yu Zhaof85876b2009-06-13 15:52:14 +08003089
3090 csr &= ~PCI_PM_CTRL_STATE_MASK;
3091 csr |= PCI_D0;
3092 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
Rafael J. Wysocki1ae861e2009-12-31 12:15:54 +01003093 pci_dev_d3_sleep(dev);
Yu Zhaof85876b2009-06-13 15:52:14 +08003094
3095 return 0;
3096}
3097
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08003098static int pci_parent_bus_reset(struct pci_dev *dev, int probe)
3099{
3100 u16 ctrl;
3101 struct pci_dev *pdev;
3102
Yu Zhao654b75e2009-06-26 14:04:46 +08003103 if (pci_is_root_bus(dev->bus) || dev->subordinate || !dev->bus->self)
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08003104 return -ENOTTY;
3105
3106 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
3107 if (pdev != dev)
3108 return -ENOTTY;
3109
3110 if (probe)
3111 return 0;
3112
3113 pci_read_config_word(dev->bus->self, PCI_BRIDGE_CONTROL, &ctrl);
3114 ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
3115 pci_write_config_word(dev->bus->self, PCI_BRIDGE_CONTROL, ctrl);
3116 msleep(100);
3117
3118 ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
3119 pci_write_config_word(dev->bus->self, PCI_BRIDGE_CONTROL, ctrl);
3120 msleep(100);
3121
3122 return 0;
3123}
3124
Konrad Rzeszutek Wilk977f8572012-04-24 13:15:18 -06003125static int __pci_dev_reset(struct pci_dev *dev, int probe)
Sheng Yang8dd7f802008-10-21 17:38:25 +08003126{
Yu Zhao8c1c6992009-06-13 15:52:13 +08003127 int rc;
Sheng Yang8dd7f802008-10-21 17:38:25 +08003128
Yu Zhao8c1c6992009-06-13 15:52:13 +08003129 might_sleep();
Sheng Yang8dd7f802008-10-21 17:38:25 +08003130
Dexuan Cuib9c3b262009-12-07 13:03:21 +08003131 rc = pci_dev_specific_reset(dev, probe);
3132 if (rc != -ENOTTY)
3133 goto done;
3134
Yu Zhao8c1c6992009-06-13 15:52:13 +08003135 rc = pcie_flr(dev, probe);
3136 if (rc != -ENOTTY)
3137 goto done;
3138
3139 rc = pci_af_flr(dev, probe);
Yu Zhaof85876b2009-06-13 15:52:14 +08003140 if (rc != -ENOTTY)
3141 goto done;
3142
3143 rc = pci_pm_reset(dev, probe);
Yu Zhaoc12ff1d2009-06-13 15:52:15 +08003144 if (rc != -ENOTTY)
3145 goto done;
3146
3147 rc = pci_parent_bus_reset(dev, probe);
Yu Zhao8c1c6992009-06-13 15:52:13 +08003148done:
Konrad Rzeszutek Wilk977f8572012-04-24 13:15:18 -06003149 return rc;
3150}
3151
3152static int pci_dev_reset(struct pci_dev *dev, int probe)
3153{
3154 int rc;
3155
3156 if (!probe) {
3157 pci_cfg_access_lock(dev);
3158 /* block PM suspend, driver probe, etc. */
3159 device_lock(&dev->dev);
3160 }
3161
3162 rc = __pci_dev_reset(dev, probe);
3163
Yu Zhao8c1c6992009-06-13 15:52:13 +08003164 if (!probe) {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08003165 device_unlock(&dev->dev);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +01003166 pci_cfg_access_unlock(dev);
Yu Zhao8c1c6992009-06-13 15:52:13 +08003167 }
Yu Zhao8c1c6992009-06-13 15:52:13 +08003168 return rc;
Sheng Yang8dd7f802008-10-21 17:38:25 +08003169}
Sheng Yang8dd7f802008-10-21 17:38:25 +08003170/**
Yu Zhao8c1c6992009-06-13 15:52:13 +08003171 * __pci_reset_function - reset a PCI device function
3172 * @dev: PCI device to reset
Sheng Yang8dd7f802008-10-21 17:38:25 +08003173 *
3174 * Some devices allow an individual function to be reset without affecting
3175 * other functions in the same device. The PCI device must be responsive
3176 * to PCI config space in order to use this function.
3177 *
3178 * The device function is presumed to be unused when this function is called.
3179 * Resetting the device will make the contents of PCI configuration space
3180 * random, so any caller of this must be prepared to reinitialise the
3181 * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
3182 * etc.
3183 *
Yu Zhao8c1c6992009-06-13 15:52:13 +08003184 * Returns 0 if the device function was successfully reset or negative if the
Sheng Yang8dd7f802008-10-21 17:38:25 +08003185 * device doesn't support resetting a single function.
3186 */
Yu Zhao8c1c6992009-06-13 15:52:13 +08003187int __pci_reset_function(struct pci_dev *dev)
Sheng Yang8dd7f802008-10-21 17:38:25 +08003188{
Yu Zhao8c1c6992009-06-13 15:52:13 +08003189 return pci_dev_reset(dev, 0);
Sheng Yang8dd7f802008-10-21 17:38:25 +08003190}
Yu Zhao8c1c6992009-06-13 15:52:13 +08003191EXPORT_SYMBOL_GPL(__pci_reset_function);
Sheng Yang8dd7f802008-10-21 17:38:25 +08003192
3193/**
Konrad Rzeszutek Wilk6fbf9e72012-01-12 12:06:46 -05003194 * __pci_reset_function_locked - reset a PCI device function while holding
3195 * the @dev mutex lock.
3196 * @dev: PCI device to reset
3197 *
3198 * Some devices allow an individual function to be reset without affecting
3199 * other functions in the same device. The PCI device must be responsive
3200 * to PCI config space in order to use this function.
3201 *
3202 * The device function is presumed to be unused and the caller is holding
3203 * the device mutex lock when this function is called.
3204 * Resetting the device will make the contents of PCI configuration space
3205 * random, so any caller of this must be prepared to reinitialise the
3206 * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
3207 * etc.
3208 *
3209 * Returns 0 if the device function was successfully reset or negative if the
3210 * device doesn't support resetting a single function.
3211 */
3212int __pci_reset_function_locked(struct pci_dev *dev)
3213{
Konrad Rzeszutek Wilk977f8572012-04-24 13:15:18 -06003214 return __pci_dev_reset(dev, 0);
Konrad Rzeszutek Wilk6fbf9e72012-01-12 12:06:46 -05003215}
3216EXPORT_SYMBOL_GPL(__pci_reset_function_locked);
3217
3218/**
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03003219 * pci_probe_reset_function - check whether the device can be safely reset
3220 * @dev: PCI device to reset
3221 *
3222 * Some devices allow an individual function to be reset without affecting
3223 * other functions in the same device. The PCI device must be responsive
3224 * to PCI config space in order to use this function.
3225 *
3226 * Returns 0 if the device function can be reset or negative if the
3227 * device doesn't support resetting a single function.
3228 */
3229int pci_probe_reset_function(struct pci_dev *dev)
3230{
3231 return pci_dev_reset(dev, 1);
3232}
3233
3234/**
Yu Zhao8c1c6992009-06-13 15:52:13 +08003235 * pci_reset_function - quiesce and reset a PCI device function
3236 * @dev: PCI device to reset
Sheng Yang8dd7f802008-10-21 17:38:25 +08003237 *
3238 * Some devices allow an individual function to be reset without affecting
3239 * other functions in the same device. The PCI device must be responsive
3240 * to PCI config space in order to use this function.
3241 *
3242 * This function does not just reset the PCI portion of a device, but
3243 * clears all the state associated with the device. This function differs
Yu Zhao8c1c6992009-06-13 15:52:13 +08003244 * from __pci_reset_function in that it saves and restores device state
Sheng Yang8dd7f802008-10-21 17:38:25 +08003245 * over the reset.
3246 *
Yu Zhao8c1c6992009-06-13 15:52:13 +08003247 * Returns 0 if the device function was successfully reset or negative if the
Sheng Yang8dd7f802008-10-21 17:38:25 +08003248 * device doesn't support resetting a single function.
3249 */
3250int pci_reset_function(struct pci_dev *dev)
3251{
Yu Zhao8c1c6992009-06-13 15:52:13 +08003252 int rc;
Sheng Yang8dd7f802008-10-21 17:38:25 +08003253
Yu Zhao8c1c6992009-06-13 15:52:13 +08003254 rc = pci_dev_reset(dev, 1);
3255 if (rc)
3256 return rc;
Sheng Yang8dd7f802008-10-21 17:38:25 +08003257
Sheng Yang8dd7f802008-10-21 17:38:25 +08003258 pci_save_state(dev);
3259
Yu Zhao8c1c6992009-06-13 15:52:13 +08003260 /*
3261 * both INTx and MSI are disabled after the Interrupt Disable bit
3262 * is set and the Bus Master bit is cleared.
3263 */
Sheng Yang8dd7f802008-10-21 17:38:25 +08003264 pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
3265
Yu Zhao8c1c6992009-06-13 15:52:13 +08003266 rc = pci_dev_reset(dev, 0);
Sheng Yang8dd7f802008-10-21 17:38:25 +08003267
3268 pci_restore_state(dev);
Sheng Yang8dd7f802008-10-21 17:38:25 +08003269
Yu Zhao8c1c6992009-06-13 15:52:13 +08003270 return rc;
Sheng Yang8dd7f802008-10-21 17:38:25 +08003271}
3272EXPORT_SYMBOL_GPL(pci_reset_function);
3273
3274/**
Peter Orubad556ad42007-05-15 13:59:13 +02003275 * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
3276 * @dev: PCI device to query
3277 *
3278 * Returns mmrbc: maximum designed memory read count in bytes
3279 * or appropriate error value.
3280 */
3281int pcix_get_max_mmrbc(struct pci_dev *dev)
3282{
Dean Nelson7c9e2b12010-03-09 22:26:55 -05003283 int cap;
Peter Orubad556ad42007-05-15 13:59:13 +02003284 u32 stat;
3285
3286 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
3287 if (!cap)
3288 return -EINVAL;
3289
Dean Nelson7c9e2b12010-03-09 22:26:55 -05003290 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
Peter Orubad556ad42007-05-15 13:59:13 +02003291 return -EINVAL;
3292
Dean Nelson25daeb52010-03-09 22:26:40 -05003293 return 512 << ((stat & PCI_X_STATUS_MAX_READ) >> 21);
Peter Orubad556ad42007-05-15 13:59:13 +02003294}
3295EXPORT_SYMBOL(pcix_get_max_mmrbc);
3296
3297/**
3298 * pcix_get_mmrbc - get PCI-X maximum memory read byte count
3299 * @dev: PCI device to query
3300 *
3301 * Returns mmrbc: maximum memory read count in bytes
3302 * or appropriate error value.
3303 */
3304int pcix_get_mmrbc(struct pci_dev *dev)
3305{
Dean Nelson7c9e2b12010-03-09 22:26:55 -05003306 int cap;
Dean Nelsonbdc2bda2010-03-09 22:26:48 -05003307 u16 cmd;
Peter Orubad556ad42007-05-15 13:59:13 +02003308
3309 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
3310 if (!cap)
3311 return -EINVAL;
3312
Dean Nelson7c9e2b12010-03-09 22:26:55 -05003313 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
3314 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02003315
Dean Nelson7c9e2b12010-03-09 22:26:55 -05003316 return 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
Peter Orubad556ad42007-05-15 13:59:13 +02003317}
3318EXPORT_SYMBOL(pcix_get_mmrbc);
3319
3320/**
3321 * pcix_set_mmrbc - set PCI-X maximum memory read byte count
3322 * @dev: PCI device to query
3323 * @mmrbc: maximum memory read count in bytes
3324 * valid values are 512, 1024, 2048, 4096
3325 *
3326 * If possible sets maximum memory read byte count, some bridges have erratas
3327 * that prevent this.
3328 */
3329int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
3330{
Dean Nelson7c9e2b12010-03-09 22:26:55 -05003331 int cap;
Dean Nelsonbdc2bda2010-03-09 22:26:48 -05003332 u32 stat, v, o;
3333 u16 cmd;
Peter Orubad556ad42007-05-15 13:59:13 +02003334
vignesh babu229f5af2007-08-13 18:23:14 +05303335 if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
Dean Nelson7c9e2b12010-03-09 22:26:55 -05003336 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02003337
3338 v = ffs(mmrbc) - 10;
3339
3340 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
3341 if (!cap)
Dean Nelson7c9e2b12010-03-09 22:26:55 -05003342 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02003343
Dean Nelson7c9e2b12010-03-09 22:26:55 -05003344 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
3345 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02003346
3347 if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
3348 return -E2BIG;
3349
Dean Nelson7c9e2b12010-03-09 22:26:55 -05003350 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
3351 return -EINVAL;
Peter Orubad556ad42007-05-15 13:59:13 +02003352
3353 o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
3354 if (o != v) {
3355 if (v > o && dev->bus &&
3356 (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
3357 return -EIO;
3358
3359 cmd &= ~PCI_X_CMD_MAX_READ;
3360 cmd |= v << 2;
Dean Nelson7c9e2b12010-03-09 22:26:55 -05003361 if (pci_write_config_word(dev, cap + PCI_X_CMD, cmd))
3362 return -EIO;
Peter Orubad556ad42007-05-15 13:59:13 +02003363 }
Dean Nelson7c9e2b12010-03-09 22:26:55 -05003364 return 0;
Peter Orubad556ad42007-05-15 13:59:13 +02003365}
3366EXPORT_SYMBOL(pcix_set_mmrbc);
3367
3368/**
3369 * pcie_get_readrq - get PCI Express read request size
3370 * @dev: PCI device to query
3371 *
3372 * Returns maximum memory read request in bytes
3373 * or appropriate error value.
3374 */
3375int pcie_get_readrq(struct pci_dev *dev)
3376{
3377 int ret, cap;
3378 u16 ctl;
3379
Kenji Kaneshige06a1cba2009-11-11 14:30:56 +09003380 cap = pci_pcie_cap(dev);
Peter Orubad556ad42007-05-15 13:59:13 +02003381 if (!cap)
3382 return -EINVAL;
3383
3384 ret = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
3385 if (!ret)
Julia Lawall93e75fa2010-08-05 22:23:16 +02003386 ret = 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
Peter Orubad556ad42007-05-15 13:59:13 +02003387
3388 return ret;
3389}
3390EXPORT_SYMBOL(pcie_get_readrq);
3391
3392/**
3393 * pcie_set_readrq - set PCI Express maximum memory read request
3394 * @dev: PCI device to query
Randy Dunlap42e61f42007-07-23 21:42:11 -07003395 * @rq: maximum memory read count in bytes
Peter Orubad556ad42007-05-15 13:59:13 +02003396 * valid values are 128, 256, 512, 1024, 2048, 4096
3397 *
Jon Masonc9b378c2011-06-28 18:26:25 -05003398 * If possible sets maximum memory read request in bytes
Peter Orubad556ad42007-05-15 13:59:13 +02003399 */
3400int pcie_set_readrq(struct pci_dev *dev, int rq)
3401{
3402 int cap, err = -EINVAL;
3403 u16 ctl, v;
3404
vignesh babu229f5af2007-08-13 18:23:14 +05303405 if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
Peter Orubad556ad42007-05-15 13:59:13 +02003406 goto out;
3407
Kenji Kaneshige06a1cba2009-11-11 14:30:56 +09003408 cap = pci_pcie_cap(dev);
Peter Orubad556ad42007-05-15 13:59:13 +02003409 if (!cap)
3410 goto out;
3411
3412 err = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
3413 if (err)
3414 goto out;
Benjamin Herrenschmidta1c473a2011-10-14 14:56:15 -05003415 /*
3416 * If using the "performance" PCIe config, we clamp the
3417 * read rq size to the max packet size to prevent the
3418 * host bridge generating requests larger than we can
3419 * cope with
3420 */
3421 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
3422 int mps = pcie_get_mps(dev);
3423
3424 if (mps < 0)
3425 return mps;
3426 if (mps < rq)
3427 rq = mps;
3428 }
3429
3430 v = (ffs(rq) - 8) << 12;
Peter Orubad556ad42007-05-15 13:59:13 +02003431
3432 if ((ctl & PCI_EXP_DEVCTL_READRQ) != v) {
3433 ctl &= ~PCI_EXP_DEVCTL_READRQ;
3434 ctl |= v;
Jon Masonc9b378c2011-06-28 18:26:25 -05003435 err = pci_write_config_word(dev, cap + PCI_EXP_DEVCTL, ctl);
Peter Orubad556ad42007-05-15 13:59:13 +02003436 }
3437
3438out:
3439 return err;
3440}
3441EXPORT_SYMBOL(pcie_set_readrq);
3442
3443/**
Jon Masonb03e7492011-07-20 15:20:54 -05003444 * pcie_get_mps - get PCI Express maximum payload size
3445 * @dev: PCI device to query
3446 *
3447 * Returns maximum payload size in bytes
3448 * or appropriate error value.
3449 */
3450int pcie_get_mps(struct pci_dev *dev)
3451{
3452 int ret, cap;
3453 u16 ctl;
3454
3455 cap = pci_pcie_cap(dev);
3456 if (!cap)
3457 return -EINVAL;
3458
3459 ret = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
3460 if (!ret)
3461 ret = 128 << ((ctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
3462
3463 return ret;
3464}
3465
3466/**
3467 * pcie_set_mps - set PCI Express maximum payload size
3468 * @dev: PCI device to query
Randy Dunlap47c08f32011-08-20 11:49:43 -07003469 * @mps: maximum payload size in bytes
Jon Masonb03e7492011-07-20 15:20:54 -05003470 * valid values are 128, 256, 512, 1024, 2048, 4096
3471 *
3472 * If possible sets maximum payload size
3473 */
3474int pcie_set_mps(struct pci_dev *dev, int mps)
3475{
3476 int cap, err = -EINVAL;
3477 u16 ctl, v;
3478
3479 if (mps < 128 || mps > 4096 || !is_power_of_2(mps))
3480 goto out;
3481
3482 v = ffs(mps) - 8;
3483 if (v > dev->pcie_mpss)
3484 goto out;
3485 v <<= 5;
3486
3487 cap = pci_pcie_cap(dev);
3488 if (!cap)
3489 goto out;
3490
3491 err = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
3492 if (err)
3493 goto out;
3494
3495 if ((ctl & PCI_EXP_DEVCTL_PAYLOAD) != v) {
3496 ctl &= ~PCI_EXP_DEVCTL_PAYLOAD;
3497 ctl |= v;
3498 err = pci_write_config_word(dev, cap + PCI_EXP_DEVCTL, ctl);
3499 }
3500out:
3501 return err;
3502}
3503
3504/**
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003505 * pci_select_bars - Make BAR mask from the type of resource
Randy Dunlapf95d8822007-02-10 14:41:56 -08003506 * @dev: the PCI device for which BAR mask is made
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003507 * @flags: resource type mask to be selected
3508 *
3509 * This helper routine makes bar mask from the type of resource.
3510 */
3511int pci_select_bars(struct pci_dev *dev, unsigned long flags)
3512{
3513 int i, bars = 0;
3514 for (i = 0; i < PCI_NUM_RESOURCES; i++)
3515 if (pci_resource_flags(dev, i) & flags)
3516 bars |= (1 << i);
3517 return bars;
3518}
3519
Yu Zhao613e7ed2008-11-22 02:41:27 +08003520/**
3521 * pci_resource_bar - get position of the BAR associated with a resource
3522 * @dev: the PCI device
3523 * @resno: the resource number
3524 * @type: the BAR type to be filled in
3525 *
3526 * Returns BAR position in config space, or 0 if the BAR is invalid.
3527 */
3528int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)
3529{
Yu Zhaod1b054d2009-03-20 11:25:11 +08003530 int reg;
3531
Yu Zhao613e7ed2008-11-22 02:41:27 +08003532 if (resno < PCI_ROM_RESOURCE) {
3533 *type = pci_bar_unknown;
3534 return PCI_BASE_ADDRESS_0 + 4 * resno;
3535 } else if (resno == PCI_ROM_RESOURCE) {
3536 *type = pci_bar_mem32;
3537 return dev->rom_base_reg;
Yu Zhaod1b054d2009-03-20 11:25:11 +08003538 } else if (resno < PCI_BRIDGE_RESOURCES) {
3539 /* device specific resource */
3540 reg = pci_iov_resource_bar(dev, resno, type);
3541 if (reg)
3542 return reg;
Yu Zhao613e7ed2008-11-22 02:41:27 +08003543 }
3544
Bjorn Helgaas865df572009-11-04 10:32:57 -07003545 dev_err(&dev->dev, "BAR %d: invalid resource\n", resno);
Yu Zhao613e7ed2008-11-22 02:41:27 +08003546 return 0;
3547}
3548
Mike Travis95a8b6e2010-02-02 14:38:13 -08003549/* Some architectures require additional programming to enable VGA */
3550static arch_set_vga_state_t arch_set_vga_state;
3551
3552void __init pci_register_set_vga_state(arch_set_vga_state_t func)
3553{
3554 arch_set_vga_state = func; /* NULL disables */
3555}
3556
3557static int pci_set_vga_state_arch(struct pci_dev *dev, bool decode,
Dave Airlie7ad35cf2011-05-25 14:00:49 +10003558 unsigned int command_bits, u32 flags)
Mike Travis95a8b6e2010-02-02 14:38:13 -08003559{
3560 if (arch_set_vga_state)
3561 return arch_set_vga_state(dev, decode, command_bits,
Dave Airlie7ad35cf2011-05-25 14:00:49 +10003562 flags);
Mike Travis95a8b6e2010-02-02 14:38:13 -08003563 return 0;
3564}
3565
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10003566/**
3567 * pci_set_vga_state - set VGA decode state on device and parents if requested
Randy Dunlap19eea632009-09-17 15:28:22 -07003568 * @dev: the PCI device
3569 * @decode: true = enable decoding, false = disable decoding
3570 * @command_bits: PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY
Randy Dunlap3f37d622011-05-25 19:21:25 -07003571 * @flags: traverse ancestors and change bridges
Dave Airlie3448a192010-06-01 15:32:24 +10003572 * CHANGE_BRIDGE_ONLY / CHANGE_BRIDGE
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10003573 */
3574int pci_set_vga_state(struct pci_dev *dev, bool decode,
Dave Airlie3448a192010-06-01 15:32:24 +10003575 unsigned int command_bits, u32 flags)
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10003576{
3577 struct pci_bus *bus;
3578 struct pci_dev *bridge;
3579 u16 cmd;
Mike Travis95a8b6e2010-02-02 14:38:13 -08003580 int rc;
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10003581
Dave Airlie3448a192010-06-01 15:32:24 +10003582 WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) & (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY)));
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10003583
Mike Travis95a8b6e2010-02-02 14:38:13 -08003584 /* ARCH specific VGA enables */
Dave Airlie3448a192010-06-01 15:32:24 +10003585 rc = pci_set_vga_state_arch(dev, decode, command_bits, flags);
Mike Travis95a8b6e2010-02-02 14:38:13 -08003586 if (rc)
3587 return rc;
3588
Dave Airlie3448a192010-06-01 15:32:24 +10003589 if (flags & PCI_VGA_STATE_CHANGE_DECODES) {
3590 pci_read_config_word(dev, PCI_COMMAND, &cmd);
3591 if (decode == true)
3592 cmd |= command_bits;
3593 else
3594 cmd &= ~command_bits;
3595 pci_write_config_word(dev, PCI_COMMAND, cmd);
3596 }
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10003597
Dave Airlie3448a192010-06-01 15:32:24 +10003598 if (!(flags & PCI_VGA_STATE_CHANGE_BRIDGE))
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10003599 return 0;
3600
3601 bus = dev->bus;
3602 while (bus) {
3603 bridge = bus->self;
3604 if (bridge) {
3605 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
3606 &cmd);
3607 if (decode == true)
3608 cmd |= PCI_BRIDGE_CTL_VGA;
3609 else
3610 cmd &= ~PCI_BRIDGE_CTL_VGA;
3611 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL,
3612 cmd);
3613 }
3614 bus = bus->parent;
3615 }
3616 return 0;
3617}
3618
Yuji Shimada32a9a6822009-03-16 17:13:39 +09003619#define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZE
3620static char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE] = {0};
Thomas Gleixnere9d1e492009-11-06 22:41:23 +00003621static DEFINE_SPINLOCK(resource_alignment_lock);
Yuji Shimada32a9a6822009-03-16 17:13:39 +09003622
3623/**
3624 * pci_specified_resource_alignment - get resource alignment specified by user.
3625 * @dev: the PCI device to get
3626 *
3627 * RETURNS: Resource alignment if it is specified.
3628 * Zero if it is not specified.
3629 */
3630resource_size_t pci_specified_resource_alignment(struct pci_dev *dev)
3631{
3632 int seg, bus, slot, func, align_order, count;
3633 resource_size_t align = 0;
3634 char *p;
3635
3636 spin_lock(&resource_alignment_lock);
3637 p = resource_alignment_param;
3638 while (*p) {
3639 count = 0;
3640 if (sscanf(p, "%d%n", &align_order, &count) == 1 &&
3641 p[count] == '@') {
3642 p += count + 1;
3643 } else {
3644 align_order = -1;
3645 }
3646 if (sscanf(p, "%x:%x:%x.%x%n",
3647 &seg, &bus, &slot, &func, &count) != 4) {
3648 seg = 0;
3649 if (sscanf(p, "%x:%x.%x%n",
3650 &bus, &slot, &func, &count) != 3) {
3651 /* Invalid format */
3652 printk(KERN_ERR "PCI: Can't parse resource_alignment parameter: %s\n",
3653 p);
3654 break;
3655 }
3656 }
3657 p += count;
3658 if (seg == pci_domain_nr(dev->bus) &&
3659 bus == dev->bus->number &&
3660 slot == PCI_SLOT(dev->devfn) &&
3661 func == PCI_FUNC(dev->devfn)) {
3662 if (align_order == -1) {
3663 align = PAGE_SIZE;
3664 } else {
3665 align = 1 << align_order;
3666 }
3667 /* Found */
3668 break;
3669 }
3670 if (*p != ';' && *p != ',') {
3671 /* End of param or invalid format */
3672 break;
3673 }
3674 p++;
3675 }
3676 spin_unlock(&resource_alignment_lock);
3677 return align;
3678}
3679
3680/**
3681 * pci_is_reassigndev - check if specified PCI is target device to reassign
3682 * @dev: the PCI device to check
3683 *
3684 * RETURNS: non-zero for PCI device is a target device to reassign,
3685 * or zero is not.
3686 */
3687int pci_is_reassigndev(struct pci_dev *dev)
3688{
3689 return (pci_specified_resource_alignment(dev) != 0);
3690}
3691
Yinghai Lu2069ecf2012-02-15 21:40:31 -08003692/*
3693 * This function disables memory decoding and releases memory resources
3694 * of the device specified by kernel's boot parameter 'pci=resource_alignment='.
3695 * It also rounds up size to specified alignment.
3696 * Later on, the kernel will assign page-aligned memory resource back
3697 * to the device.
3698 */
3699void pci_reassigndev_resource_alignment(struct pci_dev *dev)
3700{
3701 int i;
3702 struct resource *r;
3703 resource_size_t align, size;
3704 u16 command;
3705
3706 if (!pci_is_reassigndev(dev))
3707 return;
3708
3709 if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL &&
3710 (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) {
3711 dev_warn(&dev->dev,
3712 "Can't reassign resources to host bridge.\n");
3713 return;
3714 }
3715
3716 dev_info(&dev->dev,
3717 "Disabling memory decoding and releasing memory resources.\n");
3718 pci_read_config_word(dev, PCI_COMMAND, &command);
3719 command &= ~PCI_COMMAND_MEMORY;
3720 pci_write_config_word(dev, PCI_COMMAND, command);
3721
3722 align = pci_specified_resource_alignment(dev);
3723 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
3724 r = &dev->resource[i];
3725 if (!(r->flags & IORESOURCE_MEM))
3726 continue;
3727 size = resource_size(r);
3728 if (size < align) {
3729 size = align;
3730 dev_info(&dev->dev,
3731 "Rounding up size of resource #%d to %#llx.\n",
3732 i, (unsigned long long)size);
3733 }
3734 r->end = size - 1;
3735 r->start = 0;
3736 }
3737 /* Need to disable bridge's resource window,
3738 * to enable the kernel to reassign new resource
3739 * window later on.
3740 */
3741 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
3742 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
3743 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
3744 r = &dev->resource[i];
3745 if (!(r->flags & IORESOURCE_MEM))
3746 continue;
3747 r->end = resource_size(r) - 1;
3748 r->start = 0;
3749 }
3750 pci_disable_bridge_window(dev);
3751 }
3752}
3753
Yuji Shimada32a9a6822009-03-16 17:13:39 +09003754ssize_t pci_set_resource_alignment_param(const char *buf, size_t count)
3755{
3756 if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1)
3757 count = RESOURCE_ALIGNMENT_PARAM_SIZE - 1;
3758 spin_lock(&resource_alignment_lock);
3759 strncpy(resource_alignment_param, buf, count);
3760 resource_alignment_param[count] = '\0';
3761 spin_unlock(&resource_alignment_lock);
3762 return count;
3763}
3764
3765ssize_t pci_get_resource_alignment_param(char *buf, size_t size)
3766{
3767 size_t count;
3768 spin_lock(&resource_alignment_lock);
3769 count = snprintf(buf, size, "%s", resource_alignment_param);
3770 spin_unlock(&resource_alignment_lock);
3771 return count;
3772}
3773
3774static ssize_t pci_resource_alignment_show(struct bus_type *bus, char *buf)
3775{
3776 return pci_get_resource_alignment_param(buf, PAGE_SIZE);
3777}
3778
3779static ssize_t pci_resource_alignment_store(struct bus_type *bus,
3780 const char *buf, size_t count)
3781{
3782 return pci_set_resource_alignment_param(buf, count);
3783}
3784
3785BUS_ATTR(resource_alignment, 0644, pci_resource_alignment_show,
3786 pci_resource_alignment_store);
3787
3788static int __init pci_resource_alignment_sysfs_init(void)
3789{
3790 return bus_create_file(&pci_bus_type,
3791 &bus_attr_resource_alignment);
3792}
3793
3794late_initcall(pci_resource_alignment_sysfs_init);
3795
Jeff Garzik32a2eea2007-10-11 16:57:27 -04003796static void __devinit pci_no_domains(void)
3797{
3798#ifdef CONFIG_PCI_DOMAINS
3799 pci_domains_supported = 0;
3800#endif
3801}
3802
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -07003803/**
3804 * pci_ext_cfg_enabled - can we access extended PCI config space?
3805 * @dev: The PCI device of the root bridge.
3806 *
3807 * Returns 1 if we can access PCI extended config space (offsets
3808 * greater than 0xff). This is the default implementation. Architecture
3809 * implementations can override this.
3810 */
3811int __attribute__ ((weak)) pci_ext_cfg_avail(struct pci_dev *dev)
3812{
3813 return 1;
3814}
3815
Benjamin Herrenschmidt2d1c8612009-12-09 17:52:13 +11003816void __weak pci_fixup_cardbus(struct pci_bus *bus)
3817{
3818}
3819EXPORT_SYMBOL(pci_fixup_cardbus);
3820
Al Viroad04d312008-11-22 17:37:14 +00003821static int __init pci_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822{
3823 while (str) {
3824 char *k = strchr(str, ',');
3825 if (k)
3826 *k++ = 0;
3827 if (*str && (str = pcibios_setup(str)) && *str) {
Matthew Wilcox309e57d2006-03-05 22:33:34 -07003828 if (!strcmp(str, "nomsi")) {
3829 pci_no_msi();
Randy Dunlap7f785762007-10-05 13:17:58 -07003830 } else if (!strcmp(str, "noaer")) {
3831 pci_no_aer();
Yinghai Lub55438f2012-02-23 19:23:30 -08003832 } else if (!strncmp(str, "realloc=", 8)) {
3833 pci_realloc_get_opt(str + 8);
Ram Paif483d392011-07-07 11:19:10 -07003834 } else if (!strncmp(str, "realloc", 7)) {
Yinghai Lub55438f2012-02-23 19:23:30 -08003835 pci_realloc_get_opt("on");
Jeff Garzik32a2eea2007-10-11 16:57:27 -04003836 } else if (!strcmp(str, "nodomains")) {
3837 pci_no_domains();
Rafael J. Wysocki6748dcc2012-03-01 00:06:33 +01003838 } else if (!strncmp(str, "noari", 5)) {
3839 pcie_ari_disabled = true;
Atsushi Nemoto4516a612007-02-05 16:36:06 -08003840 } else if (!strncmp(str, "cbiosize=", 9)) {
3841 pci_cardbus_io_size = memparse(str + 9, &str);
3842 } else if (!strncmp(str, "cbmemsize=", 10)) {
3843 pci_cardbus_mem_size = memparse(str + 10, &str);
Yuji Shimada32a9a6822009-03-16 17:13:39 +09003844 } else if (!strncmp(str, "resource_alignment=", 19)) {
3845 pci_set_resource_alignment_param(str + 19,
3846 strlen(str + 19));
Andrew Patterson43c16402009-04-22 16:52:09 -06003847 } else if (!strncmp(str, "ecrc=", 5)) {
3848 pcie_ecrc_get_policy(str + 5);
Eric W. Biederman28760482009-09-09 14:09:24 -07003849 } else if (!strncmp(str, "hpiosize=", 9)) {
3850 pci_hotplug_io_size = memparse(str + 9, &str);
3851 } else if (!strncmp(str, "hpmemsize=", 10)) {
3852 pci_hotplug_mem_size = memparse(str + 10, &str);
Jon Mason5f39e672011-10-03 09:50:20 -05003853 } else if (!strncmp(str, "pcie_bus_tune_off", 17)) {
3854 pcie_bus_config = PCIE_BUS_TUNE_OFF;
Jon Masonb03e7492011-07-20 15:20:54 -05003855 } else if (!strncmp(str, "pcie_bus_safe", 13)) {
3856 pcie_bus_config = PCIE_BUS_SAFE;
3857 } else if (!strncmp(str, "pcie_bus_perf", 13)) {
3858 pcie_bus_config = PCIE_BUS_PERFORMANCE;
Jon Mason5f39e672011-10-03 09:50:20 -05003859 } else if (!strncmp(str, "pcie_bus_peer2peer", 18)) {
3860 pcie_bus_config = PCIE_BUS_PEER2PEER;
Bjorn Helgaas284f5f92012-04-30 15:21:02 -06003861 } else if (!strncmp(str, "pcie_scan_all", 13)) {
3862 pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS);
Matthew Wilcox309e57d2006-03-05 22:33:34 -07003863 } else {
3864 printk(KERN_ERR "PCI: Unknown option `%s'\n",
3865 str);
3866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 }
3868 str = k;
3869 }
Andi Kleen0637a702006-09-26 10:52:41 +02003870 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871}
Andi Kleen0637a702006-09-26 10:52:41 +02003872early_param("pci", pci_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Tejun Heo0b62e132007-07-27 14:43:35 +09003874EXPORT_SYMBOL(pci_reenable_device);
Benjamin Herrenschmidtb7189892007-12-20 15:28:08 +11003875EXPORT_SYMBOL(pci_enable_device_io);
3876EXPORT_SYMBOL(pci_enable_device_mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877EXPORT_SYMBOL(pci_enable_device);
Tejun Heo9ac78492007-01-20 16:00:26 +09003878EXPORT_SYMBOL(pcim_enable_device);
3879EXPORT_SYMBOL(pcim_pin_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880EXPORT_SYMBOL(pci_disable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881EXPORT_SYMBOL(pci_find_capability);
3882EXPORT_SYMBOL(pci_bus_find_capability);
3883EXPORT_SYMBOL(pci_release_regions);
3884EXPORT_SYMBOL(pci_request_regions);
Arjan van de Vene8de1482008-10-22 19:55:31 -07003885EXPORT_SYMBOL(pci_request_regions_exclusive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886EXPORT_SYMBOL(pci_release_region);
3887EXPORT_SYMBOL(pci_request_region);
Arjan van de Vene8de1482008-10-22 19:55:31 -07003888EXPORT_SYMBOL(pci_request_region_exclusive);
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003889EXPORT_SYMBOL(pci_release_selected_regions);
3890EXPORT_SYMBOL(pci_request_selected_regions);
Arjan van de Vene8de1482008-10-22 19:55:31 -07003891EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892EXPORT_SYMBOL(pci_set_master);
Ben Hutchings6a479072008-12-23 03:08:29 +00003893EXPORT_SYMBOL(pci_clear_master);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894EXPORT_SYMBOL(pci_set_mwi);
Randy Dunlap694625c2007-07-09 11:55:54 -07003895EXPORT_SYMBOL(pci_try_set_mwi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896EXPORT_SYMBOL(pci_clear_mwi);
Brett M Russa04ce0f2005-08-15 15:23:41 -04003897EXPORT_SYMBOL_GPL(pci_intx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898EXPORT_SYMBOL(pci_assign_resource);
3899EXPORT_SYMBOL(pci_find_parent_resource);
Hidetoshi Setoc87deff2006-12-18 10:31:06 +09003900EXPORT_SYMBOL(pci_select_bars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
3902EXPORT_SYMBOL(pci_set_power_state);
3903EXPORT_SYMBOL(pci_save_state);
3904EXPORT_SYMBOL(pci_restore_state);
Rafael J. Wysockie5899e12008-07-19 14:39:24 +02003905EXPORT_SYMBOL(pci_pme_capable);
Rafael J. Wysocki5a6c9b62008-08-08 00:14:24 +02003906EXPORT_SYMBOL(pci_pme_active);
Rafael J. Wysocki0235c4f2008-08-18 21:38:00 +02003907EXPORT_SYMBOL(pci_wake_from_d3);
Rafael J. Wysockie5899e12008-07-19 14:39:24 +02003908EXPORT_SYMBOL(pci_target_state);
Rafael J. Wysocki404cc2d2008-07-07 03:35:26 +02003909EXPORT_SYMBOL(pci_prepare_to_sleep);
3910EXPORT_SYMBOL(pci_back_from_sleep);
Brian Kingf7bdd122007-04-06 16:39:36 -05003911EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);