intel_breadcrumbs.c 13.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright © 2015 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 */

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 53 54 55
	if (!engine->irq_disable)
		return;

	/* 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 62
static void __intel_breadcrumbs_arm_irq(struct intel_breadcrumbs *b)
{
63 64 65 66 67
	/*
	 * Since we are waiting on a request, the GPU should be busy
	 * and should have its own rpm reference.
	 */
	if (GEM_WARN_ON(!intel_gt_pm_get_if_awake(b->irq_engine->gt)))
68 69 70 71 72 73 74 75 76 77
		return;

	/*
	 * The breadcrumb irq will be disarmed on the interrupt after the
	 * waiters are signaled. This gives us a single interrupt window in
	 * which we can add a new waiter and avoid the cost of re-enabling
	 * the irq.
	 */
	WRITE_ONCE(b->irq_armed, true);

78 79 80
	/* Requests may have completed before we could enable the interrupt. */
	if (!b->irq_enabled++ && irq_enable(b->irq_engine))
		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 87
		return;

88 89 90 91 92 93 94 95
	spin_lock(&b->irq_lock);
	if (!b->irq_armed)
		__intel_breadcrumbs_arm_irq(b);
	spin_unlock(&b->irq_lock);
}

static void __intel_breadcrumbs_disarm_irq(struct intel_breadcrumbs *b)
{
96 97
	GEM_BUG_ON(!b->irq_enabled);
	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 105 106 107 108 109 110 111
static void intel_breadcrumbs_disarm_irq(struct intel_breadcrumbs *b)
{
	spin_lock(&b->irq_lock);
	if (b->irq_armed)
		__intel_breadcrumbs_disarm_irq(b);
	spin_unlock(&b->irq_lock);
}

112 113 114
static void add_signaling_context(struct intel_breadcrumbs *b,
				  struct intel_context *ce)
{
115 116 117 118 119
	lockdep_assert_held(&ce->signal_lock);

	spin_lock(&b->signalers_lock);
	list_add_rcu(&ce->signal_link, &b->signalers);
	spin_unlock(&b->signalers_lock);
120 121
}

122
static bool remove_signaling_context(struct intel_breadcrumbs *b,
123 124
				     struct intel_context *ce)
{
125 126 127 128 129 130 131 132 133 134
	lockdep_assert_held(&ce->signal_lock);

	if (!list_empty(&ce->signals))
		return false;

	spin_lock(&b->signalers_lock);
	list_del_rcu(&ce->signal_link);
	spin_unlock(&b->signalers_lock);

	return true;
135 136
}

137 138 139 140 141
static inline bool __request_completed(const struct i915_request *rq)
{
	return i915_seqno_passed(__hwsp_seqno(rq), rq->fence.seqno);
}

142 143 144
__maybe_unused static bool
check_signal_order(struct intel_context *ce, struct i915_request *rq)
{
145 146 147
	if (rq->context != ce)
		return false;

148 149 150 151 152 153 154 155 156 157 158 159 160
	if (!list_is_last(&rq->signal_link, &ce->signals) &&
	    i915_seqno_passed(rq->fence.seqno,
			      list_next_entry(rq, signal_link)->fence.seqno))
		return false;

	if (!list_is_first(&rq->signal_link, &ce->signals) &&
	    i915_seqno_passed(list_prev_entry(rq, signal_link)->fence.seqno,
			      rq->fence.seqno))
		return false;

	return true;
}

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
static bool
__dma_fence_signal(struct dma_fence *fence)
{
	return !test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
}

static void
__dma_fence_signal__timestamp(struct dma_fence *fence, ktime_t timestamp)
{
	fence->timestamp = timestamp;
	set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
	trace_dma_fence_signaled(fence);
}

static void
176 177
__dma_fence_signal__notify(struct dma_fence *fence,
			   const struct list_head *list)
178 179 180 181 182
{
	struct dma_fence_cb *cur, *tmp;

	lockdep_assert_held(fence->lock);

183
	list_for_each_entry_safe(cur, tmp, list, node) {
184 185 186 187 188
		INIT_LIST_HEAD(&cur->node);
		cur->func(fence, cur);
	}
}

189 190
static void add_retire(struct intel_breadcrumbs *b, struct intel_timeline *tl)
{
191 192
	if (b->irq_engine)
		intel_engine_add_retire(b->irq_engine, tl);
193 194
}

195
static bool __signal_request(struct i915_request *rq)
196
{
197 198
	GEM_BUG_ON(test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags));

199 200
	if (!__dma_fence_signal(&rq->fence)) {
		i915_request_put(rq);
201
		return false;
202
	}
203

204
	return true;
205 206
}

207 208 209 210 211 212 213
static struct llist_node *
slist_add(struct llist_node *node, struct llist_node *head)
{
	node->next = head;
	return node;
}

