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

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

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

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

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

56 57
static void __intel_breadcrumbs_disarm_irq(struct intel_breadcrumbs *b)
{
58 59 60
	struct intel_engine_cs *engine =
		container_of(b, struct intel_engine_cs, breadcrumbs);

61 62
	lockdep_assert_held(&b->irq_lock);

63 64
	GEM_BUG_ON(!b->irq_enabled);
	if (!--b->irq_enabled)
65
		irq_disable(engine);
66

67
	WRITE_ONCE(b->irq_armed, false);
68
	intel_gt_pm_put_async(engine->gt);
69 70 71 72 73
}

void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
{
	struct intel_breadcrumbs *b = &engine->breadcrumbs;
74
	unsigned long flags;
75

76
	if (!READ_ONCE(b->irq_armed))
77 78
		return;

79
	spin_lock_irqsave(&b->irq_lock, flags);
80 81
	if (b->irq_armed)
		__intel_breadcrumbs_disarm_irq(b);
82
	spin_unlock_irqrestore(&b->irq_lock, flags);
83 84 85 86 87 88 89
}

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

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
__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;
}

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
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
121 122
__dma_fence_signal__notify(struct dma_fence *fence,
			   const struct list_head *list)
123 124 125 126 127
{
	struct dma_fence_cb *cur, *tmp;

	lockdep_assert_held(fence->lock);

128
	list_for_each_entry_safe(cur, tmp, list, node) {
129 130 131 132 133
		INIT_LIST_HEAD(&cur->node);
		cur->func(fence, cur);
	}
}

134 135 136 137 138
static void add_retire(struct intel_breadcrumbs *b, struct intel_timeline *tl)
{
	struct intel_engine_cs *engine =
		container_of(b, struct intel_engine_cs, breadcrumbs);

139 140 141
	if (unlikely(intel_engine_is_virtual(engine)))
		engine = intel_virtual_engine_get_sibling(engine, 0);

142 143 144
	intel_engine_add_retire(engine, tl);
}

145 146 147 148 149 150 151 152 153 154 155 156
static void __signal_request(struct i915_request *rq, struct list_head *signals)
{
	GEM_BUG_ON(!test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags));
	clear_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);

	if (!__dma_fence_signal(&rq->fence))
		return;

	i915_request_get(rq);
	list_add_tail(&rq->signal_link, signals);
}

157
static void signal_irq_work(struct irq_work *work)
158
{
159
	struct intel_breadcrumbs *b = container_of(work, typeof(*b), irq_work);
160
	const ktime_t timestamp = ktime_get();
161 162 163 164
	struct intel_context *ce, *cn;
	struct list_head *pos, *next;
	LIST_HEAD(signal);

165
	spin_lock(&b->irq_lock);
166 167 168 169

	if (b->irq_armed && list_empty(&b->signalers))
		__intel_breadcrumbs_disarm_irq(b);

170 171
	list_splice_init(&b->signaled_requests, &signal);

172 173 174 175 176 177 178
	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);

179
			GEM_BUG_ON(!check_signal_order(ce, rq));
180 181 182 183 184 185 186 187
			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.
			 */
188
			__signal_request(rq, &signal);
189 190
		}

191
		/*
192 193 194
		 * 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.
195
		 */
196 197 198
		if (!list_is_first(pos, &ce->signals)) {
			/* Advance the list to the first incomplete request */
			__list_del_many(&ce->signals, pos);
199
			if (&ce->signals == pos) { /* now empty */
200
				list_del_init(&ce->signal_link);
201
				add_retire(b, ce->timeline);
202
			}
203 204 205
		}
	}

206
	spin_unlock(&b->irq_lock);
207 208 209 210

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

213
		spin_lock(&rq->lock);
214 215 216
		list_replace(&rq->fence.cb_list, &cb_list);
		__dma_fence_signal__timestamp(&rq->fence, timestamp);
		__dma_fence_signal__notify(&rq->fence, &cb_list);
217
		spin_unlock(&rq->lock);
218

219
		i915_request_put(rq);
220
	}
221 222
}

223
static bool __intel_breadcrumbs_arm_irq(struct intel_breadcrumbs *b)
224 225 226
{
	struct intel_engine_cs *engine =
		container_of(b, struct intel_engine_cs, breadcrumbs);
227

228
	lockdep_assert_held(&b->irq_lock);
229
	if (b->irq_armed)
230 231 232 233
		return true;

	if (!intel_gt_pm_get_if_awake(engine->gt))
		return false;
234

235 236
	/*
	 * The breadcrumb irq will be disarmed on the interrupt after the
237 238 239 240
	 * 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.
	 */
241
	WRITE_ONCE(b->irq_armed, true);
242

243 244
	/*
	 * Since we are waiting on a request, the GPU should be busy
245 246 247 248
	 * 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.
249 250
	 */

251
	if (!b->irq_enabled++)
252
		irq_enable(engine);
253 254

	return true;
255 256
}

