2 * SPDX-License-Identifier: MIT
4 * Copyright © 2014-2016 Intel Corporation
7 #include <linux/jiffies.h>
9 #include <drm/drm_file.h>
12 #include "i915_file_private.h"
13 #include "i915_gem_context.h"
14 #include "i915_gem_ioctls.h"
15 #include "i915_gem_object.h"
18 * 20ms is a fairly arbitrary limit (greater than the average frame time)
19 * chosen to prevent the CPU getting more than a frame ahead of the GPU
20 * (when using lax throttling for the frontbuffer). We also use it to
21 * offer free GPU waitboosts for severely congested workloads.
23 #define DRM_I915_THROTTLE_JIFFIES msecs_to_jiffies(20)
26 * Throttle our rendering by waiting until the ring has completed our requests
27 * emitted over 20 msec ago.
29 * Note that if we were to use the current jiffies each time around the loop,
30 * we wouldn't escape the function with any frames outstanding if the time to
31 * render a frame was over 20ms.
33 * This should get us reasonable parallelism between CPU and GPU but also
34 * relatively low latency when blocking on a particular request to finish.
37 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
38 struct drm_file *file)
40 const unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
41 struct drm_i915_file_private *file_priv = file->driver_priv;
42 struct drm_i915_private *i915 = to_i915(dev);
43 struct i915_gem_context *ctx;
47 /* ABI: return -EIO if already wedged */
48 ret = intel_gt_terminally_wedged(to_gt(i915));
53 xa_for_each(&file_priv->context_xa, idx, ctx) {
54 struct i915_gem_engines_iter it;
55 struct intel_context *ce;
57 if (!kref_get_unless_zero(&ctx->ref))
61 for_each_gem_engine(ce,
62 i915_gem_context_lock_engines(ctx),
64 struct i915_request *rq, *target = NULL;
69 mutex_lock(&ce->timeline->mutex);
70 list_for_each_entry_reverse(rq,
71 &ce->timeline->requests,
73 if (i915_request_completed(rq))
76 if (time_after(rq->emitted_jiffies,
80 target = i915_request_get(rq);
83 mutex_unlock(&ce->timeline->mutex);
87 ret = i915_request_wait(target,
88 I915_WAIT_INTERRUPTIBLE,
89 MAX_SCHEDULE_TIMEOUT);
90 i915_request_put(target);
94 i915_gem_context_unlock_engines(ctx);
95 i915_gem_context_put(ctx);
101 return ret < 0 ? ret : 0;