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