Lines Matching +full:sustainable +full:- +full:power

1 // SPDX-License-Identifier: GPL-2.0
3 * A power allocator to manage temperature
9 #define pr_fmt(fmt) "Power allocator: " fmt
24 * mul_frac() - multiply two fixed-point numbers
28 * Return: the result of multiplying two fixed-point numbers. The
29 * result is also a fixed-point number.
37 * div_frac() - divide two fixed-point numbers
41 * Return: the result of dividing two fixed-point numbers. The
42 * result is also a fixed-point number.
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
66 * struct power_allocator_params - parameters for the power allocator governor
73 * @sustainable_power: Sustainable power (heat) that this thermal zone can
83 * @buffer_size: internal buffer size, to avoid runtime re-calculation
84 * @power: buffer for all power actors internal power information
97 struct power_actor *power; member
103 return (instance->trip == params->trip_max && in power_actor_is_valid()
104 cdev_is_power_actor(instance->cdev)); in power_actor_is_valid()
108 * estimate_sustainable_power() - Estimate the sustainable power of a thermal zone
113 * power of all the cooling devices as that gives a valid value that
120 struct power_allocator_params *params = tz->governor_data; in estimate_sustainable_power()
126 list_for_each_entry(instance, &tz->thermal_instances, tz_node) { in estimate_sustainable_power()
130 cdev = instance->cdev; in estimate_sustainable_power()
131 if (cdev->ops->state2power(cdev, instance->upper, &min_power)) in estimate_sustainable_power()
141 * estimate_pid_constants() - Estimate the constants for the PID controller
143 * @sustainable_power: sustainable power for the thermal zone
145 * @control_temp: target temperature for the power allocator governor
159 temperature_threshold -= trip_switch_on->temperature; in estimate_pid_constants()
172 tz->tzp->k_po = int_to_frac(sustainable_power) / in estimate_pid_constants()
175 tz->tzp->k_pu = int_to_frac(2 * sustainable_power) / in estimate_pid_constants()
178 k_i = tz->tzp->k_pu / 10; in estimate_pid_constants()
179 tz->tzp->k_i = k_i > 0 ? k_i : 1; in estimate_pid_constants()
188 * get_sustainable_power() - Get the right sustainable power
190 * @params: parameters for the power allocator governor
191 * @control_temp: target temperature for the power allocator governor
193 * This function is used for getting the proper sustainable power value based
204 if (!tz->tzp->sustainable_power) in get_sustainable_power()
207 sustainable_power = tz->tzp->sustainable_power; in get_sustainable_power()
210 if (sustainable_power != params->sustainable_power) { in get_sustainable_power()
212 params->trip_switch_on, control_temp); in get_sustainable_power()
215 tz->tzp->sustainable_power = sustainable_power; in get_sustainable_power()
216 params->sustainable_power = sustainable_power; in get_sustainable_power()
223 * pid_controller() - PID controller
226 * @max_allocatable_power: maximum allocatable power for this thermal zone
228 * This PID controller increases the available power budget so that the
230 * @control_temp and limits the power if it exceeds it. k_po is the
234 * accumulated error is only valid if the requested power will make
238 * Return: The power budget for the next period.
244 struct power_allocator_params *params = tz->governor_data; in pid_controller()
253 err = control_temp - tz->temperature; in pid_controller()
257 p = mul_frac(err < 0 ? tz->tzp->k_po : tz->tzp->k_pu, err); in pid_controller()
263 * the integral is limited to max power) in pid_controller()
265 i = mul_frac(tz->tzp->k_i, params->err_integral); in pid_controller()
267 if (err < int_to_frac(tz->tzp->integral_cutoff)) { in pid_controller()
268 s64 i_next = i + mul_frac(tz->tzp->k_i, err); in pid_controller()
272 params->err_integral += err; in pid_controller()
279 * We do err - prev_err, so with a positive k_d, a decreasing in pid_controller()
281 * power being applied, slowing down the controller) in pid_controller()
283 d = mul_frac(tz->tzp->k_d, err - params->prev_err); in pid_controller()
284 d = div_frac(d, jiffies_to_msecs(tz->passive_delay_jiffies)); in pid_controller()
285 params->prev_err = err; in pid_controller()
289 /* feed-forward the known sustainable dissipatable power */ in pid_controller()
295 frac_to_int(params->err_integral), in pid_controller()
303 * power_actor_set_power() - limit the maximum power a cooling device consumes
306 * @power: the power in milliwatts
308 * Set the cooling device to consume at most @power milliwatts. The limit is
309 * expected to be a cap at the maximum power consumption.
311 * Return: 0 on success, -EINVAL if the cooling device does not
312 * implement the power actor API or -E* for other failures.
316 struct thermal_instance *instance, u32 power) in power_actor_set_power() argument
321 ret = cdev->ops->power2state(cdev, power, &state); in power_actor_set_power()
325 instance->target = clamp_val(state, instance->lower, instance->upper); in power_actor_set_power()
326 mutex_lock(&cdev->lock); in power_actor_set_power()
328 mutex_unlock(&cdev->lock); in power_actor_set_power()
334 * divvy_up_power() - divvy the allocated power between the actors
335 * @power: buffer for all power actors internal power information
336 * @num_actors: number of power actors in this thermal zone
337 * @total_req_power: sum of all weighted requested power for all actors
338 * @power_range: total allocated power
340 * This function divides the total allocated power (@power_range)
342 * share of the @power_range according to how much power it requested
344 * requests power, then it receives all the @power_range. If
348 * If any actor received more than their maximum power, then that
349 * surplus is re-divvied among the actors based on how far they are
352 static void divvy_up_power(struct power_actor *power, int num_actors, in divvy_up_power() argument
360 * Prevent division by 0 if none of the actors request power. in divvy_up_power()
366 struct power_actor *pa = &power[i]; in divvy_up_power()
367 u64 req_range = (u64)pa->req_power * power_range; in divvy_up_power()
369 pa->granted_power = DIV_ROUND_CLOSEST_ULL(req_range, in divvy_up_power()
372 if (pa->granted_power > pa->max_power) { in divvy_up_power()
373 extra_power += pa->granted_power - pa->max_power; in divvy_up_power()
374 pa->granted_power = pa->max_power; in divvy_up_power()
377 pa->extra_actor_power = pa->max_power - pa->granted_power; in divvy_up_power()
378 capped_extra_power += pa->extra_actor_power; in divvy_up_power()
385 * Re-divvy the reclaimed extra among actors based on in divvy_up_power()
391 struct power_actor *pa = &power[i]; in divvy_up_power()
392 u64 extra_range = pa->extra_actor_power; in divvy_up_power()
395 pa->granted_power += DIV_ROUND_CLOSEST_ULL(extra_range, in divvy_up_power()
402 struct power_allocator_params *params = tz->governor_data; in allocate_power()
403 unsigned int num_actors = params->num_actors; in allocate_power()
404 struct power_actor *power = params->power; in allocate_power() local
417 /* Clean all buffers for new power estimations */ in allocate_power()
418 memset(power, 0, params->buffer_size); in allocate_power()
420 list_for_each_entry(instance, &tz->thermal_instances, tz_node) { in allocate_power()
421 struct power_actor *pa = &power[i]; in allocate_power()
426 cdev = instance->cdev; in allocate_power()
428 ret = cdev->ops->get_requested_power(cdev, &pa->req_power); in allocate_power()
432 if (!params->total_weight) in allocate_power()
435 weight = instance->weight; in allocate_power()
437 pa->weighted_req_power = frac_to_int(weight * pa->req_power); in allocate_power()
439 ret = cdev->ops->state2power(cdev, instance->lower, in allocate_power()
440 &pa->max_power); in allocate_power()
444 total_req_power += pa->req_power; in allocate_power()
445 max_allocatable_power += pa->max_power; in allocate_power()
446 total_weighted_req_power += pa->weighted_req_power; in allocate_power()
453 divvy_up_power(power, num_actors, total_weighted_req_power, in allocate_power()
457 list_for_each_entry(instance, &tz->thermal_instances, tz_node) { in allocate_power()
458 struct power_actor *pa = &power[i]; in allocate_power()
463 power_actor_set_power(instance->cdev, instance, in allocate_power()
464 pa->granted_power); in allocate_power()
465 total_granted_power += pa->granted_power; in allocate_power()
467 trace_thermal_power_actor(tz, i, pa->req_power, in allocate_power()
468 pa->granted_power); in allocate_power()
474 max_allocatable_power, tz->temperature, in allocate_power()
475 control_temp - tz->temperature); in allocate_power()
479 * get_governor_trips() - get the two trip points that are key for this governor
483 * The power allocator governor works optimally with two trips points:
502 const struct thermal_trip *trip = &td->trip; in get_governor_trips()
504 switch (trip->type) { in get_governor_trips()
521 params->trip_switch_on = first_passive; in get_governor_trips()
522 params->trip_max = last_passive; in get_governor_trips()
524 params->trip_switch_on = NULL; in get_governor_trips()
525 params->trip_max = first_passive; in get_governor_trips()
527 params->trip_switch_on = NULL; in get_governor_trips()
528 params->trip_max = last_active; in get_governor_trips()
534 params->err_integral = 0; in reset_pid_controller()
535 params->prev_err = 0; in reset_pid_controller()
540 struct power_allocator_params *params = tz->governor_data; in allow_maximum_power()
545 list_for_each_entry(instance, &tz->thermal_instances, tz_node) { in allow_maximum_power()
549 cdev = instance->cdev; in allow_maximum_power()
551 instance->target = 0; in allow_maximum_power()
552 mutex_lock(&cdev->lock); in allow_maximum_power()
558 cdev->ops->get_requested_power(cdev, &req_power); in allow_maximum_power()
560 if (params->update_cdevs) in allow_maximum_power()
563 mutex_unlock(&cdev->lock); in allow_maximum_power()
568 * check_power_actors() - Check all cooling devices and warn when they are
569 * not power actors
571 * @params: power allocator private data
574 * power actor API. The warning should help to investigate the issue, which
577 * If all of the cooling devices currently attached to @tz implement the power
579 * devices may be attached later). Otherwise, return -EINVAL.
587 list_for_each_entry(instance, &tz->thermal_instances, tz_node) { in check_power_actors()
588 if (instance->trip != params->trip_max) in check_power_actors()
591 if (!cdev_is_power_actor(instance->cdev)) { in check_power_actors()
592 dev_warn(&tz->device, "power_allocator: %s is not a power actor\n", in check_power_actors()
593 instance->cdev->type); in check_power_actors()
594 return -EINVAL; in check_power_actors()
607 kfree(params->power); in allocate_actors_buffer()
615 params->power = kcalloc(num_actors, sizeof(struct power_actor), in allocate_actors_buffer()
617 if (!params->power) { in allocate_actors_buffer()
618 ret = -ENOMEM; in allocate_actors_buffer()
622 params->num_actors = num_actors; in allocate_actors_buffer()
623 params->buffer_size = num_actors * sizeof(struct power_actor); in allocate_actors_buffer()
628 params->num_actors = 0; in allocate_actors_buffer()
629 params->buffer_size = 0; in allocate_actors_buffer()
630 params->power = NULL; in allocate_actors_buffer()
637 struct power_allocator_params *params = tz->governor_data; in power_allocator_update_tz()
644 list_for_each_entry(instance, &tz->thermal_instances, tz_node) in power_allocator_update_tz()
648 if (num_actors == params->num_actors) in power_allocator_update_tz()
654 params->total_weight = 0; in power_allocator_update_tz()
655 list_for_each_entry(instance, &tz->thermal_instances, tz_node) in power_allocator_update_tz()
657 params->total_weight += instance->weight; in power_allocator_update_tz()
665 * power_allocator_bind() - bind the power_allocator governor to a thermal zone
671 * Return: 0 on success, or -ENOMEM if we ran out of memory, or -EINVAL
681 return -ENOMEM; in power_allocator_bind()
687 dev_warn(&tz->device, "power_allocator: binding failed\n"); in power_allocator_bind()
694 dev_warn(&tz->device, "power_allocator: allocation failed\n"); in power_allocator_bind()
699 if (!tz->tzp) { in power_allocator_bind()
700 tz->tzp = kzalloc(sizeof(*tz->tzp), GFP_KERNEL); in power_allocator_bind()
701 if (!tz->tzp) { in power_allocator_bind()
702 ret = -ENOMEM; in power_allocator_bind()
706 params->allocated_tzp = true; in power_allocator_bind()
709 if (!tz->tzp->sustainable_power) in power_allocator_bind()
710 dev_warn(&tz->device, "power_allocator: sustainable_power will be estimated\n"); in power_allocator_bind()
712 params->sustainable_power = tz->tzp->sustainable_power; in power_allocator_bind()
714 if (params->trip_max) in power_allocator_bind()
715 estimate_pid_constants(tz, tz->tzp->sustainable_power, in power_allocator_bind()
716 params->trip_switch_on, in power_allocator_bind()
717 params->trip_max->temperature); in power_allocator_bind()
721 tz->governor_data = params; in power_allocator_bind()
726 kfree(params->power); in power_allocator_bind()
734 struct power_allocator_params *params = tz->governor_data; in power_allocator_unbind()
736 dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id); in power_allocator_unbind()
738 if (params->allocated_tzp) { in power_allocator_unbind()
739 kfree(tz->tzp); in power_allocator_unbind()
740 tz->tzp = NULL; in power_allocator_unbind()
743 kfree(params->power); in power_allocator_unbind()
744 kfree(tz->governor_data); in power_allocator_unbind()
745 tz->governor_data = NULL; in power_allocator_unbind()
750 struct power_allocator_params *params = tz->governor_data; in power_allocator_manage()
751 const struct thermal_trip *trip = params->trip_switch_on; in power_allocator_manage()
753 lockdep_assert_held(&tz->lock); in power_allocator_manage()
755 if (trip && tz->temperature < trip->temperature) { in power_allocator_manage()
758 params->update_cdevs = false; in power_allocator_manage()
762 if (!params->trip_max) in power_allocator_manage()
765 allocate_power(tz, params->trip_max->temperature); in power_allocator_manage()
766 params->update_cdevs = true; in power_allocator_manage()