blob: 6c66c5b8e152885a9588224e88151d5cd46abef8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/pci/pci-driver.c
3 *
Greg Kroah-Hartman2b937302007-11-28 12:23:18 -08004 * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com>
5 * (C) Copyright 2007 Novell Inc.
6 *
7 * Released under the GPL v2 only.
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/pci.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/device.h>
Andi Kleend42c6992005-07-06 19:56:03 +020015#include <linux/mempolicy.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080016#include <linux/string.h>
17#include <linux/slab.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080018#include <linux/sched.h>
Rusty Russell873392c2008-12-31 23:54:56 +103019#include <linux/cpu.h>
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +010020#include <linux/pm_runtime.h>
Rafael J. Wysockieea3fc032011-07-06 10:51:40 +020021#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "pci.h"
23
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070024struct pci_dynid {
25 struct list_head node;
26 struct pci_device_id id;
27};
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/**
Tejun Heo9dba9102009-09-03 15:26:36 +090030 * pci_add_dynid - add a new PCI device ID to this driver and re-probe devices
31 * @drv: target pci driver
32 * @vendor: PCI vendor ID
33 * @device: PCI device ID
34 * @subvendor: PCI subvendor ID
35 * @subdevice: PCI subdevice ID
36 * @class: PCI class
37 * @class_mask: PCI class mask
38 * @driver_data: private driver data
39 *
40 * Adds a new dynamic pci device ID to this driver and causes the
41 * driver to probe for all devices again. @drv must have been
42 * registered prior to calling this function.
43 *
44 * CONTEXT:
45 * Does GFP_KERNEL allocation.
46 *
47 * RETURNS:
48 * 0 on success, -errno on failure.
49 */
50int pci_add_dynid(struct pci_driver *drv,
51 unsigned int vendor, unsigned int device,
52 unsigned int subvendor, unsigned int subdevice,
53 unsigned int class, unsigned int class_mask,
54 unsigned long driver_data)
55{
56 struct pci_dynid *dynid;
57 int retval;
58
59 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
60 if (!dynid)
61 return -ENOMEM;
62
63 dynid->id.vendor = vendor;
64 dynid->id.device = device;
65 dynid->id.subvendor = subvendor;
66 dynid->id.subdevice = subdevice;
67 dynid->id.class = class;
68 dynid->id.class_mask = class_mask;
69 dynid->id.driver_data = driver_data;
70
71 spin_lock(&drv->dynids.lock);
72 list_add_tail(&dynid->node, &drv->dynids.list);
73 spin_unlock(&drv->dynids.lock);
74
Tejun Heo9dba9102009-09-03 15:26:36 +090075 retval = driver_attach(&drv->driver);
Tejun Heo9dba9102009-09-03 15:26:36 +090076
77 return retval;
78}
79
80static void pci_free_dynids(struct pci_driver *drv)
81{
82 struct pci_dynid *dynid, *n;
83
84 spin_lock(&drv->dynids.lock);
85 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
86 list_del(&dynid->node);
87 kfree(dynid);
88 }
89 spin_unlock(&drv->dynids.lock);
90}
91
92/*
93 * Dynamic device ID manipulation via sysfs is disabled for !CONFIG_HOTPLUG
94 */
95#ifdef CONFIG_HOTPLUG
96/**
97 * store_new_id - sysfs frontend to pci_add_dynid()
Randy Dunlap8f7020d2005-10-23 11:57:38 -070098 * @driver: target device driver
99 * @buf: buffer for scanning device ID data
100 * @count: input size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 *
Tejun Heo9dba9102009-09-03 15:26:36 +0900102 * Allow PCI IDs to be added to an existing driver via sysfs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
Randy Dunlapf8eb1002005-10-28 20:36:51 -0700104static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105store_new_id(struct device_driver *driver, const char *buf, size_t count)
106{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct pci_driver *pdrv = to_pci_driver(driver);
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200108 const struct pci_device_id *ids = pdrv->id_table;
Jean Delvare6ba18632007-04-07 17:21:28 +0200109 __u32 vendor, device, subvendor=PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 subdevice=PCI_ANY_ID, class=0, class_mask=0;
111 unsigned long driver_data=0;
112 int fields=0;
Tejun Heo9dba9102009-09-03 15:26:36 +0900113 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200115 fields = sscanf(buf, "%x %x %x %x %x %x %lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 &vendor, &device, &subvendor, &subdevice,
117 &class, &class_mask, &driver_data);
Jean Delvare6ba18632007-04-07 17:21:28 +0200118 if (fields < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return -EINVAL;
120
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200121 /* Only accept driver_data values that match an existing id_table
122 entry */
Chris Wright2debb4d2008-11-25 19:36:10 -0800123 if (ids) {
124 retval = -EINVAL;
125 while (ids->vendor || ids->subvendor || ids->class_mask) {
126 if (driver_data == ids->driver_data) {
127 retval = 0;
128 break;
129 }
130 ids++;
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200131 }
Chris Wright2debb4d2008-11-25 19:36:10 -0800132 if (retval) /* No match */
133 return retval;
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200134 }
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200135
Tejun Heo9dba9102009-09-03 15:26:36 +0900136 retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice,
137 class, class_mask, driver_data);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700138 if (retval)
139 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return count;
141}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Chris Wright09943752009-02-23 21:52:23 -0800143/**
144 * store_remove_id - remove a PCI device ID from this driver
145 * @driver: target device driver
146 * @buf: buffer for scanning device ID data
147 * @count: input size
148 *
149 * Removes a dynamic pci device ID to this driver.
150 */
151static ssize_t
152store_remove_id(struct device_driver *driver, const char *buf, size_t count)
153{
154 struct pci_dynid *dynid, *n;
155 struct pci_driver *pdrv = to_pci_driver(driver);
156 __u32 vendor, device, subvendor = PCI_ANY_ID,
157 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
158 int fields = 0;
159 int retval = -ENODEV;
160
161 fields = sscanf(buf, "%x %x %x %x %x %x",
162 &vendor, &device, &subvendor, &subdevice,
163 &class, &class_mask);
164 if (fields < 2)
165 return -EINVAL;
166
167 spin_lock(&pdrv->dynids.lock);
168 list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) {
169 struct pci_device_id *id = &dynid->id;
170 if ((id->vendor == vendor) &&
171 (id->device == device) &&
172 (subvendor == PCI_ANY_ID || id->subvendor == subvendor) &&
173 (subdevice == PCI_ANY_ID || id->subdevice == subdevice) &&
174 !((id->class ^ class) & class_mask)) {
175 list_del(&dynid->node);
176 kfree(dynid);
177 retval = 0;
178 break;
179 }
180 }
181 spin_unlock(&pdrv->dynids.lock);
182
183 if (retval)
184 return retval;
185 return count;
186}
Chris Wright09943752009-02-23 21:52:23 -0800187
Konstantin Khlebnikovbfb09a82012-08-08 14:47:51 +0400188static struct driver_attribute pci_drv_attrs[] = {
189 __ATTR(new_id, S_IWUSR, NULL, store_new_id),
190 __ATTR(remove_id, S_IWUSR, NULL, store_remove_id),
191 __ATTR_NULL,
192};
Alan Sterned283e92012-01-24 14:35:13 -0500193
Konstantin Khlebnikovbfb09a82012-08-08 14:47:51 +0400194#else
195#define pci_drv_attrs NULL
196#endif /* CONFIG_HOTPLUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198/**
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700199 * pci_match_id - See if a pci device matches a given pci_id table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * @ids: array of PCI device id structures to search in
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700201 * @dev: the PCI device structure to match against.
202 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * Used by a driver to check whether a PCI device present in the
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700204 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * pci_device_id structure or %NULL if there is no match.
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700206 *
Randy Dunlap8b607562007-05-09 07:19:14 +0200207 * Deprecated, don't use this as it will not catch any dynamic ids
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700208 * that a driver might want to check for.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700210const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
211 struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700213 if (ids) {
214 while (ids->vendor || ids->subvendor || ids->class_mask) {
215 if (pci_match_one_device(ids, dev))
216 return ids;
217 ids++;
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220 return NULL;
221}
222
223/**
Randy Dunlapae9608a2007-01-09 21:41:01 -0800224 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700225 * @drv: the PCI driver to match against
Henrik Kretzschmar39ba4872006-08-15 10:57:16 +0200226 * @dev: the PCI device structure to match against
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700227 *
228 * Used by a driver to check whether a PCI device present in the
229 * system is in its list of supported devices. Returns the matching
230 * pci_device_id structure or %NULL if there is no match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
Adrian Bunkd73460d2007-10-24 18:27:18 +0200232static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
233 struct pci_dev *dev)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700234{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700235 struct pci_dynid *dynid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Russell King7461b602006-11-29 21:18:04 +0000237 /* Look at the dynamic ids first, before the static ones */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700238 spin_lock(&drv->dynids.lock);
239 list_for_each_entry(dynid, &drv->dynids.list, node) {
240 if (pci_match_one_device(&dynid->id, dev)) {
241 spin_unlock(&drv->dynids.lock);
242 return &dynid->id;
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700245 spin_unlock(&drv->dynids.lock);
Russell King7461b602006-11-29 21:18:04 +0000246
247 return pci_match_id(drv->id_table, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Rusty Russell873392c2008-12-31 23:54:56 +1030250struct drv_dev_and_id {
251 struct pci_driver *drv;
252 struct pci_dev *dev;
253 const struct pci_device_id *id;
254};
255
256static long local_pci_probe(void *_ddi)
257{
258 struct drv_dev_and_id *ddi = _ddi;
Huang Ying967577b2012-11-20 16:08:22 +0800259 struct pci_dev *pci_dev = ddi->dev;
260 struct pci_driver *pci_drv = ddi->drv;
261 struct device *dev = &pci_dev->dev;
Alan Sternf3ec4f82010-06-08 15:23:51 -0400262 int rc;
Rusty Russell873392c2008-12-31 23:54:56 +1030263
Huang Ying967577b2012-11-20 16:08:22 +0800264 /*
265 * Unbound PCI devices are always put in D0, regardless of
266 * runtime PM status. During probe, the device is set to
267 * active and the usage count is incremented. If the driver
268 * supports runtime PM, it should call pm_runtime_put_noidle()
269 * in its probe routine and pm_runtime_get_noresume() in its
270 * remove routine.
Alan Sternf3ec4f82010-06-08 15:23:51 -0400271 */
Huang Ying967577b2012-11-20 16:08:22 +0800272 pm_runtime_get_sync(dev);
273 pci_dev->driver = pci_drv;
274 rc = pci_drv->probe(pci_dev, ddi->id);
Alan Sternf3ec4f82010-06-08 15:23:51 -0400275 if (rc) {
Huang Ying967577b2012-11-20 16:08:22 +0800276 pci_dev->driver = NULL;
277 pm_runtime_put_sync(dev);
Alan Sternf3ec4f82010-06-08 15:23:51 -0400278 }
279 return rc;
Rusty Russell873392c2008-12-31 23:54:56 +1030280}
281
Andi Kleend42c6992005-07-06 19:56:03 +0200282static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
283 const struct pci_device_id *id)
284{
Rusty Russell873392c2008-12-31 23:54:56 +1030285 int error, node;
286 struct drv_dev_and_id ddi = { drv, dev, id };
Mike Travisf70316d2008-04-04 18:11:06 -0700287
Rusty Russell873392c2008-12-31 23:54:56 +1030288 /* Execute driver initialization on node where the device's
289 bus is attached to. This way the driver likely allocates
290 its local memory on the right node without any need to
291 change it. */
292 node = dev_to_node(&dev->dev);
Mike Travisf70316d2008-04-04 18:11:06 -0700293 if (node >= 0) {
Rusty Russell873392c2008-12-31 23:54:56 +1030294 int cpu;
Rusty Russell873392c2008-12-31 23:54:56 +1030295
296 get_online_cpus();
Rusty Russella70f7302009-03-13 14:49:46 +1030297 cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
Rusty Russell873392c2008-12-31 23:54:56 +1030298 if (cpu < nr_cpu_ids)
299 error = work_on_cpu(cpu, local_pci_probe, &ddi);
300 else
301 error = local_pci_probe(&ddi);
302 put_online_cpus();
303 } else
304 error = local_pci_probe(&ddi);
Andi Kleend42c6992005-07-06 19:56:03 +0200305 return error;
306}
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/**
Randy Dunlap23ea3792010-11-18 15:02:31 -0800309 * __pci_device_probe - check if a driver wants to claim a specific PCI device
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700310 * @drv: driver to call to check if it wants the PCI device
311 * @pci_dev: PCI device being probed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 *
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700313 * returns 0 on success, else error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
315 */
316static int
317__pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700318{
319 const struct pci_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int error = 0;
321
322 if (!pci_dev->driver && drv->probe) {
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700323 error = -ENODEV;
324
325 id = pci_match_device(drv, pci_dev);
326 if (id)
Andi Kleend42c6992005-07-06 19:56:03 +0200327 error = pci_call_probe(drv, pci_dev, id);
Huang Ying967577b2012-11-20 16:08:22 +0800328 if (error >= 0)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700329 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331 return error;
332}
333
334static int pci_device_probe(struct device * dev)
335{
336 int error = 0;
337 struct pci_driver *drv;
338 struct pci_dev *pci_dev;
339
340 drv = to_pci_driver(dev->driver);
341 pci_dev = to_pci_dev(dev);
342 pci_dev_get(pci_dev);
343 error = __pci_device_probe(drv, pci_dev);
344 if (error)
345 pci_dev_put(pci_dev);
346
347 return error;
348}
349
350static int pci_device_remove(struct device * dev)
351{
352 struct pci_dev * pci_dev = to_pci_dev(dev);
353 struct pci_driver * drv = pci_dev->driver;
354
355 if (drv) {
Alan Sternf3ec4f82010-06-08 15:23:51 -0400356 if (drv->remove) {
357 pm_runtime_get_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 drv->remove(pci_dev);
Alan Sternf3ec4f82010-06-08 15:23:51 -0400359 pm_runtime_put_noidle(dev);
360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 pci_dev->driver = NULL;
362 }
363
Alan Sternf3ec4f82010-06-08 15:23:51 -0400364 /* Undo the runtime PM settings in local_pci_probe() */
Huang Ying967577b2012-11-20 16:08:22 +0800365 pm_runtime_put_sync(dev);
Alan Sternf3ec4f82010-06-08 15:23:51 -0400366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /*
Shaohua Li2449e062006-10-20 14:45:32 -0700368 * If the device is still on, set the power state as "unknown",
369 * since it might change by the next time we load the driver.
370 */
371 if (pci_dev->current_state == PCI_D0)
372 pci_dev->current_state = PCI_UNKNOWN;
373
374 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 * We would love to complain here if pci_dev->is_enabled is set, that
376 * the driver should have called pci_disable_device(), but the
377 * unfortunate fact is there are too many odd BIOS and bridge setups
378 * that don't like drivers doing that all of the time.
379 * Oh well, we can dream of sane hardware when we sleep, no matter how
380 * horrible the crap we have to deal with is when we are awake...
381 */
382
383 pci_dev_put(pci_dev);
384 return 0;
385}
386
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200387static void pci_device_shutdown(struct device *dev)
388{
389 struct pci_dev *pci_dev = to_pci_dev(dev);
390 struct pci_driver *drv = pci_dev->driver;
391
Huang Ying3ff2de92012-10-24 14:54:14 +0800392 pm_runtime_resume(dev);
393
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200394 if (drv && drv->shutdown)
395 drv->shutdown(pci_dev);
396 pci_msi_shutdown(pci_dev);
397 pci_msix_shutdown(pci_dev);
Rafael J. Wysocki5b415f12012-02-07 00:50:35 +0100398
399 /*
Khalid Azizb566a222012-04-27 13:00:33 -0600400 * Turn off Bus Master bit on the device to tell it to not
401 * continue to do DMA
402 */
403 pci_disable_device(pci_dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200404}
405
Rafael J. Wysockiaa338602011-02-11 00:06:54 +0100406#ifdef CONFIG_PM
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100407
408/* Auxiliary functions used for system resume and run-time resume. */
409
410/**
411 * pci_restore_standard_config - restore standard config registers of PCI device
412 * @pci_dev: PCI device to handle
413 */
414static int pci_restore_standard_config(struct pci_dev *pci_dev)
415{
416 pci_update_current_state(pci_dev, PCI_UNKNOWN);
417
418 if (pci_dev->current_state != PCI_D0) {
419 int error = pci_set_power_state(pci_dev, PCI_D0);
420 if (error)
421 return error;
422 }
423
Jon Mason1d3c16a2010-11-30 17:43:26 -0600424 pci_restore_state(pci_dev);
425 return 0;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100426}
427
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100428#endif
429
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200430#ifdef CONFIG_PM_SLEEP
431
Rafael J. Wysockidb288c92012-07-05 15:20:00 -0600432static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
433{
434 pci_power_up(pci_dev);
435 pci_restore_state(pci_dev);
436 pci_fixup_device(pci_fixup_resume_early, pci_dev);
437}
438
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200439/*
440 * Default "suspend" method for devices that have no driver provided suspend,
Rafael J. Wysockifa58d302009-01-07 13:03:42 +0100441 * or not even a driver at all (second part).
442 */
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100443static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200444{
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200445 /*
446 * mark its power state as "unknown", since we don't know if
447 * e.g. the BIOS will change its device state when we suspend.
448 */
449 if (pci_dev->current_state == PCI_D0)
450 pci_dev->current_state = PCI_UNKNOWN;
451}
452
453/*
454 * Default "resume" method for devices that have no driver provided resume,
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100455 * or not even a driver at all (second part).
456 */
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100457static int pci_pm_reenable_device(struct pci_dev *pci_dev)
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100458{
459 int retval;
460
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200461 /* if the device was enabled before suspend, reenable */
462 retval = pci_reenable_device(pci_dev);
463 /*
464 * if the device was busmaster before the suspend, make it busmaster
465 * again
466 */
467 if (pci_dev->is_busmaster)
468 pci_set_master(pci_dev);
469
470 return retval;
471}
472
473static int pci_legacy_suspend(struct device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct pci_dev * pci_dev = to_pci_dev(dev);
476 struct pci_driver * drv = pci_dev->driver;
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100477
Andrew Morton02669492006-03-23 01:38:34 -0800478 if (drv && drv->suspend) {
Rafael J. Wysocki99dadce2009-02-04 01:59:09 +0100479 pci_power_t prev = pci_dev->current_state;
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100480 int error;
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100481
Frans Pop57ef8022009-03-16 22:39:56 +0100482 error = drv->suspend(pci_dev, state);
483 suspend_report_result(drv->suspend, error);
484 if (error)
485 return error;
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100486
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100487 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
Rafael J. Wysocki99dadce2009-02-04 01:59:09 +0100488 && pci_dev->current_state != PCI_UNKNOWN) {
489 WARN_ONCE(pci_dev->current_state != prev,
490 "PCI PM: Device state not saved by %pF\n",
491 drv->suspend);
Rafael J. Wysocki99dadce2009-02-04 01:59:09 +0100492 }
Andrew Morton02669492006-03-23 01:38:34 -0800493 }
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100494
495 pci_fixup_device(pci_fixup_suspend, pci_dev);
496
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200500static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700501{
502 struct pci_dev * pci_dev = to_pci_dev(dev);
503 struct pci_driver * drv = pci_dev->driver;
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700504
505 if (drv && drv->suspend_late) {
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100506 pci_power_t prev = pci_dev->current_state;
507 int error;
508
Frans Pop57ef8022009-03-16 22:39:56 +0100509 error = drv->suspend_late(pci_dev, state);
510 suspend_report_result(drv->suspend_late, error);
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100511 if (error)
512 return error;
513
514 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
515 && pci_dev->current_state != PCI_UNKNOWN) {
516 WARN_ONCE(pci_dev->current_state != prev,
517 "PCI PM: Device state not saved by %pF\n",
518 drv->suspend_late);
519 return 0;
520 }
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700521 }
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100522
523 if (!pci_dev->state_saved)
524 pci_save_state(pci_dev);
525
526 pci_pm_set_unknown_state(pci_dev);
527
528 return 0;
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700529}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100531static int pci_legacy_resume_early(struct device *dev)
532{
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100533 struct pci_dev * pci_dev = to_pci_dev(dev);
534 struct pci_driver * drv = pci_dev->driver;
535
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100536 return drv && drv->resume_early ?
537 drv->resume_early(pci_dev) : 0;
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100538}
539
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200540static int pci_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct pci_dev * pci_dev = to_pci_dev(dev);
543 struct pci_driver * drv = pci_dev->driver;
544
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100545 pci_fixup_device(pci_fixup_resume, pci_dev);
546
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100547 return drv && drv->resume ?
548 drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Rafael J. Wysocki571ff752009-01-07 13:05:05 +0100551/* Auxiliary functions used by the new power management framework */
552
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100553static void pci_pm_default_resume(struct pci_dev *pci_dev)
Rafael J. Wysocki571ff752009-01-07 13:05:05 +0100554{
Rafael J. Wysocki734104292009-01-07 13:07:15 +0100555 pci_fixup_device(pci_fixup_resume, pci_dev);
556
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100557 if (!pci_is_bridge(pci_dev))
558 pci_enable_wake(pci_dev, PCI_D0, false);
Rafael J. Wysocki571ff752009-01-07 13:05:05 +0100559}
560
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100561static void pci_pm_default_suspend(struct pci_dev *pci_dev)
Rafael J. Wysocki734104292009-01-07 13:07:15 +0100562{
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100563 /* Disable non-bridge devices without PM support */
Rafael J. Wysockicbbc2f62009-02-04 02:01:15 +0100564 if (!pci_is_bridge(pci_dev))
565 pci_disable_enabled_device(pci_dev);
Rafael J. Wysocki734104292009-01-07 13:07:15 +0100566}
567
Rafael J. Wysocki07e836e2009-01-07 13:06:10 +0100568static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
569{
570 struct pci_driver *drv = pci_dev->driver;
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100571 bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
Rafael J. Wysocki07e836e2009-01-07 13:06:10 +0100572 || drv->resume_early);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100573
574 /*
575 * Legacy PM support is used by default, so warn if the new framework is
576 * supported as well. Drivers are supposed to support either the
577 * former, or the latter, but not both at the same time.
578 */
David Fries82440a82011-11-20 15:29:46 -0600579 WARN(ret && drv->driver.pm, "driver %s device %04x:%04x\n",
580 drv->name, pci_dev->vendor, pci_dev->device);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100581
582 return ret;
Rafael J. Wysocki07e836e2009-01-07 13:06:10 +0100583}
584
Rafael J. Wysocki571ff752009-01-07 13:05:05 +0100585/* New power management framework */
586
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200587static int pci_pm_prepare(struct device *dev)
588{
589 struct device_driver *drv = dev->driver;
590 int error = 0;
591
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100592 /*
593 * PCI devices suspended at run time need to be resumed at this
594 * point, because in general it is necessary to reconfigure them for
595 * system suspend. Namely, if the device is supposed to wake up the
596 * system from the sleep state, we may need to reconfigure it for this
597 * purpose. In turn, if the device is not supposed to wake up the
598 * system from the sleep state, we'll have to prevent it from signaling
599 * wake-up.
600 */
Rafael J. Wysockieea3fc032011-07-06 10:51:40 +0200601 pm_runtime_resume(dev);
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100602
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200603 if (drv && drv->pm && drv->pm->prepare)
604 error = drv->pm->prepare(dev);
605
606 return error;
607}
608
609static void pci_pm_complete(struct device *dev)
610{
611 struct device_driver *drv = dev->driver;
612
613 if (drv && drv->pm && drv->pm->complete)
614 drv->pm->complete(dev);
615}
616
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100617#else /* !CONFIG_PM_SLEEP */
618
619#define pci_pm_prepare NULL
620#define pci_pm_complete NULL
621
622#endif /* !CONFIG_PM_SLEEP */
623
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200624#ifdef CONFIG_SUSPEND
625
626static int pci_pm_suspend(struct device *dev)
627{
628 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700629 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200630
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100631 if (pci_has_legacy_pm_support(pci_dev))
632 return pci_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100633
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100634 if (!pm) {
635 pci_pm_default_suspend(pci_dev);
636 goto Fixup;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200637 }
Rafael J. Wysockifa58d302009-01-07 13:03:42 +0100638
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100639 if (pm->suspend) {
640 pci_power_t prev = pci_dev->current_state;
641 int error;
642
643 error = pm->suspend(dev);
644 suspend_report_result(pm->suspend, error);
645 if (error)
646 return error;
647
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100648 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100649 && pci_dev->current_state != PCI_UNKNOWN) {
650 WARN_ONCE(pci_dev->current_state != prev,
651 "PCI PM: State of device not saved by %pF\n",
652 pm->suspend);
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100653 }
654 }
655
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100656 Fixup:
657 pci_fixup_device(pci_fixup_suspend, pci_dev);
658
659 return 0;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200660}
661
662static int pci_pm_suspend_noirq(struct device *dev)
Greg KHc8958172005-04-08 14:53:31 +0900663{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100664 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700665 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Greg KHc8958172005-04-08 14:53:31 +0900666
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100667 if (pci_has_legacy_pm_support(pci_dev))
668 return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
669
Rafael J. Wysocki931ff682009-03-16 22:40:50 +0100670 if (!pm) {
671 pci_save_state(pci_dev);
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100672 return 0;
Rafael J. Wysocki931ff682009-03-16 22:40:50 +0100673 }
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100674
675 if (pm->suspend_noirq) {
676 pci_power_t prev = pci_dev->current_state;
677 int error;
678
679 error = pm->suspend_noirq(dev);
680 suspend_report_result(pm->suspend_noirq, error);
681 if (error)
682 return error;
683
684 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
685 && pci_dev->current_state != PCI_UNKNOWN) {
686 WARN_ONCE(pci_dev->current_state != prev,
687 "PCI PM: State of device not saved by %pF\n",
688 pm->suspend_noirq);
689 return 0;
690 }
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200691 }
692
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100693 if (!pci_dev->state_saved) {
694 pci_save_state(pci_dev);
695 if (!pci_is_bridge(pci_dev))
696 pci_prepare_to_sleep(pci_dev);
697 }
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100698
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100699 pci_pm_set_unknown_state(pci_dev);
700
Alan Sterndbf0e4c2012-07-09 11:09:21 -0400701 /*
702 * Some BIOSes from ASUS have a bug: If a USB EHCI host controller's
703 * PCI COMMAND register isn't 0, the BIOS assumes that the controller
704 * hasn't been quiesced and tries to turn it off. If the controller
705 * is already in D3, this can hang or cause memory corruption.
706 *
707 * Since the value of the COMMAND register doesn't matter once the
708 * device has been suspended, we can safely set it to 0 here.
709 */
710 if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
711 pci_write_config_word(pci_dev, PCI_COMMAND, 0);
712
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100713 return 0;
Greg KHc8958172005-04-08 14:53:31 +0900714}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200716static int pci_pm_resume_noirq(struct device *dev)
717{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100718 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200719 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200720 int error = 0;
721
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100722 pci_pm_default_resume_early(pci_dev);
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100723
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100724 if (pci_has_legacy_pm_support(pci_dev))
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100725 return pci_legacy_resume_early(dev);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100726
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100727 if (drv && drv->pm && drv->pm->resume_noirq)
728 error = drv->pm->resume_noirq(dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200729
730 return error;
731}
732
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100733static int pci_pm_resume(struct device *dev)
734{
735 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700736 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100737 int error = 0;
738
Rafael J. Wysocki418e4da2009-01-26 21:43:08 +0100739 /*
740 * This is necessary for the suspend error path in which resume is
741 * called without restoring the standard config registers of the device.
742 */
743 if (pci_dev->state_saved)
744 pci_restore_standard_config(pci_dev);
745
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100746 if (pci_has_legacy_pm_support(pci_dev))
747 return pci_legacy_resume(dev);
748
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100749 pci_pm_default_resume(pci_dev);
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100750
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100751 if (pm) {
752 if (pm->resume)
753 error = pm->resume(dev);
754 } else {
755 pci_pm_reenable_device(pci_dev);
756 }
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100757
Rafael J. Wysocki999cce42009-09-09 23:51:27 +0200758 return error;
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100759}
760
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200761#else /* !CONFIG_SUSPEND */
762
763#define pci_pm_suspend NULL
764#define pci_pm_suspend_noirq NULL
765#define pci_pm_resume NULL
766#define pci_pm_resume_noirq NULL
767
768#endif /* !CONFIG_SUSPEND */
769
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200770#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200771
772static int pci_pm_freeze(struct device *dev)
773{
774 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700775 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200776
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100777 if (pci_has_legacy_pm_support(pci_dev))
778 return pci_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100779
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100780 if (!pm) {
781 pci_pm_default_suspend(pci_dev);
782 return 0;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200783 }
784
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100785 if (pm->freeze) {
786 int error;
787
788 error = pm->freeze(dev);
789 suspend_report_result(pm->freeze, error);
790 if (error)
791 return error;
792 }
793
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100794 return 0;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200795}
796
797static int pci_pm_freeze_noirq(struct device *dev)
798{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100799 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200800 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200801
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100802 if (pci_has_legacy_pm_support(pci_dev))
803 return pci_legacy_suspend_late(dev, PMSG_FREEZE);
804
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100805 if (drv && drv->pm && drv->pm->freeze_noirq) {
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100806 int error;
807
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100808 error = drv->pm->freeze_noirq(dev);
809 suspend_report_result(drv->pm->freeze_noirq, error);
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100810 if (error)
811 return error;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200812 }
813
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100814 if (!pci_dev->state_saved)
815 pci_save_state(pci_dev);
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100816
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100817 pci_pm_set_unknown_state(pci_dev);
818
819 return 0;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200820}
821
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200822static int pci_pm_thaw_noirq(struct device *dev)
823{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100824 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200825 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200826 int error = 0;
827
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100828 if (pci_has_legacy_pm_support(pci_dev))
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100829 return pci_legacy_resume_early(dev);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100830
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100831 pci_update_current_state(pci_dev, PCI_D0);
832
833 if (drv && drv->pm && drv->pm->thaw_noirq)
834 error = drv->pm->thaw_noirq(dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200835
836 return error;
837}
838
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100839static int pci_pm_thaw(struct device *dev)
840{
841 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700842 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100843 int error = 0;
844
845 if (pci_has_legacy_pm_support(pci_dev))
846 return pci_legacy_resume(dev);
847
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100848 if (pm) {
849 if (pm->thaw)
850 error = pm->thaw(dev);
851 } else {
852 pci_pm_reenable_device(pci_dev);
853 }
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100854
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +0200855 pci_dev->state_saved = false;
856
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100857 return error;
858}
859
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200860static int pci_pm_poweroff(struct device *dev)
861{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100862 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700863 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200864
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100865 if (pci_has_legacy_pm_support(pci_dev))
866 return pci_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100867
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100868 if (!pm) {
869 pci_pm_default_suspend(pci_dev);
870 goto Fixup;
871 }
872
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100873 if (pm->poweroff) {
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100874 int error;
875
Rafael J. Wysockiddb7c9d2009-02-04 01:56:14 +0100876 error = pm->poweroff(dev);
877 suspend_report_result(pm->poweroff, error);
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100878 if (error)
879 return error;
880 }
881
882 Fixup:
883 pci_fixup_device(pci_fixup_suspend, pci_dev);
884
885 return 0;
886}
887
888static int pci_pm_poweroff_noirq(struct device *dev)
889{
890 struct pci_dev *pci_dev = to_pci_dev(dev);
891 struct device_driver *drv = dev->driver;
892
893 if (pci_has_legacy_pm_support(to_pci_dev(dev)))
894 return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
895
896 if (!drv || !drv->pm)
897 return 0;
898
899 if (drv->pm->poweroff_noirq) {
900 int error;
901
902 error = drv->pm->poweroff_noirq(dev);
903 suspend_report_result(drv->pm->poweroff_noirq, error);
904 if (error)
905 return error;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200906 }
907
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100908 if (!pci_dev->state_saved && !pci_is_bridge(pci_dev))
909 pci_prepare_to_sleep(pci_dev);
910
Rafael J. Wysocki0b68c8e2012-08-12 23:26:07 +0200911 /*
912 * The reason for doing this here is the same as for the analogous code
913 * in pci_pm_suspend_noirq().
914 */
915 if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
916 pci_write_config_word(pci_dev, PCI_COMMAND, 0);
917
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100918 return 0;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200919}
920
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200921static int pci_pm_restore_noirq(struct device *dev)
922{
923 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200924 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200925 int error = 0;
926
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100927 pci_pm_default_resume_early(pci_dev);
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100928
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100929 if (pci_has_legacy_pm_support(pci_dev))
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100930 return pci_legacy_resume_early(dev);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100931
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100932 if (drv && drv->pm && drv->pm->restore_noirq)
933 error = drv->pm->restore_noirq(dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200934
935 return error;
936}
937
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100938static int pci_pm_restore(struct device *dev)
939{
940 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700941 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100942 int error = 0;
943
Rafael J. Wysocki418e4da2009-01-26 21:43:08 +0100944 /*
945 * This is necessary for the hibernation error path in which restore is
946 * called without restoring the standard config registers of the device.
947 */
948 if (pci_dev->state_saved)
949 pci_restore_standard_config(pci_dev);
950
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100951 if (pci_has_legacy_pm_support(pci_dev))
952 return pci_legacy_resume(dev);
953
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100954 pci_pm_default_resume(pci_dev);
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100955
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100956 if (pm) {
957 if (pm->restore)
958 error = pm->restore(dev);
959 } else {
960 pci_pm_reenable_device(pci_dev);
961 }
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100962
963 return error;
964}
965
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200966#else /* !CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200967
968#define pci_pm_freeze NULL
969#define pci_pm_freeze_noirq NULL
970#define pci_pm_thaw NULL
971#define pci_pm_thaw_noirq NULL
972#define pci_pm_poweroff NULL
973#define pci_pm_poweroff_noirq NULL
974#define pci_pm_restore NULL
975#define pci_pm_restore_noirq NULL
976
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200977#endif /* !CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200978
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100979#ifdef CONFIG_PM_RUNTIME
980
981static int pci_pm_runtime_suspend(struct device *dev)
982{
983 struct pci_dev *pci_dev = to_pci_dev(dev);
984 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
985 pci_power_t prev = pci_dev->current_state;
986 int error;
987
Huang Ying967577b2012-11-20 16:08:22 +0800988 /*
989 * If pci_dev->driver is not set (unbound), the device should
990 * always remain in D0 regardless of the runtime PM status
991 */
992 if (!pci_dev->driver)
993 return 0;
994
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100995 if (!pm || !pm->runtime_suspend)
996 return -ENOSYS;
997
Huang Ying448bd852012-06-23 10:23:51 +0800998 pci_dev->no_d3cold = false;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100999 error = pm->runtime_suspend(dev);
1000 suspend_report_result(pm->runtime_suspend, error);
1001 if (error)
1002 return error;
Huang Ying448bd852012-06-23 10:23:51 +08001003 if (!pci_dev->d3cold_allowed)
1004 pci_dev->no_d3cold = true;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001005
1006 pci_fixup_device(pci_fixup_suspend, pci_dev);
1007
1008 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
1009 && pci_dev->current_state != PCI_UNKNOWN) {
1010 WARN_ONCE(pci_dev->current_state != prev,
1011 "PCI PM: State of device not saved by %pF\n",
1012 pm->runtime_suspend);
1013 return 0;
1014 }
1015
1016 if (!pci_dev->state_saved)
1017 pci_save_state(pci_dev);
1018
1019 pci_finish_runtime_suspend(pci_dev);
1020
1021 return 0;
1022}
1023
1024static int pci_pm_runtime_resume(struct device *dev)
1025{
Huang Ying448bd852012-06-23 10:23:51 +08001026 int rc;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001027 struct pci_dev *pci_dev = to_pci_dev(dev);
1028 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1029
Huang Ying967577b2012-11-20 16:08:22 +08001030 /*
1031 * If pci_dev->driver is not set (unbound), the device should
1032 * always remain in D0 regardless of the runtime PM status
1033 */
1034 if (!pci_dev->driver)
1035 return 0;
1036
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001037 if (!pm || !pm->runtime_resume)
1038 return -ENOSYS;
1039
Rafael J. Wysockidb288c92012-07-05 15:20:00 -06001040 pci_restore_standard_config(pci_dev);
1041 pci_fixup_device(pci_fixup_resume_early, pci_dev);
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001042 __pci_enable_wake(pci_dev, PCI_D0, true, false);
1043 pci_fixup_device(pci_fixup_resume, pci_dev);
1044
Huang Ying448bd852012-06-23 10:23:51 +08001045 rc = pm->runtime_resume(dev);
1046
1047 pci_dev->runtime_d3cold = false;
1048
1049 return rc;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001050}
1051
1052static int pci_pm_runtime_idle(struct device *dev)
1053{
Huang Ying967577b2012-11-20 16:08:22 +08001054 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001055 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1056
Huang Ying967577b2012-11-20 16:08:22 +08001057 /*
1058 * If pci_dev->driver is not set (unbound), the device should
1059 * always remain in D0 regardless of the runtime PM status
1060 */
1061 if (!pci_dev->driver)
1062 goto out;
1063
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001064 if (!pm)
1065 return -ENOSYS;
1066
1067 if (pm->runtime_idle) {
1068 int ret = pm->runtime_idle(dev);
1069 if (ret)
1070 return ret;
1071 }
1072
Huang Ying967577b2012-11-20 16:08:22 +08001073out:
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001074 pm_runtime_suspend(dev);
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001075 return 0;
1076}
1077
1078#else /* !CONFIG_PM_RUNTIME */
1079
1080#define pci_pm_runtime_suspend NULL
1081#define pci_pm_runtime_resume NULL
1082#define pci_pm_runtime_idle NULL
1083
1084#endif /* !CONFIG_PM_RUNTIME */
1085
Rafael J. Wysockiaa338602011-02-11 00:06:54 +01001086#ifdef CONFIG_PM
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001087
Dmitry Torokhov8150f322009-07-24 22:11:32 -07001088const struct dev_pm_ops pci_dev_pm_ops = {
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001089 .prepare = pci_pm_prepare,
1090 .complete = pci_pm_complete,
1091 .suspend = pci_pm_suspend,
1092 .resume = pci_pm_resume,
1093 .freeze = pci_pm_freeze,
1094 .thaw = pci_pm_thaw,
1095 .poweroff = pci_pm_poweroff,
1096 .restore = pci_pm_restore,
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001097 .suspend_noirq = pci_pm_suspend_noirq,
1098 .resume_noirq = pci_pm_resume_noirq,
1099 .freeze_noirq = pci_pm_freeze_noirq,
1100 .thaw_noirq = pci_pm_thaw_noirq,
1101 .poweroff_noirq = pci_pm_poweroff_noirq,
1102 .restore_noirq = pci_pm_restore_noirq,
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001103 .runtime_suspend = pci_pm_runtime_suspend,
1104 .runtime_resume = pci_pm_runtime_resume,
1105 .runtime_idle = pci_pm_runtime_idle,
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001106};
1107
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001108#define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001109
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001110#else /* !COMFIG_PM_OPS */
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001111
1112#define PCI_PM_OPS_PTR NULL
1113
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001114#endif /* !COMFIG_PM_OPS */
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116/**
Laurent riffard863b18f2005-10-27 23:12:54 +02001117 * __pci_register_driver - register a new pci driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 * @drv: the driver structure to register
Laurent riffard863b18f2005-10-27 23:12:54 +02001119 * @owner: owner module of drv
Randy Dunlapf95d8822007-02-10 14:41:56 -08001120 * @mod_name: module name string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 *
1122 * Adds the driver structure to the list of registered drivers.
1123 * Returns a negative value on error, otherwise 0.
Steven Coleeaae4b32005-05-03 18:38:30 -06001124 * If no error occurred, the driver remains registered even if
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 * no device was claimed during registration.
1126 */
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -08001127int __pci_register_driver(struct pci_driver *drv, struct module *owner,
1128 const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /* initialize common driver fields */
1131 drv->driver.name = drv->name;
1132 drv->driver.bus = &pci_bus_type;
Laurent riffard863b18f2005-10-27 23:12:54 +02001133 drv->driver.owner = owner;
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -08001134 drv->driver.mod_name = mod_name;
Alan Cox50b00752006-08-16 17:42:18 +01001135
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001136 spin_lock_init(&drv->dynids.lock);
1137 INIT_LIST_HEAD(&drv->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 /* register with core */
Konstantin Khlebnikovbfb09a82012-08-08 14:47:51 +04001140 return driver_register(&drv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143/**
1144 * pci_unregister_driver - unregister a pci driver
1145 * @drv: the driver structure to unregister
1146 *
1147 * Deletes the driver structure from the list of registered PCI drivers,
1148 * gives it a chance to clean up by calling its remove() function for
1149 * each device it was responsible for, and marks those devices as
1150 * driverless.
1151 */
1152
1153void
1154pci_unregister_driver(struct pci_driver *drv)
1155{
1156 driver_unregister(&drv->driver);
1157 pci_free_dynids(drv);
1158}
1159
1160static struct pci_driver pci_compat_driver = {
1161 .name = "compat"
1162};
1163
1164/**
1165 * pci_dev_driver - get the pci_driver of a device
1166 * @dev: the device to query
1167 *
1168 * Returns the appropriate pci_driver structure or %NULL if there is no
1169 * registered driver for the device.
1170 */
1171struct pci_driver *
1172pci_dev_driver(const struct pci_dev *dev)
1173{
1174 if (dev->driver)
1175 return dev->driver;
1176 else {
1177 int i;
1178 for(i=0; i<=PCI_ROM_RESOURCE; i++)
1179 if (dev->resource[i].flags & IORESOURCE_BUSY)
1180 return &pci_compat_driver;
1181 }
1182 return NULL;
1183}
1184
1185/**
1186 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 * @dev: the PCI device structure to match against
Randy Dunlap8f7020d2005-10-23 11:57:38 -07001188 * @drv: the device driver to search for matching PCI device id structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 *
1190 * Used by a driver to check whether a PCI device present in the
Randy Dunlap8f7020d2005-10-23 11:57:38 -07001191 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 * pci_device_id structure or %NULL if there is no match.
1193 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001194static int pci_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001196 struct pci_dev *pci_dev = to_pci_dev(dev);
1197 struct pci_driver *pci_drv = to_pci_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 const struct pci_device_id *found_id;
1199
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001200 found_id = pci_match_device(pci_drv, pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (found_id)
1202 return 1;
1203
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
1207/**
1208 * pci_dev_get - increments the reference count of the pci device structure
1209 * @dev: the device being referenced
1210 *
1211 * Each live reference to a device should be refcounted.
1212 *
1213 * Drivers for PCI devices should normally record such references in
1214 * their probe() methods, when they bind to a device, and release
1215 * them by calling pci_dev_put(), in their disconnect() methods.
1216 *
1217 * A pointer to the device with the incremented reference counter is returned.
1218 */
1219struct pci_dev *pci_dev_get(struct pci_dev *dev)
1220{
1221 if (dev)
1222 get_device(&dev->dev);
1223 return dev;
1224}
1225
1226/**
1227 * pci_dev_put - release a use of the pci device structure
1228 * @dev: device that's been disconnected
1229 *
1230 * Must be called when a user of a device is finished with it. When the last
1231 * user of the device calls this function, the memory of the device is freed.
1232 */
1233void pci_dev_put(struct pci_dev *dev)
1234{
1235 if (dev)
1236 put_device(&dev->dev);
1237}
1238
1239#ifndef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +02001240int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 return -ENODEV;
1243}
1244#endif
1245
1246struct bus_type pci_bus_type = {
1247 .name = "pci",
1248 .match = pci_bus_match,
Kay Sievers312c0042005-11-16 09:00:00 +01001249 .uevent = pci_uevent,
Russell Kingb15d6862006-01-05 14:30:22 +00001250 .probe = pci_device_probe,
1251 .remove = pci_device_remove,
Linus Torvaldscbd69db2006-06-24 14:50:29 -07001252 .shutdown = pci_device_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 .dev_attrs = pci_dev_attrs,
Alex Chiang705b1aa2009-03-20 14:56:31 -06001254 .bus_attrs = pci_bus_attrs,
Konstantin Khlebnikovbfb09a82012-08-08 14:47:51 +04001255 .drv_attrs = pci_drv_attrs,
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001256 .pm = PCI_PM_OPS_PTR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257};
1258
1259static int __init pci_driver_init(void)
1260{
1261 return bus_register(&pci_bus_type);
1262}
1263
1264postcore_initcall(pci_driver_init);
1265
Tejun Heo9dba9102009-09-03 15:26:36 +09001266EXPORT_SYMBOL_GPL(pci_add_dynid);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001267EXPORT_SYMBOL(pci_match_id);
Laurent riffard863b18f2005-10-27 23:12:54 +02001268EXPORT_SYMBOL(__pci_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269EXPORT_SYMBOL(pci_unregister_driver);
1270EXPORT_SYMBOL(pci_dev_driver);
1271EXPORT_SYMBOL(pci_bus_type);
1272EXPORT_SYMBOL(pci_dev_get);
1273EXPORT_SYMBOL(pci_dev_put);