Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * pci_dn.c
0004  *
0005  * Copyright (C) 2001 Todd Inglett, IBM Corporation
0006  *
0007  * PCI manipulation via device_nodes.
0008  */
0009 #include <linux/kernel.h>
0010 #include <linux/pci.h>
0011 #include <linux/string.h>
0012 #include <linux/export.h>
0013 #include <linux/init.h>
0014 #include <linux/gfp.h>
0015 #include <linux/of.h>
0016 
0017 #include <asm/io.h>
0018 #include <asm/pci-bridge.h>
0019 #include <asm/ppc-pci.h>
0020 #include <asm/firmware.h>
0021 #include <asm/eeh.h>
0022 
0023 /*
0024  * The function is used to find the firmware data of one
0025  * specific PCI device, which is attached to the indicated
0026  * PCI bus. For VFs, their firmware data is linked to that
0027  * one of PF's bridge. For other devices, their firmware
0028  * data is linked to that of their bridge.
0029  */
0030 static struct pci_dn *pci_bus_to_pdn(struct pci_bus *bus)
0031 {
0032     struct pci_bus *pbus;
0033     struct device_node *dn;
0034     struct pci_dn *pdn;
0035 
0036     /*
0037      * We probably have virtual bus which doesn't
0038      * have associated bridge.
0039      */
0040     pbus = bus;
0041     while (pbus) {
0042         if (pci_is_root_bus(pbus) || pbus->self)
0043             break;
0044 
0045         pbus = pbus->parent;
0046     }
0047 
0048     /*
0049      * Except virtual bus, all PCI buses should
0050      * have device nodes.
0051      */
0052     dn = pci_bus_to_OF_node(pbus);
0053     pdn = dn ? PCI_DN(dn) : NULL;
0054 
0055     return pdn;
0056 }
0057 
0058 struct pci_dn *pci_get_pdn_by_devfn(struct pci_bus *bus,
0059                     int devfn)
0060 {
0061     struct device_node *dn = NULL;
0062     struct pci_dn *parent, *pdn;
0063     struct pci_dev *pdev = NULL;
0064 
0065     /* Fast path: fetch from PCI device */
0066     list_for_each_entry(pdev, &bus->devices, bus_list) {
0067         if (pdev->devfn == devfn) {
0068             if (pdev->dev.archdata.pci_data)
0069                 return pdev->dev.archdata.pci_data;
0070 
0071             dn = pci_device_to_OF_node(pdev);
0072             break;
0073         }
0074     }
0075 
0076     /* Fast path: fetch from device node */
0077     pdn = dn ? PCI_DN(dn) : NULL;
0078     if (pdn)
0079         return pdn;
0080 
0081     /* Slow path: fetch from firmware data hierarchy */
0082     parent = pci_bus_to_pdn(bus);
0083     if (!parent)
0084         return NULL;
0085 
0086     list_for_each_entry(pdn, &parent->child_list, list) {
0087         if (pdn->busno == bus->number &&
0088                     pdn->devfn == devfn)
0089                         return pdn;
0090         }
0091 
0092     return NULL;
0093 }
0094 
0095 struct pci_dn *pci_get_pdn(struct pci_dev *pdev)
0096 {
0097     struct device_node *dn;
0098     struct pci_dn *parent, *pdn;
0099 
0100     /* Search device directly */
0101     if (pdev->dev.archdata.pci_data)
0102         return pdev->dev.archdata.pci_data;
0103 
0104     /* Check device node */
0105     dn = pci_device_to_OF_node(pdev);
0106     pdn = dn ? PCI_DN(dn) : NULL;
0107     if (pdn)
0108         return pdn;
0109 
0110     /*
0111      * VFs don't have device nodes. We hook their
0112      * firmware data to PF's bridge.
0113      */
0114     parent = pci_bus_to_pdn(pdev->bus);
0115     if (!parent)
0116         return NULL;
0117 
0118     list_for_each_entry(pdn, &parent->child_list, list) {
0119         if (pdn->busno == pdev->bus->number &&
0120             pdn->devfn == pdev->devfn)
0121             return pdn;
0122     }
0123 
0124     return NULL;
0125 }
0126 
0127 #ifdef CONFIG_EEH
0128 static struct eeh_dev *eeh_dev_init(struct pci_dn *pdn)
0129 {
0130     struct eeh_dev *edev;
0131 
0132     /* Allocate EEH device */
0133     edev = kzalloc(sizeof(*edev), GFP_KERNEL);
0134     if (!edev)
0135         return NULL;
0136 
0137     /* Associate EEH device with OF node */
0138     pdn->edev = edev;
0139     edev->pdn = pdn;
0140     edev->bdfn = (pdn->busno << 8) | pdn->devfn;
0141     edev->controller = pdn->phb;
0142 
0143     return edev;
0144 }
0145 #endif /* CONFIG_EEH */
0146 
0147 #ifdef CONFIG_PCI_IOV
0148 static struct pci_dn *add_one_sriov_vf_pdn(struct pci_dn *parent,
0149                        int busno, int devfn)
0150 {
0151     struct pci_dn *pdn;
0152 
0153     /* Except PHB, we always have the parent */
0154     if (!parent)
0155         return NULL;
0156 
0157     pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
0158     if (!pdn)
0159         return NULL;
0160 
0161     pdn->phb = parent->phb;
0162     pdn->parent = parent;
0163     pdn->busno = busno;
0164     pdn->devfn = devfn;
0165     pdn->pe_number = IODA_INVALID_PE;
0166     INIT_LIST_HEAD(&pdn->child_list);
0167     INIT_LIST_HEAD(&pdn->list);
0168     list_add_tail(&pdn->list, &parent->child_list);
0169 
0170     return pdn;
0171 }
0172 
0173 struct pci_dn *add_sriov_vf_pdns(struct pci_dev *pdev)
0174 {
0175     struct pci_dn *parent, *pdn;
0176     int i;
0177 
0178     /* Only support IOV for now */
0179     if (WARN_ON(!pdev->is_physfn))
0180         return NULL;
0181 
0182     /* Check if VFs have been populated */
0183     pdn = pci_get_pdn(pdev);
0184     if (!pdn || (pdn->flags & PCI_DN_FLAG_IOV_VF))
0185         return NULL;
0186 
0187     pdn->flags |= PCI_DN_FLAG_IOV_VF;
0188     parent = pci_bus_to_pdn(pdev->bus);
0189     if (!parent)
0190         return NULL;
0191 
0192     for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) {
0193         struct eeh_dev *edev __maybe_unused;
0194 
0195         pdn = add_one_sriov_vf_pdn(parent,
0196                        pci_iov_virtfn_bus(pdev, i),
0197                        pci_iov_virtfn_devfn(pdev, i));
0198         if (!pdn) {
0199             dev_warn(&pdev->dev, "%s: Cannot create firmware data for VF#%d\n",
0200                  __func__, i);
0201             return NULL;
0202         }
0203 
0204 #ifdef CONFIG_EEH
0205         /* Create the EEH device for the VF */
0206         edev = eeh_dev_init(pdn);
0207         BUG_ON(!edev);
0208 
0209         /* FIXME: these should probably be populated by the EEH probe */
0210         edev->physfn = pdev;
0211         edev->vf_index = i;
0212 #endif /* CONFIG_EEH */
0213     }
0214     return pci_get_pdn(pdev);
0215 }
0216 
0217 void remove_sriov_vf_pdns(struct pci_dev *pdev)
0218 {
0219     struct pci_dn *parent;
0220     struct pci_dn *pdn, *tmp;
0221     int i;
0222 
0223     /* Only support IOV PF for now */
0224     if (WARN_ON(!pdev->is_physfn))
0225         return;
0226 
0227     /* Check if VFs have been populated */
0228     pdn = pci_get_pdn(pdev);
0229     if (!pdn || !(pdn->flags & PCI_DN_FLAG_IOV_VF))
0230         return;
0231 
0232     pdn->flags &= ~PCI_DN_FLAG_IOV_VF;
0233     parent = pci_bus_to_pdn(pdev->bus);
0234     if (!parent)
0235         return;
0236 
0237     /*
0238      * We might introduce flag to pci_dn in future
0239      * so that we can release VF's firmware data in
0240      * a batch mode.
0241      */
0242     for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) {
0243         struct eeh_dev *edev __maybe_unused;
0244 
0245         list_for_each_entry_safe(pdn, tmp,
0246             &parent->child_list, list) {
0247             if (pdn->busno != pci_iov_virtfn_bus(pdev, i) ||
0248                 pdn->devfn != pci_iov_virtfn_devfn(pdev, i))
0249                 continue;
0250 
0251 #ifdef CONFIG_EEH
0252             /*
0253              * Release EEH state for this VF. The PCI core
0254              * has already torn down the pci_dev for this VF, but
0255              * we're responsible to removing the eeh_dev since it
0256              * has the same lifetime as the pci_dn that spawned it.
0257              */
0258             edev = pdn_to_eeh_dev(pdn);
0259             if (edev) {
0260                 /*
0261                  * We allocate pci_dn's for the totalvfs count,
0262                  * but only the vfs that were activated
0263                  * have a configured PE.
0264                  */
0265                 if (edev->pe)
0266                     eeh_pe_tree_remove(edev);
0267 
0268                 pdn->edev = NULL;
0269                 kfree(edev);
0270             }
0271 #endif /* CONFIG_EEH */
0272 
0273             if (!list_empty(&pdn->list))
0274                 list_del(&pdn->list);
0275 
0276             kfree(pdn);
0277         }
0278     }
0279 }
0280 #endif /* CONFIG_PCI_IOV */
0281 
0282 struct pci_dn *pci_add_device_node_info(struct pci_controller *hose,
0283                     struct device_node *dn)
0284 {
0285     const __be32 *type = of_get_property(dn, "ibm,pci-config-space-type", NULL);
0286     const __be32 *regs;
0287     struct device_node *parent;
0288     struct pci_dn *pdn;
0289 #ifdef CONFIG_EEH
0290     struct eeh_dev *edev;
0291 #endif
0292 
0293     pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
0294     if (pdn == NULL)
0295         return NULL;
0296     dn->data = pdn;
0297     pdn->phb = hose;
0298     pdn->pe_number = IODA_INVALID_PE;
0299     regs = of_get_property(dn, "reg", NULL);
0300     if (regs) {
0301         u32 addr = of_read_number(regs, 1);
0302 
0303         /* First register entry is addr (00BBSS00)  */
0304         pdn->busno = (addr >> 16) & 0xff;
0305         pdn->devfn = (addr >> 8) & 0xff;
0306     }
0307 
0308     /* vendor/device IDs and class code */
0309     regs = of_get_property(dn, "vendor-id", NULL);
0310     pdn->vendor_id = regs ? of_read_number(regs, 1) : 0;
0311     regs = of_get_property(dn, "device-id", NULL);
0312     pdn->device_id = regs ? of_read_number(regs, 1) : 0;
0313     regs = of_get_property(dn, "class-code", NULL);
0314     pdn->class_code = regs ? of_read_number(regs, 1) : 0;
0315 
0316     /* Extended config space */
0317     pdn->pci_ext_config_space = (type && of_read_number(type, 1) == 1);
0318 
0319     /* Create EEH device */
0320 #ifdef CONFIG_EEH
0321     edev = eeh_dev_init(pdn);
0322     if (!edev) {
0323         kfree(pdn);
0324         return NULL;
0325     }
0326 #endif
0327 
0328     /* Attach to parent node */
0329     INIT_LIST_HEAD(&pdn->child_list);
0330     INIT_LIST_HEAD(&pdn->list);
0331     parent = of_get_parent(dn);
0332     pdn->parent = parent ? PCI_DN(parent) : NULL;
0333     if (pdn->parent)
0334         list_add_tail(&pdn->list, &pdn->parent->child_list);
0335 
0336     return pdn;
0337 }
0338 EXPORT_SYMBOL_GPL(pci_add_device_node_info);
0339 
0340 void pci_remove_device_node_info(struct device_node *dn)
0341 {
0342     struct pci_dn *pdn = dn ? PCI_DN(dn) : NULL;
0343     struct device_node *parent;
0344     struct pci_dev *pdev;
0345 #ifdef CONFIG_EEH
0346     struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
0347 
0348     if (edev)
0349         edev->pdn = NULL;
0350 #endif
0351 
0352     if (!pdn)
0353         return;
0354 
0355     WARN_ON(!list_empty(&pdn->child_list));
0356     list_del(&pdn->list);
0357 
0358     /* Drop the parent pci_dn's ref to our backing dt node */
0359     parent = of_get_parent(dn);
0360     if (parent)
0361         of_node_put(parent);
0362 
0363     /*
0364      * At this point we *might* still have a pci_dev that was
0365      * instantiated from this pci_dn. So defer free()ing it until
0366      * the pci_dev's release function is called.
0367      */
0368     pdev = pci_get_domain_bus_and_slot(pdn->phb->global_number,
0369             pdn->busno, pdn->devfn);
0370     if (pdev) {
0371         /* NB: pdev has a ref to dn */
0372         pci_dbg(pdev, "marked pdn (from %pOF) as dead\n", dn);
0373         pdn->flags |= PCI_DN_FLAG_DEAD;
0374     } else {
0375         dn->data = NULL;
0376         kfree(pdn);
0377     }
0378 
0379     pci_dev_put(pdev);
0380 }
0381 EXPORT_SYMBOL_GPL(pci_remove_device_node_info);
0382 
0383 /*
0384  * Traverse a device tree stopping each PCI device in the tree.
0385  * This is done depth first.  As each node is processed, a "pre"
0386  * function is called and the children are processed recursively.
0387  *
0388  * The "pre" func returns a value.  If non-zero is returned from
0389  * the "pre" func, the traversal stops and this value is returned.
0390  * This return value is useful when using traverse as a method of
0391  * finding a device.
0392  *
0393  * NOTE: we do not run the func for devices that do not appear to
0394  * be PCI except for the start node which we assume (this is good
0395  * because the start node is often a phb which may be missing PCI
0396  * properties).
0397  * We use the class-code as an indicator. If we run into
0398  * one of these nodes we also assume its siblings are non-pci for
0399  * performance.
0400  */
0401 void *pci_traverse_device_nodes(struct device_node *start,
0402                 void *(*fn)(struct device_node *, void *),
0403                 void *data)
0404 {
0405     struct device_node *dn, *nextdn;
0406     void *ret;
0407 
0408     /* We started with a phb, iterate all childs */
0409     for (dn = start->child; dn; dn = nextdn) {
0410         const __be32 *classp;
0411         u32 class = 0;
0412 
0413         nextdn = NULL;
0414         classp = of_get_property(dn, "class-code", NULL);
0415         if (classp)
0416             class = of_read_number(classp, 1);
0417 
0418         if (fn) {
0419             ret = fn(dn, data);
0420             if (ret)
0421                 return ret;
0422         }
0423 
0424         /* If we are a PCI bridge, go down */
0425         if (dn->child && ((class >> 8) == PCI_CLASS_BRIDGE_PCI ||
0426                   (class >> 8) == PCI_CLASS_BRIDGE_CARDBUS))
0427             /* Depth first...do children */
0428             nextdn = dn->child;
0429         else if (dn->sibling)
0430             /* ok, try next sibling instead. */
0431             nextdn = dn->sibling;
0432         if (!nextdn) {
0433             /* Walk up to next valid sibling. */
0434             do {
0435                 dn = dn->parent;
0436                 if (dn == start)
0437                     return NULL;
0438             } while (dn->sibling == NULL);
0439             nextdn = dn->sibling;
0440         }
0441     }
0442     return NULL;
0443 }
0444 EXPORT_SYMBOL_GPL(pci_traverse_device_nodes);
0445 
0446 static void *add_pdn(struct device_node *dn, void *data)
0447 {
0448     struct pci_controller *hose = data;
0449     struct pci_dn *pdn;
0450 
0451     pdn = pci_add_device_node_info(hose, dn);
0452     if (!pdn)
0453         return ERR_PTR(-ENOMEM);
0454 
0455     return NULL;
0456 }
0457 
0458 /** 
0459  * pci_devs_phb_init_dynamic - setup pci devices under this PHB
0460  * phb: pci-to-host bridge (top-level bridge connecting to cpu)
0461  *
0462  * This routine is called both during boot, (before the memory
0463  * subsystem is set up, before kmalloc is valid) and during the 
0464  * dynamic lpar operation of adding a PHB to a running system.
0465  */
0466 void pci_devs_phb_init_dynamic(struct pci_controller *phb)
0467 {
0468     struct device_node *dn = phb->dn;
0469     struct pci_dn *pdn;
0470 
0471     /* PHB nodes themselves must not match */
0472     pdn = pci_add_device_node_info(phb, dn);
0473     if (pdn) {
0474         pdn->devfn = pdn->busno = -1;
0475         pdn->vendor_id = pdn->device_id = pdn->class_code = 0;
0476         pdn->phb = phb;
0477         phb->pci_data = pdn;
0478     }
0479 
0480     /* Update dn->phb ptrs for new phb and children devices */
0481     pci_traverse_device_nodes(dn, add_pdn, phb);
0482 }
0483 
0484 static void pci_dev_pdn_setup(struct pci_dev *pdev)
0485 {
0486     struct pci_dn *pdn;
0487 
0488     if (pdev->dev.archdata.pci_data)
0489         return;
0490 
0491     /* Setup the fast path */
0492     pdn = pci_get_pdn(pdev);
0493     pdev->dev.archdata.pci_data = pdn;
0494 }
0495 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pci_dev_pdn_setup);