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>
22 #include <linux/kexec.h>
26 struct list_head node;
27 struct pci_device_id id;
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
38 * @class_mask: PCI class mask
39 * @driver_data: private driver data
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.
46 * Does GFP_KERNEL allocation.
49 * 0 on success, -errno on failure.
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)
57 struct pci_dynid *dynid;
60 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
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;
72 spin_lock(&drv->dynids.lock);
73 list_add_tail(&dynid->node, &drv->dynids.list);
74 spin_unlock(&drv->dynids.lock);
76 retval = driver_attach(&drv->driver);
81 static void pci_free_dynids(struct pci_driver *drv)
83 struct pci_dynid *dynid, *n;
85 spin_lock(&drv->dynids.lock);
86 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
87 list_del(&dynid->node);
90 spin_unlock(&drv->dynids.lock);
94 * store_new_id - sysfs frontend to pci_add_dynid()
95 * @driver: target device driver
96 * @buf: buffer for scanning device ID data
99 * Allow PCI IDs to be added to an existing driver via sysfs.
102 store_new_id(struct device_driver *driver, const char *buf, size_t count)
104 struct pci_driver *pdrv = to_pci_driver(driver);
105 const struct pci_device_id *ids = pdrv->id_table;
106 __u32 vendor, device, subvendor=PCI_ANY_ID,
107 subdevice=PCI_ANY_ID, class=0, class_mask=0;
108 unsigned long driver_data=0;
112 fields = sscanf(buf, "%x %x %x %x %x %x %lx",
113 &vendor, &device, &subvendor, &subdevice,
114 &class, &class_mask, &driver_data);
118 /* Only accept driver_data values that match an existing id_table
122 while (ids->vendor || ids->subvendor || ids->class_mask) {
123 if (driver_data == ids->driver_data) {
129 if (retval) /* No match */
133 retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice,
134 class, class_mask, driver_data);
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
146 * Removes a dynamic pci device ID to this driver.
149 store_remove_id(struct device_driver *driver, const char *buf, size_t count)
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;
156 int retval = -ENODEV;
158 fields = sscanf(buf, "%x %x %x %x %x %x",
159 &vendor, &device, &subvendor, &subdevice,
160 &class, &class_mask);
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);
178 spin_unlock(&pdrv->dynids.lock);
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),
192 * pci_match_id - See if a pci device matches a given pci_id table
193 * @ids: array of PCI device id structures to search in
194 * @dev: the PCI device structure to match against.
196 * Used by a driver to check whether a PCI device present in the
197 * system is in its list of supported devices. Returns the matching
198 * pci_device_id structure or %NULL if there is no match.
200 * Deprecated, don't use this as it will not catch any dynamic ids
201 * that a driver might want to check for.
203 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
207 while (ids->vendor || ids->subvendor || ids->class_mask) {
208 if (pci_match_one_device(ids, dev))
217 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
218 * @drv: the PCI driver to match against
219 * @dev: the PCI device structure to match against
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.
225 static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
228 struct pci_dynid *dynid;
230 /* Look at the dynamic ids first, before the static ones */
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);
238 spin_unlock(&drv->dynids.lock);
240 return pci_match_id(drv->id_table, dev);
243 struct drv_dev_and_id {
244 struct pci_driver *drv;
246 const struct pci_device_id *id;
249 static long local_pci_probe(void *_ddi)
251 struct drv_dev_and_id *ddi = _ddi;
252 struct pci_dev *pci_dev = ddi->dev;
253 struct pci_driver *pci_drv = ddi->drv;
254 struct device *dev = &pci_dev->dev;
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
265 pm_runtime_get_sync(dev);
266 pci_dev->driver = pci_drv;
267 rc = pci_drv->probe(pci_dev, ddi->id);
269 pci_dev->driver = NULL;
270 pm_runtime_put_sync(dev);
275 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
276 const struct pci_device_id *id)
279 struct drv_dev_and_id ddi = { drv, dev, id };
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
285 node = dev_to_node(&dev->dev);
290 cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
291 if (cpu < nr_cpu_ids)
292 error = work_on_cpu(cpu, local_pci_probe, &ddi);
294 error = local_pci_probe(&ddi);
297 error = local_pci_probe(&ddi);
302 * __pci_device_probe - check if a driver wants to claim a specific PCI device
303 * @drv: driver to call to check if it wants the PCI device
304 * @pci_dev: PCI device being probed
306 * returns 0 on success, else error.
307 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
310 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
312 const struct pci_device_id *id;
315 if (!pci_dev->driver && drv->probe) {
318 id = pci_match_device(drv, pci_dev);
320 error = pci_call_probe(drv, pci_dev, id);
327 static int pci_device_probe(struct device * dev)
330 struct pci_driver *drv;
331 struct pci_dev *pci_dev;
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);
338 pci_dev_put(pci_dev);
343 static int pci_device_remove(struct device * dev)
345 struct pci_dev * pci_dev = to_pci_dev(dev);
346 struct pci_driver * drv = pci_dev->driver;
350 pm_runtime_get_sync(dev);
351 drv->remove(pci_dev);
352 pm_runtime_put_noidle(dev);
354 pci_dev->driver = NULL;
357 /* Undo the runtime PM settings in local_pci_probe() */
358 pm_runtime_put_sync(dev);
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.
364 if (pci_dev->current_state == PCI_D0)
365 pci_dev->current_state = PCI_UNKNOWN;
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...
376 pci_dev_put(pci_dev);
380 static void pci_device_shutdown(struct device *dev)
382 struct pci_dev *pci_dev = to_pci_dev(dev);
383 struct pci_driver *drv = pci_dev->driver;
385 pm_runtime_resume(dev);
387 if (drv && drv->shutdown)
388 drv->shutdown(pci_dev);
389 pci_msi_shutdown(pci_dev);
390 pci_msix_shutdown(pci_dev);
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.
400 if (kexec_in_progress && (pci_dev->current_state <= PCI_D3hot))
401 pci_clear_master(pci_dev);
407 /* Auxiliary functions used for system resume and run-time resume. */
410 * pci_restore_standard_config - restore standard config registers of PCI device
411 * @pci_dev: PCI device to handle
413 static int pci_restore_standard_config(struct pci_dev *pci_dev)
415 pci_update_current_state(pci_dev, PCI_UNKNOWN);
417 if (pci_dev->current_state != PCI_D0) {
418 int error = pci_set_power_state(pci_dev, PCI_D0);
423 pci_restore_state(pci_dev);
429 #ifdef CONFIG_PM_SLEEP
431 static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
433 pci_power_up(pci_dev);
434 pci_restore_state(pci_dev);
435 pci_fixup_device(pci_fixup_resume_early, pci_dev);
439 * Default "suspend" method for devices that have no driver provided suspend,
440 * or not even a driver at all (second part).
442 static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
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.
448 if (pci_dev->current_state == PCI_D0)
449 pci_dev->current_state = PCI_UNKNOWN;
453 * Default "resume" method for devices that have no driver provided resume,
454 * or not even a driver at all (second part).
456 static int pci_pm_reenable_device(struct pci_dev *pci_dev)
460 /* if the device was enabled before suspend, reenable */
461 retval = pci_reenable_device(pci_dev);
463 * if the device was busmaster before the suspend, make it busmaster
466 if (pci_dev->is_busmaster)
467 pci_set_master(pci_dev);
472 static int pci_legacy_suspend(struct device *dev, pm_message_t state)
474 struct pci_dev * pci_dev = to_pci_dev(dev);
475 struct pci_driver * drv = pci_dev->driver;
477 if (drv && drv->suspend) {
478 pci_power_t prev = pci_dev->current_state;
481 error = drv->suspend(pci_dev, state);
482 suspend_report_result(drv->suspend, error);
486 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
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",
494 pci_fixup_device(pci_fixup_suspend, pci_dev);
499 static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
501 struct pci_dev * pci_dev = to_pci_dev(dev);
502 struct pci_driver * drv = pci_dev->driver;
504 if (drv && drv->suspend_late) {
505 pci_power_t prev = pci_dev->current_state;
508 error = drv->suspend_late(pci_dev, state);
509 suspend_report_result(drv->suspend_late, error);
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",
522 if (!pci_dev->state_saved)
523 pci_save_state(pci_dev);
525 pci_pm_set_unknown_state(pci_dev);
530 static int pci_legacy_resume_early(struct device *dev)
532 struct pci_dev * pci_dev = to_pci_dev(dev);
533 struct pci_driver * drv = pci_dev->driver;
535 return drv && drv->resume_early ?
536 drv->resume_early(pci_dev) : 0;
539 static int pci_legacy_resume(struct device *dev)
541 struct pci_dev * pci_dev = to_pci_dev(dev);
542 struct pci_driver * drv = pci_dev->driver;
544 pci_fixup_device(pci_fixup_resume, pci_dev);
546 return drv && drv->resume ?
547 drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
550 /* Auxiliary functions used by the new power management framework */
552 static void pci_pm_default_resume(struct pci_dev *pci_dev)
554 pci_fixup_device(pci_fixup_resume, pci_dev);
556 if (!pci_is_bridge(pci_dev))
557 pci_enable_wake(pci_dev, PCI_D0, false);
560 static void pci_pm_default_suspend(struct pci_dev *pci_dev)
562 /* Disable non-bridge devices without PM support */
563 if (!pci_is_bridge(pci_dev))
564 pci_disable_enabled_device(pci_dev);
567 static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
569 struct pci_driver *drv = pci_dev->driver;
570 bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
571 || drv->resume_early);
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.
578 WARN(ret && drv->driver.pm, "driver %s device %04x:%04x\n",
579 drv->name, pci_dev->vendor, pci_dev->device);
584 /* New power management framework */
586 static int pci_pm_prepare(struct device *dev)
588 struct device_driver *drv = dev->driver;
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
600 pm_runtime_resume(dev);
602 if (drv && drv->pm && drv->pm->prepare)
603 error = drv->pm->prepare(dev);
608 static void pci_pm_complete(struct device *dev)
610 struct device_driver *drv = dev->driver;
612 if (drv && drv->pm && drv->pm->complete)
613 drv->pm->complete(dev);
616 #else /* !CONFIG_PM_SLEEP */
618 #define pci_pm_prepare NULL
619 #define pci_pm_complete NULL
621 #endif /* !CONFIG_PM_SLEEP */
623 #ifdef CONFIG_SUSPEND
625 static int pci_pm_suspend(struct device *dev)
627 struct pci_dev *pci_dev = to_pci_dev(dev);
628 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
630 if (pci_has_legacy_pm_support(pci_dev))
631 return pci_legacy_suspend(dev, PMSG_SUSPEND);
634 pci_pm_default_suspend(pci_dev);
638 pci_dev->state_saved = false;
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);
785 pci_dev->state_saved = false;
789 error = pm->freeze(dev);
790 suspend_report_result(pm->freeze, error);
798 static int pci_pm_freeze_noirq(struct device *dev)
800 struct pci_dev *pci_dev = to_pci_dev(dev);
801 struct device_driver *drv = dev->driver;
803 if (pci_has_legacy_pm_support(pci_dev))
804 return pci_legacy_suspend_late(dev, PMSG_FREEZE);
806 if (drv && drv->pm && drv->pm->freeze_noirq) {
809 error = drv->pm->freeze_noirq(dev);
810 suspend_report_result(drv->pm->freeze_noirq, error);
815 if (!pci_dev->state_saved)
816 pci_save_state(pci_dev);
818 pci_pm_set_unknown_state(pci_dev);
823 static int pci_pm_thaw_noirq(struct device *dev)
825 struct pci_dev *pci_dev = to_pci_dev(dev);
826 struct device_driver *drv = dev->driver;
829 if (pci_has_legacy_pm_support(pci_dev))
830 return pci_legacy_resume_early(dev);
832 pci_update_current_state(pci_dev, PCI_D0);
834 if (drv && drv->pm && drv->pm->thaw_noirq)
835 error = drv->pm->thaw_noirq(dev);
840 static int pci_pm_thaw(struct device *dev)
842 struct pci_dev *pci_dev = to_pci_dev(dev);
843 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
846 if (pci_has_legacy_pm_support(pci_dev))
847 return pci_legacy_resume(dev);
851 error = pm->thaw(dev);
853 pci_pm_reenable_device(pci_dev);
856 pci_dev->state_saved = false;
861 static int pci_pm_poweroff(struct device *dev)
863 struct pci_dev *pci_dev = to_pci_dev(dev);
864 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
866 if (pci_has_legacy_pm_support(pci_dev))
867 return pci_legacy_suspend(dev, PMSG_HIBERNATE);
870 pci_pm_default_suspend(pci_dev);
874 pci_dev->state_saved = false;
878 error = pm->poweroff(dev);
879 suspend_report_result(pm->poweroff, error);
885 pci_fixup_device(pci_fixup_suspend, pci_dev);
890 static int pci_pm_poweroff_noirq(struct device *dev)
892 struct pci_dev *pci_dev = to_pci_dev(dev);
893 struct device_driver *drv = dev->driver;
895 if (pci_has_legacy_pm_support(to_pci_dev(dev)))
896 return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
898 if (!drv || !drv->pm)
901 if (drv->pm->poweroff_noirq) {
904 error = drv->pm->poweroff_noirq(dev);
905 suspend_report_result(drv->pm->poweroff_noirq, error);
910 if (!pci_dev->state_saved && !pci_is_bridge(pci_dev))
911 pci_prepare_to_sleep(pci_dev);
914 * The reason for doing this here is the same as for the analogous code
915 * in pci_pm_suspend_noirq().
917 if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
918 pci_write_config_word(pci_dev, PCI_COMMAND, 0);
923 static int pci_pm_restore_noirq(struct device *dev)
925 struct pci_dev *pci_dev = to_pci_dev(dev);
926 struct device_driver *drv = dev->driver;
929 pci_pm_default_resume_early(pci_dev);
931 if (pci_has_legacy_pm_support(pci_dev))
932 return pci_legacy_resume_early(dev);
934 if (drv && drv->pm && drv->pm->restore_noirq)
935 error = drv->pm->restore_noirq(dev);
940 static int pci_pm_restore(struct device *dev)
942 struct pci_dev *pci_dev = to_pci_dev(dev);
943 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
947 * This is necessary for the hibernation error path in which restore is
948 * called without restoring the standard config registers of the device.
950 if (pci_dev->state_saved)
951 pci_restore_standard_config(pci_dev);
953 if (pci_has_legacy_pm_support(pci_dev))
954 return pci_legacy_resume(dev);
956 pci_pm_default_resume(pci_dev);
960 error = pm->restore(dev);
962 pci_pm_reenable_device(pci_dev);
968 #else /* !CONFIG_HIBERNATE_CALLBACKS */
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
979 #endif /* !CONFIG_HIBERNATE_CALLBACKS */
981 #ifdef CONFIG_PM_RUNTIME
983 static int pci_pm_runtime_suspend(struct device *dev)
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;
991 * If pci_dev->driver is not set (unbound), the device should
992 * always remain in D0 regardless of the runtime PM status
994 if (!pci_dev->driver)
997 if (!pm || !pm->runtime_suspend)
1000 pci_dev->state_saved = false;
1001 pci_dev->no_d3cold = false;
1002 error = pm->runtime_suspend(dev);
1003 suspend_report_result(pm->runtime_suspend, error);
1006 if (!pci_dev->d3cold_allowed)
1007 pci_dev->no_d3cold = true;
1009 pci_fixup_device(pci_fixup_suspend, pci_dev);
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);
1019 if (!pci_dev->state_saved) {
1020 pci_save_state(pci_dev);
1021 pci_finish_runtime_suspend(pci_dev);
1027 static int pci_pm_runtime_resume(struct device *dev)
1030 struct pci_dev *pci_dev = to_pci_dev(dev);
1031 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1034 * If pci_dev->driver is not set (unbound), the device should
1035 * always remain in D0 regardless of the runtime PM status
1037 if (!pci_dev->driver)
1040 if (!pm || !pm->runtime_resume)
1043 pci_restore_standard_config(pci_dev);
1044 pci_fixup_device(pci_fixup_resume_early, pci_dev);
1045 __pci_enable_wake(pci_dev, PCI_D0, true, false);
1046 pci_fixup_device(pci_fixup_resume, pci_dev);
1048 rc = pm->runtime_resume(dev);
1050 pci_dev->runtime_d3cold = false;
1055 static int pci_pm_runtime_idle(struct device *dev)
1057 struct pci_dev *pci_dev = to_pci_dev(dev);
1058 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1061 * If pci_dev->driver is not set (unbound), the device should
1062 * always remain in D0 regardless of the runtime PM status
1064 if (!pci_dev->driver)
1070 if (pm->runtime_idle) {
1071 int ret = pm->runtime_idle(dev);
1077 pm_runtime_suspend(dev);
1081 #else /* !CONFIG_PM_RUNTIME */
1083 #define pci_pm_runtime_suspend NULL
1084 #define pci_pm_runtime_resume NULL
1085 #define pci_pm_runtime_idle NULL
1087 #endif /* !CONFIG_PM_RUNTIME */
1091 const struct dev_pm_ops pci_dev_pm_ops = {
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,
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,
1106 .runtime_suspend = pci_pm_runtime_suspend,
1107 .runtime_resume = pci_pm_runtime_resume,
1108 .runtime_idle = pci_pm_runtime_idle,
1111 #define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
1113 #else /* !COMFIG_PM_OPS */
1115 #define PCI_PM_OPS_PTR NULL
1117 #endif /* !COMFIG_PM_OPS */
1120 * __pci_register_driver - register a new pci driver
1121 * @drv: the driver structure to register
1122 * @owner: owner module of drv
1123 * @mod_name: module name string
1125 * Adds the driver structure to the list of registered drivers.
1126 * Returns a negative value on error, otherwise 0.
1127 * If no error occurred, the driver remains registered even if
1128 * no device was claimed during registration.
1130 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
1131 const char *mod_name)
1133 /* initialize common driver fields */
1134 drv->driver.name = drv->name;
1135 drv->driver.bus = &pci_bus_type;
1136 drv->driver.owner = owner;
1137 drv->driver.mod_name = mod_name;
1139 spin_lock_init(&drv->dynids.lock);
1140 INIT_LIST_HEAD(&drv->dynids.list);
1142 /* register with core */
1143 return driver_register(&drv->driver);
1147 * pci_unregister_driver - unregister a pci driver
1148 * @drv: the driver structure to unregister
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
1157 pci_unregister_driver(struct pci_driver *drv)
1159 driver_unregister(&drv->driver);
1160 pci_free_dynids(drv);
1163 static struct pci_driver pci_compat_driver = {
1168 * pci_dev_driver - get the pci_driver of a device
1169 * @dev: the device to query
1171 * Returns the appropriate pci_driver structure or %NULL if there is no
1172 * registered driver for the device.
1175 pci_dev_driver(const struct pci_dev *dev)
1181 for(i=0; i<=PCI_ROM_RESOURCE; i++)
1182 if (dev->resource[i].flags & IORESOURCE_BUSY)
1183 return &pci_compat_driver;
1189 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
1190 * @dev: the PCI device structure to match against
1191 * @drv: the device driver to search for matching PCI device id structures
1193 * Used by a driver to check whether a PCI device present in the
1194 * system is in its list of supported devices. Returns the matching
1195 * pci_device_id structure or %NULL if there is no match.
1197 static int pci_bus_match(struct device *dev, struct device_driver *drv)
1199 struct pci_dev *pci_dev = to_pci_dev(dev);
1200 struct pci_driver *pci_drv;
1201 const struct pci_device_id *found_id;
1203 if (!pci_dev->match_driver)
1206 pci_drv = to_pci_driver(drv);
1207 found_id = pci_match_device(pci_drv, pci_dev);
1215 * pci_dev_get - increments the reference count of the pci device structure
1216 * @dev: the device being referenced
1218 * Each live reference to a device should be refcounted.
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.
1224 * A pointer to the device with the incremented reference counter is returned.
1226 struct pci_dev *pci_dev_get(struct pci_dev *dev)
1229 get_device(&dev->dev);
1234 * pci_dev_put - release a use of the pci device structure
1235 * @dev: device that's been disconnected
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.
1240 void pci_dev_put(struct pci_dev *dev)
1243 put_device(&dev->dev);
1246 static int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
1248 struct pci_dev *pdev;
1253 pdev = to_pci_dev(dev);
1257 if (add_uevent_var(env, "PCI_CLASS=%04X", pdev->class))
1260 if (add_uevent_var(env, "PCI_ID=%04X:%04X", pdev->vendor, pdev->device))
1263 if (add_uevent_var(env, "PCI_SUBSYS_ID=%04X:%04X", pdev->subsystem_vendor,
1264 pdev->subsystem_device))
1267 if (add_uevent_var(env, "PCI_SLOT_NAME=%s", pci_name(pdev)))
1270 if (add_uevent_var(env, "MODALIAS=pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x",
1271 pdev->vendor, pdev->device,
1272 pdev->subsystem_vendor, pdev->subsystem_device,
1273 (u8)(pdev->class >> 16), (u8)(pdev->class >> 8),
1279 struct bus_type pci_bus_type = {
1281 .match = pci_bus_match,
1282 .uevent = pci_uevent,
1283 .probe = pci_device_probe,
1284 .remove = pci_device_remove,
1285 .shutdown = pci_device_shutdown,
1286 .dev_attrs = pci_dev_attrs,
1287 .bus_attrs = pci_bus_attrs,
1288 .drv_attrs = pci_drv_attrs,
1289 .pm = PCI_PM_OPS_PTR,
1292 static int __init pci_driver_init(void)
1294 return bus_register(&pci_bus_type);
1297 postcore_initcall(pci_driver_init);
1299 EXPORT_SYMBOL_GPL(pci_add_dynid);
1300 EXPORT_SYMBOL(pci_match_id);
1301 EXPORT_SYMBOL(__pci_register_driver);
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);