blob: 409745b949dbfa7d9ecd897409147ae0225623e3 [file] [log] [blame]
Terje Bergstrom75471682013-03-22 16:34:01 +02001/*
2 * Tegra host1x Syncpoints
3 *
4 * Copyright (c) 2010-2013, NVIDIA Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/module.h>
20#include <linux/device.h>
21#include <linux/slab.h>
22
23#include <trace/events/host1x.h>
24
25#include "syncpt.h"
26#include "dev.h"
Terje Bergstrom7ede0b02013-03-22 16:34:02 +020027#include "intr.h"
Terje Bergstrom62364512013-03-22 16:34:04 +020028#include "debug.h"
Terje Bergstrom7ede0b02013-03-22 16:34:02 +020029
30#define SYNCPT_CHECK_PERIOD (2 * HZ)
31#define MAX_STUCK_CHECK_COUNT 15
Terje Bergstrom75471682013-03-22 16:34:01 +020032
33static struct host1x_syncpt *_host1x_syncpt_alloc(struct host1x *host,
34 struct device *dev,
Arto Merilainenece66892013-05-29 13:26:07 +030035 bool client_managed)
Terje Bergstrom75471682013-03-22 16:34:01 +020036{
37 int i;
38 struct host1x_syncpt *sp = host->syncpt;
39 char *name;
40
41 for (i = 0; i < host->info->nb_pts && sp->name; i++, sp++)
42 ;
Arto Merilainenedeabfc2013-05-29 13:26:06 +030043
44 if (i >= host->info->nb_pts)
Terje Bergstrom75471682013-03-22 16:34:01 +020045 return NULL;
46
47 name = kasprintf(GFP_KERNEL, "%02d-%s", sp->id,
48 dev ? dev_name(dev) : NULL);
49 if (!name)
50 return NULL;
51
52 sp->dev = dev;
53 sp->name = name;
54 sp->client_managed = client_managed;
55
56 return sp;
57}
58
59u32 host1x_syncpt_id(struct host1x_syncpt *sp)
60{
61 return sp->id;
62}
63
64/*
65 * Updates the value sent to hardware.
66 */
67u32 host1x_syncpt_incr_max(struct host1x_syncpt *sp, u32 incrs)
68{
69 return (u32)atomic_add_return(incrs, &sp->max_val);
70}
71
72 /*
73 * Write cached syncpoint and waitbase values to hardware.
74 */
75void host1x_syncpt_restore(struct host1x *host)
76{
77 struct host1x_syncpt *sp_base = host->syncpt;
78 u32 i;
79
80 for (i = 0; i < host1x_syncpt_nb_pts(host); i++)
81 host1x_hw_syncpt_restore(host, sp_base + i);
82 for (i = 0; i < host1x_syncpt_nb_bases(host); i++)
83 host1x_hw_syncpt_restore_wait_base(host, sp_base + i);
84 wmb();
85}
86
87/*
88 * Update the cached syncpoint and waitbase values by reading them
89 * from the registers.
90 */
91void host1x_syncpt_save(struct host1x *host)
92{
93 struct host1x_syncpt *sp_base = host->syncpt;
94 u32 i;
95
96 for (i = 0; i < host1x_syncpt_nb_pts(host); i++) {
97 if (host1x_syncpt_client_managed(sp_base + i))
98 host1x_hw_syncpt_load(host, sp_base + i);
99 else
100 WARN_ON(!host1x_syncpt_idle(sp_base + i));
101 }
102
103 for (i = 0; i < host1x_syncpt_nb_bases(host); i++)
104 host1x_hw_syncpt_load_wait_base(host, sp_base + i);
105}
106
107/*
108 * Updates the cached syncpoint value by reading a new value from the hardware
109 * register
110 */
111u32 host1x_syncpt_load(struct host1x_syncpt *sp)
112{
113 u32 val;
114 val = host1x_hw_syncpt_load(sp->host, sp);
115 trace_host1x_syncpt_load_min(sp->id, val);
116
117 return val;
118}
119
120/*
121 * Get the current syncpoint base
122 */
123u32 host1x_syncpt_load_wait_base(struct host1x_syncpt *sp)
124{
125 u32 val;
126 host1x_hw_syncpt_load_wait_base(sp->host, sp);
127 val = sp->base_val;
128 return val;
129}
130
131/*
Terje Bergstrom75471682013-03-22 16:34:01 +0200132 * Increment syncpoint value from cpu, updating cache
133 */
Arto Merilainenebae30b2013-05-29 13:26:08 +0300134int host1x_syncpt_incr(struct host1x_syncpt *sp)
Terje Bergstrom75471682013-03-22 16:34:01 +0200135{
Arto Merilainenebae30b2013-05-29 13:26:08 +0300136 return host1x_hw_syncpt_cpu_incr(sp->host, sp);
Terje Bergstrom75471682013-03-22 16:34:01 +0200137}
138
Terje Bergstrom7ede0b02013-03-22 16:34:02 +0200139/*
140 * Updated sync point form hardware, and returns true if syncpoint is expired,
141 * false if we may need to wait
142 */
143static bool syncpt_load_min_is_expired(struct host1x_syncpt *sp, u32 thresh)
144{
145 host1x_hw_syncpt_load(sp->host, sp);
146 return host1x_syncpt_is_expired(sp, thresh);
147}
148
149/*
150 * Main entrypoint for syncpoint value waits.
151 */
152int host1x_syncpt_wait(struct host1x_syncpt *sp, u32 thresh, long timeout,
153 u32 *value)
154{
155 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
156 void *ref;
157 struct host1x_waitlist *waiter;
158 int err = 0, check_count = 0;
159 u32 val;
160
161 if (value)
162 *value = 0;
163
164 /* first check cache */
165 if (host1x_syncpt_is_expired(sp, thresh)) {
166 if (value)
167 *value = host1x_syncpt_load(sp);
168 return 0;
169 }
170
171 /* try to read from register */
172 val = host1x_hw_syncpt_load(sp->host, sp);
173 if (host1x_syncpt_is_expired(sp, thresh)) {
174 if (value)
175 *value = val;
176 goto done;
177 }
178
179 if (!timeout) {
180 err = -EAGAIN;
181 goto done;
182 }
183
184 /* allocate a waiter */
185 waiter = kzalloc(sizeof(*waiter), GFP_KERNEL);
186 if (!waiter) {
187 err = -ENOMEM;
188 goto done;
189 }
190
191 /* schedule a wakeup when the syncpoint value is reached */
192 err = host1x_intr_add_action(sp->host, sp->id, thresh,
193 HOST1X_INTR_ACTION_WAKEUP_INTERRUPTIBLE,
194 &wq, waiter, &ref);
195 if (err)
196 goto done;
197
198 err = -EAGAIN;
199 /* Caller-specified timeout may be impractically low */
200 if (timeout < 0)
201 timeout = LONG_MAX;
202
203 /* wait for the syncpoint, or timeout, or signal */
204 while (timeout) {
205 long check = min_t(long, SYNCPT_CHECK_PERIOD, timeout);
206 int remain = wait_event_interruptible_timeout(wq,
207 syncpt_load_min_is_expired(sp, thresh),
208 check);
209 if (remain > 0 || host1x_syncpt_is_expired(sp, thresh)) {
210 if (value)
211 *value = host1x_syncpt_load(sp);
212 err = 0;
213 break;
214 }
215 if (remain < 0) {
216 err = remain;
217 break;
218 }
219 timeout -= check;
220 if (timeout && check_count <= MAX_STUCK_CHECK_COUNT) {
221 dev_warn(sp->host->dev,
222 "%s: syncpoint id %d (%s) stuck waiting %d, timeout=%ld\n",
223 current->comm, sp->id, sp->name,
224 thresh, timeout);
Terje Bergstrom62364512013-03-22 16:34:04 +0200225
226 host1x_debug_dump_syncpts(sp->host);
227 if (check_count == MAX_STUCK_CHECK_COUNT)
228 host1x_debug_dump(sp->host);
Terje Bergstrom7ede0b02013-03-22 16:34:02 +0200229 check_count++;
230 }
231 }
232 host1x_intr_put_ref(sp->host, sp->id, ref);
233
234done:
235 return err;
236}
237EXPORT_SYMBOL(host1x_syncpt_wait);
238
239/*
240 * Returns true if syncpoint is expired, false if we may need to wait
241 */
242bool host1x_syncpt_is_expired(struct host1x_syncpt *sp, u32 thresh)
243{
244 u32 current_val;
245 u32 future_val;
246 smp_rmb();
247 current_val = (u32)atomic_read(&sp->min_val);
248 future_val = (u32)atomic_read(&sp->max_val);
249
250 /* Note the use of unsigned arithmetic here (mod 1<<32).
251 *
252 * c = current_val = min_val = the current value of the syncpoint.
253 * t = thresh = the value we are checking
254 * f = future_val = max_val = the value c will reach when all
255 * outstanding increments have completed.
256 *
257 * Note that c always chases f until it reaches f.
258 *
259 * Dtf = (f - t)
260 * Dtc = (c - t)
261 *
262 * Consider all cases:
263 *
264 * A) .....c..t..f..... Dtf < Dtc need to wait
265 * B) .....c.....f..t.. Dtf > Dtc expired
266 * C) ..t..c.....f..... Dtf > Dtc expired (Dct very large)
267 *
268 * Any case where f==c: always expired (for any t). Dtf == Dcf
269 * Any case where t==c: always expired (for any f). Dtf >= Dtc (because Dtc==0)
270 * Any case where t==f!=c: always wait. Dtf < Dtc (because Dtf==0,
271 * Dtc!=0)
272 *
273 * Other cases:
274 *
275 * A) .....t..f..c..... Dtf < Dtc need to wait
276 * A) .....f..c..t..... Dtf < Dtc need to wait
277 * A) .....f..t..c..... Dtf > Dtc expired
278 *
279 * So:
280 * Dtf >= Dtc implies EXPIRED (return true)
281 * Dtf < Dtc implies WAIT (return false)
282 *
283 * Note: If t is expired then we *cannot* wait on it. We would wait
284 * forever (hang the system).
285 *
286 * Note: do NOT get clever and remove the -thresh from both sides. It
287 * is NOT the same.
288 *
289 * If future valueis zero, we have a client managed sync point. In that
290 * case we do a direct comparison.
291 */
292 if (!host1x_syncpt_client_managed(sp))
293 return future_val - thresh >= current_val - thresh;
294 else
295 return (s32)(current_val - thresh) >= 0;
296}
297
Terje Bergstrom65793242013-03-22 16:34:03 +0200298/* remove a wait pointed to by patch_addr */
299int host1x_syncpt_patch_wait(struct host1x_syncpt *sp, void *patch_addr)
300{
301 return host1x_hw_syncpt_patch_wait(sp->host, sp, patch_addr);
302}
303
Terje Bergstrom75471682013-03-22 16:34:01 +0200304int host1x_syncpt_init(struct host1x *host)
305{
306 struct host1x_syncpt *syncpt;
307 int i;
308
309 syncpt = devm_kzalloc(host->dev, sizeof(*syncpt) * host->info->nb_pts,
310 GFP_KERNEL);
311 if (!syncpt)
312 return -ENOMEM;
313
314 for (i = 0; i < host->info->nb_pts; ++i) {
315 syncpt[i].id = i;
316 syncpt[i].host = host;
317 }
318
319 host->syncpt = syncpt;
320
321 host1x_syncpt_restore(host);
322
Terje Bergstrom65793242013-03-22 16:34:03 +0200323 /* Allocate sync point to use for clearing waits for expired fences */
Arto Merilainenece66892013-05-29 13:26:07 +0300324 host->nop_sp = _host1x_syncpt_alloc(host, NULL, false);
Terje Bergstrom65793242013-03-22 16:34:03 +0200325 if (!host->nop_sp)
326 return -ENOMEM;
327
Terje Bergstrom75471682013-03-22 16:34:01 +0200328 return 0;
329}
330
331struct host1x_syncpt *host1x_syncpt_request(struct device *dev,
Arto Merilainenece66892013-05-29 13:26:07 +0300332 bool client_managed)
Terje Bergstrom75471682013-03-22 16:34:01 +0200333{
334 struct host1x *host = dev_get_drvdata(dev->parent);
335 return _host1x_syncpt_alloc(host, dev, client_managed);
336}
337
338void host1x_syncpt_free(struct host1x_syncpt *sp)
339{
340 if (!sp)
341 return;
342
343 kfree(sp->name);
344 sp->dev = NULL;
345 sp->name = NULL;
Arto Merilainenece66892013-05-29 13:26:07 +0300346 sp->client_managed = false;
Terje Bergstrom75471682013-03-22 16:34:01 +0200347}
348
349void host1x_syncpt_deinit(struct host1x *host)
350{
351 int i;
352 struct host1x_syncpt *sp = host->syncpt;
353 for (i = 0; i < host->info->nb_pts; i++, sp++)
354 kfree(sp->name);
355}
356
357int host1x_syncpt_nb_pts(struct host1x *host)
358{
359 return host->info->nb_pts;
360}
361
362int host1x_syncpt_nb_bases(struct host1x *host)
363{
364 return host->info->nb_bases;
365}
366
367int host1x_syncpt_nb_mlocks(struct host1x *host)
368{
369 return host->info->nb_mlocks;
370}
371
372struct host1x_syncpt *host1x_syncpt_get(struct host1x *host, u32 id)
373{
374 if (host->info->nb_pts < id)
375 return NULL;
376 return host->syncpt + id;
377}