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