[PATCH V2 4/4] RISC-V: KVM: AIA: Use per-HART IMSIC guest files to compute final HGEI count

From: guoren

Date: Fri Apr 24 2026 - 21:01:40 EST


From: "Guo Ren (Alibaba DAMO Academy)" <guoren@xxxxxxxxxx>

Now that `nr_guest_files` has been moved to `struct imsic_local_config`
and is computed per-HART, KVM must respect the actual number of guest
interrupt files available on each HART when setting up HGEI.

In `kvm_riscv_aia_enable()`:

- Retrieve the per-CPU IMSIC local config
- Take `min(hgctrl->nr_hgei, lc->nr_guest_files)` as the final usable
HGEI count for this HART
- Use the result to initialize `free_bitmap`

This ensures correct HGEI allocation on heterogeneous RISC-V SoCs
where different cores may have different IMSIC guest file counts, and
completes the per-HART conversion series.

Signed-off-by: Guo Ren (Alibaba DAMO Academy) <guoren@xxxxxxxxxx>
---
arch/riscv/kvm/aia.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/arch/riscv/kvm/aia.c b/arch/riscv/kvm/aia.c
index 70ff1d25dd99..a5f4b7fe1dce 100644
--- a/arch/riscv/kvm/aia.c
+++ b/arch/riscv/kvm/aia.c
@@ -534,6 +534,9 @@ static void aia_hgei_exit(void)
void kvm_riscv_aia_enable(void)
{
struct aia_hgei_control *hgctrl;
+ const struct imsic_global_config *gc;
+ const struct imsic_local_config *lc;
+ unsigned int nr_hgei;

if (!kvm_riscv_aia_available())
return;
@@ -547,8 +550,13 @@ void kvm_riscv_aia_enable(void)
if (hgctrl->nr_hgei)
hgctrl->nr_hgei--;

- if (hgctrl->nr_hgei) {
- hgctrl->free_bitmap = BIT(hgctrl->nr_hgei + 1) - 1;
+ gc = imsic_get_global_config();
+ lc = (gc) ? this_cpu_ptr(gc->local) : NULL;
+ if (lc)
+ nr_hgei = min(hgctrl->nr_hgei, lc->nr_guest_files);
+
+ if (nr_hgei) {
+ hgctrl->free_bitmap = BIT(nr_hgei + 1) - 1;
hgctrl->free_bitmap &= ~BIT(0);
} else {
hgctrl->free_bitmap = 0;
--
2.43.0