Skip to contentSkip to conversation

spapr_pci: DT field fixes and PCI DT node creation in QEMU

PATCH Superseded Nikunj A Dadhania 2 participants 10 messages Jun 3, 2015 – Jun 4, 2015

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover spapr_pci: DT field fixes and PCI DT node creation in QEMU

The patch series creates PCI device tree(DT) nodes in QEMU. The new hotplug code needs the device node creation in QEMU. While during boot, nodes were created in SLOF. It makes more sense to consolidate the code to one place for better maintainability.

Based on David's spapr-next
https://github.com/dgibson/qemu/tree/spapr-next

Needs new slof.bin:
http://patchwork.ozlabs.org/patch/469303/

Changelog V5:
* Reworked enumerate bridges to reduce scopes (Alexey)
* Fixed unit address which should be hex
* Added FDT_NAME_MAX and use snprintf for composing name (Alexey)
* Added patch to drop redundant args and change prototype (Alexey)
* Added bus number in the ibm,loc-code for qemu emulated or failed vfio device.
* Now qemu_<name>:<phb-index>:<bus>:<slot>.<fn> is having hex encoding (Alexey)

Changelog V4:
* Refactored and simplified the "ibm,loc-code" patch

Changelog V3:
* Dropped duplicate macro patches
* Squashed Michael's drc_index changes to enumeration patch
* Use common create routine for boottime and hotplug case
* Proper error handling not depending on g_assert
* Encode vfio loc-code if getting loc-code from host fails

Changelog V2:
* Fix device tree for 64-bit encoding
* Fix the class code, was failing xhci
* Remove macro duplication
* Fix DT fields generation for boot time device (Michael Roth)

Changelog v1:
* Correct indent problems reported by checkpatch(David Gibson)
* Declare sPAPRFDT structure as local (David Gibson)
* Re-arrange code to avoid multiple indentation (Alexey Kardashevskiy)

Nikunj A Dadhania (6):
spapr_pci: encode missing 64-bit memory address space
spapr_pci: encode class code including Prog IF register
spapr_pci: enumerate and add PCI device tree
spapr_pci: set device node unit address as hex
spapr_pci: populate ibm,loc-code
spapr_pci: drop redundant args in spapr_populate_pci_child_dt

 hw/ppc/spapr_pci.c | 256 +++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 219 insertions(+), 37 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v6 1/6 spapr_pci: encode missing 64-bit memory address space

The properties reg/assigned-resources need to encode 64-bit memory address space as part of phys.hi dword.

00 if configuration space
01 if IO region,
10 if 32-bit MEM region
11 if 64-bit MEM region

---
 hw/ppc/spapr_pci.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
