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