]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - virt/kvm/arm/vgic/vgic-its.c
KVM: arm64: vgic-its: Implement vgic_mmio_uaccess_write_its_iidr
[karo-tx-linux.git] / virt / kvm / arm / vgic / vgic-its.c
index 8d1da1af4b09e47c174cf7151b37b98666f03953..9338efb79a547a8dbb1d582a2654ebf1967c5197 100644 (file)
 #include "vgic.h"
 #include "vgic-mmio.h"
 
+static int vgic_its_save_tables_v0(struct vgic_its *its);
+static int vgic_its_restore_tables_v0(struct vgic_its *its);
+static int vgic_its_commit_v0(struct vgic_its *its);
+
 /*
  * Creates a new (reference to a) struct vgic_irq for a given LPI.
  * If this LPI is already mapped on another ITS, we increase its refcount
@@ -114,8 +118,8 @@ struct its_collection {
 #define its_is_collection_mapped(coll) ((coll) && \
                                ((coll)->target_addr != COLLECTION_NOT_MAPPED))
 
-struct its_itte {
-       struct list_head itte_list;
+struct its_ite {
+       struct list_head ite_list;
 
        struct vgic_irq *irq;
        struct its_collection *collection;
@@ -123,6 +127,50 @@ struct its_itte {
        u32 event_id;
 };
 
+/**
+ * struct vgic_its_abi - ITS abi ops and settings
+ * @cte_esz: collection table entry size
+ * @dte_esz: device table entry size
+ * @ite_esz: interrupt translation table entry size
+ * @save tables: save the ITS tables into guest RAM
+ * @restore_tables: restore the ITS internal structs from tables
+ *  stored in guest RAM
+ * @commit: initialize the registers which expose the ABI settings,
+ *  especially the entry sizes
+ */
+struct vgic_its_abi {
+       int cte_esz;
+       int dte_esz;
+       int ite_esz;
+       int (*save_tables)(struct vgic_its *its);
+       int (*restore_tables)(struct vgic_its *its);
+       int (*commit)(struct vgic_its *its);
+};
+
+static const struct vgic_its_abi its_table_abi_versions[] = {
+       [0] = {.cte_esz = 8, .dte_esz = 8, .ite_esz = 8,
+        .save_tables = vgic_its_save_tables_v0,
+        .restore_tables = vgic_its_restore_tables_v0,
+        .commit = vgic_its_commit_v0,
+       },
+};
+
+#define NR_ITS_ABIS    ARRAY_SIZE(its_table_abi_versions)
+
+inline const struct vgic_its_abi *vgic_its_get_abi(struct vgic_its *its)
+{
+       return &its_table_abi_versions[its->abi_rev];
+}
+
+int vgic_its_set_abi(struct vgic_its *its, int rev)
+{
+       const struct vgic_its_abi *abi;
+
+       its->abi_rev = rev;
+       abi = vgic_its_get_abi(its);
+       return abi->commit(its);
+}
+
 /*
  * Find and returns a device in the device table for an ITS.
  * Must be called with the its_lock mutex held.
@@ -143,27 +191,27 @@ static struct its_device *find_its_device(struct vgic_its *its, u32 device_id)
  * Device ID/Event ID pair on an ITS.
  * Must be called with the its_lock mutex held.
  */
-static struct its_itte *find_itte(struct vgic_its *its, u32 device_id,
+static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,
                                  u32 event_id)
 {
        struct its_device *device;
-       struct its_itte *itte;
+       struct its_ite *ite;
 
        device = find_its_device(its, device_id);
        if (device == NULL)
                return NULL;
 
-       list_for_each_entry(itte, &device->itt_head, itte_list)
-               if (itte->event_id == event_id)
-                       return itte;
+       list_for_each_entry(ite, &device->itt_head, ite_list)
+               if (ite->event_id == event_id)
+                       return ite;
 
        return NULL;
 }
 
 /* To be used as an iterator this macro misses the enclosing parentheses */
