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