Commit | Line | Data |
---|---|---|
1a89dd91 MZ |
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 | ||
01ac5e34 | 19 | #include <linux/cpu.h> |
1a89dd91 MZ |
20 | #include <linux/kvm.h> |
21 | #include <linux/kvm_host.h> | |
22 | #include <linux/interrupt.h> | |
23 | #include <linux/io.h> | |
01ac5e34 MZ |
24 | #include <linux/of.h> |
25 | #include <linux/of_address.h> | |
26 | #include <linux/of_irq.h> | |
2a2f3e26 | 27 | #include <linux/uaccess.h> |
01ac5e34 MZ |
28 | |
29 | #include <linux/irqchip/arm-gic.h> | |
30 | ||
1a89dd91 | 31 | #include <asm/kvm_emulate.h> |
01ac5e34 MZ |
32 | #include <asm/kvm_arm.h> |
33 | #include <asm/kvm_mmu.h> | |
174178fe | 34 | #include <trace/events/kvm.h> |
1a89dd91 | 35 | |
b47ef92a MZ |
36 | /* |
37 | * How the whole thing works (courtesy of Christoffer Dall): | |
38 | * | |
39 | * - At any time, the dist->irq_pending_on_cpu is the oracle that knows if | |
7e362919 CD |
40 | * something is pending on the CPU interface. |
41 | * - Interrupts that are pending on the distributor are stored on the | |
42 | * vgic.irq_pending vgic bitmap (this bitmap is updated by both user land | |
43 | * ioctls and guest mmio ops, and other in-kernel peripherals such as the | |
44 | * arch. timers). | |
b47ef92a MZ |
45 | * - Every time the bitmap changes, the irq_pending_on_cpu oracle is |
46 | * recalculated | |
47 | * - To calculate the oracle, we need info for each cpu from | |
48 | * compute_pending_for_cpu, which considers: | |
227844f5 CD |
49 | * - PPI: dist->irq_pending & dist->irq_enable |
50 | * - SPI: dist->irq_pending & dist->irq_enable & dist->irq_spi_target | |
7e362919 | 51 | * - irq_spi_target is a 'formatted' version of the GICD_ITARGETSRn |
b47ef92a MZ |
52 | * registers, stored on each vcpu. We only keep one bit of |
53 | * information per interrupt, making sure that only one vcpu can | |
54 | * accept the interrupt. | |
7e362919 | 55 | * - If any of the above state changes, we must recalculate the oracle. |
b47ef92a MZ |
56 | * - The same is true when injecting an interrupt, except that we only |
57 | * consider a single interrupt at a time. The irq_spi_cpu array | |
58 | * contains the target CPU for each SPI. | |
59 | * | |
60 | * The handling of level interrupts adds some extra complexity. We | |
61 | * need to track when the interrupt has been EOIed, so we can sample | |
62 | * the 'line' again. This is achieved as such: | |
63 | * | |
64 | * - When a level interrupt is moved onto a vcpu, the corresponding | |
dbf20f9d | 65 | * bit in irq_queued is set. As long as this bit is set, the line |
b47ef92a MZ |
66 | * will be ignored for further interrupts. The interrupt is injected |
67 | * into the vcpu with the GICH_LR_EOI bit set (generate a | |
68 | * maintenance interrupt on EOI). | |
69 | * - When the interrupt is EOIed, the maintenance interrupt fires, | |
dbf20f9d | 70 | * and clears the corresponding bit in irq_queued. This allows the |
b47ef92a | 71 | * interrupt line to be sampled again. |
faa1b46c CD |
72 | * - Note that level-triggered interrupts can also be set to pending from |
73 | * writes to GICD_ISPENDRn and lowering the external input line does not | |
74 | * cause the interrupt to become inactive in such a situation. | |
75 | * Conversely, writes to GICD_ICPENDRn do not cause the interrupt to become | |
76 | * inactive as long as the external input line is held high. | |
b47ef92a MZ |
77 | */ |
78 | ||
83215812 | 79 | #include "vgic.h" |
330690cd | 80 | |
a1fcb44e | 81 | static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu); |
8d5c6b06 | 82 | static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu); |
8d5c6b06 MZ |
83 | static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr); |
84 | static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, struct vgic_lr lr_desc); | |
01ac5e34 | 85 | |
8f186d52 MZ |
86 | static const struct vgic_ops *vgic_ops; |
87 | static const struct vgic_params *vgic; | |
b47ef92a | 88 | |
b26e5fda AP |
89 | static void add_sgi_source(struct kvm_vcpu *vcpu, int irq, int source) |
90 | { | |
91 | vcpu->kvm->arch.vgic.vm_ops.add_sgi_source(vcpu, irq, source); | |
92 | } | |
93 | ||
94 | static bool queue_sgi(struct kvm_vcpu *vcpu, int irq) | |
95 | { | |
96 | return vcpu->kvm->arch.vgic.vm_ops.queue_sgi(vcpu, irq); | |
97 | } | |
98 | ||
99 | int kvm_vgic_map_resources(struct kvm *kvm) | |
100 | { | |
101 | return kvm->arch.vgic.vm_ops.map_resources(kvm, vgic); | |
102 | } | |
103 | ||
9662fb48 | 104 | /* |
c1bfb577 MZ |
105 | * struct vgic_bitmap contains a bitmap made of unsigned longs, but |
106 | * extracts u32s out of them. | |
9662fb48 VK |
107 | * |
108 | * This does not work on 64-bit BE systems, because the bitmap access | |
109 | * will store two consecutive 32-bit words with the higher-addressed | |
110 | * register's bits at the lower index and the lower-addressed register's | |
111 | * bits at the higher index. | |
112 | * | |
113 | * Therefore, swizzle the register index when accessing the 32-bit word | |
114 | * registers to access the right register's value. | |
115 | */ | |
116 | #if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 64 | |
117 | #define REG_OFFSET_SWIZZLE 1 | |
118 | #else | |
119 | #define REG_OFFSET_SWIZZLE 0 | |
120 | #endif | |
b47ef92a | 121 | |
c1bfb577 MZ |
122 | static int vgic_init_bitmap(struct vgic_bitmap *b, int nr_cpus, int nr_irqs) |
123 | { | |
124 | int nr_longs; | |
125 | ||
126 | nr_longs = nr_cpus + BITS_TO_LONGS(nr_irqs - VGIC_NR_PRIVATE_IRQS); | |
127 | ||
128 | b->private = kzalloc(sizeof(unsigned long) * nr_longs, GFP_KERNEL); | |
129 | if (!b->private) | |
130 | return -ENOMEM; | |
131 | ||
132 | b->shared = b->private + nr_cpus; | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
137 | static void vgic_free_bitmap(struct vgic_bitmap *b) | |
138 | { | |
139 | kfree(b->private); | |
140 | b->private = NULL; | |
141 | b->shared = NULL; | |
142 | } | |
143 | ||
2df36a5d CD |
144 | /* |
145 | * Call this function to convert a u64 value to an unsigned long * bitmask | |
146 | * in a way that works on both 32-bit and 64-bit LE and BE platforms. | |
147 | * | |
148 | * Warning: Calling this function may modify *val. | |
149 | */ | |
150 | static unsigned long *u64_to_bitmask(u64 *val) | |
151 | { | |
152 | #if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 32 | |
153 | *val = (*val >> 32) | (*val << 32); | |
154 | #endif | |
155 | return (unsigned long *)val; | |
156 | } | |
157 | ||
83215812 | 158 | u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x, int cpuid, u32 offset) |
b47ef92a MZ |
159 | { |
160 | offset >>= 2; | |
161 | if (!offset) | |
c1bfb577 | 162 | return (u32 *)(x->private + cpuid) + REG_OFFSET_SWIZZLE; |
b47ef92a | 163 | else |
c1bfb577 | 164 | return (u32 *)(x->shared) + ((offset - 1) ^ REG_OFFSET_SWIZZLE); |
b47ef92a MZ |
165 | } |
166 | ||
167 | static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x, | |
168 | int cpuid, int irq) | |
169 | { | |
170 | if (irq < VGIC_NR_PRIVATE_IRQS) | |
c1bfb577 | 171 | return test_bit(irq, x->private + cpuid); |
b47ef92a | 172 | |
c1bfb577 | 173 | return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared); |
b47ef92a MZ |
174 | } |
175 | ||
83215812 AP |
176 | void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid, |
177 | int irq, int val) | |
b47ef92a MZ |
178 | { |
179 | unsigned long *reg; | |
180 | ||
181 | if (irq < VGIC_NR_PRIVATE_IRQS) { | |
c1bfb577 | 182 | reg = x->private + cpuid; |
b47ef92a | 183 | } else { |
c1bfb577 | 184 | reg = x->shared; |
b47ef92a MZ |
185 | irq -= VGIC_NR_PRIVATE_IRQS; |
186 | } | |
187 | ||
188 | if (val) | |
189 | set_bit(irq, reg); | |
190 | else | |
191 | clear_bit(irq, reg); | |
192 | } | |
193 | ||
194 | static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid) | |
195 | { | |
c1bfb577 | 196 | return x->private + cpuid; |
b47ef92a MZ |
197 | } |
198 | ||
83215812 | 199 | unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x) |
b47ef92a | 200 | { |
c1bfb577 MZ |
201 | return x->shared; |
202 | } | |
203 | ||
204 | static int vgic_init_bytemap(struct vgic_bytemap *x, int nr_cpus, int nr_irqs) | |
205 | { | |
206 | int size; | |
207 | ||
208 | size = nr_cpus * VGIC_NR_PRIVATE_IRQS; | |
209 | size += nr_irqs - VGIC_NR_PRIVATE_IRQS; | |
210 | ||
211 | x->private = kzalloc(size, GFP_KERNEL); | |
212 | if (!x->private) | |
213 | return -ENOMEM; | |
214 | ||
215 | x->shared = x->private + nr_cpus * VGIC_NR_PRIVATE_IRQS / sizeof(u32); | |
216 | return 0; | |
217 | } | |
218 | ||
219 | static void vgic_free_bytemap(struct vgic_bytemap *b) | |
220 | { | |
221 | kfree(b->private); | |
222 | b->private = NULL; | |
223 | b->shared = NULL; | |
b47ef92a MZ |
224 | } |
225 | ||
83215812 | 226 | u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset) |
b47ef92a | 227 | { |
c1bfb577 MZ |
228 | u32 *reg; |
229 | ||
230 | if (offset < VGIC_NR_PRIVATE_IRQS) { | |
231 | reg = x->private; | |
232 | offset += cpuid * VGIC_NR_PRIVATE_IRQS; | |
233 | } else { | |
234 | reg = x->shared; | |
235 | offset -= VGIC_NR_PRIVATE_IRQS; | |
236 | } | |
237 | ||
238 | return reg + (offset / sizeof(u32)); | |
b47ef92a MZ |
239 | } |
240 | ||
241 | #define VGIC_CFG_LEVEL 0 | |
242 | #define VGIC_CFG_EDGE 1 | |
243 | ||
244 | static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq) | |
245 | { | |
246 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
247 | int irq_val; | |
248 | ||
249 | irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq); | |
250 | return irq_val == VGIC_CFG_EDGE; | |
251 | } | |
252 | ||
253 | static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq) | |
254 | { | |
255 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
256 | ||
257 | return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq); | |
258 | } | |
259 | ||
dbf20f9d | 260 | static int vgic_irq_is_queued(struct kvm_vcpu *vcpu, int irq) |
9d949dce MZ |
261 | { |
262 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
263 | ||
dbf20f9d | 264 | return vgic_bitmap_get_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq); |
9d949dce MZ |
265 | } |
266 | ||
dbf20f9d | 267 | static void vgic_irq_set_queued(struct kvm_vcpu *vcpu, int irq) |
9d949dce MZ |
268 | { |
269 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
270 | ||
dbf20f9d | 271 | vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 1); |
9d949dce MZ |
272 | } |
273 | ||
dbf20f9d | 274 | static void vgic_irq_clear_queued(struct kvm_vcpu *vcpu, int irq) |
9d949dce MZ |
275 | { |
276 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
277 | ||
dbf20f9d | 278 | vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 0); |
9d949dce MZ |
279 | } |
280 | ||
faa1b46c CD |
281 | static int vgic_dist_irq_get_level(struct kvm_vcpu *vcpu, int irq) |
282 | { | |
283 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
284 | ||
285 | return vgic_bitmap_get_irq_val(&dist->irq_level, vcpu->vcpu_id, irq); | |
286 | } | |
287 | ||
288 | static void vgic_dist_irq_set_level(struct kvm_vcpu *vcpu, int irq) | |
289 | { | |
290 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
291 | ||
292 | vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 1); | |
293 | } | |
294 | ||
295 | static void vgic_dist_irq_clear_level(struct kvm_vcpu *vcpu, int irq) | |
296 | { | |
297 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
298 | ||
299 | vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 0); | |
300 | } | |
301 | ||
302 | static int vgic_dist_irq_soft_pend(struct kvm_vcpu *vcpu, int irq) | |
303 | { | |
304 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
305 | ||
306 | return vgic_bitmap_get_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq); | |
307 | } | |
308 | ||
309 | static void vgic_dist_irq_clear_soft_pend(struct kvm_vcpu *vcpu, int irq) | |
310 | { | |
311 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
312 | ||
313 | vgic_bitmap_set_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq, 0); | |
314 | } | |
315 | ||
9d949dce MZ |
316 | static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq) |
317 | { | |
318 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
319 | ||
227844f5 | 320 | return vgic_bitmap_get_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq); |
9d949dce MZ |
321 | } |
322 | ||
83215812 | 323 | void vgic_dist_irq_set_pending(struct kvm_vcpu *vcpu, int irq) |
b47ef92a MZ |
324 | { |
325 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
326 | ||
227844f5 | 327 | vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 1); |
b47ef92a MZ |
328 | } |
329 | ||
83215812 | 330 | void vgic_dist_irq_clear_pending(struct kvm_vcpu *vcpu, int irq) |
b47ef92a MZ |
331 | { |
332 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
333 | ||
227844f5 | 334 | vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 0); |
b47ef92a MZ |
335 | } |
336 | ||
337 | static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq) | |
338 | { | |
339 | if (irq < VGIC_NR_PRIVATE_IRQS) | |
340 | set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu); | |
341 | else | |
342 | set_bit(irq - VGIC_NR_PRIVATE_IRQS, | |
343 | vcpu->arch.vgic_cpu.pending_shared); | |
344 | } | |
345 | ||
83215812 | 346 | void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq) |
b47ef92a MZ |
347 | { |
348 | if (irq < VGIC_NR_PRIVATE_IRQS) | |
349 | clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu); | |
350 | else | |
351 | clear_bit(irq - VGIC_NR_PRIVATE_IRQS, | |
352 | vcpu->arch.vgic_cpu.pending_shared); | |
353 | } | |
354 | ||
dbf20f9d CD |
355 | static bool vgic_can_sample_irq(struct kvm_vcpu *vcpu, int irq) |
356 | { | |
357 | return vgic_irq_is_edge(vcpu, irq) || !vgic_irq_is_queued(vcpu, irq); | |
358 | } | |
359 | ||
1a89dd91 MZ |
360 | /** |
361 | * vgic_reg_access - access vgic register | |
362 | * @mmio: pointer to the data describing the mmio access | |
363 | * @reg: pointer to the virtual backing of vgic distributor data | |
364 | * @offset: least significant 2 bits used for word offset | |
365 | * @mode: ACCESS_ mode (see defines above) | |
366 | * | |
367 | * Helper to make vgic register access easier using one of the access | |
368 | * modes defined for vgic register access | |
369 | * (read,raz,write-ignored,setbit,clearbit,write) | |
370 | */ | |
83215812 AP |
371 | void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg, |
372 | phys_addr_t offset, int mode) | |
1a89dd91 MZ |
373 | { |
374 | int word_offset = (offset & 3) * 8; | |
375 | u32 mask = (1UL << (mmio->len * 8)) - 1; | |
376 | u32 regval; | |
377 | ||
378 | /* | |
379 | * Any alignment fault should have been delivered to the guest | |
380 | * directly (ARM ARM B3.12.7 "Prioritization of aborts"). | |
381 | */ | |
382 | ||
383 | if (reg) { | |
384 | regval = *reg; | |
385 | } else { | |
386 | BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED)); | |
387 | regval = 0; | |
388 | } | |
389 | ||
390 | if (mmio->is_write) { | |
391 | u32 data = mmio_data_read(mmio, mask) << word_offset; | |
392 | switch (ACCESS_WRITE_MASK(mode)) { | |
393 | case ACCESS_WRITE_IGNORED: | |
394 | return; | |
395 | ||
396 | case ACCESS_WRITE_SETBIT: | |
397 | regval |= data; | |
398 | break; | |
399 | ||
400 | case ACCESS_WRITE_CLEARBIT: | |
401 | regval &= ~data; | |
402 | break; | |
403 | ||
404 | case ACCESS_WRITE_VALUE: | |
405 | regval = (regval & ~(mask << word_offset)) | data; | |
406 | break; | |
407 | } | |
408 | *reg = regval; | |
409 | } else { | |
410 | switch (ACCESS_READ_MASK(mode)) { | |
411 | case ACCESS_READ_RAZ: | |
412 | regval = 0; | |
413 | /* fall through */ | |
414 | ||
415 | case ACCESS_READ_VALUE: | |
416 | mmio_data_write(mmio, mask, regval >> word_offset); | |
417 | } | |
418 | } | |
419 | } | |
420 | ||
83215812 AP |
421 | bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio, |
422 | phys_addr_t offset) | |
b47ef92a MZ |
423 | { |
424 | vgic_reg_access(mmio, NULL, offset, | |
425 | ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED); | |
426 | return false; | |
427 | } | |
428 | ||
83215812 AP |
429 | bool vgic_handle_enable_reg(struct kvm *kvm, struct kvm_exit_mmio *mmio, |
430 | phys_addr_t offset, int vcpu_id, int access) | |
b47ef92a | 431 | { |
d97f683d AP |
432 | u32 *reg; |
433 | int mode = ACCESS_READ_VALUE | access; | |
434 | struct kvm_vcpu *target_vcpu = kvm_get_vcpu(kvm, vcpu_id); | |
435 | ||
436 | reg = vgic_bitmap_get_reg(&kvm->arch.vgic.irq_enabled, vcpu_id, offset); | |
437 | vgic_reg_access(mmio, reg, offset, mode); | |
b47ef92a | 438 | if (mmio->is_write) { |
d97f683d AP |
439 | if (access & ACCESS_WRITE_CLEARBIT) { |
440 | if (offset < 4) /* Force SGI enabled */ | |
441 | *reg |= 0xffff; | |
442 | vgic_retire_disabled_irqs(target_vcpu); | |
443 | } | |
444 | vgic_update_state(kvm); | |
b47ef92a MZ |
445 | return true; |
446 | } | |
447 | ||
448 | return false; | |
449 | } | |
450 | ||
83215812 AP |
451 | bool vgic_handle_set_pending_reg(struct kvm *kvm, |
452 | struct kvm_exit_mmio *mmio, | |
453 | phys_addr_t offset, int vcpu_id) | |
b47ef92a | 454 | { |
9da48b55 | 455 | u32 *reg, orig; |
faa1b46c | 456 | u32 level_mask; |
d97f683d AP |
457 | int mode = ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT; |
458 | struct vgic_dist *dist = &kvm->arch.vgic; | |
faa1b46c | 459 | |
d97f683d | 460 | reg = vgic_bitmap_get_reg(&dist->irq_cfg, vcpu_id, offset); |
faa1b46c CD |
461 | level_mask = (~(*reg)); |
462 | ||
463 | /* Mark both level and edge triggered irqs as pending */ | |
d97f683d | 464 | reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset); |
9da48b55 | 465 | orig = *reg; |
d97f683d | 466 | vgic_reg_access(mmio, reg, offset, mode); |
faa1b46c | 467 | |
b47ef92a | 468 | if (mmio->is_write) { |
faa1b46c CD |
469 | /* Set the soft-pending flag only for level-triggered irqs */ |
470 | reg = vgic_bitmap_get_reg(&dist->irq_soft_pend, | |
d97f683d AP |
471 | vcpu_id, offset); |
472 | vgic_reg_access(mmio, reg, offset, mode); | |
faa1b46c CD |
473 | *reg &= level_mask; |
474 | ||
9da48b55 CD |
475 | /* Ignore writes to SGIs */ |
476 | if (offset < 2) { | |
477 | *reg &= ~0xffff; | |
478 | *reg |= orig & 0xffff; | |
479 | } | |
480 | ||
d97f683d | 481 | vgic_update_state(kvm); |
b47ef92a MZ |
482 | return true; |
483 | } | |
484 | ||
485 | return false; | |
486 | } | |
487 | ||
83215812 AP |
488 | bool vgic_handle_clear_pending_reg(struct kvm *kvm, |
489 | struct kvm_exit_mmio *mmio, | |
490 | phys_addr_t offset, int vcpu_id) | |
b47ef92a | 491 | { |
faa1b46c | 492 | u32 *level_active; |
9da48b55 | 493 | u32 *reg, orig; |
d97f683d AP |
494 | int mode = ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT; |
495 | struct vgic_dist *dist = &kvm->arch.vgic; | |
faa1b46c | 496 | |
d97f683d | 497 | reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset); |
9da48b55 | 498 | orig = *reg; |
d97f683d | 499 | vgic_reg_access(mmio, reg, offset, mode); |
b47ef92a | 500 | if (mmio->is_write) { |
faa1b46c CD |
501 | /* Re-set level triggered level-active interrupts */ |
502 | level_active = vgic_bitmap_get_reg(&dist->irq_level, | |
d97f683d AP |
503 | vcpu_id, offset); |
504 | reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset); | |
faa1b46c CD |
505 | *reg |= *level_active; |
506 | ||
9da48b55 CD |
507 | /* Ignore writes to SGIs */ |
508 | if (offset < 2) { | |
509 | *reg &= ~0xffff; | |
510 | *reg |= orig & 0xffff; | |
511 | } | |
512 | ||
faa1b46c CD |
513 | /* Clear soft-pending flags */ |
514 | reg = vgic_bitmap_get_reg(&dist->irq_soft_pend, | |
d97f683d AP |
515 | vcpu_id, offset); |
516 | vgic_reg_access(mmio, reg, offset, mode); | |
faa1b46c | 517 | |
d97f683d | 518 | vgic_update_state(kvm); |
b47ef92a MZ |
519 | return true; |
520 | } | |
b47ef92a MZ |
521 | return false; |
522 | } | |
523 | ||
b47ef92a MZ |
524 | static u32 vgic_cfg_expand(u16 val) |
525 | { | |
526 | u32 res = 0; | |
527 | int i; | |
528 | ||
529 | /* | |
530 | * Turn a 16bit value like abcd...mnop into a 32bit word | |
531 | * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is. | |
532 | */ | |
533 | for (i = 0; i < 16; i++) | |
534 | res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1); | |
535 | ||
536 | return res; | |
537 | } | |
538 | ||
539 | static u16 vgic_cfg_compress(u32 val) | |
540 | { | |
541 | u16 res = 0; | |
542 | int i; | |
543 | ||
544 | /* | |
545 | * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like | |
546 | * abcd...mnop which is what we really care about. | |
547 | */ | |
548 | for (i = 0; i < 16; i++) | |
549 | res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i; | |
550 | ||
551 | return res; | |
552 | } | |
553 | ||
554 | /* | |
555 | * The distributor uses 2 bits per IRQ for the CFG register, but the | |
556 | * LSB is always 0. As such, we only keep the upper bit, and use the | |
557 | * two above functions to compress/expand the bits | |
558 | */ | |
83215812 AP |
559 | bool vgic_handle_cfg_reg(u32 *reg, struct kvm_exit_mmio *mmio, |
560 | phys_addr_t offset) | |
b47ef92a MZ |
561 | { |
562 | u32 val; | |
6545eae3 | 563 | |
f2ae85b2 | 564 | if (offset & 4) |
b47ef92a MZ |
565 | val = *reg >> 16; |
566 | else | |
567 | val = *reg & 0xffff; | |
568 | ||
569 | val = vgic_cfg_expand(val); | |
570 | vgic_reg_access(mmio, &val, offset, | |
571 | ACCESS_READ_VALUE | ACCESS_WRITE_VALUE); | |
572 | if (mmio->is_write) { | |
f2ae85b2 | 573 | if (offset < 8) { |
b47ef92a MZ |
574 | *reg = ~0U; /* Force PPIs/SGIs to 1 */ |
575 | return false; | |
576 | } | |
577 | ||
578 | val = vgic_cfg_compress(val); | |
f2ae85b2 | 579 | if (offset & 4) { |
b47ef92a MZ |
580 | *reg &= 0xffff; |
581 | *reg |= val << 16; | |
582 | } else { | |
583 | *reg &= 0xffff << 16; | |
584 | *reg |= val; | |
585 | } | |
586 | } | |
587 | ||
588 | return false; | |
589 | } | |
590 | ||
cbd333a4 CD |
591 | /** |
592 | * vgic_unqueue_irqs - move pending IRQs from LRs to the distributor | |
593 | * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs | |
594 | * | |
595 | * Move any pending IRQs that have already been assigned to LRs back to the | |
596 | * emulated distributor state so that the complete emulated state can be read | |
597 | * from the main emulation structures without investigating the LRs. | |
598 | * | |
599 | * Note that IRQs in the active state in the LRs get their pending state moved | |
600 | * to the distributor but the active state stays in the LRs, because we don't | |
601 | * track the active state on the distributor side. | |
602 | */ | |
83215812 | 603 | void vgic_unqueue_irqs(struct kvm_vcpu *vcpu) |
cbd333a4 | 604 | { |
cbd333a4 | 605 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
8d5c6b06 | 606 | int i; |
cbd333a4 CD |
607 | |
608 | for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) { | |
8d5c6b06 | 609 | struct vgic_lr lr = vgic_get_lr(vcpu, i); |
cbd333a4 CD |
610 | |
611 | /* | |
612 | * There are three options for the state bits: | |
613 | * | |
614 | * 01: pending | |
615 | * 10: active | |
616 | * 11: pending and active | |
617 | * | |
618 | * If the LR holds only an active interrupt (not pending) then | |
619 | * just leave it alone. | |
620 | */ | |
8d5c6b06 | 621 | if ((lr.state & LR_STATE_MASK) == LR_STATE_ACTIVE) |
cbd333a4 CD |
622 | continue; |
623 | ||
624 | /* | |
625 | * Reestablish the pending state on the distributor and the | |
626 | * CPU interface. It may have already been pending, but that | |
627 | * is fine, then we are only setting a few bits that were | |
628 | * already set. | |
629 | */ | |
227844f5 | 630 | vgic_dist_irq_set_pending(vcpu, lr.irq); |
8d5c6b06 | 631 | if (lr.irq < VGIC_NR_SGIS) |
b26e5fda | 632 | add_sgi_source(vcpu, lr.irq, lr.source); |
8d5c6b06 MZ |
633 | lr.state &= ~LR_STATE_PENDING; |
634 | vgic_set_lr(vcpu, i, lr); | |
cbd333a4 CD |
635 | |
636 | /* | |
637 | * If there's no state left on the LR (it could still be | |
638 | * active), then the LR does not hold any useful info and can | |
639 | * be marked as free for other use. | |
640 | */ | |
cced50c9 | 641 | if (!(lr.state & LR_STATE_MASK)) { |
8d5c6b06 | 642 | vgic_retire_lr(i, lr.irq, vcpu); |
cced50c9 CD |
643 | vgic_irq_clear_queued(vcpu, lr.irq); |
644 | } | |
cbd333a4 CD |
645 | |
646 | /* Finally update the VGIC state. */ | |
647 | vgic_update_state(vcpu->kvm); | |
648 | } | |
649 | } | |
650 | ||
83215812 AP |
651 | const |
652 | struct kvm_mmio_range *vgic_find_range(const struct kvm_mmio_range *ranges, | |
1a89dd91 | 653 | struct kvm_exit_mmio *mmio, |
1006e8cb | 654 | phys_addr_t offset) |
1a89dd91 | 655 | { |
83215812 | 656 | const struct kvm_mmio_range *r = ranges; |
1a89dd91 MZ |
657 | |
658 | while (r->len) { | |
1006e8cb CD |
659 | if (offset >= r->base && |
660 | (offset + mmio->len) <= (r->base + r->len)) | |
1a89dd91 MZ |
661 | return r; |
662 | r++; | |
663 | } | |
664 | ||
665 | return NULL; | |
666 | } | |
667 | ||
c3c91836 | 668 | static bool vgic_validate_access(const struct vgic_dist *dist, |
83215812 | 669 | const struct kvm_mmio_range *range, |
c3c91836 MZ |
670 | unsigned long offset) |
671 | { | |
672 | int irq; | |
673 | ||
674 | if (!range->bits_per_irq) | |
675 | return true; /* Not an irq-based access */ | |
676 | ||
677 | irq = offset * 8 / range->bits_per_irq; | |
678 | if (irq >= dist->nr_irqs) | |
679 | return false; | |
680 | ||
681 | return true; | |
682 | } | |
683 | ||
05bc8aaf AP |
684 | /* |
685 | * Call the respective handler function for the given range. | |
686 | * We split up any 64 bit accesses into two consecutive 32 bit | |
687 | * handler calls and merge the result afterwards. | |
688 | * We do this in a little endian fashion regardless of the host's | |
689 | * or guest's endianness, because the GIC is always LE and the rest of | |
690 | * the code (vgic_reg_access) also puts it in a LE fashion already. | |
691 | * At this point we have already identified the handle function, so | |
692 | * range points to that one entry and offset is relative to this. | |
693 | */ | |
694 | static bool call_range_handler(struct kvm_vcpu *vcpu, | |
695 | struct kvm_exit_mmio *mmio, | |
696 | unsigned long offset, | |
83215812 | 697 | const struct kvm_mmio_range *range) |
05bc8aaf AP |
698 | { |
699 | u32 *data32 = (void *)mmio->data; | |
700 | struct kvm_exit_mmio mmio32; | |
701 | bool ret; | |
702 | ||
703 | if (likely(mmio->len <= 4)) | |
704 | return range->handle_mmio(vcpu, mmio, offset); | |
705 | ||
706 | /* | |
707 | * Any access bigger than 4 bytes (that we currently handle in KVM) | |
708 | * is actually 8 bytes long, caused by a 64-bit access | |
709 | */ | |
710 | ||
711 | mmio32.len = 4; | |
712 | mmio32.is_write = mmio->is_write; | |
9fedf146 | 713 | mmio32.private = mmio->private; |
05bc8aaf AP |
714 | |
715 | mmio32.phys_addr = mmio->phys_addr + 4; | |
716 | if (mmio->is_write) | |
717 | *(u32 *)mmio32.data = data32[1]; | |
718 | ret = range->handle_mmio(vcpu, &mmio32, offset + 4); | |
719 | if (!mmio->is_write) | |
720 | data32[1] = *(u32 *)mmio32.data; | |
721 | ||
722 | mmio32.phys_addr = mmio->phys_addr; | |
723 | if (mmio->is_write) | |
724 | *(u32 *)mmio32.data = data32[0]; | |
725 | ret |= range->handle_mmio(vcpu, &mmio32, offset); | |
726 | if (!mmio->is_write) | |
727 | data32[0] = *(u32 *)mmio32.data; | |
728 | ||
729 | return ret; | |
730 | } | |
731 | ||
1a89dd91 | 732 | /** |
96415257 | 733 | * vgic_handle_mmio_range - handle an in-kernel MMIO access |
1a89dd91 MZ |
734 | * @vcpu: pointer to the vcpu performing the access |
735 | * @run: pointer to the kvm_run structure | |
736 | * @mmio: pointer to the data describing the access | |
96415257 AP |
737 | * @ranges: array of MMIO ranges in a given region |
738 | * @mmio_base: base address of that region | |
1a89dd91 | 739 | * |
96415257 | 740 | * returns true if the MMIO access could be performed |
1a89dd91 | 741 | */ |
83215812 | 742 | bool vgic_handle_mmio_range(struct kvm_vcpu *vcpu, struct kvm_run *run, |
96415257 | 743 | struct kvm_exit_mmio *mmio, |
83215812 | 744 | const struct kvm_mmio_range *ranges, |
96415257 | 745 | unsigned long mmio_base) |
1a89dd91 | 746 | { |
83215812 | 747 | const struct kvm_mmio_range *range; |
b47ef92a | 748 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
b47ef92a MZ |
749 | bool updated_state; |
750 | unsigned long offset; | |
751 | ||
96415257 | 752 | offset = mmio->phys_addr - mmio_base; |
83215812 | 753 | range = vgic_find_range(ranges, mmio, offset); |
b47ef92a MZ |
754 | if (unlikely(!range || !range->handle_mmio)) { |
755 | pr_warn("Unhandled access %d %08llx %d\n", | |
756 | mmio->is_write, mmio->phys_addr, mmio->len); | |
757 | return false; | |
758 | } | |
759 | ||
760 | spin_lock(&vcpu->kvm->arch.vgic.lock); | |
96415257 | 761 | offset -= range->base; |
c3c91836 | 762 | if (vgic_validate_access(dist, range, offset)) { |
05bc8aaf | 763 | updated_state = call_range_handler(vcpu, mmio, offset, range); |
c3c91836 | 764 | } else { |
05bc8aaf AP |
765 | if (!mmio->is_write) |
766 | memset(mmio->data, 0, mmio->len); | |
c3c91836 MZ |
767 | updated_state = false; |
768 | } | |
b47ef92a MZ |
769 | spin_unlock(&vcpu->kvm->arch.vgic.lock); |
770 | kvm_prepare_mmio(run, mmio); | |
771 | kvm_handle_mmio_return(vcpu, run); | |
772 | ||
5863c2ce MZ |
773 | if (updated_state) |
774 | vgic_kick_vcpus(vcpu->kvm); | |
775 | ||
b47ef92a MZ |
776 | return true; |
777 | } | |
778 | ||
96415257 AP |
779 | /** |
780 | * vgic_handle_mmio - handle an in-kernel MMIO access for the GIC emulation | |
781 | * @vcpu: pointer to the vcpu performing the access | |
782 | * @run: pointer to the kvm_run structure | |
783 | * @mmio: pointer to the data describing the access | |
784 | * | |
785 | * returns true if the MMIO access has been performed in kernel space, | |
786 | * and false if it needs to be emulated in user space. | |
b26e5fda | 787 | * Calls the actual handling routine for the selected VGIC model. |
96415257 AP |
788 | */ |
789 | bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run, | |
790 | struct kvm_exit_mmio *mmio) | |
791 | { | |
792 | if (!irqchip_in_kernel(vcpu->kvm)) | |
793 | return false; | |
794 | ||
b26e5fda AP |
795 | /* |
796 | * This will currently call either vgic_v2_handle_mmio() or | |
797 | * vgic_v3_handle_mmio(), which in turn will call | |
798 | * vgic_handle_mmio_range() defined above. | |
799 | */ | |
800 | return vcpu->kvm->arch.vgic.vm_ops.handle_mmio(vcpu, run, mmio); | |
96415257 AP |
801 | } |
802 | ||
fb65ab63 MZ |
803 | static int vgic_nr_shared_irqs(struct vgic_dist *dist) |
804 | { | |
805 | return dist->nr_irqs - VGIC_NR_PRIVATE_IRQS; | |
806 | } | |
807 | ||
b47ef92a MZ |
808 | static int compute_pending_for_cpu(struct kvm_vcpu *vcpu) |
809 | { | |
9d949dce MZ |
810 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
811 | unsigned long *pending, *enabled, *pend_percpu, *pend_shared; | |
812 | unsigned long pending_private, pending_shared; | |
fb65ab63 | 813 | int nr_shared = vgic_nr_shared_irqs(dist); |
9d949dce MZ |
814 | int vcpu_id; |
815 | ||
816 | vcpu_id = vcpu->vcpu_id; | |
817 | pend_percpu = vcpu->arch.vgic_cpu.pending_percpu; | |
818 | pend_shared = vcpu->arch.vgic_cpu.pending_shared; | |
819 | ||
227844f5 | 820 | pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id); |
9d949dce MZ |
821 | enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id); |
822 | bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS); | |
823 | ||
227844f5 | 824 | pending = vgic_bitmap_get_shared_map(&dist->irq_pending); |
9d949dce | 825 | enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled); |
fb65ab63 | 826 | bitmap_and(pend_shared, pending, enabled, nr_shared); |
9d949dce MZ |
827 | bitmap_and(pend_shared, pend_shared, |
828 | vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]), | |
fb65ab63 | 829 | nr_shared); |
9d949dce MZ |
830 | |
831 | pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS); | |
fb65ab63 | 832 | pending_shared = find_first_bit(pend_shared, nr_shared); |
9d949dce | 833 | return (pending_private < VGIC_NR_PRIVATE_IRQS || |
fb65ab63 | 834 | pending_shared < vgic_nr_shared_irqs(dist)); |
b47ef92a MZ |
835 | } |
836 | ||
837 | /* | |
838 | * Update the interrupt state and determine which CPUs have pending | |
839 | * interrupts. Must be called with distributor lock held. | |
840 | */ | |
83215812 | 841 | void vgic_update_state(struct kvm *kvm) |
b47ef92a MZ |
842 | { |
843 | struct vgic_dist *dist = &kvm->arch.vgic; | |
844 | struct kvm_vcpu *vcpu; | |
845 | int c; | |
846 | ||
847 | if (!dist->enabled) { | |
c1bfb577 | 848 | set_bit(0, dist->irq_pending_on_cpu); |
b47ef92a MZ |
849 | return; |
850 | } | |
851 | ||
852 | kvm_for_each_vcpu(c, vcpu, kvm) { | |
853 | if (compute_pending_for_cpu(vcpu)) { | |
854 | pr_debug("CPU%d has pending interrupts\n", c); | |
c1bfb577 | 855 | set_bit(c, dist->irq_pending_on_cpu); |
b47ef92a MZ |
856 | } |
857 | } | |
1a89dd91 | 858 | } |
330690cd | 859 | |
8d5c6b06 MZ |
860 | static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr) |
861 | { | |
8f186d52 | 862 | return vgic_ops->get_lr(vcpu, lr); |
8d5c6b06 MZ |
863 | } |
864 | ||
865 | static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, | |
866 | struct vgic_lr vlr) | |
867 | { | |
8f186d52 | 868 | vgic_ops->set_lr(vcpu, lr, vlr); |
8d5c6b06 MZ |
869 | } |
870 | ||
69bb2c9f MZ |
871 | static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr, |
872 | struct vgic_lr vlr) | |
873 | { | |
8f186d52 | 874 | vgic_ops->sync_lr_elrsr(vcpu, lr, vlr); |
69bb2c9f MZ |
875 | } |
876 | ||
877 | static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu) | |
878 | { | |
8f186d52 | 879 | return vgic_ops->get_elrsr(vcpu); |
69bb2c9f MZ |
880 | } |
881 | ||
8d6a0313 MZ |
882 | static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu) |
883 | { | |
8f186d52 | 884 | return vgic_ops->get_eisr(vcpu); |
8d6a0313 MZ |
885 | } |
886 | ||
495dd859 MZ |
887 | static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu) |
888 | { | |
8f186d52 | 889 | return vgic_ops->get_interrupt_status(vcpu); |
495dd859 MZ |
890 | } |
891 | ||
909d9b50 MZ |
892 | static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu) |
893 | { | |
8f186d52 | 894 | vgic_ops->enable_underflow(vcpu); |
909d9b50 MZ |
895 | } |
896 | ||
897 | static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu) | |
898 | { | |
8f186d52 | 899 | vgic_ops->disable_underflow(vcpu); |
909d9b50 MZ |
900 | } |
901 | ||
83215812 | 902 | void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr) |
beee38b9 | 903 | { |
8f186d52 | 904 | vgic_ops->get_vmcr(vcpu, vmcr); |
beee38b9 MZ |
905 | } |
906 | ||
83215812 | 907 | void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr) |
beee38b9 | 908 | { |
8f186d52 | 909 | vgic_ops->set_vmcr(vcpu, vmcr); |
beee38b9 MZ |
910 | } |
911 | ||
da8dafd1 MZ |
912 | static inline void vgic_enable(struct kvm_vcpu *vcpu) |
913 | { | |
8f186d52 | 914 | vgic_ops->enable(vcpu); |
da8dafd1 MZ |
915 | } |
916 | ||
8d5c6b06 MZ |
917 | static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu) |
918 | { | |
919 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; | |
920 | struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr); | |
921 | ||
922 | vlr.state = 0; | |
923 | vgic_set_lr(vcpu, lr_nr, vlr); | |
924 | clear_bit(lr_nr, vgic_cpu->lr_used); | |
925 | vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY; | |
926 | } | |
a1fcb44e MZ |
927 | |
928 | /* | |
929 | * An interrupt may have been disabled after being made pending on the | |
930 | * CPU interface (the classic case is a timer running while we're | |
931 | * rebooting the guest - the interrupt would kick as soon as the CPU | |
932 | * interface gets enabled, with deadly consequences). | |
933 | * | |
934 | * The solution is to examine already active LRs, and check the | |
935 | * interrupt is still enabled. If not, just retire it. | |
936 | */ | |
937 | static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu) | |
938 | { | |
939 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; | |
940 | int lr; | |
941 | ||
8f186d52 | 942 | for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) { |
8d5c6b06 | 943 | struct vgic_lr vlr = vgic_get_lr(vcpu, lr); |
a1fcb44e | 944 | |
8d5c6b06 MZ |
945 | if (!vgic_irq_is_enabled(vcpu, vlr.irq)) { |
946 | vgic_retire_lr(lr, vlr.irq, vcpu); | |
dbf20f9d CD |
947 | if (vgic_irq_is_queued(vcpu, vlr.irq)) |
948 | vgic_irq_clear_queued(vcpu, vlr.irq); | |
a1fcb44e MZ |
949 | } |
950 | } | |
951 | } | |
952 | ||
9d949dce MZ |
953 | /* |
954 | * Queue an interrupt to a CPU virtual interface. Return true on success, | |
955 | * or false if it wasn't possible to queue it. | |
1d916229 | 956 | * sgi_source must be zero for any non-SGI interrupts. |
9d949dce | 957 | */ |
83215812 | 958 | bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq) |
9d949dce MZ |
959 | { |
960 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; | |
5fb66da6 | 961 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
8d5c6b06 | 962 | struct vgic_lr vlr; |
9d949dce MZ |
963 | int lr; |
964 | ||
965 | /* Sanitize the input... */ | |
966 | BUG_ON(sgi_source_id & ~7); | |
967 | BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS); | |
5fb66da6 | 968 | BUG_ON(irq >= dist->nr_irqs); |
9d949dce MZ |
969 | |
970 | kvm_debug("Queue IRQ%d\n", irq); | |
971 | ||
972 | lr = vgic_cpu->vgic_irq_lr_map[irq]; | |
973 | ||
974 | /* Do we have an active interrupt for the same CPUID? */ | |
8d5c6b06 MZ |
975 | if (lr != LR_EMPTY) { |
976 | vlr = vgic_get_lr(vcpu, lr); | |
977 | if (vlr.source == sgi_source_id) { | |
978 | kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq); | |
979 | BUG_ON(!test_bit(lr, vgic_cpu->lr_used)); | |
980 | vlr.state |= LR_STATE_PENDING; | |
981 | vgic_set_lr(vcpu, lr, vlr); | |
982 | return true; | |
983 | } | |
9d949dce MZ |
984 | } |
985 | ||
986 | /* Try to use another LR for this interrupt */ | |
987 | lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used, | |
8f186d52 MZ |
988 | vgic->nr_lr); |
989 | if (lr >= vgic->nr_lr) | |
9d949dce MZ |
990 | return false; |
991 | ||
992 | kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id); | |
9d949dce MZ |
993 | vgic_cpu->vgic_irq_lr_map[irq] = lr; |
994 | set_bit(lr, vgic_cpu->lr_used); | |
995 | ||
8d5c6b06 MZ |
996 | vlr.irq = irq; |
997 | vlr.source = sgi_source_id; | |
998 | vlr.state = LR_STATE_PENDING; | |
9d949dce | 999 | if (!vgic_irq_is_edge(vcpu, irq)) |
8d5c6b06 MZ |
1000 | vlr.state |= LR_EOI_INT; |
1001 | ||
1002 | vgic_set_lr(vcpu, lr, vlr); | |
9d949dce MZ |
1003 | |
1004 | return true; | |
1005 | } | |
1006 | ||
9d949dce MZ |
1007 | static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq) |
1008 | { | |
dbf20f9d | 1009 | if (!vgic_can_sample_irq(vcpu, irq)) |
9d949dce MZ |
1010 | return true; /* level interrupt, already queued */ |
1011 | ||
1012 | if (vgic_queue_irq(vcpu, 0, irq)) { | |
1013 | if (vgic_irq_is_edge(vcpu, irq)) { | |
227844f5 | 1014 | vgic_dist_irq_clear_pending(vcpu, irq); |
9d949dce MZ |
1015 | vgic_cpu_irq_clear(vcpu, irq); |
1016 | } else { | |
dbf20f9d | 1017 | vgic_irq_set_queued(vcpu, irq); |
9d949dce MZ |
1018 | } |
1019 | ||
1020 | return true; | |
1021 | } | |
1022 | ||
1023 | return false; | |
1024 | } | |
1025 | ||
1026 | /* | |
1027 | * Fill the list registers with pending interrupts before running the | |
1028 | * guest. | |
1029 | */ | |
1030 | static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu) | |
1031 | { | |
1032 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; | |
1033 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
1034 | int i, vcpu_id; | |
1035 | int overflow = 0; | |
1036 | ||
1037 | vcpu_id = vcpu->vcpu_id; | |
1038 | ||
1039 | /* | |
1040 | * We may not have any pending interrupt, or the interrupts | |
1041 | * may have been serviced from another vcpu. In all cases, | |
1042 | * move along. | |
1043 | */ | |
1044 | if (!kvm_vgic_vcpu_pending_irq(vcpu)) { | |
1045 | pr_debug("CPU%d has no pending interrupt\n", vcpu_id); | |
1046 | goto epilog; | |
1047 | } | |
1048 | ||
1049 | /* SGIs */ | |
1050 | for_each_set_bit(i, vgic_cpu->pending_percpu, VGIC_NR_SGIS) { | |
b26e5fda | 1051 | if (!queue_sgi(vcpu, i)) |
9d949dce MZ |
1052 | overflow = 1; |
1053 | } | |
1054 | ||
1055 | /* PPIs */ | |
1056 | for_each_set_bit_from(i, vgic_cpu->pending_percpu, VGIC_NR_PRIVATE_IRQS) { | |
1057 | if (!vgic_queue_hwirq(vcpu, i)) | |
1058 | overflow = 1; | |
1059 | } | |
1060 | ||
1061 | /* SPIs */ | |
fb65ab63 | 1062 | for_each_set_bit(i, vgic_cpu->pending_shared, vgic_nr_shared_irqs(dist)) { |
9d949dce MZ |
1063 | if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS)) |
1064 | overflow = 1; | |
1065 | } | |
1066 | ||
1067 | epilog: | |
1068 | if (overflow) { | |
909d9b50 | 1069 | vgic_enable_underflow(vcpu); |
9d949dce | 1070 | } else { |
909d9b50 | 1071 | vgic_disable_underflow(vcpu); |
9d949dce MZ |
1072 | /* |
1073 | * We're about to run this VCPU, and we've consumed | |
1074 | * everything the distributor had in store for | |
1075 | * us. Claim we don't have anything pending. We'll | |
1076 | * adjust that if needed while exiting. | |
1077 | */ | |
c1bfb577 | 1078 | clear_bit(vcpu_id, dist->irq_pending_on_cpu); |
9d949dce MZ |
1079 | } |
1080 | } | |
1081 | ||
1082 | static bool vgic_process_maintenance(struct kvm_vcpu *vcpu) | |
1083 | { | |
495dd859 | 1084 | u32 status = vgic_get_interrupt_status(vcpu); |
649cf739 | 1085 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
9d949dce | 1086 | bool level_pending = false; |
174178fe | 1087 | struct kvm *kvm = vcpu->kvm; |
9d949dce | 1088 | |
495dd859 | 1089 | kvm_debug("STATUS = %08x\n", status); |
9d949dce | 1090 | |
495dd859 | 1091 | if (status & INT_STATUS_EOI) { |
9d949dce MZ |
1092 | /* |
1093 | * Some level interrupts have been EOIed. Clear their | |
1094 | * active bit. | |
1095 | */ | |
8d6a0313 | 1096 | u64 eisr = vgic_get_eisr(vcpu); |
2df36a5d | 1097 | unsigned long *eisr_ptr = u64_to_bitmask(&eisr); |
8d5c6b06 | 1098 | int lr; |
9d949dce | 1099 | |
8f186d52 | 1100 | for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) { |
8d5c6b06 | 1101 | struct vgic_lr vlr = vgic_get_lr(vcpu, lr); |
faa1b46c | 1102 | WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq)); |
9d949dce | 1103 | |
649cf739 | 1104 | spin_lock(&dist->lock); |
dbf20f9d | 1105 | vgic_irq_clear_queued(vcpu, vlr.irq); |
8d5c6b06 MZ |
1106 | WARN_ON(vlr.state & LR_STATE_MASK); |
1107 | vlr.state = 0; | |
1108 | vgic_set_lr(vcpu, lr, vlr); | |
9d949dce | 1109 | |
faa1b46c CD |
1110 | /* |
1111 | * If the IRQ was EOIed it was also ACKed and we we | |
1112 | * therefore assume we can clear the soft pending | |
1113 | * state (should it had been set) for this interrupt. | |
1114 | * | |
1115 | * Note: if the IRQ soft pending state was set after | |
1116 | * the IRQ was acked, it actually shouldn't be | |
1117 | * cleared, but we have no way of knowing that unless | |
1118 | * we start trapping ACKs when the soft-pending state | |
1119 | * is set. | |
1120 | */ | |
1121 | vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq); | |
1122 | ||
174178fe EA |
1123 | /* |
1124 | * kvm_notify_acked_irq calls kvm_set_irq() | |
1125 | * to reset the IRQ level. Need to release the | |
1126 | * lock for kvm_set_irq to grab it. | |
1127 | */ | |
1128 | spin_unlock(&dist->lock); | |
1129 | ||
1130 | kvm_notify_acked_irq(kvm, 0, | |
1131 | vlr.irq - VGIC_NR_PRIVATE_IRQS); | |
1132 | spin_lock(&dist->lock); | |
1133 | ||
9d949dce | 1134 | /* Any additional pending interrupt? */ |
faa1b46c | 1135 | if (vgic_dist_irq_get_level(vcpu, vlr.irq)) { |
8d5c6b06 | 1136 | vgic_cpu_irq_set(vcpu, vlr.irq); |
9d949dce MZ |
1137 | level_pending = true; |
1138 | } else { | |
faa1b46c | 1139 | vgic_dist_irq_clear_pending(vcpu, vlr.irq); |
8d5c6b06 | 1140 | vgic_cpu_irq_clear(vcpu, vlr.irq); |
9d949dce | 1141 | } |
75da01e1 | 1142 | |
649cf739 EA |
1143 | spin_unlock(&dist->lock); |
1144 | ||
75da01e1 MZ |
1145 | /* |
1146 | * Despite being EOIed, the LR may not have | |
1147 | * been marked as empty. | |
1148 | */ | |
69bb2c9f | 1149 | vgic_sync_lr_elrsr(vcpu, lr, vlr); |
9d949dce MZ |
1150 | } |
1151 | } | |
1152 | ||
495dd859 | 1153 | if (status & INT_STATUS_UNDERFLOW) |
909d9b50 | 1154 | vgic_disable_underflow(vcpu); |
9d949dce MZ |
1155 | |
1156 | return level_pending; | |
1157 | } | |
1158 | ||
649cf739 | 1159 | /* Sync back the VGIC state after a guest run */ |
9d949dce MZ |
1160 | static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu) |
1161 | { | |
1162 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; | |
1163 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
69bb2c9f MZ |
1164 | u64 elrsr; |
1165 | unsigned long *elrsr_ptr; | |
9d949dce MZ |
1166 | int lr, pending; |
1167 | bool level_pending; | |
1168 | ||
1169 | level_pending = vgic_process_maintenance(vcpu); | |
69bb2c9f | 1170 | elrsr = vgic_get_elrsr(vcpu); |
2df36a5d | 1171 | elrsr_ptr = u64_to_bitmask(&elrsr); |
9d949dce MZ |
1172 | |
1173 | /* Clear mappings for empty LRs */ | |
8f186d52 | 1174 | for_each_set_bit(lr, elrsr_ptr, vgic->nr_lr) { |
8d5c6b06 | 1175 | struct vgic_lr vlr; |
9d949dce MZ |
1176 | |
1177 | if (!test_and_clear_bit(lr, vgic_cpu->lr_used)) | |
1178 | continue; | |
1179 | ||
8d5c6b06 | 1180 | vlr = vgic_get_lr(vcpu, lr); |
9d949dce | 1181 | |
5fb66da6 | 1182 | BUG_ON(vlr.irq >= dist->nr_irqs); |
8d5c6b06 | 1183 | vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY; |
9d949dce MZ |
1184 | } |
1185 | ||
1186 | /* Check if we still have something up our sleeve... */ | |
8f186d52 MZ |
1187 | pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr); |
1188 | if (level_pending || pending < vgic->nr_lr) | |
c1bfb577 | 1189 | set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu); |
9d949dce MZ |
1190 | } |
1191 | ||
1192 | void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu) | |
1193 | { | |
1194 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
1195 | ||
1196 | if (!irqchip_in_kernel(vcpu->kvm)) | |
1197 | return; | |
1198 | ||
1199 | spin_lock(&dist->lock); | |
1200 | __kvm_vgic_flush_hwstate(vcpu); | |
1201 | spin_unlock(&dist->lock); | |
1202 | } | |
1203 | ||
1204 | void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu) | |
1205 | { | |
1206 | if (!irqchip_in_kernel(vcpu->kvm)) | |
1207 | return; | |
1208 | ||
1209 | __kvm_vgic_sync_hwstate(vcpu); | |
1210 | } | |
1211 | ||
1212 | int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu) | |
1213 | { | |
1214 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; | |
1215 | ||
1216 | if (!irqchip_in_kernel(vcpu->kvm)) | |
1217 | return 0; | |
1218 | ||
c1bfb577 | 1219 | return test_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu); |
9d949dce MZ |
1220 | } |
1221 | ||
83215812 | 1222 | void vgic_kick_vcpus(struct kvm *kvm) |
5863c2ce MZ |
1223 | { |
1224 | struct kvm_vcpu *vcpu; | |
1225 | int c; | |
1226 | ||
1227 | /* | |
1228 | * We've injected an interrupt, time to find out who deserves | |
1229 | * a good kick... | |
1230 | */ | |
1231 | kvm_for_each_vcpu(c, vcpu, kvm) { | |
1232 | if (kvm_vgic_vcpu_pending_irq(vcpu)) | |
1233 | kvm_vcpu_kick(vcpu); | |
1234 | } | |
1235 | } | |
1236 | ||
1237 | static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level) | |
1238 | { | |
227844f5 | 1239 | int edge_triggered = vgic_irq_is_edge(vcpu, irq); |
5863c2ce MZ |
1240 | |
1241 | /* | |
1242 | * Only inject an interrupt if: | |
1243 | * - edge triggered and we have a rising edge | |
1244 | * - level triggered and we change level | |
1245 | */ | |
faa1b46c CD |
1246 | if (edge_triggered) { |
1247 | int state = vgic_dist_irq_is_pending(vcpu, irq); | |
5863c2ce | 1248 | return level > state; |
faa1b46c CD |
1249 | } else { |
1250 | int state = vgic_dist_irq_get_level(vcpu, irq); | |
5863c2ce | 1251 | return level != state; |
faa1b46c | 1252 | } |
5863c2ce MZ |
1253 | } |
1254 | ||
016ed39c | 1255 | static int vgic_update_irq_pending(struct kvm *kvm, int cpuid, |
5863c2ce MZ |
1256 | unsigned int irq_num, bool level) |
1257 | { | |
1258 | struct vgic_dist *dist = &kvm->arch.vgic; | |
1259 | struct kvm_vcpu *vcpu; | |
227844f5 | 1260 | int edge_triggered, level_triggered; |
5863c2ce | 1261 | int enabled; |
a0675c25 | 1262 | bool ret = true, can_inject = true; |
5863c2ce MZ |
1263 | |
1264 | spin_lock(&dist->lock); | |
1265 | ||
1266 | vcpu = kvm_get_vcpu(kvm, cpuid); | |
227844f5 CD |
1267 | edge_triggered = vgic_irq_is_edge(vcpu, irq_num); |
1268 | level_triggered = !edge_triggered; | |
5863c2ce MZ |
1269 | |
1270 | if (!vgic_validate_injection(vcpu, irq_num, level)) { | |
1271 | ret = false; | |
1272 | goto out; | |
1273 | } | |
1274 | ||
1275 | if (irq_num >= VGIC_NR_PRIVATE_IRQS) { | |
1276 | cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS]; | |
a0675c25 AP |
1277 | if (cpuid == VCPU_NOT_ALLOCATED) { |
1278 | /* Pretend we use CPU0, and prevent injection */ | |
1279 | cpuid = 0; | |
1280 | can_inject = false; | |
1281 | } | |
5863c2ce MZ |
1282 | vcpu = kvm_get_vcpu(kvm, cpuid); |
1283 | } | |
1284 | ||
1285 | kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid); | |
1286 | ||
faa1b46c CD |
1287 | if (level) { |
1288 | if (level_triggered) | |
1289 | vgic_dist_irq_set_level(vcpu, irq_num); | |
227844f5 | 1290 | vgic_dist_irq_set_pending(vcpu, irq_num); |
faa1b46c CD |
1291 | } else { |
1292 | if (level_triggered) { | |
1293 | vgic_dist_irq_clear_level(vcpu, irq_num); | |
1294 | if (!vgic_dist_irq_soft_pend(vcpu, irq_num)) | |
1295 | vgic_dist_irq_clear_pending(vcpu, irq_num); | |
faa1b46c | 1296 | } |
7d39f9e3 | 1297 | |
1298 | ret = false; | |
1299 | goto out; | |
faa1b46c | 1300 | } |
5863c2ce MZ |
1301 | |
1302 | enabled = vgic_irq_is_enabled(vcpu, irq_num); | |
1303 | ||
a0675c25 | 1304 | if (!enabled || !can_inject) { |
5863c2ce MZ |
1305 | ret = false; |
1306 | goto out; | |
1307 | } | |
1308 | ||
dbf20f9d | 1309 | if (!vgic_can_sample_irq(vcpu, irq_num)) { |
5863c2ce MZ |
1310 | /* |
1311 | * Level interrupt in progress, will be picked up | |
1312 | * when EOId. | |
1313 | */ | |
1314 | ret = false; | |
1315 | goto out; | |
1316 | } | |
1317 | ||
1318 | if (level) { | |
1319 | vgic_cpu_irq_set(vcpu, irq_num); | |
c1bfb577 | 1320 | set_bit(cpuid, dist->irq_pending_on_cpu); |
5863c2ce MZ |
1321 | } |
1322 | ||
1323 | out: | |
1324 | spin_unlock(&dist->lock); | |
1325 | ||
016ed39c | 1326 | return ret ? cpuid : -EINVAL; |
5863c2ce MZ |
1327 | } |
1328 | ||
1329 | /** | |
1330 | * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic | |
1331 | * @kvm: The VM structure pointer | |
1332 | * @cpuid: The CPU for PPIs | |
1333 | * @irq_num: The IRQ number that is assigned to the device | |
1334 | * @level: Edge-triggered: true: to trigger the interrupt | |
1335 | * false: to ignore the call | |
1336 | * Level-sensitive true: activates an interrupt | |
1337 | * false: deactivates an interrupt | |
1338 | * | |
1339 | * The GIC is not concerned with devices being active-LOW or active-HIGH for | |
1340 | * level-sensitive interrupts. You can think of the level parameter as 1 | |
1341 | * being HIGH and 0 being LOW and all devices being active-HIGH. | |
1342 | */ | |
1343 | int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num, | |
1344 | bool level) | |
1345 | { | |
ca7d9c82 | 1346 | int ret = 0; |
016ed39c | 1347 | int vcpu_id; |
5863c2ce | 1348 | |
ca7d9c82 | 1349 | if (unlikely(!vgic_initialized(kvm))) { |
59892136 AP |
1350 | /* |
1351 | * We only provide the automatic initialization of the VGIC | |
1352 | * for the legacy case of a GICv2. Any other type must | |
1353 | * be explicitly initialized once setup with the respective | |
1354 | * KVM device call. | |
1355 | */ | |
1356 | if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2) { | |
1357 | ret = -EBUSY; | |
1358 | goto out; | |
1359 | } | |
ca7d9c82 CD |
1360 | mutex_lock(&kvm->lock); |
1361 | ret = vgic_init(kvm); | |
1362 | mutex_unlock(&kvm->lock); | |
1363 | ||
1364 | if (ret) | |
1365 | goto out; | |
016ed39c | 1366 | } |
5863c2ce | 1367 | |
ca7d9c82 CD |
1368 | vcpu_id = vgic_update_irq_pending(kvm, cpuid, irq_num, level); |
1369 | if (vcpu_id >= 0) { | |
1370 | /* kick the specified vcpu */ | |
1371 | kvm_vcpu_kick(kvm_get_vcpu(kvm, vcpu_id)); | |
1372 | } | |
1373 | ||
1374 | out: | |
1375 | return ret; | |
5863c2ce MZ |
1376 | } |
1377 | ||
01ac5e34 MZ |
1378 | static irqreturn_t vgic_maintenance_handler(int irq, void *data) |
1379 | { | |
1380 | /* | |
1381 | * We cannot rely on the vgic maintenance interrupt to be | |
1382 | * delivered synchronously. This means we can only use it to | |
1383 | * exit the VM, and we perform the handling of EOIed | |
1384 | * interrupts on the exit path (see vgic_process_maintenance). | |
1385 | */ | |
1386 | return IRQ_HANDLED; | |
1387 | } | |
1388 | ||
c1bfb577 MZ |
1389 | void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu) |
1390 | { | |
1391 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; | |
1392 | ||
1393 | kfree(vgic_cpu->pending_shared); | |
1394 | kfree(vgic_cpu->vgic_irq_lr_map); | |
1395 | vgic_cpu->pending_shared = NULL; | |
1396 | vgic_cpu->vgic_irq_lr_map = NULL; | |
1397 | } | |
1398 | ||
1399 | static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs) | |
1400 | { | |
1401 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; | |
1402 | ||
1403 | int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8; | |
1404 | vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL); | |
6d3cfbe2 | 1405 | vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL); |
c1bfb577 MZ |
1406 | |
1407 | if (!vgic_cpu->pending_shared || !vgic_cpu->vgic_irq_lr_map) { | |
1408 | kvm_vgic_vcpu_destroy(vcpu); | |
1409 | return -ENOMEM; | |
1410 | } | |
1411 | ||
6d3cfbe2 | 1412 | memset(vgic_cpu->vgic_irq_lr_map, LR_EMPTY, nr_irqs); |
01ac5e34 MZ |
1413 | |
1414 | /* | |
ca85f623 MZ |
1415 | * Store the number of LRs per vcpu, so we don't have to go |
1416 | * all the way to the distributor structure to find out. Only | |
1417 | * assembly code should use this one. | |
01ac5e34 | 1418 | */ |
8f186d52 | 1419 | vgic_cpu->nr_lr = vgic->nr_lr; |
01ac5e34 | 1420 | |
6d3cfbe2 | 1421 | return 0; |
01ac5e34 MZ |
1422 | } |
1423 | ||
3caa2d8c AP |
1424 | /** |
1425 | * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW | |
1426 | * | |
1427 | * The host's GIC naturally limits the maximum amount of VCPUs a guest | |
1428 | * can use. | |
1429 | */ | |
1430 | int kvm_vgic_get_max_vcpus(void) | |
1431 | { | |
1432 | return vgic->max_gic_vcpus; | |
1433 | } | |
1434 | ||
c1bfb577 MZ |
1435 | void kvm_vgic_destroy(struct kvm *kvm) |
1436 | { | |
1437 | struct vgic_dist *dist = &kvm->arch.vgic; | |
1438 | struct kvm_vcpu *vcpu; | |
1439 | int i; | |
1440 | ||
1441 | kvm_for_each_vcpu(i, vcpu, kvm) | |
1442 | kvm_vgic_vcpu_destroy(vcpu); | |
1443 | ||
1444 | vgic_free_bitmap(&dist->irq_enabled); | |
1445 | vgic_free_bitmap(&dist->irq_level); | |
1446 | vgic_free_bitmap(&dist->irq_pending); | |
1447 | vgic_free_bitmap(&dist->irq_soft_pend); | |
1448 | vgic_free_bitmap(&dist->irq_queued); | |
1449 | vgic_free_bitmap(&dist->irq_cfg); | |
1450 | vgic_free_bytemap(&dist->irq_priority); | |
1451 | if (dist->irq_spi_target) { | |
1452 | for (i = 0; i < dist->nr_cpus; i++) | |
1453 | vgic_free_bitmap(&dist->irq_spi_target[i]); | |
1454 | } | |
1455 | kfree(dist->irq_sgi_sources); | |
1456 | kfree(dist->irq_spi_cpu); | |
a0675c25 | 1457 | kfree(dist->irq_spi_mpidr); |
c1bfb577 MZ |
1458 | kfree(dist->irq_spi_target); |
1459 | kfree(dist->irq_pending_on_cpu); | |
1460 | dist->irq_sgi_sources = NULL; | |
1461 | dist->irq_spi_cpu = NULL; | |
1462 | dist->irq_spi_target = NULL; | |
1463 | dist->irq_pending_on_cpu = NULL; | |
1f57be28 | 1464 | dist->nr_cpus = 0; |
c1bfb577 MZ |
1465 | } |
1466 | ||
1467 | /* | |
1468 | * Allocate and initialize the various data structures. Must be called | |
1469 | * with kvm->lock held! | |
1470 | */ | |
83215812 | 1471 | int vgic_init(struct kvm *kvm) |
c1bfb577 MZ |
1472 | { |
1473 | struct vgic_dist *dist = &kvm->arch.vgic; | |
1474 | struct kvm_vcpu *vcpu; | |
1475 | int nr_cpus, nr_irqs; | |
6d3cfbe2 | 1476 | int ret, i, vcpu_id; |
c1bfb577 | 1477 | |
1f57be28 | 1478 | if (vgic_initialized(kvm)) |
4956f2bc MZ |
1479 | return 0; |
1480 | ||
1481 | nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus); | |
1482 | if (!nr_cpus) /* No vcpus? Can't be good... */ | |
66b030e4 | 1483 | return -ENODEV; |
5fb66da6 | 1484 | |
4956f2bc MZ |
1485 | /* |
1486 | * If nobody configured the number of interrupts, use the | |
1487 | * legacy one. | |
1488 | */ | |
5fb66da6 MZ |
1489 | if (!dist->nr_irqs) |
1490 | dist->nr_irqs = VGIC_NR_IRQS_LEGACY; | |
1491 | ||
1492 | nr_irqs = dist->nr_irqs; | |
c1bfb577 MZ |
1493 | |
1494 | ret = vgic_init_bitmap(&dist->irq_enabled, nr_cpus, nr_irqs); | |
1495 | ret |= vgic_init_bitmap(&dist->irq_level, nr_cpus, nr_irqs); | |
1496 | ret |= vgic_init_bitmap(&dist->irq_pending, nr_cpus, nr_irqs); | |
1497 | ret |= vgic_init_bitmap(&dist->irq_soft_pend, nr_cpus, nr_irqs); | |
1498 | ret |= vgic_init_bitmap(&dist->irq_queued, nr_cpus, nr_irqs); | |
1499 | ret |= vgic_init_bitmap(&dist->irq_cfg, nr_cpus, nr_irqs); | |
1500 | ret |= vgic_init_bytemap(&dist->irq_priority, nr_cpus, nr_irqs); | |
1501 | ||
1502 | if (ret) | |
1503 | goto out; | |
1504 | ||
1505 | dist->irq_sgi_sources = kzalloc(nr_cpus * VGIC_NR_SGIS, GFP_KERNEL); | |
1506 | dist->irq_spi_cpu = kzalloc(nr_irqs - VGIC_NR_PRIVATE_IRQS, GFP_KERNEL); | |
1507 | dist->irq_spi_target = kzalloc(sizeof(*dist->irq_spi_target) * nr_cpus, | |
1508 | GFP_KERNEL); | |
1509 | dist->irq_pending_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long), | |
1510 | GFP_KERNEL); | |
1511 | if (!dist->irq_sgi_sources || | |
1512 | !dist->irq_spi_cpu || | |
1513 | !dist->irq_spi_target || | |
1514 | !dist->irq_pending_on_cpu) { | |
1515 | ret = -ENOMEM; | |
1516 | goto out; | |
1517 | } | |
1518 | ||
1519 | for (i = 0; i < nr_cpus; i++) | |
1520 | ret |= vgic_init_bitmap(&dist->irq_spi_target[i], | |
1521 | nr_cpus, nr_irqs); | |
1522 | ||
1523 | if (ret) | |
1524 | goto out; | |
1525 | ||
b26e5fda AP |
1526 | ret = kvm->arch.vgic.vm_ops.init_model(kvm); |
1527 | if (ret) | |
1528 | goto out; | |
6d3cfbe2 PM |
1529 | |
1530 | kvm_for_each_vcpu(vcpu_id, vcpu, kvm) { | |
c1bfb577 MZ |
1531 | ret = vgic_vcpu_init_maps(vcpu, nr_irqs); |
1532 | if (ret) { | |
1533 | kvm_err("VGIC: Failed to allocate vcpu memory\n"); | |
1534 | break; | |
1535 | } | |
c1bfb577 | 1536 | |
6d3cfbe2 PM |
1537 | for (i = 0; i < dist->nr_irqs; i++) { |
1538 | if (i < VGIC_NR_PPIS) | |
1539 | vgic_bitmap_set_irq_val(&dist->irq_enabled, | |
1540 | vcpu->vcpu_id, i, 1); | |
1541 | if (i < VGIC_NR_PRIVATE_IRQS) | |
1542 | vgic_bitmap_set_irq_val(&dist->irq_cfg, | |
1543 | vcpu->vcpu_id, i, | |
1544 | VGIC_CFG_EDGE); | |
1545 | } | |
1546 | ||
1547 | vgic_enable(vcpu); | |
1548 | } | |
4956f2bc | 1549 | |
c1bfb577 MZ |
1550 | out: |
1551 | if (ret) | |
1552 | kvm_vgic_destroy(kvm); | |
1553 | ||
1554 | return ret; | |
1555 | } | |
1556 | ||
b26e5fda AP |
1557 | static int init_vgic_model(struct kvm *kvm, int type) |
1558 | { | |
1559 | switch (type) { | |
1560 | case KVM_DEV_TYPE_ARM_VGIC_V2: | |
1561 | vgic_v2_init_emulation(kvm); | |
1562 | break; | |
b5d84ff6 AP |
1563 | #ifdef CONFIG_ARM_GIC_V3 |
1564 | case KVM_DEV_TYPE_ARM_VGIC_V3: | |
1565 | vgic_v3_init_emulation(kvm); | |
1566 | break; | |
1567 | #endif | |
b26e5fda AP |
1568 | default: |
1569 | return -ENODEV; | |
1570 | } | |
1571 | ||
3caa2d8c AP |
1572 | if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus) |
1573 | return -E2BIG; | |
1574 | ||
b26e5fda AP |
1575 | return 0; |
1576 | } | |
1577 | ||
59892136 | 1578 | int kvm_vgic_create(struct kvm *kvm, u32 type) |
01ac5e34 | 1579 | { |
6b50f540 | 1580 | int i, vcpu_lock_idx = -1, ret; |
7330672b | 1581 | struct kvm_vcpu *vcpu; |
01ac5e34 MZ |
1582 | |
1583 | mutex_lock(&kvm->lock); | |
1584 | ||
4ce7ebdf | 1585 | if (irqchip_in_kernel(kvm)) { |
01ac5e34 MZ |
1586 | ret = -EEXIST; |
1587 | goto out; | |
1588 | } | |
1589 | ||
b5d84ff6 AP |
1590 | /* |
1591 | * This function is also called by the KVM_CREATE_IRQCHIP handler, | |
1592 | * which had no chance yet to check the availability of the GICv2 | |
1593 | * emulation. So check this here again. KVM_CREATE_DEVICE does | |
1594 | * the proper checks already. | |
1595 | */ | |
1596 | if (type == KVM_DEV_TYPE_ARM_VGIC_V2 && !vgic->can_emulate_gicv2) | |
1597 | return -ENODEV; | |
1598 | ||
7330672b CD |
1599 | /* |
1600 | * Any time a vcpu is run, vcpu_load is called which tries to grab the | |
1601 | * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure | |
1602 | * that no other VCPUs are run while we create the vgic. | |
1603 | */ | |
6b50f540 | 1604 | ret = -EBUSY; |
7330672b CD |
1605 | kvm_for_each_vcpu(i, vcpu, kvm) { |
1606 | if (!mutex_trylock(&vcpu->mutex)) | |
1607 | goto out_unlock; | |
1608 | vcpu_lock_idx = i; | |
1609 | } | |
1610 | ||
1611 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
6b50f540 | 1612 | if (vcpu->arch.has_run_once) |
7330672b | 1613 | goto out_unlock; |
7330672b | 1614 | } |
6b50f540 | 1615 | ret = 0; |
7330672b | 1616 | |
b26e5fda AP |
1617 | ret = init_vgic_model(kvm, type); |
1618 | if (ret) | |
1619 | goto out_unlock; | |
1620 | ||
01ac5e34 | 1621 | spin_lock_init(&kvm->arch.vgic.lock); |
f982cf4e | 1622 | kvm->arch.vgic.in_kernel = true; |
59892136 | 1623 | kvm->arch.vgic.vgic_model = type; |
8f186d52 | 1624 | kvm->arch.vgic.vctrl_base = vgic->vctrl_base; |
01ac5e34 MZ |
1625 | kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF; |
1626 | kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF; | |
a0675c25 | 1627 | kvm->arch.vgic.vgic_redist_base = VGIC_ADDR_UNDEF; |
01ac5e34 | 1628 | |
7330672b CD |
1629 | out_unlock: |
1630 | for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) { | |
1631 | vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx); | |
1632 | mutex_unlock(&vcpu->mutex); | |
1633 | } | |
1634 | ||
01ac5e34 MZ |
1635 | out: |
1636 | mutex_unlock(&kvm->lock); | |
1637 | return ret; | |
1638 | } | |
1639 | ||
1fa451bc | 1640 | static int vgic_ioaddr_overlap(struct kvm *kvm) |
330690cd CD |
1641 | { |
1642 | phys_addr_t dist = kvm->arch.vgic.vgic_dist_base; | |
1643 | phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base; | |
1644 | ||
1645 | if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu)) | |
1646 | return 0; | |
1647 | if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) || | |
1648 | (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist)) | |
1649 | return -EBUSY; | |
1650 | return 0; | |
1651 | } | |
1652 | ||
1653 | static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr, | |
1654 | phys_addr_t addr, phys_addr_t size) | |
1655 | { | |
1656 | int ret; | |
1657 | ||
ce01e4e8 CD |
1658 | if (addr & ~KVM_PHYS_MASK) |
1659 | return -E2BIG; | |
1660 | ||
1661 | if (addr & (SZ_4K - 1)) | |
1662 | return -EINVAL; | |
1663 | ||
330690cd CD |
1664 | if (!IS_VGIC_ADDR_UNDEF(*ioaddr)) |
1665 | return -EEXIST; | |
1666 | if (addr + size < addr) | |
1667 | return -EINVAL; | |
1668 | ||
30c21170 | 1669 | *ioaddr = addr; |
330690cd CD |
1670 | ret = vgic_ioaddr_overlap(kvm); |
1671 | if (ret) | |
30c21170 HW |
1672 | *ioaddr = VGIC_ADDR_UNDEF; |
1673 | ||
330690cd CD |
1674 | return ret; |
1675 | } | |
1676 | ||
ce01e4e8 CD |
1677 | /** |
1678 | * kvm_vgic_addr - set or get vgic VM base addresses | |
1679 | * @kvm: pointer to the vm struct | |
ac3d3735 | 1680 | * @type: the VGIC addr type, one of KVM_VGIC_V[23]_ADDR_TYPE_XXX |
ce01e4e8 CD |
1681 | * @addr: pointer to address value |
1682 | * @write: if true set the address in the VM address space, if false read the | |
1683 | * address | |
1684 | * | |
1685 | * Set or get the vgic base addresses for the distributor and the virtual CPU | |
1686 | * interface in the VM physical address space. These addresses are properties | |
1687 | * of the emulated core/SoC and therefore user space initially knows this | |
1688 | * information. | |
1689 | */ | |
1690 | int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write) | |
330690cd CD |
1691 | { |
1692 | int r = 0; | |
1693 | struct vgic_dist *vgic = &kvm->arch.vgic; | |
ac3d3735 AP |
1694 | int type_needed; |
1695 | phys_addr_t *addr_ptr, block_size; | |
4fa96afd | 1696 | phys_addr_t alignment; |
330690cd | 1697 | |
330690cd CD |
1698 | mutex_lock(&kvm->lock); |
1699 | switch (type) { | |
1700 | case KVM_VGIC_V2_ADDR_TYPE_DIST: | |
ac3d3735 AP |
1701 | type_needed = KVM_DEV_TYPE_ARM_VGIC_V2; |
1702 | addr_ptr = &vgic->vgic_dist_base; | |
1703 | block_size = KVM_VGIC_V2_DIST_SIZE; | |
4fa96afd | 1704 | alignment = SZ_4K; |
330690cd CD |
1705 | break; |
1706 | case KVM_VGIC_V2_ADDR_TYPE_CPU: | |
ac3d3735 AP |
1707 | type_needed = KVM_DEV_TYPE_ARM_VGIC_V2; |
1708 | addr_ptr = &vgic->vgic_cpu_base; | |
1709 | block_size = KVM_VGIC_V2_CPU_SIZE; | |
4fa96afd | 1710 | alignment = SZ_4K; |
330690cd | 1711 | break; |
ac3d3735 AP |
1712 | #ifdef CONFIG_ARM_GIC_V3 |
1713 | case KVM_VGIC_V3_ADDR_TYPE_DIST: | |
1714 | type_needed = KVM_DEV_TYPE_ARM_VGIC_V3; | |
1715 | addr_ptr = &vgic->vgic_dist_base; | |
1716 | block_size = KVM_VGIC_V3_DIST_SIZE; | |
4fa96afd | 1717 | alignment = SZ_64K; |
ac3d3735 AP |
1718 | break; |
1719 | case KVM_VGIC_V3_ADDR_TYPE_REDIST: | |
1720 | type_needed = KVM_DEV_TYPE_ARM_VGIC_V3; | |
1721 | addr_ptr = &vgic->vgic_redist_base; | |
1722 | block_size = KVM_VGIC_V3_REDIST_SIZE; | |
4fa96afd | 1723 | alignment = SZ_64K; |
ac3d3735 AP |
1724 | break; |
1725 | #endif | |
330690cd CD |
1726 | default: |
1727 | r = -ENODEV; | |
ac3d3735 AP |
1728 | goto out; |
1729 | } | |
1730 | ||
1731 | if (vgic->vgic_model != type_needed) { | |
1732 | r = -ENODEV; | |
1733 | goto out; | |
330690cd CD |
1734 | } |
1735 | ||
4fa96afd AP |
1736 | if (write) { |
1737 | if (!IS_ALIGNED(*addr, alignment)) | |
1738 | r = -EINVAL; | |
1739 | else | |
1740 | r = vgic_ioaddr_assign(kvm, addr_ptr, *addr, | |
1741 | block_size); | |
1742 | } else { | |
ac3d3735 | 1743 | *addr = *addr_ptr; |
4fa96afd | 1744 | } |
ac3d3735 AP |
1745 | |
1746 | out: | |
330690cd CD |
1747 | mutex_unlock(&kvm->lock); |
1748 | return r; | |
1749 | } | |
7330672b | 1750 | |
83215812 | 1751 | int vgic_set_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr) |
7330672b | 1752 | { |
ce01e4e8 CD |
1753 | int r; |
1754 | ||
1755 | switch (attr->group) { | |
1756 | case KVM_DEV_ARM_VGIC_GRP_ADDR: { | |
1757 | u64 __user *uaddr = (u64 __user *)(long)attr->addr; | |
1758 | u64 addr; | |
1759 | unsigned long type = (unsigned long)attr->attr; | |
1760 | ||
1761 | if (copy_from_user(&addr, uaddr, sizeof(addr))) | |
1762 | return -EFAULT; | |
1763 | ||
1764 | r = kvm_vgic_addr(dev->kvm, type, &addr, true); | |
1765 | return (r == -ENODEV) ? -ENXIO : r; | |
1766 | } | |
a98f26f1 MZ |
1767 | case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: { |
1768 | u32 __user *uaddr = (u32 __user *)(long)attr->addr; | |
1769 | u32 val; | |
1770 | int ret = 0; | |
1771 | ||
1772 | if (get_user(val, uaddr)) | |
1773 | return -EFAULT; | |
1774 | ||
1775 | /* | |
1776 | * We require: | |
1777 | * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs | |
1778 | * - at most 1024 interrupts | |
1779 | * - a multiple of 32 interrupts | |
1780 | */ | |
1781 | if (val < (VGIC_NR_PRIVATE_IRQS + 32) || | |
1782 | val > VGIC_MAX_IRQS || | |
1783 | (val & 31)) | |
1784 | return -EINVAL; | |
1785 | ||
1786 | mutex_lock(&dev->kvm->lock); | |
1787 | ||
c52edf5f | 1788 | if (vgic_ready(dev->kvm) || dev->kvm->arch.vgic.nr_irqs) |
a98f26f1 MZ |
1789 | ret = -EBUSY; |
1790 | else | |
1791 | dev->kvm->arch.vgic.nr_irqs = val; | |
1792 | ||
1793 | mutex_unlock(&dev->kvm->lock); | |
1794 | ||
1795 | return ret; | |
1796 | } | |
065c0034 EA |
1797 | case KVM_DEV_ARM_VGIC_GRP_CTRL: { |
1798 | switch (attr->attr) { | |
1799 | case KVM_DEV_ARM_VGIC_CTRL_INIT: | |
1800 | r = vgic_init(dev->kvm); | |
1801 | return r; | |
1802 | } | |
1803 | break; | |
1804 | } | |
ce01e4e8 CD |
1805 | } |
1806 | ||
7330672b CD |
1807 | return -ENXIO; |
1808 | } | |
1809 | ||
83215812 | 1810 | int vgic_get_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr) |
7330672b | 1811 | { |
ce01e4e8 CD |
1812 | int r = -ENXIO; |
1813 | ||
1814 | switch (attr->group) { | |
1815 | case KVM_DEV_ARM_VGIC_GRP_ADDR: { | |
1816 | u64 __user *uaddr = (u64 __user *)(long)attr->addr; | |
1817 | u64 addr; | |
1818 | unsigned long type = (unsigned long)attr->attr; | |
1819 | ||
1820 | r = kvm_vgic_addr(dev->kvm, type, &addr, false); | |
1821 | if (r) | |
1822 | return (r == -ENODEV) ? -ENXIO : r; | |
1823 | ||
1824 | if (copy_to_user(uaddr, &addr, sizeof(addr))) | |
1825 | return -EFAULT; | |
c07a0191 CD |
1826 | break; |
1827 | } | |
b60da146 AP |
1828 | case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: { |
1829 | u32 __user *uaddr = (u32 __user *)(long)attr->addr; | |
1830 | ||
1831 | r = put_user(dev->kvm->arch.vgic.nr_irqs, uaddr); | |
1832 | break; | |
1833 | } | |
1834 | ||
1835 | } | |
1836 | ||
1837 | return r; | |
1838 | } | |
1839 | ||
83215812 | 1840 | int vgic_has_attr_regs(const struct kvm_mmio_range *ranges, phys_addr_t offset) |
c07a0191 CD |
1841 | { |
1842 | struct kvm_exit_mmio dev_attr_mmio; | |
1843 | ||
1844 | dev_attr_mmio.len = 4; | |
83215812 | 1845 | if (vgic_find_range(ranges, &dev_attr_mmio, offset)) |
c07a0191 CD |
1846 | return 0; |
1847 | else | |
1848 | return -ENXIO; | |
1849 | } | |
1850 | ||
c06a841b WD |
1851 | static void vgic_init_maintenance_interrupt(void *info) |
1852 | { | |
1853 | enable_percpu_irq(vgic->maint_irq, 0); | |
1854 | } | |
1855 | ||
1856 | static int vgic_cpu_notify(struct notifier_block *self, | |
1857 | unsigned long action, void *cpu) | |
1858 | { | |
1859 | switch (action) { | |
1860 | case CPU_STARTING: | |
1861 | case CPU_STARTING_FROZEN: | |
1862 | vgic_init_maintenance_interrupt(NULL); | |
1863 | break; | |
1864 | case CPU_DYING: | |
1865 | case CPU_DYING_FROZEN: | |
1866 | disable_percpu_irq(vgic->maint_irq); | |
1867 | break; | |
1868 | } | |
1869 | ||
1870 | return NOTIFY_OK; | |
1871 | } | |
1872 | ||
1873 | static struct notifier_block vgic_cpu_nb = { | |
1874 | .notifier_call = vgic_cpu_notify, | |
1875 | }; | |
1876 | ||
1877 | static const struct of_device_id vgic_ids[] = { | |
0f372475 MR |
1878 | { .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, }, |
1879 | { .compatible = "arm,cortex-a7-gic", .data = vgic_v2_probe, }, | |
1880 | { .compatible = "arm,gic-400", .data = vgic_v2_probe, }, | |
1881 | { .compatible = "arm,gic-v3", .data = vgic_v3_probe, }, | |
c06a841b WD |
1882 | {}, |
1883 | }; | |
1884 | ||
1885 | int kvm_vgic_hyp_init(void) | |
1886 | { | |
1887 | const struct of_device_id *matched_id; | |
a875dafc CD |
1888 | const int (*vgic_probe)(struct device_node *,const struct vgic_ops **, |
1889 | const struct vgic_params **); | |
c06a841b WD |
1890 | struct device_node *vgic_node; |
1891 | int ret; | |
1892 | ||
1893 | vgic_node = of_find_matching_node_and_match(NULL, | |
1894 | vgic_ids, &matched_id); | |
1895 | if (!vgic_node) { | |
1896 | kvm_err("error: no compatible GIC node found\n"); | |
1897 | return -ENODEV; | |
1898 | } | |
1899 | ||
1900 | vgic_probe = matched_id->data; | |
1901 | ret = vgic_probe(vgic_node, &vgic_ops, &vgic); | |
1902 | if (ret) | |
1903 | return ret; | |
1904 | ||
1905 | ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler, | |
1906 | "vgic", kvm_get_running_vcpus()); | |
1907 | if (ret) { | |
1908 | kvm_err("Cannot register interrupt %d\n", vgic->maint_irq); | |
1909 | return ret; | |
1910 | } | |
1911 | ||
1912 | ret = __register_cpu_notifier(&vgic_cpu_nb); | |
1913 | if (ret) { | |
1914 | kvm_err("Cannot register vgic CPU notifier\n"); | |
1915 | goto out_free_irq; | |
1916 | } | |
1917 | ||
1918 | /* Callback into for arch code for setup */ | |
1919 | vgic_arch_setup(vgic); | |
1920 | ||
1921 | on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1); | |
1922 | ||
ea2f83a7 | 1923 | return 0; |
c06a841b WD |
1924 | |
1925 | out_free_irq: | |
1926 | free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus()); | |
1927 | return ret; | |
1928 | } | |
174178fe EA |
1929 | |
1930 | int kvm_irq_map_gsi(struct kvm *kvm, | |
1931 | struct kvm_kernel_irq_routing_entry *entries, | |
1932 | int gsi) | |
1933 | { | |
1934 | return gsi; | |
1935 | } | |
1936 | ||
1937 | int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin) | |
1938 | { | |
1939 | return pin; | |
1940 | } | |
1941 | ||
1942 | int kvm_set_irq(struct kvm *kvm, int irq_source_id, | |
1943 | u32 irq, int level, bool line_status) | |
1944 | { | |
1945 | unsigned int spi = irq + VGIC_NR_PRIVATE_IRQS; | |
1946 | ||
1947 | trace_kvm_set_irq(irq, level, irq_source_id); | |
1948 | ||
1949 | BUG_ON(!vgic_initialized(kvm)); | |
1950 | ||
1951 | if (spi > kvm->arch.vgic.nr_irqs) | |
1952 | return -EINVAL; | |
1953 | return kvm_vgic_inject_irq(kvm, 0, spi, level); | |
1954 | ||
1955 | } | |
1956 | ||
1957 | /* MSI not implemented yet */ | |
1958 | int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, | |
1959 | struct kvm *kvm, int irq_source_id, | |
1960 | int level, bool line_status) | |
1961 | { | |
1962 | return 0; | |
1963 | } |