-#define for_each_lpi_its(dev, itte, its) \
+#define for_each_lpi_its(dev, ite, its) \
        list_for_each_entry(dev, &(its)->device_list, dev_list) \
-               list_for_each_entry(itte, &(dev)->itt_head, itte_list)
+               list_for_each_entry(ite, &(dev)->itt_head, ite_list)
 
 /*
  * We only implement 48 bits of PA at the moment, although the ITS
@@ -270,18 +318,18 @@ static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
  * Needs to be called whenever either the collection for a LPIs has
  * changed or the collection itself got retargeted.
  */
-static void update_affinity_itte(struct kvm *kvm, struct its_itte *itte)
+static void update_affinity_ite(struct kvm *kvm, struct its_ite *ite)
 {
        struct kvm_vcpu *vcpu;
 
-       if (!its_is_collection_mapped(itte->collection))
+       if (!its_is_collection_mapped(ite->collection))
                return;
 
-       vcpu = kvm_get_vcpu(kvm, itte->collection->target_addr);
+       vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr);
 
-       spin_lock(&itte->irq->irq_lock);
-       itte->irq->target_vcpu = vcpu;
-       spin_unlock(&itte->irq->irq_lock);
+       spin_lock(&ite->irq->irq_lock);
+       ite->irq->target_vcpu = vcpu;
+       spin_unlock(&ite->irq->irq_lock);
 }
 
 /*
@@ -292,13 +340,13 @@ static void update_affinity_collection(struct kvm *kvm, struct vgic_its *its,
                                       struct its_collection *coll)
 {
        struct its_device *device;
-       struct its_itte *itte;
+       struct its_ite *ite;
 
-       for_each_lpi_its(device, itte, its) {
-               if (!itte->collection || coll != itte->collection)
+       for_each_lpi_its(device, ite, its) {
+               if (!ite->collection || coll != ite->collection)
                        continue;
 
-               update_affinity_itte(kvm, itte);
+               update_affinity_ite(kvm, ite);
        }
 }
 
@@ -364,6 +412,7 @@ static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,
                                              struct vgic_its *its,
                                              gpa_t addr, unsigned int len)
 {
+       const struct vgic_its_abi *abi = vgic_its_get_abi(its);
        u64 reg = GITS_TYPER_PLPIS;
 
        /*
@@ -376,6 +425,7 @@ static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,
         */
        reg |= 0x0f << GITS_TYPER_DEVBITS_SHIFT;
        reg |= 0x0f << GITS_TYPER_IDBITS_SHIFT;
+       reg |= GIC_ENCODE_SZ(abi->ite_esz, 4) << GITS_TYPER_ITT_ENTRY_SIZE_SHIFT;
 
        return extract_bytes(reg, addr & 7, len);
 }
@@ -384,7 +434,23 @@ static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm,
                                             struct vgic_its *its,
                                             gpa_t addr, unsigned int len)
 {
-       return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
+       u32 val;
+
+       val = (its->abi_rev << GITS_IIDR_REV_SHIFT) & GITS_IIDR_REV_MASK;
+       val |= (PRODUCT_ID_KVM << GITS_IIDR_PRODUCTID_SHIFT) | IMPLEMENTER_ARM;
+       return val;
+}
+
+static int vgic_mmio_uaccess_write_its_iidr(struct kvm *kvm,
+                                           struct vgic_its *its,
+                                           gpa_t addr, unsigned int len,
+                                           unsigned long val)
+{
+       u32 rev = GITS_IIDR_REV(val);
+
+       if (rev >= NR_ITS_ABIS)
+               return -EINVAL;
+       return vgic_its_set_abi(its, rev);
 }
 
 static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm,
