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