dma-resv.c 14.4 KB
Newer Older
1
/*
2
 * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * Based on bo.c which bears the following copyright notice,
 * but is dual licensed:
 *
 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
 * All Rights Reserved.
 *
 * 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, sub license, 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 NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS 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.
 *
 **************************************************************************/
/*
 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
 */

35
#include <linux/dma-resv.h>
36 37
#include <linux/export.h>

R
Rob Clark 已提交
38 39 40 41 42 43 44 45 46 47 48
/**
 * DOC: Reservation Object Overview
 *
 * The reservation object provides a mechanism to manage shared and
 * exclusive fences associated with a buffer.  A reservation object
 * can have attached one exclusive fence (normally associated with
 * write operations) or N shared fences (read operations).  The RCU
 * mechanism is used to protect read access to fences from locked
 * write-side updates.
 */

49
DEFINE_WD_CLASS(reservation_ww_class);
50
EXPORT_SYMBOL(reservation_ww_class);
51

52
/**
53
 * dma_resv_list_alloc - allocate fence list
54 55
 * @shared_max: number of fences we need space for
 *
56
 * Allocate a new dma_resv_list and make sure to correctly initialize
57 58
 * shared_max.
 */
59
static struct dma_resv_list *dma_resv_list_alloc(unsigned int shared_max)
60
{
61
	struct dma_resv_list *list;
62 63 64 65 66 67 68 69 70 71 72 73

	list = kmalloc(offsetof(typeof(*list), shared[shared_max]), GFP_KERNEL);
	if (!list)
		return NULL;

	list->shared_max = (ksize(list) - offsetof(typeof(*list), shared)) /
		sizeof(*list->shared);

	return list;
}

/**
74
 * dma_resv_list_free - free fence list
75 76
 * @list: list to free
 *
77
 * Free a dma_resv_list and make sure to drop all references.
78
 */
79
static void dma_resv_list_free(struct dma_resv_list *list)
80 81 82 83 84 85 86 87 88 89 90 91
{
	unsigned int i;

	if (!list)
		return;

	for (i = 0; i < list->shared_count; ++i)
		dma_fence_put(rcu_dereference_protected(list->shared[i], true));

	kfree_rcu(list, rcu);
}

92
/**
93
 * dma_resv_init - initialize a reservation object
94 95
 * @obj: the reservation object
 */
96
void dma_resv_init(struct dma_resv *obj)
97 98 99 100 101
{
	ww_mutex_init(&obj->lock, &reservation_ww_class);
	RCU_INIT_POINTER(obj->fence, NULL);
	RCU_INIT_POINTER(obj->fence_excl, NULL);
}
102
EXPORT_SYMBOL(dma_resv_init);
103 104

/**
105
 * dma_resv_fini - destroys a reservation object
106 107
 * @obj: the reservation object
 */
108
void dma_resv_fini(struct dma_resv *obj)
109
{
110
	struct dma_resv_list *fobj;
111 112 113 114 115 116 117 118 119 120 121
	struct dma_fence *excl;

	/*
	 * This object should be dead and all references must have
	 * been released to it, so no need to be protected with rcu.
	 */
	excl = rcu_dereference_protected(obj->fence_excl, 1);
	if (excl)
		dma_fence_put(excl);

	fobj = rcu_dereference_protected(obj->fence, 1);
122
	dma_resv_list_free(fobj);
123 124
	ww_mutex_destroy(&obj->lock);
}
125
EXPORT_SYMBOL(dma_resv_fini);
126

R
Rob Clark 已提交
127
/**
128 129
 * dma_resv_reserve_shared - Reserve space to add shared fences to
 * a dma_resv.
R
Rob Clark 已提交
130
 * @obj: reservation object
131
 * @num_fences: number of fences we want to add
R
Rob Clark 已提交
132
 *
133
 * Should be called before dma_resv_add_shared_fence().  Must
R
Rob Clark 已提交
134 135 136 137
 * be called with obj->lock held.
 *
 * RETURNS
 * Zero for success, or -errno
138
 */
139
int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences)
140
{
141
	struct dma_resv_list *old, *new;
142
	unsigned int i, j, k, max;
143

144
	dma_resv_assert_held(obj);
145

146
	old = dma_resv_get_list(obj);
147 148

	if (old && old->shared_max) {
149
		if ((old->shared_count + num_fences) <= old->shared_max)
150
			return 0;
151
		else
152 153
			max = max(old->shared_count + num_fences,
				  old->shared_max * 2);
154
	} else {
155
		max = 4;
156
	}
157

158
	new = dma_resv_list_alloc(max);
159 160
	if (!new)
		return -ENOMEM;
161 162 163 164 165 166 167

	/*
	 * no need to bump fence refcounts, rcu_read access
	 * requires the use of kref_get_unless_zero, and the
	 * references from the old struct are carried over to
	 * the new.
	 */
168 169
	for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) {
		struct dma_fence *fence;
170

171
		fence = rcu_dereference_protected(old->shared[i],
172
						  dma_resv_held(obj));
173 174
		if (dma_fence_is_signaled(fence))
			RCU_INIT_POINTER(new->shared[--k], fence);
175
		else
176
			RCU_INIT_POINTER(new->shared[j++], fence);
177
	}
