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