diff options
author | Andrew Waterman <andrew@sifive.com> | 2017-01-03 17:45:33 -0800 |
---|---|---|
committer | Andrew Waterman <andrew@sifive.com> | 2017-01-03 18:53:28 -0800 |
commit | 005b1a8f84ff743710ebd693b70d208da583098d (patch) | |
tree | ebc479178e5e74a3a7ecd5e6e99e3b1cc2a06fb9 | |
parent | 2398dfda399f445cf114e29b61d9331fddb09b4e (diff) |
Regularize timing code
Provide get_timer_value() and get_timer_freq() and use them. On Arty,
they use mcycle and the known-fixed core frequency, whereas on HiFive1
they use mtime and the known-fixed mtime frequency.
-rw-r--r-- | bsp/env/freedom-e300-arty/init.c | 21 | ||||
-rw-r--r-- | bsp/env/freedom-e300-arty/platform.h | 3 | ||||
-rw-r--r-- | bsp/env/freedom-e300-hifive1/init.c | 36 | ||||
-rw-r--r-- | bsp/env/freedom-e300-hifive1/platform.h | 4 | ||||
-rw-r--r-- | software/dhrystone/dhry_stubs.c | 7 |
5 files changed, 58 insertions, 13 deletions
diff --git a/bsp/env/freedom-e300-arty/init.c b/bsp/env/freedom-e300-arty/init.c index 0a80cbb..35b1104 100644 --- a/bsp/env/freedom-e300-arty/init.c +++ b/bsp/env/freedom-e300-arty/init.c @@ -9,11 +9,30 @@ extern int main(int argc, char** argv); extern void trap_entry(); -uint32_t get_cpu_freq() +static unsigned long get_cpu_freq() { return 65000000; } +unsigned long get_timer_freq() +{ + return get_cpu_freq(); +} + +uint64_t get_timer_value() +{ +#if __riscv_xlen == 32 + while (1) { + uint32_t hi = read_csr(mcycleh); + uint32_t lo = read_csr(mcycle); + if (hi == read_csr(mcycleh)) + return ((uint64_t)hi << 32) | lo; + } +#else + return read_csr(mcycle); +#endif +} + static void uart_init(size_t baud_rate) { GPIO_REG(GPIO_IOF_SEL) &= ~IOF0_UART0_MASK; diff --git a/bsp/env/freedom-e300-arty/platform.h b/bsp/env/freedom-e300-arty/platform.h index 1f62956..d5d6dda 100644 --- a/bsp/env/freedom-e300-arty/platform.h +++ b/bsp/env/freedom-e300-arty/platform.h @@ -119,4 +119,7 @@ #define HAS_BOARD_BUTTONS #include "hifive1.h" +unsigned long get_timer_freq(void); +uint64_t get_timer_value(void); + #endif /* _SIFIVE_PLATFORM_H */ diff --git a/bsp/env/freedom-e300-hifive1/init.c b/bsp/env/freedom-e300-hifive1/init.c index 61a1ae3..71e1659 100644 --- a/bsp/env/freedom-e300-hifive1/init.c +++ b/bsp/env/freedom-e300-hifive1/init.c @@ -8,16 +8,40 @@ extern int main(int argc, char** argv); extern void trap_entry(); -uint32_t mtime_lo(void) +static unsigned long mtime_lo(void) { - return *(volatile uint32_t *)(CLINT_BASE_ADDR + CLINT_MTIME); + return *(volatile unsigned long *)(CLINT_BASE_ADDR + CLINT_MTIME); } -uint32_t mcycle_lo(void) +#ifdef __riscv32 + +static uint32_t mtime_hi(void) +{ + return *(volatile uint32_t *)(CLINT_BASE_ADDR + CLINT_MTIME + 4); +} + +uint64_t get_timer_value() +{ + while (1) { + uint32_t hi = mtime_hi(); + uint32_t lo = mtime_lo(); + if (hi == mtime_hi()) + return ((uint64_t)hi << 32) | lo; + } +} + +#else /* __riscv32 */ + +uint64_t get_timer_value() +{ + return mtime_lo(); +} + +#endif + +unsigned long get_timer_freq() { - uint32_t t; - asm volatile ("csrr %0, mcycle" : "=r" (t)); - return t; + return 32768; } static void use_hfrosc(int div, int trim) diff --git a/bsp/env/freedom-e300-hifive1/platform.h b/bsp/env/freedom-e300-hifive1/platform.h index eca708e..63efc9e 100644 --- a/bsp/env/freedom-e300-hifive1/platform.h +++ b/bsp/env/freedom-e300-hifive1/platform.h @@ -126,6 +126,8 @@ #include "hifive1.h" -uint32_t get_cpu_freq(); +unsigned long get_cpu_freq(void); +unsigned long get_timer_freq(void); +uint64_t get_timer_value(void); #endif /* _SIFIVE_PLATFORM_H */ diff --git a/software/dhrystone/dhry_stubs.c b/software/dhrystone/dhry_stubs.c index d3bd14c..0616f86 100644 --- a/software/dhrystone/dhry_stubs.c +++ b/software/dhrystone/dhry_stubs.c @@ -3,16 +3,13 @@ /* The functions in this file are only meant to support Dhrystone on an * embedded RV32 system and are obviously incorrect in general. */ -// return the cycle counter as though it were the current time long time(void) { - long t; - asm volatile ("csrr %0, mcycle" : "=r" (t)); - return t / (get_cpu_freq() / 1000); + return get_timer_value() / get_timer_freq(); } // set the number of dhrystone iterations void __wrap_scanf(const char* fmt, int* n) { - *n = 1500000; + *n = 100000000; } |