Commit | Line | Data |
---|---|---|
0fac9e2f | 1 | // SPDX-License-Identifier: GPL-2.0 |
02361418 ADK |
2 | /* |
3 | * linux/drivers/thermal/cpu_cooling.c | |
4 | * | |
5 | * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com) | |
02361418 | 6 | * |
42cd9b04 DL |
7 | * Copyright (C) 2012-2018 Linaro Limited. |
8 | * | |
9 | * Authors: Amit Daniel <amit.kachhap@linaro.org> | |
10 | * Viresh Kumar <viresh.kumar@linaro.org> | |
73904cbc | 11 | * |
02361418 | 12 | */ |
02361418 ADK |
13 | #include <linux/module.h> |
14 | #include <linux/thermal.h> | |
02361418 ADK |
15 | #include <linux/cpufreq.h> |
16 | #include <linux/err.h> | |
ae606089 | 17 | #include <linux/idr.h> |
c36cf071 | 18 | #include <linux/pm_opp.h> |
02361418 ADK |
19 | #include <linux/slab.h> |
20 | #include <linux/cpu.h> | |
21 | #include <linux/cpu_cooling.h> | |
22 | ||
6828a471 JM |
23 | #include <trace/events/thermal.h> |
24 | ||
07d888d8 VK |
25 | /* |
26 | * Cooling state <-> CPUFreq frequency | |
27 | * | |
28 | * Cooling states are translated to frequencies throughout this driver and this | |
29 | * is the relation between them. | |
30 | * | |
31 | * Highest cooling state corresponds to lowest possible frequency. | |
32 | * | |
33 | * i.e. | |
34 | * level 0 --> 1st Max Freq | |
35 | * level 1 --> 2nd Max Freq | |
36 | * ... | |
37 | */ | |
38 | ||
c36cf071 | 39 | /** |
349d39dc | 40 | * struct freq_table - frequency table along with power entries |
c36cf071 JM |
41 | * @frequency: frequency in KHz |
42 | * @power: power in mW | |
43 | * | |
44 | * This structure is built when the cooling device registers and helps | |
349d39dc | 45 | * in translating frequency to power and vice versa. |
c36cf071 | 46 | */ |
349d39dc | 47 | struct freq_table { |
c36cf071 JM |
48 | u32 frequency; |
49 | u32 power; | |
50 | }; | |
51 | ||
81ee14da VK |
52 | /** |
53 | * struct time_in_idle - Idle time stats | |
54 | * @time: previous reading of the absolute time that this cpu was idle | |
55 | * @timestamp: wall time of the last invocation of get_cpu_idle_time_us() | |
56 | */ | |
57 | struct time_in_idle { | |
58 | u64 time; | |
59 | u64 timestamp; | |
60 | }; | |
61 | ||
02361418 | 62 | /** |
3b3c0748 | 63 | * struct cpufreq_cooling_device - data for cooling device with cpufreq |
02361418 ADK |
64 | * @id: unique integer value corresponding to each cpufreq_cooling_device |
65 | * registered. | |
d72b4015 | 66 | * @last_load: load measured by the latest call to cpufreq_get_requested_power() |
02361418 ADK |
67 | * @cpufreq_state: integer value representing the current state of cpufreq |
68 | * cooling devices. | |
59f0d218 | 69 | * @clipped_freq: integer value representing the absolute value of the clipped |
02361418 | 70 | * frequency. |
dcc6c7fd VK |
71 | * @max_level: maximum cooling level. One less than total number of valid |
72 | * cpufreq frequencies. | |
d72b4015 VK |
73 | * @freq_table: Freq table in descending order of frequencies |
74 | * @cdev: thermal_cooling_device pointer to keep track of the | |
75 | * registered cooling device. | |
76 | * @policy: cpufreq policy. | |
fc4de356 | 77 | * @node: list_head to link all cpufreq_cooling_device together. |
81ee14da | 78 | * @idle_time: idle time stats |
02361418 | 79 | * |
beca6053 VK |
80 | * This structure is required for keeping information of each registered |
81 | * cpufreq_cooling_device. | |
02361418 ADK |
82 | */ |
83 | struct cpufreq_cooling_device { | |
84 | int id; | |
d72b4015 | 85 | u32 last_load; |
02361418 | 86 | unsigned int cpufreq_state; |
59f0d218 | 87 | unsigned int clipped_freq; |
dcc6c7fd | 88 | unsigned int max_level; |
349d39dc | 89 | struct freq_table *freq_table; /* In descending order */ |
d72b4015 | 90 | struct cpufreq_policy *policy; |
2dcd851f | 91 | struct list_head node; |
81ee14da | 92 | struct time_in_idle *idle_time; |
02361418 | 93 | }; |
02361418 | 94 | |
fb8ea308 | 95 | static DEFINE_IDA(cpufreq_ida); |
02373d7c | 96 | static DEFINE_MUTEX(cooling_list_lock); |
1dea432a | 97 | static LIST_HEAD(cpufreq_cdev_list); |
02361418 | 98 | |
02361418 ADK |
99 | /* Below code defines functions to be used for cpufreq as cooling device */ |
100 | ||
101 | /** | |
4843c4a1 | 102 | * get_level: Find the level for a particular frequency |
1dea432a | 103 | * @cpufreq_cdev: cpufreq_cdev for which the property is required |
4843c4a1 | 104 | * @freq: Frequency |
82b9ee40 | 105 | * |
da27f69d | 106 | * Return: level corresponding to the frequency. |
02361418 | 107 | */ |
1dea432a | 108 | static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev, |
4843c4a1 | 109 | unsigned int freq) |
02361418 | 110 | { |
da27f69d | 111 | struct freq_table *freq_table = cpufreq_cdev->freq_table; |
4843c4a1 | 112 | unsigned long level; |
a116776f | 113 | |
da27f69d VK |
114 | for (level = 1; level <= cpufreq_cdev->max_level; level++) |
115 | if (freq > freq_table[level].frequency) | |
4843c4a1 | 116 | break; |
02361418 | 117 | |
da27f69d | 118 | return level - 1; |
fc35b35c ZR |
119 | } |
120 | ||
02361418 ADK |
121 | /** |
122 | * cpufreq_thermal_notifier - notifier callback for cpufreq policy change. | |
123 | * @nb: struct notifier_block * with callback info. | |
124 | * @event: value showing cpufreq event for which this function invoked. | |
125 | * @data: callback-specific data | |
bab30554 | 126 | * |
9746b6e7 | 127 | * Callback to hijack the notification on cpufreq policy transition. |
bab30554 EV |
128 | * Every time there is a change in policy, we will intercept and |
129 | * update the cpufreq policy with thermal constraints. | |
130 | * | |
131 | * Return: 0 (success) | |
02361418 ADK |
132 | */ |
133 | static int cpufreq_thermal_notifier(struct notifier_block *nb, | |
5fda7f68 | 134 | unsigned long event, void *data) |
02361418 ADK |
135 | { |
136 | struct cpufreq_policy *policy = data; | |
abcbcc25 | 137 | unsigned long clipped_freq; |
1dea432a | 138 | struct cpufreq_cooling_device *cpufreq_cdev; |
02361418 | 139 | |
a24af233 VK |
140 | if (event != CPUFREQ_ADJUST) |
141 | return NOTIFY_DONE; | |
02361418 | 142 | |
a24af233 | 143 | mutex_lock(&cooling_list_lock); |
1dea432a | 144 | list_for_each_entry(cpufreq_cdev, &cpufreq_cdev_list, node) { |
ba76dd9d VK |
145 | /* |
146 | * A new copy of the policy is sent to the notifier and can't | |
147 | * compare that directly. | |
148 | */ | |
149 | if (policy->cpu != cpufreq_cdev->policy->cpu) | |
a24af233 | 150 | continue; |
c36cf071 | 151 | |
1afb9c53 VK |
152 | /* |
153 | * policy->max is the maximum allowed frequency defined by user | |
154 | * and clipped_freq is the maximum that thermal constraints | |
155 | * allow. | |
156 | * | |
157 | * If clipped_freq is lower than policy->max, then we need to | |
158 | * readjust policy->max. | |
159 | * | |
160 | * But, if clipped_freq is greater than policy->max, we don't | |
161 | * need to do anything. | |
162 | */ | |
1dea432a | 163 | clipped_freq = cpufreq_cdev->clipped_freq; |
c36cf071 | 164 | |
1afb9c53 | 165 | if (policy->max > clipped_freq) |
abcbcc25 | 166 | cpufreq_verify_within_limits(policy, 0, clipped_freq); |
c36cf071 | 167 | break; |
c36cf071 | 168 | } |
a24af233 | 169 | mutex_unlock(&cooling_list_lock); |
c36cf071 JM |
170 | |
171 | return NOTIFY_OK; | |
172 | } | |
173 | ||
174 | /** | |
349d39dc VK |
175 | * update_freq_table() - Update the freq table with power numbers |
176 | * @cpufreq_cdev: the cpufreq cooling device in which to update the table | |
c36cf071 JM |
177 | * @capacitance: dynamic power coefficient for these cpus |
178 | * | |
349d39dc VK |
179 | * Update the freq table with power numbers. This table will be used in |
180 | * cpu_power_to_freq() and cpu_freq_to_power() to convert between power and | |
181 | * frequency efficiently. Power is stored in mW, frequency in KHz. The | |
182 | * resulting table is in descending order. | |
c36cf071 | 183 | * |
459ac375 | 184 | * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs, |
349d39dc | 185 | * or -ENOMEM if we run out of memory. |
c36cf071 | 186 | */ |
349d39dc VK |
187 | static int update_freq_table(struct cpufreq_cooling_device *cpufreq_cdev, |
188 | u32 capacitance) | |
c36cf071 | 189 | { |
349d39dc | 190 | struct freq_table *freq_table = cpufreq_cdev->freq_table; |
c36cf071 JM |
191 | struct dev_pm_opp *opp; |
192 | struct device *dev = NULL; | |
349d39dc | 193 | int num_opps = 0, cpu = cpufreq_cdev->policy->cpu, i; |
c36cf071 | 194 | |
02bacb21 VK |
195 | dev = get_cpu_device(cpu); |
196 | if (unlikely(!dev)) { | |
72554a75 | 197 | pr_warn("No cpu device for cpu %d\n", cpu); |
02bacb21 | 198 | return -ENODEV; |
c36cf071 | 199 | } |
02361418 | 200 | |
02bacb21 VK |
201 | num_opps = dev_pm_opp_get_opp_count(dev); |
202 | if (num_opps < 0) | |
203 | return num_opps; | |
204 | ||
349d39dc VK |
205 | /* |
206 | * The cpufreq table is also built from the OPP table and so the count | |
207 | * should match. | |
208 | */ | |
209 | if (num_opps != cpufreq_cdev->max_level + 1) { | |
210 | dev_warn(dev, "Number of OPPs not matching with max_levels\n"); | |
459ac375 | 211 | return -EINVAL; |
349d39dc | 212 | } |
02361418 | 213 | |
349d39dc VK |
214 | for (i = 0; i <= cpufreq_cdev->max_level; i++) { |
215 | unsigned long freq = freq_table[i].frequency * 1000; | |
216 | u32 freq_mhz = freq_table[i].frequency / 1000; | |
c36cf071 | 217 | u64 power; |
349d39dc | 218 | u32 voltage_mv; |
c36cf071 | 219 | |
349d39dc VK |
220 | /* |
221 | * Find ceil frequency as 'freq' may be slightly lower than OPP | |
222 | * freq due to truncation while converting to kHz. | |
223 | */ | |
224 | opp = dev_pm_opp_find_freq_ceil(dev, &freq); | |
225 | if (IS_ERR(opp)) { | |
226 | dev_err(dev, "failed to get opp for %lu frequency\n", | |
227 | freq); | |
228 | return -EINVAL; | |
459ac375 JM |
229 | } |
230 | ||
c36cf071 | 231 | voltage_mv = dev_pm_opp_get_voltage(opp) / 1000; |
8a31d9d9 | 232 | dev_pm_opp_put(opp); |
c36cf071 JM |
233 | |
234 | /* | |
235 | * Do the multiplication with MHz and millivolt so as | |
236 | * to not overflow. | |
237 | */ | |
238 | power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv; | |
239 | do_div(power, 1000000000); | |
240 | ||
c36cf071 | 241 | /* power is stored in mW */ |
349d39dc | 242 | freq_table[i].power = power; |
eba4f88d | 243 | } |
c36cf071 | 244 | |
459ac375 | 245 | return 0; |
c36cf071 JM |
246 | } |
247 | ||
1dea432a | 248 | static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev, |
c36cf071 JM |
249 | u32 freq) |
250 | { | |
251 | int i; | |
349d39dc | 252 | struct freq_table *freq_table = cpufreq_cdev->freq_table; |
c36cf071 | 253 | |
349d39dc VK |
254 | for (i = 1; i <= cpufreq_cdev->max_level; i++) |
255 | if (freq > freq_table[i].frequency) | |
c36cf071 JM |
256 | break; |
257 | ||
349d39dc | 258 | return freq_table[i - 1].power; |
c36cf071 JM |
259 | } |
260 | ||
1dea432a | 261 | static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev, |
c36cf071 JM |
262 | u32 power) |
263 | { | |
264 | int i; | |
349d39dc | 265 | struct freq_table *freq_table = cpufreq_cdev->freq_table; |
c36cf071 | 266 | |
349d39dc VK |
267 | for (i = 1; i <= cpufreq_cdev->max_level; i++) |
268 | if (power > freq_table[i].power) | |
c36cf071 JM |
269 | break; |
270 | ||
349d39dc | 271 | return freq_table[i - 1].frequency; |
c36cf071 JM |
272 | } |
273 | ||
274 | /** | |
275 | * get_load() - get load for a cpu since last updated | |
1dea432a | 276 | * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu |
c36cf071 | 277 | * @cpu: cpu number |
ba76dd9d | 278 | * @cpu_idx: index of the cpu in time_in_idle* |
c36cf071 JM |
279 | * |
280 | * Return: The average load of cpu @cpu in percentage since this | |
281 | * function was last called. | |
282 | */ | |
1dea432a | 283 | static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu, |
a53b8394 | 284 | int cpu_idx) |
c36cf071 JM |
285 | { |
286 | u32 load; | |
287 | u64 now, now_idle, delta_time, delta_idle; | |
81ee14da | 288 | struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx]; |
c36cf071 JM |
289 | |
290 | now_idle = get_cpu_idle_time(cpu, &now, 0); | |
81ee14da VK |
291 | delta_idle = now_idle - idle_time->time; |
292 | delta_time = now - idle_time->timestamp; | |
c36cf071 JM |
293 | |
294 | if (delta_time <= delta_idle) | |
295 | load = 0; | |
296 | else | |
297 | load = div64_u64(100 * (delta_time - delta_idle), delta_time); | |
298 | ||
81ee14da VK |
299 | idle_time->time = now_idle; |
300 | idle_time->timestamp = now; | |
c36cf071 JM |
301 | |
302 | return load; | |
303 | } | |
304 | ||
c36cf071 JM |
305 | /** |
306 | * get_dynamic_power() - calculate the dynamic power | |
1dea432a | 307 | * @cpufreq_cdev: &cpufreq_cooling_device for this cdev |
c36cf071 JM |
308 | * @freq: current frequency |
309 | * | |
310 | * Return: the dynamic power consumed by the cpus described by | |
1dea432a | 311 | * @cpufreq_cdev. |
c36cf071 | 312 | */ |
1dea432a | 313 | static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev, |
c36cf071 JM |
314 | unsigned long freq) |
315 | { | |
316 | u32 raw_cpu_power; | |
317 | ||
1dea432a VK |
318 | raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq); |
319 | return (raw_cpu_power * cpufreq_cdev->last_load) / 100; | |
02361418 ADK |
320 | } |
321 | ||
1b9e3526 | 322 | /* cpufreq cooling device callback functions are defined below */ |
02361418 ADK |
323 | |
324 | /** | |
325 | * cpufreq_get_max_state - callback function to get the max cooling state. | |
326 | * @cdev: thermal cooling device pointer. | |
327 | * @state: fill this variable with the max cooling state. | |
62c00421 EV |
328 | * |
329 | * Callback for the thermal cooling device to return the cpufreq | |
330 | * max cooling state. | |
331 | * | |
332 | * Return: 0 on success, an error code otherwise. | |
02361418 ADK |
333 | */ |
334 | static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, | |
335 | unsigned long *state) | |
336 | { | |
1dea432a | 337 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
9c51b05a | 338 | |
1dea432a | 339 | *state = cpufreq_cdev->max_level; |
dcc6c7fd | 340 | return 0; |
02361418 ADK |
341 | } |
342 | ||
343 | /** | |
344 | * cpufreq_get_cur_state - callback function to get the current cooling state. | |
345 | * @cdev: thermal cooling device pointer. | |
346 | * @state: fill this variable with the current cooling state. | |
3672552d EV |
347 | * |
348 | * Callback for the thermal cooling device to return the cpufreq | |
349 | * current cooling state. | |
350 | * | |
351 | * Return: 0 on success, an error code otherwise. | |
02361418 ADK |
352 | */ |
353 | static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, | |
354 | unsigned long *state) | |
355 | { | |
1dea432a | 356 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
02361418 | 357 | |
1dea432a | 358 | *state = cpufreq_cdev->cpufreq_state; |
79491e53 | 359 | |
160b7d80 | 360 | return 0; |
02361418 ADK |
361 | } |
362 | ||
363 | /** | |
364 | * cpufreq_set_cur_state - callback function to set the current cooling state. | |
365 | * @cdev: thermal cooling device pointer. | |
366 | * @state: set this variable to the current cooling state. | |
56e05fdb EV |
367 | * |
368 | * Callback for the thermal cooling device to change the cpufreq | |
369 | * current cooling state. | |
370 | * | |
371 | * Return: 0 on success, an error code otherwise. | |
02361418 ADK |
372 | */ |
373 | static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, | |
374 | unsigned long state) | |
375 | { | |
1dea432a | 376 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
5194fe46 | 377 | unsigned int clip_freq; |
4843c4a1 VK |
378 | |
379 | /* Request state should be less than max_level */ | |
1dea432a | 380 | if (WARN_ON(state > cpufreq_cdev->max_level)) |
4843c4a1 | 381 | return -EINVAL; |
5194fe46 VK |
382 | |
383 | /* Check if the old cooling action is same as new cooling action */ | |
1dea432a | 384 | if (cpufreq_cdev->cpufreq_state == state) |
5194fe46 | 385 | return 0; |
02361418 | 386 | |
349d39dc | 387 | clip_freq = cpufreq_cdev->freq_table[state].frequency; |
1dea432a VK |
388 | cpufreq_cdev->cpufreq_state = state; |
389 | cpufreq_cdev->clipped_freq = clip_freq; | |
5194fe46 | 390 | |
ba76dd9d | 391 | cpufreq_update_policy(cpufreq_cdev->policy->cpu); |
5194fe46 VK |
392 | |
393 | return 0; | |
02361418 ADK |
394 | } |
395 | ||
c36cf071 JM |
396 | /** |
397 | * cpufreq_get_requested_power() - get the current power | |
398 | * @cdev: &thermal_cooling_device pointer | |
399 | * @tz: a valid thermal zone device pointer | |
400 | * @power: pointer in which to store the resulting power | |
401 | * | |
402 | * Calculate the current power consumption of the cpus in milliwatts | |
403 | * and store it in @power. This function should actually calculate | |
404 | * the requested power, but it's hard to get the frequency that | |
405 | * cpufreq would have assigned if there were no thermal limits. | |
406 | * Instead, we calculate the current power on the assumption that the | |
407 | * immediate future will look like the immediate past. | |
408 | * | |
409 | * We use the current frequency and the average load since this | |
410 | * function was last called. In reality, there could have been | |
411 | * multiple opps since this function was last called and that affects | |
412 | * the load calculation. While it's not perfectly accurate, this | |
413 | * simplification is good enough and works. REVISIT this, as more | |
414 | * complex code may be needed if experiments show that it's not | |
415 | * accurate enough. | |
416 | * | |
417 | * Return: 0 on success, -E* if getting the static power failed. | |
418 | */ | |
419 | static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev, | |
420 | struct thermal_zone_device *tz, | |
421 | u32 *power) | |
422 | { | |
423 | unsigned long freq; | |
84fe2cab VK |
424 | int i = 0, cpu; |
425 | u32 total_load = 0; | |
1dea432a | 426 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
ba76dd9d | 427 | struct cpufreq_policy *policy = cpufreq_cdev->policy; |
6828a471 | 428 | u32 *load_cpu = NULL; |
c36cf071 | 429 | |
ba76dd9d | 430 | freq = cpufreq_quick_get(policy->cpu); |
c36cf071 | 431 | |
6828a471 | 432 | if (trace_thermal_power_cpu_get_power_enabled()) { |
ba76dd9d | 433 | u32 ncpus = cpumask_weight(policy->related_cpus); |
6828a471 | 434 | |
a71544cd | 435 | load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL); |
6828a471 JM |
436 | } |
437 | ||
ba76dd9d | 438 | for_each_cpu(cpu, policy->related_cpus) { |
c36cf071 JM |
439 | u32 load; |
440 | ||
441 | if (cpu_online(cpu)) | |
1dea432a | 442 | load = get_load(cpufreq_cdev, cpu, i); |
c36cf071 JM |
443 | else |
444 | load = 0; | |
445 | ||
446 | total_load += load; | |
bf45ac18 | 447 | if (load_cpu) |
6828a471 JM |
448 | load_cpu[i] = load; |
449 | ||
450 | i++; | |
c36cf071 JM |
451 | } |
452 | ||
1dea432a | 453 | cpufreq_cdev->last_load = total_load; |
c36cf071 | 454 | |
84fe2cab | 455 | *power = get_dynamic_power(cpufreq_cdev, freq); |
6828a471 JM |
456 | |
457 | if (load_cpu) { | |
ba76dd9d | 458 | trace_thermal_power_cpu_get_power(policy->related_cpus, freq, |
84fe2cab | 459 | load_cpu, i, *power); |
6828a471 | 460 | |
a71544cd | 461 | kfree(load_cpu); |
6828a471 | 462 | } |
c36cf071 | 463 | |
c36cf071 JM |
464 | return 0; |
465 | } | |
466 | ||
467 | /** | |
468 | * cpufreq_state2power() - convert a cpu cdev state to power consumed | |
469 | * @cdev: &thermal_cooling_device pointer | |
470 | * @tz: a valid thermal zone device pointer | |
471 | * @state: cooling device state to be converted | |
472 | * @power: pointer in which to store the resulting power | |
473 | * | |
474 | * Convert cooling device state @state into power consumption in | |
475 | * milliwatts assuming 100% load. Store the calculated power in | |
476 | * @power. | |
477 | * | |
478 | * Return: 0 on success, -EINVAL if the cooling device state could not | |
479 | * be converted into a frequency or other -E* if there was an error | |
480 | * when calculating the static power. | |
481 | */ | |
482 | static int cpufreq_state2power(struct thermal_cooling_device *cdev, | |
483 | struct thermal_zone_device *tz, | |
484 | unsigned long state, u32 *power) | |
485 | { | |
486 | unsigned int freq, num_cpus; | |
1dea432a | 487 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
c36cf071 | 488 | |
cb1b6318 VK |
489 | /* Request state should be less than max_level */ |
490 | if (WARN_ON(state > cpufreq_cdev->max_level)) | |
491 | return -EINVAL; | |
492 | ||
ba76dd9d | 493 | num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus); |
c36cf071 | 494 | |
349d39dc | 495 | freq = cpufreq_cdev->freq_table[state].frequency; |
84fe2cab | 496 | *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus; |
c36cf071 | 497 | |
84fe2cab | 498 | return 0; |
c36cf071 JM |
499 | } |
500 | ||
501 | /** | |
502 | * cpufreq_power2state() - convert power to a cooling device state | |
503 | * @cdev: &thermal_cooling_device pointer | |
504 | * @tz: a valid thermal zone device pointer | |
505 | * @power: power in milliwatts to be converted | |
506 | * @state: pointer in which to store the resulting state | |
507 | * | |
508 | * Calculate a cooling device state for the cpus described by @cdev | |
509 | * that would allow them to consume at most @power mW and store it in | |
510 | * @state. Note that this calculation depends on external factors | |
511 | * such as the cpu load or the current static power. Calling this | |
512 | * function with the same power as input can yield different cooling | |
513 | * device states depending on those external factors. | |
514 | * | |
515 | * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if | |
516 | * the calculated frequency could not be converted to a valid state. | |
517 | * The latter should not happen unless the frequencies available to | |
518 | * cpufreq have changed since the initialization of the cpu cooling | |
519 | * device. | |
520 | */ | |
521 | static int cpufreq_power2state(struct thermal_cooling_device *cdev, | |
522 | struct thermal_zone_device *tz, u32 power, | |
523 | unsigned long *state) | |
524 | { | |
e0fda737 | 525 | unsigned int target_freq; |
84fe2cab | 526 | u32 last_load, normalised_power; |
1dea432a | 527 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
ba76dd9d | 528 | struct cpufreq_policy *policy = cpufreq_cdev->policy; |
c36cf071 | 529 | |
1dea432a | 530 | last_load = cpufreq_cdev->last_load ?: 1; |
84fe2cab | 531 | normalised_power = (power * 100) / last_load; |
1dea432a | 532 | target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power); |
c36cf071 | 533 | |
3e08b2df | 534 | *state = get_level(cpufreq_cdev, target_freq); |
ba76dd9d VK |
535 | trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state, |
536 | power); | |
c36cf071 JM |
537 | return 0; |
538 | } | |
539 | ||
02361418 | 540 | /* Bind cpufreq callbacks to thermal cooling device ops */ |
a305a438 | 541 | |
c36cf071 | 542 | static struct thermal_cooling_device_ops cpufreq_cooling_ops = { |
02361418 ADK |
543 | .get_max_state = cpufreq_get_max_state, |
544 | .get_cur_state = cpufreq_get_cur_state, | |
545 | .set_cur_state = cpufreq_set_cur_state, | |
546 | }; | |
547 | ||
a305a438 BJ |
548 | static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = { |
549 | .get_max_state = cpufreq_get_max_state, | |
550 | .get_cur_state = cpufreq_get_cur_state, | |
551 | .set_cur_state = cpufreq_set_cur_state, | |
552 | .get_requested_power = cpufreq_get_requested_power, | |
553 | .state2power = cpufreq_state2power, | |
554 | .power2state = cpufreq_power2state, | |
555 | }; | |
556 | ||
02361418 ADK |
557 | /* Notifier for cpufreq policy change */ |
558 | static struct notifier_block thermal_cpufreq_notifier_block = { | |
559 | .notifier_call = cpufreq_thermal_notifier, | |
560 | }; | |
561 | ||
f6859014 VK |
562 | static unsigned int find_next_max(struct cpufreq_frequency_table *table, |
563 | unsigned int prev_max) | |
564 | { | |
565 | struct cpufreq_frequency_table *pos; | |
566 | unsigned int max = 0; | |
567 | ||
568 | cpufreq_for_each_valid_entry(pos, table) { | |
569 | if (pos->frequency > max && pos->frequency < prev_max) | |
570 | max = pos->frequency; | |
571 | } | |
572 | ||
573 | return max; | |
574 | } | |
575 | ||
02361418 | 576 | /** |
39d99cff EV |
577 | * __cpufreq_cooling_register - helper function to create cpufreq cooling device |
578 | * @np: a valid struct device_node to the cooling device device tree node | |
4d753aa7 | 579 | * @policy: cpufreq policy |
405fb825 | 580 | * Normally this should be same as cpufreq policy->related_cpus. |
c36cf071 | 581 | * @capacitance: dynamic power coefficient for these cpus |
12cb08ba EV |
582 | * |
583 | * This interface function registers the cpufreq cooling device with the name | |
584 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq | |
39d99cff EV |
585 | * cooling devices. It also gives the opportunity to link the cooling device |
586 | * with a device tree node, in order to bind it via the thermal DT code. | |
12cb08ba EV |
587 | * |
588 | * Return: a valid struct thermal_cooling_device pointer on success, | |
589 | * on failure, it returns a corresponding ERR_PTR(). | |
02361418 | 590 | */ |
39d99cff EV |
591 | static struct thermal_cooling_device * |
592 | __cpufreq_cooling_register(struct device_node *np, | |
84fe2cab | 593 | struct cpufreq_policy *policy, u32 capacitance) |
02361418 | 594 | { |
04bdbdf9 | 595 | struct thermal_cooling_device *cdev; |
1dea432a | 596 | struct cpufreq_cooling_device *cpufreq_cdev; |
02361418 | 597 | char dev_name[THERMAL_NAME_LENGTH]; |
c36cf071 | 598 | unsigned int freq, i, num_cpus; |
405fb825 | 599 | int ret; |
a305a438 | 600 | struct thermal_cooling_device_ops *cooling_ops; |
088db931 | 601 | bool first; |
02361418 | 602 | |
4d753aa7 | 603 | if (IS_ERR_OR_NULL(policy)) { |
b2fd708f | 604 | pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy); |
4d753aa7 | 605 | return ERR_PTR(-EINVAL); |
f8bfc116 VK |
606 | } |
607 | ||
55d85293 VK |
608 | i = cpufreq_table_count_valid_entries(policy); |
609 | if (!i) { | |
610 | pr_debug("%s: CPUFreq table not found or has no valid entries\n", | |
611 | __func__); | |
4d753aa7 | 612 | return ERR_PTR(-ENODEV); |
02361418 | 613 | } |
0f1be51c | 614 | |
1dea432a | 615 | cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL); |
4d753aa7 VK |
616 | if (!cpufreq_cdev) |
617 | return ERR_PTR(-ENOMEM); | |
02361418 | 618 | |
b12b6519 | 619 | cpufreq_cdev->policy = policy; |
4d753aa7 | 620 | num_cpus = cpumask_weight(policy->related_cpus); |
81ee14da VK |
621 | cpufreq_cdev->idle_time = kcalloc(num_cpus, |
622 | sizeof(*cpufreq_cdev->idle_time), | |
623 | GFP_KERNEL); | |
624 | if (!cpufreq_cdev->idle_time) { | |
04bdbdf9 | 625 | cdev = ERR_PTR(-ENOMEM); |
c36cf071 JM |
626 | goto free_cdev; |
627 | } | |
628 | ||
55d85293 VK |
629 | /* max_level is an index, not a counter */ |
630 | cpufreq_cdev->max_level = i - 1; | |
dcc6c7fd | 631 | |
f19b1a17 VK |
632 | cpufreq_cdev->freq_table = kmalloc_array(i, |
633 | sizeof(*cpufreq_cdev->freq_table), | |
634 | GFP_KERNEL); | |
1dea432a | 635 | if (!cpufreq_cdev->freq_table) { |
04bdbdf9 | 636 | cdev = ERR_PTR(-ENOMEM); |
81ee14da | 637 | goto free_idle_time; |
f6859014 VK |
638 | } |
639 | ||
ae606089 MW |
640 | ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL); |
641 | if (ret < 0) { | |
04bdbdf9 | 642 | cdev = ERR_PTR(ret); |
349d39dc | 643 | goto free_table; |
02361418 | 644 | } |
1dea432a | 645 | cpufreq_cdev->id = ret; |
02361418 | 646 | |
349d39dc VK |
647 | snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d", |
648 | cpufreq_cdev->id); | |
649 | ||
f6859014 | 650 | /* Fill freq-table in descending order of frequencies */ |
1dea432a | 651 | for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) { |
55d85293 | 652 | freq = find_next_max(policy->freq_table, freq); |
349d39dc | 653 | cpufreq_cdev->freq_table[i].frequency = freq; |
f6859014 VK |
654 | |
655 | /* Warn for duplicate entries */ | |
656 | if (!freq) | |
657 | pr_warn("%s: table has duplicate entries\n", __func__); | |
658 | else | |
659 | pr_debug("%s: freq:%u KHz\n", __func__, freq); | |
02361418 | 660 | } |
f6859014 | 661 | |
349d39dc | 662 | if (capacitance) { |
349d39dc VK |
663 | ret = update_freq_table(cpufreq_cdev, capacitance); |
664 | if (ret) { | |
665 | cdev = ERR_PTR(ret); | |
666 | goto remove_ida; | |
667 | } | |
668 | ||
669 | cooling_ops = &cpufreq_power_cooling_ops; | |
670 | } else { | |
671 | cooling_ops = &cpufreq_cooling_ops; | |
672 | } | |
f840ab18 | 673 | |
04bdbdf9 VK |
674 | cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev, |
675 | cooling_ops); | |
676 | if (IS_ERR(cdev)) | |
ae606089 | 677 | goto remove_ida; |
f840ab18 | 678 | |
349d39dc | 679 | cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0].frequency; |
92e615ec | 680 | |
02373d7c | 681 | mutex_lock(&cooling_list_lock); |
088db931 | 682 | /* Register the notifier for first cpufreq cooling device */ |
1dea432a VK |
683 | first = list_empty(&cpufreq_cdev_list); |
684 | list_add(&cpufreq_cdev->node, &cpufreq_cdev_list); | |
088db931 | 685 | mutex_unlock(&cooling_list_lock); |
02373d7c | 686 | |
088db931 | 687 | if (first) |
02361418 | 688 | cpufreq_register_notifier(&thermal_cpufreq_notifier_block, |
5fda7f68 | 689 | CPUFREQ_POLICY_NOTIFIER); |
79491e53 | 690 | |
4d753aa7 | 691 | return cdev; |
730abe06 | 692 | |
ae606089 | 693 | remove_ida: |
1dea432a | 694 | ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); |
f6859014 | 695 | free_table: |
1dea432a | 696 | kfree(cpufreq_cdev->freq_table); |
81ee14da VK |
697 | free_idle_time: |
698 | kfree(cpufreq_cdev->idle_time); | |
730abe06 | 699 | free_cdev: |
1dea432a | 700 | kfree(cpufreq_cdev); |
04bdbdf9 | 701 | return cdev; |
02361418 | 702 | } |
39d99cff EV |
703 | |
704 | /** | |
705 | * cpufreq_cooling_register - function to create cpufreq cooling device. | |
4d753aa7 | 706 | * @policy: cpufreq policy |
39d99cff EV |
707 | * |
708 | * This interface function registers the cpufreq cooling device with the name | |
709 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq | |
710 | * cooling devices. | |
711 | * | |
712 | * Return: a valid struct thermal_cooling_device pointer on success, | |
713 | * on failure, it returns a corresponding ERR_PTR(). | |
714 | */ | |
715 | struct thermal_cooling_device * | |
4d753aa7 | 716 | cpufreq_cooling_register(struct cpufreq_policy *policy) |
39d99cff | 717 | { |
84fe2cab | 718 | return __cpufreq_cooling_register(NULL, policy, 0); |
39d99cff | 719 | } |
243dbd9c | 720 | EXPORT_SYMBOL_GPL(cpufreq_cooling_register); |
02361418 | 721 | |
39d99cff EV |
722 | /** |
723 | * of_cpufreq_cooling_register - function to create cpufreq cooling device. | |
4d753aa7 | 724 | * @policy: cpufreq policy |
39d99cff 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 | |
728 | * cooling devices. Using this API, the cpufreq cooling device will be | |
729 | * linked to the device tree node provided. | |
730 | * | |
c36cf071 JM |
731 | * Using this function, the cooling device will implement the power |
732 | * extensions by using a simple cpu power model. The cpus must have | |
733 | * registered their OPPs using the OPP library. | |
734 | * | |
f5f263fe VK |
735 | * It also takes into account, if property present in policy CPU node, the |
736 | * static power consumed by the cpu. | |
c36cf071 JM |
737 | * |
738 | * Return: a valid struct thermal_cooling_device pointer on success, | |
f5f263fe | 739 | * and NULL on failure. |
c36cf071 JM |
740 | */ |
741 | struct thermal_cooling_device * | |
3ebb62ff | 742 | of_cpufreq_cooling_register(struct cpufreq_policy *policy) |
c36cf071 | 743 | { |
f5f263fe VK |
744 | struct device_node *np = of_get_cpu_node(policy->cpu, NULL); |
745 | struct thermal_cooling_device *cdev = NULL; | |
746 | u32 capacitance = 0; | |
747 | ||
748 | if (!np) { | |
749 | pr_err("cpu_cooling: OF node not available for cpu%d\n", | |
750 | policy->cpu); | |
751 | return NULL; | |
752 | } | |
c36cf071 | 753 | |
f5f263fe VK |
754 | if (of_find_property(np, "#cooling-cells", NULL)) { |
755 | of_property_read_u32(np, "dynamic-power-coefficient", | |
756 | &capacitance); | |
757 | ||
84fe2cab | 758 | cdev = __cpufreq_cooling_register(np, policy, capacitance); |
f5f263fe | 759 | if (IS_ERR(cdev)) { |
bf78f133 | 760 | pr_err("cpu_cooling: cpu%d failed to register as cooling device: %ld\n", |
f5f263fe VK |
761 | policy->cpu, PTR_ERR(cdev)); |
762 | cdev = NULL; | |
763 | } | |
764 | } | |
765 | ||
766 | of_node_put(np); | |
767 | return cdev; | |
c36cf071 | 768 | } |
3ebb62ff | 769 | EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register); |
c36cf071 | 770 | |
02361418 ADK |
771 | /** |
772 | * cpufreq_cooling_unregister - function to remove cpufreq cooling device. | |
773 | * @cdev: thermal cooling device pointer. | |
135266b4 EV |
774 | * |
775 | * This interface function unregisters the "thermal-cpufreq-%x" cooling device. | |
02361418 ADK |
776 | */ |
777 | void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) | |
778 | { | |
1dea432a | 779 | struct cpufreq_cooling_device *cpufreq_cdev; |
088db931 | 780 | bool last; |
02361418 | 781 | |
50e66c7e EV |
782 | if (!cdev) |
783 | return; | |
784 | ||
1dea432a | 785 | cpufreq_cdev = cdev->devdata; |
02361418 | 786 | |
ae606089 | 787 | mutex_lock(&cooling_list_lock); |
1dea432a | 788 | list_del(&cpufreq_cdev->node); |
02361418 | 789 | /* Unregister the notifier for the last cpufreq cooling device */ |
1dea432a | 790 | last = list_empty(&cpufreq_cdev_list); |
088db931 MW |
791 | mutex_unlock(&cooling_list_lock); |
792 | ||
793 | if (last) | |
02361418 | 794 | cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block, |
5fda7f68 | 795 | CPUFREQ_POLICY_NOTIFIER); |
02373d7c | 796 | |
72554a75 | 797 | thermal_cooling_device_unregister(cdev); |
1dea432a | 798 | ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); |
81ee14da | 799 | kfree(cpufreq_cdev->idle_time); |
1dea432a VK |
800 | kfree(cpufreq_cdev->freq_table); |
801 | kfree(cpufreq_cdev); | |
02361418 | 802 | } |
243dbd9c | 803 | EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister); |