blob: 1e5f3eb6973d78b04629253a30f1e2ad35f915d4 [file] [log] [blame]
Marc Zyngier140b0862015-11-26 17:19:25 +00001/*
2 * Copyright (C) 2015, 2016 ARM Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <linux/irqchip/arm-gic.h>
18#include <linux/kvm.h>
19#include <linux/kvm_host.h>
Eric Auger90977732015-12-01 15:02:35 +010020#include <kvm/arm_vgic.h>
21#include <asm/kvm_mmu.h>
Marc Zyngier140b0862015-11-26 17:19:25 +000022
23#include "vgic.h"
24
Christoffer Dall5b0d2cc2017-03-18 13:56:56 +010025static inline void vgic_v2_write_lr(int lr, u32 val)
26{
27 void __iomem *base = kvm_vgic_global_state.vctrl_base;
28
29 writel_relaxed(val, base + GICH_LR0 + (lr * 4));
30}
31
32void vgic_v2_init_lrs(void)
33{
34 int i;
35
36 for (i = 0; i < kvm_vgic_global_state.nr_lr; i++)
37 vgic_v2_write_lr(i, 0);
38}
39
Marc Zyngier140b0862015-11-26 17:19:25 +000040void vgic_v2_set_underflow(struct kvm_vcpu *vcpu)
41{
42 struct vgic_v2_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v2;
43
44 cpuif->vgic_hcr |= GICH_HCR_UIE;
45}
46
Christoffer Dallaf061492016-12-29 15:44:27 +010047static bool lr_signals_eoi_mi(u32 lr_val)
48{
49 return !(lr_val & GICH_LR_STATE) && (lr_val & GICH_LR_EOI) &&
50 !(lr_val & GICH_LR_HW);
51}
52
Marc Zyngier140b0862015-11-26 17:19:25 +000053/*
54 * transfer the content of the LRs back into the corresponding ap_list:
55 * - active bit is transferred as is
56 * - pending bit is
57 * - transferred as is in case of edge sensitive IRQs
58 * - set to the line-level (resample time) for level sensitive IRQs
59 */
60void vgic_v2_fold_lr_state(struct kvm_vcpu *vcpu)
61{
Christoffer Dall8ac76ef2017-03-18 13:48:42 +010062 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
63 struct vgic_v2_cpu_if *cpuif = &vgic_cpu->vgic_v2;
Marc Zyngier140b0862015-11-26 17:19:25 +000064 int lr;
Christoffer Dall006df0f2016-10-16 22:19:11 +020065 unsigned long flags;
Marc Zyngier140b0862015-11-26 17:19:25 +000066
Christoffer Dallaf061492016-12-29 15:44:27 +010067 cpuif->vgic_hcr &= ~GICH_HCR_UIE;
68
Christoffer Dall8ac76ef2017-03-18 13:48:42 +010069 for (lr = 0; lr < vgic_cpu->used_lrs; lr++) {
Marc Zyngier140b0862015-11-26 17:19:25 +000070 u32 val = cpuif->vgic_lr[lr];
71 u32 intid = val & GICH_LR_VIRTUALID;
72 struct vgic_irq *irq;
73
Christoffer Dallaf061492016-12-29 15:44:27 +010074 /* Notify fds when the guest EOI'ed a level-triggered SPI */
75 if (lr_signals_eoi_mi(val) && vgic_valid_spi(vcpu->kvm, intid))
76 kvm_notify_acked_irq(vcpu->kvm, 0,
77 intid - VGIC_NR_PRIVATE_IRQS);
78
Marc Zyngier140b0862015-11-26 17:19:25 +000079 irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
80
Christoffer Dall006df0f2016-10-16 22:19:11 +020081 spin_lock_irqsave(&irq->irq_lock, flags);
Marc Zyngier140b0862015-11-26 17:19:25 +000082
83 /* Always preserve the active bit */
84 irq->active = !!(val & GICH_LR_ACTIVE_BIT);
85
86 /* Edge is the only case where we preserve the pending bit */
87 if (irq->config == VGIC_CONFIG_EDGE &&
88 (val & GICH_LR_PENDING_BIT)) {
Christoffer Dall8694e4d2017-01-23 14:07:18 +010089 irq->pending_latch = true;
Marc Zyngier140b0862015-11-26 17:19:25 +000090
91 if (vgic_irq_is_sgi(intid)) {
92 u32 cpuid = val & GICH_LR_PHYSID_CPUID;
93
94 cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT;
95 irq->source |= (1 << cpuid);
96 }
97 }
98
Marc Zyngierdf7942d2016-05-25 15:26:35 +010099 /*
100 * Clear soft pending state when level irqs have been acked.
101 * Always regenerate the pending state.
102 */
103 if (irq->config == VGIC_CONFIG_LEVEL) {
104 if (!(val & GICH_LR_PENDING_BIT))
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100105 irq->pending_latch = false;
Marc Zyngier140b0862015-11-26 17:19:25 +0000106 }
107
Christoffer Dalle40cc572017-08-29 10:40:44 +0200108 /*
109 * Level-triggered mapped IRQs are special because we only
110 * observe rising edges as input to the VGIC.
111 *
112 * If the guest never acked the interrupt we have to sample
113 * the physical line and set the line level, because the
114 * device state could have changed or we simply need to
115 * process the still pending interrupt later.
116 *
117 * If this causes us to lower the level, we have to also clear
118 * the physical active state, since we will otherwise never be
119 * told when the interrupt becomes asserted again.
120 */
121 if (vgic_irq_is_mapped_level(irq) && (val & GICH_LR_PENDING_BIT)) {
122 irq->line_level = vgic_get_phys_line_level(irq);
123
124 if (!irq->line_level)
125 vgic_irq_set_phys_active(irq, false);
126 }
127
Christoffer Dall006df0f2016-10-16 22:19:11 +0200128 spin_unlock_irqrestore(&irq->irq_lock, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100129 vgic_put_irq(vcpu->kvm, irq);
Marc Zyngier140b0862015-11-26 17:19:25 +0000130 }
Christoffer Dall8ac76ef2017-03-18 13:48:42 +0100131
132 vgic_cpu->used_lrs = 0;
Marc Zyngier140b0862015-11-26 17:19:25 +0000133}
134
135/*
136 * Populates the particular LR with the state of a given IRQ:
137 * - for an edge sensitive IRQ the pending state is cleared in struct vgic_irq
138 * - for a level sensitive IRQ the pending state value is unchanged;
139 * it is dictated directly by the input level
140 *
141 * If @irq describes an SGI with multiple sources, we choose the
142 * lowest-numbered source VCPU and clear that bit in the source bitmap.
143 *
144 * The irq_lock must be held by the caller.
145 */
146void vgic_v2_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr)
147{
148 u32 val = irq->intid;
149
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100150 if (irq_is_pending(irq)) {
Marc Zyngier140b0862015-11-26 17:19:25 +0000151 val |= GICH_LR_PENDING_BIT;
152
153 if (irq->config == VGIC_CONFIG_EDGE)
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100154 irq->pending_latch = false;
Marc Zyngier140b0862015-11-26 17:19:25 +0000155
156 if (vgic_irq_is_sgi(irq->intid)) {
157 u32 src = ffs(irq->source);
158
159 BUG_ON(!src);
160 val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT;
161 irq->source &= ~(1 << (src - 1));
162 if (irq->source)
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100163 irq->pending_latch = true;
Marc Zyngier140b0862015-11-26 17:19:25 +0000164 }
165 }
166
167 if (irq->active)
168 val |= GICH_LR_ACTIVE_BIT;
169
170 if (irq->hw) {
171 val |= GICH_LR_HW;
172 val |= irq->hwintid << GICH_LR_PHYSID_CPUID_SHIFT;
Marc Zyngierddf42d02017-05-02 14:30:39 +0100173 /*
174 * Never set pending+active on a HW interrupt, as the
175 * pending state is kept at the physical distributor
176 * level.
177 */
178 if (irq->active && irq_is_pending(irq))
179 val &= ~GICH_LR_PENDING_BIT;
Marc Zyngier140b0862015-11-26 17:19:25 +0000180 } else {
181 if (irq->config == VGIC_CONFIG_LEVEL)
182 val |= GICH_LR_EOI;
183 }
184
Christoffer Dalle40cc572017-08-29 10:40:44 +0200185 /*
186 * Level-triggered mapped IRQs are special because we only observe
187 * rising edges as input to the VGIC. We therefore lower the line
188 * level here, so that we can take new virtual IRQs. See
189 * vgic_v2_fold_lr_state for more info.
190 */
191 if (vgic_irq_is_mapped_level(irq) && (val & GICH_LR_PENDING_BIT))
192 irq->line_level = false;
193
Marc Zyngier140b0862015-11-26 17:19:25 +0000194 /* The GICv2 LR only holds five bits of priority. */
195 val |= (irq->priority >> 3) << GICH_LR_PRIORITY_SHIFT;
196
197 vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = val;
198}
199
200void vgic_v2_clear_lr(struct kvm_vcpu *vcpu, int lr)
201{
202 vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = 0;
203}
Andre Przywarae4823a72015-12-03 11:47:37 +0000204
205void vgic_v2_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
206{
Christoffer Dall328e56642016-03-24 11:21:04 +0100207 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
Andre Przywarae4823a72015-12-03 11:47:37 +0000208 u32 vmcr;
209
Christoffer Dall28232a42017-05-20 14:12:34 +0200210 vmcr = (vmcrp->grpen0 << GICH_VMCR_ENABLE_GRP0_SHIFT) &
211 GICH_VMCR_ENABLE_GRP0_MASK;
212 vmcr |= (vmcrp->grpen1 << GICH_VMCR_ENABLE_GRP1_SHIFT) &
213 GICH_VMCR_ENABLE_GRP1_MASK;
214 vmcr |= (vmcrp->ackctl << GICH_VMCR_ACK_CTL_SHIFT) &
215 GICH_VMCR_ACK_CTL_MASK;
216 vmcr |= (vmcrp->fiqen << GICH_VMCR_FIQ_EN_SHIFT) &
217 GICH_VMCR_FIQ_EN_MASK;
218 vmcr |= (vmcrp->cbpr << GICH_VMCR_CBPR_SHIFT) &
219 GICH_VMCR_CBPR_MASK;
220 vmcr |= (vmcrp->eoim << GICH_VMCR_EOI_MODE_SHIFT) &
221 GICH_VMCR_EOI_MODE_MASK;
Andre Przywarae4823a72015-12-03 11:47:37 +0000222 vmcr |= (vmcrp->abpr << GICH_VMCR_ALIAS_BINPOINT_SHIFT) &
223 GICH_VMCR_ALIAS_BINPOINT_MASK;
224 vmcr |= (vmcrp->bpr << GICH_VMCR_BINPOINT_SHIFT) &
225 GICH_VMCR_BINPOINT_MASK;
Christoffer Dall6d561112017-03-21 22:05:22 +0100226 vmcr |= ((vmcrp->pmr >> GICV_PMR_PRIORITY_SHIFT) <<
227 GICH_VMCR_PRIMASK_SHIFT) & GICH_VMCR_PRIMASK_MASK;
Andre Przywarae4823a72015-12-03 11:47:37 +0000228
Christoffer Dall328e56642016-03-24 11:21:04 +0100229 cpu_if->vgic_vmcr = vmcr;
Andre Przywarae4823a72015-12-03 11:47:37 +0000230}
231
232void vgic_v2_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
233{
Christoffer Dall328e56642016-03-24 11:21:04 +0100234 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
235 u32 vmcr;
236
237 vmcr = cpu_if->vgic_vmcr;
Andre Przywarae4823a72015-12-03 11:47:37 +0000238
Christoffer Dall28232a42017-05-20 14:12:34 +0200239 vmcrp->grpen0 = (vmcr & GICH_VMCR_ENABLE_GRP0_MASK) >>
240 GICH_VMCR_ENABLE_GRP0_SHIFT;
241 vmcrp->grpen1 = (vmcr & GICH_VMCR_ENABLE_GRP1_MASK) >>
242 GICH_VMCR_ENABLE_GRP1_SHIFT;
243 vmcrp->ackctl = (vmcr & GICH_VMCR_ACK_CTL_MASK) >>
244 GICH_VMCR_ACK_CTL_SHIFT;
245 vmcrp->fiqen = (vmcr & GICH_VMCR_FIQ_EN_MASK) >>
246 GICH_VMCR_FIQ_EN_SHIFT;
247 vmcrp->cbpr = (vmcr & GICH_VMCR_CBPR_MASK) >>
248 GICH_VMCR_CBPR_SHIFT;
249 vmcrp->eoim = (vmcr & GICH_VMCR_EOI_MODE_MASK) >>
250 GICH_VMCR_EOI_MODE_SHIFT;
251
Andre Przywarae4823a72015-12-03 11:47:37 +0000252 vmcrp->abpr = (vmcr & GICH_VMCR_ALIAS_BINPOINT_MASK) >>
253 GICH_VMCR_ALIAS_BINPOINT_SHIFT;
254 vmcrp->bpr = (vmcr & GICH_VMCR_BINPOINT_MASK) >>
255 GICH_VMCR_BINPOINT_SHIFT;
Christoffer Dall6d561112017-03-21 22:05:22 +0100256 vmcrp->pmr = ((vmcr & GICH_VMCR_PRIMASK_MASK) >>
257 GICH_VMCR_PRIMASK_SHIFT) << GICV_PMR_PRIORITY_SHIFT;
Andre Przywarae4823a72015-12-03 11:47:37 +0000258}
Eric Auger90977732015-12-01 15:02:35 +0100259
Eric Augerad275b8b2015-12-21 18:09:38 +0100260void vgic_v2_enable(struct kvm_vcpu *vcpu)
261{
Eric Augerf7b69852015-12-02 10:30:13 +0100262 /*
263 * By forcing VMCR to zero, the GIC will restore the binary
264 * points to their reset values. Anything else resets to zero
265 * anyway.
266 */
267 vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr = 0;
Eric Augerf7b69852015-12-02 10:30:13 +0100268
269 /* Get the show on the road... */
270 vcpu->arch.vgic_cpu.vgic_v2.vgic_hcr = GICH_HCR_EN;
Eric Augerad275b8b2015-12-21 18:09:38 +0100271}
272
Eric Augerb0442ee2015-12-21 15:04:42 +0100273/* check for overlapping regions and for regions crossing the end of memory */
274static bool vgic_v2_check_base(gpa_t dist_base, gpa_t cpu_base)
275{
276 if (dist_base + KVM_VGIC_V2_DIST_SIZE < dist_base)
277 return false;
278 if (cpu_base + KVM_VGIC_V2_CPU_SIZE < cpu_base)
279 return false;
280
281 if (dist_base + KVM_VGIC_V2_DIST_SIZE <= cpu_base)
282 return true;
283 if (cpu_base + KVM_VGIC_V2_CPU_SIZE <= dist_base)
284 return true;
285
286 return false;
287}
288
289int vgic_v2_map_resources(struct kvm *kvm)
290{
291 struct vgic_dist *dist = &kvm->arch.vgic;
292 int ret = 0;
293
294 if (vgic_ready(kvm))
295 goto out;
296
297 if (IS_VGIC_ADDR_UNDEF(dist->vgic_dist_base) ||
298 IS_VGIC_ADDR_UNDEF(dist->vgic_cpu_base)) {
299 kvm_err("Need to set vgic cpu and dist addresses first\n");
300 ret = -ENXIO;
301 goto out;
302 }
303
304 if (!vgic_v2_check_base(dist->vgic_dist_base, dist->vgic_cpu_base)) {
305 kvm_err("VGIC CPU and dist frames overlap\n");
306 ret = -EINVAL;
307 goto out;
308 }
309
310 /*
311 * Initialize the vgic if this hasn't already been done on demand by
312 * accessing the vgic state from userspace.
313 */
314 ret = vgic_init(kvm);
315 if (ret) {
316 kvm_err("Unable to initialize VGIC dynamic data structures\n");
317 goto out;
318 }
319
320 ret = vgic_register_dist_iodev(kvm, dist->vgic_dist_base, VGIC_V2);
321 if (ret) {
322 kvm_err("Unable to register VGIC MMIO regions\n");
323 goto out;
324 }
325
Marc Zyngiera07d3b02016-09-06 09:28:47 +0100326 if (!static_branch_unlikely(&vgic_v2_cpuif_trap)) {
327 ret = kvm_phys_addr_ioremap(kvm, dist->vgic_cpu_base,
328 kvm_vgic_global_state.vcpu_base,
329 KVM_VGIC_V2_CPU_SIZE, true);
330 if (ret) {
331 kvm_err("Unable to remap VGIC CPU to VCPU\n");
332 goto out;
333 }
Eric Augerb0442ee2015-12-21 15:04:42 +0100334 }
335
336 dist->ready = true;
337
338out:
Eric Augerb0442ee2015-12-21 15:04:42 +0100339 return ret;
340}
341
Marc Zyngierfb5ee362016-09-06 09:28:45 +0100342DEFINE_STATIC_KEY_FALSE(vgic_v2_cpuif_trap);
343
Eric Auger90977732015-12-01 15:02:35 +0100344/**
345 * vgic_v2_probe - probe for a GICv2 compatible interrupt controller in DT
346 * @node: pointer to the DT node
347 *
348 * Returns 0 if a GICv2 has been found, returns an error code otherwise
349 */
350int vgic_v2_probe(const struct gic_kvm_info *info)
351{
352 int ret;
353 u32 vtr;
354
355 if (!info->vctrl.start) {
356 kvm_err("GICH not present in the firmware table\n");
357 return -ENXIO;
358 }
359
Marc Zyngiera07d3b02016-09-06 09:28:47 +0100360 if (!PAGE_ALIGNED(info->vcpu.start) ||
361 !PAGE_ALIGNED(resource_size(&info->vcpu))) {
362 kvm_info("GICV region size/alignment is unsafe, using trapping (reduced performance)\n");
363 kvm_vgic_global_state.vcpu_base_va = ioremap(info->vcpu.start,
364 resource_size(&info->vcpu));
365 if (!kvm_vgic_global_state.vcpu_base_va) {
366 kvm_err("Cannot ioremap GICV\n");
367 return -ENOMEM;
368 }
Eric Auger90977732015-12-01 15:02:35 +0100369
Marc Zyngiera07d3b02016-09-06 09:28:47 +0100370 ret = create_hyp_io_mappings(kvm_vgic_global_state.vcpu_base_va,
371 kvm_vgic_global_state.vcpu_base_va + resource_size(&info->vcpu),
372 info->vcpu.start);
373 if (ret) {
374 kvm_err("Cannot map GICV into hyp\n");
375 goto out;
376 }
377
378 static_branch_enable(&vgic_v2_cpuif_trap);
Eric Auger90977732015-12-01 15:02:35 +0100379 }
380
381 kvm_vgic_global_state.vctrl_base = ioremap(info->vctrl.start,
382 resource_size(&info->vctrl));
383 if (!kvm_vgic_global_state.vctrl_base) {
384 kvm_err("Cannot ioremap GICH\n");
Marc Zyngiera07d3b02016-09-06 09:28:47 +0100385 ret = -ENOMEM;
386 goto out;
Eric Auger90977732015-12-01 15:02:35 +0100387 }
388
389 vtr = readl_relaxed(kvm_vgic_global_state.vctrl_base + GICH_VTR);
390 kvm_vgic_global_state.nr_lr = (vtr & 0x3f) + 1;
391
392 ret = create_hyp_io_mappings(kvm_vgic_global_state.vctrl_base,
393 kvm_vgic_global_state.vctrl_base +
394 resource_size(&info->vctrl),
395 info->vctrl.start);
Eric Auger90977732015-12-01 15:02:35 +0100396 if (ret) {
397 kvm_err("Cannot map VCTRL into hyp\n");
Marc Zyngiera07d3b02016-09-06 09:28:47 +0100398 goto out;
399 }
400
401 ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V2);
402 if (ret) {
403 kvm_err("Cannot register GICv2 KVM device\n");
404 goto out;
Eric Auger90977732015-12-01 15:02:35 +0100405 }
406
407 kvm_vgic_global_state.can_emulate_gicv2 = true;
Eric Auger90977732015-12-01 15:02:35 +0100408 kvm_vgic_global_state.vcpu_base = info->vcpu.start;
409 kvm_vgic_global_state.type = VGIC_V2;
410 kvm_vgic_global_state.max_gic_vcpus = VGIC_V2_MAX_CPUS;
411
412 kvm_info("vgic-v2@%llx\n", info->vctrl.start);
413
414 return 0;
Marc Zyngiera07d3b02016-09-06 09:28:47 +0100415out:
416 if (kvm_vgic_global_state.vctrl_base)
417 iounmap(kvm_vgic_global_state.vctrl_base);
418 if (kvm_vgic_global_state.vcpu_base_va)
419 iounmap(kvm_vgic_global_state.vcpu_base_va);
420
421 return ret;
Eric Auger90977732015-12-01 15:02:35 +0100422}
Christoffer Dall328e56642016-03-24 11:21:04 +0100423
Christoffer Dall75174ba2016-12-22 20:39:10 +0100424static void save_lrs(struct kvm_vcpu *vcpu, void __iomem *base)
425{
426 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
427 u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
428 u64 elrsr;
429 int i;
430
431 elrsr = readl_relaxed(base + GICH_ELRSR0);
432 if (unlikely(used_lrs > 32))
433 elrsr |= ((u64)readl_relaxed(base + GICH_ELRSR1)) << 32;
434
435 for (i = 0; i < used_lrs; i++) {
436 if (elrsr & (1UL << i))
437 cpu_if->vgic_lr[i] &= ~GICH_LR_STATE;
438 else
439 cpu_if->vgic_lr[i] = readl_relaxed(base + GICH_LR0 + (i * 4));
440
441 writel_relaxed(0, base + GICH_LR0 + (i * 4));
442 }
443}
444
445void vgic_v2_save_state(struct kvm_vcpu *vcpu)
446{
447 struct kvm *kvm = vcpu->kvm;
448 struct vgic_dist *vgic = &kvm->arch.vgic;
449 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
450 void __iomem *base = vgic->vctrl_base;
451 u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
452
453 if (!base)
454 return;
455
456 if (used_lrs) {
457 cpu_if->vgic_apr = readl_relaxed(base + GICH_APR);
458 save_lrs(vcpu, base);
459 writel_relaxed(0, base + GICH_HCR);
460 } else {
461 cpu_if->vgic_apr = 0;
462 }
463}
464
465void vgic_v2_restore_state(struct kvm_vcpu *vcpu)
466{
467 struct kvm *kvm = vcpu->kvm;
468 struct vgic_dist *vgic = &kvm->arch.vgic;
469 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
470 void __iomem *base = vgic->vctrl_base;
471 u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
472 int i;
473
474 if (!base)
475 return;
476
477 if (used_lrs) {
478 writel_relaxed(cpu_if->vgic_hcr, base + GICH_HCR);
479 writel_relaxed(cpu_if->vgic_apr, base + GICH_APR);
480 for (i = 0; i < used_lrs; i++) {
481 writel_relaxed(cpu_if->vgic_lr[i],
482 base + GICH_LR0 + (i * 4));
483 }
484 }
485}
486
Christoffer Dall328e56642016-03-24 11:21:04 +0100487void vgic_v2_load(struct kvm_vcpu *vcpu)
488{
489 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
490 struct vgic_dist *vgic = &vcpu->kvm->arch.vgic;
491
492 writel_relaxed(cpu_if->vgic_vmcr, vgic->vctrl_base + GICH_VMCR);
493}
494
495void vgic_v2_put(struct kvm_vcpu *vcpu)
496{
497 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
498 struct vgic_dist *vgic = &vcpu->kvm->arch.vgic;
499
500 cpu_if->vgic_vmcr = readl_relaxed(vgic->vctrl_base + GICH_VMCR);
501}