1 // SPDX-License-Identifier: GPL-2.0
3 * linux/drivers/thermal/cpu_cooling.c
5 * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
7 * Copyright (C) 2012-2018 Linaro Limited.
13 #include <linux/module.h>
14 #include <linux/thermal.h>
15 #include <linux/cpufreq.h>
16 #include <linux/err.h>
17 #include <linux/idr.h>
18 #include <linux/pm_opp.h>
19 #include <linux/slab.h>
20 #include <linux/cpu.h>
21 #include <linux/cpu_cooling.h>
23 #include <trace/events/thermal.h>
26 * Cooling state <-> CPUFreq frequency
28 * Cooling states are translated to frequencies throughout this driver and this
29 * is the relation between them.
31 * Highest cooling state corresponds to lowest possible frequency.
34 * level 0 --> 1st Max Freq
35 * level 1 --> 2nd Max Freq
40 * struct freq_table - frequency table along with power entries
41 * @frequency: frequency in KHz
44 * This structure is built when the cooling device registers and helps
45 * in translating frequency to power and vice versa.
53 * struct time_in_idle - Idle time stats
54 * @time: previous reading of the absolute time that this cpu was idle
55 * @timestamp: wall time of the last invocation of get_cpu_idle_time_us()
63 * struct cpufreq_cooling_device - data for cooling device with cpufreq
64 * @id: unique integer value corresponding to each cpufreq_cooling_device
66 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
67 * @cpufreq_state: integer value representing the current state of cpufreq
69 * @clipped_freq: integer value representing the absolute value of the clipped
71 * @max_level: maximum cooling level. One less than total number of valid
72 * cpufreq frequencies.
73 * @freq_table: Freq table in descending order of frequencies
74 * @cdev: thermal_cooling_device pointer to keep track of the
75 * registered cooling device.
76 * @policy: cpufreq policy.
77 * @node: list_head to link all cpufreq_cooling_device together.
78 * @idle_time: idle time stats
80 * This structure is required for keeping information of each registered
81 * cpufreq_cooling_device.
83 struct cpufreq_cooling_device {
86 unsigned int cpufreq_state;
87 unsigned int clipped_freq;
88 unsigned int max_level;
89 struct freq_table *freq_table; /* In descending order */
90 struct cpufreq_policy *policy;
91 struct list_head node;
92 struct time_in_idle *idle_time;
95 static DEFINE_IDA(cpufreq_ida);
96 static DEFINE_MUTEX(cooling_list_lock);
97 static LIST_HEAD(cpufreq_cdev_list);
99 /* Below code defines functions to be used for cpufreq as cooling device */
102 * get_level: Find the level for a particular frequency
103 * @cpufreq_cdev: cpufreq_cdev for which the property is required
106 * Return: level corresponding to the frequency.
108 static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
111 struct freq_table *freq_table = cpufreq_cdev->freq_table;
114 for (level = 1; level <= cpufreq_cdev->max_level; level++)
115 if (freq > freq_table[level].frequency)
122 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
123 * @nb: struct notifier_block * with callback info.
124 * @event: value showing cpufreq event for which this function invoked.
125 * @data: callback-specific data
127 * Callback to hijack the notification on cpufreq policy transition.
128 * Every time there is a change in policy, we will intercept and
129 * update the cpufreq policy with thermal constraints.
131 * Return: 0 (success)
133 static int cpufreq_thermal_notifier(struct notifier_block *nb,
134 unsigned long event, void *data)
136 struct cpufreq_policy *policy = data;
137 unsigned long clipped_freq;
138 struct cpufreq_cooling_device *cpufreq_cdev;
140 if (event != CPUFREQ_ADJUST)
143 mutex_lock(&cooling_list_lock);
144 list_for_each_entry(cpufreq_cdev, &cpufreq_cdev_list, node) {
146 * A new copy of the policy is sent to the notifier and can't
147 * compare that directly.
149 if (policy->cpu != cpufreq_cdev->policy->cpu)
153 * policy->max is the maximum allowed frequency defined by user
154 * and clipped_freq is the maximum that thermal constraints
157 * If clipped_freq is lower than policy->max, then we need to
158 * readjust policy->max.
160 * But, if clipped_freq is greater than policy->max, we don't
161 * need to do anything.
163 clipped_freq = cpufreq_cdev->clipped_freq;
165 if (policy->max > clipped_freq)
166 cpufreq_verify_within_limits(policy, 0, clipped_freq);
169 mutex_unlock(&cooling_list_lock);
175 * update_freq_table() - Update the freq table with power numbers
176 * @cpufreq_cdev: the cpufreq cooling device in which to update the table
177 * @capacitance: dynamic power coefficient for these cpus
179 * Update the freq table with power numbers. This table will be used in
180 * cpu_power_to_freq() and cpu_freq_to_power() to convert between power and
181 * frequency efficiently. Power is stored in mW, frequency in KHz. The
182 * resulting table is in descending order.
184 * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
185 * or -ENOMEM if we run out of memory.
187 static int update_freq_table(struct cpufreq_cooling_device *cpufreq_cdev,
190 struct freq_table *freq_table = cpufreq_cdev->freq_table;
191 struct dev_pm_opp *opp;
192 struct device *dev = NULL;
193 int num_opps = 0, cpu = cpufreq_cdev->policy->cpu, i;
195 dev = get_cpu_device(cpu);
196 if (unlikely(!dev)) {
197 pr_warn("No cpu device for cpu %d\n", cpu);
201 num_opps = dev_pm_opp_get_opp_count(dev);
206 * The cpufreq table is also built from the OPP table and so the count
209 if (num_opps != cpufreq_cdev->max_level + 1) {
210 dev_warn(dev, "Number of OPPs not matching with max_levels\n");
214 for (i = 0; i <= cpufreq_cdev->max_level; i++) {
215 unsigned long freq = freq_table[i].frequency * 1000;
216 u32 freq_mhz = freq_table[i].frequency / 1000;
221 * Find ceil frequency as 'freq' may be slightly lower than OPP
222 * freq due to truncation while converting to kHz.
224 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
226 dev_err(dev, "failed to get opp for %lu frequency\n",
231 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
235 * Do the multiplication with MHz and millivolt so as
238 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
239 do_div(power, 1000000000);
241 /* power is stored in mW */
242 freq_table[i].power = power;
248 static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
252 struct freq_table *freq_table = cpufreq_cdev->freq_table;
254 for (i = 1; i <= cpufreq_cdev->max_level; i++)
255 if (freq > freq_table[i].frequency)
258 return freq_table[i - 1].power;
261 static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
265 struct freq_table *freq_table = cpufreq_cdev->freq_table;
267 for (i = 1; i <= cpufreq_cdev->max_level; i++)
268 if (power > freq_table[i].power)
271 return freq_table[i - 1].frequency;
275 * get_load() - get load for a cpu since last updated
276 * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu
278 * @cpu_idx: index of the cpu in time_in_idle*
280 * Return: The average load of cpu @cpu in percentage since this
281 * function was last called.
283 static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
287 u64 now, now_idle, delta_time, delta_idle;
288 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
290 now_idle = get_cpu_idle_time(cpu, &now, 0);
291 delta_idle = now_idle - idle_time->time;
292 delta_time = now - idle_time->timestamp;
294 if (delta_time <= delta_idle)
297 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
299 idle_time->time = now_idle;
300 idle_time->timestamp = now;
306 * get_dynamic_power() - calculate the dynamic power
307 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
308 * @freq: current frequency
310 * Return: the dynamic power consumed by the cpus described by
313 static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
318 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
319 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
322 /* cpufreq cooling device callback functions are defined below */
325 * cpufreq_get_max_state - callback function to get the max cooling state.
326 * @cdev: thermal cooling device pointer.
327 * @state: fill this variable with the max cooling state.
329 * Callback for the thermal cooling device to return the cpufreq
332 * Return: 0 on success, an error code otherwise.
334 static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
335 unsigned long *state)
337 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
339 *state = cpufreq_cdev->max_level;
344 * cpufreq_get_cur_state - callback function to get the current cooling state.
345 * @cdev: thermal cooling device pointer.
346 * @state: fill this variable with the current cooling state.
348 * Callback for the thermal cooling device to return the cpufreq
349 * current cooling state.
351 * Return: 0 on success, an error code otherwise.
353 static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
354 unsigned long *state)
356 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
358 *state = cpufreq_cdev->cpufreq_state;
364 * cpufreq_set_cur_state - callback function to set the current cooling state.
365 * @cdev: thermal cooling device pointer.
366 * @state: set this variable to the current cooling state.
368 * Callback for the thermal cooling device to change the cpufreq
369 * current cooling state.
371 * Return: 0 on success, an error code otherwise.
373 static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
376 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
377 unsigned int clip_freq;
379 /* Request state should be less than max_level */
380 if (WARN_ON(state > cpufreq_cdev->max_level))
383 /* Check if the old cooling action is same as new cooling action */
384 if (cpufreq_cdev->cpufreq_state == state)
387 clip_freq = cpufreq_cdev->freq_table[state].frequency;
388 cpufreq_cdev->cpufreq_state = state;
389 cpufreq_cdev->clipped_freq = clip_freq;
391 cpufreq_update_policy(cpufreq_cdev->policy->cpu);
397 * cpufreq_get_requested_power() - get the current power
398 * @cdev: &thermal_cooling_device pointer
399 * @tz: a valid thermal zone device pointer
400 * @power: pointer in which to store the resulting power
402 * Calculate the current power consumption of the cpus in milliwatts
403 * and store it in @power. This function should actually calculate
404 * the requested power, but it's hard to get the frequency that
405 * cpufreq would have assigned if there were no thermal limits.
406 * Instead, we calculate the current power on the assumption that the
407 * immediate future will look like the immediate past.
409 * We use the current frequency and the average load since this
410 * function was last called. In reality, there could have been
411 * multiple opps since this function was last called and that affects
412 * the load calculation. While it's not perfectly accurate, this
413 * simplification is good enough and works. REVISIT this, as more
414 * complex code may be needed if experiments show that it's not
417 * Return: 0 on success, -E* if getting the static power failed.
419 static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
420 struct thermal_zone_device *tz,
426 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
427 struct cpufreq_policy *policy = cpufreq_cdev->policy;
428 u32 *load_cpu = NULL;
430 freq = cpufreq_quick_get(policy->cpu);
432 if (trace_thermal_power_cpu_get_power_enabled()) {
433 u32 ncpus = cpumask_weight(policy->related_cpus);
435 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
438 for_each_cpu(cpu, policy->related_cpus) {
442 load = get_load(cpufreq_cdev, cpu, i);
453 cpufreq_cdev->last_load = total_load;
455 *power = get_dynamic_power(cpufreq_cdev, freq);
458 trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
459 load_cpu, i, *power);
468 * cpufreq_state2power() - convert a cpu cdev state to power consumed
469 * @cdev: &thermal_cooling_device pointer
470 * @tz: a valid thermal zone device pointer
471 * @state: cooling device state to be converted
472 * @power: pointer in which to store the resulting power
474 * Convert cooling device state @state into power consumption in
475 * milliwatts assuming 100% load. Store the calculated power in
478 * Return: 0 on success, -EINVAL if the cooling device state could not
479 * be converted into a frequency or other -E* if there was an error
480 * when calculating the static power.
482 static int cpufreq_state2power(struct thermal_cooling_device *cdev,
483 struct thermal_zone_device *tz,
484 unsigned long state, u32 *power)
486 unsigned int freq, num_cpus;
487 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
489 /* Request state should be less than max_level */
490 if (WARN_ON(state > cpufreq_cdev->max_level))
493 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
495 freq = cpufreq_cdev->freq_table[state].frequency;
496 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
502 * cpufreq_power2state() - convert power to a cooling device state
503 * @cdev: &thermal_cooling_device pointer
504 * @tz: a valid thermal zone device pointer
505 * @power: power in milliwatts to be converted
506 * @state: pointer in which to store the resulting state
508 * Calculate a cooling device state for the cpus described by @cdev
509 * that would allow them to consume at most @power mW and store it in
510 * @state. Note that this calculation depends on external factors
511 * such as the cpu load or the current static power. Calling this
512 * function with the same power as input can yield different cooling
513 * device states depending on those external factors.
515 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
516 * the calculated frequency could not be converted to a valid state.
517 * The latter should not happen unless the frequencies available to
518 * cpufreq have changed since the initialization of the cpu cooling
521 static int cpufreq_power2state(struct thermal_cooling_device *cdev,
522 struct thermal_zone_device *tz, u32 power,
523 unsigned long *state)
525 unsigned int target_freq;
526 u32 last_load, normalised_power;
527 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
528 struct cpufreq_policy *policy = cpufreq_cdev->policy;
530 last_load = cpufreq_cdev->last_load ?: 1;
531 normalised_power = (power * 100) / last_load;
532 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
534 *state = get_level(cpufreq_cdev, target_freq);
535 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
540 /* Bind cpufreq callbacks to thermal cooling device ops */
542 static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
543 .get_max_state = cpufreq_get_max_state,
544 .get_cur_state = cpufreq_get_cur_state,
545 .set_cur_state = cpufreq_set_cur_state,
548 static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
549 .get_max_state = cpufreq_get_max_state,
550 .get_cur_state = cpufreq_get_cur_state,
551 .set_cur_state = cpufreq_set_cur_state,
552 .get_requested_power = cpufreq_get_requested_power,
553 .state2power = cpufreq_state2power,
554 .power2state = cpufreq_power2state,
557 /* Notifier for cpufreq policy change */
558 static struct notifier_block thermal_cpufreq_notifier_block = {
559 .notifier_call = cpufreq_thermal_notifier,
562 static unsigned int find_next_max(struct cpufreq_frequency_table *table,
563 unsigned int prev_max)
565 struct cpufreq_frequency_table *pos;
566 unsigned int max = 0;
568 cpufreq_for_each_valid_entry(pos, table) {
569 if (pos->frequency > max && pos->frequency < prev_max)
570 max = pos->frequency;
577 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
578 * @np: a valid struct device_node to the cooling device device tree node
579 * @policy: cpufreq policy
580 * Normally this should be same as cpufreq policy->related_cpus.
581 * @capacitance: dynamic power coefficient for these cpus
583 * This interface function registers the cpufreq cooling device with the name
584 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
585 * cooling devices. It also gives the opportunity to link the cooling device
586 * with a device tree node, in order to bind it via the thermal DT code.
588 * Return: a valid struct thermal_cooling_device pointer on success,
589 * on failure, it returns a corresponding ERR_PTR().
591 static struct thermal_cooling_device *
592 __cpufreq_cooling_register(struct device_node *np,
593 struct cpufreq_policy *policy, u32 capacitance)
595 struct thermal_cooling_device *cdev;
596 struct cpufreq_cooling_device *cpufreq_cdev;
597 char dev_name[THERMAL_NAME_LENGTH];
598 unsigned int freq, i, num_cpus;
600 struct thermal_cooling_device_ops *cooling_ops;
603 if (IS_ERR_OR_NULL(policy)) {
604 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
605 return ERR_PTR(-EINVAL);
608 i = cpufreq_table_count_valid_entries(policy);
610 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
612 return ERR_PTR(-ENODEV);
615 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
617 return ERR_PTR(-ENOMEM);
619 cpufreq_cdev->policy = policy;
620 num_cpus = cpumask_weight(policy->related_cpus);
621 cpufreq_cdev->idle_time = kcalloc(num_cpus,
622 sizeof(*cpufreq_cdev->idle_time),
624 if (!cpufreq_cdev->idle_time) {
625 cdev = ERR_PTR(-ENOMEM);
629 /* max_level is an index, not a counter */
630 cpufreq_cdev->max_level = i - 1;
632 cpufreq_cdev->freq_table = kmalloc_array(i,
633 sizeof(*cpufreq_cdev->freq_table),
635 if (!cpufreq_cdev->freq_table) {
636 cdev = ERR_PTR(-ENOMEM);
640 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
645 cpufreq_cdev->id = ret;
647 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
650 /* Fill freq-table in descending order of frequencies */
651 for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) {
652 freq = find_next_max(policy->freq_table, freq);
653 cpufreq_cdev->freq_table[i].frequency = freq;
655 /* Warn for duplicate entries */
657 pr_warn("%s: table has duplicate entries\n", __func__);
659 pr_debug("%s: freq:%u KHz\n", __func__, freq);
663 ret = update_freq_table(cpufreq_cdev, capacitance);
669 cooling_ops = &cpufreq_power_cooling_ops;
671 cooling_ops = &cpufreq_cooling_ops;
674 cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev,
679 cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0].frequency;
681 mutex_lock(&cooling_list_lock);
682 /* Register the notifier for first cpufreq cooling device */
683 first = list_empty(&cpufreq_cdev_list);
684 list_add(&cpufreq_cdev->node, &cpufreq_cdev_list);
685 mutex_unlock(&cooling_list_lock);
688 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
689 CPUFREQ_POLICY_NOTIFIER);
694 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
696 kfree(cpufreq_cdev->freq_table);
698 kfree(cpufreq_cdev->idle_time);
705 * cpufreq_cooling_register - function to create cpufreq cooling device.
706 * @policy: cpufreq policy
708 * This interface function registers the cpufreq cooling device with the name
709 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
712 * Return: a valid struct thermal_cooling_device pointer on success,
713 * on failure, it returns a corresponding ERR_PTR().
715 struct thermal_cooling_device *
716 cpufreq_cooling_register(struct cpufreq_policy *policy)
718 return __cpufreq_cooling_register(NULL, policy, 0);
720 EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
723 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
724 * @policy: cpufreq policy
726 * This interface function registers the cpufreq cooling device with the name
727 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
728 * cooling devices. Using this API, the cpufreq cooling device will be
729 * linked to the device tree node provided.
731 * Using this function, the cooling device will implement the power
732 * extensions by using a simple cpu power model. The cpus must have
733 * registered their OPPs using the OPP library.
735 * It also takes into account, if property present in policy CPU node, the
736 * static power consumed by the cpu.
738 * Return: a valid struct thermal_cooling_device pointer on success,
739 * and NULL on failure.
741 struct thermal_cooling_device *
742 of_cpufreq_cooling_register(struct cpufreq_policy *policy)
744 struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
745 struct thermal_cooling_device *cdev = NULL;
749 pr_err("cpu_cooling: OF node not available for cpu%d\n",
754 if (of_find_property(np, "#cooling-cells", NULL)) {
755 of_property_read_u32(np, "dynamic-power-coefficient",
758 cdev = __cpufreq_cooling_register(np, policy, capacitance);
760 pr_err("cpu_cooling: cpu%d failed to register as cooling device: %ld\n",
761 policy->cpu, PTR_ERR(cdev));
769 EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
772 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
773 * @cdev: thermal cooling device pointer.
775 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
777 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
779 struct cpufreq_cooling_device *cpufreq_cdev;
785 cpufreq_cdev = cdev->devdata;
787 mutex_lock(&cooling_list_lock);
788 list_del(&cpufreq_cdev->node);
789 /* Unregister the notifier for the last cpufreq cooling device */
790 last = list_empty(&cpufreq_cdev_list);
791 mutex_unlock(&cooling_list_lock);
794 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
795 CPUFREQ_POLICY_NOTIFIER);
797 thermal_cooling_device_unregister(cdev);
798 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
799 kfree(cpufreq_cdev->idle_time);
800 kfree(cpufreq_cdev->freq_table);
803 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);