blob: e373b76c54201c056fcb9ed8e07724913958a35a [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/**
1037 * vgic_handle_mmio - handle an in-kernel MMIO access
1038 * @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
1041 *
1042 * returns true if the MMIO access has been performed in kernel space,
1043 * and false if it needs to be emulated in user space.
1044 */
1045bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
1046 struct kvm_exit_mmio *mmio)
1047{
Marc Zyngierb47ef922013-01-21 19:36:14 -05001048 const struct mmio_range *range;
1049 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1050 unsigned long base = dist->vgic_dist_base;
1051 bool updated_state;
1052 unsigned long offset;
1053
1054 if (!irqchip_in_kernel(vcpu->kvm) ||
1055 mmio->phys_addr < base ||
1056 (mmio->phys_addr + mmio->len) > (base + KVM_VGIC_V2_DIST_SIZE))
1057 return false;
1058
1059 /* We don't support ldrd / strd or ldm / stm to the emulated vgic */
1060 if (mmio->len > 4) {
1061 kvm_inject_dabt(vcpu, mmio->phys_addr);
1062 return true;
1063 }
1064
Christoffer Dall1006e8c2013-09-23 14:55:56 -07001065 offset = mmio->phys_addr - base;
1066 range = find_matching_range(vgic_dist_ranges, mmio, offset);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001067 if (unlikely(!range || !range->handle_mmio)) {
1068 pr_warn("Unhandled access %d %08llx %d\n",
1069 mmio->is_write, mmio->phys_addr, mmio->len);
1070 return false;
1071 }
1072
1073 spin_lock(&vcpu->kvm->arch.vgic.lock);
1074 offset = mmio->phys_addr - range->base - base;
Marc Zyngierc3c91832014-07-08 12:09:04 +01001075 if (vgic_validate_access(dist, range, offset)) {
1076 updated_state = range->handle_mmio(vcpu, mmio, offset);
1077 } else {
1078 vgic_reg_access(mmio, NULL, offset,
1079 ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
1080 updated_state = false;
1081 }
Marc Zyngierb47ef922013-01-21 19:36:14 -05001082 spin_unlock(&vcpu->kvm->arch.vgic.lock);
1083 kvm_prepare_mmio(run, mmio);
1084 kvm_handle_mmio_return(vcpu, run);
1085
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001086 if (updated_state)
1087 vgic_kick_vcpus(vcpu->kvm);
1088
Marc Zyngierb47ef922013-01-21 19:36:14 -05001089 return true;
1090}
1091
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001092static u8 *vgic_get_sgi_sources(struct vgic_dist *dist, int vcpu_id, int sgi)
1093{
1094 return dist->irq_sgi_sources + vcpu_id * VGIC_NR_SGIS + sgi;
1095}
1096
Marc Zyngierb47ef922013-01-21 19:36:14 -05001097static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg)
1098{
1099 struct kvm *kvm = vcpu->kvm;
1100 struct vgic_dist *dist = &kvm->arch.vgic;
1101 int nrcpus = atomic_read(&kvm->online_vcpus);
1102 u8 target_cpus;
1103 int sgi, mode, c, vcpu_id;
1104
1105 vcpu_id = vcpu->vcpu_id;
1106
1107 sgi = reg & 0xf;
1108 target_cpus = (reg >> 16) & 0xff;
1109 mode = (reg >> 24) & 3;
1110
1111 switch (mode) {
1112 case 0:
1113 if (!target_cpus)
1114 return;
Haibin Wang91021a62014-04-10 13:14:32 +01001115 break;
Marc Zyngierb47ef922013-01-21 19:36:14 -05001116
1117 case 1:
1118 target_cpus = ((1 << nrcpus) - 1) & ~(1 << vcpu_id) & 0xff;
1119 break;
1120
1121 case 2:
1122 target_cpus = 1 << vcpu_id;
1123 break;
1124 }
1125
1126 kvm_for_each_vcpu(c, vcpu, kvm) {
1127 if (target_cpus & 1) {
1128 /* Flag the SGI as pending */
Christoffer Dall227844f2014-06-09 12:27:18 +02001129 vgic_dist_irq_set_pending(vcpu, sgi);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001130 *vgic_get_sgi_sources(dist, c, sgi) |= 1 << vcpu_id;
Marc Zyngierb47ef922013-01-21 19:36:14 -05001131 kvm_debug("SGI%d from CPU%d to CPU%d\n", sgi, vcpu_id, c);
1132 }
1133
1134 target_cpus >>= 1;
1135 }
1136}
1137
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001138static int vgic_nr_shared_irqs(struct vgic_dist *dist)
1139{
1140 return dist->nr_irqs - VGIC_NR_PRIVATE_IRQS;
1141}
1142
Marc Zyngierb47ef922013-01-21 19:36:14 -05001143static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
1144{
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001145 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1146 unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
1147 unsigned long pending_private, pending_shared;
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001148 int nr_shared = vgic_nr_shared_irqs(dist);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001149 int vcpu_id;
1150
1151 vcpu_id = vcpu->vcpu_id;
1152 pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
1153 pend_shared = vcpu->arch.vgic_cpu.pending_shared;
1154
Christoffer Dall227844f2014-06-09 12:27:18 +02001155 pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001156 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
1157 bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
1158
Christoffer Dall227844f2014-06-09 12:27:18 +02001159 pending = vgic_bitmap_get_shared_map(&dist->irq_pending);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001160 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001161 bitmap_and(pend_shared, pending, enabled, nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001162 bitmap_and(pend_shared, pend_shared,
1163 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001164 nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001165
1166 pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001167 pending_shared = find_first_bit(pend_shared, nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001168 return (pending_private < VGIC_NR_PRIVATE_IRQS ||
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001169 pending_shared < vgic_nr_shared_irqs(dist));
Marc Zyngierb47ef922013-01-21 19:36:14 -05001170}
1171
1172/*
1173 * Update the interrupt state and determine which CPUs have pending
1174 * interrupts. Must be called with distributor lock held.
1175 */
1176static void vgic_update_state(struct kvm *kvm)
1177{
1178 struct vgic_dist *dist = &kvm->arch.vgic;
1179 struct kvm_vcpu *vcpu;
1180 int c;
1181
1182 if (!dist->enabled) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001183 set_bit(0, dist->irq_pending_on_cpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001184 return;
1185 }
1186
1187 kvm_for_each_vcpu(c, vcpu, kvm) {
1188 if (compute_pending_for_cpu(vcpu)) {
1189 pr_debug("CPU%d has pending interrupts\n", c);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001190 set_bit(c, dist->irq_pending_on_cpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001191 }
1192 }
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001193}
Christoffer Dall330690c2013-01-21 19:36:13 -05001194
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001195static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr)
1196{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001197 return vgic_ops->get_lr(vcpu, lr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001198}
1199
1200static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr,
1201 struct vgic_lr vlr)
1202{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001203 vgic_ops->set_lr(vcpu, lr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001204}
1205
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001206static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
1207 struct vgic_lr vlr)
1208{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001209 vgic_ops->sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001210}
1211
1212static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu)
1213{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001214 return vgic_ops->get_elrsr(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001215}
1216
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001217static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu)
1218{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001219 return vgic_ops->get_eisr(vcpu);
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001220}
1221
Marc Zyngier495dd852013-06-04 11:02:10 +01001222static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu)
1223{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001224 return vgic_ops->get_interrupt_status(vcpu);
Marc Zyngier495dd852013-06-04 11:02:10 +01001225}
1226
Marc Zyngier909d9b52013-06-04 11:24:17 +01001227static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu)
1228{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001229 vgic_ops->enable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001230}
1231
1232static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu)
1233{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001234 vgic_ops->disable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001235}
1236
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001237static inline void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1238{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001239 vgic_ops->get_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001240}
1241
1242static void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1243{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001244 vgic_ops->set_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001245}
1246
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001247static inline void vgic_enable(struct kvm_vcpu *vcpu)
1248{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001249 vgic_ops->enable(vcpu);
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001250}
1251
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001252static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu)
1253{
1254 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1255 struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr);
1256
1257 vlr.state = 0;
1258 vgic_set_lr(vcpu, lr_nr, vlr);
1259 clear_bit(lr_nr, vgic_cpu->lr_used);
1260 vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
1261}
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001262
1263/*
1264 * An interrupt may have been disabled after being made pending on the
1265 * CPU interface (the classic case is a timer running while we're
1266 * rebooting the guest - the interrupt would kick as soon as the CPU
1267 * interface gets enabled, with deadly consequences).
1268 *
1269 * The solution is to examine already active LRs, and check the
1270 * interrupt is still enabled. If not, just retire it.
1271 */
1272static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
1273{
1274 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1275 int lr;
1276
Marc Zyngier8f186d52014-02-04 18:13:03 +00001277 for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001278 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001279
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001280 if (!vgic_irq_is_enabled(vcpu, vlr.irq)) {
1281 vgic_retire_lr(lr, vlr.irq, vcpu);
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001282 if (vgic_irq_is_queued(vcpu, vlr.irq))
1283 vgic_irq_clear_queued(vcpu, vlr.irq);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001284 }
1285 }
1286}
1287
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001288/*
1289 * Queue an interrupt to a CPU virtual interface. Return true on success,
1290 * or false if it wasn't possible to queue it.
1291 */
1292static bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
1293{
1294 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001295 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001296 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001297 int lr;
1298
1299 /* Sanitize the input... */
1300 BUG_ON(sgi_source_id & ~7);
1301 BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001302 BUG_ON(irq >= dist->nr_irqs);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001303
1304 kvm_debug("Queue IRQ%d\n", irq);
1305
1306 lr = vgic_cpu->vgic_irq_lr_map[irq];
1307
1308 /* Do we have an active interrupt for the same CPUID? */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001309 if (lr != LR_EMPTY) {
1310 vlr = vgic_get_lr(vcpu, lr);
1311 if (vlr.source == sgi_source_id) {
1312 kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq);
1313 BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
1314 vlr.state |= LR_STATE_PENDING;
1315 vgic_set_lr(vcpu, lr, vlr);
1316 return true;
1317 }
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001318 }
1319
1320 /* Try to use another LR for this interrupt */
1321 lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
Marc Zyngier8f186d52014-02-04 18:13:03 +00001322 vgic->nr_lr);
1323 if (lr >= vgic->nr_lr)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001324 return false;
1325
1326 kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001327 vgic_cpu->vgic_irq_lr_map[irq] = lr;
1328 set_bit(lr, vgic_cpu->lr_used);
1329
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001330 vlr.irq = irq;
1331 vlr.source = sgi_source_id;
1332 vlr.state = LR_STATE_PENDING;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001333 if (!vgic_irq_is_edge(vcpu, irq))
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001334 vlr.state |= LR_EOI_INT;
1335
1336 vgic_set_lr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001337
1338 return true;
1339}
1340
1341static bool vgic_queue_sgi(struct kvm_vcpu *vcpu, int irq)
1342{
1343 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1344 unsigned long sources;
1345 int vcpu_id = vcpu->vcpu_id;
1346 int c;
1347
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001348 sources = *vgic_get_sgi_sources(dist, vcpu_id, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001349
Marc Zyngierfc675e32014-07-08 12:09:03 +01001350 for_each_set_bit(c, &sources, dist->nr_cpus) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001351 if (vgic_queue_irq(vcpu, c, irq))
1352 clear_bit(c, &sources);
1353 }
1354
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001355 *vgic_get_sgi_sources(dist, vcpu_id, irq) = sources;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001356
1357 /*
1358 * If the sources bitmap has been cleared it means that we
1359 * could queue all the SGIs onto link registers (see the
1360 * clear_bit above), and therefore we are done with them in
1361 * our emulated gic and can get rid of them.
1362 */
1363 if (!sources) {
Christoffer Dall227844f2014-06-09 12:27:18 +02001364 vgic_dist_irq_clear_pending(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001365 vgic_cpu_irq_clear(vcpu, irq);
1366 return true;
1367 }
1368
1369 return false;
1370}
1371
1372static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
1373{
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001374 if (!vgic_can_sample_irq(vcpu, irq))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001375 return true; /* level interrupt, already queued */
1376
1377 if (vgic_queue_irq(vcpu, 0, irq)) {
1378 if (vgic_irq_is_edge(vcpu, irq)) {
Christoffer Dall227844f2014-06-09 12:27:18 +02001379 vgic_dist_irq_clear_pending(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001380 vgic_cpu_irq_clear(vcpu, irq);
1381 } else {
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001382 vgic_irq_set_queued(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001383 }
1384
1385 return true;
1386 }
1387
1388 return false;
1389}
1390
1391/*
1392 * Fill the list registers with pending interrupts before running the
1393 * guest.
1394 */
1395static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1396{
1397 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1398 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1399 int i, vcpu_id;
1400 int overflow = 0;
1401
1402 vcpu_id = vcpu->vcpu_id;
1403
1404 /*
1405 * We may not have any pending interrupt, or the interrupts
1406 * may have been serviced from another vcpu. In all cases,
1407 * move along.
1408 */
1409 if (!kvm_vgic_vcpu_pending_irq(vcpu)) {
1410 pr_debug("CPU%d has no pending interrupt\n", vcpu_id);
1411 goto epilog;
1412 }
1413
1414 /* SGIs */
1415 for_each_set_bit(i, vgic_cpu->pending_percpu, VGIC_NR_SGIS) {
1416 if (!vgic_queue_sgi(vcpu, i))
1417 overflow = 1;
1418 }
1419
1420 /* PPIs */
1421 for_each_set_bit_from(i, vgic_cpu->pending_percpu, VGIC_NR_PRIVATE_IRQS) {
1422 if (!vgic_queue_hwirq(vcpu, i))
1423 overflow = 1;
1424 }
1425
1426 /* SPIs */
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001427 for_each_set_bit(i, vgic_cpu->pending_shared, vgic_nr_shared_irqs(dist)) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001428 if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
1429 overflow = 1;
1430 }
1431
1432epilog:
1433 if (overflow) {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001434 vgic_enable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001435 } else {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001436 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001437 /*
1438 * We're about to run this VCPU, and we've consumed
1439 * everything the distributor had in store for
1440 * us. Claim we don't have anything pending. We'll
1441 * adjust that if needed while exiting.
1442 */
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001443 clear_bit(vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001444 }
1445}
1446
1447static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1448{
Marc Zyngier495dd852013-06-04 11:02:10 +01001449 u32 status = vgic_get_interrupt_status(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001450 bool level_pending = false;
1451
Marc Zyngier495dd852013-06-04 11:02:10 +01001452 kvm_debug("STATUS = %08x\n", status);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001453
Marc Zyngier495dd852013-06-04 11:02:10 +01001454 if (status & INT_STATUS_EOI) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001455 /*
1456 * Some level interrupts have been EOIed. Clear their
1457 * active bit.
1458 */
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001459 u64 eisr = vgic_get_eisr(vcpu);
Christoffer Dall2df36a52014-09-28 16:04:26 +02001460 unsigned long *eisr_ptr = u64_to_bitmask(&eisr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001461 int lr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001462
Marc Zyngier8f186d52014-02-04 18:13:03 +00001463 for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001464 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001465 WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001466
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001467 vgic_irq_clear_queued(vcpu, vlr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001468 WARN_ON(vlr.state & LR_STATE_MASK);
1469 vlr.state = 0;
1470 vgic_set_lr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001471
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001472 /*
1473 * If the IRQ was EOIed it was also ACKed and we we
1474 * therefore assume we can clear the soft pending
1475 * state (should it had been set) for this interrupt.
1476 *
1477 * Note: if the IRQ soft pending state was set after
1478 * the IRQ was acked, it actually shouldn't be
1479 * cleared, but we have no way of knowing that unless
1480 * we start trapping ACKs when the soft-pending state
1481 * is set.
1482 */
1483 vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
1484
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001485 /* Any additional pending interrupt? */
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001486 if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001487 vgic_cpu_irq_set(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001488 level_pending = true;
1489 } else {
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001490 vgic_dist_irq_clear_pending(vcpu, vlr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001491 vgic_cpu_irq_clear(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001492 }
Marc Zyngier75da01e2013-01-31 11:25:52 +00001493
1494 /*
1495 * Despite being EOIed, the LR may not have
1496 * been marked as empty.
1497 */
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001498 vgic_sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001499 }
1500 }
1501
Marc Zyngier495dd852013-06-04 11:02:10 +01001502 if (status & INT_STATUS_UNDERFLOW)
Marc Zyngier909d9b52013-06-04 11:24:17 +01001503 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001504
1505 return level_pending;
1506}
1507
1508/*
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001509 * Sync back the VGIC state after a guest run. The distributor lock is
1510 * needed so we don't get preempted in the middle of the state processing.
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001511 */
1512static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1513{
1514 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1515 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001516 u64 elrsr;
1517 unsigned long *elrsr_ptr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001518 int lr, pending;
1519 bool level_pending;
1520
1521 level_pending = vgic_process_maintenance(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001522 elrsr = vgic_get_elrsr(vcpu);
Christoffer Dall2df36a52014-09-28 16:04:26 +02001523 elrsr_ptr = u64_to_bitmask(&elrsr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001524
1525 /* Clear mappings for empty LRs */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001526 for_each_set_bit(lr, elrsr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001527 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001528
1529 if (!test_and_clear_bit(lr, vgic_cpu->lr_used))
1530 continue;
1531
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001532 vlr = vgic_get_lr(vcpu, lr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001533
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001534 BUG_ON(vlr.irq >= dist->nr_irqs);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001535 vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001536 }
1537
1538 /* Check if we still have something up our sleeve... */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001539 pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr);
1540 if (level_pending || pending < vgic->nr_lr)
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001541 set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001542}
1543
1544void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1545{
1546 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1547
1548 if (!irqchip_in_kernel(vcpu->kvm))
1549 return;
1550
1551 spin_lock(&dist->lock);
1552 __kvm_vgic_flush_hwstate(vcpu);
1553 spin_unlock(&dist->lock);
1554}
1555
1556void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1557{
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001558 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1559
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001560 if (!irqchip_in_kernel(vcpu->kvm))
1561 return;
1562
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001563 spin_lock(&dist->lock);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001564 __kvm_vgic_sync_hwstate(vcpu);
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001565 spin_unlock(&dist->lock);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001566}
1567
1568int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
1569{
1570 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1571
1572 if (!irqchip_in_kernel(vcpu->kvm))
1573 return 0;
1574
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001575 return test_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001576}
1577
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001578static void vgic_kick_vcpus(struct kvm *kvm)
1579{
1580 struct kvm_vcpu *vcpu;
1581 int c;
1582
1583 /*
1584 * We've injected an interrupt, time to find out who deserves
1585 * a good kick...
1586 */
1587 kvm_for_each_vcpu(c, vcpu, kvm) {
1588 if (kvm_vgic_vcpu_pending_irq(vcpu))
1589 kvm_vcpu_kick(vcpu);
1590 }
1591}
1592
1593static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
1594{
Christoffer Dall227844f2014-06-09 12:27:18 +02001595 int edge_triggered = vgic_irq_is_edge(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001596
1597 /*
1598 * Only inject an interrupt if:
1599 * - edge triggered and we have a rising edge
1600 * - level triggered and we change level
1601 */
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001602 if (edge_triggered) {
1603 int state = vgic_dist_irq_is_pending(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001604 return level > state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001605 } else {
1606 int state = vgic_dist_irq_get_level(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001607 return level != state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001608 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001609}
1610
Shannon Zhao016ed392014-11-19 10:11:25 +00001611static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001612 unsigned int irq_num, bool level)
1613{
1614 struct vgic_dist *dist = &kvm->arch.vgic;
1615 struct kvm_vcpu *vcpu;
Christoffer Dall227844f2014-06-09 12:27:18 +02001616 int edge_triggered, level_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001617 int enabled;
1618 bool ret = true;
1619
1620 spin_lock(&dist->lock);
1621
1622 vcpu = kvm_get_vcpu(kvm, cpuid);
Christoffer Dall227844f2014-06-09 12:27:18 +02001623 edge_triggered = vgic_irq_is_edge(vcpu, irq_num);
1624 level_triggered = !edge_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001625
1626 if (!vgic_validate_injection(vcpu, irq_num, level)) {
1627 ret = false;
1628 goto out;
1629 }
1630
1631 if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
1632 cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
1633 vcpu = kvm_get_vcpu(kvm, cpuid);
1634 }
1635
1636 kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
1637
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001638 if (level) {
1639 if (level_triggered)
1640 vgic_dist_irq_set_level(vcpu, irq_num);
Christoffer Dall227844f2014-06-09 12:27:18 +02001641 vgic_dist_irq_set_pending(vcpu, irq_num);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001642 } else {
1643 if (level_triggered) {
1644 vgic_dist_irq_clear_level(vcpu, irq_num);
1645 if (!vgic_dist_irq_soft_pend(vcpu, irq_num))
1646 vgic_dist_irq_clear_pending(vcpu, irq_num);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001647 }
wanghaibin7d39f9e32014-11-17 09:27:37 +00001648
1649 ret = false;
1650 goto out;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001651 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001652
1653 enabled = vgic_irq_is_enabled(vcpu, irq_num);
1654
1655 if (!enabled) {
1656 ret = false;
1657 goto out;
1658 }
1659
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001660 if (!vgic_can_sample_irq(vcpu, irq_num)) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001661 /*
1662 * Level interrupt in progress, will be picked up
1663 * when EOId.
1664 */
1665 ret = false;
1666 goto out;
1667 }
1668
1669 if (level) {
1670 vgic_cpu_irq_set(vcpu, irq_num);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001671 set_bit(cpuid, dist->irq_pending_on_cpu);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001672 }
1673
1674out:
1675 spin_unlock(&dist->lock);
1676
Shannon Zhao016ed392014-11-19 10:11:25 +00001677 return ret ? cpuid : -EINVAL;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001678}
1679
1680/**
1681 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
1682 * @kvm: The VM structure pointer
1683 * @cpuid: The CPU for PPIs
1684 * @irq_num: The IRQ number that is assigned to the device
1685 * @level: Edge-triggered: true: to trigger the interrupt
1686 * false: to ignore the call
1687 * Level-sensitive true: activates an interrupt
1688 * false: deactivates an interrupt
1689 *
1690 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1691 * level-sensitive interrupts. You can think of the level parameter as 1
1692 * being HIGH and 0 being LOW and all devices being active-HIGH.
1693 */
1694int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
1695 bool level)
1696{
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001697 int ret = 0;
Shannon Zhao016ed392014-11-19 10:11:25 +00001698 int vcpu_id;
1699
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001700 if (unlikely(!vgic_initialized(kvm))) {
1701 mutex_lock(&kvm->lock);
1702 ret = vgic_init(kvm);
1703 mutex_unlock(&kvm->lock);
1704
1705 if (ret)
1706 goto out;
Shannon Zhao016ed392014-11-19 10:11:25 +00001707 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001708
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001709 vcpu_id = vgic_update_irq_pending(kvm, cpuid, irq_num, level);
1710 if (vcpu_id >= 0) {
1711 /* kick the specified vcpu */
1712 kvm_vcpu_kick(kvm_get_vcpu(kvm, vcpu_id));
1713 }
1714
1715out:
1716 return ret;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001717}
1718
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001719static irqreturn_t vgic_maintenance_handler(int irq, void *data)
1720{
1721 /*
1722 * We cannot rely on the vgic maintenance interrupt to be
1723 * delivered synchronously. This means we can only use it to
1724 * exit the VM, and we perform the handling of EOIed
1725 * interrupts on the exit path (see vgic_process_maintenance).
1726 */
1727 return IRQ_HANDLED;
1728}
1729
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001730void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
1731{
1732 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1733
1734 kfree(vgic_cpu->pending_shared);
1735 kfree(vgic_cpu->vgic_irq_lr_map);
1736 vgic_cpu->pending_shared = NULL;
1737 vgic_cpu->vgic_irq_lr_map = NULL;
1738}
1739
1740static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
1741{
1742 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1743
1744 int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8;
1745 vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL);
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001746 vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001747
1748 if (!vgic_cpu->pending_shared || !vgic_cpu->vgic_irq_lr_map) {
1749 kvm_vgic_vcpu_destroy(vcpu);
1750 return -ENOMEM;
1751 }
1752
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001753 memset(vgic_cpu->vgic_irq_lr_map, LR_EMPTY, nr_irqs);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001754
1755 /*
Marc Zyngierca85f622013-06-18 19:17:28 +01001756 * Store the number of LRs per vcpu, so we don't have to go
1757 * all the way to the distributor structure to find out. Only
1758 * assembly code should use this one.
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001759 */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001760 vgic_cpu->nr_lr = vgic->nr_lr;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001761
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001762 return 0;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001763}
1764
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001765void kvm_vgic_destroy(struct kvm *kvm)
1766{
1767 struct vgic_dist *dist = &kvm->arch.vgic;
1768 struct kvm_vcpu *vcpu;
1769 int i;
1770
1771 kvm_for_each_vcpu(i, vcpu, kvm)
1772 kvm_vgic_vcpu_destroy(vcpu);
1773
1774 vgic_free_bitmap(&dist->irq_enabled);
1775 vgic_free_bitmap(&dist->irq_level);
1776 vgic_free_bitmap(&dist->irq_pending);
1777 vgic_free_bitmap(&dist->irq_soft_pend);
1778 vgic_free_bitmap(&dist->irq_queued);
1779 vgic_free_bitmap(&dist->irq_cfg);
1780 vgic_free_bytemap(&dist->irq_priority);
1781 if (dist->irq_spi_target) {
1782 for (i = 0; i < dist->nr_cpus; i++)
1783 vgic_free_bitmap(&dist->irq_spi_target[i]);
1784 }
1785 kfree(dist->irq_sgi_sources);
1786 kfree(dist->irq_spi_cpu);
1787 kfree(dist->irq_spi_target);
1788 kfree(dist->irq_pending_on_cpu);
1789 dist->irq_sgi_sources = NULL;
1790 dist->irq_spi_cpu = NULL;
1791 dist->irq_spi_target = NULL;
1792 dist->irq_pending_on_cpu = NULL;
Christoffer Dall1f57be22014-12-09 14:30:36 +01001793 dist->nr_cpus = 0;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001794}
1795
1796/*
1797 * Allocate and initialize the various data structures. Must be called
1798 * with kvm->lock held!
1799 */
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001800static int vgic_init(struct kvm *kvm)
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001801{
1802 struct vgic_dist *dist = &kvm->arch.vgic;
1803 struct kvm_vcpu *vcpu;
1804 int nr_cpus, nr_irqs;
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001805 int ret, i, vcpu_id;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001806
Christoffer Dall1f57be22014-12-09 14:30:36 +01001807 if (vgic_initialized(kvm))
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001808 return 0;
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001809
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001810 nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus);
1811 if (!nr_cpus) /* No vcpus? Can't be good... */
1812 return -EINVAL;
1813
1814 /*
1815 * If nobody configured the number of interrupts, use the
1816 * legacy one.
1817 */
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001818 if (!dist->nr_irqs)
1819 dist->nr_irqs = VGIC_NR_IRQS_LEGACY;
1820
1821 nr_irqs = dist->nr_irqs;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001822
1823 ret = vgic_init_bitmap(&dist->irq_enabled, nr_cpus, nr_irqs);
1824 ret |= vgic_init_bitmap(&dist->irq_level, nr_cpus, nr_irqs);
1825 ret |= vgic_init_bitmap(&dist->irq_pending, nr_cpus, nr_irqs);
1826 ret |= vgic_init_bitmap(&dist->irq_soft_pend, nr_cpus, nr_irqs);
1827 ret |= vgic_init_bitmap(&dist->irq_queued, nr_cpus, nr_irqs);
1828 ret |= vgic_init_bitmap(&dist->irq_cfg, nr_cpus, nr_irqs);
1829 ret |= vgic_init_bytemap(&dist->irq_priority, nr_cpus, nr_irqs);
1830
1831 if (ret)
1832 goto out;
1833
1834 dist->irq_sgi_sources = kzalloc(nr_cpus * VGIC_NR_SGIS, GFP_KERNEL);
1835 dist->irq_spi_cpu = kzalloc(nr_irqs - VGIC_NR_PRIVATE_IRQS, GFP_KERNEL);
1836 dist->irq_spi_target = kzalloc(sizeof(*dist->irq_spi_target) * nr_cpus,
1837 GFP_KERNEL);
1838 dist->irq_pending_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
1839 GFP_KERNEL);
1840 if (!dist->irq_sgi_sources ||
1841 !dist->irq_spi_cpu ||
1842 !dist->irq_spi_target ||
1843 !dist->irq_pending_on_cpu) {
1844 ret = -ENOMEM;
1845 goto out;
1846 }
1847
1848 for (i = 0; i < nr_cpus; i++)
1849 ret |= vgic_init_bitmap(&dist->irq_spi_target[i],
1850 nr_cpus, nr_irqs);
1851
1852 if (ret)
1853 goto out;
1854
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001855 for (i = VGIC_NR_PRIVATE_IRQS; i < dist->nr_irqs; i += 4)
1856 vgic_set_target_reg(kvm, 0, i);
1857
1858 kvm_for_each_vcpu(vcpu_id, vcpu, kvm) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001859 ret = vgic_vcpu_init_maps(vcpu, nr_irqs);
1860 if (ret) {
1861 kvm_err("VGIC: Failed to allocate vcpu memory\n");
1862 break;
1863 }
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001864
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001865 for (i = 0; i < dist->nr_irqs; i++) {
1866 if (i < VGIC_NR_PPIS)
1867 vgic_bitmap_set_irq_val(&dist->irq_enabled,
1868 vcpu->vcpu_id, i, 1);
1869 if (i < VGIC_NR_PRIVATE_IRQS)
1870 vgic_bitmap_set_irq_val(&dist->irq_cfg,
1871 vcpu->vcpu_id, i,
1872 VGIC_CFG_EDGE);
1873 }
1874
1875 vgic_enable(vcpu);
1876 }
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001877
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001878out:
1879 if (ret)
1880 kvm_vgic_destroy(kvm);
1881
1882 return ret;
1883}
1884
Christoffer Dalle1ba0202013-09-23 14:55:55 -07001885/**
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001886 * kvm_vgic_map_resources - Configure global VGIC state before running any VCPUs
Christoffer Dalle1ba0202013-09-23 14:55:55 -07001887 * @kvm: pointer to the kvm struct
1888 *
1889 * Map the virtual CPU interface into the VM before running any VCPUs. We
1890 * can't do this at creation time, because user space must first set the
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001891 * virtual CPU interface address in the guest physical address space.
Christoffer Dalle1ba0202013-09-23 14:55:55 -07001892 */
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001893int kvm_vgic_map_resources(struct kvm *kvm)
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001894{
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001895 int ret = 0;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001896
Christoffer Dalle1ba0202013-09-23 14:55:55 -07001897 if (!irqchip_in_kernel(kvm))
1898 return 0;
1899
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001900 mutex_lock(&kvm->lock);
1901
Christoffer Dallc52edf52014-12-09 14:28:09 +01001902 if (vgic_ready(kvm))
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001903 goto out;
1904
1905 if (IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_dist_base) ||
1906 IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_cpu_base)) {
1907 kvm_err("Need to set vgic cpu and dist addresses first\n");
1908 ret = -ENXIO;
1909 goto out;
1910 }
1911
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001912 /*
1913 * Initialize the vgic if this hasn't already been done on demand by
1914 * accessing the vgic state from userspace.
1915 */
1916 ret = vgic_init(kvm);
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001917 if (ret) {
1918 kvm_err("Unable to allocate maps\n");
1919 goto out;
1920 }
1921
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001922 ret = kvm_phys_addr_ioremap(kvm, kvm->arch.vgic.vgic_cpu_base,
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -07001923 vgic->vcpu_base, KVM_VGIC_V2_CPU_SIZE,
1924 true);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001925 if (ret) {
1926 kvm_err("Unable to remap VGIC CPU to VCPU\n");
1927 goto out;
1928 }
1929
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001930 kvm->arch.vgic.ready = true;
1931out:
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001932 if (ret)
1933 kvm_vgic_destroy(kvm);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001934 mutex_unlock(&kvm->lock);
1935 return ret;
1936}
1937
1938int kvm_vgic_create(struct kvm *kvm)
1939{
Christoffer Dall73306722013-10-25 17:29:18 +01001940 int i, vcpu_lock_idx = -1, ret = 0;
1941 struct kvm_vcpu *vcpu;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001942
1943 mutex_lock(&kvm->lock);
1944
Christoffer Dall73306722013-10-25 17:29:18 +01001945 if (kvm->arch.vgic.vctrl_base) {
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001946 ret = -EEXIST;
1947 goto out;
1948 }
1949
Christoffer Dall73306722013-10-25 17:29:18 +01001950 /*
1951 * Any time a vcpu is run, vcpu_load is called which tries to grab the
1952 * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
1953 * that no other VCPUs are run while we create the vgic.
1954 */
1955 kvm_for_each_vcpu(i, vcpu, kvm) {
1956 if (!mutex_trylock(&vcpu->mutex))
1957 goto out_unlock;
1958 vcpu_lock_idx = i;
1959 }
1960
1961 kvm_for_each_vcpu(i, vcpu, kvm) {
1962 if (vcpu->arch.has_run_once) {
1963 ret = -EBUSY;
1964 goto out_unlock;
1965 }
1966 }
1967
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001968 spin_lock_init(&kvm->arch.vgic.lock);
Marc Zyngierf982cf42014-05-15 10:03:25 +01001969 kvm->arch.vgic.in_kernel = true;
Marc Zyngier8f186d52014-02-04 18:13:03 +00001970 kvm->arch.vgic.vctrl_base = vgic->vctrl_base;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001971 kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
1972 kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
1973
Christoffer Dall73306722013-10-25 17:29:18 +01001974out_unlock:
1975 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
1976 vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
1977 mutex_unlock(&vcpu->mutex);
1978 }
1979
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001980out:
1981 mutex_unlock(&kvm->lock);
1982 return ret;
1983}
1984
Will Deacon1fa451b2014-08-26 15:13:24 +01001985static int vgic_ioaddr_overlap(struct kvm *kvm)
Christoffer Dall330690c2013-01-21 19:36:13 -05001986{
1987 phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
1988 phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
1989
1990 if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
1991 return 0;
1992 if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
1993 (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
1994 return -EBUSY;
1995 return 0;
1996}
1997
1998static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
1999 phys_addr_t addr, phys_addr_t size)
2000{
2001 int ret;
2002
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002003 if (addr & ~KVM_PHYS_MASK)
2004 return -E2BIG;
2005
2006 if (addr & (SZ_4K - 1))
2007 return -EINVAL;
2008
Christoffer Dall330690c2013-01-21 19:36:13 -05002009 if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
2010 return -EEXIST;
2011 if (addr + size < addr)
2012 return -EINVAL;
2013
Haibin Wang30c21172014-04-29 14:49:17 +08002014 *ioaddr = addr;
Christoffer Dall330690c2013-01-21 19:36:13 -05002015 ret = vgic_ioaddr_overlap(kvm);
2016 if (ret)
Haibin Wang30c21172014-04-29 14:49:17 +08002017 *ioaddr = VGIC_ADDR_UNDEF;
2018
Christoffer Dall330690c2013-01-21 19:36:13 -05002019 return ret;
2020}
2021
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002022/**
2023 * kvm_vgic_addr - set or get vgic VM base addresses
2024 * @kvm: pointer to the vm struct
2025 * @type: the VGIC addr type, one of KVM_VGIC_V2_ADDR_TYPE_XXX
2026 * @addr: pointer to address value
2027 * @write: if true set the address in the VM address space, if false read the
2028 * address
2029 *
2030 * Set or get the vgic base addresses for the distributor and the virtual CPU
2031 * interface in the VM physical address space. These addresses are properties
2032 * of the emulated core/SoC and therefore user space initially knows this
2033 * information.
2034 */
2035int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
Christoffer Dall330690c2013-01-21 19:36:13 -05002036{
2037 int r = 0;
2038 struct vgic_dist *vgic = &kvm->arch.vgic;
2039
Christoffer Dall330690c2013-01-21 19:36:13 -05002040 mutex_lock(&kvm->lock);
2041 switch (type) {
2042 case KVM_VGIC_V2_ADDR_TYPE_DIST:
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002043 if (write) {
2044 r = vgic_ioaddr_assign(kvm, &vgic->vgic_dist_base,
2045 *addr, KVM_VGIC_V2_DIST_SIZE);
2046 } else {
2047 *addr = vgic->vgic_dist_base;
2048 }
Christoffer Dall330690c2013-01-21 19:36:13 -05002049 break;
2050 case KVM_VGIC_V2_ADDR_TYPE_CPU:
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002051 if (write) {
2052 r = vgic_ioaddr_assign(kvm, &vgic->vgic_cpu_base,
2053 *addr, KVM_VGIC_V2_CPU_SIZE);
2054 } else {
2055 *addr = vgic->vgic_cpu_base;
2056 }
Christoffer Dall330690c2013-01-21 19:36:13 -05002057 break;
2058 default:
2059 r = -ENODEV;
2060 }
2061
2062 mutex_unlock(&kvm->lock);
2063 return r;
2064}
Christoffer Dall73306722013-10-25 17:29:18 +01002065
Christoffer Dallc07a0192013-10-25 21:17:31 +01002066static bool handle_cpu_mmio_misc(struct kvm_vcpu *vcpu,
2067 struct kvm_exit_mmio *mmio, phys_addr_t offset)
2068{
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002069 bool updated = false;
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002070 struct vgic_vmcr vmcr;
2071 u32 *vmcr_field;
2072 u32 reg;
2073
2074 vgic_get_vmcr(vcpu, &vmcr);
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002075
2076 switch (offset & ~0x3) {
2077 case GIC_CPU_CTRL:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002078 vmcr_field = &vmcr.ctlr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002079 break;
2080 case GIC_CPU_PRIMASK:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002081 vmcr_field = &vmcr.pmr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002082 break;
2083 case GIC_CPU_BINPOINT:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002084 vmcr_field = &vmcr.bpr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002085 break;
2086 case GIC_CPU_ALIAS_BINPOINT:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002087 vmcr_field = &vmcr.abpr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002088 break;
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002089 default:
2090 BUG();
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002091 }
2092
2093 if (!mmio->is_write) {
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002094 reg = *vmcr_field;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002095 mmio_data_write(mmio, ~0, reg);
2096 } else {
2097 reg = mmio_data_read(mmio, ~0);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002098 if (reg != *vmcr_field) {
2099 *vmcr_field = reg;
2100 vgic_set_vmcr(vcpu, &vmcr);
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002101 updated = true;
Marc Zyngierbeee38b2014-02-04 17:48:10 +00002102 }
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002103 }
2104 return updated;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002105}
2106
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002107static bool handle_mmio_abpr(struct kvm_vcpu *vcpu,
2108 struct kvm_exit_mmio *mmio, phys_addr_t offset)
2109{
2110 return handle_cpu_mmio_misc(vcpu, mmio, GIC_CPU_ALIAS_BINPOINT);
2111}
2112
2113static bool handle_cpu_mmio_ident(struct kvm_vcpu *vcpu,
2114 struct kvm_exit_mmio *mmio,
2115 phys_addr_t offset)
2116{
2117 u32 reg;
2118
2119 if (mmio->is_write)
2120 return false;
2121
2122 /* GICC_IIDR */
2123 reg = (PRODUCT_ID_KVM << 20) |
2124 (GICC_ARCH_VERSION_V2 << 16) |
2125 (IMPLEMENTER_ARM << 0);
2126 mmio_data_write(mmio, ~0, reg);
2127 return false;
2128}
2129
2130/*
2131 * CPU Interface Register accesses - these are not accessed by the VM, but by
2132 * user space for saving and restoring VGIC state.
2133 */
Christoffer Dallc07a0192013-10-25 21:17:31 +01002134static const struct mmio_range vgic_cpu_ranges[] = {
2135 {
2136 .base = GIC_CPU_CTRL,
2137 .len = 12,
2138 .handle_mmio = handle_cpu_mmio_misc,
2139 },
2140 {
2141 .base = GIC_CPU_ALIAS_BINPOINT,
2142 .len = 4,
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002143 .handle_mmio = handle_mmio_abpr,
Christoffer Dallc07a0192013-10-25 21:17:31 +01002144 },
2145 {
2146 .base = GIC_CPU_ACTIVEPRIO,
2147 .len = 16,
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002148 .handle_mmio = handle_mmio_raz_wi,
Christoffer Dallc07a0192013-10-25 21:17:31 +01002149 },
2150 {
2151 .base = GIC_CPU_IDENT,
2152 .len = 4,
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07002153 .handle_mmio = handle_cpu_mmio_ident,
Christoffer Dallc07a0192013-10-25 21:17:31 +01002154 },
2155};
2156
2157static int vgic_attr_regs_access(struct kvm_device *dev,
2158 struct kvm_device_attr *attr,
2159 u32 *reg, bool is_write)
2160{
2161 const struct mmio_range *r = NULL, *ranges;
2162 phys_addr_t offset;
2163 int ret, cpuid, c;
2164 struct kvm_vcpu *vcpu, *tmp_vcpu;
2165 struct vgic_dist *vgic;
2166 struct kvm_exit_mmio mmio;
2167
2168 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2169 cpuid = (attr->attr & KVM_DEV_ARM_VGIC_CPUID_MASK) >>
2170 KVM_DEV_ARM_VGIC_CPUID_SHIFT;
2171
2172 mutex_lock(&dev->kvm->lock);
2173
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00002174 ret = vgic_init(dev->kvm);
Marc Zyngier4956f2b2014-07-08 12:09:06 +01002175 if (ret)
2176 goto out;
2177
Christoffer Dallc07a0192013-10-25 21:17:31 +01002178 if (cpuid >= atomic_read(&dev->kvm->online_vcpus)) {
2179 ret = -EINVAL;
2180 goto out;
2181 }
2182
2183 vcpu = kvm_get_vcpu(dev->kvm, cpuid);
2184 vgic = &dev->kvm->arch.vgic;
2185
2186 mmio.len = 4;
2187 mmio.is_write = is_write;
2188 if (is_write)
2189 mmio_data_write(&mmio, ~0, *reg);
2190 switch (attr->group) {
2191 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2192 mmio.phys_addr = vgic->vgic_dist_base + offset;
2193 ranges = vgic_dist_ranges;
2194 break;
2195 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
2196 mmio.phys_addr = vgic->vgic_cpu_base + offset;
2197 ranges = vgic_cpu_ranges;
2198 break;
2199 default:
2200 BUG();
2201 }
2202 r = find_matching_range(ranges, &mmio, offset);
2203
2204 if (unlikely(!r || !r->handle_mmio)) {
2205 ret = -ENXIO;
2206 goto out;
2207 }
2208
2209
2210 spin_lock(&vgic->lock);
2211
2212 /*
2213 * Ensure that no other VCPU is running by checking the vcpu->cpu
2214 * field. If no other VPCUs are running we can safely access the VGIC
2215 * state, because even if another VPU is run after this point, that
2216 * VCPU will not touch the vgic state, because it will block on
2217 * getting the vgic->lock in kvm_vgic_sync_hwstate().
2218 */
2219 kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm) {
2220 if (unlikely(tmp_vcpu->cpu != -1)) {
2221 ret = -EBUSY;
2222 goto out_vgic_unlock;
2223 }
2224 }
2225
Christoffer Dallcbd333a2013-11-15 20:51:31 -08002226 /*
2227 * Move all pending IRQs from the LRs on all VCPUs so the pending
2228 * state can be properly represented in the register state accessible
2229 * through this API.
2230 */
2231 kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm)
2232 vgic_unqueue_irqs(tmp_vcpu);
2233
Christoffer Dallc07a0192013-10-25 21:17:31 +01002234 offset -= r->base;
2235 r->handle_mmio(vcpu, &mmio, offset);
2236
2237 if (!is_write)
2238 *reg = mmio_data_read(&mmio, ~0);
2239
2240 ret = 0;
2241out_vgic_unlock:
2242 spin_unlock(&vgic->lock);
2243out:
2244 mutex_unlock(&dev->kvm->lock);
2245 return ret;
2246}
2247
Christoffer Dall73306722013-10-25 17:29:18 +01002248static int vgic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2249{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002250 int r;
2251
2252 switch (attr->group) {
2253 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2254 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2255 u64 addr;
2256 unsigned long type = (unsigned long)attr->attr;
2257
2258 if (copy_from_user(&addr, uaddr, sizeof(addr)))
2259 return -EFAULT;
2260
2261 r = kvm_vgic_addr(dev->kvm, type, &addr, true);
2262 return (r == -ENODEV) ? -ENXIO : r;
2263 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01002264
2265 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2266 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
2267 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2268 u32 reg;
2269
2270 if (get_user(reg, uaddr))
2271 return -EFAULT;
2272
2273 return vgic_attr_regs_access(dev, attr, &reg, true);
2274 }
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002275 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2276 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2277 u32 val;
2278 int ret = 0;
2279
2280 if (get_user(val, uaddr))
2281 return -EFAULT;
2282
2283 /*
2284 * We require:
2285 * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs
2286 * - at most 1024 interrupts
2287 * - a multiple of 32 interrupts
2288 */
2289 if (val < (VGIC_NR_PRIVATE_IRQS + 32) ||
2290 val > VGIC_MAX_IRQS ||
2291 (val & 31))
2292 return -EINVAL;
2293
2294 mutex_lock(&dev->kvm->lock);
2295
Christoffer Dallc52edf52014-12-09 14:28:09 +01002296 if (vgic_ready(dev->kvm) || dev->kvm->arch.vgic.nr_irqs)
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002297 ret = -EBUSY;
2298 else
2299 dev->kvm->arch.vgic.nr_irqs = val;
2300
2301 mutex_unlock(&dev->kvm->lock);
2302
2303 return ret;
2304 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01002305
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002306 }
2307
Christoffer Dall73306722013-10-25 17:29:18 +01002308 return -ENXIO;
2309}
2310
2311static int vgic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2312{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002313 int r = -ENXIO;
2314
2315 switch (attr->group) {
2316 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2317 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2318 u64 addr;
2319 unsigned long type = (unsigned long)attr->attr;
2320
2321 r = kvm_vgic_addr(dev->kvm, type, &addr, false);
2322 if (r)
2323 return (r == -ENODEV) ? -ENXIO : r;
2324
2325 if (copy_to_user(uaddr, &addr, sizeof(addr)))
2326 return -EFAULT;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002327 break;
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002328 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01002329
2330 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2331 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
2332 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2333 u32 reg = 0;
2334
2335 r = vgic_attr_regs_access(dev, attr, &reg, false);
2336 if (r)
2337 return r;
2338 r = put_user(reg, uaddr);
2339 break;
2340 }
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002341 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2342 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2343 r = put_user(dev->kvm->arch.vgic.nr_irqs, uaddr);
2344 break;
2345 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01002346
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002347 }
2348
2349 return r;
Christoffer Dall73306722013-10-25 17:29:18 +01002350}
2351
Christoffer Dallc07a0192013-10-25 21:17:31 +01002352static int vgic_has_attr_regs(const struct mmio_range *ranges,
2353 phys_addr_t offset)
2354{
2355 struct kvm_exit_mmio dev_attr_mmio;
2356
2357 dev_attr_mmio.len = 4;
2358 if (find_matching_range(ranges, &dev_attr_mmio, offset))
2359 return 0;
2360 else
2361 return -ENXIO;
2362}
2363
Christoffer Dall73306722013-10-25 17:29:18 +01002364static int vgic_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2365{
Christoffer Dallc07a0192013-10-25 21:17:31 +01002366 phys_addr_t offset;
2367
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002368 switch (attr->group) {
2369 case KVM_DEV_ARM_VGIC_GRP_ADDR:
2370 switch (attr->attr) {
2371 case KVM_VGIC_V2_ADDR_TYPE_DIST:
2372 case KVM_VGIC_V2_ADDR_TYPE_CPU:
2373 return 0;
2374 }
2375 break;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002376 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2377 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2378 return vgic_has_attr_regs(vgic_dist_ranges, offset);
2379 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
2380 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2381 return vgic_has_attr_regs(vgic_cpu_ranges, offset);
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002382 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS:
2383 return 0;
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002384 }
Christoffer Dall73306722013-10-25 17:29:18 +01002385 return -ENXIO;
2386}
2387
2388static void vgic_destroy(struct kvm_device *dev)
2389{
2390 kfree(dev);
2391}
2392
2393static int vgic_create(struct kvm_device *dev, u32 type)
2394{
2395 return kvm_vgic_create(dev->kvm);
2396}
2397
Will Deaconc06a8412014-09-02 10:27:34 +01002398static struct kvm_device_ops kvm_arm_vgic_v2_ops = {
Christoffer Dall73306722013-10-25 17:29:18 +01002399 .name = "kvm-arm-vgic",
2400 .create = vgic_create,
2401 .destroy = vgic_destroy,
2402 .set_attr = vgic_set_attr,
2403 .get_attr = vgic_get_attr,
2404 .has_attr = vgic_has_attr,
2405};
Will Deaconc06a8412014-09-02 10:27:34 +01002406
2407static void vgic_init_maintenance_interrupt(void *info)
2408{
2409 enable_percpu_irq(vgic->maint_irq, 0);
2410}
2411
2412static int vgic_cpu_notify(struct notifier_block *self,
2413 unsigned long action, void *cpu)
2414{
2415 switch (action) {
2416 case CPU_STARTING:
2417 case CPU_STARTING_FROZEN:
2418 vgic_init_maintenance_interrupt(NULL);
2419 break;
2420 case CPU_DYING:
2421 case CPU_DYING_FROZEN:
2422 disable_percpu_irq(vgic->maint_irq);
2423 break;
2424 }
2425
2426 return NOTIFY_OK;
2427}
2428
2429static struct notifier_block vgic_cpu_nb = {
2430 .notifier_call = vgic_cpu_notify,
2431};
2432
2433static const struct of_device_id vgic_ids[] = {
2434 { .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, },
2435 { .compatible = "arm,gic-v3", .data = vgic_v3_probe, },
2436 {},
2437};
2438
2439int kvm_vgic_hyp_init(void)
2440{
2441 const struct of_device_id *matched_id;
Christoffer Dalla875daf2014-09-18 18:15:32 -07002442 const int (*vgic_probe)(struct device_node *,const struct vgic_ops **,
2443 const struct vgic_params **);
Will Deaconc06a8412014-09-02 10:27:34 +01002444 struct device_node *vgic_node;
2445 int ret;
2446
2447 vgic_node = of_find_matching_node_and_match(NULL,
2448 vgic_ids, &matched_id);
2449 if (!vgic_node) {
2450 kvm_err("error: no compatible GIC node found\n");
2451 return -ENODEV;
2452 }
2453
2454 vgic_probe = matched_id->data;
2455 ret = vgic_probe(vgic_node, &vgic_ops, &vgic);
2456 if (ret)
2457 return ret;
2458
2459 ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler,
2460 "vgic", kvm_get_running_vcpus());
2461 if (ret) {
2462 kvm_err("Cannot register interrupt %d\n", vgic->maint_irq);
2463 return ret;
2464 }
2465
2466 ret = __register_cpu_notifier(&vgic_cpu_nb);
2467 if (ret) {
2468 kvm_err("Cannot register vgic CPU notifier\n");
2469 goto out_free_irq;
2470 }
2471
2472 /* Callback into for arch code for setup */
2473 vgic_arch_setup(vgic);
2474
2475 on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
2476
2477 return kvm_register_device_ops(&kvm_arm_vgic_v2_ops,
2478 KVM_DEV_TYPE_ARM_VGIC_V2);
2479
2480out_free_irq:
2481 free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus());
2482 return ret;
2483}