Skip to contentSkip to conversation

Create ARM KVM VGIC with device control API

RFC PATCH Superseded Christoffer Dall 2 participants 8 messages Sep 26, 2013 – Oct 14, 2013

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover Create ARM KVM VGIC with device control API

This patch series adds generic support for issuing device control related ioctls and supports creating the ARM KVM-accelerated VGIC using the device control API while maintaining backwards compatibility for older kernels.

This is an RFC patch set because it relies on kernel header changes that are not yet upstream.

Changelogs in the individual patches.

Christoffer Dall (4):
kvm: Update headers for device control api
kvm: Introduce kvm_arch_irqchip_create
kvm: Common device control API functions
arm: vgic device control api support

 hw/intc/arm_gic_kvm.c       |   22 +++++++++++++++--
 hw/intc/gic_internal.h      |    1 +
 include/sysemu/kvm.h        |   34 ++++++++++++++++++++++++++
 kvm-all.c                   |   50 +++++++++++++++++++++++++++++++++++++--
 linux-headers/asm-arm/kvm.h |    8 +++++++
 linux-headers/linux/kvm.h   |    1 +
 stubs/Makefile.objs         |    1 +
 stubs/kvm.c                 |    7 ++++++
 target-arm/kvm.c            |   55 +++++++++++++++++++++++++++++++++++++------
 target-arm/kvm_arm.h        |   18 +++++++++-----
 trace-events                |    1 +
 11 files changed, 181 insertions(+), 17 deletions(-)
 create mode 100644 stubs/kvm.c
Signature · 1 line
1.7.10.4
#1 lore raw

RFC PATCH v2 1/4 kvm: Update headers for device control api

Update the KVM kernel headers to add support for the device control API on ARM used to create in-kernel devices and set and get attributes on these.

This is needed for VGIC save/restore with KVM ARM targets.

Headers are included from:
git://git.linaro.org/people/cdall/linux-kvm-arm.git vgic-migrate

---
 linux-headers/asm-arm/kvm.h |    8 ++++++++
 linux-headers/linux/kvm.h   |    1 +
 2 files changed, 9 insertions(+)
