]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - virt/kvm/arm/vgic/vgic-its.c
Merge tag 'mac80211-for-davem-2017-05-23' of git://git.kernel.org/pub/scm/linux/kerne...
[karo-tx-linux.git] / virt / kvm / arm / vgic / vgic-its.c
index 90afc838f138a3afb2166fa66ea1b2db41dbaeda..2dff288b3a668e401d924e52cee10b9e85ecd8df 100644 (file)
@@ -23,6 +23,7 @@
 #include <linux/interrupt.h>
 #include <linux/list.h>
 #include <linux/uaccess.h>
+#include <linux/list_sort.h>
 
 #include <linux/irqchip/arm-gic-v3.h>
 
@@ -300,13 +301,13 @@ static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
 }
 
 /*
- * Create a snapshot of the current LPI list, so that we can enumerate all
- * LPIs without holding any lock.
- * Returns the array length and puts the kmalloc'ed array into intid_ptr.
+ * Create a snapshot of the current LPIs targeting @vcpu, so that we can
+ * enumerate those LPIs without holding any lock.
+ * Returns their number and puts the kmalloc'ed array into intid_ptr.
  */
-static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
+static int vgic_copy_lpi_list(struct kvm_vcpu *vcpu, u32 **intid_ptr)
 {
-       struct vgic_dist *dist = &kvm->arch.vgic;
+       struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
        struct vgic_irq *irq;
        u32 *intids;
        int irq_count = dist->lpi_list_count, i = 0;
@@ -325,14 +326,14 @@ static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
        spin_lock(&dist->lpi_list_lock);
        list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
                /* We don't need to "get" the IRQ, as we hold the list lock. */
-               intids[i] = irq->intid;
-               if (++i == irq_count)
-                       break;
+               if (irq->target_vcpu != vcpu)
+                       continue;
+               intids[i++] = irq->intid;
        }
        spin_unlock(&dist->lpi_list_lock);
 
        *intid_ptr = intids;
-       return irq_count;
+       return i;
 }
 
 /*
@@ -381,7 +382,7 @@ static u32 max_lpis_propbaser(u64 propbaser)
 }
 
 /*
- * Scan the whole LPI pending table and sync the pending bit in there
+ * Sync the pending table pending bit of LPIs targeting @vcpu
  * with our own data structures. This relies on the LPI being
  * mapped before.
  */
@@ -394,7 +395,7 @@ static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
        u32 *intids;
        int nr_irqs, i;
 
-       nr_irqs = vgic_copy_lpi_list(vcpu->kvm, &intids);
+       nr_irqs = vgic_copy_lpi_list(vcpu, &intids);
        if (nr_irqs < 0)
                return nr_irqs;
 
@@ -1080,7 +1081,7 @@ static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its,
 
        vcpu = kvm_get_vcpu(kvm, collection->target_addr);
 
-       irq_count = vgic_copy_lpi_list(kvm, &intids);
+       irq_count = vgic_copy_lpi_list(vcpu, &intids);
        if (irq_count < 0)
                return irq_count;
 
@@ -1539,17 +1540,19 @@ void vgic_enable_lpis(struct kvm_vcpu *vcpu)
                its_sync_lpi_pending_table(vcpu);
 }
 
-static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its)
+static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its,
+                                  u64 addr)
 {
        struct vgic_io_device *iodev = &its->iodev;
        int ret;
 
-       if (!its->initialized)
-               return -EBUSY;
-
-       if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base))
-               return -ENXIO;
+       mutex_lock(&kvm->slots_lock);
+       if (!IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) {
+               ret = -EBUSY;
+               goto out;
+       }
 
+       its->vgic_its_base = addr;
        iodev->regions = its_registers;
        iodev->nr_regions = ARRAY_SIZE(its_registers);
        kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops);
@@ -1557,9 +1560,9 @@ static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its)
        iodev->base_addr = its->vgic_its_base;
        iodev->iodev_type = IODEV_ITS;
        iodev->its = its;
-       mutex_lock(&kvm->slots_lock);
        ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr,
                                      KVM_VGIC_V3_ITS_SIZE, &iodev->dev);
+out:
        mutex_unlock(&kvm->slots_lock);
 
        return ret;
