From cf91e96cb814d49d736b47f9b1a737e8b3cb1db7 Mon Sep 17 00:00:00 2001 From: cheloha Date: Thu, 10 Aug 2023 22:58:04 +0000 Subject: [PATCH] agtimer(4/arm64): agtimer_delay: compute cycle count with 64-bit arithmetic Converting from microseconds to timer cycles is much simpler with 64-bit arithmetic. Thread: https://marc.info/?l=openbsd-tech&m=169146193022516&w=2 ok drahn@ kettenis@ --- sys/arch/arm64/dev/agtimer.c | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/sys/arch/arm64/dev/agtimer.c b/sys/arch/arm64/dev/agtimer.c index 08e93b4b080..ffe40b254a0 100644 --- a/sys/arch/arm64/dev/agtimer.c +++ b/sys/arch/arm64/dev/agtimer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: agtimer.c,v 1.23 2023/07/25 18:16:19 cheloha Exp $ */ +/* $OpenBSD: agtimer.c,v 1.24 2023/08/10 22:58:04 cheloha Exp $ */ /* * Copyright (c) 2011 Dale Rahn * Copyright (c) 2013 Patrick Wildt @@ -323,32 +323,12 @@ agtimer_cpu_initclocks(void) void agtimer_delay(u_int usecs) { - uint64_t clock, oclock, delta, delaycnt; - uint64_t csec, usec; - volatile int j; - - if (usecs > (0x80000000 / agtimer_frequency)) { - csec = usecs / 10000; - usec = usecs % 10000; - - delaycnt = (agtimer_frequency / 100) * csec + - (agtimer_frequency / 100) * usec / 10000; - } else { - delaycnt = agtimer_frequency * usecs / 1000000; - } - if (delaycnt <= 1) - for (j = 100; j > 0; j--) - ; - - oclock = agtimer_readcnt64(); - while (1) { - for (j = 100; j > 0; j--) - ; - clock = agtimer_readcnt64(); - delta = clock - oclock; - if (delta > delaycnt) - break; - } + uint64_t cycles, start; + + start = agtimer_readcnt64(); + cycles = (uint64_t)usecs * agtimer_frequency / 1000000; + while (agtimer_readcnt64() - start < cycles) + continue; } void -- 2.20.1