File: arch_timer.h

package info (click to toggle)
linux 6.12.8-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,673,568 kB
  • sloc: ansic: 25,888,630; asm: 268,782; sh: 136,481; python: 64,809; makefile: 55,668; perl: 38,052; xml: 19,270; cpp: 5,893; yacc: 4,923; lex: 2,939; awk: 1,592; sed: 28; ruby: 25
file content (71 lines) | stat: -rw-r--r-- 1,369 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * RISC-V Arch Timer(sstc) specific interface
 *
 * Copyright (c) 2024 Intel Corporation
 */

#ifndef SELFTEST_KVM_ARCH_TIMER_H
#define SELFTEST_KVM_ARCH_TIMER_H

#include <asm/csr.h>
#include <asm/vdso/processor.h>

static unsigned long timer_freq;

#define msec_to_cycles(msec)	\
	((timer_freq) * (uint64_t)(msec) / 1000)

#define usec_to_cycles(usec)	\
	((timer_freq) * (uint64_t)(usec) / 1000000)

#define cycles_to_usec(cycles) \
	((uint64_t)(cycles) * 1000000 / (timer_freq))

static inline uint64_t timer_get_cycles(void)
{
	return csr_read(CSR_TIME);
}

static inline void timer_set_cmp(uint64_t cval)
{
	csr_write(CSR_STIMECMP, cval);
}

static inline uint64_t timer_get_cmp(void)
{
	return csr_read(CSR_STIMECMP);
}

static inline void timer_irq_enable(void)
{
	csr_set(CSR_SIE, IE_TIE);
}

static inline void timer_irq_disable(void)
{
	csr_clear(CSR_SIE, IE_TIE);
}

static inline void timer_set_next_cmp_ms(uint32_t msec)
{
	uint64_t now_ct = timer_get_cycles();
	uint64_t next_ct = now_ct + msec_to_cycles(msec);

	timer_set_cmp(next_ct);
}

static inline void __delay(uint64_t cycles)
{
	uint64_t start = timer_get_cycles();

	while ((timer_get_cycles() - start) < cycles)
		cpu_relax();
}

static inline void udelay(unsigned long usec)
{
	__delay(usec_to_cycles(usec));
}

#endif /* SELFTEST_KVM_ARCH_TIMER_H */