All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Jones <andrew.jones@linux.dev>
To: kvm-riscv@lists.infradead.org
Cc: Xiang W <wangxiang@iscas.ac.cn>
Subject: [kvm-unit-tests PATCH] riscv: Capture IMSIC addresses at boot time
Date: Fri, 20 Nov 2026 14:56:13 +0100	[thread overview]
Message-ID: <20261120135613.1388647-1-andrew.jones@linux.dev> (raw)

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 by parsing DT and capturing them at boot time.

Cc: Xiang W <wangxiang@iscas.ac.cn>
Signed-off-by: Andrew Jones <andrew.jones@linux.dev>
---
 lib/riscv/asm/imsic.h     |  8 ++++
 lib/riscv/asm/processor.h |  1 +
 lib/riscv/imsic.c         | 86 +++++++++++++++++++++++++++++++++++++++
 lib/riscv/setup.c         |  2 +
 riscv/Makefile            |  1 +
 5 files changed, 98 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 000000000000..c011b106da8c
--- /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 a3dab064691c..2c261805d181 100644
--- a/lib/riscv/asm/processor.h
+++ b/lib/riscv/asm/processor.h
@@ -15,6 +15,7 @@ struct thread_info {
 	int cpu;
 	unsigned long hartid;
 	unsigned long isa[1];
+	phys_addr_t imsic_addr;		/* 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 000000000000..f3e7361a79c7
--- /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_addr;
+};
+
+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_addr = 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, &reg) == 0; i++) {
+			if (off < reg.size) {
+				assert(reg.size - off >= SZ_4K);
+				info->imsic_addr = 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_addr = info.imsic_addr;
+}
diff --git a/lib/riscv/setup.c b/lib/riscv/setup.c
index a30aa0c4cb80..99acb550cf32 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/mmu.h>
 #include <asm/page.h>
 #include <asm/processor.h>
@@ -56,6 +57,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]);
 
 	if (!sbi_hart_get_status(cpus[cpu].hartid).error)
 		set_cpu_present(cpu, true);
diff --git a/riscv/Makefile b/riscv/Makefile
index 348a30a77df9..f90992ab0b71 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.56.0


-- 
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv

                 reply	other threads:[~2026-11-20 13:56 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120135613.1388647-1-andrew.jones@linux.dev \
    --to=andrew.jones@linux.dev \
    --cc=kvm-riscv@lists.infradead.org \
    --cc=wangxiang@iscas.ac.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.