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 void vgic_v3_process_maintenance(struct kvm_vcpu *vcpu)
26 struct vgic_v3_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v3;
27 u32 model = vcpu->kvm->arch.vgic.vgic_model;
29 if (cpuif->vgic_misr & ICH_MISR_EOI) {
30 unsigned long eisr_bmap = cpuif->vgic_eisr;
33 for_each_set_bit(lr, &eisr_bmap, kvm_vgic_global_state.nr_lr) {
35 u64 val = cpuif->vgic_lr[lr];
37 if (model == KVM_DEV_TYPE_ARM_VGIC_V3)
38 intid = val & ICH_LR_VIRTUAL_ID_MASK;
40 intid = val & GICH_LR_VIRTUALID;
42 WARN_ON(cpuif->vgic_lr[lr] & ICH_LR_STATE);
44 kvm_notify_acked_irq(vcpu->kvm, 0,
45 intid - VGIC_NR_PRIVATE_IRQS);
49 * In the next iterations of the vcpu loop, if we sync
50 * the vgic state after flushing it, but before
51 * entering the guest (this happens for pending
52 * signals and vmid rollovers), then make sure we
53 * don't pick up any old maintenance interrupts here.
58 cpuif->vgic_hcr &= ~ICH_HCR_UIE;
61 void vgic_v3_set_underflow(struct kvm_vcpu *vcpu)
63 struct vgic_v3_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v3;
65 cpuif->vgic_hcr |= ICH_HCR_UIE;
68 void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu)
70 struct vgic_v3_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v3;
71 u32 model = vcpu->kvm->arch.vgic.vgic_model;
74 for (lr = 0; lr < vcpu->arch.vgic_cpu.used_lrs; lr++) {
75 u64 val = cpuif->vgic_lr[lr];
79 if (model == KVM_DEV_TYPE_ARM_VGIC_V3)
80 intid = val & ICH_LR_VIRTUAL_ID_MASK;
82 intid = val & GICH_LR_VIRTUALID;
83 irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
85 spin_lock(&irq->irq_lock);
87 /* Always preserve the active bit */
88 irq->active = !!(val & ICH_LR_ACTIVE_BIT);
90 /* Edge is the only case where we preserve the pending bit */
91 if (irq->config == VGIC_CONFIG_EDGE &&
92 (val & ICH_LR_PENDING_BIT)) {
95 if (vgic_irq_is_sgi(intid) &&
96 model == KVM_DEV_TYPE_ARM_VGIC_V2) {
97 u32 cpuid = val & GICH_LR_PHYSID_CPUID;
99 cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT;
100 irq->source |= (1 << cpuid);
105 * Clear soft pending state when level irqs have been acked.
106 * Always regenerate the pending state.
108 if (irq->config == VGIC_CONFIG_LEVEL) {
109 if (!(val & ICH_LR_PENDING_BIT))
110 irq->soft_pending = false;
112 irq->pending = irq->line_level || irq->soft_pending;
115 spin_unlock(&irq->irq_lock);
119 /* Requires the irq to be locked already */
120 void vgic_v3_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr)
122 u32 model = vcpu->kvm->arch.vgic.vgic_model;
123 u64 val = irq->intid;
126 val |= ICH_LR_PENDING_BIT;
128 if (irq->config == VGIC_CONFIG_EDGE)
129 irq->pending = false;
131 if (vgic_irq_is_sgi(irq->intid) &&
132 model == KVM_DEV_TYPE_ARM_VGIC_V2) {
133 u32 src = ffs(irq->source);
136 val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT;
137 irq->source &= ~(1 << (src - 1));
144 val |= ICH_LR_ACTIVE_BIT;
148 val |= ((u64)irq->hwintid) << ICH_LR_PHYS_ID_SHIFT;
150 if (irq->config == VGIC_CONFIG_LEVEL)
155 * We currently only support Group1 interrupts, which is a
156 * known defect. This needs to be addressed at some point.
158 if (model == KVM_DEV_TYPE_ARM_VGIC_V3)
161 val |= (u64)irq->priority << ICH_LR_PRIORITY_SHIFT;
163 vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] = val;
166 void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr)
168 vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] = 0;
171 void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
175 vmcr = (vmcrp->ctlr << ICH_VMCR_CTLR_SHIFT) & ICH_VMCR_CTLR_MASK;
176 vmcr |= (vmcrp->abpr << ICH_VMCR_BPR1_SHIFT) & ICH_VMCR_BPR1_MASK;
177 vmcr |= (vmcrp->bpr << ICH_VMCR_BPR0_SHIFT) & ICH_VMCR_BPR0_MASK;
178 vmcr |= (vmcrp->pmr << ICH_VMCR_PMR_SHIFT) & ICH_VMCR_PMR_MASK;
180 vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr = vmcr;
183 void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
185 u32 vmcr = vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr;
187 vmcrp->ctlr = (vmcr & ICH_VMCR_CTLR_MASK) >> ICH_VMCR_CTLR_SHIFT;
188 vmcrp->abpr = (vmcr & ICH_VMCR_BPR1_MASK) >> ICH_VMCR_BPR1_SHIFT;
189 vmcrp->bpr = (vmcr & ICH_VMCR_BPR0_MASK) >> ICH_VMCR_BPR0_SHIFT;
190 vmcrp->pmr = (vmcr & ICH_VMCR_PMR_MASK) >> ICH_VMCR_PMR_SHIFT;
193 void vgic_v3_enable(struct kvm_vcpu *vcpu)
195 struct vgic_v3_cpu_if *vgic_v3 = &vcpu->arch.vgic_cpu.vgic_v3;
198 * By forcing VMCR to zero, the GIC will restore the binary
199 * points to their reset values. Anything else resets to zero
202 vgic_v3->vgic_vmcr = 0;
203 vgic_v3->vgic_elrsr = ~0;
206 * If we are emulating a GICv3, we do it in an non-GICv2-compatible
207 * way, so we force SRE to 1 to demonstrate this to the guest.
208 * This goes with the spec allowing the value to be RAO/WI.
210 if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3)
211 vgic_v3->vgic_sre = ICC_SRE_EL1_SRE;
213 vgic_v3->vgic_sre = 0;
215 /* Get the show on the road... */
216 vgic_v3->vgic_hcr = ICH_HCR_EN;
219 /* check for overlapping regions and for regions crossing the end of memory */
220 static bool vgic_v3_check_base(struct kvm *kvm)
222 struct vgic_dist *d = &kvm->arch.vgic;
223 gpa_t redist_size = KVM_VGIC_V3_REDIST_SIZE;
225 redist_size *= atomic_read(&kvm->online_vcpus);
227 if (d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE < d->vgic_dist_base)
229 if (d->vgic_redist_base + redist_size < d->vgic_redist_base)
232 if (d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE <= d->vgic_redist_base)
234 if (d->vgic_redist_base + redist_size <= d->vgic_dist_base)
240 int vgic_v3_map_resources(struct kvm *kvm)
243 struct vgic_dist *dist = &kvm->arch.vgic;
248 if (IS_VGIC_ADDR_UNDEF(dist->vgic_dist_base) ||
249 IS_VGIC_ADDR_UNDEF(dist->vgic_redist_base)) {
250 kvm_err("Need to set vgic distributor addresses first\n");
255 if (!vgic_v3_check_base(kvm)) {
256 kvm_err("VGIC redist and dist frames overlap\n");
262 * For a VGICv3 we require the userland to explicitly initialize
263 * the VGIC before we need to use it.
265 if (!vgic_initialized(kvm)) {
270 ret = vgic_register_dist_iodev(kvm, dist->vgic_dist_base, VGIC_V3);
272 kvm_err("Unable to register VGICv3 dist MMIO regions\n");
276 ret = vgic_register_redist_iodevs(kvm, dist->vgic_redist_base);
278 kvm_err("Unable to register VGICv3 redist MMIO regions\n");
286 kvm_vgic_destroy(kvm);
291 * vgic_v3_probe - probe for a GICv3 compatible interrupt controller in DT
292 * @node: pointer to the DT node
294 * Returns 0 if a GICv3 has been found, returns an error code otherwise
296 int vgic_v3_probe(const struct gic_kvm_info *info)
298 u32 ich_vtr_el2 = kvm_call_hyp(__vgic_v3_get_ich_vtr_el2);
301 * The ListRegs field is 5 bits, but there is a architectural
302 * maximum of 16 list registers. Just ignore bit 4...
304 kvm_vgic_global_state.nr_lr = (ich_vtr_el2 & 0xf) + 1;
305 kvm_vgic_global_state.can_emulate_gicv2 = false;
307 if (!info->vcpu.start) {
308 kvm_info("GICv3: no GICV resource entry\n");
309 kvm_vgic_global_state.vcpu_base = 0;
310 } else if (!PAGE_ALIGNED(info->vcpu.start)) {
311 pr_warn("GICV physical address 0x%llx not page aligned\n",
312 (unsigned long long)info->vcpu.start);
313 kvm_vgic_global_state.vcpu_base = 0;
314 } else if (!PAGE_ALIGNED(resource_size(&info->vcpu))) {
315 pr_warn("GICV size 0x%llx not a multiple of page size 0x%lx\n",
316 (unsigned long long)resource_size(&info->vcpu),
318 kvm_vgic_global_state.vcpu_base = 0;
320 kvm_vgic_global_state.vcpu_base = info->vcpu.start;
321 kvm_vgic_global_state.can_emulate_gicv2 = true;
322 kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V2);
323 kvm_info("vgic-v2@%llx\n", info->vcpu.start);
325 if (kvm_vgic_global_state.vcpu_base == 0)
326 kvm_info("disabling GICv2 emulation\n");
327 kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V3);
329 kvm_vgic_global_state.vctrl_base = NULL;
330 kvm_vgic_global_state.type = VGIC_V3;
331 kvm_vgic_global_state.max_gic_vcpus = VGIC_V3_MAX_CPUS;