Linux Audio

Check our new training course

Loading...
v6.8
  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 */
 31static 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 */
 44static 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 */
 57struct 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 * @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.
 72 * @sustainable_power:	Sustainable power (heat) that this thermal zone can
 73 *			dissipate
 74 * @trip_switch_on:	first passive trip point of the thermal zone.  The
 75 *			governor switches on when this trip point is crossed.
 76 *			If the thermal zone only has one passive trip point,
 77 *			@trip_switch_on should be NULL.
 78 * @trip_max:		last passive trip point of the thermal zone. The
 79 *			temperature we are controlling for.
 80 * @total_weight:	Sum of all thermal instances weights
 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
 84 */
 85struct power_allocator_params {
 86	bool allocated_tzp;
 87	s64 err_integral;
 88	s32 prev_err;
 
 
 89	u32 sustainable_power;
 90	const struct thermal_trip *trip_switch_on;
 91	const struct thermal_trip *trip_max;
 92	int total_weight;
 93	unsigned int num_actors;
 94	unsigned int buffer_size;
 95	struct power_actor *power;
 96};
 97
 98static 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
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 */
116static u32 estimate_sustainable_power(struct thermal_zone_device *tz)
117{
118	struct power_allocator_params *params = tz->governor_data;
119	struct thermal_cooling_device *cdev;
120	struct thermal_instance *instance;
121	u32 sustainable_power = 0;
122	u32 min_power;
 
123
124	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
125		if (!power_actor_is_valid(params, instance))
 
 
 
 
 
 
126			continue;
127
128		cdev = instance->cdev;
129		if (cdev->ops->state2power(cdev, instance->upper, &min_power))
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
142 * @trip_switch_on:	trip point for the switch on temperature
143 * @control_temp:	target temperature for the power allocator governor
144 *
145 * This function is used to update the estimation of the PID
146 * controller constants in struct thermal_zone_parameters.
147 */
148static void estimate_pid_constants(struct thermal_zone_device *tz,
149				   u32 sustainable_power,
150				   const struct thermal_trip *trip_switch_on,
151				   int control_temp)
152{
153	u32 temperature_threshold = control_temp;
 
 
154	s32 k_i;
155
156	if (trip_switch_on)
157		temperature_threshold -= trip_switch_on->temperature;
 
158
 
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;
169
170	tz->tzp->k_po = int_to_frac(sustainable_power) /
171		temperature_threshold;
172
173	tz->tzp->k_pu = int_to_frac(2 * sustainable_power) /
174		temperature_threshold;
175
176	k_i = tz->tzp->k_pu / 10;
177	tz->tzp->k_i = k_i > 0 ? k_i : 1;
178
179	/*
180	 * The default for k_d and integral_cutoff is 0, so we can
181	 * leave them as they are.
182	 */
183}
184
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 */
196static 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,
210				       params->trip_switch_on, control_temp);
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
220/**
221 * pid_controller() - PID controller
222 * @tz:	thermal zone we are operating in
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 */
238static u32 pid_controller(struct thermal_zone_device *tz,
239			  int control_temp,
240			  u32 max_allocatable_power)
241{
242	struct power_allocator_params *params = tz->governor_data;
243	s64 p, i, d, power_range;
244	s32 err, max_power_frac;
245	u32 sustainable_power;
 
246
247	max_power_frac = int_to_frac(max_allocatable_power);
248
249	sustainable_power = get_sustainable_power(tz, params, control_temp);
250
251	err = control_temp - tz->temperature;
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
268		if (abs(i_next) < max_power_frac) {
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);
282	d = div_frac(d, jiffies_to_msecs(tz->passive_delay_jiffies));
283	params->prev_err = err;
284
285	power_range = p + i + d;
286
287	/* feed-forward the known sustainable dissipatable power */
288	power_range = sustainable_power + frac_to_int(power_range);
289
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;
298}
299
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 */
312static int
313power_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);
325	__thermal_cdev_update(cdev);
326	mutex_unlock(&cdev->lock);
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 */
350static 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	/*
358	 * Prevent division by 0 if none of the actors request power.
359	 */
360	if (!total_req_power)
361		total_req_power = 1;
362
 
 
363	for (i = 0; i < num_actors; i++) {
364		struct power_actor *pa = &power[i];
365		u64 req_range = (u64)pa->req_power * power_range;
366
367		pa->granted_power = DIV_ROUND_CLOSEST_ULL(req_range,
368							  total_req_power);
369
370		if (pa->granted_power > pa->max_power) {
371			extra_power += pa->granted_power - pa->max_power;
372			pa->granted_power = pa->max_power;
373		}
374
375		pa->extra_actor_power = pa->max_power - pa->granted_power;
376		capped_extra_power += pa->extra_actor_power;
377	}
378
379	if (!extra_power || !capped_extra_power)
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);
387
388	for (i = 0; i < num_actors; i++) {
389		struct power_actor *pa = &power[i];
390		u64 extra_range = pa->extra_actor_power;
391
392		extra_range *= extra_power;
393		pa->granted_power += DIV_ROUND_CLOSEST_ULL(extra_range,
394						capped_extra_power);
395	}
396}
397
398static int allocate_power(struct thermal_zone_device *tz, int control_temp)
 
