summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/drm_sync_helper.c
blob: 0c6aa875b4c61f3c8ad6362a1d672ce015515e86 (plain)
1
2
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
83
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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
309
310
311
312
313
314
/*
 * drm_sync_helper.c: software fence and helper functions for fences and
 * reservations used for dma buffer access synchronization between drivers.
 *
 * Copyright 2014 Google, Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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/module.h>
#include <drm/drm_sync_helper.h>
#include <linux/slab.h>
#include <linux/reservation.h>

static DEFINE_SPINLOCK(sw_fence_lock);

void drm_add_reservation(struct reservation_object *resv,
			 struct reservation_object **resvs,
			 unsigned long *excl_resvs_bitmap,
			 unsigned int *num_resvs, bool exclusive)
{
	unsigned int r;

	for (r = 0; r < *num_resvs; r++) {
		if (resvs[r] == resv)
			return;
	}
	resvs[*num_resvs] = resv;
	if (exclusive)
		set_bit(*num_resvs, excl_resvs_bitmap);
	(*num_resvs)++;
}
EXPORT_SYMBOL(drm_add_reservation);

int drm_lock_reservations(struct reservation_object **resvs,
			  unsigned int num_resvs, struct ww_acquire_ctx *ctx)
{
	unsigned int r;
	struct reservation_object *slow_res = NULL;

	ww_acquire_init(ctx, &reservation_ww_class);

retry:
	for (r = 0; r < num_resvs; r++) {
		int ret;
		/* skip the resv we locked with slow lock */
		if (resvs[r] == slow_res) {
			slow_res = NULL;
			continue;
		}
		ret = ww_mutex_lock(&resvs[r]->lock, ctx);
		if (ret < 0) {
			unsigned int slow_r = r;
			/*
			 * undo all the locks we already done,
			 * in reverse order
			 */
			while (r > 0) {
				r--;
				ww_mutex_unlock(&resvs[r]->lock);
			}
			if (slow_res)
				ww_mutex_unlock(&slow_res->lock);
			if (ret == -EDEADLK) {
				slow_res = resvs[slow_r];
				ww_mutex_lock_slow(&slow_res->lock, ctx);
				goto retry;
			}
			ww_acquire_fini(ctx);
			return ret;
		}
	}

	ww_acquire_done(ctx);
	return 0;
}
EXPORT_SYMBOL(drm_lock_reservations);

void drm_unlock_reservations(struct reservation_object **resvs,
			     unsigned int num_resvs,
			     struct ww_acquire_ctx *ctx)
{
	unsigned int r;

	for (r = 0; r < num_resvs; r++)
		ww_mutex_unlock(&resvs[r]->lock);

	ww_acquire_fini(ctx);
}
EXPORT_SYMBOL(drm_unlock_reservations);

static void reservation_cb_fence_cb(struct fence *fence, struct fence_cb *cb)
{
	struct drm_reservation_fence_cb *rfcb =
		container_of(cb, struct drm_reservation_fence_cb, base);
	struct drm_reservation_cb *rcb = rfcb->parent;

	if (atomic_dec_and_test(&rcb->count))
		schedule_work(&rcb->work);
}

static void
reservation_cb_cleanup(struct drm_reservation_cb *rcb)
{
	unsigned cb;

	for (cb = 0; cb < rcb->num_fence_cbs; cb++) {
		if (rcb->fence_cbs[cb]) {
			fence_remove_callback(rcb->fence_cbs[cb]->fence,
						&rcb->fence_cbs[cb]->base);
			fence_put(rcb->fence_cbs[cb]->fence);
			kfree(rcb->fence_cbs[cb]);
			rcb->fence_cbs[cb] = NULL;
		}
	}
	kfree(rcb->fence_cbs);
	rcb->fence_cbs = NULL;
	rcb->num_fence_cbs = 0;
}

static void reservation_cb_work(struct work_struct *pwork)
{
	struct drm_reservation_cb *rcb =
		container_of(pwork, struct drm_reservation_cb, work);
	/*
	 * clean up everything before calling the callback, because the callback
	 * may free structure containing rcb and work_struct
	 */
	reservation_cb_cleanup(rcb);
	rcb->func(rcb, rcb->context);
}

