intel_breadcrumbs.c 9.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 <uapi/linux/sched/types.h>
27

28 29
#include "i915_drv.h"

30 31 32 33 34 35 36 37 38 39 40 41
static void irq_enable(struct intel_engine_cs *engine)
{
	if (!engine->irq_enable)
		return;

	/* Caller disables interrupts */
	spin_lock(&engine->i915->irq_lock);
	engine->irq_enable(engine);
	spin_unlock(&engine->i915->irq_lock);
}

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

	/* Caller disables interrupts */
	spin_lock(&engine->i915->irq_lock);
	engine->irq_disable(engine);
	spin_unlock(&engine->i915->irq_lock);
}
51

52 53
static void __intel_breadcrumbs_disarm_irq(struct intel_breadcrumbs *b)
{
54 55
	lockdep_assert_held(&b->irq_lock);

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
	GEM_BUG_ON(!b->irq_enabled);
	if (!--b->irq_enabled)
		irq_disable(container_of(b,
					 struct intel_engine_cs,
					 breadcrumbs));

	b->irq_armed = false;
}

void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
{
	struct intel_breadcrumbs *b = &engine->breadcrumbs;

	if (!b->irq_armed)
		return;

	spin_lock_irq(&b->irq_lock);
	if (b->irq_armed)
		__intel_breadcrumbs_disarm_irq(b);
	spin_unlock_irq(&b->irq_lock);
}

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

83
void intel_engine_breadcrumbs_irq(struct intel_engine_cs *engine)
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
{
	struct intel_breadcrumbs *b = &engine->breadcrumbs;
	struct intel_context *ce, *cn;
	struct list_head *pos, *next;
	LIST_HEAD(signal);

	spin_lock(&b->irq_lock);

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

	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);

			if (!__request_completed(rq))
				break;

			GEM_BUG_ON(!test_bit(I915_FENCE_FLAG_SIGNAL,
					     &rq->fence.flags));

			/*
			 * Queue for execution after dropping the signaling
			 * spinlock as the callback chain may end up adding
			 * more signalers to the same context or engine.
			 */
			i915_request_get(rq);
114 115 116 117 118 119 120 121

			/*
			 * We may race with direct invocation of
			 * dma_fence_signal(), e.g. i915_request_retire(),
			 * so we need to acquire our reference to the request
			 * before we cancel the breadcrumb.
			 */
			clear_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
122 123 124
			list_add_tail(&rq->signal_link, &signal);
		}

125
		/*
126 127 128
		 * 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.
129
		 */
130 131 132 133 134 135 136 137 138 139 140 141 142
		if (!list_is_first(pos, &ce->signals)) {
			/* Advance the list to the first incomplete request */
			__list_del_many(&ce->signals, pos);
			if (&ce->signals == pos) /* now empty */
				list_del_init(&ce->signal_link);
		}
	}

	spin_unlock(&b->irq_lock);

	list_for_each_safe(pos, next, &signal) {
		struct i915_request *rq =
			list_entry(pos, typeof(*rq), signal_link);
143

144 145
		dma_fence_signal(&rq->fence);
		i915_request_put(rq);
146
	}
147 148
}

149
void intel_engine_signal_breadcrumbs(struct intel_engine_cs *engine)
150
{
151
	local_irq_disable();
152
	intel_engine_breadcrumbs_irq(engine);
153
	local_irq_enable();
154 155
}

