blob: aa6b68db80b47c6089ef27c64dbf1e2654acc458 [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>
Eric Auger57a9a112017-01-09 16:27:07 +010026#include <linux/list_sort.h>
Andre Przywara59c5ab42016-07-15 12:43:30 +010027
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
Eric Auger71afe472017-04-13 09:06:20 +020037static int vgic_its_save_tables_v0(struct vgic_its *its);
38static int vgic_its_restore_tables_v0(struct vgic_its *its);
39static int vgic_its_commit_v0(struct vgic_its *its);
Eric Auger06bd5352017-05-04 11:36:32 +020040static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
41 struct kvm_vcpu *filter_vcpu);
Eric Auger71afe472017-04-13 09:06:20 +020042
Andre Przywaradf9f58f2016-07-15 12:43:36 +010043/*
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 */
Eric Auger06bd5352017-05-04 11:36:32 +020050static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid,
51 struct kvm_vcpu *vcpu)
Andre Przywaradf9f58f2016-07-15 12:43:36 +010052{
53 struct vgic_dist *dist = &kvm->arch.vgic;
54 struct vgic_irq *irq = vgic_get_irq(kvm, NULL, intid), *oldirq;
Eric Auger06bd5352017-05-04 11:36:32 +020055 int ret;
Andre Przywaradf9f58f2016-07-15 12:43:36 +010056
57 /* In this case there is no put, since we keep the reference. */
58 if (irq)
59 return irq;
60
61 irq = kzalloc(sizeof(struct vgic_irq), GFP_KERNEL);
62 if (!irq)
Christoffer Dall99e5e882016-08-01 20:25:33 +020063 return ERR_PTR(-ENOMEM);
Andre Przywaradf9f58f2016-07-15 12:43:36 +010064
65 INIT_LIST_HEAD(&irq->lpi_list);
66 INIT_LIST_HEAD(&irq->ap_list);
67 spin_lock_init(&irq->irq_lock);
68
69 irq->config = VGIC_CONFIG_EDGE;
70 kref_init(&irq->refcount);
71 irq->intid = intid;
Eric Auger06bd5352017-05-04 11:36:32 +020072 irq->target_vcpu = vcpu;
Andre Przywaradf9f58f2016-07-15 12:43:36 +010073
74 spin_lock(&dist->lpi_list_lock);
75
76 /*
77 * There could be a race with another vgic_add_lpi(), so we need to
78 * check that we don't add a second list entry with the same LPI.
79 */
80 list_for_each_entry(oldirq, &dist->lpi_list_head, lpi_list) {
81 if (oldirq->intid != intid)
82 continue;
83
84 /* Someone was faster with adding this LPI, lets use that. */
85 kfree(irq);
86 irq = oldirq;
87
88 /*
89 * This increases the refcount, the caller is expected to
90 * call vgic_put_irq() on the returned pointer once it's
91 * finished with the IRQ.
92 */
Marc Zyngierd97594e2016-07-17 11:27:23 +010093 vgic_get_irq_kref(irq);
Andre Przywaradf9f58f2016-07-15 12:43:36 +010094
95 goto out_unlock;
96 }
97
98 list_add_tail(&irq->lpi_list, &dist->lpi_list_head);
99 dist->lpi_list_count++;
100
101out_unlock:
102 spin_unlock(&dist->lpi_list_lock);
103
Eric Auger06bd5352017-05-04 11:36:32 +0200104 /*
105 * We "cache" the configuration table entries in our struct vgic_irq's.
106 * However we only have those structs for mapped IRQs, so we read in
107 * the respective config data from memory here upon mapping the LPI.
108 */
109 ret = update_lpi_config(kvm, irq, NULL);
110 if (ret)
111 return ERR_PTR(ret);
112
113 ret = vgic_v3_lpi_sync_pending_status(kvm, irq);
114 if (ret)
115 return ERR_PTR(ret);
116
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100117 return irq;
118}
119
Andre Przywara424c3382016-07-15 12:43:32 +0100120struct its_device {
121 struct list_head dev_list;
122
123 /* the head for the list of ITTEs */
124 struct list_head itt_head;
Eric Auger0d44cdb2016-12-22 18:14:14 +0100125 u32 num_eventid_bits;
Eric Auger7333cef2017-02-02 13:45:45 +0100126 gpa_t itt_addr;
Andre Przywara424c3382016-07-15 12:43:32 +0100127 u32 device_id;
128};
129
130#define COLLECTION_NOT_MAPPED ((u32)~0)
131
132struct its_collection {
133 struct list_head coll_list;
134
135 u32 collection_id;
136 u32 target_addr;
137};
138
139#define its_is_collection_mapped(coll) ((coll) && \
140 ((coll)->target_addr != COLLECTION_NOT_MAPPED))
141
Eric Auger9ce91c72017-02-08 06:09:29 +0100142struct its_ite {
143 struct list_head ite_list;
Andre Przywara424c3382016-07-15 12:43:32 +0100144
Andre Przywara38024112016-07-15 12:43:33 +0100145 struct vgic_irq *irq;
Andre Przywara424c3382016-07-15 12:43:32 +0100146 struct its_collection *collection;
147 u32 lpi;
148 u32 event_id;
149};
150
Eric Auger71afe472017-04-13 09:06:20 +0200151/**
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 */
162struct 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
171static 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
181inline 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
186int 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
Andre Przywara424c3382016-07-15 12:43:32 +0100195/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100196 * Find and returns a device in the device table for an ITS.
197 * Must be called with the its_lock mutex held.
198 */
199static 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 */
Eric Auger9ce91c72017-02-08 06:09:29 +0100215static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100216 u32 event_id)
217{
218 struct its_device *device;
Eric Auger9ce91c72017-02-08 06:09:29 +0100219 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100220
221 device = find_its_device(its, device_id);
222 if (device == NULL)
223 return NULL;
224
Eric Auger9ce91c72017-02-08 06:09:29 +0100225 list_for_each_entry(ite, &device->itt_head, ite_list)
226 if (ite->event_id == event_id)
227 return ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100228
229 return NULL;
230}
231
232/* To be used as an iterator this macro misses the enclosing parentheses */
Eric Auger9ce91c72017-02-08 06:09:29 +0100233#define for_each_lpi_its(dev, ite, its) \
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100234 list_for_each_entry(dev, &(its)->device_list, dev_list) \
Eric Auger9ce91c72017-02-08 06:09:29 +0100235 list_for_each_entry(ite, &(dev)->itt_head, ite_list)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100236
237/*
Andre Przywara424c3382016-07-15 12:43:32 +0100238 * We only implement 48 bits of PA at the moment, although the ITS
239 * supports more. Let's be restrictive here.
240 */
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100241#define BASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16))
Andre Przywara424c3382016-07-15 12:43:32 +0100242#define CBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12))
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100243
244#define GIC_LPI_OFFSET 8192
245
Eric Auger0d44cdb2016-12-22 18:14:14 +0100246#define VITS_TYPER_IDBITS 16
Eric Auger07a3e9a2017-02-02 14:37:33 +0100247#define VITS_TYPER_DEVBITS 16
Eric Auger920a7a82017-02-08 05:20:04 +0100248#define VITS_DTE_MAX_DEVID_OFFSET (BIT(14) - 1)
249#define VITS_ITE_MAX_EVENTID_OFFSET (BIT(16) - 1)
Eric Auger0d44cdb2016-12-22 18:14:14 +0100250
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100251/*
252 * Finds and returns a collection in the ITS collection table.
253 * Must be called with the its_lock mutex held.
254 */
255static 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
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100267#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 */
276static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
277 struct kvm_vcpu *filter_vcpu)
278{
Eric Auger44de9d62017-05-04 11:19:52 +0200279 u64 propbase = GICR_PROPBASER_ADDRESS(kvm->arch.vgic.propbaser);
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100280 u8 prop;
281 int ret;
282
283 ret = kvm_read_guest(kvm, propbase + irq->intid - GIC_LPI_OFFSET,
284 &prop, 1);
285
286 if (ret)
287 return ret;
288
289 spin_lock(&irq->irq_lock);
290
291 if (!filter_vcpu || filter_vcpu == irq->target_vcpu) {
292 irq->priority = LPI_PROP_PRIORITY(prop);
293 irq->enabled = LPI_PROP_ENABLE_BIT(prop);
294
295 vgic_queue_irq_unlock(kvm, irq);
296 } else {
297 spin_unlock(&irq->irq_lock);
298 }
299
300 return 0;
301}
Andre Przywara33d3bc92016-07-15 12:43:34 +0100302
303/*
Eric Augerccb1d792017-04-12 14:13:27 +0200304 * Create a snapshot of the current LPIs targeting @vcpu, so that we can
305 * enumerate those LPIs without holding any lock.
306 * Returns their number and puts the kmalloc'ed array into intid_ptr.
Andre Przywara33d3bc92016-07-15 12:43:34 +0100307 */
Eric Augerccb1d792017-04-12 14:13:27 +0200308static int vgic_copy_lpi_list(struct kvm_vcpu *vcpu, u32 **intid_ptr)
Andre Przywara33d3bc92016-07-15 12:43:34 +0100309{
Eric Augerccb1d792017-04-12 14:13:27 +0200310 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Andre Przywara33d3bc92016-07-15 12:43:34 +0100311 struct vgic_irq *irq;
312 u32 *intids;
313 int irq_count = dist->lpi_list_count, i = 0;
314
315 /*
316 * We use the current value of the list length, which may change
317 * after the kmalloc. We don't care, because the guest shouldn't
318 * change anything while the command handling is still running,
319 * and in the worst case we would miss a new IRQ, which one wouldn't
320 * expect to be covered by this command anyway.
321 */
322 intids = kmalloc_array(irq_count, sizeof(intids[0]), GFP_KERNEL);
323 if (!intids)
324 return -ENOMEM;
325
326 spin_lock(&dist->lpi_list_lock);
327 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
328 /* We don't need to "get" the IRQ, as we hold the list lock. */
Eric Augerccb1d792017-04-12 14:13:27 +0200329 if (irq->target_vcpu != vcpu)
330 continue;
331 intids[i++] = irq->intid;
Andre Przywara33d3bc92016-07-15 12:43:34 +0100332 }
333 spin_unlock(&dist->lpi_list_lock);
334
335 *intid_ptr = intids;
Eric Augerccb1d792017-04-12 14:13:27 +0200336 return i;
Andre Przywara33d3bc92016-07-15 12:43:34 +0100337}
338
339/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100340 * Promotes the ITS view of affinity of an ITTE (which redistributor this LPI
341 * is targeting) to the VGIC's view, which deals with target VCPUs.
342 * Needs to be called whenever either the collection for a LPIs has
343 * changed or the collection itself got retargeted.
344 */
Eric Auger9ce91c72017-02-08 06:09:29 +0100345static void update_affinity_ite(struct kvm *kvm, struct its_ite *ite)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100346{
347 struct kvm_vcpu *vcpu;
348
Eric Auger9ce91c72017-02-08 06:09:29 +0100349 if (!its_is_collection_mapped(ite->collection))
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100350 return;
351
Eric Auger9ce91c72017-02-08 06:09:29 +0100352 vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100353
Eric Auger9ce91c72017-02-08 06:09:29 +0100354 spin_lock(&ite->irq->irq_lock);
355 ite->irq->target_vcpu = vcpu;
356 spin_unlock(&ite->irq->irq_lock);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100357}
358
359/*
360 * Updates the target VCPU for every LPI targeting this collection.
361 * Must be called with the its_lock mutex held.
362 */
363static void update_affinity_collection(struct kvm *kvm, struct vgic_its *its,
364 struct its_collection *coll)
365{
366 struct its_device *device;
Eric Auger9ce91c72017-02-08 06:09:29 +0100367 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100368
Eric Auger9ce91c72017-02-08 06:09:29 +0100369 for_each_lpi_its(device, ite, its) {
370 if (!ite->collection || coll != ite->collection)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100371 continue;
372
Eric Auger9ce91c72017-02-08 06:09:29 +0100373 update_affinity_ite(kvm, ite);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100374 }
375}
376
377static u32 max_lpis_propbaser(u64 propbaser)
378{
379 int nr_idbits = (propbaser & 0x1f) + 1;
380
381 return 1U << min(nr_idbits, INTERRUPT_ID_BITS_ITS);
382}
383
384/*
Eric Augerccb1d792017-04-12 14:13:27 +0200385 * Sync the pending table pending bit of LPIs targeting @vcpu
Andre Przywara33d3bc92016-07-15 12:43:34 +0100386 * with our own data structures. This relies on the LPI being
387 * mapped before.
388 */
389static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
390{
Eric Auger44de9d62017-05-04 11:19:52 +0200391 gpa_t pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
Andre Przywara33d3bc92016-07-15 12:43:34 +0100392 struct vgic_irq *irq;
393 int last_byte_offset = -1;
394 int ret = 0;
395 u32 *intids;
396 int nr_irqs, i;
397
Eric Augerccb1d792017-04-12 14:13:27 +0200398 nr_irqs = vgic_copy_lpi_list(vcpu, &intids);
Andre Przywara33d3bc92016-07-15 12:43:34 +0100399 if (nr_irqs < 0)
400 return nr_irqs;
401
402 for (i = 0; i < nr_irqs; i++) {
403 int byte_offset, bit_nr;
404 u8 pendmask;
405
406 byte_offset = intids[i] / BITS_PER_BYTE;
407 bit_nr = intids[i] % BITS_PER_BYTE;
408
409 /*
410 * For contiguously allocated LPIs chances are we just read
411 * this very same byte in the last iteration. Reuse that.
412 */
413 if (byte_offset != last_byte_offset) {
414 ret = kvm_read_guest(vcpu->kvm, pendbase + byte_offset,
415 &pendmask, 1);
416 if (ret) {
417 kfree(intids);
418 return ret;
419 }
420 last_byte_offset = byte_offset;
421 }
422
423 irq = vgic_get_irq(vcpu->kvm, NULL, intids[i]);
424 spin_lock(&irq->irq_lock);
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100425 irq->pending_latch = pendmask & (1U << bit_nr);
Andre Przywara33d3bc92016-07-15 12:43:34 +0100426 vgic_queue_irq_unlock(vcpu->kvm, irq);
427 vgic_put_irq(vcpu->kvm, irq);
428 }
429
430 kfree(intids);
431
432 return ret;
433}
Andre Przywara424c3382016-07-15 12:43:32 +0100434
Andre Przywara424c3382016-07-15 12:43:32 +0100435static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,
436 struct vgic_its *its,
437 gpa_t addr, unsigned int len)
438{
Eric Auger71afe472017-04-13 09:06:20 +0200439 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
Andre Przywara424c3382016-07-15 12:43:32 +0100440 u64 reg = GITS_TYPER_PLPIS;
441
442 /*
443 * We use linear CPU numbers for redistributor addressing,
444 * so GITS_TYPER.PTA is 0.
445 * Also we force all PROPBASER registers to be the same, so
446 * CommonLPIAff is 0 as well.
447 * To avoid memory waste in the guest, we keep the number of IDBits and
448 * DevBits low - as least for the time being.
449 */
Eric Auger07a3e9a2017-02-02 14:37:33 +0100450 reg |= GIC_ENCODE_SZ(VITS_TYPER_DEVBITS, 5) << GITS_TYPER_DEVBITS_SHIFT;
Eric Auger0d44cdb2016-12-22 18:14:14 +0100451 reg |= GIC_ENCODE_SZ(VITS_TYPER_IDBITS, 5) << GITS_TYPER_IDBITS_SHIFT;
Eric Auger71afe472017-04-13 09:06:20 +0200452 reg |= GIC_ENCODE_SZ(abi->ite_esz, 4) << GITS_TYPER_ITT_ENTRY_SIZE_SHIFT;
Andre Przywara424c3382016-07-15 12:43:32 +0100453
454 return extract_bytes(reg, addr & 7, len);
455}
456
457static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm,
458 struct vgic_its *its,
459 gpa_t addr, unsigned int len)
460{
Eric Augerab01c6b2017-03-23 15:14:00 +0100461 u32 val;
462
463 val = (its->abi_rev << GITS_IIDR_REV_SHIFT) & GITS_IIDR_REV_MASK;
464 val |= (PRODUCT_ID_KVM << GITS_IIDR_PRODUCTID_SHIFT) | IMPLEMENTER_ARM;
465 return val;
466}
467
468static int vgic_mmio_uaccess_write_its_iidr(struct kvm *kvm,
469 struct vgic_its *its,
470 gpa_t addr, unsigned int len,
471 unsigned long val)
472{
473 u32 rev = GITS_IIDR_REV(val);
474
475 if (rev >= NR_ITS_ABIS)
476 return -EINVAL;
477 return vgic_its_set_abi(its, rev);
Andre Przywara424c3382016-07-15 12:43:32 +0100478}
479
480static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm,
481 struct vgic_its *its,
482 gpa_t addr, unsigned int len)
483{
484 switch (addr & 0xffff) {
485 case GITS_PIDR0:
486 return 0x92; /* part number, bits[7:0] */
487 case GITS_PIDR1:
488 return 0xb4; /* part number, bits[11:8] */
489 case GITS_PIDR2:
490 return GIC_PIDR2_ARCH_GICv3 | 0x0b;
491 case GITS_PIDR4:
492 return 0x40; /* This is a 64K software visible page */
493 /* The following are the ID registers for (any) GIC. */
494 case GITS_CIDR0:
495 return 0x0d;
496 case GITS_CIDR1:
497 return 0xf0;
498 case GITS_CIDR2:
499 return 0x05;
500 case GITS_CIDR3:
501 return 0xb1;
502 }
503
504 return 0;
505}
506
Andre Przywara2891a7d2016-07-15 12:43:37 +0100507/*
508 * Find the target VCPU and the LPI number for a given devid/eventid pair
509 * and make this IRQ pending, possibly injecting it.
510 * Must be called with the its_lock mutex held.
Andre Przywarafd837b02016-08-08 17:29:28 +0100511 * Returns 0 on success, a positive error value for any ITS mapping
512 * related errors and negative error values for generic errors.
Andre Przywara2891a7d2016-07-15 12:43:37 +0100513 */
Andre Przywarafd837b02016-08-08 17:29:28 +0100514static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its,
515 u32 devid, u32 eventid)
Andre Przywara2891a7d2016-07-15 12:43:37 +0100516{
Andre Przywarafd837b02016-08-08 17:29:28 +0100517 struct kvm_vcpu *vcpu;
Eric Auger9ce91c72017-02-08 06:09:29 +0100518 struct its_ite *ite;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100519
520 if (!its->enabled)
Andre Przywarafd837b02016-08-08 17:29:28 +0100521 return -EBUSY;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100522
Eric Auger9ce91c72017-02-08 06:09:29 +0100523 ite = find_ite(its, devid, eventid);
524 if (!ite || !its_is_collection_mapped(ite->collection))
Andre Przywarafd837b02016-08-08 17:29:28 +0100525 return E_ITS_INT_UNMAPPED_INTERRUPT;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100526
Eric Auger9ce91c72017-02-08 06:09:29 +0100527 vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr);
Andre Przywarafd837b02016-08-08 17:29:28 +0100528 if (!vcpu)
529 return E_ITS_INT_UNMAPPED_INTERRUPT;
530
531 if (!vcpu->arch.vgic_cpu.lpis_enabled)
532 return -EBUSY;
533
Eric Auger9ce91c72017-02-08 06:09:29 +0100534 spin_lock(&ite->irq->irq_lock);
535 ite->irq->pending_latch = true;
536 vgic_queue_irq_unlock(kvm, ite->irq);
Andre Przywarafd837b02016-08-08 17:29:28 +0100537
538 return 0;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100539}
540
Andre Przywara505a19e2016-08-09 10:54:29 +0100541static struct vgic_io_device *vgic_get_its_iodev(struct kvm_io_device *dev)
542{
543 struct vgic_io_device *iodev;
544
545 if (dev->ops != &kvm_io_gic_ops)
546 return NULL;
547
548 iodev = container_of(dev, struct vgic_io_device, dev);
549
550 if (iodev->iodev_type != IODEV_ITS)
551 return NULL;
552
553 return iodev;
554}
555
Andre Przywara2891a7d2016-07-15 12:43:37 +0100556/*
557 * Queries the KVM IO bus framework to get the ITS pointer from the given
558 * doorbell address.
559 * We then call vgic_its_trigger_msi() with the decoded data.
Andre Przywarafd837b02016-08-08 17:29:28 +0100560 * According to the KVM_SIGNAL_MSI API description returns 1 on success.
Andre Przywara2891a7d2016-07-15 12:43:37 +0100561 */
562int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi)
563{
564 u64 address;
565 struct kvm_io_device *kvm_io_dev;
566 struct vgic_io_device *iodev;
Andre Przywarafd837b02016-08-08 17:29:28 +0100567 int ret;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100568
569 if (!vgic_has_its(kvm))
570 return -ENODEV;
571
572 if (!(msi->flags & KVM_MSI_VALID_DEVID))
573 return -EINVAL;
574
575 address = (u64)msi->address_hi << 32 | msi->address_lo;
576
577 kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, address);
578 if (!kvm_io_dev)
Andre Przywara505a19e2016-08-09 10:54:29 +0100579 return -EINVAL;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100580
Andre Przywara505a19e2016-08-09 10:54:29 +0100581 iodev = vgic_get_its_iodev(kvm_io_dev);
582 if (!iodev)
583 return -EINVAL;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100584
585 mutex_lock(&iodev->its->its_lock);
Andre Przywarafd837b02016-08-08 17:29:28 +0100586 ret = vgic_its_trigger_msi(kvm, iodev->its, msi->devid, msi->data);
Andre Przywara2891a7d2016-07-15 12:43:37 +0100587 mutex_unlock(&iodev->its->its_lock);
588
Andre Przywarafd837b02016-08-08 17:29:28 +0100589 if (ret < 0)
590 return ret;
591
592 /*
593 * KVM_SIGNAL_MSI demands a return value > 0 for success and 0
594 * if the guest has blocked the MSI. So we map any LPI mapping
595 * related error to that.
596 */
597 if (ret)
598 return 0;
599 else
600 return 1;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100601}
602
Andre Przywara424c3382016-07-15 12:43:32 +0100603/* Requires the its_lock to be held. */
Eric Auger9ce91c72017-02-08 06:09:29 +0100604static void its_free_ite(struct kvm *kvm, struct its_ite *ite)
Andre Przywara424c3382016-07-15 12:43:32 +0100605{
Eric Auger9ce91c72017-02-08 06:09:29 +0100606 list_del(&ite->ite_list);
Andre Przywara38024112016-07-15 12:43:33 +0100607
608 /* This put matches the get in vgic_add_lpi. */
Eric Auger9ce91c72017-02-08 06:09:29 +0100609 if (ite->irq)
610 vgic_put_irq(kvm, ite->irq);
Andre Przywara38024112016-07-15 12:43:33 +0100611
Eric Auger9ce91c72017-02-08 06:09:29 +0100612 kfree(ite);
Andre Przywara424c3382016-07-15 12:43:32 +0100613}
614
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100615static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size)
616{
617 return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT_ULL(size) - 1);
618}
619
620#define its_cmd_get_command(cmd) its_cmd_mask_field(cmd, 0, 0, 8)
621#define its_cmd_get_deviceid(cmd) its_cmd_mask_field(cmd, 0, 32, 32)
Eric Auger0d44cdb2016-12-22 18:14:14 +0100622#define its_cmd_get_size(cmd) (its_cmd_mask_field(cmd, 1, 0, 5) + 1)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100623#define its_cmd_get_id(cmd) its_cmd_mask_field(cmd, 1, 0, 32)
624#define its_cmd_get_physical_id(cmd) its_cmd_mask_field(cmd, 1, 32, 32)
625#define its_cmd_get_collection(cmd) its_cmd_mask_field(cmd, 2, 0, 16)
Eric Auger7333cef2017-02-02 13:45:45 +0100626#define its_cmd_get_ittaddr(cmd) (its_cmd_mask_field(cmd, 2, 8, 44) << 8)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100627#define its_cmd_get_target_addr(cmd) its_cmd_mask_field(cmd, 2, 16, 32)
628#define its_cmd_get_validbit(cmd) its_cmd_mask_field(cmd, 2, 63, 1)
629
630/*
631 * The DISCARD command frees an Interrupt Translation Table Entry (ITTE).
632 * Must be called with the its_lock mutex held.
633 */
634static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its,
635 u64 *its_cmd)
636{
637 u32 device_id = its_cmd_get_deviceid(its_cmd);
638 u32 event_id = its_cmd_get_id(its_cmd);
Eric Auger9ce91c72017-02-08 06:09:29 +0100639 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100640
641
Eric Auger9ce91c72017-02-08 06:09:29 +0100642 ite = find_ite(its, device_id, event_id);
643 if (ite && ite->collection) {
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100644 /*
645 * Though the spec talks about removing the pending state, we
646 * don't bother here since we clear the ITTE anyway and the
647 * pending state is a property of the ITTE struct.
648 */
Eric Auger9ce91c72017-02-08 06:09:29 +0100649 its_free_ite(kvm, ite);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100650 return 0;
651 }
652
653 return E_ITS_DISCARD_UNMAPPED_INTERRUPT;
654}
655
656/*
657 * The MOVI command moves an ITTE to a different collection.
658 * Must be called with the its_lock mutex held.
659 */
660static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,
661 u64 *its_cmd)
662{
663 u32 device_id = its_cmd_get_deviceid(its_cmd);
664 u32 event_id = its_cmd_get_id(its_cmd);
665 u32 coll_id = its_cmd_get_collection(its_cmd);
666 struct kvm_vcpu *vcpu;
Eric Auger9ce91c72017-02-08 06:09:29 +0100667 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100668 struct its_collection *collection;
669
Eric Auger9ce91c72017-02-08 06:09:29 +0100670 ite = find_ite(its, device_id, event_id);
671 if (!ite)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100672 return E_ITS_MOVI_UNMAPPED_INTERRUPT;
673
Eric Auger9ce91c72017-02-08 06:09:29 +0100674 if (!its_is_collection_mapped(ite->collection))
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100675 return E_ITS_MOVI_UNMAPPED_COLLECTION;
676
677 collection = find_collection(its, coll_id);
678 if (!its_is_collection_mapped(collection))
679 return E_ITS_MOVI_UNMAPPED_COLLECTION;
680
Eric Auger9ce91c72017-02-08 06:09:29 +0100681 ite->collection = collection;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100682 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
683
Eric Auger9ce91c72017-02-08 06:09:29 +0100684 spin_lock(&ite->irq->irq_lock);
685 ite->irq->target_vcpu = vcpu;
686 spin_unlock(&ite->irq->irq_lock);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100687
688 return 0;
689}
690
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100691/*
692 * Check whether an ID can be stored into the corresponding guest table.
693 * For a direct table this is pretty easy, but gets a bit nasty for
694 * indirect tables. We check whether the resulting guest physical address
Eric Auger07a3e9a2017-02-02 14:37:33 +0100695 * is actually valid (covered by a memslot and guest accessible).
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100696 * For this we have to read the respective first level entry.
697 */
Eric Augerdceff702017-01-31 14:36:14 +0100698static bool vgic_its_check_id(struct vgic_its *its, u64 baser, u32 id,
699 gpa_t *eaddr)
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100700{
701 int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
Eric Auger07a3e9a2017-02-02 14:37:33 +0100702 u64 indirect_ptr, type = GITS_BASER_TYPE(baser);
Vladimir Murzine29bd6f2016-11-02 11:55:33 +0000703 int esz = GITS_BASER_ENTRY_SIZE(baser);
Eric Auger07a3e9a2017-02-02 14:37:33 +0100704 int index;
705 gfn_t gfn;
706
707 switch (type) {
708 case GITS_BASER_TYPE_DEVICE:
709 if (id >= BIT_ULL(VITS_TYPER_DEVBITS))
710 return false;
711 break;
712 case GITS_BASER_TYPE_COLLECTION:
713 /* as GITS_TYPER.CIL == 0, ITS supports 16-bit collection ID */
714 if (id >= BIT_ULL(16))
715 return false;
716 break;
717 default:
718 return false;
719 }
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100720
721 if (!(baser & GITS_BASER_INDIRECT)) {
722 phys_addr_t addr;
723
Vladimir Murzine29bd6f2016-11-02 11:55:33 +0000724 if (id >= (l1_tbl_size / esz))
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100725 return false;
726
Vladimir Murzine29bd6f2016-11-02 11:55:33 +0000727 addr = BASER_ADDRESS(baser) + id * esz;
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100728 gfn = addr >> PAGE_SHIFT;
729
Eric Augerdceff702017-01-31 14:36:14 +0100730 if (eaddr)
731 *eaddr = addr;
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100732 return kvm_is_visible_gfn(its->dev->kvm, gfn);
733 }
734
735 /* calculate and check the index into the 1st level */
Vladimir Murzine29bd6f2016-11-02 11:55:33 +0000736 index = id / (SZ_64K / esz);
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100737 if (index >= (l1_tbl_size / sizeof(u64)))
738 return false;
739
740 /* Each 1st level entry is represented by a 64-bit value. */
741 if (kvm_read_guest(its->dev->kvm,
742 BASER_ADDRESS(baser) + index * sizeof(indirect_ptr),
743 &indirect_ptr, sizeof(indirect_ptr)))
744 return false;
745
746 indirect_ptr = le64_to_cpu(indirect_ptr);
747
748 /* check the valid bit of the first level entry */
749 if (!(indirect_ptr & BIT_ULL(63)))
750 return false;
751
752 /*
753 * Mask the guest physical address and calculate the frame number.
754 * Any address beyond our supported 48 bits of PA will be caught
755 * by the actual check in the final step.
756 */
757 indirect_ptr &= GENMASK_ULL(51, 16);
758
759 /* Find the address of the actual entry */
Vladimir Murzine29bd6f2016-11-02 11:55:33 +0000760 index = id % (SZ_64K / esz);
761 indirect_ptr += index * esz;
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100762 gfn = indirect_ptr >> PAGE_SHIFT;
763
Eric Augerdceff702017-01-31 14:36:14 +0100764 if (eaddr)
765 *eaddr = indirect_ptr;
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100766 return kvm_is_visible_gfn(its->dev->kvm, gfn);
767}
768
Marc Zyngier17a21f52016-07-17 20:01:46 +0100769static int vgic_its_alloc_collection(struct vgic_its *its,
770 struct its_collection **colp,
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100771 u32 coll_id)
772{
Marc Zyngier17a21f52016-07-17 20:01:46 +0100773 struct its_collection *collection;
774
Eric Augerdceff702017-01-31 14:36:14 +0100775 if (!vgic_its_check_id(its, its->baser_coll_table, coll_id, NULL))
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100776 return E_ITS_MAPC_COLLECTION_OOR;
777
Marc Zyngier17a21f52016-07-17 20:01:46 +0100778 collection = kzalloc(sizeof(*collection), GFP_KERNEL);
779
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100780 collection->collection_id = coll_id;
781 collection->target_addr = COLLECTION_NOT_MAPPED;
782
783 list_add_tail(&collection->coll_list, &its->collection_list);
Marc Zyngier17a21f52016-07-17 20:01:46 +0100784 *colp = collection;
785
786 return 0;
787}
788
789static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)
790{
791 struct its_collection *collection;
792 struct its_device *device;
Eric Auger9ce91c72017-02-08 06:09:29 +0100793 struct its_ite *ite;
Marc Zyngier17a21f52016-07-17 20:01:46 +0100794
795 /*
796 * Clearing the mapping for that collection ID removes the
797 * entry from the list. If there wasn't any before, we can
798 * go home early.
799 */
800 collection = find_collection(its, coll_id);
801 if (!collection)
802 return;
803
Eric Auger9ce91c72017-02-08 06:09:29 +0100804 for_each_lpi_its(device, ite, its)
805 if (ite->collection &&
806 ite->collection->collection_id == coll_id)
807 ite->collection = NULL;
Marc Zyngier17a21f52016-07-17 20:01:46 +0100808
809 list_del(&collection->coll_list);
810 kfree(collection);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100811}
812
Eric Auger528297f2016-12-25 18:57:54 +0100813/* Must be called with its_lock mutex held */
814static struct its_ite *vgic_its_alloc_ite(struct its_device *device,
815 struct its_collection *collection,
816 u32 lpi_id, u32 event_id)
817{
818 struct its_ite *ite;
819
820 ite = kzalloc(sizeof(*ite), GFP_KERNEL);
821 if (!ite)
822 return ERR_PTR(-ENOMEM);
823
824 ite->event_id = event_id;
825 ite->collection = collection;
826 ite->lpi = lpi_id;
827
828 list_add_tail(&ite->ite_list, &device->itt_head);
829 return ite;
830}
831
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100832/*
833 * The MAPTI and MAPI commands map LPIs to ITTEs.
834 * Must be called with its_lock mutex held.
835 */
836static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
Marc Zyngiera3e7aa22016-07-17 22:38:32 +0100837 u64 *its_cmd)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100838{
839 u32 device_id = its_cmd_get_deviceid(its_cmd);
840 u32 event_id = its_cmd_get_id(its_cmd);
841 u32 coll_id = its_cmd_get_collection(its_cmd);
Eric Auger9ce91c72017-02-08 06:09:29 +0100842 struct its_ite *ite;
Eric Auger06bd5352017-05-04 11:36:32 +0200843 struct kvm_vcpu *vcpu = NULL;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100844 struct its_device *device;
845 struct its_collection *collection, *new_coll = NULL;
Christoffer Dall99e5e882016-08-01 20:25:33 +0200846 struct vgic_irq *irq;
Eric Auger528297f2016-12-25 18:57:54 +0100847 int lpi_nr;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100848
849 device = find_its_device(its, device_id);
850 if (!device)
851 return E_ITS_MAPTI_UNMAPPED_DEVICE;
852
Eric Auger0d44cdb2016-12-22 18:14:14 +0100853 if (event_id >= BIT_ULL(device->num_eventid_bits))
854 return E_ITS_MAPTI_ID_OOR;
855
Marc Zyngiera3e7aa22016-07-17 22:38:32 +0100856 if (its_cmd_get_command(its_cmd) == GITS_CMD_MAPTI)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100857 lpi_nr = its_cmd_get_physical_id(its_cmd);
858 else
859 lpi_nr = event_id;
860 if (lpi_nr < GIC_LPI_OFFSET ||
Marc Zyngier3a88bde2016-07-18 16:27:14 +0100861 lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser))
862 return E_ITS_MAPTI_PHYSICALID_OOR;
863
Andre Przywara286054a2016-08-16 17:51:06 +0100864 /* If there is an existing mapping, behavior is UNPREDICTABLE. */
Eric Auger9ce91c72017-02-08 06:09:29 +0100865 if (find_ite(its, device_id, event_id))
Andre Przywara286054a2016-08-16 17:51:06 +0100866 return 0;
867
Marc Zyngier3a88bde2016-07-18 16:27:14 +0100868 collection = find_collection(its, coll_id);
869 if (!collection) {
870 int ret = vgic_its_alloc_collection(its, &collection, coll_id);
871 if (ret)
872 return ret;
873 new_coll = collection;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100874 }
875
Eric Auger528297f2016-12-25 18:57:54 +0100876 ite = vgic_its_alloc_ite(device, collection, lpi_nr, event_id);
877 if (IS_ERR(ite)) {
Andre Przywara286054a2016-08-16 17:51:06 +0100878 if (new_coll)
879 vgic_its_free_collection(its, coll_id);
Eric Auger528297f2016-12-25 18:57:54 +0100880 return PTR_ERR(ite);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100881 }
882
Eric Auger06bd5352017-05-04 11:36:32 +0200883 if (its_is_collection_mapped(collection))
884 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
885
886 irq = vgic_add_lpi(kvm, lpi_nr, vcpu);
Christoffer Dall99e5e882016-08-01 20:25:33 +0200887 if (IS_ERR(irq)) {
888 if (new_coll)
889 vgic_its_free_collection(its, coll_id);
Eric Auger9ce91c72017-02-08 06:09:29 +0100890 its_free_ite(kvm, ite);
Christoffer Dall99e5e882016-08-01 20:25:33 +0200891 return PTR_ERR(irq);
892 }
Eric Auger9ce91c72017-02-08 06:09:29 +0100893 ite->irq = irq;
Christoffer Dall99e5e882016-08-01 20:25:33 +0200894
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100895 return 0;
896}
897
898/* Requires the its_lock to be held. */
899static void vgic_its_unmap_device(struct kvm *kvm, struct its_device *device)
900{
Eric Auger9ce91c72017-02-08 06:09:29 +0100901 struct its_ite *ite, *temp;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100902
903 /*
904 * The spec says that unmapping a device with still valid
905 * ITTEs associated is UNPREDICTABLE. We remove all ITTEs,
906 * since we cannot leave the memory unreferenced.
907 */
Eric Auger9ce91c72017-02-08 06:09:29 +0100908 list_for_each_entry_safe(ite, temp, &device->itt_head, ite_list)
909 its_free_ite(kvm, ite);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100910
911 list_del(&device->dev_list);
912 kfree(device);
913}
914
Eric Auger528297f2016-12-25 18:57:54 +0100915/* Must be called with its_lock mutex held */
916static struct its_device *vgic_its_alloc_device(struct vgic_its *its,
917 u32 device_id, gpa_t itt_addr,
918 u8 num_eventid_bits)
919{
920 struct its_device *device;
921
922 device = kzalloc(sizeof(*device), GFP_KERNEL);
923 if (!device)
924 return ERR_PTR(-ENOMEM);
925
926 device->device_id = device_id;
927 device->itt_addr = itt_addr;
928 device->num_eventid_bits = num_eventid_bits;
929 INIT_LIST_HEAD(&device->itt_head);
930
931 list_add_tail(&device->dev_list, &its->device_list);
932 return device;
933}
934
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100935/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100936 * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs).
937 * Must be called with the its_lock mutex held.
938 */
939static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
940 u64 *its_cmd)
941{
942 u32 device_id = its_cmd_get_deviceid(its_cmd);
943 bool valid = its_cmd_get_validbit(its_cmd);
Eric Auger0d44cdb2016-12-22 18:14:14 +0100944 u8 num_eventid_bits = its_cmd_get_size(its_cmd);
Eric Auger7333cef2017-02-02 13:45:45 +0100945 gpa_t itt_addr = its_cmd_get_ittaddr(its_cmd);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100946 struct its_device *device;
947
Eric Augerdceff702017-01-31 14:36:14 +0100948 if (!vgic_its_check_id(its, its->baser_device_table, device_id, NULL))
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100949 return E_ITS_MAPD_DEVICE_OOR;
950
Eric Auger0d44cdb2016-12-22 18:14:14 +0100951 if (valid && num_eventid_bits > VITS_TYPER_IDBITS)
952 return E_ITS_MAPD_ITTSIZE_OOR;
953
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100954 device = find_its_device(its, device_id);
955
956 /*
957 * The spec says that calling MAPD on an already mapped device
958 * invalidates all cached data for this device. We implement this
959 * by removing the mapping and re-establishing it.
960 */
961 if (device)
962 vgic_its_unmap_device(kvm, device);
963
964 /*
965 * The spec does not say whether unmapping a not-mapped device
966 * is an error, so we are done in any case.
967 */
968 if (!valid)
969 return 0;
970
Eric Auger528297f2016-12-25 18:57:54 +0100971 device = vgic_its_alloc_device(its, device_id, itt_addr,
972 num_eventid_bits);
973 if (IS_ERR(device))
974 return PTR_ERR(device);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100975
976 return 0;
977}
978
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100979/*
980 * The MAPC command maps collection IDs to redistributors.
981 * Must be called with the its_lock mutex held.
982 */
983static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its,
984 u64 *its_cmd)
985{
986 u16 coll_id;
987 u32 target_addr;
988 struct its_collection *collection;
989 bool valid;
990
991 valid = its_cmd_get_validbit(its_cmd);
992 coll_id = its_cmd_get_collection(its_cmd);
993 target_addr = its_cmd_get_target_addr(its_cmd);
994
995 if (target_addr >= atomic_read(&kvm->online_vcpus))
996 return E_ITS_MAPC_PROCNUM_OOR;
997
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100998 if (!valid) {
Marc Zyngier17a21f52016-07-17 20:01:46 +0100999 vgic_its_free_collection(its, coll_id);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001000 } else {
Marc Zyngier17a21f52016-07-17 20:01:46 +01001001 collection = find_collection(its, coll_id);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001002
Marc Zyngier17a21f52016-07-17 20:01:46 +01001003 if (!collection) {
1004 int ret;
1005
1006 ret = vgic_its_alloc_collection(its, &collection,
1007 coll_id);
1008 if (ret)
1009 return ret;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001010 collection->target_addr = target_addr;
1011 } else {
1012 collection->target_addr = target_addr;
1013 update_affinity_collection(kvm, its, collection);
1014 }
1015 }
1016
1017 return 0;
1018}
1019
1020/*
1021 * The CLEAR command removes the pending state for a particular LPI.
1022 * Must be called with the its_lock mutex held.
1023 */
1024static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its,
1025 u64 *its_cmd)
1026{
1027 u32 device_id = its_cmd_get_deviceid(its_cmd);
1028 u32 event_id = its_cmd_get_id(its_cmd);
Eric Auger9ce91c72017-02-08 06:09:29 +01001029 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001030
1031
Eric Auger9ce91c72017-02-08 06:09:29 +01001032 ite = find_ite(its, device_id, event_id);
1033 if (!ite)
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001034 return E_ITS_CLEAR_UNMAPPED_INTERRUPT;
1035
Eric Auger9ce91c72017-02-08 06:09:29 +01001036 ite->irq->pending_latch = false;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001037
1038 return 0;
1039}
1040
1041/*
1042 * The INV command syncs the configuration bits from the memory table.
1043 * Must be called with the its_lock mutex held.
1044 */
1045static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its,
1046 u64 *its_cmd)
1047{
1048 u32 device_id = its_cmd_get_deviceid(its_cmd);
1049 u32 event_id = its_cmd_get_id(its_cmd);
Eric Auger9ce91c72017-02-08 06:09:29 +01001050 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001051
1052
Eric Auger9ce91c72017-02-08 06:09:29 +01001053 ite = find_ite(its, device_id, event_id);
1054 if (!ite)
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001055 return E_ITS_INV_UNMAPPED_INTERRUPT;
1056
Eric Auger9ce91c72017-02-08 06:09:29 +01001057 return update_lpi_config(kvm, ite->irq, NULL);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001058}
1059
1060/*
1061 * The INVALL command requests flushing of all IRQ data in this collection.
1062 * Find the VCPU mapped to that collection, then iterate over the VM's list
1063 * of mapped LPIs and update the configuration for each IRQ which targets
1064 * the specified vcpu. The configuration will be read from the in-memory
1065 * configuration table.
1066 * Must be called with the its_lock mutex held.
1067 */
1068static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its,
1069 u64 *its_cmd)
1070{
1071 u32 coll_id = its_cmd_get_collection(its_cmd);
1072 struct its_collection *collection;
1073 struct kvm_vcpu *vcpu;
1074 struct vgic_irq *irq;
1075 u32 *intids;
1076 int irq_count, i;
1077
1078 collection = find_collection(its, coll_id);
1079 if (!its_is_collection_mapped(collection))
1080 return E_ITS_INVALL_UNMAPPED_COLLECTION;
1081
1082 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
1083
Eric Augerccb1d792017-04-12 14:13:27 +02001084 irq_count = vgic_copy_lpi_list(vcpu, &intids);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001085 if (irq_count < 0)
1086 return irq_count;
1087
1088 for (i = 0; i < irq_count; i++) {
1089 irq = vgic_get_irq(kvm, NULL, intids[i]);
1090 if (!irq)
1091 continue;
1092 update_lpi_config(kvm, irq, vcpu);
1093 vgic_put_irq(kvm, irq);
1094 }
1095
1096 kfree(intids);
1097
1098 return 0;
1099}
1100
1101/*
1102 * The MOVALL command moves the pending state of all IRQs targeting one
1103 * redistributor to another. We don't hold the pending state in the VCPUs,
1104 * but in the IRQs instead, so there is really not much to do for us here.
1105 * However the spec says that no IRQ must target the old redistributor
1106 * afterwards, so we make sure that no LPI is using the associated target_vcpu.
1107 * This command affects all LPIs in the system that target that redistributor.
1108 */
1109static int vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its,
1110 u64 *its_cmd)
1111{
1112 struct vgic_dist *dist = &kvm->arch.vgic;
1113 u32 target1_addr = its_cmd_get_target_addr(its_cmd);
1114 u32 target2_addr = its_cmd_mask_field(its_cmd, 3, 16, 32);
1115 struct kvm_vcpu *vcpu1, *vcpu2;
1116 struct vgic_irq *irq;
1117
1118 if (target1_addr >= atomic_read(&kvm->online_vcpus) ||
1119 target2_addr >= atomic_read(&kvm->online_vcpus))
1120 return E_ITS_MOVALL_PROCNUM_OOR;
1121
1122 if (target1_addr == target2_addr)
1123 return 0;
1124
1125 vcpu1 = kvm_get_vcpu(kvm, target1_addr);
1126 vcpu2 = kvm_get_vcpu(kvm, target2_addr);
1127
1128 spin_lock(&dist->lpi_list_lock);
1129
1130 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
1131 spin_lock(&irq->irq_lock);
1132
1133 if (irq->target_vcpu == vcpu1)
1134 irq->target_vcpu = vcpu2;
1135
1136 spin_unlock(&irq->irq_lock);
1137 }
1138
1139 spin_unlock(&dist->lpi_list_lock);
1140
1141 return 0;
1142}
1143
1144/*
Andre Przywara2891a7d2016-07-15 12:43:37 +01001145 * The INT command injects the LPI associated with that DevID/EvID pair.
1146 * Must be called with the its_lock mutex held.
1147 */
1148static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its,
1149 u64 *its_cmd)
1150{
1151 u32 msi_data = its_cmd_get_id(its_cmd);
1152 u64 msi_devid = its_cmd_get_deviceid(its_cmd);
1153
Andre Przywarafd837b02016-08-08 17:29:28 +01001154 return vgic_its_trigger_msi(kvm, its, msi_devid, msi_data);
Andre Przywara2891a7d2016-07-15 12:43:37 +01001155}
1156
1157/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001158 * This function is called with the its_cmd lock held, but the ITS data
1159 * structure lock dropped.
1160 */
Andre Przywara424c3382016-07-15 12:43:32 +01001161static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its,
1162 u64 *its_cmd)
1163{
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001164 int ret = -ENODEV;
1165
1166 mutex_lock(&its->its_lock);
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001167 switch (its_cmd_get_command(its_cmd)) {
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001168 case GITS_CMD_MAPD:
1169 ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd);
1170 break;
1171 case GITS_CMD_MAPC:
1172 ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd);
1173 break;
1174 case GITS_CMD_MAPI:
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001175 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001176 break;
1177 case GITS_CMD_MAPTI:
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001178 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001179 break;
1180 case GITS_CMD_MOVI:
1181 ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd);
1182 break;
1183 case GITS_CMD_DISCARD:
1184 ret = vgic_its_cmd_handle_discard(kvm, its, its_cmd);
1185 break;
1186 case GITS_CMD_CLEAR:
1187 ret = vgic_its_cmd_handle_clear(kvm, its, its_cmd);
1188 break;
1189 case GITS_CMD_MOVALL:
1190 ret = vgic_its_cmd_handle_movall(kvm, its, its_cmd);
1191 break;
Andre Przywara2891a7d2016-07-15 12:43:37 +01001192 case GITS_CMD_INT:
1193 ret = vgic_its_cmd_handle_int(kvm, its, its_cmd);
1194 break;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001195 case GITS_CMD_INV:
1196 ret = vgic_its_cmd_handle_inv(kvm, its, its_cmd);
1197 break;
1198 case GITS_CMD_INVALL:
1199 ret = vgic_its_cmd_handle_invall(kvm, its, its_cmd);
1200 break;
1201 case GITS_CMD_SYNC:
1202 /* we ignore this command: we are in sync all of the time */
1203 ret = 0;
1204 break;
1205 }
1206 mutex_unlock(&its->its_lock);
1207
1208 return ret;
Andre Przywara424c3382016-07-15 12:43:32 +01001209}
1210
1211static u64 vgic_sanitise_its_baser(u64 reg)
1212{
1213 reg = vgic_sanitise_field(reg, GITS_BASER_SHAREABILITY_MASK,
1214 GITS_BASER_SHAREABILITY_SHIFT,
1215 vgic_sanitise_shareability);
1216 reg = vgic_sanitise_field(reg, GITS_BASER_INNER_CACHEABILITY_MASK,
1217 GITS_BASER_INNER_CACHEABILITY_SHIFT,
1218 vgic_sanitise_inner_cacheability);
1219 reg = vgic_sanitise_field(reg, GITS_BASER_OUTER_CACHEABILITY_MASK,
1220 GITS_BASER_OUTER_CACHEABILITY_SHIFT,
1221 vgic_sanitise_outer_cacheability);
1222
1223 /* Bits 15:12 contain bits 51:48 of the PA, which we don't support. */
1224 reg &= ~GENMASK_ULL(15, 12);
1225
1226 /* We support only one (ITS) page size: 64K */
1227 reg = (reg & ~GITS_BASER_PAGE_SIZE_MASK) | GITS_BASER_PAGE_SIZE_64K;
1228
1229 return reg;
1230}
1231
1232static u64 vgic_sanitise_its_cbaser(u64 reg)
1233{
1234 reg = vgic_sanitise_field(reg, GITS_CBASER_SHAREABILITY_MASK,
1235 GITS_CBASER_SHAREABILITY_SHIFT,
1236 vgic_sanitise_shareability);
1237 reg = vgic_sanitise_field(reg, GITS_CBASER_INNER_CACHEABILITY_MASK,
1238 GITS_CBASER_INNER_CACHEABILITY_SHIFT,
1239 vgic_sanitise_inner_cacheability);
1240 reg = vgic_sanitise_field(reg, GITS_CBASER_OUTER_CACHEABILITY_MASK,
1241 GITS_CBASER_OUTER_CACHEABILITY_SHIFT,
1242 vgic_sanitise_outer_cacheability);
1243
1244 /*
1245 * Sanitise the physical address to be 64k aligned.
1246 * Also limit the physical addresses to 48 bits.
1247 */
1248 reg &= ~(GENMASK_ULL(51, 48) | GENMASK_ULL(15, 12));
1249
1250 return reg;
1251}
1252
1253static unsigned long vgic_mmio_read_its_cbaser(struct kvm *kvm,
1254 struct vgic_its *its,
1255 gpa_t addr, unsigned int len)
1256{
1257 return extract_bytes(its->cbaser, addr & 7, len);
1258}
1259
1260static void vgic_mmio_write_its_cbaser(struct kvm *kvm, struct vgic_its *its,
1261 gpa_t addr, unsigned int len,
1262 unsigned long val)
1263{
1264 /* When GITS_CTLR.Enable is 1, this register is RO. */
1265 if (its->enabled)
1266 return;
1267
1268 mutex_lock(&its->cmd_lock);
1269 its->cbaser = update_64bit_reg(its->cbaser, addr & 7, len, val);
1270 its->cbaser = vgic_sanitise_its_cbaser(its->cbaser);
1271 its->creadr = 0;
1272 /*
1273 * CWRITER is architecturally UNKNOWN on reset, but we need to reset
1274 * it to CREADR to make sure we start with an empty command buffer.
1275 */
1276 its->cwriter = its->creadr;
1277 mutex_unlock(&its->cmd_lock);
1278}
1279
1280#define ITS_CMD_BUFFER_SIZE(baser) ((((baser) & 0xff) + 1) << 12)
1281#define ITS_CMD_SIZE 32
1282#define ITS_CMD_OFFSET(reg) ((reg) & GENMASK(19, 5))
1283
Andre Przywaraa5e1e6c2017-02-16 10:41:20 +00001284/* Must be called with the cmd_lock held. */
1285static void vgic_its_process_commands(struct kvm *kvm, struct vgic_its *its)
Andre Przywara424c3382016-07-15 12:43:32 +01001286{
1287 gpa_t cbaser;
1288 u64 cmd_buf[4];
Andre Przywara424c3382016-07-15 12:43:32 +01001289
Andre Przywaraa5e1e6c2017-02-16 10:41:20 +00001290 /* Commands are only processed when the ITS is enabled. */
1291 if (!its->enabled)
Andre Przywara424c3382016-07-15 12:43:32 +01001292 return;
1293
Andre Przywara424c3382016-07-15 12:43:32 +01001294 cbaser = CBASER_ADDRESS(its->cbaser);
1295
1296 while (its->cwriter != its->creadr) {
1297 int ret = kvm_read_guest(kvm, cbaser + its->creadr,
1298 cmd_buf, ITS_CMD_SIZE);
1299 /*
1300 * If kvm_read_guest() fails, this could be due to the guest
1301 * programming a bogus value in CBASER or something else going
1302 * wrong from which we cannot easily recover.
1303 * According to section 6.3.2 in the GICv3 spec we can just
1304 * ignore that command then.
1305 */
1306 if (!ret)
1307 vgic_its_handle_command(kvm, its, cmd_buf);
1308
1309 its->creadr += ITS_CMD_SIZE;
1310 if (its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser))
1311 its->creadr = 0;
1312 }
Andre Przywaraa5e1e6c2017-02-16 10:41:20 +00001313}
1314
1315/*
1316 * By writing to CWRITER the guest announces new commands to be processed.
1317 * To avoid any races in the first place, we take the its_cmd lock, which
1318 * protects our ring buffer variables, so that there is only one user
1319 * per ITS handling commands at a given time.
1320 */
1321static void vgic_mmio_write_its_cwriter(struct kvm *kvm, struct vgic_its *its,
1322 gpa_t addr, unsigned int len,
1323 unsigned long val)
1324{
1325 u64 reg;
1326
1327 if (!its)
1328 return;
1329
1330 mutex_lock(&its->cmd_lock);
1331
1332 reg = update_64bit_reg(its->cwriter, addr & 7, len, val);
1333 reg = ITS_CMD_OFFSET(reg);
1334 if (reg >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
1335 mutex_unlock(&its->cmd_lock);
1336 return;
1337 }
1338 its->cwriter = reg;
1339
1340 vgic_its_process_commands(kvm, its);
Andre Przywara424c3382016-07-15 12:43:32 +01001341
1342 mutex_unlock(&its->cmd_lock);
1343}
1344
1345static unsigned long vgic_mmio_read_its_cwriter(struct kvm *kvm,
1346 struct vgic_its *its,
1347 gpa_t addr, unsigned int len)
1348{
1349 return extract_bytes(its->cwriter, addr & 0x7, len);
1350}
1351
1352static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,
1353 struct vgic_its *its,
1354 gpa_t addr, unsigned int len)
1355{
1356 return extract_bytes(its->creadr, addr & 0x7, len);
1357}
1358
Eric Auger0979bfa2017-01-04 11:58:41 +01001359static int vgic_mmio_uaccess_write_its_creadr(struct kvm *kvm,
1360 struct vgic_its *its,
1361 gpa_t addr, unsigned int len,
1362 unsigned long val)
1363{
1364 u32 cmd_offset;
1365 int ret = 0;
1366
1367 mutex_lock(&its->cmd_lock);
1368
1369 if (its->enabled) {
1370 ret = -EBUSY;
1371 goto out;
1372 }
1373
1374 cmd_offset = ITS_CMD_OFFSET(val);
1375 if (cmd_offset >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
1376 ret = -EINVAL;
1377 goto out;
1378 }
1379
1380 its->creadr = cmd_offset;
1381out:
1382 mutex_unlock(&its->cmd_lock);
1383 return ret;
1384}
1385
Andre Przywara424c3382016-07-15 12:43:32 +01001386#define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7)
1387static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm,
1388 struct vgic_its *its,
1389 gpa_t addr, unsigned int len)
1390{
1391 u64 reg;
1392
1393 switch (BASER_INDEX(addr)) {
1394 case 0:
1395 reg = its->baser_device_table;
1396 break;
1397 case 1:
1398 reg = its->baser_coll_table;
1399 break;
1400 default:
1401 reg = 0;
1402 break;
1403 }
1404
1405 return extract_bytes(reg, addr & 7, len);
1406}
1407
1408#define GITS_BASER_RO_MASK (GENMASK_ULL(52, 48) | GENMASK_ULL(58, 56))
1409static void vgic_mmio_write_its_baser(struct kvm *kvm,
1410 struct vgic_its *its,
1411 gpa_t addr, unsigned int len,
1412 unsigned long val)
1413{
Eric Auger71afe472017-04-13 09:06:20 +02001414 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
Andre Przywara424c3382016-07-15 12:43:32 +01001415 u64 entry_size, device_type;
1416 u64 reg, *regptr, clearbits = 0;
1417
1418 /* When GITS_CTLR.Enable is 1, we ignore write accesses. */
1419 if (its->enabled)
1420 return;
1421
1422 switch (BASER_INDEX(addr)) {
1423 case 0:
1424 regptr = &its->baser_device_table;
Eric Auger71afe472017-04-13 09:06:20 +02001425 entry_size = abi->dte_esz;
Andre Przywara424c3382016-07-15 12:43:32 +01001426 device_type = GITS_BASER_TYPE_DEVICE;
1427 break;
1428 case 1:
1429 regptr = &its->baser_coll_table;
Eric Auger71afe472017-04-13 09:06:20 +02001430 entry_size = abi->cte_esz;
Andre Przywara424c3382016-07-15 12:43:32 +01001431 device_type = GITS_BASER_TYPE_COLLECTION;
1432 clearbits = GITS_BASER_INDIRECT;
1433 break;
1434 default:
1435 return;
1436 }
1437
1438 reg = update_64bit_reg(*regptr, addr & 7, len, val);
1439 reg &= ~GITS_BASER_RO_MASK;
1440 reg &= ~clearbits;
1441
1442 reg |= (entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
1443 reg |= device_type << GITS_BASER_TYPE_SHIFT;
1444 reg = vgic_sanitise_its_baser(reg);
1445
1446 *regptr = reg;
1447}
1448
Andre Przywaraa5e1e6c2017-02-16 10:41:20 +00001449static unsigned long vgic_mmio_read_its_ctlr(struct kvm *vcpu,
1450 struct vgic_its *its,
1451 gpa_t addr, unsigned int len)
1452{
1453 u32 reg = 0;
1454
1455 mutex_lock(&its->cmd_lock);
1456 if (its->creadr == its->cwriter)
1457 reg |= GITS_CTLR_QUIESCENT;
1458 if (its->enabled)
1459 reg |= GITS_CTLR_ENABLE;
1460 mutex_unlock(&its->cmd_lock);
1461
1462 return reg;
1463}
1464
1465static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its,
1466 gpa_t addr, unsigned int len,
1467 unsigned long val)
1468{
1469 mutex_lock(&its->cmd_lock);
1470
1471 its->enabled = !!(val & GITS_CTLR_ENABLE);
1472
1473 /*
1474 * Try to process any pending commands. This function bails out early
1475 * if the ITS is disabled or no commands have been queued.
1476 */
1477 vgic_its_process_commands(kvm, its);
1478
1479 mutex_unlock(&its->cmd_lock);
1480}
1481
Andre Przywara59c5ab42016-07-15 12:43:30 +01001482#define REGISTER_ITS_DESC(off, rd, wr, length, acc) \
1483{ \
1484 .reg_offset = off, \
1485 .len = length, \
1486 .access_flags = acc, \
1487 .its_read = rd, \
1488 .its_write = wr, \
1489}
1490
Eric Auger0979bfa2017-01-04 11:58:41 +01001491#define REGISTER_ITS_DESC_UACCESS(off, rd, wr, uwr, length, acc)\
1492{ \
1493 .reg_offset = off, \
1494 .len = length, \
1495 .access_flags = acc, \
1496 .its_read = rd, \
1497 .its_write = wr, \
1498 .uaccess_its_write = uwr, \
1499}
1500
Andre Przywara59c5ab42016-07-15 12:43:30 +01001501static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its,
1502 gpa_t addr, unsigned int len, unsigned long val)
1503{
1504 /* Ignore */
1505}
1506
1507static struct vgic_register_region its_registers[] = {
1508 REGISTER_ITS_DESC(GITS_CTLR,
Andre Przywara424c3382016-07-15 12:43:32 +01001509 vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001510 VGIC_ACCESS_32bit),
Eric Augerab01c6b2017-03-23 15:14:00 +01001511 REGISTER_ITS_DESC_UACCESS(GITS_IIDR,
1512 vgic_mmio_read_its_iidr, its_mmio_write_wi,
1513 vgic_mmio_uaccess_write_its_iidr, 4,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001514 VGIC_ACCESS_32bit),
1515 REGISTER_ITS_DESC(GITS_TYPER,
Andre Przywara424c3382016-07-15 12:43:32 +01001516 vgic_mmio_read_its_typer, its_mmio_write_wi, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001517 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1518 REGISTER_ITS_DESC(GITS_CBASER,
Andre Przywara424c3382016-07-15 12:43:32 +01001519 vgic_mmio_read_its_cbaser, vgic_mmio_write_its_cbaser, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001520 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1521 REGISTER_ITS_DESC(GITS_CWRITER,
Andre Przywara424c3382016-07-15 12:43:32 +01001522 vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001523 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
Eric Auger0979bfa2017-01-04 11:58:41 +01001524 REGISTER_ITS_DESC_UACCESS(GITS_CREADR,
1525 vgic_mmio_read_its_creadr, its_mmio_write_wi,
1526 vgic_mmio_uaccess_write_its_creadr, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001527 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1528 REGISTER_ITS_DESC(GITS_BASER,
Andre Przywara424c3382016-07-15 12:43:32 +01001529 vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001530 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1531 REGISTER_ITS_DESC(GITS_IDREGS_BASE,
Andre Przywara424c3382016-07-15 12:43:32 +01001532 vgic_mmio_read_its_idregs, its_mmio_write_wi, 0x30,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001533 VGIC_ACCESS_32bit),
1534};
1535
Andre Przywara33d3bc92016-07-15 12:43:34 +01001536/* This is called on setting the LPI enable bit in the redistributor. */
1537void vgic_enable_lpis(struct kvm_vcpu *vcpu)
1538{
1539 if (!(vcpu->arch.vgic_cpu.pendbaser & GICR_PENDBASER_PTZ))
1540 its_sync_lpi_pending_table(vcpu);
1541}
1542
Christoffer Dall30e1b682017-05-08 13:14:57 +02001543static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its,
1544 u64 addr)
Andre Przywara59c5ab42016-07-15 12:43:30 +01001545{
1546 struct vgic_io_device *iodev = &its->iodev;
1547 int ret;
1548
Christoffer Dall30e1b682017-05-08 13:14:57 +02001549 mutex_lock(&kvm->slots_lock);
1550 if (!IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) {
1551 ret = -EBUSY;
1552 goto out;
1553 }
Andre Przywara59c5ab42016-07-15 12:43:30 +01001554
Christoffer Dall30e1b682017-05-08 13:14:57 +02001555 its->vgic_its_base = addr;
Andre Przywara59c5ab42016-07-15 12:43:30 +01001556 iodev->regions = its_registers;
1557 iodev->nr_regions = ARRAY_SIZE(its_registers);
1558 kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops);
1559
1560 iodev->base_addr = its->vgic_its_base;
1561 iodev->iodev_type = IODEV_ITS;
1562 iodev->its = its;
Andre Przywara59c5ab42016-07-15 12:43:30 +01001563 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr,
1564 KVM_VGIC_V3_ITS_SIZE, &iodev->dev);
Christoffer Dall30e1b682017-05-08 13:14:57 +02001565out:
Andre Przywara59c5ab42016-07-15 12:43:30 +01001566 mutex_unlock(&kvm->slots_lock);
1567
1568 return ret;
1569}
Andre Przywara1085fdc2016-07-15 12:43:31 +01001570
Andre Przywara424c3382016-07-15 12:43:32 +01001571#define INITIAL_BASER_VALUE \
1572 (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb) | \
1573 GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner) | \
1574 GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable) | \
Andre Przywara424c3382016-07-15 12:43:32 +01001575 GITS_BASER_PAGE_SIZE_64K)
1576
1577#define INITIAL_PROPBASER_VALUE \
1578 (GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWb) | \
1579 GIC_BASER_CACHEABILITY(GICR_PROPBASER, OUTER, SameAsInner) | \
1580 GIC_BASER_SHAREABILITY(GICR_PROPBASER, InnerShareable))
1581
Andre Przywara1085fdc2016-07-15 12:43:31 +01001582static int vgic_its_create(struct kvm_device *dev, u32 type)
1583{
1584 struct vgic_its *its;
1585
1586 if (type != KVM_DEV_TYPE_ARM_VGIC_ITS)
1587 return -ENODEV;
1588
1589 its = kzalloc(sizeof(struct vgic_its), GFP_KERNEL);
1590 if (!its)
1591 return -ENOMEM;
1592
Andre Przywara424c3382016-07-15 12:43:32 +01001593 mutex_init(&its->its_lock);
1594 mutex_init(&its->cmd_lock);
1595
Andre Przywara1085fdc2016-07-15 12:43:31 +01001596 its->vgic_its_base = VGIC_ADDR_UNDEF;
1597
Andre Przywara424c3382016-07-15 12:43:32 +01001598 INIT_LIST_HEAD(&its->device_list);
1599 INIT_LIST_HEAD(&its->collection_list);
1600
Shanker Donthineni79962a52017-07-08 08:48:30 -05001601 dev->kvm->arch.vgic.msis_require_devid = true;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001602 dev->kvm->arch.vgic.has_its = true;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001603 its->enabled = false;
Marc Zyngierbb717642016-07-17 21:35:07 +01001604 its->dev = dev;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001605
Andre Przywara424c3382016-07-15 12:43:32 +01001606 its->baser_device_table = INITIAL_BASER_VALUE |
1607 ((u64)GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT);
1608 its->baser_coll_table = INITIAL_BASER_VALUE |
1609 ((u64)GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT);
1610 dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE;
1611
Andre Przywara1085fdc2016-07-15 12:43:31 +01001612 dev->private = its;
1613
Eric Auger71afe472017-04-13 09:06:20 +02001614 return vgic_its_set_abi(its, NR_ITS_ABIS - 1);
Andre Przywara1085fdc2016-07-15 12:43:31 +01001615}
1616
Christoffer Dalla2b19e62017-05-08 13:31:12 +02001617static void vgic_its_free_device(struct kvm *kvm, struct its_device *dev)
1618{
1619 struct its_ite *ite, *tmp;
1620
1621 list_for_each_entry_safe(ite, tmp, &dev->itt_head, ite_list)
1622 its_free_ite(kvm, ite);
1623 list_del(&dev->dev_list);
1624 kfree(dev);
1625}
1626
Andre Przywara1085fdc2016-07-15 12:43:31 +01001627static void vgic_its_destroy(struct kvm_device *kvm_dev)
1628{
Andre Przywara424c3382016-07-15 12:43:32 +01001629 struct kvm *kvm = kvm_dev->kvm;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001630 struct vgic_its *its = kvm_dev->private;
Andre Przywara424c3382016-07-15 12:43:32 +01001631 struct list_head *cur, *temp;
1632
1633 /*
1634 * We may end up here without the lists ever having been initialized.
1635 * Check this and bail out early to avoid dereferencing a NULL pointer.
1636 */
1637 if (!its->device_list.next)
1638 return;
1639
1640 mutex_lock(&its->its_lock);
Christoffer Dalla2b19e62017-05-08 13:31:12 +02001641 list_for_each_safe(cur, temp, &its->device_list) {
1642 struct its_device *dev;
1643
1644 dev = list_entry(cur, struct its_device, dev_list);
1645 vgic_its_free_device(kvm, dev);
Andre Przywara424c3382016-07-15 12:43:32 +01001646 }
1647
1648 list_for_each_safe(cur, temp, &its->collection_list) {
Christoffer Dalla2b19e62017-05-08 13:31:12 +02001649 struct its_collection *coll;
1650
1651 coll = list_entry(cur, struct its_collection, coll_list);
Andre Przywara424c3382016-07-15 12:43:32 +01001652 list_del(cur);
Christoffer Dalla2b19e62017-05-08 13:31:12 +02001653 kfree(coll);
Andre Przywara424c3382016-07-15 12:43:32 +01001654 }
1655 mutex_unlock(&its->its_lock);
Andre Przywara1085fdc2016-07-15 12:43:31 +01001656
1657 kfree(its);
1658}
1659
Eric Auger876ae232016-12-20 01:36:35 -05001660int vgic_its_has_attr_regs(struct kvm_device *dev,
1661 struct kvm_device_attr *attr)
1662{
Eric Auger8331c232016-12-20 09:33:13 +01001663 const struct vgic_register_region *region;
1664 gpa_t offset = attr->attr;
1665 int align;
1666
1667 align = (offset < GITS_TYPER) || (offset >= GITS_PIDR4) ? 0x3 : 0x7;
1668
1669 if (offset & align)
1670 return -EINVAL;
1671
1672 region = vgic_find_mmio_region(its_registers,
1673 ARRAY_SIZE(its_registers),
1674 offset);
1675 if (!region)
1676 return -ENXIO;
1677
1678 return 0;
Eric Auger876ae232016-12-20 01:36:35 -05001679}
1680
1681int vgic_its_attr_regs_access(struct kvm_device *dev,
1682 struct kvm_device_attr *attr,
1683 u64 *reg, bool is_write)
1684{
Eric Auger8331c232016-12-20 09:33:13 +01001685 const struct vgic_register_region *region;
1686 struct vgic_its *its;
1687 gpa_t addr, offset;
1688 unsigned int len;
1689 int align, ret = 0;
1690
1691 its = dev->private;
1692 offset = attr->attr;
1693
1694 /*
1695 * Although the spec supports upper/lower 32-bit accesses to
1696 * 64-bit ITS registers, the userspace ABI requires 64-bit
1697 * accesses to all 64-bit wide registers. We therefore only
1698 * support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID
1699 * registers
1700 */
1701 if ((offset < GITS_TYPER) || (offset >= GITS_PIDR4))
1702 align = 0x3;
1703 else
1704 align = 0x7;
1705
1706 if (offset & align)
1707 return -EINVAL;
1708
1709 mutex_lock(&dev->kvm->lock);
1710
1711 if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) {
1712 ret = -ENXIO;
1713 goto out;
1714 }
1715
1716 region = vgic_find_mmio_region(its_registers,
1717 ARRAY_SIZE(its_registers),
1718 offset);
1719 if (!region) {
1720 ret = -ENXIO;
1721 goto out;
1722 }
1723
1724 if (!lock_all_vcpus(dev->kvm)) {
1725 ret = -EBUSY;
1726 goto out;
1727 }
1728
1729 addr = its->vgic_its_base + offset;
1730
1731 len = region->access_flags & VGIC_ACCESS_64bit ? 8 : 4;
1732
1733 if (is_write) {
1734 if (region->uaccess_its_write)
1735 ret = region->uaccess_its_write(dev->kvm, its, addr,
1736 len, *reg);
1737 else
1738 region->its_write(dev->kvm, its, addr, len, *reg);
1739 } else {
1740 *reg = region->its_read(dev->kvm, its, addr, len);
1741 }
1742 unlock_all_vcpus(dev->kvm);
1743out:
1744 mutex_unlock(&dev->kvm->lock);
1745 return ret;
Eric Auger876ae232016-12-20 01:36:35 -05001746}
1747
Eric Auger57a9a112017-01-09 16:27:07 +01001748static u32 compute_next_devid_offset(struct list_head *h,
1749 struct its_device *dev)
Eric Auger920a7a82017-02-08 05:20:04 +01001750{
1751 struct its_device *next;
1752 u32 next_offset;
1753
1754 if (list_is_last(&dev->dev_list, h))
1755 return 0;
1756 next = list_next_entry(dev, dev_list);
1757 next_offset = next->device_id - dev->device_id;
1758
1759 return min_t(u32, next_offset, VITS_DTE_MAX_DEVID_OFFSET);
1760}
1761
Eric Augereff484e2017-05-03 17:38:01 +02001762static u32 compute_next_eventid_offset(struct list_head *h, struct its_ite *ite)
Eric Auger920a7a82017-02-08 05:20:04 +01001763{
1764 struct its_ite *next;
1765 u32 next_offset;
1766
1767 if (list_is_last(&ite->ite_list, h))
1768 return 0;
1769 next = list_next_entry(ite, ite_list);
1770 next_offset = next->event_id - ite->event_id;
1771
1772 return min_t(u32, next_offset, VITS_ITE_MAX_EVENTID_OFFSET);
1773}
1774
1775/**
1776 * entry_fn_t - Callback called on a table entry restore path
1777 * @its: its handle
1778 * @id: id of the entry
1779 * @entry: pointer to the entry
1780 * @opaque: pointer to an opaque data
1781 *
1782 * Return: < 0 on error, 0 if last element was identified, id offset to next
1783 * element otherwise
1784 */
1785typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry,
1786 void *opaque);
1787
1788/**
1789 * scan_its_table - Scan a contiguous table in guest RAM and applies a function
1790 * to each entry
1791 *
1792 * @its: its handle
1793 * @base: base gpa of the table
1794 * @size: size of the table in bytes
1795 * @esz: entry size in bytes
1796 * @start_id: the ID of the first entry in the table
1797 * (non zero for 2d level tables)
1798 * @fn: function to apply on each entry
1799 *
1800 * Return: < 0 on error, 0 if last element was identified, 1 otherwise
1801 * (the last element may not be found on second level tables)
1802 */
Eric Auger57a9a112017-01-09 16:27:07 +01001803static int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz,
1804 int start_id, entry_fn_t fn, void *opaque)
Eric Auger920a7a82017-02-08 05:20:04 +01001805{
1806 void *entry = kzalloc(esz, GFP_KERNEL);
1807 struct kvm *kvm = its->dev->kvm;
1808 unsigned long len = size;
1809 int id = start_id;
1810 gpa_t gpa = base;
1811 int ret;
1812
1813 while (len > 0) {
1814 int next_offset;
1815 size_t byte_offset;
1816
1817 ret = kvm_read_guest(kvm, gpa, entry, esz);
1818 if (ret)
1819 goto out;
1820
1821 next_offset = fn(its, id, entry, opaque);
1822 if (next_offset <= 0) {
1823 ret = next_offset;
1824 goto out;
1825 }
1826
1827 byte_offset = next_offset * esz;
1828 id += next_offset;
1829 gpa += byte_offset;
1830 len -= byte_offset;
1831 }
1832 ret = 1;
1833
1834out:
1835 kfree(entry);
1836 return ret;
1837}
1838
Eric Augereff484e2017-05-03 17:38:01 +02001839/**
1840 * vgic_its_save_ite - Save an interrupt translation entry at @gpa
1841 */
1842static int vgic_its_save_ite(struct vgic_its *its, struct its_device *dev,
1843 struct its_ite *ite, gpa_t gpa, int ite_esz)
1844{
1845 struct kvm *kvm = its->dev->kvm;
1846 u32 next_offset;
1847 u64 val;
1848
1849 next_offset = compute_next_eventid_offset(&dev->itt_head, ite);
1850 val = ((u64)next_offset << KVM_ITS_ITE_NEXT_SHIFT) |
1851 ((u64)ite->lpi << KVM_ITS_ITE_PINTID_SHIFT) |
1852 ite->collection->collection_id;
1853 val = cpu_to_le64(val);
1854 return kvm_write_guest(kvm, gpa, &val, ite_esz);
1855}
1856
1857/**
1858 * vgic_its_restore_ite - restore an interrupt translation entry
1859 * @event_id: id used for indexing
1860 * @ptr: pointer to the ITE entry
1861 * @opaque: pointer to the its_device
1862 */
1863static int vgic_its_restore_ite(struct vgic_its *its, u32 event_id,
1864 void *ptr, void *opaque)
1865{
1866 struct its_device *dev = (struct its_device *)opaque;
1867 struct its_collection *collection;
1868 struct kvm *kvm = its->dev->kvm;
1869 struct kvm_vcpu *vcpu = NULL;
1870 u64 val;
1871 u64 *p = (u64 *)ptr;
1872 struct vgic_irq *irq;
1873 u32 coll_id, lpi_id;
1874 struct its_ite *ite;
1875 u32 offset;
1876
1877 val = *p;
1878
1879 val = le64_to_cpu(val);
1880
1881 coll_id = val & KVM_ITS_ITE_ICID_MASK;
1882 lpi_id = (val & KVM_ITS_ITE_PINTID_MASK) >> KVM_ITS_ITE_PINTID_SHIFT;
1883
1884 if (!lpi_id)
1885 return 1; /* invalid entry, no choice but to scan next entry */
1886
1887 if (lpi_id < VGIC_MIN_LPI)
1888 return -EINVAL;
1889
1890 offset = val >> KVM_ITS_ITE_NEXT_SHIFT;
1891 if (event_id + offset >= BIT_ULL(dev->num_eventid_bits))
1892 return -EINVAL;
1893
1894 collection = find_collection(its, coll_id);
1895 if (!collection)
1896 return -EINVAL;
1897
1898 ite = vgic_its_alloc_ite(dev, collection, lpi_id, event_id);
1899 if (IS_ERR(ite))
1900 return PTR_ERR(ite);
1901
1902 if (its_is_collection_mapped(collection))
1903 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
1904
1905 irq = vgic_add_lpi(kvm, lpi_id, vcpu);
1906 if (IS_ERR(irq))
1907 return PTR_ERR(irq);
1908 ite->irq = irq;
1909
1910 return offset;
1911}
1912
1913static int vgic_its_ite_cmp(void *priv, struct list_head *a,
1914 struct list_head *b)
1915{
1916 struct its_ite *itea = container_of(a, struct its_ite, ite_list);
1917 struct its_ite *iteb = container_of(b, struct its_ite, ite_list);
1918
1919 if (itea->event_id < iteb->event_id)
1920 return -1;
1921 else
1922 return 1;
1923}
1924
Eric Auger57a9a112017-01-09 16:27:07 +01001925static int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
1926{
Eric Augereff484e2017-05-03 17:38:01 +02001927 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
1928 gpa_t base = device->itt_addr;
1929 struct its_ite *ite;
1930 int ret;
1931 int ite_esz = abi->ite_esz;
1932
1933 list_sort(NULL, &device->itt_head, vgic_its_ite_cmp);
1934
1935 list_for_each_entry(ite, &device->itt_head, ite_list) {
1936 gpa_t gpa = base + ite->event_id * ite_esz;
1937
1938 ret = vgic_its_save_ite(its, device, ite, gpa, ite_esz);
1939 if (ret)
1940 return ret;
1941 }
1942 return 0;
Eric Auger57a9a112017-01-09 16:27:07 +01001943}
1944
1945static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
1946{
Eric Augereff484e2017-05-03 17:38:01 +02001947 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
1948 gpa_t base = dev->itt_addr;
1949 int ret;
1950 int ite_esz = abi->ite_esz;
1951 size_t max_size = BIT_ULL(dev->num_eventid_bits) * ite_esz;
1952
1953 ret = scan_its_table(its, base, max_size, ite_esz, 0,
1954 vgic_its_restore_ite, dev);
1955
1956 return ret;
Eric Auger57a9a112017-01-09 16:27:07 +01001957}
1958
1959/**
1960 * vgic_its_save_dte - Save a device table entry at a given GPA
1961 *
1962 * @its: ITS handle
1963 * @dev: ITS device
1964 * @ptr: GPA
1965 */
1966static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev,
1967 gpa_t ptr, int dte_esz)
1968{
1969 struct kvm *kvm = its->dev->kvm;
1970 u64 val, itt_addr_field;
1971 u32 next_offset;
1972
1973 itt_addr_field = dev->itt_addr >> 8;
1974 next_offset = compute_next_devid_offset(&its->device_list, dev);
1975 val = (1ULL << KVM_ITS_DTE_VALID_SHIFT |
1976 ((u64)next_offset << KVM_ITS_DTE_NEXT_SHIFT) |
1977 (itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) |
1978 (dev->num_eventid_bits - 1));
1979 val = cpu_to_le64(val);
1980 return kvm_write_guest(kvm, ptr, &val, dte_esz);
1981}
1982
1983/**
1984 * vgic_its_restore_dte - restore a device table entry
1985 *
1986 * @its: its handle
1987 * @id: device id the DTE corresponds to
1988 * @ptr: kernel VA where the 8 byte DTE is located
1989 * @opaque: unused
1990 *
1991 * Return: < 0 on error, 0 if the dte is the last one, id offset to the
1992 * next dte otherwise
1993 */
1994static int vgic_its_restore_dte(struct vgic_its *its, u32 id,
1995 void *ptr, void *opaque)
1996{
1997 struct its_device *dev;
1998 gpa_t itt_addr;
1999 u8 num_eventid_bits;
2000 u64 entry = *(u64 *)ptr;
2001 bool valid;
2002 u32 offset;
2003 int ret;
2004
2005 entry = le64_to_cpu(entry);
2006
2007 valid = entry >> KVM_ITS_DTE_VALID_SHIFT;
2008 num_eventid_bits = (entry & KVM_ITS_DTE_SIZE_MASK) + 1;
2009 itt_addr = ((entry & KVM_ITS_DTE_ITTADDR_MASK)
2010 >> KVM_ITS_DTE_ITTADDR_SHIFT) << 8;
2011
2012 if (!valid)
2013 return 1;
2014
2015 /* dte entry is valid */
2016 offset = (entry & KVM_ITS_DTE_NEXT_MASK) >> KVM_ITS_DTE_NEXT_SHIFT;
2017
2018 dev = vgic_its_alloc_device(its, id, itt_addr, num_eventid_bits);
2019 if (IS_ERR(dev))
2020 return PTR_ERR(dev);
2021
2022 ret = vgic_its_restore_itt(its, dev);
Christoffer Dalla2b19e62017-05-08 13:31:12 +02002023 if (ret) {
2024 vgic_its_free_device(its->dev->kvm, dev);
Eric Auger57a9a112017-01-09 16:27:07 +01002025 return ret;
Christoffer Dalla2b19e62017-05-08 13:31:12 +02002026 }
Eric Auger57a9a112017-01-09 16:27:07 +01002027
2028 return offset;
2029}
2030
2031static int vgic_its_device_cmp(void *priv, struct list_head *a,
2032 struct list_head *b)
2033{
2034 struct its_device *deva = container_of(a, struct its_device, dev_list);
2035 struct its_device *devb = container_of(b, struct its_device, dev_list);
2036
2037 if (deva->device_id < devb->device_id)
2038 return -1;
2039 else
2040 return 1;
2041}
2042
Eric Auger71afe472017-04-13 09:06:20 +02002043/**
Eric Auger3b658082016-12-24 18:48:04 +01002044 * vgic_its_save_device_tables - Save the device table and all ITT
2045 * into guest RAM
Eric Auger57a9a112017-01-09 16:27:07 +01002046 *
2047 * L1/L2 handling is hidden by vgic_its_check_id() helper which directly
2048 * returns the GPA of the device entry
Eric Auger3b658082016-12-24 18:48:04 +01002049 */
2050static int vgic_its_save_device_tables(struct vgic_its *its)
2051{
Eric Auger57a9a112017-01-09 16:27:07 +01002052 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2053 struct its_device *dev;
2054 int dte_esz = abi->dte_esz;
2055 u64 baser;
2056
2057 baser = its->baser_device_table;
2058
2059 list_sort(NULL, &its->device_list, vgic_its_device_cmp);
2060
2061 list_for_each_entry(dev, &its->device_list, dev_list) {
2062 int ret;
2063 gpa_t eaddr;
2064
2065 if (!vgic_its_check_id(its, baser,
2066 dev->device_id, &eaddr))
2067 return -EINVAL;
2068
2069 ret = vgic_its_save_itt(its, dev);
2070 if (ret)
2071 return ret;
2072
2073 ret = vgic_its_save_dte(its, dev, eaddr, dte_esz);
2074 if (ret)
2075 return ret;
2076 }
2077 return 0;
2078}
2079
2080/**
2081 * handle_l1_dte - callback used for L1 device table entries (2 stage case)
2082 *
2083 * @its: its handle
2084 * @id: index of the entry in the L1 table
2085 * @addr: kernel VA
2086 * @opaque: unused
2087 *
2088 * L1 table entries are scanned by steps of 1 entry
2089 * Return < 0 if error, 0 if last dte was found when scanning the L2
2090 * table, +1 otherwise (meaning next L1 entry must be scanned)
2091 */
2092static int handle_l1_dte(struct vgic_its *its, u32 id, void *addr,
2093 void *opaque)
2094{
2095 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2096 int l2_start_id = id * (SZ_64K / abi->dte_esz);
2097 u64 entry = *(u64 *)addr;
2098 int dte_esz = abi->dte_esz;
2099 gpa_t gpa;
2100 int ret;
2101
2102 entry = le64_to_cpu(entry);
2103
2104 if (!(entry & KVM_ITS_L1E_VALID_MASK))
2105 return 1;
2106
2107 gpa = entry & KVM_ITS_L1E_ADDR_MASK;
2108
2109 ret = scan_its_table(its, gpa, SZ_64K, dte_esz,
2110 l2_start_id, vgic_its_restore_dte, NULL);
2111
2112 if (ret <= 0)
2113 return ret;
2114
2115 return 1;
Eric Auger3b658082016-12-24 18:48:04 +01002116}
2117
2118/**
2119 * vgic_its_restore_device_tables - Restore the device table and all ITT
2120 * from guest RAM to internal data structs
2121 */
2122static int vgic_its_restore_device_tables(struct vgic_its *its)
2123{
Eric Auger57a9a112017-01-09 16:27:07 +01002124 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2125 u64 baser = its->baser_device_table;
2126 int l1_esz, ret;
2127 int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
2128 gpa_t l1_gpa;
2129
2130 if (!(baser & GITS_BASER_VALID))
2131 return 0;
2132
2133 l1_gpa = BASER_ADDRESS(baser);
2134
2135 if (baser & GITS_BASER_INDIRECT) {
2136 l1_esz = GITS_LVL1_ENTRY_SIZE;
2137 ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
2138 handle_l1_dte, NULL);
2139 } else {
2140 l1_esz = abi->dte_esz;
2141 ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
2142 vgic_its_restore_dte, NULL);
2143 }
2144
2145 if (ret > 0)
2146 ret = -EINVAL;
2147
2148 return ret;
Eric Auger3b658082016-12-24 18:48:04 +01002149}
2150
Eric Augerea1ad532017-01-09 16:19:41 +01002151static int vgic_its_save_cte(struct vgic_its *its,
2152 struct its_collection *collection,
2153 gpa_t gpa, int esz)
2154{
2155 u64 val;
2156
2157 val = (1ULL << KVM_ITS_CTE_VALID_SHIFT |
2158 ((u64)collection->target_addr << KVM_ITS_CTE_RDBASE_SHIFT) |
2159 collection->collection_id);
2160 val = cpu_to_le64(val);
2161 return kvm_write_guest(its->dev->kvm, gpa, &val, esz);
2162}
2163
2164static int vgic_its_restore_cte(struct vgic_its *its, gpa_t gpa, int esz)
2165{
2166 struct its_collection *collection;
2167 struct kvm *kvm = its->dev->kvm;
2168 u32 target_addr, coll_id;
2169 u64 val;
2170 int ret;
2171
2172 BUG_ON(esz > sizeof(val));
2173 ret = kvm_read_guest(kvm, gpa, &val, esz);
2174 if (ret)
2175 return ret;
2176 val = le64_to_cpu(val);
2177 if (!(val & KVM_ITS_CTE_VALID_MASK))
2178 return 0;
2179
2180 target_addr = (u32)(val >> KVM_ITS_CTE_RDBASE_SHIFT);
2181 coll_id = val & KVM_ITS_CTE_ICID_MASK;
2182
2183 if (target_addr >= atomic_read(&kvm->online_vcpus))
2184 return -EINVAL;
2185
2186 collection = find_collection(its, coll_id);
2187 if (collection)
2188 return -EEXIST;
2189 ret = vgic_its_alloc_collection(its, &collection, coll_id);
2190 if (ret)
2191 return ret;
2192 collection->target_addr = target_addr;
2193 return 1;
2194}
2195
Eric Auger3b658082016-12-24 18:48:04 +01002196/**
2197 * vgic_its_save_collection_table - Save the collection table into
2198 * guest RAM
2199 */
2200static int vgic_its_save_collection_table(struct vgic_its *its)
2201{
Eric Augerea1ad532017-01-09 16:19:41 +01002202 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2203 struct its_collection *collection;
2204 u64 val;
2205 gpa_t gpa;
2206 size_t max_size, filled = 0;
2207 int ret, cte_esz = abi->cte_esz;
2208
2209 gpa = BASER_ADDRESS(its->baser_coll_table);
2210 if (!gpa)
2211 return 0;
2212
2213 max_size = GITS_BASER_NR_PAGES(its->baser_coll_table) * SZ_64K;
2214
2215 list_for_each_entry(collection, &its->collection_list, coll_list) {
2216 ret = vgic_its_save_cte(its, collection, gpa, cte_esz);
2217 if (ret)
2218 return ret;
2219 gpa += cte_esz;
2220 filled += cte_esz;
2221 }
2222
2223 if (filled == max_size)
2224 return 0;
2225
2226 /*
2227 * table is not fully filled, add a last dummy element
2228 * with valid bit unset
2229 */
2230 val = 0;
2231 BUG_ON(cte_esz > sizeof(val));
2232 ret = kvm_write_guest(its->dev->kvm, gpa, &val, cte_esz);
2233 return ret;
Eric Auger3b658082016-12-24 18:48:04 +01002234}
2235
2236/**
2237 * vgic_its_restore_collection_table - reads the collection table
2238 * in guest memory and restores the ITS internal state. Requires the
2239 * BASER registers to be restored before.
2240 */
2241static int vgic_its_restore_collection_table(struct vgic_its *its)
2242{
Eric Augerea1ad532017-01-09 16:19:41 +01002243 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2244 int cte_esz = abi->cte_esz;
2245 size_t max_size, read = 0;
2246 gpa_t gpa;
2247 int ret;
2248
2249 if (!(its->baser_coll_table & GITS_BASER_VALID))
2250 return 0;
2251
2252 gpa = BASER_ADDRESS(its->baser_coll_table);
2253
2254 max_size = GITS_BASER_NR_PAGES(its->baser_coll_table) * SZ_64K;
2255
2256 while (read < max_size) {
2257 ret = vgic_its_restore_cte(its, gpa, cte_esz);
2258 if (ret <= 0)
2259 break;
2260 gpa += cte_esz;
2261 read += cte_esz;
2262 }
2263 return ret;
Eric Auger3b658082016-12-24 18:48:04 +01002264}
2265
2266/**
Eric Auger71afe472017-04-13 09:06:20 +02002267 * vgic_its_save_tables_v0 - Save the ITS tables into guest ARM
2268 * according to v0 ABI
2269 */
2270static int vgic_its_save_tables_v0(struct vgic_its *its)
2271{
Eric Auger3b658082016-12-24 18:48:04 +01002272 struct kvm *kvm = its->dev->kvm;
2273 int ret;
2274
2275 mutex_lock(&kvm->lock);
2276 mutex_lock(&its->its_lock);
2277
2278 if (!lock_all_vcpus(kvm)) {
2279 mutex_unlock(&its->its_lock);
2280 mutex_unlock(&kvm->lock);
2281 return -EBUSY;
2282 }
2283
2284 ret = vgic_its_save_device_tables(its);
2285 if (ret)
2286 goto out;
2287
2288 ret = vgic_its_save_collection_table(its);
2289
2290out:
2291 unlock_all_vcpus(kvm);
2292 mutex_unlock(&its->its_lock);
2293 mutex_unlock(&kvm->lock);
2294 return ret;
Eric Auger71afe472017-04-13 09:06:20 +02002295}
2296
2297/**
2298 * vgic_its_restore_tables_v0 - Restore the ITS tables from guest RAM
2299 * to internal data structs according to V0 ABI
2300 *
2301 */
2302static int vgic_its_restore_tables_v0(struct vgic_its *its)
2303{
Eric Auger3b658082016-12-24 18:48:04 +01002304 struct kvm *kvm = its->dev->kvm;
2305 int ret;
2306
2307 mutex_lock(&kvm->lock);
2308 mutex_lock(&its->its_lock);
2309
2310 if (!lock_all_vcpus(kvm)) {
2311 mutex_unlock(&its->its_lock);
2312 mutex_unlock(&kvm->lock);
2313 return -EBUSY;
2314 }
2315
2316 ret = vgic_its_restore_collection_table(its);
2317 if (ret)
2318 goto out;
2319
2320 ret = vgic_its_restore_device_tables(its);
Eric Auger3b658082016-12-24 18:48:04 +01002321out:
2322 unlock_all_vcpus(kvm);
2323 mutex_unlock(&its->its_lock);
2324 mutex_unlock(&kvm->lock);
2325
Christoffer Dall67723c22017-05-08 14:43:45 +02002326 return ret;
Eric Auger71afe472017-04-13 09:06:20 +02002327}
2328
2329static int vgic_its_commit_v0(struct vgic_its *its)
2330{
2331 const struct vgic_its_abi *abi;
2332
2333 abi = vgic_its_get_abi(its);
2334 its->baser_coll_table &= ~GITS_BASER_ENTRY_SIZE_MASK;
2335 its->baser_device_table &= ~GITS_BASER_ENTRY_SIZE_MASK;
2336
2337 its->baser_coll_table |= (GIC_ENCODE_SZ(abi->cte_esz, 5)
2338 << GITS_BASER_ENTRY_SIZE_SHIFT);
2339
2340 its->baser_device_table |= (GIC_ENCODE_SZ(abi->dte_esz, 5)
2341 << GITS_BASER_ENTRY_SIZE_SHIFT);
2342 return 0;
2343}
2344
Andre Przywara1085fdc2016-07-15 12:43:31 +01002345static int vgic_its_has_attr(struct kvm_device *dev,
2346 struct kvm_device_attr *attr)
2347{
2348 switch (attr->group) {
2349 case KVM_DEV_ARM_VGIC_GRP_ADDR:
2350 switch (attr->attr) {
2351 case KVM_VGIC_ITS_ADDR_TYPE:
2352 return 0;
2353 }
2354 break;
2355 case KVM_DEV_ARM_VGIC_GRP_CTRL:
2356 switch (attr->attr) {
2357 case KVM_DEV_ARM_VGIC_CTRL_INIT:
2358 return 0;
Eric Auger3b658082016-12-24 18:48:04 +01002359 case KVM_DEV_ARM_ITS_SAVE_TABLES:
2360 return 0;
2361 case KVM_DEV_ARM_ITS_RESTORE_TABLES:
2362 return 0;
Andre Przywara1085fdc2016-07-15 12:43:31 +01002363 }
2364 break;
Eric Auger876ae232016-12-20 01:36:35 -05002365 case KVM_DEV_ARM_VGIC_GRP_ITS_REGS:
2366 return vgic_its_has_attr_regs(dev, attr);
Andre Przywara1085fdc2016-07-15 12:43:31 +01002367 }
2368 return -ENXIO;
2369}
2370
2371static int vgic_its_set_attr(struct kvm_device *dev,
2372 struct kvm_device_attr *attr)
2373{
2374 struct vgic_its *its = dev->private;
2375 int ret;
2376
2377 switch (attr->group) {
2378 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2379 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2380 unsigned long type = (unsigned long)attr->attr;
2381 u64 addr;
2382
2383 if (type != KVM_VGIC_ITS_ADDR_TYPE)
2384 return -ENODEV;
2385
Andre Przywara1085fdc2016-07-15 12:43:31 +01002386 if (copy_from_user(&addr, uaddr, sizeof(addr)))
2387 return -EFAULT;
2388
2389 ret = vgic_check_ioaddr(dev->kvm, &its->vgic_its_base,
2390 addr, SZ_64K);
2391 if (ret)
2392 return ret;
2393
Christoffer Dall30e1b682017-05-08 13:14:57 +02002394 return vgic_register_its_iodev(dev->kvm, its, addr);
Andre Przywara1085fdc2016-07-15 12:43:31 +01002395 }
Eric Auger3b658082016-12-24 18:48:04 +01002396 case KVM_DEV_ARM_VGIC_GRP_CTRL: {
2397 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2398
Andre Przywara1085fdc2016-07-15 12:43:31 +01002399 switch (attr->attr) {
2400 case KVM_DEV_ARM_VGIC_CTRL_INIT:
Marc Zyngier6cc40f22017-05-08 18:15:40 +01002401 /* Nothing to do */
Andre Przywarac7735762016-08-08 16:45:43 +01002402 return 0;
Eric Auger3b658082016-12-24 18:48:04 +01002403 case KVM_DEV_ARM_ITS_SAVE_TABLES:
2404 return abi->save_tables(its);
2405 case KVM_DEV_ARM_ITS_RESTORE_TABLES:
2406 return abi->restore_tables(its);
Andre Przywara1085fdc2016-07-15 12:43:31 +01002407 }
Eric Auger3b658082016-12-24 18:48:04 +01002408 }
Eric Auger876ae232016-12-20 01:36:35 -05002409 case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: {
2410 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2411 u64 reg;
2412
2413 if (get_user(reg, uaddr))
2414 return -EFAULT;
2415
2416 return vgic_its_attr_regs_access(dev, attr, &reg, true);
2417 }
Andre Przywara1085fdc2016-07-15 12:43:31 +01002418 }
2419 return -ENXIO;
2420}
2421
2422static int vgic_its_get_attr(struct kvm_device *dev,
2423 struct kvm_device_attr *attr)
2424{
2425 switch (attr->group) {
2426 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2427 struct vgic_its *its = dev->private;
2428 u64 addr = its->vgic_its_base;
2429 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2430 unsigned long type = (unsigned long)attr->attr;
2431
2432 if (type != KVM_VGIC_ITS_ADDR_TYPE)
2433 return -ENODEV;
2434
2435 if (copy_to_user(uaddr, &addr, sizeof(addr)))
2436 return -EFAULT;
2437 break;
Eric Auger876ae232016-12-20 01:36:35 -05002438 }
2439 case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: {
2440 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2441 u64 reg;
2442 int ret;
2443
2444 ret = vgic_its_attr_regs_access(dev, attr, &reg, false);
2445 if (ret)
2446 return ret;
2447 return put_user(reg, uaddr);
2448 }
Andre Przywara1085fdc2016-07-15 12:43:31 +01002449 default:
2450 return -ENXIO;
2451 }
Andre Przywara1085fdc2016-07-15 12:43:31 +01002452
2453 return 0;
2454}
2455
2456static struct kvm_device_ops kvm_arm_vgic_its_ops = {
2457 .name = "kvm-arm-vgic-its",
2458 .create = vgic_its_create,
2459 .destroy = vgic_its_destroy,
2460 .set_attr = vgic_its_set_attr,
2461 .get_attr = vgic_its_get_attr,
2462 .has_attr = vgic_its_has_attr,
2463};
2464
2465int kvm_vgic_register_its_device(void)
2466{
2467 return kvm_register_device_ops(&kvm_arm_vgic_its_ops,
2468 KVM_DEV_TYPE_ARM_VGIC_ITS);
2469}