// SPDX-License-Identifier: GPL-2.0 /* * A power allocator to manage temperature * * Copyright (C) 2014 ARM Ltd. * */ #define pr_fmt(fmt) … #include <linux/slab.h> #include <linux/thermal.h> #define CREATE_TRACE_POINTS #include "thermal_trace_ipa.h" #include "thermal_core.h" #define FRAC_BITS … #define int_to_frac(x) … #define frac_to_int(x) … /** * mul_frac() - multiply two fixed-point numbers * @x: first multiplicand * @y: second multiplicand * * Return: the result of multiplying two fixed-point numbers. The * result is also a fixed-point number. */ static inline s64 mul_frac(s64 x, s64 y) { … } /** * div_frac() - divide two fixed-point numbers * @x: the dividend * @y: the divisor * * Return: the result of dividing two fixed-point numbers. The * result is also a fixed-point number. */ static inline s64 div_frac(s64 x, s64 y) { … } /** * struct power_actor - internal power information for power actor * @req_power: requested power value (not weighted) * @max_power: max allocatable power for this actor * @granted_power: granted power for this actor * @extra_actor_power: extra power that this actor can receive * @weighted_req_power: weighted requested power as input to IPA */ struct power_actor { … }; /** * struct power_allocator_params - parameters for the power allocator governor * @allocated_tzp: whether we have allocated tzp for this thermal zone and * it needs to be freed on unbind * @update_cdevs: whether or not update cdevs on the next run * @err_integral: accumulated error in the PID controller. * @prev_err: error in the previous iteration of the PID controller. * Used to calculate the derivative term. * @sustainable_power: Sustainable power (heat) that this thermal zone can * dissipate * @trip_switch_on: first passive trip point of the thermal zone. The * governor switches on when this trip point is crossed. * If the thermal zone only has one passive trip point, * @trip_switch_on should be NULL. * @trip_max: last passive trip point of the thermal zone. The * temperature we are controlling for. * @total_weight: Sum of all thermal instances weights * @num_actors: number of cooling devices supporting IPA callbacks * @buffer_size: internal buffer size, to avoid runtime re-calculation * @power: buffer for all power actors internal power information */ struct power_allocator_params { … }; static bool power_actor_is_valid(struct power_allocator_params *params, struct thermal_instance *instance) { … } /** * estimate_sustainable_power() - Estimate the sustainable power of a thermal zone * @tz: thermal zone we are operating in * * For thermal zones that don't provide a sustainable_power in their * thermal_zone_params, estimate one. Calculate it using the minimum * power of all the cooling devices as that gives a valid value that * can give some degree of functionality. For optimal performance of * this governor, provide a sustainable_power in the thermal zone's * thermal_zone_params. */ static u32 estimate_sustainable_power(struct thermal_zone_device *tz) { … } /** * estimate_pid_constants() - Estimate the constants for the PID controller * @tz: thermal zone for which to estimate the constants * @sustainable_power: sustainable power for the thermal zone * @trip_switch_on: trip point for the switch on temperature * @control_temp: target temperature for the power allocator governor * * This function is used to update the estimation of the PID * controller constants in struct thermal_zone_parameters. */ static void estimate_pid_constants(struct thermal_zone_device *tz, u32 sustainable_power, const struct thermal_trip *trip_switch_on, int control_temp) { … } /** * get_sustainable_power() - Get the right sustainable power * @tz: thermal zone for which to estimate the constants * @params: parameters for the power allocator governor * @control_temp: target temperature for the power allocator governor * * This function is used for getting the proper sustainable power value based * on variables which might be updated by the user sysfs interface. If that * happen the new value is going to be estimated and updated. It is also used * after thermal zone binding, where the initial values where set to 0. */ static u32 get_sustainable_power(struct thermal_zone_device *tz, struct power_allocator_params *params, int control_temp) { … } /** * pid_controller() - PID controller * @tz: thermal zone we are operating in * @control_temp: the target temperature in millicelsius * @max_allocatable_power: maximum allocatable power for this thermal zone * * This PID controller increases the available power budget so that the * temperature of the thermal zone gets as close as possible to * @control_temp and limits the power if it exceeds it. k_po is the * proportional term when we are overshooting, k_pu is the * proportional term when we are undershooting. integral_cutoff is a * threshold below which we stop accumulating the error. The * accumulated error is only valid if the requested power will make * the system warmer. If the system is mostly idle, there's no point * in accumulating positive error. * * Return: The power budget for the next period. */ static u32 pid_controller(struct thermal_zone_device *tz, int control_temp, u32 max_allocatable_power) { … } /** * power_actor_set_power() - limit the maximum power a cooling device consumes * @cdev: pointer to &thermal_cooling_device * @instance: thermal instance to update * @power: the power in milliwatts * * Set the cooling device to consume at most @power milliwatts. The limit is * expected to be a cap at the maximum power consumption. * * Return: 0 on success, -EINVAL if the cooling device does not * implement the power actor API or -E* for other failures. */ static int power_actor_set_power(struct thermal_cooling_device *cdev, struct thermal_instance *instance, u32 power) { … } /** * divvy_up_power() - divvy the allocated power between the actors * @power: buffer for all power actors internal power information * @num_actors: number of power actors in this thermal zone * @total_req_power: sum of all weighted requested power for all actors * @power_range: total allocated power * * This function divides the total allocated power (@power_range) * fairly between the actors. It first tries to give each actor a * share of the @power_range according to how much power it requested * compared to the rest of the actors. For example, if only one actor * requests power, then it receives all the @power_range. If * three actors each requests 1mW, each receives a third of the * @power_range. * * If any actor received more than their maximum power, then that * surplus is re-divvied among the actors based on how far they are * from their respective maximums. */ static void divvy_up_power(struct power_actor *power, int num_actors, u32 total_req_power, u32 power_range) { … } static void allocate_power(struct thermal_zone_device *tz, int control_temp) { … } /** * get_governor_trips() - get the two trip points that are key for this governor * @tz: thermal zone to operate on * @params: pointer to private data for this governor * * The power allocator governor works optimally with two trips points: * a "switch on" trip point and a "maximum desired temperature". These * are defined as the first and last passive trip points. * * If there is only one trip point, then that's considered to be the * "maximum desired temperature" trip point and the governor is always * on. If there are no passive or active trip points, then the * governor won't do anything. In fact, its throttle function * won't be called at all. */ static void get_governor_trips(struct thermal_zone_device *tz, struct power_allocator_params *params) { … } static void reset_pid_controller(struct power_allocator_params *params) { … } static void allow_maximum_power(struct thermal_zone_device *tz) { … } /** * check_power_actors() - Check all cooling devices and warn when they are * not power actors * @tz: thermal zone to operate on * @params: power allocator private data * * Check all cooling devices in the @tz and warn every time they are missing * power actor API. The warning should help to investigate the issue, which * could be e.g. lack of Energy Model for a given device. * * If all of the cooling devices currently attached to @tz implement the power * actor API, return the number of them (which may be 0, because some cooling * devices may be attached later). Otherwise, return -EINVAL. */ static int check_power_actors(struct thermal_zone_device *tz, struct power_allocator_params *params) { … } static int allocate_actors_buffer(struct power_allocator_params *params, int num_actors) { … } static void power_allocator_update_tz(struct thermal_zone_device *tz, enum thermal_notify_event reason) { … } /** * power_allocator_bind() - bind the power_allocator governor to a thermal zone * @tz: thermal zone to bind it to * * Initialize the PID controller parameters and bind it to the thermal * zone. * * Return: 0 on success, or -ENOMEM if we ran out of memory, or -EINVAL * when there are unsupported cooling devices in the @tz. */ static int power_allocator_bind(struct thermal_zone_device *tz) { … } static void power_allocator_unbind(struct thermal_zone_device *tz) { … } static void power_allocator_manage(struct thermal_zone_device *tz) { … } static struct thermal_governor thermal_gov_power_allocator = …; THERMAL_GOVERNOR_DECLARE(…) …;