drm_syncobj.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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, see struct &drm_syncobj) are
  32. * persistent objects that contain an optional fence. The fence can be updated
  33. * with a new 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 <linux/sched/signal.h>
  53. #include "drm_internal.h"
  54. #include <drm/drm_syncobj.h>
  55. /**
  56. * drm_syncobj_find - lookup and reference a sync object.
  57. * @file_private: drm file private pointer
  58. * @handle: sync object handle to lookup.
  59. *
  60. * Returns a reference to the syncobj pointed to by handle or NULL. The
  61. * reference must be released by calling drm_syncobj_put().
  62. */
  63. struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
  64. u32 handle)
  65. {
  66. struct drm_syncobj *syncobj;
  67. spin_lock(&file_private->syncobj_table_lock);
  68. /* Check if we currently have a reference on the object */
  69. syncobj = idr_find(&file_private->syncobj_idr, handle);
  70. if (syncobj)
  71. drm_syncobj_get(syncobj);
  72. spin_unlock(&file_private->syncobj_table_lock);
  73. return syncobj;
  74. }
  75. EXPORT_SYMBOL(drm_syncobj_find);
  76. static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj,
  77. struct drm_syncobj_cb *cb,
  78. drm_syncobj_func_t func)
  79. {
  80. cb->func = func;
  81. list_add_tail(&cb->node, &syncobj->cb_list);
  82. }
  83. static int drm_syncobj_fence_get_or_add_callback(struct drm_syncobj *syncobj,
  84. struct dma_fence **fence,
  85. struct drm_syncobj_cb *cb,
  86. drm_syncobj_func_t func)
  87. {
  88. int ret;
  89. WARN_ON(*fence);
  90. *fence = drm_syncobj_fence_get(syncobj);
  91. if (*fence)
  92. return 1;
  93. spin_lock(&syncobj->lock);
  94. /* We've already tried once to get a fence and failed. Now that we
  95. * have the lock, try one more time just to be sure we don't add a
  96. * callback when a fence has already been set.
  97. */
  98. if (syncobj->fence) {
  99. *fence = dma_fence_get(rcu_dereference_protected(syncobj->fence,
  100. lockdep_is_held(&syncobj->lock)));
  101. ret = 1;
  102. } else {
  103. *fence = NULL;
  104. drm_syncobj_add_callback_locked(syncobj, cb, func);
  105. ret = 0;
  106. }
  107. spin_unlock(&syncobj->lock);
  108. return ret;
  109. }
  110. /**
  111. * drm_syncobj_add_callback - adds a callback to syncobj::cb_list
  112. * @syncobj: Sync object to which to add the callback
  113. * @cb: Callback to add
  114. * @func: Func to use when initializing the drm_syncobj_cb struct
  115. *
  116. * This adds a callback to be called next time the fence is replaced
  117. */
  118. void drm_syncobj_add_callback(struct drm_syncobj *syncobj,
  119. struct drm_syncobj_cb *cb,
  120. drm_syncobj_func_t func)
  121. {
  122. spin_lock(&syncobj->lock);
  123. drm_syncobj_add_callback_locked(syncobj, cb, func);
  124. spin_unlock(&syncobj->lock);
  125. }
  126. EXPORT_SYMBOL(drm_syncobj_add_callback);
  127. /**
  128. * drm_syncobj_add_callback - removes a callback to syncobj::cb_list
  129. * @syncobj: Sync object from which to remove the callback
  130. * @cb: Callback to remove
  131. */
  132. void drm_syncobj_remove_callback(struct drm_syncobj *syncobj,
  133. struct drm_syncobj_cb *cb)
  134. {
  135. spin_lock(&syncobj->lock);
  136. list_del_init(&cb->node);
  137. spin_unlock(&syncobj->lock);
  138. }
  139. EXPORT_SYMBOL(drm_syncobj_remove_callback);
  140. /**
  141. * drm_syncobj_replace_fence - replace fence in a sync object.
  142. * @syncobj: Sync object to replace fence in
  143. * @fence: fence to install in sync file.
  144. *
  145. * This replaces the fence on a sync object.
  146. */
  147. void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
  148. struct dma_fence *fence)
  149. {
  150. struct dma_fence *old_fence;
  151. struct drm_syncobj_cb *cur, *tmp;
  152. if (fence)
  153. dma_fence_get(fence);
  154. spin_lock(&syncobj->lock);
  155. old_fence = rcu_dereference_protected(syncobj->fence,
  156. lockdep_is_held(&syncobj->lock));
  157. rcu_assign_pointer(syncobj->fence, fence);
  158. if (fence != old_fence) {
  159. list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) {
  160. list_del_init(&cur->node);
  161. cur->func(syncobj, cur);
  162. }
  163. }
  164. spin_unlock(&syncobj->lock);
  165. dma_fence_put(old_fence);
  166. }
  167. EXPORT_SYMBOL(drm_syncobj_replace_fence);
  168. struct drm_syncobj_null_fence {
  169. struct dma_fence base;
  170. spinlock_t lock;
  171. };
  172. static const char *drm_syncobj_null_fence_get_name(struct dma_fence *fence)
  173. {
  174. return "syncobjnull";
  175. }
  176. static bool drm_syncobj_null_fence_enable_signaling(struct dma_fence *fence)
  177. {
  178. dma_fence_enable_sw_signaling(fence);
  179. return !dma_fence_is_signaled(fence);
  180. }
  181. static const struct dma_fence_ops drm_syncobj_null_fence_ops = {
  182. .get_driver_name = drm_syncobj_null_fence_get_name,
  183. .get_timeline_name = drm_syncobj_null_fence_get_name,
  184. .enable_signaling = drm_syncobj_null_fence_enable_signaling,
  185. .release = NULL,
  186. };
  187. static int drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj)
  188. {
  189. struct drm_syncobj_null_fence *fence;
  190. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  191. if (fence == NULL)
  192. return -ENOMEM;
  193. spin_lock_init(&fence->lock);
  194. dma_fence_init(&fence->base, &drm_syncobj_null_fence_ops,
  195. &fence->lock, 0, 0);
  196. dma_fence_signal(&fence->base);
  197. drm_syncobj_replace_fence(syncobj, &fence->base);
  198. dma_fence_put(&fence->base);
  199. return 0;
  200. }
  201. /**
  202. * drm_syncobj_find_fence - lookup and reference the fence in a sync object
  203. * @file_private: drm file private pointer
  204. * @handle: sync object handle to lookup.
  205. * @fence: out parameter for the fence
  206. *
  207. * This is just a convenience function that combines drm_syncobj_find() and
  208. * drm_syncobj_fence_get().
  209. *
  210. * Returns 0 on success or a negative error value on failure. On success @fence
  211. * contains a reference to the fence, which must be released by calling
  212. * dma_fence_put().
  213. */
  214. int drm_syncobj_find_fence(struct drm_file *file_private,
  215. u32 handle,
  216. struct dma_fence **fence)
  217. {
  218. struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
  219. int ret = 0;
  220. if (!syncobj)
  221. return -ENOENT;
  222. *fence = drm_syncobj_fence_get(syncobj);
  223. if (!*fence) {
  224. ret = -EINVAL;
  225. }
  226. drm_syncobj_put(syncobj);
  227. return ret;
  228. }
  229. EXPORT_SYMBOL(drm_syncobj_find_fence);
  230. /**
  231. * drm_syncobj_free - free a sync object.
  232. * @kref: kref to free.
  233. *
  234. * Only to be called from kref_put in drm_syncobj_put.
  235. */
  236. void drm_syncobj_free(struct kref *kref)
  237. {
  238. struct drm_syncobj *syncobj = container_of(kref,
  239. struct drm_syncobj,
  240. refcount);
  241. drm_syncobj_replace_fence(syncobj, NULL);
  242. kfree(syncobj);
  243. }
  244. EXPORT_SYMBOL(drm_syncobj_free);
  245. /**
  246. * drm_syncobj_create - create a new syncobj
  247. * @out_syncobj: returned syncobj
  248. * @flags: DRM_SYNCOBJ_* flags
  249. * @fence: if non-NULL, the syncobj will represent this fence
  250. *
  251. * This is the first function to create a sync object. After creating, drivers
  252. * probably want to make it available to userspace, either through
  253. * drm_syncobj_get_handle() or drm_syncobj_get_fd().
  254. *
  255. * Returns 0 on success or a negative error value on failure.
  256. */
  257. int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
  258. struct dma_fence *fence)
  259. {
  260. int ret;
  261. struct drm_syncobj *syncobj;
  262. syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL);
  263. if (!syncobj)
  264. return -ENOMEM;
  265. kref_init(&syncobj->refcount);
  266. INIT_LIST_HEAD(&syncobj->cb_list);
  267. spin_lock_init(&syncobj->lock);
  268. if (flags & DRM_SYNCOBJ_CREATE_SIGNALED) {
  269. ret = drm_syncobj_assign_null_handle(syncobj);
  270. if (ret < 0) {
  271. drm_syncobj_put(syncobj);
  272. return ret;
  273. }
  274. }
  275. if (fence)
  276. drm_syncobj_replace_fence(syncobj, fence);
  277. *out_syncobj = syncobj;
  278. return 0;
  279. }
  280. EXPORT_SYMBOL(drm_syncobj_create);
  281. /**
  282. * drm_syncobj_get_handle - get a handle from a syncobj
  283. * @file_private: drm file private pointer
  284. * @syncobj: Sync object to export
  285. * @handle: out parameter with the new handle
  286. *
  287. * Exports a sync object created with drm_syncobj_create() as a handle on
  288. * @file_private to userspace.
  289. *
  290. * Returns 0 on success or a negative error value on failure.
  291. */
  292. int drm_syncobj_get_handle(struct drm_file *file_private,
  293. struct drm_syncobj *syncobj, u32 *handle)
  294. {
  295. int ret;
  296. /* take a reference to put in the idr */
  297. drm_syncobj_get(syncobj);
  298. idr_preload(GFP_KERNEL);
  299. spin_lock(&file_private->syncobj_table_lock);
  300. ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
  301. spin_unlock(&file_private->syncobj_table_lock);
  302. idr_preload_end();
  303. if (ret < 0) {
  304. drm_syncobj_put(syncobj);
  305. return ret;
  306. }
  307. *handle = ret;
  308. return 0;
  309. }
  310. EXPORT_SYMBOL(drm_syncobj_get_handle);
  311. static int drm_syncobj_create_as_handle(struct drm_file *file_private,
  312. u32 *handle, uint32_t flags)
  313. {
  314. int ret;
  315. struct drm_syncobj *syncobj;
  316. ret = drm_syncobj_create(&syncobj, flags, NULL);
  317. if (ret)
  318. return ret;
  319. ret = drm_syncobj_get_handle(file_private, syncobj, handle);
  320. drm_syncobj_put(syncobj);
  321. return ret;
  322. }
  323. static int drm_syncobj_destroy(struct drm_file *file_private,
  324. u32 handle)
  325. {
  326. struct drm_syncobj *syncobj;
  327. spin_lock(&file_private->syncobj_table_lock);
  328. syncobj = idr_remove(&file_private->syncobj_idr, handle);
  329. spin_unlock(&file_private->syncobj_table_lock);
  330. if (!syncobj)
  331. return -EINVAL;
  332. drm_syncobj_put(syncobj);
  333. return 0;
  334. }
  335. static int drm_syncobj_file_release(struct inode *inode, struct file *file)
  336. {
  337. struct drm_syncobj *syncobj = file->private_data;
  338. drm_syncobj_put(syncobj);
  339. return 0;
  340. }
  341. static const struct file_operations drm_syncobj_file_fops = {
  342. .release = drm_syncobj_file_release,
  343. };
  344. /**
  345. * drm_syncobj_get_fd - get a file descriptor from a syncobj
  346. * @syncobj: Sync object to export
  347. * @p_fd: out parameter with the new file descriptor
  348. *
  349. * Exports a sync object created with drm_syncobj_create() as a file descriptor.
  350. *
  351. * Returns 0 on success or a negative error value on failure.
  352. */
  353. int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd)
  354. {
  355. struct file *file;
  356. int fd;
  357. fd = get_unused_fd_flags(O_CLOEXEC);
  358. if (fd < 0)
  359. return fd;
  360. file = anon_inode_getfile("syncobj_file",
  361. &drm_syncobj_file_fops,
  362. syncobj, 0);
  363. if (IS_ERR(file)) {
  364. put_unused_fd(fd);
  365. return PTR_ERR(file);
  366. }
  367. drm_syncobj_get(syncobj);
  368. fd_install(fd, file);
  369. *p_fd = fd;
  370. return 0;
  371. }
  372. EXPORT_SYMBOL(drm_syncobj_get_fd);
  373. static int drm_syncobj_handle_to_fd(struct drm_file *file_private,
  374. u32 handle, int *p_fd)
  375. {
  376. struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
  377. int ret;
  378. if (!syncobj)
  379. return -EINVAL;
  380. ret = drm_syncobj_get_fd(syncobj, p_fd);
  381. drm_syncobj_put(syncobj);
  382. return ret;
  383. }
  384. static int drm_syncobj_fd_to_handle(struct drm_file *file_private,
  385. int fd, u32 *handle)
  386. {
  387. struct drm_syncobj *syncobj;
  388. struct file *file;
  389. int ret;
  390. file = fget(fd);
  391. if (!file)
  392. return -EINVAL;
  393. if (file->f_op != &drm_syncobj_file_fops) {
  394. fput(file);
  395. return -EINVAL;
  396. }
  397. /* take a reference to put in the idr */
  398. syncobj = file->private_data;
  399. drm_syncobj_get(syncobj);
  400. idr_preload(GFP_KERNEL);
  401. spin_lock(&file_private->syncobj_table_lock);
  402. ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
  403. spin_unlock(&file_private->syncobj_table_lock);
  404. idr_preload_end();
  405. if (ret > 0) {
  406. *handle = ret;
  407. ret = 0;
  408. } else
  409. drm_syncobj_put(syncobj);
  410. fput(file);
  411. return ret;
  412. }
  413. static int drm_syncobj_import_sync_file_fence(struct drm_file *file_private,
  414. int fd, int handle)
  415. {
  416. struct dma_fence *fence = sync_file_get_fence(fd);
  417. struct drm_syncobj *syncobj;
  418. if (!fence)
  419. return -EINVAL;
  420. syncobj = drm_syncobj_find(file_private, handle);
  421. if (!syncobj) {
  422. dma_fence_put(fence);
  423. return -ENOENT;
  424. }
  425. drm_syncobj_replace_fence(syncobj, fence);
  426. dma_fence_put(fence);
  427. drm_syncobj_put(syncobj);
  428. return 0;
  429. }
  430. static int drm_syncobj_export_sync_file(struct drm_file *file_private,
  431. int handle, int *p_fd)
  432. {
  433. int ret;
  434. struct dma_fence *fence;
  435. struct sync_file *sync_file;
  436. int fd = get_unused_fd_flags(O_CLOEXEC);
  437. if (fd < 0)
  438. return fd;
  439. ret = drm_syncobj_find_fence(file_private, handle, &fence);
  440. if (ret)
  441. goto err_put_fd;
  442. sync_file = sync_file_create(fence);
  443. dma_fence_put(fence);
  444. if (!sync_file) {
  445. ret = -EINVAL;
  446. goto err_put_fd;
  447. }
  448. fd_install(fd, sync_file->file);
  449. *p_fd = fd;
  450. return 0;
  451. err_put_fd:
  452. put_unused_fd(fd);
  453. return ret;
  454. }
  455. /**
  456. * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time
  457. * @file_private: drm file-private structure to set up
  458. *
  459. * Called at device open time, sets up the structure for handling refcounting
  460. * of sync objects.
  461. */
  462. void
  463. drm_syncobj_open(struct drm_file *file_private)
  464. {
  465. idr_init_base(&file_private->syncobj_idr, 1);
  466. spin_lock_init(&file_private->syncobj_table_lock);
  467. }
  468. static int
  469. drm_syncobj_release_handle(int id, void *ptr, void *data)
  470. {
  471. struct drm_syncobj *syncobj = ptr;
  472. drm_syncobj_put(syncobj);
  473. return 0;
  474. }
  475. /**
  476. * drm_syncobj_release - release file-private sync object resources
  477. * @file_private: drm file-private structure to clean up
  478. *
  479. * Called at close time when the filp is going away.
  480. *
  481. * Releases any remaining references on objects by this filp.
  482. */
  483. void
  484. drm_syncobj_release(struct drm_file *file_private)
  485. {
  486. idr_for_each(&file_private->syncobj_idr,
  487. &drm_syncobj_release_handle, file_private);
  488. idr_destroy(&file_private->syncobj_idr);
  489. }
  490. int
  491. drm_syncobj_create_ioctl(struct drm_device *dev, void *data,
  492. struct drm_file *file_private)
  493. {
  494. struct drm_syncobj_create *args = data;
  495. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  496. return -ENODEV;
  497. /* no valid flags yet */
  498. if (args->flags & ~DRM_SYNCOBJ_CREATE_SIGNALED)
  499. return -EINVAL;
  500. return drm_syncobj_create_as_handle(file_private,
  501. &args->handle, args->flags);
  502. }
  503. int
  504. drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data,
  505. struct drm_file *file_private)
  506. {
  507. struct drm_syncobj_destroy *args = data;
  508. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  509. return -ENODEV;
  510. /* make sure padding is empty */
  511. if (args->pad)
  512. return -EINVAL;
  513. return drm_syncobj_destroy(file_private, args->handle);
  514. }
  515. int
  516. drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  517. struct drm_file *file_private)
  518. {
  519. struct drm_syncobj_handle *args = data;
  520. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  521. return -ENODEV;
  522. if (args->pad)
  523. return -EINVAL;
  524. if (args->flags != 0 &&
  525. args->flags != DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
  526. return -EINVAL;
  527. if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
  528. return drm_syncobj_export_sync_file(file_private, args->handle,
  529. &args->fd);
  530. return drm_syncobj_handle_to_fd(file_private, args->handle,
  531. &args->fd);
  532. }
  533. int
  534. drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data,
  535. struct drm_file *file_private)
  536. {
  537. struct drm_syncobj_handle *args = data;
  538. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  539. return -ENODEV;
  540. if (args->pad)
  541. return -EINVAL;
  542. if (args->flags != 0 &&
  543. args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
  544. return -EINVAL;
  545. if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
  546. return drm_syncobj_import_sync_file_fence(file_private,
  547. args->fd,
  548. args->handle);
  549. return drm_syncobj_fd_to_handle(file_private, args->fd,
  550. &args->handle);
  551. }
  552. struct syncobj_wait_entry {
  553. struct task_struct *task;
  554. struct dma_fence *fence;
  555. struct dma_fence_cb fence_cb;
  556. struct drm_syncobj_cb syncobj_cb;
  557. };
  558. static void syncobj_wait_fence_func(struct dma_fence *fence,
  559. struct dma_fence_cb *cb)
  560. {
  561. struct syncobj_wait_entry *wait =
  562. container_of(cb, struct syncobj_wait_entry, fence_cb);
  563. wake_up_process(wait->task);
  564. }
  565. static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
  566. struct drm_syncobj_cb *cb)
  567. {
  568. struct syncobj_wait_entry *wait =
  569. container_of(cb, struct syncobj_wait_entry, syncobj_cb);
  570. /* This happens inside the syncobj lock */
  571. wait->fence = dma_fence_get(rcu_dereference_protected(syncobj->fence,
  572. lockdep_is_held(&syncobj->lock)));
  573. wake_up_process(wait->task);
  574. }
  575. static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
  576. uint32_t count,
  577. uint32_t flags,
  578. signed long timeout,
  579. uint32_t *idx)
  580. {
  581. struct syncobj_wait_entry *entries;
  582. struct dma_fence *fence;
  583. signed long ret;
  584. uint32_t signaled_count, i;
  585. entries = kcalloc(count, sizeof(*entries), GFP_KERNEL);
  586. if (!entries)
  587. return -ENOMEM;
  588. /* Walk the list of sync objects and initialize entries. We do
  589. * this up-front so that we can properly return -EINVAL if there is
  590. * a syncobj with a missing fence and then never have the chance of
  591. * returning -EINVAL again.
  592. */
  593. signaled_count = 0;
  594. for (i = 0; i < count; ++i) {
  595. entries[i].task = current;
  596. entries[i].fence = drm_syncobj_fence_get(syncobjs[i]);
  597. if (!entries[i].fence) {
  598. if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
  599. continue;
  600. } else {
  601. ret = -EINVAL;
  602. goto cleanup_entries;
  603. }
  604. }
  605. if (dma_fence_is_signaled(entries[i].fence)) {
  606. if (signaled_count == 0 && idx)
  607. *idx = i;
  608. signaled_count++;
  609. }
  610. }
  611. /* Initialize ret to the max of timeout and 1. That way, the
  612. * default return value indicates a successful wait and not a
  613. * timeout.
  614. */
  615. ret = max_t(signed long, timeout, 1);
  616. if (signaled_count == count ||
  617. (signaled_count > 0 &&
  618. !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL)))
  619. goto cleanup_entries;
  620. /* There's a very annoying laxness in the dma_fence API here, in
  621. * that backends are not required to automatically report when a
  622. * fence is signaled prior to fence->ops->enable_signaling() being
  623. * called. So here if we fail to match signaled_count, we need to
  624. * fallthough and try a 0 timeout wait!
  625. */
  626. if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
  627. for (i = 0; i < count; ++i) {
  628. if (entries[i].fence)
  629. continue;
  630. drm_syncobj_fence_get_or_add_callback(syncobjs[i],
  631. &entries[i].fence,
  632. &entries[i].syncobj_cb,
  633. syncobj_wait_syncobj_func);
  634. }
  635. }
  636. do {
  637. set_current_state(TASK_INTERRUPTIBLE);
  638. signaled_count = 0;
  639. for (i = 0; i < count; ++i) {
  640. fence = entries[i].fence;
  641. if (!fence)
  642. continue;
  643. if (dma_fence_is_signaled(fence) ||
  644. (!entries[i].fence_cb.func &&
  645. dma_fence_add_callback(fence,
  646. &entries[i].fence_cb,
  647. syncobj_wait_fence_func))) {
  648. /* The fence has been signaled */
  649. if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) {
  650. signaled_count++;
  651. } else {
  652. if (idx)
  653. *idx = i;
  654. goto done_waiting;
  655. }
  656. }
  657. }
  658. if (signaled_count == count)
  659. goto done_waiting;
  660. if (timeout == 0) {
  661. /* If we are doing a 0 timeout wait and we got
  662. * here, then we just timed out.
  663. */
  664. ret = 0;
  665. goto done_waiting;
  666. }
  667. ret = schedule_timeout(ret);
  668. if (ret > 0 && signal_pending(current))
  669. ret = -ERESTARTSYS;
  670. } while (ret > 0);
  671. done_waiting:
  672. __set_current_state(TASK_RUNNING);
  673. cleanup_entries:
  674. for (i = 0; i < count; ++i) {
  675. if (entries[i].syncobj_cb.func)
  676. drm_syncobj_remove_callback(syncobjs[i],
  677. &entries[i].syncobj_cb);
  678. if (entries[i].fence_cb.func)
  679. dma_fence_remove_callback(entries[i].fence,
  680. &entries[i].fence_cb);
  681. dma_fence_put(entries[i].fence);
  682. }
  683. kfree(entries);
  684. return ret;
  685. }
  686. /**
  687. * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value
  688. *
  689. * @timeout_nsec: timeout nsec component in ns, 0 for poll
  690. *
  691. * Calculate the timeout in jiffies from an absolute time in sec/nsec.
  692. */
  693. static signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec)
  694. {
  695. ktime_t abs_timeout, now;
  696. u64 timeout_ns, timeout_jiffies64;
  697. /* make 0 timeout means poll - absolute 0 doesn't seem valid */
  698. if (timeout_nsec == 0)
  699. return 0;
  700. abs_timeout = ns_to_ktime(timeout_nsec);
  701. now = ktime_get();
  702. if (!ktime_after(abs_timeout, now))
  703. return 0;
  704. timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now));
  705. timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns);
  706. /* clamp timeout to avoid infinite timeout */
  707. if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1)
  708. return MAX_SCHEDULE_TIMEOUT - 1;
  709. return timeout_jiffies64 + 1;
  710. }
  711. static int drm_syncobj_array_wait(struct drm_device *dev,
  712. struct drm_file *file_private,
  713. struct drm_syncobj_wait *wait,
  714. struct drm_syncobj **syncobjs)
  715. {
  716. signed long timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec);
  717. signed long ret = 0;
  718. uint32_t first = ~0;
  719. ret = drm_syncobj_array_wait_timeout(syncobjs,
  720. wait->count_handles,
  721. wait->flags,
  722. timeout, &first);
  723. if (ret < 0)
  724. return ret;
  725. wait->first_signaled = first;
  726. if (ret == 0)
  727. return -ETIME;
  728. return 0;
  729. }
  730. static int drm_syncobj_array_find(struct drm_file *file_private,
  731. void __user *user_handles,
  732. uint32_t count_handles,
  733. struct drm_syncobj ***syncobjs_out)
  734. {
  735. uint32_t i, *handles;
  736. struct drm_syncobj **syncobjs;
  737. int ret;
  738. handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL);
  739. if (handles == NULL)
  740. return -ENOMEM;
  741. if (copy_from_user(handles, user_handles,
  742. sizeof(uint32_t) * count_handles)) {
  743. ret = -EFAULT;
  744. goto err_free_handles;
  745. }
  746. syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL);
  747. if (syncobjs == NULL) {
  748. ret = -ENOMEM;
  749. goto err_free_handles;
  750. }
  751. for (i = 0; i < count_handles; i++) {
  752. syncobjs[i] = drm_syncobj_find(file_private, handles[i]);
  753. if (!syncobjs[i]) {
  754. ret = -ENOENT;
  755. goto err_put_syncobjs;
  756. }
  757. }
  758. kfree(handles);
  759. *syncobjs_out = syncobjs;
  760. return 0;
  761. err_put_syncobjs:
  762. while (i-- > 0)
  763. drm_syncobj_put(syncobjs[i]);
  764. kfree(syncobjs);
  765. err_free_handles:
  766. kfree(handles);
  767. return ret;
  768. }
  769. static void drm_syncobj_array_free(struct drm_syncobj **syncobjs,
  770. uint32_t count)
  771. {
  772. uint32_t i;
  773. for (i = 0; i < count; i++)
  774. drm_syncobj_put(syncobjs[i]);
  775. kfree(syncobjs);
  776. }
  777. int
  778. drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,
  779. struct drm_file *file_private)
  780. {
  781. struct drm_syncobj_wait *args = data;
  782. struct drm_syncobj **syncobjs;
  783. int ret = 0;
  784. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  785. return -ENODEV;
  786. if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
  787. DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT))
  788. return -EINVAL;
  789. if (args->count_handles == 0)
  790. return -EINVAL;
  791. ret = drm_syncobj_array_find(file_private,
  792. u64_to_user_ptr(args->handles),
  793. args->count_handles,
  794. &syncobjs);
  795. if (ret < 0)
  796. return ret;
  797. ret = drm_syncobj_array_wait(dev, file_private,
  798. args, syncobjs);
  799. drm_syncobj_array_free(syncobjs, args->count_handles);
  800. return ret;
  801. }
  802. int
  803. drm_syncobj_reset_ioctl(struct drm_device *dev, void *data,
  804. struct drm_file *file_private)
  805. {
  806. struct drm_syncobj_array *args = data;
  807. struct drm_syncobj **syncobjs;
  808. uint32_t i;
  809. int ret;
  810. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  811. return -ENODEV;
  812. if (args->pad != 0)
  813. return -EINVAL;
  814. if (args->count_handles == 0)
  815. return -EINVAL;
  816. ret = drm_syncobj_array_find(file_private,
  817. u64_to_user_ptr(args->handles),
  818. args->count_handles,
  819. &syncobjs);
  820. if (ret < 0)
  821. return ret;
  822. for (i = 0; i < args->count_handles; i++)
  823. drm_syncobj_replace_fence(syncobjs[i], NULL);
  824. drm_syncobj_array_free(syncobjs, args->count_handles);
  825. return 0;
  826. }
  827. int
  828. drm_syncobj_signal_ioctl(struct drm_device *dev, void *data,
  829. struct drm_file *file_private)
  830. {
  831. struct drm_syncobj_array *args = data;
  832. struct drm_syncobj **syncobjs;
  833. uint32_t i;
  834. int ret;
  835. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  836. return -ENODEV;
  837. if (args->pad != 0)
  838. return -EINVAL;
  839. if (args->count_handles == 0)
  840. return -EINVAL;
  841. ret = drm_syncobj_array_find(file_private,
  842. u64_to_user_ptr(args->handles),
  843. args->count_handles,
  844. &syncobjs);
  845. if (ret < 0)
  846. return ret;
  847. for (i = 0; i < args->count_handles; i++) {
  848. ret = drm_syncobj_assign_null_handle(syncobjs[i]);
  849. if (ret < 0)
  850. break;
  851. }
  852. drm_syncobj_array_free(syncobjs, args->count_handles);
  853. return ret;
  854. }