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