From 5d2bddc8825e17cc05fcd1594016c46b0b8443ec Mon Sep 17 00:00:00 2001 From: heyujiao99 Date: Fri, 4 Jul 2025 10:21:14 +0800 Subject: [PATCH 1/2] Add RISC64 Timer Implemention.To ensure the timer works properly on the RISC-V platform. Signed-off-by: heyujiao99 --- config/opal_config_asm.m4 | 3 ++ opal/include/opal/sys/Makefile.am | 1 + opal/include/opal/sys/cma.h | 4 ++ opal/include/opal/sys/riscv64/Makefile.am | 25 ++++++++++++ opal/include/opal/sys/riscv64/timer.h | 40 ++++++++++++++++++++ opal/include/opal/sys/timer.h | 2 + opal/mca/timer/linux/configure.m4 | 2 +- opal/mca/timer/linux/timer_linux_component.c | 15 ++++++++ 8 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 opal/include/opal/sys/riscv64/Makefile.am create mode 100644 opal/include/opal/sys/riscv64/timer.h diff --git a/config/opal_config_asm.m4 b/config/opal_config_asm.m4 index 1a458e748f1..472959d4bf8 100644 --- a/config/opal_config_asm.m4 +++ b/config/opal_config_asm.m4 @@ -538,6 +538,9 @@ AC_DEFUN([OPAL_CHECK_INLINE_C_GCC],[ powerpc-*|powerpc64-*|powerpcle-*|powerpc64le-*|rs6000-*|ppc-*) opal_gcc_inline_assign='"1: li %0,0" : "=&r"(ret)' ;; + riscv64*) + opal_gcc_inline_assign='"li %0, 0" : "=r"(ret)' + ;; esac AS_IF([test "$opal_gcc_inline_assign" != ""], diff --git a/opal/include/opal/sys/Makefile.am b/opal/include/opal/sys/Makefile.am index b56e909b376..d73dc914f8c 100644 --- a/opal/include/opal/sys/Makefile.am +++ b/opal/include/opal/sys/Makefile.am @@ -42,4 +42,5 @@ headers += \ include opal/sys/x86_64/Makefile.am include opal/sys/arm64/Makefile.am include opal/sys/powerpc/Makefile.am +include opal/sys/riscv64/Makefile.am include opal/sys/gcc_builtin/Makefile.am diff --git a/opal/include/opal/sys/cma.h b/opal/include/opal/sys/cma.h index b1db5f7700c..12277e19fe2 100644 --- a/opal/include/opal/sys/cma.h +++ b/opal/include/opal/sys/cma.h @@ -59,6 +59,10 @@ # define __NR_process_vm_readv 270 # define __NR_process_vm_writev 271 +# elif defined(PLATFORM_ARCH_RISCV) +# define __NR_process_vm_readv 270 +# define __NR_process_vm_writev 271 + # else # error "Unsupported architecture for process_vm_readv and process_vm_writev syscalls" # endif diff --git a/opal/include/opal/sys/riscv64/Makefile.am b/opal/include/opal/sys/riscv64/Makefile.am new file mode 100644 index 00000000000..853d27d7824 --- /dev/null +++ b/opal/include/opal/sys/riscv64/Makefile.am @@ -0,0 +1,25 @@ +# +# Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana +# University Research and Technology +# Corporation. All rights reserved. +# Copyright (c) 2004-2005 The University of Tennessee and The University +# of Tennessee Research Foundation. All rights +# reserved. +# Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, +# University of Stuttgart. All rights reserved. +# Copyright (c) 2004-2005 The Regents of the University of California. +# All rights reserved. +# Copyright (c) 2017 Research Organization for Information Science +# and Technology (RIST). All rights reserved. +# $COPYRIGHT$ +# +# Additional copyrights may follow +# +# $HEADER$ +# + +# This makefile.am does not stand on its own - it is included from opal/include/Makefile.am + +headers += \ + opal/sys/riscv64/timer.h + diff --git a/opal/include/opal/sys/riscv64/timer.h b/opal/include/opal/sys/riscv64/timer.h new file mode 100644 index 00000000000..e2daa72cbf4 --- /dev/null +++ b/opal/include/opal/sys/riscv64/timer.h @@ -0,0 +1,40 @@ +/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */ +/* + * Copyright (c) 2008 The University of Tennessee and The University + * of Tennessee Research Foundation. All rights + * reserved. + * Copyright (c) 2016 Broadcom Limited. All rights reserved. + * Copyright (c) 2016 Los Alamos National Security, LLC. All rights + * reserved. + * Copyright (c) 2021 Google, LLC. All rights reserved. + * Copyright (c) 2022 Amazon.com, Inc. or its affiliates. + * All Rights reserved. + * $COPYRIGHT$ + * + * Additional copyrights may follow + * + * $HEADER$ + */ + +#ifndef OPAL_SYS_ARCH_TIMER_H +#define OPAL_SYS_ARCH_TIMER_H 1 + +typedef uint64_t opal_timer_t; + +#if OPAL_C_GCC_INLINE_ASSEMBLY + +static inline opal_timer_t opal_sys_timer_get_cycles(void) +{ + opal_timer_t ret; + __asm__ __volatile__("fence iorw, iorw" ::: "memory"); + __asm__ __volatile__("rdtime %0" : "=r"(ret)); + + return ret; +} + +#define OPAL_HAVE_SYS_TIMER_GET_CYCLES 1 + +#endif /* OPAL_C_GCC_INLINE_ASSEMBLY */ + +#endif /* ! OPAL_SYS_ARCH_TIMER_H */ + diff --git a/opal/include/opal/sys/timer.h b/opal/include/opal/sys/timer.h index 3f63839a48c..6f9191ab4bd 100644 --- a/opal/include/opal/sys/timer.h +++ b/opal/include/opal/sys/timer.h @@ -63,6 +63,8 @@ BEGIN_C_DECLS # include "opal/sys/arm64/timer.h" #elif defined(PLATFORM_ARCH_POWERPC) # include "opal/sys/powerpc/timer.h" +#elif defined(PLATFORM_ARCH_RISCV) +# include "opal/sys/riscv64/timer.h" #endif #ifndef DOXYGEN diff --git a/opal/mca/timer/linux/configure.m4 b/opal/mca/timer/linux/configure.m4 index 5ec7b013872..a2639eaf9c8 100644 --- a/opal/mca/timer/linux/configure.m4 +++ b/opal/mca/timer/linux/configure.m4 @@ -47,7 +47,7 @@ AC_DEFUN([MCA_opal_timer_linux_CONFIG],[ [timer_linux_happy="no"])]) case "${host}" in - i?86-*linux*|x86_64*linux*|ia64-*linux*|powerpc-*linux*|powerpc64-*linux*|powerpc64le-*linux*|powerpcle-*linux*|sparc*-*linux*|aarch64-*linux*) + i?86-*linux*|x86_64*linux*|ia64-*linux*|powerpc-*linux*|powerpc64-*linux*|powerpc64le-*linux*|powerpcle-*linux*|sparc*-*linux*|aarch64-*linux*|riscv64-*linux*) AS_IF([test "$timer_linux_happy" = "yes"], [AS_IF([test -r "/proc/cpuinfo"], [timer_linux_happy="yes"], diff --git a/opal/mca/timer/linux/timer_linux_component.c b/opal/mca/timer/linux/timer_linux_component.c index 6c615cd412e..0bd679b1f4d 100644 --- a/opal/mca/timer/linux/timer_linux_component.c +++ b/opal/mca/timer/linux/timer_linux_component.c @@ -29,6 +29,7 @@ #include #include +#include #include "opal/constants.h" #include "opal/mca/timer/base/base.h" @@ -177,6 +178,20 @@ static int opal_timer_linux_find_freq(void) } } +#if defined(PLATFORM_ARCH_RISCV) + if (0 == opal_timer_linux_freq) { + /* read timebase-frequency in device-tree */ + FILE *fp_rv = fopen("/proc/device-tree/cpus/timebase-frequency", "rb"); + if (NULL == fp_rv) { + return OPAL_ERR_IN_ERRNO; + } + if (1 == fread(&opal_timer_linux_freq, 4, 1, fp_rv)){ + opal_timer_linux_freq = ntohl(opal_timer_linux_freq); + } + fclose(fp_rv); + } +#endif + fclose(fp); /* convert the timer frequency to MHz to avoid an extra operation when From 54c1e769349773f9d10ac450986de19583f74bc1 Mon Sep 17 00:00:00 2001 From: heyujiao99 Date: Sat, 5 Jul 2025 10:19:44 +0800 Subject: [PATCH 2/2] use early-clobber in opal_config_asm.m4, remove irrelated code, and update copyright. Signed-off-by: heyujiao99 --- config/opal_config_asm.m4 | 2 +- opal/include/opal/sys/cma.h | 4 ---- opal/include/opal/sys/riscv64/Makefile.am | 12 +----------- opal/include/opal/sys/riscv64/timer.h | 11 ++--------- opal/mca/timer/linux/timer_linux_component.c | 2 ++ 5 files changed, 6 insertions(+), 25 deletions(-) diff --git a/config/opal_config_asm.m4 b/config/opal_config_asm.m4 index 472959d4bf8..978c19a68ca 100644 --- a/config/opal_config_asm.m4 +++ b/config/opal_config_asm.m4 @@ -539,7 +539,7 @@ AC_DEFUN([OPAL_CHECK_INLINE_C_GCC],[ opal_gcc_inline_assign='"1: li %0,0" : "=&r"(ret)' ;; riscv64*) - opal_gcc_inline_assign='"li %0, 0" : "=r"(ret)' + opal_gcc_inline_assign='"li %0, 0" : "=&r"(ret)' ;; esac diff --git a/opal/include/opal/sys/cma.h b/opal/include/opal/sys/cma.h index 12277e19fe2..b1db5f7700c 100644 --- a/opal/include/opal/sys/cma.h +++ b/opal/include/opal/sys/cma.h @@ -59,10 +59,6 @@ # define __NR_process_vm_readv 270 # define __NR_process_vm_writev 271 -# elif defined(PLATFORM_ARCH_RISCV) -# define __NR_process_vm_readv 270 -# define __NR_process_vm_writev 271 - # else # error "Unsupported architecture for process_vm_readv and process_vm_writev syscalls" # endif diff --git a/opal/include/opal/sys/riscv64/Makefile.am b/opal/include/opal/sys/riscv64/Makefile.am index 853d27d7824..57ded02ee74 100644 --- a/opal/include/opal/sys/riscv64/Makefile.am +++ b/opal/include/opal/sys/riscv64/Makefile.am @@ -1,16 +1,6 @@ # -# Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana -# University Research and Technology -# Corporation. All rights reserved. -# Copyright (c) 2004-2005 The University of Tennessee and The University -# of Tennessee Research Foundation. All rights -# reserved. -# Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, -# University of Stuttgart. All rights reserved. -# Copyright (c) 2004-2005 The Regents of the University of California. +# Copyright (c) 2025 Software System Team, SANECHIPS. # All rights reserved. -# Copyright (c) 2017 Research Organization for Information Science -# and Technology (RIST). All rights reserved. # $COPYRIGHT$ # # Additional copyrights may follow diff --git a/opal/include/opal/sys/riscv64/timer.h b/opal/include/opal/sys/riscv64/timer.h index e2daa72cbf4..7c636f2fa74 100644 --- a/opal/include/opal/sys/riscv64/timer.h +++ b/opal/include/opal/sys/riscv64/timer.h @@ -1,14 +1,7 @@ /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */ /* - * Copyright (c) 2008 The University of Tennessee and The University - * of Tennessee Research Foundation. All rights - * reserved. - * Copyright (c) 2016 Broadcom Limited. All rights reserved. - * Copyright (c) 2016 Los Alamos National Security, LLC. All rights - * reserved. - * Copyright (c) 2021 Google, LLC. All rights reserved. - * Copyright (c) 2022 Amazon.com, Inc. or its affiliates. - * All Rights reserved. + * Copyright (c) 2025 Software System Team, SANECHIPS. + * All rights reserved. * $COPYRIGHT$ * * Additional copyrights may follow diff --git a/opal/mca/timer/linux/timer_linux_component.c b/opal/mca/timer/linux/timer_linux_component.c index 0bd679b1f4d..b2f3dd506bb 100644 --- a/opal/mca/timer/linux/timer_linux_component.c +++ b/opal/mca/timer/linux/timer_linux_component.c @@ -18,6 +18,8 @@ * Copyright (c) 2016 Broadcom Limited. All rights reserved. * Copyright (c) 2022 Amazon.com, Inc. or its affiliates. * All Rights reserved. + * Copyright (c) 2025 Software System Team, SANECHIPS. + * All rights reserved. * $COPYRIGHT$ * * Additional copyrights may follow