1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PCI Dynamic LPAR, PCI Hot Plug and PCI EEH recovery code
4 * for RPA-compliant PPC64 platform.
5 * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
6 * Copyright (C) 2005 International Business Machines
8 * Updates, 2005, John Rose <johnrose@austin.ibm.com>
9 * Updates, 2005, Linas Vepstas <linas@austin.ibm.com>
12 #include <linux/pci.h>
13 #include <linux/export.h>
14 #include <asm/pci-bridge.h>
15 #include <asm/ppc-pci.h>
16 #include <asm/firmware.h>
21 struct pci_controller *init_phb_dynamic(struct device_node *dn)
23 struct pci_controller *phb;
25 pr_debug("PCI: Initializing new hotplug PHB %pOF\n", dn);
27 phb = pcibios_alloc_controller(dn);
31 pci_process_bridge_OF_ranges(phb, dn, 0);
32 phb->controller_ops = pseries_pci_controller_ops;
34 pci_devs_phb_init_dynamic(phb);
36 pseries_msi_allocate_domains(phb);
38 /* Create EEH devices for the PHB */
39 eeh_phb_pe_create(phb);
42 pseries_eeh_init_edev_recursive(PCI_DN(dn));
44 pcibios_scan_phb(phb);
45 pcibios_finish_adding_to_bus(phb->bus);
49 EXPORT_SYMBOL_GPL(init_phb_dynamic);
51 /* RPA-specific bits for removing PHBs */
52 int remove_phb_dynamic(struct pci_controller *phb)
54 struct pci_bus *b = phb->bus;
55 struct pci_host_bridge *host_bridge = to_pci_host_bridge(b->bridge);
59 pr_debug("PCI: Removing PHB %04x:%02x...\n",
60 pci_domain_nr(b), b->number);
62 /* We cannot to remove a root bus that has children */
63 if (!(list_empty(&b->children) && list_empty(&b->devices)))
66 /* We -know- there aren't any child devices anymore at this stage
67 * and thus, we can safely unmap the IO space as it's not in use
69 res = &phb->io_resource;
70 if (res->flags & IORESOURCE_IO) {
71 rc = pcibios_unmap_io_space(b);
73 printk(KERN_ERR "%s: failed to unmap IO on bus %s\n",
79 pseries_msi_free_domains(phb);
81 /* Keep a reference so phb isn't freed yet */
82 get_device(&host_bridge->dev);
84 /* Remove the PCI bus and unregister the bridge device from sysfs */
87 host_bridge->bus = NULL;
88 device_unregister(&host_bridge->dev);
90 /* Now release the IO resource */
91 if (res->flags & IORESOURCE_IO)
92 release_resource(res);
94 /* Release memory resources */
95 for (i = 0; i < 3; ++i) {
96 res = &phb->mem_resources[i];
97 if (!(res->flags & IORESOURCE_MEM))
99 release_resource(res);
103 * The pci_controller data structure is freed by
104 * the pcibios_free_controller_deferred() callback;
105 * see pseries_root_bridge_prepare().
107 put_device(&host_bridge->dev);
111 EXPORT_SYMBOL_GPL(remove_phb_dynamic);