2 * Copyright 2017 Red Hat
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 * DRM synchronisation objects (syncobj) are a persistent objects,
31 * that contain an optional fence. The fence can be updated with a new
34 * syncobj's can be export to fd's and back, these fd's are opaque and
35 * have no other use case, except passing the syncobj between processes.
37 * Their primary use-case is to implement Vulkan fences and semaphores.
39 * syncobj have a kref reference count, but also have an optional file.
40 * The file is only created once the syncobj is exported.
41 * The file takes a reference on the kref.
45 #include <linux/file.h>
47 #include <linux/anon_inodes.h>
48 #include <linux/sync_file.h>
50 #include "drm_internal.h"
51 #include <drm/drm_syncobj.h>
54 * drm_syncobj_find - lookup and reference a sync object.
55 * @file_private: drm file private pointer
56 * @handle: sync object handle to lookup.
58 * Returns a reference to the syncobj pointed to by handle or NULL.
60 struct drm_syncobj
*drm_syncobj_find(struct drm_file
*file_private
,
63 struct drm_syncobj
*syncobj
;
65 spin_lock(&file_private
->syncobj_table_lock
);
67 /* Check if we currently have a reference on the object */
68 syncobj
= idr_find(&file_private
->syncobj_idr
, handle
);
70 drm_syncobj_get(syncobj
);
72 spin_unlock(&file_private
->syncobj_table_lock
);
76 EXPORT_SYMBOL(drm_syncobj_find
);
79 * drm_syncobj_replace_fence - replace fence in a sync object.
80 * @syncobj: Sync object to replace fence in
81 * @fence: fence to install in sync file.
83 * This replaces the fence on a sync object.
85 void drm_syncobj_replace_fence(struct drm_syncobj
*syncobj
,
86 struct dma_fence
*fence
)
88 struct dma_fence
*old_fence
;
92 old_fence
= xchg(&syncobj
->fence
, fence
);
94 dma_fence_put(old_fence
);
96 EXPORT_SYMBOL(drm_syncobj_replace_fence
);
98 int drm_syncobj_fence_get(struct drm_file
*file_private
,
100 struct dma_fence
**fence
)
102 struct drm_syncobj
*syncobj
= drm_syncobj_find(file_private
, handle
);
108 *fence
= dma_fence_get(syncobj
->fence
);
112 drm_syncobj_put(syncobj
);
115 EXPORT_SYMBOL(drm_syncobj_fence_get
);
118 * drm_syncobj_free - free a sync object.
119 * @kref: kref to free.
121 * Only to be called from kref_put in drm_syncobj_put.
123 void drm_syncobj_free(struct kref
*kref
)
125 struct drm_syncobj
*syncobj
= container_of(kref
,
128 dma_fence_put(syncobj
->fence
);
131 EXPORT_SYMBOL(drm_syncobj_free
);
133 static int drm_syncobj_create(struct drm_file
*file_private
,
137 struct drm_syncobj
*syncobj
;
139 syncobj
= kzalloc(sizeof(struct drm_syncobj
), GFP_KERNEL
);
143 kref_init(&syncobj
->refcount
);
145 idr_preload(GFP_KERNEL
);
146 spin_lock(&file_private
->syncobj_table_lock
);
147 ret
= idr_alloc(&file_private
->syncobj_idr
, syncobj
, 1, 0, GFP_NOWAIT
);
148 spin_unlock(&file_private
->syncobj_table_lock
);
153 drm_syncobj_put(syncobj
);
161 static int drm_syncobj_destroy(struct drm_file
*file_private
,
164 struct drm_syncobj
*syncobj
;
166 spin_lock(&file_private
->syncobj_table_lock
);
167 syncobj
= idr_remove(&file_private
->syncobj_idr
, handle
);
168 spin_unlock(&file_private
->syncobj_table_lock
);
173 drm_syncobj_put(syncobj
);
177 static int drm_syncobj_file_release(struct inode
*inode
, struct file
*file
)
179 struct drm_syncobj
*syncobj
= file
->private_data
;
181 drm_syncobj_put(syncobj
);
185 static const struct file_operations drm_syncobj_file_fops
= {
186 .release
= drm_syncobj_file_release
,
189 static int drm_syncobj_alloc_file(struct drm_syncobj
*syncobj
)
191 struct file
*file
= anon_inode_getfile("syncobj_file",
192 &drm_syncobj_file_fops
,
195 return PTR_ERR(file
);
197 drm_syncobj_get(syncobj
);
198 if (cmpxchg(&syncobj
->file
, NULL
, file
)) {
206 static int drm_syncobj_handle_to_fd(struct drm_file
*file_private
,
207 u32 handle
, int *p_fd
)
209 struct drm_syncobj
*syncobj
= drm_syncobj_find(file_private
, handle
);
216 fd
= get_unused_fd_flags(O_CLOEXEC
);
218 drm_syncobj_put(syncobj
);
222 if (!syncobj
->file
) {
223 ret
= drm_syncobj_alloc_file(syncobj
);
227 fd_install(fd
, syncobj
->file
);
228 drm_syncobj_put(syncobj
);
233 drm_syncobj_put(syncobj
);
237 static struct drm_syncobj
*drm_syncobj_fdget(int fd
)
239 struct file
*file
= fget(fd
);
243 if (file
->f_op
!= &drm_syncobj_file_fops
)
246 return file
->private_data
;
252 static int drm_syncobj_fd_to_handle(struct drm_file
*file_private
,
255 struct drm_syncobj
*syncobj
= drm_syncobj_fdget(fd
);
261 /* take a reference to put in the idr */
262 drm_syncobj_get(syncobj
);
264 idr_preload(GFP_KERNEL
);
265 spin_lock(&file_private
->syncobj_table_lock
);
266 ret
= idr_alloc(&file_private
->syncobj_idr
, syncobj
, 1, 0, GFP_NOWAIT
);
267 spin_unlock(&file_private
->syncobj_table_lock
);
278 int drm_syncobj_import_sync_file_fence(struct drm_file
*file_private
,
281 struct dma_fence
*fence
= sync_file_get_fence(fd
);
282 struct drm_syncobj
*syncobj
;
287 syncobj
= drm_syncobj_find(file_private
, handle
);
289 dma_fence_put(fence
);
293 drm_syncobj_replace_fence(syncobj
, fence
);
294 dma_fence_put(fence
);
295 drm_syncobj_put(syncobj
);
299 int drm_syncobj_export_sync_file(struct drm_file
*file_private
,
300 int handle
, int *p_fd
)
303 struct dma_fence
*fence
;
304 struct sync_file
*sync_file
;
305 int fd
= get_unused_fd_flags(O_CLOEXEC
);
310 ret
= drm_syncobj_fence_get(file_private
, handle
, &fence
);
314 sync_file
= sync_file_create(fence
);
316 dma_fence_put(fence
);
323 fd_install(fd
, sync_file
->file
);
332 * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time
333 * @dev: drm_device which is being opened by userspace
334 * @file_private: drm file-private structure to set up
336 * Called at device open time, sets up the structure for handling refcounting
340 drm_syncobj_open(struct drm_file
*file_private
)
342 idr_init(&file_private
->syncobj_idr
);
343 spin_lock_init(&file_private
->syncobj_table_lock
);
347 drm_syncobj_release_handle(int id
, void *ptr
, void *data
)
349 struct drm_syncobj
*syncobj
= ptr
;
351 drm_syncobj_put(syncobj
);
356 * drm_syncobj_release - release file-private sync object resources
357 * @dev: drm_device which is being closed by userspace
358 * @file_private: drm file-private structure to clean up
360 * Called at close time when the filp is going away.
362 * Releases any remaining references on objects by this filp.
365 drm_syncobj_release(struct drm_file
*file_private
)
367 idr_for_each(&file_private
->syncobj_idr
,
368 &drm_syncobj_release_handle
, file_private
);
369 idr_destroy(&file_private
->syncobj_idr
);
373 drm_syncobj_create_ioctl(struct drm_device
*dev
, void *data
,
374 struct drm_file
*file_private
)
376 struct drm_syncobj_create
*args
= data
;
378 if (!drm_core_check_feature(dev
, DRIVER_SYNCOBJ
))
381 /* no valid flags yet */
385 return drm_syncobj_create(file_private
,
390 drm_syncobj_destroy_ioctl(struct drm_device
*dev
, void *data
,
391 struct drm_file
*file_private
)
393 struct drm_syncobj_destroy
*args
= data
;
395 if (!drm_core_check_feature(dev
, DRIVER_SYNCOBJ
))
398 /* make sure padding is empty */
401 return drm_syncobj_destroy(file_private
, args
->handle
);
405 drm_syncobj_handle_to_fd_ioctl(struct drm_device
*dev
, void *data
,
406 struct drm_file
*file_private
)
408 struct drm_syncobj_handle
*args
= data
;
410 if (!drm_core_check_feature(dev
, DRIVER_SYNCOBJ
))
416 if (args
->flags
!= 0 &&
417 args
->flags
!= DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE
)
420 if (args
->flags
& DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE
)
421 return drm_syncobj_export_sync_file(file_private
, args
->handle
,
424 return drm_syncobj_handle_to_fd(file_private
, args
->handle
,
429 drm_syncobj_fd_to_handle_ioctl(struct drm_device
*dev
, void *data
,
430 struct drm_file
*file_private
)
432 struct drm_syncobj_handle
*args
= data
;
434 if (!drm_core_check_feature(dev
, DRIVER_SYNCOBJ
))
440 if (args
->flags
!= 0 &&
441 args
->flags
!= DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE
)
444 if (args
->flags
& DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE
)
445 return drm_syncobj_import_sync_file_fence(file_private
,
449 return drm_syncobj_fd_to_handle(file_private
, args
->fd
,