summaryrefslogtreecommitdiff
path: root/drivers/gpu/arm/mali/linux/mali_kernel_devfreq.c
blob: 1213e7bc6946560d370eaa5dd351c780a9e0d4ac (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
/*
 * Copyright (C) 2010-2011 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 <linux/devfreq.h>
#include <linux/platform_device.h>
#include "mali_kernel_devfreq.h"
#include "mali_osk.h"
#include "mali_platform.h"
#include "mali_linux_pm.h"

#define MALI_GPU_UTILIZATION_PERIOD	500

static _mali_osk_lock_t *time_data_lock;

static _mali_osk_atomic_t num_running_cores;

static u64 period_start_time = 0;
static u64 work_start_time = 0;
static u64 accumulated_work_time = 0;
static mali_bool timer_running = MALI_FALSE;

static struct devfreq *mali_devfreq;

static int mali_get_dev_status(struct device *dev,
				struct devfreq_dev_status *stat)
{
	u64 time_now;
	u64 time_period;

	_mali_osk_lock_wait(time_data_lock, _MALI_OSK_LOCKMODE_RW);

	if (accumulated_work_time == 0 && work_start_time == 0)
	{
		/* No work done for this period, report zero usage */
		stat->total_time = 0;
		stat->busy_time = 0;
		stat->current_frequency = get_mali_platform_cur_freq();

		_mali_osk_lock_signal(time_data_lock, _MALI_OSK_LOCKMODE_RW);

		return 0;
	}

	time_now = _mali_osk_time_get_ns();
	time_period = time_now - period_start_time;

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

	stat->total_time = time_period;
	stat->busy_time = accumulated_work_time;
	stat->current_frequency = get_mali_platform_cur_freq();

	accumulated_work_time = 0;
	/* start a new period */
	period_start_time = time_now;
	_mali_osk_lock_signal(time_data_lock, _MALI_OSK_LOCKMODE_RW);

	return 0;
}

static int mali_set_target_freq(struct device *dev,
				unsigned long *freq,
				u32 flags)
{
	mali_gpu_utilization_handler(*freq);
	return 0;
}

static int mali_get_cur_freq(struct device *dev, unsigned long *freq)
{
	*freq = get_mali_platform_cur_freq();
	return 0;
}

static struct devfreq_dev_profile mali_devfreq_profile = {
	.polling_ms = MALI_GPU_UTILIZATION_PERIOD,
	.initial_freq = 0,
	.target = mali_set_target_freq,
	.get_dev_status = mali_get_dev_status,
	.get_cur_freq = mali_get_cur_freq,
};

_mali_osk_errcode_t mali_utilization_init(void)
{
	/* Register mali devfreq with ondemand governor */
	mali_devfreq = devfreq_add_device(&mali_gpu_device.dev,
					&mali_devfreq_profile,
					&devfreq_simple_ondemand,
					NULL);
	if (NULL == mali_devfreq)
	{
		return _MALI_OSK_ERR_FAULT;
	}

	time_data_lock = _mali_osk_lock_init( 0, 0, 0 );
	if (NULL == time_data_lock)
	{
		return _MALI_OSK_ERR_FAULT;
	}

	_mali_osk_atomic_init(&num_running_cores, 0);

	return _MALI_OSK_ERR_OK;
}

void mali_utilization_suspend(void)
{
	if (timer_running == MALI_TRUE)
	{
		devfreq_suspend_device(mali_devfreq);
		_mali_osk_lock_wait(time_data_lock, _MALI_OSK_LOCKMODE_RW);
		timer_running = MALI_FALSE;
		work_start_time = 0;
		period_start_time = 0;
		accumulated_work_time = 0;
		_mali_osk_lock_signal(time_data_lock, _MALI_OSK_LOCKMODE_RW);
	}
}

void mali_utilization_resume(void)
{
	devfreq_resume_device(mali_devfreq);
}

void mali_utilization_term(void)
{
	devfreq_remove_device(mali_devfreq);
	mali_devfreq = NULL;

	timer_running = MALI_FALSE;

	_mali_osk_atomic_term(&num_running_cores);

	_mali_osk_lock_term(time_data_lock);
}

void mali_utilization_core_start(void)
{
	if (_mali_osk_atomic_inc_return(&num_running_cores) == 1)
	{
		/*
		 * We went from zero cores working, to one core working,
		 * we now consider the entire GPU for being busy
		 */
		_mali_osk_lock_wait(time_data_lock, _MALI_OSK_LOCKMODE_RW);

		work_start_time = _mali_osk_time_get_ns();

		if (timer_running != MALI_TRUE)
		{
			timer_running = MALI_TRUE;
			period_start_time = work_start_time;
		}

		_mali_osk_lock_signal(time_data_lock, _MALI_OSK_LOCKMODE_RW);
	}
}

void mali_utilization_core_end(void)
{
	if (_mali_osk_atomic_dec_return(&num_running_cores) == 0)
	{
		/*
		 * No more cores are working, so accumulate the time we was busy.
		 */
		u64 time_now;

		_mali_osk_lock_wait(time_data_lock, _MALI_OSK_LOCKMODE_RW);

		time_now = _mali_osk_time_get_ns();
		accumulated_work_time += (time_now - work_start_time);
		work_start_time = 0;

		_mali_osk_lock_signal(time_data_lock, _MALI_OSK_LOCKMODE_RW);
	}
}