hw/ppc/spapr_pci.c +9 -1
index 4df3a33..ea1a092 100644
@@ -786,7 +786,13 @@ typedef struct ResourceProps {
  * phys.hi = 0xYYXXXXZZ, where:
  *   0xYY = npt000ss
  *          |||   |
- *          |||   +-- space code: 1 if IO region, 2 if MEM region
+ *          |||   +-- space code
+ *          |||               |
+ *          |||               +  00 if configuration space
+ *          |||               +  01 if IO region,
+ *          |||               +  10 if 32-bit MEM region
+ *          |||               +  11 if 64-bit MEM region
+ *          |||
  *          ||+------ for non-relocatable IO: 1 if aliased
  *          ||        for relocatable IO: 1 if below 64KB
  *          ||        for MEM: 1 if below 1MB
@@ -846,6 +852,8 @@ static void populate_resource_props(PCIDevice *d, ResourceProps *rp)
         reg->phys_hi = cpu_to_be32(dev_id | b_rrrrrrrr(pci_bar(d, i)));
         if (d->io_regions[i].type & PCI_BASE_ADDRESS_SPACE_IO) {
             reg->phys_hi |= cpu_to_be32(b_ss(1));
+        } else if (d->io_regions[i].type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
+            reg->phys_hi |= cpu_to_be32(b_ss(3));
         } else {
             reg->phys_hi |= cpu_to_be32(b_ss(2));
         }
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v6 2/6 spapr_pci: encode class code including Prog IF register

In reply to Nikunj A Dadhania

Current code missed the Prog IF register. All Class Code, Subclass, and Prog IF registers are needed to identify the accurate device type.

For example: USB controllers use the PROG IF for denoting: USB FullSpeed, HighSpeed or SuperSpeed.

---
 hw/ppc/spapr_pci.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/ppc/spapr_pci.c +1 -2
index ea1a092..8b02a3e 100644
@@ -899,8 +899,7 @@ static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,
     _FDT(fdt_setprop_cell(fdt, offset, "revision-id",
                           pci_default_read_config(dev, PCI_REVISION_ID, 1)));
     _FDT(fdt_setprop_cell(fdt, offset, "class-code",
-                          pci_default_read_config(dev, PCI_CLASS_DEVICE, 2)
-                            << 8));
+                          pci_default_read_config(dev, PCI_CLASS_PROG, 3)));
     if (pci_default_read_config(dev, PCI_INTERRUPT_PIN, 1)) {
         _FDT(fdt_setprop_cell(fdt, offset, "interrupts",
                  pci_default_read_config(dev, PCI_INTERRUPT_PIN, 1)));
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH v6 3/6 spapr_pci: enumerate and add PCI device tree

In reply to Nikunj A Dadhania

All the PCI enumeration and device node creation was off-loaded to SLOF. With PCI hotplug support, code needed to be added to add device node. This creates multiple copy of the code one in SLOF and other in hotplug code. To unify this, the patch adds the pci device node creation in Qemu. For backward compatibility, a flag "qemu,phb-enumerated" is added to the phb, suggesting to SLOF to not do device node creation.

[ Squashed Michael's drc_index changes ]
---
 hw/ppc/spapr_pci.c | 167 +++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 142 insertions(+), 25 deletions(-)
hw/ppc/spapr_pci.c +142 -25
index 8b02a3e..d251e5b 100644
@@ -23,6 +23,7 @@
  * THE SOFTWARE.
  */
 #include "hw/hw.h"
+#include "hw/sysbus.h"
 #include "hw/pci/pci.h"
 #include "hw/pci/msi.h"
 #include "hw/pci/msix.h"
@@ -35,6 +36,7 @@
 #include "qemu/error-report.h"
 #include "qapi/qmp/qerror.h"
 
+#include "hw/pci/pci_bridge.h"
 #include "hw/pci/pci_bus.h"
 #include "hw/ppc/spapr_drc.h"
 #include "sysemu/device_tree.h"
@@ -945,8 +947,13 @@ 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"));
-    _FDT(fdt_setprop(fdt, offset, "ibm,loc-code", drc_name, strlen(drc_name)));
-    _FDT(fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index));
+    if (drc_name) {
+        _FDT(fdt_setprop(fdt, offset, "ibm,loc-code", drc_name,
+                         strlen(drc_name)));
+    }
+    if (drc_index) {
+        _FDT(fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index));
+    }
 
     _FDT(fdt_setprop_cell(fdt, offset, "#address-cells",
                           RESOURCE_CELLS_ADDRESS));
@@ -963,30 +970,38 @@ static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,
     return 0;
 }
 
+typedef struct sPAPRFDT {
+    void *fdt;
+    int node_off;
+    sPAPRPHBState *sphb;
+} sPAPRFDT;
+
+static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState *phb,
+                                            PCIDevice *pdev);
+
 /* create OF node for pci device and required OF DT properties */
