Lines Matching +full:pa +full:- +full:stats
1 // SPDX-License-Identifier: GPL-2.0
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
66 * struct power_allocator_params - parameters for the power allocator governor
83 * @buffer_size: internal buffer size, to avoid runtime re-calculation
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
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
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
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
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()
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()
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
311 * Return: 0 on success, -EINVAL if the cooling device does not
312 * implement the power actor API or -E* for other failures.
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
349 * surplus is re-divvied among the actors based on how far they are
366 struct power_actor *pa = &power[i]; in divvy_up_power() local
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() local
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()
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() local
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()
457 list_for_each_entry(instance, &tz->thermal_instances, tz_node) { in allocate_power()
458 struct power_actor *pa = &power[i]; in allocate_power() local
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
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()
554 * Call for updating the cooling devices local stats and avoid 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
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()