xref: /openbmc/qemu/tests/tcg/aarch64/system/vtimer.c (revision 2e1cacfb)
1 /*
2  * Simple Virtual Timer Test
3  *
4  * Copyright (c) 2020 Linaro Ltd
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include <stdint.h>
10 #include <minilib.h>
11 
12 /* grabbed from Linux */
13 #define __stringify_1(x...) #x
14 #define __stringify(x...)   __stringify_1(x)
15 
16 #define read_sysreg(r) ({                                           \
17             uint64_t __val;                                         \
18             asm volatile("mrs %0, " __stringify(r) : "=r" (__val)); \
19             __val;                                                  \
20 })
21 
22 #define write_sysreg(r, v) do {                     \
23         uint64_t __val = (uint64_t)(v);             \
24         asm volatile("msr " __stringify(r) ", %x0"  \
25                  : : "rZ" (__val));                 \
26 } while (0)
27 
28 int main(void)
29 {
30     int i;
31 
32     ml_printf("VTimer Test\n");
33 
34     write_sysreg(cntvoff_el2, 1);
35     write_sysreg(cntv_cval_el0, -1);
36     write_sysreg(cntv_ctl_el0, 1);
37 
38     ml_printf("cntvoff_el2=%lx\n", read_sysreg(cntvoff_el2));
39     ml_printf("cntv_cval_el0=%lx\n", read_sysreg(cntv_cval_el0));
40     ml_printf("cntv_ctl_el0=%lx\n", read_sysreg(cntv_ctl_el0));
41 
42     /* Now read cval a few times */
43     for (i = 0; i < 10; i++) {
44         ml_printf("%d: cntv_cval_el0=%lx\n", i, read_sysreg(cntv_cval_el0));
45     }
46 
47     return 0;
48 }
49