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