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