399{
400	struct power_allocator_params *params = tz->governor_data;
401	unsigned int num_actors = params->num_actors;
402	struct power_actor *power = params->power;
403	struct thermal_cooling_device *cdev;
404	struct thermal_instance *instance;
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;
409	u32 power_range, weight;
410	int i = 0, ret;
 
411
412	if (!num_actors)
413		return -ENODEV;
 
 
 
 
 
 
 
 
 
414
415	/* Clean all buffers for new power estimations */
416	memset(power, 0, params->buffer_size);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417
418	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
419		struct power_actor *pa = &power[i];
 
420
421		if (!power_actor_is_valid(params, instance))
422			continue;
423
424		cdev = instance->cdev;
 
425
426		ret = cdev->ops->get_requested_power(cdev, &pa->req_power);
427		if (ret)
428			continue;
429
430		if (!params->total_weight)
431			weight = 1 << FRAC_BITS;
432		else
433			weight = instance->weight;
434
435		pa->weighted_req_power = frac_to_int(weight * pa->req_power);
436
437		ret = cdev->ops->state2power(cdev, instance->lower,
438					     &pa->max_power);
439		if (ret)
440			continue;
441
442		total_req_power += pa->req_power;
443		max_allocatable_power += pa->max_power;
444		total_weighted_req_power += pa->weighted_req_power;
445
446		i++;
447	}
448
449	power_range = pid_controller(tz, control_temp, max_allocatable_power);
450
451	divvy_up_power(power, num_actors, total_weighted_req_power,
452		       power_range);
 
453
 
454	i = 0;
455	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
456		struct power_actor *pa = &power[i];
 
457
458		if (!power_actor_is_valid(params, instance))
459			continue;
460
461		power_actor_set_power(instance->cdev, instance,
462				      pa->granted_power);
463		total_granted_power += pa->granted_power;
464
465		trace_thermal_power_actor(tz, i, pa->req_power,
466					  pa->granted_power);
467		i++;
468	}
469
470	trace_thermal_power_allocator(tz, total_req_power, total_granted_power,
 
471				      num_actors, power_range,
472				      max_allocatable_power, tz->temperature,
473				      control_temp - tz->temperature);
474
475	return 0;
 
 
 
 
476}
477
478/**
479 * get_governor_trips() - get the two trip points that are key for this governor
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 */
493static void get_governor_trips(struct thermal_zone_device *tz,
494			       struct power_allocator_params *params)
495{
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;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
507			}
508			last_passive = trip;
509			break;
510		case THERMAL_TRIP_ACTIVE:
511			last_active = trip;
512			break;
513		default:
514			break;
515		}
516	}
517
518	if (last_passive) {
519		params->trip_switch_on = first_passive;
520		params->trip_max = last_passive;
521	} else if (first_passive) {
522		params->trip_switch_on = NULL;
523		params->trip_max = first_passive;
524	} else {
525		params->trip_switch_on = NULL;
526		params->trip_max = last_active;
527	}
528}
529
530static void reset_pid_controller(struct power_allocator_params *params)
531{
532	params->err_integral = 0;
533	params->prev_err = 0;
534}
535
536static void allow_maximum_power(struct thermal_zone_device *tz, bool update)
537{
538	struct power_allocator_params *params = tz->governor_data;
539	struct thermal_cooling_device *cdev;
540	struct thermal_instance *instance;
 
541	u32 req_power;
542
 
543	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
544		if (!power_actor_is_valid(params, instance))
545			continue;
546
547		cdev = instance->cdev;
 
 
548
549		instance->target = 0;
550		mutex_lock(&cdev->lock);
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
558		if (update)
559			__thermal_cdev_update(cdev);
560
561		mutex_unlock(&cdev->lock);
562	}
 
