From 15ea618a79886024f50cabac19d7f91811dec5fb Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Mon, 28 Nov 2016 18:56:57 -0800 Subject: Pass argc and argv to main() --- bsp/env/freedom-e300-hifive1/init.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'bsp/env/freedom-e300-hifive1') diff --git a/bsp/env/freedom-e300-hifive1/init.c b/bsp/env/freedom-e300-hifive1/init.c index c088079..167d652 100644 --- a/bsp/env/freedom-e300-hifive1/init.c +++ b/bsp/env/freedom-e300-hifive1/init.c @@ -188,8 +188,6 @@ void _init() write_csr(mstatus, MSTATUS_FS); // allow FPU instructions without trapping write_csr(fcsr, 0); // initialize rounding mode, undefined at reset } - - //_exit(main(0, NULL)); } void _fini() -- cgit v1.2.3 From 0f7bac0247ae4bbb6b4e51c72d4d5a25560ae2fe Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Tue, 20 Dec 2016 15:46:45 -0800 Subject: Separate .text.unlikely and .text.startup from .text This improves the instruction stream's spatial locality. --- bsp/env/freedom-e300-hifive1/link.lds | 2 ++ 1 file changed, 2 insertions(+) (limited to 'bsp/env/freedom-e300-hifive1') diff --git a/bsp/env/freedom-e300-hifive1/link.lds b/bsp/env/freedom-e300-hifive1/link.lds index e224273..90e5c8f 100644 --- a/bsp/env/freedom-e300-hifive1/link.lds +++ b/bsp/env/freedom-e300-hifive1/link.lds @@ -26,6 +26,8 @@ SECTIONS .text : { + *(.text.unlikely .text.unlikely.*) + *(.text.startup .text.startup.*) *(.text .text.*) *(.gnu.linkonce.t.*) } >flash AT>flash :flash -- cgit v1.2.3 From 2398dfda399f445cf114e29b61d9331fddb09b4e Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Tue, 3 Jan 2017 17:45:07 -0800 Subject: Improve HiFive1 CPU frequency measurement routine - Warm up I$ first. - Correct for integer division truncation error. - Wait for an RTC clock edge before starting the timing loop, which removes an error proportional to the number of loop iterations, allowing us to run for far less time. --- bsp/env/freedom-e300-hifive1/init.c | 38 ++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'bsp/env/freedom-e300-hifive1') diff --git a/bsp/env/freedom-e300-hifive1/init.c b/bsp/env/freedom-e300-hifive1/init.c index 167d652..61a1ae3 100644 --- a/bsp/env/freedom-e300-hifive1/init.c +++ b/bsp/env/freedom-e300-hifive1/init.c @@ -5,8 +5,6 @@ #include "platform.h" #include "encoding.h" -uint32_t cpu_freq = 0; - extern int main(int argc, char** argv); extern void trap_entry(); @@ -116,21 +114,40 @@ static void use_default_clocks() use_hfrosc(4, 16); } -void measure_cpu_freq(size_t n, size_t mtime_freq) +static unsigned long __attribute__((noinline)) measure_cpu_freq(size_t n) { - uint32_t start_mtime = mtime_lo(); - uint32_t start_mcycle = mcycle_lo(); + unsigned long start_mtime, delta_mtime; + unsigned long mtime_freq = get_timer_freq(); + + // Don't start measuruing until we see an mtime tick + unsigned long tmp = mtime_lo(); + do { + start_mtime = mtime_lo(); + } while (start_mtime == tmp); + + unsigned long start_mcycle = read_csr(mcycle); - while (mtime_lo() - start_mtime < n) ; + do { + delta_mtime = mtime_lo() - start_mtime; + } while (delta_mtime < n); - uint32_t end_mtime = mtime_lo(); - uint32_t end_mcycle = mcycle_lo(); + unsigned long delta_mcycle = read_csr(mcycle) - start_mcycle; - cpu_freq = (end_mcycle-start_mcycle)/n*mtime_freq; + return (delta_mcycle / delta_mtime) * mtime_freq + + ((delta_mcycle % delta_mtime) * mtime_freq) / delta_mtime; } -uint32_t get_cpu_freq() +unsigned long get_cpu_freq() { + static uint32_t cpu_freq; + + if (!cpu_freq) { + // warm up I$ + measure_cpu_freq(1); + // measure for real + cpu_freq = measure_cpu_freq(10); + } + return cpu_freq; } @@ -178,7 +195,6 @@ void _init() { use_default_clocks(); use_pll(0, 0, 1, 31, 1); - measure_cpu_freq(1000, 32768); uart_init(115200); printf("core freq at %d Hz\n", get_cpu_freq()); -- cgit v1.2.3 From 005b1a8f84ff743710ebd693b70d208da583098d Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Tue, 3 Jan 2017 17:45:33 -0800 Subject: 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. --- bsp/env/freedom-e300-hifive1/init.c | 36 +++++++++++++++++++++++++++------ bsp/env/freedom-e300-hifive1/platform.h | 4 +++- 2 files changed, 33 insertions(+), 7 deletions(-) (limited to 'bsp/env/freedom-e300-hifive1') 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 */ -- cgit v1.2.3