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