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