178
	new->shared_count = j;
179

180
	/*
181 182 183 184 185 186
	 * We are not changing the effective set of fences here so can
	 * merely update the pointer to the new array; both existing
	 * readers and new readers will see exactly the same set of
	 * active (unsignaled) shared fences. Individual fences and the
	 * old array are protected by RCU and so will not vanish under
	 * the gaze of the rcu_read_lock() readers.
187
	 */
188
	rcu_assign_pointer(obj->fence, new);
189

190
	if (!old)
191
		return 0;
192

193
	/* Drop the references to the signaled fences */
194
	for (i = k; i < max; ++i) {
195
		struct dma_fence *fence;
196

197
		fence = rcu_dereference_protected(new->shared[i],
198
						  dma_resv_held(obj));
199
		dma_fence_put(fence);
200 201
	}
	kfree_rcu(old, rcu);
202 203

	return 0;
204
}
205
EXPORT_SYMBOL(dma_resv_reserve_shared);
206

R
Rob Clark 已提交
207
/**
208
 * dma_resv_add_shared_fence - Add a fence to a shared slot
R
Rob Clark 已提交
209 210 211
 * @obj: the reservation object
 * @fence: the shared fence to add
 *
212
 * Add a fence to a shared slot, obj->lock must be held, and
213
 * dma_resv_reserve_shared() has been called.
214
 */
215
void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence)
216
{
217
	struct dma_resv_list *fobj;
218
	struct dma_fence *old;
219
	unsigned int i, count;
220

221 222
	dma_fence_get(fence);

223
	dma_resv_assert_held(obj);
224

225
	fobj = dma_resv_get_list(obj);
226
	count = fobj->shared_count;
227

228
	for (i = 0; i < count; ++i) {
229

230
		old = rcu_dereference_protected(fobj->shared[i],
231
						dma_resv_held(obj));
232 233
		if (old->context == fence->context ||
		    dma_fence_is_signaled(old))
234 235 236 237
			goto replace;
	}

	BUG_ON(fobj->shared_count >= fobj->shared_max);
238
	old = NULL;
239
	count++;
240 241 242

replace:
	RCU_INIT_POINTER(fobj->shared[i], fence);
243 244
	/* pointer update must be visible before we extend the shared_count */
	smp_store_mb(fobj->shared_count, count);
245
	dma_fence_put(old);
246
}
247
EXPORT_SYMBOL(dma_resv_add_shared_fence);
248

R
Rob Clark 已提交
249
/**
250
 * dma_resv_add_excl_fence - Add an exclusive fence.
R
Rob Clark 已提交
251 252 253 254 255
 * @obj: the reservation object
 * @fence: the shared fence to add
 *
 * Add a fence to the exclusive slot.  The obj->lock must be held.
 */
256
void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence)
257
{
258 259
	struct dma_fence *old_fence = dma_resv_get_excl(obj);
	struct dma_resv_list *old;
260 261
	u32 i = 0;

262
	dma_resv_assert_held(obj);
263

264
	old = dma_resv_get_list(obj);
265
	if (old)
266 267 268
		i = old->shared_count;

	if (fence)
269
		dma_fence_get(fence);
270

271
	preempt_disable();
272 273
	rcu_assign_pointer(obj->fence_excl, fence);
	/* pointer update must be visible before we modify the shared_count */
274
	if (old)
275
		smp_store_mb(old->shared_count, 0);
276
	preempt_enable();
277 278 279

	/* inplace update, no shared fences */
	while (i--)
280
		dma_fence_put(rcu_dereference_protected(old->shared[i],
281
						dma_resv_held(obj)));
282

283
	dma_fence_put(old_fence);
284
}
285
EXPORT_SYMBOL(dma_resv_add_excl_fence);
286

