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 | |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 25 | #include <linux/prefetch.h> |
Chris Wilson | b52992c | 2016-10-28 13:58:24 +0100 | [diff] [blame] | 26 | #include <linux/dma-fence-array.h> |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 27 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 28 | #include "i915_drv.h" |
| 29 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 30 | static const char *i915_fence_get_driver_name(struct dma_fence *fence) |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 31 | { |
| 32 | return "i915"; |
| 33 | } |
| 34 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 35 | static const char *i915_fence_get_timeline_name(struct dma_fence *fence) |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 36 | { |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 37 | return to_request(fence)->timeline->common->name; |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 40 | static bool i915_fence_signaled(struct dma_fence *fence) |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 41 | { |
| 42 | return i915_gem_request_completed(to_request(fence)); |
| 43 | } |
| 44 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 45 | static bool i915_fence_enable_signaling(struct dma_fence *fence) |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 46 | { |
| 47 | if (i915_fence_signaled(fence)) |
| 48 | return false; |
| 49 | |
| 50 | intel_engine_enable_signaling(to_request(fence)); |
| 51 | return true; |
| 52 | } |
| 53 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 54 | static signed long i915_fence_wait(struct dma_fence *fence, |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 55 | bool interruptible, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 56 | signed long timeout) |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 57 | { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 58 | return i915_wait_request(to_request(fence), interruptible, timeout); |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 61 | static void i915_fence_release(struct dma_fence *fence) |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 62 | { |
| 63 | struct drm_i915_gem_request *req = to_request(fence); |
| 64 | |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 65 | /* The request is put onto a RCU freelist (i.e. the address |
| 66 | * is immediately reused), mark the fences as being freed now. |
| 67 | * Otherwise the debugobjects for the fences are only marked as |
| 68 | * freed when the slab cache itself is freed, and so we would get |
| 69 | * caught trying to reuse dead objects. |
| 70 | */ |
| 71 | i915_sw_fence_fini(&req->submit); |
| 72 | i915_sw_fence_fini(&req->execute); |
| 73 | |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 74 | kmem_cache_free(req->i915->requests, req); |
| 75 | } |
| 76 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 77 | const struct dma_fence_ops i915_fence_ops = { |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 78 | .get_driver_name = i915_fence_get_driver_name, |
| 79 | .get_timeline_name = i915_fence_get_timeline_name, |
| 80 | .enable_signaling = i915_fence_enable_signaling, |
| 81 | .signaled = i915_fence_signaled, |
| 82 | .wait = i915_fence_wait, |
| 83 | .release = i915_fence_release, |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 86 | int i915_gem_request_add_to_client(struct drm_i915_gem_request *req, |
| 87 | struct drm_file *file) |
| 88 | { |
| 89 | struct drm_i915_private *dev_private; |
| 90 | struct drm_i915_file_private *file_priv; |
| 91 | |
| 92 | WARN_ON(!req || !file || req->file_priv); |
| 93 | |
| 94 | if (!req || !file) |
| 95 | return -EINVAL; |
| 96 | |
| 97 | if (req->file_priv) |
| 98 | return -EINVAL; |
| 99 | |
| 100 | dev_private = req->i915; |
| 101 | file_priv = file->driver_priv; |
| 102 | |
| 103 | spin_lock(&file_priv->mm.lock); |
| 104 | req->file_priv = file_priv; |
| 105 | list_add_tail(&req->client_list, &file_priv->mm.request_list); |
| 106 | spin_unlock(&file_priv->mm.lock); |
| 107 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static inline void |
| 112 | i915_gem_request_remove_from_client(struct drm_i915_gem_request *request) |
| 113 | { |
| 114 | struct drm_i915_file_private *file_priv = request->file_priv; |
| 115 | |
| 116 | if (!file_priv) |
| 117 | return; |
| 118 | |
| 119 | spin_lock(&file_priv->mm.lock); |
| 120 | list_del(&request->client_list); |
| 121 | request->file_priv = NULL; |
| 122 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 125 | static struct i915_dependency * |
| 126 | i915_dependency_alloc(struct drm_i915_private *i915) |
| 127 | { |
| 128 | return kmem_cache_alloc(i915->dependencies, GFP_KERNEL); |
| 129 | } |
| 130 | |
| 131 | static void |
| 132 | i915_dependency_free(struct drm_i915_private *i915, |
| 133 | struct i915_dependency *dep) |
| 134 | { |
| 135 | kmem_cache_free(i915->dependencies, dep); |
| 136 | } |
| 137 | |
| 138 | static void |
| 139 | __i915_priotree_add_dependency(struct i915_priotree *pt, |
| 140 | struct i915_priotree *signal, |
| 141 | struct i915_dependency *dep, |
| 142 | unsigned long flags) |
| 143 | { |
Chris Wilson | 20311bd | 2016-11-14 20:41:03 +0000 | [diff] [blame] | 144 | INIT_LIST_HEAD(&dep->dfs_link); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 145 | list_add(&dep->wait_link, &signal->waiters_list); |
| 146 | list_add(&dep->signal_link, &pt->signalers_list); |
| 147 | dep->signaler = signal; |
| 148 | dep->flags = flags; |
| 149 | } |
| 150 | |
| 151 | static int |
| 152 | i915_priotree_add_dependency(struct drm_i915_private *i915, |
| 153 | struct i915_priotree *pt, |
| 154 | struct i915_priotree *signal) |
| 155 | { |
| 156 | struct i915_dependency *dep; |
| 157 | |
| 158 | dep = i915_dependency_alloc(i915); |
| 159 | if (!dep) |
| 160 | return -ENOMEM; |
| 161 | |
| 162 | __i915_priotree_add_dependency(pt, signal, dep, I915_DEPENDENCY_ALLOC); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static void |
| 167 | i915_priotree_fini(struct drm_i915_private *i915, struct i915_priotree *pt) |
| 168 | { |
| 169 | struct i915_dependency *dep, *next; |
| 170 | |
Chris Wilson | 20311bd | 2016-11-14 20:41:03 +0000 | [diff] [blame] | 171 | GEM_BUG_ON(!RB_EMPTY_NODE(&pt->node)); |
| 172 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 173 | /* Everyone we depended upon (the fences we wait to be signaled) |
| 174 | * should retire before us and remove themselves from our list. |
| 175 | * However, retirement is run independently on each timeline and |
| 176 | * so we may be called out-of-order. |
| 177 | */ |
| 178 | list_for_each_entry_safe(dep, next, &pt->signalers_list, signal_link) { |
| 179 | list_del(&dep->wait_link); |
| 180 | if (dep->flags & I915_DEPENDENCY_ALLOC) |
| 181 | i915_dependency_free(i915, dep); |
| 182 | } |
| 183 | |
| 184 | /* Remove ourselves from everyone who depends upon us */ |
| 185 | list_for_each_entry_safe(dep, next, &pt->waiters_list, wait_link) { |
| 186 | list_del(&dep->signal_link); |
| 187 | if (dep->flags & I915_DEPENDENCY_ALLOC) |
| 188 | i915_dependency_free(i915, dep); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | i915_priotree_init(struct i915_priotree *pt) |
| 194 | { |
| 195 | INIT_LIST_HEAD(&pt->signalers_list); |
| 196 | INIT_LIST_HEAD(&pt->waiters_list); |
Chris Wilson | 20311bd | 2016-11-14 20:41:03 +0000 | [diff] [blame] | 197 | RB_CLEAR_NODE(&pt->node); |
| 198 | pt->priority = INT_MIN; |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 201 | static void unreserve_seqno(struct intel_engine_cs *engine) |
| 202 | { |
| 203 | GEM_BUG_ON(!engine->timeline->inflight_seqnos); |
| 204 | engine->timeline->inflight_seqnos--; |
| 205 | } |
| 206 | |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 207 | void i915_gem_retire_noop(struct i915_gem_active *active, |
| 208 | struct drm_i915_gem_request *request) |
| 209 | { |
| 210 | /* Space left intentionally blank */ |
| 211 | } |
| 212 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 213 | static void i915_gem_request_retire(struct drm_i915_gem_request *request) |
| 214 | { |
Chris Wilson | e8a9c58 | 2016-12-18 15:37:20 +0000 | [diff] [blame] | 215 | struct intel_engine_cs *engine = request->engine; |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 216 | struct i915_gem_active *active, *next; |
| 217 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 218 | lockdep_assert_held(&request->i915->drm.struct_mutex); |
Chris Wilson | 48bc2a4 | 2016-11-25 13:17:17 +0000 | [diff] [blame] | 219 | GEM_BUG_ON(!i915_sw_fence_signaled(&request->submit)); |
| 220 | GEM_BUG_ON(!i915_sw_fence_signaled(&request->execute)); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 221 | GEM_BUG_ON(!i915_gem_request_completed(request)); |
Chris Wilson | 4302055 | 2016-11-15 16:46:20 +0000 | [diff] [blame] | 222 | GEM_BUG_ON(!request->i915->gt.active_requests); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 223 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 224 | trace_i915_gem_request_retire(request); |
Chris Wilson | 80b204b | 2016-10-28 13:58:58 +0100 | [diff] [blame] | 225 | |
Chris Wilson | e8a9c58 | 2016-12-18 15:37:20 +0000 | [diff] [blame] | 226 | spin_lock_irq(&engine->timeline->lock); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 227 | list_del_init(&request->link); |
Chris Wilson | e8a9c58 | 2016-12-18 15:37:20 +0000 | [diff] [blame] | 228 | spin_unlock_irq(&engine->timeline->lock); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 229 | |
| 230 | /* We know the GPU must have read the request to have |
| 231 | * sent us the seqno + interrupt, so use the position |
| 232 | * of tail of the request to update the last known position |
| 233 | * of the GPU head. |
| 234 | * |
| 235 | * Note this requires that we are always called in request |
| 236 | * completion order. |
| 237 | */ |
Chris Wilson | 675d9ad | 2016-08-04 07:52:36 +0100 | [diff] [blame] | 238 | list_del(&request->ring_link); |
Chris Wilson | 1dae2df | 2016-08-02 22:50:19 +0100 | [diff] [blame] | 239 | request->ring->last_retired_head = request->postfix; |
Chris Wilson | 4302055 | 2016-11-15 16:46:20 +0000 | [diff] [blame] | 240 | if (!--request->i915->gt.active_requests) { |
| 241 | GEM_BUG_ON(!request->i915->gt.awake); |
| 242 | mod_delayed_work(request->i915->wq, |
| 243 | &request->i915->gt.idle_work, |
| 244 | msecs_to_jiffies(100)); |
| 245 | } |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 246 | unreserve_seqno(request->engine); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 247 | |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 248 | /* Walk through the active list, calling retire on each. This allows |
| 249 | * objects to track their GPU activity and mark themselves as idle |
| 250 | * when their *last* active request is completed (updating state |
| 251 | * tracking lists for eviction, active references for GEM, etc). |
| 252 | * |
| 253 | * As the ->retire() may free the node, we decouple it first and |
| 254 | * pass along the auxiliary information (to avoid dereferencing |
| 255 | * the node after the callback). |
| 256 | */ |
| 257 | list_for_each_entry_safe(active, next, &request->active_list, link) { |
| 258 | /* In microbenchmarks or focusing upon time inside the kernel, |
| 259 | * we may spend an inordinate amount of time simply handling |
| 260 | * the retirement of requests and processing their callbacks. |
| 261 | * Of which, this loop itself is particularly hot due to the |
| 262 | * cache misses when jumping around the list of i915_gem_active. |
| 263 | * So we try to keep this loop as streamlined as possible and |
| 264 | * also prefetch the next i915_gem_active to try and hide |
| 265 | * the likely cache miss. |
| 266 | */ |
| 267 | prefetchw(next); |
| 268 | |
| 269 | INIT_LIST_HEAD(&active->link); |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 270 | RCU_INIT_POINTER(active->request, NULL); |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 271 | |
| 272 | active->retire(active, request); |
| 273 | } |
| 274 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 275 | i915_gem_request_remove_from_client(request); |
| 276 | |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 277 | /* Retirement decays the ban score as it is a sign of ctx progress */ |
Mika Kuoppala | bc1d53c | 2016-11-16 17:20:34 +0200 | [diff] [blame] | 278 | if (request->ctx->ban_score > 0) |
| 279 | request->ctx->ban_score--; |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 280 | |
Chris Wilson | e8a9c58 | 2016-12-18 15:37:20 +0000 | [diff] [blame] | 281 | /* The backing object for the context is done after switching to the |
| 282 | * *next* context. Therefore we cannot retire the previous context until |
| 283 | * the next context has already started running. However, since we |
| 284 | * cannot take the required locks at i915_gem_request_submit() we |
| 285 | * defer the unpinning of the active context to now, retirement of |
| 286 | * the subsequent request. |
| 287 | */ |
| 288 | if (engine->last_retired_context) |
| 289 | engine->context_unpin(engine, engine->last_retired_context); |
| 290 | engine->last_retired_context = request->ctx; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 291 | |
| 292 | dma_fence_signal(&request->fence); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 293 | |
| 294 | i915_priotree_fini(request->i915, &request->priotree); |
Chris Wilson | e8a261e | 2016-07-20 13:31:49 +0100 | [diff] [blame] | 295 | i915_gem_request_put(request); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void i915_gem_request_retire_upto(struct drm_i915_gem_request *req) |
| 299 | { |
| 300 | struct intel_engine_cs *engine = req->engine; |
| 301 | struct drm_i915_gem_request *tmp; |
| 302 | |
| 303 | lockdep_assert_held(&req->i915->drm.struct_mutex); |
Chris Wilson | 4ffd6e0 | 2016-11-25 13:17:15 +0000 | [diff] [blame] | 304 | GEM_BUG_ON(!i915_gem_request_completed(req)); |
| 305 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 306 | if (list_empty(&req->link)) |
| 307 | return; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 308 | |
| 309 | do { |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 310 | tmp = list_first_entry(&engine->timeline->requests, |
Chris Wilson | efdf7c0 | 2016-08-04 07:52:33 +0100 | [diff] [blame] | 311 | typeof(*tmp), link); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 312 | |
| 313 | i915_gem_request_retire(tmp); |
| 314 | } while (tmp != req); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 315 | } |
| 316 | |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 317 | static int reset_all_global_seqno(struct drm_i915_private *i915, u32 seqno) |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 318 | { |
Chris Wilson | 85e17f5 | 2016-10-28 13:58:53 +0100 | [diff] [blame] | 319 | struct i915_gem_timeline *timeline = &i915->gt.global_timeline; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 320 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 321 | enum intel_engine_id id; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 322 | int ret; |
| 323 | |
| 324 | /* Carefully retire all requests without writing to the rings */ |
Chris Wilson | 85e17f5 | 2016-10-28 13:58:53 +0100 | [diff] [blame] | 325 | ret = i915_gem_wait_for_idle(i915, |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 326 | I915_WAIT_INTERRUPTIBLE | |
| 327 | I915_WAIT_LOCKED); |
| 328 | if (ret) |
| 329 | return ret; |
| 330 | |
Chris Wilson | 85e17f5 | 2016-10-28 13:58:53 +0100 | [diff] [blame] | 331 | i915_gem_retire_requests(i915); |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 332 | GEM_BUG_ON(i915->gt.active_requests > 1); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 333 | |
| 334 | /* If the seqno wraps around, we need to clear the breadcrumb rbtree */ |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 335 | for_each_engine(engine, i915, id) { |
| 336 | struct intel_timeline *tl = &timeline->engine[id]; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 337 | |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 338 | if (!i915_seqno_passed(seqno, tl->seqno)) { |
| 339 | /* spin until threads are complete */ |
| 340 | while (intel_breadcrumbs_busy(engine)) |
| 341 | cond_resched(); |
| 342 | } |
| 343 | |
| 344 | /* Finally reset hw state */ |
| 345 | tl->seqno = seqno; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 346 | intel_engine_init_global_seqno(engine, seqno); |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 347 | } |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 348 | |
Chris Wilson | 85e17f5 | 2016-10-28 13:58:53 +0100 | [diff] [blame] | 349 | list_for_each_entry(timeline, &i915->gt.timelines, link) { |
| 350 | for_each_engine(engine, i915, id) { |
| 351 | struct intel_timeline *tl = &timeline->engine[id]; |
| 352 | |
| 353 | memset(tl->sync_seqno, 0, sizeof(tl->sync_seqno)); |
| 354 | } |
| 355 | } |
| 356 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 360 | int i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno) |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 361 | { |
| 362 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 363 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 364 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
| 365 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 366 | if (seqno == 0) |
| 367 | return -EINVAL; |
| 368 | |
| 369 | /* HWS page needs to be set less than what we |
| 370 | * will inject to ring |
| 371 | */ |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 372 | return reset_all_global_seqno(dev_priv, seqno - 1); |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 373 | } |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 374 | |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 375 | static int reserve_seqno(struct intel_engine_cs *engine) |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 376 | { |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 377 | u32 active = ++engine->timeline->inflight_seqnos; |
| 378 | u32 seqno = engine->timeline->seqno; |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 379 | int ret; |
| 380 | |
| 381 | /* Reservation is fine until we need to wrap around */ |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 382 | if (likely(!add_overflows(seqno, active))) |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 383 | return 0; |
| 384 | |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 385 | /* Even though we are tracking inflight seqno individually on each |
| 386 | * engine, other engines may be observing us using hw semaphores and |
| 387 | * so we need to idle all engines before wrapping around this engine. |
| 388 | * As all engines are then idle, we can reset the seqno on all, so |
| 389 | * we don't stall in quick succession if each engine is being |
| 390 | * similarly utilized. |
| 391 | */ |
| 392 | ret = reset_all_global_seqno(engine->i915, 0); |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 393 | if (ret) { |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 394 | engine->timeline->inflight_seqnos--; |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 395 | return ret; |
| 396 | } |
| 397 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 398 | return 0; |
| 399 | } |
| 400 | |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 401 | static u32 timeline_get_seqno(struct intel_timeline *tl) |
Chris Wilson | 80b204b | 2016-10-28 13:58:58 +0100 | [diff] [blame] | 402 | { |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 403 | return ++tl->seqno; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Chris Wilson | d55ac5b | 2016-11-14 20:40:59 +0000 | [diff] [blame] | 406 | void __i915_gem_request_submit(struct drm_i915_gem_request *request) |
Chris Wilson | 5590af3 | 2016-09-09 14:11:54 +0100 | [diff] [blame] | 407 | { |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 408 | struct intel_engine_cs *engine = request->engine; |
Chris Wilson | f2d1329 | 2016-10-28 13:58:57 +0100 | [diff] [blame] | 409 | struct intel_timeline *timeline; |
| 410 | u32 seqno; |
Chris Wilson | 5590af3 | 2016-09-09 14:11:54 +0100 | [diff] [blame] | 411 | |
Chris Wilson | 80b204b | 2016-10-28 13:58:58 +0100 | [diff] [blame] | 412 | /* Transfer from per-context onto the global per-engine timeline */ |
| 413 | timeline = engine->timeline; |
| 414 | GEM_BUG_ON(timeline == request->timeline); |
Chris Wilson | d55ac5b | 2016-11-14 20:40:59 +0000 | [diff] [blame] | 415 | assert_spin_locked(&timeline->lock); |
Chris Wilson | 5590af3 | 2016-09-09 14:11:54 +0100 | [diff] [blame] | 416 | |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 417 | seqno = timeline_get_seqno(timeline); |
Chris Wilson | f2d1329 | 2016-10-28 13:58:57 +0100 | [diff] [blame] | 418 | GEM_BUG_ON(!seqno); |
| 419 | GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine), seqno)); |
| 420 | |
Chris Wilson | f2d1329 | 2016-10-28 13:58:57 +0100 | [diff] [blame] | 421 | /* We may be recursing from the signal callback of another i915 fence */ |
| 422 | spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING); |
| 423 | request->global_seqno = seqno; |
| 424 | if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags)) |
| 425 | intel_engine_enable_signaling(request); |
| 426 | spin_unlock(&request->lock); |
| 427 | |
| 428 | GEM_BUG_ON(!request->global_seqno); |
Chris Wilson | caddfe7 | 2016-10-28 13:58:52 +0100 | [diff] [blame] | 429 | engine->emit_breadcrumb(request, |
| 430 | request->ring->vaddr + request->postfix); |
Chris Wilson | 5590af3 | 2016-09-09 14:11:54 +0100 | [diff] [blame] | 431 | |
Chris Wilson | bb89485 | 2016-11-14 20:40:57 +0000 | [diff] [blame] | 432 | spin_lock(&request->timeline->lock); |
Chris Wilson | 80b204b | 2016-10-28 13:58:58 +0100 | [diff] [blame] | 433 | list_move_tail(&request->link, &timeline->requests); |
| 434 | spin_unlock(&request->timeline->lock); |
| 435 | |
Chris Wilson | 23902e4 | 2016-11-14 20:40:58 +0000 | [diff] [blame] | 436 | i915_sw_fence_commit(&request->execute); |
Tvrtko Ursulin | 354d036 | 2017-02-21 11:01:42 +0000 | [diff] [blame] | 437 | trace_i915_gem_request_execute(request); |
Chris Wilson | d55ac5b | 2016-11-14 20:40:59 +0000 | [diff] [blame] | 438 | } |
Chris Wilson | 23902e4 | 2016-11-14 20:40:58 +0000 | [diff] [blame] | 439 | |
Chris Wilson | d55ac5b | 2016-11-14 20:40:59 +0000 | [diff] [blame] | 440 | void i915_gem_request_submit(struct drm_i915_gem_request *request) |
| 441 | { |
| 442 | struct intel_engine_cs *engine = request->engine; |
| 443 | unsigned long flags; |
| 444 | |
| 445 | /* Will be called from irq-context when using foreign fences. */ |
| 446 | spin_lock_irqsave(&engine->timeline->lock, flags); |
| 447 | |
| 448 | __i915_gem_request_submit(request); |
| 449 | |
| 450 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
| 451 | } |
| 452 | |
| 453 | static int __i915_sw_fence_call |
| 454 | submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state) |
| 455 | { |
Chris Wilson | 48bc2a4 | 2016-11-25 13:17:17 +0000 | [diff] [blame] | 456 | struct drm_i915_gem_request *request = |
| 457 | container_of(fence, typeof(*request), submit); |
Chris Wilson | d55ac5b | 2016-11-14 20:40:59 +0000 | [diff] [blame] | 458 | |
Chris Wilson | 48bc2a4 | 2016-11-25 13:17:17 +0000 | [diff] [blame] | 459 | switch (state) { |
| 460 | case FENCE_COMPLETE: |
Tvrtko Ursulin | 354d036 | 2017-02-21 11:01:42 +0000 | [diff] [blame] | 461 | trace_i915_gem_request_submit(request); |
Chris Wilson | d55ac5b | 2016-11-14 20:40:59 +0000 | [diff] [blame] | 462 | request->engine->submit_request(request); |
Chris Wilson | 48bc2a4 | 2016-11-25 13:17:17 +0000 | [diff] [blame] | 463 | break; |
| 464 | |
| 465 | case FENCE_FREE: |
| 466 | i915_gem_request_put(request); |
| 467 | break; |
Chris Wilson | d55ac5b | 2016-11-14 20:40:59 +0000 | [diff] [blame] | 468 | } |
Chris Wilson | 80b204b | 2016-10-28 13:58:58 +0100 | [diff] [blame] | 469 | |
Chris Wilson | 5590af3 | 2016-09-09 14:11:54 +0100 | [diff] [blame] | 470 | return NOTIFY_DONE; |
| 471 | } |
| 472 | |
Chris Wilson | 23902e4 | 2016-11-14 20:40:58 +0000 | [diff] [blame] | 473 | static int __i915_sw_fence_call |
| 474 | execute_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state) |
| 475 | { |
Chris Wilson | 48bc2a4 | 2016-11-25 13:17:17 +0000 | [diff] [blame] | 476 | struct drm_i915_gem_request *request = |
| 477 | container_of(fence, typeof(*request), execute); |
| 478 | |
| 479 | switch (state) { |
| 480 | case FENCE_COMPLETE: |
| 481 | break; |
| 482 | |
| 483 | case FENCE_FREE: |
| 484 | i915_gem_request_put(request); |
| 485 | break; |
| 486 | } |
| 487 | |
Chris Wilson | 23902e4 | 2016-11-14 20:40:58 +0000 | [diff] [blame] | 488 | return NOTIFY_DONE; |
| 489 | } |
| 490 | |
Chris Wilson | 8e63717 | 2016-08-02 22:50:26 +0100 | [diff] [blame] | 491 | /** |
| 492 | * i915_gem_request_alloc - allocate a request structure |
| 493 | * |
| 494 | * @engine: engine that we wish to issue the request on. |
| 495 | * @ctx: context that the request will be associated with. |
| 496 | * This can be NULL if the request is not directly related to |
| 497 | * any specific user context, in which case this function will |
| 498 | * choose an appropriate context to use. |
| 499 | * |
| 500 | * Returns a pointer to the allocated request if successful, |
| 501 | * or an error code if not. |
| 502 | */ |
| 503 | struct drm_i915_gem_request * |
| 504 | i915_gem_request_alloc(struct intel_engine_cs *engine, |
| 505 | struct i915_gem_context *ctx) |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 506 | { |
| 507 | struct drm_i915_private *dev_priv = engine->i915; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 508 | struct drm_i915_gem_request *req; |
| 509 | int ret; |
| 510 | |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 511 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
| 512 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 513 | /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report |
Chris Wilson | 6ffb7d0 | 2017-01-14 16:23:33 +0000 | [diff] [blame] | 514 | * EIO if the GPU is already wedged. |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 515 | */ |
Chris Wilson | 6ffb7d0 | 2017-01-14 16:23:33 +0000 | [diff] [blame] | 516 | if (i915_terminally_wedged(&dev_priv->gpu_error)) |
| 517 | return ERR_PTR(-EIO); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 518 | |
Chris Wilson | e8a9c58 | 2016-12-18 15:37:20 +0000 | [diff] [blame] | 519 | /* Pinning the contexts may generate requests in order to acquire |
| 520 | * GGTT space, so do this first before we reserve a seqno for |
| 521 | * ourselves. |
| 522 | */ |
| 523 | ret = engine->context_pin(engine, ctx); |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 524 | if (ret) |
| 525 | return ERR_PTR(ret); |
| 526 | |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 527 | ret = reserve_seqno(engine); |
Chris Wilson | e8a9c58 | 2016-12-18 15:37:20 +0000 | [diff] [blame] | 528 | if (ret) |
| 529 | goto err_unpin; |
| 530 | |
Chris Wilson | 9b5f4e5 | 2016-07-20 09:21:09 +0100 | [diff] [blame] | 531 | /* Move the oldest request to the slab-cache (if not in use!) */ |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 532 | req = list_first_entry_or_null(&engine->timeline->requests, |
Chris Wilson | efdf7c0 | 2016-08-04 07:52:33 +0100 | [diff] [blame] | 533 | typeof(*req), link); |
Chris Wilson | 80b204b | 2016-10-28 13:58:58 +0100 | [diff] [blame] | 534 | if (req && __i915_gem_request_completed(req)) |
Chris Wilson | 2a1d775 | 2016-07-26 12:01:51 +0100 | [diff] [blame] | 535 | i915_gem_request_retire(req); |
Chris Wilson | 9b5f4e5 | 2016-07-20 09:21:09 +0100 | [diff] [blame] | 536 | |
Chris Wilson | 5a198b8 | 2016-08-09 09:23:34 +0100 | [diff] [blame] | 537 | /* Beware: Dragons be flying overhead. |
| 538 | * |
| 539 | * We use RCU to look up requests in flight. The lookups may |
| 540 | * race with the request being allocated from the slab freelist. |
| 541 | * That is the request we are writing to here, may be in the process |
Chris Wilson | 1426f71 | 2016-08-09 17:03:22 +0100 | [diff] [blame] | 542 | * of being read by __i915_gem_active_get_rcu(). As such, |
Chris Wilson | 5a198b8 | 2016-08-09 09:23:34 +0100 | [diff] [blame] | 543 | * we have to be very careful when overwriting the contents. During |
| 544 | * the RCU lookup, we change chase the request->engine pointer, |
Chris Wilson | 65e4760 | 2016-10-28 13:58:49 +0100 | [diff] [blame] | 545 | * read the request->global_seqno and increment the reference count. |
Chris Wilson | 5a198b8 | 2016-08-09 09:23:34 +0100 | [diff] [blame] | 546 | * |
| 547 | * The reference count is incremented atomically. If it is zero, |
| 548 | * the lookup knows the request is unallocated and complete. Otherwise, |
| 549 | * it is either still in use, or has been reallocated and reset |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 550 | * with dma_fence_init(). This increment is safe for release as we |
| 551 | * check that the request we have a reference to and matches the active |
Chris Wilson | 5a198b8 | 2016-08-09 09:23:34 +0100 | [diff] [blame] | 552 | * request. |
| 553 | * |
| 554 | * Before we increment the refcount, we chase the request->engine |
| 555 | * pointer. We must not call kmem_cache_zalloc() or else we set |
| 556 | * that pointer to NULL and cause a crash during the lookup. If |
| 557 | * we see the request is completed (based on the value of the |
| 558 | * old engine and seqno), the lookup is complete and reports NULL. |
| 559 | * If we decide the request is not completed (new engine or seqno), |
| 560 | * then we grab a reference and double check that it is still the |
| 561 | * active request - which it won't be and restart the lookup. |
| 562 | * |
| 563 | * Do not use kmem_cache_zalloc() here! |
| 564 | */ |
| 565 | req = kmem_cache_alloc(dev_priv->requests, GFP_KERNEL); |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 566 | if (!req) { |
| 567 | ret = -ENOMEM; |
| 568 | goto err_unreserve; |
| 569 | } |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 570 | |
Chris Wilson | 80b204b | 2016-10-28 13:58:58 +0100 | [diff] [blame] | 571 | req->timeline = i915_gem_context_lookup_timeline(ctx, engine); |
| 572 | GEM_BUG_ON(req->timeline == engine->timeline); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 573 | |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 574 | spin_lock_init(&req->lock); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 575 | dma_fence_init(&req->fence, |
| 576 | &i915_fence_ops, |
| 577 | &req->lock, |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 578 | req->timeline->fence_context, |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 579 | timeline_get_seqno(req->timeline)); |
Chris Wilson | 0476965 | 2016-07-20 09:21:11 +0100 | [diff] [blame] | 580 | |
Chris Wilson | 48bc2a4 | 2016-11-25 13:17:17 +0000 | [diff] [blame] | 581 | /* We bump the ref for the fence chain */ |
| 582 | i915_sw_fence_init(&i915_gem_request_get(req)->submit, submit_notify); |
| 583 | i915_sw_fence_init(&i915_gem_request_get(req)->execute, execute_notify); |
| 584 | |
Chris Wilson | 23902e4 | 2016-11-14 20:40:58 +0000 | [diff] [blame] | 585 | /* Ensure that the execute fence completes after the submit fence - |
| 586 | * as we complete the execute fence from within the submit fence |
| 587 | * callback, its completion would otherwise be visible first. |
| 588 | */ |
| 589 | i915_sw_fence_await_sw_fence(&req->execute, &req->submit, &req->execq); |
Chris Wilson | 5590af3 | 2016-09-09 14:11:54 +0100 | [diff] [blame] | 590 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 591 | i915_priotree_init(&req->priotree); |
| 592 | |
Chris Wilson | fa545cb | 2016-08-04 07:52:35 +0100 | [diff] [blame] | 593 | INIT_LIST_HEAD(&req->active_list); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 594 | req->i915 = dev_priv; |
| 595 | req->engine = engine; |
Chris Wilson | e8a9c58 | 2016-12-18 15:37:20 +0000 | [diff] [blame] | 596 | req->ctx = ctx; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 597 | |
Chris Wilson | 5a198b8 | 2016-08-09 09:23:34 +0100 | [diff] [blame] | 598 | /* No zalloc, must clear what we need by hand */ |
Chris Wilson | f2d1329 | 2016-10-28 13:58:57 +0100 | [diff] [blame] | 599 | req->global_seqno = 0; |
Chris Wilson | 5a198b8 | 2016-08-09 09:23:34 +0100 | [diff] [blame] | 600 | req->file_priv = NULL; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 601 | req->batch = NULL; |
Chris Wilson | 5a198b8 | 2016-08-09 09:23:34 +0100 | [diff] [blame] | 602 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 603 | /* |
| 604 | * Reserve space in the ring buffer for all the commands required to |
| 605 | * eventually emit this request. This is to guarantee that the |
| 606 | * i915_add_request() call can't fail. Note that the reserve may need |
| 607 | * to be redone if the request is not actually submitted straight |
| 608 | * away, e.g. because a GPU scheduler has deferred it. |
| 609 | */ |
| 610 | req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST; |
Chris Wilson | 98f29e8 | 2016-10-28 13:58:51 +0100 | [diff] [blame] | 611 | GEM_BUG_ON(req->reserved_space < engine->emit_breadcrumb_sz); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 612 | |
Chris Wilson | f73e739 | 2016-12-18 15:37:24 +0000 | [diff] [blame] | 613 | ret = engine->request_alloc(req); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 614 | if (ret) |
| 615 | goto err_ctx; |
| 616 | |
Chris Wilson | d045446 | 2016-08-15 10:48:40 +0100 | [diff] [blame] | 617 | /* Record the position of the start of the request so that |
| 618 | * should we detect the updated seqno part-way through the |
| 619 | * GPU processing the request, we never over-estimate the |
| 620 | * position of the head. |
| 621 | */ |
| 622 | req->head = req->ring->tail; |
| 623 | |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 624 | /* Check that we didn't interrupt ourselves with a new request */ |
| 625 | GEM_BUG_ON(req->timeline->seqno != req->fence.seqno); |
Chris Wilson | 8e63717 | 2016-08-02 22:50:26 +0100 | [diff] [blame] | 626 | return req; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 627 | |
| 628 | err_ctx: |
Chris Wilson | 1618bdb | 2016-11-25 13:17:16 +0000 | [diff] [blame] | 629 | /* Make sure we didn't add ourselves to external state before freeing */ |
| 630 | GEM_BUG_ON(!list_empty(&req->active_list)); |
| 631 | GEM_BUG_ON(!list_empty(&req->priotree.signalers_list)); |
| 632 | GEM_BUG_ON(!list_empty(&req->priotree.waiters_list)); |
| 633 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 634 | kmem_cache_free(dev_priv->requests, req); |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 635 | err_unreserve: |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 636 | unreserve_seqno(engine); |
Chris Wilson | e8a9c58 | 2016-12-18 15:37:20 +0000 | [diff] [blame] | 637 | err_unpin: |
| 638 | engine->context_unpin(engine, ctx); |
Chris Wilson | 8e63717 | 2016-08-02 22:50:26 +0100 | [diff] [blame] | 639 | return ERR_PTR(ret); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 640 | } |
| 641 | |
Chris Wilson | a2bc469 | 2016-09-09 14:11:56 +0100 | [diff] [blame] | 642 | static int |
| 643 | i915_gem_request_await_request(struct drm_i915_gem_request *to, |
| 644 | struct drm_i915_gem_request *from) |
| 645 | { |
Chris Wilson | 85e17f5 | 2016-10-28 13:58:53 +0100 | [diff] [blame] | 646 | int ret; |
Chris Wilson | a2bc469 | 2016-09-09 14:11:56 +0100 | [diff] [blame] | 647 | |
| 648 | GEM_BUG_ON(to == from); |
| 649 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 650 | if (to->engine->schedule) { |
| 651 | ret = i915_priotree_add_dependency(to->i915, |
| 652 | &to->priotree, |
| 653 | &from->priotree); |
| 654 | if (ret < 0) |
| 655 | return ret; |
| 656 | } |
| 657 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 658 | if (to->timeline == from->timeline) |
Chris Wilson | a2bc469 | 2016-09-09 14:11:56 +0100 | [diff] [blame] | 659 | return 0; |
| 660 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 661 | if (to->engine == from->engine) { |
| 662 | ret = i915_sw_fence_await_sw_fence_gfp(&to->submit, |
| 663 | &from->submit, |
| 664 | GFP_KERNEL); |
| 665 | return ret < 0 ? ret : 0; |
| 666 | } |
| 667 | |
Chris Wilson | 65e4760 | 2016-10-28 13:58:49 +0100 | [diff] [blame] | 668 | if (!from->global_seqno) { |
| 669 | ret = i915_sw_fence_await_dma_fence(&to->submit, |
| 670 | &from->fence, 0, |
| 671 | GFP_KERNEL); |
| 672 | return ret < 0 ? ret : 0; |
| 673 | } |
| 674 | |
Chris Wilson | 85e17f5 | 2016-10-28 13:58:53 +0100 | [diff] [blame] | 675 | if (from->global_seqno <= to->timeline->sync_seqno[from->engine->id]) |
Chris Wilson | a2bc469 | 2016-09-09 14:11:56 +0100 | [diff] [blame] | 676 | return 0; |
| 677 | |
| 678 | trace_i915_gem_ring_sync_to(to, from); |
| 679 | if (!i915.semaphores) { |
Chris Wilson | 0a046a0 | 2016-09-09 14:12:00 +0100 | [diff] [blame] | 680 | if (!i915_spin_request(from, TASK_INTERRUPTIBLE, 2)) { |
| 681 | ret = i915_sw_fence_await_dma_fence(&to->submit, |
| 682 | &from->fence, 0, |
| 683 | GFP_KERNEL); |
| 684 | if (ret < 0) |
| 685 | return ret; |
| 686 | } |
Chris Wilson | a2bc469 | 2016-09-09 14:11:56 +0100 | [diff] [blame] | 687 | } else { |
| 688 | ret = to->engine->semaphore.sync_to(to, from); |
| 689 | if (ret) |
| 690 | return ret; |
| 691 | } |
| 692 | |
Chris Wilson | 85e17f5 | 2016-10-28 13:58:53 +0100 | [diff] [blame] | 693 | to->timeline->sync_seqno[from->engine->id] = from->global_seqno; |
Chris Wilson | a2bc469 | 2016-09-09 14:11:56 +0100 | [diff] [blame] | 694 | return 0; |
| 695 | } |
| 696 | |
Chris Wilson | b52992c | 2016-10-28 13:58:24 +0100 | [diff] [blame] | 697 | int |
| 698 | i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req, |
| 699 | struct dma_fence *fence) |
| 700 | { |
| 701 | struct dma_fence_array *array; |
| 702 | int ret; |
| 703 | int i; |
| 704 | |
| 705 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
| 706 | return 0; |
| 707 | |
| 708 | if (dma_fence_is_i915(fence)) |
| 709 | return i915_gem_request_await_request(req, to_request(fence)); |
| 710 | |
| 711 | if (!dma_fence_is_array(fence)) { |
| 712 | ret = i915_sw_fence_await_dma_fence(&req->submit, |
| 713 | fence, I915_FENCE_TIMEOUT, |
| 714 | GFP_KERNEL); |
| 715 | return ret < 0 ? ret : 0; |
| 716 | } |
| 717 | |
| 718 | /* Note that if the fence-array was created in signal-on-any mode, |
| 719 | * we should *not* decompose it into its individual fences. However, |
| 720 | * we don't currently store which mode the fence-array is operating |
| 721 | * in. Fortunately, the only user of signal-on-any is private to |
| 722 | * amdgpu and we should not see any incoming fence-array from |
| 723 | * sync-file being in signal-on-any mode. |
| 724 | */ |
| 725 | |
| 726 | array = to_dma_fence_array(fence); |
| 727 | for (i = 0; i < array->num_fences; i++) { |
| 728 | struct dma_fence *child = array->fences[i]; |
| 729 | |
| 730 | if (dma_fence_is_i915(child)) |
| 731 | ret = i915_gem_request_await_request(req, |
| 732 | to_request(child)); |
| 733 | else |
| 734 | ret = i915_sw_fence_await_dma_fence(&req->submit, |
| 735 | child, I915_FENCE_TIMEOUT, |
| 736 | GFP_KERNEL); |
| 737 | if (ret < 0) |
| 738 | return ret; |
| 739 | } |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
Chris Wilson | a2bc469 | 2016-09-09 14:11:56 +0100 | [diff] [blame] | 744 | /** |
| 745 | * i915_gem_request_await_object - set this request to (async) wait upon a bo |
| 746 | * |
| 747 | * @to: request we are wishing to use |
| 748 | * @obj: object which may be in use on another ring. |
| 749 | * |
| 750 | * This code is meant to abstract object synchronization with the GPU. |
| 751 | * Conceptually we serialise writes between engines inside the GPU. |
| 752 | * We only allow one engine to write into a buffer at any time, but |
| 753 | * multiple readers. To ensure each has a coherent view of memory, we must: |
| 754 | * |
| 755 | * - If there is an outstanding write request to the object, the new |
| 756 | * request must wait for it to complete (either CPU or in hw, requests |
| 757 | * on the same ring will be naturally ordered). |
| 758 | * |
| 759 | * - If we are a write request (pending_write_domain is set), the new |
| 760 | * request must wait for outstanding read requests to complete. |
| 761 | * |
| 762 | * Returns 0 if successful, else propagates up the lower layer error. |
| 763 | */ |
| 764 | int |
| 765 | i915_gem_request_await_object(struct drm_i915_gem_request *to, |
| 766 | struct drm_i915_gem_object *obj, |
| 767 | bool write) |
| 768 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 769 | struct dma_fence *excl; |
| 770 | int ret = 0; |
Chris Wilson | a2bc469 | 2016-09-09 14:11:56 +0100 | [diff] [blame] | 771 | |
| 772 | if (write) { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 773 | struct dma_fence **shared; |
| 774 | unsigned int count, i; |
Chris Wilson | a2bc469 | 2016-09-09 14:11:56 +0100 | [diff] [blame] | 775 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 776 | ret = reservation_object_get_fences_rcu(obj->resv, |
| 777 | &excl, &count, &shared); |
Chris Wilson | a2bc469 | 2016-09-09 14:11:56 +0100 | [diff] [blame] | 778 | if (ret) |
| 779 | return ret; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 780 | |
| 781 | for (i = 0; i < count; i++) { |
| 782 | ret = i915_gem_request_await_dma_fence(to, shared[i]); |
| 783 | if (ret) |
| 784 | break; |
| 785 | |
| 786 | dma_fence_put(shared[i]); |
| 787 | } |
| 788 | |
| 789 | for (; i < count; i++) |
| 790 | dma_fence_put(shared[i]); |
| 791 | kfree(shared); |
| 792 | } else { |
| 793 | excl = reservation_object_get_excl_rcu(obj->resv); |
Chris Wilson | a2bc469 | 2016-09-09 14:11:56 +0100 | [diff] [blame] | 794 | } |
| 795 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 796 | if (excl) { |
| 797 | if (ret == 0) |
| 798 | ret = i915_gem_request_await_dma_fence(to, excl); |
| 799 | |
| 800 | dma_fence_put(excl); |
| 801 | } |
| 802 | |
| 803 | return ret; |
Chris Wilson | a2bc469 | 2016-09-09 14:11:56 +0100 | [diff] [blame] | 804 | } |
| 805 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 806 | static void i915_gem_mark_busy(const struct intel_engine_cs *engine) |
| 807 | { |
| 808 | struct drm_i915_private *dev_priv = engine->i915; |
| 809 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 810 | if (dev_priv->gt.awake) |
| 811 | return; |
| 812 | |
Chris Wilson | 4302055 | 2016-11-15 16:46:20 +0000 | [diff] [blame] | 813 | GEM_BUG_ON(!dev_priv->gt.active_requests); |
| 814 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 815 | intel_runtime_pm_get_noresume(dev_priv); |
| 816 | dev_priv->gt.awake = true; |
| 817 | |
Chris Wilson | 54b4f68 | 2016-07-21 21:16:19 +0100 | [diff] [blame] | 818 | intel_enable_gt_powersave(dev_priv); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 819 | i915_update_gfx_val(dev_priv); |
| 820 | if (INTEL_GEN(dev_priv) >= 6) |
| 821 | gen6_rps_busy(dev_priv); |
| 822 | |
| 823 | queue_delayed_work(dev_priv->wq, |
| 824 | &dev_priv->gt.retire_work, |
| 825 | round_jiffies_up_relative(HZ)); |
| 826 | } |
| 827 | |
| 828 | /* |
| 829 | * NB: This function is not allowed to fail. Doing so would mean the the |
| 830 | * request is not being tracked for completion but the work itself is |
| 831 | * going to happen on the hardware. This would be a Bad Thing(tm). |
| 832 | */ |
Chris Wilson | 17f298cf | 2016-08-10 13:41:46 +0100 | [diff] [blame] | 833 | void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches) |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 834 | { |
Chris Wilson | 95b2ab5 | 2016-08-15 10:48:46 +0100 | [diff] [blame] | 835 | struct intel_engine_cs *engine = request->engine; |
| 836 | struct intel_ring *ring = request->ring; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 837 | struct intel_timeline *timeline = request->timeline; |
Chris Wilson | 0a046a0 | 2016-09-09 14:12:00 +0100 | [diff] [blame] | 838 | struct drm_i915_gem_request *prev; |
Tvrtko Ursulin | 73dec95 | 2017-02-14 11:32:42 +0000 | [diff] [blame] | 839 | u32 *cs; |
Chris Wilson | caddfe7 | 2016-10-28 13:58:52 +0100 | [diff] [blame] | 840 | int err; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 841 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 842 | lockdep_assert_held(&request->i915->drm.struct_mutex); |
Chris Wilson | 0f25dff | 2016-09-09 14:11:55 +0100 | [diff] [blame] | 843 | trace_i915_gem_request_add(request); |
| 844 | |
Chris Wilson | c781c97 | 2017-01-11 14:08:58 +0000 | [diff] [blame] | 845 | /* Make sure that no request gazumped us - if it was allocated after |
| 846 | * our i915_gem_request_alloc() and called __i915_add_request() before |
| 847 | * us, the timeline will hold its seqno which is later than ours. |
| 848 | */ |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 849 | GEM_BUG_ON(timeline->seqno != request->fence.seqno); |
Chris Wilson | c781c97 | 2017-01-11 14:08:58 +0000 | [diff] [blame] | 850 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 851 | /* |
| 852 | * To ensure that this call will not fail, space for its emissions |
| 853 | * should already have been reserved in the ring buffer. Let the ring |
| 854 | * know that it is time to use that space up. |
| 855 | */ |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 856 | request->reserved_space = 0; |
| 857 | |
| 858 | /* |
| 859 | * Emit any outstanding flushes - execbuf can fail to emit the flush |
| 860 | * after having emitted the batchbuffer command. Hence we need to fix |
| 861 | * things up similar to emitting the lazy request. The difference here |
| 862 | * is that the flush _must_ happen before the next request, no matter |
| 863 | * what. |
| 864 | */ |
| 865 | if (flush_caches) { |
Chris Wilson | caddfe7 | 2016-10-28 13:58:52 +0100 | [diff] [blame] | 866 | err = engine->emit_flush(request, EMIT_FLUSH); |
Chris Wilson | c7fe7d2 | 2016-08-02 22:50:24 +0100 | [diff] [blame] | 867 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 868 | /* Not allowed to fail! */ |
Chris Wilson | caddfe7 | 2016-10-28 13:58:52 +0100 | [diff] [blame] | 869 | WARN(err, "engine->emit_flush() failed: %d!\n", err); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 870 | } |
| 871 | |
Chris Wilson | d045446 | 2016-08-15 10:48:40 +0100 | [diff] [blame] | 872 | /* Record the position of the start of the breadcrumb so that |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 873 | * should we detect the updated seqno part-way through the |
| 874 | * GPU processing the request, we never over-estimate the |
Chris Wilson | d045446 | 2016-08-15 10:48:40 +0100 | [diff] [blame] | 875 | * position of the ring's HEAD. |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 876 | */ |
Tvrtko Ursulin | 73dec95 | 2017-02-14 11:32:42 +0000 | [diff] [blame] | 877 | cs = intel_ring_begin(request, engine->emit_breadcrumb_sz); |
| 878 | GEM_BUG_ON(IS_ERR(cs)); |
| 879 | request->postfix = intel_ring_offset(request, cs); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 880 | |
Chris Wilson | 0f25dff | 2016-09-09 14:11:55 +0100 | [diff] [blame] | 881 | /* Seal the request and mark it as pending execution. Note that |
| 882 | * we may inspect this state, without holding any locks, during |
| 883 | * hangcheck. Hence we apply the barrier to ensure that we do not |
| 884 | * see a more recent value in the hws than we are tracking. |
| 885 | */ |
Chris Wilson | 0a046a0 | 2016-09-09 14:12:00 +0100 | [diff] [blame] | 886 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 887 | prev = i915_gem_active_raw(&timeline->last_request, |
Chris Wilson | 0a046a0 | 2016-09-09 14:12:00 +0100 | [diff] [blame] | 888 | &request->i915->drm.struct_mutex); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 889 | if (prev) { |
Chris Wilson | 0a046a0 | 2016-09-09 14:12:00 +0100 | [diff] [blame] | 890 | i915_sw_fence_await_sw_fence(&request->submit, &prev->submit, |
| 891 | &request->submitq); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 892 | if (engine->schedule) |
| 893 | __i915_priotree_add_dependency(&request->priotree, |
| 894 | &prev->priotree, |
| 895 | &request->dep, |
| 896 | 0); |
| 897 | } |
Chris Wilson | 0a046a0 | 2016-09-09 14:12:00 +0100 | [diff] [blame] | 898 | |
Chris Wilson | 80b204b | 2016-10-28 13:58:58 +0100 | [diff] [blame] | 899 | spin_lock_irq(&timeline->lock); |
Chris Wilson | f2d1329 | 2016-10-28 13:58:57 +0100 | [diff] [blame] | 900 | list_add_tail(&request->link, &timeline->requests); |
Chris Wilson | 80b204b | 2016-10-28 13:58:58 +0100 | [diff] [blame] | 901 | spin_unlock_irq(&timeline->lock); |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 902 | |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 903 | GEM_BUG_ON(timeline->seqno != request->fence.seqno); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 904 | i915_gem_active_set(&timeline->last_request, request); |
Chris Wilson | f2d1329 | 2016-10-28 13:58:57 +0100 | [diff] [blame] | 905 | |
Chris Wilson | 0f25dff | 2016-09-09 14:11:55 +0100 | [diff] [blame] | 906 | list_add_tail(&request->ring_link, &ring->request_list); |
Chris Wilson | f2d1329 | 2016-10-28 13:58:57 +0100 | [diff] [blame] | 907 | request->emitted_jiffies = jiffies; |
Chris Wilson | 0f25dff | 2016-09-09 14:11:55 +0100 | [diff] [blame] | 908 | |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame^] | 909 | if (!request->i915->gt.active_requests++) |
| 910 | i915_gem_mark_busy(engine); |
Chris Wilson | 5590af3 | 2016-09-09 14:11:54 +0100 | [diff] [blame] | 911 | |
Chris Wilson | 0de9136 | 2016-11-14 20:41:01 +0000 | [diff] [blame] | 912 | /* Let the backend know a new request has arrived that may need |
| 913 | * to adjust the existing execution schedule due to a high priority |
| 914 | * request - i.e. we may want to preempt the current request in order |
| 915 | * to run a high priority dependency chain *before* we can execute this |
| 916 | * request. |
| 917 | * |
| 918 | * This is called before the request is ready to run so that we can |
| 919 | * decide whether to preempt the entire chain so that it is ready to |
| 920 | * run at the earliest possible convenience. |
| 921 | */ |
| 922 | if (engine->schedule) |
Chris Wilson | 9f792eb | 2016-11-14 20:41:04 +0000 | [diff] [blame] | 923 | engine->schedule(request, request->ctx->priority); |
Chris Wilson | 0de9136 | 2016-11-14 20:41:01 +0000 | [diff] [blame] | 924 | |
Chris Wilson | 5590af3 | 2016-09-09 14:11:54 +0100 | [diff] [blame] | 925 | local_bh_disable(); |
| 926 | i915_sw_fence_commit(&request->submit); |
| 927 | local_bh_enable(); /* Kick the execlists tasklet if just scheduled */ |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 928 | } |
| 929 | |
Chris Wilson | 221fe79 | 2016-09-09 14:11:51 +0100 | [diff] [blame] | 930 | static void reset_wait_queue(wait_queue_head_t *q, wait_queue_t *wait) |
| 931 | { |
| 932 | unsigned long flags; |
| 933 | |
| 934 | spin_lock_irqsave(&q->lock, flags); |
| 935 | if (list_empty(&wait->task_list)) |
| 936 | __add_wait_queue(q, wait); |
| 937 | spin_unlock_irqrestore(&q->lock, flags); |
| 938 | } |
| 939 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 940 | static unsigned long local_clock_us(unsigned int *cpu) |
| 941 | { |
| 942 | unsigned long t; |
| 943 | |
| 944 | /* Cheaply and approximately convert from nanoseconds to microseconds. |
| 945 | * The result and subsequent calculations are also defined in the same |
| 946 | * approximate microseconds units. The principal source of timing |
| 947 | * error here is from the simple truncation. |
| 948 | * |
| 949 | * Note that local_clock() is only defined wrt to the current CPU; |
| 950 | * the comparisons are no longer valid if we switch CPUs. Instead of |
| 951 | * blocking preemption for the entire busywait, we can detect the CPU |
| 952 | * switch and use that as indicator of system load and a reason to |
| 953 | * stop busywaiting, see busywait_stop(). |
| 954 | */ |
| 955 | *cpu = get_cpu(); |
| 956 | t = local_clock() >> 10; |
| 957 | put_cpu(); |
| 958 | |
| 959 | return t; |
| 960 | } |
| 961 | |
| 962 | static bool busywait_stop(unsigned long timeout, unsigned int cpu) |
| 963 | { |
| 964 | unsigned int this_cpu; |
| 965 | |
| 966 | if (time_after(local_clock_us(&this_cpu), timeout)) |
| 967 | return true; |
| 968 | |
| 969 | return this_cpu != cpu; |
| 970 | } |
| 971 | |
| 972 | bool __i915_spin_request(const struct drm_i915_gem_request *req, |
| 973 | int state, unsigned long timeout_us) |
| 974 | { |
Chris Wilson | c33ed06 | 2017-02-17 15:13:01 +0000 | [diff] [blame] | 975 | struct intel_engine_cs *engine = req->engine; |
| 976 | unsigned int irq, cpu; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 977 | |
| 978 | /* When waiting for high frequency requests, e.g. during synchronous |
| 979 | * rendering split between the CPU and GPU, the finite amount of time |
| 980 | * required to set up the irq and wait upon it limits the response |
| 981 | * rate. By busywaiting on the request completion for a short while we |
| 982 | * can service the high frequency waits as quick as possible. However, |
| 983 | * if it is a slow request, we want to sleep as quickly as possible. |
| 984 | * The tradeoff between waiting and sleeping is roughly the time it |
| 985 | * takes to sleep on a request, on the order of a microsecond. |
| 986 | */ |
| 987 | |
Chris Wilson | c33ed06 | 2017-02-17 15:13:01 +0000 | [diff] [blame] | 988 | irq = atomic_read(&engine->irq_count); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 989 | timeout_us += local_clock_us(&cpu); |
| 990 | do { |
Chris Wilson | 65e4760 | 2016-10-28 13:58:49 +0100 | [diff] [blame] | 991 | if (__i915_gem_request_completed(req)) |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 992 | return true; |
| 993 | |
Chris Wilson | c33ed06 | 2017-02-17 15:13:01 +0000 | [diff] [blame] | 994 | /* Seqno are meant to be ordered *before* the interrupt. If |
| 995 | * we see an interrupt without a corresponding seqno advance, |
| 996 | * assume we won't see one in the near future but require |
| 997 | * the engine->seqno_barrier() to fixup coherency. |
| 998 | */ |
| 999 | if (atomic_read(&engine->irq_count) != irq) |
| 1000 | break; |
| 1001 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1002 | if (signal_pending_state(state, current)) |
| 1003 | break; |
| 1004 | |
| 1005 | if (busywait_stop(timeout_us, cpu)) |
| 1006 | break; |
| 1007 | |
Christian Borntraeger | f2f09a4 | 2016-10-25 11:03:14 +0200 | [diff] [blame] | 1008 | cpu_relax(); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1009 | } while (!need_resched()); |
| 1010 | |
| 1011 | return false; |
| 1012 | } |
| 1013 | |
Chris Wilson | 4680816b | 2016-10-28 13:58:48 +0100 | [diff] [blame] | 1014 | static long |
Chris Wilson | 23902e4 | 2016-11-14 20:40:58 +0000 | [diff] [blame] | 1015 | __i915_request_wait_for_execute(struct drm_i915_gem_request *request, |
| 1016 | unsigned int flags, |
| 1017 | long timeout) |
Chris Wilson | 4680816b | 2016-10-28 13:58:48 +0100 | [diff] [blame] | 1018 | { |
| 1019 | const int state = flags & I915_WAIT_INTERRUPTIBLE ? |
| 1020 | TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE; |
| 1021 | wait_queue_head_t *q = &request->i915->gpu_error.wait_queue; |
| 1022 | DEFINE_WAIT(reset); |
| 1023 | DEFINE_WAIT(wait); |
| 1024 | |
| 1025 | if (flags & I915_WAIT_LOCKED) |
| 1026 | add_wait_queue(q, &reset); |
| 1027 | |
| 1028 | do { |
Chris Wilson | 23902e4 | 2016-11-14 20:40:58 +0000 | [diff] [blame] | 1029 | prepare_to_wait(&request->execute.wait, &wait, state); |
Chris Wilson | 4680816b | 2016-10-28 13:58:48 +0100 | [diff] [blame] | 1030 | |
Chris Wilson | 23902e4 | 2016-11-14 20:40:58 +0000 | [diff] [blame] | 1031 | if (i915_sw_fence_done(&request->execute)) |
Chris Wilson | 4680816b | 2016-10-28 13:58:48 +0100 | [diff] [blame] | 1032 | break; |
| 1033 | |
| 1034 | if (flags & I915_WAIT_LOCKED && |
| 1035 | i915_reset_in_progress(&request->i915->gpu_error)) { |
| 1036 | __set_current_state(TASK_RUNNING); |
| 1037 | i915_reset(request->i915); |
| 1038 | reset_wait_queue(q, &reset); |
| 1039 | continue; |
| 1040 | } |
| 1041 | |
| 1042 | if (signal_pending_state(state, current)) { |
| 1043 | timeout = -ERESTARTSYS; |
| 1044 | break; |
| 1045 | } |
| 1046 | |
Chris Wilson | 969bb72 | 2017-02-08 18:12:38 +0000 | [diff] [blame] | 1047 | if (!timeout) { |
| 1048 | timeout = -ETIME; |
| 1049 | break; |
| 1050 | } |
| 1051 | |
Chris Wilson | 4680816b | 2016-10-28 13:58:48 +0100 | [diff] [blame] | 1052 | timeout = io_schedule_timeout(timeout); |
Chris Wilson | 969bb72 | 2017-02-08 18:12:38 +0000 | [diff] [blame] | 1053 | } while (1); |
Chris Wilson | 23902e4 | 2016-11-14 20:40:58 +0000 | [diff] [blame] | 1054 | finish_wait(&request->execute.wait, &wait); |
Chris Wilson | 4680816b | 2016-10-28 13:58:48 +0100 | [diff] [blame] | 1055 | |
| 1056 | if (flags & I915_WAIT_LOCKED) |
| 1057 | remove_wait_queue(q, &reset); |
| 1058 | |
| 1059 | return timeout; |
| 1060 | } |
| 1061 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1062 | /** |
Chris Wilson | 776f323 | 2016-08-04 07:52:40 +0100 | [diff] [blame] | 1063 | * i915_wait_request - wait until execution of request has finished |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1064 | * @req: the request to wait upon |
Chris Wilson | ea746f3 | 2016-09-09 14:11:49 +0100 | [diff] [blame] | 1065 | * @flags: how to wait |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1066 | * @timeout: how long to wait in jiffies |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1067 | * |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1068 | * i915_wait_request() waits for the request to be completed, for a |
| 1069 | * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an |
| 1070 | * unbounded wait). |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1071 | * |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1072 | * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED |
| 1073 | * in via the flags, and vice versa if the struct_mutex is not held, the caller |
| 1074 | * must not specify that the wait is locked. |
| 1075 | * |
| 1076 | * Returns the remaining time (in jiffies) if the request completed, which may |
| 1077 | * be zero or -ETIME if the request is unfinished after the timeout expires. |
| 1078 | * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is |
| 1079 | * pending before the request completes. |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1080 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1081 | long i915_wait_request(struct drm_i915_gem_request *req, |
| 1082 | unsigned int flags, |
| 1083 | long timeout) |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1084 | { |
Chris Wilson | ea746f3 | 2016-09-09 14:11:49 +0100 | [diff] [blame] | 1085 | const int state = flags & I915_WAIT_INTERRUPTIBLE ? |
| 1086 | TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1087 | DEFINE_WAIT(reset); |
| 1088 | struct intel_wait wait; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1089 | |
| 1090 | might_sleep(); |
Chris Wilson | 22dd3bb | 2016-09-09 14:11:50 +0100 | [diff] [blame] | 1091 | #if IS_ENABLED(CONFIG_LOCKDEP) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1092 | GEM_BUG_ON(debug_locks && |
| 1093 | !!lockdep_is_held(&req->i915->drm.struct_mutex) != |
Chris Wilson | 22dd3bb | 2016-09-09 14:11:50 +0100 | [diff] [blame] | 1094 | !!(flags & I915_WAIT_LOCKED)); |
| 1095 | #endif |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1096 | GEM_BUG_ON(timeout < 0); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1097 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1098 | if (i915_gem_request_completed(req)) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1099 | return timeout; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1100 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1101 | if (!timeout) |
| 1102 | return -ETIME; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1103 | |
Tvrtko Ursulin | 9369250 | 2017-02-21 11:00:24 +0000 | [diff] [blame] | 1104 | trace_i915_gem_request_wait_begin(req, flags); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1105 | |
Chris Wilson | 23902e4 | 2016-11-14 20:40:58 +0000 | [diff] [blame] | 1106 | if (!i915_sw_fence_done(&req->execute)) { |
| 1107 | timeout = __i915_request_wait_for_execute(req, flags, timeout); |
Chris Wilson | 4680816b | 2016-10-28 13:58:48 +0100 | [diff] [blame] | 1108 | if (timeout < 0) |
| 1109 | goto complete; |
| 1110 | |
Chris Wilson | 23902e4 | 2016-11-14 20:40:58 +0000 | [diff] [blame] | 1111 | GEM_BUG_ON(!i915_sw_fence_done(&req->execute)); |
Chris Wilson | 4680816b | 2016-10-28 13:58:48 +0100 | [diff] [blame] | 1112 | } |
Chris Wilson | 23902e4 | 2016-11-14 20:40:58 +0000 | [diff] [blame] | 1113 | GEM_BUG_ON(!i915_sw_fence_done(&req->submit)); |
Chris Wilson | 65e4760 | 2016-10-28 13:58:49 +0100 | [diff] [blame] | 1114 | GEM_BUG_ON(!req->global_seqno); |
Chris Wilson | 4680816b | 2016-10-28 13:58:48 +0100 | [diff] [blame] | 1115 | |
Daniel Vetter | 437c308 | 2016-08-05 18:11:24 +0200 | [diff] [blame] | 1116 | /* Optimistic short spin before touching IRQs */ |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1117 | if (i915_spin_request(req, state, 5)) |
| 1118 | goto complete; |
| 1119 | |
| 1120 | set_current_state(state); |
Chris Wilson | 22dd3bb | 2016-09-09 14:11:50 +0100 | [diff] [blame] | 1121 | if (flags & I915_WAIT_LOCKED) |
| 1122 | add_wait_queue(&req->i915->gpu_error.wait_queue, &reset); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1123 | |
Chris Wilson | 65e4760 | 2016-10-28 13:58:49 +0100 | [diff] [blame] | 1124 | intel_wait_init(&wait, req->global_seqno); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1125 | if (intel_engine_add_wait(req->engine, &wait)) |
| 1126 | /* In order to check that we haven't missed the interrupt |
| 1127 | * as we enabled it, we need to kick ourselves to do a |
| 1128 | * coherent check on the seqno before we sleep. |
| 1129 | */ |
| 1130 | goto wakeup; |
| 1131 | |
| 1132 | for (;;) { |
| 1133 | if (signal_pending_state(state, current)) { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1134 | timeout = -ERESTARTSYS; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1135 | break; |
| 1136 | } |
| 1137 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1138 | if (!timeout) { |
| 1139 | timeout = -ETIME; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1140 | break; |
| 1141 | } |
| 1142 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1143 | timeout = io_schedule_timeout(timeout); |
| 1144 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1145 | if (intel_wait_complete(&wait)) |
| 1146 | break; |
| 1147 | |
| 1148 | set_current_state(state); |
| 1149 | |
| 1150 | wakeup: |
| 1151 | /* Carefully check if the request is complete, giving time |
| 1152 | * for the seqno to be visible following the interrupt. |
| 1153 | * We also have to check in case we are kicked by the GPU |
| 1154 | * reset in order to drop the struct_mutex. |
| 1155 | */ |
| 1156 | if (__i915_request_irq_complete(req)) |
| 1157 | break; |
| 1158 | |
Chris Wilson | 221fe79 | 2016-09-09 14:11:51 +0100 | [diff] [blame] | 1159 | /* If the GPU is hung, and we hold the lock, reset the GPU |
| 1160 | * and then check for completion. On a full reset, the engine's |
| 1161 | * HW seqno will be advanced passed us and we are complete. |
| 1162 | * If we do a partial reset, we have to wait for the GPU to |
| 1163 | * resume and update the breadcrumb. |
| 1164 | * |
| 1165 | * If we don't hold the mutex, we can just wait for the worker |
| 1166 | * to come along and update the breadcrumb (either directly |
| 1167 | * itself, or indirectly by recovering the GPU). |
| 1168 | */ |
| 1169 | if (flags & I915_WAIT_LOCKED && |
| 1170 | i915_reset_in_progress(&req->i915->gpu_error)) { |
| 1171 | __set_current_state(TASK_RUNNING); |
| 1172 | i915_reset(req->i915); |
| 1173 | reset_wait_queue(&req->i915->gpu_error.wait_queue, |
| 1174 | &reset); |
| 1175 | continue; |
| 1176 | } |
| 1177 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1178 | /* Only spin if we know the GPU is processing this request */ |
| 1179 | if (i915_spin_request(req, state, 2)) |
| 1180 | break; |
| 1181 | } |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1182 | |
| 1183 | intel_engine_remove_wait(req->engine, &wait); |
Chris Wilson | 22dd3bb | 2016-09-09 14:11:50 +0100 | [diff] [blame] | 1184 | if (flags & I915_WAIT_LOCKED) |
| 1185 | remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset); |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1186 | __set_current_state(TASK_RUNNING); |
Chris Wilson | 22dd3bb | 2016-09-09 14:11:50 +0100 | [diff] [blame] | 1187 | |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1188 | complete: |
| 1189 | trace_i915_gem_request_wait_end(req); |
| 1190 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1191 | return timeout; |
Chris Wilson | 05235c5 | 2016-07-20 09:21:08 +0100 | [diff] [blame] | 1192 | } |
Chris Wilson | 4b8de8e | 2016-08-04 07:52:42 +0100 | [diff] [blame] | 1193 | |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 1194 | static void engine_retire_requests(struct intel_engine_cs *engine) |
Chris Wilson | 4b8de8e | 2016-08-04 07:52:42 +0100 | [diff] [blame] | 1195 | { |
| 1196 | struct drm_i915_gem_request *request, *next; |
| 1197 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 1198 | list_for_each_entry_safe(request, next, |
| 1199 | &engine->timeline->requests, link) { |
Chris Wilson | 80b204b | 2016-10-28 13:58:58 +0100 | [diff] [blame] | 1200 | if (!__i915_gem_request_completed(request)) |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 1201 | return; |
Chris Wilson | 4b8de8e | 2016-08-04 07:52:42 +0100 | [diff] [blame] | 1202 | |
| 1203 | i915_gem_request_retire(request); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | void i915_gem_retire_requests(struct drm_i915_private *dev_priv) |
| 1208 | { |
| 1209 | struct intel_engine_cs *engine; |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 1210 | enum intel_engine_id id; |
Chris Wilson | 4b8de8e | 2016-08-04 07:52:42 +0100 | [diff] [blame] | 1211 | |
| 1212 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
| 1213 | |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 1214 | if (!dev_priv->gt.active_requests) |
Chris Wilson | 4b8de8e | 2016-08-04 07:52:42 +0100 | [diff] [blame] | 1215 | return; |
| 1216 | |
Chris Wilson | 28176ef | 2016-10-28 13:58:56 +0100 | [diff] [blame] | 1217 | for_each_engine(engine, dev_priv, id) |
| 1218 | engine_retire_requests(engine); |
Chris Wilson | 4b8de8e | 2016-08-04 07:52:42 +0100 | [diff] [blame] | 1219 | } |
Chris Wilson | c835c55 | 2017-02-13 17:15:21 +0000 | [diff] [blame] | 1220 | |
| 1221 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 1222 | #include "selftests/mock_request.c" |
| 1223 | #include "selftests/i915_gem_request.c" |
| 1224 | #endif |