@@ -1596,7 +1599,6 @@ static int vgic_its_create(struct kvm_device *dev, u32 type)
        INIT_LIST_HEAD(&its->collection_list);
 
        dev->kvm->arch.vgic.has_its = true;
-       its->initialized = false;
        its->enabled = false;
        its->dev = dev;
 
@@ -1611,13 +1613,20 @@ static int vgic_its_create(struct kvm_device *dev, u32 type)
        return vgic_its_set_abi(its, NR_ITS_ABIS - 1);
 }
 
+static void vgic_its_free_device(struct kvm *kvm, struct its_device *dev)
+{
+       struct its_ite *ite, *tmp;
+
+       list_for_each_entry_safe(ite, tmp, &dev->itt_head, ite_list)
+               its_free_ite(kvm, ite);
+       list_del(&dev->dev_list);
+       kfree(dev);
+}
+
 static void vgic_its_destroy(struct kvm_device *kvm_dev)
 {
        struct kvm *kvm = kvm_dev->kvm;
        struct vgic_its *its = kvm_dev->private;
-       struct its_device *dev;
-       struct its_ite *ite;
-       struct list_head *dev_cur, *dev_temp;
        struct list_head *cur, *temp;
 
        /*
@@ -1628,19 +1637,19 @@ static void vgic_its_destroy(struct kvm_device *kvm_dev)
                return;
 
        mutex_lock(&its->its_lock);
-       list_for_each_safe(dev_cur, dev_temp, &its->device_list) {
-               dev = container_of(dev_cur, struct its_device, dev_list);
-               list_for_each_safe(cur, temp, &dev->itt_head) {
-                       ite = (container_of(cur, struct its_ite, ite_list));
-                       its_free_ite(kvm, ite);
-               }
-               list_del(dev_cur);
-               kfree(dev);
+       list_for_each_safe(cur, temp, &its->device_list) {
+               struct its_device *dev;
+
+               dev = list_entry(cur, struct its_device, dev_list);
+               vgic_its_free_device(kvm, dev);
        }
 
        list_for_each_safe(cur, temp, &its->collection_list) {
+               struct its_collection *coll;
+
+               coll = list_entry(cur, struct its_collection, coll_list);
                list_del(cur);
-               kfree(container_of(cur, struct its_collection, coll_list));
+               kfree(coll);
        }
        mutex_unlock(&its->its_lock);
 
@@ -1735,7 +1744,8 @@ out:
        return ret;
 }
 
-u32 compute_next_devid_offset(struct list_head *h, struct its_device *dev)
+static u32 compute_next_devid_offset(struct list_head *h,
+                                    struct its_device *dev)
 {
        struct its_device *next;
        u32 next_offset;
@@ -1748,7 +1758,7 @@ u32 compute_next_devid_offset(struct list_head *h, struct its_device *dev)
        return min_t(u32, next_offset, VITS_DTE_MAX_DEVID_OFFSET);
 }
 
-u32 compute_next_eventid_offset(struct list_head *h, struct its_ite *ite)
+static u32 compute_next_eventid_offset(struct list_head *h, struct its_ite *ite)
 {
        struct its_ite *next;
        u32 next_offset;
@@ -1789,8 +1799,8 @@ typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry,
  * Return: < 0 on error, 0 if last element was identified, 1 otherwise
  * (the last element may not be found on second level tables)
  */
-int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz,
-                  int start_id, entry_fn_t fn, void *opaque)
+static int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz,
+                         int start_id, entry_fn_t fn, void *opaque)
 {
        void *entry = kzalloc(esz, GFP_KERNEL);
        struct kvm *kvm = its->dev->kvm;
@@ -1825,13 +1835,283 @@ out:
        return ret;
 }
 
