blob: d66791a71320319ee5db026bdf3e895752d274ae [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 Daniel Kachhap02361412012-08-16 17:11:40 +053013#include <linux/module.h>
14#include <linux/thermal.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053015#include <linux/cpufreq.h>
16#include <linux/err.h>
Matthew Wilcoxae606082016-12-21 09:47:05 -080017#include <linux/idr.h>
Javi Merinoc36cf072015-02-26 19:00:29 +000018#include <linux/pm_opp.h>
Viresh Kumar51308022019-07-23 11:44:02 +053019#include <linux/pm_qos.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053020#include <linux/slab.h>
21#include <linux/cpu.h>
22#include <linux/cpu_cooling.h>
Quentin Perreta4e893e2019-10-30 15:14:51 +000023#include <linux/energy_model.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
Quentin Perreta4e893e2019-10-30 15:14:51 +0000126 for (i = cpufreq_cdev->max_level - 1; 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
Quentin Perreta4e893e2019-10-30 15:14:51 +0000131 return cpufreq_cdev->em->table[i + 1].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
185 * @tz: a valid thermal zone device pointer
186 * @power: pointer in which to store the resulting power
187 *
188 * Calculate the current power consumption of the cpus in milliwatts
189 * and store it in @power. This function should actually calculate
190 * the requested power, but it's hard to get the frequency that
191 * cpufreq would have assigned if there were no thermal limits.
192 * Instead, we calculate the current power on the assumption that the
193 * immediate future will look like the immediate past.
194 *
195 * We use the current frequency and the average load since this
196 * function was last called. In reality, there could have been
197 * multiple opps since this function was last called and that affects
198 * the load calculation. While it's not perfectly accurate, this
199 * simplification is good enough and works. REVISIT this, as more
200 * complex code may be needed if experiments show that it's not
201 * accurate enough.
202 *
203 * Return: 0 on success, -E* if getting the static power failed.
204 */
205static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
206 struct thermal_zone_device *tz,
207 u32 *power)
208{
209 unsigned long freq;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530210 int i = 0, cpu;
211 u32 total_load = 0;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530212 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530213 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merino6828a472015-03-02 17:17:20 +0000214 u32 *load_cpu = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000215
Viresh Kumarba76dd92017-04-25 15:57:18 +0530216 freq = cpufreq_quick_get(policy->cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000217
Javi Merino6828a472015-03-02 17:17:20 +0000218 if (trace_thermal_power_cpu_get_power_enabled()) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530219 u32 ncpus = cpumask_weight(policy->related_cpus);
Javi Merino6828a472015-03-02 17:17:20 +0000220
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530221 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
Javi Merino6828a472015-03-02 17:17:20 +0000222 }
223
Viresh Kumarba76dd92017-04-25 15:57:18 +0530224 for_each_cpu(cpu, policy->related_cpus) {
Javi Merinoc36cf072015-02-26 19:00:29 +0000225 u32 load;
226
227 if (cpu_online(cpu))
Viresh Kumar1dea4322017-04-25 15:57:10 +0530228 load = get_load(cpufreq_cdev, cpu, i);
Javi Merinoc36cf072015-02-26 19:00:29 +0000229 else
230 load = 0;
231
232 total_load += load;
Matthias Kaehlckebf45ac12019-05-02 11:32:38 -0700233 if (load_cpu)
Javi Merino6828a472015-03-02 17:17:20 +0000234 load_cpu[i] = load;
235
236 i++;
Javi Merinoc36cf072015-02-26 19:00:29 +0000237 }
238
Viresh Kumar1dea4322017-04-25 15:57:10 +0530239 cpufreq_cdev->last_load = total_load;
Javi Merinoc36cf072015-02-26 19:00:29 +0000240
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530241 *power = get_dynamic_power(cpufreq_cdev, freq);
Javi Merino6828a472015-03-02 17:17:20 +0000242
243 if (load_cpu) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530244 trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530245 load_cpu, i, *power);
Javi Merino6828a472015-03-02 17:17:20 +0000246
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530247 kfree(load_cpu);
Javi Merino6828a472015-03-02 17:17:20 +0000248 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000249
Javi Merinoc36cf072015-02-26 19:00:29 +0000250 return 0;
251}
252
253/**
254 * cpufreq_state2power() - convert a cpu cdev state to power consumed
255 * @cdev: &thermal_cooling_device pointer
256 * @tz: a valid thermal zone device pointer
257 * @state: cooling device state to be converted
258 * @power: pointer in which to store the resulting power
259 *
260 * Convert cooling device state @state into power consumption in
261 * milliwatts assuming 100% load. Store the calculated power in
262 * @power.
263 *
264 * Return: 0 on success, -EINVAL if the cooling device state could not
265 * be converted into a frequency or other -E* if there was an error
266 * when calculating the static power.
267 */
268static int cpufreq_state2power(struct thermal_cooling_device *cdev,
269 struct thermal_zone_device *tz,
270 unsigned long state, u32 *power)
271{
Quentin Perreta4e893e2019-10-30 15:14:51 +0000272 unsigned int freq, num_cpus, idx;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530273 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Javi Merinoc36cf072015-02-26 19:00:29 +0000274
Viresh Kumarcb1b6312017-04-25 15:57:23 +0530275 /* Request state should be less than max_level */
Daniel Lezcano40ea5682020-03-21 20:31:07 +0100276 if (state > cpufreq_cdev->max_level)
Viresh Kumarcb1b6312017-04-25 15:57:23 +0530277 return -EINVAL;
278
Viresh Kumarba76dd92017-04-25 15:57:18 +0530279 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
Javi Merinoc36cf072015-02-26 19:00:29 +0000280
Quentin Perreta4e893e2019-10-30 15:14:51 +0000281 idx = cpufreq_cdev->max_level - state;
282 freq = cpufreq_cdev->em->table[idx].frequency;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530283 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
Javi Merinoc36cf072015-02-26 19:00:29 +0000284
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530285 return 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000286}
287
288/**
289 * cpufreq_power2state() - convert power to a cooling device state
290 * @cdev: &thermal_cooling_device pointer
291 * @tz: a valid thermal zone device pointer
292 * @power: power in milliwatts to be converted
293 * @state: pointer in which to store the resulting state
294 *
295 * Calculate a cooling device state for the cpus described by @cdev
296 * that would allow them to consume at most @power mW and store it in
297 * @state. Note that this calculation depends on external factors
298 * such as the cpu load or the current static power. Calling this
299 * function with the same power as input can yield different cooling
300 * device states depending on those external factors.
301 *
302 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
303 * the calculated frequency could not be converted to a valid state.
304 * The latter should not happen unless the frequencies available to
305 * cpufreq have changed since the initialization of the cpu cooling
306 * device.
307 */
308static int cpufreq_power2state(struct thermal_cooling_device *cdev,
309 struct thermal_zone_device *tz, u32 power,
310 unsigned long *state)
311{
Shaokun Zhange0fda732019-02-18 14:22:30 +0800312 unsigned int target_freq;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530313 u32 last_load, normalised_power;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530314 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530315 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merinoc36cf072015-02-26 19:00:29 +0000316
Viresh Kumar1dea4322017-04-25 15:57:10 +0530317 last_load = cpufreq_cdev->last_load ?: 1;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530318 normalised_power = (power * 100) / last_load;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530319 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000320
Viresh Kumar3e08b2d2017-04-25 15:57:12 +0530321 *state = get_level(cpufreq_cdev, target_freq);
Viresh Kumarba76dd92017-04-25 15:57:18 +0530322 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
323 power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000324 return 0;
325}
Quentin Perreta4e893e2019-10-30 15:14:51 +0000326
327static inline bool em_is_sane(struct cpufreq_cooling_device *cpufreq_cdev,
328 struct em_perf_domain *em) {
329 struct cpufreq_policy *policy;
330 unsigned int nr_levels;
331
332 if (!em)
333 return false;
334
335 policy = cpufreq_cdev->policy;
336 if (!cpumask_equal(policy->related_cpus, to_cpumask(em->cpus))) {
337 pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n",
338 cpumask_pr_args(to_cpumask(em->cpus)),
339 cpumask_pr_args(policy->related_cpus));
340 return false;
341 }
342
343 nr_levels = cpufreq_cdev->max_level + 1;
344 if (em->nr_cap_states != nr_levels) {
345 pr_err("The number of cap states in pd %*pbl (%u) doesn't match the number of cooling levels (%u)\n",
346 cpumask_pr_args(to_cpumask(em->cpus)),
347 em->nr_cap_states, nr_levels);
348 return false;
349 }
350
351 return true;
352}
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000353#endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */
354
Quentin Perreta4e893e2019-10-30 15:14:51 +0000355static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev,
356 unsigned long state)
357{
358 struct cpufreq_policy *policy;
359 unsigned long idx;
360
361#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
362 /* Use the Energy Model table if available */
363 if (cpufreq_cdev->em) {
364 idx = cpufreq_cdev->max_level - state;
365 return cpufreq_cdev->em->table[idx].frequency;
366 }
367#endif
368
369 /* Otherwise, fallback on the CPUFreq table */
370 policy = cpufreq_cdev->policy;
371 if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
372 idx = cpufreq_cdev->max_level - state;
373 else
374 idx = state;
375
376 return policy->freq_table[idx].frequency;
377}
378
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000379/* cpufreq cooling device callback functions are defined below */
380
381/**
382 * cpufreq_get_max_state - callback function to get the max cooling state.
383 * @cdev: thermal cooling device pointer.
384 * @state: fill this variable with the max cooling state.
385 *
386 * Callback for the thermal cooling device to return the cpufreq
387 * max cooling state.
388 *
389 * Return: 0 on success, an error code otherwise.
390 */
391static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
392 unsigned long *state)
393{
394 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
395
396 *state = cpufreq_cdev->max_level;
397 return 0;
398}
399
400/**
401 * cpufreq_get_cur_state - callback function to get the current cooling state.
402 * @cdev: thermal cooling device pointer.
403 * @state: fill this variable with the current cooling state.
404 *
405 * Callback for the thermal cooling device to return the cpufreq
406 * current cooling state.
407 *
408 * Return: 0 on success, an error code otherwise.
409 */
410static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
411 unsigned long *state)
412{
413 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
414
415 *state = cpufreq_cdev->cpufreq_state;
416
417 return 0;
418}
419
420/**
421 * cpufreq_set_cur_state - callback function to set the current cooling state.
422 * @cdev: thermal cooling device pointer.
423 * @state: set this variable to the current cooling state.
424 *
425 * Callback for the thermal cooling device to change the cpufreq
426 * current cooling state.
427 *
428 * Return: 0 on success, an error code otherwise.
429 */
430static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
431 unsigned long state)
432{
433 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Willy Wolffff44f672020-03-21 09:27:40 +0000434 int ret;
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000435
436 /* Request state should be less than max_level */
Daniel Lezcano40ea5682020-03-21 20:31:07 +0100437 if (state > cpufreq_cdev->max_level)
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000438 return -EINVAL;
439
440 /* Check if the old cooling action is same as new cooling action */
441 if (cpufreq_cdev->cpufreq_state == state)
442 return 0;
443
444 cpufreq_cdev->cpufreq_state = state;
445
Willy Wolffff44f672020-03-21 09:27:40 +0000446 ret = freq_qos_update_request(&cpufreq_cdev->qos_req,
447 get_state_freq(cpufreq_cdev, state));
448 return ret < 0 ? ret : 0;
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000449}
Javi Merinoc36cf072015-02-26 19:00:29 +0000450
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530451/* Bind cpufreq callbacks to thermal cooling device ops */
Brendan Jackmana305a432016-08-17 16:14:59 +0100452
Javi Merinoc36cf072015-02-26 19:00:29 +0000453static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
Brendan Jackmana305a432016-08-17 16:14:59 +0100454 .get_max_state = cpufreq_get_max_state,
455 .get_cur_state = cpufreq_get_cur_state,
456 .set_cur_state = cpufreq_set_cur_state,
Brendan Jackmana305a432016-08-17 16:14:59 +0100457};
458
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530459/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400460 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
461 * @np: a valid struct device_node to the cooling device device tree node
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530462 * @policy: cpufreq policy
Viresh Kumar405fb822014-12-04 09:41:55 +0530463 * Normally this should be same as cpufreq policy->related_cpus.
Quentin Perreta4e893e2019-10-30 15:14:51 +0000464 * @em: Energy Model of the cpufreq policy
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000465 *
466 * This interface function registers the cpufreq cooling device with the name
467 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400468 * cooling devices. It also gives the opportunity to link the cooling device
469 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000470 *
471 * Return: a valid struct thermal_cooling_device pointer on success,
472 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530473 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400474static struct thermal_cooling_device *
475__cpufreq_cooling_register(struct device_node *np,
Quentin Perreta4e893e2019-10-30 15:14:51 +0000476 struct cpufreq_policy *policy,
477 struct em_perf_domain *em)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530478{
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530479 struct thermal_cooling_device *cdev;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530480 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530481 char dev_name[THERMAL_NAME_LENGTH];
Quentin Perreta4e893e2019-10-30 15:14:51 +0000482 unsigned int i, num_cpus;
Viresh Kumar51308022019-07-23 11:44:02 +0530483 struct device *dev;
Viresh Kumar405fb822014-12-04 09:41:55 +0530484 int ret;
Brendan Jackmana305a432016-08-17 16:14:59 +0100485 struct thermal_cooling_device_ops *cooling_ops;
Viresh Kumar51308022019-07-23 11:44:02 +0530486
487 dev = get_cpu_device(policy->cpu);
488 if (unlikely(!dev)) {
489 pr_warn("No cpu device for cpu %d\n", policy->cpu);
490 return ERR_PTR(-ENODEV);
491 }
492
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530493
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530494 if (IS_ERR_OR_NULL(policy)) {
Arvind Yadavb2fd7082017-10-24 13:20:39 +0530495 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530496 return ERR_PTR(-EINVAL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530497 }
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530498
Viresh Kumar55d85292017-04-25 15:57:15 +0530499 i = cpufreq_table_count_valid_entries(policy);
500 if (!i) {
501 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
502 __func__);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530503 return ERR_PTR(-ENODEV);
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530504 }
505
Viresh Kumar1dea4322017-04-25 15:57:10 +0530506 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530507 if (!cpufreq_cdev)
508 return ERR_PTR(-ENOMEM);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530509
Viresh Kumarb12b6512017-04-25 15:57:16 +0530510 cpufreq_cdev->policy = policy;
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530511 num_cpus = cpumask_weight(policy->related_cpus);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530512 cpufreq_cdev->idle_time = kcalloc(num_cpus,
513 sizeof(*cpufreq_cdev->idle_time),
514 GFP_KERNEL);
515 if (!cpufreq_cdev->idle_time) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530516 cdev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +0000517 goto free_cdev;
518 }
519
Viresh Kumar55d85292017-04-25 15:57:15 +0530520 /* max_level is an index, not a counter */
521 cpufreq_cdev->max_level = i - 1;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530522
Matthew Wilcoxae606082016-12-21 09:47:05 -0800523 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
524 if (ret < 0) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530525 cdev = ERR_PTR(ret);
Quentin Perreta4e893e2019-10-30 15:14:51 +0000526 goto free_idle_time;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530527 }
Viresh Kumar1dea4322017-04-25 15:57:10 +0530528 cpufreq_cdev->id = ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530529
Viresh Kumar349d39d2017-04-25 15:57:19 +0530530 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
531 cpufreq_cdev->id);
532
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000533 cooling_ops = &cpufreq_cooling_ops;
534
535#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
Quentin Perreta4e893e2019-10-30 15:14:51 +0000536 if (em_is_sane(cpufreq_cdev, em)) {
537 cpufreq_cdev->em = em;
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000538 cooling_ops->get_requested_power = cpufreq_get_requested_power;
539 cooling_ops->state2power = cpufreq_state2power;
540 cooling_ops->power2state = cpufreq_power2state;
Quentin Perreta4e893e2019-10-30 15:14:51 +0000541 } else
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000542#endif
Quentin Perreta4e893e2019-10-30 15:14:51 +0000543 if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) {
544 pr_err("%s: unsorted frequency tables are not supported\n",
545 __func__);
546 cdev = ERR_PTR(-EINVAL);
547 goto remove_ida;
548 }
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100549
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +0200550 ret = freq_qos_add_request(&policy->constraints,
551 &cpufreq_cdev->qos_req, FREQ_QOS_MAX,
Quentin Perreta4e893e2019-10-30 15:14:51 +0000552 get_state_freq(cpufreq_cdev, 0));
Viresh Kumar51308022019-07-23 11:44:02 +0530553 if (ret < 0) {
554 pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
555 ret);
556 cdev = ERR_PTR(ret);
557 goto remove_ida;
558 }
559
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530560 cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev,
561 cooling_ops);
562 if (IS_ERR(cdev))
Viresh Kumar51308022019-07-23 11:44:02 +0530563 goto remove_qos_req;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530564
Russell King02373d72015-08-12 15:22:16 +0530565 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530566 list_add(&cpufreq_cdev->node, &cpufreq_cdev_list);
Matthew Wilcox088db932017-03-10 18:33:28 +0000567 mutex_unlock(&cooling_list_lock);
568
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530569 return cdev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530570
Viresh Kumar51308022019-07-23 11:44:02 +0530571remove_qos_req:
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +0200572 freq_qos_remove_request(&cpufreq_cdev->qos_req);
Matthew Wilcoxae606082016-12-21 09:47:05 -0800573remove_ida:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530574 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530575free_idle_time:
576 kfree(cpufreq_cdev->idle_time);
Viresh Kumar730abe02014-12-04 09:41:58 +0530577free_cdev:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530578 kfree(cpufreq_cdev);
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530579 return cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530580}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400581
582/**
583 * cpufreq_cooling_register - function to create cpufreq cooling device.
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530584 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400585 *
586 * This interface function registers the cpufreq cooling device with the name
587 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
588 * cooling devices.
589 *
590 * Return: a valid struct thermal_cooling_device pointer on success,
591 * on failure, it returns a corresponding ERR_PTR().
592 */
593struct thermal_cooling_device *
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530594cpufreq_cooling_register(struct cpufreq_policy *policy)
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400595{
Quentin Perreta4e893e2019-10-30 15:14:51 +0000596 return __cpufreq_cooling_register(NULL, policy, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400597}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000598EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530599
600/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400601 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530602 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400603 *
604 * This interface function registers the cpufreq cooling device with the name
605 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
606 * cooling devices. Using this API, the cpufreq cooling device will be
607 * linked to the device tree node provided.
608 *
Javi Merinoc36cf072015-02-26 19:00:29 +0000609 * Using this function, the cooling device will implement the power
610 * extensions by using a simple cpu power model. The cpus must have
611 * registered their OPPs using the OPP library.
612 *
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530613 * It also takes into account, if property present in policy CPU node, the
614 * static power consumed by the cpu.
Javi Merinoc36cf072015-02-26 19:00:29 +0000615 *
616 * Return: a valid struct thermal_cooling_device pointer on success,
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530617 * and NULL on failure.
Javi Merinoc36cf072015-02-26 19:00:29 +0000618 */
619struct thermal_cooling_device *
Viresh Kumar3ebb62f2017-12-05 11:02:45 +0530620of_cpufreq_cooling_register(struct cpufreq_policy *policy)
Javi Merinoc36cf072015-02-26 19:00:29 +0000621{
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530622 struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
623 struct thermal_cooling_device *cdev = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000624
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530625 if (!np) {
Daniel Lezcano23affa22019-12-19 23:53:17 +0100626 pr_err("cpufreq_cooling: OF node not available for cpu%d\n",
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530627 policy->cpu);
628 return NULL;
629 }
630
631 if (of_find_property(np, "#cooling-cells", NULL)) {
Quentin Perreta4e893e2019-10-30 15:14:51 +0000632 struct em_perf_domain *em = em_cpu_get(policy->cpu);
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530633
Quentin Perreta4e893e2019-10-30 15:14:51 +0000634 cdev = __cpufreq_cooling_register(np, policy, em);
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530635 if (IS_ERR(cdev)) {
Daniel Lezcano23affa22019-12-19 23:53:17 +0100636 pr_err("cpufreq_cooling: cpu%d failed to register as cooling device: %ld\n",
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530637 policy->cpu, PTR_ERR(cdev));
638 cdev = NULL;
639 }
640 }
641
642 of_node_put(np);
643 return cdev;
Javi Merinoc36cf072015-02-26 19:00:29 +0000644}
Viresh Kumar3ebb62f2017-12-05 11:02:45 +0530645EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
Javi Merinoc36cf072015-02-26 19:00:29 +0000646
647/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530648 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
649 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000650 *
651 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530652 */
653void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
654{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530655 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530656
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400657 if (!cdev)
658 return;
659
Viresh Kumar1dea4322017-04-25 15:57:10 +0530660 cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530661
Matthew Wilcoxae606082016-12-21 09:47:05 -0800662 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530663 list_del(&cpufreq_cdev->node);
Matthew Wilcox088db932017-03-10 18:33:28 +0000664 mutex_unlock(&cooling_list_lock);
665
Daniel Lezcano72554a72019-04-28 11:51:05 +0200666 thermal_cooling_device_unregister(cdev);
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +0200667 freq_qos_remove_request(&cpufreq_cdev->qos_req);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530668 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530669 kfree(cpufreq_cdev->idle_time);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530670 kfree(cpufreq_cdev);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530671}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000672EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);