static int
reservation_cb_add_fence_cb(struct drm_reservation_cb *rcb, struct fence *fence)
{
	int ret = 0;
	struct drm_reservation_fence_cb *fence_cb;
	struct drm_reservation_fence_cb **new_fence_cbs;

	new_fence_cbs = krealloc(rcb->fence_cbs,
				(rcb->num_fence_cbs + 1)
				* sizeof(struct drm_reservation_fence_cb *),
				GFP_KERNEL);
	if (!new_fence_cbs)
		return -ENOMEM;
	rcb->fence_cbs = new_fence_cbs;

	fence_cb = kzalloc(sizeof(struct drm_reservation_fence_cb), GFP_KERNEL);
	if (!fence_cb)
		return -ENOMEM;

	/*
	 * do not want for fence to disappear on us while we are waiting for
	 * callback and we need it in case we want to remove callbacks
	 */
	fence_get(fence);
	fence_cb->fence = fence;
	fence_cb->parent = rcb;
	rcb->fence_cbs[rcb->num_fence_cbs] = fence_cb;
	atomic_inc(&rcb->count);
	ret = fence_add_callback(fence, &fence_cb->base,
					reservation_cb_fence_cb);
	if (ret == -ENOENT) {
		/* already signaled */
		atomic_dec(&rcb->count);
		fence_put(fence_cb->fence);
		kfree(fence_cb);
		ret = 0;
	} else if (ret < 0) {
		atomic_dec(&rcb->count);
		fence_put(fence_cb->fence);
		kfree(fence_cb);
		return ret;
	} else {
		rcb->num_fence_cbs++;
	}
	return ret;
}

void
drm_reservation_cb_init(struct drm_reservation_cb *rcb,
			drm_reservation_cb_func_t func, void *context)
{
	INIT_WORK(&rcb->work, reservation_cb_work);
	atomic_set(&rcb->count, 1);
	rcb->num_fence_cbs = 0;
	rcb->fence_cbs = NULL;
	rcb->func = func;
	rcb->context = context;
}
EXPORT_SYMBOL(drm_reservation_cb_init);

int
drm_reservation_cb_add(struct drm_reservation_cb *rcb,
			struct reservation_object *resv, bool exclusive)
{
	int ret = 0;
	struct fence *fence;
	unsigned shared_count = 0, f;
	struct fence **shared_fences = NULL;

	/* enum all the fences in the reservation and add callbacks */
	ret = reservation_object_get_fences_rcu(resv, &fence,
					&shared_count, &shared_fences);
	if (ret < 0)
		return ret;

	if (fence) {
		ret = reservation_cb_add_fence_cb(rcb, fence);
		if (ret < 0) {
			reservation_cb_cleanup(rcb);
			goto error;
		}
	}

	if (exclusive) {
		for (f = 0; f < shared_count; f++) {
			ret = reservation_cb_add_fence_cb(rcb,
						shared_fences[f]);
			if (ret < 0) {
				reservation_cb_cleanup(rcb);
				goto error;
			}
		}
	}

error:
	if (fence)
		fence_put(fence);
	if (shared_fences) {
		for (f = 0; f < shared_count; f++)
			fence_put(shared_fences[f]);
		kfree(shared_fences);
	}
	return ret;
}
EXPORT_SYMBOL(drm_reservation_cb_add);

void
drm_reservation_cb_done(struct drm_reservation_cb *rcb)
{
	/*
	 * we need to decrement from initial 1
	 * and trigger the callback in case all the
	 * fences were already triggered
	 */
	if (atomic_dec_and_test(&rcb->count)) {
		/*
		 * we could call the callback here directly but in case
		 * the callback function needs to lock the same mutex
		 * as our caller it could cause a deadlock, so it is
		 * safer to call it from a worker
		 */
		schedule_work(&rcb->work);
	}
}
EXPORT_SYMBOL(drm_reservation_cb_done);

void
drm_reservation_cb_fini(struct drm_reservation_cb *rcb)
{
	/* make sure no work will be triggered */
	atomic_set(&rcb->count, 0);
	cancel_work_sync(&rcb->work);
	reservation_cb_cleanup(rcb);
}
EXPORT_SYMBOL(drm_reservation_cb_fini);

static bool sw_fence_enable_signaling(struct fence *f)
{
	return true;
}

static const char *sw_fence_get_get_driver_name(struct fence *fence)
{
	return "drm_sync_helper";
}

static const char *sw_fence_get_timeline_name(struct fence *f)
{
	return "drm_sync.sw";
}

static const struct fence_ops sw_fence_ops = {
	.get_driver_name = sw_fence_get_get_driver_name,
	.get_timeline_name = sw_fence_get_timeline_name,
	.enable_signaling = sw_fence_enable_signaling,
	.signaled = NULL,
	.wait = fence_default_wait,
	.release = NULL
};

struct fence *drm_sw_fence_new(unsigned int context, unsigned seqno)
{
	struct fence *fence;

	fence = kzalloc(sizeof(*fence), GFP_KERNEL);
	if (!fence)
		return ERR_PTR(-ENOMEM);
	fence_init(fence,
		   &sw_fence_ops,
		   &sw_fence_lock,
		   context, seqno);

	return fence;
}
EXPORT_SYMBOL(drm_sw_fence_new);