aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/thermal/cpu_cooling.c
blob: 408aab0a55ffc5032ce17b2bff91dc60dbb10f74 (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
/*
 *  linux/drivers/thermal/cpu_cooling.c
 *
 *  Copyright (C) 2011	Samsung Electronics Co., Ltd(http://www.samsung.com)
 *  Copyright (C) 2011  Amit Daniel <amit.kachhap at linaro.org>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/thermal.h>
#include <linux/platform_device.h>
#include <linux/cpufreq.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/cpu.h>
#include <linux/cpu_cooling.h>

#ifdef CONFIG_CPU_FREQ
struct cpufreq_cooling_device {
	struct thermal_cooling_device *cool_dev;
	struct freq_pctg_table *tab_ptr;
	unsigned int tab_size;
	unsigned int cpufreq_state;
	const struct cpumask *allowed_cpus;
};

static struct cpufreq_cooling_device *cpufreq_device;

/*Below codes defines functions to be used for cpufreq as cooling device*/
static bool is_cpufreq_valid(int cpu)
{
	struct cpufreq_policy policy;
	if (!cpufreq_get_policy(&policy, cpu))
		return true;
	return false;
}

static int cpufreq_apply_cooling(int cooling_state)
{
	int cpuid, this_cpu = smp_processor_id();

	if (!is_cpufreq_valid(this_cpu))
		return 0;

	if (cooling_state > cpufreq_device->tab_size)
		return -EINVAL;

	/*Check if last cooling level is same as current cooling level*/
	if (cpufreq_device->cpufreq_state == cooling_state)
		return 0;

	cpufreq_device->cpufreq_state = cooling_state;

	for_each_cpu(cpuid, cpufreq_device->allowed_cpus) {
		if (is_cpufreq_valid(cpuid))
			cpufreq_update_policy(cpuid);
	}

	return 0;
}

static int thermal_cpufreq_notifier(struct notifier_block *nb,
					unsigned long event, void *data)
{
	struct cpufreq_policy *policy = data;
	struct freq_pctg_table *th_table;
	unsigned long max_freq = 0;
	unsigned int cpu = policy->cpu, th_pctg = 0, level;

	if (event != CPUFREQ_ADJUST)
		return 0;

	level = cpufreq_device->cpufreq_state;

	if (level > 0) {
		th_table =
			&(cpufreq_device->tab_ptr[level - 1]);
		th_pctg = th_table->freq_clip_pctg[cpu];
	}

	max_freq =
		(policy->cpuinfo.max_freq * (100 - th_pctg)) / 100;

	cpufreq_verify_within_limits(policy, 0, max_freq);

	return 0;
}

/*
 * cpufreq cooling device callback functions
 */
static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
				 unsigned long *state)
{
	*state = cpufreq_device->tab_size;
	return 0;
}

static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
				 unsigned long *state)
{
	*state = cpufreq_device->cpufreq_state;
	return 0;
}

/*This cooling may be as PASSIVE/STATE_ACTIVE type*/
static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
				 unsigned long state)
{
	cpufreq_apply_cooling(state);
	return 0;
}

/* bind cpufreq callbacks to cpufreq cooling device */
static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
	.get_max_state = cpufreq_get_max_state,
	.get_cur_state = cpufreq_get_cur_state,
	.set_cur_state = cpufreq_set_cur_state,
};

static struct notifier_block thermal_cpufreq_notifier_block = {
	.notifier_call = thermal_cpufreq_notifier,
};

struct thermal_cooling_device *cpufreq_cooling_register(
	struct freq_pctg_table *tab_ptr, unsigned int tab_size,
	const struct cpumask *mask_val)
{
	struct thermal_cooling_device *cool_dev;

	if (tab_ptr == NULL || tab_size == 0)
		return ERR_PTR(-EINVAL);

	cpufreq_device =
		kzalloc(sizeof(struct cpufreq_cooling_device), GFP_KERNEL);

	if (!cpufreq_device)
		return ERR_PTR(-ENOMEM);

	cool_dev = thermal_cooling_device_register("thermal-cpufreq", NULL,
						&cpufreq_cooling_ops);
	if (!cool_dev) {
		kfree(cpufreq_device);
		return ERR_PTR(-EINVAL);
	}

	cpufreq_device->tab_ptr = tab_ptr;
	cpufreq_device->tab_size = tab_size;
	cpufreq_device->cool_dev = cool_dev;
	cpufreq_device->allowed_cpus = mask_val;

	cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
						CPUFREQ_POLICY_NOTIFIER);
	return cool_dev;
}
EXPORT_SYMBOL(cpufreq_cooling_register);

