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