Commit | Line | Data |
---|---|---|
0fac9e2f | 1 | // SPDX-License-Identifier: GPL-2.0 |
02361418 | 2 | /* |
23affa2e | 3 | * linux/drivers/thermal/cpufreq_cooling.c |
02361418 ADK |
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 | */ |
5ccb451e | 13 | #include <linux/cpu.h> |
02361418 | 14 | #include <linux/cpufreq.h> |
5ccb451e AK |
15 | #include <linux/cpu_cooling.h> |
16 | #include <linux/energy_model.h> | |
02361418 | 17 | #include <linux/err.h> |
c65f83c0 | 18 | #include <linux/export.h> |
ae606089 | 19 | #include <linux/idr.h> |
c36cf071 | 20 | #include <linux/pm_opp.h> |
5130802d | 21 | #include <linux/pm_qos.h> |
02361418 | 22 | #include <linux/slab.h> |
5ccb451e | 23 | #include <linux/thermal.h> |
02361418 | 24 | |
6828a471 JM |
25 | #include <trace/events/thermal.h> |
26 | ||
07d888d8 VK |
27 | /* |
28 | * Cooling state <-> CPUFreq frequency | |
29 | * | |
30 | * Cooling states are translated to frequencies throughout this driver and this | |
31 | * is the relation between them. | |
32 | * | |
33 | * Highest cooling state corresponds to lowest possible frequency. | |
34 | * | |
35 | * i.e. | |
36 | * level 0 --> 1st Max Freq | |
37 | * level 1 --> 2nd Max Freq | |
38 | * ... | |
39 | */ | |
40 | ||
81ee14da VK |
41 | /** |
42 | * struct time_in_idle - Idle time stats | |
43 | * @time: previous reading of the absolute time that this cpu was idle | |
44 | * @timestamp: wall time of the last invocation of get_cpu_idle_time_us() | |
45 | */ | |
46 | struct time_in_idle { | |
47 | u64 time; | |
48 | u64 timestamp; | |
49 | }; | |
50 | ||
02361418 | 51 | /** |
3b3c0748 | 52 | * struct cpufreq_cooling_device - data for cooling device with cpufreq |
02361418 ADK |
53 | * @id: unique integer value corresponding to each cpufreq_cooling_device |
54 | * registered. | |
d72b4015 | 55 | * @last_load: load measured by the latest call to cpufreq_get_requested_power() |
02361418 ADK |
56 | * @cpufreq_state: integer value representing the current state of cpufreq |
57 | * cooling devices. | |
dcc6c7fd VK |
58 | * @max_level: maximum cooling level. One less than total number of valid |
59 | * cpufreq frequencies. | |
a4e893e8 | 60 | * @em: Reference on the Energy Model of the device |
d72b4015 VK |
61 | * @cdev: thermal_cooling_device pointer to keep track of the |
62 | * registered cooling device. | |
63 | * @policy: cpufreq policy. | |
fc4de356 | 64 | * @node: list_head to link all cpufreq_cooling_device together. |
81ee14da | 65 | * @idle_time: idle time stats |
7b4e7f07 | 66 | * @qos_req: PM QoS contraint to apply |
02361418 | 67 | * |
beca6053 VK |
68 | * This structure is required for keeping information of each registered |
69 | * cpufreq_cooling_device. | |
02361418 ADK |
70 | */ |
71 | struct cpufreq_cooling_device { | |
72 | int id; | |
d72b4015 | 73 | u32 last_load; |
02361418 | 74 | unsigned int cpufreq_state; |
dcc6c7fd | 75 | unsigned int max_level; |
a4e893e8 | 76 | struct em_perf_domain *em; |
d72b4015 | 77 | struct cpufreq_policy *policy; |
2dcd851f | 78 | struct list_head node; |
81ee14da | 79 | struct time_in_idle *idle_time; |
3000ce3c | 80 | struct freq_qos_request qos_req; |
02361418 | 81 | }; |
02361418 | 82 | |
fb8ea308 | 83 | static DEFINE_IDA(cpufreq_ida); |
02373d7c | 84 | static DEFINE_MUTEX(cooling_list_lock); |
1dea432a | 85 | static LIST_HEAD(cpufreq_cdev_list); |
02361418 | 86 | |
5a4e5b78 | 87 | #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR |
02361418 | 88 | /** |
4843c4a1 | 89 | * get_level: Find the level for a particular frequency |
1dea432a | 90 | * @cpufreq_cdev: cpufreq_cdev for which the property is required |
4843c4a1 | 91 | * @freq: Frequency |
82b9ee40 | 92 | * |
da27f69d | 93 | * Return: level corresponding to the frequency. |
02361418 | 94 | */ |
1dea432a | 95 | static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev, |
4843c4a1 | 96 | unsigned int freq) |
02361418 | 97 | { |
a4e893e8 | 98 | int i; |
a116776f | 99 | |
a4e893e8 QP |
100 | for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) { |
101 | if (freq > cpufreq_cdev->em->table[i].frequency) | |
4843c4a1 | 102 | break; |
c36cf071 | 103 | } |
02361418 | 104 | |
a4e893e8 | 105 | return cpufreq_cdev->max_level - i - 1; |
c36cf071 JM |
106 | } |
107 | ||
1dea432a | 108 | static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev, |
c36cf071 JM |
109 | u32 freq) |
110 | { | |
111 | int i; | |
c36cf071 | 112 | |
a4e893e8 QP |
113 | for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) { |
114 | if (freq > cpufreq_cdev->em->table[i].frequency) | |
c36cf071 | 115 | break; |
a4e893e8 | 116 | } |
c36cf071 | 117 | |
a4e893e8 | 118 | return cpufreq_cdev->em->table[i + 1].power; |
c36cf071 JM |
119 | } |
120 | ||
1dea432a | 121 | static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev, |
c36cf071 JM |
122 | u32 power) |
123 | { | |
124 | int i; | |
c36cf071 | 125 | |
371a3bc7 FX |
126 | for (i = cpufreq_cdev->max_level; i >= 0; i--) { |
127 | if (power >= cpufreq_cdev->em->table[i].power) | |
c36cf071 | 128 | break; |
a4e893e8 | 129 | } |
c36cf071 | 130 | |
371a3bc7 | 131 | return cpufreq_cdev->em->table[i].frequency; |
c36cf071 JM |
132 | } |
133 | ||
134 | /** | |
135 | * get_load() - get load for a cpu since last updated | |
1dea432a | 136 | * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu |
c36cf071 | 137 | * @cpu: cpu number |
ba76dd9d | 138 | * @cpu_idx: index of the cpu in time_in_idle* |
c36cf071 JM |
139 | * |
140 | * Return: The average load of cpu @cpu in percentage since this | |
141 | * function was last called. | |
142 | */ | |
1dea432a | 143 | static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu, |
a53b8394 | 144 | int cpu_idx) |
c36cf071 JM |
145 | { |
146 | u32 load; | |
147 | u64 now, now_idle, delta_time, delta_idle; | |
81ee14da | 148 | struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx]; |
c36cf071 JM |
149 | |
150 | now_idle = get_cpu_idle_time(cpu, &now, 0); | |
81ee14da VK |
151 | delta_idle = now_idle - idle_time->time; |
152 | delta_time = now - idle_time->timestamp; | |
c36cf071 JM |
153 | |
154 | if (delta_time <= delta_idle) | |
155 | load = 0; | |
156 | else | |
157 | load = div64_u64(100 * (delta_time - delta_idle), delta_time); | |
158 | ||
81ee14da VK |
159 | idle_time->time = now_idle; |
160 | idle_time->timestamp = now; | |
c36cf071 JM |
161 | |
162 | return load; | |
163 | } | |
164 | ||
c36cf071 JM |
165 | /** |
166 | * get_dynamic_power() - calculate the dynamic power | |
1dea432a | 167 | * @cpufreq_cdev: &cpufreq_cooling_device for this cdev |
c36cf071 JM |
168 | * @freq: current frequency |
169 | * | |
170 | * Return: the dynamic power consumed by the cpus described by | |
1dea432a | 171 | * @cpufreq_cdev. |
c36cf071 | 172 | */ |
1dea432a | 173 | static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev, |
c36cf071 JM |
174 | unsigned long freq) |
175 | { | |
176 | u32 raw_cpu_power; | |
177 | ||
1dea432a VK |
178 | raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq); |
179 | return (raw_cpu_power * cpufreq_cdev->last_load) / 100; | |
02361418 ADK |
180 | } |
181 | ||
c36cf071 JM |
182 | /** |
183 | * cpufreq_get_requested_power() - get the current power | |
184 | * @cdev: &thermal_cooling_device pointer | |
c36cf071 JM |
185 | * @power: pointer in which to store the resulting power |
186 | * | |
187 | * Calculate the current power consumption of the cpus in milliwatts | |
188 | * and store it in @power. This function should actually calculate | |
189 | * the requested power, but it's hard to get the frequency that | |
190 | * cpufreq would have assigned if there were no thermal limits. | |
191 | * Instead, we calculate the current power on the assumption that the | |
192 | * immediate future will look like the immediate past. | |
193 | * | |
194 | * We use the current frequency and the average load since this | |
195 | * function was last called. In reality, there could have been | |
196 | * multiple opps since this function was last called and that affects | |
197 | * the load calculation. While it's not perfectly accurate, this | |
198 | * simplification is good enough and works. REVISIT this, as more | |
199 | * complex code may be needed if experiments show that it's not | |
200 | * accurate enough. | |
201 | * | |
202 | * Return: 0 on success, -E* if getting the static power failed. | |
203 | */ | |
204 | static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev, | |
c36cf071 JM |
205 | u32 *power) |
206 | { | |
207 | unsigned long freq; | |
84fe2cab VK |
208 | int i = 0, cpu; |
209 | u32 total_load = 0; | |
1dea432a | 210 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
ba76dd9d | 211 | struct cpufreq_policy *policy = cpufreq_cdev->policy; |
6828a471 | 212 | u32 *load_cpu = NULL; |
c36cf071 | 213 | |
ba76dd9d | 214 | freq = cpufreq_quick_get(policy->cpu); |
c36cf071 | 215 | |
6828a471 | 216 | if (trace_thermal_power_cpu_get_power_enabled()) { |
ba76dd9d | 217 | u32 ncpus = cpumask_weight(policy->related_cpus); |
6828a471 | 218 | |
a71544cd | 219 | load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL); |
6828a471 JM |
220 | } |
221 | ||
ba76dd9d | 222 | for_each_cpu(cpu, policy->related_cpus) { |
c36cf071 JM |
223 | u32 load; |
224 | ||
225 | if (cpu_online(cpu)) | |
1dea432a | 226 | load = get_load(cpufreq_cdev, cpu, i); |
c36cf071 JM |
227 | else |
228 | load = 0; | |
229 | ||
230 | total_load += load; | |
bf45ac18 | 231 | if (load_cpu) |
6828a471 JM |
232 | load_cpu[i] = load; |
233 | ||
234 | i++; | |
c36cf071 JM |
235 | } |
236 | ||
1dea432a | 237 | cpufreq_cdev->last_load = total_load; |
c36cf071 | 238 | |
84fe2cab | 239 | *power = get_dynamic_power(cpufreq_cdev, freq); |
6828a471 JM |
240 | |
241 | if (load_cpu) { | |
ba76dd9d | 242 | trace_thermal_power_cpu_get_power(policy->related_cpus, freq, |
84fe2cab | 243 | load_cpu, i, *power); |
6828a471 | 244 | |
a71544cd | 245 | kfree(load_cpu); |
6828a471 | 246 | } |
c36cf071 | 247 | |
c36cf071 JM |
248 | return 0; |
249 | } | |
250 | ||
251 | /** | |
252 | * cpufreq_state2power() - convert a cpu cdev state to power consumed | |
253 | * @cdev: &thermal_cooling_device pointer | |
c36cf071 JM |
254 | * @state: cooling device state to be converted |
255 | * @power: pointer in which to store the resulting power | |
256 | * | |
257 | * Convert cooling device state @state into power consumption in | |
258 | * milliwatts assuming 100% load. Store the calculated power in | |
259 | * @power. | |
260 | * | |
261 | * Return: 0 on success, -EINVAL if the cooling device state could not | |
262 | * be converted into a frequency or other -E* if there was an error | |
263 | * when calculating the static power. | |
264 | */ | |
265 | static int cpufreq_state2power(struct thermal_cooling_device *cdev, | |
c36cf071 JM |
266 | unsigned long state, u32 *power) |
267 | { | |
a4e893e8 | 268 | unsigned int freq, num_cpus, idx; |
1dea432a | 269 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
c36cf071 | 270 | |
cb1b6318 | 271 | /* Request state should be less than max_level */ |
40ea5685 | 272 | if (state > cpufreq_cdev->max_level) |
cb1b6318 VK |
273 | return -EINVAL; |
274 | ||
ba76dd9d | 275 | num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus); |
c36cf071 | 276 | |
a4e893e8 QP |
277 | idx = cpufreq_cdev->max_level - state; |
278 | freq = cpufreq_cdev->em->table[idx].frequency; | |
84fe2cab | 279 | *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus; |
c36cf071 | 280 | |
84fe2cab | 281 | return 0; |
c36cf071 JM |
282 | } |
283 | ||
284 | /** | |
285 | * cpufreq_power2state() - convert power to a cooling device state | |
286 | * @cdev: &thermal_cooling_device pointer | |
c36cf071 JM |
287 | * @power: power in milliwatts to be converted |
288 | * @state: pointer in which to store the resulting state | |
289 | * | |
290 | * Calculate a cooling device state for the cpus described by @cdev | |
291 | * that would allow them to consume at most @power mW and store it in | |
292 | * @state. Note that this calculation depends on external factors | |
293 | * such as the cpu load or the current static power. Calling this | |
294 | * function with the same power as input can yield different cooling | |
295 | * device states depending on those external factors. | |
296 | * | |
297 | * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if | |
298 | * the calculated frequency could not be converted to a valid state. | |
299 | * The latter should not happen unless the frequencies available to | |
300 | * cpufreq have changed since the initialization of the cpu cooling | |
301 | * device. | |
302 | */ | |
303 | static int cpufreq_power2state(struct thermal_cooling_device *cdev, | |
ecd1d2a3 | 304 | u32 power, unsigned long *state) |
c36cf071 | 305 | { |
e0fda737 | 306 | unsigned int target_freq; |
84fe2cab | 307 | u32 last_load, normalised_power; |
1dea432a | 308 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
ba76dd9d | 309 | struct cpufreq_policy *policy = cpufreq_cdev->policy; |
c36cf071 | 310 | |
1dea432a | 311 | last_load = cpufreq_cdev->last_load ?: 1; |
84fe2cab | 312 | normalised_power = (power * 100) / last_load; |
1dea432a | 313 | target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power); |
c36cf071 | 314 | |
3e08b2df | 315 | *state = get_level(cpufreq_cdev, target_freq); |
ba76dd9d VK |
316 | trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state, |
317 | power); | |
c36cf071 JM |
318 | return 0; |
319 | } | |
a4e893e8 QP |
320 | |
321 | static inline bool em_is_sane(struct cpufreq_cooling_device *cpufreq_cdev, | |
322 | struct em_perf_domain *em) { | |
323 | struct cpufreq_policy *policy; | |
324 | unsigned int nr_levels; | |
325 | ||
326 | if (!em) | |
327 | return false; | |
328 | ||
329 | policy = cpufreq_cdev->policy; | |
521b512b | 330 | if (!cpumask_equal(policy->related_cpus, em_span_cpus(em))) { |
a4e893e8 | 331 | pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n", |
521b512b | 332 | cpumask_pr_args(em_span_cpus(em)), |
a4e893e8 QP |
333 | cpumask_pr_args(policy->related_cpus)); |
334 | return false; | |
335 | } | |
336 | ||
337 | nr_levels = cpufreq_cdev->max_level + 1; | |
521b512b LL |
338 | if (em_pd_nr_perf_states(em) != nr_levels) { |
339 | pr_err("The number of performance states in pd %*pbl (%u) doesn't match the number of cooling levels (%u)\n", | |
340 | cpumask_pr_args(em_span_cpus(em)), | |
341 | em_pd_nr_perf_states(em), nr_levels); | |
a4e893e8 QP |
342 | return false; |
343 | } | |
344 | ||
345 | return true; | |
346 | } | |
5a4e5b78 QP |
347 | #endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */ |
348 | ||
a4e893e8 QP |
349 | static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev, |
350 | unsigned long state) | |
351 | { | |
352 | struct cpufreq_policy *policy; | |
353 | unsigned long idx; | |
354 | ||
355 | #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR | |
356 | /* Use the Energy Model table if available */ | |
357 | if (cpufreq_cdev->em) { | |
358 | idx = cpufreq_cdev->max_level - state; | |
359 | return cpufreq_cdev->em->table[idx].frequency; | |
360 | } | |
361 | #endif | |
362 | ||
363 | /* Otherwise, fallback on the CPUFreq table */ | |
364 | policy = cpufreq_cdev->policy; | |
365 | if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) | |
366 | idx = cpufreq_cdev->max_level - state; | |
367 | else | |
368 | idx = state; | |
369 | ||
370 | return policy->freq_table[idx].frequency; | |
371 | } | |
372 | ||
5a4e5b78 QP |
373 | /* cpufreq cooling device callback functions are defined below */ |
374 | ||
375 | /** | |
376 | * cpufreq_get_max_state - callback function to get the max cooling state. | |
377 | * @cdev: thermal cooling device pointer. | |
378 | * @state: fill this variable with the max cooling state. | |
379 | * | |
380 | * Callback for the thermal cooling device to return the cpufreq | |
381 | * max cooling state. | |
382 | * | |
383 | * Return: 0 on success, an error code otherwise. | |
384 | */ | |
385 | static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, | |
386 | unsigned long *state) | |
387 | { | |
388 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; | |
389 | ||
390 | *state = cpufreq_cdev->max_level; | |
391 | return 0; | |
392 | } | |
393 | ||
394 | /** | |
395 | * cpufreq_get_cur_state - callback function to get the current cooling state. | |
396 | * @cdev: thermal cooling device pointer. | |
397 | * @state: fill this variable with the current cooling state. | |
398 | * | |
399 | * Callback for the thermal cooling device to return the cpufreq | |
400 | * current cooling state. | |
401 | * | |
402 | * Return: 0 on success, an error code otherwise. | |
403 | */ | |
404 | static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, | |
405 | unsigned long *state) | |
406 | { | |
407 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; | |
408 | ||
409 | *state = cpufreq_cdev->cpufreq_state; | |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
414 | /** | |
415 | * cpufreq_set_cur_state - callback function to set the current cooling state. | |
416 | * @cdev: thermal cooling device pointer. | |
417 | * @state: set this variable to the current cooling state. | |
418 | * | |
419 | * Callback for the thermal cooling device to change the cpufreq | |
420 | * current cooling state. | |
421 | * | |
422 | * Return: 0 on success, an error code otherwise. | |
423 | */ | |
424 | static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, | |
425 | unsigned long state) | |
426 | { | |
427 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; | |
f12e4f66 TG |
428 | struct cpumask *cpus; |
429 | unsigned int frequency; | |
430 | unsigned long max_capacity, capacity; | |
431 | int ret; | |
5a4e5b78 QP |
432 | |
433 | /* Request state should be less than max_level */ | |
40ea5685 | 434 | if (state > cpufreq_cdev->max_level) |
5a4e5b78 QP |
435 | return -EINVAL; |
436 | ||
437 | /* Check if the old cooling action is same as new cooling action */ | |
438 | if (cpufreq_cdev->cpufreq_state == state) | |
439 | return 0; | |
440 | ||
f12e4f66 TG |
441 | frequency = get_state_freq(cpufreq_cdev, state); |
442 | ||
443 | ret = freq_qos_update_request(&cpufreq_cdev->qos_req, frequency); | |
f12e4f66 | 444 | if (ret > 0) { |
236761f1 | 445 | cpufreq_cdev->cpufreq_state = state; |
f12e4f66 TG |
446 | cpus = cpufreq_cdev->policy->cpus; |
447 | max_capacity = arch_scale_cpu_capacity(cpumask_first(cpus)); | |
448 | capacity = frequency * max_capacity; | |
449 | capacity /= cpufreq_cdev->policy->cpuinfo.max_freq; | |
450 | arch_set_thermal_pressure(cpus, max_capacity - capacity); | |
34183ddd | 451 | ret = 0; |
f12e4f66 TG |
452 | } |
453 | ||
454 | return ret; | |
5a4e5b78 | 455 | } |
c36cf071 | 456 | |
02361418 | 457 | /* Bind cpufreq callbacks to thermal cooling device ops */ |
a305a438 | 458 | |
c36cf071 | 459 | static struct thermal_cooling_device_ops cpufreq_cooling_ops = { |
a305a438 BJ |
460 | .get_max_state = cpufreq_get_max_state, |
461 | .get_cur_state = cpufreq_get_cur_state, | |
462 | .set_cur_state = cpufreq_set_cur_state, | |
a305a438 BJ |
463 | }; |
464 | ||
02361418 | 465 | /** |
39d99cff EV |
466 | * __cpufreq_cooling_register - helper function to create cpufreq cooling device |
467 | * @np: a valid struct device_node to the cooling device device tree node | |
4d753aa7 | 468 | * @policy: cpufreq policy |
405fb825 | 469 | * Normally this should be same as cpufreq policy->related_cpus. |
a4e893e8 | 470 | * @em: Energy Model of the cpufreq policy |
12cb08ba EV |
471 | * |
472 | * This interface function registers the cpufreq cooling device with the name | |
473 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq | |
39d99cff EV |
474 | * cooling devices. It also gives the opportunity to link the cooling device |
475 | * with a device tree node, in order to bind it via the thermal DT code. | |
12cb08ba EV |
476 | * |
477 | * Return: a valid struct thermal_cooling_device pointer on success, | |
478 | * on failure, it returns a corresponding ERR_PTR(). | |
02361418 | 479 | */ |
39d99cff EV |
480 | static struct thermal_cooling_device * |
481 | __cpufreq_cooling_register(struct device_node *np, | |
a4e893e8 QP |
482 | struct cpufreq_policy *policy, |
483 | struct em_perf_domain *em) | |
02361418 | 484 | { |
04bdbdf9 | 485 | struct thermal_cooling_device *cdev; |
1dea432a | 486 | struct cpufreq_cooling_device *cpufreq_cdev; |
02361418 | 487 | char dev_name[THERMAL_NAME_LENGTH]; |
a4e893e8 | 488 | unsigned int i, num_cpus; |
5130802d | 489 | struct device *dev; |
405fb825 | 490 | int ret; |
a305a438 | 491 | struct thermal_cooling_device_ops *cooling_ops; |
5130802d VK |
492 | |
493 | dev = get_cpu_device(policy->cpu); | |
494 | if (unlikely(!dev)) { | |
495 | pr_warn("No cpu device for cpu %d\n", policy->cpu); | |
496 | return ERR_PTR(-ENODEV); | |
497 | } | |
498 | ||
02361418 | 499 | |
4d753aa7 | 500 | if (IS_ERR_OR_NULL(policy)) { |
b2fd708f | 501 | pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy); |
4d753aa7 | 502 | return ERR_PTR(-EINVAL); |
f8bfc116 VK |
503 | } |
504 | ||
55d85293 VK |
505 | i = cpufreq_table_count_valid_entries(policy); |
506 | if (!i) { | |
507 | pr_debug("%s: CPUFreq table not found or has no valid entries\n", | |
508 | __func__); | |
4d753aa7 | 509 | return ERR_PTR(-ENODEV); |
02361418 | 510 | } |
0f1be51c | 511 | |
1dea432a | 512 | cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL); |
4d753aa7 VK |
513 | if (!cpufreq_cdev) |
514 | return ERR_PTR(-ENOMEM); | |
02361418 | 515 | |
b12b6519 | 516 | cpufreq_cdev->policy = policy; |
4d753aa7 | 517 | num_cpus = cpumask_weight(policy->related_cpus); |
81ee14da VK |
518 | cpufreq_cdev->idle_time = kcalloc(num_cpus, |
519 | sizeof(*cpufreq_cdev->idle_time), | |
520 | GFP_KERNEL); | |
521 | if (!cpufreq_cdev->idle_time) { | |
04bdbdf9 | 522 | cdev = ERR_PTR(-ENOMEM); |
c36cf071 JM |
523 | goto free_cdev; |
524 | } | |
525 | ||
55d85293 VK |
526 | /* max_level is an index, not a counter */ |
527 | cpufreq_cdev->max_level = i - 1; | |
dcc6c7fd | 528 | |
ae606089 MW |
529 | ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL); |
530 | if (ret < 0) { | |
04bdbdf9 | 531 | cdev = ERR_PTR(ret); |
a4e893e8 | 532 | goto free_idle_time; |
02361418 | 533 | } |
1dea432a | 534 | cpufreq_cdev->id = ret; |
02361418 | 535 | |
349d39dc VK |
536 | snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d", |
537 | cpufreq_cdev->id); | |
538 | ||
5a4e5b78 QP |
539 | cooling_ops = &cpufreq_cooling_ops; |
540 | ||
541 | #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR | |
a4e893e8 QP |
542 | if (em_is_sane(cpufreq_cdev, em)) { |
543 | cpufreq_cdev->em = em; | |
5a4e5b78 QP |
544 | cooling_ops->get_requested_power = cpufreq_get_requested_power; |
545 | cooling_ops->state2power = cpufreq_state2power; | |
546 | cooling_ops->power2state = cpufreq_power2state; | |
a4e893e8 | 547 | } else |
5a4e5b78 | 548 | #endif |
a4e893e8 QP |
549 | if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) { |
550 | pr_err("%s: unsorted frequency tables are not supported\n", | |
551 | __func__); | |
552 | cdev = ERR_PTR(-EINVAL); | |
553 | goto remove_ida; | |
554 | } | |
f840ab18 | 555 | |
3000ce3c RW |
556 | ret = freq_qos_add_request(&policy->constraints, |
557 | &cpufreq_cdev->qos_req, FREQ_QOS_MAX, | |
a4e893e8 | 558 | get_state_freq(cpufreq_cdev, 0)); |
5130802d VK |
559 | if (ret < 0) { |
560 | pr_err("%s: Failed to add freq constraint (%d)\n", __func__, | |
561 | ret); | |
562 | cdev = ERR_PTR(ret); | |
563 | goto remove_ida; | |
564 | } | |
565 | ||
04bdbdf9 VK |
566 | cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev, |
567 | cooling_ops); | |
568 | if (IS_ERR(cdev)) | |
5130802d | 569 | goto remove_qos_req; |
92e615ec | 570 | |
02373d7c | 571 | mutex_lock(&cooling_list_lock); |
1dea432a | 572 | list_add(&cpufreq_cdev->node, &cpufreq_cdev_list); |
088db931 | 573 | mutex_unlock(&cooling_list_lock); |
02373d7c | 574 | |
4d753aa7 | 575 | return cdev; |
730abe06 | 576 | |
5130802d | 577 | remove_qos_req: |
3000ce3c | 578 | freq_qos_remove_request(&cpufreq_cdev->qos_req); |
ae606089 | 579 | remove_ida: |
1dea432a | 580 | ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); |
81ee14da VK |
581 | free_idle_time: |
582 | kfree(cpufreq_cdev->idle_time); | |
730abe06 | 583 | free_cdev: |
1dea432a | 584 | kfree(cpufreq_cdev); |
04bdbdf9 | 585 | return cdev; |
02361418 | 586 | } |
39d99cff EV |
587 | |
588 | /** | |
589 | * cpufreq_cooling_register - function to create cpufreq cooling device. | |
4d753aa7 | 590 | * @policy: cpufreq policy |
39d99cff EV |
591 | * |
592 | * This interface function registers the cpufreq cooling device with the name | |
593 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq | |
594 | * cooling devices. | |
595 | * | |
596 | * Return: a valid struct thermal_cooling_device pointer on success, | |
597 | * on failure, it returns a corresponding ERR_PTR(). | |
598 | */ | |
599 | struct thermal_cooling_device * | |
4d753aa7 | 600 | cpufreq_cooling_register(struct cpufreq_policy *policy) |
39d99cff | 601 | { |
a4e893e8 | 602 | return __cpufreq_cooling_register(NULL, policy, NULL); |
39d99cff | 603 | } |
243dbd9c | 604 | EXPORT_SYMBOL_GPL(cpufreq_cooling_register); |
02361418 | 605 | |
39d99cff EV |
606 | /** |
607 | * of_cpufreq_cooling_register - function to create cpufreq cooling device. | |
4d753aa7 | 608 | * @policy: cpufreq policy |
39d99cff EV |
609 | * |
610 | * This interface function registers the cpufreq cooling device with the name | |
611 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq | |
612 | * cooling devices. Using this API, the cpufreq cooling device will be | |
613 | * linked to the device tree node provided. | |
614 | * | |
c36cf071 JM |
615 | * Using this function, the cooling device will implement the power |
616 | * extensions by using a simple cpu power model. The cpus must have | |
617 | * registered their OPPs using the OPP library. | |
618 | * | |
f5f263fe VK |
619 | * It also takes into account, if property present in policy CPU node, the |
620 | * static power consumed by the cpu. | |
c36cf071 JM |
621 | * |
622 | * Return: a valid struct thermal_cooling_device pointer on success, | |
f5f263fe | 623 | * and NULL on failure. |
c36cf071 JM |
624 | */ |
625 | struct thermal_cooling_device * | |
3ebb62ff | 626 | of_cpufreq_cooling_register(struct cpufreq_policy *policy) |
c36cf071 | 627 | { |
f5f263fe VK |
628 | struct device_node *np = of_get_cpu_node(policy->cpu, NULL); |
629 | struct thermal_cooling_device *cdev = NULL; | |
f5f263fe VK |
630 | |
631 | if (!np) { | |
23affa2e | 632 | pr_err("cpufreq_cooling: OF node not available for cpu%d\n", |
f5f263fe VK |
633 | policy->cpu); |
634 | return NULL; | |
635 | } | |
c36cf071 | 636 | |
f5f263fe | 637 | if (of_find_property(np, "#cooling-cells", NULL)) { |
a4e893e8 | 638 | struct em_perf_domain *em = em_cpu_get(policy->cpu); |
f5f263fe | 639 | |
a4e893e8 | 640 | cdev = __cpufreq_cooling_register(np, policy, em); |
f5f263fe | 641 | if (IS_ERR(cdev)) { |
23affa2e | 642 | pr_err("cpufreq_cooling: cpu%d failed to register as cooling device: %ld\n", |
f5f263fe VK |
643 | policy->cpu, PTR_ERR(cdev)); |
644 | cdev = NULL; | |
645 | } | |
646 | } | |
647 | ||
648 | of_node_put(np); | |
649 | return cdev; | |
c36cf071 | 650 | } |
3ebb62ff | 651 | EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register); |
c36cf071 | 652 | |
02361418 ADK |
653 | /** |
654 | * cpufreq_cooling_unregister - function to remove cpufreq cooling device. | |
655 | * @cdev: thermal cooling device pointer. | |
135266b4 EV |
656 | * |
657 | * This interface function unregisters the "thermal-cpufreq-%x" cooling device. | |
02361418 ADK |
658 | */ |
659 | void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) | |
660 | { | |
1dea432a | 661 | struct cpufreq_cooling_device *cpufreq_cdev; |
02361418 | 662 | |
50e66c7e EV |
663 | if (!cdev) |
664 | return; | |
665 | ||
1dea432a | 666 | cpufreq_cdev = cdev->devdata; |
02361418 | 667 | |
ae606089 | 668 | mutex_lock(&cooling_list_lock); |
1dea432a | 669 | list_del(&cpufreq_cdev->node); |
088db931 MW |
670 | mutex_unlock(&cooling_list_lock); |
671 | ||
72554a75 | 672 | thermal_cooling_device_unregister(cdev); |
3000ce3c | 673 | freq_qos_remove_request(&cpufreq_cdev->qos_req); |
1dea432a | 674 | ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); |
81ee14da | 675 | kfree(cpufreq_cdev->idle_time); |
1dea432a | 676 | kfree(cpufreq_cdev); |
02361418 | 677 | } |
243dbd9c | 678 | EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister); |