blob: 06073fac03a9bf9eb430237d6f1aed252a570b4c [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
Peter Maydell6d3cfbe2014-12-04 15:02:24 +000094static int vgic_init(struct kvm *kvm);
Marc Zyngiera1fcb442013-01-21 19:36:15 -050095static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +010096static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -050097static void vgic_update_state(struct kvm *kvm);
Marc Zyngier5863c2c2013-01-21 19:36:15 -050098static void vgic_kick_vcpus(struct kvm *kvm);
Marc Zyngierc1bfb572014-07-08 12:09:01 +010099static u8 *vgic_get_sgi_sources(struct vgic_dist *dist, int vcpu_id, int sgi);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500100static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100101static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr);
102static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, struct vgic_lr lr_desc);
Marc Zyngierbeee38b2014-02-04 17:48:10 +0000103static void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
104static void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500105
Marc Zyngier8f186d52014-02-04 18:13:03 +0000106static const struct vgic_ops *vgic_ops;
107static const struct vgic_params *vgic;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500108
Andre Przywarab26e5fd2014-06-02 16:19:12 +0200109static void add_sgi_source(struct kvm_vcpu *vcpu, int irq, int source)
110{
111 vcpu->kvm->arch.vgic.vm_ops.add_sgi_source(vcpu, irq, source);
112}
113
114static bool queue_sgi(struct kvm_vcpu *vcpu, int irq)
115{
116 return vcpu->kvm->arch.vgic.vm_ops.queue_sgi(vcpu, irq);
117}
118
119int kvm_vgic_map_resources(struct kvm *kvm)
120{
121 return kvm->arch.vgic.vm_ops.map_resources(kvm, vgic);
122}
123
Victor Kamensky9662fb42014-06-12 09:30:10 -0700124/*
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100125 * struct vgic_bitmap contains a bitmap made of unsigned longs, but
126 * extracts u32s out of them.
Victor Kamensky9662fb42014-06-12 09:30:10 -0700127 *
128 * This does not work on 64-bit BE systems, because the bitmap access
129 * will store two consecutive 32-bit words with the higher-addressed
130 * register's bits at the lower index and the lower-addressed register's
131 * bits at the higher index.
132 *
133 * Therefore, swizzle the register index when accessing the 32-bit word
134 * registers to access the right register's value.
135 */
136#if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 64
137#define REG_OFFSET_SWIZZLE 1
138#else
139#define REG_OFFSET_SWIZZLE 0
140#endif
Marc Zyngierb47ef922013-01-21 19:36:14 -0500141
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100142static int vgic_init_bitmap(struct vgic_bitmap *b, int nr_cpus, int nr_irqs)
143{
144 int nr_longs;
145
146 nr_longs = nr_cpus + BITS_TO_LONGS(nr_irqs - VGIC_NR_PRIVATE_IRQS);
147
148 b->private = kzalloc(sizeof(unsigned long) * nr_longs, GFP_KERNEL);
149 if (!b->private)
150 return -ENOMEM;
151
152 b->shared = b->private + nr_cpus;
153
154 return 0;
155}
156
157static void vgic_free_bitmap(struct vgic_bitmap *b)
158{
159 kfree(b->private);
160 b->private = NULL;
161 b->shared = NULL;
162}
163
Christoffer Dall2df36a52014-09-28 16:04:26 +0200164/*
165 * Call this function to convert a u64 value to an unsigned long * bitmask
166 * in a way that works on both 32-bit and 64-bit LE and BE platforms.
167 *
168 * Warning: Calling this function may modify *val.
169 */
170static unsigned long *u64_to_bitmask(u64 *val)
171{
172#if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 32
173 *val = (*val >> 32) | (*val << 32);
174#endif
175 return (unsigned long *)val;
176}
177
Marc Zyngierb47ef922013-01-21 19:36:14 -0500178static u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x,
179 int cpuid, u32 offset)
180{
181 offset >>= 2;
182 if (!offset)
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100183 return (u32 *)(x->private + cpuid) + REG_OFFSET_SWIZZLE;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500184 else
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100185 return (u32 *)(x->shared) + ((offset - 1) ^ REG_OFFSET_SWIZZLE);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500186}
187
188static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
189 int cpuid, int irq)
190{
191 if (irq < VGIC_NR_PRIVATE_IRQS)
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100192 return test_bit(irq, x->private + cpuid);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500193
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100194 return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500195}
196
197static void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
198 int irq, int val)
199{
200 unsigned long *reg;
201
202 if (irq < VGIC_NR_PRIVATE_IRQS) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100203 reg = x->private + cpuid;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500204 } else {
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100205 reg = x->shared;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500206 irq -= VGIC_NR_PRIVATE_IRQS;
207 }
208
209 if (val)
210 set_bit(irq, reg);
211 else
212 clear_bit(irq, reg);
213}
214
215static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
216{
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100217 return x->private + cpuid;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500218}
219
220static unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
221{
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100222 return x->shared;
223}
224
225static int vgic_init_bytemap(struct vgic_bytemap *x, int nr_cpus, int nr_irqs)
226{
227 int size;
228
229 size = nr_cpus * VGIC_NR_PRIVATE_IRQS;
230 size += nr_irqs - VGIC_NR_PRIVATE_IRQS;
231
232 x->private = kzalloc(size, GFP_KERNEL);
233 if (!x->private)
234 return -ENOMEM;
235
236 x->shared = x->private + nr_cpus * VGIC_NR_PRIVATE_IRQS / sizeof(u32);
237 return 0;
238}
239
240static void vgic_free_bytemap(struct vgic_bytemap *b)
241{
242 kfree(b->private);
243 b->private = NULL;
244 b->shared = NULL;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500245}
246
247static u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
248{
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100249 u32 *reg;
250
251 if (offset < VGIC_NR_PRIVATE_IRQS) {
252 reg = x->private;
253 offset += cpuid * VGIC_NR_PRIVATE_IRQS;
254 } else {
255 reg = x->shared;
256 offset -= VGIC_NR_PRIVATE_IRQS;
257 }
258
259 return reg + (offset / sizeof(u32));
Marc Zyngierb47ef922013-01-21 19:36:14 -0500260}
261
262#define VGIC_CFG_LEVEL 0
263#define VGIC_CFG_EDGE 1
264
265static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq)
266{
267 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
268 int irq_val;
269
270 irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq);
271 return irq_val == VGIC_CFG_EDGE;
272}
273
274static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq)
275{
276 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
277
278 return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq);
279}
280
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200281static int vgic_irq_is_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500282{
283 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
284
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200285 return vgic_bitmap_get_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500286}
287
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200288static void vgic_irq_set_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500289{
290 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
291
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200292 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 1);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500293}
294
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200295static void vgic_irq_clear_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500296{
297 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
298
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200299 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 0);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500300}
301
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200302static int vgic_dist_irq_get_level(struct kvm_vcpu *vcpu, int irq)
303{
304 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
305
306 return vgic_bitmap_get_irq_val(&dist->irq_level, vcpu->vcpu_id, irq);
307}
308
309static void vgic_dist_irq_set_level(struct kvm_vcpu *vcpu, int irq)
310{
311 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
312
313 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 1);
314}
315
316static void vgic_dist_irq_clear_level(struct kvm_vcpu *vcpu, int irq)
317{
318 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
319
320 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 0);
321}
322
323static int vgic_dist_irq_soft_pend(struct kvm_vcpu *vcpu, int irq)
324{
325 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
326
327 return vgic_bitmap_get_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq);
328}
329
330static void vgic_dist_irq_clear_soft_pend(struct kvm_vcpu *vcpu, int irq)
331{
332 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
333
334 vgic_bitmap_set_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq, 0);
335}
336
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500337static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
338{
339 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
340
Christoffer Dall227844f2014-06-09 12:27:18 +0200341 return vgic_bitmap_get_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500342}
343
Christoffer Dall227844f2014-06-09 12:27:18 +0200344static void vgic_dist_irq_set_pending(struct kvm_vcpu *vcpu, int irq)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500345{
346 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
347
Christoffer Dall227844f2014-06-09 12:27:18 +0200348 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 1);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500349}
350
Christoffer Dall227844f2014-06-09 12:27:18 +0200351static void vgic_dist_irq_clear_pending(struct kvm_vcpu *vcpu, int irq)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500352{
353 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
354
Christoffer Dall227844f2014-06-09 12:27:18 +0200355 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 0);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500356}
357
358static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq)
359{
360 if (irq < VGIC_NR_PRIVATE_IRQS)
361 set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
362 else
363 set_bit(irq - VGIC_NR_PRIVATE_IRQS,
364 vcpu->arch.vgic_cpu.pending_shared);
365}
366
367static void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
368{
369 if (irq < VGIC_NR_PRIVATE_IRQS)
370 clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
371 else
372 clear_bit(irq - VGIC_NR_PRIVATE_IRQS,
373 vcpu->arch.vgic_cpu.pending_shared);
374}
375
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200376static bool vgic_can_sample_irq(struct kvm_vcpu *vcpu, int irq)
377{
378 return vgic_irq_is_edge(vcpu, irq) || !vgic_irq_is_queued(vcpu, irq);
379}
380
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500381static u32 mmio_data_read(struct kvm_exit_mmio *mmio, u32 mask)
382{
Victor Kamensky1c9f0472014-06-12 09:30:04 -0700383 return le32_to_cpu(*((u32 *)mmio->data)) & mask;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500384}
385
386static void mmio_data_write(struct kvm_exit_mmio *mmio, u32 mask, u32 value)
387{
Victor Kamensky1c9f0472014-06-12 09:30:04 -0700388 *((u32 *)mmio->data) = cpu_to_le32(value) & mask;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500389}
390
391/**
392 * vgic_reg_access - access vgic register
393 * @mmio: pointer to the data describing the mmio access
394 * @reg: pointer to the virtual backing of vgic distributor data
395 * @offset: least significant 2 bits used for word offset
396 * @mode: ACCESS_ mode (see defines above)
397 *
398 * Helper to make vgic register access easier using one of the access
399 * modes defined for vgic register access
400 * (read,raz,write-ignored,setbit,clearbit,write)
401 */
402static void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
403 phys_addr_t offset, int mode)
404{
405 int word_offset = (offset & 3) * 8;
406 u32 mask = (1UL << (mmio->len * 8)) - 1;
407 u32 regval;
408
409 /*
410 * Any alignment fault should have been delivered to the guest
411 * directly (ARM ARM B3.12.7 "Prioritization of aborts").
412 */
413
414 if (reg) {
415 regval = *reg;
416 } else {
417 BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED));
418 regval = 0;
419 }
420
421 if (mmio->is_write) {
422 u32 data = mmio_data_read(mmio, mask) << word_offset;
423 switch (ACCESS_WRITE_MASK(mode)) {
424 case ACCESS_WRITE_IGNORED:
425 return;
426
427 case ACCESS_WRITE_SETBIT:
428 regval |= data;
429 break;
430
431 case ACCESS_WRITE_CLEARBIT:
432 regval &= ~data;
433 break;
434
435 case ACCESS_WRITE_VALUE:
436 regval = (regval & ~(mask << word_offset)) | data;
437 break;
438 }
439 *reg = regval;
440 } else {
441 switch (ACCESS_READ_MASK(mode)) {
442 case ACCESS_READ_RAZ:
443 regval = 0;
444 /* fall through */
445
446 case ACCESS_READ_VALUE:
447 mmio_data_write(mmio, mask, regval >> word_offset);
448 }
449 }
450}
451
Marc Zyngierb47ef922013-01-21 19:36:14 -0500452static bool handle_mmio_misc(struct kvm_vcpu *vcpu,
453 struct kvm_exit_mmio *mmio, phys_addr_t offset)
454{
455 u32 reg;
456 u32 word_offset = offset & 3;
457
458 switch (offset & ~3) {
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -0700459 case 0: /* GICD_CTLR */
Marc Zyngierb47ef922013-01-21 19:36:14 -0500460 reg = vcpu->kvm->arch.vgic.enabled;
461 vgic_reg_access(mmio, &reg, word_offset,
462 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
463 if (mmio->is_write) {
464 vcpu->kvm->arch.vgic.enabled = reg & 1;
465 vgic_update_state(vcpu->kvm);
466 return true;
467 }
468 break;
469
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -0700470 case 4: /* GICD_TYPER */
Marc Zyngierb47ef922013-01-21 19:36:14 -0500471 reg = (atomic_read(&vcpu->kvm->online_vcpus) - 1) << 5;
Marc Zyngier5fb66da2014-07-08 12:09:05 +0100472 reg |= (vcpu->kvm->arch.vgic.nr_irqs >> 5) - 1;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500473 vgic_reg_access(mmio, &reg, word_offset,
474 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
475 break;
476
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -0700477 case 8: /* GICD_IIDR */
478 reg = (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500479 vgic_reg_access(mmio, &reg, word_offset,
480 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
481 break;
482 }
483
484 return false;
485}
486
487static bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu,
488 struct kvm_exit_mmio *mmio, phys_addr_t offset)
489{
490 vgic_reg_access(mmio, NULL, offset,
491 ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
492 return false;
493}
494
Andre Przywarad97f6832014-06-11 14:11:49 +0200495static bool vgic_handle_enable_reg(struct kvm *kvm, struct kvm_exit_mmio *mmio,
496 phys_addr_t offset, int vcpu_id, int access)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500497{
Andre Przywarad97f6832014-06-11 14:11:49 +0200498 u32 *reg;
499 int mode = ACCESS_READ_VALUE | access;
500 struct kvm_vcpu *target_vcpu = kvm_get_vcpu(kvm, vcpu_id);
501
502 reg = vgic_bitmap_get_reg(&kvm->arch.vgic.irq_enabled, vcpu_id, offset);
503 vgic_reg_access(mmio, reg, offset, mode);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500504 if (mmio->is_write) {
Andre Przywarad97f6832014-06-11 14:11:49 +0200505 if (access & ACCESS_WRITE_CLEARBIT) {
506 if (offset < 4) /* Force SGI enabled */
507 *reg |= 0xffff;
508 vgic_retire_disabled_irqs(target_vcpu);
509 }
510 vgic_update_state(kvm);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500511 return true;
512 }
513
514 return false;
515}
516
Andre Przywarad97f6832014-06-11 14:11:49 +0200517static bool handle_mmio_set_enable_reg(struct kvm_vcpu *vcpu,
518 struct kvm_exit_mmio *mmio,
519 phys_addr_t offset)
520{
521 return vgic_handle_enable_reg(vcpu->kvm, mmio, offset,
522 vcpu->vcpu_id, ACCESS_WRITE_SETBIT);
523}
524
Marc Zyngierb47ef922013-01-21 19:36:14 -0500525static bool handle_mmio_clear_enable_reg(struct kvm_vcpu *vcpu,
526 struct kvm_exit_mmio *mmio,
527 phys_addr_t offset)
528{
Andre Przywarad97f6832014-06-11 14:11:49 +0200529 return vgic_handle_enable_reg(vcpu->kvm, mmio, offset,
530 vcpu->vcpu_id, ACCESS_WRITE_CLEARBIT);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500531}
532
Andre Przywarad97f6832014-06-11 14:11:49 +0200533static bool vgic_handle_set_pending_reg(struct kvm *kvm,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500534 struct kvm_exit_mmio *mmio,
Andre Przywarad97f6832014-06-11 14:11:49 +0200535 phys_addr_t offset, int vcpu_id)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500536{
Christoffer Dall9da48b52014-06-14 22:30:45 +0200537 u32 *reg, orig;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200538 u32 level_mask;
Andre Przywarad97f6832014-06-11 14:11:49 +0200539 int mode = ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT;
540 struct vgic_dist *dist = &kvm->arch.vgic;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200541
Andre Przywarad97f6832014-06-11 14:11:49 +0200542 reg = vgic_bitmap_get_reg(&dist->irq_cfg, vcpu_id, offset);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200543 level_mask = (~(*reg));
544
545 /* Mark both level and edge triggered irqs as pending */
Andre Przywarad97f6832014-06-11 14:11:49 +0200546 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
Christoffer Dall9da48b52014-06-14 22:30:45 +0200547 orig = *reg;
Andre Przywarad97f6832014-06-11 14:11:49 +0200548 vgic_reg_access(mmio, reg, offset, mode);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200549
Marc Zyngierb47ef922013-01-21 19:36:14 -0500550 if (mmio->is_write) {
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200551 /* Set the soft-pending flag only for level-triggered irqs */
552 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
Andre Przywarad97f6832014-06-11 14:11:49 +0200553 vcpu_id, offset);
554 vgic_reg_access(mmio, reg, offset, mode);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200555 *reg &= level_mask;
556
Christoffer Dall9da48b52014-06-14 22:30:45 +0200557 /* Ignore writes to SGIs */
558 if (offset < 2) {
559 *reg &= ~0xffff;
560 *reg |= orig & 0xffff;
561 }
562
Andre Przywarad97f6832014-06-11 14:11:49 +0200563 vgic_update_state(kvm);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500564 return true;
565 }
566
567 return false;
568}
569
Andre Przywarad97f6832014-06-11 14:11:49 +0200570static bool vgic_handle_clear_pending_reg(struct kvm *kvm,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500571 struct kvm_exit_mmio *mmio,
Andre Przywarad97f6832014-06-11 14:11:49 +0200572 phys_addr_t offset, int vcpu_id)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500573{
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200574 u32 *level_active;
Christoffer Dall9da48b52014-06-14 22:30:45 +0200575 u32 *reg, orig;
Andre Przywarad97f6832014-06-11 14:11:49 +0200576 int mode = ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT;
577 struct vgic_dist *dist = &kvm->arch.vgic;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200578
Andre Przywarad97f6832014-06-11 14:11:49 +0200579 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
Christoffer Dall9da48b52014-06-14 22:30:45 +0200580 orig = *reg;
Andre Przywarad97f6832014-06-11 14:11:49 +0200581 vgic_reg_access(mmio, reg, offset, mode);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500582 if (mmio->is_write) {
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200583 /* Re-set level triggered level-active interrupts */
584 level_active = vgic_bitmap_get_reg(&dist->irq_level,
Andre Przywarad97f6832014-06-11 14:11:49 +0200585 vcpu_id, offset);
586 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200587 *reg |= *level_active;
588
Christoffer Dall9da48b52014-06-14 22:30:45 +0200589 /* Ignore writes to SGIs */
590 if (offset < 2) {
591 *reg &= ~0xffff;
592 *reg |= orig & 0xffff;
593 }
594
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200595 /* Clear soft-pending flags */
596 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
Andre Przywarad97f6832014-06-11 14:11:49 +0200597 vcpu_id, offset);
598 vgic_reg_access(mmio, reg, offset, mode);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200599
Andre Przywarad97f6832014-06-11 14:11:49 +0200600 vgic_update_state(kvm);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500601 return true;
602 }
Marc Zyngierb47ef922013-01-21 19:36:14 -0500603 return false;
604}
605
Andre Przywarad97f6832014-06-11 14:11:49 +0200606static bool handle_mmio_set_pending_reg(struct kvm_vcpu *vcpu,
607 struct kvm_exit_mmio *mmio,
608 phys_addr_t offset)
609{
610 return vgic_handle_set_pending_reg(vcpu->kvm, mmio, offset,
611 vcpu->vcpu_id);
612}
613
614static bool handle_mmio_clear_pending_reg(struct kvm_vcpu *vcpu,
615 struct kvm_exit_mmio *mmio,
616 phys_addr_t offset)
617{
618 return vgic_handle_clear_pending_reg(vcpu->kvm, mmio, offset,
619 vcpu->vcpu_id);
620}
621
Marc Zyngierb47ef922013-01-21 19:36:14 -0500622static bool handle_mmio_priority_reg(struct kvm_vcpu *vcpu,
623 struct kvm_exit_mmio *mmio,
624 phys_addr_t offset)
625{
626 u32 *reg = vgic_bytemap_get_reg(&vcpu->kvm->arch.vgic.irq_priority,
627 vcpu->vcpu_id, offset);
628 vgic_reg_access(mmio, reg, offset,
629 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
630 return false;
631}
632
633#define GICD_ITARGETSR_SIZE 32
634#define GICD_CPUTARGETS_BITS 8
635#define GICD_IRQS_PER_ITARGETSR (GICD_ITARGETSR_SIZE / GICD_CPUTARGETS_BITS)
636static u32 vgic_get_target_reg(struct kvm *kvm, int irq)
637{
638 struct vgic_dist *dist = &kvm->arch.vgic;
Marc Zyngier986af8e2013-08-29 11:08:22 +0100639 int i;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500640 u32 val = 0;
641
642 irq -= VGIC_NR_PRIVATE_IRQS;
643
Marc Zyngier986af8e2013-08-29 11:08:22 +0100644 for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++)
645 val |= 1 << (dist->irq_spi_cpu[irq + i] + i * 8);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500646
647 return val;
648}
649
650static void vgic_set_target_reg(struct kvm *kvm, u32 val, int irq)
651{
652 struct vgic_dist *dist = &kvm->arch.vgic;
653 struct kvm_vcpu *vcpu;
654 int i, c;
655 unsigned long *bmap;
656 u32 target;
657
658 irq -= VGIC_NR_PRIVATE_IRQS;
659
660 /*
661 * Pick the LSB in each byte. This ensures we target exactly
662 * one vcpu per IRQ. If the byte is null, assume we target
663 * CPU0.
664 */
665 for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++) {
666 int shift = i * GICD_CPUTARGETS_BITS;
667 target = ffs((val >> shift) & 0xffU);
668 target = target ? (target - 1) : 0;
669 dist->irq_spi_cpu[irq + i] = target;
670 kvm_for_each_vcpu(c, vcpu, kvm) {
671 bmap = vgic_bitmap_get_shared_map(&dist->irq_spi_target[c]);
672 if (c == target)
673 set_bit(irq + i, bmap);
674 else
675 clear_bit(irq + i, bmap);
676 }
677 }
678}
679
680static bool handle_mmio_target_reg(struct kvm_vcpu *vcpu,
681 struct kvm_exit_mmio *mmio,
682 phys_addr_t offset)
683{
684 u32 reg;
685
686 /* We treat the banked interrupts targets as read-only */
687 if (offset < 32) {
688 u32 roreg = 1 << vcpu->vcpu_id;
689 roreg |= roreg << 8;
690 roreg |= roreg << 16;
691
692 vgic_reg_access(mmio, &roreg, offset,
693 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
694 return false;
695 }
696
697 reg = vgic_get_target_reg(vcpu->kvm, offset & ~3U);
698 vgic_reg_access(mmio, &reg, offset,
699 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
700 if (mmio->is_write) {
701 vgic_set_target_reg(vcpu->kvm, reg, offset & ~3U);
702 vgic_update_state(vcpu->kvm);
703 return true;
704 }
705
706 return false;
707}
708
709static u32 vgic_cfg_expand(u16 val)
710{
711 u32 res = 0;
712 int i;
713
714 /*
715 * Turn a 16bit value like abcd...mnop into a 32bit word
716 * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
717 */
718 for (i = 0; i < 16; i++)
719 res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
720
721 return res;
722}
723
724static u16 vgic_cfg_compress(u32 val)
725{
726 u16 res = 0;
727 int i;
728
729 /*
730 * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
731 * abcd...mnop which is what we really care about.
732 */
733 for (i = 0; i < 16; i++)
734 res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
735
736 return res;
737}
738
739/*
740 * The distributor uses 2 bits per IRQ for the CFG register, but the
741 * LSB is always 0. As such, we only keep the upper bit, and use the
742 * two above functions to compress/expand the bits
743 */
Andre Przywarad97f6832014-06-11 14:11:49 +0200744static bool vgic_handle_cfg_reg(u32 *reg, struct kvm_exit_mmio *mmio,
745 phys_addr_t offset)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500746{
747 u32 val;
Marc Zyngier6545eae2013-08-29 11:08:23 +0100748
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200749 if (offset & 4)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500750 val = *reg >> 16;
751 else
752 val = *reg & 0xffff;
753
754 val = vgic_cfg_expand(val);
755 vgic_reg_access(mmio, &val, offset,
756 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
757 if (mmio->is_write) {
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200758 if (offset < 8) {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500759 *reg = ~0U; /* Force PPIs/SGIs to 1 */
760 return false;
761 }
762
763 val = vgic_cfg_compress(val);
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200764 if (offset & 4) {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500765 *reg &= 0xffff;
766 *reg |= val << 16;
767 } else {
768 *reg &= 0xffff << 16;
769 *reg |= val;
770 }
771 }
772
773 return false;
774}
775
Andre Przywarad97f6832014-06-11 14:11:49 +0200776static bool handle_mmio_cfg_reg(struct kvm_vcpu *vcpu,
777 struct kvm_exit_mmio *mmio, phys_addr_t offset)
778{
779 u32 *reg;
780
781 reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_cfg,
782 vcpu->vcpu_id, offset >> 1);
783
784 return vgic_handle_cfg_reg(reg, mmio, offset);
785}
786
Marc Zyngierb47ef922013-01-21 19:36:14 -0500787static bool handle_mmio_sgi_reg(struct kvm_vcpu *vcpu,
788 struct kvm_exit_mmio *mmio, phys_addr_t offset)
789{
790 u32 reg;
791 vgic_reg_access(mmio, &reg, offset,
792 ACCESS_READ_RAZ | ACCESS_WRITE_VALUE);
793 if (mmio->is_write) {
794 vgic_dispatch_sgi(vcpu, reg);
795 vgic_update_state(vcpu->kvm);
796 return true;
797 }
798
799 return false;
800}
801
Andre Przywarab26e5fd2014-06-02 16:19:12 +0200802static void vgic_v2_add_sgi_source(struct kvm_vcpu *vcpu, int irq, int source)
803{
804 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
805
806 *vgic_get_sgi_sources(dist, vcpu->vcpu_id, irq) |= 1 << source;
807}
808
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800809/**
810 * vgic_unqueue_irqs - move pending IRQs from LRs to the distributor
811 * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs
812 *
813 * Move any pending IRQs that have already been assigned to LRs back to the
814 * emulated distributor state so that the complete emulated state can be read
815 * from the main emulation structures without investigating the LRs.
816 *
817 * Note that IRQs in the active state in the LRs get their pending state moved
818 * to the distributor but the active state stays in the LRs, because we don't
819 * track the active state on the distributor side.
820 */
821static void vgic_unqueue_irqs(struct kvm_vcpu *vcpu)
822{
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800823 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100824 int i;
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800825
826 for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100827 struct vgic_lr lr = vgic_get_lr(vcpu, i);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800828
829 /*
830 * There are three options for the state bits:
831 *
832 * 01: pending
833 * 10: active
834 * 11: pending and active
835 *
836 * If the LR holds only an active interrupt (not pending) then
837 * just leave it alone.
838 */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100839 if ((lr.state & LR_STATE_MASK) == LR_STATE_ACTIVE)
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800840 continue;
841
842 /*
843 * Reestablish the pending state on the distributor and the
844 * CPU interface. It may have already been pending, but that
845 * is fine, then we are only setting a few bits that were
846 * already set.
847 */
Christoffer Dall227844f2014-06-09 12:27:18 +0200848 vgic_dist_irq_set_pending(vcpu, lr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100849 if (lr.irq < VGIC_NR_SGIS)
Andre Przywarab26e5fd2014-06-02 16:19:12 +0200850 add_sgi_source(vcpu, lr.irq, lr.source);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100851 lr.state &= ~LR_STATE_PENDING;
852 vgic_set_lr(vcpu, i, lr);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800853
854 /*
855 * If there's no state left on the LR (it could still be
856 * active), then the LR does not hold any useful info and can
857 * be marked as free for other use.
858 */
Christoffer Dallcced50c2014-06-14 22:37:33 +0200859 if (!(lr.state & LR_STATE_MASK)) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100860 vgic_retire_lr(i, lr.irq, vcpu);
Christoffer Dallcced50c2014-06-14 22:37:33 +0200861 vgic_irq_clear_queued(vcpu, lr.irq);
862 }
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800863
864 /* Finally update the VGIC state. */
865 vgic_update_state(vcpu->kvm);
866 }
867}
868
Christoffer Dall90a53552013-10-25 21:22:31 +0100869/* Handle reads of GICD_CPENDSGIRn and GICD_SPENDSGIRn */
870static bool read_set_clear_sgi_pend_reg(struct kvm_vcpu *vcpu,
871 struct kvm_exit_mmio *mmio,
872 phys_addr_t offset)
Christoffer Dallc07a0192013-10-25 21:17:31 +0100873{
Christoffer Dall90a53552013-10-25 21:22:31 +0100874 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
875 int sgi;
Christoffer Dall0fea6d72014-09-25 18:41:07 +0200876 int min_sgi = (offset & ~0x3);
Christoffer Dall90a53552013-10-25 21:22:31 +0100877 int max_sgi = min_sgi + 3;
878 int vcpu_id = vcpu->vcpu_id;
879 u32 reg = 0;
880
881 /* Copy source SGIs from distributor side */
882 for (sgi = min_sgi; sgi <= max_sgi; sgi++) {
883 int shift = 8 * (sgi - min_sgi);
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100884 reg |= ((u32)*vgic_get_sgi_sources(dist, vcpu_id, sgi)) << shift;
Christoffer Dall90a53552013-10-25 21:22:31 +0100885 }
886
887 mmio_data_write(mmio, ~0, reg);
Christoffer Dallc07a0192013-10-25 21:17:31 +0100888 return false;
889}
890
Christoffer Dall90a53552013-10-25 21:22:31 +0100891static bool write_set_clear_sgi_pend_reg(struct kvm_vcpu *vcpu,
892 struct kvm_exit_mmio *mmio,
893 phys_addr_t offset, bool set)
894{
895 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
896 int sgi;
Christoffer Dall0fea6d72014-09-25 18:41:07 +0200897 int min_sgi = (offset & ~0x3);
Christoffer Dall90a53552013-10-25 21:22:31 +0100898 int max_sgi = min_sgi + 3;
899 int vcpu_id = vcpu->vcpu_id;
900 u32 reg;
901 bool updated = false;
902
903 reg = mmio_data_read(mmio, ~0);
904
905 /* Clear pending SGIs on the distributor */
906 for (sgi = min_sgi; sgi <= max_sgi; sgi++) {
907 u8 mask = reg >> (8 * (sgi - min_sgi));
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100908 u8 *src = vgic_get_sgi_sources(dist, vcpu_id, sgi);
Christoffer Dall90a53552013-10-25 21:22:31 +0100909 if (set) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100910 if ((*src & mask) != mask)
Christoffer Dall90a53552013-10-25 21:22:31 +0100911 updated = true;
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100912 *src |= mask;
Christoffer Dall90a53552013-10-25 21:22:31 +0100913 } else {
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100914 if (*src & mask)
Christoffer Dall90a53552013-10-25 21:22:31 +0100915 updated = true;
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100916 *src &= ~mask;
Christoffer Dall90a53552013-10-25 21:22:31 +0100917 }
918 }
919
920 if (updated)
921 vgic_update_state(vcpu->kvm);
922
923 return updated;
924}
925
Christoffer Dallc07a0192013-10-25 21:17:31 +0100926static bool handle_mmio_sgi_set(struct kvm_vcpu *vcpu,
927 struct kvm_exit_mmio *mmio,
928 phys_addr_t offset)
929{
Christoffer Dall90a53552013-10-25 21:22:31 +0100930 if (!mmio->is_write)
931 return read_set_clear_sgi_pend_reg(vcpu, mmio, offset);
932 else
933 return write_set_clear_sgi_pend_reg(vcpu, mmio, offset, true);
934}
935
936static bool handle_mmio_sgi_clear(struct kvm_vcpu *vcpu,
937 struct kvm_exit_mmio *mmio,
938 phys_addr_t offset)
939{
940 if (!mmio->is_write)
941 return read_set_clear_sgi_pend_reg(vcpu, mmio, offset);
942 else
943 return write_set_clear_sgi_pend_reg(vcpu, mmio, offset, false);
Christoffer Dallc07a0192013-10-25 21:17:31 +0100944}
945
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500946/*
947 * I would have liked to use the kvm_bus_io_*() API instead, but it
948 * cannot cope with banked registers (only the VM pointer is passed
949 * around, and we need the vcpu). One of these days, someone please
950 * fix it!
951 */
952struct mmio_range {
953 phys_addr_t base;
954 unsigned long len;
Marc Zyngierc3c91832014-07-08 12:09:04 +0100955 int bits_per_irq;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500956 bool (*handle_mmio)(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
957 phys_addr_t offset);
958};
959
Christoffer Dall1006e8c2013-09-23 14:55:56 -0700960static const struct mmio_range vgic_dist_ranges[] = {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500961 {
962 .base = GIC_DIST_CTRL,
963 .len = 12,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100964 .bits_per_irq = 0,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500965 .handle_mmio = handle_mmio_misc,
966 },
967 {
968 .base = GIC_DIST_IGROUP,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100969 .len = VGIC_MAX_IRQS / 8,
970 .bits_per_irq = 1,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500971 .handle_mmio = handle_mmio_raz_wi,
972 },
973 {
974 .base = GIC_DIST_ENABLE_SET,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100975 .len = VGIC_MAX_IRQS / 8,
976 .bits_per_irq = 1,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500977 .handle_mmio = handle_mmio_set_enable_reg,
978 },
979 {
980 .base = GIC_DIST_ENABLE_CLEAR,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100981 .len = VGIC_MAX_IRQS / 8,
982 .bits_per_irq = 1,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500983 .handle_mmio = handle_mmio_clear_enable_reg,
984 },
985 {
986 .base = GIC_DIST_PENDING_SET,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100987 .len = VGIC_MAX_IRQS / 8,
988 .bits_per_irq = 1,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500989 .handle_mmio = handle_mmio_set_pending_reg,
990 },
991 {
992 .base = GIC_DIST_PENDING_CLEAR,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100993 .len = VGIC_MAX_IRQS / 8,
994 .bits_per_irq = 1,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500995 .handle_mmio = handle_mmio_clear_pending_reg,
996 },
997 {
998 .base = GIC_DIST_ACTIVE_SET,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100999 .len = VGIC_MAX_IRQS / 8,
1000 .bits_per_irq = 1,
Marc Zyngierb47ef922013-01-21 19:36:14 -05001001 .handle_mmio = handle_mmio_raz_wi,
1002 },
1003 {
1004 .base = GIC_DIST_ACTIVE_CLEAR,
Marc Zyngierc3c91832014-07-08 12:09:04 +01001005 .len = VGIC_MAX_IRQS / 8,
1006 .bits_per_irq = 1,
Marc Zyngierb47ef922013-01-21 19:36:14 -05001007 .handle_mmio = handle_mmio_raz_wi,
1008 },
1009 {
1010 .base = GIC_DIST_PRI,
Marc Zyngierc3c91832014-07-08 12:09:04 +01001011 .len = VGIC_MAX_IRQS,
1012 .bits_per_irq = 8,
Marc Zyngierb47ef922013-01-21 19:36:14 -05001013 .handle_mmio = handle_mmio_priority_reg,
1014 },
1015 {
1016 .base = GIC_DIST_TARGET,
Marc Zyngierc3c91832014-07-08 12:09:04 +01001017 .len = VGIC_MAX_IRQS,
1018 .bits_per_irq = 8,
Marc Zyngierb47ef922013-01-21 19:36:14 -05001019 .handle_mmio = handle_mmio_target_reg,
1020 },
1021 {
1022 .base = GIC_DIST_CONFIG,
Marc Zyngierc3c91832014-07-08 12:09:04 +01001023 .len = VGIC_MAX_IRQS / 4,
1024 .bits_per_irq = 2,
Marc Zyngierb47ef922013-01-21 19:36:14 -05001025 .handle_mmio = handle_mmio_cfg_reg,
1026 },
1027 {
1028 .base = GIC_DIST_SOFTINT,
1029 .len = 4,
1030 .handle_mmio = handle_mmio_sgi_reg,
1031 },
Christoffer Dallc07a0192013-10-25 21:17:31 +01001032 {
1033 .base = GIC_DIST_SGI_PENDING_CLEAR,
1034 .len = VGIC_NR_SGIS,
1035 .handle_mmio = handle_mmio_sgi_clear,
1036 },
1037 {
1038 .base = GIC_DIST_SGI_PENDING_SET,
1039 .len = VGIC_NR_SGIS,
1040 .handle_mmio = handle_mmio_sgi_set,
1041 },
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001042 {}
1043};
1044
1045static const
1046struct mmio_range *find_matching_range(const struct mmio_range *ranges,
1047 struct kvm_exit_mmio *mmio,
Christoffer Dall1006e8c2013-09-23 14:55:56 -07001048 phys_addr_t offset)
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001049{
1050 const struct mmio_range *r = ranges;
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001051
1052 while (r->len) {
Christoffer Dall1006e8c2013-09-23 14:55:56 -07001053 if (offset >= r->base &&
1054 (offset + mmio->len) <= (r->base + r->len))
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001055 return r;
1056 r++;
1057 }
1058
1059 return NULL;
1060}
1061
Marc Zyngierc3c91832014-07-08 12:09:04 +01001062static bool vgic_validate_access(const struct vgic_dist *dist,
1063 const struct mmio_range *range,
1064 unsigned long offset)
1065{
1066 int irq;
1067
1068 if (!range->bits_per_irq)
1069 return true; /* Not an irq-based access */
1070
1071 irq = offset * 8 / range->bits_per_irq;
1072 if (irq >= dist->nr_irqs)
1073 return false;
1074
1075 return true;
1076}
1077
Andre Przywara05bc8aa2014-06-05 16:07:50 +02001078/*
1079 * Call the respective handler function for the given range.
1080 * We split up any 64 bit accesses into two consecutive 32 bit
1081 * handler calls and merge the result afterwards.
1082 * We do this in a little endian fashion regardless of the host's
1083 * or guest's endianness, because the GIC is always LE and the rest of
1084 * the code (vgic_reg_access) also puts it in a LE fashion already.
1085 * At this point we have already identified the handle function, so
1086 * range points to that one entry and offset is relative to this.
1087 */
1088static bool call_range_handler(struct kvm_vcpu *vcpu,
1089 struct kvm_exit_mmio *mmio,
1090 unsigned long offset,
1091 const struct mmio_range *range)
1092{
1093 u32 *data32 = (void *)mmio->data;
1094 struct kvm_exit_mmio mmio32;
1095 bool ret;
1096
1097 if (likely(mmio->len <= 4))
1098 return range->handle_mmio(vcpu, mmio, offset);
1099
1100 /*
1101 * Any access bigger than 4 bytes (that we currently handle in KVM)
1102 * is actually 8 bytes long, caused by a 64-bit access
1103 */
1104
1105 mmio32.len = 4;
1106 mmio32.is_write = mmio->is_write;
1107
1108 mmio32.phys_addr = mmio->phys_addr + 4;
1109 if (mmio->is_write)
1110 *(u32 *)mmio32.data = data32[1];
1111 ret = range->handle_mmio(vcpu, &mmio32, offset + 4);
1112 if (!mmio->is_write)
1113 data32[1] = *(u32 *)mmio32.data;
1114
1115 mmio32.phys_addr = mmio->phys_addr;
1116 if (mmio->is_write)
1117 *(u32 *)mmio32.data = data32[0];
1118 ret |= range->handle_mmio(vcpu, &mmio32, offset);
1119 if (!mmio->is_write)
1120 data32[0] = *(u32 *)mmio32.data;
1121
1122 return ret;
1123}
1124
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001125/**
Andre Przywara96415252014-06-02 22:44:37 +02001126 * vgic_handle_mmio_range - handle an in-kernel MMIO access
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001127 * @vcpu: pointer to the vcpu performing the access
1128 * @run: pointer to the kvm_run structure
1129 * @mmio: pointer to the data describing the access
Andre Przywara96415252014-06-02 22:44:37 +02001130 * @ranges: array of MMIO ranges in a given region
1131 * @mmio_base: base address of that region
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001132 *
Andre Przywara96415252014-06-02 22:44:37 +02001133 * returns true if the MMIO access could be performed
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001134 */
Andre Przywara96415252014-06-02 22:44:37 +02001135static bool vgic_handle_mmio_range(struct kvm_vcpu *vcpu, struct kvm_run *run,
1136 struct kvm_exit_mmio *mmio,
1137 const struct mmio_range *ranges,
1138 unsigned long mmio_base)
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001139{
Marc Zyngierb47ef922013-01-21 19:36:14 -05001140 const struct mmio_range *range;
1141 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngierb47ef922013-01-21 19:36:14 -05001142 bool updated_state;
1143 unsigned long offset;
1144
Andre Przywara96415252014-06-02 22:44:37 +02001145 offset = mmio->phys_addr - mmio_base;
1146 range = find_matching_range(ranges, mmio, offset);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001147 if (unlikely(!range || !range->handle_mmio)) {
1148 pr_warn("Unhandled access %d %08llx %d\n",
1149 mmio->is_write, mmio->phys_addr, mmio->len);
1150 return false;
1151 }
1152
1153 spin_lock(&vcpu->kvm->arch.vgic.lock);
Andre Przywara96415252014-06-02 22:44:37 +02001154 offset -= range->base;
Marc Zyngierc3c91832014-07-08 12:09:04 +01001155 if (vgic_validate_access(dist, range, offset)) {
Andre Przywara05bc8aa2014-06-05 16:07:50 +02001156 updated_state = call_range_handler(vcpu, mmio, offset, range);
Marc Zyngierc3c91832014-07-08 12:09:04 +01001157 } else {
Andre Przywara05bc8aa2014-06-05 16:07:50 +02001158 if (!mmio->is_write)
1159 memset(mmio->data, 0, mmio->len);
Marc Zyngierc3c91832014-07-08 12:09:04 +01001160 updated_state = false;
1161 }
Marc Zyngierb47ef922013-01-21 19:36:14 -05001162 spin_unlock(&vcpu->kvm->arch.vgic.lock);
1163 kvm_prepare_mmio(run, mmio);
1164 kvm_handle_mmio_return(vcpu, run);
1165
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001166 if (updated_state)
1167 vgic_kick_vcpus(vcpu->kvm);
1168
Marc Zyngierb47ef922013-01-21 19:36:14 -05001169 return true;
1170}
1171
Andre Przywara96415252014-06-02 22:44:37 +02001172static inline bool is_in_range(phys_addr_t addr, unsigned long len,
1173 phys_addr_t baseaddr, unsigned long size)
1174{
1175 return (addr >= baseaddr) && (addr + len <= baseaddr + size);
1176}
1177
1178static bool vgic_v2_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
1179 struct kvm_exit_mmio *mmio)
1180{
1181 unsigned long base = vcpu->kvm->arch.vgic.vgic_dist_base;
1182
1183 if (!is_in_range(mmio->phys_addr, mmio->len, base,
1184 KVM_VGIC_V2_DIST_SIZE))
1185 return false;
1186
1187 /* GICv2 does not support accesses wider than 32 bits */
1188 if (mmio->len > 4) {
1189 kvm_inject_dabt(vcpu, mmio->phys_addr);
1190 return true;
1191 }
1192
1193 return vgic_handle_mmio_range(vcpu, run, mmio, vgic_dist_ranges, base);
1194}
1195
1196/**
1197 * vgic_handle_mmio - handle an in-kernel MMIO access for the GIC emulation
1198 * @vcpu: pointer to the vcpu performing the access
1199 * @run: pointer to the kvm_run structure
1200 * @mmio: pointer to the data describing the access
1201 *
1202 * returns true if the MMIO access has been performed in kernel space,
1203 * and false if it needs to be emulated in user space.
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001204 * Calls the actual handling routine for the selected VGIC model.
Andre Przywara96415252014-06-02 22:44:37 +02001205 */
1206bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
1207 struct kvm_exit_mmio *mmio)
1208{
1209 if (!irqchip_in_kernel(vcpu->kvm))
1210 return false;
1211
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001212 /*
1213 * This will currently call either vgic_v2_handle_mmio() or
1214 * vgic_v3_handle_mmio(), which in turn will call
1215 * vgic_handle_mmio_range() defined above.
1216 */
1217 return vcpu->kvm->arch.vgic.vm_ops.handle_mmio(vcpu, run, mmio);
Andre Przywara96415252014-06-02 22:44:37 +02001218}
1219
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001220static u8 *vgic_get_sgi_sources(struct vgic_dist *dist, int vcpu_id, int sgi)
1221{
1222 return dist->irq_sgi_sources + vcpu_id * VGIC_NR_SGIS + sgi;
1223}
1224
Marc Zyngierb47ef922013-01-21 19:36:14 -05001225static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg)
1226{
1227 struct kvm *kvm = vcpu->kvm;
1228 struct vgic_dist *dist = &kvm->arch.vgic;
1229 int nrcpus = atomic_read(&kvm->online_vcpus);
1230 u8 target_cpus;
1231 int sgi, mode, c, vcpu_id;
1232
1233 vcpu_id = vcpu->vcpu_id;
1234
1235 sgi = reg & 0xf;
1236 target_cpus = (reg >> 16) & 0xff;
1237 mode = (reg >> 24) & 3;
1238
1239 switch (mode) {
1240 case 0:
1241 if (!target_cpus)
1242 return;
Haibin Wang91021a62014-04-10 13:14:32 +01001243 break;
Marc Zyngierb47ef922013-01-21 19:36:14 -05001244
1245 case 1:
1246 target_cpus = ((1 << nrcpus) - 1) & ~(1 << vcpu_id) & 0xff;
1247 break;
1248
1249 case 2:
1250 target_cpus = 1 << vcpu_id;
1251 break;
1252 }
1253
1254 kvm_for_each_vcpu(c, vcpu, kvm) {
1255 if (target_cpus & 1) {
1256 /* Flag the SGI as pending */
Christoffer Dall227844f2014-06-09 12:27:18 +02001257 vgic_dist_irq_set_pending(vcpu, sgi);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001258 *vgic_get_sgi_sources(dist, c, sgi) |= 1 << vcpu_id;
Marc Zyngierb47ef922013-01-21 19:36:14 -05001259 kvm_debug("SGI%d from CPU%d to CPU%d\n", sgi, vcpu_id, c);
1260 }
1261
1262 target_cpus >>= 1;
1263 }
1264}
1265
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001266static int vgic_nr_shared_irqs(struct vgic_dist *dist)
1267{
1268 return dist->nr_irqs - VGIC_NR_PRIVATE_IRQS;
1269}
1270
Marc Zyngierb47ef922013-01-21 19:36:14 -05001271static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
1272{
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001273 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1274 unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
1275 unsigned long pending_private, pending_shared;
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001276 int nr_shared = vgic_nr_shared_irqs(dist);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001277 int vcpu_id;
1278
1279 vcpu_id = vcpu->vcpu_id;
1280 pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
1281 pend_shared = vcpu->arch.vgic_cpu.pending_shared;
1282
Christoffer Dall227844f2014-06-09 12:27:18 +02001283 pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001284 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
1285 bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
1286
Christoffer Dall227844f2014-06-09 12:27:18 +02001287 pending = vgic_bitmap_get_shared_map(&dist->irq_pending);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001288 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001289 bitmap_and(pend_shared, pending, enabled, nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001290 bitmap_and(pend_shared, pend_shared,
1291 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001292 nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001293
1294 pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001295 pending_shared = find_first_bit(pend_shared, nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001296 return (pending_private < VGIC_NR_PRIVATE_IRQS ||
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001297 pending_shared < vgic_nr_shared_irqs(dist));
Marc Zyngierb47ef922013-01-21 19:36:14 -05001298}
1299
1300/*
1301 * Update the interrupt state and determine which CPUs have pending
1302 * interrupts. Must be called with distributor lock held.
1303 */
1304static void vgic_update_state(struct kvm *kvm)
1305{
1306 struct vgic_dist *dist = &kvm->arch.vgic;
1307 struct kvm_vcpu *vcpu;
1308 int c;
1309
1310 if (!dist->enabled) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001311 set_bit(0, dist->irq_pending_on_cpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001312 return;
1313 }
1314
1315 kvm_for_each_vcpu(c, vcpu, kvm) {
1316 if (compute_pending_for_cpu(vcpu)) {
1317 pr_debug("CPU%d has pending interrupts\n", c);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001318 set_bit(c, dist->irq_pending_on_cpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001319 }
1320 }
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001321}
Christoffer Dall330690c2013-01-21 19:36:13 -05001322
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001323static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr)
1324{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001325 return vgic_ops->get_lr(vcpu, lr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001326}
1327
1328static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr,
1329 struct vgic_lr vlr)
1330{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001331 vgic_ops->set_lr(vcpu, lr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001332}
1333
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001334static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
1335 struct vgic_lr vlr)
1336{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001337 vgic_ops->sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001338}
1339
1340static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu)
1341{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001342 return vgic_ops->get_elrsr(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001343}
1344
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001345static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu)
1346{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001347 return vgic_ops->get_eisr(vcpu);
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001348}
1349
Marc Zyngier495dd852013-06-04 11:02:10 +01001350static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu)
1351{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001352 return vgic_ops->get_interrupt_status(vcpu);
Marc Zyngier495dd852013-06-04 11:02:10 +01001353}
1354
Marc Zyngier909d9b52013-06-04 11:24:17 +01001355static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu)
1356{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001357 vgic_ops->enable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001358}
1359
1360static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu)
1361{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001362 vgic_ops->disable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001363}
1364
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001365static inline void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1366{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001367 vgic_ops->get_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001368}
1369
1370static void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1371{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001372 vgic_ops->set_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001373}
1374
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001375static inline void vgic_enable(struct kvm_vcpu *vcpu)
1376{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001377 vgic_ops->enable(vcpu);
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001378}
1379
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001380static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu)
1381{
1382 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1383 struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr);
1384
1385 vlr.state = 0;
1386 vgic_set_lr(vcpu, lr_nr, vlr);
1387 clear_bit(lr_nr, vgic_cpu->lr_used);
1388 vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
1389}
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001390
1391/*
1392 * An interrupt may have been disabled after being made pending on the
1393 * CPU interface (the classic case is a timer running while we're
1394 * rebooting the guest - the interrupt would kick as soon as the CPU
1395 * interface gets enabled, with deadly consequences).
1396 *
1397 * The solution is to examine already active LRs, and check the
1398 * interrupt is still enabled. If not, just retire it.
1399 */
1400static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
1401{
1402 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1403 int lr;
1404
Marc Zyngier8f186d52014-02-04 18:13:03 +00001405 for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001406 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001407
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001408 if (!vgic_irq_is_enabled(vcpu, vlr.irq)) {
1409 vgic_retire_lr(lr, vlr.irq, vcpu);
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001410 if (vgic_irq_is_queued(vcpu, vlr.irq))
1411 vgic_irq_clear_queued(vcpu, vlr.irq);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001412 }
1413 }
1414}
1415
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001416/*
1417 * Queue an interrupt to a CPU virtual interface. Return true on success,
1418 * or false if it wasn't possible to queue it.
1419 */
1420static bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
1421{
1422 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001423 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001424 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001425 int lr;
1426
1427 /* Sanitize the input... */
1428 BUG_ON(sgi_source_id & ~7);
1429 BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001430 BUG_ON(irq >= dist->nr_irqs);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001431
1432 kvm_debug("Queue IRQ%d\n", irq);
1433
1434 lr = vgic_cpu->vgic_irq_lr_map[irq];
1435
1436 /* Do we have an active interrupt for the same CPUID? */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001437 if (lr != LR_EMPTY) {
1438 vlr = vgic_get_lr(vcpu, lr);
1439 if (vlr.source == sgi_source_id) {
1440 kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq);
1441 BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
1442 vlr.state |= LR_STATE_PENDING;
1443 vgic_set_lr(vcpu, lr, vlr);
1444 return true;
1445 }
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001446 }
1447
1448 /* Try to use another LR for this interrupt */
1449 lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
Marc Zyngier8f186d52014-02-04 18:13:03 +00001450 vgic->nr_lr);
1451 if (lr >= vgic->nr_lr)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001452 return false;
1453
1454 kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001455 vgic_cpu->vgic_irq_lr_map[irq] = lr;
1456 set_bit(lr, vgic_cpu->lr_used);
1457
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001458 vlr.irq = irq;
1459 vlr.source = sgi_source_id;
1460 vlr.state = LR_STATE_PENDING;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001461 if (!vgic_irq_is_edge(vcpu, irq))
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001462 vlr.state |= LR_EOI_INT;
1463
1464 vgic_set_lr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001465
1466 return true;
1467}
1468
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001469static bool vgic_v2_queue_sgi(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001470{
1471 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1472 unsigned long sources;
1473 int vcpu_id = vcpu->vcpu_id;
1474 int c;
1475
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001476 sources = *vgic_get_sgi_sources(dist, vcpu_id, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001477
Marc Zyngierfc675e32014-07-08 12:09:03 +01001478 for_each_set_bit(c, &sources, dist->nr_cpus) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001479 if (vgic_queue_irq(vcpu, c, irq))
1480 clear_bit(c, &sources);
1481 }
1482
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001483 *vgic_get_sgi_sources(dist, vcpu_id, irq) = sources;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001484
1485 /*
1486 * If the sources bitmap has been cleared it means that we
1487 * could queue all the SGIs onto link registers (see the
1488 * clear_bit above), and therefore we are done with them in
1489 * our emulated gic and can get rid of them.
1490 */
1491 if (!sources) {
Christoffer Dall227844f2014-06-09 12:27:18 +02001492 vgic_dist_irq_clear_pending(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001493 vgic_cpu_irq_clear(vcpu, irq);
1494 return true;
1495 }
1496
1497 return false;
1498}
1499
1500static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
1501{
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001502 if (!vgic_can_sample_irq(vcpu, irq))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001503 return true; /* level interrupt, already queued */
1504
1505 if (vgic_queue_irq(vcpu, 0, irq)) {
1506 if (vgic_irq_is_edge(vcpu, irq)) {
Christoffer Dall227844f2014-06-09 12:27:18 +02001507 vgic_dist_irq_clear_pending(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001508 vgic_cpu_irq_clear(vcpu, irq);
1509 } else {
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001510 vgic_irq_set_queued(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001511 }
1512
1513 return true;
1514 }
1515
1516 return false;
1517}
1518
1519/*
1520 * Fill the list registers with pending interrupts before running the
1521 * guest.
1522 */
1523static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1524{
1525 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1526 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1527 int i, vcpu_id;
1528 int overflow = 0;
1529
1530 vcpu_id = vcpu->vcpu_id;
1531
1532 /*
1533 * We may not have any pending interrupt, or the interrupts
1534 * may have been serviced from another vcpu. In all cases,
1535 * move along.
1536 */
1537 if (!kvm_vgic_vcpu_pending_irq(vcpu)) {
1538 pr_debug("CPU%d has no pending interrupt\n", vcpu_id);
1539 goto epilog;
1540 }
1541
1542 /* SGIs */
1543 for_each_set_bit(i, vgic_cpu->pending_percpu, VGIC_NR_SGIS) {
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001544 if (!queue_sgi(vcpu, i))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001545 overflow = 1;
1546 }
1547
1548 /* PPIs */
1549 for_each_set_bit_from(i, vgic_cpu->pending_percpu, VGIC_NR_PRIVATE_IRQS) {
1550 if (!vgic_queue_hwirq(vcpu, i))
1551 overflow = 1;
1552 }
1553
1554 /* SPIs */
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001555 for_each_set_bit(i, vgic_cpu->pending_shared, vgic_nr_shared_irqs(dist)) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001556 if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
1557 overflow = 1;
1558 }
1559
1560epilog:
1561 if (overflow) {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001562 vgic_enable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001563 } else {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001564 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001565 /*
1566 * We're about to run this VCPU, and we've consumed
1567 * everything the distributor had in store for
1568 * us. Claim we don't have anything pending. We'll
1569 * adjust that if needed while exiting.
1570 */
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001571 clear_bit(vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001572 }
1573}
1574
1575static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1576{
Marc Zyngier495dd852013-06-04 11:02:10 +01001577 u32 status = vgic_get_interrupt_status(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001578 bool level_pending = false;
1579
Marc Zyngier495dd852013-06-04 11:02:10 +01001580 kvm_debug("STATUS = %08x\n", status);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001581
Marc Zyngier495dd852013-06-04 11:02:10 +01001582 if (status & INT_STATUS_EOI) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001583 /*
1584 * Some level interrupts have been EOIed. Clear their
1585 * active bit.
1586 */
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001587 u64 eisr = vgic_get_eisr(vcpu);
Christoffer Dall2df36a52014-09-28 16:04:26 +02001588 unsigned long *eisr_ptr = u64_to_bitmask(&eisr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001589 int lr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001590
Marc Zyngier8f186d52014-02-04 18:13:03 +00001591 for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001592 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001593 WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001594
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001595 vgic_irq_clear_queued(vcpu, vlr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001596 WARN_ON(vlr.state & LR_STATE_MASK);
1597 vlr.state = 0;
1598 vgic_set_lr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001599
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001600 /*
1601 * If the IRQ was EOIed it was also ACKed and we we
1602 * therefore assume we can clear the soft pending
1603 * state (should it had been set) for this interrupt.
1604 *
1605 * Note: if the IRQ soft pending state was set after
1606 * the IRQ was acked, it actually shouldn't be
1607 * cleared, but we have no way of knowing that unless
1608 * we start trapping ACKs when the soft-pending state
1609 * is set.
1610 */
1611 vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
1612
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001613 /* Any additional pending interrupt? */
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001614 if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001615 vgic_cpu_irq_set(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001616 level_pending = true;
1617 } else {
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001618 vgic_dist_irq_clear_pending(vcpu, vlr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001619 vgic_cpu_irq_clear(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001620 }
Marc Zyngier75da01e2013-01-31 11:25:52 +00001621
1622 /*
1623 * Despite being EOIed, the LR may not have
1624 * been marked as empty.
1625 */
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001626 vgic_sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001627 }
1628 }
1629
Marc Zyngier495dd852013-06-04 11:02:10 +01001630 if (status & INT_STATUS_UNDERFLOW)
Marc Zyngier909d9b52013-06-04 11:24:17 +01001631 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001632
1633 return level_pending;
1634}
1635
1636/*
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001637 * Sync back the VGIC state after a guest run. The distributor lock is
1638 * needed so we don't get preempted in the middle of the state processing.
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001639 */
1640static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1641{
1642 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1643 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001644 u64 elrsr;
1645 unsigned long *elrsr_ptr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001646 int lr, pending;
1647 bool level_pending;
1648
1649 level_pending = vgic_process_maintenance(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001650 elrsr = vgic_get_elrsr(vcpu);
Christoffer Dall2df36a52014-09-28 16:04:26 +02001651 elrsr_ptr = u64_to_bitmask(&elrsr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001652
1653 /* Clear mappings for empty LRs */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001654 for_each_set_bit(lr, elrsr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001655 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001656
1657 if (!test_and_clear_bit(lr, vgic_cpu->lr_used))
1658 continue;
1659
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001660 vlr = vgic_get_lr(vcpu, lr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001661
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001662 BUG_ON(vlr.irq >= dist->nr_irqs);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001663 vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001664 }
1665
1666 /* Check if we still have something up our sleeve... */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001667 pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr);
1668 if (level_pending || pending < vgic->nr_lr)
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001669 set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001670}
1671
1672void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1673{
1674 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1675
1676 if (!irqchip_in_kernel(vcpu->kvm))
1677 return;
1678
1679 spin_lock(&dist->lock);
1680 __kvm_vgic_flush_hwstate(vcpu);
1681 spin_unlock(&dist->lock);
1682}
1683
1684void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1685{
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001686 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1687
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001688 if (!irqchip_in_kernel(vcpu->kvm))
1689 return;
1690
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001691 spin_lock(&dist->lock);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001692 __kvm_vgic_sync_hwstate(vcpu);
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001693 spin_unlock(&dist->lock);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001694}
1695
1696int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
1697{
1698 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1699
1700 if (!irqchip_in_kernel(vcpu->kvm))
1701 return 0;
1702
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001703 return test_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001704}
1705
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001706static void vgic_kick_vcpus(struct kvm *kvm)
1707{
1708 struct kvm_vcpu *vcpu;
1709 int c;
1710
1711 /*
1712 * We've injected an interrupt, time to find out who deserves
1713 * a good kick...
1714 */
1715 kvm_for_each_vcpu(c, vcpu, kvm) {
1716 if (kvm_vgic_vcpu_pending_irq(vcpu))
1717 kvm_vcpu_kick(vcpu);
1718 }
1719}
1720
1721static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
1722{
Christoffer Dall227844f2014-06-09 12:27:18 +02001723 int edge_triggered = vgic_irq_is_edge(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001724
1725 /*
1726 * Only inject an interrupt if:
1727 * - edge triggered and we have a rising edge
1728 * - level triggered and we change level
1729 */
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001730 if (edge_triggered) {
1731 int state = vgic_dist_irq_is_pending(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001732 return level > state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001733 } else {
1734 int state = vgic_dist_irq_get_level(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001735 return level != state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001736 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001737}
1738
Shannon Zhao016ed392014-11-19 10:11:25 +00001739static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001740 unsigned int irq_num, bool level)
1741{
1742 struct vgic_dist *dist = &kvm->arch.vgic;
1743 struct kvm_vcpu *vcpu;
Christoffer Dall227844f2014-06-09 12:27:18 +02001744 int edge_triggered, level_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001745 int enabled;
1746 bool ret = true;
1747
1748 spin_lock(&dist->lock);
1749
1750 vcpu = kvm_get_vcpu(kvm, cpuid);
Christoffer Dall227844f2014-06-09 12:27:18 +02001751 edge_triggered = vgic_irq_is_edge(vcpu, irq_num);
1752 level_triggered = !edge_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001753
1754 if (!vgic_validate_injection(vcpu, irq_num, level)) {
1755 ret = false;
1756 goto out;
1757 }
1758
1759 if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
1760 cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
1761 vcpu = kvm_get_vcpu(kvm, cpuid);
1762 }
1763
1764 kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
1765
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001766 if (level) {
1767 if (level_triggered)
1768 vgic_dist_irq_set_level(vcpu, irq_num);
Christoffer Dall227844f2014-06-09 12:27:18 +02001769 vgic_dist_irq_set_pending(vcpu, irq_num);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001770 } else {
1771 if (level_triggered) {
1772 vgic_dist_irq_clear_level(vcpu, irq_num);
1773 if (!vgic_dist_irq_soft_pend(vcpu, irq_num))
1774 vgic_dist_irq_clear_pending(vcpu, irq_num);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001775 }
wanghaibin7d39f9e32014-11-17 09:27:37 +00001776
1777 ret = false;
1778 goto out;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001779 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001780
1781 enabled = vgic_irq_is_enabled(vcpu, irq_num);
1782
1783 if (!enabled) {
1784 ret = false;
1785 goto out;
1786 }
1787
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001788 if (!vgic_can_sample_irq(vcpu, irq_num)) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001789 /*
1790 * Level interrupt in progress, will be picked up
1791 * when EOId.
1792 */
1793 ret = false;
1794 goto out;
1795 }
1796
1797 if (level) {
1798 vgic_cpu_irq_set(vcpu, irq_num);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001799 set_bit(cpuid, dist->irq_pending_on_cpu);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001800 }
1801
1802out:
1803 spin_unlock(&dist->lock);
1804
Shannon Zhao016ed392014-11-19 10:11:25 +00001805 return ret ? cpuid : -EINVAL;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001806}
1807
1808/**
1809 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
1810 * @kvm: The VM structure pointer
1811 * @cpuid: The CPU for PPIs
1812 * @irq_num: The IRQ number that is assigned to the device
1813 * @level: Edge-triggered: true: to trigger the interrupt
1814 * false: to ignore the call
1815 * Level-sensitive true: activates an interrupt
1816 * false: deactivates an interrupt
1817 *
1818 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1819 * level-sensitive interrupts. You can think of the level parameter as 1
1820 * being HIGH and 0 being LOW and all devices being active-HIGH.
1821 */
1822int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
1823 bool level)
1824{
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001825 int ret = 0;
Shannon Zhao016ed392014-11-19 10:11:25 +00001826 int vcpu_id;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001827
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001828 if (unlikely(!vgic_initialized(kvm))) {
Andre Przywara598921362014-06-03 09:33:10 +02001829 /*
1830 * We only provide the automatic initialization of the VGIC
1831 * for the legacy case of a GICv2. Any other type must
1832 * be explicitly initialized once setup with the respective
1833 * KVM device call.
1834 */
1835 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2) {
1836 ret = -EBUSY;
1837 goto out;
1838 }
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001839 mutex_lock(&kvm->lock);
1840 ret = vgic_init(kvm);
1841 mutex_unlock(&kvm->lock);
1842
1843 if (ret)
1844 goto out;
Shannon Zhao016ed392014-11-19 10:11:25 +00001845 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001846
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001847 vcpu_id = vgic_update_irq_pending(kvm, cpuid, irq_num, level);
1848 if (vcpu_id >= 0) {
1849 /* kick the specified vcpu */
1850 kvm_vcpu_kick(kvm_get_vcpu(kvm, vcpu_id));
1851 }
1852
1853out:
1854 return ret;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001855}
1856
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001857static irqreturn_t vgic_maintenance_handler(int irq, void *data)
1858{
1859 /*
1860 * We cannot rely on the vgic maintenance interrupt to be
1861 * delivered synchronously. This means we can only use it to
1862 * exit the VM, and we perform the handling of EOIed
1863 * interrupts on the exit path (see vgic_process_maintenance).
1864 */
1865 return IRQ_HANDLED;
1866}
1867
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001868void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
1869{
1870 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1871
1872 kfree(vgic_cpu->pending_shared);
1873 kfree(vgic_cpu->vgic_irq_lr_map);
1874 vgic_cpu->pending_shared = NULL;
1875 vgic_cpu->vgic_irq_lr_map = NULL;
1876}
1877
1878static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
1879{
1880 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1881
1882 int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8;
1883 vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL);
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001884 vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001885
1886 if (!vgic_cpu->pending_shared || !vgic_cpu->vgic_irq_lr_map) {
1887 kvm_vgic_vcpu_destroy(vcpu);
1888 return -ENOMEM;
1889 }
1890
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001891 memset(vgic_cpu->vgic_irq_lr_map, LR_EMPTY, nr_irqs);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001892
1893 /*
Marc Zyngierca85f622013-06-18 19:17:28 +01001894 * Store the number of LRs per vcpu, so we don't have to go
1895 * all the way to the distributor structure to find out. Only
1896 * assembly code should use this one.
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001897 */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001898 vgic_cpu->nr_lr = vgic->nr_lr;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001899
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001900 return 0;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001901}
1902
Andre Przywara3caa2d82014-06-02 16:26:01 +02001903/**
1904 * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW
1905 *
1906 * The host's GIC naturally limits the maximum amount of VCPUs a guest
1907 * can use.
1908 */
1909int kvm_vgic_get_max_vcpus(void)
1910{
1911 return vgic->max_gic_vcpus;
1912}
1913
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001914void kvm_vgic_destroy(struct kvm *kvm)
1915{
1916 struct vgic_dist *dist = &kvm->arch.vgic;
1917 struct kvm_vcpu *vcpu;
1918 int i;
1919
1920 kvm_for_each_vcpu(i, vcpu, kvm)
1921 kvm_vgic_vcpu_destroy(vcpu);
1922
1923 vgic_free_bitmap(&dist->irq_enabled);
1924 vgic_free_bitmap(&dist->irq_level);
1925 vgic_free_bitmap(&dist->irq_pending);
1926 vgic_free_bitmap(&dist->irq_soft_pend);
1927 vgic_free_bitmap(&dist->irq_queued);
1928 vgic_free_bitmap(&dist->irq_cfg);
1929 vgic_free_bytemap(&dist->irq_priority);
1930 if (dist->irq_spi_target) {
1931 for (i = 0; i < dist->nr_cpus; i++)
1932 vgic_free_bitmap(&dist->irq_spi_target[i]);
1933 }
1934 kfree(dist->irq_sgi_sources);
1935 kfree(dist->irq_spi_cpu);
1936 kfree(dist->irq_spi_target);
1937 kfree(dist->irq_pending_on_cpu);
1938 dist->irq_sgi_sources = NULL;
1939 dist->irq_spi_cpu = NULL;
1940 dist->irq_spi_target = NULL;
1941 dist->irq_pending_on_cpu = NULL;
Christoffer Dall1f57be22014-12-09 14:30:36 +01001942 dist->nr_cpus = 0;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001943}
1944
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001945static int vgic_v2_init_model(struct kvm *kvm)
1946{
1947 int i;
1948
1949 for (i = VGIC_NR_PRIVATE_IRQS; i < kvm->arch.vgic.nr_irqs; i += 4)
1950 vgic_set_target_reg(kvm, 0, i);
1951
1952 return 0;
1953}
1954
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001955/*
1956 * Allocate and initialize the various data structures. Must be called
1957 * with kvm->lock held!
1958 */
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001959static int vgic_init(struct kvm *kvm)
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001960{
1961 struct vgic_dist *dist = &kvm->arch.vgic;
1962 struct kvm_vcpu *vcpu;
1963 int nr_cpus, nr_irqs;
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001964 int ret, i, vcpu_id;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001965
Christoffer Dall1f57be22014-12-09 14:30:36 +01001966 if (vgic_initialized(kvm))
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001967 return 0;
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001968
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001969 nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus);
1970 if (!nr_cpus) /* No vcpus? Can't be good... */
Eric Auger66b030e2014-12-15 18:43:32 +01001971 return -ENODEV;
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001972
1973 /*
1974 * If nobody configured the number of interrupts, use the
1975 * legacy one.
1976 */
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001977 if (!dist->nr_irqs)
1978 dist->nr_irqs = VGIC_NR_IRQS_LEGACY;
1979
1980 nr_irqs = dist->nr_irqs;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001981
1982 ret = vgic_init_bitmap(&dist->irq_enabled, nr_cpus, nr_irqs);
1983 ret |= vgic_init_bitmap(&dist->irq_level, nr_cpus, nr_irqs);
1984 ret |= vgic_init_bitmap(&dist->irq_pending, nr_cpus, nr_irqs);
1985 ret |= vgic_init_bitmap(&dist->irq_soft_pend, nr_cpus, nr_irqs);
1986 ret |= vgic_init_bitmap(&dist->irq_queued, nr_cpus, nr_irqs);
1987 ret |= vgic_init_bitmap(&dist->irq_cfg, nr_cpus, nr_irqs);
1988 ret |= vgic_init_bytemap(&dist->irq_priority, nr_cpus, nr_irqs);
1989
1990 if (ret)
1991 goto out;
1992
1993 dist->irq_sgi_sources = kzalloc(nr_cpus * VGIC_NR_SGIS, GFP_KERNEL);
1994 dist->irq_spi_cpu = kzalloc(nr_irqs - VGIC_NR_PRIVATE_IRQS, GFP_KERNEL);
1995 dist->irq_spi_target = kzalloc(sizeof(*dist->irq_spi_target) * nr_cpus,
1996 GFP_KERNEL);
1997 dist->irq_pending_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
1998 GFP_KERNEL);
1999 if (!dist->irq_sgi_sources ||
2000 !dist->irq_spi_cpu ||
2001 !dist->irq_spi_target ||
2002 !dist->irq_pending_on_cpu) {
2003 ret = -ENOMEM;
2004 goto out;
2005 }
2006
2007 for (i = 0; i < nr_cpus; i++)
2008 ret |= vgic_init_bitmap(&dist->irq_spi_target[i],
2009 nr_cpus, nr_irqs);
2010
2011 if (ret)
2012 goto out;
2013
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002014 ret = kvm->arch.vgic.vm_ops.init_model(kvm);
2015 if (ret)
2016 goto out;
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00002017
2018 kvm_for_each_vcpu(vcpu_id, vcpu, kvm) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002019 ret = vgic_vcpu_init_maps(vcpu, nr_irqs);
2020 if (ret) {
2021 kvm_err("VGIC: Failed to allocate vcpu memory\n");
2022 break;
2023 }
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002024
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00002025 for (i = 0; i < dist->nr_irqs; i++) {
2026 if (i < VGIC_NR_PPIS)
2027 vgic_bitmap_set_irq_val(&dist->irq_enabled,
2028 vcpu->vcpu_id, i, 1);
2029 if (i < VGIC_NR_PRIVATE_IRQS)
2030 vgic_bitmap_set_irq_val(&dist->irq_cfg,
2031 vcpu->vcpu_id, i,
2032 VGIC_CFG_EDGE);
2033 }
2034
2035 vgic_enable(vcpu);
2036 }
Marc Zyngier4956f2b2014-07-08 12:09:06 +01002037
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002038out:
2039 if (ret)
2040 kvm_vgic_destroy(kvm);
2041
2042 return ret;
2043}
2044
Christoffer Dalle1ba0202013-09-23 14:55:55 -07002045/**
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00002046 * kvm_vgic_map_resources - Configure global VGIC state before running any VCPUs
Christoffer Dalle1ba0202013-09-23 14:55:55 -07002047 * @kvm: pointer to the kvm struct
2048 *
2049 * Map the virtual CPU interface into the VM before running any VCPUs. We
2050 * can't do this at creation time, because user space must first set the
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00002051 * virtual CPU interface address in the guest physical address space.
Christoffer Dalle1ba0202013-09-23 14:55:55 -07002052 */
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002053static int vgic_v2_map_resources(struct kvm *kvm,
2054 const struct vgic_params *params)
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002055{
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00002056 int ret = 0;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002057
Christoffer Dalle1ba0202013-09-23 14:55:55 -07002058 if (!irqchip_in_kernel(kvm))
2059 return 0;
2060
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002061 mutex_lock(&kvm->lock);
2062
Christoffer Dallc52edf52014-12-09 14:28:09 +01002063 if (vgic_ready(kvm))
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002064 goto out;
2065
2066 if (IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_dist_base) ||
2067 IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_cpu_base)) {
2068 kvm_err("Need to set vgic cpu and dist addresses first\n");
2069 ret = -ENXIO;
2070 goto out;
2071 }
2072
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00002073 /*
2074 * Initialize the vgic if this hasn't already been done on demand by
2075 * accessing the vgic state from userspace.
2076 */
2077 ret = vgic_init(kvm);
Marc Zyngier4956f2b2014-07-08 12:09:06 +01002078 if (ret) {
2079 kvm_err("Unable to allocate maps\n");
2080 goto out;
2081 }
2082
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002083 ret = kvm_phys_addr_ioremap(kvm, kvm->arch.vgic.vgic_cpu_base,
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002084 params->vcpu_base, KVM_VGIC_V2_CPU_SIZE,
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -07002085 true);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002086 if (ret) {
2087 kvm_err("Unable to remap VGIC CPU to VCPU\n");
2088 goto out;
2089 }
2090
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002091 kvm->arch.vgic.ready = true;
2092out:
Marc Zyngier4956f2b2014-07-08 12:09:06 +01002093 if (ret)
2094 kvm_vgic_destroy(kvm);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002095 mutex_unlock(&kvm->lock);
2096 return ret;
2097}
2098
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002099static void vgic_v2_init_emulation(struct kvm *kvm)
2100{
2101 struct vgic_dist *dist = &kvm->arch.vgic;
2102
2103 dist->vm_ops.handle_mmio = vgic_v2_handle_mmio;
2104 dist->vm_ops.queue_sgi = vgic_v2_queue_sgi;
2105 dist->vm_ops.add_sgi_source = vgic_v2_add_sgi_source;
2106 dist->vm_ops.init_model = vgic_v2_init_model;
2107 dist->vm_ops.map_resources = vgic_v2_map_resources;
Andre Przywara3caa2d82014-06-02 16:26:01 +02002108
2109 kvm->arch.max_vcpus = VGIC_V2_MAX_CPUS;
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002110}
2111
2112static int init_vgic_model(struct kvm *kvm, int type)
2113{
2114 switch (type) {
2115 case KVM_DEV_TYPE_ARM_VGIC_V2:
2116 vgic_v2_init_emulation(kvm);
2117 break;
2118 default:
2119 return -ENODEV;
2120 }
2121
Andre Przywara3caa2d82014-06-02 16:26:01 +02002122 if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus)
2123 return -E2BIG;
2124
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002125 return 0;
2126}
2127
Andre Przywara598921362014-06-03 09:33:10 +02002128int kvm_vgic_create(struct kvm *kvm, u32 type)
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002129{
Christoffer Dall6b50f542014-11-06 11:47:39 +00002130 int i, vcpu_lock_idx = -1, ret;
Christoffer Dall73306722013-10-25 17:29:18 +01002131 struct kvm_vcpu *vcpu;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002132
2133 mutex_lock(&kvm->lock);
2134
Andre Przywara4ce7ebd2014-10-26 23:18:14 +00002135 if (irqchip_in_kernel(kvm)) {
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002136 ret = -EEXIST;
2137 goto out;
2138 }
2139
Christoffer Dall73306722013-10-25 17:29:18 +01002140 /*
2141 * Any time a vcpu is run, vcpu_load is called which tries to grab the
2142 * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
2143 * that no other VCPUs are run while we create the vgic.
2144 */
Christoffer Dall6b50f542014-11-06 11:47:39 +00002145 ret = -EBUSY;
Christoffer Dall73306722013-10-25 17:29:18 +01002146 kvm_for_each_vcpu(i, vcpu, kvm) {
2147 if (!mutex_trylock(&vcpu->mutex))
2148 goto out_unlock;
2149 vcpu_lock_idx = i;
2150 }
2151
2152 kvm_for_each_vcpu(i, vcpu, kvm) {
Christoffer Dall6b50f542014-11-06 11:47:39 +00002153 if (vcpu->arch.has_run_once)
Christoffer Dall73306722013-10-25 17:29:18 +01002154 goto out_unlock;
Christoffer Dall73306722013-10-25 17:29:18 +01002155 }
Christoffer Dall6b50f542014-11-06 11:47:39 +00002156 ret = 0;
Christoffer Dall73306722013-10-25 17:29:18 +01002157
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002158 ret = init_vgic_model(kvm, type);
2159 if (ret)
2160 goto out_unlock;
2161
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002162 spin_lock_init(&kvm->arch.vgic.lock);
Marc Zyngierf982cf42014-05-15 10:03:25 +01002163 kvm->arch.vgic.in_kernel = true;
Andre Przywara598921362014-06-03 09:33:10 +02002164 kvm->arch.vgic.vgic_model = type;
Marc Zyngier8f186d52014-02-04 18:13:03 +00002165 kvm->arch.vgic.vctrl_base = vgic->vctrl_base;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002166 kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
2167 kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
2168
Christoffer Dall73306722013-10-25 17:29:18 +01002169out_unlock:
2170 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
2171 vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
2172 mutex_unlock(&vcpu->mutex);
2173 }
2174
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002175out:
2176 mutex_unlock(&kvm->lock);
2177 return ret;
2178}
2179
Will Deacon1fa451b2014-08-26 15:13:24 +01002180static int vgic_ioaddr_overlap(struct kvm *kvm)
Christoffer Dall330690c2013-01-21 19:36:13 -05002181{
2182 phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
2183 phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
2184
2185 if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
2186 return 0;
2187 if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
2188 (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
2189 return -EBUSY;
2190 return 0;
2191}
2192
2193static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
2194 phys_addr_t addr, phys_addr_t size)
2195{
2196 int ret;
2197
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002198 if (addr & ~KVM_PHYS_MASK)
2199 return -E2BIG;
2200
2201 if (addr & (SZ_4K - 1))
2202 return -EINVAL;
2203
Christoffer Dall330690c2013-01-21 19:36:13 -05002204 if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
2205 return -EEXIST;
2206 if (addr + size < addr)
2207 return -EINVAL;
2208
Haibin Wang30c21172014-04-29 14:49:17 +08002209 *ioaddr = addr;
Christoffer Dall330690c2013-01-21 19:36:13 -05002210 ret = vgic_ioaddr_overlap(kvm);
2211 if (ret)
Haibin Wang30c21172014-04-29 14:49:17 +08002212 *ioaddr = VGIC_ADDR_UNDEF;
2213
Christoffer Dall330690c2013-01-21 19:36:13 -05002214 return ret;
2215}
2216
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002217/**
2218 * kvm_vgic_addr - set or get vgic VM base addresses
2219 * @kvm: pointer to the vm struct
2220 * @type: the VGIC addr type, one of KVM_VGIC_V2_ADDR_TYPE_XXX
2221 * @addr: pointer to address value
2222 * @write: if true set the address in the VM address space, if false read the
2223 * address
2224 *
2225 * Set or get the vgic base addresses for the distributor and the virtual CPU
2226 * interface in the VM physical address space. These addresses are properties
2227 * of the emulated core/SoC and therefore user space initially knows this
2228 * information.
2229 */
2230int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
Christoffer Dall330690c2013-01-21 19:36:13 -05002231{
2232 int r = 0;
2233 struct vgic_dist *vgic = &kvm->arch.vgic;
2234
Christoffer Dall330690c2013-01-21 19:36:13 -05002235 mutex_lock(&kvm->lock);
2236 switch (type) {
2237 case KVM_VGIC_V2_ADDR_TYPE_DIST:
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002238 if (write) {
2239 r = vgic_ioaddr_assign(kvm, &vgic->vgic_dist_base,
2240 *addr, KVM_VGIC_V2_DIST_SIZE);
2241 } else {
2242 *addr = vgic->vgic_dist_base;
2243 }
Christoffer Dall330690c2013-01-21 19:36:13 -05002244 break;
2245 case KVM_VGIC_V2_ADDR_TYPE_CPU:
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002246 if (write) {
2247 r = vgic_ioaddr_assign(kvm, &vgic->vgic_cpu_base,
2248 *addr, KVM_VGIC_V2_CPU_SIZE);
2249 } else {
2250 *addr = vgic->vgic_cpu_base;
2251 }
Christoffer Dall330690c2013-01-21 19:36:13 -05002252 break;
2253 default:
2254 r = -ENODEV;
2255 }
2256
2257 mutex_unlock(&kvm->lock);
2258 return r;
2259}
Christoffer Dall73306722013-10-25 17:29:18 +01002260
Christoffer Dallc07a0192013-10-25 21:17:31 +01002261static bool handle_cpu_mmio_misc(struct kvm_vcpu *vcpu,
2262 struct kvm_exit_mmio *mmio, phys_addr_t offset)
2263{
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002264 bool updated = false;
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002265 struct vgic_vmcr vmcr;
2266 u32 *vmcr_field;
2267 u32 reg;
2268
2269 vgic_get_vmcr(vcpu, &vmcr);
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002270
2271 switch (offset & ~0x3) {
2272 case GIC_CPU_CTRL:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002273 vmcr_field = &vmcr.ctlr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002274 break;
2275 case GIC_CPU_PRIMASK:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002276 vmcr_field = &vmcr.pmr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002277 break;
2278 case GIC_CPU_BINPOINT:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002279 vmcr_field = &vmcr.bpr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002280 break;
2281 case GIC_CPU_ALIAS_BINPOINT:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002282 vmcr_field = &vmcr.abpr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002283 break;
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002284 default:
2285 BUG();
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002286 }
2287
2288 if (!mmio->is_write) {
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002289 reg = *vmcr_field;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002290 mmio_data_write(mmio, ~0, reg);
2291 } else {
2292 reg = mmio_data_read(mmio, ~0);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002293 if (reg != *vmcr_field) {
2294 *vmcr_field = reg;
2295 vgic_set_vmcr(vcpu, &vmcr);
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002296 updated = true;
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002297 }
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002298 }
2299 return updated;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002300}
2301
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002302static bool handle_mmio_abpr(struct kvm_vcpu *vcpu,
2303 struct kvm_exit_mmio *mmio, phys_addr_t offset)
2304{
2305 return handle_cpu_mmio_misc(vcpu, mmio, GIC_CPU_ALIAS_BINPOINT);
2306}
2307
2308static bool handle_cpu_mmio_ident(struct kvm_vcpu *vcpu,
2309 struct kvm_exit_mmio *mmio,
2310 phys_addr_t offset)
2311{
2312 u32 reg;
2313
2314 if (mmio->is_write)
2315 return false;
2316
2317 /* GICC_IIDR */
2318 reg = (PRODUCT_ID_KVM << 20) |
2319 (GICC_ARCH_VERSION_V2 << 16) |
2320 (IMPLEMENTER_ARM << 0);
2321 mmio_data_write(mmio, ~0, reg);
2322 return false;
2323}
2324
2325/*
2326 * CPU Interface Register accesses - these are not accessed by the VM, but by
2327 * user space for saving and restoring VGIC state.
2328 */
Christoffer Dallc07a0192013-10-25 21:17:31 +01002329static const struct mmio_range vgic_cpu_ranges[] = {
2330 {
2331 .base = GIC_CPU_CTRL,
2332 .len = 12,
2333 .handle_mmio = handle_cpu_mmio_misc,
2334 },
2335 {
2336 .base = GIC_CPU_ALIAS_BINPOINT,
2337 .len = 4,
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002338 .handle_mmio = handle_mmio_abpr,
Christoffer Dallc07a0192013-10-25 21:17:31 +01002339 },
2340 {
2341 .base = GIC_CPU_ACTIVEPRIO,
2342 .len = 16,
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002343 .handle_mmio = handle_mmio_raz_wi,
Christoffer Dallc07a0192013-10-25 21:17:31 +01002344 },
2345 {
2346 .base = GIC_CPU_IDENT,
2347 .len = 4,
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002348 .handle_mmio = handle_cpu_mmio_ident,
Christoffer Dallc07a0192013-10-25 21:17:31 +01002349 },
2350};
2351
2352static int vgic_attr_regs_access(struct kvm_device *dev,
2353 struct kvm_device_attr *attr,
2354 u32 *reg, bool is_write)
2355{
2356 const struct mmio_range *r = NULL, *ranges;
2357 phys_addr_t offset;
2358 int ret, cpuid, c;
2359 struct kvm_vcpu *vcpu, *tmp_vcpu;
2360 struct vgic_dist *vgic;
2361 struct kvm_exit_mmio mmio;
2362
2363 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2364 cpuid = (attr->attr & KVM_DEV_ARM_VGIC_CPUID_MASK) >>
2365 KVM_DEV_ARM_VGIC_CPUID_SHIFT;
2366
2367 mutex_lock(&dev->kvm->lock);
2368
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00002369 ret = vgic_init(dev->kvm);
Marc Zyngier4956f2b2014-07-08 12:09:06 +01002370 if (ret)
2371 goto out;
2372
Christoffer Dallc07a0192013-10-25 21:17:31 +01002373 if (cpuid >= atomic_read(&dev->kvm->online_vcpus)) {
2374 ret = -EINVAL;
2375 goto out;
2376 }
2377
2378 vcpu = kvm_get_vcpu(dev->kvm, cpuid);
2379 vgic = &dev->kvm->arch.vgic;
2380
2381 mmio.len = 4;
2382 mmio.is_write = is_write;
2383 if (is_write)
2384 mmio_data_write(&mmio, ~0, *reg);
2385 switch (attr->group) {
2386 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2387 mmio.phys_addr = vgic->vgic_dist_base + offset;
2388 ranges = vgic_dist_ranges;
2389 break;
2390 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
2391 mmio.phys_addr = vgic->vgic_cpu_base + offset;
2392 ranges = vgic_cpu_ranges;
2393 break;
2394 default:
2395 BUG();
2396 }
2397 r = find_matching_range(ranges, &mmio, offset);
2398
2399 if (unlikely(!r || !r->handle_mmio)) {
2400 ret = -ENXIO;
2401 goto out;
2402 }
2403
2404
2405 spin_lock(&vgic->lock);
2406
2407 /*
2408 * Ensure that no other VCPU is running by checking the vcpu->cpu
2409 * field. If no other VPCUs are running we can safely access the VGIC
2410 * state, because even if another VPU is run after this point, that
2411 * VCPU will not touch the vgic state, because it will block on
2412 * getting the vgic->lock in kvm_vgic_sync_hwstate().
2413 */
2414 kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm) {
2415 if (unlikely(tmp_vcpu->cpu != -1)) {
2416 ret = -EBUSY;
2417 goto out_vgic_unlock;
2418 }
2419 }
2420
Christoffer Dallcbd333a2013-11-15 20:51:31 -08002421 /*
2422 * Move all pending IRQs from the LRs on all VCPUs so the pending
2423 * state can be properly represented in the register state accessible
2424 * through this API.
2425 */
2426 kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm)
2427 vgic_unqueue_irqs(tmp_vcpu);
2428
Christoffer Dallc07a0192013-10-25 21:17:31 +01002429 offset -= r->base;
2430 r->handle_mmio(vcpu, &mmio, offset);
2431
2432 if (!is_write)
2433 *reg = mmio_data_read(&mmio, ~0);
2434
2435 ret = 0;
2436out_vgic_unlock:
2437 spin_unlock(&vgic->lock);
2438out:
2439 mutex_unlock(&dev->kvm->lock);
2440 return ret;
2441}
2442
Andre Przywarab60da142014-08-21 11:08:27 +01002443static int vgic_set_common_attr(struct kvm_device *dev,
2444 struct kvm_device_attr *attr)
Christoffer Dall73306722013-10-25 17:29:18 +01002445{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002446 int r;
2447
2448 switch (attr->group) {
2449 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2450 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2451 u64 addr;
2452 unsigned long type = (unsigned long)attr->attr;
2453
2454 if (copy_from_user(&addr, uaddr, sizeof(addr)))
2455 return -EFAULT;
2456
2457 r = kvm_vgic_addr(dev->kvm, type, &addr, true);
2458 return (r == -ENODEV) ? -ENXIO : r;
2459 }
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002460 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2461 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2462 u32 val;
2463 int ret = 0;
2464
2465 if (get_user(val, uaddr))
2466 return -EFAULT;
2467
2468 /*
2469 * We require:
2470 * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs
2471 * - at most 1024 interrupts
2472 * - a multiple of 32 interrupts
2473 */
2474 if (val < (VGIC_NR_PRIVATE_IRQS + 32) ||
2475 val > VGIC_MAX_IRQS ||
2476 (val & 31))
2477 return -EINVAL;
2478
2479 mutex_lock(&dev->kvm->lock);
2480
Christoffer Dallc52edf52014-12-09 14:28:09 +01002481 if (vgic_ready(dev->kvm) || dev->kvm->arch.vgic.nr_irqs)
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002482 ret = -EBUSY;
2483 else
2484 dev->kvm->arch.vgic.nr_irqs = val;
2485
2486 mutex_unlock(&dev->kvm->lock);
2487
2488 return ret;
2489 }
Eric Auger065c0032014-12-15 18:43:33 +01002490 case KVM_DEV_ARM_VGIC_GRP_CTRL: {
2491 switch (attr->attr) {
2492 case KVM_DEV_ARM_VGIC_CTRL_INIT:
2493 r = vgic_init(dev->kvm);
2494 return r;
2495 }
2496 break;
2497 }
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002498 }
2499
Christoffer Dall73306722013-10-25 17:29:18 +01002500 return -ENXIO;
2501}
2502
Andre Przywarab60da142014-08-21 11:08:27 +01002503static int vgic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2504{
2505 int ret;
2506
2507 ret = vgic_set_common_attr(dev, attr);
2508 if (ret != -ENXIO)
2509 return ret;
2510
2511 switch (attr->group) {
2512 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2513 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
2514 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2515 u32 reg;
2516
2517 if (get_user(reg, uaddr))
2518 return -EFAULT;
2519
2520 return vgic_attr_regs_access(dev, attr, &reg, true);
2521 }
2522
2523 }
2524
2525 return -ENXIO;
2526}
2527
2528static int vgic_get_common_attr(struct kvm_device *dev,
2529 struct kvm_device_attr *attr)
Christoffer Dall73306722013-10-25 17:29:18 +01002530{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002531 int r = -ENXIO;
2532
2533 switch (attr->group) {
2534 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2535 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2536 u64 addr;
2537 unsigned long type = (unsigned long)attr->attr;
2538
2539 r = kvm_vgic_addr(dev->kvm, type, &addr, false);
2540 if (r)
2541 return (r == -ENODEV) ? -ENXIO : r;
2542
2543 if (copy_to_user(uaddr, &addr, sizeof(addr)))
2544 return -EFAULT;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002545 break;
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002546 }
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002547 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2548 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
Andre Przywarab60da142014-08-21 11:08:27 +01002549
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002550 r = put_user(dev->kvm->arch.vgic.nr_irqs, uaddr);
2551 break;
2552 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01002553
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002554 }
2555
2556 return r;
Christoffer Dall73306722013-10-25 17:29:18 +01002557}
2558
Andre Przywarab60da142014-08-21 11:08:27 +01002559static int vgic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2560{
2561 int ret;
2562
2563 ret = vgic_get_common_attr(dev, attr);
2564 if (ret != -ENXIO)
2565 return ret;
2566
2567 switch (attr->group) {
2568 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2569 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
2570 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2571 u32 reg = 0;
2572
2573 ret = vgic_attr_regs_access(dev, attr, &reg, false);
2574 if (ret)
2575 return ret;
2576 return put_user(reg, uaddr);
2577 }
2578
2579 }
2580
2581 return -ENXIO;
2582}
2583
Christoffer Dallc07a0192013-10-25 21:17:31 +01002584static int vgic_has_attr_regs(const struct mmio_range *ranges,
2585 phys_addr_t offset)
2586{
2587 struct kvm_exit_mmio dev_attr_mmio;
2588
2589 dev_attr_mmio.len = 4;
2590 if (find_matching_range(ranges, &dev_attr_mmio, offset))
2591 return 0;
2592 else
2593 return -ENXIO;
2594}
2595
Christoffer Dall73306722013-10-25 17:29:18 +01002596static int vgic_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2597{
Christoffer Dallc07a0192013-10-25 21:17:31 +01002598 phys_addr_t offset;
2599
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002600 switch (attr->group) {
2601 case KVM_DEV_ARM_VGIC_GRP_ADDR:
2602 switch (attr->attr) {
2603 case KVM_VGIC_V2_ADDR_TYPE_DIST:
2604 case KVM_VGIC_V2_ADDR_TYPE_CPU:
2605 return 0;
2606 }
2607 break;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002608 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2609 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2610 return vgic_has_attr_regs(vgic_dist_ranges, offset);
2611 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
2612 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2613 return vgic_has_attr_regs(vgic_cpu_ranges, offset);
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002614 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS:
2615 return 0;
Eric Auger065c0032014-12-15 18:43:33 +01002616 case KVM_DEV_ARM_VGIC_GRP_CTRL:
2617 switch (attr->attr) {
2618 case KVM_DEV_ARM_VGIC_CTRL_INIT:
2619 return 0;
2620 }
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002621 }
Christoffer Dall73306722013-10-25 17:29:18 +01002622 return -ENXIO;
2623}
2624
2625static void vgic_destroy(struct kvm_device *dev)
2626{
2627 kfree(dev);
2628}
2629
2630static int vgic_create(struct kvm_device *dev, u32 type)
2631{
Andre Przywara598921362014-06-03 09:33:10 +02002632 return kvm_vgic_create(dev->kvm, type);
Christoffer Dall73306722013-10-25 17:29:18 +01002633}
2634
Andre Przywaraea2f83a2014-10-26 23:17:00 +00002635struct kvm_device_ops kvm_arm_vgic_v2_ops = {
Christoffer Dall73306722013-10-25 17:29:18 +01002636 .name = "kvm-arm-vgic",
2637 .create = vgic_create,
2638 .destroy = vgic_destroy,
2639 .set_attr = vgic_set_attr,
2640 .get_attr = vgic_get_attr,
2641 .has_attr = vgic_has_attr,
2642};
Will Deaconc06a8412014-09-02 10:27:34 +01002643
2644static void vgic_init_maintenance_interrupt(void *info)
2645{
2646 enable_percpu_irq(vgic->maint_irq, 0);
2647}
2648
2649static int vgic_cpu_notify(struct notifier_block *self,
2650 unsigned long action, void *cpu)
2651{
2652 switch (action) {
2653 case CPU_STARTING:
2654 case CPU_STARTING_FROZEN:
2655 vgic_init_maintenance_interrupt(NULL);
2656 break;
2657 case CPU_DYING:
2658 case CPU_DYING_FROZEN:
2659 disable_percpu_irq(vgic->maint_irq);
2660 break;
2661 }
2662
2663 return NOTIFY_OK;
2664}
2665
2666static struct notifier_block vgic_cpu_nb = {
2667 .notifier_call = vgic_cpu_notify,
2668};
2669
2670static const struct of_device_id vgic_ids[] = {
2671 { .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, },
2672 { .compatible = "arm,gic-v3", .data = vgic_v3_probe, },
2673 {},
2674};
2675
2676int kvm_vgic_hyp_init(void)
2677{
2678 const struct of_device_id *matched_id;
Christoffer Dalla875daf2014-09-18 18:15:32 -07002679 const int (*vgic_probe)(struct device_node *,const struct vgic_ops **,
2680 const struct vgic_params **);
Will Deaconc06a8412014-09-02 10:27:34 +01002681 struct device_node *vgic_node;
2682 int ret;
2683
2684 vgic_node = of_find_matching_node_and_match(NULL,
2685 vgic_ids, &matched_id);
2686 if (!vgic_node) {
2687 kvm_err("error: no compatible GIC node found\n");
2688 return -ENODEV;
2689 }
2690
2691 vgic_probe = matched_id->data;
2692 ret = vgic_probe(vgic_node, &vgic_ops, &vgic);
2693 if (ret)
2694 return ret;
2695
2696 ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler,
2697 "vgic", kvm_get_running_vcpus());
2698 if (ret) {
2699 kvm_err("Cannot register interrupt %d\n", vgic->maint_irq);
2700 return ret;
2701 }
2702
2703 ret = __register_cpu_notifier(&vgic_cpu_nb);
2704 if (ret) {
2705 kvm_err("Cannot register vgic CPU notifier\n");
2706 goto out_free_irq;
2707 }
2708
2709 /* Callback into for arch code for setup */
2710 vgic_arch_setup(vgic);
2711
2712 on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
2713
Andre Przywaraea2f83a2014-10-26 23:17:00 +00002714 return 0;
Will Deaconc06a8412014-09-02 10:27:34 +01002715
2716out_free_irq:
2717 free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus());
2718 return ret;
2719}