aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-keystone2/odp_time.c
blob: 31146f7c235d9d221dd593e8284820f79839937e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (c) 2015, Linaro Limited
 * Copyright (c) 2015, Texas Instruments Incorporated
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#define _POSIX_C_SOURCE 200809L

#include <odp/time.h>
#include <odp/hints.h>
#include <odp/plat/ti_mcsdk.h>
#include <odp_internal.h>

#define GIGA			1000000000

typedef struct time_data_s {
	uint64_t rate;
	double ns_to_tick;
	odp_time_t start;
} time_data_t;

static time_data_t local_time;

static inline uint64_t time_local_res(void)
{
	return local_time.rate;
}

static inline uint64_t time_global_res(void)
{
	return time_local_res();
}

static inline odp_time_t time_diff_strict(odp_time_t t2, odp_time_t t1)
{
	odp_time_t time;

	if (odp_likely(t2.tick >= t1.tick))
		time.tick = t2.tick - t1.tick;
	else
		time.tick = t2.tick + (UINT64_MAX - t1.tick) + 1;

	time.type = t1.type;

	return time;
}

static inline odp_time_t time_sum(odp_time_t t1, odp_time_t t2)
{
	odp_time_t time;

	time.type = t1.type;
	time.tick = t1.tick + t2.tick;

	return time;
}

static inline odp_time_t time_local(void)
{
	odp_time_t time;

	time.type = ODP_LOCAL_TIME;
	time.tick = hplib_mUtilGetTimestamp();

	return time_diff_strict(time, local_time.start);
}

static inline odp_time_t time_local_from_ns(uint64_t ns)
{
	odp_time_t time;

	time.tick = ns * local_time.ns_to_tick;
	time.type = ODP_LOCAL_TIME;

	return time;
}

static inline int time_cmp(odp_time_t t2, odp_time_t t1)
{
	if (t1.tick < t2.tick)
		return 1;

	if (t1.tick > t2.tick)
		return -1;

	return 0;
}

static inline void time_wait_until(odp_time_t time)
{
	odp_time_t cur;

	do {
		cur = time_local();
	} while (time_cmp(time, cur) > 0);
}

odp_time_t odp_time_local(void)
{
	return time_local();
}

odp_time_t odp_time_global(void)
{
	return time_local();
}

odp_time_t odp_time_diff(odp_time_t t2, odp_time_t t1)
{
	odp_time_t time;

	time.tick = t2.tick - t1.tick;
	time.type = t1.type;

	return time;
}

uint64_t odp_time_to_ns(odp_time_t time)
{
	return time.tick / local_time.ns_to_tick;
}

odp_time_t odp_time_local_from_ns(uint64_t ns)
{
	return time_local_from_ns(ns);
}

odp_time_t odp_time_global_from_ns(uint64_t ns)
{
	return time_local_from_ns(ns);
}

int odp_time_cmp(odp_time_t t2, odp_time_t t1)
{
	return time_cmp(t2, t1);
}

odp_time_t odp_time_sum(odp_time_t t1, odp_time_t t2)
{
	return time_sum(t1, t2);
}

void odp_time_wait_ns(uint64_t ns)
{
	odp_time_t cur = time_local();
	odp_time_t wait = time_local_from_ns(ns);
	odp_time_t end_time = time_sum(cur, wait);

	time_wait_until(end_time);
}

void odp_time_wait_until(odp_time_t time)
{
	return time_wait_until(time);
}

uint64_t odp_time_local_res(void)
{
	return time_local_res();
}

uint64_t odp_time_global_res(void)
{
	return time_global_res();
}

uint64_t odp_time_to_u64(odp_time_t time)
{
	return time.tick;
}

int odp_time_global_init(void)
{
	local_time.rate = hplib_mUtilGetTicksPerSec();
	local_time.ns_to_tick = (double)local_time.rate / GIGA;
	local_time.start.type = ODP_LOCAL_TIME;
	local_time.start.tick = hplib_mUtilGetTimestamp();

	return 0;
}