Skip to contentSkip to conversation

VFIO platform: Use device properties API

RFC PATCH New Baptiste Reynal 1 participant 5 messages Feb 11, 2015

lore
Share & tools
lore
#0 lore raw

v2 cover VFIO platform: Use device properties API

This RFC shows the implementation on QEMU side of the device properties interface presented in kernel patch series:
[RFC PATCH v3 0/3] vfio: platform: return device properties for a platform device from branch vfio-device-properties-v3 on the repository:
https://github.com/virtualopensystems/linux-kvm-arm.git

When a VFIO device is bound to the VM, properties are queried to the host to fill the device tree.

One issue here is whether a property may change between the host and the guest. Currently, interrupt numbers and registers change, other information is kept (including interrupt type and flags).

Regarding the clock, any primecell device requiring a clock is attached to apb-pclk.

DMA pl330 is used as an example. For this reason the last patch (3/3, arm,pl330 vfio device property) relies on the following patch series: [RFC PATCH v3 0/3] VFIO support for AMBA devices.

Changes since v1:
- various fixes
- code clarification (some comments added)

Baptiste Reynal (4):
linux-headers update
hw/vfio/common.c : vfio_get_dev_property hw/arm/sysbus-fdt: vfio device property for interrupts hw/arm/sysbus-fdt: arm,pl330 vfio device property

 hw/arm/sysbus-fdt.c           | 134 ++++++++++++++++++++++++++++++++++++++++--
 hw/vfio/common.c              |  32 ++++++++++
 include/hw/vfio/vfio-common.h |   2 +
 linux-headers/linux/vfio.h    |  25 ++++++++
 4 files changed, 187 insertions(+), 6 deletions(-)
Signature · 1 line
2.3.0
#1 lore raw

RFC PATCH v2 1/4 linux-headers update

Add VFIO device property constants

---
 linux-headers/linux/vfio.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