@@ -425,25 +491,25 @@ static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its,
                                u32 devid, u32 eventid)
 {
        struct kvm_vcpu *vcpu;
-       struct its_itte *itte;
+       struct its_ite *ite;
 
        if (!its->enabled)
                return -EBUSY;
 
-       itte = find_itte(its, devid, eventid);
-       if (!itte || !its_is_collection_mapped(itte->collection))
+       ite = find_ite(its, devid, eventid);
+       if (!ite || !its_is_collection_mapped(ite->collection))
                return E_ITS_INT_UNMAPPED_INTERRUPT;
 
-       vcpu = kvm_get_vcpu(kvm, itte->collection->target_addr);
+       vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr);
        if (!vcpu)
                return E_ITS_INT_UNMAPPED_INTERRUPT;
 
        if (!vcpu->arch.vgic_cpu.lpis_enabled)
                return -EBUSY;
 
-       spin_lock(&itte->irq->irq_lock);
-       itte->irq->pending_latch = true;
-       vgic_queue_irq_unlock(kvm, itte->irq);
+       spin_lock(&ite->irq->irq_lock);
+       ite->irq->pending_latch = true;
+       vgic_queue_irq_unlock(kvm, ite->irq);
 
        return 0;
 }
@@ -511,15 +577,15 @@ int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi)
 }
 
 /* Requires the its_lock to be held. */
-static void its_free_itte(struct kvm *kvm, struct its_itte *itte)
+static void its_free_ite(struct kvm *kvm, struct its_ite *ite)
 {
-       list_del(&itte->itte_list);
+       list_del(&ite->ite_list);
 
        /* This put matches the get in vgic_add_lpi. */
-       if (itte->irq)
-               vgic_put_irq(kvm, itte->irq);
+       if (ite->irq)
+               vgic_put_irq(kvm, ite->irq);
 
-       kfree(itte);
+       kfree(ite);
 }
 
 static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size)
@@ -544,17 +610,17 @@ static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its,
 {
        u32 device_id = its_cmd_get_deviceid(its_cmd);
        u32 event_id = its_cmd_get_id(its_cmd);
-       struct its_itte *itte;
+       struct its_ite *ite;
 
 
-       itte = find_itte(its, device_id, event_id);
-       if (itte && itte->collection) {
+       ite = find_ite(its, device_id, event_id);
+       if (ite && ite->collection) {
                /*
                 * Though the spec talks about removing the pending state, we
                 * don't bother here since we clear the ITTE anyway and the
                 * pending state is a property of the ITTE struct.
                 */
-               its_free_itte(kvm, itte);
+               its_free_ite(kvm, ite);
                return 0;
        }
 
@@ -572,26 +638,26 @@ static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,
        u32 event_id = its_cmd_get_id(its_cmd);
        u32 coll_id = its_cmd_get_collection(its_cmd);
        struct kvm_vcpu *vcpu;
-       struct its_itte *itte;
+       struct its_ite *ite;
        struct its_collection *collection;
 
-       itte = find_itte(its, device_id, event_id);
-       if (!itte)
+       ite = find_ite(its, device_id, event_id);
+       if (!ite)
                return E_ITS_MOVI_UNMAPPED_INTERRUPT;
 
-       if (!its_is_collection_mapped(itte->collection))
+       if (!its_is_collection_mapped(ite->collection))
                return E_ITS_MOVI_UNMAPPED_COLLECTION;
 
        collection = find_collection(its, coll_id);
        if (!its_is_collection_mapped(collection))
                return E_ITS_MOVI_UNMAPPED_COLLECTION;
 
-       itte->collection = collection;
+       ite->collection = collection;
        vcpu = kvm_get_vcpu(kvm, collection->target_addr);
 
-       spin_lock(&itte->irq->irq_lock);
-       itte->irq->target_vcpu = vcpu;
-       spin_unlock(&itte->irq->irq_lock);
+       spin_lock(&ite->irq->irq_lock);
+       ite->irq->target_vcpu = vcpu;
+       spin_unlock(&ite->irq->irq_lock);
 
        return 0;
 }
@@ -679,7 +745,7 @@ static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)
 {
        struct its_collection *collection;
        struct its_device *device;
-       struct its_itte *itte;
+       struct its_ite *ite;
 
        /*
         * Clearing the mapping for that collection ID removes the
@@ -690,10 +756,10 @@ static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)
        if (!collection)
                return;
 
-       for_each_lpi_its(device, itte, its)
-               if (itte->collection &&
-                   itte->collection->collection_id == coll_id)
-                       itte->collection = NULL;
+       for_each_lpi_its(device, ite, its)
+               if (ite->collection &&
+                   ite->collection->collection_id == coll_id)
+                       ite->collection = NULL;
 
        list_del(&collection->coll_list);
        kfree(collection);
@@ -709,7 +775,7 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
        u32 device_id = its_cmd_get_deviceid(its_cmd);
        u32 event_id = its_cmd_get_id(its_cmd);
        u32 coll_id = its_cmd_get_collection(its_cmd);
-       struct its_itte *itte;
+       struct its_ite *ite;
        struct its_device *device;
        struct its_collection *collection, *new_coll = NULL;
        int lpi_nr;
@@ -728,7 +794,7 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
                return E_ITS_MAPTI_PHYSICALID_OOR;
 
        /* If there is an existing mapping, behavior is UNPREDICTABLE. */
-       if (find_itte(its, device_id, event_id))
+       if (find_ite(its, device_id, event_id))
                return 0;
 
        collection = find_collection(its, coll_id);
@@ -739,36 +805,36 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
                new_coll = collection;
        }
 
