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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 89B26C001B0 for ; Tue, 15 Aug 2023 10:44:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=eVlAfcRN1M4ax3E3exFxY4Olqn3OpF8eqqA0VvWig5c=; b=epVwrkkiNnF9le pMMZb+/MorAyzwlVTTRMFsz9knBKcBDjKGN7kmY919rTJ69iO4yO6CEL72TyoD0iOvbNdLLRzxQyM Cqm7Q7yjICcodWMFxqqvk5y6toSsbFgPqjxvWUqvIg4UwmpxtCOzenOOemcSFRERyTZK4aYQXLRGO ZJhXhwvTHDS4VW8rdRZPaErRebW6ok981a8zVi0fqGewduSBf+ULZNrEqeX9JNmMgmtEwkn7XMjyt KZs7wRcY1WApNLJA5GseRWD0qIg+gpbfE3Xw5vgunZ37FA7s87dcgisdZa53j50fW9smDZwiTuhfi wwrSx952+/Ypx+LeMfDg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qVrWg-001JWZ-1A; Tue, 15 Aug 2023 10:43:50 +0000 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qVrWc-001JUl-2A for linux-arm-kernel@lists.infradead.org; Tue, 15 Aug 2023 10:43:48 +0000 Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-99cdb0fd093so718946466b.1 for ; Tue, 15 Aug 2023 03:43:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692096220; x=1692701020; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=izO0tZzm3QLcY+sUcTdrcBNTqHL831hXZtp+UuXI19g=; b=S/WIormd94u7em4MZjGUFv8zIwrpEoyaTwbXs+rlOhpgq4lFQBYGWilNdyZiFCd4Oc eOLIju7rRkIgqloXyVFSdcUj5Fet4XAXLWf/7HGUbx/mpqg9SPLBmLwacuef15Ave6vA 38hQUkZ+qKOk6WyPOMqa1fT3LKDNkA/iJpX6PjCOD/ALXzJwxiE8aPATuzpZbMtD8Rki R7ny3A6XVRQJ/So0+CZeN9VaIBVW0YaNNOsmJOaDrEmScgJW60fcx7J4Zvrvq3hFUW+3 /P8y+PVV07Ngfevc0la8N3s3oWjrSqFpsaRcdH2PQmpn9JFYyN+Uw7vyq0hrD6Iqi9y1 e+Aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692096220; x=1692701020; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=izO0tZzm3QLcY+sUcTdrcBNTqHL831hXZtp+UuXI19g=; b=es/U279xdJdPdcFIDbK/ie/nYCdKMITpv2r0I7N02cmXMYt36KIbBR39u6xd9sZQR2 J5qmAecCz60LH4+7ObSH3U4yNPozXhyRitWXfgGKn9zaqSJdljfcZ4UVeynib2K1BHwf ehcCYcaMDLTRVZkUebygisKV4Rz1sD2S3GFCtXVBIOEa5xM7Gnhk1lsvGW4o8eK+41NY XTLpMUCivhV98NumCbDbABh40s+S+lBsrhYPz5eJG0+/F6ojVS5qAsJHVxGCUK4iIQ/k hC5FcJN+pJHxrK7qeFEpQoLOSmU4QAaKwP1ICJnj4738abVMZSzvBXaCEPQR+PmdkNg/ 4XMQ== X-Gm-Message-State: AOJu0YwI+sLjQCpQGDXDlMgXGQu7MWxrVrlAoPbCBjU1OBqW5FM/MJBZ SyGIlC3CrlTLDuYi28OqVhl1cI+je7gkgnXZ X-Google-Smtp-Source: AGHT+IEjn0TgKyCUAw/r/ZD1Yio4oGXG55KLp3wwLdcgB2uGZ7G7CxI+L1GmvoL+wV+Z6boGgz3n0g== X-Received: by 2002:a17:906:20d7:b0:99b:c949:5ef8 with SMTP id c23-20020a17090620d700b0099bc9495ef8mr10187701ejc.54.1692096220330; Tue, 15 Aug 2023 03:43:40 -0700 (PDT) Received: from localhost.localdomain (pd9e9e2b1.dip0.t-ipconnect.de. [217.233.226.177]) by smtp.gmail.com with ESMTPSA id md15-20020a170906ae8f00b0098d486d2bdfsm6944627ejb.177.2023.08.15.03.43.39 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 15 Aug 2023 03:43:39 -0700 (PDT) From: Leif Middelschulte X-Google-Original-From: Leif Middelschulte To: Thierry Reding , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team Cc: Leif Middelschulte , linux-pwm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/4] pwm: imx27: fix race condition .apply,.get_state Date: Tue, 15 Aug 2023 12:43:29 +0200 Message-Id: <20230815104332.55044-1-Leif.Middelschulte@gmail.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20230310174517.rb7xxrougkse2lrc@pengutronix.de> References: <20230310174517.rb7xxrougkse2lrc@pengutronix.de> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230815_034346_706306_66C7CA03 X-CRM114-Status: GOOD ( 26.54 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Leif Middelschulte With CONFIG_PWM_DEBUG=y after writing a value to the PWMSAR register in .apply(), the register is read in .get_state(). Unless a period completed in the meantime, this read yields the previously used duty cycle configuration. As the PWM_DEBUG code applies the read out configuration for testing purposes this effectively undoes the intended effect by rewriting the previous hardware state. Note that this change merely implements a sensible heuristic. The i.MX has a 4 slot FIFO to configure the duty cycle. This FIFO cannot be read back in its entirety. The "write x then read back x from hw" semantics are therefore not easily applicable. With this change, the .get_state() function tries to wait for some stabilization in the FIFO (empty state). In this state it keeps applying the last value written to the sample register. Signed-off-by: Leif Middelschulte --- drivers/pwm/pwm-imx27.c | 50 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/drivers/pwm/pwm-imx27.c b/drivers/pwm/pwm-imx27.c index 29a3089c534c..32389ca2da3e 100644 --- a/drivers/pwm/pwm-imx27.c +++ b/drivers/pwm/pwm-imx27.c @@ -75,6 +75,7 @@ (x)) + 1) #define MX3_PWM_SWR_LOOP 5 +#define MX3_PWM_FIFOAV_EMPTY_LOOP 4 /* PWMPR register value of 0xffff has the same effect as 0xfffe */ #define MX3_PWMPR_MAX 0xfffe @@ -118,8 +119,28 @@ static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx) clk_disable_unprepare(imx->clk_ipg); } +static int pwm_imx27_wait_fifo_empty(struct pwm_chip *chip, + struct pwm_device *pwm) +{ + struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip); + struct device *dev = chip->dev; + unsigned int period_ms = DIV_ROUND_UP_ULL(pwm_get_period(pwm), NSEC_PER_MSEC); + int tries = MX3_PWM_FIFOAV_EMPTY_LOOP; + int fifoav; + u32 sr; + + while (tries--) { + sr = readl(imx->mmio_base + MX3_PWMSR); + fifoav = FIELD_GET(MX3_PWMSR_FIFOAV, sr); + if (fifoav == MX3_PWMSR_FIFOAV_EMPTY) + return; + msleep(period_ms); + } + dev_warn(dev, "FIFO has been refilled concurrently\n"); +} + static int pwm_imx27_get_state(struct pwm_chip *chip, - struct pwm_device *pwm, struct pwm_state *state) + struct pwm_device *pwm, struct pwm_state *state) { struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip); u32 period, prescaler, pwm_clk, val; @@ -161,10 +182,33 @@ static int pwm_imx27_get_state(struct pwm_chip *chip, * PWMSAR can be read only if PWM is enabled. If the PWM is disabled, * use the cached value. */ - if (state->enabled) + if (state->enabled) { + /* + * From the i.MX PWM reference manual: + * "A read on the sample register yields the current FIFO value that + * is being used, or will be used, by the PWM for generation on the + * output signal. Therefore, a write and a subsequent read on the + * sample register may result in different values being obtained." + * Furthermore: + * "When a new value is written, the duty cycle changes after the + * current period is over." + * Note "changes" vs. "changes to the given value"! + * Finally: + * "The PWM will run at the last set duty-cycle setting if all the + * values of the FIFO has been utilized, until the FIFO is reloaded + * or the PWM is disabled." + * Try to be at least a bit more deterministic about which value is + * read by waiting until the FIFO is empty. In this state the last/most + * recently pushed sample (duty cycle) value is continuously applied. + * Beware that this approach is still racy, as a new value could have + * been supplied and a period expired between the call of the wait + * function and the subsequent readl. + */ + pwm_imx27_wait_fifo_empty(chip, pwm); val = readl(imx->mmio_base + MX3_PWMSAR); - else + } else { val = imx->duty_cycle; + } tmp = NSEC_PER_SEC * (u64)(val) * prescaler; state->duty_cycle = DIV_ROUND_UP_ULL(tmp, pwm_clk); -- 2.39.2 (Apple Git-143) _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel