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_emulate.h>
23 #include <asm/kvm_hyp.h>
25 #define vtr_to_max_lr_idx(v) ((v) & 0xf)
26 #define vtr_to_nr_pre_bits(v) ((((u32)(v) >> 26) & 7) + 1)
27 #define vtr_to_nr_apr_regs(v) (1 << (vtr_to_nr_pre_bits(v) - 5))
29 static u64 __hyp_text
__gic_v3_get_lr(unsigned int lr
)
33 return read_gicreg(ICH_LR0_EL2
);
35 return read_gicreg(ICH_LR1_EL2
);
37 return read_gicreg(ICH_LR2_EL2
);
39 return read_gicreg(ICH_LR3_EL2
);
41 return read_gicreg(ICH_LR4_EL2
);
43 return read_gicreg(ICH_LR5_EL2
);
45 return read_gicreg(ICH_LR6_EL2
);
47 return read_gicreg(ICH_LR7_EL2
);
49 return read_gicreg(ICH_LR8_EL2
);
51 return read_gicreg(ICH_LR9_EL2
);
53 return read_gicreg(ICH_LR10_EL2
);
55 return read_gicreg(ICH_LR11_EL2
);
57 return read_gicreg(ICH_LR12_EL2
);
59 return read_gicreg(ICH_LR13_EL2
);
61 return read_gicreg(ICH_LR14_EL2
);
63 return read_gicreg(ICH_LR15_EL2
);
69 static void __hyp_text
__gic_v3_set_lr(u64 val
, int lr
)
73 write_gicreg(val
, ICH_LR0_EL2
);
76 write_gicreg(val
, ICH_LR1_EL2
);
79 write_gicreg(val
, ICH_LR2_EL2
);
82 write_gicreg(val
, ICH_LR3_EL2
);
85 write_gicreg(val
, ICH_LR4_EL2
);
88 write_gicreg(val
, ICH_LR5_EL2
);
91 write_gicreg(val
, ICH_LR6_EL2
);
94 write_gicreg(val
, ICH_LR7_EL2
);
97 write_gicreg(val
, ICH_LR8_EL2
);
100 write_gicreg(val
, ICH_LR9_EL2
);
103 write_gicreg(val
, ICH_LR10_EL2
);
106 write_gicreg(val
, ICH_LR11_EL2
);
109 write_gicreg(val
, ICH_LR12_EL2
);
112 write_gicreg(val
, ICH_LR13_EL2
);
115 write_gicreg(val
, ICH_LR14_EL2
);
118 write_gicreg(val
, ICH_LR15_EL2
);
123 static void __hyp_text
__vgic_v3_write_ap0rn(u32 val
, int n
)
127 write_gicreg(val
, ICH_AP0R0_EL2
);
130 write_gicreg(val
, ICH_AP0R1_EL2
);
133 write_gicreg(val
, ICH_AP0R2_EL2
);
136 write_gicreg(val
, ICH_AP0R3_EL2
);
141 static void __hyp_text
__vgic_v3_write_ap1rn(u32 val
, int n
)
145 write_gicreg(val
, ICH_AP1R0_EL2
);
148 write_gicreg(val
, ICH_AP1R1_EL2
);
151 write_gicreg(val
, ICH_AP1R2_EL2
);
154 write_gicreg(val
, ICH_AP1R3_EL2
);
159 static u32 __hyp_text
__vgic_v3_read_ap0rn(int n
)
165 val
= read_gicreg(ICH_AP0R0_EL2
);
168 val
= read_gicreg(ICH_AP0R1_EL2
);
171 val
= read_gicreg(ICH_AP0R2_EL2
);
174 val
= read_gicreg(ICH_AP0R3_EL2
);
183 static u32 __hyp_text
__vgic_v3_read_ap1rn(int n
)
189 val
= read_gicreg(ICH_AP1R0_EL2
);
192 val
= read_gicreg(ICH_AP1R1_EL2
);
195 val
= read_gicreg(ICH_AP1R2_EL2
);
198 val
= read_gicreg(ICH_AP1R3_EL2
);
207 void __hyp_text
__vgic_v3_save_state(struct kvm_vcpu
*vcpu
)
209 struct vgic_v3_cpu_if
*cpu_if
= &vcpu
->arch
.vgic_cpu
.vgic_v3
;
210 u64 used_lrs
= vcpu
->arch
.vgic_cpu
.used_lrs
;
214 * Make sure stores to the GIC via the memory mapped interface
215 * are now visible to the system register interface.
217 if (!cpu_if
->vgic_sre
) {
219 cpu_if
->vgic_vmcr
= read_gicreg(ICH_VMCR_EL2
);
226 cpu_if
->vgic_elrsr
= read_gicreg(ICH_ELSR_EL2
);
228 write_gicreg(0, ICH_HCR_EL2
);
229 val
= read_gicreg(ICH_VTR_EL2
);
230 nr_pre_bits
= vtr_to_nr_pre_bits(val
);
232 for (i
= 0; i
< used_lrs
; i
++) {
233 if (cpu_if
->vgic_elrsr
& (1 << i
))
234 cpu_if
->vgic_lr
[i
] &= ~ICH_LR_STATE
;
236 cpu_if
->vgic_lr
[i
] = __gic_v3_get_lr(i
);
238 __gic_v3_set_lr(0, i
);
241 switch (nr_pre_bits
) {
243 cpu_if
->vgic_ap0r
[3] = __vgic_v3_read_ap0rn(3);
244 cpu_if
->vgic_ap0r
[2] = __vgic_v3_read_ap0rn(2);
246 cpu_if
->vgic_ap0r
[1] = __vgic_v3_read_ap0rn(1);
248 cpu_if
->vgic_ap0r
[0] = __vgic_v3_read_ap0rn(0);
251 switch (nr_pre_bits
) {
253 cpu_if
->vgic_ap1r
[3] = __vgic_v3_read_ap1rn(3);
254 cpu_if
->vgic_ap1r
[2] = __vgic_v3_read_ap1rn(2);
256 cpu_if
->vgic_ap1r
[1] = __vgic_v3_read_ap1rn(1);
258 cpu_if
->vgic_ap1r
[0] = __vgic_v3_read_ap1rn(0);
261 if (static_branch_unlikely(&vgic_v3_cpuif_trap
) ||
262 cpu_if
->its_vpe
.its_vm
)
263 write_gicreg(0, ICH_HCR_EL2
);
265 cpu_if
->vgic_elrsr
= 0xffff;
266 cpu_if
->vgic_ap0r
[0] = 0;
267 cpu_if
->vgic_ap0r
[1] = 0;
268 cpu_if
->vgic_ap0r
[2] = 0;
269 cpu_if
->vgic_ap0r
[3] = 0;
270 cpu_if
->vgic_ap1r
[0] = 0;
271 cpu_if
->vgic_ap1r
[1] = 0;
272 cpu_if
->vgic_ap1r
[2] = 0;
273 cpu_if
->vgic_ap1r
[3] = 0;
276 val
= read_gicreg(ICC_SRE_EL2
);
277 write_gicreg(val
| ICC_SRE_EL2_ENABLE
, ICC_SRE_EL2
);
279 if (!cpu_if
->vgic_sre
) {
280 /* Make sure ENABLE is set at EL2 before setting SRE at EL1 */
282 write_gicreg(1, ICC_SRE_EL1
);
286 void __hyp_text
__vgic_v3_restore_state(struct kvm_vcpu
*vcpu
)
288 struct vgic_v3_cpu_if
*cpu_if
= &vcpu
->arch
.vgic_cpu
.vgic_v3
;
289 u64 used_lrs
= vcpu
->arch
.vgic_cpu
.used_lrs
;
295 * VFIQEn is RES1 if ICC_SRE_EL1.SRE is 1. This causes a
296 * Group0 interrupt (as generated in GICv2 mode) to be
297 * delivered as a FIQ to the guest, with potentially fatal
298 * consequences. So we must make sure that ICC_SRE_EL1 has
299 * been actually programmed with the value we want before
300 * starting to mess with the rest of the GIC, and VMCR_EL2 in
303 if (!cpu_if
->vgic_sre
) {
304 write_gicreg(0, ICC_SRE_EL1
);
306 write_gicreg(cpu_if
->vgic_vmcr
, ICH_VMCR_EL2
);
309 val
= read_gicreg(ICH_VTR_EL2
);
310 nr_pre_bits
= vtr_to_nr_pre_bits(val
);
313 write_gicreg(cpu_if
->vgic_hcr
, ICH_HCR_EL2
);
315 switch (nr_pre_bits
) {
317 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[3], 3);
318 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[2], 2);
320 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[1], 1);
322 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[0], 0);
325 switch (nr_pre_bits
) {
327 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[3], 3);
328 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[2], 2);
330 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[1], 1);
332 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[0], 0);
335 for (i
= 0; i
< used_lrs
; i
++)
336 __gic_v3_set_lr(cpu_if
->vgic_lr
[i
], i
);
339 * If we need to trap system registers, we must write
340 * ICH_HCR_EL2 anyway, even if no interrupts are being
341 * injected. Same thing if GICv4 is used, as VLPI
342 * delivery is gated by ICH_HCR_EL2.En.
344 if (static_branch_unlikely(&vgic_v3_cpuif_trap
) ||
345 cpu_if
->its_vpe
.its_vm
)
346 write_gicreg(cpu_if
->vgic_hcr
, ICH_HCR_EL2
);
350 * Ensures that the above will have reached the
351 * (re)distributors. This ensure the guest will read the
352 * correct values from the memory-mapped interface.
354 if (!cpu_if
->vgic_sre
) {
360 * Prevent the guest from touching the GIC system registers if
361 * SRE isn't enabled for GICv3 emulation.
363 write_gicreg(read_gicreg(ICC_SRE_EL2
) & ~ICC_SRE_EL2_ENABLE
,
367 void __hyp_text
__vgic_v3_init_lrs(void)
369 int max_lr_idx
= vtr_to_max_lr_idx(read_gicreg(ICH_VTR_EL2
));
372 for (i
= 0; i
<= max_lr_idx
; i
++)
373 __gic_v3_set_lr(0, i
);
376 u64 __hyp_text
__vgic_v3_get_ich_vtr_el2(void)
378 return read_gicreg(ICH_VTR_EL2
);
381 u64 __hyp_text
__vgic_v3_read_vmcr(void)
383 return read_gicreg(ICH_VMCR_EL2
);
386 void __hyp_text
__vgic_v3_write_vmcr(u32 vmcr
)
388 write_gicreg(vmcr
, ICH_VMCR_EL2
);
393 static int __hyp_text
__vgic_v3_bpr_min(void)
395 /* See Pseudocode for VPriorityGroup */
396 return 8 - vtr_to_nr_pre_bits(read_gicreg(ICH_VTR_EL2
));
399 static int __hyp_text
__vgic_v3_get_group(struct kvm_vcpu
*vcpu
)
401 u32 esr
= kvm_vcpu_get_hsr(vcpu
);
402 u8 crm
= (esr
& ESR_ELx_SYS64_ISS_CRM_MASK
) >> ESR_ELx_SYS64_ISS_CRM_SHIFT
;
407 #define GICv3_IDLE_PRIORITY 0xff
409 static int __hyp_text
__vgic_v3_highest_priority_lr(struct kvm_vcpu
*vcpu
,
413 unsigned int used_lrs
= vcpu
->arch
.vgic_cpu
.used_lrs
;
414 u8 priority
= GICv3_IDLE_PRIORITY
;
417 for (i
= 0; i
< used_lrs
; i
++) {
418 u64 val
= __gic_v3_get_lr(i
);
419 u8 lr_prio
= (val
& ICH_LR_PRIORITY_MASK
) >> ICH_LR_PRIORITY_SHIFT
;
421 /* Not pending in the state? */
422 if ((val
& ICH_LR_STATE
) != ICH_LR_PENDING_BIT
)
425 /* Group-0 interrupt, but Group-0 disabled? */
426 if (!(val
& ICH_LR_GROUP
) && !(vmcr
& ICH_VMCR_ENG0_MASK
))
429 /* Group-1 interrupt, but Group-1 disabled? */
430 if ((val
& ICH_LR_GROUP
) && !(vmcr
& ICH_VMCR_ENG1_MASK
))
433 /* Not the highest priority? */
434 if (lr_prio
>= priority
)
437 /* This is a candidate */
444 *lr_val
= ICC_IAR1_EL1_SPURIOUS
;
449 static int __hyp_text
__vgic_v3_find_active_lr(struct kvm_vcpu
*vcpu
,
450 int intid
, u64
*lr_val
)
452 unsigned int used_lrs
= vcpu
->arch
.vgic_cpu
.used_lrs
;
455 for (i
= 0; i
< used_lrs
; i
++) {
456 u64 val
= __gic_v3_get_lr(i
);
458 if ((val
& ICH_LR_VIRTUAL_ID_MASK
) == intid
&&
459 (val
& ICH_LR_ACTIVE_BIT
)) {
465 *lr_val
= ICC_IAR1_EL1_SPURIOUS
;
469 static int __hyp_text
__vgic_v3_get_highest_active_priority(void)
471 u8 nr_apr_regs
= vtr_to_nr_apr_regs(read_gicreg(ICH_VTR_EL2
));
475 for (i
= 0; i
< nr_apr_regs
; i
++) {
479 * The ICH_AP0Rn_EL2 and ICH_AP1Rn_EL2 registers
480 * contain the active priority levels for this VCPU
481 * for the maximum number of supported priority
482 * levels, and we return the full priority level only
483 * if the BPR is programmed to its minimum, otherwise
484 * we return a combination of the priority level and
485 * subpriority, as determined by the setting of the
486 * BPR, but without the full subpriority.
488 val
= __vgic_v3_read_ap0rn(i
);
489 val
|= __vgic_v3_read_ap1rn(i
);
495 return (hap
+ __ffs(val
)) << __vgic_v3_bpr_min();
498 return GICv3_IDLE_PRIORITY
;
501 static unsigned int __hyp_text
__vgic_v3_get_bpr0(u32 vmcr
)
503 return (vmcr
& ICH_VMCR_BPR0_MASK
) >> ICH_VMCR_BPR0_SHIFT
;
506 static unsigned int __hyp_text
__vgic_v3_get_bpr1(u32 vmcr
)
510 if (vmcr
& ICH_VMCR_CBPR_MASK
) {
511 bpr
= __vgic_v3_get_bpr0(vmcr
);
515 bpr
= (vmcr
& ICH_VMCR_BPR1_MASK
) >> ICH_VMCR_BPR1_SHIFT
;
522 * Convert a priority to a preemption level, taking the relevant BPR
523 * into account by zeroing the sub-priority bits.
525 static u8 __hyp_text
__vgic_v3_pri_to_pre(u8 pri
, u32 vmcr
, int grp
)
530 bpr
= __vgic_v3_get_bpr0(vmcr
) + 1;
532 bpr
= __vgic_v3_get_bpr1(vmcr
);
534 return pri
& (GENMASK(7, 0) << bpr
);
538 * The priority value is independent of any of the BPR values, so we
539 * normalize it using the minumal BPR value. This guarantees that no
540 * matter what the guest does with its BPR, we can always set/get the
541 * same value of a priority.
543 static void __hyp_text
__vgic_v3_set_active_priority(u8 pri
, u32 vmcr
, int grp
)
549 pre
= __vgic_v3_pri_to_pre(pri
, vmcr
, grp
);
550 ap
= pre
>> __vgic_v3_bpr_min();
554 val
= __vgic_v3_read_ap0rn(apr
);
555 __vgic_v3_write_ap0rn(val
| BIT(ap
% 32), apr
);
557 val
= __vgic_v3_read_ap1rn(apr
);
558 __vgic_v3_write_ap1rn(val
| BIT(ap
% 32), apr
);
562 static int __hyp_text
__vgic_v3_clear_highest_active_priority(void)
564 u8 nr_apr_regs
= vtr_to_nr_apr_regs(read_gicreg(ICH_VTR_EL2
));
568 for (i
= 0; i
< nr_apr_regs
; i
++) {
572 ap0
= __vgic_v3_read_ap0rn(i
);
573 ap1
= __vgic_v3_read_ap1rn(i
);
579 c0
= ap0
? __ffs(ap0
) : 32;
580 c1
= ap1
? __ffs(ap1
) : 32;
582 /* Always clear the LSB, which is the highest priority */
585 __vgic_v3_write_ap0rn(ap0
, i
);
589 __vgic_v3_write_ap1rn(ap1
, i
);
593 /* Rescale to 8 bits of priority */
594 return hap
<< __vgic_v3_bpr_min();
597 return GICv3_IDLE_PRIORITY
;
600 static void __hyp_text
__vgic_v3_read_iar(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
606 grp
= __vgic_v3_get_group(vcpu
);
608 lr
= __vgic_v3_highest_priority_lr(vcpu
, vmcr
, &lr_val
);
612 if (grp
!= !!(lr_val
& ICH_LR_GROUP
))
615 pmr
= (vmcr
& ICH_VMCR_PMR_MASK
) >> ICH_VMCR_PMR_SHIFT
;
616 lr_prio
= (lr_val
& ICH_LR_PRIORITY_MASK
) >> ICH_LR_PRIORITY_SHIFT
;
620 if (__vgic_v3_get_highest_active_priority() <= __vgic_v3_pri_to_pre(lr_prio
, vmcr
, grp
))
623 lr_val
&= ~ICH_LR_STATE
;
624 /* No active state for LPIs */
625 if ((lr_val
& ICH_LR_VIRTUAL_ID_MASK
) <= VGIC_MAX_SPI
)
626 lr_val
|= ICH_LR_ACTIVE_BIT
;
627 __gic_v3_set_lr(lr_val
, lr
);
628 __vgic_v3_set_active_priority(lr_prio
, vmcr
, grp
);
629 vcpu_set_reg(vcpu
, rt
, lr_val
& ICH_LR_VIRTUAL_ID_MASK
);
633 vcpu_set_reg(vcpu
, rt
, ICC_IAR1_EL1_SPURIOUS
);
636 static void __hyp_text
__vgic_v3_clear_active_lr(int lr
, u64 lr_val
)
638 lr_val
&= ~ICH_LR_ACTIVE_BIT
;
639 if (lr_val
& ICH_LR_HW
) {
642 pid
= (lr_val
& ICH_LR_PHYS_ID_MASK
) >> ICH_LR_PHYS_ID_SHIFT
;
646 __gic_v3_set_lr(lr_val
, lr
);
649 static void __hyp_text
__vgic_v3_bump_eoicount(void)
653 hcr
= read_gicreg(ICH_HCR_EL2
);
654 hcr
+= 1 << ICH_HCR_EOIcount_SHIFT
;
655 write_gicreg(hcr
, ICH_HCR_EL2
);
658 static void __hyp_text
__vgic_v3_write_dir(struct kvm_vcpu
*vcpu
,
661 u32 vid
= vcpu_get_reg(vcpu
, rt
);
665 /* EOImode == 0, nothing to be done here */
666 if (!(vmcr
& ICH_VMCR_EOIM_MASK
))
669 /* No deactivate to be performed on an LPI */
670 if (vid
>= VGIC_MIN_LPI
)
673 lr
= __vgic_v3_find_active_lr(vcpu
, vid
, &lr_val
);
675 __vgic_v3_bump_eoicount();
679 __vgic_v3_clear_active_lr(lr
, lr_val
);
682 static void __hyp_text
__vgic_v3_write_eoir(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
684 u32 vid
= vcpu_get_reg(vcpu
, rt
);
686 u8 lr_prio
, act_prio
;
689 grp
= __vgic_v3_get_group(vcpu
);
691 /* Drop priority in any case */
692 act_prio
= __vgic_v3_clear_highest_active_priority();
694 /* If EOIing an LPI, no deactivate to be performed */
695 if (vid
>= VGIC_MIN_LPI
)
698 /* EOImode == 1, nothing to be done here */
699 if (vmcr
& ICH_VMCR_EOIM_MASK
)
702 lr
= __vgic_v3_find_active_lr(vcpu
, vid
, &lr_val
);
704 __vgic_v3_bump_eoicount();
708 lr_prio
= (lr_val
& ICH_LR_PRIORITY_MASK
) >> ICH_LR_PRIORITY_SHIFT
;
710 /* If priorities or group do not match, the guest has fscked-up. */
711 if (grp
!= !!(lr_val
& ICH_LR_GROUP
) ||
712 __vgic_v3_pri_to_pre(lr_prio
, vmcr
, grp
) != act_prio
)
715 /* Let's now perform the deactivation */
716 __vgic_v3_clear_active_lr(lr
, lr_val
);
719 static void __hyp_text
__vgic_v3_read_igrpen0(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
721 vcpu_set_reg(vcpu
, rt
, !!(vmcr
& ICH_VMCR_ENG0_MASK
));
724 static void __hyp_text
__vgic_v3_read_igrpen1(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
726 vcpu_set_reg(vcpu
, rt
, !!(vmcr
& ICH_VMCR_ENG1_MASK
));
729 static void __hyp_text
__vgic_v3_write_igrpen0(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
731 u64 val
= vcpu_get_reg(vcpu
, rt
);
734 vmcr
|= ICH_VMCR_ENG0_MASK
;
736 vmcr
&= ~ICH_VMCR_ENG0_MASK
;
738 __vgic_v3_write_vmcr(vmcr
);
741 static void __hyp_text
__vgic_v3_write_igrpen1(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
743 u64 val
= vcpu_get_reg(vcpu
, rt
);
746 vmcr
|= ICH_VMCR_ENG1_MASK
;
748 vmcr
&= ~ICH_VMCR_ENG1_MASK
;
750 __vgic_v3_write_vmcr(vmcr
);
753 static void __hyp_text
__vgic_v3_read_bpr0(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
755 vcpu_set_reg(vcpu
, rt
, __vgic_v3_get_bpr0(vmcr
));
758 static void __hyp_text
__vgic_v3_read_bpr1(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
760 vcpu_set_reg(vcpu
, rt
, __vgic_v3_get_bpr1(vmcr
));
763 static void __hyp_text
__vgic_v3_write_bpr0(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
765 u64 val
= vcpu_get_reg(vcpu
, rt
);
766 u8 bpr_min
= __vgic_v3_bpr_min() - 1;
768 /* Enforce BPR limiting */
772 val
<<= ICH_VMCR_BPR0_SHIFT
;
773 val
&= ICH_VMCR_BPR0_MASK
;
774 vmcr
&= ~ICH_VMCR_BPR0_MASK
;
777 __vgic_v3_write_vmcr(vmcr
);
780 static void __hyp_text
__vgic_v3_write_bpr1(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
782 u64 val
= vcpu_get_reg(vcpu
, rt
);
783 u8 bpr_min
= __vgic_v3_bpr_min();
785 if (vmcr
& ICH_VMCR_CBPR_MASK
)
788 /* Enforce BPR limiting */
792 val
<<= ICH_VMCR_BPR1_SHIFT
;
793 val
&= ICH_VMCR_BPR1_MASK
;
794 vmcr
&= ~ICH_VMCR_BPR1_MASK
;
797 __vgic_v3_write_vmcr(vmcr
);
800 static void __hyp_text
__vgic_v3_read_apxrn(struct kvm_vcpu
*vcpu
, int rt
, int n
)
804 if (!__vgic_v3_get_group(vcpu
))
805 val
= __vgic_v3_read_ap0rn(n
);
807 val
= __vgic_v3_read_ap1rn(n
);
809 vcpu_set_reg(vcpu
, rt
, val
);
812 static void __hyp_text
__vgic_v3_write_apxrn(struct kvm_vcpu
*vcpu
, int rt
, int n
)
814 u32 val
= vcpu_get_reg(vcpu
, rt
);
816 if (!__vgic_v3_get_group(vcpu
))
817 __vgic_v3_write_ap0rn(val
, n
);
819 __vgic_v3_write_ap1rn(val
, n
);
822 static void __hyp_text
__vgic_v3_read_apxr0(struct kvm_vcpu
*vcpu
,
825 __vgic_v3_read_apxrn(vcpu
, rt
, 0);
828 static void __hyp_text
__vgic_v3_read_apxr1(struct kvm_vcpu
*vcpu
,
831 __vgic_v3_read_apxrn(vcpu
, rt
, 1);
834 static void __hyp_text
__vgic_v3_read_apxr2(struct kvm_vcpu
*vcpu
,
837 __vgic_v3_read_apxrn(vcpu
, rt
, 2);
840 static void __hyp_text
__vgic_v3_read_apxr3(struct kvm_vcpu
*vcpu
,
843 __vgic_v3_read_apxrn(vcpu
, rt
, 3);
846 static void __hyp_text
__vgic_v3_write_apxr0(struct kvm_vcpu
*vcpu
,
849 __vgic_v3_write_apxrn(vcpu
, rt
, 0);
852 static void __hyp_text
__vgic_v3_write_apxr1(struct kvm_vcpu
*vcpu
,
855 __vgic_v3_write_apxrn(vcpu
, rt
, 1);
858 static void __hyp_text
__vgic_v3_write_apxr2(struct kvm_vcpu
*vcpu
,
861 __vgic_v3_write_apxrn(vcpu
, rt
, 2);
864 static void __hyp_text
__vgic_v3_write_apxr3(struct kvm_vcpu
*vcpu
,
867 __vgic_v3_write_apxrn(vcpu
, rt
, 3);
870 static void __hyp_text
__vgic_v3_read_hppir(struct kvm_vcpu
*vcpu
,
876 grp
= __vgic_v3_get_group(vcpu
);
878 lr
= __vgic_v3_highest_priority_lr(vcpu
, vmcr
, &lr_val
);
882 lr_grp
= !!(lr_val
& ICH_LR_GROUP
);
884 lr_val
= ICC_IAR1_EL1_SPURIOUS
;
887 vcpu_set_reg(vcpu
, rt
, lr_val
& ICH_LR_VIRTUAL_ID_MASK
);
890 static void __hyp_text
__vgic_v3_read_pmr(struct kvm_vcpu
*vcpu
,
893 vmcr
&= ICH_VMCR_PMR_MASK
;
894 vmcr
>>= ICH_VMCR_PMR_SHIFT
;
895 vcpu_set_reg(vcpu
, rt
, vmcr
);
898 static void __hyp_text
__vgic_v3_write_pmr(struct kvm_vcpu
*vcpu
,
901 u32 val
= vcpu_get_reg(vcpu
, rt
);
903 val
<<= ICH_VMCR_PMR_SHIFT
;
904 val
&= ICH_VMCR_PMR_MASK
;
905 vmcr
&= ~ICH_VMCR_PMR_MASK
;
908 write_gicreg(vmcr
, ICH_VMCR_EL2
);
911 static void __hyp_text
__vgic_v3_read_rpr(struct kvm_vcpu
*vcpu
,
914 u32 val
= __vgic_v3_get_highest_active_priority();
915 vcpu_set_reg(vcpu
, rt
, val
);
918 static void __hyp_text
__vgic_v3_read_ctlr(struct kvm_vcpu
*vcpu
,
923 vtr
= read_gicreg(ICH_VTR_EL2
);
925 val
= ((vtr
>> 29) & 7) << ICC_CTLR_EL1_PRI_BITS_SHIFT
;
927 val
|= ((vtr
>> 23) & 7) << ICC_CTLR_EL1_ID_BITS_SHIFT
;
929 val
|= ((vtr
>> 22) & 1) << ICC_CTLR_EL1_SEIS_SHIFT
;
931 val
|= ((vtr
>> 21) & 1) << ICC_CTLR_EL1_A3V_SHIFT
;
933 val
|= ((vmcr
& ICH_VMCR_EOIM_MASK
) >> ICH_VMCR_EOIM_SHIFT
) << ICC_CTLR_EL1_EOImode_SHIFT
;
935 val
|= (vmcr
& ICH_VMCR_CBPR_MASK
) >> ICH_VMCR_CBPR_SHIFT
;
937 vcpu_set_reg(vcpu
, rt
, val
);
940 static void __hyp_text
__vgic_v3_write_ctlr(struct kvm_vcpu
*vcpu
,
943 u32 val
= vcpu_get_reg(vcpu
, rt
);
945 if (val
& ICC_CTLR_EL1_CBPR_MASK
)
946 vmcr
|= ICH_VMCR_CBPR_MASK
;
948 vmcr
&= ~ICH_VMCR_CBPR_MASK
;
950 if (val
& ICC_CTLR_EL1_EOImode_MASK
)
951 vmcr
|= ICH_VMCR_EOIM_MASK
;
953 vmcr
&= ~ICH_VMCR_EOIM_MASK
;
955 write_gicreg(vmcr
, ICH_VMCR_EL2
);
958 int __hyp_text
__vgic_v3_perform_cpuif_access(struct kvm_vcpu
*vcpu
)
963 void (*fn
)(struct kvm_vcpu
*, u32
, int);
967 esr
= kvm_vcpu_get_hsr(vcpu
);
968 if (vcpu_mode_is_32bit(vcpu
)) {
969 if (!kvm_condition_valid(vcpu
))
972 sysreg
= esr_cp15_to_sysreg(esr
);
974 sysreg
= esr_sys64_to_sysreg(esr
);
977 is_read
= (esr
& ESR_ELx_SYS64_ISS_DIR_MASK
) == ESR_ELx_SYS64_ISS_DIR_READ
;
980 case SYS_ICC_IAR0_EL1
:
981 case SYS_ICC_IAR1_EL1
:
982 if (unlikely(!is_read
))
984 fn
= __vgic_v3_read_iar
;
986 case SYS_ICC_EOIR0_EL1
:
987 case SYS_ICC_EOIR1_EL1
:
988 if (unlikely(is_read
))
990 fn
= __vgic_v3_write_eoir
;
992 case SYS_ICC_IGRPEN1_EL1
:
994 fn
= __vgic_v3_read_igrpen1
;
996 fn
= __vgic_v3_write_igrpen1
;
998 case SYS_ICC_BPR1_EL1
:
1000 fn
= __vgic_v3_read_bpr1
;
1002 fn
= __vgic_v3_write_bpr1
;
1004 case SYS_ICC_AP0Rn_EL1(0):
1005 case SYS_ICC_AP1Rn_EL1(0):
1007 fn
= __vgic_v3_read_apxr0
;
1009 fn
= __vgic_v3_write_apxr0
;
1011 case SYS_ICC_AP0Rn_EL1(1):
1012 case SYS_ICC_AP1Rn_EL1(1):
1014 fn
= __vgic_v3_read_apxr1
;
1016 fn
= __vgic_v3_write_apxr1
;
1018 case SYS_ICC_AP0Rn_EL1(2):
1019 case SYS_ICC_AP1Rn_EL1(2):
1021 fn
= __vgic_v3_read_apxr2
;
1023 fn
= __vgic_v3_write_apxr2
;
1025 case SYS_ICC_AP0Rn_EL1(3):
1026 case SYS_ICC_AP1Rn_EL1(3):
1028 fn
= __vgic_v3_read_apxr3
;
1030 fn
= __vgic_v3_write_apxr3
;
1032 case SYS_ICC_HPPIR0_EL1
:
1033 case SYS_ICC_HPPIR1_EL1
:
1034 if (unlikely(!is_read
))
1036 fn
= __vgic_v3_read_hppir
;
1038 case SYS_ICC_IGRPEN0_EL1
:
1040 fn
= __vgic_v3_read_igrpen0
;
1042 fn
= __vgic_v3_write_igrpen0
;
1044 case SYS_ICC_BPR0_EL1
:
1046 fn
= __vgic_v3_read_bpr0
;
1048 fn
= __vgic_v3_write_bpr0
;
1050 case SYS_ICC_DIR_EL1
:
1051 if (unlikely(is_read
))
1053 fn
= __vgic_v3_write_dir
;
1055 case SYS_ICC_RPR_EL1
:
1056 if (unlikely(!is_read
))
1058 fn
= __vgic_v3_read_rpr
;
1060 case SYS_ICC_CTLR_EL1
:
1062 fn
= __vgic_v3_read_ctlr
;
1064 fn
= __vgic_v3_write_ctlr
;
1066 case SYS_ICC_PMR_EL1
:
1068 fn
= __vgic_v3_read_pmr
;
1070 fn
= __vgic_v3_write_pmr
;
1076 vmcr
= __vgic_v3_read_vmcr();
1077 rt
= kvm_vcpu_sys_get_rt(vcpu
);