xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/i915/gt/intel_breadcrumbs.c (revision 2a8c33eaff5adddac3ef2c5cb48ee67ef6d5d6dc)
1 /*	$NetBSD: intel_breadcrumbs.c,v 1.5 2021/12/19 12:32:15 riastradh Exp $	*/
2 
3 /*
4  * Copyright © 2015 Intel Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23  * IN THE SOFTWARE.
24  *
25  */
26 
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: intel_breadcrumbs.c,v 1.5 2021/12/19 12:32:15 riastradh Exp $");
29 
30 #include <linux/kthread.h>
31 #include <trace/events/dma_fence.h>
32 #include <uapi/linux/sched/types.h>
33 
34 #include "i915_drv.h"
35 #include "i915_trace.h"
36 #include "intel_gt_pm.h"
37 #include "intel_gt_requests.h"
38 
39 #include <linux/nbsd-namespace.h>
40 
irq_enable(struct intel_engine_cs * engine)41 static void irq_enable(struct intel_engine_cs *engine)
42 {
43 	if (!engine->irq_enable)
44 		return;
45 
46 	/* Caller disables interrupts */
47 	spin_lock(&engine->gt->irq_lock);
48 	engine->irq_enable(engine);
49 	spin_unlock(&engine->gt->irq_lock);
50 }
51 
irq_disable(struct intel_engine_cs * engine)52 static void irq_disable(struct intel_engine_cs *engine)
53 {
54 	if (!engine->irq_disable)
55 		return;
56 
57 	/* Caller disables interrupts */
58 	spin_lock(&engine->gt->irq_lock);
59 	engine->irq_disable(engine);
60 	spin_unlock(&engine->gt->irq_lock);
61 }
62 
__intel_breadcrumbs_disarm_irq(struct intel_breadcrumbs * b)63 static void __intel_breadcrumbs_disarm_irq(struct intel_breadcrumbs *b)
64 {
65 	struct intel_engine_cs *engine =
66 		container_of(b, struct intel_engine_cs, breadcrumbs);
67 
68 	lockdep_assert_held(&b->irq_lock);
69 
70 	GEM_BUG_ON(!b->irq_enabled);
71 	if (!--b->irq_enabled)
72 		irq_disable(engine);
73 
74 	b->irq_armed = false;
75 	intel_gt_pm_put_async(engine->gt);
76 }
77 
intel_engine_disarm_breadcrumbs(struct intel_engine_cs * engine)78 void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
79 {
80 	struct intel_breadcrumbs *b = &engine->breadcrumbs;
81 	unsigned long flags;
82 
83 	if (!b->irq_armed)
84 		return;
85 
86 	spin_lock_irqsave(&b->irq_lock, flags);
87 	if (b->irq_armed)
88 		__intel_breadcrumbs_disarm_irq(b);
89 	spin_unlock_irqrestore(&b->irq_lock, flags);
90 }
91 
__request_completed(const struct i915_request * rq)92 static inline bool __request_completed(const struct i915_request *rq)
93 {
94 	return i915_seqno_passed(__hwsp_seqno(rq), rq->fence.seqno);
95 }
96 
97 __maybe_unused static bool
check_signal_order(struct intel_context * ce,struct i915_request * rq)98 check_signal_order(struct intel_context *ce, struct i915_request *rq)
99 {
100 	if (!list_is_last(&rq->signal_link, &ce->signals) &&
101 	    i915_seqno_passed(rq->fence.seqno,
102 			      list_next_entry(rq, signal_link)->fence.seqno))
103 		return false;
104 
105 	if (!list_is_first(&rq->signal_link, &ce->signals) &&
106 	    i915_seqno_passed(list_prev_entry(rq, signal_link)->fence.seqno,
107 			      rq->fence.seqno))
108 		return false;
109 
110 	return true;
111 }
112 
113 #ifndef __NetBSD__
114 
115 static bool
__dma_fence_signal(struct dma_fence * fence)116 __dma_fence_signal(struct dma_fence *fence)
117 {
118 	return !test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
119 }
120 
121 static void
__dma_fence_signal__timestamp(struct dma_fence * fence,ktime_t timestamp)122 __dma_fence_signal__timestamp(struct dma_fence *fence, ktime_t timestamp)
123 {
124 	fence->timestamp = timestamp;
125 	set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
126 	trace_dma_fence_signaled(fence);
127 }
128 
129 static void
__dma_fence_signal__notify(struct dma_fence * fence,const struct list_head * list)130 __dma_fence_signal__notify(struct dma_fence *fence,
131 			   const struct list_head *list)
132 {
133 	struct dma_fence_cb *cur, *tmp;
134 
135 	lockdep_assert_held(fence->lock);
136 
137 	list_for_each_entry_safe(cur, tmp, list, node) {
138 		INIT_LIST_HEAD(&cur->node);
139 		cur->func(fence, cur);
140 	}
141 }
142 
143 #endif
144 
add_retire(struct intel_breadcrumbs * b,struct intel_timeline * tl)145 static void add_retire(struct intel_breadcrumbs *b, struct intel_timeline *tl)
146 {
147 	struct intel_engine_cs *engine =
148 		container_of(b, struct intel_engine_cs, breadcrumbs);
149 
150 	if (unlikely(intel_engine_is_virtual(engine)))
151 		engine = intel_virtual_engine_get_sibling(engine, 0);
152 
153 	intel_engine_add_retire(engine, tl);
154 }
155 
signal_irq_work(struct irq_work * work)156 static void signal_irq_work(struct irq_work *work)
157 {
158 	struct intel_breadcrumbs *b = container_of(work, typeof(*b), irq_work);
159 	const ktime_t timestamp = ktime_get();
160 	struct intel_context *ce, *cn;
161 	struct list_head *pos, *next;
162 	LIST_HEAD(signal);
163 
164 	spin_lock(&b->irq_lock);
165 
166 	if (b->irq_armed && list_empty(&b->signalers))
167 		__intel_breadcrumbs_disarm_irq(b);
168 
169 	list_for_each_entry_safe(ce, cn, &b->signalers, signal_link) {
170 		GEM_BUG_ON(list_empty(&ce->signals));
171 
172 		list_for_each_safe(pos, next, &ce->signals) {
173 			struct i915_request *rq =
174 				list_entry(pos, typeof(*rq), signal_link);
175 
176 			GEM_BUG_ON(!check_signal_order(ce, rq));
177 
178 			if (!__request_completed(rq))
179 				break;
180 
181 			GEM_BUG_ON(!test_bit(I915_FENCE_FLAG_SIGNAL,
182 					     &rq->fence.flags));
183 			clear_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
184 
185 			if (!__dma_fence_signal(&rq->fence))
186 				continue;
187 
188 			/*
189 			 * Queue for execution after dropping the signaling
190 			 * spinlock as the callback chain may end up adding
191 			 * more signalers to the same context or engine.
192 			 */
193 			i915_request_get(rq);
194 			list_add_tail(&rq->signal_link, &signal);
195 		}
196 
197 		/*
198 		 * We process the list deletion in bulk, only using a list_add
199 		 * (not list_move) above but keeping the status of
200 		 * rq->signal_link known with the I915_FENCE_FLAG_SIGNAL bit.
201 		 */
202 		if (!list_is_first(pos, &ce->signals)) {
203 			/* Advance the list to the first incomplete request */
204 			__list_del_many(&ce->signals, pos);
205 			if (&ce->signals == pos) { /* now empty */
206 				list_del_init(&ce->signal_link);
207 				add_retire(b, ce->timeline);
208 			}
209 		}
210 	}
211 
212 	spin_unlock(&b->irq_lock);
213 
214 	list_for_each_safe(pos, next, &signal) {
215 		struct i915_request *rq =
216 			list_entry(pos, typeof(*rq), signal_link);
217 #ifdef __NetBSD__
218 		__dma_fence_signal_wake(&rq->fence, timestamp);
219 #else
220 		struct list_head cb_list;
221 
222 		spin_lock(&rq->lock);
223 		list_replace(&rq->fence.cb_list, &cb_list);
224 		__dma_fence_signal__timestamp(&rq->fence, timestamp);
225 		__dma_fence_signal__notify(&rq->fence, &cb_list);
226 		spin_unlock(&rq->lock);
227 #endif
228 
229 		i915_request_put(rq);
230 	}
231 }
232 
__intel_breadcrumbs_arm_irq(struct intel_breadcrumbs * b)233 static bool __intel_breadcrumbs_arm_irq(struct intel_breadcrumbs *b)
234 {
235 	struct intel_engine_cs *engine =
236 		container_of(b, struct intel_engine_cs, breadcrumbs);
237 
238 	lockdep_assert_held(&b->irq_lock);
239 	if (b->irq_armed)
240 		return true;
241 
242 	if (!intel_gt_pm_get_if_awake(engine->gt))
243 		return false;
244 
245 	/*
246 	 * The breadcrumb irq will be disarmed on the interrupt after the
247 	 * waiters are signaled. This gives us a single interrupt window in
248 	 * which we can add a new waiter and avoid the cost of re-enabling
249 	 * the irq.
250 	 */
251 	b->irq_armed = true;
252 
253 	/*
254 	 * Since we are waiting on a request, the GPU should be busy
255 	 * and should have its own rpm reference. This is tracked
256 	 * by i915->gt.awake, we can forgo holding our own wakref
257 	 * for the interrupt as before i915->gt.awake is released (when
258 	 * the driver is idle) we disarm the breadcrumbs.
259 	 */
260 
261 	if (!b->irq_enabled++)
262 		irq_enable(engine);
263 
264 	return true;
265 }
266 
intel_engine_init_breadcrumbs(struct intel_engine_cs * engine)267 void intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
268 {
269 	struct intel_breadcrumbs *b = &engine->breadcrumbs;
270 
271 	spin_lock_init(&b->irq_lock);
272 	INIT_LIST_HEAD(&b->signalers);
273 
274 	init_irq_work(&b->irq_work, signal_irq_work);
275 }
276 
intel_engine_reset_breadcrumbs(struct intel_engine_cs * engine)277 void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
278 {
279 	struct intel_breadcrumbs *b = &engine->breadcrumbs;
280 	unsigned long flags;
281 
282 	spin_lock_irqsave(&b->irq_lock, flags);
283 
284 	if (b->irq_enabled)
285 		irq_enable(engine);
286 	else
287 		irq_disable(engine);
288 
289 	spin_unlock_irqrestore(&b->irq_lock, flags);
290 }
291 
intel_engine_fini_breadcrumbs(struct intel_engine_cs * engine)292 void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
293 {
294 	struct intel_breadcrumbs *b = &engine->breadcrumbs;
295 
296 	spin_lock_destroy(&b->irq_lock);
297 }
298 
i915_request_enable_breadcrumb(struct i915_request * rq)299 bool i915_request_enable_breadcrumb(struct i915_request *rq)
300 {
301 	lockdep_assert_held(&rq->lock);
302 
303 	if (test_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags)) {
304 		struct intel_breadcrumbs *b = &rq->engine->breadcrumbs;
305 		struct intel_context *ce = rq->context;
306 		struct list_head *pos;
307 
308 		spin_lock(&b->irq_lock);
309 		GEM_BUG_ON(test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags));
310 
311 		if (!__intel_breadcrumbs_arm_irq(b))
312 			goto unlock;
313 
314 		/*
315 		 * We keep the seqno in retirement order, so we can break
316 		 * inside intel_engine_signal_breadcrumbs as soon as we've
317 		 * passed the last completed request (or seen a request that
318 		 * hasn't event started). We could walk the timeline->requests,
319 		 * but keeping a separate signalers_list has the advantage of
320 		 * hopefully being much smaller than the full list and so
321 		 * provides faster iteration and detection when there are no
322 		 * more interrupts required for this context.
323 		 *
324 		 * We typically expect to add new signalers in order, so we
325 		 * start looking for our insertion point from the tail of
326 		 * the list.
327 		 */
328 		list_for_each_prev(pos, &ce->signals) {
329 			struct i915_request *it =
330 				list_entry(pos, typeof(*it), signal_link);
331 
332 			if (i915_seqno_passed(rq->fence.seqno, it->fence.seqno))
333 				break;
334 		}
335 		list_add(&rq->signal_link, pos);
336 		if (pos == &ce->signals) /* catch transitions from empty list */
337 			list_move_tail(&ce->signal_link, &b->signalers);
338 		GEM_BUG_ON(!check_signal_order(ce, rq));
339 
340 		set_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
341 unlock:
342 		spin_unlock(&b->irq_lock);
343 	}
344 
345 	return !__request_completed(rq);
346 }
347 
i915_request_cancel_breadcrumb(struct i915_request * rq)348 void i915_request_cancel_breadcrumb(struct i915_request *rq)
349 {
350 	struct intel_breadcrumbs *b = &rq->engine->breadcrumbs;
351 
352 	lockdep_assert_held(&rq->lock);
353 
354 	/*
355 	 * We must wait for b->irq_lock so that we know the interrupt handler
356 	 * has released its reference to the intel_context and has completed
357 	 * the DMA_FENCE_FLAG_SIGNALED_BIT/I915_FENCE_FLAG_SIGNAL dance (if
358 	 * required).
359 	 */
360 	spin_lock(&b->irq_lock);
361 	if (test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags)) {
362 		struct intel_context *ce = rq->context;
363 
364 		list_del(&rq->signal_link);
365 		if (list_empty(&ce->signals))
366 			list_del_init(&ce->signal_link);
367 
368 		clear_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
369 	}
370 	spin_unlock(&b->irq_lock);
371 }
372 
intel_engine_print_breadcrumbs(struct intel_engine_cs * engine,struct drm_printer * p)373 void intel_engine_print_breadcrumbs(struct intel_engine_cs *engine,
374 				    struct drm_printer *p)
375 {
376 	struct intel_breadcrumbs *b = &engine->breadcrumbs;
377 	struct intel_context *ce;
378 	struct i915_request *rq;
379 
380 	if (list_empty(&b->signalers))
381 		return;
382 
383 	drm_printf(p, "Signals:\n");
384 
385 	spin_lock_irq(&b->irq_lock);
386 	list_for_each_entry(ce, &b->signalers, signal_link) {
387 		list_for_each_entry(rq, &ce->signals, signal_link) {
388 			drm_printf(p, "\t[%"PRIx64":%"PRIx64"%s] @ %dms\n",
389 				   (uint64_t)rq->fence.context,
390 				   (uint64_t)rq->fence.seqno,
391 				   i915_request_completed(rq) ? "!" :
392 				   i915_request_started(rq) ? "*" :
393 				   "",
394 				   jiffies_to_msecs(jiffies - rq->emitted_jiffies));
395 		}
396 	}
397 	spin_unlock_irq(&b->irq_lock);
398 }
399