On Sun, 2026-11-01 at 11:15 +0800, Tan Chi wrote:
> KVM AIA initialization uses the RISC-V NUMA helpers to query
> the socket count and per-socket hart topology.
> 
> However, KVM support can be enabled without CONFIG_RISCV_NUMA.
> In this configuration, kvm-cpu.c is built while numa.c is not,
> resulting in undefined references to riscv_socket_count(),
> riscv_socket_hart_count() and riscv_socket_first_hartid().
> 
> The virt machine already determines the hart topology when
> initializing its hart arrays. Pass the socket topology directly
> to KVM AIA instead of querying the NUMA helpers again.
> 
> This removes the build-time dependency on CONFIG_RISCV_NUMA
> without changing the IMSIC address configuration.
> 
> Signed-off-by: Tan Chi <[email protected]>

Reviewed-by: Alistair Francis <[email protected]>

Alistair

> ---
>  hw/riscv/aia.h               |  5 +++++
>  hw/riscv/virt.c              | 10 +++++++++-
>  target/riscv/kvm/kvm-cpu.c   | 10 +++++-----
>  target/riscv/kvm/kvm-stub.c  |  3 ++-
>  target/riscv/kvm/kvm_riscv.h |  5 ++++-
>  5 files changed, 25 insertions(+), 8 deletions(-)
> 
> diff --git a/hw/riscv/aia.h b/hw/riscv/aia.h
> index e4dcc5aae0..ed9b613f3c 100644
> --- a/hw/riscv/aia.h
> +++ b/hw/riscv/aia.h
> @@ -12,6 +12,11 @@
>  #include "exec/hwaddr.h"
>  #include "qemu/bitops.h"
>  
> +typedef struct RISCVAIASocket {
> +    uint32_t hartid_base;
> +    uint32_t num_harts;
> +} RISCVAIASocket;
> +
>  static inline uint32_t imsic_num_bits(uint32_t count)
>  {
>      uint32_t ret = 0;
> diff --git a/hw/riscv/virt.c b/hw/riscv/virt.c
> index 7ec7596b24..f1b1353647 100644
> --- a/hw/riscv/virt.c
> +++ b/hw/riscv/virt.c
> @@ -853,7 +853,15 @@ static void virt_machine_init(MachineState
> *machine)
>      }
>  
>      if (kvm_enabled() && virt_use_kvm_aia_aplic_imsic(s->aia_type))
> {
> -        kvm_riscv_aia_create(machine, IMSIC_MMIO_GROUP_MIN_SHIFT,
> +        g_autofree RISCVAIASocket *aia_sockets =
> +                g_new(RISCVAIASocket, socket_count);
> +        for (i = 0; i < socket_count; i++) {
> +            aia_sockets[i].hartid_base = s->soc[i].hartid_base;
> +            aia_sockets[i].num_harts = s->soc[i].num_harts;
> +        }
> +
> +        kvm_riscv_aia_create(aia_sockets, socket_count,
> +                             IMSIC_MMIO_GROUP_MIN_SHIFT,
>                               VIRT_IRQCHIP_NUM_SOURCES,
> VIRT_IRQCHIP_NUM_MSIS,
>                               s->memmap[VIRT_APLIC_S].base,
>                               s->memmap[VIRT_IMSIC_S].base,
> diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c
> index 45ad5e1525..1a74b9a37e 100644
> --- a/target/riscv/kvm/kvm-cpu.c
> +++ b/target/riscv/kvm/kvm-cpu.c
> @@ -46,7 +46,7 @@
>  #include "sbi_ecall_interface.h"
>  #include "chardev/char-fe.h"
>  #include "system/runstate.h"
> -#include "hw/riscv/numa.h"
> +#include "hw/riscv/aia.h"
>  
>  #define PR_RISCV_V_SET_CONTROL            69
>  #define PR_RISCV_V_VSTATE_CTRL_ON          2
> @@ -1897,7 +1897,8 @@ void kvm_arch_accel_class_init(ObjectClass *oc)
>                                      "auto");
>  }
>  
> -void kvm_riscv_aia_create(MachineState *machine, uint64_t
> group_shift,
> +void kvm_riscv_aia_create(const RISCVAIASocket *sockets,
> +                          uint32_t socket_count, uint64_t
> group_shift,
>                            uint64_t aia_irq_num, uint64_t
> aia_msi_num,
>                            uint64_t aplic_base, uint64_t imsic_base,
>                            uint64_t guest_num)
> @@ -1905,7 +1906,6 @@ void kvm_riscv_aia_create(MachineState
> *machine, uint64_t group_shift,
>      int ret, i;
>      int aia_fd = -1;
>      uint64_t default_aia_mode;
> -    uint64_t socket_count = riscv_socket_count(machine);
>      uint64_t max_hart_per_socket = 0;
>      uint64_t socket, base_hart, hart_count, socket_imsic_base,
> imsic_addr;
>      uint64_t socket_bits, hart_bits, guest_bits;
> @@ -2006,8 +2006,8 @@ void kvm_riscv_aia_create(MachineState
> *machine, uint64_t group_shift,
>  
>      for (socket = 0; socket < socket_count; socket++) {
>          socket_imsic_base = imsic_base + socket * (1U <<
> group_shift);
> -        hart_count = riscv_socket_hart_count(machine, socket);
> -        base_hart = riscv_socket_first_hartid(machine, socket);
> +        hart_count = sockets[socket].num_harts;
> +        base_hart = sockets[socket].hartid_base;
>  
>          if (max_hart_per_socket < hart_count) {
>              max_hart_per_socket = hart_count;
> diff --git a/target/riscv/kvm/kvm-stub.c b/target/riscv/kvm/kvm-
> stub.c
> index 64e39c96d8..f929161628 100644
> --- a/target/riscv/kvm/kvm-stub.c
> +++ b/target/riscv/kvm/kvm-stub.c
> @@ -9,7 +9,8 @@
>  #include "qemu/osdep.h"
>  #include "target/riscv/kvm/kvm_riscv.h"
>  
> -void kvm_riscv_aia_create(MachineState *machine, uint64_t
> group_shift,
> +void kvm_riscv_aia_create(const RISCVAIASocket *sockets,
> +                          uint32_t socket_count, uint64_t
> group_shift,
>                            uint64_t aia_irq_num, uint64_t
> aia_msi_num,
>                            uint64_t aplic_base, uint64_t imsic_base,
>                            uint64_t guest_num)
> diff --git a/target/riscv/kvm/kvm_riscv.h
> b/target/riscv/kvm/kvm_riscv.h
> index aef681f0a2..bad78e28f4 100644
> --- a/target/riscv/kvm/kvm_riscv.h
> +++ b/target/riscv/kvm/kvm_riscv.h
> @@ -21,6 +21,8 @@
>  
>  #include "target/riscv/cpu-qom.h"
>  
> +typedef struct RISCVAIASocket RISCVAIASocket;
> +
>  void kvm_riscv_reset_vcpu(RISCVCPU *cpu);
>  /**
>   * kvm_riscv_set_irq:
> @@ -29,7 +31,8 @@ void kvm_riscv_reset_vcpu(RISCVCPU *cpu);
>   * @level: %0 to clear the interrupt, non-zero to set it.
>   */
>  void kvm_riscv_set_irq(RISCVCPU *cpu, int irq, int level);
> -void kvm_riscv_aia_create(MachineState *machine, uint64_t
> group_shift,
> +void kvm_riscv_aia_create(const RISCVAIASocket *sockets,
> +                          uint32_t socket_count, uint64_t
> group_shift,
>                            uint64_t aia_irq_num, uint64_t
> aia_msi_num,
>                            uint64_t aplic_base, uint64_t imsic_base,
>                            uint64_t guest_num);

Reply via email to