-static void *spapr_create_pci_child_dt(sPAPRPHBState *phb, PCIDevice *dev,
-                                       int drc_index, const char *drc_name,
-                                       int *dt_offset)
+static int spapr_create_pci_child_dt(PCIDevice *dev, sPAPRFDT *p,
+                                     const char *drc_name)
 {
-    void *fdt;
-    int offset, ret, fdt_size;
+    int offset, ret;
     int slot = PCI_SLOT(dev->devfn);
     int func = PCI_FUNC(dev->devfn);
     char nodename[512];
+    uint32_t drc_index = spapr_phb_get_pci_drc_index(p->sphb, dev);
 
-    fdt = create_device_tree(&fdt_size);
     if (func != 0) {
         sprintf(nodename, "pci@%d,%d", slot, func);
     } else {
         sprintf(nodename, "pci@%d", slot);
     }
-    offset = fdt_add_subnode(fdt, 0, nodename);
-    ret = spapr_populate_pci_child_dt(dev, fdt, offset, phb->index, drc_index,
-                                      drc_name);
+    offset = fdt_add_subnode(p->fdt, p->node_off, nodename);
+    ret = spapr_populate_pci_child_dt(dev, p->fdt, offset, p->sphb->index,
+                                      drc_index, drc_name);
     g_assert(!ret);
-
-    *dt_offset = offset;
-    return fdt;
+    if (ret) {
+        return 0;
+    }
+    return offset;
 }
 
 static void spapr_phb_add_pci_device(sPAPRDRConnector *drc,
@@ -996,24 +1011,26 @@ static void spapr_phb_add_pci_device(sPAPRDRConnector *drc,
 {
     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
     DeviceState *dev = DEVICE(pdev);
-    int drc_index = drck->get_index(drc);
     const char *drc_name = drck->get_name(drc);
-    void *fdt = NULL;
-    int fdt_start_offset = 0;
+    int fdt_start_offset = 0, fdt_size;
+    sPAPRFDT s_fdt = {NULL, 0, NULL};
 
-    /* boot-time devices get their device tree node created by SLOF, but for
-     * hotplugged devices we need QEMU to generate it so the guest can fetch
-     * it via RTAS
-     */
     if (dev->hotplugged) {
-        fdt = spapr_create_pci_child_dt(phb, pdev, drc_index, drc_name,
-                                        &fdt_start_offset);
+        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);
+        if (!fdt_start_offset) {
+            error_setg(errp, "Failed to create pci child device tree node");
+            goto out;
+        }
     }
 
     drck->attach(drc, DEVICE(pdev),
-                 fdt, fdt_start_offset, !dev->hotplugged, errp);
+                 s_fdt.fdt, fdt_start_offset, !dev->hotplugged, errp);
+out:
     if (*errp) {
-        g_free(fdt);
+        g_free(s_fdt.fdt);
     }
 }
 
@@ -1053,6 +1070,20 @@ static sPAPRDRConnector *spapr_phb_get_pci_drc(sPAPRPHBState *phb,
                                     pdev->devfn);
 }
 
+static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState *phb,
+                                            PCIDevice *pdev)
+{
+    sPAPRDRConnector *drc = spapr_phb_get_pci_drc(phb, pdev);
+    sPAPRDRConnectorClass *drck;
+
+    if (!drc) {
+        return 0;
+    }
+
+    drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
+    return drck->get_index(drc);
+}
+
 static void spapr_phb_hot_plug_child(HotplugHandler *plug_handler,
                                      DeviceState *plugged_dev, Error **errp)
 {
@@ -1482,6 +1513,78 @@ PCIHostState *spapr_create_phb(sPAPREnvironment *spapr, int index)
     return PCI_HOST_BRIDGE(dev);
 }
 