563}
564
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
569 * @params:	power allocator private data
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 *
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.
578 */
579static int check_power_actors(struct thermal_zone_device *tz,
580			      struct power_allocator_params *params)
581{
582	struct thermal_instance *instance;
583	int ret = 0;
584
585	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
586		if (instance->trip != params->trip_max)
587			continue;
588
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);
592			return -EINVAL;
593		}
594		ret++;
595	}
596
597	return ret;
598}
599
600static 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) {
609		ret = -EINVAL;
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
625clean_state:
626	params->num_actors = 0;
627	params->buffer_size = 0;
628	params->power = NULL;
629	return ret;
630}
631
632static 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)
643			if (power_actor_is_valid(params, instance))
644				num_actors++;
645
646		if (num_actors == params->num_actors)
647			return;
648
649		allocate_actors_buffer(params, num_actors);
650		break;
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;
657	default:
658		break;
659	}
660}
661
662/**
663 * power_allocator_bind() - bind the power_allocator governor to a thermal zone
664 * @tz:	thermal zone to bind it to
665 *
666 * Initialize the PID controller parameters and bind it to the thermal
667 * zone.
668 *
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.
671 */
672static int power_allocator_bind(struct thermal_zone_device *tz)
673{
674	struct power_allocator_params *params;
675	int ret;
 
 
 
 
 
 
676
677	params = kzalloc(sizeof(*params), GFP_KERNEL);
678	if (!params)
679		return -ENOMEM;
680
681	get_governor_trips(tz, params);
682	if (!params->trip_max) {
683		dev_warn(&tz->device, "power_allocator: missing trip_max\n");
684		kfree(params);
685		return -EINVAL;
686	}
687
688	ret = check_power_actors(tz, params);
689	if (ret < 0) {
690		dev_warn(&tz->device, "power_allocator: binding failed\n");
691		kfree(params);
692		return ret;
693	}
694
695	ret = allocate_actors_buffer(params, ret);
696	if (ret) {
697		dev_warn(&tz->device, "power_allocator: allocation failed\n");
698		kfree(params);
699		return ret;
700	}
701
702	if (!tz->tzp) {
703		tz->tzp = kzalloc(sizeof(*tz->tzp), GFP_KERNEL);
704		if (!tz->tzp) {
705			ret = -ENOMEM;
706			goto free_params;
707		}
708
709		params->allocated_tzp = true;
710	}
711
712	if (!tz->tzp->sustainable_power)
713		dev_warn(&tz->device, "power_allocator: sustainable_power will be estimated\n");
714
715	estimate_pid_constants(tz, tz->tzp->sustainable_power,
716			       params->trip_switch_on,
717			       params->trip_max->temperature);
 
 
 
 
 
 
 
 
718
719	reset_pid_controller(params);
720
721	tz->governor_data = params;
722
723	return 0;
724
725free_params:
726	kfree(params->power);
727	kfree(params);
728
729	return ret;
730}
731
732static void power_allocator_unbind(struct thermal_zone_device *tz)
733{
734	struct power_allocator_params *params = tz->governor_data;
735
736	dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id);
737
738	if (params->allocated_tzp) {
739		kfree(tz->tzp);
740		tz->tzp = NULL;
741	}
742
743	kfree(params->power);
744	kfree(tz->governor_data);
745	tz->governor_data = NULL;
746}
747
748static int power_allocator_throttle(struct thermal_zone_device *tz,
749				    const struct thermal_trip *trip)
750{
 
 
751	struct power_allocator_params *params = tz->governor_data;
752	bool update;
753
754	lockdep_assert_held(&tz->lock);
755
756	/*
757	 * We get called for every trip point but we only need to do
758	 * our calculations once
759	 */
760	if (trip != params->trip_max)
761		return 0;
762
763	trip = params->trip_switch_on;
764	if (trip && tz->temperature < trip->temperature) {
765		update = tz->passive;
 
766		tz->passive = 0;
767		reset_pid_controller(params);
768		allow_maximum_power(tz, update);
769		return 0;
770	}
771
772	tz->passive = 1;
773
774	return allocate_power(tz, params->trip_max->temperature);
 
 
 
 
 
 
 
 
 
775}
776
777static struct thermal_governor thermal_gov_power_allocator = {
778	.name		= "power_allocator",
779	.bind_to_tz	= power_allocator_bind,
780	.unbind_from_tz	= power_allocator_unbind,
781	.throttle	= power_allocator_throttle,
782	.update_tz	= power_allocator_update_tz,
783};
784THERMAL_GOVERNOR_DECLARE(thermal_gov_power_allocator);
v5.14.15
  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/rculist.h>
 12#include <linux/slab.h>
 13#include <linux/thermal.h>
 14
 15#define CREATE_TRACE_POINTS
 16#include <trace/events/thermal_power_allocator.h>
 17
 18#include "thermal_core.h"
 19
 20#define INVALID_TRIP -1
 21
 22#define FRAC_BITS 10
 23#define int_to_frac(x) ((x) << FRAC_BITS)
 24#define frac_to_int(x) ((x) >> FRAC_BITS)
 25
 26/**
 27 * mul_frac() - multiply two fixed-point numbers
 28 * @x:	first multiplicand
 29 * @y:	second multiplicand
 30 *
 31 * Return: the result of multiplying two fixed-point numbers.  The
 32 * result is also a fixed-point number.
 33 */
 34static inline s64 mul_frac(s64 x, s64 y)
 35{
 36	return (x * y) >> FRAC_BITS;
 37}
 38
 39/**
 40 * div_frac() - divide two fixed-point numbers
 41 * @x:	the dividend
 42 * @y:	the divisor
 43 *
 44 * Return: the result of dividing two fixed-point numbers.  The
 45 * result is also a fixed-point number.
 46 */
 47static inline s64 div_frac(s64 x, s64 y)
 48{
 49	return div_s64(x << FRAC_BITS, y);
 50}
 51
 52/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 53 * struct power_allocator_params - parameters for the power allocator governor
 54 * @allocated_tzp:	whether we have allocated tzp for this thermal zone and
 55 *			it needs to be freed on unbind
 56 * @err_integral:	accumulated error in the PID controller.
 57 * @prev_err:	error in the previous iteration of the PID controller.
 58 *		Used to calculate the derivative term.
 
 
 59 * @trip_switch_on:	first passive trip point of the thermal zone.  The
 60 *			governor switches on when this trip point is crossed.
 61 *			If the thermal zone only has one passive trip point,
 62 *			@trip_switch_on should be INVALID_TRIP.
 63 * @trip_max_desired_temperature:	last passive trip point of the thermal
 64 *					zone.  The temperature we are
 65 *					controlling for.
 66 * @sustainable_power:	Sustainable power (heat) that this thermal zone can
 67 *			dissipate
 
 68 */
 69struct power_allocator_params {
 70	bool allocated_tzp;
 71	s64 err_integral;
 72	s32 prev_err;
 73	int trip_switch_on;
 74	int trip_max_desired_temperature;
 75	u32 sustainable_power;
 
 
 
 
 
 
 76};
 77
 
 
 
 
 
 
 
 78/**
 79 * estimate_sustainable_power() - Estimate the sustainable power of a thermal zone
 80 * @tz: thermal zone we are operating in
 81 *
 82 * For thermal zones that don't provide a sustainable_power in their
 83 * thermal_zone_params, estimate one.  Calculate it using the minimum
 84 * power of all the cooling devices as that gives a valid value that
 85 * can give some degree of functionality.  For optimal performance of
 86 * this governor, provide a sustainable_power in the thermal zone's
 87 * thermal_zone_params.
 88 */
 89static u32 estimate_sustainable_power(struct thermal_zone_device *tz)
 90{
 
 
 
 91	u32 sustainable_power = 0;
 92	struct thermal_instance *instance;
 93	struct power_allocator_params *params = tz->governor_data;
 94
 95	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
 96		struct thermal_cooling_device *cdev = instance->cdev;
 97		u32 min_power;
 98
 99		if (instance->trip != params->trip_max_desired_temperature)
100			continue;
101
102		if (!cdev_is_power_actor(cdev))
103			continue;
104
 
105		if (cdev->ops->state2power(cdev, instance->upper, &min_power))
106			continue;
107
108		sustainable_power += min_power;
109	}
110
111	return sustainable_power;
112}
113
114/**
115 * estimate_pid_constants() - Estimate the constants for the PID controller
116 * @tz:		thermal zone for which to estimate the constants
117 * @sustainable_power:	sustainable power for the thermal zone
118 * @trip_switch_on:	trip point number for the switch on temperature
119 * @control_temp:	target temperature for the power allocator governor
120 *
121 * This function is used to update the estimation of the PID
122 * controller constants in struct thermal_zone_parameters.
123 */
124static void estimate_pid_constants(struct thermal_zone_device *tz,
125				   u32 sustainable_power, int trip_switch_on,
 
126				   int control_temp)
127{
128	int ret;
129	int switch_on_temp;
130	u32 temperature_threshold;
131	s32 k_i;
132
133	ret = tz->ops->get_trip_temp(tz, trip_switch_on, &switch_on_temp);
134	if (ret)
135		switch_on_temp = 0;
136
137	temperature_threshold = control_temp - switch_on_temp;
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 */
175static 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 */
217static 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 */
291static int
292power_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 */
337static 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
383static 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	u32 *req_power, *max_power, *granted_power, *extra_actor_power;
389	u32 *weighted_req_power;
390	u32 total_req_power, max_allocatable_power, total_weighted_req_power;
391	u32 total_granted_power, power_range;
392	int i, num_actors, total_weight, ret = 0;
393	int trip_max_desired_temperature = params->trip_max_desired_temperature;
394
395	mutex_lock(&tz->lock);
396
397	num_actors = 0;
398	total_weight = 0;
399	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
400		if ((instance->trip == trip_max_desired_temperature) &&
401		    cdev_is_power_actor(instance->cdev)) {
402			num_actors++;
403			total_weight += instance->weight;
404		}
405	}
406
407	if (!num_actors) {
408		ret = -ENODEV;
409		goto unlock;
410	}
411
412	/*
413	 * We need to allocate five arrays of the same size:
414	 * req_power, max_power, granted_power, extra_actor_power and
415	 * weighted_req_power.  They are going to be needed until this
416	 * function returns.  Allocate them all in one go to simplify
417	 * the allocation and deallocation logic.
418	 */
419	BUILD_BUG_ON(sizeof(*req_power) != sizeof(*max_power));
420	BUILD_BUG_ON(sizeof(*req_power) != sizeof(*granted_power));
421	BUILD_BUG_ON(sizeof(*req_power) != sizeof(*extra_actor_power));
422	BUILD_BUG_ON(sizeof(*req_power) != sizeof(*weighted_req_power));
423	req_power = kcalloc(num_actors * 5, sizeof(*req_power), GFP_KERNEL);
424	if (!req_power) {
425		ret = -ENOMEM;
426		goto unlock;
427	}
428
429	max_power = &req_power[num_actors];
430	granted_power = &req_power[2 * num_actors];
431	extra_actor_power = &req_power[3 * num_actors];
432	weighted_req_power = &req_power[4 * num_actors];
433
434	i = 0;
435	total_weighted_req_power = 0;
436	total_req_power = 0;
437	max_allocatable_power = 0;
438
439	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
440		int weight;
441		struct thermal_cooling_device *cdev = instance->cdev;
442
443		if (instance->trip != trip_max_desired_temperature)
444			continue;
445
446		if (!cdev_is_power_actor(cdev))
447			continue;
448
449		if (cdev->ops->get_requested_power(cdev, &req_power[i]))
 