linux-headers/linux/vfio.h +25 -0
index 58f549c..1d16c12 100644
@@ -167,6 +167,31 @@ struct vfio_device_info {
 #define VFIO_DEVICE_GET_INFO		_IO(VFIO_TYPE, VFIO_BASE + 7)
 
 /**
+ * VFIO_DEVICE_GET_DEV_PROPERTY - _IOR(VFIO_TYPE, VFIO_BASE + 16,
+ *						struct vfio_devtree_info)
+ *
+ * Retrieve a device property, e.g. from a device tree if available.
+ * Caller will initialize data[] with a single string with the requested
+ * devicetree property name, and type depending on whether a array of strings
+ * or an array of u32 values is expected. On success, data[] will be extended
+ * with the requested information, either as an array of u32, or with a list
+ * of strings separated by the NULL terminating character.
+ * Return: 0 on success, -errno on failure.
+ */
+struct vfio_dev_property {
+	__u32	argsz;
+	__u32	type;
+#define VFIO_DEV_PROPERTY_TYPE_STRINGS	0
+#define VFIO_DEV_PROPERTY_TYPE_U8	1
+#define VFIO_DEV_PROPERTY_TYPE_U16	2
+#define VFIO_DEV_PROPERTY_TYPE_U32	3
+#define VFIO_DEV_PROPERTY_TYPE_U64	4
+	__u32	length;
+	__u8	data[];
+};
+#define VFIO_DEVICE_GET_DEV_PROPERTY	_IO(VFIO_TYPE, VFIO_BASE + 17)
+
+/**
  * VFIO_DEVICE_GET_REGION_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 8,
  *				       struct vfio_region_info)
  *
Signature · 1 line
2.3.0
#2 lore raw

RFC PATCH v2 2/4 hw/vfio/common.c : vfio_get_dev_property

In reply to Baptiste Reynal

Add a function to handle ioctl VFIO_DEVICE_GET_DEV_PROPERTY to retrieve properties from a VFIO device.

---
v1 -> v2:
fix property->length initialization

---
 hw/vfio/common.c              | 32 ++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio-common.h |  2 ++
 2 files changed, 34 insertions(+)
hw/vfio/common.c +32 -0
index cf483ff..728afee 100644
@@ -957,3 +957,35 @@ int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
 
     return vfio_container_do_ioctl(as, groupid, req, param);
 }
+
+struct vfio_dev_property *vfio_get_dev_property(int device, const char *name,
+        unsigned int type)
+{
+    unsigned int length, argsz;
+    struct vfio_dev_property *property = NULL;
+    int ret;
+
+    length = strlen(name) + 1;
+    argsz = sizeof(struct vfio_dev_property) + length;
+    property = malloc(argsz);
+    property->argsz = argsz;
+    property->type = type;
+    property->length = length;
+
+    do {
+        length = property->length;
+        argsz = sizeof(struct vfio_dev_property) + length;
+        property = realloc(property, argsz);
+        property->argsz = argsz;
+        strcpy((char *) property->data, name);
+
+        ret = ioctl(device, VFIO_DEVICE_GET_DEV_PROPERTY, property);
+    } while (length < property->length);
+
+    if (ret) {
+        g_free(property);
+        property = NULL;
+    }
+
+    return property;
+}
include/hw/vfio/vfio-common.h +2 -0
index 8ab880a..66e9c5a 100644
@@ -144,6 +144,8 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace *as);
 void vfio_put_group(VFIOGroup *group);
 int vfio_get_device(VFIOGroup *group, const char *name,
                     VFIODevice *vbasedev);
+struct vfio_dev_property *vfio_get_dev_property(int device, const char *name,
+        unsigned int type);
 
 extern const MemoryRegionOps vfio_region_ops;
 extern const MemoryListener vfio_memory_listener;
Signature · 1 line
2.3.0
#3 lore raw

RFC PATCH v2 3/4 hw/arm/sysbus-fdt: vfio device property for interrupts

In reply to Baptiste Reynal

Use the VFIO device property API to retrieve interrupt information (type and flags) during device node creation.

---
v1 -> v2:
check irq_prop->data size
add some comments

---
 hw/arm/sysbus-fdt.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
hw/arm/sysbus-fdt.c +17 -6
index 0c9d59a..c3b3d97 100644
@@ -31,6 +31,7 @@
 #include "hw/vfio/vfio-pl330.h"
 
 #include <libfdt.h>
+#include <linux/vfio.h>
 
 /*
  * internal struct that contains the information to create dynamic
@@ -64,9 +65,10 @@ typedef struct NodeCreationPair {
  * set_interrupts_fdt_node
  *
  * Helper function, generate interrupts property for a given node
+ * based on the host device tree
  */
 static int set_interrupts_fdt_node(char *nodename, SysBusDevice *sbdev,
-        void *opaque, int type, int flags)
+        void *opaque)
 {
     PlatformBusFDTData *data = opaque;
     PlatformBusDevice *pbus = data->pbus;
@@ -76,15 +78,23 @@ static int set_interrupts_fdt_node(char *nodename, SysBusDevice *sbdev,
     int i, ret;
     VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
     VFIODevice *vbasedev = &vdev->vbasedev;
+    struct vfio_dev_property *irq_prop;
+
+    irq_prop = vfio_get_dev_property(vbasedev->fd, "interrupts",
+            VFIO_DEV_PROPERTY_TYPE_U32);
+
+    if (irq_prop == NULL || irq_prop->length < vbasedev->num_irqs * 3) {
+        return -1;
+    }
 
     irq_attr = g_new(uint32_t, vbasedev->num_irqs*3);
 
     for (i = 0; i < vbasedev->num_irqs; i++) {
         irq_number = platform_bus_get_irqn(pbus, sbdev , i)
-            + data->irq_start;
-        irq_attr[3*i] = cpu_to_be32(type);
+                         + data->irq_start;
+        irq_attr[3*i] = cpu_to_be32(((__u32 *) irq_prop->data)[3*i]);
         irq_attr[3*i+1] = cpu_to_be32(irq_number);
-        irq_attr[3*i+2] = cpu_to_be32(flags);
+        irq_attr[3*i+2] = cpu_to_be32(((__u32 *) irq_prop->data)[3*i+2]);
     }
 
     ret = qemu_fdt_setprop(fdt, nodename, "interrupts",
@@ -94,6 +104,7 @@ static int set_interrupts_fdt_node(char *nodename, SysBusDevice *sbdev,
         error_report("could not set interrupts property of node %s", nodename);
     }
     g_free(irq_attr);
+    g_free(irq_prop);
 
     return ret;
 }
@@ -173,7 +184,7 @@ static int add_calxeda_midway_xgmac_fdt_node(SysBusDevice *sbdev, void *opaque)
         goto fail;
     }
 
-    ret = set_interrupts_fdt_node(nodename, sbdev, opaque, 0, 0x4);
+    ret = set_interrupts_fdt_node(nodename, sbdev, opaque);
 
     if (ret < 0) {
         goto fail;
@@ -253,7 +264,7 @@ static int add_arm_pl330_fdt_node(SysBusDevice *sbdev, void *opaque)
         goto fail;
     }
 
-    ret = set_interrupts_fdt_node(nodename, sbdev, opaque, 0, 0x4);
+    ret = set_interrupts_fdt_node(nodename, sbdev, opaque);
 
     if (ret < 0) {
         error_report("could not set interrupts property of node %s",
Signature · 1 line
2.3.0
#4 lore raw

RFC PATCH v2 4/4 hw/arm/sysbus-fdt: arm, pl330 vfio device property

In reply to Baptiste Reynal

Adapt arm,pl330 function to use the vfio device property API.

Clock apb-pclk is the default if a clock is needed by the device.

Three optional parameters are taken into account :
- #dma-cells
- #dma-channels
- #dma-requests

---
v1 -> v2:
add some comments
free pointers (property)

---
 hw/arm/sysbus-fdt.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 111 insertions(+)
hw/arm/sysbus-fdt.c +111 -0
index c3b3d97..d507daa 100644
@@ -148,6 +148,60 @@ static int set_regions_fdt_node(char *nodename, SysBusDevice *sbdev,
 }
 
 /**
+ * set_arm_primecell_node
+ *
+ * Set node properties for an arm primecell device
+ * see Documentation/devicetree/bindings/arm/primecell.txt
+ */
+static int set_arm_primecell_node(char *nodename, SysBusDevice *sbdev, void
+        *opaque)
+{
+    PlatformBusFDTData *data = opaque;
+    void *fdt = data->fdt;
+    int ret;
+    VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
+    VFIODevice *vbasedev = &vdev->vbasedev;
+    struct vfio_dev_property *property;
+
+    /* Optional properties */
+    property = vfio_get_dev_property(vbasedev->fd, "interrupts",
+            VFIO_DEV_PROPERTY_TYPE_U32);
+    if (property != NULL) {
+        ret = set_interrupts_fdt_node(nodename, sbdev, opaque);
+        if (ret < 0) {
+            error_report("could not set interrupts property of node %s",
+                    nodename);
+            goto fail;
+        }
+        g_free(property);
+    }
+
+    property = vfio_get_dev_property(vbasedev->fd, "clock-names",
+            VFIO_DEV_PROPERTY_TYPE_STRINGS);
+    if (property != NULL) {
+        /* If a clock is attached, we rely on apb-pclk */
+        /* Check clock existence */
+        ret = fdt_path_offset(fdt, "/apb-pclk");
+
+        if (ret < 0) {
+            error_report("could not set clocks property of node %s", nodename);
+            goto fail;
+        } else {
+            qemu_fdt_setprop_cells(fdt, nodename, "clocks",
+                    qemu_fdt_getprop_cell(fdt, "/apb-pclk", "phandle"));
+            char clock_names[] = "apb_pclk";
+            qemu_fdt_setprop(fdt, nodename, "clock-names", clock_names,
+                    sizeof(clock_names));
+        }
+        g_free(property);
+    }
+
+    return 0;
+
+fail:
+    return -1;
+}
+/**
  * add_calxeda_midway_xgmac_fdt_node
  *
  * Generates a very simple node with following properties:
@@ -205,6 +259,8 @@ fail:
  *
  * Generates a very simple node with following properties:
  * compatible string, regs, interrupts, clocks, clock-names
+ *
+ * See Documentation/devicetree/bindings/dma/arm-pl330.txt
  */
 static int add_arm_pl330_fdt_node(SysBusDevice *sbdev, void *opaque)
 {
@@ -218,6 +274,8 @@ static int add_arm_pl330_fdt_node(SysBusDevice *sbdev, void *opaque)
     VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
     VFIODevice *vbasedev = &vdev->vbasedev;
     Object *obj = OBJECT(sbdev);
+    struct vfio_dev_property *property;
+    int propint;
 
     mmio_base = object_property_get_int(obj, "mmio[0]", NULL);
 
@@ -227,6 +285,13 @@ static int add_arm_pl330_fdt_node(SysBusDevice *sbdev, void *opaque)
 
     qemu_fdt_add_subnode(fdt, nodename);
 
+    ret = set_arm_primecell_node(nodename, sbdev, opaque);
+    if (ret < 0) {
+        error_report("could not set arm,primecell properties of node %s",
+                nodename);
+        goto fail;
+    }
+
     /*
      * Process compatible string to deal with multiple strings
      * (; is replaced by \0)
@@ -272,6 +337,52 @@ static int add_arm_pl330_fdt_node(SysBusDevice *sbdev, void *opaque)
         goto fail;
     }
 
+    /* Optional properties */
+    property = vfio_get_dev_property(vbasedev->fd, "#dma-cells",
+            VFIO_DEV_PROPERTY_TYPE_U32);
+    if (property != NULL) {
+        propint = cpu_to_be32(((__u32 *) property->data)[0]);
+        ret = qemu_fdt_setprop(fdt, nodename, "#dma-cells", &propint,
+                sizeof(int));
+        if (ret < 0) {
+            error_report("could not set #dma-cells property of node %s",
+                    nodename);
+            g_free(property);
+            goto fail;
+        }
+        g_free(property);
+    }
+
+    property = vfio_get_dev_property(vbasedev->fd, "#dma-channels",
+            VFIO_DEV_PROPERTY_TYPE_U32);
+    if (property != NULL) {
+        propint = cpu_to_be32(((__u32 *) property->data)[0]);
+        ret = qemu_fdt_setprop(fdt, nodename, "#dma-cells", &propint,
+                sizeof(int));
+        if (ret < 0) {
+            error_report("could not set #dma-cells property of node %s",
+                    nodename);
+            g_free(property);
+            goto fail;
+        }
+        g_free(property);
+    }
+
+    property = vfio_get_dev_property(vbasedev->fd, "#dma-requests",
+            VFIO_DEV_PROPERTY_TYPE_U32);
+    if (property != NULL) {
+        propint = cpu_to_be32(((__u32 *) property->data)[0]);
+        ret = qemu_fdt_setprop(fdt, nodename, "#dma-cells", &propint,
+                sizeof(int));
+        if (ret < 0) {
+            error_report("could not set #dma-cells property of node %s",
+                    nodename);
+            g_free(property);
+            goto fail;
+        }
+        g_free(property);
+    }
+
     g_free(nodename);
 
     return 0;
Signature · 1 line
2.3.0