AIA provides a hardware-accelerated mechanism for delivering external interrupts to domains via "guest interrupt files" located in IMSIC. A single physical hart can implement multiple such files (up to GEILEN), allowing several virtual harts to receive interrupts directly from hardware
Introduce per-CPU tracking of guest interrupt file identifiers (VGEIN) for systems implementing AIA specification. Each CPU maintains a bitmap describing which guest interrupt files are currently in use. Add helpers to initialize the bitmap based on the number of available guest interrupt files (GEILEN), assign a VGEIN to a vCPU, and release it when no longer needed. When assigning a VGEIN, the corresponding value is written to the VGEIN field of the guest hstatus register so that VS-level external interrupts are delivered from the selected interrupt file. Signed-off-by: Oleksii Kurochko <[email protected]> --- xen/arch/riscv/aia.c | 112 ++++++++++++++++++++++++++++++- xen/arch/riscv/include/asm/aia.h | 18 +++++ 2 files changed, 129 insertions(+), 1 deletion(-) diff --git a/xen/arch/riscv/aia.c b/xen/arch/riscv/aia.c index 5e3f190e8e2c..7bd66d1e37c6 100644 --- a/xen/arch/riscv/aia.c +++ b/xen/arch/riscv/aia.c @@ -1,11 +1,24 @@ /* SPDX-License-Identifier: GPL-2.0-only */ +#include <xen/bitmap.h> #include <xen/errno.h> #include <xen/init.h> #include <xen/sections.h> +#include <xen/sched.h> +#include <xen/spinlock.h> #include <xen/types.h> +#include <xen/xvmalloc.h> +#include <asm/aia.h> #include <asm/cpufeature.h> +#include <asm/csr.h> +#include <asm/current.h> + +/* + * Bitmap for each physical cpus to detect which VS (guest) + * interrupt file id was used. + */ +DEFINE_PER_CPU(struct vgein_bmp, vgein_bmp); static bool __ro_after_init is_aia_available; @@ -14,12 +27,109 @@ bool aia_available(void) return is_aia_available; } +int __init vgein_init(unsigned int cpu) +{ + struct vgein_bmp *vgein = &per_cpu(vgein_bmp, cpu); + + csr_write(CSR_HGEIE, -1UL); + vgein->geilen = flsl(csr_read(CSR_HGEIE)); + csr_write(CSR_HGEIE, 0); + if ( vgein->geilen ) + vgein->geilen--; + + BUG_ON(!vgein->geilen); + + printk("cpu%d.geilen=%d\n", cpu, vgein->geilen); + + if ( !vgein->bmp ) + { + vgein->bmp = xvzalloc_array(unsigned long, BITS_TO_LONGS(vgein->geilen)); + if ( !vgein->bmp ) + return -ENOMEM; + } + + spin_lock_init(&vgein->lock); + + return 0; +} + int __init aia_init(void) { + int rc = 0; + if ( !riscv_isa_extension_available(NULL, RISCV_ISA_EXT_ssaia) ) return -ENODEV; + if ( (rc = vgein_init(0)) ) + return rc; + is_aia_available = true; - return 0; + return rc; +} + +unsigned int vgein_assign(struct vcpu *v) +{ + unsigned int vgein_id; + + struct vgein_bmp *vgein_bmp = &per_cpu(vgein_bmp, v->processor); + unsigned long *bmp = vgein_bmp->bmp; + unsigned long flags; + + spin_lock_irqsave(&vgein_bmp->lock, flags); + vgein_id = bitmap_weight(bmp, vgein_bmp->geilen); + + /* + * All vCPU guest interrupt files are used and we don't support a case + * when number of vCPU on 1 pCPU is bigger then geilen. + */ + ASSERT(vgein_id < vgein_bmp->geilen); + + bitmap_set(bmp, vgein_id, 1); + spin_unlock_irqrestore(&vgein_bmp->lock, flags); + + /* + * The vgein_id shouldn't be zero, as it will indicate that no guest + * external interrupt source is selected for VS-level external interrupts + * according to RISC-V priviliged spec: + * 8.2.1 Hypervisor Status Register (hstatus) in RISC-V priviliged spec: + * + * The VGEIN (Virtual Guest External Interrupt Number) field selects + * a guest external interrupt source for VS-level external interrupts. + * VGEIN is a WLRL field that must be able to hold values between zero + * and the maximum guest external interrupt number (known as GEILEN), + * inclusive. + * When VGEIN=0, no guest external interrupt source is selected for + * VS-level external interrupts. + */ + vgein_id++; + +#ifdef VGEIN_DEBUG + printk("%s: %pv: vgein_id(%u), xen_cpu%d_bmp=%#lx\n", + __func__, v, vgein_id, v->processor, *bmp); +#endif + + vcpu_guest_cpu_user_regs(v)->hstatus &= ~HSTATUS_VGEIN; + vcpu_guest_cpu_user_regs(v)->hstatus |= + MASK_INSR(vgein_id, HSTATUS_VGEIN); + + return vgein_id; +} + +void vgein_release(struct vcpu *v, unsigned int vgen_id) +{ + unsigned long flags; + + struct vgein_bmp *vgein_bmp = &per_cpu(vgein_bmp, v->processor); + + spin_lock_irqsave(&vgein_bmp->lock, flags); + bitmap_clear(vgein_bmp->bmp, vgen_id - 1, 1); + spin_unlock_irqrestore(&vgein_bmp->lock, flags); + +#ifdef VGEIN_DEBUG + printk("%s: vgein_id(%u), xen_cpu%d_bmp=%#lx\n", + __func__, vgen_id, v->processor, *vgein_bmp->bmp); +#endif + + vcpu_guest_cpu_user_regs(v)->hstatus &= ~HSTATUS_VGEIN; } diff --git a/xen/arch/riscv/include/asm/aia.h b/xen/arch/riscv/include/asm/aia.h index 039607faf685..c2717504cbea 100644 --- a/xen/arch/riscv/include/asm/aia.h +++ b/xen/arch/riscv/include/asm/aia.h @@ -3,8 +3,26 @@ #ifndef ASM__RISCV__AIA_H #define ASM__RISCV__AIA_H +#include <xen/percpu.h> +#include <xen/spinlock.h> + +struct vcpu; + +struct vgein_bmp { + unsigned long *bmp; + spinlock_t lock; + struct vcpu *owners[BITS_PER_LONG]; + unsigned int geilen; +}; + +DECLARE_PER_CPU(struct vgein_bmp, vgein_bmp); + bool aia_available(void); int aia_init(void); +int vgein_init(unsigned int cpu); +unsigned int vgein_assign(struct vcpu *v); +void vgein_release(struct vcpu *v, unsigned int vgen_id); + #endif /* ASM__RISCV__ACPI_H */ -- 2.53.0
