git: 3c236686822b - stable/13 - riscv: S-mode extension parsing

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Mon, 12 Jun 2023 13:50:24 UTC
The branch stable/13 has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=3c236686822bdf60a322e69f2a6ce0249f2eb8dc

commit 3c236686822bdf60a322e69f2a6ce0249f2eb8dc
Author:     Mitchell Horne <mhorne@FreeBSD.org>
AuthorDate: 2023-05-25 17:07:26 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2023-06-12 13:49:54 +0000

    riscv: S-mode extension parsing
    
    There are now several Supervisor-mode extensions that have entered the
    'ratified' status, so begin parsing and reporting a few of these.
    
    Recognize the following extensions:
     - Sstc: stimecmp/vstimecmp CSR
     - Svnapot: NAPOT* translation contiguity
     - Svpbmt: page-based memory types
     - Svinval: fine-grained TLB invalidation instructions
     - Sscofpmf: performance counter overflow
    
    *i.e. "naturally aligned power-of-2" page granularity
    
    For now, provide globals for Sstc and Sscofpmf, as we will make use of
    these in the near future.
    
    Plus, update the copyright statement after my recent work on this file.
    
    Reviewed by:    jhb
    MFC after:      2 weeks
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D40240
    
    (cherry picked from commit 8bebb786820f634d47522711b8cd56a66db3785d)
---
 sys/riscv/include/md_var.h |  4 ++++
 sys/riscv/riscv/identcpu.c | 53 +++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/sys/riscv/include/md_var.h b/sys/riscv/include/md_var.h
index 687ab9a3a77e..9951c838a2e6 100644
--- a/sys/riscv/include/md_var.h
+++ b/sys/riscv/include/md_var.h
@@ -42,6 +42,10 @@ extern register_t marchid;
 extern register_t mimpid;
 extern u_int mmu_caps;
 
+/* Supervisor-mode extension support */
+extern bool has_sstc;
+extern bool has_sscofpmf;
+
 struct dumperinfo;
 struct minidumpstate;
 
diff --git a/sys/riscv/riscv/identcpu.c b/sys/riscv/riscv/identcpu.c
index dafab6530024..a9a2542f70b6 100644
--- a/sys/riscv/riscv/identcpu.c
+++ b/sys/riscv/riscv/identcpu.c
@@ -1,6 +1,10 @@
 /*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
  * Copyright (c) 2015-2016 Ruslan Bukin <br@bsdpad.com>
  * All rights reserved.
+ * Copyright (c) 2022 Mitchell Horne <mhorne@FreeBSD.org>
+ * Copyright (c) 2023 The FreeBSD Foundation
  *
  * Portions of this software were developed by SRI International and the
  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
@@ -10,6 +14,9 @@
  * Computer Laboratory as part of the CTSRD Project, with support from the
  * UK Higher Education Innovation Fund (HEIF).
  *
+ * Portions of this software were developed by Mitchell Horne
+ * <mhorne@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
+ *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
@@ -67,11 +74,21 @@ register_t mimpid;	/* The implementation ID */
 
 u_int mmu_caps;
 
+/* Supervisor-mode extension support. */
+bool __read_frequently has_sstc;
+bool __read_frequently has_sscofpmf;
+
 struct cpu_desc {
 	const char	*cpu_mvendor_name;
 	const char	*cpu_march_name;
 	u_int		isa_extensions;		/* Single-letter extensions. */
 	u_int		mmu_caps;
+	u_int		smode_extensions;
+#define	 SV_SSTC	(1 << 0)
+#define	 SV_SVNAPOT	(1 << 1)
+#define	 SV_SVPBMT	(1 << 2)
+#define	 SV_SVINVAL	(1 << 3)
+#define	 SV_SSCOFPMF	(1 << 4)
 };
 
 struct cpu_desc cpu_desc[MAXCPU];
@@ -131,13 +148,29 @@ static const struct {
 #define	ISA_PREFIX_LEN		(sizeof(ISA_PREFIX) - 1)
 
 static __inline int
-parse_ext_s(struct cpu_desc *desc __unused, char *isa, int idx, int len)
+parse_ext_s(struct cpu_desc *desc, char *isa, int idx, int len)
 {
+#define	CHECK_S_EXT(str, flag)						\
+	do {								\
+		if (strncmp(&isa[idx], (str),				\
+		    MIN(strlen(str), len - idx)) == 0) {		\
+			desc->smode_extensions |= flag;			\
+			return (idx + strlen(str));			\
+		}							\
+	} while (0)
+
+	/* Check for known/supported extensions. */
+	CHECK_S_EXT("sstc",	SV_SSTC);
+	CHECK_S_EXT("svnapot",	SV_SVNAPOT);
+	CHECK_S_EXT("svpbmt",	SV_SVPBMT);
+	CHECK_S_EXT("svinval",	SV_SVINVAL);
+	CHECK_S_EXT("sscofpmf",	SV_SSCOFPMF);
+
+#undef CHECK_S_EXT
+
 	/*
 	 * Proceed to the next multi-letter extension or the end of the
 	 * string.
-	 *
-	 * TODO: parse these once we gain support
 	 */
 	while (isa[idx] != '_' && idx < len) {
 		idx++;
@@ -383,6 +416,10 @@ update_global_capabilities(u_int cpu, struct cpu_desc *desc)
 	 */
 	UPDATE_CAP(mmu_caps, desc->mmu_caps);
 
+	/* Supervisor-mode extension support. */
+	UPDATE_CAP(has_sstc, (desc->smode_extensions & SV_SSTC) != 0);
+	UPDATE_CAP(has_sscofpmf, (desc->smode_extensions & SV_SSCOFPMF) != 0);
+
 #undef UPDATE_CAP
 }
 
@@ -482,5 +519,15 @@ printcpuinfo(u_int cpu)
 		    "\15Mult/Div");
 	}
 
+	if (SHOULD_PRINT(smode_extensions)) {
+		printf("  S-mode Extensions: %#b\n", desc->smode_extensions,
+		    "\020"
+		    "\01Sstc"
+		    "\02Svnapot"
+		    "\03Svpbmt"
+		    "\04Svinval"
+		    "\05Sscofpmf");
+	}
+
 #undef SHOULD_PRINT
 }