pci-hotplug.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <[email protected]>
  6. * Copyright (C) 2005 International Business Machines
  7. *
  8. * Updates, 2005, John Rose <[email protected]>
  9. * Updates, 2005, Linas Vepstas <[email protected]>
  10. * Updates, 2013, Gavin Shan <[email protected]>
  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. /**
  82. * pci_hp_add_devices - adds new pci devices to bus
  83. * @bus: the indicated PCI bus
  84. *
  85. * This routine will find and fixup new pci devices under
  86. * the indicated bus. This routine presumes that there
  87. * might already be some devices under this bridge, so
  88. * it carefully tries to add only new devices. (And that
  89. * is how this routine differs from other, similar pcibios
  90. * routines.)
  91. */
  92. void pci_hp_add_devices(struct pci_bus *bus)
  93. {
  94. int slotno, mode, max;
  95. struct pci_dev *dev;
  96. struct pci_controller *phb;
  97. struct device_node *dn = pci_bus_to_OF_node(bus);
  98. phb = pci_bus_to_host(bus);
  99. mode = PCI_PROBE_NORMAL;
  100. if (phb->controller_ops.probe_mode)
  101. mode = phb->controller_ops.probe_mode(bus);
  102. if (mode == PCI_PROBE_DEVTREE) {
  103. /* use ofdt-based probe */
  104. of_rescan_bus(dn, bus);
  105. } else if (mode == PCI_PROBE_NORMAL &&
  106. dn->child && PCI_DN(dn->child)) {
  107. /*
  108. * Use legacy probe. In the partial hotplug case, we
  109. * probably have grandchildren devices unplugged. So
  110. * we don't check the return value from pci_scan_slot() in
  111. * order for fully rescan all the way down to pick them up.
  112. * They can have been removed during partial hotplug.
  113. */
  114. slotno = PCI_SLOT(PCI_DN(dn->child)->devfn);
  115. pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
  116. max = bus->busn_res.start;
  117. /*
  118. * Scan bridges that are already configured. We don't touch
  119. * them unless they are misconfigured (which will be done in
  120. * the second scan below).
  121. */
  122. for_each_pci_bridge(dev, bus)
  123. max = pci_scan_bridge(bus, dev, max, 0);
  124. /* Scan bridges that need to be reconfigured */
  125. for_each_pci_bridge(dev, bus)
  126. max = pci_scan_bridge(bus, dev, max, 1);
  127. }
  128. pcibios_finish_adding_to_bus(bus);
  129. }
  130. EXPORT_SYMBOL_GPL(pci_hp_add_devices);