remove.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include "pci.h"
  8. static void pci_free_resources(struct pci_dev *dev)
  9. {
  10. struct resource *res;
  11. pci_dev_for_each_resource(dev, res) {
  12. if (res->parent)
  13. release_resource(res);
  14. }
  15. }
  16. static int pci_pwrctl_unregister(struct device *dev, void *data)
  17. {
  18. struct device_node *pci_node = data, *plat_node = dev_of_node(dev);
  19. if (dev_is_platform(dev) && plat_node && plat_node == pci_node) {
  20. of_device_unregister(to_platform_device(dev));
  21. of_node_clear_flag(plat_node, OF_POPULATED);
  22. }
  23. return 0;
  24. }
  25. static void pci_stop_dev(struct pci_dev *dev)
  26. {
  27. pci_pme_active(dev, false);
  28. if (pci_dev_is_added(dev)) {
  29. device_for_each_child(dev->dev.parent, dev_of_node(&dev->dev),
  30. pci_pwrctl_unregister);
  31. device_release_driver(&dev->dev);
  32. pci_proc_detach_device(dev);
  33. pci_remove_sysfs_dev_files(dev);
  34. of_pci_remove_node(dev);
  35. pci_dev_assign_added(dev, false);
  36. }
  37. }
  38. static void pci_destroy_dev(struct pci_dev *dev)
  39. {
  40. if (!dev->dev.kobj.parent)
  41. return;
  42. pci_npem_remove(dev);
  43. device_del(&dev->dev);
  44. down_write(&pci_bus_sem);
  45. list_del(&dev->bus_list);
  46. up_write(&pci_bus_sem);
  47. pci_doe_destroy(dev);
  48. pcie_aspm_exit_link_state(dev);
  49. pci_bridge_d3_update(dev);
  50. pci_free_resources(dev);
  51. put_device(&dev->dev);
  52. }
  53. void pci_remove_bus(struct pci_bus *bus)
  54. {
  55. pci_proc_detach_bus(bus);
  56. down_write(&pci_bus_sem);
  57. list_del(&bus->node);
  58. pci_bus_release_busn_res(bus);
  59. up_write(&pci_bus_sem);
  60. pci_remove_legacy_files(bus);
  61. if (bus->ops->remove_bus)
  62. bus->ops->remove_bus(bus);
  63. pcibios_remove_bus(bus);
  64. device_unregister(&bus->dev);
  65. }
  66. EXPORT_SYMBOL(pci_remove_bus);
  67. static void pci_stop_bus_device(struct pci_dev *dev)
  68. {
  69. struct pci_bus *bus = dev->subordinate;
  70. struct pci_dev *child, *tmp;
  71. /*
  72. * Stopping an SR-IOV PF device removes all the associated VFs,
  73. * which will update the bus->devices list and confuse the
  74. * iterator. Therefore, iterate in reverse so we remove the VFs
  75. * first, then the PF.
  76. */
  77. if (bus) {
  78. list_for_each_entry_safe_reverse(child, tmp,
  79. &bus->devices, bus_list)
  80. pci_stop_bus_device(child);
  81. }
  82. pci_stop_dev(dev);
  83. }
  84. static void pci_remove_bus_device(struct pci_dev *dev)
  85. {
  86. struct pci_bus *bus = dev->subordinate;
  87. struct pci_dev *child, *tmp;
  88. if (bus) {
  89. list_for_each_entry_safe(child, tmp,
  90. &bus->devices, bus_list)
  91. pci_remove_bus_device(child);
  92. pci_remove_bus(bus);
  93. dev->subordinate = NULL;
  94. }
  95. pci_destroy_dev(dev);
  96. }
  97. /**
  98. * pci_stop_and_remove_bus_device - remove a PCI device and any children
  99. * @dev: the device to remove
  100. *
  101. * Remove a PCI device from the device lists, informing the drivers
  102. * that the device has been removed. We also remove any subordinate
  103. * buses and children in a depth-first manner.
  104. *
  105. * For each device we remove, delete the device structure from the
  106. * device lists, remove the /proc entry, and notify userspace
  107. * (/sbin/hotplug).
  108. */
  109. void pci_stop_and_remove_bus_device(struct pci_dev *dev)
  110. {
  111. pci_stop_bus_device(dev);
  112. pci_remove_bus_device(dev);
  113. }
  114. EXPORT_SYMBOL(pci_stop_and_remove_bus_device);
  115. void pci_stop_and_remove_bus_device_locked(struct pci_dev *dev)
  116. {
  117. pci_lock_rescan_remove();
  118. pci_stop_and_remove_bus_device(dev);
  119. pci_unlock_rescan_remove();
  120. }
  121. EXPORT_SYMBOL_GPL(pci_stop_and_remove_bus_device_locked);
  122. void pci_stop_root_bus(struct pci_bus *bus)
  123. {
  124. struct pci_dev *child, *tmp;
  125. struct pci_host_bridge *host_bridge;
  126. if (!pci_is_root_bus(bus))
  127. return;
  128. host_bridge = to_pci_host_bridge(bus->bridge);
  129. list_for_each_entry_safe_reverse(child, tmp,
  130. &bus->devices, bus_list)
  131. pci_stop_bus_device(child);
  132. /* stop the host bridge */
  133. device_release_driver(&host_bridge->dev);
  134. }
  135. EXPORT_SYMBOL_GPL(pci_stop_root_bus);
  136. void pci_remove_root_bus(struct pci_bus *bus)
  137. {
  138. struct pci_dev *child, *tmp;
  139. struct pci_host_bridge *host_bridge;
  140. if (!pci_is_root_bus(bus))
  141. return;
  142. host_bridge = to_pci_host_bridge(bus->bridge);
  143. list_for_each_entry_safe(child, tmp,
  144. &bus->devices, bus_list)
  145. pci_remove_bus_device(child);
  146. #ifdef CONFIG_PCI_DOMAINS_GENERIC
  147. /* Release domain_nr if it was dynamically allocated */
  148. if (host_bridge->domain_nr == PCI_DOMAIN_NR_NOT_SET)
  149. pci_bus_release_domain_nr(host_bridge->dev.parent, bus->domain_nr);
  150. #endif
  151. pci_remove_bus(bus);
  152. host_bridge->bus = NULL;
  153. /* remove the host bridge */
  154. device_del(&host_bridge->dev);
  155. }
  156. EXPORT_SYMBOL_GPL(pci_remove_root_bus);