blob: b70174e748684c5a01005dced63e21fa883f735a [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>
Eric Auger174178f2015-03-04 11:14:36 +010034#include <trace/events/kvm.h>
Andre Przywara6777f772015-03-26 14:39:34 +000035#include <asm/kvm.h>
36#include <kvm/iodev.h>
Marc Zyngier1a89dd92013-01-21 19:36:12 -050037
Marc Zyngierb47ef922013-01-21 19:36:14 -050038/*
39 * How the whole thing works (courtesy of Christoffer Dall):
40 *
41 * - At any time, the dist->irq_pending_on_cpu is the oracle that knows if
Christoffer Dall7e362912014-06-14 22:34:04 +020042 * something is pending on the CPU interface.
43 * - Interrupts that are pending on the distributor are stored on the
44 * vgic.irq_pending vgic bitmap (this bitmap is updated by both user land
45 * ioctls and guest mmio ops, and other in-kernel peripherals such as the
46 * arch. timers).
Marc Zyngierb47ef922013-01-21 19:36:14 -050047 * - Every time the bitmap changes, the irq_pending_on_cpu oracle is
48 * recalculated
49 * - To calculate the oracle, we need info for each cpu from
50 * compute_pending_for_cpu, which considers:
Christoffer Dall227844f2014-06-09 12:27:18 +020051 * - PPI: dist->irq_pending & dist->irq_enable
52 * - SPI: dist->irq_pending & dist->irq_enable & dist->irq_spi_target
Christoffer Dall7e362912014-06-14 22:34:04 +020053 * - irq_spi_target is a 'formatted' version of the GICD_ITARGETSRn
Marc Zyngierb47ef922013-01-21 19:36:14 -050054 * registers, stored on each vcpu. We only keep one bit of
55 * information per interrupt, making sure that only one vcpu can
56 * accept the interrupt.
Christoffer Dall7e362912014-06-14 22:34:04 +020057 * - If any of the above state changes, we must recalculate the oracle.
Marc Zyngierb47ef922013-01-21 19:36:14 -050058 * - The same is true when injecting an interrupt, except that we only
59 * consider a single interrupt at a time. The irq_spi_cpu array
60 * contains the target CPU for each SPI.
61 *
62 * The handling of level interrupts adds some extra complexity. We
63 * need to track when the interrupt has been EOIed, so we can sample
64 * the 'line' again. This is achieved as such:
65 *
66 * - When a level interrupt is moved onto a vcpu, the corresponding
Christoffer Dalldbf20f92014-06-09 12:55:13 +020067 * bit in irq_queued is set. As long as this bit is set, the line
Marc Zyngierb47ef922013-01-21 19:36:14 -050068 * will be ignored for further interrupts. The interrupt is injected
69 * into the vcpu with the GICH_LR_EOI bit set (generate a
70 * maintenance interrupt on EOI).
71 * - When the interrupt is EOIed, the maintenance interrupt fires,
Christoffer Dalldbf20f92014-06-09 12:55:13 +020072 * and clears the corresponding bit in irq_queued. This allows the
Marc Zyngierb47ef922013-01-21 19:36:14 -050073 * interrupt line to be sampled again.
Christoffer Dallfaa1b462014-06-14 21:54:51 +020074 * - Note that level-triggered interrupts can also be set to pending from
75 * writes to GICD_ISPENDRn and lowering the external input line does not
76 * cause the interrupt to become inactive in such a situation.
77 * Conversely, writes to GICD_ICPENDRn do not cause the interrupt to become
78 * inactive as long as the external input line is held high.
Marc Zyngierb47ef922013-01-21 19:36:14 -050079 */
80
Andre Przywara83215812014-06-07 00:53:08 +020081#include "vgic.h"
Christoffer Dall330690c2013-01-21 19:36:13 -050082
Marc Zyngiera1fcb442013-01-21 19:36:15 -050083static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +010084static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +010085static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr);
86static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, struct vgic_lr lr_desc);
Marc Zyngier01ac5e32013-01-21 19:36:16 -050087
Marc Zyngier8f186d52014-02-04 18:13:03 +000088static const struct vgic_ops *vgic_ops;
89static const struct vgic_params *vgic;
Marc Zyngierb47ef922013-01-21 19:36:14 -050090
Andre Przywarab26e5fd2014-06-02 16:19:12 +020091static void add_sgi_source(struct kvm_vcpu *vcpu, int irq, int source)
92{
93 vcpu->kvm->arch.vgic.vm_ops.add_sgi_source(vcpu, irq, source);
94}
95
96static bool queue_sgi(struct kvm_vcpu *vcpu, int irq)
97{
98 return vcpu->kvm->arch.vgic.vm_ops.queue_sgi(vcpu, irq);
99}
100
101int kvm_vgic_map_resources(struct kvm *kvm)
102{
103 return kvm->arch.vgic.vm_ops.map_resources(kvm, vgic);
104}
105
Victor Kamensky9662fb42014-06-12 09:30:10 -0700106/*
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100107 * struct vgic_bitmap contains a bitmap made of unsigned longs, but
108 * extracts u32s out of them.
Victor Kamensky9662fb42014-06-12 09:30:10 -0700109 *
110 * This does not work on 64-bit BE systems, because the bitmap access
111 * will store two consecutive 32-bit words with the higher-addressed
112 * register's bits at the lower index and the lower-addressed register's
113 * bits at the higher index.
114 *
115 * Therefore, swizzle the register index when accessing the 32-bit word
116 * registers to access the right register's value.
117 */
118#if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 64
119#define REG_OFFSET_SWIZZLE 1
120#else
121#define REG_OFFSET_SWIZZLE 0
122#endif
Marc Zyngierb47ef922013-01-21 19:36:14 -0500123
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100124static int vgic_init_bitmap(struct vgic_bitmap *b, int nr_cpus, int nr_irqs)
125{
126 int nr_longs;
127
128 nr_longs = nr_cpus + BITS_TO_LONGS(nr_irqs - VGIC_NR_PRIVATE_IRQS);
129
130 b->private = kzalloc(sizeof(unsigned long) * nr_longs, GFP_KERNEL);
131 if (!b->private)
132 return -ENOMEM;
133
134 b->shared = b->private + nr_cpus;
135
136 return 0;
137}
138
139static void vgic_free_bitmap(struct vgic_bitmap *b)
140{
141 kfree(b->private);
142 b->private = NULL;
143 b->shared = NULL;
144}
145
Christoffer Dall2df36a52014-09-28 16:04:26 +0200146/*
147 * Call this function to convert a u64 value to an unsigned long * bitmask
148 * in a way that works on both 32-bit and 64-bit LE and BE platforms.
149 *
150 * Warning: Calling this function may modify *val.
151 */
152static unsigned long *u64_to_bitmask(u64 *val)
153{
154#if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 32
155 *val = (*val >> 32) | (*val << 32);
156#endif
157 return (unsigned long *)val;
158}
159
Andre Przywara83215812014-06-07 00:53:08 +0200160u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x, int cpuid, u32 offset)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500161{
162 offset >>= 2;
163 if (!offset)
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100164 return (u32 *)(x->private + cpuid) + REG_OFFSET_SWIZZLE;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500165 else
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100166 return (u32 *)(x->shared) + ((offset - 1) ^ REG_OFFSET_SWIZZLE);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500167}
168
169static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
170 int cpuid, int irq)
171{
172 if (irq < VGIC_NR_PRIVATE_IRQS)
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100173 return test_bit(irq, x->private + cpuid);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500174
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100175 return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500176}
177
Andre Przywara83215812014-06-07 00:53:08 +0200178void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
179 int irq, int val)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500180{
181 unsigned long *reg;
182
183 if (irq < VGIC_NR_PRIVATE_IRQS) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100184 reg = x->private + cpuid;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500185 } else {
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100186 reg = x->shared;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500187 irq -= VGIC_NR_PRIVATE_IRQS;
188 }
189
190 if (val)
191 set_bit(irq, reg);
192 else
193 clear_bit(irq, reg);
194}
195
196static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
197{
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100198 return x->private + cpuid;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500199}
200
Andre Przywara83215812014-06-07 00:53:08 +0200201unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500202{
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100203 return x->shared;
204}
205
206static int vgic_init_bytemap(struct vgic_bytemap *x, int nr_cpus, int nr_irqs)
207{
208 int size;
209
210 size = nr_cpus * VGIC_NR_PRIVATE_IRQS;
211 size += nr_irqs - VGIC_NR_PRIVATE_IRQS;
212
213 x->private = kzalloc(size, GFP_KERNEL);
214 if (!x->private)
215 return -ENOMEM;
216
217 x->shared = x->private + nr_cpus * VGIC_NR_PRIVATE_IRQS / sizeof(u32);
218 return 0;
219}
220
221static void vgic_free_bytemap(struct vgic_bytemap *b)
222{
223 kfree(b->private);
224 b->private = NULL;
225 b->shared = NULL;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500226}
227
Andre Przywara83215812014-06-07 00:53:08 +0200228u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500229{
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100230 u32 *reg;
231
232 if (offset < VGIC_NR_PRIVATE_IRQS) {
233 reg = x->private;
234 offset += cpuid * VGIC_NR_PRIVATE_IRQS;
235 } else {
236 reg = x->shared;
237 offset -= VGIC_NR_PRIVATE_IRQS;
238 }
239
240 return reg + (offset / sizeof(u32));
Marc Zyngierb47ef922013-01-21 19:36:14 -0500241}
242
243#define VGIC_CFG_LEVEL 0
244#define VGIC_CFG_EDGE 1
245
246static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq)
247{
248 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
249 int irq_val;
250
251 irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq);
252 return irq_val == VGIC_CFG_EDGE;
253}
254
255static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq)
256{
257 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
258
259 return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq);
260}
261
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200262static int vgic_irq_is_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500263{
264 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
265
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200266 return vgic_bitmap_get_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500267}
268
Christoffer Dall47a98b12015-03-13 17:02:54 +0000269static int vgic_irq_is_active(struct kvm_vcpu *vcpu, int irq)
270{
271 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
272
273 return vgic_bitmap_get_irq_val(&dist->irq_active, vcpu->vcpu_id, irq);
274}
275
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200276static void vgic_irq_set_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500277{
278 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
279
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200280 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 1);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500281}
282
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200283static void vgic_irq_clear_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500284{
285 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
286
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200287 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 0);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500288}
289
Christoffer Dall47a98b12015-03-13 17:02:54 +0000290static void vgic_irq_set_active(struct kvm_vcpu *vcpu, int irq)
291{
292 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
293
294 vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 1);
295}
296
297static void vgic_irq_clear_active(struct kvm_vcpu *vcpu, int irq)
298{
299 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
300
301 vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 0);
302}
303
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200304static int vgic_dist_irq_get_level(struct kvm_vcpu *vcpu, int irq)
305{
306 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
307
308 return vgic_bitmap_get_irq_val(&dist->irq_level, vcpu->vcpu_id, irq);
309}
310
311static void vgic_dist_irq_set_level(struct kvm_vcpu *vcpu, int irq)
312{
313 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
314
315 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 1);
316}
317
318static void vgic_dist_irq_clear_level(struct kvm_vcpu *vcpu, int irq)
319{
320 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
321
322 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 0);
323}
324
325static int vgic_dist_irq_soft_pend(struct kvm_vcpu *vcpu, int irq)
326{
327 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
328
329 return vgic_bitmap_get_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq);
330}
331
332static void vgic_dist_irq_clear_soft_pend(struct kvm_vcpu *vcpu, int irq)
333{
334 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
335
336 vgic_bitmap_set_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq, 0);
337}
338
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500339static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
340{
341 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
342
Christoffer Dall227844f2014-06-09 12:27:18 +0200343 return vgic_bitmap_get_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500344}
345
Andre Przywara83215812014-06-07 00:53:08 +0200346void vgic_dist_irq_set_pending(struct kvm_vcpu *vcpu, int irq)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500347{
348 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
349
Christoffer Dall227844f2014-06-09 12:27:18 +0200350 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 1);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500351}
352
Andre Przywara83215812014-06-07 00:53:08 +0200353void vgic_dist_irq_clear_pending(struct kvm_vcpu *vcpu, int irq)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500354{
355 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
356
Christoffer Dall227844f2014-06-09 12:27:18 +0200357 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 0);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500358}
359
360static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq)
361{
362 if (irq < VGIC_NR_PRIVATE_IRQS)
363 set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
364 else
365 set_bit(irq - VGIC_NR_PRIVATE_IRQS,
366 vcpu->arch.vgic_cpu.pending_shared);
367}
368
Andre Przywara83215812014-06-07 00:53:08 +0200369void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500370{
371 if (irq < VGIC_NR_PRIVATE_IRQS)
372 clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
373 else
374 clear_bit(irq - VGIC_NR_PRIVATE_IRQS,
375 vcpu->arch.vgic_cpu.pending_shared);
376}
377
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200378static bool vgic_can_sample_irq(struct kvm_vcpu *vcpu, int irq)
379{
380 return vgic_irq_is_edge(vcpu, irq) || !vgic_irq_is_queued(vcpu, irq);
381}
382
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500383/**
384 * vgic_reg_access - access vgic register
385 * @mmio: pointer to the data describing the mmio access
386 * @reg: pointer to the virtual backing of vgic distributor data
387 * @offset: least significant 2 bits used for word offset
388 * @mode: ACCESS_ mode (see defines above)
389 *
390 * Helper to make vgic register access easier using one of the access
391 * modes defined for vgic register access
392 * (read,raz,write-ignored,setbit,clearbit,write)
393 */
Andre Przywara83215812014-06-07 00:53:08 +0200394void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
395 phys_addr_t offset, int mode)
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500396{
397 int word_offset = (offset & 3) * 8;
398 u32 mask = (1UL << (mmio->len * 8)) - 1;
399 u32 regval;
400
401 /*
402 * Any alignment fault should have been delivered to the guest
403 * directly (ARM ARM B3.12.7 "Prioritization of aborts").
404 */
405
406 if (reg) {
407 regval = *reg;
408 } else {
409 BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED));
410 regval = 0;
411 }
412
413 if (mmio->is_write) {
414 u32 data = mmio_data_read(mmio, mask) << word_offset;
415 switch (ACCESS_WRITE_MASK(mode)) {
416 case ACCESS_WRITE_IGNORED:
417 return;
418
419 case ACCESS_WRITE_SETBIT:
420 regval |= data;
421 break;
422
423 case ACCESS_WRITE_CLEARBIT:
424 regval &= ~data;
425 break;
426
427 case ACCESS_WRITE_VALUE:
428 regval = (regval & ~(mask << word_offset)) | data;
429 break;
430 }
431 *reg = regval;
432 } else {
433 switch (ACCESS_READ_MASK(mode)) {
434 case ACCESS_READ_RAZ:
435 regval = 0;
436 /* fall through */
437
438 case ACCESS_READ_VALUE:
439 mmio_data_write(mmio, mask, regval >> word_offset);
440 }
441 }
442}
443
Andre Przywara83215812014-06-07 00:53:08 +0200444bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
445 phys_addr_t offset)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500446{
447 vgic_reg_access(mmio, NULL, offset,
448 ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
449 return false;
450}
451
Andre Przywara83215812014-06-07 00:53:08 +0200452bool vgic_handle_enable_reg(struct kvm *kvm, struct kvm_exit_mmio *mmio,
453 phys_addr_t offset, int vcpu_id, int access)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500454{
Andre Przywarad97f6832014-06-11 14:11:49 +0200455 u32 *reg;
456 int mode = ACCESS_READ_VALUE | access;
457 struct kvm_vcpu *target_vcpu = kvm_get_vcpu(kvm, vcpu_id);
458
459 reg = vgic_bitmap_get_reg(&kvm->arch.vgic.irq_enabled, vcpu_id, offset);
460 vgic_reg_access(mmio, reg, offset, mode);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500461 if (mmio->is_write) {
Andre Przywarad97f6832014-06-11 14:11:49 +0200462 if (access & ACCESS_WRITE_CLEARBIT) {
463 if (offset < 4) /* Force SGI enabled */
464 *reg |= 0xffff;
465 vgic_retire_disabled_irqs(target_vcpu);
466 }
467 vgic_update_state(kvm);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500468 return true;
469 }
470
471 return false;
472}
473
Andre Przywara83215812014-06-07 00:53:08 +0200474bool vgic_handle_set_pending_reg(struct kvm *kvm,
475 struct kvm_exit_mmio *mmio,
476 phys_addr_t offset, int vcpu_id)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500477{
Christoffer Dall9da48b52014-06-14 22:30:45 +0200478 u32 *reg, orig;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200479 u32 level_mask;
Andre Przywarad97f6832014-06-11 14:11:49 +0200480 int mode = ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT;
481 struct vgic_dist *dist = &kvm->arch.vgic;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200482
Andre Przywarad97f6832014-06-11 14:11:49 +0200483 reg = vgic_bitmap_get_reg(&dist->irq_cfg, vcpu_id, offset);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200484 level_mask = (~(*reg));
485
486 /* Mark both level and edge triggered irqs as pending */
Andre Przywarad97f6832014-06-11 14:11:49 +0200487 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
Christoffer Dall9da48b52014-06-14 22:30:45 +0200488 orig = *reg;
Andre Przywarad97f6832014-06-11 14:11:49 +0200489 vgic_reg_access(mmio, reg, offset, mode);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200490
Marc Zyngierb47ef922013-01-21 19:36:14 -0500491 if (mmio->is_write) {
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200492 /* Set the soft-pending flag only for level-triggered irqs */
493 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
Andre Przywarad97f6832014-06-11 14:11:49 +0200494 vcpu_id, offset);
495 vgic_reg_access(mmio, reg, offset, mode);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200496 *reg &= level_mask;
497
Christoffer Dall9da48b52014-06-14 22:30:45 +0200498 /* Ignore writes to SGIs */
499 if (offset < 2) {
500 *reg &= ~0xffff;
501 *reg |= orig & 0xffff;
502 }
503
Andre Przywarad97f6832014-06-11 14:11:49 +0200504 vgic_update_state(kvm);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500505 return true;
506 }
507
508 return false;
509}
510
Andre Przywara83215812014-06-07 00:53:08 +0200511bool vgic_handle_clear_pending_reg(struct kvm *kvm,
512 struct kvm_exit_mmio *mmio,
513 phys_addr_t offset, int vcpu_id)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500514{
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200515 u32 *level_active;
Christoffer Dall9da48b52014-06-14 22:30:45 +0200516 u32 *reg, orig;
Andre Przywarad97f6832014-06-11 14:11:49 +0200517 int mode = ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT;
518 struct vgic_dist *dist = &kvm->arch.vgic;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200519
Andre Przywarad97f6832014-06-11 14:11:49 +0200520 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
Christoffer Dall9da48b52014-06-14 22:30:45 +0200521 orig = *reg;
Andre Przywarad97f6832014-06-11 14:11:49 +0200522 vgic_reg_access(mmio, reg, offset, mode);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500523 if (mmio->is_write) {
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200524 /* Re-set level triggered level-active interrupts */
525 level_active = vgic_bitmap_get_reg(&dist->irq_level,
Andre Przywarad97f6832014-06-11 14:11:49 +0200526 vcpu_id, offset);
527 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200528 *reg |= *level_active;
529
Christoffer Dall9da48b52014-06-14 22:30:45 +0200530 /* Ignore writes to SGIs */
531 if (offset < 2) {
532 *reg &= ~0xffff;
533 *reg |= orig & 0xffff;
534 }
535
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200536 /* Clear soft-pending flags */
537 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
Andre Przywarad97f6832014-06-11 14:11:49 +0200538 vcpu_id, offset);
539 vgic_reg_access(mmio, reg, offset, mode);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200540
Andre Przywarad97f6832014-06-11 14:11:49 +0200541 vgic_update_state(kvm);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500542 return true;
543 }
Marc Zyngierb47ef922013-01-21 19:36:14 -0500544 return false;
545}
546
Christoffer Dall47a98b12015-03-13 17:02:54 +0000547bool vgic_handle_set_active_reg(struct kvm *kvm,
548 struct kvm_exit_mmio *mmio,
549 phys_addr_t offset, int vcpu_id)
550{
551 u32 *reg;
552 struct vgic_dist *dist = &kvm->arch.vgic;
553
554 reg = vgic_bitmap_get_reg(&dist->irq_active, vcpu_id, offset);
555 vgic_reg_access(mmio, reg, offset,
556 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
557
558 if (mmio->is_write) {
559 vgic_update_state(kvm);
560 return true;
561 }
562
563 return false;
564}
565
566bool vgic_handle_clear_active_reg(struct kvm *kvm,
567 struct kvm_exit_mmio *mmio,
568 phys_addr_t offset, int vcpu_id)
569{
570 u32 *reg;
571 struct vgic_dist *dist = &kvm->arch.vgic;
572
573 reg = vgic_bitmap_get_reg(&dist->irq_active, vcpu_id, offset);
574 vgic_reg_access(mmio, reg, offset,
575 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
576
577 if (mmio->is_write) {
578 vgic_update_state(kvm);
579 return true;
580 }
581
582 return false;
583}
584
Marc Zyngierb47ef922013-01-21 19:36:14 -0500585static u32 vgic_cfg_expand(u16 val)
586{
587 u32 res = 0;
588 int i;
589
590 /*
591 * Turn a 16bit value like abcd...mnop into a 32bit word
592 * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
593 */
594 for (i = 0; i < 16; i++)
595 res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
596
597 return res;
598}
599
600static u16 vgic_cfg_compress(u32 val)
601{
602 u16 res = 0;
603 int i;
604
605 /*
606 * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
607 * abcd...mnop which is what we really care about.
608 */
609 for (i = 0; i < 16; i++)
610 res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
611
612 return res;
613}
614
615/*
616 * The distributor uses 2 bits per IRQ for the CFG register, but the
617 * LSB is always 0. As such, we only keep the upper bit, and use the
618 * two above functions to compress/expand the bits
619 */
Andre Przywara83215812014-06-07 00:53:08 +0200620bool vgic_handle_cfg_reg(u32 *reg, struct kvm_exit_mmio *mmio,
621 phys_addr_t offset)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500622{
623 u32 val;
Marc Zyngier6545eae2013-08-29 11:08:23 +0100624
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200625 if (offset & 4)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500626 val = *reg >> 16;
627 else
628 val = *reg & 0xffff;
629
630 val = vgic_cfg_expand(val);
631 vgic_reg_access(mmio, &val, offset,
632 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
633 if (mmio->is_write) {
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200634 if (offset < 8) {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500635 *reg = ~0U; /* Force PPIs/SGIs to 1 */
636 return false;
637 }
638
639 val = vgic_cfg_compress(val);
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200640 if (offset & 4) {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500641 *reg &= 0xffff;
642 *reg |= val << 16;
643 } else {
644 *reg &= 0xffff << 16;
645 *reg |= val;
646 }
647 }
648
649 return false;
650}
651
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800652/**
Christoffer Dall47a98b12015-03-13 17:02:54 +0000653 * vgic_unqueue_irqs - move pending/active IRQs from LRs to the distributor
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800654 * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs
655 *
Christoffer Dall47a98b12015-03-13 17:02:54 +0000656 * Move any IRQs that have already been assigned to LRs back to the
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800657 * emulated distributor state so that the complete emulated state can be read
658 * from the main emulation structures without investigating the LRs.
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800659 */
Andre Przywara83215812014-06-07 00:53:08 +0200660void vgic_unqueue_irqs(struct kvm_vcpu *vcpu)
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800661{
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800662 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100663 int i;
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800664
665 for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100666 struct vgic_lr lr = vgic_get_lr(vcpu, i);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800667
668 /*
669 * There are three options for the state bits:
670 *
671 * 01: pending
672 * 10: active
673 * 11: pending and active
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800674 */
Christoffer Dall47a98b12015-03-13 17:02:54 +0000675 BUG_ON(!(lr.state & LR_STATE_MASK));
676
677 /* Reestablish SGI source for pending and active IRQs */
678 if (lr.irq < VGIC_NR_SGIS)
679 add_sgi_source(vcpu, lr.irq, lr.source);
680
681 /*
682 * If the LR holds an active (10) or a pending and active (11)
683 * interrupt then move the active state to the
684 * distributor tracking bit.
685 */
686 if (lr.state & LR_STATE_ACTIVE) {
687 vgic_irq_set_active(vcpu, lr.irq);
688 lr.state &= ~LR_STATE_ACTIVE;
689 }
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800690
691 /*
692 * Reestablish the pending state on the distributor and the
693 * CPU interface. It may have already been pending, but that
694 * is fine, then we are only setting a few bits that were
695 * already set.
696 */
Christoffer Dall47a98b12015-03-13 17:02:54 +0000697 if (lr.state & LR_STATE_PENDING) {
698 vgic_dist_irq_set_pending(vcpu, lr.irq);
699 lr.state &= ~LR_STATE_PENDING;
700 }
701
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100702 vgic_set_lr(vcpu, i, lr);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800703
704 /*
Christoffer Dall47a98b12015-03-13 17:02:54 +0000705 * Mark the LR as free for other use.
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800706 */
Christoffer Dall47a98b12015-03-13 17:02:54 +0000707 BUG_ON(lr.state & LR_STATE_MASK);
708 vgic_retire_lr(i, lr.irq, vcpu);
709 vgic_irq_clear_queued(vcpu, lr.irq);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800710
711 /* Finally update the VGIC state. */
712 vgic_update_state(vcpu->kvm);
713 }
714}
715
Andre Przywara83215812014-06-07 00:53:08 +0200716const
Andre Przywaracf50a1e2015-03-26 14:39:32 +0000717struct vgic_io_range *vgic_find_range(const struct vgic_io_range *ranges,
Andre Przywara9f199d02015-03-26 14:39:33 +0000718 int len, gpa_t offset)
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500719{
Andre Przywara9f199d02015-03-26 14:39:33 +0000720 while (ranges->len) {
721 if (offset >= ranges->base &&
722 (offset + len) <= (ranges->base + ranges->len))
723 return ranges;
724 ranges++;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500725 }
726
727 return NULL;
728}
729
Marc Zyngierc3c91832014-07-08 12:09:04 +0100730static bool vgic_validate_access(const struct vgic_dist *dist,
Andre Przywaracf50a1e2015-03-26 14:39:32 +0000731 const struct vgic_io_range *range,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100732 unsigned long offset)
733{
734 int irq;
735
736 if (!range->bits_per_irq)
737 return true; /* Not an irq-based access */
738
739 irq = offset * 8 / range->bits_per_irq;
740 if (irq >= dist->nr_irqs)
741 return false;
742
743 return true;
744}
745
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200746/*
747 * Call the respective handler function for the given range.
748 * We split up any 64 bit accesses into two consecutive 32 bit
749 * handler calls and merge the result afterwards.
750 * We do this in a little endian fashion regardless of the host's
751 * or guest's endianness, because the GIC is always LE and the rest of
752 * the code (vgic_reg_access) also puts it in a LE fashion already.
753 * At this point we have already identified the handle function, so
754 * range points to that one entry and offset is relative to this.
755 */
756static bool call_range_handler(struct kvm_vcpu *vcpu,
757 struct kvm_exit_mmio *mmio,
758 unsigned long offset,
Andre Przywaracf50a1e2015-03-26 14:39:32 +0000759 const struct vgic_io_range *range)
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200760{
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200761 struct kvm_exit_mmio mmio32;
762 bool ret;
763
764 if (likely(mmio->len <= 4))
765 return range->handle_mmio(vcpu, mmio, offset);
766
767 /*
768 * Any access bigger than 4 bytes (that we currently handle in KVM)
769 * is actually 8 bytes long, caused by a 64-bit access
770 */
771
772 mmio32.len = 4;
773 mmio32.is_write = mmio->is_write;
Andre Przywara9fedf142014-11-13 16:21:35 +0000774 mmio32.private = mmio->private;
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200775
776 mmio32.phys_addr = mmio->phys_addr + 4;
Andre Przywara950324a2015-03-28 01:13:13 +0000777 mmio32.data = &((u32 *)mmio->data)[1];
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200778 ret = range->handle_mmio(vcpu, &mmio32, offset + 4);
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200779
780 mmio32.phys_addr = mmio->phys_addr;
Andre Przywara950324a2015-03-28 01:13:13 +0000781 mmio32.data = &((u32 *)mmio->data)[0];
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200782 ret |= range->handle_mmio(vcpu, &mmio32, offset);
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200783
784 return ret;
785}
786
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500787/**
Andre Przywara6777f772015-03-26 14:39:34 +0000788 * vgic_handle_mmio_access - handle an in-kernel MMIO access
789 * This is called by the read/write KVM IO device wrappers below.
790 * @vcpu: pointer to the vcpu performing the access
791 * @this: pointer to the KVM IO device in charge
792 * @addr: guest physical address of the access
793 * @len: size of the access
794 * @val: pointer to the data region
795 * @is_write: read or write access
796 *
797 * returns true if the MMIO access could be performed
798 */
799static int vgic_handle_mmio_access(struct kvm_vcpu *vcpu,
800 struct kvm_io_device *this, gpa_t addr,
801 int len, void *val, bool is_write)
802{
803 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
804 struct vgic_io_device *iodev = container_of(this,
805 struct vgic_io_device, dev);
806 struct kvm_run *run = vcpu->run;
807 const struct vgic_io_range *range;
808 struct kvm_exit_mmio mmio;
809 bool updated_state;
810 gpa_t offset;
811
812 offset = addr - iodev->addr;
813 range = vgic_find_range(iodev->reg_ranges, len, offset);
814 if (unlikely(!range || !range->handle_mmio)) {
815 pr_warn("Unhandled access %d %08llx %d\n", is_write, addr, len);
816 return -ENXIO;
817 }
818
819 mmio.phys_addr = addr;
820 mmio.len = len;
821 mmio.is_write = is_write;
Andre Przywara950324a2015-03-28 01:13:13 +0000822 mmio.data = val;
Andre Przywara6777f772015-03-26 14:39:34 +0000823 mmio.private = iodev->redist_vcpu;
824
825 spin_lock(&dist->lock);
826 offset -= range->base;
827 if (vgic_validate_access(dist, range, offset)) {
828 updated_state = call_range_handler(vcpu, &mmio, offset, range);
Andre Przywara6777f772015-03-26 14:39:34 +0000829 } else {
830 if (!is_write)
831 memset(val, 0, len);
832 updated_state = false;
833 }
834 spin_unlock(&dist->lock);
Andre Przywara950324a2015-03-28 01:13:13 +0000835 run->mmio.is_write = is_write;
836 run->mmio.len = len;
837 run->mmio.phys_addr = addr;
838 memcpy(run->mmio.data, val, len);
839
Andre Przywara6777f772015-03-26 14:39:34 +0000840 kvm_handle_mmio_return(vcpu, run);
841
842 if (updated_state)
843 vgic_kick_vcpus(vcpu->kvm);
844
845 return 0;
846}
847
Andre Przywara6777f772015-03-26 14:39:34 +0000848static int vgic_handle_mmio_read(struct kvm_vcpu *vcpu,
849 struct kvm_io_device *this,
850 gpa_t addr, int len, void *val)
851{
852 return vgic_handle_mmio_access(vcpu, this, addr, len, val, false);
853}
854
855static int vgic_handle_mmio_write(struct kvm_vcpu *vcpu,
856 struct kvm_io_device *this,
857 gpa_t addr, int len, const void *val)
858{
859 return vgic_handle_mmio_access(vcpu, this, addr, len, (void *)val,
860 true);
861}
862
863struct kvm_io_device_ops vgic_io_ops = {
864 .read = vgic_handle_mmio_read,
865 .write = vgic_handle_mmio_write,
866};
867
868/**
869 * vgic_register_kvm_io_dev - register VGIC register frame on the KVM I/O bus
870 * @kvm: The VM structure pointer
871 * @base: The (guest) base address for the register frame
872 * @len: Length of the register frame window
873 * @ranges: Describing the handler functions for each register
874 * @redist_vcpu_id: The VCPU ID to pass on to the handlers on call
875 * @iodev: Points to memory to be passed on to the handler
876 *
877 * @iodev stores the parameters of this function to be usable by the handler
878 * respectively the dispatcher function (since the KVM I/O bus framework lacks
879 * an opaque parameter). Initialization is done in this function, but the
880 * reference should be valid and unique for the whole VGIC lifetime.
881 * If the register frame is not mapped for a specific VCPU, pass -1 to
882 * @redist_vcpu_id.
883 */
884int vgic_register_kvm_io_dev(struct kvm *kvm, gpa_t base, int len,
885 const struct vgic_io_range *ranges,
886 int redist_vcpu_id,
887 struct vgic_io_device *iodev)
888{
889 struct kvm_vcpu *vcpu = NULL;
890 int ret;
891
892 if (redist_vcpu_id >= 0)
893 vcpu = kvm_get_vcpu(kvm, redist_vcpu_id);
894
895 iodev->addr = base;
896 iodev->len = len;
897 iodev->reg_ranges = ranges;
898 iodev->redist_vcpu = vcpu;
899
900 kvm_iodevice_init(&iodev->dev, &vgic_io_ops);
901
902 mutex_lock(&kvm->slots_lock);
903
904 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, base, len,
905 &iodev->dev);
906 mutex_unlock(&kvm->slots_lock);
907
908 /* Mark the iodev as invalid if registration fails. */
909 if (ret)
910 iodev->dev.ops = NULL;
911
912 return ret;
913}
914
Marc Zyngierfb65ab62014-07-08 12:09:02 +0100915static int vgic_nr_shared_irqs(struct vgic_dist *dist)
916{
917 return dist->nr_irqs - VGIC_NR_PRIVATE_IRQS;
918}
919
Christoffer Dall47a98b12015-03-13 17:02:54 +0000920static int compute_active_for_cpu(struct kvm_vcpu *vcpu)
921{
922 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
923 unsigned long *active, *enabled, *act_percpu, *act_shared;
924 unsigned long active_private, active_shared;
925 int nr_shared = vgic_nr_shared_irqs(dist);
926 int vcpu_id;
927
928 vcpu_id = vcpu->vcpu_id;
929 act_percpu = vcpu->arch.vgic_cpu.active_percpu;
930 act_shared = vcpu->arch.vgic_cpu.active_shared;
931
932 active = vgic_bitmap_get_cpu_map(&dist->irq_active, vcpu_id);
933 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
934 bitmap_and(act_percpu, active, enabled, VGIC_NR_PRIVATE_IRQS);
935
936 active = vgic_bitmap_get_shared_map(&dist->irq_active);
937 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
938 bitmap_and(act_shared, active, enabled, nr_shared);
939 bitmap_and(act_shared, act_shared,
940 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
941 nr_shared);
942
943 active_private = find_first_bit(act_percpu, VGIC_NR_PRIVATE_IRQS);
944 active_shared = find_first_bit(act_shared, nr_shared);
945
946 return (active_private < VGIC_NR_PRIVATE_IRQS ||
947 active_shared < nr_shared);
948}
949
Marc Zyngierb47ef922013-01-21 19:36:14 -0500950static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
951{
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500952 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
953 unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
954 unsigned long pending_private, pending_shared;
Marc Zyngierfb65ab62014-07-08 12:09:02 +0100955 int nr_shared = vgic_nr_shared_irqs(dist);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500956 int vcpu_id;
957
958 vcpu_id = vcpu->vcpu_id;
959 pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
960 pend_shared = vcpu->arch.vgic_cpu.pending_shared;
961
Christoffer Dall227844f2014-06-09 12:27:18 +0200962 pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500963 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
964 bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
965
Christoffer Dall227844f2014-06-09 12:27:18 +0200966 pending = vgic_bitmap_get_shared_map(&dist->irq_pending);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500967 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
Marc Zyngierfb65ab62014-07-08 12:09:02 +0100968 bitmap_and(pend_shared, pending, enabled, nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500969 bitmap_and(pend_shared, pend_shared,
970 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
Marc Zyngierfb65ab62014-07-08 12:09:02 +0100971 nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500972
973 pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
Marc Zyngierfb65ab62014-07-08 12:09:02 +0100974 pending_shared = find_first_bit(pend_shared, nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500975 return (pending_private < VGIC_NR_PRIVATE_IRQS ||
Marc Zyngierfb65ab62014-07-08 12:09:02 +0100976 pending_shared < vgic_nr_shared_irqs(dist));
Marc Zyngierb47ef922013-01-21 19:36:14 -0500977}
978
979/*
980 * Update the interrupt state and determine which CPUs have pending
Christoffer Dall47a98b12015-03-13 17:02:54 +0000981 * or active interrupts. Must be called with distributor lock held.
Marc Zyngierb47ef922013-01-21 19:36:14 -0500982 */
Andre Przywara83215812014-06-07 00:53:08 +0200983void vgic_update_state(struct kvm *kvm)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500984{
985 struct vgic_dist *dist = &kvm->arch.vgic;
986 struct kvm_vcpu *vcpu;
987 int c;
988
989 if (!dist->enabled) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100990 set_bit(0, dist->irq_pending_on_cpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500991 return;
992 }
993
994 kvm_for_each_vcpu(c, vcpu, kvm) {
Christoffer Dall47a98b12015-03-13 17:02:54 +0000995 if (compute_pending_for_cpu(vcpu))
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100996 set_bit(c, dist->irq_pending_on_cpu);
Christoffer Dall47a98b12015-03-13 17:02:54 +0000997
998 if (compute_active_for_cpu(vcpu))
999 set_bit(c, dist->irq_active_on_cpu);
1000 else
1001 clear_bit(c, dist->irq_active_on_cpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001002 }
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001003}
Christoffer Dall330690c2013-01-21 19:36:13 -05001004
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001005static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr)
1006{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001007 return vgic_ops->get_lr(vcpu, lr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001008}
1009
1010static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr,
1011 struct vgic_lr vlr)
1012{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001013 vgic_ops->set_lr(vcpu, lr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001014}
1015
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001016static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
1017 struct vgic_lr vlr)
1018{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001019 vgic_ops->sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001020}
1021
1022static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu)
1023{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001024 return vgic_ops->get_elrsr(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001025}
1026
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001027static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu)
1028{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001029 return vgic_ops->get_eisr(vcpu);
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001030}
1031
Marc Zyngier495dd852013-06-04 11:02:10 +01001032static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu)
1033{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001034 return vgic_ops->get_interrupt_status(vcpu);
Marc Zyngier495dd852013-06-04 11:02:10 +01001035}
1036
Marc Zyngier909d9b52013-06-04 11:24:17 +01001037static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu)
1038{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001039 vgic_ops->enable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001040}
1041
1042static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu)
1043{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001044 vgic_ops->disable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001045}
1046
Andre Przywara83215812014-06-07 00:53:08 +02001047void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001048{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001049 vgic_ops->get_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001050}
1051
Andre Przywara83215812014-06-07 00:53:08 +02001052void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001053{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001054 vgic_ops->set_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001055}
1056
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001057static inline void vgic_enable(struct kvm_vcpu *vcpu)
1058{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001059 vgic_ops->enable(vcpu);
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001060}
1061
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001062static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu)
1063{
1064 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1065 struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr);
1066
1067 vlr.state = 0;
1068 vgic_set_lr(vcpu, lr_nr, vlr);
1069 clear_bit(lr_nr, vgic_cpu->lr_used);
1070 vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
1071}
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001072
1073/*
1074 * An interrupt may have been disabled after being made pending on the
1075 * CPU interface (the classic case is a timer running while we're
1076 * rebooting the guest - the interrupt would kick as soon as the CPU
1077 * interface gets enabled, with deadly consequences).
1078 *
1079 * The solution is to examine already active LRs, and check the
1080 * interrupt is still enabled. If not, just retire it.
1081 */
1082static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
1083{
1084 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1085 int lr;
1086
Marc Zyngier8f186d52014-02-04 18:13:03 +00001087 for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001088 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001089
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001090 if (!vgic_irq_is_enabled(vcpu, vlr.irq)) {
1091 vgic_retire_lr(lr, vlr.irq, vcpu);
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001092 if (vgic_irq_is_queued(vcpu, vlr.irq))
1093 vgic_irq_clear_queued(vcpu, vlr.irq);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001094 }
1095 }
1096}
1097
Alex Bennée71760952015-03-13 17:02:53 +00001098static void vgic_queue_irq_to_lr(struct kvm_vcpu *vcpu, int irq,
1099 int lr_nr, struct vgic_lr vlr)
1100{
Christoffer Dall47a98b12015-03-13 17:02:54 +00001101 if (vgic_irq_is_active(vcpu, irq)) {
1102 vlr.state |= LR_STATE_ACTIVE;
1103 kvm_debug("Set active, clear distributor: 0x%x\n", vlr.state);
1104 vgic_irq_clear_active(vcpu, irq);
1105 vgic_update_state(vcpu->kvm);
1106 } else if (vgic_dist_irq_is_pending(vcpu, irq)) {
Alex Bennée71760952015-03-13 17:02:53 +00001107 vlr.state |= LR_STATE_PENDING;
1108 kvm_debug("Set pending: 0x%x\n", vlr.state);
1109 }
1110
1111 if (!vgic_irq_is_edge(vcpu, irq))
1112 vlr.state |= LR_EOI_INT;
1113
1114 vgic_set_lr(vcpu, lr_nr, vlr);
1115}
1116
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001117/*
1118 * Queue an interrupt to a CPU virtual interface. Return true on success,
1119 * or false if it wasn't possible to queue it.
Andre Przywara1d916222014-06-07 00:53:08 +02001120 * sgi_source must be zero for any non-SGI interrupts.
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001121 */
Andre Przywara83215812014-06-07 00:53:08 +02001122bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001123{
1124 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001125 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001126 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001127 int lr;
1128
1129 /* Sanitize the input... */
1130 BUG_ON(sgi_source_id & ~7);
1131 BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001132 BUG_ON(irq >= dist->nr_irqs);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001133
1134 kvm_debug("Queue IRQ%d\n", irq);
1135
1136 lr = vgic_cpu->vgic_irq_lr_map[irq];
1137
1138 /* Do we have an active interrupt for the same CPUID? */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001139 if (lr != LR_EMPTY) {
1140 vlr = vgic_get_lr(vcpu, lr);
1141 if (vlr.source == sgi_source_id) {
1142 kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq);
1143 BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
Alex Bennée71760952015-03-13 17:02:53 +00001144 vgic_queue_irq_to_lr(vcpu, irq, lr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001145 return true;
1146 }
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001147 }
1148
1149 /* Try to use another LR for this interrupt */
1150 lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
Marc Zyngier8f186d52014-02-04 18:13:03 +00001151 vgic->nr_lr);
1152 if (lr >= vgic->nr_lr)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001153 return false;
1154
1155 kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001156 vgic_cpu->vgic_irq_lr_map[irq] = lr;
1157 set_bit(lr, vgic_cpu->lr_used);
1158
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001159 vlr.irq = irq;
1160 vlr.source = sgi_source_id;
Alex Bennée71760952015-03-13 17:02:53 +00001161 vlr.state = 0;
1162 vgic_queue_irq_to_lr(vcpu, irq, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001163
1164 return true;
1165}
1166
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001167static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
1168{
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001169 if (!vgic_can_sample_irq(vcpu, irq))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001170 return true; /* level interrupt, already queued */
1171
1172 if (vgic_queue_irq(vcpu, 0, irq)) {
1173 if (vgic_irq_is_edge(vcpu, irq)) {
Christoffer Dall227844f2014-06-09 12:27:18 +02001174 vgic_dist_irq_clear_pending(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001175 vgic_cpu_irq_clear(vcpu, irq);
1176 } else {
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001177 vgic_irq_set_queued(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001178 }
1179
1180 return true;
1181 }
1182
1183 return false;
1184}
1185
1186/*
1187 * Fill the list registers with pending interrupts before running the
1188 * guest.
1189 */
1190static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1191{
1192 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1193 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Christoffer Dall47a98b12015-03-13 17:02:54 +00001194 unsigned long *pa_percpu, *pa_shared;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001195 int i, vcpu_id;
1196 int overflow = 0;
Christoffer Dall47a98b12015-03-13 17:02:54 +00001197 int nr_shared = vgic_nr_shared_irqs(dist);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001198
1199 vcpu_id = vcpu->vcpu_id;
1200
Christoffer Dall47a98b12015-03-13 17:02:54 +00001201 pa_percpu = vcpu->arch.vgic_cpu.pend_act_percpu;
1202 pa_shared = vcpu->arch.vgic_cpu.pend_act_shared;
1203
1204 bitmap_or(pa_percpu, vgic_cpu->pending_percpu, vgic_cpu->active_percpu,
1205 VGIC_NR_PRIVATE_IRQS);
1206 bitmap_or(pa_shared, vgic_cpu->pending_shared, vgic_cpu->active_shared,
1207 nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001208 /*
1209 * We may not have any pending interrupt, or the interrupts
1210 * may have been serviced from another vcpu. In all cases,
1211 * move along.
1212 */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001213 if (!kvm_vgic_vcpu_pending_irq(vcpu) && !kvm_vgic_vcpu_active_irq(vcpu))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001214 goto epilog;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001215
1216 /* SGIs */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001217 for_each_set_bit(i, pa_percpu, VGIC_NR_SGIS) {
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001218 if (!queue_sgi(vcpu, i))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001219 overflow = 1;
1220 }
1221
1222 /* PPIs */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001223 for_each_set_bit_from(i, pa_percpu, VGIC_NR_PRIVATE_IRQS) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001224 if (!vgic_queue_hwirq(vcpu, i))
1225 overflow = 1;
1226 }
1227
1228 /* SPIs */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001229 for_each_set_bit(i, pa_shared, nr_shared) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001230 if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
1231 overflow = 1;
1232 }
1233
Christoffer Dall47a98b12015-03-13 17:02:54 +00001234
1235
1236
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001237epilog:
1238 if (overflow) {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001239 vgic_enable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001240 } else {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001241 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001242 /*
1243 * We're about to run this VCPU, and we've consumed
1244 * everything the distributor had in store for
1245 * us. Claim we don't have anything pending. We'll
1246 * adjust that if needed while exiting.
1247 */
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001248 clear_bit(vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001249 }
1250}
1251
1252static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1253{
Marc Zyngier495dd852013-06-04 11:02:10 +01001254 u32 status = vgic_get_interrupt_status(vcpu);
Eric Auger649cf732015-03-04 11:14:35 +01001255 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001256 bool level_pending = false;
Eric Auger174178f2015-03-04 11:14:36 +01001257 struct kvm *kvm = vcpu->kvm;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001258
Marc Zyngier495dd852013-06-04 11:02:10 +01001259 kvm_debug("STATUS = %08x\n", status);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001260
Marc Zyngier495dd852013-06-04 11:02:10 +01001261 if (status & INT_STATUS_EOI) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001262 /*
1263 * Some level interrupts have been EOIed. Clear their
1264 * active bit.
1265 */
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001266 u64 eisr = vgic_get_eisr(vcpu);
Christoffer Dall2df36a52014-09-28 16:04:26 +02001267 unsigned long *eisr_ptr = u64_to_bitmask(&eisr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001268 int lr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001269
Marc Zyngier8f186d52014-02-04 18:13:03 +00001270 for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001271 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001272 WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001273
Eric Auger649cf732015-03-04 11:14:35 +01001274 spin_lock(&dist->lock);
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001275 vgic_irq_clear_queued(vcpu, vlr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001276 WARN_ON(vlr.state & LR_STATE_MASK);
1277 vlr.state = 0;
1278 vgic_set_lr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001279
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001280 /*
1281 * If the IRQ was EOIed it was also ACKed and we we
1282 * therefore assume we can clear the soft pending
1283 * state (should it had been set) for this interrupt.
1284 *
1285 * Note: if the IRQ soft pending state was set after
1286 * the IRQ was acked, it actually shouldn't be
1287 * cleared, but we have no way of knowing that unless
1288 * we start trapping ACKs when the soft-pending state
1289 * is set.
1290 */
1291 vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
1292
Eric Auger174178f2015-03-04 11:14:36 +01001293 /*
1294 * kvm_notify_acked_irq calls kvm_set_irq()
1295 * to reset the IRQ level. Need to release the
1296 * lock for kvm_set_irq to grab it.
1297 */
1298 spin_unlock(&dist->lock);
1299
1300 kvm_notify_acked_irq(kvm, 0,
1301 vlr.irq - VGIC_NR_PRIVATE_IRQS);
1302 spin_lock(&dist->lock);
1303
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001304 /* Any additional pending interrupt? */
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001305 if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001306 vgic_cpu_irq_set(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001307 level_pending = true;
1308 } else {
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001309 vgic_dist_irq_clear_pending(vcpu, vlr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001310 vgic_cpu_irq_clear(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001311 }
Marc Zyngier75da01e2013-01-31 11:25:52 +00001312
Eric Auger649cf732015-03-04 11:14:35 +01001313 spin_unlock(&dist->lock);
1314
Marc Zyngier75da01e2013-01-31 11:25:52 +00001315 /*
1316 * Despite being EOIed, the LR may not have
1317 * been marked as empty.
1318 */
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001319 vgic_sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001320 }
1321 }
1322
Marc Zyngier495dd852013-06-04 11:02:10 +01001323 if (status & INT_STATUS_UNDERFLOW)
Marc Zyngier909d9b52013-06-04 11:24:17 +01001324 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001325
1326 return level_pending;
1327}
1328
Eric Auger649cf732015-03-04 11:14:35 +01001329/* Sync back the VGIC state after a guest run */
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001330static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1331{
1332 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1333 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001334 u64 elrsr;
1335 unsigned long *elrsr_ptr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001336 int lr, pending;
1337 bool level_pending;
1338
1339 level_pending = vgic_process_maintenance(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001340 elrsr = vgic_get_elrsr(vcpu);
Christoffer Dall2df36a52014-09-28 16:04:26 +02001341 elrsr_ptr = u64_to_bitmask(&elrsr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001342
1343 /* Clear mappings for empty LRs */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001344 for_each_set_bit(lr, elrsr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001345 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001346
1347 if (!test_and_clear_bit(lr, vgic_cpu->lr_used))
1348 continue;
1349
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001350 vlr = vgic_get_lr(vcpu, lr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001351
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001352 BUG_ON(vlr.irq >= dist->nr_irqs);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001353 vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001354 }
1355
1356 /* Check if we still have something up our sleeve... */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001357 pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr);
1358 if (level_pending || pending < vgic->nr_lr)
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001359 set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001360}
1361
1362void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1363{
1364 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1365
1366 if (!irqchip_in_kernel(vcpu->kvm))
1367 return;
1368
1369 spin_lock(&dist->lock);
1370 __kvm_vgic_flush_hwstate(vcpu);
1371 spin_unlock(&dist->lock);
1372}
1373
1374void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1375{
1376 if (!irqchip_in_kernel(vcpu->kvm))
1377 return;
1378
1379 __kvm_vgic_sync_hwstate(vcpu);
1380}
1381
1382int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
1383{
1384 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1385
1386 if (!irqchip_in_kernel(vcpu->kvm))
1387 return 0;
1388
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001389 return test_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001390}
1391
Christoffer Dall47a98b12015-03-13 17:02:54 +00001392int kvm_vgic_vcpu_active_irq(struct kvm_vcpu *vcpu)
1393{
1394 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1395
1396 if (!irqchip_in_kernel(vcpu->kvm))
1397 return 0;
1398
1399 return test_bit(vcpu->vcpu_id, dist->irq_active_on_cpu);
1400}
1401
1402
Andre Przywara83215812014-06-07 00:53:08 +02001403void vgic_kick_vcpus(struct kvm *kvm)
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001404{
1405 struct kvm_vcpu *vcpu;
1406 int c;
1407
1408 /*
1409 * We've injected an interrupt, time to find out who deserves
1410 * a good kick...
1411 */
1412 kvm_for_each_vcpu(c, vcpu, kvm) {
1413 if (kvm_vgic_vcpu_pending_irq(vcpu))
1414 kvm_vcpu_kick(vcpu);
1415 }
1416}
1417
1418static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
1419{
Christoffer Dall227844f2014-06-09 12:27:18 +02001420 int edge_triggered = vgic_irq_is_edge(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001421
1422 /*
1423 * Only inject an interrupt if:
1424 * - edge triggered and we have a rising edge
1425 * - level triggered and we change level
1426 */
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001427 if (edge_triggered) {
1428 int state = vgic_dist_irq_is_pending(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001429 return level > state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001430 } else {
1431 int state = vgic_dist_irq_get_level(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001432 return level != state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001433 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001434}
1435
Shannon Zhao016ed392014-11-19 10:11:25 +00001436static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001437 unsigned int irq_num, bool level)
1438{
1439 struct vgic_dist *dist = &kvm->arch.vgic;
1440 struct kvm_vcpu *vcpu;
Christoffer Dall227844f2014-06-09 12:27:18 +02001441 int edge_triggered, level_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001442 int enabled;
Andre Przywaraa0675c22014-06-07 00:54:51 +02001443 bool ret = true, can_inject = true;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001444
1445 spin_lock(&dist->lock);
1446
1447 vcpu = kvm_get_vcpu(kvm, cpuid);
Christoffer Dall227844f2014-06-09 12:27:18 +02001448 edge_triggered = vgic_irq_is_edge(vcpu, irq_num);
1449 level_triggered = !edge_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001450
1451 if (!vgic_validate_injection(vcpu, irq_num, level)) {
1452 ret = false;
1453 goto out;
1454 }
1455
1456 if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
1457 cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
Andre Przywaraa0675c22014-06-07 00:54:51 +02001458 if (cpuid == VCPU_NOT_ALLOCATED) {
1459 /* Pretend we use CPU0, and prevent injection */
1460 cpuid = 0;
1461 can_inject = false;
1462 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001463 vcpu = kvm_get_vcpu(kvm, cpuid);
1464 }
1465
1466 kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
1467
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001468 if (level) {
1469 if (level_triggered)
1470 vgic_dist_irq_set_level(vcpu, irq_num);
Christoffer Dall227844f2014-06-09 12:27:18 +02001471 vgic_dist_irq_set_pending(vcpu, irq_num);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001472 } else {
1473 if (level_triggered) {
1474 vgic_dist_irq_clear_level(vcpu, irq_num);
1475 if (!vgic_dist_irq_soft_pend(vcpu, irq_num))
1476 vgic_dist_irq_clear_pending(vcpu, irq_num);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001477 }
wanghaibin7d39f9e32014-11-17 09:27:37 +00001478
1479 ret = false;
1480 goto out;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001481 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001482
1483 enabled = vgic_irq_is_enabled(vcpu, irq_num);
1484
Andre Przywaraa0675c22014-06-07 00:54:51 +02001485 if (!enabled || !can_inject) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001486 ret = false;
1487 goto out;
1488 }
1489
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001490 if (!vgic_can_sample_irq(vcpu, irq_num)) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001491 /*
1492 * Level interrupt in progress, will be picked up
1493 * when EOId.
1494 */
1495 ret = false;
1496 goto out;
1497 }
1498
1499 if (level) {
1500 vgic_cpu_irq_set(vcpu, irq_num);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001501 set_bit(cpuid, dist->irq_pending_on_cpu);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001502 }
1503
1504out:
1505 spin_unlock(&dist->lock);
1506
Shannon Zhao016ed392014-11-19 10:11:25 +00001507 return ret ? cpuid : -EINVAL;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001508}
1509
1510/**
1511 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
1512 * @kvm: The VM structure pointer
1513 * @cpuid: The CPU for PPIs
1514 * @irq_num: The IRQ number that is assigned to the device
1515 * @level: Edge-triggered: true: to trigger the interrupt
1516 * false: to ignore the call
1517 * Level-sensitive true: activates an interrupt
1518 * false: deactivates an interrupt
1519 *
1520 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1521 * level-sensitive interrupts. You can think of the level parameter as 1
1522 * being HIGH and 0 being LOW and all devices being active-HIGH.
1523 */
1524int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
1525 bool level)
1526{
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001527 int ret = 0;
Shannon Zhao016ed392014-11-19 10:11:25 +00001528 int vcpu_id;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001529
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001530 if (unlikely(!vgic_initialized(kvm))) {
Andre Przywara598921362014-06-03 09:33:10 +02001531 /*
1532 * We only provide the automatic initialization of the VGIC
1533 * for the legacy case of a GICv2. Any other type must
1534 * be explicitly initialized once setup with the respective
1535 * KVM device call.
1536 */
1537 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2) {
1538 ret = -EBUSY;
1539 goto out;
1540 }
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001541 mutex_lock(&kvm->lock);
1542 ret = vgic_init(kvm);
1543 mutex_unlock(&kvm->lock);
1544
1545 if (ret)
1546 goto out;
Shannon Zhao016ed392014-11-19 10:11:25 +00001547 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001548
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001549 vcpu_id = vgic_update_irq_pending(kvm, cpuid, irq_num, level);
1550 if (vcpu_id >= 0) {
1551 /* kick the specified vcpu */
1552 kvm_vcpu_kick(kvm_get_vcpu(kvm, vcpu_id));
1553 }
1554
1555out:
1556 return ret;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001557}
1558
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001559static irqreturn_t vgic_maintenance_handler(int irq, void *data)
1560{
1561 /*
1562 * We cannot rely on the vgic maintenance interrupt to be
1563 * delivered synchronously. This means we can only use it to
1564 * exit the VM, and we perform the handling of EOIed
1565 * interrupts on the exit path (see vgic_process_maintenance).
1566 */
1567 return IRQ_HANDLED;
1568}
1569
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001570void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
1571{
1572 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1573
1574 kfree(vgic_cpu->pending_shared);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001575 kfree(vgic_cpu->active_shared);
1576 kfree(vgic_cpu->pend_act_shared);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001577 kfree(vgic_cpu->vgic_irq_lr_map);
1578 vgic_cpu->pending_shared = NULL;
Christoffer Dall47a98b12015-03-13 17:02:54 +00001579 vgic_cpu->active_shared = NULL;
1580 vgic_cpu->pend_act_shared = NULL;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001581 vgic_cpu->vgic_irq_lr_map = NULL;
1582}
1583
1584static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
1585{
1586 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1587
1588 int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8;
1589 vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001590 vgic_cpu->active_shared = kzalloc(sz, GFP_KERNEL);
1591 vgic_cpu->pend_act_shared = kzalloc(sz, GFP_KERNEL);
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001592 vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001593
Christoffer Dall47a98b12015-03-13 17:02:54 +00001594 if (!vgic_cpu->pending_shared
1595 || !vgic_cpu->active_shared
1596 || !vgic_cpu->pend_act_shared
1597 || !vgic_cpu->vgic_irq_lr_map) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001598 kvm_vgic_vcpu_destroy(vcpu);
1599 return -ENOMEM;
1600 }
1601
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001602 memset(vgic_cpu->vgic_irq_lr_map, LR_EMPTY, nr_irqs);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001603
1604 /*
Marc Zyngierca85f622013-06-18 19:17:28 +01001605 * Store the number of LRs per vcpu, so we don't have to go
1606 * all the way to the distributor structure to find out. Only
1607 * assembly code should use this one.
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001608 */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001609 vgic_cpu->nr_lr = vgic->nr_lr;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001610
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001611 return 0;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001612}
1613
Andre Przywara3caa2d82014-06-02 16:26:01 +02001614/**
1615 * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW
1616 *
1617 * The host's GIC naturally limits the maximum amount of VCPUs a guest
1618 * can use.
1619 */
1620int kvm_vgic_get_max_vcpus(void)
1621{
1622 return vgic->max_gic_vcpus;
1623}
1624
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001625void kvm_vgic_destroy(struct kvm *kvm)
1626{
1627 struct vgic_dist *dist = &kvm->arch.vgic;
1628 struct kvm_vcpu *vcpu;
1629 int i;
1630
1631 kvm_for_each_vcpu(i, vcpu, kvm)
1632 kvm_vgic_vcpu_destroy(vcpu);
1633
1634 vgic_free_bitmap(&dist->irq_enabled);
1635 vgic_free_bitmap(&dist->irq_level);
1636 vgic_free_bitmap(&dist->irq_pending);
1637 vgic_free_bitmap(&dist->irq_soft_pend);
1638 vgic_free_bitmap(&dist->irq_queued);
1639 vgic_free_bitmap(&dist->irq_cfg);
1640 vgic_free_bytemap(&dist->irq_priority);
1641 if (dist->irq_spi_target) {
1642 for (i = 0; i < dist->nr_cpus; i++)
1643 vgic_free_bitmap(&dist->irq_spi_target[i]);
1644 }
1645 kfree(dist->irq_sgi_sources);
1646 kfree(dist->irq_spi_cpu);
Andre Przywaraa0675c22014-06-07 00:54:51 +02001647 kfree(dist->irq_spi_mpidr);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001648 kfree(dist->irq_spi_target);
1649 kfree(dist->irq_pending_on_cpu);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001650 kfree(dist->irq_active_on_cpu);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001651 dist->irq_sgi_sources = NULL;
1652 dist->irq_spi_cpu = NULL;
1653 dist->irq_spi_target = NULL;
1654 dist->irq_pending_on_cpu = NULL;
Christoffer Dall47a98b12015-03-13 17:02:54 +00001655 dist->irq_active_on_cpu = NULL;
Christoffer Dall1f57be22014-12-09 14:30:36 +01001656 dist->nr_cpus = 0;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001657}
1658
1659/*
1660 * Allocate and initialize the various data structures. Must be called
1661 * with kvm->lock held!
1662 */
Andre Przywara83215812014-06-07 00:53:08 +02001663int vgic_init(struct kvm *kvm)
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001664{
1665 struct vgic_dist *dist = &kvm->arch.vgic;
1666 struct kvm_vcpu *vcpu;
1667 int nr_cpus, nr_irqs;
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001668 int ret, i, vcpu_id;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001669
Christoffer Dall1f57be22014-12-09 14:30:36 +01001670 if (vgic_initialized(kvm))
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001671 return 0;
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001672
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001673 nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus);
1674 if (!nr_cpus) /* No vcpus? Can't be good... */
Eric Auger66b030e2014-12-15 18:43:32 +01001675 return -ENODEV;
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001676
1677 /*
1678 * If nobody configured the number of interrupts, use the
1679 * legacy one.
1680 */
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001681 if (!dist->nr_irqs)
1682 dist->nr_irqs = VGIC_NR_IRQS_LEGACY;
1683
1684 nr_irqs = dist->nr_irqs;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001685
1686 ret = vgic_init_bitmap(&dist->irq_enabled, nr_cpus, nr_irqs);
1687 ret |= vgic_init_bitmap(&dist->irq_level, nr_cpus, nr_irqs);
1688 ret |= vgic_init_bitmap(&dist->irq_pending, nr_cpus, nr_irqs);
1689 ret |= vgic_init_bitmap(&dist->irq_soft_pend, nr_cpus, nr_irqs);
1690 ret |= vgic_init_bitmap(&dist->irq_queued, nr_cpus, nr_irqs);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001691 ret |= vgic_init_bitmap(&dist->irq_active, nr_cpus, nr_irqs);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001692 ret |= vgic_init_bitmap(&dist->irq_cfg, nr_cpus, nr_irqs);
1693 ret |= vgic_init_bytemap(&dist->irq_priority, nr_cpus, nr_irqs);
1694
1695 if (ret)
1696 goto out;
1697
1698 dist->irq_sgi_sources = kzalloc(nr_cpus * VGIC_NR_SGIS, GFP_KERNEL);
1699 dist->irq_spi_cpu = kzalloc(nr_irqs - VGIC_NR_PRIVATE_IRQS, GFP_KERNEL);
1700 dist->irq_spi_target = kzalloc(sizeof(*dist->irq_spi_target) * nr_cpus,
1701 GFP_KERNEL);
1702 dist->irq_pending_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
1703 GFP_KERNEL);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001704 dist->irq_active_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
1705 GFP_KERNEL);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001706 if (!dist->irq_sgi_sources ||
1707 !dist->irq_spi_cpu ||
1708 !dist->irq_spi_target ||
Christoffer Dall47a98b12015-03-13 17:02:54 +00001709 !dist->irq_pending_on_cpu ||
1710 !dist->irq_active_on_cpu) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001711 ret = -ENOMEM;
1712 goto out;
1713 }
1714
1715 for (i = 0; i < nr_cpus; i++)
1716 ret |= vgic_init_bitmap(&dist->irq_spi_target[i],
1717 nr_cpus, nr_irqs);
1718
1719 if (ret)
1720 goto out;
1721
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001722 ret = kvm->arch.vgic.vm_ops.init_model(kvm);
1723 if (ret)
1724 goto out;
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001725
1726 kvm_for_each_vcpu(vcpu_id, vcpu, kvm) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001727 ret = vgic_vcpu_init_maps(vcpu, nr_irqs);
1728 if (ret) {
1729 kvm_err("VGIC: Failed to allocate vcpu memory\n");
1730 break;
1731 }
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001732
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001733 for (i = 0; i < dist->nr_irqs; i++) {
1734 if (i < VGIC_NR_PPIS)
1735 vgic_bitmap_set_irq_val(&dist->irq_enabled,
1736 vcpu->vcpu_id, i, 1);
1737 if (i < VGIC_NR_PRIVATE_IRQS)
1738 vgic_bitmap_set_irq_val(&dist->irq_cfg,
1739 vcpu->vcpu_id, i,
1740 VGIC_CFG_EDGE);
1741 }
1742
1743 vgic_enable(vcpu);
1744 }
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001745
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001746out:
1747 if (ret)
1748 kvm_vgic_destroy(kvm);
1749
1750 return ret;
1751}
1752
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001753static int init_vgic_model(struct kvm *kvm, int type)
1754{
1755 switch (type) {
1756 case KVM_DEV_TYPE_ARM_VGIC_V2:
1757 vgic_v2_init_emulation(kvm);
1758 break;
Andre Przywarab5d84ff2014-06-03 10:26:03 +02001759#ifdef CONFIG_ARM_GIC_V3
1760 case KVM_DEV_TYPE_ARM_VGIC_V3:
1761 vgic_v3_init_emulation(kvm);
1762 break;
1763#endif
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001764 default:
1765 return -ENODEV;
1766 }
1767
Andre Przywara3caa2d82014-06-02 16:26:01 +02001768 if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus)
1769 return -E2BIG;
1770
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001771 return 0;
1772}
1773
Andre Przywara598921362014-06-03 09:33:10 +02001774int kvm_vgic_create(struct kvm *kvm, u32 type)
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001775{
Christoffer Dall6b50f542014-11-06 11:47:39 +00001776 int i, vcpu_lock_idx = -1, ret;
Christoffer Dall73306722013-10-25 17:29:18 +01001777 struct kvm_vcpu *vcpu;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001778
1779 mutex_lock(&kvm->lock);
1780
Andre Przywara4ce7ebd2014-10-26 23:18:14 +00001781 if (irqchip_in_kernel(kvm)) {
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001782 ret = -EEXIST;
1783 goto out;
1784 }
1785
Christoffer Dall73306722013-10-25 17:29:18 +01001786 /*
Andre Przywarab5d84ff2014-06-03 10:26:03 +02001787 * This function is also called by the KVM_CREATE_IRQCHIP handler,
1788 * which had no chance yet to check the availability of the GICv2
1789 * emulation. So check this here again. KVM_CREATE_DEVICE does
1790 * the proper checks already.
1791 */
1792 if (type == KVM_DEV_TYPE_ARM_VGIC_V2 && !vgic->can_emulate_gicv2)
1793 return -ENODEV;
1794
1795 /*
Christoffer Dall73306722013-10-25 17:29:18 +01001796 * Any time a vcpu is run, vcpu_load is called which tries to grab the
1797 * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
1798 * that no other VCPUs are run while we create the vgic.
1799 */
Christoffer Dall6b50f542014-11-06 11:47:39 +00001800 ret = -EBUSY;
Christoffer Dall73306722013-10-25 17:29:18 +01001801 kvm_for_each_vcpu(i, vcpu, kvm) {
1802 if (!mutex_trylock(&vcpu->mutex))
1803 goto out_unlock;
1804 vcpu_lock_idx = i;
1805 }
1806
1807 kvm_for_each_vcpu(i, vcpu, kvm) {
Christoffer Dall6b50f542014-11-06 11:47:39 +00001808 if (vcpu->arch.has_run_once)
Christoffer Dall73306722013-10-25 17:29:18 +01001809 goto out_unlock;
Christoffer Dall73306722013-10-25 17:29:18 +01001810 }
Christoffer Dall6b50f542014-11-06 11:47:39 +00001811 ret = 0;
Christoffer Dall73306722013-10-25 17:29:18 +01001812
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001813 ret = init_vgic_model(kvm, type);
1814 if (ret)
1815 goto out_unlock;
1816
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001817 spin_lock_init(&kvm->arch.vgic.lock);
Marc Zyngierf982cf42014-05-15 10:03:25 +01001818 kvm->arch.vgic.in_kernel = true;
Andre Przywara598921362014-06-03 09:33:10 +02001819 kvm->arch.vgic.vgic_model = type;
Marc Zyngier8f186d52014-02-04 18:13:03 +00001820 kvm->arch.vgic.vctrl_base = vgic->vctrl_base;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001821 kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
1822 kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
Andre Przywaraa0675c22014-06-07 00:54:51 +02001823 kvm->arch.vgic.vgic_redist_base = VGIC_ADDR_UNDEF;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001824
Christoffer Dall73306722013-10-25 17:29:18 +01001825out_unlock:
1826 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
1827 vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
1828 mutex_unlock(&vcpu->mutex);
1829 }
1830
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001831out:
1832 mutex_unlock(&kvm->lock);
1833 return ret;
1834}
1835
Will Deacon1fa451b2014-08-26 15:13:24 +01001836static int vgic_ioaddr_overlap(struct kvm *kvm)
Christoffer Dall330690c2013-01-21 19:36:13 -05001837{
1838 phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
1839 phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
1840
1841 if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
1842 return 0;
1843 if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
1844 (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
1845 return -EBUSY;
1846 return 0;
1847}
1848
1849static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
1850 phys_addr_t addr, phys_addr_t size)
1851{
1852 int ret;
1853
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001854 if (addr & ~KVM_PHYS_MASK)
1855 return -E2BIG;
1856
1857 if (addr & (SZ_4K - 1))
1858 return -EINVAL;
1859
Christoffer Dall330690c2013-01-21 19:36:13 -05001860 if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
1861 return -EEXIST;
1862 if (addr + size < addr)
1863 return -EINVAL;
1864
Haibin Wang30c21172014-04-29 14:49:17 +08001865 *ioaddr = addr;
Christoffer Dall330690c2013-01-21 19:36:13 -05001866 ret = vgic_ioaddr_overlap(kvm);
1867 if (ret)
Haibin Wang30c21172014-04-29 14:49:17 +08001868 *ioaddr = VGIC_ADDR_UNDEF;
1869
Christoffer Dall330690c2013-01-21 19:36:13 -05001870 return ret;
1871}
1872
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001873/**
1874 * kvm_vgic_addr - set or get vgic VM base addresses
1875 * @kvm: pointer to the vm struct
Andre Przywaraac3d3732014-06-03 10:26:30 +02001876 * @type: the VGIC addr type, one of KVM_VGIC_V[23]_ADDR_TYPE_XXX
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001877 * @addr: pointer to address value
1878 * @write: if true set the address in the VM address space, if false read the
1879 * address
1880 *
1881 * Set or get the vgic base addresses for the distributor and the virtual CPU
1882 * interface in the VM physical address space. These addresses are properties
1883 * of the emulated core/SoC and therefore user space initially knows this
1884 * information.
1885 */
1886int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
Christoffer Dall330690c2013-01-21 19:36:13 -05001887{
1888 int r = 0;
1889 struct vgic_dist *vgic = &kvm->arch.vgic;
Andre Przywaraac3d3732014-06-03 10:26:30 +02001890 int type_needed;
1891 phys_addr_t *addr_ptr, block_size;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00001892 phys_addr_t alignment;
Christoffer Dall330690c2013-01-21 19:36:13 -05001893
Christoffer Dall330690c2013-01-21 19:36:13 -05001894 mutex_lock(&kvm->lock);
1895 switch (type) {
1896 case KVM_VGIC_V2_ADDR_TYPE_DIST:
Andre Przywaraac3d3732014-06-03 10:26:30 +02001897 type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
1898 addr_ptr = &vgic->vgic_dist_base;
1899 block_size = KVM_VGIC_V2_DIST_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00001900 alignment = SZ_4K;
Christoffer Dall330690c2013-01-21 19:36:13 -05001901 break;
1902 case KVM_VGIC_V2_ADDR_TYPE_CPU:
Andre Przywaraac3d3732014-06-03 10:26:30 +02001903 type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
1904 addr_ptr = &vgic->vgic_cpu_base;
1905 block_size = KVM_VGIC_V2_CPU_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00001906 alignment = SZ_4K;
Christoffer Dall330690c2013-01-21 19:36:13 -05001907 break;
Andre Przywaraac3d3732014-06-03 10:26:30 +02001908#ifdef CONFIG_ARM_GIC_V3
1909 case KVM_VGIC_V3_ADDR_TYPE_DIST:
1910 type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
1911 addr_ptr = &vgic->vgic_dist_base;
1912 block_size = KVM_VGIC_V3_DIST_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00001913 alignment = SZ_64K;
Andre Przywaraac3d3732014-06-03 10:26:30 +02001914 break;
1915 case KVM_VGIC_V3_ADDR_TYPE_REDIST:
1916 type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
1917 addr_ptr = &vgic->vgic_redist_base;
1918 block_size = KVM_VGIC_V3_REDIST_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00001919 alignment = SZ_64K;
Andre Przywaraac3d3732014-06-03 10:26:30 +02001920 break;
1921#endif
Christoffer Dall330690c2013-01-21 19:36:13 -05001922 default:
1923 r = -ENODEV;
Andre Przywaraac3d3732014-06-03 10:26:30 +02001924 goto out;
Christoffer Dall330690c2013-01-21 19:36:13 -05001925 }
1926
Andre Przywaraac3d3732014-06-03 10:26:30 +02001927 if (vgic->vgic_model != type_needed) {
1928 r = -ENODEV;
1929 goto out;
1930 }
1931
Andre Przywara4fa96afd2015-01-13 12:02:13 +00001932 if (write) {
1933 if (!IS_ALIGNED(*addr, alignment))
1934 r = -EINVAL;
1935 else
1936 r = vgic_ioaddr_assign(kvm, addr_ptr, *addr,
1937 block_size);
1938 } else {
Andre Przywaraac3d3732014-06-03 10:26:30 +02001939 *addr = *addr_ptr;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00001940 }
Andre Przywaraac3d3732014-06-03 10:26:30 +02001941
1942out:
Christoffer Dall330690c2013-01-21 19:36:13 -05001943 mutex_unlock(&kvm->lock);
1944 return r;
1945}
Christoffer Dall73306722013-10-25 17:29:18 +01001946
Andre Przywara83215812014-06-07 00:53:08 +02001947int vgic_set_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
Christoffer Dall73306722013-10-25 17:29:18 +01001948{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001949 int r;
1950
1951 switch (attr->group) {
1952 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
1953 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1954 u64 addr;
1955 unsigned long type = (unsigned long)attr->attr;
1956
1957 if (copy_from_user(&addr, uaddr, sizeof(addr)))
1958 return -EFAULT;
1959
1960 r = kvm_vgic_addr(dev->kvm, type, &addr, true);
1961 return (r == -ENODEV) ? -ENXIO : r;
1962 }
Marc Zyngiera98f26f2014-07-08 12:09:07 +01001963 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
1964 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
1965 u32 val;
1966 int ret = 0;
1967
1968 if (get_user(val, uaddr))
1969 return -EFAULT;
1970
1971 /*
1972 * We require:
1973 * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs
1974 * - at most 1024 interrupts
1975 * - a multiple of 32 interrupts
1976 */
1977 if (val < (VGIC_NR_PRIVATE_IRQS + 32) ||
1978 val > VGIC_MAX_IRQS ||
1979 (val & 31))
1980 return -EINVAL;
1981
1982 mutex_lock(&dev->kvm->lock);
1983
Christoffer Dallc52edf52014-12-09 14:28:09 +01001984 if (vgic_ready(dev->kvm) || dev->kvm->arch.vgic.nr_irqs)
Marc Zyngiera98f26f2014-07-08 12:09:07 +01001985 ret = -EBUSY;
1986 else
1987 dev->kvm->arch.vgic.nr_irqs = val;
1988
1989 mutex_unlock(&dev->kvm->lock);
1990
1991 return ret;
1992 }
Eric Auger065c0032014-12-15 18:43:33 +01001993 case KVM_DEV_ARM_VGIC_GRP_CTRL: {
1994 switch (attr->attr) {
1995 case KVM_DEV_ARM_VGIC_CTRL_INIT:
1996 r = vgic_init(dev->kvm);
1997 return r;
1998 }
1999 break;
2000 }
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002001 }
2002
Christoffer Dall73306722013-10-25 17:29:18 +01002003 return -ENXIO;
2004}
2005
Andre Przywara83215812014-06-07 00:53:08 +02002006int vgic_get_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
Christoffer Dall73306722013-10-25 17:29:18 +01002007{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002008 int r = -ENXIO;
2009
2010 switch (attr->group) {
2011 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2012 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2013 u64 addr;
2014 unsigned long type = (unsigned long)attr->attr;
2015
2016 r = kvm_vgic_addr(dev->kvm, type, &addr, false);
2017 if (r)
2018 return (r == -ENODEV) ? -ENXIO : r;
2019
2020 if (copy_to_user(uaddr, &addr, sizeof(addr)))
2021 return -EFAULT;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002022 break;
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002023 }
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002024 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2025 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
Andre Przywarab60da142014-08-21 11:08:27 +01002026
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002027 r = put_user(dev->kvm->arch.vgic.nr_irqs, uaddr);
2028 break;
2029 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01002030
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002031 }
2032
2033 return r;
Christoffer Dall73306722013-10-25 17:29:18 +01002034}
2035
Andre Przywaracf50a1e2015-03-26 14:39:32 +00002036int vgic_has_attr_regs(const struct vgic_io_range *ranges, phys_addr_t offset)
Christoffer Dallc07a0192013-10-25 21:17:31 +01002037{
Andre Przywara9f199d02015-03-26 14:39:33 +00002038 if (vgic_find_range(ranges, 4, offset))
Christoffer Dallc07a0192013-10-25 21:17:31 +01002039 return 0;
2040 else
2041 return -ENXIO;
2042}
2043
Will Deaconc06a8412014-09-02 10:27:34 +01002044static void vgic_init_maintenance_interrupt(void *info)
2045{
2046 enable_percpu_irq(vgic->maint_irq, 0);
2047}
2048
2049static int vgic_cpu_notify(struct notifier_block *self,
2050 unsigned long action, void *cpu)
2051{
2052 switch (action) {
2053 case CPU_STARTING:
2054 case CPU_STARTING_FROZEN:
2055 vgic_init_maintenance_interrupt(NULL);
2056 break;
2057 case CPU_DYING:
2058 case CPU_DYING_FROZEN:
2059 disable_percpu_irq(vgic->maint_irq);
2060 break;
2061 }
2062
2063 return NOTIFY_OK;
2064}
2065
2066static struct notifier_block vgic_cpu_nb = {
2067 .notifier_call = vgic_cpu_notify,
2068};
2069
2070static const struct of_device_id vgic_ids[] = {
Mark Rutland0f3724752015-03-05 14:47:44 +00002071 { .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, },
2072 { .compatible = "arm,cortex-a7-gic", .data = vgic_v2_probe, },
2073 { .compatible = "arm,gic-400", .data = vgic_v2_probe, },
2074 { .compatible = "arm,gic-v3", .data = vgic_v3_probe, },
Will Deaconc06a8412014-09-02 10:27:34 +01002075 {},
2076};
2077
2078int kvm_vgic_hyp_init(void)
2079{
2080 const struct of_device_id *matched_id;
Christoffer Dalla875daf2014-09-18 18:15:32 -07002081 const int (*vgic_probe)(struct device_node *,const struct vgic_ops **,
2082 const struct vgic_params **);
Will Deaconc06a8412014-09-02 10:27:34 +01002083 struct device_node *vgic_node;
2084 int ret;
2085
2086 vgic_node = of_find_matching_node_and_match(NULL,
2087 vgic_ids, &matched_id);
2088 if (!vgic_node) {
2089 kvm_err("error: no compatible GIC node found\n");
2090 return -ENODEV;
2091 }
2092
2093 vgic_probe = matched_id->data;
2094 ret = vgic_probe(vgic_node, &vgic_ops, &vgic);
2095 if (ret)
2096 return ret;
2097
2098 ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler,
2099 "vgic", kvm_get_running_vcpus());
2100 if (ret) {
2101 kvm_err("Cannot register interrupt %d\n", vgic->maint_irq);
2102 return ret;
2103 }
2104
2105 ret = __register_cpu_notifier(&vgic_cpu_nb);
2106 if (ret) {
2107 kvm_err("Cannot register vgic CPU notifier\n");
2108 goto out_free_irq;
2109 }
2110
2111 /* Callback into for arch code for setup */
2112 vgic_arch_setup(vgic);
2113
2114 on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
2115
Andre Przywaraea2f83a2014-10-26 23:17:00 +00002116 return 0;
Will Deaconc06a8412014-09-02 10:27:34 +01002117
2118out_free_irq:
2119 free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus());
2120 return ret;
2121}
Eric Auger174178f2015-03-04 11:14:36 +01002122
2123int kvm_irq_map_gsi(struct kvm *kvm,
2124 struct kvm_kernel_irq_routing_entry *entries,
2125 int gsi)
2126{
2127 return gsi;
2128}
2129
2130int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin)
2131{
2132 return pin;
2133}
2134
2135int kvm_set_irq(struct kvm *kvm, int irq_source_id,
2136 u32 irq, int level, bool line_status)
2137{
2138 unsigned int spi = irq + VGIC_NR_PRIVATE_IRQS;
2139
2140 trace_kvm_set_irq(irq, level, irq_source_id);
2141
2142 BUG_ON(!vgic_initialized(kvm));
2143
2144 if (spi > kvm->arch.vgic.nr_irqs)
2145 return -EINVAL;
2146 return kvm_vgic_inject_irq(kvm, 0, spi, level);
2147
2148}
2149
2150/* MSI not implemented yet */
2151int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
2152 struct kvm *kvm, int irq_source_id,
2153 int level, bool line_status)
2154{
2155 return 0;
2156}