450			continue;
451
452		if (!total_weight)
453			weight = 1 << FRAC_BITS;
454		else
455			weight = instance->weight;
456
457		weighted_req_power[i] = frac_to_int(weight * req_power[i]);
458
459		if (cdev->ops->state2power(cdev, instance->lower,
460					   &max_power[i]))
 
461			continue;
462
463		total_req_power += req_power[i];
464		max_allocatable_power += max_power[i];
465		total_weighted_req_power += weighted_req_power[i];
466
467		i++;
468	}
469
470	power_range = pid_controller(tz, control_temp, max_allocatable_power);
471
472	divvy_up_power(weighted_req_power, max_power, num_actors,
473		       total_weighted_req_power, power_range, granted_power,
474		       extra_actor_power);
475
476	total_granted_power = 0;
477	i = 0;
478	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
479		if (instance->trip != trip_max_desired_temperature)
480			continue;
481
482		if (!cdev_is_power_actor(instance->cdev))
483			continue;
484
485		power_actor_set_power(instance->cdev, instance,
486				      granted_power[i]);
487		total_granted_power += granted_power[i];
488
 
 
489		i++;
490	}
491
492	trace_thermal_power_allocator(tz, req_power, total_req_power,
493				      granted_power, total_granted_power,
494				      num_actors, power_range,
495				      max_allocatable_power, tz->temperature,
496				      control_temp - tz->temperature);
497
498	kfree(req_power);
499unlock:
500	mutex_unlock(&tz->lock);
501
502	return ret;
503}
504
505/**
506 * get_governor_trips() - get the number of the two trip points that are key for this governor
507 * @tz:	thermal zone to operate on
508 * @params:	pointer to private data for this governor
509 *
510 * The power allocator governor works optimally with two trips points:
511 * a "switch on" trip point and a "maximum desired temperature".  These
512 * are defined as the first and last passive trip points.
513 *
514 * If there is only one trip point, then that's considered to be the
515 * "maximum desired temperature" trip point and the governor is always
516 * on.  If there are no passive or active trip points, then the
517 * governor won't do anything.  In fact, its throttle function
518 * won't be called at all.
519 */
520static void get_governor_trips(struct thermal_zone_device *tz,
521			       struct power_allocator_params *params)
522{
523	int i, last_active, last_passive;
524	bool found_first_passive;
525
526	found_first_passive = false;
527	last_active = INVALID_TRIP;
528	last_passive = INVALID_TRIP;
529
530	for (i = 0; i < tz->trips; i++) {
531		enum thermal_trip_type type;
532		int ret;
533
534		ret = tz->ops->get_trip_type(tz, i, &type);
535		if (ret) {
536			dev_warn(&tz->device,
537				 "Failed to get trip point %d type: %d\n", i,
538				 ret);
539			continue;
540		}
541
542		if (type == THERMAL_TRIP_PASSIVE) {
543			if (!found_first_passive) {
544				params->trip_switch_on = i;
545				found_first_passive = true;
546			} else  {
547				last_passive = i;
548			}
549		} else if (type == THERMAL_TRIP_ACTIVE) {
550			last_active = i;
551		} else {
 
 
 
552			break;
553		}
554	}
555
556	if (last_passive != INVALID_TRIP) {
557		params->trip_max_desired_temperature = last_passive;
558	} else if (found_first_passive) {
559		params->trip_max_desired_temperature = params->trip_switch_on;
560		params->trip_switch_on = INVALID_TRIP;
 
561	} else {
562		params->trip_switch_on = INVALID_TRIP;
563		params->trip_max_desired_temperature = last_active;
564	}
565}
566
567static void reset_pid_controller(struct power_allocator_params *params)
568{
569	params->err_integral = 0;
570	params->prev_err = 0;
571}
572
573static void allow_maximum_power(struct thermal_zone_device *tz, bool update)
574{
 
 
575	struct thermal_instance *instance;
576	struct power_allocator_params *params = tz->governor_data;
577	u32 req_power;
578
579	mutex_lock(&tz->lock);
580	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
581		struct thermal_cooling_device *cdev = instance->cdev;
 
582
583		if ((instance->trip != params->trip_max_desired_temperature) ||
584		    (!cdev_is_power_actor(instance->cdev)))
585			continue;
586
587		instance->target = 0;
588		mutex_lock(&instance->cdev->lock);
589		/*
590		 * Call for updating the cooling devices local stats and avoid
591		 * periods of dozen of seconds when those have not been
592		 * maintained.
593		 */
594		cdev->ops->get_requested_power(cdev, &req_power);
595
596		if (update)
597			__thermal_cdev_update(instance->cdev);
598
599		mutex_unlock(&instance->cdev->lock);
600	}
601	mutex_unlock(&tz->lock);
602}
603
604/**
605 * check_power_actors() - Check all cooling devices and warn when they are
606 *			not power actors
607 * @tz:		thermal zone to operate on
 
608 *
609 * Check all cooling devices in the @tz and warn every time they are missing
610 * power actor API. The warning should help to investigate the issue, which
611 * could be e.g. lack of Energy Model for a given device.
612 *
613 * Return: 0 on success, -EINVAL if any cooling device does not implement
614 * the power actor API.
 
615 */
616static int check_power_actors(struct thermal_zone_device *tz)
 