+static void spapr_populate_pci_devices_dt(PCIBus *bus, PCIDevice *pdev,
+                                          void *opaque)
+{
+    PCIBus *sec_bus;
+    sPAPRFDT *p = opaque;
+    int offset;
+    sPAPRFDT s_fdt;
+
+    offset = spapr_create_pci_child_dt(pdev, p, NULL);
+    if (!offset) {
+        error_report("Failed to create pci child device tree node");
+        return;
+    }
+
+    if ((pci_default_read_config(pdev, PCI_HEADER_TYPE, 1) !=
+         PCI_HEADER_TYPE_BRIDGE)) {
+        return;
+    }
+
+    sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
+    if (!sec_bus) {
+        return;
+    }
+
+    s_fdt.fdt = p->fdt;
+    s_fdt.node_off = offset;
+    s_fdt.sphb = p->sphb;
+    pci_for_each_device(sec_bus, pci_bus_num(sec_bus),
+                        spapr_populate_pci_devices_dt,
+                        &s_fdt);
+}
+
+static void spapr_phb_pci_enumerate_bridge(PCIBus *bus, PCIDevice *pdev,
+                                           void *opaque)
+{
+    unsigned int *bus_no = opaque;
+    unsigned int primary = *bus_no;
+    unsigned int subordinate = 0xff;
+    PCIBus *sec_bus = NULL;
+
+    if ((pci_default_read_config(pdev, PCI_HEADER_TYPE, 1) !=
+         PCI_HEADER_TYPE_BRIDGE)) {
+        return;
+    }
+
+    (*bus_no)++;
+    pci_default_write_config(pdev, PCI_PRIMARY_BUS, primary, 1);
+    pci_default_write_config(pdev, PCI_SECONDARY_BUS, *bus_no, 1);
+    pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, *bus_no, 1);
+
+    sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
+    if (!sec_bus) {
+        return;
+    }
+
+    pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, subordinate, 1);
+    pci_for_each_device(sec_bus, pci_bus_num(sec_bus),
+                        spapr_phb_pci_enumerate_bridge, bus_no);
+    pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, *bus_no, 1);
+}
+
+static void spapr_phb_pci_enumerate(sPAPRPHBState *phb)
+{
+    PCIBus *bus = PCI_HOST_BRIDGE(phb)->bus;
+    unsigned int bus_no = 0;
+
+    pci_for_each_device(bus, pci_bus_num(bus),
+                        spapr_phb_pci_enumerate_bridge,
+                        &bus_no);
+
+}
+
 int spapr_populate_pci_dt(sPAPRPHBState *phb,
                           uint32_t xics_phandle,
                           void *fdt)
@@ -1521,6 +1624,8 @@ int spapr_populate_pci_dt(sPAPRPHBState *phb,
         cpu_to_be32(b_ddddd(-1)|b_fff(0)), 0x0, 0x0, cpu_to_be32(-1)};
     uint32_t interrupt_map[PCI_SLOT_MAX * PCI_NUM_PINS][7];
     sPAPRTCETable *tcet;
+    PCIBus *bus = PCI_HOST_BRIDGE(phb)->bus;
+    sPAPRFDT s_fdt;
 
     /* Start populating the FDT */
     sprintf(nodename, "pci@%" PRIx64, phb->buid);
@@ -1570,6 +1675,18 @@ int spapr_populate_pci_dt(sPAPRPHBState *phb,
                  tcet->liobn, tcet->bus_offset,
                  tcet->nb_table << tcet->page_shift);
 
