Commit | Line | Data |
---|---|---|
05235c53 CW |
1 | /* |
2 | * Copyright © 2008-2015 Intel Corporation | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a | |
5 | * copy of this software and associated documentation files (the "Software"), | |
6 | * to deal in the Software without restriction, including without limitation | |
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
8 | * and/or sell copies of the Software, and to permit persons to whom the | |
9 | * Software is furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice (including the next | |
12 | * paragraph) shall be included in all copies or substantial portions of the | |
13 | * Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
21 | * IN THE SOFTWARE. | |
22 | * | |
23 | */ | |
24 | ||
25 | #include "i915_drv.h" | |
26 | ||
04769652 CW |
27 | static const char *i915_fence_get_driver_name(struct fence *fence) |
28 | { | |
29 | return "i915"; | |
30 | } | |
31 | ||
32 | static const char *i915_fence_get_timeline_name(struct fence *fence) | |
33 | { | |
34 | /* Timelines are bound by eviction to a VM. However, since | |
35 | * we only have a global seqno at the moment, we only have | |
36 | * a single timeline. Note that each timeline will have | |
37 | * multiple execution contexts (fence contexts) as we allow | |
38 | * engines within a single timeline to execute in parallel. | |
39 | */ | |
40 | return "global"; | |
41 | } | |
42 | ||
43 | static bool i915_fence_signaled(struct fence *fence) | |
44 | { | |
45 | return i915_gem_request_completed(to_request(fence)); | |
46 | } | |
47 | ||
48 | static bool i915_fence_enable_signaling(struct fence *fence) | |
49 | { | |
50 | if (i915_fence_signaled(fence)) | |
51 | return false; | |
52 | ||
53 | intel_engine_enable_signaling(to_request(fence)); | |
54 | return true; | |
55 | } | |
56 | ||
57 | static signed long i915_fence_wait(struct fence *fence, | |
58 | bool interruptible, | |
59 | signed long timeout_jiffies) | |
60 | { | |
61 | s64 timeout_ns, *timeout; | |
62 | int ret; | |
63 | ||
64 | if (timeout_jiffies != MAX_SCHEDULE_TIMEOUT) { | |
65 | timeout_ns = jiffies_to_nsecs(timeout_jiffies); | |
66 | timeout = &timeout_ns; | |
67 | } else { | |
68 | timeout = NULL; | |
69 | } | |
70 | ||
71 | ret = __i915_wait_request(to_request(fence), | |
72 | interruptible, timeout, | |
42df2714 | 73 | NO_WAITBOOST); |
04769652 CW |
74 | if (ret == -ETIME) |
75 | return 0; | |
76 | ||
77 | if (ret < 0) | |
78 | return ret; | |
79 | ||
80 | if (timeout_jiffies != MAX_SCHEDULE_TIMEOUT) | |
81 | timeout_jiffies = nsecs_to_jiffies(timeout_ns); | |
82 | ||
83 | return timeout_jiffies; | |
84 | } | |
85 | ||
86 | static void i915_fence_value_str(struct fence *fence, char *str, int size) | |
87 | { | |
88 | snprintf(str, size, "%u", fence->seqno); | |
89 | } | |
90 | ||
91 | static void i915_fence_timeline_value_str(struct fence *fence, char *str, | |
92 | int size) | |
93 | { | |
94 | snprintf(str, size, "%u", | |
95 | intel_engine_get_seqno(to_request(fence)->engine)); | |
96 | } | |
97 | ||
98 | static void i915_fence_release(struct fence *fence) | |
99 | { | |
100 | struct drm_i915_gem_request *req = to_request(fence); | |
101 | ||
102 | kmem_cache_free(req->i915->requests, req); | |
103 | } | |
104 | ||
105 | const struct fence_ops i915_fence_ops = { | |
106 | .get_driver_name = i915_fence_get_driver_name, | |
107 | .get_timeline_name = i915_fence_get_timeline_name, | |
108 | .enable_signaling = i915_fence_enable_signaling, | |
109 | .signaled = i915_fence_signaled, | |
110 | .wait = i915_fence_wait, | |
111 | .release = i915_fence_release, | |
112 | .fence_value_str = i915_fence_value_str, | |
113 | .timeline_value_str = i915_fence_timeline_value_str, | |
114 | }; | |
115 | ||
05235c53 CW |
116 | int i915_gem_request_add_to_client(struct drm_i915_gem_request *req, |
117 | struct drm_file *file) | |
118 | { | |
119 | struct drm_i915_private *dev_private; | |
120 | struct drm_i915_file_private *file_priv; | |
121 | ||
122 | WARN_ON(!req || !file || req->file_priv); | |
123 | ||
124 | if (!req || !file) | |
125 | return -EINVAL; | |
126 | ||
127 | if (req->file_priv) | |
128 | return -EINVAL; | |
129 | ||
130 | dev_private = req->i915; | |
131 | file_priv = file->driver_priv; | |
132 | ||
133 | spin_lock(&file_priv->mm.lock); | |
134 | req->file_priv = file_priv; | |
135 | list_add_tail(&req->client_list, &file_priv->mm.request_list); | |
136 | spin_unlock(&file_priv->mm.lock); | |
137 | ||
138 | req->pid = get_pid(task_pid(current)); | |
139 | ||
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); | |
155 | ||
156 | put_pid(request->pid); | |
157 | request->pid = NULL; | |
158 | } | |
159 | ||
160 | static void i915_gem_request_retire(struct drm_i915_gem_request *request) | |
161 | { | |
162 | trace_i915_gem_request_retire(request); | |
163 | list_del_init(&request->list); | |
164 | ||
165 | /* We know the GPU must have read the request to have | |
166 | * sent us the seqno + interrupt, so use the position | |
167 | * of tail of the request to update the last known position | |
168 | * of the GPU head. | |
169 | * | |
170 | * Note this requires that we are always called in request | |
171 | * completion order. | |
172 | */ | |
1dae2dfb | 173 | request->ring->last_retired_head = request->postfix; |
05235c53 CW |
174 | |
175 | i915_gem_request_remove_from_client(request); | |
176 | ||
177 | if (request->previous_context) { | |
178 | if (i915.enable_execlists) | |
179 | intel_lr_context_unpin(request->previous_context, | |
180 | request->engine); | |
181 | } | |
182 | ||
9a6feaf0 | 183 | i915_gem_context_put(request->ctx); |
e8a261ea | 184 | i915_gem_request_put(request); |
05235c53 CW |
185 | } |
186 | ||
187 | void i915_gem_request_retire_upto(struct drm_i915_gem_request *req) | |
188 | { | |
189 | struct intel_engine_cs *engine = req->engine; | |
190 | struct drm_i915_gem_request *tmp; | |
191 | ||
192 | lockdep_assert_held(&req->i915->drm.struct_mutex); | |
193 | ||
194 | if (list_empty(&req->list)) | |
195 | return; | |
196 | ||
197 | do { | |
198 | tmp = list_first_entry(&engine->request_list, | |
199 | typeof(*tmp), list); | |
200 | ||
201 | i915_gem_request_retire(tmp); | |
202 | } while (tmp != req); | |
203 | ||
204 | WARN_ON(i915_verify_lists(engine->dev)); | |
205 | } | |
206 | ||
207 | static int i915_gem_check_wedge(unsigned int reset_counter, bool interruptible) | |
208 | { | |
209 | if (__i915_terminally_wedged(reset_counter)) | |
210 | return -EIO; | |
211 | ||
212 | if (__i915_reset_in_progress(reset_counter)) { | |
213 | /* Non-interruptible callers can't handle -EAGAIN, hence return | |
214 | * -EIO unconditionally for these. | |
215 | */ | |
216 | if (!interruptible) | |
217 | return -EIO; | |
218 | ||
219 | return -EAGAIN; | |
220 | } | |
221 | ||
222 | return 0; | |
223 | } | |
224 | ||
225 | static int i915_gem_init_seqno(struct drm_i915_private *dev_priv, u32 seqno) | |
226 | { | |
227 | struct intel_engine_cs *engine; | |
228 | int ret; | |
229 | ||
230 | /* Carefully retire all requests without writing to the rings */ | |
231 | for_each_engine(engine, dev_priv) { | |
232 | ret = intel_engine_idle(engine); | |
233 | if (ret) | |
234 | return ret; | |
235 | } | |
236 | i915_gem_retire_requests(dev_priv); | |
237 | ||
238 | /* If the seqno wraps around, we need to clear the breadcrumb rbtree */ | |
239 | if (!i915_seqno_passed(seqno, dev_priv->next_seqno)) { | |
240 | while (intel_kick_waiters(dev_priv) || | |
241 | intel_kick_signalers(dev_priv)) | |
242 | yield(); | |
243 | } | |
244 | ||
245 | /* Finally reset hw state */ | |
246 | for_each_engine(engine, dev_priv) | |
7e37f889 | 247 | intel_engine_init_seqno(engine, seqno); |
05235c53 CW |
248 | |
249 | return 0; | |
250 | } | |
251 | ||
252 | int i915_gem_set_seqno(struct drm_device *dev, u32 seqno) | |
253 | { | |
254 | struct drm_i915_private *dev_priv = to_i915(dev); | |
255 | int ret; | |
256 | ||
257 | if (seqno == 0) | |
258 | return -EINVAL; | |
259 | ||
260 | /* HWS page needs to be set less than what we | |
261 | * will inject to ring | |
262 | */ | |
263 | ret = i915_gem_init_seqno(dev_priv, seqno - 1); | |
264 | if (ret) | |
265 | return ret; | |
266 | ||
267 | /* Carefully set the last_seqno value so that wrap | |
268 | * detection still works | |
269 | */ | |
270 | dev_priv->next_seqno = seqno; | |
271 | dev_priv->last_seqno = seqno - 1; | |
272 | if (dev_priv->last_seqno == 0) | |
273 | dev_priv->last_seqno--; | |
274 | ||
275 | return 0; | |
276 | } | |
277 | ||
278 | static int i915_gem_get_seqno(struct drm_i915_private *dev_priv, u32 *seqno) | |
279 | { | |
280 | /* reserve 0 for non-seqno */ | |
281 | if (unlikely(dev_priv->next_seqno == 0)) { | |
282 | int ret; | |
283 | ||
284 | ret = i915_gem_init_seqno(dev_priv, 0); | |
285 | if (ret) | |
286 | return ret; | |
287 | ||
288 | dev_priv->next_seqno = 1; | |
289 | } | |
290 | ||
291 | *seqno = dev_priv->last_seqno = dev_priv->next_seqno++; | |
292 | return 0; | |
293 | } | |
294 | ||
8e637178 CW |
295 | /** |
296 | * i915_gem_request_alloc - allocate a request structure | |
297 | * | |
298 | * @engine: engine that we wish to issue the request on. | |
299 | * @ctx: context that the request will be associated with. | |
300 | * This can be NULL if the request is not directly related to | |
301 | * any specific user context, in which case this function will | |
302 | * choose an appropriate context to use. | |
303 | * | |
304 | * Returns a pointer to the allocated request if successful, | |
305 | * or an error code if not. | |
306 | */ | |
307 | struct drm_i915_gem_request * | |
308 | i915_gem_request_alloc(struct intel_engine_cs *engine, | |
309 | struct i915_gem_context *ctx) | |
05235c53 CW |
310 | { |
311 | struct drm_i915_private *dev_priv = engine->i915; | |
312 | unsigned int reset_counter = i915_reset_counter(&dev_priv->gpu_error); | |
313 | struct drm_i915_gem_request *req; | |
04769652 | 314 | u32 seqno; |
05235c53 CW |
315 | int ret; |
316 | ||
05235c53 CW |
317 | /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report |
318 | * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex | |
319 | * and restart. | |
320 | */ | |
321 | ret = i915_gem_check_wedge(reset_counter, dev_priv->mm.interruptible); | |
322 | if (ret) | |
8e637178 | 323 | return ERR_PTR(ret); |
05235c53 | 324 | |
9b5f4e5e | 325 | /* Move the oldest request to the slab-cache (if not in use!) */ |
2a1d7752 | 326 | req = list_first_entry_or_null(&engine->request_list, |
9b5f4e5e | 327 | typeof(*req), list); |
2a1d7752 CW |
328 | if (req && i915_gem_request_completed(req)) |
329 | i915_gem_request_retire(req); | |
9b5f4e5e | 330 | |
05235c53 CW |
331 | req = kmem_cache_zalloc(dev_priv->requests, GFP_KERNEL); |
332 | if (!req) | |
8e637178 | 333 | return ERR_PTR(-ENOMEM); |
05235c53 | 334 | |
04769652 | 335 | ret = i915_gem_get_seqno(dev_priv, &seqno); |
05235c53 CW |
336 | if (ret) |
337 | goto err; | |
338 | ||
04769652 CW |
339 | spin_lock_init(&req->lock); |
340 | fence_init(&req->fence, | |
341 | &i915_fence_ops, | |
342 | &req->lock, | |
343 | engine->fence_context, | |
344 | seqno); | |
345 | ||
05235c53 CW |
346 | req->i915 = dev_priv; |
347 | req->engine = engine; | |
9a6feaf0 | 348 | req->ctx = i915_gem_context_get(ctx); |
05235c53 CW |
349 | |
350 | /* | |
351 | * Reserve space in the ring buffer for all the commands required to | |
352 | * eventually emit this request. This is to guarantee that the | |
353 | * i915_add_request() call can't fail. Note that the reserve may need | |
354 | * to be redone if the request is not actually submitted straight | |
355 | * away, e.g. because a GPU scheduler has deferred it. | |
356 | */ | |
357 | req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST; | |
358 | ||
359 | if (i915.enable_execlists) | |
360 | ret = intel_logical_ring_alloc_request_extras(req); | |
361 | else | |
362 | ret = intel_ring_alloc_request_extras(req); | |
363 | if (ret) | |
364 | goto err_ctx; | |
365 | ||
8e637178 | 366 | return req; |
05235c53 CW |
367 | |
368 | err_ctx: | |
9a6feaf0 | 369 | i915_gem_context_put(ctx); |
05235c53 CW |
370 | err: |
371 | kmem_cache_free(dev_priv->requests, req); | |
8e637178 | 372 | return ERR_PTR(ret); |
05235c53 CW |
373 | } |
374 | ||
375 | static void i915_gem_mark_busy(const struct intel_engine_cs *engine) | |
376 | { | |
377 | struct drm_i915_private *dev_priv = engine->i915; | |
378 | ||
379 | dev_priv->gt.active_engines |= intel_engine_flag(engine); | |
380 | if (dev_priv->gt.awake) | |
381 | return; | |
382 | ||
383 | intel_runtime_pm_get_noresume(dev_priv); | |
384 | dev_priv->gt.awake = true; | |
385 | ||
54b4f68f | 386 | intel_enable_gt_powersave(dev_priv); |
05235c53 CW |
387 | i915_update_gfx_val(dev_priv); |
388 | if (INTEL_GEN(dev_priv) >= 6) | |
389 | gen6_rps_busy(dev_priv); | |
390 | ||
391 | queue_delayed_work(dev_priv->wq, | |
392 | &dev_priv->gt.retire_work, | |
393 | round_jiffies_up_relative(HZ)); | |
394 | } | |
395 | ||
396 | /* | |
397 | * NB: This function is not allowed to fail. Doing so would mean the the | |
398 | * request is not being tracked for completion but the work itself is | |
399 | * going to happen on the hardware. This would be a Bad Thing(tm). | |
400 | */ | |
401 | void __i915_add_request(struct drm_i915_gem_request *request, | |
402 | struct drm_i915_gem_object *obj, | |
403 | bool flush_caches) | |
404 | { | |
405 | struct intel_engine_cs *engine; | |
7e37f889 | 406 | struct intel_ring *ring; |
05235c53 CW |
407 | u32 request_start; |
408 | u32 reserved_tail; | |
409 | int ret; | |
410 | ||
411 | if (WARN_ON(!request)) | |
412 | return; | |
413 | ||
414 | engine = request->engine; | |
1dae2dfb | 415 | ring = request->ring; |
05235c53 CW |
416 | |
417 | /* | |
418 | * To ensure that this call will not fail, space for its emissions | |
419 | * should already have been reserved in the ring buffer. Let the ring | |
420 | * know that it is time to use that space up. | |
421 | */ | |
1dae2dfb | 422 | request_start = intel_ring_get_tail(ring); |
05235c53 CW |
423 | reserved_tail = request->reserved_space; |
424 | request->reserved_space = 0; | |
425 | ||
426 | /* | |
427 | * Emit any outstanding flushes - execbuf can fail to emit the flush | |
428 | * after having emitted the batchbuffer command. Hence we need to fix | |
429 | * things up similar to emitting the lazy request. The difference here | |
430 | * is that the flush _must_ happen before the next request, no matter | |
431 | * what. | |
432 | */ | |
433 | if (flush_caches) { | |
7c9cf4e3 | 434 | ret = engine->emit_flush(request, EMIT_FLUSH); |
c7fe7d25 | 435 | |
05235c53 | 436 | /* Not allowed to fail! */ |
c7fe7d25 | 437 | WARN(ret, "engine->emit_flush() failed: %d!\n", ret); |
05235c53 CW |
438 | } |
439 | ||
440 | trace_i915_gem_request_add(request); | |
441 | ||
442 | request->head = request_start; | |
443 | ||
444 | /* Whilst this request exists, batch_obj will be on the | |
445 | * active_list, and so will hold the active reference. Only when this | |
446 | * request is retired will the the batch_obj be moved onto the | |
447 | * inactive_list and lose its active reference. Hence we do not need | |
448 | * to explicitly hold another reference here. | |
449 | */ | |
450 | request->batch_obj = obj; | |
451 | ||
452 | /* Seal the request and mark it as pending execution. Note that | |
453 | * we may inspect this state, without holding any locks, during | |
454 | * hangcheck. Hence we apply the barrier to ensure that we do not | |
455 | * see a more recent value in the hws than we are tracking. | |
456 | */ | |
457 | request->emitted_jiffies = jiffies; | |
458 | request->previous_seqno = engine->last_submitted_seqno; | |
04769652 | 459 | smp_store_mb(engine->last_submitted_seqno, request->fence.seqno); |
05235c53 CW |
460 | list_add_tail(&request->list, &engine->request_list); |
461 | ||
462 | /* Record the position of the start of the request so that | |
463 | * should we detect the updated seqno part-way through the | |
464 | * GPU processing the request, we never over-estimate the | |
465 | * position of the head. | |
466 | */ | |
1dae2dfb | 467 | request->postfix = intel_ring_get_tail(ring); |
05235c53 CW |
468 | |
469 | if (i915.enable_execlists) { | |
470 | ret = engine->emit_request(request); | |
471 | } else { | |
472 | ret = engine->add_request(request); | |
473 | ||
1dae2dfb | 474 | request->tail = intel_ring_get_tail(ring); |
05235c53 CW |
475 | } |
476 | /* Not allowed to fail! */ | |
477 | WARN(ret, "emit|add_request failed: %d!\n", ret); | |
478 | /* Sanity check that the reserved size was large enough. */ | |
1dae2dfb | 479 | ret = intel_ring_get_tail(ring) - request_start; |
05235c53 | 480 | if (ret < 0) |
1dae2dfb | 481 | ret += ring->size; |
05235c53 CW |
482 | WARN_ONCE(ret > reserved_tail, |
483 | "Not enough space reserved (%d bytes) " | |
484 | "for adding the request (%d bytes)\n", | |
485 | reserved_tail, ret); | |
486 | ||
487 | i915_gem_mark_busy(engine); | |
488 | } | |
489 | ||
490 | static unsigned long local_clock_us(unsigned int *cpu) | |
491 | { | |
492 | unsigned long t; | |
493 | ||
494 | /* Cheaply and approximately convert from nanoseconds to microseconds. | |
495 | * The result and subsequent calculations are also defined in the same | |
496 | * approximate microseconds units. The principal source of timing | |
497 | * error here is from the simple truncation. | |
498 | * | |
499 | * Note that local_clock() is only defined wrt to the current CPU; | |
500 | * the comparisons are no longer valid if we switch CPUs. Instead of | |
501 | * blocking preemption for the entire busywait, we can detect the CPU | |
502 | * switch and use that as indicator of system load and a reason to | |
503 | * stop busywaiting, see busywait_stop(). | |
504 | */ | |
505 | *cpu = get_cpu(); | |
506 | t = local_clock() >> 10; | |
507 | put_cpu(); | |
508 | ||
509 | return t; | |
510 | } | |
511 | ||
512 | static bool busywait_stop(unsigned long timeout, unsigned int cpu) | |
513 | { | |
514 | unsigned int this_cpu; | |
515 | ||
516 | if (time_after(local_clock_us(&this_cpu), timeout)) | |
517 | return true; | |
518 | ||
519 | return this_cpu != cpu; | |
520 | } | |
521 | ||
522 | bool __i915_spin_request(const struct drm_i915_gem_request *req, | |
523 | int state, unsigned long timeout_us) | |
524 | { | |
525 | unsigned int cpu; | |
526 | ||
527 | /* When waiting for high frequency requests, e.g. during synchronous | |
528 | * rendering split between the CPU and GPU, the finite amount of time | |
529 | * required to set up the irq and wait upon it limits the response | |
530 | * rate. By busywaiting on the request completion for a short while we | |
531 | * can service the high frequency waits as quick as possible. However, | |
532 | * if it is a slow request, we want to sleep as quickly as possible. | |
533 | * The tradeoff between waiting and sleeping is roughly the time it | |
534 | * takes to sleep on a request, on the order of a microsecond. | |
535 | */ | |
536 | ||
537 | timeout_us += local_clock_us(&cpu); | |
538 | do { | |
539 | if (i915_gem_request_completed(req)) | |
540 | return true; | |
541 | ||
542 | if (signal_pending_state(state, current)) | |
543 | break; | |
544 | ||
545 | if (busywait_stop(timeout_us, cpu)) | |
546 | break; | |
547 | ||
548 | cpu_relax_lowlatency(); | |
549 | } while (!need_resched()); | |
550 | ||
551 | return false; | |
552 | } | |
553 | ||
554 | /** | |
555 | * __i915_wait_request - wait until execution of request has finished | |
556 | * @req: duh! | |
557 | * @interruptible: do an interruptible wait (normally yes) | |
558 | * @timeout: in - how long to wait (NULL forever); out - how much time remaining | |
559 | * @rps: client to charge for RPS boosting | |
560 | * | |
561 | * Note: It is of utmost importance that the passed in seqno and reset_counter | |
562 | * values have been read by the caller in an smp safe manner. Where read-side | |
563 | * locks are involved, it is sufficient to read the reset_counter before | |
564 | * unlocking the lock that protects the seqno. For lockless tricks, the | |
565 | * reset_counter _must_ be read before, and an appropriate smp_rmb must be | |
566 | * inserted. | |
567 | * | |
568 | * Returns 0 if the request was found within the alloted time. Else returns the | |
569 | * errno with remaining time filled in timeout argument. | |
570 | */ | |
571 | int __i915_wait_request(struct drm_i915_gem_request *req, | |
572 | bool interruptible, | |
573 | s64 *timeout, | |
574 | struct intel_rps_client *rps) | |
575 | { | |
576 | int state = interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE; | |
577 | DEFINE_WAIT(reset); | |
578 | struct intel_wait wait; | |
579 | unsigned long timeout_remain; | |
580 | int ret = 0; | |
581 | ||
582 | might_sleep(); | |
583 | ||
584 | if (list_empty(&req->list)) | |
585 | return 0; | |
586 | ||
587 | if (i915_gem_request_completed(req)) | |
588 | return 0; | |
589 | ||
590 | timeout_remain = MAX_SCHEDULE_TIMEOUT; | |
591 | if (timeout) { | |
592 | if (WARN_ON(*timeout < 0)) | |
593 | return -EINVAL; | |
594 | ||
595 | if (*timeout == 0) | |
596 | return -ETIME; | |
597 | ||
598 | /* Record current time in case interrupted, or wedged */ | |
599 | timeout_remain = nsecs_to_jiffies_timeout(*timeout); | |
600 | *timeout += ktime_get_raw_ns(); | |
601 | } | |
602 | ||
603 | trace_i915_gem_request_wait_begin(req); | |
604 | ||
605 | /* This client is about to stall waiting for the GPU. In many cases | |
606 | * this is undesirable and limits the throughput of the system, as | |
607 | * many clients cannot continue processing user input/output whilst | |
608 | * blocked. RPS autotuning may take tens of milliseconds to respond | |
609 | * to the GPU load and thus incurs additional latency for the client. | |
610 | * We can circumvent that by promoting the GPU frequency to maximum | |
611 | * before we wait. This makes the GPU throttle up much more quickly | |
612 | * (good for benchmarks and user experience, e.g. window animations), | |
613 | * but at a cost of spending more power processing the workload | |
614 | * (bad for battery). Not all clients even want their results | |
615 | * immediately and for them we should just let the GPU select its own | |
616 | * frequency to maximise efficiency. To prevent a single client from | |
617 | * forcing the clocks too high for the whole system, we only allow | |
618 | * each client to waitboost once in a busy period. | |
619 | */ | |
42df2714 | 620 | if (IS_RPS_CLIENT(rps) && INTEL_GEN(req->i915) >= 6) |
05235c53 CW |
621 | gen6_rps_boost(req->i915, rps, req->emitted_jiffies); |
622 | ||
623 | /* Optimistic spin for the next ~jiffie before touching IRQs */ | |
624 | if (i915_spin_request(req, state, 5)) | |
625 | goto complete; | |
626 | ||
627 | set_current_state(state); | |
628 | add_wait_queue(&req->i915->gpu_error.wait_queue, &reset); | |
629 | ||
04769652 | 630 | intel_wait_init(&wait, req->fence.seqno); |
05235c53 CW |
631 | if (intel_engine_add_wait(req->engine, &wait)) |
632 | /* In order to check that we haven't missed the interrupt | |
633 | * as we enabled it, we need to kick ourselves to do a | |
634 | * coherent check on the seqno before we sleep. | |
635 | */ | |
636 | goto wakeup; | |
637 | ||
638 | for (;;) { | |
639 | if (signal_pending_state(state, current)) { | |
640 | ret = -ERESTARTSYS; | |
641 | break; | |
642 | } | |
643 | ||
644 | timeout_remain = io_schedule_timeout(timeout_remain); | |
645 | if (timeout_remain == 0) { | |
646 | ret = -ETIME; | |
647 | break; | |
648 | } | |
649 | ||
650 | if (intel_wait_complete(&wait)) | |
651 | break; | |
652 | ||
653 | set_current_state(state); | |
654 | ||
655 | wakeup: | |
656 | /* Carefully check if the request is complete, giving time | |
657 | * for the seqno to be visible following the interrupt. | |
658 | * We also have to check in case we are kicked by the GPU | |
659 | * reset in order to drop the struct_mutex. | |
660 | */ | |
661 | if (__i915_request_irq_complete(req)) | |
662 | break; | |
663 | ||
664 | /* Only spin if we know the GPU is processing this request */ | |
665 | if (i915_spin_request(req, state, 2)) | |
666 | break; | |
667 | } | |
668 | remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset); | |
669 | ||
670 | intel_engine_remove_wait(req->engine, &wait); | |
671 | __set_current_state(TASK_RUNNING); | |
672 | complete: | |
673 | trace_i915_gem_request_wait_end(req); | |
674 | ||
675 | if (timeout) { | |
676 | *timeout -= ktime_get_raw_ns(); | |
677 | if (*timeout < 0) | |
678 | *timeout = 0; | |
679 | ||
680 | /* | |
681 | * Apparently ktime isn't accurate enough and occasionally has a | |
682 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch | |
683 | * things up to make the test happy. We allow up to 1 jiffy. | |
684 | * | |
685 | * This is a regrssion from the timespec->ktime conversion. | |
686 | */ | |
687 | if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000) | |
688 | *timeout = 0; | |
689 | } | |
690 | ||
42df2714 CW |
691 | if (IS_RPS_USER(rps) && |
692 | req->fence.seqno == req->engine->last_submitted_seqno) { | |
05235c53 CW |
693 | /* The GPU is now idle and this client has stalled. |
694 | * Since no other client has submitted a request in the | |
695 | * meantime, assume that this client is the only one | |
696 | * supplying work to the GPU but is unable to keep that | |
697 | * work supplied because it is waiting. Since the GPU is | |
698 | * then never kept fully busy, RPS autoclocking will | |
699 | * keep the clocks relatively low, causing further delays. | |
700 | * Compensate by giving the synchronous client credit for | |
701 | * a waitboost next time. | |
702 | */ | |
703 | spin_lock(&req->i915->rps.client_lock); | |
704 | list_del_init(&rps->link); | |
705 | spin_unlock(&req->i915->rps.client_lock); | |
706 | } | |
707 | ||
708 | return ret; | |
709 | } | |
710 | ||
711 | /** | |
712 | * Waits for a request to be signaled, and cleans up the | |
713 | * request and object lists appropriately for that event. | |
714 | */ | |
715 | int i915_wait_request(struct drm_i915_gem_request *req) | |
716 | { | |
717 | int ret; | |
718 | ||
719 | GEM_BUG_ON(!req); | |
720 | lockdep_assert_held(&req->i915->drm.struct_mutex); | |
721 | ||
722 | ret = __i915_wait_request(req, req->i915->mm.interruptible, NULL, NULL); | |
723 | if (ret) | |
724 | return ret; | |
725 | ||
726 | /* If the GPU hung, we want to keep the requests to find the guilty. */ | |
727 | if (!i915_reset_in_progress(&req->i915->gpu_error)) | |
728 | i915_gem_request_retire_upto(req); | |
729 | ||
730 | return 0; | |
731 | } |