Merge branch 'kbuild' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[deliverable/linux.git] / virt / kvm / arm / vgic / vgic-its.c
index dcae567c522dc86c5c472bd5e639b1a142931c8f..07411cf967b98767bada3c6287fc1cc69c53a86a 100644 (file)
@@ -581,12 +581,73 @@ static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,
        return 0;
 }
 
+/*
+ * Check whether an ID can be stored into the corresponding guest table.
+ * For a direct table this is pretty easy, but gets a bit nasty for
+ * indirect tables. We check whether the resulting guest physical address
+ * is actually valid (covered by a memslot and guest accessbible).
+ * For this we have to read the respective first level entry.
+ */
+static bool vgic_its_check_id(struct vgic_its *its, u64 baser, int id)
+{
+       int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
+       int index;
+       u64 indirect_ptr;
+       gfn_t gfn;
+
+       if (!(baser & GITS_BASER_INDIRECT)) {
+               phys_addr_t addr;
+
+               if (id >= (l1_tbl_size / GITS_BASER_ENTRY_SIZE(baser)))
+                       return false;
+
+               addr = BASER_ADDRESS(baser) + id * GITS_BASER_ENTRY_SIZE(baser);
+               gfn = addr >> PAGE_SHIFT;
+
+               return kvm_is_visible_gfn(its->dev->kvm, gfn);
+       }
+
+       /* calculate and check the index into the 1st level */
+       index = id / (SZ_64K / GITS_BASER_ENTRY_SIZE(baser));
+       if (index >= (l1_tbl_size / sizeof(u64)))
+               return false;
+
+       /* Each 1st level entry is represented by a 64-bit value. */
+       if (kvm_read_guest(its->dev->kvm,
+                          BASER_ADDRESS(baser) + index * sizeof(indirect_ptr),
+                          &indirect_ptr, sizeof(indirect_ptr)))
+               return false;
+
+       indirect_ptr = le64_to_cpu(indirect_ptr);
+
+       /* check the valid bit of the first level entry */
+       if (!(indirect_ptr & BIT_ULL(63)))
+               return false;
+
+       /*
+        * Mask the guest physical address and calculate the frame number.
+        * Any address beyond our supported 48 bits of PA will be caught
+        * by the actual check in the final step.
+        */
+       indirect_ptr &= GENMASK_ULL(51, 16);
+
+       /* Find the address of the actual entry */
+       index = id % (SZ_64K / GITS_BASER_ENTRY_SIZE(baser));
+       indirect_ptr += index * GITS_BASER_ENTRY_SIZE(baser);
+       gfn = indirect_ptr >> PAGE_SHIFT;
+
+       return kvm_is_visible_gfn(its->dev->kvm, gfn);
+}
+
 static int vgic_its_alloc_collection(struct vgic_its *its,
                                     struct its_collection **colp,
                                     u32 coll_id)
 {
        struct its_collection *collection;
 
+       if (!vgic_its_check_id(its, its->baser_coll_table, coll_id))
+               return E_ITS_MAPC_COLLECTION_OOR;
+
        collection = kzalloc(sizeof(*collection), GFP_KERNEL);
 
        collection->collection_id = coll_id;
@@ -627,7 +688,7 @@ static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)
  * Must be called with its_lock mutex held.
  */
 static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
-                                   u64 *its_cmd, u8 subcmd)
+                                   u64 *its_cmd)
 {
        u32 device_id = its_cmd_get_deviceid(its_cmd);
        u32 event_id = its_cmd_get_id(its_cmd);
@@ -636,36 +697,34 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
        struct its_device *device;
        struct its_collection *collection, *new_coll = NULL;
        int lpi_nr;
-       int ret;
 
        device = find_its_device(its, device_id);
        if (!device)
                return E_ITS_MAPTI_UNMAPPED_DEVICE;
 
+       if (its_cmd_get_command(its_cmd) == GITS_CMD_MAPTI)
+               lpi_nr = its_cmd_get_physical_id(its_cmd);
+       else
+               lpi_nr = event_id;
+       if (lpi_nr < GIC_LPI_OFFSET ||
+           lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser))
+               return E_ITS_MAPTI_PHYSICALID_OOR;
+
        collection = find_collection(its, coll_id);
        if (!collection) {
-               ret = vgic_its_alloc_collection(its, &collection, coll_id);
+               int ret = vgic_its_alloc_collection(its, &collection, coll_id);
                if (ret)
                        return ret;
                new_coll = collection;
        }
 
-       if (subcmd == GITS_CMD_MAPTI)
-               lpi_nr = its_cmd_get_physical_id(its_cmd);
-       else
-               lpi_nr = event_id;
-       if (lpi_nr < GIC_LPI_OFFSET ||
-           lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser)) {
-               ret = E_ITS_MAPTI_PHYSICALID_OOR;
-               goto err;
-       }
-
        itte = find_itte(its, device_id, event_id);
        if (!itte) {
                itte = kzalloc(sizeof(struct its_itte), GFP_KERNEL);
                if (!itte) {
-                       ret = -ENOMEM;
-                       goto err;
+                       if (new_coll)
+                               vgic_its_free_collection(its, coll_id);
+                       return -ENOMEM;
                }
 
                itte->event_id  = event_id;
@@ -685,10 +744,6 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
        update_lpi_config(kvm, itte->irq, NULL);
 
        return 0;
-err:
-       if (new_coll)
-               vgic_its_free_collection(its, coll_id);
-       return ret;
 }
 
 /* Requires the its_lock to be held. */