617{
618	struct thermal_instance *instance;
619	int ret = 0;
620
621	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
 
 
 
622		if (!cdev_is_power_actor(instance->cdev)) {
623			dev_warn(&tz->device, "power_allocator: %s is not a power actor\n",
624				 instance->cdev->type);
625			ret = -EINVAL;
626		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
627	}
628
 
 
 
 
 
 
 
 
 
629	return ret;
630}
631
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
632/**
633 * power_allocator_bind() - bind the power_allocator governor to a thermal zone
634 * @tz:	thermal zone to bind it to
635 *
636 * Initialize the PID controller parameters and bind it to the thermal
637 * zone.
638 *
639 * Return: 0 on success, or -ENOMEM if we ran out of memory, or -EINVAL
640 * when there are unsupported cooling devices in the @tz.
641 */
642static int power_allocator_bind(struct thermal_zone_device *tz)
643{
 
644	int ret;
645	struct power_allocator_params *params;
646	int control_temp;
647
648	ret = check_power_actors(tz);
649	if (ret)
650		return ret;
651
652	params = kzalloc(sizeof(*params), GFP_KERNEL);
653	if (!params)
654		return -ENOMEM;
655
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
656	if (!tz->tzp) {
657		tz->tzp = kzalloc(sizeof(*tz->tzp), GFP_KERNEL);
658		if (!tz->tzp) {
659			ret = -ENOMEM;
660			goto free_params;
661		}
662
663		params->allocated_tzp = true;
664	}
665
666	if (!tz->tzp->sustainable_power)
667		dev_warn(&tz->device, "power_allocator: sustainable_power will be estimated\n");
668
669	get_governor_trips(tz, params);
670
671	if (tz->trips > 0) {
672		ret = tz->ops->get_trip_temp(tz,
673					params->trip_max_desired_temperature,
674					&control_temp);
675		if (!ret)
676			estimate_pid_constants(tz, tz->tzp->sustainable_power,
677					       params->trip_switch_on,
678					       control_temp);
679	}
680
681	reset_pid_controller(params);
682
683	tz->governor_data = params;
684
685	return 0;
686
687free_params:
 
688	kfree(params);
689
690	return ret;
691}
692
693static void power_allocator_unbind(struct thermal_zone_device *tz)
694{
695	struct power_allocator_params *params = tz->governor_data;
696
697	dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id);
698
699	if (params->allocated_tzp) {
700		kfree(tz->tzp);
701		tz->tzp = NULL;
702	}
703
 
