4 * Copyright (C) 2015,2016 ARM Ltd.
5 * Author: Andre Przywara <andre.przywara@arm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/cpu.h>
21 #include <linux/kvm.h>
22 #include <linux/kvm_host.h>
23 #include <linux/interrupt.h>
24 #include <linux/list.h>
25 #include <linux/uaccess.h>
27 #include <linux/irqchip/arm-gic-v3.h>
29 #include <asm/kvm_emulate.h>
30 #include <asm/kvm_arm.h>
31 #include <asm/kvm_mmu.h>
34 #include "vgic-mmio.h"
36 static int vgic_its_save_tables_v0(struct vgic_its *its);
37 static int vgic_its_restore_tables_v0(struct vgic_its *its);
38 static int vgic_its_commit_v0(struct vgic_its *its);
41 * Creates a new (reference to a) struct vgic_irq for a given LPI.
42 * If this LPI is already mapped on another ITS, we increase its refcount
43 * and return a pointer to the existing structure.
44 * If this is a "new" LPI, we allocate and initialize a new struct vgic_irq.
45 * This function returns a pointer to the _unlocked_ structure.
47 static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid)
49 struct vgic_dist *dist = &kvm->arch.vgic;
50 struct vgic_irq *irq = vgic_get_irq(kvm, NULL, intid), *oldirq;
52 /* In this case there is no put, since we keep the reference. */
56 irq = kzalloc(sizeof(struct vgic_irq), GFP_KERNEL);
58 return ERR_PTR(-ENOMEM);
60 INIT_LIST_HEAD(&irq->lpi_list);
61 INIT_LIST_HEAD(&irq->ap_list);
62 spin_lock_init(&irq->irq_lock);
64 irq->config = VGIC_CONFIG_EDGE;
65 kref_init(&irq->refcount);
68 spin_lock(&dist->lpi_list_lock);
71 * There could be a race with another vgic_add_lpi(), so we need to
72 * check that we don't add a second list entry with the same LPI.
74 list_for_each_entry(oldirq, &dist->lpi_list_head, lpi_list) {
75 if (oldirq->intid != intid)
78 /* Someone was faster with adding this LPI, lets use that. */
83 * This increases the refcount, the caller is expected to
84 * call vgic_put_irq() on the returned pointer once it's
85 * finished with the IRQ.
87 vgic_get_irq_kref(irq);
92 list_add_tail(&irq->lpi_list, &dist->lpi_list_head);
93 dist->lpi_list_count++;
96 spin_unlock(&dist->lpi_list_lock);
102 struct list_head dev_list;
104 /* the head for the list of ITTEs */
105 struct list_head itt_head;
106 u32 num_eventid_bits;
110 #define COLLECTION_NOT_MAPPED ((u32)~0)
112 struct its_collection {
113 struct list_head coll_list;
119 #define its_is_collection_mapped(coll) ((coll) && \
120 ((coll)->target_addr != COLLECTION_NOT_MAPPED))
123 struct list_head ite_list;
125 struct vgic_irq *irq;
126 struct its_collection *collection;
132 * struct vgic_its_abi - ITS abi ops and settings
133 * @cte_esz: collection table entry size
134 * @dte_esz: device table entry size
135 * @ite_esz: interrupt translation table entry size
136 * @save tables: save the ITS tables into guest RAM
137 * @restore_tables: restore the ITS internal structs from tables
138 * stored in guest RAM
139 * @commit: initialize the registers which expose the ABI settings,
140 * especially the entry sizes
142 struct vgic_its_abi {
146 int (*save_tables)(struct vgic_its *its);
147 int (*restore_tables)(struct vgic_its *its);
148 int (*commit)(struct vgic_its *its);
151 static const struct vgic_its_abi its_table_abi_versions[] = {
152 [0] = {.cte_esz = 8, .dte_esz = 8, .ite_esz = 8,
153 .save_tables = vgic_its_save_tables_v0,
154 .restore_tables = vgic_its_restore_tables_v0,
155 .commit = vgic_its_commit_v0,
159 #define NR_ITS_ABIS ARRAY_SIZE(its_table_abi_versions)
161 inline const struct vgic_its_abi *vgic_its_get_abi(struct vgic_its *its)
163 return &its_table_abi_versions[its->abi_rev];
166 int vgic_its_set_abi(struct vgic_its *its, int rev)
168 const struct vgic_its_abi *abi;
171 abi = vgic_its_get_abi(its);
172 return abi->commit(its);
176 * Find and returns a device in the device table for an ITS.
177 * Must be called with the its_lock mutex held.
179 static struct its_device *find_its_device(struct vgic_its *its, u32 device_id)
181 struct its_device *device;
183 list_for_each_entry(device, &its->device_list, dev_list)
184 if (device_id == device->device_id)
191 * Find and returns an interrupt translation table entry (ITTE) for a given
192 * Device ID/Event ID pair on an ITS.
193 * Must be called with the its_lock mutex held.
195 static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,
198 struct its_device *device;
201 device = find_its_device(its, device_id);
205 list_for_each_entry(ite, &device->itt_head, ite_list)
206 if (ite->event_id == event_id)
212 /* To be used as an iterator this macro misses the enclosing parentheses */
213 #define for_each_lpi_its(dev, ite, its) \
214 list_for_each_entry(dev, &(its)->device_list, dev_list) \
215 list_for_each_entry(ite, &(dev)->itt_head, ite_list)
218 * We only implement 48 bits of PA at the moment, although the ITS
219 * supports more. Let's be restrictive here.
221 #define BASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16))
222 #define CBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12))
223 #define PENDBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16))
224 #define PROPBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12))
226 #define GIC_LPI_OFFSET 8192
228 #define VITS_TYPER_IDBITS 16
231 * Finds and returns a collection in the ITS collection table.
232 * Must be called with the its_lock mutex held.
234 static struct its_collection *find_collection(struct vgic_its *its, int coll_id)
236 struct its_collection *collection;
238 list_for_each_entry(collection, &its->collection_list, coll_list) {
239 if (coll_id == collection->collection_id)
246 #define LPI_PROP_ENABLE_BIT(p) ((p) & LPI_PROP_ENABLED)
247 #define LPI_PROP_PRIORITY(p) ((p) & 0xfc)
250 * Reads the configuration data for a given LPI from guest memory and
251 * updates the fields in struct vgic_irq.
252 * If filter_vcpu is not NULL, applies only if the IRQ is targeting this
253 * VCPU. Unconditionally applies if filter_vcpu is NULL.
255 static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
256 struct kvm_vcpu *filter_vcpu)
258 u64 propbase = PROPBASER_ADDRESS(kvm->arch.vgic.propbaser);
262 ret = kvm_read_guest(kvm, propbase + irq->intid - GIC_LPI_OFFSET,
268 spin_lock(&irq->irq_lock);
270 if (!filter_vcpu || filter_vcpu == irq->target_vcpu) {
271 irq->priority = LPI_PROP_PRIORITY(prop);
272 irq->enabled = LPI_PROP_ENABLE_BIT(prop);
274 vgic_queue_irq_unlock(kvm, irq);
276 spin_unlock(&irq->irq_lock);
283 * Create a snapshot of the current LPI list, so that we can enumerate all
284 * LPIs without holding any lock.
285 * Returns the array length and puts the kmalloc'ed array into intid_ptr.
287 static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
289 struct vgic_dist *dist = &kvm->arch.vgic;
290 struct vgic_irq *irq;
292 int irq_count = dist->lpi_list_count, i = 0;
295 * We use the current value of the list length, which may change
296 * after the kmalloc. We don't care, because the guest shouldn't
297 * change anything while the command handling is still running,
298 * and in the worst case we would miss a new IRQ, which one wouldn't
299 * expect to be covered by this command anyway.
301 intids = kmalloc_array(irq_count, sizeof(intids[0]), GFP_KERNEL);
305 spin_lock(&dist->lpi_list_lock);
306 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
307 /* We don't need to "get" the IRQ, as we hold the list lock. */
308 intids[i] = irq->intid;
309 if (++i == irq_count)
312 spin_unlock(&dist->lpi_list_lock);
319 * Promotes the ITS view of affinity of an ITTE (which redistributor this LPI
320 * is targeting) to the VGIC's view, which deals with target VCPUs.
321 * Needs to be called whenever either the collection for a LPIs has
322 * changed or the collection itself got retargeted.
324 static void update_affinity_ite(struct kvm *kvm, struct its_ite *ite)
326 struct kvm_vcpu *vcpu;
328 if (!its_is_collection_mapped(ite->collection))
331 vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr);
333 spin_lock(&ite->irq->irq_lock);
334 ite->irq->target_vcpu = vcpu;
335 spin_unlock(&ite->irq->irq_lock);
339 * Updates the target VCPU for every LPI targeting this collection.
340 * Must be called with the its_lock mutex held.
342 static void update_affinity_collection(struct kvm *kvm, struct vgic_its *its,
343 struct its_collection *coll)
345 struct its_device *device;
348 for_each_lpi_its(device, ite, its) {
349 if (!ite->collection || coll != ite->collection)
352 update_affinity_ite(kvm, ite);
356 static u32 max_lpis_propbaser(u64 propbaser)
358 int nr_idbits = (propbaser & 0x1f) + 1;
360 return 1U << min(nr_idbits, INTERRUPT_ID_BITS_ITS);
364 * Scan the whole LPI pending table and sync the pending bit in there
365 * with our own data structures. This relies on the LPI being
368 static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
370 gpa_t pendbase = PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
371 struct vgic_irq *irq;
372 int last_byte_offset = -1;
377 nr_irqs = vgic_copy_lpi_list(vcpu->kvm, &intids);
381 for (i = 0; i < nr_irqs; i++) {
382 int byte_offset, bit_nr;
385 byte_offset = intids[i] / BITS_PER_BYTE;
386 bit_nr = intids[i] % BITS_PER_BYTE;
389 * For contiguously allocated LPIs chances are we just read
390 * this very same byte in the last iteration. Reuse that.
392 if (byte_offset != last_byte_offset) {
393 ret = kvm_read_guest(vcpu->kvm, pendbase + byte_offset,
399 last_byte_offset = byte_offset;
402 irq = vgic_get_irq(vcpu->kvm, NULL, intids[i]);
403 spin_lock(&irq->irq_lock);
404 irq->pending_latch = pendmask & (1U << bit_nr);
405 vgic_queue_irq_unlock(vcpu->kvm, irq);
406 vgic_put_irq(vcpu->kvm, irq);
414 static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,
415 struct vgic_its *its,
416 gpa_t addr, unsigned int len)
418 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
419 u64 reg = GITS_TYPER_PLPIS;
422 * We use linear CPU numbers for redistributor addressing,
423 * so GITS_TYPER.PTA is 0.
424 * Also we force all PROPBASER registers to be the same, so
425 * CommonLPIAff is 0 as well.
426 * To avoid memory waste in the guest, we keep the number of IDBits and
427 * DevBits low - as least for the time being.
429 reg |= 0x0f << GITS_TYPER_DEVBITS_SHIFT;
430 reg |= GIC_ENCODE_SZ(VITS_TYPER_IDBITS, 5) << GITS_TYPER_IDBITS_SHIFT;
431 reg |= GIC_ENCODE_SZ(abi->ite_esz, 4) << GITS_TYPER_ITT_ENTRY_SIZE_SHIFT;
433 return extract_bytes(reg, addr & 7, len);
436 static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm,
437 struct vgic_its *its,
438 gpa_t addr, unsigned int len)
442 val = (its->abi_rev << GITS_IIDR_REV_SHIFT) & GITS_IIDR_REV_MASK;
443 val |= (PRODUCT_ID_KVM << GITS_IIDR_PRODUCTID_SHIFT) | IMPLEMENTER_ARM;
447 static int vgic_mmio_uaccess_write_its_iidr(struct kvm *kvm,
448 struct vgic_its *its,
449 gpa_t addr, unsigned int len,
452 u32 rev = GITS_IIDR_REV(val);
454 if (rev >= NR_ITS_ABIS)
456 return vgic_its_set_abi(its, rev);
459 static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm,
460 struct vgic_its *its,
461 gpa_t addr, unsigned int len)
463 switch (addr & 0xffff) {
465 return 0x92; /* part number, bits[7:0] */
467 return 0xb4; /* part number, bits[11:8] */
469 return GIC_PIDR2_ARCH_GICv3 | 0x0b;
471 return 0x40; /* This is a 64K software visible page */
472 /* The following are the ID registers for (any) GIC. */
487 * Find the target VCPU and the LPI number for a given devid/eventid pair
488 * and make this IRQ pending, possibly injecting it.
489 * Must be called with the its_lock mutex held.
490 * Returns 0 on success, a positive error value for any ITS mapping
491 * related errors and negative error values for generic errors.
493 static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its,
494 u32 devid, u32 eventid)
496 struct kvm_vcpu *vcpu;
502 ite = find_ite(its, devid, eventid);
503 if (!ite || !its_is_collection_mapped(ite->collection))
504 return E_ITS_INT_UNMAPPED_INTERRUPT;
506 vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr);
508 return E_ITS_INT_UNMAPPED_INTERRUPT;
510 if (!vcpu->arch.vgic_cpu.lpis_enabled)
513 spin_lock(&ite->irq->irq_lock);
514 ite->irq->pending_latch = true;
515 vgic_queue_irq_unlock(kvm, ite->irq);
520 static struct vgic_io_device *vgic_get_its_iodev(struct kvm_io_device *dev)
522 struct vgic_io_device *iodev;
524 if (dev->ops != &kvm_io_gic_ops)
527 iodev = container_of(dev, struct vgic_io_device, dev);
529 if (iodev->iodev_type != IODEV_ITS)
536 * Queries the KVM IO bus framework to get the ITS pointer from the given
538 * We then call vgic_its_trigger_msi() with the decoded data.
539 * According to the KVM_SIGNAL_MSI API description returns 1 on success.
541 int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi)
544 struct kvm_io_device *kvm_io_dev;
545 struct vgic_io_device *iodev;
548 if (!vgic_has_its(kvm))
551 if (!(msi->flags & KVM_MSI_VALID_DEVID))
554 address = (u64)msi->address_hi << 32 | msi->address_lo;
556 kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, address);
560 iodev = vgic_get_its_iodev(kvm_io_dev);
564 mutex_lock(&iodev->its->its_lock);
565 ret = vgic_its_trigger_msi(kvm, iodev->its, msi->devid, msi->data);
566 mutex_unlock(&iodev->its->its_lock);
572 * KVM_SIGNAL_MSI demands a return value > 0 for success and 0
573 * if the guest has blocked the MSI. So we map any LPI mapping
574 * related error to that.
582 /* Requires the its_lock to be held. */
583 static void its_free_ite(struct kvm *kvm, struct its_ite *ite)
585 list_del(&ite->ite_list);
587 /* This put matches the get in vgic_add_lpi. */
589 vgic_put_irq(kvm, ite->irq);
594 static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size)
596 return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT_ULL(size) - 1);
599 #define its_cmd_get_command(cmd) its_cmd_mask_field(cmd, 0, 0, 8)
600 #define its_cmd_get_deviceid(cmd) its_cmd_mask_field(cmd, 0, 32, 32)
601 #define its_cmd_get_size(cmd) (its_cmd_mask_field(cmd, 1, 0, 5) + 1)
602 #define its_cmd_get_id(cmd) its_cmd_mask_field(cmd, 1, 0, 32)
603 #define its_cmd_get_physical_id(cmd) its_cmd_mask_field(cmd, 1, 32, 32)
604 #define its_cmd_get_collection(cmd) its_cmd_mask_field(cmd, 2, 0, 16)
605 #define its_cmd_get_target_addr(cmd) its_cmd_mask_field(cmd, 2, 16, 32)
606 #define its_cmd_get_validbit(cmd) its_cmd_mask_field(cmd, 2, 63, 1)
609 * The DISCARD command frees an Interrupt Translation Table Entry (ITTE).
610 * Must be called with the its_lock mutex held.
612 static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its,
615 u32 device_id = its_cmd_get_deviceid(its_cmd);
616 u32 event_id = its_cmd_get_id(its_cmd);
620 ite = find_ite(its, device_id, event_id);
621 if (ite && ite->collection) {
623 * Though the spec talks about removing the pending state, we
624 * don't bother here since we clear the ITTE anyway and the
625 * pending state is a property of the ITTE struct.
627 its_free_ite(kvm, ite);
631 return E_ITS_DISCARD_UNMAPPED_INTERRUPT;
635 * The MOVI command moves an ITTE to a different collection.
636 * Must be called with the its_lock mutex held.
638 static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,
641 u32 device_id = its_cmd_get_deviceid(its_cmd);
642 u32 event_id = its_cmd_get_id(its_cmd);
643 u32 coll_id = its_cmd_get_collection(its_cmd);
644 struct kvm_vcpu *vcpu;
646 struct its_collection *collection;
648 ite = find_ite(its, device_id, event_id);
650 return E_ITS_MOVI_UNMAPPED_INTERRUPT;
652 if (!its_is_collection_mapped(ite->collection))
653 return E_ITS_MOVI_UNMAPPED_COLLECTION;
655 collection = find_collection(its, coll_id);
656 if (!its_is_collection_mapped(collection))
657 return E_ITS_MOVI_UNMAPPED_COLLECTION;
659 ite->collection = collection;
660 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
662 spin_lock(&ite->irq->irq_lock);
663 ite->irq->target_vcpu = vcpu;
664 spin_unlock(&ite->irq->irq_lock);
670 * Check whether an ID can be stored into the corresponding guest table.
671 * For a direct table this is pretty easy, but gets a bit nasty for
672 * indirect tables. We check whether the resulting guest physical address
673 * is actually valid (covered by a memslot and guest accessbible).
674 * For this we have to read the respective first level entry.
676 static bool vgic_its_check_id(struct vgic_its *its, u64 baser, int id)
678 int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
682 int esz = GITS_BASER_ENTRY_SIZE(baser);
684 if (!(baser & GITS_BASER_INDIRECT)) {
687 if (id >= (l1_tbl_size / esz))
690 addr = BASER_ADDRESS(baser) + id * esz;
691 gfn = addr >> PAGE_SHIFT;
693 return kvm_is_visible_gfn(its->dev->kvm, gfn);
696 /* calculate and check the index into the 1st level */
697 index = id / (SZ_64K / esz);
698 if (index >= (l1_tbl_size / sizeof(u64)))
701 /* Each 1st level entry is represented by a 64-bit value. */
702 if (kvm_read_guest(its->dev->kvm,
703 BASER_ADDRESS(baser) + index * sizeof(indirect_ptr),
704 &indirect_ptr, sizeof(indirect_ptr)))
707 indirect_ptr = le64_to_cpu(indirect_ptr);
709 /* check the valid bit of the first level entry */
710 if (!(indirect_ptr & BIT_ULL(63)))
714 * Mask the guest physical address and calculate the frame number.
715 * Any address beyond our supported 48 bits of PA will be caught
716 * by the actual check in the final step.
718 indirect_ptr &= GENMASK_ULL(51, 16);
720 /* Find the address of the actual entry */
721 index = id % (SZ_64K / esz);
722 indirect_ptr += index * esz;
723 gfn = indirect_ptr >> PAGE_SHIFT;
725 return kvm_is_visible_gfn(its->dev->kvm, gfn);
728 static int vgic_its_alloc_collection(struct vgic_its *its,
729 struct its_collection **colp,
732 struct its_collection *collection;
734 if (!vgic_its_check_id(its, its->baser_coll_table, coll_id))
735 return E_ITS_MAPC_COLLECTION_OOR;
737 collection = kzalloc(sizeof(*collection), GFP_KERNEL);
739 collection->collection_id = coll_id;
740 collection->target_addr = COLLECTION_NOT_MAPPED;
742 list_add_tail(&collection->coll_list, &its->collection_list);
748 static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)
750 struct its_collection *collection;
751 struct its_device *device;
755 * Clearing the mapping for that collection ID removes the
756 * entry from the list. If there wasn't any before, we can
759 collection = find_collection(its, coll_id);
763 for_each_lpi_its(device, ite, its)
764 if (ite->collection &&
765 ite->collection->collection_id == coll_id)
766 ite->collection = NULL;
768 list_del(&collection->coll_list);
773 * The MAPTI and MAPI commands map LPIs to ITTEs.
774 * Must be called with its_lock mutex held.
776 static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
779 u32 device_id = its_cmd_get_deviceid(its_cmd);
780 u32 event_id = its_cmd_get_id(its_cmd);
781 u32 coll_id = its_cmd_get_collection(its_cmd);
783 struct its_device *device;
784 struct its_collection *collection, *new_coll = NULL;
786 struct vgic_irq *irq;
788 device = find_its_device(its, device_id);
790 return E_ITS_MAPTI_UNMAPPED_DEVICE;
792 if (event_id >= BIT_ULL(device->num_eventid_bits))
793 return E_ITS_MAPTI_ID_OOR;
795 if (its_cmd_get_command(its_cmd) == GITS_CMD_MAPTI)
796 lpi_nr = its_cmd_get_physical_id(its_cmd);
799 if (lpi_nr < GIC_LPI_OFFSET ||
800 lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser))
801 return E_ITS_MAPTI_PHYSICALID_OOR;
803 /* If there is an existing mapping, behavior is UNPREDICTABLE. */
804 if (find_ite(its, device_id, event_id))
807 collection = find_collection(its, coll_id);
809 int ret = vgic_its_alloc_collection(its, &collection, coll_id);
812 new_coll = collection;
815 ite = kzalloc(sizeof(struct its_ite), GFP_KERNEL);
818 vgic_its_free_collection(its, coll_id);
822 ite->event_id = event_id;
823 list_add_tail(&ite->ite_list, &device->itt_head);
825 ite->collection = collection;
828 irq = vgic_add_lpi(kvm, lpi_nr);
831 vgic_its_free_collection(its, coll_id);
832 its_free_ite(kvm, ite);
837 update_affinity_ite(kvm, ite);
840 * We "cache" the configuration table entries in out struct vgic_irq's.
841 * However we only have those structs for mapped IRQs, so we read in
842 * the respective config data from memory here upon mapping the LPI.
844 update_lpi_config(kvm, ite->irq, NULL);
849 /* Requires the its_lock to be held. */
850 static void vgic_its_unmap_device(struct kvm *kvm, struct its_device *device)
852 struct its_ite *ite, *temp;
855 * The spec says that unmapping a device with still valid
856 * ITTEs associated is UNPREDICTABLE. We remove all ITTEs,
857 * since we cannot leave the memory unreferenced.
859 list_for_each_entry_safe(ite, temp, &device->itt_head, ite_list)
860 its_free_ite(kvm, ite);
862 list_del(&device->dev_list);
867 * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs).
868 * Must be called with the its_lock mutex held.
870 static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
873 u32 device_id = its_cmd_get_deviceid(its_cmd);
874 bool valid = its_cmd_get_validbit(its_cmd);
875 u8 num_eventid_bits = its_cmd_get_size(its_cmd);
876 struct its_device *device;
878 if (!vgic_its_check_id(its, its->baser_device_table, device_id))
879 return E_ITS_MAPD_DEVICE_OOR;
881 if (valid && num_eventid_bits > VITS_TYPER_IDBITS)
882 return E_ITS_MAPD_ITTSIZE_OOR;
884 device = find_its_device(its, device_id);
887 * The spec says that calling MAPD on an already mapped device
888 * invalidates all cached data for this device. We implement this
889 * by removing the mapping and re-establishing it.
892 vgic_its_unmap_device(kvm, device);
895 * The spec does not say whether unmapping a not-mapped device
896 * is an error, so we are done in any case.
901 device = kzalloc(sizeof(struct its_device), GFP_KERNEL);
905 device->device_id = device_id;
906 device->num_eventid_bits = num_eventid_bits;
908 INIT_LIST_HEAD(&device->itt_head);
910 list_add_tail(&device->dev_list, &its->device_list);
916 * The MAPC command maps collection IDs to redistributors.
917 * Must be called with the its_lock mutex held.
919 static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its,
924 struct its_collection *collection;
927 valid = its_cmd_get_validbit(its_cmd);
928 coll_id = its_cmd_get_collection(its_cmd);
929 target_addr = its_cmd_get_target_addr(its_cmd);
931 if (target_addr >= atomic_read(&kvm->online_vcpus))
932 return E_ITS_MAPC_PROCNUM_OOR;
935 vgic_its_free_collection(its, coll_id);
937 collection = find_collection(its, coll_id);
942 ret = vgic_its_alloc_collection(its, &collection,
946 collection->target_addr = target_addr;
948 collection->target_addr = target_addr;
949 update_affinity_collection(kvm, its, collection);
957 * The CLEAR command removes the pending state for a particular LPI.
958 * Must be called with the its_lock mutex held.
960 static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its,
963 u32 device_id = its_cmd_get_deviceid(its_cmd);
964 u32 event_id = its_cmd_get_id(its_cmd);
968 ite = find_ite(its, device_id, event_id);
970 return E_ITS_CLEAR_UNMAPPED_INTERRUPT;
972 ite->irq->pending_latch = false;
978 * The INV command syncs the configuration bits from the memory table.
979 * Must be called with the its_lock mutex held.
981 static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its,
984 u32 device_id = its_cmd_get_deviceid(its_cmd);
985 u32 event_id = its_cmd_get_id(its_cmd);
989 ite = find_ite(its, device_id, event_id);
991 return E_ITS_INV_UNMAPPED_INTERRUPT;
993 return update_lpi_config(kvm, ite->irq, NULL);
997 * The INVALL command requests flushing of all IRQ data in this collection.
998 * Find the VCPU mapped to that collection, then iterate over the VM's list
999 * of mapped LPIs and update the configuration for each IRQ which targets
1000 * the specified vcpu. The configuration will be read from the in-memory
1001 * configuration table.
1002 * Must be called with the its_lock mutex held.
1004 static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its,
1007 u32 coll_id = its_cmd_get_collection(its_cmd);
1008 struct its_collection *collection;
1009 struct kvm_vcpu *vcpu;
1010 struct vgic_irq *irq;
1014 collection = find_collection(its, coll_id);
1015 if (!its_is_collection_mapped(collection))
1016 return E_ITS_INVALL_UNMAPPED_COLLECTION;
1018 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
1020 irq_count = vgic_copy_lpi_list(kvm, &intids);
1024 for (i = 0; i < irq_count; i++) {
1025 irq = vgic_get_irq(kvm, NULL, intids[i]);
1028 update_lpi_config(kvm, irq, vcpu);
1029 vgic_put_irq(kvm, irq);
1038 * The MOVALL command moves the pending state of all IRQs targeting one
1039 * redistributor to another. We don't hold the pending state in the VCPUs,
1040 * but in the IRQs instead, so there is really not much to do for us here.
1041 * However the spec says that no IRQ must target the old redistributor
1042 * afterwards, so we make sure that no LPI is using the associated target_vcpu.
1043 * This command affects all LPIs in the system that target that redistributor.
1045 static int vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its,
1048 struct vgic_dist *dist = &kvm->arch.vgic;
1049 u32 target1_addr = its_cmd_get_target_addr(its_cmd);
1050 u32 target2_addr = its_cmd_mask_field(its_cmd, 3, 16, 32);
1051 struct kvm_vcpu *vcpu1, *vcpu2;
1052 struct vgic_irq *irq;
1054 if (target1_addr >= atomic_read(&kvm->online_vcpus) ||
1055 target2_addr >= atomic_read(&kvm->online_vcpus))
1056 return E_ITS_MOVALL_PROCNUM_OOR;
1058 if (target1_addr == target2_addr)
1061 vcpu1 = kvm_get_vcpu(kvm, target1_addr);
1062 vcpu2 = kvm_get_vcpu(kvm, target2_addr);
1064 spin_lock(&dist->lpi_list_lock);
1066 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
1067 spin_lock(&irq->irq_lock);
1069 if (irq->target_vcpu == vcpu1)
1070 irq->target_vcpu = vcpu2;
1072 spin_unlock(&irq->irq_lock);
1075 spin_unlock(&dist->lpi_list_lock);
1081 * The INT command injects the LPI associated with that DevID/EvID pair.
1082 * Must be called with the its_lock mutex held.
1084 static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its,
1087 u32 msi_data = its_cmd_get_id(its_cmd);
1088 u64 msi_devid = its_cmd_get_deviceid(its_cmd);
1090 return vgic_its_trigger_msi(kvm, its, msi_devid, msi_data);
1094 * This function is called with the its_cmd lock held, but the ITS data
1095 * structure lock dropped.
1097 static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its,
1102 mutex_lock(&its->its_lock);
1103 switch (its_cmd_get_command(its_cmd)) {
1105 ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd);
1108 ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd);
1111 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
1113 case GITS_CMD_MAPTI:
1114 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
1117 ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd);
1119 case GITS_CMD_DISCARD:
1120 ret = vgic_its_cmd_handle_discard(kvm, its, its_cmd);
1122 case GITS_CMD_CLEAR:
1123 ret = vgic_its_cmd_handle_clear(kvm, its, its_cmd);
1125 case GITS_CMD_MOVALL:
1126 ret = vgic_its_cmd_handle_movall(kvm, its, its_cmd);
1129 ret = vgic_its_cmd_handle_int(kvm, its, its_cmd);
1132 ret = vgic_its_cmd_handle_inv(kvm, its, its_cmd);
1134 case GITS_CMD_INVALL:
1135 ret = vgic_its_cmd_handle_invall(kvm, its, its_cmd);
1138 /* we ignore this command: we are in sync all of the time */
1142 mutex_unlock(&its->its_lock);
1147 static u64 vgic_sanitise_its_baser(u64 reg)
1149 reg = vgic_sanitise_field(reg, GITS_BASER_SHAREABILITY_MASK,
1150 GITS_BASER_SHAREABILITY_SHIFT,
1151 vgic_sanitise_shareability);
1152 reg = vgic_sanitise_field(reg, GITS_BASER_INNER_CACHEABILITY_MASK,
1153 GITS_BASER_INNER_CACHEABILITY_SHIFT,
1154 vgic_sanitise_inner_cacheability);
1155 reg = vgic_sanitise_field(reg, GITS_BASER_OUTER_CACHEABILITY_MASK,
1156 GITS_BASER_OUTER_CACHEABILITY_SHIFT,
1157 vgic_sanitise_outer_cacheability);
1159 /* Bits 15:12 contain bits 51:48 of the PA, which we don't support. */
1160 reg &= ~GENMASK_ULL(15, 12);
1162 /* We support only one (ITS) page size: 64K */
1163 reg = (reg & ~GITS_BASER_PAGE_SIZE_MASK) | GITS_BASER_PAGE_SIZE_64K;
1168 static u64 vgic_sanitise_its_cbaser(u64 reg)
1170 reg = vgic_sanitise_field(reg, GITS_CBASER_SHAREABILITY_MASK,
1171 GITS_CBASER_SHAREABILITY_SHIFT,
1172 vgic_sanitise_shareability);
1173 reg = vgic_sanitise_field(reg, GITS_CBASER_INNER_CACHEABILITY_MASK,
1174 GITS_CBASER_INNER_CACHEABILITY_SHIFT,
1175 vgic_sanitise_inner_cacheability);
1176 reg = vgic_sanitise_field(reg, GITS_CBASER_OUTER_CACHEABILITY_MASK,
1177 GITS_CBASER_OUTER_CACHEABILITY_SHIFT,
1178 vgic_sanitise_outer_cacheability);
1181 * Sanitise the physical address to be 64k aligned.
1182 * Also limit the physical addresses to 48 bits.
1184 reg &= ~(GENMASK_ULL(51, 48) | GENMASK_ULL(15, 12));
1189 static unsigned long vgic_mmio_read_its_cbaser(struct kvm *kvm,
1190 struct vgic_its *its,
1191 gpa_t addr, unsigned int len)
1193 return extract_bytes(its->cbaser, addr & 7, len);
1196 static void vgic_mmio_write_its_cbaser(struct kvm *kvm, struct vgic_its *its,
1197 gpa_t addr, unsigned int len,
1200 /* When GITS_CTLR.Enable is 1, this register is RO. */
1204 mutex_lock(&its->cmd_lock);
1205 its->cbaser = update_64bit_reg(its->cbaser, addr & 7, len, val);
1206 its->cbaser = vgic_sanitise_its_cbaser(its->cbaser);
1209 * CWRITER is architecturally UNKNOWN on reset, but we need to reset
1210 * it to CREADR to make sure we start with an empty command buffer.
1212 its->cwriter = its->creadr;
1213 mutex_unlock(&its->cmd_lock);
1216 #define ITS_CMD_BUFFER_SIZE(baser) ((((baser) & 0xff) + 1) << 12)
1217 #define ITS_CMD_SIZE 32
1218 #define ITS_CMD_OFFSET(reg) ((reg) & GENMASK(19, 5))
1220 /* Must be called with the cmd_lock held. */
1221 static void vgic_its_process_commands(struct kvm *kvm, struct vgic_its *its)
1226 /* Commands are only processed when the ITS is enabled. */
1230 cbaser = CBASER_ADDRESS(its->cbaser);
1232 while (its->cwriter != its->creadr) {
1233 int ret = kvm_read_guest(kvm, cbaser + its->creadr,
1234 cmd_buf, ITS_CMD_SIZE);
1236 * If kvm_read_guest() fails, this could be due to the guest
1237 * programming a bogus value in CBASER or something else going
1238 * wrong from which we cannot easily recover.
1239 * According to section 6.3.2 in the GICv3 spec we can just
1240 * ignore that command then.
1243 vgic_its_handle_command(kvm, its, cmd_buf);
1245 its->creadr += ITS_CMD_SIZE;
1246 if (its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser))
1252 * By writing to CWRITER the guest announces new commands to be processed.
1253 * To avoid any races in the first place, we take the its_cmd lock, which
1254 * protects our ring buffer variables, so that there is only one user
1255 * per ITS handling commands at a given time.
1257 static void vgic_mmio_write_its_cwriter(struct kvm *kvm, struct vgic_its *its,
1258 gpa_t addr, unsigned int len,
1266 mutex_lock(&its->cmd_lock);
1268 reg = update_64bit_reg(its->cwriter, addr & 7, len, val);
1269 reg = ITS_CMD_OFFSET(reg);
1270 if (reg >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
1271 mutex_unlock(&its->cmd_lock);
1276 vgic_its_process_commands(kvm, its);
1278 mutex_unlock(&its->cmd_lock);
1281 static unsigned long vgic_mmio_read_its_cwriter(struct kvm *kvm,
1282 struct vgic_its *its,
1283 gpa_t addr, unsigned int len)
1285 return extract_bytes(its->cwriter, addr & 0x7, len);
1288 static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,
1289 struct vgic_its *its,
1290 gpa_t addr, unsigned int len)
1292 return extract_bytes(its->creadr, addr & 0x7, len);
1295 static int vgic_mmio_uaccess_write_its_creadr(struct kvm *kvm,
1296 struct vgic_its *its,
1297 gpa_t addr, unsigned int len,
1303 mutex_lock(&its->cmd_lock);
1310 cmd_offset = ITS_CMD_OFFSET(val);
1311 if (cmd_offset >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
1316 its->creadr = cmd_offset;
1318 mutex_unlock(&its->cmd_lock);
1322 #define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7)
1323 static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm,
1324 struct vgic_its *its,
1325 gpa_t addr, unsigned int len)
1329 switch (BASER_INDEX(addr)) {
1331 reg = its->baser_device_table;
1334 reg = its->baser_coll_table;
1341 return extract_bytes(reg, addr & 7, len);
1344 #define GITS_BASER_RO_MASK (GENMASK_ULL(52, 48) | GENMASK_ULL(58, 56))
1345 static void vgic_mmio_write_its_baser(struct kvm *kvm,
1346 struct vgic_its *its,
1347 gpa_t addr, unsigned int len,
1350 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
1351 u64 entry_size, device_type;
1352 u64 reg, *regptr, clearbits = 0;
1354 /* When GITS_CTLR.Enable is 1, we ignore write accesses. */
1358 switch (BASER_INDEX(addr)) {
1360 regptr = &its->baser_device_table;
1361 entry_size = abi->dte_esz;
1362 device_type = GITS_BASER_TYPE_DEVICE;
1365 regptr = &its->baser_coll_table;
1366 entry_size = abi->cte_esz;
1367 device_type = GITS_BASER_TYPE_COLLECTION;
1368 clearbits = GITS_BASER_INDIRECT;
1374 reg = update_64bit_reg(*regptr, addr & 7, len, val);
1375 reg &= ~GITS_BASER_RO_MASK;
1378 reg |= (entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
1379 reg |= device_type << GITS_BASER_TYPE_SHIFT;
1380 reg = vgic_sanitise_its_baser(reg);
1385 static unsigned long vgic_mmio_read_its_ctlr(struct kvm *vcpu,
1386 struct vgic_its *its,
1387 gpa_t addr, unsigned int len)
1391 mutex_lock(&its->cmd_lock);
1392 if (its->creadr == its->cwriter)
1393 reg |= GITS_CTLR_QUIESCENT;
1395 reg |= GITS_CTLR_ENABLE;
1396 mutex_unlock(&its->cmd_lock);
1401 static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its,
1402 gpa_t addr, unsigned int len,
1405 mutex_lock(&its->cmd_lock);
1407 its->enabled = !!(val & GITS_CTLR_ENABLE);
1410 * Try to process any pending commands. This function bails out early
1411 * if the ITS is disabled or no commands have been queued.
1413 vgic_its_process_commands(kvm, its);
1415 mutex_unlock(&its->cmd_lock);
1418 #define REGISTER_ITS_DESC(off, rd, wr, length, acc) \
1420 .reg_offset = off, \
1422 .access_flags = acc, \
1427 #define REGISTER_ITS_DESC_UACCESS(off, rd, wr, uwr, length, acc)\
1429 .reg_offset = off, \
1431 .access_flags = acc, \
1434 .uaccess_its_write = uwr, \
1437 static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its,
1438 gpa_t addr, unsigned int len, unsigned long val)
1443 static struct vgic_register_region its_registers[] = {
1444 REGISTER_ITS_DESC(GITS_CTLR,
1445 vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4,
1447 REGISTER_ITS_DESC_UACCESS(GITS_IIDR,
1448 vgic_mmio_read_its_iidr, its_mmio_write_wi,
1449 vgic_mmio_uaccess_write_its_iidr, 4,
1451 REGISTER_ITS_DESC(GITS_TYPER,
1452 vgic_mmio_read_its_typer, its_mmio_write_wi, 8,
1453 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1454 REGISTER_ITS_DESC(GITS_CBASER,
1455 vgic_mmio_read_its_cbaser, vgic_mmio_write_its_cbaser, 8,
1456 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1457 REGISTER_ITS_DESC(GITS_CWRITER,
1458 vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8,
1459 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1460 REGISTER_ITS_DESC_UACCESS(GITS_CREADR,
1461 vgic_mmio_read_its_creadr, its_mmio_write_wi,
1462 vgic_mmio_uaccess_write_its_creadr, 8,
1463 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1464 REGISTER_ITS_DESC(GITS_BASER,
1465 vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,
1466 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1467 REGISTER_ITS_DESC(GITS_IDREGS_BASE,
1468 vgic_mmio_read_its_idregs, its_mmio_write_wi, 0x30,
1472 /* This is called on setting the LPI enable bit in the redistributor. */
1473 void vgic_enable_lpis(struct kvm_vcpu *vcpu)
1475 if (!(vcpu->arch.vgic_cpu.pendbaser & GICR_PENDBASER_PTZ))
1476 its_sync_lpi_pending_table(vcpu);
1479 static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its)
1481 struct vgic_io_device *iodev = &its->iodev;
1484 if (!its->initialized)
1487 if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base))
1490 iodev->regions = its_registers;
1491 iodev->nr_regions = ARRAY_SIZE(its_registers);
1492 kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops);
1494 iodev->base_addr = its->vgic_its_base;
1495 iodev->iodev_type = IODEV_ITS;
1497 mutex_lock(&kvm->slots_lock);
1498 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr,
1499 KVM_VGIC_V3_ITS_SIZE, &iodev->dev);
1500 mutex_unlock(&kvm->slots_lock);
1505 #define INITIAL_BASER_VALUE \
1506 (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb) | \
1507 GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner) | \
1508 GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable) | \
1509 GITS_BASER_PAGE_SIZE_64K)
1511 #define INITIAL_PROPBASER_VALUE \
1512 (GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWb) | \
1513 GIC_BASER_CACHEABILITY(GICR_PROPBASER, OUTER, SameAsInner) | \
1514 GIC_BASER_SHAREABILITY(GICR_PROPBASER, InnerShareable))
1516 static int vgic_its_create(struct kvm_device *dev, u32 type)
1518 struct vgic_its *its;
1520 if (type != KVM_DEV_TYPE_ARM_VGIC_ITS)
1523 its = kzalloc(sizeof(struct vgic_its), GFP_KERNEL);
1527 mutex_init(&its->its_lock);
1528 mutex_init(&its->cmd_lock);
1530 its->vgic_its_base = VGIC_ADDR_UNDEF;
1532 INIT_LIST_HEAD(&its->device_list);
1533 INIT_LIST_HEAD(&its->collection_list);
1535 dev->kvm->arch.vgic.has_its = true;
1536 its->initialized = false;
1537 its->enabled = false;
1540 its->baser_device_table = INITIAL_BASER_VALUE |
1541 ((u64)GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT);
1542 its->baser_coll_table = INITIAL_BASER_VALUE |
1543 ((u64)GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT);
1544 dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE;
1548 return vgic_its_set_abi(its, NR_ITS_ABIS - 1);
1551 static void vgic_its_destroy(struct kvm_device *kvm_dev)
1553 struct kvm *kvm = kvm_dev->kvm;
1554 struct vgic_its *its = kvm_dev->private;
1555 struct its_device *dev;
1556 struct its_ite *ite;
1557 struct list_head *dev_cur, *dev_temp;
1558 struct list_head *cur, *temp;
1561 * We may end up here without the lists ever having been initialized.
1562 * Check this and bail out early to avoid dereferencing a NULL pointer.
1564 if (!its->device_list.next)
1567 mutex_lock(&its->its_lock);
1568 list_for_each_safe(dev_cur, dev_temp, &its->device_list) {
1569 dev = container_of(dev_cur, struct its_device, dev_list);
1570 list_for_each_safe(cur, temp, &dev->itt_head) {
1571 ite = (container_of(cur, struct its_ite, ite_list));
1572 its_free_ite(kvm, ite);
1578 list_for_each_safe(cur, temp, &its->collection_list) {
1580 kfree(container_of(cur, struct its_collection, coll_list));
1582 mutex_unlock(&its->its_lock);
1587 int vgic_its_has_attr_regs(struct kvm_device *dev,
1588 struct kvm_device_attr *attr)
1590 const struct vgic_register_region *region;
1591 gpa_t offset = attr->attr;
1594 align = (offset < GITS_TYPER) || (offset >= GITS_PIDR4) ? 0x3 : 0x7;
1599 region = vgic_find_mmio_region(its_registers,
1600 ARRAY_SIZE(its_registers),
1608 int vgic_its_attr_regs_access(struct kvm_device *dev,
1609 struct kvm_device_attr *attr,
1610 u64 *reg, bool is_write)
1612 const struct vgic_register_region *region;
1613 struct vgic_its *its;
1619 offset = attr->attr;
1622 * Although the spec supports upper/lower 32-bit accesses to
1623 * 64-bit ITS registers, the userspace ABI requires 64-bit
1624 * accesses to all 64-bit wide registers. We therefore only
1625 * support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID
1628 if ((offset < GITS_TYPER) || (offset >= GITS_PIDR4))
1636 mutex_lock(&dev->kvm->lock);
1638 if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) {
1643 region = vgic_find_mmio_region(its_registers,
1644 ARRAY_SIZE(its_registers),
1651 if (!lock_all_vcpus(dev->kvm)) {
1656 addr = its->vgic_its_base + offset;
1658 len = region->access_flags & VGIC_ACCESS_64bit ? 8 : 4;
1661 if (region->uaccess_its_write)
1662 ret = region->uaccess_its_write(dev->kvm, its, addr,
1665 region->its_write(dev->kvm, its, addr, len, *reg);
1667 *reg = region->its_read(dev->kvm, its, addr, len);
1669 unlock_all_vcpus(dev->kvm);
1671 mutex_unlock(&dev->kvm->lock);
1676 * vgic_its_save_tables_v0 - Save the ITS tables into guest ARM
1677 * according to v0 ABI
1679 static int vgic_its_save_tables_v0(struct vgic_its *its)
1685 * vgic_its_restore_tables_v0 - Restore the ITS tables from guest RAM
1686 * to internal data structs according to V0 ABI
1689 static int vgic_its_restore_tables_v0(struct vgic_its *its)
1694 static int vgic_its_commit_v0(struct vgic_its *its)
1696 const struct vgic_its_abi *abi;
1698 abi = vgic_its_get_abi(its);
1699 its->baser_coll_table &= ~GITS_BASER_ENTRY_SIZE_MASK;
1700 its->baser_device_table &= ~GITS_BASER_ENTRY_SIZE_MASK;
1702 its->baser_coll_table |= (GIC_ENCODE_SZ(abi->cte_esz, 5)
1703 << GITS_BASER_ENTRY_SIZE_SHIFT);
1705 its->baser_device_table |= (GIC_ENCODE_SZ(abi->dte_esz, 5)
1706 << GITS_BASER_ENTRY_SIZE_SHIFT);
1710 static int vgic_its_has_attr(struct kvm_device *dev,
1711 struct kvm_device_attr *attr)
1713 switch (attr->group) {
1714 case KVM_DEV_ARM_VGIC_GRP_ADDR:
1715 switch (attr->attr) {
1716 case KVM_VGIC_ITS_ADDR_TYPE:
1720 case KVM_DEV_ARM_VGIC_GRP_CTRL:
1721 switch (attr->attr) {
1722 case KVM_DEV_ARM_VGIC_CTRL_INIT:
1726 case KVM_DEV_ARM_VGIC_GRP_ITS_REGS:
1727 return vgic_its_has_attr_regs(dev, attr);
1732 static int vgic_its_set_attr(struct kvm_device *dev,
1733 struct kvm_device_attr *attr)
1735 struct vgic_its *its = dev->private;
1738 switch (attr->group) {
1739 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
1740 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1741 unsigned long type = (unsigned long)attr->attr;
1744 if (type != KVM_VGIC_ITS_ADDR_TYPE)
1747 if (copy_from_user(&addr, uaddr, sizeof(addr)))
1750 ret = vgic_check_ioaddr(dev->kvm, &its->vgic_its_base,
1755 its->vgic_its_base = addr;
1759 case KVM_DEV_ARM_VGIC_GRP_CTRL:
1760 switch (attr->attr) {
1761 case KVM_DEV_ARM_VGIC_CTRL_INIT:
1762 its->initialized = true;
1767 case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: {
1768 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1771 if (get_user(reg, uaddr))
1774 return vgic_its_attr_regs_access(dev, attr, ®, true);
1780 static int vgic_its_get_attr(struct kvm_device *dev,
1781 struct kvm_device_attr *attr)
1783 switch (attr->group) {
1784 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
1785 struct vgic_its *its = dev->private;
1786 u64 addr = its->vgic_its_base;
1787 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1788 unsigned long type = (unsigned long)attr->attr;
1790 if (type != KVM_VGIC_ITS_ADDR_TYPE)
1793 if (copy_to_user(uaddr, &addr, sizeof(addr)))
1797 case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: {
1798 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1802 ret = vgic_its_attr_regs_access(dev, attr, ®, false);
1805 return put_user(reg, uaddr);
1814 static struct kvm_device_ops kvm_arm_vgic_its_ops = {
1815 .name = "kvm-arm-vgic-its",
1816 .create = vgic_its_create,
1817 .destroy = vgic_its_destroy,
1818 .set_attr = vgic_its_set_attr,
1819 .get_attr = vgic_its_get_attr,
1820 .has_attr = vgic_its_has_attr,
1823 int kvm_vgic_register_its_device(void)
1825 return kvm_register_device_ops(&kvm_arm_vgic_its_ops,
1826 KVM_DEV_TYPE_ARM_VGIC_ITS);
1830 * Registers all ITSes with the kvm_io_bus framework.
1831 * To follow the existing VGIC initialization sequence, this has to be
1832 * done as late as possible, just before the first VCPU runs.
1834 int vgic_register_its_iodevs(struct kvm *kvm)
1836 struct kvm_device *dev;
1839 list_for_each_entry(dev, &kvm->devices, vm_node) {
1840 if (dev->ops != &kvm_arm_vgic_its_ops)
1843 ret = vgic_register_its_iodev(kvm, dev->private);
1847 * We don't need to care about tearing down previously
1848 * registered ITSes, as the kvm_io_bus framework removes
1849 * them for us if the VM gets destroyed.