+    /* Walk the bridges and program the bus numbers*/
+    spapr_phb_pci_enumerate(phb);
+    _FDT(fdt_setprop_cell(fdt, bus_off, "qemu,phb-enumerated", 0x1));
+
+    /* Populate tree nodes with PCI devices attached */
+    s_fdt.fdt = fdt;
+    s_fdt.node_off = bus_off;
+    s_fdt.sphb = phb;
+    pci_for_each_device(bus, pci_bus_num(bus),
+                        spapr_populate_pci_devices_dt,
+                        &s_fdt);
+
     ret = spapr_drc_populate_dt(fdt, bus_off, OBJECT(phb),
                                 SPAPR_DR_CONNECTOR_TYPE_PCI);
     if (ret) {
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH v6 4/6 spapr_pci: set device node unit address as hex

In reply to Nikunj A Dadhania

Device node names should encode the unit address as hex, while the code was encodind it as integers.

Also, use FDT_NAME_MAX macro for allocating and composing the name.

---
 hw/ppc/spapr_pci.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
hw/ppc/spapr_pci.c +7 -5
index d251e5b..4226468 100644
@@ -52,6 +52,8 @@
 #define RTAS_TYPE_MSI           1
 #define RTAS_TYPE_MSIX          2
 
+#define FDT_NAME_MAX          128
+
 #define _FDT(exp) \
     do { \
         int ret = (exp);                                           \
@@ -986,13 +988,13 @@ static int spapr_create_pci_child_dt(PCIDevice *dev, sPAPRFDT *p,
     int offset, ret;
     int slot = PCI_SLOT(dev->devfn);
     int func = PCI_FUNC(dev->devfn);
-    char nodename[512];
+    char nodename[FDT_NAME_MAX];
     uint32_t drc_index = spapr_phb_get_pci_drc_index(p->sphb, dev);
 
     if (func != 0) {
-        sprintf(nodename, "pci@%d,%d", slot, func);
+        snprintf(nodename, FDT_NAME_MAX, "pci@%x,%x", slot, func);
     } else {
-        sprintf(nodename, "pci@%d", slot);
+        snprintf(nodename, FDT_NAME_MAX, "pci@%x", slot);
     }
     offset = fdt_add_subnode(p->fdt, p->node_off, nodename);
     ret = spapr_populate_pci_child_dt(dev, p->fdt, offset, p->sphb->index,
@@ -1590,7 +1592,7 @@ int spapr_populate_pci_dt(sPAPRPHBState *phb,
                           void *fdt)
 {
     int bus_off, i, j, ret;
-    char nodename[256];
+    char nodename[FDT_NAME_MAX];
     uint32_t bus_range[] = { cpu_to_be32(0), cpu_to_be32(0xff) };
     const uint64_t mmiosize = memory_region_size(&phb->memwindow);
     const uint64_t w32max = (1ULL << 32) - SPAPR_PCI_MEM_WIN_BUS_OFFSET;
@@ -1628,7 +1630,7 @@ int spapr_populate_pci_dt(sPAPRPHBState *phb,
     sPAPRFDT s_fdt;
 
     /* Start populating the FDT */
-    sprintf(nodename, "pci@%" PRIx64, phb->buid);
+    snprintf(nodename, FDT_NAME_MAX, "pci@%" PRIx64, phb->buid);
     bus_off = fdt_add_subnode(fdt, 0, nodename);
     if (bus_off < 0) {
         return bus_off;
Signature · 1 line
1.8.3.1
Reviewed-by on v6 4/6
#5 lore raw

Re: [Qemu-devel] [PATCH v6 4/6] spapr_pci: set device node unit address as hex

Quoted 9 lines · On Wed, 3 Jun 2015 16:55:55 +0530 Nikunj A Dadhania…

Device node names should encode the unit address as hex, while the code was encodind it as integers.

Also, use FDT_NAME_MAX macro for allocating and composing the name.

---
 hw/ppc/spapr_pci.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
#6 lore raw

PATCH v6 5/6 spapr_pci: populate ibm,loc-code

In reply to 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 “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_<name>:<phb-index>:<bus>:<slot>.<fn>

2) Emulated devices encode as following:
qemu_<name>:<phb-index>:<bus>:<slot>.<fn>

---
 hw/ppc/spapr_pci.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 68 insertions(+), 10 deletions(-)
hw/ppc/spapr_pci.c +68 -10
index 4226468..986bb21 100644
@@ -746,6 +746,60 @@ static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
     return &phb->iommu_as;
 }
 
+static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState *sphb,  PCIDevice *pdev)
+{
+    char *path = NULL, *buf = NULL, *host = NULL;
+
+    /* Get the PCI VFIO host id */
+    host = 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 = 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 = 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 = "qemu";
+    uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))));
+
+    if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
+        buf = spapr_phb_vfio_get_loc_code(sphb, pdev);
+        if (buf) {
+            return buf;
+        }
+        devtype = "vfio";
+    }
+    /*
+     * For emulated devices and VFIO-failure case, make up
+     * the loc-code.
+     */
+    buf = 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 */
@@ -884,11 +938,12 @@ static void populate_resource_props(PCIDevice *d, ResourceProps *rp)
 
 static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,
                                        int phb_index, int drc_index,
