]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - virt/kvm/arm/hyp/vgic-v3-sr.c
cfc9f79db2c6b457e37e6e290916c5f7e4df1c7a
2 * Copyright (C) 2012-2015 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/compiler.h>
19 #include <linux/irqchip/arm-gic-v3.h>
20 #include <linux/kvm_host.h>
22 #include <asm/kvm_hyp.h>
24 #define vtr_to_max_lr_idx(v) ((v) & 0xf)
25 #define vtr_to_nr_pre_bits(v) ((((u32)(v) >> 26) & 7) + 1)
27 static u64 __hyp_text
__gic_v3_get_lr(unsigned int lr
)
31 return read_gicreg(ICH_LR0_EL2
);
33 return read_gicreg(ICH_LR1_EL2
);
35 return read_gicreg(ICH_LR2_EL2
);
37 return read_gicreg(ICH_LR3_EL2
);
39 return read_gicreg(ICH_LR4_EL2
);
41 return read_gicreg(ICH_LR5_EL2
);
43 return read_gicreg(ICH_LR6_EL2
);
45 return read_gicreg(ICH_LR7_EL2
);
47 return read_gicreg(ICH_LR8_EL2
);
49 return read_gicreg(ICH_LR9_EL2
);
51 return read_gicreg(ICH_LR10_EL2
);
53 return read_gicreg(ICH_LR11_EL2
);
55 return read_gicreg(ICH_LR12_EL2
);
57 return read_gicreg(ICH_LR13_EL2
);
59 return read_gicreg(ICH_LR14_EL2
);
61 return read_gicreg(ICH_LR15_EL2
);
67 static void __hyp_text
__gic_v3_set_lr(u64 val
, int lr
)
71 write_gicreg(val
, ICH_LR0_EL2
);
74 write_gicreg(val
, ICH_LR1_EL2
);
77 write_gicreg(val
, ICH_LR2_EL2
);
80 write_gicreg(val
, ICH_LR3_EL2
);
83 write_gicreg(val
, ICH_LR4_EL2
);
86 write_gicreg(val
, ICH_LR5_EL2
);
89 write_gicreg(val
, ICH_LR6_EL2
);
92 write_gicreg(val
, ICH_LR7_EL2
);
95 write_gicreg(val
, ICH_LR8_EL2
);
98 write_gicreg(val
, ICH_LR9_EL2
);
101 write_gicreg(val
, ICH_LR10_EL2
);
104 write_gicreg(val
, ICH_LR11_EL2
);
107 write_gicreg(val
, ICH_LR12_EL2
);
110 write_gicreg(val
, ICH_LR13_EL2
);
113 write_gicreg(val
, ICH_LR14_EL2
);
116 write_gicreg(val
, ICH_LR15_EL2
);
121 static void __hyp_text
save_maint_int_state(struct kvm_vcpu
*vcpu
, int nr_lr
)
123 struct vgic_v3_cpu_if
*cpu_if
= &vcpu
->arch
.vgic_cpu
.vgic_v3
;
127 expect_mi
= !!(cpu_if
->vgic_hcr
& ICH_HCR_UIE
);
129 for (i
= 0; i
< nr_lr
; i
++) {
130 if (!(vcpu
->arch
.vgic_cpu
.live_lrs
& (1UL << i
)))
133 expect_mi
|= (!(cpu_if
->vgic_lr
[i
] & ICH_LR_HW
) &&
134 (cpu_if
->vgic_lr
[i
] & ICH_LR_EOI
));
138 cpu_if
->vgic_misr
= read_gicreg(ICH_MISR_EL2
);
140 if (cpu_if
->vgic_misr
& ICH_MISR_EOI
)
141 cpu_if
->vgic_eisr
= read_gicreg(ICH_EISR_EL2
);
143 cpu_if
->vgic_eisr
= 0;
145 cpu_if
->vgic_misr
= 0;
146 cpu_if
->vgic_eisr
= 0;
150 static void __hyp_text
__vgic_v3_write_ap0rn(u32 val
, int n
)
154 write_gicreg(val
, ICH_AP0R0_EL2
);
157 write_gicreg(val
, ICH_AP0R1_EL2
);
160 write_gicreg(val
, ICH_AP0R2_EL2
);
163 write_gicreg(val
, ICH_AP0R3_EL2
);
168 static void __hyp_text
__vgic_v3_write_ap1rn(u32 val
, int n
)
172 write_gicreg(val
, ICH_AP1R0_EL2
);
175 write_gicreg(val
, ICH_AP1R1_EL2
);
178 write_gicreg(val
, ICH_AP1R2_EL2
);
181 write_gicreg(val
, ICH_AP1R3_EL2
);
186 static u32 __hyp_text
__vgic_v3_read_ap0rn(int n
)
192 val
= read_gicreg(ICH_AP0R0_EL2
);
195 val
= read_gicreg(ICH_AP0R1_EL2
);
198 val
= read_gicreg(ICH_AP0R2_EL2
);
201 val
= read_gicreg(ICH_AP0R3_EL2
);
210 static u32 __hyp_text
__vgic_v3_read_ap1rn(int n
)
216 val
= read_gicreg(ICH_AP1R0_EL2
);
219 val
= read_gicreg(ICH_AP1R1_EL2
);
222 val
= read_gicreg(ICH_AP1R2_EL2
);
225 val
= read_gicreg(ICH_AP1R3_EL2
);
234 void __hyp_text
__vgic_v3_save_state(struct kvm_vcpu
*vcpu
)
236 struct vgic_v3_cpu_if
*cpu_if
= &vcpu
->arch
.vgic_cpu
.vgic_v3
;
240 * Make sure stores to the GIC via the memory mapped interface
241 * are now visible to the system register interface.
243 if (!cpu_if
->vgic_sre
)
246 cpu_if
->vgic_vmcr
= read_gicreg(ICH_VMCR_EL2
);
248 if (vcpu
->arch
.vgic_cpu
.live_lrs
) {
250 u32 max_lr_idx
, nr_pre_bits
;
252 cpu_if
->vgic_elrsr
= read_gicreg(ICH_ELSR_EL2
);
254 write_gicreg(0, ICH_HCR_EL2
);
255 val
= read_gicreg(ICH_VTR_EL2
);
256 max_lr_idx
= vtr_to_max_lr_idx(val
);
257 nr_pre_bits
= vtr_to_nr_pre_bits(val
);
259 save_maint_int_state(vcpu
, max_lr_idx
+ 1);
261 for (i
= 0; i
<= max_lr_idx
; i
++) {
262 if (!(vcpu
->arch
.vgic_cpu
.live_lrs
& (1UL << i
)))
265 if (cpu_if
->vgic_elrsr
& (1 << i
))
266 cpu_if
->vgic_lr
[i
] &= ~ICH_LR_STATE
;
268 cpu_if
->vgic_lr
[i
] = __gic_v3_get_lr(i
);
270 __gic_v3_set_lr(0, i
);
273 switch (nr_pre_bits
) {
275 cpu_if
->vgic_ap0r
[3] = __vgic_v3_read_ap0rn(3);
276 cpu_if
->vgic_ap0r
[2] = __vgic_v3_read_ap0rn(2);
278 cpu_if
->vgic_ap0r
[1] = __vgic_v3_read_ap0rn(1);
280 cpu_if
->vgic_ap0r
[0] = __vgic_v3_read_ap0rn(0);
283 switch (nr_pre_bits
) {
285 cpu_if
->vgic_ap1r
[3] = __vgic_v3_read_ap1rn(3);
286 cpu_if
->vgic_ap1r
[2] = __vgic_v3_read_ap1rn(2);
288 cpu_if
->vgic_ap1r
[1] = __vgic_v3_read_ap1rn(1);
290 cpu_if
->vgic_ap1r
[0] = __vgic_v3_read_ap1rn(0);
293 vcpu
->arch
.vgic_cpu
.live_lrs
= 0;
295 cpu_if
->vgic_misr
= 0;
296 cpu_if
->vgic_eisr
= 0;
297 cpu_if
->vgic_elrsr
= 0xffff;
298 cpu_if
->vgic_ap0r
[0] = 0;
299 cpu_if
->vgic_ap0r
[1] = 0;
300 cpu_if
->vgic_ap0r
[2] = 0;
301 cpu_if
->vgic_ap0r
[3] = 0;
302 cpu_if
->vgic_ap1r
[0] = 0;
303 cpu_if
->vgic_ap1r
[1] = 0;
304 cpu_if
->vgic_ap1r
[2] = 0;
305 cpu_if
->vgic_ap1r
[3] = 0;
308 val
= read_gicreg(ICC_SRE_EL2
);
309 write_gicreg(val
| ICC_SRE_EL2_ENABLE
, ICC_SRE_EL2
);
311 if (!cpu_if
->vgic_sre
) {
312 /* Make sure ENABLE is set at EL2 before setting SRE at EL1 */
314 write_gicreg(1, ICC_SRE_EL1
);
318 void __hyp_text
__vgic_v3_restore_state(struct kvm_vcpu
*vcpu
)
320 struct vgic_v3_cpu_if
*cpu_if
= &vcpu
->arch
.vgic_cpu
.vgic_v3
;
322 u32 max_lr_idx
, nr_pre_bits
;
327 * VFIQEn is RES1 if ICC_SRE_EL1.SRE is 1. This causes a
328 * Group0 interrupt (as generated in GICv2 mode) to be
329 * delivered as a FIQ to the guest, with potentially fatal
330 * consequences. So we must make sure that ICC_SRE_EL1 has
331 * been actually programmed with the value we want before
332 * starting to mess with the rest of the GIC.
334 if (!cpu_if
->vgic_sre
) {
335 write_gicreg(0, ICC_SRE_EL1
);
339 val
= read_gicreg(ICH_VTR_EL2
);
340 max_lr_idx
= vtr_to_max_lr_idx(val
);
341 nr_pre_bits
= vtr_to_nr_pre_bits(val
);
343 for (i
= 0; i
<= max_lr_idx
; i
++) {
344 if (cpu_if
->vgic_lr
[i
] & ICH_LR_STATE
)
345 live_lrs
|= (1 << i
);
348 write_gicreg(cpu_if
->vgic_vmcr
, ICH_VMCR_EL2
);
351 write_gicreg(cpu_if
->vgic_hcr
, ICH_HCR_EL2
);
353 switch (nr_pre_bits
) {
355 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[3], 3);
356 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[2], 2);
358 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[1], 1);
360 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[0], 0);
363 switch (nr_pre_bits
) {
365 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[3], 3);
366 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[2], 2);
368 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[1], 1);
370 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[0], 0);
373 for (i
= 0; i
<= max_lr_idx
; i
++) {
374 if (!(live_lrs
& (1 << i
)))
377 __gic_v3_set_lr(cpu_if
->vgic_lr
[i
], i
);
382 * Ensures that the above will have reached the
383 * (re)distributors. This ensure the guest will read the
384 * correct values from the memory-mapped interface.
386 if (!cpu_if
->vgic_sre
) {
390 vcpu
->arch
.vgic_cpu
.live_lrs
= live_lrs
;
393 * Prevent the guest from touching the GIC system registers if
394 * SRE isn't enabled for GICv3 emulation.
396 write_gicreg(read_gicreg(ICC_SRE_EL2
) & ~ICC_SRE_EL2_ENABLE
,
400 void __hyp_text
__vgic_v3_init_lrs(void)
402 int max_lr_idx
= vtr_to_max_lr_idx(read_gicreg(ICH_VTR_EL2
));
405 for (i
= 0; i
<= max_lr_idx
; i
++)
406 __gic_v3_set_lr(0, i
);
409 u64 __hyp_text
__vgic_v3_get_ich_vtr_el2(void)
411 return read_gicreg(ICH_VTR_EL2
);