-       itte = kzalloc(sizeof(struct its_itte), GFP_KERNEL);
-       if (!itte) {
+       ite = kzalloc(sizeof(struct its_ite), GFP_KERNEL);
+       if (!ite) {
                if (new_coll)
                        vgic_its_free_collection(its, coll_id);
                return -ENOMEM;
        }
 
-       itte->event_id  = event_id;
-       list_add_tail(&itte->itte_list, &device->itt_head);
+       ite->event_id   = event_id;
+       list_add_tail(&ite->ite_list, &device->itt_head);
 
-       itte->collection = collection;
-       itte->lpi = lpi_nr;
+       ite->collection = collection;
+       ite->lpi = lpi_nr;
 
        irq = vgic_add_lpi(kvm, lpi_nr);
        if (IS_ERR(irq)) {
                if (new_coll)
                        vgic_its_free_collection(its, coll_id);
-               its_free_itte(kvm, itte);
+               its_free_ite(kvm, ite);
                return PTR_ERR(irq);
        }
-       itte->irq = irq;
+       ite->irq = irq;
 
-       update_affinity_itte(kvm, itte);
+       update_affinity_ite(kvm, ite);
 
        /*
         * We "cache" the configuration table entries in out struct vgic_irq's.
         * However we only have those structs for mapped IRQs, so we read in
         * the respective config data from memory here upon mapping the LPI.
         */
-       update_lpi_config(kvm, itte->irq, NULL);
+       update_lpi_config(kvm, ite->irq, NULL);
 
        return 0;
 }
