Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * drivers/pci/pci-driver.c | |
3 | * | |
2b937303 GKH |
4 | * (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 | * | |
1da177e4 LT |
9 | */ |
10 | ||
11 | #include <linux/pci.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/device.h> | |
d42c6997 | 15 | #include <linux/mempolicy.h> |
4e57b681 TS |
16 | #include <linux/string.h> |
17 | #include <linux/slab.h> | |
8c65b4a6 | 18 | #include <linux/sched.h> |
873392ca | 19 | #include <linux/cpu.h> |
6cbf8214 | 20 | #include <linux/pm_runtime.h> |
eea3fc03 | 21 | #include <linux/suspend.h> |
2a038881 | 22 | #include <linux/kexec.h> |
1da177e4 LT |
23 | #include "pci.h" |
24 | ||
75865858 GKH |
25 | struct pci_dynid { |
26 | struct list_head node; | |
27 | struct pci_device_id id; | |
28 | }; | |
1da177e4 | 29 | |
9dba910e TH |
30 | /** |
31 | * 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 | */ | |
51 | int 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; | |
3d3c2ae1 | 71 | |
9dba910e TH |
72 | spin_lock(&drv->dynids.lock); |
73 | list_add_tail(&dynid->node, &drv->dynids.list); | |
74 | spin_unlock(&drv->dynids.lock); | |
75 | ||
9dba910e | 76 | retval = driver_attach(&drv->driver); |
9dba910e TH |
77 | |
78 | return retval; | |
79 | } | |
80 | ||
81 | static void pci_free_dynids(struct pci_driver *drv) | |
82 | { | |
83 | struct pci_dynid *dynid, *n; | |
3d3c2ae1 | 84 | |
9dba910e TH |
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 | ||
1da177e4 | 93 | /** |
9dba910e | 94 | * store_new_id - sysfs frontend to pci_add_dynid() |
8f7020d3 RD |
95 | * @driver: target device driver |
96 | * @buf: buffer for scanning device ID data | |
97 | * @count: input size | |
1da177e4 | 98 | * |
9dba910e | 99 | * Allow PCI IDs to be added to an existing driver via sysfs. |
1da177e4 | 100 | */ |
f8eb1005 | 101 | static ssize_t |
1da177e4 LT |
102 | store_new_id(struct device_driver *driver, const char *buf, size_t count) |
103 | { | |
1da177e4 | 104 | struct pci_driver *pdrv = to_pci_driver(driver); |
b41d6cf3 | 105 | const struct pci_device_id *ids = pdrv->id_table; |
6ba18636 | 106 | __u32 vendor, device, subvendor=PCI_ANY_ID, |
1da177e4 LT |
107 | subdevice=PCI_ANY_ID, class=0, class_mask=0; |
108 | unsigned long driver_data=0; | |
109 | int fields=0; | |
9dba910e | 110 | int retval; |
1da177e4 | 111 | |
b41d6cf3 | 112 | fields = sscanf(buf, "%x %x %x %x %x %x %lx", |
1da177e4 LT |
113 | &vendor, &device, &subvendor, &subdevice, |
114 | &class, &class_mask, &driver_data); | |
6ba18636 | 115 | if (fields < 2) |
1da177e4 LT |
116 | return -EINVAL; |
117 | ||
b41d6cf3 JD |
118 | /* Only accept driver_data values that match an existing id_table |
119 | entry */ | |
2debb4d2 CW |
120 | 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++; | |
b41d6cf3 | 128 | } |
2debb4d2 CW |
129 | if (retval) /* No match */ |
130 | return retval; | |
b41d6cf3 | 131 | } |
b41d6cf3 | 132 | |
9dba910e TH |
133 | retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice, |
134 | class, class_mask, driver_data); | |
b19441af GKH |
135 | if (retval) |
136 | return retval; | |
1da177e4 LT |
137 | return count; |
138 | } | |
1da177e4 | 139 | |
0994375e CW |
140 | /** |
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 | */ | |
148 | static ssize_t | |
149 | store_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 | } | |
0994375e | 184 | |
bfb09a86 KK |
185 | static 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 | }; | |
0994375e | 190 | |
1da177e4 | 191 | /** |
75865858 | 192 | * pci_match_id - See if a pci device matches a given pci_id table |
1da177e4 | 193 | * @ids: array of PCI device id structures to search in |
75865858 GKH |
194 | * @dev: the PCI device structure to match against. |
195 | * | |
1da177e4 | 196 | * Used by a driver to check whether a PCI device present in the |
75865858 | 197 | * system is in its list of supported devices. Returns the matching |
1da177e4 | 198 | * pci_device_id structure or %NULL if there is no match. |
75865858 | 199 | * |
8b60756a | 200 | * Deprecated, don't use this as it will not catch any dynamic ids |
75865858 | 201 | * that a driver might want to check for. |
1da177e4 | 202 | */ |
75865858 GKH |
203 | const struct pci_device_id *pci_match_id(const struct pci_device_id *ids, |
204 | struct pci_dev *dev) | |
1da177e4 | 205 | { |
75865858 GKH |
206 | 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 | } | |
1da177e4 LT |
212 | } |
213 | return NULL; | |
214 | } | |
215 | ||
216 | /** | |
ae9608af | 217 | * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure |
75865858 | 218 | * @drv: the PCI driver to match against |
39ba487f | 219 | * @dev: the PCI device structure to match against |
75865858 GKH |
220 | * |
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. | |
1da177e4 | 224 | */ |
d73460d7 AB |
225 | static const struct pci_device_id *pci_match_device(struct pci_driver *drv, |
226 | struct pci_dev *dev) | |
75865858 | 227 | { |
75865858 | 228 | struct pci_dynid *dynid; |
1da177e4 | 229 | |
7461b60a | 230 | /* Look at the dynamic ids first, before the static ones */ |
75865858 GKH |
231 | 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 | } | |
1da177e4 | 237 | } |
75865858 | 238 | spin_unlock(&drv->dynids.lock); |
7461b60a RK |
239 | |
240 | return pci_match_id(drv->id_table, dev); | |
1da177e4 LT |
241 | } |
242 | ||
873392ca RR |
243 | struct drv_dev_and_id { |
244 | struct pci_driver *drv; | |
245 | struct pci_dev *dev; | |
246 | const struct pci_device_id *id; | |
247 | }; | |
248 | ||
249 | static long local_pci_probe(void *_ddi) | |
250 | { | |
251 | struct drv_dev_and_id *ddi = _ddi; | |
967577b0 HY |
252 | struct pci_dev *pci_dev = ddi->dev; |
253 | struct pci_driver *pci_drv = ddi->drv; | |
254 | struct device *dev = &pci_dev->dev; | |
f3ec4f87 AS |
255 | int rc; |
256 | ||
967577b0 HY |
257 | /* |
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. | |
f3ec4f87 | 264 | */ |
967577b0 HY |
265 | pm_runtime_get_sync(dev); |
266 | pci_dev->driver = pci_drv; | |
267 | rc = pci_drv->probe(pci_dev, ddi->id); | |
f3ec4f87 | 268 | if (rc) { |
967577b0 HY |
269 | pci_dev->driver = NULL; |
270 | pm_runtime_put_sync(dev); | |
f3ec4f87 AS |
271 | } |
272 | return rc; | |
873392ca RR |
273 | } |
274 | ||
d42c6997 AK |
275 | static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev, |
276 | const struct pci_device_id *id) | |
277 | { | |
873392ca RR |
278 | int error, node; |
279 | struct drv_dev_and_id ddi = { drv, dev, id }; | |
280 | ||
281 | /* 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); | |
f70316da | 286 | if (node >= 0) { |
873392ca | 287 | int cpu; |
873392ca RR |
288 | |
289 | get_online_cpus(); | |
a70f7302 | 290 | cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); |
873392ca RR |
291 | 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); | |
d42c6997 AK |
298 | return error; |
299 | } | |
300 | ||
1da177e4 | 301 | /** |
23ea3793 | 302 | * __pci_device_probe - check if a driver wants to claim a specific PCI device |
8f7020d3 RD |
303 | * @drv: driver to call to check if it wants the PCI device |
304 | * @pci_dev: PCI device being probed | |
1da177e4 | 305 | * |
8f7020d3 | 306 | * returns 0 on success, else error. |
1da177e4 LT |
307 | * side-effect: pci_dev->driver is set to drv when drv claims pci_dev. |
308 | */ | |
309 | static int | |
310 | __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev) | |
75865858 GKH |
311 | { |
312 | const struct pci_device_id *id; | |
1da177e4 LT |
313 | int error = 0; |
314 | ||
315 | if (!pci_dev->driver && drv->probe) { | |
75865858 GKH |
316 | error = -ENODEV; |
317 | ||
318 | id = pci_match_device(drv, pci_dev); | |
319 | if (id) | |
d42c6997 | 320 | error = pci_call_probe(drv, pci_dev, id); |
967577b0 | 321 | if (error >= 0) |
75865858 | 322 | error = 0; |
1da177e4 LT |
323 | } |
324 | return error; | |
325 | } | |
326 | ||
327 | static 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 | ||
343 | static 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) { | |
f3ec4f87 AS |
349 | if (drv->remove) { |
350 | pm_runtime_get_sync(dev); | |
1da177e4 | 351 | drv->remove(pci_dev); |
f3ec4f87 AS |
352 | pm_runtime_put_noidle(dev); |
353 | } | |
1da177e4 LT |
354 | pci_dev->driver = NULL; |
355 | } | |
356 | ||
f3ec4f87 | 357 | /* Undo the runtime PM settings in local_pci_probe() */ |
967577b0 | 358 | pm_runtime_put_sync(dev); |
f3ec4f87 | 359 | |
2449e06a SL |
360 | /* |
361 | * 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 | ||
1da177e4 LT |
367 | /* |
368 | * 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 | ||
bbb44d9f RW |
380 | static 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 | ||
3ff2de9b HY |
385 | pm_runtime_resume(dev); |
386 | ||
bbb44d9f RW |
387 | if (drv && drv->shutdown) |
388 | drv->shutdown(pci_dev); | |
389 | pci_msi_shutdown(pci_dev); | |
390 | pci_msix_shutdown(pci_dev); | |
5b415f1e | 391 | |
2a038881 | 392 | #ifdef CONFIG_KEXEC |
b566a22c | 393 | /* |
2a038881 KA |
394 | * 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. | |
b566a22c | 399 | */ |
2a038881 | 400 | if (kexec_in_progress && (pci_dev->current_state <= PCI_D3hot)) |
6e0eda3c | 401 | pci_clear_master(pci_dev); |
2a038881 | 402 | #endif |
bbb44d9f RW |
403 | } |
404 | ||
aa338601 | 405 | #ifdef CONFIG_PM |
6cbf8214 RW |
406 | |
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 | */ | |
413 | static 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 | ||
1d3c16a8 JM |
423 | pci_restore_state(pci_dev); |
424 | return 0; | |
6cbf8214 RW |
425 | } |
426 | ||
db288c9c RW |
427 | #endif |
428 | ||
429 | #ifdef CONFIG_PM_SLEEP | |
430 | ||
6cbf8214 RW |
431 | static void pci_pm_default_resume_early(struct pci_dev *pci_dev) |
432 | { | |
db288c9c RW |
433 | pci_power_up(pci_dev); |
434 | pci_restore_state(pci_dev); | |
6cbf8214 RW |
435 | pci_fixup_device(pci_fixup_resume_early, pci_dev); |
436 | } | |
437 | ||
fa58d305 RW |
438 | /* |
439 | * Default "suspend" method for devices that have no driver provided suspend, | |
440 | * or not even a driver at all (second part). | |
bbb44d9f | 441 | */ |
bb808945 | 442 | static void pci_pm_set_unknown_state(struct pci_dev *pci_dev) |
bbb44d9f | 443 | { |
bbb44d9f RW |
444 | /* |
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 | ||
355a72d7 RW |
452 | /* |
453 | * Default "resume" method for devices that have no driver provided resume, | |
454 | * or not even a driver at all (second part). | |
455 | */ | |
bb808945 | 456 | static int pci_pm_reenable_device(struct pci_dev *pci_dev) |
355a72d7 RW |
457 | { |
458 | int retval; | |
459 | ||
bbb44d9f RW |
460 | /* 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 | ||
472 | static int pci_legacy_suspend(struct device *dev, pm_message_t state) | |
1da177e4 LT |
473 | { |
474 | struct pci_dev * pci_dev = to_pci_dev(dev); | |
475 | struct pci_driver * drv = pci_dev->driver; | |
46939f8b | 476 | |
02669492 | 477 | if (drv && drv->suspend) { |
99dadce8 | 478 | pci_power_t prev = pci_dev->current_state; |
46939f8b | 479 | int error; |
aa8c6c93 | 480 | |
57ef8026 FP |
481 | error = drv->suspend(pci_dev, state); |
482 | suspend_report_result(drv->suspend, error); | |
483 | if (error) | |
484 | return error; | |
aa8c6c93 | 485 | |
46939f8b | 486 | if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0 |
99dadce8 RW |
487 | && 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); | |
99dadce8 | 491 | } |
02669492 | 492 | } |
ad8cfa1d RW |
493 | |
494 | pci_fixup_device(pci_fixup_suspend, pci_dev); | |
495 | ||
46939f8b | 496 | return 0; |
1da177e4 LT |
497 | } |
498 | ||
bbb44d9f | 499 | static int pci_legacy_suspend_late(struct device *dev, pm_message_t state) |
cbd69dbb LT |
500 | { |
501 | struct pci_dev * pci_dev = to_pci_dev(dev); | |
502 | struct pci_driver * drv = pci_dev->driver; | |
cbd69dbb LT |
503 | |
504 | if (drv && drv->suspend_late) { | |
46939f8b RW |
505 | pci_power_t prev = pci_dev->current_state; |
506 | int error; | |
507 | ||
57ef8026 FP |
508 | error = drv->suspend_late(pci_dev, state); |
509 | suspend_report_result(drv->suspend_late, error); | |
46939f8b RW |
510 | 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 | } | |
cbd69dbb | 520 | } |
46939f8b RW |
521 | |
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; | |
cbd69dbb | 528 | } |
1da177e4 | 529 | |
f6dc1e5e RW |
530 | static int pci_legacy_resume_early(struct device *dev) |
531 | { | |
f6dc1e5e RW |
532 | struct pci_dev * pci_dev = to_pci_dev(dev); |
533 | struct pci_driver * drv = pci_dev->driver; | |
534 | ||
aa8c6c93 RW |
535 | return drv && drv->resume_early ? |
536 | drv->resume_early(pci_dev) : 0; | |
f6dc1e5e RW |
537 | } |
538 | ||
bbb44d9f | 539 | static int pci_legacy_resume(struct device *dev) |
1da177e4 LT |
540 | { |
541 | struct pci_dev * pci_dev = to_pci_dev(dev); | |
542 | struct pci_driver * drv = pci_dev->driver; | |
543 | ||
ad8cfa1d RW |
544 | pci_fixup_device(pci_fixup_resume, pci_dev); |
545 | ||
aa8c6c93 RW |
546 | return drv && drv->resume ? |
547 | drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev); | |
1da177e4 LT |
548 | } |
549 | ||
571ff758 RW |
550 | /* Auxiliary functions used by the new power management framework */ |
551 | ||
5294e256 | 552 | static void pci_pm_default_resume(struct pci_dev *pci_dev) |
571ff758 | 553 | { |
73410429 RW |
554 | pci_fixup_device(pci_fixup_resume, pci_dev); |
555 | ||
5294e256 RW |
556 | if (!pci_is_bridge(pci_dev)) |
557 | pci_enable_wake(pci_dev, PCI_D0, false); | |
571ff758 RW |
558 | } |
559 | ||
5294e256 | 560 | static void pci_pm_default_suspend(struct pci_dev *pci_dev) |
73410429 | 561 | { |
5294e256 | 562 | /* Disable non-bridge devices without PM support */ |
cbbc2f6b RW |
563 | if (!pci_is_bridge(pci_dev)) |
564 | pci_disable_enabled_device(pci_dev); | |
73410429 RW |
565 | } |
566 | ||
07e836e8 RW |
567 | static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev) |
568 | { | |
569 | struct pci_driver *drv = pci_dev->driver; | |
bb808945 | 570 | bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume |
07e836e8 | 571 | || drv->resume_early); |
bb808945 RW |
572 | |
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 | */ | |
82440a82 DF |
578 | WARN(ret && drv->driver.pm, "driver %s device %04x:%04x\n", |
579 | drv->name, pci_dev->vendor, pci_dev->device); | |
bb808945 RW |
580 | |
581 | return ret; | |
07e836e8 RW |
582 | } |
583 | ||
571ff758 RW |
584 | /* New power management framework */ |
585 | ||
bbb44d9f RW |
586 | static int pci_pm_prepare(struct device *dev) |
587 | { | |
588 | struct device_driver *drv = dev->driver; | |
589 | int error = 0; | |
590 | ||
6cbf8214 RW |
591 | /* |
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 | */ | |
eea3fc03 | 600 | pm_runtime_resume(dev); |
6cbf8214 | 601 | |
bbb44d9f RW |
602 | if (drv && drv->pm && drv->pm->prepare) |
603 | error = drv->pm->prepare(dev); | |
604 | ||
605 | return error; | |
606 | } | |
607 | ||
608 | static 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 | ||
6cbf8214 RW |
616 | #else /* !CONFIG_PM_SLEEP */ |
617 | ||
618 | #define pci_pm_prepare NULL | |
619 | #define pci_pm_complete NULL | |
620 | ||
621 | #endif /* !CONFIG_PM_SLEEP */ | |
622 | ||
bbb44d9f RW |
623 | #ifdef CONFIG_SUSPEND |
624 | ||
625 | static int pci_pm_suspend(struct device *dev) | |
626 | { | |
627 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
8150f32b | 628 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
bbb44d9f | 629 | |
ad8cfa1d RW |
630 | if (pci_has_legacy_pm_support(pci_dev)) |
631 | return pci_legacy_suspend(dev, PMSG_SUSPEND); | |
bb808945 | 632 | |
5294e256 RW |
633 | if (!pm) { |
634 | pci_pm_default_suspend(pci_dev); | |
635 | goto Fixup; | |
636 | } | |
637 | ||
82fee4d6 | 638 | pci_dev->state_saved = false; |
5294e256 RW |
639 | if (pm->suspend) { |
640 | pci_power_t prev = pci_dev->current_state; | |
641 | int error; | |
642 | ||
ddb7c9d2 RW |
643 | error = pm->suspend(dev); |
644 | suspend_report_result(pm->suspend, error); | |
5294e256 RW |
645 | if (error) |
646 | return error; | |
647 | ||
46939f8b | 648 | if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0 |
5294e256 RW |
649 | && 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); | |
5294e256 | 653 | } |
bbb44d9f | 654 | } |
fa58d305 | 655 | |
5294e256 RW |
656 | Fixup: |
657 | pci_fixup_device(pci_fixup_suspend, pci_dev); | |
658 | ||
659 | return 0; | |
bbb44d9f RW |
660 | } |
661 | ||
662 | static int pci_pm_suspend_noirq(struct device *dev) | |
c8958177 | 663 | { |
355a72d7 | 664 | struct pci_dev *pci_dev = to_pci_dev(dev); |
8150f32b | 665 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
c8958177 | 666 | |
bb808945 RW |
667 | if (pci_has_legacy_pm_support(pci_dev)) |
668 | return pci_legacy_suspend_late(dev, PMSG_SUSPEND); | |
669 | ||
931ff68a RW |
670 | if (!pm) { |
671 | pci_save_state(pci_dev); | |
46939f8b | 672 | return 0; |
931ff68a | 673 | } |
46939f8b RW |
674 | |
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 | } | |
bbb44d9f RW |
691 | } |
692 | ||
46939f8b RW |
693 | 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 | } | |
d67e37d7 | 698 | |
46939f8b RW |
699 | pci_pm_set_unknown_state(pci_dev); |
700 | ||
dbf0e4c7 AS |
701 | /* |
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 | ||
46939f8b | 713 | return 0; |
c8958177 | 714 | } |
1da177e4 | 715 | |
f6dc1e5e | 716 | static int pci_pm_resume_noirq(struct device *dev) |
bbb44d9f RW |
717 | { |
718 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
719 | struct device_driver *drv = dev->driver; | |
355a72d7 | 720 | int error = 0; |
bbb44d9f | 721 | |
6cbf8214 | 722 | pci_pm_default_resume_early(pci_dev); |
aa8c6c93 | 723 | |
ad8cfa1d | 724 | if (pci_has_legacy_pm_support(pci_dev)) |
f6dc1e5e | 725 | return pci_legacy_resume_early(dev); |
bb808945 | 726 | |
f6dc1e5e RW |
727 | if (drv && drv->pm && drv->pm->resume_noirq) |
728 | error = drv->pm->resume_noirq(dev); | |
bbb44d9f RW |
729 | |
730 | return error; | |
731 | } | |
732 | ||
f6dc1e5e | 733 | static int pci_pm_resume(struct device *dev) |
bbb44d9f | 734 | { |
355a72d7 | 735 | struct pci_dev *pci_dev = to_pci_dev(dev); |
8150f32b | 736 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
bbb44d9f RW |
737 | int error = 0; |
738 | ||
418e4da3 RW |
739 | /* |
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 | ||
ad8cfa1d | 746 | if (pci_has_legacy_pm_support(pci_dev)) |
f6dc1e5e | 747 | return pci_legacy_resume(dev); |
bb808945 | 748 | |
5294e256 | 749 | pci_pm_default_resume(pci_dev); |
73410429 | 750 | |
5294e256 RW |
751 | if (pm) { |
752 | if (pm->resume) | |
753 | error = pm->resume(dev); | |
754 | } else { | |
755 | pci_pm_reenable_device(pci_dev); | |
756 | } | |
bbb44d9f | 757 | |
999cce4a | 758 | return error; |
bbb44d9f RW |
759 | } |
760 | ||
761 | #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 | ||
1f112cee | 770 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
bbb44d9f RW |
771 | |
772 | static int pci_pm_freeze(struct device *dev) | |
773 | { | |
774 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
8150f32b | 775 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
bbb44d9f | 776 | |
ad8cfa1d RW |
777 | if (pci_has_legacy_pm_support(pci_dev)) |
778 | return pci_legacy_suspend(dev, PMSG_FREEZE); | |
bb808945 | 779 | |
5294e256 RW |
780 | if (!pm) { |
781 | pci_pm_default_suspend(pci_dev); | |
782 | return 0; | |
bbb44d9f RW |
783 | } |
784 | ||
82fee4d6 | 785 | pci_dev->state_saved = false; |
5294e256 RW |
786 | 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 | ||
5294e256 | 795 | return 0; |
bbb44d9f RW |
796 | } |
797 | ||
798 | static int pci_pm_freeze_noirq(struct device *dev) | |
799 | { | |
355a72d7 | 800 | struct pci_dev *pci_dev = to_pci_dev(dev); |
adf09493 | 801 | struct device_driver *drv = dev->driver; |
bbb44d9f | 802 | |
bb808945 RW |
803 | if (pci_has_legacy_pm_support(pci_dev)) |
804 | return pci_legacy_suspend_late(dev, PMSG_FREEZE); | |
805 | ||
d67e37d7 | 806 | if (drv && drv->pm && drv->pm->freeze_noirq) { |
46939f8b RW |
807 | int error; |
808 | ||
d67e37d7 RW |
809 | error = drv->pm->freeze_noirq(dev); |
810 | suspend_report_result(drv->pm->freeze_noirq, error); | |
46939f8b RW |
811 | if (error) |
812 | return error; | |
bbb44d9f RW |
813 | } |
814 | ||
46939f8b RW |
815 | if (!pci_dev->state_saved) |
816 | pci_save_state(pci_dev); | |
d67e37d7 | 817 | |
46939f8b RW |
818 | pci_pm_set_unknown_state(pci_dev); |
819 | ||
820 | return 0; | |
bbb44d9f RW |
821 | } |
822 | ||
f6dc1e5e | 823 | static int pci_pm_thaw_noirq(struct device *dev) |
bbb44d9f | 824 | { |
355a72d7 | 825 | struct pci_dev *pci_dev = to_pci_dev(dev); |
bbb44d9f RW |
826 | struct device_driver *drv = dev->driver; |
827 | int error = 0; | |
828 | ||
ad8cfa1d | 829 | if (pci_has_legacy_pm_support(pci_dev)) |
f6dc1e5e | 830 | return pci_legacy_resume_early(dev); |
bb808945 | 831 | |
f6dc1e5e | 832 | pci_update_current_state(pci_dev, PCI_D0); |
d67e37d7 | 833 | |
f6dc1e5e RW |
834 | if (drv && drv->pm && drv->pm->thaw_noirq) |
835 | error = drv->pm->thaw_noirq(dev); | |
bbb44d9f RW |
836 | |
837 | return error; | |
838 | } | |
839 | ||
f6dc1e5e | 840 | static int pci_pm_thaw(struct device *dev) |
bbb44d9f | 841 | { |
355a72d7 | 842 | struct pci_dev *pci_dev = to_pci_dev(dev); |
8150f32b | 843 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
bbb44d9f RW |
844 | int error = 0; |
845 | ||
ad8cfa1d | 846 | if (pci_has_legacy_pm_support(pci_dev)) |
f6dc1e5e | 847 | return pci_legacy_resume(dev); |
bb808945 | 848 | |
5294e256 RW |
849 | if (pm) { |
850 | if (pm->thaw) | |
851 | error = pm->thaw(dev); | |
852 | } else { | |
853 | pci_pm_reenable_device(pci_dev); | |
854 | } | |
bbb44d9f | 855 | |
4b77b0a2 RW |
856 | pci_dev->state_saved = false; |
857 | ||
bbb44d9f RW |
858 | return error; |
859 | } | |
860 | ||
861 | static int pci_pm_poweroff(struct device *dev) | |
862 | { | |
355a72d7 | 863 | struct pci_dev *pci_dev = to_pci_dev(dev); |
8150f32b | 864 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
bbb44d9f | 865 | |
ad8cfa1d RW |
866 | if (pci_has_legacy_pm_support(pci_dev)) |
867 | return pci_legacy_suspend(dev, PMSG_HIBERNATE); | |
bb808945 | 868 | |
5294e256 RW |
869 | if (!pm) { |
870 | pci_pm_default_suspend(pci_dev); | |
871 | goto Fixup; | |
872 | } | |
873 | ||
82fee4d6 | 874 | pci_dev->state_saved = false; |
5294e256 | 875 | if (pm->poweroff) { |
46939f8b RW |
876 | int error; |
877 | ||
ddb7c9d2 RW |
878 | error = pm->poweroff(dev); |
879 | suspend_report_result(pm->poweroff, error); | |
46939f8b RW |
880 | if (error) |
881 | return error; | |
bbb44d9f RW |
882 | } |
883 | ||
5294e256 RW |
884 | Fixup: |
885 | pci_fixup_device(pci_fixup_suspend, pci_dev); | |
c9b9972b | 886 | |
46939f8b | 887 | return 0; |
bbb44d9f RW |
888 | } |
889 | ||
890 | static int pci_pm_poweroff_noirq(struct device *dev) | |
891 | { | |
46939f8b | 892 | struct pci_dev *pci_dev = to_pci_dev(dev); |
adf09493 | 893 | struct device_driver *drv = dev->driver; |
bbb44d9f | 894 | |
bb808945 RW |
895 | if (pci_has_legacy_pm_support(to_pci_dev(dev))) |
896 | return pci_legacy_suspend_late(dev, PMSG_HIBERNATE); | |
897 | ||
46939f8b RW |
898 | if (!drv || !drv->pm) |
899 | return 0; | |
900 | ||
901 | if (drv->pm->poweroff_noirq) { | |
902 | int error; | |
903 | ||
d67e37d7 RW |
904 | error = drv->pm->poweroff_noirq(dev); |
905 | suspend_report_result(drv->pm->poweroff_noirq, error); | |
46939f8b RW |
906 | if (error) |
907 | return error; | |
bbb44d9f RW |
908 | } |
909 | ||
46939f8b RW |
910 | if (!pci_dev->state_saved && !pci_is_bridge(pci_dev)) |
911 | pci_prepare_to_sleep(pci_dev); | |
912 | ||
0b68c8e2 RW |
913 | /* |
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 | ||
46939f8b | 920 | return 0; |
bbb44d9f RW |
921 | } |
922 | ||
f6dc1e5e | 923 | static int pci_pm_restore_noirq(struct device *dev) |
bbb44d9f RW |
924 | { |
925 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
926 | struct device_driver *drv = dev->driver; | |
355a72d7 | 927 | int error = 0; |
bbb44d9f | 928 | |
6cbf8214 | 929 | pci_pm_default_resume_early(pci_dev); |
aa8c6c93 | 930 | |
ad8cfa1d | 931 | if (pci_has_legacy_pm_support(pci_dev)) |
f6dc1e5e | 932 | return pci_legacy_resume_early(dev); |
bb808945 | 933 | |
f6dc1e5e RW |
934 | if (drv && drv->pm && drv->pm->restore_noirq) |
935 | error = drv->pm->restore_noirq(dev); | |
bbb44d9f RW |
936 | |
937 | return error; | |
938 | } | |
939 | ||
f6dc1e5e | 940 | static int pci_pm_restore(struct device *dev) |
bbb44d9f RW |
941 | { |
942 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
8150f32b | 943 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
bbb44d9f RW |
944 | int error = 0; |
945 | ||
418e4da3 RW |
946 | /* |
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 | ||
ad8cfa1d | 953 | if (pci_has_legacy_pm_support(pci_dev)) |
f6dc1e5e | 954 | return pci_legacy_resume(dev); |
bb808945 | 955 | |
5294e256 | 956 | pci_pm_default_resume(pci_dev); |
73410429 | 957 | |
5294e256 RW |
958 | if (pm) { |
959 | if (pm->restore) | |
960 | error = pm->restore(dev); | |
961 | } else { | |
962 | pci_pm_reenable_device(pci_dev); | |
963 | } | |
bbb44d9f RW |
964 | |
965 | return error; | |
c8958177 | 966 | } |
1da177e4 | 967 | |
1f112cee | 968 | #else /* !CONFIG_HIBERNATE_CALLBACKS */ |
bbb44d9f RW |
969 | |
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 | ||
1f112cee | 979 | #endif /* !CONFIG_HIBERNATE_CALLBACKS */ |
bbb44d9f | 980 | |
6cbf8214 RW |
981 | #ifdef CONFIG_PM_RUNTIME |
982 | ||
983 | static 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 | ||
967577b0 HY |
990 | /* |
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 | ||
6cbf8214 RW |
997 | if (!pm || !pm->runtime_suspend) |
998 | return -ENOSYS; | |
999 | ||
82fee4d6 | 1000 | pci_dev->state_saved = false; |
448bd857 | 1001 | pci_dev->no_d3cold = false; |
6cbf8214 RW |
1002 | error = pm->runtime_suspend(dev); |
1003 | suspend_report_result(pm->runtime_suspend, error); | |
1004 | if (error) | |
1005 | return error; | |
448bd857 HY |
1006 | if (!pci_dev->d3cold_allowed) |
1007 | pci_dev->no_d3cold = true; | |
6cbf8214 RW |
1008 | |
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 | ||
42eca230 | 1019 | if (!pci_dev->state_saved) { |
6cbf8214 | 1020 | pci_save_state(pci_dev); |
42eca230 DA |
1021 | pci_finish_runtime_suspend(pci_dev); |
1022 | } | |
6cbf8214 RW |
1023 | |
1024 | return 0; | |
1025 | } | |
1026 | ||
1027 | static int pci_pm_runtime_resume(struct device *dev) | |
1028 | { | |
448bd857 | 1029 | int rc; |
6cbf8214 RW |
1030 | struct pci_dev *pci_dev = to_pci_dev(dev); |
1031 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; | |
1032 | ||
967577b0 HY |
1033 | /* |
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 | ||
6cbf8214 RW |
1040 | if (!pm || !pm->runtime_resume) |
1041 | return -ENOSYS; | |
1042 | ||
db288c9c RW |
1043 | pci_restore_standard_config(pci_dev); |
1044 | pci_fixup_device(pci_fixup_resume_early, pci_dev); | |
6cbf8214 RW |
1045 | __pci_enable_wake(pci_dev, PCI_D0, true, false); |
1046 | pci_fixup_device(pci_fixup_resume, pci_dev); | |
1047 | ||
448bd857 HY |
1048 | rc = pm->runtime_resume(dev); |
1049 | ||
1050 | pci_dev->runtime_d3cold = false; | |
1051 | ||
1052 | return rc; | |
6cbf8214 RW |
1053 | } |
1054 | ||
1055 | static int pci_pm_runtime_idle(struct device *dev) | |
1056 | { | |
967577b0 | 1057 | struct pci_dev *pci_dev = to_pci_dev(dev); |
6cbf8214 RW |
1058 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
1059 | ||
967577b0 HY |
1060 | /* |
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 | ||
6cbf8214 RW |
1067 | 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 | ||
967577b0 | 1076 | out: |
6cbf8214 | 1077 | pm_runtime_suspend(dev); |
6cbf8214 RW |
1078 | 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 | ||
aa338601 | 1089 | #ifdef CONFIG_PM |
6cbf8214 | 1090 | |
8150f32b | 1091 | const struct dev_pm_ops pci_dev_pm_ops = { |
adf09493 RW |
1092 | .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, | |
bbb44d9f RW |
1100 | .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, | |
6cbf8214 RW |
1106 | .runtime_suspend = pci_pm_runtime_suspend, |
1107 | .runtime_resume = pci_pm_runtime_resume, | |
1108 | .runtime_idle = pci_pm_runtime_idle, | |
bbb44d9f RW |
1109 | }; |
1110 | ||
adf09493 | 1111 | #define PCI_PM_OPS_PTR (&pci_dev_pm_ops) |
bbb44d9f | 1112 | |
6cbf8214 | 1113 | #else /* !COMFIG_PM_OPS */ |
bbb44d9f RW |
1114 | |
1115 | #define PCI_PM_OPS_PTR NULL | |
1116 | ||
6cbf8214 | 1117 | #endif /* !COMFIG_PM_OPS */ |
bbb44d9f | 1118 | |
1da177e4 | 1119 | /** |
863b18f4 | 1120 | * __pci_register_driver - register a new pci driver |
1da177e4 | 1121 | * @drv: the driver structure to register |
863b18f4 | 1122 | * @owner: owner module of drv |
f95d882d | 1123 | * @mod_name: module name string |
1da177e4 LT |
1124 | * |
1125 | * Adds the driver structure to the list of registered drivers. | |
1126 | * Returns a negative value on error, otherwise 0. | |
eaae4b3a | 1127 | * If no error occurred, the driver remains registered even if |
1da177e4 LT |
1128 | * no device was claimed during registration. |
1129 | */ | |
725522b5 GKH |
1130 | int __pci_register_driver(struct pci_driver *drv, struct module *owner, |
1131 | const char *mod_name) | |
1da177e4 | 1132 | { |
1da177e4 LT |
1133 | /* initialize common driver fields */ |
1134 | drv->driver.name = drv->name; | |
1135 | drv->driver.bus = &pci_bus_type; | |
863b18f4 | 1136 | drv->driver.owner = owner; |
725522b5 | 1137 | drv->driver.mod_name = mod_name; |
50b00755 | 1138 | |
75865858 GKH |
1139 | spin_lock_init(&drv->dynids.lock); |
1140 | INIT_LIST_HEAD(&drv->dynids.list); | |
1da177e4 LT |
1141 | |
1142 | /* register with core */ | |
bfb09a86 | 1143 | return driver_register(&drv->driver); |
1da177e4 LT |
1144 | } |
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 | ||
1156 | void | |
1157 | pci_unregister_driver(struct pci_driver *drv) | |
1158 | { | |
1159 | driver_unregister(&drv->driver); | |
1160 | pci_free_dynids(drv); | |
1161 | } | |
1162 | ||
1163 | static 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 | */ | |
1174 | struct pci_driver * | |
1175 | pci_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 | |
1da177e4 | 1190 | * @dev: the PCI device structure to match against |
8f7020d3 | 1191 | * @drv: the device driver to search for matching PCI device id structures |
1da177e4 LT |
1192 | * |
1193 | * Used by a driver to check whether a PCI device present in the | |
8f7020d3 | 1194 | * system is in its list of supported devices. Returns the matching |
1da177e4 LT |
1195 | * pci_device_id structure or %NULL if there is no match. |
1196 | */ | |
75865858 | 1197 | static int pci_bus_match(struct device *dev, struct device_driver *drv) |
1da177e4 | 1198 | { |
75865858 | 1199 | struct pci_dev *pci_dev = to_pci_dev(dev); |
58d9a38f | 1200 | struct pci_driver *pci_drv; |
1da177e4 LT |
1201 | const struct pci_device_id *found_id; |
1202 | ||
58d9a38f YL |
1203 | if (!pci_dev->match_driver) |
1204 | return 0; | |
1205 | ||
1206 | pci_drv = to_pci_driver(drv); | |
75865858 | 1207 | found_id = pci_match_device(pci_drv, pci_dev); |
1da177e4 LT |
1208 | if (found_id) |
1209 | return 1; | |
1210 | ||
75865858 | 1211 | return 0; |
1da177e4 LT |
1212 | } |
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 | */ | |
1226 | struct 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 | */ | |
1240 | void pci_dev_put(struct pci_dev *dev) | |
1241 | { | |
1242 | if (dev) | |
1243 | put_device(&dev->dev); | |
1244 | } | |
1245 | ||
8ccc9aa1 BP |
1246 | static 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 | ||
fbc0c467 | 1270 | if (add_uevent_var(env, "MODALIAS=pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X", |
8ccc9aa1 BP |
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 | ||
1da177e4 LT |
1279 | struct bus_type pci_bus_type = { |
1280 | .name = "pci", | |
1281 | .match = pci_bus_match, | |
312c004d | 1282 | .uevent = pci_uevent, |
b15d686a RK |
1283 | .probe = pci_device_probe, |
1284 | .remove = pci_device_remove, | |
cbd69dbb | 1285 | .shutdown = pci_device_shutdown, |
1da177e4 | 1286 | .dev_attrs = pci_dev_attrs, |
705b1aaa | 1287 | .bus_attrs = pci_bus_attrs, |
bfb09a86 | 1288 | .drv_attrs = pci_drv_attrs, |
bbb44d9f | 1289 | .pm = PCI_PM_OPS_PTR, |
1da177e4 LT |
1290 | }; |
1291 | ||
1292 | static int __init pci_driver_init(void) | |
1293 | { | |
1294 | return bus_register(&pci_bus_type); | |
1295 | } | |
1296 | ||
1297 | postcore_initcall(pci_driver_init); | |
1298 | ||
9dba910e | 1299 | EXPORT_SYMBOL_GPL(pci_add_dynid); |
75865858 | 1300 | EXPORT_SYMBOL(pci_match_id); |
863b18f4 | 1301 | EXPORT_SYMBOL(__pci_register_driver); |
1da177e4 LT |
1302 | EXPORT_SYMBOL(pci_unregister_driver); |
1303 | EXPORT_SYMBOL(pci_dev_driver); | |
1304 | EXPORT_SYMBOL(pci_bus_type); | |
1305 | EXPORT_SYMBOL(pci_dev_get); | |
1306 | EXPORT_SYMBOL(pci_dev_put); |