18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * VGICv3 MMIO handling functions 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/bitfield.h> 78c2ecf20Sopenharmony_ci#include <linux/irqchip/arm-gic-v3.h> 88c2ecf20Sopenharmony_ci#include <linux/kvm.h> 98c2ecf20Sopenharmony_ci#include <linux/kvm_host.h> 108c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 118c2ecf20Sopenharmony_ci#include <kvm/iodev.h> 128c2ecf20Sopenharmony_ci#include <kvm/arm_vgic.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <asm/kvm_emulate.h> 158c2ecf20Sopenharmony_ci#include <asm/kvm_arm.h> 168c2ecf20Sopenharmony_ci#include <asm/kvm_mmu.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include "vgic.h" 198c2ecf20Sopenharmony_ci#include "vgic-mmio.h" 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci/* extract @num bytes at @offset bytes offset in data */ 228c2ecf20Sopenharmony_ciunsigned long extract_bytes(u64 data, unsigned int offset, 238c2ecf20Sopenharmony_ci unsigned int num) 248c2ecf20Sopenharmony_ci{ 258c2ecf20Sopenharmony_ci return (data >> (offset * 8)) & GENMASK_ULL(num * 8 - 1, 0); 268c2ecf20Sopenharmony_ci} 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci/* allows updates of any half of a 64-bit register (or the whole thing) */ 298c2ecf20Sopenharmony_ciu64 update_64bit_reg(u64 reg, unsigned int offset, unsigned int len, 308c2ecf20Sopenharmony_ci unsigned long val) 318c2ecf20Sopenharmony_ci{ 328c2ecf20Sopenharmony_ci int lower = (offset & 4) * 8; 338c2ecf20Sopenharmony_ci int upper = lower + 8 * len - 1; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci reg &= ~GENMASK_ULL(upper, lower); 368c2ecf20Sopenharmony_ci val &= GENMASK_ULL(len * 8 - 1, 0); 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci return reg | ((u64)val << lower); 398c2ecf20Sopenharmony_ci} 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cibool vgic_has_its(struct kvm *kvm) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci struct vgic_dist *dist = &kvm->arch.vgic; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci if (dist->vgic_model != KVM_DEV_TYPE_ARM_VGIC_V3) 468c2ecf20Sopenharmony_ci return false; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci return dist->has_its; 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cibool vgic_supports_direct_msis(struct kvm *kvm) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci return (kvm_vgic_global_state.has_gicv4_1 || 548c2ecf20Sopenharmony_ci (kvm_vgic_global_state.has_gicv4 && vgic_has_its(kvm))); 558c2ecf20Sopenharmony_ci} 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci/* 588c2ecf20Sopenharmony_ci * The Revision field in the IIDR have the following meanings: 598c2ecf20Sopenharmony_ci * 608c2ecf20Sopenharmony_ci * Revision 2: Interrupt groups are guest-configurable and signaled using 618c2ecf20Sopenharmony_ci * their configured groups. 628c2ecf20Sopenharmony_ci */ 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistatic unsigned long vgic_mmio_read_v3_misc(struct kvm_vcpu *vcpu, 658c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci struct vgic_dist *vgic = &vcpu->kvm->arch.vgic; 688c2ecf20Sopenharmony_ci u32 value = 0; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci switch (addr & 0x0c) { 718c2ecf20Sopenharmony_ci case GICD_CTLR: 728c2ecf20Sopenharmony_ci if (vgic->enabled) 738c2ecf20Sopenharmony_ci value |= GICD_CTLR_ENABLE_SS_G1; 748c2ecf20Sopenharmony_ci value |= GICD_CTLR_ARE_NS | GICD_CTLR_DS; 758c2ecf20Sopenharmony_ci if (vgic->nassgireq) 768c2ecf20Sopenharmony_ci value |= GICD_CTLR_nASSGIreq; 778c2ecf20Sopenharmony_ci break; 788c2ecf20Sopenharmony_ci case GICD_TYPER: 798c2ecf20Sopenharmony_ci value = vgic->nr_spis + VGIC_NR_PRIVATE_IRQS; 808c2ecf20Sopenharmony_ci value = (value >> 5) - 1; 818c2ecf20Sopenharmony_ci if (vgic_has_its(vcpu->kvm)) { 828c2ecf20Sopenharmony_ci value |= (INTERRUPT_ID_BITS_ITS - 1) << 19; 838c2ecf20Sopenharmony_ci value |= GICD_TYPER_LPIS; 848c2ecf20Sopenharmony_ci } else { 858c2ecf20Sopenharmony_ci value |= (INTERRUPT_ID_BITS_SPIS - 1) << 19; 868c2ecf20Sopenharmony_ci } 878c2ecf20Sopenharmony_ci break; 888c2ecf20Sopenharmony_ci case GICD_TYPER2: 898c2ecf20Sopenharmony_ci if (kvm_vgic_global_state.has_gicv4_1) 908c2ecf20Sopenharmony_ci value = GICD_TYPER2_nASSGIcap; 918c2ecf20Sopenharmony_ci break; 928c2ecf20Sopenharmony_ci case GICD_IIDR: 938c2ecf20Sopenharmony_ci value = (PRODUCT_ID_KVM << GICD_IIDR_PRODUCT_ID_SHIFT) | 948c2ecf20Sopenharmony_ci (vgic->implementation_rev << GICD_IIDR_REVISION_SHIFT) | 958c2ecf20Sopenharmony_ci (IMPLEMENTER_ARM << GICD_IIDR_IMPLEMENTER_SHIFT); 968c2ecf20Sopenharmony_ci break; 978c2ecf20Sopenharmony_ci default: 988c2ecf20Sopenharmony_ci return 0; 998c2ecf20Sopenharmony_ci } 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci return value; 1028c2ecf20Sopenharmony_ci} 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistatic void vgic_mmio_write_v3_misc(struct kvm_vcpu *vcpu, 1058c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len, 1068c2ecf20Sopenharmony_ci unsigned long val) 1078c2ecf20Sopenharmony_ci{ 1088c2ecf20Sopenharmony_ci struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci switch (addr & 0x0c) { 1118c2ecf20Sopenharmony_ci case GICD_CTLR: { 1128c2ecf20Sopenharmony_ci bool was_enabled, is_hwsgi; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci mutex_lock(&vcpu->kvm->lock); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci was_enabled = dist->enabled; 1178c2ecf20Sopenharmony_ci is_hwsgi = dist->nassgireq; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci dist->enabled = val & GICD_CTLR_ENABLE_SS_G1; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci /* Not a GICv4.1? No HW SGIs */ 1228c2ecf20Sopenharmony_ci if (!kvm_vgic_global_state.has_gicv4_1) 1238c2ecf20Sopenharmony_ci val &= ~GICD_CTLR_nASSGIreq; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci /* Dist stays enabled? nASSGIreq is RO */ 1268c2ecf20Sopenharmony_ci if (was_enabled && dist->enabled) { 1278c2ecf20Sopenharmony_ci val &= ~GICD_CTLR_nASSGIreq; 1288c2ecf20Sopenharmony_ci val |= FIELD_PREP(GICD_CTLR_nASSGIreq, is_hwsgi); 1298c2ecf20Sopenharmony_ci } 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci /* Switching HW SGIs? */ 1328c2ecf20Sopenharmony_ci dist->nassgireq = val & GICD_CTLR_nASSGIreq; 1338c2ecf20Sopenharmony_ci if (is_hwsgi != dist->nassgireq) 1348c2ecf20Sopenharmony_ci vgic_v4_configure_vsgis(vcpu->kvm); 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci if (kvm_vgic_global_state.has_gicv4_1 && 1378c2ecf20Sopenharmony_ci was_enabled != dist->enabled) 1388c2ecf20Sopenharmony_ci kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_RELOAD_GICv4); 1398c2ecf20Sopenharmony_ci else if (!was_enabled && dist->enabled) 1408c2ecf20Sopenharmony_ci vgic_kick_vcpus(vcpu->kvm); 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci mutex_unlock(&vcpu->kvm->lock); 1438c2ecf20Sopenharmony_ci break; 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci case GICD_TYPER: 1468c2ecf20Sopenharmony_ci case GICD_TYPER2: 1478c2ecf20Sopenharmony_ci case GICD_IIDR: 1488c2ecf20Sopenharmony_ci /* This is at best for documentation purposes... */ 1498c2ecf20Sopenharmony_ci return; 1508c2ecf20Sopenharmony_ci } 1518c2ecf20Sopenharmony_ci} 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cistatic int vgic_mmio_uaccess_write_v3_misc(struct kvm_vcpu *vcpu, 1548c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len, 1558c2ecf20Sopenharmony_ci unsigned long val) 1568c2ecf20Sopenharmony_ci{ 1578c2ecf20Sopenharmony_ci struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci switch (addr & 0x0c) { 1608c2ecf20Sopenharmony_ci case GICD_TYPER2: 1618c2ecf20Sopenharmony_ci case GICD_IIDR: 1628c2ecf20Sopenharmony_ci if (val != vgic_mmio_read_v3_misc(vcpu, addr, len)) 1638c2ecf20Sopenharmony_ci return -EINVAL; 1648c2ecf20Sopenharmony_ci return 0; 1658c2ecf20Sopenharmony_ci case GICD_CTLR: 1668c2ecf20Sopenharmony_ci /* Not a GICv4.1? No HW SGIs */ 1678c2ecf20Sopenharmony_ci if (!kvm_vgic_global_state.has_gicv4_1) 1688c2ecf20Sopenharmony_ci val &= ~GICD_CTLR_nASSGIreq; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci dist->enabled = val & GICD_CTLR_ENABLE_SS_G1; 1718c2ecf20Sopenharmony_ci dist->nassgireq = val & GICD_CTLR_nASSGIreq; 1728c2ecf20Sopenharmony_ci return 0; 1738c2ecf20Sopenharmony_ci } 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci vgic_mmio_write_v3_misc(vcpu, addr, len, val); 1768c2ecf20Sopenharmony_ci return 0; 1778c2ecf20Sopenharmony_ci} 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_cistatic unsigned long vgic_mmio_read_irouter(struct kvm_vcpu *vcpu, 1808c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len) 1818c2ecf20Sopenharmony_ci{ 1828c2ecf20Sopenharmony_ci int intid = VGIC_ADDR_TO_INTID(addr, 64); 1838c2ecf20Sopenharmony_ci struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, NULL, intid); 1848c2ecf20Sopenharmony_ci unsigned long ret = 0; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci if (!irq) 1878c2ecf20Sopenharmony_ci return 0; 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci /* The upper word is RAZ for us. */ 1908c2ecf20Sopenharmony_ci if (!(addr & 4)) 1918c2ecf20Sopenharmony_ci ret = extract_bytes(READ_ONCE(irq->mpidr), addr & 7, len); 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci vgic_put_irq(vcpu->kvm, irq); 1948c2ecf20Sopenharmony_ci return ret; 1958c2ecf20Sopenharmony_ci} 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_cistatic void vgic_mmio_write_irouter(struct kvm_vcpu *vcpu, 1988c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len, 1998c2ecf20Sopenharmony_ci unsigned long val) 2008c2ecf20Sopenharmony_ci{ 2018c2ecf20Sopenharmony_ci int intid = VGIC_ADDR_TO_INTID(addr, 64); 2028c2ecf20Sopenharmony_ci struct vgic_irq *irq; 2038c2ecf20Sopenharmony_ci unsigned long flags; 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci /* The upper word is WI for us since we don't implement Aff3. */ 2068c2ecf20Sopenharmony_ci if (addr & 4) 2078c2ecf20Sopenharmony_ci return; 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci irq = vgic_get_irq(vcpu->kvm, NULL, intid); 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci if (!irq) 2128c2ecf20Sopenharmony_ci return; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci raw_spin_lock_irqsave(&irq->irq_lock, flags); 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci /* We only care about and preserve Aff0, Aff1 and Aff2. */ 2178c2ecf20Sopenharmony_ci irq->mpidr = val & GENMASK(23, 0); 2188c2ecf20Sopenharmony_ci irq->target_vcpu = kvm_mpidr_to_vcpu(vcpu->kvm, irq->mpidr); 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&irq->irq_lock, flags); 2218c2ecf20Sopenharmony_ci vgic_put_irq(vcpu->kvm, irq); 2228c2ecf20Sopenharmony_ci} 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_cistatic unsigned long vgic_mmio_read_v3r_ctlr(struct kvm_vcpu *vcpu, 2258c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len) 2268c2ecf20Sopenharmony_ci{ 2278c2ecf20Sopenharmony_ci struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci return vgic_cpu->lpis_enabled ? GICR_CTLR_ENABLE_LPIS : 0; 2308c2ecf20Sopenharmony_ci} 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_cistatic void vgic_mmio_write_v3r_ctlr(struct kvm_vcpu *vcpu, 2348c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len, 2358c2ecf20Sopenharmony_ci unsigned long val) 2368c2ecf20Sopenharmony_ci{ 2378c2ecf20Sopenharmony_ci struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 2388c2ecf20Sopenharmony_ci bool was_enabled = vgic_cpu->lpis_enabled; 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci if (!vgic_has_its(vcpu->kvm)) 2418c2ecf20Sopenharmony_ci return; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci vgic_cpu->lpis_enabled = val & GICR_CTLR_ENABLE_LPIS; 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci if (was_enabled && !vgic_cpu->lpis_enabled) { 2468c2ecf20Sopenharmony_ci vgic_flush_pending_lpis(vcpu); 2478c2ecf20Sopenharmony_ci vgic_its_invalidate_cache(vcpu->kvm); 2488c2ecf20Sopenharmony_ci } 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci if (!was_enabled && vgic_cpu->lpis_enabled) 2518c2ecf20Sopenharmony_ci vgic_enable_lpis(vcpu); 2528c2ecf20Sopenharmony_ci} 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_cistatic unsigned long vgic_mmio_read_v3r_typer(struct kvm_vcpu *vcpu, 2558c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len) 2568c2ecf20Sopenharmony_ci{ 2578c2ecf20Sopenharmony_ci unsigned long mpidr = kvm_vcpu_get_mpidr_aff(vcpu); 2588c2ecf20Sopenharmony_ci struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 2598c2ecf20Sopenharmony_ci struct vgic_redist_region *rdreg = vgic_cpu->rdreg; 2608c2ecf20Sopenharmony_ci int target_vcpu_id = vcpu->vcpu_id; 2618c2ecf20Sopenharmony_ci gpa_t last_rdist_typer = rdreg->base + GICR_TYPER + 2628c2ecf20Sopenharmony_ci (rdreg->free_index - 1) * KVM_VGIC_V3_REDIST_SIZE; 2638c2ecf20Sopenharmony_ci u64 value; 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci value = (u64)(mpidr & GENMASK(23, 0)) << 32; 2668c2ecf20Sopenharmony_ci value |= ((target_vcpu_id & 0xffff) << 8); 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci if (addr == last_rdist_typer) 2698c2ecf20Sopenharmony_ci value |= GICR_TYPER_LAST; 2708c2ecf20Sopenharmony_ci if (vgic_has_its(vcpu->kvm)) 2718c2ecf20Sopenharmony_ci value |= GICR_TYPER_PLPIS; 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci return extract_bytes(value, addr & 7, len); 2748c2ecf20Sopenharmony_ci} 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_cistatic unsigned long vgic_uaccess_read_v3r_typer(struct kvm_vcpu *vcpu, 2778c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len) 2788c2ecf20Sopenharmony_ci{ 2798c2ecf20Sopenharmony_ci unsigned long mpidr = kvm_vcpu_get_mpidr_aff(vcpu); 2808c2ecf20Sopenharmony_ci int target_vcpu_id = vcpu->vcpu_id; 2818c2ecf20Sopenharmony_ci u64 value; 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci value = (u64)(mpidr & GENMASK(23, 0)) << 32; 2848c2ecf20Sopenharmony_ci value |= ((target_vcpu_id & 0xffff) << 8); 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci if (vgic_has_its(vcpu->kvm)) 2878c2ecf20Sopenharmony_ci value |= GICR_TYPER_PLPIS; 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci /* reporting of the Last bit is not supported for userspace */ 2908c2ecf20Sopenharmony_ci return extract_bytes(value, addr & 7, len); 2918c2ecf20Sopenharmony_ci} 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_cistatic unsigned long vgic_mmio_read_v3r_iidr(struct kvm_vcpu *vcpu, 2948c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len) 2958c2ecf20Sopenharmony_ci{ 2968c2ecf20Sopenharmony_ci return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0); 2978c2ecf20Sopenharmony_ci} 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_cistatic unsigned long vgic_mmio_read_v3_idregs(struct kvm_vcpu *vcpu, 3008c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len) 3018c2ecf20Sopenharmony_ci{ 3028c2ecf20Sopenharmony_ci switch (addr & 0xffff) { 3038c2ecf20Sopenharmony_ci case GICD_PIDR2: 3048c2ecf20Sopenharmony_ci /* report a GICv3 compliant implementation */ 3058c2ecf20Sopenharmony_ci return 0x3b; 3068c2ecf20Sopenharmony_ci } 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci return 0; 3098c2ecf20Sopenharmony_ci} 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_cistatic unsigned long vgic_v3_uaccess_read_pending(struct kvm_vcpu *vcpu, 3128c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len) 3138c2ecf20Sopenharmony_ci{ 3148c2ecf20Sopenharmony_ci u32 intid = VGIC_ADDR_TO_INTID(addr, 1); 3158c2ecf20Sopenharmony_ci u32 value = 0; 3168c2ecf20Sopenharmony_ci int i; 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_ci /* 3198c2ecf20Sopenharmony_ci * pending state of interrupt is latched in pending_latch variable. 3208c2ecf20Sopenharmony_ci * Userspace will save and restore pending state and line_level 3218c2ecf20Sopenharmony_ci * separately. 3228c2ecf20Sopenharmony_ci * Refer to Documentation/virt/kvm/devices/arm-vgic-v3.rst 3238c2ecf20Sopenharmony_ci * for handling of ISPENDR and ICPENDR. 3248c2ecf20Sopenharmony_ci */ 3258c2ecf20Sopenharmony_ci for (i = 0; i < len * 8; i++) { 3268c2ecf20Sopenharmony_ci struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); 3278c2ecf20Sopenharmony_ci bool state = irq->pending_latch; 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_ci if (irq->hw && vgic_irq_is_sgi(irq->intid)) { 3308c2ecf20Sopenharmony_ci int err; 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci err = irq_get_irqchip_state(irq->host_irq, 3338c2ecf20Sopenharmony_ci IRQCHIP_STATE_PENDING, 3348c2ecf20Sopenharmony_ci &state); 3358c2ecf20Sopenharmony_ci WARN_ON(err); 3368c2ecf20Sopenharmony_ci } 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci if (state) 3398c2ecf20Sopenharmony_ci value |= (1U << i); 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci vgic_put_irq(vcpu->kvm, irq); 3428c2ecf20Sopenharmony_ci } 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ci return value; 3458c2ecf20Sopenharmony_ci} 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_cistatic int vgic_v3_uaccess_write_pending(struct kvm_vcpu *vcpu, 3488c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len, 3498c2ecf20Sopenharmony_ci unsigned long val) 3508c2ecf20Sopenharmony_ci{ 3518c2ecf20Sopenharmony_ci u32 intid = VGIC_ADDR_TO_INTID(addr, 1); 3528c2ecf20Sopenharmony_ci int i; 3538c2ecf20Sopenharmony_ci unsigned long flags; 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci for (i = 0; i < len * 8; i++) { 3568c2ecf20Sopenharmony_ci struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci raw_spin_lock_irqsave(&irq->irq_lock, flags); 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci /* 3618c2ecf20Sopenharmony_ci * pending_latch is set irrespective of irq type 3628c2ecf20Sopenharmony_ci * (level or edge) to avoid dependency that VM should 3638c2ecf20Sopenharmony_ci * restore irq config before pending info. 3648c2ecf20Sopenharmony_ci */ 3658c2ecf20Sopenharmony_ci irq->pending_latch = test_bit(i, &val); 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci if (irq->hw && vgic_irq_is_sgi(irq->intid)) { 3688c2ecf20Sopenharmony_ci irq_set_irqchip_state(irq->host_irq, 3698c2ecf20Sopenharmony_ci IRQCHIP_STATE_PENDING, 3708c2ecf20Sopenharmony_ci irq->pending_latch); 3718c2ecf20Sopenharmony_ci irq->pending_latch = false; 3728c2ecf20Sopenharmony_ci } 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci if (irq->pending_latch) 3758c2ecf20Sopenharmony_ci vgic_queue_irq_unlock(vcpu->kvm, irq, flags); 3768c2ecf20Sopenharmony_ci else 3778c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&irq->irq_lock, flags); 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci vgic_put_irq(vcpu->kvm, irq); 3808c2ecf20Sopenharmony_ci } 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci return 0; 3838c2ecf20Sopenharmony_ci} 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_ci/* We want to avoid outer shareable. */ 3868c2ecf20Sopenharmony_ciu64 vgic_sanitise_shareability(u64 field) 3878c2ecf20Sopenharmony_ci{ 3888c2ecf20Sopenharmony_ci switch (field) { 3898c2ecf20Sopenharmony_ci case GIC_BASER_OuterShareable: 3908c2ecf20Sopenharmony_ci return GIC_BASER_InnerShareable; 3918c2ecf20Sopenharmony_ci default: 3928c2ecf20Sopenharmony_ci return field; 3938c2ecf20Sopenharmony_ci } 3948c2ecf20Sopenharmony_ci} 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ci/* Avoid any inner non-cacheable mapping. */ 3978c2ecf20Sopenharmony_ciu64 vgic_sanitise_inner_cacheability(u64 field) 3988c2ecf20Sopenharmony_ci{ 3998c2ecf20Sopenharmony_ci switch (field) { 4008c2ecf20Sopenharmony_ci case GIC_BASER_CACHE_nCnB: 4018c2ecf20Sopenharmony_ci case GIC_BASER_CACHE_nC: 4028c2ecf20Sopenharmony_ci return GIC_BASER_CACHE_RaWb; 4038c2ecf20Sopenharmony_ci default: 4048c2ecf20Sopenharmony_ci return field; 4058c2ecf20Sopenharmony_ci } 4068c2ecf20Sopenharmony_ci} 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci/* Non-cacheable or same-as-inner are OK. */ 4098c2ecf20Sopenharmony_ciu64 vgic_sanitise_outer_cacheability(u64 field) 4108c2ecf20Sopenharmony_ci{ 4118c2ecf20Sopenharmony_ci switch (field) { 4128c2ecf20Sopenharmony_ci case GIC_BASER_CACHE_SameAsInner: 4138c2ecf20Sopenharmony_ci case GIC_BASER_CACHE_nC: 4148c2ecf20Sopenharmony_ci return field; 4158c2ecf20Sopenharmony_ci default: 4168c2ecf20Sopenharmony_ci return GIC_BASER_CACHE_SameAsInner; 4178c2ecf20Sopenharmony_ci } 4188c2ecf20Sopenharmony_ci} 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ciu64 vgic_sanitise_field(u64 reg, u64 field_mask, int field_shift, 4218c2ecf20Sopenharmony_ci u64 (*sanitise_fn)(u64)) 4228c2ecf20Sopenharmony_ci{ 4238c2ecf20Sopenharmony_ci u64 field = (reg & field_mask) >> field_shift; 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci field = sanitise_fn(field) << field_shift; 4268c2ecf20Sopenharmony_ci return (reg & ~field_mask) | field; 4278c2ecf20Sopenharmony_ci} 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci#define PROPBASER_RES0_MASK \ 4308c2ecf20Sopenharmony_ci (GENMASK_ULL(63, 59) | GENMASK_ULL(55, 52) | GENMASK_ULL(6, 5)) 4318c2ecf20Sopenharmony_ci#define PENDBASER_RES0_MASK \ 4328c2ecf20Sopenharmony_ci (BIT_ULL(63) | GENMASK_ULL(61, 59) | GENMASK_ULL(55, 52) | \ 4338c2ecf20Sopenharmony_ci GENMASK_ULL(15, 12) | GENMASK_ULL(6, 0)) 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_cistatic u64 vgic_sanitise_pendbaser(u64 reg) 4368c2ecf20Sopenharmony_ci{ 4378c2ecf20Sopenharmony_ci reg = vgic_sanitise_field(reg, GICR_PENDBASER_SHAREABILITY_MASK, 4388c2ecf20Sopenharmony_ci GICR_PENDBASER_SHAREABILITY_SHIFT, 4398c2ecf20Sopenharmony_ci vgic_sanitise_shareability); 4408c2ecf20Sopenharmony_ci reg = vgic_sanitise_field(reg, GICR_PENDBASER_INNER_CACHEABILITY_MASK, 4418c2ecf20Sopenharmony_ci GICR_PENDBASER_INNER_CACHEABILITY_SHIFT, 4428c2ecf20Sopenharmony_ci vgic_sanitise_inner_cacheability); 4438c2ecf20Sopenharmony_ci reg = vgic_sanitise_field(reg, GICR_PENDBASER_OUTER_CACHEABILITY_MASK, 4448c2ecf20Sopenharmony_ci GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT, 4458c2ecf20Sopenharmony_ci vgic_sanitise_outer_cacheability); 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci reg &= ~PENDBASER_RES0_MASK; 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci return reg; 4508c2ecf20Sopenharmony_ci} 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_cistatic u64 vgic_sanitise_propbaser(u64 reg) 4538c2ecf20Sopenharmony_ci{ 4548c2ecf20Sopenharmony_ci reg = vgic_sanitise_field(reg, GICR_PROPBASER_SHAREABILITY_MASK, 4558c2ecf20Sopenharmony_ci GICR_PROPBASER_SHAREABILITY_SHIFT, 4568c2ecf20Sopenharmony_ci vgic_sanitise_shareability); 4578c2ecf20Sopenharmony_ci reg = vgic_sanitise_field(reg, GICR_PROPBASER_INNER_CACHEABILITY_MASK, 4588c2ecf20Sopenharmony_ci GICR_PROPBASER_INNER_CACHEABILITY_SHIFT, 4598c2ecf20Sopenharmony_ci vgic_sanitise_inner_cacheability); 4608c2ecf20Sopenharmony_ci reg = vgic_sanitise_field(reg, GICR_PROPBASER_OUTER_CACHEABILITY_MASK, 4618c2ecf20Sopenharmony_ci GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT, 4628c2ecf20Sopenharmony_ci vgic_sanitise_outer_cacheability); 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci reg &= ~PROPBASER_RES0_MASK; 4658c2ecf20Sopenharmony_ci return reg; 4668c2ecf20Sopenharmony_ci} 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_cistatic unsigned long vgic_mmio_read_propbase(struct kvm_vcpu *vcpu, 4698c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len) 4708c2ecf20Sopenharmony_ci{ 4718c2ecf20Sopenharmony_ci struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_ci return extract_bytes(dist->propbaser, addr & 7, len); 4748c2ecf20Sopenharmony_ci} 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_cistatic void vgic_mmio_write_propbase(struct kvm_vcpu *vcpu, 4778c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len, 4788c2ecf20Sopenharmony_ci unsigned long val) 4798c2ecf20Sopenharmony_ci{ 4808c2ecf20Sopenharmony_ci struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 4818c2ecf20Sopenharmony_ci struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 4828c2ecf20Sopenharmony_ci u64 old_propbaser, propbaser; 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_ci /* Storing a value with LPIs already enabled is undefined */ 4858c2ecf20Sopenharmony_ci if (vgic_cpu->lpis_enabled) 4868c2ecf20Sopenharmony_ci return; 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ci do { 4898c2ecf20Sopenharmony_ci old_propbaser = READ_ONCE(dist->propbaser); 4908c2ecf20Sopenharmony_ci propbaser = old_propbaser; 4918c2ecf20Sopenharmony_ci propbaser = update_64bit_reg(propbaser, addr & 4, len, val); 4928c2ecf20Sopenharmony_ci propbaser = vgic_sanitise_propbaser(propbaser); 4938c2ecf20Sopenharmony_ci } while (cmpxchg64(&dist->propbaser, old_propbaser, 4948c2ecf20Sopenharmony_ci propbaser) != old_propbaser); 4958c2ecf20Sopenharmony_ci} 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_cistatic unsigned long vgic_mmio_read_pendbase(struct kvm_vcpu *vcpu, 4988c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len) 4998c2ecf20Sopenharmony_ci{ 5008c2ecf20Sopenharmony_ci struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 5018c2ecf20Sopenharmony_ci u64 value = vgic_cpu->pendbaser; 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci value &= ~GICR_PENDBASER_PTZ; 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci return extract_bytes(value, addr & 7, len); 5068c2ecf20Sopenharmony_ci} 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_cistatic void vgic_mmio_write_pendbase(struct kvm_vcpu *vcpu, 5098c2ecf20Sopenharmony_ci gpa_t addr, unsigned int len, 5108c2ecf20Sopenharmony_ci unsigned long val) 5118c2ecf20Sopenharmony_ci{ 5128c2ecf20Sopenharmony_ci struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 5138c2ecf20Sopenharmony_ci u64 old_pendbaser, pendbaser; 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci /* Storing a value with LPIs already enabled is undefined */ 5168c2ecf20Sopenharmony_ci if (vgic_cpu->lpis_enabled) 5178c2ecf20Sopenharmony_ci return; 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_ci do { 5208c2ecf20Sopenharmony_ci old_pendbaser = READ_ONCE(vgic_cpu->pendbaser); 5218c2ecf20Sopenharmony_ci pendbaser = old_pendbaser; 5228c2ecf20Sopenharmony_ci pendbaser = update_64bit_reg(pendbaser, addr & 4, len, val); 5238c2ecf20Sopenharmony_ci pendbaser = vgic_sanitise_pendbaser(pendbaser); 5248c2ecf20Sopenharmony_ci } while (cmpxchg64(&vgic_cpu->pendbaser, old_pendbaser, 5258c2ecf20Sopenharmony_ci pendbaser) != old_pendbaser); 5268c2ecf20Sopenharmony_ci} 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ci/* 5298c2ecf20Sopenharmony_ci * The GICv3 per-IRQ registers are split to control PPIs and SGIs in the 5308c2ecf20Sopenharmony_ci * redistributors, while SPIs are covered by registers in the distributor 5318c2ecf20Sopenharmony_ci * block. Trying to set private IRQs in this block gets ignored. 5328c2ecf20Sopenharmony_ci * We take some special care here to fix the calculation of the register 5338c2ecf20Sopenharmony_ci * offset. 5348c2ecf20Sopenharmony_ci */ 5358c2ecf20Sopenharmony_ci#define REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(off, rd, wr, ur, uw, bpi, acc) \ 5368c2ecf20Sopenharmony_ci { \ 5378c2ecf20Sopenharmony_ci .reg_offset = off, \ 5388c2ecf20Sopenharmony_ci .bits_per_irq = bpi, \ 5398c2ecf20Sopenharmony_ci .len = (bpi * VGIC_NR_PRIVATE_IRQS) / 8, \ 5408c2ecf20Sopenharmony_ci .access_flags = acc, \ 5418c2ecf20Sopenharmony_ci .read = vgic_mmio_read_raz, \ 5428c2ecf20Sopenharmony_ci .write = vgic_mmio_write_wi, \ 5438c2ecf20Sopenharmony_ci }, { \ 5448c2ecf20Sopenharmony_ci .reg_offset = off + (bpi * VGIC_NR_PRIVATE_IRQS) / 8, \ 5458c2ecf20Sopenharmony_ci .bits_per_irq = bpi, \ 5468c2ecf20Sopenharmony_ci .len = (bpi * (1024 - VGIC_NR_PRIVATE_IRQS)) / 8, \ 5478c2ecf20Sopenharmony_ci .access_flags = acc, \ 5488c2ecf20Sopenharmony_ci .read = rd, \ 5498c2ecf20Sopenharmony_ci .write = wr, \ 5508c2ecf20Sopenharmony_ci .uaccess_read = ur, \ 5518c2ecf20Sopenharmony_ci .uaccess_write = uw, \ 5528c2ecf20Sopenharmony_ci } 5538c2ecf20Sopenharmony_ci 5548c2ecf20Sopenharmony_cistatic const struct vgic_register_region vgic_v3_dist_registers[] = { 5558c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH_UACCESS(GICD_CTLR, 5568c2ecf20Sopenharmony_ci vgic_mmio_read_v3_misc, vgic_mmio_write_v3_misc, 5578c2ecf20Sopenharmony_ci NULL, vgic_mmio_uaccess_write_v3_misc, 5588c2ecf20Sopenharmony_ci 16, VGIC_ACCESS_32bit), 5598c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH(GICD_STATUSR, 5608c2ecf20Sopenharmony_ci vgic_mmio_read_rao, vgic_mmio_write_wi, 4, 5618c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 5628c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IGROUPR, 5638c2ecf20Sopenharmony_ci vgic_mmio_read_group, vgic_mmio_write_group, NULL, NULL, 1, 5648c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 5658c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ISENABLER, 5668c2ecf20Sopenharmony_ci vgic_mmio_read_enable, vgic_mmio_write_senable, 5678c2ecf20Sopenharmony_ci NULL, vgic_uaccess_write_senable, 1, 5688c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 5698c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICENABLER, 5708c2ecf20Sopenharmony_ci vgic_mmio_read_enable, vgic_mmio_write_cenable, 5718c2ecf20Sopenharmony_ci NULL, vgic_uaccess_write_cenable, 1, 5728c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 5738c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ISPENDR, 5748c2ecf20Sopenharmony_ci vgic_mmio_read_pending, vgic_mmio_write_spending, 5758c2ecf20Sopenharmony_ci vgic_v3_uaccess_read_pending, vgic_v3_uaccess_write_pending, 1, 5768c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 5778c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICPENDR, 5788c2ecf20Sopenharmony_ci vgic_mmio_read_pending, vgic_mmio_write_cpending, 5798c2ecf20Sopenharmony_ci vgic_mmio_read_raz, vgic_mmio_uaccess_write_wi, 1, 5808c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 5818c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ISACTIVER, 5828c2ecf20Sopenharmony_ci vgic_mmio_read_active, vgic_mmio_write_sactive, 5838c2ecf20Sopenharmony_ci vgic_uaccess_read_active, vgic_mmio_uaccess_write_sactive, 1, 5848c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 5858c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICACTIVER, 5868c2ecf20Sopenharmony_ci vgic_mmio_read_active, vgic_mmio_write_cactive, 5878c2ecf20Sopenharmony_ci vgic_uaccess_read_active, vgic_mmio_uaccess_write_cactive, 5888c2ecf20Sopenharmony_ci 1, VGIC_ACCESS_32bit), 5898c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IPRIORITYR, 5908c2ecf20Sopenharmony_ci vgic_mmio_read_priority, vgic_mmio_write_priority, NULL, NULL, 5918c2ecf20Sopenharmony_ci 8, VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), 5928c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ITARGETSR, 5938c2ecf20Sopenharmony_ci vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 8, 5948c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), 5958c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICFGR, 5968c2ecf20Sopenharmony_ci vgic_mmio_read_config, vgic_mmio_write_config, NULL, NULL, 2, 5978c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 5988c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IGRPMODR, 5998c2ecf20Sopenharmony_ci vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1, 6008c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6018c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IROUTER, 6028c2ecf20Sopenharmony_ci vgic_mmio_read_irouter, vgic_mmio_write_irouter, NULL, NULL, 64, 6038c2ecf20Sopenharmony_ci VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), 6048c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH(GICD_IDREGS, 6058c2ecf20Sopenharmony_ci vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48, 6068c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6078c2ecf20Sopenharmony_ci}; 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_cistatic const struct vgic_register_region vgic_v3_rd_registers[] = { 6108c2ecf20Sopenharmony_ci /* RD_base registers */ 6118c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH(GICR_CTLR, 6128c2ecf20Sopenharmony_ci vgic_mmio_read_v3r_ctlr, vgic_mmio_write_v3r_ctlr, 4, 6138c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6148c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH(GICR_STATUSR, 6158c2ecf20Sopenharmony_ci vgic_mmio_read_raz, vgic_mmio_write_wi, 4, 6168c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6178c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH(GICR_IIDR, 6188c2ecf20Sopenharmony_ci vgic_mmio_read_v3r_iidr, vgic_mmio_write_wi, 4, 6198c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6208c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH_UACCESS(GICR_TYPER, 6218c2ecf20Sopenharmony_ci vgic_mmio_read_v3r_typer, vgic_mmio_write_wi, 6228c2ecf20Sopenharmony_ci vgic_uaccess_read_v3r_typer, vgic_mmio_uaccess_write_wi, 8, 6238c2ecf20Sopenharmony_ci VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), 6248c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH(GICR_WAKER, 6258c2ecf20Sopenharmony_ci vgic_mmio_read_raz, vgic_mmio_write_wi, 4, 6268c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6278c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH(GICR_PROPBASER, 6288c2ecf20Sopenharmony_ci vgic_mmio_read_propbase, vgic_mmio_write_propbase, 8, 6298c2ecf20Sopenharmony_ci VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), 6308c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH(GICR_PENDBASER, 6318c2ecf20Sopenharmony_ci vgic_mmio_read_pendbase, vgic_mmio_write_pendbase, 8, 6328c2ecf20Sopenharmony_ci VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), 6338c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH(GICR_IDREGS, 6348c2ecf20Sopenharmony_ci vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48, 6358c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6368c2ecf20Sopenharmony_ci /* SGI_base registers */ 6378c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_IGROUPR0, 6388c2ecf20Sopenharmony_ci vgic_mmio_read_group, vgic_mmio_write_group, 4, 6398c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6408c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH_UACCESS(SZ_64K + GICR_ISENABLER0, 6418c2ecf20Sopenharmony_ci vgic_mmio_read_enable, vgic_mmio_write_senable, 6428c2ecf20Sopenharmony_ci NULL, vgic_uaccess_write_senable, 4, 6438c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6448c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH_UACCESS(SZ_64K + GICR_ICENABLER0, 6458c2ecf20Sopenharmony_ci vgic_mmio_read_enable, vgic_mmio_write_cenable, 6468c2ecf20Sopenharmony_ci NULL, vgic_uaccess_write_cenable, 4, 6478c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6488c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH_UACCESS(SZ_64K + GICR_ISPENDR0, 6498c2ecf20Sopenharmony_ci vgic_mmio_read_pending, vgic_mmio_write_spending, 6508c2ecf20Sopenharmony_ci vgic_v3_uaccess_read_pending, vgic_v3_uaccess_write_pending, 4, 6518c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6528c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH_UACCESS(SZ_64K + GICR_ICPENDR0, 6538c2ecf20Sopenharmony_ci vgic_mmio_read_pending, vgic_mmio_write_cpending, 6548c2ecf20Sopenharmony_ci vgic_mmio_read_raz, vgic_mmio_uaccess_write_wi, 4, 6558c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6568c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH_UACCESS(SZ_64K + GICR_ISACTIVER0, 6578c2ecf20Sopenharmony_ci vgic_mmio_read_active, vgic_mmio_write_sactive, 6588c2ecf20Sopenharmony_ci vgic_uaccess_read_active, vgic_mmio_uaccess_write_sactive, 4, 6598c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6608c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH_UACCESS(SZ_64K + GICR_ICACTIVER0, 6618c2ecf20Sopenharmony_ci vgic_mmio_read_active, vgic_mmio_write_cactive, 6628c2ecf20Sopenharmony_ci vgic_uaccess_read_active, vgic_mmio_uaccess_write_cactive, 4, 6638c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6648c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_IPRIORITYR0, 6658c2ecf20Sopenharmony_ci vgic_mmio_read_priority, vgic_mmio_write_priority, 32, 6668c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), 6678c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_ICFGR0, 6688c2ecf20Sopenharmony_ci vgic_mmio_read_config, vgic_mmio_write_config, 8, 6698c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6708c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_IGRPMODR0, 6718c2ecf20Sopenharmony_ci vgic_mmio_read_raz, vgic_mmio_write_wi, 4, 6728c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6738c2ecf20Sopenharmony_ci REGISTER_DESC_WITH_LENGTH(SZ_64K + GICR_NSACR, 6748c2ecf20Sopenharmony_ci vgic_mmio_read_raz, vgic_mmio_write_wi, 4, 6758c2ecf20Sopenharmony_ci VGIC_ACCESS_32bit), 6768c2ecf20Sopenharmony_ci}; 6778c2ecf20Sopenharmony_ci 6788c2ecf20Sopenharmony_ciunsigned int vgic_v3_init_dist_iodev(struct vgic_io_device *dev) 6798c2ecf20Sopenharmony_ci{ 6808c2ecf20Sopenharmony_ci dev->regions = vgic_v3_dist_registers; 6818c2ecf20Sopenharmony_ci dev->nr_regions = ARRAY_SIZE(vgic_v3_dist_registers); 6828c2ecf20Sopenharmony_ci 6838c2ecf20Sopenharmony_ci kvm_iodevice_init(&dev->dev, &kvm_io_gic_ops); 6848c2ecf20Sopenharmony_ci 6858c2ecf20Sopenharmony_ci return SZ_64K; 6868c2ecf20Sopenharmony_ci} 6878c2ecf20Sopenharmony_ci 6888c2ecf20Sopenharmony_ci/** 6898c2ecf20Sopenharmony_ci * vgic_register_redist_iodev - register a single redist iodev 6908c2ecf20Sopenharmony_ci * @vcpu: The VCPU to which the redistributor belongs 6918c2ecf20Sopenharmony_ci * 6928c2ecf20Sopenharmony_ci * Register a KVM iodev for this VCPU's redistributor using the address 6938c2ecf20Sopenharmony_ci * provided. 6948c2ecf20Sopenharmony_ci * 6958c2ecf20Sopenharmony_ci * Return 0 on success, -ERRNO otherwise. 6968c2ecf20Sopenharmony_ci */ 6978c2ecf20Sopenharmony_ciint vgic_register_redist_iodev(struct kvm_vcpu *vcpu) 6988c2ecf20Sopenharmony_ci{ 6998c2ecf20Sopenharmony_ci struct kvm *kvm = vcpu->kvm; 7008c2ecf20Sopenharmony_ci struct vgic_dist *vgic = &kvm->arch.vgic; 7018c2ecf20Sopenharmony_ci struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 7028c2ecf20Sopenharmony_ci struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev; 7038c2ecf20Sopenharmony_ci struct vgic_redist_region *rdreg; 7048c2ecf20Sopenharmony_ci gpa_t rd_base; 7058c2ecf20Sopenharmony_ci int ret; 7068c2ecf20Sopenharmony_ci 7078c2ecf20Sopenharmony_ci if (!IS_VGIC_ADDR_UNDEF(vgic_cpu->rd_iodev.base_addr)) 7088c2ecf20Sopenharmony_ci return 0; 7098c2ecf20Sopenharmony_ci 7108c2ecf20Sopenharmony_ci /* 7118c2ecf20Sopenharmony_ci * We may be creating VCPUs before having set the base address for the 7128c2ecf20Sopenharmony_ci * redistributor region, in which case we will come back to this 7138c2ecf20Sopenharmony_ci * function for all VCPUs when the base address is set. Just return 7148c2ecf20Sopenharmony_ci * without doing any work for now. 7158c2ecf20Sopenharmony_ci */ 7168c2ecf20Sopenharmony_ci rdreg = vgic_v3_rdist_free_slot(&vgic->rd_regions); 7178c2ecf20Sopenharmony_ci if (!rdreg) 7188c2ecf20Sopenharmony_ci return 0; 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci if (!vgic_v3_check_base(kvm)) 7218c2ecf20Sopenharmony_ci return -EINVAL; 7228c2ecf20Sopenharmony_ci 7238c2ecf20Sopenharmony_ci vgic_cpu->rdreg = rdreg; 7248c2ecf20Sopenharmony_ci 7258c2ecf20Sopenharmony_ci rd_base = rdreg->base + rdreg->free_index * KVM_VGIC_V3_REDIST_SIZE; 7268c2ecf20Sopenharmony_ci 7278c2ecf20Sopenharmony_ci kvm_iodevice_init(&rd_dev->dev, &kvm_io_gic_ops); 7288c2ecf20Sopenharmony_ci rd_dev->base_addr = rd_base; 7298c2ecf20Sopenharmony_ci rd_dev->iodev_type = IODEV_REDIST; 7308c2ecf20Sopenharmony_ci rd_dev->regions = vgic_v3_rd_registers; 7318c2ecf20Sopenharmony_ci rd_dev->nr_regions = ARRAY_SIZE(vgic_v3_rd_registers); 7328c2ecf20Sopenharmony_ci rd_dev->redist_vcpu = vcpu; 7338c2ecf20Sopenharmony_ci 7348c2ecf20Sopenharmony_ci mutex_lock(&kvm->slots_lock); 7358c2ecf20Sopenharmony_ci ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, rd_base, 7368c2ecf20Sopenharmony_ci 2 * SZ_64K, &rd_dev->dev); 7378c2ecf20Sopenharmony_ci mutex_unlock(&kvm->slots_lock); 7388c2ecf20Sopenharmony_ci 7398c2ecf20Sopenharmony_ci if (ret) 7408c2ecf20Sopenharmony_ci return ret; 7418c2ecf20Sopenharmony_ci 7428c2ecf20Sopenharmony_ci rdreg->free_index++; 7438c2ecf20Sopenharmony_ci return 0; 7448c2ecf20Sopenharmony_ci} 7458c2ecf20Sopenharmony_ci 7468c2ecf20Sopenharmony_cistatic void vgic_unregister_redist_iodev(struct kvm_vcpu *vcpu) 7478c2ecf20Sopenharmony_ci{ 7488c2ecf20Sopenharmony_ci struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev; 7498c2ecf20Sopenharmony_ci 7508c2ecf20Sopenharmony_ci kvm_io_bus_unregister_dev(vcpu->kvm, KVM_MMIO_BUS, &rd_dev->dev); 7518c2ecf20Sopenharmony_ci} 7528c2ecf20Sopenharmony_ci 7538c2ecf20Sopenharmony_cistatic int vgic_register_all_redist_iodevs(struct kvm *kvm) 7548c2ecf20Sopenharmony_ci{ 7558c2ecf20Sopenharmony_ci struct kvm_vcpu *vcpu; 7568c2ecf20Sopenharmony_ci int c, ret = 0; 7578c2ecf20Sopenharmony_ci 7588c2ecf20Sopenharmony_ci kvm_for_each_vcpu(c, vcpu, kvm) { 7598c2ecf20Sopenharmony_ci ret = vgic_register_redist_iodev(vcpu); 7608c2ecf20Sopenharmony_ci if (ret) 7618c2ecf20Sopenharmony_ci break; 7628c2ecf20Sopenharmony_ci } 7638c2ecf20Sopenharmony_ci 7648c2ecf20Sopenharmony_ci if (ret) { 7658c2ecf20Sopenharmony_ci /* The current c failed, so we start with the previous one. */ 7668c2ecf20Sopenharmony_ci mutex_lock(&kvm->slots_lock); 7678c2ecf20Sopenharmony_ci for (c--; c >= 0; c--) { 7688c2ecf20Sopenharmony_ci vcpu = kvm_get_vcpu(kvm, c); 7698c2ecf20Sopenharmony_ci vgic_unregister_redist_iodev(vcpu); 7708c2ecf20Sopenharmony_ci } 7718c2ecf20Sopenharmony_ci mutex_unlock(&kvm->slots_lock); 7728c2ecf20Sopenharmony_ci } 7738c2ecf20Sopenharmony_ci 7748c2ecf20Sopenharmony_ci return ret; 7758c2ecf20Sopenharmony_ci} 7768c2ecf20Sopenharmony_ci 7778c2ecf20Sopenharmony_ci/** 7788c2ecf20Sopenharmony_ci * vgic_v3_insert_redist_region - Insert a new redistributor region 7798c2ecf20Sopenharmony_ci * 7808c2ecf20Sopenharmony_ci * Performs various checks before inserting the rdist region in the list. 7818c2ecf20Sopenharmony_ci * Those tests depend on whether the size of the rdist region is known 7828c2ecf20Sopenharmony_ci * (ie. count != 0). The list is sorted by rdist region index. 7838c2ecf20Sopenharmony_ci * 7848c2ecf20Sopenharmony_ci * @kvm: kvm handle 7858c2ecf20Sopenharmony_ci * @index: redist region index 7868c2ecf20Sopenharmony_ci * @base: base of the new rdist region 7878c2ecf20Sopenharmony_ci * @count: number of redistributors the region is made of (0 in the old style 7888c2ecf20Sopenharmony_ci * single region, whose size is induced from the number of vcpus) 7898c2ecf20Sopenharmony_ci * 7908c2ecf20Sopenharmony_ci * Return 0 on success, < 0 otherwise 7918c2ecf20Sopenharmony_ci */ 7928c2ecf20Sopenharmony_cistatic int vgic_v3_insert_redist_region(struct kvm *kvm, uint32_t index, 7938c2ecf20Sopenharmony_ci gpa_t base, uint32_t count) 7948c2ecf20Sopenharmony_ci{ 7958c2ecf20Sopenharmony_ci struct vgic_dist *d = &kvm->arch.vgic; 7968c2ecf20Sopenharmony_ci struct vgic_redist_region *rdreg; 7978c2ecf20Sopenharmony_ci struct list_head *rd_regions = &d->rd_regions; 7988c2ecf20Sopenharmony_ci size_t size = count * KVM_VGIC_V3_REDIST_SIZE; 7998c2ecf20Sopenharmony_ci int ret; 8008c2ecf20Sopenharmony_ci 8018c2ecf20Sopenharmony_ci /* single rdist region already set ?*/ 8028c2ecf20Sopenharmony_ci if (!count && !list_empty(rd_regions)) 8038c2ecf20Sopenharmony_ci return -EINVAL; 8048c2ecf20Sopenharmony_ci 8058c2ecf20Sopenharmony_ci /* cross the end of memory ? */ 8068c2ecf20Sopenharmony_ci if (base + size < base) 8078c2ecf20Sopenharmony_ci return -EINVAL; 8088c2ecf20Sopenharmony_ci 8098c2ecf20Sopenharmony_ci if (list_empty(rd_regions)) { 8108c2ecf20Sopenharmony_ci if (index != 0) 8118c2ecf20Sopenharmony_ci return -EINVAL; 8128c2ecf20Sopenharmony_ci } else { 8138c2ecf20Sopenharmony_ci rdreg = list_last_entry(rd_regions, 8148c2ecf20Sopenharmony_ci struct vgic_redist_region, list); 8158c2ecf20Sopenharmony_ci if (index != rdreg->index + 1) 8168c2ecf20Sopenharmony_ci return -EINVAL; 8178c2ecf20Sopenharmony_ci 8188c2ecf20Sopenharmony_ci /* Cannot add an explicitly sized regions after legacy region */ 8198c2ecf20Sopenharmony_ci if (!rdreg->count) 8208c2ecf20Sopenharmony_ci return -EINVAL; 8218c2ecf20Sopenharmony_ci } 8228c2ecf20Sopenharmony_ci 8238c2ecf20Sopenharmony_ci /* 8248c2ecf20Sopenharmony_ci * For legacy single-region redistributor regions (!count), 8258c2ecf20Sopenharmony_ci * check that the redistributor region does not overlap with the 8268c2ecf20Sopenharmony_ci * distributor's address space. 8278c2ecf20Sopenharmony_ci */ 8288c2ecf20Sopenharmony_ci if (!count && !IS_VGIC_ADDR_UNDEF(d->vgic_dist_base) && 8298c2ecf20Sopenharmony_ci vgic_dist_overlap(kvm, base, size)) 8308c2ecf20Sopenharmony_ci return -EINVAL; 8318c2ecf20Sopenharmony_ci 8328c2ecf20Sopenharmony_ci /* collision with any other rdist region? */ 8338c2ecf20Sopenharmony_ci if (vgic_v3_rdist_overlap(kvm, base, size)) 8348c2ecf20Sopenharmony_ci return -EINVAL; 8358c2ecf20Sopenharmony_ci 8368c2ecf20Sopenharmony_ci rdreg = kzalloc(sizeof(*rdreg), GFP_KERNEL); 8378c2ecf20Sopenharmony_ci if (!rdreg) 8388c2ecf20Sopenharmony_ci return -ENOMEM; 8398c2ecf20Sopenharmony_ci 8408c2ecf20Sopenharmony_ci rdreg->base = VGIC_ADDR_UNDEF; 8418c2ecf20Sopenharmony_ci 8428c2ecf20Sopenharmony_ci ret = vgic_check_ioaddr(kvm, &rdreg->base, base, SZ_64K); 8438c2ecf20Sopenharmony_ci if (ret) 8448c2ecf20Sopenharmony_ci goto free; 8458c2ecf20Sopenharmony_ci 8468c2ecf20Sopenharmony_ci rdreg->base = base; 8478c2ecf20Sopenharmony_ci rdreg->count = count; 8488c2ecf20Sopenharmony_ci rdreg->free_index = 0; 8498c2ecf20Sopenharmony_ci rdreg->index = index; 8508c2ecf20Sopenharmony_ci 8518c2ecf20Sopenharmony_ci list_add_tail(&rdreg->list, rd_regions); 8528c2ecf20Sopenharmony_ci return 0; 8538c2ecf20Sopenharmony_cifree: 8548c2ecf20Sopenharmony_ci kfree(rdreg); 8558c2ecf20Sopenharmony_ci return ret; 8568c2ecf20Sopenharmony_ci} 8578c2ecf20Sopenharmony_ci 8588c2ecf20Sopenharmony_ciint vgic_v3_set_redist_base(struct kvm *kvm, u32 index, u64 addr, u32 count) 8598c2ecf20Sopenharmony_ci{ 8608c2ecf20Sopenharmony_ci int ret; 8618c2ecf20Sopenharmony_ci 8628c2ecf20Sopenharmony_ci ret = vgic_v3_insert_redist_region(kvm, index, addr, count); 8638c2ecf20Sopenharmony_ci if (ret) 8648c2ecf20Sopenharmony_ci return ret; 8658c2ecf20Sopenharmony_ci 8668c2ecf20Sopenharmony_ci /* 8678c2ecf20Sopenharmony_ci * Register iodevs for each existing VCPU. Adding more VCPUs 8688c2ecf20Sopenharmony_ci * afterwards will register the iodevs when needed. 8698c2ecf20Sopenharmony_ci */ 8708c2ecf20Sopenharmony_ci ret = vgic_register_all_redist_iodevs(kvm); 8718c2ecf20Sopenharmony_ci if (ret) 8728c2ecf20Sopenharmony_ci return ret; 8738c2ecf20Sopenharmony_ci 8748c2ecf20Sopenharmony_ci return 0; 8758c2ecf20Sopenharmony_ci} 8768c2ecf20Sopenharmony_ci 8778c2ecf20Sopenharmony_ciint vgic_v3_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr) 8788c2ecf20Sopenharmony_ci{ 8798c2ecf20Sopenharmony_ci const struct vgic_register_region *region; 8808c2ecf20Sopenharmony_ci struct vgic_io_device iodev; 8818c2ecf20Sopenharmony_ci struct vgic_reg_attr reg_attr; 8828c2ecf20Sopenharmony_ci struct kvm_vcpu *vcpu; 8838c2ecf20Sopenharmony_ci gpa_t addr; 8848c2ecf20Sopenharmony_ci int ret; 8858c2ecf20Sopenharmony_ci 8868c2ecf20Sopenharmony_ci ret = vgic_v3_parse_attr(dev, attr, ®_attr); 8878c2ecf20Sopenharmony_ci if (ret) 8888c2ecf20Sopenharmony_ci return ret; 8898c2ecf20Sopenharmony_ci 8908c2ecf20Sopenharmony_ci vcpu = reg_attr.vcpu; 8918c2ecf20Sopenharmony_ci addr = reg_attr.addr; 8928c2ecf20Sopenharmony_ci 8938c2ecf20Sopenharmony_ci switch (attr->group) { 8948c2ecf20Sopenharmony_ci case KVM_DEV_ARM_VGIC_GRP_DIST_REGS: 8958c2ecf20Sopenharmony_ci iodev.regions = vgic_v3_dist_registers; 8968c2ecf20Sopenharmony_ci iodev.nr_regions = ARRAY_SIZE(vgic_v3_dist_registers); 8978c2ecf20Sopenharmony_ci iodev.base_addr = 0; 8988c2ecf20Sopenharmony_ci break; 8998c2ecf20Sopenharmony_ci case KVM_DEV_ARM_VGIC_GRP_REDIST_REGS:{ 9008c2ecf20Sopenharmony_ci iodev.regions = vgic_v3_rd_registers; 9018c2ecf20Sopenharmony_ci iodev.nr_regions = ARRAY_SIZE(vgic_v3_rd_registers); 9028c2ecf20Sopenharmony_ci iodev.base_addr = 0; 9038c2ecf20Sopenharmony_ci break; 9048c2ecf20Sopenharmony_ci } 9058c2ecf20Sopenharmony_ci case KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS: { 9068c2ecf20Sopenharmony_ci u64 reg, id; 9078c2ecf20Sopenharmony_ci 9088c2ecf20Sopenharmony_ci id = (attr->attr & KVM_DEV_ARM_VGIC_SYSREG_INSTR_MASK); 9098c2ecf20Sopenharmony_ci return vgic_v3_has_cpu_sysregs_attr(vcpu, 0, id, ®); 9108c2ecf20Sopenharmony_ci } 9118c2ecf20Sopenharmony_ci default: 9128c2ecf20Sopenharmony_ci return -ENXIO; 9138c2ecf20Sopenharmony_ci } 9148c2ecf20Sopenharmony_ci 9158c2ecf20Sopenharmony_ci /* We only support aligned 32-bit accesses. */ 9168c2ecf20Sopenharmony_ci if (addr & 3) 9178c2ecf20Sopenharmony_ci return -ENXIO; 9188c2ecf20Sopenharmony_ci 9198c2ecf20Sopenharmony_ci region = vgic_get_mmio_region(vcpu, &iodev, addr, sizeof(u32)); 9208c2ecf20Sopenharmony_ci if (!region) 9218c2ecf20Sopenharmony_ci return -ENXIO; 9228c2ecf20Sopenharmony_ci 9238c2ecf20Sopenharmony_ci return 0; 9248c2ecf20Sopenharmony_ci} 9258c2ecf20Sopenharmony_ci/* 9268c2ecf20Sopenharmony_ci * Compare a given affinity (level 1-3 and a level 0 mask, from the SGI 9278c2ecf20Sopenharmony_ci * generation register ICC_SGI1R_EL1) with a given VCPU. 9288c2ecf20Sopenharmony_ci * If the VCPU's MPIDR matches, return the level0 affinity, otherwise 9298c2ecf20Sopenharmony_ci * return -1. 9308c2ecf20Sopenharmony_ci */ 9318c2ecf20Sopenharmony_cistatic int match_mpidr(u64 sgi_aff, u16 sgi_cpu_mask, struct kvm_vcpu *vcpu) 9328c2ecf20Sopenharmony_ci{ 9338c2ecf20Sopenharmony_ci unsigned long affinity; 9348c2ecf20Sopenharmony_ci int level0; 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci /* 9378c2ecf20Sopenharmony_ci * Split the current VCPU's MPIDR into affinity level 0 and the 9388c2ecf20Sopenharmony_ci * rest as this is what we have to compare against. 9398c2ecf20Sopenharmony_ci */ 9408c2ecf20Sopenharmony_ci affinity = kvm_vcpu_get_mpidr_aff(vcpu); 9418c2ecf20Sopenharmony_ci level0 = MPIDR_AFFINITY_LEVEL(affinity, 0); 9428c2ecf20Sopenharmony_ci affinity &= ~MPIDR_LEVEL_MASK; 9438c2ecf20Sopenharmony_ci 9448c2ecf20Sopenharmony_ci /* bail out if the upper three levels don't match */ 9458c2ecf20Sopenharmony_ci if (sgi_aff != affinity) 9468c2ecf20Sopenharmony_ci return -1; 9478c2ecf20Sopenharmony_ci 9488c2ecf20Sopenharmony_ci /* Is this VCPU's bit set in the mask ? */ 9498c2ecf20Sopenharmony_ci if (!(sgi_cpu_mask & BIT(level0))) 9508c2ecf20Sopenharmony_ci return -1; 9518c2ecf20Sopenharmony_ci 9528c2ecf20Sopenharmony_ci return level0; 9538c2ecf20Sopenharmony_ci} 9548c2ecf20Sopenharmony_ci 9558c2ecf20Sopenharmony_ci/* 9568c2ecf20Sopenharmony_ci * The ICC_SGI* registers encode the affinity differently from the MPIDR, 9578c2ecf20Sopenharmony_ci * so provide a wrapper to use the existing defines to isolate a certain 9588c2ecf20Sopenharmony_ci * affinity level. 9598c2ecf20Sopenharmony_ci */ 9608c2ecf20Sopenharmony_ci#define SGI_AFFINITY_LEVEL(reg, level) \ 9618c2ecf20Sopenharmony_ci ((((reg) & ICC_SGI1R_AFFINITY_## level ##_MASK) \ 9628c2ecf20Sopenharmony_ci >> ICC_SGI1R_AFFINITY_## level ##_SHIFT) << MPIDR_LEVEL_SHIFT(level)) 9638c2ecf20Sopenharmony_ci 9648c2ecf20Sopenharmony_ci/** 9658c2ecf20Sopenharmony_ci * vgic_v3_dispatch_sgi - handle SGI requests from VCPUs 9668c2ecf20Sopenharmony_ci * @vcpu: The VCPU requesting a SGI 9678c2ecf20Sopenharmony_ci * @reg: The value written into ICC_{ASGI1,SGI0,SGI1}R by that VCPU 9688c2ecf20Sopenharmony_ci * @allow_group1: Does the sysreg access allow generation of G1 SGIs 9698c2ecf20Sopenharmony_ci * 9708c2ecf20Sopenharmony_ci * With GICv3 (and ARE=1) CPUs trigger SGIs by writing to a system register. 9718c2ecf20Sopenharmony_ci * This will trap in sys_regs.c and call this function. 9728c2ecf20Sopenharmony_ci * This ICC_SGI1R_EL1 register contains the upper three affinity levels of the 9738c2ecf20Sopenharmony_ci * target processors as well as a bitmask of 16 Aff0 CPUs. 9748c2ecf20Sopenharmony_ci * If the interrupt routing mode bit is not set, we iterate over all VCPUs to 9758c2ecf20Sopenharmony_ci * check for matching ones. If this bit is set, we signal all, but not the 9768c2ecf20Sopenharmony_ci * calling VCPU. 9778c2ecf20Sopenharmony_ci */ 9788c2ecf20Sopenharmony_civoid vgic_v3_dispatch_sgi(struct kvm_vcpu *vcpu, u64 reg, bool allow_group1) 9798c2ecf20Sopenharmony_ci{ 9808c2ecf20Sopenharmony_ci struct kvm *kvm = vcpu->kvm; 9818c2ecf20Sopenharmony_ci struct kvm_vcpu *c_vcpu; 9828c2ecf20Sopenharmony_ci u16 target_cpus; 9838c2ecf20Sopenharmony_ci u64 mpidr; 9848c2ecf20Sopenharmony_ci int sgi, c; 9858c2ecf20Sopenharmony_ci int vcpu_id = vcpu->vcpu_id; 9868c2ecf20Sopenharmony_ci bool broadcast; 9878c2ecf20Sopenharmony_ci unsigned long flags; 9888c2ecf20Sopenharmony_ci 9898c2ecf20Sopenharmony_ci sgi = (reg & ICC_SGI1R_SGI_ID_MASK) >> ICC_SGI1R_SGI_ID_SHIFT; 9908c2ecf20Sopenharmony_ci broadcast = reg & BIT_ULL(ICC_SGI1R_IRQ_ROUTING_MODE_BIT); 9918c2ecf20Sopenharmony_ci target_cpus = (reg & ICC_SGI1R_TARGET_LIST_MASK) >> ICC_SGI1R_TARGET_LIST_SHIFT; 9928c2ecf20Sopenharmony_ci mpidr = SGI_AFFINITY_LEVEL(reg, 3); 9938c2ecf20Sopenharmony_ci mpidr |= SGI_AFFINITY_LEVEL(reg, 2); 9948c2ecf20Sopenharmony_ci mpidr |= SGI_AFFINITY_LEVEL(reg, 1); 9958c2ecf20Sopenharmony_ci 9968c2ecf20Sopenharmony_ci /* 9978c2ecf20Sopenharmony_ci * We iterate over all VCPUs to find the MPIDRs matching the request. 9988c2ecf20Sopenharmony_ci * If we have handled one CPU, we clear its bit to detect early 9998c2ecf20Sopenharmony_ci * if we are already finished. This avoids iterating through all 10008c2ecf20Sopenharmony_ci * VCPUs when most of the times we just signal a single VCPU. 10018c2ecf20Sopenharmony_ci */ 10028c2ecf20Sopenharmony_ci kvm_for_each_vcpu(c, c_vcpu, kvm) { 10038c2ecf20Sopenharmony_ci struct vgic_irq *irq; 10048c2ecf20Sopenharmony_ci 10058c2ecf20Sopenharmony_ci /* Exit early if we have dealt with all requested CPUs */ 10068c2ecf20Sopenharmony_ci if (!broadcast && target_cpus == 0) 10078c2ecf20Sopenharmony_ci break; 10088c2ecf20Sopenharmony_ci 10098c2ecf20Sopenharmony_ci /* Don't signal the calling VCPU */ 10108c2ecf20Sopenharmony_ci if (broadcast && c == vcpu_id) 10118c2ecf20Sopenharmony_ci continue; 10128c2ecf20Sopenharmony_ci 10138c2ecf20Sopenharmony_ci if (!broadcast) { 10148c2ecf20Sopenharmony_ci int level0; 10158c2ecf20Sopenharmony_ci 10168c2ecf20Sopenharmony_ci level0 = match_mpidr(mpidr, target_cpus, c_vcpu); 10178c2ecf20Sopenharmony_ci if (level0 == -1) 10188c2ecf20Sopenharmony_ci continue; 10198c2ecf20Sopenharmony_ci 10208c2ecf20Sopenharmony_ci /* remove this matching VCPU from the mask */ 10218c2ecf20Sopenharmony_ci target_cpus &= ~BIT(level0); 10228c2ecf20Sopenharmony_ci } 10238c2ecf20Sopenharmony_ci 10248c2ecf20Sopenharmony_ci irq = vgic_get_irq(vcpu->kvm, c_vcpu, sgi); 10258c2ecf20Sopenharmony_ci 10268c2ecf20Sopenharmony_ci raw_spin_lock_irqsave(&irq->irq_lock, flags); 10278c2ecf20Sopenharmony_ci 10288c2ecf20Sopenharmony_ci /* 10298c2ecf20Sopenharmony_ci * An access targeting Group0 SGIs can only generate 10308c2ecf20Sopenharmony_ci * those, while an access targeting Group1 SGIs can 10318c2ecf20Sopenharmony_ci * generate interrupts of either group. 10328c2ecf20Sopenharmony_ci */ 10338c2ecf20Sopenharmony_ci if (!irq->group || allow_group1) { 10348c2ecf20Sopenharmony_ci if (!irq->hw) { 10358c2ecf20Sopenharmony_ci irq->pending_latch = true; 10368c2ecf20Sopenharmony_ci vgic_queue_irq_unlock(vcpu->kvm, irq, flags); 10378c2ecf20Sopenharmony_ci } else { 10388c2ecf20Sopenharmony_ci /* HW SGI? Ask the GIC to inject it */ 10398c2ecf20Sopenharmony_ci int err; 10408c2ecf20Sopenharmony_ci err = irq_set_irqchip_state(irq->host_irq, 10418c2ecf20Sopenharmony_ci IRQCHIP_STATE_PENDING, 10428c2ecf20Sopenharmony_ci true); 10438c2ecf20Sopenharmony_ci WARN_RATELIMIT(err, "IRQ %d", irq->host_irq); 10448c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&irq->irq_lock, flags); 10458c2ecf20Sopenharmony_ci } 10468c2ecf20Sopenharmony_ci } else { 10478c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&irq->irq_lock, flags); 10488c2ecf20Sopenharmony_ci } 10498c2ecf20Sopenharmony_ci 10508c2ecf20Sopenharmony_ci vgic_put_irq(vcpu->kvm, irq); 10518c2ecf20Sopenharmony_ci } 10528c2ecf20Sopenharmony_ci} 10538c2ecf20Sopenharmony_ci 10548c2ecf20Sopenharmony_ciint vgic_v3_dist_uaccess(struct kvm_vcpu *vcpu, bool is_write, 10558c2ecf20Sopenharmony_ci int offset, u32 *val) 10568c2ecf20Sopenharmony_ci{ 10578c2ecf20Sopenharmony_ci struct vgic_io_device dev = { 10588c2ecf20Sopenharmony_ci .regions = vgic_v3_dist_registers, 10598c2ecf20Sopenharmony_ci .nr_regions = ARRAY_SIZE(vgic_v3_dist_registers), 10608c2ecf20Sopenharmony_ci }; 10618c2ecf20Sopenharmony_ci 10628c2ecf20Sopenharmony_ci return vgic_uaccess(vcpu, &dev, is_write, offset, val); 10638c2ecf20Sopenharmony_ci} 10648c2ecf20Sopenharmony_ci 10658c2ecf20Sopenharmony_ciint vgic_v3_redist_uaccess(struct kvm_vcpu *vcpu, bool is_write, 10668c2ecf20Sopenharmony_ci int offset, u32 *val) 10678c2ecf20Sopenharmony_ci{ 10688c2ecf20Sopenharmony_ci struct vgic_io_device rd_dev = { 10698c2ecf20Sopenharmony_ci .regions = vgic_v3_rd_registers, 10708c2ecf20Sopenharmony_ci .nr_regions = ARRAY_SIZE(vgic_v3_rd_registers), 10718c2ecf20Sopenharmony_ci }; 10728c2ecf20Sopenharmony_ci 10738c2ecf20Sopenharmony_ci return vgic_uaccess(vcpu, &rd_dev, is_write, offset, val); 10748c2ecf20Sopenharmony_ci} 10758c2ecf20Sopenharmony_ci 10768c2ecf20Sopenharmony_ciint vgic_v3_line_level_info_uaccess(struct kvm_vcpu *vcpu, bool is_write, 10778c2ecf20Sopenharmony_ci u32 intid, u64 *val) 10788c2ecf20Sopenharmony_ci{ 10798c2ecf20Sopenharmony_ci if (intid % 32) 10808c2ecf20Sopenharmony_ci return -EINVAL; 10818c2ecf20Sopenharmony_ci 10828c2ecf20Sopenharmony_ci if (is_write) 10838c2ecf20Sopenharmony_ci vgic_write_irq_line_level_info(vcpu, intid, *val); 10848c2ecf20Sopenharmony_ci else 10858c2ecf20Sopenharmony_ci *val = vgic_read_irq_line_level_info(vcpu, intid); 10868c2ecf20Sopenharmony_ci 10878c2ecf20Sopenharmony_ci return 0; 10888c2ecf20Sopenharmony_ci} 1089