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