intel_breadcrumbs.c 12.2 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_gt_pm.h"
33
#include "intel_gt_requests.h"
34

35 36 37 38 39 40
static void irq_enable(struct intel_engine_cs *engine)
{
	if (!engine->irq_enable)
		return;

	/* Caller disables interrupts */
41
	spin_lock(&engine->gt->irq_lock);
42
	engine->irq_enable(engine);
43
	spin_unlock(&engine->gt->irq_lock);
44 45 46
}

static void irq_disable(struct intel_engine_cs *engine)
47
{
48 49 50 51
	if (!engine->irq_disable)
		return;

	/* Caller disables interrupts */
52
	spin_lock(&engine->gt->irq_lock);
53
	engine->irq_disable(engine);
54
	spin_unlock(&engine->gt->irq_lock);
55
}
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
static void __intel_breadcrumbs_arm_irq(struct intel_breadcrumbs *b)
{
	lockdep_assert_held(&b->irq_lock);

	if (!b->irq_engine || b->irq_armed)
		return;

	if (!intel_gt_pm_get_if_awake(b->irq_engine->gt))
		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);

	/*
	 * Since we are waiting on a request, the GPU should be busy
	 * and should have its own rpm reference. This is tracked
	 * by i915->gt.awake, we can forgo holding our own wakref
	 * for the interrupt as before i915->gt.awake is released (when
	 * the driver is idle) we disarm the breadcrumbs.
	 */

	if (!b->irq_enabled++)
		irq_enable(b->irq_engine);
}

87 88
static void __intel_breadcrumbs_disarm_irq(struct intel_breadcrumbs *b)
{
89 90
	lockdep_assert_held(&b->irq_lock);

91 92 93
	if (!b->irq_engine || !b->irq_armed)
		return;

94 95
	GEM_BUG_ON(!b->irq_enabled);
	if (!--b->irq_enabled)
96
		irq_disable(b->irq_engine);
97

98
	WRITE_ONCE(b->irq_armed, false);
99
	intel_gt_pm_put_async(b->irq_engine->gt);
100 101 102 103 104 105 106
}

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

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
__maybe_unused static bool
check_signal_order(struct intel_context *ce, struct i915_request *rq)
{
	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;
}

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
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
138 139
__dma_fence_signal__notify(struct dma_fence *fence,
			   const struct list_head *list)
140 141 142 143 144
{
	struct dma_fence_cb *cur, *tmp;

	lockdep_assert_held(fence->lock);

145
	list_for_each_entry_safe(cur, tmp, list, node) {
146 147 148 149 150
		INIT_LIST_HEAD(&cur->node);
		cur->func(fence, cur);
	}
}

151 152
static void add_retire(struct intel_breadcrumbs *b, struct intel_timeline *tl)
{
153 154
	if (b->irq_engine)
		intel_engine_add_retire(b->irq_engine, tl);
155 156
}

157
static bool __signal_request(struct i915_request *rq, struct list_head *signals)
158 159 160 161
{
	clear_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);

	if (!__dma_fence_signal(&rq->fence))
162
		return false;
163 164 165

	i915_request_get(rq);
	list_add_tail(&rq->signal_link, signals);
166
	return true;
167 168
}

