blob: 69d0f430b2d190756de94d4b6b6334d1af2dfa50 [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>
Matthew Wilcoxae606082016-12-21 09:47:05 -080029#include <linux/idr.h>
Javi Merinoc36cf072015-02-26 19:00:29 +000030#include <linux/pm_opp.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053031#include <linux/slab.h>
32#include <linux/cpu.h>
33#include <linux/cpu_cooling.h>
34
Javi Merino6828a472015-03-02 17:17:20 +000035#include <trace/events/thermal.h>
36
Viresh Kumar07d888d2014-12-04 09:41:49 +053037/*
38 * Cooling state <-> CPUFreq frequency
39 *
40 * Cooling states are translated to frequencies throughout this driver and this
41 * is the relation between them.
42 *
43 * Highest cooling state corresponds to lowest possible frequency.
44 *
45 * i.e.
46 * level 0 --> 1st Max Freq
47 * level 1 --> 2nd Max Freq
48 * ...
49 */
50
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053051/**
Javi Merinoc36cf072015-02-26 19:00:29 +000052 * struct power_table - frequency to power conversion
53 * @frequency: frequency in KHz
54 * @power: power in mW
55 *
56 * This structure is built when the cooling device registers and helps
57 * in translating frequency to power and viceversa.
58 */
59struct power_table {
60 u32 frequency;
61 u32 power;
62};
63
64/**
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000065 * struct cpufreq_cooling_device - data for cooling device with cpufreq
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053066 * @id: unique integer value corresponding to each cpufreq_cooling_device
67 * registered.
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000068 * @cool_dev: thermal_cooling_device pointer to keep track of the
69 * registered cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053070 * @cpufreq_state: integer value representing the current state of cpufreq
71 * cooling devices.
Viresh Kumar59f0d212015-07-30 12:40:33 +053072 * @clipped_freq: integer value representing the absolute value of the clipped
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053073 * frequency.
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053074 * @max_level: maximum cooling level. One less than total number of valid
75 * cpufreq frequencies.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053076 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
Javi Merinofc4de352014-12-15 16:55:52 +000077 * @node: list_head to link all cpufreq_cooling_device together.
Hugh Kang0744f132016-09-07 09:35:39 +090078 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
Javi Merinoc36cf072015-02-26 19:00:29 +000079 * @time_in_idle: previous reading of the absolute time that this cpu was idle
80 * @time_in_idle_timestamp: wall time of the last invocation of
81 * get_cpu_idle_time_us()
82 * @dyn_power_table: array of struct power_table for frequency to power
83 * conversion, sorted in ascending order.
84 * @dyn_power_table_entries: number of entries in the @dyn_power_table array
85 * @cpu_dev: the first cpu_device from @allowed_cpus that has OPPs registered
86 * @plat_get_static_power: callback to calculate the static power
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053087 *
Viresh Kumarbeca6052014-12-04 09:41:48 +053088 * This structure is required for keeping information of each registered
89 * cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053090 */
91struct cpufreq_cooling_device {
92 int id;
93 struct thermal_cooling_device *cool_dev;
94 unsigned int cpufreq_state;
Viresh Kumar59f0d212015-07-30 12:40:33 +053095 unsigned int clipped_freq;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053096 unsigned int max_level;
Viresh Kumarf6859012014-12-04 09:42:06 +053097 unsigned int *freq_table; /* In descending order */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053098 struct cpumask allowed_cpus;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053099 struct list_head node;
Javi Merinoc36cf072015-02-26 19:00:29 +0000100 u32 last_load;
101 u64 *time_in_idle;
102 u64 *time_in_idle_timestamp;
103 struct power_table *dyn_power_table;
104 int dyn_power_table_entries;
105 struct device *cpu_dev;
106 get_static_t plat_get_static_power;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530107};
Matthew Wilcoxae606082016-12-21 09:47:05 -0800108static DEFINE_IDA(cpufreq_ida);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530109
Russell King02373d72015-08-12 15:22:16 +0530110static DEFINE_MUTEX(cooling_list_lock);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530111static LIST_HEAD(cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530112
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530113/* Below code defines functions to be used for cpufreq as cooling device */
114
115/**
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530116 * get_level: Find the level for a particular frequency
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530117 * @cpufreq_dev: cpufreq_dev for which the property is required
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530118 * @freq: Frequency
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000119 *
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530120 * Return: level on success, THERMAL_CSTATE_INVALID on error.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530121 */
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530122static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_dev,
123 unsigned int freq)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530124{
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530125 unsigned long level;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000126
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530127 for (level = 0; level <= cpufreq_dev->max_level; level++) {
128 if (freq == cpufreq_dev->freq_table[level])
129 return level;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530130
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530131 if (freq > cpufreq_dev->freq_table[level])
132 break;
Zhang Ruifc35b352013-02-08 13:09:32 +0800133 }
Zhang Ruia1167762014-01-02 11:57:48 +0800134
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530135 return THERMAL_CSTATE_INVALID;
Zhang Ruifc35b352013-02-08 13:09:32 +0800136}
137
Eduardo Valentin44952d32013-04-17 17:12:05 +0000138/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530139 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
Eduardo Valentin44952d32013-04-17 17:12:05 +0000140 * @cpu: cpu for which the level is required
141 * @freq: the frequency of interest
142 *
143 * This function will match the cooling level corresponding to the
144 * requested @freq and return it.
145 *
146 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
147 * otherwise.
148 */
Zhang Rui57df8102013-02-08 14:52:06 +0800149unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
150{
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530151 struct cpufreq_cooling_device *cpufreq_dev;
Zhang Rui57df8102013-02-08 14:52:06 +0800152
Russell King02373d72015-08-12 15:22:16 +0530153 mutex_lock(&cooling_list_lock);
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530154 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
155 if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
Russell King02373d72015-08-12 15:22:16 +0530156 mutex_unlock(&cooling_list_lock);
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530157 return get_level(cpufreq_dev, freq);
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530158 }
159 }
Russell King02373d72015-08-12 15:22:16 +0530160 mutex_unlock(&cooling_list_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000161
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530162 pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu);
163 return THERMAL_CSTATE_INVALID;
Zhang Rui57df8102013-02-08 14:52:06 +0800164}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000165EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
Zhang Rui57df8102013-02-08 14:52:06 +0800166
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530167/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530168 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
169 * @nb: struct notifier_block * with callback info.
170 * @event: value showing cpufreq event for which this function invoked.
171 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000172 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100173 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000174 * Every time there is a change in policy, we will intercept and
175 * update the cpufreq policy with thermal constraints.
176 *
177 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530178 */
179static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000180 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530181{
182 struct cpufreq_policy *policy = data;
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530183 unsigned long clipped_freq;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530184 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530185
Viresh Kumara24af232015-07-30 12:40:32 +0530186 if (event != CPUFREQ_ADJUST)
Javi Merinoc36cf072015-02-26 19:00:29 +0000187 return NOTIFY_DONE;
Viresh Kumara24af232015-07-30 12:40:32 +0530188
189 mutex_lock(&cooling_list_lock);
190 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
191 if (!cpumask_test_cpu(policy->cpu, &cpufreq_dev->allowed_cpus))
192 continue;
193
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530194 /*
195 * policy->max is the maximum allowed frequency defined by user
196 * and clipped_freq is the maximum that thermal constraints
197 * allow.
198 *
199 * If clipped_freq is lower than policy->max, then we need to
200 * readjust policy->max.
201 *
202 * But, if clipped_freq is greater than policy->max, we don't
203 * need to do anything.
204 */
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530205 clipped_freq = cpufreq_dev->clipped_freq;
Viresh Kumara24af232015-07-30 12:40:32 +0530206
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530207 if (policy->max > clipped_freq)
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530208 cpufreq_verify_within_limits(policy, 0, clipped_freq);
Viresh Kumara24af232015-07-30 12:40:32 +0530209 break;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530210 }
Viresh Kumara24af232015-07-30 12:40:32 +0530211 mutex_unlock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530212
Javi Merinoc36cf072015-02-26 19:00:29 +0000213 return NOTIFY_OK;
214}
215
216/**
217 * build_dyn_power_table() - create a dynamic power to frequency table
218 * @cpufreq_device: the cpufreq cooling device in which to store the table
219 * @capacitance: dynamic power coefficient for these cpus
220 *
221 * Build a dynamic power to frequency table for this cpu and store it
222 * in @cpufreq_device. This table will be used in cpu_power_to_freq() and
223 * cpu_freq_to_power() to convert between power and frequency
224 * efficiently. Power is stored in mW, frequency in KHz. The
225 * resulting table is in ascending order.
226 *
Javi Merino459ac372015-08-17 19:21:42 +0100227 * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
228 * -ENOMEM if we run out of memory or -EAGAIN if an OPP was
229 * added/enabled while the function was executing.
Javi Merinoc36cf072015-02-26 19:00:29 +0000230 */
231static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_device,
232 u32 capacitance)
233{
234 struct power_table *power_table;
235 struct dev_pm_opp *opp;
236 struct device *dev = NULL;
Javi Merinoeba4f882015-08-17 19:21:43 +0100237 int num_opps = 0, cpu, i, ret = 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000238 unsigned long freq;
239
Javi Merinoc36cf072015-02-26 19:00:29 +0000240 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
241 dev = get_cpu_device(cpu);
242 if (!dev) {
243 dev_warn(&cpufreq_device->cool_dev->device,
244 "No cpu device for cpu %d\n", cpu);
245 continue;
246 }
247
248 num_opps = dev_pm_opp_get_opp_count(dev);
Javi Merino459ac372015-08-17 19:21:42 +0100249 if (num_opps > 0)
Javi Merinoc36cf072015-02-26 19:00:29 +0000250 break;
Javi Merino459ac372015-08-17 19:21:42 +0100251 else if (num_opps < 0)
252 return num_opps;
Javi Merinoc36cf072015-02-26 19:00:29 +0000253 }
254
Javi Merino459ac372015-08-17 19:21:42 +0100255 if (num_opps == 0)
256 return -EINVAL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000257
258 power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL);
Javi Merino459ac372015-08-17 19:21:42 +0100259 if (!power_table)
260 return -ENOMEM;
261
Javi Merinoc36cf072015-02-26 19:00:29 +0000262 for (freq = 0, i = 0;
263 opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp);
264 freq++, i++) {
265 u32 freq_mhz, voltage_mv;
266 u64 power;
267
Javi Merino459ac372015-08-17 19:21:42 +0100268 if (i >= num_opps) {
Javi Merinoeba4f882015-08-17 19:21:43 +0100269 ret = -EAGAIN;
270 goto free_power_table;
Javi Merino459ac372015-08-17 19:21:42 +0100271 }
272
Javi Merinoc36cf072015-02-26 19:00:29 +0000273 freq_mhz = freq / 1000000;
274 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530275 dev_pm_opp_put(opp);
Javi Merinoc36cf072015-02-26 19:00:29 +0000276
277 /*
278 * Do the multiplication with MHz and millivolt so as
279 * to not overflow.
280 */
281 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
282 do_div(power, 1000000000);
283
284 /* frequency is stored in power_table in KHz */
285 power_table[i].frequency = freq / 1000;
286
287 /* power is stored in mW */
288 power_table[i].power = power;
289 }
290
Javi Merinoeba4f882015-08-17 19:21:43 +0100291 if (i != num_opps) {
292 ret = PTR_ERR(opp);
293 goto free_power_table;
294 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000295
296 cpufreq_device->cpu_dev = dev;
297 cpufreq_device->dyn_power_table = power_table;
298 cpufreq_device->dyn_power_table_entries = i;
299
Javi Merino459ac372015-08-17 19:21:42 +0100300 return 0;
Javi Merinoeba4f882015-08-17 19:21:43 +0100301
302free_power_table:
303 kfree(power_table);
304
305 return ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000306}
307
308static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_device,
309 u32 freq)
310{
311 int i;
312 struct power_table *pt = cpufreq_device->dyn_power_table;
313
314 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
315 if (freq < pt[i].frequency)
316 break;
317
318 return pt[i - 1].power;
319}
320
321static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_device,
322 u32 power)
323{
324 int i;
325 struct power_table *pt = cpufreq_device->dyn_power_table;
326
327 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
328 if (power < pt[i].power)
329 break;
330
331 return pt[i - 1].frequency;
332}
333
334/**
335 * get_load() - get load for a cpu since last updated
336 * @cpufreq_device: &struct cpufreq_cooling_device for this cpu
337 * @cpu: cpu number
Javi Merinoa53b8392016-02-11 12:00:51 +0000338 * @cpu_idx: index of the cpu in cpufreq_device->allowed_cpus
Javi Merinoc36cf072015-02-26 19:00:29 +0000339 *
340 * Return: The average load of cpu @cpu in percentage since this
341 * function was last called.
342 */
Javi Merinoa53b8392016-02-11 12:00:51 +0000343static u32 get_load(struct cpufreq_cooling_device *cpufreq_device, int cpu,
344 int cpu_idx)
Javi Merinoc36cf072015-02-26 19:00:29 +0000345{
346 u32 load;
347 u64 now, now_idle, delta_time, delta_idle;
348
349 now_idle = get_cpu_idle_time(cpu, &now, 0);
Javi Merinoa53b8392016-02-11 12:00:51 +0000350 delta_idle = now_idle - cpufreq_device->time_in_idle[cpu_idx];
351 delta_time = now - cpufreq_device->time_in_idle_timestamp[cpu_idx];
Javi Merinoc36cf072015-02-26 19:00:29 +0000352
353 if (delta_time <= delta_idle)
354 load = 0;
355 else
356 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
357
Javi Merinoa53b8392016-02-11 12:00:51 +0000358 cpufreq_device->time_in_idle[cpu_idx] = now_idle;
359 cpufreq_device->time_in_idle_timestamp[cpu_idx] = now;
Javi Merinoc36cf072015-02-26 19:00:29 +0000360
361 return load;
362}
363
364/**
365 * get_static_power() - calculate the static power consumed by the cpus
366 * @cpufreq_device: struct &cpufreq_cooling_device for this cpu cdev
367 * @tz: thermal zone device in which we're operating
368 * @freq: frequency in KHz
369 * @power: pointer in which to store the calculated static power
370 *
371 * Calculate the static power consumed by the cpus described by
372 * @cpu_actor running at frequency @freq. This function relies on a
373 * platform specific function that should have been provided when the
374 * actor was registered. If it wasn't, the static power is assumed to
375 * be negligible. The calculated static power is stored in @power.
376 *
377 * Return: 0 on success, -E* on failure.
378 */
379static int get_static_power(struct cpufreq_cooling_device *cpufreq_device,
380 struct thermal_zone_device *tz, unsigned long freq,
381 u32 *power)
382{
383 struct dev_pm_opp *opp;
384 unsigned long voltage;
385 struct cpumask *cpumask = &cpufreq_device->allowed_cpus;
386 unsigned long freq_hz = freq * 1000;
387
388 if (!cpufreq_device->plat_get_static_power ||
389 !cpufreq_device->cpu_dev) {
390 *power = 0;
391 return 0;
392 }
393
Javi Merinoc36cf072015-02-26 19:00:29 +0000394 opp = dev_pm_opp_find_freq_exact(cpufreq_device->cpu_dev, freq_hz,
395 true);
Viresh Kumar3ea32172017-02-07 09:40:05 +0530396 if (IS_ERR(opp)) {
397 dev_warn_ratelimited(cpufreq_device->cpu_dev,
398 "Failed to find OPP for frequency %lu: %ld\n",
399 freq_hz, PTR_ERR(opp));
400 return -EINVAL;
401 }
402
Javi Merinoc36cf072015-02-26 19:00:29 +0000403 voltage = dev_pm_opp_get_voltage(opp);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530404 dev_pm_opp_put(opp);
Javi Merinoc36cf072015-02-26 19:00:29 +0000405
406 if (voltage == 0) {
Viresh Kumar9aec9082017-02-07 09:40:04 +0530407 dev_err_ratelimited(cpufreq_device->cpu_dev,
Viresh Kumar3ea32172017-02-07 09:40:05 +0530408 "Failed to get voltage for frequency %lu\n",
409 freq_hz);
Javi Merinoc36cf072015-02-26 19:00:29 +0000410 return -EINVAL;
411 }
412
413 return cpufreq_device->plat_get_static_power(cpumask, tz->passive_delay,
414 voltage, power);
415}
416
417/**
418 * get_dynamic_power() - calculate the dynamic power
419 * @cpufreq_device: &cpufreq_cooling_device for this cdev
420 * @freq: current frequency
421 *
422 * Return: the dynamic power consumed by the cpus described by
423 * @cpufreq_device.
424 */
425static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_device,
426 unsigned long freq)
427{
428 u32 raw_cpu_power;
429
430 raw_cpu_power = cpu_freq_to_power(cpufreq_device, freq);
431 return (raw_cpu_power * cpufreq_device->last_load) / 100;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530432}
433
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000434/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530435
436/**
437 * cpufreq_get_max_state - callback function to get the max cooling state.
438 * @cdev: thermal cooling device pointer.
439 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000440 *
441 * Callback for the thermal cooling device to return the cpufreq
442 * max cooling state.
443 *
444 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530445 */
446static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
447 unsigned long *state)
448{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100449 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530450
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530451 *state = cpufreq_device->max_level;
452 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530453}
454
455/**
456 * cpufreq_get_cur_state - callback function to get the current cooling state.
457 * @cdev: thermal cooling device pointer.
458 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000459 *
460 * Callback for the thermal cooling device to return the cpufreq
461 * current cooling state.
462 *
463 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530464 */
465static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
466 unsigned long *state)
467{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100468 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530469
hongbo.zhang160b7d82012-10-30 17:48:59 +0100470 *state = cpufreq_device->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000471
hongbo.zhang160b7d82012-10-30 17:48:59 +0100472 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530473}
474
475/**
476 * cpufreq_set_cur_state - callback function to set the current cooling state.
477 * @cdev: thermal cooling device pointer.
478 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fd2013-04-17 17:12:14 +0000479 *
480 * Callback for the thermal cooling device to change the cpufreq
481 * current cooling state.
482 *
483 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530484 */
485static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
486 unsigned long state)
487{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100488 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530489 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
490 unsigned int clip_freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530491
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530492 /* Request state should be less than max_level */
493 if (WARN_ON(state > cpufreq_device->max_level))
494 return -EINVAL;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530495
Viresh Kumar5194fe42014-12-04 09:42:00 +0530496 /* Check if the old cooling action is same as new cooling action */
497 if (cpufreq_device->cpufreq_state == state)
498 return 0;
499
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530500 clip_freq = cpufreq_device->freq_table[state];
Viresh Kumar5194fe42014-12-04 09:42:00 +0530501 cpufreq_device->cpufreq_state = state;
Viresh Kumar59f0d212015-07-30 12:40:33 +0530502 cpufreq_device->clipped_freq = clip_freq;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530503
504 cpufreq_update_policy(cpu);
505
506 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530507}
508
Javi Merinoc36cf072015-02-26 19:00:29 +0000509/**
510 * cpufreq_get_requested_power() - get the current power
511 * @cdev: &thermal_cooling_device pointer
512 * @tz: a valid thermal zone device pointer
513 * @power: pointer in which to store the resulting power
514 *
515 * Calculate the current power consumption of the cpus in milliwatts
516 * and store it in @power. This function should actually calculate
517 * the requested power, but it's hard to get the frequency that
518 * cpufreq would have assigned if there were no thermal limits.
519 * Instead, we calculate the current power on the assumption that the
520 * immediate future will look like the immediate past.
521 *
522 * We use the current frequency and the average load since this
523 * function was last called. In reality, there could have been
524 * multiple opps since this function was last called and that affects
525 * the load calculation. While it's not perfectly accurate, this
526 * simplification is good enough and works. REVISIT this, as more
527 * complex code may be needed if experiments show that it's not
528 * accurate enough.
529 *
530 * Return: 0 on success, -E* if getting the static power failed.
531 */
532static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
533 struct thermal_zone_device *tz,
534 u32 *power)
535{
536 unsigned long freq;
Javi Merino6828a472015-03-02 17:17:20 +0000537 int i = 0, cpu, ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000538 u32 static_power, dynamic_power, total_load = 0;
539 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Javi Merino6828a472015-03-02 17:17:20 +0000540 u32 *load_cpu = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000541
Kapileshwar Singhdd658e02015-03-16 12:00:51 +0000542 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
543
544 /*
545 * All the CPUs are offline, thus the requested power by
546 * the cdev is 0
547 */
548 if (cpu >= nr_cpu_ids) {
549 *power = 0;
550 return 0;
551 }
552
553 freq = cpufreq_quick_get(cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000554
Javi Merino6828a472015-03-02 17:17:20 +0000555 if (trace_thermal_power_cpu_get_power_enabled()) {
556 u32 ncpus = cpumask_weight(&cpufreq_device->allowed_cpus);
557
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530558 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
Javi Merino6828a472015-03-02 17:17:20 +0000559 }
560
Javi Merinoc36cf072015-02-26 19:00:29 +0000561 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
562 u32 load;
563
564 if (cpu_online(cpu))
Javi Merinoa53b8392016-02-11 12:00:51 +0000565 load = get_load(cpufreq_device, cpu, i);
Javi Merinoc36cf072015-02-26 19:00:29 +0000566 else
567 load = 0;
568
569 total_load += load;
Javi Merino6828a472015-03-02 17:17:20 +0000570 if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
571 load_cpu[i] = load;
572
573 i++;
Javi Merinoc36cf072015-02-26 19:00:29 +0000574 }
575
576 cpufreq_device->last_load = total_load;
577
578 dynamic_power = get_dynamic_power(cpufreq_device, freq);
579 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
Javi Merino6828a472015-03-02 17:17:20 +0000580 if (ret) {
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530581 kfree(load_cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000582 return ret;
Javi Merino6828a472015-03-02 17:17:20 +0000583 }
584
585 if (load_cpu) {
586 trace_thermal_power_cpu_get_power(
587 &cpufreq_device->allowed_cpus,
588 freq, load_cpu, i, dynamic_power, static_power);
589
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530590 kfree(load_cpu);
Javi Merino6828a472015-03-02 17:17:20 +0000591 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000592
593 *power = static_power + dynamic_power;
594 return 0;
595}
596
597/**
598 * cpufreq_state2power() - convert a cpu cdev state to power consumed
599 * @cdev: &thermal_cooling_device pointer
600 * @tz: a valid thermal zone device pointer
601 * @state: cooling device state to be converted
602 * @power: pointer in which to store the resulting power
603 *
604 * Convert cooling device state @state into power consumption in
605 * milliwatts assuming 100% load. Store the calculated power in
606 * @power.
607 *
608 * Return: 0 on success, -EINVAL if the cooling device state could not
609 * be converted into a frequency or other -E* if there was an error
610 * when calculating the static power.
611 */
612static int cpufreq_state2power(struct thermal_cooling_device *cdev,
613 struct thermal_zone_device *tz,
614 unsigned long state, u32 *power)
615{
616 unsigned int freq, num_cpus;
Arnd Bergmannd9cc34a2017-02-02 15:46:26 +0100617 cpumask_var_t cpumask;
Javi Merinoc36cf072015-02-26 19:00:29 +0000618 u32 static_power, dynamic_power;
619 int ret;
620 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
621
Arnd Bergmannd9cc34a2017-02-02 15:46:26 +0100622 if (!alloc_cpumask_var(&cpumask, GFP_KERNEL))
623 return -ENOMEM;
624
625 cpumask_and(cpumask, &cpufreq_device->allowed_cpus, cpu_online_mask);
626 num_cpus = cpumask_weight(cpumask);
Javi Merinoc36cf072015-02-26 19:00:29 +0000627
628 /* None of our cpus are online, so no power */
629 if (num_cpus == 0) {
630 *power = 0;
Arnd Bergmannd9cc34a2017-02-02 15:46:26 +0100631 ret = 0;
632 goto out;
Javi Merinoc36cf072015-02-26 19:00:29 +0000633 }
634
635 freq = cpufreq_device->freq_table[state];
Arnd Bergmannd9cc34a2017-02-02 15:46:26 +0100636 if (!freq) {
637 ret = -EINVAL;
638 goto out;
639 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000640
641 dynamic_power = cpu_freq_to_power(cpufreq_device, freq) * num_cpus;
642 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
643 if (ret)
Arnd Bergmannd9cc34a2017-02-02 15:46:26 +0100644 goto out;
Javi Merinoc36cf072015-02-26 19:00:29 +0000645
646 *power = static_power + dynamic_power;
Arnd Bergmannd9cc34a2017-02-02 15:46:26 +0100647out:
648 free_cpumask_var(cpumask);
649 return ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000650}
651
652/**
653 * cpufreq_power2state() - convert power to a cooling device state
654 * @cdev: &thermal_cooling_device pointer
655 * @tz: a valid thermal zone device pointer
656 * @power: power in milliwatts to be converted
657 * @state: pointer in which to store the resulting state
658 *
659 * Calculate a cooling device state for the cpus described by @cdev
660 * that would allow them to consume at most @power mW and store it in
661 * @state. Note that this calculation depends on external factors
662 * such as the cpu load or the current static power. Calling this
663 * function with the same power as input can yield different cooling
664 * device states depending on those external factors.
665 *
666 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
667 * the calculated frequency could not be converted to a valid state.
668 * The latter should not happen unless the frequencies available to
669 * cpufreq have changed since the initialization of the cpu cooling
670 * device.
671 */
672static int cpufreq_power2state(struct thermal_cooling_device *cdev,
673 struct thermal_zone_device *tz, u32 power,
674 unsigned long *state)
675{
676 unsigned int cpu, cur_freq, target_freq;
677 int ret;
678 s32 dyn_power;
679 u32 last_load, normalised_power, static_power;
680 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
681
682 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
683
684 /* None of our cpus are online */
685 if (cpu >= nr_cpu_ids)
686 return -ENODEV;
687
688 cur_freq = cpufreq_quick_get(cpu);
689 ret = get_static_power(cpufreq_device, tz, cur_freq, &static_power);
690 if (ret)
691 return ret;
692
693 dyn_power = power - static_power;
694 dyn_power = dyn_power > 0 ? dyn_power : 0;
695 last_load = cpufreq_device->last_load ?: 1;
696 normalised_power = (dyn_power * 100) / last_load;
697 target_freq = cpu_power_to_freq(cpufreq_device, normalised_power);
698
699 *state = cpufreq_cooling_get_level(cpu, target_freq);
700 if (*state == THERMAL_CSTATE_INVALID) {
Viresh Kumar9aec9082017-02-07 09:40:04 +0530701 dev_err_ratelimited(&cdev->device,
702 "Failed to convert %dKHz for cpu %d into a cdev state\n",
703 target_freq, cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000704 return -EINVAL;
705 }
706
Javi Merino6828a472015-03-02 17:17:20 +0000707 trace_thermal_power_cpu_limit(&cpufreq_device->allowed_cpus,
708 target_freq, *state, power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000709 return 0;
710}
711
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530712/* Bind cpufreq callbacks to thermal cooling device ops */
Brendan Jackmana305a432016-08-17 16:14:59 +0100713
Javi Merinoc36cf072015-02-26 19:00:29 +0000714static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530715 .get_max_state = cpufreq_get_max_state,
716 .get_cur_state = cpufreq_get_cur_state,
717 .set_cur_state = cpufreq_set_cur_state,
718};
719
Brendan Jackmana305a432016-08-17 16:14:59 +0100720static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
721 .get_max_state = cpufreq_get_max_state,
722 .get_cur_state = cpufreq_get_cur_state,
723 .set_cur_state = cpufreq_set_cur_state,
724 .get_requested_power = cpufreq_get_requested_power,
725 .state2power = cpufreq_state2power,
726 .power2state = cpufreq_power2state,
727};
728
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530729/* Notifier for cpufreq policy change */
730static struct notifier_block thermal_cpufreq_notifier_block = {
731 .notifier_call = cpufreq_thermal_notifier,
732};
733
Viresh Kumarf6859012014-12-04 09:42:06 +0530734static unsigned int find_next_max(struct cpufreq_frequency_table *table,
735 unsigned int prev_max)
736{
737 struct cpufreq_frequency_table *pos;
738 unsigned int max = 0;
739
740 cpufreq_for_each_valid_entry(pos, table) {
741 if (pos->frequency > max && pos->frequency < prev_max)
742 max = pos->frequency;
743 }
744
745 return max;
746}
747
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530748/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400749 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
750 * @np: a valid struct device_node to the cooling device device tree node
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530751 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
Viresh Kumar405fb822014-12-04 09:41:55 +0530752 * Normally this should be same as cpufreq policy->related_cpus.
Javi Merinoc36cf072015-02-26 19:00:29 +0000753 * @capacitance: dynamic power coefficient for these cpus
754 * @plat_static_func: function to calculate the static power consumed by these
755 * cpus (optional)
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000756 *
757 * This interface function registers the cpufreq cooling device with the name
758 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400759 * cooling devices. It also gives the opportunity to link the cooling device
760 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000761 *
762 * Return: a valid struct thermal_cooling_device pointer on success,
763 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530764 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400765static struct thermal_cooling_device *
766__cpufreq_cooling_register(struct device_node *np,
Javi Merinoc36cf072015-02-26 19:00:29 +0000767 const struct cpumask *clip_cpus, u32 capacitance,
768 get_static_t plat_static_func)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530769{
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530770 struct cpufreq_policy *policy;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530771 struct thermal_cooling_device *cool_dev;
Viresh Kumar5d3bdb82014-12-04 09:41:52 +0530772 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530773 char dev_name[THERMAL_NAME_LENGTH];
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530774 struct cpufreq_frequency_table *pos, *table;
Arnd Bergmannd9cc34a2017-02-02 15:46:26 +0100775 cpumask_var_t temp_mask;
Javi Merinoc36cf072015-02-26 19:00:29 +0000776 unsigned int freq, i, num_cpus;
Viresh Kumar405fb822014-12-04 09:41:55 +0530777 int ret;
Brendan Jackmana305a432016-08-17 16:14:59 +0100778 struct thermal_cooling_device_ops *cooling_ops;
Matthew Wilcox088db932017-03-10 18:33:28 +0000779 bool first;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530780
Arnd Bergmannd9cc34a2017-02-02 15:46:26 +0100781 if (!alloc_cpumask_var(&temp_mask, GFP_KERNEL))
782 return ERR_PTR(-ENOMEM);
783
784 cpumask_and(temp_mask, clip_cpus, cpu_online_mask);
785 policy = cpufreq_cpu_get(cpumask_first(temp_mask));
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530786 if (!policy) {
787 pr_debug("%s: CPUFreq policy not found\n", __func__);
Arnd Bergmannd9cc34a2017-02-02 15:46:26 +0100788 cool_dev = ERR_PTR(-EPROBE_DEFER);
789 goto free_cpumask;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530790 }
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530791
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530792 table = policy->freq_table;
793 if (!table) {
794 pr_debug("%s: CPUFreq table not found\n", __func__);
795 cool_dev = ERR_PTR(-ENODEV);
796 goto put_policy;
797 }
798
Viresh Kumar98d522f2014-12-04 09:41:50 +0530799 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530800 if (!cpufreq_dev) {
801 cool_dev = ERR_PTR(-ENOMEM);
802 goto put_policy;
803 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530804
Javi Merinoc36cf072015-02-26 19:00:29 +0000805 num_cpus = cpumask_weight(clip_cpus);
806 cpufreq_dev->time_in_idle = kcalloc(num_cpus,
807 sizeof(*cpufreq_dev->time_in_idle),
808 GFP_KERNEL);
809 if (!cpufreq_dev->time_in_idle) {
810 cool_dev = ERR_PTR(-ENOMEM);
811 goto free_cdev;
812 }
813
814 cpufreq_dev->time_in_idle_timestamp =
815 kcalloc(num_cpus, sizeof(*cpufreq_dev->time_in_idle_timestamp),
816 GFP_KERNEL);
817 if (!cpufreq_dev->time_in_idle_timestamp) {
818 cool_dev = ERR_PTR(-ENOMEM);
819 goto free_time_in_idle;
820 }
821
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530822 /* Find max levels */
823 cpufreq_for_each_valid_entry(pos, table)
824 cpufreq_dev->max_level++;
825
Viresh Kumarf6859012014-12-04 09:42:06 +0530826 cpufreq_dev->freq_table = kmalloc(sizeof(*cpufreq_dev->freq_table) *
827 cpufreq_dev->max_level, GFP_KERNEL);
828 if (!cpufreq_dev->freq_table) {
Viresh Kumarf6859012014-12-04 09:42:06 +0530829 cool_dev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +0000830 goto free_time_in_idle_timestamp;
Viresh Kumarf6859012014-12-04 09:42:06 +0530831 }
832
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530833 /* max_level is an index, not a counter */
834 cpufreq_dev->max_level--;
835
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530836 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
837
Javi Merinoc36cf072015-02-26 19:00:29 +0000838 if (capacitance) {
Javi Merinoc36cf072015-02-26 19:00:29 +0000839 cpufreq_dev->plat_get_static_power = plat_static_func;
840
841 ret = build_dyn_power_table(cpufreq_dev, capacitance);
842 if (ret) {
843 cool_dev = ERR_PTR(ret);
844 goto free_table;
845 }
Brendan Jackmana305a432016-08-17 16:14:59 +0100846
847 cooling_ops = &cpufreq_power_cooling_ops;
848 } else {
849 cooling_ops = &cpufreq_cooling_ops;
Javi Merinoc36cf072015-02-26 19:00:29 +0000850 }
851
Matthew Wilcoxae606082016-12-21 09:47:05 -0800852 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
853 if (ret < 0) {
Viresh Kumar730abe02014-12-04 09:41:58 +0530854 cool_dev = ERR_PTR(ret);
Javi Merinoeba4f882015-08-17 19:21:43 +0100855 goto free_power_table;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530856 }
Matthew Wilcoxae606082016-12-21 09:47:05 -0800857 cpufreq_dev->id = ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530858
Viresh Kumarf6859012014-12-04 09:42:06 +0530859 /* Fill freq-table in descending order of frequencies */
860 for (i = 0, freq = -1; i <= cpufreq_dev->max_level; i++) {
861 freq = find_next_max(table, freq);
862 cpufreq_dev->freq_table[i] = freq;
863
864 /* Warn for duplicate entries */
865 if (!freq)
866 pr_warn("%s: table has duplicate entries\n", __func__);
867 else
868 pr_debug("%s: freq:%u KHz\n", __func__, freq);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530869 }
Viresh Kumarf6859012014-12-04 09:42:06 +0530870
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100871 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
872 cpufreq_dev->id);
873
874 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
Brendan Jackmana305a432016-08-17 16:14:59 +0100875 cooling_ops);
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100876 if (IS_ERR(cool_dev))
Matthew Wilcoxae606082016-12-21 09:47:05 -0800877 goto remove_ida;
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100878
Viresh Kumar59f0d212015-07-30 12:40:33 +0530879 cpufreq_dev->clipped_freq = cpufreq_dev->freq_table[0];
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530880 cpufreq_dev->cool_dev = cool_dev;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530881
Russell King02373d72015-08-12 15:22:16 +0530882 mutex_lock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530883 /* Register the notifier for first cpufreq cooling device */
Matthew Wilcox088db932017-03-10 18:33:28 +0000884 first = list_empty(&cpufreq_dev_list);
885 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
886 mutex_unlock(&cooling_list_lock);
887
888 if (first)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530889 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000890 CPUFREQ_POLICY_NOTIFIER);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000891
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530892 goto put_policy;
Viresh Kumar730abe02014-12-04 09:41:58 +0530893
Matthew Wilcoxae606082016-12-21 09:47:05 -0800894remove_ida:
895 ida_simple_remove(&cpufreq_ida, cpufreq_dev->id);
Javi Merinoeba4f882015-08-17 19:21:43 +0100896free_power_table:
897 kfree(cpufreq_dev->dyn_power_table);
Viresh Kumarf6859012014-12-04 09:42:06 +0530898free_table:
899 kfree(cpufreq_dev->freq_table);
Javi Merinoc36cf072015-02-26 19:00:29 +0000900free_time_in_idle_timestamp:
901 kfree(cpufreq_dev->time_in_idle_timestamp);
902free_time_in_idle:
903 kfree(cpufreq_dev->time_in_idle);
Viresh Kumar730abe02014-12-04 09:41:58 +0530904free_cdev:
905 kfree(cpufreq_dev);
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530906put_policy:
907 cpufreq_cpu_put(policy);
Arnd Bergmannd9cc34a2017-02-02 15:46:26 +0100908free_cpumask:
909 free_cpumask_var(temp_mask);
Viresh Kumar730abe02014-12-04 09:41:58 +0530910 return cool_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530911}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400912
913/**
914 * cpufreq_cooling_register - function to create cpufreq cooling device.
915 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
916 *
917 * This interface function registers the cpufreq cooling device with the name
918 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
919 * cooling devices.
920 *
921 * Return: a valid struct thermal_cooling_device pointer on success,
922 * on failure, it returns a corresponding ERR_PTR().
923 */
924struct thermal_cooling_device *
925cpufreq_cooling_register(const struct cpumask *clip_cpus)
926{
Javi Merinoc36cf072015-02-26 19:00:29 +0000927 return __cpufreq_cooling_register(NULL, clip_cpus, 0, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400928}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000929EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530930
931/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400932 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
933 * @np: a valid struct device_node to the cooling device device tree node
934 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
935 *
936 * This interface function registers the cpufreq cooling device with the name
937 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
938 * cooling devices. Using this API, the cpufreq cooling device will be
939 * linked to the device tree node provided.
940 *
941 * Return: a valid struct thermal_cooling_device pointer on success,
942 * on failure, it returns a corresponding ERR_PTR().
943 */
944struct thermal_cooling_device *
945of_cpufreq_cooling_register(struct device_node *np,
946 const struct cpumask *clip_cpus)
947{
948 if (!np)
949 return ERR_PTR(-EINVAL);
950
Javi Merinoc36cf072015-02-26 19:00:29 +0000951 return __cpufreq_cooling_register(np, clip_cpus, 0, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400952}
953EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
954
955/**
Javi Merinoc36cf072015-02-26 19:00:29 +0000956 * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
957 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
958 * @capacitance: dynamic power coefficient for these cpus
959 * @plat_static_func: function to calculate the static power consumed by these
960 * cpus (optional)
961 *
962 * This interface function registers the cpufreq cooling device with
963 * the name "thermal-cpufreq-%x". This api can support multiple
964 * instances of cpufreq cooling devices. Using this function, the
965 * cooling device will implement the power extensions by using a
966 * simple cpu power model. The cpus must have registered their OPPs
967 * using the OPP library.
968 *
969 * An optional @plat_static_func may be provided to calculate the
970 * static power consumed by these cpus. If the platform's static
971 * power consumption is unknown or negligible, make it NULL.
972 *
973 * Return: a valid struct thermal_cooling_device pointer on success,
974 * on failure, it returns a corresponding ERR_PTR().
975 */
976struct thermal_cooling_device *
977cpufreq_power_cooling_register(const struct cpumask *clip_cpus, u32 capacitance,
978 get_static_t plat_static_func)
979{
980 return __cpufreq_cooling_register(NULL, clip_cpus, capacitance,
981 plat_static_func);
982}
983EXPORT_SYMBOL(cpufreq_power_cooling_register);
984
985/**
986 * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
987 * @np: a valid struct device_node to the cooling device device tree node
988 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
989 * @capacitance: dynamic power coefficient for these cpus
990 * @plat_static_func: function to calculate the static power consumed by these
991 * cpus (optional)
992 *
993 * This interface function registers the cpufreq cooling device with
994 * the name "thermal-cpufreq-%x". This api can support multiple
995 * instances of cpufreq cooling devices. Using this API, the cpufreq
996 * cooling device will be linked to the device tree node provided.
997 * Using this function, the cooling device will implement the power
998 * extensions by using a simple cpu power model. The cpus must have
999 * registered their OPPs using the OPP library.
1000 *
1001 * An optional @plat_static_func may be provided to calculate the
1002 * static power consumed by these cpus. If the platform's static
1003 * power consumption is unknown or negligible, make it NULL.
1004 *
1005 * Return: a valid struct thermal_cooling_device pointer on success,
1006 * on failure, it returns a corresponding ERR_PTR().
1007 */
1008struct thermal_cooling_device *
1009of_cpufreq_power_cooling_register(struct device_node *np,
1010 const struct cpumask *clip_cpus,
1011 u32 capacitance,
1012 get_static_t plat_static_func)
1013{
1014 if (!np)
1015 return ERR_PTR(-EINVAL);
1016
1017 return __cpufreq_cooling_register(np, clip_cpus, capacitance,
1018 plat_static_func);
1019}
1020EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
1021
1022/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301023 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
1024 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +00001025 *
1026 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301027 */
1028void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
1029{
Eduardo Valentin50e66c72013-08-15 10:54:46 -04001030 struct cpufreq_cooling_device *cpufreq_dev;
Matthew Wilcox088db932017-03-10 18:33:28 +00001031 bool last;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301032
Eduardo Valentin50e66c72013-08-15 10:54:46 -04001033 if (!cdev)
1034 return;
1035
1036 cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301037
Matthew Wilcoxae606082016-12-21 09:47:05 -08001038 mutex_lock(&cooling_list_lock);
Matthew Wilcox088db932017-03-10 18:33:28 +00001039 list_del(&cpufreq_dev->node);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301040 /* Unregister the notifier for the last cpufreq cooling device */
Matthew Wilcox088db932017-03-10 18:33:28 +00001041 last = list_empty(&cpufreq_dev_list);
1042 mutex_unlock(&cooling_list_lock);
1043
1044 if (last)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301045 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +00001046 CPUFREQ_POLICY_NOTIFIER);
Russell King02373d72015-08-12 15:22:16 +05301047
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301048 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
Matthew Wilcoxae606082016-12-21 09:47:05 -08001049 ida_simple_remove(&cpufreq_ida, cpufreq_dev->id);
Javi Merinoeba4f882015-08-17 19:21:43 +01001050 kfree(cpufreq_dev->dyn_power_table);
Javi Merinoc36cf072015-02-26 19:00:29 +00001051 kfree(cpufreq_dev->time_in_idle_timestamp);
1052 kfree(cpufreq_dev->time_in_idle);
Viresh Kumarf6859012014-12-04 09:42:06 +05301053 kfree(cpufreq_dev->freq_table);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301054 kfree(cpufreq_dev);
1055}
Eduardo Valentin243dbd92013-04-17 17:11:57 +00001056EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);