1 /*
2  * Arm SCP/MCP Software
3  * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include <fwk_arch.h>
9 #include <fwk_attributes.h>
10 #include <fwk_status.h>
11 #include <fwk_time.h>
12 
13 #include <string.h>
14 
15 struct {
16     struct fwk_time_driver driver; /* Time driver */
17     const void *driver_ctx; /* Time driver context */
18 } fwk_time_ctx;
19 
fwk_time_init(void)20 FWK_CONSTRUCTOR void fwk_time_init(void)
21 {
22     struct fwk_time_driver driver = fmw_time_driver(&fwk_time_ctx.driver_ctx);
23 
24     (void)memcpy(&fwk_time_ctx.driver, &driver, sizeof(driver));
25 }
26 
fwk_time_current(void)27 fwk_timestamp_t fwk_time_current(void)
28 {
29     if (fwk_time_ctx.driver.timestamp == NULL) {
30         return FWK_NS(0);
31     }
32 
33     return fwk_time_ctx.driver.timestamp(fwk_time_ctx.driver_ctx);
34 }
35 
fwk_time_stamp_duration(fwk_timestamp_t timestamp)36 fwk_duration_ns_t fwk_time_stamp_duration(fwk_timestamp_t timestamp)
37 {
38     return FWK_NS(timestamp);
39 }
40 
fwk_time_duration(fwk_timestamp_t start,fwk_timestamp_t end)41 fwk_duration_ns_t fwk_time_duration(fwk_timestamp_t start, fwk_timestamp_t end)
42 {
43     fwk_assert(end > start);
44 
45     return end - start;
46 }
47 
fwk_time_duration_us(fwk_duration_ns_t duration)48 fwk_duration_us_t fwk_time_duration_us(fwk_duration_ns_t duration)
49 {
50     return duration / FWK_US(1);
51 }
52 
fwk_time_duration_ms(fwk_duration_ns_t duration)53 fwk_duration_ms_t fwk_time_duration_ms(fwk_duration_ns_t duration)
54 {
55     return duration / FWK_MS(1);
56 }
57 
fwk_time_duration_s(fwk_duration_ns_t duration)58 fwk_duration_s_t fwk_time_duration_s(fwk_duration_ns_t duration)
59 {
60     return duration / FWK_S(1);
61 }
62 
fwk_time_duration_m(fwk_duration_ns_t duration)63 fwk_duration_m_t fwk_time_duration_m(fwk_duration_ns_t duration)
64 {
65     return (uint32_t)(duration / FWK_M(1));
66 }
67 
fwk_time_duration_h(fwk_duration_ns_t duration)68 fwk_duration_h_t fwk_time_duration_h(fwk_duration_ns_t duration)
69 {
70     return (uint32_t)(duration / FWK_H(1));
71 }
72 
fmw_time_driver(const void ** ctx)73 FWK_WEAK struct fwk_time_driver fmw_time_driver(const void **ctx)
74 {
75     return (struct fwk_time_driver){
76         .timestamp = NULL,
77     };
78 }
79