169
static void signal_irq_work(struct irq_work *work)
170
{
171
	struct intel_breadcrumbs *b = container_of(work, typeof(*b), irq_work);
172
	const ktime_t timestamp = ktime_get();
173 174 175 176
	struct intel_context *ce, *cn;
	struct list_head *pos, *next;
	LIST_HEAD(signal);

177
	spin_lock(&b->irq_lock);
178

179
	if (list_empty(&b->signalers))
180 181
		__intel_breadcrumbs_disarm_irq(b);

182 183
	list_splice_init(&b->signaled_requests, &signal);

184 185 186 187 188 189 190
	list_for_each_entry_safe(ce, cn, &b->signalers, signal_link) {
		GEM_BUG_ON(list_empty(&ce->signals));

		list_for_each_safe(pos, next, &ce->signals) {
			struct i915_request *rq =
				list_entry(pos, typeof(*rq), signal_link);

191
			GEM_BUG_ON(!check_signal_order(ce, rq));
192 193 194 195 196 197 198 199
			if (!__request_completed(rq))
				break;

			/*
			 * Queue for execution after dropping the signaling
			 * spinlock as the callback chain may end up adding
			 * more signalers to the same context or engine.
			 */
200
			__signal_request(rq, &signal);
201 202
		}

203
		/*
204 205 206
		 * We process the list deletion in bulk, only using a list_add
		 * (not list_move) above but keeping the status of
		 * rq->signal_link known with the I915_FENCE_FLAG_SIGNAL bit.
207
		 */
208 209 210
		if (!list_is_first(pos, &ce->signals)) {
			/* Advance the list to the first incomplete request */
			__list_del_many(&ce->signals, pos);
211
			if (&ce->signals == pos) { /* now empty */
212
				list_del_init(&ce->signal_link);
213
				add_retire(b, ce->timeline);
214
			}
215 216 217
		}
	}

218
	spin_unlock(&b->irq_lock);
219 220 221 222

	list_for_each_safe(pos, next, &signal) {
		struct i915_request *rq =
			list_entry(pos, typeof(*rq), signal_link);
223
		struct list_head cb_list;
224

225
		spin_lock(&rq->lock);
226 227 228
		list_replace(&rq->fence.cb_list, &cb_list);
		__dma_fence_signal__timestamp(&rq->fence, timestamp);
		__dma_fence_signal__notify(&rq->fence, &cb_list);
229
		spin_unlock(&rq->lock);
230

231
		i915_request_put(rq);
232
	}
233 234
}

235 236
struct intel_breadcrumbs *
intel_breadcrumbs_create(struct intel_engine_cs *irq_engine)
237
{
238 239 240 241 242
	struct intel_breadcrumbs *b;

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

244 245
	spin_lock_init(&b->irq_lock);
	INIT_LIST_HEAD(&b->signalers);
246
	INIT_LIST_HEAD(&b->signaled_requests);
247

248
	init_irq_work(&b->irq_work, signal_irq_work);
249 250 251 252

	b->irq_engine = irq_engine;

	return b;
253 254
}

255
void intel_breadcrumbs_reset(struct intel_breadcrumbs *b)
256
{
257
	unsigned long flags;
258

259 260 261
	if (!b->irq_engine)
		return;

262
	spin_lock_irqsave(&b->irq_lock, flags);
263

264
	if (b->irq_enabled)
265
		irq_enable(b->irq_engine);
266
	else
267
		irq_disable(b->irq_engine);
268

269
	spin_unlock_irqrestore(&b->irq_lock, flags);
270 271 272 273 274 275 276 277 278 279 280 281
}

void intel_breadcrumbs_park(struct intel_breadcrumbs *b)
{
	unsigned long flags;

	if (!READ_ONCE(b->irq_armed))
		return;

	spin_lock_irqsave(&b->irq_lock, flags);
	__intel_breadcrumbs_disarm_irq(b);
	spin_unlock_irqrestore(&b->irq_lock, flags);
282 283
}

284
void intel_breadcrumbs_free(struct intel_breadcrumbs *b)
285
{
286
	kfree(b);
287 288
}

289 290
static void insert_breadcrumb(struct i915_request *rq,
			      struct intel_breadcrumbs *b)
291
{
292 293
	struct intel_context *ce = rq->context;
	struct list_head *pos;
294

295 296
	if (test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags))
		return;
297

298 299 300 301 302 303 304 305 306 307 308
	/*
	 * 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)) {
		if (__signal_request(rq, &b->signaled_requests))
			irq_work_queue(&b->irq_work);
		return;
	}

309
	__intel_breadcrumbs_arm_irq(b);
310

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	/*
	 * 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);
328

329 330 331 332 333 334 335 336
		if (i915_seqno_passed(rq->fence.seqno, it->fence.seqno))
			break;
	}
	list_add(&rq->signal_link, pos);
	if (pos == &ce->signals) /* catch transitions from empty list */
		list_move_tail(&ce->signal_link, &b->signalers);
	GEM_BUG_ON(!check_signal_order(ce, rq));
	set_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
