1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * VGICv3 MMIO handling functions
4 */
5
6 #include <linux/irqchip/arm-gic-v3.h>
7 #include <linux/kvm.h>
8 #include <linux/kvm_host.h>
9 #include <kvm/iodev.h>
10 #include <kvm/arm_vgic.h>
11
12 #include <asm/kvm_emulate.h>
13 #include <asm/kvm_arm.h>
14 #include <asm/kvm_mmu.h>
15
16 #include "vgic.h"
17 #include "vgic-mmio.h"
18
19 /* extract @num bytes at @offset bytes offset in data */
extract_bytes(u64 data,unsigned int offset,unsigned int num)20 unsigned long extract_bytes(u64 data, unsigned int offset,
21 unsigned int num)
22 {
23 return (data >> (offset * 8)) & GENMASK_ULL(num * 8 - 1, 0);
24 }
25
26 /* allows updates of any half of a 64-bit register (or the whole thing) */
update_64bit_reg(u64 reg,unsigned int offset,unsigned int len,unsigned long val)27 u64 update_64bit_reg(u64 reg, unsigned int offset, unsigned int len,
28 unsigned long val)
29 {
30 int lower = (offset & 4) * 8;
31 int upper = lower + 8 * len - 1;
32
33 reg &= ~GENMASK_ULL(upper, lower);
34 val &= GENMASK_ULL(len * 8 - 1, 0);
35
36 return reg | ((u64)val << lower);
37 }
38
vgic_has_its(struct kvm * kvm)39 bool vgic_has_its(struct kvm *kvm)
40 {
41 struct vgic_dist *dist = &kvm->arch.vgic;
42
43 if (dist->vgic_model != KVM_DEV_TYPE_ARM_VGIC_V3)
44 return false;
45
46 return dist->has_its;
47 }
48
vgic_supports_direct_msis(struct kvm * kvm)49 bool vgic_supports_direct_msis(struct kvm *kvm)
50 {
51 return kvm_vgic_global_state.has_gicv4 && vgic_has_its(kvm);
52 }
53
54 /*
55 * The Revision field in the IIDR have the following meanings:
56 *
57 * Revision 2: Interrupt groups are guest-configurable and signaled using
58 * their configured groups.
59 */
60
vgic_mmio_read_v3_misc(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)61 static unsigned long vgic_mmio_read_v3_misc(struct kvm_vcpu *vcpu,
62 gpa_t addr, unsigned int len)
63 {
64 struct vgic_dist *vgic = &vcpu->kvm->arch.vgic;
65 u32 value = 0;
66
67 switch (addr & 0x0c) {
68 case GICD_CTLR:
69 if (vgic->enabled)
70 value |= GICD_CTLR_ENABLE_SS_G1;
71 value |= GICD_CTLR_ARE_NS | GICD_CTLR_DS;
72 break;
73 case GICD_TYPER:
74 value = vgic->nr_spis + VGIC_NR_PRIVATE_IRQS;
75 value = (value >> 5) - 1;
76 if (vgic_has_its(vcpu->kvm)) {
77 value |= (INTERRUPT_ID_BITS_ITS - 1) << 19;
78 value |= GICD_TYPER_LPIS;
79 } else {
80 value |= (INTERRUPT_ID_BITS_SPIS - 1) << 19;
81 }
82 break;
83 case GICD_IIDR:
84 value = (PRODUCT_ID_KVM << GICD_IIDR_PRODUCT_ID_SHIFT) |
85 (vgic->implementation_rev << GICD_IIDR_REVISION_SHIFT) |
86 (IMPLEMENTER_ARM << GICD_IIDR_IMPLEMENTER_SHIFT);
87 break;
88 default:
89 return 0;
90 }
91
92 return value;
93 }
94
vgic_mmio_write_v3_misc(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)95 static void vgic_mmio_write_v3_misc(struct kvm_vcpu *vcpu,
96 gpa_t addr, unsigned int len,
97 unsigned long val)
98 {
99 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
100 bool was_enabled = dist->enabled;
101
102 switch (addr & 0x0c) {
103 case GICD_CTLR:
104 dist->enabled = val & GICD_CTLR_ENABLE_SS_G1;
105
106 if (!was_enabled && dist->enabled)
107 vgic_kick_vcpus(vcpu->kvm);
108 break;
109 case GICD_TYPER:
110 case GICD_IIDR:
111 return;
112 }
113 }
114
vgic_mmio_uaccess_write_v3_misc(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)115 static int vgic_mmio_uaccess_write_v3_misc(struct kvm_vcpu *vcpu,
116 gpa_t addr, unsigned int len,
117 unsigned long val)
118 {
119 switch (addr & 0x0c) {
120 case GICD_IIDR:
121 if (val != vgic_mmio_read_v3_misc(vcpu, addr, len))
122 return -EINVAL;
123 }
124
125 vgic_mmio_write_v3_misc(vcpu, addr, len, val);
126 return 0;
127 }
128
vgic_mmio_read_irouter(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)129 static unsigned long vgic_mmio_read_irouter(struct kvm_vcpu *vcpu,
130 gpa_t addr, unsigned int len)
131 {
132 int intid = VGIC_ADDR_TO_INTID(addr, 64);
133 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, NULL, intid);
134 unsigned long ret = 0;
135
136 if (!irq)
137 return 0;
138
139 /* The upper word is RAZ for us. */
140 if (!(addr & 4))
141 ret = extract_bytes(READ_ONCE(irq->mpidr), addr & 7, len);
142
143 vgic_put_irq(vcpu->kvm, irq);
144 return ret;
145 }
146
vgic_mmio_write_irouter(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)147 static void vgic_mmio_write_irouter(struct kvm_vcpu *vcpu,
148 gpa_t addr, unsigned int len,
149 unsigned long val)
150 {
151 int intid = VGIC_ADDR_TO_INTID(addr, 64);
152 struct vgic_irq *irq;
153 unsigned long flags;
154
155 /* The upper word is WI for us since we don't implement Aff3. */
156 if (addr & 4)
157 return;
158
159 irq = vgic_get_irq(vcpu->kvm, NULL, intid);
160
161 if (!irq)
162 return;
163
164 raw_spin_lock_irqsave(&irq->irq_lock, flags);
165
166 /* We only care about and preserve Aff0, Aff1 and Aff2. */
167 irq->mpidr = val & GENMASK(23, 0);
168 irq->target_vcpu = kvm_mpidr_to_vcpu(vcpu->kvm, irq->mpidr);
169
170 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
171 vgic_put_irq(vcpu->kvm, irq);
172 }
173
vgic_mmio_read_v3r_ctlr(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)174 static unsigned long vgic_mmio_read_v3r_ctlr(struct kvm_vcpu *vcpu,
175 gpa_t addr, unsigned int len)
176 {
177 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
178
179 return vgic_cpu->lpis_enabled ? GICR_CTLR_ENABLE_LPIS : 0;
180 }
181
182
vgic_mmio_write_v3r_ctlr(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)183 static void vgic_mmio_write_v3r_ctlr(struct kvm_vcpu *vcpu,
184 gpa_t addr, unsigned int len,
185 unsigned long val)
186 {
187 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
188 bool was_enabled = vgic_cpu->lpis_enabled;
189
190 if (!vgic_has_its(vcpu->kvm))
191 return;
192
193 vgic_cpu->lpis_enabled = val & GICR_CTLR_ENABLE_LPIS;
194
195 if (was_enabled && !vgic_cpu->lpis_enabled) {
196 vgic_flush_pending_lpis(vcpu);
197 vgic_its_invalidate_cache(vcpu->kvm);
198 }
199
200 if (!was_enabled && vgic_cpu->lpis_enabled)
201 vgic_enable_lpis(vcpu);
202 }
203
vgic_mmio_read_v3r_typer(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)204 static unsigned long vgic_mmio_read_v3r_typer(struct kvm_vcpu *vcpu,
205 gpa_t addr, unsigned int len)
206 {
207 unsigned long mpidr = kvm_vcpu_get_mpidr_aff(vcpu);
208 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
209 struct vgic_redist_region *rdreg = vgic_cpu->rdreg;
210 int target_vcpu_id = vcpu->vcpu_id;
211 gpa_t last_rdist_typer = rdreg->base + GICR_TYPER +
212 (rdreg->free_index - 1) * KVM_VGIC_V3_REDIST_SIZE;
213 u64 value;
214
215 value = (u64)(mpidr & GENMASK(23, 0)) << 32;
216 value |= ((target_vcpu_id & 0xffff) << 8);
217
218 if (addr == last_rdist_typer)
219 value |= GICR_TYPER_LAST;
220 if (vgic_has_its(vcpu->kvm))
221 value |= GICR_TYPER_PLPIS;
222
223 return extract_bytes(value, addr & 7, len);
224 }
225
vgic_uaccess_read_v3r_typer(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)226 static unsigned long vgic_uaccess_read_v3r_typer(struct kvm_vcpu *vcpu,
227 gpa_t addr, unsigned int len)
228 {
229 unsigned long mpidr = kvm_vcpu_get_mpidr_aff(vcpu);
230 int target_vcpu_id = vcpu->vcpu_id;
231 u64 value;
232
233 value = (u64)(mpidr & GENMASK(23, 0)) << 32;
234 value |= ((target_vcpu_id & 0xffff) << 8);
235
236 if (vgic_has_its(vcpu->kvm))
237 value |= GICR_TYPER_PLPIS;
238
239 /* reporting of the Last bit is not supported for userspace */
240 return extract_bytes(value, addr & 7, len);
241 }
242
vgic_mmio_read_v3r_iidr(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)243 static unsigned long vgic_mmio_read_v3r_iidr(struct kvm_vcpu *vcpu,
244 gpa_t addr, unsigned int len)
245 {
246 return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
247 }
248
vgic_mmio_read_v3_idregs(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)249 static unsigned long vgic_mmio_read_v3_idregs(struct kvm_vcpu *vcpu,
250 gpa_t addr, unsigned int len)
251 {
252 switch (addr & 0xffff) {
253 case GICD_PIDR2:
254 /* report a GICv3 compliant implementation */
255 return 0x3b;
256 }
257
258 return 0;
259 }
260
vgic_v3_uaccess_read_pending(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)261 static unsigned long vgic_v3_uaccess_read_pending(struct kvm_vcpu *vcpu,
262 gpa_t addr, unsigned int len)
263 {
264 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
265 u32 value = 0;
266 int i;
267
268 /*
269 * pending state of interrupt is latched in pending_latch variable.
270 * Userspace will save and restore pending state and line_level
271 * separately.
272 * Refer to Documentation/virt/kvm/devices/arm-vgic-v3.txt
273 * for handling of ISPENDR and ICPENDR.
274 */
275 for (i = 0; i < len * 8; i++) {
276 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
277
278 if (irq->pending_latch)
279 value |= (1U << i);
280
281 vgic_put_irq(vcpu->kvm, irq);
282 }
283
284 return value;
285 }
286
vgic_v3_uaccess_write_pending(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)287 static int vgic_v3_uaccess_write_pending(struct kvm_vcpu *vcpu,
288 gpa_t addr, unsigned int len,
289 unsigned long val)
290 {
291 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
292 int i;
293 unsigned long flags;
294
295 for (i = 0; i < len * 8; i++) {
296 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
297
298 raw_spin_lock_irqsave(&irq->irq_lock, flags);
299 if (test_bit(i, &val)) {
300 /*
301 * pending_latch is set irrespective of irq type
302 * (level or edge) to avoid dependency that VM should
303 * restore irq config before pending info.
304 */
305 irq->pending_latch = true;
306 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
307 } else {
308 irq->pending_latch = false;
309 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
310 }
311
312 vgic_put_irq(vcpu->kvm, irq);
313 }
314
315 return 0;
316 }
317
318 /* We want to avoid outer shareable. */
vgic_sanitise_shareability(u64 field)319 u64 vgic_sanitise_shareability(u64 field)
320 {
321 switch (field) {
322 case GIC_BASER_OuterShareable:
323 return GIC_BASER_InnerShareable;
324 default:
325 return field;
326 }
327 }
328
329 /* Avoid any inner non-cacheable mapping. */
vgic_sanitise_inner_cacheability(u64 field)330 u64 vgic_sanitise_inner_cacheability(u64 field)
331 {
332 switch (field) {
333 case GIC_BASER_CACHE_nCnB:
334 case GIC_BASER_CACHE_nC:
335 return GIC_BASER_CACHE_RaWb;
336 default:
337 return field;
338 }
339 }
340
341 /* Non-cacheable or same-as-inner are OK. */
vgic_sanitise_outer_cacheability(u64 field)342 u64 vgic_sanitise_outer_cacheability(u64 field)
343 {
344 switch (field) {
345 case GIC_BASER_CACHE_SameAsInner:
346 case GIC_BASER_CACHE_nC:
347 return field;
348 default:
349 return GIC_BASER_CACHE_nC;
350 }
351 }
352
vgic_sanitise_field(u64 reg,u64 field_mask,int field_shift,u64 (* sanitise_fn)(u64))353 u64 vgic_sanitise_field(u64 reg, u64 field_mask, int field_shift,
354 u64 (*sanitise_fn)(u64))
355 {
356 u64 field = (reg & field_mask) >> field_shift;
357
358 field = sanitise_fn(field) << field_shift;
359 return (reg & ~field_mask) | field;
360 }
361
362 #define PROPBASER_RES0_MASK \
363 (GENMASK_ULL(63, 59) | GENMASK_ULL(55, 52) | GENMASK_ULL(6, 5))
364 #define PENDBASER_RES0_MASK \
365 (BIT_ULL(63) | GENMASK_ULL(61, 59) | GENMASK_ULL(55, 52) | \
366 GENMASK_ULL(15, 12) | GENMASK_ULL(6, 0))
367
vgic_sanitise_pendbaser(u64 reg)368 static u64 vgic_sanitise_pendbaser(u64 reg)
369 {
370 reg = vgic_sanitise_field(reg, GICR_PENDBASER_SHAREABILITY_MASK,
371 GICR_PENDBASER_SHAREABILITY_SHIFT,
372 vgic_sanitise_shareability);
373 reg = vgic_sanitise_field(reg, GICR_PENDBASER_INNER_CACHEABILITY_MASK,
374 GICR_PENDBASER_INNER_CACHEABILITY_SHIFT,
375 vgic_sanitise_inner_cacheability);
376 reg = vgic_sanitise_field(reg, GICR_PENDBASER_OUTER_CACHEABILITY_MASK,
377 GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT,
378 vgic_sanitise_outer_cacheability);
379
380 reg &= ~PENDBASER_RES0_MASK;
381
382 return reg;
383 }
384
vgic_sanitise_propbaser(u64 reg)385 static u64 vgic_sanitise_propbaser(u64 reg)
386 {
387 reg = vgic_sanitise_field(reg, GICR_PROPBASER_SHAREABILITY_MASK,
388 GICR_PROPBASER_SHAREABILITY_SHIFT,
389 vgic_sanitise_shareability);
390 reg = vgic_sanitise_field(reg, GICR_PROPBASER_INNER_CACHEABILITY_MASK,
391 GICR_PROPBASER_INNER_CACHEABILITY_SHIFT,
392 vgic_sanitise_inner_cacheability);
393 reg = vgic_sanitise_field(reg, GICR_PROPBASER_OUTER_CACHEABILITY_MASK,
394 GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT,
395 vgic_sanitise_outer_cacheability);
396
397 reg &= ~PROPBASER_RES0_MASK;
398 return reg;
399 }
400
vgic_mmio_read_propbase(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)401 static unsigned long vgic_mmio_read_propbase(struct kvm_vcpu *vcpu,
402 gpa_t addr, unsigned int len)
403 {
404 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
405
406 return extract_bytes(dist->propbaser, addr & 7, len);
407 }
408
vgic_mmio_write_propbase(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)409 static void vgic_mmio_write_propbase(struct kvm_vcpu *vcpu,
410 gpa_t addr, unsigned int len,
411 unsigned long val)
412 {
413 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
414 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
415 u64 old_propbaser, propbaser;
416
417 /* Storing a value with LPIs already enabled is undefined */
418 if (vgic_cpu->lpis_enabled)
419 return;
420
421 do {
422 old_propbaser = READ_ONCE(dist->propbaser);
423 propbaser = old_propbaser;
424 propbaser = update_64bit_reg(propbaser, addr & 4, len, val);
425 propbaser = vgic_sanitise_propbaser(propbaser);
426 } while (cmpxchg64(&dist->propbaser, old_propbaser,
427 propbaser) != old_propbaser);
428 }
429
vgic_mmio_read_pendbase(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)430 static unsigned long vgic_mmio_read_pendbase(struct kvm_vcpu *vcpu,
431 gpa_t addr, unsigned int len)
432 {
433 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
434
435 return extract_bytes(vgic_cpu->pendbaser, addr & 7, len);
436 }
437
vgic_mmio_write_pendbase(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)438 static void vgic_mmio_write_pendbase(struct kvm_vcpu *vcpu,
439 gpa_t addr, unsigned int len,
440 unsigned long val)
441 {
442 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
443 u64 old_pendbaser, pendbaser;
444
445 /* Storing a value with LPIs already enabled is undefined */
446 if (vgic_cpu->lpis_enabled)
447 return;
448
449 do {
450 old_pendbaser = READ_ONCE(vgic_cpu->pendbaser);
451 pendbaser = old_pendbaser;
452 pendbaser = update_64bit_reg(pendbaser, addr & 4, len, val);
453 pendbaser = vgic_sanitise_pendbaser(pendbaser);
454 } while (cmpxchg64(&vgic_cpu->pendbaser, old_pendbaser,
455 pendbaser) != old_pendbaser);
456 }
457
458 /*
459 * The GICv3 per-IRQ registers are split to control PPIs and SGIs in the
460 * redistributors, while SPIs are covered by registers in the distributor
461 * block. Trying to set private IRQs in this block gets ignored.
462 * We take some special care here to fix the calculation of the register
463 * offset.
464 */
465 #define REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(off, rd, wr, ur, uw, bpi, acc) \
466 { \
467 .reg_offset = off, \
468 .bits_per_irq = bpi, \
469 .len = (bpi * VGIC_NR_PRIVATE_IRQS) / 8, \
470 .access_flags = acc, \
471 .read = vgic_mmio_read_raz, \
472 .write = vgic_mmio_write_wi, \
473 }, { \
474 .reg_offset = off + (bpi * VGIC_NR_PRIVATE_IRQS) / 8, \
475 .bits_per_irq = bpi, \
476 .len = (bpi * (1024 - VGIC_NR_PRIVATE_IRQS)) / 8, \
477 .access_flags = acc, \
478 .read = rd, \
479 .write = wr, \
480 .uaccess_read = ur, \
481 .uaccess_write = uw, \
482 }
483
484 static const struct vgic_register_region vgic_v3_dist_registers[] = {
485 REGISTER_DESC_WITH_LENGTH_UACCESS(GICD_CTLR,
486 vgic_mmio_read_v3_misc, vgic_mmio_write_v3_misc,
487 NULL, vgic_mmio_uaccess_write_v3_misc,
488 16, VGIC_ACCESS_32bit),
489 REGISTER_DESC_WITH_LENGTH(GICD_STATUSR,
490 vgic_mmio_read_rao, vgic_mmio_write_wi, 4,
491 VGIC_ACCESS_32bit),
492 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IGROUPR,
493 vgic_mmio_read_group, vgic_mmio_write_group, NULL, NULL, 1,
494 VGIC_ACCESS_32bit),
495 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ISENABLER,
496 vgic_mmio_read_enable, vgic_mmio_write_senable, NULL, NULL, 1,
497 VGIC_ACCESS_32bit),
498 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICENABLER,
499 vgic_mmio_read_enable, vgic_mmio_write_cenable, NULL, NULL, 1,
500 VGIC_ACCESS_32bit),
501 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ISPENDR,
502 vgic_mmio_read_pending, vgic_mmio_write_spending,
503 vgic_v3_uaccess_read_pending, vgic_v3_uaccess_write_pending, 1,
504 VGIC_ACCESS_32bit),
505 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICPENDR,
506 vgic_mmio_read_pending, vgic_mmio_write_cpending,
507 vgic_mmio_read_raz, vgic_mmio_uaccess_write_wi, 1,
508 VGIC_ACCESS_32bit),
509 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ISACTIVER,
510 vgic_mmio_read_active, vgic_mmio_write_sactive,
511 vgic_uaccess_read_active, vgic_mmio_uaccess_write_sactive, 1,
512 VGIC_ACCESS_32bit),
513 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICACTIVER,
514 vgic_mmio_read_active, vgic_mmio_write_cactive,
515 vgic_uaccess_read_active, vgic_mmio_uaccess_write_cactive,
516 1, VGIC_ACCESS_32bit),
517 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IPRIORITYR,
518 vgic_mmio_read_priority, vgic_mmio_write_priority, NULL, NULL,
519 8, VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
520 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ITARGETSR,
521 vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 8,
522 VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
523 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICFGR,
524 vgic_mmio_read_config, vgic_mmio_write_config, NULL, NULL, 2,
525 VGIC_ACCESS_32bit),
526 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IGRPMODR,
527 vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
528 VGIC_ACCESS_32bit),
529 REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IROUTER,
530 vgic_mmio_read_irouter, vgic_mmio_write_irouter, NULL, NULL, 64,
531 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
532 REGISTER_DESC_WITH_LENGTH(GICD_IDREGS,
533 vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48,
534 VGIC_ACCESS_32bit),
535 };
536
537 static const struct vgic_register_region vgic_v3_rd_registers[] = {
538 /* RD_base registers */
539 REGISTER_DESC_WITH_LENGTH(GICR_CTLR,
540 vgic_mmio_read_v3r_ctlr, vgic_mmio_write_v3r_ctlr, 4,
541 VGIC_ACCESS_32bit),
542 REGISTER_DESC_WITH_LENGTH(GICR_STATUSR,
543 vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
544 VGIC_ACCESS_32bit),
545 REGISTER_DESC_WITH_LENGTH(GICR_IIDR,
546 vgic_mmio_read_v3r_iidr, vgic_mmio_write_wi, 4,
547 VGIC_ACCESS_32bit),
548 REGISTER_DESC_WITH_LENGTH_UACCESS(GICR_TYPER,
549 vgic_mmio_read_v3r_typer, vgic_mmio_write_wi,
550 vgic_uaccess_read_v3r_typer, vgic_mmio_uaccess_write_wi, 8,
551 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
552 REGISTER_DESC_WITH_LENGTH(GICR_WAKER,
553 vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
554 VGIC_ACCESS_32bit),
555 REGISTER_DESC_WITH_LENGTH(GICR_PROPBASER,
556 vgic_mmio_read_propbase, vgic_mmio_write_propbase, 8,
557 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
558 REGISTER_DESC_WITH_LENGTH(GICR_PENDBASER,
559 vgic_mmio_read_pendbase, vgic_mmio_write_pendbase, 8,
560 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
561 REGISTER_DESC_WITH_LENGTH(GICR_IDREGS,
562 vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48,
563 VGIC_ACCESS_32bit),
564 /* SGI_base registers */
565 REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_IGROUPR0,
566 vgic_mmio_read_group, vgic_mmio_write_group, 4,
567 VGIC_ACCESS_32bit),
568 REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_ISENABLER0,
569 vgic_mmio_read_enable, vgic_mmio_write_senable, 4,
570 VGIC_ACCESS_32bit),
571 REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_ICENABLER0,
572 vgic_mmio_read_enable, vgic_mmio_write_cenable, 4,
573 VGIC_ACCESS_32bit),
574 REGISTER_DESC_WITH_LENGTH_UACCESS(SZ_64K + GICR_ISPENDR0,
575 vgic_mmio_read_pending, vgic_mmio_write_spending,
576 vgic_v3_uaccess_read_pending, vgic_v3_uaccess_write_pending, 4,
577 VGIC_ACCESS_32bit),
578 REGISTER_DESC_WITH_LENGTH_UACCESS(SZ_64K + GICR_ICPENDR0,
579 vgic_mmio_read_pending, vgic_mmio_write_cpending,
580 vgic_mmio_read_raz, vgic_mmio_uaccess_write_wi, 4,
581 VGIC_ACCESS_32bit),
582 REGISTER_DESC_WITH_LENGTH_UACCESS(SZ_64K + GICR_ISACTIVER0,
583 vgic_mmio_read_active, vgic_mmio_write_sactive,
584 vgic_uaccess_read_active, vgic_mmio_uaccess_write_sactive, 4,
585 VGIC_ACCESS_32bit),
586 REGISTER_DESC_WITH_LENGTH_UACCESS(SZ_64K + GICR_ICACTIVER0,
587 vgic_mmio_read_active, vgic_mmio_write_cactive,
588 vgic_uaccess_read_active, vgic_mmio_uaccess_write_cactive, 4,
589 VGIC_ACCESS_32bit),
590 REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_IPRIORITYR0,
591 vgic_mmio_read_priority, vgic_mmio_write_priority, 32,
592 VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
593 REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_ICFGR0,
594 vgic_mmio_read_config, vgic_mmio_write_config, 8,
595 VGIC_ACCESS_32bit),
596 REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_IGRPMODR0,
597 vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
598 VGIC_ACCESS_32bit),
599 REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_NSACR,
600 vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
601 VGIC_ACCESS_32bit),
602 };
603
vgic_v3_init_dist_iodev(struct vgic_io_device * dev)604 unsigned int vgic_v3_init_dist_iodev(struct vgic_io_device *dev)
605 {
606 dev->regions = vgic_v3_dist_registers;
607 dev->nr_regions = ARRAY_SIZE(vgic_v3_dist_registers);
608
609 kvm_iodevice_init(&dev->dev, &kvm_io_gic_ops);
610
611 return SZ_64K;
612 }
613
614 /**
615 * vgic_register_redist_iodev - register a single redist iodev
616 * @vcpu: The VCPU to which the redistributor belongs
617 *
618 * Register a KVM iodev for this VCPU's redistributor using the address
619 * provided.
620 *
621 * Return 0 on success, -ERRNO otherwise.
622 */
vgic_register_redist_iodev(struct kvm_vcpu * vcpu)623 int vgic_register_redist_iodev(struct kvm_vcpu *vcpu)
624 {
625 struct kvm *kvm = vcpu->kvm;
626 struct vgic_dist *vgic = &kvm->arch.vgic;
627 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
628 struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev;
629 struct vgic_redist_region *rdreg;
630 gpa_t rd_base;
631 int ret;
632
633 if (!IS_VGIC_ADDR_UNDEF(vgic_cpu->rd_iodev.base_addr))
634 return 0;
635
636 /*
637 * We may be creating VCPUs before having set the base address for the
638 * redistributor region, in which case we will come back to this
639 * function for all VCPUs when the base address is set. Just return
640 * without doing any work for now.
641 */
642 rdreg = vgic_v3_rdist_free_slot(&vgic->rd_regions);
643 if (!rdreg)
644 return 0;
645
646 if (!vgic_v3_check_base(kvm))
647 return -EINVAL;
648
649 vgic_cpu->rdreg = rdreg;
650
651 rd_base = rdreg->base + rdreg->free_index * KVM_VGIC_V3_REDIST_SIZE;
652
653 kvm_iodevice_init(&rd_dev->dev, &kvm_io_gic_ops);
654 rd_dev->base_addr = rd_base;
655 rd_dev->iodev_type = IODEV_REDIST;
656 rd_dev->regions = vgic_v3_rd_registers;
657 rd_dev->nr_regions = ARRAY_SIZE(vgic_v3_rd_registers);
658 rd_dev->redist_vcpu = vcpu;
659
660 mutex_lock(&kvm->slots_lock);
661 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, rd_base,
662 2 * SZ_64K, &rd_dev->dev);
663 mutex_unlock(&kvm->slots_lock);
664
665 if (ret)
666 return ret;
667
668 rdreg->free_index++;
669 return 0;
670 }
671
vgic_unregister_redist_iodev(struct kvm_vcpu * vcpu)672 static void vgic_unregister_redist_iodev(struct kvm_vcpu *vcpu)
673 {
674 struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev;
675
676 kvm_io_bus_unregister_dev(vcpu->kvm, KVM_MMIO_BUS, &rd_dev->dev);
677 }
678
vgic_register_all_redist_iodevs(struct kvm * kvm)679 static int vgic_register_all_redist_iodevs(struct kvm *kvm)
680 {
681 struct kvm_vcpu *vcpu;
682 int c, ret = 0;
683
684 kvm_for_each_vcpu(c, vcpu, kvm) {
685 ret = vgic_register_redist_iodev(vcpu);
686 if (ret)
687 break;
688 }
689
690 if (ret) {
691 /* The current c failed, so we start with the previous one. */
692 mutex_lock(&kvm->slots_lock);
693 for (c--; c >= 0; c--) {
694 vcpu = kvm_get_vcpu(kvm, c);
695 vgic_unregister_redist_iodev(vcpu);
696 }
697 mutex_unlock(&kvm->slots_lock);
698 }
699
700 return ret;
701 }
702
703 /**
704 * vgic_v3_insert_redist_region - Insert a new redistributor region
705 *
706 * Performs various checks before inserting the rdist region in the list.
707 * Those tests depend on whether the size of the rdist region is known
708 * (ie. count != 0). The list is sorted by rdist region index.
709 *
710 * @kvm: kvm handle
711 * @index: redist region index
712 * @base: base of the new rdist region
713 * @count: number of redistributors the region is made of (0 in the old style
714 * single region, whose size is induced from the number of vcpus)
715 *
716 * Return 0 on success, < 0 otherwise
717 */
vgic_v3_insert_redist_region(struct kvm * kvm,uint32_t index,gpa_t base,uint32_t count)718 static int vgic_v3_insert_redist_region(struct kvm *kvm, uint32_t index,
719 gpa_t base, uint32_t count)
720 {
721 struct vgic_dist *d = &kvm->arch.vgic;
722 struct vgic_redist_region *rdreg;
723 struct list_head *rd_regions = &d->rd_regions;
724 size_t size = count * KVM_VGIC_V3_REDIST_SIZE;
725 int ret;
726
727 /* single rdist region already set ?*/
728 if (!count && !list_empty(rd_regions))
729 return -EINVAL;
730
731 /* cross the end of memory ? */
732 if (base + size < base)
733 return -EINVAL;
734
735 if (list_empty(rd_regions)) {
736 if (index != 0)
737 return -EINVAL;
738 } else {
739 rdreg = list_last_entry(rd_regions,
740 struct vgic_redist_region, list);
741 if (index != rdreg->index + 1)
742 return -EINVAL;
743
744 /* Cannot add an explicitly sized regions after legacy region */
745 if (!rdreg->count)
746 return -EINVAL;
747 }
748
749 /*
750 * For legacy single-region redistributor regions (!count),
751 * check that the redistributor region does not overlap with the
752 * distributor's address space.
753 */
754 if (!count && !IS_VGIC_ADDR_UNDEF(d->vgic_dist_base) &&
755 vgic_dist_overlap(kvm, base, size))
756 return -EINVAL;
757
758 /* collision with any other rdist region? */
759 if (vgic_v3_rdist_overlap(kvm, base, size))
760 return -EINVAL;
761
762 rdreg = kzalloc(sizeof(*rdreg), GFP_KERNEL);
763 if (!rdreg)
764 return -ENOMEM;
765
766 rdreg->base = VGIC_ADDR_UNDEF;
767
768 ret = vgic_check_ioaddr(kvm, &rdreg->base, base, SZ_64K);
769 if (ret)
770 goto free;
771
772 rdreg->base = base;
773 rdreg->count = count;
774 rdreg->free_index = 0;
775 rdreg->index = index;
776
777 list_add_tail(&rdreg->list, rd_regions);
778 return 0;
779 free:
780 kfree(rdreg);
781 return ret;
782 }
783
vgic_v3_set_redist_base(struct kvm * kvm,u32 index,u64 addr,u32 count)784 int vgic_v3_set_redist_base(struct kvm *kvm, u32 index, u64 addr, u32 count)
785 {
786 int ret;
787
788 ret = vgic_v3_insert_redist_region(kvm, index, addr, count);
789 if (ret)
790 return ret;
791
792 /*
793 * Register iodevs for each existing VCPU. Adding more VCPUs
794 * afterwards will register the iodevs when needed.
795 */
796 ret = vgic_register_all_redist_iodevs(kvm);
797 if (ret)
798 return ret;
799
800 return 0;
801 }
802
vgic_v3_has_attr_regs(struct kvm_device * dev,struct kvm_device_attr * attr)803 int vgic_v3_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr)
804 {
805 const struct vgic_register_region *region;
806 struct vgic_io_device iodev;
807 struct vgic_reg_attr reg_attr;
808 struct kvm_vcpu *vcpu;
809 gpa_t addr;
810 int ret;
811
812 ret = vgic_v3_parse_attr(dev, attr, ®_attr);
813 if (ret)
814 return ret;
815
816 vcpu = reg_attr.vcpu;
817 addr = reg_attr.addr;
818
819 switch (attr->group) {
820 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
821 iodev.regions = vgic_v3_dist_registers;
822 iodev.nr_regions = ARRAY_SIZE(vgic_v3_dist_registers);
823 iodev.base_addr = 0;
824 break;
825 case KVM_DEV_ARM_VGIC_GRP_REDIST_REGS:{
826 iodev.regions = vgic_v3_rd_registers;
827 iodev.nr_regions = ARRAY_SIZE(vgic_v3_rd_registers);
828 iodev.base_addr = 0;
829 break;
830 }
831 case KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS: {
832 u64 reg, id;
833
834 id = (attr->attr & KVM_DEV_ARM_VGIC_SYSREG_INSTR_MASK);
835 return vgic_v3_has_cpu_sysregs_attr(vcpu, 0, id, ®);
836 }
837 default:
838 return -ENXIO;
839 }
840
841 /* We only support aligned 32-bit accesses. */
842 if (addr & 3)
843 return -ENXIO;
844
845 region = vgic_get_mmio_region(vcpu, &iodev, addr, sizeof(u32));
846 if (!region)
847 return -ENXIO;
848
849 return 0;
850 }
851 /*
852 * Compare a given affinity (level 1-3 and a level 0 mask, from the SGI
853 * generation register ICC_SGI1R_EL1) with a given VCPU.
854 * If the VCPU's MPIDR matches, return the level0 affinity, otherwise
855 * return -1.
856 */
match_mpidr(u64 sgi_aff,u16 sgi_cpu_mask,struct kvm_vcpu * vcpu)857 static int match_mpidr(u64 sgi_aff, u16 sgi_cpu_mask, struct kvm_vcpu *vcpu)
858 {
859 unsigned long affinity;
860 int level0;
861
862 /*
863 * Split the current VCPU's MPIDR into affinity level 0 and the
864 * rest as this is what we have to compare against.
865 */
866 affinity = kvm_vcpu_get_mpidr_aff(vcpu);
867 level0 = MPIDR_AFFINITY_LEVEL(affinity, 0);
868 affinity &= ~MPIDR_LEVEL_MASK;
869
870 /* bail out if the upper three levels don't match */
871 if (sgi_aff != affinity)
872 return -1;
873
874 /* Is this VCPU's bit set in the mask ? */
875 if (!(sgi_cpu_mask & BIT(level0)))
876 return -1;
877
878 return level0;
879 }
880
881 /*
882 * The ICC_SGI* registers encode the affinity differently from the MPIDR,
883 * so provide a wrapper to use the existing defines to isolate a certain
884 * affinity level.
885 */
886 #define SGI_AFFINITY_LEVEL(reg, level) \
887 ((((reg) & ICC_SGI1R_AFFINITY_## level ##_MASK) \
888 >> ICC_SGI1R_AFFINITY_## level ##_SHIFT) << MPIDR_LEVEL_SHIFT(level))
889
890 /**
891 * vgic_v3_dispatch_sgi - handle SGI requests from VCPUs
892 * @vcpu: The VCPU requesting a SGI
893 * @reg: The value written into ICC_{ASGI1,SGI0,SGI1}R by that VCPU
894 * @allow_group1: Does the sysreg access allow generation of G1 SGIs
895 *
896 * With GICv3 (and ARE=1) CPUs trigger SGIs by writing to a system register.
897 * This will trap in sys_regs.c and call this function.
898 * This ICC_SGI1R_EL1 register contains the upper three affinity levels of the
899 * target processors as well as a bitmask of 16 Aff0 CPUs.
900 * If the interrupt routing mode bit is not set, we iterate over all VCPUs to
901 * check for matching ones. If this bit is set, we signal all, but not the
902 * calling VCPU.
903 */
vgic_v3_dispatch_sgi(struct kvm_vcpu * vcpu,u64 reg,bool allow_group1)904 void vgic_v3_dispatch_sgi(struct kvm_vcpu *vcpu, u64 reg, bool allow_group1)
905 {
906 struct kvm *kvm = vcpu->kvm;
907 struct kvm_vcpu *c_vcpu;
908 u16 target_cpus;
909 u64 mpidr;
910 int sgi, c;
911 int vcpu_id = vcpu->vcpu_id;
912 bool broadcast;
913 unsigned long flags;
914
915 sgi = (reg & ICC_SGI1R_SGI_ID_MASK) >> ICC_SGI1R_SGI_ID_SHIFT;
916 broadcast = reg & BIT_ULL(ICC_SGI1R_IRQ_ROUTING_MODE_BIT);
917 target_cpus = (reg & ICC_SGI1R_TARGET_LIST_MASK) >> ICC_SGI1R_TARGET_LIST_SHIFT;
918 mpidr = SGI_AFFINITY_LEVEL(reg, 3);
919 mpidr |= SGI_AFFINITY_LEVEL(reg, 2);
920 mpidr |= SGI_AFFINITY_LEVEL(reg, 1);
921
922 /*
923 * We iterate over all VCPUs to find the MPIDRs matching the request.
924 * If we have handled one CPU, we clear its bit to detect early
925 * if we are already finished. This avoids iterating through all
926 * VCPUs when most of the times we just signal a single VCPU.
927 */
928 kvm_for_each_vcpu(c, c_vcpu, kvm) {
929 struct vgic_irq *irq;
930
931 /* Exit early if we have dealt with all requested CPUs */
932 if (!broadcast && target_cpus == 0)
933 break;
934
935 /* Don't signal the calling VCPU */
936 if (broadcast && c == vcpu_id)
937 continue;
938
939 if (!broadcast) {
940 int level0;
941
942 level0 = match_mpidr(mpidr, target_cpus, c_vcpu);
943 if (level0 == -1)
944 continue;
945
946 /* remove this matching VCPU from the mask */
947 target_cpus &= ~BIT(level0);
948 }
949
950 irq = vgic_get_irq(vcpu->kvm, c_vcpu, sgi);
951
952 raw_spin_lock_irqsave(&irq->irq_lock, flags);
953
954 /*
955 * An access targetting Group0 SGIs can only generate
956 * those, while an access targetting Group1 SGIs can
957 * generate interrupts of either group.
958 */
959 if (!irq->group || allow_group1) {
960 irq->pending_latch = true;
961 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
962 } else {
963 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
964 }
965
966 vgic_put_irq(vcpu->kvm, irq);
967 }
968 }
969
vgic_v3_dist_uaccess(struct kvm_vcpu * vcpu,bool is_write,int offset,u32 * val)970 int vgic_v3_dist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
971 int offset, u32 *val)
972 {
973 struct vgic_io_device dev = {
974 .regions = vgic_v3_dist_registers,
975 .nr_regions = ARRAY_SIZE(vgic_v3_dist_registers),
976 };
977
978 return vgic_uaccess(vcpu, &dev, is_write, offset, val);
979 }
980
vgic_v3_redist_uaccess(struct kvm_vcpu * vcpu,bool is_write,int offset,u32 * val)981 int vgic_v3_redist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
982 int offset, u32 *val)
983 {
984 struct vgic_io_device rd_dev = {
985 .regions = vgic_v3_rd_registers,
986 .nr_regions = ARRAY_SIZE(vgic_v3_rd_registers),
987 };
988
989 return vgic_uaccess(vcpu, &rd_dev, is_write, offset, val);
990 }
991
vgic_v3_line_level_info_uaccess(struct kvm_vcpu * vcpu,bool is_write,u32 intid,u64 * val)992 int vgic_v3_line_level_info_uaccess(struct kvm_vcpu *vcpu, bool is_write,
993 u32 intid, u64 *val)
994 {
995 if (intid % 32)
996 return -EINVAL;
997
998 if (is_write)
999 vgic_write_irq_line_level_info(vcpu, intid, *val);
1000 else
1001 *val = vgic_read_irq_line_level_info(vcpu, intid);
1002
1003 return 0;
1004 }
1005