git: 608eb81477af - main - riscv: Apply the T-HEAD errata only to early cores

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Thu, 26 Nov 2026 16:23:03 UTC
The branch main has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=608eb81477af2993282add5e39a6760ea9da6712

commit 608eb81477af2993282add5e39a6760ea9da6712
Author:     Han Gao <gaohan@iscas.ac.cn>
AuthorDate: 2026-10-28 17:04:56 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2026-11-26 16:22:40 +0000

    riscv: Apply the T-HEAD errata only to early cores
    
    The XTheadMae page attributes and the vendor cache operations are needed
    by the early T-HEAD cores (C906, C910, C920), which report zero marchid
    and mimpid. Later cores such as the C908 and C920v2 report non-zero
    values and implement the standard Svpbmt and Zicbom extensions instead.
    Setting the T-HEAD memory attribute bits there makes the early page
    tables use reserved PBMT encodings.
    
    Check marchid and mimpid before applying the quirks, both in
    handle_thead_quirks() and in the early PTE setup in locore.
    
    Signed-off-by:  Han Gao <gaohan@iscas.ac.cn>
    Reviewed by:    mhorne
    MFC after:      1 week
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/2522
---
 sys/riscv/riscv/identcpu.c | 13 +++++++------
 sys/riscv/riscv/locore.S   |  5 +++++
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/sys/riscv/riscv/identcpu.c b/sys/riscv/riscv/identcpu.c
index 486176dc6333..5cdd5b883997 100644
--- a/sys/riscv/riscv/identcpu.c
+++ b/sys/riscv/riscv/identcpu.c
@@ -590,16 +590,17 @@ handle_thead_quirks(u_int cpu, struct cpu_desc *desc)
 		return;
 
 	/*
-	 * For now, it is assumed that T-HEAD CPUs have both marchid and mimpid
-	 * values of zero (although we leave this unchecked). It is true in
-	 * practice for the early generations of this hardware (C906, C910,
-	 * C920). In the future, the identity checks may need to become more
-	 * granular, but until then all known T-HEAD quirks are applied
-	 * indiscriminantly.
+	 * The early generations of this hardware (C906, C910, C920) report
+	 * marchid and mimpid values of zero and need the XTheadMae page
+	 * attributes and the vendor cache operations. Later cores, such as
+	 * the C908 and C920v2, report non-zero values and implement the
+	 * standard Svpbmt and Zicbom extensions instead.
 	 *
 	 * Note: any changes in this function relating to has_errata_thead_pbmt
 	 * may need to be applied to get_pte_fixup_bits (in locore.S) as well.
 	 */
+	if (marchid != 0 || mimpid != 0)
+		return;
 
 	has_errata_thead_pbmt = true;
 	thead_setup_cache();
diff --git a/sys/riscv/riscv/locore.S b/sys/riscv/riscv/locore.S
index b4ab4aa43ff8..792a49e6d4b4 100644
--- a/sys/riscv/riscv/locore.S
+++ b/sys/riscv/riscv/locore.S
@@ -310,6 +310,11 @@ get_pte_fixup_bits:
 	li	t0, MVENDORID_THEAD
 	xor	t0, t0, a1
 	bnez	t0, 1f		/* branch if a1 != t0 */
+	/* Only early T-HEAD cores, with zero marchid and mimpid, need this. */
+	SBI_CALL(SBI_EXT_ID_BASE, SBI_BASE_GET_MARCHID)
+	bnez	a1, 1f
+	SBI_CALL(SBI_EXT_ID_BASE, SBI_BASE_GET_MIMPID)
+	bnez	a1, 1f
 	li	s8, PTE_THEAD_MA_NONE
 1:
 	ret