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> |
1da177e4 LT |
22 | #include "pci.h" |
23 | ||
75865858 GKH |
24 | struct pci_dynid { |
25 | struct list_head node; | |
26 | struct pci_device_id id; | |
27 | }; | |
1da177e4 | 28 | |
9dba910e TH |
29 | /** |
30 | * pci_add_dynid - add a new PCI device ID to this driver and re-probe devices | |
31 | * @drv: target pci driver | |
32 | * @vendor: PCI vendor ID | |
33 | * @device: PCI device ID | |
34 | * @subvendor: PCI subvendor ID | |
35 | * @subdevice: PCI subdevice ID | |
36 | * @class: PCI class | |
37 | * @class_mask: PCI class mask | |
38 | * @driver_data: private driver data | |
39 | * | |
40 | * Adds a new dynamic pci device ID to this driver and causes the | |
41 | * driver to probe for all devices again. @drv must have been | |
42 | * registered prior to calling this function. | |
43 | * | |
44 | * CONTEXT: | |
45 | * Does GFP_KERNEL allocation. | |
46 | * | |
47 | * RETURNS: | |
48 | * 0 on success, -errno on failure. | |
49 | */ | |
50 | int pci_add_dynid(struct pci_driver *drv, | |
51 | unsigned int vendor, unsigned int device, | |
52 | unsigned int subvendor, unsigned int subdevice, | |
53 | unsigned int class, unsigned int class_mask, | |
54 | unsigned long driver_data) | |
55 | { | |
56 | struct pci_dynid *dynid; | |
57 | int retval; | |
58 | ||
59 | dynid = kzalloc(sizeof(*dynid), GFP_KERNEL); | |
60 | if (!dynid) | |
61 | return -ENOMEM; | |
62 | ||
63 | dynid->id.vendor = vendor; | |
64 | dynid->id.device = device; | |
65 | dynid->id.subvendor = subvendor; | |
66 | dynid->id.subdevice = subdevice; | |
67 | dynid->id.class = class; | |
68 | dynid->id.class_mask = class_mask; | |
69 | dynid->id.driver_data = driver_data; | |
3d3c2ae1 | 70 | |
9dba910e TH |
71 | spin_lock(&drv->dynids.lock); |
72 | list_add_tail(&dynid->node, &drv->dynids.list); | |
73 | spin_unlock(&drv->dynids.lock); | |
74 | ||
9dba910e | 75 | retval = driver_attach(&drv->driver); |
9dba910e TH |
76 | |
77 | return retval; | |
78 | } | |
79 | ||
80 | static void pci_free_dynids(struct pci_driver *drv) | |
81 | { | |
82 | struct pci_dynid *dynid, *n; | |
3d3c2ae1 | 83 | |
9dba910e TH |
84 | spin_lock(&drv->dynids.lock); |
85 | list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) { | |
86 | list_del(&dynid->node); | |
87 | kfree(dynid); | |
88 | } | |
89 | spin_unlock(&drv->dynids.lock); | |
90 | } | |
91 | ||
92 | /* | |
93 | * Dynamic device ID manipulation via sysfs is disabled for !CONFIG_HOTPLUG | |
94 | */ | |
95 | #ifdef CONFIG_HOTPLUG | |
1da177e4 | 96 | /** |
9dba910e | 97 | * store_new_id - sysfs frontend to pci_add_dynid() |
8f7020d3 RD |
98 | * @driver: target device driver |
99 | * @buf: buffer for scanning device ID data | |
100 | * @count: input size | |
1da177e4 | 101 | * |
9dba910e | 102 | * Allow PCI IDs to be added to an existing driver via sysfs. |
1da177e4 | 103 | */ |
f8eb1005 | 104 | static ssize_t |
1da177e4 LT |
105 | store_new_id(struct device_driver *driver, const char *buf, size_t count) |
106 | { | |
1da177e4 | 107 | struct pci_driver *pdrv = to_pci_driver(driver); |
b41d6cf3 | 108 | const struct pci_device_id *ids = pdrv->id_table; |
6ba18636 | 109 | __u32 vendor, device, subvendor=PCI_ANY_ID, |
1da177e4 LT |
110 | subdevice=PCI_ANY_ID, class=0, class_mask=0; |
111 | unsigned long driver_data=0; | |
112 | int fields=0; | |
9dba910e | 113 | int retval; |
1da177e4 | 114 | |
b41d6cf3 | 115 | fields = sscanf(buf, "%x %x %x %x %x %x %lx", |
1da177e4 LT |
116 | &vendor, &device, &subvendor, &subdevice, |
117 | &class, &class_mask, &driver_data); | |
6ba18636 | 118 | if (fields < 2) |
1da177e4 LT |
119 | return -EINVAL; |
120 | ||
b41d6cf3 JD |
121 | /* Only accept driver_data values that match an existing id_table |
122 | entry */ | |
2debb4d2 CW |
123 | if (ids) { |
124 | retval = -EINVAL; | |
125 | while (ids->vendor || ids->subvendor || ids->class_mask) { | |
126 | if (driver_data == ids->driver_data) { | |
127 | retval = 0; | |
128 | break; | |
129 | } | |
130 | ids++; | |
b41d6cf3 | 131 | } |
2debb4d2 CW |
132 | if (retval) /* No match */ |
133 | return retval; | |
b41d6cf3 | 134 | } |
b41d6cf3 | 135 | |
9dba910e TH |
136 | retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice, |
137 | class, class_mask, driver_data); | |
b19441af GKH |
138 | if (retval) |
139 | return retval; | |
1da177e4 LT |
140 | return count; |
141 | } | |
1da177e4 | 142 | |
0994375e CW |
143 | /** |
144 | * store_remove_id - remove a PCI device ID from this driver | |
145 | * @driver: target device driver | |
146 | * @buf: buffer for scanning device ID data | |
147 | * @count: input size | |
148 | * | |
149 | * Removes a dynamic pci device ID to this driver. | |
150 | */ | |
151 | static ssize_t | |
152 | store_remove_id(struct device_driver *driver, const char *buf, size_t count) | |
153 | { | |
154 | struct pci_dynid *dynid, *n; | |
155 | struct pci_driver *pdrv = to_pci_driver(driver); | |
156 | __u32 vendor, device, subvendor = PCI_ANY_ID, | |
157 | subdevice = PCI_ANY_ID, class = 0, class_mask = 0; | |
158 | int fields = 0; | |
159 | int retval = -ENODEV; | |
160 | ||
161 | fields = sscanf(buf, "%x %x %x %x %x %x", | |
162 | &vendor, &device, &subvendor, &subdevice, | |
163 | &class, &class_mask); | |
164 | if (fields < 2) | |
165 | return -EINVAL; | |
166 | ||
167 | spin_lock(&pdrv->dynids.lock); | |
168 | list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) { | |
169 | struct pci_device_id *id = &dynid->id; | |
170 | if ((id->vendor == vendor) && | |
171 | (id->device == device) && | |
172 | (subvendor == PCI_ANY_ID || id->subvendor == subvendor) && | |
173 | (subdevice == PCI_ANY_ID || id->subdevice == subdevice) && | |
174 | !((id->class ^ class) & class_mask)) { | |
175 | list_del(&dynid->node); | |
176 | kfree(dynid); | |
177 | retval = 0; | |
178 | break; | |
179 | } | |
180 | } | |
181 | spin_unlock(&pdrv->dynids.lock); | |
182 | ||
183 | if (retval) | |
184 | return retval; | |
185 | return count; | |
186 | } | |
0994375e | 187 | |
bfb09a86 KK |
188 | static struct driver_attribute pci_drv_attrs[] = { |
189 | __ATTR(new_id, S_IWUSR, NULL, store_new_id), | |
190 | __ATTR(remove_id, S_IWUSR, NULL, store_remove_id), | |
191 | __ATTR_NULL, | |
192 | }; | |
0994375e | 193 | |
bfb09a86 KK |
194 | #else |
195 | #define pci_drv_attrs NULL | |
196 | #endif /* CONFIG_HOTPLUG */ | |
1da177e4 LT |
197 | |
198 | /** | |
75865858 | 199 | * pci_match_id - See if a pci device matches a given pci_id table |
1da177e4 | 200 | * @ids: array of PCI device id structures to search in |
75865858 GKH |
201 | * @dev: the PCI device structure to match against. |
202 | * | |
1da177e4 | 203 | * Used by a driver to check whether a PCI device present in the |
75865858 | 204 | * system is in its list of supported devices. Returns the matching |
1da177e4 | 205 | * pci_device_id structure or %NULL if there is no match. |
75865858 | 206 | * |
8b60756a | 207 | * Deprecated, don't use this as it will not catch any dynamic ids |
75865858 | 208 | * that a driver might want to check for. |
1da177e4 | 209 | */ |
75865858 GKH |
210 | const struct pci_device_id *pci_match_id(const struct pci_device_id *ids, |
211 | struct pci_dev *dev) | |
1da177e4 | 212 | { |
75865858 GKH |
213 | if (ids) { |
214 | while (ids->vendor || ids->subvendor || ids->class_mask) { | |
215 | if (pci_match_one_device(ids, dev)) | |
216 | return ids; | |
217 | ids++; | |
218 | } | |
1da177e4 LT |
219 | } |
220 | return NULL; | |
221 | } | |
222 | ||
223 | /** | |
ae9608af | 224 | * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure |
75865858 | 225 | * @drv: the PCI driver to match against |
39ba487f | 226 | * @dev: the PCI device structure to match against |
75865858 GKH |
227 | * |
228 | * Used by a driver to check whether a PCI device present in the | |
229 | * system is in its list of supported devices. Returns the matching | |
230 | * pci_device_id structure or %NULL if there is no match. | |
1da177e4 | 231 | */ |
d73460d7 AB |
232 | static const struct pci_device_id *pci_match_device(struct pci_driver *drv, |
233 | struct pci_dev *dev) | |
75865858 | 234 | { |
75865858 | 235 | struct pci_dynid *dynid; |
1da177e4 | 236 | |
7461b60a | 237 | /* Look at the dynamic ids first, before the static ones */ |
75865858 GKH |
238 | spin_lock(&drv->dynids.lock); |
239 | list_for_each_entry(dynid, &drv->dynids.list, node) { | |
240 | if (pci_match_one_device(&dynid->id, dev)) { | |
241 | spin_unlock(&drv->dynids.lock); | |
242 | return &dynid->id; | |
243 | } | |
1da177e4 | 244 | } |
75865858 | 245 | spin_unlock(&drv->dynids.lock); |
7461b60a RK |
246 | |
247 | return pci_match_id(drv->id_table, dev); | |
1da177e4 LT |
248 | } |
249 | ||
873392ca RR |
250 | struct drv_dev_and_id { |
251 | struct pci_driver *drv; | |
252 | struct pci_dev *dev; | |
253 | const struct pci_device_id *id; | |
254 | }; | |
255 | ||
256 | static long local_pci_probe(void *_ddi) | |
257 | { | |
258 | struct drv_dev_and_id *ddi = _ddi; | |
967577b0 HY |
259 | struct pci_dev *pci_dev = ddi->dev; |
260 | struct pci_driver *pci_drv = ddi->drv; | |
261 | struct device *dev = &pci_dev->dev; | |
f3ec4f87 AS |
262 | int rc; |
263 | ||
967577b0 HY |
264 | /* |
265 | * Unbound PCI devices are always put in D0, regardless of | |
266 | * runtime PM status. During probe, the device is set to | |
267 | * active and the usage count is incremented. If the driver | |
268 | * supports runtime PM, it should call pm_runtime_put_noidle() | |
269 | * in its probe routine and pm_runtime_get_noresume() in its | |
270 | * remove routine. | |
f3ec4f87 | 271 | */ |
967577b0 HY |
272 | pm_runtime_get_sync(dev); |
273 | pci_dev->driver = pci_drv; | |
274 | rc = pci_drv->probe(pci_dev, ddi->id); | |
f3ec4f87 | 275 | if (rc) { |
967577b0 HY |
276 | pci_dev->driver = NULL; |
277 | pm_runtime_put_sync(dev); | |
f3ec4f87 AS |
278 | } |
279 | return rc; | |
873392ca RR |
280 | } |
281 | ||
d42c6997 AK |
282 | static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev, |
283 | const struct pci_device_id *id) | |
284 | { | |
873392ca RR |
285 | int error, node; |
286 | struct drv_dev_and_id ddi = { drv, dev, id }; | |
287 | ||
288 | /* Execute driver initialization on node where the device's | |
289 | bus is attached to. This way the driver likely allocates | |
290 | its local memory on the right node without any need to | |
291 | change it. */ | |
292 | node = dev_to_node(&dev->dev); | |
f70316da | 293 | if (node >= 0) { |
873392ca | 294 | int cpu; |
873392ca RR |
295 | |
296 | get_online_cpus(); | |
a70f7302 | 297 | cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); |
873392ca RR |
298 | if (cpu < nr_cpu_ids) |
299 | error = work_on_cpu(cpu, local_pci_probe, &ddi); | |
300 | else | |
301 | error = local_pci_probe(&ddi); | |
302 | put_online_cpus(); | |
303 | } else | |
304 | error = local_pci_probe(&ddi); | |
d42c6997 AK |
305 | return error; |
306 | } | |
307 | ||
1da177e4 | 308 | /** |
23ea3793 | 309 | * __pci_device_probe - check if a driver wants to claim a specific PCI device |
8f7020d3 RD |
310 | * @drv: driver to call to check if it wants the PCI device |
311 | * @pci_dev: PCI device being probed | |
1da177e4 | 312 | * |
8f7020d3 | 313 | * returns 0 on success, else error. |
1da177e4 LT |
314 | * side-effect: pci_dev->driver is set to drv when drv claims pci_dev. |
315 | */ | |
316 | static int | |
317 | __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev) | |
75865858 GKH |
318 | { |
319 | const struct pci_device_id *id; | |
1da177e4 LT |
320 | int error = 0; |
321 | ||
322 | if (!pci_dev->driver && drv->probe) { | |
75865858 GKH |
323 | error = -ENODEV; |
324 | ||
325 | id = pci_match_device(drv, pci_dev); | |
326 | if (id) | |
d42c6997 | 327 | error = pci_call_probe(drv, pci_dev, id); |
967577b0 | 328 | if (error >= 0) |
75865858 | 329 | error = 0; |
1da177e4 LT |
330 | } |
331 | return error; | |
332 | } | |
333 | ||
334 | static int pci_device_probe(struct device * dev) | |
335 | { | |
336 | int error = 0; | |
337 | struct pci_driver *drv; | |
338 | struct pci_dev *pci_dev; | |
339 | ||
340 | drv = to_pci_driver(dev->driver); | |
341 | pci_dev = to_pci_dev(dev); | |
342 | pci_dev_get(pci_dev); | |
343 | error = __pci_device_probe(drv, pci_dev); | |
344 | if (error) | |
345 | pci_dev_put(pci_dev); | |
346 | ||
347 | return error; | |
348 | } | |
349 | ||
350 | static int pci_device_remove(struct device * dev) | |
351 | { | |
352 | struct pci_dev * pci_dev = to_pci_dev(dev); | |
353 | struct pci_driver * drv = pci_dev->driver; | |
354 | ||
355 | if (drv) { | |
f3ec4f87 AS |
356 | if (drv->remove) { |
357 | pm_runtime_get_sync(dev); | |
1da177e4 | 358 | drv->remove(pci_dev); |
f3ec4f87 AS |
359 | pm_runtime_put_noidle(dev); |
360 | } | |
1da177e4 LT |
361 | pci_dev->driver = NULL; |
362 | } | |
363 | ||
f3ec4f87 | 364 | /* Undo the runtime PM settings in local_pci_probe() */ |
967577b0 | 365 | pm_runtime_put_sync(dev); |
f3ec4f87 | 366 | |
2449e06a SL |
367 | /* |
368 | * If the device is still on, set the power state as "unknown", | |
369 | * since it might change by the next time we load the driver. | |
370 | */ | |
371 | if (pci_dev->current_state == PCI_D0) | |
372 | pci_dev->current_state = PCI_UNKNOWN; | |
373 | ||
1da177e4 LT |
374 | /* |
375 | * We would love to complain here if pci_dev->is_enabled is set, that | |
376 | * the driver should have called pci_disable_device(), but the | |
377 | * unfortunate fact is there are too many odd BIOS and bridge setups | |
378 | * that don't like drivers doing that all of the time. | |
379 | * Oh well, we can dream of sane hardware when we sleep, no matter how | |
380 | * horrible the crap we have to deal with is when we are awake... | |
381 | */ | |
382 | ||
383 | pci_dev_put(pci_dev); | |
384 | return 0; | |
385 | } | |
386 | ||
bbb44d9f RW |
387 | static void pci_device_shutdown(struct device *dev) |
388 | { | |
389 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
390 | struct pci_driver *drv = pci_dev->driver; | |
391 | ||
3ff2de9b HY |
392 | pm_runtime_resume(dev); |
393 | ||
bbb44d9f RW |
394 | if (drv && drv->shutdown) |
395 | drv->shutdown(pci_dev); | |
396 | pci_msi_shutdown(pci_dev); | |
397 | pci_msix_shutdown(pci_dev); | |
5b415f1e | 398 | |
b566a22c KA |
399 | /* |
400 | * Turn off Bus Master bit on the device to tell it to not | |
401 | * continue to do DMA | |
402 | */ | |
403 | pci_disable_device(pci_dev); | |
bbb44d9f RW |
404 | } |
405 | ||
aa338601 | 406 | #ifdef CONFIG_PM |
6cbf8214 RW |
407 | |
408 | /* Auxiliary functions used for system resume and run-time resume. */ | |
409 | ||
410 | /** | |
411 | * pci_restore_standard_config - restore standard config registers of PCI device | |
412 | * @pci_dev: PCI device to handle | |
413 | */ | |
414 | static int pci_restore_standard_config(struct pci_dev *pci_dev) | |
415 | { | |
416 | pci_update_current_state(pci_dev, PCI_UNKNOWN); | |
417 | ||
418 | if (pci_dev->current_state != PCI_D0) { | |
419 | int error = pci_set_power_state(pci_dev, PCI_D0); | |
420 | if (error) | |
421 | return error; | |
422 | } | |
423 | ||
1d3c16a8 JM |
424 | pci_restore_state(pci_dev); |
425 | return 0; | |
6cbf8214 RW |
426 | } |
427 | ||
db288c9c RW |
428 | #endif |
429 | ||
430 | #ifdef CONFIG_PM_SLEEP | |
431 | ||
6cbf8214 RW |
432 | static void pci_pm_default_resume_early(struct pci_dev *pci_dev) |
433 | { | |
db288c9c RW |
434 | pci_power_up(pci_dev); |
435 | pci_restore_state(pci_dev); | |
6cbf8214 RW |
436 | pci_fixup_device(pci_fixup_resume_early, pci_dev); |
437 | } | |
438 | ||
fa58d305 RW |
439 | /* |
440 | * Default "suspend" method for devices that have no driver provided suspend, | |
441 | * or not even a driver at all (second part). | |
bbb44d9f | 442 | */ |
bb808945 | 443 | static void pci_pm_set_unknown_state(struct pci_dev *pci_dev) |
bbb44d9f | 444 | { |
bbb44d9f RW |
445 | /* |
446 | * mark its power state as "unknown", since we don't know if | |
447 | * e.g. the BIOS will change its device state when we suspend. | |
448 | */ | |
449 | if (pci_dev->current_state == PCI_D0) | |
450 | pci_dev->current_state = PCI_UNKNOWN; | |
451 | } | |
452 | ||
355a72d7 RW |
453 | /* |
454 | * Default "resume" method for devices that have no driver provided resume, | |
455 | * or not even a driver at all (second part). | |
456 | */ | |
bb808945 | 457 | static int pci_pm_reenable_device(struct pci_dev *pci_dev) |
355a72d7 RW |
458 | { |
459 | int retval; | |
460 | ||
bbb44d9f RW |
461 | /* if the device was enabled before suspend, reenable */ |
462 | retval = pci_reenable_device(pci_dev); | |
463 | /* | |
464 | * if the device was busmaster before the suspend, make it busmaster | |
465 | * again | |
466 | */ | |
467 | if (pci_dev->is_busmaster) | |
468 | pci_set_master(pci_dev); | |
469 | ||
470 | return retval; | |
471 | } | |
472 | ||
473 | static int pci_legacy_suspend(struct device *dev, pm_message_t state) | |
1da177e4 LT |
474 | { |
475 | struct pci_dev * pci_dev = to_pci_dev(dev); | |
476 | struct pci_driver * drv = pci_dev->driver; | |
46939f8b | 477 | |
02669492 | 478 | if (drv && drv->suspend) { |
99dadce8 | 479 | pci_power_t prev = pci_dev->current_state; |
46939f8b | 480 | int error; |
aa8c6c93 | 481 | |
57ef8026 FP |
482 | error = drv->suspend(pci_dev, state); |
483 | suspend_report_result(drv->suspend, error); | |
484 | if (error) | |
485 | return error; | |
aa8c6c93 | 486 | |
46939f8b | 487 | if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0 |
99dadce8 RW |
488 | && pci_dev->current_state != PCI_UNKNOWN) { |
489 | WARN_ONCE(pci_dev->current_state != prev, | |
490 | "PCI PM: Device state not saved by %pF\n", | |
491 | drv->suspend); | |
99dadce8 | 492 | } |
02669492 | 493 | } |
ad8cfa1d RW |
494 | |
495 | pci_fixup_device(pci_fixup_suspend, pci_dev); | |
496 | ||
46939f8b | 497 | return 0; |
1da177e4 LT |
498 | } |
499 | ||
bbb44d9f | 500 | static int pci_legacy_suspend_late(struct device *dev, pm_message_t state) |
cbd69dbb LT |
501 | { |
502 | struct pci_dev * pci_dev = to_pci_dev(dev); | |
503 | struct pci_driver * drv = pci_dev->driver; | |
cbd69dbb LT |
504 | |
505 | if (drv && drv->suspend_late) { | |
46939f8b RW |
506 | pci_power_t prev = pci_dev->current_state; |
507 | int error; | |
508 | ||
57ef8026 FP |
509 | error = drv->suspend_late(pci_dev, state); |
510 | suspend_report_result(drv->suspend_late, error); | |
46939f8b RW |
511 | if (error) |
512 | return error; | |
513 | ||
514 | if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0 | |
515 | && pci_dev->current_state != PCI_UNKNOWN) { | |
516 | WARN_ONCE(pci_dev->current_state != prev, | |
517 | "PCI PM: Device state not saved by %pF\n", | |
518 | drv->suspend_late); | |
519 | return 0; | |
520 | } | |
cbd69dbb | 521 | } |
46939f8b RW |
522 | |
523 | if (!pci_dev->state_saved) | |
524 | pci_save_state(pci_dev); | |
525 | ||
526 | pci_pm_set_unknown_state(pci_dev); | |
527 | ||
528 | return 0; | |
cbd69dbb | 529 | } |
1da177e4 | 530 | |
f6dc1e5e RW |
531 | static int pci_legacy_resume_early(struct device *dev) |
532 | { | |
f6dc1e5e RW |
533 | struct pci_dev * pci_dev = to_pci_dev(dev); |
534 | struct pci_driver * drv = pci_dev->driver; | |
535 | ||
aa8c6c93 RW |
536 | return drv && drv->resume_early ? |
537 | drv->resume_early(pci_dev) : 0; | |
f6dc1e5e RW |
538 | } |
539 | ||
bbb44d9f | 540 | static int pci_legacy_resume(struct device *dev) |
1da177e4 LT |
541 | { |
542 | struct pci_dev * pci_dev = to_pci_dev(dev); | |
543 | struct pci_driver * drv = pci_dev->driver; | |
544 | ||
ad8cfa1d RW |
545 | pci_fixup_device(pci_fixup_resume, pci_dev); |
546 | ||
aa8c6c93 RW |
547 | return drv && drv->resume ? |
548 | drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev); | |
1da177e4 LT |
549 | } |
550 | ||
571ff758 RW |
551 | /* Auxiliary functions used by the new power management framework */ |
552 | ||
5294e256 | 553 | static void pci_pm_default_resume(struct pci_dev *pci_dev) |
571ff758 | 554 | { |
73410429 RW |
555 | pci_fixup_device(pci_fixup_resume, pci_dev); |
556 | ||
5294e256 RW |
557 | if (!pci_is_bridge(pci_dev)) |
558 | pci_enable_wake(pci_dev, PCI_D0, false); | |
571ff758 RW |
559 | } |
560 | ||
5294e256 | 561 | static void pci_pm_default_suspend(struct pci_dev *pci_dev) |
73410429 | 562 | { |
5294e256 | 563 | /* Disable non-bridge devices without PM support */ |
cbbc2f6b RW |
564 | if (!pci_is_bridge(pci_dev)) |
565 | pci_disable_enabled_device(pci_dev); | |
73410429 RW |
566 | } |
567 | ||
07e836e8 RW |
568 | static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev) |
569 | { | |
570 | struct pci_driver *drv = pci_dev->driver; | |
bb808945 | 571 | bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume |
07e836e8 | 572 | || drv->resume_early); |
bb808945 RW |
573 | |
574 | /* | |
575 | * Legacy PM support is used by default, so warn if the new framework is | |
576 | * supported as well. Drivers are supposed to support either the | |
577 | * former, or the latter, but not both at the same time. | |
578 | */ | |
82440a82 DF |
579 | WARN(ret && drv->driver.pm, "driver %s device %04x:%04x\n", |
580 | drv->name, pci_dev->vendor, pci_dev->device); | |
bb808945 RW |
581 | |
582 | return ret; | |
07e836e8 RW |
583 | } |
584 | ||
571ff758 RW |
585 | /* New power management framework */ |
586 | ||
bbb44d9f RW |
587 | static int pci_pm_prepare(struct device *dev) |
588 | { | |
589 | struct device_driver *drv = dev->driver; | |
590 | int error = 0; | |
591 | ||
6cbf8214 RW |
592 | /* |
593 | * PCI devices suspended at run time need to be resumed at this | |
594 | * point, because in general it is necessary to reconfigure them for | |
595 | * system suspend. Namely, if the device is supposed to wake up the | |
596 | * system from the sleep state, we may need to reconfigure it for this | |
597 | * purpose. In turn, if the device is not supposed to wake up the | |
598 | * system from the sleep state, we'll have to prevent it from signaling | |
599 | * wake-up. | |
600 | */ | |
eea3fc03 | 601 | pm_runtime_resume(dev); |
6cbf8214 | 602 | |
bbb44d9f RW |
603 | if (drv && drv->pm && drv->pm->prepare) |
604 | error = drv->pm->prepare(dev); | |
605 | ||
606 | return error; | |
607 | } | |
608 | ||
609 | static void pci_pm_complete(struct device *dev) | |
610 | { | |
611 | struct device_driver *drv = dev->driver; | |
612 | ||
613 | if (drv && drv->pm && drv->pm->complete) | |
614 | drv->pm->complete(dev); | |
615 | } | |
616 | ||
6cbf8214 RW |
617 | #else /* !CONFIG_PM_SLEEP */ |
618 | ||
619 | #define pci_pm_prepare NULL | |
620 | #define pci_pm_complete NULL | |
621 | ||
622 | #endif /* !CONFIG_PM_SLEEP */ | |
623 | ||
bbb44d9f RW |
624 | #ifdef CONFIG_SUSPEND |
625 | ||
626 | static int pci_pm_suspend(struct device *dev) | |
627 | { | |
628 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
8150f32b | 629 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
bbb44d9f | 630 | |
ad8cfa1d RW |
631 | if (pci_has_legacy_pm_support(pci_dev)) |
632 | return pci_legacy_suspend(dev, PMSG_SUSPEND); | |
bb808945 | 633 | |
5294e256 RW |
634 | if (!pm) { |
635 | pci_pm_default_suspend(pci_dev); | |
636 | goto Fixup; | |
637 | } | |
638 | ||
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 | ||
5294e256 RW |
785 | if (pm->freeze) { |
786 | int error; | |
787 | ||
788 | error = pm->freeze(dev); | |
789 | suspend_report_result(pm->freeze, error); | |
790 | if (error) | |
791 | return error; | |
792 | } | |
793 | ||
5294e256 | 794 | return 0; |
bbb44d9f RW |
795 | } |
796 | ||
797 | static int pci_pm_freeze_noirq(struct device *dev) | |
798 | { | |
355a72d7 | 799 | struct pci_dev *pci_dev = to_pci_dev(dev); |
adf09493 | 800 | struct device_driver *drv = dev->driver; |
bbb44d9f | 801 | |
bb808945 RW |
802 | if (pci_has_legacy_pm_support(pci_dev)) |
803 | return pci_legacy_suspend_late(dev, PMSG_FREEZE); | |
804 | ||
d67e37d7 | 805 | if (drv && drv->pm && drv->pm->freeze_noirq) { |
46939f8b RW |
806 | int error; |
807 | ||
d67e37d7 RW |
808 | error = drv->pm->freeze_noirq(dev); |
809 | suspend_report_result(drv->pm->freeze_noirq, error); | |
46939f8b RW |
810 | if (error) |
811 | return error; | |
bbb44d9f RW |
812 | } |
813 | ||
46939f8b RW |
814 | if (!pci_dev->state_saved) |
815 | pci_save_state(pci_dev); | |
d67e37d7 | 816 | |
46939f8b RW |
817 | pci_pm_set_unknown_state(pci_dev); |
818 | ||
819 | return 0; | |
bbb44d9f RW |
820 | } |
821 | ||
f6dc1e5e | 822 | static int pci_pm_thaw_noirq(struct device *dev) |
bbb44d9f | 823 | { |
355a72d7 | 824 | struct pci_dev *pci_dev = to_pci_dev(dev); |
bbb44d9f RW |
825 | struct device_driver *drv = dev->driver; |
826 | int error = 0; | |
827 | ||
ad8cfa1d | 828 | if (pci_has_legacy_pm_support(pci_dev)) |
f6dc1e5e | 829 | return pci_legacy_resume_early(dev); |
bb808945 | 830 | |
f6dc1e5e | 831 | pci_update_current_state(pci_dev, PCI_D0); |
d67e37d7 | 832 | |
f6dc1e5e RW |
833 | if (drv && drv->pm && drv->pm->thaw_noirq) |
834 | error = drv->pm->thaw_noirq(dev); | |
bbb44d9f RW |
835 | |
836 | return error; | |
837 | } | |
838 | ||
f6dc1e5e | 839 | static int pci_pm_thaw(struct device *dev) |
bbb44d9f | 840 | { |
355a72d7 | 841 | struct pci_dev *pci_dev = to_pci_dev(dev); |
8150f32b | 842 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
bbb44d9f RW |
843 | int error = 0; |
844 | ||
ad8cfa1d | 845 | if (pci_has_legacy_pm_support(pci_dev)) |
f6dc1e5e | 846 | return pci_legacy_resume(dev); |
bb808945 | 847 | |
5294e256 RW |
848 | if (pm) { |
849 | if (pm->thaw) | |
850 | error = pm->thaw(dev); | |
851 | } else { | |
852 | pci_pm_reenable_device(pci_dev); | |
853 | } | |
bbb44d9f | 854 | |
4b77b0a2 RW |
855 | pci_dev->state_saved = false; |
856 | ||
bbb44d9f RW |
857 | return error; |
858 | } | |
859 | ||
860 | static int pci_pm_poweroff(struct device *dev) | |
861 | { | |
355a72d7 | 862 | struct pci_dev *pci_dev = to_pci_dev(dev); |
8150f32b | 863 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
bbb44d9f | 864 | |
ad8cfa1d RW |
865 | if (pci_has_legacy_pm_support(pci_dev)) |
866 | return pci_legacy_suspend(dev, PMSG_HIBERNATE); | |
bb808945 | 867 | |
5294e256 RW |
868 | if (!pm) { |
869 | pci_pm_default_suspend(pci_dev); | |
870 | goto Fixup; | |
871 | } | |
872 | ||
5294e256 | 873 | if (pm->poweroff) { |
46939f8b RW |
874 | int error; |
875 | ||
ddb7c9d2 RW |
876 | error = pm->poweroff(dev); |
877 | suspend_report_result(pm->poweroff, error); | |
46939f8b RW |
878 | if (error) |
879 | return error; | |
bbb44d9f RW |
880 | } |
881 | ||
5294e256 RW |
882 | Fixup: |
883 | pci_fixup_device(pci_fixup_suspend, pci_dev); | |
c9b9972b | 884 | |
46939f8b | 885 | return 0; |
bbb44d9f RW |
886 | } |
887 | ||
888 | static int pci_pm_poweroff_noirq(struct device *dev) | |
889 | { | |
46939f8b | 890 | struct pci_dev *pci_dev = to_pci_dev(dev); |
adf09493 | 891 | struct device_driver *drv = dev->driver; |
bbb44d9f | 892 | |
bb808945 RW |
893 | if (pci_has_legacy_pm_support(to_pci_dev(dev))) |
894 | return pci_legacy_suspend_late(dev, PMSG_HIBERNATE); | |
895 | ||
46939f8b RW |
896 | if (!drv || !drv->pm) |
897 | return 0; | |
898 | ||
899 | if (drv->pm->poweroff_noirq) { | |
900 | int error; | |
901 | ||
d67e37d7 RW |
902 | error = drv->pm->poweroff_noirq(dev); |
903 | suspend_report_result(drv->pm->poweroff_noirq, error); | |
46939f8b RW |
904 | if (error) |
905 | return error; | |
bbb44d9f RW |
906 | } |
907 | ||
46939f8b RW |
908 | if (!pci_dev->state_saved && !pci_is_bridge(pci_dev)) |
909 | pci_prepare_to_sleep(pci_dev); | |
910 | ||
0b68c8e2 RW |
911 | /* |
912 | * The reason for doing this here is the same as for the analogous code | |
913 | * in pci_pm_suspend_noirq(). | |
914 | */ | |
915 | if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI) | |
916 | pci_write_config_word(pci_dev, PCI_COMMAND, 0); | |
917 | ||
46939f8b | 918 | return 0; |
bbb44d9f RW |
919 | } |
920 | ||
f6dc1e5e | 921 | static int pci_pm_restore_noirq(struct device *dev) |
bbb44d9f RW |
922 | { |
923 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
924 | struct device_driver *drv = dev->driver; | |
355a72d7 | 925 | int error = 0; |
bbb44d9f | 926 | |
6cbf8214 | 927 | pci_pm_default_resume_early(pci_dev); |
aa8c6c93 | 928 | |
ad8cfa1d | 929 | if (pci_has_legacy_pm_support(pci_dev)) |
f6dc1e5e | 930 | return pci_legacy_resume_early(dev); |
bb808945 | 931 | |
f6dc1e5e RW |
932 | if (drv && drv->pm && drv->pm->restore_noirq) |
933 | error = drv->pm->restore_noirq(dev); | |
bbb44d9f RW |
934 | |
935 | return error; | |
936 | } | |
937 | ||
f6dc1e5e | 938 | static int pci_pm_restore(struct device *dev) |
bbb44d9f RW |
939 | { |
940 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
8150f32b | 941 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
bbb44d9f RW |
942 | int error = 0; |
943 | ||
418e4da3 RW |
944 | /* |
945 | * This is necessary for the hibernation error path in which restore is | |
946 | * called without restoring the standard config registers of the device. | |
947 | */ | |
948 | if (pci_dev->state_saved) | |
949 | pci_restore_standard_config(pci_dev); | |
950 | ||
ad8cfa1d | 951 | if (pci_has_legacy_pm_support(pci_dev)) |
f6dc1e5e | 952 | return pci_legacy_resume(dev); |
bb808945 | 953 | |
5294e256 | 954 | pci_pm_default_resume(pci_dev); |
73410429 | 955 | |
5294e256 RW |
956 | if (pm) { |
957 | if (pm->restore) | |
958 | error = pm->restore(dev); | |
959 | } else { | |
960 | pci_pm_reenable_device(pci_dev); | |
961 | } | |
bbb44d9f RW |
962 | |
963 | return error; | |
c8958177 | 964 | } |
1da177e4 | 965 | |
1f112cee | 966 | #else /* !CONFIG_HIBERNATE_CALLBACKS */ |
bbb44d9f RW |
967 | |
968 | #define pci_pm_freeze NULL | |
969 | #define pci_pm_freeze_noirq NULL | |
970 | #define pci_pm_thaw NULL | |
971 | #define pci_pm_thaw_noirq NULL | |
972 | #define pci_pm_poweroff NULL | |
973 | #define pci_pm_poweroff_noirq NULL | |
974 | #define pci_pm_restore NULL | |
975 | #define pci_pm_restore_noirq NULL | |
976 | ||
1f112cee | 977 | #endif /* !CONFIG_HIBERNATE_CALLBACKS */ |
bbb44d9f | 978 | |
6cbf8214 RW |
979 | #ifdef CONFIG_PM_RUNTIME |
980 | ||
981 | static int pci_pm_runtime_suspend(struct device *dev) | |
982 | { | |
983 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
984 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; | |
985 | pci_power_t prev = pci_dev->current_state; | |
986 | int error; | |
987 | ||
967577b0 HY |
988 | /* |
989 | * If pci_dev->driver is not set (unbound), the device should | |
990 | * always remain in D0 regardless of the runtime PM status | |
991 | */ | |
992 | if (!pci_dev->driver) | |
993 | return 0; | |
994 | ||
6cbf8214 RW |
995 | if (!pm || !pm->runtime_suspend) |
996 | return -ENOSYS; | |
997 | ||
448bd857 | 998 | pci_dev->no_d3cold = false; |
6cbf8214 RW |
999 | error = pm->runtime_suspend(dev); |
1000 | suspend_report_result(pm->runtime_suspend, error); | |
1001 | if (error) | |
1002 | return error; | |
448bd857 HY |
1003 | if (!pci_dev->d3cold_allowed) |
1004 | pci_dev->no_d3cold = true; | |
6cbf8214 RW |
1005 | |
1006 | pci_fixup_device(pci_fixup_suspend, pci_dev); | |
1007 | ||
1008 | if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0 | |
1009 | && pci_dev->current_state != PCI_UNKNOWN) { | |
1010 | WARN_ONCE(pci_dev->current_state != prev, | |
1011 | "PCI PM: State of device not saved by %pF\n", | |
1012 | pm->runtime_suspend); | |
1013 | return 0; | |
1014 | } | |
1015 | ||
1016 | if (!pci_dev->state_saved) | |
1017 | pci_save_state(pci_dev); | |
1018 | ||
1019 | pci_finish_runtime_suspend(pci_dev); | |
1020 | ||
1021 | return 0; | |
1022 | } | |
1023 | ||
1024 | static int pci_pm_runtime_resume(struct device *dev) | |
1025 | { | |
448bd857 | 1026 | int rc; |
6cbf8214 RW |
1027 | struct pci_dev *pci_dev = to_pci_dev(dev); |
1028 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; | |
1029 | ||
967577b0 HY |
1030 | /* |
1031 | * If pci_dev->driver is not set (unbound), the device should | |
1032 | * always remain in D0 regardless of the runtime PM status | |
1033 | */ | |
1034 | if (!pci_dev->driver) | |
1035 | return 0; | |
1036 | ||
6cbf8214 RW |
1037 | if (!pm || !pm->runtime_resume) |
1038 | return -ENOSYS; | |
1039 | ||
db288c9c RW |
1040 | pci_restore_standard_config(pci_dev); |
1041 | pci_fixup_device(pci_fixup_resume_early, pci_dev); | |
6cbf8214 RW |
1042 | __pci_enable_wake(pci_dev, PCI_D0, true, false); |
1043 | pci_fixup_device(pci_fixup_resume, pci_dev); | |
1044 | ||
448bd857 HY |
1045 | rc = pm->runtime_resume(dev); |
1046 | ||
1047 | pci_dev->runtime_d3cold = false; | |
1048 | ||
1049 | return rc; | |
6cbf8214 RW |
1050 | } |
1051 | ||
1052 | static int pci_pm_runtime_idle(struct device *dev) | |
1053 | { | |
967577b0 | 1054 | struct pci_dev *pci_dev = to_pci_dev(dev); |
6cbf8214 RW |
1055 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
1056 | ||
967577b0 HY |
1057 | /* |
1058 | * If pci_dev->driver is not set (unbound), the device should | |
1059 | * always remain in D0 regardless of the runtime PM status | |
1060 | */ | |
1061 | if (!pci_dev->driver) | |
1062 | goto out; | |
1063 | ||
6cbf8214 RW |
1064 | if (!pm) |
1065 | return -ENOSYS; | |
1066 | ||
1067 | if (pm->runtime_idle) { | |
1068 | int ret = pm->runtime_idle(dev); | |
1069 | if (ret) | |
1070 | return ret; | |
1071 | } | |
1072 | ||
967577b0 | 1073 | out: |
6cbf8214 | 1074 | pm_runtime_suspend(dev); |
6cbf8214 RW |
1075 | return 0; |
1076 | } | |
1077 | ||
1078 | #else /* !CONFIG_PM_RUNTIME */ | |
1079 | ||
1080 | #define pci_pm_runtime_suspend NULL | |
1081 | #define pci_pm_runtime_resume NULL | |
1082 | #define pci_pm_runtime_idle NULL | |
1083 | ||
1084 | #endif /* !CONFIG_PM_RUNTIME */ | |
1085 | ||
aa338601 | 1086 | #ifdef CONFIG_PM |
6cbf8214 | 1087 | |
8150f32b | 1088 | const struct dev_pm_ops pci_dev_pm_ops = { |
adf09493 RW |
1089 | .prepare = pci_pm_prepare, |
1090 | .complete = pci_pm_complete, | |
1091 | .suspend = pci_pm_suspend, | |
1092 | .resume = pci_pm_resume, | |
1093 | .freeze = pci_pm_freeze, | |
1094 | .thaw = pci_pm_thaw, | |
1095 | .poweroff = pci_pm_poweroff, | |
1096 | .restore = pci_pm_restore, | |
bbb44d9f RW |
1097 | .suspend_noirq = pci_pm_suspend_noirq, |
1098 | .resume_noirq = pci_pm_resume_noirq, | |
1099 | .freeze_noirq = pci_pm_freeze_noirq, | |
1100 | .thaw_noirq = pci_pm_thaw_noirq, | |
1101 | .poweroff_noirq = pci_pm_poweroff_noirq, | |
1102 | .restore_noirq = pci_pm_restore_noirq, | |
6cbf8214 RW |
1103 | .runtime_suspend = pci_pm_runtime_suspend, |
1104 | .runtime_resume = pci_pm_runtime_resume, | |
1105 | .runtime_idle = pci_pm_runtime_idle, | |
bbb44d9f RW |
1106 | }; |
1107 | ||
adf09493 | 1108 | #define PCI_PM_OPS_PTR (&pci_dev_pm_ops) |
bbb44d9f | 1109 | |
6cbf8214 | 1110 | #else /* !COMFIG_PM_OPS */ |
bbb44d9f RW |
1111 | |
1112 | #define PCI_PM_OPS_PTR NULL | |
1113 | ||
6cbf8214 | 1114 | #endif /* !COMFIG_PM_OPS */ |
bbb44d9f | 1115 | |
1da177e4 | 1116 | /** |
863b18f4 | 1117 | * __pci_register_driver - register a new pci driver |
1da177e4 | 1118 | * @drv: the driver structure to register |
863b18f4 | 1119 | * @owner: owner module of drv |
f95d882d | 1120 | * @mod_name: module name string |
1da177e4 LT |
1121 | * |
1122 | * Adds the driver structure to the list of registered drivers. | |
1123 | * Returns a negative value on error, otherwise 0. | |
eaae4b3a | 1124 | * If no error occurred, the driver remains registered even if |
1da177e4 LT |
1125 | * no device was claimed during registration. |
1126 | */ | |
725522b5 GKH |
1127 | int __pci_register_driver(struct pci_driver *drv, struct module *owner, |
1128 | const char *mod_name) | |
1da177e4 | 1129 | { |
1da177e4 LT |
1130 | /* initialize common driver fields */ |
1131 | drv->driver.name = drv->name; | |
1132 | drv->driver.bus = &pci_bus_type; | |
863b18f4 | 1133 | drv->driver.owner = owner; |
725522b5 | 1134 | drv->driver.mod_name = mod_name; |
50b00755 | 1135 | |
75865858 GKH |
1136 | spin_lock_init(&drv->dynids.lock); |
1137 | INIT_LIST_HEAD(&drv->dynids.list); | |
1da177e4 LT |
1138 | |
1139 | /* register with core */ | |
bfb09a86 | 1140 | return driver_register(&drv->driver); |
1da177e4 LT |
1141 | } |
1142 | ||
1143 | /** | |
1144 | * pci_unregister_driver - unregister a pci driver | |
1145 | * @drv: the driver structure to unregister | |
1146 | * | |
1147 | * Deletes the driver structure from the list of registered PCI drivers, | |
1148 | * gives it a chance to clean up by calling its remove() function for | |
1149 | * each device it was responsible for, and marks those devices as | |
1150 | * driverless. | |
1151 | */ | |
1152 | ||
1153 | void | |
1154 | pci_unregister_driver(struct pci_driver *drv) | |
1155 | { | |
1156 | driver_unregister(&drv->driver); | |
1157 | pci_free_dynids(drv); | |
1158 | } | |
1159 | ||
1160 | static struct pci_driver pci_compat_driver = { | |
1161 | .name = "compat" | |
1162 | }; | |
1163 | ||
1164 | /** | |
1165 | * pci_dev_driver - get the pci_driver of a device | |
1166 | * @dev: the device to query | |
1167 | * | |
1168 | * Returns the appropriate pci_driver structure or %NULL if there is no | |
1169 | * registered driver for the device. | |
1170 | */ | |
1171 | struct pci_driver * | |
1172 | pci_dev_driver(const struct pci_dev *dev) | |
1173 | { | |
1174 | if (dev->driver) | |
1175 | return dev->driver; | |
1176 | else { | |
1177 | int i; | |
1178 | for(i=0; i<=PCI_ROM_RESOURCE; i++) | |
1179 | if (dev->resource[i].flags & IORESOURCE_BUSY) | |
1180 | return &pci_compat_driver; | |
1181 | } | |
1182 | return NULL; | |
1183 | } | |
1184 | ||
1185 | /** | |
1186 | * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure | |
1da177e4 | 1187 | * @dev: the PCI device structure to match against |
8f7020d3 | 1188 | * @drv: the device driver to search for matching PCI device id structures |
1da177e4 LT |
1189 | * |
1190 | * Used by a driver to check whether a PCI device present in the | |
8f7020d3 | 1191 | * system is in its list of supported devices. Returns the matching |
1da177e4 LT |
1192 | * pci_device_id structure or %NULL if there is no match. |
1193 | */ | |
75865858 | 1194 | static int pci_bus_match(struct device *dev, struct device_driver *drv) |
1da177e4 | 1195 | { |
75865858 GKH |
1196 | struct pci_dev *pci_dev = to_pci_dev(dev); |
1197 | struct pci_driver *pci_drv = to_pci_driver(drv); | |
1da177e4 LT |
1198 | const struct pci_device_id *found_id; |
1199 | ||
75865858 | 1200 | found_id = pci_match_device(pci_drv, pci_dev); |
1da177e4 LT |
1201 | if (found_id) |
1202 | return 1; | |
1203 | ||
75865858 | 1204 | return 0; |
1da177e4 LT |
1205 | } |
1206 | ||
1207 | /** | |
1208 | * pci_dev_get - increments the reference count of the pci device structure | |
1209 | * @dev: the device being referenced | |
1210 | * | |
1211 | * Each live reference to a device should be refcounted. | |
1212 | * | |
1213 | * Drivers for PCI devices should normally record such references in | |
1214 | * their probe() methods, when they bind to a device, and release | |
1215 | * them by calling pci_dev_put(), in their disconnect() methods. | |
1216 | * | |
1217 | * A pointer to the device with the incremented reference counter is returned. | |
1218 | */ | |
1219 | struct pci_dev *pci_dev_get(struct pci_dev *dev) | |
1220 | { | |
1221 | if (dev) | |
1222 | get_device(&dev->dev); | |
1223 | return dev; | |
1224 | } | |
1225 | ||
1226 | /** | |
1227 | * pci_dev_put - release a use of the pci device structure | |
1228 | * @dev: device that's been disconnected | |
1229 | * | |
1230 | * Must be called when a user of a device is finished with it. When the last | |
1231 | * user of the device calls this function, the memory of the device is freed. | |
1232 | */ | |
1233 | void pci_dev_put(struct pci_dev *dev) | |
1234 | { | |
1235 | if (dev) | |
1236 | put_device(&dev->dev); | |
1237 | } | |
1238 | ||
1239 | #ifndef CONFIG_HOTPLUG | |
7eff2e7a | 1240 | int pci_uevent(struct device *dev, struct kobj_uevent_env *env) |
1da177e4 LT |
1241 | { |
1242 | return -ENODEV; | |
1243 | } | |
1244 | #endif | |
1245 | ||
1246 | struct bus_type pci_bus_type = { | |
1247 | .name = "pci", | |
1248 | .match = pci_bus_match, | |
312c004d | 1249 | .uevent = pci_uevent, |
b15d686a RK |
1250 | .probe = pci_device_probe, |
1251 | .remove = pci_device_remove, | |
cbd69dbb | 1252 | .shutdown = pci_device_shutdown, |
1da177e4 | 1253 | .dev_attrs = pci_dev_attrs, |
705b1aaa | 1254 | .bus_attrs = pci_bus_attrs, |
bfb09a86 | 1255 | .drv_attrs = pci_drv_attrs, |
bbb44d9f | 1256 | .pm = PCI_PM_OPS_PTR, |
1da177e4 LT |
1257 | }; |
1258 | ||
1259 | static int __init pci_driver_init(void) | |
1260 | { | |
1261 | return bus_register(&pci_bus_type); | |
1262 | } | |
1263 | ||
1264 | postcore_initcall(pci_driver_init); | |
1265 | ||
9dba910e | 1266 | EXPORT_SYMBOL_GPL(pci_add_dynid); |
75865858 | 1267 | EXPORT_SYMBOL(pci_match_id); |
863b18f4 | 1268 | EXPORT_SYMBOL(__pci_register_driver); |
1da177e4 LT |
1269 | EXPORT_SYMBOL(pci_unregister_driver); |
1270 | EXPORT_SYMBOL(pci_dev_driver); | |
1271 | EXPORT_SYMBOL(pci_bus_type); | |
1272 | EXPORT_SYMBOL(pci_dev_get); | |
1273 | EXPORT_SYMBOL(pci_dev_put); |