From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35325) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YqcwD-0008Rk-6f for qemu-devel@nongnu.org; Fri, 08 May 2015 03:43:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yqcw7-00023s-2t for qemu-devel@nongnu.org; Fri, 08 May 2015 03:43:13 -0400 Received: from mail-pd0-f178.google.com ([209.85.192.178]:33283) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yqcw6-00023f-Qh for qemu-devel@nongnu.org; Fri, 08 May 2015 03:43:07 -0400 Received: by pdbnk13 with SMTP id nk13so69934295pdb.0 for ; Fri, 08 May 2015 00:43:05 -0700 (PDT) Message-ID: <554C68DC.4020604@ozlabs.ru> Date: Fri, 08 May 2015 17:42:20 +1000 From: Alexey Kardashevskiy MIME-Version: 1.0 References: <1430983314-5009-1-git-send-email-nikunj@linux.vnet.ibm.com> <1430983314-5009-5-git-send-email-nikunj@linux.vnet.ibm.com> In-Reply-To: <1430983314-5009-5-git-send-email-nikunj@linux.vnet.ibm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [Qemu-devel] [PATCH v4 4/4] spapr_pci: populate ibm,loc-code List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Nikunj A Dadhania , qemu-devel@nongnu.org, david@gibson.dropbear.id.au Cc: nikunj.dadhania@gmail.com, qemu-ppc@nongnu.org, agraf@suse.de, mdroth@linux.vnet.ibm.com On 05/07/2015 05:21 PM, Nikunj A Dadhania wrote: > Each hardware instance has a platform unique location code. The OF > device tree that describes a part of a hardware entity must include > the “ibm,loc-code” 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 > --- > hw/ppc/spapr_pci.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++------- > 1 file changed, 86 insertions(+), 12 deletions(-) > > diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c > index 12f1b9c..d901007 100644 > --- a/hw/ppc/spapr_pci.c > +++ b/hw/ppc/spapr_pci.c > @@ -769,6 +769,81 @@ static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState *phb, > return drck->get_index(drc); > } > > +static bool spapr_phb_vfio_get_devspec_value(PCIDevice *pdev, char **value) Does it have to be a separate function? > +{ > + char *host; > + char path[PATH_MAX]; > + > + host = object_property_get_str(OBJECT(pdev), "host", NULL); > + if (!host) { > + return false; > + } > + > + snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/devspec", host); > + g_free(host); > + > + return g_file_get_contents(path, value, NULL, NULL); > +} > + > +static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev) > +{ > + char path[PATH_MAX], *buf = NULL; > + > + /* We have a vfio host bridge lets get the path. */ > + if (!spapr_phb_vfio_get_devspec_value(pdev, &buf)) { > + return NULL; > + } > + > + snprintf(path, sizeof(path), "/proc/device-tree%s/ibm,loc-code", buf); > + g_free(buf); > + > + g_file_get_contents(path, &buf, NULL, NULL); > + return buf; > +} > + > +static char *spapr_phb_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev) > +{ > + char *path = g_malloc(PATH_MAX); > + > + if (!path) { > + return NULL; > + } > + > + /* > + * For non-vfio devices make up the location code out > + * of the name, slot and function. > + * > + * qemu_::. > + */ > + snprintf(path, PATH_MAX, "qemu_%s:%02d:%02d.%1d", pdev->name, > + sphb->index, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)); g_strdup_printf? > + return path; > +} > + > + > +static char *spapr_ibm_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev) s/spapr_ibm_get_loc_code/spapr_phb_get_loc_code/ Strange to see "ibm" in a function name, so far we have only used "ibm" in RTAS handler names :) > +{ > + if (object_dynamic_cast(OBJECT(pdev), "vfio-pci") != NULL) { QEMU does not compare object_dynamic_cast with NULL anywhere, so: if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) { > + char *buf = spapr_phb_vfio_get_loc_code(sphb, pdev); > + > + /* > + * In case of failures reading the loc-code, make it up > + * indicating a vfio device > + */ > + if (!buf) { > + buf = g_malloc(PATH_MAX); > + if (!buf) { > + return NULL; > + } > + snprintf(buf, PATH_MAX, "vfio_%s:%02d:%02d.%1d", pdev->name, > + sphb->index, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)); g_strdup_printf? Also, "vfio_%s:%02d:%02d.%1d" looks very similar to "qemu_%s:%02d:%02d.%1d", you could extend spapr_phb_get_loc_code() to take "vfio"/"qemu" as a parameter. > + } > + return buf; > + } else { > + return spapr_phb_get_loc_code(sphb, pdev); > + } > +} > + > /* 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 */ > @@ -906,12 +981,12 @@ static void populate_resource_props(PCIDevice *d, ResourceProps *rp) > } > > static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset, > - sPAPRPHBState *sphb, > - const char *drc_name) > + sPAPRPHBState *sphb) > { > ResourceProps rp; > bool is_bridge = false; > int pci_status; > + char *buf = NULL; > uint32_t drc_index = spapr_phb_get_pci_drc_index(sphb, dev); > > if (pci_default_read_config(dev, PCI_HEADER_TYPE, 1) == > @@ -973,9 +1048,10 @@ static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset, > * processed by OF beforehand > */ > _FDT(fdt_setprop_string(fdt, offset, "name", "pci")); > - if (drc_name) { > - _FDT(fdt_setprop(fdt, offset, "ibm,loc-code", drc_name, > - strlen(drc_name))); > + buf = spapr_ibm_get_loc_code(sphb, dev); > + if (buf) { It is rather: if (!buf) { error_report("Bad thing just happened"); return -1; } OR g_assert(!buf); Your code can only return NULL if g_malloc() failed (otherwise it will be at least "(qemu"vfio)_%s:%02d:%02d.%1d") and if this happened, something went horribly bad. > + _FDT(fdt_setprop_string(fdt, offset, "ibm,loc-code", buf)); > + g_free(buf); > } > if (drc_index) { > _FDT(fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)); > @@ -1003,8 +1079,7 @@ typedef struct sPAPRFDT { > } sPAPRFDT; > > /* create OF node for pci device and required OF DT properties */ > -static int spapr_create_pci_child_dt(PCIDevice *pdev, sPAPRFDT *p, > - const char *drc_name) > +static int spapr_create_pci_child_dt(PCIDevice *pdev, sPAPRFDT *p) > { > int offset, ret; > char nodename[64]; > @@ -1017,8 +1092,8 @@ static int spapr_create_pci_child_dt(PCIDevice *pdev, sPAPRFDT *p, > sprintf(nodename, "pci@%d", slot); > } > offset = fdt_add_subnode(p->fdt, p->node_off, nodename); > - ret = spapr_populate_pci_child_dt(pdev, p->fdt, offset, p->sphb, > - drc_name); > + > + ret = spapr_populate_pci_child_dt(pdev, p->fdt, offset, p->sphb); > g_assert(!ret); > if (ret) { > return 0; > @@ -1033,7 +1108,6 @@ static void spapr_phb_add_pci_device(sPAPRDRConnector *drc, > { > sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); > DeviceState *dev = DEVICE(pdev); > - const char *drc_name = drck->get_name(drc); > int fdt_start_offset = 0, fdt_size; > sPAPRFDT s_fdt = {NULL, 0, NULL}; > > @@ -1041,7 +1115,7 @@ static void spapr_phb_add_pci_device(sPAPRDRConnector *drc, > s_fdt.fdt = create_device_tree(&fdt_size); > s_fdt.sphb = phb; > s_fdt.node_off = 0; > - fdt_start_offset = spapr_create_pci_child_dt(pdev, &s_fdt, drc_name); > + fdt_start_offset = spapr_create_pci_child_dt(pdev, &s_fdt); > if (!fdt_start_offset) { > error_setg(errp, "Failed to create pci child device tree node"); > goto out; > @@ -1519,7 +1593,7 @@ static void spapr_populate_pci_devices_dt(PCIBus *bus, PCIDevice *pdev, > int offset; > sPAPRFDT s_fdt; > > - offset = spapr_create_pci_child_dt(pdev, p, NULL); > + offset = spapr_create_pci_child_dt(pdev, p); > if (!offset) { > error_report("Failed to create pci child device tree node"); > return; > -- Alexey