summaryrefslogtreecommitdiff
path: root/drivers/gpu/arm/utgard/linux/mali_sync.c
blob: dc1e3a2a4d73597715481124c734bbb5ae0cdf52 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * Copyright (C) 2012-2015 ARM Limited. All rights reserved.
 * 
 * This program is free software and is provided to you under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
 * 
 * A copy of the licence is included with the program, and can also be obtained from Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "mali_sync.h"

#include "mali_osk.h"
#include "mali_kernel_common.h"
#include "mali_timeline.h"
#include "mali_executor.h"

#include <linux/file.h>
#include <linux/seq_file.h>
#include <linux/module.h>
#include <linux/fcntl.h>

struct mali_sync_pt {
	struct sync_pt         sync_pt;
	struct mali_sync_flag *flag;
	struct sync_timeline *sync_tl;  /**< Sync timeline this pt is connected to. */
};

/**
 * The sync flag is used to connect sync fences to the Mali Timeline system.  Sync fences can be
 * created from a sync flag, and when the flag is signaled, the sync fences will also be signaled.
 */
struct mali_sync_flag {
	struct sync_timeline *sync_tl;  /**< Sync timeline this flag is connected to. */
	u32                   point;    /**< Point on timeline. */
	int                   status;   /**< 0 if unsignaled, 1 if signaled without error or negative if signaled with error. */
	struct kref           refcount; /**< Reference count. */
};

/**
 * Mali sync timeline is used to connect mali timeline to sync_timeline.
 * When fence timeout can print more detailed mali timeline system info.
 */
struct mali_sync_timeline_container {
	struct sync_timeline sync_timeline;
	struct mali_timeline *timeline;
};

MALI_STATIC_INLINE struct mali_sync_pt *to_mali_sync_pt(struct sync_pt *pt)
{
	return container_of(pt, struct mali_sync_pt, sync_pt);
}

MALI_STATIC_INLINE struct mali_sync_timeline_container *to_mali_sync_tl_container(struct sync_timeline *sync_tl)
{
	return container_of(sync_tl, struct mali_sync_timeline_container, sync_timeline);
}

static struct sync_pt *timeline_dup(struct sync_pt *pt)
{
	struct mali_sync_pt *mpt, *new_mpt;
	struct sync_pt *new_pt;

	MALI_DEBUG_ASSERT_POINTER(pt);
	mpt = to_mali_sync_pt(pt);

	new_pt = sync_pt_create(mpt->sync_tl, sizeof(struct mali_sync_pt));
	if (NULL == new_pt) return NULL;

	new_mpt = to_mali_sync_pt(new_pt);

	mali_sync_flag_get(mpt->flag);
	new_mpt->flag = mpt->flag;
	new_mpt->sync_tl = mpt->sync_tl;

	return new_pt;
}

static int timeline_has_signaled(struct sync_pt *pt)
{
	struct mali_sync_pt *mpt;

	MALI_DEBUG_ASSERT_POINTER(pt);
	mpt = to_mali_sync_pt(pt);

	MALI_DEBUG_ASSERT_POINTER(mpt->flag);

	return mpt->flag->status;
}

static int timeline_compare(struct sync_pt *pta, struct sync_pt *ptb)
{
	struct mali_sync_pt *mpta;
	struct mali_sync_pt *mptb;
	u32 a, b;

	MALI_DEBUG_ASSERT_POINTER(pta);
	MALI_DEBUG_ASSERT_POINTER(ptb);
	mpta = to_mali_sync_pt(pta);
	mptb = to_mali_sync_pt(ptb);

	MALI_DEBUG_ASSERT_POINTER(mpta->flag);
	MALI_DEBUG_ASSERT_POINTER(mptb->flag);

	a = mpta->flag->point;
	b = mptb->flag->point;

	if (a == b) return 0;

	return ((b - a) < (a - b) ? -1 : 1);
}

static void timeline_free_pt(struct sync_pt *pt)
{
	struct mali_sync_pt *mpt;

	MALI_DEBUG_ASSERT_POINTER(pt);
	mpt = to_mali_sync_pt(pt);

	mali_sync_flag_put(mpt->flag);
}

static void timeline_release(struct sync_timeline *sync_timeline)
{
	struct mali_sync_timeline_container *mali_sync_tl = NULL;
	struct mali_timeline *mali_tl = NULL;

	MALI_DEBUG_ASSERT_POINTER(sync_timeline);

	mali_sync_tl = to_mali_sync_tl_container(sync_timeline);
	MALI_DEBUG_ASSERT_POINTER(mali_sync_tl);

	mali_tl = mali_sync_tl->timeline;

	/* always signaled timeline didn't have mali container */
	if (mali_tl) {
		if (NULL != mali_tl->spinlock) {
			mali_spinlock_reentrant_term(mali_tl->spinlock);
		}
		_mali_osk_free(mali_tl);
	}

	module_put(THIS_MODULE);
}

