1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2016-2020, Linaro Limited
4  * Copyright (c) 2014, STMicroelectronics International N.V.
5  */
6 
7 #include <compiler.h>
8 #include <initcall.h>
9 #include <kernel/tee_time.h>
10 #include <kernel/thread.h>
11 #include <kernel/time_source.h>
12 #include <mm/core_mmu.h>
13 #include <optee_rpc_cmd.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 struct time_source _time_source;
18 
register_time_source(void)19 static TEE_Result register_time_source(void)
20 {
21 	time_source_init();
22 
23 	return TEE_SUCCESS;
24 }
25 early_init(register_time_source);
26 
tee_time_get_sys_time(TEE_Time * time)27 TEE_Result tee_time_get_sys_time(TEE_Time *time)
28 {
29 	return _time_source.get_sys_time(time);
30 }
31 
tee_time_get_sys_time_protection_level(void)32 uint32_t tee_time_get_sys_time_protection_level(void)
33 {
34 	return _time_source.protection_level;
35 }
36 
tee_time_wait(uint32_t milliseconds_delay)37 void tee_time_wait(uint32_t milliseconds_delay)
38 {
39 	struct thread_param params =
40 		THREAD_PARAM_VALUE(IN, milliseconds_delay, 0, 0);
41 
42 	thread_rpc_cmd(OPTEE_RPC_CMD_SUSPEND, 1, &params);
43 }
44 
45 /*
46  * tee_time_get_ree_time(): this function implements the GP Internal API
47  * function TEE_GetREETime()
48  * Goal is to get the time of the Rich Execution Environment
49  * This is why this time is provided through the supplicant
50  */
tee_time_get_ree_time(TEE_Time * time)51 TEE_Result tee_time_get_ree_time(TEE_Time *time)
52 {
53 	struct thread_param params = THREAD_PARAM_VALUE(OUT, 0, 0, 0);
54 	TEE_Result res = TEE_SUCCESS;
55 
56 	if (!time)
57 		return TEE_ERROR_BAD_PARAMETERS;
58 
59 	res = thread_rpc_cmd(OPTEE_RPC_CMD_GET_TIME, 1, &params);
60 	if (res == TEE_SUCCESS) {
61 		time->seconds = params.u.value.a;
62 		time->millis = params.u.value.b / 1000000;
63 	}
64 
65 	return res;
66 }
67