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