summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/nvgpu/common/linux/timers.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpu/nvgpu/common/linux/timers.c')
-rw-r--r--drivers/gpu/nvgpu/common/linux/timers.c177
1 files changed, 177 insertions, 0 deletions
diff --git a/drivers/gpu/nvgpu/common/linux/timers.c b/drivers/gpu/nvgpu/common/linux/timers.c
new file mode 100644
index 00000000..e1e08f82
--- /dev/null
+++ b/drivers/gpu/nvgpu/common/linux/timers.c
@@ -0,0 +1,177 @@
1/*
2 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <linux/jiffies.h>
18
19#include <nvgpu/timers.h>
20
21#include "gk20a/gk20a.h"
22
23/*
24 * Returns 1 if the platform is pre-Si and should ignore the timeout checking.
25 * Setting %NVGPU_TIMER_NO_PRE_SI will make this always return 0 (i.e do the
26 * timeout check regardless of platform).
27 */
28static int nvgpu_timeout_is_pre_silicon(struct nvgpu_timeout *timeout)
29{
30 if (timeout->flags & NVGPU_TIMER_NO_PRE_SI)
31 return 0;
32
33 return !tegra_platform_is_silicon();
34}
35
36/**
37 * nvgpu_timeout_init - Init timer.
38 *
39 * @g - nvgpu device.
40 * @timeout - The timer.
41 * @duration - Timeout in milliseconds or number of retries.
42 * @flags - Flags for timer.
43 *
44 * This configures the timeout to start the timeout duration now, i.e: when this
45 * function is called. Available flags to pass to @flags:
46 *
47 * %NVGPU_TIMER_CPU_TIMER
48 * %NVGPU_TIMER_RETRY_TIMER
49 * %NVGPU_TIMER_NO_PRE_SI
50 * %NVGPU_TIMER_SILENT_TIMEOUT
51 *
52 * If neither %NVGPU_TIMER_CPU_TIMER or %NVGPU_TIMER_RETRY_TIMER is passed then
53 * a CPU timer is used by default.
54 */
55int nvgpu_timeout_init(struct gk20a *g, struct nvgpu_timeout *timeout,
56 int duration, unsigned long flags)
57{
58 if (flags & ~NVGPU_TIMER_FLAG_MASK)
59 return -EINVAL;
60
61 memset(timeout, 0, sizeof(*timeout));
62
63 timeout->g = g;
64 timeout->flags = flags;
65
66 if (flags & NVGPU_TIMER_RETRY_TIMER)
67 timeout->retries.max = duration;
68 else
69 timeout->time = jiffies + msecs_to_jiffies(duration);
70
71 return 0;
72}
73
74static int __nvgpu_timeout_check_msg_cpu(struct nvgpu_timeout *timeout,
75 void *caller,
76 const char *fmt, va_list args)
77{
78 struct gk20a *g = timeout->g;
79 unsigned long now = jiffies;
80
81 if (nvgpu_timeout_is_pre_silicon(timeout))
82 return 0;
83
84 if (time_after(now, (unsigned long)timeout->time)) {
85 if (!(timeout->flags & NVGPU_TIMER_SILENT_TIMEOUT)) {
86 char buf[128];
87
88 vsnprintf(buf, sizeof(buf), fmt, args);
89
90 dev_err(dev_from_gk20a(g),
91 "Timeout detected @ 0x%pF %s\n", caller, buf);
92 }
93
94 return -ETIMEDOUT;
95 }
96
97 return 0;
98}
99
100static int __nvgpu_timeout_check_msg_retry(struct nvgpu_timeout *timeout,
101 void *caller,
102 const char *fmt, va_list args)
103{
104 struct gk20a *g = timeout->g;
105
106 if (nvgpu_timeout_is_pre_silicon(timeout))
107 return 0;
108
109 if (timeout->retries.attempted >= timeout->retries.max) {
110 if (!(timeout->flags & NVGPU_TIMER_SILENT_TIMEOUT)) {
111 char buf[128];
112
113 vsnprintf(buf, sizeof(buf), fmt, args);
114
115 dev_err(dev_from_gk20a(g),
116 "No more retries @ 0x%pF %s\n", caller, buf);
117 }
118
119 return -ETIMEDOUT;
120 }
121
122 timeout->retries.attempted++;
123
124 return 0;
125}
126
127/**
128 * __nvgpu_timeout_check_msg - Check if a timeout has expired.
129 *
130 * @timeout - The timeout to check.
131 * @caller - Address of the caller of this function.
132 * @fmt - The fmt string.
133 *
134 * Returns -ETIMEDOUT if the timeout has expired, 0 otherwise.
135 *
136 * If a timeout occurs and %NVGPU_TIMER_SILENT_TIMEOUT is not set in the timeout
137 * then a message is printed based on %fmt.
138 */
139int __nvgpu_timeout_check_msg(struct nvgpu_timeout *timeout,
140 void *caller, const char *fmt, ...)
141{
142 int ret;
143 va_list args;
144
145 va_start(args, fmt);
146 if (timeout->flags & NVGPU_TIMER_RETRY_TIMER)
147 ret = __nvgpu_timeout_check_msg_retry(timeout, caller, fmt,
148 args);
149 else
150 ret = __nvgpu_timeout_check_msg_cpu(timeout, caller, fmt,
151 args);
152 va_end(args);
153
154 return ret;
155}
156
157/**
158 * nvgpu_timeout_peek - Check the status of a timeout.
159 *
160 * @timeout - The timeout to check.
161 *
162 * Returns non-zero if the timeout is expired, zero otherwise. In the case of
163 * retry timers this will not increment the underlying retry count. Also if the
164 * timer has expired no messages will be printed.
165 *
166 * This function honors the pre-Si check as well.
167 */
168int nvgpu_timeout_peek(struct nvgpu_timeout *timeout)
169{
170 if (nvgpu_timeout_is_pre_silicon(timeout))
171 return 0;
172
173 if (timeout->flags & NVGPU_TIMER_RETRY_TIMER)
174 return timeout->retries.attempted >= timeout->retries.max;
175 else
176 return time_after(jiffies, (unsigned long)timeout->time);
177}