blob: cc2959f22f01a90394dd06b7d51374615894b032 [file] [log] [blame]
Daniel Lezcano0fac9e2f2019-04-28 11:51:04 +02001// SPDX-License-Identifier: GPL-2.0
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05302/*
Daniel Lezcano23affa22019-12-19 23:53:17 +01003 * linux/drivers/thermal/cpufreq_cooling.c
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05304 *
5 * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05306 *
Daniel Lezcano42cd9b02019-04-28 11:51:03 +02007 * Copyright (C) 2012-2018 Linaro Limited.
8 *
9 * Authors: Amit Daniel <amit.kachhap@linaro.org>
10 * Viresh Kumar <viresh.kumar@linaro.org>
Viresh Kumar73904cb2014-12-04 09:42:08 +053011 *
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053012 */
Amit Kucheria5ccb4512020-05-11 17:54:57 +053013#include <linux/cpu.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053014#include <linux/cpufreq.h>
Amit Kucheria5ccb4512020-05-11 17:54:57 +053015#include <linux/cpu_cooling.h>
16#include <linux/energy_model.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053017#include <linux/err.h>
Amit Kucheriac65f83c2020-05-11 17:54:58 +053018#include <linux/export.h>
Matthew Wilcoxae606082016-12-21 09:47:05 -080019#include <linux/idr.h>
Javi Merinoc36cf072015-02-26 19:00:29 +000020#include <linux/pm_opp.h>
Viresh Kumar51308022019-07-23 11:44:02 +053021#include <linux/pm_qos.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053022#include <linux/slab.h>
Amit Kucheria5ccb4512020-05-11 17:54:57 +053023#include <linux/thermal.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053024
Javi Merino6828a472015-03-02 17:17:20 +000025#include <trace/events/thermal.h>
26
Viresh Kumar07d888d2014-12-04 09:41:49 +053027/*
28 * Cooling state <-> CPUFreq frequency
29 *
30 * Cooling states are translated to frequencies throughout this driver and this
31 * is the relation between them.
32 *
33 * Highest cooling state corresponds to lowest possible frequency.
34 *
35 * i.e.
36 * level 0 --> 1st Max Freq
37 * level 1 --> 2nd Max Freq
38 * ...
39 */
40
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053041/**
Viresh Kumar81ee14d2017-04-25 15:57:20 +053042 * 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()
45 */
46struct time_in_idle {
47 u64 time;
48 u64 timestamp;
49};
50
51/**
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000052 * struct cpufreq_cooling_device - data for cooling device with cpufreq
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053053 * @id: unique integer value corresponding to each cpufreq_cooling_device
54 * registered.
Viresh Kumard72b4012017-04-25 15:57:24 +053055 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053056 * @cpufreq_state: integer value representing the current state of cpufreq
57 * cooling devices.
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053058 * @max_level: maximum cooling level. One less than total number of valid
59 * cpufreq frequencies.
Quentin Perreta4e893e2019-10-30 15:14:51 +000060 * @em: Reference on the Energy Model of the device
Viresh Kumard72b4012017-04-25 15:57:24 +053061 * @cdev: thermal_cooling_device pointer to keep track of the
62 * registered cooling device.
63 * @policy: cpufreq policy.
Javi Merinofc4de352014-12-15 16:55:52 +000064 * @node: list_head to link all cpufreq_cooling_device together.
Viresh Kumar81ee14d2017-04-25 15:57:20 +053065 * @idle_time: idle time stats
Amit Kucheria7b4e7f02019-11-20 21:15:11 +053066 * @qos_req: PM QoS contraint to apply
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053067 *
Viresh Kumarbeca6052014-12-04 09:41:48 +053068 * This structure is required for keeping information of each registered
69 * cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053070 */
71struct cpufreq_cooling_device {
72 int id;
Viresh Kumard72b4012017-04-25 15:57:24 +053073 u32 last_load;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053074 unsigned int cpufreq_state;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053075 unsigned int max_level;
Quentin Perreta4e893e2019-10-30 15:14:51 +000076 struct em_perf_domain *em;
Viresh Kumard72b4012017-04-25 15:57:24 +053077 struct cpufreq_policy *policy;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053078 struct list_head node;
Viresh Kumar81ee14d2017-04-25 15:57:20 +053079 struct time_in_idle *idle_time;
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +020080 struct freq_qos_request qos_req;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053081};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053082
Viresh Kumarfb8ea302017-04-25 15:57:09 +053083static DEFINE_IDA(cpufreq_ida);
Russell King02373d72015-08-12 15:22:16 +053084static DEFINE_MUTEX(cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +053085static LIST_HEAD(cpufreq_cdev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053086
Quentin Perret5a4e5b72019-10-30 15:14:50 +000087#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053088/**
Viresh Kumar4843c4a2014-12-04 09:42:07 +053089 * get_level: Find the level for a particular frequency
Viresh Kumar1dea4322017-04-25 15:57:10 +053090 * @cpufreq_cdev: cpufreq_cdev for which the property is required
Viresh Kumar4843c4a2014-12-04 09:42:07 +053091 * @freq: Frequency
Eduardo Valentin82b9ee42013-04-17 17:12:00 +000092 *
Viresh Kumarda27f692017-04-25 15:57:21 +053093 * Return: level corresponding to the frequency.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053094 */
Viresh Kumar1dea4322017-04-25 15:57:10 +053095static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
Viresh Kumar4843c4a2014-12-04 09:42:07 +053096 unsigned int freq)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053097{
Quentin Perreta4e893e2019-10-30 15:14:51 +000098 int i;
Eduardo Valentin79491e52013-04-17 17:11:59 +000099
Quentin Perreta4e893e2019-10-30 15:14:51 +0000100 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
101 if (freq > cpufreq_cdev->em->table[i].frequency)
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530102 break;
Javi Merinoc36cf072015-02-26 19:00:29 +0000103 }
104
Quentin Perreta4e893e2019-10-30 15:14:51 +0000105 return cpufreq_cdev->max_level - i - 1;
Javi Merinoc36cf072015-02-26 19:00:29 +0000106}
107
Viresh Kumar1dea4322017-04-25 15:57:10 +0530108static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000109 u32 freq)
110{
111 int i;
Javi Merinoc36cf072015-02-26 19:00:29 +0000112
Quentin Perreta4e893e2019-10-30 15:14:51 +0000113 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
114 if (freq > cpufreq_cdev->em->table[i].frequency)
Javi Merinoc36cf072015-02-26 19:00:29 +0000115 break;
Quentin Perreta4e893e2019-10-30 15:14:51 +0000116 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000117
Quentin Perreta4e893e2019-10-30 15:14:51 +0000118 return cpufreq_cdev->em->table[i + 1].power;
Javi Merinoc36cf072015-02-26 19:00:29 +0000119}
120
Viresh Kumar1dea4322017-04-25 15:57:10 +0530121static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000122 u32 power)
123{
124 int i;
Javi Merinoc36cf072015-02-26 19:00:29 +0000125
Finley Xiao371a3bc2020-06-19 17:08:25 +0800126 for (i = cpufreq_cdev->max_level; i >= 0; i--) {
127 if (power >= cpufreq_cdev->em->table[i].power)
Javi Merinoc36cf072015-02-26 19:00:29 +0000128 break;
Quentin Perreta4e893e2019-10-30 15:14:51 +0000129 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000130
Finley Xiao371a3bc2020-06-19 17:08:25 +0800131 return cpufreq_cdev->em->table[i].frequency;
Javi Merinoc36cf072015-02-26 19:00:29 +0000132}
133
134/**
135 * get_load() - get load for a cpu since last updated
Viresh Kumar1dea4322017-04-25 15:57:10 +0530136 * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu
Javi Merinoc36cf072015-02-26 19:00:29 +0000137 * @cpu: cpu number
Viresh Kumarba76dd92017-04-25 15:57:18 +0530138 * @cpu_idx: index of the cpu in time_in_idle*
Javi Merinoc36cf072015-02-26 19:00:29 +0000139 *
140 * Return: The average load of cpu @cpu in percentage since this
141 * function was last called.
142 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530143static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
Javi Merinoa53b8392016-02-11 12:00:51 +0000144 int cpu_idx)
Javi Merinoc36cf072015-02-26 19:00:29 +0000145{
146 u32 load;
147 u64 now, now_idle, delta_time, delta_idle;
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530148 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
Javi Merinoc36cf072015-02-26 19:00:29 +0000149
150 now_idle = get_cpu_idle_time(cpu, &now, 0);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530151 delta_idle = now_idle - idle_time->time;
152 delta_time = now - idle_time->timestamp;
Javi Merinoc36cf072015-02-26 19:00:29 +0000153
154 if (delta_time <= delta_idle)
155 load = 0;
156 else
157 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
158
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530159 idle_time->time = now_idle;
160 idle_time->timestamp = now;
Javi Merinoc36cf072015-02-26 19:00:29 +0000161
162 return load;
163}
164
165/**
Javi Merinoc36cf072015-02-26 19:00:29 +0000166 * get_dynamic_power() - calculate the dynamic power
Viresh Kumar1dea4322017-04-25 15:57:10 +0530167 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
Javi Merinoc36cf072015-02-26 19:00:29 +0000168 * @freq: current frequency
169 *
170 * Return: the dynamic power consumed by the cpus described by
Viresh Kumar1dea4322017-04-25 15:57:10 +0530171 * @cpufreq_cdev.
Javi Merinoc36cf072015-02-26 19:00:29 +0000172 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530173static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000174 unsigned long freq)
175{
176 u32 raw_cpu_power;
177
Viresh Kumar1dea4322017-04-25 15:57:10 +0530178 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
179 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530180}
181
Javi Merinoc36cf072015-02-26 19:00:29 +0000182/**
183 * cpufreq_get_requested_power() - get the current power
184 * @cdev: &thermal_cooling_device pointer
Javi Merinoc36cf072015-02-26 19:00:29 +0000185 * @power: pointer in which to store the resulting power
186 *
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.
193 *
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
200 * accurate enough.
201 *
202 * Return: 0 on success, -E* if getting the static power failed.
203 */
204static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000205 u32 *power)
206{
207 unsigned long freq;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530208 int i = 0, cpu;
209 u32 total_load = 0;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530210 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530211 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merino6828a472015-03-02 17:17:20 +0000212 u32 *load_cpu = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000213
Viresh Kumarba76dd92017-04-25 15:57:18 +0530214 freq = cpufreq_quick_get(policy->cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000215
Javi Merino6828a472015-03-02 17:17:20 +0000216 if (trace_thermal_power_cpu_get_power_enabled()) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530217 u32 ncpus = cpumask_weight(policy->related_cpus);
Javi Merino6828a472015-03-02 17:17:20 +0000218
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530219 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
Javi Merino6828a472015-03-02 17:17:20 +0000220 }
221
Viresh Kumarba76dd92017-04-25 15:57:18 +0530222 for_each_cpu(cpu, policy->related_cpus) {
Javi Merinoc36cf072015-02-26 19:00:29 +0000223 u32 load;
224
225 if (cpu_online(cpu))
Viresh Kumar1dea4322017-04-25 15:57:10 +0530226 load = get_load(cpufreq_cdev, cpu, i);
Javi Merinoc36cf072015-02-26 19:00:29 +0000227 else
228 load = 0;
229
230 total_load += load;
Matthias Kaehlckebf45ac12019-05-02 11:32:38 -0700231 if (load_cpu)
Javi Merino6828a472015-03-02 17:17:20 +0000232 load_cpu[i] = load;
233
234 i++;
Javi Merinoc36cf072015-02-26 19:00:29 +0000235 }
236
Viresh Kumar1dea4322017-04-25 15:57:10 +0530237 cpufreq_cdev->last_load = total_load;
Javi Merinoc36cf072015-02-26 19:00:29 +0000238
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530239 *power = get_dynamic_power(cpufreq_cdev, freq);
Javi Merino6828a472015-03-02 17:17:20 +0000240
241 if (load_cpu) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530242 trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530243 load_cpu, i, *power);
Javi Merino6828a472015-03-02 17:17:20 +0000244
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530245 kfree(load_cpu);
Javi Merino6828a472015-03-02 17:17:20 +0000246 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000247
Javi Merinoc36cf072015-02-26 19:00:29 +0000248 return 0;
249}
250
251/**
252 * cpufreq_state2power() - convert a cpu cdev state to power consumed
253 * @cdev: &thermal_cooling_device pointer
Javi Merinoc36cf072015-02-26 19:00:29 +0000254 * @state: cooling device state to be converted
255 * @power: pointer in which to store the resulting power
256 *
257 * Convert cooling device state @state into power consumption in
258 * milliwatts assuming 100% load. Store the calculated power in
259 * @power.
260 *
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.
264 */
265static int cpufreq_state2power(struct thermal_cooling_device *cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000266 unsigned long state, u32 *power)
267{
Quentin Perreta4e893e2019-10-30 15:14:51 +0000268 unsigned int freq, num_cpus, idx;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530269 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Javi Merinoc36cf072015-02-26 19:00:29 +0000270
Viresh Kumarcb1b6312017-04-25 15:57:23 +0530271 /* Request state should be less than max_level */
Daniel Lezcano40ea5682020-03-21 20:31:07 +0100272 if (state > cpufreq_cdev->max_level)
Viresh Kumarcb1b6312017-04-25 15:57:23 +0530273 return -EINVAL;
274
Viresh Kumarba76dd92017-04-25 15:57:18 +0530275 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
Javi Merinoc36cf072015-02-26 19:00:29 +0000276
Quentin Perreta4e893e2019-10-30 15:14:51 +0000277 idx = cpufreq_cdev->max_level - state;
278 freq = cpufreq_cdev->em->table[idx].frequency;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530279 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
Javi Merinoc36cf072015-02-26 19:00:29 +0000280
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530281 return 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000282}
283
284/**
285 * cpufreq_power2state() - convert power to a cooling device state
286 * @cdev: &thermal_cooling_device pointer
Javi Merinoc36cf072015-02-26 19:00:29 +0000287 * @power: power in milliwatts to be converted
288 * @state: pointer in which to store the resulting state
289 *
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.
296 *
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
301 * device.
302 */
303static int cpufreq_power2state(struct thermal_cooling_device *cdev,
zhuguangqingecd1d2a2020-09-14 15:11:01 +0800304 u32 power, unsigned long *state)
Javi Merinoc36cf072015-02-26 19:00:29 +0000305{
Shaokun Zhange0fda732019-02-18 14:22:30 +0800306 unsigned int target_freq;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530307 u32 last_load, normalised_power;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530308 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530309 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merinoc36cf072015-02-26 19:00:29 +0000310
Viresh Kumar1dea4322017-04-25 15:57:10 +0530311 last_load = cpufreq_cdev->last_load ?: 1;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530312 normalised_power = (power * 100) / last_load;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530313 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000314
Viresh Kumar3e08b2d2017-04-25 15:57:12 +0530315 *state = get_level(cpufreq_cdev, target_freq);
Viresh Kumarba76dd92017-04-25 15:57:18 +0530316 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
317 power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000318 return 0;
319}
Quentin Perreta4e893e2019-10-30 15:14:51 +0000320
321static 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;
325
326 if (!em)
327 return false;
328
329 policy = cpufreq_cdev->policy;
Lukasz Luba521b5122020-05-27 10:58:47 +0100330 if (!cpumask_equal(policy->related_cpus, em_span_cpus(em))) {
Quentin Perreta4e893e2019-10-30 15:14:51 +0000331 pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n",
Lukasz Luba521b5122020-05-27 10:58:47 +0100332 cpumask_pr_args(em_span_cpus(em)),
Quentin Perreta4e893e2019-10-30 15:14:51 +0000333 cpumask_pr_args(policy->related_cpus));
334 return false;
335 }
336
337 nr_levels = cpufreq_cdev->max_level + 1;
Lukasz Luba521b5122020-05-27 10:58:47 +0100338 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);
Quentin Perreta4e893e2019-10-30 15:14:51 +0000342 return false;
343 }
344
345 return true;
346}
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000347#endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */
348
Quentin Perreta4e893e2019-10-30 15:14:51 +0000349static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev,
350 unsigned long state)
351{
352 struct cpufreq_policy *policy;
353 unsigned long idx;
354
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;
360 }
361#endif
362
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;
367 else
368 idx = state;
369
370 return policy->freq_table[idx].frequency;
371}
372
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000373/* cpufreq cooling device callback functions are defined below */
374
375/**
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.
379 *
380 * Callback for the thermal cooling device to return the cpufreq
381 * max cooling state.
382 *
383 * Return: 0 on success, an error code otherwise.
384 */
385static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
386 unsigned long *state)
387{
388 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
389
390 *state = cpufreq_cdev->max_level;
391 return 0;
392}
393
394/**
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.
398 *
399 * Callback for the thermal cooling device to return the cpufreq
400 * current cooling state.
401 *
402 * Return: 0 on success, an error code otherwise.
403 */
404static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
405 unsigned long *state)
406{
407 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
408
409 *state = cpufreq_cdev->cpufreq_state;
410
411 return 0;
412}
413
414/**
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.
418 *
419 * Callback for the thermal cooling device to change the cpufreq
420 * current cooling state.
421 *
422 * Return: 0 on success, an error code otherwise.
423 */
424static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
425 unsigned long state)
426{
427 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Thara Gopinathf12e4f62020-02-21 19:52:12 -0500428 struct cpumask *cpus;
429 unsigned int frequency;
430 unsigned long max_capacity, capacity;
431 int ret;
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000432
433 /* Request state should be less than max_level */
Daniel Lezcano40ea5682020-03-21 20:31:07 +0100434 if (state > cpufreq_cdev->max_level)
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000435 return -EINVAL;
436
437 /* Check if the old cooling action is same as new cooling action */
438 if (cpufreq_cdev->cpufreq_state == state)
439 return 0;
440
441 cpufreq_cdev->cpufreq_state = state;
442
Thara Gopinathf12e4f62020-02-21 19:52:12 -0500443 frequency = get_state_freq(cpufreq_cdev, state);
444
445 ret = freq_qos_update_request(&cpufreq_cdev->qos_req, frequency);
446
447 if (ret > 0) {
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);
Linus Torvalds34183dd2020-04-07 20:00:16 -0700453 ret = 0;
Thara Gopinathf12e4f62020-02-21 19:52:12 -0500454 }
455
456 return ret;
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000457}
Javi Merinoc36cf072015-02-26 19:00:29 +0000458
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530459/* Bind cpufreq callbacks to thermal cooling device ops */
Brendan Jackmana305a432016-08-17 16:14:59 +0100460
Javi Merinoc36cf072015-02-26 19:00:29 +0000461static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
Brendan Jackmana305a432016-08-17 16:14:59 +0100462 .get_max_state = cpufreq_get_max_state,
463 .get_cur_state = cpufreq_get_cur_state,
464 .set_cur_state = cpufreq_set_cur_state,
Brendan Jackmana305a432016-08-17 16:14:59 +0100465};
466
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530467/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400468 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
469 * @np: a valid struct device_node to the cooling device device tree node
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530470 * @policy: cpufreq policy
Viresh Kumar405fb822014-12-04 09:41:55 +0530471 * Normally this should be same as cpufreq policy->related_cpus.
Quentin Perreta4e893e2019-10-30 15:14:51 +0000472 * @em: Energy Model of the cpufreq policy
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000473 *
474 * This interface function registers the cpufreq cooling device with the name
475 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400476 * 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.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000478 *
479 * Return: a valid struct thermal_cooling_device pointer on success,
480 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530481 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400482static struct thermal_cooling_device *
483__cpufreq_cooling_register(struct device_node *np,
Quentin Perreta4e893e2019-10-30 15:14:51 +0000484 struct cpufreq_policy *policy,
485 struct em_perf_domain *em)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530486{
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530487 struct thermal_cooling_device *cdev;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530488 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530489 char dev_name[THERMAL_NAME_LENGTH];
Quentin Perreta4e893e2019-10-30 15:14:51 +0000490 unsigned int i, num_cpus;
Viresh Kumar51308022019-07-23 11:44:02 +0530491 struct device *dev;
Viresh Kumar405fb822014-12-04 09:41:55 +0530492 int ret;
Brendan Jackmana305a432016-08-17 16:14:59 +0100493 struct thermal_cooling_device_ops *cooling_ops;
Viresh Kumar51308022019-07-23 11:44:02 +0530494
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);
499 }
500
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530501
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530502 if (IS_ERR_OR_NULL(policy)) {
Arvind Yadavb2fd7082017-10-24 13:20:39 +0530503 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530504 return ERR_PTR(-EINVAL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530505 }
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530506
Viresh Kumar55d85292017-04-25 15:57:15 +0530507 i = cpufreq_table_count_valid_entries(policy);
508 if (!i) {
509 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
510 __func__);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530511 return ERR_PTR(-ENODEV);
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530512 }
513
Viresh Kumar1dea4322017-04-25 15:57:10 +0530514 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530515 if (!cpufreq_cdev)
516 return ERR_PTR(-ENOMEM);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530517
Viresh Kumarb12b6512017-04-25 15:57:16 +0530518 cpufreq_cdev->policy = policy;
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530519 num_cpus = cpumask_weight(policy->related_cpus);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530520 cpufreq_cdev->idle_time = kcalloc(num_cpus,
521 sizeof(*cpufreq_cdev->idle_time),
522 GFP_KERNEL);
523 if (!cpufreq_cdev->idle_time) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530524 cdev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +0000525 goto free_cdev;
526 }
527
Viresh Kumar55d85292017-04-25 15:57:15 +0530528 /* max_level is an index, not a counter */
529 cpufreq_cdev->max_level = i - 1;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530530
Matthew Wilcoxae606082016-12-21 09:47:05 -0800531 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
532 if (ret < 0) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530533 cdev = ERR_PTR(ret);
Quentin Perreta4e893e2019-10-30 15:14:51 +0000534 goto free_idle_time;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530535 }
Viresh Kumar1dea4322017-04-25 15:57:10 +0530536 cpufreq_cdev->id = ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530537
Viresh Kumar349d39d2017-04-25 15:57:19 +0530538 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
539 cpufreq_cdev->id);
540
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000541 cooling_ops = &cpufreq_cooling_ops;
542
543#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
Quentin Perreta4e893e2019-10-30 15:14:51 +0000544 if (em_is_sane(cpufreq_cdev, em)) {
545 cpufreq_cdev->em = em;
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000546 cooling_ops->get_requested_power = cpufreq_get_requested_power;
547 cooling_ops->state2power = cpufreq_state2power;
548 cooling_ops->power2state = cpufreq_power2state;
Quentin Perreta4e893e2019-10-30 15:14:51 +0000549 } else
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000550#endif
Quentin Perreta4e893e2019-10-30 15:14:51 +0000551 if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) {
552 pr_err("%s: unsorted frequency tables are not supported\n",
553 __func__);
554 cdev = ERR_PTR(-EINVAL);
555 goto remove_ida;
556 }
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100557
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +0200558 ret = freq_qos_add_request(&policy->constraints,
559 &cpufreq_cdev->qos_req, FREQ_QOS_MAX,
Quentin Perreta4e893e2019-10-30 15:14:51 +0000560 get_state_freq(cpufreq_cdev, 0));
Viresh Kumar51308022019-07-23 11:44:02 +0530561 if (ret < 0) {
562 pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
563 ret);
564 cdev = ERR_PTR(ret);
565 goto remove_ida;
566 }
567
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530568 cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev,
569 cooling_ops);
570 if (IS_ERR(cdev))
Viresh Kumar51308022019-07-23 11:44:02 +0530571 goto remove_qos_req;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530572
Russell King02373d72015-08-12 15:22:16 +0530573 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530574 list_add(&cpufreq_cdev->node, &cpufreq_cdev_list);
Matthew Wilcox088db932017-03-10 18:33:28 +0000575 mutex_unlock(&cooling_list_lock);
576
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530577 return cdev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530578
Viresh Kumar51308022019-07-23 11:44:02 +0530579remove_qos_req:
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +0200580 freq_qos_remove_request(&cpufreq_cdev->qos_req);
Matthew Wilcoxae606082016-12-21 09:47:05 -0800581remove_ida:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530582 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530583free_idle_time:
584 kfree(cpufreq_cdev->idle_time);
Viresh Kumar730abe02014-12-04 09:41:58 +0530585free_cdev:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530586 kfree(cpufreq_cdev);
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530587 return cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530588}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400589
590/**
591 * cpufreq_cooling_register - function to create cpufreq cooling device.
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530592 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400593 *
594 * This interface function registers the cpufreq cooling device with the name
595 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
596 * cooling devices.
597 *
598 * Return: a valid struct thermal_cooling_device pointer on success,
599 * on failure, it returns a corresponding ERR_PTR().
600 */
601struct thermal_cooling_device *
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530602cpufreq_cooling_register(struct cpufreq_policy *policy)
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400603{
Quentin Perreta4e893e2019-10-30 15:14:51 +0000604 return __cpufreq_cooling_register(NULL, policy, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400605}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000606EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530607
608/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400609 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530610 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400611 *
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.
616 *
Javi Merinoc36cf072015-02-26 19:00:29 +0000617 * 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.
620 *
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530621 * It also takes into account, if property present in policy CPU node, the
622 * static power consumed by the cpu.
Javi Merinoc36cf072015-02-26 19:00:29 +0000623 *
624 * Return: a valid struct thermal_cooling_device pointer on success,
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530625 * and NULL on failure.
Javi Merinoc36cf072015-02-26 19:00:29 +0000626 */
627struct thermal_cooling_device *
Viresh Kumar3ebb62f2017-12-05 11:02:45 +0530628of_cpufreq_cooling_register(struct cpufreq_policy *policy)
Javi Merinoc36cf072015-02-26 19:00:29 +0000629{
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530630 struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
631 struct thermal_cooling_device *cdev = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000632
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530633 if (!np) {
Daniel Lezcano23affa22019-12-19 23:53:17 +0100634 pr_err("cpufreq_cooling: OF node not available for cpu%d\n",
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530635 policy->cpu);
636 return NULL;
637 }
638
639 if (of_find_property(np, "#cooling-cells", NULL)) {
Quentin Perreta4e893e2019-10-30 15:14:51 +0000640 struct em_perf_domain *em = em_cpu_get(policy->cpu);
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530641
Quentin Perreta4e893e2019-10-30 15:14:51 +0000642 cdev = __cpufreq_cooling_register(np, policy, em);
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530643 if (IS_ERR(cdev)) {
Daniel Lezcano23affa22019-12-19 23:53:17 +0100644 pr_err("cpufreq_cooling: cpu%d failed to register as cooling device: %ld\n",
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530645 policy->cpu, PTR_ERR(cdev));
646 cdev = NULL;
647 }
648 }
649
650 of_node_put(np);
651 return cdev;
Javi Merinoc36cf072015-02-26 19:00:29 +0000652}
Viresh Kumar3ebb62f2017-12-05 11:02:45 +0530653EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
Javi Merinoc36cf072015-02-26 19:00:29 +0000654
655/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530656 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
657 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000658 *
659 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530660 */
661void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
662{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530663 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530664
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400665 if (!cdev)
666 return;
667
Viresh Kumar1dea4322017-04-25 15:57:10 +0530668 cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530669
Matthew Wilcoxae606082016-12-21 09:47:05 -0800670 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530671 list_del(&cpufreq_cdev->node);
Matthew Wilcox088db932017-03-10 18:33:28 +0000672 mutex_unlock(&cooling_list_lock);
673
Daniel Lezcano72554a72019-04-28 11:51:05 +0200674 thermal_cooling_device_unregister(cdev);
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +0200675 freq_qos_remove_request(&cpufreq_cdev->qos_req);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530676 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530677 kfree(cpufreq_cdev->idle_time);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530678 kfree(cpufreq_cdev);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530679}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000680EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);