1 // SPDX-License-Identifier: GPL-2.0
3 * linux/drivers/thermal/cpufreq_cooling.c
5 * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
7 * Copyright (C) 2012-2018 Linaro Limited.
9 * Authors: Amit Daniel <amit.kachhap@linaro.org>
10 * Viresh Kumar <viresh.kumar@linaro.org>
13 #include <linux/cpu.h>
14 #include <linux/cpufreq.h>
15 #include <linux/cpu_cooling.h>
16 #include <linux/device.h>
17 #include <linux/energy_model.h>
18 #include <linux/err.h>
19 #include <linux/export.h>
20 #include <linux/pm_opp.h>
21 #include <linux/pm_qos.h>
22 #include <linux/slab.h>
23 #include <linux/thermal.h>
25 #include <trace/events/thermal.h>
28 * Cooling state <-> CPUFreq frequency
30 * Cooling states are translated to frequencies throughout this driver and this
31 * is the relation between them.
33 * Highest cooling state corresponds to lowest possible frequency.
36 * level 0 --> 1st Max Freq
37 * level 1 --> 2nd Max Freq
42 * struct time_in_idle - Idle time stats
43 * @time: previous reading of the absolute time that this cpu was idle
44 * @timestamp: wall time of the last invocation of get_cpu_idle_time_us()
52 * struct cpufreq_cooling_device - data for cooling device with cpufreq
53 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
54 * @cpufreq_state: integer value representing the current state of cpufreq
56 * @max_level: maximum cooling level. One less than total number of valid
57 * cpufreq frequencies.
58 * @em: Reference on the Energy Model of the device
59 * @cdev: thermal_cooling_device pointer to keep track of the
60 * registered cooling device.
61 * @policy: cpufreq policy.
62 * @idle_time: idle time stats
63 * @qos_req: PM QoS contraint to apply
65 * This structure is required for keeping information of each registered
66 * cpufreq_cooling_device.
68 struct cpufreq_cooling_device {
70 unsigned int cpufreq_state;
71 unsigned int max_level;
72 struct em_perf_domain *em;
73 struct cpufreq_policy *policy;
75 struct time_in_idle *idle_time;
77 struct freq_qos_request qos_req;
80 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
82 * get_level: Find the level for a particular frequency
83 * @cpufreq_cdev: cpufreq_cdev for which the property is required
86 * Return: level corresponding to the frequency.
88 static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
93 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
94 if (freq > cpufreq_cdev->em->table[i].frequency)
98 return cpufreq_cdev->max_level - i - 1;
101 static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
106 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
107 if (freq > cpufreq_cdev->em->table[i].frequency)
111 return cpufreq_cdev->em->table[i + 1].power;
114 static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
119 for (i = cpufreq_cdev->max_level; i > 0; i--) {
120 if (power >= cpufreq_cdev->em->table[i].power)
124 return cpufreq_cdev->em->table[i].frequency;
128 * get_load() - get load for a cpu
129 * @cpufreq_cdev: struct cpufreq_cooling_device for the cpu
131 * @cpu_idx: index of the cpu in time_in_idle array
133 * Return: The average load of cpu @cpu in percentage since this
134 * function was last called.
137 static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
140 unsigned long max = arch_scale_cpu_capacity(cpu);
143 util = sched_cpu_util(cpu, max);
144 return (util * 100) / max;
146 #else /* !CONFIG_SMP */
147 static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
151 u64 now, now_idle, delta_time, delta_idle;
152 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
154 now_idle = get_cpu_idle_time(cpu, &now, 0);
155 delta_idle = now_idle - idle_time->time;
156 delta_time = now - idle_time->timestamp;
158 if (delta_time <= delta_idle)
161 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
163 idle_time->time = now_idle;
164 idle_time->timestamp = now;
168 #endif /* CONFIG_SMP */
171 * get_dynamic_power() - calculate the dynamic power
172 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
173 * @freq: current frequency
175 * Return: the dynamic power consumed by the cpus described by
178 static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
183 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
184 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
188 * cpufreq_get_requested_power() - get the current power
189 * @cdev: &thermal_cooling_device pointer
190 * @power: pointer in which to store the resulting power
192 * Calculate the current power consumption of the cpus in milliwatts
193 * and store it in @power. This function should actually calculate
194 * the requested power, but it's hard to get the frequency that
195 * cpufreq would have assigned if there were no thermal limits.
196 * Instead, we calculate the current power on the assumption that the
197 * immediate future will look like the immediate past.
199 * We use the current frequency and the average load since this
200 * function was last called. In reality, there could have been
201 * multiple opps since this function was last called and that affects
202 * the load calculation. While it's not perfectly accurate, this
203 * simplification is good enough and works. REVISIT this, as more
204 * complex code may be needed if experiments show that it's not
207 * Return: 0 on success, -E* if getting the static power failed.
209 static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
215 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
216 struct cpufreq_policy *policy = cpufreq_cdev->policy;
217 u32 *load_cpu = NULL;
219 freq = cpufreq_quick_get(policy->cpu);
221 if (trace_thermal_power_cpu_get_power_enabled()) {
222 u32 ncpus = cpumask_weight(policy->related_cpus);
224 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
227 for_each_cpu(cpu, policy->related_cpus) {
231 load = get_load(cpufreq_cdev, cpu, i);
242 cpufreq_cdev->last_load = total_load;
244 *power = get_dynamic_power(cpufreq_cdev, freq);
247 trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
248 load_cpu, i, *power);
257 * cpufreq_state2power() - convert a cpu cdev state to power consumed
258 * @cdev: &thermal_cooling_device pointer
259 * @state: cooling device state to be converted
260 * @power: pointer in which to store the resulting power
262 * Convert cooling device state @state into power consumption in
263 * milliwatts assuming 100% load. Store the calculated power in
266 * Return: 0 on success, -EINVAL if the cooling device state could not
267 * be converted into a frequency or other -E* if there was an error
268 * when calculating the static power.
270 static int cpufreq_state2power(struct thermal_cooling_device *cdev,
271 unsigned long state, u32 *power)
273 unsigned int freq, num_cpus, idx;
274 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
276 /* Request state should be less than max_level */
277 if (state > cpufreq_cdev->max_level)
280 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
282 idx = cpufreq_cdev->max_level - state;
283 freq = cpufreq_cdev->em->table[idx].frequency;
284 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
290 * cpufreq_power2state() - convert power to a cooling device state
291 * @cdev: &thermal_cooling_device pointer
292 * @power: power in milliwatts to be converted
293 * @state: pointer in which to store the resulting state
295 * Calculate a cooling device state for the cpus described by @cdev
296 * that would allow them to consume at most @power mW and store it in
297 * @state. Note that this calculation depends on external factors
298 * such as the cpu load or the current static power. Calling this
299 * function with the same power as input can yield different cooling
300 * device states depending on those external factors.
302 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
303 * the calculated frequency could not be converted to a valid state.
304 * The latter should not happen unless the frequencies available to
305 * cpufreq have changed since the initialization of the cpu cooling
308 static int cpufreq_power2state(struct thermal_cooling_device *cdev,
309 u32 power, unsigned long *state)
311 unsigned int target_freq;
312 u32 last_load, normalised_power;
313 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
314 struct cpufreq_policy *policy = cpufreq_cdev->policy;
316 last_load = cpufreq_cdev->last_load ?: 1;
317 normalised_power = (power * 100) / last_load;
318 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
320 *state = get_level(cpufreq_cdev, target_freq);
321 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
326 static inline bool em_is_sane(struct cpufreq_cooling_device *cpufreq_cdev,
327 struct em_perf_domain *em) {
328 struct cpufreq_policy *policy;
329 unsigned int nr_levels;
334 policy = cpufreq_cdev->policy;
335 if (!cpumask_equal(policy->related_cpus, em_span_cpus(em))) {
336 pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n",
337 cpumask_pr_args(em_span_cpus(em)),
338 cpumask_pr_args(policy->related_cpus));
342 nr_levels = cpufreq_cdev->max_level + 1;
343 if (em_pd_nr_perf_states(em) != nr_levels) {
344 pr_err("The number of performance states in pd %*pbl (%u) doesn't match the number of cooling levels (%u)\n",
345 cpumask_pr_args(em_span_cpus(em)),
346 em_pd_nr_perf_states(em), nr_levels);
352 #endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */
355 static inline int allocate_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
360 static inline void free_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
364 static int allocate_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
366 unsigned int num_cpus = cpumask_weight(cpufreq_cdev->policy->related_cpus);
368 cpufreq_cdev->idle_time = kcalloc(num_cpus,
369 sizeof(*cpufreq_cdev->idle_time),
371 if (!cpufreq_cdev->idle_time)
377 static void free_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
379 kfree(cpufreq_cdev->idle_time);
380 cpufreq_cdev->idle_time = NULL;
382 #endif /* CONFIG_SMP */
384 static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev,
387 struct cpufreq_policy *policy;
390 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
391 /* Use the Energy Model table if available */
392 if (cpufreq_cdev->em) {
393 idx = cpufreq_cdev->max_level - state;
394 return cpufreq_cdev->em->table[idx].frequency;
398 /* Otherwise, fallback on the CPUFreq table */
399 policy = cpufreq_cdev->policy;
400 if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
401 idx = cpufreq_cdev->max_level - state;
405 return policy->freq_table[idx].frequency;
408 /* cpufreq cooling device callback functions are defined below */
411 * cpufreq_get_max_state - callback function to get the max cooling state.
412 * @cdev: thermal cooling device pointer.
413 * @state: fill this variable with the max cooling state.
415 * Callback for the thermal cooling device to return the cpufreq
418 * Return: 0 on success, an error code otherwise.
420 static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
421 unsigned long *state)
423 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
425 *state = cpufreq_cdev->max_level;
430 * cpufreq_get_cur_state - callback function to get the current cooling state.
431 * @cdev: thermal cooling device pointer.
432 * @state: fill this variable with the current cooling state.
434 * Callback for the thermal cooling device to return the cpufreq
435 * current cooling state.
437 * Return: 0 on success, an error code otherwise.
439 static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
440 unsigned long *state)
442 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
444 *state = cpufreq_cdev->cpufreq_state;
450 * cpufreq_set_cur_state - callback function to set the current cooling state.
451 * @cdev: thermal cooling device pointer.
452 * @state: set this variable to the current cooling state.
454 * Callback for the thermal cooling device to change the cpufreq
455 * current cooling state.
457 * Return: 0 on success, an error code otherwise.
459 static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
462 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
463 struct cpumask *cpus;
464 unsigned int frequency;
465 unsigned long max_capacity, capacity;
468 /* Request state should be less than max_level */
469 if (state > cpufreq_cdev->max_level)
472 /* Check if the old cooling action is same as new cooling action */
473 if (cpufreq_cdev->cpufreq_state == state)
476 frequency = get_state_freq(cpufreq_cdev, state);
478 ret = freq_qos_update_request(&cpufreq_cdev->qos_req, frequency);
480 cpufreq_cdev->cpufreq_state = state;
481 cpus = cpufreq_cdev->policy->related_cpus;
482 max_capacity = arch_scale_cpu_capacity(cpumask_first(cpus));
483 capacity = frequency * max_capacity;
484 capacity /= cpufreq_cdev->policy->cpuinfo.max_freq;
485 arch_set_thermal_pressure(cpus, max_capacity - capacity);
492 /* Bind cpufreq callbacks to thermal cooling device ops */
494 static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
495 .get_max_state = cpufreq_get_max_state,
496 .get_cur_state = cpufreq_get_cur_state,
497 .set_cur_state = cpufreq_set_cur_state,
501 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
502 * @np: a valid struct device_node to the cooling device device tree node
503 * @policy: cpufreq policy
504 * Normally this should be same as cpufreq policy->related_cpus.
505 * @em: Energy Model of the cpufreq policy
507 * This interface function registers the cpufreq cooling device with the name
508 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
509 * cooling devices. It also gives the opportunity to link the cooling device
510 * with a device tree node, in order to bind it via the thermal DT code.
512 * Return: a valid struct thermal_cooling_device pointer on success,
513 * on failure, it returns a corresponding ERR_PTR().
515 static struct thermal_cooling_device *
516 __cpufreq_cooling_register(struct device_node *np,
517 struct cpufreq_policy *policy,
518 struct em_perf_domain *em)
520 struct thermal_cooling_device *cdev;
521 struct cpufreq_cooling_device *cpufreq_cdev;
525 struct thermal_cooling_device_ops *cooling_ops;
528 dev = get_cpu_device(policy->cpu);
529 if (unlikely(!dev)) {
530 pr_warn("No cpu device for cpu %d\n", policy->cpu);
531 return ERR_PTR(-ENODEV);
534 if (IS_ERR_OR_NULL(policy)) {
535 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
536 return ERR_PTR(-EINVAL);
539 i = cpufreq_table_count_valid_entries(policy);
541 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
543 return ERR_PTR(-ENODEV);
546 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
548 return ERR_PTR(-ENOMEM);
550 cpufreq_cdev->policy = policy;
552 ret = allocate_idle_time(cpufreq_cdev);
558 /* max_level is an index, not a counter */
559 cpufreq_cdev->max_level = i - 1;
561 cooling_ops = &cpufreq_cooling_ops;
563 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
564 if (em_is_sane(cpufreq_cdev, em)) {
565 cpufreq_cdev->em = em;
566 cooling_ops->get_requested_power = cpufreq_get_requested_power;
567 cooling_ops->state2power = cpufreq_state2power;
568 cooling_ops->power2state = cpufreq_power2state;
571 if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) {
572 pr_err("%s: unsorted frequency tables are not supported\n",
574 cdev = ERR_PTR(-EINVAL);
578 ret = freq_qos_add_request(&policy->constraints,
579 &cpufreq_cdev->qos_req, FREQ_QOS_MAX,
580 get_state_freq(cpufreq_cdev, 0));
582 pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
588 cdev = ERR_PTR(-ENOMEM);
589 name = kasprintf(GFP_KERNEL, "cpufreq-%s", dev_name(dev));
593 cdev = thermal_of_cooling_device_register(np, name, cpufreq_cdev,
603 freq_qos_remove_request(&cpufreq_cdev->qos_req);
605 free_idle_time(cpufreq_cdev);
612 * cpufreq_cooling_register - function to create cpufreq cooling device.
613 * @policy: cpufreq policy
615 * This interface function registers the cpufreq cooling device with the name
616 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
619 * Return: a valid struct thermal_cooling_device pointer on success,
620 * on failure, it returns a corresponding ERR_PTR().
622 struct thermal_cooling_device *
623 cpufreq_cooling_register(struct cpufreq_policy *policy)
625 return __cpufreq_cooling_register(NULL, policy, NULL);
627 EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
630 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
631 * @policy: cpufreq policy
633 * This interface function registers the cpufreq cooling device with the name
634 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
635 * cooling devices. Using this API, the cpufreq cooling device will be
636 * linked to the device tree node provided.
638 * Using this function, the cooling device will implement the power
639 * extensions by using a simple cpu power model. The cpus must have
640 * registered their OPPs using the OPP library.
642 * It also takes into account, if property present in policy CPU node, the
643 * static power consumed by the cpu.
645 * Return: a valid struct thermal_cooling_device pointer on success,
646 * and NULL on failure.
648 struct thermal_cooling_device *
649 of_cpufreq_cooling_register(struct cpufreq_policy *policy)
651 struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
652 struct thermal_cooling_device *cdev = NULL;
655 pr_err("cpufreq_cooling: OF node not available for cpu%d\n",
660 if (of_find_property(np, "#cooling-cells", NULL)) {
661 struct em_perf_domain *em = em_cpu_get(policy->cpu);
663 cdev = __cpufreq_cooling_register(np, policy, em);
665 pr_err("cpufreq_cooling: cpu%d failed to register as cooling device: %ld\n",
666 policy->cpu, PTR_ERR(cdev));
674 EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
677 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
678 * @cdev: thermal cooling device pointer.
680 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
682 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
684 struct cpufreq_cooling_device *cpufreq_cdev;
689 cpufreq_cdev = cdev->devdata;
691 thermal_cooling_device_unregister(cdev);
692 freq_qos_remove_request(&cpufreq_cdev->qos_req);
693 free_idle_time(cpufreq_cdev);
696 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);