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.
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.
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/>.
15 #include <linux/irqchip/arm-gic-v3.h>
16 #include <linux/kvm.h>
17 #include <linux/kvm_host.h>
18 #include <kvm/arm_vgic.h>
19 #include <asm/kvm_mmu.h>
20 #include <asm/kvm_asm.h>
24 static bool group0_trap
;
25 static bool group1_trap
;
26 static bool common_trap
;
27 static bool gicv4_enable
;
29 void vgic_v3_set_underflow(struct kvm_vcpu
*vcpu
)
31 struct vgic_v3_cpu_if
*cpuif
= &vcpu
->arch
.vgic_cpu
.vgic_v3
;
33 cpuif
->vgic_hcr
|= ICH_HCR_UIE
;
36 static bool lr_signals_eoi_mi(u64 lr_val
)
38 return !(lr_val
& ICH_LR_STATE
) && (lr_val
& ICH_LR_EOI
) &&
39 !(lr_val
& ICH_LR_HW
);
42 void vgic_v3_fold_lr_state(struct kvm_vcpu
*vcpu
)
44 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
45 struct vgic_v3_cpu_if
*cpuif
= &vgic_cpu
->vgic_v3
;
46 u32 model
= vcpu
->kvm
->arch
.vgic
.vgic_model
;
50 cpuif
->vgic_hcr
&= ~ICH_HCR_UIE
;
52 for (lr
= 0; lr
< vgic_cpu
->used_lrs
; lr
++) {
53 u64 val
= cpuif
->vgic_lr
[lr
];
57 if (model
== KVM_DEV_TYPE_ARM_VGIC_V3
)
58 intid
= val
& ICH_LR_VIRTUAL_ID_MASK
;
60 intid
= val
& GICH_LR_VIRTUALID
;
62 /* Notify fds when the guest EOI'ed a level-triggered IRQ */
63 if (lr_signals_eoi_mi(val
) && vgic_valid_spi(vcpu
->kvm
, intid
))
64 kvm_notify_acked_irq(vcpu
->kvm
, 0,
65 intid
- VGIC_NR_PRIVATE_IRQS
);
67 irq
= vgic_get_irq(vcpu
->kvm
, vcpu
, intid
);
68 if (!irq
) /* An LPI could have been unmapped. */
71 spin_lock_irqsave(&irq
->irq_lock
, flags
);
73 /* Always preserve the active bit */
74 irq
->active
= !!(val
& ICH_LR_ACTIVE_BIT
);
76 /* Edge is the only case where we preserve the pending bit */
77 if (irq
->config
== VGIC_CONFIG_EDGE
&&
78 (val
& ICH_LR_PENDING_BIT
)) {
79 irq
->pending_latch
= true;
81 if (vgic_irq_is_sgi(intid
) &&
82 model
== KVM_DEV_TYPE_ARM_VGIC_V2
) {
83 u32 cpuid
= val
& GICH_LR_PHYSID_CPUID
;
85 cpuid
>>= GICH_LR_PHYSID_CPUID_SHIFT
;
86 irq
->source
|= (1 << cpuid
);
91 * Clear soft pending state when level irqs have been acked.
92 * Always regenerate the pending state.
94 if (irq
->config
== VGIC_CONFIG_LEVEL
) {
95 if (!(val
& ICH_LR_PENDING_BIT
))
96 irq
->pending_latch
= false;
100 * Level-triggered mapped IRQs are special because we only
101 * observe rising edges as input to the VGIC.
103 * If the guest never acked the interrupt we have to sample
104 * the physical line and set the line level, because the
105 * device state could have changed or we simply need to
106 * process the still pending interrupt later.
108 * If this causes us to lower the level, we have to also clear
109 * the physical active state, since we will otherwise never be
110 * told when the interrupt becomes asserted again.
112 if (vgic_irq_is_mapped_level(irq
) && (val
& ICH_LR_PENDING_BIT
)) {
113 irq
->line_level
= vgic_get_phys_line_level(irq
);
115 if (!irq
->line_level
)
116 vgic_irq_set_phys_active(irq
, false);
119 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
120 vgic_put_irq(vcpu
->kvm
, irq
);
123 vgic_cpu
->used_lrs
= 0;
126 /* Requires the irq to be locked already */
127 void vgic_v3_populate_lr(struct kvm_vcpu
*vcpu
, struct vgic_irq
*irq
, int lr
)
129 u32 model
= vcpu
->kvm
->arch
.vgic
.vgic_model
;
130 u64 val
= irq
->intid
;
132 if (irq_is_pending(irq
)) {
133 val
|= ICH_LR_PENDING_BIT
;
135 if (irq
->config
== VGIC_CONFIG_EDGE
)
136 irq
->pending_latch
= false;
138 if (vgic_irq_is_sgi(irq
->intid
) &&
139 model
== KVM_DEV_TYPE_ARM_VGIC_V2
) {
140 u32 src
= ffs(irq
->source
);
143 val
|= (src
- 1) << GICH_LR_PHYSID_CPUID_SHIFT
;
144 irq
->source
&= ~(1 << (src
- 1));
146 irq
->pending_latch
= true;
151 val
|= ICH_LR_ACTIVE_BIT
;
155 val
|= ((u64
)irq
->hwintid
) << ICH_LR_PHYS_ID_SHIFT
;
157 * Never set pending+active on a HW interrupt, as the
158 * pending state is kept at the physical distributor
161 if (irq
->active
&& irq_is_pending(irq
))
162 val
&= ~ICH_LR_PENDING_BIT
;
164 if (irq
->config
== VGIC_CONFIG_LEVEL
)
169 * Level-triggered mapped IRQs are special because we only observe
170 * rising edges as input to the VGIC. We therefore lower the line
171 * level here, so that we can take new virtual IRQs. See
172 * vgic_v3_fold_lr_state for more info.
174 if (vgic_irq_is_mapped_level(irq
) && (val
& ICH_LR_PENDING_BIT
))
175 irq
->line_level
= false;
178 * We currently only support Group1 interrupts, which is a
179 * known defect. This needs to be addressed at some point.
181 if (model
== KVM_DEV_TYPE_ARM_VGIC_V3
)
184 val
|= (u64
)irq
->priority
<< ICH_LR_PRIORITY_SHIFT
;
186 vcpu
->arch
.vgic_cpu
.vgic_v3
.vgic_lr
[lr
] = val
;
189 void vgic_v3_clear_lr(struct kvm_vcpu
*vcpu
, int lr
)
191 vcpu
->arch
.vgic_cpu
.vgic_v3
.vgic_lr
[lr
] = 0;
194 void vgic_v3_set_vmcr(struct kvm_vcpu
*vcpu
, struct vgic_vmcr
*vmcrp
)
196 struct vgic_v3_cpu_if
*cpu_if
= &vcpu
->arch
.vgic_cpu
.vgic_v3
;
197 u32 model
= vcpu
->kvm
->arch
.vgic
.vgic_model
;
200 if (model
== KVM_DEV_TYPE_ARM_VGIC_V2
) {
201 vmcr
= (vmcrp
->ackctl
<< ICH_VMCR_ACK_CTL_SHIFT
) &
202 ICH_VMCR_ACK_CTL_MASK
;
203 vmcr
|= (vmcrp
->fiqen
<< ICH_VMCR_FIQ_EN_SHIFT
) &
204 ICH_VMCR_FIQ_EN_MASK
;
207 * When emulating GICv3 on GICv3 with SRE=1 on the
208 * VFIQEn bit is RES1 and the VAckCtl bit is RES0.
210 vmcr
= ICH_VMCR_FIQ_EN_MASK
;
213 vmcr
|= (vmcrp
->cbpr
<< ICH_VMCR_CBPR_SHIFT
) & ICH_VMCR_CBPR_MASK
;
214 vmcr
|= (vmcrp
->eoim
<< ICH_VMCR_EOIM_SHIFT
) & ICH_VMCR_EOIM_MASK
;
215 vmcr
|= (vmcrp
->abpr
<< ICH_VMCR_BPR1_SHIFT
) & ICH_VMCR_BPR1_MASK
;
216 vmcr
|= (vmcrp
->bpr
<< ICH_VMCR_BPR0_SHIFT
) & ICH_VMCR_BPR0_MASK
;
217 vmcr
|= (vmcrp
->pmr
<< ICH_VMCR_PMR_SHIFT
) & ICH_VMCR_PMR_MASK
;
218 vmcr
|= (vmcrp
->grpen0
<< ICH_VMCR_ENG0_SHIFT
) & ICH_VMCR_ENG0_MASK
;
219 vmcr
|= (vmcrp
->grpen1
<< ICH_VMCR_ENG1_SHIFT
) & ICH_VMCR_ENG1_MASK
;
221 cpu_if
->vgic_vmcr
= vmcr
;
224 void vgic_v3_get_vmcr(struct kvm_vcpu
*vcpu
, struct vgic_vmcr
*vmcrp
)
226 struct vgic_v3_cpu_if
*cpu_if
= &vcpu
->arch
.vgic_cpu
.vgic_v3
;
227 u32 model
= vcpu
->kvm
->arch
.vgic
.vgic_model
;
230 vmcr
= cpu_if
->vgic_vmcr
;
232 if (model
== KVM_DEV_TYPE_ARM_VGIC_V2
) {
233 vmcrp
->ackctl
= (vmcr
& ICH_VMCR_ACK_CTL_MASK
) >>
234 ICH_VMCR_ACK_CTL_SHIFT
;
235 vmcrp
->fiqen
= (vmcr
& ICH_VMCR_FIQ_EN_MASK
) >>
236 ICH_VMCR_FIQ_EN_SHIFT
;
239 * When emulating GICv3 on GICv3 with SRE=1 on the
240 * VFIQEn bit is RES1 and the VAckCtl bit is RES0.
246 vmcrp
->cbpr
= (vmcr
& ICH_VMCR_CBPR_MASK
) >> ICH_VMCR_CBPR_SHIFT
;
247 vmcrp
->eoim
= (vmcr
& ICH_VMCR_EOIM_MASK
) >> ICH_VMCR_EOIM_SHIFT
;
248 vmcrp
->abpr
= (vmcr
& ICH_VMCR_BPR1_MASK
) >> ICH_VMCR_BPR1_SHIFT
;
249 vmcrp
->bpr
= (vmcr
& ICH_VMCR_BPR0_MASK
) >> ICH_VMCR_BPR0_SHIFT
;
250 vmcrp
->pmr
= (vmcr
& ICH_VMCR_PMR_MASK
) >> ICH_VMCR_PMR_SHIFT
;
251 vmcrp
->grpen0
= (vmcr
& ICH_VMCR_ENG0_MASK
) >> ICH_VMCR_ENG0_SHIFT
;
252 vmcrp
->grpen1
= (vmcr
& ICH_VMCR_ENG1_MASK
) >> ICH_VMCR_ENG1_SHIFT
;
255 #define INITIAL_PENDBASER_VALUE \
256 (GIC_BASER_CACHEABILITY(GICR_PENDBASER, INNER, RaWb) | \
257 GIC_BASER_CACHEABILITY(GICR_PENDBASER, OUTER, SameAsInner) | \
258 GIC_BASER_SHAREABILITY(GICR_PENDBASER, InnerShareable))
260 void vgic_v3_enable(struct kvm_vcpu
*vcpu
)
262 struct vgic_v3_cpu_if
*vgic_v3
= &vcpu
->arch
.vgic_cpu
.vgic_v3
;
265 * By forcing VMCR to zero, the GIC will restore the binary
266 * points to their reset values. Anything else resets to zero
269 vgic_v3
->vgic_vmcr
= 0;
270 vgic_v3
->vgic_elrsr
= ~0;
273 * If we are emulating a GICv3, we do it in an non-GICv2-compatible
274 * way, so we force SRE to 1 to demonstrate this to the guest.
275 * Also, we don't support any form of IRQ/FIQ bypass.
276 * This goes with the spec allowing the value to be RAO/WI.
278 if (vcpu
->kvm
->arch
.vgic
.vgic_model
== KVM_DEV_TYPE_ARM_VGIC_V3
) {
279 vgic_v3
->vgic_sre
= (ICC_SRE_EL1_DIB
|
282 vcpu
->arch
.vgic_cpu
.pendbaser
= INITIAL_PENDBASER_VALUE
;
284 vgic_v3
->vgic_sre
= 0;
287 vcpu
->arch
.vgic_cpu
.num_id_bits
= (kvm_vgic_global_state
.ich_vtr_el2
&
288 ICH_VTR_ID_BITS_MASK
) >>
289 ICH_VTR_ID_BITS_SHIFT
;
290 vcpu
->arch
.vgic_cpu
.num_pri_bits
= ((kvm_vgic_global_state
.ich_vtr_el2
&
291 ICH_VTR_PRI_BITS_MASK
) >>
292 ICH_VTR_PRI_BITS_SHIFT
) + 1;
294 /* Get the show on the road... */
295 vgic_v3
->vgic_hcr
= ICH_HCR_EN
;
297 vgic_v3
->vgic_hcr
|= ICH_HCR_TALL0
;
299 vgic_v3
->vgic_hcr
|= ICH_HCR_TALL1
;
301 vgic_v3
->vgic_hcr
|= ICH_HCR_TC
;
304 int vgic_v3_lpi_sync_pending_status(struct kvm
*kvm
, struct vgic_irq
*irq
)
306 struct kvm_vcpu
*vcpu
;
307 int byte_offset
, bit_nr
;
315 vcpu
= irq
->target_vcpu
;
319 pendbase
= GICR_PENDBASER_ADDRESS(vcpu
->arch
.vgic_cpu
.pendbaser
);
321 byte_offset
= irq
->intid
/ BITS_PER_BYTE
;
322 bit_nr
= irq
->intid
% BITS_PER_BYTE
;
323 ptr
= pendbase
+ byte_offset
;
325 ret
= kvm_read_guest(kvm
, ptr
, &val
, 1);
329 status
= val
& (1 << bit_nr
);
331 spin_lock_irqsave(&irq
->irq_lock
, flags
);
332 if (irq
->target_vcpu
!= vcpu
) {
333 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
336 irq
->pending_latch
= status
;
337 vgic_queue_irq_unlock(vcpu
->kvm
, irq
, flags
);
340 /* clear consumed data */
341 val
&= ~(1 << bit_nr
);
342 ret
= kvm_write_guest(kvm
, ptr
, &val
, 1);
350 * vgic_its_save_pending_tables - Save the pending tables into guest RAM
351 * kvm lock and all vcpu lock must be held
353 int vgic_v3_save_pending_tables(struct kvm
*kvm
)
355 struct vgic_dist
*dist
= &kvm
->arch
.vgic
;
356 int last_byte_offset
= -1;
357 struct vgic_irq
*irq
;
361 list_for_each_entry(irq
, &dist
->lpi_list_head
, lpi_list
) {
362 int byte_offset
, bit_nr
;
363 struct kvm_vcpu
*vcpu
;
367 vcpu
= irq
->target_vcpu
;
371 pendbase
= GICR_PENDBASER_ADDRESS(vcpu
->arch
.vgic_cpu
.pendbaser
);
373 byte_offset
= irq
->intid
/ BITS_PER_BYTE
;
374 bit_nr
= irq
->intid
% BITS_PER_BYTE
;
375 ptr
= pendbase
+ byte_offset
;
377 if (byte_offset
!= last_byte_offset
) {
378 ret
= kvm_read_guest(kvm
, ptr
, &val
, 1);
381 last_byte_offset
= byte_offset
;
384 stored
= val
& (1U << bit_nr
);
385 if (stored
== irq
->pending_latch
)
388 if (irq
->pending_latch
)
391 val
&= ~(1 << bit_nr
);
393 ret
= kvm_write_guest(kvm
, ptr
, &val
, 1);
401 * Check for overlapping regions and for regions crossing the end of memory
402 * for base addresses which have already been set.
404 bool vgic_v3_check_base(struct kvm
*kvm
)
406 struct vgic_dist
*d
= &kvm
->arch
.vgic
;
407 gpa_t redist_size
= KVM_VGIC_V3_REDIST_SIZE
;
409 redist_size
*= atomic_read(&kvm
->online_vcpus
);
411 if (!IS_VGIC_ADDR_UNDEF(d
->vgic_dist_base
) &&
412 d
->vgic_dist_base
+ KVM_VGIC_V3_DIST_SIZE
< d
->vgic_dist_base
)
415 if (!IS_VGIC_ADDR_UNDEF(d
->vgic_redist_base
) &&
416 d
->vgic_redist_base
+ redist_size
< d
->vgic_redist_base
)
419 /* Both base addresses must be set to check if they overlap */
420 if (IS_VGIC_ADDR_UNDEF(d
->vgic_dist_base
) ||
421 IS_VGIC_ADDR_UNDEF(d
->vgic_redist_base
))
424 if (d
->vgic_dist_base
+ KVM_VGIC_V3_DIST_SIZE
<= d
->vgic_redist_base
)
426 if (d
->vgic_redist_base
+ redist_size
<= d
->vgic_dist_base
)
432 int vgic_v3_map_resources(struct kvm
*kvm
)
435 struct vgic_dist
*dist
= &kvm
->arch
.vgic
;
440 if (IS_VGIC_ADDR_UNDEF(dist
->vgic_dist_base
) ||
441 IS_VGIC_ADDR_UNDEF(dist
->vgic_redist_base
)) {
442 kvm_err("Need to set vgic distributor addresses first\n");
447 if (!vgic_v3_check_base(kvm
)) {
448 kvm_err("VGIC redist and dist frames overlap\n");
454 * For a VGICv3 we require the userland to explicitly initialize
455 * the VGIC before we need to use it.
457 if (!vgic_initialized(kvm
)) {
462 ret
= vgic_register_dist_iodev(kvm
, dist
->vgic_dist_base
, VGIC_V3
);
464 kvm_err("Unable to register VGICv3 dist MMIO regions\n");
474 DEFINE_STATIC_KEY_FALSE(vgic_v3_cpuif_trap
);
476 static int __init
early_group0_trap_cfg(char *buf
)
478 return strtobool(buf
, &group0_trap
);
480 early_param("kvm-arm.vgic_v3_group0_trap", early_group0_trap_cfg
);
482 static int __init
early_group1_trap_cfg(char *buf
)
484 return strtobool(buf
, &group1_trap
);
486 early_param("kvm-arm.vgic_v3_group1_trap", early_group1_trap_cfg
);
488 static int __init
early_common_trap_cfg(char *buf
)
490 return strtobool(buf
, &common_trap
);
492 early_param("kvm-arm.vgic_v3_common_trap", early_common_trap_cfg
);
494 static int __init
early_gicv4_enable(char *buf
)
496 return strtobool(buf
, &gicv4_enable
);
498 early_param("kvm-arm.vgic_v4_enable", early_gicv4_enable
);
501 * vgic_v3_probe - probe for a GICv3 compatible interrupt controller in DT
502 * @node: pointer to the DT node
504 * Returns 0 if a GICv3 has been found, returns an error code otherwise
506 int vgic_v3_probe(const struct gic_kvm_info
*info
)
508 u32 ich_vtr_el2
= kvm_call_hyp(__vgic_v3_get_ich_vtr_el2
);
512 * The ListRegs field is 5 bits, but there is a architectural
513 * maximum of 16 list registers. Just ignore bit 4...
515 kvm_vgic_global_state
.nr_lr
= (ich_vtr_el2
& 0xf) + 1;
516 kvm_vgic_global_state
.can_emulate_gicv2
= false;
517 kvm_vgic_global_state
.ich_vtr_el2
= ich_vtr_el2
;
521 kvm_vgic_global_state
.has_gicv4
= gicv4_enable
;
522 kvm_info("GICv4 support %sabled\n",
523 gicv4_enable
? "en" : "dis");
526 if (!info
->vcpu
.start
) {
527 kvm_info("GICv3: no GICV resource entry\n");
528 kvm_vgic_global_state
.vcpu_base
= 0;
529 } else if (!PAGE_ALIGNED(info
->vcpu
.start
)) {
530 pr_warn("GICV physical address 0x%llx not page aligned\n",
531 (unsigned long long)info
->vcpu
.start
);
532 kvm_vgic_global_state
.vcpu_base
= 0;
533 } else if (!PAGE_ALIGNED(resource_size(&info
->vcpu
))) {
534 pr_warn("GICV size 0x%llx not a multiple of page size 0x%lx\n",
535 (unsigned long long)resource_size(&info
->vcpu
),
537 kvm_vgic_global_state
.vcpu_base
= 0;
539 kvm_vgic_global_state
.vcpu_base
= info
->vcpu
.start
;
540 kvm_vgic_global_state
.can_emulate_gicv2
= true;
541 ret
= kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V2
);
543 kvm_err("Cannot register GICv2 KVM device.\n");
546 kvm_info("vgic-v2@%llx\n", info
->vcpu
.start
);
548 ret
= kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V3
);
550 kvm_err("Cannot register GICv3 KVM device.\n");
551 kvm_unregister_device_ops(KVM_DEV_TYPE_ARM_VGIC_V2
);
555 if (kvm_vgic_global_state
.vcpu_base
== 0)
556 kvm_info("disabling GICv2 emulation\n");
559 if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_30115
)) {
565 if (group0_trap
|| group1_trap
|| common_trap
) {
566 kvm_info("GICv3 sysreg trapping enabled ([%s%s%s], reduced performance)\n",
567 group0_trap
? "G0" : "",
568 group1_trap
? "G1" : "",
569 common_trap
? "C" : "");
570 static_branch_enable(&vgic_v3_cpuif_trap
);
573 kvm_vgic_global_state
.vctrl_base
= NULL
;
574 kvm_vgic_global_state
.type
= VGIC_V3
;
575 kvm_vgic_global_state
.max_gic_vcpus
= VGIC_V3_MAX_CPUS
;
580 void vgic_v3_load(struct kvm_vcpu
*vcpu
)
582 struct vgic_v3_cpu_if
*cpu_if
= &vcpu
->arch
.vgic_cpu
.vgic_v3
;
585 * If dealing with a GICv2 emulation on GICv3, VMCR_EL2.VFIQen
586 * is dependent on ICC_SRE_EL1.SRE, and we have to perform the
587 * VMCR_EL2 save/restore in the world switch.
589 if (likely(cpu_if
->vgic_sre
))
590 kvm_call_hyp(__vgic_v3_write_vmcr
, cpu_if
->vgic_vmcr
);
593 void vgic_v3_put(struct kvm_vcpu
*vcpu
)
595 struct vgic_v3_cpu_if
*cpu_if
= &vcpu
->arch
.vgic_cpu
.vgic_v3
;
597 if (likely(cpu_if
->vgic_sre
))
598 cpu_if
->vgic_vmcr
= kvm_call_hyp(__vgic_v3_read_vmcr
);