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