287
/**
288
* dma_resv_copy_fences - Copy all fences from src to dst.
289 290 291
* @dst: the destination reservation object
* @src: the source reservation object
*
292
* Copy all fences from src to dst. dst-lock must be held.
293
*/
294
int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src)
295
{
296
	struct dma_resv_list *src_list, *dst_list;
297
	struct dma_fence *old, *new;
298
	unsigned int i, shared_count;
299

300
	dma_resv_assert_held(dst);
301

302
	rcu_read_lock();
303

304
retry:
305
	dma_resv_fences(src, &new, &src_list, &shared_count);
306
	if (shared_count) {
307 308
		rcu_read_unlock();

309
		dst_list = dma_resv_list_alloc(shared_count);
310 311 312
		if (!dst_list)
			return -ENOMEM;

313
		rcu_read_lock();
314
		dma_resv_fences(src, &new, &src_list, &shared_count);
315
		if (!src_list || shared_count > dst_list->shared_max) {
316 317 318 319 320
			kfree(dst_list);
			goto retry;
		}

		dst_list->shared_count = 0;
321
		for (i = 0; i < shared_count; ++i) {
322 323 324 325 326 327 328 329
			struct dma_fence *fence;

			fence = rcu_dereference(src_list->shared[i]);
			if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
				     &fence->flags))
				continue;

			if (!dma_fence_get_rcu(fence)) {
330
				dma_resv_list_free(dst_list);
331 332 333 334 335 336 337 338
				goto retry;
			}

			if (dma_fence_is_signaled(fence)) {
				dma_fence_put(fence);
				continue;
			}

339
			rcu_assign_pointer(dst_list->shared[dst_list->shared_count++], fence);
340
		}
341 342 343 344
	} else {
		dst_list = NULL;
	}

345
	if (new && !dma_fence_get_rcu(new)) {
346
		dma_resv_list_free(dst_list);
347 348
		goto retry;
	}
349 350
	rcu_read_unlock();

351 352
	src_list = dma_resv_get_list(dst);
	old = dma_resv_get_excl(dst);
353 354

	preempt_disable();
355 356
	rcu_assign_pointer(dst->fence_excl, new);
	rcu_assign_pointer(dst->fence, dst_list);
357 358
	preempt_enable();

359
	dma_resv_list_free(src_list);
360 361 362 363
	dma_fence_put(old);

	return 0;
}
364
EXPORT_SYMBOL(dma_resv_copy_fences);
365

R
Rob Clark 已提交
366
/**
367
 * dma_resv_get_fences_rcu - Get an object's shared and exclusive
R
Rob Clark 已提交
368 369 370 371 372 373 374
 * fences without update side lock held
 * @obj: the reservation object
 * @pfence_excl: the returned exclusive fence (or NULL)
 * @pshared_count: the number of shared fences returned
 * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
 * the required size, and must be freed by caller)
 *
375 376 377
 * Retrieve all fences from the reservation object. If the pointer for the
 * exclusive fence is not specified the fence is put into the array of the
 * shared fences as well. Returns either zero or -ENOMEM.
R
Rob Clark 已提交
378
 */
379 380 381 382
int dma_resv_get_fences_rcu(struct dma_resv *obj,
			    struct dma_fence **pfence_excl,
			    unsigned *pshared_count,
			    struct dma_fence ***pshared)
383
{
384 385
	struct dma_fence **shared = NULL;
	struct dma_fence *fence_excl;
386 387
	unsigned int shared_count;
	int ret = 1;
388

389
	do {
390
		struct dma_resv_list *fobj;
391
		unsigned int i;
392
		size_t sz = 0;
393

394
		i = 0;
395 396

		rcu_read_lock();
397
		dma_resv_fences(obj, &fence_excl, &fobj,
398
					  &shared_count);
399

400
		if (fence_excl && !dma_fence_get_rcu(fence_excl))
401
			goto unlock;
402

403 404 405 406 407 408 409
		if (fobj)
			sz += sizeof(*shared) * fobj->shared_max;

		if (!pfence_excl && fence_excl)
			sz += sizeof(*shared);

		if (sz) {
410
			struct dma_fence **nshared;
411 412 413 414 415

			nshared = krealloc(shared, sz,
					   GFP_NOWAIT | __GFP_NOWARN);
			if (!nshared) {
				rcu_read_unlock();
416 417 418 419

				dma_fence_put(fence_excl);
				fence_excl = NULL;

420 421 422 423 424 425 426 427 428 429 430
				nshared = krealloc(shared, sz, GFP_KERNEL);
				if (nshared) {
					shared = nshared;
					continue;
				}

				ret = -ENOMEM;
				break;
			}
			shared = nshared;
			for (i = 0; i < shared_count; ++i) {
431
				shared[i] = rcu_dereference(fobj->shared[i]);
432
				if (!dma_fence_get_rcu(shared[i]))
433
					break;
434
			}
435
		}
436

437
		if (i != shared_count) {
438
			while (i--)
439 440
				dma_fence_put(shared[i]);
			dma_fence_put(fence_excl);
441 442 443 444
			goto unlock;
		}

		ret = 0;
445 446
unlock:
		rcu_read_unlock();
447 448
	} while (ret);

449 450 451 452 453
	if (pfence_excl)
		*pfence_excl = fence_excl;
	else if (fence_excl)
		shared[++shared_count] = fence_excl;

454
	if (!shared_count) {
455
		kfree(shared);
456
		shared = NULL;
457
	}
458 459 460

	*pshared_count = shared_count;
	*pshared = shared;
461 462
	return ret;
}
463
EXPORT_SYMBOL_GPL(dma_resv_get_fences_rcu);
464

