blob: 6af22fe9e08239e413e60fb31b72611ee037a56c [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
Andre Przywaradf9f58f2016-07-15 12:43:36 +010036/*
37 * Creates a new (reference to a) struct vgic_irq for a given LPI.
38 * If this LPI is already mapped on another ITS, we increase its refcount
39 * and return a pointer to the existing structure.
40 * If this is a "new" LPI, we allocate and initialize a new struct vgic_irq.
41 * This function returns a pointer to the _unlocked_ structure.
42 */
43static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid)
44{
45 struct vgic_dist *dist = &kvm->arch.vgic;
46 struct vgic_irq *irq = vgic_get_irq(kvm, NULL, intid), *oldirq;
47
48 /* In this case there is no put, since we keep the reference. */
49 if (irq)
50 return irq;
51
52 irq = kzalloc(sizeof(struct vgic_irq), GFP_KERNEL);
53 if (!irq)
Christoffer Dall99e5e882016-08-01 20:25:33 +020054 return ERR_PTR(-ENOMEM);
Andre Przywaradf9f58f2016-07-15 12:43:36 +010055
56 INIT_LIST_HEAD(&irq->lpi_list);
57 INIT_LIST_HEAD(&irq->ap_list);
58 spin_lock_init(&irq->irq_lock);
59
60 irq->config = VGIC_CONFIG_EDGE;
61 kref_init(&irq->refcount);
62 irq->intid = intid;
63
64 spin_lock(&dist->lpi_list_lock);
65
66 /*
67 * There could be a race with another vgic_add_lpi(), so we need to
68 * check that we don't add a second list entry with the same LPI.
69 */
70 list_for_each_entry(oldirq, &dist->lpi_list_head, lpi_list) {
71 if (oldirq->intid != intid)
72 continue;
73
74 /* Someone was faster with adding this LPI, lets use that. */
75 kfree(irq);
76 irq = oldirq;
77
78 /*
79 * This increases the refcount, the caller is expected to
80 * call vgic_put_irq() on the returned pointer once it's
81 * finished with the IRQ.
82 */
Marc Zyngierd97594e2016-07-17 11:27:23 +010083 vgic_get_irq_kref(irq);
Andre Przywaradf9f58f2016-07-15 12:43:36 +010084
85 goto out_unlock;
86 }
87
88 list_add_tail(&irq->lpi_list, &dist->lpi_list_head);
89 dist->lpi_list_count++;
90
91out_unlock:
92 spin_unlock(&dist->lpi_list_lock);
93
94 return irq;
95}
96
Andre Przywara424c3382016-07-15 12:43:32 +010097struct its_device {
98 struct list_head dev_list;
99
100 /* the head for the list of ITTEs */
101 struct list_head itt_head;
102 u32 device_id;
103};
104
105#define COLLECTION_NOT_MAPPED ((u32)~0)
106
107struct its_collection {
108 struct list_head coll_list;
109
110 u32 collection_id;
111 u32 target_addr;
112};
113
114#define its_is_collection_mapped(coll) ((coll) && \
115 ((coll)->target_addr != COLLECTION_NOT_MAPPED))
116
117struct its_itte {
118 struct list_head itte_list;
119
Andre Przywara38024112016-07-15 12:43:33 +0100120 struct vgic_irq *irq;
Andre Przywara424c3382016-07-15 12:43:32 +0100121 struct its_collection *collection;
122 u32 lpi;
123 u32 event_id;
124};
125
126/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100127 * Find and returns a device in the device table for an ITS.
128 * Must be called with the its_lock mutex held.
129 */
130static struct its_device *find_its_device(struct vgic_its *its, u32 device_id)
131{
132 struct its_device *device;
133
134 list_for_each_entry(device, &its->device_list, dev_list)
135 if (device_id == device->device_id)
136 return device;
137
138 return NULL;
139}
140
141/*
142 * Find and returns an interrupt translation table entry (ITTE) for a given
143 * Device ID/Event ID pair on an ITS.
144 * Must be called with the its_lock mutex held.
145 */
146static struct its_itte *find_itte(struct vgic_its *its, u32 device_id,
147 u32 event_id)
148{
149 struct its_device *device;
150 struct its_itte *itte;
151
152 device = find_its_device(its, device_id);
153 if (device == NULL)
154 return NULL;
155
156 list_for_each_entry(itte, &device->itt_head, itte_list)
157 if (itte->event_id == event_id)
158 return itte;
159
160 return NULL;
161}
162
163/* To be used as an iterator this macro misses the enclosing parentheses */
164#define for_each_lpi_its(dev, itte, its) \
165 list_for_each_entry(dev, &(its)->device_list, dev_list) \
166 list_for_each_entry(itte, &(dev)->itt_head, itte_list)
167
168/*
Andre Przywara424c3382016-07-15 12:43:32 +0100169 * We only implement 48 bits of PA at the moment, although the ITS
170 * supports more. Let's be restrictive here.
171 */
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100172#define BASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16))
Andre Przywara424c3382016-07-15 12:43:32 +0100173#define CBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12))
Andre Przywara33d3bc92016-07-15 12:43:34 +0100174#define PENDBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16))
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100175#define PROPBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12))
176
177#define GIC_LPI_OFFSET 8192
178
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100179/*
180 * Finds and returns a collection in the ITS collection table.
181 * Must be called with the its_lock mutex held.
182 */
183static struct its_collection *find_collection(struct vgic_its *its, int coll_id)
184{
185 struct its_collection *collection;
186
187 list_for_each_entry(collection, &its->collection_list, coll_list) {
188 if (coll_id == collection->collection_id)
189 return collection;
190 }
191
192 return NULL;
193}
194
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100195#define LPI_PROP_ENABLE_BIT(p) ((p) & LPI_PROP_ENABLED)
196#define LPI_PROP_PRIORITY(p) ((p) & 0xfc)
197
198/*
199 * Reads the configuration data for a given LPI from guest memory and
200 * updates the fields in struct vgic_irq.
201 * If filter_vcpu is not NULL, applies only if the IRQ is targeting this
202 * VCPU. Unconditionally applies if filter_vcpu is NULL.
203 */
204static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
205 struct kvm_vcpu *filter_vcpu)
206{
207 u64 propbase = PROPBASER_ADDRESS(kvm->arch.vgic.propbaser);
208 u8 prop;
209 int ret;
210
211 ret = kvm_read_guest(kvm, propbase + irq->intid - GIC_LPI_OFFSET,
212 &prop, 1);
213
214 if (ret)
215 return ret;
216
217 spin_lock(&irq->irq_lock);
218
219 if (!filter_vcpu || filter_vcpu == irq->target_vcpu) {
220 irq->priority = LPI_PROP_PRIORITY(prop);
221 irq->enabled = LPI_PROP_ENABLE_BIT(prop);
222
223 vgic_queue_irq_unlock(kvm, irq);
224 } else {
225 spin_unlock(&irq->irq_lock);
226 }
227
228 return 0;
229}
Andre Przywara33d3bc92016-07-15 12:43:34 +0100230
231/*
232 * Create a snapshot of the current LPI list, so that we can enumerate all
233 * LPIs without holding any lock.
234 * Returns the array length and puts the kmalloc'ed array into intid_ptr.
235 */
236static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
237{
238 struct vgic_dist *dist = &kvm->arch.vgic;
239 struct vgic_irq *irq;
240 u32 *intids;
241 int irq_count = dist->lpi_list_count, i = 0;
242
243 /*
244 * We use the current value of the list length, which may change
245 * after the kmalloc. We don't care, because the guest shouldn't
246 * change anything while the command handling is still running,
247 * and in the worst case we would miss a new IRQ, which one wouldn't
248 * expect to be covered by this command anyway.
249 */
250 intids = kmalloc_array(irq_count, sizeof(intids[0]), GFP_KERNEL);
251 if (!intids)
252 return -ENOMEM;
253
254 spin_lock(&dist->lpi_list_lock);
255 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
256 /* We don't need to "get" the IRQ, as we hold the list lock. */
257 intids[i] = irq->intid;
258 if (++i == irq_count)
259 break;
260 }
261 spin_unlock(&dist->lpi_list_lock);
262
263 *intid_ptr = intids;
264 return irq_count;
265}
266
267/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100268 * Promotes the ITS view of affinity of an ITTE (which redistributor this LPI
269 * is targeting) to the VGIC's view, which deals with target VCPUs.
270 * Needs to be called whenever either the collection for a LPIs has
271 * changed or the collection itself got retargeted.
272 */
273static void update_affinity_itte(struct kvm *kvm, struct its_itte *itte)
274{
275 struct kvm_vcpu *vcpu;
276
277 if (!its_is_collection_mapped(itte->collection))
278 return;
279
280 vcpu = kvm_get_vcpu(kvm, itte->collection->target_addr);
281
282 spin_lock(&itte->irq->irq_lock);
283 itte->irq->target_vcpu = vcpu;
284 spin_unlock(&itte->irq->irq_lock);
285}
286
287/*
288 * Updates the target VCPU for every LPI targeting this collection.
289 * Must be called with the its_lock mutex held.
290 */
291static void update_affinity_collection(struct kvm *kvm, struct vgic_its *its,
292 struct its_collection *coll)
293{
294 struct its_device *device;
295 struct its_itte *itte;
296
297 for_each_lpi_its(device, itte, its) {
298 if (!itte->collection || coll != itte->collection)
299 continue;
300
301 update_affinity_itte(kvm, itte);
302 }
303}
304
305static u32 max_lpis_propbaser(u64 propbaser)
306{
307 int nr_idbits = (propbaser & 0x1f) + 1;
308
309 return 1U << min(nr_idbits, INTERRUPT_ID_BITS_ITS);
310}
311
312/*
Andre Przywara33d3bc92016-07-15 12:43:34 +0100313 * Scan the whole LPI pending table and sync the pending bit in there
314 * with our own data structures. This relies on the LPI being
315 * mapped before.
316 */
317static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
318{
319 gpa_t pendbase = PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
320 struct vgic_irq *irq;
321 int last_byte_offset = -1;
322 int ret = 0;
323 u32 *intids;
324 int nr_irqs, i;
325
326 nr_irqs = vgic_copy_lpi_list(vcpu->kvm, &intids);
327 if (nr_irqs < 0)
328 return nr_irqs;
329
330 for (i = 0; i < nr_irqs; i++) {
331 int byte_offset, bit_nr;
332 u8 pendmask;
333
334 byte_offset = intids[i] / BITS_PER_BYTE;
335 bit_nr = intids[i] % BITS_PER_BYTE;
336
337 /*
338 * For contiguously allocated LPIs chances are we just read
339 * this very same byte in the last iteration. Reuse that.
340 */
341 if (byte_offset != last_byte_offset) {
342 ret = kvm_read_guest(vcpu->kvm, pendbase + byte_offset,
343 &pendmask, 1);
344 if (ret) {
345 kfree(intids);
346 return ret;
347 }
348 last_byte_offset = byte_offset;
349 }
350
351 irq = vgic_get_irq(vcpu->kvm, NULL, intids[i]);
352 spin_lock(&irq->irq_lock);
353 irq->pending = pendmask & (1U << bit_nr);
354 vgic_queue_irq_unlock(vcpu->kvm, irq);
355 vgic_put_irq(vcpu->kvm, irq);
356 }
357
358 kfree(intids);
359
360 return ret;
361}
Andre Przywara424c3382016-07-15 12:43:32 +0100362
363static unsigned long vgic_mmio_read_its_ctlr(struct kvm *vcpu,
364 struct vgic_its *its,
365 gpa_t addr, unsigned int len)
366{
367 u32 reg = 0;
368
369 mutex_lock(&its->cmd_lock);
370 if (its->creadr == its->cwriter)
371 reg |= GITS_CTLR_QUIESCENT;
372 if (its->enabled)
373 reg |= GITS_CTLR_ENABLE;
374 mutex_unlock(&its->cmd_lock);
375
376 return reg;
377}
378
379static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its,
380 gpa_t addr, unsigned int len,
381 unsigned long val)
382{
383 its->enabled = !!(val & GITS_CTLR_ENABLE);
384}
385
386static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,
387 struct vgic_its *its,
388 gpa_t addr, unsigned int len)
389{
390 u64 reg = GITS_TYPER_PLPIS;
391
392 /*
393 * We use linear CPU numbers for redistributor addressing,
394 * so GITS_TYPER.PTA is 0.
395 * Also we force all PROPBASER registers to be the same, so
396 * CommonLPIAff is 0 as well.
397 * To avoid memory waste in the guest, we keep the number of IDBits and
398 * DevBits low - as least for the time being.
399 */
400 reg |= 0x0f << GITS_TYPER_DEVBITS_SHIFT;
401 reg |= 0x0f << GITS_TYPER_IDBITS_SHIFT;
402
403 return extract_bytes(reg, addr & 7, len);
404}
405
406static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm,
407 struct vgic_its *its,
408 gpa_t addr, unsigned int len)
409{
410 return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
411}
412
413static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm,
414 struct vgic_its *its,
415 gpa_t addr, unsigned int len)
416{
417 switch (addr & 0xffff) {
418 case GITS_PIDR0:
419 return 0x92; /* part number, bits[7:0] */
420 case GITS_PIDR1:
421 return 0xb4; /* part number, bits[11:8] */
422 case GITS_PIDR2:
423 return GIC_PIDR2_ARCH_GICv3 | 0x0b;
424 case GITS_PIDR4:
425 return 0x40; /* This is a 64K software visible page */
426 /* The following are the ID registers for (any) GIC. */
427 case GITS_CIDR0:
428 return 0x0d;
429 case GITS_CIDR1:
430 return 0xf0;
431 case GITS_CIDR2:
432 return 0x05;
433 case GITS_CIDR3:
434 return 0xb1;
435 }
436
437 return 0;
438}
439
Andre Przywara2891a7d2016-07-15 12:43:37 +0100440/*
441 * Find the target VCPU and the LPI number for a given devid/eventid pair
442 * and make this IRQ pending, possibly injecting it.
443 * Must be called with the its_lock mutex held.
Andre Przywarafd837b02016-08-08 17:29:28 +0100444 * Returns 0 on success, a positive error value for any ITS mapping
445 * related errors and negative error values for generic errors.
Andre Przywara2891a7d2016-07-15 12:43:37 +0100446 */
Andre Przywarafd837b02016-08-08 17:29:28 +0100447static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its,
448 u32 devid, u32 eventid)
Andre Przywara2891a7d2016-07-15 12:43:37 +0100449{
Andre Przywarafd837b02016-08-08 17:29:28 +0100450 struct kvm_vcpu *vcpu;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100451 struct its_itte *itte;
452
453 if (!its->enabled)
Andre Przywarafd837b02016-08-08 17:29:28 +0100454 return -EBUSY;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100455
456 itte = find_itte(its, devid, eventid);
Andre Przywarafd837b02016-08-08 17:29:28 +0100457 if (!itte || !its_is_collection_mapped(itte->collection))
458 return E_ITS_INT_UNMAPPED_INTERRUPT;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100459
Andre Przywarafd837b02016-08-08 17:29:28 +0100460 vcpu = kvm_get_vcpu(kvm, itte->collection->target_addr);
461 if (!vcpu)
462 return E_ITS_INT_UNMAPPED_INTERRUPT;
463
464 if (!vcpu->arch.vgic_cpu.lpis_enabled)
465 return -EBUSY;
466
467 spin_lock(&itte->irq->irq_lock);
468 itte->irq->pending = true;
469 vgic_queue_irq_unlock(kvm, itte->irq);
470
471 return 0;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100472}
473
Andre Przywara505a19e2016-08-09 10:54:29 +0100474static struct vgic_io_device *vgic_get_its_iodev(struct kvm_io_device *dev)
475{
476 struct vgic_io_device *iodev;
477
478 if (dev->ops != &kvm_io_gic_ops)
479 return NULL;
480
481 iodev = container_of(dev, struct vgic_io_device, dev);
482
483 if (iodev->iodev_type != IODEV_ITS)
484 return NULL;
485
486 return iodev;
487}
488
Andre Przywara2891a7d2016-07-15 12:43:37 +0100489/*
490 * Queries the KVM IO bus framework to get the ITS pointer from the given
491 * doorbell address.
492 * We then call vgic_its_trigger_msi() with the decoded data.
Andre Przywarafd837b02016-08-08 17:29:28 +0100493 * According to the KVM_SIGNAL_MSI API description returns 1 on success.
Andre Przywara2891a7d2016-07-15 12:43:37 +0100494 */
495int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi)
496{
497 u64 address;
498 struct kvm_io_device *kvm_io_dev;
499 struct vgic_io_device *iodev;
Andre Przywarafd837b02016-08-08 17:29:28 +0100500 int ret;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100501
502 if (!vgic_has_its(kvm))
503 return -ENODEV;
504
505 if (!(msi->flags & KVM_MSI_VALID_DEVID))
506 return -EINVAL;
507
508 address = (u64)msi->address_hi << 32 | msi->address_lo;
509
510 kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, address);
511 if (!kvm_io_dev)
Andre Przywara505a19e2016-08-09 10:54:29 +0100512 return -EINVAL;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100513
Andre Przywara505a19e2016-08-09 10:54:29 +0100514 iodev = vgic_get_its_iodev(kvm_io_dev);
515 if (!iodev)
516 return -EINVAL;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100517
518 mutex_lock(&iodev->its->its_lock);
Andre Przywarafd837b02016-08-08 17:29:28 +0100519 ret = vgic_its_trigger_msi(kvm, iodev->its, msi->devid, msi->data);
Andre Przywara2891a7d2016-07-15 12:43:37 +0100520 mutex_unlock(&iodev->its->its_lock);
521
Andre Przywarafd837b02016-08-08 17:29:28 +0100522 if (ret < 0)
523 return ret;
524
525 /*
526 * KVM_SIGNAL_MSI demands a return value > 0 for success and 0
527 * if the guest has blocked the MSI. So we map any LPI mapping
528 * related error to that.
529 */
530 if (ret)
531 return 0;
532 else
533 return 1;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100534}
535
Andre Przywara424c3382016-07-15 12:43:32 +0100536/* Requires the its_lock to be held. */
537static void its_free_itte(struct kvm *kvm, struct its_itte *itte)
538{
539 list_del(&itte->itte_list);
Andre Przywara38024112016-07-15 12:43:33 +0100540
541 /* This put matches the get in vgic_add_lpi. */
Christoffer Dall99e5e882016-08-01 20:25:33 +0200542 if (itte->irq)
543 vgic_put_irq(kvm, itte->irq);
Andre Przywara38024112016-07-15 12:43:33 +0100544
Andre Przywara424c3382016-07-15 12:43:32 +0100545 kfree(itte);
546}
547
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100548static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size)
549{
550 return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT_ULL(size) - 1);
551}
552
553#define its_cmd_get_command(cmd) its_cmd_mask_field(cmd, 0, 0, 8)
554#define its_cmd_get_deviceid(cmd) its_cmd_mask_field(cmd, 0, 32, 32)
555#define its_cmd_get_id(cmd) its_cmd_mask_field(cmd, 1, 0, 32)
556#define its_cmd_get_physical_id(cmd) its_cmd_mask_field(cmd, 1, 32, 32)
557#define its_cmd_get_collection(cmd) its_cmd_mask_field(cmd, 2, 0, 16)
558#define its_cmd_get_target_addr(cmd) its_cmd_mask_field(cmd, 2, 16, 32)
559#define its_cmd_get_validbit(cmd) its_cmd_mask_field(cmd, 2, 63, 1)
560
561/*
562 * The DISCARD command frees an Interrupt Translation Table Entry (ITTE).
563 * Must be called with the its_lock mutex held.
564 */
565static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its,
566 u64 *its_cmd)
567{
568 u32 device_id = its_cmd_get_deviceid(its_cmd);
569 u32 event_id = its_cmd_get_id(its_cmd);
570 struct its_itte *itte;
571
572
573 itte = find_itte(its, device_id, event_id);
574 if (itte && itte->collection) {
575 /*
576 * Though the spec talks about removing the pending state, we
577 * don't bother here since we clear the ITTE anyway and the
578 * pending state is a property of the ITTE struct.
579 */
580 its_free_itte(kvm, itte);
581 return 0;
582 }
583
584 return E_ITS_DISCARD_UNMAPPED_INTERRUPT;
585}
586
587/*
588 * The MOVI command moves an ITTE to a different collection.
589 * Must be called with the its_lock mutex held.
590 */
591static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,
592 u64 *its_cmd)
593{
594 u32 device_id = its_cmd_get_deviceid(its_cmd);
595 u32 event_id = its_cmd_get_id(its_cmd);
596 u32 coll_id = its_cmd_get_collection(its_cmd);
597 struct kvm_vcpu *vcpu;
598 struct its_itte *itte;
599 struct its_collection *collection;
600
601 itte = find_itte(its, device_id, event_id);
602 if (!itte)
603 return E_ITS_MOVI_UNMAPPED_INTERRUPT;
604
605 if (!its_is_collection_mapped(itte->collection))
606 return E_ITS_MOVI_UNMAPPED_COLLECTION;
607
608 collection = find_collection(its, coll_id);
609 if (!its_is_collection_mapped(collection))
610 return E_ITS_MOVI_UNMAPPED_COLLECTION;
611
612 itte->collection = collection;
613 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
614
615 spin_lock(&itte->irq->irq_lock);
616 itte->irq->target_vcpu = vcpu;
617 spin_unlock(&itte->irq->irq_lock);
618
619 return 0;
620}
621
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100622/*
623 * Check whether an ID can be stored into the corresponding guest table.
624 * For a direct table this is pretty easy, but gets a bit nasty for
625 * indirect tables. We check whether the resulting guest physical address
626 * is actually valid (covered by a memslot and guest accessbible).
627 * For this we have to read the respective first level entry.
628 */
629static bool vgic_its_check_id(struct vgic_its *its, u64 baser, int id)
630{
631 int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
632 int index;
633 u64 indirect_ptr;
634 gfn_t gfn;
635
636 if (!(baser & GITS_BASER_INDIRECT)) {
637 phys_addr_t addr;
638
639 if (id >= (l1_tbl_size / GITS_BASER_ENTRY_SIZE(baser)))
640 return false;
641
642 addr = BASER_ADDRESS(baser) + id * GITS_BASER_ENTRY_SIZE(baser);
643 gfn = addr >> PAGE_SHIFT;
644
645 return kvm_is_visible_gfn(its->dev->kvm, gfn);
646 }
647
648 /* calculate and check the index into the 1st level */
649 index = id / (SZ_64K / GITS_BASER_ENTRY_SIZE(baser));
650 if (index >= (l1_tbl_size / sizeof(u64)))
651 return false;
652
653 /* Each 1st level entry is represented by a 64-bit value. */
654 if (kvm_read_guest(its->dev->kvm,
655 BASER_ADDRESS(baser) + index * sizeof(indirect_ptr),
656 &indirect_ptr, sizeof(indirect_ptr)))
657 return false;
658
659 indirect_ptr = le64_to_cpu(indirect_ptr);
660
661 /* check the valid bit of the first level entry */
662 if (!(indirect_ptr & BIT_ULL(63)))
663 return false;
664
665 /*
666 * Mask the guest physical address and calculate the frame number.
667 * Any address beyond our supported 48 bits of PA will be caught
668 * by the actual check in the final step.
669 */
670 indirect_ptr &= GENMASK_ULL(51, 16);
671
672 /* Find the address of the actual entry */
673 index = id % (SZ_64K / GITS_BASER_ENTRY_SIZE(baser));
674 indirect_ptr += index * GITS_BASER_ENTRY_SIZE(baser);
675 gfn = indirect_ptr >> PAGE_SHIFT;
676
677 return kvm_is_visible_gfn(its->dev->kvm, gfn);
678}
679
Marc Zyngier17a21f52016-07-17 20:01:46 +0100680static int vgic_its_alloc_collection(struct vgic_its *its,
681 struct its_collection **colp,
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100682 u32 coll_id)
683{
Marc Zyngier17a21f52016-07-17 20:01:46 +0100684 struct its_collection *collection;
685
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100686 if (!vgic_its_check_id(its, its->baser_coll_table, coll_id))
687 return E_ITS_MAPC_COLLECTION_OOR;
688
Marc Zyngier17a21f52016-07-17 20:01:46 +0100689 collection = kzalloc(sizeof(*collection), GFP_KERNEL);
Marc Zyngier7df3dbe2017-11-16 17:58:18 +0000690 if (!collection)
691 return -ENOMEM;
Marc Zyngier17a21f52016-07-17 20:01:46 +0100692
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100693 collection->collection_id = coll_id;
694 collection->target_addr = COLLECTION_NOT_MAPPED;
695
696 list_add_tail(&collection->coll_list, &its->collection_list);
Marc Zyngier17a21f52016-07-17 20:01:46 +0100697 *colp = collection;
698
699 return 0;
700}
701
702static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)
703{
704 struct its_collection *collection;
705 struct its_device *device;
706 struct its_itte *itte;
707
708 /*
709 * Clearing the mapping for that collection ID removes the
710 * entry from the list. If there wasn't any before, we can
711 * go home early.
712 */
713 collection = find_collection(its, coll_id);
714 if (!collection)
715 return;
716
717 for_each_lpi_its(device, itte, its)
718 if (itte->collection &&
719 itte->collection->collection_id == coll_id)
720 itte->collection = NULL;
721
722 list_del(&collection->coll_list);
723 kfree(collection);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100724}
725
726/*
727 * The MAPTI and MAPI commands map LPIs to ITTEs.
728 * Must be called with its_lock mutex held.
729 */
730static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
Marc Zyngiera3e7aa22016-07-17 22:38:32 +0100731 u64 *its_cmd)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100732{
733 u32 device_id = its_cmd_get_deviceid(its_cmd);
734 u32 event_id = its_cmd_get_id(its_cmd);
735 u32 coll_id = its_cmd_get_collection(its_cmd);
Andre Przywara286054a2016-08-16 17:51:06 +0100736 struct its_itte *itte;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100737 struct its_device *device;
738 struct its_collection *collection, *new_coll = NULL;
739 int lpi_nr;
Christoffer Dall99e5e882016-08-01 20:25:33 +0200740 struct vgic_irq *irq;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100741
742 device = find_its_device(its, device_id);
743 if (!device)
744 return E_ITS_MAPTI_UNMAPPED_DEVICE;
745
Marc Zyngiera3e7aa22016-07-17 22:38:32 +0100746 if (its_cmd_get_command(its_cmd) == GITS_CMD_MAPTI)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100747 lpi_nr = its_cmd_get_physical_id(its_cmd);
748 else
749 lpi_nr = event_id;
750 if (lpi_nr < GIC_LPI_OFFSET ||
Marc Zyngier3a88bde2016-07-18 16:27:14 +0100751 lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser))
752 return E_ITS_MAPTI_PHYSICALID_OOR;
753
Andre Przywara286054a2016-08-16 17:51:06 +0100754 /* If there is an existing mapping, behavior is UNPREDICTABLE. */
755 if (find_itte(its, device_id, event_id))
756 return 0;
757
Marc Zyngier3a88bde2016-07-18 16:27:14 +0100758 collection = find_collection(its, coll_id);
759 if (!collection) {
760 int ret = vgic_its_alloc_collection(its, &collection, coll_id);
761 if (ret)
762 return ret;
763 new_coll = collection;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100764 }
765
Andre Przywara286054a2016-08-16 17:51:06 +0100766 itte = kzalloc(sizeof(struct its_itte), GFP_KERNEL);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100767 if (!itte) {
Andre Przywara286054a2016-08-16 17:51:06 +0100768 if (new_coll)
769 vgic_its_free_collection(its, coll_id);
770 return -ENOMEM;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100771 }
772
Andre Przywara286054a2016-08-16 17:51:06 +0100773 itte->event_id = event_id;
774 list_add_tail(&itte->itte_list, &device->itt_head);
775
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100776 itte->collection = collection;
777 itte->lpi = lpi_nr;
Christoffer Dall99e5e882016-08-01 20:25:33 +0200778
779 irq = vgic_add_lpi(kvm, lpi_nr);
780 if (IS_ERR(irq)) {
781 if (new_coll)
782 vgic_its_free_collection(its, coll_id);
Andre Przywara286054a2016-08-16 17:51:06 +0100783 its_free_itte(kvm, itte);
Christoffer Dall99e5e882016-08-01 20:25:33 +0200784 return PTR_ERR(irq);
785 }
786 itte->irq = irq;
787
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100788 update_affinity_itte(kvm, itte);
789
790 /*
791 * We "cache" the configuration table entries in out struct vgic_irq's.
792 * However we only have those structs for mapped IRQs, so we read in
793 * the respective config data from memory here upon mapping the LPI.
794 */
795 update_lpi_config(kvm, itte->irq, NULL);
796
797 return 0;
798}
799
800/* Requires the its_lock to be held. */
801static void vgic_its_unmap_device(struct kvm *kvm, struct its_device *device)
802{
803 struct its_itte *itte, *temp;
804
805 /*
806 * The spec says that unmapping a device with still valid
807 * ITTEs associated is UNPREDICTABLE. We remove all ITTEs,
808 * since we cannot leave the memory unreferenced.
809 */
810 list_for_each_entry_safe(itte, temp, &device->itt_head, itte_list)
811 its_free_itte(kvm, itte);
812
813 list_del(&device->dev_list);
814 kfree(device);
815}
816
817/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100818 * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs).
819 * Must be called with the its_lock mutex held.
820 */
821static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
822 u64 *its_cmd)
823{
824 u32 device_id = its_cmd_get_deviceid(its_cmd);
825 bool valid = its_cmd_get_validbit(its_cmd);
826 struct its_device *device;
827
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100828 if (!vgic_its_check_id(its, its->baser_device_table, device_id))
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100829 return E_ITS_MAPD_DEVICE_OOR;
830
831 device = find_its_device(its, device_id);
832
833 /*
834 * The spec says that calling MAPD on an already mapped device
835 * invalidates all cached data for this device. We implement this
836 * by removing the mapping and re-establishing it.
837 */
838 if (device)
839 vgic_its_unmap_device(kvm, device);
840
841 /*
842 * The spec does not say whether unmapping a not-mapped device
843 * is an error, so we are done in any case.
844 */
845 if (!valid)
846 return 0;
847
848 device = kzalloc(sizeof(struct its_device), GFP_KERNEL);
849 if (!device)
850 return -ENOMEM;
851
852 device->device_id = device_id;
853 INIT_LIST_HEAD(&device->itt_head);
854
855 list_add_tail(&device->dev_list, &its->device_list);
856
857 return 0;
858}
859
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100860/*
861 * The MAPC command maps collection IDs to redistributors.
862 * Must be called with the its_lock mutex held.
863 */
864static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its,
865 u64 *its_cmd)
866{
867 u16 coll_id;
868 u32 target_addr;
869 struct its_collection *collection;
870 bool valid;
871
872 valid = its_cmd_get_validbit(its_cmd);
873 coll_id = its_cmd_get_collection(its_cmd);
874 target_addr = its_cmd_get_target_addr(its_cmd);
875
876 if (target_addr >= atomic_read(&kvm->online_vcpus))
877 return E_ITS_MAPC_PROCNUM_OOR;
878
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100879 if (!valid) {
Marc Zyngier17a21f52016-07-17 20:01:46 +0100880 vgic_its_free_collection(its, coll_id);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100881 } else {
Marc Zyngier17a21f52016-07-17 20:01:46 +0100882 collection = find_collection(its, coll_id);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100883
Marc Zyngier17a21f52016-07-17 20:01:46 +0100884 if (!collection) {
885 int ret;
886
887 ret = vgic_its_alloc_collection(its, &collection,
888 coll_id);
889 if (ret)
890 return ret;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100891 collection->target_addr = target_addr;
892 } else {
893 collection->target_addr = target_addr;
894 update_affinity_collection(kvm, its, collection);
895 }
896 }
897
898 return 0;
899}
900
901/*
902 * The CLEAR command removes the pending state for a particular LPI.
903 * Must be called with the its_lock mutex held.
904 */
905static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its,
906 u64 *its_cmd)
907{
908 u32 device_id = its_cmd_get_deviceid(its_cmd);
909 u32 event_id = its_cmd_get_id(its_cmd);
910 struct its_itte *itte;
911
912
913 itte = find_itte(its, device_id, event_id);
914 if (!itte)
915 return E_ITS_CLEAR_UNMAPPED_INTERRUPT;
916
917 itte->irq->pending = false;
918
919 return 0;
920}
921
922/*
923 * The INV command syncs the configuration bits from the memory table.
924 * Must be called with the its_lock mutex held.
925 */
926static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its,
927 u64 *its_cmd)
928{
929 u32 device_id = its_cmd_get_deviceid(its_cmd);
930 u32 event_id = its_cmd_get_id(its_cmd);
931 struct its_itte *itte;
932
933
934 itte = find_itte(its, device_id, event_id);
935 if (!itte)
936 return E_ITS_INV_UNMAPPED_INTERRUPT;
937
938 return update_lpi_config(kvm, itte->irq, NULL);
939}
940
941/*
942 * The INVALL command requests flushing of all IRQ data in this collection.
943 * Find the VCPU mapped to that collection, then iterate over the VM's list
944 * of mapped LPIs and update the configuration for each IRQ which targets
945 * the specified vcpu. The configuration will be read from the in-memory
946 * configuration table.
947 * Must be called with the its_lock mutex held.
948 */
949static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its,
950 u64 *its_cmd)
951{
952 u32 coll_id = its_cmd_get_collection(its_cmd);
953 struct its_collection *collection;
954 struct kvm_vcpu *vcpu;
955 struct vgic_irq *irq;
956 u32 *intids;
957 int irq_count, i;
958
959 collection = find_collection(its, coll_id);
960 if (!its_is_collection_mapped(collection))
961 return E_ITS_INVALL_UNMAPPED_COLLECTION;
962
963 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
964
965 irq_count = vgic_copy_lpi_list(kvm, &intids);
966 if (irq_count < 0)
967 return irq_count;
968
969 for (i = 0; i < irq_count; i++) {
970 irq = vgic_get_irq(kvm, NULL, intids[i]);
971 if (!irq)
972 continue;
973 update_lpi_config(kvm, irq, vcpu);
974 vgic_put_irq(kvm, irq);
975 }
976
977 kfree(intids);
978
979 return 0;
980}
981
982/*
983 * The MOVALL command moves the pending state of all IRQs targeting one
984 * redistributor to another. We don't hold the pending state in the VCPUs,
985 * but in the IRQs instead, so there is really not much to do for us here.
986 * However the spec says that no IRQ must target the old redistributor
987 * afterwards, so we make sure that no LPI is using the associated target_vcpu.
988 * This command affects all LPIs in the system that target that redistributor.
989 */
990static int vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its,
991 u64 *its_cmd)
992{
993 struct vgic_dist *dist = &kvm->arch.vgic;
994 u32 target1_addr = its_cmd_get_target_addr(its_cmd);
995 u32 target2_addr = its_cmd_mask_field(its_cmd, 3, 16, 32);
996 struct kvm_vcpu *vcpu1, *vcpu2;
997 struct vgic_irq *irq;
998
999 if (target1_addr >= atomic_read(&kvm->online_vcpus) ||
1000 target2_addr >= atomic_read(&kvm->online_vcpus))
1001 return E_ITS_MOVALL_PROCNUM_OOR;
1002
1003 if (target1_addr == target2_addr)
1004 return 0;
1005
1006 vcpu1 = kvm_get_vcpu(kvm, target1_addr);
1007 vcpu2 = kvm_get_vcpu(kvm, target2_addr);
1008
1009 spin_lock(&dist->lpi_list_lock);
1010
1011 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
1012 spin_lock(&irq->irq_lock);
1013
1014 if (irq->target_vcpu == vcpu1)
1015 irq->target_vcpu = vcpu2;
1016
1017 spin_unlock(&irq->irq_lock);
1018 }
1019
1020 spin_unlock(&dist->lpi_list_lock);
1021
1022 return 0;
1023}
1024
1025/*
Andre Przywara2891a7d2016-07-15 12:43:37 +01001026 * The INT command injects the LPI associated with that DevID/EvID pair.
1027 * Must be called with the its_lock mutex held.
1028 */
1029static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its,
1030 u64 *its_cmd)
1031{
1032 u32 msi_data = its_cmd_get_id(its_cmd);
1033 u64 msi_devid = its_cmd_get_deviceid(its_cmd);
1034
Andre Przywarafd837b02016-08-08 17:29:28 +01001035 return vgic_its_trigger_msi(kvm, its, msi_devid, msi_data);
Andre Przywara2891a7d2016-07-15 12:43:37 +01001036}
1037
1038/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001039 * This function is called with the its_cmd lock held, but the ITS data
1040 * structure lock dropped.
1041 */
Andre Przywara424c3382016-07-15 12:43:32 +01001042static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its,
1043 u64 *its_cmd)
1044{
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001045 int ret = -ENODEV;
1046
1047 mutex_lock(&its->its_lock);
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001048 switch (its_cmd_get_command(its_cmd)) {
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001049 case GITS_CMD_MAPD:
1050 ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd);
1051 break;
1052 case GITS_CMD_MAPC:
1053 ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd);
1054 break;
1055 case GITS_CMD_MAPI:
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001056 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001057 break;
1058 case GITS_CMD_MAPTI:
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001059 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001060 break;
1061 case GITS_CMD_MOVI:
1062 ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd);
1063 break;
1064 case GITS_CMD_DISCARD:
1065 ret = vgic_its_cmd_handle_discard(kvm, its, its_cmd);
1066 break;
1067 case GITS_CMD_CLEAR:
1068 ret = vgic_its_cmd_handle_clear(kvm, its, its_cmd);
1069 break;
1070 case GITS_CMD_MOVALL:
1071 ret = vgic_its_cmd_handle_movall(kvm, its, its_cmd);
1072 break;
Andre Przywara2891a7d2016-07-15 12:43:37 +01001073 case GITS_CMD_INT:
1074 ret = vgic_its_cmd_handle_int(kvm, its, its_cmd);
1075 break;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001076 case GITS_CMD_INV:
1077 ret = vgic_its_cmd_handle_inv(kvm, its, its_cmd);
1078 break;
1079 case GITS_CMD_INVALL:
1080 ret = vgic_its_cmd_handle_invall(kvm, its, its_cmd);
1081 break;
1082 case GITS_CMD_SYNC:
1083 /* we ignore this command: we are in sync all of the time */
1084 ret = 0;
1085 break;
1086 }
1087 mutex_unlock(&its->its_lock);
1088
1089 return ret;
Andre Przywara424c3382016-07-15 12:43:32 +01001090}
1091
1092static u64 vgic_sanitise_its_baser(u64 reg)
1093{
1094 reg = vgic_sanitise_field(reg, GITS_BASER_SHAREABILITY_MASK,
1095 GITS_BASER_SHAREABILITY_SHIFT,
1096 vgic_sanitise_shareability);
1097 reg = vgic_sanitise_field(reg, GITS_BASER_INNER_CACHEABILITY_MASK,
1098 GITS_BASER_INNER_CACHEABILITY_SHIFT,
1099 vgic_sanitise_inner_cacheability);
1100 reg = vgic_sanitise_field(reg, GITS_BASER_OUTER_CACHEABILITY_MASK,
1101 GITS_BASER_OUTER_CACHEABILITY_SHIFT,
1102 vgic_sanitise_outer_cacheability);
1103
1104 /* Bits 15:12 contain bits 51:48 of the PA, which we don't support. */
1105 reg &= ~GENMASK_ULL(15, 12);
1106
1107 /* We support only one (ITS) page size: 64K */
1108 reg = (reg & ~GITS_BASER_PAGE_SIZE_MASK) | GITS_BASER_PAGE_SIZE_64K;
1109
1110 return reg;
1111}
1112
1113static u64 vgic_sanitise_its_cbaser(u64 reg)
1114{
1115 reg = vgic_sanitise_field(reg, GITS_CBASER_SHAREABILITY_MASK,
1116 GITS_CBASER_SHAREABILITY_SHIFT,
1117 vgic_sanitise_shareability);
1118 reg = vgic_sanitise_field(reg, GITS_CBASER_INNER_CACHEABILITY_MASK,
1119 GITS_CBASER_INNER_CACHEABILITY_SHIFT,
1120 vgic_sanitise_inner_cacheability);
1121 reg = vgic_sanitise_field(reg, GITS_CBASER_OUTER_CACHEABILITY_MASK,
1122 GITS_CBASER_OUTER_CACHEABILITY_SHIFT,
1123 vgic_sanitise_outer_cacheability);
1124
1125 /*
1126 * Sanitise the physical address to be 64k aligned.
1127 * Also limit the physical addresses to 48 bits.
1128 */
1129 reg &= ~(GENMASK_ULL(51, 48) | GENMASK_ULL(15, 12));
1130
1131 return reg;
1132}
1133
1134static unsigned long vgic_mmio_read_its_cbaser(struct kvm *kvm,
1135 struct vgic_its *its,
1136 gpa_t addr, unsigned int len)
1137{
1138 return extract_bytes(its->cbaser, addr & 7, len);
1139}
1140
1141static void vgic_mmio_write_its_cbaser(struct kvm *kvm, struct vgic_its *its,
1142 gpa_t addr, unsigned int len,
1143 unsigned long val)
1144{
1145 /* When GITS_CTLR.Enable is 1, this register is RO. */
1146 if (its->enabled)
1147 return;
1148
1149 mutex_lock(&its->cmd_lock);
1150 its->cbaser = update_64bit_reg(its->cbaser, addr & 7, len, val);
1151 its->cbaser = vgic_sanitise_its_cbaser(its->cbaser);
1152 its->creadr = 0;
1153 /*
1154 * CWRITER is architecturally UNKNOWN on reset, but we need to reset
1155 * it to CREADR to make sure we start with an empty command buffer.
1156 */
1157 its->cwriter = its->creadr;
1158 mutex_unlock(&its->cmd_lock);
1159}
1160
1161#define ITS_CMD_BUFFER_SIZE(baser) ((((baser) & 0xff) + 1) << 12)
1162#define ITS_CMD_SIZE 32
1163#define ITS_CMD_OFFSET(reg) ((reg) & GENMASK(19, 5))
1164
1165/*
1166 * By writing to CWRITER the guest announces new commands to be processed.
1167 * To avoid any races in the first place, we take the its_cmd lock, which
1168 * protects our ring buffer variables, so that there is only one user
1169 * per ITS handling commands at a given time.
1170 */
1171static void vgic_mmio_write_its_cwriter(struct kvm *kvm, struct vgic_its *its,
1172 gpa_t addr, unsigned int len,
1173 unsigned long val)
1174{
1175 gpa_t cbaser;
1176 u64 cmd_buf[4];
1177 u32 reg;
1178
1179 if (!its)
1180 return;
1181
1182 mutex_lock(&its->cmd_lock);
1183
1184 reg = update_64bit_reg(its->cwriter, addr & 7, len, val);
1185 reg = ITS_CMD_OFFSET(reg);
1186 if (reg >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
1187 mutex_unlock(&its->cmd_lock);
1188 return;
1189 }
1190
1191 its->cwriter = reg;
1192 cbaser = CBASER_ADDRESS(its->cbaser);
1193
1194 while (its->cwriter != its->creadr) {
1195 int ret = kvm_read_guest(kvm, cbaser + its->creadr,
1196 cmd_buf, ITS_CMD_SIZE);
1197 /*
1198 * If kvm_read_guest() fails, this could be due to the guest
1199 * programming a bogus value in CBASER or something else going
1200 * wrong from which we cannot easily recover.
1201 * According to section 6.3.2 in the GICv3 spec we can just
1202 * ignore that command then.
1203 */
1204 if (!ret)
1205 vgic_its_handle_command(kvm, its, cmd_buf);
1206
1207 its->creadr += ITS_CMD_SIZE;
1208 if (its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser))
1209 its->creadr = 0;
1210 }
1211
1212 mutex_unlock(&its->cmd_lock);
1213}
1214
1215static unsigned long vgic_mmio_read_its_cwriter(struct kvm *kvm,
1216 struct vgic_its *its,
1217 gpa_t addr, unsigned int len)
1218{
1219 return extract_bytes(its->cwriter, addr & 0x7, len);
1220}
1221
1222static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,
1223 struct vgic_its *its,
1224 gpa_t addr, unsigned int len)
1225{
1226 return extract_bytes(its->creadr, addr & 0x7, len);
1227}
1228
1229#define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7)
1230static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm,
1231 struct vgic_its *its,
1232 gpa_t addr, unsigned int len)
1233{
1234 u64 reg;
1235
1236 switch (BASER_INDEX(addr)) {
1237 case 0:
1238 reg = its->baser_device_table;
1239 break;
1240 case 1:
1241 reg = its->baser_coll_table;
1242 break;
1243 default:
1244 reg = 0;
1245 break;
1246 }
1247
1248 return extract_bytes(reg, addr & 7, len);
1249}
1250
1251#define GITS_BASER_RO_MASK (GENMASK_ULL(52, 48) | GENMASK_ULL(58, 56))
1252static void vgic_mmio_write_its_baser(struct kvm *kvm,
1253 struct vgic_its *its,
1254 gpa_t addr, unsigned int len,
1255 unsigned long val)
1256{
1257 u64 entry_size, device_type;
1258 u64 reg, *regptr, clearbits = 0;
1259
1260 /* When GITS_CTLR.Enable is 1, we ignore write accesses. */
1261 if (its->enabled)
1262 return;
1263
1264 switch (BASER_INDEX(addr)) {
1265 case 0:
1266 regptr = &its->baser_device_table;
1267 entry_size = 8;
1268 device_type = GITS_BASER_TYPE_DEVICE;
1269 break;
1270 case 1:
1271 regptr = &its->baser_coll_table;
1272 entry_size = 8;
1273 device_type = GITS_BASER_TYPE_COLLECTION;
1274 clearbits = GITS_BASER_INDIRECT;
1275 break;
1276 default:
1277 return;
1278 }
1279
1280 reg = update_64bit_reg(*regptr, addr & 7, len, val);
1281 reg &= ~GITS_BASER_RO_MASK;
1282 reg &= ~clearbits;
1283
1284 reg |= (entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
1285 reg |= device_type << GITS_BASER_TYPE_SHIFT;
1286 reg = vgic_sanitise_its_baser(reg);
1287
1288 *regptr = reg;
1289}
1290
Andre Przywara59c5ab42016-07-15 12:43:30 +01001291#define REGISTER_ITS_DESC(off, rd, wr, length, acc) \
1292{ \
1293 .reg_offset = off, \
1294 .len = length, \
1295 .access_flags = acc, \
1296 .its_read = rd, \
1297 .its_write = wr, \
1298}
1299
Andre Przywara59c5ab42016-07-15 12:43:30 +01001300static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its,
1301 gpa_t addr, unsigned int len, unsigned long val)
1302{
1303 /* Ignore */
1304}
1305
1306static struct vgic_register_region its_registers[] = {
1307 REGISTER_ITS_DESC(GITS_CTLR,
Andre Przywara424c3382016-07-15 12:43:32 +01001308 vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001309 VGIC_ACCESS_32bit),
1310 REGISTER_ITS_DESC(GITS_IIDR,
Andre Przywara424c3382016-07-15 12:43:32 +01001311 vgic_mmio_read_its_iidr, its_mmio_write_wi, 4,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001312 VGIC_ACCESS_32bit),
1313 REGISTER_ITS_DESC(GITS_TYPER,
Andre Przywara424c3382016-07-15 12:43:32 +01001314 vgic_mmio_read_its_typer, its_mmio_write_wi, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001315 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1316 REGISTER_ITS_DESC(GITS_CBASER,
Andre Przywara424c3382016-07-15 12:43:32 +01001317 vgic_mmio_read_its_cbaser, vgic_mmio_write_its_cbaser, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001318 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1319 REGISTER_ITS_DESC(GITS_CWRITER,
Andre Przywara424c3382016-07-15 12:43:32 +01001320 vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001321 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1322 REGISTER_ITS_DESC(GITS_CREADR,
Andre Przywara424c3382016-07-15 12:43:32 +01001323 vgic_mmio_read_its_creadr, its_mmio_write_wi, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001324 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1325 REGISTER_ITS_DESC(GITS_BASER,
Andre Przywara424c3382016-07-15 12:43:32 +01001326 vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001327 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1328 REGISTER_ITS_DESC(GITS_IDREGS_BASE,
Andre Przywara424c3382016-07-15 12:43:32 +01001329 vgic_mmio_read_its_idregs, its_mmio_write_wi, 0x30,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001330 VGIC_ACCESS_32bit),
1331};
1332
Andre Przywara33d3bc92016-07-15 12:43:34 +01001333/* This is called on setting the LPI enable bit in the redistributor. */
1334void vgic_enable_lpis(struct kvm_vcpu *vcpu)
1335{
1336 if (!(vcpu->arch.vgic_cpu.pendbaser & GICR_PENDBASER_PTZ))
1337 its_sync_lpi_pending_table(vcpu);
1338}
1339
Andre Przywarac7735762016-08-08 16:45:43 +01001340static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its)
Andre Przywara59c5ab42016-07-15 12:43:30 +01001341{
1342 struct vgic_io_device *iodev = &its->iodev;
1343 int ret;
1344
Andre Przywarac7735762016-08-08 16:45:43 +01001345 if (!its->initialized)
1346 return -EBUSY;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001347
Andre Przywara59c5ab42016-07-15 12:43:30 +01001348 if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base))
1349 return -ENXIO;
1350
1351 iodev->regions = its_registers;
1352 iodev->nr_regions = ARRAY_SIZE(its_registers);
1353 kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops);
1354
1355 iodev->base_addr = its->vgic_its_base;
1356 iodev->iodev_type = IODEV_ITS;
1357 iodev->its = its;
1358 mutex_lock(&kvm->slots_lock);
1359 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr,
1360 KVM_VGIC_V3_ITS_SIZE, &iodev->dev);
1361 mutex_unlock(&kvm->slots_lock);
1362
1363 return ret;
1364}
Andre Przywara1085fdc2016-07-15 12:43:31 +01001365
Andre Przywara424c3382016-07-15 12:43:32 +01001366#define INITIAL_BASER_VALUE \
1367 (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb) | \
1368 GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner) | \
1369 GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable) | \
1370 ((8ULL - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) | \
1371 GITS_BASER_PAGE_SIZE_64K)
1372
1373#define INITIAL_PROPBASER_VALUE \
1374 (GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWb) | \
1375 GIC_BASER_CACHEABILITY(GICR_PROPBASER, OUTER, SameAsInner) | \
1376 GIC_BASER_SHAREABILITY(GICR_PROPBASER, InnerShareable))
1377
Andre Przywara1085fdc2016-07-15 12:43:31 +01001378static int vgic_its_create(struct kvm_device *dev, u32 type)
1379{
1380 struct vgic_its *its;
1381
1382 if (type != KVM_DEV_TYPE_ARM_VGIC_ITS)
1383 return -ENODEV;
1384
1385 its = kzalloc(sizeof(struct vgic_its), GFP_KERNEL);
1386 if (!its)
1387 return -ENOMEM;
1388
Andre Przywara424c3382016-07-15 12:43:32 +01001389 mutex_init(&its->its_lock);
1390 mutex_init(&its->cmd_lock);
1391
Andre Przywara1085fdc2016-07-15 12:43:31 +01001392 its->vgic_its_base = VGIC_ADDR_UNDEF;
1393
Andre Przywara424c3382016-07-15 12:43:32 +01001394 INIT_LIST_HEAD(&its->device_list);
1395 INIT_LIST_HEAD(&its->collection_list);
1396
Andre Przywara1085fdc2016-07-15 12:43:31 +01001397 dev->kvm->arch.vgic.has_its = true;
1398 its->initialized = false;
1399 its->enabled = false;
Marc Zyngierbb717642016-07-17 21:35:07 +01001400 its->dev = dev;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001401
Andre Przywara424c3382016-07-15 12:43:32 +01001402 its->baser_device_table = INITIAL_BASER_VALUE |
1403 ((u64)GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT);
1404 its->baser_coll_table = INITIAL_BASER_VALUE |
1405 ((u64)GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT);
1406 dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE;
1407
Andre Przywara1085fdc2016-07-15 12:43:31 +01001408 dev->private = its;
1409
1410 return 0;
1411}
1412
1413static void vgic_its_destroy(struct kvm_device *kvm_dev)
1414{
Andre Przywara424c3382016-07-15 12:43:32 +01001415 struct kvm *kvm = kvm_dev->kvm;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001416 struct vgic_its *its = kvm_dev->private;
Andre Przywara424c3382016-07-15 12:43:32 +01001417 struct its_device *dev;
1418 struct its_itte *itte;
1419 struct list_head *dev_cur, *dev_temp;
1420 struct list_head *cur, *temp;
1421
1422 /*
1423 * We may end up here without the lists ever having been initialized.
1424 * Check this and bail out early to avoid dereferencing a NULL pointer.
1425 */
1426 if (!its->device_list.next)
1427 return;
1428
1429 mutex_lock(&its->its_lock);
1430 list_for_each_safe(dev_cur, dev_temp, &its->device_list) {
1431 dev = container_of(dev_cur, struct its_device, dev_list);
1432 list_for_each_safe(cur, temp, &dev->itt_head) {
1433 itte = (container_of(cur, struct its_itte, itte_list));
1434 its_free_itte(kvm, itte);
1435 }
1436 list_del(dev_cur);
1437 kfree(dev);
1438 }
1439
1440 list_for_each_safe(cur, temp, &its->collection_list) {
1441 list_del(cur);
1442 kfree(container_of(cur, struct its_collection, coll_list));
1443 }
1444 mutex_unlock(&its->its_lock);
Andre Przywara1085fdc2016-07-15 12:43:31 +01001445
1446 kfree(its);
1447}
1448
1449static int vgic_its_has_attr(struct kvm_device *dev,
1450 struct kvm_device_attr *attr)
1451{
1452 switch (attr->group) {
1453 case KVM_DEV_ARM_VGIC_GRP_ADDR:
1454 switch (attr->attr) {
1455 case KVM_VGIC_ITS_ADDR_TYPE:
1456 return 0;
1457 }
1458 break;
1459 case KVM_DEV_ARM_VGIC_GRP_CTRL:
1460 switch (attr->attr) {
1461 case KVM_DEV_ARM_VGIC_CTRL_INIT:
1462 return 0;
1463 }
1464 break;
1465 }
1466 return -ENXIO;
1467}
1468
1469static int vgic_its_set_attr(struct kvm_device *dev,
1470 struct kvm_device_attr *attr)
1471{
1472 struct vgic_its *its = dev->private;
1473 int ret;
1474
1475 switch (attr->group) {
1476 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
1477 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1478 unsigned long type = (unsigned long)attr->attr;
1479 u64 addr;
1480
1481 if (type != KVM_VGIC_ITS_ADDR_TYPE)
1482 return -ENODEV;
1483
Andre Przywara1085fdc2016-07-15 12:43:31 +01001484 if (copy_from_user(&addr, uaddr, sizeof(addr)))
1485 return -EFAULT;
1486
1487 ret = vgic_check_ioaddr(dev->kvm, &its->vgic_its_base,
1488 addr, SZ_64K);
1489 if (ret)
1490 return ret;
1491
1492 its->vgic_its_base = addr;
1493
1494 return 0;
1495 }
1496 case KVM_DEV_ARM_VGIC_GRP_CTRL:
1497 switch (attr->attr) {
1498 case KVM_DEV_ARM_VGIC_CTRL_INIT:
Andre Przywarac7735762016-08-08 16:45:43 +01001499 its->initialized = true;
1500
1501 return 0;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001502 }
1503 break;
1504 }
1505 return -ENXIO;
1506}
1507
1508static int vgic_its_get_attr(struct kvm_device *dev,
1509 struct kvm_device_attr *attr)
1510{
1511 switch (attr->group) {
1512 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
1513 struct vgic_its *its = dev->private;
1514 u64 addr = its->vgic_its_base;
1515 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1516 unsigned long type = (unsigned long)attr->attr;
1517
1518 if (type != KVM_VGIC_ITS_ADDR_TYPE)
1519 return -ENODEV;
1520
1521 if (copy_to_user(uaddr, &addr, sizeof(addr)))
1522 return -EFAULT;
1523 break;
1524 default:
1525 return -ENXIO;
1526 }
1527 }
1528
1529 return 0;
1530}
1531
1532static struct kvm_device_ops kvm_arm_vgic_its_ops = {
1533 .name = "kvm-arm-vgic-its",
1534 .create = vgic_its_create,
1535 .destroy = vgic_its_destroy,
1536 .set_attr = vgic_its_set_attr,
1537 .get_attr = vgic_its_get_attr,
1538 .has_attr = vgic_its_has_attr,
1539};
1540
1541int kvm_vgic_register_its_device(void)
1542{
1543 return kvm_register_device_ops(&kvm_arm_vgic_its_ops,
1544 KVM_DEV_TYPE_ARM_VGIC_ITS);
1545}
Andre Przywarac7735762016-08-08 16:45:43 +01001546
1547/*
1548 * Registers all ITSes with the kvm_io_bus framework.
1549 * To follow the existing VGIC initialization sequence, this has to be
1550 * done as late as possible, just before the first VCPU runs.
1551 */
1552int vgic_register_its_iodevs(struct kvm *kvm)
1553{
1554 struct kvm_device *dev;
1555 int ret = 0;
1556
1557 list_for_each_entry(dev, &kvm->devices, vm_node) {
1558 if (dev->ops != &kvm_arm_vgic_its_ops)
1559 continue;
1560
1561 ret = vgic_register_its_iodev(kvm, dev->private);
1562 if (ret)
1563 return ret;
1564 /*
1565 * We don't need to care about tearing down previously
1566 * registered ITSes, as the kvm_io_bus framework removes
1567 * them for us if the VM gets destroyed.
1568 */
1569 }
1570
1571 return ret;
1572}