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