2 * drivers/pci/pci-driver.c
4 * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com>
5 * (C) Copyright 2007 Novell Inc.
7 * Released under the GPL v2 only.
11 #include <linux/pci.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/device.h>
15 #include <linux/mempolicy.h>
16 #include <linux/string.h>
17 #include <linux/slab.h>
18 #include <linux/sched.h>
19 #include <linux/cpu.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/suspend.h>
25 struct list_head node;
26 struct pci_device_id id;
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
37 * @class_mask: PCI class mask
38 * @driver_data: private driver data
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.
45 * Does GFP_KERNEL allocation.
48 * 0 on success, -errno on failure.
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)
56 struct pci_dynid *dynid;
59 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
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;
71 spin_lock(&drv->dynids.lock);
72 list_add_tail(&dynid->node, &drv->dynids.list);
73 spin_unlock(&drv->dynids.lock);
75 retval = driver_attach(&drv->driver);
80 static void pci_free_dynids(struct pci_driver *drv)
82 struct pci_dynid *dynid, *n;
84 spin_lock(&drv->dynids.lock);
85 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
86 list_del(&dynid->node);
89 spin_unlock(&drv->dynids.lock);
93 * Dynamic device ID manipulation via sysfs is disabled for !CONFIG_HOTPLUG
97 * store_new_id - sysfs frontend to pci_add_dynid()
98 * @driver: target device driver
99 * @buf: buffer for scanning device ID data
102 * Allow PCI IDs to be added to an existing driver via sysfs.
105 store_new_id(struct device_driver *driver, const char *buf, size_t count)
107 struct pci_driver *pdrv = to_pci_driver(driver);
108 const struct pci_device_id *ids = pdrv->id_table;
109 __u32 vendor, device, subvendor=PCI_ANY_ID,
110 subdevice=PCI_ANY_ID, class=0, class_mask=0;
111 unsigned long driver_data=0;
115 fields = sscanf(buf, "%x %x %x %x %x %x %lx",
116 &vendor, &device, &subvendor, &subdevice,
117 &class, &class_mask, &driver_data);
121 /* Only accept driver_data values that match an existing id_table
125 while (ids->vendor || ids->subvendor || ids->class_mask) {
126 if (driver_data == ids->driver_data) {
132 if (retval) /* No match */
136 retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice,
137 class, class_mask, driver_data);
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
149 * Removes a dynamic pci device ID to this driver.
152 store_remove_id(struct device_driver *driver, const char *buf, size_t count)
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;
159 int retval = -ENODEV;
161 fields = sscanf(buf, "%x %x %x %x %x %x",
162 &vendor, &device, &subvendor, &subdevice,
163 &class, &class_mask);
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);
181 spin_unlock(&pdrv->dynids.lock);
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),
195 #define pci_drv_attrs NULL
196 #endif /* CONFIG_HOTPLUG */
199 * pci_match_id - See if a pci device matches a given pci_id table
200 * @ids: array of PCI device id structures to search in
201 * @dev: the PCI device structure to match against.
203 * Used by a driver to check whether a PCI device present in the
204 * system is in its list of supported devices. Returns the matching
205 * pci_device_id structure or %NULL if there is no match.
207 * Deprecated, don't use this as it will not catch any dynamic ids
208 * that a driver might want to check for.
210 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
214 while (ids->vendor || ids->subvendor || ids->class_mask) {
215 if (pci_match_one_device(ids, dev))
224 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
225 * @drv: the PCI driver to match against
226 * @dev: the PCI device structure to match against
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.
232 static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
235 struct pci_dynid *dynid;
237 /* Look at the dynamic ids first, before the static ones */
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);
245 spin_unlock(&drv->dynids.lock);
247 return pci_match_id(drv->id_table, dev);
250 struct drv_dev_and_id {
251 struct pci_driver *drv;
253 const struct pci_device_id *id;
256 static long local_pci_probe(void *_ddi)
258 struct drv_dev_and_id *ddi = _ddi;
259 struct pci_dev *pci_dev = ddi->dev;
260 struct pci_driver *pci_drv = ddi->drv;
261 struct device *dev = &pci_dev->dev;
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
272 pm_runtime_get_sync(dev);
273 pci_dev->driver = pci_drv;
274 rc = pci_drv->probe(pci_dev, ddi->id);
276 pci_dev->driver = NULL;
277 pm_runtime_put_sync(dev);
282 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
283 const struct pci_device_id *id)
286 struct drv_dev_and_id ddi = { drv, dev, id };
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
292 node = dev_to_node(&dev->dev);
297 cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
298 if (cpu < nr_cpu_ids)
299 error = work_on_cpu(cpu, local_pci_probe, &ddi);
301 error = local_pci_probe(&ddi);
304 error = local_pci_probe(&ddi);
309 * __pci_device_probe - check if a driver wants to claim a specific PCI device
310 * @drv: driver to call to check if it wants the PCI device
311 * @pci_dev: PCI device being probed
313 * returns 0 on success, else error.
314 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
317 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
319 const struct pci_device_id *id;
322 if (!pci_dev->driver && drv->probe) {
325 id = pci_match_device(drv, pci_dev);
327 error = pci_call_probe(drv, pci_dev, id);
334 static int pci_device_probe(struct device * dev)
337 struct pci_driver *drv;
338 struct pci_dev *pci_dev;
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);
345 pci_dev_put(pci_dev);
350 static int pci_device_remove(struct device * dev)
352 struct pci_dev * pci_dev = to_pci_dev(dev);
353 struct pci_driver * drv = pci_dev->driver;
357 pm_runtime_get_sync(dev);
358 drv->remove(pci_dev);
359 pm_runtime_put_noidle(dev);
361 pci_dev->driver = NULL;
364 /* Undo the runtime PM settings in local_pci_probe() */
365 pm_runtime_put_sync(dev);
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.
371 if (pci_dev->current_state == PCI_D0)
372 pci_dev->current_state = PCI_UNKNOWN;
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...
383 pci_dev_put(pci_dev);
387 static void pci_device_shutdown(struct device *dev)
389 struct pci_dev *pci_dev = to_pci_dev(dev);
390 struct pci_driver *drv = pci_dev->driver;
392 pm_runtime_resume(dev);
394 if (drv && drv->shutdown)
395 drv->shutdown(pci_dev);
396 pci_msi_shutdown(pci_dev);
397 pci_msix_shutdown(pci_dev);
400 * Turn off Bus Master bit on the device to tell it to not
403 pci_disable_device(pci_dev);
408 /* Auxiliary functions used for system resume and run-time resume. */
411 * pci_restore_standard_config - restore standard config registers of PCI device
412 * @pci_dev: PCI device to handle
414 static int pci_restore_standard_config(struct pci_dev *pci_dev)
416 pci_update_current_state(pci_dev, PCI_UNKNOWN);
418 if (pci_dev->current_state != PCI_D0) {
419 int error = pci_set_power_state(pci_dev, PCI_D0);
424 pci_restore_state(pci_dev);
430 #ifdef CONFIG_PM_SLEEP
432 static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
434 pci_power_up(pci_dev);
435 pci_restore_state(pci_dev);
436 pci_fixup_device(pci_fixup_resume_early, pci_dev);
440 * Default "suspend" method for devices that have no driver provided suspend,
441 * or not even a driver at all (second part).
443 static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
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.
449 if (pci_dev->current_state == PCI_D0)
450 pci_dev->current_state = PCI_UNKNOWN;
454 * Default "resume" method for devices that have no driver provided resume,
455 * or not even a driver at all (second part).
457 static int pci_pm_reenable_device(struct pci_dev *pci_dev)
461 /* if the device was enabled before suspend, reenable */
462 retval = pci_reenable_device(pci_dev);
464 * if the device was busmaster before the suspend, make it busmaster
467 if (pci_dev->is_busmaster)
468 pci_set_master(pci_dev);
473 static int pci_legacy_suspend(struct device *dev, pm_message_t state)
475 struct pci_dev * pci_dev = to_pci_dev(dev);
476 struct pci_driver * drv = pci_dev->driver;
478 if (drv && drv->suspend) {
479 pci_power_t prev = pci_dev->current_state;
482 error = drv->suspend(pci_dev, state);
483 suspend_report_result(drv->suspend, error);
487 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
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",
495 pci_fixup_device(pci_fixup_suspend, pci_dev);
500 static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
502 struct pci_dev * pci_dev = to_pci_dev(dev);
503 struct pci_driver * drv = pci_dev->driver;
505 if (drv && drv->suspend_late) {
506 pci_power_t prev = pci_dev->current_state;
509 error = drv->suspend_late(pci_dev, state);
510 suspend_report_result(drv->suspend_late, error);
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",
523 if (!pci_dev->state_saved)
524 pci_save_state(pci_dev);
526 pci_pm_set_unknown_state(pci_dev);
531 static int pci_legacy_resume_early(struct device *dev)
533 struct pci_dev * pci_dev = to_pci_dev(dev);
534 struct pci_driver * drv = pci_dev->driver;
536 return drv && drv->resume_early ?
537 drv->resume_early(pci_dev) : 0;
540 static int pci_legacy_resume(struct device *dev)
542 struct pci_dev * pci_dev = to_pci_dev(dev);
543 struct pci_driver * drv = pci_dev->driver;
545 pci_fixup_device(pci_fixup_resume, pci_dev);
547 return drv && drv->resume ?
548 drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
551 /* Auxiliary functions used by the new power management framework */
553 static void pci_pm_default_resume(struct pci_dev *pci_dev)
555 pci_fixup_device(pci_fixup_resume, pci_dev);
557 if (!pci_is_bridge(pci_dev))
558 pci_enable_wake(pci_dev, PCI_D0, false);
561 static void pci_pm_default_suspend(struct pci_dev *pci_dev)
563 /* Disable non-bridge devices without PM support */
564 if (!pci_is_bridge(pci_dev))
565 pci_disable_enabled_device(pci_dev);
568 static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
570 struct pci_driver *drv = pci_dev->driver;
571 bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
572 || drv->resume_early);
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.
579 WARN(ret && drv->driver.pm, "driver %s device %04x:%04x\n",
580 drv->name, pci_dev->vendor, pci_dev->device);
585 /* New power management framework */
587 static int pci_pm_prepare(struct device *dev)
589 struct device_driver *drv = dev->driver;
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
601 pm_runtime_resume(dev);
603 if (drv && drv->pm && drv->pm->prepare)
604 error = drv->pm->prepare(dev);
609 static void pci_pm_complete(struct device *dev)
611 struct device_driver *drv = dev->driver;
613 if (drv && drv->pm && drv->pm->complete)
614 drv->pm->complete(dev);
617 #else /* !CONFIG_PM_SLEEP */
619 #define pci_pm_prepare NULL
620 #define pci_pm_complete NULL
622 #endif /* !CONFIG_PM_SLEEP */
624 #ifdef CONFIG_SUSPEND
626 static int pci_pm_suspend(struct device *dev)
628 struct pci_dev *pci_dev = to_pci_dev(dev);
629 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
631 if (pci_has_legacy_pm_support(pci_dev))
632 return pci_legacy_suspend(dev, PMSG_SUSPEND);
635 pci_pm_default_suspend(pci_dev);
640 pci_power_t prev = pci_dev->current_state;
643 error = pm->suspend(dev);
644 suspend_report_result(pm->suspend, error);
648 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
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",
657 pci_fixup_device(pci_fixup_suspend, pci_dev);
662 static int pci_pm_suspend_noirq(struct device *dev)
664 struct pci_dev *pci_dev = to_pci_dev(dev);
665 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
667 if (pci_has_legacy_pm_support(pci_dev))
668 return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
671 pci_save_state(pci_dev);
675 if (pm->suspend_noirq) {
676 pci_power_t prev = pci_dev->current_state;
679 error = pm->suspend_noirq(dev);
680 suspend_report_result(pm->suspend_noirq, error);
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",
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);
699 pci_pm_set_unknown_state(pci_dev);
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.
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.
710 if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
711 pci_write_config_word(pci_dev, PCI_COMMAND, 0);
716 static int pci_pm_resume_noirq(struct device *dev)
718 struct pci_dev *pci_dev = to_pci_dev(dev);
719 struct device_driver *drv = dev->driver;
722 pci_pm_default_resume_early(pci_dev);
724 if (pci_has_legacy_pm_support(pci_dev))
725 return pci_legacy_resume_early(dev);
727 if (drv && drv->pm && drv->pm->resume_noirq)
728 error = drv->pm->resume_noirq(dev);
733 static int pci_pm_resume(struct device *dev)
735 struct pci_dev *pci_dev = to_pci_dev(dev);
736 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
740 * This is necessary for the suspend error path in which resume is
741 * called without restoring the standard config registers of the device.
743 if (pci_dev->state_saved)
744 pci_restore_standard_config(pci_dev);
746 if (pci_has_legacy_pm_support(pci_dev))
747 return pci_legacy_resume(dev);
749 pci_pm_default_resume(pci_dev);
753 error = pm->resume(dev);
755 pci_pm_reenable_device(pci_dev);
761 #else /* !CONFIG_SUSPEND */
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
768 #endif /* !CONFIG_SUSPEND */
770 #ifdef CONFIG_HIBERNATE_CALLBACKS
772 static int pci_pm_freeze(struct device *dev)
774 struct pci_dev *pci_dev = to_pci_dev(dev);
775 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
777 if (pci_has_legacy_pm_support(pci_dev))
778 return pci_legacy_suspend(dev, PMSG_FREEZE);
781 pci_pm_default_suspend(pci_dev);
788 error = pm->freeze(dev);
789 suspend_report_result(pm->freeze, error);
797 static int pci_pm_freeze_noirq(struct device *dev)
799 struct pci_dev *pci_dev = to_pci_dev(dev);
800 struct device_driver *drv = dev->driver;
802 if (pci_has_legacy_pm_support(pci_dev))
803 return pci_legacy_suspend_late(dev, PMSG_FREEZE);
805 if (drv && drv->pm && drv->pm->freeze_noirq) {
808 error = drv->pm->freeze_noirq(dev);
809 suspend_report_result(drv->pm->freeze_noirq, error);
814 if (!pci_dev->state_saved)
815 pci_save_state(pci_dev);
817 pci_pm_set_unknown_state(pci_dev);
822 static int pci_pm_thaw_noirq(struct device *dev)
824 struct pci_dev *pci_dev = to_pci_dev(dev);
825 struct device_driver *drv = dev->driver;
828 if (pci_has_legacy_pm_support(pci_dev))
829 return pci_legacy_resume_early(dev);
831 pci_update_current_state(pci_dev, PCI_D0);
833 if (drv && drv->pm && drv->pm->thaw_noirq)
834 error = drv->pm->thaw_noirq(dev);
839 static int pci_pm_thaw(struct device *dev)
841 struct pci_dev *pci_dev = to_pci_dev(dev);
842 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
845 if (pci_has_legacy_pm_support(pci_dev))
846 return pci_legacy_resume(dev);
850 error = pm->thaw(dev);
852 pci_pm_reenable_device(pci_dev);
855 pci_dev->state_saved = false;
860 static int pci_pm_poweroff(struct device *dev)
862 struct pci_dev *pci_dev = to_pci_dev(dev);
863 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
865 if (pci_has_legacy_pm_support(pci_dev))
866 return pci_legacy_suspend(dev, PMSG_HIBERNATE);
869 pci_pm_default_suspend(pci_dev);
876 error = pm->poweroff(dev);
877 suspend_report_result(pm->poweroff, error);
883 pci_fixup_device(pci_fixup_suspend, pci_dev);
888 static int pci_pm_poweroff_noirq(struct device *dev)
890 struct pci_dev *pci_dev = to_pci_dev(dev);
891 struct device_driver *drv = dev->driver;
893 if (pci_has_legacy_pm_support(to_pci_dev(dev)))
894 return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
896 if (!drv || !drv->pm)
899 if (drv->pm->poweroff_noirq) {
902 error = drv->pm->poweroff_noirq(dev);
903 suspend_report_result(drv->pm->poweroff_noirq, error);
908 if (!pci_dev->state_saved && !pci_is_bridge(pci_dev))
909 pci_prepare_to_sleep(pci_dev);
912 * The reason for doing this here is the same as for the analogous code
913 * in pci_pm_suspend_noirq().
915 if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
916 pci_write_config_word(pci_dev, PCI_COMMAND, 0);
921 static int pci_pm_restore_noirq(struct device *dev)
923 struct pci_dev *pci_dev = to_pci_dev(dev);
924 struct device_driver *drv = dev->driver;
927 pci_pm_default_resume_early(pci_dev);
929 if (pci_has_legacy_pm_support(pci_dev))
930 return pci_legacy_resume_early(dev);
932 if (drv && drv->pm && drv->pm->restore_noirq)
933 error = drv->pm->restore_noirq(dev);
938 static int pci_pm_restore(struct device *dev)
940 struct pci_dev *pci_dev = to_pci_dev(dev);
941 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
945 * This is necessary for the hibernation error path in which restore is
946 * called without restoring the standard config registers of the device.
948 if (pci_dev->state_saved)
949 pci_restore_standard_config(pci_dev);
951 if (pci_has_legacy_pm_support(pci_dev))
952 return pci_legacy_resume(dev);
954 pci_pm_default_resume(pci_dev);
958 error = pm->restore(dev);
960 pci_pm_reenable_device(pci_dev);
966 #else /* !CONFIG_HIBERNATE_CALLBACKS */
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
977 #endif /* !CONFIG_HIBERNATE_CALLBACKS */
979 #ifdef CONFIG_PM_RUNTIME
981 static int pci_pm_runtime_suspend(struct device *dev)
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;
989 * If pci_dev->driver is not set (unbound), the device should
990 * always remain in D0 regardless of the runtime PM status
992 if (!pci_dev->driver)
995 if (!pm || !pm->runtime_suspend)
998 pci_dev->no_d3cold = false;
999 error = pm->runtime_suspend(dev);
1000 suspend_report_result(pm->runtime_suspend, error);
1003 if (!pci_dev->d3cold_allowed)
1004 pci_dev->no_d3cold = true;
1006 pci_fixup_device(pci_fixup_suspend, pci_dev);
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);
1016 if (!pci_dev->state_saved)
1017 pci_save_state(pci_dev);
1019 pci_finish_runtime_suspend(pci_dev);
1024 static int pci_pm_runtime_resume(struct device *dev)
1027 struct pci_dev *pci_dev = to_pci_dev(dev);
1028 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1031 * If pci_dev->driver is not set (unbound), the device should
1032 * always remain in D0 regardless of the runtime PM status
1034 if (!pci_dev->driver)
1037 if (!pm || !pm->runtime_resume)
1040 pci_restore_standard_config(pci_dev);
1041 pci_fixup_device(pci_fixup_resume_early, pci_dev);
1042 __pci_enable_wake(pci_dev, PCI_D0, true, false);
1043 pci_fixup_device(pci_fixup_resume, pci_dev);
1045 rc = pm->runtime_resume(dev);
1047 pci_dev->runtime_d3cold = false;
1052 static int pci_pm_runtime_idle(struct device *dev)
1054 struct pci_dev *pci_dev = to_pci_dev(dev);
1055 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1058 * If pci_dev->driver is not set (unbound), the device should
1059 * always remain in D0 regardless of the runtime PM status
1061 if (!pci_dev->driver)
1067 if (pm->runtime_idle) {
1068 int ret = pm->runtime_idle(dev);
1074 pm_runtime_suspend(dev);
1078 #else /* !CONFIG_PM_RUNTIME */
1080 #define pci_pm_runtime_suspend NULL
1081 #define pci_pm_runtime_resume NULL
1082 #define pci_pm_runtime_idle NULL
1084 #endif /* !CONFIG_PM_RUNTIME */
1088 const struct dev_pm_ops pci_dev_pm_ops = {
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,
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,
1103 .runtime_suspend = pci_pm_runtime_suspend,
1104 .runtime_resume = pci_pm_runtime_resume,
1105 .runtime_idle = pci_pm_runtime_idle,
1108 #define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
1110 #else /* !COMFIG_PM_OPS */
1112 #define PCI_PM_OPS_PTR NULL
1114 #endif /* !COMFIG_PM_OPS */
1117 * __pci_register_driver - register a new pci driver
1118 * @drv: the driver structure to register
1119 * @owner: owner module of drv
1120 * @mod_name: module name string
1122 * Adds the driver structure to the list of registered drivers.
1123 * Returns a negative value on error, otherwise 0.
1124 * If no error occurred, the driver remains registered even if
1125 * no device was claimed during registration.
1127 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
1128 const char *mod_name)
1130 /* initialize common driver fields */
1131 drv->driver.name = drv->name;
1132 drv->driver.bus = &pci_bus_type;
1133 drv->driver.owner = owner;
1134 drv->driver.mod_name = mod_name;
1136 spin_lock_init(&drv->dynids.lock);
1137 INIT_LIST_HEAD(&drv->dynids.list);
1139 /* register with core */
1140 return driver_register(&drv->driver);
1144 * pci_unregister_driver - unregister a pci driver
1145 * @drv: the driver structure to unregister
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
1154 pci_unregister_driver(struct pci_driver *drv)
1156 driver_unregister(&drv->driver);
1157 pci_free_dynids(drv);
1160 static struct pci_driver pci_compat_driver = {
1165 * pci_dev_driver - get the pci_driver of a device
1166 * @dev: the device to query
1168 * Returns the appropriate pci_driver structure or %NULL if there is no
1169 * registered driver for the device.
1172 pci_dev_driver(const struct pci_dev *dev)
1178 for(i=0; i<=PCI_ROM_RESOURCE; i++)
1179 if (dev->resource[i].flags & IORESOURCE_BUSY)
1180 return &pci_compat_driver;
1186 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
1187 * @dev: the PCI device structure to match against
1188 * @drv: the device driver to search for matching PCI device id structures
1190 * Used by a driver to check whether a PCI device present in the
1191 * system is in its list of supported devices. Returns the matching
1192 * pci_device_id structure or %NULL if there is no match.
1194 static int pci_bus_match(struct device *dev, struct device_driver *drv)
1196 struct pci_dev *pci_dev = to_pci_dev(dev);
1197 struct pci_driver *pci_drv = to_pci_driver(drv);
1198 const struct pci_device_id *found_id;
1200 found_id = pci_match_device(pci_drv, pci_dev);
1208 * pci_dev_get - increments the reference count of the pci device structure
1209 * @dev: the device being referenced
1211 * Each live reference to a device should be refcounted.
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.
1217 * A pointer to the device with the incremented reference counter is returned.
1219 struct pci_dev *pci_dev_get(struct pci_dev *dev)
1222 get_device(&dev->dev);
1227 * pci_dev_put - release a use of the pci device structure
1228 * @dev: device that's been disconnected
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.
1233 void pci_dev_put(struct pci_dev *dev)
1236 put_device(&dev->dev);
1239 #ifndef CONFIG_HOTPLUG
1240 int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
1246 struct bus_type pci_bus_type = {
1248 .match = pci_bus_match,
1249 .uevent = pci_uevent,
1250 .probe = pci_device_probe,
1251 .remove = pci_device_remove,
1252 .shutdown = pci_device_shutdown,
1253 .dev_attrs = pci_dev_attrs,
1254 .bus_attrs = pci_bus_attrs,
1255 .drv_attrs = pci_drv_attrs,
1256 .pm = PCI_PM_OPS_PTR,
1259 static int __init pci_driver_init(void)
1261 return bus_register(&pci_bus_type);
1264 postcore_initcall(pci_driver_init);
1266 EXPORT_SYMBOL_GPL(pci_add_dynid);
1267 EXPORT_SYMBOL(pci_match_id);
1268 EXPORT_SYMBOL(__pci_register_driver);
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);