xref: /openbmc/linux/arch/riscv/include/asm/timex.h (revision 9726bfcd)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2012 Regents of the University of California
4  */
5 
6 #ifndef _ASM_RISCV_TIMEX_H
7 #define _ASM_RISCV_TIMEX_H
8 
9 #include <asm/param.h>
10 
11 typedef unsigned long cycles_t;
12 
13 static inline cycles_t get_cycles_inline(void)
14 {
15 	cycles_t n;
16 
17 	__asm__ __volatile__ (
18 		"rdtime %0"
19 		: "=r" (n));
20 	return n;
21 }
22 #define get_cycles get_cycles_inline
23 
24 #ifdef CONFIG_64BIT
25 static inline uint64_t get_cycles64(void)
26 {
27         return get_cycles();
28 }
29 #else
30 static inline uint64_t get_cycles64(void)
31 {
32 	u32 lo, hi, tmp;
33 	__asm__ __volatile__ (
34 		"1:\n"
35 		"rdtimeh %0\n"
36 		"rdtime %1\n"
37 		"rdtimeh %2\n"
38 		"bne %0, %2, 1b"
39 		: "=&r" (hi), "=&r" (lo), "=&r" (tmp));
40 	return ((u64)hi << 32) | lo;
41 }
42 #endif
43 
44 #define ARCH_HAS_READ_CURRENT_TIMER
45 
46 static inline int read_current_timer(unsigned long *timer_val)
47 {
48 	*timer_val = get_cycles();
49 	return 0;
50 }
51 
52 #endif /* _ASM_RISCV_TIMEX_H */
53