drm/nouveau: fix kernel-doc comments
[drm/drm-misc.git] / drivers / thermal / cpufreq_cooling.c
blob280071be30b1571f03fa8eeec7608e2290751ee0
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
24 #include <linux/units.h>
26 #include "thermal_trace.h"
29 * Cooling state <-> CPUFreq frequency
31 * Cooling states are translated to frequencies throughout this driver and this
32 * is the relation between them.
34 * Highest cooling state corresponds to lowest possible frequency.
36 * i.e.
37 * level 0 --> 1st Max Freq
38 * level 1 --> 2nd Max Freq
39 * ...
42 /**
43 * struct time_in_idle - Idle time stats
44 * @time: previous reading of the absolute time that this cpu was idle
45 * @timestamp: wall time of the last invocation of get_cpu_idle_time_us()
47 struct time_in_idle {
48 u64 time;
49 u64 timestamp;
52 /**
53 * struct cpufreq_cooling_device - data for cooling device with cpufreq
54 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
55 * @cpufreq_state: integer value representing the current state of cpufreq
56 * cooling devices.
57 * @max_level: maximum cooling level. One less than total number of valid
58 * cpufreq frequencies.
59 * @em: Reference on the Energy Model of the device
60 * @cdev: thermal_cooling_device pointer to keep track of the
61 * registered cooling device.
62 * @policy: cpufreq policy.
63 * @cooling_ops: cpufreq callbacks to thermal cooling device ops
64 * @idle_time: idle time stats
65 * @qos_req: PM QoS contraint to apply
67 * This structure is required for keeping information of each registered
68 * cpufreq_cooling_device.
70 struct cpufreq_cooling_device {
71 u32 last_load;
72 unsigned int cpufreq_state;
73 unsigned int max_level;
74 struct em_perf_domain *em;
75 struct cpufreq_policy *policy;
76 struct thermal_cooling_device_ops cooling_ops;
77 #ifndef CONFIG_SMP
78 struct time_in_idle *idle_time;
79 #endif
80 struct freq_qos_request qos_req;
83 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
84 /**
85 * get_level: Find the level for a particular frequency
86 * @cpufreq_cdev: cpufreq_cdev for which the property is required
87 * @freq: Frequency
89 * Return: level corresponding to the frequency.
91 static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
92 unsigned int freq)
94 struct em_perf_state *table;
95 int i;
97 rcu_read_lock();
98 table = em_perf_state_from_pd(cpufreq_cdev->em);
99 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
100 if (freq > table[i].frequency)
101 break;
103 rcu_read_unlock();
105 return cpufreq_cdev->max_level - i - 1;
108 static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
109 u32 freq)
111 struct em_perf_state *table;
112 unsigned long power_mw;
113 int i;
115 rcu_read_lock();
116 table = em_perf_state_from_pd(cpufreq_cdev->em);
117 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
118 if (freq > table[i].frequency)
119 break;
122 power_mw = table[i + 1].power;
123 power_mw /= MICROWATT_PER_MILLIWATT;
124 rcu_read_unlock();
126 return power_mw;
129 static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
130 u32 power)
132 struct em_perf_state *table;
133 unsigned long em_power_mw;
134 u32 freq;
135 int i;
137 rcu_read_lock();
138 table = em_perf_state_from_pd(cpufreq_cdev->em);
139 for (i = cpufreq_cdev->max_level; i > 0; i--) {
140 /* Convert EM power to milli-Watts to make safe comparison */
141 em_power_mw = table[i].power;
142 em_power_mw /= MICROWATT_PER_MILLIWATT;
143 if (power >= em_power_mw)
144 break;
146 freq = table[i].frequency;
147 rcu_read_unlock();
149 return freq;
153 * get_load() - get load for a cpu
154 * @cpufreq_cdev: struct cpufreq_cooling_device for the cpu
155 * @cpu: cpu number
156 * @cpu_idx: index of the cpu in time_in_idle array
158 * Return: The average load of cpu @cpu in percentage since this
159 * function was last called.
161 #ifdef CONFIG_SMP
162 static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
163 int cpu_idx)
165 unsigned long util = sched_cpu_util(cpu);
167 return (util * 100) / arch_scale_cpu_capacity(cpu);
169 #else /* !CONFIG_SMP */
170 static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
171 int cpu_idx)
173 u32 load;
174 u64 now, now_idle, delta_time, delta_idle;
175 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
177 now_idle = get_cpu_idle_time(cpu, &now, 0);
178 delta_idle = now_idle - idle_time->time;
179 delta_time = now - idle_time->timestamp;
181 if (delta_time <= delta_idle)
182 load = 0;
183 else
184 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
186 idle_time->time = now_idle;
187 idle_time->timestamp = now;
189 return load;
191 #endif /* CONFIG_SMP */
194 * get_dynamic_power() - calculate the dynamic power
195 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
196 * @freq: current frequency
198 * Return: the dynamic power consumed by the cpus described by
199 * @cpufreq_cdev.
201 static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
202 unsigned long freq)
204 u32 raw_cpu_power;
206 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
207 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
211 * cpufreq_get_requested_power() - get the current power
212 * @cdev: &thermal_cooling_device pointer
213 * @power: pointer in which to store the resulting power
215 * Calculate the current power consumption of the cpus in milliwatts
216 * and store it in @power. This function should actually calculate
217 * the requested power, but it's hard to get the frequency that
218 * cpufreq would have assigned if there were no thermal limits.
219 * Instead, we calculate the current power on the assumption that the
220 * immediate future will look like the immediate past.
222 * We use the current frequency and the average load since this
223 * function was last called. In reality, there could have been
224 * multiple opps since this function was last called and that affects
225 * the load calculation. While it's not perfectly accurate, this
226 * simplification is good enough and works. REVISIT this, as more
227 * complex code may be needed if experiments show that it's not
228 * accurate enough.
230 * Return: 0 on success, this function doesn't fail.
232 static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
233 u32 *power)
235 unsigned long freq;
236 int i = 0, cpu;
237 u32 total_load = 0;
238 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
239 struct cpufreq_policy *policy = cpufreq_cdev->policy;
241 freq = cpufreq_quick_get(policy->cpu);
243 for_each_cpu(cpu, policy->related_cpus) {
244 u32 load;
246 if (cpu_online(cpu))
247 load = get_load(cpufreq_cdev, cpu, i);
248 else
249 load = 0;
251 total_load += load;
254 cpufreq_cdev->last_load = total_load;
256 *power = get_dynamic_power(cpufreq_cdev, freq);
258 trace_thermal_power_cpu_get_power_simple(policy->cpu, *power);
260 return 0;
264 * cpufreq_state2power() - convert a cpu cdev state to power consumed
265 * @cdev: &thermal_cooling_device pointer
266 * @state: cooling device state to be converted
267 * @power: pointer in which to store the resulting power
269 * Convert cooling device state @state into power consumption in
270 * milliwatts assuming 100% load. Store the calculated power in
271 * @power.
273 * Return: 0 on success, -EINVAL if the cooling device state is bigger
274 * than maximum allowed.
276 static int cpufreq_state2power(struct thermal_cooling_device *cdev,
277 unsigned long state, u32 *power)
279 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
280 unsigned int freq, num_cpus, idx;
281 struct em_perf_state *table;
283 /* Request state should be less than max_level */
284 if (state > cpufreq_cdev->max_level)
285 return -EINVAL;
287 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
289 idx = cpufreq_cdev->max_level - state;
291 rcu_read_lock();
292 table = em_perf_state_from_pd(cpufreq_cdev->em);
293 freq = table[idx].frequency;
294 rcu_read_unlock();
296 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
298 return 0;
302 * cpufreq_power2state() - convert power to a cooling device state
303 * @cdev: &thermal_cooling_device pointer
304 * @power: power in milliwatts to be converted
305 * @state: pointer in which to store the resulting state
307 * Calculate a cooling device state for the cpus described by @cdev
308 * that would allow them to consume at most @power mW and store it in
309 * @state. Note that this calculation depends on external factors
310 * such as the CPUs load. Calling this function with the same power
311 * as input can yield different cooling device states depending on those
312 * external factors.
314 * Return: 0 on success, this function doesn't fail.
316 static int cpufreq_power2state(struct thermal_cooling_device *cdev,
317 u32 power, unsigned long *state)
319 unsigned int target_freq;
320 u32 last_load, normalised_power;
321 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
322 struct cpufreq_policy *policy = cpufreq_cdev->policy;
324 last_load = cpufreq_cdev->last_load ?: 1;
325 normalised_power = (power * 100) / last_load;
326 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
328 *state = get_level(cpufreq_cdev, target_freq);
329 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
330 power);
331 return 0;
334 static inline bool em_is_sane(struct cpufreq_cooling_device *cpufreq_cdev,
335 struct em_perf_domain *em) {
336 struct cpufreq_policy *policy;
337 unsigned int nr_levels;
339 if (!em || em_is_artificial(em))
340 return false;
342 policy = cpufreq_cdev->policy;
343 if (!cpumask_equal(policy->related_cpus, em_span_cpus(em))) {
344 pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n",
345 cpumask_pr_args(em_span_cpus(em)),
346 cpumask_pr_args(policy->related_cpus));
347 return false;
350 nr_levels = cpufreq_cdev->max_level + 1;
351 if (em_pd_nr_perf_states(em) != nr_levels) {
352 pr_err("The number of performance states in pd %*pbl (%u) doesn't match the number of cooling levels (%u)\n",
353 cpumask_pr_args(em_span_cpus(em)),
354 em_pd_nr_perf_states(em), nr_levels);
355 return false;
358 return true;
360 #endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */
362 #ifdef CONFIG_SMP
363 static inline int allocate_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
365 return 0;
368 static inline void free_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
371 #else
372 static int allocate_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
374 unsigned int num_cpus = cpumask_weight(cpufreq_cdev->policy->related_cpus);
376 cpufreq_cdev->idle_time = kcalloc(num_cpus,
377 sizeof(*cpufreq_cdev->idle_time),
378 GFP_KERNEL);
379 if (!cpufreq_cdev->idle_time)
380 return -ENOMEM;
382 return 0;
385 static void free_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
387 kfree(cpufreq_cdev->idle_time);
388 cpufreq_cdev->idle_time = NULL;
390 #endif /* CONFIG_SMP */
392 static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev,
393 unsigned long state)
395 struct cpufreq_policy *policy;
396 unsigned long idx;
398 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
399 /* Use the Energy Model table if available */
400 if (cpufreq_cdev->em) {
401 struct em_perf_state *table;
402 unsigned int freq;
404 idx = cpufreq_cdev->max_level - state;
406 rcu_read_lock();
407 table = em_perf_state_from_pd(cpufreq_cdev->em);
408 freq = table[idx].frequency;
409 rcu_read_unlock();
411 return freq;
413 #endif
415 /* Otherwise, fallback on the CPUFreq table */
416 policy = cpufreq_cdev->policy;
417 if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
418 idx = cpufreq_cdev->max_level - state;
419 else
420 idx = state;
422 return policy->freq_table[idx].frequency;
425 /* cpufreq cooling device callback functions are defined below */
428 * cpufreq_get_max_state - callback function to get the max cooling state.
429 * @cdev: thermal cooling device pointer.
430 * @state: fill this variable with the max cooling state.
432 * Callback for the thermal cooling device to return the cpufreq
433 * max cooling state.
435 * Return: 0 on success, this function doesn't fail.
437 static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
438 unsigned long *state)
440 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
442 *state = cpufreq_cdev->max_level;
443 return 0;
447 * cpufreq_get_cur_state - callback function to get the current cooling state.
448 * @cdev: thermal cooling device pointer.
449 * @state: fill this variable with the current cooling state.
451 * Callback for the thermal cooling device to return the cpufreq
452 * current cooling state.
454 * Return: 0 on success, this function doesn't fail.
456 static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
457 unsigned long *state)
459 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
461 *state = cpufreq_cdev->cpufreq_state;
463 return 0;
467 * cpufreq_set_cur_state - callback function to set the current cooling state.
468 * @cdev: thermal cooling device pointer.
469 * @state: set this variable to the current cooling state.
471 * Callback for the thermal cooling device to change the cpufreq
472 * current cooling state.
474 * Return: 0 on success, an error code otherwise.
476 static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
477 unsigned long state)
479 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
480 unsigned int frequency;
481 int ret;
483 /* Request state should be less than max_level */
484 if (state > cpufreq_cdev->max_level)
485 return -EINVAL;
487 /* Check if the old cooling action is same as new cooling action */
488 if (cpufreq_cdev->cpufreq_state == state)
489 return 0;
491 frequency = get_state_freq(cpufreq_cdev, state);
493 ret = freq_qos_update_request(&cpufreq_cdev->qos_req, frequency);
494 if (ret >= 0) {
495 cpufreq_cdev->cpufreq_state = state;
496 ret = 0;
499 return ret;
503 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
504 * @np: a valid struct device_node to the cooling device tree node
505 * @policy: cpufreq policy
506 * Normally this should be same as cpufreq policy->related_cpus.
507 * @em: Energy Model of the cpufreq policy
509 * This interface function registers the cpufreq cooling device with the name
510 * "cpufreq-%s". This API can support multiple instances of cpufreq
511 * cooling devices. It also gives the opportunity to link the cooling device
512 * with a device tree node, in order to bind it via the thermal DT code.
514 * Return: a valid struct thermal_cooling_device pointer on success,
515 * on failure, it returns a corresponding ERR_PTR().
517 static struct thermal_cooling_device *
518 __cpufreq_cooling_register(struct device_node *np,
519 struct cpufreq_policy *policy,
520 struct em_perf_domain *em)
522 struct thermal_cooling_device *cdev;
523 struct cpufreq_cooling_device *cpufreq_cdev;
524 unsigned int i;
525 struct device *dev;
526 int ret;
527 struct thermal_cooling_device_ops *cooling_ops;
528 char *name;
530 if (IS_ERR_OR_NULL(policy)) {
531 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
532 return ERR_PTR(-EINVAL);
535 dev = get_cpu_device(policy->cpu);
536 if (unlikely(!dev)) {
537 pr_warn("No cpu device for cpu %d\n", policy->cpu);
538 return ERR_PTR(-ENODEV);
541 i = cpufreq_table_count_valid_entries(policy);
542 if (!i) {
543 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
544 __func__);
545 return ERR_PTR(-ENODEV);
548 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
549 if (!cpufreq_cdev)
550 return ERR_PTR(-ENOMEM);
552 cpufreq_cdev->policy = policy;
554 ret = allocate_idle_time(cpufreq_cdev);
555 if (ret) {
556 cdev = ERR_PTR(ret);
557 goto free_cdev;
560 /* max_level is an index, not a counter */
561 cpufreq_cdev->max_level = i - 1;
563 cooling_ops = &cpufreq_cdev->cooling_ops;
564 cooling_ops->get_max_state = cpufreq_get_max_state;
565 cooling_ops->get_cur_state = cpufreq_get_cur_state;
566 cooling_ops->set_cur_state = cpufreq_set_cur_state;
568 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
569 if (em_is_sane(cpufreq_cdev, em)) {
570 cpufreq_cdev->em = em;
571 cooling_ops->get_requested_power = cpufreq_get_requested_power;
572 cooling_ops->state2power = cpufreq_state2power;
573 cooling_ops->power2state = cpufreq_power2state;
574 } else
575 #endif
576 if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) {
577 pr_err("%s: unsorted frequency tables are not supported\n",
578 __func__);
579 cdev = ERR_PTR(-EINVAL);
580 goto free_idle_time;
583 ret = freq_qos_add_request(&policy->constraints,
584 &cpufreq_cdev->qos_req, FREQ_QOS_MAX,
585 get_state_freq(cpufreq_cdev, 0));
586 if (ret < 0) {
587 pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
588 ret);
589 cdev = ERR_PTR(ret);
590 goto free_idle_time;
593 cdev = ERR_PTR(-ENOMEM);
594 name = kasprintf(GFP_KERNEL, "cpufreq-%s", dev_name(dev));
595 if (!name)
596 goto remove_qos_req;
598 cdev = thermal_of_cooling_device_register(np, name, cpufreq_cdev,
599 cooling_ops);
600 kfree(name);
602 if (IS_ERR(cdev))
603 goto remove_qos_req;
605 return cdev;
607 remove_qos_req:
608 freq_qos_remove_request(&cpufreq_cdev->qos_req);
609 free_idle_time:
610 free_idle_time(cpufreq_cdev);
611 free_cdev:
612 kfree(cpufreq_cdev);
613 return cdev;
617 * cpufreq_cooling_register - function to create cpufreq cooling device.
618 * @policy: cpufreq policy
620 * This interface function registers the cpufreq cooling device with the name
621 * "cpufreq-%s". This API can support multiple instances of cpufreq cooling
622 * devices.
624 * Return: a valid struct thermal_cooling_device pointer on success,
625 * on failure, it returns a corresponding ERR_PTR().
627 struct thermal_cooling_device *
628 cpufreq_cooling_register(struct cpufreq_policy *policy)
630 return __cpufreq_cooling_register(NULL, policy, NULL);
632 EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
635 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
636 * @policy: cpufreq policy
638 * This interface function registers the cpufreq cooling device with the name
639 * "cpufreq-%s". This API can support multiple instances of cpufreq cooling
640 * devices. Using this API, the cpufreq cooling device will be linked to the
641 * device tree node provided.
643 * Using this function, the cooling device will implement the power
644 * extensions by using the Energy Model (if present). The cpus must have
645 * registered their OPPs using the OPP library.
647 * Return: a valid struct thermal_cooling_device pointer on success,
648 * and NULL on failure.
650 struct thermal_cooling_device *
651 of_cpufreq_cooling_register(struct cpufreq_policy *policy)
653 struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
654 struct thermal_cooling_device *cdev = NULL;
656 if (!np) {
657 pr_err("cpufreq_cooling: OF node not available for cpu%d\n",
658 policy->cpu);
659 return NULL;
662 if (of_property_present(np, "#cooling-cells")) {
663 struct em_perf_domain *em = em_cpu_get(policy->cpu);
665 cdev = __cpufreq_cooling_register(np, policy, em);
666 if (IS_ERR(cdev)) {
667 pr_err("cpufreq_cooling: cpu%d failed to register as cooling device: %ld\n",
668 policy->cpu, PTR_ERR(cdev));
669 cdev = NULL;
673 of_node_put(np);
674 return cdev;
676 EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
679 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
680 * @cdev: thermal cooling device pointer.
682 * This interface function unregisters the "cpufreq-%x" cooling device.
684 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
686 struct cpufreq_cooling_device *cpufreq_cdev;
688 if (!cdev)
689 return;
691 cpufreq_cdev = cdev->devdata;
693 thermal_cooling_device_unregister(cdev);
694 freq_qos_remove_request(&cpufreq_cdev->qos_req);
695 free_idle_time(cpufreq_cdev);
696 kfree(cpufreq_cdev);
698 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);