1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/module.h>
5 #include <linux/of_platform.h>
6 #include <linux/platform_device.h>
10 static void pci_free_resources(struct pci_dev
*dev
)
14 pci_dev_for_each_resource(dev
, res
) {
16 release_resource(res
);
20 static void pci_pwrctrl_unregister(struct device
*dev
)
22 struct platform_device
*pdev
;
24 pdev
= of_find_device_by_node(dev_of_node(dev
));
28 of_device_unregister(pdev
);
29 of_node_clear_flag(dev_of_node(dev
), OF_POPULATED
);
32 static void pci_stop_dev(struct pci_dev
*dev
)
34 pci_pme_active(dev
, false);
36 if (!pci_dev_test_and_clear_added(dev
))
39 pci_pwrctrl_unregister(&dev
->dev
);
40 device_release_driver(&dev
->dev
);
41 pci_proc_detach_device(dev
);
42 pci_remove_sysfs_dev_files(dev
);
43 of_pci_remove_node(dev
);
46 static void pci_destroy_dev(struct pci_dev
*dev
)
48 if (pci_dev_test_and_set_removed(dev
))
53 device_del(&dev
->dev
);
55 down_write(&pci_bus_sem
);
56 list_del(&dev
->bus_list
);
57 up_write(&pci_bus_sem
);
60 pcie_aspm_exit_link_state(dev
);
61 pci_bridge_d3_update(dev
);
62 pci_free_resources(dev
);
63 put_device(&dev
->dev
);
66 void pci_remove_bus(struct pci_bus
*bus
)
68 pci_proc_detach_bus(bus
);
70 down_write(&pci_bus_sem
);
72 pci_bus_release_busn_res(bus
);
73 up_write(&pci_bus_sem
);
74 pci_remove_legacy_files(bus
);
76 if (bus
->ops
->remove_bus
)
77 bus
->ops
->remove_bus(bus
);
79 pcibios_remove_bus(bus
);
80 device_unregister(&bus
->dev
);
82 EXPORT_SYMBOL(pci_remove_bus
);
84 static void pci_stop_bus_device(struct pci_dev
*dev
)
86 struct pci_bus
*bus
= dev
->subordinate
;
87 struct pci_dev
*child
, *tmp
;
90 * Stopping an SR-IOV PF device removes all the associated VFs,
91 * which will update the bus->devices list and confuse the
92 * iterator. Therefore, iterate in reverse so we remove the VFs
96 list_for_each_entry_safe_reverse(child
, tmp
,
97 &bus
->devices
, bus_list
)
98 pci_stop_bus_device(child
);
104 static void pci_remove_bus_device(struct pci_dev
*dev
)
106 struct pci_bus
*bus
= dev
->subordinate
;
107 struct pci_dev
*child
, *tmp
;
110 list_for_each_entry_safe(child
, tmp
,
111 &bus
->devices
, bus_list
)
112 pci_remove_bus_device(child
);
115 dev
->subordinate
= NULL
;
118 pci_destroy_dev(dev
);
122 * pci_stop_and_remove_bus_device - remove a PCI device and any children
123 * @dev: the device to remove
125 * Remove a PCI device from the device lists, informing the drivers
126 * that the device has been removed. We also remove any subordinate
127 * buses and children in a depth-first manner.
129 * For each device we remove, delete the device structure from the
130 * device lists, remove the /proc entry, and notify userspace
133 void pci_stop_and_remove_bus_device(struct pci_dev
*dev
)
135 pci_stop_bus_device(dev
);
136 pci_remove_bus_device(dev
);
138 EXPORT_SYMBOL(pci_stop_and_remove_bus_device
);
140 void pci_stop_and_remove_bus_device_locked(struct pci_dev
*dev
)
142 pci_lock_rescan_remove();
143 pci_stop_and_remove_bus_device(dev
);
144 pci_unlock_rescan_remove();
146 EXPORT_SYMBOL_GPL(pci_stop_and_remove_bus_device_locked
);
148 void pci_stop_root_bus(struct pci_bus
*bus
)
150 struct pci_dev
*child
, *tmp
;
151 struct pci_host_bridge
*host_bridge
;
153 if (!pci_is_root_bus(bus
))
156 host_bridge
= to_pci_host_bridge(bus
->bridge
);
157 list_for_each_entry_safe_reverse(child
, tmp
,
158 &bus
->devices
, bus_list
)
159 pci_stop_bus_device(child
);
161 /* stop the host bridge */
162 device_release_driver(&host_bridge
->dev
);
164 EXPORT_SYMBOL_GPL(pci_stop_root_bus
);
166 void pci_remove_root_bus(struct pci_bus
*bus
)
168 struct pci_dev
*child
, *tmp
;
169 struct pci_host_bridge
*host_bridge
;
171 if (!pci_is_root_bus(bus
))
174 host_bridge
= to_pci_host_bridge(bus
->bridge
);
175 list_for_each_entry_safe(child
, tmp
,
176 &bus
->devices
, bus_list
)
177 pci_remove_bus_device(child
);
179 #ifdef CONFIG_PCI_DOMAINS_GENERIC
180 /* Release domain_nr if it was dynamically allocated */
181 if (host_bridge
->domain_nr
== PCI_DOMAIN_NR_NOT_SET
)
182 pci_bus_release_domain_nr(host_bridge
->dev
.parent
, bus
->domain_nr
);
186 host_bridge
->bus
= NULL
;
188 /* remove the host bridge */
189 device_del(&host_bridge
->dev
);
191 EXPORT_SYMBOL_GPL(pci_remove_root_bus
);