linux-headers/asm-arm/kvm.h +8 -0
index c1ee007..587f1ae 100644
@@ -142,6 +142,14 @@ struct kvm_arch_memory_slot {
 #define KVM_REG_ARM_VFP_FPINST		0x1009
 #define KVM_REG_ARM_VFP_FPINST2		0x100A
 
+/* Device Control API: ARM VGIC */
+#define KVM_DEV_ARM_VGIC_GRP_ADDR	0
+#define KVM_DEV_ARM_VGIC_GRP_DIST_REGS	1
+#define KVM_DEV_ARM_VGIC_GRP_CPU_REGS	2
+#define   KVM_DEV_ARM_VGIC_CPUID_SHIFT	32
+#define   KVM_DEV_ARM_VGIC_CPUID_MASK	(0xffULL << KVM_DEV_ARM_VGIC_CPUID_SHIFT)
+#define   KVM_DEV_ARM_VGIC_OFFSET_SHIFT	0
+#define   KVM_DEV_ARM_VGIC_OFFSET_MASK	(0xffffffffULL << KVM_DEV_ARM_VGIC_OFFSET_SHIFT)
 
 /* KVM_IRQ_LINE irq field index values */
 #define KVM_ARM_IRQ_TYPE_SHIFT		24
linux-headers/linux/kvm.h +1 -0
index c614070..7f66a4f 100644
@@ -839,6 +839,7 @@ struct kvm_device_attr {
 #define KVM_DEV_TYPE_FSL_MPIC_20	1
 #define KVM_DEV_TYPE_FSL_MPIC_42	2
 #define KVM_DEV_TYPE_XICS		3
+#define KVM_DEV_TYPE_ARM_VGIC_V2	4
 
 /*
  * ioctls for VM fds
Signature · 1 line
1.7.10.4
#2 lore raw

RFC PATCH v2 2/4 kvm: Introduce kvm_arch_irqchip_create

In reply to Christoffer Dall

Introduce kvm_arch_irqchip_create an arch-specific hook in preparation for architecture-specific use of the device control API to create IRQ chips.

Following patches will implement the ARM irqchip create method to prefer the device control API over the older KVM_CREATE_IRQCHIP API.

---
Changelog[v2]:
- Proper formatted function comments
- Use QEMU's stubs mechanism for KVM stubs

---
 include/sysemu/kvm.h |   12 ++++++++++++
 kvm-all.c            |   11 +++++++++--
 stubs/Makefile.objs  |    1 +
 stubs/kvm.c          |    7 +++++++
 4 files changed, 29 insertions(+), 2 deletions(-)
 create mode 100644 stubs/kvm.c
include/sysemu/kvm.h +12 -0
index de74411..fbb2776 100644
@@ -314,4 +314,16 @@ int kvm_irqchip_remove_irqfd_notifier(KVMState *s, EventNotifier *n, int virq);
 void kvm_pc_gsi_handler(void *opaque, int n, int level);
 void kvm_pc_setup_irq_routing(bool pci_enabled);
 void kvm_init_irq_routing(KVMState *s);
+
+/**
+ * kvm_arch_irqchip_create:
+ * @KVMState: The KVMState pointer
+ *
+ * Allow architectures to create an in-kernel irq chip themselves.
+ *
+ * Returns: < 0: error
+ *            0: irq chip was not created
+ *          > 0: irq chip was created
+ */
+int kvm_arch_irqchip_create(KVMState *s);
 #endif
kvm-all.c +9 -2
index 716860f..fe64f3b 100644
@@ -1295,10 +1295,17 @@ static int kvm_irqchip_create(KVMState *s)
         return 0;
     }
 
-    ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
+    /* First probe and see if there's a arch-specific hook to create the
+     * in-kernel irqchip for us */
+    ret = kvm_arch_irqchip_create(s);
     if (ret < 0) {
-        fprintf(stderr, "Create kernel irqchip failed\n");
         return ret;
+    } else if (ret == 0) {
+        ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
+        if (ret < 0) {
+            fprintf(stderr, "Create kernel irqchip failed\n");
+            return ret;
+        }
     }
 
     kvm_kernel_irqchip = true;
stubs/Makefile.objs +1 -0
index f306cba..f3eba26 100644
@@ -26,3 +26,4 @@ stub-obj-y += vm-stop.o
 stub-obj-y += vmstate.o
 stub-obj-$(CONFIG_WIN32) += fd-register.o
 stub-obj-y += cpus.o
+stub-obj-y += kvm.o
stubs/kvm.c new file +7 -0
new file mode 100644
index 0000000..e7c60b6
@@ -0,0 +1,7 @@
+#include "qemu-common.h"
+#include "sysemu/kvm.h"
+
+int kvm_arch_irqchip_create(KVMState *s)
+{
+    return 0;
+}
Signature · 1 line
1.7.10.4
Reviewed-by on v2 2/4
#3 lore raw

Re: [Qemu-devel] [RFC PATCH v2 2/4] kvm: Introduce kvm_arch_irqchip_create

Quoted 8 lines · Christoffer Dall

Introduce kvm_arch_irqchip_create an arch-specific hook in preparation for architecture-specific use of the device control API to create IRQ chips.

Following patches will implement the ARM irqchip create method to prefer the device control API over the older KVM_CREATE_IRQCHIP API.

-- PMM

#4 lore raw

RFC PATCH v2 3/4 kvm: Common device control API functions

In reply to Christoffer Dall

Introduces two simple functions:

    int kvm_device_ioctl(int fd, int type, ...);
    int kvm_create_device(KVMState *s, uint64_t type, bool test);

These functions wrap the basic ioctl-based interactions with KVM in a way similar to other KVM ioctl wrappers.

---
Changelog[v2]:
- Added function docs and adjust code formatting
- Return proper error value from kvm_create_device

---
 include/sysemu/kvm.h |   22 ++++++++++++++++++++++
 kvm-all.c            |   39 +++++++++++++++++++++++++++++++++++++++
 trace-events         |    1 +
 3 files changed, 62 insertions(+)
include/sysemu/kvm.h +22 -0
index fbb2776..7227a81 100644
@@ -190,6 +190,28 @@ int kvm_vm_ioctl(KVMState *s, int type, ...);
 
 int kvm_vcpu_ioctl(CPUState *cpu, int type, ...);
 
+/**
+ * kvm_device_ioctl - call an ioctl on a kvm device
+ * @fd: The KVM device file descriptor as returned from KVM_CREATE_DEVICE
+ * @type: The device-ctrl ioctl number
+ *
+ * Returns: -errno on error, nonnegative on success
+ */
+int kvm_device_ioctl(int fd, int type, ...);
+
+/**
+ * kvm_create_device - create a KVM device for the device control API
+ * @KVMState: The KVMState pointer
+ * @type: The KVM device type (see Documentation/virtual/kvm/devices in the
+ *        kernel source)
+ * @test: If true, only test if device can be created, but don't actually
+ *        create the device.
+ *
+ * Returns: -errno on error, nonnegative on success: @test ? 0 : device fd;
+ */
+int kvm_create_device(KVMState *s, uint64_t type, bool test);
+
+
 /* Arch specific hooks */
 
 extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
kvm-all.c +39 -0
index fe64f3b..0899c9d 100644
@@ -1770,6 +1770,24 @@ int kvm_vcpu_ioctl(CPUState *cpu, int type, ...)
     return ret;
 }
 
