blob: 99da1a207c19b0563030bf4ad842d8d805b1b153 [file] [log] [blame]
Andre Przywaraed9b8ce2015-12-01 14:34:34 +00001/*
2 * VGICv3 MMIO handling functions
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/irqchip/arm-gic-v3.h>
15#include <linux/kvm.h>
16#include <linux/kvm_host.h>
17#include <kvm/iodev.h>
18#include <kvm/arm_vgic.h>
19
20#include <asm/kvm_emulate.h>
Vijaya Kumar K94574c92017-01-26 19:50:47 +053021#include <asm/kvm_arm.h>
22#include <asm/kvm_mmu.h>
Andre Przywaraed9b8ce2015-12-01 14:34:34 +000023
24#include "vgic.h"
25#include "vgic-mmio.h"
26
Andre Przywara741972d2016-01-27 14:54:46 +000027/* extract @num bytes at @offset bytes offset in data */
Vladimir Murzind7d0a112016-09-12 15:49:20 +010028unsigned long extract_bytes(u64 data, unsigned int offset,
Andre Przywara424c3382016-07-15 12:43:32 +010029 unsigned int num)
Andre Przywara741972d2016-01-27 14:54:46 +000030{
31 return (data >> (offset * 8)) & GENMASK_ULL(num * 8 - 1, 0);
32}
33
Andre Przywara0aa1de52016-07-15 12:43:29 +010034/* allows updates of any half of a 64-bit register (or the whole thing) */
Andre Przywara424c3382016-07-15 12:43:32 +010035u64 update_64bit_reg(u64 reg, unsigned int offset, unsigned int len,
36 unsigned long val)
Andre Przywara0aa1de52016-07-15 12:43:29 +010037{
38 int lower = (offset & 4) * 8;
39 int upper = lower + 8 * len - 1;
40
41 reg &= ~GENMASK_ULL(upper, lower);
42 val &= GENMASK_ULL(len * 8 - 1, 0);
43
44 return reg | ((u64)val << lower);
45}
46
Andre Przywara59c5ab42016-07-15 12:43:30 +010047bool vgic_has_its(struct kvm *kvm)
48{
49 struct vgic_dist *dist = &kvm->arch.vgic;
50
51 if (dist->vgic_model != KVM_DEV_TYPE_ARM_VGIC_V3)
52 return false;
53
Andre Przywara1085fdc2016-07-15 12:43:31 +010054 return dist->has_its;
Andre Przywara59c5ab42016-07-15 12:43:30 +010055}
56
Andre Przywarafd59ed32016-01-27 14:54:30 +000057static unsigned long vgic_mmio_read_v3_misc(struct kvm_vcpu *vcpu,
58 gpa_t addr, unsigned int len)
59{
60 u32 value = 0;
61
62 switch (addr & 0x0c) {
63 case GICD_CTLR:
64 if (vcpu->kvm->arch.vgic.enabled)
65 value |= GICD_CTLR_ENABLE_SS_G1;
66 value |= GICD_CTLR_ARE_NS | GICD_CTLR_DS;
67 break;
68 case GICD_TYPER:
69 value = vcpu->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
70 value = (value >> 5) - 1;
Andre Przywara0e4e82f2016-07-15 12:43:38 +010071 if (vgic_has_its(vcpu->kvm)) {
72 value |= (INTERRUPT_ID_BITS_ITS - 1) << 19;
73 value |= GICD_TYPER_LPIS;
74 } else {
75 value |= (INTERRUPT_ID_BITS_SPIS - 1) << 19;
76 }
Andre Przywarafd59ed32016-01-27 14:54:30 +000077 break;
78 case GICD_IIDR:
79 value = (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
80 break;
81 default:
82 return 0;
83 }
84
85 return value;
86}
87
88static void vgic_mmio_write_v3_misc(struct kvm_vcpu *vcpu,
89 gpa_t addr, unsigned int len,
90 unsigned long val)
91{
92 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
93 bool was_enabled = dist->enabled;
94
95 switch (addr & 0x0c) {
96 case GICD_CTLR:
97 dist->enabled = val & GICD_CTLR_ENABLE_SS_G1;
98
99 if (!was_enabled && dist->enabled)
100 vgic_kick_vcpus(vcpu->kvm);
101 break;
102 case GICD_TYPER:
103 case GICD_IIDR:
104 return;
105 }
106}
107
Andre Przywara78a714a2016-01-25 16:45:37 +0000108static unsigned long vgic_mmio_read_irouter(struct kvm_vcpu *vcpu,
109 gpa_t addr, unsigned int len)
110{
111 int intid = VGIC_ADDR_TO_INTID(addr, 64);
112 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, NULL, intid);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100113 unsigned long ret = 0;
Andre Przywara78a714a2016-01-25 16:45:37 +0000114
115 if (!irq)
116 return 0;
117
118 /* The upper word is RAZ for us. */
Andre Przywara5dd4b922016-07-15 12:43:27 +0100119 if (!(addr & 4))
120 ret = extract_bytes(READ_ONCE(irq->mpidr), addr & 7, len);
Andre Przywara78a714a2016-01-25 16:45:37 +0000121
Andre Przywara5dd4b922016-07-15 12:43:27 +0100122 vgic_put_irq(vcpu->kvm, irq);
123 return ret;
Andre Przywara78a714a2016-01-25 16:45:37 +0000124}
125
126static void vgic_mmio_write_irouter(struct kvm_vcpu *vcpu,
127 gpa_t addr, unsigned int len,
128 unsigned long val)
129{
130 int intid = VGIC_ADDR_TO_INTID(addr, 64);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100131 struct vgic_irq *irq;
Andre Przywara78a714a2016-01-25 16:45:37 +0000132
133 /* The upper word is WI for us since we don't implement Aff3. */
134 if (addr & 4)
135 return;
136
Andre Przywara5dd4b922016-07-15 12:43:27 +0100137 irq = vgic_get_irq(vcpu->kvm, NULL, intid);
138
139 if (!irq)
140 return;
141
Andre Przywara78a714a2016-01-25 16:45:37 +0000142 spin_lock(&irq->irq_lock);
143
144 /* We only care about and preserve Aff0, Aff1 and Aff2. */
145 irq->mpidr = val & GENMASK(23, 0);
146 irq->target_vcpu = kvm_mpidr_to_vcpu(vcpu->kvm, irq->mpidr);
147
148 spin_unlock(&irq->irq_lock);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100149 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara78a714a2016-01-25 16:45:37 +0000150}
151
Andre Przywara59c5ab42016-07-15 12:43:30 +0100152static unsigned long vgic_mmio_read_v3r_ctlr(struct kvm_vcpu *vcpu,
153 gpa_t addr, unsigned int len)
154{
155 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
156
157 return vgic_cpu->lpis_enabled ? GICR_CTLR_ENABLE_LPIS : 0;
158}
159
160
161static void vgic_mmio_write_v3r_ctlr(struct kvm_vcpu *vcpu,
162 gpa_t addr, unsigned int len,
163 unsigned long val)
164{
165 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
166 bool was_enabled = vgic_cpu->lpis_enabled;
167
168 if (!vgic_has_its(vcpu->kvm))
169 return;
170
171 vgic_cpu->lpis_enabled = val & GICR_CTLR_ENABLE_LPIS;
172
Andre Przywara0e4e82f2016-07-15 12:43:38 +0100173 if (!was_enabled && vgic_cpu->lpis_enabled)
174 vgic_enable_lpis(vcpu);
Andre Przywara59c5ab42016-07-15 12:43:30 +0100175}
176
Andre Przywara741972d2016-01-27 14:54:46 +0000177static unsigned long vgic_mmio_read_v3r_typer(struct kvm_vcpu *vcpu,
178 gpa_t addr, unsigned int len)
179{
180 unsigned long mpidr = kvm_vcpu_get_mpidr_aff(vcpu);
181 int target_vcpu_id = vcpu->vcpu_id;
182 u64 value;
183
Vladimir Murzine533a372016-09-12 15:49:19 +0100184 value = (u64)(mpidr & GENMASK(23, 0)) << 32;
Andre Przywara741972d2016-01-27 14:54:46 +0000185 value |= ((target_vcpu_id & 0xffff) << 8);
186 if (target_vcpu_id == atomic_read(&vcpu->kvm->online_vcpus) - 1)
187 value |= GICR_TYPER_LAST;
Andre Przywara0e4e82f2016-07-15 12:43:38 +0100188 if (vgic_has_its(vcpu->kvm))
189 value |= GICR_TYPER_PLPIS;
Andre Przywara741972d2016-01-27 14:54:46 +0000190
191 return extract_bytes(value, addr & 7, len);
192}
193
194static unsigned long vgic_mmio_read_v3r_iidr(struct kvm_vcpu *vcpu,
195 gpa_t addr, unsigned int len)
196{
197 return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
198}
199
Andre Przywara54f59d22016-01-22 18:18:52 +0000200static unsigned long vgic_mmio_read_v3_idregs(struct kvm_vcpu *vcpu,
201 gpa_t addr, unsigned int len)
202{
203 switch (addr & 0xffff) {
204 case GICD_PIDR2:
205 /* report a GICv3 compliant implementation */
206 return 0x3b;
207 }
208
209 return 0;
210}
211
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530212static unsigned long vgic_v3_uaccess_read_pending(struct kvm_vcpu *vcpu,
213 gpa_t addr, unsigned int len)
214{
215 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
216 u32 value = 0;
217 int i;
218
219 /*
220 * pending state of interrupt is latched in pending_latch variable.
221 * Userspace will save and restore pending state and line_level
222 * separately.
223 * Refer to Documentation/virtual/kvm/devices/arm-vgic-v3.txt
224 * for handling of ISPENDR and ICPENDR.
225 */
226 for (i = 0; i < len * 8; i++) {
227 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
228
229 if (irq->pending_latch)
230 value |= (1U << i);
231
232 vgic_put_irq(vcpu->kvm, irq);
233 }
234
235 return value;
236}
237
238static void vgic_v3_uaccess_write_pending(struct kvm_vcpu *vcpu,
239 gpa_t addr, unsigned int len,
240 unsigned long val)
241{
242 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
243 int i;
244
245 for (i = 0; i < len * 8; i++) {
246 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
247
248 spin_lock(&irq->irq_lock);
249 if (test_bit(i, &val)) {
250 /*
251 * pending_latch is set irrespective of irq type
252 * (level or edge) to avoid dependency that VM should
253 * restore irq config before pending info.
254 */
255 irq->pending_latch = true;
256 vgic_queue_irq_unlock(vcpu->kvm, irq);
257 } else {
258 irq->pending_latch = false;
259 spin_unlock(&irq->irq_lock);
260 }
261
262 vgic_put_irq(vcpu->kvm, irq);
263 }
264}
265
Andre Przywara0aa1de52016-07-15 12:43:29 +0100266/* We want to avoid outer shareable. */
267u64 vgic_sanitise_shareability(u64 field)
268{
269 switch (field) {
270 case GIC_BASER_OuterShareable:
271 return GIC_BASER_InnerShareable;
272 default:
273 return field;
274 }
275}
276
277/* Avoid any inner non-cacheable mapping. */
278u64 vgic_sanitise_inner_cacheability(u64 field)
279{
280 switch (field) {
281 case GIC_BASER_CACHE_nCnB:
282 case GIC_BASER_CACHE_nC:
283 return GIC_BASER_CACHE_RaWb;
284 default:
285 return field;
286 }
287}
288
289/* Non-cacheable or same-as-inner are OK. */
290u64 vgic_sanitise_outer_cacheability(u64 field)
291{
292 switch (field) {
293 case GIC_BASER_CACHE_SameAsInner:
294 case GIC_BASER_CACHE_nC:
295 return field;
296 default:
297 return GIC_BASER_CACHE_nC;
298 }
299}
300
301u64 vgic_sanitise_field(u64 reg, u64 field_mask, int field_shift,
302 u64 (*sanitise_fn)(u64))
303{
304 u64 field = (reg & field_mask) >> field_shift;
305
306 field = sanitise_fn(field) << field_shift;
307 return (reg & ~field_mask) | field;
308}
309
310#define PROPBASER_RES0_MASK \
311 (GENMASK_ULL(63, 59) | GENMASK_ULL(55, 52) | GENMASK_ULL(6, 5))
312#define PENDBASER_RES0_MASK \
313 (BIT_ULL(63) | GENMASK_ULL(61, 59) | GENMASK_ULL(55, 52) | \
314 GENMASK_ULL(15, 12) | GENMASK_ULL(6, 0))
315
316static u64 vgic_sanitise_pendbaser(u64 reg)
317{
318 reg = vgic_sanitise_field(reg, GICR_PENDBASER_SHAREABILITY_MASK,
319 GICR_PENDBASER_SHAREABILITY_SHIFT,
320 vgic_sanitise_shareability);
321 reg = vgic_sanitise_field(reg, GICR_PENDBASER_INNER_CACHEABILITY_MASK,
322 GICR_PENDBASER_INNER_CACHEABILITY_SHIFT,
323 vgic_sanitise_inner_cacheability);
324 reg = vgic_sanitise_field(reg, GICR_PENDBASER_OUTER_CACHEABILITY_MASK,
325 GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT,
326 vgic_sanitise_outer_cacheability);
327
328 reg &= ~PENDBASER_RES0_MASK;
329 reg &= ~GENMASK_ULL(51, 48);
330
331 return reg;
332}
333
334static u64 vgic_sanitise_propbaser(u64 reg)
335{
336 reg = vgic_sanitise_field(reg, GICR_PROPBASER_SHAREABILITY_MASK,
337 GICR_PROPBASER_SHAREABILITY_SHIFT,
338 vgic_sanitise_shareability);
339 reg = vgic_sanitise_field(reg, GICR_PROPBASER_INNER_CACHEABILITY_MASK,
340 GICR_PROPBASER_INNER_CACHEABILITY_SHIFT,
341 vgic_sanitise_inner_cacheability);
342 reg = vgic_sanitise_field(reg, GICR_PROPBASER_OUTER_CACHEABILITY_MASK,
343 GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT,
344 vgic_sanitise_outer_cacheability);
345
346 reg &= ~PROPBASER_RES0_MASK;
347 reg &= ~GENMASK_ULL(51, 48);
348 return reg;
349}
350
351static unsigned long vgic_mmio_read_propbase(struct kvm_vcpu *vcpu,
352 gpa_t addr, unsigned int len)
353{
354 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
355
356 return extract_bytes(dist->propbaser, addr & 7, len);
357}
358
359static void vgic_mmio_write_propbase(struct kvm_vcpu *vcpu,
360 gpa_t addr, unsigned int len,
361 unsigned long val)
362{
363 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
364 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Christoffer Dalld9ae4492016-08-03 18:03:44 +0200365 u64 old_propbaser, propbaser;
Andre Przywara0aa1de52016-07-15 12:43:29 +0100366
367 /* Storing a value with LPIs already enabled is undefined */
368 if (vgic_cpu->lpis_enabled)
369 return;
370
Christoffer Dalld9ae4492016-08-03 18:03:44 +0200371 do {
372 old_propbaser = dist->propbaser;
373 propbaser = old_propbaser;
374 propbaser = update_64bit_reg(propbaser, addr & 4, len, val);
375 propbaser = vgic_sanitise_propbaser(propbaser);
376 } while (cmpxchg64(&dist->propbaser, old_propbaser,
377 propbaser) != old_propbaser);
Andre Przywara0aa1de52016-07-15 12:43:29 +0100378}
379
380static unsigned long vgic_mmio_read_pendbase(struct kvm_vcpu *vcpu,
381 gpa_t addr, unsigned int len)
382{
383 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
384
385 return extract_bytes(vgic_cpu->pendbaser, addr & 7, len);
386}
387
388static void vgic_mmio_write_pendbase(struct kvm_vcpu *vcpu,
389 gpa_t addr, unsigned int len,
390 unsigned long val)
391{
392 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Christoffer Dalld9ae4492016-08-03 18:03:44 +0200393 u64 old_pendbaser, pendbaser;
Andre Przywara0aa1de52016-07-15 12:43:29 +0100394
395 /* Storing a value with LPIs already enabled is undefined */
396 if (vgic_cpu->lpis_enabled)
397 return;
398
Christoffer Dalld9ae4492016-08-03 18:03:44 +0200399 do {
400 old_pendbaser = vgic_cpu->pendbaser;
401 pendbaser = old_pendbaser;
402 pendbaser = update_64bit_reg(pendbaser, addr & 4, len, val);
403 pendbaser = vgic_sanitise_pendbaser(pendbaser);
404 } while (cmpxchg64(&vgic_cpu->pendbaser, old_pendbaser,
405 pendbaser) != old_pendbaser);
Andre Przywara0aa1de52016-07-15 12:43:29 +0100406}
407
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000408/*
409 * The GICv3 per-IRQ registers are split to control PPIs and SGIs in the
410 * redistributors, while SPIs are covered by registers in the distributor
411 * block. Trying to set private IRQs in this block gets ignored.
412 * We take some special care here to fix the calculation of the register
413 * offset.
414 */
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530415#define REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(off, rd, wr, ur, uw, bpi, acc) \
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000416 { \
417 .reg_offset = off, \
418 .bits_per_irq = bpi, \
419 .len = (bpi * VGIC_NR_PRIVATE_IRQS) / 8, \
420 .access_flags = acc, \
421 .read = vgic_mmio_read_raz, \
422 .write = vgic_mmio_write_wi, \
423 }, { \
424 .reg_offset = off + (bpi * VGIC_NR_PRIVATE_IRQS) / 8, \
425 .bits_per_irq = bpi, \
426 .len = (bpi * (1024 - VGIC_NR_PRIVATE_IRQS)) / 8, \
427 .access_flags = acc, \
428 .read = rd, \
429 .write = wr, \
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530430 .uaccess_read = ur, \
431 .uaccess_write = uw, \
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000432 }
433
434static const struct vgic_register_region vgic_v3_dist_registers[] = {
435 REGISTER_DESC_WITH_LENGTH(GICD_CTLR,
Andre Przywarafd59ed32016-01-27 14:54:30 +0000436 vgic_mmio_read_v3_misc, vgic_mmio_write_v3_misc, 16,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000437 VGIC_ACCESS_32bit),
Vijaya Kumar K94574c92017-01-26 19:50:47 +0530438 REGISTER_DESC_WITH_LENGTH(GICD_STATUSR,
439 vgic_mmio_read_rao, vgic_mmio_write_wi, 4,
440 VGIC_ACCESS_32bit),
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000441 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IGROUPR,
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530442 vgic_mmio_read_rao, vgic_mmio_write_wi, NULL, NULL, 1,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000443 VGIC_ACCESS_32bit),
444 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ISENABLER,
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530445 vgic_mmio_read_enable, vgic_mmio_write_senable, NULL, NULL, 1,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000446 VGIC_ACCESS_32bit),
447 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICENABLER,
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530448 vgic_mmio_read_enable, vgic_mmio_write_cenable, NULL, NULL, 1,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000449 VGIC_ACCESS_32bit),
450 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ISPENDR,
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530451 vgic_mmio_read_pending, vgic_mmio_write_spending,
452 vgic_v3_uaccess_read_pending, vgic_v3_uaccess_write_pending, 1,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000453 VGIC_ACCESS_32bit),
454 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICPENDR,
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530455 vgic_mmio_read_pending, vgic_mmio_write_cpending,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000456 vgic_mmio_read_raz, vgic_mmio_write_wi, 1,
457 VGIC_ACCESS_32bit),
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530458 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ISACTIVER,
459 vgic_mmio_read_active, vgic_mmio_write_sactive, NULL, NULL, 1,
460 VGIC_ACCESS_32bit),
461 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICACTIVER,
462 vgic_mmio_read_active, vgic_mmio_write_cactive, NULL, NULL, 1,
463 VGIC_ACCESS_32bit),
464 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IPRIORITYR,
465 vgic_mmio_read_priority, vgic_mmio_write_priority, NULL, NULL,
466 8, VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
467 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ITARGETSR,
468 vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 8,
469 VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
470 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICFGR,
471 vgic_mmio_read_config, vgic_mmio_write_config, NULL, NULL, 2,
472 VGIC_ACCESS_32bit),
473 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IGRPMODR,
474 vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
475 VGIC_ACCESS_32bit),
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000476 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IROUTER,
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530477 vgic_mmio_read_irouter, vgic_mmio_write_irouter, NULL, NULL, 64,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000478 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
479 REGISTER_DESC_WITH_LENGTH(GICD_IDREGS,
Andre Przywara54f59d22016-01-22 18:18:52 +0000480 vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000481 VGIC_ACCESS_32bit),
482};
483
484static const struct vgic_register_region vgic_v3_rdbase_registers[] = {
485 REGISTER_DESC_WITH_LENGTH(GICR_CTLR,
Andre Przywara59c5ab42016-07-15 12:43:30 +0100486 vgic_mmio_read_v3r_ctlr, vgic_mmio_write_v3r_ctlr, 4,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000487 VGIC_ACCESS_32bit),
Vijaya Kumar K94574c92017-01-26 19:50:47 +0530488 REGISTER_DESC_WITH_LENGTH(GICR_STATUSR,
489 vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
490 VGIC_ACCESS_32bit),
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000491 REGISTER_DESC_WITH_LENGTH(GICR_IIDR,
Andre Przywara741972d2016-01-27 14:54:46 +0000492 vgic_mmio_read_v3r_iidr, vgic_mmio_write_wi, 4,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000493 VGIC_ACCESS_32bit),
494 REGISTER_DESC_WITH_LENGTH(GICR_TYPER,
Andre Przywara741972d2016-01-27 14:54:46 +0000495 vgic_mmio_read_v3r_typer, vgic_mmio_write_wi, 8,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000496 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
Vijaya Kumar K94574c92017-01-26 19:50:47 +0530497 REGISTER_DESC_WITH_LENGTH(GICR_WAKER,
498 vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
499 VGIC_ACCESS_32bit),
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000500 REGISTER_DESC_WITH_LENGTH(GICR_PROPBASER,
Andre Przywara0aa1de52016-07-15 12:43:29 +0100501 vgic_mmio_read_propbase, vgic_mmio_write_propbase, 8,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000502 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
503 REGISTER_DESC_WITH_LENGTH(GICR_PENDBASER,
Andre Przywara0aa1de52016-07-15 12:43:29 +0100504 vgic_mmio_read_pendbase, vgic_mmio_write_pendbase, 8,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000505 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
506 REGISTER_DESC_WITH_LENGTH(GICR_IDREGS,
Andre Przywara54f59d22016-01-22 18:18:52 +0000507 vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000508 VGIC_ACCESS_32bit),
509};
510
511static const struct vgic_register_region vgic_v3_sgibase_registers[] = {
512 REGISTER_DESC_WITH_LENGTH(GICR_IGROUPR0,
513 vgic_mmio_read_rao, vgic_mmio_write_wi, 4,
514 VGIC_ACCESS_32bit),
515 REGISTER_DESC_WITH_LENGTH(GICR_ISENABLER0,
516 vgic_mmio_read_enable, vgic_mmio_write_senable, 4,
517 VGIC_ACCESS_32bit),
518 REGISTER_DESC_WITH_LENGTH(GICR_ICENABLER0,
519 vgic_mmio_read_enable, vgic_mmio_write_cenable, 4,
520 VGIC_ACCESS_32bit),
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530521 REGISTER_DESC_WITH_LENGTH_UACCESS(GICR_ISPENDR0,
522 vgic_mmio_read_pending, vgic_mmio_write_spending,
523 vgic_v3_uaccess_read_pending, vgic_v3_uaccess_write_pending, 4,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000524 VGIC_ACCESS_32bit),
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530525 REGISTER_DESC_WITH_LENGTH_UACCESS(GICR_ICPENDR0,
526 vgic_mmio_read_pending, vgic_mmio_write_cpending,
527 vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000528 VGIC_ACCESS_32bit),
529 REGISTER_DESC_WITH_LENGTH(GICR_ISACTIVER0,
530 vgic_mmio_read_active, vgic_mmio_write_sactive, 4,
531 VGIC_ACCESS_32bit),
532 REGISTER_DESC_WITH_LENGTH(GICR_ICACTIVER0,
533 vgic_mmio_read_active, vgic_mmio_write_cactive, 4,
534 VGIC_ACCESS_32bit),
535 REGISTER_DESC_WITH_LENGTH(GICR_IPRIORITYR0,
536 vgic_mmio_read_priority, vgic_mmio_write_priority, 32,
537 VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
538 REGISTER_DESC_WITH_LENGTH(GICR_ICFGR0,
539 vgic_mmio_read_config, vgic_mmio_write_config, 8,
540 VGIC_ACCESS_32bit),
541 REGISTER_DESC_WITH_LENGTH(GICR_IGRPMODR0,
542 vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
543 VGIC_ACCESS_32bit),
544 REGISTER_DESC_WITH_LENGTH(GICR_NSACR,
545 vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
546 VGIC_ACCESS_32bit),
547};
548
549unsigned int vgic_v3_init_dist_iodev(struct vgic_io_device *dev)
550{
551 dev->regions = vgic_v3_dist_registers;
552 dev->nr_regions = ARRAY_SIZE(vgic_v3_dist_registers);
553
554 kvm_iodevice_init(&dev->dev, &kvm_io_gic_ops);
555
556 return SZ_64K;
557}
558
Christoffer Dall7fadcd32017-05-08 12:18:26 +0200559/**
560 * vgic_register_redist_iodev - register a single redist iodev
561 * @vcpu: The VCPU to which the redistributor belongs
562 *
563 * Register a KVM iodev for this VCPU's redistributor using the address
564 * provided.
565 *
566 * Return 0 on success, -ERRNO otherwise.
567 */
Christoffer Dall1aab6f42017-05-08 12:30:24 +0200568int vgic_register_redist_iodev(struct kvm_vcpu *vcpu)
Christoffer Dall7fadcd32017-05-08 12:18:26 +0200569{
570 struct kvm *kvm = vcpu->kvm;
571 struct vgic_dist *vgic = &kvm->arch.vgic;
572 struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev;
573 struct vgic_io_device *sgi_dev = &vcpu->arch.vgic_cpu.sgi_iodev;
574 gpa_t rd_base, sgi_base;
575 int ret;
576
Christoffer Dall1aab6f42017-05-08 12:30:24 +0200577 /*
578 * We may be creating VCPUs before having set the base address for the
579 * redistributor region, in which case we will come back to this
580 * function for all VCPUs when the base address is set. Just return
581 * without doing any work for now.
582 */
583 if (IS_VGIC_ADDR_UNDEF(vgic->vgic_redist_base))
584 return 0;
585
586 if (!vgic_v3_check_base(kvm))
587 return -EINVAL;
588
Christoffer Dall7fadcd32017-05-08 12:18:26 +0200589 rd_base = vgic->vgic_redist_base + kvm_vcpu_get_idx(vcpu) * SZ_64K * 2;
590 sgi_base = rd_base + SZ_64K;
591
592 kvm_iodevice_init(&rd_dev->dev, &kvm_io_gic_ops);
593 rd_dev->base_addr = rd_base;
594 rd_dev->iodev_type = IODEV_REDIST;
595 rd_dev->regions = vgic_v3_rdbase_registers;
596 rd_dev->nr_regions = ARRAY_SIZE(vgic_v3_rdbase_registers);
597 rd_dev->redist_vcpu = vcpu;
598
599 mutex_lock(&kvm->slots_lock);
600 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, rd_base,
601 SZ_64K, &rd_dev->dev);
602 mutex_unlock(&kvm->slots_lock);
603
604 if (ret)
605 return ret;
606
607 kvm_iodevice_init(&sgi_dev->dev, &kvm_io_gic_ops);
608 sgi_dev->base_addr = sgi_base;
609 sgi_dev->iodev_type = IODEV_REDIST;
610 sgi_dev->regions = vgic_v3_sgibase_registers;
611 sgi_dev->nr_regions = ARRAY_SIZE(vgic_v3_sgibase_registers);
612 sgi_dev->redist_vcpu = vcpu;
613
614 mutex_lock(&kvm->slots_lock);
615 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, sgi_base,
616 SZ_64K, &sgi_dev->dev);
617 mutex_unlock(&kvm->slots_lock);
618 if (ret)
619 kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS,
620 &rd_dev->dev);
621
622 return ret;
623}
624
625static void vgic_unregister_redist_iodev(struct kvm_vcpu *vcpu)
626{
627 struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev;
628 struct vgic_io_device *sgi_dev = &vcpu->arch.vgic_cpu.sgi_iodev;
629
630 kvm_io_bus_unregister_dev(vcpu->kvm, KVM_MMIO_BUS, &rd_dev->dev);
631 kvm_io_bus_unregister_dev(vcpu->kvm, KVM_MMIO_BUS, &sgi_dev->dev);
632}
633
Christoffer Dall1aab6f42017-05-08 12:30:24 +0200634static int vgic_register_all_redist_iodevs(struct kvm *kvm)
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000635{
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000636 struct kvm_vcpu *vcpu;
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000637 int c, ret = 0;
638
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000639 kvm_for_each_vcpu(c, vcpu, kvm) {
Christoffer Dall7fadcd32017-05-08 12:18:26 +0200640 ret = vgic_register_redist_iodev(vcpu);
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000641 if (ret)
642 break;
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000643 }
644
645 if (ret) {
646 /* The current c failed, so we start with the previous one. */
647 for (c--; c >= 0; c--) {
Andre Przywara8f6cdc12016-07-15 12:43:22 +0100648 vcpu = kvm_get_vcpu(kvm, c);
Christoffer Dall7fadcd32017-05-08 12:18:26 +0200649 vgic_unregister_redist_iodev(vcpu);
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000650 }
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000651 }
652
653 return ret;
654}
Andre Przywara621ecd82016-01-26 15:31:15 +0000655
Christoffer Dall1aab6f42017-05-08 12:30:24 +0200656int vgic_v3_set_redist_base(struct kvm *kvm, u64 addr)
657{
658 struct vgic_dist *vgic = &kvm->arch.vgic;
659 int ret;
660
661 /* vgic_check_ioaddr makes sure we don't do this twice */
662 ret = vgic_check_ioaddr(kvm, &vgic->vgic_redist_base, addr, SZ_64K);
663 if (ret)
664 return ret;
665
666 vgic->vgic_redist_base = addr;
667 if (!vgic_v3_check_base(kvm)) {
668 vgic->vgic_redist_base = VGIC_ADDR_UNDEF;
669 return -EINVAL;
670 }
671
672 /*
673 * Register iodevs for each existing VCPU. Adding more VCPUs
674 * afterwards will register the iodevs when needed.
675 */
676 ret = vgic_register_all_redist_iodevs(kvm);
677 if (ret)
678 return ret;
679
680 return 0;
681}
682
Vijaya Kumar K94574c92017-01-26 19:50:47 +0530683int vgic_v3_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr)
684{
685 const struct vgic_register_region *region;
686 struct vgic_io_device iodev;
687 struct vgic_reg_attr reg_attr;
688 struct kvm_vcpu *vcpu;
689 gpa_t addr;
690 int ret;
691
692 ret = vgic_v3_parse_attr(dev, attr, &reg_attr);
693 if (ret)
694 return ret;
695
696 vcpu = reg_attr.vcpu;
697 addr = reg_attr.addr;
698
699 switch (attr->group) {
700 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
701 iodev.regions = vgic_v3_dist_registers;
702 iodev.nr_regions = ARRAY_SIZE(vgic_v3_dist_registers);
703 iodev.base_addr = 0;
704 break;
705 case KVM_DEV_ARM_VGIC_GRP_REDIST_REGS:{
706 iodev.regions = vgic_v3_rdbase_registers;
707 iodev.nr_regions = ARRAY_SIZE(vgic_v3_rdbase_registers);
708 iodev.base_addr = 0;
709 break;
710 }
Vijaya Kumar Kd017d7b2017-01-26 19:50:51 +0530711 case KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS: {
712 u64 reg, id;
713
714 id = (attr->attr & KVM_DEV_ARM_VGIC_SYSREG_INSTR_MASK);
715 return vgic_v3_has_cpu_sysregs_attr(vcpu, 0, id, &reg);
716 }
Vijaya Kumar K94574c92017-01-26 19:50:47 +0530717 default:
718 return -ENXIO;
719 }
720
721 /* We only support aligned 32-bit accesses. */
722 if (addr & 3)
723 return -ENXIO;
724
725 region = vgic_get_mmio_region(vcpu, &iodev, addr, sizeof(u32));
726 if (!region)
727 return -ENXIO;
728
729 return 0;
730}
Andre Przywara621ecd82016-01-26 15:31:15 +0000731/*
732 * Compare a given affinity (level 1-3 and a level 0 mask, from the SGI
733 * generation register ICC_SGI1R_EL1) with a given VCPU.
734 * If the VCPU's MPIDR matches, return the level0 affinity, otherwise
735 * return -1.
736 */
737static int match_mpidr(u64 sgi_aff, u16 sgi_cpu_mask, struct kvm_vcpu *vcpu)
738{
739 unsigned long affinity;
740 int level0;
741
742 /*
743 * Split the current VCPU's MPIDR into affinity level 0 and the
744 * rest as this is what we have to compare against.
745 */
746 affinity = kvm_vcpu_get_mpidr_aff(vcpu);
747 level0 = MPIDR_AFFINITY_LEVEL(affinity, 0);
748 affinity &= ~MPIDR_LEVEL_MASK;
749
750 /* bail out if the upper three levels don't match */
751 if (sgi_aff != affinity)
752 return -1;
753
754 /* Is this VCPU's bit set in the mask ? */
755 if (!(sgi_cpu_mask & BIT(level0)))
756 return -1;
757
758 return level0;
759}
760
761/*
762 * The ICC_SGI* registers encode the affinity differently from the MPIDR,
763 * so provide a wrapper to use the existing defines to isolate a certain
764 * affinity level.
765 */
766#define SGI_AFFINITY_LEVEL(reg, level) \
767 ((((reg) & ICC_SGI1R_AFFINITY_## level ##_MASK) \
768 >> ICC_SGI1R_AFFINITY_## level ##_SHIFT) << MPIDR_LEVEL_SHIFT(level))
769
770/**
771 * vgic_v3_dispatch_sgi - handle SGI requests from VCPUs
772 * @vcpu: The VCPU requesting a SGI
773 * @reg: The value written into the ICC_SGI1R_EL1 register by that VCPU
774 *
775 * With GICv3 (and ARE=1) CPUs trigger SGIs by writing to a system register.
776 * This will trap in sys_regs.c and call this function.
777 * This ICC_SGI1R_EL1 register contains the upper three affinity levels of the
778 * target processors as well as a bitmask of 16 Aff0 CPUs.
779 * If the interrupt routing mode bit is not set, we iterate over all VCPUs to
780 * check for matching ones. If this bit is set, we signal all, but not the
781 * calling VCPU.
782 */
783void vgic_v3_dispatch_sgi(struct kvm_vcpu *vcpu, u64 reg)
784{
785 struct kvm *kvm = vcpu->kvm;
786 struct kvm_vcpu *c_vcpu;
787 u16 target_cpus;
788 u64 mpidr;
789 int sgi, c;
790 int vcpu_id = vcpu->vcpu_id;
791 bool broadcast;
792
793 sgi = (reg & ICC_SGI1R_SGI_ID_MASK) >> ICC_SGI1R_SGI_ID_SHIFT;
Vladimir Murzine533a372016-09-12 15:49:19 +0100794 broadcast = reg & BIT_ULL(ICC_SGI1R_IRQ_ROUTING_MODE_BIT);
Andre Przywara621ecd82016-01-26 15:31:15 +0000795 target_cpus = (reg & ICC_SGI1R_TARGET_LIST_MASK) >> ICC_SGI1R_TARGET_LIST_SHIFT;
796 mpidr = SGI_AFFINITY_LEVEL(reg, 3);
797 mpidr |= SGI_AFFINITY_LEVEL(reg, 2);
798 mpidr |= SGI_AFFINITY_LEVEL(reg, 1);
799
800 /*
801 * We iterate over all VCPUs to find the MPIDRs matching the request.
802 * If we have handled one CPU, we clear its bit to detect early
803 * if we are already finished. This avoids iterating through all
804 * VCPUs when most of the times we just signal a single VCPU.
805 */
806 kvm_for_each_vcpu(c, c_vcpu, kvm) {
807 struct vgic_irq *irq;
808
809 /* Exit early if we have dealt with all requested CPUs */
810 if (!broadcast && target_cpus == 0)
811 break;
812
813 /* Don't signal the calling VCPU */
814 if (broadcast && c == vcpu_id)
815 continue;
816
817 if (!broadcast) {
818 int level0;
819
820 level0 = match_mpidr(mpidr, target_cpus, c_vcpu);
821 if (level0 == -1)
822 continue;
823
824 /* remove this matching VCPU from the mask */
825 target_cpus &= ~BIT(level0);
826 }
827
828 irq = vgic_get_irq(vcpu->kvm, c_vcpu, sgi);
829
830 spin_lock(&irq->irq_lock);
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100831 irq->pending_latch = true;
Andre Przywara621ecd82016-01-26 15:31:15 +0000832
833 vgic_queue_irq_unlock(vcpu->kvm, irq);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100834 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara621ecd82016-01-26 15:31:15 +0000835 }
836}
Vijaya Kumar K94574c92017-01-26 19:50:47 +0530837
838int vgic_v3_dist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
839 int offset, u32 *val)
840{
841 struct vgic_io_device dev = {
842 .regions = vgic_v3_dist_registers,
843 .nr_regions = ARRAY_SIZE(vgic_v3_dist_registers),
844 };
845
846 return vgic_uaccess(vcpu, &dev, is_write, offset, val);
847}
848
849int vgic_v3_redist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
850 int offset, u32 *val)
851{
852 struct vgic_io_device rd_dev = {
853 .regions = vgic_v3_rdbase_registers,
854 .nr_regions = ARRAY_SIZE(vgic_v3_rdbase_registers),
855 };
856
857 struct vgic_io_device sgi_dev = {
858 .regions = vgic_v3_sgibase_registers,
859 .nr_regions = ARRAY_SIZE(vgic_v3_sgibase_registers),
860 };
861
862 /* SGI_base is the next 64K frame after RD_base */
863 if (offset >= SZ_64K)
864 return vgic_uaccess(vcpu, &sgi_dev, is_write, offset - SZ_64K,
865 val);
866 else
867 return vgic_uaccess(vcpu, &rd_dev, is_write, offset, val);
868}
Vijaya Kumar Ke96a0062017-01-26 19:50:52 +0530869
870int vgic_v3_line_level_info_uaccess(struct kvm_vcpu *vcpu, bool is_write,
871 u32 intid, u64 *val)
872{
873 if (intid % 32)
874 return -EINVAL;
875
876 if (is_write)
877 vgic_write_irq_line_level_info(vcpu, intid, *val);
878 else
879 *val = vgic_read_irq_line_level_info(vcpu, intid);
880
881 return 0;
882}