blob: 7bdb62bf6b4054068094590a861f254f76c8b5c8 [file] [log] [blame]
Erik Gilling7ad530b2013-02-28 16:42:57 -08001/*
2 * drivers/base/sync.c
3 *
4 * Copyright (C) 2012 Google, Inc.
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
Erik Gillingaf7582f2013-02-28 16:43:00 -080017#include <linux/debugfs.h>
Erik Gilling8edb4ad2013-02-28 16:43:06 -080018#include <linux/export.h>
Erik Gilling7ad530b2013-02-28 16:42:57 -080019#include <linux/file.h>
20#include <linux/fs.h>
21#include <linux/kernel.h>
Erik Gilling57b505b2013-02-28 16:43:04 -080022#include <linux/poll.h>
Erik Gilling7ad530b2013-02-28 16:42:57 -080023#include <linux/sched.h>
Erik Gillingaf7582f2013-02-28 16:43:00 -080024#include <linux/seq_file.h>
Erik Gilling7ad530b2013-02-28 16:42:57 -080025#include <linux/slab.h>
26#include <linux/uaccess.h>
27#include <linux/anon_inodes.h>
28
29#include "sync.h"
30
Erik Gillingb699a642013-02-28 16:43:23 -080031#define CREATE_TRACE_POINTS
32#include "trace/sync.h"
33
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020034static const struct fence_ops android_fence_ops;
35static const struct file_operations sync_fence_fops;
Erik Gillingaf7582f2013-02-28 16:43:00 -080036
Erik Gilling7ad530b2013-02-28 16:42:57 -080037struct sync_timeline *sync_timeline_create(const struct sync_timeline_ops *ops,
38 int size, const char *name)
39{
40 struct sync_timeline *obj;
41
42 if (size < sizeof(struct sync_timeline))
43 return NULL;
44
45 obj = kzalloc(size, GFP_KERNEL);
46 if (obj == NULL)
47 return NULL;
48
Erik Gillingc5b86b72013-02-28 16:43:11 -080049 kref_init(&obj->kref);
Erik Gilling7ad530b2013-02-28 16:42:57 -080050 obj->ops = ops;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020051 obj->context = fence_context_alloc(1);
Erik Gilling7ad530b2013-02-28 16:42:57 -080052 strlcpy(obj->name, name, sizeof(obj->name));
53
54 INIT_LIST_HEAD(&obj->child_list_head);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020055 INIT_LIST_HEAD(&obj->active_list_head);
Erik Gilling7ad530b2013-02-28 16:42:57 -080056 spin_lock_init(&obj->child_list_lock);
57
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020058 sync_timeline_debug_add(obj);
Erik Gillingaf7582f2013-02-28 16:43:00 -080059
Erik Gilling7ad530b2013-02-28 16:42:57 -080060 return obj;
61}
Erik Gilling8edb4ad2013-02-28 16:43:06 -080062EXPORT_SYMBOL(sync_timeline_create);
Erik Gilling7ad530b2013-02-28 16:42:57 -080063
Erik Gillingc5b86b72013-02-28 16:43:11 -080064static void sync_timeline_free(struct kref *kref)
Erik Gillingaf7582f2013-02-28 16:43:00 -080065{
Erik Gillingc5b86b72013-02-28 16:43:11 -080066 struct sync_timeline *obj =
67 container_of(kref, struct sync_timeline, kref);
Erik Gillingaf7582f2013-02-28 16:43:00 -080068
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020069 sync_timeline_debug_remove(obj);
Erik Gillingaf7582f2013-02-28 16:43:00 -080070
Alistair Strachan5cf045f2014-02-04 16:08:36 -080071 if (obj->ops->release_obj)
72 obj->ops->release_obj(obj);
73
Erik Gillingaf7582f2013-02-28 16:43:00 -080074 kfree(obj);
75}
76
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020077static void sync_timeline_get(struct sync_timeline *obj)
78{
79 kref_get(&obj->kref);
80}
81
82static void sync_timeline_put(struct sync_timeline *obj)
83{
84 kref_put(&obj->kref, sync_timeline_free);
85}
86
Erik Gilling7ad530b2013-02-28 16:42:57 -080087void sync_timeline_destroy(struct sync_timeline *obj)
88{
Erik Gilling7ad530b2013-02-28 16:42:57 -080089 obj->destroyed = true;
Niv Yehezkel29606602014-05-24 10:28:07 -040090 /*
91 * Ensure timeline is marked as destroyed before
92 * changing timeline's fences status.
93 */
Prakash Kamliyaac5b7052014-02-04 16:08:35 -080094 smp_wmb();
Erik Gilling7ad530b2013-02-28 16:42:57 -080095
Erik Gillingc5b86b72013-02-28 16:43:11 -080096 /*
Prakash Kamliyaac5b7052014-02-04 16:08:35 -080097 * signal any children that their parent is going away.
Erik Gillingc5b86b72013-02-28 16:43:11 -080098 */
Prakash Kamliyaac5b7052014-02-04 16:08:35 -080099 sync_timeline_signal(obj);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200100 sync_timeline_put(obj);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800101}
Erik Gilling8edb4ad2013-02-28 16:43:06 -0800102EXPORT_SYMBOL(sync_timeline_destroy);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800103
Erik Gilling7ad530b2013-02-28 16:42:57 -0800104void sync_timeline_signal(struct sync_timeline *obj)
105{
106 unsigned long flags;
107 LIST_HEAD(signaled_pts);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200108 struct sync_pt *pt, *next;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800109
Erik Gillingb699a642013-02-28 16:43:23 -0800110 trace_sync_timeline(obj);
111
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200112 spin_lock_irqsave(&obj->child_list_lock, flags);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800113
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200114 list_for_each_entry_safe(pt, next, &obj->active_list_head,
115 active_list) {
116 if (fence_is_signaled_locked(&pt->base))
117 list_del(&pt->active_list);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800118 }
119
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200120 spin_unlock_irqrestore(&obj->child_list_lock, flags);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800121}
Erik Gilling8edb4ad2013-02-28 16:43:06 -0800122EXPORT_SYMBOL(sync_timeline_signal);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800123
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200124struct sync_pt *sync_pt_create(struct sync_timeline *obj, int size)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800125{
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200126 unsigned long flags;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800127 struct sync_pt *pt;
128
129 if (size < sizeof(struct sync_pt))
130 return NULL;
131
132 pt = kzalloc(size, GFP_KERNEL);
133 if (pt == NULL)
134 return NULL;
135
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200136 spin_lock_irqsave(&obj->child_list_lock, flags);
137 sync_timeline_get(obj);
138 fence_init(&pt->base, &android_fence_ops, &obj->child_list_lock,
139 obj->context, ++obj->value);
140 list_add_tail(&pt->child_list, &obj->child_list_head);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800141 INIT_LIST_HEAD(&pt->active_list);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200142 spin_unlock_irqrestore(&obj->child_list_lock, flags);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800143 return pt;
144}
Erik Gilling8edb4ad2013-02-28 16:43:06 -0800145EXPORT_SYMBOL(sync_pt_create);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800146
147void sync_pt_free(struct sync_pt *pt)
148{
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200149 fence_put(&pt->base);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800150}
Erik Gilling8edb4ad2013-02-28 16:43:06 -0800151EXPORT_SYMBOL(sync_pt_free);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800152
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200153static struct sync_fence *sync_fence_alloc(int size, const char *name)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800154{
155 struct sync_fence *fence;
156
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200157 fence = kzalloc(size, GFP_KERNEL);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800158 if (fence == NULL)
159 return NULL;
160
161 fence->file = anon_inode_getfile("sync_fence", &sync_fence_fops,
162 fence, 0);
Wei Yongjun59691362013-03-11 21:45:34 +0800163 if (IS_ERR(fence->file))
Erik Gilling7ad530b2013-02-28 16:42:57 -0800164 goto err;
165
Erik Gilling01544172013-02-28 16:43:10 -0800166 kref_init(&fence->kref);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800167 strlcpy(fence->name, name, sizeof(fence->name));
168
Erik Gilling7ad530b2013-02-28 16:42:57 -0800169 init_waitqueue_head(&fence->wq);
Erik Gillingaf7582f2013-02-28 16:43:00 -0800170
Erik Gilling7ad530b2013-02-28 16:42:57 -0800171 return fence;
172
173err:
174 kfree(fence);
175 return NULL;
176}
177
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200178static void fence_check_cb_func(struct fence *f, struct fence_cb *cb)
179{
180 struct sync_fence_cb *check;
181 struct sync_fence *fence;
182
183 check = container_of(cb, struct sync_fence_cb, cb);
184 fence = check->fence;
185
186 if (atomic_dec_and_test(&fence->status))
187 wake_up_all(&fence->wq);
188}
189
Erik Gilling7ad530b2013-02-28 16:42:57 -0800190/* TODO: implement a create which takes more that one sync_pt */
191struct sync_fence *sync_fence_create(const char *name, struct sync_pt *pt)
192{
193 struct sync_fence *fence;
194
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200195 fence = sync_fence_alloc(offsetof(struct sync_fence, cbs[1]), name);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800196 if (fence == NULL)
197 return NULL;
198
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200199 fence->num_fences = 1;
200 atomic_set(&fence->status, 1);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800201
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200202 fence->cbs[0].sync_pt = &pt->base;
203 fence->cbs[0].fence = fence;
204 if (fence_add_callback(&pt->base, &fence->cbs[0].cb,
205 fence_check_cb_func))
206 atomic_dec(&fence->status);
207
208 sync_fence_debug_add(fence);
Erik Gillingeeb2f572013-02-28 16:43:19 -0800209
Erik Gilling7ad530b2013-02-28 16:42:57 -0800210 return fence;
211}
Erik Gilling8edb4ad2013-02-28 16:43:06 -0800212EXPORT_SYMBOL(sync_fence_create);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800213
Erik Gilling7ad530b2013-02-28 16:42:57 -0800214struct sync_fence *sync_fence_fdget(int fd)
215{
216 struct file *file = fget(fd);
217
218 if (file == NULL)
219 return NULL;
220
221 if (file->f_op != &sync_fence_fops)
222 goto err;
223
224 return file->private_data;
225
226err:
227 fput(file);
228 return NULL;
229}
Erik Gilling8edb4ad2013-02-28 16:43:06 -0800230EXPORT_SYMBOL(sync_fence_fdget);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800231
232void sync_fence_put(struct sync_fence *fence)
233{
234 fput(fence->file);
235}
Erik Gilling8edb4ad2013-02-28 16:43:06 -0800236EXPORT_SYMBOL(sync_fence_put);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800237
238void sync_fence_install(struct sync_fence *fence, int fd)
239{
240 fd_install(fd, fence->file);
241}
Erik Gilling8edb4ad2013-02-28 16:43:06 -0800242EXPORT_SYMBOL(sync_fence_install);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800243
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200244static void sync_fence_add_pt(struct sync_fence *fence,
245 int *i, struct fence *pt)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800246{
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200247 fence->cbs[*i].sync_pt = pt;
248 fence->cbs[*i].fence = fence;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800249
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200250 if (!fence_add_callback(pt, &fence->cbs[*i].cb, fence_check_cb_func)) {
251 fence_get(pt);
252 (*i)++;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800253 }
Erik Gilling7ad530b2013-02-28 16:42:57 -0800254}
255
256struct sync_fence *sync_fence_merge(const char *name,
257 struct sync_fence *a, struct sync_fence *b)
258{
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200259 int num_fences = a->num_fences + b->num_fences;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800260 struct sync_fence *fence;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200261 int i, i_a, i_b;
262 unsigned long size = offsetof(struct sync_fence, cbs[num_fences]);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800263
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200264 fence = sync_fence_alloc(size, name);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800265 if (fence == NULL)
266 return NULL;
267
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200268 atomic_set(&fence->status, num_fences);
Ørjan Eide713648f2013-02-28 16:43:24 -0800269
Erik Gillingeeb2f572013-02-28 16:43:19 -0800270 /*
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200271 * Assume sync_fence a and b are both ordered and have no
272 * duplicates with the same context.
273 *
274 * If a sync_fence can only be created with sync_fence_merge
275 * and sync_fence_create, this is a reasonable assumption.
Erik Gillingeeb2f572013-02-28 16:43:19 -0800276 */
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200277 for (i = i_a = i_b = 0; i_a < a->num_fences && i_b < b->num_fences; ) {
278 struct fence *pt_a = a->cbs[i_a].sync_pt;
279 struct fence *pt_b = b->cbs[i_b].sync_pt;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800280
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200281 if (pt_a->context < pt_b->context) {
282 sync_fence_add_pt(fence, &i, pt_a);
283
284 i_a++;
285 } else if (pt_a->context > pt_b->context) {
286 sync_fence_add_pt(fence, &i, pt_b);
287
288 i_b++;
289 } else {
290 if (pt_a->seqno - pt_b->seqno <= INT_MAX)
291 sync_fence_add_pt(fence, &i, pt_a);
292 else
293 sync_fence_add_pt(fence, &i, pt_b);
294
295 i_a++;
296 i_b++;
297 }
298 }
299
300 for (; i_a < a->num_fences; i_a++)
301 sync_fence_add_pt(fence, &i, a->cbs[i_a].sync_pt);
302
303 for (; i_b < b->num_fences; i_b++)
304 sync_fence_add_pt(fence, &i, b->cbs[i_b].sync_pt);
305
306 if (num_fences > i)
307 atomic_sub(num_fences - i, &fence->status);
308 fence->num_fences = i;
309
310 sync_fence_debug_add(fence);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800311 return fence;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800312}
Erik Gilling8edb4ad2013-02-28 16:43:06 -0800313EXPORT_SYMBOL(sync_fence_merge);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800314
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200315int sync_fence_wake_up_wq(wait_queue_t *curr, unsigned mode,
316 int wake_flags, void *key)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800317{
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200318 struct sync_fence_waiter *wait;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800319
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200320 wait = container_of(curr, struct sync_fence_waiter, work);
321 list_del_init(&wait->work.task_list);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800322
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200323 wait->callback(wait->work.private, wait);
324 return 1;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800325}
326
327int sync_fence_wait_async(struct sync_fence *fence,
Erik Gillingc0f61a42013-02-28 16:43:05 -0800328 struct sync_fence_waiter *waiter)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800329{
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200330 int err = atomic_read(&fence->status);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800331 unsigned long flags;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800332
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200333 if (err < 0)
334 return err;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800335
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200336 if (!err)
337 return 1;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800338
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200339 init_waitqueue_func_entry(&waiter->work, sync_fence_wake_up_wq);
340 waiter->work.private = fence;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800341
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200342 spin_lock_irqsave(&fence->wq.lock, flags);
343 err = atomic_read(&fence->status);
344 if (err > 0)
345 __add_wait_queue_tail(&fence->wq, &waiter->work);
346 spin_unlock_irqrestore(&fence->wq.lock, flags);
347
348 if (err < 0)
349 return err;
350
351 return !err;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800352}
Erik Gilling8edb4ad2013-02-28 16:43:06 -0800353EXPORT_SYMBOL(sync_fence_wait_async);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800354
Erik Gillingc0f61a42013-02-28 16:43:05 -0800355int sync_fence_cancel_async(struct sync_fence *fence,
356 struct sync_fence_waiter *waiter)
357{
Erik Gillingc0f61a42013-02-28 16:43:05 -0800358 unsigned long flags;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200359 int ret = 0;
Erik Gillingc0f61a42013-02-28 16:43:05 -0800360
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200361 spin_lock_irqsave(&fence->wq.lock, flags);
362 if (!list_empty(&waiter->work.task_list))
363 list_del_init(&waiter->work.task_list);
364 else
365 ret = -ENOENT;
366 spin_unlock_irqrestore(&fence->wq.lock, flags);
Erik Gillingc0f61a42013-02-28 16:43:05 -0800367 return ret;
368}
Erik Gilling8edb4ad2013-02-28 16:43:06 -0800369EXPORT_SYMBOL(sync_fence_cancel_async);
Erik Gillingc0f61a42013-02-28 16:43:05 -0800370
Erik Gilling7ad530b2013-02-28 16:42:57 -0800371int sync_fence_wait(struct sync_fence *fence, long timeout)
372{
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200373 long ret;
374 int i;
375
376 if (timeout < 0)
377 timeout = MAX_SCHEDULE_TIMEOUT;
378 else
379 timeout = msecs_to_jiffies(timeout);
Erik Gillingb699a642013-02-28 16:43:23 -0800380
381 trace_sync_wait(fence, 1);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200382 for (i = 0; i < fence->num_fences; ++i)
383 trace_sync_pt(fence->cbs[i].sync_pt);
384 ret = wait_event_interruptible_timeout(fence->wq,
385 atomic_read(&fence->status) <= 0,
386 timeout);
Erik Gillingb699a642013-02-28 16:43:23 -0800387 trace_sync_wait(fence, 0);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800388
Grzegorz Swirski220f1152014-09-02 20:24:04 +0100389 if (ret < 0) {
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200390 return ret;
Grzegorz Swirski220f1152014-09-02 20:24:04 +0100391 } else if (ret == 0) {
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200392 if (timeout) {
Jamie Gennis573632c2013-02-28 16:43:26 -0800393 pr_info("fence timeout on [%p] after %dms\n", fence,
394 jiffies_to_msecs(timeout));
395 sync_dump();
396 }
Erik Gilling7ad530b2013-02-28 16:42:57 -0800397 return -ETIME;
Erik Gillingf56388f2013-02-28 16:43:15 -0800398 }
Erik Gilling7ad530b2013-02-28 16:42:57 -0800399
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200400 ret = atomic_read(&fence->status);
401 if (ret) {
402 pr_info("fence error %ld on [%p]\n", ret, fence);
403 sync_dump();
404 }
405 return ret;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800406}
Erik Gilling8edb4ad2013-02-28 16:43:06 -0800407EXPORT_SYMBOL(sync_fence_wait);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800408
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200409static const char *android_fence_get_driver_name(struct fence *fence)
410{
411 struct sync_pt *pt = container_of(fence, struct sync_pt, base);
412 struct sync_timeline *parent = sync_pt_parent(pt);
413
414 return parent->ops->driver_name;
415}
416
417static const char *android_fence_get_timeline_name(struct fence *fence)
418{
419 struct sync_pt *pt = container_of(fence, struct sync_pt, base);
420 struct sync_timeline *parent = sync_pt_parent(pt);
421
422 return parent->name;
423}
424
425static void android_fence_release(struct fence *fence)
426{
427 struct sync_pt *pt = container_of(fence, struct sync_pt, base);
428 struct sync_timeline *parent = sync_pt_parent(pt);
429 unsigned long flags;
430
431 spin_lock_irqsave(fence->lock, flags);
432 list_del(&pt->child_list);
433 if (WARN_ON_ONCE(!list_empty(&pt->active_list)))
434 list_del(&pt->active_list);
435 spin_unlock_irqrestore(fence->lock, flags);
436
437 if (parent->ops->free_pt)
438 parent->ops->free_pt(pt);
439
440 sync_timeline_put(parent);
441 fence_free(&pt->base);
442}
443
444static bool android_fence_signaled(struct fence *fence)
445{
446 struct sync_pt *pt = container_of(fence, struct sync_pt, base);
447 struct sync_timeline *parent = sync_pt_parent(pt);
448 int ret;
449
450 ret = parent->ops->has_signaled(pt);
451 if (ret < 0)
452 fence->status = ret;
453 return ret;
454}
455
456static bool android_fence_enable_signaling(struct fence *fence)
457{
458 struct sync_pt *pt = container_of(fence, struct sync_pt, base);
459 struct sync_timeline *parent = sync_pt_parent(pt);
460
461 if (android_fence_signaled(fence))
462 return false;
463
464 list_add_tail(&pt->active_list, &parent->active_list_head);
465 return true;
466}
467
468static int android_fence_fill_driver_data(struct fence *fence,
469 void *data, int size)
470{
471 struct sync_pt *pt = container_of(fence, struct sync_pt, base);
472 struct sync_timeline *parent = sync_pt_parent(pt);
473
474 if (!parent->ops->fill_driver_data)
475 return 0;
476 return parent->ops->fill_driver_data(pt, data, size);
477}
478
479static void android_fence_value_str(struct fence *fence,
480 char *str, int size)
481{
482 struct sync_pt *pt = container_of(fence, struct sync_pt, base);
483 struct sync_timeline *parent = sync_pt_parent(pt);
484
485 if (!parent->ops->pt_value_str) {
486 if (size)
487 *str = 0;
488 return;
489 }
490 parent->ops->pt_value_str(pt, str, size);
491}
492
493static void android_fence_timeline_value_str(struct fence *fence,
494 char *str, int size)
495{
496 struct sync_pt *pt = container_of(fence, struct sync_pt, base);
497 struct sync_timeline *parent = sync_pt_parent(pt);
498
499 if (!parent->ops->timeline_value_str) {
500 if (size)
501 *str = 0;
502 return;
503 }
504 parent->ops->timeline_value_str(parent, str, size);
505}
506
507static const struct fence_ops android_fence_ops = {
508 .get_driver_name = android_fence_get_driver_name,
509 .get_timeline_name = android_fence_get_timeline_name,
510 .enable_signaling = android_fence_enable_signaling,
511 .signaled = android_fence_signaled,
512 .wait = fence_default_wait,
513 .release = android_fence_release,
514 .fill_driver_data = android_fence_fill_driver_data,
515 .fence_value_str = android_fence_value_str,
516 .timeline_value_str = android_fence_timeline_value_str,
517};
518
Erik Gilling01544172013-02-28 16:43:10 -0800519static void sync_fence_free(struct kref *kref)
520{
521 struct sync_fence *fence = container_of(kref, struct sync_fence, kref);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200522 int i, status = atomic_read(&fence->status);
Erik Gilling01544172013-02-28 16:43:10 -0800523
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200524 for (i = 0; i < fence->num_fences; ++i) {
525 if (status)
526 fence_remove_callback(fence->cbs[i].sync_pt,
527 &fence->cbs[i].cb);
528 fence_put(fence->cbs[i].sync_pt);
529 }
Erik Gilling01544172013-02-28 16:43:10 -0800530
531 kfree(fence);
532}
533
Erik Gilling7ad530b2013-02-28 16:42:57 -0800534static int sync_fence_release(struct inode *inode, struct file *file)
535{
536 struct sync_fence *fence = file->private_data;
537
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200538 sync_fence_debug_remove(fence);
Erik Gillingcc3c5cd2013-02-28 16:43:08 -0800539
Erik Gilling01544172013-02-28 16:43:10 -0800540 kref_put(&fence->kref, sync_fence_free);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800541 return 0;
542}
543
Erik Gilling57b505b2013-02-28 16:43:04 -0800544static unsigned int sync_fence_poll(struct file *file, poll_table *wait)
545{
546 struct sync_fence *fence = file->private_data;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200547 int status;
Erik Gilling57b505b2013-02-28 16:43:04 -0800548
549 poll_wait(file, &fence->wq, wait);
550
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200551 status = atomic_read(&fence->status);
Erik Gillingc6792122013-02-28 16:43:18 -0800552
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200553 if (!status)
Erik Gilling57b505b2013-02-28 16:43:04 -0800554 return POLLIN;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200555 else if (status < 0)
Erik Gilling57b505b2013-02-28 16:43:04 -0800556 return POLLERR;
Lucas Tanure6a44b502014-07-15 00:32:35 -0300557 return 0;
Erik Gilling57b505b2013-02-28 16:43:04 -0800558}
559
Erik Gilling7ad530b2013-02-28 16:42:57 -0800560static long sync_fence_ioctl_wait(struct sync_fence *fence, unsigned long arg)
561{
562 __s32 value;
563
564 if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
565 return -EFAULT;
566
567 return sync_fence_wait(fence, value);
568}
569
570static long sync_fence_ioctl_merge(struct sync_fence *fence, unsigned long arg)
571{
Yann Droneaud9c6cd3b2013-08-15 15:10:54 +0200572 int fd = get_unused_fd_flags(O_CLOEXEC);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800573 int err;
574 struct sync_fence *fence2, *fence3;
575 struct sync_merge_data data;
576
Rebecca Schultz Zavin92ea915a2013-02-28 16:43:12 -0800577 if (fd < 0)
578 return fd;
579
580 if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
581 err = -EFAULT;
582 goto err_put_fd;
583 }
Erik Gilling7ad530b2013-02-28 16:42:57 -0800584
585 fence2 = sync_fence_fdget(data.fd2);
586 if (fence2 == NULL) {
587 err = -ENOENT;
588 goto err_put_fd;
589 }
590
591 data.name[sizeof(data.name) - 1] = '\0';
592 fence3 = sync_fence_merge(data.name, fence, fence2);
593 if (fence3 == NULL) {
594 err = -ENOMEM;
595 goto err_put_fence2;
596 }
597
598 data.fence = fd;
599 if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
600 err = -EFAULT;
601 goto err_put_fence3;
602 }
603
604 sync_fence_install(fence3, fd);
605 sync_fence_put(fence2);
606 return 0;
607
608err_put_fence3:
609 sync_fence_put(fence3);
610
611err_put_fence2:
612 sync_fence_put(fence2);
613
614err_put_fd:
615 put_unused_fd(fd);
616 return err;
617}
618
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200619static int sync_fill_pt_info(struct fence *fence, void *data, int size)
Erik Gilling79ba1522013-02-28 16:43:02 -0800620{
621 struct sync_pt_info *info = data;
622 int ret;
623
624 if (size < sizeof(struct sync_pt_info))
625 return -ENOMEM;
626
627 info->len = sizeof(struct sync_pt_info);
628
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200629 if (fence->ops->fill_driver_data) {
630 ret = fence->ops->fill_driver_data(fence, info->driver_data,
631 size - sizeof(*info));
Erik Gilling79ba1522013-02-28 16:43:02 -0800632 if (ret < 0)
633 return ret;
634
635 info->len += ret;
636 }
637
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200638 strlcpy(info->obj_name, fence->ops->get_timeline_name(fence),
639 sizeof(info->obj_name));
640 strlcpy(info->driver_name, fence->ops->get_driver_name(fence),
Erik Gilling79ba1522013-02-28 16:43:02 -0800641 sizeof(info->driver_name));
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200642 if (fence_is_signaled(fence))
643 info->status = fence->status >= 0 ? 1 : fence->status;
644 else
645 info->status = 0;
646 info->timestamp_ns = ktime_to_ns(fence->timestamp);
Erik Gilling79ba1522013-02-28 16:43:02 -0800647
648 return info->len;
649}
650
651static long sync_fence_ioctl_fence_info(struct sync_fence *fence,
652 unsigned long arg)
653{
654 struct sync_fence_info_data *data;
Erik Gilling79ba1522013-02-28 16:43:02 -0800655 __u32 size;
656 __u32 len = 0;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200657 int ret, i;
Erik Gilling79ba1522013-02-28 16:43:02 -0800658
659 if (copy_from_user(&size, (void __user *)arg, sizeof(size)))
660 return -EFAULT;
661
662 if (size < sizeof(struct sync_fence_info_data))
663 return -EINVAL;
664
665 if (size > 4096)
666 size = 4096;
667
668 data = kzalloc(size, GFP_KERNEL);
669 if (data == NULL)
670 return -ENOMEM;
671
672 strlcpy(data->name, fence->name, sizeof(data->name));
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200673 data->status = atomic_read(&fence->status);
674 if (data->status >= 0)
675 data->status = !data->status;
676
Erik Gilling79ba1522013-02-28 16:43:02 -0800677 len = sizeof(struct sync_fence_info_data);
678
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200679 for (i = 0; i < fence->num_fences; ++i) {
680 struct fence *pt = fence->cbs[i].sync_pt;
Erik Gilling79ba1522013-02-28 16:43:02 -0800681
682 ret = sync_fill_pt_info(pt, (u8 *)data + len, size - len);
683
684 if (ret < 0)
685 goto out;
686
687 len += ret;
688 }
689
690 data->len = len;
691
692 if (copy_to_user((void __user *)arg, data, len))
693 ret = -EFAULT;
694 else
695 ret = 0;
696
697out:
698 kfree(data);
699
700 return ret;
701}
Erik Gilling7ad530b2013-02-28 16:42:57 -0800702
703static long sync_fence_ioctl(struct file *file, unsigned int cmd,
704 unsigned long arg)
705{
706 struct sync_fence *fence = file->private_data;
Dipak Zope7b1046e2014-08-19 04:21:54 -0400707
Erik Gilling7ad530b2013-02-28 16:42:57 -0800708 switch (cmd) {
709 case SYNC_IOC_WAIT:
710 return sync_fence_ioctl_wait(fence, arg);
711
712 case SYNC_IOC_MERGE:
713 return sync_fence_ioctl_merge(fence, arg);
Erik Gillingaf7582f2013-02-28 16:43:00 -0800714
Erik Gilling79ba1522013-02-28 16:43:02 -0800715 case SYNC_IOC_FENCE_INFO:
716 return sync_fence_ioctl_fence_info(fence, arg);
717
Erik Gilling7ad530b2013-02-28 16:42:57 -0800718 default:
719 return -ENOTTY;
720 }
721}
722
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200723static const struct file_operations sync_fence_fops = {
724 .release = sync_fence_release,
725 .poll = sync_fence_poll,
726 .unlocked_ioctl = sync_fence_ioctl,
727 .compat_ioctl = sync_fence_ioctl,
Erik Gillingaf7582f2013-02-28 16:43:00 -0800728};
729