Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp351193rbb; Sat, 24 Feb 2024 03:29:20 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXS46U8CAYrUzwKDE9pFzbrn4iBYYiQslNsAZvLS4J3HPeZFpC1tCbJn4TBPqMixyxKYOwGxl3Akq8RDECujbcuaDr7OwuKU1vSeIW/Jg== X-Google-Smtp-Source: AGHT+IECwtBGVBQRBg+5tYLtN3wIemllT6slvqZfuGMslrsc2bgM/f4Jm6ihJnzfPSbNr35481iL X-Received: by 2002:a05:6402:1a36:b0:565:bbc4:9c88 with SMTP id be22-20020a0564021a3600b00565bbc49c88mr53306edb.20.1708774160724; Sat, 24 Feb 2024 03:29:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708774160; cv=pass; d=google.com; s=arc-20160816; b=lisoaU66eR4kZHpWgtnWXQFg9Xf98H4gUjsPdfy0lmvcBlSqkDa6GW6ge7nefCt/5O kZgPNt2z2l4aXNtVlol0razDgJEYurh4Vw3CXgnkBKi9/SAgnXE+IYHBT7LQ/xlxy8M8 VhIe3e6ZKgsCXGaRwLgVHKIZw9JsA3gYXRIUuJsnket2lb01j9t86kPcn4p7hbn8QTgl p502bkXZYfl7LbV6J/iSEtAsBNapdIhE2otCulNnVJVNIBnSpff6a3WvXKR8tTbM121n uT9dASNcdTkM48i5DVkvPXojaO3k0LQ0IKet8kP2WmxM9pdVzVi0w84xPqUGGGeqkRV/ lfug== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=JYAho2X90qK7tOrS84lGLoReElvfwN9Vc1VtqG59PbA=; fh=Sty37SHU7Aijum2bk/4LGsaBJUevakYPzgKLPFJ2xZg=; b=wO++KIAiGIpuCTq0jWIu7M0lmgxjZ7Cf0l1zxbKsz8KoFh3oJvatyN0M4QsHVHs9FN hi2huA2t+q0B74ylAninBTQpoT+2SwCCi4UjcSGRXvtF4HcbSqV2UF9GqQuBzwFtkY7O 1ENwq8eILiV0AHpMIhJ8ad6hcWmtUuJrrP0qYweSpz84b+rPFoO7Ve3FTOjQ1vx8GxvW dtffEwKaazXQMnWLnFVRhJYhgIatF3Y+ilESsTh5zEhBQ1+ahUURQqpFFfOhBmiFJluq s3aqdZZ/0zFLnESnvPccrlw9T05ssehAyWMqhaPc8qv2RBb1OKqICOz6VDpUU0bDfbAy cEXw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=lnITihWZ; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-79586-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79586-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id k2-20020a508ac2000000b00565bb8f9171si27387edk.76.2024.02.24.03.29.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Feb 2024 03:29:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-79586-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=lnITihWZ; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-79586-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79586-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 403EC1F22F43 for ; Sat, 24 Feb 2024 11:29:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 100DB2575E; Sat, 24 Feb 2024 11:29:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lnITihWZ" Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF08425565; Sat, 24 Feb 2024 11:29:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708774153; cv=none; b=UASbM7ixlcXLnMRdW7fuBnLYL+t8UeW/RNsyA84KKj8BtsyfE2ObPEGBDjDmTApRRquuQtBDSnG4R9oo0B90hOWwHEQRRcHmsMbfYOzGq7YEQGxJNHrQArkFb5kZsMF27r1FOAO6v1YHtwtgoDOX5v3pHuPjSduX6bQJ+O32uhs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708774153; c=relaxed/simple; bh=8Wmjz1P/B2yK+9hfRLUJbl27WLHSx0xp5D2G5Vt/aLY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XnZMXaRHeBCNxS7Yc6ETRsaw/ESC+XYbSdeBa25rt8sRkrNUoA5aEouB4ooFHmXc2hfU5wW1pK0IYA6NaYBj8FRNOAXyiu3BXrrZzBnzm5yXP9TJtm/gIosfkeyxLS6BrMe9w2X78aposIxShpN5daR3V1uOeyDFi/fYlX7fNQM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=lnITihWZ; arc=none smtp.client-ip=209.85.218.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-a3566c0309fso151004666b.1; Sat, 24 Feb 2024 03:29:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708774149; x=1709378949; darn=vger.kernel.org; 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=JYAho2X90qK7tOrS84lGLoReElvfwN9Vc1VtqG59PbA=; b=lnITihWZyVM8tLIkUE552jG2HVHhER9n8blbPFNfDqHIRArSD31rH1VUaeD2BGP+aB MYCU0lpg5ObZ7e9Odmw3+4MyqCvC5TnNR2xEj/I+fyAMaYaoLvyR5nrf/uuL4DhLlnUX P7z6BjIE0KwWmLCsrs3StImyToOgueBvXALM5r5mPk9OscdNkTkgeaF45zsTvubB5iC5 L9oil7N8OBZ3NQ1VSsf3irRrhBtw0Ve/YfT1bEsew+99KzImf4vAkvC48oNXAezu9vkA +l8gZ/+cAoERGQCuSML/5dh4yHjFcvDyx9BX81Da/t8bSMljF551lqUOLj8mWXHtxczT ZaMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708774149; x=1709378949; 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=JYAho2X90qK7tOrS84lGLoReElvfwN9Vc1VtqG59PbA=; b=TMxtfKDXdyxTeUws2tbzoF2dBk3s0bLfQYfxj3ZsZgjDLTjk9XG5nHhivOKpLb4248 V4Wa66/B3/bp+ZMv5z5wICxXu034ITixqk4YHZ3TJR+RuD3LViAYriK+AtMJjIxKouWQ yZqv1jUYoGOrG1FyIqxF5iJFNBXJMB/2txXLh+Zw5vvPvsrw0kdteEZIZe5ScuHde3HP oZ819OHJkx9dYp0v361oh1EnuRrf9vte1F+lOAE1+TVsde6Nk23zghZsT9arOsHgC4sc aCRRnPJaV9X1zSRLXoaEPphHSfsiKcADbKzjsT1/fGQcQ+qUNklcCu+ToPgtkiWZhHbn e2FA== X-Forwarded-Encrypted: i=1; AJvYcCXk8BUpE9hlgWDNJvZn9gN+3O5Jf6z6IjBt9l1+VJcG6P7G7pX2XP8cK7PMUIk3FHjsb9KVaNoUArATyZv59ZxFscfmkwvJ5LQ99nDIFhyeAWzarl3R/64maXwsftSeFZF2cEuX1UZR X-Gm-Message-State: AOJu0Yy/kBt3CxUuuOgd+vUuejBWyH1thI0yJaRDycKAuby8J5VdS8hZ 2xEA8FgG8IwF63/h9UHu4psfvbTaAjLQkKgNpqQuUiNKTF+lksCsBJpqku1HmNI= X-Received: by 2002:a17:906:2b05:b0:a40:65ea:61 with SMTP id a5-20020a1709062b0500b00a4065ea0061mr1475681ejg.46.1708774148756; Sat, 24 Feb 2024 03:29:08 -0800 (PST) Received: from localhost.localdomain (p5dc68d61.dip0.t-ipconnect.de. [93.198.141.97]) by smtp.gmail.com with ESMTPSA id s1-20020a170906060100b00a3d9e6e9983sm507181ejb.174.2024.02.24.03.29.08 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sat, 24 Feb 2024 03:29:08 -0800 (PST) From: Leif Middelschulte X-Google-Original-From: Leif Middelschulte To: =?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 v4 1/2] pwm: imx27: fix race condition .apply,.get_state Date: Sat, 24 Feb 2024 12:29:00 +0100 Message-Id: <20240224112902.55539-1-Leif.Middelschulte@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20230906154215.4ikrrbx4xgx2nmu5@pengutronix.de> References: <20230906154215.4ikrrbx4xgx2nmu5@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 | 55 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/drivers/pwm/pwm-imx27.c b/drivers/pwm/pwm-imx27.c index 7d9bc43f12b0..cb564460b79c 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,6 +119,31 @@ 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->state.period, NSEC_PER_MSEC); + int tries = MX3_PWM_FIFOAV_EMPTY_LOOP; + int fifoav, previous_fifoav = INT_MAX; + 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 0; + /* if the FIFO value does not decrease, there is another problem */ + if (previous_fifoav == fifoav) + break; + previous_fifoav = fifoav; + msleep(period_ms); + } + + return -EAGAIN; +} + static int pwm_imx27_get_state(struct pwm_chip *chip, struct pwm_device *pwm, struct pwm_state *state) { @@ -161,10 +187,35 @@ 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. + */ + ret = pwm_imx27_wait_fifo_empty(chip, pwm); + if (ret) + return ret; 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.3 (Apple Git-145)