#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
static void timeline_print_pt(struct seq_file *s, struct sync_pt *sync_pt)
{
	struct mali_sync_pt *mpt;

	MALI_DEBUG_ASSERT_POINTER(s);
	MALI_DEBUG_ASSERT_POINTER(sync_pt);

	mpt = to_mali_sync_pt(sync_pt);

	/* It is possible this sync point is just under construct,
	 * make sure the flag is valid before accessing it
	*/
	if (mpt->flag) {
		seq_printf(s, "%u", mpt->flag->point);
	} else {
		seq_printf(s, "uninitialized");
	}
}

static void timeline_print_obj(struct seq_file *s, struct sync_timeline *sync_tl)
{
	struct mali_sync_timeline_container *mali_sync_tl = NULL;
	struct mali_timeline *mali_tl = NULL;

	MALI_DEBUG_ASSERT_POINTER(sync_tl);

	mali_sync_tl = to_mali_sync_tl_container(sync_tl);
	MALI_DEBUG_ASSERT_POINTER(mali_sync_tl);

	mali_tl = mali_sync_tl->timeline;

	if (NULL != mali_tl) {
		seq_printf(s, "oldest (%u) ", mali_tl->point_oldest);
		seq_printf(s, "next (%u)", mali_tl->point_next);
		seq_printf(s, "\n");

#if defined(MALI_TIMELINE_DEBUG_FUNCTIONS)
		{
			u32 tid = _mali_osk_get_tid();
			struct mali_timeline_system *system = mali_tl->system;

			mali_spinlock_reentrant_wait(mali_tl->spinlock, tid);
			if (!mali_tl->destroyed) {
				mali_spinlock_reentrant_wait(system->spinlock, tid);
				mali_timeline_debug_print_timeline(mali_tl, s);
				mali_spinlock_reentrant_signal(system->spinlock, tid);
			}
			mali_spinlock_reentrant_signal(mali_tl->spinlock, tid);

			/* dump job queue status and group running status */
			mali_executor_status_dump();
		}
#endif
	}
}
#else
static void timeline_pt_value_str(struct sync_pt *pt, char *str, int size)
{
	struct mali_sync_pt *mpt;

	MALI_DEBUG_ASSERT_POINTER(str);
	MALI_DEBUG_ASSERT_POINTER(pt);

	mpt = to_mali_sync_pt(pt);

	/* It is possible this sync point is just under construct,
	 * make sure the flag is valid before accessing it
	*/
	if (mpt->flag) {
		_mali_osk_snprintf(str, size, "%u", mpt->flag->point);
	} else {
		_mali_osk_snprintf(str, size, "uninitialized");
	}
}

static void timeline_value_str(struct sync_timeline *timeline, char *str, int size)
{
	struct mali_sync_timeline_container *mali_sync_tl = NULL;
	struct mali_timeline *mali_tl = NULL;

	MALI_DEBUG_ASSERT_POINTER(timeline);

	mali_sync_tl = to_mali_sync_tl_container(timeline);
	MALI_DEBUG_ASSERT_POINTER(mali_sync_tl);

	mali_tl = mali_sync_tl->timeline;

	if (NULL != mali_tl) {
		_mali_osk_snprintf(str, size, "oldest (%u) ", mali_tl->point_oldest);
		_mali_osk_snprintf(str, size, "next (%u)", mali_tl->point_next);
		_mali_osk_snprintf(str, size, "\n");

#if defined(MALI_TIMELINE_DEBUG_FUNCTIONS)
		{
			u32 tid = _mali_osk_get_tid();
			struct mali_timeline_system *system = mali_tl->system;

			mali_spinlock_reentrant_wait(mali_tl->spinlock, tid);
			if (!mali_tl->destroyed) {
				mali_spinlock_reentrant_wait(system->spinlock, tid);
				mali_timeline_debug_direct_print_timeline(mali_tl);
				mali_spinlock_reentrant_signal(system->spinlock, tid);
			}
			mali_spinlock_reentrant_signal(mali_tl->spinlock, tid);

			/* dump job queue status and group running status */
			mali_executor_status_dump();
		}
#endif
	}
}
#endif


static struct sync_timeline_ops mali_timeline_ops = {
	.driver_name    = "Mali",
	.dup            = timeline_dup,
	.has_signaled   = timeline_has_signaled,
	.compare        = timeline_compare,
	.free_pt        = timeline_free_pt,
	.release_obj    = timeline_release,
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
	.print_pt       = timeline_print_pt,
	.print_obj      = timeline_print_obj,
#else
	.pt_value_str = timeline_pt_value_str,
	.timeline_value_str = timeline_value_str,
#endif
};

struct sync_timeline *mali_sync_timeline_create(struct mali_timeline *timeline, const char *name)
{
	struct sync_timeline *sync_tl;
	struct mali_sync_timeline_container *mali_sync_tl;

	sync_tl = sync_timeline_create(&mali_timeline_ops, sizeof(struct mali_sync_timeline_container), name);
	if (NULL == sync_tl) return NULL;