-                                       const char *drc_name)
+                                       sPAPRPHBState *sphb)
 {
     ResourceProps rp;
     bool is_bridge = false;
     int pci_status;
+    char *buf = NULL;
 
     if (pci_default_read_config(dev, PCI_HEADER_TYPE, 1) ==
         PCI_HEADER_TYPE_BRIDGE) {
@@ -949,10 +1004,15 @@ 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_phb_get_loc_code(sphb, dev);
+    if (!buf) {
+        error_report("Failed setting the ibm,loc-code");
+        return -1;
     }
+
+    _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));
     }
@@ -982,8 +1042,7 @@ static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState *phb,
                                             PCIDevice *pdev);
 
 /* create OF node for pci device and required OF DT properties */
-static int spapr_create_pci_child_dt(PCIDevice *dev, sPAPRFDT *p,
-                                     const char *drc_name)
+static int spapr_create_pci_child_dt(PCIDevice *dev, sPAPRFDT *p)
 {
     int offset, ret;
     int slot = PCI_SLOT(dev->devfn);
@@ -998,7 +1057,7 @@ static int spapr_create_pci_child_dt(PCIDevice *dev, sPAPRFDT *p,
     }
     offset = fdt_add_subnode(p->fdt, p->node_off, nodename);
     ret = spapr_populate_pci_child_dt(dev, p->fdt, offset, p->sphb->index,
-                                      drc_index, drc_name);
+                                      drc_index, p->sphb);
     g_assert(!ret);
     if (ret) {
         return 0;
@@ -1013,7 +1072,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};
 
@@ -1021,7 +1079,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;
@@ -1523,7 +1581,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;
Signature · 1 line
1.8.3.1
#7 lore raw

Re: [Qemu-devel] [PATCH v6 5/6] spapr_pci: populate ibm,loc-code

Quoted 42 lines · On Wed, 3 Jun 2015 16:55:56 +0530 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 “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_<name>:<phb-index>:<bus>:<slot>.<fn>

2) Emulated devices encode as following:

   qemu_<name>:<phb-index>:<bus>:<slot>.<fn>
---
 hw/ppc/spapr_pci.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 68 insertions(+), 10 deletions(-)
hw/ppc/spapr_pci.c +15 -0
index 4226468..986bb21 100644
@@ -746,6 +746,60 @@ static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
     return &phb->iommu_as;
 }
 
+static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState *sphb,  PCIDevice *pdev)
+{
+    char *path = NULL, *buf = NULL, *host = NULL;
+
+    /* Get the PCI VFIO host id */
+    host = 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 = 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;

hw/ppc/spapr_pci.c static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn) · line 773

Quoted 10 lines
hw/ppc/spapr_pci.c +10 -0
+    }
+    g_free(path);
+
+    /* Construct and read from host device tree the loc-code */
+    path = 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;

I'd maybe change the above 4 lines into:

    if (path && g_file_get_contents(path, &buf, NULL, NULL)) {
        return buf;
    }

so that you can get rid of one goto here.

Quoted 32 lines
hw/ppc/spapr_pci.c +28 -0
+err_out:
+    g_free(path);
+    return NULL;
+}
+
+static char *spapr_phb_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev)
+{
+    char *buf;
+    const char *devtype = "qemu";
+    uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))));
+
+    if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
+        buf = spapr_phb_vfio_get_loc_code(sphb, pdev);
+        if (buf) {
+            return buf;
+        }
+        devtype = "vfio";
+    }
+    /*
+     * For emulated devices and VFIO-failure case, make up
+     * the loc-code.
+     */
+    buf = 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 */

hw/ppc/spapr_pci.c static void populate_resource_props(PCIDevice *d, ResourceProps *rp) · line 946

Quoted 10 lines
hw/ppc/spapr_pci.c +2 -1
@@ -884,11 +938,12 @@ static void populate_resource_props(PCIDevice *d, ResourceProps *rp)
 
 static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,
                                        int phb_index, int drc_index,
