drm_syncobj.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright 2017 Red Hat
  3. *
  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:
  10. *
  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
  13. * Software.
  14. *
  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
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. *
  25. */
  26. /**
  27. * DOC: Overview
  28. *
  29. * DRM synchronisation objects (syncobj) are a persistent objects,
  30. * that contain an optional fence. The fence can be updated with a new
  31. * fence, or be NULL.
  32. *
  33. * syncobj's can be export to fd's and back, these fd's are opaque and
  34. * have no other use case, except passing the syncobj between processes.
  35. *
  36. * Their primary use-case is to implement Vulkan fences and semaphores.
  37. *
  38. * syncobj have a kref reference count, but also have an optional file.
  39. * The file is only created once the syncobj is exported.
  40. * The file takes a reference on the kref.
  41. */
  42. #include <drm/drmP.h>
  43. #include <linux/file.h>
  44. #include <linux/fs.h>
  45. #include <linux/anon_inodes.h>
  46. #include <linux/sync_file.h>
  47. #include "drm_internal.h"
  48. #include <drm/drm_syncobj.h>
  49. /**
  50. * drm_syncobj_find - lookup and reference a sync object.
  51. * @file_private: drm file private pointer
  52. * @handle: sync object handle to lookup.
  53. *
  54. * Returns a reference to the syncobj pointed to by handle or NULL.
  55. */
  56. struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
  57. u32 handle)
  58. {
  59. struct drm_syncobj *syncobj;
  60. spin_lock(&file_private->syncobj_table_lock);
  61. /* Check if we currently have a reference on the object */
  62. syncobj = idr_find(&file_private->syncobj_idr, handle);
  63. if (syncobj)
  64. drm_syncobj_get(syncobj);
  65. spin_unlock(&file_private->syncobj_table_lock);
  66. return syncobj;
  67. }
  68. EXPORT_SYMBOL(drm_syncobj_find);
  69. /**
  70. * drm_syncobj_replace_fence - replace fence in a sync object.
  71. * @syncobj: Sync object to replace fence in
  72. * @fence: fence to install in sync file.
  73. *
  74. * This replaces the fence on a sync object.
  75. */
  76. void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
  77. struct dma_fence *fence)
  78. {
  79. struct dma_fence *old_fence;
  80. if (fence)
  81. dma_fence_get(fence);
  82. old_fence = xchg(&syncobj->fence, fence);
  83. dma_fence_put(old_fence);
  84. }
  85. EXPORT_SYMBOL(drm_syncobj_replace_fence);
  86. int drm_syncobj_fence_get(struct drm_file *file_private,
  87. u32 handle,
  88. struct dma_fence **fence)
  89. {
  90. struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
  91. int ret = 0;
  92. if (!syncobj)
  93. return -ENOENT;
  94. *fence = dma_fence_get(syncobj->fence);
  95. if (!*fence) {
  96. ret = -EINVAL;
  97. }
  98. drm_syncobj_put(syncobj);
  99. return ret;
  100. }
  101. EXPORT_SYMBOL(drm_syncobj_fence_get);
  102. /**
  103. * drm_syncobj_free - free a sync object.
  104. * @kref: kref to free.
  105. *
  106. * Only to be called from kref_put in drm_syncobj_put.
  107. */
  108. void drm_syncobj_free(struct kref *kref)
  109. {
  110. struct drm_syncobj *syncobj = container_of(kref,
  111. struct drm_syncobj,
  112. refcount);
  113. dma_fence_put(syncobj->fence);
  114. kfree(syncobj);
  115. }
  116. EXPORT_SYMBOL(drm_syncobj_free);
  117. static int drm_syncobj_create(struct drm_file *file_private,
  118. u32 *handle)
  119. {
  120. int ret;
  121. struct drm_syncobj *syncobj;
  122. syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL);
  123. if (!syncobj)
  124. return -ENOMEM;
  125. kref_init(&syncobj->refcount);
  126. idr_preload(GFP_KERNEL);
  127. spin_lock(&file_private->syncobj_table_lock);
  128. ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
  129. spin_unlock(&file_private->syncobj_table_lock);
  130. idr_preload_end();
  131. if (ret < 0) {
  132. drm_syncobj_put(syncobj);
  133. return ret;
  134. }
  135. *handle = ret;
  136. return 0;
  137. }
  138. static int drm_syncobj_destroy(struct drm_file *file_private,
  139. u32 handle)
  140. {
  141. struct drm_syncobj *syncobj;
  142. spin_lock(&file_private->syncobj_table_lock);
  143. syncobj = idr_remove(&file_private->syncobj_idr, handle);
  144. spin_unlock(&file_private->syncobj_table_lock);
  145. if (!syncobj)
  146. return -EINVAL;
  147. drm_syncobj_put(syncobj);
  148. return 0;
  149. }
  150. static int drm_syncobj_file_release(struct inode *inode, struct file *file)
  151. {
  152. struct drm_syncobj *syncobj = file->private_data;
  153. drm_syncobj_put(syncobj);
  154. return 0;
  155. }
  156. static const struct file_operations drm_syncobj_file_fops = {
  157. .release = drm_syncobj_file_release,
  158. };
  159. static int drm_syncobj_alloc_file(struct drm_syncobj *syncobj)
  160. {
  161. struct file *file = anon_inode_getfile("syncobj_file",
  162. &drm_syncobj_file_fops,
  163. syncobj, 0);
  164. if (IS_ERR(file))
  165. return PTR_ERR(file);
  166. drm_syncobj_get(syncobj);
  167. if (cmpxchg(&syncobj->file, NULL, file)) {
  168. /* lost the race */
  169. fput(file);
  170. }
  171. return 0;
  172. }
  173. static int drm_syncobj_handle_to_fd(struct drm_file *file_private,
  174. u32 handle, int *p_fd)
  175. {
  176. struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
  177. int ret;
  178. int fd;
  179. if (!syncobj)
  180. return -EINVAL;
  181. fd = get_unused_fd_flags(O_CLOEXEC);
  182. if (fd < 0) {
  183. drm_syncobj_put(syncobj);
  184. return fd;
  185. }
  186. if (!syncobj->file) {
  187. ret = drm_syncobj_alloc_file(syncobj);
  188. if (ret)
  189. goto out_put_fd;
  190. }
  191. fd_install(fd, syncobj->file);
  192. drm_syncobj_put(syncobj);
  193. *p_fd = fd;
  194. return 0;
  195. out_put_fd:
  196. put_unused_fd(fd);
  197. drm_syncobj_put(syncobj);
  198. return ret;
  199. }
  200. static struct drm_syncobj *drm_syncobj_fdget(int fd)
  201. {
  202. struct file *file = fget(fd);
  203. if (!file)
  204. return NULL;
  205. if (file->f_op != &drm_syncobj_file_fops)
  206. goto err;
  207. return file->private_data;
  208. err:
  209. fput(file);
  210. return NULL;
  211. };
  212. static int drm_syncobj_fd_to_handle(struct drm_file *file_private,
  213. int fd, u32 *handle)
  214. {
  215. struct drm_syncobj *syncobj = drm_syncobj_fdget(fd);
  216. int ret;
  217. if (!syncobj)
  218. return -EINVAL;
  219. /* take a reference to put in the idr */
  220. drm_syncobj_get(syncobj);
  221. idr_preload(GFP_KERNEL);
  222. spin_lock(&file_private->syncobj_table_lock);
  223. ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
  224. spin_unlock(&file_private->syncobj_table_lock);
  225. idr_preload_end();
  226. if (ret < 0) {
  227. fput(syncobj->file);
  228. return ret;
  229. }
  230. *handle = ret;
  231. return 0;
  232. }
  233. int drm_syncobj_import_sync_file_fence(struct drm_file *file_private,
  234. int fd, int handle)
  235. {
  236. struct dma_fence *fence = sync_file_get_fence(fd);
  237. struct drm_syncobj *syncobj;
  238. if (!fence)
  239. return -EINVAL;
  240. syncobj = drm_syncobj_find(file_private, handle);
  241. if (!syncobj) {
  242. dma_fence_put(fence);
  243. return -ENOENT;
  244. }
  245. drm_syncobj_replace_fence(syncobj, fence);
  246. dma_fence_put(fence);
  247. drm_syncobj_put(syncobj);
  248. return 0;
  249. }
  250. int drm_syncobj_export_sync_file(struct drm_file *file_private,
  251. int handle, int *p_fd)
  252. {
  253. int ret;
  254. struct dma_fence *fence;
  255. struct sync_file *sync_file;
  256. int fd = get_unused_fd_flags(O_CLOEXEC);
  257. if (fd < 0)
  258. return fd;
  259. ret = drm_syncobj_fence_get(file_private, handle, &fence);
  260. if (ret)
  261. goto err_put_fd;
  262. sync_file = sync_file_create(fence);
  263. dma_fence_put(fence);
  264. if (!sync_file) {
  265. ret = -EINVAL;
  266. goto err_put_fd;
  267. }
  268. fd_install(fd, sync_file->file);
  269. *p_fd = fd;
  270. return 0;
  271. err_put_fd:
  272. put_unused_fd(fd);
  273. return ret;
  274. }
  275. /**
  276. * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time
  277. * @file_private: drm file-private structure to set up
  278. *
  279. * Called at device open time, sets up the structure for handling refcounting
  280. * of sync objects.
  281. */
  282. void
  283. drm_syncobj_open(struct drm_file *file_private)
  284. {
  285. idr_init(&file_private->syncobj_idr);
  286. spin_lock_init(&file_private->syncobj_table_lock);
  287. }
  288. static int
  289. drm_syncobj_release_handle(int id, void *ptr, void *data)
  290. {
  291. struct drm_syncobj *syncobj = ptr;
  292. drm_syncobj_put(syncobj);
  293. return 0;
  294. }
  295. /**
  296. * drm_syncobj_release - release file-private sync object resources
  297. * @file_private: drm file-private structure to clean up
  298. *
  299. * Called at close time when the filp is going away.
  300. *
  301. * Releases any remaining references on objects by this filp.
  302. */
  303. void
  304. drm_syncobj_release(struct drm_file *file_private)
  305. {
  306. idr_for_each(&file_private->syncobj_idr,
  307. &drm_syncobj_release_handle, file_private);
  308. idr_destroy(&file_private->syncobj_idr);
  309. }
  310. int
  311. drm_syncobj_create_ioctl(struct drm_device *dev, void *data,
  312. struct drm_file *file_private)
  313. {
  314. struct drm_syncobj_create *args = data;
  315. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  316. return -ENODEV;
  317. /* no valid flags yet */
  318. if (args->flags)
  319. return -EINVAL;
  320. return drm_syncobj_create(file_private,
  321. &args->handle);
  322. }
  323. int
  324. drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data,
  325. struct drm_file *file_private)
  326. {
  327. struct drm_syncobj_destroy *args = data;
  328. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  329. return -ENODEV;
  330. /* make sure padding is empty */
  331. if (args->pad)
  332. return -EINVAL;
  333. return drm_syncobj_destroy(file_private, args->handle);
  334. }
  335. int
  336. drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  337. struct drm_file *file_private)
  338. {
  339. struct drm_syncobj_handle *args = data;
  340. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  341. return -ENODEV;
  342. if (args->pad)
  343. return -EINVAL;
  344. if (args->flags != 0 &&
  345. args->flags != DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
  346. return -EINVAL;
  347. if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
  348. return drm_syncobj_export_sync_file(file_private, args->handle,
  349. &args->fd);
  350. return drm_syncobj_handle_to_fd(file_private, args->handle,
  351. &args->fd);
  352. }
  353. int
  354. drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data,
  355. struct drm_file *file_private)
  356. {
  357. struct drm_syncobj_handle *args = data;
  358. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  359. return -ENODEV;
  360. if (args->pad)
  361. return -EINVAL;
  362. if (args->flags != 0 &&
  363. args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
  364. return -EINVAL;
  365. if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
  366. return drm_syncobj_import_sync_file_fence(file_private,
  367. args->fd,
  368. args->handle);
  369. return drm_syncobj_fd_to_handle(file_private, args->fd,
  370. &args->handle);
  371. }