blob: a89ded04b13798debc6b1264505cace71b806ad8 [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;
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -020035static const struct file_operations sync_file_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);
Ioana Ciornei375fb532015-11-01 16:38:20 +020046 if (!obj)
Erik Gilling7ad530b2013-02-28 16:42:57 -080047 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
71 kfree(obj);
72}
73
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020074static void sync_timeline_get(struct sync_timeline *obj)
75{
76 kref_get(&obj->kref);
77}
78
79static void sync_timeline_put(struct sync_timeline *obj)
80{
81 kref_put(&obj->kref, sync_timeline_free);
82}
83
Erik Gilling7ad530b2013-02-28 16:42:57 -080084void sync_timeline_destroy(struct sync_timeline *obj)
85{
Erik Gilling7ad530b2013-02-28 16:42:57 -080086 obj->destroyed = true;
Niv Yehezkel29606602014-05-24 10:28:07 -040087 /*
88 * Ensure timeline is marked as destroyed before
89 * changing timeline's fences status.
90 */
Prakash Kamliyaac5b7052014-02-04 16:08:35 -080091 smp_wmb();
Erik Gilling7ad530b2013-02-28 16:42:57 -080092
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020093 sync_timeline_put(obj);
Erik Gilling7ad530b2013-02-28 16:42:57 -080094}
Erik Gilling8edb4ad2013-02-28 16:43:06 -080095EXPORT_SYMBOL(sync_timeline_destroy);
Erik Gilling7ad530b2013-02-28 16:42:57 -080096
Erik Gilling7ad530b2013-02-28 16:42:57 -080097void sync_timeline_signal(struct sync_timeline *obj)
98{
99 unsigned long flags;
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200100 struct fence *fence, *next;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800101
Erik Gillingb699a642013-02-28 16:43:23 -0800102 trace_sync_timeline(obj);
103
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200104 spin_lock_irqsave(&obj->child_list_lock, flags);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800105
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200106 list_for_each_entry_safe(fence, next, &obj->active_list_head,
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200107 active_list) {
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200108 if (fence_is_signaled_locked(fence))
109 list_del_init(&fence->active_list);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800110 }
111
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200112 spin_unlock_irqrestore(&obj->child_list_lock, flags);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800113}
Erik Gilling8edb4ad2013-02-28 16:43:06 -0800114EXPORT_SYMBOL(sync_timeline_signal);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800115
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200116struct fence *sync_pt_create(struct sync_timeline *obj, int size)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800117{
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200118 unsigned long flags;
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200119 struct fence *fence;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800120
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200121 if (size < sizeof(*fence))
Erik Gilling7ad530b2013-02-28 16:42:57 -0800122 return NULL;
123
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200124 fence = kzalloc(size, GFP_KERNEL);
125 if (!fence)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800126 return NULL;
127
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200128 spin_lock_irqsave(&obj->child_list_lock, flags);
129 sync_timeline_get(obj);
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200130 fence_init(fence, &android_fence_ops, &obj->child_list_lock,
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200131 obj->context, ++obj->value);
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200132 list_add_tail(&fence->child_list, &obj->child_list_head);
133 INIT_LIST_HEAD(&fence->active_list);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200134 spin_unlock_irqrestore(&obj->child_list_lock, flags);
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200135 return fence;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800136}
Erik Gilling8edb4ad2013-02-28 16:43:06 -0800137EXPORT_SYMBOL(sync_pt_create);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800138
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200139static struct sync_file *sync_file_alloc(int size, const char *name)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800140{
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200141 struct sync_file *sync_file;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800142
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200143 sync_file = kzalloc(size, GFP_KERNEL);
144 if (!sync_file)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800145 return NULL;
146
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200147 sync_file->file = anon_inode_getfile("sync_file", &sync_file_fops,
148 sync_file, 0);
149 if (IS_ERR(sync_file->file))
Erik Gilling7ad530b2013-02-28 16:42:57 -0800150 goto err;
151
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200152 kref_init(&sync_file->kref);
153 strlcpy(sync_file->name, name, sizeof(sync_file->name));
Erik Gilling7ad530b2013-02-28 16:42:57 -0800154
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200155 init_waitqueue_head(&sync_file->wq);
Erik Gillingaf7582f2013-02-28 16:43:00 -0800156
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200157 return sync_file;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800158
159err:
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200160 kfree(sync_file);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800161 return NULL;
162}
163
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200164static void fence_check_cb_func(struct fence *f, struct fence_cb *cb)
165{
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200166 struct sync_file_cb *check;
167 struct sync_file *sync_file;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200168
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200169 check = container_of(cb, struct sync_file_cb, cb);
170 sync_file = check->sync_file;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200171
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200172 if (atomic_dec_and_test(&sync_file->status))
173 wake_up_all(&sync_file->wq);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200174}
175
Gustavo Padovan696f74c2016-04-28 10:46:50 -0300176/**
177 * sync_fence_create() - creates a sync fence
178 * @name: name of fence to create
179 * @fence: fence to add to the sync_fence
180 *
181 * Creates a sync_file containg @fence. Once this is called, the sync_file
182 * takes ownership of @fence.
183 */
Gustavo Padovand52ef2c2016-01-21 10:49:24 -0200184struct sync_file *sync_file_create(const char *name, struct fence *fence)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800185{
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200186 struct sync_file *sync_file;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800187
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200188 sync_file = sync_file_alloc(offsetof(struct sync_file, cbs[1]),
189 name);
190 if (!sync_file)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800191 return NULL;
192
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200193 sync_file->num_fences = 1;
194 atomic_set(&sync_file->status, 1);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800195
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200196 sync_file->cbs[0].fence = fence;
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200197 sync_file->cbs[0].sync_file = sync_file;
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200198 if (fence_add_callback(fence, &sync_file->cbs[0].cb,
199 fence_check_cb_func))
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200200 atomic_dec(&sync_file->status);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200201
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200202 sync_file_debug_add(sync_file);
Erik Gillingeeb2f572013-02-28 16:43:19 -0800203
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200204 return sync_file;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800205}
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200206EXPORT_SYMBOL(sync_file_create);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800207
Gustavo Padovan696f74c2016-04-28 10:46:50 -0300208/**
209 * sync_file_fdget() - get a sync_file from an fd
210 * @fd: fd referencing a fence
211 *
212 * Ensures @fd references a valid sync_file, increments the refcount of the
213 * backing file. Returns the sync_file or NULL in case of error.
214 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200215struct sync_file *sync_file_fdget(int fd)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800216{
217 struct file *file = fget(fd);
218
Ioana Ciornei375fb532015-11-01 16:38:20 +0200219 if (!file)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800220 return NULL;
221
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200222 if (file->f_op != &sync_file_fops)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800223 goto err;
224
225 return file->private_data;
226
227err:
228 fput(file);
229 return NULL;
230}
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200231EXPORT_SYMBOL(sync_file_fdget);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800232
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200233static void sync_file_add_pt(struct sync_file *sync_file, int *i,
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200234 struct fence *fence)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800235{
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200236 sync_file->cbs[*i].fence = fence;
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200237 sync_file->cbs[*i].sync_file = sync_file;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800238
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200239 if (!fence_add_callback(fence, &sync_file->cbs[*i].cb,
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200240 fence_check_cb_func)) {
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200241 fence_get(fence);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200242 (*i)++;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800243 }
Erik Gilling7ad530b2013-02-28 16:42:57 -0800244}
245
Gustavo Padovan696f74c2016-04-28 10:46:50 -0300246/**
247 * sync_file_merge() - merge two sync_files
248 * @name: name of new fence
249 * @a: sync_file a
250 * @b: sync_file b
251 *
252 * Creates a new sync_file which contains copies of all the fences in both
253 * @a and @b. @a and @b remain valid, independent sync_file. Returns the
254 * new merged sync_file or NULL in case of error.
255 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200256struct sync_file *sync_file_merge(const char *name,
257 struct sync_file *a, struct sync_file *b)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800258{
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200259 int num_fences = a->num_fences + b->num_fences;
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200260 struct sync_file *sync_file;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200261 int i, i_a, i_b;
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200262 unsigned long size = offsetof(struct sync_file, cbs[num_fences]);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800263
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200264 sync_file = sync_file_alloc(size, name);
265 if (!sync_file)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800266 return NULL;
267
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200268 atomic_set(&sync_file->status, num_fences);
Ørjan Eide713648f2013-02-28 16:43:24 -0800269
Erik Gillingeeb2f572013-02-28 16:43:19 -0800270 /*
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200271 * Assume sync_file a and b are both ordered and have no
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200272 * duplicates with the same context.
273 *
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200274 * If a sync_file can only be created with sync_file_merge
275 * and sync_file_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; ) {
Gustavo Padovanc88b26d2016-01-21 10:49:20 -0200278 struct fence *pt_a = a->cbs[i_a].fence;
279 struct fence *pt_b = b->cbs[i_b].fence;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800280
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200281 if (pt_a->context < pt_b->context) {
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200282 sync_file_add_pt(sync_file, &i, pt_a);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200283
284 i_a++;
285 } else if (pt_a->context > pt_b->context) {
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200286 sync_file_add_pt(sync_file, &i, pt_b);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200287
288 i_b++;
289 } else {
290 if (pt_a->seqno - pt_b->seqno <= INT_MAX)
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200291 sync_file_add_pt(sync_file, &i, pt_a);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200292 else
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200293 sync_file_add_pt(sync_file, &i, pt_b);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200294
295 i_a++;
296 i_b++;
297 }
298 }
299
300 for (; i_a < a->num_fences; i_a++)
Gustavo Padovanc88b26d2016-01-21 10:49:20 -0200301 sync_file_add_pt(sync_file, &i, a->cbs[i_a].fence);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200302
303 for (; i_b < b->num_fences; i_b++)
Gustavo Padovanc88b26d2016-01-21 10:49:20 -0200304 sync_file_add_pt(sync_file, &i, b->cbs[i_b].fence);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200305
306 if (num_fences > i)
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200307 atomic_sub(num_fences - i, &sync_file->status);
308 sync_file->num_fences = i;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200309
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200310 sync_file_debug_add(sync_file);
311 return sync_file;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800312}
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200313EXPORT_SYMBOL(sync_file_merge);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800314
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200315static const char *android_fence_get_driver_name(struct fence *fence)
316{
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200317 struct sync_timeline *parent = fence_parent(fence);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200318
319 return parent->ops->driver_name;
320}
321
322static const char *android_fence_get_timeline_name(struct fence *fence)
323{
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200324 struct sync_timeline *parent = fence_parent(fence);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200325
326 return parent->name;
327}
328
329static void android_fence_release(struct fence *fence)
330{
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200331 struct sync_timeline *parent = fence_parent(fence);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200332 unsigned long flags;
333
334 spin_lock_irqsave(fence->lock, flags);
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200335 list_del(&fence->child_list);
336 if (WARN_ON_ONCE(!list_empty(&fence->active_list)))
337 list_del(&fence->active_list);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200338 spin_unlock_irqrestore(fence->lock, flags);
339
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200340 sync_timeline_put(parent);
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200341 fence_free(fence);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200342}
343
344static bool android_fence_signaled(struct fence *fence)
345{
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200346 struct sync_timeline *parent = fence_parent(fence);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200347 int ret;
348
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200349 ret = parent->ops->has_signaled(fence);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200350 if (ret < 0)
351 fence->status = ret;
352 return ret;
353}
354
355static bool android_fence_enable_signaling(struct fence *fence)
356{
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200357 struct sync_timeline *parent = fence_parent(fence);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200358
359 if (android_fence_signaled(fence))
360 return false;
361
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200362 list_add_tail(&fence->active_list, &parent->active_list_head);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200363 return true;
364}
365
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200366static void android_fence_value_str(struct fence *fence,
367 char *str, int size)
368{
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200369 struct sync_timeline *parent = fence_parent(fence);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200370
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200371 if (!parent->ops->fence_value_str) {
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200372 if (size)
373 *str = 0;
374 return;
375 }
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200376 parent->ops->fence_value_str(fence, str, size);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200377}
378
379static void android_fence_timeline_value_str(struct fence *fence,
380 char *str, int size)
381{
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200382 struct sync_timeline *parent = fence_parent(fence);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200383
384 if (!parent->ops->timeline_value_str) {
385 if (size)
386 *str = 0;
387 return;
388 }
389 parent->ops->timeline_value_str(parent, str, size);
390}
391
392static const struct fence_ops android_fence_ops = {
393 .get_driver_name = android_fence_get_driver_name,
394 .get_timeline_name = android_fence_get_timeline_name,
395 .enable_signaling = android_fence_enable_signaling,
396 .signaled = android_fence_signaled,
397 .wait = fence_default_wait,
398 .release = android_fence_release,
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200399 .fence_value_str = android_fence_value_str,
400 .timeline_value_str = android_fence_timeline_value_str,
401};
402
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200403static void sync_file_free(struct kref *kref)
Erik Gilling01544172013-02-28 16:43:10 -0800404{
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200405 struct sync_file *sync_file = container_of(kref, struct sync_file,
406 kref);
Dmitry Torokhov699f6852015-12-14 17:34:08 -0800407 int i;
Erik Gilling01544172013-02-28 16:43:10 -0800408
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200409 for (i = 0; i < sync_file->num_fences; ++i) {
Gustavo Padovanc88b26d2016-01-21 10:49:20 -0200410 fence_remove_callback(sync_file->cbs[i].fence,
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200411 &sync_file->cbs[i].cb);
Gustavo Padovanc88b26d2016-01-21 10:49:20 -0200412 fence_put(sync_file->cbs[i].fence);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200413 }
Erik Gilling01544172013-02-28 16:43:10 -0800414
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200415 kfree(sync_file);
Erik Gilling01544172013-02-28 16:43:10 -0800416}
417
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200418static int sync_file_release(struct inode *inode, struct file *file)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800419{
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200420 struct sync_file *sync_file = file->private_data;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800421
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200422 sync_file_debug_remove(sync_file);
Erik Gillingcc3c5cd2013-02-28 16:43:08 -0800423
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200424 kref_put(&sync_file->kref, sync_file_free);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800425 return 0;
426}
427
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200428static unsigned int sync_file_poll(struct file *file, poll_table *wait)
Erik Gilling57b505b2013-02-28 16:43:04 -0800429{
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200430 struct sync_file *sync_file = file->private_data;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200431 int status;
Erik Gilling57b505b2013-02-28 16:43:04 -0800432
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200433 poll_wait(file, &sync_file->wq, wait);
Erik Gilling57b505b2013-02-28 16:43:04 -0800434
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200435 status = atomic_read(&sync_file->status);
Erik Gillingc6792122013-02-28 16:43:18 -0800436
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200437 if (!status)
Erik Gilling57b505b2013-02-28 16:43:04 -0800438 return POLLIN;
Janani Ravichandran5f9001d2016-02-18 17:39:11 -0500439 if (status < 0)
Erik Gilling57b505b2013-02-28 16:43:04 -0800440 return POLLERR;
Lucas Tanure6a44b502014-07-15 00:32:35 -0300441 return 0;
Erik Gilling57b505b2013-02-28 16:43:04 -0800442}
443
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200444static long sync_file_ioctl_merge(struct sync_file *sync_file,
445 unsigned long arg)
Erik Gilling7ad530b2013-02-28 16:42:57 -0800446{
Yann Droneaud9c6cd3b2013-08-15 15:10:54 +0200447 int fd = get_unused_fd_flags(O_CLOEXEC);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800448 int err;
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200449 struct sync_file *fence2, *fence3;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800450 struct sync_merge_data data;
451
Rebecca Schultz Zavin92ea915a2013-02-28 16:43:12 -0800452 if (fd < 0)
453 return fd;
454
455 if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
456 err = -EFAULT;
457 goto err_put_fd;
458 }
Erik Gilling7ad530b2013-02-28 16:42:57 -0800459
Gustavo Padovan2d75c882016-04-26 12:32:28 -0300460 if (data.flags || data.pad) {
461 err = -EINVAL;
462 goto err_put_fd;
463 }
464
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200465 fence2 = sync_file_fdget(data.fd2);
Ioana Ciornei375fb532015-11-01 16:38:20 +0200466 if (!fence2) {
Erik Gilling7ad530b2013-02-28 16:42:57 -0800467 err = -ENOENT;
468 goto err_put_fd;
469 }
470
471 data.name[sizeof(data.name) - 1] = '\0';
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200472 fence3 = sync_file_merge(data.name, sync_file, fence2);
Ioana Ciornei375fb532015-11-01 16:38:20 +0200473 if (!fence3) {
Erik Gilling7ad530b2013-02-28 16:42:57 -0800474 err = -ENOMEM;
475 goto err_put_fence2;
476 }
477
478 data.fence = fd;
479 if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
480 err = -EFAULT;
481 goto err_put_fence3;
482 }
483
Gustavo Padovan2a7c1db2016-04-28 10:46:49 -0300484 fd_install(fd, fence3->file);
485 fput(fence2->file);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800486 return 0;
487
488err_put_fence3:
Gustavo Padovan2a7c1db2016-04-28 10:46:49 -0300489 fput(fence3->file);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800490
491err_put_fence2:
Gustavo Padovan2a7c1db2016-04-28 10:46:49 -0300492 fput(fence2->file);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800493
494err_put_fd:
495 put_unused_fd(fd);
496 return err;
497}
498
Gustavo Padovan2d75c882016-04-26 12:32:28 -0300499static void sync_fill_fence_info(struct fence *fence,
500 struct sync_fence_info *info)
Erik Gilling79ba1522013-02-28 16:43:02 -0800501{
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200502 strlcpy(info->obj_name, fence->ops->get_timeline_name(fence),
503 sizeof(info->obj_name));
504 strlcpy(info->driver_name, fence->ops->get_driver_name(fence),
Erik Gilling79ba1522013-02-28 16:43:02 -0800505 sizeof(info->driver_name));
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200506 if (fence_is_signaled(fence))
507 info->status = fence->status >= 0 ? 1 : fence->status;
508 else
509 info->status = 0;
510 info->timestamp_ns = ktime_to_ns(fence->timestamp);
Erik Gilling79ba1522013-02-28 16:43:02 -0800511}
512
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200513static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
Erik Gilling79ba1522013-02-28 16:43:02 -0800514 unsigned long arg)
515{
Gustavo Padovan2d75c882016-04-26 12:32:28 -0300516 struct sync_file_info info;
517 struct sync_fence_info *fence_info = NULL;
Erik Gilling79ba1522013-02-28 16:43:02 -0800518 __u32 size;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200519 int ret, i;
Erik Gilling79ba1522013-02-28 16:43:02 -0800520
Gustavo Padovan2d75c882016-04-26 12:32:28 -0300521 if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
Erik Gilling79ba1522013-02-28 16:43:02 -0800522 return -EFAULT;
523
Gustavo Padovan2d75c882016-04-26 12:32:28 -0300524 if (info.flags || info.pad)
Erik Gilling79ba1522013-02-28 16:43:02 -0800525 return -EINVAL;
526
Gustavo Padovan2d75c882016-04-26 12:32:28 -0300527 /*
528 * Passing num_fences = 0 means that userspace doesn't want to
529 * retrieve any sync_fence_info. If num_fences = 0 we skip filling
530 * sync_fence_info and return the actual number of fences on
531 * info->num_fences.
532 */
533 if (!info.num_fences)
534 goto no_fences;
Erik Gilling79ba1522013-02-28 16:43:02 -0800535
Gustavo Padovan2d75c882016-04-26 12:32:28 -0300536 if (info.num_fences < sync_file->num_fences)
537 return -EINVAL;
538
539 size = sync_file->num_fences * sizeof(*fence_info);
540 fence_info = kzalloc(size, GFP_KERNEL);
541 if (!fence_info)
Erik Gilling79ba1522013-02-28 16:43:02 -0800542 return -ENOMEM;
543
Gustavo Padovan2d75c882016-04-26 12:32:28 -0300544 for (i = 0; i < sync_file->num_fences; ++i)
545 sync_fill_fence_info(sync_file->cbs[i].fence, &fence_info[i]);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200546
Gustavo Padovan2d75c882016-04-26 12:32:28 -0300547 if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info,
548 size)) {
549 ret = -EFAULT;
550 goto out;
Erik Gilling79ba1522013-02-28 16:43:02 -0800551 }
552
Gustavo Padovan2d75c882016-04-26 12:32:28 -0300553no_fences:
554 strlcpy(info.name, sync_file->name, sizeof(info.name));
555 info.status = atomic_read(&sync_file->status);
556 if (info.status >= 0)
557 info.status = !info.status;
Erik Gilling79ba1522013-02-28 16:43:02 -0800558
Gustavo Padovan2d75c882016-04-26 12:32:28 -0300559 info.num_fences = sync_file->num_fences;
560
561 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
Erik Gilling79ba1522013-02-28 16:43:02 -0800562 ret = -EFAULT;
563 else
564 ret = 0;
565
566out:
Gustavo Padovan2d75c882016-04-26 12:32:28 -0300567 kfree(fence_info);
Erik Gilling79ba1522013-02-28 16:43:02 -0800568
569 return ret;
570}
Erik Gilling7ad530b2013-02-28 16:42:57 -0800571
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200572static long sync_file_ioctl(struct file *file, unsigned int cmd,
Erik Gilling7ad530b2013-02-28 16:42:57 -0800573 unsigned long arg)
574{
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200575 struct sync_file *sync_file = file->private_data;
Dipak Zope7b1046e2014-08-19 04:21:54 -0400576
Erik Gilling7ad530b2013-02-28 16:42:57 -0800577 switch (cmd) {
Erik Gilling7ad530b2013-02-28 16:42:57 -0800578 case SYNC_IOC_MERGE:
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200579 return sync_file_ioctl_merge(sync_file, arg);
Erik Gillingaf7582f2013-02-28 16:43:00 -0800580
Gustavo Padovan2d75c882016-04-26 12:32:28 -0300581 case SYNC_IOC_FILE_INFO:
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200582 return sync_file_ioctl_fence_info(sync_file, arg);
Erik Gilling79ba1522013-02-28 16:43:02 -0800583
Erik Gilling7ad530b2013-02-28 16:42:57 -0800584 default:
585 return -ENOTTY;
586 }
587}
588
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200589static const struct file_operations sync_file_fops = {
590 .release = sync_file_release,
591 .poll = sync_file_poll,
592 .unlocked_ioctl = sync_file_ioctl,
593 .compat_ioctl = sync_file_ioctl,
Erik Gillingaf7582f2013-02-28 16:43:00 -0800594};
595