214
static void signal_irq_work(struct irq_work *work)
215
{
216
	struct intel_breadcrumbs *b = container_of(work, typeof(*b), irq_work);
217
	const ktime_t timestamp = ktime_get();
218
	struct llist_node *signal, *sn;
219
	struct intel_context *ce;
220 221 222 223

	signal = NULL;
	if (unlikely(!llist_empty(&b->signaled_requests)))
		signal = llist_del_all(&b->signaled_requests);
224

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	/*
	 * Keep the irq armed until the interrupt after all listeners are gone.
	 *
	 * Enabling/disabling the interrupt is rather costly, roughly a couple
	 * of hundred microseconds. If we are proactive and enable/disable
	 * the interrupt around every request that wants a breadcrumb, we
	 * quickly drown in the extra orders of magnitude of latency imposed
	 * on request submission.
	 *
	 * So we try to be lazy, and keep the interrupts enabled until no
	 * more listeners appear within a breadcrumb interrupt interval (that
	 * is until a request completes that no one cares about). The
	 * observation is that listeners come in batches, and will often
	 * listen to a bunch of requests in succession. Though note on icl+,
	 * interrupts are always enabled due to concerns with rc6 being
	 * dysfunctional with per-engine interrupt masking.
	 *
	 * We also try to avoid raising too many interrupts, as they may
	 * be generated by userspace batches and it is unfortunately rather
	 * too easy to drown the CPU under a flood of GPU interrupts. Thus
	 * whenever no one appears to be listening, we turn off the interrupts.
	 * Fewer interrupts should conserve power -- at the very least, fewer
	 * interrupt draw less ire from other users of the system and tools
	 * like powertop.
	 */
250 251
	if (!signal && READ_ONCE(b->irq_armed) && list_empty(&b->signalers))
		intel_breadcrumbs_disarm_irq(b);
252

253 254 255
	rcu_read_lock();
	list_for_each_entry_rcu(ce, &b->signalers, signal_link) {
		struct i915_request *rq;
256

257 258
		list_for_each_entry_rcu(rq, &ce->signals, signal_link) {
			bool release;
259 260 261 262

			if (!__request_completed(rq))
				break;

263 264 265 266
			if (!test_and_clear_bit(I915_FENCE_FLAG_SIGNAL,
						&rq->fence.flags))
				break;

267 268 269 270 271
			/*
			 * Queue for execution after dropping the signaling
			 * spinlock as the callback chain may end up adding
			 * more signalers to the same context or engine.
			 */
272 273 274 275 276
			spin_lock(&ce->signal_lock);
			list_del_rcu(&rq->signal_link);
			release = remove_signaling_context(b, ce);
			spin_unlock(&ce->signal_lock);

277 278 279
			if (__signal_request(rq))
				/* We own signal_node now, xfer to local list */
				signal = slist_add(&rq->signal_node, signal);
280

281
			if (release) {
282
				add_retire(b, ce->timeline);
283
				intel_context_put(ce);
284
			}
285 286
		}
	}
287
	rcu_read_unlock();
288

289
	llist_for_each_safe(signal, sn, signal) {
290
		struct i915_request *rq =
291
			llist_entry(signal, typeof(*rq), signal_node);
292
		struct list_head cb_list;
293

294
		spin_lock(&rq->lock);
295 296 297
		list_replace(&rq->fence.cb_list, &cb_list);
		__dma_fence_signal__timestamp(&rq->fence, timestamp);
		__dma_fence_signal__notify(&rq->fence, &cb_list);
298
		spin_unlock(&rq->lock);
299

300
		i915_request_put(rq);
301
	}
302 303 304

	if (!READ_ONCE(b->irq_armed) && !list_empty(&b->signalers))
		intel_breadcrumbs_arm_irq(b);
305 306
}

307 308
struct intel_breadcrumbs *
intel_breadcrumbs_create(struct intel_engine_cs *irq_engine)
309
{
310 311 312 313 314
	struct intel_breadcrumbs *b;

	b = kzalloc(sizeof(*b), GFP_KERNEL);
	if (!b)
		return NULL;
315

316 317 318
	b->irq_engine = irq_engine;

	spin_lock_init(&b->signalers_lock);
319
	INIT_LIST_HEAD(&b->signalers);
320
	init_llist_head(&b->signaled_requests);
321

322
	spin_lock_init(&b->irq_lock);
323
	init_irq_work(&b->irq_work, signal_irq_work);
324 325

	return b;
326 327
}

328
void intel_breadcrumbs_reset(struct intel_breadcrumbs *b)
329
{
330
	unsigned long flags;
331

332 333 334
	if (!b->irq_engine)
		return;

335
	spin_lock_irqsave(&b->irq_lock, flags);
336

337
	if (b->irq_enabled)
338
		irq_enable(b->irq_engine);
339
	else
340
		irq_disable(b->irq_engine);
341

342
	spin_unlock_irqrestore(&b->irq_lock, flags);
343 344 345 346
}

void intel_breadcrumbs_park(struct intel_breadcrumbs *b)
{
347 348 349 350 351 352 353 354 355
	/* Kick the work once more to drain the signalers */
	irq_work_sync(&b->irq_work);
	while (unlikely(READ_ONCE(b->irq_armed))) {
		local_irq_disable();
		signal_irq_work(&b->irq_work);
		local_irq_enable();
		cond_resched();
	}
	GEM_BUG_ON(!list_empty(&b->signalers));
356 357
}

