Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp8619837rwp; Wed, 19 Jul 2023 12:36:06 -0700 (PDT) X-Google-Smtp-Source: APBJJlFensY4OTphdJZEaRZ31Oun/fwc4cOo0c82z9t4moeWsVybb5qs6WNSBlYWGT4ym0WgQt1e X-Received: by 2002:a2e:3c09:0:b0:2b9:3684:165 with SMTP id j9-20020a2e3c09000000b002b936840165mr737411lja.8.1689795366198; Wed, 19 Jul 2023 12:36:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689795366; cv=none; d=google.com; s=arc-20160816; b=EuZBEVuAGIJE64GoUIg3Qj+n5YbRiA0S8imh9vp9cw4m5GQT4Qf8sbeiw9IfCZ0nAI lT1gXt/AXgja3p4e2p1XzukmtiPeirweG9ZMQqRBQVg1zwLw9grbPbTe4cwBACqMtZKx 9+uibCH76aFSrBfFVmz2LRXJnj9irWarudPHFYsBiHPiytQ3dSsQGTqvQCrFm6wom5PY 7rzRCRkFStFhVSVDEWNIDy2Usu7ymSj7+/rGNNGAZZuv+Np5f5ak3yMBWyxdPdJQjxgL TOrwqsMDgD+/u9BkQmx5JkBtZIKE387NVCLOhTVAZ5prI8ZVOS9ibe16WaFCBihAp9ua tzcA== 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=wvp7Fzom6G8eQBNnvii+v1anqp2ftWoWgCCVQB4T9M4=; fh=2fL3qmmhGXf+1jsl/FWClzGQUxMsMIm6JIxD+4F2j8A=; b=tIFSt0a5jeI2+4rd3ootMfwQYEEISqdxdp8Ikc0cBd/jXCC6vl+w+/u5HJwS1L+G40 UNsudK7XSjORbW9CvpOIgWy+Z0TmbTREeJXBM9tx5cQIgMJp5RmS4x//6dLaa/1yQpcp uaW1vsGZuKs9ISxGk0S9pdcMedION4U8+nKcTEyFd2P/+zIeMpLWrOv09bdmnoPjcI+l 7brc85+VJKkTpXaFbACoyRTEublobM7Jh3qSw6Iqicy/T4B59BxIHKUHLTD1FUQWIjvl Wsar1j//9edwxSqm6qe7yxopgBi6yDm+EwQxdLpiOF1Mj1Gl4xQXCuJBRr1Z8UtjzKLF He6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=MUtWUYLg; 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 d18-20020a170906545200b009893415be37si3225554ejp.501.2023.07.19.12.35.41; Wed, 19 Jul 2023 12:36:06 -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=MUtWUYLg; 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 S230521AbjGSTXa (ORCPT + 99 others); Wed, 19 Jul 2023 15:23:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230106AbjGSTX3 (ORCPT ); Wed, 19 Jul 2023 15:23:29 -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 6E5D01BFC for ; Wed, 19 Jul 2023 12:23:27 -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 041DE6606F97; Wed, 19 Jul 2023 20:23:23 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1689794605; bh=9RSSdAc6acLEaTRxg2Yr+1H4A/wy2mLAUFDBTbvZP5s=; h=From:To:Cc:Subject:Date:From; b=MUtWUYLgNUeuwHYSntirqooUNHyZ0BrrrcNJ5H3f6QCxJiYVa2lKNGj8wET4RhpJn dr6a6Z4WvHSF7vkOflmuI0WUqBHgEfTb2TqSkuOKEgHJJKki8kJyk617mSISwLbg13 CcN2csyNS2VTdrt8iaQX+l2/YRmUs2j9B9A/mHy5mfJgGtnpQur7plUxthwm2bU3Je i2rGg6kO4ekhiyc6UK95BdF10Dfo4kp3o0hXP5XAAnXViA+a556AeMnbGNtTAsXGg+ Hw6WlTpXYkaaFcOncKkkCNqQBdA4aYXd9o6Fos1SaO7IrWIMS3e+wrHbWDyHyb0ihX /K7HNmSy4q+NA== 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 v2] spmi: mtk-pmif: Serialize PMIF status check and command submission Date: Wed, 19 Jul 2023 15:23:16 -0400 Message-ID: <20230719192319.252617-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,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 (during probe or at a later point) read the bus simultaneously, breaking the PMIF interface and consequently slowing down the whole system. To fix the issue at its root cause, introduce locking around the channel status check and the command write, so that both become an atomic operation, preventing race conditions between two (or more) SPMI bus read/write operations. 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 --- Changes in v2: - Added missing spin_unlocks to error paths - Moved memcpy outside spinlock region in the write_cmd function - Reworded commit message to make clear that issue can happen at any point in runtime, not only during boot drivers/spmi/spmi-mtk-pmif.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/drivers/spmi/spmi-mtk-pmif.c b/drivers/spmi/spmi-mtk-pmif.c index b3c991e1ea40..78b69e0b5c81 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,12 +336,14 @@ 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], data, GET_SWINF(data) == SWINF_IDLE, PMIF_DELAY_US, PMIF_TIMEOUT_US); if (ret < 0) { + spin_unlock_irqrestore(&arb->lock, flags); /* set channel ready if the data has transferred */ if (pmif_is_fsm_vldclr(arb)) pmif_writel(arb, 1, inf_reg->ch_rdy); @@ -350,6 +354,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, @@ -376,7 +381,8 @@ static int pmif_spmi_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid, struct pmif *arb = spmi_controller_get_drvdata(ctrl); struct ch_reg *inf_reg; int ret; - u32 data, cmd; + u32 data, wdata, cmd; + unsigned long flags; if (len > 4) { dev_err(&ctrl->dev, "pmif supports 1..4 bytes per trans, but:%zu requested", len); @@ -394,12 +400,17 @@ static int pmif_spmi_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid, else return -EINVAL; + /* Set the write data. */ + memcpy(&wdata, buf, len); + + 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], data, GET_SWINF(data) == SWINF_IDLE, PMIF_DELAY_US, PMIF_TIMEOUT_US); if (ret < 0) { + spin_unlock_irqrestore(&arb->lock, flags); /* set channel ready if the data has transferred */ if (pmif_is_fsm_vldclr(arb)) pmif_writel(arb, 1, inf_reg->ch_rdy); @@ -407,13 +418,12 @@ static int pmif_spmi_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid, return ret; } - /* Set the write data. */ - memcpy(&data, buf, len); - pmif_writel(arb, data, inf_reg->wdata); + pmif_writel(arb, wdata, inf_reg->wdata); /* 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 +498,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