|
1 |
| -#include <platform-timer.hh> |
| 1 | +#include <cheri.hh> |
| 2 | +#include <stdint.h> |
| 3 | +#include <utils.hh> |
2 | 4 |
|
3 |
| -extern "C" uint64_t get_time() { |
| 5 | +#include "mphalport.h" |
| 6 | + |
| 7 | +class StandardClint : private utils::NoCopyNoMove |
| 8 | +{ |
| 9 | +public: |
| 10 | +/** |
| 11 | + * Initialise the interface. |
| 12 | + */ |
| 13 | +static void init() { |
| 14 | + // This needs to be csr_read(mhartid) if we support multicore |
| 15 | + // systems, but we don't plan to any time soon. |
| 16 | + constexpr uint32_t HartId = 0; |
| 17 | + |
| 18 | + auto setField = [&](auto &field, size_t offset, size_t size) { |
| 19 | + CHERI::Capability capability{MMIO_CAPABILITY(uint32_t, clint)}; |
| 20 | + capability.address() += offset; |
| 21 | + capability.bounds() = size; |
| 22 | + field = capability; |
| 23 | + }; |
| 24 | + setField(pmtimer, StandardClint::ClintMtime, 2 * sizeof(uint32_t)); |
| 25 | + setField(pmtimercmp, |
| 26 | + StandardClint::ClintMtimecmp + HartId * 8, |
| 27 | + 2 * sizeof(uint32_t)); |
| 28 | +} |
| 29 | + |
| 30 | +static uint64_t time() { |
| 31 | + // The timer is little endian, so the high 32 bits are after the low 32 |
| 32 | + // bits. We can't do atomic 64-bit loads and so we have to read these |
| 33 | + // separately. |
| 34 | + volatile uint32_t *timerHigh = pmtimer + 1; |
| 35 | + uint32_t timeLow, timeHigh; |
| 36 | + |
| 37 | + // Read the current time. Loop until the high 32 bits are stable. |
| 38 | + do |
| 39 | + { |
| 40 | + timeHigh = *timerHigh; |
| 41 | + timeLow = *pmtimer; |
| 42 | + } while (timeHigh != *timerHigh); |
| 43 | + return (uint64_t(timeHigh) << 32) | timeLow; |
| 44 | +} |
| 45 | + |
| 46 | +private: |
| 47 | +#ifdef IBEX_SAFE |
| 48 | +/** |
| 49 | + * The Ibex-SAFE platform doesn't implement a complete CLINT, only the |
| 50 | + * timer part (which is all that we use). Its offsets within the CLINT |
| 51 | + * region are different. |
| 52 | + */ |
| 53 | +static constexpr size_t ClintMtime = 0x10; |
| 54 | +static constexpr size_t ClintMtimecmp = 0x18; |
| 55 | +#else |
| 56 | +/** |
| 57 | + * The standard RISC-V CLINT is a large memory-mapped device and places the |
| 58 | + * timers a long way in. |
| 59 | + */ |
| 60 | +static constexpr size_t ClintMtimecmp = 0x4000U; |
| 61 | +static constexpr size_t ClintMtime = 0xbff8U; |
| 62 | +#endif |
| 63 | + |
| 64 | +static inline volatile uint32_t *pmtimercmp; |
| 65 | +static inline volatile uint32_t *pmtimer; |
| 66 | +}; |
| 67 | + |
| 68 | +using TimerCore = StandardClint; |
| 69 | + |
| 70 | + |
| 71 | +uint64_t MP_HAL_FUNC get_time() { |
4 | 72 | StandardClint::init();
|
5 | 73 | return StandardClint::time();
|
6 | 74 | }
|
7 | 75 |
|
8 |
| -// extern "C" void set_next(uint64_t next_time) { |
| 76 | +// void MP_HAL_FUNC set_next(uint64_t next_time) { |
9 | 77 | // StandardClint::init();
|
10 | 78 | // StandardClint::setnext(next_time);
|
11 | 79 | // }
|
12 | 80 |
|
13 |
| -// extern "C" void clear() { |
| 81 | +// void MP_HAL_FUNC clear() { |
14 | 82 | // StandardClient::clear();
|
15 | 83 | // }
|
0 commit comments