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 * @cooling_ops: cpufreq callbacks to thermal cooling device ops
63 * @idle_time: idle time stats
64 * @qos_req: PM QoS contraint to apply
66 * This structure is required for keeping information of each registered
67 * cpufreq_cooling_device.
69 struct cpufreq_cooling_device {
71 unsigned int cpufreq_state;
72 unsigned int max_level;
73 struct em_perf_domain *em;
74 struct cpufreq_policy *policy;
75 struct thermal_cooling_device_ops cooling_ops;
77 struct time_in_idle *idle_time;
79 struct freq_qos_request qos_req;
82 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
84 * get_level: Find the level for a particular frequency
85 * @cpufreq_cdev: cpufreq_cdev for which the property is required
88 * Return: level corresponding to the frequency.
90 static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
95 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
96 if (freq > cpufreq_cdev->em->table[i].frequency)
100 return cpufreq_cdev->max_level - i - 1;
103 static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
108 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
109 if (freq > cpufreq_cdev->em->table[i].frequency)
113 return cpufreq_cdev->em->table[i + 1].power;
116 static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
121 for (i = cpufreq_cdev->max_level; i > 0; i--) {
122 if (power >= cpufreq_cdev->em->table[i].power)
126 return cpufreq_cdev->em->table[i].frequency;
130 * get_load() - get load for a cpu
131 * @cpufreq_cdev: struct cpufreq_cooling_device for the cpu
133 * @cpu_idx: index of the cpu in time_in_idle array
135 * Return: The average load of cpu @cpu in percentage since this
136 * function was last called.
139 static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
142 unsigned long max = arch_scale_cpu_capacity(cpu);
145 util = sched_cpu_util(cpu, max);
146 return (util * 100) / max;
148 #else /* !CONFIG_SMP */
149 static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
153 u64 now, now_idle, delta_time, delta_idle;
154 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
156 now_idle = get_cpu_idle_time(cpu, &now, 0);
157 delta_idle = now_idle - idle_time->time;
158 delta_time = now - idle_time->timestamp;
160 if (delta_time <= delta_idle)
163 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
165 idle_time->time = now_idle;
166 idle_time->timestamp = now;
170 #endif /* CONFIG_SMP */
173 * get_dynamic_power() - calculate the dynamic power
174 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
175 * @freq: current frequency
177 * Return: the dynamic power consumed by the cpus described by
180 static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
185 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
186 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
190 * cpufreq_get_requested_power() - get the current power
191 * @cdev: &thermal_cooling_device pointer
192 * @power: pointer in which to store the resulting power
194 * Calculate the current power consumption of the cpus in milliwatts
195 * and store it in @power. This function should actually calculate
196 * the requested power, but it's hard to get the frequency that
197 * cpufreq would have assigned if there were no thermal limits.
198 * Instead, we calculate the current power on the assumption that the
199 * immediate future will look like the immediate past.
201 * We use the current frequency and the average load since this
202 * function was last called. In reality, there could have been
203 * multiple opps since this function was last called and that affects
204 * the load calculation. While it's not perfectly accurate, this
205 * simplification is good enough and works. REVISIT this, as more
206 * complex code may be needed if experiments show that it's not
209 * Return: 0 on success, this function doesn't fail.
211 static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
217 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
218 struct cpufreq_policy *policy = cpufreq_cdev->policy;
220 freq = cpufreq_quick_get(policy->cpu);
222 for_each_cpu(cpu, policy->related_cpus) {
226 load = get_load(cpufreq_cdev, cpu, i);
233 cpufreq_cdev->last_load = total_load;
235 *power = get_dynamic_power(cpufreq_cdev, freq);
237 trace_thermal_power_cpu_get_power_simple(policy->cpu, *power);
243 * cpufreq_state2power() - convert a cpu cdev state to power consumed
244 * @cdev: &thermal_cooling_device pointer
245 * @state: cooling device state to be converted
246 * @power: pointer in which to store the resulting power
248 * Convert cooling device state @state into power consumption in
249 * milliwatts assuming 100% load. Store the calculated power in
252 * Return: 0 on success, -EINVAL if the cooling device state is bigger
253 * than maximum allowed.
255 static int cpufreq_state2power(struct thermal_cooling_device *cdev,
256 unsigned long state, u32 *power)
258 unsigned int freq, num_cpus, idx;
259 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
261 /* Request state should be less than max_level */
262 if (state > cpufreq_cdev->max_level)
265 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
267 idx = cpufreq_cdev->max_level - state;
268 freq = cpufreq_cdev->em->table[idx].frequency;
269 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
275 * cpufreq_power2state() - convert power to a cooling device state
276 * @cdev: &thermal_cooling_device pointer
277 * @power: power in milliwatts to be converted
278 * @state: pointer in which to store the resulting state
280 * Calculate a cooling device state for the cpus described by @cdev
281 * that would allow them to consume at most @power mW and store it in
282 * @state. Note that this calculation depends on external factors
283 * such as the CPUs load. Calling this function with the same power
284 * as input can yield different cooling device states depending on those
287 * Return: 0 on success, this function doesn't fail.
289 static int cpufreq_power2state(struct thermal_cooling_device *cdev,
290 u32 power, unsigned long *state)
292 unsigned int target_freq;
293 u32 last_load, normalised_power;
294 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
295 struct cpufreq_policy *policy = cpufreq_cdev->policy;
297 last_load = cpufreq_cdev->last_load ?: 1;
298 normalised_power = (power * 100) / last_load;
299 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
301 *state = get_level(cpufreq_cdev, target_freq);
302 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
307 static inline bool em_is_sane(struct cpufreq_cooling_device *cpufreq_cdev,
308 struct em_perf_domain *em) {
309 struct cpufreq_policy *policy;
310 unsigned int nr_levels;
312 if (!em || em_is_artificial(em))
315 policy = cpufreq_cdev->policy;
316 if (!cpumask_equal(policy->related_cpus, em_span_cpus(em))) {
317 pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n",
318 cpumask_pr_args(em_span_cpus(em)),
319 cpumask_pr_args(policy->related_cpus));
323 nr_levels = cpufreq_cdev->max_level + 1;
324 if (em_pd_nr_perf_states(em) != nr_levels) {
325 pr_err("The number of performance states in pd %*pbl (%u) doesn't match the number of cooling levels (%u)\n",
326 cpumask_pr_args(em_span_cpus(em)),
327 em_pd_nr_perf_states(em), nr_levels);
333 #endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */
336 static inline int allocate_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
341 static inline void free_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
345 static int allocate_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
347 unsigned int num_cpus = cpumask_weight(cpufreq_cdev->policy->related_cpus);
349 cpufreq_cdev->idle_time = kcalloc(num_cpus,
350 sizeof(*cpufreq_cdev->idle_time),
352 if (!cpufreq_cdev->idle_time)
358 static void free_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
360 kfree(cpufreq_cdev->idle_time);
361 cpufreq_cdev->idle_time = NULL;
363 #endif /* CONFIG_SMP */
365 static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev,
368 struct cpufreq_policy *policy;
371 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
372 /* Use the Energy Model table if available */
373 if (cpufreq_cdev->em) {
374 idx = cpufreq_cdev->max_level - state;
375 return cpufreq_cdev->em->table[idx].frequency;
379 /* Otherwise, fallback on the CPUFreq table */
380 policy = cpufreq_cdev->policy;
381 if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
382 idx = cpufreq_cdev->max_level - state;
386 return policy->freq_table[idx].frequency;
389 /* cpufreq cooling device callback functions are defined below */
392 * cpufreq_get_max_state - callback function to get the max cooling state.
393 * @cdev: thermal cooling device pointer.
394 * @state: fill this variable with the max cooling state.
396 * Callback for the thermal cooling device to return the cpufreq
399 * Return: 0 on success, this function doesn't fail.
401 static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
402 unsigned long *state)
404 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
406 *state = cpufreq_cdev->max_level;
411 * cpufreq_get_cur_state - callback function to get the current cooling state.
412 * @cdev: thermal cooling device pointer.
413 * @state: fill this variable with the current cooling state.
415 * Callback for the thermal cooling device to return the cpufreq
416 * current cooling state.
418 * Return: 0 on success, this function doesn't fail.
420 static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
421 unsigned long *state)
423 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
425 *state = cpufreq_cdev->cpufreq_state;
431 * cpufreq_set_cur_state - callback function to set the current cooling state.
432 * @cdev: thermal cooling device pointer.
433 * @state: set this variable to the current cooling state.
435 * Callback for the thermal cooling device to change the cpufreq
436 * current cooling state.
438 * Return: 0 on success, an error code otherwise.
440 static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
443 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
444 struct cpumask *cpus;
445 unsigned int frequency;
448 /* Request state should be less than max_level */
449 if (state > cpufreq_cdev->max_level)
452 /* Check if the old cooling action is same as new cooling action */
453 if (cpufreq_cdev->cpufreq_state == state)
456 frequency = get_state_freq(cpufreq_cdev, state);
458 ret = freq_qos_update_request(&cpufreq_cdev->qos_req, frequency);
460 cpufreq_cdev->cpufreq_state = state;
461 cpus = cpufreq_cdev->policy->related_cpus;
462 arch_update_thermal_pressure(cpus, frequency);
470 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
471 * @np: a valid struct device_node to the cooling device device tree node
472 * @policy: cpufreq policy
473 * Normally this should be same as cpufreq policy->related_cpus.
474 * @em: Energy Model of the cpufreq policy
476 * This interface function registers the cpufreq cooling device with the name
477 * "cpufreq-%s". This API can support multiple instances of cpufreq
478 * cooling devices. It also gives the opportunity to link the cooling device
479 * with a device tree node, in order to bind it via the thermal DT code.
481 * Return: a valid struct thermal_cooling_device pointer on success,
482 * on failure, it returns a corresponding ERR_PTR().
484 static struct thermal_cooling_device *
485 __cpufreq_cooling_register(struct device_node *np,
486 struct cpufreq_policy *policy,
487 struct em_perf_domain *em)
489 struct thermal_cooling_device *cdev;
490 struct cpufreq_cooling_device *cpufreq_cdev;
494 struct thermal_cooling_device_ops *cooling_ops;
497 dev = get_cpu_device(policy->cpu);
498 if (unlikely(!dev)) {
499 pr_warn("No cpu device for cpu %d\n", policy->cpu);
500 return ERR_PTR(-ENODEV);
503 if (IS_ERR_OR_NULL(policy)) {
504 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
505 return ERR_PTR(-EINVAL);
508 i = cpufreq_table_count_valid_entries(policy);
510 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
512 return ERR_PTR(-ENODEV);
515 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
517 return ERR_PTR(-ENOMEM);
519 cpufreq_cdev->policy = policy;
521 ret = allocate_idle_time(cpufreq_cdev);
527 /* max_level is an index, not a counter */
528 cpufreq_cdev->max_level = i - 1;
530 cooling_ops = &cpufreq_cdev->cooling_ops;
531 cooling_ops->get_max_state = cpufreq_get_max_state;
532 cooling_ops->get_cur_state = cpufreq_get_cur_state;
533 cooling_ops->set_cur_state = cpufreq_set_cur_state;
535 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
536 if (em_is_sane(cpufreq_cdev, em)) {
537 cpufreq_cdev->em = em;
538 cooling_ops->get_requested_power = cpufreq_get_requested_power;
539 cooling_ops->state2power = cpufreq_state2power;
540 cooling_ops->power2state = cpufreq_power2state;
543 if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) {
544 pr_err("%s: unsorted frequency tables are not supported\n",
546 cdev = ERR_PTR(-EINVAL);
550 ret = freq_qos_add_request(&policy->constraints,
551 &cpufreq_cdev->qos_req, FREQ_QOS_MAX,
552 get_state_freq(cpufreq_cdev, 0));
554 pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
560 cdev = ERR_PTR(-ENOMEM);
561 name = kasprintf(GFP_KERNEL, "cpufreq-%s", dev_name(dev));
565 cdev = thermal_of_cooling_device_register(np, name, cpufreq_cdev,
575 freq_qos_remove_request(&cpufreq_cdev->qos_req);
577 free_idle_time(cpufreq_cdev);
584 * cpufreq_cooling_register - function to create cpufreq cooling device.
585 * @policy: cpufreq policy
587 * This interface function registers the cpufreq cooling device with the name
588 * "cpufreq-%s". This API can support multiple instances of cpufreq cooling
591 * Return: a valid struct thermal_cooling_device pointer on success,
592 * on failure, it returns a corresponding ERR_PTR().
594 struct thermal_cooling_device *
595 cpufreq_cooling_register(struct cpufreq_policy *policy)
597 return __cpufreq_cooling_register(NULL, policy, NULL);
599 EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
602 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
603 * @policy: cpufreq policy
605 * This interface function registers the cpufreq cooling device with the name
606 * "cpufreq-%s". This API can support multiple instances of cpufreq cooling
607 * devices. Using this API, the cpufreq cooling device will be linked to the
608 * device tree node provided.
610 * Using this function, the cooling device will implement the power
611 * extensions by using the Energy Model (if present). The cpus must have
612 * registered their OPPs using the OPP library.
614 * Return: a valid struct thermal_cooling_device pointer on success,
615 * and NULL on failure.
617 struct thermal_cooling_device *
618 of_cpufreq_cooling_register(struct cpufreq_policy *policy)
620 struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
621 struct thermal_cooling_device *cdev = NULL;
624 pr_err("cpufreq_cooling: OF node not available for cpu%d\n",
629 if (of_find_property(np, "#cooling-cells", NULL)) {
630 struct em_perf_domain *em = em_cpu_get(policy->cpu);
632 cdev = __cpufreq_cooling_register(np, policy, em);
634 pr_err("cpufreq_cooling: cpu%d failed to register as cooling device: %ld\n",
635 policy->cpu, PTR_ERR(cdev));
643 EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
646 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
647 * @cdev: thermal cooling device pointer.
649 * This interface function unregisters the "cpufreq-%x" cooling device.
651 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
653 struct cpufreq_cooling_device *cpufreq_cdev;
658 cpufreq_cdev = cdev->devdata;
660 thermal_cooling_device_unregister(cdev);
661 freq_qos_remove_request(&cpufreq_cdev->qos_req);
662 free_idle_time(cpufreq_cdev);
665 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);