1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, Linaro Limited
4  */
5 
6 #include <kernel/tee_time.h>
7 #include <kernel/time_source.h>
8 #include <kernel/mutex.h>
9 
10 static TEE_Time prev;
11 
12 static struct mutex time_mu = MUTEX_INITIALIZER;
13 
get_monotonic_ree_time(TEE_Time * time)14 static TEE_Result get_monotonic_ree_time(TEE_Time *time)
15 {
16 	TEE_Result res;
17 
18 	res = tee_time_get_ree_time(time);
19 	if (res != TEE_SUCCESS)
20 		return res;
21 
22 	mutex_lock(&time_mu);
23 	if (time->seconds < prev.seconds ||
24 		(time->seconds == prev.seconds &&
25 		 time->millis < prev.millis))
26 		*time = prev; /* REE time was rolled back */
27 	else
28 		prev = *time;
29 	mutex_unlock(&time_mu);
30 
31 	return res;
32 }
33 
34 static const struct time_source ree_time_source = {
35 	.name = "ree",
36 	.protection_level = 100,
37 	.get_sys_time = get_monotonic_ree_time,
38 };
39 
40 REGISTER_TIME_SOURCE(ree_time_source)
41