arm/arm64: KVM: Use appropriate define in VGIC reset code
[deliverable/linux.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 static int compute_pending_for_cpu(struct kvm_vcpu *vcpu);
111
112 static const struct vgic_ops *vgic_ops;
113 static const struct vgic_params *vgic;
114
115 static void add_sgi_source(struct kvm_vcpu *vcpu, int irq, int source)
116 {
117 vcpu->kvm->arch.vgic.vm_ops.add_sgi_source(vcpu, irq, source);
118 }
119
120 static bool queue_sgi(struct kvm_vcpu *vcpu, int irq)
121 {
122 return vcpu->kvm->arch.vgic.vm_ops.queue_sgi(vcpu, irq);
123 }
124
125 int kvm_vgic_map_resources(struct kvm *kvm)
126 {
127 return kvm->arch.vgic.vm_ops.map_resources(kvm, vgic);
128 }
129
130 /*
131 * struct vgic_bitmap contains a bitmap made of unsigned longs, but
132 * extracts u32s out of them.
133 *
134 * This does not work on 64-bit BE systems, because the bitmap access
135 * will store two consecutive 32-bit words with the higher-addressed
136 * register's bits at the lower index and the lower-addressed register's
137 * bits at the higher index.
138 *
139 * Therefore, swizzle the register index when accessing the 32-bit word
140 * registers to access the right register's value.
141 */
142 #if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 64
143 #define REG_OFFSET_SWIZZLE 1
144 #else
145 #define REG_OFFSET_SWIZZLE 0
146 #endif
147
148 static int vgic_init_bitmap(struct vgic_bitmap *b, int nr_cpus, int nr_irqs)
149 {
150 int nr_longs;
151
152 nr_longs = nr_cpus + BITS_TO_LONGS(nr_irqs - VGIC_NR_PRIVATE_IRQS);
153
154 b->private = kzalloc(sizeof(unsigned long) * nr_longs, GFP_KERNEL);
155 if (!b->private)
156 return -ENOMEM;
157
158 b->shared = b->private + nr_cpus;
159
160 return 0;
161 }
162
163 static void vgic_free_bitmap(struct vgic_bitmap *b)
164 {
165 kfree(b->private);
166 b->private = NULL;
167 b->shared = NULL;
168 }
169
170 /*
171 * Call this function to convert a u64 value to an unsigned long * bitmask
172 * in a way that works on both 32-bit and 64-bit LE and BE platforms.
173 *
174 * Warning: Calling this function may modify *val.
175 */
176 static unsigned long *u64_to_bitmask(u64 *val)
177 {
178 #if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 32
179 *val = (*val >> 32) | (*val << 32);
180 #endif
181 return (unsigned long *)val;
182 }
183
184 u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x, int cpuid, u32 offset)
185 {
186 offset >>= 2;
187 if (!offset)
188 return (u32 *)(x->private + cpuid) + REG_OFFSET_SWIZZLE;
189 else
190 return (u32 *)(x->shared) + ((offset - 1) ^ REG_OFFSET_SWIZZLE);
191 }
192
193 static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
194 int cpuid, int irq)
195 {
196 if (irq < VGIC_NR_PRIVATE_IRQS)
197 return test_bit(irq, x->private + cpuid);
198
199 return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared);
200 }
201
202 void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
203 int irq, int val)
204 {
205 unsigned long *reg;
206
207 if (irq < VGIC_NR_PRIVATE_IRQS) {
208 reg = x->private + cpuid;
209 } else {
210 reg = x->shared;
211 irq -= VGIC_NR_PRIVATE_IRQS;
212 }
213
214 if (val)
215 set_bit(irq, reg);
216 else
217 clear_bit(irq, reg);
218 }
219
220 static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
221 {
222 return x->private + cpuid;
223 }
224
225 unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
226 {
227 return x->shared;
228 }
229
230 static int vgic_init_bytemap(struct vgic_bytemap *x, int nr_cpus, int nr_irqs)
231 {
232 int size;
233
234 size = nr_cpus * VGIC_NR_PRIVATE_IRQS;
235 size += nr_irqs - VGIC_NR_PRIVATE_IRQS;
236
237 x->private = kzalloc(size, GFP_KERNEL);
238 if (!x->private)
239 return -ENOMEM;
240
241 x->shared = x->private + nr_cpus * VGIC_NR_PRIVATE_IRQS / sizeof(u32);
242 return 0;
243 }
244
245 static void vgic_free_bytemap(struct vgic_bytemap *b)
246 {
247 kfree(b->private);
248 b->private = NULL;
249 b->shared = NULL;
250 }
251
252 u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
253 {
254 u32 *reg;
255
256 if (offset < VGIC_NR_PRIVATE_IRQS) {
257 reg = x->private;
258 offset += cpuid * VGIC_NR_PRIVATE_IRQS;
259 } else {
260 reg = x->shared;
261 offset -= VGIC_NR_PRIVATE_IRQS;
262 }
263
264 return reg + (offset / sizeof(u32));
265 }
266
267 #define VGIC_CFG_LEVEL 0
268 #define VGIC_CFG_EDGE 1
269
270 static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq)
271 {
272 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
273 int irq_val;
274
275 irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq);
276 return irq_val == VGIC_CFG_EDGE;
277 }
278
279 static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq)
280 {
281 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
282
283 return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq);
284 }
285
286 static int vgic_irq_is_queued(struct kvm_vcpu *vcpu, int irq)
287 {
288 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
289
290 return vgic_bitmap_get_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq);
291 }
292
293 static int vgic_irq_is_active(struct kvm_vcpu *vcpu, int irq)
294 {
295 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
296
297 return vgic_bitmap_get_irq_val(&dist->irq_active, vcpu->vcpu_id, irq);
298 }
299
300 static void vgic_irq_set_queued(struct kvm_vcpu *vcpu, int irq)
301 {
302 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
303
304 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 1);
305 }
306
307 static void vgic_irq_clear_queued(struct kvm_vcpu *vcpu, int irq)
308 {
309 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
310
311 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 0);
312 }
313
314 static void vgic_irq_set_active(struct kvm_vcpu *vcpu, int irq)
315 {
316 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
317
318 vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 1);
319 }
320
321 static void vgic_irq_clear_active(struct kvm_vcpu *vcpu, int irq)
322 {
323 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
324
325 vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 0);
326 }
327
328 static int vgic_dist_irq_get_level(struct kvm_vcpu *vcpu, int irq)
329 {
330 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
331
332 return vgic_bitmap_get_irq_val(&dist->irq_level, vcpu->vcpu_id, irq);
333 }
334
335 static void vgic_dist_irq_set_level(struct kvm_vcpu *vcpu, int irq)
336 {
337 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
338
339 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 1);
340 }
341
342 static void vgic_dist_irq_clear_level(struct kvm_vcpu *vcpu, int irq)
343 {
344 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
345
346 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 0);
347 }
348
349 static int vgic_dist_irq_soft_pend(struct kvm_vcpu *vcpu, int irq)
350 {
351 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
352
353 return vgic_bitmap_get_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq);
354 }
355
356 static void vgic_dist_irq_clear_soft_pend(struct kvm_vcpu *vcpu, int irq)
357 {
358 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
359
360 vgic_bitmap_set_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq, 0);
361 if (!vgic_dist_irq_get_level(vcpu, irq)) {
362 vgic_dist_irq_clear_pending(vcpu, irq);
363 if (!compute_pending_for_cpu(vcpu))
364 clear_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
365 }
366 }
367
368 static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
369 {
370 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
371
372 return vgic_bitmap_get_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq);
373 }
374
375 void vgic_dist_irq_set_pending(struct kvm_vcpu *vcpu, int irq)
376 {
377 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
378
379 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 1);
380 }
381
382 void vgic_dist_irq_clear_pending(struct kvm_vcpu *vcpu, int irq)
383 {
384 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
385
386 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 0);
387 }
388
389 static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq)
390 {
391 if (irq < VGIC_NR_PRIVATE_IRQS)
392 set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
393 else
394 set_bit(irq - VGIC_NR_PRIVATE_IRQS,
395 vcpu->arch.vgic_cpu.pending_shared);
396 }
397
398 void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
399 {
400 if (irq < VGIC_NR_PRIVATE_IRQS)
401 clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
402 else
403 clear_bit(irq - VGIC_NR_PRIVATE_IRQS,
404 vcpu->arch.vgic_cpu.pending_shared);
405 }
406
407 static bool vgic_can_sample_irq(struct kvm_vcpu *vcpu, int irq)
408 {
409 return !vgic_irq_is_queued(vcpu, irq);
410 }
411
412 /**
413 * vgic_reg_access - access vgic register
414 * @mmio: pointer to the data describing the mmio access
415 * @reg: pointer to the virtual backing of vgic distributor data
416 * @offset: least significant 2 bits used for word offset
417 * @mode: ACCESS_ mode (see defines above)
418 *
419 * Helper to make vgic register access easier using one of the access
420 * modes defined for vgic register access
421 * (read,raz,write-ignored,setbit,clearbit,write)
422 */
423 void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
424 phys_addr_t offset, int mode)
425 {
426 int word_offset = (offset & 3) * 8;
427 u32 mask = (1UL << (mmio->len * 8)) - 1;
428 u32 regval;
429
430 /*
431 * Any alignment fault should have been delivered to the guest
432 * directly (ARM ARM B3.12.7 "Prioritization of aborts").
433 */
434
435 if (reg) {
436 regval = *reg;
437 } else {
438 BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED));
439 regval = 0;
440 }
441
442 if (mmio->is_write) {
443 u32 data = mmio_data_read(mmio, mask) << word_offset;
444 switch (ACCESS_WRITE_MASK(mode)) {
445 case ACCESS_WRITE_IGNORED:
446 return;
447
448 case ACCESS_WRITE_SETBIT:
449 regval |= data;
450 break;
451
452 case ACCESS_WRITE_CLEARBIT:
453 regval &= ~data;
454 break;
455
456 case ACCESS_WRITE_VALUE:
457 regval = (regval & ~(mask << word_offset)) | data;
458 break;
459 }
460 *reg = regval;
461 } else {
462 switch (ACCESS_READ_MASK(mode)) {
463 case ACCESS_READ_RAZ:
464 regval = 0;
465 /* fall through */
466
467 case ACCESS_READ_VALUE:
468 mmio_data_write(mmio, mask, regval >> word_offset);
469 }
470 }
471 }
472
473 bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
474 phys_addr_t offset)
475 {
476 vgic_reg_access(mmio, NULL, offset,
477 ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
478 return false;
479 }
480
481 bool vgic_handle_enable_reg(struct kvm *kvm, struct kvm_exit_mmio *mmio,
482 phys_addr_t offset, int vcpu_id, int access)
483 {
484 u32 *reg;
485 int mode = ACCESS_READ_VALUE | access;
486 struct kvm_vcpu *target_vcpu = kvm_get_vcpu(kvm, vcpu_id);
487
488 reg = vgic_bitmap_get_reg(&kvm->arch.vgic.irq_enabled, vcpu_id, offset);
489 vgic_reg_access(mmio, reg, offset, mode);
490 if (mmio->is_write) {
491 if (access & ACCESS_WRITE_CLEARBIT) {
492 if (offset < 4) /* Force SGI enabled */
493 *reg |= 0xffff;
494 vgic_retire_disabled_irqs(target_vcpu);
495 }
496 vgic_update_state(kvm);
497 return true;
498 }
499
500 return false;
501 }
502
503 bool vgic_handle_set_pending_reg(struct kvm *kvm,
504 struct kvm_exit_mmio *mmio,
505 phys_addr_t offset, int vcpu_id)
506 {
507 u32 *reg, orig;
508 u32 level_mask;
509 int mode = ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT;
510 struct vgic_dist *dist = &kvm->arch.vgic;
511
512 reg = vgic_bitmap_get_reg(&dist->irq_cfg, vcpu_id, offset);
513 level_mask = (~(*reg));
514
515 /* Mark both level and edge triggered irqs as pending */
516 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
517 orig = *reg;
518 vgic_reg_access(mmio, reg, offset, mode);
519
520 if (mmio->is_write) {
521 /* Set the soft-pending flag only for level-triggered irqs */
522 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
523 vcpu_id, offset);
524 vgic_reg_access(mmio, reg, offset, mode);
525 *reg &= level_mask;
526
527 /* Ignore writes to SGIs */
528 if (offset < 2) {
529 *reg &= ~0xffff;
530 *reg |= orig & 0xffff;
531 }
532
533 vgic_update_state(kvm);
534 return true;
535 }
536
537 return false;
538 }
539
540 /*
541 * If a mapped interrupt's state has been modified by the guest such that it
542 * is no longer active or pending, without it have gone through the sync path,
543 * then the map->active field must be cleared so the interrupt can be taken
544 * again.
545 */
546 static void vgic_handle_clear_mapped_irq(struct kvm_vcpu *vcpu)
547 {
548 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
549 struct list_head *root;
550 struct irq_phys_map_entry *entry;
551 struct irq_phys_map *map;
552
553 rcu_read_lock();
554
555 /* Check for PPIs */
556 root = &vgic_cpu->irq_phys_map_list;
557 list_for_each_entry_rcu(entry, root, entry) {
558 map = &entry->map;
559
560 if (!vgic_dist_irq_is_pending(vcpu, map->virt_irq) &&
561 !vgic_irq_is_active(vcpu, map->virt_irq))
562 map->active = false;
563 }
564
565 rcu_read_unlock();
566 }
567
568 bool vgic_handle_clear_pending_reg(struct kvm *kvm,
569 struct kvm_exit_mmio *mmio,
570 phys_addr_t offset, int vcpu_id)
571 {
572 u32 *level_active;
573 u32 *reg, orig;
574 int mode = ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT;
575 struct vgic_dist *dist = &kvm->arch.vgic;
576
577 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
578 orig = *reg;
579 vgic_reg_access(mmio, reg, offset, mode);
580 if (mmio->is_write) {
581 /* Re-set level triggered level-active interrupts */
582 level_active = vgic_bitmap_get_reg(&dist->irq_level,
583 vcpu_id, offset);
584 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
585 *reg |= *level_active;
586
587 /* Ignore writes to SGIs */
588 if (offset < 2) {
589 *reg &= ~0xffff;
590 *reg |= orig & 0xffff;
591 }
592
593 /* Clear soft-pending flags */
594 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
595 vcpu_id, offset);
596 vgic_reg_access(mmio, reg, offset, mode);
597
598 vgic_handle_clear_mapped_irq(kvm_get_vcpu(kvm, vcpu_id));
599 vgic_update_state(kvm);
600 return true;
601 }
602 return false;
603 }
604
605 bool vgic_handle_set_active_reg(struct kvm *kvm,
606 struct kvm_exit_mmio *mmio,
607 phys_addr_t offset, int vcpu_id)
608 {
609 u32 *reg;
610 struct vgic_dist *dist = &kvm->arch.vgic;
611
612 reg = vgic_bitmap_get_reg(&dist->irq_active, vcpu_id, offset);
613 vgic_reg_access(mmio, reg, offset,
614 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
615
616 if (mmio->is_write) {
617 vgic_update_state(kvm);
618 return true;
619 }
620
621 return false;
622 }
623
624 bool vgic_handle_clear_active_reg(struct kvm *kvm,
625 struct kvm_exit_mmio *mmio,
626 phys_addr_t offset, int vcpu_id)
627 {
628 u32 *reg;
629 struct vgic_dist *dist = &kvm->arch.vgic;
630
631 reg = vgic_bitmap_get_reg(&dist->irq_active, vcpu_id, offset);
632 vgic_reg_access(mmio, reg, offset,
633 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
634
635 if (mmio->is_write) {
636 vgic_handle_clear_mapped_irq(kvm_get_vcpu(kvm, vcpu_id));
637 vgic_update_state(kvm);
638 return true;
639 }
640
641 return false;
642 }
643
644 static u32 vgic_cfg_expand(u16 val)
645 {
646 u32 res = 0;
647 int i;
648
649 /*
650 * Turn a 16bit value like abcd...mnop into a 32bit word
651 * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
652 */
653 for (i = 0; i < 16; i++)
654 res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
655
656 return res;
657 }
658
659 static u16 vgic_cfg_compress(u32 val)
660 {
661 u16 res = 0;
662 int i;
663
664 /*
665 * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
666 * abcd...mnop which is what we really care about.
667 */
668 for (i = 0; i < 16; i++)
669 res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
670
671 return res;
672 }
673
674 /*
675 * The distributor uses 2 bits per IRQ for the CFG register, but the
676 * LSB is always 0. As such, we only keep the upper bit, and use the
677 * two above functions to compress/expand the bits
678 */
679 bool vgic_handle_cfg_reg(u32 *reg, struct kvm_exit_mmio *mmio,
680 phys_addr_t offset)
681 {
682 u32 val;
683
684 if (offset & 4)
685 val = *reg >> 16;
686 else
687 val = *reg & 0xffff;
688
689 val = vgic_cfg_expand(val);
690 vgic_reg_access(mmio, &val, offset,
691 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
692 if (mmio->is_write) {
693 /* Ignore writes to read-only SGI and PPI bits */
694 if (offset < 8)
695 return false;
696
697 val = vgic_cfg_compress(val);
698 if (offset & 4) {
699 *reg &= 0xffff;
700 *reg |= val << 16;
701 } else {
702 *reg &= 0xffff << 16;
703 *reg |= val;
704 }
705 }
706
707 return false;
708 }
709
710 /**
711 * vgic_unqueue_irqs - move pending/active IRQs from LRs to the distributor
712 * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs
713 *
714 * Move any IRQs that have already been assigned to LRs back to the
715 * emulated distributor state so that the complete emulated state can be read
716 * from the main emulation structures without investigating the LRs.
717 */
718 void vgic_unqueue_irqs(struct kvm_vcpu *vcpu)
719 {
720 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
721 int i;
722
723 for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
724 struct vgic_lr lr = vgic_get_lr(vcpu, i);
725
726 /*
727 * There are three options for the state bits:
728 *
729 * 01: pending
730 * 10: active
731 * 11: pending and active
732 */
733 BUG_ON(!(lr.state & LR_STATE_MASK));
734
735 /* Reestablish SGI source for pending and active IRQs */
736 if (lr.irq < VGIC_NR_SGIS)
737 add_sgi_source(vcpu, lr.irq, lr.source);
738
739 /*
740 * If the LR holds an active (10) or a pending and active (11)
741 * interrupt then move the active state to the
742 * distributor tracking bit.
743 */
744 if (lr.state & LR_STATE_ACTIVE) {
745 vgic_irq_set_active(vcpu, lr.irq);
746 lr.state &= ~LR_STATE_ACTIVE;
747 }
748
749 /*
750 * Reestablish the pending state on the distributor and the
751 * CPU interface. It may have already been pending, but that
752 * is fine, then we are only setting a few bits that were
753 * already set.
754 */
755 if (lr.state & LR_STATE_PENDING) {
756 vgic_dist_irq_set_pending(vcpu, lr.irq);
757 lr.state &= ~LR_STATE_PENDING;
758 }
759
760 vgic_set_lr(vcpu, i, lr);
761
762 /*
763 * Mark the LR as free for other use.
764 */
765 BUG_ON(lr.state & LR_STATE_MASK);
766 vgic_retire_lr(i, lr.irq, vcpu);
767 vgic_irq_clear_queued(vcpu, lr.irq);
768
769 /* Finally update the VGIC state. */
770 vgic_update_state(vcpu->kvm);
771 }
772 }
773
774 const
775 struct vgic_io_range *vgic_find_range(const struct vgic_io_range *ranges,
776 int len, gpa_t offset)
777 {
778 while (ranges->len) {
779 if (offset >= ranges->base &&
780 (offset + len) <= (ranges->base + ranges->len))
781 return ranges;
782 ranges++;
783 }
784
785 return NULL;
786 }
787
788 static bool vgic_validate_access(const struct vgic_dist *dist,
789 const struct vgic_io_range *range,
790 unsigned long offset)
791 {
792 int irq;
793
794 if (!range->bits_per_irq)
795 return true; /* Not an irq-based access */
796
797 irq = offset * 8 / range->bits_per_irq;
798 if (irq >= dist->nr_irqs)
799 return false;
800
801 return true;
802 }
803
804 /*
805 * Call the respective handler function for the given range.
806 * We split up any 64 bit accesses into two consecutive 32 bit
807 * handler calls and merge the result afterwards.
808 * We do this in a little endian fashion regardless of the host's
809 * or guest's endianness, because the GIC is always LE and the rest of
810 * the code (vgic_reg_access) also puts it in a LE fashion already.
811 * At this point we have already identified the handle function, so
812 * range points to that one entry and offset is relative to this.
813 */
814 static bool call_range_handler(struct kvm_vcpu *vcpu,
815 struct kvm_exit_mmio *mmio,
816 unsigned long offset,
817 const struct vgic_io_range *range)
818 {
819 struct kvm_exit_mmio mmio32;
820 bool ret;
821
822 if (likely(mmio->len <= 4))
823 return range->handle_mmio(vcpu, mmio, offset);
824
825 /*
826 * Any access bigger than 4 bytes (that we currently handle in KVM)
827 * is actually 8 bytes long, caused by a 64-bit access
828 */
829
830 mmio32.len = 4;
831 mmio32.is_write = mmio->is_write;
832 mmio32.private = mmio->private;
833
834 mmio32.phys_addr = mmio->phys_addr + 4;
835 mmio32.data = &((u32 *)mmio->data)[1];
836 ret = range->handle_mmio(vcpu, &mmio32, offset + 4);
837
838 mmio32.phys_addr = mmio->phys_addr;
839 mmio32.data = &((u32 *)mmio->data)[0];
840 ret |= range->handle_mmio(vcpu, &mmio32, offset);
841
842 return ret;
843 }
844
845 /**
846 * vgic_handle_mmio_access - handle an in-kernel MMIO access
847 * This is called by the read/write KVM IO device wrappers below.
848 * @vcpu: pointer to the vcpu performing the access
849 * @this: pointer to the KVM IO device in charge
850 * @addr: guest physical address of the access
851 * @len: size of the access
852 * @val: pointer to the data region
853 * @is_write: read or write access
854 *
855 * returns true if the MMIO access could be performed
856 */
857 static int vgic_handle_mmio_access(struct kvm_vcpu *vcpu,
858 struct kvm_io_device *this, gpa_t addr,
859 int len, void *val, bool is_write)
860 {
861 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
862 struct vgic_io_device *iodev = container_of(this,
863 struct vgic_io_device, dev);
864 struct kvm_run *run = vcpu->run;
865 const struct vgic_io_range *range;
866 struct kvm_exit_mmio mmio;
867 bool updated_state;
868 gpa_t offset;
869
870 offset = addr - iodev->addr;
871 range = vgic_find_range(iodev->reg_ranges, len, offset);
872 if (unlikely(!range || !range->handle_mmio)) {
873 pr_warn("Unhandled access %d %08llx %d\n", is_write, addr, len);
874 return -ENXIO;
875 }
876
877 mmio.phys_addr = addr;
878 mmio.len = len;
879 mmio.is_write = is_write;
880 mmio.data = val;
881 mmio.private = iodev->redist_vcpu;
882
883 spin_lock(&dist->lock);
884 offset -= range->base;
885 if (vgic_validate_access(dist, range, offset)) {
886 updated_state = call_range_handler(vcpu, &mmio, offset, range);
887 } else {
888 if (!is_write)
889 memset(val, 0, len);
890 updated_state = false;
891 }
892 spin_unlock(&dist->lock);
893 run->mmio.is_write = is_write;
894 run->mmio.len = len;
895 run->mmio.phys_addr = addr;
896 memcpy(run->mmio.data, val, len);
897
898 kvm_handle_mmio_return(vcpu, run);
899
900 if (updated_state)
901 vgic_kick_vcpus(vcpu->kvm);
902
903 return 0;
904 }
905
906 static int vgic_handle_mmio_read(struct kvm_vcpu *vcpu,
907 struct kvm_io_device *this,
908 gpa_t addr, int len, void *val)
909 {
910 return vgic_handle_mmio_access(vcpu, this, addr, len, val, false);
911 }
912
913 static int vgic_handle_mmio_write(struct kvm_vcpu *vcpu,
914 struct kvm_io_device *this,
915 gpa_t addr, int len, const void *val)
916 {
917 return vgic_handle_mmio_access(vcpu, this, addr, len, (void *)val,
918 true);
919 }
920
921 struct kvm_io_device_ops vgic_io_ops = {
922 .read = vgic_handle_mmio_read,
923 .write = vgic_handle_mmio_write,
924 };
925
926 /**
927 * vgic_register_kvm_io_dev - register VGIC register frame on the KVM I/O bus
928 * @kvm: The VM structure pointer
929 * @base: The (guest) base address for the register frame
930 * @len: Length of the register frame window
931 * @ranges: Describing the handler functions for each register
932 * @redist_vcpu_id: The VCPU ID to pass on to the handlers on call
933 * @iodev: Points to memory to be passed on to the handler
934 *
935 * @iodev stores the parameters of this function to be usable by the handler
936 * respectively the dispatcher function (since the KVM I/O bus framework lacks
937 * an opaque parameter). Initialization is done in this function, but the
938 * reference should be valid and unique for the whole VGIC lifetime.
939 * If the register frame is not mapped for a specific VCPU, pass -1 to
940 * @redist_vcpu_id.
941 */
942 int vgic_register_kvm_io_dev(struct kvm *kvm, gpa_t base, int len,
943 const struct vgic_io_range *ranges,
944 int redist_vcpu_id,
945 struct vgic_io_device *iodev)
946 {
947 struct kvm_vcpu *vcpu = NULL;
948 int ret;
949
950 if (redist_vcpu_id >= 0)
951 vcpu = kvm_get_vcpu(kvm, redist_vcpu_id);
952
953 iodev->addr = base;
954 iodev->len = len;
955 iodev->reg_ranges = ranges;
956 iodev->redist_vcpu = vcpu;
957
958 kvm_iodevice_init(&iodev->dev, &vgic_io_ops);
959
960 mutex_lock(&kvm->slots_lock);
961
962 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, base, len,
963 &iodev->dev);
964 mutex_unlock(&kvm->slots_lock);
965
966 /* Mark the iodev as invalid if registration fails. */
967 if (ret)
968 iodev->dev.ops = NULL;
969
970 return ret;
971 }
972
973 static int vgic_nr_shared_irqs(struct vgic_dist *dist)
974 {
975 return dist->nr_irqs - VGIC_NR_PRIVATE_IRQS;
976 }
977
978 static int compute_active_for_cpu(struct kvm_vcpu *vcpu)
979 {
980 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
981 unsigned long *active, *enabled, *act_percpu, *act_shared;
982 unsigned long active_private, active_shared;
983 int nr_shared = vgic_nr_shared_irqs(dist);
984 int vcpu_id;
985
986 vcpu_id = vcpu->vcpu_id;
987 act_percpu = vcpu->arch.vgic_cpu.active_percpu;
988 act_shared = vcpu->arch.vgic_cpu.active_shared;
989
990 active = vgic_bitmap_get_cpu_map(&dist->irq_active, vcpu_id);
991 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
992 bitmap_and(act_percpu, active, enabled, VGIC_NR_PRIVATE_IRQS);
993
994 active = vgic_bitmap_get_shared_map(&dist->irq_active);
995 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
996 bitmap_and(act_shared, active, enabled, nr_shared);
997 bitmap_and(act_shared, act_shared,
998 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
999 nr_shared);
1000
1001 active_private = find_first_bit(act_percpu, VGIC_NR_PRIVATE_IRQS);
1002 active_shared = find_first_bit(act_shared, nr_shared);
1003
1004 return (active_private < VGIC_NR_PRIVATE_IRQS ||
1005 active_shared < nr_shared);
1006 }
1007
1008 static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
1009 {
1010 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1011 unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
1012 unsigned long pending_private, pending_shared;
1013 int nr_shared = vgic_nr_shared_irqs(dist);
1014 int vcpu_id;
1015
1016 vcpu_id = vcpu->vcpu_id;
1017 pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
1018 pend_shared = vcpu->arch.vgic_cpu.pending_shared;
1019
1020 if (!dist->enabled) {
1021 bitmap_zero(pend_percpu, VGIC_NR_PRIVATE_IRQS);
1022 bitmap_zero(pend_shared, nr_shared);
1023 return 0;
1024 }
1025
1026 pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id);
1027 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
1028 bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
1029
1030 pending = vgic_bitmap_get_shared_map(&dist->irq_pending);
1031 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
1032 bitmap_and(pend_shared, pending, enabled, nr_shared);
1033 bitmap_and(pend_shared, pend_shared,
1034 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
1035 nr_shared);
1036
1037 pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
1038 pending_shared = find_first_bit(pend_shared, nr_shared);
1039 return (pending_private < VGIC_NR_PRIVATE_IRQS ||
1040 pending_shared < vgic_nr_shared_irqs(dist));
1041 }
1042
1043 /*
1044 * Update the interrupt state and determine which CPUs have pending
1045 * or active interrupts. Must be called with distributor lock held.
1046 */
1047 void vgic_update_state(struct kvm *kvm)
1048 {
1049 struct vgic_dist *dist = &kvm->arch.vgic;
1050 struct kvm_vcpu *vcpu;
1051 int c;
1052
1053 kvm_for_each_vcpu(c, vcpu, kvm) {
1054 if (compute_pending_for_cpu(vcpu))
1055 set_bit(c, dist->irq_pending_on_cpu);
1056
1057 if (compute_active_for_cpu(vcpu))
1058 set_bit(c, dist->irq_active_on_cpu);
1059 else
1060 clear_bit(c, dist->irq_active_on_cpu);
1061 }
1062 }
1063
1064 static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr)
1065 {
1066 return vgic_ops->get_lr(vcpu, lr);
1067 }
1068
1069 static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr,
1070 struct vgic_lr vlr)
1071 {
1072 vgic_ops->set_lr(vcpu, lr, vlr);
1073 }
1074
1075 static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
1076 struct vgic_lr vlr)
1077 {
1078 vgic_ops->sync_lr_elrsr(vcpu, lr, vlr);
1079 }
1080
1081 static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu)
1082 {
1083 return vgic_ops->get_elrsr(vcpu);
1084 }
1085
1086 static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu)
1087 {
1088 return vgic_ops->get_eisr(vcpu);
1089 }
1090
1091 static inline void vgic_clear_eisr(struct kvm_vcpu *vcpu)
1092 {
1093 vgic_ops->clear_eisr(vcpu);
1094 }
1095
1096 static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu)
1097 {
1098 return vgic_ops->get_interrupt_status(vcpu);
1099 }
1100
1101 static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu)
1102 {
1103 vgic_ops->enable_underflow(vcpu);
1104 }
1105
1106 static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu)
1107 {
1108 vgic_ops->disable_underflow(vcpu);
1109 }
1110
1111 void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1112 {
1113 vgic_ops->get_vmcr(vcpu, vmcr);
1114 }
1115
1116 void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1117 {
1118 vgic_ops->set_vmcr(vcpu, vmcr);
1119 }
1120
1121 static inline void vgic_enable(struct kvm_vcpu *vcpu)
1122 {
1123 vgic_ops->enable(vcpu);
1124 }
1125
1126 static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu)
1127 {
1128 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1129 struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr);
1130
1131 /*
1132 * We must transfer the pending state back to the distributor before
1133 * retiring the LR, otherwise we may loose edge-triggered interrupts.
1134 */
1135 if (vlr.state & LR_STATE_PENDING) {
1136 vgic_dist_irq_set_pending(vcpu, irq);
1137 vlr.hwirq = 0;
1138 }
1139
1140 vlr.state = 0;
1141 vgic_set_lr(vcpu, lr_nr, vlr);
1142 clear_bit(lr_nr, vgic_cpu->lr_used);
1143 vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
1144 vgic_sync_lr_elrsr(vcpu, lr_nr, vlr);
1145 }
1146
1147 /*
1148 * An interrupt may have been disabled after being made pending on the
1149 * CPU interface (the classic case is a timer running while we're
1150 * rebooting the guest - the interrupt would kick as soon as the CPU
1151 * interface gets enabled, with deadly consequences).
1152 *
1153 * The solution is to examine already active LRs, and check the
1154 * interrupt is still enabled. If not, just retire it.
1155 */
1156 static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
1157 {
1158 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1159 int lr;
1160
1161 for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) {
1162 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
1163
1164 if (!vgic_irq_is_enabled(vcpu, vlr.irq)) {
1165 vgic_retire_lr(lr, vlr.irq, vcpu);
1166 if (vgic_irq_is_queued(vcpu, vlr.irq))
1167 vgic_irq_clear_queued(vcpu, vlr.irq);
1168 }
1169 }
1170 }
1171
1172 static void vgic_queue_irq_to_lr(struct kvm_vcpu *vcpu, int irq,
1173 int lr_nr, struct vgic_lr vlr)
1174 {
1175 if (vgic_irq_is_active(vcpu, irq)) {
1176 vlr.state |= LR_STATE_ACTIVE;
1177 kvm_debug("Set active, clear distributor: 0x%x\n", vlr.state);
1178 vgic_irq_clear_active(vcpu, irq);
1179 vgic_update_state(vcpu->kvm);
1180 } else {
1181 WARN_ON(!vgic_dist_irq_is_pending(vcpu, irq));
1182 vlr.state |= LR_STATE_PENDING;
1183 kvm_debug("Set pending: 0x%x\n", vlr.state);
1184 }
1185
1186 if (!vgic_irq_is_edge(vcpu, irq))
1187 vlr.state |= LR_EOI_INT;
1188
1189 if (vlr.irq >= VGIC_NR_SGIS) {
1190 struct irq_phys_map *map;
1191 map = vgic_irq_map_search(vcpu, irq);
1192
1193 if (map) {
1194 vlr.hwirq = map->phys_irq;
1195 vlr.state |= LR_HW;
1196 vlr.state &= ~LR_EOI_INT;
1197
1198 /*
1199 * Make sure we're not going to sample this
1200 * again, as a HW-backed interrupt cannot be
1201 * in the PENDING_ACTIVE stage.
1202 */
1203 vgic_irq_set_queued(vcpu, irq);
1204 }
1205 }
1206
1207 vgic_set_lr(vcpu, lr_nr, vlr);
1208 vgic_sync_lr_elrsr(vcpu, lr_nr, vlr);
1209 }
1210
1211 /*
1212 * Queue an interrupt to a CPU virtual interface. Return true on success,
1213 * or false if it wasn't possible to queue it.
1214 * sgi_source must be zero for any non-SGI interrupts.
1215 */
1216 bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
1217 {
1218 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1219 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1220 struct vgic_lr vlr;
1221 int lr;
1222
1223 /* Sanitize the input... */
1224 BUG_ON(sgi_source_id & ~7);
1225 BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
1226 BUG_ON(irq >= dist->nr_irqs);
1227
1228 kvm_debug("Queue IRQ%d\n", irq);
1229
1230 lr = vgic_cpu->vgic_irq_lr_map[irq];
1231
1232 /* Do we have an active interrupt for the same CPUID? */
1233 if (lr != LR_EMPTY) {
1234 vlr = vgic_get_lr(vcpu, lr);
1235 if (vlr.source == sgi_source_id) {
1236 kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq);
1237 BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
1238 vgic_queue_irq_to_lr(vcpu, irq, lr, vlr);
1239 return true;
1240 }
1241 }
1242
1243 /* Try to use another LR for this interrupt */
1244 lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
1245 vgic->nr_lr);
1246 if (lr >= vgic->nr_lr)
1247 return false;
1248
1249 kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
1250 vgic_cpu->vgic_irq_lr_map[irq] = lr;
1251 set_bit(lr, vgic_cpu->lr_used);
1252
1253 vlr.irq = irq;
1254 vlr.source = sgi_source_id;
1255 vlr.state = 0;
1256 vgic_queue_irq_to_lr(vcpu, irq, lr, vlr);
1257
1258 return true;
1259 }
1260
1261 static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
1262 {
1263 if (!vgic_can_sample_irq(vcpu, irq))
1264 return true; /* level interrupt, already queued */
1265
1266 if (vgic_queue_irq(vcpu, 0, irq)) {
1267 if (vgic_irq_is_edge(vcpu, irq)) {
1268 vgic_dist_irq_clear_pending(vcpu, irq);
1269 vgic_cpu_irq_clear(vcpu, irq);
1270 } else {
1271 vgic_irq_set_queued(vcpu, irq);
1272 }
1273
1274 return true;
1275 }
1276
1277 return false;
1278 }
1279
1280 /*
1281 * Fill the list registers with pending interrupts before running the
1282 * guest.
1283 */
1284 static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1285 {
1286 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1287 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1288 unsigned long *pa_percpu, *pa_shared;
1289 int i, vcpu_id;
1290 int overflow = 0;
1291 int nr_shared = vgic_nr_shared_irqs(dist);
1292
1293 vcpu_id = vcpu->vcpu_id;
1294
1295 pa_percpu = vcpu->arch.vgic_cpu.pend_act_percpu;
1296 pa_shared = vcpu->arch.vgic_cpu.pend_act_shared;
1297
1298 bitmap_or(pa_percpu, vgic_cpu->pending_percpu, vgic_cpu->active_percpu,
1299 VGIC_NR_PRIVATE_IRQS);
1300 bitmap_or(pa_shared, vgic_cpu->pending_shared, vgic_cpu->active_shared,
1301 nr_shared);
1302 /*
1303 * We may not have any pending interrupt, or the interrupts
1304 * may have been serviced from another vcpu. In all cases,
1305 * move along.
1306 */
1307 if (!kvm_vgic_vcpu_pending_irq(vcpu) && !kvm_vgic_vcpu_active_irq(vcpu))
1308 goto epilog;
1309
1310 /* SGIs */
1311 for_each_set_bit(i, pa_percpu, VGIC_NR_SGIS) {
1312 if (!queue_sgi(vcpu, i))
1313 overflow = 1;
1314 }
1315
1316 /* PPIs */
1317 for_each_set_bit_from(i, pa_percpu, VGIC_NR_PRIVATE_IRQS) {
1318 if (!vgic_queue_hwirq(vcpu, i))
1319 overflow = 1;
1320 }
1321
1322 /* SPIs */
1323 for_each_set_bit(i, pa_shared, nr_shared) {
1324 if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
1325 overflow = 1;
1326 }
1327
1328
1329
1330
1331 epilog:
1332 if (overflow) {
1333 vgic_enable_underflow(vcpu);
1334 } else {
1335 vgic_disable_underflow(vcpu);
1336 /*
1337 * We're about to run this VCPU, and we've consumed
1338 * everything the distributor had in store for
1339 * us. Claim we don't have anything pending. We'll
1340 * adjust that if needed while exiting.
1341 */
1342 clear_bit(vcpu_id, dist->irq_pending_on_cpu);
1343 }
1344 }
1345
1346 static int process_level_irq(struct kvm_vcpu *vcpu, int lr, struct vgic_lr vlr)
1347 {
1348 int level_pending = 0;
1349
1350 vlr.state = 0;
1351 vlr.hwirq = 0;
1352 vgic_set_lr(vcpu, lr, vlr);
1353
1354 /*
1355 * If the IRQ was EOIed (called from vgic_process_maintenance) or it
1356 * went from active to non-active (called from vgic_sync_hwirq) it was
1357 * also ACKed and we we therefore assume we can clear the soft pending
1358 * state (should it had been set) for this interrupt.
1359 *
1360 * Note: if the IRQ soft pending state was set after the IRQ was
1361 * acked, it actually shouldn't be cleared, but we have no way of
1362 * knowing that unless we start trapping ACKs when the soft-pending
1363 * state is set.
1364 */
1365 vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
1366
1367 /*
1368 * Tell the gic to start sampling the line of this interrupt again.
1369 */
1370 vgic_irq_clear_queued(vcpu, vlr.irq);
1371
1372 /* Any additional pending interrupt? */
1373 if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
1374 vgic_cpu_irq_set(vcpu, vlr.irq);
1375 level_pending = 1;
1376 } else {
1377 vgic_dist_irq_clear_pending(vcpu, vlr.irq);
1378 vgic_cpu_irq_clear(vcpu, vlr.irq);
1379 }
1380
1381 /*
1382 * Despite being EOIed, the LR may not have
1383 * been marked as empty.
1384 */
1385 vgic_sync_lr_elrsr(vcpu, lr, vlr);
1386
1387 return level_pending;
1388 }
1389
1390 static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1391 {
1392 u32 status = vgic_get_interrupt_status(vcpu);
1393 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1394 struct kvm *kvm = vcpu->kvm;
1395 int level_pending = 0;
1396
1397 kvm_debug("STATUS = %08x\n", status);
1398
1399 if (status & INT_STATUS_EOI) {
1400 /*
1401 * Some level interrupts have been EOIed. Clear their
1402 * active bit.
1403 */
1404 u64 eisr = vgic_get_eisr(vcpu);
1405 unsigned long *eisr_ptr = u64_to_bitmask(&eisr);
1406 int lr;
1407
1408 for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
1409 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
1410
1411 WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
1412 WARN_ON(vlr.state & LR_STATE_MASK);
1413
1414
1415 /*
1416 * kvm_notify_acked_irq calls kvm_set_irq()
1417 * to reset the IRQ level, which grabs the dist->lock
1418 * so we call this before taking the dist->lock.
1419 */
1420 kvm_notify_acked_irq(kvm, 0,
1421 vlr.irq - VGIC_NR_PRIVATE_IRQS);
1422
1423 spin_lock(&dist->lock);
1424 level_pending |= process_level_irq(vcpu, lr, vlr);
1425 spin_unlock(&dist->lock);
1426 }
1427 }
1428
1429 if (status & INT_STATUS_UNDERFLOW)
1430 vgic_disable_underflow(vcpu);
1431
1432 /*
1433 * In the next iterations of the vcpu loop, if we sync the vgic state
1434 * after flushing it, but before entering the guest (this happens for
1435 * pending signals and vmid rollovers), then make sure we don't pick
1436 * up any old maintenance interrupts here.
1437 */
1438 vgic_clear_eisr(vcpu);
1439
1440 return level_pending;
1441 }
1442
1443 /*
1444 * Save the physical active state, and reset it to inactive.
1445 *
1446 * Return 1 if HW interrupt went from active to inactive, and 0 otherwise.
1447 */
1448 static int vgic_sync_hwirq(struct kvm_vcpu *vcpu, struct vgic_lr vlr)
1449 {
1450 struct irq_phys_map *map;
1451 int ret;
1452
1453 if (!(vlr.state & LR_HW))
1454 return 0;
1455
1456 map = vgic_irq_map_search(vcpu, vlr.irq);
1457 BUG_ON(!map);
1458
1459 ret = irq_get_irqchip_state(map->irq,
1460 IRQCHIP_STATE_ACTIVE,
1461 &map->active);
1462
1463 WARN_ON(ret);
1464
1465 if (map->active)
1466 return 0;
1467
1468 return 1;
1469 }
1470
1471 /* Sync back the VGIC state after a guest run */
1472 static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1473 {
1474 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1475 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1476 u64 elrsr;
1477 unsigned long *elrsr_ptr;
1478 int lr, pending;
1479 bool level_pending;
1480
1481 level_pending = vgic_process_maintenance(vcpu);
1482 elrsr = vgic_get_elrsr(vcpu);
1483 elrsr_ptr = u64_to_bitmask(&elrsr);
1484
1485 /* Deal with HW interrupts, and clear mappings for empty LRs */
1486 for (lr = 0; lr < vgic->nr_lr; lr++) {
1487 struct vgic_lr vlr;
1488
1489 if (!test_bit(lr, vgic_cpu->lr_used))
1490 continue;
1491
1492 vlr = vgic_get_lr(vcpu, lr);
1493 if (vgic_sync_hwirq(vcpu, vlr)) {
1494 /*
1495 * So this is a HW interrupt that the guest
1496 * EOI-ed. Clean the LR state and allow the
1497 * interrupt to be sampled again.
1498 */
1499 vlr.state = 0;
1500 vlr.hwirq = 0;
1501 vgic_set_lr(vcpu, lr, vlr);
1502 vgic_irq_clear_queued(vcpu, vlr.irq);
1503 set_bit(lr, elrsr_ptr);
1504 }
1505
1506 if (!test_bit(lr, elrsr_ptr))
1507 continue;
1508
1509 clear_bit(lr, vgic_cpu->lr_used);
1510
1511 BUG_ON(vlr.irq >= dist->nr_irqs);
1512 vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
1513 }
1514
1515 /* Check if we still have something up our sleeve... */
1516 pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr);
1517 if (level_pending || pending < vgic->nr_lr)
1518 set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
1519 }
1520
1521 void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1522 {
1523 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1524
1525 if (!irqchip_in_kernel(vcpu->kvm))
1526 return;
1527
1528 spin_lock(&dist->lock);
1529 __kvm_vgic_flush_hwstate(vcpu);
1530 spin_unlock(&dist->lock);
1531 }
1532
1533 void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1534 {
1535 if (!irqchip_in_kernel(vcpu->kvm))
1536 return;
1537
1538 __kvm_vgic_sync_hwstate(vcpu);
1539 }
1540
1541 int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
1542 {
1543 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1544
1545 if (!irqchip_in_kernel(vcpu->kvm))
1546 return 0;
1547
1548 return test_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
1549 }
1550
1551 int kvm_vgic_vcpu_active_irq(struct kvm_vcpu *vcpu)
1552 {
1553 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1554
1555 if (!irqchip_in_kernel(vcpu->kvm))
1556 return 0;
1557
1558 return test_bit(vcpu->vcpu_id, dist->irq_active_on_cpu);
1559 }
1560
1561
1562 void vgic_kick_vcpus(struct kvm *kvm)
1563 {
1564 struct kvm_vcpu *vcpu;
1565 int c;
1566
1567 /*
1568 * We've injected an interrupt, time to find out who deserves
1569 * a good kick...
1570 */
1571 kvm_for_each_vcpu(c, vcpu, kvm) {
1572 if (kvm_vgic_vcpu_pending_irq(vcpu))
1573 kvm_vcpu_kick(vcpu);
1574 }
1575 }
1576
1577 static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
1578 {
1579 int edge_triggered = vgic_irq_is_edge(vcpu, irq);
1580
1581 /*
1582 * Only inject an interrupt if:
1583 * - edge triggered and we have a rising edge
1584 * - level triggered and we change level
1585 */
1586 if (edge_triggered) {
1587 int state = vgic_dist_irq_is_pending(vcpu, irq);
1588 return level > state;
1589 } else {
1590 int state = vgic_dist_irq_get_level(vcpu, irq);
1591 return level != state;
1592 }
1593 }
1594
1595 static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
1596 struct irq_phys_map *map,
1597 unsigned int irq_num, bool level)
1598 {
1599 struct vgic_dist *dist = &kvm->arch.vgic;
1600 struct kvm_vcpu *vcpu;
1601 int edge_triggered, level_triggered;
1602 int enabled;
1603 bool ret = true, can_inject = true;
1604
1605 if (irq_num >= min(kvm->arch.vgic.nr_irqs, 1020))
1606 return -EINVAL;
1607
1608 spin_lock(&dist->lock);
1609
1610 vcpu = kvm_get_vcpu(kvm, cpuid);
1611 edge_triggered = vgic_irq_is_edge(vcpu, irq_num);
1612 level_triggered = !edge_triggered;
1613
1614 if (!vgic_validate_injection(vcpu, irq_num, level)) {
1615 ret = false;
1616 goto out;
1617 }
1618
1619 if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
1620 cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
1621 if (cpuid == VCPU_NOT_ALLOCATED) {
1622 /* Pretend we use CPU0, and prevent injection */
1623 cpuid = 0;
1624 can_inject = false;
1625 }
1626 vcpu = kvm_get_vcpu(kvm, cpuid);
1627 }
1628
1629 kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
1630
1631 if (level) {
1632 if (level_triggered)
1633 vgic_dist_irq_set_level(vcpu, irq_num);
1634 vgic_dist_irq_set_pending(vcpu, irq_num);
1635 } else {
1636 if (level_triggered) {
1637 vgic_dist_irq_clear_level(vcpu, irq_num);
1638 if (!vgic_dist_irq_soft_pend(vcpu, irq_num)) {
1639 vgic_dist_irq_clear_pending(vcpu, irq_num);
1640 vgic_cpu_irq_clear(vcpu, irq_num);
1641 if (!compute_pending_for_cpu(vcpu))
1642 clear_bit(cpuid, dist->irq_pending_on_cpu);
1643 }
1644 }
1645
1646 ret = false;
1647 goto out;
1648 }
1649
1650 enabled = vgic_irq_is_enabled(vcpu, irq_num);
1651
1652 if (!enabled || !can_inject) {
1653 ret = false;
1654 goto out;
1655 }
1656
1657 if (!vgic_can_sample_irq(vcpu, irq_num)) {
1658 /*
1659 * Level interrupt in progress, will be picked up
1660 * when EOId.
1661 */
1662 ret = false;
1663 goto out;
1664 }
1665
1666 if (level) {
1667 vgic_cpu_irq_set(vcpu, irq_num);
1668 set_bit(cpuid, dist->irq_pending_on_cpu);
1669 }
1670
1671 out:
1672 spin_unlock(&dist->lock);
1673
1674 if (ret) {
1675 /* kick the specified vcpu */
1676 kvm_vcpu_kick(kvm_get_vcpu(kvm, cpuid));
1677 }
1678
1679 return 0;
1680 }
1681
1682 static int vgic_lazy_init(struct kvm *kvm)
1683 {
1684 int ret = 0;
1685
1686 if (unlikely(!vgic_initialized(kvm))) {
1687 /*
1688 * We only provide the automatic initialization of the VGIC
1689 * for the legacy case of a GICv2. Any other type must
1690 * be explicitly initialized once setup with the respective
1691 * KVM device call.
1692 */
1693 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2)
1694 return -EBUSY;
1695
1696 mutex_lock(&kvm->lock);
1697 ret = vgic_init(kvm);
1698 mutex_unlock(&kvm->lock);
1699 }
1700
1701 return ret;
1702 }
1703
1704 /**
1705 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
1706 * @kvm: The VM structure pointer
1707 * @cpuid: The CPU for PPIs
1708 * @irq_num: The IRQ number that is assigned to the device. This IRQ
1709 * must not be mapped to a HW interrupt.
1710 * @level: Edge-triggered: true: to trigger the interrupt
1711 * false: to ignore the call
1712 * Level-sensitive true: raise the input signal
1713 * false: lower the input signal
1714 *
1715 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1716 * level-sensitive interrupts. You can think of the level parameter as 1
1717 * being HIGH and 0 being LOW and all devices being active-HIGH.
1718 */
1719 int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
1720 bool level)
1721 {
1722 struct irq_phys_map *map;
1723 int ret;
1724
1725 ret = vgic_lazy_init(kvm);
1726 if (ret)
1727 return ret;
1728
1729 map = vgic_irq_map_search(kvm_get_vcpu(kvm, cpuid), irq_num);
1730 if (map)
1731 return -EINVAL;
1732
1733 return vgic_update_irq_pending(kvm, cpuid, NULL, irq_num, level);
1734 }
1735
1736 /**
1737 * kvm_vgic_inject_mapped_irq - Inject a physically mapped IRQ to the vgic
1738 * @kvm: The VM structure pointer
1739 * @cpuid: The CPU for PPIs
1740 * @map: Pointer to a irq_phys_map structure describing the mapping
1741 * @level: Edge-triggered: true: to trigger the interrupt
1742 * false: to ignore the call
1743 * Level-sensitive true: raise the input signal
1744 * false: lower the input signal
1745 *
1746 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1747 * level-sensitive interrupts. You can think of the level parameter as 1
1748 * being HIGH and 0 being LOW and all devices being active-HIGH.
1749 */
1750 int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid,
1751 struct irq_phys_map *map, bool level)
1752 {
1753 int ret;
1754
1755 ret = vgic_lazy_init(kvm);
1756 if (ret)
1757 return ret;
1758
1759 return vgic_update_irq_pending(kvm, cpuid, map, map->virt_irq, level);
1760 }
1761
1762 static irqreturn_t vgic_maintenance_handler(int irq, void *data)
1763 {
1764 /*
1765 * We cannot rely on the vgic maintenance interrupt to be
1766 * delivered synchronously. This means we can only use it to
1767 * exit the VM, and we perform the handling of EOIed
1768 * interrupts on the exit path (see vgic_process_maintenance).
1769 */
1770 return IRQ_HANDLED;
1771 }
1772
1773 static struct list_head *vgic_get_irq_phys_map_list(struct kvm_vcpu *vcpu,
1774 int virt_irq)
1775 {
1776 if (virt_irq < VGIC_NR_PRIVATE_IRQS)
1777 return &vcpu->arch.vgic_cpu.irq_phys_map_list;
1778 else
1779 return &vcpu->kvm->arch.vgic.irq_phys_map_list;
1780 }
1781
1782 /**
1783 * kvm_vgic_map_phys_irq - map a virtual IRQ to a physical IRQ
1784 * @vcpu: The VCPU pointer
1785 * @virt_irq: The virtual irq number
1786 * @irq: The Linux IRQ number
1787 *
1788 * Establish a mapping between a guest visible irq (@virt_irq) and a
1789 * Linux irq (@irq). On injection, @virt_irq will be associated with
1790 * the physical interrupt represented by @irq. This mapping can be
1791 * established multiple times as long as the parameters are the same.
1792 *
1793 * Returns a valid pointer on success, and an error pointer otherwise
1794 */
1795 struct irq_phys_map *kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu,
1796 int virt_irq, int irq)
1797 {
1798 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1799 struct list_head *root = vgic_get_irq_phys_map_list(vcpu, virt_irq);
1800 struct irq_phys_map *map;
1801 struct irq_phys_map_entry *entry;
1802 struct irq_desc *desc;
1803 struct irq_data *data;
1804 int phys_irq;
1805
1806 desc = irq_to_desc(irq);
1807 if (!desc) {
1808 kvm_err("%s: no interrupt descriptor\n", __func__);
1809 return ERR_PTR(-EINVAL);
1810 }
1811
1812 data = irq_desc_get_irq_data(desc);
1813 while (data->parent_data)
1814 data = data->parent_data;
1815
1816 phys_irq = data->hwirq;
1817
1818 /* Create a new mapping */
1819 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1820 if (!entry)
1821 return ERR_PTR(-ENOMEM);
1822
1823 spin_lock(&dist->irq_phys_map_lock);
1824
1825 /* Try to match an existing mapping */
1826 map = vgic_irq_map_search(vcpu, virt_irq);
1827 if (map) {
1828 /* Make sure this mapping matches */
1829 if (map->phys_irq != phys_irq ||
1830 map->irq != irq)
1831 map = ERR_PTR(-EINVAL);
1832
1833 /* Found an existing, valid mapping */
1834 goto out;
1835 }
1836
1837 map = &entry->map;
1838 map->virt_irq = virt_irq;
1839 map->phys_irq = phys_irq;
1840 map->irq = irq;
1841
1842 list_add_tail_rcu(&entry->entry, root);
1843
1844 out:
1845 spin_unlock(&dist->irq_phys_map_lock);
1846 /* If we've found a hit in the existing list, free the useless
1847 * entry */
1848 if (IS_ERR(map) || map != &entry->map)
1849 kfree(entry);
1850 return map;
1851 }
1852
1853 static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu,
1854 int virt_irq)
1855 {
1856 struct list_head *root = vgic_get_irq_phys_map_list(vcpu, virt_irq);
1857 struct irq_phys_map_entry *entry;
1858 struct irq_phys_map *map;
1859
1860 rcu_read_lock();
1861
1862 list_for_each_entry_rcu(entry, root, entry) {
1863 map = &entry->map;
1864 if (map->virt_irq == virt_irq) {
1865 rcu_read_unlock();
1866 return map;
1867 }
1868 }
1869
1870 rcu_read_unlock();
1871
1872 return NULL;
1873 }
1874
1875 static void vgic_free_phys_irq_map_rcu(struct rcu_head *rcu)
1876 {
1877 struct irq_phys_map_entry *entry;
1878
1879 entry = container_of(rcu, struct irq_phys_map_entry, rcu);
1880 kfree(entry);
1881 }
1882
1883 /**
1884 * kvm_vgic_get_phys_irq_active - Return the active state of a mapped IRQ
1885 *
1886 * Return the logical active state of a mapped interrupt. This doesn't
1887 * necessarily reflects the current HW state.
1888 */
1889 bool kvm_vgic_get_phys_irq_active(struct irq_phys_map *map)
1890 {
1891 BUG_ON(!map);
1892 return map->active;
1893 }
1894
1895 /**
1896 * kvm_vgic_set_phys_irq_active - Set the active state of a mapped IRQ
1897 *
1898 * Set the logical active state of a mapped interrupt. This doesn't
1899 * immediately affects the HW state.
1900 */
1901 void kvm_vgic_set_phys_irq_active(struct irq_phys_map *map, bool active)
1902 {
1903 BUG_ON(!map);
1904 map->active = active;
1905 }
1906
1907 /**
1908 * kvm_vgic_unmap_phys_irq - Remove a virtual to physical IRQ mapping
1909 * @vcpu: The VCPU pointer
1910 * @map: The pointer to a mapping obtained through kvm_vgic_map_phys_irq
1911 *
1912 * Remove an existing mapping between virtual and physical interrupts.
1913 */
1914 int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, struct irq_phys_map *map)
1915 {
1916 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1917 struct irq_phys_map_entry *entry;
1918 struct list_head *root;
1919
1920 if (!map)
1921 return -EINVAL;
1922
1923 root = vgic_get_irq_phys_map_list(vcpu, map->virt_irq);
1924
1925 spin_lock(&dist->irq_phys_map_lock);
1926
1927 list_for_each_entry(entry, root, entry) {
1928 if (&entry->map == map) {
1929 list_del_rcu(&entry->entry);
1930 call_rcu(&entry->rcu, vgic_free_phys_irq_map_rcu);
1931 break;
1932 }
1933 }
1934
1935 spin_unlock(&dist->irq_phys_map_lock);
1936
1937 return 0;
1938 }
1939
1940 static void vgic_destroy_irq_phys_map(struct kvm *kvm, struct list_head *root)
1941 {
1942 struct vgic_dist *dist = &kvm->arch.vgic;
1943 struct irq_phys_map_entry *entry;
1944
1945 spin_lock(&dist->irq_phys_map_lock);
1946
1947 list_for_each_entry(entry, root, entry) {
1948 list_del_rcu(&entry->entry);
1949 call_rcu(&entry->rcu, vgic_free_phys_irq_map_rcu);
1950 }
1951
1952 spin_unlock(&dist->irq_phys_map_lock);
1953 }
1954
1955 void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
1956 {
1957 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1958
1959 kfree(vgic_cpu->pending_shared);
1960 kfree(vgic_cpu->active_shared);
1961 kfree(vgic_cpu->pend_act_shared);
1962 kfree(vgic_cpu->vgic_irq_lr_map);
1963 vgic_destroy_irq_phys_map(vcpu->kvm, &vgic_cpu->irq_phys_map_list);
1964 vgic_cpu->pending_shared = NULL;
1965 vgic_cpu->active_shared = NULL;
1966 vgic_cpu->pend_act_shared = NULL;
1967 vgic_cpu->vgic_irq_lr_map = NULL;
1968 }
1969
1970 static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
1971 {
1972 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1973
1974 int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8;
1975 vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL);
1976 vgic_cpu->active_shared = kzalloc(sz, GFP_KERNEL);
1977 vgic_cpu->pend_act_shared = kzalloc(sz, GFP_KERNEL);
1978 vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL);
1979
1980 if (!vgic_cpu->pending_shared
1981 || !vgic_cpu->active_shared
1982 || !vgic_cpu->pend_act_shared
1983 || !vgic_cpu->vgic_irq_lr_map) {
1984 kvm_vgic_vcpu_destroy(vcpu);
1985 return -ENOMEM;
1986 }
1987
1988 memset(vgic_cpu->vgic_irq_lr_map, LR_EMPTY, nr_irqs);
1989
1990 /*
1991 * Store the number of LRs per vcpu, so we don't have to go
1992 * all the way to the distributor structure to find out. Only
1993 * assembly code should use this one.
1994 */
1995 vgic_cpu->nr_lr = vgic->nr_lr;
1996
1997 return 0;
1998 }
1999
2000 /**
2001 * kvm_vgic_vcpu_early_init - Earliest possible per-vcpu vgic init stage
2002 *
2003 * No memory allocation should be performed here, only static init.
2004 */
2005 void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu)
2006 {
2007 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
2008 INIT_LIST_HEAD(&vgic_cpu->irq_phys_map_list);
2009 }
2010
2011 /**
2012 * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW
2013 *
2014 * The host's GIC naturally limits the maximum amount of VCPUs a guest
2015 * can use.
2016 */
2017 int kvm_vgic_get_max_vcpus(void)
2018 {
2019 return vgic->max_gic_vcpus;
2020 }
2021
2022 void kvm_vgic_destroy(struct kvm *kvm)
2023 {
2024 struct vgic_dist *dist = &kvm->arch.vgic;
2025 struct kvm_vcpu *vcpu;
2026 int i;
2027
2028 kvm_for_each_vcpu(i, vcpu, kvm)
2029 kvm_vgic_vcpu_destroy(vcpu);
2030
2031 vgic_free_bitmap(&dist->irq_enabled);
2032 vgic_free_bitmap(&dist->irq_level);
2033 vgic_free_bitmap(&dist->irq_pending);
2034 vgic_free_bitmap(&dist->irq_soft_pend);
2035 vgic_free_bitmap(&dist->irq_queued);
2036 vgic_free_bitmap(&dist->irq_cfg);
2037 vgic_free_bytemap(&dist->irq_priority);
2038 if (dist->irq_spi_target) {
2039 for (i = 0; i < dist->nr_cpus; i++)
2040 vgic_free_bitmap(&dist->irq_spi_target[i]);
2041 }
2042 kfree(dist->irq_sgi_sources);
2043 kfree(dist->irq_spi_cpu);
2044 kfree(dist->irq_spi_mpidr);
2045 kfree(dist->irq_spi_target);
2046 kfree(dist->irq_pending_on_cpu);
2047 kfree(dist->irq_active_on_cpu);
2048 vgic_destroy_irq_phys_map(kvm, &dist->irq_phys_map_list);
2049 dist->irq_sgi_sources = NULL;
2050 dist->irq_spi_cpu = NULL;
2051 dist->irq_spi_target = NULL;
2052 dist->irq_pending_on_cpu = NULL;
2053 dist->irq_active_on_cpu = NULL;
2054 dist->nr_cpus = 0;
2055 }
2056
2057 /*
2058 * Allocate and initialize the various data structures. Must be called
2059 * with kvm->lock held!
2060 */
2061 int vgic_init(struct kvm *kvm)
2062 {
2063 struct vgic_dist *dist = &kvm->arch.vgic;
2064 struct kvm_vcpu *vcpu;
2065 int nr_cpus, nr_irqs;
2066 int ret, i, vcpu_id;
2067
2068 if (vgic_initialized(kvm))
2069 return 0;
2070
2071 nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus);
2072 if (!nr_cpus) /* No vcpus? Can't be good... */
2073 return -ENODEV;
2074
2075 /*
2076 * If nobody configured the number of interrupts, use the
2077 * legacy one.
2078 */
2079 if (!dist->nr_irqs)
2080 dist->nr_irqs = VGIC_NR_IRQS_LEGACY;
2081
2082 nr_irqs = dist->nr_irqs;
2083
2084 ret = vgic_init_bitmap(&dist->irq_enabled, nr_cpus, nr_irqs);
2085 ret |= vgic_init_bitmap(&dist->irq_level, nr_cpus, nr_irqs);
2086 ret |= vgic_init_bitmap(&dist->irq_pending, nr_cpus, nr_irqs);
2087 ret |= vgic_init_bitmap(&dist->irq_soft_pend, nr_cpus, nr_irqs);
2088 ret |= vgic_init_bitmap(&dist->irq_queued, nr_cpus, nr_irqs);
2089 ret |= vgic_init_bitmap(&dist->irq_active, nr_cpus, nr_irqs);
2090 ret |= vgic_init_bitmap(&dist->irq_cfg, nr_cpus, nr_irqs);
2091 ret |= vgic_init_bytemap(&dist->irq_priority, nr_cpus, nr_irqs);
2092
2093 if (ret)
2094 goto out;
2095
2096 dist->irq_sgi_sources = kzalloc(nr_cpus * VGIC_NR_SGIS, GFP_KERNEL);
2097 dist->irq_spi_cpu = kzalloc(nr_irqs - VGIC_NR_PRIVATE_IRQS, GFP_KERNEL);
2098 dist->irq_spi_target = kzalloc(sizeof(*dist->irq_spi_target) * nr_cpus,
2099 GFP_KERNEL);
2100 dist->irq_pending_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
2101 GFP_KERNEL);
2102 dist->irq_active_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
2103 GFP_KERNEL);
2104 if (!dist->irq_sgi_sources ||
2105 !dist->irq_spi_cpu ||
2106 !dist->irq_spi_target ||
2107 !dist->irq_pending_on_cpu ||
2108 !dist->irq_active_on_cpu) {
2109 ret = -ENOMEM;
2110 goto out;
2111 }
2112
2113 for (i = 0; i < nr_cpus; i++)
2114 ret |= vgic_init_bitmap(&dist->irq_spi_target[i],
2115 nr_cpus, nr_irqs);
2116
2117 if (ret)
2118 goto out;
2119
2120 ret = kvm->arch.vgic.vm_ops.init_model(kvm);
2121 if (ret)
2122 goto out;
2123
2124 kvm_for_each_vcpu(vcpu_id, vcpu, kvm) {
2125 ret = vgic_vcpu_init_maps(vcpu, nr_irqs);
2126 if (ret) {
2127 kvm_err("VGIC: Failed to allocate vcpu memory\n");
2128 break;
2129 }
2130
2131 /*
2132 * Enable all SGIs and configure all private IRQs as
2133 * edge-triggered.
2134 */
2135 for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
2136 if (i < VGIC_NR_SGIS)
2137 vgic_bitmap_set_irq_val(&dist->irq_enabled,
2138 vcpu->vcpu_id, i, 1);
2139 if (i < VGIC_NR_PRIVATE_IRQS)
2140 vgic_bitmap_set_irq_val(&dist->irq_cfg,
2141 vcpu->vcpu_id, i,
2142 VGIC_CFG_EDGE);
2143 }
2144
2145 vgic_enable(vcpu);
2146 }
2147
2148 out:
2149 if (ret)
2150 kvm_vgic_destroy(kvm);
2151
2152 return ret;
2153 }
2154
2155 static int init_vgic_model(struct kvm *kvm, int type)
2156 {
2157 switch (type) {
2158 case KVM_DEV_TYPE_ARM_VGIC_V2:
2159 vgic_v2_init_emulation(kvm);
2160 break;
2161 #ifdef CONFIG_ARM_GIC_V3
2162 case KVM_DEV_TYPE_ARM_VGIC_V3:
2163 vgic_v3_init_emulation(kvm);
2164 break;
2165 #endif
2166 default:
2167 return -ENODEV;
2168 }
2169
2170 if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus)
2171 return -E2BIG;
2172
2173 return 0;
2174 }
2175
2176 /**
2177 * kvm_vgic_early_init - Earliest possible vgic initialization stage
2178 *
2179 * No memory allocation should be performed here, only static init.
2180 */
2181 void kvm_vgic_early_init(struct kvm *kvm)
2182 {
2183 spin_lock_init(&kvm->arch.vgic.lock);
2184 spin_lock_init(&kvm->arch.vgic.irq_phys_map_lock);
2185 INIT_LIST_HEAD(&kvm->arch.vgic.irq_phys_map_list);
2186 }
2187
2188 int kvm_vgic_create(struct kvm *kvm, u32 type)
2189 {
2190 int i, vcpu_lock_idx = -1, ret;
2191 struct kvm_vcpu *vcpu;
2192
2193 mutex_lock(&kvm->lock);
2194
2195 if (irqchip_in_kernel(kvm)) {
2196 ret = -EEXIST;
2197 goto out;
2198 }
2199
2200 /*
2201 * This function is also called by the KVM_CREATE_IRQCHIP handler,
2202 * which had no chance yet to check the availability of the GICv2
2203 * emulation. So check this here again. KVM_CREATE_DEVICE does
2204 * the proper checks already.
2205 */
2206 if (type == KVM_DEV_TYPE_ARM_VGIC_V2 && !vgic->can_emulate_gicv2) {
2207 ret = -ENODEV;
2208 goto out;
2209 }
2210
2211 /*
2212 * Any time a vcpu is run, vcpu_load is called which tries to grab the
2213 * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
2214 * that no other VCPUs are run while we create the vgic.
2215 */
2216 ret = -EBUSY;
2217 kvm_for_each_vcpu(i, vcpu, kvm) {
2218 if (!mutex_trylock(&vcpu->mutex))
2219 goto out_unlock;
2220 vcpu_lock_idx = i;
2221 }
2222
2223 kvm_for_each_vcpu(i, vcpu, kvm) {
2224 if (vcpu->arch.has_run_once)
2225 goto out_unlock;
2226 }
2227 ret = 0;
2228
2229 ret = init_vgic_model(kvm, type);
2230 if (ret)
2231 goto out_unlock;
2232
2233 kvm->arch.vgic.in_kernel = true;
2234 kvm->arch.vgic.vgic_model = type;
2235 kvm->arch.vgic.vctrl_base = vgic->vctrl_base;
2236 kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
2237 kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
2238 kvm->arch.vgic.vgic_redist_base = VGIC_ADDR_UNDEF;
2239
2240 out_unlock:
2241 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
2242 vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
2243 mutex_unlock(&vcpu->mutex);
2244 }
2245
2246 out:
2247 mutex_unlock(&kvm->lock);
2248 return ret;
2249 }
2250
2251 static int vgic_ioaddr_overlap(struct kvm *kvm)
2252 {
2253 phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
2254 phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
2255
2256 if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
2257 return 0;
2258 if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
2259 (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
2260 return -EBUSY;
2261 return 0;
2262 }
2263
2264 static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
2265 phys_addr_t addr, phys_addr_t size)
2266 {
2267 int ret;
2268
2269 if (addr & ~KVM_PHYS_MASK)
2270 return -E2BIG;
2271
2272 if (addr & (SZ_4K - 1))
2273 return -EINVAL;
2274
2275 if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
2276 return -EEXIST;
2277 if (addr + size < addr)
2278 return -EINVAL;
2279
2280 *ioaddr = addr;
2281 ret = vgic_ioaddr_overlap(kvm);
2282 if (ret)
2283 *ioaddr = VGIC_ADDR_UNDEF;
2284
2285 return ret;
2286 }
2287
2288 /**
2289 * kvm_vgic_addr - set or get vgic VM base addresses
2290 * @kvm: pointer to the vm struct
2291 * @type: the VGIC addr type, one of KVM_VGIC_V[23]_ADDR_TYPE_XXX
2292 * @addr: pointer to address value
2293 * @write: if true set the address in the VM address space, if false read the
2294 * address
2295 *
2296 * Set or get the vgic base addresses for the distributor and the virtual CPU
2297 * interface in the VM physical address space. These addresses are properties
2298 * of the emulated core/SoC and therefore user space initially knows this
2299 * information.
2300 */
2301 int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
2302 {
2303 int r = 0;
2304 struct vgic_dist *vgic = &kvm->arch.vgic;
2305 int type_needed;
2306 phys_addr_t *addr_ptr, block_size;
2307 phys_addr_t alignment;
2308
2309 mutex_lock(&kvm->lock);
2310 switch (type) {
2311 case KVM_VGIC_V2_ADDR_TYPE_DIST:
2312 type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
2313 addr_ptr = &vgic->vgic_dist_base;
2314 block_size = KVM_VGIC_V2_DIST_SIZE;
2315 alignment = SZ_4K;
2316 break;
2317 case KVM_VGIC_V2_ADDR_TYPE_CPU:
2318 type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
2319 addr_ptr = &vgic->vgic_cpu_base;
2320 block_size = KVM_VGIC_V2_CPU_SIZE;
2321 alignment = SZ_4K;
2322 break;
2323 #ifdef CONFIG_ARM_GIC_V3
2324 case KVM_VGIC_V3_ADDR_TYPE_DIST:
2325 type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
2326 addr_ptr = &vgic->vgic_dist_base;
2327 block_size = KVM_VGIC_V3_DIST_SIZE;
2328 alignment = SZ_64K;
2329 break;
2330 case KVM_VGIC_V3_ADDR_TYPE_REDIST:
2331 type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
2332 addr_ptr = &vgic->vgic_redist_base;
2333 block_size = KVM_VGIC_V3_REDIST_SIZE;
2334 alignment = SZ_64K;
2335 break;
2336 #endif
2337 default:
2338 r = -ENODEV;
2339 goto out;
2340 }
2341
2342 if (vgic->vgic_model != type_needed) {
2343 r = -ENODEV;
2344 goto out;
2345 }
2346
2347 if (write) {
2348 if (!IS_ALIGNED(*addr, alignment))
2349 r = -EINVAL;
2350 else
2351 r = vgic_ioaddr_assign(kvm, addr_ptr, *addr,
2352 block_size);
2353 } else {
2354 *addr = *addr_ptr;
2355 }
2356
2357 out:
2358 mutex_unlock(&kvm->lock);
2359 return r;
2360 }
2361
2362 int vgic_set_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2363 {
2364 int r;
2365
2366 switch (attr->group) {
2367 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2368 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2369 u64 addr;
2370 unsigned long type = (unsigned long)attr->attr;
2371
2372 if (copy_from_user(&addr, uaddr, sizeof(addr)))
2373 return -EFAULT;
2374
2375 r = kvm_vgic_addr(dev->kvm, type, &addr, true);
2376 return (r == -ENODEV) ? -ENXIO : r;
2377 }
2378 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2379 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2380 u32 val;
2381 int ret = 0;
2382
2383 if (get_user(val, uaddr))
2384 return -EFAULT;
2385
2386 /*
2387 * We require:
2388 * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs
2389 * - at most 1024 interrupts
2390 * - a multiple of 32 interrupts
2391 */
2392 if (val < (VGIC_NR_PRIVATE_IRQS + 32) ||
2393 val > VGIC_MAX_IRQS ||
2394 (val & 31))
2395 return -EINVAL;
2396
2397 mutex_lock(&dev->kvm->lock);
2398
2399 if (vgic_ready(dev->kvm) || dev->kvm->arch.vgic.nr_irqs)
2400 ret = -EBUSY;
2401 else
2402 dev->kvm->arch.vgic.nr_irqs = val;
2403
2404 mutex_unlock(&dev->kvm->lock);
2405
2406 return ret;
2407 }
2408 case KVM_DEV_ARM_VGIC_GRP_CTRL: {
2409 switch (attr->attr) {
2410 case KVM_DEV_ARM_VGIC_CTRL_INIT:
2411 r = vgic_init(dev->kvm);
2412 return r;
2413 }
2414 break;
2415 }
2416 }
2417
2418 return -ENXIO;
2419 }
2420
2421 int vgic_get_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2422 {
2423 int r = -ENXIO;
2424
2425 switch (attr->group) {
2426 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2427 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2428 u64 addr;
2429 unsigned long type = (unsigned long)attr->attr;
2430
2431 r = kvm_vgic_addr(dev->kvm, type, &addr, false);
2432 if (r)
2433 return (r == -ENODEV) ? -ENXIO : r;
2434
2435 if (copy_to_user(uaddr, &addr, sizeof(addr)))
2436 return -EFAULT;
2437 break;
2438 }
2439 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2440 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2441
2442 r = put_user(dev->kvm->arch.vgic.nr_irqs, uaddr);
2443 break;
2444 }
2445
2446 }
2447
2448 return r;
2449 }
2450
2451 int vgic_has_attr_regs(const struct vgic_io_range *ranges, phys_addr_t offset)
2452 {
2453 if (vgic_find_range(ranges, 4, offset))
2454 return 0;
2455 else
2456 return -ENXIO;
2457 }
2458
2459 static void vgic_init_maintenance_interrupt(void *info)
2460 {
2461 enable_percpu_irq(vgic->maint_irq, 0);
2462 }
2463
2464 static int vgic_cpu_notify(struct notifier_block *self,
2465 unsigned long action, void *cpu)
2466 {
2467 switch (action) {
2468 case CPU_STARTING:
2469 case CPU_STARTING_FROZEN:
2470 vgic_init_maintenance_interrupt(NULL);
2471 break;
2472 case CPU_DYING:
2473 case CPU_DYING_FROZEN:
2474 disable_percpu_irq(vgic->maint_irq);
2475 break;
2476 }
2477
2478 return NOTIFY_OK;
2479 }
2480
2481 static struct notifier_block vgic_cpu_nb = {
2482 .notifier_call = vgic_cpu_notify,
2483 };
2484
2485 static const struct of_device_id vgic_ids[] = {
2486 { .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, },
2487 { .compatible = "arm,cortex-a7-gic", .data = vgic_v2_probe, },
2488 { .compatible = "arm,gic-400", .data = vgic_v2_probe, },
2489 { .compatible = "arm,gic-v3", .data = vgic_v3_probe, },
2490 {},
2491 };
2492
2493 int kvm_vgic_hyp_init(void)
2494 {
2495 const struct of_device_id *matched_id;
2496 const int (*vgic_probe)(struct device_node *,const struct vgic_ops **,
2497 const struct vgic_params **);
2498 struct device_node *vgic_node;
2499 int ret;
2500
2501 vgic_node = of_find_matching_node_and_match(NULL,
2502 vgic_ids, &matched_id);
2503 if (!vgic_node) {
2504 kvm_err("error: no compatible GIC node found\n");
2505 return -ENODEV;
2506 }
2507
2508 vgic_probe = matched_id->data;
2509 ret = vgic_probe(vgic_node, &vgic_ops, &vgic);
2510 if (ret)
2511 return ret;
2512
2513 ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler,
2514 "vgic", kvm_get_running_vcpus());
2515 if (ret) {
2516 kvm_err("Cannot register interrupt %d\n", vgic->maint_irq);
2517 return ret;
2518 }
2519
2520 ret = __register_cpu_notifier(&vgic_cpu_nb);
2521 if (ret) {
2522 kvm_err("Cannot register vgic CPU notifier\n");
2523 goto out_free_irq;
2524 }
2525
2526 on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
2527
2528 return 0;
2529
2530 out_free_irq:
2531 free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus());
2532 return ret;
2533 }
2534
2535 int kvm_irq_map_gsi(struct kvm *kvm,
2536 struct kvm_kernel_irq_routing_entry *entries,
2537 int gsi)
2538 {
2539 return 0;
2540 }
2541
2542 int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin)
2543 {
2544 return pin;
2545 }
2546
2547 int kvm_set_irq(struct kvm *kvm, int irq_source_id,
2548 u32 irq, int level, bool line_status)
2549 {
2550 unsigned int spi = irq + VGIC_NR_PRIVATE_IRQS;
2551
2552 trace_kvm_set_irq(irq, level, irq_source_id);
2553
2554 BUG_ON(!vgic_initialized(kvm));
2555
2556 return kvm_vgic_inject_irq(kvm, 0, spi, level);
2557 }
2558
2559 /* MSI not implemented yet */
2560 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
2561 struct kvm *kvm, int irq_source_id,
2562 int level, bool line_status)
2563 {
2564 return 0;
2565 }
This page took 0.104207 seconds and 5 git commands to generate.