blob: 37fd20d3575942cbab5b322817809eebbcea4957 [file] [log] [blame]
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001/*
2 * Copyright (C) 2012 ARM Ltd.
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
Marc Zyngier01ac5e32013-01-21 19:36:16 -050019#include <linux/cpu.h>
Marc Zyngier1a89dd92013-01-21 19:36:12 -050020#include <linux/kvm.h>
21#include <linux/kvm_host.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
Marc Zyngier01ac5e32013-01-21 19:36:16 -050024#include <linux/of.h>
25#include <linux/of_address.h>
26#include <linux/of_irq.h>
Christoffer Dall2a2f3e262014-02-02 13:41:02 -080027#include <linux/uaccess.h>
Marc Zyngier01ac5e32013-01-21 19:36:16 -050028
29#include <linux/irqchip/arm-gic.h>
30
Marc Zyngier1a89dd92013-01-21 19:36:12 -050031#include <asm/kvm_emulate.h>
Marc Zyngier01ac5e32013-01-21 19:36:16 -050032#include <asm/kvm_arm.h>
33#include <asm/kvm_mmu.h>
Marc Zyngier1a89dd92013-01-21 19:36:12 -050034
Marc Zyngierb47ef922013-01-21 19:36:14 -050035/*
36 * How the whole thing works (courtesy of Christoffer Dall):
37 *
38 * - At any time, the dist->irq_pending_on_cpu is the oracle that knows if
Christoffer Dall7e362912014-06-14 22:34:04 +020039 * something is pending on the CPU interface.
40 * - Interrupts that are pending on the distributor are stored on the
41 * vgic.irq_pending vgic bitmap (this bitmap is updated by both user land
42 * ioctls and guest mmio ops, and other in-kernel peripherals such as the
43 * arch. timers).
Marc Zyngierb47ef922013-01-21 19:36:14 -050044 * - Every time the bitmap changes, the irq_pending_on_cpu oracle is
45 * recalculated
46 * - To calculate the oracle, we need info for each cpu from
47 * compute_pending_for_cpu, which considers:
Christoffer Dall227844f2014-06-09 12:27:18 +020048 * - PPI: dist->irq_pending & dist->irq_enable
49 * - SPI: dist->irq_pending & dist->irq_enable & dist->irq_spi_target
Christoffer Dall7e362912014-06-14 22:34:04 +020050 * - irq_spi_target is a 'formatted' version of the GICD_ITARGETSRn
Marc Zyngierb47ef922013-01-21 19:36:14 -050051 * registers, stored on each vcpu. We only keep one bit of
52 * information per interrupt, making sure that only one vcpu can
53 * accept the interrupt.
Christoffer Dall7e362912014-06-14 22:34:04 +020054 * - If any of the above state changes, we must recalculate the oracle.
Marc Zyngierb47ef922013-01-21 19:36:14 -050055 * - The same is true when injecting an interrupt, except that we only
56 * consider a single interrupt at a time. The irq_spi_cpu array
57 * contains the target CPU for each SPI.
58 *
59 * The handling of level interrupts adds some extra complexity. We
60 * need to track when the interrupt has been EOIed, so we can sample
61 * the 'line' again. This is achieved as such:
62 *
63 * - When a level interrupt is moved onto a vcpu, the corresponding
Christoffer Dalldbf20f92014-06-09 12:55:13 +020064 * bit in irq_queued is set. As long as this bit is set, the line
Marc Zyngierb47ef922013-01-21 19:36:14 -050065 * will be ignored for further interrupts. The interrupt is injected
66 * into the vcpu with the GICH_LR_EOI bit set (generate a
67 * maintenance interrupt on EOI).
68 * - When the interrupt is EOIed, the maintenance interrupt fires,
Christoffer Dalldbf20f92014-06-09 12:55:13 +020069 * and clears the corresponding bit in irq_queued. This allows the
Marc Zyngierb47ef922013-01-21 19:36:14 -050070 * interrupt line to be sampled again.
Christoffer Dallfaa1b462014-06-14 21:54:51 +020071 * - Note that level-triggered interrupts can also be set to pending from
72 * writes to GICD_ISPENDRn and lowering the external input line does not
73 * cause the interrupt to become inactive in such a situation.
74 * Conversely, writes to GICD_ICPENDRn do not cause the interrupt to become
75 * inactive as long as the external input line is held high.
Marc Zyngierb47ef922013-01-21 19:36:14 -050076 */
77
Christoffer Dall330690c2013-01-21 19:36:13 -050078#define VGIC_ADDR_UNDEF (-1)
79#define IS_VGIC_ADDR_UNDEF(_x) ((_x) == VGIC_ADDR_UNDEF)
80
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -070081#define PRODUCT_ID_KVM 0x4b /* ASCII code K */
82#define IMPLEMENTER_ARM 0x43b
83#define GICC_ARCH_VERSION_V2 0x2
84
Marc Zyngier1a89dd92013-01-21 19:36:12 -050085#define ACCESS_READ_VALUE (1 << 0)
86#define ACCESS_READ_RAZ (0 << 0)
87#define ACCESS_READ_MASK(x) ((x) & (1 << 0))
88#define ACCESS_WRITE_IGNORED (0 << 1)
89#define ACCESS_WRITE_SETBIT (1 << 1)
90#define ACCESS_WRITE_CLEARBIT (2 << 1)
91#define ACCESS_WRITE_VALUE (3 << 1)
92#define ACCESS_WRITE_MASK(x) ((x) & (3 << 1))
93
Marc Zyngiera1fcb442013-01-21 19:36:15 -050094static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +010095static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -050096static void vgic_update_state(struct kvm *kvm);
Marc Zyngier5863c2c2013-01-21 19:36:15 -050097static void vgic_kick_vcpus(struct kvm *kvm);
Marc Zyngierb47ef922013-01-21 19:36:14 -050098static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +010099static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr);
100static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, struct vgic_lr lr_desc);
Marc Zyngierbeee38b2014-02-04 17:48:10 +0000101static void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
102static void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500103
Marc Zyngier8f186d52014-02-04 18:13:03 +0000104static const struct vgic_ops *vgic_ops;
105static const struct vgic_params *vgic;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500106
Victor Kamensky9662fb42014-06-12 09:30:10 -0700107/*
108 * struct vgic_bitmap contains unions that provide two views of
109 * the same data. In one case it is an array of registers of
110 * u32's, and in the other case it is a bitmap of unsigned
111 * longs.
112 *
113 * This does not work on 64-bit BE systems, because the bitmap access
114 * will store two consecutive 32-bit words with the higher-addressed
115 * register's bits at the lower index and the lower-addressed register's
116 * bits at the higher index.
117 *
118 * Therefore, swizzle the register index when accessing the 32-bit word
119 * registers to access the right register's value.
120 */
121#if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 64
122#define REG_OFFSET_SWIZZLE 1
123#else
124#define REG_OFFSET_SWIZZLE 0
125#endif
Marc Zyngierb47ef922013-01-21 19:36:14 -0500126
127static u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x,
128 int cpuid, u32 offset)
129{
130 offset >>= 2;
131 if (!offset)
Victor Kamensky9662fb42014-06-12 09:30:10 -0700132 return x->percpu[cpuid].reg + (offset ^ REG_OFFSET_SWIZZLE);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500133 else
Victor Kamensky9662fb42014-06-12 09:30:10 -0700134 return x->shared.reg + ((offset - 1) ^ REG_OFFSET_SWIZZLE);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500135}
136
137static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
138 int cpuid, int irq)
139{
140 if (irq < VGIC_NR_PRIVATE_IRQS)
141 return test_bit(irq, x->percpu[cpuid].reg_ul);
142
143 return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared.reg_ul);
144}
145
146static void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
147 int irq, int val)
148{
149 unsigned long *reg;
150
151 if (irq < VGIC_NR_PRIVATE_IRQS) {
152 reg = x->percpu[cpuid].reg_ul;
153 } else {
154 reg = x->shared.reg_ul;
155 irq -= VGIC_NR_PRIVATE_IRQS;
156 }
157
158 if (val)
159 set_bit(irq, reg);
160 else
161 clear_bit(irq, reg);
162}
163
164static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
165{
166 if (unlikely(cpuid >= VGIC_MAX_CPUS))
167 return NULL;
168 return x->percpu[cpuid].reg_ul;
169}
170
171static unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
172{
173 return x->shared.reg_ul;
174}
175
176static u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
177{
178 offset >>= 2;
179 BUG_ON(offset > (VGIC_NR_IRQS / 4));
Christoffer Dall8d989152013-08-29 11:08:24 +0100180 if (offset < 8)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500181 return x->percpu[cpuid] + offset;
182 else
183 return x->shared + offset - 8;
184}
185
186#define VGIC_CFG_LEVEL 0
187#define VGIC_CFG_EDGE 1
188
189static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq)
190{
191 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
192 int irq_val;
193
194 irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq);
195 return irq_val == VGIC_CFG_EDGE;
196}
197
198static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq)
199{
200 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
201
202 return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq);
203}
204
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200205static int vgic_irq_is_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500206{
207 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
208
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200209 return vgic_bitmap_get_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500210}
211
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200212static void vgic_irq_set_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500213{
214 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
215
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200216 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 1);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500217}
218
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200219static void vgic_irq_clear_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500220{
221 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
222
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200223 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 0);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500224}
225
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200226static int vgic_dist_irq_get_level(struct kvm_vcpu *vcpu, int irq)
227{
228 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
229
230 return vgic_bitmap_get_irq_val(&dist->irq_level, vcpu->vcpu_id, irq);
231}
232
233static void vgic_dist_irq_set_level(struct kvm_vcpu *vcpu, int irq)
234{
235 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
236
237 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 1);
238}
239
240static void vgic_dist_irq_clear_level(struct kvm_vcpu *vcpu, int irq)
241{
242 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
243
244 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 0);
245}
246
247static int vgic_dist_irq_soft_pend(struct kvm_vcpu *vcpu, int irq)
248{
249 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
250
251 return vgic_bitmap_get_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq);
252}
253
254static void vgic_dist_irq_clear_soft_pend(struct kvm_vcpu *vcpu, int irq)
255{
256 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
257
258 vgic_bitmap_set_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq, 0);
259}
260
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500261static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
262{
263 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
264
Christoffer Dall227844f2014-06-09 12:27:18 +0200265 return vgic_bitmap_get_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500266}
267
Christoffer Dall227844f2014-06-09 12:27:18 +0200268static void vgic_dist_irq_set_pending(struct kvm_vcpu *vcpu, int irq)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500269{
270 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
271
Christoffer Dall227844f2014-06-09 12:27:18 +0200272 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 1);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500273}
274
Christoffer Dall227844f2014-06-09 12:27:18 +0200275static void vgic_dist_irq_clear_pending(struct kvm_vcpu *vcpu, int irq)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500276{
277 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
278
Christoffer Dall227844f2014-06-09 12:27:18 +0200279 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 0);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500280}
281
282static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq)
283{
284 if (irq < VGIC_NR_PRIVATE_IRQS)
285 set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
286 else
287 set_bit(irq - VGIC_NR_PRIVATE_IRQS,
288 vcpu->arch.vgic_cpu.pending_shared);
289}
290
291static void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
292{
293 if (irq < VGIC_NR_PRIVATE_IRQS)
294 clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
295 else
296 clear_bit(irq - VGIC_NR_PRIVATE_IRQS,
297 vcpu->arch.vgic_cpu.pending_shared);
298}
299
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200300static bool vgic_can_sample_irq(struct kvm_vcpu *vcpu, int irq)
301{
302 return vgic_irq_is_edge(vcpu, irq) || !vgic_irq_is_queued(vcpu, irq);
303}
304
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500305static u32 mmio_data_read(struct kvm_exit_mmio *mmio, u32 mask)
306{
Victor Kamensky1c9f0472014-06-12 09:30:04 -0700307 return le32_to_cpu(*((u32 *)mmio->data)) & mask;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500308}
309
310static void mmio_data_write(struct kvm_exit_mmio *mmio, u32 mask, u32 value)
311{
Victor Kamensky1c9f0472014-06-12 09:30:04 -0700312 *((u32 *)mmio->data) = cpu_to_le32(value) & mask;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500313}
314
315/**
316 * vgic_reg_access - access vgic register
317 * @mmio: pointer to the data describing the mmio access
318 * @reg: pointer to the virtual backing of vgic distributor data
319 * @offset: least significant 2 bits used for word offset
320 * @mode: ACCESS_ mode (see defines above)
321 *
322 * Helper to make vgic register access easier using one of the access
323 * modes defined for vgic register access
324 * (read,raz,write-ignored,setbit,clearbit,write)
325 */
326static void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
327 phys_addr_t offset, int mode)
328{
329 int word_offset = (offset & 3) * 8;
330 u32 mask = (1UL << (mmio->len * 8)) - 1;
331 u32 regval;
332
333 /*
334 * Any alignment fault should have been delivered to the guest
335 * directly (ARM ARM B3.12.7 "Prioritization of aborts").
336 */
337
338 if (reg) {
339 regval = *reg;
340 } else {
341 BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED));
342 regval = 0;
343 }
344
345 if (mmio->is_write) {
346 u32 data = mmio_data_read(mmio, mask) << word_offset;
347 switch (ACCESS_WRITE_MASK(mode)) {
348 case ACCESS_WRITE_IGNORED:
349 return;
350
351 case ACCESS_WRITE_SETBIT:
352 regval |= data;
353 break;
354
355 case ACCESS_WRITE_CLEARBIT:
356 regval &= ~data;
357 break;
358
359 case ACCESS_WRITE_VALUE:
360 regval = (regval & ~(mask << word_offset)) | data;
361 break;
362 }
363 *reg = regval;
364 } else {
365 switch (ACCESS_READ_MASK(mode)) {
366 case ACCESS_READ_RAZ:
367 regval = 0;
368 /* fall through */
369
370 case ACCESS_READ_VALUE:
371 mmio_data_write(mmio, mask, regval >> word_offset);
372 }
373 }
374}
375
Marc Zyngierb47ef922013-01-21 19:36:14 -0500376static bool handle_mmio_misc(struct kvm_vcpu *vcpu,
377 struct kvm_exit_mmio *mmio, phys_addr_t offset)
378{
379 u32 reg;
380 u32 word_offset = offset & 3;
381
382 switch (offset & ~3) {
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -0700383 case 0: /* GICD_CTLR */
Marc Zyngierb47ef922013-01-21 19:36:14 -0500384 reg = vcpu->kvm->arch.vgic.enabled;
385 vgic_reg_access(mmio, &reg, word_offset,
386 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
387 if (mmio->is_write) {
388 vcpu->kvm->arch.vgic.enabled = reg & 1;
389 vgic_update_state(vcpu->kvm);
390 return true;
391 }
392 break;
393
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -0700394 case 4: /* GICD_TYPER */
Marc Zyngierb47ef922013-01-21 19:36:14 -0500395 reg = (atomic_read(&vcpu->kvm->online_vcpus) - 1) << 5;
396 reg |= (VGIC_NR_IRQS >> 5) - 1;
397 vgic_reg_access(mmio, &reg, word_offset,
398 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
399 break;
400
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -0700401 case 8: /* GICD_IIDR */
402 reg = (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500403 vgic_reg_access(mmio, &reg, word_offset,
404 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
405 break;
406 }
407
408 return false;
409}
410
411static bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu,
412 struct kvm_exit_mmio *mmio, phys_addr_t offset)
413{
414 vgic_reg_access(mmio, NULL, offset,
415 ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
416 return false;
417}
418
419static bool handle_mmio_set_enable_reg(struct kvm_vcpu *vcpu,
420 struct kvm_exit_mmio *mmio,
421 phys_addr_t offset)
422{
423 u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
424 vcpu->vcpu_id, offset);
425 vgic_reg_access(mmio, reg, offset,
426 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
427 if (mmio->is_write) {
428 vgic_update_state(vcpu->kvm);
429 return true;
430 }
431
432 return false;
433}
434
435static bool handle_mmio_clear_enable_reg(struct kvm_vcpu *vcpu,
436 struct kvm_exit_mmio *mmio,
437 phys_addr_t offset)
438{
439 u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
440 vcpu->vcpu_id, offset);
441 vgic_reg_access(mmio, reg, offset,
442 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
443 if (mmio->is_write) {
444 if (offset < 4) /* Force SGI enabled */
445 *reg |= 0xffff;
Marc Zyngiera1fcb442013-01-21 19:36:15 -0500446 vgic_retire_disabled_irqs(vcpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500447 vgic_update_state(vcpu->kvm);
448 return true;
449 }
450
451 return false;
452}
453
454static bool handle_mmio_set_pending_reg(struct kvm_vcpu *vcpu,
455 struct kvm_exit_mmio *mmio,
456 phys_addr_t offset)
457{
Christoffer Dall9da48b52014-06-14 22:30:45 +0200458 u32 *reg, orig;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200459 u32 level_mask;
460 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
461
462 reg = vgic_bitmap_get_reg(&dist->irq_cfg, vcpu->vcpu_id, offset);
463 level_mask = (~(*reg));
464
465 /* Mark both level and edge triggered irqs as pending */
466 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu->vcpu_id, offset);
Christoffer Dall9da48b52014-06-14 22:30:45 +0200467 orig = *reg;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500468 vgic_reg_access(mmio, reg, offset,
469 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200470
Marc Zyngierb47ef922013-01-21 19:36:14 -0500471 if (mmio->is_write) {
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200472 /* Set the soft-pending flag only for level-triggered irqs */
473 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
474 vcpu->vcpu_id, offset);
475 vgic_reg_access(mmio, reg, offset,
476 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
477 *reg &= level_mask;
478
Christoffer Dall9da48b52014-06-14 22:30:45 +0200479 /* Ignore writes to SGIs */
480 if (offset < 2) {
481 *reg &= ~0xffff;
482 *reg |= orig & 0xffff;
483 }
484
Marc Zyngierb47ef922013-01-21 19:36:14 -0500485 vgic_update_state(vcpu->kvm);
486 return true;
487 }
488
489 return false;
490}
491
492static bool handle_mmio_clear_pending_reg(struct kvm_vcpu *vcpu,
493 struct kvm_exit_mmio *mmio,
494 phys_addr_t offset)
495{
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200496 u32 *level_active;
Christoffer Dall9da48b52014-06-14 22:30:45 +0200497 u32 *reg, orig;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200498 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
499
500 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu->vcpu_id, offset);
Christoffer Dall9da48b52014-06-14 22:30:45 +0200501 orig = *reg;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500502 vgic_reg_access(mmio, reg, offset,
503 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
504 if (mmio->is_write) {
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200505 /* Re-set level triggered level-active interrupts */
506 level_active = vgic_bitmap_get_reg(&dist->irq_level,
507 vcpu->vcpu_id, offset);
508 reg = vgic_bitmap_get_reg(&dist->irq_pending,
509 vcpu->vcpu_id, offset);
510 *reg |= *level_active;
511
Christoffer Dall9da48b52014-06-14 22:30:45 +0200512 /* Ignore writes to SGIs */
513 if (offset < 2) {
514 *reg &= ~0xffff;
515 *reg |= orig & 0xffff;
516 }
517
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200518 /* Clear soft-pending flags */
519 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
520 vcpu->vcpu_id, offset);
521 vgic_reg_access(mmio, reg, offset,
522 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
523
Marc Zyngierb47ef922013-01-21 19:36:14 -0500524 vgic_update_state(vcpu->kvm);
525 return true;
526 }
527
528 return false;
529}
530
531static bool handle_mmio_priority_reg(struct kvm_vcpu *vcpu,
532 struct kvm_exit_mmio *mmio,
533 phys_addr_t offset)
534{
535 u32 *reg = vgic_bytemap_get_reg(&vcpu->kvm->arch.vgic.irq_priority,
536 vcpu->vcpu_id, offset);
537 vgic_reg_access(mmio, reg, offset,
538 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
539 return false;
540}
541
542#define GICD_ITARGETSR_SIZE 32
543#define GICD_CPUTARGETS_BITS 8
544#define GICD_IRQS_PER_ITARGETSR (GICD_ITARGETSR_SIZE / GICD_CPUTARGETS_BITS)
545static u32 vgic_get_target_reg(struct kvm *kvm, int irq)
546{
547 struct vgic_dist *dist = &kvm->arch.vgic;
Marc Zyngier986af8e2013-08-29 11:08:22 +0100548 int i;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500549 u32 val = 0;
550
551 irq -= VGIC_NR_PRIVATE_IRQS;
552
Marc Zyngier986af8e2013-08-29 11:08:22 +0100553 for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++)
554 val |= 1 << (dist->irq_spi_cpu[irq + i] + i * 8);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500555
556 return val;
557}
558
559static void vgic_set_target_reg(struct kvm *kvm, u32 val, int irq)
560{
561 struct vgic_dist *dist = &kvm->arch.vgic;
562 struct kvm_vcpu *vcpu;
563 int i, c;
564 unsigned long *bmap;
565 u32 target;
566
567 irq -= VGIC_NR_PRIVATE_IRQS;
568
569 /*
570 * Pick the LSB in each byte. This ensures we target exactly
571 * one vcpu per IRQ. If the byte is null, assume we target
572 * CPU0.
573 */
574 for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++) {
575 int shift = i * GICD_CPUTARGETS_BITS;
576 target = ffs((val >> shift) & 0xffU);
577 target = target ? (target - 1) : 0;
578 dist->irq_spi_cpu[irq + i] = target;
579 kvm_for_each_vcpu(c, vcpu, kvm) {
580 bmap = vgic_bitmap_get_shared_map(&dist->irq_spi_target[c]);
581 if (c == target)
582 set_bit(irq + i, bmap);
583 else
584 clear_bit(irq + i, bmap);
585 }
586 }
587}
588
589static bool handle_mmio_target_reg(struct kvm_vcpu *vcpu,
590 struct kvm_exit_mmio *mmio,
591 phys_addr_t offset)
592{
593 u32 reg;
594
595 /* We treat the banked interrupts targets as read-only */
596 if (offset < 32) {
597 u32 roreg = 1 << vcpu->vcpu_id;
598 roreg |= roreg << 8;
599 roreg |= roreg << 16;
600
601 vgic_reg_access(mmio, &roreg, offset,
602 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
603 return false;
604 }
605
606 reg = vgic_get_target_reg(vcpu->kvm, offset & ~3U);
607 vgic_reg_access(mmio, &reg, offset,
608 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
609 if (mmio->is_write) {
610 vgic_set_target_reg(vcpu->kvm, reg, offset & ~3U);
611 vgic_update_state(vcpu->kvm);
612 return true;
613 }
614
615 return false;
616}
617
618static u32 vgic_cfg_expand(u16 val)
619{
620 u32 res = 0;
621 int i;
622
623 /*
624 * Turn a 16bit value like abcd...mnop into a 32bit word
625 * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
626 */
627 for (i = 0; i < 16; i++)
628 res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
629
630 return res;
631}
632
633static u16 vgic_cfg_compress(u32 val)
634{
635 u16 res = 0;
636 int i;
637
638 /*
639 * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
640 * abcd...mnop which is what we really care about.
641 */
642 for (i = 0; i < 16; i++)
643 res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
644
645 return res;
646}
647
648/*
649 * The distributor uses 2 bits per IRQ for the CFG register, but the
650 * LSB is always 0. As such, we only keep the upper bit, and use the
651 * two above functions to compress/expand the bits
652 */
653static bool handle_mmio_cfg_reg(struct kvm_vcpu *vcpu,
654 struct kvm_exit_mmio *mmio, phys_addr_t offset)
655{
656 u32 val;
Marc Zyngier6545eae2013-08-29 11:08:23 +0100657 u32 *reg;
658
Marc Zyngier6545eae2013-08-29 11:08:23 +0100659 reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_cfg,
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200660 vcpu->vcpu_id, offset >> 1);
Marc Zyngier6545eae2013-08-29 11:08:23 +0100661
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200662 if (offset & 4)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500663 val = *reg >> 16;
664 else
665 val = *reg & 0xffff;
666
667 val = vgic_cfg_expand(val);
668 vgic_reg_access(mmio, &val, offset,
669 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
670 if (mmio->is_write) {
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200671 if (offset < 8) {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500672 *reg = ~0U; /* Force PPIs/SGIs to 1 */
673 return false;
674 }
675
676 val = vgic_cfg_compress(val);
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200677 if (offset & 4) {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500678 *reg &= 0xffff;
679 *reg |= val << 16;
680 } else {
681 *reg &= 0xffff << 16;
682 *reg |= val;
683 }
684 }
685
686 return false;
687}
688
689static bool handle_mmio_sgi_reg(struct kvm_vcpu *vcpu,
690 struct kvm_exit_mmio *mmio, phys_addr_t offset)
691{
692 u32 reg;
693 vgic_reg_access(mmio, &reg, offset,
694 ACCESS_READ_RAZ | ACCESS_WRITE_VALUE);
695 if (mmio->is_write) {
696 vgic_dispatch_sgi(vcpu, reg);
697 vgic_update_state(vcpu->kvm);
698 return true;
699 }
700
701 return false;
702}
703
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800704/**
705 * vgic_unqueue_irqs - move pending IRQs from LRs to the distributor
706 * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs
707 *
708 * Move any pending IRQs that have already been assigned to LRs back to the
709 * emulated distributor state so that the complete emulated state can be read
710 * from the main emulation structures without investigating the LRs.
711 *
712 * Note that IRQs in the active state in the LRs get their pending state moved
713 * to the distributor but the active state stays in the LRs, because we don't
714 * track the active state on the distributor side.
715 */
716static void vgic_unqueue_irqs(struct kvm_vcpu *vcpu)
717{
718 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
719 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
720 int vcpu_id = vcpu->vcpu_id;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100721 int i;
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800722
723 for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100724 struct vgic_lr lr = vgic_get_lr(vcpu, i);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800725
726 /*
727 * There are three options for the state bits:
728 *
729 * 01: pending
730 * 10: active
731 * 11: pending and active
732 *
733 * If the LR holds only an active interrupt (not pending) then
734 * just leave it alone.
735 */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100736 if ((lr.state & LR_STATE_MASK) == LR_STATE_ACTIVE)
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800737 continue;
738
739 /*
740 * Reestablish the pending state on the distributor and the
741 * CPU interface. It may have already been pending, but that
742 * is fine, then we are only setting a few bits that were
743 * already set.
744 */
Christoffer Dall227844f2014-06-09 12:27:18 +0200745 vgic_dist_irq_set_pending(vcpu, lr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100746 if (lr.irq < VGIC_NR_SGIS)
747 dist->irq_sgi_sources[vcpu_id][lr.irq] |= 1 << lr.source;
748 lr.state &= ~LR_STATE_PENDING;
749 vgic_set_lr(vcpu, i, lr);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800750
751 /*
752 * If there's no state left on the LR (it could still be
753 * active), then the LR does not hold any useful info and can
754 * be marked as free for other use.
755 */
Christoffer Dallcced50c2014-06-14 22:37:33 +0200756 if (!(lr.state & LR_STATE_MASK)) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100757 vgic_retire_lr(i, lr.irq, vcpu);
Christoffer Dallcced50c2014-06-14 22:37:33 +0200758 vgic_irq_clear_queued(vcpu, lr.irq);
759 }
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800760
761 /* Finally update the VGIC state. */
762 vgic_update_state(vcpu->kvm);
763 }
764}
765
Christoffer Dall90a53552013-10-25 21:22:31 +0100766/* Handle reads of GICD_CPENDSGIRn and GICD_SPENDSGIRn */
767static bool read_set_clear_sgi_pend_reg(struct kvm_vcpu *vcpu,
768 struct kvm_exit_mmio *mmio,
769 phys_addr_t offset)
Christoffer Dallc07a0192013-10-25 21:17:31 +0100770{
Christoffer Dall90a53552013-10-25 21:22:31 +0100771 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
772 int sgi;
773 int min_sgi = (offset & ~0x3) * 4;
774 int max_sgi = min_sgi + 3;
775 int vcpu_id = vcpu->vcpu_id;
776 u32 reg = 0;
777
778 /* Copy source SGIs from distributor side */
779 for (sgi = min_sgi; sgi <= max_sgi; sgi++) {
780 int shift = 8 * (sgi - min_sgi);
781 reg |= (u32)dist->irq_sgi_sources[vcpu_id][sgi] << shift;
782 }
783
784 mmio_data_write(mmio, ~0, reg);
Christoffer Dallc07a0192013-10-25 21:17:31 +0100785 return false;
786}
787
Christoffer Dall90a53552013-10-25 21:22:31 +0100788static bool write_set_clear_sgi_pend_reg(struct kvm_vcpu *vcpu,
789 struct kvm_exit_mmio *mmio,
790 phys_addr_t offset, bool set)
791{
792 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
793 int sgi;
794 int min_sgi = (offset & ~0x3) * 4;
795 int max_sgi = min_sgi + 3;
796 int vcpu_id = vcpu->vcpu_id;
797 u32 reg;
798 bool updated = false;
799
800 reg = mmio_data_read(mmio, ~0);
801
802 /* Clear pending SGIs on the distributor */
803 for (sgi = min_sgi; sgi <= max_sgi; sgi++) {
804 u8 mask = reg >> (8 * (sgi - min_sgi));
805 if (set) {
806 if ((dist->irq_sgi_sources[vcpu_id][sgi] & mask) != mask)
807 updated = true;
808 dist->irq_sgi_sources[vcpu_id][sgi] |= mask;
809 } else {
810 if (dist->irq_sgi_sources[vcpu_id][sgi] & mask)
811 updated = true;
812 dist->irq_sgi_sources[vcpu_id][sgi] &= ~mask;
813 }
814 }
815
816 if (updated)
817 vgic_update_state(vcpu->kvm);
818
819 return updated;
820}
821
Christoffer Dallc07a0192013-10-25 21:17:31 +0100822static bool handle_mmio_sgi_set(struct kvm_vcpu *vcpu,
823 struct kvm_exit_mmio *mmio,
824 phys_addr_t offset)
825{
Christoffer Dall90a53552013-10-25 21:22:31 +0100826 if (!mmio->is_write)
827 return read_set_clear_sgi_pend_reg(vcpu, mmio, offset);
828 else
829 return write_set_clear_sgi_pend_reg(vcpu, mmio, offset, true);
830}
831
832static bool handle_mmio_sgi_clear(struct kvm_vcpu *vcpu,
833 struct kvm_exit_mmio *mmio,
834 phys_addr_t offset)
835{
836 if (!mmio->is_write)
837 return read_set_clear_sgi_pend_reg(vcpu, mmio, offset);
838 else
839 return write_set_clear_sgi_pend_reg(vcpu, mmio, offset, false);
Christoffer Dallc07a0192013-10-25 21:17:31 +0100840}
841
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500842/*
843 * I would have liked to use the kvm_bus_io_*() API instead, but it
844 * cannot cope with banked registers (only the VM pointer is passed
845 * around, and we need the vcpu). One of these days, someone please
846 * fix it!
847 */
848struct mmio_range {
849 phys_addr_t base;
850 unsigned long len;
851 bool (*handle_mmio)(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
852 phys_addr_t offset);
853};
854
Christoffer Dall1006e8c2013-09-23 14:55:56 -0700855static const struct mmio_range vgic_dist_ranges[] = {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500856 {
857 .base = GIC_DIST_CTRL,
858 .len = 12,
859 .handle_mmio = handle_mmio_misc,
860 },
861 {
862 .base = GIC_DIST_IGROUP,
863 .len = VGIC_NR_IRQS / 8,
864 .handle_mmio = handle_mmio_raz_wi,
865 },
866 {
867 .base = GIC_DIST_ENABLE_SET,
868 .len = VGIC_NR_IRQS / 8,
869 .handle_mmio = handle_mmio_set_enable_reg,
870 },
871 {
872 .base = GIC_DIST_ENABLE_CLEAR,
873 .len = VGIC_NR_IRQS / 8,
874 .handle_mmio = handle_mmio_clear_enable_reg,
875 },
876 {
877 .base = GIC_DIST_PENDING_SET,
878 .len = VGIC_NR_IRQS / 8,
879 .handle_mmio = handle_mmio_set_pending_reg,
880 },
881 {
882 .base = GIC_DIST_PENDING_CLEAR,
883 .len = VGIC_NR_IRQS / 8,
884 .handle_mmio = handle_mmio_clear_pending_reg,
885 },
886 {
887 .base = GIC_DIST_ACTIVE_SET,
888 .len = VGIC_NR_IRQS / 8,
889 .handle_mmio = handle_mmio_raz_wi,
890 },
891 {
892 .base = GIC_DIST_ACTIVE_CLEAR,
893 .len = VGIC_NR_IRQS / 8,
894 .handle_mmio = handle_mmio_raz_wi,
895 },
896 {
897 .base = GIC_DIST_PRI,
898 .len = VGIC_NR_IRQS,
899 .handle_mmio = handle_mmio_priority_reg,
900 },
901 {
902 .base = GIC_DIST_TARGET,
903 .len = VGIC_NR_IRQS,
904 .handle_mmio = handle_mmio_target_reg,
905 },
906 {
907 .base = GIC_DIST_CONFIG,
908 .len = VGIC_NR_IRQS / 4,
909 .handle_mmio = handle_mmio_cfg_reg,
910 },
911 {
912 .base = GIC_DIST_SOFTINT,
913 .len = 4,
914 .handle_mmio = handle_mmio_sgi_reg,
915 },
Christoffer Dallc07a0192013-10-25 21:17:31 +0100916 {
917 .base = GIC_DIST_SGI_PENDING_CLEAR,
918 .len = VGIC_NR_SGIS,
919 .handle_mmio = handle_mmio_sgi_clear,
920 },
921 {
922 .base = GIC_DIST_SGI_PENDING_SET,
923 .len = VGIC_NR_SGIS,
924 .handle_mmio = handle_mmio_sgi_set,
925 },
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500926 {}
927};
928
929static const
930struct mmio_range *find_matching_range(const struct mmio_range *ranges,
931 struct kvm_exit_mmio *mmio,
Christoffer Dall1006e8c2013-09-23 14:55:56 -0700932 phys_addr_t offset)
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500933{
934 const struct mmio_range *r = ranges;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500935
936 while (r->len) {
Christoffer Dall1006e8c2013-09-23 14:55:56 -0700937 if (offset >= r->base &&
938 (offset + mmio->len) <= (r->base + r->len))
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500939 return r;
940 r++;
941 }
942
943 return NULL;
944}
945
946/**
947 * vgic_handle_mmio - handle an in-kernel MMIO access
948 * @vcpu: pointer to the vcpu performing the access
949 * @run: pointer to the kvm_run structure
950 * @mmio: pointer to the data describing the access
951 *
952 * returns true if the MMIO access has been performed in kernel space,
953 * and false if it needs to be emulated in user space.
954 */
955bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
956 struct kvm_exit_mmio *mmio)
957{
Marc Zyngierb47ef922013-01-21 19:36:14 -0500958 const struct mmio_range *range;
959 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
960 unsigned long base = dist->vgic_dist_base;
961 bool updated_state;
962 unsigned long offset;
963
964 if (!irqchip_in_kernel(vcpu->kvm) ||
965 mmio->phys_addr < base ||
966 (mmio->phys_addr + mmio->len) > (base + KVM_VGIC_V2_DIST_SIZE))
967 return false;
968
969 /* We don't support ldrd / strd or ldm / stm to the emulated vgic */
970 if (mmio->len > 4) {
971 kvm_inject_dabt(vcpu, mmio->phys_addr);
972 return true;
973 }
974
Christoffer Dall1006e8c2013-09-23 14:55:56 -0700975 offset = mmio->phys_addr - base;
976 range = find_matching_range(vgic_dist_ranges, mmio, offset);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500977 if (unlikely(!range || !range->handle_mmio)) {
978 pr_warn("Unhandled access %d %08llx %d\n",
979 mmio->is_write, mmio->phys_addr, mmio->len);
980 return false;
981 }
982
983 spin_lock(&vcpu->kvm->arch.vgic.lock);
984 offset = mmio->phys_addr - range->base - base;
985 updated_state = range->handle_mmio(vcpu, mmio, offset);
986 spin_unlock(&vcpu->kvm->arch.vgic.lock);
987 kvm_prepare_mmio(run, mmio);
988 kvm_handle_mmio_return(vcpu, run);
989
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500990 if (updated_state)
991 vgic_kick_vcpus(vcpu->kvm);
992
Marc Zyngierb47ef922013-01-21 19:36:14 -0500993 return true;
994}
995
996static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg)
997{
998 struct kvm *kvm = vcpu->kvm;
999 struct vgic_dist *dist = &kvm->arch.vgic;
1000 int nrcpus = atomic_read(&kvm->online_vcpus);
1001 u8 target_cpus;
1002 int sgi, mode, c, vcpu_id;
1003
1004 vcpu_id = vcpu->vcpu_id;
1005
1006 sgi = reg & 0xf;
1007 target_cpus = (reg >> 16) & 0xff;
1008 mode = (reg >> 24) & 3;
1009
1010 switch (mode) {
1011 case 0:
1012 if (!target_cpus)
1013 return;
Haibin Wang91021a62014-04-10 13:14:32 +01001014 break;
Marc Zyngierb47ef922013-01-21 19:36:14 -05001015
1016 case 1:
1017 target_cpus = ((1 << nrcpus) - 1) & ~(1 << vcpu_id) & 0xff;
1018 break;
1019
1020 case 2:
1021 target_cpus = 1 << vcpu_id;
1022 break;
1023 }
1024
1025 kvm_for_each_vcpu(c, vcpu, kvm) {
1026 if (target_cpus & 1) {
1027 /* Flag the SGI as pending */
Christoffer Dall227844f2014-06-09 12:27:18 +02001028 vgic_dist_irq_set_pending(vcpu, sgi);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001029 dist->irq_sgi_sources[c][sgi] |= 1 << vcpu_id;
1030 kvm_debug("SGI%d from CPU%d to CPU%d\n", sgi, vcpu_id, c);
1031 }
1032
1033 target_cpus >>= 1;
1034 }
1035}
1036
1037static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
1038{
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001039 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1040 unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
1041 unsigned long pending_private, pending_shared;
1042 int vcpu_id;
1043
1044 vcpu_id = vcpu->vcpu_id;
1045 pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
1046 pend_shared = vcpu->arch.vgic_cpu.pending_shared;
1047
Christoffer Dall227844f2014-06-09 12:27:18 +02001048 pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001049 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
1050 bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
1051
Christoffer Dall227844f2014-06-09 12:27:18 +02001052 pending = vgic_bitmap_get_shared_map(&dist->irq_pending);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001053 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
1054 bitmap_and(pend_shared, pending, enabled, VGIC_NR_SHARED_IRQS);
1055 bitmap_and(pend_shared, pend_shared,
1056 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
1057 VGIC_NR_SHARED_IRQS);
1058
1059 pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
1060 pending_shared = find_first_bit(pend_shared, VGIC_NR_SHARED_IRQS);
1061 return (pending_private < VGIC_NR_PRIVATE_IRQS ||
1062 pending_shared < VGIC_NR_SHARED_IRQS);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001063}
1064
1065/*
1066 * Update the interrupt state and determine which CPUs have pending
1067 * interrupts. Must be called with distributor lock held.
1068 */
1069static void vgic_update_state(struct kvm *kvm)
1070{
1071 struct vgic_dist *dist = &kvm->arch.vgic;
1072 struct kvm_vcpu *vcpu;
1073 int c;
1074
1075 if (!dist->enabled) {
1076 set_bit(0, &dist->irq_pending_on_cpu);
1077 return;
1078 }
1079
1080 kvm_for_each_vcpu(c, vcpu, kvm) {
1081 if (compute_pending_for_cpu(vcpu)) {
1082 pr_debug("CPU%d has pending interrupts\n", c);
1083 set_bit(c, &dist->irq_pending_on_cpu);
1084 }
1085 }
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001086}
Christoffer Dall330690c2013-01-21 19:36:13 -05001087
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001088static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr)
1089{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001090 return vgic_ops->get_lr(vcpu, lr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001091}
1092
1093static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr,
1094 struct vgic_lr vlr)
1095{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001096 vgic_ops->set_lr(vcpu, lr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001097}
1098
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001099static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
1100 struct vgic_lr vlr)
1101{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001102 vgic_ops->sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001103}
1104
1105static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu)
1106{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001107 return vgic_ops->get_elrsr(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001108}
1109
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001110static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu)
1111{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001112 return vgic_ops->get_eisr(vcpu);
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001113}
1114
Marc Zyngier495dd852013-06-04 11:02:10 +01001115static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu)
1116{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001117 return vgic_ops->get_interrupt_status(vcpu);
Marc Zyngier495dd852013-06-04 11:02:10 +01001118}
1119
Marc Zyngier909d9b52013-06-04 11:24:17 +01001120static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu)
1121{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001122 vgic_ops->enable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001123}
1124
1125static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu)
1126{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001127 vgic_ops->disable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001128}
1129
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001130static inline void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1131{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001132 vgic_ops->get_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001133}
1134
1135static void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1136{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001137 vgic_ops->set_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001138}
1139
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001140static inline void vgic_enable(struct kvm_vcpu *vcpu)
1141{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001142 vgic_ops->enable(vcpu);
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001143}
1144
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001145static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu)
1146{
1147 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1148 struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr);
1149
1150 vlr.state = 0;
1151 vgic_set_lr(vcpu, lr_nr, vlr);
1152 clear_bit(lr_nr, vgic_cpu->lr_used);
1153 vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
1154}
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001155
1156/*
1157 * An interrupt may have been disabled after being made pending on the
1158 * CPU interface (the classic case is a timer running while we're
1159 * rebooting the guest - the interrupt would kick as soon as the CPU
1160 * interface gets enabled, with deadly consequences).
1161 *
1162 * The solution is to examine already active LRs, and check the
1163 * interrupt is still enabled. If not, just retire it.
1164 */
1165static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
1166{
1167 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1168 int lr;
1169
Marc Zyngier8f186d52014-02-04 18:13:03 +00001170 for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001171 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001172
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001173 if (!vgic_irq_is_enabled(vcpu, vlr.irq)) {
1174 vgic_retire_lr(lr, vlr.irq, vcpu);
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001175 if (vgic_irq_is_queued(vcpu, vlr.irq))
1176 vgic_irq_clear_queued(vcpu, vlr.irq);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001177 }
1178 }
1179}
1180
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001181/*
1182 * Queue an interrupt to a CPU virtual interface. Return true on success,
1183 * or false if it wasn't possible to queue it.
1184 */
1185static bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
1186{
1187 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001188 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001189 int lr;
1190
1191 /* Sanitize the input... */
1192 BUG_ON(sgi_source_id & ~7);
1193 BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
1194 BUG_ON(irq >= VGIC_NR_IRQS);
1195
1196 kvm_debug("Queue IRQ%d\n", irq);
1197
1198 lr = vgic_cpu->vgic_irq_lr_map[irq];
1199
1200 /* Do we have an active interrupt for the same CPUID? */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001201 if (lr != LR_EMPTY) {
1202 vlr = vgic_get_lr(vcpu, lr);
1203 if (vlr.source == sgi_source_id) {
1204 kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq);
1205 BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
1206 vlr.state |= LR_STATE_PENDING;
1207 vgic_set_lr(vcpu, lr, vlr);
1208 return true;
1209 }
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001210 }
1211
1212 /* Try to use another LR for this interrupt */
1213 lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
Marc Zyngier8f186d52014-02-04 18:13:03 +00001214 vgic->nr_lr);
1215 if (lr >= vgic->nr_lr)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001216 return false;
1217
1218 kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001219 vgic_cpu->vgic_irq_lr_map[irq] = lr;
1220 set_bit(lr, vgic_cpu->lr_used);
1221
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001222 vlr.irq = irq;
1223 vlr.source = sgi_source_id;
1224 vlr.state = LR_STATE_PENDING;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001225 if (!vgic_irq_is_edge(vcpu, irq))
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001226 vlr.state |= LR_EOI_INT;
1227
1228 vgic_set_lr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001229
1230 return true;
1231}
1232
1233static bool vgic_queue_sgi(struct kvm_vcpu *vcpu, int irq)
1234{
1235 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1236 unsigned long sources;
1237 int vcpu_id = vcpu->vcpu_id;
1238 int c;
1239
1240 sources = dist->irq_sgi_sources[vcpu_id][irq];
1241
1242 for_each_set_bit(c, &sources, VGIC_MAX_CPUS) {
1243 if (vgic_queue_irq(vcpu, c, irq))
1244 clear_bit(c, &sources);
1245 }
1246
1247 dist->irq_sgi_sources[vcpu_id][irq] = sources;
1248
1249 /*
1250 * If the sources bitmap has been cleared it means that we
1251 * could queue all the SGIs onto link registers (see the
1252 * clear_bit above), and therefore we are done with them in
1253 * our emulated gic and can get rid of them.
1254 */
1255 if (!sources) {
Christoffer Dall227844f2014-06-09 12:27:18 +02001256 vgic_dist_irq_clear_pending(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001257 vgic_cpu_irq_clear(vcpu, irq);
1258 return true;
1259 }
1260
1261 return false;
1262}
1263
1264static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
1265{
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001266 if (!vgic_can_sample_irq(vcpu, irq))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001267 return true; /* level interrupt, already queued */
1268
1269 if (vgic_queue_irq(vcpu, 0, irq)) {
1270 if (vgic_irq_is_edge(vcpu, irq)) {
Christoffer Dall227844f2014-06-09 12:27:18 +02001271 vgic_dist_irq_clear_pending(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001272 vgic_cpu_irq_clear(vcpu, irq);
1273 } else {
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001274 vgic_irq_set_queued(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001275 }
1276
1277 return true;
1278 }
1279
1280 return false;
1281}
1282
1283/*
1284 * Fill the list registers with pending interrupts before running the
1285 * guest.
1286 */
1287static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1288{
1289 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1290 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1291 int i, vcpu_id;
1292 int overflow = 0;
1293
1294 vcpu_id = vcpu->vcpu_id;
1295
1296 /*
1297 * We may not have any pending interrupt, or the interrupts
1298 * may have been serviced from another vcpu. In all cases,
1299 * move along.
1300 */
1301 if (!kvm_vgic_vcpu_pending_irq(vcpu)) {
1302 pr_debug("CPU%d has no pending interrupt\n", vcpu_id);
1303 goto epilog;
1304 }
1305
1306 /* SGIs */
1307 for_each_set_bit(i, vgic_cpu->pending_percpu, VGIC_NR_SGIS) {
1308 if (!vgic_queue_sgi(vcpu, i))
1309 overflow = 1;
1310 }
1311
1312 /* PPIs */
1313 for_each_set_bit_from(i, vgic_cpu->pending_percpu, VGIC_NR_PRIVATE_IRQS) {
1314 if (!vgic_queue_hwirq(vcpu, i))
1315 overflow = 1;
1316 }
1317
1318 /* SPIs */
1319 for_each_set_bit(i, vgic_cpu->pending_shared, VGIC_NR_SHARED_IRQS) {
1320 if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
1321 overflow = 1;
1322 }
1323
1324epilog:
1325 if (overflow) {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001326 vgic_enable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001327 } else {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001328 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001329 /*
1330 * We're about to run this VCPU, and we've consumed
1331 * everything the distributor had in store for
1332 * us. Claim we don't have anything pending. We'll
1333 * adjust that if needed while exiting.
1334 */
1335 clear_bit(vcpu_id, &dist->irq_pending_on_cpu);
1336 }
1337}
1338
1339static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1340{
Marc Zyngier495dd852013-06-04 11:02:10 +01001341 u32 status = vgic_get_interrupt_status(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001342 bool level_pending = false;
1343
Marc Zyngier495dd852013-06-04 11:02:10 +01001344 kvm_debug("STATUS = %08x\n", status);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001345
Marc Zyngier495dd852013-06-04 11:02:10 +01001346 if (status & INT_STATUS_EOI) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001347 /*
1348 * Some level interrupts have been EOIed. Clear their
1349 * active bit.
1350 */
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001351 u64 eisr = vgic_get_eisr(vcpu);
1352 unsigned long *eisr_ptr = (unsigned long *)&eisr;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001353 int lr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001354
Marc Zyngier8f186d52014-02-04 18:13:03 +00001355 for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001356 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001357 WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001358
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001359 vgic_irq_clear_queued(vcpu, vlr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001360 WARN_ON(vlr.state & LR_STATE_MASK);
1361 vlr.state = 0;
1362 vgic_set_lr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001363
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001364 /*
1365 * If the IRQ was EOIed it was also ACKed and we we
1366 * therefore assume we can clear the soft pending
1367 * state (should it had been set) for this interrupt.
1368 *
1369 * Note: if the IRQ soft pending state was set after
1370 * the IRQ was acked, it actually shouldn't be
1371 * cleared, but we have no way of knowing that unless
1372 * we start trapping ACKs when the soft-pending state
1373 * is set.
1374 */
1375 vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
1376
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001377 /* Any additional pending interrupt? */
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001378 if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001379 vgic_cpu_irq_set(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001380 level_pending = true;
1381 } else {
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001382 vgic_dist_irq_clear_pending(vcpu, vlr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001383 vgic_cpu_irq_clear(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001384 }
Marc Zyngier75da01e2013-01-31 11:25:52 +00001385
1386 /*
1387 * Despite being EOIed, the LR may not have
1388 * been marked as empty.
1389 */
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001390 vgic_sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001391 }
1392 }
1393
Marc Zyngier495dd852013-06-04 11:02:10 +01001394 if (status & INT_STATUS_UNDERFLOW)
Marc Zyngier909d9b52013-06-04 11:24:17 +01001395 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001396
1397 return level_pending;
1398}
1399
1400/*
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001401 * Sync back the VGIC state after a guest run. The distributor lock is
1402 * needed so we don't get preempted in the middle of the state processing.
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001403 */
1404static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1405{
1406 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1407 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001408 u64 elrsr;
1409 unsigned long *elrsr_ptr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001410 int lr, pending;
1411 bool level_pending;
1412
1413 level_pending = vgic_process_maintenance(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001414 elrsr = vgic_get_elrsr(vcpu);
1415 elrsr_ptr = (unsigned long *)&elrsr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001416
1417 /* Clear mappings for empty LRs */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001418 for_each_set_bit(lr, elrsr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001419 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001420
1421 if (!test_and_clear_bit(lr, vgic_cpu->lr_used))
1422 continue;
1423
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001424 vlr = vgic_get_lr(vcpu, lr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001425
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001426 BUG_ON(vlr.irq >= VGIC_NR_IRQS);
1427 vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001428 }
1429
1430 /* Check if we still have something up our sleeve... */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001431 pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr);
1432 if (level_pending || pending < vgic->nr_lr)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001433 set_bit(vcpu->vcpu_id, &dist->irq_pending_on_cpu);
1434}
1435
1436void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1437{
1438 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1439
1440 if (!irqchip_in_kernel(vcpu->kvm))
1441 return;
1442
1443 spin_lock(&dist->lock);
1444 __kvm_vgic_flush_hwstate(vcpu);
1445 spin_unlock(&dist->lock);
1446}
1447
1448void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1449{
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001450 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1451
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001452 if (!irqchip_in_kernel(vcpu->kvm))
1453 return;
1454
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001455 spin_lock(&dist->lock);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001456 __kvm_vgic_sync_hwstate(vcpu);
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001457 spin_unlock(&dist->lock);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001458}
1459
1460int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
1461{
1462 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1463
1464 if (!irqchip_in_kernel(vcpu->kvm))
1465 return 0;
1466
1467 return test_bit(vcpu->vcpu_id, &dist->irq_pending_on_cpu);
1468}
1469
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001470static void vgic_kick_vcpus(struct kvm *kvm)
1471{
1472 struct kvm_vcpu *vcpu;
1473 int c;
1474
1475 /*
1476 * We've injected an interrupt, time to find out who deserves
1477 * a good kick...
1478 */
1479 kvm_for_each_vcpu(c, vcpu, kvm) {
1480 if (kvm_vgic_vcpu_pending_irq(vcpu))
1481 kvm_vcpu_kick(vcpu);
1482 }
1483}
1484
1485static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
1486{
Christoffer Dall227844f2014-06-09 12:27:18 +02001487 int edge_triggered = vgic_irq_is_edge(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001488
1489 /*
1490 * Only inject an interrupt if:
1491 * - edge triggered and we have a rising edge
1492 * - level triggered and we change level
1493 */
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001494 if (edge_triggered) {
1495 int state = vgic_dist_irq_is_pending(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001496 return level > state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001497 } else {
1498 int state = vgic_dist_irq_get_level(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001499 return level != state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001500 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001501}
1502
Christoffer Dall227844f2014-06-09 12:27:18 +02001503static bool vgic_update_irq_pending(struct kvm *kvm, int cpuid,
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001504 unsigned int irq_num, bool level)
1505{
1506 struct vgic_dist *dist = &kvm->arch.vgic;
1507 struct kvm_vcpu *vcpu;
Christoffer Dall227844f2014-06-09 12:27:18 +02001508 int edge_triggered, level_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001509 int enabled;
1510 bool ret = true;
1511
1512 spin_lock(&dist->lock);
1513
1514 vcpu = kvm_get_vcpu(kvm, cpuid);
Christoffer Dall227844f2014-06-09 12:27:18 +02001515 edge_triggered = vgic_irq_is_edge(vcpu, irq_num);
1516 level_triggered = !edge_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001517
1518 if (!vgic_validate_injection(vcpu, irq_num, level)) {
1519 ret = false;
1520 goto out;
1521 }
1522
1523 if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
1524 cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
1525 vcpu = kvm_get_vcpu(kvm, cpuid);
1526 }
1527
1528 kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
1529
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001530 if (level) {
1531 if (level_triggered)
1532 vgic_dist_irq_set_level(vcpu, irq_num);
Christoffer Dall227844f2014-06-09 12:27:18 +02001533 vgic_dist_irq_set_pending(vcpu, irq_num);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001534 } else {
1535 if (level_triggered) {
1536 vgic_dist_irq_clear_level(vcpu, irq_num);
1537 if (!vgic_dist_irq_soft_pend(vcpu, irq_num))
1538 vgic_dist_irq_clear_pending(vcpu, irq_num);
1539 } else {
1540 vgic_dist_irq_clear_pending(vcpu, irq_num);
1541 }
1542 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001543
1544 enabled = vgic_irq_is_enabled(vcpu, irq_num);
1545
1546 if (!enabled) {
1547 ret = false;
1548 goto out;
1549 }
1550
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001551 if (!vgic_can_sample_irq(vcpu, irq_num)) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001552 /*
1553 * Level interrupt in progress, will be picked up
1554 * when EOId.
1555 */
1556 ret = false;
1557 goto out;
1558 }
1559
1560 if (level) {
1561 vgic_cpu_irq_set(vcpu, irq_num);
1562 set_bit(cpuid, &dist->irq_pending_on_cpu);
1563 }
1564
1565out:
1566 spin_unlock(&dist->lock);
1567
1568 return ret;
1569}
1570
1571/**
1572 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
1573 * @kvm: The VM structure pointer
1574 * @cpuid: The CPU for PPIs
1575 * @irq_num: The IRQ number that is assigned to the device
1576 * @level: Edge-triggered: true: to trigger the interrupt
1577 * false: to ignore the call
1578 * Level-sensitive true: activates an interrupt
1579 * false: deactivates an interrupt
1580 *
1581 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1582 * level-sensitive interrupts. You can think of the level parameter as 1
1583 * being HIGH and 0 being LOW and all devices being active-HIGH.
1584 */
1585int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
1586 bool level)
1587{
Christoffer Dall227844f2014-06-09 12:27:18 +02001588 if (vgic_update_irq_pending(kvm, cpuid, irq_num, level))
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001589 vgic_kick_vcpus(kvm);
1590
1591 return 0;
1592}
1593
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001594static irqreturn_t vgic_maintenance_handler(int irq, void *data)
1595{
1596 /*
1597 * We cannot rely on the vgic maintenance interrupt to be
1598 * delivered synchronously. This means we can only use it to
1599 * exit the VM, and we perform the handling of EOIed
1600 * interrupts on the exit path (see vgic_process_maintenance).
1601 */
1602 return IRQ_HANDLED;
1603}
1604
Christoffer Dalle1ba0202013-09-23 14:55:55 -07001605/**
1606 * kvm_vgic_vcpu_init - Initialize per-vcpu VGIC state
1607 * @vcpu: pointer to the vcpu struct
1608 *
1609 * Initialize the vgic_cpu struct and vgic_dist struct fields pertaining to
1610 * this vcpu and enable the VGIC for this VCPU
1611 */
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001612int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
1613{
1614 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1615 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1616 int i;
1617
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001618 if (vcpu->vcpu_id >= VGIC_MAX_CPUS)
1619 return -EBUSY;
1620
1621 for (i = 0; i < VGIC_NR_IRQS; i++) {
1622 if (i < VGIC_NR_PPIS)
1623 vgic_bitmap_set_irq_val(&dist->irq_enabled,
1624 vcpu->vcpu_id, i, 1);
1625 if (i < VGIC_NR_PRIVATE_IRQS)
1626 vgic_bitmap_set_irq_val(&dist->irq_cfg,
1627 vcpu->vcpu_id, i, VGIC_CFG_EDGE);
1628
1629 vgic_cpu->vgic_irq_lr_map[i] = LR_EMPTY;
1630 }
1631
1632 /*
Marc Zyngierca85f622013-06-18 19:17:28 +01001633 * Store the number of LRs per vcpu, so we don't have to go
1634 * all the way to the distributor structure to find out. Only
1635 * assembly code should use this one.
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001636 */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001637 vgic_cpu->nr_lr = vgic->nr_lr;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001638
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001639 vgic_enable(vcpu);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001640
1641 return 0;
1642}
1643
Christoffer Dalle1ba0202013-09-23 14:55:55 -07001644/**
1645 * kvm_vgic_init - Initialize global VGIC state before running any VCPUs
1646 * @kvm: pointer to the kvm struct
1647 *
1648 * Map the virtual CPU interface into the VM before running any VCPUs. We
1649 * can't do this at creation time, because user space must first set the
1650 * virtual CPU interface address in the guest physical address space. Also
1651 * initialize the ITARGETSRn regs to 0 on the emulated distributor.
1652 */
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001653int kvm_vgic_init(struct kvm *kvm)
1654{
1655 int ret = 0, i;
1656
Christoffer Dalle1ba0202013-09-23 14:55:55 -07001657 if (!irqchip_in_kernel(kvm))
1658 return 0;
1659
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001660 mutex_lock(&kvm->lock);
1661
1662 if (vgic_initialized(kvm))
1663 goto out;
1664
1665 if (IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_dist_base) ||
1666 IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_cpu_base)) {
1667 kvm_err("Need to set vgic cpu and dist addresses first\n");
1668 ret = -ENXIO;
1669 goto out;
1670 }
1671
1672 ret = kvm_phys_addr_ioremap(kvm, kvm->arch.vgic.vgic_cpu_base,
Marc Zyngier8f186d52014-02-04 18:13:03 +00001673 vgic->vcpu_base, KVM_VGIC_V2_CPU_SIZE);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001674 if (ret) {
1675 kvm_err("Unable to remap VGIC CPU to VCPU\n");
1676 goto out;
1677 }
1678
1679 for (i = VGIC_NR_PRIVATE_IRQS; i < VGIC_NR_IRQS; i += 4)
1680 vgic_set_target_reg(kvm, 0, i);
1681
1682 kvm->arch.vgic.ready = true;
1683out:
1684 mutex_unlock(&kvm->lock);
1685 return ret;
1686}
1687
1688int kvm_vgic_create(struct kvm *kvm)
1689{
Christoffer Dall73306722013-10-25 17:29:18 +01001690 int i, vcpu_lock_idx = -1, ret = 0;
1691 struct kvm_vcpu *vcpu;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001692
1693 mutex_lock(&kvm->lock);
1694
Christoffer Dall73306722013-10-25 17:29:18 +01001695 if (kvm->arch.vgic.vctrl_base) {
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001696 ret = -EEXIST;
1697 goto out;
1698 }
1699
Christoffer Dall73306722013-10-25 17:29:18 +01001700 /*
1701 * Any time a vcpu is run, vcpu_load is called which tries to grab the
1702 * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
1703 * that no other VCPUs are run while we create the vgic.
1704 */
1705 kvm_for_each_vcpu(i, vcpu, kvm) {
1706 if (!mutex_trylock(&vcpu->mutex))
1707 goto out_unlock;
1708 vcpu_lock_idx = i;
1709 }
1710
1711 kvm_for_each_vcpu(i, vcpu, kvm) {
1712 if (vcpu->arch.has_run_once) {
1713 ret = -EBUSY;
1714 goto out_unlock;
1715 }
1716 }
1717
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001718 spin_lock_init(&kvm->arch.vgic.lock);
Marc Zyngierf982cf42014-05-15 10:03:25 +01001719 kvm->arch.vgic.in_kernel = true;
Marc Zyngier8f186d52014-02-04 18:13:03 +00001720 kvm->arch.vgic.vctrl_base = vgic->vctrl_base;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001721 kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
1722 kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
1723
Christoffer Dall73306722013-10-25 17:29:18 +01001724out_unlock:
1725 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
1726 vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
1727 mutex_unlock(&vcpu->mutex);
1728 }
1729
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001730out:
1731 mutex_unlock(&kvm->lock);
1732 return ret;
1733}
1734
Will Deacon1fa451b2014-08-26 15:13:24 +01001735static int vgic_ioaddr_overlap(struct kvm *kvm)
Christoffer Dall330690c2013-01-21 19:36:13 -05001736{
1737 phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
1738 phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
1739
1740 if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
1741 return 0;
1742 if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
1743 (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
1744 return -EBUSY;
1745 return 0;
1746}
1747
1748static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
1749 phys_addr_t addr, phys_addr_t size)
1750{
1751 int ret;
1752
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001753 if (addr & ~KVM_PHYS_MASK)
1754 return -E2BIG;
1755
1756 if (addr & (SZ_4K - 1))
1757 return -EINVAL;
1758
Christoffer Dall330690c2013-01-21 19:36:13 -05001759 if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
1760 return -EEXIST;
1761 if (addr + size < addr)
1762 return -EINVAL;
1763
Haibin Wang30c21172014-04-29 14:49:17 +08001764 *ioaddr = addr;
Christoffer Dall330690c2013-01-21 19:36:13 -05001765 ret = vgic_ioaddr_overlap(kvm);
1766 if (ret)
Haibin Wang30c21172014-04-29 14:49:17 +08001767 *ioaddr = VGIC_ADDR_UNDEF;
1768
Christoffer Dall330690c2013-01-21 19:36:13 -05001769 return ret;
1770}
1771
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001772/**
1773 * kvm_vgic_addr - set or get vgic VM base addresses
1774 * @kvm: pointer to the vm struct
1775 * @type: the VGIC addr type, one of KVM_VGIC_V2_ADDR_TYPE_XXX
1776 * @addr: pointer to address value
1777 * @write: if true set the address in the VM address space, if false read the
1778 * address
1779 *
1780 * Set or get the vgic base addresses for the distributor and the virtual CPU
1781 * interface in the VM physical address space. These addresses are properties
1782 * of the emulated core/SoC and therefore user space initially knows this
1783 * information.
1784 */
1785int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
Christoffer Dall330690c2013-01-21 19:36:13 -05001786{
1787 int r = 0;
1788 struct vgic_dist *vgic = &kvm->arch.vgic;
1789
Christoffer Dall330690c2013-01-21 19:36:13 -05001790 mutex_lock(&kvm->lock);
1791 switch (type) {
1792 case KVM_VGIC_V2_ADDR_TYPE_DIST:
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001793 if (write) {
1794 r = vgic_ioaddr_assign(kvm, &vgic->vgic_dist_base,
1795 *addr, KVM_VGIC_V2_DIST_SIZE);
1796 } else {
1797 *addr = vgic->vgic_dist_base;
1798 }
Christoffer Dall330690c2013-01-21 19:36:13 -05001799 break;
1800 case KVM_VGIC_V2_ADDR_TYPE_CPU:
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001801 if (write) {
1802 r = vgic_ioaddr_assign(kvm, &vgic->vgic_cpu_base,
1803 *addr, KVM_VGIC_V2_CPU_SIZE);
1804 } else {
1805 *addr = vgic->vgic_cpu_base;
1806 }
Christoffer Dall330690c2013-01-21 19:36:13 -05001807 break;
1808 default:
1809 r = -ENODEV;
1810 }
1811
1812 mutex_unlock(&kvm->lock);
1813 return r;
1814}
Christoffer Dall73306722013-10-25 17:29:18 +01001815
Christoffer Dallc07a0192013-10-25 21:17:31 +01001816static bool handle_cpu_mmio_misc(struct kvm_vcpu *vcpu,
1817 struct kvm_exit_mmio *mmio, phys_addr_t offset)
1818{
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001819 bool updated = false;
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001820 struct vgic_vmcr vmcr;
1821 u32 *vmcr_field;
1822 u32 reg;
1823
1824 vgic_get_vmcr(vcpu, &vmcr);
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001825
1826 switch (offset & ~0x3) {
1827 case GIC_CPU_CTRL:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001828 vmcr_field = &vmcr.ctlr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001829 break;
1830 case GIC_CPU_PRIMASK:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001831 vmcr_field = &vmcr.pmr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001832 break;
1833 case GIC_CPU_BINPOINT:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001834 vmcr_field = &vmcr.bpr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001835 break;
1836 case GIC_CPU_ALIAS_BINPOINT:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001837 vmcr_field = &vmcr.abpr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001838 break;
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001839 default:
1840 BUG();
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001841 }
1842
1843 if (!mmio->is_write) {
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001844 reg = *vmcr_field;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001845 mmio_data_write(mmio, ~0, reg);
1846 } else {
1847 reg = mmio_data_read(mmio, ~0);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001848 if (reg != *vmcr_field) {
1849 *vmcr_field = reg;
1850 vgic_set_vmcr(vcpu, &vmcr);
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001851 updated = true;
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001852 }
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001853 }
1854 return updated;
Christoffer Dallc07a0192013-10-25 21:17:31 +01001855}
1856
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001857static bool handle_mmio_abpr(struct kvm_vcpu *vcpu,
1858 struct kvm_exit_mmio *mmio, phys_addr_t offset)
1859{
1860 return handle_cpu_mmio_misc(vcpu, mmio, GIC_CPU_ALIAS_BINPOINT);
1861}
1862
1863static bool handle_cpu_mmio_ident(struct kvm_vcpu *vcpu,
1864 struct kvm_exit_mmio *mmio,
1865 phys_addr_t offset)
1866{
1867 u32 reg;
1868
1869 if (mmio->is_write)
1870 return false;
1871
1872 /* GICC_IIDR */
1873 reg = (PRODUCT_ID_KVM << 20) |
1874 (GICC_ARCH_VERSION_V2 << 16) |
1875 (IMPLEMENTER_ARM << 0);
1876 mmio_data_write(mmio, ~0, reg);
1877 return false;
1878}
1879
1880/*
1881 * CPU Interface Register accesses - these are not accessed by the VM, but by
1882 * user space for saving and restoring VGIC state.
1883 */
Christoffer Dallc07a0192013-10-25 21:17:31 +01001884static const struct mmio_range vgic_cpu_ranges[] = {
1885 {
1886 .base = GIC_CPU_CTRL,
1887 .len = 12,
1888 .handle_mmio = handle_cpu_mmio_misc,
1889 },
1890 {
1891 .base = GIC_CPU_ALIAS_BINPOINT,
1892 .len = 4,
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001893 .handle_mmio = handle_mmio_abpr,
Christoffer Dallc07a0192013-10-25 21:17:31 +01001894 },
1895 {
1896 .base = GIC_CPU_ACTIVEPRIO,
1897 .len = 16,
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001898 .handle_mmio = handle_mmio_raz_wi,
Christoffer Dallc07a0192013-10-25 21:17:31 +01001899 },
1900 {
1901 .base = GIC_CPU_IDENT,
1902 .len = 4,
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001903 .handle_mmio = handle_cpu_mmio_ident,
Christoffer Dallc07a0192013-10-25 21:17:31 +01001904 },
1905};
1906
1907static int vgic_attr_regs_access(struct kvm_device *dev,
1908 struct kvm_device_attr *attr,
1909 u32 *reg, bool is_write)
1910{
1911 const struct mmio_range *r = NULL, *ranges;
1912 phys_addr_t offset;
1913 int ret, cpuid, c;
1914 struct kvm_vcpu *vcpu, *tmp_vcpu;
1915 struct vgic_dist *vgic;
1916 struct kvm_exit_mmio mmio;
1917
1918 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
1919 cpuid = (attr->attr & KVM_DEV_ARM_VGIC_CPUID_MASK) >>
1920 KVM_DEV_ARM_VGIC_CPUID_SHIFT;
1921
1922 mutex_lock(&dev->kvm->lock);
1923
1924 if (cpuid >= atomic_read(&dev->kvm->online_vcpus)) {
1925 ret = -EINVAL;
1926 goto out;
1927 }
1928
1929 vcpu = kvm_get_vcpu(dev->kvm, cpuid);
1930 vgic = &dev->kvm->arch.vgic;
1931
1932 mmio.len = 4;
1933 mmio.is_write = is_write;
1934 if (is_write)
1935 mmio_data_write(&mmio, ~0, *reg);
1936 switch (attr->group) {
1937 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
1938 mmio.phys_addr = vgic->vgic_dist_base + offset;
1939 ranges = vgic_dist_ranges;
1940 break;
1941 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
1942 mmio.phys_addr = vgic->vgic_cpu_base + offset;
1943 ranges = vgic_cpu_ranges;
1944 break;
1945 default:
1946 BUG();
1947 }
1948 r = find_matching_range(ranges, &mmio, offset);
1949
1950 if (unlikely(!r || !r->handle_mmio)) {
1951 ret = -ENXIO;
1952 goto out;
1953 }
1954
1955
1956 spin_lock(&vgic->lock);
1957
1958 /*
1959 * Ensure that no other VCPU is running by checking the vcpu->cpu
1960 * field. If no other VPCUs are running we can safely access the VGIC
1961 * state, because even if another VPU is run after this point, that
1962 * VCPU will not touch the vgic state, because it will block on
1963 * getting the vgic->lock in kvm_vgic_sync_hwstate().
1964 */
1965 kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm) {
1966 if (unlikely(tmp_vcpu->cpu != -1)) {
1967 ret = -EBUSY;
1968 goto out_vgic_unlock;
1969 }
1970 }
1971
Christoffer Dallcbd333a2013-11-15 20:51:31 -08001972 /*
1973 * Move all pending IRQs from the LRs on all VCPUs so the pending
1974 * state can be properly represented in the register state accessible
1975 * through this API.
1976 */
1977 kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm)
1978 vgic_unqueue_irqs(tmp_vcpu);
1979
Christoffer Dallc07a0192013-10-25 21:17:31 +01001980 offset -= r->base;
1981 r->handle_mmio(vcpu, &mmio, offset);
1982
1983 if (!is_write)
1984 *reg = mmio_data_read(&mmio, ~0);
1985
1986 ret = 0;
1987out_vgic_unlock:
1988 spin_unlock(&vgic->lock);
1989out:
1990 mutex_unlock(&dev->kvm->lock);
1991 return ret;
1992}
1993
Christoffer Dall73306722013-10-25 17:29:18 +01001994static int vgic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1995{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001996 int r;
1997
1998 switch (attr->group) {
1999 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2000 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2001 u64 addr;
2002 unsigned long type = (unsigned long)attr->attr;
2003
2004 if (copy_from_user(&addr, uaddr, sizeof(addr)))
2005 return -EFAULT;
2006
2007 r = kvm_vgic_addr(dev->kvm, type, &addr, true);
2008 return (r == -ENODEV) ? -ENXIO : r;
2009 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01002010
2011 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2012 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
2013 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2014 u32 reg;
2015
2016 if (get_user(reg, uaddr))
2017 return -EFAULT;
2018
2019 return vgic_attr_regs_access(dev, attr, &reg, true);
2020 }
2021
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002022 }
2023
Christoffer Dall73306722013-10-25 17:29:18 +01002024 return -ENXIO;
2025}
2026
2027static int vgic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2028{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002029 int r = -ENXIO;
2030
2031 switch (attr->group) {
2032 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2033 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2034 u64 addr;
2035 unsigned long type = (unsigned long)attr->attr;
2036
2037 r = kvm_vgic_addr(dev->kvm, type, &addr, false);
2038 if (r)
2039 return (r == -ENODEV) ? -ENXIO : r;
2040
2041 if (copy_to_user(uaddr, &addr, sizeof(addr)))
2042 return -EFAULT;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002043 break;
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002044 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01002045
2046 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2047 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
2048 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2049 u32 reg = 0;
2050
2051 r = vgic_attr_regs_access(dev, attr, &reg, false);
2052 if (r)
2053 return r;
2054 r = put_user(reg, uaddr);
2055 break;
2056 }
2057
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002058 }
2059
2060 return r;
Christoffer Dall73306722013-10-25 17:29:18 +01002061}
2062
Christoffer Dallc07a0192013-10-25 21:17:31 +01002063static int vgic_has_attr_regs(const struct mmio_range *ranges,
2064 phys_addr_t offset)
2065{
2066 struct kvm_exit_mmio dev_attr_mmio;
2067
2068 dev_attr_mmio.len = 4;
2069 if (find_matching_range(ranges, &dev_attr_mmio, offset))
2070 return 0;
2071 else
2072 return -ENXIO;
2073}
2074
Christoffer Dall73306722013-10-25 17:29:18 +01002075static int vgic_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2076{
Christoffer Dallc07a0192013-10-25 21:17:31 +01002077 phys_addr_t offset;
2078
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002079 switch (attr->group) {
2080 case KVM_DEV_ARM_VGIC_GRP_ADDR:
2081 switch (attr->attr) {
2082 case KVM_VGIC_V2_ADDR_TYPE_DIST:
2083 case KVM_VGIC_V2_ADDR_TYPE_CPU:
2084 return 0;
2085 }
2086 break;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002087 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2088 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2089 return vgic_has_attr_regs(vgic_dist_ranges, offset);
2090 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
2091 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2092 return vgic_has_attr_regs(vgic_cpu_ranges, offset);
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002093 }
Christoffer Dall73306722013-10-25 17:29:18 +01002094 return -ENXIO;
2095}
2096
2097static void vgic_destroy(struct kvm_device *dev)
2098{
2099 kfree(dev);
2100}
2101
2102static int vgic_create(struct kvm_device *dev, u32 type)
2103{
2104 return kvm_vgic_create(dev->kvm);
2105}
2106
Will Deaconc06a8412014-09-02 10:27:34 +01002107static struct kvm_device_ops kvm_arm_vgic_v2_ops = {
Christoffer Dall73306722013-10-25 17:29:18 +01002108 .name = "kvm-arm-vgic",
2109 .create = vgic_create,
2110 .destroy = vgic_destroy,
2111 .set_attr = vgic_set_attr,
2112 .get_attr = vgic_get_attr,
2113 .has_attr = vgic_has_attr,
2114};
Will Deaconc06a8412014-09-02 10:27:34 +01002115
2116static void vgic_init_maintenance_interrupt(void *info)
2117{
2118 enable_percpu_irq(vgic->maint_irq, 0);
2119}
2120
2121static int vgic_cpu_notify(struct notifier_block *self,
2122 unsigned long action, void *cpu)
2123{
2124 switch (action) {
2125 case CPU_STARTING:
2126 case CPU_STARTING_FROZEN:
2127 vgic_init_maintenance_interrupt(NULL);
2128 break;
2129 case CPU_DYING:
2130 case CPU_DYING_FROZEN:
2131 disable_percpu_irq(vgic->maint_irq);
2132 break;
2133 }
2134
2135 return NOTIFY_OK;
2136}
2137
2138static struct notifier_block vgic_cpu_nb = {
2139 .notifier_call = vgic_cpu_notify,
2140};
2141
2142static const struct of_device_id vgic_ids[] = {
2143 { .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, },
2144 { .compatible = "arm,gic-v3", .data = vgic_v3_probe, },
2145 {},
2146};
2147
2148int kvm_vgic_hyp_init(void)
2149{
2150 const struct of_device_id *matched_id;
Christoffer Dalla875daf2014-09-18 18:15:32 -07002151 const int (*vgic_probe)(struct device_node *,const struct vgic_ops **,
2152 const struct vgic_params **);
Will Deaconc06a8412014-09-02 10:27:34 +01002153 struct device_node *vgic_node;
2154 int ret;
2155
2156 vgic_node = of_find_matching_node_and_match(NULL,
2157 vgic_ids, &matched_id);
2158 if (!vgic_node) {
2159 kvm_err("error: no compatible GIC node found\n");
2160 return -ENODEV;
2161 }
2162
2163 vgic_probe = matched_id->data;
2164 ret = vgic_probe(vgic_node, &vgic_ops, &vgic);
2165 if (ret)
2166 return ret;
2167
2168 ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler,
2169 "vgic", kvm_get_running_vcpus());
2170 if (ret) {
2171 kvm_err("Cannot register interrupt %d\n", vgic->maint_irq);
2172 return ret;
2173 }
2174
2175 ret = __register_cpu_notifier(&vgic_cpu_nb);
2176 if (ret) {
2177 kvm_err("Cannot register vgic CPU notifier\n");
2178 goto out_free_irq;
2179 }
2180
2181 /* Callback into for arch code for setup */
2182 vgic_arch_setup(vgic);
2183
2184 on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
2185
2186 return kvm_register_device_ops(&kvm_arm_vgic_v2_ops,
2187 KVM_DEV_TYPE_ARM_VGIC_V2);
2188
2189out_free_irq:
2190 free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus());
2191 return ret;
2192}