From nobody Sat Jan 10 23:10:16 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D27D0E81E1E for ; Fri, 6 Oct 2023 17:51:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233122AbjJFRvo (ORCPT ); Fri, 6 Oct 2023 13:51:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233074AbjJFRvc (ORCPT ); Fri, 6 Oct 2023 13:51:32 -0400 Received: from cloudserver094114.home.pl (cloudserver094114.home.pl [79.96.170.134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B42CCC5; Fri, 6 Oct 2023 10:51:29 -0700 (PDT) Received: from localhost (127.0.0.1) (HELO v370.home.net.pl) by /usr/run/smtp (/usr/run/postfix/private/idea_relay_lmtp) via UNIX with SMTP (IdeaSmtpServer 5.2.0) id 2288febb9f555858; Fri, 6 Oct 2023 19:51:28 +0200 Received: from kreacher.localnet (unknown [195.136.19.94]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by v370.home.net.pl (Postfix) with ESMTPSA id AFB2D665D08; Fri, 6 Oct 2023 19:51:27 +0200 (CEST) From: "Rafael J. Wysocki" To: Linux PM Cc: LKML , Daniel Lezcano , Srinivas Pandruvada , Zhang Rui , Lukasz Luba Subject: [PATCH v1 4/6] thermal: gov_power_allocator: Use trip pointers instead of trip indices Date: Fri, 06 Oct 2023 19:47:52 +0200 Message-ID: <3768557.kQq0lBPeGt@kreacher> In-Reply-To: <13365827.uLZWGnKmhe@kreacher> References: <13365827.uLZWGnKmhe@kreacher> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CLIENT-IP: 195.136.19.94 X-CLIENT-HOSTNAME: 195.136.19.94 X-VADE-SPAMSTATE: clean X-VADE-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedvkedrgeeigdduudehucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecujffqoffgrffnpdggtffipffknecuuegrihhlohhuthemucduhedtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufffkfgjfhgggfgtsehtufertddttdejnecuhfhrohhmpedftfgrfhgrvghlucflrdcuhgihshhotghkihdfuceorhhjfiesrhhjfiihshhotghkihdrnhgvtheqnecuggftrfgrthhtvghrnhepvdffueeitdfgvddtudegueejtdffteetgeefkeffvdeftddttdeuhfegfedvjefhnecukfhppeduleehrddufeeirdduledrleegnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepudelhedrudefiedrudelrdelgedphhgvlhhopehkrhgvrggthhgvrhdrlhhotggrlhhnvghtpdhmrghilhhfrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqedpnhgspghrtghpthhtohepiedprhgtphhtthhopehlihhnuhigqdhpmhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopegurghnihgvlhdrlhgviigtrghnoheslhhinhgrrhhordhorhhgpdhrtghpthhtohepshhrihhnihhvrghsrdhprghnughruhhvrggurgeslhhinhhugidrihhnthgv lhdrtghomhdprhgtphhtthhopehruhhirdiihhgrnhhgsehinhhtvghlrdgtohhmpdhrtghpthhtoheplhhukhgrshiirdhluhgsrgesrghrmhdrtghomh X-DCC--Metrics: v370.home.net.pl 1024; Body=6 Fuz1=6 Fuz2=6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Rafael J. Wysocki Modify the power allocator thermal governor to use trip pointers instead of trip indices everywhere except for the power_allocator_throttle() second argument that will be changed subsequently along with the definition of the .throttle() governor callback. The general functionality is not expected to be changed. Signed-off-by: Rafael J. Wysocki Reviewed-by: Lukasz Luba Tested-by: Lukasz Luba --- drivers/thermal/gov_power_allocator.c | 123 +++++++++++++----------------= ----- 1 file changed, 49 insertions(+), 74 deletions(-) Index: linux-pm/drivers/thermal/gov_power_allocator.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- linux-pm.orig/drivers/thermal/gov_power_allocator.c +++ linux-pm/drivers/thermal/gov_power_allocator.c @@ -16,8 +16,6 @@ =20 #include "thermal_core.h" =20 -#define INVALID_TRIP -1 - #define FRAC_BITS 10 #define int_to_frac(x) ((x) << FRAC_BITS) #define frac_to_int(x) ((x) >> FRAC_BITS) @@ -55,23 +53,23 @@ static inline s64 div_frac(s64 x, s64 y) * @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 INVALID_TRIP. + * @trip_switch_on should be NULL. * @trip_max_desired_temperature: last passive trip point of the thermal * zone. The temperature we are * controlling for. - * @sustainable_power: Sustainable power (heat) that this thermal zone can - * dissipate */ struct power_allocator_params { bool allocated_tzp; s64 err_integral; s32 prev_err; - int trip_switch_on; - int trip_max_desired_temperature; u32 sustainable_power; + const struct thermal_trip *trip_switch_on; + const struct thermal_trip *trip_max_desired_temperature; }; =20 /** @@ -90,14 +88,12 @@ static u32 estimate_sustainable_power(st u32 sustainable_power =3D 0; struct thermal_instance *instance; struct power_allocator_params *params =3D tz->governor_data; - const struct thermal_trip *trip_max_desired_temperature =3D - &tz->trips[params->trip_max_desired_temperature]; =20 list_for_each_entry(instance, &tz->thermal_instances, tz_node) { struct thermal_cooling_device *cdev =3D instance->cdev; u32 min_power; =20 - if (instance->trip !=3D trip_max_desired_temperature) + if (instance->trip !=3D params->trip_max_desired_temperature) continue; =20 if (!cdev_is_power_actor(cdev)) @@ -116,24 +112,23 @@ static u32 estimate_sustainable_power(st * 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 number for the switch on temperature + * @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, int trip_switch_on, + u32 sustainable_power, + const struct thermal_trip *trip_switch_on, int control_temp) { - struct thermal_trip trip; u32 temperature_threshold =3D control_temp; int ret; s32 k_i; =20 - ret =3D __thermal_zone_get_trip(tz, trip_switch_on, &trip); - if (!ret) - temperature_threshold -=3D trip.temperature; + if (trip_switch_on) + temperature_threshold -=3D trip_switch_on->temperature; =20 /* * estimate_pid_constants() tries to find appropriate default @@ -386,7 +381,7 @@ static int allocate_power(struct thermal struct thermal_instance *instance; struct power_allocator_params *params =3D tz->governor_data; const struct thermal_trip *trip_max_desired_temperature =3D - &tz->trips[params->trip_max_desired_temperature]; + params->trip_max_desired_temperature; u32 *req_power, *max_power, *granted_power, *extra_actor_power; u32 *weighted_req_power; u32 total_req_power, max_allocatable_power, total_weighted_req_power; @@ -496,7 +491,7 @@ static int allocate_power(struct thermal } =20 /** - * get_governor_trips() - get the number of the two trip points that are k= ey for this governor + * get_governor_trips() - get the two trip points that are key for this go= vernor * @tz: thermal zone to operate on * @params: pointer to private data for this governor * @@ -513,46 +508,36 @@ static int allocate_power(struct thermal static void get_governor_trips(struct thermal_zone_device *tz, struct power_allocator_params *params) { - int i, last_active, last_passive; - bool found_first_passive; - - found_first_passive =3D false; - last_active =3D INVALID_TRIP; - last_passive =3D INVALID_TRIP; - - for (i =3D 0; i < tz->num_trips; i++) { - struct thermal_trip trip; - int ret; - - ret =3D __thermal_zone_get_trip(tz, i, &trip); - if (ret) { - dev_warn(&tz->device, - "Failed to get trip point %d type: %d\n", i, - ret); - continue; - } - - if (trip.type =3D=3D THERMAL_TRIP_PASSIVE) { - if (!found_first_passive) { - params->trip_switch_on =3D i; - found_first_passive =3D true; - } else { - last_passive =3D i; + const struct thermal_trip *first_passive =3D NULL; + const struct thermal_trip *last_passive =3D NULL; + const struct thermal_trip *last_active =3D NULL; + const struct thermal_trip *trip; + + for_each_trip(tz, trip) { + switch (trip->type) { + case THERMAL_TRIP_PASSIVE: + if (!first_passive) { + first_passive =3D trip; + break; } - } else if (trip.type =3D=3D THERMAL_TRIP_ACTIVE) { - last_active =3D i; - } else { + last_passive =3D trip; + break; + case THERMAL_TRIP_ACTIVE: + last_active =3D trip; + break; + default: break; } } =20 - if (last_passive !=3D INVALID_TRIP) { + if (last_passive) { + params->trip_switch_on =3D first_passive; params->trip_max_desired_temperature =3D last_passive; - } else if (found_first_passive) { - params->trip_max_desired_temperature =3D params->trip_switch_on; - params->trip_switch_on =3D INVALID_TRIP; + } else if (first_passive) { + params->trip_switch_on =3D NULL; + params->trip_max_desired_temperature =3D first_passive; } else { - params->trip_switch_on =3D INVALID_TRIP; + params->trip_switch_on =3D NULL; params->trip_max_desired_temperature =3D last_active; } } @@ -567,14 +552,12 @@ static void allow_maximum_power(struct t { struct thermal_instance *instance; struct power_allocator_params *params =3D tz->governor_data; - const struct thermal_trip *trip_max_desired_temperature =3D - &tz->trips[params->trip_max_desired_temperature]; u32 req_power; =20 list_for_each_entry(instance, &tz->thermal_instances, tz_node) { struct thermal_cooling_device *cdev =3D instance->cdev; =20 - if ((instance->trip !=3D trip_max_desired_temperature) || + if (instance->trip !=3D params->trip_max_desired_temperature || (!cdev_is_power_actor(instance->cdev))) continue; =20 @@ -636,7 +619,6 @@ static int power_allocator_bind(struct t { int ret; struct power_allocator_params *params; - struct thermal_trip trip; =20 ret =3D check_power_actors(tz); if (ret) @@ -662,12 +644,13 @@ static int power_allocator_bind(struct t get_governor_trips(tz, params); =20 if (tz->num_trips > 0) { - ret =3D __thermal_zone_get_trip(tz, params->trip_max_desired_temperature, - &trip); - if (!ret) + const struct thermal_trip *trip; + + trip =3D params->trip_max_desired_temperature; + if (trip) estimate_pid_constants(tz, tz->tzp->sustainable_power, params->trip_switch_on, - trip.temperature); + trip->temperature); } =20 reset_pid_controller(params); @@ -697,11 +680,10 @@ static void power_allocator_unbind(struc tz->governor_data =3D NULL; } =20 -static int power_allocator_throttle(struct thermal_zone_device *tz, int tr= ip_id) +static int power_allocator_throttle(struct thermal_zone_device *tz, int tr= ip_index) { struct power_allocator_params *params =3D tz->governor_data; - struct thermal_trip trip; - int ret; + const struct thermal_trip *trip =3D &tz->trips[trip_index]; bool update; =20 lockdep_assert_held(&tz->lock); @@ -710,12 +692,12 @@ static int power_allocator_throttle(stru * We get called for every trip point but we only need to do * our calculations once */ - if (trip_id !=3D params->trip_max_desired_temperature) + if (trip !=3D params->trip_max_desired_temperature) return 0; =20 - ret =3D __thermal_zone_get_trip(tz, params->trip_switch_on, &trip); - if (!ret && (tz->temperature < trip.temperature)) { - update =3D (tz->last_temperature >=3D trip.temperature); + trip =3D params->trip_switch_on; + if (trip && tz->temperature < trip->temperature) { + update =3D tz->last_temperature >=3D trip->temperature; tz->passive =3D 0; reset_pid_controller(params); allow_maximum_power(tz, update); @@ -724,14 +706,7 @@ static int power_allocator_throttle(stru =20 tz->passive =3D 1; =20 - ret =3D __thermal_zone_get_trip(tz, params->trip_max_desired_temperature,= &trip); - if (ret) { - dev_warn(&tz->device, "Failed to get the maximum desired temperature: %d= \n", - ret); - return ret; - } - - return allocate_power(tz, trip.temperature); + return allocate_power(tz, params->trip_max_desired_temperature->temperatu= re); } =20 static struct thermal_governor thermal_gov_power_allocator =3D {