]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/thermal/cpu_cooling.c
Merge branches 'for-4.11/upstream-fixes', 'for-4.12/accutouch', 'for-4.12/cp2112...
[mirror_ubuntu-artful-kernel.git] / drivers / thermal / cpu_cooling.c
CommitLineData
02361418
ADK
1/*
2 * linux/drivers/thermal/cpu_cooling.c
3 *
4 * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
5 * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org>
6 *
73904cbc
VK
7 * Copyright (C) 2014 Viresh Kumar <viresh.kumar@linaro.org>
8 *
02361418
ADK
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
02361418
ADK
25#include <linux/module.h>
26#include <linux/thermal.h>
02361418
ADK
27#include <linux/cpufreq.h>
28#include <linux/err.h>
c36cf071 29#include <linux/pm_opp.h>
02361418
ADK
30#include <linux/slab.h>
31#include <linux/cpu.h>
32#include <linux/cpu_cooling.h>
33
6828a471
JM
34#include <trace/events/thermal.h>
35
07d888d8
VK
36/*
37 * Cooling state <-> CPUFreq frequency
38 *
39 * Cooling states are translated to frequencies throughout this driver and this
40 * is the relation between them.
41 *
42 * Highest cooling state corresponds to lowest possible frequency.
43 *
44 * i.e.
45 * level 0 --> 1st Max Freq
46 * level 1 --> 2nd Max Freq
47 * ...
48 */
49
c36cf071
JM
50/**
51 * struct power_table - frequency to power conversion
52 * @frequency: frequency in KHz
53 * @power: power in mW
54 *
55 * This structure is built when the cooling device registers and helps
56 * in translating frequency to power and viceversa.
57 */
58struct power_table {
59 u32 frequency;
60 u32 power;
61};
62
02361418 63/**
3b3c0748 64 * struct cpufreq_cooling_device - data for cooling device with cpufreq
02361418
ADK
65 * @id: unique integer value corresponding to each cpufreq_cooling_device
66 * registered.
3b3c0748
EV
67 * @cool_dev: thermal_cooling_device pointer to keep track of the
68 * registered cooling device.
02361418
ADK
69 * @cpufreq_state: integer value representing the current state of cpufreq
70 * cooling devices.
59f0d218 71 * @clipped_freq: integer value representing the absolute value of the clipped
02361418 72 * frequency.
dcc6c7fd
VK
73 * @max_level: maximum cooling level. One less than total number of valid
74 * cpufreq frequencies.
02361418 75 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
fc4de356 76 * @node: list_head to link all cpufreq_cooling_device together.
0744f130 77 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
c36cf071
JM
78 * @time_in_idle: previous reading of the absolute time that this cpu was idle
79 * @time_in_idle_timestamp: wall time of the last invocation of
80 * get_cpu_idle_time_us()
81 * @dyn_power_table: array of struct power_table for frequency to power
82 * conversion, sorted in ascending order.
83 * @dyn_power_table_entries: number of entries in the @dyn_power_table array
84 * @cpu_dev: the first cpu_device from @allowed_cpus that has OPPs registered
85 * @plat_get_static_power: callback to calculate the static power
02361418 86 *
beca6053
VK
87 * This structure is required for keeping information of each registered
88 * cpufreq_cooling_device.
02361418
ADK
89 */
90struct cpufreq_cooling_device {
91 int id;
92 struct thermal_cooling_device *cool_dev;
93 unsigned int cpufreq_state;
59f0d218 94 unsigned int clipped_freq;
dcc6c7fd 95 unsigned int max_level;
f6859014 96 unsigned int *freq_table; /* In descending order */
02361418 97 struct cpumask allowed_cpus;
2dcd851f 98 struct list_head node;
c36cf071
JM
99 u32 last_load;
100 u64 *time_in_idle;
101 u64 *time_in_idle_timestamp;
102 struct power_table *dyn_power_table;
103 int dyn_power_table_entries;
104 struct device *cpu_dev;
105 get_static_t plat_get_static_power;
02361418 106};
02361418 107static DEFINE_IDR(cpufreq_idr);
160b7d80 108static DEFINE_MUTEX(cooling_cpufreq_lock);
02361418 109
02373d7c
RK
110static unsigned int cpufreq_dev_count;
111
112static DEFINE_MUTEX(cooling_list_lock);
2dcd851f 113static LIST_HEAD(cpufreq_dev_list);
02361418
ADK
114
115/**
116 * get_idr - function to get a unique id.
117 * @idr: struct idr * handle used to create a id.
118 * @id: int * value generated by this function.
79491e53
EV
119 *
120 * This function will populate @id with an unique
121 * id, using the idr API.
122 *
123 * Return: 0 on success, an error code on failure.
02361418
ADK
124 */
125static int get_idr(struct idr *idr, int *id)
126{
6deb69fa 127 int ret;
02361418
ADK
128
129 mutex_lock(&cooling_cpufreq_lock);
6deb69fa 130 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
02361418 131 mutex_unlock(&cooling_cpufreq_lock);
6deb69fa
TH
132 if (unlikely(ret < 0))
133 return ret;
134 *id = ret;
79491e53 135
02361418
ADK
136 return 0;
137}
138
139/**
140 * release_idr - function to free the unique id.
141 * @idr: struct idr * handle used for creating the id.
142 * @id: int value representing the unique id.
143 */
144static void release_idr(struct idr *idr, int id)
145{
146 mutex_lock(&cooling_cpufreq_lock);
147 idr_remove(idr, id);
148 mutex_unlock(&cooling_cpufreq_lock);
149}
150
151/* Below code defines functions to be used for cpufreq as cooling device */
152
153/**
4843c4a1 154 * get_level: Find the level for a particular frequency
b9f8b416 155 * @cpufreq_dev: cpufreq_dev for which the property is required
4843c4a1 156 * @freq: Frequency
82b9ee40 157 *
4843c4a1 158 * Return: level on success, THERMAL_CSTATE_INVALID on error.
02361418 159 */
4843c4a1
VK
160static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_dev,
161 unsigned int freq)
02361418 162{
4843c4a1 163 unsigned long level;
a116776f 164
4843c4a1
VK
165 for (level = 0; level <= cpufreq_dev->max_level; level++) {
166 if (freq == cpufreq_dev->freq_table[level])
167 return level;
02361418 168
4843c4a1
VK
169 if (freq > cpufreq_dev->freq_table[level])
170 break;
fc35b35c 171 }
02361418 172
4843c4a1 173 return THERMAL_CSTATE_INVALID;
fc35b35c
ZR
174}
175
44952d33 176/**
728c03c9 177 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
44952d33
EV
178 * @cpu: cpu for which the level is required
179 * @freq: the frequency of interest
180 *
181 * This function will match the cooling level corresponding to the
182 * requested @freq and return it.
183 *
184 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
185 * otherwise.
186 */
57df8106
ZR
187unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
188{
b9f8b416 189 struct cpufreq_cooling_device *cpufreq_dev;
02361418 190
02373d7c 191 mutex_lock(&cooling_list_lock);
b9f8b416
VK
192 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
193 if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
02373d7c 194 mutex_unlock(&cooling_list_lock);
4843c4a1 195 return get_level(cpufreq_dev, freq);
b9f8b416 196 }
02361418 197 }
02373d7c 198 mutex_unlock(&cooling_list_lock);
02361418 199
b9f8b416
VK
200 pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu);
201 return THERMAL_CSTATE_INVALID;
02361418 202}
243dbd9c 203EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
02361418
ADK
204
205/**
206 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
207 * @nb: struct notifier_block * with callback info.
208 * @event: value showing cpufreq event for which this function invoked.
209 * @data: callback-specific data
bab30554 210 *
9746b6e7 211 * Callback to hijack the notification on cpufreq policy transition.
bab30554
EV
212 * Every time there is a change in policy, we will intercept and
213 * update the cpufreq policy with thermal constraints.
214 *
215 * Return: 0 (success)
02361418
ADK
216 */
217static int cpufreq_thermal_notifier(struct notifier_block *nb,
5fda7f68 218 unsigned long event, void *data)
02361418
ADK
219{
220 struct cpufreq_policy *policy = data;
abcbcc25 221 unsigned long clipped_freq;
2dcd851f 222 struct cpufreq_cooling_device *cpufreq_dev;
02361418 223
a24af233
VK
224 if (event != CPUFREQ_ADJUST)
225 return NOTIFY_DONE;
02361418 226
a24af233
VK
227 mutex_lock(&cooling_list_lock);
228 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
229 if (!cpumask_test_cpu(policy->cpu, &cpufreq_dev->allowed_cpus))
230 continue;
c36cf071 231
1afb9c53
VK
232 /*
233 * policy->max is the maximum allowed frequency defined by user
234 * and clipped_freq is the maximum that thermal constraints
235 * allow.
236 *
237 * If clipped_freq is lower than policy->max, then we need to
238 * readjust policy->max.
239 *
240 * But, if clipped_freq is greater than policy->max, we don't
241 * need to do anything.
242 */
abcbcc25 243 clipped_freq = cpufreq_dev->clipped_freq;
c36cf071 244
1afb9c53 245 if (policy->max > clipped_freq)
abcbcc25 246 cpufreq_verify_within_limits(policy, 0, clipped_freq);
c36cf071 247 break;
c36cf071 248 }
a24af233 249 mutex_unlock(&cooling_list_lock);
c36cf071
JM
250
251 return NOTIFY_OK;
252}
253
254/**
255 * build_dyn_power_table() - create a dynamic power to frequency table
256 * @cpufreq_device: the cpufreq cooling device in which to store the table
257 * @capacitance: dynamic power coefficient for these cpus
258 *
259 * Build a dynamic power to frequency table for this cpu and store it
260 * in @cpufreq_device. This table will be used in cpu_power_to_freq() and
261 * cpu_freq_to_power() to convert between power and frequency
262 * efficiently. Power is stored in mW, frequency in KHz. The
263 * resulting table is in ascending order.
264 *
459ac375
JM
265 * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
266 * -ENOMEM if we run out of memory or -EAGAIN if an OPP was
267 * added/enabled while the function was executing.
c36cf071
JM
268 */
269static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_device,
270 u32 capacitance)
271{
272 struct power_table *power_table;
273 struct dev_pm_opp *opp;
274 struct device *dev = NULL;
eba4f88d 275 int num_opps = 0, cpu, i, ret = 0;
c36cf071
JM
276 unsigned long freq;
277
c36cf071
JM
278 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
279 dev = get_cpu_device(cpu);
280 if (!dev) {
281 dev_warn(&cpufreq_device->cool_dev->device,
282 "No cpu device for cpu %d\n", cpu);
2dcd851f 283 continue;
c36cf071 284 }
2dcd851f 285
c36cf071 286 num_opps = dev_pm_opp_get_opp_count(dev);
459ac375 287 if (num_opps > 0)
c36cf071 288 break;
459ac375
JM
289 else if (num_opps < 0)
290 return num_opps;
c36cf071 291 }
02361418 292
459ac375
JM
293 if (num_opps == 0)
294 return -EINVAL;
02361418 295
c36cf071 296 power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL);
459ac375
JM
297 if (!power_table)
298 return -ENOMEM;
299
c36cf071
JM
300 for (freq = 0, i = 0;
301 opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp);
302 freq++, i++) {
303 u32 freq_mhz, voltage_mv;
304 u64 power;
305
459ac375 306 if (i >= num_opps) {
eba4f88d
JM
307 ret = -EAGAIN;
308 goto free_power_table;
459ac375
JM
309 }
310
c36cf071
JM
311 freq_mhz = freq / 1000000;
312 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
8a31d9d9 313 dev_pm_opp_put(opp);
c36cf071
JM
314
315 /*
316 * Do the multiplication with MHz and millivolt so as
317 * to not overflow.
318 */
319 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
320 do_div(power, 1000000000);
321
322 /* frequency is stored in power_table in KHz */
323 power_table[i].frequency = freq / 1000;
324
325 /* power is stored in mW */
326 power_table[i].power = power;
327 }
328
eba4f88d
JM
329 if (i != num_opps) {
330 ret = PTR_ERR(opp);
331 goto free_power_table;
332 }
c36cf071
JM
333
334 cpufreq_device->cpu_dev = dev;
335 cpufreq_device->dyn_power_table = power_table;
336 cpufreq_device->dyn_power_table_entries = i;
337
459ac375 338 return 0;
eba4f88d
JM
339
340free_power_table:
341 kfree(power_table);
342
343 return ret;
c36cf071
JM
344}
345
346static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_device,
347 u32 freq)
348{
349 int i;
350 struct power_table *pt = cpufreq_device->dyn_power_table;
351
352 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
353 if (freq < pt[i].frequency)
354 break;
355
356 return pt[i - 1].power;
357}
358
359static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_device,
360 u32 power)
361{
362 int i;
363 struct power_table *pt = cpufreq_device->dyn_power_table;
364
365 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
366 if (power < pt[i].power)
367 break;
368
369 return pt[i - 1].frequency;
370}
371
372/**
373 * get_load() - get load for a cpu since last updated
374 * @cpufreq_device: &struct cpufreq_cooling_device for this cpu
375 * @cpu: cpu number
a53b8394 376 * @cpu_idx: index of the cpu in cpufreq_device->allowed_cpus
c36cf071
JM
377 *
378 * Return: The average load of cpu @cpu in percentage since this
379 * function was last called.
380 */
a53b8394
JM
381static u32 get_load(struct cpufreq_cooling_device *cpufreq_device, int cpu,
382 int cpu_idx)
c36cf071
JM
383{
384 u32 load;
385 u64 now, now_idle, delta_time, delta_idle;
386
387 now_idle = get_cpu_idle_time(cpu, &now, 0);
a53b8394
JM
388 delta_idle = now_idle - cpufreq_device->time_in_idle[cpu_idx];
389 delta_time = now - cpufreq_device->time_in_idle_timestamp[cpu_idx];
c36cf071
JM
390
391 if (delta_time <= delta_idle)
392 load = 0;
393 else
394 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
395
a53b8394
JM
396 cpufreq_device->time_in_idle[cpu_idx] = now_idle;
397 cpufreq_device->time_in_idle_timestamp[cpu_idx] = now;
c36cf071
JM
398
399 return load;
400}
401
402/**
403 * get_static_power() - calculate the static power consumed by the cpus
404 * @cpufreq_device: struct &cpufreq_cooling_device for this cpu cdev
405 * @tz: thermal zone device in which we're operating
406 * @freq: frequency in KHz
407 * @power: pointer in which to store the calculated static power
408 *
409 * Calculate the static power consumed by the cpus described by
410 * @cpu_actor running at frequency @freq. This function relies on a
411 * platform specific function that should have been provided when the
412 * actor was registered. If it wasn't, the static power is assumed to
413 * be negligible. The calculated static power is stored in @power.
414 *
415 * Return: 0 on success, -E* on failure.
416 */
417static int get_static_power(struct cpufreq_cooling_device *cpufreq_device,
418 struct thermal_zone_device *tz, unsigned long freq,
419 u32 *power)
420{
421 struct dev_pm_opp *opp;
422 unsigned long voltage;
423 struct cpumask *cpumask = &cpufreq_device->allowed_cpus;
424 unsigned long freq_hz = freq * 1000;
425
426 if (!cpufreq_device->plat_get_static_power ||
427 !cpufreq_device->cpu_dev) {
428 *power = 0;
429 return 0;
430 }
431
c36cf071
JM
432 opp = dev_pm_opp_find_freq_exact(cpufreq_device->cpu_dev, freq_hz,
433 true);
434 voltage = dev_pm_opp_get_voltage(opp);
8a31d9d9 435 dev_pm_opp_put(opp);
c36cf071
JM
436
437 if (voltage == 0) {
438 dev_warn_ratelimited(cpufreq_device->cpu_dev,
439 "Failed to get voltage for frequency %lu: %ld\n",
440 freq_hz, IS_ERR(opp) ? PTR_ERR(opp) : 0);
441 return -EINVAL;
442 }
443
444 return cpufreq_device->plat_get_static_power(cpumask, tz->passive_delay,
445 voltage, power);
446}
447
448/**
449 * get_dynamic_power() - calculate the dynamic power
450 * @cpufreq_device: &cpufreq_cooling_device for this cdev
451 * @freq: current frequency
452 *
453 * Return: the dynamic power consumed by the cpus described by
454 * @cpufreq_device.
455 */
456static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_device,
457 unsigned long freq)
458{
459 u32 raw_cpu_power;
460
461 raw_cpu_power = cpu_freq_to_power(cpufreq_device, freq);
462 return (raw_cpu_power * cpufreq_device->last_load) / 100;
02361418
ADK
463}
464
1b9e3526 465/* cpufreq cooling device callback functions are defined below */
02361418
ADK
466
467/**
468 * cpufreq_get_max_state - callback function to get the max cooling state.
469 * @cdev: thermal cooling device pointer.
470 * @state: fill this variable with the max cooling state.
62c00421
EV
471 *
472 * Callback for the thermal cooling device to return the cpufreq
473 * max cooling state.
474 *
475 * Return: 0 on success, an error code otherwise.
02361418
ADK
476 */
477static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
478 unsigned long *state)
479{
160b7d80 480 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
9c51b05a 481
dcc6c7fd
VK
482 *state = cpufreq_device->max_level;
483 return 0;
02361418
ADK
484}
485
486/**
487 * cpufreq_get_cur_state - callback function to get the current cooling state.
488 * @cdev: thermal cooling device pointer.
489 * @state: fill this variable with the current cooling state.
3672552d
EV
490 *
491 * Callback for the thermal cooling device to return the cpufreq
492 * current cooling state.
493 *
494 * Return: 0 on success, an error code otherwise.
02361418
ADK
495 */
496static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
497 unsigned long *state)
498{
160b7d80 499 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
02361418 500
160b7d80 501 *state = cpufreq_device->cpufreq_state;
79491e53 502
160b7d80 503 return 0;
02361418
ADK
504}
505
506/**
507 * cpufreq_set_cur_state - callback function to set the current cooling state.
508 * @cdev: thermal cooling device pointer.
509 * @state: set this variable to the current cooling state.
56e05fdb
EV
510 *
511 * Callback for the thermal cooling device to change the cpufreq
512 * current cooling state.
513 *
514 * Return: 0 on success, an error code otherwise.
02361418
ADK
515 */
516static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
517 unsigned long state)
518{
160b7d80 519 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
5194fe46
VK
520 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
521 unsigned int clip_freq;
4843c4a1
VK
522
523 /* Request state should be less than max_level */
524 if (WARN_ON(state > cpufreq_device->max_level))
525 return -EINVAL;
5194fe46
VK
526
527 /* Check if the old cooling action is same as new cooling action */
528 if (cpufreq_device->cpufreq_state == state)
529 return 0;
02361418 530
4843c4a1 531 clip_freq = cpufreq_device->freq_table[state];
5194fe46 532 cpufreq_device->cpufreq_state = state;
59f0d218 533 cpufreq_device->clipped_freq = clip_freq;
5194fe46
VK
534
535 cpufreq_update_policy(cpu);
536
537 return 0;
02361418
ADK
538}
539
c36cf071
JM
540/**
541 * cpufreq_get_requested_power() - get the current power
542 * @cdev: &thermal_cooling_device pointer
543 * @tz: a valid thermal zone device pointer
544 * @power: pointer in which to store the resulting power
545 *
546 * Calculate the current power consumption of the cpus in milliwatts
547 * and store it in @power. This function should actually calculate
548 * the requested power, but it's hard to get the frequency that
549 * cpufreq would have assigned if there were no thermal limits.
550 * Instead, we calculate the current power on the assumption that the
551 * immediate future will look like the immediate past.
552 *
553 * We use the current frequency and the average load since this
554 * function was last called. In reality, there could have been
555 * multiple opps since this function was last called and that affects
556 * the load calculation. While it's not perfectly accurate, this
557 * simplification is good enough and works. REVISIT this, as more
558 * complex code may be needed if experiments show that it's not
559 * accurate enough.
560 *
561 * Return: 0 on success, -E* if getting the static power failed.
562 */
563static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
564 struct thermal_zone_device *tz,
565 u32 *power)
566{
567 unsigned long freq;
6828a471 568 int i = 0, cpu, ret;
c36cf071
JM
569 u32 static_power, dynamic_power, total_load = 0;
570 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
6828a471 571 u32 *load_cpu = NULL;
c36cf071 572
dd658e02
KS
573 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
574
575 /*
576 * All the CPUs are offline, thus the requested power by
577 * the cdev is 0
578 */
579 if (cpu >= nr_cpu_ids) {
580 *power = 0;
581 return 0;
582 }
583
584 freq = cpufreq_quick_get(cpu);
c36cf071 585
6828a471
JM
586 if (trace_thermal_power_cpu_get_power_enabled()) {
587 u32 ncpus = cpumask_weight(&cpufreq_device->allowed_cpus);
588
a71544cd 589 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
6828a471
JM
590 }
591
c36cf071
JM
592 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
593 u32 load;
594
595 if (cpu_online(cpu))
a53b8394 596 load = get_load(cpufreq_device, cpu, i);
c36cf071
JM
597 else
598 load = 0;
599
600 total_load += load;
6828a471
JM
601 if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
602 load_cpu[i] = load;
603
604 i++;
c36cf071
JM
605 }
606
607 cpufreq_device->last_load = total_load;
608
609 dynamic_power = get_dynamic_power(cpufreq_device, freq);
610 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
6828a471 611 if (ret) {
a71544cd 612 kfree(load_cpu);
c36cf071 613 return ret;
6828a471
JM
614 }
615
616 if (load_cpu) {
617 trace_thermal_power_cpu_get_power(
618 &cpufreq_device->allowed_cpus,
619 freq, load_cpu, i, dynamic_power, static_power);
620
a71544cd 621 kfree(load_cpu);
6828a471 622 }
c36cf071
JM
623
624 *power = static_power + dynamic_power;
625 return 0;
626}
627
628/**
629 * cpufreq_state2power() - convert a cpu cdev state to power consumed
630 * @cdev: &thermal_cooling_device pointer
631 * @tz: a valid thermal zone device pointer
632 * @state: cooling device state to be converted
633 * @power: pointer in which to store the resulting power
634 *
635 * Convert cooling device state @state into power consumption in
636 * milliwatts assuming 100% load. Store the calculated power in
637 * @power.
638 *
639 * Return: 0 on success, -EINVAL if the cooling device state could not
640 * be converted into a frequency or other -E* if there was an error
641 * when calculating the static power.
642 */
643static int cpufreq_state2power(struct thermal_cooling_device *cdev,
644 struct thermal_zone_device *tz,
645 unsigned long state, u32 *power)
646{
647 unsigned int freq, num_cpus;
648 cpumask_t cpumask;
649 u32 static_power, dynamic_power;
650 int ret;
651 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
652
653 cpumask_and(&cpumask, &cpufreq_device->allowed_cpus, cpu_online_mask);
654 num_cpus = cpumask_weight(&cpumask);
655
656 /* None of our cpus are online, so no power */
657 if (num_cpus == 0) {
658 *power = 0;
659 return 0;
660 }
661
662 freq = cpufreq_device->freq_table[state];
663 if (!freq)
664 return -EINVAL;
665
666 dynamic_power = cpu_freq_to_power(cpufreq_device, freq) * num_cpus;
667 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
668 if (ret)
669 return ret;
670
671 *power = static_power + dynamic_power;
672 return 0;
673}
674
675/**
676 * cpufreq_power2state() - convert power to a cooling device state
677 * @cdev: &thermal_cooling_device pointer
678 * @tz: a valid thermal zone device pointer
679 * @power: power in milliwatts to be converted
680 * @state: pointer in which to store the resulting state
681 *
682 * Calculate a cooling device state for the cpus described by @cdev
683 * that would allow them to consume at most @power mW and store it in
684 * @state. Note that this calculation depends on external factors
685 * such as the cpu load or the current static power. Calling this
686 * function with the same power as input can yield different cooling
687 * device states depending on those external factors.
688 *
689 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
690 * the calculated frequency could not be converted to a valid state.
691 * The latter should not happen unless the frequencies available to
692 * cpufreq have changed since the initialization of the cpu cooling
693 * device.
694 */
695static int cpufreq_power2state(struct thermal_cooling_device *cdev,
696 struct thermal_zone_device *tz, u32 power,
697 unsigned long *state)
698{
699 unsigned int cpu, cur_freq, target_freq;
700 int ret;
701 s32 dyn_power;
702 u32 last_load, normalised_power, static_power;
703 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
704
705 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
706
707 /* None of our cpus are online */
708 if (cpu >= nr_cpu_ids)
709 return -ENODEV;
710
711 cur_freq = cpufreq_quick_get(cpu);
712 ret = get_static_power(cpufreq_device, tz, cur_freq, &static_power);
713 if (ret)
714 return ret;
715
716 dyn_power = power - static_power;
717 dyn_power = dyn_power > 0 ? dyn_power : 0;
718 last_load = cpufreq_device->last_load ?: 1;
719 normalised_power = (dyn_power * 100) / last_load;
720 target_freq = cpu_power_to_freq(cpufreq_device, normalised_power);
721
722 *state = cpufreq_cooling_get_level(cpu, target_freq);
723 if (*state == THERMAL_CSTATE_INVALID) {
724 dev_warn_ratelimited(&cdev->device,
725 "Failed to convert %dKHz for cpu %d into a cdev state\n",
726 target_freq, cpu);
727 return -EINVAL;
728 }
729
6828a471
JM
730 trace_thermal_power_cpu_limit(&cpufreq_device->allowed_cpus,
731 target_freq, *state, power);
c36cf071
JM
732 return 0;
733}
734
02361418 735/* Bind cpufreq callbacks to thermal cooling device ops */
a305a438 736
c36cf071 737static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
02361418
ADK
738 .get_max_state = cpufreq_get_max_state,
739 .get_cur_state = cpufreq_get_cur_state,
740 .set_cur_state = cpufreq_set_cur_state,
741};
742
a305a438
BJ
743static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
744 .get_max_state = cpufreq_get_max_state,
745 .get_cur_state = cpufreq_get_cur_state,
746 .set_cur_state = cpufreq_set_cur_state,
747 .get_requested_power = cpufreq_get_requested_power,
748 .state2power = cpufreq_state2power,
749 .power2state = cpufreq_power2state,
750};
751
02361418
ADK
752/* Notifier for cpufreq policy change */
753static struct notifier_block thermal_cpufreq_notifier_block = {
754 .notifier_call = cpufreq_thermal_notifier,
755};
756
f6859014
VK
757static unsigned int find_next_max(struct cpufreq_frequency_table *table,
758 unsigned int prev_max)
759{
760 struct cpufreq_frequency_table *pos;
761 unsigned int max = 0;
762
763 cpufreq_for_each_valid_entry(pos, table) {
764 if (pos->frequency > max && pos->frequency < prev_max)
765 max = pos->frequency;
766 }
767
768 return max;
769}
770
02361418 771/**
39d99cff
EV
772 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
773 * @np: a valid struct device_node to the cooling device device tree node
02361418 774 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
405fb825 775 * Normally this should be same as cpufreq policy->related_cpus.
c36cf071
JM
776 * @capacitance: dynamic power coefficient for these cpus
777 * @plat_static_func: function to calculate the static power consumed by these
778 * cpus (optional)
12cb08ba
EV
779 *
780 * This interface function registers the cpufreq cooling device with the name
781 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
39d99cff
EV
782 * cooling devices. It also gives the opportunity to link the cooling device
783 * with a device tree node, in order to bind it via the thermal DT code.
12cb08ba
EV
784 *
785 * Return: a valid struct thermal_cooling_device pointer on success,
786 * on failure, it returns a corresponding ERR_PTR().
02361418 787 */
39d99cff
EV
788static struct thermal_cooling_device *
789__cpufreq_cooling_register(struct device_node *np,
c36cf071
JM
790 const struct cpumask *clip_cpus, u32 capacitance,
791 get_static_t plat_static_func)
02361418 792{
f8bfc116 793 struct cpufreq_policy *policy;
02361418 794 struct thermal_cooling_device *cool_dev;
5d3bdb89 795 struct cpufreq_cooling_device *cpufreq_dev;
02361418 796 char dev_name[THERMAL_NAME_LENGTH];
dcc6c7fd 797 struct cpufreq_frequency_table *pos, *table;
f8bfc116 798 struct cpumask temp_mask;
c36cf071 799 unsigned int freq, i, num_cpus;
405fb825 800 int ret;
a305a438 801 struct thermal_cooling_device_ops *cooling_ops;
02361418 802
f8bfc116
VK
803 cpumask_and(&temp_mask, clip_cpus, cpu_online_mask);
804 policy = cpufreq_cpu_get(cpumask_first(&temp_mask));
805 if (!policy) {
806 pr_debug("%s: CPUFreq policy not found\n", __func__);
807 return ERR_PTR(-EPROBE_DEFER);
808 }
809
810 table = policy->freq_table;
dcc6c7fd 811 if (!table) {
0f1be51c 812 pr_debug("%s: CPUFreq table not found\n", __func__);
f8bfc116
VK
813 cool_dev = ERR_PTR(-ENODEV);
814 goto put_policy;
02361418 815 }
0f1be51c 816
98d522f0 817 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
f8bfc116
VK
818 if (!cpufreq_dev) {
819 cool_dev = ERR_PTR(-ENOMEM);
820 goto put_policy;
821 }
02361418 822
c36cf071
JM
823 num_cpus = cpumask_weight(clip_cpus);
824 cpufreq_dev->time_in_idle = kcalloc(num_cpus,
825 sizeof(*cpufreq_dev->time_in_idle),
826 GFP_KERNEL);
827 if (!cpufreq_dev->time_in_idle) {
828 cool_dev = ERR_PTR(-ENOMEM);
829 goto free_cdev;
830 }
831
832 cpufreq_dev->time_in_idle_timestamp =
833 kcalloc(num_cpus, sizeof(*cpufreq_dev->time_in_idle_timestamp),
834 GFP_KERNEL);
835 if (!cpufreq_dev->time_in_idle_timestamp) {
836 cool_dev = ERR_PTR(-ENOMEM);
837 goto free_time_in_idle;
838 }
839
dcc6c7fd
VK
840 /* Find max levels */
841 cpufreq_for_each_valid_entry(pos, table)
842 cpufreq_dev->max_level++;
843
f6859014
VK
844 cpufreq_dev->freq_table = kmalloc(sizeof(*cpufreq_dev->freq_table) *
845 cpufreq_dev->max_level, GFP_KERNEL);
846 if (!cpufreq_dev->freq_table) {
f6859014 847 cool_dev = ERR_PTR(-ENOMEM);
c36cf071 848 goto free_time_in_idle_timestamp;
f6859014
VK
849 }
850
dcc6c7fd
VK
851 /* max_level is an index, not a counter */
852 cpufreq_dev->max_level--;
853
02361418
ADK
854 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
855
c36cf071 856 if (capacitance) {
c36cf071
JM
857 cpufreq_dev->plat_get_static_power = plat_static_func;
858
859 ret = build_dyn_power_table(cpufreq_dev, capacitance);
860 if (ret) {
861 cool_dev = ERR_PTR(ret);
862 goto free_table;
863 }
a305a438
BJ
864
865 cooling_ops = &cpufreq_power_cooling_ops;
866 } else {
867 cooling_ops = &cpufreq_cooling_ops;
c36cf071
JM
868 }
869
02361418
ADK
870 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
871 if (ret) {
730abe06 872 cool_dev = ERR_PTR(ret);
eba4f88d 873 goto free_power_table;
02361418
ADK
874 }
875
f6859014
VK
876 /* Fill freq-table in descending order of frequencies */
877 for (i = 0, freq = -1; i <= cpufreq_dev->max_level; i++) {
878 freq = find_next_max(table, freq);
879 cpufreq_dev->freq_table[i] = freq;
880
881 /* Warn for duplicate entries */
882 if (!freq)
883 pr_warn("%s: table has duplicate entries\n", __func__);
884 else
885 pr_debug("%s: freq:%u KHz\n", __func__, freq);
02361418 886 }
f6859014 887
f840ab18
LL
888 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
889 cpufreq_dev->id);
890
891 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
a305a438 892 cooling_ops);
f840ab18
LL
893 if (IS_ERR(cool_dev))
894 goto remove_idr;
895
59f0d218 896 cpufreq_dev->clipped_freq = cpufreq_dev->freq_table[0];
02361418 897 cpufreq_dev->cool_dev = cool_dev;
92e615ec 898
02361418 899 mutex_lock(&cooling_cpufreq_lock);
02361418 900
02373d7c
RK
901 mutex_lock(&cooling_list_lock);
902 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
903 mutex_unlock(&cooling_list_lock);
904
02361418 905 /* Register the notifier for first cpufreq cooling device */
02373d7c 906 if (!cpufreq_dev_count++)
02361418 907 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
5fda7f68 908 CPUFREQ_POLICY_NOTIFIER);
02361418 909 mutex_unlock(&cooling_cpufreq_lock);
79491e53 910
f8bfc116 911 goto put_policy;
730abe06
VK
912
913remove_idr:
914 release_idr(&cpufreq_idr, cpufreq_dev->id);
eba4f88d
JM
915free_power_table:
916 kfree(cpufreq_dev->dyn_power_table);
f6859014
VK
917free_table:
918 kfree(cpufreq_dev->freq_table);
c36cf071
JM
919free_time_in_idle_timestamp:
920 kfree(cpufreq_dev->time_in_idle_timestamp);
921free_time_in_idle:
922 kfree(cpufreq_dev->time_in_idle);
730abe06
VK
923free_cdev:
924 kfree(cpufreq_dev);
f8bfc116
VK
925put_policy:
926 cpufreq_cpu_put(policy);
730abe06 927
02361418
ADK
928 return cool_dev;
929}
39d99cff
EV
930
931/**
932 * cpufreq_cooling_register - function to create cpufreq cooling device.
933 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
934 *
935 * This interface function registers the cpufreq cooling device with the name
936 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
937 * cooling devices.
938 *
939 * Return: a valid struct thermal_cooling_device pointer on success,
940 * on failure, it returns a corresponding ERR_PTR().
941 */
942struct thermal_cooling_device *
943cpufreq_cooling_register(const struct cpumask *clip_cpus)
944{
c36cf071 945 return __cpufreq_cooling_register(NULL, clip_cpus, 0, NULL);
39d99cff 946}
243dbd9c 947EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
02361418 948
39d99cff
EV
949/**
950 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
951 * @np: a valid struct device_node to the cooling device device tree node
952 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
953 *
954 * This interface function registers the cpufreq cooling device with the name
955 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
956 * cooling devices. Using this API, the cpufreq cooling device will be
957 * linked to the device tree node provided.
958 *
959 * Return: a valid struct thermal_cooling_device pointer on success,
960 * on failure, it returns a corresponding ERR_PTR().
961 */
962struct thermal_cooling_device *
963of_cpufreq_cooling_register(struct device_node *np,
964 const struct cpumask *clip_cpus)
965{
966 if (!np)
967 return ERR_PTR(-EINVAL);
968
c36cf071 969 return __cpufreq_cooling_register(np, clip_cpus, 0, NULL);
39d99cff
EV
970}
971EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
972
c36cf071
JM
973/**
974 * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
975 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
976 * @capacitance: dynamic power coefficient for these cpus
977 * @plat_static_func: function to calculate the static power consumed by these
978 * cpus (optional)
979 *
980 * This interface function registers the cpufreq cooling device with
981 * the name "thermal-cpufreq-%x". This api can support multiple
982 * instances of cpufreq cooling devices. Using this function, the
983 * cooling device will implement the power extensions by using a
984 * simple cpu power model. The cpus must have registered their OPPs
985 * using the OPP library.
986 *
987 * An optional @plat_static_func may be provided to calculate the
988 * static power consumed by these cpus. If the platform's static
989 * power consumption is unknown or negligible, make it NULL.
990 *
991 * Return: a valid struct thermal_cooling_device pointer on success,
992 * on failure, it returns a corresponding ERR_PTR().
993 */
994struct thermal_cooling_device *
995cpufreq_power_cooling_register(const struct cpumask *clip_cpus, u32 capacitance,
996 get_static_t plat_static_func)
997{
998 return __cpufreq_cooling_register(NULL, clip_cpus, capacitance,
999 plat_static_func);
1000}
1001EXPORT_SYMBOL(cpufreq_power_cooling_register);
1002
1003/**
1004 * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
1005 * @np: a valid struct device_node to the cooling device device tree node
1006 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
1007 * @capacitance: dynamic power coefficient for these cpus
1008 * @plat_static_func: function to calculate the static power consumed by these
1009 * cpus (optional)
1010 *
1011 * This interface function registers the cpufreq cooling device with
1012 * the name "thermal-cpufreq-%x". This api can support multiple
1013 * instances of cpufreq cooling devices. Using this API, the cpufreq
1014 * cooling device will be linked to the device tree node provided.
1015 * Using this function, the cooling device will implement the power
1016 * extensions by using a simple cpu power model. The cpus must have
1017 * registered their OPPs using the OPP library.
1018 *
1019 * An optional @plat_static_func may be provided to calculate the
1020 * static power consumed by these cpus. If the platform's static
1021 * power consumption is unknown or negligible, make it NULL.
1022 *
1023 * Return: a valid struct thermal_cooling_device pointer on success,
1024 * on failure, it returns a corresponding ERR_PTR().
1025 */
1026struct thermal_cooling_device *
1027of_cpufreq_power_cooling_register(struct device_node *np,
1028 const struct cpumask *clip_cpus,
1029 u32 capacitance,
1030 get_static_t plat_static_func)
1031{
1032 if (!np)
1033 return ERR_PTR(-EINVAL);
1034
1035 return __cpufreq_cooling_register(np, clip_cpus, capacitance,
1036 plat_static_func);
1037}
1038EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
1039
02361418
ADK
1040/**
1041 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
1042 * @cdev: thermal cooling device pointer.
135266b4
EV
1043 *
1044 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
02361418
ADK
1045 */
1046void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
1047{
50e66c7e 1048 struct cpufreq_cooling_device *cpufreq_dev;
02361418 1049
50e66c7e
EV
1050 if (!cdev)
1051 return;
1052
1053 cpufreq_dev = cdev->devdata;
02361418
ADK
1054
1055 /* Unregister the notifier for the last cpufreq cooling device */
02373d7c
RK
1056 mutex_lock(&cooling_cpufreq_lock);
1057 if (!--cpufreq_dev_count)
02361418 1058 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
5fda7f68 1059 CPUFREQ_POLICY_NOTIFIER);
02373d7c
RK
1060
1061 mutex_lock(&cooling_list_lock);
1062 list_del(&cpufreq_dev->node);
1063 mutex_unlock(&cooling_list_lock);
1064
02361418 1065 mutex_unlock(&cooling_cpufreq_lock);
160b7d80 1066
02361418
ADK
1067 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
1068 release_idr(&cpufreq_idr, cpufreq_dev->id);
eba4f88d 1069 kfree(cpufreq_dev->dyn_power_table);
c36cf071
JM
1070 kfree(cpufreq_dev->time_in_idle_timestamp);
1071 kfree(cpufreq_dev->time_in_idle);
f6859014 1072 kfree(cpufreq_dev->freq_table);
02361418
ADK
1073 kfree(cpufreq_dev);
1074}
243dbd9c 1075EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);