cpu_cooling.c 16.4 KB
Newer Older
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
/*
 *  linux/drivers/thermal/cpu_cooling.c
 *
 *  Copyright (C) 2012	Samsung Electronics Co., Ltd(http://www.samsung.com)
 *  Copyright (C) 2012  Amit Daniel <amit.kachhap@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/module.h>
#include <linux/thermal.h>
#include <linux/cpufreq.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/cpu.h>
#include <linux/cpu_cooling.h>

/**
32
 * struct cpufreq_cooling_device - data for cooling device with cpufreq
33 34
 * @id: unique integer value corresponding to each cpufreq_cooling_device
 *	registered.
35 36
 * @cool_dev: thermal_cooling_device pointer to keep track of the
 *	registered cooling device.
37 38 39 40 41 42 43
 * @cpufreq_state: integer value representing the current state of cpufreq
 *	cooling	devices.
 * @cpufreq_val: integer value representing the absolute value of the clipped
 *	frequency.
 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
 *
 * This structure is required for keeping information of each
44
 * cpufreq_cooling_device registered. In order to prevent corruption of this a
45 46 47 48 49 50 51 52 53 54
 * mutex lock cooling_cpufreq_lock is used.
 */
struct cpufreq_cooling_device {
	int id;
	struct thermal_cooling_device *cool_dev;
	unsigned int cpufreq_state;
	unsigned int cpufreq_val;
	struct cpumask allowed_cpus;
};
static DEFINE_IDR(cpufreq_idr);
55
static DEFINE_MUTEX(cooling_cpufreq_lock);
56

57
static unsigned int cpufreq_dev_count;
58 59 60

/* notify_table passes value to the CPUFREQ_ADJUST callback function. */
#define NOTIFY_INVALID NULL
61
static struct cpufreq_cooling_device *notify_device;
62 63 64 65 66

/**
 * get_idr - function to get a unique id.
 * @idr: struct idr * handle used to create a id.
 * @id: int * value generated by this function.
67 68 69 70 71
 *
 * This function will populate @id with an unique
 * id, using the idr API.
 *
 * Return: 0 on success, an error code on failure.
72 73 74
 */
static int get_idr(struct idr *idr, int *id)
{
T
Tejun Heo 已提交
75
	int ret;
76 77

	mutex_lock(&cooling_cpufreq_lock);
T
Tejun Heo 已提交
78
	ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
79
	mutex_unlock(&cooling_cpufreq_lock);
T
Tejun Heo 已提交
80 81 82
	if (unlikely(ret < 0))
		return ret;
	*id = ret;
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	return 0;
}

/**
 * release_idr - function to free the unique id.
 * @idr: struct idr * handle used for creating the id.
 * @id: int value representing the unique id.
 */
static void release_idr(struct idr *idr, int id)
{
	mutex_lock(&cooling_cpufreq_lock);
	idr_remove(idr, id);
	mutex_unlock(&cooling_cpufreq_lock);
}

/* Below code defines functions to be used for cpufreq as cooling device */

/**
102
 * is_cpufreq_valid - function to check frequency transitioning capability.
103
 * @cpu: cpu for which check is needed.
104 105 106 107 108 109
 *
 * This function will check the current state of the system if
 * it is capable of changing the frequency for a given @cpu.
 *
 * Return: 0 if the system is not currently capable of changing
 * the frequency of given cpu. !0 in case the frequency is changeable.
110 111 112 113
 */
static int is_cpufreq_valid(int cpu)
{
	struct cpufreq_policy policy;
114

115 116 117
	return !cpufreq_get_policy(&policy, cpu);
}

118 119 120 121 122 123
enum cpufreq_cooling_property {
	GET_LEVEL,
	GET_FREQ,
	GET_MAXL,
};

124 125 126 127 128 129 130 131
/**
 * get_property - fetch a property of interest for a give cpu.
 * @cpu: cpu for which the property is required
 * @input: query parameter
 * @output: query return
 * @property: type of query (frequency, level, max level)
 *
 * This is the common function to
132 133 134 135 136 137 138 139
 * 1. get maximum cpu cooling states
 * 2. translate frequency to cooling state
 * 3. translate cooling state to frequency
 * Note that the code may be not in good shape
 * but it is written in this way in order to:
 * a) reduce duplicate code as most of the code can be shared.
 * b) make sure the logic is consistent when translating between
 *    cooling states and frequencies.
140 141 142
 *
 * Return: 0 on success, -EINVAL when invalid parameters are passed.
 */
