[kvm-unit-tests PATCH v3 8/9] riscv: Capture IMSIC addresses at boot time
Xiang W
wangxiang at iscas.ac.cn
Tue Nov 24 17:44:08 PST 2026
Tests may want to use the IMSICs for MSIs and the upcoming BRS tests
want to confirm IMSICs are present. Provide those tests the S-mode
interrupt file addresses at boot time. Parse DT IMSIC nodes from
cpu_set_fdt() via imsic_init(). On ACPI, copy imsic_addr from the
MADT RINTC entry only when an IMSIC structure is present; ACPI
requires OSPM to ignore RINTC.imsic_addr otherwise.
Co-authored-by: Andrew Jones <andrew.jones at linux.dev>
Signed-off-by: Andrew Jones <andrew.jones at linux.dev>
Signed-off-by: Xiang W <wangxiang at iscas.ac.cn>
---
lib/riscv/asm/imsic.h | 8 ++++
lib/riscv/asm/processor.h | 1 +
lib/riscv/imsic.c | 86 +++++++++++++++++++++++++++++++++++++++
lib/riscv/setup.c | 13 ++++++
riscv/Makefile | 1 +
5 files changed, 109 insertions(+)
create mode 100644 lib/riscv/asm/imsic.h
create mode 100644 lib/riscv/imsic.c
diff --git a/lib/riscv/asm/imsic.h b/lib/riscv/asm/imsic.h
new file mode 100644
index 00000000..c011b106
--- /dev/null
+++ b/lib/riscv/asm/imsic.h
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASMRISCV_IMSIC_H_
+#define _ASMRISCV_IMSIC_H_
+#include <asm/processor.h>
+
+void imsic_init(struct thread_info *ti);
+
+#endif /* _ASMRISCV_IMSIC_H_ */
diff --git a/lib/riscv/asm/processor.h b/lib/riscv/asm/processor.h
index 5a1df355..fa0c7356 100644
--- a/lib/riscv/asm/processor.h
+++ b/lib/riscv/asm/processor.h
@@ -16,6 +16,7 @@ struct thread_info {
unsigned long hartid;
unsigned long uid;
unsigned long isa[1];
+ phys_addr_t imsic_pa; /* S-level interrupt file, INVALID_PHYS_ADDR if none */
unsigned long sp;
exception_fn exception_handlers[EXCEPTION_CAUSE_MAX];
exception_fn interrupt_handlers[INTERRUPT_CAUSE_MAX];
diff --git a/lib/riscv/imsic.c b/lib/riscv/imsic.c
new file mode 100644
index 00000000..38665c10
--- /dev/null
+++ b/lib/riscv/imsic.c
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * IMSIC support
+ */
+#include <libcflat.h>
+#include <bitops.h>
+#include <devicetree.h>
+#include <asm/imsic.h>
+#include <asm/processor.h>
+
+struct imsic_info {
+ unsigned long hartid;
+ phys_addr_t imsic_pa;
+};
+
+static void imsic_parse_fdt(int cpu_node, u64 hartid, void *data)
+{
+ struct imsic_info *info = (struct imsic_info *)data;
+ const void *fdt = dt_fdt();
+ int intc, node = -1;
+ u32 phandle;
+
+ if (info->hartid != hartid)
+ return;
+
+ info->imsic_pa = INVALID_PHYS_ADDR;
+
+ intc = fdt_subnode_offset(fdt, cpu_node, "interrupt-controller");
+ if (intc < 0)
+ return;
+
+ phandle = fdt_get_phandle(fdt, intc);
+
+ while ((node = fdt_node_offset_by_compatible(fdt, node, "riscv,imsics")) >= 0) {
+ struct dt_pbus_reg reg;
+ const fdt32_t *prop;
+ u64 stride, off;
+ u32 guest_bits;
+ int len, i;
+
+ prop = fdt_getprop(fdt, node, "interrupts-extended", &len);
+ if (!prop)
+ continue;
+
+ /* <phandle irq> pairs, since cpu-intc has #interrupt-cells = 1 */
+ for (i = 0; i + 1 < len / (int)sizeof(fdt32_t); i += 2) {
+ if (fdt32_to_cpu(prop[i]) == phandle &&
+ fdt32_to_cpu(prop[i + 1]) == IRQ_S_EXT)
+ break;
+ }
+ if (i + 1 >= len / (int)sizeof(fdt32_t))
+ continue;
+
+ prop = fdt_getprop(fdt, node, "riscv,guest-index-bits", &len);
+ guest_bits = prop && len == sizeof(fdt32_t) ? fdt32_to_cpu(*prop) : 0;
+ stride = BIT_ULL(guest_bits) * SZ_4K;
+ off = (i / 2) * stride;
+
+ for (i = 0; dt_pbus_translate_node(node, i, ®) == 0; i++) {
+ if (off < reg.size) {
+ assert(reg.size - off >= SZ_4K);
+ info->imsic_pa = reg.addr + off;
+ return;
+ }
+ off -= ALIGN(reg.size, stride);
+ }
+ assert_msg(false, "IMSIC interrupt file beyond reg regions");
+ }
+}
+
+void imsic_init(struct thread_info *ti)
+{
+ struct imsic_info info = {
+ .hartid = ti->hartid,
+ };
+ int ret;
+
+ if (dt_available()) {
+ ret = dt_for_each_cpu_node(imsic_parse_fdt, &info);
+ assert(ret == 0);
+ } else {
+ assert_msg(false, "ACPI not available");
+ }
+
+ ti->imsic_pa = info.imsic_pa;
+}
diff --git a/lib/riscv/setup.c b/lib/riscv/setup.c
index 547851f5..e9563ad4 100644
--- a/lib/riscv/setup.c
+++ b/lib/riscv/setup.c
@@ -16,6 +16,7 @@
#include <on-cpus.h>
#include <vmalloc.h>
#include <asm/csr.h>
+#include <asm/imsic.h>
#include <asm/isa.h>
#include <asm/mmu.h>
#include <asm/page.h>
@@ -52,6 +53,14 @@ static struct mem_region riscv_mem_regions[NR_MEM_REGIONS + 1];
#ifdef CONFIG_EFI
#include <acpi.h>
+static bool has_imsic = false;
+
+static int acpi_find_imsic(struct acpi_subtable_header *header)
+{
+ has_imsic = true;
+ return 0;
+}
+
static int cpu_set_acpi(struct acpi_subtable_header *header)
{
int cpu = nr_cpus++;
@@ -62,6 +71,8 @@ static int cpu_set_acpi(struct acpi_subtable_header *header)
cpus[cpu].cpu = cpu;
cpus[cpu].hartid = intc->hart_id;
cpus[cpu].uid = intc->uid;
+ /* RINTC.imsic_addr is ignored unless an IMSIC MADT structure is present. */
+ cpus[cpu].imsic_pa = has_imsic ? intc->imsic_addr : INVALID_PHYS_ADDR;
isa_init(&cpus[cpu]);
if (intc->flags & ACPI_MADT_ENABLED) {
@@ -81,6 +92,7 @@ static void cpu_set_fdt(int fdtnode __unused, u64 regval, void *info __unused)
cpus[cpu].cpu = cpu;
cpus[cpu].hartid = regval;
+ imsic_init(&cpus[cpu]);
isa_init(&cpus[cpu]);
if (!sbi_hart_get_status(cpus[cpu].hartid).error)
@@ -90,6 +102,7 @@ static void cpu_set_fdt(int fdtnode __unused, u64 regval, void *info __unused)
static void cpu_init_acpi(void)
{
#ifdef CONFIG_EFI
+ acpi_table_parse_madt(ACPI_MADT_TYPE_IMSIC, acpi_find_imsic);
acpi_table_parse_madt(ACPI_MADT_TYPE_RINTC, cpu_set_acpi);
#else
assert_msg(false, "ACPI not available");
diff --git a/riscv/Makefile b/riscv/Makefile
index 348a30a7..f90992ab 100644
--- a/riscv/Makefile
+++ b/riscv/Makefile
@@ -39,6 +39,7 @@ cflatobjs += lib/on-cpus.o
cflatobjs += lib/vmalloc.o
cflatobjs += lib/riscv/bitops.o
cflatobjs += lib/riscv/delay.o
+cflatobjs += lib/riscv/imsic.o
cflatobjs += lib/riscv/io.o
cflatobjs += lib/riscv/isa.o
cflatobjs += lib/riscv/mmu.o
--
2.47.3
More information about the kvm-riscv
mailing list