drm_syncobj.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719
  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) provide a
  32. * container for a synchronization primitive which can be used by userspace
  33. * to explicitly synchronize GPU commands, can be shared between userspace
  34. * processes, and can be shared between different DRM drivers.
  35. * Their primary use-case is to implement Vulkan fences and semaphores.
  36. * The syncobj userspace API provides ioctls for several operations:
  37. *
  38. * - Creation and destruction of syncobjs
  39. * - Import and export of syncobjs to/from a syncobj file descriptor
  40. * - Import and export a syncobj's underlying fence to/from a sync file
  41. * - Reset a syncobj (set its fence to NULL)
  42. * - Signal a syncobj (set a trivially signaled fence)
  43. * - Wait for a syncobj's fence to appear and be signaled
  44. *
  45. * The syncobj userspace API also provides operations to manipulate a syncobj
  46. * in terms of a timeline of struct &dma_fence_chain rather than a single
  47. * struct &dma_fence, through the following operations:
  48. *
  49. * - Signal a given point on the timeline
  50. * - Wait for a given point to appear and/or be signaled
  51. * - Import and export from/to a given point of a timeline
  52. *
  53. * At it's core, a syncobj is simply a wrapper around a pointer to a struct
  54. * &dma_fence which may be NULL.
  55. * When a syncobj is first created, its pointer is either NULL or a pointer
  56. * to an already signaled fence depending on whether the
  57. * &DRM_SYNCOBJ_CREATE_SIGNALED flag is passed to
  58. * &DRM_IOCTL_SYNCOBJ_CREATE.
  59. *
  60. * If the syncobj is considered as a binary (its state is either signaled or
  61. * unsignaled) primitive, when GPU work is enqueued in a DRM driver to signal
  62. * the syncobj, the syncobj's fence is replaced with a fence which will be
  63. * signaled by the completion of that work.
  64. * If the syncobj is considered as a timeline primitive, when GPU work is
  65. * enqueued in a DRM driver to signal the a given point of the syncobj, a new
  66. * struct &dma_fence_chain pointing to the DRM driver's fence and also
  67. * pointing to the previous fence that was in the syncobj. The new struct
  68. * &dma_fence_chain fence replace the syncobj's fence and will be signaled by
  69. * completion of the DRM driver's work and also any work associated with the
  70. * fence previously in the syncobj.
  71. *
  72. * When GPU work which waits on a syncobj is enqueued in a DRM driver, at the
  73. * time the work is enqueued, it waits on the syncobj's fence before
  74. * submitting the work to hardware. That fence is either :
  75. *
  76. * - The syncobj's current fence if the syncobj is considered as a binary
  77. * primitive.
  78. * - The struct &dma_fence associated with a given point if the syncobj is
  79. * considered as a timeline primitive.
  80. *
  81. * If the syncobj's fence is NULL or not present in the syncobj's timeline,
  82. * the enqueue operation is expected to fail.
  83. *
  84. * With binary syncobj, all manipulation of the syncobjs's fence happens in
  85. * terms of the current fence at the time the ioctl is called by userspace
  86. * regardless of whether that operation is an immediate host-side operation
  87. * (signal or reset) or or an operation which is enqueued in some driver
  88. * queue. &DRM_IOCTL_SYNCOBJ_RESET and &DRM_IOCTL_SYNCOBJ_SIGNAL can be used
  89. * to manipulate a syncobj from the host by resetting its pointer to NULL or
  90. * setting its pointer to a fence which is already signaled.
  91. *
  92. * With a timeline syncobj, all manipulation of the synobj's fence happens in
  93. * terms of a u64 value referring to point in the timeline. See
  94. * dma_fence_chain_find_seqno() to see how a given point is found in the
  95. * timeline.
  96. *
  97. * Note that applications should be careful to always use timeline set of
  98. * ioctl() when dealing with syncobj considered as timeline. Using a binary
  99. * set of ioctl() with a syncobj considered as timeline could result incorrect
  100. * synchronization. The use of binary syncobj is supported through the
  101. * timeline set of ioctl() by using a point value of 0, this will reproduce
  102. * the behavior of the binary set of ioctl() (for example replace the
  103. * syncobj's fence when signaling).
  104. *
  105. *
  106. * Host-side wait on syncobjs
  107. * --------------------------
  108. *
  109. * &DRM_IOCTL_SYNCOBJ_WAIT takes an array of syncobj handles and does a
  110. * host-side wait on all of the syncobj fences simultaneously.
  111. * If &DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL is set, the wait ioctl will wait on
  112. * all of the syncobj fences to be signaled before it returns.
  113. * Otherwise, it returns once at least one syncobj fence has been signaled
  114. * and the index of a signaled fence is written back to the client.
  115. *
  116. * Unlike the enqueued GPU work dependencies which fail if they see a NULL
  117. * fence in a syncobj, if &DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT is set,
  118. * the host-side wait will first wait for the syncobj to receive a non-NULL
  119. * fence and then wait on that fence.
  120. * If &DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT is not set and any one of the
  121. * syncobjs in the array has a NULL fence, -EINVAL will be returned.
  122. * Assuming the syncobj starts off with a NULL fence, this allows a client
  123. * to do a host wait in one thread (or process) which waits on GPU work
  124. * submitted in another thread (or process) without having to manually
  125. * synchronize between the two.
  126. * This requirement is inherited from the Vulkan fence API.
  127. *
  128. * If &DRM_SYNCOBJ_WAIT_FLAGS_WAIT_DEADLINE is set, the ioctl will also set
  129. * a fence deadline hint on the backing fences before waiting, to provide the
  130. * fence signaler with an appropriate sense of urgency. The deadline is
  131. * specified as an absolute &CLOCK_MONOTONIC value in units of ns.
  132. *
  133. * Similarly, &DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT takes an array of syncobj
  134. * handles as well as an array of u64 points and does a host-side wait on all
  135. * of syncobj fences at the given points simultaneously.
  136. *
  137. * &DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT also adds the ability to wait for a given
  138. * fence to materialize on the timeline without waiting for the fence to be
  139. * signaled by using the &DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE flag. This
  140. * requirement is inherited from the wait-before-signal behavior required by
  141. * the Vulkan timeline semaphore API.
  142. *
  143. * Alternatively, &DRM_IOCTL_SYNCOBJ_EVENTFD can be used to wait without
  144. * blocking: an eventfd will be signaled when the syncobj is. This is useful to
  145. * integrate the wait in an event loop.
  146. *
  147. *
  148. * Import/export of syncobjs
  149. * -------------------------
  150. *
  151. * &DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE and &DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD
  152. * provide two mechanisms for import/export of syncobjs.
  153. *
  154. * The first lets the client import or export an entire syncobj to a file
  155. * descriptor.
  156. * These fd's are opaque and have no other use case, except passing the
  157. * syncobj between processes.
  158. * All exported file descriptors and any syncobj handles created as a
  159. * result of importing those file descriptors own a reference to the
  160. * same underlying struct &drm_syncobj and the syncobj can be used
  161. * persistently across all the processes with which it is shared.
  162. * The syncobj is freed only once the last reference is dropped.
  163. * Unlike dma-buf, importing a syncobj creates a new handle (with its own
  164. * reference) for every import instead of de-duplicating.
  165. * The primary use-case of this persistent import/export is for shared
  166. * Vulkan fences and semaphores.
  167. *
  168. * The second import/export mechanism, which is indicated by
  169. * &DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE or
  170. * &DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE lets the client
  171. * import/export the syncobj's current fence from/to a &sync_file.
  172. * When a syncobj is exported to a sync file, that sync file wraps the
  173. * sycnobj's fence at the time of export and any later signal or reset
  174. * operations on the syncobj will not affect the exported sync file.
  175. * When a sync file is imported into a syncobj, the syncobj's fence is set
  176. * to the fence wrapped by that sync file.
  177. * Because sync files are immutable, resetting or signaling the syncobj
  178. * will not affect any sync files whose fences have been imported into the
  179. * syncobj.
  180. *
  181. *
  182. * Import/export of timeline points in timeline syncobjs
  183. * -----------------------------------------------------
  184. *
  185. * &DRM_IOCTL_SYNCOBJ_TRANSFER provides a mechanism to transfer a struct
  186. * &dma_fence_chain of a syncobj at a given u64 point to another u64 point
  187. * into another syncobj.
  188. *
  189. * Note that if you want to transfer a struct &dma_fence_chain from a given
  190. * point on a timeline syncobj from/into a binary syncobj, you can use the
  191. * point 0 to mean take/replace the fence in the syncobj.
  192. */
  193. #include <linux/anon_inodes.h>
  194. #include <linux/dma-fence-unwrap.h>
  195. #include <linux/eventfd.h>
  196. #include <linux/export.h>
  197. #include <linux/file.h>
  198. #include <linux/fs.h>
  199. #include <linux/sched/signal.h>
  200. #include <linux/sync_file.h>
  201. #include <linux/uaccess.h>
  202. #include <drm/drm.h>
  203. #include <drm/drm_drv.h>
  204. #include <drm/drm_file.h>
  205. #include <drm/drm_gem.h>
  206. #include <drm/drm_print.h>
  207. #include <drm/drm_syncobj.h>
  208. #include <drm/drm_utils.h>
  209. #include "drm_internal.h"
  210. struct syncobj_wait_entry {
  211. struct list_head node;
  212. struct task_struct *task;
  213. struct dma_fence *fence;
  214. struct dma_fence_cb fence_cb;
  215. u64 point;
  216. };
  217. static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
  218. struct syncobj_wait_entry *wait);
  219. struct syncobj_eventfd_entry {
  220. struct list_head node;
  221. struct dma_fence *fence;
  222. struct dma_fence_cb fence_cb;
  223. struct drm_syncobj *syncobj;
  224. struct eventfd_ctx *ev_fd_ctx;
  225. u64 point;
  226. u32 flags;
  227. };
  228. static void
  229. syncobj_eventfd_entry_func(struct drm_syncobj *syncobj,
  230. struct syncobj_eventfd_entry *entry);
  231. /**
  232. * drm_syncobj_find - lookup and reference a sync object.
  233. * @file_private: drm file private pointer
  234. * @handle: sync object handle to lookup.
  235. *
  236. * Returns a reference to the syncobj pointed to by handle or NULL. The
  237. * reference must be released by calling drm_syncobj_put().
  238. */
  239. struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
  240. u32 handle)
  241. {
  242. struct drm_syncobj *syncobj;
  243. xa_lock(&file_private->syncobj_xa);
  244. /* Check if we currently have a reference on the object */
  245. syncobj = xa_load(&file_private->syncobj_xa, handle);
  246. if (syncobj)
  247. drm_syncobj_get(syncobj);
  248. xa_unlock(&file_private->syncobj_xa);
  249. return syncobj;
  250. }
  251. EXPORT_SYMBOL(drm_syncobj_find);
  252. static void drm_syncobj_fence_add_wait(struct drm_syncobj *syncobj,
  253. struct syncobj_wait_entry *wait)
  254. {
  255. struct dma_fence *fence;
  256. if (wait->fence)
  257. return;
  258. spin_lock(&syncobj->lock);
  259. /* We've already tried once to get a fence and failed. Now that we
  260. * have the lock, try one more time just to be sure we don't add a
  261. * callback when a fence has already been set.
  262. */
  263. fence = dma_fence_get(rcu_dereference_protected(syncobj->fence, 1));
  264. if (!fence || dma_fence_chain_find_seqno(&fence, wait->point)) {
  265. dma_fence_put(fence);
  266. list_add_tail(&wait->node, &syncobj->cb_list);
  267. } else if (!fence) {
  268. wait->fence = dma_fence_get_stub();
  269. } else {
  270. wait->fence = fence;
  271. }
  272. spin_unlock(&syncobj->lock);
  273. }
  274. static void drm_syncobj_remove_wait(struct drm_syncobj *syncobj,
  275. struct syncobj_wait_entry *wait)
  276. {
  277. if (!wait->node.next)
  278. return;
  279. spin_lock(&syncobj->lock);
  280. list_del_init(&wait->node);
  281. spin_unlock(&syncobj->lock);
  282. }
  283. static void
  284. syncobj_eventfd_entry_free(struct syncobj_eventfd_entry *entry)
  285. {
  286. eventfd_ctx_put(entry->ev_fd_ctx);
  287. dma_fence_put(entry->fence);
  288. /* This happens either inside the syncobj lock, or after the node has
  289. * already been removed from the list.
  290. */
  291. list_del(&entry->node);
  292. kfree(entry);
  293. }
  294. static void
  295. drm_syncobj_add_eventfd(struct drm_syncobj *syncobj,
  296. struct syncobj_eventfd_entry *entry)
  297. {
  298. spin_lock(&syncobj->lock);
  299. list_add_tail(&entry->node, &syncobj->ev_fd_list);
  300. syncobj_eventfd_entry_func(syncobj, entry);
  301. spin_unlock(&syncobj->lock);
  302. }
  303. /**
  304. * drm_syncobj_add_point - add new timeline point to the syncobj
  305. * @syncobj: sync object to add timeline point do
  306. * @chain: chain node to use to add the point
  307. * @fence: fence to encapsulate in the chain node
  308. * @point: sequence number to use for the point
  309. *
  310. * Add the chain node as new timeline point to the syncobj.
  311. */
  312. void drm_syncobj_add_point(struct drm_syncobj *syncobj,
  313. struct dma_fence_chain *chain,
  314. struct dma_fence *fence,
  315. uint64_t point)
  316. {
  317. struct syncobj_wait_entry *wait_cur, *wait_tmp;
  318. struct syncobj_eventfd_entry *ev_fd_cur, *ev_fd_tmp;
  319. struct dma_fence *prev;
  320. dma_fence_get(fence);
  321. spin_lock(&syncobj->lock);
  322. prev = drm_syncobj_fence_get(syncobj);
  323. /* You are adding an unorder point to timeline, which could cause payload returned from query_ioctl is 0! */
  324. if (prev && prev->seqno >= point)
  325. DRM_DEBUG("You are adding an unorder point to timeline!\n");
  326. dma_fence_chain_init(chain, prev, fence, point);
  327. rcu_assign_pointer(syncobj->fence, &chain->base);
  328. list_for_each_entry_safe(wait_cur, wait_tmp, &syncobj->cb_list, node)
  329. syncobj_wait_syncobj_func(syncobj, wait_cur);
  330. list_for_each_entry_safe(ev_fd_cur, ev_fd_tmp, &syncobj->ev_fd_list, node)
  331. syncobj_eventfd_entry_func(syncobj, ev_fd_cur);
  332. spin_unlock(&syncobj->lock);
  333. /* Walk the chain once to trigger garbage collection */
  334. dma_fence_chain_for_each(fence, prev);
  335. dma_fence_put(prev);
  336. }
  337. EXPORT_SYMBOL(drm_syncobj_add_point);
  338. /**
  339. * drm_syncobj_replace_fence - replace fence in a sync object.
  340. * @syncobj: Sync object to replace fence in
  341. * @fence: fence to install in sync file.
  342. *
  343. * This replaces the fence on a sync object.
  344. */
  345. void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
  346. struct dma_fence *fence)
  347. {
  348. struct dma_fence *old_fence;
  349. struct syncobj_wait_entry *wait_cur, *wait_tmp;
  350. struct syncobj_eventfd_entry *ev_fd_cur, *ev_fd_tmp;
  351. if (fence)
  352. dma_fence_get(fence);
  353. spin_lock(&syncobj->lock);
  354. old_fence = rcu_dereference_protected(syncobj->fence,
  355. lockdep_is_held(&syncobj->lock));
  356. rcu_assign_pointer(syncobj->fence, fence);
  357. if (fence != old_fence) {
  358. list_for_each_entry_safe(wait_cur, wait_tmp, &syncobj->cb_list, node)
  359. syncobj_wait_syncobj_func(syncobj, wait_cur);
  360. list_for_each_entry_safe(ev_fd_cur, ev_fd_tmp, &syncobj->ev_fd_list, node)
  361. syncobj_eventfd_entry_func(syncobj, ev_fd_cur);
  362. }
  363. spin_unlock(&syncobj->lock);
  364. dma_fence_put(old_fence);
  365. }
  366. EXPORT_SYMBOL(drm_syncobj_replace_fence);
  367. /**
  368. * drm_syncobj_assign_null_handle - assign a stub fence to the sync object
  369. * @syncobj: sync object to assign the fence on
  370. *
  371. * Assign a already signaled stub fence to the sync object.
  372. */
  373. static int drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj)
  374. {
  375. struct dma_fence *fence = dma_fence_allocate_private_stub(ktime_get());
  376. if (!fence)
  377. return -ENOMEM;
  378. drm_syncobj_replace_fence(syncobj, fence);
  379. dma_fence_put(fence);
  380. return 0;
  381. }
  382. /* 5s default for wait submission */
  383. #define DRM_SYNCOBJ_WAIT_FOR_SUBMIT_TIMEOUT 5000000000ULL
  384. /**
  385. * drm_syncobj_find_fence - lookup and reference the fence in a sync object
  386. * @file_private: drm file private pointer
  387. * @handle: sync object handle to lookup.
  388. * @point: timeline point
  389. * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not
  390. * @fence: out parameter for the fence
  391. *
  392. * This is just a convenience function that combines drm_syncobj_find() and
  393. * drm_syncobj_fence_get().
  394. *
  395. * Returns 0 on success or a negative error value on failure. On success @fence
  396. * contains a reference to the fence, which must be released by calling
  397. * dma_fence_put().
  398. */
  399. int drm_syncobj_find_fence(struct drm_file *file_private,
  400. u32 handle, u64 point, u64 flags,
  401. struct dma_fence **fence)
  402. {
  403. struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
  404. struct syncobj_wait_entry wait;
  405. u64 timeout = nsecs_to_jiffies64(DRM_SYNCOBJ_WAIT_FOR_SUBMIT_TIMEOUT);
  406. int ret;
  407. if (flags & ~DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT)
  408. return -EINVAL;
  409. if (!syncobj)
  410. return -ENOENT;
  411. /* Waiting for userspace with locks help is illegal cause that can
  412. * trivial deadlock with page faults for example. Make lockdep complain
  413. * about it early on.
  414. */
  415. if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
  416. might_sleep();
  417. lockdep_assert_none_held_once();
  418. }
  419. *fence = drm_syncobj_fence_get(syncobj);
  420. if (*fence) {
  421. ret = dma_fence_chain_find_seqno(fence, point);
  422. if (!ret) {
  423. /* If the requested seqno is already signaled
  424. * drm_syncobj_find_fence may return a NULL
  425. * fence. To make sure the recipient gets
  426. * signalled, use a new fence instead.
  427. */
  428. if (!*fence)
  429. *fence = dma_fence_get_stub();
  430. goto out;
  431. }
  432. dma_fence_put(*fence);
  433. } else {
  434. ret = -EINVAL;
  435. }
  436. if (!(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT))
  437. goto out;
  438. memset(&wait, 0, sizeof(wait));
  439. wait.task = current;
  440. wait.point = point;
  441. drm_syncobj_fence_add_wait(syncobj, &wait);
  442. do {
  443. set_current_state(TASK_INTERRUPTIBLE);
  444. if (wait.fence) {
  445. ret = 0;
  446. break;
  447. }
  448. if (timeout == 0) {
  449. ret = -ETIME;
  450. break;
  451. }
  452. if (signal_pending(current)) {
  453. ret = -ERESTARTSYS;
  454. break;
  455. }
  456. timeout = schedule_timeout(timeout);
  457. } while (1);
  458. __set_current_state(TASK_RUNNING);
  459. *fence = wait.fence;
  460. if (wait.node.next)
  461. drm_syncobj_remove_wait(syncobj, &wait);
  462. out:
  463. drm_syncobj_put(syncobj);
  464. return ret;
  465. }
  466. EXPORT_SYMBOL(drm_syncobj_find_fence);
  467. /**
  468. * drm_syncobj_free - free a sync object.
  469. * @kref: kref to free.
  470. *
  471. * Only to be called from kref_put in drm_syncobj_put.
  472. */
  473. void drm_syncobj_free(struct kref *kref)
  474. {
  475. struct drm_syncobj *syncobj = container_of(kref,
  476. struct drm_syncobj,
  477. refcount);
  478. struct syncobj_eventfd_entry *ev_fd_cur, *ev_fd_tmp;
  479. drm_syncobj_replace_fence(syncobj, NULL);
  480. list_for_each_entry_safe(ev_fd_cur, ev_fd_tmp, &syncobj->ev_fd_list, node)
  481. syncobj_eventfd_entry_free(ev_fd_cur);
  482. kfree(syncobj);
  483. }
  484. EXPORT_SYMBOL(drm_syncobj_free);
  485. /**
  486. * drm_syncobj_create - create a new syncobj
  487. * @out_syncobj: returned syncobj
  488. * @flags: DRM_SYNCOBJ_* flags
  489. * @fence: if non-NULL, the syncobj will represent this fence
  490. *
  491. * This is the first function to create a sync object. After creating, drivers
  492. * probably want to make it available to userspace, either through
  493. * drm_syncobj_get_handle() or drm_syncobj_get_fd().
  494. *
  495. * Returns 0 on success or a negative error value on failure.
  496. */
  497. int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
  498. struct dma_fence *fence)
  499. {
  500. int ret;
  501. struct drm_syncobj *syncobj;
  502. syncobj = kzalloc_obj(struct drm_syncobj);
  503. if (!syncobj)
  504. return -ENOMEM;
  505. kref_init(&syncobj->refcount);
  506. INIT_LIST_HEAD(&syncobj->cb_list);
  507. INIT_LIST_HEAD(&syncobj->ev_fd_list);
  508. spin_lock_init(&syncobj->lock);
  509. if (flags & DRM_SYNCOBJ_CREATE_SIGNALED) {
  510. ret = drm_syncobj_assign_null_handle(syncobj);
  511. if (ret < 0) {
  512. drm_syncobj_put(syncobj);
  513. return ret;
  514. }
  515. }
  516. if (fence)
  517. drm_syncobj_replace_fence(syncobj, fence);
  518. *out_syncobj = syncobj;
  519. return 0;
  520. }
  521. EXPORT_SYMBOL(drm_syncobj_create);
  522. /**
  523. * drm_syncobj_get_handle - get a handle from a syncobj
  524. * @file_private: drm file private pointer
  525. * @syncobj: Sync object to export
  526. * @handle: out parameter with the new handle
  527. *
  528. * Exports a sync object created with drm_syncobj_create() as a handle on
  529. * @file_private to userspace.
  530. *
  531. * Returns 0 on success or a negative error value on failure.
  532. */
  533. int drm_syncobj_get_handle(struct drm_file *file_private,
  534. struct drm_syncobj *syncobj, u32 *handle)
  535. {
  536. int ret;
  537. /* take a reference to put in the xarray */
  538. drm_syncobj_get(syncobj);
  539. ret = xa_alloc(&file_private->syncobj_xa, handle, syncobj, xa_limit_32b,
  540. GFP_KERNEL);
  541. if (ret)
  542. drm_syncobj_put(syncobj);
  543. return ret;
  544. }
  545. EXPORT_SYMBOL(drm_syncobj_get_handle);
  546. static int drm_syncobj_create_as_handle(struct drm_file *file_private,
  547. u32 *handle, uint32_t flags)
  548. {
  549. int ret;
  550. struct drm_syncobj *syncobj;
  551. ret = drm_syncobj_create(&syncobj, flags, NULL);
  552. if (ret)
  553. return ret;
  554. ret = drm_syncobj_get_handle(file_private, syncobj, handle);
  555. drm_syncobj_put(syncobj);
  556. return ret;
  557. }
  558. static int drm_syncobj_destroy(struct drm_file *file_private,
  559. u32 handle)
  560. {
  561. struct drm_syncobj *syncobj;
  562. syncobj = xa_erase(&file_private->syncobj_xa, handle);
  563. if (!syncobj)
  564. return -EINVAL;
  565. drm_syncobj_put(syncobj);
  566. return 0;
  567. }
  568. static int drm_syncobj_file_release(struct inode *inode, struct file *file)
  569. {
  570. struct drm_syncobj *syncobj = file->private_data;
  571. drm_syncobj_put(syncobj);
  572. return 0;
  573. }
  574. static const struct file_operations drm_syncobj_file_fops = {
  575. .release = drm_syncobj_file_release,
  576. };
  577. /**
  578. * drm_syncobj_get_fd - get a file descriptor from a syncobj
  579. * @syncobj: Sync object to export
  580. * @p_fd: out parameter with the new file descriptor
  581. *
  582. * Exports a sync object created with drm_syncobj_create() as a file descriptor.
  583. *
  584. * Returns 0 on success or a negative error value on failure.
  585. */
  586. int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd)
  587. {
  588. struct file *file;
  589. int fd;
  590. fd = get_unused_fd_flags(O_CLOEXEC);
  591. if (fd < 0)
  592. return fd;
  593. file = anon_inode_getfile("syncobj_file",
  594. &drm_syncobj_file_fops,
  595. syncobj, 0);
  596. if (IS_ERR(file)) {
  597. put_unused_fd(fd);
  598. return PTR_ERR(file);
  599. }
  600. drm_syncobj_get(syncobj);
  601. fd_install(fd, file);
  602. *p_fd = fd;
  603. return 0;
  604. }
  605. EXPORT_SYMBOL(drm_syncobj_get_fd);
  606. static int drm_syncobj_handle_to_fd(struct drm_file *file_private,
  607. u32 handle, int *p_fd)
  608. {
  609. struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
  610. int ret;
  611. if (!syncobj)
  612. return -EINVAL;
  613. ret = drm_syncobj_get_fd(syncobj, p_fd);
  614. drm_syncobj_put(syncobj);
  615. return ret;
  616. }
  617. static int drm_syncobj_fd_to_handle(struct drm_file *file_private,
  618. int fd, u32 *handle)
  619. {
  620. struct drm_syncobj *syncobj;
  621. CLASS(fd, f)(fd);
  622. int ret;
  623. if (fd_empty(f))
  624. return -EINVAL;
  625. if (fd_file(f)->f_op != &drm_syncobj_file_fops)
  626. return -EINVAL;
  627. /* take a reference to put in the xarray */
  628. syncobj = fd_file(f)->private_data;
  629. drm_syncobj_get(syncobj);
  630. ret = xa_alloc(&file_private->syncobj_xa, handle, syncobj, xa_limit_32b,
  631. GFP_KERNEL);
  632. if (ret)
  633. drm_syncobj_put(syncobj);
  634. return ret;
  635. }
  636. static int drm_syncobj_import_sync_file_fence(struct drm_file *file_private,
  637. int fd, int handle, u64 point)
  638. {
  639. struct dma_fence *fence = sync_file_get_fence(fd);
  640. struct drm_syncobj *syncobj;
  641. if (!fence)
  642. return -EINVAL;
  643. syncobj = drm_syncobj_find(file_private, handle);
  644. if (!syncobj) {
  645. dma_fence_put(fence);
  646. return -ENOENT;
  647. }
  648. if (point) {
  649. struct dma_fence_chain *chain = dma_fence_chain_alloc();
  650. if (!chain)
  651. return -ENOMEM;
  652. drm_syncobj_add_point(syncobj, chain, fence, point);
  653. } else {
  654. drm_syncobj_replace_fence(syncobj, fence);
  655. }
  656. dma_fence_put(fence);
  657. drm_syncobj_put(syncobj);
  658. return 0;
  659. }
  660. static int drm_syncobj_export_sync_file(struct drm_file *file_private,
  661. int handle, u64 point, int *p_fd)
  662. {
  663. int ret;
  664. struct dma_fence *fence;
  665. struct sync_file *sync_file;
  666. int fd = get_unused_fd_flags(O_CLOEXEC);
  667. if (fd < 0)
  668. return fd;
  669. ret = drm_syncobj_find_fence(file_private, handle, point, 0, &fence);
  670. if (ret)
  671. goto err_put_fd;
  672. sync_file = sync_file_create(fence);
  673. dma_fence_put(fence);
  674. if (!sync_file) {
  675. ret = -EINVAL;
  676. goto err_put_fd;
  677. }
  678. fd_install(fd, sync_file->file);
  679. *p_fd = fd;
  680. return 0;
  681. err_put_fd:
  682. put_unused_fd(fd);
  683. return ret;
  684. }
  685. /**
  686. * drm_syncobj_open - initializes syncobj file-private structures at devnode open time
  687. * @file_private: drm file-private structure to set up
  688. *
  689. * Called at device open time, sets up the structure for handling refcounting
  690. * of sync objects.
  691. */
  692. void
  693. drm_syncobj_open(struct drm_file *file_private)
  694. {
  695. xa_init_flags(&file_private->syncobj_xa, XA_FLAGS_ALLOC1);
  696. }
  697. /**
  698. * drm_syncobj_release - release file-private sync object resources
  699. * @file_private: drm file-private structure to clean up
  700. *
  701. * Called at close time when the filp is going away.
  702. *
  703. * Releases any remaining references on objects by this filp.
  704. */
  705. void
  706. drm_syncobj_release(struct drm_file *file_private)
  707. {
  708. struct drm_syncobj *syncobj;
  709. unsigned long handle;
  710. xa_for_each(&file_private->syncobj_xa, handle, syncobj)
  711. drm_syncobj_put(syncobj);
  712. xa_destroy(&file_private->syncobj_xa);
  713. }
  714. int
  715. drm_syncobj_create_ioctl(struct drm_device *dev, void *data,
  716. struct drm_file *file_private)
  717. {
  718. struct drm_syncobj_create *args = data;
  719. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  720. return -EOPNOTSUPP;
  721. /* no valid flags yet */
  722. if (args->flags & ~DRM_SYNCOBJ_CREATE_SIGNALED)
  723. return -EINVAL;
  724. return drm_syncobj_create_as_handle(file_private,
  725. &args->handle, args->flags);
  726. }
  727. int
  728. drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data,
  729. struct drm_file *file_private)
  730. {
  731. struct drm_syncobj_destroy *args = data;
  732. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  733. return -EOPNOTSUPP;
  734. /* make sure padding is empty */
  735. if (args->pad)
  736. return -EINVAL;
  737. return drm_syncobj_destroy(file_private, args->handle);
  738. }
  739. int
  740. drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  741. struct drm_file *file_private)
  742. {
  743. struct drm_syncobj_handle *args = data;
  744. unsigned int valid_flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_TIMELINE |
  745. DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE;
  746. u64 point = 0;
  747. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  748. return -EOPNOTSUPP;
  749. if (args->pad)
  750. return -EINVAL;
  751. if (args->flags & ~valid_flags)
  752. return -EINVAL;
  753. if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_TIMELINE)
  754. point = args->point;
  755. if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
  756. return drm_syncobj_export_sync_file(file_private, args->handle,
  757. point, &args->fd);
  758. if (args->point)
  759. return -EINVAL;
  760. return drm_syncobj_handle_to_fd(file_private, args->handle,
  761. &args->fd);
  762. }
  763. int
  764. drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data,
  765. struct drm_file *file_private)
  766. {
  767. struct drm_syncobj_handle *args = data;
  768. unsigned int valid_flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_TIMELINE |
  769. DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE;
  770. u64 point = 0;
  771. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  772. return -EOPNOTSUPP;
  773. if (args->pad)
  774. return -EINVAL;
  775. if (args->flags & ~valid_flags)
  776. return -EINVAL;
  777. if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_TIMELINE)
  778. point = args->point;
  779. if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
  780. return drm_syncobj_import_sync_file_fence(file_private,
  781. args->fd,
  782. args->handle,
  783. point);
  784. if (args->point)
  785. return -EINVAL;
  786. return drm_syncobj_fd_to_handle(file_private, args->fd,
  787. &args->handle);
  788. }
  789. static int drm_syncobj_transfer_to_timeline(struct drm_file *file_private,
  790. struct drm_syncobj_transfer *args)
  791. {
  792. struct drm_syncobj *timeline_syncobj = NULL;
  793. struct dma_fence *fence, *tmp;
  794. struct dma_fence_chain *chain;
  795. int ret;
  796. timeline_syncobj = drm_syncobj_find(file_private, args->dst_handle);
  797. if (!timeline_syncobj) {
  798. return -ENOENT;
  799. }
  800. ret = drm_syncobj_find_fence(file_private, args->src_handle,
  801. args->src_point, args->flags,
  802. &tmp);
  803. if (ret)
  804. goto err_put_timeline;
  805. fence = dma_fence_unwrap_merge(tmp);
  806. dma_fence_put(tmp);
  807. if (!fence) {
  808. ret = -ENOMEM;
  809. goto err_put_timeline;
  810. }
  811. chain = dma_fence_chain_alloc();
  812. if (!chain) {
  813. ret = -ENOMEM;
  814. goto err_free_fence;
  815. }
  816. drm_syncobj_add_point(timeline_syncobj, chain, fence, args->dst_point);
  817. err_free_fence:
  818. dma_fence_put(fence);
  819. err_put_timeline:
  820. drm_syncobj_put(timeline_syncobj);
  821. return ret;
  822. }
  823. static int
  824. drm_syncobj_transfer_to_binary(struct drm_file *file_private,
  825. struct drm_syncobj_transfer *args)
  826. {
  827. struct drm_syncobj *binary_syncobj = NULL;
  828. struct dma_fence *fence;
  829. int ret;
  830. binary_syncobj = drm_syncobj_find(file_private, args->dst_handle);
  831. if (!binary_syncobj)
  832. return -ENOENT;
  833. ret = drm_syncobj_find_fence(file_private, args->src_handle,
  834. args->src_point, args->flags, &fence);
  835. if (ret)
  836. goto err;
  837. drm_syncobj_replace_fence(binary_syncobj, fence);
  838. dma_fence_put(fence);
  839. err:
  840. drm_syncobj_put(binary_syncobj);
  841. return ret;
  842. }
  843. int
  844. drm_syncobj_transfer_ioctl(struct drm_device *dev, void *data,
  845. struct drm_file *file_private)
  846. {
  847. struct drm_syncobj_transfer *args = data;
  848. int ret;
  849. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
  850. return -EOPNOTSUPP;
  851. if (args->pad)
  852. return -EINVAL;
  853. if (args->dst_point)
  854. ret = drm_syncobj_transfer_to_timeline(file_private, args);
  855. else
  856. ret = drm_syncobj_transfer_to_binary(file_private, args);
  857. return ret;
  858. }
  859. static void syncobj_wait_fence_func(struct dma_fence *fence,
  860. struct dma_fence_cb *cb)
  861. {
  862. struct syncobj_wait_entry *wait =
  863. container_of(cb, struct syncobj_wait_entry, fence_cb);
  864. wake_up_process(wait->task);
  865. }
  866. static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
  867. struct syncobj_wait_entry *wait)
  868. {
  869. struct dma_fence *fence;
  870. /* This happens inside the syncobj lock */
  871. fence = rcu_dereference_protected(syncobj->fence,
  872. lockdep_is_held(&syncobj->lock));
  873. dma_fence_get(fence);
  874. if (!fence || dma_fence_chain_find_seqno(&fence, wait->point)) {
  875. dma_fence_put(fence);
  876. return;
  877. } else if (!fence) {
  878. wait->fence = dma_fence_get_stub();
  879. } else {
  880. wait->fence = fence;
  881. }
  882. wake_up_process(wait->task);
  883. list_del_init(&wait->node);
  884. }
  885. static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
  886. void __user *user_points,
  887. uint32_t count,
  888. uint32_t flags,
  889. signed long timeout,
  890. uint32_t *idx,
  891. ktime_t *deadline)
  892. {
  893. struct syncobj_wait_entry *entries;
  894. struct dma_fence *fence;
  895. uint64_t *points;
  896. uint32_t signaled_count, i;
  897. if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |
  898. DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE)) {
  899. might_sleep();
  900. lockdep_assert_none_held_once();
  901. }
  902. points = kmalloc_array(count, sizeof(*points), GFP_KERNEL);
  903. if (points == NULL)
  904. return -ENOMEM;
  905. if (!user_points) {
  906. memset(points, 0, count * sizeof(uint64_t));
  907. } else if (copy_from_user(points, user_points,
  908. sizeof(uint64_t) * count)) {
  909. timeout = -EFAULT;
  910. goto err_free_points;
  911. }
  912. entries = kzalloc_objs(*entries, count);
  913. if (!entries) {
  914. timeout = -ENOMEM;
  915. goto err_free_points;
  916. }
  917. /* Walk the list of sync objects and initialize entries. We do
  918. * this up-front so that we can properly return -EINVAL if there is
  919. * a syncobj with a missing fence and then never have the chance of
  920. * returning -EINVAL again.
  921. */
  922. signaled_count = 0;
  923. for (i = 0; i < count; ++i) {
  924. struct dma_fence *fence;
  925. entries[i].task = current;
  926. entries[i].point = points[i];
  927. fence = drm_syncobj_fence_get(syncobjs[i]);
  928. if (!fence || dma_fence_chain_find_seqno(&fence, points[i])) {
  929. dma_fence_put(fence);
  930. if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |
  931. DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE)) {
  932. continue;
  933. } else {
  934. timeout = -EINVAL;
  935. goto cleanup_entries;
  936. }
  937. }
  938. if (fence)
  939. entries[i].fence = fence;
  940. else
  941. entries[i].fence = dma_fence_get_stub();
  942. if ((flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE) ||
  943. dma_fence_is_signaled(entries[i].fence)) {
  944. if (signaled_count == 0 && idx)
  945. *idx = i;
  946. signaled_count++;
  947. }
  948. }
  949. if (signaled_count == count ||
  950. (signaled_count > 0 &&
  951. !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL)))
  952. goto cleanup_entries;
  953. /* There's a very annoying laxness in the dma_fence API here, in
  954. * that backends are not required to automatically report when a
  955. * fence is signaled prior to fence->ops->enable_signaling() being
  956. * called. So here if we fail to match signaled_count, we need to
  957. * fallthough and try a 0 timeout wait!
  958. */
  959. if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |
  960. DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE)) {
  961. for (i = 0; i < count; ++i)
  962. drm_syncobj_fence_add_wait(syncobjs[i], &entries[i]);
  963. }
  964. if (deadline) {
  965. for (i = 0; i < count; ++i) {
  966. fence = entries[i].fence;
  967. if (!fence)
  968. continue;
  969. dma_fence_set_deadline(fence, *deadline);
  970. }
  971. }
  972. do {
  973. set_current_state(TASK_INTERRUPTIBLE);
  974. signaled_count = 0;
  975. for (i = 0; i < count; ++i) {
  976. fence = entries[i].fence;
  977. if (!fence)
  978. continue;
  979. if ((flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE) ||
  980. dma_fence_is_signaled(fence) ||
  981. (!entries[i].fence_cb.func &&
  982. dma_fence_add_callback(fence,
  983. &entries[i].fence_cb,
  984. syncobj_wait_fence_func))) {
  985. /* The fence has been signaled */
  986. if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) {
  987. signaled_count++;
  988. } else {
  989. if (idx)
  990. *idx = i;
  991. goto done_waiting;
  992. }
  993. }
  994. }
  995. if (signaled_count == count)
  996. goto done_waiting;
  997. if (timeout == 0) {
  998. timeout = -ETIME;
  999. goto done_waiting;
  1000. }
  1001. if (signal_pending(current)) {
  1002. timeout = -ERESTARTSYS;
  1003. goto done_waiting;
  1004. }
  1005. timeout = schedule_timeout(timeout);
  1006. } while (1);
  1007. done_waiting:
  1008. __set_current_state(TASK_RUNNING);
  1009. cleanup_entries:
  1010. for (i = 0; i < count; ++i) {
  1011. drm_syncobj_remove_wait(syncobjs[i], &entries[i]);
  1012. if (entries[i].fence_cb.func)
  1013. dma_fence_remove_callback(entries[i].fence,
  1014. &entries[i].fence_cb);
  1015. dma_fence_put(entries[i].fence);
  1016. }
  1017. kfree(entries);
  1018. err_free_points:
  1019. kfree(points);
  1020. return timeout;
  1021. }
  1022. /**
  1023. * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value
  1024. *
  1025. * @timeout_nsec: timeout nsec component in ns, 0 for poll
  1026. *
  1027. * Calculate the timeout in jiffies from an absolute time in sec/nsec.
  1028. */
  1029. signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec)
  1030. {
  1031. ktime_t abs_timeout, now;
  1032. u64 timeout_ns, timeout_jiffies64;
  1033. /* make 0 timeout means poll - absolute 0 doesn't seem valid */
  1034. if (timeout_nsec == 0)
  1035. return 0;
  1036. abs_timeout = ns_to_ktime(timeout_nsec);
  1037. now = ktime_get();
  1038. if (!ktime_after(abs_timeout, now))
  1039. return 0;
  1040. timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now));
  1041. timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns);
  1042. /* clamp timeout to avoid infinite timeout */
  1043. if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1)
  1044. return MAX_SCHEDULE_TIMEOUT - 1;
  1045. return timeout_jiffies64 + 1;
  1046. }
  1047. EXPORT_SYMBOL(drm_timeout_abs_to_jiffies);
  1048. static int drm_syncobj_array_wait(struct drm_device *dev,
  1049. struct drm_file *file_private,
  1050. struct drm_syncobj_wait *wait,
  1051. struct drm_syncobj_timeline_wait *timeline_wait,
  1052. struct drm_syncobj **syncobjs, bool timeline,
  1053. ktime_t *deadline)
  1054. {
  1055. signed long timeout = 0;
  1056. uint32_t first = ~0;
  1057. if (!timeline) {
  1058. timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec);
  1059. timeout = drm_syncobj_array_wait_timeout(syncobjs,
  1060. NULL,
  1061. wait->count_handles,
  1062. wait->flags,
  1063. timeout, &first,
  1064. deadline);
  1065. if (timeout < 0)
  1066. return timeout;
  1067. wait->first_signaled = first;
  1068. } else {
  1069. timeout = drm_timeout_abs_to_jiffies(timeline_wait->timeout_nsec);
  1070. timeout = drm_syncobj_array_wait_timeout(syncobjs,
  1071. u64_to_user_ptr(timeline_wait->points),
  1072. timeline_wait->count_handles,
  1073. timeline_wait->flags,
  1074. timeout, &first,
  1075. deadline);
  1076. if (timeout < 0)
  1077. return timeout;
  1078. timeline_wait->first_signaled = first;
  1079. }
  1080. return 0;
  1081. }
  1082. static int drm_syncobj_array_find(struct drm_file *file_private,
  1083. void __user *user_handles,
  1084. uint32_t count_handles,
  1085. struct drm_syncobj ***syncobjs_out)
  1086. {
  1087. uint32_t i, *handles;
  1088. struct drm_syncobj **syncobjs;
  1089. int ret;
  1090. handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL);
  1091. if (handles == NULL)
  1092. return -ENOMEM;
  1093. if (copy_from_user(handles, user_handles,
  1094. sizeof(uint32_t) * count_handles)) {
  1095. ret = -EFAULT;
  1096. goto err_free_handles;
  1097. }
  1098. syncobjs = kmalloc_objs(*syncobjs, count_handles);
  1099. if (syncobjs == NULL) {
  1100. ret = -ENOMEM;
  1101. goto err_free_handles;
  1102. }
  1103. for (i = 0; i < count_handles; i++) {
  1104. syncobjs[i] = drm_syncobj_find(file_private, handles[i]);
  1105. if (!syncobjs[i]) {
  1106. ret = -ENOENT;
  1107. goto err_put_syncobjs;
  1108. }
  1109. }
  1110. kfree(handles);
  1111. *syncobjs_out = syncobjs;
  1112. return 0;
  1113. err_put_syncobjs:
  1114. while (i-- > 0)
  1115. drm_syncobj_put(syncobjs[i]);
  1116. kfree(syncobjs);
  1117. err_free_handles:
  1118. kfree(handles);
  1119. return ret;
  1120. }
  1121. static void drm_syncobj_array_free(struct drm_syncobj **syncobjs,
  1122. uint32_t count)
  1123. {
  1124. uint32_t i;
  1125. for (i = 0; i < count; i++)
  1126. drm_syncobj_put(syncobjs[i]);
  1127. kfree(syncobjs);
  1128. }
  1129. int
  1130. drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,
  1131. struct drm_file *file_private)
  1132. {
  1133. struct drm_syncobj_wait *args = data;
  1134. struct drm_syncobj **syncobjs;
  1135. unsigned int possible_flags;
  1136. ktime_t t, *tp = NULL;
  1137. int ret = 0;
  1138. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  1139. return -EOPNOTSUPP;
  1140. possible_flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
  1141. DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |
  1142. DRM_SYNCOBJ_WAIT_FLAGS_WAIT_DEADLINE;
  1143. if (args->flags & ~possible_flags)
  1144. return -EINVAL;
  1145. if (args->count_handles == 0)
  1146. return 0;
  1147. ret = drm_syncobj_array_find(file_private,
  1148. u64_to_user_ptr(args->handles),
  1149. args->count_handles,
  1150. &syncobjs);
  1151. if (ret < 0)
  1152. return ret;
  1153. if (args->flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_DEADLINE) {
  1154. t = ns_to_ktime(args->deadline_nsec);
  1155. tp = &t;
  1156. }
  1157. ret = drm_syncobj_array_wait(dev, file_private,
  1158. args, NULL, syncobjs, false, tp);
  1159. drm_syncobj_array_free(syncobjs, args->count_handles);
  1160. return ret;
  1161. }
  1162. int
  1163. drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data,
  1164. struct drm_file *file_private)
  1165. {
  1166. struct drm_syncobj_timeline_wait *args = data;
  1167. struct drm_syncobj **syncobjs;
  1168. unsigned int possible_flags;
  1169. ktime_t t, *tp = NULL;
  1170. int ret = 0;
  1171. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
  1172. return -EOPNOTSUPP;
  1173. possible_flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
  1174. DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |
  1175. DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE |
  1176. DRM_SYNCOBJ_WAIT_FLAGS_WAIT_DEADLINE;
  1177. if (args->flags & ~possible_flags)
  1178. return -EINVAL;
  1179. if (args->count_handles == 0)
  1180. return 0;
  1181. ret = drm_syncobj_array_find(file_private,
  1182. u64_to_user_ptr(args->handles),
  1183. args->count_handles,
  1184. &syncobjs);
  1185. if (ret < 0)
  1186. return ret;
  1187. if (args->flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_DEADLINE) {
  1188. t = ns_to_ktime(args->deadline_nsec);
  1189. tp = &t;
  1190. }
  1191. ret = drm_syncobj_array_wait(dev, file_private,
  1192. NULL, args, syncobjs, true, tp);
  1193. drm_syncobj_array_free(syncobjs, args->count_handles);
  1194. return ret;
  1195. }
  1196. static void syncobj_eventfd_entry_fence_func(struct dma_fence *fence,
  1197. struct dma_fence_cb *cb)
  1198. {
  1199. struct syncobj_eventfd_entry *entry =
  1200. container_of(cb, struct syncobj_eventfd_entry, fence_cb);
  1201. eventfd_signal(entry->ev_fd_ctx);
  1202. syncobj_eventfd_entry_free(entry);
  1203. }
  1204. static void
  1205. syncobj_eventfd_entry_func(struct drm_syncobj *syncobj,
  1206. struct syncobj_eventfd_entry *entry)
  1207. {
  1208. int ret;
  1209. struct dma_fence *fence;
  1210. /* This happens inside the syncobj lock */
  1211. fence = dma_fence_get(rcu_dereference_protected(syncobj->fence, 1));
  1212. if (!fence)
  1213. return;
  1214. ret = dma_fence_chain_find_seqno(&fence, entry->point);
  1215. if (ret != 0) {
  1216. /* The given seqno has not been submitted yet. */
  1217. dma_fence_put(fence);
  1218. return;
  1219. } else if (!fence) {
  1220. /* If dma_fence_chain_find_seqno returns 0 but sets the fence
  1221. * to NULL, it implies that the given seqno is signaled and a
  1222. * later seqno has already been submitted. Assign a stub fence
  1223. * so that the eventfd still gets signaled below.
  1224. */
  1225. fence = dma_fence_get_stub();
  1226. }
  1227. list_del_init(&entry->node);
  1228. entry->fence = fence;
  1229. if (entry->flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE) {
  1230. eventfd_signal(entry->ev_fd_ctx);
  1231. syncobj_eventfd_entry_free(entry);
  1232. } else {
  1233. ret = dma_fence_add_callback(fence, &entry->fence_cb,
  1234. syncobj_eventfd_entry_fence_func);
  1235. if (ret == -ENOENT) {
  1236. eventfd_signal(entry->ev_fd_ctx);
  1237. syncobj_eventfd_entry_free(entry);
  1238. }
  1239. }
  1240. }
  1241. int
  1242. drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data,
  1243. struct drm_file *file_private)
  1244. {
  1245. struct drm_syncobj_eventfd *args = data;
  1246. struct drm_syncobj *syncobj;
  1247. struct eventfd_ctx *ev_fd_ctx;
  1248. struct syncobj_eventfd_entry *entry;
  1249. int ret;
  1250. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
  1251. return -EOPNOTSUPP;
  1252. if (args->flags & ~DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE)
  1253. return -EINVAL;
  1254. if (args->pad)
  1255. return -EINVAL;
  1256. syncobj = drm_syncobj_find(file_private, args->handle);
  1257. if (!syncobj)
  1258. return -ENOENT;
  1259. ev_fd_ctx = eventfd_ctx_fdget(args->fd);
  1260. if (IS_ERR(ev_fd_ctx)) {
  1261. ret = PTR_ERR(ev_fd_ctx);
  1262. goto err_fdget;
  1263. }
  1264. entry = kzalloc_obj(*entry);
  1265. if (!entry) {
  1266. ret = -ENOMEM;
  1267. goto err_kzalloc;
  1268. }
  1269. entry->syncobj = syncobj;
  1270. entry->ev_fd_ctx = ev_fd_ctx;
  1271. entry->point = args->point;
  1272. entry->flags = args->flags;
  1273. drm_syncobj_add_eventfd(syncobj, entry);
  1274. drm_syncobj_put(syncobj);
  1275. return 0;
  1276. err_kzalloc:
  1277. eventfd_ctx_put(ev_fd_ctx);
  1278. err_fdget:
  1279. drm_syncobj_put(syncobj);
  1280. return ret;
  1281. }
  1282. int
  1283. drm_syncobj_reset_ioctl(struct drm_device *dev, void *data,
  1284. struct drm_file *file_private)
  1285. {
  1286. struct drm_syncobj_array *args = data;
  1287. struct drm_syncobj **syncobjs;
  1288. uint32_t i;
  1289. int ret;
  1290. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  1291. return -EOPNOTSUPP;
  1292. if (args->pad != 0)
  1293. return -EINVAL;
  1294. if (args->count_handles == 0)
  1295. return -EINVAL;
  1296. ret = drm_syncobj_array_find(file_private,
  1297. u64_to_user_ptr(args->handles),
  1298. args->count_handles,
  1299. &syncobjs);
  1300. if (ret < 0)
  1301. return ret;
  1302. for (i = 0; i < args->count_handles; i++)
  1303. drm_syncobj_replace_fence(syncobjs[i], NULL);
  1304. drm_syncobj_array_free(syncobjs, args->count_handles);
  1305. return 0;
  1306. }
  1307. int
  1308. drm_syncobj_signal_ioctl(struct drm_device *dev, void *data,
  1309. struct drm_file *file_private)
  1310. {
  1311. struct drm_syncobj_array *args = data;
  1312. struct drm_syncobj **syncobjs;
  1313. uint32_t i;
  1314. int ret;
  1315. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  1316. return -EOPNOTSUPP;
  1317. if (args->pad != 0)
  1318. return -EINVAL;
  1319. if (args->count_handles == 0)
  1320. return -EINVAL;
  1321. ret = drm_syncobj_array_find(file_private,
  1322. u64_to_user_ptr(args->handles),
  1323. args->count_handles,
  1324. &syncobjs);
  1325. if (ret < 0)
  1326. return ret;
  1327. for (i = 0; i < args->count_handles; i++) {
  1328. ret = drm_syncobj_assign_null_handle(syncobjs[i]);
  1329. if (ret < 0)
  1330. break;
  1331. }
  1332. drm_syncobj_array_free(syncobjs, args->count_handles);
  1333. return ret;
  1334. }
  1335. int
  1336. drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,
  1337. struct drm_file *file_private)
  1338. {
  1339. struct drm_syncobj_timeline_array *args = data;
  1340. struct drm_syncobj **syncobjs;
  1341. struct dma_fence_chain **chains;
  1342. uint64_t *points;
  1343. uint32_t i, j;
  1344. int ret;
  1345. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
  1346. return -EOPNOTSUPP;
  1347. if (args->flags != 0)
  1348. return -EINVAL;
  1349. if (args->count_handles == 0)
  1350. return -EINVAL;
  1351. ret = drm_syncobj_array_find(file_private,
  1352. u64_to_user_ptr(args->handles),
  1353. args->count_handles,
  1354. &syncobjs);
  1355. if (ret < 0)
  1356. return ret;
  1357. points = kmalloc_array(args->count_handles, sizeof(*points),
  1358. GFP_KERNEL);
  1359. if (!points) {
  1360. ret = -ENOMEM;
  1361. goto out;
  1362. }
  1363. if (!u64_to_user_ptr(args->points)) {
  1364. memset(points, 0, args->count_handles * sizeof(uint64_t));
  1365. } else if (copy_from_user(points, u64_to_user_ptr(args->points),
  1366. sizeof(uint64_t) * args->count_handles)) {
  1367. ret = -EFAULT;
  1368. goto err_points;
  1369. }
  1370. chains = kmalloc_array(args->count_handles, sizeof(void *), GFP_KERNEL);
  1371. if (!chains) {
  1372. ret = -ENOMEM;
  1373. goto err_points;
  1374. }
  1375. for (i = 0; i < args->count_handles; i++) {
  1376. chains[i] = dma_fence_chain_alloc();
  1377. if (!chains[i]) {
  1378. for (j = 0; j < i; j++)
  1379. dma_fence_chain_free(chains[j]);
  1380. ret = -ENOMEM;
  1381. goto err_chains;
  1382. }
  1383. }
  1384. for (i = 0; i < args->count_handles; i++) {
  1385. struct dma_fence *fence = dma_fence_get_stub();
  1386. drm_syncobj_add_point(syncobjs[i], chains[i],
  1387. fence, points[i]);
  1388. dma_fence_put(fence);
  1389. }
  1390. err_chains:
  1391. kfree(chains);
  1392. err_points:
  1393. kfree(points);
  1394. out:
  1395. drm_syncobj_array_free(syncobjs, args->count_handles);
  1396. return ret;
  1397. }
  1398. int drm_syncobj_query_ioctl(struct drm_device *dev, void *data,
  1399. struct drm_file *file_private)
  1400. {
  1401. struct drm_syncobj_timeline_array *args = data;
  1402. struct drm_syncobj **syncobjs;
  1403. uint64_t __user *points = u64_to_user_ptr(args->points);
  1404. uint32_t i;
  1405. int ret;
  1406. if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
  1407. return -EOPNOTSUPP;
  1408. if (args->flags & ~DRM_SYNCOBJ_QUERY_FLAGS_LAST_SUBMITTED)
  1409. return -EINVAL;
  1410. if (args->count_handles == 0)
  1411. return -EINVAL;
  1412. ret = drm_syncobj_array_find(file_private,
  1413. u64_to_user_ptr(args->handles),
  1414. args->count_handles,
  1415. &syncobjs);
  1416. if (ret < 0)
  1417. return ret;
  1418. for (i = 0; i < args->count_handles; i++) {
  1419. struct dma_fence_chain *chain;
  1420. struct dma_fence *fence;
  1421. uint64_t point;
  1422. fence = drm_syncobj_fence_get(syncobjs[i]);
  1423. chain = to_dma_fence_chain(fence);
  1424. if (chain) {
  1425. struct dma_fence *iter, *last_signaled =
  1426. dma_fence_get(fence);
  1427. if (args->flags &
  1428. DRM_SYNCOBJ_QUERY_FLAGS_LAST_SUBMITTED) {
  1429. point = fence->seqno;
  1430. } else {
  1431. dma_fence_chain_for_each(iter, fence) {
  1432. if (iter->context != fence->context) {
  1433. dma_fence_put(iter);
  1434. /* It is most likely that timeline has
  1435. * unorder points. */
  1436. break;
  1437. }
  1438. dma_fence_put(last_signaled);
  1439. last_signaled = dma_fence_get(iter);
  1440. }
  1441. point = dma_fence_is_signaled(last_signaled) ?
  1442. last_signaled->seqno :
  1443. to_dma_fence_chain(last_signaled)->prev_seqno;
  1444. }
  1445. dma_fence_put(last_signaled);
  1446. } else {
  1447. point = 0;
  1448. }
  1449. dma_fence_put(fence);
  1450. ret = copy_to_user(&points[i], &point, sizeof(uint64_t));
  1451. ret = ret ? -EFAULT : 0;
  1452. if (ret)
  1453. break;
  1454. }
  1455. drm_syncobj_array_free(syncobjs, args->count_handles);
  1456. return ret;
  1457. }