This source file includes following definitions.
- i915_gem_object_wait_fence
- i915_gem_object_wait_reservation
- __fence_set_priority
- fence_set_priority
- i915_gem_object_wait_priority
- i915_gem_object_wait
- nsecs_to_jiffies_timeout
- to_wait_timeout
- i915_gem_wait_ioctl
1
2
3
4
5
6
7 #include <linux/dma-fence-array.h>
8 #include <linux/jiffies.h>
9
10 #include "gt/intel_engine.h"
11
12 #include "i915_gem_ioctls.h"
13 #include "i915_gem_object.h"
14
15 static long
16 i915_gem_object_wait_fence(struct dma_fence *fence,
17 unsigned int flags,
18 long timeout)
19 {
20 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
21
22 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
23 return timeout;
24
25 if (dma_fence_is_i915(fence))
26 return i915_request_wait(to_request(fence), flags, timeout);
27
28 return dma_fence_wait_timeout(fence,
29 flags & I915_WAIT_INTERRUPTIBLE,
30 timeout);
31 }
32
33 static long
34 i915_gem_object_wait_reservation(struct dma_resv *resv,
35 unsigned int flags,
36 long timeout)
37 {
38 struct dma_fence *excl;
39 bool prune_fences = false;
40
41 if (flags & I915_WAIT_ALL) {
42 struct dma_fence **shared;
43 unsigned int count, i;
44 int ret;
45
46 ret = dma_resv_get_fences_rcu(resv,
47 &excl, &count, &shared);
48 if (ret)
49 return ret;
50
51 for (i = 0; i < count; i++) {
52 timeout = i915_gem_object_wait_fence(shared[i],
53 flags, timeout);
54 if (timeout < 0)
55 break;
56
57 dma_fence_put(shared[i]);
58 }
59
60 for (; i < count; i++)
61 dma_fence_put(shared[i]);
62 kfree(shared);
63
64
65
66
67
68
69
70
71
72
73 prune_fences = count && timeout >= 0;
74 } else {
75 excl = dma_resv_get_excl_rcu(resv);
76 }
77
78 if (excl && timeout >= 0)
79 timeout = i915_gem_object_wait_fence(excl, flags, timeout);
80
81 dma_fence_put(excl);
82
83
84
85
86
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);
91 }
92
93 return timeout;
94 }
95
96 static void __fence_set_priority(struct dma_fence *fence,
97 const struct i915_sched_attr *attr)
98 {
99 struct i915_request *rq;
100 struct intel_engine_cs *engine;
101
102 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
103 return;
104
105 rq = to_request(fence);
106 engine = rq->engine;
107
108 local_bh_disable();
109 rcu_read_lock();
110 if (engine->schedule)
111 engine->schedule(rq, attr);
112 rcu_read_unlock();
113 local_bh_enable();
114 }
115
116 static void fence_set_priority(struct dma_fence *fence,
117 const struct i915_sched_attr *attr)
118 {
119
120 if (dma_fence_is_array(fence)) {
121 struct dma_fence_array *array = to_dma_fence_array(fence);
122 int i;
123
124 for (i = 0; i < array->num_fences; i++)
125 __fence_set_priority(array->fences[i], attr);
126 } else {
127 __fence_set_priority(fence, attr);
128 }
129 }
130
131 int
132 i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
133 unsigned int flags,
134 const struct i915_sched_attr *attr)
135 {
136 struct dma_fence *excl;
137
138 if (flags & I915_WAIT_ALL) {
139 struct dma_fence **shared;
140 unsigned int count, i;
141 int ret;
142
143 ret = dma_resv_get_fences_rcu(obj->base.resv,
144 &excl, &count, &shared);
145 if (ret)
146 return ret;
147
148 for (i = 0; i < count; i++) {
149 fence_set_priority(shared[i], attr);
150 dma_fence_put(shared[i]);
151 }
152
153 kfree(shared);
154 } else {
155 excl = dma_resv_get_excl_rcu(obj->base.resv);
156 }
157
158 if (excl) {
159 fence_set_priority(excl, attr);
160 dma_fence_put(excl);
161 }
162 return 0;
163 }
164
165
166
167
168
169
170
171 int
172 i915_gem_object_wait(struct drm_i915_gem_object *obj,
173 unsigned int flags,
174 long timeout)
175 {
176 might_sleep();
177 GEM_BUG_ON(timeout < 0);
178
179 timeout = i915_gem_object_wait_reservation(obj->base.resv,
180 flags, timeout);
181 return timeout < 0 ? timeout : 0;
182 }
183
184 static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
185 {
186
187 if (NSEC_PER_SEC % HZ &&
188 div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
189 return MAX_JIFFY_OFFSET;
190
191 return min_t(u64, MAX_JIFFY_OFFSET, nsecs_to_jiffies64(n) + 1);
192 }
193
194 static unsigned long to_wait_timeout(s64 timeout_ns)
195 {
196 if (timeout_ns < 0)
197 return MAX_SCHEDULE_TIMEOUT;
198
199 if (timeout_ns == 0)
200 return 0;
201
202 return nsecs_to_jiffies_timeout(timeout_ns);
203 }
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 int
230 i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
231 {
232 struct drm_i915_gem_wait *args = data;
233 struct drm_i915_gem_object *obj;
234 ktime_t start;
235 long ret;
236
237 if (args->flags != 0)
238 return -EINVAL;
239
240 obj = i915_gem_object_lookup(file, args->bo_handle);
241 if (!obj)
242 return -ENOENT;
243
244 start = ktime_get();
245
246 ret = i915_gem_object_wait(obj,
247 I915_WAIT_INTERRUPTIBLE |
248 I915_WAIT_PRIORITY |
249 I915_WAIT_ALL,
250 to_wait_timeout(args->timeout_ns));
251
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;
256
257
258
259
260
261
262
263
264 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
265 args->timeout_ns = 0;
266
267
268 if (ret == -ETIME && args->timeout_ns)
269 ret = -EAGAIN;
270 }
271
272 i915_gem_object_put(obj);
273 return ret;
274 }