358
void intel_breadcrumbs_free(struct intel_breadcrumbs *b)
359
{
360 361 362
	irq_work_sync(&b->irq_work);
	GEM_BUG_ON(!list_empty(&b->signalers));
	GEM_BUG_ON(b->irq_armed);
363
	kfree(b);
364 365
}

366
static void insert_breadcrumb(struct i915_request *rq)
367
{
368
	struct intel_breadcrumbs *b = READ_ONCE(rq->engine)->breadcrumbs;
369 370
	struct intel_context *ce = rq->context;
	struct list_head *pos;
371

372 373
	if (test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags))
		return;
374

375 376
	i915_request_get(rq);

377 378 379 380 381 382
	/*
	 * If the request is already completed, we can transfer it
	 * straight onto a signaled list, and queue the irq worker for
	 * its signal completion.
	 */
	if (__request_completed(rq)) {
383 384
		if (__signal_request(rq) &&
		    llist_add(&rq->signal_node, &b->signaled_requests))
385 386 387 388
			irq_work_queue(&b->irq_work);
		return;
	}

389
	if (list_empty(&ce->signals)) {
390
		intel_context_get(ce);
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
		add_signaling_context(b, ce);
		pos = &ce->signals;
	} else {
		/*
		 * We keep the seqno in retirement order, so we can break
		 * inside intel_engine_signal_breadcrumbs as soon as we've
		 * passed the last completed request (or seen a request that
		 * hasn't event started). We could walk the timeline->requests,
		 * but keeping a separate signalers_list has the advantage of
		 * hopefully being much smaller than the full list and so
		 * provides faster iteration and detection when there are no
		 * more interrupts required for this context.
		 *
		 * We typically expect to add new signalers in order, so we
		 * start looking for our insertion point from the tail of
		 * the list.
		 */
		list_for_each_prev(pos, &ce->signals) {
			struct i915_request *it =
				list_entry(pos, typeof(*it), signal_link);
411

412 413 414
			if (i915_seqno_passed(rq->fence.seqno, it->fence.seqno))
				break;
		}
415
	}
416
	list_add_rcu(&rq->signal_link, pos);
417
	GEM_BUG_ON(!check_signal_order(ce, rq));
418
	GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags));
419
	set_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
420

421 422 423 424 425 426
	/*
	 * Defer enabling the interrupt to after HW submission and recheck
	 * the request as it may have completed and raised the interrupt as
	 * we were attaching it into the lists.
	 */
	irq_work_queue(&b->irq_work);
427
}
428

429 430
bool i915_request_enable_breadcrumb(struct i915_request *rq)
{
431
	struct intel_context *ce = rq->context;
432

433 434 435
	/* Serialises with i915_request_retire() using rq->lock */
	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags))
		return true;
436

437 438 439 440 441 442 443 444 445
	/*
	 * Peek at i915_request_submit()/i915_request_unsubmit() status.
	 *
	 * If the request is not yet active (and not signaled), we will
	 * attach the breadcrumb later.
	 */
	if (!test_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags))
		return true;

446
	spin_lock(&ce->signal_lock);
447
	if (test_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags))
448 449
		insert_breadcrumb(rq);
	spin_unlock(&ce->signal_lock);
450

451
	return true;
452 453
}

454
void i915_request_cancel_breadcrumb(struct i915_request *rq)
455
{
456 457
	struct intel_context *ce = rq->context;
	bool release;
458

459 460
	if (!test_and_clear_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags))
		return;
461

462 463 464 465 466 467
	spin_lock(&ce->signal_lock);
	list_del_rcu(&rq->signal_link);
	release = remove_signaling_context(rq->engine->breadcrumbs, ce);
	spin_unlock(&ce->signal_lock);
	if (release)
		intel_context_put(ce);
468

469
	i915_request_put(rq);
470 471
}

472
static void print_signals(struct intel_breadcrumbs *b, struct drm_printer *p)
473
{
474 475
	struct intel_context *ce;
	struct i915_request *rq;
476

477
	drm_printf(p, "Signals:\n");
478

479 480 481
	rcu_read_lock();
	list_for_each_entry_rcu(ce, &b->signalers, signal_link) {
		list_for_each_entry_rcu(rq, &ce->signals, signal_link)
482 483 484 485 486 487 488
			drm_printf(p, "\t[%llx:%llx%s] @ %dms\n",
				   rq->fence.context, rq->fence.seqno,
				   i915_request_completed(rq) ? "!" :
				   i915_request_started(rq) ? "*" :
				   "",
				   jiffies_to_msecs(jiffies - rq->emitted_jiffies));
	}
489
	rcu_read_unlock();
490
}
491 492 493 494 495 496 497 498 499 500 501 502 503 504

void intel_engine_print_breadcrumbs(struct intel_engine_cs *engine,
				    struct drm_printer *p)
{
	struct intel_breadcrumbs *b;

	b = engine->breadcrumbs;
	if (!b)
		return;

	drm_printf(p, "IRQ: %s\n", enableddisabled(b->irq_armed));
	if (!list_empty(&b->signalers))
		print_signals(b, p);
}