11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
27568cb4eSPaul Mackerras /*
37568cb4eSPaul Mackerras * pci_dn.c
47568cb4eSPaul Mackerras *
57568cb4eSPaul Mackerras * Copyright (C) 2001 Todd Inglett, IBM Corporation
67568cb4eSPaul Mackerras *
77568cb4eSPaul Mackerras * PCI manipulation via device_nodes.
87568cb4eSPaul Mackerras */
97568cb4eSPaul Mackerras #include <linux/kernel.h>
107568cb4eSPaul Mackerras #include <linux/pci.h>
117568cb4eSPaul Mackerras #include <linux/string.h>
1266b15db6SPaul Gortmaker #include <linux/export.h>
137568cb4eSPaul Mackerras #include <linux/init.h>
145a0e3ad6STejun Heo #include <linux/gfp.h>
15e6f6390aSChristophe Leroy #include <linux/of.h>
167568cb4eSPaul Mackerras
177568cb4eSPaul Mackerras #include <asm/io.h>
187568cb4eSPaul Mackerras #include <asm/pci-bridge.h>
197568cb4eSPaul Mackerras #include <asm/ppc-pci.h>
20095eed4fSStephen Rothwell #include <asm/firmware.h>
21d468fcafSMichael Ellerman #include <asm/eeh.h>
227568cb4eSPaul Mackerras
23cca87d30SGavin Shan /*
24cca87d30SGavin Shan * The function is used to find the firmware data of one
25cca87d30SGavin Shan * specific PCI device, which is attached to the indicated
26cca87d30SGavin Shan * PCI bus. For VFs, their firmware data is linked to that
27cca87d30SGavin Shan * one of PF's bridge. For other devices, their firmware
28cca87d30SGavin Shan * data is linked to that of their bridge.
29cca87d30SGavin Shan */
pci_bus_to_pdn(struct pci_bus * bus)30cca87d30SGavin Shan static struct pci_dn *pci_bus_to_pdn(struct pci_bus *bus)
31cca87d30SGavin Shan {
32cca87d30SGavin Shan struct pci_bus *pbus;
33cca87d30SGavin Shan struct device_node *dn;
34cca87d30SGavin Shan struct pci_dn *pdn;
35cca87d30SGavin Shan
36cca87d30SGavin Shan /*
37cca87d30SGavin Shan * We probably have virtual bus which doesn't
38cca87d30SGavin Shan * have associated bridge.
39cca87d30SGavin Shan */
40cca87d30SGavin Shan pbus = bus;
41cca87d30SGavin Shan while (pbus) {
42cca87d30SGavin Shan if (pci_is_root_bus(pbus) || pbus->self)
43cca87d30SGavin Shan break;
44cca87d30SGavin Shan
45cca87d30SGavin Shan pbus = pbus->parent;
46cca87d30SGavin Shan }
47cca87d30SGavin Shan
48cca87d30SGavin Shan /*
49cca87d30SGavin Shan * Except virtual bus, all PCI buses should
50cca87d30SGavin Shan * have device nodes.
51cca87d30SGavin Shan */
52cca87d30SGavin Shan dn = pci_bus_to_OF_node(pbus);
53cca87d30SGavin Shan pdn = dn ? PCI_DN(dn) : NULL;
54cca87d30SGavin Shan
55cca87d30SGavin Shan return pdn;
56cca87d30SGavin Shan }
57cca87d30SGavin Shan
pci_get_pdn_by_devfn(struct pci_bus * bus,int devfn)58cca87d30SGavin Shan struct pci_dn *pci_get_pdn_by_devfn(struct pci_bus *bus,
59cca87d30SGavin Shan int devfn)
60cca87d30SGavin Shan {
61cca87d30SGavin Shan struct device_node *dn = NULL;
62cca87d30SGavin Shan struct pci_dn *parent, *pdn;
63cca87d30SGavin Shan struct pci_dev *pdev = NULL;
64cca87d30SGavin Shan
65cca87d30SGavin Shan /* Fast path: fetch from PCI device */
66cca87d30SGavin Shan list_for_each_entry(pdev, &bus->devices, bus_list) {
67cca87d30SGavin Shan if (pdev->devfn == devfn) {
68cca87d30SGavin Shan if (pdev->dev.archdata.pci_data)
69cca87d30SGavin Shan return pdev->dev.archdata.pci_data;
70cca87d30SGavin Shan
71cca87d30SGavin Shan dn = pci_device_to_OF_node(pdev);
72cca87d30SGavin Shan break;
73cca87d30SGavin Shan }
74cca87d30SGavin Shan }
75cca87d30SGavin Shan
76cca87d30SGavin Shan /* Fast path: fetch from device node */
77cca87d30SGavin Shan pdn = dn ? PCI_DN(dn) : NULL;
78cca87d30SGavin Shan if (pdn)
79cca87d30SGavin Shan return pdn;
80cca87d30SGavin Shan
81cca87d30SGavin Shan /* Slow path: fetch from firmware data hierarchy */
82cca87d30SGavin Shan parent = pci_bus_to_pdn(bus);
83cca87d30SGavin Shan if (!parent)
84cca87d30SGavin Shan return NULL;
85cca87d30SGavin Shan
86cca87d30SGavin Shan list_for_each_entry(pdn, &parent->child_list, list) {
87cca87d30SGavin Shan if (pdn->busno == bus->number &&
88cca87d30SGavin Shan pdn->devfn == devfn)
89cca87d30SGavin Shan return pdn;
90cca87d30SGavin Shan }
91cca87d30SGavin Shan
92cca87d30SGavin Shan return NULL;
93cca87d30SGavin Shan }
94cca87d30SGavin Shan
pci_get_pdn(struct pci_dev * pdev)95b72c1f65SBenjamin Herrenschmidt struct pci_dn *pci_get_pdn(struct pci_dev *pdev)
96b72c1f65SBenjamin Herrenschmidt {
97cca87d30SGavin Shan struct device_node *dn;
98cca87d30SGavin Shan struct pci_dn *parent, *pdn;
99cca87d30SGavin Shan
100cca87d30SGavin Shan /* Search device directly */
101cca87d30SGavin Shan if (pdev->dev.archdata.pci_data)
102cca87d30SGavin Shan return pdev->dev.archdata.pci_data;
103cca87d30SGavin Shan
104cca87d30SGavin Shan /* Check device node */
105cca87d30SGavin Shan dn = pci_device_to_OF_node(pdev);
106cca87d30SGavin Shan pdn = dn ? PCI_DN(dn) : NULL;
107cca87d30SGavin Shan if (pdn)
108cca87d30SGavin Shan return pdn;
109cca87d30SGavin Shan
110cca87d30SGavin Shan /*
111cca87d30SGavin Shan * VFs don't have device nodes. We hook their
112cca87d30SGavin Shan * firmware data to PF's bridge.
113cca87d30SGavin Shan */
114cca87d30SGavin Shan parent = pci_bus_to_pdn(pdev->bus);
115cca87d30SGavin Shan if (!parent)
116b72c1f65SBenjamin Herrenschmidt return NULL;
117cca87d30SGavin Shan
118cca87d30SGavin Shan list_for_each_entry(pdn, &parent->child_list, list) {
119cca87d30SGavin Shan if (pdn->busno == pdev->bus->number &&
120cca87d30SGavin Shan pdn->devfn == pdev->devfn)
121cca87d30SGavin Shan return pdn;
122cca87d30SGavin Shan }
123cca87d30SGavin Shan
124cca87d30SGavin Shan return NULL;
125b72c1f65SBenjamin Herrenschmidt }
126b72c1f65SBenjamin Herrenschmidt
127d74ee8e9SOliver O'Halloran #ifdef CONFIG_EEH
eeh_dev_init(struct pci_dn * pdn)128d74ee8e9SOliver O'Halloran static struct eeh_dev *eeh_dev_init(struct pci_dn *pdn)
129d74ee8e9SOliver O'Halloran {
130d74ee8e9SOliver O'Halloran struct eeh_dev *edev;
131d74ee8e9SOliver O'Halloran
132d74ee8e9SOliver O'Halloran /* Allocate EEH device */
133d74ee8e9SOliver O'Halloran edev = kzalloc(sizeof(*edev), GFP_KERNEL);
134d74ee8e9SOliver O'Halloran if (!edev)
135d74ee8e9SOliver O'Halloran return NULL;
136d74ee8e9SOliver O'Halloran
137d74ee8e9SOliver O'Halloran /* Associate EEH device with OF node */
138d74ee8e9SOliver O'Halloran pdn->edev = edev;
139d74ee8e9SOliver O'Halloran edev->pdn = pdn;
140d74ee8e9SOliver O'Halloran edev->bdfn = (pdn->busno << 8) | pdn->devfn;
141d74ee8e9SOliver O'Halloran edev->controller = pdn->phb;
142d74ee8e9SOliver O'Halloran
143d74ee8e9SOliver O'Halloran return edev;
144d74ee8e9SOliver O'Halloran }
145d74ee8e9SOliver O'Halloran #endif /* CONFIG_EEH */
146d74ee8e9SOliver O'Halloran
147a8b2f828SGavin Shan #ifdef CONFIG_PCI_IOV
add_one_sriov_vf_pdn(struct pci_dn * parent,int busno,int devfn)1488cd6aaccSOliver O'Halloran static struct pci_dn *add_one_sriov_vf_pdn(struct pci_dn *parent,
149a8b2f828SGavin Shan int busno, int devfn)
150a8b2f828SGavin Shan {
151a8b2f828SGavin Shan struct pci_dn *pdn;
152a8b2f828SGavin Shan
153a8b2f828SGavin Shan /* Except PHB, we always have the parent */
154a8b2f828SGavin Shan if (!parent)
155a8b2f828SGavin Shan return NULL;
156a8b2f828SGavin Shan
157a8b2f828SGavin Shan pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
1585f600b17SAlexey Kardashevskiy if (!pdn)
159a8b2f828SGavin Shan return NULL;
160a8b2f828SGavin Shan
161a8b2f828SGavin Shan pdn->phb = parent->phb;
162a8b2f828SGavin Shan pdn->parent = parent;
163a8b2f828SGavin Shan pdn->busno = busno;
164a8b2f828SGavin Shan pdn->devfn = devfn;
165a8b2f828SGavin Shan pdn->pe_number = IODA_INVALID_PE;
166a8b2f828SGavin Shan INIT_LIST_HEAD(&pdn->child_list);
167a8b2f828SGavin Shan INIT_LIST_HEAD(&pdn->list);
168a8b2f828SGavin Shan list_add_tail(&pdn->list, &parent->child_list);
169a8b2f828SGavin Shan
170a8b2f828SGavin Shan return pdn;
171a8b2f828SGavin Shan }
172a8b2f828SGavin Shan
add_sriov_vf_pdns(struct pci_dev * pdev)1738cd6aaccSOliver O'Halloran struct pci_dn *add_sriov_vf_pdns(struct pci_dev *pdev)
174a8b2f828SGavin Shan {
175a8b2f828SGavin Shan struct pci_dn *parent, *pdn;
176a8b2f828SGavin Shan int i;
177a8b2f828SGavin Shan
178a8b2f828SGavin Shan /* Only support IOV for now */
179a4af49f3SOliver O'Halloran if (WARN_ON(!pdev->is_physfn))
180a4af49f3SOliver O'Halloran return NULL;
181a8b2f828SGavin Shan
182a8b2f828SGavin Shan /* Check if VFs have been populated */
183a8b2f828SGavin Shan pdn = pci_get_pdn(pdev);
184a8b2f828SGavin Shan if (!pdn || (pdn->flags & PCI_DN_FLAG_IOV_VF))
185a8b2f828SGavin Shan return NULL;
186a8b2f828SGavin Shan
187a8b2f828SGavin Shan pdn->flags |= PCI_DN_FLAG_IOV_VF;
188a8b2f828SGavin Shan parent = pci_bus_to_pdn(pdev->bus);
189a8b2f828SGavin Shan if (!parent)
190a8b2f828SGavin Shan return NULL;
191a8b2f828SGavin Shan
192a8b2f828SGavin Shan for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) {
193d468fcafSMichael Ellerman struct eeh_dev *edev __maybe_unused;
194d468fcafSMichael Ellerman
195dffa9153SOliver O'Halloran pdn = add_one_sriov_vf_pdn(parent,
196a8b2f828SGavin Shan pci_iov_virtfn_bus(pdev, i),
197a8b2f828SGavin Shan pci_iov_virtfn_devfn(pdev, i));
198a8b2f828SGavin Shan if (!pdn) {
199a8b2f828SGavin Shan dev_warn(&pdev->dev, "%s: Cannot create firmware data for VF#%d\n",
200a8b2f828SGavin Shan __func__, i);
201a8b2f828SGavin Shan return NULL;
202a8b2f828SGavin Shan }
20339218cd0SWei Yang
204fb36e907SRussell Currey #ifdef CONFIG_EEH
20539218cd0SWei Yang /* Create the EEH device for the VF */
2068cc7581cSGavin Shan edev = eeh_dev_init(pdn);
20739218cd0SWei Yang BUG_ON(!edev);
208dffa9153SOliver O'Halloran
209dffa9153SOliver O'Halloran /* FIXME: these should probably be populated by the EEH probe */
21039218cd0SWei Yang edev->physfn = pdev;
211dffa9153SOliver O'Halloran edev->vf_index = i;
212fb36e907SRussell Currey #endif /* CONFIG_EEH */
213a8b2f828SGavin Shan }
214a8b2f828SGavin Shan return pci_get_pdn(pdev);
215a8b2f828SGavin Shan }
216a8b2f828SGavin Shan
remove_sriov_vf_pdns(struct pci_dev * pdev)2178cd6aaccSOliver O'Halloran void remove_sriov_vf_pdns(struct pci_dev *pdev)
218a8b2f828SGavin Shan {
219a8b2f828SGavin Shan struct pci_dn *parent;
220a8b2f828SGavin Shan struct pci_dn *pdn, *tmp;
221a8b2f828SGavin Shan int i;
222a8b2f828SGavin Shan
223a8b2f828SGavin Shan /* Only support IOV PF for now */
224a4af49f3SOliver O'Halloran if (WARN_ON(!pdev->is_physfn))
225a8b2f828SGavin Shan return;
226a8b2f828SGavin Shan
227a8b2f828SGavin Shan /* Check if VFs have been populated */
228a8b2f828SGavin Shan pdn = pci_get_pdn(pdev);
229a8b2f828SGavin Shan if (!pdn || !(pdn->flags & PCI_DN_FLAG_IOV_VF))
230a8b2f828SGavin Shan return;
231a8b2f828SGavin Shan
232a8b2f828SGavin Shan pdn->flags &= ~PCI_DN_FLAG_IOV_VF;
233a8b2f828SGavin Shan parent = pci_bus_to_pdn(pdev->bus);
234a8b2f828SGavin Shan if (!parent)
235a8b2f828SGavin Shan return;
236a8b2f828SGavin Shan
237a8b2f828SGavin Shan /*
238a8b2f828SGavin Shan * We might introduce flag to pci_dn in future
239a8b2f828SGavin Shan * so that we can release VF's firmware data in
240a8b2f828SGavin Shan * a batch mode.
241a8b2f828SGavin Shan */
242a8b2f828SGavin Shan for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) {
243d468fcafSMichael Ellerman struct eeh_dev *edev __maybe_unused;
244d468fcafSMichael Ellerman
245a8b2f828SGavin Shan list_for_each_entry_safe(pdn, tmp,
246a8b2f828SGavin Shan &parent->child_list, list) {
247a8b2f828SGavin Shan if (pdn->busno != pci_iov_virtfn_bus(pdev, i) ||
248a8b2f828SGavin Shan pdn->devfn != pci_iov_virtfn_devfn(pdev, i))
249a8b2f828SGavin Shan continue;
250a8b2f828SGavin Shan
251fb36e907SRussell Currey #ifdef CONFIG_EEH
2521fb4124cSOliver O'Halloran /*
2531fb4124cSOliver O'Halloran * Release EEH state for this VF. The PCI core
2541fb4124cSOliver O'Halloran * has already torn down the pci_dev for this VF, but
2551fb4124cSOliver O'Halloran * we're responsible to removing the eeh_dev since it
2561fb4124cSOliver O'Halloran * has the same lifetime as the pci_dn that spawned it.
2571fb4124cSOliver O'Halloran */
25839218cd0SWei Yang edev = pdn_to_eeh_dev(pdn);
25939218cd0SWei Yang if (edev) {
2601fb4124cSOliver O'Halloran /*
2611fb4124cSOliver O'Halloran * We allocate pci_dn's for the totalvfs count,
2622b461880SMichael Ellerman * but only the vfs that were activated
2631fb4124cSOliver O'Halloran * have a configured PE.
2641fb4124cSOliver O'Halloran */
2651fb4124cSOliver O'Halloran if (edev->pe)
266d923ab7aSOliver O'Halloran eeh_pe_tree_remove(edev);
2671fb4124cSOliver O'Halloran
26839218cd0SWei Yang pdn->edev = NULL;
26939218cd0SWei Yang kfree(edev);
27039218cd0SWei Yang }
271fb36e907SRussell Currey #endif /* CONFIG_EEH */
27239218cd0SWei Yang
273a8b2f828SGavin Shan if (!list_empty(&pdn->list))
274a8b2f828SGavin Shan list_del(&pdn->list);
275a8b2f828SGavin Shan
276a8b2f828SGavin Shan kfree(pdn);
277a8b2f828SGavin Shan }
278a8b2f828SGavin Shan }
279a8b2f828SGavin Shan }
2808cd6aaccSOliver O'Halloran #endif /* CONFIG_PCI_IOV */
281a8b2f828SGavin Shan
pci_add_device_node_info(struct pci_controller * hose,struct device_node * dn)282d8f66f41SGavin Shan struct pci_dn *pci_add_device_node_info(struct pci_controller *hose,
283d8f66f41SGavin Shan struct device_node *dn)
2847568cb4eSPaul Mackerras {
285c6296b96SAnton Blanchard const __be32 *type = of_get_property(dn, "ibm,pci-config-space-type", NULL);
286c6296b96SAnton Blanchard const __be32 *regs;
287cca87d30SGavin Shan struct device_node *parent;
2887568cb4eSPaul Mackerras struct pci_dn *pdn;
2898cc7581cSGavin Shan #ifdef CONFIG_EEH
2908cc7581cSGavin Shan struct eeh_dev *edev;
2918cc7581cSGavin Shan #endif
2927568cb4eSPaul Mackerras
2938cc7581cSGavin Shan pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
2947568cb4eSPaul Mackerras if (pdn == NULL)
2957568cb4eSPaul Mackerras return NULL;
2967568cb4eSPaul Mackerras dn->data = pdn;
297d8f66f41SGavin Shan pdn->phb = hose;
298184cd4a3SBenjamin Herrenschmidt pdn->pe_number = IODA_INVALID_PE;
299e2eb6392SStephen Rothwell regs = of_get_property(dn, "reg", NULL);
3007568cb4eSPaul Mackerras if (regs) {
301c6296b96SAnton Blanchard u32 addr = of_read_number(regs, 1);
302c6296b96SAnton Blanchard
3037568cb4eSPaul Mackerras /* First register entry is addr (00BBSS00) */
304c6296b96SAnton Blanchard pdn->busno = (addr >> 16) & 0xff;
305c6296b96SAnton Blanchard pdn->devfn = (addr >> 8) & 0xff;
3067568cb4eSPaul Mackerras }
3077568cb4eSPaul Mackerras
308c035ff1dSGavin Shan /* vendor/device IDs and class code */
309c035ff1dSGavin Shan regs = of_get_property(dn, "vendor-id", NULL);
310c035ff1dSGavin Shan pdn->vendor_id = regs ? of_read_number(regs, 1) : 0;
311c035ff1dSGavin Shan regs = of_get_property(dn, "device-id", NULL);
312c035ff1dSGavin Shan pdn->device_id = regs ? of_read_number(regs, 1) : 0;
313c035ff1dSGavin Shan regs = of_get_property(dn, "class-code", NULL);
314c035ff1dSGavin Shan pdn->class_code = regs ? of_read_number(regs, 1) : 0;
315c035ff1dSGavin Shan
316c035ff1dSGavin Shan /* Extended config space */
317c6296b96SAnton Blanchard pdn->pci_ext_config_space = (type && of_read_number(type, 1) == 1);
318cca87d30SGavin Shan
3198cc7581cSGavin Shan /* Create EEH device */
3208cc7581cSGavin Shan #ifdef CONFIG_EEH
3218cc7581cSGavin Shan edev = eeh_dev_init(pdn);
3228cc7581cSGavin Shan if (!edev) {
3238cc7581cSGavin Shan kfree(pdn);
3248cc7581cSGavin Shan return NULL;
3258cc7581cSGavin Shan }
3268cc7581cSGavin Shan #endif
3278cc7581cSGavin Shan
328cca87d30SGavin Shan /* Attach to parent node */
329cca87d30SGavin Shan INIT_LIST_HEAD(&pdn->child_list);
330cca87d30SGavin Shan INIT_LIST_HEAD(&pdn->list);
331cca87d30SGavin Shan parent = of_get_parent(dn);
332cca87d30SGavin Shan pdn->parent = parent ? PCI_DN(parent) : NULL;
333*110a1fcbSLiang He of_node_put(parent);
334cca87d30SGavin Shan if (pdn->parent)
335cca87d30SGavin Shan list_add_tail(&pdn->list, &pdn->parent->child_list);
336cca87d30SGavin Shan
337d8f66f41SGavin Shan return pdn;
3387568cb4eSPaul Mackerras }
339d8f66f41SGavin Shan EXPORT_SYMBOL_GPL(pci_add_device_node_info);
3407568cb4eSPaul Mackerras
pci_remove_device_node_info(struct device_node * dn)341de5a28acSGavin Shan void pci_remove_device_node_info(struct device_node *dn)
342de5a28acSGavin Shan {
343de5a28acSGavin Shan struct pci_dn *pdn = dn ? PCI_DN(dn) : NULL;
34414db3d52SAlexey Kardashevskiy struct device_node *parent;
3455ef753aeSOliver O'Halloran struct pci_dev *pdev;
346de5a28acSGavin Shan #ifdef CONFIG_EEH
347de5a28acSGavin Shan struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
348de5a28acSGavin Shan
349de5a28acSGavin Shan if (edev)
350de5a28acSGavin Shan edev->pdn = NULL;
351de5a28acSGavin Shan #endif
352de5a28acSGavin Shan
353de5a28acSGavin Shan if (!pdn)
354de5a28acSGavin Shan return;
355de5a28acSGavin Shan
356de5a28acSGavin Shan WARN_ON(!list_empty(&pdn->child_list));
357de5a28acSGavin Shan list_del(&pdn->list);
35814db3d52SAlexey Kardashevskiy
3595ef753aeSOliver O'Halloran /* Drop the parent pci_dn's ref to our backing dt node */
36014db3d52SAlexey Kardashevskiy parent = of_get_parent(dn);
36114db3d52SAlexey Kardashevskiy if (parent)
36214db3d52SAlexey Kardashevskiy of_node_put(parent);
363de5a28acSGavin Shan
3645ef753aeSOliver O'Halloran /*
3655ef753aeSOliver O'Halloran * At this point we *might* still have a pci_dev that was
3665ef753aeSOliver O'Halloran * instantiated from this pci_dn. So defer free()ing it until
3675ef753aeSOliver O'Halloran * the pci_dev's release function is called.
3685ef753aeSOliver O'Halloran */
3695ef753aeSOliver O'Halloran pdev = pci_get_domain_bus_and_slot(pdn->phb->global_number,
3705ef753aeSOliver O'Halloran pdn->busno, pdn->devfn);
3715ef753aeSOliver O'Halloran if (pdev) {
3725ef753aeSOliver O'Halloran /* NB: pdev has a ref to dn */
3735ef753aeSOliver O'Halloran pci_dbg(pdev, "marked pdn (from %pOF) as dead\n", dn);
3745ef753aeSOliver O'Halloran pdn->flags |= PCI_DN_FLAG_DEAD;
3755ef753aeSOliver O'Halloran } else {
376de5a28acSGavin Shan dn->data = NULL;
377de5a28acSGavin Shan kfree(pdn);
378de5a28acSGavin Shan }
3795ef753aeSOliver O'Halloran
3805ef753aeSOliver O'Halloran pci_dev_put(pdev);
3815ef753aeSOliver O'Halloran }
382de5a28acSGavin Shan EXPORT_SYMBOL_GPL(pci_remove_device_node_info);
383de5a28acSGavin Shan
3847568cb4eSPaul Mackerras /*
3857568cb4eSPaul Mackerras * Traverse a device tree stopping each PCI device in the tree.
3867568cb4eSPaul Mackerras * This is done depth first. As each node is processed, a "pre"
3877568cb4eSPaul Mackerras * function is called and the children are processed recursively.
3887568cb4eSPaul Mackerras *
3897568cb4eSPaul Mackerras * The "pre" func returns a value. If non-zero is returned from
3907568cb4eSPaul Mackerras * the "pre" func, the traversal stops and this value is returned.
3917568cb4eSPaul Mackerras * This return value is useful when using traverse as a method of
3927568cb4eSPaul Mackerras * finding a device.
3937568cb4eSPaul Mackerras *
3947568cb4eSPaul Mackerras * NOTE: we do not run the func for devices that do not appear to
3957568cb4eSPaul Mackerras * be PCI except for the start node which we assume (this is good
3967568cb4eSPaul Mackerras * because the start node is often a phb which may be missing PCI
3977568cb4eSPaul Mackerras * properties).
3987568cb4eSPaul Mackerras * We use the class-code as an indicator. If we run into
3997568cb4eSPaul Mackerras * one of these nodes we also assume its siblings are non-pci for
4007568cb4eSPaul Mackerras * performance.
4017568cb4eSPaul Mackerras */
pci_traverse_device_nodes(struct device_node * start,void * (* fn)(struct device_node *,void *),void * data)402cdddc577SGavin Shan void *pci_traverse_device_nodes(struct device_node *start,
403cdddc577SGavin Shan void *(*fn)(struct device_node *, void *),
4047568cb4eSPaul Mackerras void *data)
4057568cb4eSPaul Mackerras {
4067568cb4eSPaul Mackerras struct device_node *dn, *nextdn;
4077568cb4eSPaul Mackerras void *ret;
4087568cb4eSPaul Mackerras
4097568cb4eSPaul Mackerras /* We started with a phb, iterate all childs */
4107568cb4eSPaul Mackerras for (dn = start->child; dn; dn = nextdn) {
411c6296b96SAnton Blanchard const __be32 *classp;
412c6296b96SAnton Blanchard u32 class = 0;
4137568cb4eSPaul Mackerras
4147568cb4eSPaul Mackerras nextdn = NULL;
415e2eb6392SStephen Rothwell classp = of_get_property(dn, "class-code", NULL);
416c6296b96SAnton Blanchard if (classp)
417c6296b96SAnton Blanchard class = of_read_number(classp, 1);
4187568cb4eSPaul Mackerras
419cdddc577SGavin Shan if (fn) {
420cdddc577SGavin Shan ret = fn(dn, data);
421cdddc577SGavin Shan if (ret)
4227568cb4eSPaul Mackerras return ret;
423cdddc577SGavin Shan }
4247568cb4eSPaul Mackerras
4257568cb4eSPaul Mackerras /* If we are a PCI bridge, go down */
4267568cb4eSPaul Mackerras if (dn->child && ((class >> 8) == PCI_CLASS_BRIDGE_PCI ||
4277568cb4eSPaul Mackerras (class >> 8) == PCI_CLASS_BRIDGE_CARDBUS))
4287568cb4eSPaul Mackerras /* Depth first...do children */
4297568cb4eSPaul Mackerras nextdn = dn->child;
4307568cb4eSPaul Mackerras else if (dn->sibling)
4317568cb4eSPaul Mackerras /* ok, try next sibling instead. */
4327568cb4eSPaul Mackerras nextdn = dn->sibling;
4337568cb4eSPaul Mackerras if (!nextdn) {
4347568cb4eSPaul Mackerras /* Walk up to next valid sibling. */
4357568cb4eSPaul Mackerras do {
4367568cb4eSPaul Mackerras dn = dn->parent;
4377568cb4eSPaul Mackerras if (dn == start)
4387568cb4eSPaul Mackerras return NULL;
4397568cb4eSPaul Mackerras } while (dn->sibling == NULL);
4407568cb4eSPaul Mackerras nextdn = dn->sibling;
4417568cb4eSPaul Mackerras }
4427568cb4eSPaul Mackerras }
4437568cb4eSPaul Mackerras return NULL;
4447568cb4eSPaul Mackerras }
445cdddc577SGavin Shan EXPORT_SYMBOL_GPL(pci_traverse_device_nodes);
4467568cb4eSPaul Mackerras
add_pdn(struct device_node * dn,void * data)447d8f66f41SGavin Shan static void *add_pdn(struct device_node *dn, void *data)
448d8f66f41SGavin Shan {
449d8f66f41SGavin Shan struct pci_controller *hose = data;
450d8f66f41SGavin Shan struct pci_dn *pdn;
451d8f66f41SGavin Shan
452d8f66f41SGavin Shan pdn = pci_add_device_node_info(hose, dn);
453d8f66f41SGavin Shan if (!pdn)
454d8f66f41SGavin Shan return ERR_PTR(-ENOMEM);
455d8f66f41SGavin Shan
456d8f66f41SGavin Shan return NULL;
457d8f66f41SGavin Shan }
458d8f66f41SGavin Shan
4597568cb4eSPaul Mackerras /**
4607568cb4eSPaul Mackerras * pci_devs_phb_init_dynamic - setup pci devices under this PHB
4617568cb4eSPaul Mackerras * phb: pci-to-host bridge (top-level bridge connecting to cpu)
4627568cb4eSPaul Mackerras *
4637568cb4eSPaul Mackerras * This routine is called both during boot, (before the memory
4647568cb4eSPaul Mackerras * subsystem is set up, before kmalloc is valid) and during the
4657568cb4eSPaul Mackerras * dynamic lpar operation of adding a PHB to a running system.
4667568cb4eSPaul Mackerras */
pci_devs_phb_init_dynamic(struct pci_controller * phb)467cad5cef6SGreg Kroah-Hartman void pci_devs_phb_init_dynamic(struct pci_controller *phb)
4687568cb4eSPaul Mackerras {
46944ef3390SStephen Rothwell struct device_node *dn = phb->dn;
4707568cb4eSPaul Mackerras struct pci_dn *pdn;
4717568cb4eSPaul Mackerras
4727568cb4eSPaul Mackerras /* PHB nodes themselves must not match */
473d8f66f41SGavin Shan pdn = pci_add_device_node_info(phb, dn);
474cca87d30SGavin Shan if (pdn) {
4757568cb4eSPaul Mackerras pdn->devfn = pdn->busno = -1;
476c035ff1dSGavin Shan pdn->vendor_id = pdn->device_id = pdn->class_code = 0;
477cca87d30SGavin Shan pdn->phb = phb;
478cca87d30SGavin Shan phb->pci_data = pdn;
479cca87d30SGavin Shan }
4807568cb4eSPaul Mackerras
4817568cb4eSPaul Mackerras /* Update dn->phb ptrs for new phb and children devices */
482cdddc577SGavin Shan pci_traverse_device_nodes(dn, add_pdn, phb);
4837568cb4eSPaul Mackerras }
4847568cb4eSPaul Mackerras
pci_dev_pdn_setup(struct pci_dev * pdev)485cca87d30SGavin Shan static void pci_dev_pdn_setup(struct pci_dev *pdev)
486cca87d30SGavin Shan {
487cca87d30SGavin Shan struct pci_dn *pdn;
488cca87d30SGavin Shan
489cca87d30SGavin Shan if (pdev->dev.archdata.pci_data)
490cca87d30SGavin Shan return;
491cca87d30SGavin Shan
492cca87d30SGavin Shan /* Setup the fast path */
493cca87d30SGavin Shan pdn = pci_get_pdn(pdev);
494cca87d30SGavin Shan pdev->dev.archdata.pci_data = pdn;
495cca87d30SGavin Shan }
496cca87d30SGavin Shan DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pci_dev_pdn_setup);
497