From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41453) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZAXuJ-0000p0-TY for qemu-devel@nongnu.org; Thu, 02 Jul 2015 02:23:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZAXuG-0002P3-EB for qemu-devel@nongnu.org; Thu, 02 Jul 2015 02:23:35 -0400 From: David Gibson Date: Thu, 2 Jul 2015 16:23:22 +1000 Message-Id: <1435818208-11808-22-git-send-email-david@gibson.dropbear.id.au> In-Reply-To: <1435818208-11808-1-git-send-email-david@gibson.dropbear.id.au> References: <1435818208-11808-1-git-send-email-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH 21/27] spapr_pci: populate ibm,loc-code List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: agraf@suse.de, afaerber@suse.de Cc: lvivier@redhat.com, thuth@redhat.com, Nikunj A Dadhania , aik@ozlabs.ru, qemu-devel@nongnu.org, mdroth@linux.vnet.ibm.com, qemu-ppc@nongnu.org, bharata@linux.vnet.ibm.com, David Gibson From: Nikunj A Dadhania Each hardware instance has a platform unique location code. The OF device tree that describes a part of a hardware entity must include the =E2=80=9Cibm,loc-code=E2=80=9D property with a value that represents = the location code for that hardware entity. Populate ibm,loc-code. 1) PCI passthru devices need to identify with its own ibm,loc-code available on the host. In failure cases use: vfio_:::. 2) Emulated devices encode as following: qemu_:::. Signed-off-by: Nikunj A Dadhania Signed-off-by: David Gibson --- hw/ppc/spapr_pci.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++= +----- 1 file changed, 71 insertions(+), 6 deletions(-) diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c index 7c3621c..7585983 100644 --- a/hw/ppc/spapr_pci.c +++ b/hw/ppc/spapr_pci.c @@ -747,6 +747,60 @@ static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus= , void *opaque, int devfn) return &phb->iommu_as; } =20 +static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState *sphb, PCIDevice= *pdev) +{ + char *path =3D NULL, *buf =3D NULL, *host =3D NULL; + + /* Get the PCI VFIO host id */ + host =3D object_property_get_str(OBJECT(pdev), "host", NULL); + if (!host) { + goto err_out; + } + + /* Construct the path of the file that will give us the DT location = */ + path =3D g_strdup_printf("/sys/bus/pci/devices/%s/devspec", host); + g_free(host); + if (!path || !g_file_get_contents(path, &buf, NULL, NULL)) { + goto err_out; + } + g_free(path); + + /* Construct and read from host device tree the loc-code */ + path =3D g_strdup_printf("/proc/device-tree%s/ibm,loc-code", buf); + g_free(buf); + if (!path || !g_file_get_contents(path, &buf, NULL, NULL)) { + goto err_out; + } + return buf; + +err_out: + g_free(path); + return NULL; +} + +static char *spapr_phb_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev= ) +{ + char *buf; + const char *devtype =3D "qemu"; + uint32_t busnr =3D pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pd= ev)))); + + if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) { + buf =3D spapr_phb_vfio_get_loc_code(sphb, pdev); + if (buf) { + return buf; + } + devtype =3D "vfio"; + } + /* + * For emulated devices and VFIO-failure case, make up + * the loc-code. + */ + buf =3D g_strdup_printf("%s_%s:%04x:%02x:%02x.%x", + devtype, pdev->name, sphb->index, busnr, + PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)); + return buf; +} + /* Macros to operate with address in OF binding to PCI */ #define b_x(x, p, l) (((x) & ((1<<(l))-1)) << (p)) #define b_n(x) b_x((x), 31, 1) /* 0 if relocatable */ @@ -885,11 +939,12 @@ static void populate_resource_props(PCIDevice *d, R= esourceProps *rp) =20 static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int of= fset, int phb_index, int drc_index, - const char *drc_name) + sPAPRPHBState *sphb) { ResourceProps rp; bool is_bridge =3D false; - int pci_status; + int pci_status, err; + char *buf =3D NULL; =20 if (pci_default_read_config(dev, PCI_HEADER_TYPE, 1) =3D=3D PCI_HEADER_TYPE_BRIDGE) { @@ -950,7 +1005,18 @@ static int spapr_populate_pci_child_dt(PCIDevice *d= ev, void *fdt, int offset, * processed by OF beforehand */ _FDT(fdt_setprop_string(fdt, offset, "name", "pci")); - _FDT(fdt_setprop(fdt, offset, "ibm,loc-code", drc_name, strlen(drc_n= ame))); + buf =3D spapr_phb_get_loc_code(sphb, dev); + if (!buf) { + error_report("Failed setting the ibm,loc-code"); + return -1; + } + + err =3D fdt_setprop_string(fdt, offset, "ibm,loc-code", buf); + g_free(buf); + if (err < 0) { + return err; + } + _FDT(fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)); =20 _FDT(fdt_setprop_cell(fdt, offset, "#address-cells", @@ -988,7 +1054,7 @@ static int spapr_create_pci_child_dt(sPAPRPHBState *= phb, PCIDevice *dev, } offset =3D fdt_add_subnode(fdt, node_offset, nodename); ret =3D spapr_populate_pci_child_dt(dev, fdt, offset, phb->index, dr= c_index, - drc_name); + phb); g_assert(!ret); if (ret) { return 0; @@ -1004,14 +1070,13 @@ static void spapr_phb_add_pci_device(sPAPRDRConne= ctor *drc, sPAPRDRConnectorClass *drck =3D SPAPR_DR_CONNECTOR_GET_CLASS(drc); DeviceState *dev =3D DEVICE(pdev); int drc_index =3D drck->get_index(drc); - const char *drc_name =3D drck->get_name(drc); void *fdt =3D NULL; int fdt_start_offset =3D 0, fdt_size; =20 if (dev->hotplugged) { fdt =3D create_device_tree(&fdt_size); fdt_start_offset =3D spapr_create_pci_child_dt(phb, pdev, - drc_index, drc_name= , + drc_index, NULL, fdt, 0); if (!fdt_start_offset) { error_setg(errp, "Failed to create pci child device tree nod= e"); --=20 2.4.3