+/**
+ * vgic_its_save_ite - Save an interrupt translation entry at @gpa
+ */
+static int vgic_its_save_ite(struct vgic_its *its, struct its_device *dev,
+                             struct its_ite *ite, gpa_t gpa, int ite_esz)
+{
+       struct kvm *kvm = its->dev->kvm;
+       u32 next_offset;
+       u64 val;
+
+       next_offset = compute_next_eventid_offset(&dev->itt_head, ite);
+       val = ((u64)next_offset << KVM_ITS_ITE_NEXT_SHIFT) |
+              ((u64)ite->lpi << KVM_ITS_ITE_PINTID_SHIFT) |
+               ite->collection->collection_id;
+       val = cpu_to_le64(val);
+       return kvm_write_guest(kvm, gpa, &val, ite_esz);
+}
+
+/**
+ * vgic_its_restore_ite - restore an interrupt translation entry
+ * @event_id: id used for indexing
+ * @ptr: pointer to the ITE entry
+ * @opaque: pointer to the its_device
+ */
+static int vgic_its_restore_ite(struct vgic_its *its, u32 event_id,
+                               void *ptr, void *opaque)
+{
+       struct its_device *dev = (struct its_device *)opaque;
+       struct its_collection *collection;
+       struct kvm *kvm = its->dev->kvm;
+       struct kvm_vcpu *vcpu = NULL;
+       u64 val;
+       u64 *p = (u64 *)ptr;
+       struct vgic_irq *irq;
+       u32 coll_id, lpi_id;
+       struct its_ite *ite;
+       u32 offset;
+
+       val = *p;
+
+       val = le64_to_cpu(val);
+
+       coll_id = val & KVM_ITS_ITE_ICID_MASK;
+       lpi_id = (val & KVM_ITS_ITE_PINTID_MASK) >> KVM_ITS_ITE_PINTID_SHIFT;
+
+       if (!lpi_id)
+               return 1; /* invalid entry, no choice but to scan next entry */
+
+       if (lpi_id < VGIC_MIN_LPI)
+               return -EINVAL;
+
+       offset = val >> KVM_ITS_ITE_NEXT_SHIFT;
+       if (event_id + offset >= BIT_ULL(dev->num_eventid_bits))
+               return -EINVAL;
+
+       collection = find_collection(its, coll_id);
+       if (!collection)
+               return -EINVAL;
+
+       ite = vgic_its_alloc_ite(dev, collection, lpi_id, event_id);
+       if (IS_ERR(ite))
+               return PTR_ERR(ite);
+
+       if (its_is_collection_mapped(collection))
+               vcpu = kvm_get_vcpu(kvm, collection->target_addr);
+
+       irq = vgic_add_lpi(kvm, lpi_id, vcpu);
+       if (IS_ERR(irq))
+               return PTR_ERR(irq);
+       ite->irq = irq;
+
+       return offset;
+}
+
+static int vgic_its_ite_cmp(void *priv, struct list_head *a,
+                           struct list_head *b)
+{
+       struct its_ite *itea = container_of(a, struct its_ite, ite_list);
+       struct its_ite *iteb = container_of(b, struct its_ite, ite_list);
+
+       if (itea->event_id < iteb->event_id)
+               return -1;
+       else
+               return 1;
+}
+
+static int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
+{
+       const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+       gpa_t base = device->itt_addr;
+       struct its_ite *ite;
+       int ret;
+       int ite_esz = abi->ite_esz;
+
+       list_sort(NULL, &device->itt_head, vgic_its_ite_cmp);
+
+       list_for_each_entry(ite, &device->itt_head, ite_list) {
+               gpa_t gpa = base + ite->event_id * ite_esz;
+
+               ret = vgic_its_save_ite(its, device, ite, gpa, ite_esz);
+               if (ret)
+                       return ret;
+       }
+       return 0;
+}
+
+static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
+{
+       const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+       gpa_t base = dev->itt_addr;
+       int ret;
+       int ite_esz = abi->ite_esz;
+       size_t max_size = BIT_ULL(dev->num_eventid_bits) * ite_esz;
+
+       ret = scan_its_table(its, base, max_size, ite_esz, 0,
+                            vgic_its_restore_ite, dev);
+
+       return ret;
+}
+
+/**
+ * vgic_its_save_dte - Save a device table entry at a given GPA
+ *
+ * @its: ITS handle
+ * @dev: ITS device
+ * @ptr: GPA
+ */
+static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev,
+                            gpa_t ptr, int dte_esz)
+{
+       struct kvm *kvm = its->dev->kvm;
+       u64 val, itt_addr_field;
+       u32 next_offset;
+
+       itt_addr_field = dev->itt_addr >> 8;
+       next_offset = compute_next_devid_offset(&its->device_list, dev);
+       val = (1ULL << KVM_ITS_DTE_VALID_SHIFT |
+              ((u64)next_offset << KVM_ITS_DTE_NEXT_SHIFT) |
+              (itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) |
+               (dev->num_eventid_bits - 1));
+       val = cpu_to_le64(val);
+       return kvm_write_guest(kvm, ptr, &val, dte_esz);
+}
+
+/**
+ * vgic_its_restore_dte - restore a device table entry
+ *
+ * @its: its handle
+ * @id: device id the DTE corresponds to
+ * @ptr: kernel VA where the 8 byte DTE is located
+ * @opaque: unused
+ *
+ * Return: < 0 on error, 0 if the dte is the last one, id offset to the
+ * next dte otherwise
+ */
+static int vgic_its_restore_dte(struct vgic_its *its, u32 id,
+                               void *ptr, void *opaque)
+{
+       struct its_device *dev;
+       gpa_t itt_addr;
+       u8 num_eventid_bits;
+       u64 entry = *(u64 *)ptr;
+       bool valid;
+       u32 offset;
+       int ret;
+
+       entry = le64_to_cpu(entry);
+
+       valid = entry >> KVM_ITS_DTE_VALID_SHIFT;
+       num_eventid_bits = (entry & KVM_ITS_DTE_SIZE_MASK) + 1;
+       itt_addr = ((entry & KVM_ITS_DTE_ITTADDR_MASK)
+                       >> KVM_ITS_DTE_ITTADDR_SHIFT) << 8;
+
+       if (!valid)
+               return 1;
+
+       /* dte entry is valid */
+       offset = (entry & KVM_ITS_DTE_NEXT_MASK) >> KVM_ITS_DTE_NEXT_SHIFT;
+
+       dev = vgic_its_alloc_device(its, id, itt_addr, num_eventid_bits);
+       if (IS_ERR(dev))
+               return PTR_ERR(dev);
+
+       ret = vgic_its_restore_itt(its, dev);
+       if (ret) {
+               vgic_its_free_device(its->dev->kvm, dev);
+               return ret;
+       }
+
+       return offset;
+}
+
+static int vgic_its_device_cmp(void *priv, struct list_head *a,
+                              struct list_head *b)
+{
+       struct its_device *deva = container_of(a, struct its_device, dev_list);
+       struct its_device *devb = container_of(b, struct its_device, dev_list);
+
+       if (deva->device_id < devb->device_id)
+               return -1;
+       else
+               return 1;
+}
+
 /**
  * vgic_its_save_device_tables - Save the device table and all ITT
  * into guest RAM
+ *
+ * L1/L2 handling is hidden by vgic_its_check_id() helper which directly
+ * returns the GPA of the device entry
  */
 static int vgic_its_save_device_tables(struct vgic_its *its)
 {
-       return -ENXIO;
+       const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+       struct its_device *dev;
+       int dte_esz = abi->dte_esz;
+       u64 baser;
+
+       baser = its->baser_device_table;
+
+       list_sort(NULL, &its->device_list, vgic_its_device_cmp);
+
+       list_for_each_entry(dev, &its->device_list, dev_list) {
+               int ret;
+               gpa_t eaddr;
+
+               if (!vgic_its_check_id(its, baser,
+                                      dev->device_id, &eaddr))
+                       return -EINVAL;
+
+               ret = vgic_its_save_itt(its, dev);
+               if (ret)
+                       return ret;
+
+               ret = vgic_its_save_dte(its, dev, eaddr, dte_esz);
+               if (ret)
+                       return ret;
+       }
+       return 0;
+}
+
+/**
+ * handle_l1_dte - callback used for L1 device table entries (2 stage case)
+ *
+ * @its: its handle
+ * @id: index of the entry in the L1 table
+ * @addr: kernel VA
+ * @opaque: unused
+ *
+ * L1 table entries are scanned by steps of 1 entry
+ * Return < 0 if error, 0 if last dte was found when scanning the L2
+ * table, +1 otherwise (meaning next L1 entry must be scanned)
+ */
+static int handle_l1_dte(struct vgic_its *its, u32 id, void *addr,
+                        void *opaque)
+{
+       const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+       int l2_start_id = id * (SZ_64K / abi->dte_esz);
+       u64 entry = *(u64 *)addr;
+       int dte_esz = abi->dte_esz;
+       gpa_t gpa;
+       int ret;
+
+       entry = le64_to_cpu(entry);
+
+       if (!(entry & KVM_ITS_L1E_VALID_MASK))
+               return 1;
+
+       gpa = entry & KVM_ITS_L1E_ADDR_MASK;
+
+       ret = scan_its_table(its, gpa, SZ_64K, dte_esz,
+                            l2_start_id, vgic_its_restore_dte, NULL);
+
+       if (ret <= 0)
+               return ret;
+
+       return 1;
 }
 
 /**
@@ -1840,7 +2120,31 @@ static int vgic_its_save_device_tables(struct vgic_its *its)
  */
 static int vgic_its_restore_device_tables(struct vgic_its *its)
 {
-       return -ENXIO;
+       const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+       u64 baser = its->baser_device_table;
+       int l1_esz, ret;
+       int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
+       gpa_t l1_gpa;
+
+       if (!(baser & GITS_BASER_VALID))
+               return 0;
+
+       l1_gpa = BASER_ADDRESS(baser);
+
+       if (baser & GITS_BASER_INDIRECT) {
+               l1_esz = GITS_LVL1_ENTRY_SIZE;
+               ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
+                                    handle_l1_dte, NULL);
+       } else {
+               l1_esz = abi->dte_esz;
+               ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
+                                    vgic_its_restore_dte, NULL);
+       }
+
+       if (ret > 0)
+               ret = -EINVAL;
+
+       return ret;
 }
 
 static int vgic_its_save_cte(struct vgic_its *its,
@@ -2013,20 +2317,12 @@ static int vgic_its_restore_tables_v0(struct vgic_its *its)
                goto out;
 
        ret = vgic_its_restore_device_tables(its);
-
 out:
        unlock_all_vcpus(kvm);
        mutex_unlock(&its->its_lock);
        mutex_unlock(&kvm->lock);
 
-       if (ret)
-               return ret;
-
-       /*
-        * On restore path, MSI injections can happen before the
-        * first VCPU run so let's complete the GIC init here.
-        */
-       return kvm_vgic_map_resources(its->dev->kvm);
+       return ret;
 }
 
 static int vgic_its_commit_v0(struct vgic_its *its)