void cpufreq_cooling_unregister(void)
{
	cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
						CPUFREQ_POLICY_NOTIFIER);
	thermal_cooling_device_unregister(cpufreq_device->cool_dev);
	kfree(cpufreq_device);
}
EXPORT_SYMBOL(cpufreq_cooling_unregister);
#else /*!CONFIG_CPU_FREQ*/
struct thermal_cooling_device *cpufreq_cooling_register(
	struct freq_pctg_table *tab_ptr, unsigned int tab_size)
{
	return NULL;
}
EXPORT_SYMBOL(cpufreq_cooling_register);
void cpufreq_cooling_unregister(void)
{
	return;
}
EXPORT_SYMBOL(cpufreq_cooling_unregister);
#endif /*CONFIG_CPU_FREQ*/

#ifdef CONFIG_HOTPLUG_CPU

struct hotplug_cooling_device {
	struct thermal_cooling_device *cool_dev;
	unsigned int hotplug_state;
	const struct cpumask *allowed_cpus;
};
static struct hotplug_cooling_device *hotplug_device;

/*
 * cpu hotplug cooling device callback functions
 */
static int cpuhotplug_get_max_state(struct thermal_cooling_device *cdev,
				 unsigned long *state)
{
	*state = 1;
	return 0;
}

static int cpuhotplug_get_cur_state(struct thermal_cooling_device *cdev,
				 unsigned long *state)
{
	/*This cooling device may be of type ACTIVE, so state field
	can be 0 or 1*/
	*state = hotplug_device->hotplug_state;
	return 0;
}

/*This cooling may be as PASSIVE/STATE_ACTIVE type*/
static int cpuhotplug_set_cur_state(struct thermal_cooling_device *cdev,
				 unsigned long state)
{
	int cpuid, this_cpu = smp_processor_id();

	if (hotplug_device->hotplug_state == state)
		return 0;

	/*This cooling device may be of type ACTIVE, so state field
	can be 0 or 1*/
	if (state == 1) {
		for_each_cpu(cpuid, hotplug_device->allowed_cpus) {
			if (cpu_online(cpuid) && (cpuid != this_cpu))
				cpu_down(cpuid);
		}
	} else if (state == 0) {
		for_each_cpu(cpuid, hotplug_device->allowed_cpus) {
			if (!cpu_online(cpuid) && (cpuid != this_cpu))
				cpu_up(cpuid);
		}
	} else
		return -EINVAL;

	hotplug_device->hotplug_state = state;

	return 0;
}
/* bind hotplug callbacks to cpu hotplug cooling device */
static struct thermal_cooling_device_ops cpuhotplug_cooling_ops = {
	.get_max_state = cpuhotplug_get_max_state,
	.get_cur_state = cpuhotplug_get_cur_state,
	.set_cur_state = cpuhotplug_set_cur_state,
};

struct thermal_cooling_device *cpuhotplug_cooling_register(
	const struct cpumask *mask_val)
{
	struct thermal_cooling_device *cool_dev;

	hotplug_device =
		kzalloc(sizeof(struct hotplug_cooling_device), GFP_KERNEL);

	if (!hotplug_device)
		return ERR_PTR(-ENOMEM);

	cool_dev = thermal_cooling_device_register("thermal-cpuhotplug", NULL,
						&cpuhotplug_cooling_ops);
	if (!cool_dev) {
		kfree(hotplug_device);
		return ERR_PTR(-EINVAL);
	}

	hotplug_device->cool_dev = cool_dev;
	hotplug_device->hotplug_state = 0;
	hotplug_device->allowed_cpus = mask_val;

	return cool_dev;
}
EXPORT_SYMBOL(cpuhotplug_cooling_register);

void cpuhotplug_cooling_unregister(void)
{
	thermal_cooling_device_unregister(hotplug_device->cool_dev);
	kfree(hotplug_device);
}
EXPORT_SYMBOL(cpuhotplug_cooling_unregister);
#else /*!CONFIG_HOTPLUG_CPU*/
struct thermal_cooling_device *cpuhotplug_cooling_register(
	const struct cpumask *mask_val)
{
	return NULL;
}
EXPORT_SYMBOL(cpuhotplug_cooling_register);
void cpuhotplug_cooling_unregister(void)
{
	return;
}
EXPORT_SYMBOL(cpuhotplug_cooling_unregister);
#endif /*CONFIG_HOTPLUG_CPU*/