thermal/cpu_cooling: rename max_freq as clipped_freq in notifier
[deliverable/linux.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.
c36cf071
JM
77 * @last_load: load measured by the latest call to cpufreq_get_actual_power()
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
abcbcc25 232 clipped_freq = cpufreq_dev->clipped_freq;
c36cf071 233
abcbcc25
VK
234 if (policy->max != clipped_freq)
235 cpufreq_verify_within_limits(policy, 0, clipped_freq);
c36cf071 236 break;
c36cf071 237 }
a24af233 238 mutex_unlock(&cooling_list_lock);
c36cf071
JM
239
240 return NOTIFY_OK;
241}
242
243/**
244 * build_dyn_power_table() - create a dynamic power to frequency table
245 * @cpufreq_device: the cpufreq cooling device in which to store the table
246 * @capacitance: dynamic power coefficient for these cpus
247 *
248 * Build a dynamic power to frequency table for this cpu and store it
249 * in @cpufreq_device. This table will be used in cpu_power_to_freq() and
250 * cpu_freq_to_power() to convert between power and frequency
251 * efficiently. Power is stored in mW, frequency in KHz. The
252 * resulting table is in ascending order.
253 *
254 * Return: 0 on success, -E* on error.
255 */
256static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_device,
257 u32 capacitance)
258{
259 struct power_table *power_table;
260 struct dev_pm_opp *opp;
261 struct device *dev = NULL;
262 int num_opps = 0, cpu, i, ret = 0;
263 unsigned long freq;
264
265 rcu_read_lock();
266
267 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
268 dev = get_cpu_device(cpu);
269 if (!dev) {
270 dev_warn(&cpufreq_device->cool_dev->device,
271 "No cpu device for cpu %d\n", cpu);
2dcd851f 272 continue;
c36cf071 273 }
2dcd851f 274
c36cf071
JM
275 num_opps = dev_pm_opp_get_opp_count(dev);
276 if (num_opps > 0) {
277 break;
278 } else if (num_opps < 0) {
279 ret = num_opps;
280 goto unlock;
281 }
282 }
02361418 283
c36cf071
JM
284 if (num_opps == 0) {
285 ret = -EINVAL;
286 goto unlock;
2dcd851f 287 }
02361418 288
c36cf071 289 power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL);
0cdf97e1
JM
290 if (!power_table) {
291 ret = -ENOMEM;
292 goto unlock;
293 }
c36cf071
JM
294
295 for (freq = 0, i = 0;
296 opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp);
297 freq++, i++) {
298 u32 freq_mhz, voltage_mv;
299 u64 power;
300
301 freq_mhz = freq / 1000000;
302 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
303
304 /*
305 * Do the multiplication with MHz and millivolt so as
306 * to not overflow.
307 */
308 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
309 do_div(power, 1000000000);
310
311 /* frequency is stored in power_table in KHz */
312 power_table[i].frequency = freq / 1000;
313
314 /* power is stored in mW */
315 power_table[i].power = power;
316 }
317
318 if (i == 0) {
319 ret = PTR_ERR(opp);
320 goto unlock;
321 }
322
323 cpufreq_device->cpu_dev = dev;
324 cpufreq_device->dyn_power_table = power_table;
325 cpufreq_device->dyn_power_table_entries = i;
326
327unlock:
328 rcu_read_unlock();
329 return ret;
330}
331
332static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_device,
333 u32 freq)
334{
335 int i;
336 struct power_table *pt = cpufreq_device->dyn_power_table;
337
338 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
339 if (freq < pt[i].frequency)
340 break;
341
342 return pt[i - 1].power;
343}
344
345static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_device,
346 u32 power)
347{
348 int i;
349 struct power_table *pt = cpufreq_device->dyn_power_table;
350
351 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
352 if (power < pt[i].power)
353 break;
354
355 return pt[i - 1].frequency;
356}
357
358/**
359 * get_load() - get load for a cpu since last updated
360 * @cpufreq_device: &struct cpufreq_cooling_device for this cpu
361 * @cpu: cpu number
362 *
363 * Return: The average load of cpu @cpu in percentage since this
364 * function was last called.
365 */
366static u32 get_load(struct cpufreq_cooling_device *cpufreq_device, int cpu)
367{
368 u32 load;
369 u64 now, now_idle, delta_time, delta_idle;
370
371 now_idle = get_cpu_idle_time(cpu, &now, 0);
372 delta_idle = now_idle - cpufreq_device->time_in_idle[cpu];
373 delta_time = now - cpufreq_device->time_in_idle_timestamp[cpu];
374
375 if (delta_time <= delta_idle)
376 load = 0;
377 else
378 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
379
380 cpufreq_device->time_in_idle[cpu] = now_idle;
381 cpufreq_device->time_in_idle_timestamp[cpu] = now;
382
383 return load;
384}
385
386/**
387 * get_static_power() - calculate the static power consumed by the cpus
388 * @cpufreq_device: struct &cpufreq_cooling_device for this cpu cdev
389 * @tz: thermal zone device in which we're operating
390 * @freq: frequency in KHz
391 * @power: pointer in which to store the calculated static power
392 *
393 * Calculate the static power consumed by the cpus described by
394 * @cpu_actor running at frequency @freq. This function relies on a
395 * platform specific function that should have been provided when the
396 * actor was registered. If it wasn't, the static power is assumed to
397 * be negligible. The calculated static power is stored in @power.
398 *
399 * Return: 0 on success, -E* on failure.
400 */
401static int get_static_power(struct cpufreq_cooling_device *cpufreq_device,
402 struct thermal_zone_device *tz, unsigned long freq,
403 u32 *power)
404{
405 struct dev_pm_opp *opp;
406 unsigned long voltage;
407 struct cpumask *cpumask = &cpufreq_device->allowed_cpus;
408 unsigned long freq_hz = freq * 1000;
409
410 if (!cpufreq_device->plat_get_static_power ||
411 !cpufreq_device->cpu_dev) {
412 *power = 0;
413 return 0;
414 }
415
416 rcu_read_lock();
417
418 opp = dev_pm_opp_find_freq_exact(cpufreq_device->cpu_dev, freq_hz,
419 true);
420 voltage = dev_pm_opp_get_voltage(opp);
421
422 rcu_read_unlock();
423
424 if (voltage == 0) {
425 dev_warn_ratelimited(cpufreq_device->cpu_dev,
426 "Failed to get voltage for frequency %lu: %ld\n",
427 freq_hz, IS_ERR(opp) ? PTR_ERR(opp) : 0);
428 return -EINVAL;
429 }
430
431 return cpufreq_device->plat_get_static_power(cpumask, tz->passive_delay,
432 voltage, power);
433}
434
435/**
436 * get_dynamic_power() - calculate the dynamic power
437 * @cpufreq_device: &cpufreq_cooling_device for this cdev
438 * @freq: current frequency
439 *
440 * Return: the dynamic power consumed by the cpus described by
441 * @cpufreq_device.
442 */
443static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_device,
444 unsigned long freq)
445{
446 u32 raw_cpu_power;
447
448 raw_cpu_power = cpu_freq_to_power(cpufreq_device, freq);
449 return (raw_cpu_power * cpufreq_device->last_load) / 100;
02361418
ADK
450}
451
1b9e3526 452/* cpufreq cooling device callback functions are defined below */
02361418
ADK
453
454/**
455 * cpufreq_get_max_state - callback function to get the max cooling state.
456 * @cdev: thermal cooling device pointer.
457 * @state: fill this variable with the max cooling state.
62c00421
EV
458 *
459 * Callback for the thermal cooling device to return the cpufreq
460 * max cooling state.
461 *
462 * Return: 0 on success, an error code otherwise.
02361418
ADK
463 */
464static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
465 unsigned long *state)
466{
160b7d80 467 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
9c51b05a 468
dcc6c7fd
VK
469 *state = cpufreq_device->max_level;
470 return 0;
02361418
ADK
471}
472
473/**
474 * cpufreq_get_cur_state - callback function to get the current cooling state.
475 * @cdev: thermal cooling device pointer.
476 * @state: fill this variable with the current cooling state.
3672552d
EV
477 *
478 * Callback for the thermal cooling device to return the cpufreq
479 * current cooling state.
480 *
481 * Return: 0 on success, an error code otherwise.
02361418
ADK
482 */
483static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
484 unsigned long *state)
485{
160b7d80 486 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
02361418 487
160b7d80 488 *state = cpufreq_device->cpufreq_state;
79491e53 489
160b7d80 490 return 0;
02361418
ADK
491}
492
493/**
494 * cpufreq_set_cur_state - callback function to set the current cooling state.
495 * @cdev: thermal cooling device pointer.
496 * @state: set this variable to the current cooling state.
56e05fdb
EV
497 *
498 * Callback for the thermal cooling device to change the cpufreq
499 * current cooling state.
500 *
501 * Return: 0 on success, an error code otherwise.
02361418
ADK
502 */
503static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
504 unsigned long state)
505{
160b7d80 506 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
5194fe46
VK
507 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
508 unsigned int clip_freq;
4843c4a1
VK
509
510 /* Request state should be less than max_level */
511 if (WARN_ON(state > cpufreq_device->max_level))
512 return -EINVAL;
5194fe46
VK
513
514 /* Check if the old cooling action is same as new cooling action */
515 if (cpufreq_device->cpufreq_state == state)
516 return 0;
02361418 517
4843c4a1 518 clip_freq = cpufreq_device->freq_table[state];
5194fe46 519 cpufreq_device->cpufreq_state = state;
59f0d218 520 cpufreq_device->clipped_freq = clip_freq;
5194fe46
VK
521
522 cpufreq_update_policy(cpu);
523
524 return 0;
02361418
ADK
525}
526
c36cf071
JM
527/**
528 * cpufreq_get_requested_power() - get the current power
529 * @cdev: &thermal_cooling_device pointer
530 * @tz: a valid thermal zone device pointer
531 * @power: pointer in which to store the resulting power
532 *
533 * Calculate the current power consumption of the cpus in milliwatts
534 * and store it in @power. This function should actually calculate
535 * the requested power, but it's hard to get the frequency that
536 * cpufreq would have assigned if there were no thermal limits.
537 * Instead, we calculate the current power on the assumption that the
538 * immediate future will look like the immediate past.
539 *
540 * We use the current frequency and the average load since this
541 * function was last called. In reality, there could have been
542 * multiple opps since this function was last called and that affects
543 * the load calculation. While it's not perfectly accurate, this
544 * simplification is good enough and works. REVISIT this, as more
545 * complex code may be needed if experiments show that it's not
546 * accurate enough.
547 *
548 * Return: 0 on success, -E* if getting the static power failed.
549 */
550static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
551 struct thermal_zone_device *tz,
552 u32 *power)
553{
554 unsigned long freq;
6828a471 555 int i = 0, cpu, ret;
c36cf071
JM
556 u32 static_power, dynamic_power, total_load = 0;
557 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
6828a471 558 u32 *load_cpu = NULL;
c36cf071 559
dd658e02
KS
560 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
561
562 /*
563 * All the CPUs are offline, thus the requested power by
564 * the cdev is 0
565 */
566 if (cpu >= nr_cpu_ids) {
567 *power = 0;
568 return 0;
569 }
570
571 freq = cpufreq_quick_get(cpu);
c36cf071 572
6828a471
JM
573 if (trace_thermal_power_cpu_get_power_enabled()) {
574 u32 ncpus = cpumask_weight(&cpufreq_device->allowed_cpus);
575
576 load_cpu = devm_kcalloc(&cdev->device, ncpus, sizeof(*load_cpu),
577 GFP_KERNEL);
578 }
579
c36cf071
JM
580 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
581 u32 load;
582
583 if (cpu_online(cpu))
584 load = get_load(cpufreq_device, cpu);
585 else
586 load = 0;
587
588 total_load += load;
6828a471
JM
589 if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
590 load_cpu[i] = load;
591
592 i++;
c36cf071
JM
593 }
594
595 cpufreq_device->last_load = total_load;
596
597 dynamic_power = get_dynamic_power(cpufreq_device, freq);
598 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
6828a471
JM
599 if (ret) {
600 if (load_cpu)
601 devm_kfree(&cdev->device, load_cpu);
c36cf071 602 return ret;
6828a471
JM
603 }
604
605 if (load_cpu) {
606 trace_thermal_power_cpu_get_power(
607 &cpufreq_device->allowed_cpus,
608 freq, load_cpu, i, dynamic_power, static_power);
609
610 devm_kfree(&cdev->device, load_cpu);
611 }
c36cf071
JM
612
613 *power = static_power + dynamic_power;
614 return 0;
615}
616
617/**
618 * cpufreq_state2power() - convert a cpu cdev state to power consumed
619 * @cdev: &thermal_cooling_device pointer
620 * @tz: a valid thermal zone device pointer
621 * @state: cooling device state to be converted
622 * @power: pointer in which to store the resulting power
623 *
624 * Convert cooling device state @state into power consumption in
625 * milliwatts assuming 100% load. Store the calculated power in
626 * @power.
627 *
628 * Return: 0 on success, -EINVAL if the cooling device state could not
629 * be converted into a frequency or other -E* if there was an error
630 * when calculating the static power.
631 */
632static int cpufreq_state2power(struct thermal_cooling_device *cdev,
633 struct thermal_zone_device *tz,
634 unsigned long state, u32 *power)
635{
636 unsigned int freq, num_cpus;
637 cpumask_t cpumask;
638 u32 static_power, dynamic_power;
639 int ret;
640 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
641
642 cpumask_and(&cpumask, &cpufreq_device->allowed_cpus, cpu_online_mask);
643 num_cpus = cpumask_weight(&cpumask);
644
645 /* None of our cpus are online, so no power */
646 if (num_cpus == 0) {
647 *power = 0;
648 return 0;
649 }
650
651 freq = cpufreq_device->freq_table[state];
652 if (!freq)
653 return -EINVAL;
654
655 dynamic_power = cpu_freq_to_power(cpufreq_device, freq) * num_cpus;
656 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
657 if (ret)
658 return ret;
659
660 *power = static_power + dynamic_power;
661 return 0;
662}
663
664/**
665 * cpufreq_power2state() - convert power to a cooling device state
666 * @cdev: &thermal_cooling_device pointer
667 * @tz: a valid thermal zone device pointer
668 * @power: power in milliwatts to be converted
669 * @state: pointer in which to store the resulting state
670 *
671 * Calculate a cooling device state for the cpus described by @cdev
672 * that would allow them to consume at most @power mW and store it in
673 * @state. Note that this calculation depends on external factors
674 * such as the cpu load or the current static power. Calling this
675 * function with the same power as input can yield different cooling
676 * device states depending on those external factors.
677 *
678 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
679 * the calculated frequency could not be converted to a valid state.
680 * The latter should not happen unless the frequencies available to
681 * cpufreq have changed since the initialization of the cpu cooling
682 * device.
683 */
684static int cpufreq_power2state(struct thermal_cooling_device *cdev,
685 struct thermal_zone_device *tz, u32 power,
686 unsigned long *state)
687{
688 unsigned int cpu, cur_freq, target_freq;
689 int ret;
690 s32 dyn_power;
691 u32 last_load, normalised_power, static_power;
692 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
693
694 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
695
696 /* None of our cpus are online */
697 if (cpu >= nr_cpu_ids)
698 return -ENODEV;
699
700 cur_freq = cpufreq_quick_get(cpu);
701 ret = get_static_power(cpufreq_device, tz, cur_freq, &static_power);
702 if (ret)
703 return ret;
704
705 dyn_power = power - static_power;
706 dyn_power = dyn_power > 0 ? dyn_power : 0;
707 last_load = cpufreq_device->last_load ?: 1;
708 normalised_power = (dyn_power * 100) / last_load;
709 target_freq = cpu_power_to_freq(cpufreq_device, normalised_power);
710
711 *state = cpufreq_cooling_get_level(cpu, target_freq);
712 if (*state == THERMAL_CSTATE_INVALID) {
713 dev_warn_ratelimited(&cdev->device,
714 "Failed to convert %dKHz for cpu %d into a cdev state\n",
715 target_freq, cpu);
716 return -EINVAL;
717 }
718
6828a471
JM
719 trace_thermal_power_cpu_limit(&cpufreq_device->allowed_cpus,
720 target_freq, *state, power);
c36cf071
JM
721 return 0;
722}
723
02361418 724/* Bind cpufreq callbacks to thermal cooling device ops */
c36cf071 725static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
02361418
ADK
726 .get_max_state = cpufreq_get_max_state,
727 .get_cur_state = cpufreq_get_cur_state,
728 .set_cur_state = cpufreq_set_cur_state,
729};
730
731/* Notifier for cpufreq policy change */
732static struct notifier_block thermal_cpufreq_notifier_block = {
733 .notifier_call = cpufreq_thermal_notifier,
734};
735
f6859014
VK
736static unsigned int find_next_max(struct cpufreq_frequency_table *table,
737 unsigned int prev_max)
738{
739 struct cpufreq_frequency_table *pos;
740 unsigned int max = 0;
741
742 cpufreq_for_each_valid_entry(pos, table) {
743 if (pos->frequency > max && pos->frequency < prev_max)
744 max = pos->frequency;
745 }
746
747 return max;
748}
749
02361418 750/**
39d99cff
EV
751 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
752 * @np: a valid struct device_node to the cooling device device tree node
02361418 753 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
405fb825 754 * Normally this should be same as cpufreq policy->related_cpus.
c36cf071
JM
755 * @capacitance: dynamic power coefficient for these cpus
756 * @plat_static_func: function to calculate the static power consumed by these
757 * cpus (optional)
12cb08ba
EV
758 *
759 * This interface function registers the cpufreq cooling device with the name
760 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
39d99cff
EV
761 * cooling devices. It also gives the opportunity to link the cooling device
762 * with a device tree node, in order to bind it via the thermal DT code.
12cb08ba
EV
763 *
764 * Return: a valid struct thermal_cooling_device pointer on success,
765 * on failure, it returns a corresponding ERR_PTR().
02361418 766 */
39d99cff
EV
767static struct thermal_cooling_device *
768__cpufreq_cooling_register(struct device_node *np,
c36cf071
JM
769 const struct cpumask *clip_cpus, u32 capacitance,
770 get_static_t plat_static_func)
02361418
ADK
771{
772 struct thermal_cooling_device *cool_dev;
5d3bdb89 773 struct cpufreq_cooling_device *cpufreq_dev;
02361418 774 char dev_name[THERMAL_NAME_LENGTH];
dcc6c7fd 775 struct cpufreq_frequency_table *pos, *table;
c36cf071 776 unsigned int freq, i, num_cpus;
405fb825 777 int ret;
02361418 778
dcc6c7fd
VK
779 table = cpufreq_frequency_get_table(cpumask_first(clip_cpus));
780 if (!table) {
0f1be51c
EV
781 pr_debug("%s: CPUFreq table not found\n", __func__);
782 return ERR_PTR(-EPROBE_DEFER);
02361418 783 }
0f1be51c 784
98d522f0 785 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
02361418
ADK
786 if (!cpufreq_dev)
787 return ERR_PTR(-ENOMEM);
788
c36cf071
JM
789 num_cpus = cpumask_weight(clip_cpus);
790 cpufreq_dev->time_in_idle = kcalloc(num_cpus,
791 sizeof(*cpufreq_dev->time_in_idle),
792 GFP_KERNEL);
793 if (!cpufreq_dev->time_in_idle) {
794 cool_dev = ERR_PTR(-ENOMEM);
795 goto free_cdev;
796 }
797
798 cpufreq_dev->time_in_idle_timestamp =
799 kcalloc(num_cpus, sizeof(*cpufreq_dev->time_in_idle_timestamp),
800 GFP_KERNEL);
801 if (!cpufreq_dev->time_in_idle_timestamp) {
802 cool_dev = ERR_PTR(-ENOMEM);
803 goto free_time_in_idle;
804 }
805
dcc6c7fd
VK
806 /* Find max levels */
807 cpufreq_for_each_valid_entry(pos, table)
808 cpufreq_dev->max_level++;
809
f6859014
VK
810 cpufreq_dev->freq_table = kmalloc(sizeof(*cpufreq_dev->freq_table) *
811 cpufreq_dev->max_level, GFP_KERNEL);
812 if (!cpufreq_dev->freq_table) {
f6859014 813 cool_dev = ERR_PTR(-ENOMEM);
c36cf071 814 goto free_time_in_idle_timestamp;
f6859014
VK
815 }
816
dcc6c7fd
VK
817 /* max_level is an index, not a counter */
818 cpufreq_dev->max_level--;
819
02361418
ADK
820 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
821
c36cf071
JM
822 if (capacitance) {
823 cpufreq_cooling_ops.get_requested_power =
824 cpufreq_get_requested_power;
825 cpufreq_cooling_ops.state2power = cpufreq_state2power;
826 cpufreq_cooling_ops.power2state = cpufreq_power2state;
827 cpufreq_dev->plat_get_static_power = plat_static_func;
828
829 ret = build_dyn_power_table(cpufreq_dev, capacitance);
830 if (ret) {
831 cool_dev = ERR_PTR(ret);
832 goto free_table;
833 }
834 }
835
02361418
ADK
836 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
837 if (ret) {
730abe06 838 cool_dev = ERR_PTR(ret);
f6859014 839 goto free_table;
02361418
ADK
840 }
841
99871a71
EV
842 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
843 cpufreq_dev->id);
02361418 844
39d99cff
EV
845 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
846 &cpufreq_cooling_ops);
730abe06
VK
847 if (IS_ERR(cool_dev))
848 goto remove_idr;
849
f6859014
VK
850 /* Fill freq-table in descending order of frequencies */
851 for (i = 0, freq = -1; i <= cpufreq_dev->max_level; i++) {
852 freq = find_next_max(table, freq);
853 cpufreq_dev->freq_table[i] = freq;
854
855 /* Warn for duplicate entries */
856 if (!freq)
857 pr_warn("%s: table has duplicate entries\n", __func__);
858 else
859 pr_debug("%s: freq:%u KHz\n", __func__, freq);
02361418 860 }
f6859014 861
59f0d218 862 cpufreq_dev->clipped_freq = cpufreq_dev->freq_table[0];
02361418 863 cpufreq_dev->cool_dev = cool_dev;
92e615ec 864
02361418 865 mutex_lock(&cooling_cpufreq_lock);
02361418 866
02373d7c
RK
867 mutex_lock(&cooling_list_lock);
868 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
869 mutex_unlock(&cooling_list_lock);
870
02361418 871 /* Register the notifier for first cpufreq cooling device */
02373d7c 872 if (!cpufreq_dev_count++)
02361418 873 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
5fda7f68 874 CPUFREQ_POLICY_NOTIFIER);
02361418 875 mutex_unlock(&cooling_cpufreq_lock);
79491e53 876
730abe06
VK
877 return cool_dev;
878
879remove_idr:
880 release_idr(&cpufreq_idr, cpufreq_dev->id);
f6859014
VK
881free_table:
882 kfree(cpufreq_dev->freq_table);
c36cf071
JM
883free_time_in_idle_timestamp:
884 kfree(cpufreq_dev->time_in_idle_timestamp);
885free_time_in_idle:
886 kfree(cpufreq_dev->time_in_idle);
730abe06
VK
887free_cdev:
888 kfree(cpufreq_dev);
889
02361418
ADK
890 return cool_dev;
891}
39d99cff
EV
892
893/**
894 * cpufreq_cooling_register - function to create cpufreq cooling device.
895 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
896 *
897 * This interface function registers the cpufreq cooling device with the name
898 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
899 * cooling devices.
900 *
901 * Return: a valid struct thermal_cooling_device pointer on success,
902 * on failure, it returns a corresponding ERR_PTR().
903 */
904struct thermal_cooling_device *
905cpufreq_cooling_register(const struct cpumask *clip_cpus)
906{
c36cf071 907 return __cpufreq_cooling_register(NULL, clip_cpus, 0, NULL);
39d99cff 908}
243dbd9c 909EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
02361418 910
39d99cff
EV
911/**
912 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
913 * @np: a valid struct device_node to the cooling device device tree node
914 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
915 *
916 * This interface function registers the cpufreq cooling device with the name
917 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
918 * cooling devices. Using this API, the cpufreq cooling device will be
919 * linked to the device tree node provided.
920 *
921 * Return: a valid struct thermal_cooling_device pointer on success,
922 * on failure, it returns a corresponding ERR_PTR().
923 */
924struct thermal_cooling_device *
925of_cpufreq_cooling_register(struct device_node *np,
926 const struct cpumask *clip_cpus)
927{
928 if (!np)
929 return ERR_PTR(-EINVAL);
930
c36cf071 931 return __cpufreq_cooling_register(np, clip_cpus, 0, NULL);
39d99cff
EV
932}
933EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
934
c36cf071
JM
935/**
936 * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
937 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
938 * @capacitance: dynamic power coefficient for these cpus
939 * @plat_static_func: function to calculate the static power consumed by these
940 * cpus (optional)
941 *
942 * This interface function registers the cpufreq cooling device with
943 * the name "thermal-cpufreq-%x". This api can support multiple
944 * instances of cpufreq cooling devices. Using this function, the
945 * cooling device will implement the power extensions by using a
946 * simple cpu power model. The cpus must have registered their OPPs
947 * using the OPP library.
948 *
949 * An optional @plat_static_func may be provided to calculate the
950 * static power consumed by these cpus. If the platform's static
951 * power consumption is unknown or negligible, make it NULL.
952 *
953 * Return: a valid struct thermal_cooling_device pointer on success,
954 * on failure, it returns a corresponding ERR_PTR().
955 */
956struct thermal_cooling_device *
957cpufreq_power_cooling_register(const struct cpumask *clip_cpus, u32 capacitance,
958 get_static_t plat_static_func)
959{
960 return __cpufreq_cooling_register(NULL, clip_cpus, capacitance,
961 plat_static_func);
962}
963EXPORT_SYMBOL(cpufreq_power_cooling_register);
964
965/**
966 * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
967 * @np: a valid struct device_node to the cooling device device tree node
968 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
969 * @capacitance: dynamic power coefficient for these cpus
970 * @plat_static_func: function to calculate the static power consumed by these
971 * cpus (optional)
972 *
973 * This interface function registers the cpufreq cooling device with
974 * the name "thermal-cpufreq-%x". This api can support multiple
975 * instances of cpufreq cooling devices. Using this API, the cpufreq
976 * cooling device will be linked to the device tree node provided.
977 * Using this function, the cooling device will implement the power
978 * extensions by using a simple cpu power model. The cpus must have
979 * registered their OPPs using the OPP library.
980 *
981 * An optional @plat_static_func may be provided to calculate the
982 * static power consumed by these cpus. If the platform's static
983 * power consumption is unknown or negligible, make it NULL.
984 *
985 * Return: a valid struct thermal_cooling_device pointer on success,
986 * on failure, it returns a corresponding ERR_PTR().
987 */
988struct thermal_cooling_device *
989of_cpufreq_power_cooling_register(struct device_node *np,
990 const struct cpumask *clip_cpus,
991 u32 capacitance,
992 get_static_t plat_static_func)
993{
994 if (!np)
995 return ERR_PTR(-EINVAL);
996
997 return __cpufreq_cooling_register(np, clip_cpus, capacitance,
998 plat_static_func);
999}
1000EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
1001
02361418
ADK
1002/**
1003 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
1004 * @cdev: thermal cooling device pointer.
135266b4
EV
1005 *
1006 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
02361418
ADK
1007 */
1008void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
1009{
50e66c7e 1010 struct cpufreq_cooling_device *cpufreq_dev;
02361418 1011
50e66c7e
EV
1012 if (!cdev)
1013 return;
1014
1015 cpufreq_dev = cdev->devdata;
02361418
ADK
1016
1017 /* Unregister the notifier for the last cpufreq cooling device */
02373d7c
RK
1018 mutex_lock(&cooling_cpufreq_lock);
1019 if (!--cpufreq_dev_count)
02361418 1020 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
5fda7f68 1021 CPUFREQ_POLICY_NOTIFIER);
02373d7c
RK
1022
1023 mutex_lock(&cooling_list_lock);
1024 list_del(&cpufreq_dev->node);
1025 mutex_unlock(&cooling_list_lock);
1026
02361418 1027 mutex_unlock(&cooling_cpufreq_lock);
160b7d80 1028
02361418
ADK
1029 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
1030 release_idr(&cpufreq_idr, cpufreq_dev->id);
c36cf071
JM
1031 kfree(cpufreq_dev->time_in_idle_timestamp);
1032 kfree(cpufreq_dev->time_in_idle);
f6859014 1033 kfree(cpufreq_dev->freq_table);
02361418
ADK
1034 kfree(cpufreq_dev);
1035}
243dbd9c 1036EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);
This page took 0.191849 seconds and 5 git commands to generate.