diff options
Diffstat (limited to 'include/os/posix/timers.c')
-rw-r--r-- | include/os/posix/timers.c | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/include/os/posix/timers.c b/include/os/posix/timers.c new file mode 100644 index 0000000..c84b0de --- /dev/null +++ b/include/os/posix/timers.c | |||
@@ -0,0 +1,169 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <sys/time.h> | ||
24 | |||
25 | #include <nvgpu/bug.h> | ||
26 | #include <nvgpu/log.h> | ||
27 | #include <nvgpu/timers.h> | ||
28 | |||
29 | static s64 now(void) | ||
30 | { | ||
31 | return nvgpu_current_time_ms(); | ||
32 | } | ||
33 | |||
34 | /* | ||
35 | * Returns true if a > b; | ||
36 | */ | ||
37 | static bool time_after(s64 a, s64 b) | ||
38 | { | ||
39 | return a - b > 0; | ||
40 | } | ||
41 | |||
42 | int nvgpu_timeout_init(struct gk20a *g, struct nvgpu_timeout *timeout, | ||
43 | u32 duration, unsigned long flags) | ||
44 | { | ||
45 | if (flags & ~NVGPU_TIMER_FLAG_MASK) | ||
46 | return -EINVAL; | ||
47 | |||
48 | memset(timeout, 0, sizeof(*timeout)); | ||
49 | |||
50 | timeout->g = g; | ||
51 | timeout->flags = flags; | ||
52 | |||
53 | if (flags & NVGPU_TIMER_RETRY_TIMER) | ||
54 | timeout->retries.max = duration; | ||
55 | else | ||
56 | timeout->time = nvgpu_current_time_ms() + (s64)duration; | ||
57 | |||
58 | return 0; | ||
59 | } | ||
60 | |||
61 | static int __nvgpu_timeout_expired_msg_cpu(struct nvgpu_timeout *timeout, | ||
62 | void *caller, | ||
63 | const char *fmt, va_list args) | ||
64 | { | ||
65 | struct gk20a *g = timeout->g; | ||
66 | |||
67 | if (time_after(now(), timeout->time)) { | ||
68 | if (!(timeout->flags & NVGPU_TIMER_SILENT_TIMEOUT)) { | ||
69 | char buf[128]; | ||
70 | |||
71 | vsnprintf(buf, sizeof(buf), fmt, args); | ||
72 | |||
73 | nvgpu_err(g, "Timeout detected @ %p %s", caller, buf); | ||
74 | } | ||
75 | |||
76 | return -ETIMEDOUT; | ||
77 | } | ||
78 | |||
79 | return 0; | ||
80 | } | ||
81 | |||
82 | static int __nvgpu_timeout_expired_msg_retry(struct nvgpu_timeout *timeout, | ||
83 | void *caller, | ||
84 | const char *fmt, va_list args) | ||
85 | { | ||
86 | struct gk20a *g = timeout->g; | ||
87 | |||
88 | if (timeout->retries.attempted >= timeout->retries.max) { | ||
89 | if (!(timeout->flags & NVGPU_TIMER_SILENT_TIMEOUT)) { | ||
90 | char buf[128]; | ||
91 | |||
92 | vsnprintf(buf, sizeof(buf), fmt, args); | ||
93 | |||
94 | nvgpu_err(g, "No more retries @ %p %s", caller, buf); | ||
95 | } | ||
96 | |||
97 | return -ETIMEDOUT; | ||
98 | } | ||
99 | |||
100 | timeout->retries.attempted++; | ||
101 | |||
102 | return 0; | ||
103 | } | ||
104 | |||
105 | int __nvgpu_timeout_expired_msg(struct nvgpu_timeout *timeout, | ||
106 | void *caller, const char *fmt, ...) | ||
107 | { | ||
108 | int ret; | ||
109 | va_list args; | ||
110 | |||
111 | va_start(args, fmt); | ||
112 | if (timeout->flags & NVGPU_TIMER_RETRY_TIMER) | ||
113 | ret = __nvgpu_timeout_expired_msg_retry(timeout, caller, fmt, | ||
114 | args); | ||
115 | else | ||
116 | ret = __nvgpu_timeout_expired_msg_cpu(timeout, caller, fmt, | ||
117 | args); | ||
118 | va_end(args); | ||
119 | |||
120 | return ret; | ||
121 | } | ||
122 | |||
123 | int nvgpu_timeout_peek_expired(struct nvgpu_timeout *timeout) | ||
124 | { | ||
125 | if (timeout->flags & NVGPU_TIMER_RETRY_TIMER) | ||
126 | return timeout->retries.attempted >= timeout->retries.max; | ||
127 | else | ||
128 | return time_after(now(), timeout->time); | ||
129 | } | ||
130 | |||
131 | void nvgpu_udelay(unsigned int usecs) | ||
132 | { | ||
133 | BUG(); | ||
134 | } | ||
135 | |||
136 | void nvgpu_usleep_range(unsigned int min_us, unsigned int max_us) | ||
137 | { | ||
138 | BUG(); | ||
139 | } | ||
140 | |||
141 | void nvgpu_msleep(unsigned int msecs) | ||
142 | { | ||
143 | BUG(); | ||
144 | } | ||
145 | |||
146 | static inline s64 __nvgpu_current_time_us(void) | ||
147 | { | ||
148 | struct timeval now; | ||
149 | s64 time_now; | ||
150 | int ret; | ||
151 | |||
152 | ret = gettimeofday(&now, NULL); | ||
153 | if (ret != 0) | ||
154 | BUG(); | ||
155 | |||
156 | time_now = ((s64)now.tv_sec * (s64)1000000) + (s64)now.tv_usec; | ||
157 | |||
158 | return time_now; | ||
159 | } | ||
160 | |||
161 | s64 nvgpu_current_time_ms(void) | ||
162 | { | ||
163 | return __nvgpu_current_time_us() / (s64)1000; | ||
164 | } | ||
165 | |||
166 | u64 nvgpu_hr_timestamp(void) | ||
167 | { | ||
168 | return __nvgpu_current_time_us(); | ||
169 | } | ||