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.
13 #include <linux/cpu.h>
14 #include <linux/cpufreq.h>
15 #include <linux/cpu_cooling.h>
16 #include <linux/energy_model.h>
17 #include <linux/err.h>
18 #include <linux/export.h>
19 #include <linux/idr.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 * @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
66 * @qos_req: PM QoS contraint to apply
68 * This structure is required for keeping information of each registered
69 * cpufreq_cooling_device.
71 struct cpufreq_cooling_device {
74 unsigned int cpufreq_state;
75 unsigned int max_level;
76 struct em_perf_domain *em;
77 struct cpufreq_policy *policy;
78 struct list_head node;
79 struct time_in_idle *idle_time;
80 struct freq_qos_request qos_req;
83 static DEFINE_IDA(cpufreq_ida);
84 static DEFINE_MUTEX(cooling_list_lock);
85 static LIST_HEAD(cpufreq_cdev_list);
87 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
89 * get_level: Find the level for a particular frequency
90 * @cpufreq_cdev: cpufreq_cdev for which the property is required
93 * Return: level corresponding to the frequency.
95 static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
100 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
101 if (freq > cpufreq_cdev->em->table[i].frequency)
105 return cpufreq_cdev->max_level - i - 1;
108 static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
113 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
114 if (freq > cpufreq_cdev->em->table[i].frequency)
118 return cpufreq_cdev->em->table[i + 1].power;
121 static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
126 for (i = cpufreq_cdev->max_level; i >= 0; i--) {
127 if (power >= cpufreq_cdev->em->table[i].power)
131 return cpufreq_cdev->em->table[i].frequency;
135 * get_load() - get load for a cpu since last updated
136 * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu
138 * @cpu_idx: index of the cpu in time_in_idle*
140 * Return: The average load of cpu @cpu in percentage since this
141 * function was last called.
143 static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
147 u64 now, now_idle, delta_time, delta_idle;
148 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
150 now_idle = get_cpu_idle_time(cpu, &now, 0);
151 delta_idle = now_idle - idle_time->time;
152 delta_time = now - idle_time->timestamp;
154 if (delta_time <= delta_idle)
157 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
159 idle_time->time = now_idle;
160 idle_time->timestamp = now;
166 * get_dynamic_power() - calculate the dynamic power
167 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
168 * @freq: current frequency
170 * Return: the dynamic power consumed by the cpus described by
173 static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
178 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
179 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
183 * cpufreq_get_requested_power() - get the current power
184 * @cdev: &thermal_cooling_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,
210 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
211 struct cpufreq_policy *policy = cpufreq_cdev->policy;
212 u32 *load_cpu = NULL;
214 freq = cpufreq_quick_get(policy->cpu);
216 if (trace_thermal_power_cpu_get_power_enabled()) {
217 u32 ncpus = cpumask_weight(policy->related_cpus);
219 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
222 for_each_cpu(cpu, policy->related_cpus) {
226 load = get_load(cpufreq_cdev, cpu, i);
237 cpufreq_cdev->last_load = total_load;
239 *power = get_dynamic_power(cpufreq_cdev, freq);
242 trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
243 load_cpu, i, *power);
252 * cpufreq_state2power() - convert a cpu cdev state to power consumed
253 * @cdev: &thermal_cooling_device pointer
254 * @state: cooling device state to be converted
255 * @power: pointer in which to store the resulting power
257 * Convert cooling device state @state into power consumption in
258 * milliwatts assuming 100% load. Store the calculated power in
261 * Return: 0 on success, -EINVAL if the cooling device state could not
262 * be converted into a frequency or other -E* if there was an error
263 * when calculating the static power.
265 static int cpufreq_state2power(struct thermal_cooling_device *cdev,
266 unsigned long state, u32 *power)
268 unsigned int freq, num_cpus, idx;
269 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
271 /* Request state should be less than max_level */
272 if (state > cpufreq_cdev->max_level)
275 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
277 idx = cpufreq_cdev->max_level - state;
278 freq = cpufreq_cdev->em->table[idx].frequency;
279 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
285 * cpufreq_power2state() - convert power to a cooling device state
286 * @cdev: &thermal_cooling_device pointer
287 * @power: power in milliwatts to be converted
288 * @state: pointer in which to store the resulting state
290 * Calculate a cooling device state for the cpus described by @cdev
291 * that would allow them to consume at most @power mW and store it in
292 * @state. Note that this calculation depends on external factors
293 * such as the cpu load or the current static power. Calling this
294 * function with the same power as input can yield different cooling
295 * device states depending on those external factors.
297 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
298 * the calculated frequency could not be converted to a valid state.
299 * The latter should not happen unless the frequencies available to
300 * cpufreq have changed since the initialization of the cpu cooling
303 static int cpufreq_power2state(struct thermal_cooling_device *cdev,
304 u32 power, unsigned long *state)
306 unsigned int target_freq;
307 u32 last_load, normalised_power;
308 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
309 struct cpufreq_policy *policy = cpufreq_cdev->policy;
311 last_load = cpufreq_cdev->last_load ?: 1;
312 normalised_power = (power * 100) / last_load;
313 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
315 *state = get_level(cpufreq_cdev, target_freq);
316 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
321 static inline bool em_is_sane(struct cpufreq_cooling_device *cpufreq_cdev,
322 struct em_perf_domain *em) {
323 struct cpufreq_policy *policy;
324 unsigned int nr_levels;
329 policy = cpufreq_cdev->policy;
330 if (!cpumask_equal(policy->related_cpus, em_span_cpus(em))) {
331 pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n",
332 cpumask_pr_args(em_span_cpus(em)),
333 cpumask_pr_args(policy->related_cpus));
337 nr_levels = cpufreq_cdev->max_level + 1;
338 if (em_pd_nr_perf_states(em) != nr_levels) {
339 pr_err("The number of performance states in pd %*pbl (%u) doesn't match the number of cooling levels (%u)\n",
340 cpumask_pr_args(em_span_cpus(em)),
341 em_pd_nr_perf_states(em), nr_levels);
347 #endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */
349 static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev,
352 struct cpufreq_policy *policy;
355 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
356 /* Use the Energy Model table if available */
357 if (cpufreq_cdev->em) {
358 idx = cpufreq_cdev->max_level - state;
359 return cpufreq_cdev->em->table[idx].frequency;
363 /* Otherwise, fallback on the CPUFreq table */
364 policy = cpufreq_cdev->policy;
365 if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
366 idx = cpufreq_cdev->max_level - state;
370 return policy->freq_table[idx].frequency;
373 /* cpufreq cooling device callback functions are defined below */
376 * cpufreq_get_max_state - callback function to get the max cooling state.
377 * @cdev: thermal cooling device pointer.
378 * @state: fill this variable with the max cooling state.
380 * Callback for the thermal cooling device to return the cpufreq
383 * Return: 0 on success, an error code otherwise.
385 static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
386 unsigned long *state)
388 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
390 *state = cpufreq_cdev->max_level;
395 * cpufreq_get_cur_state - callback function to get the current cooling state.
396 * @cdev: thermal cooling device pointer.
397 * @state: fill this variable with the current cooling state.
399 * Callback for the thermal cooling device to return the cpufreq
400 * current cooling state.
402 * Return: 0 on success, an error code otherwise.
404 static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
405 unsigned long *state)
407 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
409 *state = cpufreq_cdev->cpufreq_state;
415 * cpufreq_set_cur_state - callback function to set the current cooling state.
416 * @cdev: thermal cooling device pointer.
417 * @state: set this variable to the current cooling state.
419 * Callback for the thermal cooling device to change the cpufreq
420 * current cooling state.
422 * Return: 0 on success, an error code otherwise.
424 static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
427 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
428 struct cpumask *cpus;
429 unsigned int frequency;
430 unsigned long max_capacity, capacity;
433 /* Request state should be less than max_level */
434 if (state > cpufreq_cdev->max_level)
437 /* Check if the old cooling action is same as new cooling action */
438 if (cpufreq_cdev->cpufreq_state == state)
441 cpufreq_cdev->cpufreq_state = state;
443 frequency = get_state_freq(cpufreq_cdev, state);
445 ret = freq_qos_update_request(&cpufreq_cdev->qos_req, frequency);
448 cpus = cpufreq_cdev->policy->cpus;
449 max_capacity = arch_scale_cpu_capacity(cpumask_first(cpus));
450 capacity = frequency * max_capacity;
451 capacity /= cpufreq_cdev->policy->cpuinfo.max_freq;
452 arch_set_thermal_pressure(cpus, max_capacity - capacity);
459 /* Bind cpufreq callbacks to thermal cooling device ops */
461 static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
462 .get_max_state = cpufreq_get_max_state,
463 .get_cur_state = cpufreq_get_cur_state,
464 .set_cur_state = cpufreq_set_cur_state,
468 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
469 * @np: a valid struct device_node to the cooling device device tree node
470 * @policy: cpufreq policy
471 * Normally this should be same as cpufreq policy->related_cpus.
472 * @em: Energy Model of the cpufreq policy
474 * This interface function registers the cpufreq cooling device with the name
475 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
476 * cooling devices. It also gives the opportunity to link the cooling device
477 * with a device tree node, in order to bind it via the thermal DT code.
479 * Return: a valid struct thermal_cooling_device pointer on success,
480 * on failure, it returns a corresponding ERR_PTR().
482 static struct thermal_cooling_device *
483 __cpufreq_cooling_register(struct device_node *np,
484 struct cpufreq_policy *policy,
485 struct em_perf_domain *em)
487 struct thermal_cooling_device *cdev;
488 struct cpufreq_cooling_device *cpufreq_cdev;
489 char dev_name[THERMAL_NAME_LENGTH];
490 unsigned int i, num_cpus;
493 struct thermal_cooling_device_ops *cooling_ops;
495 dev = get_cpu_device(policy->cpu);
496 if (unlikely(!dev)) {
497 pr_warn("No cpu device for cpu %d\n", policy->cpu);
498 return ERR_PTR(-ENODEV);
502 if (IS_ERR_OR_NULL(policy)) {
503 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
504 return ERR_PTR(-EINVAL);
507 i = cpufreq_table_count_valid_entries(policy);
509 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
511 return ERR_PTR(-ENODEV);
514 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
516 return ERR_PTR(-ENOMEM);
518 cpufreq_cdev->policy = policy;
519 num_cpus = cpumask_weight(policy->related_cpus);
520 cpufreq_cdev->idle_time = kcalloc(num_cpus,
521 sizeof(*cpufreq_cdev->idle_time),
523 if (!cpufreq_cdev->idle_time) {
524 cdev = ERR_PTR(-ENOMEM);
528 /* max_level is an index, not a counter */
529 cpufreq_cdev->max_level = i - 1;
531 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
536 cpufreq_cdev->id = ret;
538 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
541 cooling_ops = &cpufreq_cooling_ops;
543 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
544 if (em_is_sane(cpufreq_cdev, em)) {
545 cpufreq_cdev->em = em;
546 cooling_ops->get_requested_power = cpufreq_get_requested_power;
547 cooling_ops->state2power = cpufreq_state2power;
548 cooling_ops->power2state = cpufreq_power2state;
551 if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) {
552 pr_err("%s: unsorted frequency tables are not supported\n",
554 cdev = ERR_PTR(-EINVAL);
558 ret = freq_qos_add_request(&policy->constraints,
559 &cpufreq_cdev->qos_req, FREQ_QOS_MAX,
560 get_state_freq(cpufreq_cdev, 0));
562 pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
568 cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev,
573 mutex_lock(&cooling_list_lock);
574 list_add(&cpufreq_cdev->node, &cpufreq_cdev_list);
575 mutex_unlock(&cooling_list_lock);
580 freq_qos_remove_request(&cpufreq_cdev->qos_req);
582 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
584 kfree(cpufreq_cdev->idle_time);
591 * cpufreq_cooling_register - function to create cpufreq cooling device.
592 * @policy: cpufreq policy
594 * This interface function registers the cpufreq cooling device with the name
595 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
598 * Return: a valid struct thermal_cooling_device pointer on success,
599 * on failure, it returns a corresponding ERR_PTR().
601 struct thermal_cooling_device *
602 cpufreq_cooling_register(struct cpufreq_policy *policy)
604 return __cpufreq_cooling_register(NULL, policy, NULL);
606 EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
609 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
610 * @policy: cpufreq policy
612 * This interface function registers the cpufreq cooling device with the name
613 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
614 * cooling devices. Using this API, the cpufreq cooling device will be
615 * linked to the device tree node provided.
617 * Using this function, the cooling device will implement the power
618 * extensions by using a simple cpu power model. The cpus must have
619 * registered their OPPs using the OPP library.
621 * It also takes into account, if property present in policy CPU node, the
622 * static power consumed by the cpu.
624 * Return: a valid struct thermal_cooling_device pointer on success,
625 * and NULL on failure.
627 struct thermal_cooling_device *
628 of_cpufreq_cooling_register(struct cpufreq_policy *policy)
630 struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
631 struct thermal_cooling_device *cdev = NULL;
634 pr_err("cpufreq_cooling: OF node not available for cpu%d\n",
639 if (of_find_property(np, "#cooling-cells", NULL)) {
640 struct em_perf_domain *em = em_cpu_get(policy->cpu);
642 cdev = __cpufreq_cooling_register(np, policy, em);
644 pr_err("cpufreq_cooling: cpu%d failed to register as cooling device: %ld\n",
645 policy->cpu, PTR_ERR(cdev));
653 EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
656 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
657 * @cdev: thermal cooling device pointer.
659 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
661 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
663 struct cpufreq_cooling_device *cpufreq_cdev;
668 cpufreq_cdev = cdev->devdata;
670 mutex_lock(&cooling_list_lock);
671 list_del(&cpufreq_cdev->node);
672 mutex_unlock(&cooling_list_lock);
674 thermal_cooling_device_unregister(cdev);
675 freq_qos_remove_request(&cpufreq_cdev->qos_req);
676 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
677 kfree(cpufreq_cdev->idle_time);
680 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);