2 * linux/drivers/thermal/cpu_cooling.c
4 * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
5 * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org>
7 * Copyright (C) 2014 Viresh Kumar <viresh.kumar@linaro.org>
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #include <linux/module.h>
26 #include <linux/thermal.h>
27 #include <linux/cpufreq.h>
28 #include <linux/err.h>
29 #include <linux/idr.h>
30 #include <linux/pm_opp.h>
31 #include <linux/slab.h>
32 #include <linux/cpu.h>
33 #include <linux/cpu_cooling.h>
35 #include <trace/events/thermal.h>
38 * Cooling state <-> CPUFreq frequency
40 * Cooling states are translated to frequencies throughout this driver and this
41 * is the relation between them.
43 * Highest cooling state corresponds to lowest possible frequency.
46 * level 0 --> 1st Max Freq
47 * level 1 --> 2nd Max Freq
52 * struct power_table - frequency to power conversion
53 * @frequency: frequency in KHz
56 * This structure is built when the cooling device registers and helps
57 * in translating frequency to power and viceversa.
65 * struct cpufreq_cooling_device - data for cooling device with cpufreq
66 * @id: unique integer value corresponding to each cpufreq_cooling_device
68 * @cool_dev: thermal_cooling_device pointer to keep track of the
69 * registered cooling device.
70 * @cpufreq_state: integer value representing the current state of cpufreq
72 * @clipped_freq: integer value representing the absolute value of the clipped
74 * @max_level: maximum cooling level. One less than total number of valid
75 * cpufreq frequencies.
76 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
77 * @node: list_head to link all cpufreq_cooling_device together.
78 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
79 * @time_in_idle: previous reading of the absolute time that this cpu was idle
80 * @time_in_idle_timestamp: wall time of the last invocation of
81 * get_cpu_idle_time_us()
82 * @dyn_power_table: array of struct power_table for frequency to power
83 * conversion, sorted in ascending order.
84 * @dyn_power_table_entries: number of entries in the @dyn_power_table array
85 * @cpu_dev: the first cpu_device from @allowed_cpus that has OPPs registered
86 * @plat_get_static_power: callback to calculate the static power
88 * This structure is required for keeping information of each registered
89 * cpufreq_cooling_device.
91 struct cpufreq_cooling_device {
93 struct thermal_cooling_device *cool_dev;
94 unsigned int cpufreq_state;
95 unsigned int clipped_freq;
96 unsigned int max_level;
97 unsigned int *freq_table; /* In descending order */
98 struct cpumask allowed_cpus;
99 struct list_head node;
102 u64 *time_in_idle_timestamp;
103 struct power_table *dyn_power_table;
104 int dyn_power_table_entries;
105 struct device *cpu_dev;
106 get_static_t plat_get_static_power;
108 static DEFINE_IDA(cpufreq_ida);
110 static unsigned int cpufreq_dev_count;
112 static DEFINE_MUTEX(cooling_list_lock);
113 static LIST_HEAD(cpufreq_dev_list);
115 /* Below code defines functions to be used for cpufreq as cooling device */
118 * get_level: Find the level for a particular frequency
119 * @cpufreq_dev: cpufreq_dev for which the property is required
122 * Return: level on success, THERMAL_CSTATE_INVALID on error.
124 static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_dev,
129 for (level = 0; level <= cpufreq_dev->max_level; level++) {
130 if (freq == cpufreq_dev->freq_table[level])
133 if (freq > cpufreq_dev->freq_table[level])
137 return THERMAL_CSTATE_INVALID;
141 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
142 * @cpu: cpu for which the level is required
143 * @freq: the frequency of interest
145 * This function will match the cooling level corresponding to the
146 * requested @freq and return it.
148 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
151 unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
153 struct cpufreq_cooling_device *cpufreq_dev;
155 mutex_lock(&cooling_list_lock);
156 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
157 if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
158 mutex_unlock(&cooling_list_lock);
159 return get_level(cpufreq_dev, freq);
162 mutex_unlock(&cooling_list_lock);
164 pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu);
165 return THERMAL_CSTATE_INVALID;
167 EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
170 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
171 * @nb: struct notifier_block * with callback info.
172 * @event: value showing cpufreq event for which this function invoked.
173 * @data: callback-specific data
175 * Callback to hijack the notification on cpufreq policy transition.
176 * Every time there is a change in policy, we will intercept and
177 * update the cpufreq policy with thermal constraints.
179 * Return: 0 (success)
181 static int cpufreq_thermal_notifier(struct notifier_block *nb,
182 unsigned long event, void *data)
184 struct cpufreq_policy *policy = data;
185 unsigned long clipped_freq;
186 struct cpufreq_cooling_device *cpufreq_dev;
188 if (event != CPUFREQ_ADJUST)
191 mutex_lock(&cooling_list_lock);
192 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
193 if (!cpumask_test_cpu(policy->cpu, &cpufreq_dev->allowed_cpus))
197 * policy->max is the maximum allowed frequency defined by user
198 * and clipped_freq is the maximum that thermal constraints
201 * If clipped_freq is lower than policy->max, then we need to
202 * readjust policy->max.
204 * But, if clipped_freq is greater than policy->max, we don't
205 * need to do anything.
207 clipped_freq = cpufreq_dev->clipped_freq;
209 if (policy->max > clipped_freq)
210 cpufreq_verify_within_limits(policy, 0, clipped_freq);
213 mutex_unlock(&cooling_list_lock);
219 * build_dyn_power_table() - create a dynamic power to frequency table
220 * @cpufreq_device: the cpufreq cooling device in which to store the table
221 * @capacitance: dynamic power coefficient for these cpus
223 * Build a dynamic power to frequency table for this cpu and store it
224 * in @cpufreq_device. This table will be used in cpu_power_to_freq() and
225 * cpu_freq_to_power() to convert between power and frequency
226 * efficiently. Power is stored in mW, frequency in KHz. The
227 * resulting table is in ascending order.
229 * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
230 * -ENOMEM if we run out of memory or -EAGAIN if an OPP was
231 * added/enabled while the function was executing.
233 static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_device,
236 struct power_table *power_table;
237 struct dev_pm_opp *opp;
238 struct device *dev = NULL;
239 int num_opps = 0, cpu, i, ret = 0;
242 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
243 dev = get_cpu_device(cpu);
245 dev_warn(&cpufreq_device->cool_dev->device,
246 "No cpu device for cpu %d\n", cpu);
250 num_opps = dev_pm_opp_get_opp_count(dev);
253 else if (num_opps < 0)
260 power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL);
264 for (freq = 0, i = 0;
265 opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp);
267 u32 freq_mhz, voltage_mv;
272 goto free_power_table;
275 freq_mhz = freq / 1000000;
276 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
280 * Do the multiplication with MHz and millivolt so as
283 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
284 do_div(power, 1000000000);
286 /* frequency is stored in power_table in KHz */
287 power_table[i].frequency = freq / 1000;
289 /* power is stored in mW */
290 power_table[i].power = power;
295 goto free_power_table;
298 cpufreq_device->cpu_dev = dev;
299 cpufreq_device->dyn_power_table = power_table;
300 cpufreq_device->dyn_power_table_entries = i;
310 static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_device,
314 struct power_table *pt = cpufreq_device->dyn_power_table;
316 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
317 if (freq < pt[i].frequency)
320 return pt[i - 1].power;
323 static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_device,
327 struct power_table *pt = cpufreq_device->dyn_power_table;
329 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
330 if (power < pt[i].power)
333 return pt[i - 1].frequency;
337 * get_load() - get load for a cpu since last updated
338 * @cpufreq_device: &struct cpufreq_cooling_device for this cpu
340 * @cpu_idx: index of the cpu in cpufreq_device->allowed_cpus
342 * Return: The average load of cpu @cpu in percentage since this
343 * function was last called.
345 static u32 get_load(struct cpufreq_cooling_device *cpufreq_device, int cpu,
349 u64 now, now_idle, delta_time, delta_idle;
351 now_idle = get_cpu_idle_time(cpu, &now, 0);
352 delta_idle = now_idle - cpufreq_device->time_in_idle[cpu_idx];
353 delta_time = now - cpufreq_device->time_in_idle_timestamp[cpu_idx];
355 if (delta_time <= delta_idle)
358 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
360 cpufreq_device->time_in_idle[cpu_idx] = now_idle;
361 cpufreq_device->time_in_idle_timestamp[cpu_idx] = now;
367 * get_static_power() - calculate the static power consumed by the cpus
368 * @cpufreq_device: struct &cpufreq_cooling_device for this cpu cdev
369 * @tz: thermal zone device in which we're operating
370 * @freq: frequency in KHz
371 * @power: pointer in which to store the calculated static power
373 * Calculate the static power consumed by the cpus described by
374 * @cpu_actor running at frequency @freq. This function relies on a
375 * platform specific function that should have been provided when the
376 * actor was registered. If it wasn't, the static power is assumed to
377 * be negligible. The calculated static power is stored in @power.
379 * Return: 0 on success, -E* on failure.
381 static int get_static_power(struct cpufreq_cooling_device *cpufreq_device,
382 struct thermal_zone_device *tz, unsigned long freq,
385 struct dev_pm_opp *opp;
386 unsigned long voltage;
387 struct cpumask *cpumask = &cpufreq_device->allowed_cpus;
388 unsigned long freq_hz = freq * 1000;
390 if (!cpufreq_device->plat_get_static_power ||
391 !cpufreq_device->cpu_dev) {
396 opp = dev_pm_opp_find_freq_exact(cpufreq_device->cpu_dev, freq_hz,
398 voltage = dev_pm_opp_get_voltage(opp);
402 dev_warn_ratelimited(cpufreq_device->cpu_dev,
403 "Failed to get voltage for frequency %lu: %ld\n",
404 freq_hz, IS_ERR(opp) ? PTR_ERR(opp) : 0);
408 return cpufreq_device->plat_get_static_power(cpumask, tz->passive_delay,
413 * get_dynamic_power() - calculate the dynamic power
414 * @cpufreq_device: &cpufreq_cooling_device for this cdev
415 * @freq: current frequency
417 * Return: the dynamic power consumed by the cpus described by
420 static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_device,
425 raw_cpu_power = cpu_freq_to_power(cpufreq_device, freq);
426 return (raw_cpu_power * cpufreq_device->last_load) / 100;
429 /* cpufreq cooling device callback functions are defined below */
432 * cpufreq_get_max_state - callback function to get the max cooling state.
433 * @cdev: thermal cooling device pointer.
434 * @state: fill this variable with the max cooling state.
436 * Callback for the thermal cooling device to return the cpufreq
439 * Return: 0 on success, an error code otherwise.
441 static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
442 unsigned long *state)
444 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
446 *state = cpufreq_device->max_level;
451 * cpufreq_get_cur_state - callback function to get the current cooling state.
452 * @cdev: thermal cooling device pointer.
453 * @state: fill this variable with the current cooling state.
455 * Callback for the thermal cooling device to return the cpufreq
456 * current cooling state.
458 * Return: 0 on success, an error code otherwise.
460 static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
461 unsigned long *state)
463 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
465 *state = cpufreq_device->cpufreq_state;
471 * cpufreq_set_cur_state - callback function to set the current cooling state.
472 * @cdev: thermal cooling device pointer.
473 * @state: set this variable to the current cooling state.
475 * Callback for the thermal cooling device to change the cpufreq
476 * current cooling state.
478 * Return: 0 on success, an error code otherwise.
480 static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
483 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
484 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
485 unsigned int clip_freq;
487 /* Request state should be less than max_level */
488 if (WARN_ON(state > cpufreq_device->max_level))
491 /* Check if the old cooling action is same as new cooling action */
492 if (cpufreq_device->cpufreq_state == state)
495 clip_freq = cpufreq_device->freq_table[state];
496 cpufreq_device->cpufreq_state = state;
497 cpufreq_device->clipped_freq = clip_freq;
499 cpufreq_update_policy(cpu);
505 * cpufreq_get_requested_power() - get the current power
506 * @cdev: &thermal_cooling_device pointer
507 * @tz: a valid thermal zone device pointer
508 * @power: pointer in which to store the resulting power
510 * Calculate the current power consumption of the cpus in milliwatts
511 * and store it in @power. This function should actually calculate
512 * the requested power, but it's hard to get the frequency that
513 * cpufreq would have assigned if there were no thermal limits.
514 * Instead, we calculate the current power on the assumption that the
515 * immediate future will look like the immediate past.
517 * We use the current frequency and the average load since this
518 * function was last called. In reality, there could have been
519 * multiple opps since this function was last called and that affects
520 * the load calculation. While it's not perfectly accurate, this
521 * simplification is good enough and works. REVISIT this, as more
522 * complex code may be needed if experiments show that it's not
525 * Return: 0 on success, -E* if getting the static power failed.
527 static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
528 struct thermal_zone_device *tz,
533 u32 static_power, dynamic_power, total_load = 0;
534 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
535 u32 *load_cpu = NULL;
537 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
540 * All the CPUs are offline, thus the requested power by
543 if (cpu >= nr_cpu_ids) {
548 freq = cpufreq_quick_get(cpu);
550 if (trace_thermal_power_cpu_get_power_enabled()) {
551 u32 ncpus = cpumask_weight(&cpufreq_device->allowed_cpus);
553 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
556 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
560 load = get_load(cpufreq_device, cpu, i);
565 if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
571 cpufreq_device->last_load = total_load;
573 dynamic_power = get_dynamic_power(cpufreq_device, freq);
574 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
581 trace_thermal_power_cpu_get_power(
582 &cpufreq_device->allowed_cpus,
583 freq, load_cpu, i, dynamic_power, static_power);
588 *power = static_power + dynamic_power;
593 * cpufreq_state2power() - convert a cpu cdev state to power consumed
594 * @cdev: &thermal_cooling_device pointer
595 * @tz: a valid thermal zone device pointer
596 * @state: cooling device state to be converted
597 * @power: pointer in which to store the resulting power
599 * Convert cooling device state @state into power consumption in
600 * milliwatts assuming 100% load. Store the calculated power in
603 * Return: 0 on success, -EINVAL if the cooling device state could not
604 * be converted into a frequency or other -E* if there was an error
605 * when calculating the static power.
607 static int cpufreq_state2power(struct thermal_cooling_device *cdev,
608 struct thermal_zone_device *tz,
609 unsigned long state, u32 *power)
611 unsigned int freq, num_cpus;
612 cpumask_var_t cpumask;
613 u32 static_power, dynamic_power;
615 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
617 if (!alloc_cpumask_var(&cpumask, GFP_KERNEL))
620 cpumask_and(cpumask, &cpufreq_device->allowed_cpus, cpu_online_mask);
621 num_cpus = cpumask_weight(cpumask);
623 /* None of our cpus are online, so no power */
630 freq = cpufreq_device->freq_table[state];
636 dynamic_power = cpu_freq_to_power(cpufreq_device, freq) * num_cpus;
637 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
641 *power = static_power + dynamic_power;
643 free_cpumask_var(cpumask);
648 * cpufreq_power2state() - convert power to a cooling device state
649 * @cdev: &thermal_cooling_device pointer
650 * @tz: a valid thermal zone device pointer
651 * @power: power in milliwatts to be converted
652 * @state: pointer in which to store the resulting state
654 * Calculate a cooling device state for the cpus described by @cdev
655 * that would allow them to consume at most @power mW and store it in
656 * @state. Note that this calculation depends on external factors
657 * such as the cpu load or the current static power. Calling this
658 * function with the same power as input can yield different cooling
659 * device states depending on those external factors.
661 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
662 * the calculated frequency could not be converted to a valid state.
663 * The latter should not happen unless the frequencies available to
664 * cpufreq have changed since the initialization of the cpu cooling
667 static int cpufreq_power2state(struct thermal_cooling_device *cdev,
668 struct thermal_zone_device *tz, u32 power,
669 unsigned long *state)
671 unsigned int cpu, cur_freq, target_freq;
674 u32 last_load, normalised_power, static_power;
675 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
677 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
679 /* None of our cpus are online */
680 if (cpu >= nr_cpu_ids)
683 cur_freq = cpufreq_quick_get(cpu);
684 ret = get_static_power(cpufreq_device, tz, cur_freq, &static_power);
688 dyn_power = power - static_power;
689 dyn_power = dyn_power > 0 ? dyn_power : 0;
690 last_load = cpufreq_device->last_load ?: 1;
691 normalised_power = (dyn_power * 100) / last_load;
692 target_freq = cpu_power_to_freq(cpufreq_device, normalised_power);
694 *state = cpufreq_cooling_get_level(cpu, target_freq);
695 if (*state == THERMAL_CSTATE_INVALID) {
696 dev_warn_ratelimited(&cdev->device,
697 "Failed to convert %dKHz for cpu %d into a cdev state\n",
702 trace_thermal_power_cpu_limit(&cpufreq_device->allowed_cpus,
703 target_freq, *state, power);
707 /* Bind cpufreq callbacks to thermal cooling device ops */
709 static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
710 .get_max_state = cpufreq_get_max_state,
711 .get_cur_state = cpufreq_get_cur_state,
712 .set_cur_state = cpufreq_set_cur_state,
715 static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
716 .get_max_state = cpufreq_get_max_state,
717 .get_cur_state = cpufreq_get_cur_state,
718 .set_cur_state = cpufreq_set_cur_state,
719 .get_requested_power = cpufreq_get_requested_power,
720 .state2power = cpufreq_state2power,
721 .power2state = cpufreq_power2state,
724 /* Notifier for cpufreq policy change */
725 static struct notifier_block thermal_cpufreq_notifier_block = {
726 .notifier_call = cpufreq_thermal_notifier,
729 static unsigned int find_next_max(struct cpufreq_frequency_table *table,
730 unsigned int prev_max)
732 struct cpufreq_frequency_table *pos;
733 unsigned int max = 0;
735 cpufreq_for_each_valid_entry(pos, table) {
736 if (pos->frequency > max && pos->frequency < prev_max)
737 max = pos->frequency;
744 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
745 * @np: a valid struct device_node to the cooling device device tree node
746 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
747 * Normally this should be same as cpufreq policy->related_cpus.
748 * @capacitance: dynamic power coefficient for these cpus
749 * @plat_static_func: function to calculate the static power consumed by these
752 * This interface function registers the cpufreq cooling device with the name
753 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
754 * cooling devices. It also gives the opportunity to link the cooling device
755 * with a device tree node, in order to bind it via the thermal DT code.
757 * Return: a valid struct thermal_cooling_device pointer on success,
758 * on failure, it returns a corresponding ERR_PTR().
760 static struct thermal_cooling_device *
761 __cpufreq_cooling_register(struct device_node *np,
762 const struct cpumask *clip_cpus, u32 capacitance,
763 get_static_t plat_static_func)
765 struct cpufreq_policy *policy;
766 struct thermal_cooling_device *cool_dev;
767 struct cpufreq_cooling_device *cpufreq_dev;
768 char dev_name[THERMAL_NAME_LENGTH];
769 struct cpufreq_frequency_table *pos, *table;
770 cpumask_var_t temp_mask;
771 unsigned int freq, i, num_cpus;
773 struct thermal_cooling_device_ops *cooling_ops;
775 if (!alloc_cpumask_var(&temp_mask, GFP_KERNEL))
776 return ERR_PTR(-ENOMEM);
778 cpumask_and(temp_mask, clip_cpus, cpu_online_mask);
779 policy = cpufreq_cpu_get(cpumask_first(temp_mask));
781 pr_debug("%s: CPUFreq policy not found\n", __func__);
782 cool_dev = ERR_PTR(-EPROBE_DEFER);
786 table = policy->freq_table;
788 pr_debug("%s: CPUFreq table not found\n", __func__);
789 cool_dev = ERR_PTR(-ENODEV);
793 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
795 cool_dev = ERR_PTR(-ENOMEM);
799 num_cpus = cpumask_weight(clip_cpus);
800 cpufreq_dev->time_in_idle = kcalloc(num_cpus,
801 sizeof(*cpufreq_dev->time_in_idle),
803 if (!cpufreq_dev->time_in_idle) {
804 cool_dev = ERR_PTR(-ENOMEM);
808 cpufreq_dev->time_in_idle_timestamp =
809 kcalloc(num_cpus, sizeof(*cpufreq_dev->time_in_idle_timestamp),
811 if (!cpufreq_dev->time_in_idle_timestamp) {
812 cool_dev = ERR_PTR(-ENOMEM);
813 goto free_time_in_idle;
816 /* Find max levels */
817 cpufreq_for_each_valid_entry(pos, table)
818 cpufreq_dev->max_level++;
820 cpufreq_dev->freq_table = kmalloc(sizeof(*cpufreq_dev->freq_table) *
821 cpufreq_dev->max_level, GFP_KERNEL);
822 if (!cpufreq_dev->freq_table) {
823 cool_dev = ERR_PTR(-ENOMEM);
824 goto free_time_in_idle_timestamp;
827 /* max_level is an index, not a counter */
828 cpufreq_dev->max_level--;
830 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
833 cpufreq_dev->plat_get_static_power = plat_static_func;
835 ret = build_dyn_power_table(cpufreq_dev, capacitance);
837 cool_dev = ERR_PTR(ret);
841 cooling_ops = &cpufreq_power_cooling_ops;
843 cooling_ops = &cpufreq_cooling_ops;
846 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
848 cool_dev = ERR_PTR(ret);
849 goto free_power_table;
851 cpufreq_dev->id = ret;
853 /* Fill freq-table in descending order of frequencies */
854 for (i = 0, freq = -1; i <= cpufreq_dev->max_level; i++) {
855 freq = find_next_max(table, freq);
856 cpufreq_dev->freq_table[i] = freq;
858 /* Warn for duplicate entries */
860 pr_warn("%s: table has duplicate entries\n", __func__);
862 pr_debug("%s: freq:%u KHz\n", __func__, freq);
865 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
868 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
870 if (IS_ERR(cool_dev))
873 cpufreq_dev->clipped_freq = cpufreq_dev->freq_table[0];
874 cpufreq_dev->cool_dev = cool_dev;
876 mutex_lock(&cooling_list_lock);
877 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
879 /* Register the notifier for first cpufreq cooling device */
880 if (!cpufreq_dev_count++)
881 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
882 CPUFREQ_POLICY_NOTIFIER);
883 mutex_unlock(&cooling_list_lock);
888 ida_simple_remove(&cpufreq_ida, cpufreq_dev->id);
890 kfree(cpufreq_dev->dyn_power_table);
892 kfree(cpufreq_dev->freq_table);
893 free_time_in_idle_timestamp:
894 kfree(cpufreq_dev->time_in_idle_timestamp);
896 kfree(cpufreq_dev->time_in_idle);
900 cpufreq_cpu_put(policy);
902 free_cpumask_var(temp_mask);
907 * cpufreq_cooling_register - function to create cpufreq cooling device.
908 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
910 * This interface function registers the cpufreq cooling device with the name
911 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
914 * Return: a valid struct thermal_cooling_device pointer on success,
915 * on failure, it returns a corresponding ERR_PTR().
917 struct thermal_cooling_device *
918 cpufreq_cooling_register(const struct cpumask *clip_cpus)
920 return __cpufreq_cooling_register(NULL, clip_cpus, 0, NULL);
922 EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
925 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
926 * @np: a valid struct device_node to the cooling device device tree node
927 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
929 * This interface function registers the cpufreq cooling device with the name
930 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
931 * cooling devices. Using this API, the cpufreq cooling device will be
932 * linked to the device tree node provided.
934 * Return: a valid struct thermal_cooling_device pointer on success,
935 * on failure, it returns a corresponding ERR_PTR().
937 struct thermal_cooling_device *
938 of_cpufreq_cooling_register(struct device_node *np,
939 const struct cpumask *clip_cpus)
942 return ERR_PTR(-EINVAL);
944 return __cpufreq_cooling_register(np, clip_cpus, 0, NULL);
946 EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
949 * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
950 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
951 * @capacitance: dynamic power coefficient for these cpus
952 * @plat_static_func: function to calculate the static power consumed by these
955 * This interface function registers the cpufreq cooling device with
956 * the name "thermal-cpufreq-%x". This api can support multiple
957 * instances of cpufreq cooling devices. Using this function, the
958 * cooling device will implement the power extensions by using a
959 * simple cpu power model. The cpus must have registered their OPPs
960 * using the OPP library.
962 * An optional @plat_static_func may be provided to calculate the
963 * static power consumed by these cpus. If the platform's static
964 * power consumption is unknown or negligible, make it NULL.
966 * Return: a valid struct thermal_cooling_device pointer on success,
967 * on failure, it returns a corresponding ERR_PTR().
969 struct thermal_cooling_device *
970 cpufreq_power_cooling_register(const struct cpumask *clip_cpus, u32 capacitance,
971 get_static_t plat_static_func)
973 return __cpufreq_cooling_register(NULL, clip_cpus, capacitance,
976 EXPORT_SYMBOL(cpufreq_power_cooling_register);
979 * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
980 * @np: a valid struct device_node to the cooling device device tree node
981 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
982 * @capacitance: dynamic power coefficient for these cpus
983 * @plat_static_func: function to calculate the static power consumed by these
986 * This interface function registers the cpufreq cooling device with
987 * the name "thermal-cpufreq-%x". This api can support multiple
988 * instances of cpufreq cooling devices. Using this API, the cpufreq
989 * cooling device will be linked to the device tree node provided.
990 * Using this function, the cooling device will implement the power
991 * extensions by using a simple cpu power model. The cpus must have
992 * registered their OPPs using the OPP library.
994 * An optional @plat_static_func may be provided to calculate the
995 * static power consumed by these cpus. If the platform's static
996 * power consumption is unknown or negligible, make it NULL.
998 * Return: a valid struct thermal_cooling_device pointer on success,
999 * on failure, it returns a corresponding ERR_PTR().
1001 struct thermal_cooling_device *
1002 of_cpufreq_power_cooling_register(struct device_node *np,
1003 const struct cpumask *clip_cpus,
1005 get_static_t plat_static_func)
1008 return ERR_PTR(-EINVAL);
1010 return __cpufreq_cooling_register(np, clip_cpus, capacitance,
1013 EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
1016 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
1017 * @cdev: thermal cooling device pointer.
1019 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
1021 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
1023 struct cpufreq_cooling_device *cpufreq_dev;
1028 cpufreq_dev = cdev->devdata;
1030 mutex_lock(&cooling_list_lock);
1031 /* Unregister the notifier for the last cpufreq cooling device */
1032 if (!--cpufreq_dev_count)
1033 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
1034 CPUFREQ_POLICY_NOTIFIER);
1036 list_del(&cpufreq_dev->node);
1037 mutex_unlock(&cooling_list_lock);
1039 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
1040 ida_simple_remove(&cpufreq_ida, cpufreq_dev->id);
1041 kfree(cpufreq_dev->dyn_power_table);
1042 kfree(cpufreq_dev->time_in_idle_timestamp);
1043 kfree(cpufreq_dev->time_in_idle);
1044 kfree(cpufreq_dev->freq_table);
1047 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);