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