143
static int get_property(unsigned int cpu, unsigned long input,
144 145
			unsigned int *output,
			enum cpufreq_cooling_property property)
146
{
147
	int i;
148
	unsigned long max_level = 0, level = 0;
149 150
	unsigned int freq = CPUFREQ_ENTRY_INVALID;
	int descend = -1;
151
	struct cpufreq_frequency_table *pos, *table =
152
					cpufreq_frequency_get_table(cpu);
153

154 155 156
	if (!output)
		return -EINVAL;

157
	if (!table)
158
		return -EINVAL;
159

160
	cpufreq_for_each_valid_entry(pos, table) {
161
		/* ignore duplicate entry */
162
		if (freq == pos->frequency)
163 164 165
			continue;

		/* get the frequency order */
166
		if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
167
			descend = freq > pos->frequency;
168

169
		freq = pos->frequency;
170
		max_level++;
171
	}
172 173 174 175 176

	/* No valid cpu frequency entry */
	if (max_level == 0)
		return -EINVAL;

177 178
	/* max_level is an index, not a counter */
	max_level--;
179

180 181 182 183 184
	/* get max level */
	if (property == GET_MAXL) {
		*output = (unsigned int)max_level;
		return 0;
	}
185

186
	if (property == GET_FREQ)
187
		level = descend ? input : (max_level - input);
188

189 190
	i = 0;
	cpufreq_for_each_valid_entry(pos, table) {
191
		/* ignore duplicate entry */
192
		if (freq == pos->frequency)
193 194 195
			continue;

		/* now we have a valid frequency entry */
196
		freq = pos->frequency;
197 198 199

		if (property == GET_LEVEL && (unsigned int)input == freq) {
			/* get level by frequency */
200
			*output = descend ? i : (max_level - i);
201 202
			return 0;
		}
203
		if (property == GET_FREQ && level == i) {
204 205 206 207
			/* get frequency by level */
			*output = freq;
			return 0;
		}
208
		i++;
209
	}
210

211 212 213
	return -EINVAL;
}

214 215 216 217 218 219 220 221 222 223 224
/**
 * cpufreq_cooling_get_level - for a give cpu, return the cooling level.
 * @cpu: cpu for which the level is required
 * @freq: the frequency of interest
 *
 * This function will match the cooling level corresponding to the
 * requested @freq and return it.
 *
 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
 * otherwise.
 */
225 226 227 228 229 230
unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
{
	unsigned int val;

	if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
		return THERMAL_CSTATE_INVALID;
231

232 233
	return (unsigned long)val;
}
234
EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
235

236 237 238
/**
 * get_cpu_frequency - get the absolute value of frequency from level.
 * @cpu: cpu for which frequency is fetched.
239 240 241 242 243
 * @level: cooling level
 *
 * This function matches cooling level with frequency. Based on a cooling level
 * of frequency, equals cooling state of cpu cooling device, it will return
 * the corresponding frequency.
244
 *	e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
245 246
 *
 * Return: 0 on error, the corresponding frequency otherwise.
247 248 249 250 251 252 253 254 255
 */
static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
{
	int ret = 0;
	unsigned int freq;

	ret = get_property(cpu, level, &freq, GET_FREQ);
	if (ret)
		return 0;
256

257
	return freq;
258 259 260 261 262 263 264
}

/**
 * cpufreq_apply_cooling - function to apply frequency clipping.
 * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
 *	clipping data.
 * @cooling_state: value of the cooling state.
265 266 267 268 269 270
 *
 * Function used to make sure the cpufreq layer is aware of current thermal
 * limits. The limits are applied by updating the cpufreq policy.
 *
 * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
 * cooling state).
271 272
 */
static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
273
				 unsigned long cooling_state)
