blob: 391f39776c6ab8c6205ef0a122079887e53220ea [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/*
3 * linux/drivers/thermal/cpu_cooling.c
4 *
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>
23
Javi Merino6828a472015-03-02 17:17:20 +000024#include <trace/events/thermal.h>
25
Viresh Kumar07d888d2014-12-04 09:41:49 +053026/*
27 * Cooling state <-> CPUFreq frequency
28 *
29 * Cooling states are translated to frequencies throughout this driver and this
30 * is the relation between them.
31 *
32 * Highest cooling state corresponds to lowest possible frequency.
33 *
34 * i.e.
35 * level 0 --> 1st Max Freq
36 * level 1 --> 2nd Max Freq
37 * ...
38 */
39
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053040/**
Viresh Kumar349d39d2017-04-25 15:57:19 +053041 * struct freq_table - frequency table along with power entries
Javi Merinoc36cf072015-02-26 19:00:29 +000042 * @frequency: frequency in KHz
43 * @power: power in mW
44 *
45 * This structure is built when the cooling device registers and helps
Viresh Kumar349d39d2017-04-25 15:57:19 +053046 * in translating frequency to power and vice versa.
Javi Merinoc36cf072015-02-26 19:00:29 +000047 */
Viresh Kumar349d39d2017-04-25 15:57:19 +053048struct freq_table {
Javi Merinoc36cf072015-02-26 19:00:29 +000049 u32 frequency;
50 u32 power;
51};
52
53/**
Viresh Kumar81ee14d2017-04-25 15:57:20 +053054 * struct time_in_idle - Idle time stats
55 * @time: previous reading of the absolute time that this cpu was idle
56 * @timestamp: wall time of the last invocation of get_cpu_idle_time_us()
57 */
58struct time_in_idle {
59 u64 time;
60 u64 timestamp;
61};
62
63/**
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000064 * struct cpufreq_cooling_device - data for cooling device with cpufreq
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053065 * @id: unique integer value corresponding to each cpufreq_cooling_device
66 * registered.
Viresh Kumard72b4012017-04-25 15:57:24 +053067 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053068 * @cpufreq_state: integer value representing the current state of cpufreq
69 * cooling devices.
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053070 * @max_level: maximum cooling level. One less than total number of valid
71 * cpufreq frequencies.
Viresh Kumard72b4012017-04-25 15:57:24 +053072 * @freq_table: Freq table in descending order of frequencies
73 * @cdev: thermal_cooling_device pointer to keep track of the
74 * registered cooling device.
75 * @policy: cpufreq policy.
Javi Merinofc4de352014-12-15 16:55:52 +000076 * @node: list_head to link all cpufreq_cooling_device together.
Viresh Kumar81ee14d2017-04-25 15:57:20 +053077 * @idle_time: idle time stats
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053078 *
Viresh Kumarbeca6052014-12-04 09:41:48 +053079 * This structure is required for keeping information of each registered
80 * cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053081 */
82struct cpufreq_cooling_device {
83 int id;
Viresh Kumard72b4012017-04-25 15:57:24 +053084 u32 last_load;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053085 unsigned int cpufreq_state;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053086 unsigned int max_level;
Viresh Kumar349d39d2017-04-25 15:57:19 +053087 struct freq_table *freq_table; /* In descending order */
Viresh Kumard72b4012017-04-25 15:57:24 +053088 struct cpufreq_policy *policy;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053089 struct list_head node;
Viresh Kumar81ee14d2017-04-25 15:57:20 +053090 struct time_in_idle *idle_time;
Viresh Kumar51308022019-07-23 11:44:02 +053091 struct dev_pm_qos_request qos_req;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053092};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053093
Viresh Kumarfb8ea302017-04-25 15:57:09 +053094static DEFINE_IDA(cpufreq_ida);
Russell King02373d72015-08-12 15:22:16 +053095static DEFINE_MUTEX(cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +053096static LIST_HEAD(cpufreq_cdev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053097
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053098/* Below code defines functions to be used for cpufreq as cooling device */
99
100/**
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530101 * get_level: Find the level for a particular frequency
Viresh Kumar1dea4322017-04-25 15:57:10 +0530102 * @cpufreq_cdev: cpufreq_cdev for which the property is required
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530103 * @freq: Frequency
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000104 *
Viresh Kumarda27f692017-04-25 15:57:21 +0530105 * Return: level corresponding to the frequency.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530106 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530107static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530108 unsigned int freq)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530109{
Viresh Kumarda27f692017-04-25 15:57:21 +0530110 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530111 unsigned long level;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000112
Viresh Kumarda27f692017-04-25 15:57:21 +0530113 for (level = 1; level <= cpufreq_cdev->max_level; level++)
114 if (freq > freq_table[level].frequency)
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530115 break;
Zhang Ruia1167762014-01-02 11:57:48 +0800116
Viresh Kumarda27f692017-04-25 15:57:21 +0530117 return level - 1;
Zhang Ruifc35b352013-02-08 13:09:32 +0800118}
119
Eduardo Valentin44952d32013-04-17 17:12:05 +0000120/**
Viresh Kumar349d39d2017-04-25 15:57:19 +0530121 * update_freq_table() - Update the freq table with power numbers
122 * @cpufreq_cdev: the cpufreq cooling device in which to update the table
Javi Merinoc36cf072015-02-26 19:00:29 +0000123 * @capacitance: dynamic power coefficient for these cpus
124 *
Viresh Kumar349d39d2017-04-25 15:57:19 +0530125 * Update the freq table with power numbers. This table will be used in
126 * cpu_power_to_freq() and cpu_freq_to_power() to convert between power and
127 * frequency efficiently. Power is stored in mW, frequency in KHz. The
128 * resulting table is in descending order.
Javi Merinoc36cf072015-02-26 19:00:29 +0000129 *
Javi Merino459ac372015-08-17 19:21:42 +0100130 * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
Viresh Kumar349d39d2017-04-25 15:57:19 +0530131 * or -ENOMEM if we run out of memory.
Javi Merinoc36cf072015-02-26 19:00:29 +0000132 */
Viresh Kumar349d39d2017-04-25 15:57:19 +0530133static int update_freq_table(struct cpufreq_cooling_device *cpufreq_cdev,
134 u32 capacitance)
Javi Merinoc36cf072015-02-26 19:00:29 +0000135{
Viresh Kumar349d39d2017-04-25 15:57:19 +0530136 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000137 struct dev_pm_opp *opp;
138 struct device *dev = NULL;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530139 int num_opps = 0, cpu = cpufreq_cdev->policy->cpu, i;
Javi Merinoc36cf072015-02-26 19:00:29 +0000140
Viresh Kumar02bacb22017-04-25 15:57:17 +0530141 dev = get_cpu_device(cpu);
142 if (unlikely(!dev)) {
Daniel Lezcano72554a72019-04-28 11:51:05 +0200143 pr_warn("No cpu device for cpu %d\n", cpu);
Viresh Kumar02bacb22017-04-25 15:57:17 +0530144 return -ENODEV;
Javi Merinoc36cf072015-02-26 19:00:29 +0000145 }
146
Viresh Kumar02bacb22017-04-25 15:57:17 +0530147 num_opps = dev_pm_opp_get_opp_count(dev);
148 if (num_opps < 0)
149 return num_opps;
150
Viresh Kumar349d39d2017-04-25 15:57:19 +0530151 /*
152 * The cpufreq table is also built from the OPP table and so the count
153 * should match.
154 */
155 if (num_opps != cpufreq_cdev->max_level + 1) {
156 dev_warn(dev, "Number of OPPs not matching with max_levels\n");
Javi Merino459ac372015-08-17 19:21:42 +0100157 return -EINVAL;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530158 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000159
Viresh Kumar349d39d2017-04-25 15:57:19 +0530160 for (i = 0; i <= cpufreq_cdev->max_level; i++) {
161 unsigned long freq = freq_table[i].frequency * 1000;
162 u32 freq_mhz = freq_table[i].frequency / 1000;
Javi Merinoc36cf072015-02-26 19:00:29 +0000163 u64 power;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530164 u32 voltage_mv;
Javi Merinoc36cf072015-02-26 19:00:29 +0000165
Viresh Kumar349d39d2017-04-25 15:57:19 +0530166 /*
167 * Find ceil frequency as 'freq' may be slightly lower than OPP
168 * freq due to truncation while converting to kHz.
169 */
170 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
171 if (IS_ERR(opp)) {
172 dev_err(dev, "failed to get opp for %lu frequency\n",
173 freq);
174 return -EINVAL;
Javi Merino459ac372015-08-17 19:21:42 +0100175 }
176
Javi Merinoc36cf072015-02-26 19:00:29 +0000177 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530178 dev_pm_opp_put(opp);
Javi Merinoc36cf072015-02-26 19:00:29 +0000179
180 /*
181 * Do the multiplication with MHz and millivolt so as
182 * to not overflow.
183 */
184 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
185 do_div(power, 1000000000);
186
Javi Merinoc36cf072015-02-26 19:00:29 +0000187 /* power is stored in mW */
Viresh Kumar349d39d2017-04-25 15:57:19 +0530188 freq_table[i].power = power;
Javi Merinoeba4f882015-08-17 19:21:43 +0100189 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000190
Javi Merino459ac372015-08-17 19:21:42 +0100191 return 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000192}
193
Viresh Kumar1dea4322017-04-25 15:57:10 +0530194static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000195 u32 freq)
196{
197 int i;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530198 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000199
Viresh Kumar349d39d2017-04-25 15:57:19 +0530200 for (i = 1; i <= cpufreq_cdev->max_level; i++)
201 if (freq > freq_table[i].frequency)
Javi Merinoc36cf072015-02-26 19:00:29 +0000202 break;
203
Viresh Kumar349d39d2017-04-25 15:57:19 +0530204 return freq_table[i - 1].power;
Javi Merinoc36cf072015-02-26 19:00:29 +0000205}
206
Viresh Kumar1dea4322017-04-25 15:57:10 +0530207static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000208 u32 power)
209{
210 int i;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530211 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000212
Viresh Kumar349d39d2017-04-25 15:57:19 +0530213 for (i = 1; i <= cpufreq_cdev->max_level; i++)
214 if (power > freq_table[i].power)
Javi Merinoc36cf072015-02-26 19:00:29 +0000215 break;
216
Viresh Kumar349d39d2017-04-25 15:57:19 +0530217 return freq_table[i - 1].frequency;
Javi Merinoc36cf072015-02-26 19:00:29 +0000218}
219
220/**
221 * get_load() - get load for a cpu since last updated
Viresh Kumar1dea4322017-04-25 15:57:10 +0530222 * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu
Javi Merinoc36cf072015-02-26 19:00:29 +0000223 * @cpu: cpu number
Viresh Kumarba76dd92017-04-25 15:57:18 +0530224 * @cpu_idx: index of the cpu in time_in_idle*
Javi Merinoc36cf072015-02-26 19:00:29 +0000225 *
226 * Return: The average load of cpu @cpu in percentage since this
227 * function was last called.
228 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530229static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
Javi Merinoa53b8392016-02-11 12:00:51 +0000230 int cpu_idx)
Javi Merinoc36cf072015-02-26 19:00:29 +0000231{
232 u32 load;
233 u64 now, now_idle, delta_time, delta_idle;
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530234 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
Javi Merinoc36cf072015-02-26 19:00:29 +0000235
236 now_idle = get_cpu_idle_time(cpu, &now, 0);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530237 delta_idle = now_idle - idle_time->time;
238 delta_time = now - idle_time->timestamp;
Javi Merinoc36cf072015-02-26 19:00:29 +0000239
240 if (delta_time <= delta_idle)
241 load = 0;
242 else
243 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
244
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530245 idle_time->time = now_idle;
246 idle_time->timestamp = now;
Javi Merinoc36cf072015-02-26 19:00:29 +0000247
248 return load;
249}
250
251/**
Javi Merinoc36cf072015-02-26 19:00:29 +0000252 * get_dynamic_power() - calculate the dynamic power
Viresh Kumar1dea4322017-04-25 15:57:10 +0530253 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
Javi Merinoc36cf072015-02-26 19:00:29 +0000254 * @freq: current frequency
255 *
256 * Return: the dynamic power consumed by the cpus described by
Viresh Kumar1dea4322017-04-25 15:57:10 +0530257 * @cpufreq_cdev.
Javi Merinoc36cf072015-02-26 19:00:29 +0000258 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530259static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000260 unsigned long freq)
261{
262 u32 raw_cpu_power;
263
Viresh Kumar1dea4322017-04-25 15:57:10 +0530264 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
265 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530266}
267
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000268/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530269
270/**
271 * cpufreq_get_max_state - callback function to get the max cooling state.
272 * @cdev: thermal cooling device pointer.
273 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000274 *
275 * Callback for the thermal cooling device to return the cpufreq
276 * max cooling state.
277 *
278 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530279 */
280static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
281 unsigned long *state)
282{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530283 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530284
Viresh Kumar1dea4322017-04-25 15:57:10 +0530285 *state = cpufreq_cdev->max_level;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530286 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530287}
288
289/**
290 * cpufreq_get_cur_state - callback function to get the current cooling state.
291 * @cdev: thermal cooling device pointer.
292 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000293 *
294 * Callback for the thermal cooling device to return the cpufreq
295 * current cooling state.
296 *
297 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530298 */
299static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
300 unsigned long *state)
301{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530302 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530303
Viresh Kumar1dea4322017-04-25 15:57:10 +0530304 *state = cpufreq_cdev->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000305
hongbo.zhang160b7d82012-10-30 17:48:59 +0100306 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530307}
308
309/**
310 * cpufreq_set_cur_state - callback function to set the current cooling state.
311 * @cdev: thermal cooling device pointer.
312 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fd2013-04-17 17:12:14 +0000313 *
314 * Callback for the thermal cooling device to change the cpufreq
315 * current cooling state.
316 *
317 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530318 */
319static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
320 unsigned long state)
321{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530322 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530323
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530324 /* Request state should be less than max_level */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530325 if (WARN_ON(state > cpufreq_cdev->max_level))
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530326 return -EINVAL;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530327
Viresh Kumar5194fe42014-12-04 09:42:00 +0530328 /* Check if the old cooling action is same as new cooling action */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530329 if (cpufreq_cdev->cpufreq_state == state)
Viresh Kumar5194fe42014-12-04 09:42:00 +0530330 return 0;
331
Viresh Kumar1dea4322017-04-25 15:57:10 +0530332 cpufreq_cdev->cpufreq_state = state;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530333
Viresh Kumar51308022019-07-23 11:44:02 +0530334 return dev_pm_qos_update_request(&cpufreq_cdev->qos_req,
335 cpufreq_cdev->freq_table[state].frequency);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530336}
337
Javi Merinoc36cf072015-02-26 19:00:29 +0000338/**
339 * cpufreq_get_requested_power() - get the current power
340 * @cdev: &thermal_cooling_device pointer
341 * @tz: a valid thermal zone device pointer
342 * @power: pointer in which to store the resulting power
343 *
344 * Calculate the current power consumption of the cpus in milliwatts
345 * and store it in @power. This function should actually calculate
346 * the requested power, but it's hard to get the frequency that
347 * cpufreq would have assigned if there were no thermal limits.
348 * Instead, we calculate the current power on the assumption that the
349 * immediate future will look like the immediate past.
350 *
351 * We use the current frequency and the average load since this
352 * function was last called. In reality, there could have been
353 * multiple opps since this function was last called and that affects
354 * the load calculation. While it's not perfectly accurate, this
355 * simplification is good enough and works. REVISIT this, as more
356 * complex code may be needed if experiments show that it's not
357 * accurate enough.
358 *
359 * Return: 0 on success, -E* if getting the static power failed.
360 */
361static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
362 struct thermal_zone_device *tz,
363 u32 *power)
364{
365 unsigned long freq;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530366 int i = 0, cpu;
367 u32 total_load = 0;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530368 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530369 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merino6828a472015-03-02 17:17:20 +0000370 u32 *load_cpu = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000371
Viresh Kumarba76dd92017-04-25 15:57:18 +0530372 freq = cpufreq_quick_get(policy->cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000373
Javi Merino6828a472015-03-02 17:17:20 +0000374 if (trace_thermal_power_cpu_get_power_enabled()) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530375 u32 ncpus = cpumask_weight(policy->related_cpus);
Javi Merino6828a472015-03-02 17:17:20 +0000376
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530377 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
Javi Merino6828a472015-03-02 17:17:20 +0000378 }
379
Viresh Kumarba76dd92017-04-25 15:57:18 +0530380 for_each_cpu(cpu, policy->related_cpus) {
Javi Merinoc36cf072015-02-26 19:00:29 +0000381 u32 load;
382
383 if (cpu_online(cpu))
Viresh Kumar1dea4322017-04-25 15:57:10 +0530384 load = get_load(cpufreq_cdev, cpu, i);
Javi Merinoc36cf072015-02-26 19:00:29 +0000385 else
386 load = 0;
387
388 total_load += load;
Matthias Kaehlckebf45ac12019-05-02 11:32:38 -0700389 if (load_cpu)
Javi Merino6828a472015-03-02 17:17:20 +0000390 load_cpu[i] = load;
391
392 i++;
Javi Merinoc36cf072015-02-26 19:00:29 +0000393 }
394
Viresh Kumar1dea4322017-04-25 15:57:10 +0530395 cpufreq_cdev->last_load = total_load;
Javi Merinoc36cf072015-02-26 19:00:29 +0000396
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530397 *power = get_dynamic_power(cpufreq_cdev, freq);
Javi Merino6828a472015-03-02 17:17:20 +0000398
399 if (load_cpu) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530400 trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530401 load_cpu, i, *power);
Javi Merino6828a472015-03-02 17:17:20 +0000402
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530403 kfree(load_cpu);
Javi Merino6828a472015-03-02 17:17:20 +0000404 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000405
Javi Merinoc36cf072015-02-26 19:00:29 +0000406 return 0;
407}
408
409/**
410 * cpufreq_state2power() - convert a cpu cdev state to power consumed
411 * @cdev: &thermal_cooling_device pointer
412 * @tz: a valid thermal zone device pointer
413 * @state: cooling device state to be converted
414 * @power: pointer in which to store the resulting power
415 *
416 * Convert cooling device state @state into power consumption in
417 * milliwatts assuming 100% load. Store the calculated power in
418 * @power.
419 *
420 * Return: 0 on success, -EINVAL if the cooling device state could not
421 * be converted into a frequency or other -E* if there was an error
422 * when calculating the static power.
423 */
424static int cpufreq_state2power(struct thermal_cooling_device *cdev,
425 struct thermal_zone_device *tz,
426 unsigned long state, u32 *power)
427{
428 unsigned int freq, num_cpus;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530429 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Javi Merinoc36cf072015-02-26 19:00:29 +0000430
Viresh Kumarcb1b6312017-04-25 15:57:23 +0530431 /* Request state should be less than max_level */
432 if (WARN_ON(state > cpufreq_cdev->max_level))
433 return -EINVAL;
434
Viresh Kumarba76dd92017-04-25 15:57:18 +0530435 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
Javi Merinoc36cf072015-02-26 19:00:29 +0000436
Viresh Kumar349d39d2017-04-25 15:57:19 +0530437 freq = cpufreq_cdev->freq_table[state].frequency;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530438 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
Javi Merinoc36cf072015-02-26 19:00:29 +0000439
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530440 return 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000441}
442
443/**
444 * cpufreq_power2state() - convert power to a cooling device state
445 * @cdev: &thermal_cooling_device pointer
446 * @tz: a valid thermal zone device pointer
447 * @power: power in milliwatts to be converted
448 * @state: pointer in which to store the resulting state
449 *
450 * Calculate a cooling device state for the cpus described by @cdev
451 * that would allow them to consume at most @power mW and store it in
452 * @state. Note that this calculation depends on external factors
453 * such as the cpu load or the current static power. Calling this
454 * function with the same power as input can yield different cooling
455 * device states depending on those external factors.
456 *
457 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
458 * the calculated frequency could not be converted to a valid state.
459 * The latter should not happen unless the frequencies available to
460 * cpufreq have changed since the initialization of the cpu cooling
461 * device.
462 */
463static int cpufreq_power2state(struct thermal_cooling_device *cdev,
464 struct thermal_zone_device *tz, u32 power,
465 unsigned long *state)
466{
Shaokun Zhange0fda732019-02-18 14:22:30 +0800467 unsigned int target_freq;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530468 u32 last_load, normalised_power;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530469 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530470 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merinoc36cf072015-02-26 19:00:29 +0000471
Viresh Kumar1dea4322017-04-25 15:57:10 +0530472 last_load = cpufreq_cdev->last_load ?: 1;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530473 normalised_power = (power * 100) / last_load;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530474 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000475
Viresh Kumar3e08b2d2017-04-25 15:57:12 +0530476 *state = get_level(cpufreq_cdev, target_freq);
Viresh Kumarba76dd92017-04-25 15:57:18 +0530477 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
478 power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000479 return 0;
480}
481
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530482/* Bind cpufreq callbacks to thermal cooling device ops */
Brendan Jackmana305a432016-08-17 16:14:59 +0100483
Javi Merinoc36cf072015-02-26 19:00:29 +0000484static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530485 .get_max_state = cpufreq_get_max_state,
486 .get_cur_state = cpufreq_get_cur_state,
487 .set_cur_state = cpufreq_set_cur_state,
488};
489
Brendan Jackmana305a432016-08-17 16:14:59 +0100490static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
491 .get_max_state = cpufreq_get_max_state,
492 .get_cur_state = cpufreq_get_cur_state,
493 .set_cur_state = cpufreq_set_cur_state,
494 .get_requested_power = cpufreq_get_requested_power,
495 .state2power = cpufreq_state2power,
496 .power2state = cpufreq_power2state,
497};
498
Viresh Kumarf6859012014-12-04 09:42:06 +0530499static unsigned int find_next_max(struct cpufreq_frequency_table *table,
500 unsigned int prev_max)
501{
502 struct cpufreq_frequency_table *pos;
503 unsigned int max = 0;
504
505 cpufreq_for_each_valid_entry(pos, table) {
506 if (pos->frequency > max && pos->frequency < prev_max)
507 max = pos->frequency;
508 }
509
510 return max;
511}
512
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530513/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400514 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
515 * @np: a valid struct device_node to the cooling device device tree node
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530516 * @policy: cpufreq policy
Viresh Kumar405fb822014-12-04 09:41:55 +0530517 * Normally this should be same as cpufreq policy->related_cpus.
Javi Merinoc36cf072015-02-26 19:00:29 +0000518 * @capacitance: dynamic power coefficient for these cpus
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000519 *
520 * This interface function registers the cpufreq cooling device with the name
521 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400522 * cooling devices. It also gives the opportunity to link the cooling device
523 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000524 *
525 * Return: a valid struct thermal_cooling_device pointer on success,
526 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530527 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400528static struct thermal_cooling_device *
529__cpufreq_cooling_register(struct device_node *np,
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530530 struct cpufreq_policy *policy, u32 capacitance)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530531{
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530532 struct thermal_cooling_device *cdev;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530533 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530534 char dev_name[THERMAL_NAME_LENGTH];
Javi Merinoc36cf072015-02-26 19:00:29 +0000535 unsigned int freq, i, num_cpus;
Viresh Kumar51308022019-07-23 11:44:02 +0530536 struct device *dev;
Viresh Kumar405fb822014-12-04 09:41:55 +0530537 int ret;
Brendan Jackmana305a432016-08-17 16:14:59 +0100538 struct thermal_cooling_device_ops *cooling_ops;
Viresh Kumar51308022019-07-23 11:44:02 +0530539
540 dev = get_cpu_device(policy->cpu);
541 if (unlikely(!dev)) {
542 pr_warn("No cpu device for cpu %d\n", policy->cpu);
543 return ERR_PTR(-ENODEV);
544 }
545
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530546
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530547 if (IS_ERR_OR_NULL(policy)) {
Arvind Yadavb2fd7082017-10-24 13:20:39 +0530548 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530549 return ERR_PTR(-EINVAL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530550 }
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530551
Viresh Kumar55d85292017-04-25 15:57:15 +0530552 i = cpufreq_table_count_valid_entries(policy);
553 if (!i) {
554 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
555 __func__);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530556 return ERR_PTR(-ENODEV);
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530557 }
558
Viresh Kumar1dea4322017-04-25 15:57:10 +0530559 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530560 if (!cpufreq_cdev)
561 return ERR_PTR(-ENOMEM);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530562
Viresh Kumarb12b6512017-04-25 15:57:16 +0530563 cpufreq_cdev->policy = policy;
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530564 num_cpus = cpumask_weight(policy->related_cpus);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530565 cpufreq_cdev->idle_time = kcalloc(num_cpus,
566 sizeof(*cpufreq_cdev->idle_time),
567 GFP_KERNEL);
568 if (!cpufreq_cdev->idle_time) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530569 cdev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +0000570 goto free_cdev;
571 }
572
Viresh Kumar55d85292017-04-25 15:57:15 +0530573 /* max_level is an index, not a counter */
574 cpufreq_cdev->max_level = i - 1;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530575
Viresh Kumarf19b1a12017-05-23 12:33:06 +0530576 cpufreq_cdev->freq_table = kmalloc_array(i,
577 sizeof(*cpufreq_cdev->freq_table),
578 GFP_KERNEL);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530579 if (!cpufreq_cdev->freq_table) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530580 cdev = ERR_PTR(-ENOMEM);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530581 goto free_idle_time;
Viresh Kumarf6859012014-12-04 09:42:06 +0530582 }
583
Matthew Wilcoxae606082016-12-21 09:47:05 -0800584 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
585 if (ret < 0) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530586 cdev = ERR_PTR(ret);
Viresh Kumar349d39d2017-04-25 15:57:19 +0530587 goto free_table;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530588 }
Viresh Kumar1dea4322017-04-25 15:57:10 +0530589 cpufreq_cdev->id = ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530590
Viresh Kumar349d39d2017-04-25 15:57:19 +0530591 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
592 cpufreq_cdev->id);
593
Viresh Kumarf6859012014-12-04 09:42:06 +0530594 /* Fill freq-table in descending order of frequencies */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530595 for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) {
Viresh Kumar55d85292017-04-25 15:57:15 +0530596 freq = find_next_max(policy->freq_table, freq);
Viresh Kumar349d39d2017-04-25 15:57:19 +0530597 cpufreq_cdev->freq_table[i].frequency = freq;
Viresh Kumarf6859012014-12-04 09:42:06 +0530598
599 /* Warn for duplicate entries */
600 if (!freq)
601 pr_warn("%s: table has duplicate entries\n", __func__);
602 else
603 pr_debug("%s: freq:%u KHz\n", __func__, freq);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530604 }
Viresh Kumarf6859012014-12-04 09:42:06 +0530605
Viresh Kumar349d39d2017-04-25 15:57:19 +0530606 if (capacitance) {
Viresh Kumar349d39d2017-04-25 15:57:19 +0530607 ret = update_freq_table(cpufreq_cdev, capacitance);
608 if (ret) {
609 cdev = ERR_PTR(ret);
610 goto remove_ida;
611 }
612
613 cooling_ops = &cpufreq_power_cooling_ops;
614 } else {
615 cooling_ops = &cpufreq_cooling_ops;
616 }
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100617
Viresh Kumar51308022019-07-23 11:44:02 +0530618 ret = dev_pm_qos_add_request(dev, &cpufreq_cdev->qos_req,
619 DEV_PM_QOS_MAX_FREQUENCY,
620 cpufreq_cdev->freq_table[0].frequency);
621 if (ret < 0) {
622 pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
623 ret);
624 cdev = ERR_PTR(ret);
625 goto remove_ida;
626 }
627
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530628 cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev,
629 cooling_ops);
630 if (IS_ERR(cdev))
Viresh Kumar51308022019-07-23 11:44:02 +0530631 goto remove_qos_req;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530632
Russell King02373d72015-08-12 15:22:16 +0530633 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530634 list_add(&cpufreq_cdev->node, &cpufreq_cdev_list);
Matthew Wilcox088db932017-03-10 18:33:28 +0000635 mutex_unlock(&cooling_list_lock);
636
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530637 return cdev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530638
Viresh Kumar51308022019-07-23 11:44:02 +0530639remove_qos_req:
640 dev_pm_qos_remove_request(&cpufreq_cdev->qos_req);
Matthew Wilcoxae606082016-12-21 09:47:05 -0800641remove_ida:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530642 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumarf6859012014-12-04 09:42:06 +0530643free_table:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530644 kfree(cpufreq_cdev->freq_table);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530645free_idle_time:
646 kfree(cpufreq_cdev->idle_time);
Viresh Kumar730abe02014-12-04 09:41:58 +0530647free_cdev:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530648 kfree(cpufreq_cdev);
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530649 return cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530650}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400651
652/**
653 * cpufreq_cooling_register - function to create cpufreq cooling device.
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530654 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400655 *
656 * This interface function registers the cpufreq cooling device with the name
657 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
658 * cooling devices.
659 *
660 * Return: a valid struct thermal_cooling_device pointer on success,
661 * on failure, it returns a corresponding ERR_PTR().
662 */
663struct thermal_cooling_device *
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530664cpufreq_cooling_register(struct cpufreq_policy *policy)
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400665{
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530666 return __cpufreq_cooling_register(NULL, policy, 0);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400667}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000668EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530669
670/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400671 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530672 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400673 *
674 * This interface function registers the cpufreq cooling device with the name
675 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
676 * cooling devices. Using this API, the cpufreq cooling device will be
677 * linked to the device tree node provided.
678 *
Javi Merinoc36cf072015-02-26 19:00:29 +0000679 * Using this function, the cooling device will implement the power
680 * extensions by using a simple cpu power model. The cpus must have
681 * registered their OPPs using the OPP library.
682 *
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530683 * It also takes into account, if property present in policy CPU node, the
684 * static power consumed by the cpu.
Javi Merinoc36cf072015-02-26 19:00:29 +0000685 *
686 * Return: a valid struct thermal_cooling_device pointer on success,
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530687 * and NULL on failure.
Javi Merinoc36cf072015-02-26 19:00:29 +0000688 */
689struct thermal_cooling_device *
Viresh Kumar3ebb62f2017-12-05 11:02:45 +0530690of_cpufreq_cooling_register(struct cpufreq_policy *policy)
Javi Merinoc36cf072015-02-26 19:00:29 +0000691{
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530692 struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
693 struct thermal_cooling_device *cdev = NULL;
694 u32 capacitance = 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000695
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530696 if (!np) {
697 pr_err("cpu_cooling: OF node not available for cpu%d\n",
698 policy->cpu);
699 return NULL;
700 }
701
702 if (of_find_property(np, "#cooling-cells", NULL)) {
703 of_property_read_u32(np, "dynamic-power-coefficient",
704 &capacitance);
705
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530706 cdev = __cpufreq_cooling_register(np, policy, capacitance);
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530707 if (IS_ERR(cdev)) {
Amit Kucheriabf78f132019-01-21 15:12:23 +0530708 pr_err("cpu_cooling: cpu%d failed to register as cooling device: %ld\n",
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530709 policy->cpu, PTR_ERR(cdev));
710 cdev = NULL;
711 }
712 }
713
714 of_node_put(np);
715 return cdev;
Javi Merinoc36cf072015-02-26 19:00:29 +0000716}
Viresh Kumar3ebb62f2017-12-05 11:02:45 +0530717EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
Javi Merinoc36cf072015-02-26 19:00:29 +0000718
719/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530720 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
721 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000722 *
723 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530724 */
725void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
726{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530727 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530728
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400729 if (!cdev)
730 return;
731
Viresh Kumar1dea4322017-04-25 15:57:10 +0530732 cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530733
Matthew Wilcoxae606082016-12-21 09:47:05 -0800734 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530735 list_del(&cpufreq_cdev->node);
Matthew Wilcox088db932017-03-10 18:33:28 +0000736 mutex_unlock(&cooling_list_lock);
737
Daniel Lezcano72554a72019-04-28 11:51:05 +0200738 thermal_cooling_device_unregister(cdev);
Viresh Kumar51308022019-07-23 11:44:02 +0530739 dev_pm_qos_remove_request(&cpufreq_cdev->qos_req);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530740 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530741 kfree(cpufreq_cdev->idle_time);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530742 kfree(cpufreq_cdev->freq_table);
743 kfree(cpufreq_cdev);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530744}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000745EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);