@@ -776,15 +842,15 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
 /* Requires the its_lock to be held. */
 static void vgic_its_unmap_device(struct kvm *kvm, struct its_device *device)
 {
-       struct its_itte *itte, *temp;
+       struct its_ite *ite, *temp;
 
        /*
         * The spec says that unmapping a device with still valid
         * ITTEs associated is UNPREDICTABLE. We remove all ITTEs,
         * since we cannot leave the memory unreferenced.
         */
-       list_for_each_entry_safe(itte, temp, &device->itt_head, itte_list)
-               its_free_itte(kvm, itte);
+       list_for_each_entry_safe(ite, temp, &device->itt_head, ite_list)
+               its_free_ite(kvm, ite);
 
        list_del(&device->dev_list);
        kfree(device);
@@ -883,14 +949,14 @@ static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its,
 {
        u32 device_id = its_cmd_get_deviceid(its_cmd);
        u32 event_id = its_cmd_get_id(its_cmd);
-       struct its_itte *itte;
+       struct its_ite *ite;
 
 
-       itte = find_itte(its, device_id, event_id);
-       if (!itte)
+       ite = find_ite(its, device_id, event_id);
+       if (!ite)
                return E_ITS_CLEAR_UNMAPPED_INTERRUPT;
 
-       itte->irq->pending_latch = false;
+       ite->irq->pending_latch = false;
 
        return 0;
 }
@@ -904,14 +970,14 @@ static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its,
 {
        u32 device_id = its_cmd_get_deviceid(its_cmd);
        u32 event_id = its_cmd_get_id(its_cmd);
-       struct its_itte *itte;
+       struct its_ite *ite;
 
 
-       itte = find_itte(its, device_id, event_id);
-       if (!itte)
+       ite = find_ite(its, device_id, event_id);
+       if (!ite)
                return E_ITS_INV_UNMAPPED_INTERRUPT;
 
-       return update_lpi_config(kvm, itte->irq, NULL);
+       return update_lpi_config(kvm, ite->irq, NULL);
 }
 
 /*
@@ -1213,6 +1279,33 @@ static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,
        return extract_bytes(its->creadr, addr & 0x7, len);
 }
 
+static int vgic_mmio_uaccess_write_its_creadr(struct kvm *kvm,
+                                             struct vgic_its *its,
+                                             gpa_t addr, unsigned int len,
+                                             unsigned long val)
+{
+       u32 cmd_offset;
+       int ret = 0;
+
+       mutex_lock(&its->cmd_lock);
+
+       if (its->enabled) {
+               ret = -EBUSY;
+               goto out;
+       }
+
+       cmd_offset = ITS_CMD_OFFSET(val);
+       if (cmd_offset >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
+               ret = -EINVAL;
+               goto out;
+       }
+
+       its->creadr = cmd_offset;
+out:
+       mutex_unlock(&its->cmd_lock);
+       return ret;
+}
+
 #define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7)
 static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm,
                                              struct vgic_its *its,
@@ -1241,6 +1334,7 @@ static void vgic_mmio_write_its_baser(struct kvm *kvm,
                                      gpa_t addr, unsigned int len,
                                      unsigned long val)
 {
+       const struct vgic_its_abi *abi = vgic_its_get_abi(its);
        u64 entry_size, device_type;
        u64 reg, *regptr, clearbits = 0;
 
@@ -1251,12 +1345,12 @@ static void vgic_mmio_write_its_baser(struct kvm *kvm,
        switch (BASER_INDEX(addr)) {
        case 0:
                regptr = &its->baser_device_table;
-               entry_size = 8;
+               entry_size = abi->dte_esz;
                device_type = GITS_BASER_TYPE_DEVICE;
                break;
        case 1:
                regptr = &its->baser_coll_table;
-               entry_size = 8;
+               entry_size = abi->cte_esz;
                device_type = GITS_BASER_TYPE_COLLECTION;
                clearbits = GITS_BASER_INDIRECT;
                break;
@@ -1317,6 +1411,16 @@ static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its,
        .its_write = wr,                                        \
 }
 
+#define REGISTER_ITS_DESC_UACCESS(off, rd, wr, uwr, length, acc)\
+{                                                              \
+       .reg_offset = off,                                      \
+       .len = length,                                          \
+       .access_flags = acc,                                    \
+       .its_read = rd,                                         \
+       .its_write = wr,                                        \
+       .uaccess_its_write = uwr,                               \
+}
+
 static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its,
                              gpa_t addr, unsigned int len, unsigned long val)
 {
@@ -1327,8 +1431,9 @@ static struct vgic_register_region its_registers[] = {
        REGISTER_ITS_DESC(GITS_CTLR,
                vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4,
                VGIC_ACCESS_32bit),
-       REGISTER_ITS_DESC(GITS_IIDR,
-               vgic_mmio_read_its_iidr, its_mmio_write_wi, 4,
+       REGISTER_ITS_DESC_UACCESS(GITS_IIDR,
+               vgic_mmio_read_its_iidr, its_mmio_write_wi,
+               vgic_mmio_uaccess_write_its_iidr, 4,
                VGIC_ACCESS_32bit),
        REGISTER_ITS_DESC(GITS_TYPER,
                vgic_mmio_read_its_typer, its_mmio_write_wi, 8,
@@ -1339,8 +1444,9 @@ static struct vgic_register_region its_registers[] = {
        REGISTER_ITS_DESC(GITS_CWRITER,
                vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8,
                VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
-       REGISTER_ITS_DESC(GITS_CREADR,
-               vgic_mmio_read_its_creadr, its_mmio_write_wi, 8,
+       REGISTER_ITS_DESC_UACCESS(GITS_CREADR,
+               vgic_mmio_read_its_creadr, its_mmio_write_wi,
+               vgic_mmio_uaccess_write_its_creadr, 8,
                VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
        REGISTER_ITS_DESC(GITS_BASER,
                vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,
@@ -1387,7 +1493,6 @@ static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its)
        (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb)                | \
         GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner)         | \
         GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable)             | \
-        ((8ULL - 1) << GITS_BASER_ENTRY_SIZE_SHIFT)                    | \
         GITS_BASER_PAGE_SIZE_64K)
 
 #define INITIAL_PROPBASER_VALUE                                                  \
@@ -1427,7 +1532,7 @@ static int vgic_its_create(struct kvm_device *dev, u32 type)
 
        dev->private = its;
 
-       return 0;
+       return vgic_its_set_abi(its, NR_ITS_ABIS - 1);
 }
 
 static void vgic_its_destroy(struct kvm_device *kvm_dev)
@@ -1435,7 +1540,7 @@ 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_itte *itte;
+       struct its_ite *ite;
        struct list_head *dev_cur, *dev_temp;
        struct list_head *cur, *temp;
 
@@ -1450,8 +1555,8 @@ static void vgic_its_destroy(struct kvm_device *kvm_dev)
        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) {
-                       itte = (container_of(cur, struct its_itte, itte_list));
-                       its_free_itte(kvm, itte);
+                       ite = (container_of(cur, struct its_ite, ite_list));
+                       its_free_ite(kvm, ite);
                }
                list_del(dev_cur);
                kfree(dev);
@@ -1466,6 +1571,129 @@ static void vgic_its_destroy(struct kvm_device *kvm_dev)
        kfree(its);
 }
 
+int vgic_its_has_attr_regs(struct kvm_device *dev,
+                          struct kvm_device_attr *attr)
+{
+       const struct vgic_register_region *region;
+       gpa_t offset = attr->attr;
+       int align;
+
+       align = (offset < GITS_TYPER) || (offset >= GITS_PIDR4) ? 0x3 : 0x7;
+
+       if (offset & align)
+               return -EINVAL;
+
+       region = vgic_find_mmio_region(its_registers,
+                                      ARRAY_SIZE(its_registers),
+                                      offset);
+       if (!region)
+               return -ENXIO;
+
+       return 0;
+}
+
+int vgic_its_attr_regs_access(struct kvm_device *dev,
+                             struct kvm_device_attr *attr,
+                             u64 *reg, bool is_write)
+{
+       const struct vgic_register_region *region;
+       struct vgic_its *its;
+       gpa_t addr, offset;
+       unsigned int len;
+       int align, ret = 0;
+
+       its = dev->private;
+       offset = attr->attr;
+
+       /*
+        * Although the spec supports upper/lower 32-bit accesses to
+        * 64-bit ITS registers, the userspace ABI requires 64-bit
+        * accesses to all 64-bit wide registers. We therefore only
+        * support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID
+        * registers
+        */
+       if ((offset < GITS_TYPER) || (offset >= GITS_PIDR4))
+               align = 0x3;
+       else
+               align = 0x7;
+
+       if (offset & align)
+               return -EINVAL;
+
+       mutex_lock(&dev->kvm->lock);
+
+       if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) {
+               ret = -ENXIO;
+               goto out;
+       }
+
+       region = vgic_find_mmio_region(its_registers,
+                                      ARRAY_SIZE(its_registers),
+                                      offset);
+       if (!region) {
+               ret = -ENXIO;
+               goto out;
+       }
+
+       if (!lock_all_vcpus(dev->kvm)) {
+               ret = -EBUSY;
+               goto out;
+       }
+
+       addr = its->vgic_its_base + offset;
+
+       len = region->access_flags & VGIC_ACCESS_64bit ? 8 : 4;
+
+       if (is_write) {
+               if (region->uaccess_its_write)
+                       ret = region->uaccess_its_write(dev->kvm, its, addr,
+                                                       len, *reg);
+               else
+                       region->its_write(dev->kvm, its, addr, len, *reg);
+       } else {
+               *reg = region->its_read(dev->kvm, its, addr, len);
+       }
+       unlock_all_vcpus(dev->kvm);
+out:
+       mutex_unlock(&dev->kvm->lock);
+       return ret;
+}
+
+/**
+ * vgic_its_save_tables_v0 - Save the ITS tables into guest ARM
+ * according to v0 ABI
+ */
+static int vgic_its_save_tables_v0(struct vgic_its *its)
+{
+       return -ENXIO;
+}
+
+/**
+ * vgic_its_restore_tables_v0 - Restore the ITS tables from guest RAM
+ * to internal data structs according to V0 ABI
+ *
+ */
+static int vgic_its_restore_tables_v0(struct vgic_its *its)
+{
+       return -ENXIO;
+}
+
+static int vgic_its_commit_v0(struct vgic_its *its)
+{
+       const struct vgic_its_abi *abi;
+
+       abi = vgic_its_get_abi(its);
+       its->baser_coll_table &= ~GITS_BASER_ENTRY_SIZE_MASK;
+       its->baser_device_table &= ~GITS_BASER_ENTRY_SIZE_MASK;
+
+       its->baser_coll_table |= (GIC_ENCODE_SZ(abi->cte_esz, 5)
+                                       << GITS_BASER_ENTRY_SIZE_SHIFT);
+
+       its->baser_device_table |= (GIC_ENCODE_SZ(abi->dte_esz, 5)
+                                       << GITS_BASER_ENTRY_SIZE_SHIFT);
+       return 0;
+}
+
 static int vgic_its_has_attr(struct kvm_device *dev,
                             struct kvm_device_attr *attr)
 {
@@ -1482,6 +1710,8 @@ static int vgic_its_has_attr(struct kvm_device *dev,
                        return 0;
                }
                break;
+       case KVM_DEV_ARM_VGIC_GRP_ITS_REGS:
+               return vgic_its_has_attr_regs(dev, attr);
        }
        return -ENXIO;
 }
@@ -1521,6 +1751,15 @@ static int vgic_its_set_attr(struct kvm_device *dev,
                        return 0;
                }
                break;
+       case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: {
+               u64 __user *uaddr = (u64 __user *)(long)attr->addr;
+               u64 reg;
+
+               if (get_user(reg, uaddr))
+                       return -EFAULT;
+
+               return vgic_its_attr_regs_access(dev, attr, &reg, true);
+       }
        }
        return -ENXIO;
 }
@@ -1541,10 +1780,20 @@ static int vgic_its_get_attr(struct kvm_device *dev,
                if (copy_to_user(uaddr, &addr, sizeof(addr)))
                        return -EFAULT;
                break;
+       }
+       case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: {
+               u64 __user *uaddr = (u64 __user *)(long)attr->addr;
+               u64 reg;
+               int ret;
+
+               ret = vgic_its_attr_regs_access(dev, attr, &reg, false);
+               if (ret)
+                       return ret;
+               return put_user(reg, uaddr);
+       }
        default:
                return -ENXIO;
        }
-       }
 
        return 0;
 }