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.
9 * Authors: Amit Daniel <amit.kachhap@linaro.org>
10 * Viresh Kumar <viresh.kumar@linaro.org>
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/pm_qos.h>
20 #include <linux/slab.h>
21 #include <linux/cpu.h>
22 #include <linux/cpu_cooling.h>
23 #include <linux/energy_model.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 * @id: unique integer value corresponding to each cpufreq_cooling_device
55 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
56 * @cpufreq_state: integer value representing the current state of cpufreq
58 * @max_level: maximum cooling level. One less than total number of valid
59 * cpufreq frequencies.
60 * @em: Reference on the Energy Model of the device
61 * @cdev: thermal_cooling_device pointer to keep track of the
62 * registered cooling device.
63 * @policy: cpufreq policy.
64 * @node: list_head to link all cpufreq_cooling_device together.
65 * @idle_time: idle time stats
67 * This structure is required for keeping information of each registered
68 * cpufreq_cooling_device.
70 struct cpufreq_cooling_device {
73 unsigned int cpufreq_state;
74 unsigned int max_level;
75 struct em_perf_domain *em;
76 struct cpufreq_policy *policy;
77 struct list_head node;
78 struct time_in_idle *idle_time;
79 struct freq_qos_request qos_req;
82 static DEFINE_IDA(cpufreq_ida);
83 static DEFINE_MUTEX(cooling_list_lock);
84 static LIST_HEAD(cpufreq_cdev_list);
86 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
88 * get_level: Find the level for a particular frequency
89 * @cpufreq_cdev: cpufreq_cdev for which the property is required
92 * Return: level corresponding to the frequency.
94 static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
99 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
100 if (freq > cpufreq_cdev->em->table[i].frequency)
104 return cpufreq_cdev->max_level - i - 1;
107 static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
112 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
113 if (freq > cpufreq_cdev->em->table[i].frequency)
117 return cpufreq_cdev->em->table[i + 1].power;
120 static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
125 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
126 if (power > cpufreq_cdev->em->table[i].power)
130 return cpufreq_cdev->em->table[i + 1].frequency;
134 * get_load() - get load for a cpu since last updated
135 * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu
137 * @cpu_idx: index of the cpu in time_in_idle*
139 * Return: The average load of cpu @cpu in percentage since this
140 * function was last called.
142 static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
146 u64 now, now_idle, delta_time, delta_idle;
147 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
149 now_idle = get_cpu_idle_time(cpu, &now, 0);
150 delta_idle = now_idle - idle_time->time;
151 delta_time = now - idle_time->timestamp;
153 if (delta_time <= delta_idle)
156 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
158 idle_time->time = now_idle;
159 idle_time->timestamp = now;
165 * get_dynamic_power() - calculate the dynamic power
166 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
167 * @freq: current frequency
169 * Return: the dynamic power consumed by the cpus described by
172 static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
177 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
178 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
182 * cpufreq_get_requested_power() - get the current power
183 * @cdev: &thermal_cooling_device pointer
184 * @tz: a valid thermal zone device pointer
185 * @power: pointer in which to store the resulting power
187 * Calculate the current power consumption of the cpus in milliwatts
188 * and store it in @power. This function should actually calculate
189 * the requested power, but it's hard to get the frequency that
190 * cpufreq would have assigned if there were no thermal limits.
191 * Instead, we calculate the current power on the assumption that the
192 * immediate future will look like the immediate past.
194 * We use the current frequency and the average load since this
195 * function was last called. In reality, there could have been
196 * multiple opps since this function was last called and that affects
197 * the load calculation. While it's not perfectly accurate, this
198 * simplification is good enough and works. REVISIT this, as more
199 * complex code may be needed if experiments show that it's not
202 * Return: 0 on success, -E* if getting the static power failed.
204 static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
205 struct thermal_zone_device *tz,
211 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
212 struct cpufreq_policy *policy = cpufreq_cdev->policy;
213 u32 *load_cpu = NULL;
215 freq = cpufreq_quick_get(policy->cpu);
217 if (trace_thermal_power_cpu_get_power_enabled()) {
218 u32 ncpus = cpumask_weight(policy->related_cpus);
220 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
223 for_each_cpu(cpu, policy->related_cpus) {
227 load = get_load(cpufreq_cdev, cpu, i);
238 cpufreq_cdev->last_load = total_load;
240 *power = get_dynamic_power(cpufreq_cdev, freq);
243 trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
244 load_cpu, i, *power);
253 * cpufreq_state2power() - convert a cpu cdev state to power consumed
254 * @cdev: &thermal_cooling_device pointer
255 * @tz: a valid thermal zone device pointer
256 * @state: cooling device state to be converted
257 * @power: pointer in which to store the resulting power
259 * Convert cooling device state @state into power consumption in
260 * milliwatts assuming 100% load. Store the calculated power in
263 * Return: 0 on success, -EINVAL if the cooling device state could not
264 * be converted into a frequency or other -E* if there was an error
265 * when calculating the static power.
267 static int cpufreq_state2power(struct thermal_cooling_device *cdev,
268 struct thermal_zone_device *tz,
269 unsigned long state, u32 *power)
271 unsigned int freq, num_cpus, idx;
272 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
274 /* Request state should be less than max_level */
275 if (WARN_ON(state > cpufreq_cdev->max_level))
278 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
280 idx = cpufreq_cdev->max_level - state;
281 freq = cpufreq_cdev->em->table[idx].frequency;
282 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
288 * cpufreq_power2state() - convert power to a cooling device state
289 * @cdev: &thermal_cooling_device pointer
290 * @tz: a valid thermal zone device pointer
291 * @power: power in milliwatts to be converted
292 * @state: pointer in which to store the resulting state
294 * Calculate a cooling device state for the cpus described by @cdev
295 * that would allow them to consume at most @power mW and store it in
296 * @state. Note that this calculation depends on external factors
297 * such as the cpu load or the current static power. Calling this
298 * function with the same power as input can yield different cooling
299 * device states depending on those external factors.
301 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
302 * the calculated frequency could not be converted to a valid state.
303 * The latter should not happen unless the frequencies available to
304 * cpufreq have changed since the initialization of the cpu cooling
307 static int cpufreq_power2state(struct thermal_cooling_device *cdev,
308 struct thermal_zone_device *tz, u32 power,
309 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, to_cpumask(em->cpus))) {
336 pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n",
337 cpumask_pr_args(to_cpumask(em->cpus)),
338 cpumask_pr_args(policy->related_cpus));
342 nr_levels = cpufreq_cdev->max_level + 1;
343 if (em->nr_cap_states != nr_levels) {
344 pr_err("The number of cap states in pd %*pbl (%u) doesn't match the number of cooling levels (%u)\n",
345 cpumask_pr_args(to_cpumask(em->cpus)),
346 em->nr_cap_states, nr_levels);
352 #endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */
354 static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev,
357 struct cpufreq_policy *policy;
360 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
361 /* Use the Energy Model table if available */
362 if (cpufreq_cdev->em) {
363 idx = cpufreq_cdev->max_level - state;
364 return cpufreq_cdev->em->table[idx].frequency;
368 /* Otherwise, fallback on the CPUFreq table */
369 policy = cpufreq_cdev->policy;
370 if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
371 idx = cpufreq_cdev->max_level - state;
375 return policy->freq_table[idx].frequency;
378 /* cpufreq cooling device callback functions are defined below */
381 * cpufreq_get_max_state - callback function to get the max cooling state.
382 * @cdev: thermal cooling device pointer.
383 * @state: fill this variable with the max cooling state.
385 * Callback for the thermal cooling device to return the cpufreq
388 * Return: 0 on success, an error code otherwise.
390 static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
391 unsigned long *state)
393 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
395 *state = cpufreq_cdev->max_level;
400 * cpufreq_get_cur_state - callback function to get the current cooling state.
401 * @cdev: thermal cooling device pointer.
402 * @state: fill this variable with the current cooling state.
404 * Callback for the thermal cooling device to return the cpufreq
405 * current cooling state.
407 * Return: 0 on success, an error code otherwise.
409 static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
410 unsigned long *state)
412 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
414 *state = cpufreq_cdev->cpufreq_state;
420 * cpufreq_set_cur_state - callback function to set the current cooling state.
421 * @cdev: thermal cooling device pointer.
422 * @state: set this variable to the current cooling state.
424 * Callback for the thermal cooling device to change the cpufreq
425 * current cooling state.
427 * Return: 0 on success, an error code otherwise.
429 static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
432 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
434 /* Request state should be less than max_level */
435 if (WARN_ON(state > cpufreq_cdev->max_level))
438 /* Check if the old cooling action is same as new cooling action */
439 if (cpufreq_cdev->cpufreq_state == state)
442 cpufreq_cdev->cpufreq_state = state;
444 return freq_qos_update_request(&cpufreq_cdev->qos_req,
445 get_state_freq(cpufreq_cdev, state));
448 /* Bind cpufreq callbacks to thermal cooling device ops */
450 static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
451 .get_max_state = cpufreq_get_max_state,
452 .get_cur_state = cpufreq_get_cur_state,
453 .set_cur_state = cpufreq_set_cur_state,
457 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
458 * @np: a valid struct device_node to the cooling device device tree node
459 * @policy: cpufreq policy
460 * Normally this should be same as cpufreq policy->related_cpus.
461 * @em: Energy Model of the cpufreq policy
463 * This interface function registers the cpufreq cooling device with the name
464 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
465 * cooling devices. It also gives the opportunity to link the cooling device
466 * with a device tree node, in order to bind it via the thermal DT code.
468 * Return: a valid struct thermal_cooling_device pointer on success,
469 * on failure, it returns a corresponding ERR_PTR().
471 static struct thermal_cooling_device *
472 __cpufreq_cooling_register(struct device_node *np,
473 struct cpufreq_policy *policy,
474 struct em_perf_domain *em)
476 struct thermal_cooling_device *cdev;
477 struct cpufreq_cooling_device *cpufreq_cdev;
478 char dev_name[THERMAL_NAME_LENGTH];
479 unsigned int i, num_cpus;
482 struct thermal_cooling_device_ops *cooling_ops;
484 dev = get_cpu_device(policy->cpu);
485 if (unlikely(!dev)) {
486 pr_warn("No cpu device for cpu %d\n", policy->cpu);
487 return ERR_PTR(-ENODEV);
491 if (IS_ERR_OR_NULL(policy)) {
492 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
493 return ERR_PTR(-EINVAL);
496 i = cpufreq_table_count_valid_entries(policy);
498 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
500 return ERR_PTR(-ENODEV);
503 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
505 return ERR_PTR(-ENOMEM);
507 cpufreq_cdev->policy = policy;
508 num_cpus = cpumask_weight(policy->related_cpus);
509 cpufreq_cdev->idle_time = kcalloc(num_cpus,
510 sizeof(*cpufreq_cdev->idle_time),
512 if (!cpufreq_cdev->idle_time) {
513 cdev = ERR_PTR(-ENOMEM);
517 /* max_level is an index, not a counter */
518 cpufreq_cdev->max_level = i - 1;
520 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
525 cpufreq_cdev->id = ret;
527 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
530 cooling_ops = &cpufreq_cooling_ops;
532 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
533 if (em_is_sane(cpufreq_cdev, em)) {
534 cpufreq_cdev->em = em;
535 cooling_ops->get_requested_power = cpufreq_get_requested_power;
536 cooling_ops->state2power = cpufreq_state2power;
537 cooling_ops->power2state = cpufreq_power2state;
540 if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) {
541 pr_err("%s: unsorted frequency tables are not supported\n",
543 cdev = ERR_PTR(-EINVAL);
547 ret = freq_qos_add_request(&policy->constraints,
548 &cpufreq_cdev->qos_req, FREQ_QOS_MAX,
549 get_state_freq(cpufreq_cdev, 0));
551 pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
557 cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev,
562 mutex_lock(&cooling_list_lock);
563 list_add(&cpufreq_cdev->node, &cpufreq_cdev_list);
564 mutex_unlock(&cooling_list_lock);
569 freq_qos_remove_request(&cpufreq_cdev->qos_req);
571 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
573 kfree(cpufreq_cdev->idle_time);
580 * cpufreq_cooling_register - function to create cpufreq cooling device.
581 * @policy: cpufreq policy
583 * This interface function registers the cpufreq cooling device with the name
584 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
587 * Return: a valid struct thermal_cooling_device pointer on success,
588 * on failure, it returns a corresponding ERR_PTR().
590 struct thermal_cooling_device *
591 cpufreq_cooling_register(struct cpufreq_policy *policy)
593 return __cpufreq_cooling_register(NULL, policy, NULL);
595 EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
598 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
599 * @policy: cpufreq policy
601 * This interface function registers the cpufreq cooling device with the name
602 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
603 * cooling devices. Using this API, the cpufreq cooling device will be
604 * linked to the device tree node provided.
606 * Using this function, the cooling device will implement the power
607 * extensions by using a simple cpu power model. The cpus must have
608 * registered their OPPs using the OPP library.
610 * It also takes into account, if property present in policy CPU node, the
611 * static power consumed by the cpu.
613 * Return: a valid struct thermal_cooling_device pointer on success,
614 * and NULL on failure.
616 struct thermal_cooling_device *
617 of_cpufreq_cooling_register(struct cpufreq_policy *policy)
619 struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
620 struct thermal_cooling_device *cdev = NULL;
623 pr_err("cpu_cooling: OF node not available for cpu%d\n",
628 if (of_find_property(np, "#cooling-cells", NULL)) {
629 struct em_perf_domain *em = em_cpu_get(policy->cpu);
631 cdev = __cpufreq_cooling_register(np, policy, em);
633 pr_err("cpu_cooling: cpu%d failed to register as cooling device: %ld\n",
634 policy->cpu, PTR_ERR(cdev));
642 EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
645 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
646 * @cdev: thermal cooling device pointer.
648 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
650 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
652 struct cpufreq_cooling_device *cpufreq_cdev;
657 cpufreq_cdev = cdev->devdata;
659 mutex_lock(&cooling_list_lock);
660 list_del(&cpufreq_cdev->node);
661 mutex_unlock(&cooling_list_lock);
663 thermal_cooling_device_unregister(cdev);
664 freq_qos_remove_request(&cpufreq_cdev->qos_req);
665 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
666 kfree(cpufreq_cdev->idle_time);
669 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);