|
| 1 | +#include <stdio.h> |
| 2 | +#include <inttypes.h> |
| 3 | + |
| 4 | +#include "common_aarch64.h" |
| 5 | + |
| 6 | +#define CNTV_CTL_ENABLE (1 << 0) |
| 7 | +#define CNTV_CTL_IMASK (1 << 1) |
| 8 | +#define CNTV_CTL_ISTATUS (1 << 2) |
| 9 | + |
| 10 | +/* Frequency in Hz. ? */ |
| 11 | +SYSREG_READ_WRITE(uint64_t, cntfrq_el0) |
| 12 | + |
| 13 | +/* Current virtual counter value. */ |
| 14 | +SYSREG_READ(uint64_t, cntvct_el0) |
| 15 | + |
| 16 | +/* Compare value. See: cntv_ctl_el0_enable. */ |
| 17 | +SYSREG_READ_WRITE(uint64_t, cntv_cval_el0) |
| 18 | + |
| 19 | +/* On write, set cntv_cval_el0 = (cntvct_el0 + cntv_tval_el0). |
| 20 | + * This means that the next interrupt will happen in cntv_tval_el0 cycles. |
| 21 | + */ |
| 22 | +SYSREG_READ_WRITE(uint64_t, cntv_tval_el0) |
| 23 | + |
| 24 | +/* Control register. */ |
| 25 | +SYSREG_READ_WRITE(uint32_t, cntv_ctl_el0) |
| 26 | + |
| 27 | +void cntv_ctl_el0_disable(void) { |
| 28 | + sysreg_cntv_ctl_el0_write(sysreg_cntv_ctl_el0_read() & ~CNTV_CTL_ENABLE); |
| 29 | +} |
| 30 | + |
| 31 | +/* If enabled, when: cntv_ctl > cntv_cval then: |
| 32 | + * |
| 33 | + * * if CNTV_CTL_IMASK is clear, raise an interrupt |
| 34 | + * * set CNTV_CTL_ISTATUS |
| 35 | + */ |
| 36 | +void cntv_ctl_el0_enable(void) { |
| 37 | + sysreg_cntv_ctl_el0_write(sysreg_cntv_ctl_el0_read() | CNTV_CTL_ENABLE); |
| 38 | +} |
| 39 | + |
| 40 | +int main(void) { |
| 41 | + /* Initial state. */ |
| 42 | + printf("cntv_ctl_el0 0x%" PRIx32 "\n", sysreg_cntv_ctl_el0_read()); |
| 43 | + printf("cntfrq_el0 0x%" PRIx64 "\n", sysreg_cntfrq_el0_read()); |
| 44 | + printf("cntv_cval_el0 0x%" PRIx64 "\n", sysreg_cntv_cval_el0_read()); |
| 45 | + |
| 46 | + /* Get the counter value many times to watch the time pass. */ |
| 47 | + printf("cntvct_el0 0x%" PRIx64 "\n", sysreg_cntvct_el0_read()); |
| 48 | + printf("cntvct_el0 0x%" PRIx64 "\n", sysreg_cntvct_el0_read()); |
| 49 | + printf("cntvct_el0 0x%" PRIx64 "\n", sysreg_cntvct_el0_read()); |
| 50 | + |
| 51 | +#if 0 |
| 52 | + /* TODO crashes gem5. */ |
| 53 | + puts("cntfrq_el0 = 1"); |
| 54 | + sysreg_cntfrq_el0_write(1); |
| 55 | + printf("cntfrq_el0 0x%" PRIx64 "\n", sysreg_cntfrq_el0_read()); |
| 56 | +#endif |
| 57 | + |
| 58 | + return 0; |
| 59 | +} |
0 commit comments