AMBA platform device passthrough
RFC PATCH v3 1/4 Add AMBA devices support to VFIO
In reply to Alvise RigoThe impossibility to add more then one compatibility property to the device tree node was not permitting to bind AMBA devices.
Now we can add an arbitrary number of compatibility property values divided by the character ";".
If the compatibility string contains the substring "arm,primecell", a clock property will be added to the device tree node in order to allow the AMBA bus code to probe the device.
[Eric Auger]
put str_ptr in the declaration part and rename pcompat into compat
--- hw/arm/virt.c | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-)
hw/arm/virt.c +35 -10
@@ -369,6 +369,7 @@ static int vfio_init_func(QemuOpts *opts, void *opaque)int irq_start = vbi->avail_vfio_irq;hwaddr vfio_base = vbi->avail_vfio_base;char *nodename; +char *str_ptr;char *corrected_compat, *compat, *name;int num_irqs, num_regions;MemoryRegion *mr;
@@ -377,6 +378,8 @@ static int vfio_init_func(QemuOpts *opts, void *opaque)uint64_t *reg_attr;uint64_t size;Error *errp =NULL ; +bool is_amba = false; +int compat_str_len;if (!driver) {qerror_report (QERR_MISSING_PARAMETER ,"driver" );
@@ -442,17 +445,30 @@ static int vfio_init_func(QemuOpts *opts, void *opaque)/* * process compatibility property string passed by end-user -* replaces / by , -* currently a single property compatibility value is supported! +* replaces / by , and ; by NUL character */ corrected_compat =g_strdup (compat); -char *slash =strchr (corrected_compat,'/' ); -if (slash !=NULL ) { - *slash =',' ; - }else { -error_report ("Wrong compat string %s, should contain a /\n" , - compat); -exit (1 ); +/* +* the total length of the string has to include also the last +* NUL char. +*/ + compat_str_len =strlen (corrected_compat) +1 ; + + str_ptr = corrected_compat; +while ((str_ptr =strchr (str_ptr,'/' )) !=NULL ) { + *str_ptr =',' ; + } + +/* check if is an AMBA device */ + str_ptr = corrected_compat; +if (strstr (str_ptr,"arm,primecell" ) !=NULL ) { + is_amba = true; + } + +/* substitute ";" with the NUL char */ + str_ptr = corrected_compat; +while ((str_ptr =strchr (str_ptr,';' )) !=NULL ) { + *str_ptr ='\0' ; }sysbus_mmio_map (s,0 , vfio_base);
@@ -462,7 +478,7 @@ static int vfio_init_func(QemuOpts *opts, void *opaque)qemu_fdt_add_subnode (vbi->fdt, nodename);qemu_fdt_setprop (vbi->fdt, nodename,"compatible" , - corrected_compat,strlen (corrected_compat)); + corrected_compat, compat_str_len); ret =qemu_fdt_setprop_sized_cells_from_array (vbi->fdt, nodename,"reg" , num_regions*2 , reg_attr);
@@ -471,6 +487,15 @@ static int vfio_init_func(QemuOpts *opts, void *opaque) } irq_attr =g_new (uint32_t, num_irqs*3 ); + +if (is_amba) { +qemu_fdt_setprop_cells (vbi->fdt, nodename,"clocks" , + vbi->clock_phandle); +char clock_names[] ="apb_pclk" ; +qemu_fdt_setprop (vbi->fdt, nodename,"clock-names" , clock_names, +sizeof (clock_names)); + } +for (i =0 ; i < num_irqs; i++) {sysbus_connect_irq (s, i, vbi->pic[irq_start+i]);
Signature · 1 line
1.9.1
RFC PATCH v3 2/4 vfio: Add irqfd support in platform device
In reply to Alvise RigoFrom: Eric Auger <[email protected]>
This patch aims at optimizing IRQ handling using irqfd framework. It brings significant performance improvement over "traditional" IRQ handling introduced in :
"vfio: Add initial IRQ support in platform device".
This new IRQ handling method depends on kernel KVM irqfd/GSI routing capability.
The IRQ handling method can be dynamically chosen (default is irqfd, if kernel supports it obviously). For example to disable irqfd handling, use:
-device vfio-platform,vfio_device="fff51000.ethernet",\
compat="calxeda/hb-xgmac",mmap-timeout-ms=110,irqfd=false\
Performances are improved for the following reasons:
- eventfds signalled by the VFIO platform driver are handled on kernel side by the KVM irqfd framework.
- the end of interrupt(EOI) is trapped at GIC level and not at MMIO region level. As a reminder, in traditional IRQ handling QEMU assumed the first guest access to a device MMIO region after IRQ hit was the IRQ status register reset. This trap was approximate and obliged to swap to slow path after IRQ hit. A mmap timer mechanism enabled to swap back to fast path after the mmap period introducing extra complexity. Now GIC detects the completion of the virtual IRQ and signals a resampler eventfd on maintenance IRQ. The corresponding handler re-enables the physical IRQ.
Next optimization step consists in attempting to remove EOI trap (ie. maintenance IRQ). This should be covered by another patch in near future.
This work was tested with Calxeda Midway xgmac.
--- hw/arm/virt.c | 14 +++++ hw/intc/arm_gic_kvm.c | 1 + hw/vfio/platform.c | 165 +++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 166 insertions(+), 14 deletions(-)
hw/arm/virt.c +14 -0
@@ -43,6 +43,9 @@#include "monitor/qdev.h" #include "qemu/config-file.h" +#define ENABLE_IRQFD 1 +void vfio_setup_irqfd (SysBusDevice *s,int index,int virq); +#define NUM_VIRTIO_TRANSPORTS 32/* Number of external interrupt lines to configure the GIC with */
@@ -380,6 +383,7 @@ static int vfio_init_func(QemuOpts *opts, void *opaque)Error *errp =NULL ;bool is_amba = false;int compat_str_len; +bool irqfd_allowed;if (!driver) {qerror_report (QERR_MISSING_PARAMETER ,"driver" );
@@ -417,6 +421,13 @@ static int vfio_init_func(QemuOpts *opts, void *opaque) error_get_pretty(errp));exit (1 ); } + irqfd_allowed =object_property_get_bool (OBJECT (s),"irqfd" , &errp); +if (errp !=NULL ) { +error_report ("Couldn't retrieve irqfd flag: %s\n" , +error_get_pretty (errp)); +exit (1 ); + } +/* * collect region info and build reg property as tuplets
@@ -502,6 +513,9 @@ static int vfio_init_func(QemuOpts *opts, void *opaque) irq_attr[3 *i] =cpu_to_be32 (0 ); irq_attr[3 *i+1 ] =cpu_to_be32 (irq_start+i); irq_attr[3 *i+2 ] =cpu_to_be32 (0x4 ); +if (irqfd_allowed) { +vfio_setup_irqfd (s, i, irq_start+i); + } } ret = qemu_fdt_setprop(vbi->fdt, nodename, "interrupts",
hw/intc/arm_gic_kvm.c +1 -0
@@ -576,6 +576,7 @@ static void kvm_arm_gic_realize(DeviceState *dev, Error **errp)KVM_DEV_ARM_VGIC_GRP_ADDR ,KVM_VGIC_V2_ADDR_TYPE_CPU , s->dev_fd); + kvm_irqfds_allowed = true; }static void kvm_arm_gic_class_init (ObjectClass *klass,void *data)
hw/vfio/platform.c +151 -14
229 lines not on this page · raw message
Signature · 1 line
1.9.1
RFC PATCH v3 3/4 Force eventfd as notifying mechanism for VFIO
In reply to Alvise RigoThe method event_notifier_init doesn't assure that eventfd is used. Create a new method (eventfd_notifier_init) to initialize only eventfd file descriptors. This method is also used by event_notifier_init which has not been modified to keep compatibility with util/event_notifier-win32.c.
--- hw/vfio/pci.c | 18 +++++++++--------- hw/vfio/platform.c | 8 ++++---- include/qemu/event_notifier.h | 1 + util/event_notifier-posix.c | 24 +++++++++++++++++++++--- 4 files changed, 35 insertions(+), 16 deletions(-)
hw/vfio/pci.c +9 -9
@@ -261,8 +261,8 @@ static void vfio_enable_intx_kvm(VFIOPCIDevice *vdev)pci_irq_deassert (&vdev->pdev);/* Get an eventfd for resample/unmask */ -if (event_notifier_init (&vdev->intx.unmask,0 )) { -error_report ("vfio: Error: event_notifier_init failed eoi" ); +if (eventfd_notifier_init (&vdev->intx.unmask,0 )) { +error_report ("vfio: Error: eventfd_notifier_init failed eoi" ); goto fail; }
@@ -414,9 +414,9 @@ static int vfio_enable_intx(VFIOPCIDevice *vdev) }#endif - ret =event_notifier_init (&vdev->intx.interrupt,0 ); + ret =eventfd_notifier_init (&vdev->intx.interrupt,0 );if (ret) { -error_report ("vfio: Error: event_notifier_init failed" ); +error_report ("vfio: Error: eventfd_notifier_init failed" );return ret; }
@@ -555,8 +555,8 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,msix_vector_use (pdev, nr); -if (event_notifier_init (&vector->interrupt,0 )) { -error_report ("vfio: Error: event_notifier_init failed" ); +if (eventfd_notifier_init (&vector->interrupt,0 )) { +error_report ("vfio: Error: eventfd_notifier_init failed" ); }/*
@@ -718,8 +718,8 @@ retry: vector->vdev = vdev; vector->use = true; -if (event_notifier_init (&vector->interrupt,0 )) { -error_report ("vfio: Error: event_notifier_init failed" ); +if (eventfd_notifier_init (&vector->interrupt,0 )) { +error_report ("vfio: Error: eventfd_notifier_init failed" ); } vector->msg =msi_get_message (&vdev->pdev, i);
@@ -2925,7 +2925,7 @@ static void vfio_register_err_notifier(VFIOPCIDevice *vdev)return ; } -if (event_notifier_init (&vdev->err_notifier,0 )) { +if (eventfd_notifier_init (&vdev->err_notifier,0 )) {error_report ("vfio: Unable to init event notifier for error detection" ); vdev->pci_aer = false;return ;
hw/vfio/platform.c +4 -4
@@ -349,9 +349,9 @@ static int vfio_enable_intp(VFIODevice *vdev, unsigned int index)sysbus_init_irq (sbdev, &intp->qemuirq); - ret =event_notifier_init (&intp->interrupt,0 ); + ret =eventfd_notifier_init (&intp->interrupt,0 );if (ret) { -error_report ("vfio: Error: event_notifier_init failed " ); +error_report ("vfio: Error: eventfd_notifier_init failed " );return ret; }/* build the irq_set to be passed to the vfio kernel driver */
@@ -471,8 +471,8 @@ static void vfio_enable_intp_kvm(VFIOINTp *intp)qemu_set_irq (intp->qemuirq,0 );/* Get an eventfd for resample/unmask */ -if (event_notifier_init (&intp->unmask,0 )) { -error_report ("vfio: Error: event_notifier_init failed eoi" ); +if (eventfd_notifier_init (&intp->unmask,0 )) { +error_report ("vfio: Error: eventfd_notifier_init failed eoi" ); goto fail; }
include/qemu/event_notifier.h +1 -0
@@ -30,6 +30,7 @@ struct EventNotifier {typedef void EventNotifierHandler (EventNotifier *); +int eventfd_notifier_init (EventNotifier *,int active);int event_notifier_init (EventNotifier *,int active);void event_notifier_cleanup (EventNotifier *);int event_notifier_set (EventNotifier *);
util/event_notifier-posix.c +21 -3
@@ -25,19 +25,37 @@ void event_notifier_init_fd(EventNotifier *e, int fd) e->wfd = fd; } -int event_notifier_init (EventNotifier *e,int active) +int eventfd_notifier_init (EventNotifier *e,int active) { -int fds[2 ];int ret;#ifdef CONFIG_EVENTFD ret =eventfd (0 ,EFD_NONBLOCK |EFD_CLOEXEC ); +if (ret >=0 ) { + e->rfd = e->wfd = ret; + ret =0 ; + +if (active) { +event_notifier_set (e); + } + }#else ret =-1 ; errno =ENOSYS ;#endif + +return ret; +} + +int event_notifier_init (EventNotifier *e,int active) +{ +int fds[2 ]; +int ret; + + ret =eventfd_notifier_init (e, active); +if (ret >=0 ) { - e->rfd = e->wfd = ret; +return 0 ; }else {if (errno !=ENOSYS ) {return -errno;
Signature · 1 line
1.9.1
RFC PATCH v3 4/4 Let VFIO handle the unmasking of interrupts at EOI
In reply to Alvise RigoNow that VFIO can unmask the interrupt autonomously through an eventfd file descriptor, get rid of the resampler_handler.
TODO: move this code and PCI one to common.c
--- hw/vfio/platform.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-)
hw/vfio/platform.c +24 -14
@@ -440,20 +440,13 @@ static void vfio_disable_intp(VFIOINTp *intp)/* IRQFD */ -static void resampler_handler (void *opaque) -{ -VFIOINTp *intp = (VFIOINTp *)opaque; -DPRINTF ("%s index %d virtual ID = %d fd = %d\n" , - __func__, - intp->pin, intp->virtualID, -event_notifier_get_fd (&intp->unmask)); -vfio_unmask_irqindex (&intp->vdev->vdev, intp->pin); -} - - -static void vfio_enable_intp_kvm (VFIOINTp *intp) +static void vfio_enable_intp_kvm (VFIOPlatformDevice *vdev,VFIOINTp *intp) {#ifdef CONFIG_KVM +int argsz, ret; +struct vfio_irq_set *irq_set; +uint32_t *efd; +struct kvm_irqfd irqfd = { .fd =event_notifier_get_fd (&intp->interrupt), .gsi = intp->virtualID,
@@ -476,10 +469,27 @@ static void vfio_enable_intp_kvm(VFIOINTp *intp) goto fail; } + argsz =sizeof (*irq_set) +sizeof (*efd); + + irq_set =g_malloc0 (argsz); + irq_set->argsz = argsz; + irq_set->flags =VFIO_IRQ_SET_DATA_EVENTFD |VFIO_IRQ_SET_ACTION_UNMASK ; + irq_set->index = intp->pin; + irq_set->start =0 ; + irq_set->count =1 ; +/* KVM triggers it, VFIO listens for it */ irqfd.resamplefd =event_notifier_get_fd (&intp->unmask); -qemu_set_fd_handler (irqfd.resamplefd, resampler_handler,NULL , intp); + efd = (uint32_t *)&irq_set->data; + + *efd = irqfd.resamplefd; + ret =ioctl (vdev->vdev.fd,VFIO_DEVICE_SET_IRQS , irq_set); +g_free (irq_set); +if (ret) { +error_report ("vfio: Error: Failed to setup interrupt unmask fd: %m" ); + goto fail_irqfd; + }if (kvm_vm_ioctl (kvm_state,KVM_IRQFD , &irqfd)) {error_report ("vfio: Error: Failed to setup resample irqfd: %m" );
@@ -508,7 +518,7 @@ void vfio_setup_irqfd(SysBusDevice *s, int index, int virq) QLIST_FOREACH(intp, &vdev->intp_list, next) {if (intp->pin == index) { intp->virtualID = virq; -vfio_enable_intp_kvm (intp); +vfio_enable_intp_kvm (vdev, intp); } } }
Signature · 1 line
1.9.1
v3 cover AMBA platform device passthrough
These patches are the next iteration of "[Qemu-devel] [RFC v2 0/4] AMBA platform device passthrough" and are based on the latest version of VFIO for platform devices "[RFC PATCH v6 00/20] VFIO support for platform devices on ARM" and the initial patch series "[Qemu-devel] [RFC v3 0/10] KVM platform device passthrough".
- [PATCH 1/4] Possibility to bind a wider class of devices. In particular the patch proposes a solution to enhance a bit the device tree nodes generation, allowing to specify more than one compatibility property (handy for AMBA devices). This was required by the DMA330 that needs "arm,pl330","arm,primecell" as compatibility string, together with a clock source defined in the device tree. In the future VFIO will be able to tell if we are dealing with an AMBA device; before that, we have to rely on the compatibility string to state that.
- [PATCH 3/4] The last version of VFIO allows to pass an eventfd file descriptor to unmask a host interrupt. This can be used in conjunction with IRQFD to benefit of the resampler feature; in this way QEMU no longer has to unmask the interrupt because the kernel will take care of it.
- [PATCH 4/4] If event_notifier_init fails to create a new eventfd, it will fallback using pipe/pipe2 and we end up passing to the kernel a wrong file descriptor. This patch force to use eventfd.
Changes since v2:
- The EXEC_FLAG now is always used by VFIO if it is supported. QEMU does not need to query the kernel any more to know whether using this flag or not. For this reason, the patch [1/4] of the previous version has been removed.
- Included patch [2/4] from Eric Auger, required by patch [3/4] and [4/4].
- The patch [3/4] of the previous version has been also removed since the introduction of the new mechanism of handling IRQs and EOIs through IRQFD. The patch can nevertheless be used to improve the standard, timer based, mechanism.
- [PATCH 4/4] Instead of checking whether eventfd has been used by event_notifier_init, a new method (eventfd_notifier_init) is added to fulfill this purpose.
Alvise Rigo (3):
Add AMBA devices support to VFIO
Force eventfd as notifying mechanism for VFIO Let VFIO handle the unmasking of interrupts at EOI
Eric Auger (1):
vfio: Add irqfd support in platform device
hw/arm/virt.c | 59 +++++++++++--- hw/intc/arm_gic_kvm.c | 1 + hw/vfio/pci.c | 18 ++--- hw/vfio/platform.c | 179 ++++++++++++++++++++++++++++++++++++++---- include/qemu/event_notifier.h | 1 + util/event_notifier-posix.c | 24 +++++- 6 files changed, 244 insertions(+), 38 deletions(-)
Signature · 1 line
1.9.1