xref: /linux/drivers/gpu/drm/i915/gt/intel_breadcrumbs.c (revision c88fb897c1fb5a590dc6353ac4b01c8f46a347b3)
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 intel_breadcrumbs_disarm_irq(struct intel_breadcrumbs *b)
105 {
106 	spin_lock(&b->irq_lock);
107 	if (b->irq_armed)
108 		__intel_breadcrumbs_disarm_irq(b);
109 	spin_unlock(&b->irq_lock);
110 }
111 
112 static void add_signaling_context(struct intel_breadcrumbs *b,
113 				  struct intel_context *ce)
114 {
115 	lockdep_assert_held(&ce->signal_lock);
116 
117 	spin_lock(&b->signalers_lock);
118 	list_add_rcu(&ce->signal_link, &b->signalers);
119 	spin_unlock(&b->signalers_lock);
120 }
121 
122 static bool remove_signaling_context(struct intel_breadcrumbs *b,
123 				     struct intel_context *ce)
124 {
125 	lockdep_assert_held(&ce->signal_lock);
126 
127 	if (!list_empty(&ce->signals))
128 		return false;
129 
130 	spin_lock(&b->signalers_lock);
131 	list_del_rcu(&ce->signal_link);
132 	spin_unlock(&b->signalers_lock);
133 
134 	return true;
135 }
136 
137 __maybe_unused static bool
138 check_signal_order(struct intel_context *ce, struct i915_request *rq)
139 {
140 	if (rq->context != ce)
141 		return false;
142 
143 	if (!list_is_last(&rq->signal_link, &ce->signals) &&
144 	    i915_seqno_passed(rq->fence.seqno,
145 			      list_next_entry(rq, signal_link)->fence.seqno))
146 		return false;
147 
148 	if (!list_is_first(&rq->signal_link, &ce->signals) &&
149 	    i915_seqno_passed(list_prev_entry(rq, signal_link)->fence.seqno,
150 			      rq->fence.seqno))
151 		return false;
152 
153 	return true;
154 }
155 
156 static bool
157 __dma_fence_signal(struct dma_fence *fence)
158 {
159 	return !test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
160 }
161 
162 static void
163 __dma_fence_signal__timestamp(struct dma_fence *fence, ktime_t timestamp)
164 {
165 	fence->timestamp = timestamp;
166 	set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
167 	trace_dma_fence_signaled(fence);
168 }
169 
170 static void
171 __dma_fence_signal__notify(struct dma_fence *fence,
172 			   const struct list_head *list)
173 {
174 	struct dma_fence_cb *cur, *tmp;
175 
176 	lockdep_assert_held(fence->lock);
177 
178 	list_for_each_entry_safe(cur, tmp, list, node) {
179 		INIT_LIST_HEAD(&cur->node);
180 		cur->func(fence, cur);
181 	}
182 }
183 
184 static void add_retire(struct intel_breadcrumbs *b, struct intel_timeline *tl)
185 {
186 	if (b->irq_engine)
187 		intel_engine_add_retire(b->irq_engine, tl);
188 }
189 
190 static struct llist_node *
191 slist_add(struct llist_node *node, struct llist_node *head)
192 {
193 	node->next = head;
194 	return node;
195 }
196 
197 static void signal_irq_work(struct irq_work *work)
198 {
199 	struct intel_breadcrumbs *b = container_of(work, typeof(*b), irq_work);
200 	const ktime_t timestamp = ktime_get();
201 	struct llist_node *signal, *sn;
202 	struct intel_context *ce;
203 
204 	signal = NULL;
205 	if (unlikely(!llist_empty(&b->signaled_requests)))
206 		signal = llist_del_all(&b->signaled_requests);
207 
208 	/*
209 	 * Keep the irq armed until the interrupt after all listeners are gone.
210 	 *
211 	 * Enabling/disabling the interrupt is rather costly, roughly a couple
212 	 * of hundred microseconds. If we are proactive and enable/disable
213 	 * the interrupt around every request that wants a breadcrumb, we
214 	 * quickly drown in the extra orders of magnitude of latency imposed
215 	 * on request submission.
216 	 *
217 	 * So we try to be lazy, and keep the interrupts enabled until no
218 	 * more listeners appear within a breadcrumb interrupt interval (that
219 	 * is until a request completes that no one cares about). The
220 	 * observation is that listeners come in batches, and will often
221 	 * listen to a bunch of requests in succession. Though note on icl+,
222 	 * interrupts are always enabled due to concerns with rc6 being
223 	 * dysfunctional with per-engine interrupt masking.
224 	 *
225 	 * We also try to avoid raising too many interrupts, as they may
226 	 * be generated by userspace batches and it is unfortunately rather
227 	 * too easy to drown the CPU under a flood of GPU interrupts. Thus
228 	 * whenever no one appears to be listening, we turn off the interrupts.
229 	 * Fewer interrupts should conserve power -- at the very least, fewer
230 	 * interrupt draw less ire from other users of the system and tools
231 	 * like powertop.
232 	 */
233 	if (!signal && READ_ONCE(b->irq_armed) && list_empty(&b->signalers))
234 		intel_breadcrumbs_disarm_irq(b);
235 
236 	rcu_read_lock();
237 	list_for_each_entry_rcu(ce, &b->signalers, signal_link) {
238 		struct i915_request *rq;
239 
240 		list_for_each_entry_rcu(rq, &ce->signals, signal_link) {
241 			bool release;
242 
243 			if (!__i915_request_is_complete(rq))
244 				break;
245 
246 			if (!test_and_clear_bit(I915_FENCE_FLAG_SIGNAL,
247 						&rq->fence.flags))
248 				break;
249 
250 			/*
251 			 * Queue for execution after dropping the signaling
252 			 * spinlock as the callback chain may end up adding
253 			 * more signalers to the same context or engine.
254 			 */
255 			spin_lock(&ce->signal_lock);
256 			list_del_rcu(&rq->signal_link);
257 			release = remove_signaling_context(b, ce);
258 			spin_unlock(&ce->signal_lock);
259 
260 			if (__dma_fence_signal(&rq->fence))
261 				/* We own signal_node now, xfer to local list */
262 				signal = slist_add(&rq->signal_node, signal);
263 			else
264 				i915_request_put(rq);
265 
266 			if (release) {
267 				add_retire(b, ce->timeline);
268 				intel_context_put(ce);
269 			}
270 		}
271 	}
272 	rcu_read_unlock();
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 	b->irq_engine = irq_engine;
302 
303 	spin_lock_init(&b->signalers_lock);
304 	INIT_LIST_HEAD(&b->signalers);
305 	init_llist_head(&b->signaled_requests);
306 
307 	spin_lock_init(&b->irq_lock);
308 	init_irq_work(&b->irq_work, signal_irq_work);
309 
310 	return b;
311 }
312 
313 void intel_breadcrumbs_reset(struct intel_breadcrumbs *b)
314 {
315 	unsigned long flags;
316 
317 	if (!b->irq_engine)
318 		return;
319 
320 	spin_lock_irqsave(&b->irq_lock, flags);
321 
322 	if (b->irq_enabled)
323 		irq_enable(b->irq_engine);
324 	else
325 		irq_disable(b->irq_engine);
326 
327 	spin_unlock_irqrestore(&b->irq_lock, flags);
328 }
329 
330 void intel_breadcrumbs_park(struct intel_breadcrumbs *b)
331 {
332 	/* Kick the work once more to drain the signalers */
333 	irq_work_sync(&b->irq_work);
334 	while (unlikely(READ_ONCE(b->irq_armed))) {
335 		local_irq_disable();
336 		signal_irq_work(&b->irq_work);
337 		local_irq_enable();
338 		cond_resched();
339 	}
340 	GEM_BUG_ON(!list_empty(&b->signalers));
341 }
342 
343 void intel_breadcrumbs_free(struct intel_breadcrumbs *b)
344 {
345 	irq_work_sync(&b->irq_work);
346 	GEM_BUG_ON(!list_empty(&b->signalers));
347 	GEM_BUG_ON(b->irq_armed);
348 	kfree(b);
349 }
350 
351 static void irq_signal_request(struct i915_request *rq,
352 			       struct intel_breadcrumbs *b)
353 {
354 	if (!__dma_fence_signal(&rq->fence))
355 		return;
356 
357 	i915_request_get(rq);
358 	if (llist_add(&rq->signal_node, &b->signaled_requests))
359 		irq_work_queue(&b->irq_work);
360 }
361 
362 static void insert_breadcrumb(struct i915_request *rq)
363 {
364 	struct intel_breadcrumbs *b = READ_ONCE(rq->engine)->breadcrumbs;
365 	struct intel_context *ce = rq->context;
366 	struct list_head *pos;
367 
368 	if (test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags))
369 		return;
370 
371 	/*
372 	 * If the request is already completed, we can transfer it
373 	 * straight onto a signaled list, and queue the irq worker for
374 	 * its signal completion.
375 	 */
376 	if (__i915_request_is_complete(rq)) {
377 		irq_signal_request(rq, b);
378 		return;
379 	}
380 
381 	if (list_empty(&ce->signals)) {
382 		intel_context_get(ce);
383 		add_signaling_context(b, ce);
384 		pos = &ce->signals;
385 	} else {
386 		/*
387 		 * We keep the seqno in retirement order, so we can break
388 		 * inside intel_engine_signal_breadcrumbs as soon as we've
389 		 * passed the last completed request (or seen a request that
390 		 * hasn't event started). We could walk the timeline->requests,
391 		 * but keeping a separate signalers_list has the advantage of
392 		 * hopefully being much smaller than the full list and so
393 		 * provides faster iteration and detection when there are no
394 		 * more interrupts required for this context.
395 		 *
396 		 * We typically expect to add new signalers in order, so we
397 		 * start looking for our insertion point from the tail of
398 		 * the list.
399 		 */
400 		list_for_each_prev(pos, &ce->signals) {
401 			struct i915_request *it =
402 				list_entry(pos, typeof(*it), signal_link);
403 
404 			if (i915_seqno_passed(rq->fence.seqno, it->fence.seqno))
405 				break;
406 		}
407 	}
408 
409 	i915_request_get(rq);
410 	list_add_rcu(&rq->signal_link, pos);
411 	GEM_BUG_ON(!check_signal_order(ce, rq));
412 	GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags));
413 	set_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
414 
415 	/*
416 	 * Defer enabling the interrupt to after HW submission and recheck
417 	 * the request as it may have completed and raised the interrupt as
418 	 * we were attaching it into the lists.
419 	 */
420 	irq_work_queue(&b->irq_work);
421 }
422 
423 bool i915_request_enable_breadcrumb(struct i915_request *rq)
424 {
425 	struct intel_context *ce = rq->context;
426 
427 	/* Serialises with i915_request_retire() using rq->lock */
428 	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags))
429 		return true;
430 
431 	/*
432 	 * Peek at i915_request_submit()/i915_request_unsubmit() status.
433 	 *
434 	 * If the request is not yet active (and not signaled), we will
435 	 * attach the breadcrumb later.
436 	 */
437 	if (!test_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags))
438 		return true;
439 
440 	spin_lock(&ce->signal_lock);
441 	if (test_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags))
442 		insert_breadcrumb(rq);
443 	spin_unlock(&ce->signal_lock);
444 
445 	return true;
446 }
447 
448 void i915_request_cancel_breadcrumb(struct i915_request *rq)
449 {
450 	struct intel_breadcrumbs *b = READ_ONCE(rq->engine)->breadcrumbs;
451 	struct intel_context *ce = rq->context;
452 	bool release;
453 
454 	spin_lock(&ce->signal_lock);
455 	if (!test_and_clear_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags)) {
456 		spin_unlock(&ce->signal_lock);
457 		return;
458 	}
459 
460 	list_del_rcu(&rq->signal_link);
461 	release = remove_signaling_context(b, ce);
462 	spin_unlock(&ce->signal_lock);
463 	if (release)
464 		intel_context_put(ce);
465 
466 	if (__i915_request_is_complete(rq))
467 		irq_signal_request(rq, b);
468 
469 	i915_request_put(rq);
470 }
471 
472 static void print_signals(struct intel_breadcrumbs *b, struct drm_printer *p)
473 {
474 	struct intel_context *ce;
475 	struct i915_request *rq;
476 
477 	drm_printf(p, "Signals:\n");
478 
479 	rcu_read_lock();
480 	list_for_each_entry_rcu(ce, &b->signalers, signal_link) {
481 		list_for_each_entry_rcu(rq, &ce->signals, signal_link)
482 			drm_printf(p, "\t[%llx:%llx%s] @ %dms\n",
483 				   rq->fence.context, rq->fence.seqno,
484 				   i915_request_completed(rq) ? "!" :
485 				   i915_request_started(rq) ? "*" :
486 				   "",
487 				   jiffies_to_msecs(jiffies - rq->emitted_jiffies));
488 	}
489 	rcu_read_unlock();
490 }
491 
492 void intel_engine_print_breadcrumbs(struct intel_engine_cs *engine,
493 				    struct drm_printer *p)
494 {
495 	struct intel_breadcrumbs *b;
496 
497 	b = engine->breadcrumbs;
498 	if (!b)
499 		return;
500 
501 	drm_printf(p, "IRQ: %s\n", enableddisabled(b->irq_armed));
502 	if (!list_empty(&b->signalers))
503 		print_signals(b, p);
504 }
505