dma-fence-array.c 5.14 KB
Newer Older
1
/*
2
 * dma-fence-array: aggregate fences to be waited together
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Copyright (C) 2016 Collabora Ltd
 * Copyright (C) 2016 Advanced Micro Devices, Inc.
 * Authors:
 *	Gustavo Padovan <gustavo@padovan.org>
 *	Christian König <christian.koenig@amd.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */

#include <linux/export.h>
#include <linux/slab.h>
22
#include <linux/dma-fence-array.h>
23

24
static const char *dma_fence_array_get_driver_name(struct dma_fence *fence)
25
{
26
	return "dma_fence_array";
27 28
}

29
static const char *dma_fence_array_get_timeline_name(struct dma_fence *fence)
30 31 32 33
{
	return "unbound";
}

34 35
static void dma_fence_array_cb_func(struct dma_fence *f,
				    struct dma_fence_cb *cb)
36
{
37 38 39
	struct dma_fence_array_cb *array_cb =
		container_of(cb, struct dma_fence_array_cb, cb);
	struct dma_fence_array *array = array_cb->array;
40 41

	if (atomic_dec_and_test(&array->num_pending))
42 43
		dma_fence_signal(&array->base);
	dma_fence_put(&array->base);
44 45
}

46
static bool dma_fence_array_enable_signaling(struct dma_fence *fence)
47
{
48 49
	struct dma_fence_array *array = to_dma_fence_array(fence);
	struct dma_fence_array_cb *cb = (void *)(&array[1]);
50 51 52 53
	unsigned i;

	for (i = 0; i < array->num_fences; ++i) {
		cb[i].array = array;
54 55 56 57 58 59 60 61
		/*
		 * As we may report that the fence is signaled before all
		 * callbacks are complete, we need to take an additional
		 * reference count on the array so that we do not free it too
		 * early. The core fence handling will only hold the reference
		 * until we signal the array as complete (but that is now
		 * insufficient).
		 */
62 63 64 65
		dma_fence_get(&array->base);
		if (dma_fence_add_callback(array->fences[i], &cb[i].cb,
					   dma_fence_array_cb_func)) {
			dma_fence_put(&array->base);
66 67
			if (atomic_dec_and_test(&array->num_pending))
				return false;
68
		}
69 70 71 72 73
	}

	return true;
}

74
static bool dma_fence_array_signaled(struct dma_fence *fence)
75
{
76
	struct dma_fence_array *array = to_dma_fence_array(fence);
77

78
	return atomic_read(&array->num_pending) <= 0;
79 80
}

81
static void dma_fence_array_release(struct dma_fence *fence)
82
{
83
	struct dma_fence_array *array = to_dma_fence_array(fence);
84 85 86
	unsigned i;

	for (i = 0; i < array->num_fences; ++i)
87
		dma_fence_put(array->fences[i]);
88 89

	kfree(array->fences);
90
	dma_fence_free(fence);
91 92
}

93 94 95 96 97 98 99
const struct dma_fence_ops dma_fence_array_ops = {
	.get_driver_name = dma_fence_array_get_driver_name,
	.get_timeline_name = dma_fence_array_get_timeline_name,
	.enable_signaling = dma_fence_array_enable_signaling,
	.signaled = dma_fence_array_signaled,
	.wait = dma_fence_default_wait,
	.release = dma_fence_array_release,
100
};
101
EXPORT_SYMBOL(dma_fence_array_ops);
102 103

/**
104
 * dma_fence_array_create - Create a custom fence array
105 106 107 108
 * @num_fences:		[in]	number of fences to add in the array
 * @fences:		[in]	array containing the fences
 * @context:		[in]	fence context to use
 * @seqno:		[in]	sequence number to use
109
 * @signal_on_any:	[in]	signal on any fence in the array
110
 *
111 112
 * Allocate a dma_fence_array object and initialize the base fence with
 * dma_fence_init().
113 114
 * In case of error it returns NULL.
 *
115
 * The caller should allocate the fences array with num_fences size
116
 * and fill it with the fences it wants to add to the object. Ownership of this
117
 * array is taken and dma_fence_put() is used on each fence on release.
118 119 120
 *
 * If @signal_on_any is true the fence array signals if any fence in the array
 * signals, otherwise it signals when all fences in the array signal.
121
 */
122 123 124 125
struct dma_fence_array *dma_fence_array_create(int num_fences,
					       struct dma_fence **fences,
					       u64 context, unsigned seqno,
					       bool signal_on_any)
126
{
127
	struct dma_fence_array *array;
128 129 130
	size_t size = sizeof(*array);

	/* Allocate the callback structures behind the array. */
131
	size += num_fences * sizeof(struct dma_fence_array_cb);
132 133 134 135 136
	array = kzalloc(size, GFP_KERNEL);
	if (!array)
		return NULL;

	spin_lock_init(&array->lock);
137 138
	dma_fence_init(&array->base, &dma_fence_array_ops, &array->lock,
		       context, seqno);
139 140

	array->num_fences = num_fences;
141
	atomic_set(&array->num_pending, signal_on_any ? 1 : num_fences);
142 143 144 145
	array->fences = fences;

	return array;
}
146
EXPORT_SYMBOL(dma_fence_array_create);
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

/**
 * dma_fence_match_context - Check if all fences are from the given context
 * @fence:		[in]	fence or fence array
 * @context:		[in]	fence context to check all fences against
 *
 * Checks the provided fence or, for a fence array, all fences in the array
 * against the given context. Returns false if any fence is from a different
 * context.
 */
bool dma_fence_match_context(struct dma_fence *fence, u64 context)
{
	struct dma_fence_array *array = to_dma_fence_array(fence);
	unsigned i;

	if (!dma_fence_is_array(fence))
		return fence->context == context;

	for (i = 0; i < array->num_fences; i++) {
		if (array->fences[i]->context != context)
			return false;
	}

	return true;
}
EXPORT_SYMBOL(dma_fence_match_context);