1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/pci.h> 3 #include <linux/module.h> 4 #include <linux/of.h> 5 #include <linux/of_platform.h> 6 #include <linux/platform_device.h> 7 8 #include "pci.h" 9 10 static void pci_free_resources(struct pci_dev *dev) 11 { 12 struct resource *res; 13 14 pci_dev_for_each_resource(dev, res) { 15 if (res->parent) 16 release_resource(res); 17 } 18 } 19 20 static void pci_pwrctrl_unregister(struct device *dev) 21 { 22 struct device_node *np; 23 struct platform_device *pdev; 24 25 np = dev_of_node(dev); 26 if (!np) 27 return; 28 29 pdev = of_find_device_by_node(np); 30 if (!pdev) 31 return; 32 33 of_device_unregister(pdev); 34 of_node_clear_flag(np, OF_POPULATED); 35 } 36 37 static void pci_stop_dev(struct pci_dev *dev) 38 { 39 pci_pme_active(dev, false); 40 41 if (!pci_dev_test_and_clear_added(dev)) 42 return; 43 44 device_release_driver(&dev->dev); 45 pci_proc_detach_device(dev); 46 pci_remove_sysfs_dev_files(dev); 47 of_pci_remove_node(dev); 48 } 49 50 static void pci_destroy_dev(struct pci_dev *dev) 51 { 52 if (pci_dev_test_and_set_removed(dev)) 53 return; 54 55 pci_doe_sysfs_teardown(dev); 56 pci_npem_remove(dev); 57 58 device_del(&dev->dev); 59 60 down_write(&pci_bus_sem); 61 list_del(&dev->bus_list); 62 up_write(&pci_bus_sem); 63 64 pci_doe_destroy(dev); 65 pcie_aspm_exit_link_state(dev); 66 pci_bridge_d3_update(dev); 67 pci_pwrctrl_unregister(&dev->dev); 68 pci_free_resources(dev); 69 put_device(&dev->dev); 70 } 71 72 void pci_remove_bus(struct pci_bus *bus) 73 { 74 pci_proc_detach_bus(bus); 75 76 down_write(&pci_bus_sem); 77 list_del(&bus->node); 78 pci_bus_release_busn_res(bus); 79 up_write(&pci_bus_sem); 80 pci_remove_legacy_files(bus); 81 82 if (bus->ops->remove_bus) 83 bus->ops->remove_bus(bus); 84 85 pcibios_remove_bus(bus); 86 device_unregister(&bus->dev); 87 } 88 EXPORT_SYMBOL(pci_remove_bus); 89 90 static void pci_stop_bus_device(struct pci_dev *dev) 91 { 92 struct pci_bus *bus = dev->subordinate; 93 struct pci_dev *child, *tmp; 94 95 /* 96 * Stopping an SR-IOV PF device removes all the associated VFs, 97 * which will update the bus->devices list and confuse the 98 * iterator. Therefore, iterate in reverse so we remove the VFs 99 * first, then the PF. 100 */ 101 if (bus) { 102 list_for_each_entry_safe_reverse(child, tmp, 103 &bus->devices, bus_list) 104 pci_stop_bus_device(child); 105 } 106 107 pci_stop_dev(dev); 108 } 109 110 static void pci_remove_bus_device(struct pci_dev *dev) 111 { 112 struct pci_bus *bus = dev->subordinate; 113 struct pci_dev *child, *tmp; 114 115 if (bus) { 116 list_for_each_entry_safe(child, tmp, 117 &bus->devices, bus_list) 118 pci_remove_bus_device(child); 119 120 pci_remove_bus(bus); 121 dev->subordinate = NULL; 122 } 123 124 pci_destroy_dev(dev); 125 } 126 127 /** 128 * pci_stop_and_remove_bus_device - remove a PCI device and any children 129 * @dev: the device to remove 130 * 131 * Remove a PCI device from the device lists, informing the drivers 132 * that the device has been removed. We also remove any subordinate 133 * buses and children in a depth-first manner. 134 * 135 * For each device we remove, delete the device structure from the 136 * device lists, remove the /proc entry, and notify userspace 137 * (/sbin/hotplug). 138 */ 139 void pci_stop_and_remove_bus_device(struct pci_dev *dev) 140 { 141 pci_stop_bus_device(dev); 142 pci_remove_bus_device(dev); 143 } 144 EXPORT_SYMBOL(pci_stop_and_remove_bus_device); 145 146 void pci_stop_and_remove_bus_device_locked(struct pci_dev *dev) 147 { 148 pci_lock_rescan_remove(); 149 pci_stop_and_remove_bus_device(dev); 150 pci_unlock_rescan_remove(); 151 } 152 EXPORT_SYMBOL_GPL(pci_stop_and_remove_bus_device_locked); 153 154 void pci_stop_root_bus(struct pci_bus *bus) 155 { 156 struct pci_dev *child, *tmp; 157 struct pci_host_bridge *host_bridge; 158 159 if (!pci_is_root_bus(bus)) 160 return; 161 162 host_bridge = to_pci_host_bridge(bus->bridge); 163 list_for_each_entry_safe_reverse(child, tmp, 164 &bus->devices, bus_list) 165 pci_stop_bus_device(child); 166 167 of_pci_remove_host_bridge_node(host_bridge); 168 169 /* stop the host bridge */ 170 device_release_driver(&host_bridge->dev); 171 } 172 EXPORT_SYMBOL_GPL(pci_stop_root_bus); 173 174 void pci_remove_root_bus(struct pci_bus *bus) 175 { 176 struct pci_dev *child, *tmp; 177 struct pci_host_bridge *host_bridge; 178 179 if (!pci_is_root_bus(bus)) 180 return; 181 182 host_bridge = to_pci_host_bridge(bus->bridge); 183 list_for_each_entry_safe(child, tmp, 184 &bus->devices, bus_list) 185 pci_remove_bus_device(child); 186 187 #ifdef CONFIG_PCI_DOMAINS_GENERIC 188 /* Release domain_nr if it was dynamically allocated */ 189 if (host_bridge->domain_nr == PCI_DOMAIN_NR_NOT_SET) 190 pci_bus_release_domain_nr(host_bridge->dev.parent, bus->domain_nr); 191 #endif 192 193 pci_remove_bus(bus); 194 host_bridge->bus = NULL; 195 196 /* remove the host bridge */ 197 device_del(&host_bridge->dev); 198 } 199 EXPORT_SYMBOL_GPL(pci_remove_root_bus); 200