Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 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 | |
| 25 | #ifndef I915_GEM_REQUEST_H |
| 26 | #define I915_GEM_REQUEST_H |
| 27 | |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 28 | #include <linux/fence.h> |
| 29 | |
| 30 | #include "i915_gem.h" |
| 31 | |
Chris Wilson | dcff85c | 2016-08-05 10:14:11 +0100 | [diff] [blame] | 32 | struct intel_wait { |
| 33 | struct rb_node node; |
| 34 | struct task_struct *tsk; |
| 35 | u32 seqno; |
| 36 | }; |
| 37 | |
| 38 | struct intel_signal_node { |
| 39 | struct rb_node node; |
| 40 | struct intel_wait wait; |
| 41 | }; |
| 42 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 43 | /** |
| 44 | * Request queue structure. |
| 45 | * |
| 46 | * The request queue allows us to note sequence numbers that have been emitted |
| 47 | * and may be associated with active buffers to be retired. |
| 48 | * |
| 49 | * By keeping this list, we can avoid having to do questionable sequence |
| 50 | * number comparisons on buffer last_read|write_seqno. It also allows an |
| 51 | * emission time to be associated with the request for tracking how far ahead |
| 52 | * of the GPU the submission is. |
| 53 | * |
Chris Wilson | 5a198b8 | 2016-08-09 09:23:34 +0100 | [diff] [blame] | 54 | * When modifying this structure be very aware that we perform a lockless |
| 55 | * RCU lookup of it that may race against reallocation of the struct |
| 56 | * from the slab freelist. We intentionally do not zero the structure on |
| 57 | * allocation so that the lookup can use the dangling pointers (and is |
| 58 | * cogniscent that those pointers may be wrong). Instead, everything that |
| 59 | * needs to be initialised must be done so explicitly. |
| 60 | * |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 61 | * The requests are reference counted. |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 62 | */ |
| 63 | struct drm_i915_gem_request { |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 64 | struct fence fence; |
| 65 | spinlock_t lock; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 66 | |
| 67 | /** On Which ring this request was generated */ |
| 68 | struct drm_i915_private *i915; |
| 69 | |
| 70 | /** |
| 71 | * Context and ring buffer related to this request |
| 72 | * Contexts are refcounted, so when this request is associated with a |
| 73 | * context, we must increment the context's refcount, to guarantee that |
| 74 | * it persists while any request is linked to it. Requests themselves |
| 75 | * are also refcounted, so the request will only be freed when the last |
| 76 | * reference to it is dismissed, and the code in |
| 77 | * i915_gem_request_free() will then decrement the refcount on the |
| 78 | * context. |
| 79 | */ |
| 80 | struct i915_gem_context *ctx; |
| 81 | struct intel_engine_cs *engine; |
Chris Wilson | 7e37f88 | 2016-08-02 22:50:21 +0100 | [diff] [blame] | 82 | struct intel_ring *ring; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 83 | struct intel_signal_node signaling; |
| 84 | |
| 85 | /** GEM sequence number associated with the previous request, |
| 86 | * when the HWS breadcrumb is equal to this the GPU is processing |
| 87 | * this request. |
| 88 | */ |
| 89 | u32 previous_seqno; |
| 90 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 91 | /** Position in the ringbuffer of the start of the request */ |
| 92 | u32 head; |
| 93 | |
| 94 | /** |
| 95 | * Position in the ringbuffer of the start of the postfix. |
| 96 | * This is required to calculate the maximum available ringbuffer |
| 97 | * space without overwriting the postfix. |
| 98 | */ |
| 99 | u32 postfix; |
| 100 | |
| 101 | /** Position in the ringbuffer of the end of the whole request */ |
| 102 | u32 tail; |
| 103 | |
| 104 | /** Preallocate space in the ringbuffer for the emitting the request */ |
| 105 | u32 reserved_space; |
| 106 | |
| 107 | /** |
| 108 | * Context related to the previous request. |
| 109 | * As the contexts are accessed by the hardware until the switch is |
| 110 | * completed to a new context, the hardware may still be writing |
| 111 | * to the context object after the breadcrumb is visible. We must |
| 112 | * not unpin/unbind/prune that object whilst still active and so |
| 113 | * we keep the previous context pinned until the following (this) |
| 114 | * request is retired. |
| 115 | */ |
| 116 | struct i915_gem_context *previous_context; |
| 117 | |
| 118 | /** Batch buffer related to this request if any (used for |
| 119 | * error state dump only). |
| 120 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 121 | struct i915_vma *batch; |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 122 | struct list_head active_list; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 123 | |
| 124 | /** Time at which this request was emitted, in jiffies. */ |
| 125 | unsigned long emitted_jiffies; |
| 126 | |
Chris Wilson | efdf7c0 | 2016-08-04 07:52:33 +0100 | [diff] [blame] | 127 | /** engine->request_list entry for this request */ |
| 128 | struct list_head link; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 129 | |
Chris Wilson | 675d9ad | 2016-08-04 07:52:36 +0100 | [diff] [blame] | 130 | /** ring->request_list entry for this request */ |
| 131 | struct list_head ring_link; |
| 132 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 133 | struct drm_i915_file_private *file_priv; |
| 134 | /** file_priv list entry for this request */ |
| 135 | struct list_head client_list; |
| 136 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 137 | /** |
| 138 | * The ELSP only accepts two elements at a time, so we queue |
| 139 | * context/tail pairs on a given queue (ring->execlist_queue) until the |
| 140 | * hardware is available. The queue serves a double purpose: we also use |
| 141 | * it to keep track of the up to 2 contexts currently in the hardware |
| 142 | * (usually one in execution and the other queued up by the GPU): We |
| 143 | * only remove elements from the head of the queue when the hardware |
| 144 | * informs us that an element has been completed. |
| 145 | * |
| 146 | * All accesses to the queue are mediated by a spinlock |
| 147 | * (ring->execlist_lock). |
| 148 | */ |
| 149 | |
| 150 | /** Execlist link in the submission queue.*/ |
| 151 | struct list_head execlist_link; |
| 152 | |
| 153 | /** Execlists no. of times this request has been sent to the ELSP */ |
| 154 | int elsp_submitted; |
| 155 | |
| 156 | /** Execlists context hardware id. */ |
| 157 | unsigned int ctx_hw_id; |
| 158 | }; |
| 159 | |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 160 | extern const struct fence_ops i915_fence_ops; |
| 161 | |
| 162 | static inline bool fence_is_i915(struct fence *fence) |
| 163 | { |
| 164 | return fence->ops == &i915_fence_ops; |
| 165 | } |
| 166 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 167 | struct drm_i915_gem_request * __must_check |
| 168 | i915_gem_request_alloc(struct intel_engine_cs *engine, |
| 169 | struct i915_gem_context *ctx); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 170 | int i915_gem_request_add_to_client(struct drm_i915_gem_request *req, |
| 171 | struct drm_file *file); |
| 172 | void i915_gem_request_retire_upto(struct drm_i915_gem_request *req); |
| 173 | |
| 174 | static inline u32 |
| 175 | i915_gem_request_get_seqno(struct drm_i915_gem_request *req) |
| 176 | { |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 177 | return req ? req->fence.seqno : 0; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static inline struct intel_engine_cs * |
| 181 | i915_gem_request_get_engine(struct drm_i915_gem_request *req) |
| 182 | { |
| 183 | return req ? req->engine : NULL; |
| 184 | } |
| 185 | |
| 186 | static inline struct drm_i915_gem_request * |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 187 | to_request(struct fence *fence) |
| 188 | { |
| 189 | /* We assume that NULL fence/request are interoperable */ |
| 190 | BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0); |
| 191 | GEM_BUG_ON(fence && !fence_is_i915(fence)); |
| 192 | return container_of(fence, struct drm_i915_gem_request, fence); |
| 193 | } |
| 194 | |
| 195 | static inline struct drm_i915_gem_request * |
Chris Wilson | e8a261e | 2016-07-20 13:31:49 +0100 | [diff] [blame] | 196 | i915_gem_request_get(struct drm_i915_gem_request *req) |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 197 | { |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 198 | return to_request(fence_get(&req->fence)); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 201 | static inline struct drm_i915_gem_request * |
| 202 | i915_gem_request_get_rcu(struct drm_i915_gem_request *req) |
| 203 | { |
| 204 | return to_request(fence_get_rcu(&req->fence)); |
| 205 | } |
| 206 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 207 | static inline void |
Chris Wilson | e8a261e | 2016-07-20 13:31:49 +0100 | [diff] [blame] | 208 | i915_gem_request_put(struct drm_i915_gem_request *req) |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 209 | { |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 210 | fence_put(&req->fence); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst, |
| 214 | struct drm_i915_gem_request *src) |
| 215 | { |
| 216 | if (src) |
Chris Wilson | e8a261e | 2016-07-20 13:31:49 +0100 | [diff] [blame] | 217 | i915_gem_request_get(src); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 218 | |
| 219 | if (*pdst) |
Chris Wilson | e8a261e | 2016-07-20 13:31:49 +0100 | [diff] [blame] | 220 | i915_gem_request_put(*pdst); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 221 | |
| 222 | *pdst = src; |
| 223 | } |
| 224 | |
Chris Wilson | 17f298cf | 2016-08-10 13:41:46 +0100 | [diff] [blame] | 225 | void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 226 | #define i915_add_request(req) \ |
Chris Wilson | 17f298cf | 2016-08-10 13:41:46 +0100 | [diff] [blame] | 227 | __i915_add_request(req, true) |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 228 | #define i915_add_request_no_flush(req) \ |
Chris Wilson | 17f298cf | 2016-08-10 13:41:46 +0100 | [diff] [blame] | 229 | __i915_add_request(req, false) |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 230 | |
| 231 | struct intel_rps_client; |
Chris Wilson | 42df271 | 2016-07-20 09:21:12 +0100 | [diff] [blame] | 232 | #define NO_WAITBOOST ERR_PTR(-1) |
| 233 | #define IS_RPS_CLIENT(p) (!IS_ERR(p)) |
| 234 | #define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p)) |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 235 | |
Chris Wilson | 776f323 | 2016-08-04 07:52:40 +0100 | [diff] [blame] | 236 | int i915_wait_request(struct drm_i915_gem_request *req, |
| 237 | bool interruptible, |
| 238 | s64 *timeout, |
| 239 | struct intel_rps_client *rps) |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 240 | __attribute__((nonnull(1))); |
| 241 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 242 | static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine); |
| 243 | |
| 244 | /** |
| 245 | * Returns true if seq1 is later than seq2. |
| 246 | */ |
| 247 | static inline bool i915_seqno_passed(u32 seq1, u32 seq2) |
| 248 | { |
| 249 | return (s32)(seq1 - seq2) >= 0; |
| 250 | } |
| 251 | |
| 252 | static inline bool |
| 253 | i915_gem_request_started(const struct drm_i915_gem_request *req) |
| 254 | { |
| 255 | return i915_seqno_passed(intel_engine_get_seqno(req->engine), |
| 256 | req->previous_seqno); |
| 257 | } |
| 258 | |
| 259 | static inline bool |
| 260 | i915_gem_request_completed(const struct drm_i915_gem_request *req) |
| 261 | { |
| 262 | return i915_seqno_passed(intel_engine_get_seqno(req->engine), |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 263 | req->fence.seqno); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | bool __i915_spin_request(const struct drm_i915_gem_request *request, |
| 267 | int state, unsigned long timeout_us); |
| 268 | static inline bool i915_spin_request(const struct drm_i915_gem_request *request, |
| 269 | int state, unsigned long timeout_us) |
| 270 | { |
| 271 | return (i915_gem_request_started(request) && |
| 272 | __i915_spin_request(request, state, timeout_us)); |
| 273 | } |
| 274 | |
Chris Wilson | 381f371 | 2016-08-04 07:52:29 +0100 | [diff] [blame] | 275 | /* We treat requests as fences. This is not be to confused with our |
| 276 | * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync. |
| 277 | * We use the fences to synchronize access from the CPU with activity on the |
| 278 | * GPU, for example, we should not rewrite an object's PTE whilst the GPU |
| 279 | * is reading them. We also track fences at a higher level to provide |
| 280 | * implicit synchronisation around GEM objects, e.g. set-domain will wait |
| 281 | * for outstanding GPU rendering before marking the object ready for CPU |
| 282 | * access, or a pageflip will wait until the GPU is complete before showing |
| 283 | * the frame on the scanout. |
| 284 | * |
| 285 | * In order to use a fence, the object must track the fence it needs to |
| 286 | * serialise with. For example, GEM objects want to track both read and |
| 287 | * write access so that we can perform concurrent read operations between |
| 288 | * the CPU and GPU engines, as well as waiting for all rendering to |
| 289 | * complete, or waiting for the last GPU user of a "fence register". The |
| 290 | * object then embeds a #i915_gem_active to track the most recent (in |
| 291 | * retirement order) request relevant for the desired mode of access. |
| 292 | * The #i915_gem_active is updated with i915_gem_active_set() to track the |
| 293 | * most recent fence request, typically this is done as part of |
| 294 | * i915_vma_move_to_active(). |
| 295 | * |
| 296 | * When the #i915_gem_active completes (is retired), it will |
| 297 | * signal its completion to the owner through a callback as well as mark |
| 298 | * itself as idle (i915_gem_active.request == NULL). The owner |
| 299 | * can then perform any action, such as delayed freeing of an active |
| 300 | * resource including itself. |
| 301 | */ |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 302 | struct i915_gem_active; |
| 303 | |
| 304 | typedef void (*i915_gem_retire_fn)(struct i915_gem_active *, |
| 305 | struct drm_i915_gem_request *); |
| 306 | |
Chris Wilson | 381f371 | 2016-08-04 07:52:29 +0100 | [diff] [blame] | 307 | struct i915_gem_active { |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 308 | struct drm_i915_gem_request __rcu *request; |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 309 | struct list_head link; |
| 310 | i915_gem_retire_fn retire; |
Chris Wilson | 381f371 | 2016-08-04 07:52:29 +0100 | [diff] [blame] | 311 | }; |
| 312 | |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 313 | void i915_gem_retire_noop(struct i915_gem_active *, |
| 314 | struct drm_i915_gem_request *request); |
| 315 | |
| 316 | /** |
| 317 | * init_request_active - prepares the activity tracker for use |
| 318 | * @active - the active tracker |
| 319 | * @func - a callback when then the tracker is retired (becomes idle), |
| 320 | * can be NULL |
| 321 | * |
| 322 | * init_request_active() prepares the embedded @active struct for use as |
| 323 | * an activity tracker, that is for tracking the last known active request |
| 324 | * associated with it. When the last request becomes idle, when it is retired |
| 325 | * after completion, the optional callback @func is invoked. |
| 326 | */ |
| 327 | static inline void |
| 328 | init_request_active(struct i915_gem_active *active, |
| 329 | i915_gem_retire_fn retire) |
| 330 | { |
| 331 | INIT_LIST_HEAD(&active->link); |
| 332 | active->retire = retire ?: i915_gem_retire_noop; |
| 333 | } |
| 334 | |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 335 | /** |
| 336 | * i915_gem_active_set - updates the tracker to watch the current request |
| 337 | * @active - the active tracker |
| 338 | * @request - the request to watch |
| 339 | * |
| 340 | * i915_gem_active_set() watches the given @request for completion. Whilst |
| 341 | * that @request is busy, the @active reports busy. When that @request is |
| 342 | * retired, the @active tracker is updated to report idle. |
| 343 | */ |
Chris Wilson | 381f371 | 2016-08-04 07:52:29 +0100 | [diff] [blame] | 344 | static inline void |
| 345 | i915_gem_active_set(struct i915_gem_active *active, |
| 346 | struct drm_i915_gem_request *request) |
| 347 | { |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 348 | list_move(&active->link, &request->active_list); |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 349 | rcu_assign_pointer(active->request, request); |
Chris Wilson | 381f371 | 2016-08-04 07:52:29 +0100 | [diff] [blame] | 350 | } |
| 351 | |
Chris Wilson | d72d908 | 2016-08-04 07:52:31 +0100 | [diff] [blame] | 352 | static inline struct drm_i915_gem_request * |
| 353 | __i915_gem_active_peek(const struct i915_gem_active *active) |
| 354 | { |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 355 | /* Inside the error capture (running with the driver in an unknown |
| 356 | * state), we want to bend the rules slightly (a lot). |
| 357 | * |
| 358 | * Work is in progress to make it safer, in the meantime this keeps |
| 359 | * the known issue from spamming the logs. |
| 360 | */ |
| 361 | return rcu_dereference_protected(active->request, 1); |
Chris Wilson | d72d908 | 2016-08-04 07:52:31 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 364 | /** |
Chris Wilson | 385384a | 2016-08-09 08:37:01 +0100 | [diff] [blame] | 365 | * i915_gem_active_raw - return the active request |
| 366 | * @active - the active tracker |
| 367 | * |
| 368 | * i915_gem_active_raw() returns the current request being tracked, or NULL. |
| 369 | * It does not obtain a reference on the request for the caller, so the caller |
| 370 | * must hold struct_mutex. |
| 371 | */ |
| 372 | static inline struct drm_i915_gem_request * |
| 373 | i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex) |
| 374 | { |
| 375 | return rcu_dereference_protected(active->request, |
| 376 | lockdep_is_held(mutex)); |
| 377 | } |
| 378 | |
| 379 | /** |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 380 | * i915_gem_active_peek - report the active request being monitored |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 381 | * @active - the active tracker |
| 382 | * |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 383 | * i915_gem_active_peek() returns the current request being tracked if |
| 384 | * still active, or NULL. It does not obtain a reference on the request |
| 385 | * for the caller, so the caller must hold struct_mutex. |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 386 | */ |
| 387 | static inline struct drm_i915_gem_request * |
Chris Wilson | d72d908 | 2016-08-04 07:52:31 +0100 | [diff] [blame] | 388 | i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex) |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 389 | { |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 390 | struct drm_i915_gem_request *request; |
| 391 | |
Chris Wilson | 385384a | 2016-08-09 08:37:01 +0100 | [diff] [blame] | 392 | request = i915_gem_active_raw(active, mutex); |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 393 | if (!request || i915_gem_request_completed(request)) |
| 394 | return NULL; |
| 395 | |
| 396 | return request; |
| 397 | } |
| 398 | |
| 399 | /** |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 400 | * i915_gem_active_get - return a reference to the active request |
| 401 | * @active - the active tracker |
| 402 | * |
| 403 | * i915_gem_active_get() returns a reference to the active request, or NULL |
| 404 | * if the active tracker is idle. The caller must hold struct_mutex. |
| 405 | */ |
| 406 | static inline struct drm_i915_gem_request * |
Chris Wilson | d72d908 | 2016-08-04 07:52:31 +0100 | [diff] [blame] | 407 | i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex) |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 408 | { |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 409 | return i915_gem_request_get(i915_gem_active_peek(active, mutex)); |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /** |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 413 | * __i915_gem_active_get_rcu - return a reference to the active request |
| 414 | * @active - the active tracker |
| 415 | * |
| 416 | * __i915_gem_active_get() returns a reference to the active request, or NULL |
| 417 | * if the active tracker is idle. The caller must hold the RCU read lock, but |
| 418 | * the returned pointer is safe to use outside of RCU. |
| 419 | */ |
| 420 | static inline struct drm_i915_gem_request * |
| 421 | __i915_gem_active_get_rcu(const struct i915_gem_active *active) |
| 422 | { |
| 423 | /* Performing a lockless retrieval of the active request is super |
| 424 | * tricky. SLAB_DESTROY_BY_RCU merely guarantees that the backing |
| 425 | * slab of request objects will not be freed whilst we hold the |
| 426 | * RCU read lock. It does not guarantee that the request itself |
| 427 | * will not be freed and then *reused*. Viz, |
| 428 | * |
| 429 | * Thread A Thread B |
| 430 | * |
| 431 | * req = active.request |
| 432 | * retire(req) -> free(req); |
| 433 | * (req is now first on the slab freelist) |
| 434 | * active.request = NULL |
| 435 | * |
| 436 | * req = new submission on a new object |
| 437 | * ref(req) |
| 438 | * |
| 439 | * To prevent the request from being reused whilst the caller |
| 440 | * uses it, we take a reference like normal. Whilst acquiring |
| 441 | * the reference we check that it is not in a destroyed state |
| 442 | * (refcnt == 0). That prevents the request being reallocated |
| 443 | * whilst the caller holds on to it. To check that the request |
| 444 | * was not reallocated as we acquired the reference we have to |
| 445 | * check that our request remains the active request across |
| 446 | * the lookup, in the same manner as a seqlock. The visibility |
| 447 | * of the pointer versus the reference counting is controlled |
| 448 | * by using RCU barriers (rcu_dereference and rcu_assign_pointer). |
| 449 | * |
| 450 | * In the middle of all that, we inspect whether the request is |
| 451 | * complete. Retiring is lazy so the request may be completed long |
| 452 | * before the active tracker is updated. Querying whether the |
| 453 | * request is complete is far cheaper (as it involves no locked |
| 454 | * instructions setting cachelines to exclusive) than acquiring |
| 455 | * the reference, so we do it first. The RCU read lock ensures the |
| 456 | * pointer dereference is valid, but does not ensure that the |
| 457 | * seqno nor HWS is the right one! However, if the request was |
| 458 | * reallocated, that means the active tracker's request was complete. |
| 459 | * If the new request is also complete, then both are and we can |
| 460 | * just report the active tracker is idle. If the new request is |
| 461 | * incomplete, then we acquire a reference on it and check that |
| 462 | * it remained the active request. |
Chris Wilson | 5a198b8 | 2016-08-09 09:23:34 +0100 | [diff] [blame] | 463 | * |
| 464 | * It is then imperative that we do not zero the request on |
| 465 | * reallocation, so that we can chase the dangling pointers! |
| 466 | * See i915_gem_request_alloc(). |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 467 | */ |
| 468 | do { |
| 469 | struct drm_i915_gem_request *request; |
| 470 | |
| 471 | request = rcu_dereference(active->request); |
| 472 | if (!request || i915_gem_request_completed(request)) |
| 473 | return NULL; |
| 474 | |
| 475 | request = i915_gem_request_get_rcu(request); |
| 476 | |
| 477 | /* What stops the following rcu_access_pointer() from occurring |
| 478 | * before the above i915_gem_request_get_rcu()? If we were |
| 479 | * to read the value before pausing to get the reference to |
| 480 | * the request, we may not notice a change in the active |
| 481 | * tracker. |
| 482 | * |
| 483 | * The rcu_access_pointer() is a mere compiler barrier, which |
| 484 | * means both the CPU and compiler are free to perform the |
| 485 | * memory read without constraint. The compiler only has to |
| 486 | * ensure that any operations after the rcu_access_pointer() |
| 487 | * occur afterwards in program order. This means the read may |
| 488 | * be performed earlier by an out-of-order CPU, or adventurous |
| 489 | * compiler. |
| 490 | * |
| 491 | * The atomic operation at the heart of |
| 492 | * i915_gem_request_get_rcu(), see fence_get_rcu(), is |
| 493 | * atomic_inc_not_zero() which is only a full memory barrier |
| 494 | * when successful. That is, if i915_gem_request_get_rcu() |
| 495 | * returns the request (and so with the reference counted |
| 496 | * incremented) then the following read for rcu_access_pointer() |
| 497 | * must occur after the atomic operation and so confirm |
| 498 | * that this request is the one currently being tracked. |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 499 | * |
| 500 | * The corresponding write barrier is part of |
| 501 | * rcu_assign_pointer(). |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 502 | */ |
| 503 | if (!request || request == rcu_access_pointer(active->request)) |
| 504 | return rcu_pointer_handoff(request); |
| 505 | |
| 506 | i915_gem_request_put(request); |
| 507 | } while (1); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * i915_gem_active_get_unlocked - return a reference to the active request |
| 512 | * @active - the active tracker |
| 513 | * |
| 514 | * i915_gem_active_get_unlocked() returns a reference to the active request, |
| 515 | * or NULL if the active tracker is idle. The reference is obtained under RCU, |
| 516 | * so no locking is required by the caller. |
| 517 | * |
| 518 | * The reference should be freed with i915_gem_request_put(). |
| 519 | */ |
| 520 | static inline struct drm_i915_gem_request * |
| 521 | i915_gem_active_get_unlocked(const struct i915_gem_active *active) |
| 522 | { |
| 523 | struct drm_i915_gem_request *request; |
| 524 | |
| 525 | rcu_read_lock(); |
| 526 | request = __i915_gem_active_get_rcu(active); |
| 527 | rcu_read_unlock(); |
| 528 | |
| 529 | return request; |
| 530 | } |
| 531 | |
| 532 | /** |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 533 | * i915_gem_active_isset - report whether the active tracker is assigned |
| 534 | * @active - the active tracker |
| 535 | * |
| 536 | * i915_gem_active_isset() returns true if the active tracker is currently |
| 537 | * assigned to a request. Due to the lazy retiring, that request may be idle |
| 538 | * and this may report stale information. |
| 539 | */ |
| 540 | static inline bool |
| 541 | i915_gem_active_isset(const struct i915_gem_active *active) |
| 542 | { |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 543 | return rcu_access_pointer(active->request); |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /** |
| 547 | * i915_gem_active_is_idle - report whether the active tracker is idle |
| 548 | * @active - the active tracker |
| 549 | * |
| 550 | * i915_gem_active_is_idle() returns true if the active tracker is currently |
| 551 | * unassigned or if the request is complete (but not yet retired). Requires |
| 552 | * the caller to hold struct_mutex (but that can be relaxed if desired). |
| 553 | */ |
| 554 | static inline bool |
Chris Wilson | d72d908 | 2016-08-04 07:52:31 +0100 | [diff] [blame] | 555 | i915_gem_active_is_idle(const struct i915_gem_active *active, |
| 556 | struct mutex *mutex) |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 557 | { |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 558 | return !i915_gem_active_peek(active, mutex); |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /** |
| 562 | * i915_gem_active_wait - waits until the request is completed |
| 563 | * @active - the active request on which to wait |
| 564 | * |
| 565 | * i915_gem_active_wait() waits until the request is completed before |
| 566 | * returning. Note that it does not guarantee that the request is |
| 567 | * retired first, see i915_gem_active_retire(). |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 568 | * |
| 569 | * i915_gem_active_wait() returns immediately if the active |
| 570 | * request is already complete. |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 571 | */ |
| 572 | static inline int __must_check |
Chris Wilson | d72d908 | 2016-08-04 07:52:31 +0100 | [diff] [blame] | 573 | i915_gem_active_wait(const struct i915_gem_active *active, struct mutex *mutex) |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 574 | { |
| 575 | struct drm_i915_gem_request *request; |
| 576 | |
Chris Wilson | d72d908 | 2016-08-04 07:52:31 +0100 | [diff] [blame] | 577 | request = i915_gem_active_peek(active, mutex); |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 578 | if (!request) |
| 579 | return 0; |
| 580 | |
Chris Wilson | 776f323 | 2016-08-04 07:52:40 +0100 | [diff] [blame] | 581 | return i915_wait_request(request, true, NULL, NULL); |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /** |
Chris Wilson | 2467658 | 2016-08-05 10:14:06 +0100 | [diff] [blame] | 585 | * i915_gem_active_wait_unlocked - waits until the request is completed |
| 586 | * @active - the active request on which to wait |
| 587 | * @interruptible - whether the wait can be woken by a userspace signal |
| 588 | * @timeout - how long to wait at most |
| 589 | * @rps - userspace client to charge for a waitboost |
| 590 | * |
| 591 | * i915_gem_active_wait_unlocked() waits until the request is completed before |
| 592 | * returning, without requiring any locks to be held. Note that it does not |
| 593 | * retire any requests before returning. |
| 594 | * |
| 595 | * This function relies on RCU in order to acquire the reference to the active |
| 596 | * request without holding any locks. See __i915_gem_active_get_rcu() for the |
| 597 | * glory details on how that is managed. Once the reference is acquired, we |
| 598 | * can then wait upon the request, and afterwards release our reference, |
| 599 | * free of any locking. |
| 600 | * |
| 601 | * This function wraps i915_wait_request(), see it for the full details on |
| 602 | * the arguments. |
| 603 | * |
| 604 | * Returns 0 if successful, or a negative error code. |
| 605 | */ |
| 606 | static inline int |
| 607 | i915_gem_active_wait_unlocked(const struct i915_gem_active *active, |
| 608 | bool interruptible, |
| 609 | s64 *timeout, |
| 610 | struct intel_rps_client *rps) |
| 611 | { |
| 612 | struct drm_i915_gem_request *request; |
| 613 | int ret = 0; |
| 614 | |
| 615 | request = i915_gem_active_get_unlocked(active); |
| 616 | if (request) { |
| 617 | ret = i915_wait_request(request, interruptible, timeout, rps); |
| 618 | i915_gem_request_put(request); |
| 619 | } |
| 620 | |
| 621 | return ret; |
| 622 | } |
| 623 | |
| 624 | /** |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 625 | * i915_gem_active_retire - waits until the request is retired |
| 626 | * @active - the active request on which to wait |
| 627 | * |
| 628 | * i915_gem_active_retire() waits until the request is completed, |
| 629 | * and then ensures that at least the retirement handler for this |
| 630 | * @active tracker is called before returning. If the @active |
| 631 | * tracker is idle, the function returns immediately. |
| 632 | */ |
| 633 | static inline int __must_check |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 634 | i915_gem_active_retire(struct i915_gem_active *active, |
Chris Wilson | d72d908 | 2016-08-04 07:52:31 +0100 | [diff] [blame] | 635 | struct mutex *mutex) |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 636 | { |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 637 | struct drm_i915_gem_request *request; |
| 638 | int ret; |
| 639 | |
Chris Wilson | 385384a | 2016-08-09 08:37:01 +0100 | [diff] [blame] | 640 | request = i915_gem_active_raw(active, mutex); |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 641 | if (!request) |
| 642 | return 0; |
| 643 | |
Chris Wilson | 776f323 | 2016-08-04 07:52:40 +0100 | [diff] [blame] | 644 | ret = i915_wait_request(request, true, NULL, NULL); |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 645 | if (ret) |
| 646 | return ret; |
| 647 | |
| 648 | list_del_init(&active->link); |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 649 | RCU_INIT_POINTER(active->request, NULL); |
| 650 | |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 651 | active->retire(active, request); |
| 652 | |
| 653 | return 0; |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | /* Convenience functions for peeking at state inside active's request whilst |
| 657 | * guarded by the struct_mutex. |
| 658 | */ |
| 659 | |
| 660 | static inline uint32_t |
Chris Wilson | d72d908 | 2016-08-04 07:52:31 +0100 | [diff] [blame] | 661 | i915_gem_active_get_seqno(const struct i915_gem_active *active, |
| 662 | struct mutex *mutex) |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 663 | { |
Chris Wilson | d72d908 | 2016-08-04 07:52:31 +0100 | [diff] [blame] | 664 | return i915_gem_request_get_seqno(i915_gem_active_peek(active, mutex)); |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static inline struct intel_engine_cs * |
Chris Wilson | d72d908 | 2016-08-04 07:52:31 +0100 | [diff] [blame] | 668 | i915_gem_active_get_engine(const struct i915_gem_active *active, |
| 669 | struct mutex *mutex) |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 670 | { |
Chris Wilson | d72d908 | 2016-08-04 07:52:31 +0100 | [diff] [blame] | 671 | return i915_gem_request_get_engine(i915_gem_active_peek(active, mutex)); |
Chris Wilson | 27c01aa | 2016-08-04 07:52:30 +0100 | [diff] [blame] | 672 | } |
| 673 | |
Chris Wilson | 381f371 | 2016-08-04 07:52:29 +0100 | [diff] [blame] | 674 | #define for_each_active(mask, idx) \ |
| 675 | for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx)) |
| 676 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 677 | #endif /* I915_GEM_REQUEST_H */ |