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 dma_resv
*resv
,
38 struct dma_fence
*excl
;
39 bool prune_fences
= false;
41 if (flags
& I915_WAIT_ALL
) {
42 struct dma_fence
**shared
;
43 unsigned int count
, i
;
46 ret
= dma_resv_get_fences_rcu(resv
,
47 &excl
, &count
, &shared
);
51 for (i
= 0; i
< count
; i
++) {
52 timeout
= i915_gem_object_wait_fence(shared
[i
],
57 dma_fence_put(shared
[i
]);
60 for (; i
< count
; i
++)
61 dma_fence_put(shared
[i
]);
65 * If both shared fences and an exclusive fence exist,
66 * then by construction the shared fences must be later
67 * than the exclusive fence. If we successfully wait for
68 * all the shared fences, we know that the exclusive fence
69 * must all be signaled. If all the shared fences are
70 * signaled, we can prune the array and recover the
71 * floating references on the fences/requests.
73 prune_fences
= count
&& timeout
>= 0;
75 excl
= dma_resv_get_excl_rcu(resv
);
78 if (excl
&& timeout
>= 0)
79 timeout
= i915_gem_object_wait_fence(excl
, flags
, timeout
);
84 * Opportunistically prune the fences iff we know they have *all* been
87 if (prune_fences
&& dma_resv_trylock(resv
)) {
88 if (dma_resv_test_signaled_rcu(resv
, true))
89 dma_resv_add_excl_fence(resv
, NULL
);
90 dma_resv_unlock(resv
);
96 static void __fence_set_priority(struct dma_fence
*fence
,
97 const struct i915_sched_attr
*attr
)
99 struct i915_request
*rq
;
100 struct intel_engine_cs
*engine
;
102 if (dma_fence_is_signaled(fence
) || !dma_fence_is_i915(fence
))
105 rq
= to_request(fence
);
109 rcu_read_lock(); /* RCU serialisation for set-wedged protection */
110 if (engine
->schedule
)
111 engine
->schedule(rq
, attr
);
113 local_bh_enable(); /* kick the tasklets if queues were reprioritised */
116 static void fence_set_priority(struct dma_fence
*fence
,
117 const struct i915_sched_attr
*attr
)
119 /* Recurse once into a fence-array */
120 if (dma_fence_is_array(fence
)) {
121 struct dma_fence_array
*array
= to_dma_fence_array(fence
);
124 for (i
= 0; i
< array
->num_fences
; i
++)
125 __fence_set_priority(array
->fences
[i
], attr
);
127 __fence_set_priority(fence
, attr
);
132 i915_gem_object_wait_priority(struct drm_i915_gem_object
*obj
,
134 const struct i915_sched_attr
*attr
)
136 struct dma_fence
*excl
;
138 if (flags
& I915_WAIT_ALL
) {
139 struct dma_fence
**shared
;
140 unsigned int count
, i
;
143 ret
= dma_resv_get_fences_rcu(obj
->base
.resv
,
144 &excl
, &count
, &shared
);
148 for (i
= 0; i
< count
; i
++) {
149 fence_set_priority(shared
[i
], attr
);
150 dma_fence_put(shared
[i
]);
155 excl
= dma_resv_get_excl_rcu(obj
->base
.resv
);
159 fence_set_priority(excl
, attr
);
166 * Waits for rendering to the object to be completed
167 * @obj: i915 gem object
168 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
169 * @timeout: how long to wait
172 i915_gem_object_wait(struct drm_i915_gem_object
*obj
,
177 GEM_BUG_ON(timeout
< 0);
179 timeout
= i915_gem_object_wait_reservation(obj
->base
.resv
,
181 return timeout
< 0 ? timeout
: 0;
184 static inline unsigned long nsecs_to_jiffies_timeout(const u64 n
)
186 /* nsecs_to_jiffies64() does not guard against overflow */
187 if (NSEC_PER_SEC
% HZ
&&
188 div_u64(n
, NSEC_PER_SEC
) >= MAX_JIFFY_OFFSET
/ HZ
)
189 return MAX_JIFFY_OFFSET
;
191 return min_t(u64
, MAX_JIFFY_OFFSET
, nsecs_to_jiffies64(n
) + 1);
194 static unsigned long to_wait_timeout(s64 timeout_ns
)
197 return MAX_SCHEDULE_TIMEOUT
;
202 return nsecs_to_jiffies_timeout(timeout_ns
);
206 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
207 * @dev: drm device pointer
208 * @data: ioctl data blob
209 * @file: drm file pointer
211 * Returns 0 if successful, else an error is returned with the remaining time in
212 * the timeout parameter.
213 * -ETIME: object is still busy after timeout
214 * -ERESTARTSYS: signal interrupted the wait
215 * -ENONENT: object doesn't exist
216 * Also possible, but rare:
217 * -EAGAIN: incomplete, restart syscall
219 * -ENODEV: Internal IRQ fail
220 * -E?: The add request failed
222 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
223 * non-zero timeout parameter the wait ioctl will wait for the given number of
224 * nanoseconds on an object becoming unbusy. Since the wait itself does so
225 * without holding struct_mutex the object may become re-busied before this
226 * function completes. A similar but shorter * race condition exists in the busy
230 i915_gem_wait_ioctl(struct drm_device
*dev
, void *data
, struct drm_file
*file
)
232 struct drm_i915_gem_wait
*args
= data
;
233 struct drm_i915_gem_object
*obj
;
237 if (args
->flags
!= 0)
240 obj
= i915_gem_object_lookup(file
, args
->bo_handle
);
246 ret
= i915_gem_object_wait(obj
,
247 I915_WAIT_INTERRUPTIBLE
|
250 to_wait_timeout(args
->timeout_ns
));
252 if (args
->timeout_ns
> 0) {
253 args
->timeout_ns
-= ktime_to_ns(ktime_sub(ktime_get(), start
));
254 if (args
->timeout_ns
< 0)
255 args
->timeout_ns
= 0;
258 * Apparently ktime isn't accurate enough and occasionally has a
259 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
260 * things up to make the test happy. We allow up to 1 jiffy.
262 * This is a regression from the timespec->ktime conversion.
264 if (ret
== -ETIME
&& !nsecs_to_jiffies(args
->timeout_ns
))
265 args
->timeout_ns
= 0;
267 /* Asked to wait beyond the jiffie/scheduler precision? */
268 if (ret
== -ETIME
&& args
->timeout_ns
)
272 i915_gem_object_put(obj
);