blob: 2c4a63a880d4b6b3198e81497a8fddda002ce677 [file] [log] [blame]
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301/*
2 * linux/drivers/thermal/cpu_cooling.c
3 *
4 * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
5 * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org>
6 *
Viresh Kumar73904cb2014-12-04 09:42:08 +05307 * Copyright (C) 2014 Viresh Kumar <viresh.kumar@linaro.org>
8 *
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05309 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053025#include <linux/module.h>
26#include <linux/thermal.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053027#include <linux/cpufreq.h>
28#include <linux/err.h>
Javi Merinoc36cf072015-02-26 19:00:29 +000029#include <linux/pm_opp.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053030#include <linux/slab.h>
31#include <linux/cpu.h>
32#include <linux/cpu_cooling.h>
Lina Iyer97a13ed2016-07-15 14:53:58 -060033#include <linux/sched.h>
Lina Iyer986fde12016-02-23 13:08:31 -070034#include <linux/of_device.h>
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -060035#include <linux/suspend.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053036
Javi Merino6828a472015-03-02 17:17:20 +000037#include <trace/events/thermal.h>
38
Viresh Kumar07d888d2014-12-04 09:41:49 +053039/*
40 * Cooling state <-> CPUFreq frequency
41 *
42 * Cooling states are translated to frequencies throughout this driver and this
43 * is the relation between them.
44 *
45 * Highest cooling state corresponds to lowest possible frequency.
46 *
47 * i.e.
48 * level 0 --> 1st Max Freq
49 * level 1 --> 2nd Max Freq
50 * ...
Lina Iyer97a13ed2016-07-15 14:53:58 -060051 * leven n --> core isolated
Viresh Kumar07d888d2014-12-04 09:41:49 +053052 */
53
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053054/**
Javi Merinoc36cf072015-02-26 19:00:29 +000055 * struct power_table - frequency to power conversion
56 * @frequency: frequency in KHz
57 * @power: power in mW
58 *
59 * This structure is built when the cooling device registers and helps
60 * in translating frequency to power and viceversa.
61 */
62struct power_table {
63 u32 frequency;
64 u32 power;
65};
66
67/**
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000068 * struct cpufreq_cooling_device - data for cooling device with cpufreq
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053069 * @id: unique integer value corresponding to each cpufreq_cooling_device
70 * registered.
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000071 * @cool_dev: thermal_cooling_device pointer to keep track of the
72 * registered cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053073 * @cpufreq_state: integer value representing the current state of cpufreq
74 * cooling devices.
Viresh Kumar59f0d212015-07-30 12:40:33 +053075 * @clipped_freq: integer value representing the absolute value of the clipped
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053076 * frequency.
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -060077 * @cpufreq_floor_state: integer value representing the frequency floor state
78 * of cpufreq cooling devices.
79 * @floor_freq: integer value representing the absolute value of the floor
80 * frequency.
Lina Iyer97a13ed2016-07-15 14:53:58 -060081 * @max_level: maximum cooling level. [0..max_level-1: <freq>
82 * max_level: Core unavailable]
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053083 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
Javi Merinofc4de352014-12-15 16:55:52 +000084 * @node: list_head to link all cpufreq_cooling_device together.
Hugh Kang0744f132016-09-07 09:35:39 +090085 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
Javi Merinoc36cf072015-02-26 19:00:29 +000086 * @time_in_idle: previous reading of the absolute time that this cpu was idle
87 * @time_in_idle_timestamp: wall time of the last invocation of
88 * get_cpu_idle_time_us()
89 * @dyn_power_table: array of struct power_table for frequency to power
90 * conversion, sorted in ascending order.
91 * @dyn_power_table_entries: number of entries in the @dyn_power_table array
92 * @cpu_dev: the first cpu_device from @allowed_cpus that has OPPs registered
93 * @plat_get_static_power: callback to calculate the static power
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053094 *
Viresh Kumarbeca6052014-12-04 09:41:48 +053095 * This structure is required for keeping information of each registered
96 * cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053097 */
98struct cpufreq_cooling_device {
99 int id;
100 struct thermal_cooling_device *cool_dev;
101 unsigned int cpufreq_state;
Viresh Kumar59f0d212015-07-30 12:40:33 +0530102 unsigned int clipped_freq;
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600103 unsigned int cpufreq_floor_state;
104 unsigned int floor_freq;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530105 unsigned int max_level;
Viresh Kumarf6859012014-12-04 09:42:06 +0530106 unsigned int *freq_table; /* In descending order */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530107 struct cpumask allowed_cpus;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530108 struct list_head node;
Javi Merinoc36cf072015-02-26 19:00:29 +0000109 u32 last_load;
110 u64 *time_in_idle;
111 u64 *time_in_idle_timestamp;
112 struct power_table *dyn_power_table;
113 int dyn_power_table_entries;
114 struct device *cpu_dev;
115 get_static_t plat_get_static_power;
Lina Iyer986fde12016-02-23 13:08:31 -0700116 struct cpu_cooling_ops *plat_ops;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530117};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530118static DEFINE_IDR(cpufreq_idr);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100119static DEFINE_MUTEX(cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530120
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600121static atomic_t in_suspend;
Russell King02373d72015-08-12 15:22:16 +0530122static unsigned int cpufreq_dev_count;
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600123static int8_t cpuhp_registered;
124static struct work_struct cpuhp_register_work;
125static struct cpumask cpus_pending_online;
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600126static struct cpumask cpus_isolated_by_thermal;
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600127static DEFINE_MUTEX(core_isolate_lock);
Russell King02373d72015-08-12 15:22:16 +0530128
129static DEFINE_MUTEX(cooling_list_lock);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530130static LIST_HEAD(cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530131
132/**
133 * get_idr - function to get a unique id.
134 * @idr: struct idr * handle used to create a id.
135 * @id: int * value generated by this function.
Eduardo Valentin79491e52013-04-17 17:11:59 +0000136 *
137 * This function will populate @id with an unique
138 * id, using the idr API.
139 *
140 * Return: 0 on success, an error code on failure.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530141 */
142static int get_idr(struct idr *idr, int *id)
143{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800144 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530145
146 mutex_lock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800147 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530148 mutex_unlock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800149 if (unlikely(ret < 0))
150 return ret;
151 *id = ret;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000152
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530153 return 0;
154}
155
156/**
157 * release_idr - function to free the unique id.
158 * @idr: struct idr * handle used for creating the id.
159 * @id: int value representing the unique id.
160 */
161static void release_idr(struct idr *idr, int id)
162{
163 mutex_lock(&cooling_cpufreq_lock);
164 idr_remove(idr, id);
165 mutex_unlock(&cooling_cpufreq_lock);
166}
167
168/* Below code defines functions to be used for cpufreq as cooling device */
169
170/**
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530171 * get_level: Find the level for a particular frequency
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530172 * @cpufreq_dev: cpufreq_dev for which the property is required
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530173 * @freq: Frequency
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000174 *
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530175 * Return: level on success, THERMAL_CSTATE_INVALID on error.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530176 */
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530177static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_dev,
178 unsigned int freq)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530179{
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530180 unsigned long level;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000181
Lina Iyer97a13ed2016-07-15 14:53:58 -0600182 for (level = 0; level < cpufreq_dev->max_level; level++) {
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530183 if (freq == cpufreq_dev->freq_table[level])
184 return level;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530185
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530186 if (freq > cpufreq_dev->freq_table[level])
187 break;
Zhang Ruifc35b352013-02-08 13:09:32 +0800188 }
Zhang Ruia1167762014-01-02 11:57:48 +0800189
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530190 return THERMAL_CSTATE_INVALID;
Zhang Ruifc35b352013-02-08 13:09:32 +0800191}
192
Eduardo Valentin44952d32013-04-17 17:12:05 +0000193/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530194 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
Eduardo Valentin44952d32013-04-17 17:12:05 +0000195 * @cpu: cpu for which the level is required
196 * @freq: the frequency of interest
197 *
198 * This function will match the cooling level corresponding to the
199 * requested @freq and return it.
200 *
201 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
202 * otherwise.
203 */
Zhang Rui57df8102013-02-08 14:52:06 +0800204unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
205{
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530206 struct cpufreq_cooling_device *cpufreq_dev;
Zhang Rui57df8102013-02-08 14:52:06 +0800207
Russell King02373d72015-08-12 15:22:16 +0530208 mutex_lock(&cooling_list_lock);
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530209 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
210 if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
Viresh Kumar7cd7b562017-04-25 15:57:08 +0530211 unsigned long level = get_level(cpufreq_dev, freq);
212
Russell King02373d72015-08-12 15:22:16 +0530213 mutex_unlock(&cooling_list_lock);
Viresh Kumar7cd7b562017-04-25 15:57:08 +0530214 return level;
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530215 }
216 }
Russell King02373d72015-08-12 15:22:16 +0530217 mutex_unlock(&cooling_list_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000218
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530219 pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu);
220 return THERMAL_CSTATE_INVALID;
Zhang Rui57df8102013-02-08 14:52:06 +0800221}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000222EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
Zhang Rui57df8102013-02-08 14:52:06 +0800223
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600224static int cpufreq_cooling_pm_notify(struct notifier_block *nb,
225 unsigned long mode, void *_unused)
226{
227 struct cpufreq_cooling_device *cpufreq_dev;
228 unsigned int cpu;
229
230 switch (mode) {
231 case PM_HIBERNATION_PREPARE:
232 case PM_RESTORE_PREPARE:
233 case PM_SUSPEND_PREPARE:
234 atomic_set(&in_suspend, 1);
235 break;
236 case PM_POST_HIBERNATION:
237 case PM_POST_RESTORE:
238 case PM_POST_SUSPEND:
239 mutex_lock(&cooling_list_lock);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600240 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
Ram Chandrasekar2d02e0a2018-01-18 16:58:07 -0700241 mutex_lock(&core_isolate_lock);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600242 if (cpufreq_dev->cpufreq_state ==
243 cpufreq_dev->max_level) {
244 cpu = cpumask_any(&cpufreq_dev->allowed_cpus);
Ram Chandrasekar2d02e0a2018-01-18 16:58:07 -0700245 /*
246 * Unlock this lock before calling
247 * schedule_isolate. as this could lead to
248 * deadlock with hotplug path.
249 */
250 mutex_unlock(&core_isolate_lock);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600251 if (cpu_online(cpu) &&
252 !cpumask_test_and_set_cpu(cpu,
253 &cpus_isolated_by_thermal)) {
254 if (sched_isolate_cpu(cpu))
255 cpumask_clear_cpu(cpu,
256 &cpus_isolated_by_thermal);
257 }
Ram Chandrasekar2d02e0a2018-01-18 16:58:07 -0700258 continue;
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600259 }
Ram Chandrasekar2d02e0a2018-01-18 16:58:07 -0700260 mutex_unlock(&core_isolate_lock);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600261 }
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600262 mutex_unlock(&cooling_list_lock);
263
264 atomic_set(&in_suspend, 0);
265 break;
266 default:
267 break;
268 }
269 return 0;
270}
271
272static struct notifier_block cpufreq_cooling_pm_nb = {
273 .notifier_call = cpufreq_cooling_pm_notify,
274};
275
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600276static int cpufreq_hp_offline(unsigned int offline_cpu)
277{
278 struct cpufreq_cooling_device *cpufreq_dev;
279
280 mutex_lock(&cooling_list_lock);
281 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
282 if (!cpumask_test_cpu(offline_cpu, &cpufreq_dev->allowed_cpus))
283 continue;
284
285 mutex_lock(&core_isolate_lock);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600286 if ((cpufreq_dev->cpufreq_state == cpufreq_dev->max_level) &&
287 (cpumask_test_and_clear_cpu(offline_cpu,
288 &cpus_isolated_by_thermal)))
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600289 sched_unisolate_cpu_unlocked(offline_cpu);
290 mutex_unlock(&core_isolate_lock);
291 break;
292 }
293 mutex_unlock(&cooling_list_lock);
294
295 return 0;
296}
297
298static int cpufreq_hp_online(unsigned int online_cpu)
299{
300 struct cpufreq_cooling_device *cpufreq_dev;
301 int ret = 0;
302
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600303 if (atomic_read(&in_suspend))
304 return 0;
305
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600306 mutex_lock(&cooling_list_lock);
307 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
308 if (!cpumask_test_cpu(online_cpu, &cpufreq_dev->allowed_cpus))
309 continue;
310
311 mutex_lock(&core_isolate_lock);
312 if (cpufreq_dev->cpufreq_state == cpufreq_dev->max_level) {
313 cpumask_set_cpu(online_cpu, &cpus_pending_online);
314 ret = NOTIFY_BAD;
315 }
316 mutex_unlock(&core_isolate_lock);
317 break;
318 }
319 mutex_unlock(&cooling_list_lock);
320
321 return ret;
322}
323
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530324/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530325 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
326 * @nb: struct notifier_block * with callback info.
327 * @event: value showing cpufreq event for which this function invoked.
328 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000329 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100330 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000331 * Every time there is a change in policy, we will intercept and
332 * update the cpufreq policy with thermal constraints.
333 *
334 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530335 */
336static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000337 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530338{
339 struct cpufreq_policy *policy = data;
Manaf Meethalavalappu Pallikunhic7b1c572018-01-16 21:07:30 +0530340 unsigned long clipped_freq = ULONG_MAX, floor_freq = 0;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530341 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530342
Viresh Kumara24af232015-07-30 12:40:32 +0530343 if (event != CPUFREQ_ADJUST)
Javi Merinoc36cf072015-02-26 19:00:29 +0000344 return NOTIFY_DONE;
Viresh Kumara24af232015-07-30 12:40:32 +0530345
346 mutex_lock(&cooling_list_lock);
347 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
Manaf Meethalavalappu Pallikunhic7b1c572018-01-16 21:07:30 +0530348 if (!cpumask_intersects(&cpufreq_dev->allowed_cpus,
349 policy->related_cpus))
Viresh Kumara24af232015-07-30 12:40:32 +0530350 continue;
Manaf Meethalavalappu Pallikunhic7b1c572018-01-16 21:07:30 +0530351 if (cpufreq_dev->clipped_freq < clipped_freq)
352 clipped_freq = cpufreq_dev->clipped_freq;
353 if (cpufreq_dev->floor_freq > floor_freq)
354 floor_freq = cpufreq_dev->floor_freq;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530355 }
Manaf Meethalavalappu Pallikunhic7b1c572018-01-16 21:07:30 +0530356 /*
357 * policy->max is the maximum allowed frequency defined by user
358 * and clipped_freq is the maximum that thermal constraints
359 * allow.
360 *
361 * If clipped_freq is lower than policy->max, then we need to
362 * readjust policy->max.
363 *
364 * But, if clipped_freq is greater than policy->max, we don't
365 * need to do anything.
366 *
367 * Similarly, if policy minimum set by the user is less than
368 * the floor_frequency, then adjust the policy->min.
369 */
370 if (policy->max > clipped_freq || policy->min < floor_freq)
371 cpufreq_verify_within_limits(policy, floor_freq, clipped_freq);
Viresh Kumara24af232015-07-30 12:40:32 +0530372 mutex_unlock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530373
Javi Merinoc36cf072015-02-26 19:00:29 +0000374 return NOTIFY_OK;
375}
376
377/**
378 * build_dyn_power_table() - create a dynamic power to frequency table
379 * @cpufreq_device: the cpufreq cooling device in which to store the table
380 * @capacitance: dynamic power coefficient for these cpus
381 *
382 * Build a dynamic power to frequency table for this cpu and store it
383 * in @cpufreq_device. This table will be used in cpu_power_to_freq() and
384 * cpu_freq_to_power() to convert between power and frequency
385 * efficiently. Power is stored in mW, frequency in KHz. The
386 * resulting table is in ascending order.
387 *
Javi Merino459ac372015-08-17 19:21:42 +0100388 * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
389 * -ENOMEM if we run out of memory or -EAGAIN if an OPP was
390 * added/enabled while the function was executing.
Javi Merinoc36cf072015-02-26 19:00:29 +0000391 */
392static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_device,
393 u32 capacitance)
394{
395 struct power_table *power_table;
396 struct dev_pm_opp *opp;
397 struct device *dev = NULL;
Javi Merinoeba4f882015-08-17 19:21:43 +0100398 int num_opps = 0, cpu, i, ret = 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000399 unsigned long freq;
400
Javi Merinoc36cf072015-02-26 19:00:29 +0000401 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
402 dev = get_cpu_device(cpu);
403 if (!dev) {
404 dev_warn(&cpufreq_device->cool_dev->device,
405 "No cpu device for cpu %d\n", cpu);
406 continue;
407 }
408
409 num_opps = dev_pm_opp_get_opp_count(dev);
Javi Merino459ac372015-08-17 19:21:42 +0100410 if (num_opps > 0)
Javi Merinoc36cf072015-02-26 19:00:29 +0000411 break;
Javi Merino459ac372015-08-17 19:21:42 +0100412 else if (num_opps < 0)
413 return num_opps;
Javi Merinoc36cf072015-02-26 19:00:29 +0000414 }
415
Javi Merino459ac372015-08-17 19:21:42 +0100416 if (num_opps == 0)
417 return -EINVAL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000418
419 power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL);
Javi Merino459ac372015-08-17 19:21:42 +0100420 if (!power_table)
421 return -ENOMEM;
422
423 rcu_read_lock();
Javi Merinoc36cf072015-02-26 19:00:29 +0000424
425 for (freq = 0, i = 0;
426 opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp);
427 freq++, i++) {
428 u32 freq_mhz, voltage_mv;
429 u64 power;
430
Javi Merino459ac372015-08-17 19:21:42 +0100431 if (i >= num_opps) {
432 rcu_read_unlock();
Javi Merinoeba4f882015-08-17 19:21:43 +0100433 ret = -EAGAIN;
434 goto free_power_table;
Javi Merino459ac372015-08-17 19:21:42 +0100435 }
436
Javi Merinoc36cf072015-02-26 19:00:29 +0000437 freq_mhz = freq / 1000000;
438 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
439
440 /*
441 * Do the multiplication with MHz and millivolt so as
442 * to not overflow.
443 */
444 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
445 do_div(power, 1000000000);
446
447 /* frequency is stored in power_table in KHz */
448 power_table[i].frequency = freq / 1000;
449
450 /* power is stored in mW */
451 power_table[i].power = power;
452 }
453
Javi Merino459ac372015-08-17 19:21:42 +0100454 rcu_read_unlock();
455
Javi Merinoeba4f882015-08-17 19:21:43 +0100456 if (i != num_opps) {
457 ret = PTR_ERR(opp);
458 goto free_power_table;
459 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000460
461 cpufreq_device->cpu_dev = dev;
462 cpufreq_device->dyn_power_table = power_table;
463 cpufreq_device->dyn_power_table_entries = i;
464
Javi Merino459ac372015-08-17 19:21:42 +0100465 return 0;
Javi Merinoeba4f882015-08-17 19:21:43 +0100466
467free_power_table:
468 kfree(power_table);
469
470 return ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000471}
472
473static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_device,
474 u32 freq)
475{
476 int i;
477 struct power_table *pt = cpufreq_device->dyn_power_table;
478
479 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
480 if (freq < pt[i].frequency)
481 break;
482
483 return pt[i - 1].power;
484}
485
486static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_device,
487 u32 power)
488{
489 int i;
490 struct power_table *pt = cpufreq_device->dyn_power_table;
491
492 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
493 if (power < pt[i].power)
494 break;
495
496 return pt[i - 1].frequency;
497}
498
499/**
500 * get_load() - get load for a cpu since last updated
501 * @cpufreq_device: &struct cpufreq_cooling_device for this cpu
502 * @cpu: cpu number
Javi Merinoa53b8392016-02-11 12:00:51 +0000503 * @cpu_idx: index of the cpu in cpufreq_device->allowed_cpus
Javi Merinoc36cf072015-02-26 19:00:29 +0000504 *
505 * Return: The average load of cpu @cpu in percentage since this
506 * function was last called.
507 */
Javi Merinoa53b8392016-02-11 12:00:51 +0000508static u32 get_load(struct cpufreq_cooling_device *cpufreq_device, int cpu,
509 int cpu_idx)
Javi Merinoc36cf072015-02-26 19:00:29 +0000510{
511 u32 load;
512 u64 now, now_idle, delta_time, delta_idle;
513
514 now_idle = get_cpu_idle_time(cpu, &now, 0);
Javi Merinoa53b8392016-02-11 12:00:51 +0000515 delta_idle = now_idle - cpufreq_device->time_in_idle[cpu_idx];
516 delta_time = now - cpufreq_device->time_in_idle_timestamp[cpu_idx];
Javi Merinoc36cf072015-02-26 19:00:29 +0000517
518 if (delta_time <= delta_idle)
519 load = 0;
520 else
521 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
522
Javi Merinoa53b8392016-02-11 12:00:51 +0000523 cpufreq_device->time_in_idle[cpu_idx] = now_idle;
524 cpufreq_device->time_in_idle_timestamp[cpu_idx] = now;
Javi Merinoc36cf072015-02-26 19:00:29 +0000525
526 return load;
527}
528
529/**
530 * get_static_power() - calculate the static power consumed by the cpus
531 * @cpufreq_device: struct &cpufreq_cooling_device for this cpu cdev
532 * @tz: thermal zone device in which we're operating
533 * @freq: frequency in KHz
534 * @power: pointer in which to store the calculated static power
535 *
536 * Calculate the static power consumed by the cpus described by
537 * @cpu_actor running at frequency @freq. This function relies on a
538 * platform specific function that should have been provided when the
539 * actor was registered. If it wasn't, the static power is assumed to
540 * be negligible. The calculated static power is stored in @power.
541 *
542 * Return: 0 on success, -E* on failure.
543 */
544static int get_static_power(struct cpufreq_cooling_device *cpufreq_device,
545 struct thermal_zone_device *tz, unsigned long freq,
546 u32 *power)
547{
548 struct dev_pm_opp *opp;
549 unsigned long voltage;
550 struct cpumask *cpumask = &cpufreq_device->allowed_cpus;
551 unsigned long freq_hz = freq * 1000;
552
553 if (!cpufreq_device->plat_get_static_power ||
554 !cpufreq_device->cpu_dev) {
555 *power = 0;
556 return 0;
557 }
558
559 rcu_read_lock();
560
561 opp = dev_pm_opp_find_freq_exact(cpufreq_device->cpu_dev, freq_hz,
562 true);
563 voltage = dev_pm_opp_get_voltage(opp);
564
565 rcu_read_unlock();
566
567 if (voltage == 0) {
568 dev_warn_ratelimited(cpufreq_device->cpu_dev,
569 "Failed to get voltage for frequency %lu: %ld\n",
570 freq_hz, IS_ERR(opp) ? PTR_ERR(opp) : 0);
571 return -EINVAL;
572 }
573
574 return cpufreq_device->plat_get_static_power(cpumask, tz->passive_delay,
575 voltage, power);
576}
577
578/**
579 * get_dynamic_power() - calculate the dynamic power
580 * @cpufreq_device: &cpufreq_cooling_device for this cdev
581 * @freq: current frequency
582 *
583 * Return: the dynamic power consumed by the cpus described by
584 * @cpufreq_device.
585 */
586static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_device,
587 unsigned long freq)
588{
589 u32 raw_cpu_power;
590
591 raw_cpu_power = cpu_freq_to_power(cpufreq_device, freq);
592 return (raw_cpu_power * cpufreq_device->last_load) / 100;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530593}
594
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000595/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530596
597/**
598 * cpufreq_get_max_state - callback function to get the max cooling state.
599 * @cdev: thermal cooling device pointer.
600 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000601 *
602 * Callback for the thermal cooling device to return the cpufreq
603 * max cooling state.
604 *
605 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530606 */
607static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
608 unsigned long *state)
609{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100610 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530611
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530612 *state = cpufreq_device->max_level;
613 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530614}
615
616/**
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600617 * cpufreq_get_min_state - callback function to get the device floor state.
618 * @cdev: thermal cooling device pointer.
619 * @state: fill this variable with the cooling device floor.
620 *
621 * Callback for the thermal cooling device to return the cpufreq
622 * floor state.
623 *
624 * Return: 0 on success, an error code otherwise.
625 */
626static int cpufreq_get_min_state(struct thermal_cooling_device *cdev,
627 unsigned long *state)
628{
629 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
630
631 *state = cpufreq_device->cpufreq_floor_state;
632
633 return 0;
634}
635
636/**
637 * cpufreq_set_min_state - callback function to set the device floor state.
638 * @cdev: thermal cooling device pointer.
639 * @state: set this variable to the current cooling state.
640 *
641 * Callback for the thermal cooling device to change the cpufreq
642 * floor state.
643 *
644 * Return: 0 on success, an error code otherwise.
645 */
646static int cpufreq_set_min_state(struct thermal_cooling_device *cdev,
647 unsigned long state)
648{
649 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
650 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
651 unsigned int floor_freq;
652
653 if (state > cpufreq_device->max_level)
654 state = cpufreq_device->max_level;
655
656 if (cpufreq_device->cpufreq_floor_state == state)
657 return 0;
658
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600659 cpufreq_device->cpufreq_floor_state = state;
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600660
Ram Chandrasekar23b2db42017-04-19 13:23:31 -0600661 /*
662 * Check if the device has a platform mitigation function that
663 * can handle the CPU freq mitigation, if not, notify cpufreq
664 * framework.
665 */
666 if (cpufreq_device->plat_ops &&
667 cpufreq_device->plat_ops->floor_limit) {
668 /*
669 * Last level is core isolation so use the frequency
670 * of previous state.
671 */
672 if (state == cpufreq_device->max_level)
673 state--;
674 floor_freq = cpufreq_device->freq_table[state];
675 cpufreq_device->floor_freq = floor_freq;
676 cpufreq_device->plat_ops->floor_limit(cpu, floor_freq);
677 } else {
678 floor_freq = cpufreq_device->freq_table[state];
679 cpufreq_device->floor_freq = floor_freq;
680 cpufreq_update_policy(cpu);
681 }
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600682
683 return 0;
684}
685
686/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530687 * cpufreq_get_cur_state - callback function to get the current cooling state.
688 * @cdev: thermal cooling device pointer.
689 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000690 *
691 * Callback for the thermal cooling device to return the cpufreq
692 * current cooling state.
693 *
694 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530695 */
696static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
697 unsigned long *state)
698{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100699 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530700
hongbo.zhang160b7d82012-10-30 17:48:59 +0100701 *state = cpufreq_device->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000702
hongbo.zhang160b7d82012-10-30 17:48:59 +0100703 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530704}
705
706/**
707 * cpufreq_set_cur_state - callback function to set the current cooling state.
708 * @cdev: thermal cooling device pointer.
709 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fdb2013-04-17 17:12:14 +0000710 *
711 * Callback for the thermal cooling device to change the cpufreq
712 * current cooling state.
713 *
714 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530715 */
716static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
717 unsigned long state)
718{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100719 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530720 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
721 unsigned int clip_freq;
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600722 unsigned long prev_state;
723 struct device *cpu_dev;
724 int ret = 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530725
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530726 /* Request state should be less than max_level */
727 if (WARN_ON(state > cpufreq_device->max_level))
728 return -EINVAL;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530729
Viresh Kumar5194fe42014-12-04 09:42:00 +0530730 /* Check if the old cooling action is same as new cooling action */
731 if (cpufreq_device->cpufreq_state == state)
732 return 0;
733
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600734 mutex_lock(&core_isolate_lock);
735 prev_state = cpufreq_device->cpufreq_state;
Ram Chandrasekarbd080e42017-05-25 15:49:20 -0600736 cpufreq_device->cpufreq_state = state;
Ram Chandrasekar2d02e0a2018-01-18 16:58:07 -0700737 mutex_unlock(&core_isolate_lock);
Lina Iyer97a13ed2016-07-15 14:53:58 -0600738 /* If state is the last, isolate the CPU */
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600739 if (state == cpufreq_device->max_level) {
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600740 if (cpu_online(cpu) &&
741 (!cpumask_test_and_set_cpu(cpu,
742 &cpus_isolated_by_thermal))) {
743 if (sched_isolate_cpu(cpu))
744 cpumask_clear_cpu(cpu,
745 &cpus_isolated_by_thermal);
746 }
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600747 return ret;
748 } else if ((prev_state == cpufreq_device->max_level)
749 && (state < cpufreq_device->max_level)) {
750 if (cpumask_test_and_clear_cpu(cpu, &cpus_pending_online)) {
751 cpu_dev = get_cpu_device(cpu);
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600752 ret = device_online(cpu_dev);
753 if (ret)
754 pr_err("CPU:%d online error:%d\n", cpu, ret);
755 goto update_frequency;
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600756 } else if (cpumask_test_and_clear_cpu(cpu,
757 &cpus_isolated_by_thermal)) {
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600758 sched_unisolate_cpu(cpu);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600759 }
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600760 }
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600761update_frequency:
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530762 clip_freq = cpufreq_device->freq_table[state];
Viresh Kumar59f0d212015-07-30 12:40:33 +0530763 cpufreq_device->clipped_freq = clip_freq;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530764
Lina Iyer986fde12016-02-23 13:08:31 -0700765 /* Check if the device has a platform mitigation function that
766 * can handle the CPU freq mitigation, if not, notify cpufreq
767 * framework.
768 */
769 if (cpufreq_device->plat_ops) {
770 if (cpufreq_device->plat_ops->ceil_limit)
771 cpufreq_device->plat_ops->ceil_limit(cpu,
772 clip_freq);
773 } else {
774 cpufreq_update_policy(cpu);
775 }
Viresh Kumar5194fe42014-12-04 09:42:00 +0530776
777 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530778}
779
Javi Merinoc36cf072015-02-26 19:00:29 +0000780/**
781 * cpufreq_get_requested_power() - get the current power
782 * @cdev: &thermal_cooling_device pointer
783 * @tz: a valid thermal zone device pointer
784 * @power: pointer in which to store the resulting power
785 *
786 * Calculate the current power consumption of the cpus in milliwatts
787 * and store it in @power. This function should actually calculate
788 * the requested power, but it's hard to get the frequency that
789 * cpufreq would have assigned if there were no thermal limits.
790 * Instead, we calculate the current power on the assumption that the
791 * immediate future will look like the immediate past.
792 *
793 * We use the current frequency and the average load since this
794 * function was last called. In reality, there could have been
795 * multiple opps since this function was last called and that affects
796 * the load calculation. While it's not perfectly accurate, this
797 * simplification is good enough and works. REVISIT this, as more
798 * complex code may be needed if experiments show that it's not
799 * accurate enough.
800 *
801 * Return: 0 on success, -E* if getting the static power failed.
802 */
803static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
804 struct thermal_zone_device *tz,
805 u32 *power)
806{
807 unsigned long freq;
Javi Merino6828a472015-03-02 17:17:20 +0000808 int i = 0, cpu, ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000809 u32 static_power, dynamic_power, total_load = 0;
810 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Javi Merino6828a472015-03-02 17:17:20 +0000811 u32 *load_cpu = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000812
Kapileshwar Singhdd658e02015-03-16 12:00:51 +0000813 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
814
815 /*
816 * All the CPUs are offline, thus the requested power by
817 * the cdev is 0
818 */
819 if (cpu >= nr_cpu_ids) {
820 *power = 0;
821 return 0;
822 }
823
824 freq = cpufreq_quick_get(cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000825
Javi Merino6828a472015-03-02 17:17:20 +0000826 if (trace_thermal_power_cpu_get_power_enabled()) {
827 u32 ncpus = cpumask_weight(&cpufreq_device->allowed_cpus);
828
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530829 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
Javi Merino6828a472015-03-02 17:17:20 +0000830 }
831
Javi Merinoc36cf072015-02-26 19:00:29 +0000832 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
833 u32 load;
834
835 if (cpu_online(cpu))
Javi Merinoa53b8392016-02-11 12:00:51 +0000836 load = get_load(cpufreq_device, cpu, i);
Javi Merinoc36cf072015-02-26 19:00:29 +0000837 else
838 load = 0;
839
840 total_load += load;
Javi Merino6828a472015-03-02 17:17:20 +0000841 if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
842 load_cpu[i] = load;
843
844 i++;
Javi Merinoc36cf072015-02-26 19:00:29 +0000845 }
846
847 cpufreq_device->last_load = total_load;
848
849 dynamic_power = get_dynamic_power(cpufreq_device, freq);
850 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
Javi Merino6828a472015-03-02 17:17:20 +0000851 if (ret) {
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530852 kfree(load_cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000853 return ret;
Javi Merino6828a472015-03-02 17:17:20 +0000854 }
855
856 if (load_cpu) {
857 trace_thermal_power_cpu_get_power(
858 &cpufreq_device->allowed_cpus,
859 freq, load_cpu, i, dynamic_power, static_power);
860
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530861 kfree(load_cpu);
Javi Merino6828a472015-03-02 17:17:20 +0000862 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000863
864 *power = static_power + dynamic_power;
865 return 0;
866}
867
868/**
869 * cpufreq_state2power() - convert a cpu cdev state to power consumed
870 * @cdev: &thermal_cooling_device pointer
871 * @tz: a valid thermal zone device pointer
872 * @state: cooling device state to be converted
873 * @power: pointer in which to store the resulting power
874 *
875 * Convert cooling device state @state into power consumption in
876 * milliwatts assuming 100% load. Store the calculated power in
877 * @power.
878 *
879 * Return: 0 on success, -EINVAL if the cooling device state could not
880 * be converted into a frequency or other -E* if there was an error
881 * when calculating the static power.
882 */
883static int cpufreq_state2power(struct thermal_cooling_device *cdev,
884 struct thermal_zone_device *tz,
885 unsigned long state, u32 *power)
886{
887 unsigned int freq, num_cpus;
888 cpumask_t cpumask;
889 u32 static_power, dynamic_power;
890 int ret;
891 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
892
893 cpumask_and(&cpumask, &cpufreq_device->allowed_cpus, cpu_online_mask);
894 num_cpus = cpumask_weight(&cpumask);
895
896 /* None of our cpus are online, so no power */
897 if (num_cpus == 0) {
898 *power = 0;
899 return 0;
900 }
901
902 freq = cpufreq_device->freq_table[state];
903 if (!freq)
904 return -EINVAL;
905
906 dynamic_power = cpu_freq_to_power(cpufreq_device, freq) * num_cpus;
907 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
908 if (ret)
909 return ret;
910
911 *power = static_power + dynamic_power;
912 return 0;
913}
914
915/**
916 * cpufreq_power2state() - convert power to a cooling device state
917 * @cdev: &thermal_cooling_device pointer
918 * @tz: a valid thermal zone device pointer
919 * @power: power in milliwatts to be converted
920 * @state: pointer in which to store the resulting state
921 *
922 * Calculate a cooling device state for the cpus described by @cdev
923 * that would allow them to consume at most @power mW and store it in
924 * @state. Note that this calculation depends on external factors
925 * such as the cpu load or the current static power. Calling this
926 * function with the same power as input can yield different cooling
927 * device states depending on those external factors.
928 *
929 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
930 * the calculated frequency could not be converted to a valid state.
931 * The latter should not happen unless the frequencies available to
932 * cpufreq have changed since the initialization of the cpu cooling
933 * device.
934 */
935static int cpufreq_power2state(struct thermal_cooling_device *cdev,
936 struct thermal_zone_device *tz, u32 power,
937 unsigned long *state)
938{
939 unsigned int cpu, cur_freq, target_freq;
940 int ret;
941 s32 dyn_power;
942 u32 last_load, normalised_power, static_power;
943 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
944
945 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
946
947 /* None of our cpus are online */
948 if (cpu >= nr_cpu_ids)
949 return -ENODEV;
950
951 cur_freq = cpufreq_quick_get(cpu);
952 ret = get_static_power(cpufreq_device, tz, cur_freq, &static_power);
953 if (ret)
954 return ret;
955
956 dyn_power = power - static_power;
957 dyn_power = dyn_power > 0 ? dyn_power : 0;
958 last_load = cpufreq_device->last_load ?: 1;
959 normalised_power = (dyn_power * 100) / last_load;
960 target_freq = cpu_power_to_freq(cpufreq_device, normalised_power);
961
962 *state = cpufreq_cooling_get_level(cpu, target_freq);
963 if (*state == THERMAL_CSTATE_INVALID) {
964 dev_warn_ratelimited(&cdev->device,
965 "Failed to convert %dKHz for cpu %d into a cdev state\n",
966 target_freq, cpu);
967 return -EINVAL;
968 }
969
Javi Merino6828a472015-03-02 17:17:20 +0000970 trace_thermal_power_cpu_limit(&cpufreq_device->allowed_cpus,
971 target_freq, *state, power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000972 return 0;
973}
974
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530975/* Bind cpufreq callbacks to thermal cooling device ops */
Brendan Jackmana305a432016-08-17 16:14:59 +0100976
Javi Merinoc36cf072015-02-26 19:00:29 +0000977static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530978 .get_max_state = cpufreq_get_max_state,
979 .get_cur_state = cpufreq_get_cur_state,
980 .set_cur_state = cpufreq_set_cur_state,
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600981 .set_min_state = cpufreq_set_min_state,
982 .get_min_state = cpufreq_get_min_state,
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530983};
984
Brendan Jackmana305a432016-08-17 16:14:59 +0100985static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
986 .get_max_state = cpufreq_get_max_state,
987 .get_cur_state = cpufreq_get_cur_state,
988 .set_cur_state = cpufreq_set_cur_state,
989 .get_requested_power = cpufreq_get_requested_power,
990 .state2power = cpufreq_state2power,
991 .power2state = cpufreq_power2state,
992};
993
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530994/* Notifier for cpufreq policy change */
995static struct notifier_block thermal_cpufreq_notifier_block = {
996 .notifier_call = cpufreq_thermal_notifier,
997};
998
Viresh Kumarf6859012014-12-04 09:42:06 +0530999static unsigned int find_next_max(struct cpufreq_frequency_table *table,
1000 unsigned int prev_max)
1001{
1002 struct cpufreq_frequency_table *pos;
1003 unsigned int max = 0;
1004
1005 cpufreq_for_each_valid_entry(pos, table) {
1006 if (pos->frequency > max && pos->frequency < prev_max)
1007 max = pos->frequency;
1008 }
1009
1010 return max;
1011}
1012
Ram Chandrasekar30ca8042017-06-13 16:05:47 -06001013static void register_cdev(struct work_struct *work)
1014{
1015 int ret = 0;
1016
1017 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
1018 "cpu_cooling/no-sched", cpufreq_hp_online,
1019 cpufreq_hp_offline);
1020 if (ret < 0)
1021 pr_err("Error registering for hotpug callback:%d\n", ret);
1022}
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301023/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001024 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
1025 * @np: a valid struct device_node to the cooling device device tree node
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301026 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
Viresh Kumar405fb822014-12-04 09:41:55 +05301027 * Normally this should be same as cpufreq policy->related_cpus.
Javi Merinoc36cf072015-02-26 19:00:29 +00001028 * @capacitance: dynamic power coefficient for these cpus
1029 * @plat_static_func: function to calculate the static power consumed by these
1030 * cpus (optional)
Lina Iyer986fde12016-02-23 13:08:31 -07001031 * @plat_mitig_func: function that does the mitigation by changing the
1032 * frequencies (Optional). By default, cpufreq framweork will
1033 * be notified of the new limits.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +00001034 *
1035 * This interface function registers the cpufreq cooling device with the name
1036 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001037 * cooling devices. It also gives the opportunity to link the cooling device
1038 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +00001039 *
1040 * Return: a valid struct thermal_cooling_device pointer on success,
1041 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301042 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001043static struct thermal_cooling_device *
1044__cpufreq_cooling_register(struct device_node *np,
Javi Merinoc36cf072015-02-26 19:00:29 +00001045 const struct cpumask *clip_cpus, u32 capacitance,
Lina Iyer986fde12016-02-23 13:08:31 -07001046 get_static_t plat_static_func,
1047 struct cpu_cooling_ops *plat_ops)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301048{
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301049 struct cpufreq_policy *policy;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301050 struct thermal_cooling_device *cool_dev;
Viresh Kumar5d3bdb82014-12-04 09:41:52 +05301051 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301052 char dev_name[THERMAL_NAME_LENGTH];
Viresh Kumardcc6c7f2014-12-04 09:42:02 +05301053 struct cpufreq_frequency_table *pos, *table;
Javi Merinoc36cf072015-02-26 19:00:29 +00001054 unsigned int freq, i, num_cpus;
Viresh Kumar405fb822014-12-04 09:41:55 +05301055 int ret;
Brendan Jackmana305a432016-08-17 16:14:59 +01001056 struct thermal_cooling_device_ops *cooling_ops;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301057
Ram Chandrasekar5682d552017-05-03 16:01:53 -06001058 policy = cpufreq_cpu_get(cpumask_first(clip_cpus));
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301059 if (!policy) {
1060 pr_debug("%s: CPUFreq policy not found\n", __func__);
Eduardo Valentin0f1be512014-12-04 09:41:43 +05301061 return ERR_PTR(-EPROBE_DEFER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301062 }
Eduardo Valentin0f1be512014-12-04 09:41:43 +05301063
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301064 table = policy->freq_table;
1065 if (!table) {
1066 pr_debug("%s: CPUFreq table not found\n", __func__);
1067 cool_dev = ERR_PTR(-ENODEV);
1068 goto put_policy;
1069 }
1070
Viresh Kumar98d522f2014-12-04 09:41:50 +05301071 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301072 if (!cpufreq_dev) {
1073 cool_dev = ERR_PTR(-ENOMEM);
1074 goto put_policy;
1075 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301076
Javi Merinoc36cf072015-02-26 19:00:29 +00001077 num_cpus = cpumask_weight(clip_cpus);
1078 cpufreq_dev->time_in_idle = kcalloc(num_cpus,
1079 sizeof(*cpufreq_dev->time_in_idle),
1080 GFP_KERNEL);
1081 if (!cpufreq_dev->time_in_idle) {
1082 cool_dev = ERR_PTR(-ENOMEM);
1083 goto free_cdev;
1084 }
1085
1086 cpufreq_dev->time_in_idle_timestamp =
1087 kcalloc(num_cpus, sizeof(*cpufreq_dev->time_in_idle_timestamp),
1088 GFP_KERNEL);
1089 if (!cpufreq_dev->time_in_idle_timestamp) {
1090 cool_dev = ERR_PTR(-ENOMEM);
1091 goto free_time_in_idle;
1092 }
1093
Viresh Kumardcc6c7f2014-12-04 09:42:02 +05301094 /* Find max levels */
1095 cpufreq_for_each_valid_entry(pos, table)
1096 cpufreq_dev->max_level++;
1097
Lina Iyer97a13ed2016-07-15 14:53:58 -06001098 /* Last level will indicate the core will be isolated. */
1099 cpufreq_dev->max_level++;
1100 cpufreq_dev->freq_table = kzalloc(sizeof(*cpufreq_dev->freq_table) *
Viresh Kumarf6859012014-12-04 09:42:06 +05301101 cpufreq_dev->max_level, GFP_KERNEL);
1102 if (!cpufreq_dev->freq_table) {
Viresh Kumarf6859012014-12-04 09:42:06 +05301103 cool_dev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +00001104 goto free_time_in_idle_timestamp;
Viresh Kumarf6859012014-12-04 09:42:06 +05301105 }
1106
Viresh Kumardcc6c7f2014-12-04 09:42:02 +05301107 /* max_level is an index, not a counter */
1108 cpufreq_dev->max_level--;
1109
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301110 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
1111
Javi Merinoc36cf072015-02-26 19:00:29 +00001112 if (capacitance) {
Javi Merinoc36cf072015-02-26 19:00:29 +00001113 cpufreq_dev->plat_get_static_power = plat_static_func;
1114
1115 ret = build_dyn_power_table(cpufreq_dev, capacitance);
1116 if (ret) {
1117 cool_dev = ERR_PTR(ret);
1118 goto free_table;
1119 }
Brendan Jackmana305a432016-08-17 16:14:59 +01001120
1121 cooling_ops = &cpufreq_power_cooling_ops;
1122 } else {
1123 cooling_ops = &cpufreq_cooling_ops;
Javi Merinoc36cf072015-02-26 19:00:29 +00001124 }
1125
Lina Iyer986fde12016-02-23 13:08:31 -07001126 cpufreq_dev->plat_ops = plat_ops;
1127
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301128 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
1129 if (ret) {
Viresh Kumar730abe02014-12-04 09:41:58 +05301130 cool_dev = ERR_PTR(ret);
Javi Merinoeba4f882015-08-17 19:21:43 +01001131 goto free_power_table;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301132 }
1133
Viresh Kumarf6859012014-12-04 09:42:06 +05301134 /* Fill freq-table in descending order of frequencies */
Lina Iyer97a13ed2016-07-15 14:53:58 -06001135 for (i = 0, freq = -1; i < cpufreq_dev->max_level; i++) {
Viresh Kumarf6859012014-12-04 09:42:06 +05301136 freq = find_next_max(table, freq);
1137 cpufreq_dev->freq_table[i] = freq;
1138
1139 /* Warn for duplicate entries */
1140 if (!freq)
1141 pr_warn("%s: table has duplicate entries\n", __func__);
1142 else
1143 pr_debug("%s: freq:%u KHz\n", __func__, freq);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301144 }
Viresh Kumarf6859012014-12-04 09:42:06 +05301145
Lukasz Lubaf840ab12016-05-31 11:32:02 +01001146 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
1147 cpufreq_dev->id);
1148
1149 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
Brendan Jackmana305a432016-08-17 16:14:59 +01001150 cooling_ops);
Lukasz Lubaf840ab12016-05-31 11:32:02 +01001151 if (IS_ERR(cool_dev))
1152 goto remove_idr;
1153
Viresh Kumar59f0d212015-07-30 12:40:33 +05301154 cpufreq_dev->clipped_freq = cpufreq_dev->freq_table[0];
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -06001155 cpufreq_dev->floor_freq =
1156 cpufreq_dev->freq_table[cpufreq_dev->max_level];
1157 cpufreq_dev->cpufreq_floor_state = cpufreq_dev->max_level;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301158 cpufreq_dev->cool_dev = cool_dev;
Viresh Kumar92e615e2014-12-04 09:41:51 +05301159
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301160 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301161
Russell King02373d72015-08-12 15:22:16 +05301162 mutex_lock(&cooling_list_lock);
1163 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
1164 mutex_unlock(&cooling_list_lock);
1165
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301166 /* Register the notifier for first cpufreq cooling device */
Ram Chandrasekarcbd1a402017-10-27 12:18:49 -06001167 if (!cpufreq_dev_count++ && !cpufreq_dev->plat_ops)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301168 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +00001169 CPUFREQ_POLICY_NOTIFIER);
Ram Chandrasekar30ca8042017-06-13 16:05:47 -06001170 if (!cpuhp_registered) {
1171 cpuhp_registered = 1;
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -06001172 register_pm_notifier(&cpufreq_cooling_pm_nb);
Ram Chandrasekar30ca8042017-06-13 16:05:47 -06001173 cpumask_clear(&cpus_pending_online);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -06001174 cpumask_clear(&cpus_isolated_by_thermal);
Ram Chandrasekar30ca8042017-06-13 16:05:47 -06001175 INIT_WORK(&cpuhp_register_work, register_cdev);
1176 queue_work(system_wq, &cpuhp_register_work);
1177 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301178 mutex_unlock(&cooling_cpufreq_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +00001179
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301180 goto put_policy;
Viresh Kumar730abe02014-12-04 09:41:58 +05301181
1182remove_idr:
1183 release_idr(&cpufreq_idr, cpufreq_dev->id);
Javi Merinoeba4f882015-08-17 19:21:43 +01001184free_power_table:
1185 kfree(cpufreq_dev->dyn_power_table);
Viresh Kumarf6859012014-12-04 09:42:06 +05301186free_table:
1187 kfree(cpufreq_dev->freq_table);
Javi Merinoc36cf072015-02-26 19:00:29 +00001188free_time_in_idle_timestamp:
1189 kfree(cpufreq_dev->time_in_idle_timestamp);
1190free_time_in_idle:
1191 kfree(cpufreq_dev->time_in_idle);
Viresh Kumar730abe02014-12-04 09:41:58 +05301192free_cdev:
1193 kfree(cpufreq_dev);
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301194put_policy:
1195 cpufreq_cpu_put(policy);
Viresh Kumar730abe02014-12-04 09:41:58 +05301196
1197 return cool_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301198}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001199
1200/**
1201 * cpufreq_cooling_register - function to create cpufreq cooling device.
1202 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
1203 *
1204 * This interface function registers the cpufreq cooling device with the name
1205 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
1206 * cooling devices.
1207 *
1208 * Return: a valid struct thermal_cooling_device pointer on success,
1209 * on failure, it returns a corresponding ERR_PTR().
1210 */
1211struct thermal_cooling_device *
1212cpufreq_cooling_register(const struct cpumask *clip_cpus)
1213{
Lina Iyer986fde12016-02-23 13:08:31 -07001214 return __cpufreq_cooling_register(NULL, clip_cpus, 0, NULL, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001215}
Eduardo Valentin243dbd92013-04-17 17:11:57 +00001216EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301217
1218/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001219 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
1220 * @np: a valid struct device_node to the cooling device device tree node
1221 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
1222 *
1223 * This interface function registers the cpufreq cooling device with the name
1224 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
1225 * cooling devices. Using this API, the cpufreq cooling device will be
1226 * linked to the device tree node provided.
1227 *
1228 * Return: a valid struct thermal_cooling_device pointer on success,
1229 * on failure, it returns a corresponding ERR_PTR().
1230 */
1231struct thermal_cooling_device *
1232of_cpufreq_cooling_register(struct device_node *np,
1233 const struct cpumask *clip_cpus)
1234{
1235 if (!np)
1236 return ERR_PTR(-EINVAL);
1237
Lina Iyer986fde12016-02-23 13:08:31 -07001238 return __cpufreq_cooling_register(np, clip_cpus, 0, NULL, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001239}
1240EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
1241
1242/**
Javi Merinoc36cf072015-02-26 19:00:29 +00001243 * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
1244 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
1245 * @capacitance: dynamic power coefficient for these cpus
1246 * @plat_static_func: function to calculate the static power consumed by these
1247 * cpus (optional)
1248 *
1249 * This interface function registers the cpufreq cooling device with
1250 * the name "thermal-cpufreq-%x". This api can support multiple
1251 * instances of cpufreq cooling devices. Using this function, the
1252 * cooling device will implement the power extensions by using a
1253 * simple cpu power model. The cpus must have registered their OPPs
1254 * using the OPP library.
1255 *
1256 * An optional @plat_static_func may be provided to calculate the
1257 * static power consumed by these cpus. If the platform's static
1258 * power consumption is unknown or negligible, make it NULL.
1259 *
1260 * Return: a valid struct thermal_cooling_device pointer on success,
1261 * on failure, it returns a corresponding ERR_PTR().
1262 */
1263struct thermal_cooling_device *
1264cpufreq_power_cooling_register(const struct cpumask *clip_cpus, u32 capacitance,
1265 get_static_t plat_static_func)
1266{
1267 return __cpufreq_cooling_register(NULL, clip_cpus, capacitance,
Lina Iyer986fde12016-02-23 13:08:31 -07001268 plat_static_func, NULL);
Javi Merinoc36cf072015-02-26 19:00:29 +00001269}
1270EXPORT_SYMBOL(cpufreq_power_cooling_register);
1271
1272/**
Lina Iyer986fde12016-02-23 13:08:31 -07001273 * cpufreq_platform_cooling_register() - create cpufreq cooling device with
1274 * additional platform specific mitigation function.
1275 *
1276 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
1277 * @plat_ops: the platform mitigation functions that will be called insted of
1278 * cpufreq, if provided.
1279 *
1280 * Return: a valid struct thermal_cooling_device pointer on success,
1281 * on failure, it returns a corresponding ERR_PTR().
1282 */
1283struct thermal_cooling_device *
1284cpufreq_platform_cooling_register(const struct cpumask *clip_cpus,
1285 struct cpu_cooling_ops *plat_ops)
1286{
1287 struct device_node *cpu_node;
1288
1289 cpu_node = of_cpu_device_node_get(cpumask_first(clip_cpus));
1290 return __cpufreq_cooling_register(cpu_node, clip_cpus, 0, NULL,
1291 plat_ops);
1292}
1293EXPORT_SYMBOL(cpufreq_platform_cooling_register);
1294
1295/**
Javi Merinoc36cf072015-02-26 19:00:29 +00001296 * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
1297 * @np: a valid struct device_node to the cooling device device tree node
1298 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
1299 * @capacitance: dynamic power coefficient for these cpus
1300 * @plat_static_func: function to calculate the static power consumed by these
1301 * cpus (optional)
1302 *
1303 * This interface function registers the cpufreq cooling device with
1304 * the name "thermal-cpufreq-%x". This api can support multiple
1305 * instances of cpufreq cooling devices. Using this API, the cpufreq
1306 * cooling device will be linked to the device tree node provided.
1307 * Using this function, the cooling device will implement the power
1308 * extensions by using a simple cpu power model. The cpus must have
1309 * registered their OPPs using the OPP library.
1310 *
1311 * An optional @plat_static_func may be provided to calculate the
1312 * static power consumed by these cpus. If the platform's static
1313 * power consumption is unknown or negligible, make it NULL.
1314 *
1315 * Return: a valid struct thermal_cooling_device pointer on success,
1316 * on failure, it returns a corresponding ERR_PTR().
1317 */
1318struct thermal_cooling_device *
1319of_cpufreq_power_cooling_register(struct device_node *np,
1320 const struct cpumask *clip_cpus,
1321 u32 capacitance,
1322 get_static_t plat_static_func)
1323{
1324 if (!np)
1325 return ERR_PTR(-EINVAL);
1326
1327 return __cpufreq_cooling_register(np, clip_cpus, capacitance,
Lina Iyer986fde12016-02-23 13:08:31 -07001328 plat_static_func, NULL);
Javi Merinoc36cf072015-02-26 19:00:29 +00001329}
1330EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
1331
1332/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301333 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
1334 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +00001335 *
1336 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301337 */
1338void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
1339{
Eduardo Valentin50e66c72013-08-15 10:54:46 -04001340 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301341
Eduardo Valentin50e66c72013-08-15 10:54:46 -04001342 if (!cdev)
1343 return;
1344
1345 cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301346
1347 /* Unregister the notifier for the last cpufreq cooling device */
Russell King02373d72015-08-12 15:22:16 +05301348 mutex_lock(&cooling_cpufreq_lock);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -06001349 if (!--cpufreq_dev_count) {
1350 unregister_pm_notifier(&cpufreq_cooling_pm_nb);
1351 if (!cpufreq_dev->plat_ops)
1352 cpufreq_unregister_notifier(
1353 &thermal_cpufreq_notifier_block,
1354 CPUFREQ_POLICY_NOTIFIER);
1355 }
Russell King02373d72015-08-12 15:22:16 +05301356
1357 mutex_lock(&cooling_list_lock);
1358 list_del(&cpufreq_dev->node);
1359 mutex_unlock(&cooling_list_lock);
1360
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301361 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +01001362
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301363 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
1364 release_idr(&cpufreq_idr, cpufreq_dev->id);
Javi Merinoeba4f882015-08-17 19:21:43 +01001365 kfree(cpufreq_dev->dyn_power_table);
Javi Merinoc36cf072015-02-26 19:00:29 +00001366 kfree(cpufreq_dev->time_in_idle_timestamp);
1367 kfree(cpufreq_dev->time_in_idle);
Viresh Kumarf6859012014-12-04 09:42:06 +05301368 kfree(cpufreq_dev->freq_table);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301369 kfree(cpufreq_dev);
1370}
Eduardo Valentin243dbd92013-04-17 17:11:57 +00001371EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);