	mali_sync_tl = to_mali_sync_tl_container(sync_tl);
	mali_sync_tl->timeline = timeline;

	/* Grab a reference on the module to ensure the callbacks are present
	 * as long some timeline exists. The reference is released when the
	 * timeline is freed.
	 * Since this function is called from a ioctl on an open file we know
	 * we already have a reference, so using __module_get is safe. */
	__module_get(THIS_MODULE);

	return sync_tl;
}

s32 mali_sync_fence_fd_alloc(struct sync_fence *sync_fence)
{
	s32 fd = -1;

	fd = get_unused_fd_flags(O_CLOEXEC);
	if (fd < 0) {
		sync_fence_put(sync_fence);
		return -1;
	}
	sync_fence_install(sync_fence, fd);

	return fd;
}

struct sync_fence *mali_sync_fence_merge(struct sync_fence *sync_fence1, struct sync_fence *sync_fence2)
{
	struct sync_fence *sync_fence;

	MALI_DEBUG_ASSERT_POINTER(sync_fence1);
	MALI_DEBUG_ASSERT_POINTER(sync_fence1);

	sync_fence = sync_fence_merge("mali_merge_fence", sync_fence1, sync_fence2);
	sync_fence_put(sync_fence1);
	sync_fence_put(sync_fence2);

	return sync_fence;
}

struct sync_fence *mali_sync_timeline_create_signaled_fence(struct sync_timeline *sync_tl)
{
	struct mali_sync_flag *flag;
	struct sync_fence *sync_fence;

	MALI_DEBUG_ASSERT_POINTER(sync_tl);

	flag = mali_sync_flag_create(sync_tl, 0);
	if (NULL == flag) return NULL;

	sync_fence = mali_sync_flag_create_fence(flag);

	mali_sync_flag_signal(flag, 0);
	mali_sync_flag_put(flag);

	return sync_fence;
}

struct mali_sync_flag *mali_sync_flag_create(struct sync_timeline *sync_tl, mali_timeline_point point)
{
	struct mali_sync_flag *flag;

	if (NULL == sync_tl) return NULL;

	flag = _mali_osk_calloc(1, sizeof(*flag));
	if (NULL == flag) return NULL;

	flag->sync_tl = sync_tl;
	flag->point = point;

	flag->status = 0;
	kref_init(&flag->refcount);

	return flag;
}

void mali_sync_flag_get(struct mali_sync_flag *flag)
{
	MALI_DEBUG_ASSERT_POINTER(flag);
	kref_get(&flag->refcount);
}

/**
 * Free sync flag.
 *
 * @param ref kref object embedded in sync flag that should be freed.
 */
static void mali_sync_flag_free(struct kref *ref)
{
	struct mali_sync_flag *flag;

	MALI_DEBUG_ASSERT_POINTER(ref);
	flag = container_of(ref, struct mali_sync_flag, refcount);

	_mali_osk_free(flag);
}

void mali_sync_flag_put(struct mali_sync_flag *flag)
{
	MALI_DEBUG_ASSERT_POINTER(flag);
	kref_put(&flag->refcount, mali_sync_flag_free);
}

void mali_sync_flag_signal(struct mali_sync_flag *flag, int error)
{
	MALI_DEBUG_ASSERT_POINTER(flag);

	MALI_DEBUG_ASSERT(0 == flag->status);
	flag->status = (0 > error) ? error : 1;

	_mali_osk_write_mem_barrier();

	sync_timeline_signal(flag->sync_tl);
}

/**
 * Create a sync point attached to given sync flag.
 *
 * @note Sync points must be triggered in *exactly* the same order as they are created.
 *
 * @param flag Sync flag.
 * @return New sync point if successful, NULL if not.
 */
static struct sync_pt *mali_sync_flag_create_pt(struct mali_sync_flag *flag)
{
	struct sync_pt *pt;
	struct mali_sync_pt *mpt;

	MALI_DEBUG_ASSERT_POINTER(flag);
	MALI_DEBUG_ASSERT_POINTER(flag->sync_tl);

	pt = sync_pt_create(flag->sync_tl, sizeof(struct mali_sync_pt));
	if (NULL == pt) return NULL;

	mali_sync_flag_get(flag);

	mpt = to_mali_sync_pt(pt);
	mpt->flag = flag;
	mpt->sync_tl = flag->sync_tl;

	return pt;
}

struct sync_fence *mali_sync_flag_create_fence(struct mali_sync_flag *flag)
{
	struct sync_pt    *sync_pt;
	struct sync_fence *sync_fence;

	MALI_DEBUG_ASSERT_POINTER(flag);
	MALI_DEBUG_ASSERT_POINTER(flag->sync_tl);

	sync_pt = mali_sync_flag_create_pt(flag);
	if (NULL == sync_pt) return NULL;

	sync_fence = sync_fence_create("mali_flag_fence", sync_pt);
	if (NULL == sync_fence) {
		sync_pt_free(sync_pt);
		return NULL;
	}

	return sync_fence;
}