drm_syncobj.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 "drm_internal.h"
  47. #include <drm/drm_syncobj.h>
  48. /**
  49. * drm_syncobj_find - lookup and reference a sync object.
  50. * @file_private: drm file private pointer
  51. * @handle: sync object handle to lookup.
  52. *
  53. * Returns a reference to the syncobj pointed to by handle or NULL.
  54. */
  55. struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
  56. u32 handle)
  57. {
  58. struct drm_syncobj *syncobj;
  59. spin_lock(&file_private->syncobj_table_lock);
  60. /* Check if we currently have a reference on the object */
  61. syncobj = idr_find(&file_private->syncobj_idr, handle);
  62. if (syncobj)
  63. drm_syncobj_get(syncobj);
  64. spin_unlock(&file_private->syncobj_table_lock);
  65. return syncobj;
  66. }
  67. EXPORT_SYMBOL(drm_syncobj_find);
  68. /**
  69. * drm_syncobj_replace_fence - replace fence in a sync object.
  70. * @file_private: drm file private pointer.
  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_file *file_private,
  77. struct drm_syncobj *syncobj,
  78. struct dma_fence *fence)
  79. {
  80. struct dma_fence *old_fence = NULL;
  81. if (fence)
  82. dma_fence_get(fence);
  83. old_fence = xchg(&syncobj->fence, fence);
  84. dma_fence_put(old_fence);
  85. }
  86. EXPORT_SYMBOL(drm_syncobj_replace_fence);
  87. int drm_syncobj_fence_get(struct drm_file *file_private,
  88. u32 handle,
  89. struct dma_fence **fence)
  90. {
  91. struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
  92. int ret = 0;
  93. if (!syncobj)
  94. return -ENOENT;
  95. *fence = dma_fence_get(syncobj->fence);
  96. if (!*fence) {
  97. ret = -EINVAL;
  98. }
  99. drm_syncobj_put(syncobj);
  100. return ret;
  101. }
  102. EXPORT_SYMBOL(drm_syncobj_fence_get);
  103. /**
  104. * drm_syncobj_free - free a sync object.
  105. * @kref: kref to free.
  106. *
  107. * Only to be called from kref_put in drm_syncobj_put.
  108. */
  109. void drm_syncobj_free(struct kref *kref)
  110. {
  111. struct drm_syncobj *syncobj = container_of(kref,
  112. struct drm_syncobj,
  113. refcount);
  114. dma_fence_put(syncobj->fence);
  115. kfree(syncobj);
  116. }
  117. EXPORT_SYMBOL(drm_syncobj_free);
  118. static int drm_syncobj_create(struct drm_file *file_private,
  119. u32 *handle)
  120. {
  121. int ret;
  122. struct drm_syncobj *syncobj;
  123. syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL);
  124. if (!syncobj)
  125. return -ENOMEM;
  126. kref_init(&syncobj->refcount);
  127. idr_preload(GFP_KERNEL);
  128. spin_lock(&file_private->syncobj_table_lock);
  129. ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
  130. spin_unlock(&file_private->syncobj_table_lock);
  131. idr_preload_end();
  132. if (ret < 0) {
  133. drm_syncobj_put(syncobj);
  134. return ret;
  135. }
  136. *handle = ret;
  137. return 0;
  138. }
  139. static int drm_syncobj_destroy(struct drm_file *file_private,
  140. u32 handle)
  141. {
  142. struct drm_syncobj *syncobj;
  143. spin_lock(&file_private->syncobj_table_lock);
  144. syncobj = idr_remove(&file_private->syncobj_idr, handle);
  145. spin_unlock(&file_private->syncobj_table_lock);
  146. if (!syncobj)
  147. return -EINVAL;
  148. drm_syncobj_put(syncobj);
  149. return 0;
  150. }
  151. static int drm_syncobj_file_release(struct inode *inode, struct file *file)
  152. {
  153. struct drm_syncobj *syncobj = file->private_data;
  154. drm_syncobj_put(syncobj);
  155. return 0;
  156. }
  157. static const struct file_operations drm_syncobj_file_fops = {
  158. .release = drm_syncobj_file_release,
  159. };
  160. static int drm_syncobj_alloc_file(struct drm_syncobj *syncobj)
  161. {
  162. struct file *file = anon_inode_getfile("syncobj_file",
  163. &drm_syncobj_file_fops,
  164. syncobj, 0);
  165. if (IS_ERR(file))
  166. return PTR_ERR(file);
  167. drm_syncobj_get(syncobj);
  168. if (cmpxchg(&syncobj->file, NULL, file)) {
  169. /* lost the race */
  170. fput(file);
  171. }
  172. return 0;
  173. }
  174. static int drm_syncobj_handle_to_fd(struct drm_file *file_private,
  175. u32 handle, int *p_fd)
  176. {
  177. struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
  178. int ret;
  179. int fd;
  180. if (!syncobj)
  181. return -EINVAL;
  182. fd = get_unused_fd_flags(O_CLOEXEC);
  183. if (fd < 0) {
  184. drm_syncobj_put(syncobj);
  185. return fd;
  186. }
  187. if (!syncobj->file) {
  188. ret = drm_syncobj_alloc_file(syncobj);
  189. if (ret)
  190. goto out_put_fd;
  191. }
  192. fd_install(fd, syncobj->file);
  193. drm_syncobj_put(syncobj);
  194. *p_fd = fd;
  195. return 0;
  196. out_put_fd:
  197. put_unused_fd(fd);
  198. drm_syncobj_put(syncobj);
  199. return ret;
  200. }
  201. static struct drm_syncobj *drm_syncobj_fdget(int fd)
  202. {
  203. struct file *file = fget(fd);
  204. if (!file)
  205. return NULL;
  206. if (file->f_op != &drm_syncobj_file_fops)
  207. goto err;
  208. return file->private_data;
  209. err:
  210. fput(file);
  211. return NULL;
  212. };
  213. static int drm_syncobj_fd_to_handle(struct drm_file *file_private,
  214. int fd, u32 *handle)
  215. {
  216. struct drm_syncobj *syncobj = drm_syncobj_fdget(fd);
  217. int ret;
  218. if (!syncobj)
  219. return -EINVAL;
  220. /* take a reference to put in the idr */
  221. drm_syncobj_get(syncobj);
  222. idr_preload(GFP_KERNEL);
  223. spin_lock(&file_private->syncobj_table_lock);
  224. ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
  225. spin_unlock(&file_private->syncobj_table_lock);
  226. idr_preload_end();
  227. if (ret < 0) {
  228. fput(syncobj->file);
  229. return ret;
  230. }
  231. *handle = ret;
  232. return 0;
  233. }
  234. /**
  235. * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time
  236. * @dev: drm_device which is being opened by userspace
  237. * @file_private: drm file-private structure to set up
  238. *
  239. * Called at device open time, sets up the structure for handling refcounting
  240. * of sync objects.
  241. */
  242. void
  243. drm_syncobj_open(struct drm_file *file_private)
  244. {
  245. idr_init(&file_private->syncobj_idr);
  246. spin_lock_init(&file_private->syncobj_table_lock);
  247. }
  248. static int
  249. drm_syncobj_release_handle(int id, void *ptr, void *data)
  250. {
  251. struct drm_syncobj *syncobj = ptr;
  252. drm_syncobj_put(syncobj);
  253. return 0;
  254. }
  255. /**
  256. * drm_syncobj_release - release file-private sync object resources
  257. * @dev: drm_device which is being closed by userspace
  258. * @file_private: drm file-private structure to clean up
  259. *
  260. * Called at close time when the filp is going away.
  261. *
  262. * Releases any remaining references on objects by this filp.
  263. */
  264. void
  265. drm_syncobj_release(struct drm_file *file_private)
  266. {
  267. idr_for_each(&file_private->syncobj_idr,
  268. &drm_syncobj_release_handle, file_private);
  269. idr_destroy(&file_private->syncobj_idr);
  270. }
  271. int
  272. drm_syncobj_create_ioctl(struct drm_device *dev, void *data,
  273. struct drm_file *file_private)
  274. {
  275. struct drm_syncobj_create *args = data;
  276. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  277. return -ENODEV;
  278. /* no valid flags yet */
  279. if (args->flags)
  280. return -EINVAL;
  281. return drm_syncobj_create(file_private,
  282. &args->handle);
  283. }
  284. int
  285. drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data,
  286. struct drm_file *file_private)
  287. {
  288. struct drm_syncobj_destroy *args = data;
  289. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  290. return -ENODEV;
  291. /* make sure padding is empty */
  292. if (args->pad)
  293. return -EINVAL;
  294. return drm_syncobj_destroy(file_private, args->handle);
  295. }
  296. int
  297. drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  298. struct drm_file *file_private)
  299. {
  300. struct drm_syncobj_handle *args = data;
  301. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  302. return -ENODEV;
  303. if (args->pad || args->flags)
  304. return -EINVAL;
  305. return drm_syncobj_handle_to_fd(file_private, args->handle,
  306. &args->fd);
  307. }
  308. int
  309. drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data,
  310. struct drm_file *file_private)
  311. {
  312. struct drm_syncobj_handle *args = data;
  313. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  314. return -ENODEV;
  315. if (args->pad || args->flags)
  316. return -EINVAL;
  317. return drm_syncobj_fd_to_handle(file_private, args->fd,
  318. &args->handle);
  319. }