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: 10880423 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 304681708 for ; Mon, 1 Apr 2019 17:58:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1E4C628630 for ; Mon, 1 Apr 2019 17:58:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 116FB28640; Mon, 1 Apr 2019 17:58:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 8B93828630 for ; Mon, 1 Apr 2019 17:58:26 +0000 (UTC) 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=5ch/xrypHN2owZP61pWHeFR+iYtt4uX2WHBjWHrih3I=; b=gkDJKqf7sgMuBW ObxY1mllbnNqcgrT19lywPXkqpaUy8Ku1H/auMzPHMqGZVymuI9ZATsUgXEWCeYZ9z0Bvg2Xp87xn PiWdKybEMRFpiuyBoHSy+zlg9u2vfvFATm71OGoU+lmSEUiKAu5/GiEfqJYgtS6xrdQhtljE303vZ iv220Iv6jMf437VDZ9Ax5hOeWPCj/wsN+JgaxySktSCfnlPBG7qhV6DD1dtvtpuRP8ZQKou4cl+KV Rmj847YDrIxNv0Gss3pn4SV/vif8mGEeP7fxPezOgFHpjvowgEb2GMXw76RmNZuL7OUt2CpFe+Fz/ rlV8CpLBdNELrLeTsuuw==; 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 1hB1CT-0007ZB-FX; Mon, 01 Apr 2019 17:58:25 +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-BeenThere: linux-amlogic@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: 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-amlogic" Errors-To: linux-amlogic-bounces+patchwork-linux-amlogic=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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,