blob: 51c9900756248b6d8703636115ddb0289aa044ab [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
Andre Przywara83215812014-06-07 00:53:08 +0200534bool vgic_handle_clear_pending_reg(struct kvm *kvm,
535 struct kvm_exit_mmio *mmio,
536 phys_addr_t offset, int vcpu_id)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500537{
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200538 u32 *level_active;
Christoffer Dall9da48b52014-06-14 22:30:45 +0200539 u32 *reg, orig;
Andre Przywarad97f6832014-06-11 14:11:49 +0200540 int mode = ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT;
541 struct vgic_dist *dist = &kvm->arch.vgic;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200542
Andre Przywarad97f6832014-06-11 14:11:49 +0200543 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
Christoffer Dall9da48b52014-06-14 22:30:45 +0200544 orig = *reg;
Andre Przywarad97f6832014-06-11 14:11:49 +0200545 vgic_reg_access(mmio, reg, offset, mode);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500546 if (mmio->is_write) {
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200547 /* Re-set level triggered level-active interrupts */
548 level_active = vgic_bitmap_get_reg(&dist->irq_level,
Andre Przywarad97f6832014-06-11 14:11:49 +0200549 vcpu_id, offset);
550 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200551 *reg |= *level_active;
552
Christoffer Dall9da48b52014-06-14 22:30:45 +0200553 /* Ignore writes to SGIs */
554 if (offset < 2) {
555 *reg &= ~0xffff;
556 *reg |= orig & 0xffff;
557 }
558
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200559 /* Clear soft-pending flags */
560 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
Andre Przywarad97f6832014-06-11 14:11:49 +0200561 vcpu_id, offset);
562 vgic_reg_access(mmio, reg, offset, mode);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200563
Andre Przywarad97f6832014-06-11 14:11:49 +0200564 vgic_update_state(kvm);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500565 return true;
566 }
Marc Zyngierb47ef922013-01-21 19:36:14 -0500567 return false;
568}
569
Christoffer Dall47a98b12015-03-13 17:02:54 +0000570bool vgic_handle_set_active_reg(struct kvm *kvm,
571 struct kvm_exit_mmio *mmio,
572 phys_addr_t offset, int vcpu_id)
573{
574 u32 *reg;
575 struct vgic_dist *dist = &kvm->arch.vgic;
576
577 reg = vgic_bitmap_get_reg(&dist->irq_active, vcpu_id, offset);
578 vgic_reg_access(mmio, reg, offset,
579 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
580
581 if (mmio->is_write) {
582 vgic_update_state(kvm);
583 return true;
584 }
585
586 return false;
587}
588
589bool vgic_handle_clear_active_reg(struct kvm *kvm,
590 struct kvm_exit_mmio *mmio,
591 phys_addr_t offset, int vcpu_id)
592{
593 u32 *reg;
594 struct vgic_dist *dist = &kvm->arch.vgic;
595
596 reg = vgic_bitmap_get_reg(&dist->irq_active, vcpu_id, offset);
597 vgic_reg_access(mmio, reg, offset,
598 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
599
600 if (mmio->is_write) {
601 vgic_update_state(kvm);
602 return true;
603 }
604
605 return false;
606}
607
Marc Zyngierb47ef922013-01-21 19:36:14 -0500608static u32 vgic_cfg_expand(u16 val)
609{
610 u32 res = 0;
611 int i;
612
613 /*
614 * Turn a 16bit value like abcd...mnop into a 32bit word
615 * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
616 */
617 for (i = 0; i < 16; i++)
618 res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
619
620 return res;
621}
622
623static u16 vgic_cfg_compress(u32 val)
624{
625 u16 res = 0;
626 int i;
627
628 /*
629 * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
630 * abcd...mnop which is what we really care about.
631 */
632 for (i = 0; i < 16; i++)
633 res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
634
635 return res;
636}
637
638/*
639 * The distributor uses 2 bits per IRQ for the CFG register, but the
640 * LSB is always 0. As such, we only keep the upper bit, and use the
641 * two above functions to compress/expand the bits
642 */
Andre Przywara83215812014-06-07 00:53:08 +0200643bool vgic_handle_cfg_reg(u32 *reg, struct kvm_exit_mmio *mmio,
644 phys_addr_t offset)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500645{
646 u32 val;
Marc Zyngier6545eae2013-08-29 11:08:23 +0100647
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200648 if (offset & 4)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500649 val = *reg >> 16;
650 else
651 val = *reg & 0xffff;
652
653 val = vgic_cfg_expand(val);
654 vgic_reg_access(mmio, &val, offset,
655 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
656 if (mmio->is_write) {
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200657 if (offset < 8) {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500658 *reg = ~0U; /* Force PPIs/SGIs to 1 */
659 return false;
660 }
661
662 val = vgic_cfg_compress(val);
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200663 if (offset & 4) {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500664 *reg &= 0xffff;
665 *reg |= val << 16;
666 } else {
667 *reg &= 0xffff << 16;
668 *reg |= val;
669 }
670 }
671
672 return false;
673}
674
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800675/**
Christoffer Dall47a98b12015-03-13 17:02:54 +0000676 * vgic_unqueue_irqs - move pending/active IRQs from LRs to the distributor
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800677 * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs
678 *
Christoffer Dall47a98b12015-03-13 17:02:54 +0000679 * Move any IRQs that have already been assigned to LRs back to the
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800680 * emulated distributor state so that the complete emulated state can be read
681 * from the main emulation structures without investigating the LRs.
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800682 */
Andre Przywara83215812014-06-07 00:53:08 +0200683void vgic_unqueue_irqs(struct kvm_vcpu *vcpu)
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800684{
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800685 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100686 int i;
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800687
688 for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100689 struct vgic_lr lr = vgic_get_lr(vcpu, i);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800690
691 /*
692 * There are three options for the state bits:
693 *
694 * 01: pending
695 * 10: active
696 * 11: pending and active
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800697 */
Christoffer Dall47a98b12015-03-13 17:02:54 +0000698 BUG_ON(!(lr.state & LR_STATE_MASK));
699
700 /* Reestablish SGI source for pending and active IRQs */
701 if (lr.irq < VGIC_NR_SGIS)
702 add_sgi_source(vcpu, lr.irq, lr.source);
703
704 /*
705 * If the LR holds an active (10) or a pending and active (11)
706 * interrupt then move the active state to the
707 * distributor tracking bit.
708 */
709 if (lr.state & LR_STATE_ACTIVE) {
710 vgic_irq_set_active(vcpu, lr.irq);
711 lr.state &= ~LR_STATE_ACTIVE;
712 }
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800713
714 /*
715 * Reestablish the pending state on the distributor and the
716 * CPU interface. It may have already been pending, but that
717 * is fine, then we are only setting a few bits that were
718 * already set.
719 */
Christoffer Dall47a98b12015-03-13 17:02:54 +0000720 if (lr.state & LR_STATE_PENDING) {
721 vgic_dist_irq_set_pending(vcpu, lr.irq);
722 lr.state &= ~LR_STATE_PENDING;
723 }
724
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100725 vgic_set_lr(vcpu, i, lr);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800726
727 /*
Christoffer Dall47a98b12015-03-13 17:02:54 +0000728 * Mark the LR as free for other use.
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800729 */
Christoffer Dall47a98b12015-03-13 17:02:54 +0000730 BUG_ON(lr.state & LR_STATE_MASK);
731 vgic_retire_lr(i, lr.irq, vcpu);
732 vgic_irq_clear_queued(vcpu, lr.irq);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800733
734 /* Finally update the VGIC state. */
735 vgic_update_state(vcpu->kvm);
736 }
737}
738
Andre Przywara83215812014-06-07 00:53:08 +0200739const
Andre Przywaracf50a1e2015-03-26 14:39:32 +0000740struct vgic_io_range *vgic_find_range(const struct vgic_io_range *ranges,
Andre Przywara9f199d02015-03-26 14:39:33 +0000741 int len, gpa_t offset)
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500742{
Andre Przywara9f199d02015-03-26 14:39:33 +0000743 while (ranges->len) {
744 if (offset >= ranges->base &&
745 (offset + len) <= (ranges->base + ranges->len))
746 return ranges;
747 ranges++;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500748 }
749
750 return NULL;
751}
752
Marc Zyngierc3c91832014-07-08 12:09:04 +0100753static bool vgic_validate_access(const struct vgic_dist *dist,
Andre Przywaracf50a1e2015-03-26 14:39:32 +0000754 const struct vgic_io_range *range,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100755 unsigned long offset)
756{
757 int irq;
758
759 if (!range->bits_per_irq)
760 return true; /* Not an irq-based access */
761
762 irq = offset * 8 / range->bits_per_irq;
763 if (irq >= dist->nr_irqs)
764 return false;
765
766 return true;
767}
768
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200769/*
770 * Call the respective handler function for the given range.
771 * We split up any 64 bit accesses into two consecutive 32 bit
772 * handler calls and merge the result afterwards.
773 * We do this in a little endian fashion regardless of the host's
774 * or guest's endianness, because the GIC is always LE and the rest of
775 * the code (vgic_reg_access) also puts it in a LE fashion already.
776 * At this point we have already identified the handle function, so
777 * range points to that one entry and offset is relative to this.
778 */
779static bool call_range_handler(struct kvm_vcpu *vcpu,
780 struct kvm_exit_mmio *mmio,
781 unsigned long offset,
Andre Przywaracf50a1e2015-03-26 14:39:32 +0000782 const struct vgic_io_range *range)
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200783{
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200784 struct kvm_exit_mmio mmio32;
785 bool ret;
786
787 if (likely(mmio->len <= 4))
788 return range->handle_mmio(vcpu, mmio, offset);
789
790 /*
791 * Any access bigger than 4 bytes (that we currently handle in KVM)
792 * is actually 8 bytes long, caused by a 64-bit access
793 */
794
795 mmio32.len = 4;
796 mmio32.is_write = mmio->is_write;
Andre Przywara9fedf142014-11-13 16:21:35 +0000797 mmio32.private = mmio->private;
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200798
799 mmio32.phys_addr = mmio->phys_addr + 4;
Andre Przywara950324a2015-03-28 01:13:13 +0000800 mmio32.data = &((u32 *)mmio->data)[1];
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200801 ret = range->handle_mmio(vcpu, &mmio32, offset + 4);
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200802
803 mmio32.phys_addr = mmio->phys_addr;
Andre Przywara950324a2015-03-28 01:13:13 +0000804 mmio32.data = &((u32 *)mmio->data)[0];
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200805 ret |= range->handle_mmio(vcpu, &mmio32, offset);
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200806
807 return ret;
808}
809
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500810/**
Andre Przywara6777f772015-03-26 14:39:34 +0000811 * vgic_handle_mmio_access - handle an in-kernel MMIO access
812 * This is called by the read/write KVM IO device wrappers below.
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500813 * @vcpu: pointer to the vcpu performing the access
Andre Przywara6777f772015-03-26 14:39:34 +0000814 * @this: pointer to the KVM IO device in charge
815 * @addr: guest physical address of the access
816 * @len: size of the access
817 * @val: pointer to the data region
818 * @is_write: read or write access
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500819 *
Andre Przywara96415252014-06-02 22:44:37 +0200820 * returns true if the MMIO access could be performed
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500821 */
Andre Przywara6777f772015-03-26 14:39:34 +0000822static int vgic_handle_mmio_access(struct kvm_vcpu *vcpu,
823 struct kvm_io_device *this, gpa_t addr,
824 int len, void *val, bool is_write)
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500825{
Marc Zyngierb47ef922013-01-21 19:36:14 -0500826 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Andre Przywara6777f772015-03-26 14:39:34 +0000827 struct vgic_io_device *iodev = container_of(this,
828 struct vgic_io_device, dev);
829 struct kvm_run *run = vcpu->run;
830 const struct vgic_io_range *range;
831 struct kvm_exit_mmio mmio;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500832 bool updated_state;
Andre Przywara6777f772015-03-26 14:39:34 +0000833 gpa_t offset;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500834
Andre Przywara6777f772015-03-26 14:39:34 +0000835 offset = addr - iodev->addr;
836 range = vgic_find_range(iodev->reg_ranges, len, offset);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500837 if (unlikely(!range || !range->handle_mmio)) {
Andre Przywara6777f772015-03-26 14:39:34 +0000838 pr_warn("Unhandled access %d %08llx %d\n", is_write, addr, len);
839 return -ENXIO;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500840 }
841
Andre Przywara6777f772015-03-26 14:39:34 +0000842 mmio.phys_addr = addr;
843 mmio.len = len;
844 mmio.is_write = is_write;
Andre Przywara950324a2015-03-28 01:13:13 +0000845 mmio.data = val;
Andre Przywara6777f772015-03-26 14:39:34 +0000846 mmio.private = iodev->redist_vcpu;
847
848 spin_lock(&dist->lock);
Andre Przywara96415252014-06-02 22:44:37 +0200849 offset -= range->base;
Marc Zyngierc3c91832014-07-08 12:09:04 +0100850 if (vgic_validate_access(dist, range, offset)) {
Andre Przywara6777f772015-03-26 14:39:34 +0000851 updated_state = call_range_handler(vcpu, &mmio, offset, range);
Marc Zyngierc3c91832014-07-08 12:09:04 +0100852 } else {
Andre Przywara6777f772015-03-26 14:39:34 +0000853 if (!is_write)
854 memset(val, 0, len);
Marc Zyngierc3c91832014-07-08 12:09:04 +0100855 updated_state = false;
856 }
Andre Przywara6777f772015-03-26 14:39:34 +0000857 spin_unlock(&dist->lock);
Andre Przywara950324a2015-03-28 01:13:13 +0000858 run->mmio.is_write = is_write;
859 run->mmio.len = len;
860 run->mmio.phys_addr = addr;
861 memcpy(run->mmio.data, val, len);
862
Marc Zyngierb47ef922013-01-21 19:36:14 -0500863 kvm_handle_mmio_return(vcpu, run);
864
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500865 if (updated_state)
866 vgic_kick_vcpus(vcpu->kvm);
867
Andre Przywara6777f772015-03-26 14:39:34 +0000868 return 0;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500869}
870
Andre Przywara6777f772015-03-26 14:39:34 +0000871static int vgic_handle_mmio_read(struct kvm_vcpu *vcpu,
872 struct kvm_io_device *this,
873 gpa_t addr, int len, void *val)
Andre Przywara96415252014-06-02 22:44:37 +0200874{
Andre Przywara6777f772015-03-26 14:39:34 +0000875 return vgic_handle_mmio_access(vcpu, this, addr, len, val, false);
876}
Andre Przywara96415252014-06-02 22:44:37 +0200877
Andre Przywara6777f772015-03-26 14:39:34 +0000878static int vgic_handle_mmio_write(struct kvm_vcpu *vcpu,
879 struct kvm_io_device *this,
880 gpa_t addr, int len, const void *val)
881{
882 return vgic_handle_mmio_access(vcpu, this, addr, len, (void *)val,
883 true);
884}
885
886struct kvm_io_device_ops vgic_io_ops = {
887 .read = vgic_handle_mmio_read,
888 .write = vgic_handle_mmio_write,
889};
890
891/**
892 * vgic_register_kvm_io_dev - register VGIC register frame on the KVM I/O bus
893 * @kvm: The VM structure pointer
894 * @base: The (guest) base address for the register frame
895 * @len: Length of the register frame window
896 * @ranges: Describing the handler functions for each register
897 * @redist_vcpu_id: The VCPU ID to pass on to the handlers on call
898 * @iodev: Points to memory to be passed on to the handler
899 *
900 * @iodev stores the parameters of this function to be usable by the handler
901 * respectively the dispatcher function (since the KVM I/O bus framework lacks
902 * an opaque parameter). Initialization is done in this function, but the
903 * reference should be valid and unique for the whole VGIC lifetime.
904 * If the register frame is not mapped for a specific VCPU, pass -1 to
905 * @redist_vcpu_id.
906 */
907int vgic_register_kvm_io_dev(struct kvm *kvm, gpa_t base, int len,
908 const struct vgic_io_range *ranges,
909 int redist_vcpu_id,
910 struct vgic_io_device *iodev)
911{
912 struct kvm_vcpu *vcpu = NULL;
913 int ret;
914
915 if (redist_vcpu_id >= 0)
916 vcpu = kvm_get_vcpu(kvm, redist_vcpu_id);
917
918 iodev->addr = base;
919 iodev->len = len;
920 iodev->reg_ranges = ranges;
921 iodev->redist_vcpu = vcpu;
922
923 kvm_iodevice_init(&iodev->dev, &vgic_io_ops);
924
925 mutex_lock(&kvm->slots_lock);
926
927 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, base, len,
928 &iodev->dev);
929 mutex_unlock(&kvm->slots_lock);
930
931 /* Mark the iodev as invalid if registration fails. */
932 if (ret)
933 iodev->dev.ops = NULL;
934
935 return ret;
Andre Przywara96415252014-06-02 22:44:37 +0200936}
937
Marc Zyngierfb65ab62014-07-08 12:09:02 +0100938static int vgic_nr_shared_irqs(struct vgic_dist *dist)
939{
940 return dist->nr_irqs - VGIC_NR_PRIVATE_IRQS;
941}
942
Christoffer Dall47a98b12015-03-13 17:02:54 +0000943static int compute_active_for_cpu(struct kvm_vcpu *vcpu)
944{
945 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
946 unsigned long *active, *enabled, *act_percpu, *act_shared;
947 unsigned long active_private, active_shared;
948 int nr_shared = vgic_nr_shared_irqs(dist);
949 int vcpu_id;
950
951 vcpu_id = vcpu->vcpu_id;
952 act_percpu = vcpu->arch.vgic_cpu.active_percpu;
953 act_shared = vcpu->arch.vgic_cpu.active_shared;
954
955 active = vgic_bitmap_get_cpu_map(&dist->irq_active, vcpu_id);
956 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
957 bitmap_and(act_percpu, active, enabled, VGIC_NR_PRIVATE_IRQS);
958
959 active = vgic_bitmap_get_shared_map(&dist->irq_active);
960 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
961 bitmap_and(act_shared, active, enabled, nr_shared);
962 bitmap_and(act_shared, act_shared,
963 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
964 nr_shared);
965
966 active_private = find_first_bit(act_percpu, VGIC_NR_PRIVATE_IRQS);
967 active_shared = find_first_bit(act_shared, nr_shared);
968
969 return (active_private < VGIC_NR_PRIVATE_IRQS ||
970 active_shared < nr_shared);
971}
972
Marc Zyngierb47ef922013-01-21 19:36:14 -0500973static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
974{
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500975 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
976 unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
977 unsigned long pending_private, pending_shared;
Marc Zyngierfb65ab62014-07-08 12:09:02 +0100978 int nr_shared = vgic_nr_shared_irqs(dist);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500979 int vcpu_id;
980
981 vcpu_id = vcpu->vcpu_id;
982 pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
983 pend_shared = vcpu->arch.vgic_cpu.pending_shared;
984
Christoffer Dall227844f2014-06-09 12:27:18 +0200985 pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500986 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
987 bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
988
Christoffer Dall227844f2014-06-09 12:27:18 +0200989 pending = vgic_bitmap_get_shared_map(&dist->irq_pending);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500990 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
Marc Zyngierfb65ab62014-07-08 12:09:02 +0100991 bitmap_and(pend_shared, pending, enabled, nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500992 bitmap_and(pend_shared, pend_shared,
993 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
Marc Zyngierfb65ab62014-07-08 12:09:02 +0100994 nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500995
996 pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
Marc Zyngierfb65ab62014-07-08 12:09:02 +0100997 pending_shared = find_first_bit(pend_shared, nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500998 return (pending_private < VGIC_NR_PRIVATE_IRQS ||
Marc Zyngierfb65ab62014-07-08 12:09:02 +0100999 pending_shared < vgic_nr_shared_irqs(dist));
Marc Zyngierb47ef922013-01-21 19:36:14 -05001000}
1001
1002/*
1003 * Update the interrupt state and determine which CPUs have pending
Christoffer Dall47a98b12015-03-13 17:02:54 +00001004 * or active interrupts. Must be called with distributor lock held.
Marc Zyngierb47ef922013-01-21 19:36:14 -05001005 */
Andre Przywara83215812014-06-07 00:53:08 +02001006void vgic_update_state(struct kvm *kvm)
Marc Zyngierb47ef922013-01-21 19:36:14 -05001007{
1008 struct vgic_dist *dist = &kvm->arch.vgic;
1009 struct kvm_vcpu *vcpu;
1010 int c;
1011
1012 if (!dist->enabled) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001013 set_bit(0, dist->irq_pending_on_cpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001014 return;
1015 }
1016
1017 kvm_for_each_vcpu(c, vcpu, kvm) {
Christoffer Dall47a98b12015-03-13 17:02:54 +00001018 if (compute_pending_for_cpu(vcpu))
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001019 set_bit(c, dist->irq_pending_on_cpu);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001020
1021 if (compute_active_for_cpu(vcpu))
1022 set_bit(c, dist->irq_active_on_cpu);
1023 else
1024 clear_bit(c, dist->irq_active_on_cpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001025 }
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001026}
Christoffer Dall330690c2013-01-21 19:36:13 -05001027
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001028static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr)
1029{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001030 return vgic_ops->get_lr(vcpu, lr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001031}
1032
1033static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr,
1034 struct vgic_lr vlr)
1035{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001036 vgic_ops->set_lr(vcpu, lr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001037}
1038
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001039static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
1040 struct vgic_lr vlr)
1041{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001042 vgic_ops->sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001043}
1044
1045static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu)
1046{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001047 return vgic_ops->get_elrsr(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001048}
1049
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001050static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu)
1051{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001052 return vgic_ops->get_eisr(vcpu);
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001053}
1054
Christoffer Dallae705932015-03-13 17:02:56 +00001055static inline void vgic_clear_eisr(struct kvm_vcpu *vcpu)
1056{
1057 vgic_ops->clear_eisr(vcpu);
1058}
1059
Marc Zyngier495dd852013-06-04 11:02:10 +01001060static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu)
1061{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001062 return vgic_ops->get_interrupt_status(vcpu);
Marc Zyngier495dd852013-06-04 11:02:10 +01001063}
1064
Marc Zyngier909d9b52013-06-04 11:24:17 +01001065static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu)
1066{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001067 vgic_ops->enable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001068}
1069
1070static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu)
1071{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001072 vgic_ops->disable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001073}
1074
Andre Przywara83215812014-06-07 00:53:08 +02001075void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001076{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001077 vgic_ops->get_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001078}
1079
Andre Przywara83215812014-06-07 00:53:08 +02001080void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001081{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001082 vgic_ops->set_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001083}
1084
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001085static inline void vgic_enable(struct kvm_vcpu *vcpu)
1086{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001087 vgic_ops->enable(vcpu);
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001088}
1089
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001090static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu)
1091{
1092 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1093 struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr);
1094
1095 vlr.state = 0;
1096 vgic_set_lr(vcpu, lr_nr, vlr);
1097 clear_bit(lr_nr, vgic_cpu->lr_used);
1098 vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
Christoffer Dallae705932015-03-13 17:02:56 +00001099 vgic_sync_lr_elrsr(vcpu, lr_nr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001100}
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001101
1102/*
1103 * An interrupt may have been disabled after being made pending on the
1104 * CPU interface (the classic case is a timer running while we're
1105 * rebooting the guest - the interrupt would kick as soon as the CPU
1106 * interface gets enabled, with deadly consequences).
1107 *
1108 * The solution is to examine already active LRs, and check the
1109 * interrupt is still enabled. If not, just retire it.
1110 */
1111static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
1112{
1113 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1114 int lr;
1115
Marc Zyngier8f186d52014-02-04 18:13:03 +00001116 for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001117 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001118
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001119 if (!vgic_irq_is_enabled(vcpu, vlr.irq)) {
1120 vgic_retire_lr(lr, vlr.irq, vcpu);
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001121 if (vgic_irq_is_queued(vcpu, vlr.irq))
1122 vgic_irq_clear_queued(vcpu, vlr.irq);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001123 }
1124 }
1125}
1126
Alex Bennée71760952015-03-13 17:02:53 +00001127static void vgic_queue_irq_to_lr(struct kvm_vcpu *vcpu, int irq,
1128 int lr_nr, struct vgic_lr vlr)
1129{
Christoffer Dall47a98b12015-03-13 17:02:54 +00001130 if (vgic_irq_is_active(vcpu, irq)) {
1131 vlr.state |= LR_STATE_ACTIVE;
1132 kvm_debug("Set active, clear distributor: 0x%x\n", vlr.state);
1133 vgic_irq_clear_active(vcpu, irq);
1134 vgic_update_state(vcpu->kvm);
1135 } else if (vgic_dist_irq_is_pending(vcpu, irq)) {
Alex Bennée71760952015-03-13 17:02:53 +00001136 vlr.state |= LR_STATE_PENDING;
1137 kvm_debug("Set pending: 0x%x\n", vlr.state);
1138 }
1139
1140 if (!vgic_irq_is_edge(vcpu, irq))
1141 vlr.state |= LR_EOI_INT;
1142
1143 vgic_set_lr(vcpu, lr_nr, vlr);
Paolo Bonzinibf0fb672015-04-07 18:09:20 +02001144 vgic_sync_lr_elrsr(vcpu, lr_nr, vlr);
Alex Bennée71760952015-03-13 17:02:53 +00001145}
1146
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001147/*
1148 * Queue an interrupt to a CPU virtual interface. Return true on success,
1149 * or false if it wasn't possible to queue it.
Andre Przywara1d916222014-06-07 00:53:08 +02001150 * sgi_source must be zero for any non-SGI interrupts.
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001151 */
Andre Przywara83215812014-06-07 00:53:08 +02001152bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001153{
1154 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001155 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001156 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001157 int lr;
1158
1159 /* Sanitize the input... */
1160 BUG_ON(sgi_source_id & ~7);
1161 BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001162 BUG_ON(irq >= dist->nr_irqs);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001163
1164 kvm_debug("Queue IRQ%d\n", irq);
1165
1166 lr = vgic_cpu->vgic_irq_lr_map[irq];
1167
1168 /* Do we have an active interrupt for the same CPUID? */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001169 if (lr != LR_EMPTY) {
1170 vlr = vgic_get_lr(vcpu, lr);
1171 if (vlr.source == sgi_source_id) {
1172 kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq);
1173 BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
Alex Bennée71760952015-03-13 17:02:53 +00001174 vgic_queue_irq_to_lr(vcpu, irq, lr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001175 return true;
1176 }
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001177 }
1178
1179 /* Try to use another LR for this interrupt */
1180 lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
Marc Zyngier8f186d52014-02-04 18:13:03 +00001181 vgic->nr_lr);
1182 if (lr >= vgic->nr_lr)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001183 return false;
1184
1185 kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001186 vgic_cpu->vgic_irq_lr_map[irq] = lr;
1187 set_bit(lr, vgic_cpu->lr_used);
1188
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001189 vlr.irq = irq;
1190 vlr.source = sgi_source_id;
Alex Bennée71760952015-03-13 17:02:53 +00001191 vlr.state = 0;
1192 vgic_queue_irq_to_lr(vcpu, irq, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001193
1194 return true;
1195}
1196
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001197static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
1198{
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001199 if (!vgic_can_sample_irq(vcpu, irq))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001200 return true; /* level interrupt, already queued */
1201
1202 if (vgic_queue_irq(vcpu, 0, irq)) {
1203 if (vgic_irq_is_edge(vcpu, irq)) {
Christoffer Dall227844f2014-06-09 12:27:18 +02001204 vgic_dist_irq_clear_pending(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001205 vgic_cpu_irq_clear(vcpu, irq);
1206 } else {
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001207 vgic_irq_set_queued(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001208 }
1209
1210 return true;
1211 }
1212
1213 return false;
1214}
1215
1216/*
1217 * Fill the list registers with pending interrupts before running the
1218 * guest.
1219 */
1220static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1221{
1222 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1223 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Christoffer Dall47a98b12015-03-13 17:02:54 +00001224 unsigned long *pa_percpu, *pa_shared;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001225 int i, vcpu_id;
1226 int overflow = 0;
Christoffer Dall47a98b12015-03-13 17:02:54 +00001227 int nr_shared = vgic_nr_shared_irqs(dist);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001228
1229 vcpu_id = vcpu->vcpu_id;
1230
Christoffer Dall47a98b12015-03-13 17:02:54 +00001231 pa_percpu = vcpu->arch.vgic_cpu.pend_act_percpu;
1232 pa_shared = vcpu->arch.vgic_cpu.pend_act_shared;
1233
1234 bitmap_or(pa_percpu, vgic_cpu->pending_percpu, vgic_cpu->active_percpu,
1235 VGIC_NR_PRIVATE_IRQS);
1236 bitmap_or(pa_shared, vgic_cpu->pending_shared, vgic_cpu->active_shared,
1237 nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001238 /*
1239 * We may not have any pending interrupt, or the interrupts
1240 * may have been serviced from another vcpu. In all cases,
1241 * move along.
1242 */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001243 if (!kvm_vgic_vcpu_pending_irq(vcpu) && !kvm_vgic_vcpu_active_irq(vcpu))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001244 goto epilog;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001245
1246 /* SGIs */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001247 for_each_set_bit(i, pa_percpu, VGIC_NR_SGIS) {
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001248 if (!queue_sgi(vcpu, i))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001249 overflow = 1;
1250 }
1251
1252 /* PPIs */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001253 for_each_set_bit_from(i, pa_percpu, VGIC_NR_PRIVATE_IRQS) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001254 if (!vgic_queue_hwirq(vcpu, i))
1255 overflow = 1;
1256 }
1257
1258 /* SPIs */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001259 for_each_set_bit(i, pa_shared, nr_shared) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001260 if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
1261 overflow = 1;
1262 }
1263
Christoffer Dall47a98b12015-03-13 17:02:54 +00001264
1265
1266
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001267epilog:
1268 if (overflow) {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001269 vgic_enable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001270 } else {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001271 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001272 /*
1273 * We're about to run this VCPU, and we've consumed
1274 * everything the distributor had in store for
1275 * us. Claim we don't have anything pending. We'll
1276 * adjust that if needed while exiting.
1277 */
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001278 clear_bit(vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001279 }
1280}
1281
1282static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1283{
Marc Zyngier495dd852013-06-04 11:02:10 +01001284 u32 status = vgic_get_interrupt_status(vcpu);
Eric Auger649cf732015-03-04 11:14:35 +01001285 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001286 bool level_pending = false;
Eric Auger174178f2015-03-04 11:14:36 +01001287 struct kvm *kvm = vcpu->kvm;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001288
Marc Zyngier495dd852013-06-04 11:02:10 +01001289 kvm_debug("STATUS = %08x\n", status);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001290
Marc Zyngier495dd852013-06-04 11:02:10 +01001291 if (status & INT_STATUS_EOI) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001292 /*
1293 * Some level interrupts have been EOIed. Clear their
1294 * active bit.
1295 */
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001296 u64 eisr = vgic_get_eisr(vcpu);
Christoffer Dall2df36a52014-09-28 16:04:26 +02001297 unsigned long *eisr_ptr = u64_to_bitmask(&eisr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001298 int lr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001299
Marc Zyngier8f186d52014-02-04 18:13:03 +00001300 for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001301 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001302 WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001303
Eric Auger649cf732015-03-04 11:14:35 +01001304 spin_lock(&dist->lock);
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001305 vgic_irq_clear_queued(vcpu, vlr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001306 WARN_ON(vlr.state & LR_STATE_MASK);
1307 vlr.state = 0;
1308 vgic_set_lr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001309
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001310 /*
1311 * If the IRQ was EOIed it was also ACKed and we we
1312 * therefore assume we can clear the soft pending
1313 * state (should it had been set) for this interrupt.
1314 *
1315 * Note: if the IRQ soft pending state was set after
1316 * the IRQ was acked, it actually shouldn't be
1317 * cleared, but we have no way of knowing that unless
1318 * we start trapping ACKs when the soft-pending state
1319 * is set.
1320 */
1321 vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
1322
Eric Auger174178f2015-03-04 11:14:36 +01001323 /*
1324 * kvm_notify_acked_irq calls kvm_set_irq()
1325 * to reset the IRQ level. Need to release the
1326 * lock for kvm_set_irq to grab it.
1327 */
1328 spin_unlock(&dist->lock);
1329
1330 kvm_notify_acked_irq(kvm, 0,
1331 vlr.irq - VGIC_NR_PRIVATE_IRQS);
1332 spin_lock(&dist->lock);
1333
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001334 /* Any additional pending interrupt? */
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001335 if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001336 vgic_cpu_irq_set(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001337 level_pending = true;
1338 } else {
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001339 vgic_dist_irq_clear_pending(vcpu, vlr.irq);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001340 vgic_cpu_irq_clear(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001341 }
Marc Zyngier75da01e2013-01-31 11:25:52 +00001342
Eric Auger649cf732015-03-04 11:14:35 +01001343 spin_unlock(&dist->lock);
1344
Marc Zyngier75da01e2013-01-31 11:25:52 +00001345 /*
1346 * Despite being EOIed, the LR may not have
1347 * been marked as empty.
1348 */
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001349 vgic_sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001350 }
1351 }
1352
Marc Zyngier495dd852013-06-04 11:02:10 +01001353 if (status & INT_STATUS_UNDERFLOW)
Marc Zyngier909d9b52013-06-04 11:24:17 +01001354 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001355
Christoffer Dallae705932015-03-13 17:02:56 +00001356 /*
1357 * In the next iterations of the vcpu loop, if we sync the vgic state
1358 * after flushing it, but before entering the guest (this happens for
1359 * pending signals and vmid rollovers), then make sure we don't pick
1360 * up any old maintenance interrupts here.
1361 */
1362 vgic_clear_eisr(vcpu);
1363
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001364 return level_pending;
1365}
1366
Eric Auger649cf732015-03-04 11:14:35 +01001367/* Sync back the VGIC state after a guest run */
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001368static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1369{
1370 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1371 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001372 u64 elrsr;
1373 unsigned long *elrsr_ptr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001374 int lr, pending;
1375 bool level_pending;
1376
1377 level_pending = vgic_process_maintenance(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001378 elrsr = vgic_get_elrsr(vcpu);
Christoffer Dall2df36a52014-09-28 16:04:26 +02001379 elrsr_ptr = u64_to_bitmask(&elrsr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001380
1381 /* Clear mappings for empty LRs */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001382 for_each_set_bit(lr, elrsr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001383 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001384
1385 if (!test_and_clear_bit(lr, vgic_cpu->lr_used))
1386 continue;
1387
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001388 vlr = vgic_get_lr(vcpu, lr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001389
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001390 BUG_ON(vlr.irq >= dist->nr_irqs);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001391 vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001392 }
1393
1394 /* Check if we still have something up our sleeve... */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001395 pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr);
1396 if (level_pending || pending < vgic->nr_lr)
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001397 set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001398}
1399
1400void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1401{
1402 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1403
1404 if (!irqchip_in_kernel(vcpu->kvm))
1405 return;
1406
1407 spin_lock(&dist->lock);
1408 __kvm_vgic_flush_hwstate(vcpu);
1409 spin_unlock(&dist->lock);
1410}
1411
1412void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1413{
1414 if (!irqchip_in_kernel(vcpu->kvm))
1415 return;
1416
1417 __kvm_vgic_sync_hwstate(vcpu);
1418}
1419
1420int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
1421{
1422 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1423
1424 if (!irqchip_in_kernel(vcpu->kvm))
1425 return 0;
1426
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001427 return test_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001428}
1429
Christoffer Dall47a98b12015-03-13 17:02:54 +00001430int kvm_vgic_vcpu_active_irq(struct kvm_vcpu *vcpu)
1431{
1432 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1433
1434 if (!irqchip_in_kernel(vcpu->kvm))
1435 return 0;
1436
1437 return test_bit(vcpu->vcpu_id, dist->irq_active_on_cpu);
1438}
1439
1440
Andre Przywara83215812014-06-07 00:53:08 +02001441void vgic_kick_vcpus(struct kvm *kvm)
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001442{
1443 struct kvm_vcpu *vcpu;
1444 int c;
1445
1446 /*
1447 * We've injected an interrupt, time to find out who deserves
1448 * a good kick...
1449 */
1450 kvm_for_each_vcpu(c, vcpu, kvm) {
1451 if (kvm_vgic_vcpu_pending_irq(vcpu))
1452 kvm_vcpu_kick(vcpu);
1453 }
1454}
1455
1456static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
1457{
Christoffer Dall227844f2014-06-09 12:27:18 +02001458 int edge_triggered = vgic_irq_is_edge(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001459
1460 /*
1461 * Only inject an interrupt if:
1462 * - edge triggered and we have a rising edge
1463 * - level triggered and we change level
1464 */
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001465 if (edge_triggered) {
1466 int state = vgic_dist_irq_is_pending(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001467 return level > state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001468 } else {
1469 int state = vgic_dist_irq_get_level(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001470 return level != state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001471 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001472}
1473
Shannon Zhao016ed392014-11-19 10:11:25 +00001474static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001475 unsigned int irq_num, bool level)
1476{
1477 struct vgic_dist *dist = &kvm->arch.vgic;
1478 struct kvm_vcpu *vcpu;
Christoffer Dall227844f2014-06-09 12:27:18 +02001479 int edge_triggered, level_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001480 int enabled;
Andre Przywaraa0675c22014-06-07 00:54:51 +02001481 bool ret = true, can_inject = true;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001482
1483 spin_lock(&dist->lock);
1484
1485 vcpu = kvm_get_vcpu(kvm, cpuid);
Christoffer Dall227844f2014-06-09 12:27:18 +02001486 edge_triggered = vgic_irq_is_edge(vcpu, irq_num);
1487 level_triggered = !edge_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001488
1489 if (!vgic_validate_injection(vcpu, irq_num, level)) {
1490 ret = false;
1491 goto out;
1492 }
1493
1494 if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
1495 cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
Andre Przywaraa0675c22014-06-07 00:54:51 +02001496 if (cpuid == VCPU_NOT_ALLOCATED) {
1497 /* Pretend we use CPU0, and prevent injection */
1498 cpuid = 0;
1499 can_inject = false;
1500 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001501 vcpu = kvm_get_vcpu(kvm, cpuid);
1502 }
1503
1504 kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
1505
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001506 if (level) {
1507 if (level_triggered)
1508 vgic_dist_irq_set_level(vcpu, irq_num);
Christoffer Dall227844f2014-06-09 12:27:18 +02001509 vgic_dist_irq_set_pending(vcpu, irq_num);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001510 } else {
1511 if (level_triggered) {
1512 vgic_dist_irq_clear_level(vcpu, irq_num);
1513 if (!vgic_dist_irq_soft_pend(vcpu, irq_num))
1514 vgic_dist_irq_clear_pending(vcpu, irq_num);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001515 }
wanghaibin7d39f9e32014-11-17 09:27:37 +00001516
1517 ret = false;
1518 goto out;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001519 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001520
1521 enabled = vgic_irq_is_enabled(vcpu, irq_num);
1522
Andre Przywaraa0675c22014-06-07 00:54:51 +02001523 if (!enabled || !can_inject) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001524 ret = false;
1525 goto out;
1526 }
1527
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001528 if (!vgic_can_sample_irq(vcpu, irq_num)) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001529 /*
1530 * Level interrupt in progress, will be picked up
1531 * when EOId.
1532 */
1533 ret = false;
1534 goto out;
1535 }
1536
1537 if (level) {
1538 vgic_cpu_irq_set(vcpu, irq_num);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001539 set_bit(cpuid, dist->irq_pending_on_cpu);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001540 }
1541
1542out:
1543 spin_unlock(&dist->lock);
1544
Shannon Zhao016ed392014-11-19 10:11:25 +00001545 return ret ? cpuid : -EINVAL;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001546}
1547
1548/**
1549 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
1550 * @kvm: The VM structure pointer
1551 * @cpuid: The CPU for PPIs
1552 * @irq_num: The IRQ number that is assigned to the device
1553 * @level: Edge-triggered: true: to trigger the interrupt
1554 * false: to ignore the call
1555 * Level-sensitive true: activates an interrupt
1556 * false: deactivates an interrupt
1557 *
1558 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1559 * level-sensitive interrupts. You can think of the level parameter as 1
1560 * being HIGH and 0 being LOW and all devices being active-HIGH.
1561 */
1562int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
1563 bool level)
1564{
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001565 int ret = 0;
Shannon Zhao016ed392014-11-19 10:11:25 +00001566 int vcpu_id;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001567
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001568 if (unlikely(!vgic_initialized(kvm))) {
Andre Przywara598921362014-06-03 09:33:10 +02001569 /*
1570 * We only provide the automatic initialization of the VGIC
1571 * for the legacy case of a GICv2. Any other type must
1572 * be explicitly initialized once setup with the respective
1573 * KVM device call.
1574 */
1575 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2) {
1576 ret = -EBUSY;
1577 goto out;
1578 }
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001579 mutex_lock(&kvm->lock);
1580 ret = vgic_init(kvm);
1581 mutex_unlock(&kvm->lock);
1582
1583 if (ret)
1584 goto out;
Shannon Zhao016ed392014-11-19 10:11:25 +00001585 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001586
Marc Zyngier4839ddc22015-06-17 14:43:35 +01001587 if (irq_num >= min(kvm->arch.vgic.nr_irqs, 1020))
Andre Przywarafd1d0dd2015-04-10 16:17:59 +01001588 return -EINVAL;
1589
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001590 vcpu_id = vgic_update_irq_pending(kvm, cpuid, irq_num, level);
1591 if (vcpu_id >= 0) {
1592 /* kick the specified vcpu */
1593 kvm_vcpu_kick(kvm_get_vcpu(kvm, vcpu_id));
1594 }
1595
1596out:
1597 return ret;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001598}
1599
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001600static irqreturn_t vgic_maintenance_handler(int irq, void *data)
1601{
1602 /*
1603 * We cannot rely on the vgic maintenance interrupt to be
1604 * delivered synchronously. This means we can only use it to
1605 * exit the VM, and we perform the handling of EOIed
1606 * interrupts on the exit path (see vgic_process_maintenance).
1607 */
1608 return IRQ_HANDLED;
1609}
1610
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01001611static struct list_head *vgic_get_irq_phys_map_list(struct kvm_vcpu *vcpu,
1612 int virt_irq)
1613{
1614 if (virt_irq < VGIC_NR_PRIVATE_IRQS)
1615 return &vcpu->arch.vgic_cpu.irq_phys_map_list;
1616 else
1617 return &vcpu->kvm->arch.vgic.irq_phys_map_list;
1618}
1619
1620/**
1621 * kvm_vgic_map_phys_irq - map a virtual IRQ to a physical IRQ
1622 * @vcpu: The VCPU pointer
1623 * @virt_irq: The virtual irq number
1624 * @irq: The Linux IRQ number
1625 *
1626 * Establish a mapping between a guest visible irq (@virt_irq) and a
1627 * Linux irq (@irq). On injection, @virt_irq will be associated with
1628 * the physical interrupt represented by @irq. This mapping can be
1629 * established multiple times as long as the parameters are the same.
1630 *
1631 * Returns a valid pointer on success, and an error pointer otherwise
1632 */
1633struct irq_phys_map *kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu,
1634 int virt_irq, int irq)
1635{
1636 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1637 struct list_head *root = vgic_get_irq_phys_map_list(vcpu, virt_irq);
1638 struct irq_phys_map *map;
1639 struct irq_phys_map_entry *entry;
1640 struct irq_desc *desc;
1641 struct irq_data *data;
1642 int phys_irq;
1643
1644 desc = irq_to_desc(irq);
1645 if (!desc) {
1646 kvm_err("%s: no interrupt descriptor\n", __func__);
1647 return ERR_PTR(-EINVAL);
1648 }
1649
1650 data = irq_desc_get_irq_data(desc);
1651 while (data->parent_data)
1652 data = data->parent_data;
1653
1654 phys_irq = data->hwirq;
1655
1656 /* Create a new mapping */
1657 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1658 if (!entry)
1659 return ERR_PTR(-ENOMEM);
1660
1661 spin_lock(&dist->irq_phys_map_lock);
1662
1663 /* Try to match an existing mapping */
1664 map = vgic_irq_map_search(vcpu, virt_irq);
1665 if (map) {
1666 /* Make sure this mapping matches */
1667 if (map->phys_irq != phys_irq ||
1668 map->irq != irq)
1669 map = ERR_PTR(-EINVAL);
1670
1671 /* Found an existing, valid mapping */
1672 goto out;
1673 }
1674
1675 map = &entry->map;
1676 map->virt_irq = virt_irq;
1677 map->phys_irq = phys_irq;
1678 map->irq = irq;
1679
1680 list_add_tail_rcu(&entry->entry, root);
1681
1682out:
1683 spin_unlock(&dist->irq_phys_map_lock);
1684 /* If we've found a hit in the existing list, free the useless
1685 * entry */
1686 if (IS_ERR(map) || map != &entry->map)
1687 kfree(entry);
1688 return map;
1689}
1690
1691static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu,
1692 int virt_irq)
1693{
1694 struct list_head *root = vgic_get_irq_phys_map_list(vcpu, virt_irq);
1695 struct irq_phys_map_entry *entry;
1696 struct irq_phys_map *map;
1697
1698 rcu_read_lock();
1699
1700 list_for_each_entry_rcu(entry, root, entry) {
1701 map = &entry->map;
1702 if (map->virt_irq == virt_irq) {
1703 rcu_read_unlock();
1704 return map;
1705 }
1706 }
1707
1708 rcu_read_unlock();
1709
1710 return NULL;
1711}
1712
1713static void vgic_free_phys_irq_map_rcu(struct rcu_head *rcu)
1714{
1715 struct irq_phys_map_entry *entry;
1716
1717 entry = container_of(rcu, struct irq_phys_map_entry, rcu);
1718 kfree(entry);
1719}
1720
1721/**
1722 * kvm_vgic_unmap_phys_irq - Remove a virtual to physical IRQ mapping
1723 * @vcpu: The VCPU pointer
1724 * @map: The pointer to a mapping obtained through kvm_vgic_map_phys_irq
1725 *
1726 * Remove an existing mapping between virtual and physical interrupts.
1727 */
1728int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, struct irq_phys_map *map)
1729{
1730 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1731 struct irq_phys_map_entry *entry;
1732 struct list_head *root;
1733
1734 if (!map)
1735 return -EINVAL;
1736
1737 root = vgic_get_irq_phys_map_list(vcpu, map->virt_irq);
1738
1739 spin_lock(&dist->irq_phys_map_lock);
1740
1741 list_for_each_entry(entry, root, entry) {
1742 if (&entry->map == map) {
1743 list_del_rcu(&entry->entry);
1744 call_rcu(&entry->rcu, vgic_free_phys_irq_map_rcu);
1745 break;
1746 }
1747 }
1748
1749 spin_unlock(&dist->irq_phys_map_lock);
1750
1751 return 0;
1752}
1753
1754static void vgic_destroy_irq_phys_map(struct kvm *kvm, struct list_head *root)
1755{
1756 struct vgic_dist *dist = &kvm->arch.vgic;
1757 struct irq_phys_map_entry *entry;
1758
1759 spin_lock(&dist->irq_phys_map_lock);
1760
1761 list_for_each_entry(entry, root, entry) {
1762 list_del_rcu(&entry->entry);
1763 call_rcu(&entry->rcu, vgic_free_phys_irq_map_rcu);
1764 }
1765
1766 spin_unlock(&dist->irq_phys_map_lock);
1767}
1768
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001769void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
1770{
1771 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1772
1773 kfree(vgic_cpu->pending_shared);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001774 kfree(vgic_cpu->active_shared);
1775 kfree(vgic_cpu->pend_act_shared);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001776 kfree(vgic_cpu->vgic_irq_lr_map);
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01001777 vgic_destroy_irq_phys_map(vcpu->kvm, &vgic_cpu->irq_phys_map_list);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001778 vgic_cpu->pending_shared = NULL;
Christoffer Dall47a98b12015-03-13 17:02:54 +00001779 vgic_cpu->active_shared = NULL;
1780 vgic_cpu->pend_act_shared = NULL;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001781 vgic_cpu->vgic_irq_lr_map = NULL;
1782}
1783
1784static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
1785{
1786 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1787
1788 int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8;
1789 vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001790 vgic_cpu->active_shared = kzalloc(sz, GFP_KERNEL);
1791 vgic_cpu->pend_act_shared = kzalloc(sz, GFP_KERNEL);
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001792 vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001793
Christoffer Dall47a98b12015-03-13 17:02:54 +00001794 if (!vgic_cpu->pending_shared
1795 || !vgic_cpu->active_shared
1796 || !vgic_cpu->pend_act_shared
1797 || !vgic_cpu->vgic_irq_lr_map) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001798 kvm_vgic_vcpu_destroy(vcpu);
1799 return -ENOMEM;
1800 }
1801
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001802 memset(vgic_cpu->vgic_irq_lr_map, LR_EMPTY, nr_irqs);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001803
1804 /*
Marc Zyngierca85f622013-06-18 19:17:28 +01001805 * Store the number of LRs per vcpu, so we don't have to go
1806 * all the way to the distributor structure to find out. Only
1807 * assembly code should use this one.
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001808 */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001809 vgic_cpu->nr_lr = vgic->nr_lr;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001810
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001811 return 0;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001812}
1813
Andre Przywara3caa2d82014-06-02 16:26:01 +02001814/**
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01001815 * kvm_vgic_vcpu_early_init - Earliest possible per-vcpu vgic init stage
1816 *
1817 * No memory allocation should be performed here, only static init.
1818 */
1819void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu)
1820{
1821 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1822 INIT_LIST_HEAD(&vgic_cpu->irq_phys_map_list);
1823}
1824
1825/**
Andre Przywara3caa2d82014-06-02 16:26:01 +02001826 * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW
1827 *
1828 * The host's GIC naturally limits the maximum amount of VCPUs a guest
1829 * can use.
1830 */
1831int kvm_vgic_get_max_vcpus(void)
1832{
1833 return vgic->max_gic_vcpus;
1834}
1835
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001836void kvm_vgic_destroy(struct kvm *kvm)
1837{
1838 struct vgic_dist *dist = &kvm->arch.vgic;
1839 struct kvm_vcpu *vcpu;
1840 int i;
1841
1842 kvm_for_each_vcpu(i, vcpu, kvm)
1843 kvm_vgic_vcpu_destroy(vcpu);
1844
1845 vgic_free_bitmap(&dist->irq_enabled);
1846 vgic_free_bitmap(&dist->irq_level);
1847 vgic_free_bitmap(&dist->irq_pending);
1848 vgic_free_bitmap(&dist->irq_soft_pend);
1849 vgic_free_bitmap(&dist->irq_queued);
1850 vgic_free_bitmap(&dist->irq_cfg);
1851 vgic_free_bytemap(&dist->irq_priority);
1852 if (dist->irq_spi_target) {
1853 for (i = 0; i < dist->nr_cpus; i++)
1854 vgic_free_bitmap(&dist->irq_spi_target[i]);
1855 }
1856 kfree(dist->irq_sgi_sources);
1857 kfree(dist->irq_spi_cpu);
Andre Przywaraa0675c22014-06-07 00:54:51 +02001858 kfree(dist->irq_spi_mpidr);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001859 kfree(dist->irq_spi_target);
1860 kfree(dist->irq_pending_on_cpu);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001861 kfree(dist->irq_active_on_cpu);
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01001862 vgic_destroy_irq_phys_map(kvm, &dist->irq_phys_map_list);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001863 dist->irq_sgi_sources = NULL;
1864 dist->irq_spi_cpu = NULL;
1865 dist->irq_spi_target = NULL;
1866 dist->irq_pending_on_cpu = NULL;
Christoffer Dall47a98b12015-03-13 17:02:54 +00001867 dist->irq_active_on_cpu = NULL;
Christoffer Dall1f57be22014-12-09 14:30:36 +01001868 dist->nr_cpus = 0;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001869}
1870
1871/*
1872 * Allocate and initialize the various data structures. Must be called
1873 * with kvm->lock held!
1874 */
Andre Przywara83215812014-06-07 00:53:08 +02001875int vgic_init(struct kvm *kvm)
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001876{
1877 struct vgic_dist *dist = &kvm->arch.vgic;
1878 struct kvm_vcpu *vcpu;
1879 int nr_cpus, nr_irqs;
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001880 int ret, i, vcpu_id;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001881
Christoffer Dall1f57be22014-12-09 14:30:36 +01001882 if (vgic_initialized(kvm))
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001883 return 0;
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001884
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001885 nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus);
1886 if (!nr_cpus) /* No vcpus? Can't be good... */
Eric Auger66b030e2014-12-15 18:43:32 +01001887 return -ENODEV;
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001888
1889 /*
1890 * If nobody configured the number of interrupts, use the
1891 * legacy one.
1892 */
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001893 if (!dist->nr_irqs)
1894 dist->nr_irqs = VGIC_NR_IRQS_LEGACY;
1895
1896 nr_irqs = dist->nr_irqs;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001897
1898 ret = vgic_init_bitmap(&dist->irq_enabled, nr_cpus, nr_irqs);
1899 ret |= vgic_init_bitmap(&dist->irq_level, nr_cpus, nr_irqs);
1900 ret |= vgic_init_bitmap(&dist->irq_pending, nr_cpus, nr_irqs);
1901 ret |= vgic_init_bitmap(&dist->irq_soft_pend, nr_cpus, nr_irqs);
1902 ret |= vgic_init_bitmap(&dist->irq_queued, nr_cpus, nr_irqs);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001903 ret |= vgic_init_bitmap(&dist->irq_active, nr_cpus, nr_irqs);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001904 ret |= vgic_init_bitmap(&dist->irq_cfg, nr_cpus, nr_irqs);
1905 ret |= vgic_init_bytemap(&dist->irq_priority, nr_cpus, nr_irqs);
1906
1907 if (ret)
1908 goto out;
1909
1910 dist->irq_sgi_sources = kzalloc(nr_cpus * VGIC_NR_SGIS, GFP_KERNEL);
1911 dist->irq_spi_cpu = kzalloc(nr_irqs - VGIC_NR_PRIVATE_IRQS, GFP_KERNEL);
1912 dist->irq_spi_target = kzalloc(sizeof(*dist->irq_spi_target) * nr_cpus,
1913 GFP_KERNEL);
1914 dist->irq_pending_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
1915 GFP_KERNEL);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001916 dist->irq_active_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
1917 GFP_KERNEL);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001918 if (!dist->irq_sgi_sources ||
1919 !dist->irq_spi_cpu ||
1920 !dist->irq_spi_target ||
Christoffer Dall47a98b12015-03-13 17:02:54 +00001921 !dist->irq_pending_on_cpu ||
1922 !dist->irq_active_on_cpu) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001923 ret = -ENOMEM;
1924 goto out;
1925 }
1926
1927 for (i = 0; i < nr_cpus; i++)
1928 ret |= vgic_init_bitmap(&dist->irq_spi_target[i],
1929 nr_cpus, nr_irqs);
1930
1931 if (ret)
1932 goto out;
1933
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001934 ret = kvm->arch.vgic.vm_ops.init_model(kvm);
1935 if (ret)
1936 goto out;
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001937
1938 kvm_for_each_vcpu(vcpu_id, vcpu, kvm) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001939 ret = vgic_vcpu_init_maps(vcpu, nr_irqs);
1940 if (ret) {
1941 kvm_err("VGIC: Failed to allocate vcpu memory\n");
1942 break;
1943 }
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001944
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001945 for (i = 0; i < dist->nr_irqs; i++) {
1946 if (i < VGIC_NR_PPIS)
1947 vgic_bitmap_set_irq_val(&dist->irq_enabled,
1948 vcpu->vcpu_id, i, 1);
1949 if (i < VGIC_NR_PRIVATE_IRQS)
1950 vgic_bitmap_set_irq_val(&dist->irq_cfg,
1951 vcpu->vcpu_id, i,
1952 VGIC_CFG_EDGE);
1953 }
1954
1955 vgic_enable(vcpu);
1956 }
Marc Zyngier4956f2b2014-07-08 12:09:06 +01001957
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001958out:
1959 if (ret)
1960 kvm_vgic_destroy(kvm);
1961
1962 return ret;
1963}
1964
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001965static int init_vgic_model(struct kvm *kvm, int type)
1966{
1967 switch (type) {
1968 case KVM_DEV_TYPE_ARM_VGIC_V2:
1969 vgic_v2_init_emulation(kvm);
1970 break;
Andre Przywarab5d84ff2014-06-03 10:26:03 +02001971#ifdef CONFIG_ARM_GIC_V3
1972 case KVM_DEV_TYPE_ARM_VGIC_V3:
1973 vgic_v3_init_emulation(kvm);
1974 break;
1975#endif
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001976 default:
1977 return -ENODEV;
1978 }
1979
Andre Przywara3caa2d82014-06-02 16:26:01 +02001980 if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus)
1981 return -E2BIG;
1982
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001983 return 0;
1984}
1985
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01001986/**
1987 * kvm_vgic_early_init - Earliest possible vgic initialization stage
1988 *
1989 * No memory allocation should be performed here, only static init.
1990 */
1991void kvm_vgic_early_init(struct kvm *kvm)
1992{
1993 spin_lock_init(&kvm->arch.vgic.lock);
1994 spin_lock_init(&kvm->arch.vgic.irq_phys_map_lock);
1995 INIT_LIST_HEAD(&kvm->arch.vgic.irq_phys_map_list);
1996}
1997
Andre Przywara598921362014-06-03 09:33:10 +02001998int kvm_vgic_create(struct kvm *kvm, u32 type)
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001999{
Christoffer Dall6b50f542014-11-06 11:47:39 +00002000 int i, vcpu_lock_idx = -1, ret;
Christoffer Dall73306722013-10-25 17:29:18 +01002001 struct kvm_vcpu *vcpu;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002002
2003 mutex_lock(&kvm->lock);
2004
Andre Przywara4ce7ebd2014-10-26 23:18:14 +00002005 if (irqchip_in_kernel(kvm)) {
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002006 ret = -EEXIST;
2007 goto out;
2008 }
2009
Christoffer Dall73306722013-10-25 17:29:18 +01002010 /*
Andre Przywarab5d84ff2014-06-03 10:26:03 +02002011 * This function is also called by the KVM_CREATE_IRQCHIP handler,
2012 * which had no chance yet to check the availability of the GICv2
2013 * emulation. So check this here again. KVM_CREATE_DEVICE does
2014 * the proper checks already.
2015 */
Wei Yongjunb52104e2015-02-27 19:41:45 +08002016 if (type == KVM_DEV_TYPE_ARM_VGIC_V2 && !vgic->can_emulate_gicv2) {
2017 ret = -ENODEV;
2018 goto out;
2019 }
Andre Przywarab5d84ff2014-06-03 10:26:03 +02002020
2021 /*
Christoffer Dall73306722013-10-25 17:29:18 +01002022 * Any time a vcpu is run, vcpu_load is called which tries to grab the
2023 * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
2024 * that no other VCPUs are run while we create the vgic.
2025 */
Christoffer Dall6b50f542014-11-06 11:47:39 +00002026 ret = -EBUSY;
Christoffer Dall73306722013-10-25 17:29:18 +01002027 kvm_for_each_vcpu(i, vcpu, kvm) {
2028 if (!mutex_trylock(&vcpu->mutex))
2029 goto out_unlock;
2030 vcpu_lock_idx = i;
2031 }
2032
2033 kvm_for_each_vcpu(i, vcpu, kvm) {
Christoffer Dall6b50f542014-11-06 11:47:39 +00002034 if (vcpu->arch.has_run_once)
Christoffer Dall73306722013-10-25 17:29:18 +01002035 goto out_unlock;
Christoffer Dall73306722013-10-25 17:29:18 +01002036 }
Christoffer Dall6b50f542014-11-06 11:47:39 +00002037 ret = 0;
Christoffer Dall73306722013-10-25 17:29:18 +01002038
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002039 ret = init_vgic_model(kvm, type);
2040 if (ret)
2041 goto out_unlock;
2042
Marc Zyngierf982cf42014-05-15 10:03:25 +01002043 kvm->arch.vgic.in_kernel = true;
Andre Przywara598921362014-06-03 09:33:10 +02002044 kvm->arch.vgic.vgic_model = type;
Marc Zyngier8f186d52014-02-04 18:13:03 +00002045 kvm->arch.vgic.vctrl_base = vgic->vctrl_base;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002046 kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
2047 kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
Andre Przywaraa0675c22014-06-07 00:54:51 +02002048 kvm->arch.vgic.vgic_redist_base = VGIC_ADDR_UNDEF;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002049
Christoffer Dall73306722013-10-25 17:29:18 +01002050out_unlock:
2051 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
2052 vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
2053 mutex_unlock(&vcpu->mutex);
2054 }
2055
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002056out:
2057 mutex_unlock(&kvm->lock);
2058 return ret;
2059}
2060
Will Deacon1fa451b2014-08-26 15:13:24 +01002061static int vgic_ioaddr_overlap(struct kvm *kvm)
Christoffer Dall330690c2013-01-21 19:36:13 -05002062{
2063 phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
2064 phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
2065
2066 if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
2067 return 0;
2068 if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
2069 (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
2070 return -EBUSY;
2071 return 0;
2072}
2073
2074static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
2075 phys_addr_t addr, phys_addr_t size)
2076{
2077 int ret;
2078
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002079 if (addr & ~KVM_PHYS_MASK)
2080 return -E2BIG;
2081
2082 if (addr & (SZ_4K - 1))
2083 return -EINVAL;
2084
Christoffer Dall330690c2013-01-21 19:36:13 -05002085 if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
2086 return -EEXIST;
2087 if (addr + size < addr)
2088 return -EINVAL;
2089
Haibin Wang30c21172014-04-29 14:49:17 +08002090 *ioaddr = addr;
Christoffer Dall330690c2013-01-21 19:36:13 -05002091 ret = vgic_ioaddr_overlap(kvm);
2092 if (ret)
Haibin Wang30c21172014-04-29 14:49:17 +08002093 *ioaddr = VGIC_ADDR_UNDEF;
2094
Christoffer Dall330690c2013-01-21 19:36:13 -05002095 return ret;
2096}
2097
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002098/**
2099 * kvm_vgic_addr - set or get vgic VM base addresses
2100 * @kvm: pointer to the vm struct
Andre Przywaraac3d3732014-06-03 10:26:30 +02002101 * @type: the VGIC addr type, one of KVM_VGIC_V[23]_ADDR_TYPE_XXX
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002102 * @addr: pointer to address value
2103 * @write: if true set the address in the VM address space, if false read the
2104 * address
2105 *
2106 * Set or get the vgic base addresses for the distributor and the virtual CPU
2107 * interface in the VM physical address space. These addresses are properties
2108 * of the emulated core/SoC and therefore user space initially knows this
2109 * information.
2110 */
2111int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
Christoffer Dall330690c2013-01-21 19:36:13 -05002112{
2113 int r = 0;
2114 struct vgic_dist *vgic = &kvm->arch.vgic;
Andre Przywaraac3d3732014-06-03 10:26:30 +02002115 int type_needed;
2116 phys_addr_t *addr_ptr, block_size;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002117 phys_addr_t alignment;
Christoffer Dall330690c2013-01-21 19:36:13 -05002118
Christoffer Dall330690c2013-01-21 19:36:13 -05002119 mutex_lock(&kvm->lock);
2120 switch (type) {
2121 case KVM_VGIC_V2_ADDR_TYPE_DIST:
Andre Przywaraac3d3732014-06-03 10:26:30 +02002122 type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
2123 addr_ptr = &vgic->vgic_dist_base;
2124 block_size = KVM_VGIC_V2_DIST_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002125 alignment = SZ_4K;
Christoffer Dall330690c2013-01-21 19:36:13 -05002126 break;
2127 case KVM_VGIC_V2_ADDR_TYPE_CPU:
Andre Przywaraac3d3732014-06-03 10:26:30 +02002128 type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
2129 addr_ptr = &vgic->vgic_cpu_base;
2130 block_size = KVM_VGIC_V2_CPU_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002131 alignment = SZ_4K;
Christoffer Dall330690c2013-01-21 19:36:13 -05002132 break;
Andre Przywaraac3d3732014-06-03 10:26:30 +02002133#ifdef CONFIG_ARM_GIC_V3
2134 case KVM_VGIC_V3_ADDR_TYPE_DIST:
2135 type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
2136 addr_ptr = &vgic->vgic_dist_base;
2137 block_size = KVM_VGIC_V3_DIST_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002138 alignment = SZ_64K;
Andre Przywaraac3d3732014-06-03 10:26:30 +02002139 break;
2140 case KVM_VGIC_V3_ADDR_TYPE_REDIST:
2141 type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
2142 addr_ptr = &vgic->vgic_redist_base;
2143 block_size = KVM_VGIC_V3_REDIST_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002144 alignment = SZ_64K;
Andre Przywaraac3d3732014-06-03 10:26:30 +02002145 break;
2146#endif
Christoffer Dall330690c2013-01-21 19:36:13 -05002147 default:
2148 r = -ENODEV;
Andre Przywaraac3d3732014-06-03 10:26:30 +02002149 goto out;
Christoffer Dall330690c2013-01-21 19:36:13 -05002150 }
2151
Andre Przywaraac3d3732014-06-03 10:26:30 +02002152 if (vgic->vgic_model != type_needed) {
2153 r = -ENODEV;
2154 goto out;
2155 }
2156
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002157 if (write) {
2158 if (!IS_ALIGNED(*addr, alignment))
2159 r = -EINVAL;
2160 else
2161 r = vgic_ioaddr_assign(kvm, addr_ptr, *addr,
2162 block_size);
2163 } else {
Andre Przywaraac3d3732014-06-03 10:26:30 +02002164 *addr = *addr_ptr;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002165 }
Andre Przywaraac3d3732014-06-03 10:26:30 +02002166
2167out:
Christoffer Dall330690c2013-01-21 19:36:13 -05002168 mutex_unlock(&kvm->lock);
2169 return r;
2170}
Christoffer Dall73306722013-10-25 17:29:18 +01002171
Andre Przywara83215812014-06-07 00:53:08 +02002172int vgic_set_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
Christoffer Dall73306722013-10-25 17:29:18 +01002173{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002174 int r;
2175
2176 switch (attr->group) {
2177 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2178 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2179 u64 addr;
2180 unsigned long type = (unsigned long)attr->attr;
2181
2182 if (copy_from_user(&addr, uaddr, sizeof(addr)))
2183 return -EFAULT;
2184
2185 r = kvm_vgic_addr(dev->kvm, type, &addr, true);
2186 return (r == -ENODEV) ? -ENXIO : r;
2187 }
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002188 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2189 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2190 u32 val;
2191 int ret = 0;
2192
2193 if (get_user(val, uaddr))
2194 return -EFAULT;
2195
2196 /*
2197 * We require:
2198 * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs
2199 * - at most 1024 interrupts
2200 * - a multiple of 32 interrupts
2201 */
2202 if (val < (VGIC_NR_PRIVATE_IRQS + 32) ||
2203 val > VGIC_MAX_IRQS ||
2204 (val & 31))
2205 return -EINVAL;
2206
2207 mutex_lock(&dev->kvm->lock);
2208
Christoffer Dallc52edf52014-12-09 14:28:09 +01002209 if (vgic_ready(dev->kvm) || dev->kvm->arch.vgic.nr_irqs)
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002210 ret = -EBUSY;
2211 else
2212 dev->kvm->arch.vgic.nr_irqs = val;
2213
2214 mutex_unlock(&dev->kvm->lock);
2215
2216 return ret;
2217 }
Eric Auger065c0032014-12-15 18:43:33 +01002218 case KVM_DEV_ARM_VGIC_GRP_CTRL: {
2219 switch (attr->attr) {
2220 case KVM_DEV_ARM_VGIC_CTRL_INIT:
2221 r = vgic_init(dev->kvm);
2222 return r;
2223 }
2224 break;
2225 }
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002226 }
2227
Christoffer Dall73306722013-10-25 17:29:18 +01002228 return -ENXIO;
2229}
2230
Andre Przywara83215812014-06-07 00:53:08 +02002231int vgic_get_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
Christoffer Dall73306722013-10-25 17:29:18 +01002232{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002233 int r = -ENXIO;
2234
2235 switch (attr->group) {
2236 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2237 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2238 u64 addr;
2239 unsigned long type = (unsigned long)attr->attr;
2240
2241 r = kvm_vgic_addr(dev->kvm, type, &addr, false);
2242 if (r)
2243 return (r == -ENODEV) ? -ENXIO : r;
2244
2245 if (copy_to_user(uaddr, &addr, sizeof(addr)))
2246 return -EFAULT;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002247 break;
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002248 }
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002249 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2250 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
Andre Przywarab60da142014-08-21 11:08:27 +01002251
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002252 r = put_user(dev->kvm->arch.vgic.nr_irqs, uaddr);
2253 break;
2254 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01002255
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002256 }
2257
2258 return r;
Christoffer Dall73306722013-10-25 17:29:18 +01002259}
2260
Andre Przywaracf50a1e2015-03-26 14:39:32 +00002261int vgic_has_attr_regs(const struct vgic_io_range *ranges, phys_addr_t offset)
Christoffer Dallc07a0192013-10-25 21:17:31 +01002262{
Andre Przywara9f199d02015-03-26 14:39:33 +00002263 if (vgic_find_range(ranges, 4, offset))
Christoffer Dallc07a0192013-10-25 21:17:31 +01002264 return 0;
2265 else
2266 return -ENXIO;
2267}
2268
Will Deaconc06a8412014-09-02 10:27:34 +01002269static void vgic_init_maintenance_interrupt(void *info)
2270{
2271 enable_percpu_irq(vgic->maint_irq, 0);
2272}
2273
2274static int vgic_cpu_notify(struct notifier_block *self,
2275 unsigned long action, void *cpu)
2276{
2277 switch (action) {
2278 case CPU_STARTING:
2279 case CPU_STARTING_FROZEN:
2280 vgic_init_maintenance_interrupt(NULL);
2281 break;
2282 case CPU_DYING:
2283 case CPU_DYING_FROZEN:
2284 disable_percpu_irq(vgic->maint_irq);
2285 break;
2286 }
2287
2288 return NOTIFY_OK;
2289}
2290
2291static struct notifier_block vgic_cpu_nb = {
2292 .notifier_call = vgic_cpu_notify,
2293};
2294
2295static const struct of_device_id vgic_ids[] = {
Mark Rutland0f3724752015-03-05 14:47:44 +00002296 { .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, },
2297 { .compatible = "arm,cortex-a7-gic", .data = vgic_v2_probe, },
2298 { .compatible = "arm,gic-400", .data = vgic_v2_probe, },
2299 { .compatible = "arm,gic-v3", .data = vgic_v3_probe, },
Will Deaconc06a8412014-09-02 10:27:34 +01002300 {},
2301};
2302
2303int kvm_vgic_hyp_init(void)
2304{
2305 const struct of_device_id *matched_id;
Christoffer Dalla875daf2014-09-18 18:15:32 -07002306 const int (*vgic_probe)(struct device_node *,const struct vgic_ops **,
2307 const struct vgic_params **);
Will Deaconc06a8412014-09-02 10:27:34 +01002308 struct device_node *vgic_node;
2309 int ret;
2310
2311 vgic_node = of_find_matching_node_and_match(NULL,
2312 vgic_ids, &matched_id);
2313 if (!vgic_node) {
2314 kvm_err("error: no compatible GIC node found\n");
2315 return -ENODEV;
2316 }
2317
2318 vgic_probe = matched_id->data;
2319 ret = vgic_probe(vgic_node, &vgic_ops, &vgic);
2320 if (ret)
2321 return ret;
2322
2323 ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler,
2324 "vgic", kvm_get_running_vcpus());
2325 if (ret) {
2326 kvm_err("Cannot register interrupt %d\n", vgic->maint_irq);
2327 return ret;
2328 }
2329
2330 ret = __register_cpu_notifier(&vgic_cpu_nb);
2331 if (ret) {
2332 kvm_err("Cannot register vgic CPU notifier\n");
2333 goto out_free_irq;
2334 }
2335
Will Deaconc06a8412014-09-02 10:27:34 +01002336 on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
2337
Andre Przywaraea2f83a2014-10-26 23:17:00 +00002338 return 0;
Will Deaconc06a8412014-09-02 10:27:34 +01002339
2340out_free_irq:
2341 free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus());
2342 return ret;
2343}
Eric Auger174178f2015-03-04 11:14:36 +01002344
2345int kvm_irq_map_gsi(struct kvm *kvm,
2346 struct kvm_kernel_irq_routing_entry *entries,
2347 int gsi)
2348{
Eric Auger0b3289e2015-04-13 15:01:59 +02002349 return 0;
Eric Auger174178f2015-03-04 11:14:36 +01002350}
2351
2352int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin)
2353{
2354 return pin;
2355}
2356
2357int kvm_set_irq(struct kvm *kvm, int irq_source_id,
2358 u32 irq, int level, bool line_status)
2359{
2360 unsigned int spi = irq + VGIC_NR_PRIVATE_IRQS;
2361
2362 trace_kvm_set_irq(irq, level, irq_source_id);
2363
2364 BUG_ON(!vgic_initialized(kvm));
2365
Eric Auger174178f2015-03-04 11:14:36 +01002366 return kvm_vgic_inject_irq(kvm, 0, spi, level);
Eric Auger174178f2015-03-04 11:14:36 +01002367}
2368
2369/* MSI not implemented yet */
2370int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
2371 struct kvm *kvm, int irq_source_id,
2372 int level, bool line_status)
2373{
2374 return 0;
2375}