aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/samsung/mali/common/mali_kernel_profiling.c
blob: ca04b5fe86b39063a2fe229fc4be99aef954b1ae (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
/*
 * Copyright (C) 2010 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_kernel_common.h"
#include "mali_osk.h"
#include "mali_osk_mali.h"
#include "mali_ukk.h"
#include "mali_timestamp.h"
#include "mali_kernel_profiling.h"
#include "mali_linux_trace.h"

typedef struct mali_profiling_entry
{
	u64 timestamp;
	u32 event_id;
	u32 data[5];
} mali_profiling_entry;


typedef enum mali_profiling_state
{
	MALI_PROFILING_STATE_UNINITIALIZED,
	MALI_PROFILING_STATE_IDLE,
	MALI_PROFILING_STATE_RUNNING,
	MALI_PROFILING_STATE_RETURN,
} mali_profiling_state;


static _mali_osk_lock_t *lock = NULL;
static mali_profiling_state prof_state = MALI_PROFILING_STATE_UNINITIALIZED;
static mali_profiling_entry* profile_entries = NULL;
static u32 profile_entry_count = 0;
static _mali_osk_atomic_t profile_insert_index;
static _mali_osk_atomic_t profile_entries_written;
static mali_bool mali_profiling_default_enable = MALI_FALSE;

_mali_osk_errcode_t _mali_profiling_init(mali_bool auto_start)
{
	profile_entries = NULL;
	profile_entry_count = 0;
	_mali_osk_atomic_init(&profile_insert_index, 0);
	_mali_osk_atomic_init(&profile_entries_written, 0);

	lock = _mali_osk_lock_init( _MALI_OSK_LOCKFLAG_SPINLOCK | _MALI_OSK_LOCKFLAG_NONINTERRUPTABLE, 0, 0 );
	if (NULL == lock)
	{
		return _MALI_OSK_ERR_FAULT;
	}

	prof_state = MALI_PROFILING_STATE_IDLE;

	if (MALI_TRUE == auto_start)
	{
		u32 limit = MALI_PROFILING_MAX_BUFFER_ENTRIES; /* Use maximum buffer size */

		mali_profiling_default_enable = MALI_TRUE; /* save this so user space can query this on their startup */
		if (_MALI_OSK_ERR_OK != _mali_profiling_start(&limit))
		{
			return _MALI_OSK_ERR_FAULT;
		}
	}

	return _MALI_OSK_ERR_OK;
}

void _mali_profiling_term(void)
{
	prof_state = MALI_PROFILING_STATE_UNINITIALIZED;

	/* wait for all elements to be completely inserted into array */
	while (_mali_osk_atomic_read(&profile_insert_index) != _mali_osk_atomic_read(&profile_entries_written))
	{
		/* do nothing */;
	}

	if (NULL != profile_entries)
	{
		_mali_osk_vfree(profile_entries);
		profile_entries = NULL;
	}

	if (NULL != lock)
	{
		_mali_osk_lock_term(lock);
		lock = NULL;
	}
}

inline _mali_osk_errcode_t _mali_profiling_start(u32 * limit)
{
	_mali_osk_errcode_t ret;

	mali_profiling_entry *new_profile_entries = _mali_osk_valloc(*limit * sizeof(mali_profiling_entry));

	if(NULL == new_profile_entries)
	{
		return _MALI_OSK_ERR_NOMEM;
	}

	_mali_osk_lock_wait(lock, _MALI_OSK_LOCKMODE_RW);

	if (prof_state != MALI_PROFILING_STATE_IDLE)
	{
		_mali_osk_lock_signal(lock, _MALI_OSK_LOCKMODE_RW);
		_mali_osk_vfree(new_profile_entries);
		return _MALI_OSK_ERR_INVALID_ARGS; /* invalid to call this function in this state */
	}

	if (*limit > MALI_PROFILING_MAX_BUFFER_ENTRIES)
	{
		*limit = MALI_PROFILING_MAX_BUFFER_ENTRIES;
	}

	profile_entries = new_profile_entries;
	profile_entry_count = *limit;

	ret = _mali_timestamp_reset();

	if (ret == _MALI_OSK_ERR_OK)
	{
		prof_state = MALI_PROFILING_STATE_RUNNING;
	}
	else
	{
		_mali_osk_vfree(profile_entries);
		profile_entries = NULL;
	}

	_mali_osk_lock_signal(lock, _MALI_OSK_LOCKMODE_RW);
	return ret;
}

