xref: /linux/drivers/gpu/drm/i915/gt/intel_breadcrumbs.c (revision f4bd0b4a9b21c609ede28cee2dcd16824c0489a8)
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #include <linux/kthread.h>
26 #include <trace/events/dma_fence.h>
27 #include <uapi/linux/sched/types.h>
28 
29 #include "i915_drv.h"
30 #include "i915_trace.h"
31 #include "intel_breadcrumbs.h"
32 #include "intel_context.h"
33 #include "intel_engine_pm.h"
34 #include "intel_gt_pm.h"
35 #include "intel_gt_requests.h"
36 
37 static bool irq_enable(struct intel_engine_cs *engine)
38 {
39 	if (!engine->irq_enable)
40 		return false;
41 
42 	/* Caller disables interrupts */
43 	spin_lock(&engine->gt->irq_lock);
44 	engine->irq_enable(engine);
45 	spin_unlock(&engine->gt->irq_lock);
46 
47 	return true;
48 }
49 
50 static void irq_disable(struct intel_engine_cs *engine)
51 {
52 	if (!engine->irq_disable)
53 		return;
54 
55 	/* Caller disables interrupts */
56 	spin_lock(&engine->gt->irq_lock);
57 	engine->irq_disable(engine);
58 	spin_unlock(&engine->gt->irq_lock);
59 }
60 
61 static void __intel_breadcrumbs_arm_irq(struct intel_breadcrumbs *b)
62 {
63 	/*
64 	 * Since we are waiting on a request, the GPU should be busy
65 	 * and should have its own rpm reference.
66 	 */
67 	if (GEM_WARN_ON(!intel_gt_pm_get_if_awake(b->irq_engine->gt)))
68 		return;
69 
70 	/*
71 	 * The breadcrumb irq will be disarmed on the interrupt after the
72 	 * waiters are signaled. This gives us a single interrupt window in
73 	 * which we can add a new waiter and avoid the cost of re-enabling
74 	 * the irq.
75 	 */
76 	WRITE_ONCE(b->irq_armed, true);
77 
78 	/* Requests may have completed before we could enable the interrupt. */
79 	if (!b->irq_enabled++ && irq_enable(b->irq_engine))
80 		irq_work_queue(&b->irq_work);
81 }
82 
83 static void intel_breadcrumbs_arm_irq(struct intel_breadcrumbs *b)
84 {
85 	if (!b->irq_engine)
86 		return;
87 
88 	spin_lock(&b->irq_lock);
89 	if (!b->irq_armed)
90 		__intel_breadcrumbs_arm_irq(b);
91 	spin_unlock(&b->irq_lock);
92 }
93 
94 static void __intel_breadcrumbs_disarm_irq(struct intel_breadcrumbs *b)
95 {
96 	GEM_BUG_ON(!b->irq_enabled);
97 	if (!--b->irq_enabled)
98 		irq_disable(b->irq_engine);
99 
100 	WRITE_ONCE(b->irq_armed, false);
101 	intel_gt_pm_put_async(b->irq_engine->gt);
102 }
103 
104 static void add_signaling_context(struct intel_breadcrumbs *b,
105 				  struct intel_context *ce)
106 {
107 	intel_context_get(ce);
108 	list_add_tail(&ce->signal_link, &b->signalers);
109 }
110 
111 static void remove_signaling_context(struct intel_breadcrumbs *b,
112 				     struct intel_context *ce)
113 {
114 	list_del(&ce->signal_link);
115 	intel_context_put(ce);
116 }
117 
118 static inline bool __request_completed(const struct i915_request *rq)
119 {
120 	return i915_seqno_passed(__hwsp_seqno(rq), rq->fence.seqno);
121 }
122 
123 __maybe_unused static bool
124 check_signal_order(struct intel_context *ce, struct i915_request *rq)
125 {
126 	if (rq->context != ce)
127 		return false;
128 
129 	if (!list_is_last(&rq->signal_link, &ce->signals) &&
130 	    i915_seqno_passed(rq->fence.seqno,
131 			      list_next_entry(rq, signal_link)->fence.seqno))
132 		return false;
133 
134 	if (!list_is_first(&rq->signal_link, &ce->signals) &&
135 	    i915_seqno_passed(list_prev_entry(rq, signal_link)->fence.seqno,
136 			      rq->fence.seqno))
137 		return false;
138 
139 	return true;
140 }
141 
142 static bool
143 __dma_fence_signal(struct dma_fence *fence)
144 {
145 	return !test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
146 }
147 
148 static void
149 __dma_fence_signal__timestamp(struct dma_fence *fence, ktime_t timestamp)
150 {
151 	fence->timestamp = timestamp;
152 	set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
153 	trace_dma_fence_signaled(fence);
154 }
155 
156 static void
157 __dma_fence_signal__notify(struct dma_fence *fence,
158 			   const struct list_head *list)
159 {
160 	struct dma_fence_cb *cur, *tmp;
161 
162 	lockdep_assert_held(fence->lock);
163 
164 	list_for_each_entry_safe(cur, tmp, list, node) {
165 		INIT_LIST_HEAD(&cur->node);
166 		cur->func(fence, cur);
167 	}
168 }
169 
170 static void add_retire(struct intel_breadcrumbs *b, struct intel_timeline *tl)
171 {
172 	if (b->irq_engine)
173 		intel_engine_add_retire(b->irq_engine, tl);
174 }
175 
176 static bool __signal_request(struct i915_request *rq)
177 {
178 	if (!__dma_fence_signal(&rq->fence)) {
179 		i915_request_put(rq);
180 		return false;
181 	}
182 
183 	return true;
184 }
185 
186 static struct llist_node *
187 slist_add(struct llist_node *node, struct llist_node *head)
188 {
189 	node->next = head;
190 	return node;
191 }
192 
193 static void signal_irq_work(struct irq_work *work)
194 {
195 	struct intel_breadcrumbs *b = container_of(work, typeof(*b), irq_work);
196 	const ktime_t timestamp = ktime_get();
197 	struct llist_node *signal, *sn;
198 	struct intel_context *ce, *cn;
199 	struct list_head *pos, *next;
200 
201 	signal = NULL;
202 	if (unlikely(!llist_empty(&b->signaled_requests)))
203 		signal = llist_del_all(&b->signaled_requests);
204 
205 	spin_lock(&b->irq_lock);
206 
207 	/*
208 	 * Keep the irq armed until the interrupt after all listeners are gone.
209 	 *
210 	 * Enabling/disabling the interrupt is rather costly, roughly a couple
211 	 * of hundred microseconds. If we are proactive and enable/disable
212 	 * the interrupt around every request that wants a breadcrumb, we
213 	 * quickly drown in the extra orders of magnitude of latency imposed
214 	 * on request submission.
215 	 *
216 	 * So we try to be lazy, and keep the interrupts enabled until no
217 	 * more listeners appear within a breadcrumb interrupt interval (that
218 	 * is until a request completes that no one cares about). The
219 	 * observation is that listeners come in batches, and will often
220 	 * listen to a bunch of requests in succession. Though note on icl+,
221 	 * interrupts are always enabled due to concerns with rc6 being
222 	 * dysfunctional with per-engine interrupt masking.
223 	 *
224 	 * We also try to avoid raising too many interrupts, as they may
225 	 * be generated by userspace batches and it is unfortunately rather
226 	 * too easy to drown the CPU under a flood of GPU interrupts. Thus
227 	 * whenever no one appears to be listening, we turn off the interrupts.
228 	 * Fewer interrupts should conserve power -- at the very least, fewer
229 	 * interrupt draw less ire from other users of the system and tools
230 	 * like powertop.
231 	 */
232 	if (!signal && b->irq_armed && list_empty(&b->signalers))
233 		__intel_breadcrumbs_disarm_irq(b);
234 
235 	list_for_each_entry_safe(ce, cn, &b->signalers, signal_link) {
236 		GEM_BUG_ON(list_empty(&ce->signals));
237 
238 		list_for_each_safe(pos, next, &ce->signals) {
239 			struct i915_request *rq =
240 				list_entry(pos, typeof(*rq), signal_link);
241 
242 			GEM_BUG_ON(!check_signal_order(ce, rq));
243 			if (!__request_completed(rq))
244 				break;
245 
246 			/*
247 			 * Queue for execution after dropping the signaling
248 			 * spinlock as the callback chain may end up adding
249 			 * more signalers to the same context or engine.
250 			 */
251 			clear_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
252 			if (__signal_request(rq))
253 				/* We own signal_node now, xfer to local list */
254 				signal = slist_add(&rq->signal_node, signal);
255 		}
256 
257 		/*
258 		 * We process the list deletion in bulk, only using a list_add
259 		 * (not list_move) above but keeping the status of
260 		 * rq->signal_link known with the I915_FENCE_FLAG_SIGNAL bit.
261 		 */
262 		if (!list_is_first(pos, &ce->signals)) {
263 			/* Advance the list to the first incomplete request */
264 			__list_del_many(&ce->signals, pos);
265 			if (&ce->signals == pos) { /* now empty */
266 				add_retire(b, ce->timeline);
267 				remove_signaling_context(b, ce);
268 			}
269 		}
270 	}
271 
272 	spin_unlock(&b->irq_lock);
273 
274 	llist_for_each_safe(signal, sn, signal) {
275 		struct i915_request *rq =
276 			llist_entry(signal, typeof(*rq), signal_node);
277 		struct list_head cb_list;
278 
279 		spin_lock(&rq->lock);
280 		list_replace(&rq->fence.cb_list, &cb_list);
281 		__dma_fence_signal__timestamp(&rq->fence, timestamp);
282 		__dma_fence_signal__notify(&rq->fence, &cb_list);
283 		spin_unlock(&rq->lock);
284 
285 		i915_request_put(rq);
286 	}
287 
288 	if (!READ_ONCE(b->irq_armed) && !list_empty(&b->signalers))
289 		intel_breadcrumbs_arm_irq(b);
290 }
291 
292 struct intel_breadcrumbs *
293 intel_breadcrumbs_create(struct intel_engine_cs *irq_engine)
294 {
295 	struct intel_breadcrumbs *b;
296 
297 	b = kzalloc(sizeof(*b), GFP_KERNEL);
298 	if (!b)
299 		return NULL;
300 
301 	spin_lock_init(&b->irq_lock);
302 	INIT_LIST_HEAD(&b->signalers);
303 	init_llist_head(&b->signaled_requests);
304 
305 	init_irq_work(&b->irq_work, signal_irq_work);
306 
307 	b->irq_engine = irq_engine;
308 
309 	return b;
310 }
311 
312 void intel_breadcrumbs_reset(struct intel_breadcrumbs *b)
313 {
314 	unsigned long flags;
315 
316 	if (!b->irq_engine)
317 		return;
318 
319 	spin_lock_irqsave(&b->irq_lock, flags);
320 
321 	if (b->irq_enabled)
322 		irq_enable(b->irq_engine);
323 	else
324 		irq_disable(b->irq_engine);
325 
326 	spin_unlock_irqrestore(&b->irq_lock, flags);
327 }
328 
329 void intel_breadcrumbs_park(struct intel_breadcrumbs *b)
330 {
331 	/* Kick the work once more to drain the signalers */
332 	irq_work_sync(&b->irq_work);
333 	while (unlikely(READ_ONCE(b->irq_armed))) {
334 		local_irq_disable();
335 		signal_irq_work(&b->irq_work);
336 		local_irq_enable();
337 		cond_resched();
338 	}
339 	GEM_BUG_ON(!list_empty(&b->signalers));
340 }
341 
342 void intel_breadcrumbs_free(struct intel_breadcrumbs *b)
343 {
344 	irq_work_sync(&b->irq_work);
345 	GEM_BUG_ON(!list_empty(&b->signalers));
346 	GEM_BUG_ON(b->irq_armed);
347 	kfree(b);
348 }
349 
350 static void insert_breadcrumb(struct i915_request *rq,
351 			      struct intel_breadcrumbs *b)
352 {
353 	struct intel_context *ce = rq->context;
354 	struct list_head *pos;
355 
356 	if (test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags))
357 		return;
358 
359 	i915_request_get(rq);
360 
361 	/*
362 	 * If the request is already completed, we can transfer it
363 	 * straight onto a signaled list, and queue the irq worker for
364 	 * its signal completion.
365 	 */
366 	if (__request_completed(rq)) {
367 		if (__signal_request(rq) &&
368 		    llist_add(&rq->signal_node, &b->signaled_requests))
369 			irq_work_queue(&b->irq_work);
370 		return;
371 	}
372 
373 	if (list_empty(&ce->signals)) {
374 		add_signaling_context(b, ce);
375 		pos = &ce->signals;
376 	} else {
377 		/*
378 		 * We keep the seqno in retirement order, so we can break
379 		 * inside intel_engine_signal_breadcrumbs as soon as we've
380 		 * passed the last completed request (or seen a request that
381 		 * hasn't event started). We could walk the timeline->requests,
382 		 * but keeping a separate signalers_list has the advantage of
383 		 * hopefully being much smaller than the full list and so
384 		 * provides faster iteration and detection when there are no
385 		 * more interrupts required for this context.
386 		 *
387 		 * We typically expect to add new signalers in order, so we
388 		 * start looking for our insertion point from the tail of
389 		 * the list.
390 		 */
391 		list_for_each_prev(pos, &ce->signals) {
392 			struct i915_request *it =
393 				list_entry(pos, typeof(*it), signal_link);
394 
395 			if (i915_seqno_passed(rq->fence.seqno, it->fence.seqno))
396 				break;
397 		}
398 	}
399 	list_add(&rq->signal_link, pos);
400 	GEM_BUG_ON(!check_signal_order(ce, rq));
401 	set_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
402 
403 	/*
404 	 * Defer enabling the interrupt to after HW submission and recheck
405 	 * the request as it may have completed and raised the interrupt as
406 	 * we were attaching it into the lists.
407 	 */
408 	irq_work_queue(&b->irq_work);
409 }
410 
411 bool i915_request_enable_breadcrumb(struct i915_request *rq)
412 {
413 	struct intel_breadcrumbs *b;
414 
415 	/* Serialises with i915_request_retire() using rq->lock */
416 	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags))
417 		return true;
418 
419 	/*
420 	 * Peek at i915_request_submit()/i915_request_unsubmit() status.
421 	 *
422 	 * If the request is not yet active (and not signaled), we will
423 	 * attach the breadcrumb later.
424 	 */
425 	if (!test_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags))
426 		return true;
427 
428 	/*
429 	 * rq->engine is locked by rq->engine->active.lock. That however
430 	 * is not known until after rq->engine has been dereferenced and
431 	 * the lock acquired. Hence we acquire the lock and then validate
432 	 * that rq->engine still matches the lock we hold for it.
433 	 *
434 	 * Here, we are using the breadcrumb lock as a proxy for the
435 	 * rq->engine->active.lock, and we know that since the breadcrumb
436 	 * will be serialised within i915_request_submit/i915_request_unsubmit,
437 	 * the engine cannot change while active as long as we hold the
438 	 * breadcrumb lock on that engine.
439 	 *
440 	 * From the dma_fence_enable_signaling() path, we are outside of the
441 	 * request submit/unsubmit path, and so we must be more careful to
442 	 * acquire the right lock.
443 	 */
444 	b = READ_ONCE(rq->engine)->breadcrumbs;
445 	spin_lock(&b->irq_lock);
446 	while (unlikely(b != READ_ONCE(rq->engine)->breadcrumbs)) {
447 		spin_unlock(&b->irq_lock);
448 		b = READ_ONCE(rq->engine)->breadcrumbs;
449 		spin_lock(&b->irq_lock);
450 	}
451 
452 	/*
453 	 * Now that we are finally serialised with request submit/unsubmit,
454 	 * [with b->irq_lock] and with i915_request_retire() [via checking
455 	 * SIGNALED with rq->lock] confirm the request is indeed active. If
456 	 * it is no longer active, the breadcrumb will be attached upon
457 	 * i915_request_submit().
458 	 */
459 	if (test_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags))
460 		insert_breadcrumb(rq, b);
461 
462 	spin_unlock(&b->irq_lock);
463 
464 	return true;
465 }
466 
467 void i915_request_cancel_breadcrumb(struct i915_request *rq)
468 {
469 	struct intel_breadcrumbs *b = rq->engine->breadcrumbs;
470 
471 	/*
472 	 * We must wait for b->irq_lock so that we know the interrupt handler
473 	 * has released its reference to the intel_context and has completed
474 	 * the DMA_FENCE_FLAG_SIGNALED_BIT/I915_FENCE_FLAG_SIGNAL dance (if
475 	 * required).
476 	 */
477 	spin_lock(&b->irq_lock);
478 	if (test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags)) {
479 		struct intel_context *ce = rq->context;
480 
481 		list_del(&rq->signal_link);
482 		if (list_empty(&ce->signals))
483 			remove_signaling_context(b, ce);
484 
485 		clear_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
486 		i915_request_put(rq);
487 	}
488 	spin_unlock(&b->irq_lock);
489 }
490 
491 static void print_signals(struct intel_breadcrumbs *b, struct drm_printer *p)
492 {
493 	struct intel_context *ce;
494 	struct i915_request *rq;
495 
496 	drm_printf(p, "Signals:\n");
497 
498 	spin_lock_irq(&b->irq_lock);
499 	list_for_each_entry(ce, &b->signalers, signal_link) {
500 		list_for_each_entry(rq, &ce->signals, signal_link) {
501 			drm_printf(p, "\t[%llx:%llx%s] @ %dms\n",
502 				   rq->fence.context, rq->fence.seqno,
503 				   i915_request_completed(rq) ? "!" :
504 				   i915_request_started(rq) ? "*" :
505 				   "",
506 				   jiffies_to_msecs(jiffies - rq->emitted_jiffies));
507 		}
508 	}
509 	spin_unlock_irq(&b->irq_lock);
510 }
511 
512 void intel_engine_print_breadcrumbs(struct intel_engine_cs *engine,
513 				    struct drm_printer *p)
514 {
515 	struct intel_breadcrumbs *b;
516 
517 	b = engine->breadcrumbs;
518 	if (!b)
519 		return;
520 
521 	drm_printf(p, "IRQ: %s\n", enableddisabled(b->irq_armed));
522 	if (!list_empty(&b->signalers))
523 		print_signals(b, p);
524 }
525