blob: 6752f3b74bc9bdfe0ce142462959cda41bf67817 [file] [log] [blame]
Shrenuj Bansala419c792016-10-20 14:05:11 -07001/* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <linux/err.h>
15#include <linux/file.h>
16#include <linux/sched.h>
17#include <linux/slab.h>
18#include <linux/uaccess.h>
19
20#include <asm/current.h>
21
22#include "kgsl_sync.h"
23
24static void kgsl_sync_timeline_signal(struct sync_timeline *timeline,
25 unsigned int timestamp);
26
27static struct sync_pt *kgsl_sync_pt_create(struct sync_timeline *timeline,
28 struct kgsl_context *context, unsigned int timestamp)
29{
30 struct sync_pt *pt;
31
32 pt = sync_pt_create(timeline, (int) sizeof(struct kgsl_sync_pt));
33 if (pt) {
34 struct kgsl_sync_pt *kpt = (struct kgsl_sync_pt *) pt;
35
36 kpt->context = context;
37 kpt->timestamp = timestamp;
38 }
39 return pt;
40}
41
42/*
43 * This should only be called on sync_pts which have been created but
44 * not added to a fence.
45 */
46static void kgsl_sync_pt_destroy(struct sync_pt *pt)
47{
48 sync_pt_free(pt);
49}
50
51static struct sync_pt *kgsl_sync_pt_dup(struct sync_pt *pt)
52{
53 struct kgsl_sync_pt *kpt = (struct kgsl_sync_pt *) pt;
54
55 return kgsl_sync_pt_create(sync_pt_parent(pt),
56 kpt->context, kpt->timestamp);
57}
58
59static int kgsl_sync_pt_has_signaled(struct sync_pt *pt)
60{
61 struct kgsl_sync_pt *kpt = (struct kgsl_sync_pt *) pt;
62 struct kgsl_sync_timeline *ktimeline =
63 (struct kgsl_sync_timeline *) sync_pt_parent(pt);
64 unsigned int ts = kpt->timestamp;
65 int ret = 0;
66
67 spin_lock(&ktimeline->lock);
68 ret = (timestamp_cmp(ktimeline->last_timestamp, ts) >= 0);
69 spin_unlock(&ktimeline->lock);
70
71 return ret;
72}
73
74static int kgsl_sync_pt_compare(struct sync_pt *a, struct sync_pt *b)
75{
76 struct kgsl_sync_pt *kpt_a = (struct kgsl_sync_pt *) a;
77 struct kgsl_sync_pt *kpt_b = (struct kgsl_sync_pt *) b;
78 unsigned int ts_a = kpt_a->timestamp;
79 unsigned int ts_b = kpt_b->timestamp;
80
81 return timestamp_cmp(ts_a, ts_b);
82}
83
84struct kgsl_fence_event_priv {
85 struct kgsl_context *context;
86 unsigned int timestamp;
87};
88
89/**
90 * kgsl_fence_event_cb - Event callback for a fence timestamp event
91 * @device - The KGSL device that expired the timestamp
92 * @context- Pointer to the context that owns the event
93 * @priv: Private data for the callback
94 * @result - Result of the event (retired or canceled)
95 *
96 * Signal a fence following the expiration of a timestamp
97 */
98
99static void kgsl_fence_event_cb(struct kgsl_device *device,
100 struct kgsl_event_group *group, void *priv, int result)
101{
102 struct kgsl_fence_event_priv *ev = priv;
103
104 kgsl_sync_timeline_signal(ev->context->timeline, ev->timestamp);
105 kgsl_context_put(ev->context);
106 kfree(ev);
107}
108
109static int _add_fence_event(struct kgsl_device *device,
110 struct kgsl_context *context, unsigned int timestamp)
111{
112 struct kgsl_fence_event_priv *event;
113 int ret;
114
115 event = kmalloc(sizeof(*event), GFP_KERNEL);
116 if (event == NULL)
117 return -ENOMEM;
118
119 /*
120 * Increase the refcount for the context to keep it through the
121 * callback
122 */
123 if (!_kgsl_context_get(context)) {
124 kfree(event);
125 return -ENOENT;
126 }
127
128 event->context = context;
129 event->timestamp = timestamp;
130 event->context = context;
131
132 ret = kgsl_add_event(device, &context->events, timestamp,
133 kgsl_fence_event_cb, event);
134
135 if (ret) {
136 kgsl_context_put(context);
137 kfree(event);
138 }
139
140 return ret;
141}
142
143/**
144 * kgsl_add_fence_event - Create a new fence event
145 * @device - KGSL device to create the event on
146 * @timestamp - Timestamp to trigger the event
147 * @data - Return fence fd stored in struct kgsl_timestamp_event_fence
148 * @len - length of the fence event
149 * @owner - driver instance that owns this event
150 * @returns 0 on success or error code on error
151 *
152 * Create a fence and register an event to signal the fence when
153 * the timestamp expires
154 */
155
156int kgsl_add_fence_event(struct kgsl_device *device,
157 u32 context_id, u32 timestamp, void __user *data, int len,
158 struct kgsl_device_private *owner)
159{
160 struct kgsl_timestamp_event_fence priv;
161 struct kgsl_context *context;
162 struct sync_pt *pt;
163 struct sync_fence *fence = NULL;
164 int ret = -EINVAL;
165 char fence_name[sizeof(fence->name)] = {};
166 unsigned int cur;
167
168 priv.fence_fd = -1;
169
170 if (len != sizeof(priv))
171 return -EINVAL;
172
173 context = kgsl_context_get_owner(owner, context_id);
174
175 if (context == NULL)
176 return -EINVAL;
177
178 if (test_bit(KGSL_CONTEXT_PRIV_INVALID, &context->priv))
179 goto out;
180
181 pt = kgsl_sync_pt_create(context->timeline, context, timestamp);
182 if (pt == NULL) {
183 KGSL_DRV_CRIT_RATELIMIT(device, "kgsl_sync_pt_create failed\n");
184 ret = -ENOMEM;
185 goto out;
186 }
187 snprintf(fence_name, sizeof(fence_name),
188 "%s-pid-%d-ctx-%d-ts-%d",
189 device->name, current->group_leader->pid,
190 context_id, timestamp);
191
192
193 fence = sync_fence_create(fence_name, pt);
194 if (fence == NULL) {
195 /* only destroy pt when not added to fence */
196 kgsl_sync_pt_destroy(pt);
197 KGSL_DRV_CRIT_RATELIMIT(device, "sync_fence_create failed\n");
198 ret = -ENOMEM;
199 goto out;
200 }
201
202 priv.fence_fd = get_unused_fd_flags(0);
203 if (priv.fence_fd < 0) {
204 KGSL_DRV_CRIT_RATELIMIT(device,
205 "Unable to get a file descriptor: %d\n",
206 priv.fence_fd);
207 ret = priv.fence_fd;
208 goto out;
209 }
210
211 /*
212 * If the timestamp hasn't expired yet create an event to trigger it.
213 * Otherwise, just signal the fence - there is no reason to go through
214 * the effort of creating a fence we don't need.
215 */
216
217 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED, &cur);
218
219 if (timestamp_cmp(cur, timestamp) >= 0) {
220 ret = 0;
221 kgsl_sync_timeline_signal(context->timeline, cur);
222 } else {
223 ret = _add_fence_event(device, context, timestamp);
224 if (ret)
225 goto out;
226 }
227
228 if (copy_to_user(data, &priv, sizeof(priv))) {
229 ret = -EFAULT;
230 goto out;
231 }
232 sync_fence_install(fence, priv.fence_fd);
233out:
234 kgsl_context_put(context);
235 if (ret) {
236 if (priv.fence_fd >= 0)
237 put_unused_fd(priv.fence_fd);
238
239 if (fence)
240 sync_fence_put(fence);
241 }
242 return ret;
243}
244
245static unsigned int kgsl_sync_get_timestamp(
246 struct kgsl_sync_timeline *ktimeline, enum kgsl_timestamp_type type)
247{
248 unsigned int ret = 0;
249 struct kgsl_context *context;
250
251 if (ktimeline->device == NULL)
252 return 0;
253
254 context = kgsl_context_get(ktimeline->device,
255 ktimeline->context_id);
256
257 if (context)
258 kgsl_readtimestamp(ktimeline->device, context, type, &ret);
259
260 kgsl_context_put(context);
261 return ret;
262}
263
264static void kgsl_sync_timeline_value_str(struct sync_timeline *sync_timeline,
265 char *str, int size)
266{
267 struct kgsl_sync_timeline *ktimeline =
268 (struct kgsl_sync_timeline *) sync_timeline;
269
270 /*
271 * This callback can be called before the device and spinlock are
272 * initialized in struct kgsl_sync_timeline. kgsl_sync_get_timestamp()
273 * will check if device is NULL and return 0. Queued and retired
274 * timestamp of the context will be reported as 0, which is correct
275 * because the context and timeline are just getting initialized.
276 */
277 unsigned int timestamp_retired = kgsl_sync_get_timestamp(ktimeline,
278 KGSL_TIMESTAMP_RETIRED);
279 unsigned int timestamp_queued = kgsl_sync_get_timestamp(ktimeline,
280 KGSL_TIMESTAMP_QUEUED);
281
282 snprintf(str, size, "%u queued:%u retired:%u",
283 ktimeline->last_timestamp,
284 timestamp_queued, timestamp_retired);
285}
286
287static void kgsl_sync_pt_value_str(struct sync_pt *sync_pt,
288 char *str, int size)
289{
290 struct kgsl_sync_pt *kpt = (struct kgsl_sync_pt *) sync_pt;
291
292 snprintf(str, size, "%u", kpt->timestamp);
293}
294
295static int kgsl_sync_fill_driver_data(struct sync_pt *sync_pt, void *data,
296 int size)
297{
298 struct kgsl_sync_pt *kpt = (struct kgsl_sync_pt *) sync_pt;
299
300 if (size < sizeof(kpt->timestamp))
301 return -ENOMEM;
302
303 memcpy(data, &kpt->timestamp, sizeof(kpt->timestamp));
304 return sizeof(kpt->timestamp);
305}
306
307static void kgsl_sync_timeline_release_obj(struct sync_timeline *sync_timeline)
308{
309 /*
310 * Make sure to free the timeline only after destroy flag is set.
311 * This is to avoid further accessing to the timeline from KGSL and
312 * also to catch any unbalanced kref of timeline.
313 */
314 BUG_ON(sync_timeline && (sync_timeline->destroyed != true));
315}
316static const struct sync_timeline_ops kgsl_sync_timeline_ops = {
317 .driver_name = "kgsl-timeline",
318 .dup = kgsl_sync_pt_dup,
319 .has_signaled = kgsl_sync_pt_has_signaled,
320 .compare = kgsl_sync_pt_compare,
321 .timeline_value_str = kgsl_sync_timeline_value_str,
322 .pt_value_str = kgsl_sync_pt_value_str,
323 .fill_driver_data = kgsl_sync_fill_driver_data,
324 .release_obj = kgsl_sync_timeline_release_obj,
325};
326
327int kgsl_sync_timeline_create(struct kgsl_context *context)
328{
329 struct kgsl_sync_timeline *ktimeline;
330
331 /*
332 * Generate a name which includes the thread name, thread id, process
333 * name, process id, and context id. This makes it possible to
334 * identify the context of a timeline in the sync dump.
335 */
336 char ktimeline_name[sizeof(context->timeline->name)] = {};
337
338 snprintf(ktimeline_name, sizeof(ktimeline_name),
339 "%s_%.15s(%d)-%.15s(%d)-%d",
340 context->device->name,
341 current->group_leader->comm, current->group_leader->pid,
342 current->comm, current->pid, context->id);
343
344 context->timeline = sync_timeline_create(&kgsl_sync_timeline_ops,
345 (int) sizeof(struct kgsl_sync_timeline), ktimeline_name);
346 if (context->timeline == NULL)
347 return -EINVAL;
348
349 ktimeline = (struct kgsl_sync_timeline *) context->timeline;
350 ktimeline->last_timestamp = 0;
351 ktimeline->device = context->device;
352 ktimeline->context_id = context->id;
353
354 spin_lock_init(&ktimeline->lock);
355 return 0;
356}
357
358static void kgsl_sync_timeline_signal(struct sync_timeline *timeline,
359 unsigned int timestamp)
360{
361 struct kgsl_sync_timeline *ktimeline =
362 (struct kgsl_sync_timeline *) timeline;
363
364 spin_lock(&ktimeline->lock);
365 if (timestamp_cmp(timestamp, ktimeline->last_timestamp) > 0)
366 ktimeline->last_timestamp = timestamp;
367 spin_unlock(&ktimeline->lock);
368
369 sync_timeline_signal(timeline);
370}
371
372void kgsl_sync_timeline_destroy(struct kgsl_context *context)
373{
374 sync_timeline_destroy(context->timeline);
375}
376
377static void kgsl_sync_callback(struct sync_fence *fence,
378 struct sync_fence_waiter *waiter)
379{
380 struct kgsl_sync_fence_waiter *kwaiter =
381 (struct kgsl_sync_fence_waiter *) waiter;
382 kwaiter->func(kwaiter->priv);
383 sync_fence_put(kwaiter->fence);
384 kfree(kwaiter);
385}
386
387struct kgsl_sync_fence_waiter *kgsl_sync_fence_async_wait(int fd,
388 void (*func)(void *priv), void *priv)
389{
390 struct kgsl_sync_fence_waiter *kwaiter;
391 struct sync_fence *fence;
392 int status;
393
394 fence = sync_fence_fdget(fd);
395 if (fence == NULL)
396 return ERR_PTR(-EINVAL);
397
398 /* create the waiter */
399 kwaiter = kzalloc(sizeof(*kwaiter), GFP_ATOMIC);
400 if (kwaiter == NULL) {
401 sync_fence_put(fence);
402 return ERR_PTR(-ENOMEM);
403 }
404
405 kwaiter->fence = fence;
406 kwaiter->priv = priv;
407 kwaiter->func = func;
408
409 strlcpy(kwaiter->name, fence->name, sizeof(kwaiter->name));
410
411 sync_fence_waiter_init((struct sync_fence_waiter *) kwaiter,
412 kgsl_sync_callback);
413
414 /* if status then error or signaled */
415 status = sync_fence_wait_async(fence,
416 (struct sync_fence_waiter *) kwaiter);
417 if (status) {
418 kfree(kwaiter);
419 sync_fence_put(fence);
420 if (status < 0)
421 kwaiter = ERR_PTR(status);
422 else
423 kwaiter = NULL;
424 }
425
426 return kwaiter;
427}
428
429int kgsl_sync_fence_async_cancel(struct kgsl_sync_fence_waiter *kwaiter)
430{
431 if (kwaiter == NULL)
432 return 0;
433
434 if (sync_fence_cancel_async(kwaiter->fence,
435 (struct sync_fence_waiter *) kwaiter) == 0) {
436 sync_fence_put(kwaiter->fence);
437 kfree(kwaiter);
438 return 1;
439 }
440 return 0;
441}
442
443#ifdef CONFIG_ONESHOT_SYNC
444
445#include "oneshot_sync.h"
446
447struct kgsl_syncsource {
448 struct kref refcount;
449 int id;
450 struct kgsl_process_private *private;
451 struct oneshot_sync_timeline *oneshot;
452};
453
454long kgsl_ioctl_syncsource_create(struct kgsl_device_private *dev_priv,
455 unsigned int cmd, void *data)
456{
457 struct kgsl_syncsource *syncsource = NULL;
458 struct kgsl_syncsource_create *param = data;
459 int ret = -EINVAL;
460 int id = 0;
461 struct kgsl_process_private *private = dev_priv->process_priv;
462 char name[32];
463
464 syncsource = kzalloc(sizeof(*syncsource), GFP_KERNEL);
465 if (syncsource == NULL) {
466 ret = -ENOMEM;
467 goto out;
468 }
469
470 snprintf(name, sizeof(name), "kgsl-syncsource-pid-%d",
471 current->group_leader->pid);
472
473 syncsource->oneshot = oneshot_timeline_create(name);
474 if (syncsource->oneshot == NULL) {
475 ret = -ENOMEM;
476 goto out;
477 }
478
479 kref_init(&syncsource->refcount);
480 syncsource->private = private;
481
482 idr_preload(GFP_KERNEL);
483 spin_lock(&private->syncsource_lock);
484 id = idr_alloc(&private->syncsource_idr, syncsource, 1, 0, GFP_NOWAIT);
485 if (id > 0) {
486 syncsource->id = id;
487 param->id = id;
488 ret = 0;
489 } else {
490 ret = id;
491 }
492
493 spin_unlock(&private->syncsource_lock);
494 idr_preload_end();
495
496out:
497 if (ret) {
498 if (syncsource && syncsource->oneshot)
499 oneshot_timeline_destroy(syncsource->oneshot);
500 kfree(syncsource);
501 }
502
503 return ret;
504}
505
506static struct kgsl_syncsource *
507kgsl_syncsource_get(struct kgsl_process_private *private, int id)
508{
509 int result = 0;
510 struct kgsl_syncsource *syncsource = NULL;
511
512 spin_lock(&private->syncsource_lock);
513
514 syncsource = idr_find(&private->syncsource_idr, id);
515 if (syncsource)
516 result = kref_get_unless_zero(&syncsource->refcount);
517
518 spin_unlock(&private->syncsource_lock);
519
520 return result ? syncsource : NULL;
521}
522
523static void kgsl_syncsource_destroy(struct kref *kref)
524{
525 struct kgsl_syncsource *syncsource = container_of(kref,
526 struct kgsl_syncsource,
527 refcount);
528
529 struct kgsl_process_private *private = syncsource->private;
530
531 spin_lock(&private->syncsource_lock);
532 if (syncsource->id != 0) {
533 idr_remove(&private->syncsource_idr, syncsource->id);
534 syncsource->id = 0;
535 }
536 oneshot_timeline_destroy(syncsource->oneshot);
537 spin_unlock(&private->syncsource_lock);
538
539 kfree(syncsource);
540}
541
542void kgsl_syncsource_put(struct kgsl_syncsource *syncsource)
543{
544 if (syncsource)
545 kref_put(&syncsource->refcount, kgsl_syncsource_destroy);
546}
547
548long kgsl_ioctl_syncsource_destroy(struct kgsl_device_private *dev_priv,
549 unsigned int cmd, void *data)
550{
551 struct kgsl_syncsource_destroy *param = data;
552 struct kgsl_syncsource *syncsource = NULL;
553 struct kgsl_process_private *private = dev_priv->process_priv;
554
555 spin_lock(&private->syncsource_lock);
556 syncsource = idr_find(&private->syncsource_idr, param->id);
557
558 if (syncsource) {
559 idr_remove(&private->syncsource_idr, param->id);
560 syncsource->id = 0;
561 }
562
563 spin_unlock(&private->syncsource_lock);
564
565 if (syncsource == NULL)
566 return -EINVAL;
567
568 /* put reference from syncsource creation */
569 kgsl_syncsource_put(syncsource);
570 return 0;
571}
572
573long kgsl_ioctl_syncsource_create_fence(struct kgsl_device_private *dev_priv,
574 unsigned int cmd, void *data)
575{
576 struct kgsl_syncsource_create_fence *param = data;
577 struct kgsl_syncsource *syncsource = NULL;
578 int ret = -EINVAL;
579 struct sync_fence *fence = NULL;
580 int fd = -1;
581 char name[32];
582
583
584 syncsource = kgsl_syncsource_get(dev_priv->process_priv,
585 param->id);
586 if (syncsource == NULL)
587 goto out;
588
589 snprintf(name, sizeof(name), "kgsl-syncsource-pid-%d-%d",
590 current->group_leader->pid, syncsource->id);
591
592 fence = oneshot_fence_create(syncsource->oneshot, name);
593 if (fence == NULL) {
594 ret = -ENOMEM;
595 goto out;
596 }
597
598 fd = get_unused_fd_flags(0);
599 if (fd < 0) {
600 ret = -EBADF;
601 goto out;
602 }
603 ret = 0;
604
605 sync_fence_install(fence, fd);
606
607 param->fence_fd = fd;
608out:
609 if (ret) {
610 if (fence)
611 sync_fence_put(fence);
612 if (fd >= 0)
613 put_unused_fd(fd);
614
615 }
616 kgsl_syncsource_put(syncsource);
617 return ret;
618}
619
620long kgsl_ioctl_syncsource_signal_fence(struct kgsl_device_private *dev_priv,
621 unsigned int cmd, void *data)
622{
623 int ret = -EINVAL;
624 struct kgsl_syncsource_signal_fence *param = data;
625 struct kgsl_syncsource *syncsource = NULL;
626 struct sync_fence *fence = NULL;
627
628 syncsource = kgsl_syncsource_get(dev_priv->process_priv,
629 param->id);
630 if (syncsource == NULL)
631 goto out;
632
633 fence = sync_fence_fdget(param->fence_fd);
634 if (fence == NULL) {
635 ret = -EBADF;
636 goto out;
637 }
638
639 ret = oneshot_fence_signal(syncsource->oneshot, fence);
640out:
641 if (fence)
642 sync_fence_put(fence);
643 kgsl_syncsource_put(syncsource);
644 return ret;
645}
646#endif