Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp2974531rwp; Fri, 14 Jul 2023 14:52:22 -0700 (PDT) X-Google-Smtp-Source: APBJJlGkgx1DEZMmCy1NzJ1hbpxZjOattCUnfAJFNmlF8ezfNPddT5dnJhpdhf49ZVFtVh+GKsZq X-Received: by 2002:a05:6358:42aa:b0:134:eb97:ea01 with SMTP id s42-20020a05635842aa00b00134eb97ea01mr7376900rwc.27.1689371542599; Fri, 14 Jul 2023 14:52:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689371542; cv=none; d=google.com; s=arc-20160816; b=PEisHMwmw51Fe1kEIP949NMSKusCCmtmb0/Dh6DBpewjxWXwkVbSebB9Cz3QAEl7vA OQ9HkWgoKTCnw+Nh2Mf6/L3XSKHgfXVzB1VtgAcAaup3V2sXGioAXpPMLLZpA/30kmTP U07hAlbv86qxwmQdXxVVQSHxjsTVzZK6fW18uIBaqPzzPbHN+RcC+5Wf5TMncYGWtH1x mjOjLFfJzBZsXYIPRxa6+DIhkzmu5chknhhSWNe2XRpiWUh2sNZWB6iqLPQwAvO4hpjB akJ0Kb1i96NEa/XdjGRqP5Nxs1Q43Fs9Ejz8Gf7viESUw3nasvobKRpShB+6AZxLgL7X iNkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=f9vd/UBc1SwqgKl3wWxYWn0N3AMiNAnsf5IgxuUq+0U=; fh=2fL3qmmhGXf+1jsl/FWClzGQUxMsMIm6JIxD+4F2j8A=; b=al/s3NnUNTPqefExusWuCPJEWie5Xt3/C+FOE7zBPs3IZiKpQawGitqiWzOUDDilV3 5Fu0aAb3XetTdxIP1QQqUwZYUgGTe4xJ3maaJWPBZ4Htw8ozGLOtw3Cj6ZU/FGtiFYkf YijIbhXgV6fhvjbgXaOCVIas7hy2HD0UZiEunDhIiy3DLSUM+KnnJ1rgjrX8XCTuhXpd rkZxzBeGsnbk2fYZmw+k0741kAuLEFQ3dsfgZetmBEjnTglYWccU8QL6B6ZfBqVPc/oD O2twm0ocTHnqEsS5MV7urxc9fkHjn4pC2rQMboBBHthEVf+Bks+7p4ZHuPuSDliZ5hOt D4kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=jZRn7K12; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g15-20020a056a001a0f00b0067eeaacd277si7811961pfv.124.2023.07.14.14.52.09; Fri, 14 Jul 2023 14:52:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=jZRn7K12; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235982AbjGNVRv (ORCPT + 99 others); Fri, 14 Jul 2023 17:17:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233200AbjGNVRu (ORCPT ); Fri, 14 Jul 2023 17:17:50 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73FE32D63 for ; Fri, 14 Jul 2023 14:17:48 -0700 (PDT) Received: from notapiano.myfiosgateway.com (zone.collabora.co.uk [167.235.23.81]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: nfraprado) by madras.collabora.co.uk (Postfix) with ESMTPSA id 6AC5D6607061; Fri, 14 Jul 2023 22:17:44 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1689369466; bh=XnDJ8qbJU/PYiUdNGYMysu5RX/VThqsRxmhyWnGafds=; h=From:To:Cc:Subject:Date:From; b=jZRn7K12Ns+WqeS++HfR8mExp7Lsn3OxEK67DFBBgAU/8urfa5rPaiCXQgAX/DYFq ebqxYECWUitP5vuJTx1N9Id5lzngzet/pCwkF0pNfakzsfbL5IsLvFhp70RIcMTYkL 7e5kYJ/+f5khcACX/Uv/82igcOV/Zx+06fGkG6N0ZKLfisLSjaJeq2wEVPbrFxVqa/ 4WJ7pJCZ+xQmvVW63BOLR0MMMw+1nz4c0Az68yu3QfhDMR5s9bEOXKBC2aIy1fDZ3s OmSxY2fQEhGDiUvU3LG+Z+Ij3Oxs+tiV+S8XQcXlu5qUhyptNoL9c1L5T9dpOsONH3 olYTtoKPudFaw== From: =?UTF-8?q?N=C3=ADcolas=20F=2E=20R=2E=20A=2E=20Prado?= To: Stephen Boyd Cc: kernel@collabora.com, AngeloGioacchino Del Regno , =?UTF-8?q?N=C3=ADcolas=20F=2E=20R=2E=20A=2E=20Prado?= , Greg Kroah-Hartman , Hsin-Hsiung Wang , James Lo , Matthias Brugger , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mediatek@lists.infradead.org Subject: [PATCH] spmi: mtk-pmif: Serialize PMIF status check and command submission Date: Fri, 14 Jul 2023 17:17:10 -0400 Message-ID: <20230714211739.42928-1-nfraprado@collabora.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Before writing the read or write command to the SPMI arbiter through the PMIF interface, the current status of the channel is checked to ensure it is idle. However, since the status only changes from idle when the command is written, it is possible for two concurrent calls to determine that the channel is idle and simultaneously send their commands. At this point the PMIF interface hangs, with the status register no longer being updated, and thus causing all subsequent operations to time out. This was observed on the mt8195-cherry-tomato-r2 machine, particularly after commit 46600ab142f8 ("regulator: Set PROBE_PREFER_ASYNCHRONOUS for drivers between 5.10 and 5.15") was applied, since then the two MT6315 devices present on the SPMI bus would probe assynchronously and sometimes read the bus simultaneously, breaking the PMIF interface and consequently slowing down the whole system. To fix the issue, introduce locking around the channel status check and the command write, so that both become an atomic operation. A spinlock is used since this is a fast bus, as indicated by the usage of the atomic variant of readl_poll, and '.fast_io = true' being used in the mt6315 driver, so spinlocks are already used for the regmap access. Fixes: b45b3ccef8c0 ("spmi: mediatek: Add support for MT6873/8192") Signed-off-by: NĂ­colas F. R. A. Prado --- drivers/spmi/spmi-mtk-pmif.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/drivers/spmi/spmi-mtk-pmif.c b/drivers/spmi/spmi-mtk-pmif.c index b3c991e1ea40..208ba0adfb98 100644 --- a/drivers/spmi/spmi-mtk-pmif.c +++ b/drivers/spmi/spmi-mtk-pmif.c @@ -50,6 +50,7 @@ struct pmif { struct clk_bulk_data clks[PMIF_MAX_CLKS]; size_t nclks; const struct pmif_data *data; + spinlock_t lock; }; static const char * const pmif_clock_names[] = { @@ -314,6 +315,7 @@ static int pmif_spmi_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid, struct ch_reg *inf_reg; int ret; u32 data, cmd; + unsigned long flags; /* Check for argument validation. */ if (sid & ~0xf) { @@ -334,6 +336,7 @@ static int pmif_spmi_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid, else return -EINVAL; + spin_lock_irqsave(&arb->lock, flags); /* Wait for Software Interface FSM state to be IDLE. */ inf_reg = &arb->chan; ret = readl_poll_timeout_atomic(arb->base + arb->data->regs[inf_reg->ch_sta], @@ -350,6 +353,7 @@ static int pmif_spmi_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid, /* Send the command. */ cmd = (opc << 30) | (sid << 24) | ((len - 1) << 16) | addr; pmif_writel(arb, cmd, inf_reg->ch_send); + spin_unlock_irqrestore(&arb->lock, flags); /* * Wait for Software Interface FSM state to be WFVLDCLR, @@ -377,6 +381,7 @@ static int pmif_spmi_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid, struct ch_reg *inf_reg; int ret; u32 data, cmd; + unsigned long flags; if (len > 4) { dev_err(&ctrl->dev, "pmif supports 1..4 bytes per trans, but:%zu requested", len); @@ -394,6 +399,7 @@ static int pmif_spmi_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid, else return -EINVAL; + spin_lock_irqsave(&arb->lock, flags); /* Wait for Software Interface FSM state to be IDLE. */ inf_reg = &arb->chan; ret = readl_poll_timeout_atomic(arb->base + arb->data->regs[inf_reg->ch_sta], @@ -414,6 +420,7 @@ static int pmif_spmi_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid, /* Send the command. */ cmd = (opc << 30) | BIT(29) | (sid << 24) | ((len - 1) << 16) | addr; pmif_writel(arb, cmd, inf_reg->ch_send); + spin_unlock_irqrestore(&arb->lock, flags); return 0; } @@ -488,6 +495,8 @@ static int mtk_spmi_probe(struct platform_device *pdev) arb->chan.ch_send = PMIF_SWINF_0_ACC + chan_offset; arb->chan.ch_rdy = PMIF_SWINF_0_VLD_CLR + chan_offset; + spin_lock_init(&arb->lock); + platform_set_drvdata(pdev, ctrl); err = spmi_controller_add(ctrl); -- 2.41.0