blob: ebcaf4641d2b7c1f69378c83f61a1afee0b468cf [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
Andre Przywara424c3382016-07-15 12:43:32 +0100363static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,
364 struct vgic_its *its,
365 gpa_t addr, unsigned int len)
366{
367 u64 reg = GITS_TYPER_PLPIS;
368
369 /*
370 * We use linear CPU numbers for redistributor addressing,
371 * so GITS_TYPER.PTA is 0.
372 * Also we force all PROPBASER registers to be the same, so
373 * CommonLPIAff is 0 as well.
374 * To avoid memory waste in the guest, we keep the number of IDBits and
375 * DevBits low - as least for the time being.
376 */
377 reg |= 0x0f << GITS_TYPER_DEVBITS_SHIFT;
378 reg |= 0x0f << GITS_TYPER_IDBITS_SHIFT;
379
380 return extract_bytes(reg, addr & 7, len);
381}
382
383static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm,
384 struct vgic_its *its,
385 gpa_t addr, unsigned int len)
386{
387 return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
388}
389
390static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm,
391 struct vgic_its *its,
392 gpa_t addr, unsigned int len)
393{
394 switch (addr & 0xffff) {
395 case GITS_PIDR0:
396 return 0x92; /* part number, bits[7:0] */
397 case GITS_PIDR1:
398 return 0xb4; /* part number, bits[11:8] */
399 case GITS_PIDR2:
400 return GIC_PIDR2_ARCH_GICv3 | 0x0b;
401 case GITS_PIDR4:
402 return 0x40; /* This is a 64K software visible page */
403 /* The following are the ID registers for (any) GIC. */
404 case GITS_CIDR0:
405 return 0x0d;
406 case GITS_CIDR1:
407 return 0xf0;
408 case GITS_CIDR2:
409 return 0x05;
410 case GITS_CIDR3:
411 return 0xb1;
412 }
413
414 return 0;
415}
416
Andre Przywara2891a7d2016-07-15 12:43:37 +0100417/*
418 * Find the target VCPU and the LPI number for a given devid/eventid pair
419 * and make this IRQ pending, possibly injecting it.
420 * Must be called with the its_lock mutex held.
Andre Przywarafd837b02016-08-08 17:29:28 +0100421 * Returns 0 on success, a positive error value for any ITS mapping
422 * related errors and negative error values for generic errors.
Andre Przywara2891a7d2016-07-15 12:43:37 +0100423 */
Andre Przywarafd837b02016-08-08 17:29:28 +0100424static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its,
425 u32 devid, u32 eventid)
Andre Przywara2891a7d2016-07-15 12:43:37 +0100426{
Andre Przywarafd837b02016-08-08 17:29:28 +0100427 struct kvm_vcpu *vcpu;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100428 struct its_itte *itte;
429
430 if (!its->enabled)
Andre Przywarafd837b02016-08-08 17:29:28 +0100431 return -EBUSY;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100432
433 itte = find_itte(its, devid, eventid);
Andre Przywarafd837b02016-08-08 17:29:28 +0100434 if (!itte || !its_is_collection_mapped(itte->collection))
435 return E_ITS_INT_UNMAPPED_INTERRUPT;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100436
Andre Przywarafd837b02016-08-08 17:29:28 +0100437 vcpu = kvm_get_vcpu(kvm, itte->collection->target_addr);
438 if (!vcpu)
439 return E_ITS_INT_UNMAPPED_INTERRUPT;
440
441 if (!vcpu->arch.vgic_cpu.lpis_enabled)
442 return -EBUSY;
443
444 spin_lock(&itte->irq->irq_lock);
445 itte->irq->pending = true;
446 vgic_queue_irq_unlock(kvm, itte->irq);
447
448 return 0;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100449}
450
Andre Przywara505a19e2016-08-09 10:54:29 +0100451static struct vgic_io_device *vgic_get_its_iodev(struct kvm_io_device *dev)
452{
453 struct vgic_io_device *iodev;
454
455 if (dev->ops != &kvm_io_gic_ops)
456 return NULL;
457
458 iodev = container_of(dev, struct vgic_io_device, dev);
459
460 if (iodev->iodev_type != IODEV_ITS)
461 return NULL;
462
463 return iodev;
464}
465
Andre Przywara2891a7d2016-07-15 12:43:37 +0100466/*
467 * Queries the KVM IO bus framework to get the ITS pointer from the given
468 * doorbell address.
469 * We then call vgic_its_trigger_msi() with the decoded data.
Andre Przywarafd837b02016-08-08 17:29:28 +0100470 * According to the KVM_SIGNAL_MSI API description returns 1 on success.
Andre Przywara2891a7d2016-07-15 12:43:37 +0100471 */
472int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi)
473{
474 u64 address;
475 struct kvm_io_device *kvm_io_dev;
476 struct vgic_io_device *iodev;
Andre Przywarafd837b02016-08-08 17:29:28 +0100477 int ret;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100478
479 if (!vgic_has_its(kvm))
480 return -ENODEV;
481
482 if (!(msi->flags & KVM_MSI_VALID_DEVID))
483 return -EINVAL;
484
485 address = (u64)msi->address_hi << 32 | msi->address_lo;
486
487 kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, address);
488 if (!kvm_io_dev)
Andre Przywara505a19e2016-08-09 10:54:29 +0100489 return -EINVAL;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100490
Andre Przywara505a19e2016-08-09 10:54:29 +0100491 iodev = vgic_get_its_iodev(kvm_io_dev);
492 if (!iodev)
493 return -EINVAL;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100494
495 mutex_lock(&iodev->its->its_lock);
Andre Przywarafd837b02016-08-08 17:29:28 +0100496 ret = vgic_its_trigger_msi(kvm, iodev->its, msi->devid, msi->data);
Andre Przywara2891a7d2016-07-15 12:43:37 +0100497 mutex_unlock(&iodev->its->its_lock);
498
Andre Przywarafd837b02016-08-08 17:29:28 +0100499 if (ret < 0)
500 return ret;
501
502 /*
503 * KVM_SIGNAL_MSI demands a return value > 0 for success and 0
504 * if the guest has blocked the MSI. So we map any LPI mapping
505 * related error to that.
506 */
507 if (ret)
508 return 0;
509 else
510 return 1;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100511}
512
Andre Przywara424c3382016-07-15 12:43:32 +0100513/* Requires the its_lock to be held. */
514static void its_free_itte(struct kvm *kvm, struct its_itte *itte)
515{
516 list_del(&itte->itte_list);
Andre Przywara38024112016-07-15 12:43:33 +0100517
518 /* This put matches the get in vgic_add_lpi. */
Christoffer Dall99e5e882016-08-01 20:25:33 +0200519 if (itte->irq)
520 vgic_put_irq(kvm, itte->irq);
Andre Przywara38024112016-07-15 12:43:33 +0100521
Andre Przywara424c3382016-07-15 12:43:32 +0100522 kfree(itte);
523}
524
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100525static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size)
526{
527 return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT_ULL(size) - 1);
528}
529
530#define its_cmd_get_command(cmd) its_cmd_mask_field(cmd, 0, 0, 8)
531#define its_cmd_get_deviceid(cmd) its_cmd_mask_field(cmd, 0, 32, 32)
532#define its_cmd_get_id(cmd) its_cmd_mask_field(cmd, 1, 0, 32)
533#define its_cmd_get_physical_id(cmd) its_cmd_mask_field(cmd, 1, 32, 32)
534#define its_cmd_get_collection(cmd) its_cmd_mask_field(cmd, 2, 0, 16)
535#define its_cmd_get_target_addr(cmd) its_cmd_mask_field(cmd, 2, 16, 32)
536#define its_cmd_get_validbit(cmd) its_cmd_mask_field(cmd, 2, 63, 1)
537
538/*
539 * The DISCARD command frees an Interrupt Translation Table Entry (ITTE).
540 * Must be called with the its_lock mutex held.
541 */
542static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its,
543 u64 *its_cmd)
544{
545 u32 device_id = its_cmd_get_deviceid(its_cmd);
546 u32 event_id = its_cmd_get_id(its_cmd);
547 struct its_itte *itte;
548
549
550 itte = find_itte(its, device_id, event_id);
551 if (itte && itte->collection) {
552 /*
553 * Though the spec talks about removing the pending state, we
554 * don't bother here since we clear the ITTE anyway and the
555 * pending state is a property of the ITTE struct.
556 */
557 its_free_itte(kvm, itte);
558 return 0;
559 }
560
561 return E_ITS_DISCARD_UNMAPPED_INTERRUPT;
562}
563
564/*
565 * The MOVI command moves an ITTE to a different collection.
566 * Must be called with the its_lock mutex held.
567 */
568static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,
569 u64 *its_cmd)
570{
571 u32 device_id = its_cmd_get_deviceid(its_cmd);
572 u32 event_id = its_cmd_get_id(its_cmd);
573 u32 coll_id = its_cmd_get_collection(its_cmd);
574 struct kvm_vcpu *vcpu;
575 struct its_itte *itte;
576 struct its_collection *collection;
577
578 itte = find_itte(its, device_id, event_id);
579 if (!itte)
580 return E_ITS_MOVI_UNMAPPED_INTERRUPT;
581
582 if (!its_is_collection_mapped(itte->collection))
583 return E_ITS_MOVI_UNMAPPED_COLLECTION;
584
585 collection = find_collection(its, coll_id);
586 if (!its_is_collection_mapped(collection))
587 return E_ITS_MOVI_UNMAPPED_COLLECTION;
588
589 itte->collection = collection;
590 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
591
592 spin_lock(&itte->irq->irq_lock);
593 itte->irq->target_vcpu = vcpu;
594 spin_unlock(&itte->irq->irq_lock);
595
596 return 0;
597}
598
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100599/*
600 * Check whether an ID can be stored into the corresponding guest table.
601 * For a direct table this is pretty easy, but gets a bit nasty for
602 * indirect tables. We check whether the resulting guest physical address
603 * is actually valid (covered by a memslot and guest accessbible).
604 * For this we have to read the respective first level entry.
605 */
606static bool vgic_its_check_id(struct vgic_its *its, u64 baser, int id)
607{
608 int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
609 int index;
610 u64 indirect_ptr;
611 gfn_t gfn;
612
613 if (!(baser & GITS_BASER_INDIRECT)) {
614 phys_addr_t addr;
615
616 if (id >= (l1_tbl_size / GITS_BASER_ENTRY_SIZE(baser)))
617 return false;
618
619 addr = BASER_ADDRESS(baser) + id * GITS_BASER_ENTRY_SIZE(baser);
620 gfn = addr >> PAGE_SHIFT;
621
622 return kvm_is_visible_gfn(its->dev->kvm, gfn);
623 }
624
625 /* calculate and check the index into the 1st level */
626 index = id / (SZ_64K / GITS_BASER_ENTRY_SIZE(baser));
627 if (index >= (l1_tbl_size / sizeof(u64)))
628 return false;
629
630 /* Each 1st level entry is represented by a 64-bit value. */
631 if (kvm_read_guest(its->dev->kvm,
632 BASER_ADDRESS(baser) + index * sizeof(indirect_ptr),
633 &indirect_ptr, sizeof(indirect_ptr)))
634 return false;
635
636 indirect_ptr = le64_to_cpu(indirect_ptr);
637
638 /* check the valid bit of the first level entry */
639 if (!(indirect_ptr & BIT_ULL(63)))
640 return false;
641
642 /*
643 * Mask the guest physical address and calculate the frame number.
644 * Any address beyond our supported 48 bits of PA will be caught
645 * by the actual check in the final step.
646 */
647 indirect_ptr &= GENMASK_ULL(51, 16);
648
649 /* Find the address of the actual entry */
650 index = id % (SZ_64K / GITS_BASER_ENTRY_SIZE(baser));
651 indirect_ptr += index * GITS_BASER_ENTRY_SIZE(baser);
652 gfn = indirect_ptr >> PAGE_SHIFT;
653
654 return kvm_is_visible_gfn(its->dev->kvm, gfn);
655}
656
Marc Zyngier17a21f52016-07-17 20:01:46 +0100657static int vgic_its_alloc_collection(struct vgic_its *its,
658 struct its_collection **colp,
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100659 u32 coll_id)
660{
Marc Zyngier17a21f52016-07-17 20:01:46 +0100661 struct its_collection *collection;
662
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100663 if (!vgic_its_check_id(its, its->baser_coll_table, coll_id))
664 return E_ITS_MAPC_COLLECTION_OOR;
665
Marc Zyngier17a21f52016-07-17 20:01:46 +0100666 collection = kzalloc(sizeof(*collection), GFP_KERNEL);
Marc Zyngier7df3dbe2017-11-16 17:58:18 +0000667 if (!collection)
668 return -ENOMEM;
Marc Zyngier17a21f52016-07-17 20:01:46 +0100669
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100670 collection->collection_id = coll_id;
671 collection->target_addr = COLLECTION_NOT_MAPPED;
672
673 list_add_tail(&collection->coll_list, &its->collection_list);
Marc Zyngier17a21f52016-07-17 20:01:46 +0100674 *colp = collection;
675
676 return 0;
677}
678
679static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)
680{
681 struct its_collection *collection;
682 struct its_device *device;
683 struct its_itte *itte;
684
685 /*
686 * Clearing the mapping for that collection ID removes the
687 * entry from the list. If there wasn't any before, we can
688 * go home early.
689 */
690 collection = find_collection(its, coll_id);
691 if (!collection)
692 return;
693
694 for_each_lpi_its(device, itte, its)
695 if (itte->collection &&
696 itte->collection->collection_id == coll_id)
697 itte->collection = NULL;
698
699 list_del(&collection->coll_list);
700 kfree(collection);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100701}
702
703/*
704 * The MAPTI and MAPI commands map LPIs to ITTEs.
705 * Must be called with its_lock mutex held.
706 */
707static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
Marc Zyngiera3e7aa22016-07-17 22:38:32 +0100708 u64 *its_cmd)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100709{
710 u32 device_id = its_cmd_get_deviceid(its_cmd);
711 u32 event_id = its_cmd_get_id(its_cmd);
712 u32 coll_id = its_cmd_get_collection(its_cmd);
Andre Przywara286054a2016-08-16 17:51:06 +0100713 struct its_itte *itte;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100714 struct its_device *device;
715 struct its_collection *collection, *new_coll = NULL;
716 int lpi_nr;
Christoffer Dall99e5e882016-08-01 20:25:33 +0200717 struct vgic_irq *irq;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100718
719 device = find_its_device(its, device_id);
720 if (!device)
721 return E_ITS_MAPTI_UNMAPPED_DEVICE;
722
Marc Zyngiera3e7aa22016-07-17 22:38:32 +0100723 if (its_cmd_get_command(its_cmd) == GITS_CMD_MAPTI)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100724 lpi_nr = its_cmd_get_physical_id(its_cmd);
725 else
726 lpi_nr = event_id;
727 if (lpi_nr < GIC_LPI_OFFSET ||
Marc Zyngier3a88bde2016-07-18 16:27:14 +0100728 lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser))
729 return E_ITS_MAPTI_PHYSICALID_OOR;
730
Andre Przywara286054a2016-08-16 17:51:06 +0100731 /* If there is an existing mapping, behavior is UNPREDICTABLE. */
732 if (find_itte(its, device_id, event_id))
733 return 0;
734
Marc Zyngier3a88bde2016-07-18 16:27:14 +0100735 collection = find_collection(its, coll_id);
736 if (!collection) {
737 int ret = vgic_its_alloc_collection(its, &collection, coll_id);
738 if (ret)
739 return ret;
740 new_coll = collection;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100741 }
742
Andre Przywara286054a2016-08-16 17:51:06 +0100743 itte = kzalloc(sizeof(struct its_itte), GFP_KERNEL);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100744 if (!itte) {
Andre Przywara286054a2016-08-16 17:51:06 +0100745 if (new_coll)
746 vgic_its_free_collection(its, coll_id);
747 return -ENOMEM;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100748 }
749
Andre Przywara286054a2016-08-16 17:51:06 +0100750 itte->event_id = event_id;
751 list_add_tail(&itte->itte_list, &device->itt_head);
752
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100753 itte->collection = collection;
754 itte->lpi = lpi_nr;
Christoffer Dall99e5e882016-08-01 20:25:33 +0200755
756 irq = vgic_add_lpi(kvm, lpi_nr);
757 if (IS_ERR(irq)) {
758 if (new_coll)
759 vgic_its_free_collection(its, coll_id);
Andre Przywara286054a2016-08-16 17:51:06 +0100760 its_free_itte(kvm, itte);
Christoffer Dall99e5e882016-08-01 20:25:33 +0200761 return PTR_ERR(irq);
762 }
763 itte->irq = irq;
764
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100765 update_affinity_itte(kvm, itte);
766
767 /*
768 * We "cache" the configuration table entries in out struct vgic_irq's.
769 * However we only have those structs for mapped IRQs, so we read in
770 * the respective config data from memory here upon mapping the LPI.
771 */
772 update_lpi_config(kvm, itte->irq, NULL);
773
774 return 0;
775}
776
777/* Requires the its_lock to be held. */
778static void vgic_its_unmap_device(struct kvm *kvm, struct its_device *device)
779{
780 struct its_itte *itte, *temp;
781
782 /*
783 * The spec says that unmapping a device with still valid
784 * ITTEs associated is UNPREDICTABLE. We remove all ITTEs,
785 * since we cannot leave the memory unreferenced.
786 */
787 list_for_each_entry_safe(itte, temp, &device->itt_head, itte_list)
788 its_free_itte(kvm, itte);
789
790 list_del(&device->dev_list);
791 kfree(device);
792}
793
794/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100795 * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs).
796 * Must be called with the its_lock mutex held.
797 */
798static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
799 u64 *its_cmd)
800{
801 u32 device_id = its_cmd_get_deviceid(its_cmd);
802 bool valid = its_cmd_get_validbit(its_cmd);
803 struct its_device *device;
804
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100805 if (!vgic_its_check_id(its, its->baser_device_table, device_id))
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100806 return E_ITS_MAPD_DEVICE_OOR;
807
808 device = find_its_device(its, device_id);
809
810 /*
811 * The spec says that calling MAPD on an already mapped device
812 * invalidates all cached data for this device. We implement this
813 * by removing the mapping and re-establishing it.
814 */
815 if (device)
816 vgic_its_unmap_device(kvm, device);
817
818 /*
819 * The spec does not say whether unmapping a not-mapped device
820 * is an error, so we are done in any case.
821 */
822 if (!valid)
823 return 0;
824
825 device = kzalloc(sizeof(struct its_device), GFP_KERNEL);
826 if (!device)
827 return -ENOMEM;
828
829 device->device_id = device_id;
830 INIT_LIST_HEAD(&device->itt_head);
831
832 list_add_tail(&device->dev_list, &its->device_list);
833
834 return 0;
835}
836
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100837/*
838 * The MAPC command maps collection IDs to redistributors.
839 * Must be called with the its_lock mutex held.
840 */
841static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its,
842 u64 *its_cmd)
843{
844 u16 coll_id;
845 u32 target_addr;
846 struct its_collection *collection;
847 bool valid;
848
849 valid = its_cmd_get_validbit(its_cmd);
850 coll_id = its_cmd_get_collection(its_cmd);
851 target_addr = its_cmd_get_target_addr(its_cmd);
852
853 if (target_addr >= atomic_read(&kvm->online_vcpus))
854 return E_ITS_MAPC_PROCNUM_OOR;
855
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100856 if (!valid) {
Marc Zyngier17a21f52016-07-17 20:01:46 +0100857 vgic_its_free_collection(its, coll_id);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100858 } else {
Marc Zyngier17a21f52016-07-17 20:01:46 +0100859 collection = find_collection(its, coll_id);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100860
Marc Zyngier17a21f52016-07-17 20:01:46 +0100861 if (!collection) {
862 int ret;
863
864 ret = vgic_its_alloc_collection(its, &collection,
865 coll_id);
866 if (ret)
867 return ret;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100868 collection->target_addr = target_addr;
869 } else {
870 collection->target_addr = target_addr;
871 update_affinity_collection(kvm, its, collection);
872 }
873 }
874
875 return 0;
876}
877
878/*
879 * The CLEAR command removes the pending state for a particular LPI.
880 * Must be called with the its_lock mutex held.
881 */
882static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its,
883 u64 *its_cmd)
884{
885 u32 device_id = its_cmd_get_deviceid(its_cmd);
886 u32 event_id = its_cmd_get_id(its_cmd);
887 struct its_itte *itte;
888
889
890 itte = find_itte(its, device_id, event_id);
891 if (!itte)
892 return E_ITS_CLEAR_UNMAPPED_INTERRUPT;
893
894 itte->irq->pending = false;
895
896 return 0;
897}
898
899/*
900 * The INV command syncs the configuration bits from the memory table.
901 * Must be called with the its_lock mutex held.
902 */
903static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its,
904 u64 *its_cmd)
905{
906 u32 device_id = its_cmd_get_deviceid(its_cmd);
907 u32 event_id = its_cmd_get_id(its_cmd);
908 struct its_itte *itte;
909
910
911 itte = find_itte(its, device_id, event_id);
912 if (!itte)
913 return E_ITS_INV_UNMAPPED_INTERRUPT;
914
915 return update_lpi_config(kvm, itte->irq, NULL);
916}
917
918/*
919 * The INVALL command requests flushing of all IRQ data in this collection.
920 * Find the VCPU mapped to that collection, then iterate over the VM's list
921 * of mapped LPIs and update the configuration for each IRQ which targets
922 * the specified vcpu. The configuration will be read from the in-memory
923 * configuration table.
924 * Must be called with the its_lock mutex held.
925 */
926static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its,
927 u64 *its_cmd)
928{
929 u32 coll_id = its_cmd_get_collection(its_cmd);
930 struct its_collection *collection;
931 struct kvm_vcpu *vcpu;
932 struct vgic_irq *irq;
933 u32 *intids;
934 int irq_count, i;
935
936 collection = find_collection(its, coll_id);
937 if (!its_is_collection_mapped(collection))
938 return E_ITS_INVALL_UNMAPPED_COLLECTION;
939
940 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
941
942 irq_count = vgic_copy_lpi_list(kvm, &intids);
943 if (irq_count < 0)
944 return irq_count;
945
946 for (i = 0; i < irq_count; i++) {
947 irq = vgic_get_irq(kvm, NULL, intids[i]);
948 if (!irq)
949 continue;
950 update_lpi_config(kvm, irq, vcpu);
951 vgic_put_irq(kvm, irq);
952 }
953
954 kfree(intids);
955
956 return 0;
957}
958
959/*
960 * The MOVALL command moves the pending state of all IRQs targeting one
961 * redistributor to another. We don't hold the pending state in the VCPUs,
962 * but in the IRQs instead, so there is really not much to do for us here.
963 * However the spec says that no IRQ must target the old redistributor
964 * afterwards, so we make sure that no LPI is using the associated target_vcpu.
965 * This command affects all LPIs in the system that target that redistributor.
966 */
967static int vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its,
968 u64 *its_cmd)
969{
970 struct vgic_dist *dist = &kvm->arch.vgic;
971 u32 target1_addr = its_cmd_get_target_addr(its_cmd);
972 u32 target2_addr = its_cmd_mask_field(its_cmd, 3, 16, 32);
973 struct kvm_vcpu *vcpu1, *vcpu2;
974 struct vgic_irq *irq;
975
976 if (target1_addr >= atomic_read(&kvm->online_vcpus) ||
977 target2_addr >= atomic_read(&kvm->online_vcpus))
978 return E_ITS_MOVALL_PROCNUM_OOR;
979
980 if (target1_addr == target2_addr)
981 return 0;
982
983 vcpu1 = kvm_get_vcpu(kvm, target1_addr);
984 vcpu2 = kvm_get_vcpu(kvm, target2_addr);
985
986 spin_lock(&dist->lpi_list_lock);
987
988 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
989 spin_lock(&irq->irq_lock);
990
991 if (irq->target_vcpu == vcpu1)
992 irq->target_vcpu = vcpu2;
993
994 spin_unlock(&irq->irq_lock);
995 }
996
997 spin_unlock(&dist->lpi_list_lock);
998
999 return 0;
1000}
1001
1002/*
Andre Przywara2891a7d2016-07-15 12:43:37 +01001003 * The INT command injects the LPI associated with that DevID/EvID pair.
1004 * Must be called with the its_lock mutex held.
1005 */
1006static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its,
1007 u64 *its_cmd)
1008{
1009 u32 msi_data = its_cmd_get_id(its_cmd);
1010 u64 msi_devid = its_cmd_get_deviceid(its_cmd);
1011
Andre Przywarafd837b02016-08-08 17:29:28 +01001012 return vgic_its_trigger_msi(kvm, its, msi_devid, msi_data);
Andre Przywara2891a7d2016-07-15 12:43:37 +01001013}
1014
1015/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001016 * This function is called with the its_cmd lock held, but the ITS data
1017 * structure lock dropped.
1018 */
Andre Przywara424c3382016-07-15 12:43:32 +01001019static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its,
1020 u64 *its_cmd)
1021{
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001022 int ret = -ENODEV;
1023
1024 mutex_lock(&its->its_lock);
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001025 switch (its_cmd_get_command(its_cmd)) {
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001026 case GITS_CMD_MAPD:
1027 ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd);
1028 break;
1029 case GITS_CMD_MAPC:
1030 ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd);
1031 break;
1032 case GITS_CMD_MAPI:
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001033 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001034 break;
1035 case GITS_CMD_MAPTI:
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001036 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001037 break;
1038 case GITS_CMD_MOVI:
1039 ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd);
1040 break;
1041 case GITS_CMD_DISCARD:
1042 ret = vgic_its_cmd_handle_discard(kvm, its, its_cmd);
1043 break;
1044 case GITS_CMD_CLEAR:
1045 ret = vgic_its_cmd_handle_clear(kvm, its, its_cmd);
1046 break;
1047 case GITS_CMD_MOVALL:
1048 ret = vgic_its_cmd_handle_movall(kvm, its, its_cmd);
1049 break;
Andre Przywara2891a7d2016-07-15 12:43:37 +01001050 case GITS_CMD_INT:
1051 ret = vgic_its_cmd_handle_int(kvm, its, its_cmd);
1052 break;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001053 case GITS_CMD_INV:
1054 ret = vgic_its_cmd_handle_inv(kvm, its, its_cmd);
1055 break;
1056 case GITS_CMD_INVALL:
1057 ret = vgic_its_cmd_handle_invall(kvm, its, its_cmd);
1058 break;
1059 case GITS_CMD_SYNC:
1060 /* we ignore this command: we are in sync all of the time */
1061 ret = 0;
1062 break;
1063 }
1064 mutex_unlock(&its->its_lock);
1065
1066 return ret;
Andre Przywara424c3382016-07-15 12:43:32 +01001067}
1068
1069static u64 vgic_sanitise_its_baser(u64 reg)
1070{
1071 reg = vgic_sanitise_field(reg, GITS_BASER_SHAREABILITY_MASK,
1072 GITS_BASER_SHAREABILITY_SHIFT,
1073 vgic_sanitise_shareability);
1074 reg = vgic_sanitise_field(reg, GITS_BASER_INNER_CACHEABILITY_MASK,
1075 GITS_BASER_INNER_CACHEABILITY_SHIFT,
1076 vgic_sanitise_inner_cacheability);
1077 reg = vgic_sanitise_field(reg, GITS_BASER_OUTER_CACHEABILITY_MASK,
1078 GITS_BASER_OUTER_CACHEABILITY_SHIFT,
1079 vgic_sanitise_outer_cacheability);
1080
1081 /* Bits 15:12 contain bits 51:48 of the PA, which we don't support. */
1082 reg &= ~GENMASK_ULL(15, 12);
1083
1084 /* We support only one (ITS) page size: 64K */
1085 reg = (reg & ~GITS_BASER_PAGE_SIZE_MASK) | GITS_BASER_PAGE_SIZE_64K;
1086
1087 return reg;
1088}
1089
1090static u64 vgic_sanitise_its_cbaser(u64 reg)
1091{
1092 reg = vgic_sanitise_field(reg, GITS_CBASER_SHAREABILITY_MASK,
1093 GITS_CBASER_SHAREABILITY_SHIFT,
1094 vgic_sanitise_shareability);
1095 reg = vgic_sanitise_field(reg, GITS_CBASER_INNER_CACHEABILITY_MASK,
1096 GITS_CBASER_INNER_CACHEABILITY_SHIFT,
1097 vgic_sanitise_inner_cacheability);
1098 reg = vgic_sanitise_field(reg, GITS_CBASER_OUTER_CACHEABILITY_MASK,
1099 GITS_CBASER_OUTER_CACHEABILITY_SHIFT,
1100 vgic_sanitise_outer_cacheability);
1101
1102 /*
1103 * Sanitise the physical address to be 64k aligned.
1104 * Also limit the physical addresses to 48 bits.
1105 */
1106 reg &= ~(GENMASK_ULL(51, 48) | GENMASK_ULL(15, 12));
1107
1108 return reg;
1109}
1110
1111static unsigned long vgic_mmio_read_its_cbaser(struct kvm *kvm,
1112 struct vgic_its *its,
1113 gpa_t addr, unsigned int len)
1114{
1115 return extract_bytes(its->cbaser, addr & 7, len);
1116}
1117
1118static void vgic_mmio_write_its_cbaser(struct kvm *kvm, struct vgic_its *its,
1119 gpa_t addr, unsigned int len,
1120 unsigned long val)
1121{
1122 /* When GITS_CTLR.Enable is 1, this register is RO. */
1123 if (its->enabled)
1124 return;
1125
1126 mutex_lock(&its->cmd_lock);
1127 its->cbaser = update_64bit_reg(its->cbaser, addr & 7, len, val);
1128 its->cbaser = vgic_sanitise_its_cbaser(its->cbaser);
1129 its->creadr = 0;
1130 /*
1131 * CWRITER is architecturally UNKNOWN on reset, but we need to reset
1132 * it to CREADR to make sure we start with an empty command buffer.
1133 */
1134 its->cwriter = its->creadr;
1135 mutex_unlock(&its->cmd_lock);
1136}
1137
1138#define ITS_CMD_BUFFER_SIZE(baser) ((((baser) & 0xff) + 1) << 12)
1139#define ITS_CMD_SIZE 32
1140#define ITS_CMD_OFFSET(reg) ((reg) & GENMASK(19, 5))
1141
Andre Przywarab1f71142017-02-16 10:41:20 +00001142/* Must be called with the cmd_lock held. */
1143static void vgic_its_process_commands(struct kvm *kvm, struct vgic_its *its)
Andre Przywara424c3382016-07-15 12:43:32 +01001144{
1145 gpa_t cbaser;
1146 u64 cmd_buf[4];
Andre Przywara424c3382016-07-15 12:43:32 +01001147
Andre Przywarab1f71142017-02-16 10:41:20 +00001148 /* Commands are only processed when the ITS is enabled. */
1149 if (!its->enabled)
Andre Przywara424c3382016-07-15 12:43:32 +01001150 return;
1151
Andre Przywara424c3382016-07-15 12:43:32 +01001152 cbaser = CBASER_ADDRESS(its->cbaser);
1153
1154 while (its->cwriter != its->creadr) {
1155 int ret = kvm_read_guest(kvm, cbaser + its->creadr,
1156 cmd_buf, ITS_CMD_SIZE);
1157 /*
1158 * If kvm_read_guest() fails, this could be due to the guest
1159 * programming a bogus value in CBASER or something else going
1160 * wrong from which we cannot easily recover.
1161 * According to section 6.3.2 in the GICv3 spec we can just
1162 * ignore that command then.
1163 */
1164 if (!ret)
1165 vgic_its_handle_command(kvm, its, cmd_buf);
1166
1167 its->creadr += ITS_CMD_SIZE;
1168 if (its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser))
1169 its->creadr = 0;
1170 }
Andre Przywarab1f71142017-02-16 10:41:20 +00001171}
1172
1173/*
1174 * By writing to CWRITER the guest announces new commands to be processed.
1175 * To avoid any races in the first place, we take the its_cmd lock, which
1176 * protects our ring buffer variables, so that there is only one user
1177 * per ITS handling commands at a given time.
1178 */
1179static void vgic_mmio_write_its_cwriter(struct kvm *kvm, struct vgic_its *its,
1180 gpa_t addr, unsigned int len,
1181 unsigned long val)
1182{
1183 u64 reg;
1184
1185 if (!its)
1186 return;
1187
1188 mutex_lock(&its->cmd_lock);
1189
1190 reg = update_64bit_reg(its->cwriter, addr & 7, len, val);
1191 reg = ITS_CMD_OFFSET(reg);
1192 if (reg >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
1193 mutex_unlock(&its->cmd_lock);
1194 return;
1195 }
1196 its->cwriter = reg;
1197
1198 vgic_its_process_commands(kvm, its);
Andre Przywara424c3382016-07-15 12:43:32 +01001199
1200 mutex_unlock(&its->cmd_lock);
1201}
1202
1203static unsigned long vgic_mmio_read_its_cwriter(struct kvm *kvm,
1204 struct vgic_its *its,
1205 gpa_t addr, unsigned int len)
1206{
1207 return extract_bytes(its->cwriter, addr & 0x7, len);
1208}
1209
1210static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,
1211 struct vgic_its *its,
1212 gpa_t addr, unsigned int len)
1213{
1214 return extract_bytes(its->creadr, addr & 0x7, len);
1215}
1216
1217#define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7)
1218static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm,
1219 struct vgic_its *its,
1220 gpa_t addr, unsigned int len)
1221{
1222 u64 reg;
1223
1224 switch (BASER_INDEX(addr)) {
1225 case 0:
1226 reg = its->baser_device_table;
1227 break;
1228 case 1:
1229 reg = its->baser_coll_table;
1230 break;
1231 default:
1232 reg = 0;
1233 break;
1234 }
1235
1236 return extract_bytes(reg, addr & 7, len);
1237}
1238
1239#define GITS_BASER_RO_MASK (GENMASK_ULL(52, 48) | GENMASK_ULL(58, 56))
1240static void vgic_mmio_write_its_baser(struct kvm *kvm,
1241 struct vgic_its *its,
1242 gpa_t addr, unsigned int len,
1243 unsigned long val)
1244{
1245 u64 entry_size, device_type;
1246 u64 reg, *regptr, clearbits = 0;
1247
1248 /* When GITS_CTLR.Enable is 1, we ignore write accesses. */
1249 if (its->enabled)
1250 return;
1251
1252 switch (BASER_INDEX(addr)) {
1253 case 0:
1254 regptr = &its->baser_device_table;
1255 entry_size = 8;
1256 device_type = GITS_BASER_TYPE_DEVICE;
1257 break;
1258 case 1:
1259 regptr = &its->baser_coll_table;
1260 entry_size = 8;
1261 device_type = GITS_BASER_TYPE_COLLECTION;
1262 clearbits = GITS_BASER_INDIRECT;
1263 break;
1264 default:
1265 return;
1266 }
1267
1268 reg = update_64bit_reg(*regptr, addr & 7, len, val);
1269 reg &= ~GITS_BASER_RO_MASK;
1270 reg &= ~clearbits;
1271
1272 reg |= (entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
1273 reg |= device_type << GITS_BASER_TYPE_SHIFT;
1274 reg = vgic_sanitise_its_baser(reg);
1275
1276 *regptr = reg;
1277}
1278
Andre Przywarab1f71142017-02-16 10:41:20 +00001279static unsigned long vgic_mmio_read_its_ctlr(struct kvm *vcpu,
1280 struct vgic_its *its,
1281 gpa_t addr, unsigned int len)
1282{
1283 u32 reg = 0;
1284
1285 mutex_lock(&its->cmd_lock);
1286 if (its->creadr == its->cwriter)
1287 reg |= GITS_CTLR_QUIESCENT;
1288 if (its->enabled)
1289 reg |= GITS_CTLR_ENABLE;
1290 mutex_unlock(&its->cmd_lock);
1291
1292 return reg;
1293}
1294
1295static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its,
1296 gpa_t addr, unsigned int len,
1297 unsigned long val)
1298{
1299 mutex_lock(&its->cmd_lock);
1300
1301 its->enabled = !!(val & GITS_CTLR_ENABLE);
1302
1303 /*
1304 * Try to process any pending commands. This function bails out early
1305 * if the ITS is disabled or no commands have been queued.
1306 */
1307 vgic_its_process_commands(kvm, its);
1308
1309 mutex_unlock(&its->cmd_lock);
1310}
1311
Andre Przywara59c5ab42016-07-15 12:43:30 +01001312#define REGISTER_ITS_DESC(off, rd, wr, length, acc) \
1313{ \
1314 .reg_offset = off, \
1315 .len = length, \
1316 .access_flags = acc, \
1317 .its_read = rd, \
1318 .its_write = wr, \
1319}
1320
Andre Przywara59c5ab42016-07-15 12:43:30 +01001321static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its,
1322 gpa_t addr, unsigned int len, unsigned long val)
1323{
1324 /* Ignore */
1325}
1326
1327static struct vgic_register_region its_registers[] = {
1328 REGISTER_ITS_DESC(GITS_CTLR,
Andre Przywara424c3382016-07-15 12:43:32 +01001329 vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001330 VGIC_ACCESS_32bit),
1331 REGISTER_ITS_DESC(GITS_IIDR,
Andre Przywara424c3382016-07-15 12:43:32 +01001332 vgic_mmio_read_its_iidr, its_mmio_write_wi, 4,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001333 VGIC_ACCESS_32bit),
1334 REGISTER_ITS_DESC(GITS_TYPER,
Andre Przywara424c3382016-07-15 12:43:32 +01001335 vgic_mmio_read_its_typer, its_mmio_write_wi, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001336 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1337 REGISTER_ITS_DESC(GITS_CBASER,
Andre Przywara424c3382016-07-15 12:43:32 +01001338 vgic_mmio_read_its_cbaser, vgic_mmio_write_its_cbaser, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001339 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1340 REGISTER_ITS_DESC(GITS_CWRITER,
Andre Przywara424c3382016-07-15 12:43:32 +01001341 vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001342 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1343 REGISTER_ITS_DESC(GITS_CREADR,
Andre Przywara424c3382016-07-15 12:43:32 +01001344 vgic_mmio_read_its_creadr, its_mmio_write_wi, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001345 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1346 REGISTER_ITS_DESC(GITS_BASER,
Andre Przywara424c3382016-07-15 12:43:32 +01001347 vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001348 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1349 REGISTER_ITS_DESC(GITS_IDREGS_BASE,
Andre Przywara424c3382016-07-15 12:43:32 +01001350 vgic_mmio_read_its_idregs, its_mmio_write_wi, 0x30,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001351 VGIC_ACCESS_32bit),
1352};
1353
Andre Przywara33d3bc92016-07-15 12:43:34 +01001354/* This is called on setting the LPI enable bit in the redistributor. */
1355void vgic_enable_lpis(struct kvm_vcpu *vcpu)
1356{
1357 if (!(vcpu->arch.vgic_cpu.pendbaser & GICR_PENDBASER_PTZ))
1358 its_sync_lpi_pending_table(vcpu);
1359}
1360
Andre Przywarac7735762016-08-08 16:45:43 +01001361static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its)
Andre Przywara59c5ab42016-07-15 12:43:30 +01001362{
1363 struct vgic_io_device *iodev = &its->iodev;
1364 int ret;
1365
Andre Przywarac7735762016-08-08 16:45:43 +01001366 if (!its->initialized)
1367 return -EBUSY;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001368
Andre Przywara59c5ab42016-07-15 12:43:30 +01001369 if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base))
1370 return -ENXIO;
1371
1372 iodev->regions = its_registers;
1373 iodev->nr_regions = ARRAY_SIZE(its_registers);
1374 kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops);
1375
1376 iodev->base_addr = its->vgic_its_base;
1377 iodev->iodev_type = IODEV_ITS;
1378 iodev->its = its;
1379 mutex_lock(&kvm->slots_lock);
1380 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr,
1381 KVM_VGIC_V3_ITS_SIZE, &iodev->dev);
1382 mutex_unlock(&kvm->slots_lock);
1383
1384 return ret;
1385}
Andre Przywara1085fdc2016-07-15 12:43:31 +01001386
Andre Przywara424c3382016-07-15 12:43:32 +01001387#define INITIAL_BASER_VALUE \
1388 (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb) | \
1389 GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner) | \
1390 GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable) | \
1391 ((8ULL - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) | \
1392 GITS_BASER_PAGE_SIZE_64K)
1393
1394#define INITIAL_PROPBASER_VALUE \
1395 (GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWb) | \
1396 GIC_BASER_CACHEABILITY(GICR_PROPBASER, OUTER, SameAsInner) | \
1397 GIC_BASER_SHAREABILITY(GICR_PROPBASER, InnerShareable))
1398
Andre Przywara1085fdc2016-07-15 12:43:31 +01001399static int vgic_its_create(struct kvm_device *dev, u32 type)
1400{
1401 struct vgic_its *its;
1402
1403 if (type != KVM_DEV_TYPE_ARM_VGIC_ITS)
1404 return -ENODEV;
1405
1406 its = kzalloc(sizeof(struct vgic_its), GFP_KERNEL);
1407 if (!its)
1408 return -ENOMEM;
1409
Andre Przywara424c3382016-07-15 12:43:32 +01001410 mutex_init(&its->its_lock);
1411 mutex_init(&its->cmd_lock);
1412
Andre Przywara1085fdc2016-07-15 12:43:31 +01001413 its->vgic_its_base = VGIC_ADDR_UNDEF;
1414
Andre Przywara424c3382016-07-15 12:43:32 +01001415 INIT_LIST_HEAD(&its->device_list);
1416 INIT_LIST_HEAD(&its->collection_list);
1417
Andre Przywara1085fdc2016-07-15 12:43:31 +01001418 dev->kvm->arch.vgic.has_its = true;
1419 its->initialized = false;
1420 its->enabled = false;
Marc Zyngierbb717642016-07-17 21:35:07 +01001421 its->dev = dev;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001422
Andre Przywara424c3382016-07-15 12:43:32 +01001423 its->baser_device_table = INITIAL_BASER_VALUE |
1424 ((u64)GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT);
1425 its->baser_coll_table = INITIAL_BASER_VALUE |
1426 ((u64)GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT);
1427 dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE;
1428
Andre Przywara1085fdc2016-07-15 12:43:31 +01001429 dev->private = its;
1430
1431 return 0;
1432}
1433
1434static void vgic_its_destroy(struct kvm_device *kvm_dev)
1435{
Andre Przywara424c3382016-07-15 12:43:32 +01001436 struct kvm *kvm = kvm_dev->kvm;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001437 struct vgic_its *its = kvm_dev->private;
Andre Przywara424c3382016-07-15 12:43:32 +01001438 struct its_device *dev;
1439 struct its_itte *itte;
1440 struct list_head *dev_cur, *dev_temp;
1441 struct list_head *cur, *temp;
1442
1443 /*
1444 * We may end up here without the lists ever having been initialized.
1445 * Check this and bail out early to avoid dereferencing a NULL pointer.
1446 */
1447 if (!its->device_list.next)
1448 return;
1449
1450 mutex_lock(&its->its_lock);
1451 list_for_each_safe(dev_cur, dev_temp, &its->device_list) {
1452 dev = container_of(dev_cur, struct its_device, dev_list);
1453 list_for_each_safe(cur, temp, &dev->itt_head) {
1454 itte = (container_of(cur, struct its_itte, itte_list));
1455 its_free_itte(kvm, itte);
1456 }
1457 list_del(dev_cur);
1458 kfree(dev);
1459 }
1460
1461 list_for_each_safe(cur, temp, &its->collection_list) {
1462 list_del(cur);
1463 kfree(container_of(cur, struct its_collection, coll_list));
1464 }
1465 mutex_unlock(&its->its_lock);
Andre Przywara1085fdc2016-07-15 12:43:31 +01001466
1467 kfree(its);
1468}
1469
1470static int vgic_its_has_attr(struct kvm_device *dev,
1471 struct kvm_device_attr *attr)
1472{
1473 switch (attr->group) {
1474 case KVM_DEV_ARM_VGIC_GRP_ADDR:
1475 switch (attr->attr) {
1476 case KVM_VGIC_ITS_ADDR_TYPE:
1477 return 0;
1478 }
1479 break;
1480 case KVM_DEV_ARM_VGIC_GRP_CTRL:
1481 switch (attr->attr) {
1482 case KVM_DEV_ARM_VGIC_CTRL_INIT:
1483 return 0;
1484 }
1485 break;
1486 }
1487 return -ENXIO;
1488}
1489
1490static int vgic_its_set_attr(struct kvm_device *dev,
1491 struct kvm_device_attr *attr)
1492{
1493 struct vgic_its *its = dev->private;
1494 int ret;
1495
1496 switch (attr->group) {
1497 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
1498 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1499 unsigned long type = (unsigned long)attr->attr;
1500 u64 addr;
1501
1502 if (type != KVM_VGIC_ITS_ADDR_TYPE)
1503 return -ENODEV;
1504
Andre Przywara1085fdc2016-07-15 12:43:31 +01001505 if (copy_from_user(&addr, uaddr, sizeof(addr)))
1506 return -EFAULT;
1507
1508 ret = vgic_check_ioaddr(dev->kvm, &its->vgic_its_base,
1509 addr, SZ_64K);
1510 if (ret)
1511 return ret;
1512
1513 its->vgic_its_base = addr;
1514
1515 return 0;
1516 }
1517 case KVM_DEV_ARM_VGIC_GRP_CTRL:
1518 switch (attr->attr) {
1519 case KVM_DEV_ARM_VGIC_CTRL_INIT:
Andre Przywarac7735762016-08-08 16:45:43 +01001520 its->initialized = true;
1521
1522 return 0;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001523 }
1524 break;
1525 }
1526 return -ENXIO;
1527}
1528
1529static int vgic_its_get_attr(struct kvm_device *dev,
1530 struct kvm_device_attr *attr)
1531{
1532 switch (attr->group) {
1533 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
1534 struct vgic_its *its = dev->private;
1535 u64 addr = its->vgic_its_base;
1536 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1537 unsigned long type = (unsigned long)attr->attr;
1538
1539 if (type != KVM_VGIC_ITS_ADDR_TYPE)
1540 return -ENODEV;
1541
1542 if (copy_to_user(uaddr, &addr, sizeof(addr)))
1543 return -EFAULT;
1544 break;
1545 default:
1546 return -ENXIO;
1547 }
1548 }
1549
1550 return 0;
1551}
1552
1553static struct kvm_device_ops kvm_arm_vgic_its_ops = {
1554 .name = "kvm-arm-vgic-its",
1555 .create = vgic_its_create,
1556 .destroy = vgic_its_destroy,
1557 .set_attr = vgic_its_set_attr,
1558 .get_attr = vgic_its_get_attr,
1559 .has_attr = vgic_its_has_attr,
1560};
1561
1562int kvm_vgic_register_its_device(void)
1563{
1564 return kvm_register_device_ops(&kvm_arm_vgic_its_ops,
1565 KVM_DEV_TYPE_ARM_VGIC_ITS);
1566}
Andre Przywarac7735762016-08-08 16:45:43 +01001567
1568/*
1569 * Registers all ITSes with the kvm_io_bus framework.
1570 * To follow the existing VGIC initialization sequence, this has to be
1571 * done as late as possible, just before the first VCPU runs.
1572 */
1573int vgic_register_its_iodevs(struct kvm *kvm)
1574{
1575 struct kvm_device *dev;
1576 int ret = 0;
1577
1578 list_for_each_entry(dev, &kvm->devices, vm_node) {
1579 if (dev->ops != &kvm_arm_vgic_its_ops)
1580 continue;
1581
1582 ret = vgic_register_its_iodev(kvm, dev->private);
1583 if (ret)
1584 return ret;
1585 /*
1586 * We don't need to care about tearing down previously
1587 * registered ITSes, as the kvm_io_bus framework removes
1588 * them for us if the VM gets destroyed.
1589 */
1590 }
1591
1592 return ret;
1593}