From 82fa1b30dbd7fa64d08237720da3999618c34d6c Mon Sep 17 00:00:00 2001 From: jca Date: Sat, 24 Jul 2021 22:41:09 +0000 Subject: [PATCH] riscv64 userland timecounter support ok kettenis@ --- lib/libc/arch/riscv64/gen/usertc.c | 27 +++++++++++++++++++++++++-- sys/arch/riscv64/include/timetc.h | 3 ++- sys/arch/riscv64/riscv64/clock.c | 3 ++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/libc/arch/riscv64/gen/usertc.c b/lib/libc/arch/riscv64/gen/usertc.c index 2274976a08f..f0f36b6fa38 100644 --- a/lib/libc/arch/riscv64/gen/usertc.c +++ b/lib/libc/arch/riscv64/gen/usertc.c @@ -1,6 +1,7 @@ -/* $OpenBSD: usertc.c,v 1.1 2021/04/29 18:33:36 drahn Exp $ */ +/* $OpenBSD: usertc.c,v 1.2 2021/07/24 22:41:09 jca Exp $ */ /* * Copyright (c) 2020 Paul Irofti + * Copyright (c) 2021 Jeremie Courreges-Anglas * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -18,4 +19,26 @@ #include #include -int (*const _tc_get_timecount)(struct timekeep *, u_int *) = NULL; +static inline u_int +rdtime(void) +{ + uint64_t ret; + + __asm volatile("rdtime %0" : "=r"(ret)); + + return ret; +} + +static int +tc_get_timecount(struct timekeep *tk, u_int *tc) +{ + switch (tk->tk_user) { + case TC_TB: + *tc = rdtime(); + return 0; + } + + return -1; +} + +int (*const _tc_get_timecount)(struct timekeep *, u_int *) = tc_get_timecount; diff --git a/sys/arch/riscv64/include/timetc.h b/sys/arch/riscv64/include/timetc.h index 5b5bd942162..284749253cf 100644 --- a/sys/arch/riscv64/include/timetc.h +++ b/sys/arch/riscv64/include/timetc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: timetc.h,v 1.2 2021/05/12 01:20:52 jsg Exp $ */ +/* $OpenBSD: timetc.h,v 1.3 2021/07/24 22:41:09 jca Exp $ */ /* * Copyright (c) 2020 Paul Irofti @@ -19,5 +19,6 @@ #ifndef _MACHINE_TIMETC_H_ #define _MACHINE_TIMETC_H_ +#define TC_TB 1 #endif /* _MACHINE_TIMETC_H_ */ diff --git a/sys/arch/riscv64/riscv64/clock.c b/sys/arch/riscv64/riscv64/clock.c index 02984bb4ed8..15b2eaf8c5d 100644 --- a/sys/arch/riscv64/riscv64/clock.c +++ b/sys/arch/riscv64/riscv64/clock.c @@ -1,4 +1,4 @@ -/* $OpenBSD: clock.c,v 1.2 2021/06/21 15:19:39 kettenis Exp $ */ +/* $OpenBSD: clock.c,v 1.3 2021/07/24 22:41:09 jca Exp $ */ /* * Copyright (c) 2020 Mark Kettenis @@ -47,6 +47,7 @@ static struct timecounter tb_timecounter = { .tc_name = "tb", .tc_quality = 0, .tc_priv = NULL, + .tc_user = TC_TB, }; void cpu_startclock(void); -- 2.20.1