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