KVM: arm64: vgic-its: Introduce new KVM ITS device
authorAndre Przywara <andre.przywara@arm.com>
Fri, 15 Jul 2016 11:43:31 +0000 (12:43 +0100)
committerMarc Zyngier <marc.zyngier@arm.com>
Mon, 18 Jul 2016 17:14:35 +0000 (18:14 +0100)
Introduce a new KVM device that represents an ARM Interrupt Translation
Service (ITS) controller. Since there can be multiple of this per guest,
we can't piggy back on the existing GICv3 distributor device, but create
a new type of KVM device.
On the KVM_CREATE_DEVICE ioctl we allocate and initialize the ITS data
structure and store the pointer in the kvm_device data.
Upon an explicit init ioctl from userland (after having setup the MMIO
address) we register the handlers with the kvm_io_bus framework.
Any reference to an ITS thus has to go via this interface.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
Reviewed-by: Marc Zyngier <marc.zyngier@arm.com>
Tested-by: Eric Auger <eric.auger@redhat.com>
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
Documentation/virtual/kvm/devices/arm-vgic.txt
arch/arm/kvm/arm.c
arch/arm64/include/uapi/asm/kvm.h
include/kvm/arm_vgic.h
include/uapi/linux/kvm.h
virt/kvm/arm/vgic/vgic-its.c
virt/kvm/arm/vgic/vgic-kvm-device.c
virt/kvm/arm/vgic/vgic-mmio-v3.c
virt/kvm/arm/vgic/vgic.h

index 59541d49e15c019f11e56475832823239b677525..89182f80cc7f21a6329205795d2ceab6cbe01786 100644 (file)
@@ -4,16 +4,22 @@ ARM Virtual Generic Interrupt Controller (VGIC)
 Device types supported:
   KVM_DEV_TYPE_ARM_VGIC_V2     ARM Generic Interrupt Controller v2.0
   KVM_DEV_TYPE_ARM_VGIC_V3     ARM Generic Interrupt Controller v3.0
+  KVM_DEV_TYPE_ARM_VGIC_ITS    ARM Interrupt Translation Service Controller
 
-Only one VGIC instance may be instantiated through either this API or the
-legacy KVM_CREATE_IRQCHIP api.  The created VGIC will act as the VM interrupt
-controller, requiring emulated user-space devices to inject interrupts to the
-VGIC instead of directly to CPUs.
+Only one VGIC instance of the V2/V3 types above may be instantiated through
+either this API or the legacy KVM_CREATE_IRQCHIP api.  The created VGIC will
+act as the VM interrupt controller, requiring emulated user-space devices to
+inject interrupts to the VGIC instead of directly to CPUs.
 
 Creating a guest GICv3 device requires a host GICv3 as well.
 GICv3 implementations with hardware compatibility support allow a guest GICv2
 as well.
 
+Creating a virtual ITS controller requires a host GICv3 (but does not depend
+on having physical ITS controllers).
+There can be multiple ITS controllers per guest, each of them has to have
+a separate, non-overlapping MMIO region.
+
 Groups:
   KVM_DEV_ARM_VGIC_GRP_ADDR
   Attributes:
@@ -39,6 +45,13 @@ Groups:
       Only valid for KVM_DEV_TYPE_ARM_VGIC_V3.
       This address needs to be 64K aligned.
 
+    KVM_VGIC_V3_ADDR_TYPE_ITS (rw, 64-bit)
+      Base address in the guest physical address space of the GICv3 ITS
+      control register frame. The ITS allows MSI(-X) interrupts to be
+      injected into guests. This extension is optional. If the kernel
+      does not support the ITS, the call returns -ENODEV.
+      Only valid for KVM_DEV_TYPE_ARM_VGIC_ITS.
+      This address needs to be 64K aligned and the region covers 128K.
 
   KVM_DEV_ARM_VGIC_GRP_DIST_REGS
   Attributes:
@@ -109,8 +122,8 @@ Groups:
   KVM_DEV_ARM_VGIC_GRP_CTRL
   Attributes:
     KVM_DEV_ARM_VGIC_CTRL_INIT
-      request the initialization of the VGIC, no additional parameter in
-      kvm_device_attr.addr.
+      request the initialization of the VGIC or ITS, no additional parameter
+      in kvm_device_attr.addr.
   Errors:
     -ENXIO: VGIC not properly configured as required prior to calling
      this attribute
index 972075cc111cb8530415682982068b18181ec441..fb4661cf896ecdb5558b5c29fd3d2fe732356178 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/errno.h>
 #include <linux/err.h>
 #include <linux/kvm_host.h>
+#include <linux/list.h>
 #include <linux/module.h>
 #include <linux/vmalloc.h>
 #include <linux/fs.h>