274 275
{
	unsigned int cpuid, clip_freq;
276 277
	struct cpumask *mask = &cpufreq_device->allowed_cpus;
	unsigned int cpu = cpumask_any(mask);
278 279 280 281 282 283 284 285 286 287 288 289 290 291


	/* Check if the old cooling action is same as new cooling action */
	if (cpufreq_device->cpufreq_state == cooling_state)
		return 0;

	clip_freq = get_cpu_frequency(cpu, cooling_state);
	if (!clip_freq)
		return -EINVAL;

	cpufreq_device->cpufreq_state = cooling_state;
	cpufreq_device->cpufreq_val = clip_freq;
	notify_device = cpufreq_device;

292
	for_each_cpu(cpuid, mask) {
293 294 295 296 297 298 299 300 301 302 303 304 305 306
		if (is_cpufreq_valid(cpuid))
			cpufreq_update_policy(cpuid);
	}

	notify_device = NOTIFY_INVALID;

	return 0;
}

/**
 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
 * @nb:	struct notifier_block * with callback info.
 * @event: value showing cpufreq event for which this function invoked.
 * @data: callback-specific data
307
 *
308
 * Callback to hijack the notification on cpufreq policy transition.
309 310 311 312
 * Every time there is a change in policy, we will intercept and
 * update the cpufreq policy with thermal constraints.
 *
 * Return: 0 (success)
313 314
 */
static int cpufreq_thermal_notifier(struct notifier_block *nb,
315
				    unsigned long event, void *data)
316 317 318 319 320 321 322 323 324
{
	struct cpufreq_policy *policy = data;
	unsigned long max_freq = 0;

	if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
		return 0;

	if (cpumask_test_cpu(policy->cpu, &notify_device->allowed_cpus))
		max_freq = notify_device->cpufreq_val;
325 326
	else
		return 0;
327

328
	/* Never exceed user_policy.max */
329 330 331 332 333 334 335 336 337
	if (max_freq > policy->user_policy.max)
		max_freq = policy->user_policy.max;

	if (policy->max != max_freq)
		cpufreq_verify_within_limits(policy, 0, max_freq);

	return 0;
}

338
/* cpufreq cooling device callback functions are defined below */
339 340 341 342 343

/**
 * cpufreq_get_max_state - callback function to get the max cooling state.
 * @cdev: thermal cooling device pointer.
 * @state: fill this variable with the max cooling state.
344 345 346 347 348
 *
 * Callback for the thermal cooling device to return the cpufreq
 * max cooling state.
 *
 * Return: 0 on success, an error code otherwise.
349 350 351 352
 */
static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
				 unsigned long *state)
{
353
	struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
354
	struct cpumask *mask = &cpufreq_device->allowed_cpus;
355
	unsigned int cpu;
356
	unsigned int count = 0;
357
	int ret;
358

359
	cpu = cpumask_any(mask);
360

361
	ret = get_property(cpu, 0, &count, GET_MAXL);
362

363 364
	if (count > 0)
		*state = count;
365

366
	return ret;
367 368 369 370 371 372
}

/**
 * cpufreq_get_cur_state - callback function to get the current cooling state.
 * @cdev: thermal cooling device pointer.
 * @state: fill this variable with the current cooling state.
373 374 375 376 377
 *
 * Callback for the thermal cooling device to return the cpufreq
 * current cooling state.
 *
 * Return: 0 on success, an error code otherwise.
378 379 380 381
 */
static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
				 unsigned long *state)
{
382
	struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
383

384
	*state = cpufreq_device->cpufreq_state;
385

386
	return 0;
387 388 389 390 391 392
}

/**
 * cpufreq_set_cur_state - callback function to set the current cooling state.
 * @cdev: thermal cooling device pointer.
 * @state: set this variable to the current cooling state.
393 394 395 396 397
 *
 * Callback for the thermal cooling device to change the cpufreq
 * current cooling state.
 *
 * Return: 0 on success, an error code otherwise.
398 399 400 401
 */
static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
				 unsigned long state)
{
402
	struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
403

404
	return cpufreq_apply_cooling(cpufreq_device, state);
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
}

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

/* Notifier for cpufreq policy change */
static struct notifier_block thermal_cpufreq_notifier_block = {
	.notifier_call = cpufreq_thermal_notifier,
};

/**
420 421
 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
 * @np: a valid struct device_node to the cooling device device tree node
422
 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
423 424 425
 *
 * This interface function registers the cpufreq cooling device with the name
 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
426 427
 * cooling devices. It also gives the opportunity to link the cooling device
 * with a device tree node, in order to bind it via the thermal DT code.
428 429 430
 *
 * Return: a valid struct thermal_cooling_device pointer on success,
 * on failure, it returns a corresponding ERR_PTR().
431
 */
