blob: 58b125676785693e4453809484ad4c9057bddfe2 [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>
Marc Zyngier6c3d63c2014-06-23 17:37:18 +010027#include <linux/rculist.h>
Christoffer Dall2a2f3e262014-02-02 13:41:02 -080028#include <linux/uaccess.h>
Marc Zyngier01ac5e32013-01-21 19:36:16 -050029
Marc Zyngier1a89dd92013-01-21 19:36:12 -050030#include <asm/kvm_emulate.h>
Marc Zyngier01ac5e32013-01-21 19:36:16 -050031#include <asm/kvm_arm.h>
32#include <asm/kvm_mmu.h>
Eric Auger174178f2015-03-04 11:14:36 +010033#include <trace/events/kvm.h>
Andre Przywara6777f772015-03-26 14:39:34 +000034#include <asm/kvm.h>
35#include <kvm/iodev.h>
Marc Zyngier1a89dd92013-01-21 19:36:12 -050036
Marc Zyngierb47ef922013-01-21 19:36:14 -050037/*
38 * How the whole thing works (courtesy of Christoffer Dall):
39 *
40 * - At any time, the dist->irq_pending_on_cpu is the oracle that knows if
Christoffer Dall7e362912014-06-14 22:34:04 +020041 * something is pending on the CPU interface.
42 * - Interrupts that are pending on the distributor are stored on the
43 * vgic.irq_pending vgic bitmap (this bitmap is updated by both user land
44 * ioctls and guest mmio ops, and other in-kernel peripherals such as the
45 * arch. timers).
Marc Zyngierb47ef922013-01-21 19:36:14 -050046 * - Every time the bitmap changes, the irq_pending_on_cpu oracle is
47 * recalculated
48 * - To calculate the oracle, we need info for each cpu from
49 * compute_pending_for_cpu, which considers:
Christoffer Dall227844f2014-06-09 12:27:18 +020050 * - PPI: dist->irq_pending & dist->irq_enable
51 * - SPI: dist->irq_pending & dist->irq_enable & dist->irq_spi_target
Christoffer Dall7e362912014-06-14 22:34:04 +020052 * - irq_spi_target is a 'formatted' version of the GICD_ITARGETSRn
Marc Zyngierb47ef922013-01-21 19:36:14 -050053 * registers, stored on each vcpu. We only keep one bit of
54 * information per interrupt, making sure that only one vcpu can
55 * accept the interrupt.
Christoffer Dall7e362912014-06-14 22:34:04 +020056 * - If any of the above state changes, we must recalculate the oracle.
Marc Zyngierb47ef922013-01-21 19:36:14 -050057 * - The same is true when injecting an interrupt, except that we only
58 * consider a single interrupt at a time. The irq_spi_cpu array
59 * contains the target CPU for each SPI.
60 *
61 * The handling of level interrupts adds some extra complexity. We
62 * need to track when the interrupt has been EOIed, so we can sample
63 * the 'line' again. This is achieved as such:
64 *
65 * - When a level interrupt is moved onto a vcpu, the corresponding
Christoffer Dalldbf20f92014-06-09 12:55:13 +020066 * bit in irq_queued is set. As long as this bit is set, the line
Marc Zyngierb47ef922013-01-21 19:36:14 -050067 * will be ignored for further interrupts. The interrupt is injected
68 * into the vcpu with the GICH_LR_EOI bit set (generate a
69 * maintenance interrupt on EOI).
70 * - When the interrupt is EOIed, the maintenance interrupt fires,
Christoffer Dalldbf20f92014-06-09 12:55:13 +020071 * and clears the corresponding bit in irq_queued. This allows the
Marc Zyngierb47ef922013-01-21 19:36:14 -050072 * interrupt line to be sampled again.
Christoffer Dallfaa1b462014-06-14 21:54:51 +020073 * - Note that level-triggered interrupts can also be set to pending from
74 * writes to GICD_ISPENDRn and lowering the external input line does not
75 * cause the interrupt to become inactive in such a situation.
76 * Conversely, writes to GICD_ICPENDRn do not cause the interrupt to become
77 * inactive as long as the external input line is held high.
Marc Zyngier6c3d63c2014-06-23 17:37:18 +010078 *
79 *
80 * Initialization rules: there are multiple stages to the vgic
81 * initialization, both for the distributor and the CPU interfaces.
82 *
83 * Distributor:
84 *
85 * - kvm_vgic_early_init(): initialization of static data that doesn't
86 * depend on any sizing information or emulation type. No allocation
87 * is allowed there.
88 *
89 * - vgic_init(): allocation and initialization of the generic data
90 * structures that depend on sizing information (number of CPUs,
91 * number of interrupts). Also initializes the vcpu specific data
92 * structures. Can be executed lazily for GICv2.
93 * [to be renamed to kvm_vgic_init??]
94 *
95 * CPU Interface:
96 *
97 * - kvm_vgic_cpu_early_init(): initialization of static data that
98 * doesn't depend on any sizing information or emulation type. No
99 * allocation is allowed there.
Marc Zyngierb47ef922013-01-21 19:36:14 -0500100 */
101
Andre Przywara83215812014-06-07 00:53:08 +0200102#include "vgic.h"
Christoffer Dall330690c2013-01-21 19:36:13 -0500103
Marc Zyngiera1fcb442013-01-21 19:36:15 -0500104static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100105static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100106static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr);
107static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, struct vgic_lr lr_desc);
Marc Zyngier6c3d63c2014-06-23 17:37:18 +0100108static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu,
109 int virt_irq);
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500110
Marc Zyngier8f186d52014-02-04 18:13:03 +0000111static const struct vgic_ops *vgic_ops;
112static const struct vgic_params *vgic;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500113
Andre Przywarab26e5fd2014-06-02 16:19:12 +0200114static void add_sgi_source(struct kvm_vcpu *vcpu, int irq, int source)
115{
116 vcpu->kvm->arch.vgic.vm_ops.add_sgi_source(vcpu, irq, source);
117}
118
119static bool queue_sgi(struct kvm_vcpu *vcpu, int irq)
120{
121 return vcpu->kvm->arch.vgic.vm_ops.queue_sgi(vcpu, irq);
122}
123
124int kvm_vgic_map_resources(struct kvm *kvm)
125{
126 return kvm->arch.vgic.vm_ops.map_resources(kvm, vgic);
127}
128
Victor Kamensky9662fb42014-06-12 09:30:10 -0700129/*
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100130 * struct vgic_bitmap contains a bitmap made of unsigned longs, but
131 * extracts u32s out of them.
Victor Kamensky9662fb42014-06-12 09:30:10 -0700132 *
133 * This does not work on 64-bit BE systems, because the bitmap access
134 * will store two consecutive 32-bit words with the higher-addressed
135 * register's bits at the lower index and the lower-addressed register's
136 * bits at the higher index.
137 *
138 * Therefore, swizzle the register index when accessing the 32-bit word
139 * registers to access the right register's value.
140 */
141#if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 64
142#define REG_OFFSET_SWIZZLE 1
143#else
144#define REG_OFFSET_SWIZZLE 0
145#endif
Marc Zyngierb47ef922013-01-21 19:36:14 -0500146
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100147static int vgic_init_bitmap(struct vgic_bitmap *b, int nr_cpus, int nr_irqs)
148{
149 int nr_longs;
150
151 nr_longs = nr_cpus + BITS_TO_LONGS(nr_irqs - VGIC_NR_PRIVATE_IRQS);
152
153 b->private = kzalloc(sizeof(unsigned long) * nr_longs, GFP_KERNEL);
154 if (!b->private)
155 return -ENOMEM;
156
157 b->shared = b->private + nr_cpus;
158
159 return 0;
160}
161
162static void vgic_free_bitmap(struct vgic_bitmap *b)
163{
164 kfree(b->private);
165 b->private = NULL;
166 b->shared = NULL;
167}
168
Christoffer Dall2df36a52014-09-28 16:04:26 +0200169/*
170 * Call this function to convert a u64 value to an unsigned long * bitmask
171 * in a way that works on both 32-bit and 64-bit LE and BE platforms.
172 *
173 * Warning: Calling this function may modify *val.
174 */
175static unsigned long *u64_to_bitmask(u64 *val)
176{
177#if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 32
178 *val = (*val >> 32) | (*val << 32);
179#endif
180 return (unsigned long *)val;
181}
182
Andre Przywara83215812014-06-07 00:53:08 +0200183u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x, int cpuid, u32 offset)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500184{
185 offset >>= 2;
186 if (!offset)
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100187 return (u32 *)(x->private + cpuid) + REG_OFFSET_SWIZZLE;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500188 else
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100189 return (u32 *)(x->shared) + ((offset - 1) ^ REG_OFFSET_SWIZZLE);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500190}
191
192static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
193 int cpuid, int irq)
194{
195 if (irq < VGIC_NR_PRIVATE_IRQS)
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100196 return test_bit(irq, x->private + cpuid);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500197
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100198 return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500199}
200
Andre Przywara83215812014-06-07 00:53:08 +0200201void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
202 int irq, int val)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500203{
204 unsigned long *reg;
205
206 if (irq < VGIC_NR_PRIVATE_IRQS) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100207 reg = x->private + cpuid;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500208 } else {
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100209 reg = x->shared;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500210 irq -= VGIC_NR_PRIVATE_IRQS;
211 }
212
213 if (val)
214 set_bit(irq, reg);
215 else
216 clear_bit(irq, reg);
217}
218
219static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
220{
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100221 return x->private + cpuid;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500222}
223
Andre Przywara83215812014-06-07 00:53:08 +0200224unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500225{
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100226 return x->shared;
227}
228
229static int vgic_init_bytemap(struct vgic_bytemap *x, int nr_cpus, int nr_irqs)
230{
231 int size;
232
233 size = nr_cpus * VGIC_NR_PRIVATE_IRQS;
234 size += nr_irqs - VGIC_NR_PRIVATE_IRQS;
235
236 x->private = kzalloc(size, GFP_KERNEL);
237 if (!x->private)
238 return -ENOMEM;
239
240 x->shared = x->private + nr_cpus * VGIC_NR_PRIVATE_IRQS / sizeof(u32);
241 return 0;
242}
243
244static void vgic_free_bytemap(struct vgic_bytemap *b)
245{
246 kfree(b->private);
247 b->private = NULL;
248 b->shared = NULL;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500249}
250
Andre Przywara83215812014-06-07 00:53:08 +0200251u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500252{
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100253 u32 *reg;
254
255 if (offset < VGIC_NR_PRIVATE_IRQS) {
256 reg = x->private;
257 offset += cpuid * VGIC_NR_PRIVATE_IRQS;
258 } else {
259 reg = x->shared;
260 offset -= VGIC_NR_PRIVATE_IRQS;
261 }
262
263 return reg + (offset / sizeof(u32));
Marc Zyngierb47ef922013-01-21 19:36:14 -0500264}
265
266#define VGIC_CFG_LEVEL 0
267#define VGIC_CFG_EDGE 1
268
269static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq)
270{
271 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
272 int irq_val;
273
274 irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq);
275 return irq_val == VGIC_CFG_EDGE;
276}
277
278static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq)
279{
280 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
281
282 return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq);
283}
284
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200285static int vgic_irq_is_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500286{
287 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
288
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200289 return vgic_bitmap_get_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500290}
291
Christoffer Dall47a98b12015-03-13 17:02:54 +0000292static int vgic_irq_is_active(struct kvm_vcpu *vcpu, int irq)
293{
294 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
295
296 return vgic_bitmap_get_irq_val(&dist->irq_active, vcpu->vcpu_id, irq);
297}
298
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200299static void vgic_irq_set_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500300{
301 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
302
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200303 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 1);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500304}
305
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200306static void vgic_irq_clear_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500307{
308 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
309
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200310 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 0);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500311}
312
Christoffer Dall47a98b12015-03-13 17:02:54 +0000313static void vgic_irq_set_active(struct kvm_vcpu *vcpu, int irq)
314{
315 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
316
317 vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 1);
318}
319
320static void vgic_irq_clear_active(struct kvm_vcpu *vcpu, int irq)
321{
322 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
323
324 vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 0);
325}
326
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200327static int vgic_dist_irq_get_level(struct kvm_vcpu *vcpu, int irq)
328{
329 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
330
331 return vgic_bitmap_get_irq_val(&dist->irq_level, vcpu->vcpu_id, irq);
332}
333
334static void vgic_dist_irq_set_level(struct kvm_vcpu *vcpu, int irq)
335{
336 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
337
338 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 1);
339}
340
341static void vgic_dist_irq_clear_level(struct kvm_vcpu *vcpu, int irq)
342{
343 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
344
345 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 0);
346}
347
348static int vgic_dist_irq_soft_pend(struct kvm_vcpu *vcpu, int irq)
349{
350 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
351
352 return vgic_bitmap_get_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq);
353}
354
355static void vgic_dist_irq_clear_soft_pend(struct kvm_vcpu *vcpu, int irq)
356{
357 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
358
359 vgic_bitmap_set_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq, 0);
360}
361
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500362static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
363{
364 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
365
Christoffer Dall227844f2014-06-09 12:27:18 +0200366 return vgic_bitmap_get_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500367}
368
Andre Przywara83215812014-06-07 00:53:08 +0200369void vgic_dist_irq_set_pending(struct kvm_vcpu *vcpu, int irq)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500370{
371 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
372
Christoffer Dall227844f2014-06-09 12:27:18 +0200373 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 1);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500374}
375
Andre Przywara83215812014-06-07 00:53:08 +0200376void vgic_dist_irq_clear_pending(struct kvm_vcpu *vcpu, int irq)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500377{
378 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
379
Christoffer Dall227844f2014-06-09 12:27:18 +0200380 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 0);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500381}
382
383static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq)
384{
385 if (irq < VGIC_NR_PRIVATE_IRQS)
386 set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
387 else
388 set_bit(irq - VGIC_NR_PRIVATE_IRQS,
389 vcpu->arch.vgic_cpu.pending_shared);
390}
391
Andre Przywara83215812014-06-07 00:53:08 +0200392void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500393{
394 if (irq < VGIC_NR_PRIVATE_IRQS)
395 clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
396 else
397 clear_bit(irq - VGIC_NR_PRIVATE_IRQS,
398 vcpu->arch.vgic_cpu.pending_shared);
399}
400
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200401static bool vgic_can_sample_irq(struct kvm_vcpu *vcpu, int irq)
402{
Marc Zyngier7a67b4b2015-06-05 16:45:29 +0100403 return !vgic_irq_is_queued(vcpu, irq);
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200404}
405
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500406/**
407 * vgic_reg_access - access vgic register
408 * @mmio: pointer to the data describing the mmio access
409 * @reg: pointer to the virtual backing of vgic distributor data
410 * @offset: least significant 2 bits used for word offset
411 * @mode: ACCESS_ mode (see defines above)
412 *
413 * Helper to make vgic register access easier using one of the access
414 * modes defined for vgic register access
415 * (read,raz,write-ignored,setbit,clearbit,write)
416 */
Andre Przywara83215812014-06-07 00:53:08 +0200417void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
418 phys_addr_t offset, int mode)
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500419{
420 int word_offset = (offset & 3) * 8;
421 u32 mask = (1UL << (mmio->len * 8)) - 1;
422 u32 regval;
423
424 /*
425 * Any alignment fault should have been delivered to the guest
426 * directly (ARM ARM B3.12.7 "Prioritization of aborts").
427 */
428
429 if (reg) {
430 regval = *reg;
431 } else {
432 BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED));
433 regval = 0;
434 }
435
436 if (mmio->is_write) {
437 u32 data = mmio_data_read(mmio, mask) << word_offset;
438 switch (ACCESS_WRITE_MASK(mode)) {
439 case ACCESS_WRITE_IGNORED:
440 return;
441
442 case ACCESS_WRITE_SETBIT:
443 regval |= data;
444 break;
445
446 case ACCESS_WRITE_CLEARBIT:
447 regval &= ~data;
448 break;
449
450 case ACCESS_WRITE_VALUE:
451 regval = (regval & ~(mask << word_offset)) | data;
452 break;
453 }
454 *reg = regval;
455 } else {
456 switch (ACCESS_READ_MASK(mode)) {
457 case ACCESS_READ_RAZ:
458 regval = 0;
459 /* fall through */
460
461 case ACCESS_READ_VALUE:
462 mmio_data_write(mmio, mask, regval >> word_offset);
463 }
464 }
465}
466
Andre Przywara83215812014-06-07 00:53:08 +0200467bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
468 phys_addr_t offset)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500469{
470 vgic_reg_access(mmio, NULL, offset,
471 ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
472 return false;
473}
474
Andre Przywara83215812014-06-07 00:53:08 +0200475bool vgic_handle_enable_reg(struct kvm *kvm, struct kvm_exit_mmio *mmio,
476 phys_addr_t offset, int vcpu_id, int access)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500477{
Andre Przywarad97f6832014-06-11 14:11:49 +0200478 u32 *reg;
479 int mode = ACCESS_READ_VALUE | access;
480 struct kvm_vcpu *target_vcpu = kvm_get_vcpu(kvm, vcpu_id);
481
482 reg = vgic_bitmap_get_reg(&kvm->arch.vgic.irq_enabled, vcpu_id, offset);
483 vgic_reg_access(mmio, reg, offset, mode);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500484 if (mmio->is_write) {
Andre Przywarad97f6832014-06-11 14:11:49 +0200485 if (access & ACCESS_WRITE_CLEARBIT) {
486 if (offset < 4) /* Force SGI enabled */
487 *reg |= 0xffff;
488 vgic_retire_disabled_irqs(target_vcpu);
489 }
490 vgic_update_state(kvm);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500491 return true;
492 }
493
494 return false;
495}
496
Andre Przywara83215812014-06-07 00:53:08 +0200497bool vgic_handle_set_pending_reg(struct kvm *kvm,
498 struct kvm_exit_mmio *mmio,
499 phys_addr_t offset, int vcpu_id)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500500{
Christoffer Dall9da48b52014-06-14 22:30:45 +0200501 u32 *reg, orig;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200502 u32 level_mask;
Andre Przywarad97f6832014-06-11 14:11:49 +0200503 int mode = ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT;
504 struct vgic_dist *dist = &kvm->arch.vgic;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200505
Andre Przywarad97f6832014-06-11 14:11:49 +0200506 reg = vgic_bitmap_get_reg(&dist->irq_cfg, vcpu_id, offset);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200507 level_mask = (~(*reg));
508
509 /* Mark both level and edge triggered irqs as pending */
Andre Przywarad97f6832014-06-11 14:11:49 +0200510 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
Christoffer Dall9da48b52014-06-14 22:30:45 +0200511 orig = *reg;
Andre Przywarad97f6832014-06-11 14:11:49 +0200512 vgic_reg_access(mmio, reg, offset, mode);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200513
Marc Zyngierb47ef922013-01-21 19:36:14 -0500514 if (mmio->is_write) {
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200515 /* Set the soft-pending flag only for level-triggered irqs */
516 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
Andre Przywarad97f6832014-06-11 14:11:49 +0200517 vcpu_id, offset);
518 vgic_reg_access(mmio, reg, offset, mode);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200519 *reg &= level_mask;
520
Christoffer Dall9da48b52014-06-14 22:30:45 +0200521 /* Ignore writes to SGIs */
522 if (offset < 2) {
523 *reg &= ~0xffff;
524 *reg |= orig & 0xffff;
525 }
526
Andre Przywarad97f6832014-06-11 14:11:49 +0200527 vgic_update_state(kvm);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500528 return true;
529 }
530
531 return false;
532}
533
Christoffer Dall544c5722015-10-17 17:55:12 +0200534/*
535 * If a mapped interrupt's state has been modified by the guest such that it
536 * is no longer active or pending, without it have gone through the sync path,
537 * then the map->active field must be cleared so the interrupt can be taken
538 * again.
539 */
540static void vgic_handle_clear_mapped_irq(struct kvm_vcpu *vcpu)
541{
542 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
543 struct list_head *root;
544 struct irq_phys_map_entry *entry;
545 struct irq_phys_map *map;
546
547 rcu_read_lock();
548
549 /* Check for PPIs */
550 root = &vgic_cpu->irq_phys_map_list;
551 list_for_each_entry_rcu(entry, root, entry) {
552 map = &entry->map;
553
554 if (!vgic_dist_irq_is_pending(vcpu, map->virt_irq) &&
555 !vgic_irq_is_active(vcpu, map->virt_irq))
556 map->active = false;
557 }
558
559 rcu_read_unlock();
560}
561
Andre Przywara83215812014-06-07 00:53:08 +0200562bool vgic_handle_clear_pending_reg(struct kvm *kvm,
563 struct kvm_exit_mmio *mmio,
564 phys_addr_t offset, int vcpu_id)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500565{
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200566 u32 *level_active;
Christoffer Dall9da48b52014-06-14 22:30:45 +0200567 u32 *reg, orig;
Andre Przywarad97f6832014-06-11 14:11:49 +0200568 int mode = ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT;
569 struct vgic_dist *dist = &kvm->arch.vgic;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200570
Andre Przywarad97f6832014-06-11 14:11:49 +0200571 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
Christoffer Dall9da48b52014-06-14 22:30:45 +0200572 orig = *reg;
Andre Przywarad97f6832014-06-11 14:11:49 +0200573 vgic_reg_access(mmio, reg, offset, mode);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500574 if (mmio->is_write) {
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200575 /* Re-set level triggered level-active interrupts */
576 level_active = vgic_bitmap_get_reg(&dist->irq_level,
Andre Przywarad97f6832014-06-11 14:11:49 +0200577 vcpu_id, offset);
578 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200579 *reg |= *level_active;
580
Christoffer Dall9da48b52014-06-14 22:30:45 +0200581 /* Ignore writes to SGIs */
582 if (offset < 2) {
583 *reg &= ~0xffff;
584 *reg |= orig & 0xffff;
585 }
586
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200587 /* Clear soft-pending flags */
588 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
Andre Przywarad97f6832014-06-11 14:11:49 +0200589 vcpu_id, offset);
590 vgic_reg_access(mmio, reg, offset, mode);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200591
Christoffer Dall544c5722015-10-17 17:55:12 +0200592 vgic_handle_clear_mapped_irq(kvm_get_vcpu(kvm, vcpu_id));
Andre Przywarad97f6832014-06-11 14:11:49 +0200593 vgic_update_state(kvm);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500594 return true;
595 }
Marc Zyngierb47ef922013-01-21 19:36:14 -0500596 return false;
597}
598
Christoffer Dall47a98b12015-03-13 17:02:54 +0000599bool vgic_handle_set_active_reg(struct kvm *kvm,
600 struct kvm_exit_mmio *mmio,
601 phys_addr_t offset, int vcpu_id)
602{
603 u32 *reg;
604 struct vgic_dist *dist = &kvm->arch.vgic;
605
606 reg = vgic_bitmap_get_reg(&dist->irq_active, vcpu_id, offset);
607 vgic_reg_access(mmio, reg, offset,
608 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
609
610 if (mmio->is_write) {
611 vgic_update_state(kvm);
612 return true;
613 }
614
615 return false;
616}
617
618bool vgic_handle_clear_active_reg(struct kvm *kvm,
619 struct kvm_exit_mmio *mmio,
620 phys_addr_t offset, int vcpu_id)
621{
622 u32 *reg;
623 struct vgic_dist *dist = &kvm->arch.vgic;
624
625 reg = vgic_bitmap_get_reg(&dist->irq_active, vcpu_id, offset);
626 vgic_reg_access(mmio, reg, offset,
627 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
628
629 if (mmio->is_write) {
Christoffer Dall544c5722015-10-17 17:55:12 +0200630 vgic_handle_clear_mapped_irq(kvm_get_vcpu(kvm, vcpu_id));
Christoffer Dall47a98b12015-03-13 17:02:54 +0000631 vgic_update_state(kvm);
632 return true;
633 }
634
635 return false;
636}
637
Marc Zyngierb47ef922013-01-21 19:36:14 -0500638static u32 vgic_cfg_expand(u16 val)
639{
640 u32 res = 0;
641 int i;
642
643 /*
644 * Turn a 16bit value like abcd...mnop into a 32bit word
645 * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
646 */
647 for (i = 0; i < 16; i++)
648 res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
649
650 return res;
651}
652
653static u16 vgic_cfg_compress(u32 val)
654{
655 u16 res = 0;
656 int i;
657
658 /*
659 * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
660 * abcd...mnop which is what we really care about.
661 */
662 for (i = 0; i < 16; i++)
663 res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
664
665 return res;
666}
667
668/*
669 * The distributor uses 2 bits per IRQ for the CFG register, but the
670 * LSB is always 0. As such, we only keep the upper bit, and use the
671 * two above functions to compress/expand the bits
672 */
Andre Przywara83215812014-06-07 00:53:08 +0200673bool vgic_handle_cfg_reg(u32 *reg, struct kvm_exit_mmio *mmio,
674 phys_addr_t offset)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500675{
676 u32 val;
Marc Zyngier6545eae2013-08-29 11:08:23 +0100677
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200678 if (offset & 4)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500679 val = *reg >> 16;
680 else
681 val = *reg & 0xffff;
682
683 val = vgic_cfg_expand(val);
684 vgic_reg_access(mmio, &val, offset,
685 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
686 if (mmio->is_write) {
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200687 if (offset < 8) {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500688 *reg = ~0U; /* Force PPIs/SGIs to 1 */
689 return false;
690 }
691
692 val = vgic_cfg_compress(val);
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200693 if (offset & 4) {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500694 *reg &= 0xffff;
695 *reg |= val << 16;
696 } else {
697 *reg &= 0xffff << 16;
698 *reg |= val;
699 }
700 }
701
702 return false;
703}
704
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800705/**
Christoffer Dall47a98b12015-03-13 17:02:54 +0000706 * vgic_unqueue_irqs - move pending/active IRQs from LRs to the distributor
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800707 * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs
708 *
Christoffer Dall47a98b12015-03-13 17:02:54 +0000709 * Move any IRQs that have already been assigned to LRs back to the
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800710 * emulated distributor state so that the complete emulated state can be read
711 * from the main emulation structures without investigating the LRs.
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800712 */
Andre Przywara83215812014-06-07 00:53:08 +0200713void vgic_unqueue_irqs(struct kvm_vcpu *vcpu)
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800714{
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800715 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100716 int i;
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800717
718 for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100719 struct vgic_lr lr = vgic_get_lr(vcpu, i);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800720
721 /*
722 * There are three options for the state bits:
723 *
724 * 01: pending
725 * 10: active
726 * 11: pending and active
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800727 */
Christoffer Dall47a98b12015-03-13 17:02:54 +0000728 BUG_ON(!(lr.state & LR_STATE_MASK));
729
730 /* Reestablish SGI source for pending and active IRQs */
731 if (lr.irq < VGIC_NR_SGIS)
732 add_sgi_source(vcpu, lr.irq, lr.source);
733
734 /*
735 * If the LR holds an active (10) or a pending and active (11)
736 * interrupt then move the active state to the
737 * distributor tracking bit.
738 */
739 if (lr.state & LR_STATE_ACTIVE) {
740 vgic_irq_set_active(vcpu, lr.irq);
741 lr.state &= ~LR_STATE_ACTIVE;
742 }
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800743
744 /*
745 * Reestablish the pending state on the distributor and the
746 * CPU interface. It may have already been pending, but that
747 * is fine, then we are only setting a few bits that were
748 * already set.
749 */
Christoffer Dall47a98b12015-03-13 17:02:54 +0000750 if (lr.state & LR_STATE_PENDING) {
751 vgic_dist_irq_set_pending(vcpu, lr.irq);
752 lr.state &= ~LR_STATE_PENDING;
753 }
754
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100755 vgic_set_lr(vcpu, i, lr);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800756
757 /*
Christoffer Dall47a98b12015-03-13 17:02:54 +0000758 * Mark the LR as free for other use.
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800759 */
Christoffer Dall47a98b12015-03-13 17:02:54 +0000760 BUG_ON(lr.state & LR_STATE_MASK);
761 vgic_retire_lr(i, lr.irq, vcpu);
762 vgic_irq_clear_queued(vcpu, lr.irq);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800763
764 /* Finally update the VGIC state. */
765 vgic_update_state(vcpu->kvm);
766 }
767}
768
Andre Przywara83215812014-06-07 00:53:08 +0200769const
Andre Przywaracf50a1e2015-03-26 14:39:32 +0000770struct vgic_io_range *vgic_find_range(const struct vgic_io_range *ranges,
Andre Przywara9f199d02015-03-26 14:39:33 +0000771 int len, gpa_t offset)
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500772{
Andre Przywara9f199d02015-03-26 14:39:33 +0000773 while (ranges->len) {
774 if (offset >= ranges->base &&
775 (offset + len) <= (ranges->base + ranges->len))
776 return ranges;
777 ranges++;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500778 }
779
780 return NULL;
781}
782
Marc Zyngierc3c91832014-07-08 12:09:04 +0100783static bool vgic_validate_access(const struct vgic_dist *dist,
Andre Przywaracf50a1e2015-03-26 14:39:32 +0000784 const struct vgic_io_range *range,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100785 unsigned long offset)
786{
787 int irq;
788
789 if (!range->bits_per_irq)
790 return true; /* Not an irq-based access */
791
792 irq = offset * 8 / range->bits_per_irq;
793 if (irq >= dist->nr_irqs)
794 return false;
795
796 return true;
797}
798
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200799/*
800 * Call the respective handler function for the given range.
801 * We split up any 64 bit accesses into two consecutive 32 bit
802 * handler calls and merge the result afterwards.
803 * We do this in a little endian fashion regardless of the host's
804 * or guest's endianness, because the GIC is always LE and the rest of
805 * the code (vgic_reg_access) also puts it in a LE fashion already.
806 * At this point we have already identified the handle function, so
807 * range points to that one entry and offset is relative to this.
808 */
809static bool call_range_handler(struct kvm_vcpu *vcpu,
810 struct kvm_exit_mmio *mmio,
811 unsigned long offset,
Andre Przywaracf50a1e2015-03-26 14:39:32 +0000812 const struct vgic_io_range *range)
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200813{
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200814 struct kvm_exit_mmio mmio32;
815 bool ret;
816
817 if (likely(mmio->len <= 4))
818 return range->handle_mmio(vcpu, mmio, offset);
819
820 /*
821 * Any access bigger than 4 bytes (that we currently handle in KVM)
822 * is actually 8 bytes long, caused by a 64-bit access
823 */
824
825 mmio32.len = 4;
826 mmio32.is_write = mmio->is_write;
Andre Przywara9fedf142014-11-13 16:21:35 +0000827 mmio32.private = mmio->private;
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200828
829 mmio32.phys_addr = mmio->phys_addr + 4;
Andre Przywara950324a2015-03-28 01:13:13 +0000830 mmio32.data = &((u32 *)mmio->data)[1];
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200831 ret = range->handle_mmio(vcpu, &mmio32, offset + 4);
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200832
833 mmio32.phys_addr = mmio->phys_addr;
Andre Przywara950324a2015-03-28 01:13:13 +0000834 mmio32.data = &((u32 *)mmio->data)[0];
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200835 ret |= range->handle_mmio(vcpu, &mmio32, offset);
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200836
837 return ret;
838}
839
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500840/**
Andre Przywara6777f772015-03-26 14:39:34 +0000841 * vgic_handle_mmio_access - handle an in-kernel MMIO access
842 * This is called by the read/write KVM IO device wrappers below.
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500843 * @vcpu: pointer to the vcpu performing the access
Andre Przywara6777f772015-03-26 14:39:34 +0000844 * @this: pointer to the KVM IO device in charge
845 * @addr: guest physical address of the access
846 * @len: size of the access
847 * @val: pointer to the data region
848 * @is_write: read or write access
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500849 *
Andre Przywara96415252014-06-02 22:44:37 +0200850 * returns true if the MMIO access could be performed
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500851 */
Andre Przywara6777f772015-03-26 14:39:34 +0000852static int vgic_handle_mmio_access(struct kvm_vcpu *vcpu,
853 struct kvm_io_device *this, gpa_t addr,
854 int len, void *val, bool is_write)
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500855{
Marc Zyngierb47ef922013-01-21 19:36:14 -0500856 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Andre Przywara6777f772015-03-26 14:39:34 +0000857 struct vgic_io_device *iodev = container_of(this,
858 struct vgic_io_device, dev);
859 struct kvm_run *run = vcpu->run;
860 const struct vgic_io_range *range;
861 struct kvm_exit_mmio mmio;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500862 bool updated_state;
Andre Przywara6777f772015-03-26 14:39:34 +0000863 gpa_t offset;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500864
Andre Przywara6777f772015-03-26 14:39:34 +0000865 offset = addr - iodev->addr;
866 range = vgic_find_range(iodev->reg_ranges, len, offset);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500867 if (unlikely(!range || !range->handle_mmio)) {
Andre Przywara6777f772015-03-26 14:39:34 +0000868 pr_warn("Unhandled access %d %08llx %d\n", is_write, addr, len);
869 return -ENXIO;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500870 }
871
Andre Przywara6777f772015-03-26 14:39:34 +0000872 mmio.phys_addr = addr;
873 mmio.len = len;
874 mmio.is_write = is_write;
Andre Przywara950324a2015-03-28 01:13:13 +0000875 mmio.data = val;
Andre Przywara6777f772015-03-26 14:39:34 +0000876 mmio.private = iodev->redist_vcpu;
877
878 spin_lock(&dist->lock);
Andre Przywara96415252014-06-02 22:44:37 +0200879 offset -= range->base;
Marc Zyngierc3c91832014-07-08 12:09:04 +0100880 if (vgic_validate_access(dist, range, offset)) {
Andre Przywara6777f772015-03-26 14:39:34 +0000881 updated_state = call_range_handler(vcpu, &mmio, offset, range);
Marc Zyngierc3c91832014-07-08 12:09:04 +0100882 } else {
Andre Przywara6777f772015-03-26 14:39:34 +0000883 if (!is_write)
884 memset(val, 0, len);
Marc Zyngierc3c91832014-07-08 12:09:04 +0100885 updated_state = false;
886 }
Andre Przywara6777f772015-03-26 14:39:34 +0000887 spin_unlock(&dist->lock);
Andre Przywara950324a2015-03-28 01:13:13 +0000888 run->mmio.is_write = is_write;
889 run->mmio.len = len;
890 run->mmio.phys_addr = addr;
891 memcpy(run->mmio.data, val, len);
892
Marc Zyngierb47ef922013-01-21 19:36:14 -0500893 kvm_handle_mmio_return(vcpu, run);
894
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500895 if (updated_state)
896 vgic_kick_vcpus(vcpu->kvm);
897
Andre Przywara6777f772015-03-26 14:39:34 +0000898 return 0;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500899}
900
Andre Przywara6777f772015-03-26 14:39:34 +0000901static int vgic_handle_mmio_read(struct kvm_vcpu *vcpu,
902 struct kvm_io_device *this,
903 gpa_t addr, int len, void *val)
Andre Przywara96415252014-06-02 22:44:37 +0200904{
Andre Przywara6777f772015-03-26 14:39:34 +0000905 return vgic_handle_mmio_access(vcpu, this, addr, len, val, false);
906}
Andre Przywara96415252014-06-02 22:44:37 +0200907
Andre Przywara6777f772015-03-26 14:39:34 +0000908static int vgic_handle_mmio_write(struct kvm_vcpu *vcpu,
909 struct kvm_io_device *this,
910 gpa_t addr, int len, const void *val)
911{
912 return vgic_handle_mmio_access(vcpu, this, addr, len, (void *)val,
913 true);
914}
915
916struct kvm_io_device_ops vgic_io_ops = {
917 .read = vgic_handle_mmio_read,
918 .write = vgic_handle_mmio_write,
919};
920
921/**
922 * vgic_register_kvm_io_dev - register VGIC register frame on the KVM I/O bus
923 * @kvm: The VM structure pointer
924 * @base: The (guest) base address for the register frame
925 * @len: Length of the register frame window
926 * @ranges: Describing the handler functions for each register
927 * @redist_vcpu_id: The VCPU ID to pass on to the handlers on call
928 * @iodev: Points to memory to be passed on to the handler
929 *
930 * @iodev stores the parameters of this function to be usable by the handler
931 * respectively the dispatcher function (since the KVM I/O bus framework lacks
932 * an opaque parameter). Initialization is done in this function, but the
933 * reference should be valid and unique for the whole VGIC lifetime.
934 * If the register frame is not mapped for a specific VCPU, pass -1 to
935 * @redist_vcpu_id.
936 */
937int vgic_register_kvm_io_dev(struct kvm *kvm, gpa_t base, int len,
938 const struct vgic_io_range *ranges,
939 int redist_vcpu_id,
940 struct vgic_io_device *iodev)
941{
942 struct kvm_vcpu *vcpu = NULL;
943 int ret;
944
945 if (redist_vcpu_id >= 0)
946 vcpu = kvm_get_vcpu(kvm, redist_vcpu_id);
947
948 iodev->addr = base;
949 iodev->len = len;
950 iodev->reg_ranges = ranges;
951 iodev->redist_vcpu = vcpu;
952
953 kvm_iodevice_init(&iodev->dev, &vgic_io_ops);
954
955 mutex_lock(&kvm->slots_lock);
956
957 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, base, len,
958 &iodev->dev);
959 mutex_unlock(&kvm->slots_lock);
960
961 /* Mark the iodev as invalid if registration fails. */
962 if (ret)
963 iodev->dev.ops = NULL;
964
965 return ret;
Andre Przywara96415252014-06-02 22:44:37 +0200966}
967
Marc Zyngierfb65ab62014-07-08 12:09:02 +0100968static int vgic_nr_shared_irqs(struct vgic_dist *dist)
969{
970 return dist->nr_irqs - VGIC_NR_PRIVATE_IRQS;
971}
972
Christoffer Dall47a98b12015-03-13 17:02:54 +0000973static int compute_active_for_cpu(struct kvm_vcpu *vcpu)
974{
975 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
976 unsigned long *active, *enabled, *act_percpu, *act_shared;
977 unsigned long active_private, active_shared;
978 int nr_shared = vgic_nr_shared_irqs(dist);
979 int vcpu_id;
980
981 vcpu_id = vcpu->vcpu_id;
982 act_percpu = vcpu->arch.vgic_cpu.active_percpu;
983 act_shared = vcpu->arch.vgic_cpu.active_shared;
984
985 active = vgic_bitmap_get_cpu_map(&dist->irq_active, vcpu_id);
986 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
987 bitmap_and(act_percpu, active, enabled, VGIC_NR_PRIVATE_IRQS);
988
989 active = vgic_bitmap_get_shared_map(&dist->irq_active);
990 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
991 bitmap_and(act_shared, active, enabled, nr_shared);
992 bitmap_and(act_shared, act_shared,
993 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
994 nr_shared);
995
996 active_private = find_first_bit(act_percpu, VGIC_NR_PRIVATE_IRQS);
997 active_shared = find_first_bit(act_shared, nr_shared);
998
999 return (active_private < VGIC_NR_PRIVATE_IRQS ||
1000 active_shared < nr_shared);
1001}
1002
Marc Zyngierb47ef922013-01-21 19:36:14 -05001003static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
1004{
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001005 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1006 unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
1007 unsigned long pending_private, pending_shared;
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001008 int nr_shared = vgic_nr_shared_irqs(dist);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001009 int vcpu_id;
1010
1011 vcpu_id = vcpu->vcpu_id;
1012 pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
1013 pend_shared = vcpu->arch.vgic_cpu.pending_shared;
1014
Christoffer Dall227844f2014-06-09 12:27:18 +02001015 pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001016 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
1017 bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
1018
Christoffer Dall227844f2014-06-09 12:27:18 +02001019 pending = vgic_bitmap_get_shared_map(&dist->irq_pending);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001020 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001021 bitmap_and(pend_shared, pending, enabled, nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001022 bitmap_and(pend_shared, pend_shared,
1023 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001024 nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001025
1026 pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001027 pending_shared = find_first_bit(pend_shared, nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001028 return (pending_private < VGIC_NR_PRIVATE_IRQS ||
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001029 pending_shared < vgic_nr_shared_irqs(dist));
Marc Zyngierb47ef922013-01-21 19:36:14 -05001030}
1031
1032/*
1033 * Update the interrupt state and determine which CPUs have pending
Christoffer Dall47a98b12015-03-13 17:02:54 +00001034 * or active interrupts. Must be called with distributor lock held.
Marc Zyngierb47ef922013-01-21 19:36:14 -05001035 */
Andre Przywara83215812014-06-07 00:53:08 +02001036void vgic_update_state(struct kvm *kvm)
Marc Zyngierb47ef922013-01-21 19:36:14 -05001037{
1038 struct vgic_dist *dist = &kvm->arch.vgic;
1039 struct kvm_vcpu *vcpu;
1040 int c;
1041
1042 if (!dist->enabled) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001043 set_bit(0, dist->irq_pending_on_cpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001044 return;
1045 }
1046
1047 kvm_for_each_vcpu(c, vcpu, kvm) {
Christoffer Dall47a98b12015-03-13 17:02:54 +00001048 if (compute_pending_for_cpu(vcpu))
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001049 set_bit(c, dist->irq_pending_on_cpu);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001050
1051 if (compute_active_for_cpu(vcpu))
1052 set_bit(c, dist->irq_active_on_cpu);
1053 else
1054 clear_bit(c, dist->irq_active_on_cpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001055 }
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001056}
Christoffer Dall330690c2013-01-21 19:36:13 -05001057
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001058static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr)
1059{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001060 return vgic_ops->get_lr(vcpu, lr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001061}
1062
1063static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr,
1064 struct vgic_lr vlr)
1065{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001066 vgic_ops->set_lr(vcpu, lr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001067}
1068
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001069static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
1070 struct vgic_lr vlr)
1071{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001072 vgic_ops->sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001073}
1074
1075static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu)
1076{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001077 return vgic_ops->get_elrsr(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001078}
1079
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001080static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu)
1081{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001082 return vgic_ops->get_eisr(vcpu);
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001083}
1084
Christoffer Dallae705932015-03-13 17:02:56 +00001085static inline void vgic_clear_eisr(struct kvm_vcpu *vcpu)
1086{
1087 vgic_ops->clear_eisr(vcpu);
1088}
1089
Marc Zyngier495dd852013-06-04 11:02:10 +01001090static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu)
1091{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001092 return vgic_ops->get_interrupt_status(vcpu);
Marc Zyngier495dd852013-06-04 11:02:10 +01001093}
1094
Marc Zyngier909d9b52013-06-04 11:24:17 +01001095static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu)
1096{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001097 vgic_ops->enable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001098}
1099
1100static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu)
1101{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001102 vgic_ops->disable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001103}
1104
Andre Przywara83215812014-06-07 00:53:08 +02001105void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001106{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001107 vgic_ops->get_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001108}
1109
Andre Przywara83215812014-06-07 00:53:08 +02001110void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001111{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001112 vgic_ops->set_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001113}
1114
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001115static inline void vgic_enable(struct kvm_vcpu *vcpu)
1116{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001117 vgic_ops->enable(vcpu);
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001118}
1119
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001120static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu)
1121{
1122 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1123 struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr);
1124
Christoffer Dallcff92112015-10-16 12:41:21 +02001125 /*
1126 * We must transfer the pending state back to the distributor before
1127 * retiring the LR, otherwise we may loose edge-triggered interrupts.
1128 */
1129 if (vlr.state & LR_STATE_PENDING) {
1130 vgic_dist_irq_set_pending(vcpu, irq);
1131 vlr.hwirq = 0;
1132 }
1133
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001134 vlr.state = 0;
1135 vgic_set_lr(vcpu, lr_nr, vlr);
1136 clear_bit(lr_nr, vgic_cpu->lr_used);
1137 vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
Christoffer Dallae705932015-03-13 17:02:56 +00001138 vgic_sync_lr_elrsr(vcpu, lr_nr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001139}
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001140
1141/*
1142 * An interrupt may have been disabled after being made pending on the
1143 * CPU interface (the classic case is a timer running while we're
1144 * rebooting the guest - the interrupt would kick as soon as the CPU
1145 * interface gets enabled, with deadly consequences).
1146 *
1147 * The solution is to examine already active LRs, and check the
1148 * interrupt is still enabled. If not, just retire it.
1149 */
1150static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
1151{
1152 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1153 int lr;
1154
Marc Zyngier8f186d52014-02-04 18:13:03 +00001155 for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001156 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001157
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001158 if (!vgic_irq_is_enabled(vcpu, vlr.irq)) {
1159 vgic_retire_lr(lr, vlr.irq, vcpu);
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001160 if (vgic_irq_is_queued(vcpu, vlr.irq))
1161 vgic_irq_clear_queued(vcpu, vlr.irq);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001162 }
1163 }
1164}
1165
Alex Bennée71760952015-03-13 17:02:53 +00001166static void vgic_queue_irq_to_lr(struct kvm_vcpu *vcpu, int irq,
1167 int lr_nr, struct vgic_lr vlr)
1168{
Christoffer Dall47a98b12015-03-13 17:02:54 +00001169 if (vgic_irq_is_active(vcpu, irq)) {
1170 vlr.state |= LR_STATE_ACTIVE;
1171 kvm_debug("Set active, clear distributor: 0x%x\n", vlr.state);
1172 vgic_irq_clear_active(vcpu, irq);
1173 vgic_update_state(vcpu->kvm);
Pavel Fedin437f9962015-09-25 17:00:29 +03001174 } else {
1175 WARN_ON(!vgic_dist_irq_is_pending(vcpu, irq));
Alex Bennée71760952015-03-13 17:02:53 +00001176 vlr.state |= LR_STATE_PENDING;
1177 kvm_debug("Set pending: 0x%x\n", vlr.state);
1178 }
1179
1180 if (!vgic_irq_is_edge(vcpu, irq))
1181 vlr.state |= LR_EOI_INT;
1182
Marc Zyngier08fd6462015-06-08 16:06:13 +01001183 if (vlr.irq >= VGIC_NR_SGIS) {
1184 struct irq_phys_map *map;
1185 map = vgic_irq_map_search(vcpu, irq);
1186
Marc Zyngier08fd6462015-06-08 16:06:13 +01001187 if (map) {
Marc Zyngier08fd6462015-06-08 16:06:13 +01001188 vlr.hwirq = map->phys_irq;
1189 vlr.state |= LR_HW;
1190 vlr.state &= ~LR_EOI_INT;
1191
Marc Zyngier08fd6462015-06-08 16:06:13 +01001192 /*
1193 * Make sure we're not going to sample this
1194 * again, as a HW-backed interrupt cannot be
1195 * in the PENDING_ACTIVE stage.
1196 */
1197 vgic_irq_set_queued(vcpu, irq);
1198 }
1199 }
1200
Alex Bennée71760952015-03-13 17:02:53 +00001201 vgic_set_lr(vcpu, lr_nr, vlr);
Paolo Bonzinibf0fb672015-04-07 18:09:20 +02001202 vgic_sync_lr_elrsr(vcpu, lr_nr, vlr);
Alex Bennée71760952015-03-13 17:02:53 +00001203}
1204
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001205/*
1206 * Queue an interrupt to a CPU virtual interface. Return true on success,
1207 * or false if it wasn't possible to queue it.
Andre Przywara1d916222014-06-07 00:53:08 +02001208 * sgi_source must be zero for any non-SGI interrupts.
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001209 */
Andre Przywara83215812014-06-07 00:53:08 +02001210bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001211{
1212 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001213 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001214 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001215 int lr;
1216
1217 /* Sanitize the input... */
1218 BUG_ON(sgi_source_id & ~7);
1219 BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001220 BUG_ON(irq >= dist->nr_irqs);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001221
1222 kvm_debug("Queue IRQ%d\n", irq);
1223
1224 lr = vgic_cpu->vgic_irq_lr_map[irq];
1225
1226 /* Do we have an active interrupt for the same CPUID? */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001227 if (lr != LR_EMPTY) {
1228 vlr = vgic_get_lr(vcpu, lr);
1229 if (vlr.source == sgi_source_id) {
1230 kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq);
1231 BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
Alex Bennée71760952015-03-13 17:02:53 +00001232 vgic_queue_irq_to_lr(vcpu, irq, lr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001233 return true;
1234 }
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001235 }
1236
1237 /* Try to use another LR for this interrupt */
1238 lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
Marc Zyngier8f186d52014-02-04 18:13:03 +00001239 vgic->nr_lr);
1240 if (lr >= vgic->nr_lr)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001241 return false;
1242
1243 kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001244 vgic_cpu->vgic_irq_lr_map[irq] = lr;
1245 set_bit(lr, vgic_cpu->lr_used);
1246
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001247 vlr.irq = irq;
1248 vlr.source = sgi_source_id;
Alex Bennée71760952015-03-13 17:02:53 +00001249 vlr.state = 0;
1250 vgic_queue_irq_to_lr(vcpu, irq, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001251
1252 return true;
1253}
1254
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001255static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
1256{
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001257 if (!vgic_can_sample_irq(vcpu, irq))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001258 return true; /* level interrupt, already queued */
1259
1260 if (vgic_queue_irq(vcpu, 0, irq)) {
1261 if (vgic_irq_is_edge(vcpu, irq)) {
Christoffer Dall227844f2014-06-09 12:27:18 +02001262 vgic_dist_irq_clear_pending(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001263 vgic_cpu_irq_clear(vcpu, irq);
1264 } else {
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001265 vgic_irq_set_queued(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001266 }
1267
1268 return true;
1269 }
1270
1271 return false;
1272}
1273
1274/*
1275 * Fill the list registers with pending interrupts before running the
1276 * guest.
1277 */
1278static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1279{
1280 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1281 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Christoffer Dall47a98b12015-03-13 17:02:54 +00001282 unsigned long *pa_percpu, *pa_shared;
Christoffer Dallcff92112015-10-16 12:41:21 +02001283 int i, vcpu_id;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001284 int overflow = 0;
Christoffer Dall47a98b12015-03-13 17:02:54 +00001285 int nr_shared = vgic_nr_shared_irqs(dist);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001286
1287 vcpu_id = vcpu->vcpu_id;
1288
Christoffer Dall47a98b12015-03-13 17:02:54 +00001289 pa_percpu = vcpu->arch.vgic_cpu.pend_act_percpu;
1290 pa_shared = vcpu->arch.vgic_cpu.pend_act_shared;
1291
1292 bitmap_or(pa_percpu, vgic_cpu->pending_percpu, vgic_cpu->active_percpu,
1293 VGIC_NR_PRIVATE_IRQS);
1294 bitmap_or(pa_shared, vgic_cpu->pending_shared, vgic_cpu->active_shared,
1295 nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001296 /*
1297 * We may not have any pending interrupt, or the interrupts
1298 * may have been serviced from another vcpu. In all cases,
1299 * move along.
1300 */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001301 if (!kvm_vgic_vcpu_pending_irq(vcpu) && !kvm_vgic_vcpu_active_irq(vcpu))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001302 goto epilog;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001303
1304 /* SGIs */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001305 for_each_set_bit(i, pa_percpu, VGIC_NR_SGIS) {
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001306 if (!queue_sgi(vcpu, i))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001307 overflow = 1;
1308 }
1309
1310 /* PPIs */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001311 for_each_set_bit_from(i, pa_percpu, VGIC_NR_PRIVATE_IRQS) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001312 if (!vgic_queue_hwirq(vcpu, i))
1313 overflow = 1;
1314 }
1315
1316 /* SPIs */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001317 for_each_set_bit(i, pa_shared, nr_shared) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001318 if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
1319 overflow = 1;
1320 }
1321
Christoffer Dall47a98b12015-03-13 17:02:54 +00001322
1323
1324
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001325epilog:
1326 if (overflow) {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001327 vgic_enable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001328 } else {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001329 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001330 /*
1331 * We're about to run this VCPU, and we've consumed
1332 * everything the distributor had in store for
1333 * us. Claim we don't have anything pending. We'll
1334 * adjust that if needed while exiting.
1335 */
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001336 clear_bit(vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001337 }
1338}
1339
1340static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1341{
Marc Zyngier495dd852013-06-04 11:02:10 +01001342 u32 status = vgic_get_interrupt_status(vcpu);
Eric Auger649cf732015-03-04 11:14:35 +01001343 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001344 bool level_pending = false;
Eric Auger174178f2015-03-04 11:14:36 +01001345 struct kvm *kvm = vcpu->kvm;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001346
Marc Zyngier495dd852013-06-04 11:02:10 +01001347 kvm_debug("STATUS = %08x\n", status);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001348
Marc Zyngier495dd852013-06-04 11:02:10 +01001349 if (status & INT_STATUS_EOI) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001350 /*
1351 * Some level interrupts have been EOIed. Clear their
1352 * active bit.
1353 */
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001354 u64 eisr = vgic_get_eisr(vcpu);
Christoffer Dall2df36a52014-09-28 16:04:26 +02001355 unsigned long *eisr_ptr = u64_to_bitmask(&eisr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001356 int lr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001357
Marc Zyngier8f186d52014-02-04 18:13:03 +00001358 for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001359 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001360 WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001361
Eric Auger649cf732015-03-04 11:14:35 +01001362 spin_lock(&dist->lock);
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001363 vgic_irq_clear_queued(vcpu, vlr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001364 WARN_ON(vlr.state & LR_STATE_MASK);
1365 vlr.state = 0;
1366 vgic_set_lr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001367
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001368 /*
1369 * If the IRQ was EOIed it was also ACKed and we we
1370 * therefore assume we can clear the soft pending
1371 * state (should it had been set) for this interrupt.
1372 *
1373 * Note: if the IRQ soft pending state was set after
1374 * the IRQ was acked, it actually shouldn't be
1375 * cleared, but we have no way of knowing that unless
1376 * we start trapping ACKs when the soft-pending state
1377 * is set.
1378 */
1379 vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
1380
Eric Auger174178f2015-03-04 11:14:36 +01001381 /*
1382 * kvm_notify_acked_irq calls kvm_set_irq()
1383 * to reset the IRQ level. Need to release the
1384 * lock for kvm_set_irq to grab it.
1385 */
1386 spin_unlock(&dist->lock);
1387
1388 kvm_notify_acked_irq(kvm, 0,
1389 vlr.irq - VGIC_NR_PRIVATE_IRQS);
1390 spin_lock(&dist->lock);
1391
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001392 /* Any additional pending interrupt? */
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001393 if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001394 vgic_cpu_irq_set(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001395 level_pending = true;
1396 } else {
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001397 vgic_dist_irq_clear_pending(vcpu, vlr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001398 vgic_cpu_irq_clear(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001399 }
Marc Zyngier75da01e2013-01-31 11:25:52 +00001400
Eric Auger649cf732015-03-04 11:14:35 +01001401 spin_unlock(&dist->lock);
1402
Marc Zyngier75da01e2013-01-31 11:25:52 +00001403 /*
1404 * Despite being EOIed, the LR may not have
1405 * been marked as empty.
1406 */
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001407 vgic_sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001408 }
1409 }
1410
Marc Zyngier495dd852013-06-04 11:02:10 +01001411 if (status & INT_STATUS_UNDERFLOW)
Marc Zyngier909d9b52013-06-04 11:24:17 +01001412 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001413
Christoffer Dallae705932015-03-13 17:02:56 +00001414 /*
1415 * In the next iterations of the vcpu loop, if we sync the vgic state
1416 * after flushing it, but before entering the guest (this happens for
1417 * pending signals and vmid rollovers), then make sure we don't pick
1418 * up any old maintenance interrupts here.
1419 */
1420 vgic_clear_eisr(vcpu);
1421
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001422 return level_pending;
1423}
1424
Marc Zyngier08fd6462015-06-08 16:06:13 +01001425/*
1426 * Save the physical active state, and reset it to inactive.
1427 *
1428 * Return 1 if HW interrupt went from active to inactive, and 0 otherwise.
1429 */
1430static int vgic_sync_hwirq(struct kvm_vcpu *vcpu, struct vgic_lr vlr)
1431{
1432 struct irq_phys_map *map;
1433 int ret;
1434
1435 if (!(vlr.state & LR_HW))
1436 return 0;
1437
1438 map = vgic_irq_map_search(vcpu, vlr.irq);
Christoffer Dall544c5722015-10-17 17:55:12 +02001439 BUG_ON(!map);
Marc Zyngier08fd6462015-06-08 16:06:13 +01001440
1441 ret = irq_get_irqchip_state(map->irq,
1442 IRQCHIP_STATE_ACTIVE,
1443 &map->active);
1444
1445 WARN_ON(ret);
1446
Christoffer Dallcff92112015-10-16 12:41:21 +02001447 if (map->active)
Marc Zyngier08fd6462015-06-08 16:06:13 +01001448 return 0;
Marc Zyngier08fd6462015-06-08 16:06:13 +01001449
1450 return 1;
1451}
1452
Eric Auger649cf732015-03-04 11:14:35 +01001453/* Sync back the VGIC state after a guest run */
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001454static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1455{
1456 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1457 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001458 u64 elrsr;
1459 unsigned long *elrsr_ptr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001460 int lr, pending;
1461 bool level_pending;
1462
1463 level_pending = vgic_process_maintenance(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001464 elrsr = vgic_get_elrsr(vcpu);
Christoffer Dall2df36a52014-09-28 16:04:26 +02001465 elrsr_ptr = u64_to_bitmask(&elrsr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001466
Marc Zyngier08fd6462015-06-08 16:06:13 +01001467 /* Deal with HW interrupts, and clear mappings for empty LRs */
1468 for (lr = 0; lr < vgic->nr_lr; lr++) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001469 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001470
Marc Zyngier08fd6462015-06-08 16:06:13 +01001471 if (!test_bit(lr, vgic_cpu->lr_used))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001472 continue;
1473
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001474 vlr = vgic_get_lr(vcpu, lr);
Marc Zyngier08fd6462015-06-08 16:06:13 +01001475 if (vgic_sync_hwirq(vcpu, vlr)) {
1476 /*
1477 * So this is a HW interrupt that the guest
1478 * EOI-ed. Clean the LR state and allow the
1479 * interrupt to be sampled again.
1480 */
1481 vlr.state = 0;
1482 vlr.hwirq = 0;
1483 vgic_set_lr(vcpu, lr, vlr);
1484 vgic_irq_clear_queued(vcpu, vlr.irq);
1485 set_bit(lr, elrsr_ptr);
1486 }
1487
1488 if (!test_bit(lr, elrsr_ptr))
1489 continue;
1490
1491 clear_bit(lr, vgic_cpu->lr_used);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001492
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001493 BUG_ON(vlr.irq >= dist->nr_irqs);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001494 vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001495 }
1496
1497 /* Check if we still have something up our sleeve... */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001498 pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr);
1499 if (level_pending || pending < vgic->nr_lr)
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001500 set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001501}
1502
1503void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1504{
1505 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1506
1507 if (!irqchip_in_kernel(vcpu->kvm))
1508 return;
1509
1510 spin_lock(&dist->lock);
1511 __kvm_vgic_flush_hwstate(vcpu);
1512 spin_unlock(&dist->lock);
1513}
1514
1515void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1516{
1517 if (!irqchip_in_kernel(vcpu->kvm))
1518 return;
1519
1520 __kvm_vgic_sync_hwstate(vcpu);
1521}
1522
1523int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
1524{
1525 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1526
1527 if (!irqchip_in_kernel(vcpu->kvm))
1528 return 0;
1529
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001530 return test_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001531}
1532
Christoffer Dall47a98b12015-03-13 17:02:54 +00001533int kvm_vgic_vcpu_active_irq(struct kvm_vcpu *vcpu)
1534{
1535 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1536
1537 if (!irqchip_in_kernel(vcpu->kvm))
1538 return 0;
1539
1540 return test_bit(vcpu->vcpu_id, dist->irq_active_on_cpu);
1541}
1542
1543
Andre Przywara83215812014-06-07 00:53:08 +02001544void vgic_kick_vcpus(struct kvm *kvm)
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001545{
1546 struct kvm_vcpu *vcpu;
1547 int c;
1548
1549 /*
1550 * We've injected an interrupt, time to find out who deserves
1551 * a good kick...
1552 */
1553 kvm_for_each_vcpu(c, vcpu, kvm) {
1554 if (kvm_vgic_vcpu_pending_irq(vcpu))
1555 kvm_vcpu_kick(vcpu);
1556 }
1557}
1558
1559static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
1560{
Christoffer Dall227844f2014-06-09 12:27:18 +02001561 int edge_triggered = vgic_irq_is_edge(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001562
1563 /*
1564 * Only inject an interrupt if:
1565 * - edge triggered and we have a rising edge
1566 * - level triggered and we change level
1567 */
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001568 if (edge_triggered) {
1569 int state = vgic_dist_irq_is_pending(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001570 return level > state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001571 } else {
1572 int state = vgic_dist_irq_get_level(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001573 return level != state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001574 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001575}
1576
Shannon Zhao016ed392014-11-19 10:11:25 +00001577static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
Marc Zyngier773299a2015-07-24 11:30:43 +01001578 struct irq_phys_map *map,
1579 unsigned int irq_num, bool level)
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001580{
1581 struct vgic_dist *dist = &kvm->arch.vgic;
1582 struct kvm_vcpu *vcpu;
Christoffer Dall227844f2014-06-09 12:27:18 +02001583 int edge_triggered, level_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001584 int enabled;
Andre Przywaraa0675c22014-06-07 00:54:51 +02001585 bool ret = true, can_inject = true;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001586
Marc Zyngier773299a2015-07-24 11:30:43 +01001587 if (irq_num >= min(kvm->arch.vgic.nr_irqs, 1020))
1588 return -EINVAL;
1589
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001590 spin_lock(&dist->lock);
1591
1592 vcpu = kvm_get_vcpu(kvm, cpuid);
Christoffer Dall227844f2014-06-09 12:27:18 +02001593 edge_triggered = vgic_irq_is_edge(vcpu, irq_num);
1594 level_triggered = !edge_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001595
1596 if (!vgic_validate_injection(vcpu, irq_num, level)) {
1597 ret = false;
1598 goto out;
1599 }
1600
1601 if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
1602 cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
Andre Przywaraa0675c22014-06-07 00:54:51 +02001603 if (cpuid == VCPU_NOT_ALLOCATED) {
1604 /* Pretend we use CPU0, and prevent injection */
1605 cpuid = 0;
1606 can_inject = false;
1607 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001608 vcpu = kvm_get_vcpu(kvm, cpuid);
1609 }
1610
1611 kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
1612
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001613 if (level) {
1614 if (level_triggered)
1615 vgic_dist_irq_set_level(vcpu, irq_num);
Christoffer Dall227844f2014-06-09 12:27:18 +02001616 vgic_dist_irq_set_pending(vcpu, irq_num);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001617 } else {
1618 if (level_triggered) {
1619 vgic_dist_irq_clear_level(vcpu, irq_num);
Pavel Fedin437f9962015-09-25 17:00:29 +03001620 if (!vgic_dist_irq_soft_pend(vcpu, irq_num)) {
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001621 vgic_dist_irq_clear_pending(vcpu, irq_num);
Pavel Fedin437f9962015-09-25 17:00:29 +03001622 vgic_cpu_irq_clear(vcpu, irq_num);
1623 if (!compute_pending_for_cpu(vcpu))
1624 clear_bit(cpuid, dist->irq_pending_on_cpu);
1625 }
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001626 }
wanghaibin7d39f9e32014-11-17 09:27:37 +00001627
1628 ret = false;
1629 goto out;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001630 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001631
1632 enabled = vgic_irq_is_enabled(vcpu, irq_num);
1633
Andre Przywaraa0675c22014-06-07 00:54:51 +02001634 if (!enabled || !can_inject) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001635 ret = false;
1636 goto out;
1637 }
1638
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001639 if (!vgic_can_sample_irq(vcpu, irq_num)) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001640 /*
1641 * Level interrupt in progress, will be picked up
1642 * when EOId.
1643 */
1644 ret = false;
1645 goto out;
1646 }
1647
1648 if (level) {
1649 vgic_cpu_irq_set(vcpu, irq_num);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001650 set_bit(cpuid, dist->irq_pending_on_cpu);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001651 }
1652
1653out:
1654 spin_unlock(&dist->lock);
1655
Marc Zyngier773299a2015-07-24 11:30:43 +01001656 if (ret) {
1657 /* kick the specified vcpu */
1658 kvm_vcpu_kick(kvm_get_vcpu(kvm, cpuid));
1659 }
1660
1661 return 0;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001662}
1663
Marc Zyngier773299a2015-07-24 11:30:43 +01001664static int vgic_lazy_init(struct kvm *kvm)
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001665{
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001666 int ret = 0;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001667
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001668 if (unlikely(!vgic_initialized(kvm))) {
Andre Przywara598921362014-06-03 09:33:10 +02001669 /*
1670 * We only provide the automatic initialization of the VGIC
1671 * for the legacy case of a GICv2. Any other type must
1672 * be explicitly initialized once setup with the respective
1673 * KVM device call.
1674 */
Marc Zyngier773299a2015-07-24 11:30:43 +01001675 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2)
1676 return -EBUSY;
1677
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001678 mutex_lock(&kvm->lock);
1679 ret = vgic_init(kvm);
1680 mutex_unlock(&kvm->lock);
Shannon Zhao016ed392014-11-19 10:11:25 +00001681 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001682
Marc Zyngier773299a2015-07-24 11:30:43 +01001683 return ret;
1684}
1685
1686/**
1687 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
1688 * @kvm: The VM structure pointer
1689 * @cpuid: The CPU for PPIs
1690 * @irq_num: The IRQ number that is assigned to the device. This IRQ
1691 * must not be mapped to a HW interrupt.
1692 * @level: Edge-triggered: true: to trigger the interrupt
1693 * false: to ignore the call
1694 * Level-sensitive true: raise the input signal
1695 * false: lower the input signal
1696 *
1697 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1698 * level-sensitive interrupts. You can think of the level parameter as 1
1699 * being HIGH and 0 being LOW and all devices being active-HIGH.
1700 */
1701int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
1702 bool level)
1703{
1704 struct irq_phys_map *map;
1705 int ret;
1706
1707 ret = vgic_lazy_init(kvm);
1708 if (ret)
1709 return ret;
1710
1711 map = vgic_irq_map_search(kvm_get_vcpu(kvm, cpuid), irq_num);
1712 if (map)
Andre Przywarafd1d0dd2015-04-10 16:17:59 +01001713 return -EINVAL;
1714
Marc Zyngier773299a2015-07-24 11:30:43 +01001715 return vgic_update_irq_pending(kvm, cpuid, NULL, irq_num, level);
1716}
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001717
Marc Zyngier773299a2015-07-24 11:30:43 +01001718/**
1719 * kvm_vgic_inject_mapped_irq - Inject a physically mapped IRQ to the vgic
1720 * @kvm: The VM structure pointer
1721 * @cpuid: The CPU for PPIs
1722 * @map: Pointer to a irq_phys_map structure describing the mapping
1723 * @level: Edge-triggered: true: to trigger the interrupt
1724 * false: to ignore the call
1725 * Level-sensitive true: raise the input signal
1726 * false: lower the input signal
1727 *
1728 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1729 * level-sensitive interrupts. You can think of the level parameter as 1
1730 * being HIGH and 0 being LOW and all devices being active-HIGH.
1731 */
1732int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid,
1733 struct irq_phys_map *map, bool level)
1734{
1735 int ret;
1736
1737 ret = vgic_lazy_init(kvm);
1738 if (ret)
1739 return ret;
1740
1741 return vgic_update_irq_pending(kvm, cpuid, map, map->virt_irq, level);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001742}
1743
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001744static irqreturn_t vgic_maintenance_handler(int irq, void *data)
1745{
1746 /*
1747 * We cannot rely on the vgic maintenance interrupt to be
1748 * delivered synchronously. This means we can only use it to
1749 * exit the VM, and we perform the handling of EOIed
1750 * interrupts on the exit path (see vgic_process_maintenance).
1751 */
1752 return IRQ_HANDLED;
1753}
1754
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01001755static struct list_head *vgic_get_irq_phys_map_list(struct kvm_vcpu *vcpu,
1756 int virt_irq)
1757{
1758 if (virt_irq < VGIC_NR_PRIVATE_IRQS)
1759 return &vcpu->arch.vgic_cpu.irq_phys_map_list;
1760 else
1761 return &vcpu->kvm->arch.vgic.irq_phys_map_list;
1762}
1763
1764/**
1765 * kvm_vgic_map_phys_irq - map a virtual IRQ to a physical IRQ
1766 * @vcpu: The VCPU pointer
1767 * @virt_irq: The virtual irq number
1768 * @irq: The Linux IRQ number
1769 *
1770 * Establish a mapping between a guest visible irq (@virt_irq) and a
1771 * Linux irq (@irq). On injection, @virt_irq will be associated with
1772 * the physical interrupt represented by @irq. This mapping can be
1773 * established multiple times as long as the parameters are the same.
1774 *
1775 * Returns a valid pointer on success, and an error pointer otherwise
1776 */
1777struct irq_phys_map *kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu,
1778 int virt_irq, int irq)
1779{
1780 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1781 struct list_head *root = vgic_get_irq_phys_map_list(vcpu, virt_irq);
1782 struct irq_phys_map *map;
1783 struct irq_phys_map_entry *entry;
1784 struct irq_desc *desc;
1785 struct irq_data *data;
1786 int phys_irq;
1787
1788 desc = irq_to_desc(irq);
1789 if (!desc) {
1790 kvm_err("%s: no interrupt descriptor\n", __func__);
1791 return ERR_PTR(-EINVAL);
1792 }
1793
1794 data = irq_desc_get_irq_data(desc);
1795 while (data->parent_data)
1796 data = data->parent_data;
1797
1798 phys_irq = data->hwirq;
1799
1800 /* Create a new mapping */
1801 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1802 if (!entry)
1803 return ERR_PTR(-ENOMEM);
1804
1805 spin_lock(&dist->irq_phys_map_lock);
1806
1807 /* Try to match an existing mapping */
1808 map = vgic_irq_map_search(vcpu, virt_irq);
1809 if (map) {
1810 /* Make sure this mapping matches */
1811 if (map->phys_irq != phys_irq ||
1812 map->irq != irq)
1813 map = ERR_PTR(-EINVAL);
1814
1815 /* Found an existing, valid mapping */
1816 goto out;
1817 }
1818
1819 map = &entry->map;
1820 map->virt_irq = virt_irq;
1821 map->phys_irq = phys_irq;
1822 map->irq = irq;
1823
1824 list_add_tail_rcu(&entry->entry, root);
1825
1826out:
1827 spin_unlock(&dist->irq_phys_map_lock);
1828 /* If we've found a hit in the existing list, free the useless
1829 * entry */
1830 if (IS_ERR(map) || map != &entry->map)
1831 kfree(entry);
1832 return map;
1833}
1834
1835static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu,
1836 int virt_irq)
1837{
1838 struct list_head *root = vgic_get_irq_phys_map_list(vcpu, virt_irq);
1839 struct irq_phys_map_entry *entry;
1840 struct irq_phys_map *map;
1841
1842 rcu_read_lock();
1843
1844 list_for_each_entry_rcu(entry, root, entry) {
1845 map = &entry->map;
1846 if (map->virt_irq == virt_irq) {
1847 rcu_read_unlock();
1848 return map;
1849 }
1850 }
1851
1852 rcu_read_unlock();
1853
1854 return NULL;
1855}
1856
1857static void vgic_free_phys_irq_map_rcu(struct rcu_head *rcu)
1858{
1859 struct irq_phys_map_entry *entry;
1860
1861 entry = container_of(rcu, struct irq_phys_map_entry, rcu);
1862 kfree(entry);
1863}
1864
1865/**
Marc Zyngier6e84e0e2015-06-08 16:13:30 +01001866 * kvm_vgic_get_phys_irq_active - Return the active state of a mapped IRQ
1867 *
1868 * Return the logical active state of a mapped interrupt. This doesn't
1869 * necessarily reflects the current HW state.
1870 */
1871bool kvm_vgic_get_phys_irq_active(struct irq_phys_map *map)
1872{
1873 BUG_ON(!map);
1874 return map->active;
1875}
1876
1877/**
1878 * kvm_vgic_set_phys_irq_active - Set the active state of a mapped IRQ
1879 *
1880 * Set the logical active state of a mapped interrupt. This doesn't
1881 * immediately affects the HW state.
1882 */
1883void kvm_vgic_set_phys_irq_active(struct irq_phys_map *map, bool active)
1884{
1885 BUG_ON(!map);
1886 map->active = active;
1887}
1888
1889/**
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01001890 * kvm_vgic_unmap_phys_irq - Remove a virtual to physical IRQ mapping
1891 * @vcpu: The VCPU pointer
1892 * @map: The pointer to a mapping obtained through kvm_vgic_map_phys_irq
1893 *
1894 * Remove an existing mapping between virtual and physical interrupts.
1895 */
1896int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, struct irq_phys_map *map)
1897{
1898 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1899 struct irq_phys_map_entry *entry;
1900 struct list_head *root;
1901
1902 if (!map)
1903 return -EINVAL;
1904
1905 root = vgic_get_irq_phys_map_list(vcpu, map->virt_irq);
1906
1907 spin_lock(&dist->irq_phys_map_lock);
1908
1909 list_for_each_entry(entry, root, entry) {
1910 if (&entry->map == map) {
1911 list_del_rcu(&entry->entry);
1912 call_rcu(&entry->rcu, vgic_free_phys_irq_map_rcu);
1913 break;
1914 }
1915 }
1916
1917 spin_unlock(&dist->irq_phys_map_lock);
1918
1919 return 0;
1920}
1921
1922static void vgic_destroy_irq_phys_map(struct kvm *kvm, struct list_head *root)
1923{
1924 struct vgic_dist *dist = &kvm->arch.vgic;
1925 struct irq_phys_map_entry *entry;
1926
1927 spin_lock(&dist->irq_phys_map_lock);
1928
1929 list_for_each_entry(entry, root, entry) {
1930 list_del_rcu(&entry->entry);
1931 call_rcu(&entry->rcu, vgic_free_phys_irq_map_rcu);
1932 }
1933
1934 spin_unlock(&dist->irq_phys_map_lock);
1935}
1936
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001937void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
1938{
1939 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1940
1941 kfree(vgic_cpu->pending_shared);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001942 kfree(vgic_cpu->active_shared);
1943 kfree(vgic_cpu->pend_act_shared);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001944 kfree(vgic_cpu->vgic_irq_lr_map);
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01001945 vgic_destroy_irq_phys_map(vcpu->kvm, &vgic_cpu->irq_phys_map_list);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001946 vgic_cpu->pending_shared = NULL;
Christoffer Dall47a98b12015-03-13 17:02:54 +00001947 vgic_cpu->active_shared = NULL;
1948 vgic_cpu->pend_act_shared = NULL;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001949 vgic_cpu->vgic_irq_lr_map = NULL;
1950}
1951
1952static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
1953{
1954 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1955
1956 int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8;
1957 vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001958 vgic_cpu->active_shared = kzalloc(sz, GFP_KERNEL);
1959 vgic_cpu->pend_act_shared = kzalloc(sz, GFP_KERNEL);
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001960 vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001961
Christoffer Dall47a98b12015-03-13 17:02:54 +00001962 if (!vgic_cpu->pending_shared
1963 || !vgic_cpu->active_shared
1964 || !vgic_cpu->pend_act_shared
1965 || !vgic_cpu->vgic_irq_lr_map) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001966 kvm_vgic_vcpu_destroy(vcpu);
1967 return -ENOMEM;
1968 }
1969
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001970 memset(vgic_cpu->vgic_irq_lr_map, LR_EMPTY, nr_irqs);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001971
1972 /*
Marc Zyngierca85f622013-06-18 19:17:28 +01001973 * Store the number of LRs per vcpu, so we don't have to go
1974 * all the way to the distributor structure to find out. Only
1975 * assembly code should use this one.
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001976 */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001977 vgic_cpu->nr_lr = vgic->nr_lr;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001978
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001979 return 0;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001980}
1981
Andre Przywara3caa2d82014-06-02 16:26:01 +02001982/**
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01001983 * kvm_vgic_vcpu_early_init - Earliest possible per-vcpu vgic init stage
1984 *
1985 * No memory allocation should be performed here, only static init.
1986 */
1987void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu)
1988{
1989 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1990 INIT_LIST_HEAD(&vgic_cpu->irq_phys_map_list);
1991}
1992
1993/**
Andre Przywara3caa2d82014-06-02 16:26:01 +02001994 * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW
1995 *
1996 * The host's GIC naturally limits the maximum amount of VCPUs a guest
1997 * can use.
1998 */
1999int kvm_vgic_get_max_vcpus(void)
2000{
2001 return vgic->max_gic_vcpus;
2002}
2003
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002004void kvm_vgic_destroy(struct kvm *kvm)
2005{
2006 struct vgic_dist *dist = &kvm->arch.vgic;
2007 struct kvm_vcpu *vcpu;
2008 int i;
2009
2010 kvm_for_each_vcpu(i, vcpu, kvm)
2011 kvm_vgic_vcpu_destroy(vcpu);
2012
2013 vgic_free_bitmap(&dist->irq_enabled);
2014 vgic_free_bitmap(&dist->irq_level);
2015 vgic_free_bitmap(&dist->irq_pending);
2016 vgic_free_bitmap(&dist->irq_soft_pend);
2017 vgic_free_bitmap(&dist->irq_queued);
2018 vgic_free_bitmap(&dist->irq_cfg);
2019 vgic_free_bytemap(&dist->irq_priority);
2020 if (dist->irq_spi_target) {
2021 for (i = 0; i < dist->nr_cpus; i++)
2022 vgic_free_bitmap(&dist->irq_spi_target[i]);
2023 }
2024 kfree(dist->irq_sgi_sources);
2025 kfree(dist->irq_spi_cpu);
Andre Przywaraa0675c22014-06-07 00:54:51 +02002026 kfree(dist->irq_spi_mpidr);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002027 kfree(dist->irq_spi_target);
2028 kfree(dist->irq_pending_on_cpu);
Christoffer Dall47a98b12015-03-13 17:02:54 +00002029 kfree(dist->irq_active_on_cpu);
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01002030 vgic_destroy_irq_phys_map(kvm, &dist->irq_phys_map_list);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002031 dist->irq_sgi_sources = NULL;
2032 dist->irq_spi_cpu = NULL;
2033 dist->irq_spi_target = NULL;
2034 dist->irq_pending_on_cpu = NULL;
Christoffer Dall47a98b12015-03-13 17:02:54 +00002035 dist->irq_active_on_cpu = NULL;
Christoffer Dall1f57be22014-12-09 14:30:36 +01002036 dist->nr_cpus = 0;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002037}
2038
2039/*
2040 * Allocate and initialize the various data structures. Must be called
2041 * with kvm->lock held!
2042 */
Andre Przywara83215812014-06-07 00:53:08 +02002043int vgic_init(struct kvm *kvm)
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002044{
2045 struct vgic_dist *dist = &kvm->arch.vgic;
2046 struct kvm_vcpu *vcpu;
2047 int nr_cpus, nr_irqs;
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00002048 int ret, i, vcpu_id;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002049
Christoffer Dall1f57be22014-12-09 14:30:36 +01002050 if (vgic_initialized(kvm))
Marc Zyngier4956f2b2014-07-08 12:09:06 +01002051 return 0;
Marc Zyngier5fb66da2014-07-08 12:09:05 +01002052
Marc Zyngier4956f2b2014-07-08 12:09:06 +01002053 nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus);
2054 if (!nr_cpus) /* No vcpus? Can't be good... */
Eric Auger66b030e2014-12-15 18:43:32 +01002055 return -ENODEV;
Marc Zyngier4956f2b2014-07-08 12:09:06 +01002056
2057 /*
2058 * If nobody configured the number of interrupts, use the
2059 * legacy one.
2060 */
Marc Zyngier5fb66da2014-07-08 12:09:05 +01002061 if (!dist->nr_irqs)
2062 dist->nr_irqs = VGIC_NR_IRQS_LEGACY;
2063
2064 nr_irqs = dist->nr_irqs;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002065
2066 ret = vgic_init_bitmap(&dist->irq_enabled, nr_cpus, nr_irqs);
2067 ret |= vgic_init_bitmap(&dist->irq_level, nr_cpus, nr_irqs);
2068 ret |= vgic_init_bitmap(&dist->irq_pending, nr_cpus, nr_irqs);
2069 ret |= vgic_init_bitmap(&dist->irq_soft_pend, nr_cpus, nr_irqs);
2070 ret |= vgic_init_bitmap(&dist->irq_queued, nr_cpus, nr_irqs);
Christoffer Dall47a98b12015-03-13 17:02:54 +00002071 ret |= vgic_init_bitmap(&dist->irq_active, nr_cpus, nr_irqs);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002072 ret |= vgic_init_bitmap(&dist->irq_cfg, nr_cpus, nr_irqs);
2073 ret |= vgic_init_bytemap(&dist->irq_priority, nr_cpus, nr_irqs);
2074
2075 if (ret)
2076 goto out;
2077
2078 dist->irq_sgi_sources = kzalloc(nr_cpus * VGIC_NR_SGIS, GFP_KERNEL);
2079 dist->irq_spi_cpu = kzalloc(nr_irqs - VGIC_NR_PRIVATE_IRQS, GFP_KERNEL);
2080 dist->irq_spi_target = kzalloc(sizeof(*dist->irq_spi_target) * nr_cpus,
2081 GFP_KERNEL);
2082 dist->irq_pending_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
2083 GFP_KERNEL);
Christoffer Dall47a98b12015-03-13 17:02:54 +00002084 dist->irq_active_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
2085 GFP_KERNEL);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002086 if (!dist->irq_sgi_sources ||
2087 !dist->irq_spi_cpu ||
2088 !dist->irq_spi_target ||
Christoffer Dall47a98b12015-03-13 17:02:54 +00002089 !dist->irq_pending_on_cpu ||
2090 !dist->irq_active_on_cpu) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002091 ret = -ENOMEM;
2092 goto out;
2093 }
2094
2095 for (i = 0; i < nr_cpus; i++)
2096 ret |= vgic_init_bitmap(&dist->irq_spi_target[i],
2097 nr_cpus, nr_irqs);
2098
2099 if (ret)
2100 goto out;
2101
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002102 ret = kvm->arch.vgic.vm_ops.init_model(kvm);
2103 if (ret)
2104 goto out;
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00002105
2106 kvm_for_each_vcpu(vcpu_id, vcpu, kvm) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002107 ret = vgic_vcpu_init_maps(vcpu, nr_irqs);
2108 if (ret) {
2109 kvm_err("VGIC: Failed to allocate vcpu memory\n");
2110 break;
2111 }
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002112
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00002113 for (i = 0; i < dist->nr_irqs; i++) {
2114 if (i < VGIC_NR_PPIS)
2115 vgic_bitmap_set_irq_val(&dist->irq_enabled,
2116 vcpu->vcpu_id, i, 1);
2117 if (i < VGIC_NR_PRIVATE_IRQS)
2118 vgic_bitmap_set_irq_val(&dist->irq_cfg,
2119 vcpu->vcpu_id, i,
2120 VGIC_CFG_EDGE);
2121 }
2122
2123 vgic_enable(vcpu);
2124 }
Marc Zyngier4956f2b2014-07-08 12:09:06 +01002125
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002126out:
2127 if (ret)
2128 kvm_vgic_destroy(kvm);
2129
2130 return ret;
2131}
2132
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002133static int init_vgic_model(struct kvm *kvm, int type)
2134{
2135 switch (type) {
2136 case KVM_DEV_TYPE_ARM_VGIC_V2:
2137 vgic_v2_init_emulation(kvm);
2138 break;
Andre Przywarab5d84ff2014-06-03 10:26:03 +02002139#ifdef CONFIG_ARM_GIC_V3
2140 case KVM_DEV_TYPE_ARM_VGIC_V3:
2141 vgic_v3_init_emulation(kvm);
2142 break;
2143#endif
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002144 default:
2145 return -ENODEV;
2146 }
2147
Andre Przywara3caa2d82014-06-02 16:26:01 +02002148 if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus)
2149 return -E2BIG;
2150
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002151 return 0;
2152}
2153
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01002154/**
2155 * kvm_vgic_early_init - Earliest possible vgic initialization stage
2156 *
2157 * No memory allocation should be performed here, only static init.
2158 */
2159void kvm_vgic_early_init(struct kvm *kvm)
2160{
2161 spin_lock_init(&kvm->arch.vgic.lock);
2162 spin_lock_init(&kvm->arch.vgic.irq_phys_map_lock);
2163 INIT_LIST_HEAD(&kvm->arch.vgic.irq_phys_map_list);
2164}
2165
Andre Przywara598921362014-06-03 09:33:10 +02002166int kvm_vgic_create(struct kvm *kvm, u32 type)
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002167{
Christoffer Dall6b50f542014-11-06 11:47:39 +00002168 int i, vcpu_lock_idx = -1, ret;
Christoffer Dall73306722013-10-25 17:29:18 +01002169 struct kvm_vcpu *vcpu;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002170
2171 mutex_lock(&kvm->lock);
2172
Andre Przywara4ce7ebd2014-10-26 23:18:14 +00002173 if (irqchip_in_kernel(kvm)) {
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002174 ret = -EEXIST;
2175 goto out;
2176 }
2177
Christoffer Dall73306722013-10-25 17:29:18 +01002178 /*
Andre Przywarab5d84ff2014-06-03 10:26:03 +02002179 * This function is also called by the KVM_CREATE_IRQCHIP handler,
2180 * which had no chance yet to check the availability of the GICv2
2181 * emulation. So check this here again. KVM_CREATE_DEVICE does
2182 * the proper checks already.
2183 */
Wei Yongjunb52104e2015-02-27 19:41:45 +08002184 if (type == KVM_DEV_TYPE_ARM_VGIC_V2 && !vgic->can_emulate_gicv2) {
2185 ret = -ENODEV;
2186 goto out;
2187 }
Andre Przywarab5d84ff2014-06-03 10:26:03 +02002188
2189 /*
Christoffer Dall73306722013-10-25 17:29:18 +01002190 * Any time a vcpu is run, vcpu_load is called which tries to grab the
2191 * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
2192 * that no other VCPUs are run while we create the vgic.
2193 */
Christoffer Dall6b50f542014-11-06 11:47:39 +00002194 ret = -EBUSY;
Christoffer Dall73306722013-10-25 17:29:18 +01002195 kvm_for_each_vcpu(i, vcpu, kvm) {
2196 if (!mutex_trylock(&vcpu->mutex))
2197 goto out_unlock;
2198 vcpu_lock_idx = i;
2199 }
2200
2201 kvm_for_each_vcpu(i, vcpu, kvm) {
Christoffer Dall6b50f542014-11-06 11:47:39 +00002202 if (vcpu->arch.has_run_once)
Christoffer Dall73306722013-10-25 17:29:18 +01002203 goto out_unlock;
Christoffer Dall73306722013-10-25 17:29:18 +01002204 }
Christoffer Dall6b50f542014-11-06 11:47:39 +00002205 ret = 0;
Christoffer Dall73306722013-10-25 17:29:18 +01002206
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002207 ret = init_vgic_model(kvm, type);
2208 if (ret)
2209 goto out_unlock;
2210
Marc Zyngierf982cf42014-05-15 10:03:25 +01002211 kvm->arch.vgic.in_kernel = true;
Andre Przywara598921362014-06-03 09:33:10 +02002212 kvm->arch.vgic.vgic_model = type;
Marc Zyngier8f186d52014-02-04 18:13:03 +00002213 kvm->arch.vgic.vctrl_base = vgic->vctrl_base;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002214 kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
2215 kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
Andre Przywaraa0675c22014-06-07 00:54:51 +02002216 kvm->arch.vgic.vgic_redist_base = VGIC_ADDR_UNDEF;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002217
Christoffer Dall73306722013-10-25 17:29:18 +01002218out_unlock:
2219 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
2220 vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
2221 mutex_unlock(&vcpu->mutex);
2222 }
2223
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002224out:
2225 mutex_unlock(&kvm->lock);
2226 return ret;
2227}
2228
Will Deacon1fa451b2014-08-26 15:13:24 +01002229static int vgic_ioaddr_overlap(struct kvm *kvm)
Christoffer Dall330690c2013-01-21 19:36:13 -05002230{
2231 phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
2232 phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
2233
2234 if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
2235 return 0;
2236 if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
2237 (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
2238 return -EBUSY;
2239 return 0;
2240}
2241
2242static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
2243 phys_addr_t addr, phys_addr_t size)
2244{
2245 int ret;
2246
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002247 if (addr & ~KVM_PHYS_MASK)
2248 return -E2BIG;
2249
2250 if (addr & (SZ_4K - 1))
2251 return -EINVAL;
2252
Christoffer Dall330690c2013-01-21 19:36:13 -05002253 if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
2254 return -EEXIST;
2255 if (addr + size < addr)
2256 return -EINVAL;
2257
Haibin Wang30c21172014-04-29 14:49:17 +08002258 *ioaddr = addr;
Christoffer Dall330690c2013-01-21 19:36:13 -05002259 ret = vgic_ioaddr_overlap(kvm);
2260 if (ret)
Haibin Wang30c21172014-04-29 14:49:17 +08002261 *ioaddr = VGIC_ADDR_UNDEF;
2262
Christoffer Dall330690c2013-01-21 19:36:13 -05002263 return ret;
2264}
2265
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002266/**
2267 * kvm_vgic_addr - set or get vgic VM base addresses
2268 * @kvm: pointer to the vm struct
Andre Przywaraac3d3732014-06-03 10:26:30 +02002269 * @type: the VGIC addr type, one of KVM_VGIC_V[23]_ADDR_TYPE_XXX
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002270 * @addr: pointer to address value
2271 * @write: if true set the address in the VM address space, if false read the
2272 * address
2273 *
2274 * Set or get the vgic base addresses for the distributor and the virtual CPU
2275 * interface in the VM physical address space. These addresses are properties
2276 * of the emulated core/SoC and therefore user space initially knows this
2277 * information.
2278 */
2279int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
Christoffer Dall330690c2013-01-21 19:36:13 -05002280{
2281 int r = 0;
2282 struct vgic_dist *vgic = &kvm->arch.vgic;
Andre Przywaraac3d3732014-06-03 10:26:30 +02002283 int type_needed;
2284 phys_addr_t *addr_ptr, block_size;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002285 phys_addr_t alignment;
Christoffer Dall330690c2013-01-21 19:36:13 -05002286
Christoffer Dall330690c2013-01-21 19:36:13 -05002287 mutex_lock(&kvm->lock);
2288 switch (type) {
2289 case KVM_VGIC_V2_ADDR_TYPE_DIST:
Andre Przywaraac3d3732014-06-03 10:26:30 +02002290 type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
2291 addr_ptr = &vgic->vgic_dist_base;
2292 block_size = KVM_VGIC_V2_DIST_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002293 alignment = SZ_4K;
Christoffer Dall330690c2013-01-21 19:36:13 -05002294 break;
2295 case KVM_VGIC_V2_ADDR_TYPE_CPU:
Andre Przywaraac3d3732014-06-03 10:26:30 +02002296 type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
2297 addr_ptr = &vgic->vgic_cpu_base;
2298 block_size = KVM_VGIC_V2_CPU_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002299 alignment = SZ_4K;
Christoffer Dall330690c2013-01-21 19:36:13 -05002300 break;
Andre Przywaraac3d3732014-06-03 10:26:30 +02002301#ifdef CONFIG_ARM_GIC_V3
2302 case KVM_VGIC_V3_ADDR_TYPE_DIST:
2303 type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
2304 addr_ptr = &vgic->vgic_dist_base;
2305 block_size = KVM_VGIC_V3_DIST_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002306 alignment = SZ_64K;
Andre Przywaraac3d3732014-06-03 10:26:30 +02002307 break;
2308 case KVM_VGIC_V3_ADDR_TYPE_REDIST:
2309 type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
2310 addr_ptr = &vgic->vgic_redist_base;
2311 block_size = KVM_VGIC_V3_REDIST_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002312 alignment = SZ_64K;
Andre Przywaraac3d3732014-06-03 10:26:30 +02002313 break;
2314#endif
Christoffer Dall330690c2013-01-21 19:36:13 -05002315 default:
2316 r = -ENODEV;
Andre Przywaraac3d3732014-06-03 10:26:30 +02002317 goto out;
Christoffer Dall330690c2013-01-21 19:36:13 -05002318 }
2319
Andre Przywaraac3d3732014-06-03 10:26:30 +02002320 if (vgic->vgic_model != type_needed) {
2321 r = -ENODEV;
2322 goto out;
2323 }
2324
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002325 if (write) {
2326 if (!IS_ALIGNED(*addr, alignment))
2327 r = -EINVAL;
2328 else
2329 r = vgic_ioaddr_assign(kvm, addr_ptr, *addr,
2330 block_size);
2331 } else {
Andre Przywaraac3d3732014-06-03 10:26:30 +02002332 *addr = *addr_ptr;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002333 }
Andre Przywaraac3d3732014-06-03 10:26:30 +02002334
2335out:
Christoffer Dall330690c2013-01-21 19:36:13 -05002336 mutex_unlock(&kvm->lock);
2337 return r;
2338}
Christoffer Dall73306722013-10-25 17:29:18 +01002339
Andre Przywara83215812014-06-07 00:53:08 +02002340int vgic_set_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
Christoffer Dall73306722013-10-25 17:29:18 +01002341{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002342 int r;
2343
2344 switch (attr->group) {
2345 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2346 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2347 u64 addr;
2348 unsigned long type = (unsigned long)attr->attr;
2349
2350 if (copy_from_user(&addr, uaddr, sizeof(addr)))
2351 return -EFAULT;
2352
2353 r = kvm_vgic_addr(dev->kvm, type, &addr, true);
2354 return (r == -ENODEV) ? -ENXIO : r;
2355 }
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002356 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2357 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2358 u32 val;
2359 int ret = 0;
2360
2361 if (get_user(val, uaddr))
2362 return -EFAULT;
2363
2364 /*
2365 * We require:
2366 * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs
2367 * - at most 1024 interrupts
2368 * - a multiple of 32 interrupts
2369 */
2370 if (val < (VGIC_NR_PRIVATE_IRQS + 32) ||
2371 val > VGIC_MAX_IRQS ||
2372 (val & 31))
2373 return -EINVAL;
2374
2375 mutex_lock(&dev->kvm->lock);
2376
Christoffer Dallc52edf52014-12-09 14:28:09 +01002377 if (vgic_ready(dev->kvm) || dev->kvm->arch.vgic.nr_irqs)
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002378 ret = -EBUSY;
2379 else
2380 dev->kvm->arch.vgic.nr_irqs = val;
2381
2382 mutex_unlock(&dev->kvm->lock);
2383
2384 return ret;
2385 }
Eric Auger065c0032014-12-15 18:43:33 +01002386 case KVM_DEV_ARM_VGIC_GRP_CTRL: {
2387 switch (attr->attr) {
2388 case KVM_DEV_ARM_VGIC_CTRL_INIT:
2389 r = vgic_init(dev->kvm);
2390 return r;
2391 }
2392 break;
2393 }
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002394 }
2395
Christoffer Dall73306722013-10-25 17:29:18 +01002396 return -ENXIO;
2397}
2398
Andre Przywara83215812014-06-07 00:53:08 +02002399int vgic_get_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
Christoffer Dall73306722013-10-25 17:29:18 +01002400{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002401 int r = -ENXIO;
2402
2403 switch (attr->group) {
2404 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2405 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2406 u64 addr;
2407 unsigned long type = (unsigned long)attr->attr;
2408
2409 r = kvm_vgic_addr(dev->kvm, type, &addr, false);
2410 if (r)
2411 return (r == -ENODEV) ? -ENXIO : r;
2412
2413 if (copy_to_user(uaddr, &addr, sizeof(addr)))
2414 return -EFAULT;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002415 break;
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002416 }
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002417 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2418 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
Andre Przywarab60da142014-08-21 11:08:27 +01002419
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002420 r = put_user(dev->kvm->arch.vgic.nr_irqs, uaddr);
2421 break;
2422 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01002423
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002424 }
2425
2426 return r;
Christoffer Dall73306722013-10-25 17:29:18 +01002427}
2428
Andre Przywaracf50a1e2015-03-26 14:39:32 +00002429int vgic_has_attr_regs(const struct vgic_io_range *ranges, phys_addr_t offset)
Christoffer Dallc07a0192013-10-25 21:17:31 +01002430{
Andre Przywara9f199d02015-03-26 14:39:33 +00002431 if (vgic_find_range(ranges, 4, offset))
Christoffer Dallc07a0192013-10-25 21:17:31 +01002432 return 0;
2433 else
2434 return -ENXIO;
2435}
2436
Will Deaconc06a8412014-09-02 10:27:34 +01002437static void vgic_init_maintenance_interrupt(void *info)
2438{
2439 enable_percpu_irq(vgic->maint_irq, 0);
2440}
2441
2442static int vgic_cpu_notify(struct notifier_block *self,
2443 unsigned long action, void *cpu)
2444{
2445 switch (action) {
2446 case CPU_STARTING:
2447 case CPU_STARTING_FROZEN:
2448 vgic_init_maintenance_interrupt(NULL);
2449 break;
2450 case CPU_DYING:
2451 case CPU_DYING_FROZEN:
2452 disable_percpu_irq(vgic->maint_irq);
2453 break;
2454 }
2455
2456 return NOTIFY_OK;
2457}
2458
2459static struct notifier_block vgic_cpu_nb = {
2460 .notifier_call = vgic_cpu_notify,
2461};
2462
2463static const struct of_device_id vgic_ids[] = {
Mark Rutland0f3724752015-03-05 14:47:44 +00002464 { .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, },
2465 { .compatible = "arm,cortex-a7-gic", .data = vgic_v2_probe, },
2466 { .compatible = "arm,gic-400", .data = vgic_v2_probe, },
2467 { .compatible = "arm,gic-v3", .data = vgic_v3_probe, },
Will Deaconc06a8412014-09-02 10:27:34 +01002468 {},
2469};
2470
2471int kvm_vgic_hyp_init(void)
2472{
2473 const struct of_device_id *matched_id;
Christoffer Dalla875daf2014-09-18 18:15:32 -07002474 const int (*vgic_probe)(struct device_node *,const struct vgic_ops **,
2475 const struct vgic_params **);
Will Deaconc06a8412014-09-02 10:27:34 +01002476 struct device_node *vgic_node;
2477 int ret;
2478
2479 vgic_node = of_find_matching_node_and_match(NULL,
2480 vgic_ids, &matched_id);
2481 if (!vgic_node) {
2482 kvm_err("error: no compatible GIC node found\n");
2483 return -ENODEV;
2484 }
2485
2486 vgic_probe = matched_id->data;
2487 ret = vgic_probe(vgic_node, &vgic_ops, &vgic);
2488 if (ret)
2489 return ret;
2490
2491 ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler,
2492 "vgic", kvm_get_running_vcpus());
2493 if (ret) {
2494 kvm_err("Cannot register interrupt %d\n", vgic->maint_irq);
2495 return ret;
2496 }
2497
2498 ret = __register_cpu_notifier(&vgic_cpu_nb);
2499 if (ret) {
2500 kvm_err("Cannot register vgic CPU notifier\n");
2501 goto out_free_irq;
2502 }
2503
Will Deaconc06a8412014-09-02 10:27:34 +01002504 on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
2505
Andre Przywaraea2f83a2014-10-26 23:17:00 +00002506 return 0;
Will Deaconc06a8412014-09-02 10:27:34 +01002507
2508out_free_irq:
2509 free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus());
2510 return ret;
2511}
Eric Auger174178f2015-03-04 11:14:36 +01002512
2513int kvm_irq_map_gsi(struct kvm *kvm,
2514 struct kvm_kernel_irq_routing_entry *entries,
2515 int gsi)
2516{
Eric Auger0b3289e2015-04-13 15:01:59 +02002517 return 0;
Eric Auger174178f2015-03-04 11:14:36 +01002518}
2519
2520int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin)
2521{
2522 return pin;
2523}
2524
2525int kvm_set_irq(struct kvm *kvm, int irq_source_id,
2526 u32 irq, int level, bool line_status)
2527{
2528 unsigned int spi = irq + VGIC_NR_PRIVATE_IRQS;
2529
2530 trace_kvm_set_irq(irq, level, irq_source_id);
2531
2532 BUG_ON(!vgic_initialized(kvm));
2533
Eric Auger174178f2015-03-04 11:14:36 +01002534 return kvm_vgic_inject_irq(kvm, 0, spi, level);
Eric Auger174178f2015-03-04 11:14:36 +01002535}
2536
2537/* MSI not implemented yet */
2538int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
2539 struct kvm *kvm, int irq_source_id,
2540 int level, bool line_status)
2541{
2542 return 0;
2543}