i915_sw_fence.c 12.2 KB
Newer Older
1
/*
2
 * SPDX-License-Identifier: MIT
3
 *
4
 * (C) Copyright 2016 Intel Corporation
5 6 7
 */

#include <linux/slab.h>
8
#include <linux/dma-fence.h>
9
#include <linux/irq_work.h>
10 11 12
#include <linux/reservation.h>

#include "i915_sw_fence.h"
13
#include "i915_selftest.h"
14

15 16
#define I915_SW_FENCE_FLAG_ALLOC BIT(3) /* after WQ_FLAG_* for safety */

17 18
static DEFINE_SPINLOCK(i915_sw_fence_lock);

19 20 21 22 23 24 25 26 27 28
enum {
	DEBUG_FENCE_IDLE = 0,
	DEBUG_FENCE_NOTIFY,
};

static void *i915_sw_fence_debug_hint(void *addr)
{
	return (void *)(((struct i915_sw_fence *)addr)->flags & I915_SW_FENCE_MASK);
}

29 30
#ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS

31 32 33 34 35 36 37 38 39 40
static struct debug_obj_descr i915_sw_fence_debug_descr = {
	.name = "i915_sw_fence",
	.debug_hint = i915_sw_fence_debug_hint,
};

static inline void debug_fence_init(struct i915_sw_fence *fence)
{
	debug_object_init(fence, &i915_sw_fence_debug_descr);
}

