VFIO support for AMBA devices
v3 cover VFIO support for AMBA devices
The following series add VFIO support for AMBA devices.
It introduces multiple compatible string support to deal with arm,primecell compatible string.
The VFIOPlatformDevice now checks for this string and performs amba specific operations if it is present (change path of the device, add clock in the fd).
This patch applies on top of http://git.linaro.org/people/eric.auger/qemu.git
on branch vfio_integ_v9.
The series has been tested using PL330 device.
The test performed relies on a special guest kernel, which can be found here
http://github.com/virtualopensystems/linux-kvm-arm.git on branch vfio-platform-v6-guide, with CONFIG_KVM, CONFIG_ARM_SMMU, CONFIG_PL330_DMA and CONFIG_VOSYS_DMATEST enabled.
Run qemu using this command:
./qemu-system-arm -M virt -m 512M -enable-kvm \
-device vfio-pl330,host="2c0a0000.dma",id=dma,x-irqfd=false,x-forward=false \
-append "earlyprintk console=ttyAMA0" \
-kernel zImage -nographicThen run DMA test:
mount -t debugfs none /sys/kernel/debug
echo 1 > /sys/kernel/debug/vosys_dmatest/start
Baptiste Reynal (3):
hw/vfio/sysbus-fdt: helper routines to create fdt nodes hw/vfio: amba device support
hw/vfio: add pl330 device support
hw/arm/sysbus-fdt.c | 210 ++++++++++++++++++++++++++++++++++-------- hw/vfio/Makefile.objs | 1 + hw/vfio/pl330.c | 41 +++++++++ hw/vfio/platform.c | 20 +++- include/hw/vfio/vfio-common.h | 1 + include/hw/vfio/vfio-pl330.h | 26 ++++++ 6 files changed, 258 insertions(+), 41 deletions(-) create mode 100644 hw/vfio/pl330.c create mode 100644 include/hw/vfio/vfio-pl330.h
Signature · 1 line
2.3.0
RFC PATCH v3 1/3 hw/vfio/sysbus-fdt: helper routines to create fdt nodes
Creates set_interrupts_fdt_node and set_regions_fdt_node for code reusability.
---
v2 -> v3:
handle intermediate ret values in set_regions_fdt_node and set_interrupts_fdt_node
--- hw/arm/sysbus-fdt.c | 129 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 91 insertions(+), 38 deletions(-)
hw/arm/sysbus-fdt.c +91 -38
@@ -58,39 +58,59 @@ typedef struct NodeCreationPair {/* Device Specific Code */ /** -* add_calxeda_midway_xgmac_fdt_node +* set_interrupts_fdt_node * -* Generates a very simple node with following properties: -* compatible string, regs, interrupts +* Helper function, generate interrupts property for a given node */ -static int add_calxeda_midway_xgmac_fdt_node (SysBusDevice *sbdev,void *opaque) +static int set_interrupts_fdt_node (char *nodename,SysBusDevice *sbdev, +void *opaque,int type,int flags) {PlatformBusFDTData *data = opaque;PlatformBusDevice *pbus = data->pbus;void *fdt = data->fdt; -const char *parent_node = data->pbus_node_name; -int compat_str_len; -char *nodename; -int i, ret =-1 ;uint32_t *irq_attr; -uint64_t *reg_attr; -uint64_t mmio_base;uint64_t irq_number; +int i, ret;VFIOPlatformDevice *vdev =VFIO_PLATFORM_DEVICE (sbdev);VFIODevice *vbasedev = &vdev->vbasedev; -Object *obj =OBJECT (sbdev); - mmio_base =object_property_get_int (obj,"mmio[0]" ,NULL ); + irq_attr =g_new (uint32_t, vbasedev->num_irqs*3 ); - nodename =g_strdup_printf ("%s/%s@%" PRIx64, parent_node, - vbasedev->name, - mmio_base); +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); + irq_attr[3 *i+1 ] =cpu_to_be32 (irq_number); + irq_attr[3 *i+2 ] =cpu_to_be32 (flags); + } -qemu_fdt_add_subnode (fdt, nodename); + ret =qemu_fdt_setprop (fdt, nodename,"interrupts" , + irq_attr, vbasedev->num_irqs*3 *sizeof (uint32_t )); - compat_str_len =strlen (vdev->compat) +1 ; -qemu_fdt_setprop (fdt, nodename,"compatible" , - vdev->compat, compat_str_len); +if (ret <0 ) { +error_report ("could not set interrupts property of node %s" , nodename); + } +g_free (irq_attr); + +return ret; +} + +/** +* set_regions_fdt_node +* +* Helper function, generate reg property for a given node +*/ +static int set_regions_fdt_node (char *nodename,SysBusDevice *sbdev, +void *opaque) +{ +PlatformBusFDTData *data = opaque; +PlatformBusDevice *pbus = data->pbus; +void *fdt = data->fdt; +uint64_t *reg_attr; +uint64_t mmio_base; +int i, ret; +VFIOPlatformDevice *vdev =VFIO_PLATFORM_DEVICE (sbdev); +VFIODevice *vbasedev = &vdev->vbasedev; reg_attr =g_new (uint64_t, vbasedev->num_regions*4 );
@@ -103,34 +123,67 @@ static int add_calxeda_midway_xgmac_fdt_node(SysBusDevice *sbdev, void *opaque) } ret =qemu_fdt_setprop_sized_cells_from_array (fdt, nodename,"reg" , - vbasedev->num_regions*2 , reg_attr); -if (ret) { + vbasedev->num_regions*2 , reg_attr); + +if (ret <0 ) {error_report ("could not set reg property of node %s" , nodename); - goto fail_reg; } +g_free (reg_attr); - irq_attr =g_new (uint32_t, vbasedev->num_irqs*3 ); +return ret; +} -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 (0 ); - irq_attr[3 *i+1 ] =cpu_to_be32 (irq_number); - irq_attr[3 *i+2 ] =cpu_to_be32 (0x4 ); +/** +* add_calxeda_midway_xgmac_fdt_node +* +* Generates a very simple node with the following properties: +* compatible string, regs, interrupts +*/ +static int add_calxeda_midway_xgmac_fdt_node (SysBusDevice *sbdev,void *opaque) +{ +PlatformBusFDTData *data = opaque; +void *fdt = data->fdt; +const char *parent_node = data->pbus_node_name; +int compat_str_len; +char *nodename; +int ret; +uint64_t mmio_base; +VFIOPlatformDevice *vdev =VFIO_PLATFORM_DEVICE (sbdev); +VFIODevice *vbasedev = &vdev->vbasedev; +Object *obj =OBJECT (sbdev); + + mmio_base =object_property_get_int (obj,"mmio[0]" ,NULL ); + + nodename =g_strdup_printf ("%s/%s@%" PRIx64, parent_node, + vbasedev->name, + mmio_base); + +qemu_fdt_add_subnode (fdt, nodename); + + compat_str_len =strlen (vdev->compat) +1 ; +qemu_fdt_setprop (fdt, nodename,"compatible" , + vdev->compat, compat_str_len); + + ret =set_regions_fdt_node (nodename, sbdev, opaque); + +if (ret <0 ) { + goto fail; } - ret =qemu_fdt_setprop (fdt, nodename,"interrupts" , - irq_attr, vbasedev->num_irqs*3 *sizeof (uint32_t )); -if (ret) { -error_report ("could not set interrupts property of node %s" , - nodename); + ret =set_interrupts_fdt_node (nodename, sbdev, opaque,0 ,0x4 ); + +if (ret <0 ) { + goto fail; } -g_free (irq_attr); -fail_reg: -g_free (reg_attr);g_free (nodename); -return ret; + +return 0 ; + +fail: +error_report ("could not initialize node %s" , nodename); +g_free (nodename); +return -1 ; }/* list of supported dynamic sysbus devices */
Signature · 1 line
2.3.0
RFC PATCH v3 2/3 hw/vfio: amba device support
In reply to Baptiste ReynalAdd VFIO_DEVICE_TYPE_AMBA.
Differentiate amba and platform devices according to compatible string.
---
v2 -> v3:
add amba flag check in vfio_populate_device
--- hw/vfio/platform.c | 20 +++++++++++++++----- include/hw/vfio/vfio-common.h | 1 + 2 files changed, 16 insertions(+), 5 deletions(-)
hw/vfio/platform.c +15 -5
@@ -422,8 +422,9 @@ static int vfio_populate_device(VFIODevice *vbasedev)VFIOPlatformDevice *vdev =container_of (vbasedev, VFIOPlatformDevice, vbasedev); -if (!(vbasedev->flags &VFIO_DEVICE_FLAGS_PLATFORM )) { -error_report ("vfio: Um, this isn't a platform device" ); +if (!(vbasedev->flags & (VFIO_DEVICE_FLAGS_PLATFORM | +VFIO_DEVICE_FLAGS_AMBA ))) { +error_report ("vfio: Um, this isn't a platform neither an amba device" ); goto error; }
@@ -521,8 +522,13 @@ static int vfio_base_device_init(VFIODevice *vbasedev) }/* Check that the host device exists */ -snprintf (path,sizeof (path),"/sys/bus/platform/devices/%s/" , - vbasedev->name); +if (vbasedev->type ==VFIO_DEVICE_TYPE_AMBA ) { +snprintf (path,sizeof (path),"/sys/bus/amba/devices/%s/" , + vbasedev->name); + }else { +snprintf (path,sizeof (path),"/sys/bus/platform/devices/%s/" , + vbasedev->name); + }if (stat (path, &st) <0 ) {error_report ("vfio: error: no such host device: %s" , path);
@@ -619,7 +625,11 @@ static void vfio_platform_realize(DeviceState *dev, Error **errp)VFIODevice *vbasedev = &vdev->vbasedev;int i, ret; - vbasedev->type =VFIO_DEVICE_TYPE_PLATFORM ; +if (strstr (vdev->compat,"arm,primecell" )) { + vbasedev->type =VFIO_DEVICE_TYPE_AMBA ; + }else { + vbasedev->type =VFIO_DEVICE_TYPE_PLATFORM ; + } vbasedev->ops = &vfio_platform_ops;#ifdef CONFIG_KVM
include/hw/vfio/vfio-common.h +1 -0
@@ -44,6 +44,7 @@enum {VFIO_DEVICE_TYPE_PCI =0 ,VFIO_DEVICE_TYPE_PLATFORM =1 , +VFIO_DEVICE_TYPE_AMBA =2 , }; typedef struct VFIORegion {
Signature · 1 line
2.3.0
RFC PATCH v3 3/3 hw/vfio: add pl330 device support
In reply to Baptiste ReynalCreate a meta-device for PL330 DMA.
Add add_arm_pl330_fdt_node function, with multiple compatible string and clocks support.
---
v2 -> v3:
got to fail in case of apb_pclk missing
free nodename in fail case
--- hw/arm/sysbus-fdt.c | 85 ++++++++++++++++++++++++++++++++++++++++++++ hw/vfio/Makefile.objs | 1 + hw/vfio/pl330.c | 41 +++++++++++++++++++++ include/hw/vfio/vfio-pl330.h | 26 ++++++++++++++ 4 files changed, 153 insertions(+) create mode 100644 hw/vfio/pl330.c create mode 100644 include/hw/vfio/vfio-pl330.h
hw/arm/sysbus-fdt.c +85 -0
@@ -28,6 +28,9 @@#include "sysemu/sysemu.h" #include "hw/vfio/vfio-platform.h" #include "hw/vfio/vfio-calxeda-xgmac.h" +#include "hw/vfio/vfio-pl330.h" + +#include <libfdt.h> /* * internal struct that contains the information to create dynamic
@@ -186,9 +189,91 @@ fail:return -1 ; } +/** +* add_arm_pl330_fdt_node +* +* Generates a very simple node with following properties: +* compatible string, regs, interrupts, clocks, clock-names +*/ +static int add_arm_pl330_fdt_node (SysBusDevice *sbdev,void *opaque) +{ +PlatformBusFDTData *data = opaque; +void *fdt = data->fdt; +const char *parent_node = data->pbus_node_name; +int compat_str_len; +char *nodename; +int ret; +uint64_t mmio_base; +VFIOPlatformDevice *vdev =VFIO_PLATFORM_DEVICE (sbdev); +VFIODevice *vbasedev = &vdev->vbasedev; +Object *obj =OBJECT (sbdev); + + mmio_base =object_property_get_int (obj,"mmio[0]" ,NULL ); + + nodename =g_strdup_printf ("%s/%s@%" PRIx64, parent_node, + vbasedev->name, + mmio_base); + +qemu_fdt_add_subnode (fdt, nodename); + +/* +* Process compatible string to deal with multiple strings +* (; is replaced by \0) +*/ +char *compat =g_strdup (vdev->compat); + compat_str_len =strlen (compat) +1 ; + +char *semicolon = compat; +while ((semicolon =strchr (semicolon,';' )) !=NULL ) { + *semicolon ='\0' ; + } + +qemu_fdt_setprop (fdt, nodename,"compatible" , + compat, compat_str_len); + +/* Setup clocks for AMBA device */ +/* 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)); + } + + ret =set_regions_fdt_node (nodename, sbdev, opaque); + +if (ret <0 ) { +error_report ("could not set reg property of node %s" , nodename); + goto fail; + } + + ret =set_interrupts_fdt_node (nodename, sbdev, opaque,0 ,0x4 ); + +if (ret <0 ) { +error_report ("could not set interrupts property of node %s" , + nodename); + goto fail; + } + +g_free (nodename); + +return 0 ; + +fail: +g_free (nodename); +return -1 ; +} +/* list of supported dynamic sysbus devices */ static const NodeCreationPair add_fdt_node_functions[] = { {TYPE_VFIO_CALXEDA_XGMAC , add_calxeda_midway_xgmac_fdt_node}, + {TYPE_VFIO_PL330 , add_arm_pl330_fdt_node}, {"" ,NULL },/* last element */ };
hw/vfio/Makefile.objs +1 -0
@@ -3,4 +3,5 @@ obj-$(CONFIG_SOFTMMU) += common.o obj-$(CONFIG_PCI) += pci.o obj-$(CONFIG_SOFTMMU) += platform.o obj-$(CONFIG_SOFTMMU) += calxeda-xgmac.o +obj-$(CONFIG_SOFTMMU) += pl330.o endif
hw/vfio/pl330.c new file +41 -0
41 lines not on this page · raw message
include/hw/vfio/vfio-pl330.h new file +26 -0
@@ -0,0 +1,26 @@ +#ifndef HW_VFIO_VFIO_PL330 +#define HW_VFIO_VFIO_PL330 + +#include "hw/vfio/vfio-platform.h" + +#define TYPE_VFIO_PL330 "vfio-pl330" + +typedef struct VFIOPl330Device { +VFIOPlatformDevice vdev; +}VFIOPl330Device ; + +typedef struct VFIOPl330DeviceClass { +VFIOPlatformDeviceClass parent_class; +DeviceRealize parent_realize; +}VFIOPl330DeviceClass ; + +#define VFIO_PL330_DEVICE (obj) \ + OBJECT_CHECK(VFIOPl330Device, (obj), TYPE_VFIO_PL330) +#define VFIO_PL330_DEVICE_CLASS (klass) \ + OBJECT_CLASS_CHECK(VFIOPl330DeviceClass, (klass), \ + TYPE_VFIO_PL330) +#define VFIO_PL330_DEVICE_GET_CLASS (obj) \ + OBJECT_GET_CLASS(VFIOPl330DeviceClass, (obj), \ + TYPE_VFIO_PL330) + +#endif
Signature · 1 line
2.3.0