Merge tag 'nfsd-5.1' of git://linux-nfs.org/~bfields/linux
[linux-2.6-block.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>
ae606089 29#include <linux/idr.h>
c36cf071 30#include <linux/pm_opp.h>
02361418
ADK
31#include <linux/slab.h>
32#include <linux/cpu.h>
33#include <linux/cpu_cooling.h>
34
6828a471
JM
35#include <trace/events/thermal.h>
36
07d888d8
VK
37/*
38 * Cooling state <-> CPUFreq frequency
39 *
40 * Cooling states are translated to frequencies throughout this driver and this
41 * is the relation between them.
42 *
43 * Highest cooling state corresponds to lowest possible frequency.
44 *
45 * i.e.
46 * level 0 --> 1st Max Freq
47 * level 1 --> 2nd Max Freq
48 * ...
49 */
50
c36cf071 51/**
349d39dc 52 * struct freq_table - frequency table along with power entries
c36cf071
JM
53 * @frequency: frequency in KHz
54 * @power: power in mW
55 *
56 * This structure is built when the cooling device registers and helps
349d39dc 57 * in translating frequency to power and vice versa.
c36cf071 58 */
349d39dc 59struct freq_table {
c36cf071
JM
60 u32 frequency;
61 u32 power;
62};
63
81ee14da
VK
64/**
65 * struct time_in_idle - Idle time stats
66 * @time: previous reading of the absolute time that this cpu was idle
67 * @timestamp: wall time of the last invocation of get_cpu_idle_time_us()
68 */
69struct time_in_idle {
70 u64 time;
71 u64 timestamp;
72};
73
02361418 74/**
3b3c0748 75 * struct cpufreq_cooling_device - data for cooling device with cpufreq
02361418
ADK
76 * @id: unique integer value corresponding to each cpufreq_cooling_device
77 * registered.
d72b4015 78 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
02361418
ADK
79 * @cpufreq_state: integer value representing the current state of cpufreq
80 * cooling devices.
59f0d218 81 * @clipped_freq: integer value representing the absolute value of the clipped
02361418 82 * frequency.
dcc6c7fd
VK
83 * @max_level: maximum cooling level. One less than total number of valid
84 * cpufreq frequencies.
d72b4015
VK
85 * @freq_table: Freq table in descending order of frequencies
86 * @cdev: thermal_cooling_device pointer to keep track of the
87 * registered cooling device.
88 * @policy: cpufreq policy.
fc4de356 89 * @node: list_head to link all cpufreq_cooling_device together.
81ee14da 90 * @idle_time: idle time stats
02361418 91 *
beca6053
VK
92 * This structure is required for keeping information of each registered
93 * cpufreq_cooling_device.
02361418
ADK
94 */
95struct cpufreq_cooling_device {
96 int id;
d72b4015 97 u32 last_load;
02361418 98 unsigned int cpufreq_state;
59f0d218 99 unsigned int clipped_freq;
dcc6c7fd 100 unsigned int max_level;
349d39dc 101 struct freq_table *freq_table; /* In descending order */
d72b4015
VK
102 struct thermal_cooling_device *cdev;
103 struct cpufreq_policy *policy;
2dcd851f 104 struct list_head node;
81ee14da 105 struct time_in_idle *idle_time;
02361418 106};
02361418 107
fb8ea308 108static DEFINE_IDA(cpufreq_ida);
02373d7c 109static DEFINE_MUTEX(cooling_list_lock);
1dea432a 110static LIST_HEAD(cpufreq_cdev_list);
02361418 111
02361418
ADK
112/* Below code defines functions to be used for cpufreq as cooling device */
113
114/**
4843c4a1 115 * get_level: Find the level for a particular frequency
1dea432a 116 * @cpufreq_cdev: cpufreq_cdev for which the property is required
4843c4a1 117 * @freq: Frequency
82b9ee40 118 *
da27f69d 119 * Return: level corresponding to the frequency.
02361418 120 */
1dea432a 121static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
4843c4a1 122 unsigned int freq)
02361418 123{
da27f69d 124 struct freq_table *freq_table = cpufreq_cdev->freq_table;
4843c4a1 125 unsigned long level;
a116776f 126
da27f69d
VK
127 for (level = 1; level <= cpufreq_cdev->max_level; level++)
128 if (freq > freq_table[level].frequency)
4843c4a1 129 break;
02361418 130
da27f69d 131 return level - 1;
fc35b35c
ZR
132}
133
02361418
ADK
134/**
135 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
136 * @nb: struct notifier_block * with callback info.
137 * @event: value showing cpufreq event for which this function invoked.
138 * @data: callback-specific data
bab30554 139 *
9746b6e7 140 * Callback to hijack the notification on cpufreq policy transition.
bab30554
EV
141 * Every time there is a change in policy, we will intercept and
142 * update the cpufreq policy with thermal constraints.
143 *
144 * Return: 0 (success)
02361418
ADK
145 */
146static int cpufreq_thermal_notifier(struct notifier_block *nb,
5fda7f68 147 unsigned long event, void *data)
02361418
ADK
148{
149 struct cpufreq_policy *policy = data;
abcbcc25 150 unsigned long clipped_freq;
1dea432a 151 struct cpufreq_cooling_device *cpufreq_cdev;
02361418 152
a24af233
VK
153 if (event != CPUFREQ_ADJUST)
154 return NOTIFY_DONE;
02361418 155
a24af233 156 mutex_lock(&cooling_list_lock);
1dea432a 157 list_for_each_entry(cpufreq_cdev, &cpufreq_cdev_list, node) {
ba76dd9d
VK
158 /*
159 * A new copy of the policy is sent to the notifier and can't
160 * compare that directly.
161 */
162 if (policy->cpu != cpufreq_cdev->policy->cpu)
a24af233 163 continue;
c36cf071 164
1afb9c53
VK
165 /*
166 * policy->max is the maximum allowed frequency defined by user
167 * and clipped_freq is the maximum that thermal constraints
168 * allow.
169 *
170 * If clipped_freq is lower than policy->max, then we need to
171 * readjust policy->max.
172 *
173 * But, if clipped_freq is greater than policy->max, we don't
174 * need to do anything.
175 */
1dea432a 176 clipped_freq = cpufreq_cdev->clipped_freq;
c36cf071 177
1afb9c53 178 if (policy->max > clipped_freq)
abcbcc25 179 cpufreq_verify_within_limits(policy, 0, clipped_freq);
c36cf071 180 break;
c36cf071 181 }
a24af233 182 mutex_unlock(&cooling_list_lock);
c36cf071
JM
183
184 return NOTIFY_OK;
185}
186
187/**
349d39dc
VK
188 * update_freq_table() - Update the freq table with power numbers
189 * @cpufreq_cdev: the cpufreq cooling device in which to update the table
c36cf071
JM
190 * @capacitance: dynamic power coefficient for these cpus
191 *
349d39dc
VK
192 * Update the freq table with power numbers. This table will be used in
193 * cpu_power_to_freq() and cpu_freq_to_power() to convert between power and
194 * frequency efficiently. Power is stored in mW, frequency in KHz. The
195 * resulting table is in descending order.
c36cf071 196 *
459ac375 197 * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
349d39dc 198 * or -ENOMEM if we run out of memory.
c36cf071 199 */
349d39dc
VK
200static int update_freq_table(struct cpufreq_cooling_device *cpufreq_cdev,
201 u32 capacitance)
c36cf071 202{
349d39dc 203 struct freq_table *freq_table = cpufreq_cdev->freq_table;
c36cf071
JM
204 struct dev_pm_opp *opp;
205 struct device *dev = NULL;
349d39dc 206 int num_opps = 0, cpu = cpufreq_cdev->policy->cpu, i;
c36cf071 207
02bacb21
VK
208 dev = get_cpu_device(cpu);
209 if (unlikely(!dev)) {
210 dev_warn(&cpufreq_cdev->cdev->device,
211 "No cpu device for cpu %d\n", cpu);
212 return -ENODEV;
c36cf071 213 }
02361418 214
02bacb21
VK
215 num_opps = dev_pm_opp_get_opp_count(dev);
216 if (num_opps < 0)
217 return num_opps;
218
349d39dc
VK
219 /*
220 * The cpufreq table is also built from the OPP table and so the count
221 * should match.
222 */
223 if (num_opps != cpufreq_cdev->max_level + 1) {
224 dev_warn(dev, "Number of OPPs not matching with max_levels\n");
459ac375 225 return -EINVAL;
349d39dc 226 }
02361418 227
349d39dc
VK
228 for (i = 0; i <= cpufreq_cdev->max_level; i++) {
229 unsigned long freq = freq_table[i].frequency * 1000;
230 u32 freq_mhz = freq_table[i].frequency / 1000;
c36cf071 231 u64 power;
349d39dc 232 u32 voltage_mv;
c36cf071 233
349d39dc
VK
234 /*
235 * Find ceil frequency as 'freq' may be slightly lower than OPP
236 * freq due to truncation while converting to kHz.
237 */
238 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
239 if (IS_ERR(opp)) {
240 dev_err(dev, "failed to get opp for %lu frequency\n",
241 freq);
242 return -EINVAL;
459ac375
JM
243 }
244
c36cf071 245 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
8a31d9d9 246 dev_pm_opp_put(opp);
c36cf071
JM
247
248 /*
249 * Do the multiplication with MHz and millivolt so as
250 * to not overflow.
251 */
252 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
253 do_div(power, 1000000000);
254
c36cf071 255 /* power is stored in mW */
349d39dc 256 freq_table[i].power = power;
eba4f88d 257 }
c36cf071 258
459ac375 259 return 0;
c36cf071
JM
260}
261
1dea432a 262static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
c36cf071
JM
263 u32 freq)
264{
265 int i;
349d39dc 266 struct freq_table *freq_table = cpufreq_cdev->freq_table;
c36cf071 267
349d39dc
VK
268 for (i = 1; i <= cpufreq_cdev->max_level; i++)
269 if (freq > freq_table[i].frequency)
c36cf071
JM
270 break;
271
349d39dc 272 return freq_table[i - 1].power;
c36cf071
JM
273}
274
1dea432a 275static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
c36cf071
JM
276 u32 power)
277{
278 int i;
349d39dc 279 struct freq_table *freq_table = cpufreq_cdev->freq_table;
c36cf071 280
349d39dc
VK
281 for (i = 1; i <= cpufreq_cdev->max_level; i++)
282 if (power > freq_table[i].power)
c36cf071
JM
283 break;
284
349d39dc 285 return freq_table[i - 1].frequency;
c36cf071
JM
286}
287
288/**
289 * get_load() - get load for a cpu since last updated
1dea432a 290 * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu
c36cf071 291 * @cpu: cpu number
ba76dd9d 292 * @cpu_idx: index of the cpu in time_in_idle*
c36cf071
JM
293 *
294 * Return: The average load of cpu @cpu in percentage since this
295 * function was last called.
296 */
1dea432a 297static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
a53b8394 298 int cpu_idx)
c36cf071
JM
299{
300 u32 load;
301 u64 now, now_idle, delta_time, delta_idle;
81ee14da 302 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
c36cf071
JM
303
304 now_idle = get_cpu_idle_time(cpu, &now, 0);
81ee14da
VK
305 delta_idle = now_idle - idle_time->time;
306 delta_time = now - idle_time->timestamp;
c36cf071
JM
307
308 if (delta_time <= delta_idle)
309 load = 0;
310 else
311 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
312
81ee14da
VK
313 idle_time->time = now_idle;
314 idle_time->timestamp = now;
c36cf071
JM
315
316 return load;
317}
318
c36cf071
JM
319/**
320 * get_dynamic_power() - calculate the dynamic power
1dea432a 321 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
c36cf071
JM
322 * @freq: current frequency
323 *
324 * Return: the dynamic power consumed by the cpus described by
1dea432a 325 * @cpufreq_cdev.
c36cf071 326 */
1dea432a 327static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
c36cf071
JM
328 unsigned long freq)
329{
330 u32 raw_cpu_power;
331
1dea432a
VK
332 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
333 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
02361418
ADK
334}
335
1b9e3526 336/* cpufreq cooling device callback functions are defined below */
02361418
ADK
337
338/**
339 * cpufreq_get_max_state - callback function to get the max cooling state.
340 * @cdev: thermal cooling device pointer.
341 * @state: fill this variable with the max cooling state.
62c00421
EV
342 *
343 * Callback for the thermal cooling device to return the cpufreq
344 * max cooling state.
345 *
346 * Return: 0 on success, an error code otherwise.
02361418
ADK
347 */
348static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
349 unsigned long *state)
350{
1dea432a 351 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
9c51b05a 352
1dea432a 353 *state = cpufreq_cdev->max_level;
dcc6c7fd 354 return 0;
02361418
ADK
355}
356
357/**
358 * cpufreq_get_cur_state - callback function to get the current cooling state.
359 * @cdev: thermal cooling device pointer.
360 * @state: fill this variable with the current cooling state.
3672552d
EV
361 *
362 * Callback for the thermal cooling device to return the cpufreq
363 * current cooling state.
364 *
365 * Return: 0 on success, an error code otherwise.
02361418
ADK
366 */
367static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
368 unsigned long *state)
369{
1dea432a 370 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
02361418 371
1dea432a 372 *state = cpufreq_cdev->cpufreq_state;
79491e53 373
160b7d80 374 return 0;
02361418
ADK
375}
376
377/**
378 * cpufreq_set_cur_state - callback function to set the current cooling state.
379 * @cdev: thermal cooling device pointer.
380 * @state: set this variable to the current cooling state.
56e05fdb
EV
381 *
382 * Callback for the thermal cooling device to change the cpufreq
383 * current cooling state.
384 *
385 * Return: 0 on success, an error code otherwise.
02361418
ADK
386 */
387static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
388 unsigned long state)
389{
1dea432a 390 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
5194fe46 391 unsigned int clip_freq;
4843c4a1
VK
392
393 /* Request state should be less than max_level */
1dea432a 394 if (WARN_ON(state > cpufreq_cdev->max_level))
4843c4a1 395 return -EINVAL;
5194fe46
VK
396
397 /* Check if the old cooling action is same as new cooling action */
1dea432a 398 if (cpufreq_cdev->cpufreq_state == state)
5194fe46 399 return 0;
02361418 400
349d39dc 401 clip_freq = cpufreq_cdev->freq_table[state].frequency;
1dea432a
VK
402 cpufreq_cdev->cpufreq_state = state;
403 cpufreq_cdev->clipped_freq = clip_freq;
5194fe46 404
ba76dd9d 405 cpufreq_update_policy(cpufreq_cdev->policy->cpu);
5194fe46
VK
406
407 return 0;
02361418
ADK
408}
409
c36cf071
JM
410/**
411 * cpufreq_get_requested_power() - get the current power
412 * @cdev: &thermal_cooling_device pointer
413 * @tz: a valid thermal zone device pointer
414 * @power: pointer in which to store the resulting power
415 *
416 * Calculate the current power consumption of the cpus in milliwatts
417 * and store it in @power. This function should actually calculate
418 * the requested power, but it's hard to get the frequency that
419 * cpufreq would have assigned if there were no thermal limits.
420 * Instead, we calculate the current power on the assumption that the
421 * immediate future will look like the immediate past.
422 *
423 * We use the current frequency and the average load since this
424 * function was last called. In reality, there could have been
425 * multiple opps since this function was last called and that affects
426 * the load calculation. While it's not perfectly accurate, this
427 * simplification is good enough and works. REVISIT this, as more
428 * complex code may be needed if experiments show that it's not
429 * accurate enough.
430 *
431 * Return: 0 on success, -E* if getting the static power failed.
432 */
433static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
434 struct thermal_zone_device *tz,
435 u32 *power)
436{
437 unsigned long freq;
84fe2cab
VK
438 int i = 0, cpu;
439 u32 total_load = 0;
1dea432a 440 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
ba76dd9d 441 struct cpufreq_policy *policy = cpufreq_cdev->policy;
6828a471 442 u32 *load_cpu = NULL;
c36cf071 443
ba76dd9d 444 freq = cpufreq_quick_get(policy->cpu);
c36cf071 445
6828a471 446 if (trace_thermal_power_cpu_get_power_enabled()) {
ba76dd9d 447 u32 ncpus = cpumask_weight(policy->related_cpus);
6828a471 448
a71544cd 449 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
6828a471
JM
450 }
451
ba76dd9d 452 for_each_cpu(cpu, policy->related_cpus) {
c36cf071
JM
453 u32 load;
454
455 if (cpu_online(cpu))
1dea432a 456 load = get_load(cpufreq_cdev, cpu, i);
c36cf071
JM
457 else
458 load = 0;
459
460 total_load += load;
6828a471
JM
461 if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
462 load_cpu[i] = load;
463
464 i++;
c36cf071
JM
465 }
466
1dea432a 467 cpufreq_cdev->last_load = total_load;
c36cf071 468
84fe2cab 469 *power = get_dynamic_power(cpufreq_cdev, freq);
6828a471
JM
470
471 if (load_cpu) {
ba76dd9d 472 trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
84fe2cab 473 load_cpu, i, *power);
6828a471 474
a71544cd 475 kfree(load_cpu);
6828a471 476 }
c36cf071 477
c36cf071
JM
478 return 0;
479}
480
481/**
482 * cpufreq_state2power() - convert a cpu cdev state to power consumed
483 * @cdev: &thermal_cooling_device pointer
484 * @tz: a valid thermal zone device pointer
485 * @state: cooling device state to be converted
486 * @power: pointer in which to store the resulting power
487 *
488 * Convert cooling device state @state into power consumption in
489 * milliwatts assuming 100% load. Store the calculated power in
490 * @power.
491 *
492 * Return: 0 on success, -EINVAL if the cooling device state could not
493 * be converted into a frequency or other -E* if there was an error
494 * when calculating the static power.
495 */
496static int cpufreq_state2power(struct thermal_cooling_device *cdev,
497 struct thermal_zone_device *tz,
498 unsigned long state, u32 *power)
499{
500 unsigned int freq, num_cpus;
1dea432a 501 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
c36cf071 502
cb1b6318
VK
503 /* Request state should be less than max_level */
504 if (WARN_ON(state > cpufreq_cdev->max_level))
505 return -EINVAL;
506
ba76dd9d 507 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
c36cf071 508
349d39dc 509 freq = cpufreq_cdev->freq_table[state].frequency;
84fe2cab 510 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
c36cf071 511
84fe2cab 512 return 0;
c36cf071
JM
513}
514
515/**
516 * cpufreq_power2state() - convert power to a cooling device state
517 * @cdev: &thermal_cooling_device pointer
518 * @tz: a valid thermal zone device pointer
519 * @power: power in milliwatts to be converted
520 * @state: pointer in which to store the resulting state
521 *
522 * Calculate a cooling device state for the cpus described by @cdev
523 * that would allow them to consume at most @power mW and store it in
524 * @state. Note that this calculation depends on external factors
525 * such as the cpu load or the current static power. Calling this
526 * function with the same power as input can yield different cooling
527 * device states depending on those external factors.
528 *
529 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
530 * the calculated frequency could not be converted to a valid state.
531 * The latter should not happen unless the frequencies available to
532 * cpufreq have changed since the initialization of the cpu cooling
533 * device.
534 */
535static int cpufreq_power2state(struct thermal_cooling_device *cdev,
536 struct thermal_zone_device *tz, u32 power,
537 unsigned long *state)
538{
ba76dd9d 539 unsigned int cur_freq, target_freq;
84fe2cab 540 u32 last_load, normalised_power;
1dea432a 541 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
ba76dd9d 542 struct cpufreq_policy *policy = cpufreq_cdev->policy;
c36cf071 543
ba76dd9d 544 cur_freq = cpufreq_quick_get(policy->cpu);
84fe2cab 545 power = power > 0 ? power : 0;
1dea432a 546 last_load = cpufreq_cdev->last_load ?: 1;
84fe2cab 547 normalised_power = (power * 100) / last_load;
1dea432a 548 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
c36cf071 549
3e08b2df 550 *state = get_level(cpufreq_cdev, target_freq);
ba76dd9d
VK
551 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
552 power);
c36cf071
JM
553 return 0;
554}
555
02361418 556/* Bind cpufreq callbacks to thermal cooling device ops */
a305a438 557
c36cf071 558static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
02361418
ADK
559 .get_max_state = cpufreq_get_max_state,
560 .get_cur_state = cpufreq_get_cur_state,
561 .set_cur_state = cpufreq_set_cur_state,
562};
563
a305a438
BJ
564static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
565 .get_max_state = cpufreq_get_max_state,
566 .get_cur_state = cpufreq_get_cur_state,
567 .set_cur_state = cpufreq_set_cur_state,
568 .get_requested_power = cpufreq_get_requested_power,
569 .state2power = cpufreq_state2power,
570 .power2state = cpufreq_power2state,
571};
572
02361418
ADK
573/* Notifier for cpufreq policy change */
574static struct notifier_block thermal_cpufreq_notifier_block = {
575 .notifier_call = cpufreq_thermal_notifier,
576};
577
f6859014
VK
578static unsigned int find_next_max(struct cpufreq_frequency_table *table,
579 unsigned int prev_max)
580{
581 struct cpufreq_frequency_table *pos;
582 unsigned int max = 0;
583
584 cpufreq_for_each_valid_entry(pos, table) {
585 if (pos->frequency > max && pos->frequency < prev_max)
586 max = pos->frequency;
587 }
588
589 return max;
590}
591
02361418 592/**
39d99cff
EV
593 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
594 * @np: a valid struct device_node to the cooling device device tree node
4d753aa7 595 * @policy: cpufreq policy
405fb825 596 * Normally this should be same as cpufreq policy->related_cpus.
c36cf071 597 * @capacitance: dynamic power coefficient for these cpus
12cb08ba
EV
598 *
599 * This interface function registers the cpufreq cooling device with the name
600 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
39d99cff
EV
601 * cooling devices. It also gives the opportunity to link the cooling device
602 * with a device tree node, in order to bind it via the thermal DT code.
12cb08ba
EV
603 *
604 * Return: a valid struct thermal_cooling_device pointer on success,
605 * on failure, it returns a corresponding ERR_PTR().
02361418 606 */
39d99cff
EV
607static struct thermal_cooling_device *
608__cpufreq_cooling_register(struct device_node *np,
84fe2cab 609 struct cpufreq_policy *policy, u32 capacitance)
02361418 610{
04bdbdf9 611 struct thermal_cooling_device *cdev;
1dea432a 612 struct cpufreq_cooling_device *cpufreq_cdev;
02361418 613 char dev_name[THERMAL_NAME_LENGTH];
c36cf071 614 unsigned int freq, i, num_cpus;
405fb825 615 int ret;
a305a438 616 struct thermal_cooling_device_ops *cooling_ops;
088db931 617 bool first;
02361418 618
4d753aa7 619 if (IS_ERR_OR_NULL(policy)) {
b2fd708f 620 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
4d753aa7 621 return ERR_PTR(-EINVAL);
f8bfc116
VK
622 }
623
55d85293
VK
624 i = cpufreq_table_count_valid_entries(policy);
625 if (!i) {
626 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
627 __func__);
4d753aa7 628 return ERR_PTR(-ENODEV);
02361418 629 }
0f1be51c 630
1dea432a 631 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
4d753aa7
VK
632 if (!cpufreq_cdev)
633 return ERR_PTR(-ENOMEM);
02361418 634
b12b6519 635 cpufreq_cdev->policy = policy;
4d753aa7 636 num_cpus = cpumask_weight(policy->related_cpus);
81ee14da
VK
637 cpufreq_cdev->idle_time = kcalloc(num_cpus,
638 sizeof(*cpufreq_cdev->idle_time),
639 GFP_KERNEL);
640 if (!cpufreq_cdev->idle_time) {
04bdbdf9 641 cdev = ERR_PTR(-ENOMEM);
c36cf071
JM
642 goto free_cdev;
643 }
644
55d85293
VK
645 /* max_level is an index, not a counter */
646 cpufreq_cdev->max_level = i - 1;
dcc6c7fd 647
f19b1a17
VK
648 cpufreq_cdev->freq_table = kmalloc_array(i,
649 sizeof(*cpufreq_cdev->freq_table),
650 GFP_KERNEL);
1dea432a 651 if (!cpufreq_cdev->freq_table) {
04bdbdf9 652 cdev = ERR_PTR(-ENOMEM);
81ee14da 653 goto free_idle_time;
f6859014
VK
654 }
655
ae606089
MW
656 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
657 if (ret < 0) {
04bdbdf9 658 cdev = ERR_PTR(ret);
349d39dc 659 goto free_table;
02361418 660 }
1dea432a 661 cpufreq_cdev->id = ret;
02361418 662
349d39dc
VK
663 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
664 cpufreq_cdev->id);
665
f6859014 666 /* Fill freq-table in descending order of frequencies */
1dea432a 667 for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) {
55d85293 668 freq = find_next_max(policy->freq_table, freq);
349d39dc 669 cpufreq_cdev->freq_table[i].frequency = freq;
f6859014
VK
670
671 /* Warn for duplicate entries */
672 if (!freq)
673 pr_warn("%s: table has duplicate entries\n", __func__);
674 else
675 pr_debug("%s: freq:%u KHz\n", __func__, freq);
02361418 676 }
f6859014 677
349d39dc 678 if (capacitance) {
349d39dc
VK
679 ret = update_freq_table(cpufreq_cdev, capacitance);
680 if (ret) {
681 cdev = ERR_PTR(ret);
682 goto remove_ida;
683 }
684
685 cooling_ops = &cpufreq_power_cooling_ops;
686 } else {
687 cooling_ops = &cpufreq_cooling_ops;
688 }
f840ab18 689
04bdbdf9
VK
690 cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev,
691 cooling_ops);
692 if (IS_ERR(cdev))
ae606089 693 goto remove_ida;
f840ab18 694
349d39dc 695 cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0].frequency;
04bdbdf9 696 cpufreq_cdev->cdev = cdev;
92e615ec 697
02373d7c 698 mutex_lock(&cooling_list_lock);
088db931 699 /* Register the notifier for first cpufreq cooling device */
1dea432a
VK
700 first = list_empty(&cpufreq_cdev_list);
701 list_add(&cpufreq_cdev->node, &cpufreq_cdev_list);
088db931 702 mutex_unlock(&cooling_list_lock);
02373d7c 703
088db931 704 if (first)
02361418 705 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
5fda7f68 706 CPUFREQ_POLICY_NOTIFIER);
79491e53 707
4d753aa7 708 return cdev;
730abe06 709
ae606089 710remove_ida:
1dea432a 711 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
f6859014 712free_table:
1dea432a 713 kfree(cpufreq_cdev->freq_table);
81ee14da
VK
714free_idle_time:
715 kfree(cpufreq_cdev->idle_time);
730abe06 716free_cdev:
1dea432a 717 kfree(cpufreq_cdev);
04bdbdf9 718 return cdev;
02361418 719}
39d99cff
EV
720
721/**
722 * cpufreq_cooling_register - function to create cpufreq cooling device.
4d753aa7 723 * @policy: cpufreq policy
39d99cff
EV
724 *
725 * This interface function registers the cpufreq cooling device with the name
726 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
727 * cooling devices.
728 *
729 * Return: a valid struct thermal_cooling_device pointer on success,
730 * on failure, it returns a corresponding ERR_PTR().
731 */
732struct thermal_cooling_device *
4d753aa7 733cpufreq_cooling_register(struct cpufreq_policy *policy)
39d99cff 734{
84fe2cab 735 return __cpufreq_cooling_register(NULL, policy, 0);
39d99cff 736}
243dbd9c 737EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
02361418 738
39d99cff
EV
739/**
740 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
4d753aa7 741 * @policy: cpufreq policy
39d99cff
EV
742 *
743 * This interface function registers the cpufreq cooling device with the name
744 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
745 * cooling devices. Using this API, the cpufreq cooling device will be
746 * linked to the device tree node provided.
747 *
c36cf071
JM
748 * Using this function, the cooling device will implement the power
749 * extensions by using a simple cpu power model. The cpus must have
750 * registered their OPPs using the OPP library.
751 *
f5f263fe
VK
752 * It also takes into account, if property present in policy CPU node, the
753 * static power consumed by the cpu.
c36cf071
JM
754 *
755 * Return: a valid struct thermal_cooling_device pointer on success,
f5f263fe 756 * and NULL on failure.
c36cf071
JM
757 */
758struct thermal_cooling_device *
3ebb62ff 759of_cpufreq_cooling_register(struct cpufreq_policy *policy)
c36cf071 760{
f5f263fe
VK
761 struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
762 struct thermal_cooling_device *cdev = NULL;
763 u32 capacitance = 0;
764
765 if (!np) {
766 pr_err("cpu_cooling: OF node not available for cpu%d\n",
767 policy->cpu);
768 return NULL;
769 }
c36cf071 770
f5f263fe
VK
771 if (of_find_property(np, "#cooling-cells", NULL)) {
772 of_property_read_u32(np, "dynamic-power-coefficient",
773 &capacitance);
774
84fe2cab 775 cdev = __cpufreq_cooling_register(np, policy, capacitance);
f5f263fe 776 if (IS_ERR(cdev)) {
bf78f133 777 pr_err("cpu_cooling: cpu%d failed to register as cooling device: %ld\n",
f5f263fe
VK
778 policy->cpu, PTR_ERR(cdev));
779 cdev = NULL;
780 }
781 }
782
783 of_node_put(np);
784 return cdev;
c36cf071 785}
3ebb62ff 786EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
c36cf071 787
02361418
ADK
788/**
789 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
790 * @cdev: thermal cooling device pointer.
135266b4
EV
791 *
792 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
02361418
ADK
793 */
794void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
795{
1dea432a 796 struct cpufreq_cooling_device *cpufreq_cdev;
088db931 797 bool last;
02361418 798
50e66c7e
EV
799 if (!cdev)
800 return;
801
1dea432a 802 cpufreq_cdev = cdev->devdata;
02361418 803
ae606089 804 mutex_lock(&cooling_list_lock);
1dea432a 805 list_del(&cpufreq_cdev->node);
02361418 806 /* Unregister the notifier for the last cpufreq cooling device */
1dea432a 807 last = list_empty(&cpufreq_cdev_list);
088db931
MW
808 mutex_unlock(&cooling_list_lock);
809
810 if (last)
02361418 811 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
5fda7f68 812 CPUFREQ_POLICY_NOTIFIER);
02373d7c 813
04bdbdf9 814 thermal_cooling_device_unregister(cpufreq_cdev->cdev);
1dea432a 815 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
81ee14da 816 kfree(cpufreq_cdev->idle_time);
1dea432a
VK
817 kfree(cpufreq_cdev->freq_table);
818 kfree(cpufreq_cdev);
02361418 819}
243dbd9c 820EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);