R
Rob Clark 已提交
465
/**
466
 * dma_resv_wait_timeout_rcu - Wait on reservation's objects
R
Rob Clark 已提交
467 468 469 470 471 472 473 474 475 476
 * shared and/or exclusive fences.
 * @obj: the reservation object
 * @wait_all: if true, wait on all fences, else wait on just exclusive fence
 * @intr: if true, do interruptible wait
 * @timeout: timeout value in jiffies or zero to return immediately
 *
 * RETURNS
 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
 * greater than zer on success.
 */
477 478 479
long dma_resv_wait_timeout_rcu(struct dma_resv *obj,
			       bool wait_all, bool intr,
			       unsigned long timeout)
480
{
481
	struct dma_resv_list *fobj;
482
	struct dma_fence *fence;
483
	unsigned shared_count;
484
	long ret = timeout ? timeout : 1;
485
	int i;
486

487 488
retry:
	rcu_read_lock();
489
	i = -1;
490

491
	dma_resv_fences(obj, &fence, &fobj, &shared_count);
492 493 494 495 496 497 498 499 500 501 502 503 504
	if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
		if (!dma_fence_get_rcu(fence))
			goto unlock_retry;

		if (dma_fence_is_signaled(fence)) {
			dma_fence_put(fence);
			fence = NULL;
		}

	} else {
		fence = NULL;
	}

505 506
	if (wait_all) {
		for (i = 0; !fence && i < shared_count; ++i) {
507
			struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
508

509 510
			if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
				     &lfence->flags))
511 512
				continue;

513
			if (!dma_fence_get_rcu(lfence))
514 515
				goto unlock_retry;

516 517
			if (dma_fence_is_signaled(lfence)) {
				dma_fence_put(lfence);
518 519 520 521 522 523 524 525 526 527
				continue;
			}

			fence = lfence;
			break;
		}
	}

	rcu_read_unlock();
	if (fence) {
528 529
		ret = dma_fence_wait_timeout(fence, intr, ret);
		dma_fence_put(fence);
530 531 532 533 534 535 536 537 538
		if (ret > 0 && wait_all && (i + 1 < shared_count))
			goto retry;
	}
	return ret;

unlock_retry:
	rcu_read_unlock();
	goto retry;
}
539
EXPORT_SYMBOL_GPL(dma_resv_wait_timeout_rcu);
540 541


542
static inline int dma_resv_test_signaled_single(struct dma_fence *passed_fence)
543
{
544
	struct dma_fence *fence, *lfence = passed_fence;
545 546
	int ret = 1;

547 548
	if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
		fence = dma_fence_get_rcu(lfence);
549 550 551
		if (!fence)
			return -1;

552 553
		ret = !!dma_fence_is_signaled(fence);
		dma_fence_put(fence);
554 555 556 557
	}
	return ret;
}

R
Rob Clark 已提交
558
/**
559
 * dma_resv_test_signaled_rcu - Test if a reservation object's
R
Rob Clark 已提交
560 561 562 563 564 565 566 567
 * fences have been signaled.
 * @obj: the reservation object
 * @test_all: if true, test all fences, otherwise only test the exclusive
 * fence
 *
 * RETURNS
 * true if all fences signaled, else false
 */
568
bool dma_resv_test_signaled_rcu(struct dma_resv *obj, bool test_all)
569
{
570
	struct dma_resv_list *fobj;
571 572
	struct dma_fence *fence_excl;
	unsigned shared_count;
573
	int ret;
574

575
	rcu_read_lock();
576
retry:
577
	ret = true;
578

579
	dma_resv_fences(obj, &fence_excl, &fobj, &shared_count);
580 581 582 583
	if (test_all) {
		unsigned i;

		for (i = 0; i < shared_count; ++i) {
584
			struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
585

586
			ret = dma_resv_test_signaled_single(fence);
587
			if (ret < 0)
588
				goto retry;
589 590 591 592 593
			else if (!ret)
				break;
		}
	}

594
	if (!shared_count && fence_excl) {
595
		ret = dma_resv_test_signaled_single(fence_excl);
596 597
		if (ret < 0)
			goto retry;
598 599 600 601 602
	}

	rcu_read_unlock();
	return ret;
}
603
EXPORT_SYMBOL_GPL(dma_resv_test_signaled_rcu);