@@ -2094,17 +2390,14 @@ static int vgic_its_set_attr(struct kvm_device *dev,
                if (ret)
                        return ret;
 
-               its->vgic_its_base = addr;
-
-               return 0;
+               return vgic_register_its_iodev(dev->kvm, its, addr);
        }
        case KVM_DEV_ARM_VGIC_GRP_CTRL: {
                const struct vgic_its_abi *abi = vgic_its_get_abi(its);
 
                switch (attr->attr) {
                case KVM_DEV_ARM_VGIC_CTRL_INIT:
-                       its->initialized = true;
-
+                       /* Nothing to do */
                        return 0;
                case KVM_DEV_ARM_ITS_SAVE_TABLES:
                        return abi->save_tables(its);
@@ -2173,30 +2466,3 @@ int kvm_vgic_register_its_device(void)
        return kvm_register_device_ops(&kvm_arm_vgic_its_ops,
                                       KVM_DEV_TYPE_ARM_VGIC_ITS);
 }
-
-/*
- * Registers all ITSes with the kvm_io_bus framework.
- * To follow the existing VGIC initialization sequence, this has to be
- * done as late as possible, just before the first VCPU runs.
- */
-int vgic_register_its_iodevs(struct kvm *kvm)
-{
-       struct kvm_device *dev;
-       int ret = 0;
-
-       list_for_each_entry(dev, &kvm->devices, vm_node) {
-               if (dev->ops != &kvm_arm_vgic_its_ops)
-                       continue;
-
-               ret = vgic_register_its_iodev(kvm, dev->private);
-               if (ret)
-                       return ret;
-               /*
-                * We don't need to care about tearing down previously
-                * registered ITSes, as the kvm_io_bus framework removes
-                * them for us if the VM gets destroyed.
-                */
-       }
-
-       return ret;
-}