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