aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/mali400/r3p2/mali/linux/mali_sync.c
blob: 629361050609ec3f5295b5004fd2543670ef8148 (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
/*
 * Copyright (C) 2012 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.
 */

/**
 * @file mali_sync.c
 *
 */

#include <linux/seq_file.h>
#include <linux/sync.h>
#include <linux/timer.h>

#include "mali_osk.h"
#include "mali_kernel_common.h"

struct mali_sync_timeline
{
	struct sync_timeline timeline;
	atomic_t counter;
	atomic_t signalled;
};

struct mali_sync_pt
{
	struct sync_pt pt;
	u32 order;
	s32 error;
	struct timer_list timer;
};

static void mali_sync_timed_pt_timeout(unsigned long data);

static inline struct mali_sync_timeline *to_mali_sync_timeline(struct sync_timeline *timeline)
{
	return container_of(timeline, struct mali_sync_timeline, timeline);
}

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

static struct sync_pt *timeline_dup(struct sync_pt *pt)
{
	struct mali_sync_pt *mpt = to_mali_sync_pt(pt);
	struct mali_sync_pt *new_mpt;
	struct sync_pt *new_pt = sync_pt_create(pt->parent, sizeof(struct mali_sync_pt));

	if (!new_pt)
	{
		return NULL;
	}

	new_mpt = to_mali_sync_pt(new_pt);
	new_mpt->order = mpt->order;

	return new_pt;

}

static int timeline_has_signaled(struct sync_pt *pt)
{
	struct mali_sync_pt *mpt = to_mali_sync_pt(pt);
	struct mali_sync_timeline *mtl = to_mali_sync_timeline(pt->parent);
	long diff;

	if (0 != mpt->error)
	{
		return mpt->error;
	}

	diff = atomic_read(&mtl->signalled) - mpt->order;

	return diff >= 0;
}

static int timeline_compare(struct sync_pt *a, struct sync_pt *b)
{
	struct mali_sync_pt *ma = container_of(a, struct mali_sync_pt, pt);
	struct mali_sync_pt *mb = container_of(b, struct mali_sync_pt, pt);

	long diff = ma->order - mb->order;

	if (diff < 0)
	{
		return -1;
	}
	else if (diff == 0)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

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

	if (mpt->timer.function == mali_sync_timed_pt_timeout)
	{
		del_timer_sync(&mpt->timer);
	}
}

static void timeline_print_tl(struct seq_file *s, struct sync_timeline *sync_timeline)
{
	struct mali_sync_timeline *mtl = to_mali_sync_timeline(sync_timeline);

	seq_printf(s, "%u, %u", atomic_read(&mtl->signalled), atomic_read(&mtl->counter));
}

static void timeline_print_pt(struct seq_file *s, struct sync_pt *sync_pt)
{
	struct mali_sync_pt *mpt = to_mali_sync_pt(sync_pt);

	seq_printf(s, "%u", mpt->order);

}

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,
	.print_obj      = timeline_print_tl,
	.print_pt       = timeline_print_pt
};

int mali_sync_timeline_is_ours(struct sync_timeline *timeline)
{
	return (timeline->ops == &mali_timeline_ops);
}

struct sync_timeline *mali_sync_timeline_alloc(const char * name)
{
	struct sync_timeline *tl;
	struct mali_sync_timeline *mtl;

	tl = sync_timeline_create(&mali_timeline_ops,
	                          sizeof(struct mali_sync_timeline), name);
	if (!tl)
	{
		return NULL;
	}

	/* Set the counter in our private struct */
	mtl = to_mali_sync_timeline(tl);
	atomic_set(&mtl->counter, 0);
	atomic_set(&mtl->signalled, 0);

	return tl;
}

struct sync_pt *mali_sync_pt_alloc(struct sync_timeline *parent)
{
	struct sync_pt *pt = sync_pt_create(parent, sizeof(struct mali_sync_pt));
	struct mali_sync_timeline *mtl = to_mali_sync_timeline(parent);
	struct mali_sync_pt *mpt;

	if (!pt)
	{
		return NULL;
	}

	mpt = to_mali_sync_pt(pt);
	mpt->order = atomic_inc_return(&mtl->counter);
	mpt->error = 0;

	return pt;
}

static void mali_sync_timed_pt_timeout(unsigned long data)
{
	struct sync_pt *pt = (struct sync_pt *)data;

	MALI_DEBUG_ASSERT_POINTER(pt);

	mali_sync_signal_pt(pt, -ETIME);
}

struct sync_pt *mali_sync_timed_pt_alloc(struct sync_timeline *parent)
{
	struct sync_pt *pt;
	struct mali_sync_pt *mpt;
	const u32 timeout = msecs_to_jiffies(MALI_SYNC_TIMED_FENCE_TIMEOUT);

	pt = mali_sync_pt_alloc(parent);
	if (NULL == pt) return NULL;
	mpt = to_mali_sync_pt(pt);

	init_timer(&mpt->timer);

	mpt->timer.function = mali_sync_timed_pt_timeout;
	mpt->timer.data = (unsigned long)pt;
	mpt->timer.expires = jiffies + timeout;

	add_timer(&mpt->timer);

	return pt;
}

/*
 * Returns 0 if sync_pt has been committed and are ready for use, -ETIME if
 * timeout already happened and the fence has been signalled.
 *
 * If an error occurs the sync point can not be used.
 */
int mali_sync_timed_commit(struct sync_pt *pt)
{
	struct mali_sync_pt *mpt = to_mali_sync_pt(pt);
	int ret;

	if (!mali_sync_timeline_is_ours(pt->parent))
	{
		return -EINVAL;
	}

	/* Stop timer */
	ret = del_timer_sync(&mpt->timer);

	if (0 == ret)
	{
		return -ETIME;
	}

	MALI_DEBUG_ASSERT(0 == timeline_has_signaled(pt));

	return 0;
}

void mali_sync_signal_pt(struct sync_pt *pt, int error)
{
	struct mali_sync_pt *mpt = to_mali_sync_pt(pt);
	struct mali_sync_timeline *mtl = to_mali_sync_timeline(pt->parent);
	int signalled;
	long diff;

	if (0 != error)
	{
		MALI_DEBUG_ASSERT(0 > error);
		mpt->error = error;
	}

	do {

		signalled = atomic_read(&mtl->signalled);

		diff = signalled - mpt->order;

		if (diff > 0)
		{
			/* The timeline is already at or ahead of this point. This should not happen unless userspace
			 * has been signalling fences out of order, so warn but don't violate the sync_pt API.
			 * The warning is only in debug builds to prevent a malicious user being able to spam dmesg.
			 */
			MALI_DEBUG_PRINT_ERROR(("Sync points were triggerd in a different order to allocation!\n"));
			return;
		}
	} while (atomic_cmpxchg(&mtl->signalled, signalled, mpt->order) != signalled);

	sync_timeline_signal(pt->parent);
}