156 157 158 159 160 161 162 163
static void signal_irq_work(struct irq_work *work)
{
	struct intel_engine_cs *engine =
		container_of(work, typeof(*engine), breadcrumbs.irq_work);

	intel_engine_breadcrumbs_irq(engine);
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
void intel_engine_pin_breadcrumbs_irq(struct intel_engine_cs *engine)
{
	struct intel_breadcrumbs *b = &engine->breadcrumbs;

	spin_lock_irq(&b->irq_lock);
	if (!b->irq_enabled++)
		irq_enable(engine);
	GEM_BUG_ON(!b->irq_enabled); /* no overflow! */
	spin_unlock_irq(&b->irq_lock);
}

void intel_engine_unpin_breadcrumbs_irq(struct intel_engine_cs *engine)
{
	struct intel_breadcrumbs *b = &engine->breadcrumbs;

	spin_lock_irq(&b->irq_lock);
	GEM_BUG_ON(!b->irq_enabled); /* no underflow! */
	if (!--b->irq_enabled)
		irq_disable(engine);
	spin_unlock_irq(&b->irq_lock);
}

186
static void __intel_breadcrumbs_arm_irq(struct intel_breadcrumbs *b)
187 188 189
{
	struct intel_engine_cs *engine =
		container_of(b, struct intel_engine_cs, breadcrumbs);
190

191
	lockdep_assert_held(&b->irq_lock);
192
	if (b->irq_armed)
193
		return;
194

195 196
	/*
	 * The breadcrumb irq will be disarmed on the interrupt after the
197 198 199 200 201 202
	 * 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.
	 */
	b->irq_armed = true;

203 204
	/*
	 * Since we are waiting on a request, the GPU should be busy
205 206 207 208
	 * 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.
209 210
	 */

211
	if (!b->irq_enabled++)
212
		irq_enable(engine);
213 214
}

215
void intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
216
{
217
	struct intel_breadcrumbs *b = &engine->breadcrumbs;
218

219 220
	spin_lock_init(&b->irq_lock);
	INIT_LIST_HEAD(&b->signalers);
221

222
	init_irq_work(&b->irq_work, signal_irq_work);
223 224
}

225
void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
226 227
{
	struct intel_breadcrumbs *b = &engine->breadcrumbs;
228
	unsigned long flags;
229

230
	spin_lock_irqsave(&b->irq_lock, flags);
231

232 233
	if (b->irq_enabled)
		irq_enable(engine);
234
	else
235
		irq_disable(engine);
236

237
	spin_unlock_irqrestore(&b->irq_lock, flags);
238 239
}

240
void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
241
{
242 243
}

244
bool i915_request_enable_breadcrumb(struct i915_request *rq)
245
{
246
	struct intel_breadcrumbs *b = &rq->engine->breadcrumbs;
247

248
	GEM_BUG_ON(test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags));
249

250 251
	if (!test_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags))
		return true;
252

253 254 255 256 257
	spin_lock(&b->irq_lock);
	if (test_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags) &&
	    !__request_completed(rq)) {
		struct intel_context *ce = rq->hw_context;
		struct list_head *pos;
258

259
		__intel_breadcrumbs_arm_irq(b);
260

261
		/*
262 263 264 265 266 267 268 269 270 271 272 273
		 * We keep the seqno in retirement order, so we can break
		 * inside intel_engine_breadcrumbs_irq as soon as we've passed
		 * the last completed request (or seen a request that hasn't
		 * event started). We could iterate the timeline->requests list,
		 * 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.
274
		 */
275 276 277
		list_for_each_prev(pos, &ce->signals) {
			struct i915_request *it =
				list_entry(pos, typeof(*it), signal_link);
278

279
			if (i915_seqno_passed(rq->fence.seqno, it->fence.seqno))
280 281
				break;
		}
282 283 284
		list_add(&rq->signal_link, pos);
		if (pos == &ce->signals) /* catch transitions from empty list */
			list_move_tail(&ce->signal_link, &b->signalers);
285

286
		set_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
287
	}
288
	spin_unlock(&b->irq_lock);
289

290
	return !__request_completed(rq);
291 292
}

293
void i915_request_cancel_breadcrumb(struct i915_request *rq)
294
{
295
	struct intel_breadcrumbs *b = &rq->engine->breadcrumbs;
296

297
	if (!test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags))
298
		return;
299

300 301 302
	spin_lock(&b->irq_lock);
	if (test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags)) {
		struct intel_context *ce = rq->hw_context;
303

304 305 306
		list_del(&rq->signal_link);
		if (list_empty(&ce->signals))
			list_del_init(&ce->signal_link);
307

308 309 310
		clear_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
	}
	spin_unlock(&b->irq_lock);
311 312
}

313 314
void intel_engine_print_breadcrumbs(struct intel_engine_cs *engine,
				    struct drm_printer *p)
315 316
{
	struct intel_breadcrumbs *b = &engine->breadcrumbs;
317 318
	struct intel_context *ce;
	struct i915_request *rq;
319

320 321
	if (list_empty(&b->signalers))
		return;
322

323
	drm_printf(p, "Signals:\n");
324

325 326 327 328 329 330 331 332 333 334 335 336
	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);
337
}