i915_gem_request.h 26 KB

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