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 16 struct time_source _time_source __nex_bss; 17 18 static TEE_Result register_time_source(void) 19 { 20 time_source_init(); 21 22 return TEE_SUCCESS; 23 } 24 nex_early_init(register_time_source); 25 26 TEE_Result tee_time_get_sys_time(TEE_Time *time) 27 { 28 return _time_source.get_sys_time(time); 29 } 30 31 uint32_t tee_time_get_sys_time_protection_level(void) 32 { 33 return _time_source.protection_level; 34 } 35 36 void tee_time_wait(uint32_t milliseconds_delay) 37 { 38 struct thread_param params = 39 THREAD_PARAM_VALUE(IN, milliseconds_delay, 0, 0); 40 41 thread_rpc_cmd(OPTEE_RPC_CMD_SUSPEND, 1, ¶ms); 42 } 43 44 /* 45 * tee_time_get_ree_time(): this function implements the GP Internal API 46 * function TEE_GetREETime() 47 * Goal is to get the time of the Rich Execution Environment 48 * This is why this time is provided through the supplicant 49 */ 50 TEE_Result tee_time_get_ree_time(TEE_Time *time) 51 { 52 struct thread_param params = THREAD_PARAM_VALUE(OUT, 0, 0, 0); 53 TEE_Result res = TEE_SUCCESS; 54 55 if (!time) 56 return TEE_ERROR_BAD_PARAMETERS; 57 58 res = thread_rpc_cmd(OPTEE_RPC_CMD_GET_TIME, 1, ¶ms); 59 if (res == TEE_SUCCESS) { 60 time->seconds = params.u.value.a; 61 time->millis = params.u.value.b / 1000000; 62 } 63 64 return res; 65 } 66