blob: b5aa821b89a85ecd1f3e678fc60f1de13e51f0d5 [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
Victor Kamensky9662fb42014-06-12 09:30:10 -0700109/*
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100110 * struct vgic_bitmap contains a bitmap made of unsigned longs, but
111 * extracts u32s out of them.
Victor Kamensky9662fb42014-06-12 09:30:10 -0700112 *
113 * This does not work on 64-bit BE systems, because the bitmap access
114 * will store two consecutive 32-bit words with the higher-addressed
115 * register's bits at the lower index and the lower-addressed register's
116 * bits at the higher index.
117 *
118 * Therefore, swizzle the register index when accessing the 32-bit word
119 * registers to access the right register's value.
120 */
121#if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 64
122#define REG_OFFSET_SWIZZLE 1
123#else
124#define REG_OFFSET_SWIZZLE 0
125#endif
Marc Zyngierb47ef922013-01-21 19:36:14 -0500126
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100127static int vgic_init_bitmap(struct vgic_bitmap *b, int nr_cpus, int nr_irqs)
128{
129 int nr_longs;
130
131 nr_longs = nr_cpus + BITS_TO_LONGS(nr_irqs - VGIC_NR_PRIVATE_IRQS);
132
133 b->private = kzalloc(sizeof(unsigned long) * nr_longs, GFP_KERNEL);
134 if (!b->private)
135 return -ENOMEM;
136
137 b->shared = b->private + nr_cpus;
138
139 return 0;
140}
141
142static void vgic_free_bitmap(struct vgic_bitmap *b)
143{
144 kfree(b->private);
145 b->private = NULL;
146 b->shared = NULL;
147}
148
Christoffer Dall2df36a52014-09-28 16:04:26 +0200149/*
150 * Call this function to convert a u64 value to an unsigned long * bitmask
151 * in a way that works on both 32-bit and 64-bit LE and BE platforms.
152 *
153 * Warning: Calling this function may modify *val.
154 */
155static unsigned long *u64_to_bitmask(u64 *val)
156{
157#if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 32
158 *val = (*val >> 32) | (*val << 32);
159#endif
160 return (unsigned long *)val;
161}
162
Marc Zyngierb47ef922013-01-21 19:36:14 -0500163static u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x,
164 int cpuid, u32 offset)
165{
166 offset >>= 2;
167 if (!offset)
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100168 return (u32 *)(x->private + cpuid) + REG_OFFSET_SWIZZLE;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500169 else
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100170 return (u32 *)(x->shared) + ((offset - 1) ^ REG_OFFSET_SWIZZLE);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500171}
172
173static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
174 int cpuid, int irq)
175{
176 if (irq < VGIC_NR_PRIVATE_IRQS)
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100177 return test_bit(irq, x->private + cpuid);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500178
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100179 return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500180}
181
182static void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
183 int irq, int val)
184{
185 unsigned long *reg;
186
187 if (irq < VGIC_NR_PRIVATE_IRQS) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100188 reg = x->private + cpuid;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500189 } else {
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100190 reg = x->shared;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500191 irq -= VGIC_NR_PRIVATE_IRQS;
192 }
193
194 if (val)
195 set_bit(irq, reg);
196 else
197 clear_bit(irq, reg);
198}
199
200static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
201{
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100202 return x->private + cpuid;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500203}
204
205static unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
206{
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100207 return x->shared;
208}
209
210static int vgic_init_bytemap(struct vgic_bytemap *x, int nr_cpus, int nr_irqs)
211{
212 int size;
213
214 size = nr_cpus * VGIC_NR_PRIVATE_IRQS;
215 size += nr_irqs - VGIC_NR_PRIVATE_IRQS;
216
217 x->private = kzalloc(size, GFP_KERNEL);
218 if (!x->private)
219 return -ENOMEM;
220
221 x->shared = x->private + nr_cpus * VGIC_NR_PRIVATE_IRQS / sizeof(u32);
222 return 0;
223}
224
225static void vgic_free_bytemap(struct vgic_bytemap *b)
226{
227 kfree(b->private);
228 b->private = NULL;
229 b->shared = NULL;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500230}
231
232static u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
233{
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100234 u32 *reg;
235
236 if (offset < VGIC_NR_PRIVATE_IRQS) {
237 reg = x->private;
238 offset += cpuid * VGIC_NR_PRIVATE_IRQS;
239 } else {
240 reg = x->shared;
241 offset -= VGIC_NR_PRIVATE_IRQS;
242 }
243
244 return reg + (offset / sizeof(u32));
Marc Zyngierb47ef922013-01-21 19:36:14 -0500245}
246
247#define VGIC_CFG_LEVEL 0
248#define VGIC_CFG_EDGE 1
249
250static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq)
251{
252 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
253 int irq_val;
254
255 irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq);
256 return irq_val == VGIC_CFG_EDGE;
257}
258
259static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq)
260{
261 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
262
263 return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq);
264}
265
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200266static int vgic_irq_is_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500267{
268 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
269
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200270 return vgic_bitmap_get_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500271}
272
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200273static void vgic_irq_set_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500274{
275 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
276
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200277 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 1);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500278}
279
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200280static void vgic_irq_clear_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500281{
282 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
283
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200284 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 0);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500285}
286
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200287static int vgic_dist_irq_get_level(struct kvm_vcpu *vcpu, int irq)
288{
289 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
290
291 return vgic_bitmap_get_irq_val(&dist->irq_level, vcpu->vcpu_id, irq);
292}
293
294static void vgic_dist_irq_set_level(struct kvm_vcpu *vcpu, int irq)
295{
296 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
297
298 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 1);
299}
300
301static void vgic_dist_irq_clear_level(struct kvm_vcpu *vcpu, int irq)
302{
303 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
304
305 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 0);
306}
307
308static int vgic_dist_irq_soft_pend(struct kvm_vcpu *vcpu, int irq)
309{
310 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
311
312 return vgic_bitmap_get_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq);
313}
314
315static void vgic_dist_irq_clear_soft_pend(struct kvm_vcpu *vcpu, int irq)
316{
317 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
318
319 vgic_bitmap_set_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq, 0);
320}
321
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500322static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
323{
324 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
325
Christoffer Dall227844f2014-06-09 12:27:18 +0200326 return vgic_bitmap_get_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500327}
328
Christoffer Dall227844f2014-06-09 12:27:18 +0200329static void vgic_dist_irq_set_pending(struct kvm_vcpu *vcpu, int irq)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500330{
331 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
332
Christoffer Dall227844f2014-06-09 12:27:18 +0200333 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 1);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500334}
335
Christoffer Dall227844f2014-06-09 12:27:18 +0200336static void vgic_dist_irq_clear_pending(struct kvm_vcpu *vcpu, int irq)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500337{
338 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
339
Christoffer Dall227844f2014-06-09 12:27:18 +0200340 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 0);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500341}
342
343static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq)
344{
345 if (irq < VGIC_NR_PRIVATE_IRQS)
346 set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
347 else
348 set_bit(irq - VGIC_NR_PRIVATE_IRQS,
349 vcpu->arch.vgic_cpu.pending_shared);
350}
351
352static void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
353{
354 if (irq < VGIC_NR_PRIVATE_IRQS)
355 clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
356 else
357 clear_bit(irq - VGIC_NR_PRIVATE_IRQS,
358 vcpu->arch.vgic_cpu.pending_shared);
359}
360
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200361static bool vgic_can_sample_irq(struct kvm_vcpu *vcpu, int irq)
362{
363 return vgic_irq_is_edge(vcpu, irq) || !vgic_irq_is_queued(vcpu, irq);
364}
365
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500366static u32 mmio_data_read(struct kvm_exit_mmio *mmio, u32 mask)
367{
Victor Kamensky1c9f0472014-06-12 09:30:04 -0700368 return le32_to_cpu(*((u32 *)mmio->data)) & mask;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500369}
370
371static void mmio_data_write(struct kvm_exit_mmio *mmio, u32 mask, u32 value)
372{
Victor Kamensky1c9f0472014-06-12 09:30:04 -0700373 *((u32 *)mmio->data) = cpu_to_le32(value) & mask;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500374}
375
376/**
377 * vgic_reg_access - access vgic register
378 * @mmio: pointer to the data describing the mmio access
379 * @reg: pointer to the virtual backing of vgic distributor data
380 * @offset: least significant 2 bits used for word offset
381 * @mode: ACCESS_ mode (see defines above)
382 *
383 * Helper to make vgic register access easier using one of the access
384 * modes defined for vgic register access
385 * (read,raz,write-ignored,setbit,clearbit,write)
386 */
387static void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
388 phys_addr_t offset, int mode)
389{
390 int word_offset = (offset & 3) * 8;
391 u32 mask = (1UL << (mmio->len * 8)) - 1;
392 u32 regval;
393
394 /*
395 * Any alignment fault should have been delivered to the guest
396 * directly (ARM ARM B3.12.7 "Prioritization of aborts").
397 */
398
399 if (reg) {
400 regval = *reg;
401 } else {
402 BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED));
403 regval = 0;
404 }
405
406 if (mmio->is_write) {
407 u32 data = mmio_data_read(mmio, mask) << word_offset;
408 switch (ACCESS_WRITE_MASK(mode)) {
409 case ACCESS_WRITE_IGNORED:
410 return;
411
412 case ACCESS_WRITE_SETBIT:
413 regval |= data;
414 break;
415
416 case ACCESS_WRITE_CLEARBIT:
417 regval &= ~data;
418 break;
419
420 case ACCESS_WRITE_VALUE:
421 regval = (regval & ~(mask << word_offset)) | data;
422 break;
423 }
424 *reg = regval;
425 } else {
426 switch (ACCESS_READ_MASK(mode)) {
427 case ACCESS_READ_RAZ:
428 regval = 0;
429 /* fall through */
430
431 case ACCESS_READ_VALUE:
432 mmio_data_write(mmio, mask, regval >> word_offset);
433 }
434 }
435}
436
Marc Zyngierb47ef922013-01-21 19:36:14 -0500437static bool handle_mmio_misc(struct kvm_vcpu *vcpu,
438 struct kvm_exit_mmio *mmio, phys_addr_t offset)
439{
440 u32 reg;
441 u32 word_offset = offset & 3;
442
443 switch (offset & ~3) {
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -0700444 case 0: /* GICD_CTLR */
Marc Zyngierb47ef922013-01-21 19:36:14 -0500445 reg = vcpu->kvm->arch.vgic.enabled;
446 vgic_reg_access(mmio, &reg, word_offset,
447 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
448 if (mmio->is_write) {
449 vcpu->kvm->arch.vgic.enabled = reg & 1;
450 vgic_update_state(vcpu->kvm);
451 return true;
452 }
453 break;
454
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -0700455 case 4: /* GICD_TYPER */
Marc Zyngierb47ef922013-01-21 19:36:14 -0500456 reg = (atomic_read(&vcpu->kvm->online_vcpus) - 1) << 5;
Marc Zyngier5fb66da2014-07-08 12:09:05 +0100457 reg |= (vcpu->kvm->arch.vgic.nr_irqs >> 5) - 1;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500458 vgic_reg_access(mmio, &reg, word_offset,
459 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
460 break;
461
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -0700462 case 8: /* GICD_IIDR */
463 reg = (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500464 vgic_reg_access(mmio, &reg, word_offset,
465 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
466 break;
467 }
468
469 return false;
470}
471
472static bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu,
473 struct kvm_exit_mmio *mmio, phys_addr_t offset)
474{
475 vgic_reg_access(mmio, NULL, offset,
476 ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
477 return false;
478}
479
480static bool handle_mmio_set_enable_reg(struct kvm_vcpu *vcpu,
481 struct kvm_exit_mmio *mmio,
482 phys_addr_t offset)
483{
484 u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
485 vcpu->vcpu_id, offset);
486 vgic_reg_access(mmio, reg, offset,
487 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
488 if (mmio->is_write) {
489 vgic_update_state(vcpu->kvm);
490 return true;
491 }
492
493 return false;
494}
495
496static bool handle_mmio_clear_enable_reg(struct kvm_vcpu *vcpu,
497 struct kvm_exit_mmio *mmio,
498 phys_addr_t offset)
499{
500 u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
501 vcpu->vcpu_id, offset);
502 vgic_reg_access(mmio, reg, offset,
503 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
504 if (mmio->is_write) {
505 if (offset < 4) /* Force SGI enabled */
506 *reg |= 0xffff;
Marc Zyngiera1fcb442013-01-21 19:36:15 -0500507 vgic_retire_disabled_irqs(vcpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500508 vgic_update_state(vcpu->kvm);
509 return true;
510 }
511
512 return false;
513}
514
515static bool handle_mmio_set_pending_reg(struct kvm_vcpu *vcpu,
516 struct kvm_exit_mmio *mmio,
517 phys_addr_t offset)
518{
Christoffer Dall9da48b52014-06-14 22:30:45 +0200519 u32 *reg, orig;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200520 u32 level_mask;
521 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
522
523 reg = vgic_bitmap_get_reg(&dist->irq_cfg, vcpu->vcpu_id, offset);
524 level_mask = (~(*reg));
525
526 /* Mark both level and edge triggered irqs as pending */
527 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu->vcpu_id, offset);
Christoffer Dall9da48b52014-06-14 22:30:45 +0200528 orig = *reg;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500529 vgic_reg_access(mmio, reg, offset,
530 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200531
Marc Zyngierb47ef922013-01-21 19:36:14 -0500532 if (mmio->is_write) {
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200533 /* Set the soft-pending flag only for level-triggered irqs */
534 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
535 vcpu->vcpu_id, offset);
536 vgic_reg_access(mmio, reg, offset,
537 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
538 *reg &= level_mask;
539
Christoffer Dall9da48b52014-06-14 22:30:45 +0200540 /* Ignore writes to SGIs */
541 if (offset < 2) {
542 *reg &= ~0xffff;
543 *reg |= orig & 0xffff;
544 }
545
Marc Zyngierb47ef922013-01-21 19:36:14 -0500546 vgic_update_state(vcpu->kvm);
547 return true;
548 }
549
550 return false;
551}
552
553static bool handle_mmio_clear_pending_reg(struct kvm_vcpu *vcpu,
554 struct kvm_exit_mmio *mmio,
555 phys_addr_t offset)
556{
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200557 u32 *level_active;
Christoffer Dall9da48b52014-06-14 22:30:45 +0200558 u32 *reg, orig;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200559 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
560
561 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu->vcpu_id, offset);
Christoffer Dall9da48b52014-06-14 22:30:45 +0200562 orig = *reg;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500563 vgic_reg_access(mmio, reg, offset,
564 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
565 if (mmio->is_write) {
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200566 /* Re-set level triggered level-active interrupts */
567 level_active = vgic_bitmap_get_reg(&dist->irq_level,
568 vcpu->vcpu_id, offset);
569 reg = vgic_bitmap_get_reg(&dist->irq_pending,
570 vcpu->vcpu_id, offset);
571 *reg |= *level_active;
572
Christoffer Dall9da48b52014-06-14 22:30:45 +0200573 /* Ignore writes to SGIs */
574 if (offset < 2) {
575 *reg &= ~0xffff;
576 *reg |= orig & 0xffff;
577 }
578
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200579 /* Clear soft-pending flags */
580 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
581 vcpu->vcpu_id, offset);
582 vgic_reg_access(mmio, reg, offset,
583 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
584
Marc Zyngierb47ef922013-01-21 19:36:14 -0500585 vgic_update_state(vcpu->kvm);
586 return true;
587 }
588
589 return false;
590}
591
592static bool handle_mmio_priority_reg(struct kvm_vcpu *vcpu,
593 struct kvm_exit_mmio *mmio,
594 phys_addr_t offset)
595{
596 u32 *reg = vgic_bytemap_get_reg(&vcpu->kvm->arch.vgic.irq_priority,
597 vcpu->vcpu_id, offset);
598 vgic_reg_access(mmio, reg, offset,
599 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
600 return false;
601}
602
603#define GICD_ITARGETSR_SIZE 32
604#define GICD_CPUTARGETS_BITS 8
605#define GICD_IRQS_PER_ITARGETSR (GICD_ITARGETSR_SIZE / GICD_CPUTARGETS_BITS)
606static u32 vgic_get_target_reg(struct kvm *kvm, int irq)
607{
608 struct vgic_dist *dist = &kvm->arch.vgic;
Marc Zyngier986af8e2013-08-29 11:08:22 +0100609 int i;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500610 u32 val = 0;
611
612 irq -= VGIC_NR_PRIVATE_IRQS;
613
Marc Zyngier986af8e2013-08-29 11:08:22 +0100614 for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++)
615 val |= 1 << (dist->irq_spi_cpu[irq + i] + i * 8);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500616
617 return val;
618}
619
620static void vgic_set_target_reg(struct kvm *kvm, u32 val, int irq)
621{
622 struct vgic_dist *dist = &kvm->arch.vgic;
623 struct kvm_vcpu *vcpu;
624 int i, c;
625 unsigned long *bmap;
626 u32 target;
627
628 irq -= VGIC_NR_PRIVATE_IRQS;
629
630 /*
631 * Pick the LSB in each byte. This ensures we target exactly
632 * one vcpu per IRQ. If the byte is null, assume we target
633 * CPU0.
634 */
635 for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++) {
636 int shift = i * GICD_CPUTARGETS_BITS;
637 target = ffs((val >> shift) & 0xffU);
638 target = target ? (target - 1) : 0;
639 dist->irq_spi_cpu[irq + i] = target;
640 kvm_for_each_vcpu(c, vcpu, kvm) {
641 bmap = vgic_bitmap_get_shared_map(&dist->irq_spi_target[c]);
642 if (c == target)
643 set_bit(irq + i, bmap);
644 else
645 clear_bit(irq + i, bmap);
646 }
647 }
648}
649
650static bool handle_mmio_target_reg(struct kvm_vcpu *vcpu,
651 struct kvm_exit_mmio *mmio,
652 phys_addr_t offset)
653{
654 u32 reg;
655
656 /* We treat the banked interrupts targets as read-only */
657 if (offset < 32) {
658 u32 roreg = 1 << vcpu->vcpu_id;
659 roreg |= roreg << 8;
660 roreg |= roreg << 16;
661
662 vgic_reg_access(mmio, &roreg, offset,
663 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
664 return false;
665 }
666
667 reg = vgic_get_target_reg(vcpu->kvm, offset & ~3U);
668 vgic_reg_access(mmio, &reg, offset,
669 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
670 if (mmio->is_write) {
671 vgic_set_target_reg(vcpu->kvm, reg, offset & ~3U);
672 vgic_update_state(vcpu->kvm);
673 return true;
674 }
675
676 return false;
677}
678
679static u32 vgic_cfg_expand(u16 val)
680{
681 u32 res = 0;
682 int i;
683
684 /*
685 * Turn a 16bit value like abcd...mnop into a 32bit word
686 * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
687 */
688 for (i = 0; i < 16; i++)
689 res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
690
691 return res;
692}
693
694static u16 vgic_cfg_compress(u32 val)
695{
696 u16 res = 0;
697 int i;
698
699 /*
700 * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
701 * abcd...mnop which is what we really care about.
702 */
703 for (i = 0; i < 16; i++)
704 res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
705
706 return res;
707}
708
709/*
710 * The distributor uses 2 bits per IRQ for the CFG register, but the
711 * LSB is always 0. As such, we only keep the upper bit, and use the
712 * two above functions to compress/expand the bits
713 */
714static bool handle_mmio_cfg_reg(struct kvm_vcpu *vcpu,
715 struct kvm_exit_mmio *mmio, phys_addr_t offset)
716{
717 u32 val;
Marc Zyngier6545eae2013-08-29 11:08:23 +0100718 u32 *reg;
719
Marc Zyngier6545eae2013-08-29 11:08:23 +0100720 reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_cfg,
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200721 vcpu->vcpu_id, offset >> 1);
Marc Zyngier6545eae2013-08-29 11:08:23 +0100722
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200723 if (offset & 4)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500724 val = *reg >> 16;
725 else
726 val = *reg & 0xffff;
727
728 val = vgic_cfg_expand(val);
729 vgic_reg_access(mmio, &val, offset,
730 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
731 if (mmio->is_write) {
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200732 if (offset < 8) {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500733 *reg = ~0U; /* Force PPIs/SGIs to 1 */
734 return false;
735 }
736
737 val = vgic_cfg_compress(val);
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200738 if (offset & 4) {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500739 *reg &= 0xffff;
740 *reg |= val << 16;
741 } else {
742 *reg &= 0xffff << 16;
743 *reg |= val;
744 }
745 }
746
747 return false;
748}
749
750static bool handle_mmio_sgi_reg(struct kvm_vcpu *vcpu,
751 struct kvm_exit_mmio *mmio, phys_addr_t offset)
752{
753 u32 reg;
754 vgic_reg_access(mmio, &reg, offset,
755 ACCESS_READ_RAZ | ACCESS_WRITE_VALUE);
756 if (mmio->is_write) {
757 vgic_dispatch_sgi(vcpu, reg);
758 vgic_update_state(vcpu->kvm);
759 return true;
760 }
761
762 return false;
763}
764
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800765/**
766 * vgic_unqueue_irqs - move pending IRQs from LRs to the distributor
767 * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs
768 *
769 * Move any pending IRQs that have already been assigned to LRs back to the
770 * emulated distributor state so that the complete emulated state can be read
771 * from the main emulation structures without investigating the LRs.
772 *
773 * Note that IRQs in the active state in the LRs get their pending state moved
774 * to the distributor but the active state stays in the LRs, because we don't
775 * track the active state on the distributor side.
776 */
777static void vgic_unqueue_irqs(struct kvm_vcpu *vcpu)
778{
779 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
780 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
781 int vcpu_id = vcpu->vcpu_id;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100782 int i;
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800783
784 for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100785 struct vgic_lr lr = vgic_get_lr(vcpu, i);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800786
787 /*
788 * There are three options for the state bits:
789 *
790 * 01: pending
791 * 10: active
792 * 11: pending and active
793 *
794 * If the LR holds only an active interrupt (not pending) then
795 * just leave it alone.
796 */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100797 if ((lr.state & LR_STATE_MASK) == LR_STATE_ACTIVE)
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800798 continue;
799
800 /*
801 * Reestablish the pending state on the distributor and the
802 * CPU interface. It may have already been pending, but that
803 * is fine, then we are only setting a few bits that were
804 * already set.
805 */
Christoffer Dall227844f2014-06-09 12:27:18 +0200806 vgic_dist_irq_set_pending(vcpu, lr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100807 if (lr.irq < VGIC_NR_SGIS)
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100808 *vgic_get_sgi_sources(dist, vcpu_id, lr.irq) |= 1 << lr.source;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100809 lr.state &= ~LR_STATE_PENDING;
810 vgic_set_lr(vcpu, i, lr);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800811
812 /*
813 * If there's no state left on the LR (it could still be
814 * active), then the LR does not hold any useful info and can
815 * be marked as free for other use.
816 */
Christoffer Dallcced50c2014-06-14 22:37:33 +0200817 if (!(lr.state & LR_STATE_MASK)) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100818 vgic_retire_lr(i, lr.irq, vcpu);
Christoffer Dallcced50c2014-06-14 22:37:33 +0200819 vgic_irq_clear_queued(vcpu, lr.irq);
820 }
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800821
822 /* Finally update the VGIC state. */
823 vgic_update_state(vcpu->kvm);
824 }
825}
826
Christoffer Dall90a53552013-10-25 21:22:31 +0100827/* Handle reads of GICD_CPENDSGIRn and GICD_SPENDSGIRn */
828static bool read_set_clear_sgi_pend_reg(struct kvm_vcpu *vcpu,
829 struct kvm_exit_mmio *mmio,
830 phys_addr_t offset)
Christoffer Dallc07a0192013-10-25 21:17:31 +0100831{
Christoffer Dall90a53552013-10-25 21:22:31 +0100832 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
833 int sgi;
Christoffer Dall0fea6d72014-09-25 18:41:07 +0200834 int min_sgi = (offset & ~0x3);
Christoffer Dall90a53552013-10-25 21:22:31 +0100835 int max_sgi = min_sgi + 3;
836 int vcpu_id = vcpu->vcpu_id;
837 u32 reg = 0;
838
839 /* Copy source SGIs from distributor side */
840 for (sgi = min_sgi; sgi <= max_sgi; sgi++) {
841 int shift = 8 * (sgi - min_sgi);
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100842 reg |= ((u32)*vgic_get_sgi_sources(dist, vcpu_id, sgi)) << shift;
Christoffer Dall90a53552013-10-25 21:22:31 +0100843 }
844
845 mmio_data_write(mmio, ~0, reg);
Christoffer Dallc07a0192013-10-25 21:17:31 +0100846 return false;
847}
848
Christoffer Dall90a53552013-10-25 21:22:31 +0100849static bool write_set_clear_sgi_pend_reg(struct kvm_vcpu *vcpu,
850 struct kvm_exit_mmio *mmio,
851 phys_addr_t offset, bool set)
852{
853 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
854 int sgi;
Christoffer Dall0fea6d72014-09-25 18:41:07 +0200855 int min_sgi = (offset & ~0x3);
Christoffer Dall90a53552013-10-25 21:22:31 +0100856 int max_sgi = min_sgi + 3;
857 int vcpu_id = vcpu->vcpu_id;
858 u32 reg;
859 bool updated = false;
860
861 reg = mmio_data_read(mmio, ~0);
862
863 /* Clear pending SGIs on the distributor */
864 for (sgi = min_sgi; sgi <= max_sgi; sgi++) {
865 u8 mask = reg >> (8 * (sgi - min_sgi));
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100866 u8 *src = vgic_get_sgi_sources(dist, vcpu_id, sgi);
Christoffer Dall90a53552013-10-25 21:22:31 +0100867 if (set) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100868 if ((*src & mask) != mask)
Christoffer Dall90a53552013-10-25 21:22:31 +0100869 updated = true;
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100870 *src |= mask;
Christoffer Dall90a53552013-10-25 21:22:31 +0100871 } else {
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100872 if (*src & mask)
Christoffer Dall90a53552013-10-25 21:22:31 +0100873 updated = true;
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100874 *src &= ~mask;
Christoffer Dall90a53552013-10-25 21:22:31 +0100875 }
876 }
877
878 if (updated)
879 vgic_update_state(vcpu->kvm);
880
881 return updated;
882}
883
Christoffer Dallc07a0192013-10-25 21:17:31 +0100884static bool handle_mmio_sgi_set(struct kvm_vcpu *vcpu,
885 struct kvm_exit_mmio *mmio,
886 phys_addr_t offset)
887{
Christoffer Dall90a53552013-10-25 21:22:31 +0100888 if (!mmio->is_write)
889 return read_set_clear_sgi_pend_reg(vcpu, mmio, offset);
890 else
891 return write_set_clear_sgi_pend_reg(vcpu, mmio, offset, true);
892}
893
894static bool handle_mmio_sgi_clear(struct kvm_vcpu *vcpu,
895 struct kvm_exit_mmio *mmio,
896 phys_addr_t offset)
897{
898 if (!mmio->is_write)
899 return read_set_clear_sgi_pend_reg(vcpu, mmio, offset);
900 else
901 return write_set_clear_sgi_pend_reg(vcpu, mmio, offset, false);
Christoffer Dallc07a0192013-10-25 21:17:31 +0100902}
903
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500904/*
905 * I would have liked to use the kvm_bus_io_*() API instead, but it
906 * cannot cope with banked registers (only the VM pointer is passed
907 * around, and we need the vcpu). One of these days, someone please
908 * fix it!
909 */
910struct mmio_range {
911 phys_addr_t base;
912 unsigned long len;
Marc Zyngierc3c91832014-07-08 12:09:04 +0100913 int bits_per_irq;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500914 bool (*handle_mmio)(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
915 phys_addr_t offset);
916};
917
Christoffer Dall1006e8c2013-09-23 14:55:56 -0700918static const struct mmio_range vgic_dist_ranges[] = {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500919 {
920 .base = GIC_DIST_CTRL,
921 .len = 12,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100922 .bits_per_irq = 0,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500923 .handle_mmio = handle_mmio_misc,
924 },
925 {
926 .base = GIC_DIST_IGROUP,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100927 .len = VGIC_MAX_IRQS / 8,
928 .bits_per_irq = 1,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500929 .handle_mmio = handle_mmio_raz_wi,
930 },
931 {
932 .base = GIC_DIST_ENABLE_SET,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100933 .len = VGIC_MAX_IRQS / 8,
934 .bits_per_irq = 1,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500935 .handle_mmio = handle_mmio_set_enable_reg,
936 },
937 {
938 .base = GIC_DIST_ENABLE_CLEAR,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100939 .len = VGIC_MAX_IRQS / 8,
940 .bits_per_irq = 1,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500941 .handle_mmio = handle_mmio_clear_enable_reg,
942 },
943 {
944 .base = GIC_DIST_PENDING_SET,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100945 .len = VGIC_MAX_IRQS / 8,
946 .bits_per_irq = 1,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500947 .handle_mmio = handle_mmio_set_pending_reg,
948 },
949 {
950 .base = GIC_DIST_PENDING_CLEAR,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100951 .len = VGIC_MAX_IRQS / 8,
952 .bits_per_irq = 1,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500953 .handle_mmio = handle_mmio_clear_pending_reg,
954 },
955 {
956 .base = GIC_DIST_ACTIVE_SET,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100957 .len = VGIC_MAX_IRQS / 8,
958 .bits_per_irq = 1,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500959 .handle_mmio = handle_mmio_raz_wi,
960 },
961 {
962 .base = GIC_DIST_ACTIVE_CLEAR,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100963 .len = VGIC_MAX_IRQS / 8,
964 .bits_per_irq = 1,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500965 .handle_mmio = handle_mmio_raz_wi,
966 },
967 {
968 .base = GIC_DIST_PRI,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100969 .len = VGIC_MAX_IRQS,
970 .bits_per_irq = 8,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500971 .handle_mmio = handle_mmio_priority_reg,
972 },
973 {
974 .base = GIC_DIST_TARGET,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100975 .len = VGIC_MAX_IRQS,
976 .bits_per_irq = 8,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500977 .handle_mmio = handle_mmio_target_reg,
978 },
979 {
980 .base = GIC_DIST_CONFIG,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100981 .len = VGIC_MAX_IRQS / 4,
982 .bits_per_irq = 2,
Marc Zyngierb47ef922013-01-21 19:36:14 -0500983 .handle_mmio = handle_mmio_cfg_reg,
984 },
985 {
986 .base = GIC_DIST_SOFTINT,
987 .len = 4,
988 .handle_mmio = handle_mmio_sgi_reg,
989 },
Christoffer Dallc07a0192013-10-25 21:17:31 +0100990 {
991 .base = GIC_DIST_SGI_PENDING_CLEAR,
992 .len = VGIC_NR_SGIS,
993 .handle_mmio = handle_mmio_sgi_clear,
994 },
995 {
996 .base = GIC_DIST_SGI_PENDING_SET,
997 .len = VGIC_NR_SGIS,
998 .handle_mmio = handle_mmio_sgi_set,
999 },
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001000 {}
1001};
1002
1003static const
1004struct mmio_range *find_matching_range(const struct mmio_range *ranges,
1005 struct kvm_exit_mmio *mmio,
Christoffer Dall1006e8c2013-09-23 14:55:56 -07001006 phys_addr_t offset)
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001007{
1008 const struct mmio_range *r = ranges;
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001009
1010 while (r->len) {
Christoffer Dall1006e8c2013-09-23 14:55:56 -07001011 if (offset >= r->base &&
1012 (offset + mmio->len) <= (r->base + r->len))
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001013 return r;
1014 r++;
1015 }
1016
1017 return NULL;
1018}
1019
Marc Zyngierc3c91832014-07-08 12:09:04 +01001020static bool vgic_validate_access(const struct vgic_dist *dist,
1021 const struct mmio_range *range,
1022 unsigned long offset)
1023{
1024 int irq;
1025
1026 if (!range->bits_per_irq)
1027 return true; /* Not an irq-based access */
1028
1029 irq = offset * 8 / range->bits_per_irq;
1030 if (irq >= dist->nr_irqs)
1031 return false;
1032
1033 return true;
1034}
1035
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001036/**
Andre Przywara96415252014-06-02 22:44:37 +02001037 * vgic_handle_mmio_range - handle an in-kernel MMIO access
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001038 * @vcpu: pointer to the vcpu performing the access
1039 * @run: pointer to the kvm_run structure
1040 * @mmio: pointer to the data describing the access
Andre Przywara96415252014-06-02 22:44:37 +02001041 * @ranges: array of MMIO ranges in a given region
1042 * @mmio_base: base address of that region
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001043 *
Andre Przywara96415252014-06-02 22:44:37 +02001044 * returns true if the MMIO access could be performed
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001045 */
Andre Przywara96415252014-06-02 22:44:37 +02001046static bool vgic_handle_mmio_range(struct kvm_vcpu *vcpu, struct kvm_run *run,
1047 struct kvm_exit_mmio *mmio,
1048 const struct mmio_range *ranges,
1049 unsigned long mmio_base)
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001050{
Marc Zyngierb47ef922013-01-21 19:36:14 -05001051 const struct mmio_range *range;
1052 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngierb47ef922013-01-21 19:36:14 -05001053 bool updated_state;
1054 unsigned long offset;
1055
Andre Przywara96415252014-06-02 22:44:37 +02001056 offset = mmio->phys_addr - mmio_base;
1057 range = find_matching_range(ranges, mmio, offset);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001058 if (unlikely(!range || !range->handle_mmio)) {
1059 pr_warn("Unhandled access %d %08llx %d\n",
1060 mmio->is_write, mmio->phys_addr, mmio->len);
1061 return false;
1062 }
1063
1064 spin_lock(&vcpu->kvm->arch.vgic.lock);
Andre Przywara96415252014-06-02 22:44:37 +02001065 offset -= range->base;
Marc Zyngierc3c91832014-07-08 12:09:04 +01001066 if (vgic_validate_access(dist, range, offset)) {
1067 updated_state = range->handle_mmio(vcpu, mmio, offset);
1068 } else {
1069 vgic_reg_access(mmio, NULL, offset,
1070 ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
1071 updated_state = false;
1072 }
Marc Zyngierb47ef922013-01-21 19:36:14 -05001073 spin_unlock(&vcpu->kvm->arch.vgic.lock);
1074 kvm_prepare_mmio(run, mmio);
1075 kvm_handle_mmio_return(vcpu, run);
1076
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001077 if (updated_state)
1078 vgic_kick_vcpus(vcpu->kvm);
1079
Marc Zyngierb47ef922013-01-21 19:36:14 -05001080 return true;
1081}
1082
Andre Przywara96415252014-06-02 22:44:37 +02001083static inline bool is_in_range(phys_addr_t addr, unsigned long len,
1084 phys_addr_t baseaddr, unsigned long size)
1085{
1086 return (addr >= baseaddr) && (addr + len <= baseaddr + size);
1087}
1088
1089static bool vgic_v2_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
1090 struct kvm_exit_mmio *mmio)
1091{
1092 unsigned long base = vcpu->kvm->arch.vgic.vgic_dist_base;
1093
1094 if (!is_in_range(mmio->phys_addr, mmio->len, base,
1095 KVM_VGIC_V2_DIST_SIZE))
1096 return false;
1097
1098 /* GICv2 does not support accesses wider than 32 bits */
1099 if (mmio->len > 4) {
1100 kvm_inject_dabt(vcpu, mmio->phys_addr);
1101 return true;
1102 }
1103
1104 return vgic_handle_mmio_range(vcpu, run, mmio, vgic_dist_ranges, base);
1105}
1106
1107/**
1108 * vgic_handle_mmio - handle an in-kernel MMIO access for the GIC emulation
1109 * @vcpu: pointer to the vcpu performing the access
1110 * @run: pointer to the kvm_run structure
1111 * @mmio: pointer to the data describing the access
1112 *
1113 * returns true if the MMIO access has been performed in kernel space,
1114 * and false if it needs to be emulated in user space.
1115 */
1116bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
1117 struct kvm_exit_mmio *mmio)
1118{
1119 if (!irqchip_in_kernel(vcpu->kvm))
1120 return false;
1121
1122 return vgic_v2_handle_mmio(vcpu, run, mmio);
1123}
1124
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001125static u8 *vgic_get_sgi_sources(struct vgic_dist *dist, int vcpu_id, int sgi)
1126{
1127 return dist->irq_sgi_sources + vcpu_id * VGIC_NR_SGIS + sgi;
1128}
1129
Marc Zyngierb47ef922013-01-21 19:36:14 -05001130static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg)
1131{
1132 struct kvm *kvm = vcpu->kvm;
1133 struct vgic_dist *dist = &kvm->arch.vgic;
1134 int nrcpus = atomic_read(&kvm->online_vcpus);
1135 u8 target_cpus;
1136 int sgi, mode, c, vcpu_id;
1137
1138 vcpu_id = vcpu->vcpu_id;
1139
1140 sgi = reg & 0xf;
1141 target_cpus = (reg >> 16) & 0xff;
1142 mode = (reg >> 24) & 3;
1143
1144 switch (mode) {
1145 case 0:
1146 if (!target_cpus)
1147 return;
Haibin Wang91021a62014-04-10 13:14:32 +01001148 break;
Marc Zyngierb47ef922013-01-21 19:36:14 -05001149
1150 case 1:
1151 target_cpus = ((1 << nrcpus) - 1) & ~(1 << vcpu_id) & 0xff;
1152 break;
1153
1154 case 2:
1155 target_cpus = 1 << vcpu_id;
1156 break;
1157 }
1158
1159 kvm_for_each_vcpu(c, vcpu, kvm) {
1160 if (target_cpus & 1) {
1161 /* Flag the SGI as pending */
Christoffer Dall227844f2014-06-09 12:27:18 +02001162 vgic_dist_irq_set_pending(vcpu, sgi);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001163 *vgic_get_sgi_sources(dist, c, sgi) |= 1 << vcpu_id;
Marc Zyngierb47ef922013-01-21 19:36:14 -05001164 kvm_debug("SGI%d from CPU%d to CPU%d\n", sgi, vcpu_id, c);
1165 }
1166
1167 target_cpus >>= 1;
1168 }
1169}
1170
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001171static int vgic_nr_shared_irqs(struct vgic_dist *dist)
1172{
1173 return dist->nr_irqs - VGIC_NR_PRIVATE_IRQS;
1174}
1175
Marc Zyngierb47ef922013-01-21 19:36:14 -05001176static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
1177{
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001178 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1179 unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
1180 unsigned long pending_private, pending_shared;
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001181 int nr_shared = vgic_nr_shared_irqs(dist);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001182 int vcpu_id;
1183
1184 vcpu_id = vcpu->vcpu_id;
1185 pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
1186 pend_shared = vcpu->arch.vgic_cpu.pending_shared;
1187
Christoffer Dall227844f2014-06-09 12:27:18 +02001188 pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001189 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
1190 bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
1191
Christoffer Dall227844f2014-06-09 12:27:18 +02001192 pending = vgic_bitmap_get_shared_map(&dist->irq_pending);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001193 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001194 bitmap_and(pend_shared, pending, enabled, nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001195 bitmap_and(pend_shared, pend_shared,
1196 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001197 nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001198
1199 pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001200 pending_shared = find_first_bit(pend_shared, nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001201 return (pending_private < VGIC_NR_PRIVATE_IRQS ||
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001202 pending_shared < vgic_nr_shared_irqs(dist));
Marc Zyngierb47ef922013-01-21 19:36:14 -05001203}
1204
1205/*
1206 * Update the interrupt state and determine which CPUs have pending
1207 * interrupts. Must be called with distributor lock held.
1208 */
1209static void vgic_update_state(struct kvm *kvm)
1210{
1211 struct vgic_dist *dist = &kvm->arch.vgic;
1212 struct kvm_vcpu *vcpu;
1213 int c;
1214
1215 if (!dist->enabled) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001216 set_bit(0, dist->irq_pending_on_cpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001217 return;
1218 }
1219
1220 kvm_for_each_vcpu(c, vcpu, kvm) {
1221 if (compute_pending_for_cpu(vcpu)) {
1222 pr_debug("CPU%d has pending interrupts\n", c);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001223 set_bit(c, dist->irq_pending_on_cpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001224 }
1225 }
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001226}
Christoffer Dall330690c2013-01-21 19:36:13 -05001227
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001228static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr)
1229{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001230 return vgic_ops->get_lr(vcpu, lr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001231}
1232
1233static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr,
1234 struct vgic_lr vlr)
1235{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001236 vgic_ops->set_lr(vcpu, lr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001237}
1238
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001239static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
1240 struct vgic_lr vlr)
1241{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001242 vgic_ops->sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001243}
1244
1245static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu)
1246{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001247 return vgic_ops->get_elrsr(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001248}
1249
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001250static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu)
1251{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001252 return vgic_ops->get_eisr(vcpu);
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001253}
1254
Marc Zyngier495dd852013-06-04 11:02:10 +01001255static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu)
1256{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001257 return vgic_ops->get_interrupt_status(vcpu);
Marc Zyngier495dd852013-06-04 11:02:10 +01001258}
1259
Marc Zyngier909d9b52013-06-04 11:24:17 +01001260static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu)
1261{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001262 vgic_ops->enable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001263}
1264
1265static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu)
1266{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001267 vgic_ops->disable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001268}
1269
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001270static inline void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1271{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001272 vgic_ops->get_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001273}
1274
1275static void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1276{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001277 vgic_ops->set_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001278}
1279
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001280static inline void vgic_enable(struct kvm_vcpu *vcpu)
1281{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001282 vgic_ops->enable(vcpu);
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001283}
1284
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001285static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu)
1286{
1287 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1288 struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr);
1289
1290 vlr.state = 0;
1291 vgic_set_lr(vcpu, lr_nr, vlr);
1292 clear_bit(lr_nr, vgic_cpu->lr_used);
1293 vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
1294}
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001295
1296/*
1297 * An interrupt may have been disabled after being made pending on the
1298 * CPU interface (the classic case is a timer running while we're
1299 * rebooting the guest - the interrupt would kick as soon as the CPU
1300 * interface gets enabled, with deadly consequences).
1301 *
1302 * The solution is to examine already active LRs, and check the
1303 * interrupt is still enabled. If not, just retire it.
1304 */
1305static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
1306{
1307 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1308 int lr;
1309
Marc Zyngier8f186d52014-02-04 18:13:03 +00001310 for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001311 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001312
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001313 if (!vgic_irq_is_enabled(vcpu, vlr.irq)) {
1314 vgic_retire_lr(lr, vlr.irq, vcpu);
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001315 if (vgic_irq_is_queued(vcpu, vlr.irq))
1316 vgic_irq_clear_queued(vcpu, vlr.irq);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001317 }
1318 }
1319}
1320
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001321/*
1322 * Queue an interrupt to a CPU virtual interface. Return true on success,
1323 * or false if it wasn't possible to queue it.
1324 */
1325static bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
1326{
1327 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001328 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001329 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001330 int lr;
1331
1332 /* Sanitize the input... */
1333 BUG_ON(sgi_source_id & ~7);
1334 BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001335 BUG_ON(irq >= dist->nr_irqs);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001336
1337 kvm_debug("Queue IRQ%d\n", irq);
1338
1339 lr = vgic_cpu->vgic_irq_lr_map[irq];
1340
1341 /* Do we have an active interrupt for the same CPUID? */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001342 if (lr != LR_EMPTY) {
1343 vlr = vgic_get_lr(vcpu, lr);
1344 if (vlr.source == sgi_source_id) {
1345 kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq);
1346 BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
1347 vlr.state |= LR_STATE_PENDING;
1348 vgic_set_lr(vcpu, lr, vlr);
1349 return true;
1350 }
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001351 }
1352
1353 /* Try to use another LR for this interrupt */
1354 lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
Marc Zyngier8f186d52014-02-04 18:13:03 +00001355 vgic->nr_lr);
1356 if (lr >= vgic->nr_lr)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001357 return false;
1358
1359 kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001360 vgic_cpu->vgic_irq_lr_map[irq] = lr;
1361 set_bit(lr, vgic_cpu->lr_used);
1362
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001363 vlr.irq = irq;
1364 vlr.source = sgi_source_id;
1365 vlr.state = LR_STATE_PENDING;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001366 if (!vgic_irq_is_edge(vcpu, irq))
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001367 vlr.state |= LR_EOI_INT;
1368
1369 vgic_set_lr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001370
1371 return true;
1372}
1373
1374static bool vgic_queue_sgi(struct kvm_vcpu *vcpu, int irq)
1375{
1376 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1377 unsigned long sources;
1378 int vcpu_id = vcpu->vcpu_id;
1379 int c;
1380
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001381 sources = *vgic_get_sgi_sources(dist, vcpu_id, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001382
Marc Zyngierfc675e32014-07-08 12:09:03 +01001383 for_each_set_bit(c, &sources, dist->nr_cpus) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001384 if (vgic_queue_irq(vcpu, c, irq))
1385 clear_bit(c, &sources);
1386 }
1387
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001388 *vgic_get_sgi_sources(dist, vcpu_id, irq) = sources;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001389
1390 /*
1391 * If the sources bitmap has been cleared it means that we
1392 * could queue all the SGIs onto link registers (see the
1393 * clear_bit above), and therefore we are done with them in
1394 * our emulated gic and can get rid of them.
1395 */
1396 if (!sources) {
Christoffer Dall227844f2014-06-09 12:27:18 +02001397 vgic_dist_irq_clear_pending(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001398 vgic_cpu_irq_clear(vcpu, irq);
1399 return true;
1400 }
1401
1402 return false;
1403}
1404
1405static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
1406{
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001407 if (!vgic_can_sample_irq(vcpu, irq))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001408 return true; /* level interrupt, already queued */
1409
1410 if (vgic_queue_irq(vcpu, 0, irq)) {
1411 if (vgic_irq_is_edge(vcpu, irq)) {
Christoffer Dall227844f2014-06-09 12:27:18 +02001412 vgic_dist_irq_clear_pending(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001413 vgic_cpu_irq_clear(vcpu, irq);
1414 } else {
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001415 vgic_irq_set_queued(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001416 }
1417
1418 return true;
1419 }
1420
1421 return false;
1422}
1423
1424/*
1425 * Fill the list registers with pending interrupts before running the
1426 * guest.
1427 */
1428static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1429{
1430 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1431 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1432 int i, vcpu_id;
1433 int overflow = 0;
1434
1435 vcpu_id = vcpu->vcpu_id;
1436
1437 /*
1438 * We may not have any pending interrupt, or the interrupts
1439 * may have been serviced from another vcpu. In all cases,
1440 * move along.
1441 */
1442 if (!kvm_vgic_vcpu_pending_irq(vcpu)) {
1443 pr_debug("CPU%d has no pending interrupt\n", vcpu_id);
1444 goto epilog;
1445 }
1446
1447 /* SGIs */
1448 for_each_set_bit(i, vgic_cpu->pending_percpu, VGIC_NR_SGIS) {
1449 if (!vgic_queue_sgi(vcpu, i))
1450 overflow = 1;
1451 }
1452
1453 /* PPIs */
1454 for_each_set_bit_from(i, vgic_cpu->pending_percpu, VGIC_NR_PRIVATE_IRQS) {
1455 if (!vgic_queue_hwirq(vcpu, i))
1456 overflow = 1;
1457 }
1458
1459 /* SPIs */
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001460 for_each_set_bit(i, vgic_cpu->pending_shared, vgic_nr_shared_irqs(dist)) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001461 if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
1462 overflow = 1;
1463 }
1464
1465epilog:
1466 if (overflow) {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001467 vgic_enable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001468 } else {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001469 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001470 /*
1471 * We're about to run this VCPU, and we've consumed
1472 * everything the distributor had in store for
1473 * us. Claim we don't have anything pending. We'll
1474 * adjust that if needed while exiting.
1475 */
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001476 clear_bit(vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001477 }
1478}
1479
1480static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1481{
Marc Zyngier495dd852013-06-04 11:02:10 +01001482 u32 status = vgic_get_interrupt_status(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001483 bool level_pending = false;
1484
Marc Zyngier495dd852013-06-04 11:02:10 +01001485 kvm_debug("STATUS = %08x\n", status);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001486
Marc Zyngier495dd852013-06-04 11:02:10 +01001487 if (status & INT_STATUS_EOI) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001488 /*
1489 * Some level interrupts have been EOIed. Clear their
1490 * active bit.
1491 */
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001492 u64 eisr = vgic_get_eisr(vcpu);
Christoffer Dall2df36a52014-09-28 16:04:26 +02001493 unsigned long *eisr_ptr = u64_to_bitmask(&eisr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001494 int lr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001495
Marc Zyngier8f186d52014-02-04 18:13:03 +00001496 for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001497 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001498 WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001499
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001500 vgic_irq_clear_queued(vcpu, vlr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001501 WARN_ON(vlr.state & LR_STATE_MASK);
1502 vlr.state = 0;
1503 vgic_set_lr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001504
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001505 /*
1506 * If the IRQ was EOIed it was also ACKed and we we
1507 * therefore assume we can clear the soft pending
1508 * state (should it had been set) for this interrupt.
1509 *
1510 * Note: if the IRQ soft pending state was set after
1511 * the IRQ was acked, it actually shouldn't be
1512 * cleared, but we have no way of knowing that unless
1513 * we start trapping ACKs when the soft-pending state
1514 * is set.
1515 */
1516 vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
1517
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001518 /* Any additional pending interrupt? */
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001519 if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001520 vgic_cpu_irq_set(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001521 level_pending = true;
1522 } else {
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001523 vgic_dist_irq_clear_pending(vcpu, vlr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001524 vgic_cpu_irq_clear(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001525 }
Marc Zyngier75da01e2013-01-31 11:25:52 +00001526
1527 /*
1528 * Despite being EOIed, the LR may not have
1529 * been marked as empty.
1530 */
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001531 vgic_sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001532 }
1533 }
1534
Marc Zyngier495dd852013-06-04 11:02:10 +01001535 if (status & INT_STATUS_UNDERFLOW)
Marc Zyngier909d9b52013-06-04 11:24:17 +01001536 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001537
1538 return level_pending;
1539}
1540
1541/*
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001542 * Sync back the VGIC state after a guest run. The distributor lock is
1543 * needed so we don't get preempted in the middle of the state processing.
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001544 */
1545static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1546{
1547 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1548 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001549 u64 elrsr;
1550 unsigned long *elrsr_ptr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001551 int lr, pending;
1552 bool level_pending;
1553
1554 level_pending = vgic_process_maintenance(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001555 elrsr = vgic_get_elrsr(vcpu);
Christoffer Dall2df36a52014-09-28 16:04:26 +02001556 elrsr_ptr = u64_to_bitmask(&elrsr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001557
1558 /* Clear mappings for empty LRs */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001559 for_each_set_bit(lr, elrsr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001560 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001561
1562 if (!test_and_clear_bit(lr, vgic_cpu->lr_used))
1563 continue;
1564
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001565 vlr = vgic_get_lr(vcpu, lr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001566
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001567 BUG_ON(vlr.irq >= dist->nr_irqs);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001568 vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001569 }
1570
1571 /* Check if we still have something up our sleeve... */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001572 pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr);
1573 if (level_pending || pending < vgic->nr_lr)
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001574 set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001575}
1576
1577void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1578{
1579 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1580
1581 if (!irqchip_in_kernel(vcpu->kvm))
1582 return;
1583
1584 spin_lock(&dist->lock);
1585 __kvm_vgic_flush_hwstate(vcpu);
1586 spin_unlock(&dist->lock);
1587}
1588
1589void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1590{
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001591 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1592
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001593 if (!irqchip_in_kernel(vcpu->kvm))
1594 return;
1595
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001596 spin_lock(&dist->lock);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001597 __kvm_vgic_sync_hwstate(vcpu);
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001598 spin_unlock(&dist->lock);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001599}
1600
1601int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
1602{
1603 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1604
1605 if (!irqchip_in_kernel(vcpu->kvm))
1606 return 0;
1607
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001608 return test_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001609}
1610
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001611static void vgic_kick_vcpus(struct kvm *kvm)
1612{
1613 struct kvm_vcpu *vcpu;
1614 int c;
1615
1616 /*
1617 * We've injected an interrupt, time to find out who deserves
1618 * a good kick...
1619 */
1620 kvm_for_each_vcpu(c, vcpu, kvm) {
1621 if (kvm_vgic_vcpu_pending_irq(vcpu))
1622 kvm_vcpu_kick(vcpu);
1623 }
1624}
1625
1626static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
1627{
Christoffer Dall227844f2014-06-09 12:27:18 +02001628 int edge_triggered = vgic_irq_is_edge(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001629
1630 /*
1631 * Only inject an interrupt if:
1632 * - edge triggered and we have a rising edge
1633 * - level triggered and we change level
1634 */
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001635 if (edge_triggered) {
1636 int state = vgic_dist_irq_is_pending(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001637 return level > state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001638 } else {
1639 int state = vgic_dist_irq_get_level(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001640 return level != state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001641 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001642}
1643
Shannon Zhao016ed392014-11-19 10:11:25 +00001644static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001645 unsigned int irq_num, bool level)
1646{
1647 struct vgic_dist *dist = &kvm->arch.vgic;
1648 struct kvm_vcpu *vcpu;
Christoffer Dall227844f2014-06-09 12:27:18 +02001649 int edge_triggered, level_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001650 int enabled;
1651 bool ret = true;
1652
1653 spin_lock(&dist->lock);
1654
1655 vcpu = kvm_get_vcpu(kvm, cpuid);
Christoffer Dall227844f2014-06-09 12:27:18 +02001656 edge_triggered = vgic_irq_is_edge(vcpu, irq_num);
1657 level_triggered = !edge_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001658
1659 if (!vgic_validate_injection(vcpu, irq_num, level)) {
1660 ret = false;
1661 goto out;
1662 }
1663
1664 if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
1665 cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
1666 vcpu = kvm_get_vcpu(kvm, cpuid);
1667 }
1668
1669 kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
1670
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001671 if (level) {
1672 if (level_triggered)
1673 vgic_dist_irq_set_level(vcpu, irq_num);
Christoffer Dall227844f2014-06-09 12:27:18 +02001674 vgic_dist_irq_set_pending(vcpu, irq_num);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001675 } else {
1676 if (level_triggered) {
1677 vgic_dist_irq_clear_level(vcpu, irq_num);
1678 if (!vgic_dist_irq_soft_pend(vcpu, irq_num))
1679 vgic_dist_irq_clear_pending(vcpu, irq_num);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001680 }
wanghaibin7d39f9e32014-11-17 09:27:37 +00001681
1682 ret = false;
1683 goto out;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001684 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001685
1686 enabled = vgic_irq_is_enabled(vcpu, irq_num);
1687
1688 if (!enabled) {
1689 ret = false;
1690 goto out;
1691 }
1692
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001693 if (!vgic_can_sample_irq(vcpu, irq_num)) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001694 /*
1695 * Level interrupt in progress, will be picked up
1696 * when EOId.
1697 */
1698 ret = false;
1699 goto out;
1700 }
1701
1702 if (level) {
1703 vgic_cpu_irq_set(vcpu, irq_num);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001704 set_bit(cpuid, dist->irq_pending_on_cpu);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001705 }
1706
1707out:
1708 spin_unlock(&dist->lock);
1709
Shannon Zhao016ed392014-11-19 10:11:25 +00001710 return ret ? cpuid : -EINVAL;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001711}
1712
1713/**
1714 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
1715 * @kvm: The VM structure pointer
1716 * @cpuid: The CPU for PPIs
1717 * @irq_num: The IRQ number that is assigned to the device
1718 * @level: Edge-triggered: true: to trigger the interrupt
1719 * false: to ignore the call
1720 * Level-sensitive true: activates an interrupt
1721 * false: deactivates an interrupt
1722 *
1723 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1724 * level-sensitive interrupts. You can think of the level parameter as 1
1725 * being HIGH and 0 being LOW and all devices being active-HIGH.
1726 */
1727int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
1728 bool level)
1729{
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001730 int ret = 0;
Shannon Zhao016ed392014-11-19 10:11:25 +00001731 int vcpu_id;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001732
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001733 if (unlikely(!vgic_initialized(kvm))) {
Andre Przywara598921362014-06-03 09:33:10 +02001734 /*
1735 * We only provide the automatic initialization of the VGIC
1736 * for the legacy case of a GICv2. Any other type must
1737 * be explicitly initialized once setup with the respective
1738 * KVM device call.
1739 */
1740 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2) {
1741 ret = -EBUSY;
1742 goto out;
1743 }
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001744 mutex_lock(&kvm->lock);
1745 ret = vgic_init(kvm);
1746 mutex_unlock(&kvm->lock);
1747
1748 if (ret)
1749 goto out;
Shannon Zhao016ed392014-11-19 10:11:25 +00001750 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001751
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001752 vcpu_id = vgic_update_irq_pending(kvm, cpuid, irq_num, level);
1753 if (vcpu_id >= 0) {
1754 /* kick the specified vcpu */
1755 kvm_vcpu_kick(kvm_get_vcpu(kvm, vcpu_id));
1756 }
1757
1758out:
1759 return ret;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001760}
1761
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001762static irqreturn_t vgic_maintenance_handler(int irq, void *data)
1763{
1764 /*
1765 * We cannot rely on the vgic maintenance interrupt to be
1766 * delivered synchronously. This means we can only use it to
1767 * exit the VM, and we perform the handling of EOIed
1768 * interrupts on the exit path (see vgic_process_maintenance).
1769 */
1770 return IRQ_HANDLED;
1771}
1772
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001773void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
1774{
1775 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1776
1777 kfree(vgic_cpu->pending_shared);
1778 kfree(vgic_cpu->vgic_irq_lr_map);
1779 vgic_cpu->pending_shared = NULL;
1780 vgic_cpu->vgic_irq_lr_map = NULL;
1781}
1782
1783static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
1784{
1785 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1786
1787 int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8;
1788 vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL);
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001789 vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001790
1791 if (!vgic_cpu->pending_shared || !vgic_cpu->vgic_irq_lr_map) {
1792 kvm_vgic_vcpu_destroy(vcpu);
1793 return -ENOMEM;
1794 }
1795
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001796 memset(vgic_cpu->vgic_irq_lr_map, LR_EMPTY, nr_irqs);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001797
1798 /*
Marc Zyngierca85f622013-06-18 19:17:28 +01001799 * Store the number of LRs per vcpu, so we don't have to go
1800 * all the way to the distributor structure to find out. Only
1801 * assembly code should use this one.
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001802 */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001803 vgic_cpu->nr_lr = vgic->nr_lr;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001804
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001805 return 0;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001806}
1807
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001808void kvm_vgic_destroy(struct kvm *kvm)
1809{
1810 struct vgic_dist *dist = &kvm->arch.vgic;
1811 struct kvm_vcpu *vcpu;
1812 int i;
1813
1814 kvm_for_each_vcpu(i, vcpu, kvm)
1815 kvm_vgic_vcpu_destroy(vcpu);
1816
1817 vgic_free_bitmap(&dist->irq_enabled);
1818 vgic_free_bitmap(&dist->irq_level);
1819 vgic_free_bitmap(&dist->irq_pending);
1820 vgic_free_bitmap(&dist->irq_soft_pend);
1821 vgic_free_bitmap(&dist->irq_queued);
1822 vgic_free_bitmap(&dist->irq_cfg);
1823 vgic_free_bytemap(&dist->irq_priority);
1824 if (dist->irq_spi_target) {
1825 for (i = 0; i < dist->nr_cpus; i++)
1826 vgic_free_bitmap(&dist->irq_spi_target[i]);
1827 }
1828 kfree(dist->irq_sgi_sources);
1829 kfree(dist->irq_spi_cpu);
1830 kfree(dist->irq_spi_target);
1831 kfree(dist->irq_pending_on_cpu);
1832 dist->irq_sgi_sources = NULL;
1833 dist->irq_spi_cpu = NULL;
1834 dist->irq_spi_target = NULL;
1835 dist->irq_pending_on_cpu = NULL;
Christoffer Dall1f57be22014-12-09 14:30:36 +01001836 dist->nr_cpus = 0;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001837}
1838
1839/*
1840 * Allocate and initialize the various data structures. Must be called
1841 * with kvm->lock held!
1842 */
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001843static int vgic_init(struct kvm *kvm)
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001844{
1845 struct vgic_dist *dist = &kvm->arch.vgic;
1846 struct kvm_vcpu *vcpu;
1847 int nr_cpus, nr_irqs;
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001848 int ret, i, vcpu_id;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001849
Christoffer Dall1f57be22014-12-09 14:30:36 +01001850 if (vgic_initialized(kvm))
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001851 return 0;
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001852
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001853 nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus);
1854 if (!nr_cpus) /* No vcpus? Can't be good... */
Eric Auger66b030e2014-12-15 18:43:32 +01001855 return -ENODEV;
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001856
1857 /*
1858 * If nobody configured the number of interrupts, use the
1859 * legacy one.
1860 */
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001861 if (!dist->nr_irqs)
1862 dist->nr_irqs = VGIC_NR_IRQS_LEGACY;
1863
1864 nr_irqs = dist->nr_irqs;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001865
1866 ret = vgic_init_bitmap(&dist->irq_enabled, nr_cpus, nr_irqs);
1867 ret |= vgic_init_bitmap(&dist->irq_level, nr_cpus, nr_irqs);
1868 ret |= vgic_init_bitmap(&dist->irq_pending, nr_cpus, nr_irqs);
1869 ret |= vgic_init_bitmap(&dist->irq_soft_pend, nr_cpus, nr_irqs);
1870 ret |= vgic_init_bitmap(&dist->irq_queued, nr_cpus, nr_irqs);
1871 ret |= vgic_init_bitmap(&dist->irq_cfg, nr_cpus, nr_irqs);
1872 ret |= vgic_init_bytemap(&dist->irq_priority, nr_cpus, nr_irqs);
1873
1874 if (ret)
1875 goto out;
1876
1877 dist->irq_sgi_sources = kzalloc(nr_cpus * VGIC_NR_SGIS, GFP_KERNEL);
1878 dist->irq_spi_cpu = kzalloc(nr_irqs - VGIC_NR_PRIVATE_IRQS, GFP_KERNEL);
1879 dist->irq_spi_target = kzalloc(sizeof(*dist->irq_spi_target) * nr_cpus,
1880 GFP_KERNEL);
1881 dist->irq_pending_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
1882 GFP_KERNEL);
1883 if (!dist->irq_sgi_sources ||
1884 !dist->irq_spi_cpu ||
1885 !dist->irq_spi_target ||
1886 !dist->irq_pending_on_cpu) {
1887 ret = -ENOMEM;
1888 goto out;
1889 }
1890
1891 for (i = 0; i < nr_cpus; i++)
1892 ret |= vgic_init_bitmap(&dist->irq_spi_target[i],
1893 nr_cpus, nr_irqs);
1894
1895 if (ret)
1896 goto out;
1897
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001898 for (i = VGIC_NR_PRIVATE_IRQS; i < dist->nr_irqs; i += 4)
1899 vgic_set_target_reg(kvm, 0, i);
1900
1901 kvm_for_each_vcpu(vcpu_id, vcpu, kvm) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001902 ret = vgic_vcpu_init_maps(vcpu, nr_irqs);
1903 if (ret) {
1904 kvm_err("VGIC: Failed to allocate vcpu memory\n");
1905 break;
1906 }
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001907
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001908 for (i = 0; i < dist->nr_irqs; i++) {
1909 if (i < VGIC_NR_PPIS)
1910 vgic_bitmap_set_irq_val(&dist->irq_enabled,
1911 vcpu->vcpu_id, i, 1);
1912 if (i < VGIC_NR_PRIVATE_IRQS)
1913 vgic_bitmap_set_irq_val(&dist->irq_cfg,
1914 vcpu->vcpu_id, i,
1915 VGIC_CFG_EDGE);
1916 }
1917
1918 vgic_enable(vcpu);
1919 }
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001920
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001921out:
1922 if (ret)
1923 kvm_vgic_destroy(kvm);
1924
1925 return ret;
1926}
1927
Christoffer Dalle1ba0202013-09-23 14:55:55 -07001928/**
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001929 * kvm_vgic_map_resources - Configure global VGIC state before running any VCPUs
Christoffer Dalle1ba0202013-09-23 14:55:55 -07001930 * @kvm: pointer to the kvm struct
1931 *
1932 * Map the virtual CPU interface into the VM before running any VCPUs. We
1933 * can't do this at creation time, because user space must first set the
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001934 * virtual CPU interface address in the guest physical address space.
Christoffer Dalle1ba0202013-09-23 14:55:55 -07001935 */
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001936int kvm_vgic_map_resources(struct kvm *kvm)
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001937{
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001938 int ret = 0;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001939
Christoffer Dalle1ba0202013-09-23 14:55:55 -07001940 if (!irqchip_in_kernel(kvm))
1941 return 0;
1942
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001943 mutex_lock(&kvm->lock);
1944
Christoffer Dallc52edf52014-12-09 14:28:09 +01001945 if (vgic_ready(kvm))
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001946 goto out;
1947
1948 if (IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_dist_base) ||
1949 IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_cpu_base)) {
1950 kvm_err("Need to set vgic cpu and dist addresses first\n");
1951 ret = -ENXIO;
1952 goto out;
1953 }
1954
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001955 /*
1956 * Initialize the vgic if this hasn't already been done on demand by
1957 * accessing the vgic state from userspace.
1958 */
1959 ret = vgic_init(kvm);
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001960 if (ret) {
1961 kvm_err("Unable to allocate maps\n");
1962 goto out;
1963 }
1964
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001965 ret = kvm_phys_addr_ioremap(kvm, kvm->arch.vgic.vgic_cpu_base,
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -07001966 vgic->vcpu_base, KVM_VGIC_V2_CPU_SIZE,
1967 true);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001968 if (ret) {
1969 kvm_err("Unable to remap VGIC CPU to VCPU\n");
1970 goto out;
1971 }
1972
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001973 kvm->arch.vgic.ready = true;
1974out:
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001975 if (ret)
1976 kvm_vgic_destroy(kvm);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001977 mutex_unlock(&kvm->lock);
1978 return ret;
1979}
1980
Andre Przywara598921362014-06-03 09:33:10 +02001981int kvm_vgic_create(struct kvm *kvm, u32 type)
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001982{
Christoffer Dall6b50f542014-11-06 11:47:39 +00001983 int i, vcpu_lock_idx = -1, ret;
Christoffer Dall73306722013-10-25 17:29:18 +01001984 struct kvm_vcpu *vcpu;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001985
1986 mutex_lock(&kvm->lock);
1987
Christoffer Dall73306722013-10-25 17:29:18 +01001988 if (kvm->arch.vgic.vctrl_base) {
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001989 ret = -EEXIST;
1990 goto out;
1991 }
1992
Christoffer Dall73306722013-10-25 17:29:18 +01001993 /*
1994 * Any time a vcpu is run, vcpu_load is called which tries to grab the
1995 * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
1996 * that no other VCPUs are run while we create the vgic.
1997 */
Christoffer Dall6b50f542014-11-06 11:47:39 +00001998 ret = -EBUSY;
Christoffer Dall73306722013-10-25 17:29:18 +01001999 kvm_for_each_vcpu(i, vcpu, kvm) {
2000 if (!mutex_trylock(&vcpu->mutex))
2001 goto out_unlock;
2002 vcpu_lock_idx = i;
2003 }
2004
2005 kvm_for_each_vcpu(i, vcpu, kvm) {
Christoffer Dall6b50f542014-11-06 11:47:39 +00002006 if (vcpu->arch.has_run_once)
Christoffer Dall73306722013-10-25 17:29:18 +01002007 goto out_unlock;
Christoffer Dall73306722013-10-25 17:29:18 +01002008 }
Christoffer Dall6b50f542014-11-06 11:47:39 +00002009 ret = 0;
Christoffer Dall73306722013-10-25 17:29:18 +01002010
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002011 spin_lock_init(&kvm->arch.vgic.lock);
Marc Zyngierf982cf42014-05-15 10:03:25 +01002012 kvm->arch.vgic.in_kernel = true;
Andre Przywara598921362014-06-03 09:33:10 +02002013 kvm->arch.vgic.vgic_model = type;
Marc Zyngier8f186d52014-02-04 18:13:03 +00002014 kvm->arch.vgic.vctrl_base = vgic->vctrl_base;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002015 kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
2016 kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
2017
Christoffer Dall73306722013-10-25 17:29:18 +01002018out_unlock:
2019 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
2020 vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
2021 mutex_unlock(&vcpu->mutex);
2022 }
2023
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002024out:
2025 mutex_unlock(&kvm->lock);
2026 return ret;
2027}
2028
Will Deacon1fa451b2014-08-26 15:13:24 +01002029static int vgic_ioaddr_overlap(struct kvm *kvm)
Christoffer Dall330690c2013-01-21 19:36:13 -05002030{
2031 phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
2032 phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
2033
2034 if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
2035 return 0;
2036 if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
2037 (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
2038 return -EBUSY;
2039 return 0;
2040}
2041
2042static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
2043 phys_addr_t addr, phys_addr_t size)
2044{
2045 int ret;
2046
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002047 if (addr & ~KVM_PHYS_MASK)
2048 return -E2BIG;
2049
2050 if (addr & (SZ_4K - 1))
2051 return -EINVAL;
2052
Christoffer Dall330690c2013-01-21 19:36:13 -05002053 if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
2054 return -EEXIST;
2055 if (addr + size < addr)
2056 return -EINVAL;
2057
Haibin Wang30c21172014-04-29 14:49:17 +08002058 *ioaddr = addr;
Christoffer Dall330690c2013-01-21 19:36:13 -05002059 ret = vgic_ioaddr_overlap(kvm);
2060 if (ret)
Haibin Wang30c21172014-04-29 14:49:17 +08002061 *ioaddr = VGIC_ADDR_UNDEF;
2062
Christoffer Dall330690c2013-01-21 19:36:13 -05002063 return ret;
2064}
2065
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002066/**
2067 * kvm_vgic_addr - set or get vgic VM base addresses
2068 * @kvm: pointer to the vm struct
2069 * @type: the VGIC addr type, one of KVM_VGIC_V2_ADDR_TYPE_XXX
2070 * @addr: pointer to address value
2071 * @write: if true set the address in the VM address space, if false read the
2072 * address
2073 *
2074 * Set or get the vgic base addresses for the distributor and the virtual CPU
2075 * interface in the VM physical address space. These addresses are properties
2076 * of the emulated core/SoC and therefore user space initially knows this
2077 * information.
2078 */
2079int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
Christoffer Dall330690c2013-01-21 19:36:13 -05002080{
2081 int r = 0;
2082 struct vgic_dist *vgic = &kvm->arch.vgic;
2083
Christoffer Dall330690c2013-01-21 19:36:13 -05002084 mutex_lock(&kvm->lock);
2085 switch (type) {
2086 case KVM_VGIC_V2_ADDR_TYPE_DIST:
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002087 if (write) {
2088 r = vgic_ioaddr_assign(kvm, &vgic->vgic_dist_base,
2089 *addr, KVM_VGIC_V2_DIST_SIZE);
2090 } else {
2091 *addr = vgic->vgic_dist_base;
2092 }
Christoffer Dall330690c2013-01-21 19:36:13 -05002093 break;
2094 case KVM_VGIC_V2_ADDR_TYPE_CPU:
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002095 if (write) {
2096 r = vgic_ioaddr_assign(kvm, &vgic->vgic_cpu_base,
2097 *addr, KVM_VGIC_V2_CPU_SIZE);
2098 } else {
2099 *addr = vgic->vgic_cpu_base;
2100 }
Christoffer Dall330690c2013-01-21 19:36:13 -05002101 break;
2102 default:
2103 r = -ENODEV;
2104 }
2105
2106 mutex_unlock(&kvm->lock);
2107 return r;
2108}
Christoffer Dall73306722013-10-25 17:29:18 +01002109
Christoffer Dallc07a0192013-10-25 21:17:31 +01002110static bool handle_cpu_mmio_misc(struct kvm_vcpu *vcpu,
2111 struct kvm_exit_mmio *mmio, phys_addr_t offset)
2112{
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002113 bool updated = false;
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002114 struct vgic_vmcr vmcr;
2115 u32 *vmcr_field;
2116 u32 reg;
2117
2118 vgic_get_vmcr(vcpu, &vmcr);
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002119
2120 switch (offset & ~0x3) {
2121 case GIC_CPU_CTRL:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002122 vmcr_field = &vmcr.ctlr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002123 break;
2124 case GIC_CPU_PRIMASK:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002125 vmcr_field = &vmcr.pmr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002126 break;
2127 case GIC_CPU_BINPOINT:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002128 vmcr_field = &vmcr.bpr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002129 break;
2130 case GIC_CPU_ALIAS_BINPOINT:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002131 vmcr_field = &vmcr.abpr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002132 break;
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002133 default:
2134 BUG();
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002135 }
2136
2137 if (!mmio->is_write) {
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002138 reg = *vmcr_field;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002139 mmio_data_write(mmio, ~0, reg);
2140 } else {
2141 reg = mmio_data_read(mmio, ~0);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002142 if (reg != *vmcr_field) {
2143 *vmcr_field = reg;
2144 vgic_set_vmcr(vcpu, &vmcr);
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002145 updated = true;
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002146 }
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002147 }
2148 return updated;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002149}
2150
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002151static bool handle_mmio_abpr(struct kvm_vcpu *vcpu,
2152 struct kvm_exit_mmio *mmio, phys_addr_t offset)
2153{
2154 return handle_cpu_mmio_misc(vcpu, mmio, GIC_CPU_ALIAS_BINPOINT);
2155}
2156
2157static bool handle_cpu_mmio_ident(struct kvm_vcpu *vcpu,
2158 struct kvm_exit_mmio *mmio,
2159 phys_addr_t offset)
2160{
2161 u32 reg;
2162
2163 if (mmio->is_write)
2164 return false;
2165
2166 /* GICC_IIDR */
2167 reg = (PRODUCT_ID_KVM << 20) |
2168 (GICC_ARCH_VERSION_V2 << 16) |
2169 (IMPLEMENTER_ARM << 0);
2170 mmio_data_write(mmio, ~0, reg);
2171 return false;
2172}
2173
2174/*
2175 * CPU Interface Register accesses - these are not accessed by the VM, but by
2176 * user space for saving and restoring VGIC state.
2177 */
Christoffer Dallc07a0192013-10-25 21:17:31 +01002178static const struct mmio_range vgic_cpu_ranges[] = {
2179 {
2180 .base = GIC_CPU_CTRL,
2181 .len = 12,
2182 .handle_mmio = handle_cpu_mmio_misc,
2183 },
2184 {
2185 .base = GIC_CPU_ALIAS_BINPOINT,
2186 .len = 4,
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002187 .handle_mmio = handle_mmio_abpr,
Christoffer Dallc07a0192013-10-25 21:17:31 +01002188 },
2189 {
2190 .base = GIC_CPU_ACTIVEPRIO,
2191 .len = 16,
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002192 .handle_mmio = handle_mmio_raz_wi,
Christoffer Dallc07a0192013-10-25 21:17:31 +01002193 },
2194 {
2195 .base = GIC_CPU_IDENT,
2196 .len = 4,
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002197 .handle_mmio = handle_cpu_mmio_ident,
Christoffer Dallc07a0192013-10-25 21:17:31 +01002198 },
2199};
2200
2201static int vgic_attr_regs_access(struct kvm_device *dev,
2202 struct kvm_device_attr *attr,
2203 u32 *reg, bool is_write)
2204{
2205 const struct mmio_range *r = NULL, *ranges;
2206 phys_addr_t offset;
2207 int ret, cpuid, c;
2208 struct kvm_vcpu *vcpu, *tmp_vcpu;
2209 struct vgic_dist *vgic;
2210 struct kvm_exit_mmio mmio;
2211
2212 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2213 cpuid = (attr->attr & KVM_DEV_ARM_VGIC_CPUID_MASK) >>
2214 KVM_DEV_ARM_VGIC_CPUID_SHIFT;
2215
2216 mutex_lock(&dev->kvm->lock);
2217
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00002218 ret = vgic_init(dev->kvm);
Marc Zyngier4956f2b2014-07-08 12:09:06 +01002219 if (ret)
2220 goto out;
2221
Christoffer Dallc07a0192013-10-25 21:17:31 +01002222 if (cpuid >= atomic_read(&dev->kvm->online_vcpus)) {
2223 ret = -EINVAL;
2224 goto out;
2225 }
2226
2227 vcpu = kvm_get_vcpu(dev->kvm, cpuid);
2228 vgic = &dev->kvm->arch.vgic;
2229
2230 mmio.len = 4;
2231 mmio.is_write = is_write;
2232 if (is_write)
2233 mmio_data_write(&mmio, ~0, *reg);
2234 switch (attr->group) {
2235 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2236 mmio.phys_addr = vgic->vgic_dist_base + offset;
2237 ranges = vgic_dist_ranges;
2238 break;
2239 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
2240 mmio.phys_addr = vgic->vgic_cpu_base + offset;
2241 ranges = vgic_cpu_ranges;
2242 break;
2243 default:
2244 BUG();
2245 }
2246 r = find_matching_range(ranges, &mmio, offset);
2247
2248 if (unlikely(!r || !r->handle_mmio)) {
2249 ret = -ENXIO;
2250 goto out;
2251 }
2252
2253
2254 spin_lock(&vgic->lock);
2255
2256 /*
2257 * Ensure that no other VCPU is running by checking the vcpu->cpu
2258 * field. If no other VPCUs are running we can safely access the VGIC
2259 * state, because even if another VPU is run after this point, that
2260 * VCPU will not touch the vgic state, because it will block on
2261 * getting the vgic->lock in kvm_vgic_sync_hwstate().
2262 */
2263 kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm) {
2264 if (unlikely(tmp_vcpu->cpu != -1)) {
2265 ret = -EBUSY;
2266 goto out_vgic_unlock;
2267 }
2268 }
2269
Christoffer Dallcbd333a2013-11-15 20:51:31 -08002270 /*
2271 * Move all pending IRQs from the LRs on all VCPUs so the pending
2272 * state can be properly represented in the register state accessible
2273 * through this API.
2274 */
2275 kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm)
2276 vgic_unqueue_irqs(tmp_vcpu);
2277
Christoffer Dallc07a0192013-10-25 21:17:31 +01002278 offset -= r->base;
2279 r->handle_mmio(vcpu, &mmio, offset);
2280
2281 if (!is_write)
2282 *reg = mmio_data_read(&mmio, ~0);
2283
2284 ret = 0;
2285out_vgic_unlock:
2286 spin_unlock(&vgic->lock);
2287out:
2288 mutex_unlock(&dev->kvm->lock);
2289 return ret;
2290}
2291
Christoffer Dall73306722013-10-25 17:29:18 +01002292static int vgic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2293{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002294 int r;
2295
2296 switch (attr->group) {
2297 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2298 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2299 u64 addr;
2300 unsigned long type = (unsigned long)attr->attr;
2301
2302 if (copy_from_user(&addr, uaddr, sizeof(addr)))
2303 return -EFAULT;
2304
2305 r = kvm_vgic_addr(dev->kvm, type, &addr, true);
2306 return (r == -ENODEV) ? -ENXIO : r;
2307 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01002308
2309 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2310 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
2311 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2312 u32 reg;
2313
2314 if (get_user(reg, uaddr))
2315 return -EFAULT;
2316
2317 return vgic_attr_regs_access(dev, attr, &reg, true);
2318 }
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002319 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2320 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2321 u32 val;
2322 int ret = 0;
2323
2324 if (get_user(val, uaddr))
2325 return -EFAULT;
2326
2327 /*
2328 * We require:
2329 * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs
2330 * - at most 1024 interrupts
2331 * - a multiple of 32 interrupts
2332 */
2333 if (val < (VGIC_NR_PRIVATE_IRQS + 32) ||
2334 val > VGIC_MAX_IRQS ||
2335 (val & 31))
2336 return -EINVAL;
2337
2338 mutex_lock(&dev->kvm->lock);
2339
Christoffer Dallc52edf52014-12-09 14:28:09 +01002340 if (vgic_ready(dev->kvm) || dev->kvm->arch.vgic.nr_irqs)
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002341 ret = -EBUSY;
2342 else
2343 dev->kvm->arch.vgic.nr_irqs = val;
2344
2345 mutex_unlock(&dev->kvm->lock);
2346
2347 return ret;
2348 }
Eric Auger065c0032014-12-15 18:43:33 +01002349 case KVM_DEV_ARM_VGIC_GRP_CTRL: {
2350 switch (attr->attr) {
2351 case KVM_DEV_ARM_VGIC_CTRL_INIT:
2352 r = vgic_init(dev->kvm);
2353 return r;
2354 }
2355 break;
2356 }
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002357 }
2358
Christoffer Dall73306722013-10-25 17:29:18 +01002359 return -ENXIO;
2360}
2361
2362static int vgic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2363{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002364 int r = -ENXIO;
2365
2366 switch (attr->group) {
2367 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2368 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2369 u64 addr;
2370 unsigned long type = (unsigned long)attr->attr;
2371
2372 r = kvm_vgic_addr(dev->kvm, type, &addr, false);
2373 if (r)
2374 return (r == -ENODEV) ? -ENXIO : r;
2375
2376 if (copy_to_user(uaddr, &addr, sizeof(addr)))
2377 return -EFAULT;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002378 break;
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002379 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01002380
2381 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2382 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
2383 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2384 u32 reg = 0;
2385
2386 r = vgic_attr_regs_access(dev, attr, &reg, false);
2387 if (r)
2388 return r;
2389 r = put_user(reg, uaddr);
2390 break;
2391 }
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002392 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2393 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2394 r = put_user(dev->kvm->arch.vgic.nr_irqs, uaddr);
2395 break;
2396 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01002397
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002398 }
2399
2400 return r;
Christoffer Dall73306722013-10-25 17:29:18 +01002401}
2402
Christoffer Dallc07a0192013-10-25 21:17:31 +01002403static int vgic_has_attr_regs(const struct mmio_range *ranges,
2404 phys_addr_t offset)
2405{
2406 struct kvm_exit_mmio dev_attr_mmio;
2407
2408 dev_attr_mmio.len = 4;
2409 if (find_matching_range(ranges, &dev_attr_mmio, offset))
2410 return 0;
2411 else
2412 return -ENXIO;
2413}
2414
Christoffer Dall73306722013-10-25 17:29:18 +01002415static int vgic_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2416{
Christoffer Dallc07a0192013-10-25 21:17:31 +01002417 phys_addr_t offset;
2418
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002419 switch (attr->group) {
2420 case KVM_DEV_ARM_VGIC_GRP_ADDR:
2421 switch (attr->attr) {
2422 case KVM_VGIC_V2_ADDR_TYPE_DIST:
2423 case KVM_VGIC_V2_ADDR_TYPE_CPU:
2424 return 0;
2425 }
2426 break;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002427 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2428 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2429 return vgic_has_attr_regs(vgic_dist_ranges, offset);
2430 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
2431 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2432 return vgic_has_attr_regs(vgic_cpu_ranges, offset);
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002433 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS:
2434 return 0;
Eric Auger065c0032014-12-15 18:43:33 +01002435 case KVM_DEV_ARM_VGIC_GRP_CTRL:
2436 switch (attr->attr) {
2437 case KVM_DEV_ARM_VGIC_CTRL_INIT:
2438 return 0;
2439 }
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002440 }
Christoffer Dall73306722013-10-25 17:29:18 +01002441 return -ENXIO;
2442}
2443
2444static void vgic_destroy(struct kvm_device *dev)
2445{
2446 kfree(dev);
2447}
2448
2449static int vgic_create(struct kvm_device *dev, u32 type)
2450{
Andre Przywara598921362014-06-03 09:33:10 +02002451 return kvm_vgic_create(dev->kvm, type);
Christoffer Dall73306722013-10-25 17:29:18 +01002452}
2453
Will Deaconc06a8412014-09-02 10:27:34 +01002454static struct kvm_device_ops kvm_arm_vgic_v2_ops = {
Christoffer Dall73306722013-10-25 17:29:18 +01002455 .name = "kvm-arm-vgic",
2456 .create = vgic_create,
2457 .destroy = vgic_destroy,
2458 .set_attr = vgic_set_attr,
2459 .get_attr = vgic_get_attr,
2460 .has_attr = vgic_has_attr,
2461};
Will Deaconc06a8412014-09-02 10:27:34 +01002462
2463static void vgic_init_maintenance_interrupt(void *info)
2464{
2465 enable_percpu_irq(vgic->maint_irq, 0);
2466}
2467
2468static int vgic_cpu_notify(struct notifier_block *self,
2469 unsigned long action, void *cpu)
2470{
2471 switch (action) {
2472 case CPU_STARTING:
2473 case CPU_STARTING_FROZEN:
2474 vgic_init_maintenance_interrupt(NULL);
2475 break;
2476 case CPU_DYING:
2477 case CPU_DYING_FROZEN:
2478 disable_percpu_irq(vgic->maint_irq);
2479 break;
2480 }
2481
2482 return NOTIFY_OK;
2483}
2484
2485static struct notifier_block vgic_cpu_nb = {
2486 .notifier_call = vgic_cpu_notify,
2487};
2488
2489static const struct of_device_id vgic_ids[] = {
2490 { .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, },
2491 { .compatible = "arm,gic-v3", .data = vgic_v3_probe, },
2492 {},
2493};
2494
2495int kvm_vgic_hyp_init(void)
2496{
2497 const struct of_device_id *matched_id;
Christoffer Dalla875daf2014-09-18 18:15:32 -07002498 const int (*vgic_probe)(struct device_node *,const struct vgic_ops **,
2499 const struct vgic_params **);
Will Deaconc06a8412014-09-02 10:27:34 +01002500 struct device_node *vgic_node;
2501 int ret;
2502
2503 vgic_node = of_find_matching_node_and_match(NULL,
2504 vgic_ids, &matched_id);
2505 if (!vgic_node) {
2506 kvm_err("error: no compatible GIC node found\n");
2507 return -ENODEV;
2508 }
2509
2510 vgic_probe = matched_id->data;
2511 ret = vgic_probe(vgic_node, &vgic_ops, &vgic);
2512 if (ret)
2513 return ret;
2514
2515 ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler,
2516 "vgic", kvm_get_running_vcpus());
2517 if (ret) {
2518 kvm_err("Cannot register interrupt %d\n", vgic->maint_irq);
2519 return ret;
2520 }
2521
2522 ret = __register_cpu_notifier(&vgic_cpu_nb);
2523 if (ret) {
2524 kvm_err("Cannot register vgic CPU notifier\n");
2525 goto out_free_irq;
2526 }
2527
2528 /* Callback into for arch code for setup */
2529 vgic_arch_setup(vgic);
2530
2531 on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
2532
2533 return kvm_register_device_ops(&kvm_arm_vgic_v2_ops,
2534 KVM_DEV_TYPE_ARM_VGIC_V2);
2535
2536out_free_irq:
2537 free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus());
2538 return ret;
2539}