Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2618247pxb; Sun, 17 Oct 2021 20:40:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwIcIS84j2siYLV3LdHmAosQRS6l5UrqQ7T5JSL4d5/17xH4X4EG0fbXBsjswKirVqqlrvW X-Received: by 2002:a17:90a:fb87:: with SMTP id cp7mr44939772pjb.114.1634528419796; Sun, 17 Oct 2021 20:40:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634528419; cv=none; d=google.com; s=arc-20160816; b=s23V4tIwtMSrTdg4lh9iZDPypZA7V+hJKtK/w9/J4QaEwxBaJniAaW/F2owma9zCJh dBwnOtgChIPw0NuAAr2/x1n1GiPUTG0jolOPQDmYXo94Y2H3QmdwcIcC0e0Fh3JYCbcR dPWnLdPu70i+87pqqS6yDJX4jxptGPiynt5Sce6X9AEKmFEscOTGN9dPMtKRawFhx+sn Q2iBitsQESehkCynDUv6IpPdQwUpFJj+2J0AgXARXa/4nkvjah4rUk0fKT4OKLhP5nD9 LIIusrpX7wY01z1S1ifYmVz9x8epF3mv/qcQ8jtyVVh8m/fDt7bikW+YpuM/L3sksrSk v39A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=BqbX/wOwlUQGVZxjSyS1aiwJt03MkC15YQIT8Xq1qPI=; b=HyOTd2VHdPK3MrJXGY5k5di8GtfndkvDmjNVorjEPTzLZ4nC+Y+q3bJyWusyTkq7BN RKStU5OU2dWHuJ3r5OobfDbZWrVZd8Ffw+GqzCJ9Tci21UQ2PEaabl9y+qhZLL8LjKws WieCJm4nYEoTZtayjxdl43ByLUrlGf4NYwrGIfWv8nPebhP2JVC04OrB4qbnYWGqYz8c Hwco01T3cQrjN7sDx1zpxgd51gVkY3i6w9zC2/KmXwinUJeMzd7S2YQfCEB7mZjRnEBs RIogaOyrQBadIRQ4j2+YUl4SgARsaSWvpcHEJvbL+jUA1uoxPRVKFlNokr6DLjHRCI7T Q98w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Vi5in+qP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z191si508994pfc.353.2021.10.17.20.40.07; Sun, 17 Oct 2021 20:40:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Vi5in+qP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236932AbhJPW0S (ORCPT + 98 others); Sat, 16 Oct 2021 18:26:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236645AbhJPW0K (ORCPT ); Sat, 16 Oct 2021 18:26:10 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 605A6C061765 for ; Sat, 16 Oct 2021 15:24:00 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id y74-20020a1c7d4d000000b00322f53b9bbfso710292wmc.3 for ; Sat, 16 Oct 2021 15:24:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=BqbX/wOwlUQGVZxjSyS1aiwJt03MkC15YQIT8Xq1qPI=; b=Vi5in+qPCWcYwZt0+DU0yyOxtYcml4HWBFOXO3+j2piaNB1BbtatiVB0u2uwvT2FD/ Emduaij2cKTKSANLgZSKY5Ui5oKh9iMzsWmZeQjY2tddPoynROuld4H0tGdIqSMluuwQ ojYbMv5G7M3aXryljgjbzMbM1aG1wQZtgj7JsY6ozzw74ohPdiYwOAuSaq2brkKXgH3u GWwvwgTjB2IKxiRxW0F2AGQFSs9m+eXhsg/kML3tZ5nYDX3CmtQWmHmw4o1gEpUqVK/4 qs0GN3QwEf2Z0O87xOFqWZ2GbiJEHCW+9P2c4WqFsgMSEju0S7bGhudC4YPjvogU/4tr cbBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=BqbX/wOwlUQGVZxjSyS1aiwJt03MkC15YQIT8Xq1qPI=; b=hRLBk4QB5QQq5Yigkd4kE9/wrd4XQPmaet5KU1MwEzqOOdGCYtsZ22buPaPk/Z2h2a HC+qzB0a94aP8pC3E0EDpEwJBFMIpEWsGnFyasna6Eg87V5MY89YQG27HxTVOocZywgG Rm4probPaI2TDDCr/Ol7PnADoK5y1GYzw6xOCUqvMGwiGe4KuG0XIe7nSbzhXXqlsian GJpJpSM2lBE6O+zOP1VMAvqM99S+mOdXZ7gF+mnHHgrVHcOw9Cu6jA7EvRWrifVL3bCy trpHGaJg32thNaHKzx7Mb5gSLpuwFBYq2n7B4TzsQMl/Ae1p+bpY9RUKzOmpxU/FOWZ+ 9hCg== X-Gm-Message-State: AOAM533PxoxhrpJspq11WjZ50ZPxCpz+1oat0sN+arrLDINBvoxmEph7 A4nsh2KtqVweqYQOnI83I0deqg== X-Received: by 2002:a05:600c:896:: with SMTP id l22mr21122832wmp.92.1634423038800; Sat, 16 Oct 2021 15:23:58 -0700 (PDT) Received: from ?IPv6:2a01:e34:ed2f:f020:f04d:f65f:efd5:698? ([2a01:e34:ed2f:f020:f04d:f65f:efd5:698]) by smtp.googlemail.com with ESMTPSA id y191sm16229441wmc.36.2021.10.16.15.23.57 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 16 Oct 2021 15:23:58 -0700 (PDT) Subject: Re: [PATCH] thermal: imx: Fix temperature measurements on i.MX6 after alarm To: =?UTF-8?B?TWljaGFsIFZva8OhxI0=?= , Andrzej Pietrasiewicz , linux-pm@vger.kernel.org, Shawn Guo Cc: Amit Kucheria , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, =?UTF-8?Q?Petr_Bene=c5=a1?= , petrben@gmail.com, stable@vger.kernel.org References: <20211008081137.1948848-1-michal.vokac@ysoft.com> From: Daniel Lezcano Message-ID: Date: Sun, 17 Oct 2021 00:23:56 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: <20211008081137.1948848-1-michal.vokac@ysoft.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 08/10/2021 10:11, Michal Vokáč wrote: > From: Petr Beneš > > SoC temperature readout may not work after thermal alarm fires interrupt. > This harms userspace as well as CPU cooling device. > > Two issues with the logic involved. First, there is no protection against > concurent measurements, hence one can switch the sensor off while > the other one tries to read temperature later. Second, the interrupt path > usually fails. At the end the sensor is powered off and thermal IRQ is > disabled. One has to reenable the thermal zone by the sysfs interface. > > Most of troubles come from commit d92ed2c9d3ff ("thermal: imx: Use > driver's local data to decide whether to run a measurement") Are these troubles observed and reproduced ? Or is it your understanding from reading the code ? get_temp() and tz enable/disable are protected against races in the core code via the tz mutex > It uses data->irq_enabled as the "local data". Indeed, its value is > related to the state of the sensor loosely under normal operation and, > frankly, gets unleashed when the thermal interrupt arrives. > > Current patch adds the "local data" (new member sensor_on in > imx_thermal_data) and sets its value in controlled manner.> > Fixes: d92ed2c9d3ff ("thermal: imx: Use driver's local data to decide whether to run a measurement") > Cc: petrben@gmail.com > Cc: stable@vger.kernel.org > Signed-off-by: Petr Beneš > Signed-off-by: Michal Vokáč > --- > drivers/thermal/imx_thermal.c | 30 ++++++++++++++++++++++++++---- > 1 file changed, 26 insertions(+), 4 deletions(-) > > diff --git a/drivers/thermal/imx_thermal.c b/drivers/thermal/imx_thermal.c > index 2c7473d86a59..df5658e21828 100644 > --- a/drivers/thermal/imx_thermal.c > +++ b/drivers/thermal/imx_thermal.c > @@ -209,6 +209,8 @@ struct imx_thermal_data { > struct clk *thermal_clk; > const struct thermal_soc_data *socdata; > const char *temp_grade; > + struct mutex sensor_lock; > + bool sensor_on; > }; > > static void imx_set_panic_temp(struct imx_thermal_data *data, > @@ -252,11 +254,12 @@ static int imx_get_temp(struct thermal_zone_device *tz, int *temp) > const struct thermal_soc_data *soc_data = data->socdata; > struct regmap *map = data->tempmon; > unsigned int n_meas; > - bool wait, run_measurement; > + bool wait; > u32 val; > > - run_measurement = !data->irq_enabled; > - if (!run_measurement) { > + mutex_lock(&data->sensor_lock); > + > + if (data->sensor_on) { > /* Check if a measurement is currently in progress */ > regmap_read(map, soc_data->temp_data, &val); > wait = !(val & soc_data->temp_valid_mask); > @@ -283,13 +286,15 @@ static int imx_get_temp(struct thermal_zone_device *tz, int *temp) > > regmap_read(map, soc_data->temp_data, &val); > > - if (run_measurement) { > + if (!data->sensor_on) { > regmap_write(map, soc_data->sensor_ctrl + REG_CLR, > soc_data->measure_temp_mask); > regmap_write(map, soc_data->sensor_ctrl + REG_SET, > soc_data->power_down_mask); > } > > + mutex_unlock(&data->sensor_lock); > + > if ((val & soc_data->temp_valid_mask) == 0) { > dev_dbg(&tz->device, "temp measurement never finished\n"); > return -EAGAIN; > @@ -339,20 +344,26 @@ static int imx_change_mode(struct thermal_zone_device *tz, > const struct thermal_soc_data *soc_data = data->socdata; > > if (mode == THERMAL_DEVICE_ENABLED) { > + mutex_lock(&data->sensor_lock); > regmap_write(map, soc_data->sensor_ctrl + REG_CLR, > soc_data->power_down_mask); > regmap_write(map, soc_data->sensor_ctrl + REG_SET, > soc_data->measure_temp_mask); > + data->sensor_on = true; > + mutex_unlock(&data->sensor_lock); > > if (!data->irq_enabled) { > data->irq_enabled = true; > enable_irq(data->irq); > } > } else { > + mutex_lock(&data->sensor_lock); > regmap_write(map, soc_data->sensor_ctrl + REG_CLR, > soc_data->measure_temp_mask); > regmap_write(map, soc_data->sensor_ctrl + REG_SET, > soc_data->power_down_mask); > + data->sensor_on = false; > + mutex_unlock(&data->sensor_lock); > > if (data->irq_enabled) { > disable_irq(data->irq); > @@ -728,6 +739,8 @@ static int imx_thermal_probe(struct platform_device *pdev) > } > > /* Make sure sensor is in known good state for measurements */ > + mutex_init(&data->sensor_lock); > + mutex_lock(&data->sensor_lock); > regmap_write(map, data->socdata->sensor_ctrl + REG_CLR, > data->socdata->power_down_mask); > regmap_write(map, data->socdata->sensor_ctrl + REG_CLR, > @@ -739,6 +752,8 @@ static int imx_thermal_probe(struct platform_device *pdev) > IMX6_MISC0_REFTOP_SELBIASOFF); > regmap_write(map, data->socdata->sensor_ctrl + REG_SET, > data->socdata->power_down_mask); > + data->sensor_on = false; > + mutex_unlock(&data->sensor_lock); > > ret = imx_thermal_register_legacy_cooling(data); > if (ret) > @@ -796,10 +811,13 @@ static int imx_thermal_probe(struct platform_device *pdev) > if (data->socdata->version == TEMPMON_IMX6SX) > imx_set_panic_temp(data, data->temp_critical); > > + mutex_lock(&data->sensor_lock); > regmap_write(map, data->socdata->sensor_ctrl + REG_CLR, > data->socdata->power_down_mask); > regmap_write(map, data->socdata->sensor_ctrl + REG_SET, > data->socdata->measure_temp_mask); > + data->sensor_on = true; > + mutex_unlock(&data->sensor_lock); > > data->irq_enabled = true; > ret = thermal_zone_device_enable(data->tz); > @@ -832,8 +850,12 @@ static int imx_thermal_remove(struct platform_device *pdev) > struct regmap *map = data->tempmon; > > /* Disable measurements */ > + mutex_lock(&data->sensor_lock); > regmap_write(map, data->socdata->sensor_ctrl + REG_SET, > data->socdata->power_down_mask); > + data->sensor_on = false; > + mutex_unlock(&data->sensor_lock); > + > if (!IS_ERR(data->thermal_clk)) > clk_disable_unprepare(data->thermal_clk); > > -- Linaro.org │ Open source software for ARM SoCs Follow Linaro: Facebook | Twitter | Blog