From: kettenis Date: Fri, 2 Jul 2021 08:44:37 +0000 (+0000) Subject: Run SBI calls to to get mvendorid/marchid/mimplid on the actual CPU we're X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=843effc0a600fc43e5a702bdd4d9b9a56b06646d;p=openbsd Run SBI calls to to get mvendorid/marchid/mimplid on the actual CPU we're probing and decode mvendorid and marchid. ok mlarkin@, deraadt@, jsg@ --- diff --git a/sys/arch/riscv64/include/sbi.h b/sys/arch/riscv64/include/sbi.h index 70fd30f9a74..0cb68d797f8 100644 --- a/sys/arch/riscv64/include/sbi.h +++ b/sys/arch/riscv64/include/sbi.h @@ -1,4 +1,4 @@ -/* $OpenBSD: sbi.h,v 1.3 2021/06/27 15:02:25 kettenis Exp $ */ +/* $OpenBSD: sbi.h,v 1.4 2021/07/02 08:44:37 kettenis Exp $ */ /*- * Copyright (c) 2016-2017 Ruslan Bukin @@ -33,8 +33,6 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * - * $FreeBSD$ */ #ifndef _MACHINE_SBI_H_ @@ -134,6 +132,25 @@ extern u_long sbi_spec_version; extern u_long sbi_impl_id; extern u_long sbi_impl_version; +static __inline u_int +sbi_get_mvendorid(void) +{ + return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MVENDORID).value); +} + + +static __inline u_long +sbi_get_marchid(void) +{ + return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MARCHID).value); +} + +static __inline u_long +sbi_get_mimpid(void) +{ + return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MIMPID).value); +} + static __inline long sbi_probe_extension(long id) { diff --git a/sys/arch/riscv64/riscv64/cpu.c b/sys/arch/riscv64/riscv64/cpu.c index a6ca8927b46..2dca9889b3a 100644 --- a/sys/arch/riscv64/riscv64/cpu.c +++ b/sys/arch/riscv64/riscv64/cpu.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.c,v 1.9 2021/06/29 21:27:53 kettenis Exp $ */ +/* $OpenBSD: cpu.c,v 1.10 2021/07/02 08:44:37 kettenis Exp $ */ /* * Copyright (c) 2016 Dale Rahn @@ -33,7 +33,39 @@ #include #include -register_t mvendorid, marchid, mimpid; +/* CPU Identification */ + +#define CPU_VENDOR_SIFIVE 0x489 + +#define CPU_ARCH_U5 0x0000000000000001 +#define CPU_ARCH_U7 0x8000000000000007 + +/* Architectures */ +struct arch { + uint64_t id; + char *name; +}; + +struct arch cpu_arch_none[] = { + { 0, NULL } +}; + +struct arch cpu_arch_sifive[] = { + { CPU_ARCH_U5, "U5" }, + { CPU_ARCH_U7, "U7" }, + { 0, NULL } +}; + +/* Vendors */ +const struct vendor { + uint32_t id; + char *name; + struct arch *archlist; +} cpu_vendors[] = { + { CPU_VENDOR_SIFIVE, "SiFive", cpu_arch_sifive }, + { 0, NULL } +}; + char cpu_model[64]; int cpu_node; @@ -54,13 +86,43 @@ void cpu_identify(struct cpu_info *ci) { char isa[32]; - int len; + uint64_t marchid, mimpid; + uint32_t mvendorid; + const char *vendor_name = NULL; + const char *arch_name = NULL; + struct arch *archlist = cpu_arch_none; + int i, len; + + mvendorid = sbi_get_mvendorid(); + marchid = sbi_get_marchid(); + mimpid = sbi_get_mimpid(); + + for (i = 0; cpu_vendors[i].name; i++) { + if (mvendorid == cpu_vendors[i].id) { + vendor_name = cpu_vendors[i].name; + archlist = cpu_vendors[i].archlist; + break; + } + } - len = OF_getprop(ci->ci_node, "riscv,isa", isa, sizeof(isa)); + for (i = 0; archlist[i].name; i++) { + if (marchid == archlist[i].id) { + arch_name = archlist[i].name; + break; + } + } - printf(": vendor %lx arch %lx imp %lx", - mvendorid, marchid, mimpid); + if (vendor_name) + printf(": %s", vendor_name); + else + printf(": vendor %x", mvendorid); + if (arch_name) + printf(" %s", arch_name); + else + printf(" arch %llx", marchid); + printf(" imp %llx", mimpid); + len = OF_getprop(ci->ci_node, "riscv,isa", isa, sizeof(isa)); if (len != -1) { printf(" %s", isa); strlcpy(cpu_model, isa, sizeof(cpu_model)); diff --git a/sys/arch/riscv64/riscv64/sbi.c b/sys/arch/riscv64/riscv64/sbi.c index f5ddaa30ec9..8c85a708284 100644 --- a/sys/arch/riscv64/riscv64/sbi.c +++ b/sys/arch/riscv64/riscv64/sbi.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sbi.c,v 1.5 2021/06/27 15:02:25 kettenis Exp $ */ +/* $OpenBSD: sbi.c,v 1.6 2021/07/02 08:44:37 kettenis Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD @@ -43,10 +43,6 @@ u_long sbi_spec_version; u_long sbi_impl_id; u_long sbi_impl_version; -extern register_t mvendorid; -extern register_t mimpid; -extern register_t marchid; - static struct sbi_ret sbi_get_spec_version(void) { @@ -65,25 +61,6 @@ sbi_get_impl_version(void) return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_IMPL_VERSION)); } -static struct sbi_ret -sbi_get_mvendorid(void) -{ - return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MVENDORID)); -} - - -static struct sbi_ret -sbi_get_marchid(void) -{ - return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MARCHID)); -} - -static struct sbi_ret -sbi_get_mimpid(void) -{ - return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MIMPID)); -} - void sbi_print_version(void) { @@ -168,11 +145,6 @@ sbi_init(void) sbi_impl_id = sbi_get_impl_id().value; sbi_impl_version = sbi_get_impl_version().value; - /* Set the hardware implementation info. */ - mvendorid = sbi_get_mvendorid().value; - marchid = sbi_get_marchid().value; - mimpid = sbi_get_mimpid().value; - /* * Probe for legacy extensions. Currently we rely on all of them * to be implemented, but this is not guaranteed by the spec.