Software APIs
ibex_host.c
1// Copyright lowRISC contributors (OpenTitan project).
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
5#include <pthread.h>
6#include <time.h>
7
8#include "sw/device/silicon_creator/lib/drivers/ibex.h"
9
10// These constants are used by the pwrmgr_cdc_sync function to compute its
11// polling delay. We supply reasonable values for host tests to execute.
12const uint64_t kClockFreqCpuHz = 100000000;
13const uint64_t kClockFreqAonHz = 200000;
14
15// These ibex functions are used to implement milli- or micro-second timeout
16// loops in the firmware. The host implementations are supplied so that
17// host-based unit tests will compile and link and allow rudimentary testing of
18// the timeout functionality.
19static __thread uint64_t time_zero;
20
21static uint64_t current_time_us(void) {
22 struct timespec tp;
23 clock_gettime(CLOCK_MONOTONIC, &tp);
24 return tp.tv_sec * 1000000 + tp.tv_nsec / 1000;
25}
26
27void ibex_mcycle_zero(void) { time_zero = current_time_us(); }
28
29uint64_t ibex_mcycle(void) { return current_time_us() - time_zero; }
30
31uint32_t ibex_mcycle32(void) { return (uint32_t)ibex_mcycle(); }
32
33uint64_t ibex_time_to_cycles(uint64_t time_us) { return time_us; }