]> git.karo-electronics.de Git - linux-beck.git/blob - virt/kvm/arm/vgic.c
arm/arm64: KVM: Fix arch timer behavior for disabled interrupts
[linux-beck.git] / virt / kvm / arm / vgic.c
1 /*
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
19 #include <linux/cpu.h>
20 #include <linux/kvm.h>
21 #include <linux/kvm_host.h>
22 #include <linux/interrupt.h>
23 #include <linux/io.h>
24 #include <linux/of.h>
25 #include <linux/of_address.h>
26 #include <linux/of_irq.h>
27 #include <linux/rculist.h>
28 #include <linux/uaccess.h>
29
30 #include <asm/kvm_emulate.h>
31 #include <asm/kvm_arm.h>
32 #include <asm/kvm_mmu.h>
33 #include <trace/events/kvm.h>
34 #include <asm/kvm.h>
35 #include <kvm/iodev.h>
36
37 /*
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
41  *   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).
46  * - 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:
50  *   - PPI: dist->irq_pending & dist->irq_enable
51  *   - SPI: dist->irq_pending & dist->irq_enable & dist->irq_spi_target
52  *   - irq_spi_target is a 'formatted' version of the GICD_ITARGETSRn
53  *     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.
56  * - If any of the above state changes, we must recalculate the oracle.
57  * - 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
66  *   bit in irq_queued is set. As long as this bit is set, the line
67  *   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,
71  *   and clears the corresponding bit in irq_queued. This allows the
72  *   interrupt line to be sampled again.
73  * - 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.
78  *
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.
100  */
101
102 #include "vgic.h"
103
104 static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu);
105 static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu);
106 static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr);
107 static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, struct vgic_lr lr_desc);
108 static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu,
109                                                 int virt_irq);
110
111 static const struct vgic_ops *vgic_ops;
112 static const struct vgic_params *vgic;
113
114 static 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
119 static bool queue_sgi(struct kvm_vcpu *vcpu, int irq)
120 {
121         return vcpu->kvm->arch.vgic.vm_ops.queue_sgi(vcpu, irq);
122 }
123
124 int kvm_vgic_map_resources(struct kvm *kvm)
125 {
126         return kvm->arch.vgic.vm_ops.map_resources(kvm, vgic);
127 }
128
129 /*
130  * struct vgic_bitmap contains a bitmap made of unsigned longs, but
131  * extracts u32s out of them.
132  *
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
146
147 static 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
162 static void vgic_free_bitmap(struct vgic_bitmap *b)
163 {
164         kfree(b->private);
165         b->private = NULL;
166         b->shared = NULL;
167 }
168
169 /*
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  */
175 static 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
183 u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x, int cpuid, u32 offset)
184 {
185         offset >>= 2;
186         if (!offset)
187                 return (u32 *)(x->private + cpuid) + REG_OFFSET_SWIZZLE;
188         else
189                 return (u32 *)(x->shared) + ((offset - 1) ^ REG_OFFSET_SWIZZLE);
190 }
191
192 static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
193                                    int cpuid, int irq)
194 {
195         if (irq < VGIC_NR_PRIVATE_IRQS)
196                 return test_bit(irq, x->private + cpuid);
197
198         return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared);
199 }
200
201 void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
202                              int irq, int val)
203 {
204         unsigned long *reg;
205
206         if (irq < VGIC_NR_PRIVATE_IRQS) {
207                 reg = x->private + cpuid;
208         } else {
209                 reg = x->shared;
210                 irq -= VGIC_NR_PRIVATE_IRQS;
211         }
212
213         if (val)
214                 set_bit(irq, reg);
215         else
216                 clear_bit(irq, reg);
217 }
218
219 static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
220 {
221         return x->private + cpuid;
222 }
223
224 unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
225 {
226         return x->shared;
227 }
228
229 static 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
244 static void vgic_free_bytemap(struct vgic_bytemap *b)
245 {
246         kfree(b->private);
247         b->private = NULL;
248         b->shared = NULL;
249 }
250
251 u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
252 {
253         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));
264 }
265
266 #define VGIC_CFG_LEVEL  0
267 #define VGIC_CFG_EDGE   1
268
269 static 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
278 static 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
285 static int vgic_irq_is_queued(struct kvm_vcpu *vcpu, int irq)
286 {
287         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
288
289         return vgic_bitmap_get_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq);
290 }
291
292 static 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
299 static void vgic_irq_set_queued(struct kvm_vcpu *vcpu, int irq)
300 {
301         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
302
303         vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 1);
304 }
305
306 static void vgic_irq_clear_queued(struct kvm_vcpu *vcpu, int irq)
307 {
308         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
309
310         vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 0);
311 }
312
313 static 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
320 static 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
327 static 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
334 static 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
341 static 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
348 static 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
355 static 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
362 static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
363 {
364         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
365
366         return vgic_bitmap_get_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq);
367 }
368
369 void vgic_dist_irq_set_pending(struct kvm_vcpu *vcpu, int irq)
370 {
371         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
372
373         vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 1);
374 }
375
376 void vgic_dist_irq_clear_pending(struct kvm_vcpu *vcpu, int irq)
377 {
378         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
379
380         vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 0);
381 }
382
383 static 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
392 void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
393 {
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
401 static bool vgic_can_sample_irq(struct kvm_vcpu *vcpu, int irq)
402 {
403         return !vgic_irq_is_queued(vcpu, irq);
404 }
405
406 /**
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  */
417 void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
418                      phys_addr_t offset, int mode)
419 {
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
467 bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
468                         phys_addr_t offset)
469 {
470         vgic_reg_access(mmio, NULL, offset,
471                         ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
472         return false;
473 }
474
475 bool vgic_handle_enable_reg(struct kvm *kvm, struct kvm_exit_mmio *mmio,
476                             phys_addr_t offset, int vcpu_id, int access)
477 {
478         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);
484         if (mmio->is_write) {
485                 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);
491                 return true;
492         }
493
494         return false;
495 }
496
497 bool vgic_handle_set_pending_reg(struct kvm *kvm,
498                                  struct kvm_exit_mmio *mmio,
499                                  phys_addr_t offset, int vcpu_id)
500 {
501         u32 *reg, orig;
502         u32 level_mask;
503         int mode = ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT;
504         struct vgic_dist *dist = &kvm->arch.vgic;
505
506         reg = vgic_bitmap_get_reg(&dist->irq_cfg, vcpu_id, offset);
507         level_mask = (~(*reg));
508
509         /* Mark both level and edge triggered irqs as pending */
510         reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
511         orig = *reg;
512         vgic_reg_access(mmio, reg, offset, mode);
513
514         if (mmio->is_write) {
515                 /* Set the soft-pending flag only for level-triggered irqs */
516                 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
517                                           vcpu_id, offset);
518                 vgic_reg_access(mmio, reg, offset, mode);
519                 *reg &= level_mask;
520
521                 /* Ignore writes to SGIs */
522                 if (offset < 2) {
523                         *reg &= ~0xffff;
524                         *reg |= orig & 0xffff;
525                 }
526
527                 vgic_update_state(kvm);
528                 return true;
529         }
530
531         return false;
532 }
533
534 bool vgic_handle_clear_pending_reg(struct kvm *kvm,
535                                    struct kvm_exit_mmio *mmio,
536                                    phys_addr_t offset, int vcpu_id)
537 {
538         u32 *level_active;
539         u32 *reg, orig;
540         int mode = ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT;
541         struct vgic_dist *dist = &kvm->arch.vgic;
542
543         reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
544         orig = *reg;
545         vgic_reg_access(mmio, reg, offset, mode);
546         if (mmio->is_write) {
547                 /* Re-set level triggered level-active interrupts */
548                 level_active = vgic_bitmap_get_reg(&dist->irq_level,
549                                           vcpu_id, offset);
550                 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
551                 *reg |= *level_active;
552
553                 /* Ignore writes to SGIs */
554                 if (offset < 2) {
555                         *reg &= ~0xffff;
556                         *reg |= orig & 0xffff;
557                 }
558
559                 /* Clear soft-pending flags */
560                 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
561                                           vcpu_id, offset);
562                 vgic_reg_access(mmio, reg, offset, mode);
563
564                 vgic_update_state(kvm);
565                 return true;
566         }
567         return false;
568 }
569
570 bool 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
589 bool 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
608 static 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
623 static 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  */
643 bool vgic_handle_cfg_reg(u32 *reg, struct kvm_exit_mmio *mmio,
644                          phys_addr_t offset)
645 {
646         u32 val;
647
648         if (offset & 4)
649                 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) {
657                 if (offset < 8) {
658                         *reg = ~0U; /* Force PPIs/SGIs to 1 */
659                         return false;
660                 }
661
662                 val = vgic_cfg_compress(val);
663                 if (offset & 4) {
664                         *reg &= 0xffff;
665                         *reg |= val << 16;
666                 } else {
667                         *reg &= 0xffff << 16;
668                         *reg |= val;
669                 }
670         }
671
672         return false;
673 }
674
675 /**
676  * vgic_unqueue_irqs - move pending/active IRQs from LRs to the distributor
677  * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs
678  *
679  * Move any IRQs that have already been assigned to LRs back to the
680  * emulated distributor state so that the complete emulated state can be read
681  * from the main emulation structures without investigating the LRs.
682  */
683 void vgic_unqueue_irqs(struct kvm_vcpu *vcpu)
684 {
685         struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
686         int i;
687
688         for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
689                 struct vgic_lr lr = vgic_get_lr(vcpu, i);
690
691                 /*
692                  * There are three options for the state bits:
693                  *
694                  * 01: pending
695                  * 10: active
696                  * 11: pending and active
697                  */
698                 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                 }
713
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                  */
720                 if (lr.state & LR_STATE_PENDING) {
721                         vgic_dist_irq_set_pending(vcpu, lr.irq);
722                         lr.state &= ~LR_STATE_PENDING;
723                 }
724
725                 vgic_set_lr(vcpu, i, lr);
726
727                 /*
728                  * Mark the LR as free for other use.
729                  */
730                 BUG_ON(lr.state & LR_STATE_MASK);
731                 vgic_retire_lr(i, lr.irq, vcpu);
732                 vgic_irq_clear_queued(vcpu, lr.irq);
733
734                 /* Finally update the VGIC state. */
735                 vgic_update_state(vcpu->kvm);
736         }
737 }
738
739 const
740 struct vgic_io_range *vgic_find_range(const struct vgic_io_range *ranges,
741                                       int len, gpa_t offset)
742 {
743         while (ranges->len) {
744                 if (offset >= ranges->base &&
745                     (offset + len) <= (ranges->base + ranges->len))
746                         return ranges;
747                 ranges++;
748         }
749
750         return NULL;
751 }
752
753 static bool vgic_validate_access(const struct vgic_dist *dist,
754                                  const struct vgic_io_range *range,
755                                  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
769 /*
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  */
779 static bool call_range_handler(struct kvm_vcpu *vcpu,
780                                struct kvm_exit_mmio *mmio,
781                                unsigned long offset,
782                                const struct vgic_io_range *range)
783 {
784         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;
797         mmio32.private = mmio->private;
798
799         mmio32.phys_addr = mmio->phys_addr + 4;
800         mmio32.data = &((u32 *)mmio->data)[1];
801         ret = range->handle_mmio(vcpu, &mmio32, offset + 4);
802
803         mmio32.phys_addr = mmio->phys_addr;
804         mmio32.data = &((u32 *)mmio->data)[0];
805         ret |= range->handle_mmio(vcpu, &mmio32, offset);
806
807         return ret;
808 }
809
810 /**
811  * vgic_handle_mmio_access - handle an in-kernel MMIO access
812  * This is called by the read/write KVM IO device wrappers below.
813  * @vcpu:       pointer to the vcpu performing the access
814  * @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
819  *
820  * returns true if the MMIO access could be performed
821  */
822 static 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)
825 {
826         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
827         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;
832         bool updated_state;
833         gpa_t offset;
834
835         offset = addr - iodev->addr;
836         range = vgic_find_range(iodev->reg_ranges, len, offset);
837         if (unlikely(!range || !range->handle_mmio)) {
838                 pr_warn("Unhandled access %d %08llx %d\n", is_write, addr, len);
839                 return -ENXIO;
840         }
841
842         mmio.phys_addr = addr;
843         mmio.len = len;
844         mmio.is_write = is_write;
845         mmio.data = val;
846         mmio.private = iodev->redist_vcpu;
847
848         spin_lock(&dist->lock);
849         offset -= range->base;
850         if (vgic_validate_access(dist, range, offset)) {
851                 updated_state = call_range_handler(vcpu, &mmio, offset, range);
852         } else {
853                 if (!is_write)
854                         memset(val, 0, len);
855                 updated_state = false;
856         }
857         spin_unlock(&dist->lock);
858         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
863         kvm_handle_mmio_return(vcpu, run);
864
865         if (updated_state)
866                 vgic_kick_vcpus(vcpu->kvm);
867
868         return 0;
869 }
870
871 static int vgic_handle_mmio_read(struct kvm_vcpu *vcpu,
872                                  struct kvm_io_device *this,
873                                  gpa_t addr, int len, void *val)
874 {
875         return vgic_handle_mmio_access(vcpu, this, addr, len, val, false);
876 }
877
878 static 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
886 struct 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  */
907 int 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;
936 }
937
938 static int vgic_nr_shared_irqs(struct vgic_dist *dist)
939 {
940         return dist->nr_irqs - VGIC_NR_PRIVATE_IRQS;
941 }
942
943 static 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
973 static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
974 {
975         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
976         unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
977         unsigned long pending_private, pending_shared;
978         int nr_shared = vgic_nr_shared_irqs(dist);
979         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
985         pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id);
986         enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
987         bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
988
989         pending = vgic_bitmap_get_shared_map(&dist->irq_pending);
990         enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
991         bitmap_and(pend_shared, pending, enabled, nr_shared);
992         bitmap_and(pend_shared, pend_shared,
993                    vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
994                    nr_shared);
995
996         pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
997         pending_shared = find_first_bit(pend_shared, nr_shared);
998         return (pending_private < VGIC_NR_PRIVATE_IRQS ||
999                 pending_shared < vgic_nr_shared_irqs(dist));
1000 }
1001
1002 /*
1003  * Update the interrupt state and determine which CPUs have pending
1004  * or active interrupts. Must be called with distributor lock held.
1005  */
1006 void vgic_update_state(struct kvm *kvm)
1007 {
1008         struct vgic_dist *dist = &kvm->arch.vgic;
1009         struct kvm_vcpu *vcpu;
1010         int c;
1011
1012         if (!dist->enabled) {
1013                 set_bit(0, dist->irq_pending_on_cpu);
1014                 return;
1015         }
1016
1017         kvm_for_each_vcpu(c, vcpu, kvm) {
1018                 if (compute_pending_for_cpu(vcpu))
1019                         set_bit(c, dist->irq_pending_on_cpu);
1020
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);
1025         }
1026 }
1027
1028 static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr)
1029 {
1030         return vgic_ops->get_lr(vcpu, lr);
1031 }
1032
1033 static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr,
1034                                struct vgic_lr vlr)
1035 {
1036         vgic_ops->set_lr(vcpu, lr, vlr);
1037 }
1038
1039 static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
1040                                struct vgic_lr vlr)
1041 {
1042         vgic_ops->sync_lr_elrsr(vcpu, lr, vlr);
1043 }
1044
1045 static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu)
1046 {
1047         return vgic_ops->get_elrsr(vcpu);
1048 }
1049
1050 static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu)
1051 {
1052         return vgic_ops->get_eisr(vcpu);
1053 }
1054
1055 static inline void vgic_clear_eisr(struct kvm_vcpu *vcpu)
1056 {
1057         vgic_ops->clear_eisr(vcpu);
1058 }
1059
1060 static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu)
1061 {
1062         return vgic_ops->get_interrupt_status(vcpu);
1063 }
1064
1065 static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu)
1066 {
1067         vgic_ops->enable_underflow(vcpu);
1068 }
1069
1070 static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu)
1071 {
1072         vgic_ops->disable_underflow(vcpu);
1073 }
1074
1075 void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1076 {
1077         vgic_ops->get_vmcr(vcpu, vmcr);
1078 }
1079
1080 void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1081 {
1082         vgic_ops->set_vmcr(vcpu, vmcr);
1083 }
1084
1085 static inline void vgic_enable(struct kvm_vcpu *vcpu)
1086 {
1087         vgic_ops->enable(vcpu);
1088 }
1089
1090 static 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         /*
1096          * We must transfer the pending state back to the distributor before
1097          * retiring the LR, otherwise we may loose edge-triggered interrupts.
1098          */
1099         if (vlr.state & LR_STATE_PENDING) {
1100                 vgic_dist_irq_set_pending(vcpu, irq);
1101                 vlr.hwirq = 0;
1102         }
1103
1104         vlr.state = 0;
1105         vgic_set_lr(vcpu, lr_nr, vlr);
1106         clear_bit(lr_nr, vgic_cpu->lr_used);
1107         vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
1108         vgic_sync_lr_elrsr(vcpu, lr_nr, vlr);
1109 }
1110
1111 /*
1112  * An interrupt may have been disabled after being made pending on the
1113  * CPU interface (the classic case is a timer running while we're
1114  * rebooting the guest - the interrupt would kick as soon as the CPU
1115  * interface gets enabled, with deadly consequences).
1116  *
1117  * The solution is to examine already active LRs, and check the
1118  * interrupt is still enabled. If not, just retire it.
1119  */
1120 static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
1121 {
1122         struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1123         int lr;
1124
1125         for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) {
1126                 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
1127
1128                 if (!vgic_irq_is_enabled(vcpu, vlr.irq)) {
1129                         vgic_retire_lr(lr, vlr.irq, vcpu);
1130                         if (vgic_irq_is_queued(vcpu, vlr.irq))
1131                                 vgic_irq_clear_queued(vcpu, vlr.irq);
1132                 }
1133         }
1134 }
1135
1136 static void vgic_queue_irq_to_lr(struct kvm_vcpu *vcpu, int irq,
1137                                  int lr_nr, struct vgic_lr vlr)
1138 {
1139         if (vgic_irq_is_active(vcpu, irq)) {
1140                 vlr.state |= LR_STATE_ACTIVE;
1141                 kvm_debug("Set active, clear distributor: 0x%x\n", vlr.state);
1142                 vgic_irq_clear_active(vcpu, irq);
1143                 vgic_update_state(vcpu->kvm);
1144         } else {
1145                 WARN_ON(!vgic_dist_irq_is_pending(vcpu, irq));
1146                 vlr.state |= LR_STATE_PENDING;
1147                 kvm_debug("Set pending: 0x%x\n", vlr.state);
1148         }
1149
1150         if (!vgic_irq_is_edge(vcpu, irq))
1151                 vlr.state |= LR_EOI_INT;
1152
1153         if (vlr.irq >= VGIC_NR_SGIS) {
1154                 struct irq_phys_map *map;
1155                 map = vgic_irq_map_search(vcpu, irq);
1156
1157                 if (map) {
1158                         vlr.hwirq = map->phys_irq;
1159                         vlr.state |= LR_HW;
1160                         vlr.state &= ~LR_EOI_INT;
1161
1162                         /*
1163                          * Make sure we're not going to sample this
1164                          * again, as a HW-backed interrupt cannot be
1165                          * in the PENDING_ACTIVE stage.
1166                          */
1167                         vgic_irq_set_queued(vcpu, irq);
1168                 }
1169         }
1170
1171         vgic_set_lr(vcpu, lr_nr, vlr);
1172         vgic_sync_lr_elrsr(vcpu, lr_nr, vlr);
1173 }
1174
1175 /*
1176  * Queue an interrupt to a CPU virtual interface. Return true on success,
1177  * or false if it wasn't possible to queue it.
1178  * sgi_source must be zero for any non-SGI interrupts.
1179  */
1180 bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
1181 {
1182         struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1183         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1184         struct vgic_lr vlr;
1185         int lr;
1186
1187         /* Sanitize the input... */
1188         BUG_ON(sgi_source_id & ~7);
1189         BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
1190         BUG_ON(irq >= dist->nr_irqs);
1191
1192         kvm_debug("Queue IRQ%d\n", irq);
1193
1194         lr = vgic_cpu->vgic_irq_lr_map[irq];
1195
1196         /* Do we have an active interrupt for the same CPUID? */
1197         if (lr != LR_EMPTY) {
1198                 vlr = vgic_get_lr(vcpu, lr);
1199                 if (vlr.source == sgi_source_id) {
1200                         kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq);
1201                         BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
1202                         vgic_queue_irq_to_lr(vcpu, irq, lr, vlr);
1203                         return true;
1204                 }
1205         }
1206
1207         /* Try to use another LR for this interrupt */
1208         lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
1209                                vgic->nr_lr);
1210         if (lr >= vgic->nr_lr)
1211                 return false;
1212
1213         kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
1214         vgic_cpu->vgic_irq_lr_map[irq] = lr;
1215         set_bit(lr, vgic_cpu->lr_used);
1216
1217         vlr.irq = irq;
1218         vlr.source = sgi_source_id;
1219         vlr.state = 0;
1220         vgic_queue_irq_to_lr(vcpu, irq, lr, vlr);
1221
1222         return true;
1223 }
1224
1225 static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
1226 {
1227         if (!vgic_can_sample_irq(vcpu, irq))
1228                 return true; /* level interrupt, already queued */
1229
1230         if (vgic_queue_irq(vcpu, 0, irq)) {
1231                 if (vgic_irq_is_edge(vcpu, irq)) {
1232                         vgic_dist_irq_clear_pending(vcpu, irq);
1233                         vgic_cpu_irq_clear(vcpu, irq);
1234                 } else {
1235                         vgic_irq_set_queued(vcpu, irq);
1236                 }
1237
1238                 return true;
1239         }
1240
1241         return false;
1242 }
1243
1244 /*
1245  * Fill the list registers with pending interrupts before running the
1246  * guest.
1247  */
1248 static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1249 {
1250         struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1251         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1252         unsigned long *pa_percpu, *pa_shared;
1253         int i, vcpu_id;
1254         int overflow = 0;
1255         int nr_shared = vgic_nr_shared_irqs(dist);
1256
1257         vcpu_id = vcpu->vcpu_id;
1258
1259         pa_percpu = vcpu->arch.vgic_cpu.pend_act_percpu;
1260         pa_shared = vcpu->arch.vgic_cpu.pend_act_shared;
1261
1262         bitmap_or(pa_percpu, vgic_cpu->pending_percpu, vgic_cpu->active_percpu,
1263                   VGIC_NR_PRIVATE_IRQS);
1264         bitmap_or(pa_shared, vgic_cpu->pending_shared, vgic_cpu->active_shared,
1265                   nr_shared);
1266         /*
1267          * We may not have any pending interrupt, or the interrupts
1268          * may have been serviced from another vcpu. In all cases,
1269          * move along.
1270          */
1271         if (!kvm_vgic_vcpu_pending_irq(vcpu) && !kvm_vgic_vcpu_active_irq(vcpu))
1272                 goto epilog;
1273
1274         /* SGIs */
1275         for_each_set_bit(i, pa_percpu, VGIC_NR_SGIS) {
1276                 if (!queue_sgi(vcpu, i))
1277                         overflow = 1;
1278         }
1279
1280         /* PPIs */
1281         for_each_set_bit_from(i, pa_percpu, VGIC_NR_PRIVATE_IRQS) {
1282                 if (!vgic_queue_hwirq(vcpu, i))
1283                         overflow = 1;
1284         }
1285
1286         /* SPIs */
1287         for_each_set_bit(i, pa_shared, nr_shared) {
1288                 if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
1289                         overflow = 1;
1290         }
1291
1292
1293
1294
1295 epilog:
1296         if (overflow) {
1297                 vgic_enable_underflow(vcpu);
1298         } else {
1299                 vgic_disable_underflow(vcpu);
1300                 /*
1301                  * We're about to run this VCPU, and we've consumed
1302                  * everything the distributor had in store for
1303                  * us. Claim we don't have anything pending. We'll
1304                  * adjust that if needed while exiting.
1305                  */
1306                 clear_bit(vcpu_id, dist->irq_pending_on_cpu);
1307         }
1308 }
1309
1310 static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1311 {
1312         u32 status = vgic_get_interrupt_status(vcpu);
1313         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1314         bool level_pending = false;
1315         struct kvm *kvm = vcpu->kvm;
1316
1317         kvm_debug("STATUS = %08x\n", status);
1318
1319         if (status & INT_STATUS_EOI) {
1320                 /*
1321                  * Some level interrupts have been EOIed. Clear their
1322                  * active bit.
1323                  */
1324                 u64 eisr = vgic_get_eisr(vcpu);
1325                 unsigned long *eisr_ptr = u64_to_bitmask(&eisr);
1326                 int lr;
1327
1328                 for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
1329                         struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
1330                         WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
1331
1332                         spin_lock(&dist->lock);
1333                         vgic_irq_clear_queued(vcpu, vlr.irq);
1334                         WARN_ON(vlr.state & LR_STATE_MASK);
1335                         vlr.state = 0;
1336                         vgic_set_lr(vcpu, lr, vlr);
1337
1338                         /*
1339                          * If the IRQ was EOIed it was also ACKed and we we
1340                          * therefore assume we can clear the soft pending
1341                          * state (should it had been set) for this interrupt.
1342                          *
1343                          * Note: if the IRQ soft pending state was set after
1344                          * the IRQ was acked, it actually shouldn't be
1345                          * cleared, but we have no way of knowing that unless
1346                          * we start trapping ACKs when the soft-pending state
1347                          * is set.
1348                          */
1349                         vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
1350
1351                         /*
1352                          * kvm_notify_acked_irq calls kvm_set_irq()
1353                          * to reset the IRQ level. Need to release the
1354                          * lock for kvm_set_irq to grab it.
1355                          */
1356                         spin_unlock(&dist->lock);
1357
1358                         kvm_notify_acked_irq(kvm, 0,
1359                                              vlr.irq - VGIC_NR_PRIVATE_IRQS);
1360                         spin_lock(&dist->lock);
1361
1362                         /* Any additional pending interrupt? */
1363                         if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
1364                                 vgic_cpu_irq_set(vcpu, vlr.irq);
1365                                 level_pending = true;
1366                         } else {
1367                                 vgic_dist_irq_clear_pending(vcpu, vlr.irq);
1368                                 vgic_cpu_irq_clear(vcpu, vlr.irq);
1369                         }
1370
1371                         spin_unlock(&dist->lock);
1372
1373                         /*
1374                          * Despite being EOIed, the LR may not have
1375                          * been marked as empty.
1376                          */
1377                         vgic_sync_lr_elrsr(vcpu, lr, vlr);
1378                 }
1379         }
1380
1381         if (status & INT_STATUS_UNDERFLOW)
1382                 vgic_disable_underflow(vcpu);
1383
1384         /*
1385          * In the next iterations of the vcpu loop, if we sync the vgic state
1386          * after flushing it, but before entering the guest (this happens for
1387          * pending signals and vmid rollovers), then make sure we don't pick
1388          * up any old maintenance interrupts here.
1389          */
1390         vgic_clear_eisr(vcpu);
1391
1392         return level_pending;
1393 }
1394
1395 /*
1396  * Save the physical active state, and reset it to inactive.
1397  *
1398  * Return 1 if HW interrupt went from active to inactive, and 0 otherwise.
1399  */
1400 static int vgic_sync_hwirq(struct kvm_vcpu *vcpu, struct vgic_lr vlr)
1401 {
1402         struct irq_phys_map *map;
1403         int ret;
1404
1405         if (!(vlr.state & LR_HW))
1406                 return 0;
1407
1408         map = vgic_irq_map_search(vcpu, vlr.irq);
1409         BUG_ON(!map || !map->active);
1410
1411         ret = irq_get_irqchip_state(map->irq,
1412                                     IRQCHIP_STATE_ACTIVE,
1413                                     &map->active);
1414
1415         WARN_ON(ret);
1416
1417         if (map->active)
1418                 return 0;
1419
1420         return 1;
1421 }
1422
1423 /* Sync back the VGIC state after a guest run */
1424 static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1425 {
1426         struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1427         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1428         u64 elrsr;
1429         unsigned long *elrsr_ptr;
1430         int lr, pending;
1431         bool level_pending;
1432
1433         level_pending = vgic_process_maintenance(vcpu);
1434         elrsr = vgic_get_elrsr(vcpu);
1435         elrsr_ptr = u64_to_bitmask(&elrsr);
1436
1437         /* Deal with HW interrupts, and clear mappings for empty LRs */
1438         for (lr = 0; lr < vgic->nr_lr; lr++) {
1439                 struct vgic_lr vlr;
1440
1441                 if (!test_bit(lr, vgic_cpu->lr_used))
1442                         continue;
1443
1444                 vlr = vgic_get_lr(vcpu, lr);
1445                 if (vgic_sync_hwirq(vcpu, vlr)) {
1446                         /*
1447                          * So this is a HW interrupt that the guest
1448                          * EOI-ed. Clean the LR state and allow the
1449                          * interrupt to be sampled again.
1450                          */
1451                         vlr.state = 0;
1452                         vlr.hwirq = 0;
1453                         vgic_set_lr(vcpu, lr, vlr);
1454                         vgic_irq_clear_queued(vcpu, vlr.irq);
1455                         set_bit(lr, elrsr_ptr);
1456                 }
1457
1458                 if (!test_bit(lr, elrsr_ptr))
1459                         continue;
1460
1461                 clear_bit(lr, vgic_cpu->lr_used);
1462
1463                 BUG_ON(vlr.irq >= dist->nr_irqs);
1464                 vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
1465         }
1466
1467         /* Check if we still have something up our sleeve... */
1468         pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr);
1469         if (level_pending || pending < vgic->nr_lr)
1470                 set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
1471 }
1472
1473 void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1474 {
1475         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1476
1477         if (!irqchip_in_kernel(vcpu->kvm))
1478                 return;
1479
1480         spin_lock(&dist->lock);
1481         __kvm_vgic_flush_hwstate(vcpu);
1482         spin_unlock(&dist->lock);
1483 }
1484
1485 void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1486 {
1487         if (!irqchip_in_kernel(vcpu->kvm))
1488                 return;
1489
1490         __kvm_vgic_sync_hwstate(vcpu);
1491 }
1492
1493 int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
1494 {
1495         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1496
1497         if (!irqchip_in_kernel(vcpu->kvm))
1498                 return 0;
1499
1500         return test_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
1501 }
1502
1503 int kvm_vgic_vcpu_active_irq(struct kvm_vcpu *vcpu)
1504 {
1505         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1506
1507         if (!irqchip_in_kernel(vcpu->kvm))
1508                 return 0;
1509
1510         return test_bit(vcpu->vcpu_id, dist->irq_active_on_cpu);
1511 }
1512
1513
1514 void vgic_kick_vcpus(struct kvm *kvm)
1515 {
1516         struct kvm_vcpu *vcpu;
1517         int c;
1518
1519         /*
1520          * We've injected an interrupt, time to find out who deserves
1521          * a good kick...
1522          */
1523         kvm_for_each_vcpu(c, vcpu, kvm) {
1524                 if (kvm_vgic_vcpu_pending_irq(vcpu))
1525                         kvm_vcpu_kick(vcpu);
1526         }
1527 }
1528
1529 static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
1530 {
1531         int edge_triggered = vgic_irq_is_edge(vcpu, irq);
1532
1533         /*
1534          * Only inject an interrupt if:
1535          * - edge triggered and we have a rising edge
1536          * - level triggered and we change level
1537          */
1538         if (edge_triggered) {
1539                 int state = vgic_dist_irq_is_pending(vcpu, irq);
1540                 return level > state;
1541         } else {
1542                 int state = vgic_dist_irq_get_level(vcpu, irq);
1543                 return level != state;
1544         }
1545 }
1546
1547 static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
1548                                    struct irq_phys_map *map,
1549                                    unsigned int irq_num, bool level)
1550 {
1551         struct vgic_dist *dist = &kvm->arch.vgic;
1552         struct kvm_vcpu *vcpu;
1553         int edge_triggered, level_triggered;
1554         int enabled;
1555         bool ret = true, can_inject = true;
1556
1557         if (irq_num >= min(kvm->arch.vgic.nr_irqs, 1020))
1558                 return -EINVAL;
1559
1560         spin_lock(&dist->lock);
1561
1562         vcpu = kvm_get_vcpu(kvm, cpuid);
1563         edge_triggered = vgic_irq_is_edge(vcpu, irq_num);
1564         level_triggered = !edge_triggered;
1565
1566         if (!vgic_validate_injection(vcpu, irq_num, level)) {
1567                 ret = false;
1568                 goto out;
1569         }
1570
1571         if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
1572                 cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
1573                 if (cpuid == VCPU_NOT_ALLOCATED) {
1574                         /* Pretend we use CPU0, and prevent injection */
1575                         cpuid = 0;
1576                         can_inject = false;
1577                 }
1578                 vcpu = kvm_get_vcpu(kvm, cpuid);
1579         }
1580
1581         kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
1582
1583         if (level) {
1584                 if (level_triggered)
1585                         vgic_dist_irq_set_level(vcpu, irq_num);
1586                 vgic_dist_irq_set_pending(vcpu, irq_num);
1587         } else {
1588                 if (level_triggered) {
1589                         vgic_dist_irq_clear_level(vcpu, irq_num);
1590                         if (!vgic_dist_irq_soft_pend(vcpu, irq_num)) {
1591                                 vgic_dist_irq_clear_pending(vcpu, irq_num);
1592                                 vgic_cpu_irq_clear(vcpu, irq_num);
1593                                 if (!compute_pending_for_cpu(vcpu))
1594                                         clear_bit(cpuid, dist->irq_pending_on_cpu);
1595                         }
1596                 }
1597
1598                 ret = false;
1599                 goto out;
1600         }
1601
1602         enabled = vgic_irq_is_enabled(vcpu, irq_num);
1603
1604         if (!enabled || !can_inject) {
1605                 ret = false;
1606                 goto out;
1607         }
1608
1609         if (!vgic_can_sample_irq(vcpu, irq_num)) {
1610                 /*
1611                  * Level interrupt in progress, will be picked up
1612                  * when EOId.
1613                  */
1614                 ret = false;
1615                 goto out;
1616         }
1617
1618         if (level) {
1619                 vgic_cpu_irq_set(vcpu, irq_num);
1620                 set_bit(cpuid, dist->irq_pending_on_cpu);
1621         }
1622
1623 out:
1624         spin_unlock(&dist->lock);
1625
1626         if (ret) {
1627                 /* kick the specified vcpu */
1628                 kvm_vcpu_kick(kvm_get_vcpu(kvm, cpuid));
1629         }
1630
1631         return 0;
1632 }
1633
1634 static int vgic_lazy_init(struct kvm *kvm)
1635 {
1636         int ret = 0;
1637
1638         if (unlikely(!vgic_initialized(kvm))) {
1639                 /*
1640                  * We only provide the automatic initialization of the VGIC
1641                  * for the legacy case of a GICv2. Any other type must
1642                  * be explicitly initialized once setup with the respective
1643                  * KVM device call.
1644                  */
1645                 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2)
1646                         return -EBUSY;
1647
1648                 mutex_lock(&kvm->lock);
1649                 ret = vgic_init(kvm);
1650                 mutex_unlock(&kvm->lock);
1651         }
1652
1653         return ret;
1654 }
1655
1656 /**
1657  * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
1658  * @kvm:     The VM structure pointer
1659  * @cpuid:   The CPU for PPIs
1660  * @irq_num: The IRQ number that is assigned to the device. This IRQ
1661  *           must not be mapped to a HW interrupt.
1662  * @level:   Edge-triggered:  true:  to trigger the interrupt
1663  *                            false: to ignore the call
1664  *           Level-sensitive  true:  raise the input signal
1665  *                            false: lower the input signal
1666  *
1667  * The GIC is not concerned with devices being active-LOW or active-HIGH for
1668  * level-sensitive interrupts.  You can think of the level parameter as 1
1669  * being HIGH and 0 being LOW and all devices being active-HIGH.
1670  */
1671 int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
1672                         bool level)
1673 {
1674         struct irq_phys_map *map;
1675         int ret;
1676
1677         ret = vgic_lazy_init(kvm);
1678         if (ret)
1679                 return ret;
1680
1681         map = vgic_irq_map_search(kvm_get_vcpu(kvm, cpuid), irq_num);
1682         if (map)
1683                 return -EINVAL;
1684
1685         return vgic_update_irq_pending(kvm, cpuid, NULL, irq_num, level);
1686 }
1687
1688 /**
1689  * kvm_vgic_inject_mapped_irq - Inject a physically mapped IRQ to the vgic
1690  * @kvm:     The VM structure pointer
1691  * @cpuid:   The CPU for PPIs
1692  * @map:     Pointer to a irq_phys_map structure describing the mapping
1693  * @level:   Edge-triggered:  true:  to trigger the interrupt
1694  *                            false: to ignore the call
1695  *           Level-sensitive  true:  raise the input signal
1696  *                            false: lower the input signal
1697  *
1698  * The GIC is not concerned with devices being active-LOW or active-HIGH for
1699  * level-sensitive interrupts.  You can think of the level parameter as 1
1700  * being HIGH and 0 being LOW and all devices being active-HIGH.
1701  */
1702 int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid,
1703                                struct irq_phys_map *map, bool level)
1704 {
1705         int ret;
1706
1707         ret = vgic_lazy_init(kvm);
1708         if (ret)
1709                 return ret;
1710
1711         return vgic_update_irq_pending(kvm, cpuid, map, map->virt_irq, level);
1712 }
1713
1714 static irqreturn_t vgic_maintenance_handler(int irq, void *data)
1715 {
1716         /*
1717          * We cannot rely on the vgic maintenance interrupt to be
1718          * delivered synchronously. This means we can only use it to
1719          * exit the VM, and we perform the handling of EOIed
1720          * interrupts on the exit path (see vgic_process_maintenance).
1721          */
1722         return IRQ_HANDLED;
1723 }
1724
1725 static struct list_head *vgic_get_irq_phys_map_list(struct kvm_vcpu *vcpu,
1726                                                     int virt_irq)
1727 {
1728         if (virt_irq < VGIC_NR_PRIVATE_IRQS)
1729                 return &vcpu->arch.vgic_cpu.irq_phys_map_list;
1730         else
1731                 return &vcpu->kvm->arch.vgic.irq_phys_map_list;
1732 }
1733
1734 /**
1735  * kvm_vgic_map_phys_irq - map a virtual IRQ to a physical IRQ
1736  * @vcpu: The VCPU pointer
1737  * @virt_irq: The virtual irq number
1738  * @irq: The Linux IRQ number
1739  *
1740  * Establish a mapping between a guest visible irq (@virt_irq) and a
1741  * Linux irq (@irq). On injection, @virt_irq will be associated with
1742  * the physical interrupt represented by @irq. This mapping can be
1743  * established multiple times as long as the parameters are the same.
1744  *
1745  * Returns a valid pointer on success, and an error pointer otherwise
1746  */
1747 struct irq_phys_map *kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu,
1748                                            int virt_irq, int irq)
1749 {
1750         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1751         struct list_head *root = vgic_get_irq_phys_map_list(vcpu, virt_irq);
1752         struct irq_phys_map *map;
1753         struct irq_phys_map_entry *entry;
1754         struct irq_desc *desc;
1755         struct irq_data *data;
1756         int phys_irq;
1757
1758         desc = irq_to_desc(irq);
1759         if (!desc) {
1760                 kvm_err("%s: no interrupt descriptor\n", __func__);
1761                 return ERR_PTR(-EINVAL);
1762         }
1763
1764         data = irq_desc_get_irq_data(desc);
1765         while (data->parent_data)
1766                 data = data->parent_data;
1767
1768         phys_irq = data->hwirq;
1769
1770         /* Create a new mapping */
1771         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1772         if (!entry)
1773                 return ERR_PTR(-ENOMEM);
1774
1775         spin_lock(&dist->irq_phys_map_lock);
1776
1777         /* Try to match an existing mapping */
1778         map = vgic_irq_map_search(vcpu, virt_irq);
1779         if (map) {
1780                 /* Make sure this mapping matches */
1781                 if (map->phys_irq != phys_irq   ||
1782                     map->irq      != irq)
1783                         map = ERR_PTR(-EINVAL);
1784
1785                 /* Found an existing, valid mapping */
1786                 goto out;
1787         }
1788
1789         map           = &entry->map;
1790         map->virt_irq = virt_irq;
1791         map->phys_irq = phys_irq;
1792         map->irq      = irq;
1793
1794         list_add_tail_rcu(&entry->entry, root);
1795
1796 out:
1797         spin_unlock(&dist->irq_phys_map_lock);
1798         /* If we've found a hit in the existing list, free the useless
1799          * entry */
1800         if (IS_ERR(map) || map != &entry->map)
1801                 kfree(entry);
1802         return map;
1803 }
1804
1805 static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu,
1806                                                 int virt_irq)
1807 {
1808         struct list_head *root = vgic_get_irq_phys_map_list(vcpu, virt_irq);
1809         struct irq_phys_map_entry *entry;
1810         struct irq_phys_map *map;
1811
1812         rcu_read_lock();
1813
1814         list_for_each_entry_rcu(entry, root, entry) {
1815                 map = &entry->map;
1816                 if (map->virt_irq == virt_irq) {
1817                         rcu_read_unlock();
1818                         return map;
1819                 }
1820         }
1821
1822         rcu_read_unlock();
1823
1824         return NULL;
1825 }
1826
1827 static void vgic_free_phys_irq_map_rcu(struct rcu_head *rcu)
1828 {
1829         struct irq_phys_map_entry *entry;
1830
1831         entry = container_of(rcu, struct irq_phys_map_entry, rcu);
1832         kfree(entry);
1833 }
1834
1835 /**
1836  * kvm_vgic_get_phys_irq_active - Return the active state of a mapped IRQ
1837  *
1838  * Return the logical active state of a mapped interrupt. This doesn't
1839  * necessarily reflects the current HW state.
1840  */
1841 bool kvm_vgic_get_phys_irq_active(struct irq_phys_map *map)
1842 {
1843         BUG_ON(!map);
1844         return map->active;
1845 }
1846
1847 /**
1848  * kvm_vgic_set_phys_irq_active - Set the active state of a mapped IRQ
1849  *
1850  * Set the logical active state of a mapped interrupt. This doesn't
1851  * immediately affects the HW state.
1852  */
1853 void kvm_vgic_set_phys_irq_active(struct irq_phys_map *map, bool active)
1854 {
1855         BUG_ON(!map);
1856         map->active = active;
1857 }
1858
1859 /**
1860  * kvm_vgic_unmap_phys_irq - Remove a virtual to physical IRQ mapping
1861  * @vcpu: The VCPU pointer
1862  * @map: The pointer to a mapping obtained through kvm_vgic_map_phys_irq
1863  *
1864  * Remove an existing mapping between virtual and physical interrupts.
1865  */
1866 int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, struct irq_phys_map *map)
1867 {
1868         struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1869         struct irq_phys_map_entry *entry;
1870         struct list_head *root;
1871
1872         if (!map)
1873                 return -EINVAL;
1874
1875         root = vgic_get_irq_phys_map_list(vcpu, map->virt_irq);
1876
1877         spin_lock(&dist->irq_phys_map_lock);
1878
1879         list_for_each_entry(entry, root, entry) {
1880                 if (&entry->map == map) {
1881                         list_del_rcu(&entry->entry);
1882                         call_rcu(&entry->rcu, vgic_free_phys_irq_map_rcu);
1883                         break;
1884                 }
1885         }
1886
1887         spin_unlock(&dist->irq_phys_map_lock);
1888
1889         return 0;
1890 }
1891
1892 static void vgic_destroy_irq_phys_map(struct kvm *kvm, struct list_head *root)
1893 {
1894         struct vgic_dist *dist = &kvm->arch.vgic;
1895         struct irq_phys_map_entry *entry;
1896
1897         spin_lock(&dist->irq_phys_map_lock);
1898
1899         list_for_each_entry(entry, root, entry) {
1900                 list_del_rcu(&entry->entry);
1901                 call_rcu(&entry->rcu, vgic_free_phys_irq_map_rcu);
1902         }
1903
1904         spin_unlock(&dist->irq_phys_map_lock);
1905 }
1906
1907 void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
1908 {
1909         struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1910
1911         kfree(vgic_cpu->pending_shared);
1912         kfree(vgic_cpu->active_shared);
1913         kfree(vgic_cpu->pend_act_shared);
1914         kfree(vgic_cpu->vgic_irq_lr_map);
1915         vgic_destroy_irq_phys_map(vcpu->kvm, &vgic_cpu->irq_phys_map_list);
1916         vgic_cpu->pending_shared = NULL;
1917         vgic_cpu->active_shared = NULL;
1918         vgic_cpu->pend_act_shared = NULL;
1919         vgic_cpu->vgic_irq_lr_map = NULL;
1920 }
1921
1922 static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
1923 {
1924         struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1925
1926         int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8;
1927         vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL);
1928         vgic_cpu->active_shared = kzalloc(sz, GFP_KERNEL);
1929         vgic_cpu->pend_act_shared = kzalloc(sz, GFP_KERNEL);
1930         vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL);
1931
1932         if (!vgic_cpu->pending_shared
1933                 || !vgic_cpu->active_shared
1934                 || !vgic_cpu->pend_act_shared
1935                 || !vgic_cpu->vgic_irq_lr_map) {
1936                 kvm_vgic_vcpu_destroy(vcpu);
1937                 return -ENOMEM;
1938         }
1939
1940         memset(vgic_cpu->vgic_irq_lr_map, LR_EMPTY, nr_irqs);
1941
1942         /*
1943          * Store the number of LRs per vcpu, so we don't have to go
1944          * all the way to the distributor structure to find out. Only
1945          * assembly code should use this one.
1946          */
1947         vgic_cpu->nr_lr = vgic->nr_lr;
1948
1949         return 0;
1950 }
1951
1952 /**
1953  * kvm_vgic_vcpu_early_init - Earliest possible per-vcpu vgic init stage
1954  *
1955  * No memory allocation should be performed here, only static init.
1956  */
1957 void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu)
1958 {
1959         struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1960         INIT_LIST_HEAD(&vgic_cpu->irq_phys_map_list);
1961 }
1962
1963 /**
1964  * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW
1965  *
1966  * The host's GIC naturally limits the maximum amount of VCPUs a guest
1967  * can use.
1968  */
1969 int kvm_vgic_get_max_vcpus(void)
1970 {
1971         return vgic->max_gic_vcpus;
1972 }
1973
1974 void kvm_vgic_destroy(struct kvm *kvm)
1975 {
1976         struct vgic_dist *dist = &kvm->arch.vgic;
1977         struct kvm_vcpu *vcpu;
1978         int i;
1979
1980         kvm_for_each_vcpu(i, vcpu, kvm)
1981                 kvm_vgic_vcpu_destroy(vcpu);
1982
1983         vgic_free_bitmap(&dist->irq_enabled);
1984         vgic_free_bitmap(&dist->irq_level);
1985         vgic_free_bitmap(&dist->irq_pending);
1986         vgic_free_bitmap(&dist->irq_soft_pend);
1987         vgic_free_bitmap(&dist->irq_queued);
1988         vgic_free_bitmap(&dist->irq_cfg);
1989         vgic_free_bytemap(&dist->irq_priority);
1990         if (dist->irq_spi_target) {
1991                 for (i = 0; i < dist->nr_cpus; i++)
1992                         vgic_free_bitmap(&dist->irq_spi_target[i]);
1993         }
1994         kfree(dist->irq_sgi_sources);
1995         kfree(dist->irq_spi_cpu);
1996         kfree(dist->irq_spi_mpidr);
1997         kfree(dist->irq_spi_target);
1998         kfree(dist->irq_pending_on_cpu);
1999         kfree(dist->irq_active_on_cpu);
2000         vgic_destroy_irq_phys_map(kvm, &dist->irq_phys_map_list);
2001         dist->irq_sgi_sources = NULL;
2002         dist->irq_spi_cpu = NULL;
2003         dist->irq_spi_target = NULL;
2004         dist->irq_pending_on_cpu = NULL;
2005         dist->irq_active_on_cpu = NULL;
2006         dist->nr_cpus = 0;
2007 }
2008
2009 /*
2010  * Allocate and initialize the various data structures. Must be called
2011  * with kvm->lock held!
2012  */
2013 int vgic_init(struct kvm *kvm)
2014 {
2015         struct vgic_dist *dist = &kvm->arch.vgic;
2016         struct kvm_vcpu *vcpu;
2017         int nr_cpus, nr_irqs;
2018         int ret, i, vcpu_id;
2019
2020         if (vgic_initialized(kvm))
2021                 return 0;
2022
2023         nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus);
2024         if (!nr_cpus)           /* No vcpus? Can't be good... */
2025                 return -ENODEV;
2026
2027         /*
2028          * If nobody configured the number of interrupts, use the
2029          * legacy one.
2030          */
2031         if (!dist->nr_irqs)
2032                 dist->nr_irqs = VGIC_NR_IRQS_LEGACY;
2033
2034         nr_irqs = dist->nr_irqs;
2035
2036         ret  = vgic_init_bitmap(&dist->irq_enabled, nr_cpus, nr_irqs);
2037         ret |= vgic_init_bitmap(&dist->irq_level, nr_cpus, nr_irqs);
2038         ret |= vgic_init_bitmap(&dist->irq_pending, nr_cpus, nr_irqs);
2039         ret |= vgic_init_bitmap(&dist->irq_soft_pend, nr_cpus, nr_irqs);
2040         ret |= vgic_init_bitmap(&dist->irq_queued, nr_cpus, nr_irqs);
2041         ret |= vgic_init_bitmap(&dist->irq_active, nr_cpus, nr_irqs);
2042         ret |= vgic_init_bitmap(&dist->irq_cfg, nr_cpus, nr_irqs);
2043         ret |= vgic_init_bytemap(&dist->irq_priority, nr_cpus, nr_irqs);
2044
2045         if (ret)
2046                 goto out;
2047
2048         dist->irq_sgi_sources = kzalloc(nr_cpus * VGIC_NR_SGIS, GFP_KERNEL);
2049         dist->irq_spi_cpu = kzalloc(nr_irqs - VGIC_NR_PRIVATE_IRQS, GFP_KERNEL);
2050         dist->irq_spi_target = kzalloc(sizeof(*dist->irq_spi_target) * nr_cpus,
2051                                        GFP_KERNEL);
2052         dist->irq_pending_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
2053                                            GFP_KERNEL);
2054         dist->irq_active_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
2055                                            GFP_KERNEL);
2056         if (!dist->irq_sgi_sources ||
2057             !dist->irq_spi_cpu ||
2058             !dist->irq_spi_target ||
2059             !dist->irq_pending_on_cpu ||
2060             !dist->irq_active_on_cpu) {
2061                 ret = -ENOMEM;
2062                 goto out;
2063         }
2064
2065         for (i = 0; i < nr_cpus; i++)
2066                 ret |= vgic_init_bitmap(&dist->irq_spi_target[i],
2067                                         nr_cpus, nr_irqs);
2068
2069         if (ret)
2070                 goto out;
2071
2072         ret = kvm->arch.vgic.vm_ops.init_model(kvm);
2073         if (ret)
2074                 goto out;
2075
2076         kvm_for_each_vcpu(vcpu_id, vcpu, kvm) {
2077                 ret = vgic_vcpu_init_maps(vcpu, nr_irqs);
2078                 if (ret) {
2079                         kvm_err("VGIC: Failed to allocate vcpu memory\n");
2080                         break;
2081                 }
2082
2083                 for (i = 0; i < dist->nr_irqs; i++) {
2084                         if (i < VGIC_NR_PPIS)
2085                                 vgic_bitmap_set_irq_val(&dist->irq_enabled,
2086                                                         vcpu->vcpu_id, i, 1);
2087                         if (i < VGIC_NR_PRIVATE_IRQS)
2088                                 vgic_bitmap_set_irq_val(&dist->irq_cfg,
2089                                                         vcpu->vcpu_id, i,
2090                                                         VGIC_CFG_EDGE);
2091                 }
2092
2093                 vgic_enable(vcpu);
2094         }
2095
2096 out:
2097         if (ret)
2098                 kvm_vgic_destroy(kvm);
2099
2100         return ret;
2101 }
2102
2103 static int init_vgic_model(struct kvm *kvm, int type)
2104 {
2105         switch (type) {
2106         case KVM_DEV_TYPE_ARM_VGIC_V2:
2107                 vgic_v2_init_emulation(kvm);
2108                 break;
2109 #ifdef CONFIG_ARM_GIC_V3
2110         case KVM_DEV_TYPE_ARM_VGIC_V3:
2111                 vgic_v3_init_emulation(kvm);
2112                 break;
2113 #endif
2114         default:
2115                 return -ENODEV;
2116         }
2117
2118         if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus)
2119                 return -E2BIG;
2120
2121         return 0;
2122 }
2123
2124 /**
2125  * kvm_vgic_early_init - Earliest possible vgic initialization stage
2126  *
2127  * No memory allocation should be performed here, only static init.
2128  */
2129 void kvm_vgic_early_init(struct kvm *kvm)
2130 {
2131         spin_lock_init(&kvm->arch.vgic.lock);
2132         spin_lock_init(&kvm->arch.vgic.irq_phys_map_lock);
2133         INIT_LIST_HEAD(&kvm->arch.vgic.irq_phys_map_list);
2134 }
2135
2136 int kvm_vgic_create(struct kvm *kvm, u32 type)
2137 {
2138         int i, vcpu_lock_idx = -1, ret;
2139         struct kvm_vcpu *vcpu;
2140
2141         mutex_lock(&kvm->lock);
2142
2143         if (irqchip_in_kernel(kvm)) {
2144                 ret = -EEXIST;
2145                 goto out;
2146         }
2147
2148         /*
2149          * This function is also called by the KVM_CREATE_IRQCHIP handler,
2150          * which had no chance yet to check the availability of the GICv2
2151          * emulation. So check this here again. KVM_CREATE_DEVICE does
2152          * the proper checks already.
2153          */
2154         if (type == KVM_DEV_TYPE_ARM_VGIC_V2 && !vgic->can_emulate_gicv2) {
2155                 ret = -ENODEV;
2156                 goto out;
2157         }
2158
2159         /*
2160          * Any time a vcpu is run, vcpu_load is called which tries to grab the
2161          * vcpu->mutex.  By grabbing the vcpu->mutex of all VCPUs we ensure
2162          * that no other VCPUs are run while we create the vgic.
2163          */
2164         ret = -EBUSY;
2165         kvm_for_each_vcpu(i, vcpu, kvm) {
2166                 if (!mutex_trylock(&vcpu->mutex))
2167                         goto out_unlock;
2168                 vcpu_lock_idx = i;
2169         }
2170
2171         kvm_for_each_vcpu(i, vcpu, kvm) {
2172                 if (vcpu->arch.has_run_once)
2173                         goto out_unlock;
2174         }
2175         ret = 0;
2176
2177         ret = init_vgic_model(kvm, type);
2178         if (ret)
2179                 goto out_unlock;
2180
2181         kvm->arch.vgic.in_kernel = true;
2182         kvm->arch.vgic.vgic_model = type;
2183         kvm->arch.vgic.vctrl_base = vgic->vctrl_base;
2184         kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
2185         kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
2186         kvm->arch.vgic.vgic_redist_base = VGIC_ADDR_UNDEF;
2187
2188 out_unlock:
2189         for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
2190                 vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
2191                 mutex_unlock(&vcpu->mutex);
2192         }
2193
2194 out:
2195         mutex_unlock(&kvm->lock);
2196         return ret;
2197 }
2198
2199 static int vgic_ioaddr_overlap(struct kvm *kvm)
2200 {
2201         phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
2202         phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
2203
2204         if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
2205                 return 0;
2206         if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
2207             (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
2208                 return -EBUSY;
2209         return 0;
2210 }
2211
2212 static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
2213                               phys_addr_t addr, phys_addr_t size)
2214 {
2215         int ret;
2216
2217         if (addr & ~KVM_PHYS_MASK)
2218                 return -E2BIG;
2219
2220         if (addr & (SZ_4K - 1))
2221                 return -EINVAL;
2222
2223         if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
2224                 return -EEXIST;
2225         if (addr + size < addr)
2226                 return -EINVAL;
2227
2228         *ioaddr = addr;
2229         ret = vgic_ioaddr_overlap(kvm);
2230         if (ret)
2231                 *ioaddr = VGIC_ADDR_UNDEF;
2232
2233         return ret;
2234 }
2235
2236 /**
2237  * kvm_vgic_addr - set or get vgic VM base addresses
2238  * @kvm:   pointer to the vm struct
2239  * @type:  the VGIC addr type, one of KVM_VGIC_V[23]_ADDR_TYPE_XXX
2240  * @addr:  pointer to address value
2241  * @write: if true set the address in the VM address space, if false read the
2242  *          address
2243  *
2244  * Set or get the vgic base addresses for the distributor and the virtual CPU
2245  * interface in the VM physical address space.  These addresses are properties
2246  * of the emulated core/SoC and therefore user space initially knows this
2247  * information.
2248  */
2249 int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
2250 {
2251         int r = 0;
2252         struct vgic_dist *vgic = &kvm->arch.vgic;
2253         int type_needed;
2254         phys_addr_t *addr_ptr, block_size;
2255         phys_addr_t alignment;
2256
2257         mutex_lock(&kvm->lock);
2258         switch (type) {
2259         case KVM_VGIC_V2_ADDR_TYPE_DIST:
2260                 type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
2261                 addr_ptr = &vgic->vgic_dist_base;
2262                 block_size = KVM_VGIC_V2_DIST_SIZE;
2263                 alignment = SZ_4K;
2264                 break;
2265         case KVM_VGIC_V2_ADDR_TYPE_CPU:
2266                 type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
2267                 addr_ptr = &vgic->vgic_cpu_base;
2268                 block_size = KVM_VGIC_V2_CPU_SIZE;
2269                 alignment = SZ_4K;
2270                 break;
2271 #ifdef CONFIG_ARM_GIC_V3
2272         case KVM_VGIC_V3_ADDR_TYPE_DIST:
2273                 type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
2274                 addr_ptr = &vgic->vgic_dist_base;
2275                 block_size = KVM_VGIC_V3_DIST_SIZE;
2276                 alignment = SZ_64K;
2277                 break;
2278         case KVM_VGIC_V3_ADDR_TYPE_REDIST:
2279                 type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
2280                 addr_ptr = &vgic->vgic_redist_base;
2281                 block_size = KVM_VGIC_V3_REDIST_SIZE;
2282                 alignment = SZ_64K;
2283                 break;
2284 #endif
2285         default:
2286                 r = -ENODEV;
2287                 goto out;
2288         }
2289
2290         if (vgic->vgic_model != type_needed) {
2291                 r = -ENODEV;
2292                 goto out;
2293         }
2294
2295         if (write) {
2296                 if (!IS_ALIGNED(*addr, alignment))
2297                         r = -EINVAL;
2298                 else
2299                         r = vgic_ioaddr_assign(kvm, addr_ptr, *addr,
2300                                                block_size);
2301         } else {
2302                 *addr = *addr_ptr;
2303         }
2304
2305 out:
2306         mutex_unlock(&kvm->lock);
2307         return r;
2308 }
2309
2310 int vgic_set_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2311 {
2312         int r;
2313
2314         switch (attr->group) {
2315         case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2316                 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2317                 u64 addr;
2318                 unsigned long type = (unsigned long)attr->attr;
2319
2320                 if (copy_from_user(&addr, uaddr, sizeof(addr)))
2321                         return -EFAULT;
2322
2323                 r = kvm_vgic_addr(dev->kvm, type, &addr, true);
2324                 return (r == -ENODEV) ? -ENXIO : r;
2325         }
2326         case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2327                 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2328                 u32 val;
2329                 int ret = 0;
2330
2331                 if (get_user(val, uaddr))
2332                         return -EFAULT;
2333
2334                 /*
2335                  * We require:
2336                  * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs
2337                  * - at most 1024 interrupts
2338                  * - a multiple of 32 interrupts
2339                  */
2340                 if (val < (VGIC_NR_PRIVATE_IRQS + 32) ||
2341                     val > VGIC_MAX_IRQS ||
2342                     (val & 31))
2343                         return -EINVAL;
2344
2345                 mutex_lock(&dev->kvm->lock);
2346
2347                 if (vgic_ready(dev->kvm) || dev->kvm->arch.vgic.nr_irqs)
2348                         ret = -EBUSY;
2349                 else
2350                         dev->kvm->arch.vgic.nr_irqs = val;
2351
2352                 mutex_unlock(&dev->kvm->lock);
2353
2354                 return ret;
2355         }
2356         case KVM_DEV_ARM_VGIC_GRP_CTRL: {
2357                 switch (attr->attr) {
2358                 case KVM_DEV_ARM_VGIC_CTRL_INIT:
2359                         r = vgic_init(dev->kvm);
2360                         return r;
2361                 }
2362                 break;
2363         }
2364         }
2365
2366         return -ENXIO;
2367 }
2368
2369 int vgic_get_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2370 {
2371         int r = -ENXIO;
2372
2373         switch (attr->group) {
2374         case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2375                 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2376                 u64 addr;
2377                 unsigned long type = (unsigned long)attr->attr;
2378
2379                 r = kvm_vgic_addr(dev->kvm, type, &addr, false);
2380                 if (r)
2381                         return (r == -ENODEV) ? -ENXIO : r;
2382
2383                 if (copy_to_user(uaddr, &addr, sizeof(addr)))
2384                         return -EFAULT;
2385                 break;
2386         }
2387         case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2388                 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2389
2390                 r = put_user(dev->kvm->arch.vgic.nr_irqs, uaddr);
2391                 break;
2392         }
2393
2394         }
2395
2396         return r;
2397 }
2398
2399 int vgic_has_attr_regs(const struct vgic_io_range *ranges, phys_addr_t offset)
2400 {
2401         if (vgic_find_range(ranges, 4, offset))
2402                 return 0;
2403         else
2404                 return -ENXIO;
2405 }
2406
2407 static void vgic_init_maintenance_interrupt(void *info)
2408 {
2409         enable_percpu_irq(vgic->maint_irq, 0);
2410 }
2411
2412 static int vgic_cpu_notify(struct notifier_block *self,
2413                            unsigned long action, void *cpu)
2414 {
2415         switch (action) {
2416         case CPU_STARTING:
2417         case CPU_STARTING_FROZEN:
2418                 vgic_init_maintenance_interrupt(NULL);
2419                 break;
2420         case CPU_DYING:
2421         case CPU_DYING_FROZEN:
2422                 disable_percpu_irq(vgic->maint_irq);
2423                 break;
2424         }
2425
2426         return NOTIFY_OK;
2427 }
2428
2429 static struct notifier_block vgic_cpu_nb = {
2430         .notifier_call = vgic_cpu_notify,
2431 };
2432
2433 static const struct of_device_id vgic_ids[] = {
2434         { .compatible = "arm,cortex-a15-gic",   .data = vgic_v2_probe, },
2435         { .compatible = "arm,cortex-a7-gic",    .data = vgic_v2_probe, },
2436         { .compatible = "arm,gic-400",          .data = vgic_v2_probe, },
2437         { .compatible = "arm,gic-v3",           .data = vgic_v3_probe, },
2438         {},
2439 };
2440
2441 int kvm_vgic_hyp_init(void)
2442 {
2443         const struct of_device_id *matched_id;
2444         const int (*vgic_probe)(struct device_node *,const struct vgic_ops **,
2445                                 const struct vgic_params **);
2446         struct device_node *vgic_node;
2447         int ret;
2448
2449         vgic_node = of_find_matching_node_and_match(NULL,
2450                                                     vgic_ids, &matched_id);
2451         if (!vgic_node) {
2452                 kvm_err("error: no compatible GIC node found\n");
2453                 return -ENODEV;
2454         }
2455
2456         vgic_probe = matched_id->data;
2457         ret = vgic_probe(vgic_node, &vgic_ops, &vgic);
2458         if (ret)
2459                 return ret;
2460
2461         ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler,
2462                                  "vgic", kvm_get_running_vcpus());
2463         if (ret) {
2464                 kvm_err("Cannot register interrupt %d\n", vgic->maint_irq);
2465                 return ret;
2466         }
2467
2468         ret = __register_cpu_notifier(&vgic_cpu_nb);
2469         if (ret) {
2470                 kvm_err("Cannot register vgic CPU notifier\n");
2471                 goto out_free_irq;
2472         }
2473
2474         on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
2475
2476         return 0;
2477
2478 out_free_irq:
2479         free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus());
2480         return ret;
2481 }
2482
2483 int kvm_irq_map_gsi(struct kvm *kvm,
2484                     struct kvm_kernel_irq_routing_entry *entries,
2485                     int gsi)
2486 {
2487         return 0;
2488 }
2489
2490 int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin)
2491 {
2492         return pin;
2493 }
2494
2495 int kvm_set_irq(struct kvm *kvm, int irq_source_id,
2496                 u32 irq, int level, bool line_status)
2497 {
2498         unsigned int spi = irq + VGIC_NR_PRIVATE_IRQS;
2499
2500         trace_kvm_set_irq(irq, level, irq_source_id);
2501
2502         BUG_ON(!vgic_initialized(kvm));
2503
2504         return kvm_vgic_inject_irq(kvm, 0, spi, level);
2505 }
2506
2507 /* MSI not implemented yet */
2508 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
2509                 struct kvm *kvm, int irq_source_id,
2510                 int level, bool line_status)
2511 {
2512         return 0;
2513 }