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>
21 void vgic_v3_process_maintenance(struct kvm_vcpu *vcpu)
23 struct vgic_v3_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v3;
24 u32 model = vcpu->kvm->arch.vgic.vgic_model;
26 if (cpuif->vgic_misr & ICH_MISR_EOI) {
27 unsigned long eisr_bmap = cpuif->vgic_eisr;
30 for_each_set_bit(lr, &eisr_bmap, kvm_vgic_global_state.nr_lr) {
32 u64 val = cpuif->vgic_lr[lr];
34 if (model == KVM_DEV_TYPE_ARM_VGIC_V3)
35 intid = val & ICH_LR_VIRTUAL_ID_MASK;
37 intid = val & GICH_LR_VIRTUALID;
39 WARN_ON(cpuif->vgic_lr[lr] & ICH_LR_STATE);
41 kvm_notify_acked_irq(vcpu->kvm, 0,
42 intid - VGIC_NR_PRIVATE_IRQS);
46 * In the next iterations of the vcpu loop, if we sync
47 * the vgic state after flushing it, but before
48 * entering the guest (this happens for pending
49 * signals and vmid rollovers), then make sure we
50 * don't pick up any old maintenance interrupts here.
55 cpuif->vgic_hcr &= ~ICH_HCR_UIE;
58 void vgic_v3_set_underflow(struct kvm_vcpu *vcpu)
60 struct vgic_v3_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v3;
62 cpuif->vgic_hcr |= ICH_HCR_UIE;
65 void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu)
67 struct vgic_v3_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v3;
68 u32 model = vcpu->kvm->arch.vgic.vgic_model;
71 for (lr = 0; lr < vcpu->arch.vgic_cpu.used_lrs; lr++) {
72 u64 val = cpuif->vgic_lr[lr];
76 if (model == KVM_DEV_TYPE_ARM_VGIC_V3)
77 intid = val & ICH_LR_VIRTUAL_ID_MASK;
79 intid = val & GICH_LR_VIRTUALID;
80 irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
82 spin_lock(&irq->irq_lock);
84 /* Always preserve the active bit */
85 irq->active = !!(val & ICH_LR_ACTIVE_BIT);
87 /* Edge is the only case where we preserve the pending bit */
88 if (irq->config == VGIC_CONFIG_EDGE &&
89 (val & ICH_LR_PENDING_BIT)) {
92 if (vgic_irq_is_sgi(intid) &&
93 model == KVM_DEV_TYPE_ARM_VGIC_V2) {
94 u32 cpuid = val & GICH_LR_PHYSID_CPUID;
96 cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT;
97 irq->source |= (1 << cpuid);
101 /* Clear soft pending state when level irqs have been acked */
102 if (irq->config == VGIC_CONFIG_LEVEL &&
103 !(val & ICH_LR_PENDING_BIT)) {
104 irq->soft_pending = false;
105 irq->pending = irq->line_level;
108 spin_unlock(&irq->irq_lock);
112 /* Requires the irq to be locked already */
113 void vgic_v3_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr)
115 u32 model = vcpu->kvm->arch.vgic.vgic_model;
116 u64 val = irq->intid;
119 val |= ICH_LR_PENDING_BIT;
121 if (irq->config == VGIC_CONFIG_EDGE)
122 irq->pending = false;
124 if (vgic_irq_is_sgi(irq->intid) &&
125 model == KVM_DEV_TYPE_ARM_VGIC_V2) {
126 u32 src = ffs(irq->source);
129 val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT;
130 irq->source &= ~(1 << (src - 1));
137 val |= ICH_LR_ACTIVE_BIT;
141 val |= ((u64)irq->hwintid) << ICH_LR_PHYS_ID_SHIFT;
143 if (irq->config == VGIC_CONFIG_LEVEL)
148 * We currently only support Group1 interrupts, which is a
149 * known defect. This needs to be addressed at some point.
151 if (model == KVM_DEV_TYPE_ARM_VGIC_V3)
154 val |= (u64)irq->priority << ICH_LR_PRIORITY_SHIFT;
156 vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] = val;
159 void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr)
161 vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] = 0;