+int kvm_device_ioctl(int fd, int type, ...)
+{
+    int ret;
+    void *arg;
+    va_list ap;
+
+    va_start(ap, type);
+    arg = va_arg(ap, void *);
+    va_end(ap);
+
+    trace_kvm_device_ioctl(fd, type, arg);
+    ret = ioctl(fd, type, arg);
+    if (ret == -1) {
+        ret = -errno;
+    }
+    return ret;
+}
+
 int kvm_has_sync_mmu(void)
 {
     return kvm_check_extension(kvm_state, KVM_CAP_SYNC_MMU);
@@ -2064,3 +2082,24 @@ int kvm_on_sigbus(int code, void *addr)
 {
     return kvm_arch_on_sigbus(code, addr);
 }
+
+int kvm_create_device(KVMState *s, uint64_t type, bool test)
+{
+    int ret;
+    struct kvm_create_device create_dev;
+
+    create_dev.type = type;
+    create_dev.fd = -1;
+    create_dev.flags = test ? KVM_CREATE_DEVICE_TEST : 0;
+
+    if (!kvm_check_extension(s, KVM_CAP_DEVICE_CTRL)) {
+        return -ENOTSUP;
+    }
+
+    ret = kvm_vm_ioctl(s, KVM_CREATE_DEVICE, &create_dev);
+    if (ret) {
+        return ret;
+    }
+
+    return test ? 0 : create_dev.fd;
+}
trace-events +1 -0
index 3856b5c..5372c6e 100644
@@ -1163,6 +1163,7 @@ migrate_set_state(int new_state) "new state %d"
 kvm_ioctl(int type, void *arg) "type %d, arg %p"
 kvm_vm_ioctl(int type, void *arg) "type %d, arg %p"
 kvm_vcpu_ioctl(int cpu_index, int type, void *arg) "cpu_index %d, type %d, arg %p"
+kvm_device_ioctl(int fd, int type, void *arg) "dev fd %d, type %d, arg %p"
 kvm_run_exit(int cpu_index, uint32_t reason) "cpu_index %d, reason %d"
 
 # memory.c
Signature · 1 line
1.7.10.4
Reviewed-by on v2 3/4
#5 lore raw

Re: [Qemu-devel] [RFC PATCH v2 3/4] kvm: Common device control API functions

Quoted 11 lines · Christoffer Dall

Introduces two simple functions:

    int kvm_device_ioctl(int fd, int type, ...);
    int kvm_create_device(KVMState *s, uint64_t type, bool test);

These functions wrap the basic ioctl-based interactions with KVM in a way similar to other KVM ioctl wrappers.

trace-events migrate_set_state(int new_state) "new state %d" · line 1167

Quoted 5 lines
trace-events +1 -0
@@ -1163,6 +1163,7 @@ migrate_set_state(int new_state) "new state %d"
 kvm_ioctl(int type, void *arg) "type %d, arg %p"
 kvm_vm_ioctl(int type, void *arg) "type %d, arg %p"
 kvm_vcpu_ioctl(int cpu_index, int type, void *arg) "cpu_index %d, type %d, arg %p"
+kvm_device_ioctl(int fd, int type, void *arg) "dev fd %d, type %d, arg %p"
 kvm_run_exit(int cpu_index, uint32_t reason) "cpu_index %d, reason %d"

Following commit 4fe6e9ec this will need rebasing to avoid the conflict, and you should also update the format string in line with that commit so it reads "dev fd %d, type 0x%x, arg %p".
Other than that nit,

-- PMM

#6 lore raw

RFC PATCH v2 4/4 arm: vgic device control api support

In reply to Christoffer Dall

Support creating the ARM vgic device through the device control API and setting the base address for the distributor and cpu interfaces in KVM VMs using this API.

Because the older KVM_CREATE_IRQCHIP interface needs the irq chip to be created prior to creating the VCPUs, we first test if we can use the device control API in kvm_arch_irqchip_create (using the test flag from the device control API). If we cannot, it means we have to fall back to KVM_CREATE_IRQCHIP and use the older ioctl at this point in time. If however, we can use the device control API, we don't do anything and wait until the arm_gic_kvm driver initializes and let that use the device control API.

---
Changelog[v2]:
- Moved dev_fd into GICState
- Proper error handling in kvm_arm_gic_realize
- Coding style and other minor fixes

---
 hw/intc/arm_gic_kvm.c  |   22 +++++++++++++++++--
 hw/intc/gic_internal.h |    1 +
 target-arm/kvm.c       |   55 ++++++++++++++++++++++++++++++++++++++++++------
 target-arm/kvm_arm.h   |   18 ++++++++++------
 4 files changed, 81 insertions(+), 15 deletions(-)
hw/intc/arm_gic_kvm.c +20 -2
index f713975..158f047 100644
@@ -97,6 +97,7 @@ static void kvm_arm_gic_realize(DeviceState *dev, Error **errp)
     GICState *s = KVM_ARM_GIC(dev);
     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
     KVMARMGICClass *kgc = KVM_ARM_GIC_GET_CLASS(s);
+    int ret;
 
     kgc->parent_realize(dev, errp);
     if (error_is_set(errp)) {
@@ -119,13 +120,27 @@ static void kvm_arm_gic_realize(DeviceState *dev, Error **errp)
     for (i = 0; i < s->num_cpu; i++) {
         sysbus_init_irq(sbd, &s->parent_irq[i]);
     }
+
+    /* Try to create the device via the device control API */
+    s->dev_fd = -1;
+    ret = kvm_create_device(kvm_state, KVM_DEV_TYPE_ARM_VGIC_V2, false);
+    if (ret >= 0) {
+        s->dev_fd = ret;
+    } else if (ret != -ENODEV) {
+        error_setg_errno(errp, -ret, "error creating in-kernel VGIC");
+        return;
+    }
+
     /* Distributor */
     memory_region_init_reservation(&s->iomem, OBJECT(s),
                                    "kvm-gic_dist", 0x1000);
     sysbus_init_mmio(sbd, &s->iomem);
     kvm_arm_register_device(&s->iomem,
                             (KVM_ARM_DEVICE_VGIC_V2 << KVM_ARM_DEVICE_ID_SHIFT)
-                            | KVM_VGIC_V2_ADDR_TYPE_DIST);
+                            | KVM_VGIC_V2_ADDR_TYPE_DIST,
+                            KVM_DEV_ARM_VGIC_GRP_ADDR,
+                            KVM_VGIC_V2_ADDR_TYPE_DIST,
+                            s->dev_fd);
     /* CPU interface for current core. Unlike arm_gic, we don't
      * provide the "interface for core #N" memory regions, because
      * cores with a VGIC don't have those.
@@ -135,7 +150,10 @@ static void kvm_arm_gic_realize(DeviceState *dev, Error **errp)
     sysbus_init_mmio(sbd, &s->cpuiomem[0]);
     kvm_arm_register_device(&s->cpuiomem[0],
                             (KVM_ARM_DEVICE_VGIC_V2 << KVM_ARM_DEVICE_ID_SHIFT)
-                            | KVM_VGIC_V2_ADDR_TYPE_CPU);
+                            | KVM_VGIC_V2_ADDR_TYPE_CPU,
+                            KVM_DEV_ARM_VGIC_GRP_ADDR,
+                            KVM_VGIC_V2_ADDR_TYPE_CPU,
+                            s->dev_fd);
 }
 
 static void kvm_arm_gic_class_init(ObjectClass *klass, void *data)
hw/intc/gic_internal.h +1 -0
index 1426437..b3788a8 100644
@@ -99,6 +99,7 @@ typedef struct GICState {
     MemoryRegion cpuiomem[NCPU+1]; /* CPU interfaces */
     uint32_t num_irq;
     uint32_t revision;
+    int dev_fd; /* kvm device fd if backed by kvm vgic support */
 } GICState;
 
 /* The special cases for the revision property: */
