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