41 42 43 44 45
static inline void debug_fence_init_onstack(struct i915_sw_fence *fence)
{
	debug_object_init_on_stack(fence, &i915_sw_fence_debug_descr);
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
static inline void debug_fence_activate(struct i915_sw_fence *fence)
{
	debug_object_activate(fence, &i915_sw_fence_debug_descr);
}

static inline void debug_fence_set_state(struct i915_sw_fence *fence,
					 int old, int new)
{
	debug_object_active_state(fence, &i915_sw_fence_debug_descr, old, new);
}

static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
{
	debug_object_deactivate(fence, &i915_sw_fence_debug_descr);
}

static inline void debug_fence_destroy(struct i915_sw_fence *fence)
{
	debug_object_destroy(fence, &i915_sw_fence_debug_descr);
}

static inline void debug_fence_free(struct i915_sw_fence *fence)
{
	debug_object_free(fence, &i915_sw_fence_debug_descr);
70
	smp_wmb(); /* flush the change in state before reallocation */
71 72 73 74 75 76 77 78 79 80 81 82 83
}

static inline void debug_fence_assert(struct i915_sw_fence *fence)
{
	debug_object_assert_init(fence, &i915_sw_fence_debug_descr);
}

#else

static inline void debug_fence_init(struct i915_sw_fence *fence)
{
}

84 85 86 87
static inline void debug_fence_init_onstack(struct i915_sw_fence *fence)
{
}

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 114
static inline void debug_fence_activate(struct i915_sw_fence *fence)
{
}

static inline void debug_fence_set_state(struct i915_sw_fence *fence,
					 int old, int new)
{
}

static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
{
}

static inline void debug_fence_destroy(struct i915_sw_fence *fence)
{
}

static inline void debug_fence_free(struct i915_sw_fence *fence)
{
}

static inline void debug_fence_assert(struct i915_sw_fence *fence)
{
}

#endif

115 116 117 118 119 120 121 122 123
static int __i915_sw_fence_notify(struct i915_sw_fence *fence,
				  enum i915_sw_fence_notify state)
{
	i915_sw_fence_notify_t fn;

	fn = (i915_sw_fence_notify_t)(fence->flags & I915_SW_FENCE_MASK);
	return fn(fence, state);
}

124 125 126 127 128 129 130
#ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
void i915_sw_fence_fini(struct i915_sw_fence *fence)
{
	debug_fence_free(fence);
}
#endif

131 132 133 134
static void __i915_sw_fence_wake_up_all(struct i915_sw_fence *fence,
					struct list_head *continuation)
{
	wait_queue_head_t *x = &fence->wait;
135
	wait_queue_entry_t *pos, *next;
136 137
	unsigned long flags;

138
	debug_fence_deactivate(fence);
139 140 141 142
	atomic_set_release(&fence->pending, -1); /* 0 -> -1 [done] */

	/*
	 * To prevent unbounded recursion as we traverse the graph of
143 144
	 * i915_sw_fences, we move the entry list from this, the next ready
	 * fence, to the tail of the original fence's entry list
145 146 147 148 149
	 * (and so added to the list to be woken).
	 */

	spin_lock_irqsave_nested(&x->lock, flags, 1 + !!continuation);
	if (continuation) {
150
		list_for_each_entry_safe(pos, next, &x->head, entry) {
151 152 153
			if (pos->func == autoremove_wake_function)
				pos->func(pos, TASK_NORMAL, 0, continuation);
			else
154
				list_move_tail(&pos->entry, continuation);
155 156 157 158 159
		}
	} else {
		LIST_HEAD(extra);

		do {
160
			list_for_each_entry_safe(pos, next, &x->head, entry)
161 162 163 164 165
				pos->func(pos, TASK_NORMAL, 0, &extra);

			if (list_empty(&extra))
				break;

166
			list_splice_tail_init(&extra, &x->head);
167 168 169
		} while (1);
	}
	spin_unlock_irqrestore(&x->lock, flags);
170 171

	debug_fence_assert(fence);
172 173 174 175 176
}

static void __i915_sw_fence_complete(struct i915_sw_fence *fence,
				     struct list_head *continuation)
{
177 178
	debug_fence_assert(fence);

179 180 181
	if (!atomic_dec_and_test(&fence->pending))
		return;

182 183
	debug_fence_set_state(fence, DEBUG_FENCE_IDLE, DEBUG_FENCE_NOTIFY);

184
	if (__i915_sw_fence_notify(fence, FENCE_COMPLETE) != NOTIFY_DONE)
185 186
		return;

187 188
	debug_fence_set_state(fence, DEBUG_FENCE_NOTIFY, DEBUG_FENCE_IDLE);

189
	__i915_sw_fence_wake_up_all(fence, continuation);
190 191 192

	debug_fence_destroy(fence);
	__i915_sw_fence_notify(fence, FENCE_FREE);
193 194
}

195
void i915_sw_fence_complete(struct i915_sw_fence *fence)
196
{
197 198
	debug_fence_assert(fence);

199 200 201 202 203 204
	if (WARN_ON(i915_sw_fence_done(fence)))
		return;

	__i915_sw_fence_complete(fence, NULL);
}

205
void i915_sw_fence_await(struct i915_sw_fence *fence)
206
{
207
	debug_fence_assert(fence);
208 209 210
	WARN_ON(atomic_inc_return(&fence->pending) <= 1);
}

211 212 213 214
void __i915_sw_fence_init(struct i915_sw_fence *fence,
			  i915_sw_fence_notify_t fn,
			  const char *name,
			  struct lock_class_key *key)
215
{
216
	BUG_ON(!fn || (unsigned long)fn & ~I915_SW_FENCE_MASK);
217

218 219
	debug_fence_init(fence);

220
	__init_waitqueue_head(&fence->wait, name, key);
221 222 223 224
	atomic_set(&fence->pending, 1);
	fence->flags = (unsigned long)fn;
}

225 226 227
void i915_sw_fence_commit(struct i915_sw_fence *fence)
{
	debug_fence_activate(fence);
228
	i915_sw_fence_complete(fence);
229 230
}

231
static int i915_sw_fence_wake(wait_queue_entry_t *wq, unsigned mode, int flags, void *key)
232
{
233
	list_del(&wq->entry);
234
	__i915_sw_fence_complete(wq->private, key);
235

236 237
	if (wq->flags & I915_SW_FENCE_FLAG_ALLOC)
		kfree(wq);
238 239 240 241 242 243
	return 0;
}

static bool __i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
				    const struct i915_sw_fence * const signaler)
{
244
	wait_queue_entry_t *wq;
245 246 247 248 249 250 251

	if (__test_and_set_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
		return false;

	if (fence == signaler)
		return true;

252
	list_for_each_entry(wq, &fence->wait.head, entry) {
253 254 255 256 257 258 259 260 261 262 263 264
		if (wq->func != i915_sw_fence_wake)
			continue;

		if (__i915_sw_fence_check_if_after(wq->private, signaler))
			return true;
	}

	return false;
}

static void __i915_sw_fence_clear_checked_bit(struct i915_sw_fence *fence)
{
265
	wait_queue_entry_t *wq;
266 267 268 269

	if (!__test_and_clear_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
		return;

270
	list_for_each_entry(wq, &fence->wait.head, entry) {
271 272 273 274 275 276 277 278 279 280 281 282 283
		if (wq->func != i915_sw_fence_wake)
			continue;

		__i915_sw_fence_clear_checked_bit(wq->private);
	}
}

static bool i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
				  const struct i915_sw_fence * const signaler)
{
	unsigned long flags;
	bool err;

284
	if (!IS_ENABLED(CONFIG_DRM_I915_SW_FENCE_CHECK_DAG))
285 286 287 288 289 290 291 292 293 294
		return false;

	spin_lock_irqsave(&i915_sw_fence_lock, flags);
	err = __i915_sw_fence_check_if_after(fence, signaler);
	__i915_sw_fence_clear_checked_bit(fence);
	spin_unlock_irqrestore(&i915_sw_fence_lock, flags);

	return err;
}

295 296
static int __i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
					  struct i915_sw_fence *signaler,
297
					  wait_queue_entry_t *wq, gfp_t gfp)