target-arm/kvm.c +48 -7

90 lines not on this page · raw message

target-arm/kvm_arm.h +12 -6
index 5d14887..ea1805a 100644
@@ -18,16 +18,22 @@
  * kvm_arm_register_device:
  * @mr: memory region for this device
  * @devid: the KVM device ID
+ * @type: device control API device type
+ * @group: device control API group for setting addresses
+ * @attr: device control API address type
+ * @dev_fd: device control device file descriptor (or -1 if not supported)
  *
  * Remember the memory region @mr, and when it is mapped by the
  * machine model, tell the kernel that base address using the
- * KVM_SET_DEVICE_ADDRESS ioctl. @devid should be the ID of
- * the device as defined by KVM_SET_DEVICE_ADDRESS.
- * The machine model may map and unmap the device multiple times;
- * the kernel will only be told the final address at the point
- * where machine init is complete.
+ * KVM_ARM_SET_DEVICE_ADDRESS ioctl or the newer device control API.  @devid
+ * should be the ID of the device as defined by KVM_ARM_SET_DEVICE_ADDRESS or
+ * the arm-vgic device in the device control API.
+ * The machine model may map
+ * and unmap the device multiple times; the kernel will only be told the final
+ * address at the point where machine init is complete.
  */
-void kvm_arm_register_device(MemoryRegion *mr, uint64_t devid);
+void kvm_arm_register_device(MemoryRegion *mr, uint64_t devid, uint64_t group,
+                             uint64_t attr, int dev_fd);
 
 /**
  * write_list_to_kvmstate:
Signature · 1 line
1.7.10.4
#7 lore raw

Re: [Qemu-devel] [RFC PATCH v2 4/4] arm: vgic device control api support

Quoted 15 lines · Christoffer Dall

Support creating the ARM vgic device through the device control API and setting the base address for the distributor and cpu interfaces in KVM VMs using this API.

Because the older KVM_CREATE_IRQCHIP interface needs the irq chip to be created prior to creating the VCPUs, we first test if we can use the device control API in kvm_arch_irqchip_create (using the test flag from the device control API). If we cannot, it means we have to fall back to KVM_CREATE_IRQCHIP and use the older ioctl at this point in time. If however, we can use the device control API, we don't do anything and wait until the arm_gic_kvm driver initializes and let that use the device control API.

target-arm/kvm.c static MemoryListener devlistener = { · line 244

Quoted 5 lines
target-arm/kvm.c +5 -0
+    if (ret < 0) {
+            fprintf(stderr, "Failed to set device address: %s\n",
+                    strerror(-ret));
+            abort();
+    }

Your indentation here is wrong.

target-arm/kvm_arm.h line 24

Quoted 12 lines
target-arm/kvm_arm.h +4 -0
index 5d14887..ea1805a 100644
@@ -18,16 +18,22 @@
  * kvm_arm_register_device:
  * @mr: memory region for this device
  * @devid: the KVM device ID
+ * @type: device control API device type
+ * @group: device control API group for setting addresses
+ * @attr: device control API address type
+ * @dev_fd: device control device file descriptor (or -1 if not supported)

This adds documentation for four new parameters...

target-arm/kvm_arm.h line 36

Quoted 3 lines
target-arm/kvm_arm.h +2 -1
-void kvm_arm_register_device(MemoryRegion *mr, uint64_t devid);
+void kvm_arm_register_device(MemoryRegion *mr, uint64_t devid, uint64_t group,
+                             uint64_t attr, int dev_fd);

...but the prototype only adds three (no new 'type' param).

Looks good otherwise.

-- PMM