From patchwork Mon Jul 18 14:50:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 591797 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 D4934C43334 for ; Mon, 18 Jul 2022 14:50:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231547AbiGROur (ORCPT ); Mon, 18 Jul 2022 10:50:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234215AbiGROuq (ORCPT ); Mon, 18 Jul 2022 10:50:46 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D49DE00C for ; Mon, 18 Jul 2022 07:50:45 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id j29-20020a05600c1c1d00b003a2fdafdefbso7490497wms.2 for ; Mon, 18 Jul 2022 07:50:45 -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=PtA/Vgq5PiNGud9rKna9mzmJfODWF8PJRqr5/r8NLs4=; b=WK95xA/gIiwpzl143+RS3MKXQXXrdJJxlZGcA8WB76YcsXLNVr2m7fmruDnBVxd0rn n9xX65WxXW5RdrXz+tzN2/kBce3rxy/N/W9wBYwGQq3dX5y8WAO74XqyVpkTlYetv4jf YGWpVSd/YOZYaR8Lo2CRJvFFmEsb77hMtpr8Im7n/qWW9SRLcpNcVrM/fi1QVbj7gjHi hsJX5uCU/OvEYEJ8u14deJqrWNn3V3A6TXUkOLcKrgAYTn4TNb2qT6BiS+0JLXc2TO/4 G01mTposA//H/hajSZXpdRGPfq+e4DyOunhK7qghYfcEc8oMEATEpq7HnI4dQK0gejD3 ukcw== 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=PtA/Vgq5PiNGud9rKna9mzmJfODWF8PJRqr5/r8NLs4=; b=Wq8ENW/U6ORqLGLn6N7TLmxOj/aXm7n+g7VltV5Tb9U4paxG65rzwQ6IdGYD/WKagi c4Iap4TvifXcQ9ym7YRu0hyoZzyVsxXP+w/mnjhtpJKCxX8XY/q52G2d+rkeXWfSiPDq xlfY9hD3yOqa+DxoSBYnSjXm/3CZR/+aBH0YBYN8lL556o23X1GeM/yO2jJaYt2kTKWt SpmTQR+4PkbfkofLuhjID6gbucrEbbDtV7Re7wq3y/HdF8Mk3BsIW13X99/qr/DJ0h93 5oflS+3wuMMnqhm2pWNsjk8pBhTJnLDycurdsXRtg3T7Pj5W/UXjpwVvEiOTBA9Oj/PE wKbw== X-Gm-Message-State: AJIora8WS5au8Ks317Gj/nVCB9vqmsKeW/c/JU+xVVc9VPSDMhf40IES dRk34Mj04PiZQUjlT3hYStUqFw== X-Google-Smtp-Source: AGRyM1ul1T/l+Y5Uxhn8youVNp340vtJvsQN7CPoCjAjbTHRAitsKou9x8R/FUcb8qw2rxAZR3fG0Q== X-Received: by 2002:a7b:c7d2:0:b0:3a3:209d:cdc6 with SMTP id z18-20020a7bc7d2000000b003a3209dcdc6mr1561325wmk.55.1658155843809; Mon, 18 Jul 2022 07:50:43 -0700 (PDT) Received: from mai.box.freepro.com ([2a05:6e02:1041:c10:496e:2d41:fd5a:4e5e]) by smtp.gmail.com with ESMTPSA id r18-20020a05600c35d200b003a310631750sm11643715wmq.35.2022.07.18.07.50.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 07:50:43 -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 v4 1/4] thermal/core: Encapsulate the trip point crossed function Date: Mon, 18 Jul 2022 16:50:35 +0200 Message-Id: <20220718145038.1114379-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 Reviewed by: Zhang Rui 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 0d9e9b175f93..fa5767c6d5f4 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 Mon Jul 18 14:50:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 591574 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 3E86AC43334 for ; Mon, 18 Jul 2022 14:50:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234370AbiGROuu (ORCPT ); Mon, 18 Jul 2022 10:50:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234315AbiGROut (ORCPT ); Mon, 18 Jul 2022 10:50:49 -0400 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B31CDDF88 for ; Mon, 18 Jul 2022 07:50:46 -0700 (PDT) Received: by mail-wm1-x336.google.com with SMTP id b6so6753546wmq.5 for ; Mon, 18 Jul 2022 07:50:46 -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=cn3TgSP5aiTmcU023jjuDtoPr/sDYC9llPRhRIcuSNQ=; b=J4UuT1JSEzRi8CSx5a3btUkARmkINwON2hswFgyE9FKa6Mkqe35VAs/lX/6s2UVLw/ YJVh/StXREGEQOYsePJ4KccPZEAuf83PT6Rp/98K5chE2/fjSvhJRMeTP8rG8zzyjeiy QQFghKbcDe+Relyc0inoxylwrlL3CMbzERLy2pgB24hkDwM+Lc9me2JF/TTCVGVfPq4P dkRu1ALj6RFSoj2vMH0g9O0KeP+UsSi6OMLbks49EAqsdkuNuc+S7gZDzjBZ9tsamMA6 81IILAwRBSlFmsazGufmU1FecSEU1ZtY2QQGeWaA+yc3gSaZfcBBLbI+FAhVwCIYHR/R 1ffQ== 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=cn3TgSP5aiTmcU023jjuDtoPr/sDYC9llPRhRIcuSNQ=; b=jpgohLtV3UtBM4JxVK/OfdmoEsZ8vTUyII5101IVENgV48DriR/W6ZHs9mGyY0l5sR eB3RHF68MaZChkrcBu4UGeR7W3i+G4q1W6nWMFRfO3BMuZADb2YU0WcrX7Zk5BD1gcJe Icv2FFaXlZQzvBJA2rP5xqAG0kwn+KSkADS0k/gmMQ51g9utqUpJA75nwCMKZqFtxdeR 8BCGAP67hjANzZSrJ6HAfYuuFFi6khJdeTs2IzKyEnhEfgi99oJxXRutZ4UWYW116jgn x/arIRPjrI8+ToaXQpl82KAMYFX1AN1jkL7NoJDk3NruSi+WGLVXvD0M05QK3FFN49vO jf1g== X-Gm-Message-State: AJIora/xLeP5YJF/lACExit98XmTvopJgEsWqdE5Lwad/HiwbG6sklpo JwLBjDDK7ySlu7V9okpnfnw3JA== X-Google-Smtp-Source: AGRyM1ssLsM9xoEZO3rTBHoMyli5V+AqoDfJ4wBzLNdRXvWcSdjHwF7OWR8zE2F6vEmWhC7RM5A3Zw== X-Received: by 2002:a7b:ce8e:0:b0:3a3:1b01:8e7 with SMTP id q14-20020a7bce8e000000b003a31b0108e7mr5183073wmj.31.1658155845215; Mon, 18 Jul 2022 07:50:45 -0700 (PDT) Received: from mai.box.freepro.com ([2a05:6e02:1041:c10:496e:2d41:fd5a:4e5e]) by smtp.gmail.com with ESMTPSA id r18-20020a05600c35d200b003a310631750sm11643715wmq.35.2022.07.18.07.50.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 07:50:44 -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 v4 2/4] thermal/core: Avoid calling ->get_trip_temp() unnecessarily Date: Mon, 18 Jul 2022 16:50:36 +0200 Message-Id: <20220718145038.1114379-2-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220718145038.1114379-1-daniel.lezcano@linaro.org> References: <20220718145038.1114379-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 Reviewed-by: Zhang Rui Signed-off-by: Daniel Lezcano --- 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 fa5767c6d5f4..fc6ccc5edbfb 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 Mon Jul 18 14:50:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 591796 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 850F3CCA479 for ; Mon, 18 Jul 2022 14:50:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234315AbiGROuu (ORCPT ); Mon, 18 Jul 2022 10:50:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234435AbiGROut (ORCPT ); Mon, 18 Jul 2022 10:50:49 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37CFC17AB0 for ; Mon, 18 Jul 2022 07:50:48 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id 8-20020a05600c024800b003a2fe343db1so7489388wmj.1 for ; Mon, 18 Jul 2022 07:50:48 -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=k68R2/59EH+UP+DjwagqrQkr2q9+r1a26rRZqF6jhBY=; b=KX5y9B+zeDfHVsGiv04nVy4WsFEI1RzuFbOc7m7h2BT+9LDlbKuQcNE6qHoSx3LLYZ zlzXeZ712NSi0s1Fu3HhDcLloQ7Dxe8r57GaiBtA+iWOCl0lNDK55u/ZYoxUmM8KmLuv aRQaxk9wHUVLKtWQC/L7PmCYVgLxUWVUESW8g1JhBJfxOEzsstc46YcmVKt6hIxHDHf0 jfdTKT4G48PfGTZas1jRzCR1C8Wcc3dQXdsNh1cmCDQK0d7h1srO6XB++swvjLQbT9K3 jpe5253Z4LqgU+lEoPOIkevmAScwqMvH4eBA5/pDLmfR0BzJPSc5/okuC7oQ0JmtQPRj LCZw== 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=k68R2/59EH+UP+DjwagqrQkr2q9+r1a26rRZqF6jhBY=; b=RO1dNrK38wE9bTtckH4MvIB4NLtO+K7t9sZp/OKmF7Ii7tl0Ua1BDQTsCd4q6uArOv 5b9GvKn7s7E52Qtc8MaLt4q4xva7NICPVt3tx3/1cTFpl6o//S/c9fYdU0jtx6stj+sS kMILLnP1APQv1mLQtB4DGUUuXownJaBX9Ly/lrszHz9tctM7sEbuDsR2ZOwsjucV0dDY ZJV4qxFwc6xEIluxVeZG/W4vEU/k7VDx/MCr65hOMDoqtVIg5ttIx3WyKd3RXr5NnnJy WKMkcQgoLEe2LjUFLI3g27hAg1hxIyQZdo2cWtt0O5gGEqRRKCvsKKxbxAtnbstS6WUZ J08w== X-Gm-Message-State: AJIora+SGeHdFAOOJ2scjJghlvqKVeiqG4N9GhIKKK5I0/k7l2UxPaQ7 glN3Gu47DiVJjQkg3Tx62HroWw== X-Google-Smtp-Source: AGRyM1utRo3CPBs4csmD1ivXKxT4n5LfelOi9cdYqfZAL3N74948kf0vEbZO1m006IK4JsXb3BWZ/w== X-Received: by 2002:a05:600c:683:b0:3a2:fe34:3e1a with SMTP id a3-20020a05600c068300b003a2fe343e1amr25603120wmn.192.1658155846617; Mon, 18 Jul 2022 07:50:46 -0700 (PDT) Received: from mai.box.freepro.com ([2a05:6e02:1041:c10:496e:2d41:fd5a:4e5e]) by smtp.gmail.com with ESMTPSA id r18-20020a05600c35d200b003a310631750sm11643715wmq.35.2022.07.18.07.50.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 07:50:46 -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 v4 3/4] thermal/core: Build ascending ordered indexes for the trip points Date: Mon, 18 Jul 2022 16:50:37 +0200 Message-Id: <20220718145038.1114379-3-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220718145038.1114379-1-daniel.lezcano@linaro.org> References: <20220718145038.1114379-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 --- V4: - Fix conflicts due to tz->trips renamed to tz->num_trips Signed-off-by: Daniel Lezcano --- drivers/thermal/thermal_core.c | 63 +++++++++++++++++++++++++++------- include/linux/thermal.h | 2 ++ 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index fc6ccc5edbfb..f274dc7d9c48 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; @@ -1171,6 +1172,47 @@ static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms if (delay_ms > 1000) *delay_jiffies = round_jiffies(*delay_jiffies); } + +static void sort_trips_indexes(struct thermal_zone_device *tz) +{ + int i, j; + + for (i = 0; i < tz->num_trips; i++) + tz->trips_indexes[i] = i; + + for (i = 0; i < tz->num_trips; i++) { + + for (j = i + 1; j < tz->num_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->num_trips * sizeof(int), GFP_KERNEL); + if (!tz->trips_indexes) + return -ENOMEM; + + for (i = 0; i < tz->num_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_with_trips() - register a new thermal zone device @@ -1204,11 +1246,8 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t 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) { @@ -1281,12 +1320,9 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t if (result) goto release_device; - for (count = 0; count < num_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); @@ -1299,7 +1335,7 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t result = thermal_set_governor(tz, governor); if (result) { mutex_unlock(&thermal_governor_lock); - goto unregister; + goto kfree_indexes; } mutex_unlock(&thermal_governor_lock); @@ -1307,7 +1343,7 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t 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); @@ -1328,6 +1364,8 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t return tz; +kfree_indexes: + kfree(tz->trips_indexes); unregister: device_del(&tz->device); release_device: @@ -1406,6 +1444,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 1386c713885d..4e576184df49 100644 --- a/include/linux/thermal.h +++ b/include/linux/thermal.h @@ -125,6 +125,7 @@ struct thermal_cooling_device { * @devdata: private pointer for device private data * @trips: an array of struct thermal_trip * @num_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. @@ -166,6 +167,7 @@ struct thermal_zone_device { void *devdata; struct thermal_trip *trips; int num_trips; + int *trips_indexes; unsigned long trips_disabled; /* bitmap for disabled trips */ unsigned long passive_delay_jiffies; unsigned long polling_delay_jiffies; From patchwork Mon Jul 18 14:50:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 591573 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 C0760C43334 for ; Mon, 18 Jul 2022 14:50:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234452AbiGROuv (ORCPT ); Mon, 18 Jul 2022 10:50:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234019AbiGROuv (ORCPT ); Mon, 18 Jul 2022 10:50:51 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA75BE00C for ; Mon, 18 Jul 2022 07:50:49 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id r14so17444064wrg.1 for ; Mon, 18 Jul 2022 07:50:49 -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=jxqLuVHiz8hdZpFHyInhO7icbJrVe+z9vKVptS+rBqQ=; b=v25DyjQs8jmutA2h0KQ5xf+XwD7at62EyQOOkQDtNugFB4A0h/wHhleQsktjfZrh+P aFGM7Zk8WAO4DZV1eXwyOtPcSOSfFU2AMabONZAhwgRJDKSniFdUGdLuEm2IhKc6VkJI fNSTgC+poXlsQTBmJrrP4GLbUpMlB47z/8kY/yGgdSH9rJgTGIxHT+JnS8eY2Dlgq86+ +LIQzM1LS3hgVvYIU2r2j2Vn9rjnObId/msTeOKlikbAqh/XEFPKD5TaClPk4hn+SCfs blahL4TmHf5ydpu+00KMwcIfAO9l+pAK9QJtgSUW9W1r/blC3HnQ4EoREcUtYO+opARJ Ie4g== 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=jxqLuVHiz8hdZpFHyInhO7icbJrVe+z9vKVptS+rBqQ=; b=PL7YXCI8aRM9Si0uB5Z4KG8SYqvsGYQOjoMkMV9cr4N1e2ZqHgAnHaqn2k9Qg7MQaQ dAdpe4xwR9zcDo3sD78niHPeFPmgmr0tx2atQeWzrlPwmbgpV1tlKUwK47OoIQYwJkRw JsiZYu+pdUg0nTHd2gWH4C97e/A7mGwCA8FItNKbYSeEsLz1/xmIFtcf3mO+vYvDVoJC T6Jmc3ddYN0Uw6Q/clWYVecp3gdeN4WuT4bsCh1FyuqN1jI4lfAx8YiI/0BtQD0AxPIV OaUGudKhbqQfQccJDo3bspZCeJh4jxZUx/FQ3A6PTH99sW+iMz9uw+McuFZxyTvE8mic FiKg== X-Gm-Message-State: AJIora8/Ik8OwkfmUX3J007Ys6eJJlPlkheBsOqbUT2hw5Au315cmDKO Xn4SfPxfBYGuG46aUVtyZ1shaw== X-Google-Smtp-Source: AGRyM1vcGDTXXKjAkaiGE02Xo98iEu/7qf/LKb5YfXuVh4lyJQ5Bio6e/vO5KZJih+wu6nI7yFJ/bw== X-Received: by 2002:a05:6000:1a87:b0:21d:b5b9:7666 with SMTP id f7-20020a0560001a8700b0021db5b97666mr23722501wry.1.1658155848121; Mon, 18 Jul 2022 07:50:48 -0700 (PDT) Received: from mai.box.freepro.com ([2a05:6e02:1041:c10:496e:2d41:fd5a:4e5e]) by smtp.gmail.com with ESMTPSA id r18-20020a05600c35d200b003a310631750sm11643715wmq.35.2022.07.18.07.50.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 07:50:47 -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 v4 4/4] thermal/core: Fix thermal trip cross point Date: Mon, 18 Jul 2022 16:50:38 +0200 Message-Id: <20220718145038.1114379-4-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220718145038.1114379-1-daniel.lezcano@linaro.org> References: <20220718145038.1114379-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. Reviewed-by: Zhang Rui Signed-off-by: Daniel Lezcano --- V4: - Fix conflict due to tz->trips renamed to tz->num_trips 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 f274dc7d9c48..c43d078b7656 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->num_trips; count++) - handle_thermal_trip(tz, count); + if (tz->last_temperature <= tz->temperature) { + for (count = 0; count < tz->num_trips; count++) + handle_thermal_trip(tz, count); + } else { + for (count = tz->num_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 4e576184df49..65ba65ec95b0 100644 --- a/include/linux/thermal.h +++ b/include/linux/thermal.h @@ -138,6 +138,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 @@ -175,6 +176,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;