2 * drivers/dma-buf/sw_sync.c
4 * Copyright (C) 2012 Google, Inc.
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.
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.
17 #include <linux/file.h>
19 #include <linux/uaccess.h>
20 #include <linux/slab.h>
21 #include <linux/sync_file.h>
23 #include "sync_debug.h"
25 #define CREATE_TRACE_POINTS
26 #include "trace/sync.h"
28 struct sw_sync_create_fence_data
{
31 __s32 fence
; /* fd of new fence */
34 #define SW_SYNC_IOC_MAGIC 'W'
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)
40 static const struct fence_ops timeline_fence_ops
;
42 static inline struct sync_pt
*fence_to_sync_pt(struct fence
*fence
)
44 if (fence
->ops
!= &timeline_fence_ops
)
46 return container_of(fence
, struct sync_pt
, base
);
50 * sync_timeline_create() - creates a sync object
51 * @name: sync_timeline name
53 * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
56 struct sync_timeline
*sync_timeline_create(const char *name
)
58 struct sync_timeline
*obj
;
60 obj
= kzalloc(sizeof(*obj
), GFP_KERNEL
);
64 kref_init(&obj
->kref
);
65 obj
->context
= fence_context_alloc(1);
66 strlcpy(obj
->name
, name
, sizeof(obj
->name
));
68 INIT_LIST_HEAD(&obj
->child_list_head
);
69 INIT_LIST_HEAD(&obj
->active_list_head
);
70 spin_lock_init(&obj
->child_list_lock
);
72 sync_timeline_debug_add(obj
);
77 static void sync_timeline_free(struct kref
*kref
)
79 struct sync_timeline
*obj
=
80 container_of(kref
, struct sync_timeline
, kref
);
82 sync_timeline_debug_remove(obj
);
87 static void sync_timeline_get(struct sync_timeline
*obj
)
92 static void sync_timeline_put(struct sync_timeline
*obj
)
94 kref_put(&obj
->kref
, sync_timeline_free
);
98 * sync_timeline_signal() - signal a status change on a sync_timeline
99 * @obj: sync_timeline to signal
100 * @inc: num to increment on timeline->value
102 * A sync implementation should call this any time one of it's fences
103 * has signaled or has an error condition.
105 static void sync_timeline_signal(struct sync_timeline
*obj
, unsigned int inc
)
108 struct sync_pt
*pt
, *next
;
110 trace_sync_timeline(obj
);
112 spin_lock_irqsave(&obj
->child_list_lock
, flags
);
116 list_for_each_entry_safe(pt
, next
, &obj
->active_list_head
,
118 if (fence_is_signaled_locked(&pt
->base
))
119 list_del_init(&pt
->active_list
);
122 spin_unlock_irqrestore(&obj
->child_list_lock
, flags
);
126 * sync_pt_create() - creates a sync pt
127 * @parent: fence's parent sync_timeline
128 * @size: size to allocate for this pt
129 * @inc: value of the fence
131 * Creates a new sync_pt as a child of @parent. @size bytes will be
132 * allocated allowing for implementation specific data to be kept after
133 * the generic sync_timeline struct. Returns the sync_pt object or
134 * NULL in case of error.
136 static struct sync_pt
*sync_pt_create(struct sync_timeline
*obj
, int size
,
142 if (size
< sizeof(*pt
))
145 pt
= kzalloc(size
, GFP_KERNEL
);
149 spin_lock_irqsave(&obj
->child_list_lock
, flags
);
150 sync_timeline_get(obj
);
151 fence_init(&pt
->base
, &timeline_fence_ops
, &obj
->child_list_lock
,
152 obj
->context
, value
);
153 list_add_tail(&pt
->child_list
, &obj
->child_list_head
);
154 INIT_LIST_HEAD(&pt
->active_list
);
155 spin_unlock_irqrestore(&obj
->child_list_lock
, flags
);
159 static const char *timeline_fence_get_driver_name(struct fence
*fence
)
164 static const char *timeline_fence_get_timeline_name(struct fence
*fence
)
166 struct sync_timeline
*parent
= fence_parent(fence
);
171 static void timeline_fence_release(struct fence
*fence
)
173 struct sync_pt
*pt
= fence_to_sync_pt(fence
);
174 struct sync_timeline
*parent
= fence_parent(fence
);
177 spin_lock_irqsave(fence
->lock
, flags
);
178 list_del(&pt
->child_list
);
179 if (WARN_ON_ONCE(!list_empty(&pt
->active_list
)))
180 list_del(&pt
->active_list
);
181 spin_unlock_irqrestore(fence
->lock
, flags
);
183 sync_timeline_put(parent
);
187 static bool timeline_fence_signaled(struct fence
*fence
)
189 struct sync_timeline
*parent
= fence_parent(fence
);
191 return (fence
->seqno
> parent
->value
) ? false : true;
194 static bool timeline_fence_enable_signaling(struct fence
*fence
)
196 struct sync_pt
*pt
= fence_to_sync_pt(fence
);
197 struct sync_timeline
*parent
= fence_parent(fence
);
199 if (timeline_fence_signaled(fence
))
202 list_add_tail(&pt
->active_list
, &parent
->active_list_head
);
206 static void timeline_fence_value_str(struct fence
*fence
,
209 snprintf(str
, size
, "%d", fence
->seqno
);
212 static void timeline_fence_timeline_value_str(struct fence
*fence
,
215 struct sync_timeline
*parent
= fence_parent(fence
);
217 snprintf(str
, size
, "%d", parent
->value
);
220 static const struct fence_ops timeline_fence_ops
= {
221 .get_driver_name
= timeline_fence_get_driver_name
,
222 .get_timeline_name
= timeline_fence_get_timeline_name
,
223 .enable_signaling
= timeline_fence_enable_signaling
,
224 .signaled
= timeline_fence_signaled
,
225 .wait
= fence_default_wait
,
226 .release
= timeline_fence_release
,
227 .fence_value_str
= timeline_fence_value_str
,
228 .timeline_value_str
= timeline_fence_timeline_value_str
,
234 * improper use of this can result in deadlocking kernel drivers from userspace.
237 /* opening sw_sync create a new sync obj */
238 static int sw_sync_debugfs_open(struct inode
*inode
, struct file
*file
)
240 struct sync_timeline
*obj
;
241 char task_comm
[TASK_COMM_LEN
];
243 get_task_comm(task_comm
, current
);
245 obj
= sync_timeline_create(task_comm
);
249 file
->private_data
= obj
;
254 static int sw_sync_debugfs_release(struct inode
*inode
, struct file
*file
)
256 struct sync_timeline
*obj
= file
->private_data
;
260 sync_timeline_put(obj
);
264 static long sw_sync_ioctl_create_fence(struct sync_timeline
*obj
,
267 int fd
= get_unused_fd_flags(O_CLOEXEC
);
270 struct sync_file
*sync_file
;
271 struct sw_sync_create_fence_data data
;
276 if (copy_from_user(&data
, (void __user
*)arg
, sizeof(data
))) {
281 pt
= sync_pt_create(obj
, sizeof(*pt
), data
.value
);
287 sync_file
= sync_file_create(&pt
->base
);
289 fence_put(&pt
->base
);
295 if (copy_to_user((void __user
*)arg
, &data
, sizeof(data
))) {
296 fput(sync_file
->file
);
301 fd_install(fd
, sync_file
->file
);
310 static long sw_sync_ioctl_inc(struct sync_timeline
*obj
, unsigned long arg
)
314 if (copy_from_user(&value
, (void __user
*)arg
, sizeof(value
)))
317 sync_timeline_signal(obj
, value
);
322 static long sw_sync_ioctl(struct file
*file
, unsigned int cmd
,
325 struct sync_timeline
*obj
= file
->private_data
;
328 case SW_SYNC_IOC_CREATE_FENCE
:
329 return sw_sync_ioctl_create_fence(obj
, arg
);
331 case SW_SYNC_IOC_INC
:
332 return sw_sync_ioctl_inc(obj
, arg
);
339 const struct file_operations sw_sync_debugfs_fops
= {
340 .open
= sw_sync_debugfs_open
,
341 .release
= sw_sync_debugfs_release
,
342 .unlocked_ioctl
= sw_sync_ioctl
,
343 .compat_ioctl
= sw_sync_ioctl
,