drm_syncobj.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. * Copyright 2017 Red Hat
  3. * Parts ported from amdgpu (fence wait code).
  4. * Copyright 2016 Advanced Micro Devices, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  23. * IN THE SOFTWARE.
  24. *
  25. * Authors:
  26. *
  27. */
  28. /**
  29. * DOC: Overview
  30. *
  31. * DRM synchronisation objects (syncobj) are a persistent objects,
  32. * that contain an optional fence. The fence can be updated with a new
  33. * fence, or be NULL.
  34. *
  35. * syncobj's can be waited upon, where it will wait for the underlying
  36. * fence.
  37. *
  38. * syncobj's can be export to fd's and back, these fd's are opaque and
  39. * have no other use case, except passing the syncobj between processes.
  40. *
  41. * Their primary use-case is to implement Vulkan fences and semaphores.
  42. *
  43. * syncobj have a kref reference count, but also have an optional file.
  44. * The file is only created once the syncobj is exported.
  45. * The file takes a reference on the kref.
  46. */
  47. #include <drm/drmP.h>
  48. #include <linux/file.h>
  49. #include <linux/fs.h>
  50. #include <linux/anon_inodes.h>
  51. #include <linux/sync_file.h>
  52. #include "drm_internal.h"
  53. #include <drm/drm_syncobj.h>
  54. /**
  55. * drm_syncobj_find - lookup and reference a sync object.
  56. * @file_private: drm file private pointer
  57. * @handle: sync object handle to lookup.
  58. *
  59. * Returns a reference to the syncobj pointed to by handle or NULL.
  60. */
  61. struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
  62. u32 handle)
  63. {
  64. struct drm_syncobj *syncobj;
  65. spin_lock(&file_private->syncobj_table_lock);
  66. /* Check if we currently have a reference on the object */
  67. syncobj = idr_find(&file_private->syncobj_idr, handle);
  68. if (syncobj)
  69. drm_syncobj_get(syncobj);
  70. spin_unlock(&file_private->syncobj_table_lock);
  71. return syncobj;
  72. }
  73. EXPORT_SYMBOL(drm_syncobj_find);
  74. /**
  75. * drm_syncobj_replace_fence - replace fence in a sync object.
  76. * @syncobj: Sync object to replace fence in
  77. * @fence: fence to install in sync file.
  78. *
  79. * This replaces the fence on a sync object.
  80. */
  81. void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
  82. struct dma_fence *fence)
  83. {
  84. struct dma_fence *old_fence;
  85. if (fence)
  86. dma_fence_get(fence);
  87. old_fence = xchg(&syncobj->fence, fence);
  88. dma_fence_put(old_fence);
  89. }
  90. EXPORT_SYMBOL(drm_syncobj_replace_fence);
  91. int drm_syncobj_find_fence(struct drm_file *file_private,
  92. u32 handle,
  93. struct dma_fence **fence)
  94. {
  95. struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
  96. int ret = 0;
  97. if (!syncobj)
  98. return -ENOENT;
  99. *fence = drm_syncobj_fence_get(syncobj);
  100. if (!*fence) {
  101. ret = -EINVAL;
  102. }
  103. drm_syncobj_put(syncobj);
  104. return ret;
  105. }
  106. EXPORT_SYMBOL(drm_syncobj_find_fence);
  107. /**
  108. * drm_syncobj_free - free a sync object.
  109. * @kref: kref to free.
  110. *
  111. * Only to be called from kref_put in drm_syncobj_put.
  112. */
  113. void drm_syncobj_free(struct kref *kref)
  114. {
  115. struct drm_syncobj *syncobj = container_of(kref,
  116. struct drm_syncobj,
  117. refcount);
  118. dma_fence_put(syncobj->fence);
  119. kfree(syncobj);
  120. }
  121. EXPORT_SYMBOL(drm_syncobj_free);
  122. static int drm_syncobj_create(struct drm_file *file_private,
  123. u32 *handle)
  124. {
  125. int ret;
  126. struct drm_syncobj *syncobj;
  127. syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL);
  128. if (!syncobj)
  129. return -ENOMEM;
  130. kref_init(&syncobj->refcount);
  131. idr_preload(GFP_KERNEL);
  132. spin_lock(&file_private->syncobj_table_lock);
  133. ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
  134. spin_unlock(&file_private->syncobj_table_lock);
  135. idr_preload_end();
  136. if (ret < 0) {
  137. drm_syncobj_put(syncobj);
  138. return ret;
  139. }
  140. *handle = ret;
  141. return 0;
  142. }
  143. static int drm_syncobj_destroy(struct drm_file *file_private,
  144. u32 handle)
  145. {
  146. struct drm_syncobj *syncobj;
  147. spin_lock(&file_private->syncobj_table_lock);
  148. syncobj = idr_remove(&file_private->syncobj_idr, handle);
  149. spin_unlock(&file_private->syncobj_table_lock);
  150. if (!syncobj)
  151. return -EINVAL;
  152. drm_syncobj_put(syncobj);
  153. return 0;
  154. }
  155. static int drm_syncobj_file_release(struct inode *inode, struct file *file)
  156. {
  157. struct drm_syncobj *syncobj = file->private_data;
  158. drm_syncobj_put(syncobj);
  159. return 0;
  160. }
  161. static const struct file_operations drm_syncobj_file_fops = {
  162. .release = drm_syncobj_file_release,
  163. };
  164. static int drm_syncobj_alloc_file(struct drm_syncobj *syncobj)
  165. {
  166. struct file *file = anon_inode_getfile("syncobj_file",
  167. &drm_syncobj_file_fops,
  168. syncobj, 0);
  169. if (IS_ERR(file))
  170. return PTR_ERR(file);
  171. drm_syncobj_get(syncobj);
  172. if (cmpxchg(&syncobj->file, NULL, file)) {
  173. /* lost the race */
  174. fput(file);
  175. }
  176. return 0;
  177. }
  178. static int drm_syncobj_handle_to_fd(struct drm_file *file_private,
  179. u32 handle, int *p_fd)
  180. {
  181. struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
  182. int ret;
  183. int fd;
  184. if (!syncobj)
  185. return -EINVAL;
  186. fd = get_unused_fd_flags(O_CLOEXEC);
  187. if (fd < 0) {
  188. drm_syncobj_put(syncobj);
  189. return fd;
  190. }
  191. if (!syncobj->file) {
  192. ret = drm_syncobj_alloc_file(syncobj);
  193. if (ret)
  194. goto out_put_fd;
  195. }
  196. fd_install(fd, syncobj->file);
  197. drm_syncobj_put(syncobj);
  198. *p_fd = fd;
  199. return 0;
  200. out_put_fd:
  201. put_unused_fd(fd);
  202. drm_syncobj_put(syncobj);
  203. return ret;
  204. }
  205. static struct drm_syncobj *drm_syncobj_fdget(int fd)
  206. {
  207. struct file *file = fget(fd);
  208. if (!file)
  209. return NULL;
  210. if (file->f_op != &drm_syncobj_file_fops)
  211. goto err;
  212. return file->private_data;
  213. err:
  214. fput(file);
  215. return NULL;
  216. };
  217. static int drm_syncobj_fd_to_handle(struct drm_file *file_private,
  218. int fd, u32 *handle)
  219. {
  220. struct drm_syncobj *syncobj = drm_syncobj_fdget(fd);
  221. int ret;
  222. if (!syncobj)
  223. return -EINVAL;
  224. /* take a reference to put in the idr */
  225. drm_syncobj_get(syncobj);
  226. idr_preload(GFP_KERNEL);
  227. spin_lock(&file_private->syncobj_table_lock);
  228. ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
  229. spin_unlock(&file_private->syncobj_table_lock);
  230. idr_preload_end();
  231. if (ret < 0) {
  232. fput(syncobj->file);
  233. return ret;
  234. }
  235. *handle = ret;
  236. return 0;
  237. }
  238. int drm_syncobj_import_sync_file_fence(struct drm_file *file_private,
  239. int fd, int handle)
  240. {
  241. struct dma_fence *fence = sync_file_get_fence(fd);
  242. struct drm_syncobj *syncobj;
  243. if (!fence)
  244. return -EINVAL;
  245. syncobj = drm_syncobj_find(file_private, handle);
  246. if (!syncobj) {
  247. dma_fence_put(fence);
  248. return -ENOENT;
  249. }
  250. drm_syncobj_replace_fence(syncobj, fence);
  251. dma_fence_put(fence);
  252. drm_syncobj_put(syncobj);
  253. return 0;
  254. }
  255. int drm_syncobj_export_sync_file(struct drm_file *file_private,
  256. int handle, int *p_fd)
  257. {
  258. int ret;
  259. struct dma_fence *fence;
  260. struct sync_file *sync_file;
  261. int fd = get_unused_fd_flags(O_CLOEXEC);
  262. if (fd < 0)
  263. return fd;
  264. ret = drm_syncobj_find_fence(file_private, handle, &fence);
  265. if (ret)
  266. goto err_put_fd;
  267. sync_file = sync_file_create(fence);
  268. dma_fence_put(fence);
  269. if (!sync_file) {
  270. ret = -EINVAL;
  271. goto err_put_fd;
  272. }
  273. fd_install(fd, sync_file->file);
  274. *p_fd = fd;
  275. return 0;
  276. err_put_fd:
  277. put_unused_fd(fd);
  278. return ret;
  279. }
  280. /**
  281. * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time
  282. * @file_private: drm file-private structure to set up
  283. *
  284. * Called at device open time, sets up the structure for handling refcounting
  285. * of sync objects.
  286. */
  287. void
  288. drm_syncobj_open(struct drm_file *file_private)
  289. {
  290. idr_init(&file_private->syncobj_idr);
  291. spin_lock_init(&file_private->syncobj_table_lock);
  292. }
  293. static int
  294. drm_syncobj_release_handle(int id, void *ptr, void *data)
  295. {
  296. struct drm_syncobj *syncobj = ptr;
  297. drm_syncobj_put(syncobj);
  298. return 0;
  299. }
  300. /**
  301. * drm_syncobj_release - release file-private sync object resources
  302. * @file_private: drm file-private structure to clean up
  303. *
  304. * Called at close time when the filp is going away.
  305. *
  306. * Releases any remaining references on objects by this filp.
  307. */
  308. void
  309. drm_syncobj_release(struct drm_file *file_private)
  310. {
  311. idr_for_each(&file_private->syncobj_idr,
  312. &drm_syncobj_release_handle, file_private);
  313. idr_destroy(&file_private->syncobj_idr);
  314. }
  315. int
  316. drm_syncobj_create_ioctl(struct drm_device *dev, void *data,
  317. struct drm_file *file_private)
  318. {
  319. struct drm_syncobj_create *args = data;
  320. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  321. return -ENODEV;
  322. /* no valid flags yet */
  323. if (args->flags)
  324. return -EINVAL;
  325. return drm_syncobj_create(file_private,
  326. &args->handle);
  327. }
  328. int
  329. drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data,
  330. struct drm_file *file_private)
  331. {
  332. struct drm_syncobj_destroy *args = data;
  333. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  334. return -ENODEV;
  335. /* make sure padding is empty */
  336. if (args->pad)
  337. return -EINVAL;
  338. return drm_syncobj_destroy(file_private, args->handle);
  339. }
  340. int
  341. drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  342. struct drm_file *file_private)
  343. {
  344. struct drm_syncobj_handle *args = data;
  345. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  346. return -ENODEV;
  347. if (args->pad)
  348. return -EINVAL;
  349. if (args->flags != 0 &&
  350. args->flags != DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
  351. return -EINVAL;
  352. if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
  353. return drm_syncobj_export_sync_file(file_private, args->handle,
  354. &args->fd);
  355. return drm_syncobj_handle_to_fd(file_private, args->handle,
  356. &args->fd);
  357. }
  358. int
  359. drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data,
  360. struct drm_file *file_private)
  361. {
  362. struct drm_syncobj_handle *args = data;
  363. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  364. return -ENODEV;
  365. if (args->pad)
  366. return -EINVAL;
  367. if (args->flags != 0 &&
  368. args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
  369. return -EINVAL;
  370. if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
  371. return drm_syncobj_import_sync_file_fence(file_private,
  372. args->fd,
  373. args->handle);
  374. return drm_syncobj_fd_to_handle(file_private, args->fd,
  375. &args->handle);
  376. }
  377. /**
  378. * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value
  379. *
  380. * @timeout_nsec: timeout nsec component in ns, 0 for poll
  381. *
  382. * Calculate the timeout in jiffies from an absolute time in sec/nsec.
  383. */
  384. static signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec)
  385. {
  386. ktime_t abs_timeout, now;
  387. u64 timeout_ns, timeout_jiffies64;
  388. /* make 0 timeout means poll - absolute 0 doesn't seem valid */
  389. if (timeout_nsec == 0)
  390. return 0;
  391. abs_timeout = ns_to_ktime(timeout_nsec);
  392. now = ktime_get();
  393. if (!ktime_after(abs_timeout, now))
  394. return 0;
  395. timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now));
  396. timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns);
  397. /* clamp timeout to avoid infinite timeout */
  398. if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1)
  399. return MAX_SCHEDULE_TIMEOUT - 1;
  400. return timeout_jiffies64 + 1;
  401. }
  402. static int drm_syncobj_wait_fences(struct drm_device *dev,
  403. struct drm_file *file_private,
  404. struct drm_syncobj_wait *wait,
  405. struct dma_fence **fences)
  406. {
  407. signed long timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec);
  408. signed long ret = 0;
  409. uint32_t first = ~0;
  410. if (wait->flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) {
  411. uint32_t i;
  412. for (i = 0; i < wait->count_handles; i++) {
  413. ret = dma_fence_wait_timeout(fences[i], true, timeout);
  414. /* Various dma_fence wait callbacks will return
  415. * ENOENT to indicate that the fence has already
  416. * been signaled. We need to sanitize this to 0 so
  417. * we don't return early and the client doesn't see
  418. * an unexpected error.
  419. */
  420. if (ret == -ENOENT)
  421. ret = 0;
  422. if (ret < 0)
  423. return ret;
  424. if (ret == 0)
  425. break;
  426. timeout = ret;
  427. }
  428. first = 0;
  429. } else {
  430. ret = dma_fence_wait_any_timeout(fences,
  431. wait->count_handles,
  432. true, timeout,
  433. &first);
  434. }
  435. if (ret < 0)
  436. return ret;
  437. wait->first_signaled = first;
  438. if (ret == 0)
  439. return -ETIME;
  440. return 0;
  441. }
  442. int
  443. drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,
  444. struct drm_file *file_private)
  445. {
  446. struct drm_syncobj_wait *args = data;
  447. uint32_t *handles;
  448. struct dma_fence **fences;
  449. int ret = 0;
  450. uint32_t i;
  451. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  452. return -ENODEV;
  453. if (args->flags != 0 && args->flags != DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL)
  454. return -EINVAL;
  455. if (args->count_handles == 0)
  456. return -EINVAL;
  457. /* Get the handles from userspace */
  458. handles = kmalloc_array(args->count_handles, sizeof(uint32_t),
  459. GFP_KERNEL);
  460. if (handles == NULL)
  461. return -ENOMEM;
  462. if (copy_from_user(handles,
  463. u64_to_user_ptr(args->handles),
  464. sizeof(uint32_t) * args->count_handles)) {
  465. ret = -EFAULT;
  466. goto err_free_handles;
  467. }
  468. fences = kcalloc(args->count_handles,
  469. sizeof(struct dma_fence *), GFP_KERNEL);
  470. if (!fences) {
  471. ret = -ENOMEM;
  472. goto err_free_handles;
  473. }
  474. for (i = 0; i < args->count_handles; i++) {
  475. ret = drm_syncobj_find_fence(file_private, handles[i],
  476. &fences[i]);
  477. if (ret)
  478. goto err_free_fence_array;
  479. }
  480. ret = drm_syncobj_wait_fences(dev, file_private,
  481. args, fences);
  482. err_free_fence_array:
  483. for (i = 0; i < args->count_handles; i++)
  484. dma_fence_put(fences[i]);
  485. kfree(fences);
  486. err_free_handles:
  487. kfree(handles);
  488. return ret;
  489. }