inline void _mali_profiling_add_counter(u32 event_id, u32 data0)
{
#if MALI_TRACEPOINTS_ENABLED
	_mali_osk_profiling_add_counter(event_id, data0);	
#endif
}

inline _mali_osk_errcode_t _mali_profiling_add_event(u32 event_id, u32 data0, u32 data1, u32 data2, u32 data3, u32 data4)
{
	u32 cur_index = _mali_osk_atomic_inc_return(&profile_insert_index) - 1;

#if MALI_TRACEPOINTS_ENABLED
	_mali_osk_profiling_add_event(event_id, data0);
#endif

	if (prof_state != MALI_PROFILING_STATE_RUNNING || cur_index >= profile_entry_count)
	{
		/*
		 * Not in recording mode, or buffer is full
		 * Decrement index again, and early out
		 */
		_mali_osk_atomic_dec(&profile_insert_index);
		return _MALI_OSK_ERR_FAULT;
	}

	profile_entries[cur_index].timestamp = _mali_timestamp_get();
	profile_entries[cur_index].event_id = event_id;
	profile_entries[cur_index].data[0] = data0;
	profile_entries[cur_index].data[1] = data1;
	profile_entries[cur_index].data[2] = data2;
	profile_entries[cur_index].data[3] = data3;
	profile_entries[cur_index].data[4] = data4;

	_mali_osk_atomic_inc(&profile_entries_written);

	return _MALI_OSK_ERR_OK;
}

#if MALI_TRACEPOINTS_ENABLED
/*
 * The following code uses a bunch of magic numbers taken from the userspace
 * side of the DDK; they are re-used here verbatim. They are taken from the 
 * file mali_instrumented_counter_types.h.
 */
#define MALI_GLES_COUNTER_OFFSET   1000
#define MALI_VG_COUNTER_OFFSET     2000
#define MALI_EGL_COUNTER_OFFSET    3000
#define MALI_SHARED_COUNTER_OFFSET 4000

/* These offsets are derived from the gator driver; see gator_events_mali.c. */
#define GATOR_EGL_COUNTER_OFFSET    17
#define GATOR_GLES_COUNTER_OFFSET   18

_mali_osk_errcode_t _mali_ukk_transfer_sw_counters(_mali_uk_sw_counters_s *args)
{
	/* Convert the DDK counter ID to what gator expects */
	unsigned int gator_counter_value = 0;

	_mali_osk_lock_wait(lock, _MALI_OSK_LOCKMODE_RW);

	if (args->id >= MALI_EGL_COUNTER_OFFSET && args->id <= MALI_SHARED_COUNTER_OFFSET)
	{
		gator_counter_value = (args->id - MALI_EGL_COUNTER_OFFSET) + GATOR_EGL_COUNTER_OFFSET;
	}
	else if (args->id >= MALI_GLES_COUNTER_OFFSET && args->id <= MALI_VG_COUNTER_OFFSET)
	{
		gator_counter_value = (args->id - MALI_GLES_COUNTER_OFFSET) + GATOR_GLES_COUNTER_OFFSET;
	}
	else 
	{
		/* Pass it straight through; gator will ignore it anyway. */
		gator_counter_value = args->id;
	}

	trace_mali_sw_counter(gator_counter_value, args->value);

	_mali_osk_lock_signal(lock, _MALI_OSK_LOCKMODE_RW);

	return _MALI_OSK_ERR_OK;
}
#endif

inline _mali_osk_errcode_t _mali_profiling_stop(u32 * count)
{
	_mali_osk_lock_wait(lock, _MALI_OSK_LOCKMODE_RW);

	if (prof_state != MALI_PROFILING_STATE_RUNNING)
	{
		_mali_osk_lock_signal(lock, _MALI_OSK_LOCKMODE_RW);
		return _MALI_OSK_ERR_INVALID_ARGS; /* invalid to call this function in this state */
	}

	/* go into return state (user to retreive events), no more events will be added after this */
	prof_state = MALI_PROFILING_STATE_RETURN;

	_mali_osk_lock_signal(lock, _MALI_OSK_LOCKMODE_RW);

	/* wait for all elements to be completely inserted into array */
	while (_mali_osk_atomic_read(&profile_insert_index) != _mali_osk_atomic_read(&profile_entries_written))
	{
		/* do nothing */;
	}

	*count = _mali_osk_atomic_read(&profile_insert_index);

	return _MALI_OSK_ERR_OK;
}

