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>
23 * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
27 struct list_head node;
28 struct pci_device_id id;
34 * store_new_id - add a new PCI device ID to this driver and re-probe devices
35 * @driver: target device driver
36 * @buf: buffer for scanning device ID data
39 * Adds a new dynamic pci device ID to this driver,
40 * and causes the driver to probe for all devices again.
43 store_new_id(struct device_driver *driver, const char *buf, size_t count)
45 struct pci_dynid *dynid;
46 struct pci_driver *pdrv = to_pci_driver(driver);
47 const struct pci_device_id *ids = pdrv->id_table;
48 __u32 vendor, device, subvendor=PCI_ANY_ID,
49 subdevice=PCI_ANY_ID, class=0, class_mask=0;
50 unsigned long driver_data=0;
54 fields = sscanf(buf, "%x %x %x %x %x %x %lx",
55 &vendor, &device, &subvendor, &subdevice,
56 &class, &class_mask, &driver_data);
60 /* Only accept driver_data values that match an existing id_table
64 while (ids->vendor || ids->subvendor || ids->class_mask) {
65 if (driver_data == ids->driver_data) {
71 if (retval) /* No match */
75 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
79 dynid->id.vendor = vendor;
80 dynid->id.device = device;
81 dynid->id.subvendor = subvendor;
82 dynid->id.subdevice = subdevice;
83 dynid->id.class = class;
84 dynid->id.class_mask = class_mask;
85 dynid->id.driver_data = driver_data;
87 spin_lock(&pdrv->dynids.lock);
88 list_add_tail(&dynid->node, &pdrv->dynids.list);
89 spin_unlock(&pdrv->dynids.lock);
91 if (get_driver(&pdrv->driver)) {
92 retval = driver_attach(&pdrv->driver);
93 put_driver(&pdrv->driver);
100 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
103 pci_free_dynids(struct pci_driver *drv)
105 struct pci_dynid *dynid, *n;
107 spin_lock(&drv->dynids.lock);
108 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
109 list_del(&dynid->node);
112 spin_unlock(&drv->dynids.lock);
116 pci_create_newid_file(struct pci_driver *drv)
119 if (drv->probe != NULL)
120 error = driver_create_file(&drv->driver, &driver_attr_new_id);
124 static void pci_remove_newid_file(struct pci_driver *drv)
126 driver_remove_file(&drv->driver, &driver_attr_new_id);
128 #else /* !CONFIG_HOTPLUG */
129 static inline void pci_free_dynids(struct pci_driver *drv) {}
130 static inline int pci_create_newid_file(struct pci_driver *drv)
134 static inline void pci_remove_newid_file(struct pci_driver *drv) {}
138 * pci_match_id - See if a pci device matches a given pci_id table
139 * @ids: array of PCI device id structures to search in
140 * @dev: the PCI device structure to match against.
142 * Used by a driver to check whether a PCI device present in the
143 * system is in its list of supported devices. Returns the matching
144 * pci_device_id structure or %NULL if there is no match.
146 * Deprecated, don't use this as it will not catch any dynamic ids
147 * that a driver might want to check for.
149 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
153 while (ids->vendor || ids->subvendor || ids->class_mask) {
154 if (pci_match_one_device(ids, dev))
163 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
164 * @drv: the PCI driver to match against
165 * @dev: the PCI device structure to match against
167 * Used by a driver to check whether a PCI device present in the
168 * system is in its list of supported devices. Returns the matching
169 * pci_device_id structure or %NULL if there is no match.
171 static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
174 struct pci_dynid *dynid;
176 /* Look at the dynamic ids first, before the static ones */
177 spin_lock(&drv->dynids.lock);
178 list_for_each_entry(dynid, &drv->dynids.list, node) {
179 if (pci_match_one_device(&dynid->id, dev)) {
180 spin_unlock(&drv->dynids.lock);
184 spin_unlock(&drv->dynids.lock);
186 return pci_match_id(drv->id_table, dev);
189 struct drv_dev_and_id {
190 struct pci_driver *drv;
192 const struct pci_device_id *id;
195 static long local_pci_probe(void *_ddi)
197 struct drv_dev_and_id *ddi = _ddi;
199 return ddi->drv->probe(ddi->dev, ddi->id);
202 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
203 const struct pci_device_id *id)
206 struct drv_dev_and_id ddi = { drv, dev, id };
208 /* Execute driver initialization on node where the device's
209 bus is attached to. This way the driver likely allocates
210 its local memory on the right node without any need to
212 node = dev_to_node(&dev->dev);
215 node_to_cpumask_ptr(nodecpumask, node);
218 cpu = cpumask_any_and(nodecpumask, cpu_online_mask);
219 if (cpu < nr_cpu_ids)
220 error = work_on_cpu(cpu, local_pci_probe, &ddi);
222 error = local_pci_probe(&ddi);
225 error = local_pci_probe(&ddi);
230 * __pci_device_probe()
231 * @drv: driver to call to check if it wants the PCI device
232 * @pci_dev: PCI device being probed
234 * returns 0 on success, else error.
235 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
238 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
240 const struct pci_device_id *id;
243 if (!pci_dev->driver && drv->probe) {
246 id = pci_match_device(drv, pci_dev);
248 error = pci_call_probe(drv, pci_dev, id);
250 pci_dev->driver = drv;
257 static int pci_device_probe(struct device * dev)
260 struct pci_driver *drv;
261 struct pci_dev *pci_dev;
263 drv = to_pci_driver(dev->driver);
264 pci_dev = to_pci_dev(dev);
265 pci_dev_get(pci_dev);
266 error = __pci_device_probe(drv, pci_dev);
268 pci_dev_put(pci_dev);
273 static int pci_device_remove(struct device * dev)
275 struct pci_dev * pci_dev = to_pci_dev(dev);
276 struct pci_driver * drv = pci_dev->driver;
280 drv->remove(pci_dev);
281 pci_dev->driver = NULL;
285 * If the device is still on, set the power state as "unknown",
286 * since it might change by the next time we load the driver.
288 if (pci_dev->current_state == PCI_D0)
289 pci_dev->current_state = PCI_UNKNOWN;
292 * We would love to complain here if pci_dev->is_enabled is set, that
293 * the driver should have called pci_disable_device(), but the
294 * unfortunate fact is there are too many odd BIOS and bridge setups
295 * that don't like drivers doing that all of the time.
296 * Oh well, we can dream of sane hardware when we sleep, no matter how
297 * horrible the crap we have to deal with is when we are awake...
300 pci_dev_put(pci_dev);
304 static void pci_device_shutdown(struct device *dev)
306 struct pci_dev *pci_dev = to_pci_dev(dev);
307 struct pci_driver *drv = pci_dev->driver;
309 if (drv && drv->shutdown)
310 drv->shutdown(pci_dev);
311 pci_msi_shutdown(pci_dev);
312 pci_msix_shutdown(pci_dev);
315 #ifdef CONFIG_PM_SLEEP
318 * Default "suspend" method for devices that have no driver provided suspend,
319 * or not even a driver at all (second part).
321 static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
324 * mark its power state as "unknown", since we don't know if
325 * e.g. the BIOS will change its device state when we suspend.
327 if (pci_dev->current_state == PCI_D0)
328 pci_dev->current_state = PCI_UNKNOWN;
332 * Default "resume" method for devices that have no driver provided resume,
333 * or not even a driver at all (second part).
335 static int pci_pm_reenable_device(struct pci_dev *pci_dev)
339 /* if the device was enabled before suspend, reenable */
340 retval = pci_reenable_device(pci_dev);
342 * if the device was busmaster before the suspend, make it busmaster
345 if (pci_dev->is_busmaster)
346 pci_set_master(pci_dev);
351 static int pci_legacy_suspend(struct device *dev, pm_message_t state)
353 struct pci_dev * pci_dev = to_pci_dev(dev);
354 struct pci_driver * drv = pci_dev->driver;
357 if (drv && drv->suspend) {
358 pci_dev->state_saved = false;
360 i = drv->suspend(pci_dev, state);
361 suspend_report_result(drv->suspend, i);
365 if (pci_dev->state_saved)
368 if (WARN_ON_ONCE(pci_dev->current_state != PCI_D0))
372 pci_save_state(pci_dev);
374 * This is for compatibility with existing code with legacy PM support.
376 pci_pm_set_unknown_state(pci_dev);
379 pci_fixup_device(pci_fixup_suspend, pci_dev);
384 static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
386 struct pci_dev * pci_dev = to_pci_dev(dev);
387 struct pci_driver * drv = pci_dev->driver;
390 if (drv && drv->suspend_late) {
391 i = drv->suspend_late(pci_dev, state);
392 suspend_report_result(drv->suspend_late, i);
397 static int pci_legacy_resume_early(struct device *dev)
399 struct pci_dev * pci_dev = to_pci_dev(dev);
400 struct pci_driver * drv = pci_dev->driver;
402 return drv && drv->resume_early ?
403 drv->resume_early(pci_dev) : 0;
406 static int pci_legacy_resume(struct device *dev)
408 struct pci_dev * pci_dev = to_pci_dev(dev);
409 struct pci_driver * drv = pci_dev->driver;
411 pci_fixup_device(pci_fixup_resume, pci_dev);
413 return drv && drv->resume ?
414 drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
417 /* Auxiliary functions used by the new power management framework */
419 static void pci_pm_default_resume_noirq(struct pci_dev *pci_dev)
421 pci_restore_standard_config(pci_dev);
422 pci_fixup_device(pci_fixup_resume_early, pci_dev);
425 static int pci_pm_default_resume(struct pci_dev *pci_dev)
427 pci_fixup_device(pci_fixup_resume, pci_dev);
429 if (!pci_is_bridge(pci_dev))
430 pci_enable_wake(pci_dev, PCI_D0, false);
432 return pci_pm_reenable_device(pci_dev);
435 static void pci_pm_default_suspend_generic(struct pci_dev *pci_dev)
437 /* If device is enabled at this point, disable it */
438 pci_disable_enabled_device(pci_dev);
440 * Save state with interrupts enabled, because in principle the bus the
441 * device is on may be put into a low power state after this code runs.
443 pci_save_state(pci_dev);
446 static void pci_pm_default_suspend(struct pci_dev *pci_dev)
448 pci_pm_default_suspend_generic(pci_dev);
450 if (!pci_is_bridge(pci_dev))
451 pci_prepare_to_sleep(pci_dev);
453 pci_fixup_device(pci_fixup_suspend, pci_dev);
456 static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
458 struct pci_driver *drv = pci_dev->driver;
459 bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
460 || drv->resume_early);
463 * Legacy PM support is used by default, so warn if the new framework is
464 * supported as well. Drivers are supposed to support either the
465 * former, or the latter, but not both at the same time.
467 WARN_ON(ret && drv->driver.pm);
472 /* New power management framework */
474 static int pci_pm_prepare(struct device *dev)
476 struct device_driver *drv = dev->driver;
479 if (drv && drv->pm && drv->pm->prepare)
480 error = drv->pm->prepare(dev);
485 static void pci_pm_complete(struct device *dev)
487 struct device_driver *drv = dev->driver;
489 if (drv && drv->pm && drv->pm->complete)
490 drv->pm->complete(dev);
493 #ifdef CONFIG_SUSPEND
495 static int pci_pm_suspend(struct device *dev)
497 struct pci_dev *pci_dev = to_pci_dev(dev);
498 struct device_driver *drv = dev->driver;
501 if (pci_has_legacy_pm_support(pci_dev))
502 return pci_legacy_suspend(dev, PMSG_SUSPEND);
504 if (drv && drv->pm && drv->pm->suspend) {
505 error = drv->pm->suspend(dev);
506 suspend_report_result(drv->pm->suspend, error);
510 pci_pm_default_suspend(pci_dev);
515 static int pci_pm_suspend_noirq(struct device *dev)
517 struct pci_dev *pci_dev = to_pci_dev(dev);
518 struct device_driver *drv = dev->driver;
521 if (pci_has_legacy_pm_support(pci_dev))
522 return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
524 if (drv && drv->pm && drv->pm->suspend_noirq) {
525 error = drv->pm->suspend_noirq(dev);
526 suspend_report_result(drv->pm->suspend_noirq, error);
530 pci_pm_set_unknown_state(pci_dev);
535 static int pci_pm_resume_noirq(struct device *dev)
537 struct pci_dev *pci_dev = to_pci_dev(dev);
538 struct device_driver *drv = dev->driver;
541 pci_pm_default_resume_noirq(pci_dev);
543 if (pci_has_legacy_pm_support(pci_dev))
544 return pci_legacy_resume_early(dev);
546 if (drv && drv->pm && drv->pm->resume_noirq)
547 error = drv->pm->resume_noirq(dev);
552 static int pci_pm_resume(struct device *dev)
554 struct pci_dev *pci_dev = to_pci_dev(dev);
555 struct device_driver *drv = dev->driver;
558 if (pci_has_legacy_pm_support(pci_dev))
559 return pci_legacy_resume(dev);
561 error = pci_pm_default_resume(pci_dev);
563 if (!error && drv && drv->pm && drv->pm->resume)
564 error = drv->pm->resume(dev);
569 #else /* !CONFIG_SUSPEND */
571 #define pci_pm_suspend NULL
572 #define pci_pm_suspend_noirq NULL
573 #define pci_pm_resume NULL
574 #define pci_pm_resume_noirq NULL
576 #endif /* !CONFIG_SUSPEND */
578 #ifdef CONFIG_HIBERNATION
580 static int pci_pm_freeze(struct device *dev)
582 struct pci_dev *pci_dev = to_pci_dev(dev);
583 struct device_driver *drv = dev->driver;
586 if (pci_has_legacy_pm_support(pci_dev))
587 return pci_legacy_suspend(dev, PMSG_FREEZE);
589 if (drv && drv->pm && drv->pm->freeze) {
590 error = drv->pm->freeze(dev);
591 suspend_report_result(drv->pm->freeze, error);
595 pci_pm_default_suspend_generic(pci_dev);
600 static int pci_pm_freeze_noirq(struct device *dev)
602 struct pci_dev *pci_dev = to_pci_dev(dev);
603 struct device_driver *drv = dev->driver;
606 if (pci_has_legacy_pm_support(pci_dev))
607 return pci_legacy_suspend_late(dev, PMSG_FREEZE);
609 if (drv && drv->pm && drv->pm->freeze_noirq) {
610 error = drv->pm->freeze_noirq(dev);
611 suspend_report_result(drv->pm->freeze_noirq, error);
615 pci_pm_set_unknown_state(pci_dev);
620 static int pci_pm_thaw_noirq(struct device *dev)
622 struct pci_dev *pci_dev = to_pci_dev(dev);
623 struct device_driver *drv = dev->driver;
626 if (pci_has_legacy_pm_support(pci_dev))
627 return pci_legacy_resume_early(dev);
629 pci_update_current_state(pci_dev, PCI_D0);
631 if (drv && drv->pm && drv->pm->thaw_noirq)
632 error = drv->pm->thaw_noirq(dev);
637 static int pci_pm_thaw(struct device *dev)
639 struct pci_dev *pci_dev = to_pci_dev(dev);
640 struct device_driver *drv = dev->driver;
643 if (pci_has_legacy_pm_support(pci_dev))
644 return pci_legacy_resume(dev);
646 pci_pm_reenable_device(pci_dev);
648 if (drv && drv->pm && drv->pm->thaw)
649 error = drv->pm->thaw(dev);
654 static int pci_pm_poweroff(struct device *dev)
656 struct pci_dev *pci_dev = to_pci_dev(dev);
657 struct device_driver *drv = dev->driver;
660 if (pci_has_legacy_pm_support(pci_dev))
661 return pci_legacy_suspend(dev, PMSG_HIBERNATE);
663 if (drv && drv->pm && drv->pm->poweroff) {
664 error = drv->pm->poweroff(dev);
665 suspend_report_result(drv->pm->poweroff, error);
669 pci_pm_default_suspend(pci_dev);
674 static int pci_pm_poweroff_noirq(struct device *dev)
676 struct device_driver *drv = dev->driver;
679 if (pci_has_legacy_pm_support(to_pci_dev(dev)))
680 return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
682 if (drv && drv->pm && drv->pm->poweroff_noirq) {
683 error = drv->pm->poweroff_noirq(dev);
684 suspend_report_result(drv->pm->poweroff_noirq, error);
690 static int pci_pm_restore_noirq(struct device *dev)
692 struct pci_dev *pci_dev = to_pci_dev(dev);
693 struct device_driver *drv = dev->driver;
696 pci_pm_default_resume_noirq(pci_dev);
698 if (pci_has_legacy_pm_support(pci_dev))
699 return pci_legacy_resume_early(dev);
701 if (drv && drv->pm && drv->pm->restore_noirq)
702 error = drv->pm->restore_noirq(dev);
707 static int pci_pm_restore(struct device *dev)
709 struct pci_dev *pci_dev = to_pci_dev(dev);
710 struct device_driver *drv = dev->driver;
713 if (pci_has_legacy_pm_support(pci_dev))
714 return pci_legacy_resume(dev);
716 error = pci_pm_default_resume(pci_dev);
718 if (!error && drv && drv->pm && drv->pm->restore)
719 error = drv->pm->restore(dev);
724 #else /* !CONFIG_HIBERNATION */
726 #define pci_pm_freeze NULL
727 #define pci_pm_freeze_noirq NULL
728 #define pci_pm_thaw NULL
729 #define pci_pm_thaw_noirq NULL
730 #define pci_pm_poweroff NULL
731 #define pci_pm_poweroff_noirq NULL
732 #define pci_pm_restore NULL
733 #define pci_pm_restore_noirq NULL
735 #endif /* !CONFIG_HIBERNATION */
737 struct dev_pm_ops pci_dev_pm_ops = {
738 .prepare = pci_pm_prepare,
739 .complete = pci_pm_complete,
740 .suspend = pci_pm_suspend,
741 .resume = pci_pm_resume,
742 .freeze = pci_pm_freeze,
744 .poweroff = pci_pm_poweroff,
745 .restore = pci_pm_restore,
746 .suspend_noirq = pci_pm_suspend_noirq,
747 .resume_noirq = pci_pm_resume_noirq,
748 .freeze_noirq = pci_pm_freeze_noirq,
749 .thaw_noirq = pci_pm_thaw_noirq,
750 .poweroff_noirq = pci_pm_poweroff_noirq,
751 .restore_noirq = pci_pm_restore_noirq,
754 #define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
756 #else /* !CONFIG_PM_SLEEP */
758 #define PCI_PM_OPS_PTR NULL
760 #endif /* !CONFIG_PM_SLEEP */
763 * __pci_register_driver - register a new pci driver
764 * @drv: the driver structure to register
765 * @owner: owner module of drv
766 * @mod_name: module name string
768 * Adds the driver structure to the list of registered drivers.
769 * Returns a negative value on error, otherwise 0.
770 * If no error occurred, the driver remains registered even if
771 * no device was claimed during registration.
773 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
774 const char *mod_name)
778 /* initialize common driver fields */
779 drv->driver.name = drv->name;
780 drv->driver.bus = &pci_bus_type;
781 drv->driver.owner = owner;
782 drv->driver.mod_name = mod_name;
784 spin_lock_init(&drv->dynids.lock);
785 INIT_LIST_HEAD(&drv->dynids.list);
787 /* register with core */
788 error = driver_register(&drv->driver);
792 error = pci_create_newid_file(drv);
794 driver_unregister(&drv->driver);
800 * pci_unregister_driver - unregister a pci driver
801 * @drv: the driver structure to unregister
803 * Deletes the driver structure from the list of registered PCI drivers,
804 * gives it a chance to clean up by calling its remove() function for
805 * each device it was responsible for, and marks those devices as
810 pci_unregister_driver(struct pci_driver *drv)
812 pci_remove_newid_file(drv);
813 driver_unregister(&drv->driver);
814 pci_free_dynids(drv);
817 static struct pci_driver pci_compat_driver = {
822 * pci_dev_driver - get the pci_driver of a device
823 * @dev: the device to query
825 * Returns the appropriate pci_driver structure or %NULL if there is no
826 * registered driver for the device.
829 pci_dev_driver(const struct pci_dev *dev)
835 for(i=0; i<=PCI_ROM_RESOURCE; i++)
836 if (dev->resource[i].flags & IORESOURCE_BUSY)
837 return &pci_compat_driver;
843 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
844 * @dev: the PCI device structure to match against
845 * @drv: the device driver to search for matching PCI device id structures
847 * Used by a driver to check whether a PCI device present in the
848 * system is in its list of supported devices. Returns the matching
849 * pci_device_id structure or %NULL if there is no match.
851 static int pci_bus_match(struct device *dev, struct device_driver *drv)
853 struct pci_dev *pci_dev = to_pci_dev(dev);
854 struct pci_driver *pci_drv = to_pci_driver(drv);
855 const struct pci_device_id *found_id;
857 found_id = pci_match_device(pci_drv, pci_dev);
865 * pci_dev_get - increments the reference count of the pci device structure
866 * @dev: the device being referenced
868 * Each live reference to a device should be refcounted.
870 * Drivers for PCI devices should normally record such references in
871 * their probe() methods, when they bind to a device, and release
872 * them by calling pci_dev_put(), in their disconnect() methods.
874 * A pointer to the device with the incremented reference counter is returned.
876 struct pci_dev *pci_dev_get(struct pci_dev *dev)
879 get_device(&dev->dev);
884 * pci_dev_put - release a use of the pci device structure
885 * @dev: device that's been disconnected
887 * Must be called when a user of a device is finished with it. When the last
888 * user of the device calls this function, the memory of the device is freed.
890 void pci_dev_put(struct pci_dev *dev)
893 put_device(&dev->dev);
896 #ifndef CONFIG_HOTPLUG
897 int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
903 struct bus_type pci_bus_type = {
905 .match = pci_bus_match,
906 .uevent = pci_uevent,
907 .probe = pci_device_probe,
908 .remove = pci_device_remove,
909 .shutdown = pci_device_shutdown,
910 .dev_attrs = pci_dev_attrs,
911 .pm = PCI_PM_OPS_PTR,
914 static int __init pci_driver_init(void)
916 return bus_register(&pci_bus_type);
919 postcore_initcall(pci_driver_init);
921 EXPORT_SYMBOL(pci_match_id);
922 EXPORT_SYMBOL(__pci_register_driver);
923 EXPORT_SYMBOL(pci_unregister_driver);
924 EXPORT_SYMBOL(pci_dev_driver);
925 EXPORT_SYMBOL(pci_bus_type);
926 EXPORT_SYMBOL(pci_dev_get);
927 EXPORT_SYMBOL(pci_dev_put);