From patchwork Mon Apr 1 17:57:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Blumenstingl X-Patchwork-Id: 1073085 Return-Path: X-Original-To: incoming-imx@patchwork.ozlabs.org Delivered-To: patchwork-incoming-imx@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-arm-kernel-bounces+incoming-imx=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=quarantine dis=none) header.from=googlemail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="Kc3H2GgB"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=googlemail.com header.i=@googlemail.com header.b="pNusP/cK"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44Y0St0SHWz9sPn for ; Tue, 2 Apr 2019 04:58:38 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=9xqYkP3PtH9LoEYvvMxkyaAI4KDjSJWYEIF12XefO7c=; b=Kc3H2GgBtb9SQE wMN5AP2PEBHLkahd6dHoQ0yjoClarnPE6q3gcw6UOSCbDH4NDTpPpCQPWrsVNWHWWwmm4gQCpOHhO ZXyiZDEeitI1Kr6mFpvJFipZfm4az6EbhoLd+VGOgoSIgG0B9dFabt3BECyqn9aBiFTuoyKMiIzyT q2/cCtrfglYc/oiRo4m0y/4tZ0VhqDeiRSwlN5gD0DDoLA0nkd+zLAjq0MySmTsYQvNSkeuBG84Tl 1W6pP4FX4Keb63Bwh+tmNKSoBFywt6wFijJaZL/jyJC4LAnc3916mPR4H3Es+mAnCijaBS0wsVNRq kZVmkulhWNhIyUhVGPTQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hB1CY-0007cw-AY; Mon, 01 Apr 2019 17:58:30 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hB1CJ-0007O2-Qr; Mon, 01 Apr 2019 17:58:17 +0000 Received: by mail-wm1-x344.google.com with SMTP id q16so349495wmj.3; Mon, 01 Apr 2019 10:58:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pPeKlSug6FmM3MV9fECsotZs7maoQ1cummrLUWibeCY=; b=pNusP/cKIaeO5Q/DtT644uMd+FpsNyGf/fm0rpTltdyxvSieh5878fQ9dYOBUQHAE8 Q7QZ3jFd0DLuCpmv0T9x3j7ie+JqoV74QwERzySM6SazHHu8etV7fdTNmOqBsGDoWNoG WXj/9ol9eTsbyMFJr4Usy913VQzbuxUSqT7kaYE7Qr3yz+CuDDjczOltqvpXfBAvFSr2 Xh4wl/ayUnFK7EA0fpHqEiKcQUtSNPSt5aB/lQGpQlxpqNaIqDlK0Iak0I6rSvS4goOA r8rOgrTEwjqHUPHDfu7PV8ZLw0Xw1qWyY91k/rHfyAnd/cZ+Q1WYsZFzpLkqHmOOeof+ KRfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pPeKlSug6FmM3MV9fECsotZs7maoQ1cummrLUWibeCY=; b=MKjILCEVWzfmTqtcC24ggjQuAxD0NiYT/t5gEgjBqwqcRU+RSKpkLot18WQvstKoxl +Gvv79XjyK5JMt08zj/VPRwNeqz6Otgiif9NqwYuTuPfmwfFXP2wsaThUg4lnNrZgd2d Q0FJFqe3Pz6izmkuEzIcRdYSAqBHgMkb6oNxkiJEOSXPUw2PxHyXCAoDJRvtSPiethVe c0yCgVZ1WIxmZZDF9QV+HeO6clx/gu1ez8rOq4KlEql1XFb3cajnR8TlhGNUAkZDmMUt sauyWY8UtOXGIeGIVm6MBgilJb09EeZkMnYUr3US47CyhmgIzeftT0NrErsbo6BaJGU9 a5Zg== X-Gm-Message-State: APjAAAUrm421+TMJj7LDKVcAgaTkUrWA4VPcSisswnFPYVN+8G+0U/Qy mpdYUOUItPABngJSSXnRQxw= X-Google-Smtp-Source: APXvYqwLD5cDnTfGVwaZOk5rR/snoJXyBwTXzaf8duMpIx+mp+J4/KQTC0IfGkqsNurwSL6B747Klw== X-Received: by 2002:a7b:cb58:: with SMTP id v24mr563202wmj.121.1554141493613; Mon, 01 Apr 2019 10:58:13 -0700 (PDT) Received: from blackbox.darklights.net (p200300DCD73A0800C9C8A7BE25478D36.dip0.t-ipconnect.de. [2003:dc:d73a:800:c9c8:a7be:2547:8d36]) by smtp.googlemail.com with ESMTPSA id s189sm16655340wmf.45.2019.04.01.10.58.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 01 Apr 2019 10:58:13 -0700 (PDT) From: Martin Blumenstingl To: thierry.reding@gmail.com, linux-pwm@vger.kernel.org, linux-amlogic@lists.infradead.org Subject: [PATCH v2 1/1] pwm: meson: use the spin-lock only to protect register modifications Date: Mon, 1 Apr 2019 19:57:48 +0200 Message-Id: <20190401175748.5376-2-martin.blumenstingl@googlemail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190401175748.5376-1-martin.blumenstingl@googlemail.com> References: <20190401175748.5376-1-martin.blumenstingl@googlemail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190401_105815_864642_1E2F866E X-CRM114-Status: GOOD ( 19.48 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:344 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (martin.blumenstingl[at]googlemail.com) -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: narmstrong@baylibre.com, Martin Blumenstingl , linux-kernel@vger.kernel.org, u.kleine-koenig@pengutronix.de, linux-arm-kernel@lists.infradead.org, jbrunet@baylibre.com Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+incoming-imx=patchwork.ozlabs.org@lists.infradead.org List-Id: linux-imx-kernel.lists.patchwork.ozlabs.org Holding the spin-lock for all of the code in meson_pwm_apply() can result in a "BUG: scheduling while atomic". This can happen because clk_get_rate() (which is called from meson_pwm_calc()) may sleep. Only hold the spin-lock when modifying registers to solve this. The reason why we need a spin-lock in the driver is because the REG_MISC_AB register is shared between the two channels provided by one PWM controller. The only functions where REG_MISC_AB is modified are meson_pwm_enable() and meson_pwm_disable() so the register reads/writes in there need to be protected by the spin-lock. The original code also used the spin-lock to protect the values in struct meson_pwm_channel. This could be necessary if two consumers can use the same PWM channel. However, PWM core doesn't allow this so we don't need to protect the values in struct meson_pwm_channel with a lock. Fixes: 211ed630753d2f ("pwm: Add support for Meson PWM Controller") Signed-off-by: Martin Blumenstingl Reviewed-by: Uwe Kleine-König Reviewed-by: Neil Armstrong --- drivers/pwm/pwm-meson.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/drivers/pwm/pwm-meson.c b/drivers/pwm/pwm-meson.c index c1ed641b3e26..f6e738ad7bd9 100644 --- a/drivers/pwm/pwm-meson.c +++ b/drivers/pwm/pwm-meson.c @@ -111,6 +111,10 @@ struct meson_pwm { const struct meson_pwm_data *data; void __iomem *base; u8 inverter_mask; + /* + * Protects register (write) access to the REG_MISC_AB register + * that is shared between the two PWMs. + */ spinlock_t lock; }; @@ -235,6 +239,7 @@ static void meson_pwm_enable(struct meson_pwm *meson, { u32 value, clk_shift, clk_enable, enable; unsigned int offset; + unsigned long flags; switch (id) { case 0: @@ -255,6 +260,8 @@ static void meson_pwm_enable(struct meson_pwm *meson, return; } + spin_lock_irqsave(&meson->lock, flags); + value = readl(meson->base + REG_MISC_AB); value &= ~(MISC_CLK_DIV_MASK << clk_shift); value |= channel->pre_div << clk_shift; @@ -267,11 +274,14 @@ static void meson_pwm_enable(struct meson_pwm *meson, value = readl(meson->base + REG_MISC_AB); value |= enable; writel(value, meson->base + REG_MISC_AB); + + spin_unlock_irqrestore(&meson->lock, flags); } static void meson_pwm_disable(struct meson_pwm *meson, unsigned int id) { u32 value, enable; + unsigned long flags; switch (id) { case 0: @@ -286,9 +296,13 @@ static void meson_pwm_disable(struct meson_pwm *meson, unsigned int id) return; } + spin_lock_irqsave(&meson->lock, flags); + value = readl(meson->base + REG_MISC_AB); value &= ~enable; writel(value, meson->base + REG_MISC_AB); + + spin_unlock_irqrestore(&meson->lock, flags); } static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, @@ -296,19 +310,16 @@ static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, { struct meson_pwm_channel *channel = pwm_get_chip_data(pwm); struct meson_pwm *meson = to_meson_pwm(chip); - unsigned long flags; int err = 0; if (!state) return -EINVAL; - spin_lock_irqsave(&meson->lock, flags); - if (!state->enabled) { meson_pwm_disable(meson, pwm->hwpwm); channel->state.enabled = false; - goto unlock; + return 0; } if (state->period != channel->state.period || @@ -329,7 +340,7 @@ static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, err = meson_pwm_calc(meson, channel, pwm->hwpwm, state->duty_cycle, state->period); if (err < 0) - goto unlock; + return err; channel->state.polarity = state->polarity; channel->state.period = state->period; @@ -341,9 +352,7 @@ static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, channel->state.enabled = true; } -unlock: - spin_unlock_irqrestore(&meson->lock, flags); - return err; + return 0; } static void meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,