-                                       const char *drc_name)
+                                       sPAPRPHBState *sphb)
 {
     ResourceProps rp;
     bool is_bridge = false;
     int pci_status;
+    char *buf = NULL;

Is the "= NULL" required here? If not, please remove, newer version of gcc tend to complain otherwise.

Quoted 6 lines
hw/ppc/spapr_pci.c +0 -0
     if (pci_default_read_config(dev, PCI_HEADER_TYPE, 1) ==
         PCI_HEADER_TYPE_BRIDGE) {
@@ -949,10 +1004,15 @@ 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"));

hw/ppc/spapr_pci.c static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset, · line 1013

Quoted 10 lines
hw/ppc/spapr_pci.c +6 -3
-    if (drc_name) {
-        _FDT(fdt_setprop(fdt, offset, "ibm,loc-code", drc_name,
-                         strlen(drc_name)));
+    buf = spapr_phb_get_loc_code(sphb, dev);
+    if (!buf) {
+        error_report("Failed setting the ibm,loc-code");
+        return -1;
     }
+
+    _FDT(fdt_setprop_string(fdt, offset, "ibm,loc-code", buf));

I wonder whether this will cause some Coverity warnings later ... the _FDT macro can return immediately (ugh, return in a macro ... IMHO a bad idea...). buf is not freed in that case, and that might trigger a warning...

hw/ppc/spapr_pci.c static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset, · line 1018

Quoted 5 lines
hw/ppc/spapr_pci.c +2 -0
+    g_free(buf);
+
     if (drc_index) {
         _FDT(fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index));
     }

[...]

Thomas

#8 lore raw

Re: [Qemu-devel] [PATCH v6 5/6] spapr_pci: populate ibm,loc-code

Quoted 58 lines · Thomas Huth
Quoted 42 lines · On Wed, 3 Jun 2015 16:55:56 +0530 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 “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_<name>:<phb-index>:<bus>:<slot>.<fn>

2) Emulated devices encode as following:

   qemu_<name>:<phb-index>:<bus>:<slot>.<fn>
---
 hw/ppc/spapr_pci.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 68 insertions(+), 10 deletions(-)
hw/ppc/spapr_pci.c +15 -0
index 4226468..986bb21 100644
@@ -746,6 +746,60 @@ static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
     return &phb->iommu_as;
 }
 
+static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState *sphb,  PCIDevice *pdev)
+{
+    char *path = NULL, *buf = NULL, *host = NULL;
+
+    /* Get the PCI VFIO host id */
+    host = 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 = 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;

hw/ppc/spapr_pci.c static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn) · line 773

Quoted 10 lines
hw/ppc/spapr_pci.c +10 -0
+    }
+    g_free(path);
+
+    /* Construct and read from host device tree the loc-code */
+    path = 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;

I'd maybe change the above 4 lines into:

Quoted 5 lines
    if (path && g_file_get_contents(path, &buf, NULL, NULL)) {
        return buf;
    }

so that you can get rid of one goto here.

Wouldnt make much of a difference though !

Quoted 56 lines
Quoted 32 lines
hw/ppc/spapr_pci.c +28 -0
+err_out:
+    g_free(path);
+    return NULL;
+}
+
+static char *spapr_phb_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev)
+{
+    char *buf;
+    const char *devtype = "qemu";
+    uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))));
+
+    if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
+        buf = spapr_phb_vfio_get_loc_code(sphb, pdev);
+        if (buf) {
+            return buf;
+        }
+        devtype = "vfio";
+    }
+    /*
+     * For emulated devices and VFIO-failure case, make up
+     * the loc-code.
+     */
+    buf = 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 */

hw/ppc/spapr_pci.c static void populate_resource_props(PCIDevice *d, ResourceProps *rp) · line 946

