xref: /optee_os/core/arch/riscv/kernel/tee_time_rdtime.c (revision 1cf7e98d072c636fa35277ec149196490524e711)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright 2022-2023 NXP
4  */
5 
6 #include <kernel/clint.h>
7 #include <kernel/tee_time.h>
8 #include <riscv.h>
9 #include <utee_defines.h>
10 
read_time(void)11 __noprof uint64_t read_time(void)
12 {
13 	uint64_t time = 0;
14 	uint32_t hi __maybe_unused = 0;
15 	uint32_t lo __maybe_unused = 0;
16 
17 #ifdef CFG_RISCV_M_MODE
18 	time = clint_get_mtime();
19 #endif /*CFG_RISCV_M_MODE*/
20 
21 #ifdef CFG_RISCV_S_MODE
22 #ifdef RV32
23 	do {
24 		hi = read_csr(timeh);
25 		lo = read_csr(time);
26 	} while (hi != read_csr(timeh));
27 
28 	time = SHIFT_U64(hi, 32) | lo;
29 #else /*RV64*/
30 	time = rdtime();
31 #endif /*RV32*/
32 #endif /*CFG_RISCV_S_MODE*/
33 
34 	return time;
35 }
36 
tee_time_get_sys_time(TEE_Time * time)37 TEE_Result tee_time_get_sys_time(TEE_Time *time)
38 {
39 	uint64_t tm = read_time();
40 	uint64_t rate = read_cntfrq();
41 
42 	time->seconds = tm / rate;
43 	time->millis = (tm % rate) / (rate / TEE_TIME_MILLIS_BASE);
44 
45 	return TEE_SUCCESS;
46 }
47 
tee_time_get_sys_time_protection_level(void)48 uint32_t tee_time_get_sys_time_protection_level(void)
49 {
50 	return 1000;
51 }
52