blob: e2cc7bd308620dc0e5bd7510c6a81b022b3bb274 [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>
Daniel Lezcanoef37d1f2021-03-14 12:13:30 +010016#include <linux/device.h>
Amit Kucheria5ccb4512020-05-11 17:54:57 +053017#include <linux/energy_model.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053018#include <linux/err.h>
Amit Kucheriac65f83c2020-05-11 17:54:58 +053019#include <linux/export.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>
Lukasz Lubaae6ccaa2022-07-07 08:15:52 +010024#include <linux/units.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053025
Daniel Lezcano32a7a022023-03-07 14:37:25 +010026#include "thermal_trace.h"
Javi Merino6828a472015-03-02 17:17:20 +000027
Viresh Kumar07d888d2014-12-04 09:41:49 +053028/*
29 * Cooling state <-> CPUFreq frequency
30 *
31 * Cooling states are translated to frequencies throughout this driver and this
32 * is the relation between them.
33 *
34 * Highest cooling state corresponds to lowest possible frequency.
35 *
36 * i.e.
37 * level 0 --> 1st Max Freq
38 * level 1 --> 2nd Max Freq
39 * ...
40 */
41
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053042/**
Viresh Kumar81ee14d2017-04-25 15:57:20 +053043 * 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()
46 */
47struct time_in_idle {
48 u64 time;
49 u64 timestamp;
50};
51
52/**
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000053 * struct cpufreq_cooling_device - data for cooling device with cpufreq
Viresh Kumard72b4012017-04-25 15:57:24 +053054 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053055 * @cpufreq_state: integer value representing the current state of cpufreq
56 * cooling devices.
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053057 * @max_level: maximum cooling level. One less than total number of valid
58 * cpufreq frequencies.
Quentin Perreta4e893e2019-10-30 15:14:51 +000059 * @em: Reference on the Energy Model of the device
Viresh Kumard72b4012017-04-25 15:57:24 +053060 * @cdev: thermal_cooling_device pointer to keep track of the
61 * registered cooling device.
62 * @policy: cpufreq policy.
Lukasz Luba3cbf6a8a2022-06-13 13:43:24 +010063 * @cooling_ops: cpufreq callbacks to thermal cooling device ops
Viresh Kumar81ee14d2017-04-25 15:57:20 +053064 * @idle_time: idle time stats
Amit Kucheria7b4e7f02019-11-20 21:15:11 +053065 * @qos_req: PM QoS contraint to apply
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053066 *
Viresh Kumarbeca6052014-12-04 09:41:48 +053067 * This structure is required for keeping information of each registered
68 * cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053069 */
70struct cpufreq_cooling_device {
Viresh Kumard72b4012017-04-25 15:57:24 +053071 u32 last_load;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053072 unsigned int cpufreq_state;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053073 unsigned int max_level;
Quentin Perreta4e893e2019-10-30 15:14:51 +000074 struct em_perf_domain *em;
Viresh Kumard72b4012017-04-25 15:57:24 +053075 struct cpufreq_policy *policy;
Lukasz Luba3cbf6a8a2022-06-13 13:43:24 +010076 struct thermal_cooling_device_ops cooling_ops;
Viresh Kumard1515852020-12-08 09:46:57 +053077#ifndef CONFIG_SMP
Viresh Kumar81ee14d2017-04-25 15:57:20 +053078 struct time_in_idle *idle_time;
Viresh Kumard1515852020-12-08 09:46:57 +053079#endif
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
Quentin Perret5a4e5b72019-10-30 15:14:50 +000083#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053084/**
Viresh Kumar4843c4a2014-12-04 09:42:07 +053085 * get_level: Find the level for a particular frequency
Viresh Kumar1dea4322017-04-25 15:57:10 +053086 * @cpufreq_cdev: cpufreq_cdev for which the property is required
Viresh Kumar4843c4a2014-12-04 09:42:07 +053087 * @freq: Frequency
Eduardo Valentin82b9ee42013-04-17 17:12:00 +000088 *
Viresh Kumarda27f692017-04-25 15:57:21 +053089 * Return: level corresponding to the frequency.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053090 */
Viresh Kumar1dea4322017-04-25 15:57:10 +053091static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
Viresh Kumar4843c4a2014-12-04 09:42:07 +053092 unsigned int freq)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053093{
Quentin Perreta4e893e2019-10-30 15:14:51 +000094 int i;
Eduardo Valentin79491e52013-04-17 17:11:59 +000095
Quentin Perreta4e893e2019-10-30 15:14:51 +000096 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
97 if (freq > cpufreq_cdev->em->table[i].frequency)
Viresh Kumar4843c4a2014-12-04 09:42:07 +053098 break;
Javi Merinoc36cf072015-02-26 19:00:29 +000099 }
100
Quentin Perreta4e893e2019-10-30 15:14:51 +0000101 return cpufreq_cdev->max_level - i - 1;
Javi Merinoc36cf072015-02-26 19:00:29 +0000102}
103
Viresh Kumar1dea4322017-04-25 15:57:10 +0530104static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000105 u32 freq)
106{
Lukasz Lubaae6ccaa2022-07-07 08:15:52 +0100107 unsigned long power_mw;
Javi Merinoc36cf072015-02-26 19:00:29 +0000108 int i;
Javi Merinoc36cf072015-02-26 19:00:29 +0000109
Quentin Perreta4e893e2019-10-30 15:14:51 +0000110 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
111 if (freq > cpufreq_cdev->em->table[i].frequency)
Javi Merinoc36cf072015-02-26 19:00:29 +0000112 break;
Quentin Perreta4e893e2019-10-30 15:14:51 +0000113 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000114
Lukasz Lubaae6ccaa2022-07-07 08:15:52 +0100115 power_mw = cpufreq_cdev->em->table[i + 1].power;
116 power_mw /= MICROWATT_PER_MILLIWATT;
117
118 return power_mw;
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{
Lukasz Lubaae6ccaa2022-07-07 08:15:52 +0100124 unsigned long em_power_mw;
Javi Merinoc36cf072015-02-26 19:00:29 +0000125 int i;
Javi Merinoc36cf072015-02-26 19:00:29 +0000126
brian-sy yang34ab17c2020-12-29 13:08:31 +0800127 for (i = cpufreq_cdev->max_level; i > 0; i--) {
Lukasz Lubaae6ccaa2022-07-07 08:15:52 +0100128 /* Convert EM power to milli-Watts to make safe comparison */
129 em_power_mw = cpufreq_cdev->em->table[i].power;
130 em_power_mw /= MICROWATT_PER_MILLIWATT;
131 if (power >= em_power_mw)
Javi Merinoc36cf072015-02-26 19:00:29 +0000132 break;
Quentin Perreta4e893e2019-10-30 15:14:51 +0000133 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000134
Finley Xiao371a3bc2020-06-19 17:08:25 +0800135 return cpufreq_cdev->em->table[i].frequency;
Javi Merinoc36cf072015-02-26 19:00:29 +0000136}
137
138/**
Viresh Kumard1515852020-12-08 09:46:57 +0530139 * get_load() - get load for a cpu
140 * @cpufreq_cdev: struct cpufreq_cooling_device for the cpu
141 * @cpu: cpu number
142 * @cpu_idx: index of the cpu in time_in_idle array
Javi Merinoc36cf072015-02-26 19:00:29 +0000143 *
144 * Return: The average load of cpu @cpu in percentage since this
145 * function was last called.
146 */
Viresh Kumard1515852020-12-08 09:46:57 +0530147#ifdef CONFIG_SMP
148static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
149 int cpu_idx)
150{
Dietmar Eggemannbb447992022-06-21 10:04:10 +0100151 unsigned long util = sched_cpu_util(cpu);
Viresh Kumard1515852020-12-08 09:46:57 +0530152
Dietmar Eggemannbb447992022-06-21 10:04:10 +0100153 return (util * 100) / arch_scale_cpu_capacity(cpu);
Viresh Kumard1515852020-12-08 09:46:57 +0530154}
155#else /* !CONFIG_SMP */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530156static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
Javi Merinoa53b8392016-02-11 12:00:51 +0000157 int cpu_idx)
Javi Merinoc36cf072015-02-26 19:00:29 +0000158{
159 u32 load;
160 u64 now, now_idle, delta_time, delta_idle;
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530161 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
Javi Merinoc36cf072015-02-26 19:00:29 +0000162
163 now_idle = get_cpu_idle_time(cpu, &now, 0);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530164 delta_idle = now_idle - idle_time->time;
165 delta_time = now - idle_time->timestamp;
Javi Merinoc36cf072015-02-26 19:00:29 +0000166
167 if (delta_time <= delta_idle)
168 load = 0;
169 else
170 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
171
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530172 idle_time->time = now_idle;
173 idle_time->timestamp = now;
Javi Merinoc36cf072015-02-26 19:00:29 +0000174
175 return load;
176}
Viresh Kumard1515852020-12-08 09:46:57 +0530177#endif /* CONFIG_SMP */
Javi Merinoc36cf072015-02-26 19:00:29 +0000178
179/**
Javi Merinoc36cf072015-02-26 19:00:29 +0000180 * get_dynamic_power() - calculate the dynamic power
Viresh Kumar1dea4322017-04-25 15:57:10 +0530181 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
Javi Merinoc36cf072015-02-26 19:00:29 +0000182 * @freq: current frequency
183 *
184 * Return: the dynamic power consumed by the cpus described by
Viresh Kumar1dea4322017-04-25 15:57:10 +0530185 * @cpufreq_cdev.
Javi Merinoc36cf072015-02-26 19:00:29 +0000186 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530187static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000188 unsigned long freq)
189{
190 u32 raw_cpu_power;
191
Viresh Kumar1dea4322017-04-25 15:57:10 +0530192 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
193 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530194}
195
Javi Merinoc36cf072015-02-26 19:00:29 +0000196/**
197 * cpufreq_get_requested_power() - get the current power
198 * @cdev: &thermal_cooling_device pointer
Javi Merinoc36cf072015-02-26 19:00:29 +0000199 * @power: pointer in which to store the resulting power
200 *
201 * Calculate the current power consumption of the cpus in milliwatts
202 * and store it in @power. This function should actually calculate
203 * the requested power, but it's hard to get the frequency that
204 * cpufreq would have assigned if there were no thermal limits.
205 * Instead, we calculate the current power on the assumption that the
206 * immediate future will look like the immediate past.
207 *
208 * We use the current frequency and the average load since this
209 * function was last called. In reality, there could have been
210 * multiple opps since this function was last called and that affects
211 * the load calculation. While it's not perfectly accurate, this
212 * simplification is good enough and works. REVISIT this, as more
213 * complex code may be needed if experiments show that it's not
214 * accurate enough.
215 *
Lukasz Luba9784d2f2022-06-13 13:43:26 +0100216 * Return: 0 on success, this function doesn't fail.
Javi Merinoc36cf072015-02-26 19:00:29 +0000217 */
218static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000219 u32 *power)
220{
221 unsigned long freq;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530222 int i = 0, cpu;
223 u32 total_load = 0;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530224 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530225 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merinoc36cf072015-02-26 19:00:29 +0000226
Viresh Kumarba76dd92017-04-25 15:57:18 +0530227 freq = cpufreq_quick_get(policy->cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000228
Viresh Kumarba76dd92017-04-25 15:57:18 +0530229 for_each_cpu(cpu, policy->related_cpus) {
Javi Merinoc36cf072015-02-26 19:00:29 +0000230 u32 load;
231
232 if (cpu_online(cpu))
Viresh Kumar1dea4322017-04-25 15:57:10 +0530233 load = get_load(cpufreq_cdev, cpu, i);
Javi Merinoc36cf072015-02-26 19:00:29 +0000234 else
235 load = 0;
236
237 total_load += load;
238 }
239
Viresh Kumar1dea4322017-04-25 15:57:10 +0530240 cpufreq_cdev->last_load = total_load;
Javi Merinoc36cf072015-02-26 19:00:29 +0000241
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530242 *power = get_dynamic_power(cpufreq_cdev, freq);
Javi Merino6828a472015-03-02 17:17:20 +0000243
Lukasz Luba3f7ced72022-06-13 13:43:25 +0100244 trace_thermal_power_cpu_get_power_simple(policy->cpu, *power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000245
Javi Merinoc36cf072015-02-26 19:00:29 +0000246 return 0;
247}
248
249/**
250 * cpufreq_state2power() - convert a cpu cdev state to power consumed
251 * @cdev: &thermal_cooling_device pointer
Javi Merinoc36cf072015-02-26 19:00:29 +0000252 * @state: cooling device state to be converted
253 * @power: pointer in which to store the resulting power
254 *
255 * Convert cooling device state @state into power consumption in
256 * milliwatts assuming 100% load. Store the calculated power in
257 * @power.
258 *
Lukasz Luba9784d2f2022-06-13 13:43:26 +0100259 * Return: 0 on success, -EINVAL if the cooling device state is bigger
260 * than maximum allowed.
Javi Merinoc36cf072015-02-26 19:00:29 +0000261 */
262static int cpufreq_state2power(struct thermal_cooling_device *cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000263 unsigned long state, u32 *power)
264{
Quentin Perreta4e893e2019-10-30 15:14:51 +0000265 unsigned int freq, num_cpus, idx;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530266 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Javi Merinoc36cf072015-02-26 19:00:29 +0000267
Viresh Kumarcb1b631862017-04-25 15:57:23 +0530268 /* Request state should be less than max_level */
Daniel Lezcano40ea5682020-03-21 20:31:07 +0100269 if (state > cpufreq_cdev->max_level)
Viresh Kumarcb1b631862017-04-25 15:57:23 +0530270 return -EINVAL;
271
Viresh Kumarba76dd92017-04-25 15:57:18 +0530272 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
Javi Merinoc36cf072015-02-26 19:00:29 +0000273
Quentin Perreta4e893e2019-10-30 15:14:51 +0000274 idx = cpufreq_cdev->max_level - state;
275 freq = cpufreq_cdev->em->table[idx].frequency;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530276 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
Javi Merinoc36cf072015-02-26 19:00:29 +0000277
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530278 return 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000279}
280
281/**
282 * cpufreq_power2state() - convert power to a cooling device state
283 * @cdev: &thermal_cooling_device pointer
Javi Merinoc36cf072015-02-26 19:00:29 +0000284 * @power: power in milliwatts to be converted
285 * @state: pointer in which to store the resulting state
286 *
287 * Calculate a cooling device state for the cpus described by @cdev
288 * that would allow them to consume at most @power mW and store it in
289 * @state. Note that this calculation depends on external factors
Lukasz Luba9784d2f2022-06-13 13:43:26 +0100290 * such as the CPUs load. Calling this function with the same power
291 * as input can yield different cooling device states depending on those
292 * external factors.
Javi Merinoc36cf072015-02-26 19:00:29 +0000293 *
Lukasz Luba9784d2f2022-06-13 13:43:26 +0100294 * Return: 0 on success, this function doesn't fail.
Javi Merinoc36cf072015-02-26 19:00:29 +0000295 */
296static int cpufreq_power2state(struct thermal_cooling_device *cdev,
zhuguangqingecd1d2a2020-09-14 15:11:01 +0800297 u32 power, unsigned long *state)
Javi Merinoc36cf072015-02-26 19:00:29 +0000298{
Shaokun Zhange0fda732019-02-18 14:22:30 +0800299 unsigned int target_freq;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530300 u32 last_load, normalised_power;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530301 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530302 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merinoc36cf072015-02-26 19:00:29 +0000303
Viresh Kumar1dea4322017-04-25 15:57:10 +0530304 last_load = cpufreq_cdev->last_load ?: 1;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530305 normalised_power = (power * 100) / last_load;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530306 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000307
Viresh Kumar3e08b2d2017-04-25 15:57:12 +0530308 *state = get_level(cpufreq_cdev, target_freq);
Viresh Kumarba76dd92017-04-25 15:57:18 +0530309 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
310 power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000311 return 0;
312}
Quentin Perreta4e893e2019-10-30 15:14:51 +0000313
314static inline bool em_is_sane(struct cpufreq_cooling_device *cpufreq_cdev,
315 struct em_perf_domain *em) {
316 struct cpufreq_policy *policy;
317 unsigned int nr_levels;
318
Lukasz Luba9926bbe2022-03-21 09:57:28 +0000319 if (!em || em_is_artificial(em))
Quentin Perreta4e893e2019-10-30 15:14:51 +0000320 return false;
321
322 policy = cpufreq_cdev->policy;
Lukasz Luba521b5122020-05-27 10:58:47 +0100323 if (!cpumask_equal(policy->related_cpus, em_span_cpus(em))) {
Quentin Perreta4e893e2019-10-30 15:14:51 +0000324 pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n",
Lukasz Luba521b5122020-05-27 10:58:47 +0100325 cpumask_pr_args(em_span_cpus(em)),
Quentin Perreta4e893e2019-10-30 15:14:51 +0000326 cpumask_pr_args(policy->related_cpus));
327 return false;
328 }
329
330 nr_levels = cpufreq_cdev->max_level + 1;
Lukasz Luba521b5122020-05-27 10:58:47 +0100331 if (em_pd_nr_perf_states(em) != nr_levels) {
332 pr_err("The number of performance states in pd %*pbl (%u) doesn't match the number of cooling levels (%u)\n",
333 cpumask_pr_args(em_span_cpus(em)),
334 em_pd_nr_perf_states(em), nr_levels);
Quentin Perreta4e893e2019-10-30 15:14:51 +0000335 return false;
336 }
337
338 return true;
339}
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000340#endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */
341
Viresh Kumard1515852020-12-08 09:46:57 +0530342#ifdef CONFIG_SMP
343static inline int allocate_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
344{
345 return 0;
346}
347
348static inline void free_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
349{
350}
351#else
352static int allocate_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
353{
354 unsigned int num_cpus = cpumask_weight(cpufreq_cdev->policy->related_cpus);
355
356 cpufreq_cdev->idle_time = kcalloc(num_cpus,
357 sizeof(*cpufreq_cdev->idle_time),
358 GFP_KERNEL);
359 if (!cpufreq_cdev->idle_time)
360 return -ENOMEM;
361
362 return 0;
363}
364
365static void free_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
366{
367 kfree(cpufreq_cdev->idle_time);
368 cpufreq_cdev->idle_time = NULL;
369}
370#endif /* CONFIG_SMP */
371
Quentin Perreta4e893e2019-10-30 15:14:51 +0000372static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev,
373 unsigned long state)
374{
375 struct cpufreq_policy *policy;
376 unsigned long idx;
377
378#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
379 /* Use the Energy Model table if available */
380 if (cpufreq_cdev->em) {
381 idx = cpufreq_cdev->max_level - state;
382 return cpufreq_cdev->em->table[idx].frequency;
383 }
384#endif
385
386 /* Otherwise, fallback on the CPUFreq table */
387 policy = cpufreq_cdev->policy;
388 if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
389 idx = cpufreq_cdev->max_level - state;
390 else
391 idx = state;
392
393 return policy->freq_table[idx].frequency;
394}
395
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000396/* cpufreq cooling device callback functions are defined below */
397
398/**
399 * cpufreq_get_max_state - callback function to get the max cooling state.
400 * @cdev: thermal cooling device pointer.
401 * @state: fill this variable with the max cooling state.
402 *
403 * Callback for the thermal cooling device to return the cpufreq
404 * max cooling state.
405 *
Lukasz Luba9784d2f2022-06-13 13:43:26 +0100406 * Return: 0 on success, this function doesn't fail.
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000407 */
408static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
409 unsigned long *state)
410{
411 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
412
413 *state = cpufreq_cdev->max_level;
414 return 0;
415}
416
417/**
418 * cpufreq_get_cur_state - callback function to get the current cooling state.
419 * @cdev: thermal cooling device pointer.
420 * @state: fill this variable with the current cooling state.
421 *
422 * Callback for the thermal cooling device to return the cpufreq
423 * current cooling state.
424 *
Lukasz Luba9784d2f2022-06-13 13:43:26 +0100425 * Return: 0 on success, this function doesn't fail.
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000426 */
427static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
428 unsigned long *state)
429{
430 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
431
432 *state = cpufreq_cdev->cpufreq_state;
433
434 return 0;
435}
436
437/**
438 * cpufreq_set_cur_state - callback function to set the current cooling state.
439 * @cdev: thermal cooling device pointer.
440 * @state: set this variable to the current cooling state.
441 *
442 * Callback for the thermal cooling device to change the cpufreq
443 * current cooling state.
444 *
445 * Return: 0 on success, an error code otherwise.
446 */
447static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
448 unsigned long state)
449{
450 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Thara Gopinathf12e4f62020-02-21 19:52:12 -0500451 struct cpumask *cpus;
452 unsigned int frequency;
Thara Gopinathf12e4f62020-02-21 19:52:12 -0500453 int ret;
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000454
455 /* Request state should be less than max_level */
Daniel Lezcano40ea5682020-03-21 20:31:07 +0100456 if (state > cpufreq_cdev->max_level)
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000457 return -EINVAL;
458
459 /* Check if the old cooling action is same as new cooling action */
460 if (cpufreq_cdev->cpufreq_state == state)
461 return 0;
462
Thara Gopinathf12e4f62020-02-21 19:52:12 -0500463 frequency = get_state_freq(cpufreq_cdev, state);
464
465 ret = freq_qos_update_request(&cpufreq_cdev->qos_req, frequency);
Viresh Kumara51afb12021-02-17 11:18:58 +0530466 if (ret >= 0) {
Zhuguangqing236761f2020-11-06 17:22:43 +0800467 cpufreq_cdev->cpufreq_state = state;
Lukasz Luba2ad8ccc2021-06-14 20:10:30 +0100468 cpus = cpufreq_cdev->policy->related_cpus;
Lukasz Luba5168b1b2021-11-09 19:57:11 +0000469 arch_update_thermal_pressure(cpus, frequency);
Linus Torvalds34183dd2020-04-07 20:00:16 -0700470 ret = 0;
Thara Gopinathf12e4f62020-02-21 19:52:12 -0500471 }
472
473 return ret;
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000474}
Javi Merinoc36cf072015-02-26 19:00:29 +0000475
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530476/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400477 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
Jilin Yuan96f1c522022-08-22 20:32:39 +0800478 * @np: a valid struct device_node to the cooling device tree node
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530479 * @policy: cpufreq policy
Viresh Kumar405fb822014-12-04 09:41:55 +0530480 * Normally this should be same as cpufreq policy->related_cpus.
Quentin Perreta4e893e2019-10-30 15:14:51 +0000481 * @em: Energy Model of the cpufreq policy
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000482 *
483 * This interface function registers the cpufreq cooling device with the name
Lukasz Luba9784d2f2022-06-13 13:43:26 +0100484 * "cpufreq-%s". This API can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400485 * cooling devices. It also gives the opportunity to link the cooling device
486 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000487 *
488 * Return: a valid struct thermal_cooling_device pointer on success,
489 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530490 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400491static struct thermal_cooling_device *
492__cpufreq_cooling_register(struct device_node *np,
Quentin Perreta4e893e2019-10-30 15:14:51 +0000493 struct cpufreq_policy *policy,
494 struct em_perf_domain *em)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530495{
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530496 struct thermal_cooling_device *cdev;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530497 struct cpufreq_cooling_device *cpufreq_cdev;
Viresh Kumard1515852020-12-08 09:46:57 +0530498 unsigned int i;
Viresh Kumar51308022019-07-23 11:44:02 +0530499 struct device *dev;
Viresh Kumar405fb822014-12-04 09:41:55 +0530500 int ret;
Brendan Jackmana305a432016-08-17 16:14:59 +0100501 struct thermal_cooling_device_ops *cooling_ops;
Daniel Lezcanoef37d1f2021-03-14 12:13:30 +0100502 char *name;
Viresh Kumar51308022019-07-23 11:44:02 +0530503
Xuewen Yancff89522022-08-25 19:40:17 +0800504 if (IS_ERR_OR_NULL(policy)) {
505 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
506 return ERR_PTR(-EINVAL);
507 }
508
Viresh Kumar51308022019-07-23 11:44:02 +0530509 dev = get_cpu_device(policy->cpu);
510 if (unlikely(!dev)) {
511 pr_warn("No cpu device for cpu %d\n", policy->cpu);
512 return ERR_PTR(-ENODEV);
513 }
514
Viresh Kumar55d85292017-04-25 15:57:15 +0530515 i = cpufreq_table_count_valid_entries(policy);
516 if (!i) {
517 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
518 __func__);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530519 return ERR_PTR(-ENODEV);
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530520 }
521
Viresh Kumar1dea4322017-04-25 15:57:10 +0530522 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530523 if (!cpufreq_cdev)
524 return ERR_PTR(-ENOMEM);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530525
Viresh Kumarb12b6512017-04-25 15:57:16 +0530526 cpufreq_cdev->policy = policy;
Viresh Kumard1515852020-12-08 09:46:57 +0530527
528 ret = allocate_idle_time(cpufreq_cdev);
529 if (ret) {
530 cdev = ERR_PTR(ret);
Javi Merinoc36cf072015-02-26 19:00:29 +0000531 goto free_cdev;
532 }
533
Viresh Kumar55d85292017-04-25 15:57:15 +0530534 /* max_level is an index, not a counter */
535 cpufreq_cdev->max_level = i - 1;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530536
Lukasz Luba3cbf6a8a2022-06-13 13:43:24 +0100537 cooling_ops = &cpufreq_cdev->cooling_ops;
538 cooling_ops->get_max_state = cpufreq_get_max_state;
539 cooling_ops->get_cur_state = cpufreq_get_cur_state;
540 cooling_ops->set_cur_state = cpufreq_set_cur_state;
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000541
542#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
Quentin Perreta4e893e2019-10-30 15:14:51 +0000543 if (em_is_sane(cpufreq_cdev, em)) {
544 cpufreq_cdev->em = em;
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000545 cooling_ops->get_requested_power = cpufreq_get_requested_power;
546 cooling_ops->state2power = cpufreq_state2power;
547 cooling_ops->power2state = cpufreq_power2state;
Quentin Perreta4e893e2019-10-30 15:14:51 +0000548 } else
Quentin Perret5a4e5b72019-10-30 15:14:50 +0000549#endif
Quentin Perreta4e893e2019-10-30 15:14:51 +0000550 if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) {
551 pr_err("%s: unsorted frequency tables are not supported\n",
552 __func__);
553 cdev = ERR_PTR(-EINVAL);
Daniel Lezcanoef37d1f2021-03-14 12:13:30 +0100554 goto free_idle_time;
Quentin Perreta4e893e2019-10-30 15:14:51 +0000555 }
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100556
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +0200557 ret = freq_qos_add_request(&policy->constraints,
558 &cpufreq_cdev->qos_req, FREQ_QOS_MAX,
Quentin Perreta4e893e2019-10-30 15:14:51 +0000559 get_state_freq(cpufreq_cdev, 0));
Viresh Kumar51308022019-07-23 11:44:02 +0530560 if (ret < 0) {
561 pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
562 ret);
563 cdev = ERR_PTR(ret);
Daniel Lezcanoef37d1f2021-03-14 12:13:30 +0100564 goto free_idle_time;
Viresh Kumar51308022019-07-23 11:44:02 +0530565 }
566
Daniel Lezcanoef37d1f2021-03-14 12:13:30 +0100567 cdev = ERR_PTR(-ENOMEM);
568 name = kasprintf(GFP_KERNEL, "cpufreq-%s", dev_name(dev));
569 if (!name)
570 goto remove_qos_req;
571
572 cdev = thermal_of_cooling_device_register(np, name, cpufreq_cdev,
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530573 cooling_ops);
Daniel Lezcanoef37d1f2021-03-14 12:13:30 +0100574 kfree(name);
575
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530576 if (IS_ERR(cdev))
Viresh Kumar51308022019-07-23 11:44:02 +0530577 goto remove_qos_req;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530578
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530579 return cdev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530580
Viresh Kumar51308022019-07-23 11:44:02 +0530581remove_qos_req:
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +0200582 freq_qos_remove_request(&cpufreq_cdev->qos_req);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530583free_idle_time:
Viresh Kumard1515852020-12-08 09:46:57 +0530584 free_idle_time(cpufreq_cdev);
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
Lukasz Luba9784d2f2022-06-13 13:43:26 +0100595 * "cpufreq-%s". This API can support multiple instances of cpufreq cooling
596 * devices.
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400597 *
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
Lukasz Luba9784d2f2022-06-13 13:43:26 +0100613 * "cpufreq-%s". This API can support multiple instances of cpufreq cooling
614 * devices. Using this API, the cpufreq cooling device will be linked to the
615 * device tree node provided.
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400616 *
Javi Merinoc36cf072015-02-26 19:00:29 +0000617 * Using this function, the cooling device will implement the power
Lukasz Luba9784d2f2022-06-13 13:43:26 +0100618 * extensions by using the Energy Model (if present). The cpus must have
Javi Merinoc36cf072015-02-26 19:00:29 +0000619 * registered their OPPs using the OPP library.
620 *
Javi Merinoc36cf072015-02-26 19:00:29 +0000621 * Return: a valid struct thermal_cooling_device pointer on success,
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530622 * and NULL on failure.
Javi Merinoc36cf072015-02-26 19:00:29 +0000623 */
624struct thermal_cooling_device *
Viresh Kumar3ebb62f2017-12-05 11:02:45 +0530625of_cpufreq_cooling_register(struct cpufreq_policy *policy)
Javi Merinoc36cf072015-02-26 19:00:29 +0000626{
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530627 struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
628 struct thermal_cooling_device *cdev = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000629
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530630 if (!np) {
Daniel Lezcano23affa22019-12-19 23:53:17 +0100631 pr_err("cpufreq_cooling: OF node not available for cpu%d\n",
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530632 policy->cpu);
633 return NULL;
634 }
635
Rob Herring86df7d12023-03-10 08:47:26 -0600636 if (of_property_present(np, "#cooling-cells")) {
Quentin Perreta4e893e2019-10-30 15:14:51 +0000637 struct em_perf_domain *em = em_cpu_get(policy->cpu);
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530638
Quentin Perreta4e893e2019-10-30 15:14:51 +0000639 cdev = __cpufreq_cooling_register(np, policy, em);
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530640 if (IS_ERR(cdev)) {
Daniel Lezcano23affa22019-12-19 23:53:17 +0100641 pr_err("cpufreq_cooling: cpu%d failed to register as cooling device: %ld\n",
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530642 policy->cpu, PTR_ERR(cdev));
643 cdev = NULL;
644 }
645 }
646
647 of_node_put(np);
648 return cdev;
Javi Merinoc36cf072015-02-26 19:00:29 +0000649}
Viresh Kumar3ebb62f2017-12-05 11:02:45 +0530650EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
Javi Merinoc36cf072015-02-26 19:00:29 +0000651
652/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530653 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
654 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000655 *
Lukasz Luba9784d2f2022-06-13 13:43:26 +0100656 * This interface function unregisters the "cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530657 */
658void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
659{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530660 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530661
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400662 if (!cdev)
663 return;
664
Viresh Kumar1dea4322017-04-25 15:57:10 +0530665 cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530666
Daniel Lezcano72554a72019-04-28 11:51:05 +0200667 thermal_cooling_device_unregister(cdev);
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +0200668 freq_qos_remove_request(&cpufreq_cdev->qos_req);
Viresh Kumard1515852020-12-08 09:46:57 +0530669 free_idle_time(cpufreq_cdev);
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);