]>
Commit | Line | Data |
---|---|---|
02361418 ADK |
1 | /* |
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 | * | |
73904cbc VK |
7 | * Copyright (C) 2014 Viresh Kumar <viresh.kumar@linaro.org> |
8 | * | |
02361418 ADK |
9 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
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 | */ | |
02361418 ADK |
25 | #include <linux/module.h> |
26 | #include <linux/thermal.h> | |
02361418 ADK |
27 | #include <linux/cpufreq.h> |
28 | #include <linux/err.h> | |
ae606089 | 29 | #include <linux/idr.h> |
c36cf071 | 30 | #include <linux/pm_opp.h> |
02361418 ADK |
31 | #include <linux/slab.h> |
32 | #include <linux/cpu.h> | |
33 | #include <linux/cpu_cooling.h> | |
34 | ||
6828a471 JM |
35 | #include <trace/events/thermal.h> |
36 | ||
07d888d8 VK |
37 | /* |
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 | ||
c36cf071 JM |
51 | /** |
52 | * struct power_table - frequency to power conversion | |
53 | * @frequency: frequency in KHz | |
54 | * @power: power in mW | |
55 | * | |
56 | * This structure is built when the cooling device registers and helps | |
57 | * in translating frequency to power and viceversa. | |
58 | */ | |
59 | struct power_table { | |
60 | u32 frequency; | |
61 | u32 power; | |
62 | }; | |
63 | ||
02361418 | 64 | /** |
3b3c0748 | 65 | * struct cpufreq_cooling_device - data for cooling device with cpufreq |
02361418 ADK |
66 | * @id: unique integer value corresponding to each cpufreq_cooling_device |
67 | * registered. | |
04bdbdf9 | 68 | * @cdev: thermal_cooling_device pointer to keep track of the |
3b3c0748 | 69 | * registered cooling device. |
b12b6519 | 70 | * @policy: cpufreq policy. |
02361418 ADK |
71 | * @cpufreq_state: integer value representing the current state of cpufreq |
72 | * cooling devices. | |
59f0d218 | 73 | * @clipped_freq: integer value representing the absolute value of the clipped |
02361418 | 74 | * frequency. |
dcc6c7fd VK |
75 | * @max_level: maximum cooling level. One less than total number of valid |
76 | * cpufreq frequencies. | |
02361418 | 77 | * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device. |
fc4de356 | 78 | * @node: list_head to link all cpufreq_cooling_device together. |
0744f130 | 79 | * @last_load: load measured by the latest call to cpufreq_get_requested_power() |
c36cf071 JM |
80 | * @time_in_idle: previous reading of the absolute time that this cpu was idle |
81 | * @time_in_idle_timestamp: wall time of the last invocation of | |
82 | * get_cpu_idle_time_us() | |
83 | * @dyn_power_table: array of struct power_table for frequency to power | |
84 | * conversion, sorted in ascending order. | |
85 | * @dyn_power_table_entries: number of entries in the @dyn_power_table array | |
86 | * @cpu_dev: the first cpu_device from @allowed_cpus that has OPPs registered | |
87 | * @plat_get_static_power: callback to calculate the static power | |
02361418 | 88 | * |
beca6053 VK |
89 | * This structure is required for keeping information of each registered |
90 | * cpufreq_cooling_device. | |
02361418 ADK |
91 | */ |
92 | struct cpufreq_cooling_device { | |
93 | int id; | |
04bdbdf9 | 94 | struct thermal_cooling_device *cdev; |
b12b6519 | 95 | struct cpufreq_policy *policy; |
02361418 | 96 | unsigned int cpufreq_state; |
59f0d218 | 97 | unsigned int clipped_freq; |
dcc6c7fd | 98 | unsigned int max_level; |
f6859014 | 99 | unsigned int *freq_table; /* In descending order */ |
02361418 | 100 | struct cpumask allowed_cpus; |
2dcd851f | 101 | struct list_head node; |
c36cf071 JM |
102 | u32 last_load; |
103 | u64 *time_in_idle; | |
104 | u64 *time_in_idle_timestamp; | |
105 | struct power_table *dyn_power_table; | |
106 | int dyn_power_table_entries; | |
107 | struct device *cpu_dev; | |
108 | get_static_t plat_get_static_power; | |
02361418 | 109 | }; |
02361418 | 110 | |
fb8ea308 | 111 | static DEFINE_IDA(cpufreq_ida); |
02373d7c | 112 | static DEFINE_MUTEX(cooling_list_lock); |
1dea432a | 113 | static LIST_HEAD(cpufreq_cdev_list); |
02361418 | 114 | |
02361418 ADK |
115 | /* Below code defines functions to be used for cpufreq as cooling device */ |
116 | ||
117 | /** | |
4843c4a1 | 118 | * get_level: Find the level for a particular frequency |
1dea432a | 119 | * @cpufreq_cdev: cpufreq_cdev for which the property is required |
4843c4a1 | 120 | * @freq: Frequency |
82b9ee40 | 121 | * |
4843c4a1 | 122 | * Return: level on success, THERMAL_CSTATE_INVALID on error. |
02361418 | 123 | */ |
1dea432a | 124 | static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev, |
4843c4a1 | 125 | unsigned int freq) |
02361418 | 126 | { |
4843c4a1 | 127 | unsigned long level; |
a116776f | 128 | |
1dea432a VK |
129 | for (level = 0; level <= cpufreq_cdev->max_level; level++) { |
130 | if (freq == cpufreq_cdev->freq_table[level]) | |
4843c4a1 | 131 | return level; |
02361418 | 132 | |
1dea432a | 133 | if (freq > cpufreq_cdev->freq_table[level]) |
4843c4a1 | 134 | break; |
fc35b35c | 135 | } |
02361418 | 136 | |
4843c4a1 | 137 | return THERMAL_CSTATE_INVALID; |
fc35b35c ZR |
138 | } |
139 | ||
02361418 ADK |
140 | /** |
141 | * cpufreq_thermal_notifier - notifier callback for cpufreq policy change. | |
142 | * @nb: struct notifier_block * with callback info. | |
143 | * @event: value showing cpufreq event for which this function invoked. | |
144 | * @data: callback-specific data | |
bab30554 | 145 | * |
9746b6e7 | 146 | * Callback to hijack the notification on cpufreq policy transition. |
bab30554 EV |
147 | * Every time there is a change in policy, we will intercept and |
148 | * update the cpufreq policy with thermal constraints. | |
149 | * | |
150 | * Return: 0 (success) | |
02361418 ADK |
151 | */ |
152 | static int cpufreq_thermal_notifier(struct notifier_block *nb, | |
5fda7f68 | 153 | unsigned long event, void *data) |
02361418 ADK |
154 | { |
155 | struct cpufreq_policy *policy = data; | |
abcbcc25 | 156 | unsigned long clipped_freq; |
1dea432a | 157 | struct cpufreq_cooling_device *cpufreq_cdev; |
02361418 | 158 | |
a24af233 VK |
159 | if (event != CPUFREQ_ADJUST) |
160 | return NOTIFY_DONE; | |
02361418 | 161 | |
a24af233 | 162 | mutex_lock(&cooling_list_lock); |
1dea432a VK |
163 | list_for_each_entry(cpufreq_cdev, &cpufreq_cdev_list, node) { |
164 | if (!cpumask_test_cpu(policy->cpu, &cpufreq_cdev->allowed_cpus)) | |
a24af233 | 165 | continue; |
c36cf071 | 166 | |
1afb9c53 VK |
167 | /* |
168 | * policy->max is the maximum allowed frequency defined by user | |
169 | * and clipped_freq is the maximum that thermal constraints | |
170 | * allow. | |
171 | * | |
172 | * If clipped_freq is lower than policy->max, then we need to | |
173 | * readjust policy->max. | |
174 | * | |
175 | * But, if clipped_freq is greater than policy->max, we don't | |
176 | * need to do anything. | |
177 | */ | |
1dea432a | 178 | clipped_freq = cpufreq_cdev->clipped_freq; |
c36cf071 | 179 | |
1afb9c53 | 180 | if (policy->max > clipped_freq) |
abcbcc25 | 181 | cpufreq_verify_within_limits(policy, 0, clipped_freq); |
c36cf071 | 182 | break; |
c36cf071 | 183 | } |
a24af233 | 184 | mutex_unlock(&cooling_list_lock); |
c36cf071 JM |
185 | |
186 | return NOTIFY_OK; | |
187 | } | |
188 | ||
189 | /** | |
190 | * build_dyn_power_table() - create a dynamic power to frequency table | |
1dea432a | 191 | * @cpufreq_cdev: the cpufreq cooling device in which to store the table |
c36cf071 JM |
192 | * @capacitance: dynamic power coefficient for these cpus |
193 | * | |
194 | * Build a dynamic power to frequency table for this cpu and store it | |
1dea432a | 195 | * in @cpufreq_cdev. This table will be used in cpu_power_to_freq() and |
c36cf071 JM |
196 | * cpu_freq_to_power() to convert between power and frequency |
197 | * efficiently. Power is stored in mW, frequency in KHz. The | |
198 | * resulting table is in ascending order. | |
199 | * | |
459ac375 JM |
200 | * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs, |
201 | * -ENOMEM if we run out of memory or -EAGAIN if an OPP was | |
202 | * added/enabled while the function was executing. | |
c36cf071 | 203 | */ |
1dea432a | 204 | static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_cdev, |
c36cf071 JM |
205 | u32 capacitance) |
206 | { | |
207 | struct power_table *power_table; | |
208 | struct dev_pm_opp *opp; | |
209 | struct device *dev = NULL; | |
eba4f88d | 210 | int num_opps = 0, cpu, i, ret = 0; |
c36cf071 JM |
211 | unsigned long freq; |
212 | ||
1dea432a | 213 | for_each_cpu(cpu, &cpufreq_cdev->allowed_cpus) { |
c36cf071 JM |
214 | dev = get_cpu_device(cpu); |
215 | if (!dev) { | |
04bdbdf9 | 216 | dev_warn(&cpufreq_cdev->cdev->device, |
c36cf071 | 217 | "No cpu device for cpu %d\n", cpu); |
2dcd851f | 218 | continue; |
c36cf071 | 219 | } |
2dcd851f | 220 | |
c36cf071 | 221 | num_opps = dev_pm_opp_get_opp_count(dev); |
459ac375 | 222 | if (num_opps > 0) |
c36cf071 | 223 | break; |
459ac375 JM |
224 | else if (num_opps < 0) |
225 | return num_opps; | |
c36cf071 | 226 | } |
02361418 | 227 | |
459ac375 JM |
228 | if (num_opps == 0) |
229 | return -EINVAL; | |
02361418 | 230 | |
c36cf071 | 231 | power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL); |
459ac375 JM |
232 | if (!power_table) |
233 | return -ENOMEM; | |
234 | ||
c36cf071 JM |
235 | for (freq = 0, i = 0; |
236 | opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp); | |
237 | freq++, i++) { | |
238 | u32 freq_mhz, voltage_mv; | |
239 | u64 power; | |
240 | ||
459ac375 | 241 | if (i >= num_opps) { |
eba4f88d JM |
242 | ret = -EAGAIN; |
243 | goto free_power_table; | |
459ac375 JM |
244 | } |
245 | ||
c36cf071 JM |
246 | freq_mhz = freq / 1000000; |
247 | voltage_mv = dev_pm_opp_get_voltage(opp) / 1000; | |
8a31d9d9 | 248 | dev_pm_opp_put(opp); |
c36cf071 JM |
249 | |
250 | /* | |
251 | * Do the multiplication with MHz and millivolt so as | |
252 | * to not overflow. | |
253 | */ | |
254 | power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv; | |
255 | do_div(power, 1000000000); | |
256 | ||
257 | /* frequency is stored in power_table in KHz */ | |
258 | power_table[i].frequency = freq / 1000; | |
259 | ||
260 | /* power is stored in mW */ | |
261 | power_table[i].power = power; | |
262 | } | |
263 | ||
eba4f88d JM |
264 | if (i != num_opps) { |
265 | ret = PTR_ERR(opp); | |
266 | goto free_power_table; | |
267 | } | |
c36cf071 | 268 | |
1dea432a VK |
269 | cpufreq_cdev->cpu_dev = dev; |
270 | cpufreq_cdev->dyn_power_table = power_table; | |
271 | cpufreq_cdev->dyn_power_table_entries = i; | |
c36cf071 | 272 | |
459ac375 | 273 | return 0; |
eba4f88d JM |
274 | |
275 | free_power_table: | |
276 | kfree(power_table); | |
277 | ||
278 | return ret; | |
c36cf071 JM |
279 | } |
280 | ||
1dea432a | 281 | static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev, |
c36cf071 JM |
282 | u32 freq) |
283 | { | |
284 | int i; | |
1dea432a | 285 | struct power_table *pt = cpufreq_cdev->dyn_power_table; |
c36cf071 | 286 | |
1dea432a | 287 | for (i = 1; i < cpufreq_cdev->dyn_power_table_entries; i++) |
c36cf071 JM |
288 | if (freq < pt[i].frequency) |
289 | break; | |
290 | ||
291 | return pt[i - 1].power; | |
292 | } | |
293 | ||
1dea432a | 294 | static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev, |
c36cf071 JM |
295 | u32 power) |
296 | { | |
297 | int i; | |
1dea432a | 298 | struct power_table *pt = cpufreq_cdev->dyn_power_table; |
c36cf071 | 299 | |
1dea432a | 300 | for (i = 1; i < cpufreq_cdev->dyn_power_table_entries; i++) |
c36cf071 JM |
301 | if (power < pt[i].power) |
302 | break; | |
303 | ||
304 | return pt[i - 1].frequency; | |
305 | } | |
306 | ||
307 | /** | |
308 | * get_load() - get load for a cpu since last updated | |
1dea432a | 309 | * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu |
c36cf071 | 310 | * @cpu: cpu number |
1dea432a | 311 | * @cpu_idx: index of the cpu in cpufreq_cdev->allowed_cpus |
c36cf071 JM |
312 | * |
313 | * Return: The average load of cpu @cpu in percentage since this | |
314 | * function was last called. | |
315 | */ | |
1dea432a | 316 | static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu, |
a53b8394 | 317 | int cpu_idx) |
c36cf071 JM |
318 | { |
319 | u32 load; | |
320 | u64 now, now_idle, delta_time, delta_idle; | |
321 | ||
322 | now_idle = get_cpu_idle_time(cpu, &now, 0); | |
1dea432a VK |
323 | delta_idle = now_idle - cpufreq_cdev->time_in_idle[cpu_idx]; |
324 | delta_time = now - cpufreq_cdev->time_in_idle_timestamp[cpu_idx]; | |
c36cf071 JM |
325 | |
326 | if (delta_time <= delta_idle) | |
327 | load = 0; | |
328 | else | |
329 | load = div64_u64(100 * (delta_time - delta_idle), delta_time); | |
330 | ||
1dea432a VK |
331 | cpufreq_cdev->time_in_idle[cpu_idx] = now_idle; |
332 | cpufreq_cdev->time_in_idle_timestamp[cpu_idx] = now; | |
c36cf071 JM |
333 | |
334 | return load; | |
335 | } | |
336 | ||
337 | /** | |
338 | * get_static_power() - calculate the static power consumed by the cpus | |
1dea432a | 339 | * @cpufreq_cdev: struct &cpufreq_cooling_device for this cpu cdev |
c36cf071 JM |
340 | * @tz: thermal zone device in which we're operating |
341 | * @freq: frequency in KHz | |
342 | * @power: pointer in which to store the calculated static power | |
343 | * | |
344 | * Calculate the static power consumed by the cpus described by | |
345 | * @cpu_actor running at frequency @freq. This function relies on a | |
346 | * platform specific function that should have been provided when the | |
347 | * actor was registered. If it wasn't, the static power is assumed to | |
348 | * be negligible. The calculated static power is stored in @power. | |
349 | * | |
350 | * Return: 0 on success, -E* on failure. | |
351 | */ | |
1dea432a | 352 | static int get_static_power(struct cpufreq_cooling_device *cpufreq_cdev, |
c36cf071 JM |
353 | struct thermal_zone_device *tz, unsigned long freq, |
354 | u32 *power) | |
355 | { | |
356 | struct dev_pm_opp *opp; | |
357 | unsigned long voltage; | |
1dea432a | 358 | struct cpumask *cpumask = &cpufreq_cdev->allowed_cpus; |
c36cf071 JM |
359 | unsigned long freq_hz = freq * 1000; |
360 | ||
1dea432a | 361 | if (!cpufreq_cdev->plat_get_static_power || !cpufreq_cdev->cpu_dev) { |
c36cf071 JM |
362 | *power = 0; |
363 | return 0; | |
364 | } | |
365 | ||
1dea432a | 366 | opp = dev_pm_opp_find_freq_exact(cpufreq_cdev->cpu_dev, freq_hz, |
c36cf071 | 367 | true); |
3ea3217c | 368 | if (IS_ERR(opp)) { |
1dea432a | 369 | dev_warn_ratelimited(cpufreq_cdev->cpu_dev, |
3ea3217c VK |
370 | "Failed to find OPP for frequency %lu: %ld\n", |
371 | freq_hz, PTR_ERR(opp)); | |
372 | return -EINVAL; | |
373 | } | |
374 | ||
c36cf071 | 375 | voltage = dev_pm_opp_get_voltage(opp); |
8a31d9d9 | 376 | dev_pm_opp_put(opp); |
c36cf071 JM |
377 | |
378 | if (voltage == 0) { | |
1dea432a | 379 | dev_err_ratelimited(cpufreq_cdev->cpu_dev, |
3ea3217c VK |
380 | "Failed to get voltage for frequency %lu\n", |
381 | freq_hz); | |
c36cf071 JM |
382 | return -EINVAL; |
383 | } | |
384 | ||
1dea432a VK |
385 | return cpufreq_cdev->plat_get_static_power(cpumask, tz->passive_delay, |
386 | voltage, power); | |
c36cf071 JM |
387 | } |
388 | ||
389 | /** | |
390 | * get_dynamic_power() - calculate the dynamic power | |
1dea432a | 391 | * @cpufreq_cdev: &cpufreq_cooling_device for this cdev |
c36cf071 JM |
392 | * @freq: current frequency |
393 | * | |
394 | * Return: the dynamic power consumed by the cpus described by | |
1dea432a | 395 | * @cpufreq_cdev. |
c36cf071 | 396 | */ |
1dea432a | 397 | static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev, |
c36cf071 JM |
398 | unsigned long freq) |
399 | { | |
400 | u32 raw_cpu_power; | |
401 | ||
1dea432a VK |
402 | raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq); |
403 | return (raw_cpu_power * cpufreq_cdev->last_load) / 100; | |
02361418 ADK |
404 | } |
405 | ||
1b9e3526 | 406 | /* cpufreq cooling device callback functions are defined below */ |
02361418 ADK |
407 | |
408 | /** | |
409 | * cpufreq_get_max_state - callback function to get the max cooling state. | |
410 | * @cdev: thermal cooling device pointer. | |
411 | * @state: fill this variable with the max cooling state. | |
62c00421 EV |
412 | * |
413 | * Callback for the thermal cooling device to return the cpufreq | |
414 | * max cooling state. | |
415 | * | |
416 | * Return: 0 on success, an error code otherwise. | |
02361418 ADK |
417 | */ |
418 | static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, | |
419 | unsigned long *state) | |
420 | { | |
1dea432a | 421 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
9c51b05a | 422 | |
1dea432a | 423 | *state = cpufreq_cdev->max_level; |
dcc6c7fd | 424 | return 0; |
02361418 ADK |
425 | } |
426 | ||
427 | /** | |
428 | * cpufreq_get_cur_state - callback function to get the current cooling state. | |
429 | * @cdev: thermal cooling device pointer. | |
430 | * @state: fill this variable with the current cooling state. | |
3672552d EV |
431 | * |
432 | * Callback for the thermal cooling device to return the cpufreq | |
433 | * current cooling state. | |
434 | * | |
435 | * Return: 0 on success, an error code otherwise. | |
02361418 ADK |
436 | */ |
437 | static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, | |
438 | unsigned long *state) | |
439 | { | |
1dea432a | 440 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
02361418 | 441 | |
1dea432a | 442 | *state = cpufreq_cdev->cpufreq_state; |
79491e53 | 443 | |
160b7d80 | 444 | return 0; |
02361418 ADK |
445 | } |
446 | ||
447 | /** | |
448 | * cpufreq_set_cur_state - callback function to set the current cooling state. | |
449 | * @cdev: thermal cooling device pointer. | |
450 | * @state: set this variable to the current cooling state. | |
56e05fdb EV |
451 | * |
452 | * Callback for the thermal cooling device to change the cpufreq | |
453 | * current cooling state. | |
454 | * | |
455 | * Return: 0 on success, an error code otherwise. | |
02361418 ADK |
456 | */ |
457 | static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, | |
458 | unsigned long state) | |
459 | { | |
1dea432a | 460 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
5194fe46 | 461 | unsigned int clip_freq; |
4843c4a1 VK |
462 | |
463 | /* Request state should be less than max_level */ | |
1dea432a | 464 | if (WARN_ON(state > cpufreq_cdev->max_level)) |
4843c4a1 | 465 | return -EINVAL; |
5194fe46 VK |
466 | |
467 | /* Check if the old cooling action is same as new cooling action */ | |
1dea432a | 468 | if (cpufreq_cdev->cpufreq_state == state) |
5194fe46 | 469 | return 0; |
02361418 | 470 | |
1dea432a VK |
471 | clip_freq = cpufreq_cdev->freq_table[state]; |
472 | cpufreq_cdev->cpufreq_state = state; | |
473 | cpufreq_cdev->clipped_freq = clip_freq; | |
5194fe46 | 474 | |
18f301c9 | 475 | cpufreq_update_policy(cpumask_any(&cpufreq_cdev->allowed_cpus)); |
5194fe46 VK |
476 | |
477 | return 0; | |
02361418 ADK |
478 | } |
479 | ||
c36cf071 JM |
480 | /** |
481 | * cpufreq_get_requested_power() - get the current power | |
482 | * @cdev: &thermal_cooling_device pointer | |
483 | * @tz: a valid thermal zone device pointer | |
484 | * @power: pointer in which to store the resulting power | |
485 | * | |
486 | * Calculate the current power consumption of the cpus in milliwatts | |
487 | * and store it in @power. This function should actually calculate | |
488 | * the requested power, but it's hard to get the frequency that | |
489 | * cpufreq would have assigned if there were no thermal limits. | |
490 | * Instead, we calculate the current power on the assumption that the | |
491 | * immediate future will look like the immediate past. | |
492 | * | |
493 | * We use the current frequency and the average load since this | |
494 | * function was last called. In reality, there could have been | |
495 | * multiple opps since this function was last called and that affects | |
496 | * the load calculation. While it's not perfectly accurate, this | |
497 | * simplification is good enough and works. REVISIT this, as more | |
498 | * complex code may be needed if experiments show that it's not | |
499 | * accurate enough. | |
500 | * | |
501 | * Return: 0 on success, -E* if getting the static power failed. | |
502 | */ | |
503 | static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev, | |
504 | struct thermal_zone_device *tz, | |
505 | u32 *power) | |
506 | { | |
507 | unsigned long freq; | |
6828a471 | 508 | int i = 0, cpu, ret; |
c36cf071 | 509 | u32 static_power, dynamic_power, total_load = 0; |
1dea432a | 510 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
6828a471 | 511 | u32 *load_cpu = NULL; |
c36cf071 | 512 | |
1dea432a | 513 | cpu = cpumask_any_and(&cpufreq_cdev->allowed_cpus, cpu_online_mask); |
dd658e02 KS |
514 | |
515 | /* | |
516 | * All the CPUs are offline, thus the requested power by | |
517 | * the cdev is 0 | |
518 | */ | |
519 | if (cpu >= nr_cpu_ids) { | |
520 | *power = 0; | |
521 | return 0; | |
522 | } | |
523 | ||
524 | freq = cpufreq_quick_get(cpu); | |
c36cf071 | 525 | |
6828a471 | 526 | if (trace_thermal_power_cpu_get_power_enabled()) { |
1dea432a | 527 | u32 ncpus = cpumask_weight(&cpufreq_cdev->allowed_cpus); |
6828a471 | 528 | |
a71544cd | 529 | load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL); |
6828a471 JM |
530 | } |
531 | ||
1dea432a | 532 | for_each_cpu(cpu, &cpufreq_cdev->allowed_cpus) { |
c36cf071 JM |
533 | u32 load; |
534 | ||
535 | if (cpu_online(cpu)) | |
1dea432a | 536 | load = get_load(cpufreq_cdev, cpu, i); |
c36cf071 JM |
537 | else |
538 | load = 0; | |
539 | ||
540 | total_load += load; | |
6828a471 JM |
541 | if (trace_thermal_power_cpu_limit_enabled() && load_cpu) |
542 | load_cpu[i] = load; | |
543 | ||
544 | i++; | |
c36cf071 JM |
545 | } |
546 | ||
1dea432a | 547 | cpufreq_cdev->last_load = total_load; |
c36cf071 | 548 | |
1dea432a VK |
549 | dynamic_power = get_dynamic_power(cpufreq_cdev, freq); |
550 | ret = get_static_power(cpufreq_cdev, tz, freq, &static_power); | |
6828a471 | 551 | if (ret) { |
a71544cd | 552 | kfree(load_cpu); |
c36cf071 | 553 | return ret; |
6828a471 JM |
554 | } |
555 | ||
556 | if (load_cpu) { | |
557 | trace_thermal_power_cpu_get_power( | |
1dea432a | 558 | &cpufreq_cdev->allowed_cpus, |
6828a471 JM |
559 | freq, load_cpu, i, dynamic_power, static_power); |
560 | ||
a71544cd | 561 | kfree(load_cpu); |
6828a471 | 562 | } |
c36cf071 JM |
563 | |
564 | *power = static_power + dynamic_power; | |
565 | return 0; | |
566 | } | |
567 | ||
568 | /** | |
569 | * cpufreq_state2power() - convert a cpu cdev state to power consumed | |
570 | * @cdev: &thermal_cooling_device pointer | |
571 | * @tz: a valid thermal zone device pointer | |
572 | * @state: cooling device state to be converted | |
573 | * @power: pointer in which to store the resulting power | |
574 | * | |
575 | * Convert cooling device state @state into power consumption in | |
576 | * milliwatts assuming 100% load. Store the calculated power in | |
577 | * @power. | |
578 | * | |
579 | * Return: 0 on success, -EINVAL if the cooling device state could not | |
580 | * be converted into a frequency or other -E* if there was an error | |
581 | * when calculating the static power. | |
582 | */ | |
583 | static int cpufreq_state2power(struct thermal_cooling_device *cdev, | |
584 | struct thermal_zone_device *tz, | |
585 | unsigned long state, u32 *power) | |
586 | { | |
587 | unsigned int freq, num_cpus; | |
d9cc34a6 | 588 | cpumask_var_t cpumask; |
c36cf071 JM |
589 | u32 static_power, dynamic_power; |
590 | int ret; | |
1dea432a | 591 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
c36cf071 | 592 | |
d9cc34a6 AB |
593 | if (!alloc_cpumask_var(&cpumask, GFP_KERNEL)) |
594 | return -ENOMEM; | |
595 | ||
1dea432a | 596 | cpumask_and(cpumask, &cpufreq_cdev->allowed_cpus, cpu_online_mask); |
d9cc34a6 | 597 | num_cpus = cpumask_weight(cpumask); |
c36cf071 JM |
598 | |
599 | /* None of our cpus are online, so no power */ | |
600 | if (num_cpus == 0) { | |
601 | *power = 0; | |
d9cc34a6 AB |
602 | ret = 0; |
603 | goto out; | |
c36cf071 JM |
604 | } |
605 | ||
1dea432a | 606 | freq = cpufreq_cdev->freq_table[state]; |
d9cc34a6 AB |
607 | if (!freq) { |
608 | ret = -EINVAL; | |
609 | goto out; | |
610 | } | |
c36cf071 | 611 | |
1dea432a VK |
612 | dynamic_power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus; |
613 | ret = get_static_power(cpufreq_cdev, tz, freq, &static_power); | |
c36cf071 | 614 | if (ret) |
d9cc34a6 | 615 | goto out; |
c36cf071 JM |
616 | |
617 | *power = static_power + dynamic_power; | |
d9cc34a6 AB |
618 | out: |
619 | free_cpumask_var(cpumask); | |
620 | return ret; | |
c36cf071 JM |
621 | } |
622 | ||
623 | /** | |
624 | * cpufreq_power2state() - convert power to a cooling device state | |
625 | * @cdev: &thermal_cooling_device pointer | |
626 | * @tz: a valid thermal zone device pointer | |
627 | * @power: power in milliwatts to be converted | |
628 | * @state: pointer in which to store the resulting state | |
629 | * | |
630 | * Calculate a cooling device state for the cpus described by @cdev | |
631 | * that would allow them to consume at most @power mW and store it in | |
632 | * @state. Note that this calculation depends on external factors | |
633 | * such as the cpu load or the current static power. Calling this | |
634 | * function with the same power as input can yield different cooling | |
635 | * device states depending on those external factors. | |
636 | * | |
637 | * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if | |
638 | * the calculated frequency could not be converted to a valid state. | |
639 | * The latter should not happen unless the frequencies available to | |
640 | * cpufreq have changed since the initialization of the cpu cooling | |
641 | * device. | |
642 | */ | |
643 | static int cpufreq_power2state(struct thermal_cooling_device *cdev, | |
644 | struct thermal_zone_device *tz, u32 power, | |
645 | unsigned long *state) | |
646 | { | |
647 | unsigned int cpu, cur_freq, target_freq; | |
648 | int ret; | |
649 | s32 dyn_power; | |
650 | u32 last_load, normalised_power, static_power; | |
1dea432a | 651 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
c36cf071 | 652 | |
1dea432a | 653 | cpu = cpumask_any_and(&cpufreq_cdev->allowed_cpus, cpu_online_mask); |
c36cf071 JM |
654 | |
655 | /* None of our cpus are online */ | |
656 | if (cpu >= nr_cpu_ids) | |
657 | return -ENODEV; | |
658 | ||
659 | cur_freq = cpufreq_quick_get(cpu); | |
1dea432a | 660 | ret = get_static_power(cpufreq_cdev, tz, cur_freq, &static_power); |
c36cf071 JM |
661 | if (ret) |
662 | return ret; | |
663 | ||
664 | dyn_power = power - static_power; | |
665 | dyn_power = dyn_power > 0 ? dyn_power : 0; | |
1dea432a | 666 | last_load = cpufreq_cdev->last_load ?: 1; |
c36cf071 | 667 | normalised_power = (dyn_power * 100) / last_load; |
1dea432a | 668 | target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power); |
c36cf071 | 669 | |
3e08b2df | 670 | *state = get_level(cpufreq_cdev, target_freq); |
c36cf071 | 671 | if (*state == THERMAL_CSTATE_INVALID) { |
9aec9082 VK |
672 | dev_err_ratelimited(&cdev->device, |
673 | "Failed to convert %dKHz for cpu %d into a cdev state\n", | |
674 | target_freq, cpu); | |
c36cf071 JM |
675 | return -EINVAL; |
676 | } | |
677 | ||
1dea432a | 678 | trace_thermal_power_cpu_limit(&cpufreq_cdev->allowed_cpus, |
6828a471 | 679 | target_freq, *state, power); |
c36cf071 JM |
680 | return 0; |
681 | } | |
682 | ||
02361418 | 683 | /* Bind cpufreq callbacks to thermal cooling device ops */ |
a305a438 | 684 | |
c36cf071 | 685 | static struct thermal_cooling_device_ops cpufreq_cooling_ops = { |
02361418 ADK |
686 | .get_max_state = cpufreq_get_max_state, |
687 | .get_cur_state = cpufreq_get_cur_state, | |
688 | .set_cur_state = cpufreq_set_cur_state, | |
689 | }; | |
690 | ||
a305a438 BJ |
691 | static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = { |
692 | .get_max_state = cpufreq_get_max_state, | |
693 | .get_cur_state = cpufreq_get_cur_state, | |
694 | .set_cur_state = cpufreq_set_cur_state, | |
695 | .get_requested_power = cpufreq_get_requested_power, | |
696 | .state2power = cpufreq_state2power, | |
697 | .power2state = cpufreq_power2state, | |
698 | }; | |
699 | ||
02361418 ADK |
700 | /* Notifier for cpufreq policy change */ |
701 | static struct notifier_block thermal_cpufreq_notifier_block = { | |
702 | .notifier_call = cpufreq_thermal_notifier, | |
703 | }; | |
704 | ||
f6859014 VK |
705 | static unsigned int find_next_max(struct cpufreq_frequency_table *table, |
706 | unsigned int prev_max) | |
707 | { | |
708 | struct cpufreq_frequency_table *pos; | |
709 | unsigned int max = 0; | |
710 | ||
711 | cpufreq_for_each_valid_entry(pos, table) { | |
712 | if (pos->frequency > max && pos->frequency < prev_max) | |
713 | max = pos->frequency; | |
714 | } | |
715 | ||
716 | return max; | |
717 | } | |
718 | ||
02361418 | 719 | /** |
39d99cff EV |
720 | * __cpufreq_cooling_register - helper function to create cpufreq cooling device |
721 | * @np: a valid struct device_node to the cooling device device tree node | |
4d753aa7 | 722 | * @policy: cpufreq policy |
405fb825 | 723 | * Normally this should be same as cpufreq policy->related_cpus. |
c36cf071 JM |
724 | * @capacitance: dynamic power coefficient for these cpus |
725 | * @plat_static_func: function to calculate the static power consumed by these | |
726 | * cpus (optional) | |
12cb08ba EV |
727 | * |
728 | * This interface function registers the cpufreq cooling device with the name | |
729 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq | |
39d99cff EV |
730 | * cooling devices. It also gives the opportunity to link the cooling device |
731 | * with a device tree node, in order to bind it via the thermal DT code. | |
12cb08ba EV |
732 | * |
733 | * Return: a valid struct thermal_cooling_device pointer on success, | |
734 | * on failure, it returns a corresponding ERR_PTR(). | |
02361418 | 735 | */ |
39d99cff EV |
736 | static struct thermal_cooling_device * |
737 | __cpufreq_cooling_register(struct device_node *np, | |
4d753aa7 | 738 | struct cpufreq_policy *policy, u32 capacitance, |
c36cf071 | 739 | get_static_t plat_static_func) |
02361418 | 740 | { |
04bdbdf9 | 741 | struct thermal_cooling_device *cdev; |
1dea432a | 742 | struct cpufreq_cooling_device *cpufreq_cdev; |
02361418 | 743 | char dev_name[THERMAL_NAME_LENGTH]; |
c36cf071 | 744 | unsigned int freq, i, num_cpus; |
405fb825 | 745 | int ret; |
a305a438 | 746 | struct thermal_cooling_device_ops *cooling_ops; |
088db931 | 747 | bool first; |
02361418 | 748 | |
4d753aa7 VK |
749 | if (IS_ERR_OR_NULL(policy)) { |
750 | pr_err("%s: cpufreq policy isn't valid: %p", __func__, policy); | |
751 | return ERR_PTR(-EINVAL); | |
f8bfc116 VK |
752 | } |
753 | ||
55d85293 VK |
754 | i = cpufreq_table_count_valid_entries(policy); |
755 | if (!i) { | |
756 | pr_debug("%s: CPUFreq table not found or has no valid entries\n", | |
757 | __func__); | |
4d753aa7 | 758 | return ERR_PTR(-ENODEV); |
02361418 | 759 | } |
0f1be51c | 760 | |
1dea432a | 761 | cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL); |
4d753aa7 VK |
762 | if (!cpufreq_cdev) |
763 | return ERR_PTR(-ENOMEM); | |
02361418 | 764 | |
b12b6519 | 765 | cpufreq_cdev->policy = policy; |
4d753aa7 | 766 | num_cpus = cpumask_weight(policy->related_cpus); |
1dea432a VK |
767 | cpufreq_cdev->time_in_idle = kcalloc(num_cpus, |
768 | sizeof(*cpufreq_cdev->time_in_idle), | |
c36cf071 | 769 | GFP_KERNEL); |
1dea432a | 770 | if (!cpufreq_cdev->time_in_idle) { |
04bdbdf9 | 771 | cdev = ERR_PTR(-ENOMEM); |
c36cf071 JM |
772 | goto free_cdev; |
773 | } | |
774 | ||
1dea432a VK |
775 | cpufreq_cdev->time_in_idle_timestamp = |
776 | kcalloc(num_cpus, sizeof(*cpufreq_cdev->time_in_idle_timestamp), | |
c36cf071 | 777 | GFP_KERNEL); |
1dea432a | 778 | if (!cpufreq_cdev->time_in_idle_timestamp) { |
04bdbdf9 | 779 | cdev = ERR_PTR(-ENOMEM); |
c36cf071 JM |
780 | goto free_time_in_idle; |
781 | } | |
782 | ||
55d85293 VK |
783 | /* max_level is an index, not a counter */ |
784 | cpufreq_cdev->max_level = i - 1; | |
dcc6c7fd | 785 | |
55d85293 VK |
786 | cpufreq_cdev->freq_table = kmalloc(sizeof(*cpufreq_cdev->freq_table) * i, |
787 | GFP_KERNEL); | |
1dea432a | 788 | if (!cpufreq_cdev->freq_table) { |
04bdbdf9 | 789 | cdev = ERR_PTR(-ENOMEM); |
c36cf071 | 790 | goto free_time_in_idle_timestamp; |
f6859014 VK |
791 | } |
792 | ||
4d753aa7 | 793 | cpumask_copy(&cpufreq_cdev->allowed_cpus, policy->related_cpus); |
02361418 | 794 | |
c36cf071 | 795 | if (capacitance) { |
1dea432a | 796 | cpufreq_cdev->plat_get_static_power = plat_static_func; |
c36cf071 | 797 | |
1dea432a | 798 | ret = build_dyn_power_table(cpufreq_cdev, capacitance); |
c36cf071 | 799 | if (ret) { |
04bdbdf9 | 800 | cdev = ERR_PTR(ret); |
c36cf071 JM |
801 | goto free_table; |
802 | } | |
a305a438 BJ |
803 | |
804 | cooling_ops = &cpufreq_power_cooling_ops; | |
805 | } else { | |
806 | cooling_ops = &cpufreq_cooling_ops; | |
c36cf071 JM |
807 | } |
808 | ||
ae606089 MW |
809 | ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL); |
810 | if (ret < 0) { | |
04bdbdf9 | 811 | cdev = ERR_PTR(ret); |
eba4f88d | 812 | goto free_power_table; |
02361418 | 813 | } |
1dea432a | 814 | cpufreq_cdev->id = ret; |
02361418 | 815 | |
f6859014 | 816 | /* Fill freq-table in descending order of frequencies */ |
1dea432a | 817 | for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) { |
55d85293 | 818 | freq = find_next_max(policy->freq_table, freq); |
1dea432a | 819 | cpufreq_cdev->freq_table[i] = freq; |
f6859014 VK |
820 | |
821 | /* Warn for duplicate entries */ | |
822 | if (!freq) | |
823 | pr_warn("%s: table has duplicate entries\n", __func__); | |
824 | else | |
825 | pr_debug("%s: freq:%u KHz\n", __func__, freq); | |
02361418 | 826 | } |
f6859014 | 827 | |
f840ab18 | 828 | snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d", |
1dea432a | 829 | cpufreq_cdev->id); |
f840ab18 | 830 | |
04bdbdf9 VK |
831 | cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev, |
832 | cooling_ops); | |
833 | if (IS_ERR(cdev)) | |
ae606089 | 834 | goto remove_ida; |
f840ab18 | 835 | |
1dea432a | 836 | cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0]; |
04bdbdf9 | 837 | cpufreq_cdev->cdev = cdev; |
92e615ec | 838 | |
02373d7c | 839 | mutex_lock(&cooling_list_lock); |
088db931 | 840 | /* Register the notifier for first cpufreq cooling device */ |
1dea432a VK |
841 | first = list_empty(&cpufreq_cdev_list); |
842 | list_add(&cpufreq_cdev->node, &cpufreq_cdev_list); | |
088db931 | 843 | mutex_unlock(&cooling_list_lock); |
02373d7c | 844 | |
088db931 | 845 | if (first) |
02361418 | 846 | cpufreq_register_notifier(&thermal_cpufreq_notifier_block, |
5fda7f68 | 847 | CPUFREQ_POLICY_NOTIFIER); |
79491e53 | 848 | |
4d753aa7 | 849 | return cdev; |
730abe06 | 850 | |
ae606089 | 851 | remove_ida: |
1dea432a | 852 | ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); |
eba4f88d | 853 | free_power_table: |
1dea432a | 854 | kfree(cpufreq_cdev->dyn_power_table); |
f6859014 | 855 | free_table: |
1dea432a | 856 | kfree(cpufreq_cdev->freq_table); |
c36cf071 | 857 | free_time_in_idle_timestamp: |
1dea432a | 858 | kfree(cpufreq_cdev->time_in_idle_timestamp); |
c36cf071 | 859 | free_time_in_idle: |
1dea432a | 860 | kfree(cpufreq_cdev->time_in_idle); |
730abe06 | 861 | free_cdev: |
1dea432a | 862 | kfree(cpufreq_cdev); |
04bdbdf9 | 863 | return cdev; |
02361418 | 864 | } |
39d99cff EV |
865 | |
866 | /** | |
867 | * cpufreq_cooling_register - function to create cpufreq cooling device. | |
4d753aa7 | 868 | * @policy: cpufreq policy |
39d99cff EV |
869 | * |
870 | * This interface function registers the cpufreq cooling device with the name | |
871 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq | |
872 | * cooling devices. | |
873 | * | |
874 | * Return: a valid struct thermal_cooling_device pointer on success, | |
875 | * on failure, it returns a corresponding ERR_PTR(). | |
876 | */ | |
877 | struct thermal_cooling_device * | |
4d753aa7 | 878 | cpufreq_cooling_register(struct cpufreq_policy *policy) |
39d99cff | 879 | { |
4d753aa7 | 880 | return __cpufreq_cooling_register(NULL, policy, 0, NULL); |
39d99cff | 881 | } |
243dbd9c | 882 | EXPORT_SYMBOL_GPL(cpufreq_cooling_register); |
02361418 | 883 | |
39d99cff EV |
884 | /** |
885 | * of_cpufreq_cooling_register - function to create cpufreq cooling device. | |
886 | * @np: a valid struct device_node to the cooling device device tree node | |
4d753aa7 | 887 | * @policy: cpufreq policy |
39d99cff EV |
888 | * |
889 | * This interface function registers the cpufreq cooling device with the name | |
890 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq | |
891 | * cooling devices. Using this API, the cpufreq cooling device will be | |
892 | * linked to the device tree node provided. | |
893 | * | |
894 | * Return: a valid struct thermal_cooling_device pointer on success, | |
895 | * on failure, it returns a corresponding ERR_PTR(). | |
896 | */ | |
897 | struct thermal_cooling_device * | |
898 | of_cpufreq_cooling_register(struct device_node *np, | |
4d753aa7 | 899 | struct cpufreq_policy *policy) |
39d99cff EV |
900 | { |
901 | if (!np) | |
902 | return ERR_PTR(-EINVAL); | |
903 | ||
4d753aa7 | 904 | return __cpufreq_cooling_register(np, policy, 0, NULL); |
39d99cff EV |
905 | } |
906 | EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register); | |
907 | ||
c36cf071 JM |
908 | /** |
909 | * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions | |
4d753aa7 | 910 | * @policy: cpufreq policy |
c36cf071 JM |
911 | * @capacitance: dynamic power coefficient for these cpus |
912 | * @plat_static_func: function to calculate the static power consumed by these | |
913 | * cpus (optional) | |
914 | * | |
915 | * This interface function registers the cpufreq cooling device with | |
916 | * the name "thermal-cpufreq-%x". This api can support multiple | |
917 | * instances of cpufreq cooling devices. Using this function, the | |
918 | * cooling device will implement the power extensions by using a | |
919 | * simple cpu power model. The cpus must have registered their OPPs | |
920 | * using the OPP library. | |
921 | * | |
922 | * An optional @plat_static_func may be provided to calculate the | |
923 | * static power consumed by these cpus. If the platform's static | |
924 | * power consumption is unknown or negligible, make it NULL. | |
925 | * | |
926 | * Return: a valid struct thermal_cooling_device pointer on success, | |
927 | * on failure, it returns a corresponding ERR_PTR(). | |
928 | */ | |
929 | struct thermal_cooling_device * | |
4d753aa7 | 930 | cpufreq_power_cooling_register(struct cpufreq_policy *policy, u32 capacitance, |
c36cf071 JM |
931 | get_static_t plat_static_func) |
932 | { | |
4d753aa7 | 933 | return __cpufreq_cooling_register(NULL, policy, capacitance, |
c36cf071 JM |
934 | plat_static_func); |
935 | } | |
936 | EXPORT_SYMBOL(cpufreq_power_cooling_register); | |
937 | ||
938 | /** | |
939 | * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions | |
940 | * @np: a valid struct device_node to the cooling device device tree node | |
4d753aa7 | 941 | * @policy: cpufreq policy |
c36cf071 JM |
942 | * @capacitance: dynamic power coefficient for these cpus |
943 | * @plat_static_func: function to calculate the static power consumed by these | |
944 | * cpus (optional) | |
945 | * | |
946 | * This interface function registers the cpufreq cooling device with | |
947 | * the name "thermal-cpufreq-%x". This api can support multiple | |
948 | * instances of cpufreq cooling devices. Using this API, the cpufreq | |
949 | * cooling device will be linked to the device tree node provided. | |
950 | * Using this function, the cooling device will implement the power | |
951 | * extensions by using a simple cpu power model. The cpus must have | |
952 | * registered their OPPs using the OPP library. | |
953 | * | |
954 | * An optional @plat_static_func may be provided to calculate the | |
955 | * static power consumed by these cpus. If the platform's static | |
956 | * power consumption is unknown or negligible, make it NULL. | |
957 | * | |
958 | * Return: a valid struct thermal_cooling_device pointer on success, | |
959 | * on failure, it returns a corresponding ERR_PTR(). | |
960 | */ | |
961 | struct thermal_cooling_device * | |
962 | of_cpufreq_power_cooling_register(struct device_node *np, | |
4d753aa7 | 963 | struct cpufreq_policy *policy, |
c36cf071 JM |
964 | u32 capacitance, |
965 | get_static_t plat_static_func) | |
966 | { | |
967 | if (!np) | |
968 | return ERR_PTR(-EINVAL); | |
969 | ||
4d753aa7 | 970 | return __cpufreq_cooling_register(np, policy, capacitance, |
c36cf071 JM |
971 | plat_static_func); |
972 | } | |
973 | EXPORT_SYMBOL(of_cpufreq_power_cooling_register); | |
974 | ||
02361418 ADK |
975 | /** |
976 | * cpufreq_cooling_unregister - function to remove cpufreq cooling device. | |
977 | * @cdev: thermal cooling device pointer. | |
135266b4 EV |
978 | * |
979 | * This interface function unregisters the "thermal-cpufreq-%x" cooling device. | |
02361418 ADK |
980 | */ |
981 | void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) | |
982 | { | |
1dea432a | 983 | struct cpufreq_cooling_device *cpufreq_cdev; |
088db931 | 984 | bool last; |
02361418 | 985 | |
50e66c7e EV |
986 | if (!cdev) |
987 | return; | |
988 | ||
1dea432a | 989 | cpufreq_cdev = cdev->devdata; |
02361418 | 990 | |
ae606089 | 991 | mutex_lock(&cooling_list_lock); |
1dea432a | 992 | list_del(&cpufreq_cdev->node); |
02361418 | 993 | /* Unregister the notifier for the last cpufreq cooling device */ |
1dea432a | 994 | last = list_empty(&cpufreq_cdev_list); |
088db931 MW |
995 | mutex_unlock(&cooling_list_lock); |
996 | ||
997 | if (last) | |
02361418 | 998 | cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block, |
5fda7f68 | 999 | CPUFREQ_POLICY_NOTIFIER); |
02373d7c | 1000 | |
04bdbdf9 | 1001 | thermal_cooling_device_unregister(cpufreq_cdev->cdev); |
1dea432a VK |
1002 | ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); |
1003 | kfree(cpufreq_cdev->dyn_power_table); | |
1004 | kfree(cpufreq_cdev->time_in_idle_timestamp); | |
1005 | kfree(cpufreq_cdev->time_in_idle); | |
1006 | kfree(cpufreq_cdev->freq_table); | |
1007 | kfree(cpufreq_cdev); | |
02361418 | 1008 | } |
243dbd9c | 1009 | EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister); |