blob: 2a11fe89943a2a95342071050a4cf233c7b8c49f [file] [log] [blame]
Marc Zyngier59529f62015-11-30 13:09:53 +00001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program. If not, see <http://www.gnu.org/licenses/>.
13 */
14
15#include <linux/irqchip/arm-gic-v3.h>
16#include <linux/kvm.h>
17#include <linux/kvm_host.h>
Eric Auger90977732015-12-01 15:02:35 +010018#include <kvm/arm_vgic.h>
Christoffer Dall923a2e32017-10-05 00:18:07 +020019#include <asm/kvm_hyp.h>
Eric Auger90977732015-12-01 15:02:35 +010020#include <asm/kvm_mmu.h>
21#include <asm/kvm_asm.h>
Marc Zyngier59529f62015-11-30 13:09:53 +000022
23#include "vgic.h"
24
Marc Zyngierabf55762017-06-09 12:49:45 +010025static bool group0_trap;
Marc Zyngier9c7bfc22017-06-09 12:49:40 +010026static bool group1_trap;
Marc Zyngierff895112017-06-09 12:49:53 +010027static bool common_trap;
Marc Zyngiera7546052017-10-27 15:28:54 +010028static bool gicv4_enable;
Marc Zyngier9c7bfc22017-06-09 12:49:40 +010029
Marc Zyngier59529f62015-11-30 13:09:53 +000030void vgic_v3_set_underflow(struct kvm_vcpu *vcpu)
31{
32 struct vgic_v3_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v3;
33
34 cpuif->vgic_hcr |= ICH_HCR_UIE;
35}
36
Christoffer Dallaf061492016-12-29 15:44:27 +010037static bool lr_signals_eoi_mi(u64 lr_val)
38{
39 return !(lr_val & ICH_LR_STATE) && (lr_val & ICH_LR_EOI) &&
40 !(lr_val & ICH_LR_HW);
41}
42
Marc Zyngier59529f62015-11-30 13:09:53 +000043void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu)
44{
Christoffer Dall8ac76ef2017-03-18 13:48:42 +010045 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
46 struct vgic_v3_cpu_if *cpuif = &vgic_cpu->vgic_v3;
Marc Zyngier59529f62015-11-30 13:09:53 +000047 u32 model = vcpu->kvm->arch.vgic.vgic_model;
48 int lr;
Christoffer Dall006df0f2016-10-16 22:19:11 +020049 unsigned long flags;
Marc Zyngier59529f62015-11-30 13:09:53 +000050
Marc Zyngier53692902018-04-18 10:39:04 +010051 cpuif->vgic_hcr &= ~ICH_HCR_UIE;
Christoffer Dallaf061492016-12-29 15:44:27 +010052
Christoffer Dall8ac76ef2017-03-18 13:48:42 +010053 for (lr = 0; lr < vgic_cpu->used_lrs; lr++) {
Marc Zyngier59529f62015-11-30 13:09:53 +000054 u64 val = cpuif->vgic_lr[lr];
Marc Zyngier53692902018-04-18 10:39:04 +010055 u32 intid, cpuid;
Marc Zyngier59529f62015-11-30 13:09:53 +000056 struct vgic_irq *irq;
Marc Zyngier53692902018-04-18 10:39:04 +010057 bool is_v2_sgi = false;
Marc Zyngier59529f62015-11-30 13:09:53 +000058
Marc Zyngier53692902018-04-18 10:39:04 +010059 cpuid = val & GICH_LR_PHYSID_CPUID;
60 cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT;
61
62 if (model == KVM_DEV_TYPE_ARM_VGIC_V3) {
Marc Zyngier59529f62015-11-30 13:09:53 +000063 intid = val & ICH_LR_VIRTUAL_ID_MASK;
Marc Zyngier53692902018-04-18 10:39:04 +010064 } else {
Marc Zyngier59529f62015-11-30 13:09:53 +000065 intid = val & GICH_LR_VIRTUALID;
Marc Zyngier53692902018-04-18 10:39:04 +010066 is_v2_sgi = vgic_irq_is_sgi(intid);
67 }
Christoffer Dallaf061492016-12-29 15:44:27 +010068
69 /* Notify fds when the guest EOI'ed a level-triggered IRQ */
70 if (lr_signals_eoi_mi(val) && vgic_valid_spi(vcpu->kvm, intid))
71 kvm_notify_acked_irq(vcpu->kvm, 0,
72 intid - VGIC_NR_PRIVATE_IRQS);
73
Marc Zyngier59529f62015-11-30 13:09:53 +000074 irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
Andre Przywara38024112016-07-15 12:43:33 +010075 if (!irq) /* An LPI could have been unmapped. */
76 continue;
Marc Zyngier59529f62015-11-30 13:09:53 +000077
Christoffer Dall006df0f2016-10-16 22:19:11 +020078 spin_lock_irqsave(&irq->irq_lock, flags);
Marc Zyngier59529f62015-11-30 13:09:53 +000079
80 /* Always preserve the active bit */
81 irq->active = !!(val & ICH_LR_ACTIVE_BIT);
82
Marc Zyngier53692902018-04-18 10:39:04 +010083 if (irq->active && is_v2_sgi)
84 irq->active_source = cpuid;
85
Marc Zyngier59529f62015-11-30 13:09:53 +000086 /* Edge is the only case where we preserve the pending bit */
87 if (irq->config == VGIC_CONFIG_EDGE &&
88 (val & ICH_LR_PENDING_BIT)) {
Christoffer Dall8694e4d2017-01-23 14:07:18 +010089 irq->pending_latch = true;
Marc Zyngier59529f62015-11-30 13:09:53 +000090
Marc Zyngier53692902018-04-18 10:39:04 +010091 if (is_v2_sgi)
Marc Zyngier59529f62015-11-30 13:09:53 +000092 irq->source |= (1 << cpuid);
Marc Zyngier59529f62015-11-30 13:09:53 +000093 }
94
Marc Zyngier637d1222016-05-25 15:26:36 +010095 /*
96 * Clear soft pending state when level irqs have been acked.
Marc Zyngier637d1222016-05-25 15:26:36 +010097 */
Marc Zyngier67b5b672018-03-09 14:59:40 +000098 if (irq->config == VGIC_CONFIG_LEVEL && !(val & ICH_LR_STATE))
99 irq->pending_latch = false;
Marc Zyngier59529f62015-11-30 13:09:53 +0000100
Christoffer Dalle40cc572017-08-29 10:40:44 +0200101 /*
102 * Level-triggered mapped IRQs are special because we only
103 * observe rising edges as input to the VGIC.
104 *
105 * If the guest never acked the interrupt we have to sample
106 * the physical line and set the line level, because the
107 * device state could have changed or we simply need to
108 * process the still pending interrupt later.
109 *
110 * If this causes us to lower the level, we have to also clear
111 * the physical active state, since we will otherwise never be
112 * told when the interrupt becomes asserted again.
113 */
114 if (vgic_irq_is_mapped_level(irq) && (val & ICH_LR_PENDING_BIT)) {
115 irq->line_level = vgic_get_phys_line_level(irq);
116
117 if (!irq->line_level)
118 vgic_irq_set_phys_active(irq, false);
119 }
120
Christoffer Dall006df0f2016-10-16 22:19:11 +0200121 spin_unlock_irqrestore(&irq->irq_lock, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100122 vgic_put_irq(vcpu->kvm, irq);
Marc Zyngier59529f62015-11-30 13:09:53 +0000123 }
Christoffer Dall8ac76ef2017-03-18 13:48:42 +0100124
125 vgic_cpu->used_lrs = 0;
Marc Zyngier59529f62015-11-30 13:09:53 +0000126}
127
128/* Requires the irq to be locked already */
129void vgic_v3_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr)
130{
131 u32 model = vcpu->kvm->arch.vgic.vgic_model;
132 u64 val = irq->intid;
Marc Zyngier53692902018-04-18 10:39:04 +0100133 bool allow_pending = true, is_v2_sgi;
Marc Zyngier59529f62015-11-30 13:09:53 +0000134
Marc Zyngier53692902018-04-18 10:39:04 +0100135 is_v2_sgi = (vgic_irq_is_sgi(irq->intid) &&
136 model == KVM_DEV_TYPE_ARM_VGIC_V2);
137
138 if (irq->active) {
Marc Zyngier67b5b672018-03-09 14:59:40 +0000139 val |= ICH_LR_ACTIVE_BIT;
Marc Zyngier53692902018-04-18 10:39:04 +0100140 if (is_v2_sgi)
141 val |= irq->active_source << GICH_LR_PHYSID_CPUID_SHIFT;
142 if (vgic_irq_is_multi_sgi(irq)) {
143 allow_pending = false;
144 val |= ICH_LR_EOI;
145 }
146 }
Marc Zyngier67b5b672018-03-09 14:59:40 +0000147
148 if (irq->hw) {
149 val |= ICH_LR_HW;
150 val |= ((u64)irq->hwintid) << ICH_LR_PHYS_ID_SHIFT;
151 /*
152 * Never set pending+active on a HW interrupt, as the
153 * pending state is kept at the physical distributor
154 * level.
155 */
156 if (irq->active)
157 allow_pending = false;
158 } else {
159 if (irq->config == VGIC_CONFIG_LEVEL) {
160 val |= ICH_LR_EOI;
161
162 /*
163 * Software resampling doesn't work very well
164 * if we allow P+A, so let's not do that.
165 */
166 if (irq->active)
167 allow_pending = false;
168 }
169 }
170
171 if (allow_pending && irq_is_pending(irq)) {
Marc Zyngier59529f62015-11-30 13:09:53 +0000172 val |= ICH_LR_PENDING_BIT;
173
174 if (irq->config == VGIC_CONFIG_EDGE)
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100175 irq->pending_latch = false;
Marc Zyngier59529f62015-11-30 13:09:53 +0000176
177 if (vgic_irq_is_sgi(irq->intid) &&
178 model == KVM_DEV_TYPE_ARM_VGIC_V2) {
179 u32 src = ffs(irq->source);
180
181 BUG_ON(!src);
182 val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT;
183 irq->source &= ~(1 << (src - 1));
Marc Zyngier53692902018-04-18 10:39:04 +0100184 if (irq->source) {
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100185 irq->pending_latch = true;
Marc Zyngier53692902018-04-18 10:39:04 +0100186 val |= ICH_LR_EOI;
187 }
Marc Zyngier59529f62015-11-30 13:09:53 +0000188 }
189 }
190
Marc Zyngier59529f62015-11-30 13:09:53 +0000191 /*
Christoffer Dalle40cc572017-08-29 10:40:44 +0200192 * Level-triggered mapped IRQs are special because we only observe
193 * rising edges as input to the VGIC. We therefore lower the line
194 * level here, so that we can take new virtual IRQs. See
195 * vgic_v3_fold_lr_state for more info.
196 */
197 if (vgic_irq_is_mapped_level(irq) && (val & ICH_LR_PENDING_BIT))
198 irq->line_level = false;
199
200 /*
Marc Zyngier59529f62015-11-30 13:09:53 +0000201 * We currently only support Group1 interrupts, which is a
202 * known defect. This needs to be addressed at some point.
203 */
204 if (model == KVM_DEV_TYPE_ARM_VGIC_V3)
205 val |= ICH_LR_GROUP;
206
207 val |= (u64)irq->priority << ICH_LR_PRIORITY_SHIFT;
208
209 vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] = val;
210}
211
212void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr)
213{
214 vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] = 0;
215}
Andre Przywarae4823a72015-12-03 11:47:37 +0000216
217void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
218{
Christoffer Dall328e5662016-03-24 11:21:04 +0100219 struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
Christoffer Dall28232a42017-05-20 14:12:34 +0200220 u32 model = vcpu->kvm->arch.vgic.vgic_model;
Andre Przywarae4823a72015-12-03 11:47:37 +0000221 u32 vmcr;
222
Christoffer Dall28232a42017-05-20 14:12:34 +0200223 if (model == KVM_DEV_TYPE_ARM_VGIC_V2) {
224 vmcr = (vmcrp->ackctl << ICH_VMCR_ACK_CTL_SHIFT) &
225 ICH_VMCR_ACK_CTL_MASK;
226 vmcr |= (vmcrp->fiqen << ICH_VMCR_FIQ_EN_SHIFT) &
227 ICH_VMCR_FIQ_EN_MASK;
228 } else {
229 /*
230 * When emulating GICv3 on GICv3 with SRE=1 on the
231 * VFIQEn bit is RES1 and the VAckCtl bit is RES0.
232 */
233 vmcr = ICH_VMCR_FIQ_EN_MASK;
234 }
235
236 vmcr |= (vmcrp->cbpr << ICH_VMCR_CBPR_SHIFT) & ICH_VMCR_CBPR_MASK;
237 vmcr |= (vmcrp->eoim << ICH_VMCR_EOIM_SHIFT) & ICH_VMCR_EOIM_MASK;
Andre Przywarae4823a72015-12-03 11:47:37 +0000238 vmcr |= (vmcrp->abpr << ICH_VMCR_BPR1_SHIFT) & ICH_VMCR_BPR1_MASK;
239 vmcr |= (vmcrp->bpr << ICH_VMCR_BPR0_SHIFT) & ICH_VMCR_BPR0_MASK;
240 vmcr |= (vmcrp->pmr << ICH_VMCR_PMR_SHIFT) & ICH_VMCR_PMR_MASK;
Vijaya Kumar K5fb247d2017-01-26 19:50:50 +0530241 vmcr |= (vmcrp->grpen0 << ICH_VMCR_ENG0_SHIFT) & ICH_VMCR_ENG0_MASK;
242 vmcr |= (vmcrp->grpen1 << ICH_VMCR_ENG1_SHIFT) & ICH_VMCR_ENG1_MASK;
Andre Przywarae4823a72015-12-03 11:47:37 +0000243
Christoffer Dall328e5662016-03-24 11:21:04 +0100244 cpu_if->vgic_vmcr = vmcr;
Andre Przywarae4823a72015-12-03 11:47:37 +0000245}
246
247void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
248{
Christoffer Dall328e5662016-03-24 11:21:04 +0100249 struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
Christoffer Dall28232a42017-05-20 14:12:34 +0200250 u32 model = vcpu->kvm->arch.vgic.vgic_model;
Christoffer Dall328e5662016-03-24 11:21:04 +0100251 u32 vmcr;
252
253 vmcr = cpu_if->vgic_vmcr;
Andre Przywarae4823a72015-12-03 11:47:37 +0000254
Christoffer Dall28232a42017-05-20 14:12:34 +0200255 if (model == KVM_DEV_TYPE_ARM_VGIC_V2) {
256 vmcrp->ackctl = (vmcr & ICH_VMCR_ACK_CTL_MASK) >>
257 ICH_VMCR_ACK_CTL_SHIFT;
258 vmcrp->fiqen = (vmcr & ICH_VMCR_FIQ_EN_MASK) >>
259 ICH_VMCR_FIQ_EN_SHIFT;
260 } else {
261 /*
262 * When emulating GICv3 on GICv3 with SRE=1 on the
263 * VFIQEn bit is RES1 and the VAckCtl bit is RES0.
264 */
265 vmcrp->fiqen = 1;
266 vmcrp->ackctl = 0;
267 }
268
269 vmcrp->cbpr = (vmcr & ICH_VMCR_CBPR_MASK) >> ICH_VMCR_CBPR_SHIFT;
270 vmcrp->eoim = (vmcr & ICH_VMCR_EOIM_MASK) >> ICH_VMCR_EOIM_SHIFT;
Andre Przywarae4823a72015-12-03 11:47:37 +0000271 vmcrp->abpr = (vmcr & ICH_VMCR_BPR1_MASK) >> ICH_VMCR_BPR1_SHIFT;
272 vmcrp->bpr = (vmcr & ICH_VMCR_BPR0_MASK) >> ICH_VMCR_BPR0_SHIFT;
273 vmcrp->pmr = (vmcr & ICH_VMCR_PMR_MASK) >> ICH_VMCR_PMR_SHIFT;
Vijaya Kumar K5fb247d2017-01-26 19:50:50 +0530274 vmcrp->grpen0 = (vmcr & ICH_VMCR_ENG0_MASK) >> ICH_VMCR_ENG0_SHIFT;
275 vmcrp->grpen1 = (vmcr & ICH_VMCR_ENG1_MASK) >> ICH_VMCR_ENG1_SHIFT;
Andre Przywarae4823a72015-12-03 11:47:37 +0000276}
Eric Auger90977732015-12-01 15:02:35 +0100277
Andre Przywara0aa1de52016-07-15 12:43:29 +0100278#define INITIAL_PENDBASER_VALUE \
279 (GIC_BASER_CACHEABILITY(GICR_PENDBASER, INNER, RaWb) | \
280 GIC_BASER_CACHEABILITY(GICR_PENDBASER, OUTER, SameAsInner) | \
281 GIC_BASER_SHAREABILITY(GICR_PENDBASER, InnerShareable))
282
Eric Augerad275b8b2015-12-21 18:09:38 +0100283void vgic_v3_enable(struct kvm_vcpu *vcpu)
284{
Eric Augerf7b69852015-12-02 10:30:13 +0100285 struct vgic_v3_cpu_if *vgic_v3 = &vcpu->arch.vgic_cpu.vgic_v3;
286
287 /*
288 * By forcing VMCR to zero, the GIC will restore the binary
289 * points to their reset values. Anything else resets to zero
290 * anyway.
291 */
292 vgic_v3->vgic_vmcr = 0;
Eric Augerf7b69852015-12-02 10:30:13 +0100293
294 /*
295 * If we are emulating a GICv3, we do it in an non-GICv2-compatible
296 * way, so we force SRE to 1 to demonstrate this to the guest.
Marc Zyngier4dfc0502017-02-21 11:32:47 +0000297 * Also, we don't support any form of IRQ/FIQ bypass.
Eric Augerf7b69852015-12-02 10:30:13 +0100298 * This goes with the spec allowing the value to be RAO/WI.
299 */
Andre Przywara0aa1de52016-07-15 12:43:29 +0100300 if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
Marc Zyngier4dfc0502017-02-21 11:32:47 +0000301 vgic_v3->vgic_sre = (ICC_SRE_EL1_DIB |
302 ICC_SRE_EL1_DFB |
303 ICC_SRE_EL1_SRE);
Andre Przywara0aa1de52016-07-15 12:43:29 +0100304 vcpu->arch.vgic_cpu.pendbaser = INITIAL_PENDBASER_VALUE;
305 } else {
Eric Augerf7b69852015-12-02 10:30:13 +0100306 vgic_v3->vgic_sre = 0;
Andre Przywara0aa1de52016-07-15 12:43:29 +0100307 }
Eric Augerf7b69852015-12-02 10:30:13 +0100308
Vijaya Kumar Kd017d7b2017-01-26 19:50:51 +0530309 vcpu->arch.vgic_cpu.num_id_bits = (kvm_vgic_global_state.ich_vtr_el2 &
310 ICH_VTR_ID_BITS_MASK) >>
311 ICH_VTR_ID_BITS_SHIFT;
312 vcpu->arch.vgic_cpu.num_pri_bits = ((kvm_vgic_global_state.ich_vtr_el2 &
313 ICH_VTR_PRI_BITS_MASK) >>
314 ICH_VTR_PRI_BITS_SHIFT) + 1;
315
Eric Augerf7b69852015-12-02 10:30:13 +0100316 /* Get the show on the road... */
317 vgic_v3->vgic_hcr = ICH_HCR_EN;
Marc Zyngierabf55762017-06-09 12:49:45 +0100318 if (group0_trap)
319 vgic_v3->vgic_hcr |= ICH_HCR_TALL0;
Marc Zyngier9c7bfc22017-06-09 12:49:40 +0100320 if (group1_trap)
321 vgic_v3->vgic_hcr |= ICH_HCR_TALL1;
Marc Zyngierff895112017-06-09 12:49:53 +0100322 if (common_trap)
323 vgic_v3->vgic_hcr |= ICH_HCR_TC;
Eric Augerad275b8b2015-12-21 18:09:38 +0100324}
325
Eric Auger44de9d62017-05-04 11:19:52 +0200326int vgic_v3_lpi_sync_pending_status(struct kvm *kvm, struct vgic_irq *irq)
327{
328 struct kvm_vcpu *vcpu;
329 int byte_offset, bit_nr;
330 gpa_t pendbase, ptr;
331 bool status;
332 u8 val;
333 int ret;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200334 unsigned long flags;
Eric Auger44de9d62017-05-04 11:19:52 +0200335
336retry:
337 vcpu = irq->target_vcpu;
338 if (!vcpu)
339 return 0;
340
341 pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
342
343 byte_offset = irq->intid / BITS_PER_BYTE;
344 bit_nr = irq->intid % BITS_PER_BYTE;
345 ptr = pendbase + byte_offset;
346
347 ret = kvm_read_guest(kvm, ptr, &val, 1);
348 if (ret)
349 return ret;
350
351 status = val & (1 << bit_nr);
352
Christoffer Dall006df0f2016-10-16 22:19:11 +0200353 spin_lock_irqsave(&irq->irq_lock, flags);
Eric Auger44de9d62017-05-04 11:19:52 +0200354 if (irq->target_vcpu != vcpu) {
Christoffer Dall006df0f2016-10-16 22:19:11 +0200355 spin_unlock_irqrestore(&irq->irq_lock, flags);
Eric Auger44de9d62017-05-04 11:19:52 +0200356 goto retry;
357 }
358 irq->pending_latch = status;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200359 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
Eric Auger44de9d62017-05-04 11:19:52 +0200360
361 if (status) {
362 /* clear consumed data */
363 val &= ~(1 << bit_nr);
364 ret = kvm_write_guest(kvm, ptr, &val, 1);
365 if (ret)
366 return ret;
367 }
368 return 0;
369}
370
Eric Auger28077122017-01-09 16:28:27 +0100371/**
372 * vgic_its_save_pending_tables - Save the pending tables into guest RAM
373 * kvm lock and all vcpu lock must be held
374 */
375int vgic_v3_save_pending_tables(struct kvm *kvm)
376{
377 struct vgic_dist *dist = &kvm->arch.vgic;
378 int last_byte_offset = -1;
379 struct vgic_irq *irq;
380 int ret;
Marc Zyngierddb4b012017-11-16 17:58:16 +0000381 u8 val;
Eric Auger28077122017-01-09 16:28:27 +0100382
383 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
384 int byte_offset, bit_nr;
385 struct kvm_vcpu *vcpu;
386 gpa_t pendbase, ptr;
387 bool stored;
Eric Auger28077122017-01-09 16:28:27 +0100388
389 vcpu = irq->target_vcpu;
390 if (!vcpu)
391 continue;
392
393 pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
394
395 byte_offset = irq->intid / BITS_PER_BYTE;
396 bit_nr = irq->intid % BITS_PER_BYTE;
397 ptr = pendbase + byte_offset;
398
399 if (byte_offset != last_byte_offset) {
400 ret = kvm_read_guest(kvm, ptr, &val, 1);
401 if (ret)
402 return ret;
403 last_byte_offset = byte_offset;
404 }
405
406 stored = val & (1U << bit_nr);
407 if (stored == irq->pending_latch)
408 continue;
409
410 if (irq->pending_latch)
411 val |= 1 << bit_nr;
412 else
413 val &= ~(1 << bit_nr);
414
415 ret = kvm_write_guest(kvm, ptr, &val, 1);
416 if (ret)
417 return ret;
418 }
419 return 0;
420}
421
Christoffer Dall9a746d72017-05-08 12:23:51 +0200422/*
423 * Check for overlapping regions and for regions crossing the end of memory
424 * for base addresses which have already been set.
425 */
426bool vgic_v3_check_base(struct kvm *kvm)
Eric Augerb0442ee2015-12-21 15:04:42 +0100427{
428 struct vgic_dist *d = &kvm->arch.vgic;
429 gpa_t redist_size = KVM_VGIC_V3_REDIST_SIZE;
Eric Augerdbd97332018-05-22 09:55:08 +0200430 struct vgic_redist_region *rdreg =
431 list_first_entry(&d->rd_regions,
432 struct vgic_redist_region, list);
Eric Augerb0442ee2015-12-21 15:04:42 +0100433
434 redist_size *= atomic_read(&kvm->online_vcpus);
435
Christoffer Dall9a746d72017-05-08 12:23:51 +0200436 if (!IS_VGIC_ADDR_UNDEF(d->vgic_dist_base) &&
437 d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE < d->vgic_dist_base)
Eric Augerb0442ee2015-12-21 15:04:42 +0100438 return false;
Christoffer Dall9a746d72017-05-08 12:23:51 +0200439
Eric Augerdbd97332018-05-22 09:55:08 +0200440 if (rdreg && (rdreg->base + redist_size < rdreg->base))
Eric Augerb0442ee2015-12-21 15:04:42 +0100441 return false;
442
Christoffer Dall9a746d72017-05-08 12:23:51 +0200443 /* Both base addresses must be set to check if they overlap */
Eric Augerdbd97332018-05-22 09:55:08 +0200444 if (IS_VGIC_ADDR_UNDEF(d->vgic_dist_base) || !rdreg)
Christoffer Dall9a746d72017-05-08 12:23:51 +0200445 return true;
446
Eric Augerdbd97332018-05-22 09:55:08 +0200447 if (d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE <= rdreg->base)
Eric Augerb0442ee2015-12-21 15:04:42 +0100448 return true;
Eric Augerdbd97332018-05-22 09:55:08 +0200449
450 if (rdreg->base + redist_size <= d->vgic_dist_base)
Eric Augerb0442ee2015-12-21 15:04:42 +0100451 return true;
452
453 return false;
454}
455
Eric Augerdc524612018-05-22 09:55:09 +0200456/**
457 * vgic_v3_rdist_free_slot - Look up registered rdist regions and identify one
458 * which has free space to put a new rdist region.
459 *
460 * @rd_regions: redistributor region list head
461 *
462 * A redistributor regions maps n redistributors, n = region size / (2 x 64kB).
463 * Stride between redistributors is 0 and regions are filled in the index order.
464 *
465 * Return: the redist region handle, if any, that has space to map a new rdist
466 * region.
467 */
468struct vgic_redist_region *vgic_v3_rdist_free_slot(struct list_head *rd_regions)
469{
470 struct vgic_redist_region *rdreg;
471
472 list_for_each_entry(rdreg, rd_regions, list) {
473 if (!vgic_v3_redist_region_full(rdreg))
474 return rdreg;
475 }
476 return NULL;
477}
478
Eric Augerb0442ee2015-12-21 15:04:42 +0100479int vgic_v3_map_resources(struct kvm *kvm)
480{
481 int ret = 0;
482 struct vgic_dist *dist = &kvm->arch.vgic;
Eric Augerdbd97332018-05-22 09:55:08 +0200483 struct vgic_redist_region *rdreg =
484 list_first_entry(&dist->rd_regions,
485 struct vgic_redist_region, list);
Eric Augerb0442ee2015-12-21 15:04:42 +0100486
487 if (vgic_ready(kvm))
488 goto out;
489
Eric Augerdbd97332018-05-22 09:55:08 +0200490 if (IS_VGIC_ADDR_UNDEF(dist->vgic_dist_base) || !rdreg) {
Eric Augerb0442ee2015-12-21 15:04:42 +0100491 kvm_err("Need to set vgic distributor addresses first\n");
492 ret = -ENXIO;
493 goto out;
494 }
495
496 if (!vgic_v3_check_base(kvm)) {
497 kvm_err("VGIC redist and dist frames overlap\n");
498 ret = -EINVAL;
499 goto out;
500 }
501
502 /*
503 * For a VGICv3 we require the userland to explicitly initialize
504 * the VGIC before we need to use it.
505 */
506 if (!vgic_initialized(kvm)) {
507 ret = -EBUSY;
508 goto out;
509 }
510
511 ret = vgic_register_dist_iodev(kvm, dist->vgic_dist_base, VGIC_V3);
512 if (ret) {
513 kvm_err("Unable to register VGICv3 dist MMIO regions\n");
514 goto out;
515 }
516
Eric Augerb0442ee2015-12-21 15:04:42 +0100517 dist->ready = true;
518
519out:
Eric Augerb0442ee2015-12-21 15:04:42 +0100520 return ret;
521}
522
Marc Zyngier59da1cb2017-06-09 12:49:33 +0100523DEFINE_STATIC_KEY_FALSE(vgic_v3_cpuif_trap);
524
Marc Zyngiere23f62f2017-06-09 12:49:46 +0100525static int __init early_group0_trap_cfg(char *buf)
526{
527 return strtobool(buf, &group0_trap);
528}
529early_param("kvm-arm.vgic_v3_group0_trap", early_group0_trap_cfg);
530
Marc Zyngier182936e2017-06-09 12:49:41 +0100531static int __init early_group1_trap_cfg(char *buf)
532{
533 return strtobool(buf, &group1_trap);
534}
535early_param("kvm-arm.vgic_v3_group1_trap", early_group1_trap_cfg);
536
Marc Zyngierff895112017-06-09 12:49:53 +0100537static int __init early_common_trap_cfg(char *buf)
538{
539 return strtobool(buf, &common_trap);
540}
541early_param("kvm-arm.vgic_v3_common_trap", early_common_trap_cfg);
542
Marc Zyngiera7546052017-10-27 15:28:54 +0100543static int __init early_gicv4_enable(char *buf)
544{
545 return strtobool(buf, &gicv4_enable);
546}
547early_param("kvm-arm.vgic_v4_enable", early_gicv4_enable);
548
Eric Auger90977732015-12-01 15:02:35 +0100549/**
550 * vgic_v3_probe - probe for a GICv3 compatible interrupt controller in DT
551 * @node: pointer to the DT node
552 *
553 * Returns 0 if a GICv3 has been found, returns an error code otherwise
554 */
555int vgic_v3_probe(const struct gic_kvm_info *info)
556{
557 u32 ich_vtr_el2 = kvm_call_hyp(__vgic_v3_get_ich_vtr_el2);
Andre Przywara42c88702016-07-15 12:43:23 +0100558 int ret;
Eric Auger90977732015-12-01 15:02:35 +0100559
560 /*
561 * The ListRegs field is 5 bits, but there is a architectural
562 * maximum of 16 list registers. Just ignore bit 4...
563 */
564 kvm_vgic_global_state.nr_lr = (ich_vtr_el2 & 0xf) + 1;
565 kvm_vgic_global_state.can_emulate_gicv2 = false;
Vijaya Kumar Kd017d7b2017-01-26 19:50:51 +0530566 kvm_vgic_global_state.ich_vtr_el2 = ich_vtr_el2;
Eric Auger90977732015-12-01 15:02:35 +0100567
Marc Zyngiera7546052017-10-27 15:28:54 +0100568 /* GICv4 support? */
569 if (info->has_v4) {
570 kvm_vgic_global_state.has_gicv4 = gicv4_enable;
571 kvm_info("GICv4 support %sabled\n",
572 gicv4_enable ? "en" : "dis");
573 }
574
Eric Auger90977732015-12-01 15:02:35 +0100575 if (!info->vcpu.start) {
576 kvm_info("GICv3: no GICV resource entry\n");
577 kvm_vgic_global_state.vcpu_base = 0;
578 } else if (!PAGE_ALIGNED(info->vcpu.start)) {
579 pr_warn("GICV physical address 0x%llx not page aligned\n",
580 (unsigned long long)info->vcpu.start);
581 kvm_vgic_global_state.vcpu_base = 0;
582 } else if (!PAGE_ALIGNED(resource_size(&info->vcpu))) {
583 pr_warn("GICV size 0x%llx not a multiple of page size 0x%lx\n",
584 (unsigned long long)resource_size(&info->vcpu),
585 PAGE_SIZE);
586 kvm_vgic_global_state.vcpu_base = 0;
587 } else {
588 kvm_vgic_global_state.vcpu_base = info->vcpu.start;
589 kvm_vgic_global_state.can_emulate_gicv2 = true;
Andre Przywara42c88702016-07-15 12:43:23 +0100590 ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V2);
591 if (ret) {
592 kvm_err("Cannot register GICv2 KVM device.\n");
593 return ret;
594 }
Eric Auger90977732015-12-01 15:02:35 +0100595 kvm_info("vgic-v2@%llx\n", info->vcpu.start);
596 }
Andre Przywara42c88702016-07-15 12:43:23 +0100597 ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V3);
598 if (ret) {
599 kvm_err("Cannot register GICv3 KVM device.\n");
600 kvm_unregister_device_ops(KVM_DEV_TYPE_ARM_VGIC_V2);
601 return ret;
602 }
603
Eric Auger90977732015-12-01 15:02:35 +0100604 if (kvm_vgic_global_state.vcpu_base == 0)
605 kvm_info("disabling GICv2 emulation\n");
Eric Auger90977732015-12-01 15:02:35 +0100606
David Daney690a3412017-06-09 12:49:48 +0100607#ifdef CONFIG_ARM64
608 if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_30115)) {
609 group0_trap = true;
610 group1_trap = true;
611 }
612#endif
613
Marc Zyngierff895112017-06-09 12:49:53 +0100614 if (group0_trap || group1_trap || common_trap) {
Marc Zyngier2873b502017-06-09 12:49:54 +0100615 kvm_info("GICv3 sysreg trapping enabled ([%s%s%s], reduced performance)\n",
616 group0_trap ? "G0" : "",
617 group1_trap ? "G1" : "",
618 common_trap ? "C" : "");
Marc Zyngier182936e2017-06-09 12:49:41 +0100619 static_branch_enable(&vgic_v3_cpuif_trap);
620 }
621
Eric Auger90977732015-12-01 15:02:35 +0100622 kvm_vgic_global_state.vctrl_base = NULL;
623 kvm_vgic_global_state.type = VGIC_V3;
624 kvm_vgic_global_state.max_gic_vcpus = VGIC_V3_MAX_CPUS;
625
626 return 0;
627}
Christoffer Dall328e5662016-03-24 11:21:04 +0100628
629void vgic_v3_load(struct kvm_vcpu *vcpu)
630{
631 struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
632
Marc Zyngierff567612017-04-19 12:15:26 +0100633 /*
634 * If dealing with a GICv2 emulation on GICv3, VMCR_EL2.VFIQen
635 * is dependent on ICC_SRE_EL1.SRE, and we have to perform the
636 * VMCR_EL2 save/restore in the world switch.
637 */
638 if (likely(cpu_if->vgic_sre))
639 kvm_call_hyp(__vgic_v3_write_vmcr, cpu_if->vgic_vmcr);
Christoffer Dall923a2e32017-10-05 00:18:07 +0200640
641 kvm_call_hyp(__vgic_v3_restore_aprs, vcpu);
Christoffer Dall2d0e63e2017-10-05 17:19:19 +0200642
643 if (has_vhe())
644 __vgic_v3_activate_traps(vcpu);
Christoffer Dall328e5662016-03-24 11:21:04 +0100645}
646
647void vgic_v3_put(struct kvm_vcpu *vcpu)
648{
649 struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
650
Marc Zyngierff567612017-04-19 12:15:26 +0100651 if (likely(cpu_if->vgic_sre))
652 cpu_if->vgic_vmcr = kvm_call_hyp(__vgic_v3_read_vmcr);
Christoffer Dall923a2e32017-10-05 00:18:07 +0200653
654 kvm_call_hyp(__vgic_v3_save_aprs, vcpu);
Christoffer Dall2d0e63e2017-10-05 17:19:19 +0200655
656 if (has_vhe())
657 __vgic_v3_deactivate_traps(vcpu);
Christoffer Dall328e5662016-03-24 11:21:04 +0100658}