summaryrefslogtreecommitdiff
path: root/drivers/gpu/ion/sprd/sprd_fence.c
blob: 0baafe89a4f65e77ef7f85ade334bb689f4dbe23 (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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
 * Copyright (C) 2012 Spreadtrum Communications 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 "sprd_fence.h"
#include <linux/kernel.h>
#include <linux/file.h>
#include <linux/fs.h>

#define WAIT_FENCE_TIMEOUT 200

/*
 *  Porting from software sync
 *  (sw_sync.h sw_sync.c)
 * */
static int sprd_sync_cmp(u32 a, u32 b)
{
    if (a == b)
        return 0;

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

struct sync_pt *sprd_sync_pt_create(struct sprd_sync_timeline *obj, u32 value)
{
    struct sprd_sync_pt *pt;

    pt = (struct sprd_sync_pt *)
        sync_pt_create(&obj->obj, sizeof(struct sprd_sync_pt));

    pt->value = value;

    return (struct sync_pt *)pt;
}

static struct sync_pt *sprd_sync_pt_dup(struct sync_pt *sync_pt)
{
    struct sprd_sync_pt *pt = (struct sprd_sync_pt *) sync_pt;
    struct sprd_sync_timeline *obj =
        (struct sprd_sync_timeline *)sync_pt->parent;

    return (struct sync_pt *) sprd_sync_pt_create(obj, pt->value);
}

static int sprd_sync_pt_has_signaled(struct sync_pt *sync_pt)
{
    struct sprd_sync_pt *pt = (struct sprd_sync_pt *)sync_pt;
    struct sprd_sync_timeline *obj =
        (struct sprd_sync_timeline *)sync_pt->parent;

    return sprd_sync_cmp(obj->value, pt->value) >= 0;
}

static int sprd_sync_pt_compare(struct sync_pt *a, struct sync_pt *b)
{
    struct sprd_sync_pt *pt_a = (struct sprd_sync_pt *)a;
    struct sprd_sync_pt *pt_b = (struct sprd_sync_pt *)b;

    return sprd_sync_cmp(pt_a->value, pt_b->value);
}


static int sprd_sync_fill_driver_data(struct sync_pt *sync_pt,
                                    void *data, int size)
{
    struct sprd_sync_pt *pt = (struct sprd_sync_pt *)sync_pt;

    if (size < sizeof(pt->value))
        return -ENOMEM;

    memcpy(data, &pt->value, sizeof(pt->value));

    return sizeof(pt->value);
}

static void sprd_sync_timeline_value_str(struct sync_timeline *sync_timeline,
                                       char *str, int size)
{
    struct sprd_sync_timeline *timeline =
        (struct sprd_sync_timeline *)sync_timeline;
    snprintf(str, size, "%d", timeline->value);
}

static void sprd_sync_pt_value_str(struct sync_pt *sync_pt,
                                 char *str, int size)
{
    struct sprd_sync_pt *pt = (struct sprd_sync_pt *)sync_pt;
    snprintf(str, size, "%d", pt->value);
}

static struct sync_timeline_ops sprd_sync_timeline_ops = {
    .driver_name = "sprd_sync",
    .dup = sprd_sync_pt_dup,
    .has_signaled = sprd_sync_pt_has_signaled,
    .compare = sprd_sync_pt_compare,
    .fill_driver_data = sprd_sync_fill_driver_data,
    .timeline_value_str = sprd_sync_timeline_value_str,
    .pt_value_str = sprd_sync_pt_value_str,
};

struct sprd_sync_timeline *sprd_sync_timeline_create(const char *name)
{
    struct sprd_sync_timeline *obj = (struct sprd_sync_timeline *)
        sync_timeline_create(&sprd_sync_timeline_ops,
                             sizeof(struct sprd_sync_timeline),
                             name);

    return obj;
}

void sprd_sync_timeline_inc(struct sprd_sync_timeline *obj, u32 inc)
{
    obj->value += inc;

    sync_timeline_signal(&obj->obj);
}

/*
 *  Porting software sync done
 * */

/*
 *  user interface
 * */

/*
 *  Now build two timeline,
 *  one for PrimaryDisplayDevice,
 *  the other for VirtualDisplayDevice.
 *  If we only use single timeline, the later Device will
 *  block the former Device.
 * */
static struct sync_timeline_data sprd_timeline;
static struct sync_timeline_data sprd_timeline_virtual;
int sprd_create_timeline(enum SPRD_DEVICE_SYNC_TYPE type)
{
	if (type == SPRD_DEVICE_PRIMARY_SYNC)
	{
		mutex_lock(&(sprd_timeline.sync_mutex));
		if (sprd_timeline.timeline == NULL)
		{
			sprd_timeline.timeline = sprd_sync_timeline_create("sprd-timeline");
			if (sprd_timeline.timeline == NULL)
			{
				printk(KERN_ERR "create_timeline, cannot create time line\n");
				mutex_unlock(&(sprd_timeline.sync_mutex));
				return -ENOMEM;
			}
			else
			{
				sprd_timeline.timeline_value = 0;
			}
		}
		mutex_unlock(&(sprd_timeline.sync_mutex));
	}
	else if (type == SPRD_DEVICE_VIRTUAL_SYNC)
	{
		mutex_lock(&(sprd_timeline_virtual.sync_mutex));
		if (sprd_timeline_virtual.timeline == NULL)
		{
			sprd_timeline_virtual.timeline = sprd_sync_timeline_create("sprd-timeline-virtual");
			if (sprd_timeline_virtual.timeline == NULL)
			{
				printk(KERN_ERR "create_timeline, cannot create virtual time line\n");
				mutex_unlock(&(sprd_timeline_virtual.sync_mutex));
				return -ENOMEM;
			}
			else
			{
				sprd_timeline_virtual.timeline_value = 0;
			}
		}
		mutex_unlock(&(sprd_timeline_virtual.sync_mutex));
	}

    return 0;
}

int sprd_destroy_timeline(struct sync_timeline_data *parent)
{
	struct sprd_sync_timeline *obj = NULL;

	if (parent->timeline == NULL)
	{
		printk(KERN_ERR "sprd_fence timeline has been released\n");
		return 0;
	}

	mutex_lock(&(parent->sync_mutex));
	obj = parent->timeline;
	sync_timeline_destroy(&obj->obj);
	mutex_unlock(&(parent->sync_mutex));

	return 0;
}

int open_sprd_sync_timeline(void)
{
	int ret = -1;
	static int init = 0;

	if (init == 0)
	{
		sprd_timeline.timeline_value = -1;
		sprd_timeline.timeline = NULL;
		mutex_init(&(sprd_timeline.sync_mutex));

		sprd_timeline_virtual.timeline_value = -1;
		sprd_timeline_virtual.timeline = NULL;
		mutex_init(&(sprd_timeline_virtual.sync_mutex));

		init = 1;
	}

	ret = sprd_create_timeline(SPRD_DEVICE_PRIMARY_SYNC);
	if (ret < 0)
	{
		return ret;
	}
	ret = sprd_create_timeline(SPRD_DEVICE_VIRTUAL_SYNC);

	return ret;
}

int close_sprd_sync_timeline(void)
{
	int ret = -1;

	ret = sprd_destroy_timeline(&sprd_timeline);

	ret = sprd_destroy_timeline(&sprd_timeline_virtual);

	return ret;
}

int sprd_fence_create(struct ion_fence_data *data, char *name)
{
	int fd = get_unused_fd();
	struct sync_pt *pt;
	struct sync_fence *fence;
	struct sync_timeline_data *parent = NULL;

	if (data == NULL || name == NULL)
	{
		printk(KERN_ERR "sprd_fence_create input para is NULL\n");
		return -EFAULT;
	}


	if (fd < 0)
	{
		fd = get_unused_fd();
		if (fd < 0)
		{
			printk(KERN_ERR "sprd_sync_pt_create failed to get fd\n");
		}
		return -EFAULT;
	}

	if (data->device_type == SPRD_DEVICE_PRIMARY_SYNC)
	{
		parent = &sprd_timeline;
	}
	else if (data->device_type == SPRD_DEVICE_VIRTUAL_SYNC)
	{
		parent = &sprd_timeline_virtual;
	}

	if (parent == NULL)
	{
		printk(KERN_ERR "sprd_fence_create failed to get sync timeline\n");
		return -EFAULT;
	}

	mutex_lock(&(parent->sync_mutex));

	pt = sprd_sync_pt_create(parent->timeline, parent->timeline_value + data->life_value);
	if (pt == NULL)
	{
		printk(KERN_ERR "sprd_sync_pt_create failed\n");
		goto err;
	}

	fence = sync_fence_create(name, pt);
	if (fence == NULL)
	{
		sync_pt_free(pt);
		printk(KERN_ERR "sprd_create_fence failed\n");
		goto err;
	}

	sync_fence_install(fence, fd);

	mutex_unlock(&(parent->sync_mutex));

	pr_debug("create a fence: %p, fd: %d, life_value: %d, name: %s\n",
			(void *)fence, fd, data->life_value, name);

    return fd;

err:
   put_unused_fd(fd);
   mutex_unlock(&(parent->sync_mutex));
   return -ENOMEM;
}

int sprd_fence_destroy(struct ion_fence_data *data)
{
	if (data == NULL)
	{
		printk(KERN_ERR "sprd_fence_destroy parameters NULL\n");
		return -EFAULT;
	}

	if (data->release_fence_fd >= 0)
	{
		struct sync_fence *fence = sync_fence_fdget(data->release_fence_fd);
		if (fence == NULL)
		{
			printk(KERN_ERR "sprd_fence_destroy failed fence == NULL\n");
			return -EFAULT;
		}
	}
	else if (data->retired_fence_fd >= 0)
	{
		struct sync_fence *fence = sync_fence_fdget(data->retired_fence_fd);
		if (fence == NULL)
		{
			printk(KERN_ERR "sprd_fence_destroy failed fence == NULL\n");
		}	return -EFAULT;
	}

	return 0;
}
int sprd_fence_build(struct ion_fence_data *data)
{
	if (data == NULL)
	{
		printk(KERN_ERR "sprd_fence_build input para is NULL\n");
		return -EFAULT;
	}

	data->release_fence_fd = sprd_fence_create(data, "HWCRelease");
	if (data->release_fence_fd < 0)
	{
		printk(KERN_ERR "sprd_fence_build failed to create release fence\n");
		return -ENOMEM;
	}

	data->retired_fence_fd = sprd_fence_create(data, "HWCRetire");
	if (data->retired_fence_fd < 0)
	{
		printk(KERN_ERR "sprd_fence_build failed to create release fence\n");
		return -ENOMEM;
	}

	return 0;
}

int sprd_fence_signal_timeline(struct sync_timeline_data *parent)
{
	if (parent == NULL)
	{
		printk(KERN_ERR "sprd_fence_signal_timeline timeline is NULL\n");
		return -EFAULT;
	}

	mutex_lock(&(parent->sync_mutex));

	if (parent->timeline)
	{
		sprd_sync_timeline_inc(parent->timeline, 1);
		parent->timeline_value++;

		/*
		 *  For avoiding overflow
		 * */
		if (parent->timeline_value < 0)
		{
			parent->timeline_value = 0;
		}
	}

	mutex_unlock(&(parent->sync_mutex));

	pr_debug("sprd_signal_fence value: %d\n", parent->timeline_value);

	return 0;
}
int sprd_fence_signal(struct ion_fence_data *data)
{
	struct sync_timeline_data *parent = NULL;
	enum SPRD_DEVICE_SYNC_TYPE device_type= SPRD_DEVICE_PRIMARY_SYNC;

	if (data == NULL)
	{
		printk(KERN_ERR "sprd_fence_signal parameter is NULL\n");
		return -EFAULT;
	}

	if (data->device_type == SPRD_DEVICE_PRIMARY_SYNC)
	{
		parent = &sprd_timeline;
	}
	else if (data->device_type == SPRD_DEVICE_VIRTUAL_SYNC)
	{
		parent = &sprd_timeline_virtual;
	}

	if (parent == NULL)
	{
		printk(KERN_ERR "sprd_fence_signal failed to get sync timeline\n");
		return -EFAULT;
	}

	sprd_fence_signal_timeline(parent);
	pr_debug("sprd_signal_fence device_type: %d\n", data->device_type);

	return 0;
}

int sprd_fence_wait(int fence_fd)
{
	int ret = 0;

	struct sync_fence *fence = NULL;

	if (fence_fd < 0)
	{
		printk(KERN_ERR "sprd_wait_fence input parameters is NULL\n");
		return -EFAULT;
	}

	fence = sync_fence_fdget(fence_fd);
	if (fence == NULL)
	{
		printk(KERN_ERR "sprd_fence_wait failed fence == NULL\n");
		return -EFAULT;
	}

	ret = sync_fence_wait(fence, WAIT_FENCE_TIMEOUT);
	sync_fence_put(fence);
	if (ret < 0)
	{
		printk(KERN_ERR "sync_fence_wait failed, ret = %x\n", ret);
	}

	pr_debug("sprd_wait_fence wait fence: %p done\n", (void *)fence);

	return ret;
}

struct sync_pt *sprd_fence_dup(struct sync_pt *sync_pt)
{
	struct sync_pt *pt = NULL;

	pt = sprd_sync_pt_dup(sync_pt);

	return pt;
}