i915_gem_request.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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/fence.h>
  27. #include "i915_gem.h"
  28. /**
  29. * Request queue structure.
  30. *
  31. * The request queue allows us to note sequence numbers that have been emitted
  32. * and may be associated with active buffers to be retired.
  33. *
  34. * By keeping this list, we can avoid having to do questionable sequence
  35. * number comparisons on buffer last_read|write_seqno. It also allows an
  36. * emission time to be associated with the request for tracking how far ahead
  37. * of the GPU the submission is.
  38. *
  39. * The requests are reference counted.
  40. */
  41. struct drm_i915_gem_request {
  42. struct fence fence;
  43. spinlock_t lock;
  44. /** On Which ring this request was generated */
  45. struct drm_i915_private *i915;
  46. /**
  47. * Context and ring buffer related to this request
  48. * Contexts are refcounted, so when this request is associated with a
  49. * context, we must increment the context's refcount, to guarantee that
  50. * it persists while any request is linked to it. Requests themselves
  51. * are also refcounted, so the request will only be freed when the last
  52. * reference to it is dismissed, and the code in
  53. * i915_gem_request_free() will then decrement the refcount on the
  54. * context.
  55. */
  56. struct i915_gem_context *ctx;
  57. struct intel_engine_cs *engine;
  58. struct intel_ring *ring;
  59. struct intel_signal_node signaling;
  60. /** GEM sequence number associated with the previous request,
  61. * when the HWS breadcrumb is equal to this the GPU is processing
  62. * this request.
  63. */
  64. u32 previous_seqno;
  65. /** Position in the ringbuffer of the start of the request */
  66. u32 head;
  67. /**
  68. * Position in the ringbuffer of the start of the postfix.
  69. * This is required to calculate the maximum available ringbuffer
  70. * space without overwriting the postfix.
  71. */
  72. u32 postfix;
  73. /** Position in the ringbuffer of the end of the whole request */
  74. u32 tail;
  75. /** Preallocate space in the ringbuffer for the emitting the request */
  76. u32 reserved_space;
  77. /**
  78. * Context related to the previous request.
  79. * As the contexts are accessed by the hardware until the switch is
  80. * completed to a new context, the hardware may still be writing
  81. * to the context object after the breadcrumb is visible. We must
  82. * not unpin/unbind/prune that object whilst still active and so
  83. * we keep the previous context pinned until the following (this)
  84. * request is retired.
  85. */
  86. struct i915_gem_context *previous_context;
  87. /** Batch buffer related to this request if any (used for
  88. * error state dump only).
  89. */
  90. struct drm_i915_gem_object *batch_obj;
  91. /** Time at which this request was emitted, in jiffies. */
  92. unsigned long emitted_jiffies;
  93. /** engine->request_list entry for this request */
  94. struct list_head link;
  95. struct drm_i915_file_private *file_priv;
  96. /** file_priv list entry for this request */
  97. struct list_head client_list;
  98. /** process identifier submitting this request */
  99. struct pid *pid;
  100. /**
  101. * The ELSP only accepts two elements at a time, so we queue
  102. * context/tail pairs on a given queue (ring->execlist_queue) until the
  103. * hardware is available. The queue serves a double purpose: we also use
  104. * it to keep track of the up to 2 contexts currently in the hardware
  105. * (usually one in execution and the other queued up by the GPU): We
  106. * only remove elements from the head of the queue when the hardware
  107. * informs us that an element has been completed.
  108. *
  109. * All accesses to the queue are mediated by a spinlock
  110. * (ring->execlist_lock).
  111. */
  112. /** Execlist link in the submission queue.*/
  113. struct list_head execlist_link;
  114. /** Execlists no. of times this request has been sent to the ELSP */
  115. int elsp_submitted;
  116. /** Execlists context hardware id. */
  117. unsigned int ctx_hw_id;
  118. };
  119. extern const struct fence_ops i915_fence_ops;
  120. static inline bool fence_is_i915(struct fence *fence)
  121. {
  122. return fence->ops == &i915_fence_ops;
  123. }
  124. struct drm_i915_gem_request * __must_check
  125. i915_gem_request_alloc(struct intel_engine_cs *engine,
  126. struct i915_gem_context *ctx);
  127. int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
  128. struct drm_file *file);
  129. void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
  130. static inline u32
  131. i915_gem_request_get_seqno(struct drm_i915_gem_request *req)
  132. {
  133. return req ? req->fence.seqno : 0;
  134. }
  135. static inline struct intel_engine_cs *
  136. i915_gem_request_get_engine(struct drm_i915_gem_request *req)
  137. {
  138. return req ? req->engine : NULL;
  139. }
  140. static inline struct drm_i915_gem_request *
  141. to_request(struct fence *fence)
  142. {
  143. /* We assume that NULL fence/request are interoperable */
  144. BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
  145. GEM_BUG_ON(fence && !fence_is_i915(fence));
  146. return container_of(fence, struct drm_i915_gem_request, fence);
  147. }
  148. static inline struct drm_i915_gem_request *
  149. i915_gem_request_get(struct drm_i915_gem_request *req)
  150. {
  151. return to_request(fence_get(&req->fence));
  152. }
  153. static inline void
  154. i915_gem_request_put(struct drm_i915_gem_request *req)
  155. {
  156. fence_put(&req->fence);
  157. }
  158. static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
  159. struct drm_i915_gem_request *src)
  160. {
  161. if (src)
  162. i915_gem_request_get(src);
  163. if (*pdst)
  164. i915_gem_request_put(*pdst);
  165. *pdst = src;
  166. }
  167. void __i915_add_request(struct drm_i915_gem_request *req,
  168. struct drm_i915_gem_object *batch_obj,
  169. bool flush_caches);
  170. #define i915_add_request(req) \
  171. __i915_add_request(req, NULL, true)
  172. #define i915_add_request_no_flush(req) \
  173. __i915_add_request(req, NULL, false)
  174. struct intel_rps_client;
  175. #define NO_WAITBOOST ERR_PTR(-1)
  176. #define IS_RPS_CLIENT(p) (!IS_ERR(p))
  177. #define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
  178. int __i915_wait_request(struct drm_i915_gem_request *req,
  179. bool interruptible,
  180. s64 *timeout,
  181. struct intel_rps_client *rps);
  182. int __must_check i915_wait_request(struct drm_i915_gem_request *req);
  183. static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
  184. /**
  185. * Returns true if seq1 is later than seq2.
  186. */
  187. static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
  188. {
  189. return (s32)(seq1 - seq2) >= 0;
  190. }
  191. static inline bool
  192. i915_gem_request_started(const struct drm_i915_gem_request *req)
  193. {
  194. return i915_seqno_passed(intel_engine_get_seqno(req->engine),
  195. req->previous_seqno);
  196. }
  197. static inline bool
  198. i915_gem_request_completed(const struct drm_i915_gem_request *req)
  199. {
  200. return i915_seqno_passed(intel_engine_get_seqno(req->engine),
  201. req->fence.seqno);
  202. }
  203. bool __i915_spin_request(const struct drm_i915_gem_request *request,
  204. int state, unsigned long timeout_us);
  205. static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
  206. int state, unsigned long timeout_us)
  207. {
  208. return (i915_gem_request_started(request) &&
  209. __i915_spin_request(request, state, timeout_us));
  210. }
  211. /* We treat requests as fences. This is not be to confused with our
  212. * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
  213. * We use the fences to synchronize access from the CPU with activity on the
  214. * GPU, for example, we should not rewrite an object's PTE whilst the GPU
  215. * is reading them. We also track fences at a higher level to provide
  216. * implicit synchronisation around GEM objects, e.g. set-domain will wait
  217. * for outstanding GPU rendering before marking the object ready for CPU
  218. * access, or a pageflip will wait until the GPU is complete before showing
  219. * the frame on the scanout.
  220. *
  221. * In order to use a fence, the object must track the fence it needs to
  222. * serialise with. For example, GEM objects want to track both read and
  223. * write access so that we can perform concurrent read operations between
  224. * the CPU and GPU engines, as well as waiting for all rendering to
  225. * complete, or waiting for the last GPU user of a "fence register". The
  226. * object then embeds a #i915_gem_active to track the most recent (in
  227. * retirement order) request relevant for the desired mode of access.
  228. * The #i915_gem_active is updated with i915_gem_active_set() to track the
  229. * most recent fence request, typically this is done as part of
  230. * i915_vma_move_to_active().
  231. *
  232. * When the #i915_gem_active completes (is retired), it will
  233. * signal its completion to the owner through a callback as well as mark
  234. * itself as idle (i915_gem_active.request == NULL). The owner
  235. * can then perform any action, such as delayed freeing of an active
  236. * resource including itself.
  237. */
  238. struct i915_gem_active {
  239. struct drm_i915_gem_request *request;
  240. };
  241. /**
  242. * i915_gem_active_set - updates the tracker to watch the current request
  243. * @active - the active tracker
  244. * @request - the request to watch
  245. *
  246. * i915_gem_active_set() watches the given @request for completion. Whilst
  247. * that @request is busy, the @active reports busy. When that @request is
  248. * retired, the @active tracker is updated to report idle.
  249. */
  250. static inline void
  251. i915_gem_active_set(struct i915_gem_active *active,
  252. struct drm_i915_gem_request *request)
  253. {
  254. i915_gem_request_assign(&active->request, request);
  255. }
  256. static inline struct drm_i915_gem_request *
  257. __i915_gem_active_peek(const struct i915_gem_active *active)
  258. {
  259. return active->request;
  260. }
  261. /**
  262. * i915_gem_active_peek - report the request being monitored
  263. * @active - the active tracker
  264. *
  265. * i915_gem_active_peek() returns the current request being tracked, or NULL.
  266. * It does not obtain a reference on the request for the caller, so the
  267. * caller must hold struct_mutex.
  268. */
  269. static inline struct drm_i915_gem_request *
  270. i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
  271. {
  272. return active->request;
  273. }
  274. /**
  275. * i915_gem_active_get - return a reference to the active request
  276. * @active - the active tracker
  277. *
  278. * i915_gem_active_get() returns a reference to the active request, or NULL
  279. * if the active tracker is idle. The caller must hold struct_mutex.
  280. */
  281. static inline struct drm_i915_gem_request *
  282. i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
  283. {
  284. struct drm_i915_gem_request *request;
  285. request = i915_gem_active_peek(active, mutex);
  286. if (!request || i915_gem_request_completed(request))
  287. return NULL;
  288. return i915_gem_request_get(request);
  289. }
  290. /**
  291. * i915_gem_active_isset - report whether the active tracker is assigned
  292. * @active - the active tracker
  293. *
  294. * i915_gem_active_isset() returns true if the active tracker is currently
  295. * assigned to a request. Due to the lazy retiring, that request may be idle
  296. * and this may report stale information.
  297. */
  298. static inline bool
  299. i915_gem_active_isset(const struct i915_gem_active *active)
  300. {
  301. return active->request;
  302. }
  303. /**
  304. * i915_gem_active_is_idle - report whether the active tracker is idle
  305. * @active - the active tracker
  306. *
  307. * i915_gem_active_is_idle() returns true if the active tracker is currently
  308. * unassigned or if the request is complete (but not yet retired). Requires
  309. * the caller to hold struct_mutex (but that can be relaxed if desired).
  310. */
  311. static inline bool
  312. i915_gem_active_is_idle(const struct i915_gem_active *active,
  313. struct mutex *mutex)
  314. {
  315. struct drm_i915_gem_request *request;
  316. request = i915_gem_active_peek(active, mutex);
  317. if (!request || i915_gem_request_completed(request))
  318. return true;
  319. return false;
  320. }
  321. /**
  322. * i915_gem_active_wait - waits until the request is completed
  323. * @active - the active request on which to wait
  324. *
  325. * i915_gem_active_wait() waits until the request is completed before
  326. * returning. Note that it does not guarantee that the request is
  327. * retired first, see i915_gem_active_retire().
  328. */
  329. static inline int __must_check
  330. i915_gem_active_wait(const struct i915_gem_active *active, struct mutex *mutex)
  331. {
  332. struct drm_i915_gem_request *request;
  333. request = i915_gem_active_peek(active, mutex);
  334. if (!request)
  335. return 0;
  336. return i915_wait_request(request);
  337. }
  338. /**
  339. * i915_gem_active_retire - waits until the request is retired
  340. * @active - the active request on which to wait
  341. *
  342. * i915_gem_active_retire() waits until the request is completed,
  343. * and then ensures that at least the retirement handler for this
  344. * @active tracker is called before returning. If the @active
  345. * tracker is idle, the function returns immediately.
  346. */
  347. static inline int __must_check
  348. i915_gem_active_retire(const struct i915_gem_active *active,
  349. struct mutex *mutex)
  350. {
  351. return i915_gem_active_wait(active, mutex);
  352. }
  353. /* Convenience functions for peeking at state inside active's request whilst
  354. * guarded by the struct_mutex.
  355. */
  356. static inline uint32_t
  357. i915_gem_active_get_seqno(const struct i915_gem_active *active,
  358. struct mutex *mutex)
  359. {
  360. return i915_gem_request_get_seqno(i915_gem_active_peek(active, mutex));
  361. }
  362. static inline struct intel_engine_cs *
  363. i915_gem_active_get_engine(const struct i915_gem_active *active,
  364. struct mutex *mutex)
  365. {
  366. return i915_gem_request_get_engine(i915_gem_active_peek(active, mutex));
  367. }
  368. #define for_each_active(mask, idx) \
  369. for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
  370. #endif /* I915_GEM_REQUEST_H */