i915_gem_request.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * Copyright © 2008-2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #ifndef I915_GEM_REQUEST_H
  25. #define I915_GEM_REQUEST_H
  26. #include <linux/dma-fence.h>
  27. #include "i915_gem.h"
  28. #include "i915_sw_fence.h"
  29. struct drm_file;
  30. struct drm_i915_gem_object;
  31. struct drm_i915_gem_request;
  32. struct intel_wait {
  33. struct rb_node node;
  34. struct task_struct *tsk;
  35. struct drm_i915_gem_request *request;
  36. u32 seqno;
  37. };
  38. struct intel_signal_node {
  39. struct rb_node node;
  40. struct intel_wait wait;
  41. };
  42. struct i915_dependency {
  43. struct i915_priotree *signaler;
  44. struct list_head signal_link;
  45. struct list_head wait_link;
  46. struct list_head dfs_link;
  47. unsigned long flags;
  48. #define I915_DEPENDENCY_ALLOC BIT(0)
  49. };
  50. /* Requests exist in a complex web of interdependencies. Each request
  51. * has to wait for some other request to complete before it is ready to be run
  52. * (e.g. we have to wait until the pixels have been rendering into a texture
  53. * before we can copy from it). We track the readiness of a request in terms
  54. * of fences, but we also need to keep the dependency tree for the lifetime
  55. * of the request (beyond the life of an individual fence). We use the tree
  56. * at various points to reorder the requests whilst keeping the requests
  57. * in order with respect to their various dependencies.
  58. */
  59. struct i915_priotree {
  60. struct list_head signalers_list; /* those before us, we depend upon */
  61. struct list_head waiters_list; /* those after us, they depend upon us */
  62. struct rb_node node;
  63. int priority;
  64. #define I915_PRIORITY_MAX 1024
  65. #define I915_PRIORITY_MIN (-I915_PRIORITY_MAX)
  66. };
  67. struct i915_gem_capture_list {
  68. struct i915_gem_capture_list *next;
  69. struct i915_vma *vma;
  70. };
  71. /**
  72. * Request queue structure.
  73. *
  74. * The request queue allows us to note sequence numbers that have been emitted
  75. * and may be associated with active buffers to be retired.
  76. *
  77. * By keeping this list, we can avoid having to do questionable sequence
  78. * number comparisons on buffer last_read|write_seqno. It also allows an
  79. * emission time to be associated with the request for tracking how far ahead
  80. * of the GPU the submission is.
  81. *
  82. * When modifying this structure be very aware that we perform a lockless
  83. * RCU lookup of it that may race against reallocation of the struct
  84. * from the slab freelist. We intentionally do not zero the structure on
  85. * allocation so that the lookup can use the dangling pointers (and is
  86. * cogniscent that those pointers may be wrong). Instead, everything that
  87. * needs to be initialised must be done so explicitly.
  88. *
  89. * The requests are reference counted.
  90. */
  91. struct drm_i915_gem_request {
  92. struct dma_fence fence;
  93. spinlock_t lock;
  94. /** On Which ring this request was generated */
  95. struct drm_i915_private *i915;
  96. /**
  97. * Context and ring buffer related to this request
  98. * Contexts are refcounted, so when this request is associated with a
  99. * context, we must increment the context's refcount, to guarantee that
  100. * it persists while any request is linked to it. Requests themselves
  101. * are also refcounted, so the request will only be freed when the last
  102. * reference to it is dismissed, and the code in
  103. * i915_gem_request_free() will then decrement the refcount on the
  104. * context.
  105. */
  106. struct i915_gem_context *ctx;
  107. struct intel_engine_cs *engine;
  108. struct intel_ring *ring;
  109. struct intel_timeline *timeline;
  110. struct intel_signal_node signaling;
  111. /* Fences for the various phases in the request's lifetime.
  112. *
  113. * The submit fence is used to await upon all of the request's
  114. * dependencies. When it is signaled, the request is ready to run.
  115. * It is used by the driver to then queue the request for execution.
  116. */
  117. struct i915_sw_fence submit;
  118. wait_queue_t submitq;
  119. wait_queue_head_t execute;
  120. /* A list of everyone we wait upon, and everyone who waits upon us.
  121. * Even though we will not be submitted to the hardware before the
  122. * submit fence is signaled (it waits for all external events as well
  123. * as our own requests), the scheduler still needs to know the
  124. * dependency tree for the lifetime of the request (from execbuf
  125. * to retirement), i.e. bidirectional dependency information for the
  126. * request not tied to individual fences.
  127. */
  128. struct i915_priotree priotree;
  129. struct i915_dependency dep;
  130. /** GEM sequence number associated with this request on the
  131. * global execution timeline. It is zero when the request is not
  132. * on the HW queue (i.e. not on the engine timeline list).
  133. * Its value is guarded by the timeline spinlock.
  134. */
  135. u32 global_seqno;
  136. /** Position in the ring of the start of the request */
  137. u32 head;
  138. /**
  139. * Position in the ring of the start of the postfix.
  140. * This is required to calculate the maximum available ring space
  141. * without overwriting the postfix.
  142. */
  143. u32 postfix;
  144. /** Position in the ring of the end of the whole request */
  145. u32 tail;
  146. /** Position in the ring of the end of any workarounds after the tail */
  147. u32 wa_tail;
  148. /** Preallocate space in the ring for the emitting the request */
  149. u32 reserved_space;
  150. /** Batch buffer related to this request if any (used for
  151. * error state dump only).
  152. */
  153. struct i915_vma *batch;
  154. /** Additional buffers requested by userspace to be captured upon
  155. * a GPU hang. The vma/obj on this list are protected by their
  156. * active reference - all objects on this list must also be
  157. * on the active_list (of their final request).
  158. */
  159. struct i915_gem_capture_list *capture_list;
  160. struct list_head active_list;
  161. /** Time at which this request was emitted, in jiffies. */
  162. unsigned long emitted_jiffies;
  163. /** engine->request_list entry for this request */
  164. struct list_head link;
  165. /** ring->request_list entry for this request */
  166. struct list_head ring_link;
  167. struct drm_i915_file_private *file_priv;
  168. /** file_priv list entry for this request */
  169. struct list_head client_link;
  170. };
  171. extern const struct dma_fence_ops i915_fence_ops;
  172. static inline bool dma_fence_is_i915(const struct dma_fence *fence)
  173. {
  174. return fence->ops == &i915_fence_ops;
  175. }
  176. struct drm_i915_gem_request * __must_check
  177. i915_gem_request_alloc(struct intel_engine_cs *engine,
  178. struct i915_gem_context *ctx);
  179. void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
  180. static inline struct drm_i915_gem_request *
  181. to_request(struct dma_fence *fence)
  182. {
  183. /* We assume that NULL fence/request are interoperable */
  184. BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
  185. GEM_BUG_ON(fence && !dma_fence_is_i915(fence));
  186. return container_of(fence, struct drm_i915_gem_request, fence);
  187. }
  188. static inline struct drm_i915_gem_request *
  189. i915_gem_request_get(struct drm_i915_gem_request *req)
  190. {
  191. return to_request(dma_fence_get(&req->fence));
  192. }
  193. static inline struct drm_i915_gem_request *
  194. i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
  195. {
  196. return to_request(dma_fence_get_rcu(&req->fence));
  197. }
  198. static inline void
  199. i915_gem_request_put(struct drm_i915_gem_request *req)
  200. {
  201. dma_fence_put(&req->fence);
  202. }
  203. static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
  204. struct drm_i915_gem_request *src)
  205. {
  206. if (src)
  207. i915_gem_request_get(src);
  208. if (*pdst)
  209. i915_gem_request_put(*pdst);
  210. *pdst = src;
  211. }
  212. /**
  213. * i915_gem_request_global_seqno - report the current global seqno
  214. * @request - the request
  215. *
  216. * A request is assigned a global seqno only when it is on the hardware
  217. * execution queue. The global seqno can be used to maintain a list of
  218. * requests on the same engine in retirement order, for example for
  219. * constructing a priority queue for waiting. Prior to its execution, or
  220. * if it is subsequently removed in the event of preemption, its global
  221. * seqno is zero. As both insertion and removal from the execution queue
  222. * may operate in IRQ context, it is not guarded by the usual struct_mutex
  223. * BKL. Instead those relying on the global seqno must be prepared for its
  224. * value to change between reads. Only when the request is complete can
  225. * the global seqno be stable (due to the memory barriers on submitting
  226. * the commands to the hardware to write the breadcrumb, if the HWS shows
  227. * that it has passed the global seqno and the global seqno is unchanged
  228. * after the read, it is indeed complete).
  229. */
  230. static u32
  231. i915_gem_request_global_seqno(const struct drm_i915_gem_request *request)
  232. {
  233. return READ_ONCE(request->global_seqno);
  234. }
  235. int
  236. i915_gem_request_await_object(struct drm_i915_gem_request *to,
  237. struct drm_i915_gem_object *obj,
  238. bool write);
  239. int i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
  240. struct dma_fence *fence);
  241. void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches);
  242. #define i915_add_request(req) \
  243. __i915_add_request(req, false)
  244. void __i915_gem_request_submit(struct drm_i915_gem_request *request);
  245. void i915_gem_request_submit(struct drm_i915_gem_request *request);
  246. void __i915_gem_request_unsubmit(struct drm_i915_gem_request *request);
  247. void i915_gem_request_unsubmit(struct drm_i915_gem_request *request);
  248. struct intel_rps_client;
  249. #define NO_WAITBOOST ERR_PTR(-1)
  250. #define IS_RPS_CLIENT(p) (!IS_ERR(p))
  251. #define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
  252. long i915_wait_request(struct drm_i915_gem_request *req,
  253. unsigned int flags,
  254. long timeout)
  255. __attribute__((nonnull(1)));
  256. #define I915_WAIT_INTERRUPTIBLE BIT(0)
  257. #define I915_WAIT_LOCKED BIT(1) /* struct_mutex held, handle GPU reset */
  258. #define I915_WAIT_ALL BIT(2) /* used by i915_gem_object_wait() */
  259. static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
  260. /**
  261. * Returns true if seq1 is later than seq2.
  262. */
  263. static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
  264. {
  265. return (s32)(seq1 - seq2) >= 0;
  266. }
  267. static inline bool
  268. __i915_gem_request_started(const struct drm_i915_gem_request *req, u32 seqno)
  269. {
  270. GEM_BUG_ON(!seqno);
  271. return i915_seqno_passed(intel_engine_get_seqno(req->engine),
  272. seqno - 1);
  273. }
  274. static inline bool
  275. i915_gem_request_started(const struct drm_i915_gem_request *req)
  276. {
  277. u32 seqno;
  278. seqno = i915_gem_request_global_seqno(req);
  279. if (!seqno)
  280. return false;
  281. return __i915_gem_request_started(req, seqno);
  282. }
  283. static inline bool
  284. __i915_gem_request_completed(const struct drm_i915_gem_request *req, u32 seqno)
  285. {
  286. GEM_BUG_ON(!seqno);
  287. return i915_seqno_passed(intel_engine_get_seqno(req->engine), seqno) &&
  288. seqno == i915_gem_request_global_seqno(req);
  289. }
  290. static inline bool
  291. i915_gem_request_completed(const struct drm_i915_gem_request *req)
  292. {
  293. u32 seqno;
  294. seqno = i915_gem_request_global_seqno(req);
  295. if (!seqno)
  296. return false;
  297. return __i915_gem_request_completed(req, seqno);
  298. }
  299. bool __i915_spin_request(const struct drm_i915_gem_request *request,
  300. u32 seqno, int state, unsigned long timeout_us);
  301. static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
  302. int state, unsigned long timeout_us)
  303. {
  304. u32 seqno;
  305. seqno = i915_gem_request_global_seqno(request);
  306. if (!seqno)
  307. return 0;
  308. return (__i915_gem_request_started(request, seqno) &&
  309. __i915_spin_request(request, seqno, state, timeout_us));
  310. }
  311. /* We treat requests as fences. This is not be to confused with our
  312. * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
  313. * We use the fences to synchronize access from the CPU with activity on the
  314. * GPU, for example, we should not rewrite an object's PTE whilst the GPU
  315. * is reading them. We also track fences at a higher level to provide
  316. * implicit synchronisation around GEM objects, e.g. set-domain will wait
  317. * for outstanding GPU rendering before marking the object ready for CPU
  318. * access, or a pageflip will wait until the GPU is complete before showing
  319. * the frame on the scanout.
  320. *
  321. * In order to use a fence, the object must track the fence it needs to
  322. * serialise with. For example, GEM objects want to track both read and
  323. * write access so that we can perform concurrent read operations between
  324. * the CPU and GPU engines, as well as waiting for all rendering to
  325. * complete, or waiting for the last GPU user of a "fence register". The
  326. * object then embeds a #i915_gem_active to track the most recent (in
  327. * retirement order) request relevant for the desired mode of access.
  328. * The #i915_gem_active is updated with i915_gem_active_set() to track the
  329. * most recent fence request, typically this is done as part of
  330. * i915_vma_move_to_active().
  331. *
  332. * When the #i915_gem_active completes (is retired), it will
  333. * signal its completion to the owner through a callback as well as mark
  334. * itself as idle (i915_gem_active.request == NULL). The owner
  335. * can then perform any action, such as delayed freeing of an active
  336. * resource including itself.
  337. */
  338. struct i915_gem_active;
  339. typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
  340. struct drm_i915_gem_request *);
  341. struct i915_gem_active {
  342. struct drm_i915_gem_request __rcu *request;
  343. struct list_head link;
  344. i915_gem_retire_fn retire;
  345. };
  346. void i915_gem_retire_noop(struct i915_gem_active *,
  347. struct drm_i915_gem_request *request);
  348. /**
  349. * init_request_active - prepares the activity tracker for use
  350. * @active - the active tracker
  351. * @func - a callback when then the tracker is retired (becomes idle),
  352. * can be NULL
  353. *
  354. * init_request_active() prepares the embedded @active struct for use as
  355. * an activity tracker, that is for tracking the last known active request
  356. * associated with it. When the last request becomes idle, when it is retired
  357. * after completion, the optional callback @func is invoked.
  358. */
  359. static inline void
  360. init_request_active(struct i915_gem_active *active,
  361. i915_gem_retire_fn retire)
  362. {
  363. INIT_LIST_HEAD(&active->link);
  364. active->retire = retire ?: i915_gem_retire_noop;
  365. }
  366. /**
  367. * i915_gem_active_set - updates the tracker to watch the current request
  368. * @active - the active tracker
  369. * @request - the request to watch
  370. *
  371. * i915_gem_active_set() watches the given @request for completion. Whilst
  372. * that @request is busy, the @active reports busy. When that @request is
  373. * retired, the @active tracker is updated to report idle.
  374. */
  375. static inline void
  376. i915_gem_active_set(struct i915_gem_active *active,
  377. struct drm_i915_gem_request *request)
  378. {
  379. list_move(&active->link, &request->active_list);
  380. rcu_assign_pointer(active->request, request);
  381. }
  382. /**
  383. * i915_gem_active_set_retire_fn - updates the retirement callback
  384. * @active - the active tracker
  385. * @fn - the routine called when the request is retired
  386. * @mutex - struct_mutex used to guard retirements
  387. *
  388. * i915_gem_active_set_retire_fn() updates the function pointer that
  389. * is called when the final request associated with the @active tracker
  390. * is retired.
  391. */
  392. static inline void
  393. i915_gem_active_set_retire_fn(struct i915_gem_active *active,
  394. i915_gem_retire_fn fn,
  395. struct mutex *mutex)
  396. {
  397. lockdep_assert_held(mutex);
  398. active->retire = fn ?: i915_gem_retire_noop;
  399. }
  400. static inline struct drm_i915_gem_request *
  401. __i915_gem_active_peek(const struct i915_gem_active *active)
  402. {
  403. /* Inside the error capture (running with the driver in an unknown
  404. * state), we want to bend the rules slightly (a lot).
  405. *
  406. * Work is in progress to make it safer, in the meantime this keeps
  407. * the known issue from spamming the logs.
  408. */
  409. return rcu_dereference_protected(active->request, 1);
  410. }
  411. /**
  412. * i915_gem_active_raw - return the active request
  413. * @active - the active tracker
  414. *
  415. * i915_gem_active_raw() returns the current request being tracked, or NULL.
  416. * It does not obtain a reference on the request for the caller, so the caller
  417. * must hold struct_mutex.
  418. */
  419. static inline struct drm_i915_gem_request *
  420. i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex)
  421. {
  422. return rcu_dereference_protected(active->request,
  423. lockdep_is_held(mutex));
  424. }
  425. /**
  426. * i915_gem_active_peek - report the active request being monitored
  427. * @active - the active tracker
  428. *
  429. * i915_gem_active_peek() returns the current request being tracked if
  430. * still active, or NULL. It does not obtain a reference on the request
  431. * for the caller, so the caller must hold struct_mutex.
  432. */
  433. static inline struct drm_i915_gem_request *
  434. i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
  435. {
  436. struct drm_i915_gem_request *request;
  437. request = i915_gem_active_raw(active, mutex);
  438. if (!request || i915_gem_request_completed(request))
  439. return NULL;
  440. return request;
  441. }
  442. /**
  443. * i915_gem_active_get - return a reference to the active request
  444. * @active - the active tracker
  445. *
  446. * i915_gem_active_get() returns a reference to the active request, or NULL
  447. * if the active tracker is idle. The caller must hold struct_mutex.
  448. */
  449. static inline struct drm_i915_gem_request *
  450. i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
  451. {
  452. return i915_gem_request_get(i915_gem_active_peek(active, mutex));
  453. }
  454. /**
  455. * __i915_gem_active_get_rcu - return a reference to the active request
  456. * @active - the active tracker
  457. *
  458. * __i915_gem_active_get() returns a reference to the active request, or NULL
  459. * if the active tracker is idle. The caller must hold the RCU read lock, but
  460. * the returned pointer is safe to use outside of RCU.
  461. */
  462. static inline struct drm_i915_gem_request *
  463. __i915_gem_active_get_rcu(const struct i915_gem_active *active)
  464. {
  465. /* Performing a lockless retrieval of the active request is super
  466. * tricky. SLAB_DESTROY_BY_RCU merely guarantees that the backing
  467. * slab of request objects will not be freed whilst we hold the
  468. * RCU read lock. It does not guarantee that the request itself
  469. * will not be freed and then *reused*. Viz,
  470. *
  471. * Thread A Thread B
  472. *
  473. * req = active.request
  474. * retire(req) -> free(req);
  475. * (req is now first on the slab freelist)
  476. * active.request = NULL
  477. *
  478. * req = new submission on a new object
  479. * ref(req)
  480. *
  481. * To prevent the request from being reused whilst the caller
  482. * uses it, we take a reference like normal. Whilst acquiring
  483. * the reference we check that it is not in a destroyed state
  484. * (refcnt == 0). That prevents the request being reallocated
  485. * whilst the caller holds on to it. To check that the request
  486. * was not reallocated as we acquired the reference we have to
  487. * check that our request remains the active request across
  488. * the lookup, in the same manner as a seqlock. The visibility
  489. * of the pointer versus the reference counting is controlled
  490. * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
  491. *
  492. * In the middle of all that, we inspect whether the request is
  493. * complete. Retiring is lazy so the request may be completed long
  494. * before the active tracker is updated. Querying whether the
  495. * request is complete is far cheaper (as it involves no locked
  496. * instructions setting cachelines to exclusive) than acquiring
  497. * the reference, so we do it first. The RCU read lock ensures the
  498. * pointer dereference is valid, but does not ensure that the
  499. * seqno nor HWS is the right one! However, if the request was
  500. * reallocated, that means the active tracker's request was complete.
  501. * If the new request is also complete, then both are and we can
  502. * just report the active tracker is idle. If the new request is
  503. * incomplete, then we acquire a reference on it and check that
  504. * it remained the active request.
  505. *
  506. * It is then imperative that we do not zero the request on
  507. * reallocation, so that we can chase the dangling pointers!
  508. * See i915_gem_request_alloc().
  509. */
  510. do {
  511. struct drm_i915_gem_request *request;
  512. request = rcu_dereference(active->request);
  513. if (!request || i915_gem_request_completed(request))
  514. return NULL;
  515. /* An especially silly compiler could decide to recompute the
  516. * result of i915_gem_request_completed, more specifically
  517. * re-emit the load for request->fence.seqno. A race would catch
  518. * a later seqno value, which could flip the result from true to
  519. * false. Which means part of the instructions below might not
  520. * be executed, while later on instructions are executed. Due to
  521. * barriers within the refcounting the inconsistency can't reach
  522. * past the call to i915_gem_request_get_rcu, but not executing
  523. * that while still executing i915_gem_request_put() creates
  524. * havoc enough. Prevent this with a compiler barrier.
  525. */
  526. barrier();
  527. request = i915_gem_request_get_rcu(request);
  528. /* What stops the following rcu_access_pointer() from occurring
  529. * before the above i915_gem_request_get_rcu()? If we were
  530. * to read the value before pausing to get the reference to
  531. * the request, we may not notice a change in the active
  532. * tracker.
  533. *
  534. * The rcu_access_pointer() is a mere compiler barrier, which
  535. * means both the CPU and compiler are free to perform the
  536. * memory read without constraint. The compiler only has to
  537. * ensure that any operations after the rcu_access_pointer()
  538. * occur afterwards in program order. This means the read may
  539. * be performed earlier by an out-of-order CPU, or adventurous
  540. * compiler.
  541. *
  542. * The atomic operation at the heart of
  543. * i915_gem_request_get_rcu(), see dma_fence_get_rcu(), is
  544. * atomic_inc_not_zero() which is only a full memory barrier
  545. * when successful. That is, if i915_gem_request_get_rcu()
  546. * returns the request (and so with the reference counted
  547. * incremented) then the following read for rcu_access_pointer()
  548. * must occur after the atomic operation and so confirm
  549. * that this request is the one currently being tracked.
  550. *
  551. * The corresponding write barrier is part of
  552. * rcu_assign_pointer().
  553. */
  554. if (!request || request == rcu_access_pointer(active->request))
  555. return rcu_pointer_handoff(request);
  556. i915_gem_request_put(request);
  557. } while (1);
  558. }
  559. /**
  560. * i915_gem_active_get_unlocked - return a reference to the active request
  561. * @active - the active tracker
  562. *
  563. * i915_gem_active_get_unlocked() returns a reference to the active request,
  564. * or NULL if the active tracker is idle. The reference is obtained under RCU,
  565. * so no locking is required by the caller.
  566. *
  567. * The reference should be freed with i915_gem_request_put().
  568. */
  569. static inline struct drm_i915_gem_request *
  570. i915_gem_active_get_unlocked(const struct i915_gem_active *active)
  571. {
  572. struct drm_i915_gem_request *request;
  573. rcu_read_lock();
  574. request = __i915_gem_active_get_rcu(active);
  575. rcu_read_unlock();
  576. return request;
  577. }
  578. /**
  579. * i915_gem_active_isset - report whether the active tracker is assigned
  580. * @active - the active tracker
  581. *
  582. * i915_gem_active_isset() returns true if the active tracker is currently
  583. * assigned to a request. Due to the lazy retiring, that request may be idle
  584. * and this may report stale information.
  585. */
  586. static inline bool
  587. i915_gem_active_isset(const struct i915_gem_active *active)
  588. {
  589. return rcu_access_pointer(active->request);
  590. }
  591. /**
  592. * i915_gem_active_wait - waits until the request is completed
  593. * @active - the active request on which to wait
  594. * @flags - how to wait
  595. * @timeout - how long to wait at most
  596. * @rps - userspace client to charge for a waitboost
  597. *
  598. * i915_gem_active_wait() waits until the request is completed before
  599. * returning, without requiring any locks to be held. Note that it does not
  600. * retire any requests before returning.
  601. *
  602. * This function relies on RCU in order to acquire the reference to the active
  603. * request without holding any locks. See __i915_gem_active_get_rcu() for the
  604. * glory details on how that is managed. Once the reference is acquired, we
  605. * can then wait upon the request, and afterwards release our reference,
  606. * free of any locking.
  607. *
  608. * This function wraps i915_wait_request(), see it for the full details on
  609. * the arguments.
  610. *
  611. * Returns 0 if successful, or a negative error code.
  612. */
  613. static inline int
  614. i915_gem_active_wait(const struct i915_gem_active *active, unsigned int flags)
  615. {
  616. struct drm_i915_gem_request *request;
  617. long ret = 0;
  618. request = i915_gem_active_get_unlocked(active);
  619. if (request) {
  620. ret = i915_wait_request(request, flags, MAX_SCHEDULE_TIMEOUT);
  621. i915_gem_request_put(request);
  622. }
  623. return ret < 0 ? ret : 0;
  624. }
  625. /**
  626. * i915_gem_active_retire - waits until the request is retired
  627. * @active - the active request on which to wait
  628. *
  629. * i915_gem_active_retire() waits until the request is completed,
  630. * and then ensures that at least the retirement handler for this
  631. * @active tracker is called before returning. If the @active
  632. * tracker is idle, the function returns immediately.
  633. */
  634. static inline int __must_check
  635. i915_gem_active_retire(struct i915_gem_active *active,
  636. struct mutex *mutex)
  637. {
  638. struct drm_i915_gem_request *request;
  639. long ret;
  640. request = i915_gem_active_raw(active, mutex);
  641. if (!request)
  642. return 0;
  643. ret = i915_wait_request(request,
  644. I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
  645. MAX_SCHEDULE_TIMEOUT);
  646. if (ret < 0)
  647. return ret;
  648. list_del_init(&active->link);
  649. RCU_INIT_POINTER(active->request, NULL);
  650. active->retire(active, request);
  651. return 0;
  652. }
  653. #define for_each_active(mask, idx) \
  654. for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
  655. #endif /* I915_GEM_REQUEST_H */