blob: 7da92114901316c9e6fce1e935cf39d6997385d2 [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>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053035
Javi Merino6828a472015-03-02 17:17:20 +000036#include <trace/events/thermal.h>
37
Viresh Kumar07d888d2014-12-04 09:41:49 +053038/*
39 * Cooling state <-> CPUFreq frequency
40 *
41 * Cooling states are translated to frequencies throughout this driver and this
42 * is the relation between them.
43 *
44 * Highest cooling state corresponds to lowest possible frequency.
45 *
46 * i.e.
47 * level 0 --> 1st Max Freq
48 * level 1 --> 2nd Max Freq
49 * ...
Lina Iyer97a13ed2016-07-15 14:53:58 -060050 * leven n --> core isolated
Viresh Kumar07d888d2014-12-04 09:41:49 +053051 */
52
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053053/**
Javi Merinoc36cf072015-02-26 19:00:29 +000054 * struct power_table - frequency to power conversion
55 * @frequency: frequency in KHz
56 * @power: power in mW
57 *
58 * This structure is built when the cooling device registers and helps
59 * in translating frequency to power and viceversa.
60 */
61struct power_table {
62 u32 frequency;
63 u32 power;
64};
65
66/**
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000067 * struct cpufreq_cooling_device - data for cooling device with cpufreq
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053068 * @id: unique integer value corresponding to each cpufreq_cooling_device
69 * registered.
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000070 * @cool_dev: thermal_cooling_device pointer to keep track of the
71 * registered cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053072 * @cpufreq_state: integer value representing the current state of cpufreq
73 * cooling devices.
Viresh Kumar59f0d212015-07-30 12:40:33 +053074 * @clipped_freq: integer value representing the absolute value of the clipped
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053075 * frequency.
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -060076 * @cpufreq_floor_state: integer value representing the frequency floor state
77 * of cpufreq cooling devices.
78 * @floor_freq: integer value representing the absolute value of the floor
79 * frequency.
Lina Iyer97a13ed2016-07-15 14:53:58 -060080 * @max_level: maximum cooling level. [0..max_level-1: <freq>
81 * max_level: Core unavailable]
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053082 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
Javi Merinofc4de352014-12-15 16:55:52 +000083 * @node: list_head to link all cpufreq_cooling_device together.
Hugh Kang0744f132016-09-07 09:35:39 +090084 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
Javi Merinoc36cf072015-02-26 19:00:29 +000085 * @time_in_idle: previous reading of the absolute time that this cpu was idle
86 * @time_in_idle_timestamp: wall time of the last invocation of
87 * get_cpu_idle_time_us()
88 * @dyn_power_table: array of struct power_table for frequency to power
89 * conversion, sorted in ascending order.
90 * @dyn_power_table_entries: number of entries in the @dyn_power_table array
91 * @cpu_dev: the first cpu_device from @allowed_cpus that has OPPs registered
92 * @plat_get_static_power: callback to calculate the static power
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053093 *
Viresh Kumarbeca6052014-12-04 09:41:48 +053094 * This structure is required for keeping information of each registered
95 * cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053096 */
97struct cpufreq_cooling_device {
98 int id;
99 struct thermal_cooling_device *cool_dev;
100 unsigned int cpufreq_state;
Viresh Kumar59f0d212015-07-30 12:40:33 +0530101 unsigned int clipped_freq;
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600102 unsigned int cpufreq_floor_state;
103 unsigned int floor_freq;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530104 unsigned int max_level;
Viresh Kumarf6859012014-12-04 09:42:06 +0530105 unsigned int *freq_table; /* In descending order */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530106 struct cpumask allowed_cpus;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530107 struct list_head node;
Javi Merinoc36cf072015-02-26 19:00:29 +0000108 u32 last_load;
109 u64 *time_in_idle;
110 u64 *time_in_idle_timestamp;
111 struct power_table *dyn_power_table;
112 int dyn_power_table_entries;
113 struct device *cpu_dev;
114 get_static_t plat_get_static_power;
Lina Iyer986fde12016-02-23 13:08:31 -0700115 struct cpu_cooling_ops *plat_ops;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530116};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530117static DEFINE_IDR(cpufreq_idr);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100118static DEFINE_MUTEX(cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530119
Russell King02373d72015-08-12 15:22:16 +0530120static unsigned int cpufreq_dev_count;
121
122static DEFINE_MUTEX(cooling_list_lock);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530123static LIST_HEAD(cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530124
125/**
126 * get_idr - function to get a unique id.
127 * @idr: struct idr * handle used to create a id.
128 * @id: int * value generated by this function.
Eduardo Valentin79491e52013-04-17 17:11:59 +0000129 *
130 * This function will populate @id with an unique
131 * id, using the idr API.
132 *
133 * Return: 0 on success, an error code on failure.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530134 */
135static int get_idr(struct idr *idr, int *id)
136{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800137 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530138
139 mutex_lock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800140 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530141 mutex_unlock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800142 if (unlikely(ret < 0))
143 return ret;
144 *id = ret;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000145
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530146 return 0;
147}
148
149/**
150 * release_idr - function to free the unique id.
151 * @idr: struct idr * handle used for creating the id.
152 * @id: int value representing the unique id.
153 */
154static void release_idr(struct idr *idr, int id)
155{
156 mutex_lock(&cooling_cpufreq_lock);
157 idr_remove(idr, id);
158 mutex_unlock(&cooling_cpufreq_lock);
159}
160
161/* Below code defines functions to be used for cpufreq as cooling device */
162
163/**
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530164 * get_level: Find the level for a particular frequency
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530165 * @cpufreq_dev: cpufreq_dev for which the property is required
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530166 * @freq: Frequency
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000167 *
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530168 * Return: level on success, THERMAL_CSTATE_INVALID on error.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530169 */
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530170static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_dev,
171 unsigned int freq)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530172{
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530173 unsigned long level;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000174
Lina Iyer97a13ed2016-07-15 14:53:58 -0600175 for (level = 0; level < cpufreq_dev->max_level; level++) {
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530176 if (freq == cpufreq_dev->freq_table[level])
177 return level;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530178
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530179 if (freq > cpufreq_dev->freq_table[level])
180 break;
Zhang Ruifc35b352013-02-08 13:09:32 +0800181 }
Zhang Ruia1167762014-01-02 11:57:48 +0800182
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530183 return THERMAL_CSTATE_INVALID;
Zhang Ruifc35b352013-02-08 13:09:32 +0800184}
185
Eduardo Valentin44952d32013-04-17 17:12:05 +0000186/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530187 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
Eduardo Valentin44952d32013-04-17 17:12:05 +0000188 * @cpu: cpu for which the level is required
189 * @freq: the frequency of interest
190 *
191 * This function will match the cooling level corresponding to the
192 * requested @freq and return it.
193 *
194 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
195 * otherwise.
196 */
Zhang Rui57df8102013-02-08 14:52:06 +0800197unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
198{
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530199 struct cpufreq_cooling_device *cpufreq_dev;
Zhang Rui57df8102013-02-08 14:52:06 +0800200
Russell King02373d72015-08-12 15:22:16 +0530201 mutex_lock(&cooling_list_lock);
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530202 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
203 if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
Russell King02373d72015-08-12 15:22:16 +0530204 mutex_unlock(&cooling_list_lock);
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530205 return get_level(cpufreq_dev, freq);
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530206 }
207 }
Russell King02373d72015-08-12 15:22:16 +0530208 mutex_unlock(&cooling_list_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000209
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530210 pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu);
211 return THERMAL_CSTATE_INVALID;
Zhang Rui57df8102013-02-08 14:52:06 +0800212}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000213EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
Zhang Rui57df8102013-02-08 14:52:06 +0800214
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530215/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530216 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
217 * @nb: struct notifier_block * with callback info.
218 * @event: value showing cpufreq event for which this function invoked.
219 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000220 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100221 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000222 * Every time there is a change in policy, we will intercept and
223 * update the cpufreq policy with thermal constraints.
224 *
225 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530226 */
227static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000228 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530229{
230 struct cpufreq_policy *policy = data;
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600231 unsigned long clipped_freq, floor_freq;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530232 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530233
Viresh Kumara24af232015-07-30 12:40:32 +0530234 if (event != CPUFREQ_ADJUST)
Javi Merinoc36cf072015-02-26 19:00:29 +0000235 return NOTIFY_DONE;
Viresh Kumara24af232015-07-30 12:40:32 +0530236
237 mutex_lock(&cooling_list_lock);
238 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
239 if (!cpumask_test_cpu(policy->cpu, &cpufreq_dev->allowed_cpus))
240 continue;
241
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530242 /*
243 * policy->max is the maximum allowed frequency defined by user
244 * and clipped_freq is the maximum that thermal constraints
245 * allow.
246 *
247 * If clipped_freq is lower than policy->max, then we need to
248 * readjust policy->max.
249 *
250 * But, if clipped_freq is greater than policy->max, we don't
251 * need to do anything.
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600252 *
253 * Similarly, if policy minimum set by the user is less than
254 * the floor_frequency, then adjust the policy->min.
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530255 */
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530256 clipped_freq = cpufreq_dev->clipped_freq;
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600257 floor_freq = cpufreq_dev->floor_freq;
Viresh Kumara24af232015-07-30 12:40:32 +0530258
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600259 if (policy->max > clipped_freq || policy->min < floor_freq)
260 cpufreq_verify_within_limits(policy, floor_freq,
261 clipped_freq);
Viresh Kumara24af232015-07-30 12:40:32 +0530262 break;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530263 }
Viresh Kumara24af232015-07-30 12:40:32 +0530264 mutex_unlock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530265
Javi Merinoc36cf072015-02-26 19:00:29 +0000266 return NOTIFY_OK;
267}
268
269/**
270 * build_dyn_power_table() - create a dynamic power to frequency table
271 * @cpufreq_device: the cpufreq cooling device in which to store the table
272 * @capacitance: dynamic power coefficient for these cpus
273 *
274 * Build a dynamic power to frequency table for this cpu and store it
275 * in @cpufreq_device. This table will be used in cpu_power_to_freq() and
276 * cpu_freq_to_power() to convert between power and frequency
277 * efficiently. Power is stored in mW, frequency in KHz. The
278 * resulting table is in ascending order.
279 *
Javi Merino459ac372015-08-17 19:21:42 +0100280 * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
281 * -ENOMEM if we run out of memory or -EAGAIN if an OPP was
282 * added/enabled while the function was executing.
Javi Merinoc36cf072015-02-26 19:00:29 +0000283 */
284static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_device,
285 u32 capacitance)
286{
287 struct power_table *power_table;
288 struct dev_pm_opp *opp;
289 struct device *dev = NULL;
Javi Merinoeba4f882015-08-17 19:21:43 +0100290 int num_opps = 0, cpu, i, ret = 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000291 unsigned long freq;
292
Javi Merinoc36cf072015-02-26 19:00:29 +0000293 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
294 dev = get_cpu_device(cpu);
295 if (!dev) {
296 dev_warn(&cpufreq_device->cool_dev->device,
297 "No cpu device for cpu %d\n", cpu);
298 continue;
299 }
300
301 num_opps = dev_pm_opp_get_opp_count(dev);
Javi Merino459ac372015-08-17 19:21:42 +0100302 if (num_opps > 0)
Javi Merinoc36cf072015-02-26 19:00:29 +0000303 break;
Javi Merino459ac372015-08-17 19:21:42 +0100304 else if (num_opps < 0)
305 return num_opps;
Javi Merinoc36cf072015-02-26 19:00:29 +0000306 }
307
Javi Merino459ac372015-08-17 19:21:42 +0100308 if (num_opps == 0)
309 return -EINVAL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000310
311 power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL);
Javi Merino459ac372015-08-17 19:21:42 +0100312 if (!power_table)
313 return -ENOMEM;
314
315 rcu_read_lock();
Javi Merinoc36cf072015-02-26 19:00:29 +0000316
317 for (freq = 0, i = 0;
318 opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp);
319 freq++, i++) {
320 u32 freq_mhz, voltage_mv;
321 u64 power;
322
Javi Merino459ac372015-08-17 19:21:42 +0100323 if (i >= num_opps) {
324 rcu_read_unlock();
Javi Merinoeba4f882015-08-17 19:21:43 +0100325 ret = -EAGAIN;
326 goto free_power_table;
Javi Merino459ac372015-08-17 19:21:42 +0100327 }
328
Javi Merinoc36cf072015-02-26 19:00:29 +0000329 freq_mhz = freq / 1000000;
330 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
331
332 /*
333 * Do the multiplication with MHz and millivolt so as
334 * to not overflow.
335 */
336 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
337 do_div(power, 1000000000);
338
339 /* frequency is stored in power_table in KHz */
340 power_table[i].frequency = freq / 1000;
341
342 /* power is stored in mW */
343 power_table[i].power = power;
344 }
345
Javi Merino459ac372015-08-17 19:21:42 +0100346 rcu_read_unlock();
347
Javi Merinoeba4f882015-08-17 19:21:43 +0100348 if (i != num_opps) {
349 ret = PTR_ERR(opp);
350 goto free_power_table;
351 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000352
353 cpufreq_device->cpu_dev = dev;
354 cpufreq_device->dyn_power_table = power_table;
355 cpufreq_device->dyn_power_table_entries = i;
356
Javi Merino459ac372015-08-17 19:21:42 +0100357 return 0;
Javi Merinoeba4f882015-08-17 19:21:43 +0100358
359free_power_table:
360 kfree(power_table);
361
362 return ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000363}
364
365static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_device,
366 u32 freq)
367{
368 int i;
369 struct power_table *pt = cpufreq_device->dyn_power_table;
370
371 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
372 if (freq < pt[i].frequency)
373 break;
374
375 return pt[i - 1].power;
376}
377
378static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_device,
379 u32 power)
380{
381 int i;
382 struct power_table *pt = cpufreq_device->dyn_power_table;
383
384 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
385 if (power < pt[i].power)
386 break;
387
388 return pt[i - 1].frequency;
389}
390
391/**
392 * get_load() - get load for a cpu since last updated
393 * @cpufreq_device: &struct cpufreq_cooling_device for this cpu
394 * @cpu: cpu number
Javi Merinoa53b8392016-02-11 12:00:51 +0000395 * @cpu_idx: index of the cpu in cpufreq_device->allowed_cpus
Javi Merinoc36cf072015-02-26 19:00:29 +0000396 *
397 * Return: The average load of cpu @cpu in percentage since this
398 * function was last called.
399 */
Javi Merinoa53b8392016-02-11 12:00:51 +0000400static u32 get_load(struct cpufreq_cooling_device *cpufreq_device, int cpu,
401 int cpu_idx)
Javi Merinoc36cf072015-02-26 19:00:29 +0000402{
403 u32 load;
404 u64 now, now_idle, delta_time, delta_idle;
405
406 now_idle = get_cpu_idle_time(cpu, &now, 0);
Javi Merinoa53b8392016-02-11 12:00:51 +0000407 delta_idle = now_idle - cpufreq_device->time_in_idle[cpu_idx];
408 delta_time = now - cpufreq_device->time_in_idle_timestamp[cpu_idx];
Javi Merinoc36cf072015-02-26 19:00:29 +0000409
410 if (delta_time <= delta_idle)
411 load = 0;
412 else
413 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
414
Javi Merinoa53b8392016-02-11 12:00:51 +0000415 cpufreq_device->time_in_idle[cpu_idx] = now_idle;
416 cpufreq_device->time_in_idle_timestamp[cpu_idx] = now;
Javi Merinoc36cf072015-02-26 19:00:29 +0000417
418 return load;
419}
420
421/**
422 * get_static_power() - calculate the static power consumed by the cpus
423 * @cpufreq_device: struct &cpufreq_cooling_device for this cpu cdev
424 * @tz: thermal zone device in which we're operating
425 * @freq: frequency in KHz
426 * @power: pointer in which to store the calculated static power
427 *
428 * Calculate the static power consumed by the cpus described by
429 * @cpu_actor running at frequency @freq. This function relies on a
430 * platform specific function that should have been provided when the
431 * actor was registered. If it wasn't, the static power is assumed to
432 * be negligible. The calculated static power is stored in @power.
433 *
434 * Return: 0 on success, -E* on failure.
435 */
436static int get_static_power(struct cpufreq_cooling_device *cpufreq_device,
437 struct thermal_zone_device *tz, unsigned long freq,
438 u32 *power)
439{
440 struct dev_pm_opp *opp;
441 unsigned long voltage;
442 struct cpumask *cpumask = &cpufreq_device->allowed_cpus;
443 unsigned long freq_hz = freq * 1000;
444
445 if (!cpufreq_device->plat_get_static_power ||
446 !cpufreq_device->cpu_dev) {
447 *power = 0;
448 return 0;
449 }
450
451 rcu_read_lock();
452
453 opp = dev_pm_opp_find_freq_exact(cpufreq_device->cpu_dev, freq_hz,
454 true);
455 voltage = dev_pm_opp_get_voltage(opp);
456
457 rcu_read_unlock();
458
459 if (voltage == 0) {
460 dev_warn_ratelimited(cpufreq_device->cpu_dev,
461 "Failed to get voltage for frequency %lu: %ld\n",
462 freq_hz, IS_ERR(opp) ? PTR_ERR(opp) : 0);
463 return -EINVAL;
464 }
465
466 return cpufreq_device->plat_get_static_power(cpumask, tz->passive_delay,
467 voltage, power);
468}
469
470/**
471 * get_dynamic_power() - calculate the dynamic power
472 * @cpufreq_device: &cpufreq_cooling_device for this cdev
473 * @freq: current frequency
474 *
475 * Return: the dynamic power consumed by the cpus described by
476 * @cpufreq_device.
477 */
478static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_device,
479 unsigned long freq)
480{
481 u32 raw_cpu_power;
482
483 raw_cpu_power = cpu_freq_to_power(cpufreq_device, freq);
484 return (raw_cpu_power * cpufreq_device->last_load) / 100;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530485}
486
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000487/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530488
489/**
490 * cpufreq_get_max_state - callback function to get the max cooling state.
491 * @cdev: thermal cooling device pointer.
492 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000493 *
494 * Callback for the thermal cooling device to return the cpufreq
495 * max cooling state.
496 *
497 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530498 */
499static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
500 unsigned long *state)
501{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100502 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530503
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530504 *state = cpufreq_device->max_level;
505 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530506}
507
508/**
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600509 * cpufreq_get_min_state - callback function to get the device floor state.
510 * @cdev: thermal cooling device pointer.
511 * @state: fill this variable with the cooling device floor.
512 *
513 * Callback for the thermal cooling device to return the cpufreq
514 * floor state.
515 *
516 * Return: 0 on success, an error code otherwise.
517 */
518static int cpufreq_get_min_state(struct thermal_cooling_device *cdev,
519 unsigned long *state)
520{
521 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
522
523 *state = cpufreq_device->cpufreq_floor_state;
524
525 return 0;
526}
527
528/**
529 * cpufreq_set_min_state - callback function to set the device floor state.
530 * @cdev: thermal cooling device pointer.
531 * @state: set this variable to the current cooling state.
532 *
533 * Callback for the thermal cooling device to change the cpufreq
534 * floor state.
535 *
536 * Return: 0 on success, an error code otherwise.
537 */
538static int cpufreq_set_min_state(struct thermal_cooling_device *cdev,
539 unsigned long state)
540{
541 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
542 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
543 unsigned int floor_freq;
544
545 if (state > cpufreq_device->max_level)
546 state = cpufreq_device->max_level;
547
548 if (cpufreq_device->cpufreq_floor_state == state)
549 return 0;
550
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600551 cpufreq_device->cpufreq_floor_state = state;
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600552
Ram Chandrasekar23b2db42017-04-19 13:23:31 -0600553 /*
554 * Check if the device has a platform mitigation function that
555 * can handle the CPU freq mitigation, if not, notify cpufreq
556 * framework.
557 */
558 if (cpufreq_device->plat_ops &&
559 cpufreq_device->plat_ops->floor_limit) {
560 /*
561 * Last level is core isolation so use the frequency
562 * of previous state.
563 */
564 if (state == cpufreq_device->max_level)
565 state--;
566 floor_freq = cpufreq_device->freq_table[state];
567 cpufreq_device->floor_freq = floor_freq;
568 cpufreq_device->plat_ops->floor_limit(cpu, floor_freq);
569 } else {
570 floor_freq = cpufreq_device->freq_table[state];
571 cpufreq_device->floor_freq = floor_freq;
572 cpufreq_update_policy(cpu);
573 }
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600574
575 return 0;
576}
577
578/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530579 * cpufreq_get_cur_state - callback function to get the current cooling state.
580 * @cdev: thermal cooling device pointer.
581 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000582 *
583 * Callback for the thermal cooling device to return the cpufreq
584 * current cooling state.
585 *
586 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530587 */
588static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
589 unsigned long *state)
590{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100591 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530592
hongbo.zhang160b7d82012-10-30 17:48:59 +0100593 *state = cpufreq_device->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000594
hongbo.zhang160b7d82012-10-30 17:48:59 +0100595 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530596}
597
598/**
599 * cpufreq_set_cur_state - callback function to set the current cooling state.
600 * @cdev: thermal cooling device pointer.
601 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fdb2013-04-17 17:12:14 +0000602 *
603 * Callback for the thermal cooling device to change the cpufreq
604 * current cooling state.
605 *
606 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530607 */
608static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
609 unsigned long state)
610{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100611 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530612 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
613 unsigned int clip_freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530614
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530615 /* Request state should be less than max_level */
616 if (WARN_ON(state > cpufreq_device->max_level))
617 return -EINVAL;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530618
Viresh Kumar5194fe42014-12-04 09:42:00 +0530619 /* Check if the old cooling action is same as new cooling action */
620 if (cpufreq_device->cpufreq_state == state)
621 return 0;
622
Lina Iyer97a13ed2016-07-15 14:53:58 -0600623 /* If state is the last, isolate the CPU */
624 if (state == cpufreq_device->max_level)
625 return sched_isolate_cpu(cpu);
626 else if (state < cpufreq_device->max_level)
627 sched_unisolate_cpu(cpu);
628
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530629 clip_freq = cpufreq_device->freq_table[state];
Viresh Kumar5194fe42014-12-04 09:42:00 +0530630 cpufreq_device->cpufreq_state = state;
Viresh Kumar59f0d212015-07-30 12:40:33 +0530631 cpufreq_device->clipped_freq = clip_freq;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530632
Lina Iyer986fde12016-02-23 13:08:31 -0700633 /* Check if the device has a platform mitigation function that
634 * can handle the CPU freq mitigation, if not, notify cpufreq
635 * framework.
636 */
637 if (cpufreq_device->plat_ops) {
638 if (cpufreq_device->plat_ops->ceil_limit)
639 cpufreq_device->plat_ops->ceil_limit(cpu,
640 clip_freq);
641 } else {
642 cpufreq_update_policy(cpu);
643 }
Viresh Kumar5194fe42014-12-04 09:42:00 +0530644
645 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530646}
647
Javi Merinoc36cf072015-02-26 19:00:29 +0000648/**
649 * cpufreq_get_requested_power() - get the current power
650 * @cdev: &thermal_cooling_device pointer
651 * @tz: a valid thermal zone device pointer
652 * @power: pointer in which to store the resulting power
653 *
654 * Calculate the current power consumption of the cpus in milliwatts
655 * and store it in @power. This function should actually calculate
656 * the requested power, but it's hard to get the frequency that
657 * cpufreq would have assigned if there were no thermal limits.
658 * Instead, we calculate the current power on the assumption that the
659 * immediate future will look like the immediate past.
660 *
661 * We use the current frequency and the average load since this
662 * function was last called. In reality, there could have been
663 * multiple opps since this function was last called and that affects
664 * the load calculation. While it's not perfectly accurate, this
665 * simplification is good enough and works. REVISIT this, as more
666 * complex code may be needed if experiments show that it's not
667 * accurate enough.
668 *
669 * Return: 0 on success, -E* if getting the static power failed.
670 */
671static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
672 struct thermal_zone_device *tz,
673 u32 *power)
674{
675 unsigned long freq;
Javi Merino6828a472015-03-02 17:17:20 +0000676 int i = 0, cpu, ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000677 u32 static_power, dynamic_power, total_load = 0;
678 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Javi Merino6828a472015-03-02 17:17:20 +0000679 u32 *load_cpu = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000680
Kapileshwar Singhdd658e02015-03-16 12:00:51 +0000681 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
682
683 /*
684 * All the CPUs are offline, thus the requested power by
685 * the cdev is 0
686 */
687 if (cpu >= nr_cpu_ids) {
688 *power = 0;
689 return 0;
690 }
691
692 freq = cpufreq_quick_get(cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000693
Javi Merino6828a472015-03-02 17:17:20 +0000694 if (trace_thermal_power_cpu_get_power_enabled()) {
695 u32 ncpus = cpumask_weight(&cpufreq_device->allowed_cpus);
696
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530697 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
Javi Merino6828a472015-03-02 17:17:20 +0000698 }
699
Javi Merinoc36cf072015-02-26 19:00:29 +0000700 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
701 u32 load;
702
703 if (cpu_online(cpu))
Javi Merinoa53b8392016-02-11 12:00:51 +0000704 load = get_load(cpufreq_device, cpu, i);
Javi Merinoc36cf072015-02-26 19:00:29 +0000705 else
706 load = 0;
707
708 total_load += load;
Javi Merino6828a472015-03-02 17:17:20 +0000709 if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
710 load_cpu[i] = load;
711
712 i++;
Javi Merinoc36cf072015-02-26 19:00:29 +0000713 }
714
715 cpufreq_device->last_load = total_load;
716
717 dynamic_power = get_dynamic_power(cpufreq_device, freq);
718 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
Javi Merino6828a472015-03-02 17:17:20 +0000719 if (ret) {
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530720 kfree(load_cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000721 return ret;
Javi Merino6828a472015-03-02 17:17:20 +0000722 }
723
724 if (load_cpu) {
725 trace_thermal_power_cpu_get_power(
726 &cpufreq_device->allowed_cpus,
727 freq, load_cpu, i, dynamic_power, static_power);
728
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530729 kfree(load_cpu);
Javi Merino6828a472015-03-02 17:17:20 +0000730 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000731
732 *power = static_power + dynamic_power;
733 return 0;
734}
735
736/**
737 * cpufreq_state2power() - convert a cpu cdev state to power consumed
738 * @cdev: &thermal_cooling_device pointer
739 * @tz: a valid thermal zone device pointer
740 * @state: cooling device state to be converted
741 * @power: pointer in which to store the resulting power
742 *
743 * Convert cooling device state @state into power consumption in
744 * milliwatts assuming 100% load. Store the calculated power in
745 * @power.
746 *
747 * Return: 0 on success, -EINVAL if the cooling device state could not
748 * be converted into a frequency or other -E* if there was an error
749 * when calculating the static power.
750 */
751static int cpufreq_state2power(struct thermal_cooling_device *cdev,
752 struct thermal_zone_device *tz,
753 unsigned long state, u32 *power)
754{
755 unsigned int freq, num_cpus;
756 cpumask_t cpumask;
757 u32 static_power, dynamic_power;
758 int ret;
759 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
760
761 cpumask_and(&cpumask, &cpufreq_device->allowed_cpus, cpu_online_mask);
762 num_cpus = cpumask_weight(&cpumask);
763
764 /* None of our cpus are online, so no power */
765 if (num_cpus == 0) {
766 *power = 0;
767 return 0;
768 }
769
770 freq = cpufreq_device->freq_table[state];
771 if (!freq)
772 return -EINVAL;
773
774 dynamic_power = cpu_freq_to_power(cpufreq_device, freq) * num_cpus;
775 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
776 if (ret)
777 return ret;
778
779 *power = static_power + dynamic_power;
780 return 0;
781}
782
783/**
784 * cpufreq_power2state() - convert power to a cooling device state
785 * @cdev: &thermal_cooling_device pointer
786 * @tz: a valid thermal zone device pointer
787 * @power: power in milliwatts to be converted
788 * @state: pointer in which to store the resulting state
789 *
790 * Calculate a cooling device state for the cpus described by @cdev
791 * that would allow them to consume at most @power mW and store it in
792 * @state. Note that this calculation depends on external factors
793 * such as the cpu load or the current static power. Calling this
794 * function with the same power as input can yield different cooling
795 * device states depending on those external factors.
796 *
797 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
798 * the calculated frequency could not be converted to a valid state.
799 * The latter should not happen unless the frequencies available to
800 * cpufreq have changed since the initialization of the cpu cooling
801 * device.
802 */
803static int cpufreq_power2state(struct thermal_cooling_device *cdev,
804 struct thermal_zone_device *tz, u32 power,
805 unsigned long *state)
806{
807 unsigned int cpu, cur_freq, target_freq;
808 int ret;
809 s32 dyn_power;
810 u32 last_load, normalised_power, static_power;
811 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
812
813 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
814
815 /* None of our cpus are online */
816 if (cpu >= nr_cpu_ids)
817 return -ENODEV;
818
819 cur_freq = cpufreq_quick_get(cpu);
820 ret = get_static_power(cpufreq_device, tz, cur_freq, &static_power);
821 if (ret)
822 return ret;
823
824 dyn_power = power - static_power;
825 dyn_power = dyn_power > 0 ? dyn_power : 0;
826 last_load = cpufreq_device->last_load ?: 1;
827 normalised_power = (dyn_power * 100) / last_load;
828 target_freq = cpu_power_to_freq(cpufreq_device, normalised_power);
829
830 *state = cpufreq_cooling_get_level(cpu, target_freq);
831 if (*state == THERMAL_CSTATE_INVALID) {
832 dev_warn_ratelimited(&cdev->device,
833 "Failed to convert %dKHz for cpu %d into a cdev state\n",
834 target_freq, cpu);
835 return -EINVAL;
836 }
837
Javi Merino6828a472015-03-02 17:17:20 +0000838 trace_thermal_power_cpu_limit(&cpufreq_device->allowed_cpus,
839 target_freq, *state, power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000840 return 0;
841}
842
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530843/* Bind cpufreq callbacks to thermal cooling device ops */
Brendan Jackmana305a432016-08-17 16:14:59 +0100844
Javi Merinoc36cf072015-02-26 19:00:29 +0000845static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530846 .get_max_state = cpufreq_get_max_state,
847 .get_cur_state = cpufreq_get_cur_state,
848 .set_cur_state = cpufreq_set_cur_state,
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -0600849 .set_min_state = cpufreq_set_min_state,
850 .get_min_state = cpufreq_get_min_state,
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530851};
852
Brendan Jackmana305a432016-08-17 16:14:59 +0100853static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
854 .get_max_state = cpufreq_get_max_state,
855 .get_cur_state = cpufreq_get_cur_state,
856 .set_cur_state = cpufreq_set_cur_state,
857 .get_requested_power = cpufreq_get_requested_power,
858 .state2power = cpufreq_state2power,
859 .power2state = cpufreq_power2state,
860};
861
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530862/* Notifier for cpufreq policy change */
863static struct notifier_block thermal_cpufreq_notifier_block = {
864 .notifier_call = cpufreq_thermal_notifier,
865};
866
Viresh Kumarf6859012014-12-04 09:42:06 +0530867static unsigned int find_next_max(struct cpufreq_frequency_table *table,
868 unsigned int prev_max)
869{
870 struct cpufreq_frequency_table *pos;
871 unsigned int max = 0;
872
873 cpufreq_for_each_valid_entry(pos, table) {
874 if (pos->frequency > max && pos->frequency < prev_max)
875 max = pos->frequency;
876 }
877
878 return max;
879}
880
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530881/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400882 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
883 * @np: a valid struct device_node to the cooling device device tree node
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530884 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
Viresh Kumar405fb822014-12-04 09:41:55 +0530885 * Normally this should be same as cpufreq policy->related_cpus.
Javi Merinoc36cf072015-02-26 19:00:29 +0000886 * @capacitance: dynamic power coefficient for these cpus
887 * @plat_static_func: function to calculate the static power consumed by these
888 * cpus (optional)
Lina Iyer986fde12016-02-23 13:08:31 -0700889 * @plat_mitig_func: function that does the mitigation by changing the
890 * frequencies (Optional). By default, cpufreq framweork will
891 * be notified of the new limits.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000892 *
893 * This interface function registers the cpufreq cooling device with the name
894 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400895 * cooling devices. It also gives the opportunity to link the cooling device
896 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000897 *
898 * Return: a valid struct thermal_cooling_device pointer on success,
899 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530900 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400901static struct thermal_cooling_device *
902__cpufreq_cooling_register(struct device_node *np,
Javi Merinoc36cf072015-02-26 19:00:29 +0000903 const struct cpumask *clip_cpus, u32 capacitance,
Lina Iyer986fde12016-02-23 13:08:31 -0700904 get_static_t plat_static_func,
905 struct cpu_cooling_ops *plat_ops)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530906{
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530907 struct cpufreq_policy *policy;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530908 struct thermal_cooling_device *cool_dev;
Viresh Kumar5d3bdb82014-12-04 09:41:52 +0530909 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530910 char dev_name[THERMAL_NAME_LENGTH];
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530911 struct cpufreq_frequency_table *pos, *table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000912 unsigned int freq, i, num_cpus;
Viresh Kumar405fb822014-12-04 09:41:55 +0530913 int ret;
Brendan Jackmana305a432016-08-17 16:14:59 +0100914 struct thermal_cooling_device_ops *cooling_ops;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530915
Ram Chandrasekar5682d552017-05-03 16:01:53 -0600916 policy = cpufreq_cpu_get(cpumask_first(clip_cpus));
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530917 if (!policy) {
918 pr_debug("%s: CPUFreq policy not found\n", __func__);
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530919 return ERR_PTR(-EPROBE_DEFER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530920 }
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530921
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530922 table = policy->freq_table;
923 if (!table) {
924 pr_debug("%s: CPUFreq table not found\n", __func__);
925 cool_dev = ERR_PTR(-ENODEV);
926 goto put_policy;
927 }
928
Viresh Kumar98d522f2014-12-04 09:41:50 +0530929 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530930 if (!cpufreq_dev) {
931 cool_dev = ERR_PTR(-ENOMEM);
932 goto put_policy;
933 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530934
Javi Merinoc36cf072015-02-26 19:00:29 +0000935 num_cpus = cpumask_weight(clip_cpus);
936 cpufreq_dev->time_in_idle = kcalloc(num_cpus,
937 sizeof(*cpufreq_dev->time_in_idle),
938 GFP_KERNEL);
939 if (!cpufreq_dev->time_in_idle) {
940 cool_dev = ERR_PTR(-ENOMEM);
941 goto free_cdev;
942 }
943
944 cpufreq_dev->time_in_idle_timestamp =
945 kcalloc(num_cpus, sizeof(*cpufreq_dev->time_in_idle_timestamp),
946 GFP_KERNEL);
947 if (!cpufreq_dev->time_in_idle_timestamp) {
948 cool_dev = ERR_PTR(-ENOMEM);
949 goto free_time_in_idle;
950 }
951
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530952 /* Find max levels */
953 cpufreq_for_each_valid_entry(pos, table)
954 cpufreq_dev->max_level++;
955
Lina Iyer97a13ed2016-07-15 14:53:58 -0600956 /* Last level will indicate the core will be isolated. */
957 cpufreq_dev->max_level++;
958 cpufreq_dev->freq_table = kzalloc(sizeof(*cpufreq_dev->freq_table) *
Viresh Kumarf6859012014-12-04 09:42:06 +0530959 cpufreq_dev->max_level, GFP_KERNEL);
960 if (!cpufreq_dev->freq_table) {
Viresh Kumarf6859012014-12-04 09:42:06 +0530961 cool_dev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +0000962 goto free_time_in_idle_timestamp;
Viresh Kumarf6859012014-12-04 09:42:06 +0530963 }
964
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530965 /* max_level is an index, not a counter */
966 cpufreq_dev->max_level--;
967
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530968 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
969
Javi Merinoc36cf072015-02-26 19:00:29 +0000970 if (capacitance) {
Javi Merinoc36cf072015-02-26 19:00:29 +0000971 cpufreq_dev->plat_get_static_power = plat_static_func;
972
973 ret = build_dyn_power_table(cpufreq_dev, capacitance);
974 if (ret) {
975 cool_dev = ERR_PTR(ret);
976 goto free_table;
977 }
Brendan Jackmana305a432016-08-17 16:14:59 +0100978
979 cooling_ops = &cpufreq_power_cooling_ops;
980 } else {
981 cooling_ops = &cpufreq_cooling_ops;
Javi Merinoc36cf072015-02-26 19:00:29 +0000982 }
983
Lina Iyer986fde12016-02-23 13:08:31 -0700984 cpufreq_dev->plat_ops = plat_ops;
985
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530986 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
987 if (ret) {
Viresh Kumar730abe02014-12-04 09:41:58 +0530988 cool_dev = ERR_PTR(ret);
Javi Merinoeba4f882015-08-17 19:21:43 +0100989 goto free_power_table;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530990 }
991
Viresh Kumarf6859012014-12-04 09:42:06 +0530992 /* Fill freq-table in descending order of frequencies */
Lina Iyer97a13ed2016-07-15 14:53:58 -0600993 for (i = 0, freq = -1; i < cpufreq_dev->max_level; i++) {
Viresh Kumarf6859012014-12-04 09:42:06 +0530994 freq = find_next_max(table, freq);
995 cpufreq_dev->freq_table[i] = freq;
996
997 /* Warn for duplicate entries */
998 if (!freq)
999 pr_warn("%s: table has duplicate entries\n", __func__);
1000 else
1001 pr_debug("%s: freq:%u KHz\n", __func__, freq);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301002 }
Viresh Kumarf6859012014-12-04 09:42:06 +05301003
Lukasz Lubaf840ab12016-05-31 11:32:02 +01001004 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
1005 cpufreq_dev->id);
1006
1007 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
Brendan Jackmana305a432016-08-17 16:14:59 +01001008 cooling_ops);
Lukasz Lubaf840ab12016-05-31 11:32:02 +01001009 if (IS_ERR(cool_dev))
1010 goto remove_idr;
1011
Viresh Kumar59f0d212015-07-30 12:40:33 +05301012 cpufreq_dev->clipped_freq = cpufreq_dev->freq_table[0];
Ram Chandrasekard8e4bf22016-09-21 17:08:06 -06001013 cpufreq_dev->floor_freq =
1014 cpufreq_dev->freq_table[cpufreq_dev->max_level];
1015 cpufreq_dev->cpufreq_floor_state = cpufreq_dev->max_level;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301016 cpufreq_dev->cool_dev = cool_dev;
Viresh Kumar92e615e2014-12-04 09:41:51 +05301017
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301018 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301019
Russell King02373d72015-08-12 15:22:16 +05301020 mutex_lock(&cooling_list_lock);
1021 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
1022 mutex_unlock(&cooling_list_lock);
1023
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301024 /* Register the notifier for first cpufreq cooling device */
Russell King02373d72015-08-12 15:22:16 +05301025 if (!cpufreq_dev_count++)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301026 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +00001027 CPUFREQ_POLICY_NOTIFIER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301028 mutex_unlock(&cooling_cpufreq_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +00001029
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301030 goto put_policy;
Viresh Kumar730abe02014-12-04 09:41:58 +05301031
1032remove_idr:
1033 release_idr(&cpufreq_idr, cpufreq_dev->id);
Javi Merinoeba4f882015-08-17 19:21:43 +01001034free_power_table:
1035 kfree(cpufreq_dev->dyn_power_table);
Viresh Kumarf6859012014-12-04 09:42:06 +05301036free_table:
1037 kfree(cpufreq_dev->freq_table);
Javi Merinoc36cf072015-02-26 19:00:29 +00001038free_time_in_idle_timestamp:
1039 kfree(cpufreq_dev->time_in_idle_timestamp);
1040free_time_in_idle:
1041 kfree(cpufreq_dev->time_in_idle);
Viresh Kumar730abe02014-12-04 09:41:58 +05301042free_cdev:
1043 kfree(cpufreq_dev);
Viresh Kumarf8bfc112016-06-03 10:58:47 +05301044put_policy:
1045 cpufreq_cpu_put(policy);
Viresh Kumar730abe02014-12-04 09:41:58 +05301046
1047 return cool_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301048}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001049
1050/**
1051 * cpufreq_cooling_register - function to create cpufreq cooling device.
1052 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
1053 *
1054 * This interface function registers the cpufreq cooling device with the name
1055 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
1056 * cooling devices.
1057 *
1058 * Return: a valid struct thermal_cooling_device pointer on success,
1059 * on failure, it returns a corresponding ERR_PTR().
1060 */
1061struct thermal_cooling_device *
1062cpufreq_cooling_register(const struct cpumask *clip_cpus)
1063{
Lina Iyer986fde12016-02-23 13:08:31 -07001064 return __cpufreq_cooling_register(NULL, clip_cpus, 0, NULL, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001065}
Eduardo Valentin243dbd92013-04-17 17:11:57 +00001066EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301067
1068/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001069 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
1070 * @np: a valid struct device_node to the cooling device device tree node
1071 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
1072 *
1073 * This interface function registers the cpufreq cooling device with the name
1074 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
1075 * cooling devices. Using this API, the cpufreq cooling device will be
1076 * linked to the device tree node provided.
1077 *
1078 * Return: a valid struct thermal_cooling_device pointer on success,
1079 * on failure, it returns a corresponding ERR_PTR().
1080 */
1081struct thermal_cooling_device *
1082of_cpufreq_cooling_register(struct device_node *np,
1083 const struct cpumask *clip_cpus)
1084{
1085 if (!np)
1086 return ERR_PTR(-EINVAL);
1087
Lina Iyer986fde12016-02-23 13:08:31 -07001088 return __cpufreq_cooling_register(np, clip_cpus, 0, NULL, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -04001089}
1090EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
1091
1092/**
Javi Merinoc36cf072015-02-26 19:00:29 +00001093 * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
1094 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
1095 * @capacitance: dynamic power coefficient for these cpus
1096 * @plat_static_func: function to calculate the static power consumed by these
1097 * cpus (optional)
1098 *
1099 * This interface function registers the cpufreq cooling device with
1100 * the name "thermal-cpufreq-%x". This api can support multiple
1101 * instances of cpufreq cooling devices. Using this function, the
1102 * cooling device will implement the power extensions by using a
1103 * simple cpu power model. The cpus must have registered their OPPs
1104 * using the OPP library.
1105 *
1106 * An optional @plat_static_func may be provided to calculate the
1107 * static power consumed by these cpus. If the platform's static
1108 * power consumption is unknown or negligible, make it NULL.
1109 *
1110 * Return: a valid struct thermal_cooling_device pointer on success,
1111 * on failure, it returns a corresponding ERR_PTR().
1112 */
1113struct thermal_cooling_device *
1114cpufreq_power_cooling_register(const struct cpumask *clip_cpus, u32 capacitance,
1115 get_static_t plat_static_func)
1116{
1117 return __cpufreq_cooling_register(NULL, clip_cpus, capacitance,
Lina Iyer986fde12016-02-23 13:08:31 -07001118 plat_static_func, NULL);
Javi Merinoc36cf072015-02-26 19:00:29 +00001119}
1120EXPORT_SYMBOL(cpufreq_power_cooling_register);
1121
1122/**
Lina Iyer986fde12016-02-23 13:08:31 -07001123 * cpufreq_platform_cooling_register() - create cpufreq cooling device with
1124 * additional platform specific mitigation function.
1125 *
1126 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
1127 * @plat_ops: the platform mitigation functions that will be called insted of
1128 * cpufreq, if provided.
1129 *
1130 * Return: a valid struct thermal_cooling_device pointer on success,
1131 * on failure, it returns a corresponding ERR_PTR().
1132 */
1133struct thermal_cooling_device *
1134cpufreq_platform_cooling_register(const struct cpumask *clip_cpus,
1135 struct cpu_cooling_ops *plat_ops)
1136{
1137 struct device_node *cpu_node;
1138
1139 cpu_node = of_cpu_device_node_get(cpumask_first(clip_cpus));
1140 return __cpufreq_cooling_register(cpu_node, clip_cpus, 0, NULL,
1141 plat_ops);
1142}
1143EXPORT_SYMBOL(cpufreq_platform_cooling_register);
1144
1145/**
Javi Merinoc36cf072015-02-26 19:00:29 +00001146 * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
1147 * @np: a valid struct device_node to the cooling device device tree node
1148 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
1149 * @capacitance: dynamic power coefficient for these cpus
1150 * @plat_static_func: function to calculate the static power consumed by these
1151 * cpus (optional)
1152 *
1153 * This interface function registers the cpufreq cooling device with
1154 * the name "thermal-cpufreq-%x". This api can support multiple
1155 * instances of cpufreq cooling devices. Using this API, the cpufreq
1156 * cooling device will be linked to the device tree node provided.
1157 * Using this function, the cooling device will implement the power
1158 * extensions by using a simple cpu power model. The cpus must have
1159 * registered their OPPs using the OPP library.
1160 *
1161 * An optional @plat_static_func may be provided to calculate the
1162 * static power consumed by these cpus. If the platform's static
1163 * power consumption is unknown or negligible, make it NULL.
1164 *
1165 * Return: a valid struct thermal_cooling_device pointer on success,
1166 * on failure, it returns a corresponding ERR_PTR().
1167 */
1168struct thermal_cooling_device *
1169of_cpufreq_power_cooling_register(struct device_node *np,
1170 const struct cpumask *clip_cpus,
1171 u32 capacitance,
1172 get_static_t plat_static_func)
1173{
1174 if (!np)
1175 return ERR_PTR(-EINVAL);
1176
1177 return __cpufreq_cooling_register(np, clip_cpus, capacitance,
Lina Iyer986fde12016-02-23 13:08:31 -07001178 plat_static_func, NULL);
Javi Merinoc36cf072015-02-26 19:00:29 +00001179}
1180EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
1181
1182/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301183 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
1184 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +00001185 *
1186 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301187 */
1188void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
1189{
Eduardo Valentin50e66c72013-08-15 10:54:46 -04001190 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301191
Eduardo Valentin50e66c72013-08-15 10:54:46 -04001192 if (!cdev)
1193 return;
1194
1195 cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301196
1197 /* Unregister the notifier for the last cpufreq cooling device */
Russell King02373d72015-08-12 15:22:16 +05301198 mutex_lock(&cooling_cpufreq_lock);
1199 if (!--cpufreq_dev_count)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301200 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +00001201 CPUFREQ_POLICY_NOTIFIER);
Russell King02373d72015-08-12 15:22:16 +05301202
1203 mutex_lock(&cooling_list_lock);
1204 list_del(&cpufreq_dev->node);
1205 mutex_unlock(&cooling_list_lock);
1206
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301207 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +01001208
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301209 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
1210 release_idr(&cpufreq_idr, cpufreq_dev->id);
Javi Merinoeba4f882015-08-17 19:21:43 +01001211 kfree(cpufreq_dev->dyn_power_table);
Javi Merinoc36cf072015-02-26 19:00:29 +00001212 kfree(cpufreq_dev->time_in_idle_timestamp);
1213 kfree(cpufreq_dev->time_in_idle);
Viresh Kumarf6859012014-12-04 09:42:06 +05301214 kfree(cpufreq_dev->freq_table);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301215 kfree(cpufreq_dev);
1216}
Eduardo Valentin243dbd92013-04-17 17:11:57 +00001217EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);