summaryrefslogtreecommitdiff
path: root/drivers/gpu/arm/utgard/common/mali_kernel_utilization.c
blob: 63b94174224963bc8ea85f96651a2fb188276ba0 (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
/*
 * Copyright (C) 2010-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_kernel_utilization.h"
#include "mali_osk.h"
#include "mali_osk_mali.h"
#include "mali_kernel_common.h"
#include "mali_session.h"
#include "mali_scheduler.h"

#include "mali_executor.h"
#include "mali_dvfs_policy.h"
#include "mali_control_timer.h"

/* Thresholds for GP bound detection. */
#define MALI_GP_BOUND_GP_UTILIZATION_THRESHOLD 240
#define MALI_GP_BOUND_PP_UTILIZATION_THRESHOLD 250

static _mali_osk_spinlock_irq_t *utilization_data_lock;

static u32 num_running_gp_cores = 0;
static u32 num_running_pp_cores = 0;

static u64 work_start_time_gpu = 0;
static u64 work_start_time_gp = 0;
static u64 work_start_time_pp = 0;
static u64 accumulated_work_time_gpu = 0;
static u64 accumulated_work_time_gp = 0;
static u64 accumulated_work_time_pp = 0;

static u32 last_utilization_gpu = 0 ;
static u32 last_utilization_gp = 0 ;
static u32 last_utilization_pp = 0 ;

void (*mali_utilization_callback)(struct mali_gpu_utilization_data *data) = NULL;

/* Define the first timer control timer timeout in milliseconds */
static u32 mali_control_first_timeout = 100;
static struct mali_gpu_utilization_data mali_util_data = {0, };

struct mali_gpu_utilization_data *mali_utilization_calculate(u64 *start_time, u64 *time_period, mali_bool *need_add_timer)
{
	u64 time_now;
	u32 leading_zeroes;
	u32 shift_val;
	u32 work_normalized_gpu;
	u32 work_normalized_gp;
	u32 work_normalized_pp;
	u32 period_normalized;
	u32 utilization_gpu;
	u32 utilization_gp;
	u32 utilization_pp;

	mali_utilization_data_lock();

	time_now = _mali_osk_time_get_ns();

	*time_period = time_now - *start_time;

	if (accumulated_work_time_gpu == 0 && work_start_time_gpu == 0) {
		mali_control_timer_pause();
		/*
		 * No work done for this period
		 * - No need to reschedule timer
		 * - Report zero usage
		 */
		last_utilization_gpu = 0;
		last_utilization_gp = 0;
		last_utilization_pp = 0;

		mali_util_data.utilization_gpu = last_utilization_gpu;
		mali_util_data.utilization_gp = last_utilization_gp;
		mali_util_data.utilization_pp = last_utilization_pp;

		mali_utilization_data_unlock();

		*need_add_timer = MALI_FALSE;

		mali_executor_hint_disable(MALI_EXECUTOR_HINT_GP_BOUND);

		MALI_DEBUG_PRINT(4, ("last_utilization_gpu = %d \n", last_utilization_gpu));
		MALI_DEBUG_PRINT(4, ("last_utilization_gp = %d \n", last_utilization_gp));
		MALI_DEBUG_PRINT(4, ("last_utilization_pp = %d \n", last_utilization_pp));

		return &mali_util_data;
	}

	/* If we are currently busy, update working period up to now */
	if (work_start_time_gpu != 0) {
		accumulated_work_time_gpu += (time_now - work_start_time_gpu);
		work_start_time_gpu = time_now;

		/* GP and/or PP will also be busy if the GPU is busy at this point */

		if (work_start_time_gp != 0) {
			accumulated_work_time_gp += (time_now - work_start_time_gp);
			work_start_time_gp = time_now;
		}

		if (work_start_time_pp != 0) {
			accumulated_work_time_pp += (time_now - work_start_time_pp);
			work_start_time_pp = time_now;
		}
	}

	/*
	 * We have two 64-bit values, a dividend and a divisor.
	 * To avoid dependencies to a 64-bit divider, we shift down the two values
	 * equally first.
	 * We shift the dividend up and possibly the divisor down, making the result X in 256.
	 */

	/* Shift the 64-bit values down so they fit inside a 32-bit integer */
	leading_zeroes = _mali_osk_clz((u32)(*time_period >> 32));
	shift_val = 32 - leading_zeroes;
	work_normalized_gpu = (u32)(accumulated_work_time_gpu >> shift_val);
	work_normalized_gp = (u32)(accumulated_work_time_gp >> shift_val);
	work_normalized_pp = (u32)(accumulated_work_time_pp >> shift_val);
	period_normalized = (u32)(*time_period >> shift_val);

	/*
	 * Now, we should report the usage in parts of 256
	 * this means we must shift up the dividend or down the divisor by 8
	 * (we could do a combination, but we just use one for simplicity,
	 * but the end result should be good enough anyway)
	 */
	if (period_normalized > 0x00FFFFFF) {
		/* The divisor is so big that it is safe to shift it down */
		period_normalized >>= 8;
	} else {
		/*
		 * The divisor is so small that we can shift up the dividend, without loosing any data.
		 * (dividend is always smaller than the divisor)
		 */
		work_normalized_gpu <<= 8;
		work_normalized_gp <<= 8;
		work_normalized_pp <<= 8;
	}

	utilization_gpu = work_normalized_gpu / period_normalized;
	utilization_gp = work_normalized_gp / period_normalized;
	utilization_pp = work_normalized_pp / period_normalized;

	last_utilization_gpu = utilization_gpu;
	last_utilization_gp = utilization_gp;
	last_utilization_pp = utilization_pp;

	if ((MALI_GP_BOUND_GP_UTILIZATION_THRESHOLD < last_utilization_gp) &&
	    (MALI_GP_BOUND_PP_UTILIZATION_THRESHOLD > last_utilization_pp)) {
		mali_executor_hint_enable(MALI_EXECUTOR_HINT_GP_BOUND);
	} else {
		mali_executor_hint_disable(MALI_EXECUTOR_HINT_GP_BOUND);
	}

	/* starting a new period */
	accumulated_work_time_gpu = 0;
	accumulated_work_time_gp = 0;
	accumulated_work_time_pp = 0;

	*start_time = time_now;

	mali_util_data.utilization_gp = last_utilization_gp;
	mali_util_data.utilization_gpu = last_utilization_gpu;
	mali_util_data.utilization_pp = last_utilization_pp;

	mali_utilization_data_unlock();

	*need_add_timer = MALI_TRUE;

	MALI_DEBUG_PRINT(4, ("last_utilization_gpu = %d \n", last_utilization_gpu));
	MALI_DEBUG_PRINT(4, ("last_utilization_gp = %d \n", last_utilization_gp));
	MALI_DEBUG_PRINT(4, ("last_utilization_pp = %d \n", last_utilization_pp));

	return &mali_util_data;
}

_mali_osk_errcode_t mali_utilization_init(void)
{
#if USING_GPU_UTILIZATION
	_mali_osk_device_data data;

	if (_MALI_OSK_ERR_OK == _mali_osk_device_data_get(&data)) {
		if (NULL != data.utilization_callback) {
			mali_utilization_callback = data.utilization_callback;
			MALI_DEBUG_PRINT(2, ("Mali GPU Utilization: Utilization handler installed \n"));
		}
	}
#endif /* defined(USING_GPU_UTILIZATION) */

	if (NULL == mali_utilization_callback) {
		MALI_DEBUG_PRINT(2, ("Mali GPU Utilization: No platform utilization handler installed\n"));
	}

	utilization_data_lock = _mali_osk_spinlock_irq_init(_MALI_OSK_LOCKFLAG_ORDERED, _MALI_OSK_LOCK_ORDER_UTILIZATION);
	if (NULL == utilization_data_lock) {
		return _MALI_OSK_ERR_FAULT;
	}

	num_running_gp_cores = 0;
	num_running_pp_cores = 0;

	return _MALI_OSK_ERR_OK;
}

void mali_utilization_term(void)
{
	if (NULL != utilization_data_lock) {
		_mali_osk_spinlock_irq_term(utilization_data_lock);
	}
}

void mali_utilization_gp_start(void)
{
	mali_utilization_data_lock();

	++num_running_gp_cores;
	if (1 == num_running_gp_cores) {
		u64 time_now = _mali_osk_time_get_ns();

		/* First GP core started, consider GP busy from now and onwards */
		work_start_time_gp = time_now;

		if (0 == num_running_pp_cores) {
			mali_bool is_resume = MALI_FALSE;
			/*
			 * There are no PP cores running, so this is also the point
			 * at which we consider the GPU to be busy as well.
			 */
			work_start_time_gpu = time_now;

			is_resume  = mali_control_timer_resume(time_now);

			mali_utilization_data_unlock();

			if (is_resume) {
				/* Do some policy in new period for performance consideration */
#if defined(CONFIG_MALI_DVFS)
				/* Clear session->number_of_window_jobs, prepare parameter for dvfs */
				mali_session_max_window_num();
				if (0 == last_utilization_gpu) {
					/*
					 * for mali_dev_pause is called in set clock,
					 * so each time we change clock, we will set clock to
					 * highest step even if under down clock case,
					 * it is not nessesary, so we only set the clock under
					 * last time utilization equal 0, we stop the timer then
					 * start the GPU again case
					 */
					mali_dvfs_policy_new_period();
				}
#endif
				/*
				 * First timeout using short interval for power consideration
				 * because we give full power in the new period, but if the
				 * job loading is light, finish in 10ms, the other time all keep
				 * in high freq it will wast time.
				 */
				mali_control_timer_add(mali_control_first_timeout);
			}
		} else {
			mali_utilization_data_unlock();
		}

	} else {
		/* Nothing to do */
		mali_utilization_data_unlock();
	}
}

void mali_utilization_pp_start(void)
{
	mali_utilization_data_lock();

	++num_running_pp_cores;
	if (1 == num_running_pp_cores) {
		u64 time_now = _mali_osk_time_get_ns();

		/* First PP core started, consider PP busy from now and onwards */
		work_start_time_pp = time_now;

		if (0 == num_running_gp_cores) {
			mali_bool is_resume = MALI_FALSE;
			/*
			 * There are no GP cores running, so this is also the point
			 * at which we consider the GPU to be busy as well.
			 */
			work_start_time_gpu = time_now;

			/* Start a new period if stoped */
			is_resume = mali_control_timer_resume(time_now);

			mali_utilization_data_unlock();

			if (is_resume) {
#if defined(CONFIG_MALI_DVFS)
				/* Clear session->number_of_window_jobs, prepare parameter for dvfs */
				mali_session_max_window_num();
				if (0 == last_utilization_gpu) {
					/*
					 * for mali_dev_pause is called in set clock,
					 * so each time we change clock, we will set clock to
					 * highest step even if under down clock case,
					 * it is not nessesary, so we only set the clock under
					 * last time utilization equal 0, we stop the timer then
					 * start the GPU again case
					 */
					mali_dvfs_policy_new_period();
				}
#endif

				/*
				 * First timeout using short interval for power consideration
				 * because we give full power in the new period, but if the
				 * job loading is light, finish in 10ms, the other time all keep
				 * in high freq it will wast time.
				 */
				mali_control_timer_add(mali_control_first_timeout);
			}
		} else {
			mali_utilization_data_unlock();
		}
	} else {
		/* Nothing to do */
		mali_utilization_data_unlock();
	}
}

void mali_utilization_gp_end(void)
{
	mali_utilization_data_lock();

	--num_running_gp_cores;
	if (0 == num_running_gp_cores) {
		u64 time_now = _mali_osk_time_get_ns();

		/* Last GP core ended, consider GP idle from now and onwards */
		accumulated_work_time_gp += (time_now - work_start_time_gp);
		work_start_time_gp = 0;

		if (0 == num_running_pp_cores) {
			/*
			 * There are no PP cores running, so this is also the point
			 * at which we consider the GPU to be idle as well.
			 */
			accumulated_work_time_gpu += (time_now - work_start_time_gpu);
			work_start_time_gpu = 0;
		}
	}

	mali_utilization_data_unlock();
}

void mali_utilization_pp_end(void)
{
	mali_utilization_data_lock();

	--num_running_pp_cores;
	if (0 == num_running_pp_cores) {
		u64 time_now = _mali_osk_time_get_ns();

		/* Last PP core ended, consider PP idle from now and onwards */
		accumulated_work_time_pp += (time_now - work_start_time_pp);
		work_start_time_pp = 0;

		if (0 == num_running_gp_cores) {
			/*
			 * There are no GP cores running, so this is also the point
			 * at which we consider the GPU to be idle as well.
			 */
			accumulated_work_time_gpu += (time_now - work_start_time_gpu);
			work_start_time_gpu = 0;
		}
	}

	mali_utilization_data_unlock();
}

mali_bool mali_utilization_enabled(void)
{
#if defined(CONFIG_MALI_DVFS)
	return mali_dvfs_policy_enabled();
#else
	return (NULL != mali_utilization_callback);
#endif /* defined(CONFIG_MALI_DVFS) */
}

void mali_utilization_platform_realize(struct mali_gpu_utilization_data *util_data)
{
	MALI_DEBUG_ASSERT_POINTER(mali_utilization_callback);

	mali_utilization_callback(util_data);
}

void mali_utilization_reset(void)
{
	accumulated_work_time_gpu = 0;
	accumulated_work_time_gp = 0;
	accumulated_work_time_pp = 0;

	last_utilization_gpu = 0;
	last_utilization_gp = 0;
	last_utilization_pp = 0;
}

void mali_utilization_data_lock(void)
{
	_mali_osk_spinlock_irq_lock(utilization_data_lock);
}

void mali_utilization_data_unlock(void)
{
	_mali_osk_spinlock_irq_unlock(utilization_data_lock);
}

void mali_utilization_data_assert_locked(void)
{
	MALI_DEBUG_ASSERT_LOCK_HELD(utilization_data_lock);
}

u32 _mali_ukk_utilization_gp_pp(void)
{
	return last_utilization_gpu;
}

u32 _mali_ukk_utilization_gp(void)
{
	return last_utilization_gp;
}

u32 _mali_ukk_utilization_pp(void)
{
	return last_utilization_pp;
}