257
void intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
258
{
259
	struct intel_breadcrumbs *b = &engine->breadcrumbs;
260

261 262
	spin_lock_init(&b->irq_lock);
	INIT_LIST_HEAD(&b->signalers);
263
	INIT_LIST_HEAD(&b->signaled_requests);
264

265
	init_irq_work(&b->irq_work, signal_irq_work);
266 267
}

268
void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
269 270
{
	struct intel_breadcrumbs *b = &engine->breadcrumbs;
271
	unsigned long flags;
272

273
	spin_lock_irqsave(&b->irq_lock, flags);
274

275 276
	if (b->irq_enabled)
		irq_enable(engine);
277
	else
278
		irq_disable(engine);
279

280
	spin_unlock_irqrestore(&b->irq_lock, flags);
281 282
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
void intel_engine_transfer_stale_breadcrumbs(struct intel_engine_cs *engine,
					     struct intel_context *ce)
{
	struct intel_breadcrumbs *b = &engine->breadcrumbs;
	unsigned long flags;

	spin_lock_irqsave(&b->irq_lock, flags);
	if (!list_empty(&ce->signals)) {
		struct i915_request *rq, *next;

		/* Queue for executing the signal callbacks in the irq_work */
		list_for_each_entry_safe(rq, next, &ce->signals, signal_link) {
			GEM_BUG_ON(rq->engine != engine);
			GEM_BUG_ON(!__request_completed(rq));

			__signal_request(rq, &b->signaled_requests);
		}

		INIT_LIST_HEAD(&ce->signals);
		list_del_init(&ce->signal_link);

		irq_work_queue(&b->irq_work);
	}
	spin_unlock_irqrestore(&b->irq_lock, flags);
}

309
void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
310
{
311 312
}

313
bool i915_request_enable_breadcrumb(struct i915_request *rq)
314
{
315
	lockdep_assert_held(&rq->lock);
316

317 318 319
	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags))
		return true;

320 321
	if (test_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags)) {
		struct intel_breadcrumbs *b = &rq->engine->breadcrumbs;
322
		struct intel_context *ce = rq->context;
323
		struct list_head *pos;
324

325
		spin_lock(&b->irq_lock);
326 327 328

		if (test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags))
			goto unlock;
329

330 331
		if (!__intel_breadcrumbs_arm_irq(b))
			goto unlock;
332

333
		/*
334
		 * We keep the seqno in retirement order, so we can break
335 336 337
		 * 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,
338 339 340 341 342 343 344 345
		 * 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.
346
		 */
347 348 349
		list_for_each_prev(pos, &ce->signals) {
			struct i915_request *it =
				list_entry(pos, typeof(*it), signal_link);
350

351
			if (i915_seqno_passed(rq->fence.seqno, it->fence.seqno))
352 353
				break;
		}
354 355 356
		list_add(&rq->signal_link, pos);
		if (pos == &ce->signals) /* catch transitions from empty list */
			list_move_tail(&ce->signal_link, &b->signalers);
357
		GEM_BUG_ON(!check_signal_order(ce, rq));
358

359
		set_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
360
unlock:
361
		spin_unlock(&b->irq_lock);
362 363
	}

364
	return !__request_completed(rq);
365 366
}

367
void i915_request_cancel_breadcrumb(struct i915_request *rq)
368
{
369
	struct intel_breadcrumbs *b = &rq->engine->breadcrumbs;
370

371
	lockdep_assert_held(&rq->lock);
372

373 374 375 376 377 378
	/*
	 * 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).
	 */
379 380
	spin_lock(&b->irq_lock);
	if (test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags)) {
381
		struct intel_context *ce = rq->context;
382

383 384 385
		list_del(&rq->signal_link);
		if (list_empty(&ce->signals))
			list_del_init(&ce->signal_link);
386

387 388 389
		clear_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
	}
	spin_unlock(&b->irq_lock);
390 391
}

392 393
void intel_engine_print_breadcrumbs(struct intel_engine_cs *engine,
				    struct drm_printer *p)
394 395
{
	struct intel_breadcrumbs *b = &engine->breadcrumbs;
396 397
	struct intel_context *ce;
	struct i915_request *rq;
398

399 400
	if (list_empty(&b->signalers))
		return;
401

402
	drm_printf(p, "Signals:\n");
403

404 405 406 407 408 409 410 411 412 413 414 415
	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);
416
}