432 433 434
static struct thermal_cooling_device *
__cpufreq_cooling_register(struct device_node *np,
			   const struct cpumask *clip_cpus)
435 436 437
{
	struct thermal_cooling_device *cool_dev;
	struct cpufreq_cooling_device *cpufreq_dev = NULL;
438
	unsigned int min = 0, max = 0;
439
	char dev_name[THERMAL_NAME_LENGTH];
440
	int ret = 0, i;
441 442
	struct cpufreq_policy policy;

443
	/* Verify that all the clip cpus have same freq_min, freq_max limit */
444
	for_each_cpu(i, clip_cpus) {
445
		/* continue if cpufreq policy not found and not return error */
446 447 448 449 450 451 452
		if (!cpufreq_get_policy(&policy, i))
			continue;
		if (min == 0 && max == 0) {
			min = policy.cpuinfo.min_freq;
			max = policy.cpuinfo.max_freq;
		} else {
			if (min != policy.cpuinfo.min_freq ||
453
			    max != policy.cpuinfo.max_freq)
454
				return ERR_PTR(-EINVAL);
455
		}
456 457
	}
	cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
458
			      GFP_KERNEL);
459 460 461 462 463 464 465 466 467 468 469
	if (!cpufreq_dev)
		return ERR_PTR(-ENOMEM);

	cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);

	ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
	if (ret) {
		kfree(cpufreq_dev);
		return ERR_PTR(-EINVAL);
	}

470 471
	snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
		 cpufreq_dev->id);
472

473 474
	cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
						      &cpufreq_cooling_ops);
475
	if (IS_ERR(cool_dev)) {
476 477
		release_idr(&cpufreq_idr, cpufreq_dev->id);
		kfree(cpufreq_dev);
478
		return cool_dev;
479 480 481 482 483 484 485 486
	}
	cpufreq_dev->cool_dev = cool_dev;
	cpufreq_dev->cpufreq_state = 0;
	mutex_lock(&cooling_cpufreq_lock);

	/* Register the notifier for first cpufreq cooling device */
	if (cpufreq_dev_count == 0)
		cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
487
					  CPUFREQ_POLICY_NOTIFIER);
488
	cpufreq_dev_count++;
489 490

	mutex_unlock(&cooling_cpufreq_lock);
491

492 493
	return cool_dev;
}
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510

/**
 * cpufreq_cooling_register - function to create cpufreq cooling device.
 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
 *
 * This interface function registers the cpufreq cooling device with the name
 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
 * cooling devices.
 *
 * Return: a valid struct thermal_cooling_device pointer on success,
 * on failure, it returns a corresponding ERR_PTR().
 */
struct thermal_cooling_device *
cpufreq_cooling_register(const struct cpumask *clip_cpus)
{
	return __cpufreq_cooling_register(NULL, clip_cpus);
}
511
EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
512

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
/**
 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
 * @np: a valid struct device_node to the cooling device device tree node
 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
 *
 * This interface function registers the cpufreq cooling device with the name
 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
 * cooling devices. Using this API, the cpufreq cooling device will be
 * linked to the device tree node provided.
 *
 * Return: a valid struct thermal_cooling_device pointer on success,
 * on failure, it returns a corresponding ERR_PTR().
 */
struct thermal_cooling_device *
of_cpufreq_cooling_register(struct device_node *np,
			    const struct cpumask *clip_cpus)
{
	if (!np)
		return ERR_PTR(-EINVAL);

	return __cpufreq_cooling_register(np, clip_cpus);
}
EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);

537 538 539
/**
 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
 * @cdev: thermal cooling device pointer.
540 541
 *
 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
542 543 544
 */
void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
{
545
	struct cpufreq_cooling_device *cpufreq_dev;
546

547 548 549 550
	if (!cdev)
		return;

	cpufreq_dev = cdev->devdata;
551
	mutex_lock(&cooling_cpufreq_lock);
552
	cpufreq_dev_count--;
553 554

	/* Unregister the notifier for the last cpufreq cooling device */
555
	if (cpufreq_dev_count == 0)
556
		cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
557
					    CPUFREQ_POLICY_NOTIFIER);
558
	mutex_unlock(&cooling_cpufreq_lock);
559

560 561 562 563
	thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
	release_idr(&cpufreq_idr, cpufreq_dev->id);
	kfree(cpufreq_dev);
}
564
EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);