blob: 4f6ea46c496c83577dd4a4faaa885a0f81f28337 [file] [log] [blame]
Andre Przywara59c5ab42016-07-15 12:43:30 +01001/*
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>
Andre Przywara424c3382016-07-15 12:43:32 +010024#include <linux/list.h>
Andre Przywara1085fdc2016-07-15 12:43:31 +010025#include <linux/uaccess.h>
Andre Przywara59c5ab42016-07-15 12:43:30 +010026
27#include <linux/irqchip/arm-gic-v3.h>
28
29#include <asm/kvm_emulate.h>
30#include <asm/kvm_arm.h>
31#include <asm/kvm_mmu.h>
32
33#include "vgic.h"
34#include "vgic-mmio.h"
35
Eric Auger71afe472017-04-13 09:06:20 +020036static int vgic_its_save_tables_v0(struct vgic_its *its);
37static int vgic_its_restore_tables_v0(struct vgic_its *its);
38static int vgic_its_commit_v0(struct vgic_its *its);
39
Andre Przywaradf9f58f2016-07-15 12:43:36 +010040/*
41 * Creates a new (reference to a) struct vgic_irq for a given LPI.
42 * If this LPI is already mapped on another ITS, we increase its refcount
43 * and return a pointer to the existing structure.
44 * If this is a "new" LPI, we allocate and initialize a new struct vgic_irq.
45 * This function returns a pointer to the _unlocked_ structure.
46 */
47static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid)
48{
49 struct vgic_dist *dist = &kvm->arch.vgic;
50 struct vgic_irq *irq = vgic_get_irq(kvm, NULL, intid), *oldirq;
51
52 /* In this case there is no put, since we keep the reference. */
53 if (irq)
54 return irq;
55
56 irq = kzalloc(sizeof(struct vgic_irq), GFP_KERNEL);
57 if (!irq)
Christoffer Dall99e5e882016-08-01 20:25:33 +020058 return ERR_PTR(-ENOMEM);
Andre Przywaradf9f58f2016-07-15 12:43:36 +010059
60 INIT_LIST_HEAD(&irq->lpi_list);
61 INIT_LIST_HEAD(&irq->ap_list);
62 spin_lock_init(&irq->irq_lock);
63
64 irq->config = VGIC_CONFIG_EDGE;
65 kref_init(&irq->refcount);
66 irq->intid = intid;
67
68 spin_lock(&dist->lpi_list_lock);
69
70 /*
71 * There could be a race with another vgic_add_lpi(), so we need to
72 * check that we don't add a second list entry with the same LPI.
73 */
74 list_for_each_entry(oldirq, &dist->lpi_list_head, lpi_list) {
75 if (oldirq->intid != intid)
76 continue;
77
78 /* Someone was faster with adding this LPI, lets use that. */
79 kfree(irq);
80 irq = oldirq;
81
82 /*
83 * This increases the refcount, the caller is expected to
84 * call vgic_put_irq() on the returned pointer once it's
85 * finished with the IRQ.
86 */
Marc Zyngierd97594e2016-07-17 11:27:23 +010087 vgic_get_irq_kref(irq);
Andre Przywaradf9f58f2016-07-15 12:43:36 +010088
89 goto out_unlock;
90 }
91
92 list_add_tail(&irq->lpi_list, &dist->lpi_list_head);
93 dist->lpi_list_count++;
94
95out_unlock:
96 spin_unlock(&dist->lpi_list_lock);
97
98 return irq;
99}
100
Andre Przywara424c3382016-07-15 12:43:32 +0100101struct its_device {
102 struct list_head dev_list;
103
104 /* the head for the list of ITTEs */
105 struct list_head itt_head;
106 u32 device_id;
107};
108
109#define COLLECTION_NOT_MAPPED ((u32)~0)
110
111struct its_collection {
112 struct list_head coll_list;
113
114 u32 collection_id;
115 u32 target_addr;
116};
117
118#define its_is_collection_mapped(coll) ((coll) && \
119 ((coll)->target_addr != COLLECTION_NOT_MAPPED))
120
Eric Auger9ce91c72017-02-08 06:09:29 +0100121struct its_ite {
122 struct list_head ite_list;
Andre Przywara424c3382016-07-15 12:43:32 +0100123
Andre Przywara38024112016-07-15 12:43:33 +0100124 struct vgic_irq *irq;
Andre Przywara424c3382016-07-15 12:43:32 +0100125 struct its_collection *collection;
126 u32 lpi;
127 u32 event_id;
128};
129
Eric Auger71afe472017-04-13 09:06:20 +0200130/**
131 * struct vgic_its_abi - ITS abi ops and settings
132 * @cte_esz: collection table entry size
133 * @dte_esz: device table entry size
134 * @ite_esz: interrupt translation table entry size
135 * @save tables: save the ITS tables into guest RAM
136 * @restore_tables: restore the ITS internal structs from tables
137 * stored in guest RAM
138 * @commit: initialize the registers which expose the ABI settings,
139 * especially the entry sizes
140 */
141struct vgic_its_abi {
142 int cte_esz;
143 int dte_esz;
144 int ite_esz;
145 int (*save_tables)(struct vgic_its *its);
146 int (*restore_tables)(struct vgic_its *its);
147 int (*commit)(struct vgic_its *its);
148};
149
150static const struct vgic_its_abi its_table_abi_versions[] = {
151 [0] = {.cte_esz = 8, .dte_esz = 8, .ite_esz = 8,
152 .save_tables = vgic_its_save_tables_v0,
153 .restore_tables = vgic_its_restore_tables_v0,
154 .commit = vgic_its_commit_v0,
155 },
156};
157
158#define NR_ITS_ABIS ARRAY_SIZE(its_table_abi_versions)
159
160inline const struct vgic_its_abi *vgic_its_get_abi(struct vgic_its *its)
161{
162 return &its_table_abi_versions[its->abi_rev];
163}
164
165int vgic_its_set_abi(struct vgic_its *its, int rev)
166{
167 const struct vgic_its_abi *abi;
168
169 its->abi_rev = rev;
170 abi = vgic_its_get_abi(its);
171 return abi->commit(its);
172}
173
Andre Przywara424c3382016-07-15 12:43:32 +0100174/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100175 * Find and returns a device in the device table for an ITS.
176 * Must be called with the its_lock mutex held.
177 */
178static struct its_device *find_its_device(struct vgic_its *its, u32 device_id)
179{
180 struct its_device *device;
181
182 list_for_each_entry(device, &its->device_list, dev_list)
183 if (device_id == device->device_id)
184 return device;
185
186 return NULL;
187}
188
189/*
190 * Find and returns an interrupt translation table entry (ITTE) for a given
191 * Device ID/Event ID pair on an ITS.
192 * Must be called with the its_lock mutex held.
193 */
Eric Auger9ce91c72017-02-08 06:09:29 +0100194static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100195 u32 event_id)
196{
197 struct its_device *device;
Eric Auger9ce91c72017-02-08 06:09:29 +0100198 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100199
200 device = find_its_device(its, device_id);
201 if (device == NULL)
202 return NULL;
203
Eric Auger9ce91c72017-02-08 06:09:29 +0100204 list_for_each_entry(ite, &device->itt_head, ite_list)
205 if (ite->event_id == event_id)
206 return ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100207
208 return NULL;
209}
210
211/* To be used as an iterator this macro misses the enclosing parentheses */
Eric Auger9ce91c72017-02-08 06:09:29 +0100212#define for_each_lpi_its(dev, ite, its) \
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100213 list_for_each_entry(dev, &(its)->device_list, dev_list) \
Eric Auger9ce91c72017-02-08 06:09:29 +0100214 list_for_each_entry(ite, &(dev)->itt_head, ite_list)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100215
216/*
Andre Przywara424c3382016-07-15 12:43:32 +0100217 * We only implement 48 bits of PA at the moment, although the ITS
218 * supports more. Let's be restrictive here.
219 */
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100220#define BASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16))
Andre Przywara424c3382016-07-15 12:43:32 +0100221#define CBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12))
Andre Przywara33d3bc92016-07-15 12:43:34 +0100222#define PENDBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16))
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100223#define PROPBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12))
224
225#define GIC_LPI_OFFSET 8192
226
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100227/*
228 * Finds and returns a collection in the ITS collection table.
229 * Must be called with the its_lock mutex held.
230 */
231static struct its_collection *find_collection(struct vgic_its *its, int coll_id)
232{
233 struct its_collection *collection;
234
235 list_for_each_entry(collection, &its->collection_list, coll_list) {
236 if (coll_id == collection->collection_id)
237 return collection;
238 }
239
240 return NULL;
241}
242
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100243#define LPI_PROP_ENABLE_BIT(p) ((p) & LPI_PROP_ENABLED)
244#define LPI_PROP_PRIORITY(p) ((p) & 0xfc)
245
246/*
247 * Reads the configuration data for a given LPI from guest memory and
248 * updates the fields in struct vgic_irq.
249 * If filter_vcpu is not NULL, applies only if the IRQ is targeting this
250 * VCPU. Unconditionally applies if filter_vcpu is NULL.
251 */
252static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
253 struct kvm_vcpu *filter_vcpu)
254{
255 u64 propbase = PROPBASER_ADDRESS(kvm->arch.vgic.propbaser);
256 u8 prop;
257 int ret;
258
259 ret = kvm_read_guest(kvm, propbase + irq->intid - GIC_LPI_OFFSET,
260 &prop, 1);
261
262 if (ret)
263 return ret;
264
265 spin_lock(&irq->irq_lock);
266
267 if (!filter_vcpu || filter_vcpu == irq->target_vcpu) {
268 irq->priority = LPI_PROP_PRIORITY(prop);
269 irq->enabled = LPI_PROP_ENABLE_BIT(prop);
270
271 vgic_queue_irq_unlock(kvm, irq);
272 } else {
273 spin_unlock(&irq->irq_lock);
274 }
275
276 return 0;
277}
Andre Przywara33d3bc92016-07-15 12:43:34 +0100278
279/*
280 * Create a snapshot of the current LPI list, so that we can enumerate all
281 * LPIs without holding any lock.
282 * Returns the array length and puts the kmalloc'ed array into intid_ptr.
283 */
284static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
285{
286 struct vgic_dist *dist = &kvm->arch.vgic;
287 struct vgic_irq *irq;
288 u32 *intids;
289 int irq_count = dist->lpi_list_count, i = 0;
290
291 /*
292 * We use the current value of the list length, which may change
293 * after the kmalloc. We don't care, because the guest shouldn't
294 * change anything while the command handling is still running,
295 * and in the worst case we would miss a new IRQ, which one wouldn't
296 * expect to be covered by this command anyway.
297 */
298 intids = kmalloc_array(irq_count, sizeof(intids[0]), GFP_KERNEL);
299 if (!intids)
300 return -ENOMEM;
301
302 spin_lock(&dist->lpi_list_lock);
303 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
304 /* We don't need to "get" the IRQ, as we hold the list lock. */
305 intids[i] = irq->intid;
306 if (++i == irq_count)
307 break;
308 }
309 spin_unlock(&dist->lpi_list_lock);
310
311 *intid_ptr = intids;
312 return irq_count;
313}
314
315/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100316 * Promotes the ITS view of affinity of an ITTE (which redistributor this LPI
317 * is targeting) to the VGIC's view, which deals with target VCPUs.
318 * Needs to be called whenever either the collection for a LPIs has
319 * changed or the collection itself got retargeted.
320 */
Eric Auger9ce91c72017-02-08 06:09:29 +0100321static void update_affinity_ite(struct kvm *kvm, struct its_ite *ite)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100322{
323 struct kvm_vcpu *vcpu;
324
Eric Auger9ce91c72017-02-08 06:09:29 +0100325 if (!its_is_collection_mapped(ite->collection))
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100326 return;
327
Eric Auger9ce91c72017-02-08 06:09:29 +0100328 vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100329
Eric Auger9ce91c72017-02-08 06:09:29 +0100330 spin_lock(&ite->irq->irq_lock);
331 ite->irq->target_vcpu = vcpu;
332 spin_unlock(&ite->irq->irq_lock);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100333}
334
335/*
336 * Updates the target VCPU for every LPI targeting this collection.
337 * Must be called with the its_lock mutex held.
338 */
339static void update_affinity_collection(struct kvm *kvm, struct vgic_its *its,
340 struct its_collection *coll)
341{
342 struct its_device *device;
Eric Auger9ce91c72017-02-08 06:09:29 +0100343 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100344
Eric Auger9ce91c72017-02-08 06:09:29 +0100345 for_each_lpi_its(device, ite, its) {
346 if (!ite->collection || coll != ite->collection)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100347 continue;
348
Eric Auger9ce91c72017-02-08 06:09:29 +0100349 update_affinity_ite(kvm, ite);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100350 }
351}
352
353static u32 max_lpis_propbaser(u64 propbaser)
354{
355 int nr_idbits = (propbaser & 0x1f) + 1;
356
357 return 1U << min(nr_idbits, INTERRUPT_ID_BITS_ITS);
358}
359
360/*
Andre Przywara33d3bc92016-07-15 12:43:34 +0100361 * Scan the whole LPI pending table and sync the pending bit in there
362 * with our own data structures. This relies on the LPI being
363 * mapped before.
364 */
365static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
366{
367 gpa_t pendbase = PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
368 struct vgic_irq *irq;
369 int last_byte_offset = -1;
370 int ret = 0;
371 u32 *intids;
372 int nr_irqs, i;
373
374 nr_irqs = vgic_copy_lpi_list(vcpu->kvm, &intids);
375 if (nr_irqs < 0)
376 return nr_irqs;
377
378 for (i = 0; i < nr_irqs; i++) {
379 int byte_offset, bit_nr;
380 u8 pendmask;
381
382 byte_offset = intids[i] / BITS_PER_BYTE;
383 bit_nr = intids[i] % BITS_PER_BYTE;
384
385 /*
386 * For contiguously allocated LPIs chances are we just read
387 * this very same byte in the last iteration. Reuse that.
388 */
389 if (byte_offset != last_byte_offset) {
390 ret = kvm_read_guest(vcpu->kvm, pendbase + byte_offset,
391 &pendmask, 1);
392 if (ret) {
393 kfree(intids);
394 return ret;
395 }
396 last_byte_offset = byte_offset;
397 }
398
399 irq = vgic_get_irq(vcpu->kvm, NULL, intids[i]);
400 spin_lock(&irq->irq_lock);
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100401 irq->pending_latch = pendmask & (1U << bit_nr);
Andre Przywara33d3bc92016-07-15 12:43:34 +0100402 vgic_queue_irq_unlock(vcpu->kvm, irq);
403 vgic_put_irq(vcpu->kvm, irq);
404 }
405
406 kfree(intids);
407
408 return ret;
409}
Andre Przywara424c3382016-07-15 12:43:32 +0100410
Andre Przywara424c3382016-07-15 12:43:32 +0100411static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,
412 struct vgic_its *its,
413 gpa_t addr, unsigned int len)
414{
Eric Auger71afe472017-04-13 09:06:20 +0200415 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
Andre Przywara424c3382016-07-15 12:43:32 +0100416 u64 reg = GITS_TYPER_PLPIS;
417
418 /*
419 * We use linear CPU numbers for redistributor addressing,
420 * so GITS_TYPER.PTA is 0.
421 * Also we force all PROPBASER registers to be the same, so
422 * CommonLPIAff is 0 as well.
423 * To avoid memory waste in the guest, we keep the number of IDBits and
424 * DevBits low - as least for the time being.
425 */
426 reg |= 0x0f << GITS_TYPER_DEVBITS_SHIFT;
427 reg |= 0x0f << GITS_TYPER_IDBITS_SHIFT;
Eric Auger71afe472017-04-13 09:06:20 +0200428 reg |= GIC_ENCODE_SZ(abi->ite_esz, 4) << GITS_TYPER_ITT_ENTRY_SIZE_SHIFT;
Andre Przywara424c3382016-07-15 12:43:32 +0100429
430 return extract_bytes(reg, addr & 7, len);
431}
432
433static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm,
434 struct vgic_its *its,
435 gpa_t addr, unsigned int len)
436{
437 return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
438}
439
440static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm,
441 struct vgic_its *its,
442 gpa_t addr, unsigned int len)
443{
444 switch (addr & 0xffff) {
445 case GITS_PIDR0:
446 return 0x92; /* part number, bits[7:0] */
447 case GITS_PIDR1:
448 return 0xb4; /* part number, bits[11:8] */
449 case GITS_PIDR2:
450 return GIC_PIDR2_ARCH_GICv3 | 0x0b;
451 case GITS_PIDR4:
452 return 0x40; /* This is a 64K software visible page */
453 /* The following are the ID registers for (any) GIC. */
454 case GITS_CIDR0:
455 return 0x0d;
456 case GITS_CIDR1:
457 return 0xf0;
458 case GITS_CIDR2:
459 return 0x05;
460 case GITS_CIDR3:
461 return 0xb1;
462 }
463
464 return 0;
465}
466
Andre Przywara2891a7d2016-07-15 12:43:37 +0100467/*
468 * Find the target VCPU and the LPI number for a given devid/eventid pair
469 * and make this IRQ pending, possibly injecting it.
470 * Must be called with the its_lock mutex held.
Andre Przywarafd837b02016-08-08 17:29:28 +0100471 * Returns 0 on success, a positive error value for any ITS mapping
472 * related errors and negative error values for generic errors.
Andre Przywara2891a7d2016-07-15 12:43:37 +0100473 */
Andre Przywarafd837b02016-08-08 17:29:28 +0100474static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its,
475 u32 devid, u32 eventid)
Andre Przywara2891a7d2016-07-15 12:43:37 +0100476{
Andre Przywarafd837b02016-08-08 17:29:28 +0100477 struct kvm_vcpu *vcpu;
Eric Auger9ce91c72017-02-08 06:09:29 +0100478 struct its_ite *ite;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100479
480 if (!its->enabled)
Andre Przywarafd837b02016-08-08 17:29:28 +0100481 return -EBUSY;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100482
Eric Auger9ce91c72017-02-08 06:09:29 +0100483 ite = find_ite(its, devid, eventid);
484 if (!ite || !its_is_collection_mapped(ite->collection))
Andre Przywarafd837b02016-08-08 17:29:28 +0100485 return E_ITS_INT_UNMAPPED_INTERRUPT;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100486
Eric Auger9ce91c72017-02-08 06:09:29 +0100487 vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr);
Andre Przywarafd837b02016-08-08 17:29:28 +0100488 if (!vcpu)
489 return E_ITS_INT_UNMAPPED_INTERRUPT;
490
491 if (!vcpu->arch.vgic_cpu.lpis_enabled)
492 return -EBUSY;
493
Eric Auger9ce91c72017-02-08 06:09:29 +0100494 spin_lock(&ite->irq->irq_lock);
495 ite->irq->pending_latch = true;
496 vgic_queue_irq_unlock(kvm, ite->irq);
Andre Przywarafd837b02016-08-08 17:29:28 +0100497
498 return 0;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100499}
500
Andre Przywara505a19e2016-08-09 10:54:29 +0100501static struct vgic_io_device *vgic_get_its_iodev(struct kvm_io_device *dev)
502{
503 struct vgic_io_device *iodev;
504
505 if (dev->ops != &kvm_io_gic_ops)
506 return NULL;
507
508 iodev = container_of(dev, struct vgic_io_device, dev);
509
510 if (iodev->iodev_type != IODEV_ITS)
511 return NULL;
512
513 return iodev;
514}
515
Andre Przywara2891a7d2016-07-15 12:43:37 +0100516/*
517 * Queries the KVM IO bus framework to get the ITS pointer from the given
518 * doorbell address.
519 * We then call vgic_its_trigger_msi() with the decoded data.
Andre Przywarafd837b02016-08-08 17:29:28 +0100520 * According to the KVM_SIGNAL_MSI API description returns 1 on success.
Andre Przywara2891a7d2016-07-15 12:43:37 +0100521 */
522int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi)
523{
524 u64 address;
525 struct kvm_io_device *kvm_io_dev;
526 struct vgic_io_device *iodev;
Andre Przywarafd837b02016-08-08 17:29:28 +0100527 int ret;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100528
529 if (!vgic_has_its(kvm))
530 return -ENODEV;
531
532 if (!(msi->flags & KVM_MSI_VALID_DEVID))
533 return -EINVAL;
534
535 address = (u64)msi->address_hi << 32 | msi->address_lo;
536
537 kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, address);
538 if (!kvm_io_dev)
Andre Przywara505a19e2016-08-09 10:54:29 +0100539 return -EINVAL;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100540
Andre Przywara505a19e2016-08-09 10:54:29 +0100541 iodev = vgic_get_its_iodev(kvm_io_dev);
542 if (!iodev)
543 return -EINVAL;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100544
545 mutex_lock(&iodev->its->its_lock);
Andre Przywarafd837b02016-08-08 17:29:28 +0100546 ret = vgic_its_trigger_msi(kvm, iodev->its, msi->devid, msi->data);
Andre Przywara2891a7d2016-07-15 12:43:37 +0100547 mutex_unlock(&iodev->its->its_lock);
548
Andre Przywarafd837b02016-08-08 17:29:28 +0100549 if (ret < 0)
550 return ret;
551
552 /*
553 * KVM_SIGNAL_MSI demands a return value > 0 for success and 0
554 * if the guest has blocked the MSI. So we map any LPI mapping
555 * related error to that.
556 */
557 if (ret)
558 return 0;
559 else
560 return 1;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100561}
562
Andre Przywara424c3382016-07-15 12:43:32 +0100563/* Requires the its_lock to be held. */
Eric Auger9ce91c72017-02-08 06:09:29 +0100564static void its_free_ite(struct kvm *kvm, struct its_ite *ite)
Andre Przywara424c3382016-07-15 12:43:32 +0100565{
Eric Auger9ce91c72017-02-08 06:09:29 +0100566 list_del(&ite->ite_list);
Andre Przywara38024112016-07-15 12:43:33 +0100567
568 /* This put matches the get in vgic_add_lpi. */
Eric Auger9ce91c72017-02-08 06:09:29 +0100569 if (ite->irq)
570 vgic_put_irq(kvm, ite->irq);
Andre Przywara38024112016-07-15 12:43:33 +0100571
Eric Auger9ce91c72017-02-08 06:09:29 +0100572 kfree(ite);
Andre Przywara424c3382016-07-15 12:43:32 +0100573}
574
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100575static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size)
576{
577 return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT_ULL(size) - 1);
578}
579
580#define its_cmd_get_command(cmd) its_cmd_mask_field(cmd, 0, 0, 8)
581#define its_cmd_get_deviceid(cmd) its_cmd_mask_field(cmd, 0, 32, 32)
582#define its_cmd_get_id(cmd) its_cmd_mask_field(cmd, 1, 0, 32)
583#define its_cmd_get_physical_id(cmd) its_cmd_mask_field(cmd, 1, 32, 32)
584#define its_cmd_get_collection(cmd) its_cmd_mask_field(cmd, 2, 0, 16)
585#define its_cmd_get_target_addr(cmd) its_cmd_mask_field(cmd, 2, 16, 32)
586#define its_cmd_get_validbit(cmd) its_cmd_mask_field(cmd, 2, 63, 1)
587
588/*
589 * The DISCARD command frees an Interrupt Translation Table Entry (ITTE).
590 * Must be called with the its_lock mutex held.
591 */
592static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its,
593 u64 *its_cmd)
594{
595 u32 device_id = its_cmd_get_deviceid(its_cmd);
596 u32 event_id = its_cmd_get_id(its_cmd);
Eric Auger9ce91c72017-02-08 06:09:29 +0100597 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100598
599
Eric Auger9ce91c72017-02-08 06:09:29 +0100600 ite = find_ite(its, device_id, event_id);
601 if (ite && ite->collection) {
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100602 /*
603 * Though the spec talks about removing the pending state, we
604 * don't bother here since we clear the ITTE anyway and the
605 * pending state is a property of the ITTE struct.
606 */
Eric Auger9ce91c72017-02-08 06:09:29 +0100607 its_free_ite(kvm, ite);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100608 return 0;
609 }
610
611 return E_ITS_DISCARD_UNMAPPED_INTERRUPT;
612}
613
614/*
615 * The MOVI command moves an ITTE to a different collection.
616 * Must be called with the its_lock mutex held.
617 */
618static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,
619 u64 *its_cmd)
620{
621 u32 device_id = its_cmd_get_deviceid(its_cmd);
622 u32 event_id = its_cmd_get_id(its_cmd);
623 u32 coll_id = its_cmd_get_collection(its_cmd);
624 struct kvm_vcpu *vcpu;
Eric Auger9ce91c72017-02-08 06:09:29 +0100625 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100626 struct its_collection *collection;
627
Eric Auger9ce91c72017-02-08 06:09:29 +0100628 ite = find_ite(its, device_id, event_id);
629 if (!ite)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100630 return E_ITS_MOVI_UNMAPPED_INTERRUPT;
631
Eric Auger9ce91c72017-02-08 06:09:29 +0100632 if (!its_is_collection_mapped(ite->collection))
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100633 return E_ITS_MOVI_UNMAPPED_COLLECTION;
634
635 collection = find_collection(its, coll_id);
636 if (!its_is_collection_mapped(collection))
637 return E_ITS_MOVI_UNMAPPED_COLLECTION;
638
Eric Auger9ce91c72017-02-08 06:09:29 +0100639 ite->collection = collection;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100640 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
641
Eric Auger9ce91c72017-02-08 06:09:29 +0100642 spin_lock(&ite->irq->irq_lock);
643 ite->irq->target_vcpu = vcpu;
644 spin_unlock(&ite->irq->irq_lock);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100645
646 return 0;
647}
648
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100649/*
650 * Check whether an ID can be stored into the corresponding guest table.
651 * For a direct table this is pretty easy, but gets a bit nasty for
652 * indirect tables. We check whether the resulting guest physical address
653 * is actually valid (covered by a memslot and guest accessbible).
654 * For this we have to read the respective first level entry.
655 */
656static bool vgic_its_check_id(struct vgic_its *its, u64 baser, int id)
657{
658 int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
659 int index;
660 u64 indirect_ptr;
661 gfn_t gfn;
Vladimir Murzine29bd6f2016-11-02 11:55:33 +0000662 int esz = GITS_BASER_ENTRY_SIZE(baser);
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100663
664 if (!(baser & GITS_BASER_INDIRECT)) {
665 phys_addr_t addr;
666
Vladimir Murzine29bd6f2016-11-02 11:55:33 +0000667 if (id >= (l1_tbl_size / esz))
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100668 return false;
669
Vladimir Murzine29bd6f2016-11-02 11:55:33 +0000670 addr = BASER_ADDRESS(baser) + id * esz;
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100671 gfn = addr >> PAGE_SHIFT;
672
673 return kvm_is_visible_gfn(its->dev->kvm, gfn);
674 }
675
676 /* calculate and check the index into the 1st level */
Vladimir Murzine29bd6f2016-11-02 11:55:33 +0000677 index = id / (SZ_64K / esz);
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100678 if (index >= (l1_tbl_size / sizeof(u64)))
679 return false;
680
681 /* Each 1st level entry is represented by a 64-bit value. */
682 if (kvm_read_guest(its->dev->kvm,
683 BASER_ADDRESS(baser) + index * sizeof(indirect_ptr),
684 &indirect_ptr, sizeof(indirect_ptr)))
685 return false;
686
687 indirect_ptr = le64_to_cpu(indirect_ptr);
688
689 /* check the valid bit of the first level entry */
690 if (!(indirect_ptr & BIT_ULL(63)))
691 return false;
692
693 /*
694 * Mask the guest physical address and calculate the frame number.
695 * Any address beyond our supported 48 bits of PA will be caught
696 * by the actual check in the final step.
697 */
698 indirect_ptr &= GENMASK_ULL(51, 16);
699
700 /* Find the address of the actual entry */
Vladimir Murzine29bd6f2016-11-02 11:55:33 +0000701 index = id % (SZ_64K / esz);
702 indirect_ptr += index * esz;
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100703 gfn = indirect_ptr >> PAGE_SHIFT;
704
705 return kvm_is_visible_gfn(its->dev->kvm, gfn);
706}
707
Marc Zyngier17a21f52016-07-17 20:01:46 +0100708static int vgic_its_alloc_collection(struct vgic_its *its,
709 struct its_collection **colp,
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100710 u32 coll_id)
711{
Marc Zyngier17a21f52016-07-17 20:01:46 +0100712 struct its_collection *collection;
713
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100714 if (!vgic_its_check_id(its, its->baser_coll_table, coll_id))
715 return E_ITS_MAPC_COLLECTION_OOR;
716
Marc Zyngier17a21f52016-07-17 20:01:46 +0100717 collection = kzalloc(sizeof(*collection), GFP_KERNEL);
718
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100719 collection->collection_id = coll_id;
720 collection->target_addr = COLLECTION_NOT_MAPPED;
721
722 list_add_tail(&collection->coll_list, &its->collection_list);
Marc Zyngier17a21f52016-07-17 20:01:46 +0100723 *colp = collection;
724
725 return 0;
726}
727
728static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)
729{
730 struct its_collection *collection;
731 struct its_device *device;
Eric Auger9ce91c72017-02-08 06:09:29 +0100732 struct its_ite *ite;
Marc Zyngier17a21f52016-07-17 20:01:46 +0100733
734 /*
735 * Clearing the mapping for that collection ID removes the
736 * entry from the list. If there wasn't any before, we can
737 * go home early.
738 */
739 collection = find_collection(its, coll_id);
740 if (!collection)
741 return;
742
Eric Auger9ce91c72017-02-08 06:09:29 +0100743 for_each_lpi_its(device, ite, its)
744 if (ite->collection &&
745 ite->collection->collection_id == coll_id)
746 ite->collection = NULL;
Marc Zyngier17a21f52016-07-17 20:01:46 +0100747
748 list_del(&collection->coll_list);
749 kfree(collection);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100750}
751
752/*
753 * The MAPTI and MAPI commands map LPIs to ITTEs.
754 * Must be called with its_lock mutex held.
755 */
756static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
Marc Zyngiera3e7aa22016-07-17 22:38:32 +0100757 u64 *its_cmd)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100758{
759 u32 device_id = its_cmd_get_deviceid(its_cmd);
760 u32 event_id = its_cmd_get_id(its_cmd);
761 u32 coll_id = its_cmd_get_collection(its_cmd);
Eric Auger9ce91c72017-02-08 06:09:29 +0100762 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100763 struct its_device *device;
764 struct its_collection *collection, *new_coll = NULL;
765 int lpi_nr;
Christoffer Dall99e5e882016-08-01 20:25:33 +0200766 struct vgic_irq *irq;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100767
768 device = find_its_device(its, device_id);
769 if (!device)
770 return E_ITS_MAPTI_UNMAPPED_DEVICE;
771
Marc Zyngiera3e7aa22016-07-17 22:38:32 +0100772 if (its_cmd_get_command(its_cmd) == GITS_CMD_MAPTI)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100773 lpi_nr = its_cmd_get_physical_id(its_cmd);
774 else
775 lpi_nr = event_id;
776 if (lpi_nr < GIC_LPI_OFFSET ||
Marc Zyngier3a88bde2016-07-18 16:27:14 +0100777 lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser))
778 return E_ITS_MAPTI_PHYSICALID_OOR;
779
Andre Przywara286054a2016-08-16 17:51:06 +0100780 /* If there is an existing mapping, behavior is UNPREDICTABLE. */
Eric Auger9ce91c72017-02-08 06:09:29 +0100781 if (find_ite(its, device_id, event_id))
Andre Przywara286054a2016-08-16 17:51:06 +0100782 return 0;
783
Marc Zyngier3a88bde2016-07-18 16:27:14 +0100784 collection = find_collection(its, coll_id);
785 if (!collection) {
786 int ret = vgic_its_alloc_collection(its, &collection, coll_id);
787 if (ret)
788 return ret;
789 new_coll = collection;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100790 }
791
Eric Auger9ce91c72017-02-08 06:09:29 +0100792 ite = kzalloc(sizeof(struct its_ite), GFP_KERNEL);
793 if (!ite) {
Andre Przywara286054a2016-08-16 17:51:06 +0100794 if (new_coll)
795 vgic_its_free_collection(its, coll_id);
796 return -ENOMEM;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100797 }
798
Eric Auger9ce91c72017-02-08 06:09:29 +0100799 ite->event_id = event_id;
800 list_add_tail(&ite->ite_list, &device->itt_head);
Andre Przywara286054a2016-08-16 17:51:06 +0100801
Eric Auger9ce91c72017-02-08 06:09:29 +0100802 ite->collection = collection;
803 ite->lpi = lpi_nr;
Christoffer Dall99e5e882016-08-01 20:25:33 +0200804
805 irq = vgic_add_lpi(kvm, lpi_nr);
806 if (IS_ERR(irq)) {
807 if (new_coll)
808 vgic_its_free_collection(its, coll_id);
Eric Auger9ce91c72017-02-08 06:09:29 +0100809 its_free_ite(kvm, ite);
Christoffer Dall99e5e882016-08-01 20:25:33 +0200810 return PTR_ERR(irq);
811 }
Eric Auger9ce91c72017-02-08 06:09:29 +0100812 ite->irq = irq;
Christoffer Dall99e5e882016-08-01 20:25:33 +0200813
Eric Auger9ce91c72017-02-08 06:09:29 +0100814 update_affinity_ite(kvm, ite);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100815
816 /*
817 * We "cache" the configuration table entries in out struct vgic_irq's.
818 * However we only have those structs for mapped IRQs, so we read in
819 * the respective config data from memory here upon mapping the LPI.
820 */
Eric Auger9ce91c72017-02-08 06:09:29 +0100821 update_lpi_config(kvm, ite->irq, NULL);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100822
823 return 0;
824}
825
826/* Requires the its_lock to be held. */
827static void vgic_its_unmap_device(struct kvm *kvm, struct its_device *device)
828{
Eric Auger9ce91c72017-02-08 06:09:29 +0100829 struct its_ite *ite, *temp;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100830
831 /*
832 * The spec says that unmapping a device with still valid
833 * ITTEs associated is UNPREDICTABLE. We remove all ITTEs,
834 * since we cannot leave the memory unreferenced.
835 */
Eric Auger9ce91c72017-02-08 06:09:29 +0100836 list_for_each_entry_safe(ite, temp, &device->itt_head, ite_list)
837 its_free_ite(kvm, ite);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100838
839 list_del(&device->dev_list);
840 kfree(device);
841}
842
843/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100844 * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs).
845 * Must be called with the its_lock mutex held.
846 */
847static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
848 u64 *its_cmd)
849{
850 u32 device_id = its_cmd_get_deviceid(its_cmd);
851 bool valid = its_cmd_get_validbit(its_cmd);
852 struct its_device *device;
853
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100854 if (!vgic_its_check_id(its, its->baser_device_table, device_id))
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100855 return E_ITS_MAPD_DEVICE_OOR;
856
857 device = find_its_device(its, device_id);
858
859 /*
860 * The spec says that calling MAPD on an already mapped device
861 * invalidates all cached data for this device. We implement this
862 * by removing the mapping and re-establishing it.
863 */
864 if (device)
865 vgic_its_unmap_device(kvm, device);
866
867 /*
868 * The spec does not say whether unmapping a not-mapped device
869 * is an error, so we are done in any case.
870 */
871 if (!valid)
872 return 0;
873
874 device = kzalloc(sizeof(struct its_device), GFP_KERNEL);
875 if (!device)
876 return -ENOMEM;
877
878 device->device_id = device_id;
879 INIT_LIST_HEAD(&device->itt_head);
880
881 list_add_tail(&device->dev_list, &its->device_list);
882
883 return 0;
884}
885
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100886/*
887 * The MAPC command maps collection IDs to redistributors.
888 * Must be called with the its_lock mutex held.
889 */
890static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its,
891 u64 *its_cmd)
892{
893 u16 coll_id;
894 u32 target_addr;
895 struct its_collection *collection;
896 bool valid;
897
898 valid = its_cmd_get_validbit(its_cmd);
899 coll_id = its_cmd_get_collection(its_cmd);
900 target_addr = its_cmd_get_target_addr(its_cmd);
901
902 if (target_addr >= atomic_read(&kvm->online_vcpus))
903 return E_ITS_MAPC_PROCNUM_OOR;
904
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100905 if (!valid) {
Marc Zyngier17a21f52016-07-17 20:01:46 +0100906 vgic_its_free_collection(its, coll_id);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100907 } else {
Marc Zyngier17a21f52016-07-17 20:01:46 +0100908 collection = find_collection(its, coll_id);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100909
Marc Zyngier17a21f52016-07-17 20:01:46 +0100910 if (!collection) {
911 int ret;
912
913 ret = vgic_its_alloc_collection(its, &collection,
914 coll_id);
915 if (ret)
916 return ret;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100917 collection->target_addr = target_addr;
918 } else {
919 collection->target_addr = target_addr;
920 update_affinity_collection(kvm, its, collection);
921 }
922 }
923
924 return 0;
925}
926
927/*
928 * The CLEAR command removes the pending state for a particular LPI.
929 * Must be called with the its_lock mutex held.
930 */
931static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its,
932 u64 *its_cmd)
933{
934 u32 device_id = its_cmd_get_deviceid(its_cmd);
935 u32 event_id = its_cmd_get_id(its_cmd);
Eric Auger9ce91c72017-02-08 06:09:29 +0100936 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100937
938
Eric Auger9ce91c72017-02-08 06:09:29 +0100939 ite = find_ite(its, device_id, event_id);
940 if (!ite)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100941 return E_ITS_CLEAR_UNMAPPED_INTERRUPT;
942
Eric Auger9ce91c72017-02-08 06:09:29 +0100943 ite->irq->pending_latch = false;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100944
945 return 0;
946}
947
948/*
949 * The INV command syncs the configuration bits from the memory table.
950 * Must be called with the its_lock mutex held.
951 */
952static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its,
953 u64 *its_cmd)
954{
955 u32 device_id = its_cmd_get_deviceid(its_cmd);
956 u32 event_id = its_cmd_get_id(its_cmd);
Eric Auger9ce91c72017-02-08 06:09:29 +0100957 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100958
959
Eric Auger9ce91c72017-02-08 06:09:29 +0100960 ite = find_ite(its, device_id, event_id);
961 if (!ite)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100962 return E_ITS_INV_UNMAPPED_INTERRUPT;
963
Eric Auger9ce91c72017-02-08 06:09:29 +0100964 return update_lpi_config(kvm, ite->irq, NULL);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100965}
966
967/*
968 * The INVALL command requests flushing of all IRQ data in this collection.
969 * Find the VCPU mapped to that collection, then iterate over the VM's list
970 * of mapped LPIs and update the configuration for each IRQ which targets
971 * the specified vcpu. The configuration will be read from the in-memory
972 * configuration table.
973 * Must be called with the its_lock mutex held.
974 */
975static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its,
976 u64 *its_cmd)
977{
978 u32 coll_id = its_cmd_get_collection(its_cmd);
979 struct its_collection *collection;
980 struct kvm_vcpu *vcpu;
981 struct vgic_irq *irq;
982 u32 *intids;
983 int irq_count, i;
984
985 collection = find_collection(its, coll_id);
986 if (!its_is_collection_mapped(collection))
987 return E_ITS_INVALL_UNMAPPED_COLLECTION;
988
989 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
990
991 irq_count = vgic_copy_lpi_list(kvm, &intids);
992 if (irq_count < 0)
993 return irq_count;
994
995 for (i = 0; i < irq_count; i++) {
996 irq = vgic_get_irq(kvm, NULL, intids[i]);
997 if (!irq)
998 continue;
999 update_lpi_config(kvm, irq, vcpu);
1000 vgic_put_irq(kvm, irq);
1001 }
1002
1003 kfree(intids);
1004
1005 return 0;
1006}
1007
1008/*
1009 * The MOVALL command moves the pending state of all IRQs targeting one
1010 * redistributor to another. We don't hold the pending state in the VCPUs,
1011 * but in the IRQs instead, so there is really not much to do for us here.
1012 * However the spec says that no IRQ must target the old redistributor
1013 * afterwards, so we make sure that no LPI is using the associated target_vcpu.
1014 * This command affects all LPIs in the system that target that redistributor.
1015 */
1016static int vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its,
1017 u64 *its_cmd)
1018{
1019 struct vgic_dist *dist = &kvm->arch.vgic;
1020 u32 target1_addr = its_cmd_get_target_addr(its_cmd);
1021 u32 target2_addr = its_cmd_mask_field(its_cmd, 3, 16, 32);
1022 struct kvm_vcpu *vcpu1, *vcpu2;
1023 struct vgic_irq *irq;
1024
1025 if (target1_addr >= atomic_read(&kvm->online_vcpus) ||
1026 target2_addr >= atomic_read(&kvm->online_vcpus))
1027 return E_ITS_MOVALL_PROCNUM_OOR;
1028
1029 if (target1_addr == target2_addr)
1030 return 0;
1031
1032 vcpu1 = kvm_get_vcpu(kvm, target1_addr);
1033 vcpu2 = kvm_get_vcpu(kvm, target2_addr);
1034
1035 spin_lock(&dist->lpi_list_lock);
1036
1037 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
1038 spin_lock(&irq->irq_lock);
1039
1040 if (irq->target_vcpu == vcpu1)
1041 irq->target_vcpu = vcpu2;
1042
1043 spin_unlock(&irq->irq_lock);
1044 }
1045
1046 spin_unlock(&dist->lpi_list_lock);
1047
1048 return 0;
1049}
1050
1051/*
Andre Przywara2891a7d2016-07-15 12:43:37 +01001052 * The INT command injects the LPI associated with that DevID/EvID pair.
1053 * Must be called with the its_lock mutex held.
1054 */
1055static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its,
1056 u64 *its_cmd)
1057{
1058 u32 msi_data = its_cmd_get_id(its_cmd);
1059 u64 msi_devid = its_cmd_get_deviceid(its_cmd);
1060
Andre Przywarafd837b02016-08-08 17:29:28 +01001061 return vgic_its_trigger_msi(kvm, its, msi_devid, msi_data);
Andre Przywara2891a7d2016-07-15 12:43:37 +01001062}
1063
1064/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001065 * This function is called with the its_cmd lock held, but the ITS data
1066 * structure lock dropped.
1067 */
Andre Przywara424c3382016-07-15 12:43:32 +01001068static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its,
1069 u64 *its_cmd)
1070{
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001071 int ret = -ENODEV;
1072
1073 mutex_lock(&its->its_lock);
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001074 switch (its_cmd_get_command(its_cmd)) {
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001075 case GITS_CMD_MAPD:
1076 ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd);
1077 break;
1078 case GITS_CMD_MAPC:
1079 ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd);
1080 break;
1081 case GITS_CMD_MAPI:
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001082 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001083 break;
1084 case GITS_CMD_MAPTI:
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001085 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001086 break;
1087 case GITS_CMD_MOVI:
1088 ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd);
1089 break;
1090 case GITS_CMD_DISCARD:
1091 ret = vgic_its_cmd_handle_discard(kvm, its, its_cmd);
1092 break;
1093 case GITS_CMD_CLEAR:
1094 ret = vgic_its_cmd_handle_clear(kvm, its, its_cmd);
1095 break;
1096 case GITS_CMD_MOVALL:
1097 ret = vgic_its_cmd_handle_movall(kvm, its, its_cmd);
1098 break;
Andre Przywara2891a7d2016-07-15 12:43:37 +01001099 case GITS_CMD_INT:
1100 ret = vgic_its_cmd_handle_int(kvm, its, its_cmd);
1101 break;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001102 case GITS_CMD_INV:
1103 ret = vgic_its_cmd_handle_inv(kvm, its, its_cmd);
1104 break;
1105 case GITS_CMD_INVALL:
1106 ret = vgic_its_cmd_handle_invall(kvm, its, its_cmd);
1107 break;
1108 case GITS_CMD_SYNC:
1109 /* we ignore this command: we are in sync all of the time */
1110 ret = 0;
1111 break;
1112 }
1113 mutex_unlock(&its->its_lock);
1114
1115 return ret;
Andre Przywara424c3382016-07-15 12:43:32 +01001116}
1117
1118static u64 vgic_sanitise_its_baser(u64 reg)
1119{
1120 reg = vgic_sanitise_field(reg, GITS_BASER_SHAREABILITY_MASK,
1121 GITS_BASER_SHAREABILITY_SHIFT,
1122 vgic_sanitise_shareability);
1123 reg = vgic_sanitise_field(reg, GITS_BASER_INNER_CACHEABILITY_MASK,
1124 GITS_BASER_INNER_CACHEABILITY_SHIFT,
1125 vgic_sanitise_inner_cacheability);
1126 reg = vgic_sanitise_field(reg, GITS_BASER_OUTER_CACHEABILITY_MASK,
1127 GITS_BASER_OUTER_CACHEABILITY_SHIFT,
1128 vgic_sanitise_outer_cacheability);
1129
1130 /* Bits 15:12 contain bits 51:48 of the PA, which we don't support. */
1131 reg &= ~GENMASK_ULL(15, 12);
1132
1133 /* We support only one (ITS) page size: 64K */
1134 reg = (reg & ~GITS_BASER_PAGE_SIZE_MASK) | GITS_BASER_PAGE_SIZE_64K;
1135
1136 return reg;
1137}
1138
1139static u64 vgic_sanitise_its_cbaser(u64 reg)
1140{
1141 reg = vgic_sanitise_field(reg, GITS_CBASER_SHAREABILITY_MASK,
1142 GITS_CBASER_SHAREABILITY_SHIFT,
1143 vgic_sanitise_shareability);
1144 reg = vgic_sanitise_field(reg, GITS_CBASER_INNER_CACHEABILITY_MASK,
1145 GITS_CBASER_INNER_CACHEABILITY_SHIFT,
1146 vgic_sanitise_inner_cacheability);
1147 reg = vgic_sanitise_field(reg, GITS_CBASER_OUTER_CACHEABILITY_MASK,
1148 GITS_CBASER_OUTER_CACHEABILITY_SHIFT,
1149 vgic_sanitise_outer_cacheability);
1150
1151 /*
1152 * Sanitise the physical address to be 64k aligned.
1153 * Also limit the physical addresses to 48 bits.
1154 */
1155 reg &= ~(GENMASK_ULL(51, 48) | GENMASK_ULL(15, 12));
1156
1157 return reg;
1158}
1159
1160static unsigned long vgic_mmio_read_its_cbaser(struct kvm *kvm,
1161 struct vgic_its *its,
1162 gpa_t addr, unsigned int len)
1163{
1164 return extract_bytes(its->cbaser, addr & 7, len);
1165}
1166
1167static void vgic_mmio_write_its_cbaser(struct kvm *kvm, struct vgic_its *its,
1168 gpa_t addr, unsigned int len,
1169 unsigned long val)
1170{
1171 /* When GITS_CTLR.Enable is 1, this register is RO. */
1172 if (its->enabled)
1173 return;
1174
1175 mutex_lock(&its->cmd_lock);
1176 its->cbaser = update_64bit_reg(its->cbaser, addr & 7, len, val);
1177 its->cbaser = vgic_sanitise_its_cbaser(its->cbaser);
1178 its->creadr = 0;
1179 /*
1180 * CWRITER is architecturally UNKNOWN on reset, but we need to reset
1181 * it to CREADR to make sure we start with an empty command buffer.
1182 */
1183 its->cwriter = its->creadr;
1184 mutex_unlock(&its->cmd_lock);
1185}
1186
1187#define ITS_CMD_BUFFER_SIZE(baser) ((((baser) & 0xff) + 1) << 12)
1188#define ITS_CMD_SIZE 32
1189#define ITS_CMD_OFFSET(reg) ((reg) & GENMASK(19, 5))
1190
Andre Przywaraa5e1e6c2017-02-16 10:41:20 +00001191/* Must be called with the cmd_lock held. */
1192static void vgic_its_process_commands(struct kvm *kvm, struct vgic_its *its)
Andre Przywara424c3382016-07-15 12:43:32 +01001193{
1194 gpa_t cbaser;
1195 u64 cmd_buf[4];
Andre Przywara424c3382016-07-15 12:43:32 +01001196
Andre Przywaraa5e1e6c2017-02-16 10:41:20 +00001197 /* Commands are only processed when the ITS is enabled. */
1198 if (!its->enabled)
Andre Przywara424c3382016-07-15 12:43:32 +01001199 return;
1200
Andre Przywara424c3382016-07-15 12:43:32 +01001201 cbaser = CBASER_ADDRESS(its->cbaser);
1202
1203 while (its->cwriter != its->creadr) {
1204 int ret = kvm_read_guest(kvm, cbaser + its->creadr,
1205 cmd_buf, ITS_CMD_SIZE);
1206 /*
1207 * If kvm_read_guest() fails, this could be due to the guest
1208 * programming a bogus value in CBASER or something else going
1209 * wrong from which we cannot easily recover.
1210 * According to section 6.3.2 in the GICv3 spec we can just
1211 * ignore that command then.
1212 */
1213 if (!ret)
1214 vgic_its_handle_command(kvm, its, cmd_buf);
1215
1216 its->creadr += ITS_CMD_SIZE;
1217 if (its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser))
1218 its->creadr = 0;
1219 }
Andre Przywaraa5e1e6c2017-02-16 10:41:20 +00001220}
1221
1222/*
1223 * By writing to CWRITER the guest announces new commands to be processed.
1224 * To avoid any races in the first place, we take the its_cmd lock, which
1225 * protects our ring buffer variables, so that there is only one user
1226 * per ITS handling commands at a given time.
1227 */
1228static void vgic_mmio_write_its_cwriter(struct kvm *kvm, struct vgic_its *its,
1229 gpa_t addr, unsigned int len,
1230 unsigned long val)
1231{
1232 u64 reg;
1233
1234 if (!its)
1235 return;
1236
1237 mutex_lock(&its->cmd_lock);
1238
1239 reg = update_64bit_reg(its->cwriter, addr & 7, len, val);
1240 reg = ITS_CMD_OFFSET(reg);
1241 if (reg >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
1242 mutex_unlock(&its->cmd_lock);
1243 return;
1244 }
1245 its->cwriter = reg;
1246
1247 vgic_its_process_commands(kvm, its);
Andre Przywara424c3382016-07-15 12:43:32 +01001248
1249 mutex_unlock(&its->cmd_lock);
1250}
1251
1252static unsigned long vgic_mmio_read_its_cwriter(struct kvm *kvm,
1253 struct vgic_its *its,
1254 gpa_t addr, unsigned int len)
1255{
1256 return extract_bytes(its->cwriter, addr & 0x7, len);
1257}
1258
1259static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,
1260 struct vgic_its *its,
1261 gpa_t addr, unsigned int len)
1262{
1263 return extract_bytes(its->creadr, addr & 0x7, len);
1264}
1265
Eric Auger0979bfa2017-01-04 11:58:41 +01001266static int vgic_mmio_uaccess_write_its_creadr(struct kvm *kvm,
1267 struct vgic_its *its,
1268 gpa_t addr, unsigned int len,
1269 unsigned long val)
1270{
1271 u32 cmd_offset;
1272 int ret = 0;
1273
1274 mutex_lock(&its->cmd_lock);
1275
1276 if (its->enabled) {
1277 ret = -EBUSY;
1278 goto out;
1279 }
1280
1281 cmd_offset = ITS_CMD_OFFSET(val);
1282 if (cmd_offset >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
1283 ret = -EINVAL;
1284 goto out;
1285 }
1286
1287 its->creadr = cmd_offset;
1288out:
1289 mutex_unlock(&its->cmd_lock);
1290 return ret;
1291}
1292
Andre Przywara424c3382016-07-15 12:43:32 +01001293#define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7)
1294static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm,
1295 struct vgic_its *its,
1296 gpa_t addr, unsigned int len)
1297{
1298 u64 reg;
1299
1300 switch (BASER_INDEX(addr)) {
1301 case 0:
1302 reg = its->baser_device_table;
1303 break;
1304 case 1:
1305 reg = its->baser_coll_table;
1306 break;
1307 default:
1308 reg = 0;
1309 break;
1310 }
1311
1312 return extract_bytes(reg, addr & 7, len);
1313}
1314
1315#define GITS_BASER_RO_MASK (GENMASK_ULL(52, 48) | GENMASK_ULL(58, 56))
1316static void vgic_mmio_write_its_baser(struct kvm *kvm,
1317 struct vgic_its *its,
1318 gpa_t addr, unsigned int len,
1319 unsigned long val)
1320{
Eric Auger71afe472017-04-13 09:06:20 +02001321 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
Andre Przywara424c3382016-07-15 12:43:32 +01001322 u64 entry_size, device_type;
1323 u64 reg, *regptr, clearbits = 0;
1324
1325 /* When GITS_CTLR.Enable is 1, we ignore write accesses. */
1326 if (its->enabled)
1327 return;
1328
1329 switch (BASER_INDEX(addr)) {
1330 case 0:
1331 regptr = &its->baser_device_table;
Eric Auger71afe472017-04-13 09:06:20 +02001332 entry_size = abi->dte_esz;
Andre Przywara424c3382016-07-15 12:43:32 +01001333 device_type = GITS_BASER_TYPE_DEVICE;
1334 break;
1335 case 1:
1336 regptr = &its->baser_coll_table;
Eric Auger71afe472017-04-13 09:06:20 +02001337 entry_size = abi->cte_esz;
Andre Przywara424c3382016-07-15 12:43:32 +01001338 device_type = GITS_BASER_TYPE_COLLECTION;
1339 clearbits = GITS_BASER_INDIRECT;
1340 break;
1341 default:
1342 return;
1343 }
1344
1345 reg = update_64bit_reg(*regptr, addr & 7, len, val);
1346 reg &= ~GITS_BASER_RO_MASK;
1347 reg &= ~clearbits;
1348
1349 reg |= (entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
1350 reg |= device_type << GITS_BASER_TYPE_SHIFT;
1351 reg = vgic_sanitise_its_baser(reg);
1352
1353 *regptr = reg;
1354}
1355
Andre Przywaraa5e1e6c2017-02-16 10:41:20 +00001356static unsigned long vgic_mmio_read_its_ctlr(struct kvm *vcpu,
1357 struct vgic_its *its,
1358 gpa_t addr, unsigned int len)
1359{
1360 u32 reg = 0;
1361
1362 mutex_lock(&its->cmd_lock);
1363 if (its->creadr == its->cwriter)
1364 reg |= GITS_CTLR_QUIESCENT;
1365 if (its->enabled)
1366 reg |= GITS_CTLR_ENABLE;
1367 mutex_unlock(&its->cmd_lock);
1368
1369 return reg;
1370}
1371
1372static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its,
1373 gpa_t addr, unsigned int len,
1374 unsigned long val)
1375{
1376 mutex_lock(&its->cmd_lock);
1377
1378 its->enabled = !!(val & GITS_CTLR_ENABLE);
1379
1380 /*
1381 * Try to process any pending commands. This function bails out early
1382 * if the ITS is disabled or no commands have been queued.
1383 */
1384 vgic_its_process_commands(kvm, its);
1385
1386 mutex_unlock(&its->cmd_lock);
1387}
1388
Andre Przywara59c5ab42016-07-15 12:43:30 +01001389#define REGISTER_ITS_DESC(off, rd, wr, length, acc) \
1390{ \
1391 .reg_offset = off, \
1392 .len = length, \
1393 .access_flags = acc, \
1394 .its_read = rd, \
1395 .its_write = wr, \
1396}
1397
Eric Auger0979bfa2017-01-04 11:58:41 +01001398#define REGISTER_ITS_DESC_UACCESS(off, rd, wr, uwr, length, acc)\
1399{ \
1400 .reg_offset = off, \
1401 .len = length, \
1402 .access_flags = acc, \
1403 .its_read = rd, \
1404 .its_write = wr, \
1405 .uaccess_its_write = uwr, \
1406}
1407
Andre Przywara59c5ab42016-07-15 12:43:30 +01001408static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its,
1409 gpa_t addr, unsigned int len, unsigned long val)
1410{
1411 /* Ignore */
1412}
1413
1414static struct vgic_register_region its_registers[] = {
1415 REGISTER_ITS_DESC(GITS_CTLR,
Andre Przywara424c3382016-07-15 12:43:32 +01001416 vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001417 VGIC_ACCESS_32bit),
1418 REGISTER_ITS_DESC(GITS_IIDR,
Andre Przywara424c3382016-07-15 12:43:32 +01001419 vgic_mmio_read_its_iidr, its_mmio_write_wi, 4,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001420 VGIC_ACCESS_32bit),
1421 REGISTER_ITS_DESC(GITS_TYPER,
Andre Przywara424c3382016-07-15 12:43:32 +01001422 vgic_mmio_read_its_typer, its_mmio_write_wi, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001423 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1424 REGISTER_ITS_DESC(GITS_CBASER,
Andre Przywara424c3382016-07-15 12:43:32 +01001425 vgic_mmio_read_its_cbaser, vgic_mmio_write_its_cbaser, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001426 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1427 REGISTER_ITS_DESC(GITS_CWRITER,
Andre Przywara424c3382016-07-15 12:43:32 +01001428 vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001429 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
Eric Auger0979bfa2017-01-04 11:58:41 +01001430 REGISTER_ITS_DESC_UACCESS(GITS_CREADR,
1431 vgic_mmio_read_its_creadr, its_mmio_write_wi,
1432 vgic_mmio_uaccess_write_its_creadr, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001433 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1434 REGISTER_ITS_DESC(GITS_BASER,
Andre Przywara424c3382016-07-15 12:43:32 +01001435 vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001436 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1437 REGISTER_ITS_DESC(GITS_IDREGS_BASE,
Andre Przywara424c3382016-07-15 12:43:32 +01001438 vgic_mmio_read_its_idregs, its_mmio_write_wi, 0x30,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001439 VGIC_ACCESS_32bit),
1440};
1441
Andre Przywara33d3bc92016-07-15 12:43:34 +01001442/* This is called on setting the LPI enable bit in the redistributor. */
1443void vgic_enable_lpis(struct kvm_vcpu *vcpu)
1444{
1445 if (!(vcpu->arch.vgic_cpu.pendbaser & GICR_PENDBASER_PTZ))
1446 its_sync_lpi_pending_table(vcpu);
1447}
1448
Andre Przywarac7735762016-08-08 16:45:43 +01001449static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its)
Andre Przywara59c5ab42016-07-15 12:43:30 +01001450{
1451 struct vgic_io_device *iodev = &its->iodev;
1452 int ret;
1453
Andre Przywarac7735762016-08-08 16:45:43 +01001454 if (!its->initialized)
1455 return -EBUSY;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001456
Andre Przywara59c5ab42016-07-15 12:43:30 +01001457 if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base))
1458 return -ENXIO;
1459
1460 iodev->regions = its_registers;
1461 iodev->nr_regions = ARRAY_SIZE(its_registers);
1462 kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops);
1463
1464 iodev->base_addr = its->vgic_its_base;
1465 iodev->iodev_type = IODEV_ITS;
1466 iodev->its = its;
1467 mutex_lock(&kvm->slots_lock);
1468 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr,
1469 KVM_VGIC_V3_ITS_SIZE, &iodev->dev);
1470 mutex_unlock(&kvm->slots_lock);
1471
1472 return ret;
1473}
Andre Przywara1085fdc2016-07-15 12:43:31 +01001474
Andre Przywara424c3382016-07-15 12:43:32 +01001475#define INITIAL_BASER_VALUE \
1476 (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb) | \
1477 GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner) | \
1478 GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable) | \
Andre Przywara424c3382016-07-15 12:43:32 +01001479 GITS_BASER_PAGE_SIZE_64K)
1480
1481#define INITIAL_PROPBASER_VALUE \
1482 (GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWb) | \
1483 GIC_BASER_CACHEABILITY(GICR_PROPBASER, OUTER, SameAsInner) | \
1484 GIC_BASER_SHAREABILITY(GICR_PROPBASER, InnerShareable))
1485
Andre Przywara1085fdc2016-07-15 12:43:31 +01001486static int vgic_its_create(struct kvm_device *dev, u32 type)
1487{
1488 struct vgic_its *its;
1489
1490 if (type != KVM_DEV_TYPE_ARM_VGIC_ITS)
1491 return -ENODEV;
1492
1493 its = kzalloc(sizeof(struct vgic_its), GFP_KERNEL);
1494 if (!its)
1495 return -ENOMEM;
1496
Andre Przywara424c3382016-07-15 12:43:32 +01001497 mutex_init(&its->its_lock);
1498 mutex_init(&its->cmd_lock);
1499
Andre Przywara1085fdc2016-07-15 12:43:31 +01001500 its->vgic_its_base = VGIC_ADDR_UNDEF;
1501
Andre Przywara424c3382016-07-15 12:43:32 +01001502 INIT_LIST_HEAD(&its->device_list);
1503 INIT_LIST_HEAD(&its->collection_list);
1504
Andre Przywara1085fdc2016-07-15 12:43:31 +01001505 dev->kvm->arch.vgic.has_its = true;
1506 its->initialized = false;
1507 its->enabled = false;
Marc Zyngierbb717642016-07-17 21:35:07 +01001508 its->dev = dev;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001509
Andre Przywara424c3382016-07-15 12:43:32 +01001510 its->baser_device_table = INITIAL_BASER_VALUE |
1511 ((u64)GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT);
1512 its->baser_coll_table = INITIAL_BASER_VALUE |
1513 ((u64)GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT);
1514 dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE;
1515
Andre Przywara1085fdc2016-07-15 12:43:31 +01001516 dev->private = its;
1517
Eric Auger71afe472017-04-13 09:06:20 +02001518 return vgic_its_set_abi(its, NR_ITS_ABIS - 1);
Andre Przywara1085fdc2016-07-15 12:43:31 +01001519}
1520
1521static void vgic_its_destroy(struct kvm_device *kvm_dev)
1522{
Andre Przywara424c3382016-07-15 12:43:32 +01001523 struct kvm *kvm = kvm_dev->kvm;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001524 struct vgic_its *its = kvm_dev->private;
Andre Przywara424c3382016-07-15 12:43:32 +01001525 struct its_device *dev;
Eric Auger9ce91c72017-02-08 06:09:29 +01001526 struct its_ite *ite;
Andre Przywara424c3382016-07-15 12:43:32 +01001527 struct list_head *dev_cur, *dev_temp;
1528 struct list_head *cur, *temp;
1529
1530 /*
1531 * We may end up here without the lists ever having been initialized.
1532 * Check this and bail out early to avoid dereferencing a NULL pointer.
1533 */
1534 if (!its->device_list.next)
1535 return;
1536
1537 mutex_lock(&its->its_lock);
1538 list_for_each_safe(dev_cur, dev_temp, &its->device_list) {
1539 dev = container_of(dev_cur, struct its_device, dev_list);
1540 list_for_each_safe(cur, temp, &dev->itt_head) {
Eric Auger9ce91c72017-02-08 06:09:29 +01001541 ite = (container_of(cur, struct its_ite, ite_list));
1542 its_free_ite(kvm, ite);
Andre Przywara424c3382016-07-15 12:43:32 +01001543 }
1544 list_del(dev_cur);
1545 kfree(dev);
1546 }
1547
1548 list_for_each_safe(cur, temp, &its->collection_list) {
1549 list_del(cur);
1550 kfree(container_of(cur, struct its_collection, coll_list));
1551 }
1552 mutex_unlock(&its->its_lock);
Andre Przywara1085fdc2016-07-15 12:43:31 +01001553
1554 kfree(its);
1555}
1556
Eric Auger876ae232016-12-20 01:36:35 -05001557int vgic_its_has_attr_regs(struct kvm_device *dev,
1558 struct kvm_device_attr *attr)
1559{
Eric Auger8331c232016-12-20 09:33:13 +01001560 const struct vgic_register_region *region;
1561 gpa_t offset = attr->attr;
1562 int align;
1563
1564 align = (offset < GITS_TYPER) || (offset >= GITS_PIDR4) ? 0x3 : 0x7;
1565
1566 if (offset & align)
1567 return -EINVAL;
1568
1569 region = vgic_find_mmio_region(its_registers,
1570 ARRAY_SIZE(its_registers),
1571 offset);
1572 if (!region)
1573 return -ENXIO;
1574
1575 return 0;
Eric Auger876ae232016-12-20 01:36:35 -05001576}
1577
1578int vgic_its_attr_regs_access(struct kvm_device *dev,
1579 struct kvm_device_attr *attr,
1580 u64 *reg, bool is_write)
1581{
Eric Auger8331c232016-12-20 09:33:13 +01001582 const struct vgic_register_region *region;
1583 struct vgic_its *its;
1584 gpa_t addr, offset;
1585 unsigned int len;
1586 int align, ret = 0;
1587
1588 its = dev->private;
1589 offset = attr->attr;
1590
1591 /*
1592 * Although the spec supports upper/lower 32-bit accesses to
1593 * 64-bit ITS registers, the userspace ABI requires 64-bit
1594 * accesses to all 64-bit wide registers. We therefore only
1595 * support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID
1596 * registers
1597 */
1598 if ((offset < GITS_TYPER) || (offset >= GITS_PIDR4))
1599 align = 0x3;
1600 else
1601 align = 0x7;
1602
1603 if (offset & align)
1604 return -EINVAL;
1605
1606 mutex_lock(&dev->kvm->lock);
1607
1608 if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) {
1609 ret = -ENXIO;
1610 goto out;
1611 }
1612
1613 region = vgic_find_mmio_region(its_registers,
1614 ARRAY_SIZE(its_registers),
1615 offset);
1616 if (!region) {
1617 ret = -ENXIO;
1618 goto out;
1619 }
1620
1621 if (!lock_all_vcpus(dev->kvm)) {
1622 ret = -EBUSY;
1623 goto out;
1624 }
1625
1626 addr = its->vgic_its_base + offset;
1627
1628 len = region->access_flags & VGIC_ACCESS_64bit ? 8 : 4;
1629
1630 if (is_write) {
1631 if (region->uaccess_its_write)
1632 ret = region->uaccess_its_write(dev->kvm, its, addr,
1633 len, *reg);
1634 else
1635 region->its_write(dev->kvm, its, addr, len, *reg);
1636 } else {
1637 *reg = region->its_read(dev->kvm, its, addr, len);
1638 }
1639 unlock_all_vcpus(dev->kvm);
1640out:
1641 mutex_unlock(&dev->kvm->lock);
1642 return ret;
Eric Auger876ae232016-12-20 01:36:35 -05001643}
1644
Eric Auger71afe472017-04-13 09:06:20 +02001645/**
1646 * vgic_its_save_tables_v0 - Save the ITS tables into guest ARM
1647 * according to v0 ABI
1648 */
1649static int vgic_its_save_tables_v0(struct vgic_its *its)
1650{
1651 return -ENXIO;
1652}
1653
1654/**
1655 * vgic_its_restore_tables_v0 - Restore the ITS tables from guest RAM
1656 * to internal data structs according to V0 ABI
1657 *
1658 */
1659static int vgic_its_restore_tables_v0(struct vgic_its *its)
1660{
1661 return -ENXIO;
1662}
1663
1664static int vgic_its_commit_v0(struct vgic_its *its)
1665{
1666 const struct vgic_its_abi *abi;
1667
1668 abi = vgic_its_get_abi(its);
1669 its->baser_coll_table &= ~GITS_BASER_ENTRY_SIZE_MASK;
1670 its->baser_device_table &= ~GITS_BASER_ENTRY_SIZE_MASK;
1671
1672 its->baser_coll_table |= (GIC_ENCODE_SZ(abi->cte_esz, 5)
1673 << GITS_BASER_ENTRY_SIZE_SHIFT);
1674
1675 its->baser_device_table |= (GIC_ENCODE_SZ(abi->dte_esz, 5)
1676 << GITS_BASER_ENTRY_SIZE_SHIFT);
1677 return 0;
1678}
1679
Andre Przywara1085fdc2016-07-15 12:43:31 +01001680static int vgic_its_has_attr(struct kvm_device *dev,
1681 struct kvm_device_attr *attr)
1682{
1683 switch (attr->group) {
1684 case KVM_DEV_ARM_VGIC_GRP_ADDR:
1685 switch (attr->attr) {
1686 case KVM_VGIC_ITS_ADDR_TYPE:
1687 return 0;
1688 }
1689 break;
1690 case KVM_DEV_ARM_VGIC_GRP_CTRL:
1691 switch (attr->attr) {
1692 case KVM_DEV_ARM_VGIC_CTRL_INIT:
1693 return 0;
1694 }
1695 break;
Eric Auger876ae232016-12-20 01:36:35 -05001696 case KVM_DEV_ARM_VGIC_GRP_ITS_REGS:
1697 return vgic_its_has_attr_regs(dev, attr);
Andre Przywara1085fdc2016-07-15 12:43:31 +01001698 }
1699 return -ENXIO;
1700}
1701
1702static int vgic_its_set_attr(struct kvm_device *dev,
1703 struct kvm_device_attr *attr)
1704{
1705 struct vgic_its *its = dev->private;
1706 int ret;
1707
1708 switch (attr->group) {
1709 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
1710 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1711 unsigned long type = (unsigned long)attr->attr;
1712 u64 addr;
1713
1714 if (type != KVM_VGIC_ITS_ADDR_TYPE)
1715 return -ENODEV;
1716
Andre Przywara1085fdc2016-07-15 12:43:31 +01001717 if (copy_from_user(&addr, uaddr, sizeof(addr)))
1718 return -EFAULT;
1719
1720 ret = vgic_check_ioaddr(dev->kvm, &its->vgic_its_base,
1721 addr, SZ_64K);
1722 if (ret)
1723 return ret;
1724
1725 its->vgic_its_base = addr;
1726
1727 return 0;
1728 }
1729 case KVM_DEV_ARM_VGIC_GRP_CTRL:
1730 switch (attr->attr) {
1731 case KVM_DEV_ARM_VGIC_CTRL_INIT:
Andre Przywarac7735762016-08-08 16:45:43 +01001732 its->initialized = true;
1733
1734 return 0;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001735 }
1736 break;
Eric Auger876ae232016-12-20 01:36:35 -05001737 case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: {
1738 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1739 u64 reg;
1740
1741 if (get_user(reg, uaddr))
1742 return -EFAULT;
1743
1744 return vgic_its_attr_regs_access(dev, attr, &reg, true);
1745 }
Andre Przywara1085fdc2016-07-15 12:43:31 +01001746 }
1747 return -ENXIO;
1748}
1749
1750static int vgic_its_get_attr(struct kvm_device *dev,
1751 struct kvm_device_attr *attr)
1752{
1753 switch (attr->group) {
1754 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
1755 struct vgic_its *its = dev->private;
1756 u64 addr = its->vgic_its_base;
1757 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1758 unsigned long type = (unsigned long)attr->attr;
1759
1760 if (type != KVM_VGIC_ITS_ADDR_TYPE)
1761 return -ENODEV;
1762
1763 if (copy_to_user(uaddr, &addr, sizeof(addr)))
1764 return -EFAULT;
1765 break;
Eric Auger876ae232016-12-20 01:36:35 -05001766 }
1767 case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: {
1768 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1769 u64 reg;
1770 int ret;
1771
1772 ret = vgic_its_attr_regs_access(dev, attr, &reg, false);
1773 if (ret)
1774 return ret;
1775 return put_user(reg, uaddr);
1776 }
Andre Przywara1085fdc2016-07-15 12:43:31 +01001777 default:
1778 return -ENXIO;
1779 }
Andre Przywara1085fdc2016-07-15 12:43:31 +01001780
1781 return 0;
1782}
1783
1784static struct kvm_device_ops kvm_arm_vgic_its_ops = {
1785 .name = "kvm-arm-vgic-its",
1786 .create = vgic_its_create,
1787 .destroy = vgic_its_destroy,
1788 .set_attr = vgic_its_set_attr,
1789 .get_attr = vgic_its_get_attr,
1790 .has_attr = vgic_its_has_attr,
1791};
1792
1793int kvm_vgic_register_its_device(void)
1794{
1795 return kvm_register_device_ops(&kvm_arm_vgic_its_ops,
1796 KVM_DEV_TYPE_ARM_VGIC_ITS);
1797}
Andre Przywarac7735762016-08-08 16:45:43 +01001798
1799/*
1800 * Registers all ITSes with the kvm_io_bus framework.
1801 * To follow the existing VGIC initialization sequence, this has to be
1802 * done as late as possible, just before the first VCPU runs.
1803 */
1804int vgic_register_its_iodevs(struct kvm *kvm)
1805{
1806 struct kvm_device *dev;
1807 int ret = 0;
1808
1809 list_for_each_entry(dev, &kvm->devices, vm_node) {
1810 if (dev->ops != &kvm_arm_vgic_its_ops)
1811 continue;
1812
1813 ret = vgic_register_its_iodev(kvm, dev->private);
1814 if (ret)
1815 return ret;
1816 /*
1817 * We don't need to care about tearing down previously
1818 * registered ITSes, as the kvm_io_bus framework removes
1819 * them for us if the VM gets destroyed.
1820 */
1821 }
1822
1823 return ret;
1824}