298 299 300 301
{
	unsigned long flags;
	int pending;

302
	debug_fence_assert(fence);
303
	might_sleep_if(gfpflags_allow_blocking(gfp));
304

305 306 307
	if (i915_sw_fence_done(signaler))
		return 0;

308 309
	debug_fence_assert(signaler);

310 311 312 313
	/* The dependency graph must be acyclic. */
	if (unlikely(i915_sw_fence_check_if_after(fence, signaler)))
		return -EINVAL;

314 315 316 317 318 319 320 321 322 323 324 325 326 327
	pending = 0;
	if (!wq) {
		wq = kmalloc(sizeof(*wq), gfp);
		if (!wq) {
			if (!gfpflags_allow_blocking(gfp))
				return -ENOMEM;

			i915_sw_fence_wait(signaler);
			return 0;
		}

		pending |= I915_SW_FENCE_FLAG_ALLOC;
	}

328
	INIT_LIST_HEAD(&wq->entry);
329
	wq->flags = pending;
330
	wq->func = i915_sw_fence_wake;
331
	wq->private = fence;
332 333 334 335 336

	i915_sw_fence_await(fence);

	spin_lock_irqsave(&signaler->wait.lock, flags);
	if (likely(!i915_sw_fence_done(signaler))) {
337
		__add_wait_queue_entry_tail(&signaler->wait, wq);
338 339 340 341 342 343 344 345 346 347
		pending = 1;
	} else {
		i915_sw_fence_wake(wq, 0, 0, NULL);
		pending = 0;
	}
	spin_unlock_irqrestore(&signaler->wait.lock, flags);

	return pending;
}

348 349
int i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
				 struct i915_sw_fence *signaler,
350
				 wait_queue_entry_t *wq)
351 352 353 354 355 356 357 358 359 360 361
{
	return __i915_sw_fence_await_sw_fence(fence, signaler, wq, 0);
}

int i915_sw_fence_await_sw_fence_gfp(struct i915_sw_fence *fence,
				     struct i915_sw_fence *signaler,
				     gfp_t gfp)
{
	return __i915_sw_fence_await_sw_fence(fence, signaler, NULL, gfp);
}

362 363
struct i915_sw_dma_fence_cb {
	struct dma_fence_cb base;
364
	struct i915_sw_fence *fence;
365 366 367 368
};

struct i915_sw_dma_fence_cb_timer {
	struct i915_sw_dma_fence_cb base;
369
	struct dma_fence *dma;
370
	struct timer_list timer;
371
	struct irq_work work;
372
	struct rcu_head rcu;
373 374
};

375 376 377 378 379 380 381 382 383
static void dma_i915_sw_fence_wake(struct dma_fence *dma,
				   struct dma_fence_cb *data)
{
	struct i915_sw_dma_fence_cb *cb = container_of(data, typeof(*cb), base);

	i915_sw_fence_complete(cb->fence);
	kfree(cb);
}

384
static void timer_i915_sw_fence_wake(struct timer_list *t)
385
{
386
	struct i915_sw_dma_fence_cb_timer *cb = from_timer(cb, t, timer);
387 388
	struct i915_sw_fence *fence;

389
	fence = xchg(&cb->base.fence, NULL);
390 391
	if (!fence)
		return;
392

393
	pr_notice("Asynchronous wait on fence %s:%s:%llx timed out (hint:%pS)\n",
394 395 396 397
		  cb->dma->ops->get_driver_name(cb->dma),
		  cb->dma->ops->get_timeline_name(cb->dma),
		  cb->dma->seqno,
		  i915_sw_fence_debug_hint(fence));
398

399
	i915_sw_fence_complete(fence);
400 401
}

