Commit | Line | Data |
---|---|---|
0de967f2 | 1 | // SPDX-License-Identifier: GPL-2.0 |
6b775e87 JM |
2 | /* |
3 | * A power allocator to manage temperature | |
4 | * | |
5 | * Copyright (C) 2014 ARM Ltd. | |
6 | * | |
6b775e87 JM |
7 | */ |
8 | ||
9 | #define pr_fmt(fmt) "Power allocator: " fmt | |
10 | ||
6b775e87 JM |
11 | #include <linux/slab.h> |
12 | #include <linux/thermal.h> | |
13 | ||
6828a471 | 14 | #define CREATE_TRACE_POINTS |
32a7a021 | 15 | #include "thermal_trace_ipa.h" |
6828a471 | 16 | |
6b775e87 JM |
17 | #include "thermal_core.h" |
18 | ||
19 | #define FRAC_BITS 10 | |
20 | #define int_to_frac(x) ((x) << FRAC_BITS) | |
21 | #define frac_to_int(x) ((x) >> FRAC_BITS) | |
22 | ||
23 | /** | |
24 | * mul_frac() - multiply two fixed-point numbers | |
25 | * @x: first multiplicand | |
26 | * @y: second multiplicand | |
27 | * | |
28 | * Return: the result of multiplying two fixed-point numbers. The | |
29 | * result is also a fixed-point number. | |
30 | */ | |
31 | static inline s64 mul_frac(s64 x, s64 y) | |
32 | { | |
33 | return (x * y) >> FRAC_BITS; | |
34 | } | |
35 | ||
36 | /** | |
37 | * div_frac() - divide two fixed-point numbers | |
38 | * @x: the dividend | |
39 | * @y: the divisor | |
40 | * | |
41 | * Return: the result of dividing two fixed-point numbers. The | |
42 | * result is also a fixed-point number. | |
43 | */ | |
44 | static inline s64 div_frac(s64 x, s64 y) | |
45 | { | |
46 | return div_s64(x << FRAC_BITS, y); | |
47 | } | |
48 | ||
912e97c6 LL |
49 | /** |
50 | * struct power_actor - internal power information for power actor | |
51 | * @req_power: requested power value (not weighted) | |
52 | * @max_power: max allocatable power for this actor | |
53 | * @granted_power: granted power for this actor | |
54 | * @extra_actor_power: extra power that this actor can receive | |
55 | * @weighted_req_power: weighted requested power as input to IPA | |
56 | */ | |
57 | struct power_actor { | |
58 | u32 req_power; | |
59 | u32 max_power; | |
60 | u32 granted_power; | |
61 | u32 extra_actor_power; | |
62 | u32 weighted_req_power; | |
63 | }; | |
64 | ||
6b775e87 JM |
65 | /** |
66 | * struct power_allocator_params - parameters for the power allocator governor | |
f5cbb182 JM |
67 | * @allocated_tzp: whether we have allocated tzp for this thermal zone and |
68 | * it needs to be freed on unbind | |
6b775e87 JM |
69 | * @err_integral: accumulated error in the PID controller. |
70 | * @prev_err: error in the previous iteration of the PID controller. | |
71 | * Used to calculate the derivative term. | |
94be1d27 RW |
72 | * @sustainable_power: Sustainable power (heat) that this thermal zone can |
73 | * dissipate | |
6b775e87 JM |
74 | * @trip_switch_on: first passive trip point of the thermal zone. The |
75 | * governor switches on when this trip point is crossed. | |
8b7b390f | 76 | * If the thermal zone only has one passive trip point, |
94be1d27 | 77 | * @trip_switch_on should be NULL. |
4e6d4687 LL |
78 | * @trip_max: last passive trip point of the thermal zone. The |
79 | * temperature we are controlling for. | |
a3cd6db4 | 80 | * @total_weight: Sum of all thermal instances weights |
912e97c6 LL |
81 | * @num_actors: number of cooling devices supporting IPA callbacks |
82 | * @buffer_size: internal buffer size, to avoid runtime re-calculation | |
83 | * @power: buffer for all power actors internal power information | |
6b775e87 JM |
84 | */ |
85 | struct power_allocator_params { | |
f5cbb182 | 86 | bool allocated_tzp; |
6b775e87 JM |
87 | s64 err_integral; |
88 | s32 prev_err; | |
eda1ecfa | 89 | u32 sustainable_power; |
94be1d27 | 90 | const struct thermal_trip *trip_switch_on; |
4e6d4687 | 91 | const struct thermal_trip *trip_max; |
a3cd6db4 | 92 | int total_weight; |
912e97c6 LL |
93 | unsigned int num_actors; |
94 | unsigned int buffer_size; | |
95 | struct power_actor *power; | |
6b775e87 JM |
96 | }; |
97 | ||
e3ecd571 LL |
98 | static bool power_actor_is_valid(struct power_allocator_params *params, |
99 | struct thermal_instance *instance) | |
100 | { | |
101 | return (instance->trip == params->trip_max && | |
102 | cdev_is_power_actor(instance->cdev)); | |
103 | } | |
104 | ||
e055bb0f JM |
105 | /** |
106 | * estimate_sustainable_power() - Estimate the sustainable power of a thermal zone | |
107 | * @tz: thermal zone we are operating in | |
108 | * | |
109 | * For thermal zones that don't provide a sustainable_power in their | |
110 | * thermal_zone_params, estimate one. Calculate it using the minimum | |
111 | * power of all the cooling devices as that gives a valid value that | |
112 | * can give some degree of functionality. For optimal performance of | |
113 | * this governor, provide a sustainable_power in the thermal zone's | |
114 | * thermal_zone_params. | |
115 | */ | |
116 | static u32 estimate_sustainable_power(struct thermal_zone_device *tz) | |
117 | { | |
e055bb0f | 118 | struct power_allocator_params *params = tz->governor_data; |
499cc391 LL |
119 | struct thermal_cooling_device *cdev; |
120 | struct thermal_instance *instance; | |
121 | u32 sustainable_power = 0; | |
122 | u32 min_power; | |
e055bb0f JM |
123 | |
124 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { | |
e3ecd571 | 125 | if (!power_actor_is_valid(params, instance)) |
8132df3a LL |
126 | continue; |
127 | ||
e3ecd571 | 128 | cdev = instance->cdev; |
8132df3a | 129 | if (cdev->ops->state2power(cdev, instance->upper, &min_power)) |
e055bb0f JM |
130 | continue; |
131 | ||
132 | sustainable_power += min_power; | |
133 | } | |
134 | ||
135 | return sustainable_power; | |
136 | } | |
137 | ||
138 | /** | |
139 | * estimate_pid_constants() - Estimate the constants for the PID controller | |
140 | * @tz: thermal zone for which to estimate the constants | |
141 | * @sustainable_power: sustainable power for the thermal zone | |
94be1d27 | 142 | * @trip_switch_on: trip point for the switch on temperature |
e055bb0f | 143 | * @control_temp: target temperature for the power allocator governor |
e055bb0f JM |
144 | * |
145 | * This function is used to update the estimation of the PID | |
146 | * controller constants in struct thermal_zone_parameters. | |
e055bb0f JM |
147 | */ |
148 | static void estimate_pid_constants(struct thermal_zone_device *tz, | |
94be1d27 RW |
149 | u32 sustainable_power, |
150 | const struct thermal_trip *trip_switch_on, | |
90a99654 | 151 | int control_temp) |
e055bb0f | 152 | { |
7f725a23 | 153 | u32 temperature_threshold = control_temp; |
e34a7233 | 154 | s32 k_i; |
e055bb0f | 155 | |
94be1d27 RW |
156 | if (trip_switch_on) |
157 | temperature_threshold -= trip_switch_on->temperature; | |
e055bb0f | 158 | |
44241628 AA |
159 | /* |
160 | * estimate_pid_constants() tries to find appropriate default | |
161 | * values for thermal zones that don't provide them. If a | |
162 | * system integrator has configured a thermal zone with two | |
163 | * passive trip points at the same temperature, that person | |
164 | * hasn't put any effort to set up the thermal zone properly | |
165 | * so just give up. | |
166 | */ | |
167 | if (!temperature_threshold) | |
168 | return; | |
e055bb0f | 169 | |
90a99654 LL |
170 | tz->tzp->k_po = int_to_frac(sustainable_power) / |
171 | temperature_threshold; | |
e055bb0f | 172 | |
90a99654 LL |
173 | tz->tzp->k_pu = int_to_frac(2 * sustainable_power) / |
174 | temperature_threshold; | |
e055bb0f | 175 | |
90a99654 LL |
176 | k_i = tz->tzp->k_pu / 10; |
177 | tz->tzp->k_i = k_i > 0 ? k_i : 1; | |
e34a7233 | 178 | |
e055bb0f JM |
179 | /* |
180 | * The default for k_d and integral_cutoff is 0, so we can | |
181 | * leave them as they are. | |
182 | */ | |
183 | } | |
184 | ||
eda1ecfa LL |
185 | /** |
186 | * get_sustainable_power() - Get the right sustainable power | |
187 | * @tz: thermal zone for which to estimate the constants | |
188 | * @params: parameters for the power allocator governor | |
189 | * @control_temp: target temperature for the power allocator governor | |
190 | * | |
191 | * This function is used for getting the proper sustainable power value based | |
192 | * on variables which might be updated by the user sysfs interface. If that | |
193 | * happen the new value is going to be estimated and updated. It is also used | |
194 | * after thermal zone binding, where the initial values where set to 0. | |
195 | */ | |
196 | static u32 get_sustainable_power(struct thermal_zone_device *tz, | |
197 | struct power_allocator_params *params, | |
198 | int control_temp) | |
199 | { | |
200 | u32 sustainable_power; | |
201 | ||
202 | if (!tz->tzp->sustainable_power) | |
203 | sustainable_power = estimate_sustainable_power(tz); | |
204 | else | |
205 | sustainable_power = tz->tzp->sustainable_power; | |
206 | ||
207 | /* Check if it's init value 0 or there was update via sysfs */ | |
208 | if (sustainable_power != params->sustainable_power) { | |
209 | estimate_pid_constants(tz, sustainable_power, | |
90a99654 | 210 | params->trip_switch_on, control_temp); |
eda1ecfa LL |
211 | |
212 | /* Do the estimation only once and make available in sysfs */ | |
213 | tz->tzp->sustainable_power = sustainable_power; | |
214 | params->sustainable_power = sustainable_power; | |
215 | } | |
216 | ||
217 | return sustainable_power; | |
218 | } | |
219 | ||
6b775e87 JM |
220 | /** |
221 | * pid_controller() - PID controller | |
222 | * @tz: thermal zone we are operating in | |
6b775e87 JM |
223 | * @control_temp: the target temperature in millicelsius |
224 | * @max_allocatable_power: maximum allocatable power for this thermal zone | |
225 | * | |
226 | * This PID controller increases the available power budget so that the | |
227 | * temperature of the thermal zone gets as close as possible to | |
228 | * @control_temp and limits the power if it exceeds it. k_po is the | |
229 | * proportional term when we are overshooting, k_pu is the | |
230 | * proportional term when we are undershooting. integral_cutoff is a | |
231 | * threshold below which we stop accumulating the error. The | |
232 | * accumulated error is only valid if the requested power will make | |
233 | * the system warmer. If the system is mostly idle, there's no point | |
234 | * in accumulating positive error. | |
235 | * | |
236 | * Return: The power budget for the next period. | |
237 | */ | |
238 | static u32 pid_controller(struct thermal_zone_device *tz, | |
17e8351a | 239 | int control_temp, |
6b775e87 JM |
240 | u32 max_allocatable_power) |
241 | { | |
499cc391 | 242 | struct power_allocator_params *params = tz->governor_data; |
6b775e87 JM |
243 | s64 p, i, d, power_range; |
244 | s32 err, max_power_frac; | |
e055bb0f | 245 | u32 sustainable_power; |
6b775e87 JM |
246 | |
247 | max_power_frac = int_to_frac(max_allocatable_power); | |
248 | ||
eda1ecfa | 249 | sustainable_power = get_sustainable_power(tz, params, control_temp); |
e055bb0f | 250 | |
bb404db4 | 251 | err = control_temp - tz->temperature; |
6b775e87 JM |
252 | err = int_to_frac(err); |
253 | ||
254 | /* Calculate the proportional term */ | |
255 | p = mul_frac(err < 0 ? tz->tzp->k_po : tz->tzp->k_pu, err); | |
256 | ||
257 | /* | |
258 | * Calculate the integral term | |
259 | * | |
260 | * if the error is less than cut off allow integration (but | |
261 | * the integral is limited to max power) | |
262 | */ | |
263 | i = mul_frac(tz->tzp->k_i, params->err_integral); | |
264 | ||
265 | if (err < int_to_frac(tz->tzp->integral_cutoff)) { | |
266 | s64 i_next = i + mul_frac(tz->tzp->k_i, err); | |
267 | ||
79211c8e | 268 | if (abs(i_next) < max_power_frac) { |
6b775e87 JM |
269 | i = i_next; |
270 | params->err_integral += err; | |
271 | } | |
272 | } | |
273 | ||
274 | /* | |
275 | * Calculate the derivative term | |
276 | * | |
277 | * We do err - prev_err, so with a positive k_d, a decreasing | |
278 | * error (i.e. driving closer to the line) results in less | |
279 | * power being applied, slowing down the controller) | |
280 | */ | |
281 | d = mul_frac(tz->tzp->k_d, err - params->prev_err); | |
b39d2dd5 | 282 | d = div_frac(d, jiffies_to_msecs(tz->passive_delay_jiffies)); |
6b775e87 JM |
283 | params->prev_err = err; |
284 | ||
285 | power_range = p + i + d; | |
286 | ||
287 | /* feed-forward the known sustainable dissipatable power */ | |
e055bb0f | 288 | power_range = sustainable_power + frac_to_int(power_range); |
6b775e87 | 289 | |
6828a471 JM |
290 | power_range = clamp(power_range, (s64)0, (s64)max_allocatable_power); |
291 | ||
292 | trace_thermal_power_allocator_pid(tz, frac_to_int(err), | |
293 | frac_to_int(params->err_integral), | |
294 | frac_to_int(p), frac_to_int(i), | |
295 | frac_to_int(d), power_range); | |
296 | ||
297 | return power_range; | |
6b775e87 JM |
298 | } |
299 | ||
345a8af7 LL |
300 | /** |
301 | * power_actor_set_power() - limit the maximum power a cooling device consumes | |
302 | * @cdev: pointer to &thermal_cooling_device | |
303 | * @instance: thermal instance to update | |
304 | * @power: the power in milliwatts | |
305 | * | |
306 | * Set the cooling device to consume at most @power milliwatts. The limit is | |
307 | * expected to be a cap at the maximum power consumption. | |
308 | * | |
309 | * Return: 0 on success, -EINVAL if the cooling device does not | |
310 | * implement the power actor API or -E* for other failures. | |
311 | */ | |
312 | static int | |
313 | power_actor_set_power(struct thermal_cooling_device *cdev, | |
314 | struct thermal_instance *instance, u32 power) | |
315 | { | |
316 | unsigned long state; | |
317 | int ret; | |
318 | ||
319 | ret = cdev->ops->power2state(cdev, power, &state); | |
320 | if (ret) | |
321 | return ret; | |
322 | ||
323 | instance->target = clamp_val(state, instance->lower, instance->upper); | |
324 | mutex_lock(&cdev->lock); | |
ab39c885 | 325 | __thermal_cdev_update(cdev); |
345a8af7 | 326 | mutex_unlock(&cdev->lock); |
345a8af7 LL |
327 | |
328 | return 0; | |
329 | } | |
330 | ||
6b775e87 JM |
331 | /** |
332 | * divvy_up_power() - divvy the allocated power between the actors | |
912e97c6 LL |
333 | * @power: buffer for all power actors internal power information |
334 | * @num_actors: number of power actors in this thermal zone | |
335 | * @total_req_power: sum of all weighted requested power for all actors | |
6b775e87 | 336 | * @power_range: total allocated power |
6b775e87 JM |
337 | * |
338 | * This function divides the total allocated power (@power_range) | |
339 | * fairly between the actors. It first tries to give each actor a | |
340 | * share of the @power_range according to how much power it requested | |
341 | * compared to the rest of the actors. For example, if only one actor | |
342 | * requests power, then it receives all the @power_range. If | |
343 | * three actors each requests 1mW, each receives a third of the | |
344 | * @power_range. | |
345 | * | |
346 | * If any actor received more than their maximum power, then that | |
347 | * surplus is re-divvied among the actors based on how far they are | |
348 | * from their respective maximums. | |
6b775e87 | 349 | */ |
912e97c6 LL |
350 | static void divvy_up_power(struct power_actor *power, int num_actors, |
351 | u32 total_req_power, u32 power_range) | |
6b775e87 | 352 | { |
3d827317 LL |
353 | u32 capped_extra_power = 0; |
354 | u32 extra_power = 0; | |
6b775e87 JM |
355 | int i; |
356 | ||
357 | /* | |
358 | * Prevent division by 0 if none of the actors request power. | |
359 | */ | |
360 | if (!total_req_power) | |
361 | total_req_power = 1; | |
362 | ||
6b775e87 | 363 | for (i = 0; i < num_actors; i++) { |
912e97c6 LL |
364 | struct power_actor *pa = &power[i]; |
365 | u64 req_range = (u64)pa->req_power * power_range; | |
6b775e87 | 366 | |
912e97c6 LL |
367 | pa->granted_power = DIV_ROUND_CLOSEST_ULL(req_range, |
368 | total_req_power); | |
6b775e87 | 369 | |
912e97c6 LL |
370 | if (pa->granted_power > pa->max_power) { |
371 | extra_power += pa->granted_power - pa->max_power; | |
372 | pa->granted_power = pa->max_power; | |
6b775e87 JM |
373 | } |
374 | ||
912e97c6 LL |
375 | pa->extra_actor_power = pa->max_power - pa->granted_power; |
376 | capped_extra_power += pa->extra_actor_power; | |
6b775e87 JM |
377 | } |
378 | ||
3d827317 | 379 | if (!extra_power || !capped_extra_power) |
6b775e87 JM |
380 | return; |
381 | ||
382 | /* | |
383 | * Re-divvy the reclaimed extra among actors based on | |
384 | * how far they are from the max | |
385 | */ | |
386 | extra_power = min(extra_power, capped_extra_power); | |
3d827317 LL |
387 | |
388 | for (i = 0; i < num_actors; i++) { | |
912e97c6 LL |
389 | struct power_actor *pa = &power[i]; |
390 | u64 extra_range = pa->extra_actor_power; | |
3d827317 | 391 | |
912e97c6 LL |
392 | extra_range *= extra_power; |
393 | pa->granted_power += DIV_ROUND_CLOSEST_ULL(extra_range, | |
394 | capped_extra_power); | |
3d827317 | 395 | } |
6b775e87 JM |
396 | } |
397 | ||
499cc391 | 398 | static int allocate_power(struct thermal_zone_device *tz, int control_temp) |
6b775e87 | 399 | { |
6b775e87 | 400 | struct power_allocator_params *params = tz->governor_data; |
912e97c6 LL |
401 | unsigned int num_actors = params->num_actors; |
402 | struct power_actor *power = params->power; | |
499cc391 LL |
403 | struct thermal_cooling_device *cdev; |
404 | struct thermal_instance *instance; | |
401888e7 LL |
405 | u32 total_weighted_req_power = 0; |
406 | u32 max_allocatable_power = 0; | |
407 | u32 total_granted_power = 0; | |
408 | u32 total_req_power = 0; | |
401888e7 | 409 | u32 power_range, weight; |
912e97c6 | 410 | int i = 0, ret; |
6b775e87 | 411 | |
63561fe3 DL |
412 | if (!num_actors) |
413 | return -ENODEV; | |
97584d18 | 414 | |
912e97c6 LL |
415 | /* Clean all buffers for new power estimations */ |
416 | memset(power, 0, params->buffer_size); | |
6b775e87 | 417 | |
6b775e87 | 418 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { |
912e97c6 LL |
419 | struct power_actor *pa = &power[i]; |
420 | ||
e3ecd571 | 421 | if (!power_actor_is_valid(params, instance)) |
6b775e87 JM |
422 | continue; |
423 | ||
e3ecd571 | 424 | cdev = instance->cdev; |
6b775e87 | 425 | |
912e97c6 LL |
426 | ret = cdev->ops->get_requested_power(cdev, &pa->req_power); |
427 | if (ret) | |
6b775e87 JM |
428 | continue; |
429 | ||
a3cd6db4 | 430 | if (!params->total_weight) |
6b775e87 JM |
431 | weight = 1 << FRAC_BITS; |
432 | else | |
433 | weight = instance->weight; | |
434 | ||
912e97c6 | 435 | pa->weighted_req_power = frac_to_int(weight * pa->req_power); |
6b775e87 | 436 | |
912e97c6 LL |
437 | ret = cdev->ops->state2power(cdev, instance->lower, |
438 | &pa->max_power); | |
439 | if (ret) | |
6b775e87 JM |
440 | continue; |
441 | ||
912e97c6 LL |
442 | total_req_power += pa->req_power; |
443 | max_allocatable_power += pa->max_power; | |
444 | total_weighted_req_power += pa->weighted_req_power; | |
6b775e87 JM |
445 | |
446 | i++; | |
447 | } | |
448 | ||
bb404db4 | 449 | power_range = pid_controller(tz, control_temp, max_allocatable_power); |
6b775e87 | 450 | |
912e97c6 LL |
451 | divvy_up_power(power, num_actors, total_weighted_req_power, |
452 | power_range); | |
6b775e87 JM |
453 | |
454 | i = 0; | |
455 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { | |
912e97c6 LL |
456 | struct power_actor *pa = &power[i]; |
457 | ||
e3ecd571 | 458 | if (!power_actor_is_valid(params, instance)) |
6b775e87 JM |
459 | continue; |
460 | ||
461 | power_actor_set_power(instance->cdev, instance, | |
912e97c6 LL |
462 | pa->granted_power); |
463 | total_granted_power += pa->granted_power; | |
6b775e87 | 464 | |
912e97c6 LL |
465 | trace_thermal_power_actor(tz, i, pa->req_power, |
466 | pa->granted_power); | |
6b775e87 JM |
467 | i++; |
468 | } | |
469 | ||
792c3dc0 | 470 | trace_thermal_power_allocator(tz, total_req_power, total_granted_power, |
6828a471 | 471 | num_actors, power_range, |
bb404db4 KS |
472 | max_allocatable_power, tz->temperature, |
473 | control_temp - tz->temperature); | |
6828a471 | 474 | |
0458d536 | 475 | return 0; |
6b775e87 JM |
476 | } |
477 | ||
8b7b390f | 478 | /** |
94be1d27 | 479 | * get_governor_trips() - get the two trip points that are key for this governor |
8b7b390f JM |
480 | * @tz: thermal zone to operate on |
481 | * @params: pointer to private data for this governor | |
482 | * | |
483 | * The power allocator governor works optimally with two trips points: | |
484 | * a "switch on" trip point and a "maximum desired temperature". These | |
485 | * are defined as the first and last passive trip points. | |
486 | * | |
487 | * If there is only one trip point, then that's considered to be the | |
488 | * "maximum desired temperature" trip point and the governor is always | |
489 | * on. If there are no passive or active trip points, then the | |
490 | * governor won't do anything. In fact, its throttle function | |
491 | * won't be called at all. | |
492 | */ | |
493 | static void get_governor_trips(struct thermal_zone_device *tz, | |
494 | struct power_allocator_params *params) | |
6b775e87 | 495 | { |
94be1d27 RW |
496 | const struct thermal_trip *first_passive = NULL; |
497 | const struct thermal_trip *last_passive = NULL; | |
498 | const struct thermal_trip *last_active = NULL; | |
499 | const struct thermal_trip *trip; | |
500 | ||
501 | for_each_trip(tz, trip) { | |
502 | switch (trip->type) { | |
503 | case THERMAL_TRIP_PASSIVE: | |
504 | if (!first_passive) { | |
505 | first_passive = trip; | |
506 | break; | |
6b775e87 | 507 | } |
94be1d27 RW |
508 | last_passive = trip; |
509 | break; | |
510 | case THERMAL_TRIP_ACTIVE: | |
511 | last_active = trip; | |
512 | break; | |
513 | default: | |
6b775e87 JM |
514 | break; |
515 | } | |
516 | } | |
517 | ||
94be1d27 RW |
518 | if (last_passive) { |
519 | params->trip_switch_on = first_passive; | |
4e6d4687 | 520 | params->trip_max = last_passive; |
94be1d27 RW |
521 | } else if (first_passive) { |
522 | params->trip_switch_on = NULL; | |
4e6d4687 | 523 | params->trip_max = first_passive; |
6b775e87 | 524 | } else { |
94be1d27 | 525 | params->trip_switch_on = NULL; |
4e6d4687 | 526 | params->trip_max = last_active; |
6b775e87 | 527 | } |
6b775e87 JM |
528 | } |
529 | ||
530 | static void reset_pid_controller(struct power_allocator_params *params) | |
531 | { | |
532 | params->err_integral = 0; | |
533 | params->prev_err = 0; | |
534 | } | |
535 | ||
0952177f | 536 | static void allow_maximum_power(struct thermal_zone_device *tz, bool update) |
6b775e87 | 537 | { |
6b775e87 | 538 | struct power_allocator_params *params = tz->governor_data; |
499cc391 LL |
539 | struct thermal_cooling_device *cdev; |
540 | struct thermal_instance *instance; | |
d3b60ed9 | 541 | u32 req_power; |
6b775e87 JM |
542 | |
543 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { | |
e3ecd571 | 544 | if (!power_actor_is_valid(params, instance)) |
6b775e87 JM |
545 | continue; |
546 | ||
e3ecd571 LL |
547 | cdev = instance->cdev; |
548 | ||
6b775e87 | 549 | instance->target = 0; |
30e1178c | 550 | mutex_lock(&cdev->lock); |
d3b60ed9 LL |
551 | /* |
552 | * Call for updating the cooling devices local stats and avoid | |
553 | * periods of dozen of seconds when those have not been | |
554 | * maintained. | |
555 | */ | |
556 | cdev->ops->get_requested_power(cdev, &req_power); | |
557 | ||
0952177f | 558 | if (update) |
30e1178c | 559 | __thermal_cdev_update(cdev); |
0952177f | 560 | |
30e1178c | 561 | mutex_unlock(&cdev->lock); |
6b775e87 JM |
562 | } |
563 | } | |
564 | ||
7a583405 LL |
565 | /** |
566 | * check_power_actors() - Check all cooling devices and warn when they are | |
567 | * not power actors | |
568 | * @tz: thermal zone to operate on | |
c7568e78 | 569 | * @params: power allocator private data |
7a583405 LL |
570 | * |
571 | * Check all cooling devices in the @tz and warn every time they are missing | |
572 | * power actor API. The warning should help to investigate the issue, which | |
573 | * could be e.g. lack of Energy Model for a given device. | |
574 | * | |
2c06456f LL |
575 | * If all of the cooling devices currently attached to @tz implement the power |
576 | * actor API, return the number of them (which may be 0, because some cooling | |
577 | * devices may be attached later). Otherwise, return -EINVAL. | |
7a583405 | 578 | */ |
c7568e78 LL |
579 | static int check_power_actors(struct thermal_zone_device *tz, |
580 | struct power_allocator_params *params) | |
7a583405 LL |
581 | { |
582 | struct thermal_instance *instance; | |
583 | int ret = 0; | |
584 | ||
585 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { | |
c7568e78 LL |
586 | if (instance->trip != params->trip_max) |
587 | continue; | |
588 | ||
7a583405 LL |
589 | if (!cdev_is_power_actor(instance->cdev)) { |
590 | dev_warn(&tz->device, "power_allocator: %s is not a power actor\n", | |
591 | instance->cdev->type); | |
2c06456f | 592 | return -EINVAL; |
7a583405 | 593 | } |
2c06456f | 594 | ret++; |
7a583405 LL |
595 | } |
596 | ||
597 | return ret; | |
598 | } | |
599 | ||
912e97c6 LL |
600 | static int allocate_actors_buffer(struct power_allocator_params *params, |
601 | int num_actors) | |
602 | { | |
603 | int ret; | |
604 | ||
605 | kfree(params->power); | |
606 | ||
607 | /* There might be no cooling devices yet. */ | |
608 | if (!num_actors) { | |
1057c4c3 | 609 | ret = 0; |
912e97c6 LL |
610 | goto clean_state; |
611 | } | |
612 | ||
613 | params->power = kcalloc(num_actors, sizeof(struct power_actor), | |
614 | GFP_KERNEL); | |
615 | if (!params->power) { | |
616 | ret = -ENOMEM; | |
617 | goto clean_state; | |
618 | } | |
619 | ||
620 | params->num_actors = num_actors; | |
621 | params->buffer_size = num_actors * sizeof(struct power_actor); | |
622 | ||
623 | return 0; | |
624 | ||
625 | clean_state: | |
626 | params->num_actors = 0; | |
627 | params->buffer_size = 0; | |
628 | params->power = NULL; | |
629 | return ret; | |
630 | } | |
631 | ||
632 | static void power_allocator_update_tz(struct thermal_zone_device *tz, | |
633 | enum thermal_notify_event reason) | |
634 | { | |
635 | struct power_allocator_params *params = tz->governor_data; | |
636 | struct thermal_instance *instance; | |
637 | int num_actors = 0; | |
638 | ||
639 | switch (reason) { | |
640 | case THERMAL_TZ_BIND_CDEV: | |
641 | case THERMAL_TZ_UNBIND_CDEV: | |
642 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) | |
e3ecd571 | 643 | if (power_actor_is_valid(params, instance)) |
912e97c6 LL |
644 | num_actors++; |
645 | ||
646 | if (num_actors == params->num_actors) | |
647 | return; | |
648 | ||
649 | allocate_actors_buffer(params, num_actors); | |
650 | break; | |
a3cd6db4 LL |
651 | case THERMAL_INSTANCE_WEIGHT_CHANGED: |
652 | params->total_weight = 0; | |
653 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) | |
654 | if (power_actor_is_valid(params, instance)) | |
655 | params->total_weight += instance->weight; | |
656 | break; | |
912e97c6 LL |
657 | default: |
658 | break; | |
659 | } | |
660 | } | |
661 | ||
6b775e87 JM |
662 | /** |
663 | * power_allocator_bind() - bind the power_allocator governor to a thermal zone | |
664 | * @tz: thermal zone to bind it to | |
665 | * | |
8b7b390f JM |
666 | * Initialize the PID controller parameters and bind it to the thermal |
667 | * zone. | |
6b775e87 | 668 | * |
7a583405 LL |
669 | * Return: 0 on success, or -ENOMEM if we ran out of memory, or -EINVAL |
670 | * when there are unsupported cooling devices in the @tz. | |
6b775e87 JM |
671 | */ |
672 | static int power_allocator_bind(struct thermal_zone_device *tz) | |
673 | { | |
6b775e87 | 674 | struct power_allocator_params *params; |
499cc391 | 675 | int ret; |
6b775e87 | 676 | |
cf736ea6 | 677 | params = kzalloc(sizeof(*params), GFP_KERNEL); |
6b775e87 JM |
678 | if (!params) |
679 | return -ENOMEM; | |
680 | ||
e83747c2 | 681 | get_governor_trips(tz, params); |
e83747c2 | 682 | |
c7568e78 | 683 | ret = check_power_actors(tz, params); |
2c06456f | 684 | if (ret < 0) { |
e83747c2 LL |
685 | dev_warn(&tz->device, "power_allocator: binding failed\n"); |
686 | kfree(params); | |
687 | return ret; | |
688 | } | |
689 | ||
912e97c6 LL |
690 | ret = allocate_actors_buffer(params, ret); |
691 | if (ret) { | |
692 | dev_warn(&tz->device, "power_allocator: allocation failed\n"); | |
693 | kfree(params); | |
694 | return ret; | |
695 | } | |
696 | ||
f5cbb182 JM |
697 | if (!tz->tzp) { |
698 | tz->tzp = kzalloc(sizeof(*tz->tzp), GFP_KERNEL); | |
699 | if (!tz->tzp) { | |
700 | ret = -ENOMEM; | |
701 | goto free_params; | |
702 | } | |
703 | ||
704 | params->allocated_tzp = true; | |
705 | } | |
706 | ||
e055bb0f JM |
707 | if (!tz->tzp->sustainable_power) |
708 | dev_warn(&tz->device, "power_allocator: sustainable_power will be estimated\n"); | |
0fac6893 DS |
709 | else |
710 | params->sustainable_power = tz->tzp->sustainable_power; | |
e055bb0f | 711 | |
da781936 NT |
712 | if (params->trip_max) |
713 | estimate_pid_constants(tz, tz->tzp->sustainable_power, | |
714 | params->trip_switch_on, | |
715 | params->trip_max->temperature); | |
6b775e87 | 716 | |
6b775e87 JM |
717 | reset_pid_controller(params); |
718 | ||
719 | tz->governor_data = params; | |
720 | ||
721 | return 0; | |
f5cbb182 JM |
722 | |
723 | free_params: | |
912e97c6 | 724 | kfree(params->power); |
f5cbb182 JM |
725 | kfree(params); |
726 | ||
727 | return ret; | |
6b775e87 JM |
728 | } |
729 | ||
730 | static void power_allocator_unbind(struct thermal_zone_device *tz) | |
731 | { | |
f5cbb182 JM |
732 | struct power_allocator_params *params = tz->governor_data; |
733 | ||
6b775e87 | 734 | dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id); |
f5cbb182 JM |
735 | |
736 | if (params->allocated_tzp) { | |
737 | kfree(tz->tzp); | |
738 | tz->tzp = NULL; | |
739 | } | |
740 | ||
912e97c6 | 741 | kfree(params->power); |
cf736ea6 | 742 | kfree(tz->governor_data); |
6b775e87 JM |
743 | tz->governor_data = NULL; |
744 | } | |
745 | ||
8c35b1f4 RW |
746 | static int power_allocator_throttle(struct thermal_zone_device *tz, |
747 | const struct thermal_trip *trip) | |
6b775e87 | 748 | { |
6b775e87 | 749 | struct power_allocator_params *params = tz->governor_data; |
0952177f | 750 | bool update; |
6b775e87 | 751 | |
670a5e35 | 752 | lockdep_assert_held(&tz->lock); |
63561fe3 | 753 | |
6b775e87 JM |
754 | /* |
755 | * We get called for every trip point but we only need to do | |
756 | * our calculations once | |
757 | */ | |
4e6d4687 | 758 | if (trip != params->trip_max) |
670a5e35 | 759 | return 0; |
6b775e87 | 760 | |
94be1d27 RW |
761 | trip = params->trip_switch_on; |
762 | if (trip && tz->temperature < trip->temperature) { | |
e95fa740 | 763 | update = tz->passive; |
6b775e87 JM |
764 | tz->passive = 0; |
765 | reset_pid_controller(params); | |
0952177f | 766 | allow_maximum_power(tz, update); |
670a5e35 | 767 | return 0; |
6b775e87 JM |
768 | } |
769 | ||
770 | tz->passive = 1; | |
771 | ||
4e6d4687 | 772 | return allocate_power(tz, params->trip_max->temperature); |
6b775e87 JM |
773 | } |
774 | ||
775 | static struct thermal_governor thermal_gov_power_allocator = { | |
776 | .name = "power_allocator", | |
777 | .bind_to_tz = power_allocator_bind, | |
778 | .unbind_from_tz = power_allocator_unbind, | |
779 | .throttle = power_allocator_throttle, | |
912e97c6 | 780 | .update_tz = power_allocator_update_tz, |
6b775e87 | 781 | }; |
57c5b2ec | 782 | THERMAL_GOVERNOR_DECLARE(thermal_gov_power_allocator); |