1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * GICv3 ITS emulation
4 *
5 * Copyright (C) 2015,2016 ARM Ltd.
6 * Author: Andre Przywara <andre.przywara@arm.com>
7 */
8
9#include <linux/cpu.h>
10#include <linux/kvm.h>
11#include <linux/kvm_host.h>
12#include <linux/interrupt.h>
13#include <linux/list.h>
14#include <linux/uaccess.h>
15#include <linux/list_sort.h>
16
17#include <linux/irqchip/arm-gic-v3.h>
18
19#include <asm/kvm_emulate.h>
20#include <asm/kvm_arm.h>
21#include <asm/kvm_mmu.h>
22
23#include "vgic.h"
24#include "vgic-mmio.h"
25
26static int vgic_its_save_tables_v0(struct vgic_its *its);
27static int vgic_its_restore_tables_v0(struct vgic_its *its);
28static int vgic_its_commit_v0(struct vgic_its *its);
29static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
30			     struct kvm_vcpu *filter_vcpu, bool needs_inv);
31
32/*
33 * Creates a new (reference to a) struct vgic_irq for a given LPI.
34 * If this LPI is already mapped on another ITS, we increase its refcount
35 * and return a pointer to the existing structure.
36 * If this is a "new" LPI, we allocate and initialize a new struct vgic_irq.
37 * This function returns a pointer to the _unlocked_ structure.
38 */
39static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid,
40				     struct kvm_vcpu *vcpu)
41{
42	struct vgic_dist *dist = &kvm->arch.vgic;
43	struct vgic_irq *irq = vgic_get_irq(kvm, NULL, intid), *oldirq;
44	unsigned long flags;
45	int ret;
46
47	/* In this case there is no put, since we keep the reference. */
48	if (irq)
49		return irq;
50
51	irq = kzalloc(sizeof(struct vgic_irq), GFP_KERNEL);
52	if (!irq)
53		return ERR_PTR(-ENOMEM);
54
55	INIT_LIST_HEAD(&irq->lpi_list);
56	INIT_LIST_HEAD(&irq->ap_list);
57	raw_spin_lock_init(&irq->irq_lock);
58
59	irq->config = VGIC_CONFIG_EDGE;
60	kref_init(&irq->refcount);
61	irq->intid = intid;
62	irq->target_vcpu = vcpu;
63	irq->group = 1;
64
65	raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
66
67	/*
68	 * There could be a race with another vgic_add_lpi(), so we need to
69	 * check that we don't add a second list entry with the same LPI.
70	 */
71	list_for_each_entry(oldirq, &dist->lpi_list_head, lpi_list) {
72		if (oldirq->intid != intid)
73			continue;
74
75		/* Someone was faster with adding this LPI, lets use that. */
76		kfree(irq);
77		irq = oldirq;
78
79		/*
80		 * This increases the refcount, the caller is expected to
81		 * call vgic_put_irq() on the returned pointer once it's
82		 * finished with the IRQ.
83		 */
84		vgic_get_irq_kref(irq);
85
86		goto out_unlock;
87	}
88
89	list_add_tail(&irq->lpi_list, &dist->lpi_list_head);
90	dist->lpi_list_count++;
91
92out_unlock:
93	raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
94
95	/*
96	 * We "cache" the configuration table entries in our struct vgic_irq's.
97	 * However we only have those structs for mapped IRQs, so we read in
98	 * the respective config data from memory here upon mapping the LPI.
99	 *
100	 * Should any of these fail, behave as if we couldn't create the LPI
101	 * by dropping the refcount and returning the error.
102	 */
103	ret = update_lpi_config(kvm, irq, NULL, false);
104	if (ret) {
105		vgic_put_irq(kvm, irq);
106		return ERR_PTR(ret);
107	}
108
109	ret = vgic_v3_lpi_sync_pending_status(kvm, irq);
110	if (ret) {
111		vgic_put_irq(kvm, irq);
112		return ERR_PTR(ret);
113	}
114
115	return irq;
116}
117
118struct its_device {
119	struct list_head dev_list;
120
121	/* the head for the list of ITTEs */
122	struct list_head itt_head;
123	u32 num_eventid_bits;
124	gpa_t itt_addr;
125	u32 device_id;
126};
127
128#define COLLECTION_NOT_MAPPED ((u32)~0)
129
130struct its_collection {
131	struct list_head coll_list;
132
133	u32 collection_id;
134	u32 target_addr;
135};
136
137#define its_is_collection_mapped(coll) ((coll) && \
138				((coll)->target_addr != COLLECTION_NOT_MAPPED))
139
140struct its_ite {
141	struct list_head ite_list;
142
143	struct vgic_irq *irq;
144	struct its_collection *collection;
145	u32 event_id;
146};
147
148struct vgic_translation_cache_entry {
149	struct list_head	entry;
150	phys_addr_t		db;
151	u32			devid;
152	u32			eventid;
153	struct vgic_irq		*irq;
154};
155
156/**
157 * struct vgic_its_abi - ITS abi ops and settings
158 * @cte_esz: collection table entry size
159 * @dte_esz: device table entry size
160 * @ite_esz: interrupt translation table entry size
161 * @save tables: save the ITS tables into guest RAM
162 * @restore_tables: restore the ITS internal structs from tables
163 *  stored in guest RAM
164 * @commit: initialize the registers which expose the ABI settings,
165 *  especially the entry sizes
166 */
167struct vgic_its_abi {
168	int cte_esz;
169	int dte_esz;
170	int ite_esz;
171	int (*save_tables)(struct vgic_its *its);
172	int (*restore_tables)(struct vgic_its *its);
173	int (*commit)(struct vgic_its *its);
174};
175
176#define ABI_0_ESZ	8
177#define ESZ_MAX		ABI_0_ESZ
178
179static const struct vgic_its_abi its_table_abi_versions[] = {
180	[0] = {
181	 .cte_esz = ABI_0_ESZ,
182	 .dte_esz = ABI_0_ESZ,
183	 .ite_esz = ABI_0_ESZ,
184	 .save_tables = vgic_its_save_tables_v0,
185	 .restore_tables = vgic_its_restore_tables_v0,
186	 .commit = vgic_its_commit_v0,
187	},
188};
189
190#define NR_ITS_ABIS	ARRAY_SIZE(its_table_abi_versions)
191
192inline const struct vgic_its_abi *vgic_its_get_abi(struct vgic_its *its)
193{
194	return &its_table_abi_versions[its->abi_rev];
195}
196
197static int vgic_its_set_abi(struct vgic_its *its, u32 rev)
198{
199	const struct vgic_its_abi *abi;
200
201	its->abi_rev = rev;
202	abi = vgic_its_get_abi(its);
203	return abi->commit(its);
204}
205
206/*
207 * Find and returns a device in the device table for an ITS.
208 * Must be called with the its_lock mutex held.
209 */
210static struct its_device *find_its_device(struct vgic_its *its, u32 device_id)
211{
212	struct its_device *device;
213
214	list_for_each_entry(device, &its->device_list, dev_list)
215		if (device_id == device->device_id)
216			return device;
217
218	return NULL;
219}
220
221/*
222 * Find and returns an interrupt translation table entry (ITTE) for a given
223 * Device ID/Event ID pair on an ITS.
224 * Must be called with the its_lock mutex held.
225 */
226static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,
227				  u32 event_id)
228{
229	struct its_device *device;
230	struct its_ite *ite;
231
232	device = find_its_device(its, device_id);
233	if (device == NULL)
234		return NULL;
235
236	list_for_each_entry(ite, &device->itt_head, ite_list)
237		if (ite->event_id == event_id)
238			return ite;
239
240	return NULL;
241}
242
243/* To be used as an iterator this macro misses the enclosing parentheses */
244#define for_each_lpi_its(dev, ite, its) \
245	list_for_each_entry(dev, &(its)->device_list, dev_list) \
246		list_for_each_entry(ite, &(dev)->itt_head, ite_list)
247
248#define GIC_LPI_OFFSET 8192
249
250#define VITS_TYPER_IDBITS 16
251#define VITS_TYPER_DEVBITS 16
252#define VITS_DTE_MAX_DEVID_OFFSET	(BIT(14) - 1)
253#define VITS_ITE_MAX_EVENTID_OFFSET	(BIT(16) - 1)
254
255/*
256 * Finds and returns a collection in the ITS collection table.
257 * Must be called with the its_lock mutex held.
258 */
259static struct its_collection *find_collection(struct vgic_its *its, int coll_id)
260{
261	struct its_collection *collection;
262
263	list_for_each_entry(collection, &its->collection_list, coll_list) {
264		if (coll_id == collection->collection_id)
265			return collection;
266	}
267
268	return NULL;
269}
270
271#define LPI_PROP_ENABLE_BIT(p)	((p) & LPI_PROP_ENABLED)
272#define LPI_PROP_PRIORITY(p)	((p) & 0xfc)
273
274/*
275 * Reads the configuration data for a given LPI from guest memory and
276 * updates the fields in struct vgic_irq.
277 * If filter_vcpu is not NULL, applies only if the IRQ is targeting this
278 * VCPU. Unconditionally applies if filter_vcpu is NULL.
279 */
280static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
281			     struct kvm_vcpu *filter_vcpu, bool needs_inv)
282{
283	u64 propbase = GICR_PROPBASER_ADDRESS(kvm->arch.vgic.propbaser);
284	u8 prop;
285	int ret;
286	unsigned long flags;
287
288	ret = kvm_read_guest_lock(kvm, propbase + irq->intid - GIC_LPI_OFFSET,
289				  &prop, 1);
290
291	if (ret)
292		return ret;
293
294	raw_spin_lock_irqsave(&irq->irq_lock, flags);
295
296	if (!filter_vcpu || filter_vcpu == irq->target_vcpu) {
297		irq->priority = LPI_PROP_PRIORITY(prop);
298		irq->enabled = LPI_PROP_ENABLE_BIT(prop);
299
300		if (!irq->hw) {
301			vgic_queue_irq_unlock(kvm, irq, flags);
302			return 0;
303		}
304	}
305
306	raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
307
308	if (irq->hw)
309		return its_prop_update_vlpi(irq->host_irq, prop, needs_inv);
310
311	return 0;
312}
313
314/*
315 * Create a snapshot of the current LPIs targeting @vcpu, so that we can
316 * enumerate those LPIs without holding any lock.
317 * Returns their number and puts the kmalloc'ed array into intid_ptr.
318 */
319int vgic_copy_lpi_list(struct kvm *kvm, struct kvm_vcpu *vcpu, u32 **intid_ptr)
320{
321	struct vgic_dist *dist = &kvm->arch.vgic;
322	struct vgic_irq *irq;
323	unsigned long flags;
324	u32 *intids;
325	int irq_count, i = 0;
326
327	/*
328	 * There is an obvious race between allocating the array and LPIs
329	 * being mapped/unmapped. If we ended up here as a result of a
330	 * command, we're safe (locks are held, preventing another
331	 * command). If coming from another path (such as enabling LPIs),
332	 * we must be careful not to overrun the array.
333	 */
334	irq_count = READ_ONCE(dist->lpi_list_count);
335	intids = kmalloc_array(irq_count, sizeof(intids[0]), GFP_KERNEL);
336	if (!intids)
337		return -ENOMEM;
338
339	raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
340	list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
341		if (i == irq_count)
342			break;
343		/* We don't need to "get" the IRQ, as we hold the list lock. */
344		if (vcpu && irq->target_vcpu != vcpu)
345			continue;
346		intids[i++] = irq->intid;
347	}
348	raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
349
350	*intid_ptr = intids;
351	return i;
352}
353
354static int update_affinity(struct vgic_irq *irq, struct kvm_vcpu *vcpu)
355{
356	int ret = 0;
357	unsigned long flags;
358
359	raw_spin_lock_irqsave(&irq->irq_lock, flags);
360	irq->target_vcpu = vcpu;
361	raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
362
363	if (irq->hw) {
364		struct its_vlpi_map map;
365
366		ret = its_get_vlpi(irq->host_irq, &map);
367		if (ret)
368			return ret;
369
370		if (map.vpe)
371			atomic_dec(&map.vpe->vlpi_count);
372		map.vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
373		atomic_inc(&map.vpe->vlpi_count);
374
375		ret = its_map_vlpi(irq->host_irq, &map);
376	}
377
378	return ret;
379}
380
381/*
382 * Promotes the ITS view of affinity of an ITTE (which redistributor this LPI
383 * is targeting) to the VGIC's view, which deals with target VCPUs.
384 * Needs to be called whenever either the collection for a LPIs has
385 * changed or the collection itself got retargeted.
386 */
387static void update_affinity_ite(struct kvm *kvm, struct its_ite *ite)
388{
389	struct kvm_vcpu *vcpu;
390
391	if (!its_is_collection_mapped(ite->collection))
392		return;
393
394	vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr);
395	update_affinity(ite->irq, vcpu);
396}
397
398/*
399 * Updates the target VCPU for every LPI targeting this collection.
400 * Must be called with the its_lock mutex held.
401 */
402static void update_affinity_collection(struct kvm *kvm, struct vgic_its *its,
403				       struct its_collection *coll)
404{
405	struct its_device *device;
406	struct its_ite *ite;
407
408	for_each_lpi_its(device, ite, its) {
409		if (!ite->collection || coll != ite->collection)
410			continue;
411
412		update_affinity_ite(kvm, ite);
413	}
414}
415
416static u32 max_lpis_propbaser(u64 propbaser)
417{
418	int nr_idbits = (propbaser & 0x1f) + 1;
419
420	return 1U << min(nr_idbits, INTERRUPT_ID_BITS_ITS);
421}
422
423/*
424 * Sync the pending table pending bit of LPIs targeting @vcpu
425 * with our own data structures. This relies on the LPI being
426 * mapped before.
427 */
428static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
429{
430	gpa_t pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
431	struct vgic_irq *irq;
432	int last_byte_offset = -1;
433	int ret = 0;
434	u32 *intids;
435	int nr_irqs, i;
436	unsigned long flags;
437	u8 pendmask;
438
439	nr_irqs = vgic_copy_lpi_list(vcpu->kvm, vcpu, &intids);
440	if (nr_irqs < 0)
441		return nr_irqs;
442
443	for (i = 0; i < nr_irqs; i++) {
444		int byte_offset, bit_nr;
445
446		byte_offset = intids[i] / BITS_PER_BYTE;
447		bit_nr = intids[i] % BITS_PER_BYTE;
448
449		/*
450		 * For contiguously allocated LPIs chances are we just read
451		 * this very same byte in the last iteration. Reuse that.
452		 */
453		if (byte_offset != last_byte_offset) {
454			ret = kvm_read_guest_lock(vcpu->kvm,
455						  pendbase + byte_offset,
456						  &pendmask, 1);
457			if (ret) {
458				kfree(intids);
459				return ret;
460			}
461			last_byte_offset = byte_offset;
462		}
463
464		irq = vgic_get_irq(vcpu->kvm, NULL, intids[i]);
465		raw_spin_lock_irqsave(&irq->irq_lock, flags);
466		irq->pending_latch = pendmask & (1U << bit_nr);
467		vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
468		vgic_put_irq(vcpu->kvm, irq);
469	}
470
471	kfree(intids);
472
473	return ret;
474}
475
476static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,
477					      struct vgic_its *its,
478					      gpa_t addr, unsigned int len)
479{
480	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
481	u64 reg = GITS_TYPER_PLPIS;
482
483	/*
484	 * We use linear CPU numbers for redistributor addressing,
485	 * so GITS_TYPER.PTA is 0.
486	 * Also we force all PROPBASER registers to be the same, so
487	 * CommonLPIAff is 0 as well.
488	 * To avoid memory waste in the guest, we keep the number of IDBits and
489	 * DevBits low - as least for the time being.
490	 */
491	reg |= GIC_ENCODE_SZ(VITS_TYPER_DEVBITS, 5) << GITS_TYPER_DEVBITS_SHIFT;
492	reg |= GIC_ENCODE_SZ(VITS_TYPER_IDBITS, 5) << GITS_TYPER_IDBITS_SHIFT;
493	reg |= GIC_ENCODE_SZ(abi->ite_esz, 4) << GITS_TYPER_ITT_ENTRY_SIZE_SHIFT;
494
495	return extract_bytes(reg, addr & 7, len);
496}
497
498static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm,
499					     struct vgic_its *its,
500					     gpa_t addr, unsigned int len)
501{
502	u32 val;
503
504	val = (its->abi_rev << GITS_IIDR_REV_SHIFT) & GITS_IIDR_REV_MASK;
505	val |= (PRODUCT_ID_KVM << GITS_IIDR_PRODUCTID_SHIFT) | IMPLEMENTER_ARM;
506	return val;
507}
508
509static int vgic_mmio_uaccess_write_its_iidr(struct kvm *kvm,
510					    struct vgic_its *its,
511					    gpa_t addr, unsigned int len,
512					    unsigned long val)
513{
514	u32 rev = GITS_IIDR_REV(val);
515
516	if (rev >= NR_ITS_ABIS)
517		return -EINVAL;
518	return vgic_its_set_abi(its, rev);
519}
520
521static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm,
522					       struct vgic_its *its,
523					       gpa_t addr, unsigned int len)
524{
525	switch (addr & 0xffff) {
526	case GITS_PIDR0:
527		return 0x92;	/* part number, bits[7:0] */
528	case GITS_PIDR1:
529		return 0xb4;	/* part number, bits[11:8] */
530	case GITS_PIDR2:
531		return GIC_PIDR2_ARCH_GICv3 | 0x0b;
532	case GITS_PIDR4:
533		return 0x40;	/* This is a 64K software visible page */
534	/* The following are the ID registers for (any) GIC. */
535	case GITS_CIDR0:
536		return 0x0d;
537	case GITS_CIDR1:
538		return 0xf0;
539	case GITS_CIDR2:
540		return 0x05;
541	case GITS_CIDR3:
542		return 0xb1;
543	}
544
545	return 0;
546}
547
548static struct vgic_irq *__vgic_its_check_cache(struct vgic_dist *dist,
549					       phys_addr_t db,
550					       u32 devid, u32 eventid)
551{
552	struct vgic_translation_cache_entry *cte;
553
554	list_for_each_entry(cte, &dist->lpi_translation_cache, entry) {
555		/*
556		 * If we hit a NULL entry, there is nothing after this
557		 * point.
558		 */
559		if (!cte->irq)
560			break;
561
562		if (cte->db != db || cte->devid != devid ||
563		    cte->eventid != eventid)
564			continue;
565
566		/*
567		 * Move this entry to the head, as it is the most
568		 * recently used.
569		 */
570		if (!list_is_first(&cte->entry, &dist->lpi_translation_cache))
571			list_move(&cte->entry, &dist->lpi_translation_cache);
572
573		return cte->irq;
574	}
575
576	return NULL;
577}
578
579static struct vgic_irq *vgic_its_check_cache(struct kvm *kvm, phys_addr_t db,
580					     u32 devid, u32 eventid)
581{
582	struct vgic_dist *dist = &kvm->arch.vgic;
583	struct vgic_irq *irq;
584	unsigned long flags;
585
586	raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
587
588	irq = __vgic_its_check_cache(dist, db, devid, eventid);
589	if (irq)
590		vgic_get_irq_kref(irq);
591
592	raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
593
594	return irq;
595}
596
597static void vgic_its_cache_translation(struct kvm *kvm, struct vgic_its *its,
598				       u32 devid, u32 eventid,
599				       struct vgic_irq *irq)
600{
601	struct vgic_dist *dist = &kvm->arch.vgic;
602	struct vgic_translation_cache_entry *cte;
603	unsigned long flags;
604	phys_addr_t db;
605
606	/* Do not cache a directly injected interrupt */
607	if (irq->hw)
608		return;
609
610	raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
611
612	if (unlikely(list_empty(&dist->lpi_translation_cache)))
613		goto out;
614
615	/*
616	 * We could have raced with another CPU caching the same
617	 * translation behind our back, so let's check it is not in
618	 * already
619	 */
620	db = its->vgic_its_base + GITS_TRANSLATER;
621	if (__vgic_its_check_cache(dist, db, devid, eventid))
622		goto out;
623
624	/* Always reuse the last entry (LRU policy) */
625	cte = list_last_entry(&dist->lpi_translation_cache,
626			      typeof(*cte), entry);
627
628	/*
629	 * Caching the translation implies having an extra reference
630	 * to the interrupt, so drop the potential reference on what
631	 * was in the cache, and increment it on the new interrupt.
632	 */
633	if (cte->irq)
634		__vgic_put_lpi_locked(kvm, cte->irq);
635
636	vgic_get_irq_kref(irq);
637
638	cte->db		= db;
639	cte->devid	= devid;
640	cte->eventid	= eventid;
641	cte->irq	= irq;
642
643	/* Move the new translation to the head of the list */
644	list_move(&cte->entry, &dist->lpi_translation_cache);
645
646out:
647	raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
648}
649
650void vgic_its_invalidate_cache(struct kvm *kvm)
651{
652	struct vgic_dist *dist = &kvm->arch.vgic;
653	struct vgic_translation_cache_entry *cte;
654	unsigned long flags;
655
656	raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
657
658	list_for_each_entry(cte, &dist->lpi_translation_cache, entry) {
659		/*
660		 * If we hit a NULL entry, there is nothing after this
661		 * point.
662		 */
663		if (!cte->irq)
664			break;
665
666		__vgic_put_lpi_locked(kvm, cte->irq);
667		cte->irq = NULL;
668	}
669
670	raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
671}
672
673int vgic_its_resolve_lpi(struct kvm *kvm, struct vgic_its *its,
674			 u32 devid, u32 eventid, struct vgic_irq **irq)
675{
676	struct kvm_vcpu *vcpu;
677	struct its_ite *ite;
678
679	if (!its->enabled)
680		return -EBUSY;
681
682	ite = find_ite(its, devid, eventid);
683	if (!ite || !its_is_collection_mapped(ite->collection))
684		return E_ITS_INT_UNMAPPED_INTERRUPT;
685
686	vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr);
687	if (!vcpu)
688		return E_ITS_INT_UNMAPPED_INTERRUPT;
689
690	if (!vcpu->arch.vgic_cpu.lpis_enabled)
691		return -EBUSY;
692
693	vgic_its_cache_translation(kvm, its, devid, eventid, ite->irq);
694
695	*irq = ite->irq;
696	return 0;
697}
698
699struct vgic_its *vgic_msi_to_its(struct kvm *kvm, struct kvm_msi *msi)
700{
701	u64 address;
702	struct kvm_io_device *kvm_io_dev;
703	struct vgic_io_device *iodev;
704
705	if (!vgic_has_its(kvm))
706		return ERR_PTR(-ENODEV);
707
708	if (!(msi->flags & KVM_MSI_VALID_DEVID))
709		return ERR_PTR(-EINVAL);
710
711	address = (u64)msi->address_hi << 32 | msi->address_lo;
712
713	kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, address);
714	if (!kvm_io_dev)
715		return ERR_PTR(-EINVAL);
716
717	if (kvm_io_dev->ops != &kvm_io_gic_ops)
718		return ERR_PTR(-EINVAL);
719
720	iodev = container_of(kvm_io_dev, struct vgic_io_device, dev);
721	if (iodev->iodev_type != IODEV_ITS)
722		return ERR_PTR(-EINVAL);
723
724	return iodev->its;
725}
726
727/*
728 * Find the target VCPU and the LPI number for a given devid/eventid pair
729 * and make this IRQ pending, possibly injecting it.
730 * Must be called with the its_lock mutex held.
731 * Returns 0 on success, a positive error value for any ITS mapping
732 * related errors and negative error values for generic errors.
733 */
734static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its,
735				u32 devid, u32 eventid)
736{
737	struct vgic_irq *irq = NULL;
738	unsigned long flags;
739	int err;
740
741	err = vgic_its_resolve_lpi(kvm, its, devid, eventid, &irq);
742	if (err)
743		return err;
744
745	if (irq->hw)
746		return irq_set_irqchip_state(irq->host_irq,
747					     IRQCHIP_STATE_PENDING, true);
748
749	raw_spin_lock_irqsave(&irq->irq_lock, flags);
750	irq->pending_latch = true;
751	vgic_queue_irq_unlock(kvm, irq, flags);
752
753	return 0;
754}
755
756int vgic_its_inject_cached_translation(struct kvm *kvm, struct kvm_msi *msi)
757{
758	struct vgic_irq *irq;
759	unsigned long flags;
760	phys_addr_t db;
761
762	db = (u64)msi->address_hi << 32 | msi->address_lo;
763	irq = vgic_its_check_cache(kvm, db, msi->devid, msi->data);
764	if (!irq)
765		return -EWOULDBLOCK;
766
767	raw_spin_lock_irqsave(&irq->irq_lock, flags);
768	irq->pending_latch = true;
769	vgic_queue_irq_unlock(kvm, irq, flags);
770	vgic_put_irq(kvm, irq);
771
772	return 0;
773}
774
775/*
776 * Queries the KVM IO bus framework to get the ITS pointer from the given
777 * doorbell address.
778 * We then call vgic_its_trigger_msi() with the decoded data.
779 * According to the KVM_SIGNAL_MSI API description returns 1 on success.
780 */
781int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi)
782{
783	struct vgic_its *its;
784	int ret;
785
786	if (!vgic_its_inject_cached_translation(kvm, msi))
787		return 1;
788
789	its = vgic_msi_to_its(kvm, msi);
790	if (IS_ERR(its))
791		return PTR_ERR(its);
792
793	mutex_lock(&its->its_lock);
794	ret = vgic_its_trigger_msi(kvm, its, msi->devid, msi->data);
795	mutex_unlock(&its->its_lock);
796
797	if (ret < 0)
798		return ret;
799
800	/*
801	 * KVM_SIGNAL_MSI demands a return value > 0 for success and 0
802	 * if the guest has blocked the MSI. So we map any LPI mapping
803	 * related error to that.
804	 */
805	if (ret)
806		return 0;
807	else
808		return 1;
809}
810
811/* Requires the its_lock to be held. */
812static void its_free_ite(struct kvm *kvm, struct its_ite *ite)
813{
814	list_del(&ite->ite_list);
815
816	/* This put matches the get in vgic_add_lpi. */
817	if (ite->irq) {
818		if (ite->irq->hw)
819			WARN_ON(its_unmap_vlpi(ite->irq->host_irq));
820
821		vgic_put_irq(kvm, ite->irq);
822	}
823
824	kfree(ite);
825}
826
827static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size)
828{
829	return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT_ULL(size) - 1);
830}
831
832#define its_cmd_get_command(cmd)	its_cmd_mask_field(cmd, 0,  0,  8)
833#define its_cmd_get_deviceid(cmd)	its_cmd_mask_field(cmd, 0, 32, 32)
834#define its_cmd_get_size(cmd)		(its_cmd_mask_field(cmd, 1,  0,  5) + 1)
835#define its_cmd_get_id(cmd)		its_cmd_mask_field(cmd, 1,  0, 32)
836#define its_cmd_get_physical_id(cmd)	its_cmd_mask_field(cmd, 1, 32, 32)
837#define its_cmd_get_collection(cmd)	its_cmd_mask_field(cmd, 2,  0, 16)
838#define its_cmd_get_ittaddr(cmd)	(its_cmd_mask_field(cmd, 2,  8, 44) << 8)
839#define its_cmd_get_target_addr(cmd)	its_cmd_mask_field(cmd, 2, 16, 32)
840#define its_cmd_get_validbit(cmd)	its_cmd_mask_field(cmd, 2, 63,  1)
841
842/*
843 * The DISCARD command frees an Interrupt Translation Table Entry (ITTE).
844 * Must be called with the its_lock mutex held.
845 */
846static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its,
847				       u64 *its_cmd)
848{
849	u32 device_id = its_cmd_get_deviceid(its_cmd);
850	u32 event_id = its_cmd_get_id(its_cmd);
851	struct its_ite *ite;
852
853	ite = find_ite(its, device_id, event_id);
854	if (ite && its_is_collection_mapped(ite->collection)) {
855		/*
856		 * Though the spec talks about removing the pending state, we
857		 * don't bother here since we clear the ITTE anyway and the
858		 * pending state is a property of the ITTE struct.
859		 */
860		vgic_its_invalidate_cache(kvm);
861
862		its_free_ite(kvm, ite);
863		return 0;
864	}
865
866	return E_ITS_DISCARD_UNMAPPED_INTERRUPT;
867}
868
869/*
870 * The MOVI command moves an ITTE to a different collection.
871 * Must be called with the its_lock mutex held.
872 */
873static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,
874				    u64 *its_cmd)
875{
876	u32 device_id = its_cmd_get_deviceid(its_cmd);
877	u32 event_id = its_cmd_get_id(its_cmd);
878	u32 coll_id = its_cmd_get_collection(its_cmd);
879	struct kvm_vcpu *vcpu;
880	struct its_ite *ite;
881	struct its_collection *collection;
882
883	ite = find_ite(its, device_id, event_id);
884	if (!ite)
885		return E_ITS_MOVI_UNMAPPED_INTERRUPT;
886
887	if (!its_is_collection_mapped(ite->collection))
888		return E_ITS_MOVI_UNMAPPED_COLLECTION;
889
890	collection = find_collection(its, coll_id);
891	if (!its_is_collection_mapped(collection))
892		return E_ITS_MOVI_UNMAPPED_COLLECTION;
893
894	ite->collection = collection;
895	vcpu = kvm_get_vcpu(kvm, collection->target_addr);
896
897	vgic_its_invalidate_cache(kvm);
898
899	return update_affinity(ite->irq, vcpu);
900}
901
902/*
903 * Check whether an ID can be stored into the corresponding guest table.
904 * For a direct table this is pretty easy, but gets a bit nasty for
905 * indirect tables. We check whether the resulting guest physical address
906 * is actually valid (covered by a memslot and guest accessible).
907 * For this we have to read the respective first level entry.
908 */
909static bool vgic_its_check_id(struct vgic_its *its, u64 baser, u32 id,
910			      gpa_t *eaddr)
911{
912	int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
913	u64 indirect_ptr, type = GITS_BASER_TYPE(baser);
914	phys_addr_t base = GITS_BASER_ADDR_48_to_52(baser);
915	int esz = GITS_BASER_ENTRY_SIZE(baser);
916	int index, idx;
917	gfn_t gfn;
918	bool ret;
919
920	switch (type) {
921	case GITS_BASER_TYPE_DEVICE:
922		if (id >= BIT_ULL(VITS_TYPER_DEVBITS))
923			return false;
924		break;
925	case GITS_BASER_TYPE_COLLECTION:
926		/* as GITS_TYPER.CIL == 0, ITS supports 16-bit collection ID */
927		if (id >= BIT_ULL(16))
928			return false;
929		break;
930	default:
931		return false;
932	}
933
934	if (!(baser & GITS_BASER_INDIRECT)) {
935		phys_addr_t addr;
936
937		if (id >= (l1_tbl_size / esz))
938			return false;
939
940		addr = base + id * esz;
941		gfn = addr >> PAGE_SHIFT;
942
943		if (eaddr)
944			*eaddr = addr;
945
946		goto out;
947	}
948
949	/* calculate and check the index into the 1st level */
950	index = id / (SZ_64K / esz);
951	if (index >= (l1_tbl_size / sizeof(u64)))
952		return false;
953
954	/* Each 1st level entry is represented by a 64-bit value. */
955	if (kvm_read_guest_lock(its->dev->kvm,
956			   base + index * sizeof(indirect_ptr),
957			   &indirect_ptr, sizeof(indirect_ptr)))
958		return false;
959
960	indirect_ptr = le64_to_cpu(indirect_ptr);
961
962	/* check the valid bit of the first level entry */
963	if (!(indirect_ptr & BIT_ULL(63)))
964		return false;
965
966	/* Mask the guest physical address and calculate the frame number. */
967	indirect_ptr &= GENMASK_ULL(51, 16);
968
969	/* Find the address of the actual entry */
970	index = id % (SZ_64K / esz);
971	indirect_ptr += index * esz;
972	gfn = indirect_ptr >> PAGE_SHIFT;
973
974	if (eaddr)
975		*eaddr = indirect_ptr;
976
977out:
978	idx = srcu_read_lock(&its->dev->kvm->srcu);
979	ret = kvm_is_visible_gfn(its->dev->kvm, gfn);
980	srcu_read_unlock(&its->dev->kvm->srcu, idx);
981	return ret;
982}
983
984static int vgic_its_alloc_collection(struct vgic_its *its,
985				     struct its_collection **colp,
986				     u32 coll_id)
987{
988	struct its_collection *collection;
989
990	if (!vgic_its_check_id(its, its->baser_coll_table, coll_id, NULL))
991		return E_ITS_MAPC_COLLECTION_OOR;
992
993	collection = kzalloc(sizeof(*collection), GFP_KERNEL);
994	if (!collection)
995		return -ENOMEM;
996
997	collection->collection_id = coll_id;
998	collection->target_addr = COLLECTION_NOT_MAPPED;
999
1000	list_add_tail(&collection->coll_list, &its->collection_list);
1001	*colp = collection;
1002
1003	return 0;
1004}
1005
1006static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)
1007{
1008	struct its_collection *collection;
1009	struct its_device *device;
1010	struct its_ite *ite;
1011
1012	/*
1013	 * Clearing the mapping for that collection ID removes the
1014	 * entry from the list. If there wasn't any before, we can
1015	 * go home early.
1016	 */
1017	collection = find_collection(its, coll_id);
1018	if (!collection)
1019		return;
1020
1021	for_each_lpi_its(device, ite, its)
1022		if (ite->collection &&
1023		    ite->collection->collection_id == coll_id)
1024			ite->collection = NULL;
1025
1026	list_del(&collection->coll_list);
1027	kfree(collection);
1028}
1029
1030/* Must be called with its_lock mutex held */
1031static struct its_ite *vgic_its_alloc_ite(struct its_device *device,
1032					  struct its_collection *collection,
1033					  u32 event_id)
1034{
1035	struct its_ite *ite;
1036
1037	ite = kzalloc(sizeof(*ite), GFP_KERNEL);
1038	if (!ite)
1039		return ERR_PTR(-ENOMEM);
1040
1041	ite->event_id	= event_id;
1042	ite->collection = collection;
1043
1044	list_add_tail(&ite->ite_list, &device->itt_head);
1045	return ite;
1046}
1047
1048/*
1049 * The MAPTI and MAPI commands map LPIs to ITTEs.
1050 * Must be called with its_lock mutex held.
1051 */
1052static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
1053				    u64 *its_cmd)
1054{
1055	u32 device_id = its_cmd_get_deviceid(its_cmd);
1056	u32 event_id = its_cmd_get_id(its_cmd);
1057	u32 coll_id = its_cmd_get_collection(its_cmd);
1058	struct its_ite *ite;
1059	struct kvm_vcpu *vcpu = NULL;
1060	struct its_device *device;
1061	struct its_collection *collection, *new_coll = NULL;
1062	struct vgic_irq *irq;
1063	int lpi_nr;
1064
1065	device = find_its_device(its, device_id);
1066	if (!device)
1067		return E_ITS_MAPTI_UNMAPPED_DEVICE;
1068
1069	if (event_id >= BIT_ULL(device->num_eventid_bits))
1070		return E_ITS_MAPTI_ID_OOR;
1071
1072	if (its_cmd_get_command(its_cmd) == GITS_CMD_MAPTI)
1073		lpi_nr = its_cmd_get_physical_id(its_cmd);
1074	else
1075		lpi_nr = event_id;
1076	if (lpi_nr < GIC_LPI_OFFSET ||
1077	    lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser))
1078		return E_ITS_MAPTI_PHYSICALID_OOR;
1079
1080	/* If there is an existing mapping, behavior is UNPREDICTABLE. */
1081	if (find_ite(its, device_id, event_id))
1082		return 0;
1083
1084	collection = find_collection(its, coll_id);
1085	if (!collection) {
1086		int ret = vgic_its_alloc_collection(its, &collection, coll_id);
1087		if (ret)
1088			return ret;
1089		new_coll = collection;
1090	}
1091
1092	ite = vgic_its_alloc_ite(device, collection, event_id);
1093	if (IS_ERR(ite)) {
1094		if (new_coll)
1095			vgic_its_free_collection(its, coll_id);
1096		return PTR_ERR(ite);
1097	}
1098
1099	if (its_is_collection_mapped(collection))
1100		vcpu = kvm_get_vcpu(kvm, collection->target_addr);
1101
1102	irq = vgic_add_lpi(kvm, lpi_nr, vcpu);
1103	if (IS_ERR(irq)) {
1104		if (new_coll)
1105			vgic_its_free_collection(its, coll_id);
1106		its_free_ite(kvm, ite);
1107		return PTR_ERR(irq);
1108	}
1109	ite->irq = irq;
1110
1111	return 0;
1112}
1113
1114/* Requires the its_lock to be held. */
1115static void vgic_its_free_device(struct kvm *kvm, struct its_device *device)
1116{
1117	struct its_ite *ite, *temp;
1118
1119	/*
1120	 * The spec says that unmapping a device with still valid
1121	 * ITTEs associated is UNPREDICTABLE. We remove all ITTEs,
1122	 * since we cannot leave the memory unreferenced.
1123	 */
1124	list_for_each_entry_safe(ite, temp, &device->itt_head, ite_list)
1125		its_free_ite(kvm, ite);
1126
1127	vgic_its_invalidate_cache(kvm);
1128
1129	list_del(&device->dev_list);
1130	kfree(device);
1131}
1132
1133/* its lock must be held */
1134static void vgic_its_free_device_list(struct kvm *kvm, struct vgic_its *its)
1135{
1136	struct its_device *cur, *temp;
1137
1138	list_for_each_entry_safe(cur, temp, &its->device_list, dev_list)
1139		vgic_its_free_device(kvm, cur);
1140}
1141
1142/* its lock must be held */
1143static void vgic_its_free_collection_list(struct kvm *kvm, struct vgic_its *its)
1144{
1145	struct its_collection *cur, *temp;
1146
1147	list_for_each_entry_safe(cur, temp, &its->collection_list, coll_list)
1148		vgic_its_free_collection(its, cur->collection_id);
1149}
1150
1151/* Must be called with its_lock mutex held */
1152static struct its_device *vgic_its_alloc_device(struct vgic_its *its,
1153						u32 device_id, gpa_t itt_addr,
1154						u8 num_eventid_bits)
1155{
1156	struct its_device *device;
1157
1158	device = kzalloc(sizeof(*device), GFP_KERNEL);
1159	if (!device)
1160		return ERR_PTR(-ENOMEM);
1161
1162	device->device_id = device_id;
1163	device->itt_addr = itt_addr;
1164	device->num_eventid_bits = num_eventid_bits;
1165	INIT_LIST_HEAD(&device->itt_head);
1166
1167	list_add_tail(&device->dev_list, &its->device_list);
1168	return device;
1169}
1170
1171/*
1172 * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs).
1173 * Must be called with the its_lock mutex held.
1174 */
1175static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
1176				    u64 *its_cmd)
1177{
1178	u32 device_id = its_cmd_get_deviceid(its_cmd);
1179	bool valid = its_cmd_get_validbit(its_cmd);
1180	u8 num_eventid_bits = its_cmd_get_size(its_cmd);
1181	gpa_t itt_addr = its_cmd_get_ittaddr(its_cmd);
1182	struct its_device *device;
1183
1184	if (!vgic_its_check_id(its, its->baser_device_table, device_id, NULL))
1185		return E_ITS_MAPD_DEVICE_OOR;
1186
1187	if (valid && num_eventid_bits > VITS_TYPER_IDBITS)
1188		return E_ITS_MAPD_ITTSIZE_OOR;
1189
1190	device = find_its_device(its, device_id);
1191
1192	/*
1193	 * The spec says that calling MAPD on an already mapped device
1194	 * invalidates all cached data for this device. We implement this
1195	 * by removing the mapping and re-establishing it.
1196	 */
1197	if (device)
1198		vgic_its_free_device(kvm, device);
1199
1200	/*
1201	 * The spec does not say whether unmapping a not-mapped device
1202	 * is an error, so we are done in any case.
1203	 */
1204	if (!valid)
1205		return 0;
1206
1207	device = vgic_its_alloc_device(its, device_id, itt_addr,
1208				       num_eventid_bits);
1209
1210	return PTR_ERR_OR_ZERO(device);
1211}
1212
1213/*
1214 * The MAPC command maps collection IDs to redistributors.
1215 * Must be called with the its_lock mutex held.
1216 */
1217static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its,
1218				    u64 *its_cmd)
1219{
1220	u16 coll_id;
1221	u32 target_addr;
1222	struct its_collection *collection;
1223	bool valid;
1224
1225	valid = its_cmd_get_validbit(its_cmd);
1226	coll_id = its_cmd_get_collection(its_cmd);
1227	target_addr = its_cmd_get_target_addr(its_cmd);
1228
1229	if (target_addr >= atomic_read(&kvm->online_vcpus))
1230		return E_ITS_MAPC_PROCNUM_OOR;
1231
1232	if (!valid) {
1233		vgic_its_free_collection(its, coll_id);
1234		vgic_its_invalidate_cache(kvm);
1235	} else {
1236		collection = find_collection(its, coll_id);
1237
1238		if (!collection) {
1239			int ret;
1240
1241			ret = vgic_its_alloc_collection(its, &collection,
1242							coll_id);
1243			if (ret)
1244				return ret;
1245			collection->target_addr = target_addr;
1246		} else {
1247			collection->target_addr = target_addr;
1248			update_affinity_collection(kvm, its, collection);
1249		}
1250	}
1251
1252	return 0;
1253}
1254
1255/*
1256 * The CLEAR command removes the pending state for a particular LPI.
1257 * Must be called with the its_lock mutex held.
1258 */
1259static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its,
1260				     u64 *its_cmd)
1261{
1262	u32 device_id = its_cmd_get_deviceid(its_cmd);
1263	u32 event_id = its_cmd_get_id(its_cmd);
1264	struct its_ite *ite;
1265
1266
1267	ite = find_ite(its, device_id, event_id);
1268	if (!ite)
1269		return E_ITS_CLEAR_UNMAPPED_INTERRUPT;
1270
1271	ite->irq->pending_latch = false;
1272
1273	if (ite->irq->hw)
1274		return irq_set_irqchip_state(ite->irq->host_irq,
1275					     IRQCHIP_STATE_PENDING, false);
1276
1277	return 0;
1278}
1279
1280/*
1281 * The INV command syncs the configuration bits from the memory table.
1282 * Must be called with the its_lock mutex held.
1283 */
1284static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its,
1285				   u64 *its_cmd)
1286{
1287	u32 device_id = its_cmd_get_deviceid(its_cmd);
1288	u32 event_id = its_cmd_get_id(its_cmd);
1289	struct its_ite *ite;
1290
1291
1292	ite = find_ite(its, device_id, event_id);
1293	if (!ite)
1294		return E_ITS_INV_UNMAPPED_INTERRUPT;
1295
1296	return update_lpi_config(kvm, ite->irq, NULL, true);
1297}
1298
1299/*
1300 * The INVALL command requests flushing of all IRQ data in this collection.
1301 * Find the VCPU mapped to that collection, then iterate over the VM's list
1302 * of mapped LPIs and update the configuration for each IRQ which targets
1303 * the specified vcpu. The configuration will be read from the in-memory
1304 * configuration table.
1305 * Must be called with the its_lock mutex held.
1306 */
1307static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its,
1308				      u64 *its_cmd)
1309{
1310	u32 coll_id = its_cmd_get_collection(its_cmd);
1311	struct its_collection *collection;
1312	struct kvm_vcpu *vcpu;
1313	struct vgic_irq *irq;
1314	u32 *intids;
1315	int irq_count, i;
1316
1317	collection = find_collection(its, coll_id);
1318	if (!its_is_collection_mapped(collection))
1319		return E_ITS_INVALL_UNMAPPED_COLLECTION;
1320
1321	vcpu = kvm_get_vcpu(kvm, collection->target_addr);
1322
1323	irq_count = vgic_copy_lpi_list(kvm, vcpu, &intids);
1324	if (irq_count < 0)
1325		return irq_count;
1326
1327	for (i = 0; i < irq_count; i++) {
1328		irq = vgic_get_irq(kvm, NULL, intids[i]);
1329		if (!irq)
1330			continue;
1331		update_lpi_config(kvm, irq, vcpu, false);
1332		vgic_put_irq(kvm, irq);
1333	}
1334
1335	kfree(intids);
1336
1337	if (vcpu->arch.vgic_cpu.vgic_v3.its_vpe.its_vm)
1338		its_invall_vpe(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe);
1339
1340	return 0;
1341}
1342
1343/*
1344 * The MOVALL command moves the pending state of all IRQs targeting one
1345 * redistributor to another. We don't hold the pending state in the VCPUs,
1346 * but in the IRQs instead, so there is really not much to do for us here.
1347 * However the spec says that no IRQ must target the old redistributor
1348 * afterwards, so we make sure that no LPI is using the associated target_vcpu.
1349 * This command affects all LPIs in the system that target that redistributor.
1350 */
1351static int vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its,
1352				      u64 *its_cmd)
1353{
1354	u32 target1_addr = its_cmd_get_target_addr(its_cmd);
1355	u32 target2_addr = its_cmd_mask_field(its_cmd, 3, 16, 32);
1356	struct kvm_vcpu *vcpu1, *vcpu2;
1357	struct vgic_irq *irq;
1358	u32 *intids;
1359	int irq_count, i;
1360
1361	if (target1_addr >= atomic_read(&kvm->online_vcpus) ||
1362	    target2_addr >= atomic_read(&kvm->online_vcpus))
1363		return E_ITS_MOVALL_PROCNUM_OOR;
1364
1365	if (target1_addr == target2_addr)
1366		return 0;
1367
1368	vcpu1 = kvm_get_vcpu(kvm, target1_addr);
1369	vcpu2 = kvm_get_vcpu(kvm, target2_addr);
1370
1371	irq_count = vgic_copy_lpi_list(kvm, vcpu1, &intids);
1372	if (irq_count < 0)
1373		return irq_count;
1374
1375	for (i = 0; i < irq_count; i++) {
1376		irq = vgic_get_irq(kvm, NULL, intids[i]);
1377
1378		update_affinity(irq, vcpu2);
1379
1380		vgic_put_irq(kvm, irq);
1381	}
1382
1383	vgic_its_invalidate_cache(kvm);
1384
1385	kfree(intids);
1386	return 0;
1387}
1388
1389/*
1390 * The INT command injects the LPI associated with that DevID/EvID pair.
1391 * Must be called with the its_lock mutex held.
1392 */
1393static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its,
1394				   u64 *its_cmd)
1395{
1396	u32 msi_data = its_cmd_get_id(its_cmd);
1397	u64 msi_devid = its_cmd_get_deviceid(its_cmd);
1398
1399	return vgic_its_trigger_msi(kvm, its, msi_devid, msi_data);
1400}
1401
1402/*
1403 * This function is called with the its_cmd lock held, but the ITS data
1404 * structure lock dropped.
1405 */
1406static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its,
1407				   u64 *its_cmd)
1408{
1409	int ret = -ENODEV;
1410
1411	mutex_lock(&its->its_lock);
1412	switch (its_cmd_get_command(its_cmd)) {
1413	case GITS_CMD_MAPD:
1414		ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd);
1415		break;
1416	case GITS_CMD_MAPC:
1417		ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd);
1418		break;
1419	case GITS_CMD_MAPI:
1420		ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
1421		break;
1422	case GITS_CMD_MAPTI:
1423		ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
1424		break;
1425	case GITS_CMD_MOVI:
1426		ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd);
1427		break;
1428	case GITS_CMD_DISCARD:
1429		ret = vgic_its_cmd_handle_discard(kvm, its, its_cmd);
1430		break;
1431	case GITS_CMD_CLEAR:
1432		ret = vgic_its_cmd_handle_clear(kvm, its, its_cmd);
1433		break;
1434	case GITS_CMD_MOVALL:
1435		ret = vgic_its_cmd_handle_movall(kvm, its, its_cmd);
1436		break;
1437	case GITS_CMD_INT:
1438		ret = vgic_its_cmd_handle_int(kvm, its, its_cmd);
1439		break;
1440	case GITS_CMD_INV:
1441		ret = vgic_its_cmd_handle_inv(kvm, its, its_cmd);
1442		break;
1443	case GITS_CMD_INVALL:
1444		ret = vgic_its_cmd_handle_invall(kvm, its, its_cmd);
1445		break;
1446	case GITS_CMD_SYNC:
1447		/* we ignore this command: we are in sync all of the time */
1448		ret = 0;
1449		break;
1450	}
1451	mutex_unlock(&its->its_lock);
1452
1453	return ret;
1454}
1455
1456static u64 vgic_sanitise_its_baser(u64 reg)
1457{
1458	reg = vgic_sanitise_field(reg, GITS_BASER_SHAREABILITY_MASK,
1459				  GITS_BASER_SHAREABILITY_SHIFT,
1460				  vgic_sanitise_shareability);
1461	reg = vgic_sanitise_field(reg, GITS_BASER_INNER_CACHEABILITY_MASK,
1462				  GITS_BASER_INNER_CACHEABILITY_SHIFT,
1463				  vgic_sanitise_inner_cacheability);
1464	reg = vgic_sanitise_field(reg, GITS_BASER_OUTER_CACHEABILITY_MASK,
1465				  GITS_BASER_OUTER_CACHEABILITY_SHIFT,
1466				  vgic_sanitise_outer_cacheability);
1467
1468	/* We support only one (ITS) page size: 64K */
1469	reg = (reg & ~GITS_BASER_PAGE_SIZE_MASK) | GITS_BASER_PAGE_SIZE_64K;
1470
1471	return reg;
1472}
1473
1474static u64 vgic_sanitise_its_cbaser(u64 reg)
1475{
1476	reg = vgic_sanitise_field(reg, GITS_CBASER_SHAREABILITY_MASK,
1477				  GITS_CBASER_SHAREABILITY_SHIFT,
1478				  vgic_sanitise_shareability);
1479	reg = vgic_sanitise_field(reg, GITS_CBASER_INNER_CACHEABILITY_MASK,
1480				  GITS_CBASER_INNER_CACHEABILITY_SHIFT,
1481				  vgic_sanitise_inner_cacheability);
1482	reg = vgic_sanitise_field(reg, GITS_CBASER_OUTER_CACHEABILITY_MASK,
1483				  GITS_CBASER_OUTER_CACHEABILITY_SHIFT,
1484				  vgic_sanitise_outer_cacheability);
1485
1486	/* Sanitise the physical address to be 64k aligned. */
1487	reg &= ~GENMASK_ULL(15, 12);
1488
1489	return reg;
1490}
1491
1492static unsigned long vgic_mmio_read_its_cbaser(struct kvm *kvm,
1493					       struct vgic_its *its,
1494					       gpa_t addr, unsigned int len)
1495{
1496	return extract_bytes(its->cbaser, addr & 7, len);
1497}
1498
1499static void vgic_mmio_write_its_cbaser(struct kvm *kvm, struct vgic_its *its,
1500				       gpa_t addr, unsigned int len,
1501				       unsigned long val)
1502{
1503	/* When GITS_CTLR.Enable is 1, this register is RO. */
1504	if (its->enabled)
1505		return;
1506
1507	mutex_lock(&its->cmd_lock);
1508	its->cbaser = update_64bit_reg(its->cbaser, addr & 7, len, val);
1509	its->cbaser = vgic_sanitise_its_cbaser(its->cbaser);
1510	its->creadr = 0;
1511	/*
1512	 * CWRITER is architecturally UNKNOWN on reset, but we need to reset
1513	 * it to CREADR to make sure we start with an empty command buffer.
1514	 */
1515	its->cwriter = its->creadr;
1516	mutex_unlock(&its->cmd_lock);
1517}
1518
1519#define ITS_CMD_BUFFER_SIZE(baser)	((((baser) & 0xff) + 1) << 12)
1520#define ITS_CMD_SIZE			32
1521#define ITS_CMD_OFFSET(reg)		((reg) & GENMASK(19, 5))
1522
1523/* Must be called with the cmd_lock held. */
1524static void vgic_its_process_commands(struct kvm *kvm, struct vgic_its *its)
1525{
1526	gpa_t cbaser;
1527	u64 cmd_buf[4];
1528
1529	/* Commands are only processed when the ITS is enabled. */
1530	if (!its->enabled)
1531		return;
1532
1533	cbaser = GITS_CBASER_ADDRESS(its->cbaser);
1534
1535	while (its->cwriter != its->creadr) {
1536		int ret = kvm_read_guest_lock(kvm, cbaser + its->creadr,
1537					      cmd_buf, ITS_CMD_SIZE);
1538		/*
1539		 * If kvm_read_guest() fails, this could be due to the guest
1540		 * programming a bogus value in CBASER or something else going
1541		 * wrong from which we cannot easily recover.
1542		 * According to section 6.3.2 in the GICv3 spec we can just
1543		 * ignore that command then.
1544		 */
1545		if (!ret)
1546			vgic_its_handle_command(kvm, its, cmd_buf);
1547
1548		its->creadr += ITS_CMD_SIZE;
1549		if (its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser))
1550			its->creadr = 0;
1551	}
1552}
1553
1554/*
1555 * By writing to CWRITER the guest announces new commands to be processed.
1556 * To avoid any races in the first place, we take the its_cmd lock, which
1557 * protects our ring buffer variables, so that there is only one user
1558 * per ITS handling commands at a given time.
1559 */
1560static void vgic_mmio_write_its_cwriter(struct kvm *kvm, struct vgic_its *its,
1561					gpa_t addr, unsigned int len,
1562					unsigned long val)
1563{
1564	u64 reg;
1565
1566	if (!its)
1567		return;
1568
1569	mutex_lock(&its->cmd_lock);
1570
1571	reg = update_64bit_reg(its->cwriter, addr & 7, len, val);
1572	reg = ITS_CMD_OFFSET(reg);
1573	if (reg >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
1574		mutex_unlock(&its->cmd_lock);
1575		return;
1576	}
1577	its->cwriter = reg;
1578
1579	vgic_its_process_commands(kvm, its);
1580
1581	mutex_unlock(&its->cmd_lock);
1582}
1583
1584static unsigned long vgic_mmio_read_its_cwriter(struct kvm *kvm,
1585						struct vgic_its *its,
1586						gpa_t addr, unsigned int len)
1587{
1588	return extract_bytes(its->cwriter, addr & 0x7, len);
1589}
1590
1591static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,
1592					       struct vgic_its *its,
1593					       gpa_t addr, unsigned int len)
1594{
1595	return extract_bytes(its->creadr, addr & 0x7, len);
1596}
1597
1598static int vgic_mmio_uaccess_write_its_creadr(struct kvm *kvm,
1599					      struct vgic_its *its,
1600					      gpa_t addr, unsigned int len,
1601					      unsigned long val)
1602{
1603	u32 cmd_offset;
1604	int ret = 0;
1605
1606	mutex_lock(&its->cmd_lock);
1607
1608	if (its->enabled) {
1609		ret = -EBUSY;
1610		goto out;
1611	}
1612
1613	cmd_offset = ITS_CMD_OFFSET(val);
1614	if (cmd_offset >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
1615		ret = -EINVAL;
1616		goto out;
1617	}
1618
1619	its->creadr = cmd_offset;
1620out:
1621	mutex_unlock(&its->cmd_lock);
1622	return ret;
1623}
1624
1625#define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7)
1626static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm,
1627					      struct vgic_its *its,
1628					      gpa_t addr, unsigned int len)
1629{
1630	u64 reg;
1631
1632	switch (BASER_INDEX(addr)) {
1633	case 0:
1634		reg = its->baser_device_table;
1635		break;
1636	case 1:
1637		reg = its->baser_coll_table;
1638		break;
1639	default:
1640		reg = 0;
1641		break;
1642	}
1643
1644	return extract_bytes(reg, addr & 7, len);
1645}
1646
1647#define GITS_BASER_RO_MASK	(GENMASK_ULL(52, 48) | GENMASK_ULL(58, 56))
1648static void vgic_mmio_write_its_baser(struct kvm *kvm,
1649				      struct vgic_its *its,
1650				      gpa_t addr, unsigned int len,
1651				      unsigned long val)
1652{
1653	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
1654	u64 entry_size, table_type;
1655	u64 reg, *regptr, clearbits = 0;
1656
1657	/* When GITS_CTLR.Enable is 1, we ignore write accesses. */
1658	if (its->enabled)
1659		return;
1660
1661	switch (BASER_INDEX(addr)) {
1662	case 0:
1663		regptr = &its->baser_device_table;
1664		entry_size = abi->dte_esz;
1665		table_type = GITS_BASER_TYPE_DEVICE;
1666		break;
1667	case 1:
1668		regptr = &its->baser_coll_table;
1669		entry_size = abi->cte_esz;
1670		table_type = GITS_BASER_TYPE_COLLECTION;
1671		clearbits = GITS_BASER_INDIRECT;
1672		break;
1673	default:
1674		return;
1675	}
1676
1677	reg = update_64bit_reg(*regptr, addr & 7, len, val);
1678	reg &= ~GITS_BASER_RO_MASK;
1679	reg &= ~clearbits;
1680
1681	reg |= (entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
1682	reg |= table_type << GITS_BASER_TYPE_SHIFT;
1683	reg = vgic_sanitise_its_baser(reg);
1684
1685	*regptr = reg;
1686
1687	if (!(reg & GITS_BASER_VALID)) {
1688		/* Take the its_lock to prevent a race with a save/restore */
1689		mutex_lock(&its->its_lock);
1690		switch (table_type) {
1691		case GITS_BASER_TYPE_DEVICE:
1692			vgic_its_free_device_list(kvm, its);
1693			break;
1694		case GITS_BASER_TYPE_COLLECTION:
1695			vgic_its_free_collection_list(kvm, its);
1696			break;
1697		}
1698		mutex_unlock(&its->its_lock);
1699	}
1700}
1701
1702static unsigned long vgic_mmio_read_its_ctlr(struct kvm *vcpu,
1703					     struct vgic_its *its,
1704					     gpa_t addr, unsigned int len)
1705{
1706	u32 reg = 0;
1707
1708	mutex_lock(&its->cmd_lock);
1709	if (its->creadr == its->cwriter)
1710		reg |= GITS_CTLR_QUIESCENT;
1711	if (its->enabled)
1712		reg |= GITS_CTLR_ENABLE;
1713	mutex_unlock(&its->cmd_lock);
1714
1715	return reg;
1716}
1717
1718static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its,
1719				     gpa_t addr, unsigned int len,
1720				     unsigned long val)
1721{
1722	mutex_lock(&its->cmd_lock);
1723
1724	/*
1725	 * It is UNPREDICTABLE to enable the ITS if any of the CBASER or
1726	 * device/collection BASER are invalid
1727	 */
1728	if (!its->enabled && (val & GITS_CTLR_ENABLE) &&
1729		(!(its->baser_device_table & GITS_BASER_VALID) ||
1730		 !(its->baser_coll_table & GITS_BASER_VALID) ||
1731		 !(its->cbaser & GITS_CBASER_VALID)))
1732		goto out;
1733
1734	its->enabled = !!(val & GITS_CTLR_ENABLE);
1735	if (!its->enabled)
1736		vgic_its_invalidate_cache(kvm);
1737
1738	/*
1739	 * Try to process any pending commands. This function bails out early
1740	 * if the ITS is disabled or no commands have been queued.
1741	 */
1742	vgic_its_process_commands(kvm, its);
1743
1744out:
1745	mutex_unlock(&its->cmd_lock);
1746}
1747
1748#define REGISTER_ITS_DESC(off, rd, wr, length, acc)		\
1749{								\
1750	.reg_offset = off,					\
1751	.len = length,						\
1752	.access_flags = acc,					\
1753	.its_read = rd,						\
1754	.its_write = wr,					\
1755}
1756
1757#define REGISTER_ITS_DESC_UACCESS(off, rd, wr, uwr, length, acc)\
1758{								\
1759	.reg_offset = off,					\
1760	.len = length,						\
1761	.access_flags = acc,					\
1762	.its_read = rd,						\
1763	.its_write = wr,					\
1764	.uaccess_its_write = uwr,				\
1765}
1766
1767static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its,
1768			      gpa_t addr, unsigned int len, unsigned long val)
1769{
1770	/* Ignore */
1771}
1772
1773static struct vgic_register_region its_registers[] = {
1774	REGISTER_ITS_DESC(GITS_CTLR,
1775		vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4,
1776		VGIC_ACCESS_32bit),
1777	REGISTER_ITS_DESC_UACCESS(GITS_IIDR,
1778		vgic_mmio_read_its_iidr, its_mmio_write_wi,
1779		vgic_mmio_uaccess_write_its_iidr, 4,
1780		VGIC_ACCESS_32bit),
1781	REGISTER_ITS_DESC(GITS_TYPER,
1782		vgic_mmio_read_its_typer, its_mmio_write_wi, 8,
1783		VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1784	REGISTER_ITS_DESC(GITS_CBASER,
1785		vgic_mmio_read_its_cbaser, vgic_mmio_write_its_cbaser, 8,
1786		VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1787	REGISTER_ITS_DESC(GITS_CWRITER,
1788		vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8,
1789		VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1790	REGISTER_ITS_DESC_UACCESS(GITS_CREADR,
1791		vgic_mmio_read_its_creadr, its_mmio_write_wi,
1792		vgic_mmio_uaccess_write_its_creadr, 8,
1793		VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1794	REGISTER_ITS_DESC(GITS_BASER,
1795		vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,
1796		VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1797	REGISTER_ITS_DESC(GITS_IDREGS_BASE,
1798		vgic_mmio_read_its_idregs, its_mmio_write_wi, 0x30,
1799		VGIC_ACCESS_32bit),
1800};
1801
1802/* This is called on setting the LPI enable bit in the redistributor. */
1803void vgic_enable_lpis(struct kvm_vcpu *vcpu)
1804{
1805	if (!(vcpu->arch.vgic_cpu.pendbaser & GICR_PENDBASER_PTZ))
1806		its_sync_lpi_pending_table(vcpu);
1807}
1808
1809static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its,
1810				   u64 addr)
1811{
1812	struct vgic_io_device *iodev = &its->iodev;
1813	int ret;
1814
1815	mutex_lock(&kvm->slots_lock);
1816	if (!IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) {
1817		ret = -EBUSY;
1818		goto out;
1819	}
1820
1821	its->vgic_its_base = addr;
1822	iodev->regions = its_registers;
1823	iodev->nr_regions = ARRAY_SIZE(its_registers);
1824	kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops);
1825
1826	iodev->base_addr = its->vgic_its_base;
1827	iodev->iodev_type = IODEV_ITS;
1828	iodev->its = its;
1829	ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr,
1830				      KVM_VGIC_V3_ITS_SIZE, &iodev->dev);
1831out:
1832	mutex_unlock(&kvm->slots_lock);
1833
1834	return ret;
1835}
1836
1837/* Default is 16 cached LPIs per vcpu */
1838#define LPI_DEFAULT_PCPU_CACHE_SIZE	16
1839
1840void vgic_lpi_translation_cache_init(struct kvm *kvm)
1841{
1842	struct vgic_dist *dist = &kvm->arch.vgic;
1843	unsigned int sz;
1844	int i;
1845
1846	if (!list_empty(&dist->lpi_translation_cache))
1847		return;
1848
1849	sz = atomic_read(&kvm->online_vcpus) * LPI_DEFAULT_PCPU_CACHE_SIZE;
1850
1851	for (i = 0; i < sz; i++) {
1852		struct vgic_translation_cache_entry *cte;
1853
1854		/* An allocation failure is not fatal */
1855		cte = kzalloc(sizeof(*cte), GFP_KERNEL);
1856		if (WARN_ON(!cte))
1857			break;
1858
1859		INIT_LIST_HEAD(&cte->entry);
1860		list_add(&cte->entry, &dist->lpi_translation_cache);
1861	}
1862}
1863
1864void vgic_lpi_translation_cache_destroy(struct kvm *kvm)
1865{
1866	struct vgic_dist *dist = &kvm->arch.vgic;
1867	struct vgic_translation_cache_entry *cte, *tmp;
1868
1869	vgic_its_invalidate_cache(kvm);
1870
1871	list_for_each_entry_safe(cte, tmp,
1872				 &dist->lpi_translation_cache, entry) {
1873		list_del(&cte->entry);
1874		kfree(cte);
1875	}
1876}
1877
1878#define INITIAL_BASER_VALUE						  \
1879	(GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb)		| \
1880	 GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner)		| \
1881	 GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable)		| \
1882	 GITS_BASER_PAGE_SIZE_64K)
1883
1884#define INITIAL_PROPBASER_VALUE						  \
1885	(GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWb)		| \
1886	 GIC_BASER_CACHEABILITY(GICR_PROPBASER, OUTER, SameAsInner)	| \
1887	 GIC_BASER_SHAREABILITY(GICR_PROPBASER, InnerShareable))
1888
1889static int vgic_its_create(struct kvm_device *dev, u32 type)
1890{
1891	struct vgic_its *its;
1892
1893	if (type != KVM_DEV_TYPE_ARM_VGIC_ITS)
1894		return -ENODEV;
1895
1896	its = kzalloc(sizeof(struct vgic_its), GFP_KERNEL);
1897	if (!its)
1898		return -ENOMEM;
1899
1900	if (vgic_initialized(dev->kvm)) {
1901		int ret = vgic_v4_init(dev->kvm);
1902		if (ret < 0) {
1903			kfree(its);
1904			return ret;
1905		}
1906
1907		vgic_lpi_translation_cache_init(dev->kvm);
1908	}
1909
1910	mutex_init(&its->its_lock);
1911	mutex_init(&its->cmd_lock);
1912
1913	its->vgic_its_base = VGIC_ADDR_UNDEF;
1914
1915	INIT_LIST_HEAD(&its->device_list);
1916	INIT_LIST_HEAD(&its->collection_list);
1917
1918	dev->kvm->arch.vgic.msis_require_devid = true;
1919	dev->kvm->arch.vgic.has_its = true;
1920	its->enabled = false;
1921	its->dev = dev;
1922
1923	its->baser_device_table = INITIAL_BASER_VALUE			|
1924		((u64)GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT);
1925	its->baser_coll_table = INITIAL_BASER_VALUE |
1926		((u64)GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT);
1927	dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE;
1928
1929	dev->private = its;
1930
1931	return vgic_its_set_abi(its, NR_ITS_ABIS - 1);
1932}
1933
1934static void vgic_its_destroy(struct kvm_device *kvm_dev)
1935{
1936	struct kvm *kvm = kvm_dev->kvm;
1937	struct vgic_its *its = kvm_dev->private;
1938
1939	mutex_lock(&its->its_lock);
1940
1941	vgic_its_free_device_list(kvm, its);
1942	vgic_its_free_collection_list(kvm, its);
1943
1944	mutex_unlock(&its->its_lock);
1945	kfree(its);
1946	kfree(kvm_dev);/* alloc by kvm_ioctl_create_device, free by .destroy */
1947}
1948
1949static int vgic_its_has_attr_regs(struct kvm_device *dev,
1950				  struct kvm_device_attr *attr)
1951{
1952	const struct vgic_register_region *region;
1953	gpa_t offset = attr->attr;
1954	int align;
1955
1956	align = (offset < GITS_TYPER) || (offset >= GITS_PIDR4) ? 0x3 : 0x7;
1957
1958	if (offset & align)
1959		return -EINVAL;
1960
1961	region = vgic_find_mmio_region(its_registers,
1962				       ARRAY_SIZE(its_registers),
1963				       offset);
1964	if (!region)
1965		return -ENXIO;
1966
1967	return 0;
1968}
1969
1970static int vgic_its_attr_regs_access(struct kvm_device *dev,
1971				     struct kvm_device_attr *attr,
1972				     u64 *reg, bool is_write)
1973{
1974	const struct vgic_register_region *region;
1975	struct vgic_its *its;
1976	gpa_t addr, offset;
1977	unsigned int len;
1978	int align, ret = 0;
1979
1980	its = dev->private;
1981	offset = attr->attr;
1982
1983	/*
1984	 * Although the spec supports upper/lower 32-bit accesses to
1985	 * 64-bit ITS registers, the userspace ABI requires 64-bit
1986	 * accesses to all 64-bit wide registers. We therefore only
1987	 * support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID
1988	 * registers
1989	 */
1990	if ((offset < GITS_TYPER) || (offset >= GITS_PIDR4))
1991		align = 0x3;
1992	else
1993		align = 0x7;
1994
1995	if (offset & align)
1996		return -EINVAL;
1997
1998	mutex_lock(&dev->kvm->lock);
1999
2000	if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) {
2001		ret = -ENXIO;
2002		goto out;
2003	}
2004
2005	region = vgic_find_mmio_region(its_registers,
2006				       ARRAY_SIZE(its_registers),
2007				       offset);
2008	if (!region) {
2009		ret = -ENXIO;
2010		goto out;
2011	}
2012
2013	if (!lock_all_vcpus(dev->kvm)) {
2014		ret = -EBUSY;
2015		goto out;
2016	}
2017
2018	addr = its->vgic_its_base + offset;
2019
2020	len = region->access_flags & VGIC_ACCESS_64bit ? 8 : 4;
2021
2022	if (is_write) {
2023		if (region->uaccess_its_write)
2024			ret = region->uaccess_its_write(dev->kvm, its, addr,
2025							len, *reg);
2026		else
2027			region->its_write(dev->kvm, its, addr, len, *reg);
2028	} else {
2029		*reg = region->its_read(dev->kvm, its, addr, len);
2030	}
2031	unlock_all_vcpus(dev->kvm);
2032out:
2033	mutex_unlock(&dev->kvm->lock);
2034	return ret;
2035}
2036
2037static u32 compute_next_devid_offset(struct list_head *h,
2038				     struct its_device *dev)
2039{
2040	struct its_device *next;
2041	u32 next_offset;
2042
2043	if (list_is_last(&dev->dev_list, h))
2044		return 0;
2045	next = list_next_entry(dev, dev_list);
2046	next_offset = next->device_id - dev->device_id;
2047
2048	return min_t(u32, next_offset, VITS_DTE_MAX_DEVID_OFFSET);
2049}
2050
2051static u32 compute_next_eventid_offset(struct list_head *h, struct its_ite *ite)
2052{
2053	struct its_ite *next;
2054	u32 next_offset;
2055
2056	if (list_is_last(&ite->ite_list, h))
2057		return 0;
2058	next = list_next_entry(ite, ite_list);
2059	next_offset = next->event_id - ite->event_id;
2060
2061	return min_t(u32, next_offset, VITS_ITE_MAX_EVENTID_OFFSET);
2062}
2063
2064/**
2065 * entry_fn_t - Callback called on a table entry restore path
2066 * @its: its handle
2067 * @id: id of the entry
2068 * @entry: pointer to the entry
2069 * @opaque: pointer to an opaque data
2070 *
2071 * Return: < 0 on error, 0 if last element was identified, id offset to next
2072 * element otherwise
2073 */
2074typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry,
2075			  void *opaque);
2076
2077/**
2078 * scan_its_table - Scan a contiguous table in guest RAM and applies a function
2079 * to each entry
2080 *
2081 * @its: its handle
2082 * @base: base gpa of the table
2083 * @size: size of the table in bytes
2084 * @esz: entry size in bytes
2085 * @start_id: the ID of the first entry in the table
2086 * (non zero for 2d level tables)
2087 * @fn: function to apply on each entry
2088 *
2089 * Return: < 0 on error, 0 if last element was identified, 1 otherwise
2090 * (the last element may not be found on second level tables)
2091 */
2092static int scan_its_table(struct vgic_its *its, gpa_t base, int size, u32 esz,
2093			  int start_id, entry_fn_t fn, void *opaque)
2094{
2095	struct kvm *kvm = its->dev->kvm;
2096	unsigned long len = size;
2097	int id = start_id;
2098	gpa_t gpa = base;
2099	char entry[ESZ_MAX];
2100	int ret;
2101
2102	memset(entry, 0, esz);
2103
2104	while (true) {
2105		int next_offset;
2106		size_t byte_offset;
2107
2108		ret = kvm_read_guest_lock(kvm, gpa, entry, esz);
2109		if (ret)
2110			return ret;
2111
2112		next_offset = fn(its, id, entry, opaque);
2113		if (next_offset <= 0)
2114			return next_offset;
2115
2116		byte_offset = next_offset * esz;
2117		if (byte_offset >= len)
2118			break;
2119
2120		id += next_offset;
2121		gpa += byte_offset;
2122		len -= byte_offset;
2123	}
2124	return 1;
2125}
2126
2127/**
2128 * vgic_its_save_ite - Save an interrupt translation entry at @gpa
2129 */
2130static int vgic_its_save_ite(struct vgic_its *its, struct its_device *dev,
2131			      struct its_ite *ite, gpa_t gpa, int ite_esz)
2132{
2133	struct kvm *kvm = its->dev->kvm;
2134	u32 next_offset;
2135	u64 val;
2136
2137	next_offset = compute_next_eventid_offset(&dev->itt_head, ite);
2138	val = ((u64)next_offset << KVM_ITS_ITE_NEXT_SHIFT) |
2139	       ((u64)ite->irq->intid << KVM_ITS_ITE_PINTID_SHIFT) |
2140		ite->collection->collection_id;
2141	val = cpu_to_le64(val);
2142	return kvm_write_guest_lock(kvm, gpa, &val, ite_esz);
2143}
2144
2145/**
2146 * vgic_its_restore_ite - restore an interrupt translation entry
2147 * @event_id: id used for indexing
2148 * @ptr: pointer to the ITE entry
2149 * @opaque: pointer to the its_device
2150 */
2151static int vgic_its_restore_ite(struct vgic_its *its, u32 event_id,
2152				void *ptr, void *opaque)
2153{
2154	struct its_device *dev = (struct its_device *)opaque;
2155	struct its_collection *collection;
2156	struct kvm *kvm = its->dev->kvm;
2157	struct kvm_vcpu *vcpu = NULL;
2158	u64 val;
2159	u64 *p = (u64 *)ptr;
2160	struct vgic_irq *irq;
2161	u32 coll_id, lpi_id;
2162	struct its_ite *ite;
2163	u32 offset;
2164
2165	val = *p;
2166
2167	val = le64_to_cpu(val);
2168
2169	coll_id = val & KVM_ITS_ITE_ICID_MASK;
2170	lpi_id = (val & KVM_ITS_ITE_PINTID_MASK) >> KVM_ITS_ITE_PINTID_SHIFT;
2171
2172	if (!lpi_id)
2173		return 1; /* invalid entry, no choice but to scan next entry */
2174
2175	if (lpi_id < VGIC_MIN_LPI)
2176		return -EINVAL;
2177
2178	offset = val >> KVM_ITS_ITE_NEXT_SHIFT;
2179	if (event_id + offset >= BIT_ULL(dev->num_eventid_bits))
2180		return -EINVAL;
2181
2182	collection = find_collection(its, coll_id);
2183	if (!collection)
2184		return -EINVAL;
2185
2186	ite = vgic_its_alloc_ite(dev, collection, event_id);
2187	if (IS_ERR(ite))
2188		return PTR_ERR(ite);
2189
2190	if (its_is_collection_mapped(collection))
2191		vcpu = kvm_get_vcpu(kvm, collection->target_addr);
2192
2193	irq = vgic_add_lpi(kvm, lpi_id, vcpu);
2194	if (IS_ERR(irq))
2195		return PTR_ERR(irq);
2196	ite->irq = irq;
2197
2198	return offset;
2199}
2200
2201static int vgic_its_ite_cmp(void *priv, const struct list_head *a,
2202			    const struct list_head *b)
2203{
2204	struct its_ite *itea = container_of(a, struct its_ite, ite_list);
2205	struct its_ite *iteb = container_of(b, struct its_ite, ite_list);
2206
2207	if (itea->event_id < iteb->event_id)
2208		return -1;
2209	else
2210		return 1;
2211}
2212
2213static int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
2214{
2215	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2216	gpa_t base = device->itt_addr;
2217	struct its_ite *ite;
2218	int ret;
2219	int ite_esz = abi->ite_esz;
2220
2221	list_sort(NULL, &device->itt_head, vgic_its_ite_cmp);
2222
2223	list_for_each_entry(ite, &device->itt_head, ite_list) {
2224		gpa_t gpa = base + ite->event_id * ite_esz;
2225
2226		/*
2227		 * If an LPI carries the HW bit, this means that this
2228		 * interrupt is controlled by GICv4, and we do not
2229		 * have direct access to that state. Let's simply fail
2230		 * the save operation...
2231		 */
2232		if (ite->irq->hw)
2233			return -EACCES;
2234
2235		ret = vgic_its_save_ite(its, device, ite, gpa, ite_esz);
2236		if (ret)
2237			return ret;
2238	}
2239	return 0;
2240}
2241
2242/**
2243 * vgic_its_restore_itt - restore the ITT of a device
2244 *
2245 * @its: its handle
2246 * @dev: device handle
2247 *
2248 * Return 0 on success, < 0 on error
2249 */
2250static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
2251{
2252	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2253	gpa_t base = dev->itt_addr;
2254	int ret;
2255	int ite_esz = abi->ite_esz;
2256	size_t max_size = BIT_ULL(dev->num_eventid_bits) * ite_esz;
2257
2258	ret = scan_its_table(its, base, max_size, ite_esz, 0,
2259			     vgic_its_restore_ite, dev);
2260
2261	/* scan_its_table returns +1 if all ITEs are invalid */
2262	if (ret > 0)
2263		ret = 0;
2264
2265	return ret;
2266}
2267
2268/**
2269 * vgic_its_save_dte - Save a device table entry at a given GPA
2270 *
2271 * @its: ITS handle
2272 * @dev: ITS device
2273 * @ptr: GPA
2274 */
2275static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev,
2276			     gpa_t ptr, int dte_esz)
2277{
2278	struct kvm *kvm = its->dev->kvm;
2279	u64 val, itt_addr_field;
2280	u32 next_offset;
2281
2282	itt_addr_field = dev->itt_addr >> 8;
2283	next_offset = compute_next_devid_offset(&its->device_list, dev);
2284	val = (1ULL << KVM_ITS_DTE_VALID_SHIFT |
2285	       ((u64)next_offset << KVM_ITS_DTE_NEXT_SHIFT) |
2286	       (itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) |
2287		(dev->num_eventid_bits - 1));
2288	val = cpu_to_le64(val);
2289	return kvm_write_guest_lock(kvm, ptr, &val, dte_esz);
2290}
2291
2292/**
2293 * vgic_its_restore_dte - restore a device table entry
2294 *
2295 * @its: its handle
2296 * @id: device id the DTE corresponds to
2297 * @ptr: kernel VA where the 8 byte DTE is located
2298 * @opaque: unused
2299 *
2300 * Return: < 0 on error, 0 if the dte is the last one, id offset to the
2301 * next dte otherwise
2302 */
2303static int vgic_its_restore_dte(struct vgic_its *its, u32 id,
2304				void *ptr, void *opaque)
2305{
2306	struct its_device *dev;
2307	gpa_t itt_addr;
2308	u8 num_eventid_bits;
2309	u64 entry = *(u64 *)ptr;
2310	bool valid;
2311	u32 offset;
2312	int ret;
2313
2314	entry = le64_to_cpu(entry);
2315
2316	valid = entry >> KVM_ITS_DTE_VALID_SHIFT;
2317	num_eventid_bits = (entry & KVM_ITS_DTE_SIZE_MASK) + 1;
2318	itt_addr = ((entry & KVM_ITS_DTE_ITTADDR_MASK)
2319			>> KVM_ITS_DTE_ITTADDR_SHIFT) << 8;
2320
2321	if (!valid)
2322		return 1;
2323
2324	/* dte entry is valid */
2325	offset = (entry & KVM_ITS_DTE_NEXT_MASK) >> KVM_ITS_DTE_NEXT_SHIFT;
2326
2327	dev = vgic_its_alloc_device(its, id, itt_addr, num_eventid_bits);
2328	if (IS_ERR(dev))
2329		return PTR_ERR(dev);
2330
2331	ret = vgic_its_restore_itt(its, dev);
2332	if (ret) {
2333		vgic_its_free_device(its->dev->kvm, dev);
2334		return ret;
2335	}
2336
2337	return offset;
2338}
2339
2340static int vgic_its_device_cmp(void *priv, const struct list_head *a,
2341			       const struct list_head *b)
2342{
2343	struct its_device *deva = container_of(a, struct its_device, dev_list);
2344	struct its_device *devb = container_of(b, struct its_device, dev_list);
2345
2346	if (deva->device_id < devb->device_id)
2347		return -1;
2348	else
2349		return 1;
2350}
2351
2352/**
2353 * vgic_its_save_device_tables - Save the device table and all ITT
2354 * into guest RAM
2355 *
2356 * L1/L2 handling is hidden by vgic_its_check_id() helper which directly
2357 * returns the GPA of the device entry
2358 */
2359static int vgic_its_save_device_tables(struct vgic_its *its)
2360{
2361	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2362	u64 baser = its->baser_device_table;
2363	struct its_device *dev;
2364	int dte_esz = abi->dte_esz;
2365
2366	if (!(baser & GITS_BASER_VALID))
2367		return 0;
2368
2369	list_sort(NULL, &its->device_list, vgic_its_device_cmp);
2370
2371	list_for_each_entry(dev, &its->device_list, dev_list) {
2372		int ret;
2373		gpa_t eaddr;
2374
2375		if (!vgic_its_check_id(its, baser,
2376				       dev->device_id, &eaddr))
2377			return -EINVAL;
2378
2379		ret = vgic_its_save_itt(its, dev);
2380		if (ret)
2381			return ret;
2382
2383		ret = vgic_its_save_dte(its, dev, eaddr, dte_esz);
2384		if (ret)
2385			return ret;
2386	}
2387	return 0;
2388}
2389
2390/**
2391 * handle_l1_dte - callback used for L1 device table entries (2 stage case)
2392 *
2393 * @its: its handle
2394 * @id: index of the entry in the L1 table
2395 * @addr: kernel VA
2396 * @opaque: unused
2397 *
2398 * L1 table entries are scanned by steps of 1 entry
2399 * Return < 0 if error, 0 if last dte was found when scanning the L2
2400 * table, +1 otherwise (meaning next L1 entry must be scanned)
2401 */
2402static int handle_l1_dte(struct vgic_its *its, u32 id, void *addr,
2403			 void *opaque)
2404{
2405	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2406	int l2_start_id = id * (SZ_64K / abi->dte_esz);
2407	u64 entry = *(u64 *)addr;
2408	int dte_esz = abi->dte_esz;
2409	gpa_t gpa;
2410	int ret;
2411
2412	entry = le64_to_cpu(entry);
2413
2414	if (!(entry & KVM_ITS_L1E_VALID_MASK))
2415		return 1;
2416
2417	gpa = entry & KVM_ITS_L1E_ADDR_MASK;
2418
2419	ret = scan_its_table(its, gpa, SZ_64K, dte_esz,
2420			     l2_start_id, vgic_its_restore_dte, NULL);
2421
2422	return ret;
2423}
2424
2425/**
2426 * vgic_its_restore_device_tables - Restore the device table and all ITT
2427 * from guest RAM to internal data structs
2428 */
2429static int vgic_its_restore_device_tables(struct vgic_its *its)
2430{
2431	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2432	u64 baser = its->baser_device_table;
2433	int l1_esz, ret;
2434	int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
2435	gpa_t l1_gpa;
2436
2437	if (!(baser & GITS_BASER_VALID))
2438		return 0;
2439
2440	l1_gpa = GITS_BASER_ADDR_48_to_52(baser);
2441
2442	if (baser & GITS_BASER_INDIRECT) {
2443		l1_esz = GITS_LVL1_ENTRY_SIZE;
2444		ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
2445				     handle_l1_dte, NULL);
2446	} else {
2447		l1_esz = abi->dte_esz;
2448		ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
2449				     vgic_its_restore_dte, NULL);
2450	}
2451
2452	/* scan_its_table returns +1 if all entries are invalid */
2453	if (ret > 0)
2454		ret = 0;
2455
2456	return ret;
2457}
2458
2459static int vgic_its_save_cte(struct vgic_its *its,
2460			     struct its_collection *collection,
2461			     gpa_t gpa, int esz)
2462{
2463	u64 val;
2464
2465	val = (1ULL << KVM_ITS_CTE_VALID_SHIFT |
2466	       ((u64)collection->target_addr << KVM_ITS_CTE_RDBASE_SHIFT) |
2467	       collection->collection_id);
2468	val = cpu_to_le64(val);
2469	return kvm_write_guest_lock(its->dev->kvm, gpa, &val, esz);
2470}
2471
2472static int vgic_its_restore_cte(struct vgic_its *its, gpa_t gpa, int esz)
2473{
2474	struct its_collection *collection;
2475	struct kvm *kvm = its->dev->kvm;
2476	u32 target_addr, coll_id;
2477	u64 val;
2478	int ret;
2479
2480	BUG_ON(esz > sizeof(val));
2481	ret = kvm_read_guest_lock(kvm, gpa, &val, esz);
2482	if (ret)
2483		return ret;
2484	val = le64_to_cpu(val);
2485	if (!(val & KVM_ITS_CTE_VALID_MASK))
2486		return 0;
2487
2488	target_addr = (u32)(val >> KVM_ITS_CTE_RDBASE_SHIFT);
2489	coll_id = val & KVM_ITS_CTE_ICID_MASK;
2490
2491	if (target_addr != COLLECTION_NOT_MAPPED &&
2492	    target_addr >= atomic_read(&kvm->online_vcpus))
2493		return -EINVAL;
2494
2495	collection = find_collection(its, coll_id);
2496	if (collection)
2497		return -EEXIST;
2498	ret = vgic_its_alloc_collection(its, &collection, coll_id);
2499	if (ret)
2500		return ret;
2501	collection->target_addr = target_addr;
2502	return 1;
2503}
2504
2505/**
2506 * vgic_its_save_collection_table - Save the collection table into
2507 * guest RAM
2508 */
2509static int vgic_its_save_collection_table(struct vgic_its *its)
2510{
2511	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2512	u64 baser = its->baser_coll_table;
2513	gpa_t gpa = GITS_BASER_ADDR_48_to_52(baser);
2514	struct its_collection *collection;
2515	u64 val;
2516	size_t max_size, filled = 0;
2517	int ret, cte_esz = abi->cte_esz;
2518
2519	if (!(baser & GITS_BASER_VALID))
2520		return 0;
2521
2522	max_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
2523
2524	list_for_each_entry(collection, &its->collection_list, coll_list) {
2525		ret = vgic_its_save_cte(its, collection, gpa, cte_esz);
2526		if (ret)
2527			return ret;
2528		gpa += cte_esz;
2529		filled += cte_esz;
2530	}
2531
2532	if (filled == max_size)
2533		return 0;
2534
2535	/*
2536	 * table is not fully filled, add a last dummy element
2537	 * with valid bit unset
2538	 */
2539	val = 0;
2540	BUG_ON(cte_esz > sizeof(val));
2541	ret = kvm_write_guest_lock(its->dev->kvm, gpa, &val, cte_esz);
2542	return ret;
2543}
2544
2545/**
2546 * vgic_its_restore_collection_table - reads the collection table
2547 * in guest memory and restores the ITS internal state. Requires the
2548 * BASER registers to be restored before.
2549 */
2550static int vgic_its_restore_collection_table(struct vgic_its *its)
2551{
2552	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2553	u64 baser = its->baser_coll_table;
2554	int cte_esz = abi->cte_esz;
2555	size_t max_size, read = 0;
2556	gpa_t gpa;
2557	int ret;
2558
2559	if (!(baser & GITS_BASER_VALID))
2560		return 0;
2561
2562	gpa = GITS_BASER_ADDR_48_to_52(baser);
2563
2564	max_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
2565
2566	while (read < max_size) {
2567		ret = vgic_its_restore_cte(its, gpa, cte_esz);
2568		if (ret <= 0)
2569			break;
2570		gpa += cte_esz;
2571		read += cte_esz;
2572	}
2573
2574	if (ret > 0)
2575		return 0;
2576
2577	return ret;
2578}
2579
2580/**
2581 * vgic_its_save_tables_v0 - Save the ITS tables into guest ARM
2582 * according to v0 ABI
2583 */
2584static int vgic_its_save_tables_v0(struct vgic_its *its)
2585{
2586	int ret;
2587
2588	ret = vgic_its_save_device_tables(its);
2589	if (ret)
2590		return ret;
2591
2592	return vgic_its_save_collection_table(its);
2593}
2594
2595/**
2596 * vgic_its_restore_tables_v0 - Restore the ITS tables from guest RAM
2597 * to internal data structs according to V0 ABI
2598 *
2599 */
2600static int vgic_its_restore_tables_v0(struct vgic_its *its)
2601{
2602	int ret;
2603
2604	ret = vgic_its_restore_collection_table(its);
2605	if (ret)
2606		return ret;
2607
2608	return vgic_its_restore_device_tables(its);
2609}
2610
2611static int vgic_its_commit_v0(struct vgic_its *its)
2612{
2613	const struct vgic_its_abi *abi;
2614
2615	abi = vgic_its_get_abi(its);
2616	its->baser_coll_table &= ~GITS_BASER_ENTRY_SIZE_MASK;
2617	its->baser_device_table &= ~GITS_BASER_ENTRY_SIZE_MASK;
2618
2619	its->baser_coll_table |= (GIC_ENCODE_SZ(abi->cte_esz, 5)
2620					<< GITS_BASER_ENTRY_SIZE_SHIFT);
2621
2622	its->baser_device_table |= (GIC_ENCODE_SZ(abi->dte_esz, 5)
2623					<< GITS_BASER_ENTRY_SIZE_SHIFT);
2624	return 0;
2625}
2626
2627static void vgic_its_reset(struct kvm *kvm, struct vgic_its *its)
2628{
2629	/* We need to keep the ABI specific field values */
2630	its->baser_coll_table &= ~GITS_BASER_VALID;
2631	its->baser_device_table &= ~GITS_BASER_VALID;
2632	its->cbaser = 0;
2633	its->creadr = 0;
2634	its->cwriter = 0;
2635	its->enabled = 0;
2636	vgic_its_free_device_list(kvm, its);
2637	vgic_its_free_collection_list(kvm, its);
2638}
2639
2640static int vgic_its_has_attr(struct kvm_device *dev,
2641			     struct kvm_device_attr *attr)
2642{
2643	switch (attr->group) {
2644	case KVM_DEV_ARM_VGIC_GRP_ADDR:
2645		switch (attr->attr) {
2646		case KVM_VGIC_ITS_ADDR_TYPE:
2647			return 0;
2648		}
2649		break;
2650	case KVM_DEV_ARM_VGIC_GRP_CTRL:
2651		switch (attr->attr) {
2652		case KVM_DEV_ARM_VGIC_CTRL_INIT:
2653			return 0;
2654		case KVM_DEV_ARM_ITS_CTRL_RESET:
2655			return 0;
2656		case KVM_DEV_ARM_ITS_SAVE_TABLES:
2657			return 0;
2658		case KVM_DEV_ARM_ITS_RESTORE_TABLES:
2659			return 0;
2660		}
2661		break;
2662	case KVM_DEV_ARM_VGIC_GRP_ITS_REGS:
2663		return vgic_its_has_attr_regs(dev, attr);
2664	}
2665	return -ENXIO;
2666}
2667
2668static int vgic_its_ctrl(struct kvm *kvm, struct vgic_its *its, u64 attr)
2669{
2670	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2671	int ret = 0;
2672
2673	if (attr == KVM_DEV_ARM_VGIC_CTRL_INIT) /* Nothing to do */
2674		return 0;
2675
2676	mutex_lock(&kvm->lock);
2677	mutex_lock(&its->its_lock);
2678
2679	if (!lock_all_vcpus(kvm)) {
2680		mutex_unlock(&its->its_lock);
2681		mutex_unlock(&kvm->lock);
2682		return -EBUSY;
2683	}
2684
2685	switch (attr) {
2686	case KVM_DEV_ARM_ITS_CTRL_RESET:
2687		vgic_its_reset(kvm, its);
2688		break;
2689	case KVM_DEV_ARM_ITS_SAVE_TABLES:
2690		ret = abi->save_tables(its);
2691		break;
2692	case KVM_DEV_ARM_ITS_RESTORE_TABLES:
2693		ret = abi->restore_tables(its);
2694		break;
2695	}
2696
2697	unlock_all_vcpus(kvm);
2698	mutex_unlock(&its->its_lock);
2699	mutex_unlock(&kvm->lock);
2700	return ret;
2701}
2702
2703static int vgic_its_set_attr(struct kvm_device *dev,
2704			     struct kvm_device_attr *attr)
2705{
2706	struct vgic_its *its = dev->private;
2707	int ret;
2708
2709	switch (attr->group) {
2710	case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2711		u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2712		unsigned long type = (unsigned long)attr->attr;
2713		u64 addr;
2714
2715		if (type != KVM_VGIC_ITS_ADDR_TYPE)
2716			return -ENODEV;
2717
2718		if (copy_from_user(&addr, uaddr, sizeof(addr)))
2719			return -EFAULT;
2720
2721		ret = vgic_check_ioaddr(dev->kvm, &its->vgic_its_base,
2722					addr, SZ_64K);
2723		if (ret)
2724			return ret;
2725
2726		return vgic_register_its_iodev(dev->kvm, its, addr);
2727	}
2728	case KVM_DEV_ARM_VGIC_GRP_CTRL:
2729		return vgic_its_ctrl(dev->kvm, its, attr->attr);
2730	case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: {
2731		u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2732		u64 reg;
2733
2734		if (get_user(reg, uaddr))
2735			return -EFAULT;
2736
2737		return vgic_its_attr_regs_access(dev, attr, &reg, true);
2738	}
2739	}
2740	return -ENXIO;
2741}
2742
2743static int vgic_its_get_attr(struct kvm_device *dev,
2744			     struct kvm_device_attr *attr)
2745{
2746	switch (attr->group) {
2747	case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2748		struct vgic_its *its = dev->private;
2749		u64 addr = its->vgic_its_base;
2750		u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2751		unsigned long type = (unsigned long)attr->attr;
2752
2753		if (type != KVM_VGIC_ITS_ADDR_TYPE)
2754			return -ENODEV;
2755
2756		if (copy_to_user(uaddr, &addr, sizeof(addr)))
2757			return -EFAULT;
2758		break;
2759	}
2760	case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: {
2761		u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2762		u64 reg;
2763		int ret;
2764
2765		ret = vgic_its_attr_regs_access(dev, attr, &reg, false);
2766		if (ret)
2767			return ret;
2768		return put_user(reg, uaddr);
2769	}
2770	default:
2771		return -ENXIO;
2772	}
2773
2774	return 0;
2775}
2776
2777static struct kvm_device_ops kvm_arm_vgic_its_ops = {
2778	.name = "kvm-arm-vgic-its",
2779	.create = vgic_its_create,
2780	.destroy = vgic_its_destroy,
2781	.set_attr = vgic_its_set_attr,
2782	.get_attr = vgic_its_get_attr,
2783	.has_attr = vgic_its_has_attr,
2784};
2785
2786int kvm_vgic_register_its_device(void)
2787{
2788	return kvm_register_device_ops(&kvm_arm_vgic_its_ops,
2789				       KVM_DEV_TYPE_ARM_VGIC_ITS);
2790}
2791