xref: /openbmc/linux/arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c (revision 8a43a2b34b7dc59dc6df5fa0a3b8540918bc4c58)
1*8a43a2b3SChristoffer Dall /*
2*8a43a2b3SChristoffer Dall  * Copyright (C) 2012-2015 - ARM Ltd
3*8a43a2b3SChristoffer Dall  * Author: Marc Zyngier <marc.zyngier@arm.com>
4*8a43a2b3SChristoffer Dall  *
5*8a43a2b3SChristoffer Dall  * This program is free software; you can redistribute it and/or modify
6*8a43a2b3SChristoffer Dall  * it under the terms of the GNU General Public License version 2 as
7*8a43a2b3SChristoffer Dall  * published by the Free Software Foundation.
8*8a43a2b3SChristoffer Dall  *
9*8a43a2b3SChristoffer Dall  * This program is distributed in the hope that it will be useful,
10*8a43a2b3SChristoffer Dall  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*8a43a2b3SChristoffer Dall  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*8a43a2b3SChristoffer Dall  * GNU General Public License for more details.
13*8a43a2b3SChristoffer Dall  *
14*8a43a2b3SChristoffer Dall  * You should have received a copy of the GNU General Public License
15*8a43a2b3SChristoffer Dall  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16*8a43a2b3SChristoffer Dall  */
17*8a43a2b3SChristoffer Dall 
18*8a43a2b3SChristoffer Dall #include <linux/compiler.h>
19*8a43a2b3SChristoffer Dall #include <linux/irqchip/arm-gic.h>
20*8a43a2b3SChristoffer Dall #include <linux/kvm_host.h>
21*8a43a2b3SChristoffer Dall 
22*8a43a2b3SChristoffer Dall #include <asm/kvm_emulate.h>
23*8a43a2b3SChristoffer Dall #include <asm/kvm_hyp.h>
24*8a43a2b3SChristoffer Dall #include <asm/kvm_mmu.h>
25*8a43a2b3SChristoffer Dall 
26*8a43a2b3SChristoffer Dall /*
27*8a43a2b3SChristoffer Dall  * __vgic_v2_perform_cpuif_access -- perform a GICV access on behalf of the
28*8a43a2b3SChristoffer Dall  *				     guest.
29*8a43a2b3SChristoffer Dall  *
30*8a43a2b3SChristoffer Dall  * @vcpu: the offending vcpu
31*8a43a2b3SChristoffer Dall  *
32*8a43a2b3SChristoffer Dall  * Returns:
33*8a43a2b3SChristoffer Dall  *  1: GICV access successfully performed
34*8a43a2b3SChristoffer Dall  *  0: Not a GICV access
35*8a43a2b3SChristoffer Dall  * -1: Illegal GICV access
36*8a43a2b3SChristoffer Dall  */
37*8a43a2b3SChristoffer Dall int __hyp_text __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu)
38*8a43a2b3SChristoffer Dall {
39*8a43a2b3SChristoffer Dall 	struct kvm *kvm = kern_hyp_va(vcpu->kvm);
40*8a43a2b3SChristoffer Dall 	struct vgic_dist *vgic = &kvm->arch.vgic;
41*8a43a2b3SChristoffer Dall 	phys_addr_t fault_ipa;
42*8a43a2b3SChristoffer Dall 	void __iomem *addr;
43*8a43a2b3SChristoffer Dall 	int rd;
44*8a43a2b3SChristoffer Dall 
45*8a43a2b3SChristoffer Dall 	/* Build the full address */
46*8a43a2b3SChristoffer Dall 	fault_ipa  = kvm_vcpu_get_fault_ipa(vcpu);
47*8a43a2b3SChristoffer Dall 	fault_ipa |= kvm_vcpu_get_hfar(vcpu) & GENMASK(11, 0);
48*8a43a2b3SChristoffer Dall 
49*8a43a2b3SChristoffer Dall 	/* If not for GICV, move on */
50*8a43a2b3SChristoffer Dall 	if (fault_ipa <  vgic->vgic_cpu_base ||
51*8a43a2b3SChristoffer Dall 	    fault_ipa >= (vgic->vgic_cpu_base + KVM_VGIC_V2_CPU_SIZE))
52*8a43a2b3SChristoffer Dall 		return 0;
53*8a43a2b3SChristoffer Dall 
54*8a43a2b3SChristoffer Dall 	/* Reject anything but a 32bit access */
55*8a43a2b3SChristoffer Dall 	if (kvm_vcpu_dabt_get_as(vcpu) != sizeof(u32))
56*8a43a2b3SChristoffer Dall 		return -1;
57*8a43a2b3SChristoffer Dall 
58*8a43a2b3SChristoffer Dall 	/* Not aligned? Don't bother */
59*8a43a2b3SChristoffer Dall 	if (fault_ipa & 3)
60*8a43a2b3SChristoffer Dall 		return -1;
61*8a43a2b3SChristoffer Dall 
62*8a43a2b3SChristoffer Dall 	rd = kvm_vcpu_dabt_get_rd(vcpu);
63*8a43a2b3SChristoffer Dall 	addr  = kern_hyp_va((kern_hyp_va(&kvm_vgic_global_state))->vcpu_base_va);
64*8a43a2b3SChristoffer Dall 	addr += fault_ipa - vgic->vgic_cpu_base;
65*8a43a2b3SChristoffer Dall 
66*8a43a2b3SChristoffer Dall 	if (kvm_vcpu_dabt_iswrite(vcpu)) {
67*8a43a2b3SChristoffer Dall 		u32 data = vcpu_data_guest_to_host(vcpu,
68*8a43a2b3SChristoffer Dall 						   vcpu_get_reg(vcpu, rd),
69*8a43a2b3SChristoffer Dall 						   sizeof(u32));
70*8a43a2b3SChristoffer Dall 		writel_relaxed(data, addr);
71*8a43a2b3SChristoffer Dall 	} else {
72*8a43a2b3SChristoffer Dall 		u32 data = readl_relaxed(addr);
73*8a43a2b3SChristoffer Dall 		vcpu_set_reg(vcpu, rd, vcpu_data_host_to_guest(vcpu, data,
74*8a43a2b3SChristoffer Dall 							       sizeof(u32)));
75*8a43a2b3SChristoffer Dall 	}
76*8a43a2b3SChristoffer Dall 
77*8a43a2b3SChristoffer Dall 	return 1;
78*8a43a2b3SChristoffer Dall }
79