337 338 339 340

	/* Check after attaching to irq, interrupt may have already fired. */
	if (__request_completed(rq))
		irq_work_queue(&b->irq_work);
341
}
342

343 344 345
bool i915_request_enable_breadcrumb(struct i915_request *rq)
{
	struct intel_breadcrumbs *b;
346

347 348 349
	/* Serialises with i915_request_retire() using rq->lock */
	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags))
		return true;
350

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
	/*
	 * 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;

	/*
	 * rq->engine is locked by rq->engine->active.lock. That however
	 * is not known until after rq->engine has been dereferenced and
	 * the lock acquired. Hence we acquire the lock and then validate
	 * that rq->engine still matches the lock we hold for it.
	 *
	 * Here, we are using the breadcrumb lock as a proxy for the
	 * rq->engine->active.lock, and we know that since the breadcrumb
	 * will be serialised within i915_request_submit/i915_request_unsubmit,
	 * the engine cannot change while active as long as we hold the
	 * breadcrumb lock on that engine.
	 *
	 * From the dma_fence_enable_signaling() path, we are outside of the
	 * request submit/unsubmit path, and so we must be more careful to
	 * acquire the right lock.
	 */
376
	b = READ_ONCE(rq->engine)->breadcrumbs;
377
	spin_lock(&b->irq_lock);
378
	while (unlikely(b != READ_ONCE(rq->engine)->breadcrumbs)) {
379
		spin_unlock(&b->irq_lock);
380
		b = READ_ONCE(rq->engine)->breadcrumbs;
381
		spin_lock(&b->irq_lock);
382 383
	}

384 385 386 387 388 389 390 391 392 393 394 395
	/*
	 * Now that we are finally serialised with request submit/unsubmit,
	 * [with b->irq_lock] and with i915_request_retire() [via checking
	 * SIGNALED with rq->lock] confirm the request is indeed active. If
	 * it is no longer active, the breadcrumb will be attached upon
	 * i915_request_submit().
	 */
	if (test_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags))
		insert_breadcrumb(rq, b);

	spin_unlock(&b->irq_lock);

396
	return true;
397 398
}

399
void i915_request_cancel_breadcrumb(struct i915_request *rq)
400
{
401
	struct intel_breadcrumbs *b = rq->engine->breadcrumbs;
402

403 404 405 406 407 408
	/*
	 * We must wait for b->irq_lock so that we know the interrupt handler
	 * has released its reference to the intel_context and has completed
	 * the DMA_FENCE_FLAG_SIGNALED_BIT/I915_FENCE_FLAG_SIGNAL dance (if
	 * required).
	 */
409 410
	spin_lock(&b->irq_lock);
	if (test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags)) {
411
		struct intel_context *ce = rq->context;
412

413 414 415
		list_del(&rq->signal_link);
		if (list_empty(&ce->signals))
			list_del_init(&ce->signal_link);
416

417 418 419
		clear_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
	}
	spin_unlock(&b->irq_lock);
420 421
}

422 423
void intel_engine_print_breadcrumbs(struct intel_engine_cs *engine,
				    struct drm_printer *p)
424
{
425
	struct intel_breadcrumbs *b = engine->breadcrumbs;
426 427
	struct intel_context *ce;
	struct i915_request *rq;
428

429
	if (!b || list_empty(&b->signalers))
430
		return;
431

432
	drm_printf(p, "Signals:\n");
433

434 435 436 437 438 439 440 441 442 443 444 445
	spin_lock_irq(&b->irq_lock);
	list_for_each_entry(ce, &b->signalers, signal_link) {
		list_for_each_entry(rq, &ce->signals, signal_link) {
			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));
		}
	}
	spin_unlock_irq(&b->irq_lock);
446
}