Quoted 10 lines
hw/ppc/spapr_pci.c +2 -1
@@ -884,11 +938,12 @@ static void populate_resource_props(PCIDevice *d, ResourceProps *rp)
 
 static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,
                                        int phb_index, int drc_index,
-                                       const char *drc_name)
+                                       sPAPRPHBState *sphb)
 {
     ResourceProps rp;
     bool is_bridge = false;
     int pci_status;
+    char *buf = NULL;

Is the "= NULL" required here? If not, please remove, newer version of gcc tend to complain otherwise.

Quoted 10 lines
hw/ppc/spapr_pci.c +1 -3
     if (pci_default_read_config(dev, PCI_HEADER_TYPE, 1) ==
         PCI_HEADER_TYPE_BRIDGE) {
@@ -949,10 +1004,15 @@ 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_phb_get_loc_code(sphb, dev);
Quoted 8 lines

hw/ppc/spapr_pci.c static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset, · line 1013

Quoted 6 lines
hw/ppc/spapr_pci.c +5 -0
+    if (!buf) {
+        error_report("Failed setting the ibm,loc-code");
+        return -1;
     }
+
+    _FDT(fdt_setprop_string(fdt, offset, "ibm,loc-code", buf));

I wonder whether this will cause some Coverity warnings later ...

Not sure about that.

Quoted 3 lines

the _FDT macro can return immediately (ugh, return in a macro ... IMHO a bad idea...). buf is not freed in that case, and that might trigger a warning...

You are right buf may not get freed in case of FDT error. In this case let me open code this macro here with proper error handling:

    err = fdt_setprop_string(fdt, offset, "ibm,loc-code", buf);
    g_free(buf);
    if (err < 0) {
        return err;
    }

Regards
Nikunj

#9 lore raw

PATCH v6 6/6 spapr_pci: drop redundant args in spapr_populate_pci_child_dt

In reply to Nikunj A Dadhania

* phb_index is not being used and if required can be obtained from sphb
* use helper to get drc_index in this function

---
 hw/ppc/spapr_pci.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
hw/ppc/spapr_pci.c +5 -7
index 986bb21..d2547a1 100644
@@ -936,14 +936,17 @@ static void populate_resource_props(PCIDevice *d, ResourceProps *rp)
     rp->assigned_len = assigned_idx * sizeof(ResourceFields);
 }
 
+static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState *phb,
+                                            PCIDevice *pdev);
+
 static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,
-                                       int phb_index, int drc_index,
                                        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) ==
         PCI_HEADER_TYPE_BRIDGE) {
@@ -1038,9 +1041,6 @@ typedef struct sPAPRFDT {
     sPAPRPHBState *sphb;
 } sPAPRFDT;
 
-static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState *phb,
-                                            PCIDevice *pdev);
-
 /* create OF node for pci device and required OF DT properties */
 static int spapr_create_pci_child_dt(PCIDevice *dev, sPAPRFDT *p)
 {
@@ -1048,7 +1048,6 @@ static int spapr_create_pci_child_dt(PCIDevice *dev, sPAPRFDT *p)
     int slot = PCI_SLOT(dev->devfn);
     int func = PCI_FUNC(dev->devfn);
     char nodename[FDT_NAME_MAX];
-    uint32_t drc_index = spapr_phb_get_pci_drc_index(p->sphb, dev);
 
     if (func != 0) {
         snprintf(nodename, FDT_NAME_MAX, "pci@%x,%x", slot, func);
@@ -1056,8 +1055,7 @@ static int spapr_create_pci_child_dt(PCIDevice *dev, sPAPRFDT *p)
         snprintf(nodename, FDT_NAME_MAX, "pci@%x", slot);
     }
     offset = fdt_add_subnode(p->fdt, p->node_off, nodename);
-    ret = spapr_populate_pci_child_dt(dev, p->fdt, offset, p->sphb->index,
-                                      drc_index, p->sphb);
+    ret = spapr_populate_pci_child_dt(dev, p->fdt, offset, p->sphb);
     g_assert(!ret);
     if (ret) {
         return 0;
Signature · 1 line
1.8.3.1