aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Zyngier <marc.zyngier@arm.com>2013-07-12 10:15:23 -0400
committerChristoffer Dall <christoffer.dall@linaro.org>2014-07-11 07:57:36 -0400
commitb2fb1c0d378399e1427a91bb991c094f2ca09a2f (patch)
tree4e06317bc81281d27e80932f6c7ab0519bf5f1a6
parentac3c3747e2db2f326ffc601651de544cdd33a8e9 (diff)
KVM: ARM: vgic: add the GICv3 backend
Introduce the support code for emulating a GICv2 on top of GICv3 hardware. Acked-by: Catalin Marinas <catalin.marinas@arm.com> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
-rw-r--r--arch/arm64/include/asm/kvm_asm.h2
-rw-r--r--arch/arm64/kvm/vgic-v3-switch.S29
-rw-r--r--include/kvm/arm_vgic.h28
-rw-r--r--virt/kvm/arm/vgic-v3.c231
4 files changed, 290 insertions, 0 deletions
diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h
index 6252264341c8..ed4987bf9ac7 100644
--- a/arch/arm64/include/asm/kvm_asm.h
+++ b/arch/arm64/include/asm/kvm_asm.h
@@ -106,6 +106,8 @@ extern void __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa);
106 106
107extern int __kvm_vcpu_run(struct kvm_vcpu *vcpu); 107extern int __kvm_vcpu_run(struct kvm_vcpu *vcpu);
108 108
109extern u64 __vgic_v3_get_ich_vtr_el2(void);
110
109extern char __save_vgic_v2_state[]; 111extern char __save_vgic_v2_state[];
110extern char __restore_vgic_v2_state[]; 112extern char __restore_vgic_v2_state[];
111 113
diff --git a/arch/arm64/kvm/vgic-v3-switch.S b/arch/arm64/kvm/vgic-v3-switch.S
new file mode 100644
index 000000000000..9fbf27350c84
--- /dev/null
+++ b/arch/arm64/kvm/vgic-v3-switch.S
@@ -0,0 +1,29 @@
1/*
2 * Copyright (C) 2012,2013 - 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/linkage.h>
19#include <linux/irqchip/arm-gic-v3.h>
20
21 .text
22 .pushsection .hyp.text, "ax"
23
24ENTRY(__vgic_v3_get_ich_vtr_el2)
25 mrs x0, ICH_VTR_EL2
26 ret
27ENDPROC(__vgic_v3_get_ich_vtr_el2)
28
29 .popsection
diff --git a/include/kvm/arm_vgic.h b/include/kvm/arm_vgic.h
index 65f1121a3beb..35b0c121bb65 100644
--- a/include/kvm/arm_vgic.h
+++ b/include/kvm/arm_vgic.h
@@ -33,6 +33,7 @@
33#define VGIC_MAX_CPUS KVM_MAX_VCPUS 33#define VGIC_MAX_CPUS KVM_MAX_VCPUS
34 34
35#define VGIC_V2_MAX_LRS (1 << 6) 35#define VGIC_V2_MAX_LRS (1 << 6)
36#define VGIC_V3_MAX_LRS 16
36 37
37/* Sanity checks... */ 38/* Sanity checks... */
38#if (VGIC_MAX_CPUS > 8) 39#if (VGIC_MAX_CPUS > 8)
@@ -72,6 +73,7 @@ struct kvm_vcpu;
72 73
73enum vgic_type { 74enum vgic_type {
74 VGIC_V2, /* Good ol' GICv2 */ 75 VGIC_V2, /* Good ol' GICv2 */
76 VGIC_V3, /* New fancy GICv3 */
75}; 77};
76 78
77#define LR_STATE_PENDING (1 << 0) 79#define LR_STATE_PENDING (1 << 0)
@@ -172,6 +174,19 @@ struct vgic_v2_cpu_if {
172 u32 vgic_lr[VGIC_V2_MAX_LRS]; 174 u32 vgic_lr[VGIC_V2_MAX_LRS];
173}; 175};
174 176
177struct vgic_v3_cpu_if {
178#ifdef CONFIG_ARM_GIC_V3
179 u32 vgic_hcr;
180 u32 vgic_vmcr;
181 u32 vgic_misr; /* Saved only */
182 u32 vgic_eisr; /* Saved only */
183 u32 vgic_elrsr; /* Saved only */
184 u32 vgic_ap0r[4];
185 u32 vgic_ap1r[4];
186 u64 vgic_lr[VGIC_V3_MAX_LRS];
187#endif
188};
189
175struct vgic_cpu { 190struct vgic_cpu {
176#ifdef CONFIG_KVM_ARM_VGIC 191#ifdef CONFIG_KVM_ARM_VGIC
177 /* per IRQ to LR mapping */ 192 /* per IRQ to LR mapping */
@@ -190,6 +205,7 @@ struct vgic_cpu {
190 /* CPU vif control registers for world switch */ 205 /* CPU vif control registers for world switch */
191 union { 206 union {
192 struct vgic_v2_cpu_if vgic_v2; 207 struct vgic_v2_cpu_if vgic_v2;
208 struct vgic_v3_cpu_if vgic_v3;
193 }; 209 };
194#endif 210#endif
195}; 211};
@@ -224,6 +240,18 @@ bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
224int vgic_v2_probe(struct device_node *vgic_node, 240int vgic_v2_probe(struct device_node *vgic_node,
225 const struct vgic_ops **ops, 241 const struct vgic_ops **ops,
226 const struct vgic_params **params); 242 const struct vgic_params **params);
243#ifdef CONFIG_ARM_GIC_V3
244int vgic_v3_probe(struct device_node *vgic_node,
245 const struct vgic_ops **ops,
246 const struct vgic_params **params);
247#else
248static inline int vgic_v3_probe(struct device_node *vgic_node,
249 const struct vgic_ops **ops,
250 const struct vgic_params **params)
251{
252 return -ENODEV;
253}
254#endif
227 255
228#else 256#else
229static inline int kvm_vgic_hyp_init(void) 257static inline int kvm_vgic_hyp_init(void)
diff --git a/virt/kvm/arm/vgic-v3.c b/virt/kvm/arm/vgic-v3.c
new file mode 100644
index 000000000000..f01d44685720
--- /dev/null
+++ b/virt/kvm/arm/vgic-v3.c
@@ -0,0 +1,231 @@
1/*
2 * Copyright (C) 2013 ARM Limited, All Rights Reserved.
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/cpu.h>
19#include <linux/kvm.h>
20#include <linux/kvm_host.h>
21#include <linux/interrupt.h>
22#include <linux/io.h>
23#include <linux/of.h>
24#include <linux/of_address.h>
25#include <linux/of_irq.h>
26
27#include <linux/irqchip/arm-gic-v3.h>
28
29#include <asm/kvm_emulate.h>
30#include <asm/kvm_arm.h>
31#include <asm/kvm_mmu.h>
32
33/* These are for GICv2 emulation only */
34#define GICH_LR_VIRTUALID (0x3ffUL << 0)
35#define GICH_LR_PHYSID_CPUID_SHIFT (10)
36#define GICH_LR_PHYSID_CPUID (7UL << GICH_LR_PHYSID_CPUID_SHIFT)
37
38/*
39 * LRs are stored in reverse order in memory. make sure we index them
40 * correctly.
41 */
42#define LR_INDEX(lr) (VGIC_V3_MAX_LRS - 1 - lr)
43
44static u32 ich_vtr_el2;
45
46static struct vgic_lr vgic_v3_get_lr(const struct kvm_vcpu *vcpu, int lr)
47{
48 struct vgic_lr lr_desc;
49 u64 val = vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[LR_INDEX(lr)];
50
51 lr_desc.irq = val & GICH_LR_VIRTUALID;
52 if (lr_desc.irq <= 15)
53 lr_desc.source = (val >> GICH_LR_PHYSID_CPUID_SHIFT) & 0x7;
54 else
55 lr_desc.source = 0;
56 lr_desc.state = 0;
57
58 if (val & ICH_LR_PENDING_BIT)
59 lr_desc.state |= LR_STATE_PENDING;
60 if (val & ICH_LR_ACTIVE_BIT)
61 lr_desc.state |= LR_STATE_ACTIVE;
62 if (val & ICH_LR_EOI)
63 lr_desc.state |= LR_EOI_INT;
64
65 return lr_desc;
66}
67
68static void vgic_v3_set_lr(struct kvm_vcpu *vcpu, int lr,
69 struct vgic_lr lr_desc)
70{
71 u64 lr_val = (((u32)lr_desc.source << GICH_LR_PHYSID_CPUID_SHIFT) |
72 lr_desc.irq);
73
74 if (lr_desc.state & LR_STATE_PENDING)
75 lr_val |= ICH_LR_PENDING_BIT;
76 if (lr_desc.state & LR_STATE_ACTIVE)
77 lr_val |= ICH_LR_ACTIVE_BIT;
78 if (lr_desc.state & LR_EOI_INT)
79 lr_val |= ICH_LR_EOI;
80
81 vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[LR_INDEX(lr)] = lr_val;
82}
83
84static void vgic_v3_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
85 struct vgic_lr lr_desc)
86{
87 if (!(lr_desc.state & LR_STATE_MASK))
88 vcpu->arch.vgic_cpu.vgic_v3.vgic_elrsr |= (1U << lr);
89}
90
91static u64 vgic_v3_get_elrsr(const struct kvm_vcpu *vcpu)
92{
93 return vcpu->arch.vgic_cpu.vgic_v3.vgic_elrsr;
94}
95
96static u64 vgic_v3_get_eisr(const struct kvm_vcpu *vcpu)
97{
98 return vcpu->arch.vgic_cpu.vgic_v3.vgic_eisr;
99}
100
101static u32 vgic_v3_get_interrupt_status(const struct kvm_vcpu *vcpu)
102{
103 u32 misr = vcpu->arch.vgic_cpu.vgic_v3.vgic_misr;
104 u32 ret = 0;
105
106 if (misr & ICH_MISR_EOI)
107 ret |= INT_STATUS_EOI;
108 if (misr & ICH_MISR_U)
109 ret |= INT_STATUS_UNDERFLOW;
110
111 return ret;
112}
113
114static void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
115{
116 u32 vmcr = vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr;
117
118 vmcrp->ctlr = (vmcr & ICH_VMCR_CTLR_MASK) >> ICH_VMCR_CTLR_SHIFT;
119 vmcrp->abpr = (vmcr & ICH_VMCR_BPR1_MASK) >> ICH_VMCR_BPR1_SHIFT;
120 vmcrp->bpr = (vmcr & ICH_VMCR_BPR0_MASK) >> ICH_VMCR_BPR0_SHIFT;
121 vmcrp->pmr = (vmcr & ICH_VMCR_PMR_MASK) >> ICH_VMCR_PMR_SHIFT;
122}
123
124static void vgic_v3_enable_underflow(struct kvm_vcpu *vcpu)
125{
126 vcpu->arch.vgic_cpu.vgic_v3.vgic_hcr |= ICH_HCR_UIE;
127}
128
129static void vgic_v3_disable_underflow(struct kvm_vcpu *vcpu)
130{
131 vcpu->arch.vgic_cpu.vgic_v3.vgic_hcr &= ~ICH_HCR_UIE;
132}
133
134static void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
135{
136 u32 vmcr;
137
138 vmcr = (vmcrp->ctlr << ICH_VMCR_CTLR_SHIFT) & ICH_VMCR_CTLR_MASK;
139 vmcr |= (vmcrp->abpr << ICH_VMCR_BPR1_SHIFT) & ICH_VMCR_BPR1_MASK;
140 vmcr |= (vmcrp->bpr << ICH_VMCR_BPR0_SHIFT) & ICH_VMCR_BPR0_MASK;
141 vmcr |= (vmcrp->pmr << ICH_VMCR_PMR_SHIFT) & ICH_VMCR_PMR_MASK;
142
143 vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr = vmcr;
144}
145
146static void vgic_v3_enable(struct kvm_vcpu *vcpu)
147{
148 /*
149 * By forcing VMCR to zero, the GIC will restore the binary
150 * points to their reset values. Anything else resets to zero
151 * anyway.
152 */
153 vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr = 0;
154
155 /* Get the show on the road... */
156 vcpu->arch.vgic_cpu.vgic_v3.vgic_hcr = ICH_HCR_EN;
157}
158
159static const struct vgic_ops vgic_v3_ops = {
160 .get_lr = vgic_v3_get_lr,
161 .set_lr = vgic_v3_set_lr,
162 .sync_lr_elrsr = vgic_v3_sync_lr_elrsr,
163 .get_elrsr = vgic_v3_get_elrsr,
164 .get_eisr = vgic_v3_get_eisr,
165 .get_interrupt_status = vgic_v3_get_interrupt_status,
166 .enable_underflow = vgic_v3_enable_underflow,
167 .disable_underflow = vgic_v3_disable_underflow,
168 .get_vmcr = vgic_v3_get_vmcr,
169 .set_vmcr = vgic_v3_set_vmcr,
170 .enable = vgic_v3_enable,
171};
172
173static struct vgic_params vgic_v3_params;
174
175/**
176 * vgic_v3_probe - probe for a GICv3 compatible interrupt controller in DT
177 * @node: pointer to the DT node
178 * @ops: address of a pointer to the GICv3 operations
179 * @params: address of a pointer to HW-specific parameters
180 *
181 * Returns 0 if a GICv3 has been found, with the low level operations
182 * in *ops and the HW parameters in *params. Returns an error code
183 * otherwise.
184 */
185int vgic_v3_probe(struct device_node *vgic_node,
186 const struct vgic_ops **ops,
187 const struct vgic_params **params)
188{
189 int ret = 0;
190 u32 gicv_idx;
191 struct resource vcpu_res;
192 struct vgic_params *vgic = &vgic_v3_params;
193
194 vgic->maint_irq = irq_of_parse_and_map(vgic_node, 0);
195 if (!vgic->maint_irq) {
196 kvm_err("error getting vgic maintenance irq from DT\n");
197 ret = -ENXIO;
198 goto out;
199 }
200
201 ich_vtr_el2 = kvm_call_hyp(__vgic_v3_get_ich_vtr_el2);
202
203 /*
204 * The ListRegs field is 5 bits, but there is a architectural
205 * maximum of 16 list registers. Just ignore bit 4...
206 */
207 vgic->nr_lr = (ich_vtr_el2 & 0xf) + 1;
208
209 if (of_property_read_u32(vgic_node, "#redistributor-regions", &gicv_idx))
210 gicv_idx = 1;
211
212 gicv_idx += 3; /* Also skip GICD, GICC, GICH */
213 if (of_address_to_resource(vgic_node, gicv_idx, &vcpu_res)) {
214 kvm_err("Cannot obtain GICV region\n");
215 ret = -ENXIO;
216 goto out;
217 }
218 vgic->vcpu_base = vcpu_res.start;
219 vgic->vctrl_base = NULL;
220 vgic->type = VGIC_V3;
221
222 kvm_info("%s@%llx IRQ%d\n", vgic_node->name,
223 vcpu_res.start, vgic->maint_irq);
224
225 *ops = &vgic_v3_ops;
226 *params = vgic;
227
228out:
229 of_node_put(vgic_node);
230 return ret;
231}