]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/staging/android/sw_sync.c
staging/android: remove 'destroyed' member from struct sync_timeline
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / android / sw_sync.c
CommitLineData
1867a23b
GP
1/*
2 * drivers/dma-buf/sw_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
17#include <linux/file.h>
18#include <linux/fs.h>
19#include <linux/uaccess.h>
aff9da10 20#include <linux/slab.h>
1867a23b
GP
21#include <linux/sync_file.h>
22
1867a23b
GP
23#include "sync.h"
24
aff9da10
GP
25#define CREATE_TRACE_POINTS
26#include "trace/sync.h"
27
6f65aa89
GP
28struct sw_sync_create_fence_data {
29 __u32 value;
30 char name[32];
31 __s32 fence; /* fd of new fence */
32};
33
34#define SW_SYNC_IOC_MAGIC 'W'
35
36#define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0,\
37 struct sw_sync_create_fence_data)
38#define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
39
aff9da10
GP
40static const struct fence_ops timeline_fence_ops;
41
42static inline struct sync_pt *fence_to_sync_pt(struct fence *fence)
43{
44 if (fence->ops != &timeline_fence_ops)
45 return NULL;
46 return container_of(fence, struct sync_pt, base);
47}
48
49/**
50 * sync_timeline_create() - creates a sync object
51 * @drv_name: sync_timeline driver name
52 * @name: sync_timeline name
53 *
54 * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
55 * case of error.
56 */
57struct sync_timeline *sync_timeline_create(const char *drv_name,
58 const char *name)
59{
60 struct sync_timeline *obj;
61
62 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
63 if (!obj)
64 return NULL;
65
66 kref_init(&obj->kref);
67 obj->context = fence_context_alloc(1);
68 strlcpy(obj->name, name, sizeof(obj->name));
69 strlcpy(obj->drv_name, drv_name, sizeof(obj->drv_name));
70
71 INIT_LIST_HEAD(&obj->child_list_head);
72 INIT_LIST_HEAD(&obj->active_list_head);
73 spin_lock_init(&obj->child_list_lock);
74
75 sync_timeline_debug_add(obj);
76
77 return obj;
78}
79
80static void sync_timeline_free(struct kref *kref)
81{
82 struct sync_timeline *obj =
83 container_of(kref, struct sync_timeline, kref);
84
85 sync_timeline_debug_remove(obj);
86
87 kfree(obj);
88}
89
90static void sync_timeline_get(struct sync_timeline *obj)
91{
92 kref_get(&obj->kref);
93}
94
95static void sync_timeline_put(struct sync_timeline *obj)
96{
97 kref_put(&obj->kref, sync_timeline_free);
98}
99
100/**
101 * sync_timeline_destroy() - destroys a sync object
102 * @obj: sync_timeline to destroy
103 *
104 * A sync implementation should call this when the @obj is going away
105 * (i.e. module unload.) @obj won't actually be freed until all its children
106 * fences are freed.
107 */
108static void sync_timeline_destroy(struct sync_timeline *obj)
109{
aff9da10
GP
110 smp_wmb();
111
112 sync_timeline_put(obj);
113}
114
115/**
116 * sync_timeline_signal() - signal a status change on a sync_timeline
117 * @obj: sync_timeline to signal
118 * @inc: num to increment on timeline->value
119 *
120 * A sync implementation should call this any time one of it's fences
121 * has signaled or has an error condition.
122 */
123static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
124{
125 unsigned long flags;
126 struct sync_pt *pt, *next;
127
128 trace_sync_timeline(obj);
129
130 spin_lock_irqsave(&obj->child_list_lock, flags);
131
132 obj->value += inc;
133
134 list_for_each_entry_safe(pt, next, &obj->active_list_head,
135 active_list) {
136 if (fence_is_signaled_locked(&pt->base))
137 list_del_init(&pt->active_list);
138 }
139
140 spin_unlock_irqrestore(&obj->child_list_lock, flags);
141}
142
143/**
144 * sync_pt_create() - creates a sync pt
145 * @parent: fence's parent sync_timeline
146 * @size: size to allocate for this pt
147 * @inc: value of the fence
148 *
149 * Creates a new sync_pt as a child of @parent. @size bytes will be
150 * allocated allowing for implementation specific data to be kept after
151 * the generic sync_timeline struct. Returns the sync_pt object or
152 * NULL in case of error.
153 */
154static struct sync_pt *sync_pt_create(struct sync_timeline *obj, int size,
155 unsigned int value)
156{
157 unsigned long flags;
158 struct sync_pt *pt;
159
160 if (size < sizeof(*pt))
161 return NULL;
162
163 pt = kzalloc(size, GFP_KERNEL);
164 if (!pt)
165 return NULL;
166
167 spin_lock_irqsave(&obj->child_list_lock, flags);
168 sync_timeline_get(obj);
169 fence_init(&pt->base, &timeline_fence_ops, &obj->child_list_lock,
170 obj->context, value);
171 list_add_tail(&pt->child_list, &obj->child_list_head);
172 INIT_LIST_HEAD(&pt->active_list);
173 spin_unlock_irqrestore(&obj->child_list_lock, flags);
174 return pt;
175}
176
177static const char *timeline_fence_get_driver_name(struct fence *fence)
178{
179 struct sync_timeline *parent = fence_parent(fence);
180
181 return parent->drv_name;
182}
183
184static const char *timeline_fence_get_timeline_name(struct fence *fence)
185{
186 struct sync_timeline *parent = fence_parent(fence);
187
188 return parent->name;
189}
190
191static void timeline_fence_release(struct fence *fence)
192{
193 struct sync_pt *pt = fence_to_sync_pt(fence);
194 struct sync_timeline *parent = fence_parent(fence);
195 unsigned long flags;
196
197 spin_lock_irqsave(fence->lock, flags);
198 list_del(&pt->child_list);
199 if (WARN_ON_ONCE(!list_empty(&pt->active_list)))
200 list_del(&pt->active_list);
201 spin_unlock_irqrestore(fence->lock, flags);
202
203 sync_timeline_put(parent);
204 fence_free(fence);
205}
206
207static bool timeline_fence_signaled(struct fence *fence)
208{
209 struct sync_timeline *parent = fence_parent(fence);
210
211 return (fence->seqno > parent->value) ? false : true;
212}
213
214static bool timeline_fence_enable_signaling(struct fence *fence)
215{
216 struct sync_pt *pt = fence_to_sync_pt(fence);
217 struct sync_timeline *parent = fence_parent(fence);
218
219 if (timeline_fence_signaled(fence))
220 return false;
221
222 list_add_tail(&pt->active_list, &parent->active_list_head);
223 return true;
224}
225
226static void timeline_fence_value_str(struct fence *fence,
227 char *str, int size)
228{
229 snprintf(str, size, "%d", fence->seqno);
230}
231
232static void timeline_fence_timeline_value_str(struct fence *fence,
233 char *str, int size)
234{
235 struct sync_timeline *parent = fence_parent(fence);
236
237 snprintf(str, size, "%d", parent->value);
238}
239
240static const struct fence_ops timeline_fence_ops = {
241 .get_driver_name = timeline_fence_get_driver_name,
242 .get_timeline_name = timeline_fence_get_timeline_name,
243 .enable_signaling = timeline_fence_enable_signaling,
244 .signaled = timeline_fence_signaled,
245 .wait = fence_default_wait,
246 .release = timeline_fence_release,
247 .fence_value_str = timeline_fence_value_str,
248 .timeline_value_str = timeline_fence_timeline_value_str,
249};
250
1867a23b
GP
251/*
252 * *WARNING*
253 *
254 * improper use of this can result in deadlocking kernel drivers from userspace.
255 */
256
257/* opening sw_sync create a new sync obj */
258static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
259{
260 struct sync_timeline *obj;
261 char task_comm[TASK_COMM_LEN];
262
263 get_task_comm(task_comm, current);
264
265 obj = sync_timeline_create("sw_sync", task_comm);
266 if (!obj)
267 return -ENOMEM;
268
269 file->private_data = obj;
270
271 return 0;
272}
273
274static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
275{
276 struct sync_timeline *obj = file->private_data;
277
278 sync_timeline_destroy(obj);
279 return 0;
280}
281
282static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
283 unsigned long arg)
284{
285 int fd = get_unused_fd_flags(O_CLOEXEC);
286 int err;
287 struct sync_pt *pt;
288 struct sync_file *sync_file;
289 struct sw_sync_create_fence_data data;
290
291 if (fd < 0)
292 return fd;
293
294 if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
295 err = -EFAULT;
296 goto err;
297 }
298
299 pt = sync_pt_create(obj, sizeof(*pt), data.value);
300 if (!pt) {
301 err = -ENOMEM;
302 goto err;
303 }
304
305 sync_file = sync_file_create(&pt->base);
306 if (!sync_file) {
307 fence_put(&pt->base);
308 err = -ENOMEM;
309 goto err;
310 }
311
312 data.fence = fd;
313 if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
314 fput(sync_file->file);
315 err = -EFAULT;
316 goto err;
317 }
318
319 fd_install(fd, sync_file->file);
320
321 return 0;
322
323err:
324 put_unused_fd(fd);
325 return err;
326}
327
328static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
329{
330 u32 value;
331
332 if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
333 return -EFAULT;
334
335 sync_timeline_signal(obj, value);
336
337 return 0;
338}
339
340static long sw_sync_ioctl(struct file *file, unsigned int cmd,
341 unsigned long arg)
342{
343 struct sync_timeline *obj = file->private_data;
344
345 switch (cmd) {
346 case SW_SYNC_IOC_CREATE_FENCE:
347 return sw_sync_ioctl_create_fence(obj, arg);
348
349 case SW_SYNC_IOC_INC:
350 return sw_sync_ioctl_inc(obj, arg);
351
352 default:
353 return -ENOTTY;
354 }
355}
356
357const struct file_operations sw_sync_debugfs_fops = {
358 .open = sw_sync_debugfs_open,
359 .release = sw_sync_debugfs_release,
360 .unlocked_ioctl = sw_sync_ioctl,
361 .compat_ioctl = sw_sync_ioctl,
362};