From 63a4e21dad12e61a8688757bb1d43b75f9e41c07 Mon Sep 17 00:00:00 2001 From: cheloha Date: Sat, 5 Nov 2022 16:23:02 +0000 Subject: [PATCH] powerpc64: TimeBase: add userspace timecounting support Tested by gkoehler@. ok gkoehler@ --- lib/libc/arch/powerpc64/gen/usertc.c | 23 +++++++++++++++++++++-- sys/arch/powerpc64/include/timetc.h | 4 ++-- sys/arch/powerpc64/powerpc64/clock.c | 3 ++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/libc/arch/powerpc64/gen/usertc.c b/lib/libc/arch/powerpc64/gen/usertc.c index 2ad6f973007..f0ff2df276f 100644 --- a/lib/libc/arch/powerpc64/gen/usertc.c +++ b/lib/libc/arch/powerpc64/gen/usertc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: usertc.c,v 1.1 2020/07/11 16:21:29 kettenis Exp $ */ +/* $OpenBSD: usertc.c,v 1.2 2022/11/05 16:23:02 cheloha Exp $ */ /* * Copyright (c) 2020 Paul Irofti * @@ -18,4 +18,23 @@ #include #include -int (*const _tc_get_timecount)(struct timekeep *, u_int *) = NULL; +static u_int +mftbl(void) +{ + uint64_t value; + __asm volatile ("mftb %0" : "=r"(value)); + return value; +} + +static int +tc_get_timecount(struct timekeep *tk, u_int *tc) +{ + switch (tk->tk_user) { + case TC_TB: + *tc = mftbl(); + return 0; + } + return -1; +} + +int (*const _tc_get_timecount)(struct timekeep *, u_int *) = tc_get_timecount; diff --git a/sys/arch/powerpc64/include/timetc.h b/sys/arch/powerpc64/include/timetc.h index ec63af16d18..4567a49ada4 100644 --- a/sys/arch/powerpc64/include/timetc.h +++ b/sys/arch/powerpc64/include/timetc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: timetc.h,v 1.1 2020/07/06 13:33:08 pirofti Exp $ */ +/* $OpenBSD: timetc.h,v 1.2 2022/11/05 16:23:02 cheloha Exp $ */ /* * Copyright (c) 2020 Paul Irofti * @@ -18,6 +18,6 @@ #ifndef _MACHINE_TIMETC_H_ #define _MACHINE_TIMETC_H_ -#define TC_LAST 0 +#define TC_TB 1 #endif /* _MACHINE_TIMETC_H_ */ diff --git a/sys/arch/powerpc64/powerpc64/clock.c b/sys/arch/powerpc64/powerpc64/clock.c index aef95e83dd8..20e81c62414 100644 --- a/sys/arch/powerpc64/powerpc64/clock.c +++ b/sys/arch/powerpc64/powerpc64/clock.c @@ -1,4 +1,4 @@ -/* $OpenBSD: clock.c,v 1.5 2022/08/11 17:15:21 cheloha Exp $ */ +/* $OpenBSD: clock.c,v 1.6 2022/11/05 16:23:02 cheloha Exp $ */ /* * Copyright (c) 2020 Mark Kettenis @@ -44,6 +44,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