402 403
static void dma_i915_sw_fence_wake_timer(struct dma_fence *dma,
					 struct dma_fence_cb *data)
404
{
405 406
	struct i915_sw_dma_fence_cb_timer *cb =
		container_of(data, typeof(*cb), base.base);
407 408
	struct i915_sw_fence *fence;

409
	fence = xchg(&cb->base.fence, NULL);
410 411 412
	if (fence)
		i915_sw_fence_complete(fence);

413
	irq_work_queue(&cb->work);
414 415 416 417
}

static void irq_i915_sw_fence_work(struct irq_work *wrk)
{
418 419
	struct i915_sw_dma_fence_cb_timer *cb =
		container_of(wrk, typeof(*cb), work);
420 421

	del_timer_sync(&cb->timer);
422
	dma_fence_put(cb->dma);
423

424
	kfree_rcu(cb, rcu);
425 426 427
}

int i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
428
				  struct dma_fence *dma,
429 430 431
				  unsigned long timeout,
				  gfp_t gfp)
{
432
	struct i915_sw_dma_fence_cb *cb;
433
	dma_fence_func_t func;
434 435
	int ret;

436
	debug_fence_assert(fence);
437
	might_sleep_if(gfpflags_allow_blocking(gfp));
438

439
	if (dma_fence_is_signaled(dma))
440 441
		return 0;

442 443 444 445
	cb = kmalloc(timeout ?
		     sizeof(struct i915_sw_dma_fence_cb_timer) :
		     sizeof(struct i915_sw_dma_fence_cb),
		     gfp);
446 447 448 449
	if (!cb) {
		if (!gfpflags_allow_blocking(gfp))
			return -ENOMEM;

450
		return dma_fence_wait(dma, false);
451 452
	}

453
	cb->fence = fence;
454 455
	i915_sw_fence_await(fence);

456
	func = dma_i915_sw_fence_wake;
457
	if (timeout) {
458 459
		struct i915_sw_dma_fence_cb_timer *timer =
			container_of(cb, typeof(*timer), base);
460

461 462 463 464
		timer->dma = dma_fence_get(dma);
		init_irq_work(&timer->work, irq_i915_sw_fence_work);

		timer_setup(&timer->timer,
465
			    timer_i915_sw_fence_wake, TIMER_IRQSAFE);
466 467 468
		mod_timer(&timer->timer, round_jiffies_up(jiffies + timeout));

		func = dma_i915_sw_fence_wake_timer;
469 470
	}

471
	ret = dma_fence_add_callback(dma, &cb->base, func);
472 473 474
	if (ret == 0) {
		ret = 1;
	} else {
475
		func(dma, &cb->base);
476 477 478 479 480 481 482 483 484
		if (ret == -ENOENT) /* fence already signaled */
			ret = 0;
	}

	return ret;
}

int i915_sw_fence_await_reservation(struct i915_sw_fence *fence,
				    struct reservation_object *resv,
485
				    const struct dma_fence_ops *exclude,
486 487 488 489
				    bool write,
				    unsigned long timeout,
				    gfp_t gfp)
{
490
	struct dma_fence *excl;
491 492
	int ret = 0, pending;

493
	debug_fence_assert(fence);
494
	might_sleep_if(gfpflags_allow_blocking(gfp));
495

496
	if (write) {
497
		struct dma_fence **shared;
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
		unsigned int count, i;

		ret = reservation_object_get_fences_rcu(resv,
							&excl, &count, &shared);
		if (ret)
			return ret;

		for (i = 0; i < count; i++) {
			if (shared[i]->ops == exclude)
				continue;

			pending = i915_sw_fence_await_dma_fence(fence,
								shared[i],
								timeout,
								gfp);
			if (pending < 0) {
				ret = pending;
				break;
			}

			ret |= pending;
		}

		for (i = 0; i < count; i++)
522
			dma_fence_put(shared[i]);
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
		kfree(shared);
	} else {
		excl = reservation_object_get_excl_rcu(resv);
	}

	if (ret >= 0 && excl && excl->ops != exclude) {
		pending = i915_sw_fence_await_dma_fence(fence,
							excl,
							timeout,
							gfp);
		if (pending < 0)
			ret = pending;
		else
			ret |= pending;
	}

539
	dma_fence_put(excl);
540 541 542

	return ret;
}
543 544

#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
545
#include "selftests/lib_sw_fence.c"
546 547
#include "selftests/i915_sw_fence.c"
#endif