blob: 67ec52d5f7fc53b5e79b253e42e6f29e9aabdbe6 [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/**
Viresh Kumar349d39d2017-04-25 15:57:19 +053052 * struct freq_table - frequency table along with power entries
Javi Merinoc36cf072015-02-26 19:00:29 +000053 * @frequency: frequency in KHz
54 * @power: power in mW
55 *
56 * This structure is built when the cooling device registers and helps
Viresh Kumar349d39d2017-04-25 15:57:19 +053057 * in translating frequency to power and vice versa.
Javi Merinoc36cf072015-02-26 19:00:29 +000058 */
Viresh Kumar349d39d2017-04-25 15:57:19 +053059struct freq_table {
Javi Merinoc36cf072015-02-26 19:00:29 +000060 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.
Viresh Kumar04bdbdf2017-04-25 15:57:11 +053068 * @cdev: thermal_cooling_device pointer to keep track of the
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000069 * registered cooling device.
Viresh Kumarb12b6512017-04-25 15:57:16 +053070 * @policy: cpufreq policy.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053071 * @cpufreq_state: integer value representing the current state of cpufreq
72 * cooling devices.
Viresh Kumar59f0d212015-07-30 12:40:33 +053073 * @clipped_freq: integer value representing the absolute value of the clipped
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053074 * frequency.
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053075 * @max_level: maximum cooling level. One less than total number of valid
76 * cpufreq frequencies.
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()
Viresh Kumar02bacb22017-04-25 15:57:17 +053082 * @cpu_dev: the cpu_device of policy->cpu.
Javi Merinoc36cf072015-02-26 19:00:29 +000083 * @plat_get_static_power: callback to calculate the static power
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053084 *
Viresh Kumarbeca6052014-12-04 09:41:48 +053085 * This structure is required for keeping information of each registered
86 * cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053087 */
88struct cpufreq_cooling_device {
89 int id;
Viresh Kumar04bdbdf2017-04-25 15:57:11 +053090 struct thermal_cooling_device *cdev;
Viresh Kumarb12b6512017-04-25 15:57:16 +053091 struct cpufreq_policy *policy;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053092 unsigned int cpufreq_state;
Viresh Kumar59f0d212015-07-30 12:40:33 +053093 unsigned int clipped_freq;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053094 unsigned int max_level;
Viresh Kumar349d39d2017-04-25 15:57:19 +053095 struct freq_table *freq_table; /* In descending order */
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053096 struct list_head node;
Javi Merinoc36cf072015-02-26 19:00:29 +000097 u32 last_load;
98 u64 *time_in_idle;
99 u64 *time_in_idle_timestamp;
Javi Merinoc36cf072015-02-26 19:00:29 +0000100 struct device *cpu_dev;
101 get_static_t plat_get_static_power;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530102};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530103
Viresh Kumarfb8ea302017-04-25 15:57:09 +0530104static DEFINE_IDA(cpufreq_ida);
Russell King02373d72015-08-12 15:22:16 +0530105static DEFINE_MUTEX(cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530106static LIST_HEAD(cpufreq_cdev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530107
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530108/* Below code defines functions to be used for cpufreq as cooling device */
109
110/**
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530111 * get_level: Find the level for a particular frequency
Viresh Kumar1dea4322017-04-25 15:57:10 +0530112 * @cpufreq_cdev: cpufreq_cdev for which the property is required
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530113 * @freq: Frequency
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000114 *
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530115 * Return: level on success, THERMAL_CSTATE_INVALID on error.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530116 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530117static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530118 unsigned int freq)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530119{
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530120 unsigned long level;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000121
Viresh Kumar1dea4322017-04-25 15:57:10 +0530122 for (level = 0; level <= cpufreq_cdev->max_level; level++) {
Viresh Kumar349d39d2017-04-25 15:57:19 +0530123 if (freq == cpufreq_cdev->freq_table[level].frequency)
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530124 return level;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530125
Viresh Kumar349d39d2017-04-25 15:57:19 +0530126 if (freq > cpufreq_cdev->freq_table[level].frequency)
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530127 break;
Zhang Ruifc35b352013-02-08 13:09:32 +0800128 }
Zhang Ruia1167762014-01-02 11:57:48 +0800129
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530130 return THERMAL_CSTATE_INVALID;
Zhang Ruifc35b352013-02-08 13:09:32 +0800131}
132
Eduardo Valentin44952d32013-04-17 17:12:05 +0000133/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530134 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
135 * @nb: struct notifier_block * with callback info.
136 * @event: value showing cpufreq event for which this function invoked.
137 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000138 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100139 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000140 * Every time there is a change in policy, we will intercept and
141 * update the cpufreq policy with thermal constraints.
142 *
143 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530144 */
145static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000146 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530147{
148 struct cpufreq_policy *policy = data;
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530149 unsigned long clipped_freq;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530150 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530151
Viresh Kumara24af232015-07-30 12:40:32 +0530152 if (event != CPUFREQ_ADJUST)
Javi Merinoc36cf072015-02-26 19:00:29 +0000153 return NOTIFY_DONE;
Viresh Kumara24af232015-07-30 12:40:32 +0530154
155 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530156 list_for_each_entry(cpufreq_cdev, &cpufreq_cdev_list, node) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530157 /*
158 * A new copy of the policy is sent to the notifier and can't
159 * compare that directly.
160 */
161 if (policy->cpu != cpufreq_cdev->policy->cpu)
Viresh Kumara24af232015-07-30 12:40:32 +0530162 continue;
163
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530164 /*
165 * policy->max is the maximum allowed frequency defined by user
166 * and clipped_freq is the maximum that thermal constraints
167 * allow.
168 *
169 * If clipped_freq is lower than policy->max, then we need to
170 * readjust policy->max.
171 *
172 * But, if clipped_freq is greater than policy->max, we don't
173 * need to do anything.
174 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530175 clipped_freq = cpufreq_cdev->clipped_freq;
Viresh Kumara24af232015-07-30 12:40:32 +0530176
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530177 if (policy->max > clipped_freq)
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530178 cpufreq_verify_within_limits(policy, 0, clipped_freq);
Viresh Kumara24af232015-07-30 12:40:32 +0530179 break;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530180 }
Viresh Kumara24af232015-07-30 12:40:32 +0530181 mutex_unlock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530182
Javi Merinoc36cf072015-02-26 19:00:29 +0000183 return NOTIFY_OK;
184}
185
186/**
Viresh Kumar349d39d2017-04-25 15:57:19 +0530187 * update_freq_table() - Update the freq table with power numbers
188 * @cpufreq_cdev: the cpufreq cooling device in which to update the table
Javi Merinoc36cf072015-02-26 19:00:29 +0000189 * @capacitance: dynamic power coefficient for these cpus
190 *
Viresh Kumar349d39d2017-04-25 15:57:19 +0530191 * Update the freq table with power numbers. This table will be used in
192 * cpu_power_to_freq() and cpu_freq_to_power() to convert between power and
193 * frequency efficiently. Power is stored in mW, frequency in KHz. The
194 * resulting table is in descending order.
Javi Merinoc36cf072015-02-26 19:00:29 +0000195 *
Javi Merino459ac372015-08-17 19:21:42 +0100196 * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
Viresh Kumar349d39d2017-04-25 15:57:19 +0530197 * or -ENOMEM if we run out of memory.
Javi Merinoc36cf072015-02-26 19:00:29 +0000198 */
Viresh Kumar349d39d2017-04-25 15:57:19 +0530199static int update_freq_table(struct cpufreq_cooling_device *cpufreq_cdev,
200 u32 capacitance)
Javi Merinoc36cf072015-02-26 19:00:29 +0000201{
Viresh Kumar349d39d2017-04-25 15:57:19 +0530202 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000203 struct dev_pm_opp *opp;
204 struct device *dev = NULL;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530205 int num_opps = 0, cpu = cpufreq_cdev->policy->cpu, i;
Javi Merinoc36cf072015-02-26 19:00:29 +0000206
Viresh Kumar02bacb22017-04-25 15:57:17 +0530207 dev = get_cpu_device(cpu);
208 if (unlikely(!dev)) {
209 dev_warn(&cpufreq_cdev->cdev->device,
210 "No cpu device for cpu %d\n", cpu);
211 return -ENODEV;
Javi Merinoc36cf072015-02-26 19:00:29 +0000212 }
213
Viresh Kumar02bacb22017-04-25 15:57:17 +0530214 num_opps = dev_pm_opp_get_opp_count(dev);
215 if (num_opps < 0)
216 return num_opps;
217
Viresh Kumar349d39d2017-04-25 15:57:19 +0530218 /*
219 * The cpufreq table is also built from the OPP table and so the count
220 * should match.
221 */
222 if (num_opps != cpufreq_cdev->max_level + 1) {
223 dev_warn(dev, "Number of OPPs not matching with max_levels\n");
Javi Merino459ac372015-08-17 19:21:42 +0100224 return -EINVAL;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530225 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000226
Viresh Kumar349d39d2017-04-25 15:57:19 +0530227 for (i = 0; i <= cpufreq_cdev->max_level; i++) {
228 unsigned long freq = freq_table[i].frequency * 1000;
229 u32 freq_mhz = freq_table[i].frequency / 1000;
Javi Merinoc36cf072015-02-26 19:00:29 +0000230 u64 power;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530231 u32 voltage_mv;
Javi Merinoc36cf072015-02-26 19:00:29 +0000232
Viresh Kumar349d39d2017-04-25 15:57:19 +0530233 /*
234 * Find ceil frequency as 'freq' may be slightly lower than OPP
235 * freq due to truncation while converting to kHz.
236 */
237 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
238 if (IS_ERR(opp)) {
239 dev_err(dev, "failed to get opp for %lu frequency\n",
240 freq);
241 return -EINVAL;
Javi Merino459ac372015-08-17 19:21:42 +0100242 }
243
Javi Merinoc36cf072015-02-26 19:00:29 +0000244 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530245 dev_pm_opp_put(opp);
Javi Merinoc36cf072015-02-26 19:00:29 +0000246
247 /*
248 * Do the multiplication with MHz and millivolt so as
249 * to not overflow.
250 */
251 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
252 do_div(power, 1000000000);
253
Javi Merinoc36cf072015-02-26 19:00:29 +0000254 /* power is stored in mW */
Viresh Kumar349d39d2017-04-25 15:57:19 +0530255 freq_table[i].power = power;
Javi Merinoeba4f882015-08-17 19:21:43 +0100256 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000257
Viresh Kumar1dea4322017-04-25 15:57:10 +0530258 cpufreq_cdev->cpu_dev = dev;
Javi Merinoc36cf072015-02-26 19:00:29 +0000259
Javi Merino459ac372015-08-17 19:21:42 +0100260 return 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000261}
262
Viresh Kumar1dea4322017-04-25 15:57:10 +0530263static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000264 u32 freq)
265{
266 int i;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530267 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000268
Viresh Kumar349d39d2017-04-25 15:57:19 +0530269 for (i = 1; i <= cpufreq_cdev->max_level; i++)
270 if (freq > freq_table[i].frequency)
Javi Merinoc36cf072015-02-26 19:00:29 +0000271 break;
272
Viresh Kumar349d39d2017-04-25 15:57:19 +0530273 return freq_table[i - 1].power;
Javi Merinoc36cf072015-02-26 19:00:29 +0000274}
275
Viresh Kumar1dea4322017-04-25 15:57:10 +0530276static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000277 u32 power)
278{
279 int i;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530280 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000281
Viresh Kumar349d39d2017-04-25 15:57:19 +0530282 for (i = 1; i <= cpufreq_cdev->max_level; i++)
283 if (power > freq_table[i].power)
Javi Merinoc36cf072015-02-26 19:00:29 +0000284 break;
285
Viresh Kumar349d39d2017-04-25 15:57:19 +0530286 return freq_table[i - 1].frequency;
Javi Merinoc36cf072015-02-26 19:00:29 +0000287}
288
289/**
290 * get_load() - get load for a cpu since last updated
Viresh Kumar1dea4322017-04-25 15:57:10 +0530291 * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu
Javi Merinoc36cf072015-02-26 19:00:29 +0000292 * @cpu: cpu number
Viresh Kumarba76dd92017-04-25 15:57:18 +0530293 * @cpu_idx: index of the cpu in time_in_idle*
Javi Merinoc36cf072015-02-26 19:00:29 +0000294 *
295 * Return: The average load of cpu @cpu in percentage since this
296 * function was last called.
297 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530298static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
Javi Merinoa53b8392016-02-11 12:00:51 +0000299 int cpu_idx)
Javi Merinoc36cf072015-02-26 19:00:29 +0000300{
301 u32 load;
302 u64 now, now_idle, delta_time, delta_idle;
303
304 now_idle = get_cpu_idle_time(cpu, &now, 0);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530305 delta_idle = now_idle - cpufreq_cdev->time_in_idle[cpu_idx];
306 delta_time = now - cpufreq_cdev->time_in_idle_timestamp[cpu_idx];
Javi Merinoc36cf072015-02-26 19:00:29 +0000307
308 if (delta_time <= delta_idle)
309 load = 0;
310 else
311 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
312
Viresh Kumar1dea4322017-04-25 15:57:10 +0530313 cpufreq_cdev->time_in_idle[cpu_idx] = now_idle;
314 cpufreq_cdev->time_in_idle_timestamp[cpu_idx] = now;
Javi Merinoc36cf072015-02-26 19:00:29 +0000315
316 return load;
317}
318
319/**
320 * get_static_power() - calculate the static power consumed by the cpus
Viresh Kumar1dea4322017-04-25 15:57:10 +0530321 * @cpufreq_cdev: struct &cpufreq_cooling_device for this cpu cdev
Javi Merinoc36cf072015-02-26 19:00:29 +0000322 * @tz: thermal zone device in which we're operating
323 * @freq: frequency in KHz
324 * @power: pointer in which to store the calculated static power
325 *
326 * Calculate the static power consumed by the cpus described by
327 * @cpu_actor running at frequency @freq. This function relies on a
328 * platform specific function that should have been provided when the
329 * actor was registered. If it wasn't, the static power is assumed to
330 * be negligible. The calculated static power is stored in @power.
331 *
332 * Return: 0 on success, -E* on failure.
333 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530334static int get_static_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000335 struct thermal_zone_device *tz, unsigned long freq,
336 u32 *power)
337{
338 struct dev_pm_opp *opp;
339 unsigned long voltage;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530340 struct cpumask *cpumask = cpufreq_cdev->policy->related_cpus;
Javi Merinoc36cf072015-02-26 19:00:29 +0000341 unsigned long freq_hz = freq * 1000;
342
Viresh Kumar1dea4322017-04-25 15:57:10 +0530343 if (!cpufreq_cdev->plat_get_static_power || !cpufreq_cdev->cpu_dev) {
Javi Merinoc36cf072015-02-26 19:00:29 +0000344 *power = 0;
345 return 0;
346 }
347
Viresh Kumar1dea4322017-04-25 15:57:10 +0530348 opp = dev_pm_opp_find_freq_exact(cpufreq_cdev->cpu_dev, freq_hz,
Javi Merinoc36cf072015-02-26 19:00:29 +0000349 true);
Viresh Kumar3ea32172017-02-07 09:40:05 +0530350 if (IS_ERR(opp)) {
Viresh Kumar1dea4322017-04-25 15:57:10 +0530351 dev_warn_ratelimited(cpufreq_cdev->cpu_dev,
Viresh Kumar3ea32172017-02-07 09:40:05 +0530352 "Failed to find OPP for frequency %lu: %ld\n",
353 freq_hz, PTR_ERR(opp));
354 return -EINVAL;
355 }
356
Javi Merinoc36cf072015-02-26 19:00:29 +0000357 voltage = dev_pm_opp_get_voltage(opp);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530358 dev_pm_opp_put(opp);
Javi Merinoc36cf072015-02-26 19:00:29 +0000359
360 if (voltage == 0) {
Viresh Kumar1dea4322017-04-25 15:57:10 +0530361 dev_err_ratelimited(cpufreq_cdev->cpu_dev,
Viresh Kumar3ea32172017-02-07 09:40:05 +0530362 "Failed to get voltage for frequency %lu\n",
363 freq_hz);
Javi Merinoc36cf072015-02-26 19:00:29 +0000364 return -EINVAL;
365 }
366
Viresh Kumar1dea4322017-04-25 15:57:10 +0530367 return cpufreq_cdev->plat_get_static_power(cpumask, tz->passive_delay,
368 voltage, power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000369}
370
371/**
372 * get_dynamic_power() - calculate the dynamic power
Viresh Kumar1dea4322017-04-25 15:57:10 +0530373 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
Javi Merinoc36cf072015-02-26 19:00:29 +0000374 * @freq: current frequency
375 *
376 * Return: the dynamic power consumed by the cpus described by
Viresh Kumar1dea4322017-04-25 15:57:10 +0530377 * @cpufreq_cdev.
Javi Merinoc36cf072015-02-26 19:00:29 +0000378 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530379static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000380 unsigned long freq)
381{
382 u32 raw_cpu_power;
383
Viresh Kumar1dea4322017-04-25 15:57:10 +0530384 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
385 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530386}
387
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000388/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530389
390/**
391 * cpufreq_get_max_state - callback function to get the max cooling state.
392 * @cdev: thermal cooling device pointer.
393 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000394 *
395 * Callback for the thermal cooling device to return the cpufreq
396 * max cooling state.
397 *
398 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530399 */
400static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
401 unsigned long *state)
402{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530403 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530404
Viresh Kumar1dea4322017-04-25 15:57:10 +0530405 *state = cpufreq_cdev->max_level;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530406 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530407}
408
409/**
410 * cpufreq_get_cur_state - callback function to get the current cooling state.
411 * @cdev: thermal cooling device pointer.
412 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000413 *
414 * Callback for the thermal cooling device to return the cpufreq
415 * current cooling state.
416 *
417 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530418 */
419static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
420 unsigned long *state)
421{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530422 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530423
Viresh Kumar1dea4322017-04-25 15:57:10 +0530424 *state = cpufreq_cdev->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000425
hongbo.zhang160b7d82012-10-30 17:48:59 +0100426 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530427}
428
429/**
430 * cpufreq_set_cur_state - callback function to set the current cooling state.
431 * @cdev: thermal cooling device pointer.
432 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fd2013-04-17 17:12:14 +0000433 *
434 * Callback for the thermal cooling device to change the cpufreq
435 * current cooling state.
436 *
437 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530438 */
439static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
440 unsigned long state)
441{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530442 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530443 unsigned int clip_freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530444
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530445 /* Request state should be less than max_level */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530446 if (WARN_ON(state > cpufreq_cdev->max_level))
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530447 return -EINVAL;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530448
Viresh Kumar5194fe42014-12-04 09:42:00 +0530449 /* Check if the old cooling action is same as new cooling action */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530450 if (cpufreq_cdev->cpufreq_state == state)
Viresh Kumar5194fe42014-12-04 09:42:00 +0530451 return 0;
452
Viresh Kumar349d39d2017-04-25 15:57:19 +0530453 clip_freq = cpufreq_cdev->freq_table[state].frequency;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530454 cpufreq_cdev->cpufreq_state = state;
455 cpufreq_cdev->clipped_freq = clip_freq;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530456
Viresh Kumarba76dd92017-04-25 15:57:18 +0530457 cpufreq_update_policy(cpufreq_cdev->policy->cpu);
Viresh Kumar5194fe42014-12-04 09:42:00 +0530458
459 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530460}
461
Javi Merinoc36cf072015-02-26 19:00:29 +0000462/**
463 * cpufreq_get_requested_power() - get the current power
464 * @cdev: &thermal_cooling_device pointer
465 * @tz: a valid thermal zone device pointer
466 * @power: pointer in which to store the resulting power
467 *
468 * Calculate the current power consumption of the cpus in milliwatts
469 * and store it in @power. This function should actually calculate
470 * the requested power, but it's hard to get the frequency that
471 * cpufreq would have assigned if there were no thermal limits.
472 * Instead, we calculate the current power on the assumption that the
473 * immediate future will look like the immediate past.
474 *
475 * We use the current frequency and the average load since this
476 * function was last called. In reality, there could have been
477 * multiple opps since this function was last called and that affects
478 * the load calculation. While it's not perfectly accurate, this
479 * simplification is good enough and works. REVISIT this, as more
480 * complex code may be needed if experiments show that it's not
481 * accurate enough.
482 *
483 * Return: 0 on success, -E* if getting the static power failed.
484 */
485static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
486 struct thermal_zone_device *tz,
487 u32 *power)
488{
489 unsigned long freq;
Javi Merino6828a472015-03-02 17:17:20 +0000490 int i = 0, cpu, ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000491 u32 static_power, dynamic_power, total_load = 0;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530492 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530493 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merino6828a472015-03-02 17:17:20 +0000494 u32 *load_cpu = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000495
Viresh Kumarba76dd92017-04-25 15:57:18 +0530496 freq = cpufreq_quick_get(policy->cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000497
Javi Merino6828a472015-03-02 17:17:20 +0000498 if (trace_thermal_power_cpu_get_power_enabled()) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530499 u32 ncpus = cpumask_weight(policy->related_cpus);
Javi Merino6828a472015-03-02 17:17:20 +0000500
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530501 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
Javi Merino6828a472015-03-02 17:17:20 +0000502 }
503
Viresh Kumarba76dd92017-04-25 15:57:18 +0530504 for_each_cpu(cpu, policy->related_cpus) {
Javi Merinoc36cf072015-02-26 19:00:29 +0000505 u32 load;
506
507 if (cpu_online(cpu))
Viresh Kumar1dea4322017-04-25 15:57:10 +0530508 load = get_load(cpufreq_cdev, cpu, i);
Javi Merinoc36cf072015-02-26 19:00:29 +0000509 else
510 load = 0;
511
512 total_load += load;
Javi Merino6828a472015-03-02 17:17:20 +0000513 if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
514 load_cpu[i] = load;
515
516 i++;
Javi Merinoc36cf072015-02-26 19:00:29 +0000517 }
518
Viresh Kumar1dea4322017-04-25 15:57:10 +0530519 cpufreq_cdev->last_load = total_load;
Javi Merinoc36cf072015-02-26 19:00:29 +0000520
Viresh Kumar1dea4322017-04-25 15:57:10 +0530521 dynamic_power = get_dynamic_power(cpufreq_cdev, freq);
522 ret = get_static_power(cpufreq_cdev, tz, freq, &static_power);
Javi Merino6828a472015-03-02 17:17:20 +0000523 if (ret) {
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530524 kfree(load_cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000525 return ret;
Javi Merino6828a472015-03-02 17:17:20 +0000526 }
527
528 if (load_cpu) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530529 trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
530 load_cpu, i, dynamic_power,
531 static_power);
Javi Merino6828a472015-03-02 17:17:20 +0000532
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530533 kfree(load_cpu);
Javi Merino6828a472015-03-02 17:17:20 +0000534 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000535
536 *power = static_power + dynamic_power;
537 return 0;
538}
539
540/**
541 * cpufreq_state2power() - convert a cpu cdev state to power consumed
542 * @cdev: &thermal_cooling_device pointer
543 * @tz: a valid thermal zone device pointer
544 * @state: cooling device state to be converted
545 * @power: pointer in which to store the resulting power
546 *
547 * Convert cooling device state @state into power consumption in
548 * milliwatts assuming 100% load. Store the calculated power in
549 * @power.
550 *
551 * Return: 0 on success, -EINVAL if the cooling device state could not
552 * be converted into a frequency or other -E* if there was an error
553 * when calculating the static power.
554 */
555static int cpufreq_state2power(struct thermal_cooling_device *cdev,
556 struct thermal_zone_device *tz,
557 unsigned long state, u32 *power)
558{
559 unsigned int freq, num_cpus;
Javi Merinoc36cf072015-02-26 19:00:29 +0000560 u32 static_power, dynamic_power;
561 int ret;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530562 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Javi Merinoc36cf072015-02-26 19:00:29 +0000563
Viresh Kumarba76dd92017-04-25 15:57:18 +0530564 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
Javi Merinoc36cf072015-02-26 19:00:29 +0000565
Viresh Kumar349d39d2017-04-25 15:57:19 +0530566 freq = cpufreq_cdev->freq_table[state].frequency;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530567 if (!freq)
568 return -EINVAL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000569
Viresh Kumar1dea4322017-04-25 15:57:10 +0530570 dynamic_power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
571 ret = get_static_power(cpufreq_cdev, tz, freq, &static_power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000572 if (ret)
Viresh Kumarba76dd92017-04-25 15:57:18 +0530573 return ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000574
575 *power = static_power + dynamic_power;
Arnd Bergmannd9cc34a2017-02-02 15:46:26 +0100576 return ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000577}
578
579/**
580 * cpufreq_power2state() - convert power to a cooling device state
581 * @cdev: &thermal_cooling_device pointer
582 * @tz: a valid thermal zone device pointer
583 * @power: power in milliwatts to be converted
584 * @state: pointer in which to store the resulting state
585 *
586 * Calculate a cooling device state for the cpus described by @cdev
587 * that would allow them to consume at most @power mW and store it in
588 * @state. Note that this calculation depends on external factors
589 * such as the cpu load or the current static power. Calling this
590 * function with the same power as input can yield different cooling
591 * device states depending on those external factors.
592 *
593 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
594 * the calculated frequency could not be converted to a valid state.
595 * The latter should not happen unless the frequencies available to
596 * cpufreq have changed since the initialization of the cpu cooling
597 * device.
598 */
599static int cpufreq_power2state(struct thermal_cooling_device *cdev,
600 struct thermal_zone_device *tz, u32 power,
601 unsigned long *state)
602{
Viresh Kumarba76dd92017-04-25 15:57:18 +0530603 unsigned int cur_freq, target_freq;
Javi Merinoc36cf072015-02-26 19:00:29 +0000604 int ret;
605 s32 dyn_power;
606 u32 last_load, normalised_power, static_power;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530607 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530608 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merinoc36cf072015-02-26 19:00:29 +0000609
Viresh Kumarba76dd92017-04-25 15:57:18 +0530610 cur_freq = cpufreq_quick_get(policy->cpu);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530611 ret = get_static_power(cpufreq_cdev, tz, cur_freq, &static_power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000612 if (ret)
613 return ret;
614
615 dyn_power = power - static_power;
616 dyn_power = dyn_power > 0 ? dyn_power : 0;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530617 last_load = cpufreq_cdev->last_load ?: 1;
Javi Merinoc36cf072015-02-26 19:00:29 +0000618 normalised_power = (dyn_power * 100) / last_load;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530619 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000620
Viresh Kumar3e08b2d2017-04-25 15:57:12 +0530621 *state = get_level(cpufreq_cdev, target_freq);
Javi Merinoc36cf072015-02-26 19:00:29 +0000622 if (*state == THERMAL_CSTATE_INVALID) {
Viresh Kumar9aec9082017-02-07 09:40:04 +0530623 dev_err_ratelimited(&cdev->device,
624 "Failed to convert %dKHz for cpu %d into a cdev state\n",
Viresh Kumarba76dd92017-04-25 15:57:18 +0530625 target_freq, policy->cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000626 return -EINVAL;
627 }
628
Viresh Kumarba76dd92017-04-25 15:57:18 +0530629 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
630 power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000631 return 0;
632}
633
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530634/* Bind cpufreq callbacks to thermal cooling device ops */
Brendan Jackmana305a432016-08-17 16:14:59 +0100635
Javi Merinoc36cf072015-02-26 19:00:29 +0000636static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530637 .get_max_state = cpufreq_get_max_state,
638 .get_cur_state = cpufreq_get_cur_state,
639 .set_cur_state = cpufreq_set_cur_state,
640};
641
Brendan Jackmana305a432016-08-17 16:14:59 +0100642static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
643 .get_max_state = cpufreq_get_max_state,
644 .get_cur_state = cpufreq_get_cur_state,
645 .set_cur_state = cpufreq_set_cur_state,
646 .get_requested_power = cpufreq_get_requested_power,
647 .state2power = cpufreq_state2power,
648 .power2state = cpufreq_power2state,
649};
650
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530651/* Notifier for cpufreq policy change */
652static struct notifier_block thermal_cpufreq_notifier_block = {
653 .notifier_call = cpufreq_thermal_notifier,
654};
655
Viresh Kumarf6859012014-12-04 09:42:06 +0530656static unsigned int find_next_max(struct cpufreq_frequency_table *table,
657 unsigned int prev_max)
658{
659 struct cpufreq_frequency_table *pos;
660 unsigned int max = 0;
661
662 cpufreq_for_each_valid_entry(pos, table) {
663 if (pos->frequency > max && pos->frequency < prev_max)
664 max = pos->frequency;
665 }
666
667 return max;
668}
669
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530670/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400671 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
672 * @np: a valid struct device_node to the cooling device device tree node
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530673 * @policy: cpufreq policy
Viresh Kumar405fb822014-12-04 09:41:55 +0530674 * Normally this should be same as cpufreq policy->related_cpus.
Javi Merinoc36cf072015-02-26 19:00:29 +0000675 * @capacitance: dynamic power coefficient for these cpus
676 * @plat_static_func: function to calculate the static power consumed by these
677 * cpus (optional)
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000678 *
679 * This interface function registers the cpufreq cooling device with the name
680 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400681 * cooling devices. It also gives the opportunity to link the cooling device
682 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000683 *
684 * Return: a valid struct thermal_cooling_device pointer on success,
685 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530686 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400687static struct thermal_cooling_device *
688__cpufreq_cooling_register(struct device_node *np,
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530689 struct cpufreq_policy *policy, u32 capacitance,
Javi Merinoc36cf072015-02-26 19:00:29 +0000690 get_static_t plat_static_func)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530691{
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530692 struct thermal_cooling_device *cdev;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530693 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530694 char dev_name[THERMAL_NAME_LENGTH];
Javi Merinoc36cf072015-02-26 19:00:29 +0000695 unsigned int freq, i, num_cpus;
Viresh Kumar405fb822014-12-04 09:41:55 +0530696 int ret;
Brendan Jackmana305a432016-08-17 16:14:59 +0100697 struct thermal_cooling_device_ops *cooling_ops;
Matthew Wilcox088db932017-03-10 18:33:28 +0000698 bool first;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530699
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530700 if (IS_ERR_OR_NULL(policy)) {
701 pr_err("%s: cpufreq policy isn't valid: %p", __func__, policy);
702 return ERR_PTR(-EINVAL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530703 }
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530704
Viresh Kumar55d85292017-04-25 15:57:15 +0530705 i = cpufreq_table_count_valid_entries(policy);
706 if (!i) {
707 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
708 __func__);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530709 return ERR_PTR(-ENODEV);
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530710 }
711
Viresh Kumar1dea4322017-04-25 15:57:10 +0530712 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530713 if (!cpufreq_cdev)
714 return ERR_PTR(-ENOMEM);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530715
Viresh Kumarb12b6512017-04-25 15:57:16 +0530716 cpufreq_cdev->policy = policy;
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530717 num_cpus = cpumask_weight(policy->related_cpus);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530718 cpufreq_cdev->time_in_idle = kcalloc(num_cpus,
719 sizeof(*cpufreq_cdev->time_in_idle),
Javi Merinoc36cf072015-02-26 19:00:29 +0000720 GFP_KERNEL);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530721 if (!cpufreq_cdev->time_in_idle) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530722 cdev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +0000723 goto free_cdev;
724 }
725
Viresh Kumar1dea4322017-04-25 15:57:10 +0530726 cpufreq_cdev->time_in_idle_timestamp =
727 kcalloc(num_cpus, sizeof(*cpufreq_cdev->time_in_idle_timestamp),
Javi Merinoc36cf072015-02-26 19:00:29 +0000728 GFP_KERNEL);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530729 if (!cpufreq_cdev->time_in_idle_timestamp) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530730 cdev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +0000731 goto free_time_in_idle;
732 }
733
Viresh Kumar55d85292017-04-25 15:57:15 +0530734 /* max_level is an index, not a counter */
735 cpufreq_cdev->max_level = i - 1;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530736
Viresh Kumar55d85292017-04-25 15:57:15 +0530737 cpufreq_cdev->freq_table = kmalloc(sizeof(*cpufreq_cdev->freq_table) * i,
738 GFP_KERNEL);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530739 if (!cpufreq_cdev->freq_table) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530740 cdev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +0000741 goto free_time_in_idle_timestamp;
Viresh Kumarf6859012014-12-04 09:42:06 +0530742 }
743
Matthew Wilcoxae606082016-12-21 09:47:05 -0800744 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
745 if (ret < 0) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530746 cdev = ERR_PTR(ret);
Viresh Kumar349d39d2017-04-25 15:57:19 +0530747 goto free_table;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530748 }
Viresh Kumar1dea4322017-04-25 15:57:10 +0530749 cpufreq_cdev->id = ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530750
Viresh Kumar349d39d2017-04-25 15:57:19 +0530751 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
752 cpufreq_cdev->id);
753
Viresh Kumarf6859012014-12-04 09:42:06 +0530754 /* Fill freq-table in descending order of frequencies */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530755 for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) {
Viresh Kumar55d85292017-04-25 15:57:15 +0530756 freq = find_next_max(policy->freq_table, freq);
Viresh Kumar349d39d2017-04-25 15:57:19 +0530757 cpufreq_cdev->freq_table[i].frequency = freq;
Viresh Kumarf6859012014-12-04 09:42:06 +0530758
759 /* Warn for duplicate entries */
760 if (!freq)
761 pr_warn("%s: table has duplicate entries\n", __func__);
762 else
763 pr_debug("%s: freq:%u KHz\n", __func__, freq);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530764 }
Viresh Kumarf6859012014-12-04 09:42:06 +0530765
Viresh Kumar349d39d2017-04-25 15:57:19 +0530766 if (capacitance) {
767 cpufreq_cdev->plat_get_static_power = plat_static_func;
768
769 ret = update_freq_table(cpufreq_cdev, capacitance);
770 if (ret) {
771 cdev = ERR_PTR(ret);
772 goto remove_ida;
773 }
774
775 cooling_ops = &cpufreq_power_cooling_ops;
776 } else {
777 cooling_ops = &cpufreq_cooling_ops;
778 }
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100779
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530780 cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev,
781 cooling_ops);
782 if (IS_ERR(cdev))
Matthew Wilcoxae606082016-12-21 09:47:05 -0800783 goto remove_ida;
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100784
Viresh Kumar349d39d2017-04-25 15:57:19 +0530785 cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0].frequency;
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530786 cpufreq_cdev->cdev = cdev;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530787
Russell King02373d72015-08-12 15:22:16 +0530788 mutex_lock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530789 /* Register the notifier for first cpufreq cooling device */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530790 first = list_empty(&cpufreq_cdev_list);
791 list_add(&cpufreq_cdev->node, &cpufreq_cdev_list);
Matthew Wilcox088db932017-03-10 18:33:28 +0000792 mutex_unlock(&cooling_list_lock);
793
794 if (first)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530795 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000796 CPUFREQ_POLICY_NOTIFIER);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000797
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530798 return cdev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530799
Matthew Wilcoxae606082016-12-21 09:47:05 -0800800remove_ida:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530801 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumarf6859012014-12-04 09:42:06 +0530802free_table:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530803 kfree(cpufreq_cdev->freq_table);
Javi Merinoc36cf072015-02-26 19:00:29 +0000804free_time_in_idle_timestamp:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530805 kfree(cpufreq_cdev->time_in_idle_timestamp);
Javi Merinoc36cf072015-02-26 19:00:29 +0000806free_time_in_idle:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530807 kfree(cpufreq_cdev->time_in_idle);
Viresh Kumar730abe02014-12-04 09:41:58 +0530808free_cdev:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530809 kfree(cpufreq_cdev);
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530810 return cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530811}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400812
813/**
814 * cpufreq_cooling_register - function to create cpufreq cooling device.
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530815 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400816 *
817 * This interface function registers the cpufreq cooling device with the name
818 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
819 * cooling devices.
820 *
821 * Return: a valid struct thermal_cooling_device pointer on success,
822 * on failure, it returns a corresponding ERR_PTR().
823 */
824struct thermal_cooling_device *
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530825cpufreq_cooling_register(struct cpufreq_policy *policy)
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400826{
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530827 return __cpufreq_cooling_register(NULL, policy, 0, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400828}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000829EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530830
831/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400832 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
833 * @np: a valid struct device_node to the cooling device device tree node
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530834 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400835 *
836 * This interface function registers the cpufreq cooling device with the name
837 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
838 * cooling devices. Using this API, the cpufreq cooling device will be
839 * linked to the device tree node provided.
840 *
841 * Return: a valid struct thermal_cooling_device pointer on success,
842 * on failure, it returns a corresponding ERR_PTR().
843 */
844struct thermal_cooling_device *
845of_cpufreq_cooling_register(struct device_node *np,
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530846 struct cpufreq_policy *policy)
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400847{
848 if (!np)
849 return ERR_PTR(-EINVAL);
850
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530851 return __cpufreq_cooling_register(np, policy, 0, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400852}
853EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
854
855/**
Javi Merinoc36cf072015-02-26 19:00:29 +0000856 * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530857 * @policy: cpufreq policy
Javi Merinoc36cf072015-02-26 19:00:29 +0000858 * @capacitance: dynamic power coefficient for these cpus
859 * @plat_static_func: function to calculate the static power consumed by these
860 * cpus (optional)
861 *
862 * This interface function registers the cpufreq cooling device with
863 * the name "thermal-cpufreq-%x". This api can support multiple
864 * instances of cpufreq cooling devices. Using this function, the
865 * cooling device will implement the power extensions by using a
866 * simple cpu power model. The cpus must have registered their OPPs
867 * using the OPP library.
868 *
869 * An optional @plat_static_func may be provided to calculate the
870 * static power consumed by these cpus. If the platform's static
871 * power consumption is unknown or negligible, make it NULL.
872 *
873 * Return: a valid struct thermal_cooling_device pointer on success,
874 * on failure, it returns a corresponding ERR_PTR().
875 */
876struct thermal_cooling_device *
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530877cpufreq_power_cooling_register(struct cpufreq_policy *policy, u32 capacitance,
Javi Merinoc36cf072015-02-26 19:00:29 +0000878 get_static_t plat_static_func)
879{
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530880 return __cpufreq_cooling_register(NULL, policy, capacitance,
Javi Merinoc36cf072015-02-26 19:00:29 +0000881 plat_static_func);
882}
883EXPORT_SYMBOL(cpufreq_power_cooling_register);
884
885/**
886 * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
887 * @np: a valid struct device_node to the cooling device device tree node
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530888 * @policy: cpufreq policy
Javi Merinoc36cf072015-02-26 19:00:29 +0000889 * @capacitance: dynamic power coefficient for these cpus
890 * @plat_static_func: function to calculate the static power consumed by these
891 * cpus (optional)
892 *
893 * This interface function registers the cpufreq cooling device with
894 * the name "thermal-cpufreq-%x". This api can support multiple
895 * instances of cpufreq cooling devices. Using this API, the cpufreq
896 * cooling device will be linked to the device tree node provided.
897 * Using this function, the cooling device will implement the power
898 * extensions by using a simple cpu power model. The cpus must have
899 * registered their OPPs using the OPP library.
900 *
901 * An optional @plat_static_func may be provided to calculate the
902 * static power consumed by these cpus. If the platform's static
903 * power consumption is unknown or negligible, make it NULL.
904 *
905 * Return: a valid struct thermal_cooling_device pointer on success,
906 * on failure, it returns a corresponding ERR_PTR().
907 */
908struct thermal_cooling_device *
909of_cpufreq_power_cooling_register(struct device_node *np,
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530910 struct cpufreq_policy *policy,
Javi Merinoc36cf072015-02-26 19:00:29 +0000911 u32 capacitance,
912 get_static_t plat_static_func)
913{
914 if (!np)
915 return ERR_PTR(-EINVAL);
916
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530917 return __cpufreq_cooling_register(np, policy, capacitance,
Javi Merinoc36cf072015-02-26 19:00:29 +0000918 plat_static_func);
919}
920EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
921
922/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530923 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
924 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000925 *
926 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530927 */
928void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
929{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530930 struct cpufreq_cooling_device *cpufreq_cdev;
Matthew Wilcox088db932017-03-10 18:33:28 +0000931 bool last;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530932
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400933 if (!cdev)
934 return;
935
Viresh Kumar1dea4322017-04-25 15:57:10 +0530936 cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530937
Matthew Wilcoxae606082016-12-21 09:47:05 -0800938 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530939 list_del(&cpufreq_cdev->node);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530940 /* Unregister the notifier for the last cpufreq cooling device */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530941 last = list_empty(&cpufreq_cdev_list);
Matthew Wilcox088db932017-03-10 18:33:28 +0000942 mutex_unlock(&cooling_list_lock);
943
944 if (last)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530945 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000946 CPUFREQ_POLICY_NOTIFIER);
Russell King02373d72015-08-12 15:22:16 +0530947
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530948 thermal_cooling_device_unregister(cpufreq_cdev->cdev);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530949 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530950 kfree(cpufreq_cdev->time_in_idle_timestamp);
951 kfree(cpufreq_cdev->time_in_idle);
952 kfree(cpufreq_cdev->freq_table);
953 kfree(cpufreq_cdev);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530954}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000955EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);