blob: 66aabde827277dd533b47b7ba9bfd5ddf5ff1888 [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. Wysockieea3fc02011-07-06 10:51:40 +020021#include <linux/suspend.h>
Khalid Aziz2a038882013-11-27 15:19:25 -070022#include <linux/kexec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "pci.h"
24
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070025struct pci_dynid {
26 struct list_head node;
27 struct pci_device_id id;
28};
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/**
Tejun Heo9dba9102009-09-03 15:26:36 +090031 * pci_add_dynid - add a new PCI device ID to this driver and re-probe devices
32 * @drv: target pci driver
33 * @vendor: PCI vendor ID
34 * @device: PCI device ID
35 * @subvendor: PCI subvendor ID
36 * @subdevice: PCI subdevice ID
37 * @class: PCI class
38 * @class_mask: PCI class mask
39 * @driver_data: private driver data
40 *
41 * Adds a new dynamic pci device ID to this driver and causes the
42 * driver to probe for all devices again. @drv must have been
43 * registered prior to calling this function.
44 *
45 * CONTEXT:
46 * Does GFP_KERNEL allocation.
47 *
48 * RETURNS:
49 * 0 on success, -errno on failure.
50 */
51int pci_add_dynid(struct pci_driver *drv,
52 unsigned int vendor, unsigned int device,
53 unsigned int subvendor, unsigned int subdevice,
54 unsigned int class, unsigned int class_mask,
55 unsigned long driver_data)
56{
57 struct pci_dynid *dynid;
58 int retval;
59
60 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
61 if (!dynid)
62 return -ENOMEM;
63
64 dynid->id.vendor = vendor;
65 dynid->id.device = device;
66 dynid->id.subvendor = subvendor;
67 dynid->id.subdevice = subdevice;
68 dynid->id.class = class;
69 dynid->id.class_mask = class_mask;
70 dynid->id.driver_data = driver_data;
71
72 spin_lock(&drv->dynids.lock);
73 list_add_tail(&dynid->node, &drv->dynids.list);
74 spin_unlock(&drv->dynids.lock);
75
Tejun Heo9dba9102009-09-03 15:26:36 +090076 retval = driver_attach(&drv->driver);
Tejun Heo9dba9102009-09-03 15:26:36 +090077
78 return retval;
79}
80
81static void pci_free_dynids(struct pci_driver *drv)
82{
83 struct pci_dynid *dynid, *n;
84
85 spin_lock(&drv->dynids.lock);
86 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
87 list_del(&dynid->node);
88 kfree(dynid);
89 }
90 spin_unlock(&drv->dynids.lock);
91}
92
Tejun Heo9dba9102009-09-03 15:26:36 +090093/**
94 * store_new_id - sysfs frontend to pci_add_dynid()
Randy Dunlap8f7020d2005-10-23 11:57:38 -070095 * @driver: target device driver
96 * @buf: buffer for scanning device ID data
97 * @count: input size
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
Tejun Heo9dba9102009-09-03 15:26:36 +090099 * Allow PCI IDs to be added to an existing driver via sysfs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
Randy Dunlapf8eb1002005-10-28 20:36:51 -0700101static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102store_new_id(struct device_driver *driver, const char *buf, size_t count)
103{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct pci_driver *pdrv = to_pci_driver(driver);
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200105 const struct pci_device_id *ids = pdrv->id_table;
Jean Delvare6ba18632007-04-07 17:21:28 +0200106 __u32 vendor, device, subvendor=PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 subdevice=PCI_ANY_ID, class=0, class_mask=0;
108 unsigned long driver_data=0;
109 int fields=0;
Tejun Heo9dba9102009-09-03 15:26:36 +0900110 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200112 fields = sscanf(buf, "%x %x %x %x %x %x %lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 &vendor, &device, &subvendor, &subdevice,
114 &class, &class_mask, &driver_data);
Jean Delvare6ba18632007-04-07 17:21:28 +0200115 if (fields < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return -EINVAL;
117
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200118 /* Only accept driver_data values that match an existing id_table
119 entry */
Chris Wright2debb4d2008-11-25 19:36:10 -0800120 if (ids) {
121 retval = -EINVAL;
122 while (ids->vendor || ids->subvendor || ids->class_mask) {
123 if (driver_data == ids->driver_data) {
124 retval = 0;
125 break;
126 }
127 ids++;
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200128 }
Chris Wright2debb4d2008-11-25 19:36:10 -0800129 if (retval) /* No match */
130 return retval;
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200131 }
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200132
Tejun Heo9dba9102009-09-03 15:26:36 +0900133 retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice,
134 class, class_mask, driver_data);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700135 if (retval)
136 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return count;
138}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Chris Wright09943752009-02-23 21:52:23 -0800140/**
141 * store_remove_id - remove a PCI device ID from this driver
142 * @driver: target device driver
143 * @buf: buffer for scanning device ID data
144 * @count: input size
145 *
146 * Removes a dynamic pci device ID to this driver.
147 */
148static ssize_t
149store_remove_id(struct device_driver *driver, const char *buf, size_t count)
150{
151 struct pci_dynid *dynid, *n;
152 struct pci_driver *pdrv = to_pci_driver(driver);
153 __u32 vendor, device, subvendor = PCI_ANY_ID,
154 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
155 int fields = 0;
156 int retval = -ENODEV;
157
158 fields = sscanf(buf, "%x %x %x %x %x %x",
159 &vendor, &device, &subvendor, &subdevice,
160 &class, &class_mask);
161 if (fields < 2)
162 return -EINVAL;
163
164 spin_lock(&pdrv->dynids.lock);
165 list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) {
166 struct pci_device_id *id = &dynid->id;
167 if ((id->vendor == vendor) &&
168 (id->device == device) &&
169 (subvendor == PCI_ANY_ID || id->subvendor == subvendor) &&
170 (subdevice == PCI_ANY_ID || id->subdevice == subdevice) &&
171 !((id->class ^ class) & class_mask)) {
172 list_del(&dynid->node);
173 kfree(dynid);
174 retval = 0;
175 break;
176 }
177 }
178 spin_unlock(&pdrv->dynids.lock);
179
180 if (retval)
181 return retval;
182 return count;
183}
Chris Wright09943752009-02-23 21:52:23 -0800184
Konstantin Khlebnikovbfb09a82012-08-08 14:47:51 +0400185static struct driver_attribute pci_drv_attrs[] = {
186 __ATTR(new_id, S_IWUSR, NULL, store_new_id),
187 __ATTR(remove_id, S_IWUSR, NULL, store_remove_id),
188 __ATTR_NULL,
189};
Alan Sterned283e92012-01-24 14:35:13 -0500190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/**
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700192 * pci_match_id - See if a pci device matches a given pci_id table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * @ids: array of PCI device id structures to search in
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700194 * @dev: the PCI device structure to match against.
195 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * Used by a driver to check whether a PCI device present in the
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700197 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * pci_device_id structure or %NULL if there is no match.
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700199 *
Randy Dunlap8b607562007-05-09 07:19:14 +0200200 * Deprecated, don't use this as it will not catch any dynamic ids
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700201 * that a driver might want to check for.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700203const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
204 struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700206 if (ids) {
207 while (ids->vendor || ids->subvendor || ids->class_mask) {
208 if (pci_match_one_device(ids, dev))
209 return ids;
210 ids++;
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213 return NULL;
214}
215
216/**
Randy Dunlapae9608a2007-01-09 21:41:01 -0800217 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700218 * @drv: the PCI driver to match against
Henrik Kretzschmar39ba4872006-08-15 10:57:16 +0200219 * @dev: the PCI device structure to match against
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700220 *
221 * Used by a driver to check whether a PCI device present in the
222 * system is in its list of supported devices. Returns the matching
223 * pci_device_id structure or %NULL if there is no match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
Adrian Bunkd73460d2007-10-24 18:27:18 +0200225static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
226 struct pci_dev *dev)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700227{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700228 struct pci_dynid *dynid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Russell King7461b602006-11-29 21:18:04 +0000230 /* Look at the dynamic ids first, before the static ones */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700231 spin_lock(&drv->dynids.lock);
232 list_for_each_entry(dynid, &drv->dynids.list, node) {
233 if (pci_match_one_device(&dynid->id, dev)) {
234 spin_unlock(&drv->dynids.lock);
235 return &dynid->id;
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700238 spin_unlock(&drv->dynids.lock);
Russell King7461b602006-11-29 21:18:04 +0000239
240 return pci_match_id(drv->id_table, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Rusty Russell873392c2008-12-31 23:54:56 +1030243struct drv_dev_and_id {
244 struct pci_driver *drv;
245 struct pci_dev *dev;
246 const struct pci_device_id *id;
247};
248
249static long local_pci_probe(void *_ddi)
250{
251 struct drv_dev_and_id *ddi = _ddi;
Huang Ying967577b2012-11-20 16:08:22 +0800252 struct pci_dev *pci_dev = ddi->dev;
253 struct pci_driver *pci_drv = ddi->drv;
254 struct device *dev = &pci_dev->dev;
Alan Sternf3ec4f82010-06-08 15:23:51 -0400255 int rc;
Rusty Russell873392c2008-12-31 23:54:56 +1030256
Huang Ying967577b2012-11-20 16:08:22 +0800257 /*
258 * Unbound PCI devices are always put in D0, regardless of
259 * runtime PM status. During probe, the device is set to
260 * active and the usage count is incremented. If the driver
261 * supports runtime PM, it should call pm_runtime_put_noidle()
262 * in its probe routine and pm_runtime_get_noresume() in its
263 * remove routine.
Alan Sternf3ec4f82010-06-08 15:23:51 -0400264 */
Huang Ying967577b2012-11-20 16:08:22 +0800265 pm_runtime_get_sync(dev);
266 pci_dev->driver = pci_drv;
267 rc = pci_drv->probe(pci_dev, ddi->id);
Alan Sternf3ec4f82010-06-08 15:23:51 -0400268 if (rc) {
Huang Ying967577b2012-11-20 16:08:22 +0800269 pci_dev->driver = NULL;
270 pm_runtime_put_sync(dev);
Alan Sternf3ec4f82010-06-08 15:23:51 -0400271 }
272 return rc;
Rusty Russell873392c2008-12-31 23:54:56 +1030273}
274
Andi Kleend42c6992005-07-06 19:56:03 +0200275static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
276 const struct pci_device_id *id)
277{
Rusty Russell873392c2008-12-31 23:54:56 +1030278 int error, node;
279 struct drv_dev_and_id ddi = { drv, dev, id };
Mike Travisf70316d2008-04-04 18:11:06 -0700280
Rusty Russell873392c2008-12-31 23:54:56 +1030281 /* Execute driver initialization on node where the device's
282 bus is attached to. This way the driver likely allocates
283 its local memory on the right node without any need to
284 change it. */
285 node = dev_to_node(&dev->dev);
Mike Travisf70316d2008-04-04 18:11:06 -0700286 if (node >= 0) {
Rusty Russell873392c2008-12-31 23:54:56 +1030287 int cpu;
Rusty Russell873392c2008-12-31 23:54:56 +1030288
289 get_online_cpus();
Rusty Russella70f7302009-03-13 14:49:46 +1030290 cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
Rusty Russell873392c2008-12-31 23:54:56 +1030291 if (cpu < nr_cpu_ids)
292 error = work_on_cpu(cpu, local_pci_probe, &ddi);
293 else
294 error = local_pci_probe(&ddi);
295 put_online_cpus();
296 } else
297 error = local_pci_probe(&ddi);
Andi Kleend42c6992005-07-06 19:56:03 +0200298 return error;
299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/**
Randy Dunlap23ea3792010-11-18 15:02:31 -0800302 * __pci_device_probe - check if a driver wants to claim a specific PCI device
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700303 * @drv: driver to call to check if it wants the PCI device
304 * @pci_dev: PCI device being probed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 *
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700306 * returns 0 on success, else error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
308 */
309static int
310__pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700311{
312 const struct pci_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int error = 0;
314
315 if (!pci_dev->driver && drv->probe) {
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700316 error = -ENODEV;
317
318 id = pci_match_device(drv, pci_dev);
319 if (id)
Andi Kleend42c6992005-07-06 19:56:03 +0200320 error = pci_call_probe(drv, pci_dev, id);
Huang Ying967577b2012-11-20 16:08:22 +0800321 if (error >= 0)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700322 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324 return error;
325}
326
327static int pci_device_probe(struct device * dev)
328{
329 int error = 0;
330 struct pci_driver *drv;
331 struct pci_dev *pci_dev;
332
333 drv = to_pci_driver(dev->driver);
334 pci_dev = to_pci_dev(dev);
335 pci_dev_get(pci_dev);
336 error = __pci_device_probe(drv, pci_dev);
337 if (error)
338 pci_dev_put(pci_dev);
339
340 return error;
341}
342
343static int pci_device_remove(struct device * dev)
344{
345 struct pci_dev * pci_dev = to_pci_dev(dev);
346 struct pci_driver * drv = pci_dev->driver;
347
348 if (drv) {
Alan Sternf3ec4f82010-06-08 15:23:51 -0400349 if (drv->remove) {
350 pm_runtime_get_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 drv->remove(pci_dev);
Alan Sternf3ec4f82010-06-08 15:23:51 -0400352 pm_runtime_put_noidle(dev);
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 pci_dev->driver = NULL;
355 }
356
Alan Sternf3ec4f82010-06-08 15:23:51 -0400357 /* Undo the runtime PM settings in local_pci_probe() */
Huang Ying967577b2012-11-20 16:08:22 +0800358 pm_runtime_put_sync(dev);
Alan Sternf3ec4f82010-06-08 15:23:51 -0400359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /*
Shaohua Li2449e062006-10-20 14:45:32 -0700361 * If the device is still on, set the power state as "unknown",
362 * since it might change by the next time we load the driver.
363 */
364 if (pci_dev->current_state == PCI_D0)
365 pci_dev->current_state = PCI_UNKNOWN;
366
367 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * We would love to complain here if pci_dev->is_enabled is set, that
369 * the driver should have called pci_disable_device(), but the
370 * unfortunate fact is there are too many odd BIOS and bridge setups
371 * that don't like drivers doing that all of the time.
372 * Oh well, we can dream of sane hardware when we sleep, no matter how
373 * horrible the crap we have to deal with is when we are awake...
374 */
375
376 pci_dev_put(pci_dev);
377 return 0;
378}
379
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200380static void pci_device_shutdown(struct device *dev)
381{
382 struct pci_dev *pci_dev = to_pci_dev(dev);
383 struct pci_driver *drv = pci_dev->driver;
384
Huang Ying3ff2de92012-10-24 14:54:14 +0800385 pm_runtime_resume(dev);
386
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200387 if (drv && drv->shutdown)
388 drv->shutdown(pci_dev);
389 pci_msi_shutdown(pci_dev);
390 pci_msix_shutdown(pci_dev);
Rafael J. Wysocki5b415f12012-02-07 00:50:35 +0100391
Khalid Aziz2a038882013-11-27 15:19:25 -0700392#ifdef CONFIG_KEXEC
Rafael J. Wysocki5b415f12012-02-07 00:50:35 +0100393 /*
Khalid Aziz2a038882013-11-27 15:19:25 -0700394 * If this is a kexec reboot, turn off Bus Master bit on the
395 * device to tell it to not continue to do DMA. Don't touch
396 * devices in D3cold or unknown states.
397 * If it is not a kexec reboot, firmware will hit the PCI
398 * devices with big hammer and stop their DMA any way.
Khalid Azizb566a222012-04-27 13:00:33 -0600399 */
Khalid Aziz2a038882013-11-27 15:19:25 -0700400 if (kexec_in_progress && (pci_dev->current_state <= PCI_D3hot))
Konstantin Khlebnikov6e0eda32013-03-14 18:49:37 +0400401 pci_clear_master(pci_dev);
Khalid Aziz2a038882013-11-27 15:19:25 -0700402#endif
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200403}
404
Rafael J. Wysockiaa338602011-02-11 00:06:54 +0100405#ifdef CONFIG_PM
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100406
407/* Auxiliary functions used for system resume and run-time resume. */
408
409/**
410 * pci_restore_standard_config - restore standard config registers of PCI device
411 * @pci_dev: PCI device to handle
412 */
413static int pci_restore_standard_config(struct pci_dev *pci_dev)
414{
415 pci_update_current_state(pci_dev, PCI_UNKNOWN);
416
417 if (pci_dev->current_state != PCI_D0) {
418 int error = pci_set_power_state(pci_dev, PCI_D0);
419 if (error)
420 return error;
421 }
422
Jon Mason1d3c16a2010-11-30 17:43:26 -0600423 pci_restore_state(pci_dev);
424 return 0;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100425}
426
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100427#endif
428
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200429#ifdef CONFIG_PM_SLEEP
430
Rafael J. Wysockidb288c92012-07-05 15:20:00 -0600431static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
432{
433 pci_power_up(pci_dev);
434 pci_restore_state(pci_dev);
435 pci_fixup_device(pci_fixup_resume_early, pci_dev);
436}
437
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200438/*
439 * Default "suspend" method for devices that have no driver provided suspend,
Rafael J. Wysockifa58d302009-01-07 13:03:42 +0100440 * or not even a driver at all (second part).
441 */
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100442static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200443{
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200444 /*
445 * mark its power state as "unknown", since we don't know if
446 * e.g. the BIOS will change its device state when we suspend.
447 */
448 if (pci_dev->current_state == PCI_D0)
449 pci_dev->current_state = PCI_UNKNOWN;
450}
451
452/*
453 * Default "resume" method for devices that have no driver provided resume,
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100454 * or not even a driver at all (second part).
455 */
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100456static int pci_pm_reenable_device(struct pci_dev *pci_dev)
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100457{
458 int retval;
459
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200460 /* if the device was enabled before suspend, reenable */
461 retval = pci_reenable_device(pci_dev);
462 /*
463 * if the device was busmaster before the suspend, make it busmaster
464 * again
465 */
466 if (pci_dev->is_busmaster)
467 pci_set_master(pci_dev);
468
469 return retval;
470}
471
472static int pci_legacy_suspend(struct device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 struct pci_dev * pci_dev = to_pci_dev(dev);
475 struct pci_driver * drv = pci_dev->driver;
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100476
Andrew Morton02669492006-03-23 01:38:34 -0800477 if (drv && drv->suspend) {
Rafael J. Wysocki99dadce2009-02-04 01:59:09 +0100478 pci_power_t prev = pci_dev->current_state;
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100479 int error;
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100480
Frans Pop57ef8022009-03-16 22:39:56 +0100481 error = drv->suspend(pci_dev, state);
482 suspend_report_result(drv->suspend, error);
483 if (error)
484 return error;
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100485
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100486 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
Rafael J. Wysocki99dadce2009-02-04 01:59:09 +0100487 && pci_dev->current_state != PCI_UNKNOWN) {
488 WARN_ONCE(pci_dev->current_state != prev,
489 "PCI PM: Device state not saved by %pF\n",
490 drv->suspend);
Rafael J. Wysocki99dadce2009-02-04 01:59:09 +0100491 }
Andrew Morton02669492006-03-23 01:38:34 -0800492 }
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100493
494 pci_fixup_device(pci_fixup_suspend, pci_dev);
495
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200499static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700500{
501 struct pci_dev * pci_dev = to_pci_dev(dev);
502 struct pci_driver * drv = pci_dev->driver;
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700503
504 if (drv && drv->suspend_late) {
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100505 pci_power_t prev = pci_dev->current_state;
506 int error;
507
Frans Pop57ef8022009-03-16 22:39:56 +0100508 error = drv->suspend_late(pci_dev, state);
509 suspend_report_result(drv->suspend_late, error);
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100510 if (error)
511 return error;
512
513 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
514 && pci_dev->current_state != PCI_UNKNOWN) {
515 WARN_ONCE(pci_dev->current_state != prev,
516 "PCI PM: Device state not saved by %pF\n",
517 drv->suspend_late);
518 return 0;
519 }
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700520 }
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100521
522 if (!pci_dev->state_saved)
523 pci_save_state(pci_dev);
524
525 pci_pm_set_unknown_state(pci_dev);
526
527 return 0;
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700528}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100530static int pci_legacy_resume_early(struct device *dev)
531{
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100532 struct pci_dev * pci_dev = to_pci_dev(dev);
533 struct pci_driver * drv = pci_dev->driver;
534
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100535 return drv && drv->resume_early ?
536 drv->resume_early(pci_dev) : 0;
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100537}
538
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200539static int pci_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 struct pci_dev * pci_dev = to_pci_dev(dev);
542 struct pci_driver * drv = pci_dev->driver;
543
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100544 pci_fixup_device(pci_fixup_resume, pci_dev);
545
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100546 return drv && drv->resume ?
547 drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Rafael J. Wysocki571ff752009-01-07 13:05:05 +0100550/* Auxiliary functions used by the new power management framework */
551
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100552static void pci_pm_default_resume(struct pci_dev *pci_dev)
Rafael J. Wysocki571ff752009-01-07 13:05:05 +0100553{
Rafael J. Wysocki734104292009-01-07 13:07:15 +0100554 pci_fixup_device(pci_fixup_resume, pci_dev);
555
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100556 if (!pci_is_bridge(pci_dev))
557 pci_enable_wake(pci_dev, PCI_D0, false);
Rafael J. Wysocki571ff752009-01-07 13:05:05 +0100558}
559
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100560static void pci_pm_default_suspend(struct pci_dev *pci_dev)
Rafael J. Wysocki734104292009-01-07 13:07:15 +0100561{
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100562 /* Disable non-bridge devices without PM support */
Rafael J. Wysockicbbc2f62009-02-04 02:01:15 +0100563 if (!pci_is_bridge(pci_dev))
564 pci_disable_enabled_device(pci_dev);
Rafael J. Wysocki734104292009-01-07 13:07:15 +0100565}
566
Rafael J. Wysocki07e836e2009-01-07 13:06:10 +0100567static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
568{
569 struct pci_driver *drv = pci_dev->driver;
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100570 bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
Rafael J. Wysocki07e836e2009-01-07 13:06:10 +0100571 || drv->resume_early);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100572
573 /*
574 * Legacy PM support is used by default, so warn if the new framework is
575 * supported as well. Drivers are supposed to support either the
576 * former, or the latter, but not both at the same time.
577 */
David Fries82440a82011-11-20 15:29:46 -0600578 WARN(ret && drv->driver.pm, "driver %s device %04x:%04x\n",
579 drv->name, pci_dev->vendor, pci_dev->device);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100580
581 return ret;
Rafael J. Wysocki07e836e2009-01-07 13:06:10 +0100582}
583
Rafael J. Wysocki571ff752009-01-07 13:05:05 +0100584/* New power management framework */
585
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200586static int pci_pm_prepare(struct device *dev)
587{
588 struct device_driver *drv = dev->driver;
589 int error = 0;
590
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100591 /*
592 * PCI devices suspended at run time need to be resumed at this
593 * point, because in general it is necessary to reconfigure them for
594 * system suspend. Namely, if the device is supposed to wake up the
595 * system from the sleep state, we may need to reconfigure it for this
596 * purpose. In turn, if the device is not supposed to wake up the
597 * system from the sleep state, we'll have to prevent it from signaling
598 * wake-up.
599 */
Rafael J. Wysockieea3fc02011-07-06 10:51:40 +0200600 pm_runtime_resume(dev);
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100601
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200602 if (drv && drv->pm && drv->pm->prepare)
603 error = drv->pm->prepare(dev);
604
605 return error;
606}
607
608static void pci_pm_complete(struct device *dev)
609{
610 struct device_driver *drv = dev->driver;
611
612 if (drv && drv->pm && drv->pm->complete)
613 drv->pm->complete(dev);
614}
615
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100616#else /* !CONFIG_PM_SLEEP */
617
618#define pci_pm_prepare NULL
619#define pci_pm_complete NULL
620
621#endif /* !CONFIG_PM_SLEEP */
622
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200623#ifdef CONFIG_SUSPEND
624
625static int pci_pm_suspend(struct device *dev)
626{
627 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700628 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200629
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100630 if (pci_has_legacy_pm_support(pci_dev))
631 return pci_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100632
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100633 if (!pm) {
634 pci_pm_default_suspend(pci_dev);
635 goto Fixup;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200636 }
Rafael J. Wysockifa58d302009-01-07 13:03:42 +0100637
Rafael J. Wysocki82fee4d2013-02-04 15:56:05 +0400638 pci_dev->state_saved = false;
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. Wysocki82fee4d2013-02-04 15:56:05 +0400785 pci_dev->state_saved = false;
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100786 if (pm->freeze) {
787 int error;
788
789 error = pm->freeze(dev);
790 suspend_report_result(pm->freeze, error);
791 if (error)
792 return error;
793 }
794
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100795 return 0;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200796}
797
798static int pci_pm_freeze_noirq(struct device *dev)
799{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100800 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200801 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200802
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100803 if (pci_has_legacy_pm_support(pci_dev))
804 return pci_legacy_suspend_late(dev, PMSG_FREEZE);
805
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100806 if (drv && drv->pm && drv->pm->freeze_noirq) {
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100807 int error;
808
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100809 error = drv->pm->freeze_noirq(dev);
810 suspend_report_result(drv->pm->freeze_noirq, error);
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100811 if (error)
812 return error;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200813 }
814
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100815 if (!pci_dev->state_saved)
816 pci_save_state(pci_dev);
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100817
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100818 pci_pm_set_unknown_state(pci_dev);
819
820 return 0;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200821}
822
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200823static int pci_pm_thaw_noirq(struct device *dev)
824{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100825 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200826 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200827 int error = 0;
828
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100829 if (pci_has_legacy_pm_support(pci_dev))
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100830 return pci_legacy_resume_early(dev);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100831
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100832 pci_update_current_state(pci_dev, PCI_D0);
833
834 if (drv && drv->pm && drv->pm->thaw_noirq)
835 error = drv->pm->thaw_noirq(dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200836
837 return error;
838}
839
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100840static int pci_pm_thaw(struct device *dev)
841{
842 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700843 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100844 int error = 0;
845
846 if (pci_has_legacy_pm_support(pci_dev))
847 return pci_legacy_resume(dev);
848
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100849 if (pm) {
850 if (pm->thaw)
851 error = pm->thaw(dev);
852 } else {
853 pci_pm_reenable_device(pci_dev);
854 }
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100855
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +0200856 pci_dev->state_saved = false;
857
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100858 return error;
859}
860
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200861static int pci_pm_poweroff(struct device *dev)
862{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100863 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700864 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200865
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100866 if (pci_has_legacy_pm_support(pci_dev))
867 return pci_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100868
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100869 if (!pm) {
870 pci_pm_default_suspend(pci_dev);
871 goto Fixup;
872 }
873
Rafael J. Wysocki82fee4d2013-02-04 15:56:05 +0400874 pci_dev->state_saved = false;
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100875 if (pm->poweroff) {
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100876 int error;
877
Rafael J. Wysockiddb7c9d2009-02-04 01:56:14 +0100878 error = pm->poweroff(dev);
879 suspend_report_result(pm->poweroff, error);
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100880 if (error)
881 return error;
882 }
883
884 Fixup:
885 pci_fixup_device(pci_fixup_suspend, pci_dev);
886
887 return 0;
888}
889
890static int pci_pm_poweroff_noirq(struct device *dev)
891{
892 struct pci_dev *pci_dev = to_pci_dev(dev);
893 struct device_driver *drv = dev->driver;
894
895 if (pci_has_legacy_pm_support(to_pci_dev(dev)))
896 return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
897
898 if (!drv || !drv->pm)
899 return 0;
900
901 if (drv->pm->poweroff_noirq) {
902 int error;
903
904 error = drv->pm->poweroff_noirq(dev);
905 suspend_report_result(drv->pm->poweroff_noirq, error);
906 if (error)
907 return error;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200908 }
909
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100910 if (!pci_dev->state_saved && !pci_is_bridge(pci_dev))
911 pci_prepare_to_sleep(pci_dev);
912
Rafael J. Wysocki0b68c8e2012-08-12 23:26:07 +0200913 /*
914 * The reason for doing this here is the same as for the analogous code
915 * in pci_pm_suspend_noirq().
916 */
917 if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
918 pci_write_config_word(pci_dev, PCI_COMMAND, 0);
919
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100920 return 0;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200921}
922
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200923static int pci_pm_restore_noirq(struct device *dev)
924{
925 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200926 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200927 int error = 0;
928
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100929 pci_pm_default_resume_early(pci_dev);
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100930
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100931 if (pci_has_legacy_pm_support(pci_dev))
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100932 return pci_legacy_resume_early(dev);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100933
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100934 if (drv && drv->pm && drv->pm->restore_noirq)
935 error = drv->pm->restore_noirq(dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200936
937 return error;
938}
939
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100940static int pci_pm_restore(struct device *dev)
941{
942 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700943 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100944 int error = 0;
945
Rafael J. Wysocki418e4da2009-01-26 21:43:08 +0100946 /*
947 * This is necessary for the hibernation error path in which restore is
948 * called without restoring the standard config registers of the device.
949 */
950 if (pci_dev->state_saved)
951 pci_restore_standard_config(pci_dev);
952
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100953 if (pci_has_legacy_pm_support(pci_dev))
954 return pci_legacy_resume(dev);
955
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100956 pci_pm_default_resume(pci_dev);
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100957
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100958 if (pm) {
959 if (pm->restore)
960 error = pm->restore(dev);
961 } else {
962 pci_pm_reenable_device(pci_dev);
963 }
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100964
965 return error;
966}
967
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200968#else /* !CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200969
970#define pci_pm_freeze NULL
971#define pci_pm_freeze_noirq NULL
972#define pci_pm_thaw NULL
973#define pci_pm_thaw_noirq NULL
974#define pci_pm_poweroff NULL
975#define pci_pm_poweroff_noirq NULL
976#define pci_pm_restore NULL
977#define pci_pm_restore_noirq NULL
978
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200979#endif /* !CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200980
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100981#ifdef CONFIG_PM_RUNTIME
982
983static int pci_pm_runtime_suspend(struct device *dev)
984{
985 struct pci_dev *pci_dev = to_pci_dev(dev);
986 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
987 pci_power_t prev = pci_dev->current_state;
988 int error;
989
Huang Ying967577b2012-11-20 16:08:22 +0800990 /*
991 * If pci_dev->driver is not set (unbound), the device should
992 * always remain in D0 regardless of the runtime PM status
993 */
994 if (!pci_dev->driver)
995 return 0;
996
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100997 if (!pm || !pm->runtime_suspend)
998 return -ENOSYS;
999
Rafael J. Wysocki82fee4d2013-02-04 15:56:05 +04001000 pci_dev->state_saved = false;
Huang Ying448bd852012-06-23 10:23:51 +08001001 pci_dev->no_d3cold = false;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001002 error = pm->runtime_suspend(dev);
1003 suspend_report_result(pm->runtime_suspend, error);
1004 if (error)
1005 return error;
Huang Ying448bd852012-06-23 10:23:51 +08001006 if (!pci_dev->d3cold_allowed)
1007 pci_dev->no_d3cold = true;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001008
1009 pci_fixup_device(pci_fixup_suspend, pci_dev);
1010
1011 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
1012 && pci_dev->current_state != PCI_UNKNOWN) {
1013 WARN_ONCE(pci_dev->current_state != prev,
1014 "PCI PM: State of device not saved by %pF\n",
1015 pm->runtime_suspend);
1016 return 0;
1017 }
1018
Dave Airlie42eca232012-10-29 17:26:54 -06001019 if (!pci_dev->state_saved) {
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001020 pci_save_state(pci_dev);
Dave Airlie42eca232012-10-29 17:26:54 -06001021 pci_finish_runtime_suspend(pci_dev);
1022 }
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001023
1024 return 0;
1025}
1026
1027static int pci_pm_runtime_resume(struct device *dev)
1028{
Huang Ying448bd852012-06-23 10:23:51 +08001029 int rc;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001030 struct pci_dev *pci_dev = to_pci_dev(dev);
1031 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1032
Huang Ying967577b2012-11-20 16:08:22 +08001033 /*
1034 * If pci_dev->driver is not set (unbound), the device should
1035 * always remain in D0 regardless of the runtime PM status
1036 */
1037 if (!pci_dev->driver)
1038 return 0;
1039
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001040 if (!pm || !pm->runtime_resume)
1041 return -ENOSYS;
1042
Rafael J. Wysockidb288c92012-07-05 15:20:00 -06001043 pci_restore_standard_config(pci_dev);
1044 pci_fixup_device(pci_fixup_resume_early, pci_dev);
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001045 __pci_enable_wake(pci_dev, PCI_D0, true, false);
1046 pci_fixup_device(pci_fixup_resume, pci_dev);
1047
Huang Ying448bd852012-06-23 10:23:51 +08001048 rc = pm->runtime_resume(dev);
1049
1050 pci_dev->runtime_d3cold = false;
1051
1052 return rc;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001053}
1054
1055static int pci_pm_runtime_idle(struct device *dev)
1056{
Huang Ying967577b2012-11-20 16:08:22 +08001057 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001058 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1059
Huang Ying967577b2012-11-20 16:08:22 +08001060 /*
1061 * If pci_dev->driver is not set (unbound), the device should
1062 * always remain in D0 regardless of the runtime PM status
1063 */
1064 if (!pci_dev->driver)
1065 goto out;
1066
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001067 if (!pm)
1068 return -ENOSYS;
1069
1070 if (pm->runtime_idle) {
1071 int ret = pm->runtime_idle(dev);
1072 if (ret)
1073 return ret;
1074 }
1075
Huang Ying967577b2012-11-20 16:08:22 +08001076out:
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001077 pm_runtime_suspend(dev);
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001078 return 0;
1079}
1080
1081#else /* !CONFIG_PM_RUNTIME */
1082
1083#define pci_pm_runtime_suspend NULL
1084#define pci_pm_runtime_resume NULL
1085#define pci_pm_runtime_idle NULL
1086
1087#endif /* !CONFIG_PM_RUNTIME */
1088
Rafael J. Wysockiaa338602011-02-11 00:06:54 +01001089#ifdef CONFIG_PM
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001090
Dmitry Torokhov8150f322009-07-24 22:11:32 -07001091const struct dev_pm_ops pci_dev_pm_ops = {
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001092 .prepare = pci_pm_prepare,
1093 .complete = pci_pm_complete,
1094 .suspend = pci_pm_suspend,
1095 .resume = pci_pm_resume,
1096 .freeze = pci_pm_freeze,
1097 .thaw = pci_pm_thaw,
1098 .poweroff = pci_pm_poweroff,
1099 .restore = pci_pm_restore,
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001100 .suspend_noirq = pci_pm_suspend_noirq,
1101 .resume_noirq = pci_pm_resume_noirq,
1102 .freeze_noirq = pci_pm_freeze_noirq,
1103 .thaw_noirq = pci_pm_thaw_noirq,
1104 .poweroff_noirq = pci_pm_poweroff_noirq,
1105 .restore_noirq = pci_pm_restore_noirq,
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001106 .runtime_suspend = pci_pm_runtime_suspend,
1107 .runtime_resume = pci_pm_runtime_resume,
1108 .runtime_idle = pci_pm_runtime_idle,
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001109};
1110
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001111#define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001112
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001113#else /* !COMFIG_PM_OPS */
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001114
1115#define PCI_PM_OPS_PTR NULL
1116
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001117#endif /* !COMFIG_PM_OPS */
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119/**
Laurent riffard863b18f2005-10-27 23:12:54 +02001120 * __pci_register_driver - register a new pci driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 * @drv: the driver structure to register
Laurent riffard863b18f2005-10-27 23:12:54 +02001122 * @owner: owner module of drv
Randy Dunlapf95d8822007-02-10 14:41:56 -08001123 * @mod_name: module name string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 *
1125 * Adds the driver structure to the list of registered drivers.
1126 * Returns a negative value on error, otherwise 0.
Steven Coleeaae4b32005-05-03 18:38:30 -06001127 * If no error occurred, the driver remains registered even if
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 * no device was claimed during registration.
1129 */
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -08001130int __pci_register_driver(struct pci_driver *drv, struct module *owner,
1131 const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /* initialize common driver fields */
1134 drv->driver.name = drv->name;
1135 drv->driver.bus = &pci_bus_type;
Laurent riffard863b18f2005-10-27 23:12:54 +02001136 drv->driver.owner = owner;
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -08001137 drv->driver.mod_name = mod_name;
Alan Cox50b00752006-08-16 17:42:18 +01001138
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001139 spin_lock_init(&drv->dynids.lock);
1140 INIT_LIST_HEAD(&drv->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 /* register with core */
Konstantin Khlebnikovbfb09a82012-08-08 14:47:51 +04001143 return driver_register(&drv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146/**
1147 * pci_unregister_driver - unregister a pci driver
1148 * @drv: the driver structure to unregister
1149 *
1150 * Deletes the driver structure from the list of registered PCI drivers,
1151 * gives it a chance to clean up by calling its remove() function for
1152 * each device it was responsible for, and marks those devices as
1153 * driverless.
1154 */
1155
1156void
1157pci_unregister_driver(struct pci_driver *drv)
1158{
1159 driver_unregister(&drv->driver);
1160 pci_free_dynids(drv);
1161}
1162
1163static struct pci_driver pci_compat_driver = {
1164 .name = "compat"
1165};
1166
1167/**
1168 * pci_dev_driver - get the pci_driver of a device
1169 * @dev: the device to query
1170 *
1171 * Returns the appropriate pci_driver structure or %NULL if there is no
1172 * registered driver for the device.
1173 */
1174struct pci_driver *
1175pci_dev_driver(const struct pci_dev *dev)
1176{
1177 if (dev->driver)
1178 return dev->driver;
1179 else {
1180 int i;
1181 for(i=0; i<=PCI_ROM_RESOURCE; i++)
1182 if (dev->resource[i].flags & IORESOURCE_BUSY)
1183 return &pci_compat_driver;
1184 }
1185 return NULL;
1186}
1187
1188/**
1189 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 * @dev: the PCI device structure to match against
Randy Dunlap8f7020d2005-10-23 11:57:38 -07001191 * @drv: the device driver to search for matching PCI device id structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 *
1193 * Used by a driver to check whether a PCI device present in the
Randy Dunlap8f7020d2005-10-23 11:57:38 -07001194 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 * pci_device_id structure or %NULL if there is no match.
1196 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001197static int pci_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001199 struct pci_dev *pci_dev = to_pci_dev(dev);
Yinghai Lu58d9a382013-01-21 13:20:51 -08001200 struct pci_driver *pci_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 const struct pci_device_id *found_id;
1202
Yinghai Lu58d9a382013-01-21 13:20:51 -08001203 if (!pci_dev->match_driver)
1204 return 0;
1205
1206 pci_drv = to_pci_driver(drv);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001207 found_id = pci_match_device(pci_drv, pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (found_id)
1209 return 1;
1210
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214/**
1215 * pci_dev_get - increments the reference count of the pci device structure
1216 * @dev: the device being referenced
1217 *
1218 * Each live reference to a device should be refcounted.
1219 *
1220 * Drivers for PCI devices should normally record such references in
1221 * their probe() methods, when they bind to a device, and release
1222 * them by calling pci_dev_put(), in their disconnect() methods.
1223 *
1224 * A pointer to the device with the incremented reference counter is returned.
1225 */
1226struct pci_dev *pci_dev_get(struct pci_dev *dev)
1227{
1228 if (dev)
1229 get_device(&dev->dev);
1230 return dev;
1231}
1232
1233/**
1234 * pci_dev_put - release a use of the pci device structure
1235 * @dev: device that's been disconnected
1236 *
1237 * Must be called when a user of a device is finished with it. When the last
1238 * user of the device calls this function, the memory of the device is freed.
1239 */
1240void pci_dev_put(struct pci_dev *dev)
1241{
1242 if (dev)
1243 put_device(&dev->dev);
1244}
1245
Bill Pemberton8ccc9aa2012-11-21 15:34:58 -05001246static int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
1247{
1248 struct pci_dev *pdev;
1249
1250 if (!dev)
1251 return -ENODEV;
1252
1253 pdev = to_pci_dev(dev);
1254 if (!pdev)
1255 return -ENODEV;
1256
1257 if (add_uevent_var(env, "PCI_CLASS=%04X", pdev->class))
1258 return -ENOMEM;
1259
1260 if (add_uevent_var(env, "PCI_ID=%04X:%04X", pdev->vendor, pdev->device))
1261 return -ENOMEM;
1262
1263 if (add_uevent_var(env, "PCI_SUBSYS_ID=%04X:%04X", pdev->subsystem_vendor,
1264 pdev->subsystem_device))
1265 return -ENOMEM;
1266
1267 if (add_uevent_var(env, "PCI_SLOT_NAME=%s", pci_name(pdev)))
1268 return -ENOMEM;
1269
1270 if (add_uevent_var(env, "MODALIAS=pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x",
1271 pdev->vendor, pdev->device,
1272 pdev->subsystem_vendor, pdev->subsystem_device,
1273 (u8)(pdev->class >> 16), (u8)(pdev->class >> 8),
1274 (u8)(pdev->class)))
1275 return -ENOMEM;
1276 return 0;
1277}
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279struct bus_type pci_bus_type = {
1280 .name = "pci",
1281 .match = pci_bus_match,
Kay Sievers312c0042005-11-16 09:00:00 +01001282 .uevent = pci_uevent,
Russell Kingb15d6862006-01-05 14:30:22 +00001283 .probe = pci_device_probe,
1284 .remove = pci_device_remove,
Linus Torvaldscbd69db2006-06-24 14:50:29 -07001285 .shutdown = pci_device_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 .dev_attrs = pci_dev_attrs,
Alex Chiang705b1aa2009-03-20 14:56:31 -06001287 .bus_attrs = pci_bus_attrs,
Konstantin Khlebnikovbfb09a82012-08-08 14:47:51 +04001288 .drv_attrs = pci_drv_attrs,
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001289 .pm = PCI_PM_OPS_PTR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290};
1291
1292static int __init pci_driver_init(void)
1293{
1294 return bus_register(&pci_bus_type);
1295}
1296
1297postcore_initcall(pci_driver_init);
1298
Tejun Heo9dba9102009-09-03 15:26:36 +09001299EXPORT_SYMBOL_GPL(pci_add_dynid);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001300EXPORT_SYMBOL(pci_match_id);
Laurent riffard863b18f2005-10-27 23:12:54 +02001301EXPORT_SYMBOL(__pci_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302EXPORT_SYMBOL(pci_unregister_driver);
1303EXPORT_SYMBOL(pci_dev_driver);
1304EXPORT_SYMBOL(pci_bus_type);
1305EXPORT_SYMBOL(pci_dev_get);
1306EXPORT_SYMBOL(pci_dev_put);