@@ -708,67 +763,6 @@ static void vgic_its_unmap_device(struct kvm *kvm, struct its_device *device)
        kfree(device);
 }
 
-/*
- * Check whether a device ID can be stored into the guest device tables.
- * For a direct table this is pretty easy, but gets a bit nasty for
- * indirect tables. We check whether the resulting guest physical address
- * is actually valid (covered by a memslot and guest accessbible).
- * For this we have to read the respective first level entry.
- */
-static bool vgic_its_check_device_id(struct kvm *kvm, struct vgic_its *its,
-                                    int device_id)
-{
-       u64 r = its->baser_device_table;
-       int l1_tbl_size = GITS_BASER_NR_PAGES(r) * SZ_64K;
-       int index;
-       u64 indirect_ptr;
-       gfn_t gfn;
-
-
-       if (!(r & GITS_BASER_INDIRECT)) {
-               phys_addr_t addr;
-
-               if (device_id >= (l1_tbl_size / GITS_BASER_ENTRY_SIZE(r)))
-                       return false;
-
-               addr = BASER_ADDRESS(r) + device_id * GITS_BASER_ENTRY_SIZE(r);
-               gfn = addr >> PAGE_SHIFT;
-
-               return kvm_is_visible_gfn(kvm, gfn);
-       }
-
-       /* calculate and check the index into the 1st level */
-       index = device_id / (SZ_64K / GITS_BASER_ENTRY_SIZE(r));
-       if (index >= (l1_tbl_size / sizeof(u64)))
-               return false;
-
-       /* Each 1st level entry is represented by a 64-bit value. */
-       if (kvm_read_guest(kvm,
-                          BASER_ADDRESS(r) + index * sizeof(indirect_ptr),
-                          &indirect_ptr, sizeof(indirect_ptr)))
-               return false;
-
-       indirect_ptr = le64_to_cpu(indirect_ptr);
-
-       /* check the valid bit of the first level entry */
-       if (!(indirect_ptr & BIT_ULL(63)))
-               return false;
-
-       /*
-        * Mask the guest physical address and calculate the frame number.
-        * Any address beyond our supported 48 bits of PA will be caught
-        * by the actual check in the final step.
-        */
-       indirect_ptr &= GENMASK_ULL(51, 16);
-
-       /* Find the address of the actual entry */
-       index = device_id % (SZ_64K / GITS_BASER_ENTRY_SIZE(r));
-       indirect_ptr += index * GITS_BASER_ENTRY_SIZE(r);
-       gfn = indirect_ptr >> PAGE_SHIFT;
-
-       return kvm_is_visible_gfn(kvm, gfn);
-}
-
 /*
  * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs).
  * Must be called with the its_lock mutex held.
@@ -780,7 +774,7 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
        bool valid = its_cmd_get_validbit(its_cmd);
        struct its_device *device;
 
-       if (!vgic_its_check_device_id(kvm, its, device_id))
+       if (!vgic_its_check_id(its, its->baser_device_table, device_id))
                return E_ITS_MAPD_DEVICE_OOR;
 
        device = find_its_device(its, device_id);
@@ -812,13 +806,6 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
        return 0;
 }
 
-static int vgic_its_nr_collection_ids(struct vgic_its *its)
-{
-       u64 r = its->baser_coll_table;
-
-       return (GITS_BASER_NR_PAGES(r) * SZ_64K) / GITS_BASER_ENTRY_SIZE(r);
-}
-
 /*
  * The MAPC command maps collection IDs to redistributors.
  * Must be called with the its_lock mutex held.
@@ -838,9 +825,6 @@ static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its,
        if (target_addr >= atomic_read(&kvm->online_vcpus))
                return E_ITS_MAPC_PROCNUM_OOR;
 
-       if (coll_id >= vgic_its_nr_collection_ids(its))
-               return E_ITS_MAPC_COLLECTION_OOR;
-
        if (!valid) {
                vgic_its_free_collection(its, coll_id);
        } else {
@@ -1009,11 +993,10 @@ static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its,
 static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its,
                                   u64 *its_cmd)
 {
-       u8 cmd = its_cmd_get_command(its_cmd);
        int ret = -ENODEV;
 
        mutex_lock(&its->its_lock);
-       switch (cmd) {
+       switch (its_cmd_get_command(its_cmd)) {
        case GITS_CMD_MAPD:
                ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd);
                break;
@@ -1021,10 +1004,10 @@ static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its,
                ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd);
                break;
        case GITS_CMD_MAPI:
-               ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd, cmd);
+               ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
                break;
        case GITS_CMD_MAPTI:
-               ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd, cmd);
+               ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
                break;
        case GITS_CMD_MOVI:
                ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd);
This page took 0.036428 seconds and 5 git commands to generate.