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