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