blob: 76e163ed55a0495f37d8bf2d3e54454b636e7b56 [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{
Manaf Meethalavalappu Pallikunhib7498c22018-05-25 05:44:29 +0530227 struct cpufreq_cooling_device *cpufreq_dev, *next;
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600228 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:
Manaf Meethalavalappu Pallikunhib7498c22018-05-25 05:44:29 +0530239 list_for_each_entry_safe(cpufreq_dev, next, &cpufreq_dev_list,
240 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
263 atomic_set(&in_suspend, 0);
264 break;
265 default:
266 break;
267 }
268 return 0;
269}
270
271static struct notifier_block cpufreq_cooling_pm_nb = {
272 .notifier_call = cpufreq_cooling_pm_notify,
273};
274
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600275static int cpufreq_hp_offline(unsigned int offline_cpu)
276{
277 struct cpufreq_cooling_device *cpufreq_dev;
278
279 mutex_lock(&cooling_list_lock);
280 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
281 if (!cpumask_test_cpu(offline_cpu, &cpufreq_dev->allowed_cpus))
282 continue;
283
284 mutex_lock(&core_isolate_lock);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600285 if ((cpufreq_dev->cpufreq_state == cpufreq_dev->max_level) &&
286 (cpumask_test_and_clear_cpu(offline_cpu,
287 &cpus_isolated_by_thermal)))
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600288 sched_unisolate_cpu_unlocked(offline_cpu);
289 mutex_unlock(&core_isolate_lock);
290 break;
291 }
292 mutex_unlock(&cooling_list_lock);
293
294 return 0;
295}
296
297static int cpufreq_hp_online(unsigned int online_cpu)
298{
299 struct cpufreq_cooling_device *cpufreq_dev;
300 int ret = 0;
301
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600302 if (atomic_read(&in_suspend))
303 return 0;
304
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600305 mutex_lock(&cooling_list_lock);
306 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
307 if (!cpumask_test_cpu(online_cpu, &cpufreq_dev->allowed_cpus))
308 continue;
309
310 mutex_lock(&core_isolate_lock);
311 if (cpufreq_dev->cpufreq_state == cpufreq_dev->max_level) {
312 cpumask_set_cpu(online_cpu, &cpus_pending_online);
313 ret = NOTIFY_BAD;
314 }
315 mutex_unlock(&core_isolate_lock);
316 break;
317 }
318 mutex_unlock(&cooling_list_lock);
319
320 return ret;
321}
322
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530323/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530324 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
325 * @nb: struct notifier_block * with callback info.
326 * @event: value showing cpufreq event for which this function invoked.
327 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000328 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100329 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000330 * Every time there is a change in policy, we will intercept and
331 * update the cpufreq policy with thermal constraints.
332 *
333 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530334 */
335static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000336 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530337{
338 struct cpufreq_policy *policy = data;
Manaf Meethalavalappu Pallikunhic7b1c572018-01-16 21:07:30 +0530339 unsigned long clipped_freq = ULONG_MAX, floor_freq = 0;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530340 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530341
Manaf Meethalavalappu Pallikunhi3edb5ea2018-11-28 15:44:31 +0530342 if (event != CPUFREQ_INCOMPATIBLE)
Javi Merinoc36cf072015-02-26 19:00:29 +0000343 return NOTIFY_DONE;
Viresh Kumara24af232015-07-30 12:40:32 +0530344
345 mutex_lock(&cooling_list_lock);
346 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
Manaf Meethalavalappu Pallikunhic7b1c572018-01-16 21:07:30 +0530347 if (!cpumask_intersects(&cpufreq_dev->allowed_cpus,
348 policy->related_cpus))
Viresh Kumara24af232015-07-30 12:40:32 +0530349 continue;
Manaf Meethalavalappu Pallikunhic7b1c572018-01-16 21:07:30 +0530350 if (cpufreq_dev->clipped_freq < clipped_freq)
351 clipped_freq = cpufreq_dev->clipped_freq;
352 if (cpufreq_dev->floor_freq > floor_freq)
353 floor_freq = cpufreq_dev->floor_freq;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530354 }
Manaf Meethalavalappu Pallikunhic7b1c572018-01-16 21:07:30 +0530355 /*
356 * policy->max is the maximum allowed frequency defined by user
357 * and clipped_freq is the maximum that thermal constraints
358 * allow.
359 *
360 * If clipped_freq is lower than policy->max, then we need to
361 * readjust policy->max.
362 *
363 * But, if clipped_freq is greater than policy->max, we don't
364 * need to do anything.
365 *
366 * Similarly, if policy minimum set by the user is less than
367 * the floor_frequency, then adjust the policy->min.
368 */
369 if (policy->max > clipped_freq || policy->min < floor_freq)
370 cpufreq_verify_within_limits(policy, floor_freq, clipped_freq);
Viresh Kumara24af232015-07-30 12:40:32 +0530371 mutex_unlock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530372
Javi Merinoc36cf072015-02-26 19:00:29 +0000373 return NOTIFY_OK;
374}
375
376/**
377 * build_dyn_power_table() - create a dynamic power to frequency table
378 * @cpufreq_device: the cpufreq cooling device in which to store the table
379 * @capacitance: dynamic power coefficient for these cpus
380 *
381 * Build a dynamic power to frequency table for this cpu and store it
382 * in @cpufreq_device. This table will be used in cpu_power_to_freq() and
383 * cpu_freq_to_power() to convert between power and frequency
384 * efficiently. Power is stored in mW, frequency in KHz. The
385 * resulting table is in ascending order.
386 *
Javi Merino459ac372015-08-17 19:21:42 +0100387 * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
388 * -ENOMEM if we run out of memory or -EAGAIN if an OPP was
389 * added/enabled while the function was executing.
Javi Merinoc36cf072015-02-26 19:00:29 +0000390 */
391static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_device,
392 u32 capacitance)
393{
394 struct power_table *power_table;
395 struct dev_pm_opp *opp;
396 struct device *dev = NULL;
Javi Merinoeba4f882015-08-17 19:21:43 +0100397 int num_opps = 0, cpu, i, ret = 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000398 unsigned long freq;
399
Javi Merinoc36cf072015-02-26 19:00:29 +0000400 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
401 dev = get_cpu_device(cpu);
402 if (!dev) {
403 dev_warn(&cpufreq_device->cool_dev->device,
404 "No cpu device for cpu %d\n", cpu);
405 continue;
406 }
407
408 num_opps = dev_pm_opp_get_opp_count(dev);
Javi Merino459ac372015-08-17 19:21:42 +0100409 if (num_opps > 0)
Javi Merinoc36cf072015-02-26 19:00:29 +0000410 break;
Javi Merino459ac372015-08-17 19:21:42 +0100411 else if (num_opps < 0)
412 return num_opps;
Javi Merinoc36cf072015-02-26 19:00:29 +0000413 }
414
Javi Merino459ac372015-08-17 19:21:42 +0100415 if (num_opps == 0)
416 return -EINVAL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000417
418 power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL);
Javi Merino459ac372015-08-17 19:21:42 +0100419 if (!power_table)
420 return -ENOMEM;
421
422 rcu_read_lock();
Javi Merinoc36cf072015-02-26 19:00:29 +0000423
424 for (freq = 0, i = 0;
425 opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp);
426 freq++, i++) {
427 u32 freq_mhz, voltage_mv;
428 u64 power;
429
Javi Merino459ac372015-08-17 19:21:42 +0100430 if (i >= num_opps) {
431 rcu_read_unlock();
Javi Merinoeba4f882015-08-17 19:21:43 +0100432 ret = -EAGAIN;
433 goto free_power_table;
Javi Merino459ac372015-08-17 19:21:42 +0100434 }
435
Javi Merinoc36cf072015-02-26 19:00:29 +0000436 freq_mhz = freq / 1000000;
437 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
438
439 /*
440 * Do the multiplication with MHz and millivolt so as
441 * to not overflow.
442 */
443 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
444 do_div(power, 1000000000);
445
446 /* frequency is stored in power_table in KHz */
447 power_table[i].frequency = freq / 1000;
448
449 /* power is stored in mW */
450 power_table[i].power = power;
451 }
452
Javi Merino459ac372015-08-17 19:21:42 +0100453 rcu_read_unlock();
454
Javi Merinoeba4f882015-08-17 19:21:43 +0100455 if (i != num_opps) {
456 ret = PTR_ERR(opp);
457 goto free_power_table;
458 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000459
460 cpufreq_device->cpu_dev = dev;
461 cpufreq_device->dyn_power_table = power_table;
462 cpufreq_device->dyn_power_table_entries = i;
463
Javi Merino459ac372015-08-17 19:21:42 +0100464 return 0;
Javi Merinoeba4f882015-08-17 19:21:43 +0100465
466free_power_table:
467 kfree(power_table);
468
469 return ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000470}
471
472static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_device,
473 u32 freq)
474{
475 int i;
476 struct power_table *pt = cpufreq_device->dyn_power_table;
477
478 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
479 if (freq < pt[i].frequency)
480 break;
481
482 return pt[i - 1].power;
483}
484
485static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_device,
486 u32 power)
487{
488 int i;
489 struct power_table *pt = cpufreq_device->dyn_power_table;
490
491 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
492 if (power < pt[i].power)
493 break;
494
495 return pt[i - 1].frequency;
496}
497
498/**
499 * get_load() - get load for a cpu since last updated
500 * @cpufreq_device: &struct cpufreq_cooling_device for this cpu
501 * @cpu: cpu number
Javi Merinoa53b8392016-02-11 12:00:51 +0000502 * @cpu_idx: index of the cpu in cpufreq_device->allowed_cpus
Javi Merinoc36cf072015-02-26 19:00:29 +0000503 *
504 * Return: The average load of cpu @cpu in percentage since this
505 * function was last called.
506 */
Javi Merinoa53b8392016-02-11 12:00:51 +0000507static u32 get_load(struct cpufreq_cooling_device *cpufreq_device, int cpu,
508 int cpu_idx)
Javi Merinoc36cf072015-02-26 19:00:29 +0000509{
510 u32 load;
511 u64 now, now_idle, delta_time, delta_idle;
512
513 now_idle = get_cpu_idle_time(cpu, &now, 0);
Javi Merinoa53b8392016-02-11 12:00:51 +0000514 delta_idle = now_idle - cpufreq_device->time_in_idle[cpu_idx];
515 delta_time = now - cpufreq_device->time_in_idle_timestamp[cpu_idx];
Javi Merinoc36cf072015-02-26 19:00:29 +0000516
517 if (delta_time <= delta_idle)
518 load = 0;
519 else
520 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
521
Javi Merinoa53b8392016-02-11 12:00:51 +0000522 cpufreq_device->time_in_idle[cpu_idx] = now_idle;
523 cpufreq_device->time_in_idle_timestamp[cpu_idx] = now;
Javi Merinoc36cf072015-02-26 19:00:29 +0000524
525 return load;
526}
527
528/**
529 * get_static_power() - calculate the static power consumed by the cpus
530 * @cpufreq_device: struct &cpufreq_cooling_device for this cpu cdev
531 * @tz: thermal zone device in which we're operating
532 * @freq: frequency in KHz
533 * @power: pointer in which to store the calculated static power
534 *
535 * Calculate the static power consumed by the cpus described by
536 * @cpu_actor running at frequency @freq. This function relies on a
537 * platform specific function that should have been provided when the
538 * actor was registered. If it wasn't, the static power is assumed to
539 * be negligible. The calculated static power is stored in @power.
540 *
541 * Return: 0 on success, -E* on failure.
542 */
543static int get_static_power(struct cpufreq_cooling_device *cpufreq_device,
544 struct thermal_zone_device *tz, unsigned long freq,
545 u32 *power)
546{
547 struct dev_pm_opp *opp;
548 unsigned long voltage;
549 struct cpumask *cpumask = &cpufreq_device->allowed_cpus;
550 unsigned long freq_hz = freq * 1000;
551
552 if (!cpufreq_device->plat_get_static_power ||
553 !cpufreq_device->cpu_dev) {
554 *power = 0;
555 return 0;
556 }
557
558 rcu_read_lock();
559
560 opp = dev_pm_opp_find_freq_exact(cpufreq_device->cpu_dev, freq_hz,
561 true);
562 voltage = dev_pm_opp_get_voltage(opp);
563
564 rcu_read_unlock();
565
566 if (voltage == 0) {
567 dev_warn_ratelimited(cpufreq_device->cpu_dev,
568 "Failed to get voltage for frequency %lu: %ld\n",
569 freq_hz, IS_ERR(opp) ? PTR_ERR(opp) : 0);
570 return -EINVAL;
571 }
572
573 return cpufreq_device->plat_get_static_power(cpumask, tz->passive_delay,
574 voltage, power);
575}
576
577/**
578 * get_dynamic_power() - calculate the dynamic power
579 * @cpufreq_device: &cpufreq_cooling_device for this cdev
580 * @freq: current frequency
581 *
582 * Return: the dynamic power consumed by the cpus described by
583 * @cpufreq_device.
584 */
585static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_device,
586 unsigned long freq)
587{
588 u32 raw_cpu_power;
589
590 raw_cpu_power = cpu_freq_to_power(cpufreq_device, freq);
591 return (raw_cpu_power * cpufreq_device->last_load) / 100;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530592}
593
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000594/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530595
596/**
597 * cpufreq_get_max_state - callback function to get the max cooling state.
598 * @cdev: thermal cooling device pointer.
599 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000600 *
601 * Callback for the thermal cooling device to return the cpufreq
602 * max cooling state.
603 *
604 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530605 */
606static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
607 unsigned long *state)
608{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100609 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530610
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530611 *state = cpufreq_device->max_level;
612 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530613}
614
615/**
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600616 * cpufreq_get_min_state - callback function to get the device floor state.
617 * @cdev: thermal cooling device pointer.
618 * @state: fill this variable with the cooling device floor.
619 *
620 * Callback for the thermal cooling device to return the cpufreq
621 * floor state.
622 *
623 * Return: 0 on success, an error code otherwise.
624 */
625static int cpufreq_get_min_state(struct thermal_cooling_device *cdev,
626 unsigned long *state)
627{
628 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
629
630 *state = cpufreq_device->cpufreq_floor_state;
631
632 return 0;
633}
634
635/**
636 * cpufreq_set_min_state - callback function to set the device floor state.
637 * @cdev: thermal cooling device pointer.
638 * @state: set this variable to the current cooling state.
639 *
640 * Callback for the thermal cooling device to change the cpufreq
641 * floor state.
642 *
643 * Return: 0 on success, an error code otherwise.
644 */
645static int cpufreq_set_min_state(struct thermal_cooling_device *cdev,
646 unsigned long state)
647{
648 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
649 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
650 unsigned int floor_freq;
651
652 if (state > cpufreq_device->max_level)
653 state = cpufreq_device->max_level;
654
655 if (cpufreq_device->cpufreq_floor_state == state)
656 return 0;
657
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600658 cpufreq_device->cpufreq_floor_state = state;
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600659
Ram Chandrasekar23b2db42017-04-19 13:23:31 -0600660 /*
661 * Check if the device has a platform mitigation function that
662 * can handle the CPU freq mitigation, if not, notify cpufreq
663 * framework.
664 */
665 if (cpufreq_device->plat_ops &&
666 cpufreq_device->plat_ops->floor_limit) {
667 /*
668 * Last level is core isolation so use the frequency
669 * of previous state.
670 */
671 if (state == cpufreq_device->max_level)
672 state--;
673 floor_freq = cpufreq_device->freq_table[state];
674 cpufreq_device->floor_freq = floor_freq;
675 cpufreq_device->plat_ops->floor_limit(cpu, floor_freq);
676 } else {
677 floor_freq = cpufreq_device->freq_table[state];
678 cpufreq_device->floor_freq = floor_freq;
679 cpufreq_update_policy(cpu);
680 }
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600681
682 return 0;
683}
684
685/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530686 * cpufreq_get_cur_state - callback function to get the current cooling state.
687 * @cdev: thermal cooling device pointer.
688 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000689 *
690 * Callback for the thermal cooling device to return the cpufreq
691 * current cooling state.
692 *
693 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530694 */
695static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
696 unsigned long *state)
697{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100698 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530699
hongbo.zhang160b7d82012-10-30 17:48:59 +0100700 *state = cpufreq_device->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000701
hongbo.zhang160b7d82012-10-30 17:48:59 +0100702 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530703}
704
705/**
706 * cpufreq_set_cur_state - callback function to set the current cooling state.
707 * @cdev: thermal cooling device pointer.
708 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fdb2013-04-17 17:12:14 +0000709 *
710 * Callback for the thermal cooling device to change the cpufreq
711 * current cooling state.
712 *
713 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530714 */
715static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
716 unsigned long state)
717{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100718 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530719 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
720 unsigned int clip_freq;
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600721 unsigned long prev_state;
722 struct device *cpu_dev;
723 int ret = 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530724
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530725 /* Request state should be less than max_level */
726 if (WARN_ON(state > cpufreq_device->max_level))
727 return -EINVAL;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530728
Viresh Kumar5194fe42014-12-04 09:42:00 +0530729 /* Check if the old cooling action is same as new cooling action */
730 if (cpufreq_device->cpufreq_state == state)
731 return 0;
732
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600733 mutex_lock(&core_isolate_lock);
734 prev_state = cpufreq_device->cpufreq_state;
Ram Chandrasekarbd080e42017-05-25 15:49:20 -0600735 cpufreq_device->cpufreq_state = state;
Ram Chandrasekar2d02e0a2018-01-18 16:58:07 -0700736 mutex_unlock(&core_isolate_lock);
Lina Iyer97a13ed2016-07-15 14:53:58 -0600737 /* If state is the last, isolate the CPU */
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600738 if (state == cpufreq_device->max_level) {
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600739 if (cpu_online(cpu) &&
740 (!cpumask_test_and_set_cpu(cpu,
741 &cpus_isolated_by_thermal))) {
742 if (sched_isolate_cpu(cpu))
743 cpumask_clear_cpu(cpu,
744 &cpus_isolated_by_thermal);
745 }
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600746 return ret;
747 } else if ((prev_state == cpufreq_device->max_level)
748 && (state < cpufreq_device->max_level)) {
749 if (cpumask_test_and_clear_cpu(cpu, &cpus_pending_online)) {
750 cpu_dev = get_cpu_device(cpu);
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600751 ret = device_online(cpu_dev);
752 if (ret)
753 pr_err("CPU:%d online error:%d\n", cpu, ret);
754 goto update_frequency;
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600755 } else if (cpumask_test_and_clear_cpu(cpu,
756 &cpus_isolated_by_thermal)) {
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600757 sched_unisolate_cpu(cpu);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600758 }
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600759 }
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600760update_frequency:
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530761 clip_freq = cpufreq_device->freq_table[state];
Viresh Kumar59f0d212015-07-30 12:40:33 +0530762 cpufreq_device->clipped_freq = clip_freq;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530763
Lina Iyer986fde12016-02-23 13:08:31 -0700764 /* Check if the device has a platform mitigation function that
765 * can handle the CPU freq mitigation, if not, notify cpufreq
766 * framework.
767 */
768 if (cpufreq_device->plat_ops) {
769 if (cpufreq_device->plat_ops->ceil_limit)
770 cpufreq_device->plat_ops->ceil_limit(cpu,
771 clip_freq);
772 } else {
773 cpufreq_update_policy(cpu);
774 }
Viresh Kumar5194fe42014-12-04 09:42:00 +0530775
776 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530777}
778
Javi Merinoc36cf072015-02-26 19:00:29 +0000779/**
780 * cpufreq_get_requested_power() - get the current power
781 * @cdev: &thermal_cooling_device pointer
782 * @tz: a valid thermal zone device pointer
783 * @power: pointer in which to store the resulting power
784 *
785 * Calculate the current power consumption of the cpus in milliwatts
786 * and store it in @power. This function should actually calculate
787 * the requested power, but it's hard to get the frequency that
788 * cpufreq would have assigned if there were no thermal limits.
789 * Instead, we calculate the current power on the assumption that the
790 * immediate future will look like the immediate past.
791 *
792 * We use the current frequency and the average load since this
793 * function was last called. In reality, there could have been
794 * multiple opps since this function was last called and that affects
795 * the load calculation. While it's not perfectly accurate, this
796 * simplification is good enough and works. REVISIT this, as more
797 * complex code may be needed if experiments show that it's not
798 * accurate enough.
799 *
800 * Return: 0 on success, -E* if getting the static power failed.
801 */
802static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
803 struct thermal_zone_device *tz,
804 u32 *power)
805{
806 unsigned long freq;
Javi Merino6828a472015-03-02 17:17:20 +0000807 int i = 0, cpu, ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000808 u32 static_power, dynamic_power, total_load = 0;
809 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Javi Merino6828a472015-03-02 17:17:20 +0000810 u32 *load_cpu = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000811
Kapileshwar Singhdd658e02015-03-16 12:00:51 +0000812 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
813
814 /*
815 * All the CPUs are offline, thus the requested power by
816 * the cdev is 0
817 */
818 if (cpu >= nr_cpu_ids) {
819 *power = 0;
820 return 0;
821 }
822
823 freq = cpufreq_quick_get(cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000824
Javi Merino6828a472015-03-02 17:17:20 +0000825 if (trace_thermal_power_cpu_get_power_enabled()) {
826 u32 ncpus = cpumask_weight(&cpufreq_device->allowed_cpus);
827
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530828 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
Javi Merino6828a472015-03-02 17:17:20 +0000829 }
830
Javi Merinoc36cf072015-02-26 19:00:29 +0000831 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
832 u32 load;
833
834 if (cpu_online(cpu))
Javi Merinoa53b8392016-02-11 12:00:51 +0000835 load = get_load(cpufreq_device, cpu, i);
Javi Merinoc36cf072015-02-26 19:00:29 +0000836 else
837 load = 0;
838
839 total_load += load;
Javi Merino6828a472015-03-02 17:17:20 +0000840 if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
841 load_cpu[i] = load;
842
843 i++;
Javi Merinoc36cf072015-02-26 19:00:29 +0000844 }
845
846 cpufreq_device->last_load = total_load;
847
848 dynamic_power = get_dynamic_power(cpufreq_device, freq);
849 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
Javi Merino6828a472015-03-02 17:17:20 +0000850 if (ret) {
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530851 kfree(load_cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000852 return ret;
Javi Merino6828a472015-03-02 17:17:20 +0000853 }
854
855 if (load_cpu) {
856 trace_thermal_power_cpu_get_power(
857 &cpufreq_device->allowed_cpus,
858 freq, load_cpu, i, dynamic_power, static_power);
859
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530860 kfree(load_cpu);
Javi Merino6828a472015-03-02 17:17:20 +0000861 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000862
863 *power = static_power + dynamic_power;
864 return 0;
865}
866
867/**
868 * cpufreq_state2power() - convert a cpu cdev state to power consumed
869 * @cdev: &thermal_cooling_device pointer
870 * @tz: a valid thermal zone device pointer
871 * @state: cooling device state to be converted
872 * @power: pointer in which to store the resulting power
873 *
874 * Convert cooling device state @state into power consumption in
875 * milliwatts assuming 100% load. Store the calculated power in
876 * @power.
877 *
878 * Return: 0 on success, -EINVAL if the cooling device state could not
879 * be converted into a frequency or other -E* if there was an error
880 * when calculating the static power.
881 */
882static int cpufreq_state2power(struct thermal_cooling_device *cdev,
883 struct thermal_zone_device *tz,
884 unsigned long state, u32 *power)
885{
886 unsigned int freq, num_cpus;
887 cpumask_t cpumask;
888 u32 static_power, dynamic_power;
889 int ret;
890 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
891
892 cpumask_and(&cpumask, &cpufreq_device->allowed_cpus, cpu_online_mask);
893 num_cpus = cpumask_weight(&cpumask);
894
895 /* None of our cpus are online, so no power */
896 if (num_cpus == 0) {
897 *power = 0;
898 return 0;
899 }
900
901 freq = cpufreq_device->freq_table[state];
902 if (!freq)
903 return -EINVAL;
904
905 dynamic_power = cpu_freq_to_power(cpufreq_device, freq) * num_cpus;
906 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
907 if (ret)
908 return ret;
909
910 *power = static_power + dynamic_power;
911 return 0;
912}
913
914/**
915 * cpufreq_power2state() - convert power to a cooling device state
916 * @cdev: &thermal_cooling_device pointer
917 * @tz: a valid thermal zone device pointer
918 * @power: power in milliwatts to be converted
919 * @state: pointer in which to store the resulting state
920 *
921 * Calculate a cooling device state for the cpus described by @cdev
922 * that would allow them to consume at most @power mW and store it in
923 * @state. Note that this calculation depends on external factors
924 * such as the cpu load or the current static power. Calling this
925 * function with the same power as input can yield different cooling
926 * device states depending on those external factors.
927 *
928 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
929 * the calculated frequency could not be converted to a valid state.
930 * The latter should not happen unless the frequencies available to
931 * cpufreq have changed since the initialization of the cpu cooling
932 * device.
933 */
934static int cpufreq_power2state(struct thermal_cooling_device *cdev,
935 struct thermal_zone_device *tz, u32 power,
936 unsigned long *state)
937{
938 unsigned int cpu, cur_freq, target_freq;
939 int ret;
940 s32 dyn_power;
941 u32 last_load, normalised_power, static_power;
942 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
943
944 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
945
946 /* None of our cpus are online */
947 if (cpu >= nr_cpu_ids)
948 return -ENODEV;
949
950 cur_freq = cpufreq_quick_get(cpu);
951 ret = get_static_power(cpufreq_device, tz, cur_freq, &static_power);
952 if (ret)
953 return ret;
954
955 dyn_power = power - static_power;
956 dyn_power = dyn_power > 0 ? dyn_power : 0;
957 last_load = cpufreq_device->last_load ?: 1;
958 normalised_power = (dyn_power * 100) / last_load;
959 target_freq = cpu_power_to_freq(cpufreq_device, normalised_power);
960
961 *state = cpufreq_cooling_get_level(cpu, target_freq);
962 if (*state == THERMAL_CSTATE_INVALID) {
963 dev_warn_ratelimited(&cdev->device,
964 "Failed to convert %dKHz for cpu %d into a cdev state\n",
965 target_freq, cpu);
966 return -EINVAL;
967 }
968
Javi Merino6828a472015-03-02 17:17:20 +0000969 trace_thermal_power_cpu_limit(&cpufreq_device->allowed_cpus,
970 target_freq, *state, power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000971 return 0;
972}
973
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530974/* Bind cpufreq callbacks to thermal cooling device ops */
Brendan Jackmana305a432016-08-17 16:14:59 +0100975
Javi Merinoc36cf072015-02-26 19:00:29 +0000976static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530977 .get_max_state = cpufreq_get_max_state,
978 .get_cur_state = cpufreq_get_cur_state,
979 .set_cur_state = cpufreq_set_cur_state,
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600980 .set_min_state = cpufreq_set_min_state,
981 .get_min_state = cpufreq_get_min_state,
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530982};
983
Brendan Jackmana305a432016-08-17 16:14:59 +0100984static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
985 .get_max_state = cpufreq_get_max_state,
986 .get_cur_state = cpufreq_get_cur_state,
987 .set_cur_state = cpufreq_set_cur_state,
988 .get_requested_power = cpufreq_get_requested_power,
989 .state2power = cpufreq_state2power,
990 .power2state = cpufreq_power2state,
991};
992
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530993/* Notifier for cpufreq policy change */
994static struct notifier_block thermal_cpufreq_notifier_block = {
995 .notifier_call = cpufreq_thermal_notifier,
996};
997
Viresh Kumarf6859012014-12-04 09:42:06 +0530998static unsigned int find_next_max(struct cpufreq_frequency_table *table,
999 unsigned int prev_max)
1000{
1001 struct cpufreq_frequency_table *pos;
1002 unsigned int max = 0;
1003
1004 cpufreq_for_each_valid_entry(pos, table) {
1005 if (pos->frequency > max && pos->frequency < prev_max)
1006 max = pos->frequency;
1007 }
1008
1009 return max;
1010}
1011
Ram Chandrasekar30ca8042017-06-13 16:05:47 -06001012static void register_cdev(struct work_struct *work)
1013{
1014 int ret = 0;
1015
1016 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
1017 "cpu_cooling/no-sched", cpufreq_hp_online,
1018 cpufreq_hp_offline);
1019 if (ret < 0)
1020 pr_err("Error registering for hotpug callback:%d\n", ret);
1021}
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301022/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001023 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
1024 * @np: a valid struct device_node to the cooling device device tree node
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301025 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
Viresh Kumar405fb822014-12-04 09:41:55 +05301026 * Normally this should be same as cpufreq policy->related_cpus.
Javi Merinoc36cf072015-02-26 19:00:29 +00001027 * @capacitance: dynamic power coefficient for these cpus
1028 * @plat_static_func: function to calculate the static power consumed by these
1029 * cpus (optional)
Lina Iyer986fde12016-02-23 13:08:31 -07001030 * @plat_mitig_func: function that does the mitigation by changing the
1031 * frequencies (Optional). By default, cpufreq framweork will
1032 * be notified of the new limits.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +00001033 *
1034 * This interface function registers the cpufreq cooling device with the name
1035 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001036 * cooling devices. It also gives the opportunity to link the cooling device
1037 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +00001038 *
1039 * Return: a valid struct thermal_cooling_device pointer on success,
1040 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301041 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001042static struct thermal_cooling_device *
1043__cpufreq_cooling_register(struct device_node *np,
Javi Merinoc36cf072015-02-26 19:00:29 +00001044 const struct cpumask *clip_cpus, u32 capacitance,
Lina Iyer986fde12016-02-23 13:08:31 -07001045 get_static_t plat_static_func,
1046 struct cpu_cooling_ops *plat_ops)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301047{
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301048 struct cpufreq_policy *policy;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301049 struct thermal_cooling_device *cool_dev;
Viresh Kumar5d3bdb82014-12-04 09:41:52 +05301050 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301051 char dev_name[THERMAL_NAME_LENGTH];
Viresh Kumardcc6c7f2014-12-04 09:42:02 +05301052 struct cpufreq_frequency_table *pos, *table;
Javi Merinoc36cf072015-02-26 19:00:29 +00001053 unsigned int freq, i, num_cpus;
Viresh Kumar405fb822014-12-04 09:41:55 +05301054 int ret;
Brendan Jackmana305a432016-08-17 16:14:59 +01001055 struct thermal_cooling_device_ops *cooling_ops;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301056
Ram Chandrasekar5682d552017-05-03 16:01:53 -06001057 policy = cpufreq_cpu_get(cpumask_first(clip_cpus));
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301058 if (!policy) {
1059 pr_debug("%s: CPUFreq policy not found\n", __func__);
Eduardo Valentin0f1be512014-12-04 09:41:43 +05301060 return ERR_PTR(-EPROBE_DEFER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301061 }
Eduardo Valentin0f1be512014-12-04 09:41:43 +05301062
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301063 table = policy->freq_table;
1064 if (!table) {
1065 pr_debug("%s: CPUFreq table not found\n", __func__);
1066 cool_dev = ERR_PTR(-ENODEV);
1067 goto put_policy;
1068 }
1069
Viresh Kumar98d522f2014-12-04 09:41:50 +05301070 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301071 if (!cpufreq_dev) {
1072 cool_dev = ERR_PTR(-ENOMEM);
1073 goto put_policy;
1074 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301075
Javi Merinoc36cf072015-02-26 19:00:29 +00001076 num_cpus = cpumask_weight(clip_cpus);
1077 cpufreq_dev->time_in_idle = kcalloc(num_cpus,
1078 sizeof(*cpufreq_dev->time_in_idle),
1079 GFP_KERNEL);
1080 if (!cpufreq_dev->time_in_idle) {
1081 cool_dev = ERR_PTR(-ENOMEM);
1082 goto free_cdev;
1083 }
1084
1085 cpufreq_dev->time_in_idle_timestamp =
1086 kcalloc(num_cpus, sizeof(*cpufreq_dev->time_in_idle_timestamp),
1087 GFP_KERNEL);
1088 if (!cpufreq_dev->time_in_idle_timestamp) {
1089 cool_dev = ERR_PTR(-ENOMEM);
1090 goto free_time_in_idle;
1091 }
1092
Viresh Kumardcc6c7f2014-12-04 09:42:02 +05301093 /* Find max levels */
1094 cpufreq_for_each_valid_entry(pos, table)
1095 cpufreq_dev->max_level++;
1096
Lina Iyer97a13ed2016-07-15 14:53:58 -06001097 /* Last level will indicate the core will be isolated. */
1098 cpufreq_dev->max_level++;
1099 cpufreq_dev->freq_table = kzalloc(sizeof(*cpufreq_dev->freq_table) *
Viresh Kumarf6859012014-12-04 09:42:06 +05301100 cpufreq_dev->max_level, GFP_KERNEL);
1101 if (!cpufreq_dev->freq_table) {
Viresh Kumarf6859012014-12-04 09:42:06 +05301102 cool_dev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +00001103 goto free_time_in_idle_timestamp;
Viresh Kumarf6859012014-12-04 09:42:06 +05301104 }
1105
Viresh Kumardcc6c7f2014-12-04 09:42:02 +05301106 /* max_level is an index, not a counter */
1107 cpufreq_dev->max_level--;
1108
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301109 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
1110
Javi Merinoc36cf072015-02-26 19:00:29 +00001111 if (capacitance) {
Javi Merinoc36cf072015-02-26 19:00:29 +00001112 cpufreq_dev->plat_get_static_power = plat_static_func;
1113
1114 ret = build_dyn_power_table(cpufreq_dev, capacitance);
1115 if (ret) {
1116 cool_dev = ERR_PTR(ret);
1117 goto free_table;
1118 }
Brendan Jackmana305a432016-08-17 16:14:59 +01001119
1120 cooling_ops = &cpufreq_power_cooling_ops;
1121 } else {
1122 cooling_ops = &cpufreq_cooling_ops;
Javi Merinoc36cf072015-02-26 19:00:29 +00001123 }
1124
Lina Iyer986fde12016-02-23 13:08:31 -07001125 cpufreq_dev->plat_ops = plat_ops;
1126
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301127 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
1128 if (ret) {
Viresh Kumar730abe02014-12-04 09:41:58 +05301129 cool_dev = ERR_PTR(ret);
Javi Merinoeba4f882015-08-17 19:21:43 +01001130 goto free_power_table;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301131 }
1132
Viresh Kumarf6859012014-12-04 09:42:06 +05301133 /* Fill freq-table in descending order of frequencies */
Lina Iyer97a13ed2016-07-15 14:53:58 -06001134 for (i = 0, freq = -1; i < cpufreq_dev->max_level; i++) {
Viresh Kumarf6859012014-12-04 09:42:06 +05301135 freq = find_next_max(table, freq);
1136 cpufreq_dev->freq_table[i] = freq;
1137
1138 /* Warn for duplicate entries */
1139 if (!freq)
1140 pr_warn("%s: table has duplicate entries\n", __func__);
1141 else
1142 pr_debug("%s: freq:%u KHz\n", __func__, freq);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301143 }
Viresh Kumarf6859012014-12-04 09:42:06 +05301144
Lukasz Lubaf840ab12016-05-31 11:32:02 +01001145 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
1146 cpufreq_dev->id);
1147
1148 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
Brendan Jackmana305a432016-08-17 16:14:59 +01001149 cooling_ops);
Lukasz Lubaf840ab12016-05-31 11:32:02 +01001150 if (IS_ERR(cool_dev))
1151 goto remove_idr;
1152
Viresh Kumar59f0d212015-07-30 12:40:33 +05301153 cpufreq_dev->clipped_freq = cpufreq_dev->freq_table[0];
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -06001154 cpufreq_dev->floor_freq =
1155 cpufreq_dev->freq_table[cpufreq_dev->max_level];
1156 cpufreq_dev->cpufreq_floor_state = cpufreq_dev->max_level;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301157 cpufreq_dev->cool_dev = cool_dev;
Viresh Kumar92e615e2014-12-04 09:41:51 +05301158
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301159 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301160
Russell King02373d72015-08-12 15:22:16 +05301161 mutex_lock(&cooling_list_lock);
1162 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
1163 mutex_unlock(&cooling_list_lock);
1164
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301165 /* Register the notifier for first cpufreq cooling device */
Ram Chandrasekarcbd1a402017-10-27 12:18:49 -06001166 if (!cpufreq_dev_count++ && !cpufreq_dev->plat_ops)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301167 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +00001168 CPUFREQ_POLICY_NOTIFIER);
Ram Chandrasekar30ca8042017-06-13 16:05:47 -06001169 if (!cpuhp_registered) {
1170 cpuhp_registered = 1;
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -06001171 register_pm_notifier(&cpufreq_cooling_pm_nb);
Ram Chandrasekar30ca8042017-06-13 16:05:47 -06001172 cpumask_clear(&cpus_pending_online);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -06001173 cpumask_clear(&cpus_isolated_by_thermal);
Ram Chandrasekar30ca8042017-06-13 16:05:47 -06001174 INIT_WORK(&cpuhp_register_work, register_cdev);
1175 queue_work(system_wq, &cpuhp_register_work);
1176 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301177 mutex_unlock(&cooling_cpufreq_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +00001178
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301179 goto put_policy;
Viresh Kumar730abe02014-12-04 09:41:58 +05301180
1181remove_idr:
1182 release_idr(&cpufreq_idr, cpufreq_dev->id);
Javi Merinoeba4f882015-08-17 19:21:43 +01001183free_power_table:
1184 kfree(cpufreq_dev->dyn_power_table);
Viresh Kumarf6859012014-12-04 09:42:06 +05301185free_table:
1186 kfree(cpufreq_dev->freq_table);
Javi Merinoc36cf072015-02-26 19:00:29 +00001187free_time_in_idle_timestamp:
1188 kfree(cpufreq_dev->time_in_idle_timestamp);
1189free_time_in_idle:
1190 kfree(cpufreq_dev->time_in_idle);
Viresh Kumar730abe02014-12-04 09:41:58 +05301191free_cdev:
1192 kfree(cpufreq_dev);
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301193put_policy:
1194 cpufreq_cpu_put(policy);
Viresh Kumar730abe02014-12-04 09:41:58 +05301195
1196 return cool_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301197}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001198
1199/**
1200 * cpufreq_cooling_register - function to create cpufreq cooling device.
1201 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
1202 *
1203 * This interface function registers the cpufreq cooling device with the name
1204 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
1205 * cooling devices.
1206 *
1207 * Return: a valid struct thermal_cooling_device pointer on success,
1208 * on failure, it returns a corresponding ERR_PTR().
1209 */
1210struct thermal_cooling_device *
1211cpufreq_cooling_register(const struct cpumask *clip_cpus)
1212{
Lina Iyer986fde12016-02-23 13:08:31 -07001213 return __cpufreq_cooling_register(NULL, clip_cpus, 0, NULL, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001214}
Eduardo Valentin243dbd92013-04-17 17:11:57 +00001215EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301216
1217/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001218 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
1219 * @np: a valid struct device_node to the cooling device device tree node
1220 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
1221 *
1222 * This interface function registers the cpufreq cooling device with the name
1223 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
1224 * cooling devices. Using this API, the cpufreq cooling device will be
1225 * linked to the device tree node provided.
1226 *
1227 * Return: a valid struct thermal_cooling_device pointer on success,
1228 * on failure, it returns a corresponding ERR_PTR().
1229 */
1230struct thermal_cooling_device *
1231of_cpufreq_cooling_register(struct device_node *np,
1232 const struct cpumask *clip_cpus)
1233{
1234 if (!np)
1235 return ERR_PTR(-EINVAL);
1236
Lina Iyer986fde12016-02-23 13:08:31 -07001237 return __cpufreq_cooling_register(np, clip_cpus, 0, NULL, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001238}
1239EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
1240
1241/**
Javi Merinoc36cf072015-02-26 19:00:29 +00001242 * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
1243 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
1244 * @capacitance: dynamic power coefficient for these cpus
1245 * @plat_static_func: function to calculate the static power consumed by these
1246 * cpus (optional)
1247 *
1248 * This interface function registers the cpufreq cooling device with
1249 * the name "thermal-cpufreq-%x". This api can support multiple
1250 * instances of cpufreq cooling devices. Using this function, the
1251 * cooling device will implement the power extensions by using a
1252 * simple cpu power model. The cpus must have registered their OPPs
1253 * using the OPP library.
1254 *
1255 * An optional @plat_static_func may be provided to calculate the
1256 * static power consumed by these cpus. If the platform's static
1257 * power consumption is unknown or negligible, make it NULL.
1258 *
1259 * Return: a valid struct thermal_cooling_device pointer on success,
1260 * on failure, it returns a corresponding ERR_PTR().
1261 */
1262struct thermal_cooling_device *
1263cpufreq_power_cooling_register(const struct cpumask *clip_cpus, u32 capacitance,
1264 get_static_t plat_static_func)
1265{
1266 return __cpufreq_cooling_register(NULL, clip_cpus, capacitance,
Lina Iyer986fde12016-02-23 13:08:31 -07001267 plat_static_func, NULL);
Javi Merinoc36cf072015-02-26 19:00:29 +00001268}
1269EXPORT_SYMBOL(cpufreq_power_cooling_register);
1270
1271/**
Lina Iyer986fde12016-02-23 13:08:31 -07001272 * cpufreq_platform_cooling_register() - create cpufreq cooling device with
1273 * additional platform specific mitigation function.
1274 *
1275 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
1276 * @plat_ops: the platform mitigation functions that will be called insted of
1277 * cpufreq, if provided.
1278 *
1279 * Return: a valid struct thermal_cooling_device pointer on success,
1280 * on failure, it returns a corresponding ERR_PTR().
1281 */
1282struct thermal_cooling_device *
1283cpufreq_platform_cooling_register(const struct cpumask *clip_cpus,
1284 struct cpu_cooling_ops *plat_ops)
1285{
1286 struct device_node *cpu_node;
1287
1288 cpu_node = of_cpu_device_node_get(cpumask_first(clip_cpus));
1289 return __cpufreq_cooling_register(cpu_node, clip_cpus, 0, NULL,
1290 plat_ops);
1291}
1292EXPORT_SYMBOL(cpufreq_platform_cooling_register);
1293
1294/**
Javi Merinoc36cf072015-02-26 19:00:29 +00001295 * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
1296 * @np: a valid struct device_node to the cooling device device tree node
1297 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
1298 * @capacitance: dynamic power coefficient for these cpus
1299 * @plat_static_func: function to calculate the static power consumed by these
1300 * cpus (optional)
1301 *
1302 * This interface function registers the cpufreq cooling device with
1303 * the name "thermal-cpufreq-%x". This api can support multiple
1304 * instances of cpufreq cooling devices. Using this API, the cpufreq
1305 * cooling device will be linked to the device tree node provided.
1306 * Using this function, the cooling device will implement the power
1307 * extensions by using a simple cpu power model. The cpus must have
1308 * registered their OPPs using the OPP library.
1309 *
1310 * An optional @plat_static_func may be provided to calculate the
1311 * static power consumed by these cpus. If the platform's static
1312 * power consumption is unknown or negligible, make it NULL.
1313 *
1314 * Return: a valid struct thermal_cooling_device pointer on success,
1315 * on failure, it returns a corresponding ERR_PTR().
1316 */
1317struct thermal_cooling_device *
1318of_cpufreq_power_cooling_register(struct device_node *np,
1319 const struct cpumask *clip_cpus,
1320 u32 capacitance,
1321 get_static_t plat_static_func)
1322{
1323 if (!np)
1324 return ERR_PTR(-EINVAL);
1325
1326 return __cpufreq_cooling_register(np, clip_cpus, capacitance,
Lina Iyer986fde12016-02-23 13:08:31 -07001327 plat_static_func, NULL);
Javi Merinoc36cf072015-02-26 19:00:29 +00001328}
1329EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
1330
1331/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301332 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
1333 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +00001334 *
1335 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301336 */
1337void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
1338{
Eduardo Valentin50e66c72013-08-15 10:54:46 -04001339 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301340
Eduardo Valentin50e66c72013-08-15 10:54:46 -04001341 if (!cdev)
1342 return;
1343
1344 cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301345
1346 /* Unregister the notifier for the last cpufreq cooling device */
Russell King02373d72015-08-12 15:22:16 +05301347 mutex_lock(&cooling_cpufreq_lock);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -06001348 if (!--cpufreq_dev_count) {
1349 unregister_pm_notifier(&cpufreq_cooling_pm_nb);
1350 if (!cpufreq_dev->plat_ops)
1351 cpufreq_unregister_notifier(
1352 &thermal_cpufreq_notifier_block,
1353 CPUFREQ_POLICY_NOTIFIER);
1354 }
Russell King02373d72015-08-12 15:22:16 +05301355
1356 mutex_lock(&cooling_list_lock);
1357 list_del(&cpufreq_dev->node);
1358 mutex_unlock(&cooling_list_lock);
1359
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301360 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +01001361
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301362 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
1363 release_idr(&cpufreq_idr, cpufreq_dev->id);
Javi Merinoeba4f882015-08-17 19:21:43 +01001364 kfree(cpufreq_dev->dyn_power_table);
Javi Merinoc36cf072015-02-26 19:00:29 +00001365 kfree(cpufreq_dev->time_in_idle_timestamp);
1366 kfree(cpufreq_dev->time_in_idle);
Viresh Kumarf6859012014-12-04 09:42:06 +05301367 kfree(cpufreq_dev->freq_table);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301368 kfree(cpufreq_dev);
1369}
Eduardo Valentin243dbd92013-04-17 17:11:57 +00001370EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);