2 * SPDX-License-Identifier: MIT
4 * Copyright © 2016 Intel Corporation
7 #include <linux/dma-fence-array.h>
8 #include <linux/jiffies.h>
10 #include "gt/intel_engine.h"
12 #include "i915_gem_ioctls.h"
13 #include "i915_gem_object.h"
16 i915_gem_object_wait_fence(struct dma_fence *fence,
20 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
22 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
25 if (dma_fence_is_i915(fence))
26 return i915_request_wait(to_request(fence), flags, timeout);
28 return dma_fence_wait_timeout(fence,
29 flags & I915_WAIT_INTERRUPTIBLE,
34 i915_gem_object_wait_reservation(struct reservation_object *resv,
38 unsigned int seq = __read_seqcount_begin(&resv->seq);
39 struct dma_fence *excl;
40 bool prune_fences = false;
42 if (flags & I915_WAIT_ALL) {
43 struct dma_fence **shared;
44 unsigned int count, i;
47 ret = reservation_object_get_fences_rcu(resv,
48 &excl, &count, &shared);
52 for (i = 0; i < count; i++) {
53 timeout = i915_gem_object_wait_fence(shared[i],
58 dma_fence_put(shared[i]);
61 for (; i < count; i++)
62 dma_fence_put(shared[i]);
66 * If both shared fences and an exclusive fence exist,
67 * then by construction the shared fences must be later
68 * than the exclusive fence. If we successfully wait for
69 * all the shared fences, we know that the exclusive fence
70 * must all be signaled. If all the shared fences are
71 * signaled, we can prune the array and recover the
72 * floating references on the fences/requests.
74 prune_fences = count && timeout >= 0;
76 excl = reservation_object_get_excl_rcu(resv);
79 if (excl && timeout >= 0)
80 timeout = i915_gem_object_wait_fence(excl, flags, timeout);
85 * Opportunistically prune the fences iff we know they have *all* been
86 * signaled and that the reservation object has not been changed (i.e.
87 * no new fences have been added).
89 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
90 if (reservation_object_trylock(resv)) {
91 if (!__read_seqcount_retry(&resv->seq, seq))
92 reservation_object_add_excl_fence(resv, NULL);
93 reservation_object_unlock(resv);
100 static void __fence_set_priority(struct dma_fence *fence,
101 const struct i915_sched_attr *attr)
103 struct i915_request *rq;
104 struct intel_engine_cs *engine;
106 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
109 rq = to_request(fence);
113 rcu_read_lock(); /* RCU serialisation for set-wedged protection */
114 if (engine->schedule)
115 engine->schedule(rq, attr);
117 local_bh_enable(); /* kick the tasklets if queues were reprioritised */
120 static void fence_set_priority(struct dma_fence *fence,
121 const struct i915_sched_attr *attr)
123 /* Recurse once into a fence-array */
124 if (dma_fence_is_array(fence)) {
125 struct dma_fence_array *array = to_dma_fence_array(fence);
128 for (i = 0; i < array->num_fences; i++)
129 __fence_set_priority(array->fences[i], attr);
131 __fence_set_priority(fence, attr);
136 i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
138 const struct i915_sched_attr *attr)
140 struct dma_fence *excl;
142 if (flags & I915_WAIT_ALL) {
143 struct dma_fence **shared;
144 unsigned int count, i;
147 ret = reservation_object_get_fences_rcu(obj->base.resv,
148 &excl, &count, &shared);
152 for (i = 0; i < count; i++) {
153 fence_set_priority(shared[i], attr);
154 dma_fence_put(shared[i]);
159 excl = reservation_object_get_excl_rcu(obj->base.resv);
163 fence_set_priority(excl, attr);
170 * Waits for rendering to the object to be completed
171 * @obj: i915 gem object
172 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
173 * @timeout: how long to wait
176 i915_gem_object_wait(struct drm_i915_gem_object *obj,
181 GEM_BUG_ON(timeout < 0);
183 timeout = i915_gem_object_wait_reservation(obj->base.resv,
185 return timeout < 0 ? timeout : 0;
188 static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
190 /* nsecs_to_jiffies64() does not guard against overflow */
191 if (NSEC_PER_SEC % HZ &&
192 div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
193 return MAX_JIFFY_OFFSET;
195 return min_t(u64, MAX_JIFFY_OFFSET, nsecs_to_jiffies64(n) + 1);
198 static unsigned long to_wait_timeout(s64 timeout_ns)
201 return MAX_SCHEDULE_TIMEOUT;
206 return nsecs_to_jiffies_timeout(timeout_ns);
210 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
211 * @dev: drm device pointer
212 * @data: ioctl data blob
213 * @file: drm file pointer
215 * Returns 0 if successful, else an error is returned with the remaining time in
216 * the timeout parameter.
217 * -ETIME: object is still busy after timeout
218 * -ERESTARTSYS: signal interrupted the wait
219 * -ENONENT: object doesn't exist
220 * Also possible, but rare:
221 * -EAGAIN: incomplete, restart syscall
223 * -ENODEV: Internal IRQ fail
224 * -E?: The add request failed
226 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
227 * non-zero timeout parameter the wait ioctl will wait for the given number of
228 * nanoseconds on an object becoming unbusy. Since the wait itself does so
229 * without holding struct_mutex the object may become re-busied before this
230 * function completes. A similar but shorter * race condition exists in the busy
234 i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
236 struct drm_i915_gem_wait *args = data;
237 struct drm_i915_gem_object *obj;
241 if (args->flags != 0)
244 obj = i915_gem_object_lookup(file, args->bo_handle);
250 ret = i915_gem_object_wait(obj,
251 I915_WAIT_INTERRUPTIBLE |
254 to_wait_timeout(args->timeout_ns));
256 if (args->timeout_ns > 0) {
257 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
258 if (args->timeout_ns < 0)
259 args->timeout_ns = 0;
262 * Apparently ktime isn't accurate enough and occasionally has a
263 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
264 * things up to make the test happy. We allow up to 1 jiffy.
266 * This is a regression from the timespec->ktime conversion.
268 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
269 args->timeout_ns = 0;
271 /* Asked to wait beyond the jiffie/scheduler precision? */
272 if (ret == -ETIME && args->timeout_ns)
276 i915_gem_object_put(obj);