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/csr.h> 10 11 typedef unsigned long cycles_t; 12 13 static inline cycles_t get_cycles(void) 14 { 15 return csr_read(CSR_TIME); 16 } 17 #define get_cycles get_cycles 18 19 static inline u32 get_cycles_hi(void) 20 { 21 return csr_read(CSR_TIMEH); 22 } 23 #define get_cycles_hi get_cycles_hi 24 25 #ifdef CONFIG_64BIT 26 static inline u64 get_cycles64(void) 27 { 28 return get_cycles(); 29 } 30 #else /* CONFIG_64BIT */ 31 static inline u64 get_cycles64(void) 32 { 33 u32 hi, lo; 34 35 do { 36 hi = get_cycles_hi(); 37 lo = get_cycles(); 38 } while (hi != get_cycles_hi()); 39 40 return ((u64)hi << 32) | lo; 41 } 42 #endif /* CONFIG_64BIT */ 43 44 #define ARCH_HAS_READ_CURRENT_TIMER 45 static inline int read_current_timer(unsigned long *timer_val) 46 { 47 *timer_val = get_cycles(); 48 return 0; 49 } 50 51 #endif /* _ASM_RISCV_TIMEX_H */ 52