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