xref: /linux/drivers/gpu/host1x/syncpt.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Tegra host1x Syncpoints
4  *
5  * Copyright (c) 2010-2015, NVIDIA Corporation.
6  */
7 
8 #include <linux/module.h>
9 #include <linux/device.h>
10 #include <linux/dma-fence.h>
11 #include <linux/overflow.h>
12 #include <linux/slab.h>
13 
14 #include <trace/events/host1x.h>
15 
16 #include "syncpt.h"
17 #include "dev.h"
18 #include "intr.h"
19 #include "debug.h"
20 
21 #define SYNCPT_CHECK_PERIOD (2 * HZ)
22 #define MAX_STUCK_CHECK_COUNT 15
23 
24 static struct host1x_syncpt_base *
25 host1x_syncpt_base_request(struct host1x *host)
26 {
27 	struct host1x_syncpt_base *bases = host->bases;
28 	unsigned int i;
29 
30 	for (i = 0; i < host->info->nb_bases; i++)
31 		if (!bases[i].requested)
32 			break;
33 
34 	if (i >= host->info->nb_bases)
35 		return NULL;
36 
37 	bases[i].requested = true;
38 	return &bases[i];
39 }
40 
41 static void host1x_syncpt_base_free(struct host1x_syncpt_base *base)
42 {
43 	if (base)
44 		base->requested = false;
45 }
46 
47 /**
48  * host1x_syncpt_alloc() - allocate a syncpoint
49  * @host: host1x device data
50  * @flags: bitfield of HOST1X_SYNCPT_* flags
51  * @name: name for the syncpoint for use in debug prints
52  *
53  * Allocates a hardware syncpoint for the caller's use. The caller then has
54  * the sole authority to mutate the syncpoint's value until it is freed again.
55  *
56  * If no free syncpoints are available, or a NULL name was specified, returns
57  * NULL.
58  */
59 struct host1x_syncpt *host1x_syncpt_alloc(struct host1x *host,
60 					  unsigned long flags,
61 					  const char *name)
62 {
63 	struct host1x_syncpt *sp = host->syncpt + host->syncpt_base;
64 	char *full_name;
65 	unsigned int i;
66 
67 	if (!name)
68 		return NULL;
69 
70 	mutex_lock(&host->syncpt_mutex);
71 
72 	for (i = host->syncpt_base; i < host->syncpt_end && kref_read(&sp->ref); i++, sp++)
73 		;
74 
75 	if (i >= host->syncpt_end)
76 		goto unlock;
77 
78 	if (flags & HOST1X_SYNCPT_HAS_BASE) {
79 		sp->base = host1x_syncpt_base_request(host);
80 		if (!sp->base)
81 			goto unlock;
82 	}
83 
84 	full_name = kasprintf(GFP_KERNEL, "%u-%s", sp->id, name);
85 	if (!full_name)
86 		goto free_base;
87 
88 	sp->name = full_name;
89 
90 	if (flags & HOST1X_SYNCPT_CLIENT_MANAGED)
91 		sp->client_managed = true;
92 	else
93 		sp->client_managed = false;
94 
95 	kref_init(&sp->ref);
96 
97 	mutex_unlock(&host->syncpt_mutex);
98 	return sp;
99 
100 free_base:
101 	host1x_syncpt_base_free(sp->base);
102 	sp->base = NULL;
103 unlock:
104 	mutex_unlock(&host->syncpt_mutex);
105 	return NULL;
106 }
107 EXPORT_SYMBOL(host1x_syncpt_alloc);
108 
109 /**
110  * host1x_syncpt_id() - retrieve syncpoint ID
111  * @sp: host1x syncpoint
112  *
113  * Given a pointer to a struct host1x_syncpt, retrieves its ID. This ID is
114  * often used as a value to program into registers that control how hardware
115  * blocks interact with syncpoints.
116  */
117 u32 host1x_syncpt_id(struct host1x_syncpt *sp)
118 {
119 	return sp->id;
120 }
121 EXPORT_SYMBOL(host1x_syncpt_id);
122 
123 /**
124  * host1x_syncpt_incr_max() - update the value sent to hardware
125  * @sp: host1x syncpoint
126  * @incrs: number of increments
127  */
128 u32 host1x_syncpt_incr_max(struct host1x_syncpt *sp, u32 incrs)
129 {
130 	/*
131 	 * Syncpoint values are intended to be modulo 2^32, so overflow
132 	 * here is intended.
133 	 */
134 	return (u32)atomic_add_return(incrs, &sp->max_val);
135 }
136 EXPORT_SYMBOL(host1x_syncpt_incr_max);
137 
138  /*
139  * Write cached syncpoint and waitbase values to hardware.
140  */
141 void host1x_syncpt_restore(struct host1x *host)
142 {
143 	struct host1x_syncpt *sp_base = host->syncpt;
144 	unsigned int i;
145 
146 	for (i = host->syncpt_base; i < host->syncpt_end; i++) {
147 		/*
148 		 * Unassign syncpt from channels for purposes of Tegra186
149 		 * syncpoint protection. This prevents any channel from
150 		 * accessing it until it is reassigned.
151 		 */
152 		host1x_hw_syncpt_assign_to_channel(host, sp_base + i, NULL);
153 		host1x_hw_syncpt_restore(host, sp_base + i);
154 	}
155 
156 	for (i = 0; i < host1x_syncpt_nb_bases(host); i++)
157 		host1x_hw_syncpt_restore_wait_base(host, sp_base + i);
158 
159 	host1x_hw_syncpt_enable_protection(host);
160 
161 	wmb();
162 }
163 
164 /*
165  * Update the cached syncpoint and waitbase values by reading them
166  * from the registers.
167   */
168 void host1x_syncpt_save(struct host1x *host)
169 {
170 	struct host1x_syncpt *sp_base = host->syncpt;
171 	unsigned int i;
172 
173 	for (i = 0; i < host1x_syncpt_nb_pts(host); i++) {
174 		if (host1x_syncpt_client_managed(sp_base + i))
175 			host1x_hw_syncpt_load(host, sp_base + i);
176 		else
177 			WARN_ON(!host1x_syncpt_idle(sp_base + i));
178 	}
179 
180 	for (i = 0; i < host1x_syncpt_nb_bases(host); i++)
181 		host1x_hw_syncpt_load_wait_base(host, sp_base + i);
182 }
183 
184 /*
185  * Updates the cached syncpoint value by reading a new value from the hardware
186  * register
187  */
188 u32 host1x_syncpt_load(struct host1x_syncpt *sp)
189 {
190 	u32 val;
191 
192 	val = host1x_hw_syncpt_load(sp->host, sp);
193 	trace_host1x_syncpt_load_min(sp->id, val);
194 
195 	return val;
196 }
197 
198 /*
199  * Get the current syncpoint base
200  */
201 u32 host1x_syncpt_load_wait_base(struct host1x_syncpt *sp)
202 {
203 	host1x_hw_syncpt_load_wait_base(sp->host, sp);
204 
205 	return sp->base_val;
206 }
207 
208 /**
209  * host1x_syncpt_incr() - increment syncpoint value from CPU, updating cache
210  * @sp: host1x syncpoint
211  */
212 int host1x_syncpt_incr(struct host1x_syncpt *sp)
213 {
214 	return host1x_hw_syncpt_cpu_incr(sp->host, sp);
215 }
216 EXPORT_SYMBOL(host1x_syncpt_incr);
217 
218 /**
219  * host1x_syncpt_wait() - wait for a syncpoint to reach a given value
220  * @sp: host1x syncpoint
221  * @thresh: threshold
222  * @timeout: maximum time to wait for the syncpoint to reach the given value
223  * @value: return location for the syncpoint value
224  */
225 int host1x_syncpt_wait(struct host1x_syncpt *sp, u32 thresh, long timeout,
226 		       u32 *value)
227 {
228 	struct dma_fence *fence;
229 	long wait_err;
230 	u32 curr;
231 
232 	curr = host1x_syncpt_load(sp);
233 
234 	if (value)
235 		*value = curr;
236 
237 	if (host1x_syncpt_is_expired(sp, thresh))
238 		return 0;
239 
240 	if (timeout < 0)
241 		timeout = LONG_MAX;
242 	else if (timeout == 0)
243 		return -EAGAIN;
244 
245 	fence = host1x_fence_create(sp, thresh, false);
246 	if (IS_ERR(fence))
247 		return PTR_ERR(fence);
248 
249 	wait_err = dma_fence_wait_timeout(fence, true, timeout);
250 	if (wait_err == 0)
251 		host1x_fence_cancel(fence);
252 	dma_fence_put(fence);
253 
254 	/*
255 	 * Don't rely on dma_fence_wait_timeout return value,
256 	 * since it returns zero both on timeout and if the
257 	 * wait completed with 0 jiffies left.
258 	 */
259 	if (wait_err == 0 && !host1x_syncpt_is_expired(sp, thresh)) {
260 		if (value)
261 			*value = host1x_syncpt_load(sp);
262 
263 		return -EAGAIN;
264 	} else if (wait_err < 0) {
265 		return wait_err;
266 	} else {
267 		/* Success, read the value cached by ISR */
268 		if (value)
269 			*value = host1x_syncpt_read_min(sp);
270 
271 		return 0;
272 	}
273 }
274 EXPORT_SYMBOL(host1x_syncpt_wait);
275 
276 /*
277  * Returns true if syncpoint is expired, false if we may need to wait
278  */
279 bool host1x_syncpt_is_expired(struct host1x_syncpt *sp, u32 thresh)
280 {
281 	u32 current_val;
282 
283 	smp_rmb();
284 
285 	current_val = (u32)atomic_read(&sp->min_val);
286 
287 	return (wrapping_sub(u32, current_val, thresh) & 0x80000000U) == 0U;
288 }
289 
290 int host1x_syncpt_init(struct host1x *host)
291 {
292 	struct host1x_syncpt_base *bases;
293 	struct host1x_syncpt *syncpt;
294 	unsigned int i;
295 
296 	syncpt = devm_kzalloc_objs(host->dev, *syncpt, host->info->nb_pts);
297 	if (!syncpt)
298 		return -ENOMEM;
299 
300 	bases = devm_kzalloc_objs(host->dev, *bases, host->info->nb_bases);
301 	if (!bases)
302 		return -ENOMEM;
303 
304 	for (i = 0; i < host->info->nb_pts; i++) {
305 		syncpt[i].id = i;
306 		syncpt[i].host = host;
307 
308 		/* Default to client managed for syncpoints not owned by us */
309 		syncpt[i].client_managed = true;
310 	}
311 
312 	for (i = 0; i < host->info->nb_bases; i++)
313 		bases[i].id = i;
314 
315 	mutex_init(&host->syncpt_mutex);
316 	host->syncpt = syncpt;
317 	host->bases = bases;
318 
319 	/* Prevent syncpoint 0 from being allocated by users */
320 	if (host->syncpt_base == 0) {
321 		host->nop_sp = host1x_syncpt_alloc(host, 0, "reserved-nop");
322 		if (!host->nop_sp)
323 			return -ENOMEM;
324 	}
325 
326 	if (host->info->reserve_vblank_syncpts) {
327 		kref_init(&host->syncpt[26].ref);
328 		kref_init(&host->syncpt[27].ref);
329 	}
330 
331 	return 0;
332 }
333 
334 /**
335  * host1x_syncpt_request() - request a syncpoint
336  * @client: client requesting the syncpoint
337  * @flags: flags
338  *
339  * host1x client drivers can use this function to allocate a syncpoint for
340  * subsequent use. A syncpoint returned by this function will be reserved for
341  * use by the client exclusively. When no longer using a syncpoint, a host1x
342  * client driver needs to release it using host1x_syncpt_put().
343  */
344 struct host1x_syncpt *host1x_syncpt_request(struct host1x_client *client,
345 					    unsigned long flags)
346 {
347 	struct host1x *host = dev_get_drvdata(client->host->parent);
348 
349 	return host1x_syncpt_alloc(host, flags, dev_name(client->dev));
350 }
351 EXPORT_SYMBOL(host1x_syncpt_request);
352 
353 static void syncpt_release(struct kref *ref)
354 {
355 	struct host1x_syncpt *sp = container_of(ref, struct host1x_syncpt, ref);
356 
357 	atomic_set(&sp->max_val, host1x_syncpt_read(sp));
358 
359 	sp->locked = false;
360 
361 	host1x_syncpt_base_free(sp->base);
362 	kfree(sp->name);
363 	sp->base = NULL;
364 	sp->name = NULL;
365 	sp->client_managed = false;
366 
367 	mutex_unlock(&sp->host->syncpt_mutex);
368 }
369 
370 /**
371  * host1x_syncpt_put() - free a requested syncpoint
372  * @sp: host1x syncpoint
373  *
374  * Release a syncpoint previously allocated using host1x_syncpt_request(). A
375  * host1x client driver should call this when the syncpoint is no longer in
376  * use.
377  */
378 void host1x_syncpt_put(struct host1x_syncpt *sp)
379 {
380 	if (!sp)
381 		return;
382 
383 	kref_put_mutex(&sp->ref, syncpt_release, &sp->host->syncpt_mutex);
384 }
385 EXPORT_SYMBOL(host1x_syncpt_put);
386 
387 void host1x_syncpt_deinit(struct host1x *host)
388 {
389 	struct host1x_syncpt *sp = host->syncpt;
390 	unsigned int i;
391 
392 	for (i = 0; i < host->info->nb_pts; i++, sp++)
393 		kfree(sp->name);
394 }
395 
396 /**
397  * host1x_syncpt_read_max() - read maximum syncpoint value
398  * @sp: host1x syncpoint
399  *
400  * The maximum syncpoint value indicates how many operations there are in
401  * queue, either in channel or in a software thread.
402  */
403 u32 host1x_syncpt_read_max(struct host1x_syncpt *sp)
404 {
405 	smp_rmb();
406 
407 	return (u32)atomic_read(&sp->max_val);
408 }
409 EXPORT_SYMBOL(host1x_syncpt_read_max);
410 
411 /**
412  * host1x_syncpt_read_min() - read minimum syncpoint value
413  * @sp: host1x syncpoint
414  *
415  * The minimum syncpoint value is a shadow of the current sync point value in
416  * hardware.
417  */
418 u32 host1x_syncpt_read_min(struct host1x_syncpt *sp)
419 {
420 	smp_rmb();
421 
422 	return (u32)atomic_read(&sp->min_val);
423 }
424 EXPORT_SYMBOL(host1x_syncpt_read_min);
425 
426 /**
427  * host1x_syncpt_read() - read the current syncpoint value
428  * @sp: host1x syncpoint
429  */
430 u32 host1x_syncpt_read(struct host1x_syncpt *sp)
431 {
432 	return host1x_syncpt_load(sp);
433 }
434 EXPORT_SYMBOL(host1x_syncpt_read);
435 
436 unsigned int host1x_syncpt_nb_pts(struct host1x *host)
437 {
438 	return host->info->nb_pts;
439 }
440 
441 unsigned int host1x_syncpt_nb_bases(struct host1x *host)
442 {
443 	return host->info->nb_bases;
444 }
445 
446 unsigned int host1x_syncpt_nb_mlocks(struct host1x *host)
447 {
448 	return host->info->nb_mlocks;
449 }
450 
451 /**
452  * host1x_syncpt_get_by_id() - obtain a syncpoint by ID
453  * @host: host1x controller
454  * @id: syncpoint ID
455  */
456 struct host1x_syncpt *host1x_syncpt_get_by_id(struct host1x *host,
457 					      unsigned int id)
458 {
459 	if (id >= host->info->nb_pts)
460 		return NULL;
461 
462 	if (kref_get_unless_zero(&host->syncpt[id].ref))
463 		return &host->syncpt[id];
464 	else
465 		return NULL;
466 }
467 EXPORT_SYMBOL(host1x_syncpt_get_by_id);
468 
469 /**
470  * host1x_syncpt_get_by_id_noref() - obtain a syncpoint by ID but don't
471  * 	increase the refcount.
472  * @host: host1x controller
473  * @id: syncpoint ID
474  */
475 struct host1x_syncpt *host1x_syncpt_get_by_id_noref(struct host1x *host,
476 						    unsigned int id)
477 {
478 	if (id >= host->info->nb_pts)
479 		return NULL;
480 
481 	return &host->syncpt[id];
482 }
483 EXPORT_SYMBOL(host1x_syncpt_get_by_id_noref);
484 
485 /**
486  * host1x_syncpt_get() - increment syncpoint refcount
487  * @sp: syncpoint
488  */
489 struct host1x_syncpt *host1x_syncpt_get(struct host1x_syncpt *sp)
490 {
491 	kref_get(&sp->ref);
492 
493 	return sp;
494 }
495 EXPORT_SYMBOL(host1x_syncpt_get);
496 
497 /**
498  * host1x_syncpt_get_base() - obtain the wait base associated with a syncpoint
499  * @sp: host1x syncpoint
500  */
501 struct host1x_syncpt_base *host1x_syncpt_get_base(struct host1x_syncpt *sp)
502 {
503 	return sp ? sp->base : NULL;
504 }
505 EXPORT_SYMBOL(host1x_syncpt_get_base);
506 
507 /**
508  * host1x_syncpt_base_id() - retrieve the ID of a syncpoint wait base
509  * @base: host1x syncpoint wait base
510  */
511 u32 host1x_syncpt_base_id(struct host1x_syncpt_base *base)
512 {
513 	return base->id;
514 }
515 EXPORT_SYMBOL(host1x_syncpt_base_id);
516 
517 static void do_nothing(struct kref *ref)
518 {
519 }
520 
521 /**
522  * host1x_syncpt_release_vblank_reservation() - Make VBLANK syncpoint
523  *   available for allocation
524  *
525  * @client: host1x bus client
526  * @syncpt_id: syncpoint ID to make available
527  *
528  * Makes VBLANK<i> syncpoint available for allocatation if it was
529  * reserved at initialization time. This should be called by the display
530  * driver after it has ensured that any VBLANK increment programming configured
531  * by the boot chain has been disabled.
532  */
533 void host1x_syncpt_release_vblank_reservation(struct host1x_client *client,
534 					      u32 syncpt_id)
535 {
536 	struct host1x *host = dev_get_drvdata(client->host->parent);
537 
538 	if (!host->info->reserve_vblank_syncpts)
539 		return;
540 
541 	kref_put(&host->syncpt[syncpt_id].ref, do_nothing);
542 }
543 EXPORT_SYMBOL(host1x_syncpt_release_vblank_reservation);
544