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