704	kfree(tz->governor_data);
705	tz->governor_data = NULL;
706}
707
708static int power_allocator_throttle(struct thermal_zone_device *tz, int trip)
 
709{
710	int ret;
711	int switch_on_temp, control_temp;
712	struct power_allocator_params *params = tz->governor_data;
713	bool update;
714
 
 
715	/*
716	 * We get called for every trip point but we only need to do
717	 * our calculations once
718	 */
719	if (trip != params->trip_max_desired_temperature)
720		return 0;
721
722	ret = tz->ops->get_trip_temp(tz, params->trip_switch_on,
723				     &switch_on_temp);
724	if (!ret && (tz->temperature < switch_on_temp)) {
725		update = (tz->last_temperature >= switch_on_temp);
726		tz->passive = 0;
727		reset_pid_controller(params);
728		allow_maximum_power(tz, update);
729		return 0;
730	}
731
732	tz->passive = 1;
733
734	ret = tz->ops->get_trip_temp(tz, params->trip_max_desired_temperature,
735				&control_temp);
736	if (ret) {
737		dev_warn(&tz->device,
738			 "Failed to get the maximum desired temperature: %d\n",
739			 ret);
740		return ret;
741	}
742
743	return allocate_power(tz, control_temp);
744}
745
746static struct thermal_governor thermal_gov_power_allocator = {
747	.name		= "power_allocator",
748	.bind_to_tz	= power_allocator_bind,
749	.unbind_from_tz	= power_allocator_unbind,
750	.throttle	= power_allocator_throttle,
 
751};
752THERMAL_GOVERNOR_DECLARE(thermal_gov_power_allocator);