blob: 58b8af00ee4c9fe12ef808f3854f4606a95c7270 [file] [log] [blame]
Marc Zyngierb2fb1c02013-07-12 15:15:23 +01001/*
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);
Christoffer Dall5c0ac4b2015-05-04 09:25:26 +080089 else
90 vcpu->arch.vgic_cpu.vgic_v3.vgic_elrsr &= ~(1U << lr);
Marc Zyngierb2fb1c02013-07-12 15:15:23 +010091}
92
93static u64 vgic_v3_get_elrsr(const struct kvm_vcpu *vcpu)
94{
95 return vcpu->arch.vgic_cpu.vgic_v3.vgic_elrsr;
96}
97
98static u64 vgic_v3_get_eisr(const struct kvm_vcpu *vcpu)
99{
100 return vcpu->arch.vgic_cpu.vgic_v3.vgic_eisr;
101}
102
Christoffer Dall5c0ac4b2015-05-04 09:25:26 +0800103static void vgic_v3_clear_eisr(struct kvm_vcpu *vcpu)
104{
105 vcpu->arch.vgic_cpu.vgic_v3.vgic_eisr = 0;
106}
107
Marc Zyngierb2fb1c02013-07-12 15:15:23 +0100108static u32 vgic_v3_get_interrupt_status(const struct kvm_vcpu *vcpu)
109{
110 u32 misr = vcpu->arch.vgic_cpu.vgic_v3.vgic_misr;
111 u32 ret = 0;
112
113 if (misr & ICH_MISR_EOI)
114 ret |= INT_STATUS_EOI;
115 if (misr & ICH_MISR_U)
116 ret |= INT_STATUS_UNDERFLOW;
117
118 return ret;
119}
120
121static void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
122{
123 u32 vmcr = vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr;
124
125 vmcrp->ctlr = (vmcr & ICH_VMCR_CTLR_MASK) >> ICH_VMCR_CTLR_SHIFT;
126 vmcrp->abpr = (vmcr & ICH_VMCR_BPR1_MASK) >> ICH_VMCR_BPR1_SHIFT;
127 vmcrp->bpr = (vmcr & ICH_VMCR_BPR0_MASK) >> ICH_VMCR_BPR0_SHIFT;
128 vmcrp->pmr = (vmcr & ICH_VMCR_PMR_MASK) >> ICH_VMCR_PMR_SHIFT;
129}
130
131static void vgic_v3_enable_underflow(struct kvm_vcpu *vcpu)
132{
133 vcpu->arch.vgic_cpu.vgic_v3.vgic_hcr |= ICH_HCR_UIE;
134}
135
136static void vgic_v3_disable_underflow(struct kvm_vcpu *vcpu)
137{
138 vcpu->arch.vgic_cpu.vgic_v3.vgic_hcr &= ~ICH_HCR_UIE;
139}
140
141static void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
142{
143 u32 vmcr;
144
145 vmcr = (vmcrp->ctlr << ICH_VMCR_CTLR_SHIFT) & ICH_VMCR_CTLR_MASK;
146 vmcr |= (vmcrp->abpr << ICH_VMCR_BPR1_SHIFT) & ICH_VMCR_BPR1_MASK;
147 vmcr |= (vmcrp->bpr << ICH_VMCR_BPR0_SHIFT) & ICH_VMCR_BPR0_MASK;
148 vmcr |= (vmcrp->pmr << ICH_VMCR_PMR_SHIFT) & ICH_VMCR_PMR_MASK;
149
150 vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr = vmcr;
151}
152
153static void vgic_v3_enable(struct kvm_vcpu *vcpu)
154{
155 /*
156 * By forcing VMCR to zero, the GIC will restore the binary
157 * points to their reset values. Anything else resets to zero
158 * anyway.
159 */
160 vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr = 0;
161
162 /* Get the show on the road... */
163 vcpu->arch.vgic_cpu.vgic_v3.vgic_hcr = ICH_HCR_EN;
164}
165
166static const struct vgic_ops vgic_v3_ops = {
167 .get_lr = vgic_v3_get_lr,
168 .set_lr = vgic_v3_set_lr,
169 .sync_lr_elrsr = vgic_v3_sync_lr_elrsr,
170 .get_elrsr = vgic_v3_get_elrsr,
171 .get_eisr = vgic_v3_get_eisr,
Christoffer Dall5c0ac4b2015-05-04 09:25:26 +0800172 .clear_eisr = vgic_v3_clear_eisr,
Marc Zyngierb2fb1c02013-07-12 15:15:23 +0100173 .get_interrupt_status = vgic_v3_get_interrupt_status,
174 .enable_underflow = vgic_v3_enable_underflow,
175 .disable_underflow = vgic_v3_disable_underflow,
176 .get_vmcr = vgic_v3_get_vmcr,
177 .set_vmcr = vgic_v3_set_vmcr,
178 .enable = vgic_v3_enable,
179};
180
181static struct vgic_params vgic_v3_params;
182
183/**
184 * vgic_v3_probe - probe for a GICv3 compatible interrupt controller in DT
185 * @node: pointer to the DT node
186 * @ops: address of a pointer to the GICv3 operations
187 * @params: address of a pointer to HW-specific parameters
188 *
189 * Returns 0 if a GICv3 has been found, with the low level operations
190 * in *ops and the HW parameters in *params. Returns an error code
191 * otherwise.
192 */
193int vgic_v3_probe(struct device_node *vgic_node,
194 const struct vgic_ops **ops,
195 const struct vgic_params **params)
196{
197 int ret = 0;
198 u32 gicv_idx;
199 struct resource vcpu_res;
200 struct vgic_params *vgic = &vgic_v3_params;
201
202 vgic->maint_irq = irq_of_parse_and_map(vgic_node, 0);
203 if (!vgic->maint_irq) {
204 kvm_err("error getting vgic maintenance irq from DT\n");
205 ret = -ENXIO;
206 goto out;
207 }
208
209 ich_vtr_el2 = kvm_call_hyp(__vgic_v3_get_ich_vtr_el2);
210
211 /*
212 * The ListRegs field is 5 bits, but there is a architectural
213 * maximum of 16 list registers. Just ignore bit 4...
214 */
215 vgic->nr_lr = (ich_vtr_el2 & 0xf) + 1;
216
217 if (of_property_read_u32(vgic_node, "#redistributor-regions", &gicv_idx))
218 gicv_idx = 1;
219
220 gicv_idx += 3; /* Also skip GICD, GICC, GICH */
221 if (of_address_to_resource(vgic_node, gicv_idx, &vcpu_res)) {
222 kvm_err("Cannot obtain GICV region\n");
223 ret = -ENXIO;
224 goto out;
225 }
Marc Zyngierfb3ec672014-07-31 11:42:18 +0100226
227 if (!PAGE_ALIGNED(vcpu_res.start)) {
228 kvm_err("GICV physical address 0x%llx not page aligned\n",
229 (unsigned long long)vcpu_res.start);
230 ret = -ENXIO;
231 goto out;
232 }
233
234 if (!PAGE_ALIGNED(resource_size(&vcpu_res))) {
235 kvm_err("GICV size 0x%llx not a multiple of page size 0x%lx\n",
236 (unsigned long long)resource_size(&vcpu_res),
237 PAGE_SIZE);
238 ret = -ENXIO;
239 goto out;
240 }
241
Marc Zyngierb2fb1c02013-07-12 15:15:23 +0100242 vgic->vcpu_base = vcpu_res.start;
243 vgic->vctrl_base = NULL;
244 vgic->type = VGIC_V3;
245
246 kvm_info("%s@%llx IRQ%d\n", vgic_node->name,
247 vcpu_res.start, vgic->maint_irq);
248
249 *ops = &vgic_v3_ops;
250 *params = vgic;
251
252out:
253 of_node_put(vgic_node);
254 return ret;
255}