From patchwork Fri Jul 15 21:09:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 591275 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 47B69C43334 for ; Fri, 15 Jul 2022 21:09:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230041AbiGOVJW (ORCPT ); Fri, 15 Jul 2022 17:09:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230000AbiGOVJW (ORCPT ); Fri, 15 Jul 2022 17:09:22 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BF6A70E4E for ; Fri, 15 Jul 2022 14:09:17 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id bk26so8328907wrb.11 for ; Fri, 15 Jul 2022 14:09:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=uxbEsFkRpuwGSHkFCYXm35Pz3gor1PXEdEjjZ4dDIso=; b=ECmqWgF0/iWw8OYnFSCpnZrLQzFvpZ5Wlcl8Qk4xM83h11NVTftDk+p8Mlq3jY/MyJ 42oTvGWj10aYYQNuw+YtlAau9zvQuI0CRMe0jLmVk4CeyQgBCuc7UDrMjIGMl9WvkLqs u9BO3xzcLDJoyFV6dvjCTQU09yhh+vQoTuEd18/Y4pxcWJ6PxFjT+W1WhTmYaHR5c61t 867vz+XfF4iEbP8YgZFSDFGzMjjj34jaKb0s41Yf+y54i38dafN3VN0BhW5oa8YmTD6L Des2VcZw8kC5oDAQySjKol5oMfJEddJldCRPz+4Rz9dlKnJtggvCix2mdT0CojHikeW+ hvOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=uxbEsFkRpuwGSHkFCYXm35Pz3gor1PXEdEjjZ4dDIso=; b=k4WzFahvE1/e7Vl5cDmQXUcHBJKHxFStuYqvMDLvznFXS0lzUuj0UQvWDCUlR3ZLlf rbuv52aVsfYWN1X2KTZq7eqTNS8NfkZftyG7uFEByADtu+RxGxAt0ZRE9819GzSOkFpy zOhaIQ4+gaW0PM3jK6Xqwi2J6p5QGrdKS+YADTBWMw8ApgZcyic36iJ6DQQdV/ko54wg zfg2Q4p4LuLYF1lhTBOWH2cvsjUcZuKSua1N3fTz+fSpJkQoB0BL9pKSftgMeWjCPJ72 vdeHqgnWEuVK+sYg8lrPDqVoC5oylz+t7kLoWCrQ4wNjiL0+8bwnEi7sAPpGHfG9RPiH 4Rmg== X-Gm-Message-State: AJIora/7vHI8BB/00MiV4cDEOtpy1+04RUH0tYDF3vRFdkoVoJ6u1aOT J/UB9ThrEIXwTb/WV6WARUMryA== X-Google-Smtp-Source: AGRyM1tfI/a0XswTl9zWy7klWOZegxdyf+AgVvsQqVZ2RN6hG8W2eCK3caMais/j2oseH5w5i6M4xw== X-Received: by 2002:a5d:64c7:0:b0:21d:a742:5e2c with SMTP id f7-20020a5d64c7000000b0021da7425e2cmr14376966wri.178.1657919355571; Fri, 15 Jul 2022 14:09:15 -0700 (PDT) Received: from mai.box.freepro.com ([2a05:6e02:1041:c10:a223:f4b3:40c9:43fa]) by smtp.gmail.com with ESMTPSA id b14-20020a5d40ce000000b0021a56cda047sm4567752wrq.60.2022.07.15.14.09.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Jul 2022 14:09:14 -0700 (PDT) From: Daniel Lezcano To: daniel.lezcano@linaro.org, rafael@kernel.org Cc: quic_manafm@quicinc.com, rui.zhang@intel.com, amitk@kernel.org, lukasz.luba@arm.com, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/4] thermal/core: Encapsulate the trip point crossed function Date: Fri, 15 Jul 2022 23:09:08 +0200 Message-Id: <20220715210911.714479-1-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The routine where the trip point crossed is detected is a strategic place where different processing will happen. Encapsulate the code in a function, so all specific actions related with a trip point crossed can be grouped. Reviewed-by: Lukasz Luba Signed-off-by: Daniel Lezcano --- drivers/thermal/thermal_core.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index cdc0552e8c42..d9f771b15ed8 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -358,6 +358,25 @@ static void handle_critical_trips(struct thermal_zone_device *tz, tz->ops->critical(tz); } +static void handle_thermal_trip_crossed(struct thermal_zone_device *tz, int trip, + int trip_temp, int trip_hyst, enum thermal_trip_type trip_type) +{ + if (tz->last_temperature == THERMAL_TEMP_INVALID) + return; + + if (tz->last_temperature < trip_temp && + tz->temperature >= trip_temp) { + thermal_notify_tz_trip_up(tz->id, trip, + tz->temperature); + } + + if (tz->last_temperature >= trip_temp && + tz->temperature < (trip_temp - trip_hyst)) { + thermal_notify_tz_trip_down(tz->id, trip, + tz->temperature); + } +} + static void handle_thermal_trip(struct thermal_zone_device *tz, int trip) { enum thermal_trip_type type; @@ -372,16 +391,7 @@ static void handle_thermal_trip(struct thermal_zone_device *tz, int trip) if (tz->ops->get_trip_hyst) tz->ops->get_trip_hyst(tz, trip, &hyst); - if (tz->last_temperature != THERMAL_TEMP_INVALID) { - if (tz->last_temperature < trip_temp && - tz->temperature >= trip_temp) - thermal_notify_tz_trip_up(tz->id, trip, - tz->temperature); - if (tz->last_temperature >= trip_temp && - tz->temperature < (trip_temp - hyst)) - thermal_notify_tz_trip_down(tz->id, trip, - tz->temperature); - } + handle_thermal_trip_crossed(tz, trip, trip_temp, hyst, type); if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT) handle_critical_trips(tz, trip, type); From patchwork Fri Jul 15 21:09:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 591099 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 E8EC3C433EF for ; Fri, 15 Jul 2022 21:09:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230481AbiGOVJ3 (ORCPT ); Fri, 15 Jul 2022 17:09:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230144AbiGOVJY (ORCPT ); Fri, 15 Jul 2022 17:09:24 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B858470E67 for ; Fri, 15 Jul 2022 14:09:18 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id a5so8318998wrx.12 for ; Fri, 15 Jul 2022 14:09:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KuR49etHrCUC4nyK2G7BLQAMCLtkwxAEZTehEbHHsZ8=; b=vrTYMEyO0VU2Ius0MQsOmHiv+LMWF2wbXfb4qzWDHIDvVx/y3DSrb1ngD2MbWv8khD 3ZDDnGtdvMKq3bkdW2EWM7QXuGg5o3Q64e2ejCzlNJDDsiSFiD2qBTnHBocnecHPbpzJ KzHdRroE00f4WBJ0KjQQMs0RhoAlYt6vaXts3b+SLVbu9EWFmoS/7LtkE8M+rjgGqwMJ 6V12K8wba4wASUUF/ecMtxwilXHmm2SSin+y4A3h7n54sX7oJDIS5lal14Q9ixnBfVXf fgCCVgFwDA8fDurCl1rGMl3XStfkmRT/VltNbTp7/rdgTVkkXFG5zAbnTvdqAewygSNS xQKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KuR49etHrCUC4nyK2G7BLQAMCLtkwxAEZTehEbHHsZ8=; b=hxIjKl3ndnlhIBdLB0bQa/+9aPYZFD9LYPGDmRan7rj3Kzj0jFhy14VFDIq1wN8Gqo e9y72+9MzSdEPA67NgQcFrhJBt6lqrRgPz2tey7zS2F+aKx6uJ66aX1JaH5xxlKPkOIG fIV53Yhr75tuar/JCf2GbVNCmucZQeCXS8Xd/BHt+gi7siTfhd0qadcxQHmIHGtKg/+T +64mdLTx0RfGKTcxMBEMz/3bNalKOn22UIoR+QaiyAVdYTu3WjsJfCmiz9tokT8Fg8eA GvXbWD7mDUWrFM/hPXbP55zHXhGLww6Mg46/OmmLW3Oc84kvD1o7jPlSsgOadxX9+hD6 I/1Q== X-Gm-Message-State: AJIora9XtVA/1eq2aH0V6eCMslj5BbbKZGm84lj1k3LtriPMmceh8Dd2 d6OkQzL7O4h/tmm6AkiT0v89Jw== X-Google-Smtp-Source: AGRyM1tDsHvcoC8bUH++rJtR85y2lhI21/AEsXL5BW92qh3m2/p9Cnovv9+bj/VcddLcShI0vlNdrQ== X-Received: by 2002:a5d:47a1:0:b0:21d:a4c6:352b with SMTP id 1-20020a5d47a1000000b0021da4c6352bmr13789167wrb.365.1657919357055; Fri, 15 Jul 2022 14:09:17 -0700 (PDT) Received: from mai.box.freepro.com ([2a05:6e02:1041:c10:a223:f4b3:40c9:43fa]) by smtp.gmail.com with ESMTPSA id b14-20020a5d40ce000000b0021a56cda047sm4567752wrq.60.2022.07.15.14.09.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Jul 2022 14:09:16 -0700 (PDT) From: Daniel Lezcano To: daniel.lezcano@linaro.org, rafael@kernel.org Cc: quic_manafm@quicinc.com, rui.zhang@intel.com, amitk@kernel.org, lukasz.luba@arm.com, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/4] thermal/core: Avoid calling ->get_trip_temp() unnecessarily Date: Fri, 15 Jul 2022 23:09:09 +0200 Message-Id: <20220715210911.714479-2-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220715210911.714479-1-daniel.lezcano@linaro.org> References: <20220715210911.714479-1-daniel.lezcano@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org As the trip temperature is already available when calling the function handle_critical_trips(), pass it as a parameter instead of having this function calling the ops again to retrieve the same data. Reviewed-by: Lukasz Luba Signed-off-by: Daniel Lezcano Reviewed-by: Zhang Rui --- v3: - Massaged the patch title and the description --- drivers/thermal/thermal_core.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index d9f771b15ed8..f66036b3daae 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -340,12 +340,8 @@ void thermal_zone_device_critical(struct thermal_zone_device *tz) EXPORT_SYMBOL(thermal_zone_device_critical); static void handle_critical_trips(struct thermal_zone_device *tz, - int trip, enum thermal_trip_type trip_type) + int trip, int trip_temp, enum thermal_trip_type trip_type) { - int trip_temp; - - tz->ops->get_trip_temp(tz, trip, &trip_temp); - /* If we have not crossed the trip_temp, we do not care. */ if (trip_temp <= 0 || tz->temperature < trip_temp) return; @@ -394,7 +390,7 @@ static void handle_thermal_trip(struct thermal_zone_device *tz, int trip) handle_thermal_trip_crossed(tz, trip, trip_temp, hyst, type); if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT) - handle_critical_trips(tz, trip, type); + handle_critical_trips(tz, trip, trip_temp, type); else handle_non_critical_trips(tz, trip); /* From patchwork Fri Jul 15 21:09:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 591098 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 35632CCA47F for ; Fri, 15 Jul 2022 21:09:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231400AbiGOVJf (ORCPT ); Fri, 15 Jul 2022 17:09:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230259AbiGOVJ1 (ORCPT ); Fri, 15 Jul 2022 17:09:27 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40F5D71BCC for ; Fri, 15 Jul 2022 14:09:20 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id bu1so8324140wrb.9 for ; Fri, 15 Jul 2022 14:09:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=b68+VkE2RUgPNZUt8j2qwSumBu0/WI+4XHwjD20XVZo=; b=FjGKwMEwxuikEabAjOrG8kyXP5sCFAElt6LXQ/g1c6oZ6zNsjV+6obW4uNc+90rEZf L16OdBfikT6D18o3oE855q83+tYoR33TeQ9JRU/W0wbDs1iColkFnINh1dLaxAijLOzC oKsJWze8zV/pyOy4IcyJBy2DdrPT37VorByHE8o+UOedT4wPHMoI9R5FYZZGveYYRPLq 3YQ6sPKUUn3srPli+bQCcHsfeCS2kCJ2R+dAQLeYC8AO2Y2vituKXFURpiyoq+J013IP 2FGkLkemUoZR+YuGFAJW/EPf5vB6LwKtWkISnIXlkBIu0yvUl4QwGKTFjXRiLWZEIoD8 kKrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=b68+VkE2RUgPNZUt8j2qwSumBu0/WI+4XHwjD20XVZo=; b=v58P8h41URfzgt8jkg6fNyB+qiabWaFSghmA9lx+nayObdAvb4qHNQTiu2IvtNtCib YEvGz366vbZyOIqOOT6chikbkFoQ65BEP48IQIxM4jIgKxWICxxyga3RU4Yvqr8/ssdi SRjr8vBo10gcG+FXvn7gnB+PGeKFemLjaXUnLWMADdK8CZWJ1xDjeS0Nv7oFMwFfEQH2 vujFqFs55cHqGzDte5QZ8r0KPx6KE7/Tg0C9n3YTqvzYgsiLgsGNBEbI7OrOfUPUeD6r ZV535N30OcepWqDln06vE3y0QaHRlWuhLzzww8TcqvDCA4mML5EPVHCTiKxXJ1k9yhQj yF2w== X-Gm-Message-State: AJIora9J9TEtlD5Mv2z6qbm0NyCwdasj3AsX1c29fC+hPyojNPEhU6ol FAj5kY63ElOWyw5fdsR26rPKDA== X-Google-Smtp-Source: AGRyM1tTnLLXg5Xz3OFlTZ3Yn6BtoVIeItxO67MGpHo9HqZW3eEf4b9QWOzvydnBIRb9BkG4r+0L7g== X-Received: by 2002:a05:6000:1789:b0:21d:7699:3322 with SMTP id e9-20020a056000178900b0021d76993322mr13817279wrg.121.1657919358726; Fri, 15 Jul 2022 14:09:18 -0700 (PDT) Received: from mai.box.freepro.com ([2a05:6e02:1041:c10:a223:f4b3:40c9:43fa]) by smtp.gmail.com with ESMTPSA id b14-20020a5d40ce000000b0021a56cda047sm4567752wrq.60.2022.07.15.14.09.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Jul 2022 14:09:18 -0700 (PDT) From: Daniel Lezcano To: daniel.lezcano@linaro.org, rafael@kernel.org Cc: quic_manafm@quicinc.com, rui.zhang@intel.com, amitk@kernel.org, lukasz.luba@arm.com, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 3/4] thermal/core: Build ascending ordered indexes for the trip points Date: Fri, 15 Jul 2022 23:09:10 +0200 Message-Id: <20220715210911.714479-3-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220715210911.714479-1-daniel.lezcano@linaro.org> References: <20220715210911.714479-1-daniel.lezcano@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org By convention the trips points are declared in the ascending temperature order. However, no specification for the device tree, ACPI or documentation tells the trip points must be ordered this way. In the other hand, we need those to be ordered to browse them at the thermal events. But if we assume they are ordered and change the code based on this assumption, any platform with shuffled trip points description will be broken (if they exist). Instead of taking the risk of breaking the existing platforms, use an array of temperature ordered trip identifiers and make it available for the code needing to browse the trip points in an ordered way. Signed-off-by: Daniel Lezcano --- drivers/thermal/thermal_core.c | 62 +++++++++++++++++++++++++++------- include/linux/thermal.h | 2 ++ 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index f66036b3daae..f02f38b66445 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -355,7 +355,8 @@ static void handle_critical_trips(struct thermal_zone_device *tz, } static void handle_thermal_trip_crossed(struct thermal_zone_device *tz, int trip, - int trip_temp, int trip_hyst, enum thermal_trip_type trip_type) + int trip_temp, int trip_hyst, + enum thermal_trip_type trip_type) { if (tz->last_temperature == THERMAL_TEMP_INVALID) return; @@ -1165,6 +1166,46 @@ static void bind_tz(struct thermal_zone_device *tz) mutex_unlock(&thermal_list_lock); } +static void sort_trips_indexes(struct thermal_zone_device *tz) +{ + int i, j; + + for (i = 0; i < tz->trips; i++) + tz->trips_indexes[i] = i; + + for (i = 0; i < tz->trips; i++) { + for (j = i + 1; j < tz->trips; j++) { + int t1, t2; + + tz->ops->get_trip_temp(tz, tz->trips_indexes[i], &t1); + tz->ops->get_trip_temp(tz, tz->trips_indexes[j], &t2); + + if (t1 > t2) + swap(tz->trips_indexes[i], tz->trips_indexes[j]); + } + } +} + +static int thermal_zone_device_trip_init(struct thermal_zone_device *tz) +{ + enum thermal_trip_type trip_type; + int trip_temp, i; + + tz->trips_indexes = kzalloc(tz->trips * sizeof(int), GFP_KERNEL); + if (!tz->trips_indexes) + return -ENOMEM; + + for (i = 0; i < tz->trips; i++) { + if (tz->ops->get_trip_type(tz, i, &trip_type) || + tz->ops->get_trip_temp(tz, i, &trip_temp) || !trip_temp) + set_bit(i, &tz->trips_disabled); + } + + sort_trips_indexes(tz); + + return 0; +} + /** * thermal_zone_device_register() - register a new thermal zone device * @type: the thermal zone device type @@ -1196,11 +1237,8 @@ thermal_zone_device_register(const char *type, int trips, int mask, int polling_delay) { struct thermal_zone_device *tz; - enum thermal_trip_type trip_type; - int trip_temp; int id; int result; - int count; struct thermal_governor *governor; if (!type || strlen(type) == 0) { @@ -1272,12 +1310,9 @@ thermal_zone_device_register(const char *type, int trips, int mask, if (result) goto release_device; - for (count = 0; count < trips; count++) { - if (tz->ops->get_trip_type(tz, count, &trip_type) || - tz->ops->get_trip_temp(tz, count, &trip_temp) || - !trip_temp) - set_bit(count, &tz->trips_disabled); - } + result = thermal_zone_device_trip_init(tz); + if (result) + goto unregister; /* Update 'this' zone's governor information */ mutex_lock(&thermal_governor_lock); @@ -1290,7 +1325,7 @@ thermal_zone_device_register(const char *type, int trips, int mask, result = thermal_set_governor(tz, governor); if (result) { mutex_unlock(&thermal_governor_lock); - goto unregister; + goto kfree_indexes; } mutex_unlock(&thermal_governor_lock); @@ -1298,7 +1333,7 @@ thermal_zone_device_register(const char *type, int trips, int mask, if (!tz->tzp || !tz->tzp->no_hwmon) { result = thermal_add_hwmon_sysfs(tz); if (result) - goto unregister; + goto kfree_indexes; } mutex_lock(&thermal_list_lock); @@ -1319,6 +1354,8 @@ thermal_zone_device_register(const char *type, int trips, int mask, return tz; +kfree_indexes: + kfree(tz->trips_indexes); unregister: device_del(&tz->device); release_device: @@ -1387,6 +1424,7 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz) thermal_remove_hwmon_sysfs(tz); ida_simple_remove(&thermal_tz_ida, tz->id); ida_destroy(&tz->ida); + kfree(tz->trips_indexes); mutex_destroy(&tz->lock); device_unregister(&tz->device); diff --git a/include/linux/thermal.h b/include/linux/thermal.h index 231bac2768fb..4c3b72536772 100644 --- a/include/linux/thermal.h +++ b/include/linux/thermal.h @@ -112,6 +112,7 @@ struct thermal_cooling_device { * @mode: current mode of this thermal zone * @devdata: private pointer for device private data * @trips: number of trip points the thermal zone supports + * @trips_indexes: an array of sorted trip points indexes * @trips_disabled; bitmap for disabled trips * @passive_delay_jiffies: number of jiffies to wait between polls when * performing passive cooling. @@ -152,6 +153,7 @@ struct thermal_zone_device { enum thermal_device_mode mode; void *devdata; int trips; + int *trips_indexes; unsigned long trips_disabled; /* bitmap for disabled trips */ unsigned long passive_delay_jiffies; unsigned long polling_delay_jiffies; From patchwork Fri Jul 15 21:09:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 591274 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 EE7ACC433EF for ; Fri, 15 Jul 2022 21:09:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231224AbiGOVJe (ORCPT ); Fri, 15 Jul 2022 17:09:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229951AbiGOVJY (ORCPT ); Fri, 15 Jul 2022 17:09:24 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4FDA72ED1 for ; Fri, 15 Jul 2022 14:09:21 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id r2so7331710wrs.3 for ; Fri, 15 Jul 2022 14:09:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LBjVlwEWddWqbpURHhNM+E5xvdeghhlzqBOLCULhmQI=; b=e+S3BjS1PpKKswxkGI0iY7c0b0IKGpkD/DW+G3E/R+nDJKFDxGYujI2jryqm4pXs05 AD6t9NACr3CQZFw9nzkZgk/VNlYe9RYROB9Osq8LVylf1S8ra3H91gsTO6I3f70JKhwD 3THBQeDdNE+nCVncBkzTPYdr9Kmab1jkIfPCmK5KNcc7de6UpWbayWGKher0vQoMLEa8 2F1GPS+6za/UiSVPn+c4oe6o6pF98cQUifJqDYwIm2VaVz1X9EiAD6NfgvgSXkN8PVrE Qng3FU5exou1FoUeLfIAxp38tY1KnjQdde91hdT20wT+xeg6qMsemRXbos5S2jd2TINu 2bRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LBjVlwEWddWqbpURHhNM+E5xvdeghhlzqBOLCULhmQI=; b=h5TBp+H7TTvMdCnrPkIAKZ1e6uhxGpeeBD9vyc2MYklGU4ReJ3fH5NfBieqZo/EUJ1 PG1jijIXng7oF493EIzXiKA4Z2Qf+Be/7NCN79UHZshWG7T7kbXjk0ppMa6H9zAkQbZ+ Xkvb3aQkPon2y2xEXL/dFnaV8iqcYxgCyP4isBpexJQpVlRdFkMoQG+YNOa2SlurjY5j 7JWLwhk2kHVD9qOyF6TX0Hu/DQgtLAtL8SZaUefjNf22KbMYNf/4N6AlwCAMXDMHE+pp qDdWEoly4xkTpREA6PN9tpSOZOU5tr5jFGG48/HXuPXYa2Hw+fJ/H3jzc/bPFlmlmcNK S71A== X-Gm-Message-State: AJIora988Y/59Y+K0r7L9z187iWikxmwlZz8iPPRdiOKnLhwgy5RJZ0a 4jE08CmF9mX6qd6ASTNf4/sCFA== X-Google-Smtp-Source: AGRyM1tDc7pmR2AackU3bPHpp8kYvctzGvgmIQNpLv9oO+3tgwGp0K8GBsfnKkbGElv2cXlWsF7JBA== X-Received: by 2002:a5d:5a99:0:b0:21d:cb28:7468 with SMTP id bp25-20020a5d5a99000000b0021dcb287468mr6316741wrb.289.1657919360359; Fri, 15 Jul 2022 14:09:20 -0700 (PDT) Received: from mai.box.freepro.com ([2a05:6e02:1041:c10:a223:f4b3:40c9:43fa]) by smtp.gmail.com with ESMTPSA id b14-20020a5d40ce000000b0021a56cda047sm4567752wrq.60.2022.07.15.14.09.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Jul 2022 14:09:19 -0700 (PDT) From: Daniel Lezcano To: daniel.lezcano@linaro.org, rafael@kernel.org Cc: quic_manafm@quicinc.com, rui.zhang@intel.com, amitk@kernel.org, lukasz.luba@arm.com, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 4/4] thermal/core: Fix thermal trip cross point Date: Fri, 15 Jul 2022 23:09:11 +0200 Message-Id: <20220715210911.714479-4-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220715210911.714479-1-daniel.lezcano@linaro.org> References: <20220715210911.714479-1-daniel.lezcano@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The routine doing trip point crossing the way up or down is actually wrong. A trip point is composed with a trip temperature and a hysteresis. The trip temperature is used to detect when the trip point is crossed the way up. The trip temperature minus the hysteresis is used to detect when the trip point is crossed the way down. |-----------low--------high------------| |<--------->| | hyst | | | | -|--> crossed the way up | <---|-- crossed the way down For that, there is a two point comparison: the current temperature and the previous temperature. The actual code assumes if the current temperature is greater than the trip temperature and the previous temperature was lesser, then the trip point is crossed the way up. That is true only if we crossed the way down the low temperature boundary from the previous temperature or if the hysteresis is zero. The temperature can decrease between the low and high, so the trip point is not crossed the way down and then increase again and cross the high temperature raising a new trip point crossed detection which is incorrect. The same scenario happens when crossing the way down. The trip point crossing the way up and down must act as parenthesis, a trip point down must close a trip point up. Today we have multiple trip point up without the corresponding trip point down. In order to fix that, we store the previous trip point which gives the information about the previous trip and we change the trip point browsing order depending on the temperature trend: in the ascending order when the temperature trend is raising, otherwise in the descending order. As a sidenote, the thermal_zone_device structure has already the prev_trip_low and prev_trip_high information which are used by the thermal_zone_set_trips() function. This one can be changed to be triggered by the trip temperature crossing function, which makes more sense, and the two fields will disappear. Tested on a rk3399-rock960 with thermal stress and 4 trip points. Also tested with temperature emulation to create a temperature jump directly to the second trip point. Signed-off-by: Daniel Lezcano Reviewed-by: Zhang Rui --- V3: - Use the ordered indexes introduced in the previous patch as the trip could be not ordered V2: - As spotted by Zhang Rui, the trip cross notification does not work if the temperature drops and crosses two trip points in the same update interval. In order to fix that, we browse the trip point in the ascending order when the temperature trend is raising, otherwise in the descending order. Signed-off-by: Daniel Lezcano --- drivers/thermal/thermal_core.c | 54 ++++++++++++++++++++++++---------- include/linux/thermal.h | 2 ++ 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index f02f38b66445..a5c5f6f4e42b 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -354,30 +354,48 @@ static void handle_critical_trips(struct thermal_zone_device *tz, tz->ops->critical(tz); } -static void handle_thermal_trip_crossed(struct thermal_zone_device *tz, int trip, +static void handle_thermal_trip_crossed(struct thermal_zone_device *tz, int index, int trip_temp, int trip_hyst, enum thermal_trip_type trip_type) { + int trip_low_temp = trip_temp - trip_hyst; + int trip = tz->trips_indexes[index]; + if (tz->last_temperature == THERMAL_TEMP_INVALID) return; - if (tz->last_temperature < trip_temp && - tz->temperature >= trip_temp) { - thermal_notify_tz_trip_up(tz->id, trip, - tz->temperature); - } - - if (tz->last_temperature >= trip_temp && - tz->temperature < (trip_temp - trip_hyst)) { - thermal_notify_tz_trip_down(tz->id, trip, - tz->temperature); + /* + * Due to the hysteresis, a third information is needed to + * detect when the temperature is wavering between the + * trip_low_temp and the trip_temp. A trip point is crossed + * the way up only if the temperature is above it while the + * previous temperature was below *and* we crossed the + * trip_temp_low before. The previous trip point give us the + * previous trip point transition. The similar problem exists + * when crossing the way down. + * + * Note the mechanism works only if the caller of the function + * invoke the function with the trip point ascending or + * descending regarding the temperature trend. A temperature + * drop trend will browse the trip point in the descending + * order + */ + if (tz->last_temperature < trip_temp && tz->temperature >= trip_temp && + index != tz->prev_index) { + thermal_notify_tz_trip_up(tz->id, trip, tz->temperature); + tz->prev_index = index; + } else if (tz->last_temperature >= trip_low_temp && tz->temperature < trip_low_temp && + index == tz->prev_index) { + thermal_notify_tz_trip_down(tz->id, trip, tz->temperature); + tz->prev_index--; } } -static void handle_thermal_trip(struct thermal_zone_device *tz, int trip) +static void handle_thermal_trip(struct thermal_zone_device *tz, int index) { enum thermal_trip_type type; int trip_temp, hyst = 0; + int trip = tz->trips_indexes[index]; /* Ignore disabled trip points */ if (test_bit(trip, &tz->trips_disabled)) @@ -388,7 +406,7 @@ static void handle_thermal_trip(struct thermal_zone_device *tz, int trip) if (tz->ops->get_trip_hyst) tz->ops->get_trip_hyst(tz, trip, &hyst); - handle_thermal_trip_crossed(tz, trip, trip_temp, hyst, type); + handle_thermal_trip_crossed(tz, index, trip_temp, hyst, type); if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT) handle_critical_trips(tz, trip, trip_temp, type); @@ -428,6 +446,7 @@ static void thermal_zone_device_init(struct thermal_zone_device *tz) { struct thermal_instance *pos; tz->temperature = THERMAL_TEMP_INVALID; + tz->prev_index = -1; tz->prev_low_trip = -INT_MAX; tz->prev_high_trip = INT_MAX; list_for_each_entry(pos, &tz->thermal_instances, tz_node) @@ -512,8 +531,13 @@ void thermal_zone_device_update(struct thermal_zone_device *tz, tz->notify_event = event; - for (count = 0; count < tz->trips; count++) - handle_thermal_trip(tz, count); + if (tz->last_temperature <= tz->temperature) { + for (count = 0; count < tz->trips; count++) + handle_thermal_trip(tz, count); + } else { + for (count = tz->trips; count >= 0; count--) + handle_thermal_trip(tz, count); + } } EXPORT_SYMBOL_GPL(thermal_zone_device_update); diff --git a/include/linux/thermal.h b/include/linux/thermal.h index 4c3b72536772..d512f21561f1 100644 --- a/include/linux/thermal.h +++ b/include/linux/thermal.h @@ -125,6 +125,7 @@ struct thermal_cooling_device { * @last_temperature: previous temperature read * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION * @passive: 1 if you've crossed a passive trip point, 0 otherwise. + * @prev_index: previous index pointing to the trip point the thermal zone was * @prev_low_trip: the low current temperature if you've crossed a passive trip point. * @prev_high_trip: the above current temperature if you've crossed a @@ -161,6 +162,7 @@ struct thermal_zone_device { int last_temperature; int emul_temperature; int passive; + int prev_index; int prev_low_trip; int prev_high_trip; atomic_t need_update;