Merge tag 'drm-vc4-fixes-2016-09-14' of https://github.com/anholt/linux into drm...
[linux-2.6-block.git] / arch / powerpc / kernel / pci-hotplug.c
CommitLineData
317f06de
GS
1/*
2 * Derived from "arch/powerpc/platforms/pseries/pci_dlpar.c"
3 *
4 * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
5 * Copyright (C) 2005 International Business Machines
6 *
7 * Updates, 2005, John Rose <johnrose@austin.ibm.com>
8 * Updates, 2005, Linas Vepstas <linas@austin.ibm.com>
9 * Updates, 2013, Gavin Shan <shangw@linux.vnet.ibm.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
17#include <linux/pci.h>
18#include <linux/export.h>
19#include <asm/pci-bridge.h>
20#include <asm/ppc-pci.h>
21#include <asm/firmware.h>
22#include <asm/eeh.h>
23
6384d977
GS
24static struct pci_bus *find_bus_among_children(struct pci_bus *bus,
25 struct device_node *dn)
26{
27 struct pci_bus *child = NULL;
28 struct pci_bus *tmp;
29
30 if (pci_bus_to_OF_node(bus) == dn)
31 return bus;
32
33 list_for_each_entry(tmp, &bus->children, node) {
34 child = find_bus_among_children(tmp, dn);
35 if (child)
36 break;
37 }
38
39 return child;
40}
41
42struct pci_bus *pci_find_bus_by_node(struct device_node *dn)
43{
44 struct pci_dn *pdn = PCI_DN(dn);
45
46 if (!pdn || !pdn->phb || !pdn->phb->bus)
47 return NULL;
48
49 return find_bus_among_children(pdn->phb->bus, dn);
50}
51EXPORT_SYMBOL_GPL(pci_find_bus_by_node);
52
008a4938
GS
53/**
54 * pcibios_release_device - release PCI device
55 * @dev: PCI device
56 *
57 * The function is called before releasing the indicated PCI device.
58 */
59void pcibios_release_device(struct pci_dev *dev)
60{
10e79630
MN
61 struct pci_controller *phb = pci_bus_to_host(dev->bus);
62
807a827d 63 eeh_remove_device(dev);
10e79630
MN
64
65 if (phb->controller_ops.release_device)
66 phb->controller_ops.release_device(dev);
008a4938
GS
67}
68
317f06de 69/**
bd251b89 70 * pci_hp_remove_devices - remove all devices under this bus
317f06de 71 * @bus: the indicated PCI bus
317f06de
GS
72 *
73 * Remove all of the PCI devices under this bus both from the
74 * linux pci device tree, and from the powerpc EEH address cache.
317f06de 75 */
bd251b89 76void pci_hp_remove_devices(struct pci_bus *bus)
317f06de
GS
77{
78 struct pci_dev *dev, *tmp;
79 struct pci_bus *child_bus;
80
81 /* First go down child busses */
82 list_for_each_entry(child_bus, &bus->children, node)
bd251b89 83 pci_hp_remove_devices(child_bus);
317f06de
GS
84
85 pr_debug("PCI: Removing devices on bus %04x:%02x\n",
86 pci_domain_nr(bus), bus->number);
971427f5 87 list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
807a827d 88 pr_debug(" Removing %s...\n", pci_name(dev));
317f06de
GS
89 pci_stop_and_remove_bus_device(dev);
90 }
91}
bd251b89 92EXPORT_SYMBOL_GPL(pci_hp_remove_devices);
317f06de
GS
93
94/**
bd251b89 95 * pci_hp_add_devices - adds new pci devices to bus
317f06de
GS
96 * @bus: the indicated PCI bus
97 *
98 * This routine will find and fixup new pci devices under
99 * the indicated bus. This routine presumes that there
100 * might already be some devices under this bridge, so
101 * it carefully tries to add only new devices. (And that
102 * is how this routine differs from other, similar pcibios
103 * routines.)
104 */
bd251b89 105void pci_hp_add_devices(struct pci_bus *bus)
317f06de 106{
ab444ec9 107 int slotno, mode, pass, max;
317f06de 108 struct pci_dev *dev;
467efc2e 109 struct pci_controller *phb;
317f06de
GS
110 struct device_node *dn = pci_bus_to_OF_node(bus);
111
ff57b454 112 eeh_add_device_tree_early(PCI_DN(dn));
317f06de 113
467efc2e
DA
114 phb = pci_bus_to_host(bus);
115
317f06de 116 mode = PCI_PROBE_NORMAL;
467efc2e
DA
117 if (phb->controller_ops.probe_mode)
118 mode = phb->controller_ops.probe_mode(bus);
317f06de
GS
119
120 if (mode == PCI_PROBE_DEVTREE) {
121 /* use ofdt-based probe */
122 of_rescan_bus(dn, bus);
4a5954ed
GS
123 } else if (mode == PCI_PROBE_NORMAL &&
124 dn->child && PCI_DN(dn->child)) {
ab444ec9
GS
125 /*
126 * Use legacy probe. In the partial hotplug case, we
127 * probably have grandchildren devices unplugged. So
128 * we don't check the return value from pci_scan_slot() in
129 * order for fully rescan all the way down to pick them up.
130 * They can have been removed during partial hotplug.
131 */
317f06de 132 slotno = PCI_SLOT(PCI_DN(dn->child)->devfn);
ab444ec9 133 pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
317f06de
GS
134 pcibios_setup_bus_devices(bus);
135 max = bus->busn_res.start;
136 for (pass = 0; pass < 2; pass++) {
137 list_for_each_entry(dev, &bus->devices, bus_list) {
c888770e 138 if (pci_is_bridge(dev))
317f06de
GS
139 max = pci_scan_bridge(bus, dev,
140 max, pass);
141 }
142 }
143 }
144 pcibios_finish_adding_to_bus(bus);
145}
bd251b89 146EXPORT_SYMBOL_GPL(pci_hp_add_devices);