pci-hotplug.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Derived from "arch/powerpc/platforms/pseries/pci_dlpar.c"
  4. *
  5. * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
  6. * Copyright (C) 2005 International Business Machines
  7. *
  8. * Updates, 2005, John Rose <johnrose@austin.ibm.com>
  9. * Updates, 2005, Linas Vepstas <linas@austin.ibm.com>
  10. * Updates, 2013, Gavin Shan <shangw@linux.vnet.ibm.com>
  11. */
  12. #include <linux/pci.h>
  13. #include <linux/export.h>
  14. #include <linux/of.h>
  15. #include <asm/pci-bridge.h>
  16. #include <asm/ppc-pci.h>
  17. #include <asm/firmware.h>
  18. #include <asm/eeh.h>
  19. static struct pci_bus *find_bus_among_children(struct pci_bus *bus,
  20. struct device_node *dn)
  21. {
  22. struct pci_bus *child = NULL;
  23. struct pci_bus *tmp;
  24. if (pci_bus_to_OF_node(bus) == dn)
  25. return bus;
  26. list_for_each_entry(tmp, &bus->children, node) {
  27. child = find_bus_among_children(tmp, dn);
  28. if (child)
  29. break;
  30. }
  31. return child;
  32. }
  33. struct pci_bus *pci_find_bus_by_node(struct device_node *dn)
  34. {
  35. struct pci_dn *pdn = PCI_DN(dn);
  36. if (!pdn || !pdn->phb || !pdn->phb->bus)
  37. return NULL;
  38. return find_bus_among_children(pdn->phb->bus, dn);
  39. }
  40. EXPORT_SYMBOL_GPL(pci_find_bus_by_node);
  41. /**
  42. * pcibios_release_device - release PCI device
  43. * @dev: PCI device
  44. *
  45. * The function is called before releasing the indicated PCI device.
  46. */
  47. void pcibios_release_device(struct pci_dev *dev)
  48. {
  49. struct pci_controller *phb = pci_bus_to_host(dev->bus);
  50. struct pci_dn *pdn = pci_get_pdn(dev);
  51. if (phb->controller_ops.release_device)
  52. phb->controller_ops.release_device(dev);
  53. /* free()ing the pci_dn has been deferred to us, do it now */
  54. if (pdn && (pdn->flags & PCI_DN_FLAG_DEAD)) {
  55. pci_dbg(dev, "freeing dead pdn\n");
  56. kfree(pdn);
  57. }
  58. }
  59. /**
  60. * pci_hp_remove_devices - remove all devices under this bus
  61. * @bus: the indicated PCI bus
  62. *
  63. * Remove all of the PCI devices under this bus both from the
  64. * linux pci device tree, and from the powerpc EEH address cache.
  65. */
  66. void pci_hp_remove_devices(struct pci_bus *bus)
  67. {
  68. struct pci_dev *dev, *tmp;
  69. struct pci_bus *child_bus;
  70. /* First go down child busses */
  71. list_for_each_entry(child_bus, &bus->children, node)
  72. pci_hp_remove_devices(child_bus);
  73. pr_debug("PCI: Removing devices on bus %04x:%02x\n",
  74. pci_domain_nr(bus), bus->number);
  75. list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
  76. pr_debug(" Removing %s...\n", pci_name(dev));
  77. pci_stop_and_remove_bus_device(dev);
  78. }
  79. }
  80. EXPORT_SYMBOL_GPL(pci_hp_remove_devices);
  81. static void traverse_siblings_and_scan_slot(struct device_node *start, struct pci_bus *bus)
  82. {
  83. struct device_node *dn;
  84. int slotno;
  85. u32 class = 0;
  86. if (!of_property_read_u32(start->child, "class-code", &class)) {
  87. /* Call of pci_scan_slot for non-bridge/EP case */
  88. if (!((class >> 8) == PCI_CLASS_BRIDGE_PCI)) {
  89. slotno = PCI_SLOT(PCI_DN(start->child)->devfn);
  90. pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
  91. return;
  92. }
  93. }
  94. /* Iterate all siblings */
  95. for_each_child_of_node(start, dn) {
  96. class = 0;
  97. if (!of_property_read_u32(start->child, "class-code", &class)) {
  98. /* Call of pci_scan_slot on each sibling-nodes/bridge-ports */
  99. if ((class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  100. slotno = PCI_SLOT(PCI_DN(dn)->devfn);
  101. pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
  102. }
  103. }
  104. }
  105. }
  106. /**
  107. * pci_hp_add_devices - adds new pci devices to bus
  108. * @bus: the indicated PCI bus
  109. *
  110. * This routine will find and fixup new pci devices under
  111. * the indicated bus. This routine presumes that there
  112. * might already be some devices under this bridge, so
  113. * it carefully tries to add only new devices. (And that
  114. * is how this routine differs from other, similar pcibios
  115. * routines.)
  116. */
  117. void pci_hp_add_devices(struct pci_bus *bus)
  118. {
  119. int mode, max;
  120. struct pci_dev *dev;
  121. struct pci_controller *phb;
  122. struct device_node *dn = pci_bus_to_OF_node(bus);
  123. if (!dn)
  124. return;
  125. phb = pci_bus_to_host(bus);
  126. mode = PCI_PROBE_NORMAL;
  127. if (phb->controller_ops.probe_mode)
  128. mode = phb->controller_ops.probe_mode(bus);
  129. if (mode == PCI_PROBE_DEVTREE) {
  130. /* use ofdt-based probe */
  131. of_rescan_bus(dn, bus);
  132. } else if (mode == PCI_PROBE_NORMAL &&
  133. dn->child && PCI_DN(dn->child)) {
  134. /*
  135. * Use legacy probe. In the partial hotplug case, we
  136. * probably have grandchildren devices unplugged. So
  137. * we don't check the return value from pci_scan_slot() in
  138. * order for fully rescan all the way down to pick them up.
  139. * They can have been removed during partial hotplug.
  140. */
  141. traverse_siblings_and_scan_slot(dn, bus);
  142. max = bus->busn_res.start;
  143. /*
  144. * Scan bridges that are already configured. We don't touch
  145. * them unless they are misconfigured (which will be done in
  146. * the second scan below).
  147. */
  148. for_each_pci_bridge(dev, bus)
  149. max = pci_scan_bridge(bus, dev, max, 0);
  150. /* Scan bridges that need to be reconfigured */
  151. for_each_pci_bridge(dev, bus)
  152. max = pci_scan_bridge(bus, dev, max, 1);
  153. }
  154. pcibios_finish_adding_to_bus(bus);
  155. }
  156. EXPORT_SYMBOL_GPL(pci_hp_add_devices);