blob: 27cedf54e0ece5a7de0487812add68c7a91d24d3 [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;
Manaf Meethalavalappu Pallikunhi4a4b3552019-10-22 11:07:54 +0530108 struct cpufreq_policy *policy;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530109 struct list_head node;
Javi Merinoc36cf072015-02-26 19:00:29 +0000110 u32 last_load;
111 u64 *time_in_idle;
112 u64 *time_in_idle_timestamp;
113 struct power_table *dyn_power_table;
114 int dyn_power_table_entries;
115 struct device *cpu_dev;
116 get_static_t plat_get_static_power;
Lina Iyer986fde12016-02-23 13:08:31 -0700117 struct cpu_cooling_ops *plat_ops;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530118};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530119static DEFINE_IDR(cpufreq_idr);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100120static DEFINE_MUTEX(cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530121
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600122static atomic_t in_suspend;
Russell King02373d72015-08-12 15:22:16 +0530123static unsigned int cpufreq_dev_count;
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600124static int8_t cpuhp_registered;
125static struct work_struct cpuhp_register_work;
126static struct cpumask cpus_pending_online;
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600127static struct cpumask cpus_isolated_by_thermal;
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600128static DEFINE_MUTEX(core_isolate_lock);
Russell King02373d72015-08-12 15:22:16 +0530129
130static DEFINE_MUTEX(cooling_list_lock);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530131static LIST_HEAD(cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530132
133/**
134 * get_idr - function to get a unique id.
135 * @idr: struct idr * handle used to create a id.
136 * @id: int * value generated by this function.
Eduardo Valentin79491e52013-04-17 17:11:59 +0000137 *
138 * This function will populate @id with an unique
139 * id, using the idr API.
140 *
141 * Return: 0 on success, an error code on failure.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530142 */
143static int get_idr(struct idr *idr, int *id)
144{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800145 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530146
147 mutex_lock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800148 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530149 mutex_unlock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800150 if (unlikely(ret < 0))
151 return ret;
152 *id = ret;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000153
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530154 return 0;
155}
156
157/**
158 * release_idr - function to free the unique id.
159 * @idr: struct idr * handle used for creating the id.
160 * @id: int value representing the unique id.
161 */
162static void release_idr(struct idr *idr, int id)
163{
164 mutex_lock(&cooling_cpufreq_lock);
165 idr_remove(idr, id);
166 mutex_unlock(&cooling_cpufreq_lock);
167}
168
169/* Below code defines functions to be used for cpufreq as cooling device */
170
171/**
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530172 * get_level: Find the level for a particular frequency
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530173 * @cpufreq_dev: cpufreq_dev for which the property is required
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530174 * @freq: Frequency
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000175 *
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530176 * Return: level on success, THERMAL_CSTATE_INVALID on error.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530177 */
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530178static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_dev,
179 unsigned int freq)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530180{
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530181 unsigned long level;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000182
Lina Iyer97a13ed2016-07-15 14:53:58 -0600183 for (level = 0; level < cpufreq_dev->max_level; level++) {
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530184 if (freq == cpufreq_dev->freq_table[level])
185 return level;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530186
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530187 if (freq > cpufreq_dev->freq_table[level])
188 break;
Zhang Ruifc35b352013-02-08 13:09:32 +0800189 }
Zhang Ruia1167762014-01-02 11:57:48 +0800190
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530191 return THERMAL_CSTATE_INVALID;
Zhang Ruifc35b352013-02-08 13:09:32 +0800192}
193
Eduardo Valentin44952d32013-04-17 17:12:05 +0000194/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530195 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
Eduardo Valentin44952d32013-04-17 17:12:05 +0000196 * @cpu: cpu for which the level is required
197 * @freq: the frequency of interest
198 *
199 * This function will match the cooling level corresponding to the
200 * requested @freq and return it.
201 *
202 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
203 * otherwise.
204 */
Zhang Rui57df8102013-02-08 14:52:06 +0800205unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
206{
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530207 struct cpufreq_cooling_device *cpufreq_dev;
Zhang Rui57df8102013-02-08 14:52:06 +0800208
Russell King02373d72015-08-12 15:22:16 +0530209 mutex_lock(&cooling_list_lock);
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530210 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
211 if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
Viresh Kumar7cd7b562017-04-25 15:57:08 +0530212 unsigned long level = get_level(cpufreq_dev, freq);
213
Russell King02373d72015-08-12 15:22:16 +0530214 mutex_unlock(&cooling_list_lock);
Viresh Kumar7cd7b562017-04-25 15:57:08 +0530215 return level;
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530216 }
217 }
Russell King02373d72015-08-12 15:22:16 +0530218 mutex_unlock(&cooling_list_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000219
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530220 pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu);
221 return THERMAL_CSTATE_INVALID;
Zhang Rui57df8102013-02-08 14:52:06 +0800222}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000223EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
Zhang Rui57df8102013-02-08 14:52:06 +0800224
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600225static int cpufreq_cooling_pm_notify(struct notifier_block *nb,
226 unsigned long mode, void *_unused)
227{
Manaf Meethalavalappu Pallikunhib7498c22018-05-25 05:44:29 +0530228 struct cpufreq_cooling_device *cpufreq_dev, *next;
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600229 unsigned int cpu;
230
231 switch (mode) {
232 case PM_HIBERNATION_PREPARE:
233 case PM_RESTORE_PREPARE:
234 case PM_SUSPEND_PREPARE:
235 atomic_set(&in_suspend, 1);
236 break;
237 case PM_POST_HIBERNATION:
238 case PM_POST_RESTORE:
239 case PM_POST_SUSPEND:
Manaf Meethalavalappu Pallikunhib7498c22018-05-25 05:44:29 +0530240 list_for_each_entry_safe(cpufreq_dev, next, &cpufreq_dev_list,
241 node) {
Ram Chandrasekar2d02e0a2018-01-18 16:58:07 -0700242 mutex_lock(&core_isolate_lock);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600243 if (cpufreq_dev->cpufreq_state ==
244 cpufreq_dev->max_level) {
245 cpu = cpumask_any(&cpufreq_dev->allowed_cpus);
Ram Chandrasekar2d02e0a2018-01-18 16:58:07 -0700246 /*
247 * Unlock this lock before calling
248 * schedule_isolate. as this could lead to
249 * deadlock with hotplug path.
250 */
251 mutex_unlock(&core_isolate_lock);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600252 if (cpu_online(cpu) &&
253 !cpumask_test_and_set_cpu(cpu,
254 &cpus_isolated_by_thermal)) {
255 if (sched_isolate_cpu(cpu))
256 cpumask_clear_cpu(cpu,
257 &cpus_isolated_by_thermal);
258 }
Ram Chandrasekar2d02e0a2018-01-18 16:58:07 -0700259 continue;
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600260 }
Ram Chandrasekar2d02e0a2018-01-18 16:58:07 -0700261 mutex_unlock(&core_isolate_lock);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600262 }
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600263
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
Manaf Meethalavalappu Pallikunhi3edb5ea2018-11-28 15:44:31 +0530343 if (event != CPUFREQ_INCOMPATIBLE)
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);
Manaf Meethalavalappu Pallikunhi4a4b3552019-10-22 11:07:54 +0530651 struct cpumask policy_online_cpus;
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600652 unsigned int floor_freq;
653
654 if (state > cpufreq_device->max_level)
655 state = cpufreq_device->max_level;
656
657 if (cpufreq_device->cpufreq_floor_state == state)
658 return 0;
659
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600660 cpufreq_device->cpufreq_floor_state = state;
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600661
Ram Chandrasekar23b2db42017-04-19 13:23:31 -0600662 /*
663 * Check if the device has a platform mitigation function that
664 * can handle the CPU freq mitigation, if not, notify cpufreq
665 * framework.
666 */
667 if (cpufreq_device->plat_ops &&
668 cpufreq_device->plat_ops->floor_limit) {
669 /*
670 * Last level is core isolation so use the frequency
671 * of previous state.
672 */
673 if (state == cpufreq_device->max_level)
674 state--;
675 floor_freq = cpufreq_device->freq_table[state];
676 cpufreq_device->floor_freq = floor_freq;
677 cpufreq_device->plat_ops->floor_limit(cpu, floor_freq);
678 } else {
679 floor_freq = cpufreq_device->freq_table[state];
680 cpufreq_device->floor_freq = floor_freq;
Manaf Meethalavalappu Pallikunhi4a4b3552019-10-22 11:07:54 +0530681 if (cpumask_and(&policy_online_cpus, cpu_online_mask,
682 cpufreq_device->policy->related_cpus))
683 cpufreq_update_policy(cpumask_first(
684 &policy_online_cpus));
Ram Chandrasekar23b2db42017-04-19 13:23:31 -0600685 }
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600686
687 return 0;
688}
689
690/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530691 * cpufreq_get_cur_state - callback function to get the current cooling state.
692 * @cdev: thermal cooling device pointer.
693 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000694 *
695 * Callback for the thermal cooling device to return the cpufreq
696 * current cooling state.
697 *
698 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530699 */
700static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
701 unsigned long *state)
702{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100703 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530704
hongbo.zhang160b7d82012-10-30 17:48:59 +0100705 *state = cpufreq_device->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000706
hongbo.zhang160b7d82012-10-30 17:48:59 +0100707 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530708}
709
710/**
711 * cpufreq_set_cur_state - callback function to set the current cooling state.
712 * @cdev: thermal cooling device pointer.
713 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fdb2013-04-17 17:12:14 +0000714 *
715 * Callback for the thermal cooling device to change the cpufreq
716 * current cooling state.
717 *
718 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530719 */
720static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
721 unsigned long state)
722{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100723 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530724 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
Manaf Meethalavalappu Pallikunhi4a4b3552019-10-22 11:07:54 +0530725 struct cpumask policy_online_cpus;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530726 unsigned int clip_freq;
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600727 unsigned long prev_state;
728 struct device *cpu_dev;
729 int ret = 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530730
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530731 /* Request state should be less than max_level */
732 if (WARN_ON(state > cpufreq_device->max_level))
733 return -EINVAL;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530734
Viresh Kumar5194fe42014-12-04 09:42:00 +0530735 /* Check if the old cooling action is same as new cooling action */
736 if (cpufreq_device->cpufreq_state == state)
737 return 0;
738
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600739 mutex_lock(&core_isolate_lock);
740 prev_state = cpufreq_device->cpufreq_state;
Ram Chandrasekarbd080e42017-05-25 15:49:20 -0600741 cpufreq_device->cpufreq_state = state;
Ram Chandrasekar2d02e0a2018-01-18 16:58:07 -0700742 mutex_unlock(&core_isolate_lock);
Lina Iyer97a13ed2016-07-15 14:53:58 -0600743 /* If state is the last, isolate the CPU */
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600744 if (state == cpufreq_device->max_level) {
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600745 if (cpu_online(cpu) &&
746 (!cpumask_test_and_set_cpu(cpu,
747 &cpus_isolated_by_thermal))) {
748 if (sched_isolate_cpu(cpu))
749 cpumask_clear_cpu(cpu,
750 &cpus_isolated_by_thermal);
751 }
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600752 return ret;
753 } else if ((prev_state == cpufreq_device->max_level)
754 && (state < cpufreq_device->max_level)) {
755 if (cpumask_test_and_clear_cpu(cpu, &cpus_pending_online)) {
756 cpu_dev = get_cpu_device(cpu);
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600757 ret = device_online(cpu_dev);
758 if (ret)
759 pr_err("CPU:%d online error:%d\n", cpu, ret);
760 goto update_frequency;
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600761 } else if (cpumask_test_and_clear_cpu(cpu,
762 &cpus_isolated_by_thermal)) {
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600763 sched_unisolate_cpu(cpu);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -0600764 }
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600765 }
Ram Chandrasekar30ca8042017-06-13 16:05:47 -0600766update_frequency:
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530767 clip_freq = cpufreq_device->freq_table[state];
Viresh Kumar59f0d212015-07-30 12:40:33 +0530768 cpufreq_device->clipped_freq = clip_freq;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530769
Lina Iyer986fde12016-02-23 13:08:31 -0700770 /* Check if the device has a platform mitigation function that
771 * can handle the CPU freq mitigation, if not, notify cpufreq
772 * framework.
773 */
774 if (cpufreq_device->plat_ops) {
775 if (cpufreq_device->plat_ops->ceil_limit)
776 cpufreq_device->plat_ops->ceil_limit(cpu,
777 clip_freq);
778 } else {
Manaf Meethalavalappu Pallikunhi4a4b3552019-10-22 11:07:54 +0530779 if (cpumask_and(&policy_online_cpus, cpu_online_mask,
780 cpufreq_device->policy->related_cpus))
781 cpufreq_update_policy(cpumask_first(
782 &policy_online_cpus));
Lina Iyer986fde12016-02-23 13:08:31 -0700783 }
Viresh Kumar5194fe42014-12-04 09:42:00 +0530784
785 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530786}
787
Javi Merinoc36cf072015-02-26 19:00:29 +0000788/**
789 * cpufreq_get_requested_power() - get the current power
790 * @cdev: &thermal_cooling_device pointer
791 * @tz: a valid thermal zone device pointer
792 * @power: pointer in which to store the resulting power
793 *
794 * Calculate the current power consumption of the cpus in milliwatts
795 * and store it in @power. This function should actually calculate
796 * the requested power, but it's hard to get the frequency that
797 * cpufreq would have assigned if there were no thermal limits.
798 * Instead, we calculate the current power on the assumption that the
799 * immediate future will look like the immediate past.
800 *
801 * We use the current frequency and the average load since this
802 * function was last called. In reality, there could have been
803 * multiple opps since this function was last called and that affects
804 * the load calculation. While it's not perfectly accurate, this
805 * simplification is good enough and works. REVISIT this, as more
806 * complex code may be needed if experiments show that it's not
807 * accurate enough.
808 *
809 * Return: 0 on success, -E* if getting the static power failed.
810 */
811static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
812 struct thermal_zone_device *tz,
813 u32 *power)
814{
815 unsigned long freq;
Javi Merino6828a472015-03-02 17:17:20 +0000816 int i = 0, cpu, ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000817 u32 static_power, dynamic_power, total_load = 0;
818 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Javi Merino6828a472015-03-02 17:17:20 +0000819 u32 *load_cpu = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000820
Kapileshwar Singhdd658e02015-03-16 12:00:51 +0000821 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
822
823 /*
824 * All the CPUs are offline, thus the requested power by
825 * the cdev is 0
826 */
827 if (cpu >= nr_cpu_ids) {
828 *power = 0;
829 return 0;
830 }
831
832 freq = cpufreq_quick_get(cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000833
Javi Merino6828a472015-03-02 17:17:20 +0000834 if (trace_thermal_power_cpu_get_power_enabled()) {
835 u32 ncpus = cpumask_weight(&cpufreq_device->allowed_cpus);
836
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530837 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
Javi Merino6828a472015-03-02 17:17:20 +0000838 }
839
Javi Merinoc36cf072015-02-26 19:00:29 +0000840 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
841 u32 load;
842
843 if (cpu_online(cpu))
Javi Merinoa53b8392016-02-11 12:00:51 +0000844 load = get_load(cpufreq_device, cpu, i);
Javi Merinoc36cf072015-02-26 19:00:29 +0000845 else
846 load = 0;
847
848 total_load += load;
Matthias Kaehlcke56fc3de2019-05-02 11:32:38 -0700849 if (load_cpu)
Javi Merino6828a472015-03-02 17:17:20 +0000850 load_cpu[i] = load;
851
852 i++;
Javi Merinoc36cf072015-02-26 19:00:29 +0000853 }
854
855 cpufreq_device->last_load = total_load;
856
857 dynamic_power = get_dynamic_power(cpufreq_device, freq);
858 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
Javi Merino6828a472015-03-02 17:17:20 +0000859 if (ret) {
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530860 kfree(load_cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000861 return ret;
Javi Merino6828a472015-03-02 17:17:20 +0000862 }
863
864 if (load_cpu) {
865 trace_thermal_power_cpu_get_power(
866 &cpufreq_device->allowed_cpus,
867 freq, load_cpu, i, dynamic_power, static_power);
868
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530869 kfree(load_cpu);
Javi Merino6828a472015-03-02 17:17:20 +0000870 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000871
872 *power = static_power + dynamic_power;
873 return 0;
874}
875
876/**
877 * cpufreq_state2power() - convert a cpu cdev state to power consumed
878 * @cdev: &thermal_cooling_device pointer
879 * @tz: a valid thermal zone device pointer
880 * @state: cooling device state to be converted
881 * @power: pointer in which to store the resulting power
882 *
883 * Convert cooling device state @state into power consumption in
884 * milliwatts assuming 100% load. Store the calculated power in
885 * @power.
886 *
887 * Return: 0 on success, -EINVAL if the cooling device state could not
888 * be converted into a frequency or other -E* if there was an error
889 * when calculating the static power.
890 */
891static int cpufreq_state2power(struct thermal_cooling_device *cdev,
892 struct thermal_zone_device *tz,
893 unsigned long state, u32 *power)
894{
895 unsigned int freq, num_cpus;
896 cpumask_t cpumask;
897 u32 static_power, dynamic_power;
898 int ret;
899 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
900
901 cpumask_and(&cpumask, &cpufreq_device->allowed_cpus, cpu_online_mask);
902 num_cpus = cpumask_weight(&cpumask);
903
904 /* None of our cpus are online, so no power */
905 if (num_cpus == 0) {
906 *power = 0;
907 return 0;
908 }
909
910 freq = cpufreq_device->freq_table[state];
911 if (!freq)
912 return -EINVAL;
913
914 dynamic_power = cpu_freq_to_power(cpufreq_device, freq) * num_cpus;
915 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
916 if (ret)
917 return ret;
918
919 *power = static_power + dynamic_power;
920 return 0;
921}
922
923/**
924 * cpufreq_power2state() - convert power to a cooling device state
925 * @cdev: &thermal_cooling_device pointer
926 * @tz: a valid thermal zone device pointer
927 * @power: power in milliwatts to be converted
928 * @state: pointer in which to store the resulting state
929 *
930 * Calculate a cooling device state for the cpus described by @cdev
931 * that would allow them to consume at most @power mW and store it in
932 * @state. Note that this calculation depends on external factors
933 * such as the cpu load or the current static power. Calling this
934 * function with the same power as input can yield different cooling
935 * device states depending on those external factors.
936 *
937 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
938 * the calculated frequency could not be converted to a valid state.
939 * The latter should not happen unless the frequencies available to
940 * cpufreq have changed since the initialization of the cpu cooling
941 * device.
942 */
943static int cpufreq_power2state(struct thermal_cooling_device *cdev,
944 struct thermal_zone_device *tz, u32 power,
945 unsigned long *state)
946{
947 unsigned int cpu, cur_freq, target_freq;
948 int ret;
949 s32 dyn_power;
950 u32 last_load, normalised_power, static_power;
951 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
952
953 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
954
955 /* None of our cpus are online */
956 if (cpu >= nr_cpu_ids)
957 return -ENODEV;
958
959 cur_freq = cpufreq_quick_get(cpu);
960 ret = get_static_power(cpufreq_device, tz, cur_freq, &static_power);
961 if (ret)
962 return ret;
963
964 dyn_power = power - static_power;
965 dyn_power = dyn_power > 0 ? dyn_power : 0;
966 last_load = cpufreq_device->last_load ?: 1;
967 normalised_power = (dyn_power * 100) / last_load;
968 target_freq = cpu_power_to_freq(cpufreq_device, normalised_power);
969
970 *state = cpufreq_cooling_get_level(cpu, target_freq);
971 if (*state == THERMAL_CSTATE_INVALID) {
972 dev_warn_ratelimited(&cdev->device,
973 "Failed to convert %dKHz for cpu %d into a cdev state\n",
974 target_freq, cpu);
975 return -EINVAL;
976 }
977
Javi Merino6828a472015-03-02 17:17:20 +0000978 trace_thermal_power_cpu_limit(&cpufreq_device->allowed_cpus,
979 target_freq, *state, power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000980 return 0;
981}
982
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530983/* Bind cpufreq callbacks to thermal cooling device ops */
Brendan Jackmana305a432016-08-17 16:14:59 +0100984
Javi Merinoc36cf072015-02-26 19:00:29 +0000985static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530986 .get_max_state = cpufreq_get_max_state,
987 .get_cur_state = cpufreq_get_cur_state,
988 .set_cur_state = cpufreq_set_cur_state,
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600989 .set_min_state = cpufreq_set_min_state,
990 .get_min_state = cpufreq_get_min_state,
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530991};
992
Brendan Jackmana305a432016-08-17 16:14:59 +0100993static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
994 .get_max_state = cpufreq_get_max_state,
995 .get_cur_state = cpufreq_get_cur_state,
996 .set_cur_state = cpufreq_set_cur_state,
997 .get_requested_power = cpufreq_get_requested_power,
998 .state2power = cpufreq_state2power,
999 .power2state = cpufreq_power2state,
1000};
1001
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301002/* Notifier for cpufreq policy change */
1003static struct notifier_block thermal_cpufreq_notifier_block = {
1004 .notifier_call = cpufreq_thermal_notifier,
1005};
1006
Viresh Kumarf6859012014-12-04 09:42:06 +05301007static unsigned int find_next_max(struct cpufreq_frequency_table *table,
1008 unsigned int prev_max)
1009{
1010 struct cpufreq_frequency_table *pos;
1011 unsigned int max = 0;
1012
1013 cpufreq_for_each_valid_entry(pos, table) {
1014 if (pos->frequency > max && pos->frequency < prev_max)
1015 max = pos->frequency;
1016 }
1017
1018 return max;
1019}
1020
Ram Chandrasekar30ca8042017-06-13 16:05:47 -06001021static void register_cdev(struct work_struct *work)
1022{
1023 int ret = 0;
1024
1025 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
1026 "cpu_cooling/no-sched", cpufreq_hp_online,
1027 cpufreq_hp_offline);
1028 if (ret < 0)
1029 pr_err("Error registering for hotpug callback:%d\n", ret);
1030}
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301031/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001032 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
1033 * @np: a valid struct device_node to the cooling device device tree node
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301034 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
Viresh Kumar405fb822014-12-04 09:41:55 +05301035 * Normally this should be same as cpufreq policy->related_cpus.
Javi Merinoc36cf072015-02-26 19:00:29 +00001036 * @capacitance: dynamic power coefficient for these cpus
1037 * @plat_static_func: function to calculate the static power consumed by these
1038 * cpus (optional)
Lina Iyer986fde12016-02-23 13:08:31 -07001039 * @plat_mitig_func: function that does the mitigation by changing the
1040 * frequencies (Optional). By default, cpufreq framweork will
1041 * be notified of the new limits.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +00001042 *
1043 * This interface function registers the cpufreq cooling device with the name
1044 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001045 * cooling devices. It also gives the opportunity to link the cooling device
1046 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +00001047 *
1048 * Return: a valid struct thermal_cooling_device pointer on success,
1049 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301050 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001051static struct thermal_cooling_device *
1052__cpufreq_cooling_register(struct device_node *np,
Javi Merinoc36cf072015-02-26 19:00:29 +00001053 const struct cpumask *clip_cpus, u32 capacitance,
Lina Iyer986fde12016-02-23 13:08:31 -07001054 get_static_t plat_static_func,
1055 struct cpu_cooling_ops *plat_ops)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301056{
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301057 struct cpufreq_policy *policy;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301058 struct thermal_cooling_device *cool_dev;
Viresh Kumar5d3bdb82014-12-04 09:41:52 +05301059 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301060 char dev_name[THERMAL_NAME_LENGTH];
Viresh Kumardcc6c7f2014-12-04 09:42:02 +05301061 struct cpufreq_frequency_table *pos, *table;
Javi Merinoc36cf072015-02-26 19:00:29 +00001062 unsigned int freq, i, num_cpus;
Viresh Kumar405fb822014-12-04 09:41:55 +05301063 int ret;
Brendan Jackmana305a432016-08-17 16:14:59 +01001064 struct thermal_cooling_device_ops *cooling_ops;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301065
Ram Chandrasekar5682d552017-05-03 16:01:53 -06001066 policy = cpufreq_cpu_get(cpumask_first(clip_cpus));
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301067 if (!policy) {
1068 pr_debug("%s: CPUFreq policy not found\n", __func__);
Eduardo Valentin0f1be512014-12-04 09:41:43 +05301069 return ERR_PTR(-EPROBE_DEFER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301070 }
Eduardo Valentin0f1be512014-12-04 09:41:43 +05301071
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301072 table = policy->freq_table;
1073 if (!table) {
1074 pr_debug("%s: CPUFreq table not found\n", __func__);
1075 cool_dev = ERR_PTR(-ENODEV);
1076 goto put_policy;
1077 }
1078
Viresh Kumar98d522f2014-12-04 09:41:50 +05301079 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301080 if (!cpufreq_dev) {
1081 cool_dev = ERR_PTR(-ENOMEM);
1082 goto put_policy;
1083 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301084
Manaf Meethalavalappu Pallikunhi4a4b3552019-10-22 11:07:54 +05301085 cpufreq_dev->policy = policy;
Javi Merinoc36cf072015-02-26 19:00:29 +00001086 num_cpus = cpumask_weight(clip_cpus);
1087 cpufreq_dev->time_in_idle = kcalloc(num_cpus,
1088 sizeof(*cpufreq_dev->time_in_idle),
1089 GFP_KERNEL);
1090 if (!cpufreq_dev->time_in_idle) {
1091 cool_dev = ERR_PTR(-ENOMEM);
1092 goto free_cdev;
1093 }
1094
1095 cpufreq_dev->time_in_idle_timestamp =
1096 kcalloc(num_cpus, sizeof(*cpufreq_dev->time_in_idle_timestamp),
1097 GFP_KERNEL);
1098 if (!cpufreq_dev->time_in_idle_timestamp) {
1099 cool_dev = ERR_PTR(-ENOMEM);
1100 goto free_time_in_idle;
1101 }
1102
Viresh Kumardcc6c7f2014-12-04 09:42:02 +05301103 /* Find max levels */
1104 cpufreq_for_each_valid_entry(pos, table)
1105 cpufreq_dev->max_level++;
1106
Lina Iyer97a13ed2016-07-15 14:53:58 -06001107 /* Last level will indicate the core will be isolated. */
1108 cpufreq_dev->max_level++;
1109 cpufreq_dev->freq_table = kzalloc(sizeof(*cpufreq_dev->freq_table) *
Viresh Kumarf6859012014-12-04 09:42:06 +05301110 cpufreq_dev->max_level, GFP_KERNEL);
1111 if (!cpufreq_dev->freq_table) {
Viresh Kumarf6859012014-12-04 09:42:06 +05301112 cool_dev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +00001113 goto free_time_in_idle_timestamp;
Viresh Kumarf6859012014-12-04 09:42:06 +05301114 }
1115
Viresh Kumardcc6c7f2014-12-04 09:42:02 +05301116 /* max_level is an index, not a counter */
1117 cpufreq_dev->max_level--;
1118
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301119 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
1120
Javi Merinoc36cf072015-02-26 19:00:29 +00001121 if (capacitance) {
Javi Merinoc36cf072015-02-26 19:00:29 +00001122 cpufreq_dev->plat_get_static_power = plat_static_func;
1123
1124 ret = build_dyn_power_table(cpufreq_dev, capacitance);
1125 if (ret) {
1126 cool_dev = ERR_PTR(ret);
1127 goto free_table;
1128 }
Brendan Jackmana305a432016-08-17 16:14:59 +01001129
1130 cooling_ops = &cpufreq_power_cooling_ops;
1131 } else {
1132 cooling_ops = &cpufreq_cooling_ops;
Javi Merinoc36cf072015-02-26 19:00:29 +00001133 }
1134
Lina Iyer986fde12016-02-23 13:08:31 -07001135 cpufreq_dev->plat_ops = plat_ops;
1136
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301137 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
1138 if (ret) {
Viresh Kumar730abe02014-12-04 09:41:58 +05301139 cool_dev = ERR_PTR(ret);
Javi Merinoeba4f882015-08-17 19:21:43 +01001140 goto free_power_table;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301141 }
1142
Viresh Kumarf6859012014-12-04 09:42:06 +05301143 /* Fill freq-table in descending order of frequencies */
Lina Iyer97a13ed2016-07-15 14:53:58 -06001144 for (i = 0, freq = -1; i < cpufreq_dev->max_level; i++) {
Viresh Kumarf6859012014-12-04 09:42:06 +05301145 freq = find_next_max(table, freq);
1146 cpufreq_dev->freq_table[i] = freq;
1147
1148 /* Warn for duplicate entries */
1149 if (!freq)
1150 pr_warn("%s: table has duplicate entries\n", __func__);
1151 else
1152 pr_debug("%s: freq:%u KHz\n", __func__, freq);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301153 }
Viresh Kumarf6859012014-12-04 09:42:06 +05301154
Lukasz Lubaf840ab12016-05-31 11:32:02 +01001155 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
1156 cpufreq_dev->id);
1157
1158 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
Brendan Jackmana305a432016-08-17 16:14:59 +01001159 cooling_ops);
Lukasz Lubaf840ab12016-05-31 11:32:02 +01001160 if (IS_ERR(cool_dev))
1161 goto remove_idr;
1162
Viresh Kumar59f0d212015-07-30 12:40:33 +05301163 cpufreq_dev->clipped_freq = cpufreq_dev->freq_table[0];
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -06001164 cpufreq_dev->floor_freq =
1165 cpufreq_dev->freq_table[cpufreq_dev->max_level];
1166 cpufreq_dev->cpufreq_floor_state = cpufreq_dev->max_level;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301167 cpufreq_dev->cool_dev = cool_dev;
Viresh Kumar92e615e2014-12-04 09:41:51 +05301168
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301169 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301170
Russell King02373d72015-08-12 15:22:16 +05301171 mutex_lock(&cooling_list_lock);
1172 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
1173 mutex_unlock(&cooling_list_lock);
1174
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301175 /* Register the notifier for first cpufreq cooling device */
Ram Chandrasekarcbd1a402017-10-27 12:18:49 -06001176 if (!cpufreq_dev_count++ && !cpufreq_dev->plat_ops)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301177 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +00001178 CPUFREQ_POLICY_NOTIFIER);
Ram Chandrasekar30ca8042017-06-13 16:05:47 -06001179 if (!cpuhp_registered) {
1180 cpuhp_registered = 1;
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -06001181 register_pm_notifier(&cpufreq_cooling_pm_nb);
Ram Chandrasekar30ca8042017-06-13 16:05:47 -06001182 cpumask_clear(&cpus_pending_online);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -06001183 cpumask_clear(&cpus_isolated_by_thermal);
Ram Chandrasekar30ca8042017-06-13 16:05:47 -06001184 INIT_WORK(&cpuhp_register_work, register_cdev);
1185 queue_work(system_wq, &cpuhp_register_work);
1186 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301187 mutex_unlock(&cooling_cpufreq_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +00001188
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301189 goto put_policy;
Viresh Kumar730abe02014-12-04 09:41:58 +05301190
1191remove_idr:
1192 release_idr(&cpufreq_idr, cpufreq_dev->id);
Javi Merinoeba4f882015-08-17 19:21:43 +01001193free_power_table:
1194 kfree(cpufreq_dev->dyn_power_table);
Viresh Kumarf6859012014-12-04 09:42:06 +05301195free_table:
1196 kfree(cpufreq_dev->freq_table);
Javi Merinoc36cf072015-02-26 19:00:29 +00001197free_time_in_idle_timestamp:
1198 kfree(cpufreq_dev->time_in_idle_timestamp);
1199free_time_in_idle:
1200 kfree(cpufreq_dev->time_in_idle);
Viresh Kumar730abe02014-12-04 09:41:58 +05301201free_cdev:
1202 kfree(cpufreq_dev);
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301203put_policy:
1204 cpufreq_cpu_put(policy);
Viresh Kumar730abe02014-12-04 09:41:58 +05301205
1206 return cool_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301207}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001208
1209/**
1210 * cpufreq_cooling_register - function to create cpufreq cooling device.
1211 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
1212 *
1213 * This interface function registers the cpufreq cooling device with the name
1214 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
1215 * cooling devices.
1216 *
1217 * Return: a valid struct thermal_cooling_device pointer on success,
1218 * on failure, it returns a corresponding ERR_PTR().
1219 */
1220struct thermal_cooling_device *
1221cpufreq_cooling_register(const struct cpumask *clip_cpus)
1222{
Lina Iyer986fde12016-02-23 13:08:31 -07001223 return __cpufreq_cooling_register(NULL, clip_cpus, 0, NULL, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001224}
Eduardo Valentin243dbd92013-04-17 17:11:57 +00001225EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301226
1227/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001228 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
1229 * @np: a valid struct device_node to the cooling device device tree node
1230 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
1231 *
1232 * This interface function registers the cpufreq cooling device with the name
1233 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
1234 * cooling devices. Using this API, the cpufreq cooling device will be
1235 * linked to the device tree node provided.
1236 *
1237 * Return: a valid struct thermal_cooling_device pointer on success,
1238 * on failure, it returns a corresponding ERR_PTR().
1239 */
1240struct thermal_cooling_device *
1241of_cpufreq_cooling_register(struct device_node *np,
1242 const struct cpumask *clip_cpus)
1243{
1244 if (!np)
1245 return ERR_PTR(-EINVAL);
1246
Lina Iyer986fde12016-02-23 13:08:31 -07001247 return __cpufreq_cooling_register(np, clip_cpus, 0, NULL, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001248}
1249EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
1250
1251/**
Javi Merinoc36cf072015-02-26 19:00:29 +00001252 * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
1253 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
1254 * @capacitance: dynamic power coefficient for these cpus
1255 * @plat_static_func: function to calculate the static power consumed by these
1256 * cpus (optional)
1257 *
1258 * This interface function registers the cpufreq cooling device with
1259 * the name "thermal-cpufreq-%x". This api can support multiple
1260 * instances of cpufreq cooling devices. Using this function, the
1261 * cooling device will implement the power extensions by using a
1262 * simple cpu power model. The cpus must have registered their OPPs
1263 * using the OPP library.
1264 *
1265 * An optional @plat_static_func may be provided to calculate the
1266 * static power consumed by these cpus. If the platform's static
1267 * power consumption is unknown or negligible, make it NULL.
1268 *
1269 * Return: a valid struct thermal_cooling_device pointer on success,
1270 * on failure, it returns a corresponding ERR_PTR().
1271 */
1272struct thermal_cooling_device *
1273cpufreq_power_cooling_register(const struct cpumask *clip_cpus, u32 capacitance,
1274 get_static_t plat_static_func)
1275{
1276 return __cpufreq_cooling_register(NULL, clip_cpus, capacitance,
Lina Iyer986fde12016-02-23 13:08:31 -07001277 plat_static_func, NULL);
Javi Merinoc36cf072015-02-26 19:00:29 +00001278}
1279EXPORT_SYMBOL(cpufreq_power_cooling_register);
1280
1281/**
Lina Iyer986fde12016-02-23 13:08:31 -07001282 * cpufreq_platform_cooling_register() - create cpufreq cooling device with
1283 * additional platform specific mitigation function.
1284 *
1285 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
1286 * @plat_ops: the platform mitigation functions that will be called insted of
1287 * cpufreq, if provided.
1288 *
1289 * Return: a valid struct thermal_cooling_device pointer on success,
1290 * on failure, it returns a corresponding ERR_PTR().
1291 */
1292struct thermal_cooling_device *
1293cpufreq_platform_cooling_register(const struct cpumask *clip_cpus,
1294 struct cpu_cooling_ops *plat_ops)
1295{
1296 struct device_node *cpu_node;
1297
1298 cpu_node = of_cpu_device_node_get(cpumask_first(clip_cpus));
1299 return __cpufreq_cooling_register(cpu_node, clip_cpus, 0, NULL,
1300 plat_ops);
1301}
1302EXPORT_SYMBOL(cpufreq_platform_cooling_register);
1303
1304/**
Javi Merinoc36cf072015-02-26 19:00:29 +00001305 * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
1306 * @np: a valid struct device_node to the cooling device device tree node
1307 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
1308 * @capacitance: dynamic power coefficient for these cpus
1309 * @plat_static_func: function to calculate the static power consumed by these
1310 * cpus (optional)
1311 *
1312 * This interface function registers the cpufreq cooling device with
1313 * the name "thermal-cpufreq-%x". This api can support multiple
1314 * instances of cpufreq cooling devices. Using this API, the cpufreq
1315 * cooling device will be linked to the device tree node provided.
1316 * Using this function, the cooling device will implement the power
1317 * extensions by using a simple cpu power model. The cpus must have
1318 * registered their OPPs using the OPP library.
1319 *
1320 * An optional @plat_static_func may be provided to calculate the
1321 * static power consumed by these cpus. If the platform's static
1322 * power consumption is unknown or negligible, make it NULL.
1323 *
1324 * Return: a valid struct thermal_cooling_device pointer on success,
1325 * on failure, it returns a corresponding ERR_PTR().
1326 */
1327struct thermal_cooling_device *
1328of_cpufreq_power_cooling_register(struct device_node *np,
1329 const struct cpumask *clip_cpus,
1330 u32 capacitance,
1331 get_static_t plat_static_func)
1332{
1333 if (!np)
1334 return ERR_PTR(-EINVAL);
1335
1336 return __cpufreq_cooling_register(np, clip_cpus, capacitance,
Lina Iyer986fde12016-02-23 13:08:31 -07001337 plat_static_func, NULL);
Javi Merinoc36cf072015-02-26 19:00:29 +00001338}
1339EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
1340
1341/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301342 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
1343 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +00001344 *
1345 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301346 */
1347void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
1348{
Eduardo Valentin50e66c72013-08-15 10:54:46 -04001349 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301350
Eduardo Valentin50e66c72013-08-15 10:54:46 -04001351 if (!cdev)
1352 return;
1353
1354 cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301355
1356 /* Unregister the notifier for the last cpufreq cooling device */
Russell King02373d72015-08-12 15:22:16 +05301357 mutex_lock(&cooling_cpufreq_lock);
Ram Chandrasekar7f4821c2017-10-30 18:28:48 -06001358 if (!--cpufreq_dev_count) {
1359 unregister_pm_notifier(&cpufreq_cooling_pm_nb);
1360 if (!cpufreq_dev->plat_ops)
1361 cpufreq_unregister_notifier(
1362 &thermal_cpufreq_notifier_block,
1363 CPUFREQ_POLICY_NOTIFIER);
1364 }
Russell King02373d72015-08-12 15:22:16 +05301365
1366 mutex_lock(&cooling_list_lock);
1367 list_del(&cpufreq_dev->node);
1368 mutex_unlock(&cooling_list_lock);
1369
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301370 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +01001371
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301372 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
1373 release_idr(&cpufreq_idr, cpufreq_dev->id);
Javi Merinoeba4f882015-08-17 19:21:43 +01001374 kfree(cpufreq_dev->dyn_power_table);
Javi Merinoc36cf072015-02-26 19:00:29 +00001375 kfree(cpufreq_dev->time_in_idle_timestamp);
1376 kfree(cpufreq_dev->time_in_idle);
Viresh Kumarf6859012014-12-04 09:42:06 +05301377 kfree(cpufreq_dev->freq_table);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301378 kfree(cpufreq_dev);
1379}
Eduardo Valentin243dbd92013-04-17 17:11:57 +00001380EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);