index f209ea151dca8ab8559243dffd8662008844b27c..3051f86a9b5f4ab976568b266b41f47313f86b82 100644 (file)
@@ -87,9 +87,11 @@ struct kvm_regs {
 /* Supported VGICv3 address types  */
 #define KVM_VGIC_V3_ADDR_TYPE_DIST     2
 #define KVM_VGIC_V3_ADDR_TYPE_REDIST   3
+#define KVM_VGIC_ITS_ADDR_TYPE         4
 
 #define KVM_VGIC_V3_DIST_SIZE          SZ_64K
 #define KVM_VGIC_V3_REDIST_SIZE                (2 * SZ_64K)
+#define KVM_VGIC_V3_ITS_SIZE           (2 * SZ_64K)
 
 #define KVM_ARM_VCPU_POWER_OFF         0 /* CPU is started in OFF state */
 #define KVM_ARM_VCPU_EL1_32BIT         1 /* CPU running a 32bit VM */
index 685f33975ce4b3052eaaa1b2c7f9ca74199af3f3..8609faced83e518b3fd5a6a4307248e7d136b461 100644 (file)
@@ -134,6 +134,7 @@ struct vgic_its {
        gpa_t                   vgic_its_base;
 
        bool                    enabled;
+       bool                    initialized;
        struct vgic_io_device   iodev;
 };
 
@@ -167,6 +168,8 @@ struct vgic_dist {
 
        struct vgic_io_device   dist_iodev;
 
+       bool                    has_its;
+
        /*
         * Contains the attributes and gpa of the LPI configuration table.
         * Since we report GICR_TYPER.CommonLPIAff as 0b00, we can share
index 7de96f5bb92c35589583b4dcee8965f29eb2c5bc..d8c4c324cfae533632479324f1656eb2e054f690 100644 (file)
@@ -1077,6 +1077,8 @@ enum kvm_device_type {
 #define KVM_DEV_TYPE_FLIC              KVM_DEV_TYPE_FLIC
        KVM_DEV_TYPE_ARM_VGIC_V3,
 #define KVM_DEV_TYPE_ARM_VGIC_V3       KVM_DEV_TYPE_ARM_VGIC_V3
+       KVM_DEV_TYPE_ARM_VGIC_ITS,
+#define KVM_DEV_TYPE_ARM_VGIC_ITS      KVM_DEV_TYPE_ARM_VGIC_ITS
        KVM_DEV_TYPE_MAX,
 };
 
index 4654d6edf6a6ded36e764743c633d40e26fcb008..6b47b367469027f9d7855c28bd00770877a09460 100644 (file)
@@ -21,6 +21,7 @@
 #include <linux/kvm.h>
 #include <linux/kvm_host.h>
 #include <linux/interrupt.h>
+#include <linux/uaccess.h>
 
 #include <linux/irqchip/arm-gic-v3.h>
 
@@ -84,6 +85,9 @@ static int vgic_its_init_its(struct kvm *kvm, struct vgic_its *its)
        struct vgic_io_device *iodev = &its->iodev;
        int ret;
 
+       if (its->initialized)
+               return 0;
+
        if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base))
                return -ENXIO;
 
@@ -99,5 +103,136 @@ static int vgic_its_init_its(struct kvm *kvm, struct vgic_its *its)
                                      KVM_VGIC_V3_ITS_SIZE, &iodev->dev);
        mutex_unlock(&kvm->slots_lock);
 
+       if (!ret)
+               its->initialized = true;
+
        return ret;
 }
+
+static int vgic_its_create(struct kvm_device *dev, u32 type)
+{
+       struct vgic_its *its;
+
+       if (type != KVM_DEV_TYPE_ARM_VGIC_ITS)
+               return -ENODEV;
+
+       its = kzalloc(sizeof(struct vgic_its), GFP_KERNEL);
+       if (!its)
+               return -ENOMEM;
+
+       its->vgic_its_base = VGIC_ADDR_UNDEF;
+
+       dev->kvm->arch.vgic.has_its = true;
+       its->initialized = false;
+       its->enabled = false;
+
+       dev->private = its;
+
+       return 0;
+}
+
+static void vgic_its_destroy(struct kvm_device *kvm_dev)
+{
+       struct vgic_its *its = kvm_dev->private;
+
+       kfree(its);
+}
+
+static int vgic_its_has_attr(struct kvm_device *dev,
+                            struct kvm_device_attr *attr)
+{
+       switch (attr->group) {
+       case KVM_DEV_ARM_VGIC_GRP_ADDR:
+               switch (attr->attr) {
+               case KVM_VGIC_ITS_ADDR_TYPE:
+                       return 0;
+               }
+               break;
+       case KVM_DEV_ARM_VGIC_GRP_CTRL:
+               switch (attr->attr) {
+               case KVM_DEV_ARM_VGIC_CTRL_INIT:
+                       return 0;
+               }
+               break;
+       }
+       return -ENXIO;
+}
+
+static int vgic_its_set_attr(struct kvm_device *dev,
+                            struct kvm_device_attr *attr)
+{
+       struct vgic_its *its = dev->private;
+       int ret;
+
+       switch (attr->group) {
+       case KVM_DEV_ARM_VGIC_GRP_ADDR: {
+               u64 __user *uaddr = (u64 __user *)(long)attr->addr;
+               unsigned long type = (unsigned long)attr->attr;
+               u64 addr;
+
+               if (type != KVM_VGIC_ITS_ADDR_TYPE)
+                       return -ENODEV;
+
+               if (its->initialized)
+                       return -EBUSY;
+
+               if (copy_from_user(&addr, uaddr, sizeof(addr)))
+                       return -EFAULT;
+
+               ret = vgic_check_ioaddr(dev->kvm, &its->vgic_its_base,
+                                       addr, SZ_64K);
+               if (ret)
+                       return ret;
+
+               its->vgic_its_base = addr;
+
+               return 0;
+       }
+       case KVM_DEV_ARM_VGIC_GRP_CTRL:
+               switch (attr->attr) {
+               case KVM_DEV_ARM_VGIC_CTRL_INIT:
+                       return vgic_its_init_its(dev->kvm, its);
+               }
+               break;
+       }
+       return -ENXIO;
+}
+
+static int vgic_its_get_attr(struct kvm_device *dev,
+                            struct kvm_device_attr *attr)
+{
+       switch (attr->group) {
+       case KVM_DEV_ARM_VGIC_GRP_ADDR: {
+               struct vgic_its *its = dev->private;
+               u64 addr = its->vgic_its_base;
+               u64 __user *uaddr = (u64 __user *)(long)attr->addr;
+               unsigned long type = (unsigned long)attr->attr;
+
+               if (type != KVM_VGIC_ITS_ADDR_TYPE)
+                       return -ENODEV;
+
+               if (copy_to_user(uaddr, &addr, sizeof(addr)))
+                       return -EFAULT;
+               break;
+       default:
+               return -ENXIO;
+       }
+       }
+
+       return 0;
+}
+
+static struct kvm_device_ops kvm_arm_vgic_its_ops = {
+       .name = "kvm-arm-vgic-its",
+       .create = vgic_its_create,
+       .destroy = vgic_its_destroy,
+       .set_attr = vgic_its_set_attr,
+       .get_attr = vgic_its_get_attr,
+       .has_attr = vgic_its_has_attr,
+};
+
+int kvm_vgic_register_its_device(void)
+{
+       return kvm_register_device_ops(&kvm_arm_vgic_its_ops,
+                                      KVM_DEV_TYPE_ARM_VGIC_ITS);
+}
index 2f24f13c6c9040af07a7278fc23a732e6aabf809..561d2ba96a4f2ed3f6a2c83860d76c5732eb1045 100644 (file)
@@ -21,8 +21,8 @@
 
 /* common helpers */
 
-static int vgic_check_ioaddr(struct kvm *kvm, phys_addr_t *ioaddr,
-                            phys_addr_t addr, phys_addr_t alignment)
+int vgic_check_ioaddr(struct kvm *kvm, phys_addr_t *ioaddr,
+                     phys_addr_t addr, phys_addr_t alignment)
 {
        if (addr & ~KVM_PHYS_MASK)
                return -E2BIG;
index b92b7d6cabe6b05db0581edf684f0a178e52dd04..a5c35050c7864d4ba021233c28c644e1b80e0a59 100644 (file)
@@ -49,7 +49,7 @@ bool vgic_has_its(struct kvm *kvm)
        if (dist->vgic_model != KVM_DEV_TYPE_ARM_VGIC_V3)
                return false;
 
-       return false;
+       return dist->has_its;
 }
 
 static unsigned long vgic_mmio_read_v3_misc(struct kvm_vcpu *vcpu,
index 31807c166d2a2f86d164dbd2949bcc01900a63dd..8192a293f119cb49af42e3a2c4e304571d3c0820 100644 (file)
@@ -42,6 +42,9 @@ void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq);
 bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq);
 void vgic_kick_vcpus(struct kvm *kvm);
 
+int vgic_check_ioaddr(struct kvm *kvm, phys_addr_t *ioaddr,
+                     phys_addr_t addr, phys_addr_t alignment);
+
 void vgic_v2_process_maintenance(struct kvm_vcpu *vcpu);
 void vgic_v2_fold_lr_state(struct kvm_vcpu *vcpu);
 void vgic_v2_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr);