1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/module.h>
4 #include <linux/of_platform.h>
7 static void pci_free_resources(struct pci_dev *dev)
11 pci_dev_for_each_resource(dev, res) {
13 release_resource(res);
17 static void pci_stop_dev(struct pci_dev *dev)
19 pci_pme_active(dev, false);
21 if (pci_dev_is_added(dev)) {
22 of_platform_depopulate(&dev->dev);
23 device_release_driver(&dev->dev);
24 pci_proc_detach_device(dev);
25 pci_remove_sysfs_dev_files(dev);
26 of_pci_remove_node(dev);
28 pci_dev_assign_added(dev, false);
32 static void pci_destroy_dev(struct pci_dev *dev)
34 if (!dev->dev.kobj.parent)
37 device_del(&dev->dev);
39 down_write(&pci_bus_sem);
40 list_del(&dev->bus_list);
41 up_write(&pci_bus_sem);
44 pcie_aspm_exit_link_state(dev);
45 pci_bridge_d3_update(dev);
46 pci_free_resources(dev);
47 put_device(&dev->dev);
50 void pci_remove_bus(struct pci_bus *bus)
52 pci_proc_detach_bus(bus);
54 down_write(&pci_bus_sem);
56 pci_bus_release_busn_res(bus);
57 up_write(&pci_bus_sem);
58 pci_remove_legacy_files(bus);
60 if (bus->ops->remove_bus)
61 bus->ops->remove_bus(bus);
63 pcibios_remove_bus(bus);
64 device_unregister(&bus->dev);
66 EXPORT_SYMBOL(pci_remove_bus);
68 static void pci_stop_bus_device(struct pci_dev *dev)
70 struct pci_bus *bus = dev->subordinate;
71 struct pci_dev *child, *tmp;
74 * Stopping an SR-IOV PF device removes all the associated VFs,
75 * which will update the bus->devices list and confuse the
76 * iterator. Therefore, iterate in reverse so we remove the VFs
80 list_for_each_entry_safe_reverse(child, tmp,
81 &bus->devices, bus_list)
82 pci_stop_bus_device(child);
88 static void pci_remove_bus_device(struct pci_dev *dev)
90 struct pci_bus *bus = dev->subordinate;
91 struct pci_dev *child, *tmp;
94 list_for_each_entry_safe(child, tmp,
95 &bus->devices, bus_list)
96 pci_remove_bus_device(child);
99 dev->subordinate = NULL;
102 pci_destroy_dev(dev);
106 * pci_stop_and_remove_bus_device - remove a PCI device and any children
107 * @dev: the device to remove
109 * Remove a PCI device from the device lists, informing the drivers
110 * that the device has been removed. We also remove any subordinate
111 * buses and children in a depth-first manner.
113 * For each device we remove, delete the device structure from the
114 * device lists, remove the /proc entry, and notify userspace
117 void pci_stop_and_remove_bus_device(struct pci_dev *dev)
119 pci_stop_bus_device(dev);
120 pci_remove_bus_device(dev);
122 EXPORT_SYMBOL(pci_stop_and_remove_bus_device);
124 void pci_stop_and_remove_bus_device_locked(struct pci_dev *dev)
126 pci_lock_rescan_remove();
127 pci_stop_and_remove_bus_device(dev);
128 pci_unlock_rescan_remove();
130 EXPORT_SYMBOL_GPL(pci_stop_and_remove_bus_device_locked);
132 void pci_stop_root_bus(struct pci_bus *bus)
134 struct pci_dev *child, *tmp;
135 struct pci_host_bridge *host_bridge;
137 if (!pci_is_root_bus(bus))
140 host_bridge = to_pci_host_bridge(bus->bridge);
141 list_for_each_entry_safe_reverse(child, tmp,
142 &bus->devices, bus_list)
143 pci_stop_bus_device(child);
145 /* stop the host bridge */
146 device_release_driver(&host_bridge->dev);
148 EXPORT_SYMBOL_GPL(pci_stop_root_bus);
150 void pci_remove_root_bus(struct pci_bus *bus)
152 struct pci_dev *child, *tmp;
153 struct pci_host_bridge *host_bridge;
155 if (!pci_is_root_bus(bus))
158 host_bridge = to_pci_host_bridge(bus->bridge);
159 list_for_each_entry_safe(child, tmp,
160 &bus->devices, bus_list)
161 pci_remove_bus_device(child);
163 #ifdef CONFIG_PCI_DOMAINS_GENERIC
164 /* Release domain_nr if it was dynamically allocated */
165 if (host_bridge->domain_nr == PCI_DOMAIN_NR_NOT_SET)
166 pci_bus_release_domain_nr(bus, host_bridge->dev.parent);
170 host_bridge->bus = NULL;
172 /* remove the host bridge */
173 device_del(&host_bridge->dev);
175 EXPORT_SYMBOL_GPL(pci_remove_root_bus);