blob: 4fe6e797e8b3c5f19b43049303d616175fe3af47 [file] [log] [blame]
Marc Zyngier06282fd2015-10-19 15:50:37 +01001/*
2 * Copyright (C) 2012-2015 - 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, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <linux/compiler.h>
19#include <linux/irqchip/arm-gic.h>
20#include <linux/kvm_host.h>
21
Marc Zyngierbf8feb32016-09-06 09:28:46 +010022#include <asm/kvm_emulate.h>
Marc Zyngier13720a52016-01-28 13:44:07 +000023#include <asm/kvm_hyp.h>
Marc Zyngierd6811982017-10-23 17:11:14 +010024#include <asm/kvm_mmu.h>
Marc Zyngier06282fd2015-10-19 15:50:37 +010025
Marc Zyngier2a1044f2016-02-09 17:07:18 +000026static void __hyp_text save_elrsr(struct kvm_vcpu *vcpu, void __iomem *base)
27{
28 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
Marc Zyngier50926d82016-05-28 11:27:11 +010029 int nr_lr = (kern_hyp_va(&kvm_vgic_global_state))->nr_lr;
Marc Zyngier2a1044f2016-02-09 17:07:18 +000030 u32 elrsr0, elrsr1;
31
32 elrsr0 = readl_relaxed(base + GICH_ELRSR0);
33 if (unlikely(nr_lr > 32))
34 elrsr1 = readl_relaxed(base + GICH_ELRSR1);
35 else
36 elrsr1 = 0;
37
Marc Zyngier2a1044f2016-02-09 17:07:18 +000038 cpu_if->vgic_elrsr = ((u64)elrsr1 << 32) | elrsr0;
Marc Zyngier2a1044f2016-02-09 17:07:18 +000039}
40
Marc Zyngierf8cfbce2016-02-09 17:09:49 +000041static void __hyp_text save_lrs(struct kvm_vcpu *vcpu, void __iomem *base)
42{
43 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
Marc Zyngierf8cfbce2016-02-09 17:09:49 +000044 int i;
Christoffer Dall00dafa02016-12-23 00:04:59 +010045 u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
Marc Zyngierf8cfbce2016-02-09 17:09:49 +000046
Christoffer Dall00dafa02016-12-23 00:04:59 +010047 for (i = 0; i < used_lrs; i++) {
Christoffer Dall4d3afc92016-05-25 15:26:33 +010048 if (cpu_if->vgic_elrsr & (1UL << i))
Marc Zyngierf8cfbce2016-02-09 17:09:49 +000049 cpu_if->vgic_lr[i] &= ~GICH_LR_STATE;
Christoffer Dall4d3afc92016-05-25 15:26:33 +010050 else
51 cpu_if->vgic_lr[i] = readl_relaxed(base + GICH_LR0 + (i * 4));
Marc Zyngierf8cfbce2016-02-09 17:09:49 +000052
Marc Zyngiercc1daf02016-02-09 17:36:09 +000053 writel_relaxed(0, base + GICH_LR0 + (i * 4));
Marc Zyngierf8cfbce2016-02-09 17:09:49 +000054 }
55}
56
Marc Zyngier06282fd2015-10-19 15:50:37 +010057/* vcpu is already in the HYP VA space */
58void __hyp_text __vgic_v2_save_state(struct kvm_vcpu *vcpu)
59{
60 struct kvm *kvm = kern_hyp_va(vcpu->kvm);
61 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
62 struct vgic_dist *vgic = &kvm->arch.vgic;
63 void __iomem *base = kern_hyp_va(vgic->vctrl_base);
Christoffer Dall00dafa02016-12-23 00:04:59 +010064 u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
Marc Zyngier06282fd2015-10-19 15:50:37 +010065
66 if (!base)
67 return;
68
Christoffer Dall00dafa02016-12-23 00:04:59 +010069 if (used_lrs) {
Marc Zyngier2a1044f2016-02-09 17:07:18 +000070 cpu_if->vgic_apr = readl_relaxed(base + GICH_APR);
Marc Zyngier06282fd2015-10-19 15:50:37 +010071
Marc Zyngier2a1044f2016-02-09 17:07:18 +000072 save_elrsr(vcpu, base);
Marc Zyngierf8cfbce2016-02-09 17:09:49 +000073 save_lrs(vcpu, base);
Marc Zyngier06282fd2015-10-19 15:50:37 +010074
Marc Zyngier59f00ff2016-02-02 19:35:34 +000075 writel_relaxed(0, base + GICH_HCR);
Marc Zyngier59f00ff2016-02-02 19:35:34 +000076 } else {
Marc Zyngier59f00ff2016-02-02 19:35:34 +000077 cpu_if->vgic_elrsr = ~0UL;
Marc Zyngier59f00ff2016-02-02 19:35:34 +000078 cpu_if->vgic_apr = 0;
79 }
Marc Zyngier06282fd2015-10-19 15:50:37 +010080}
81
82/* vcpu is already in the HYP VA space */
83void __hyp_text __vgic_v2_restore_state(struct kvm_vcpu *vcpu)
84{
85 struct kvm *kvm = kern_hyp_va(vcpu->kvm);
86 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
87 struct vgic_dist *vgic = &kvm->arch.vgic;
88 void __iomem *base = kern_hyp_va(vgic->vctrl_base);
Christoffer Dall2db4c102016-04-06 14:48:53 +020089 int i;
Christoffer Dall00dafa02016-12-23 00:04:59 +010090 u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
Marc Zyngier06282fd2015-10-19 15:50:37 +010091
92 if (!base)
93 return;
94
Christoffer Dall00dafa02016-12-23 00:04:59 +010095 if (used_lrs) {
Marc Zyngier59f00ff2016-02-02 19:35:34 +000096 writel_relaxed(cpu_if->vgic_hcr, base + GICH_HCR);
97 writel_relaxed(cpu_if->vgic_apr, base + GICH_APR);
Christoffer Dall00dafa02016-12-23 00:04:59 +010098 for (i = 0; i < used_lrs; i++) {
Marc Zyngiercc1daf02016-02-09 17:36:09 +000099 writel_relaxed(cpu_if->vgic_lr[i],
100 base + GICH_LR0 + (i * 4));
Marc Zyngier59f00ff2016-02-02 19:35:34 +0000101 }
102 }
Marc Zyngier06282fd2015-10-19 15:50:37 +0100103}
Marc Zyngierfb5ee362016-09-06 09:28:45 +0100104
105#ifdef CONFIG_ARM64
Marc Zyngier3272f0d2016-09-06 14:02:17 +0100106/*
107 * __vgic_v2_perform_cpuif_access -- perform a GICV access on behalf of the
108 * guest.
109 *
110 * @vcpu: the offending vcpu
111 *
112 * Returns:
113 * 1: GICV access successfully performed
114 * 0: Not a GICV access
115 * -1: Illegal GICV access
116 */
117int __hyp_text __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu)
Marc Zyngierfb5ee362016-09-06 09:28:45 +0100118{
Marc Zyngierbf8feb32016-09-06 09:28:46 +0100119 struct kvm *kvm = kern_hyp_va(vcpu->kvm);
120 struct vgic_dist *vgic = &kvm->arch.vgic;
121 phys_addr_t fault_ipa;
122 void __iomem *addr;
123 int rd;
124
125 /* Build the full address */
126 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
127 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & GENMASK(11, 0);
128
129 /* If not for GICV, move on */
130 if (fault_ipa < vgic->vgic_cpu_base ||
131 fault_ipa >= (vgic->vgic_cpu_base + KVM_VGIC_V2_CPU_SIZE))
Marc Zyngier3272f0d2016-09-06 14:02:17 +0100132 return 0;
Marc Zyngierbf8feb32016-09-06 09:28:46 +0100133
134 /* Reject anything but a 32bit access */
135 if (kvm_vcpu_dabt_get_as(vcpu) != sizeof(u32))
Marc Zyngier3272f0d2016-09-06 14:02:17 +0100136 return -1;
Marc Zyngierbf8feb32016-09-06 09:28:46 +0100137
138 /* Not aligned? Don't bother */
139 if (fault_ipa & 3)
Marc Zyngier3272f0d2016-09-06 14:02:17 +0100140 return -1;
Marc Zyngierbf8feb32016-09-06 09:28:46 +0100141
142 rd = kvm_vcpu_dabt_get_rd(vcpu);
143 addr = kern_hyp_va((kern_hyp_va(&kvm_vgic_global_state))->vcpu_base_va);
144 addr += fault_ipa - vgic->vgic_cpu_base;
145
146 if (kvm_vcpu_dabt_iswrite(vcpu)) {
147 u32 data = vcpu_data_guest_to_host(vcpu,
148 vcpu_get_reg(vcpu, rd),
149 sizeof(u32));
150 writel_relaxed(data, addr);
151 } else {
152 u32 data = readl_relaxed(addr);
153 vcpu_set_reg(vcpu, rd, vcpu_data_host_to_guest(vcpu, data,
154 sizeof(u32)));
155 }
156
Marc Zyngier3272f0d2016-09-06 14:02:17 +0100157 return 1;
Marc Zyngierfb5ee362016-09-06 09:28:45 +0100158}
159#endif