inline u32 _mali_profiling_get_count(void)
{
	u32 retval = 0;

	_mali_osk_lock_wait(lock, _MALI_OSK_LOCKMODE_RW);
	if (prof_state == MALI_PROFILING_STATE_RETURN)
	{
		retval = _mali_osk_atomic_read(&profile_entries_written);
	}
	_mali_osk_lock_signal(lock, _MALI_OSK_LOCKMODE_RW);

	return retval;
}

inline _mali_osk_errcode_t _mali_profiling_get_event(u32 index, u64* timestamp, u32* event_id, u32 data[5])
{
	_mali_osk_lock_wait(lock, _MALI_OSK_LOCKMODE_RW);

	if (prof_state != MALI_PROFILING_STATE_RETURN)
	{
		_mali_osk_lock_signal(lock, _MALI_OSK_LOCKMODE_RW);
		return _MALI_OSK_ERR_INVALID_ARGS; /* invalid to call this function in this state */
	}

	if (index >= _mali_osk_atomic_read(&profile_entries_written))
	{
		_mali_osk_lock_signal(lock, _MALI_OSK_LOCKMODE_RW);
		return _MALI_OSK_ERR_FAULT;
	}

	*timestamp = profile_entries[index].timestamp;
	*event_id = profile_entries[index].event_id;
	data[0] = profile_entries[index].data[0];
	data[1] = profile_entries[index].data[1];
	data[2] = profile_entries[index].data[2];
	data[3] = profile_entries[index].data[3];
	data[4] = profile_entries[index].data[4];

	_mali_osk_lock_signal(lock, _MALI_OSK_LOCKMODE_RW);
	return _MALI_OSK_ERR_OK;
}

inline _mali_osk_errcode_t _mali_profiling_clear(void)
{
	_mali_osk_lock_wait(lock, _MALI_OSK_LOCKMODE_RW);

	if (prof_state != MALI_PROFILING_STATE_RETURN)
	{
		_mali_osk_lock_signal(lock, _MALI_OSK_LOCKMODE_RW);
		return _MALI_OSK_ERR_INVALID_ARGS; /* invalid to call this function in this state */
	}

	prof_state = MALI_PROFILING_STATE_IDLE;
	profile_entry_count = 0;
	_mali_osk_atomic_init(&profile_insert_index, 0);
	_mali_osk_atomic_init(&profile_entries_written, 0);
	if (NULL != profile_entries)
	{
		_mali_osk_vfree(profile_entries);
		profile_entries = NULL;
	}

	_mali_osk_lock_signal(lock, _MALI_OSK_LOCKMODE_RW);
	return _MALI_OSK_ERR_OK;
}

mali_bool _mali_profiling_is_recording(void)
{
	return prof_state == MALI_PROFILING_STATE_RUNNING ? MALI_TRUE : MALI_FALSE;
}

mali_bool _mali_profiling_have_recording(void)
{
	return prof_state == MALI_PROFILING_STATE_RETURN ? MALI_TRUE : MALI_FALSE;
}

void _mali_profiling_set_default_enable_state(mali_bool enable)
{
	mali_profiling_default_enable = enable;
}

mali_bool _mali_profiling_get_default_enable_state(void)
{
	return mali_profiling_default_enable;
}

_mali_osk_errcode_t _mali_ukk_profiling_start(_mali_uk_profiling_start_s *args)
{
	return _mali_profiling_start(&args->limit);
}

_mali_osk_errcode_t _mali_ukk_profiling_add_event(_mali_uk_profiling_add_event_s *args)
{
	/* Always add process and thread identificator in the first two data elements for events from user space */
	return _mali_profiling_add_event(args->event_id, _mali_osk_get_pid(), _mali_osk_get_tid(), args->data[2], args->data[3], args->data[4]);
}

_mali_osk_errcode_t _mali_ukk_profiling_stop(_mali_uk_profiling_stop_s *args)
{
	return _mali_profiling_stop(&args->count);
}

_mali_osk_errcode_t _mali_ukk_profiling_get_event(_mali_uk_profiling_get_event_s *args)
{
	return _mali_profiling_get_event(args->index, &args->timestamp, &args->event_id, args->data);
}

_mali_osk_errcode_t _mali_ukk_profiling_clear(_mali_uk_profiling_clear_s *args)
{
	return _mali_profiling_clear();
}

_mali_osk_errcode_t _mali_ukk_profiling_get_config(_mali_uk_profiling_get_config_s *args)
{
	args->enable_events = mali_profiling_default_enable;
	return _MALI_OSK_ERR_OK;
}