From nobody Fri Oct 10 13:32:46 2025 Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) (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 9A2E6291C05 for ; Fri, 13 Jun 2025 09:29:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749806966; cv=none; b=nhdluDPrb9E5h3kqDTHYZP4Yq+DljdaQ39IcxCPsP3xLwBuksKdPr4MUgh0oBMcpgNmww6TQrLR0M7O5+zF/vtBCjiJC7Zjs7ZRjSs+NTOJoHBOznq8i0AUD01UNA4jaCvqyoFHH/gI8JLEzbrbLTqFfJADjEyZvvkIylQ7y9Nc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749806966; c=relaxed/simple; bh=I6Uh0W1U/pEscoVd7miIYAu5wzDsffordzyPGY3jblE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=EU+5D/2PUJ5OIrwrMf45zmp9HvMIbvi389AsQV6ibKkFc1Ha4lOHAyVTOg4jr+wC6aNj0SRog6FxCYYE5Nh4EdWH2QaDU4R7pDeMCf9sJDUU/BSNVej80faqP0K3kKsNjhmTY405ec+dqDnJsTkcQmVCW35nea+ScGOZqJrfJwM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=MjveCkQJ; arc=none smtp.client-ip=209.85.221.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="MjveCkQJ" Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-3a53ee6fcd5so1198338f8f.1 for ; Fri, 13 Jun 2025 02:29:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1749806963; x=1750411763; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=jkMuSosV7oMRxRtp9yYE+2UdziIG4xoxewf16F9X2EY=; b=MjveCkQJJxrGSlTKV+eiyVOXZqfXG8DSwlfmmk8AZdqD2TezNvMdavuxkbJCcrCzmJ ozKgYVdpMuU8TPb79siT9bvIDQfVeRrKxPq7hBKgwum63b/R0L23MccPgkNgeahuLytp ZVoB6IP0VgwKYkjxEypttKTWTjtTqE6fww1cLOgEqYUbHKmSyUId7CXKaiByOfULkDfy RXIHB5kZeQUAYObAmueqRqkgWrDjcTnLzA9/clx1TlkjIrnP3WmSlTWDHPQdYT51CNoL 942izHKE4T+cVFlw6o/LAbXmIXidkBN2WPpZzTJz7OVFT0m86nCFt2pSdDNKBNbjM1mG rJqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749806963; x=1750411763; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jkMuSosV7oMRxRtp9yYE+2UdziIG4xoxewf16F9X2EY=; b=Bwx2flk5oDjEWPb3SnZqiRMWb4YqWNbdc/CPqmalxjyEsQrh8NWpnNk/E+eHJsqTXb ll9W/yg6h4z8QFp7g0nsevYDGCPHyDw+GlcybST9MxRZrn7SxUNiV6PE1n1PXDofpLem TS5Vyf0igDhmiTaiwLZxjccfwvdn78x7OSlF5UgbPHCu/F/X1YxnaD5Ire/EJnFlGCKk 8HoeQBeyCL++r8R+gzgbb2IO7+KOuVFbN9hsaNQ/bjEaRSgT3CPEDnmWwGQjiU19Ay3P 49fwhPLDbRUeMoopPy5VlmtQA1+3IJ+n54Lje8WKm8cAITX5UAYYsvmmKmeV1a968u5/ xWfQ== X-Forwarded-Encrypted: i=1; AJvYcCUlhc91Ga9xZs74DnOuU9fxYC2kflyVsN5+AA6l5TAsp146XPofsfJ/escy0/g0Ibk5OBSHgcCbpiptu28=@vger.kernel.org X-Gm-Message-State: AOJu0YxD/iZAP+QWVyplJsI3pKTqWvatd0chKGF9wD39njNNwITQHcDJ /K1UPOCda9yCC8796wYg3B6/6/Ph80T9tDDGd/+iO4mJ/PulstyMWpIoVRoO8kA9wJAtpjRHuGE Ipi+7 X-Gm-Gg: ASbGncsNNBV54vFndNB09xssfPeingC2AACwNcsSPtzbikJGW2TNDAFY3NpjnZIx9pL dxNE5Tb5PJ4fvdLj2SZ6xAAcoR6O46FgJX2gvCJGhaBiV9BvCvNUv5IxRBRLym9gNfQFIkUY62Q Bdzfs0lBR8ijnADkBM9jgoE2SoMO+AQYkgpxlOGAD/s02A0zWd8fsiN+ASFokijg30VwBDazoMQ XHHMLBmx3QhQ0f7chqmuBY/L2F64fTNPEMcpPFYR2YhSOYrxXnKkIy4FYU5IlqaSB8Ff7G1WjWH B3fWhDkS9Ghtl7U98p6Xdi7ifiR9RYxgjcNdsPfu4HJZdMWYPb587MdCMQbsgqrOE7uPvkk= X-Google-Smtp-Source: AGHT+IEsyyMaonG3OncFCbaWDdIJZCvvUaIQLI58D11q65RDAUSxHg9lX/idRHq3zl5QhSt6SMknHQ== X-Received: by 2002:a05:6000:230e:b0:3a5:276b:1ec0 with SMTP id ffacd0b85a97d-3a56870835emr1942312f8f.45.1749806962900; Fri, 13 Jun 2025 02:29:22 -0700 (PDT) Received: from ho-tower-lan.lan ([37.18.136.128]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a568b08a2bsm1805946f8f.62.2025.06.13.02.29.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jun 2025 02:29:22 -0700 (PDT) From: James Clark Date: Fri, 13 Jun 2025 10:28:56 +0100 Subject: [PATCH v2 1/5] spi: spi-fsl-dspi: Clear completion counter before initiating transfer Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250613-james-nxp-spi-dma-v2-1-017eecf24aab@linaro.org> References: <20250613-james-nxp-spi-dma-v2-0-017eecf24aab@linaro.org> In-Reply-To: <20250613-james-nxp-spi-dma-v2-0-017eecf24aab@linaro.org> To: Vladimir Oltean , Mark Brown Cc: Vladimir Oltean , Arnd Bergmann , Larisa Grigore , Frank Li , linux-spi@vger.kernel.org, imx@lists.linux.dev, linux-kernel@vger.kernel.org, James Clark X-Mailer: b4 0.14.0 In target mode, extra interrupts can be received between the end of a transfer and halting the module if the host continues sending more data. If the interrupt from this occurs after the reinit_completion() then the completion counter is left at a non-zero value. The next unrelated transfer initiated by userspace will then complete immediately without waiting for the interrupt or writing to the RX buffer. Fix it by resetting the counter before the transfer so that lingering values are cleared. This is done after clearing the FIFOs and the status register but before the transfer is initiated, so no interrupts should be received at this point resulting in other race conditions. Fixes: 4f5ee75ea171 ("spi: spi-fsl-dspi: Replace interruptible wait queue w= ith a simple completion") Signed-off-by: James Clark --- drivers/spi/spi-fsl-dspi.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/spi/spi-fsl-dspi.c b/drivers/spi/spi-fsl-dspi.c index 04c88d090c4d..744dfc561db2 100644 --- a/drivers/spi/spi-fsl-dspi.c +++ b/drivers/spi/spi-fsl-dspi.c @@ -1122,11 +1122,19 @@ static int dspi_transfer_one_message(struct spi_con= troller *ctlr, if (dspi->devtype_data->trans_mode =3D=3D DSPI_DMA_MODE) { status =3D dspi_dma_xfer(dspi); } else { + /* + * Reset completion counter to clear any extra + * complete()s from spurious interrupts that may have + * happened after the last message's completion but + * before the module was fully in stop mode. + */ + if (dspi->irq) + reinit_completion(&dspi->xfer_done); + dspi_fifo_write(dspi); =20 if (dspi->irq) { wait_for_completion(&dspi->xfer_done); - reinit_completion(&dspi->xfer_done); } else { do { status =3D dspi_poll(dspi); --=20 2.34.1 From nobody Fri Oct 10 13:32:46 2025 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (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 8D8DF291C22 for ; Fri, 13 Jun 2025 09:29:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749806967; cv=none; b=V88X3DfluGitx3yhLEveoskbcBuNHNQ49p8fMou75euDW56//yX3acqVZlqQjHbAv84JtTp5PebNz3xmARxhmF/g8+qdPx3dHrcy5A/Ydup6qH5AcIsvNnffFOdVsOSDGqGXJhSp1MOv99FaliPikQ+Wdosnz3JoLvP7231GFXU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749806967; c=relaxed/simple; bh=oPZC9WcHWp2uw7qUgqeltlwh3GKlbwEm7TL/tFX+PYc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=qCR9DhEr7f23plmbonCSruvwouREaf+6zWxPuKPVg0MNzZcxWX24fUGe5W4+mvAKya2LBI7wNtjA8VhLy5l3ZnXwuIgg9bSNyhNPPN3c/GrM2lOJRKiySwP6PWaKM+fOlscU3y9pJXzYBEMV1DpsOonyw8MrGrWrb2JYNg/5P1M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=JzqQt2A4; arc=none smtp.client-ip=209.85.221.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="JzqQt2A4" Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-3a361b8a664so1716293f8f.3 for ; Fri, 13 Jun 2025 02:29:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1749806964; x=1750411764; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=AGpw4hdT+pS+Tk0ux1ZccyT+nzLrIjJeYWKUMN+4QXw=; b=JzqQt2A4Sad4L8J1IsURmKsiYsrbiv8LKDYtcBGrqo1948/9em6cffwqWMyUeaPRcu 9sdxGkjdVjprs5BLfK4WbhWsAfHFcfqyqpYt4WR4Ye6RQqZLtoTKo2IlVZgELCwzCx5U +nE6ruqs8BgoKrxtv7jJRzMIUWphhkshW/h/MysbOd5pW+qKGcrnP2hPwCu74GpjYToa 7FokjpfT2ZgfwLUGp3hrnYr0TwLYwww5cv724YzXAFHvYcbXWF7xR8hw+hZOUyliFNHl C0LcwzLgim19Sw/Vjjv14Lvg/0WcMhXk7oNlao5ktlOimVuWVp8AtUtp2hox8Qpq+ScS p7iA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749806964; x=1750411764; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AGpw4hdT+pS+Tk0ux1ZccyT+nzLrIjJeYWKUMN+4QXw=; b=gJzb5KcQ7TZnSS6s1uibLzv6lwrgl5dmRvGLelKkTiiNvZVi5qthVF8O5jagH3+RGA ayTsQodfwx2VQh843zQy8j3FLlPPO/OaClQrY3nJ8F8kVN3hb0iffki7B0FXkbWx7x+K XDTWhUbUcIlpTzWFMhuOWOUIuEX+2exIYcWyeJ2ilJjA4naJsEPNaqp73KlQ8CbuL2ts u/KjJFmHWu2BWgE8i8BTGgwVWMW+7aaopzvMRpk0yu/S2lC8vz9bIIrSjg5jQG6coiZt IEUhJqYKwAiv9NIvYizwi/KlmtZlJiuAkENukVKwlwuRZuHgPwZ6BwQseCKfTLIP0amC XhCQ== X-Forwarded-Encrypted: i=1; AJvYcCWT5js8azucEqYDGsQx2HOt5ssbsTiQHL1moGSJcI4Am1hvvEFH43xCQ+V61VQ97vP/GC44nxZg3nNgykw=@vger.kernel.org X-Gm-Message-State: AOJu0YxUZnr8qbG+Mk0nlEiFTkfePsNpYWk3O4g8OayrfBmJ8FOsia6O uMfHGlBWT0k14nPPtVtM/GwaCiymVJREepsLLNPZhf8KhY5lGvSRrcOL8sZSi67CVoecWEwxxLJ FtuZ5 X-Gm-Gg: ASbGncs5BsXxAD9KRwE5KBp+QtLyULERtvjqCt4ZYRWmGkm8bMJtNj87Zo+j/LWQcy5 8/43tV4iKmMgvRrg2Cd0BvXpi/8inzC2A9ig9XD5Sl87in6+VXdOmYzsRTb4VYaPEOJjTIy3Vur enO7gi6s1haueQk1whB1YZkghV8o/nVKrWS3pW132zh1XPQrzZy4vQ/xJKJ/MuC9Wt/2/Vo92h9 iVVTwvcgSFUQT3O+q4DgmKcIZAxToHTzdEYR/c+appTXm9PbuU2jcci9YtvhwRSvDD+FCNTYVNV bHiqUkMjpqiC34uMge2COiw/X2kLg12jInNLOG34lNrhvm/Yju8J+vVCtp8gXA/TrL1cu7o= X-Google-Smtp-Source: AGHT+IEyN57GGxt89BBjLJPXb8Na+NGB2N/ZXIR/zS24fZTczxeUzsibUPRMUEUB3mdpyDV/5IDtqw== X-Received: by 2002:a5d:5f8f:0:b0:39f:175b:a68d with SMTP id ffacd0b85a97d-3a568683084mr1645149f8f.11.1749806963823; Fri, 13 Jun 2025 02:29:23 -0700 (PDT) Received: from ho-tower-lan.lan ([37.18.136.128]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a568b08a2bsm1805946f8f.62.2025.06.13.02.29.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jun 2025 02:29:23 -0700 (PDT) From: James Clark Date: Fri, 13 Jun 2025 10:28:57 +0100 Subject: [PATCH v2 2/5] spi: spi-fsl-dspi: Use non-coherent memory for DMA Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250613-james-nxp-spi-dma-v2-2-017eecf24aab@linaro.org> References: <20250613-james-nxp-spi-dma-v2-0-017eecf24aab@linaro.org> In-Reply-To: <20250613-james-nxp-spi-dma-v2-0-017eecf24aab@linaro.org> To: Vladimir Oltean , Mark Brown Cc: Vladimir Oltean , Arnd Bergmann , Larisa Grigore , Frank Li , linux-spi@vger.kernel.org, imx@lists.linux.dev, linux-kernel@vger.kernel.org, James Clark X-Mailer: b4 0.14.0 Using coherent memory here isn't functionally necessary. Because the change to use non-coherent memory isn't overly complex and only a few synchronization points are required, we might as well do it while fixing up some other DMA issues. Suggested-by: Arnd Bergmann Signed-off-by: James Clark --- drivers/spi/spi-fsl-dspi.c | 55 +++++++++++++++++++++++++++++-------------= ---- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/drivers/spi/spi-fsl-dspi.c b/drivers/spi/spi-fsl-dspi.c index 744dfc561db2..f19404e10c92 100644 --- a/drivers/spi/spi-fsl-dspi.c +++ b/drivers/spi/spi-fsl-dspi.c @@ -379,6 +379,11 @@ static bool is_s32g_dspi(struct fsl_dspi *data) data->devtype_data =3D=3D &devtype_data[S32G_TARGET]; } =20 +static int dspi_dma_transfer_size(struct fsl_dspi *dspi) +{ + return dspi->words_in_flight * DMA_SLAVE_BUSWIDTH_4_BYTES; +} + static void dspi_native_host_to_dev(struct fsl_dspi *dspi, u32 *txdata) { switch (dspi->oper_word_size) { @@ -493,7 +498,10 @@ static void dspi_tx_dma_callback(void *arg) { struct fsl_dspi *dspi =3D arg; struct fsl_dspi_dma *dma =3D dspi->dma; + struct device *dev =3D &dspi->pdev->dev; =20 + dma_sync_single_for_cpu(dev, dma->tx_dma_phys, + dspi_dma_transfer_size(dspi), DMA_TO_DEVICE); complete(&dma->cmd_tx_complete); } =20 @@ -501,9 +509,13 @@ static void dspi_rx_dma_callback(void *arg) { struct fsl_dspi *dspi =3D arg; struct fsl_dspi_dma *dma =3D dspi->dma; + struct device *dev =3D &dspi->pdev->dev; int i; =20 if (dspi->rx) { + dma_sync_single_for_cpu(dev, dma->rx_dma_phys, + dspi_dma_transfer_size(dspi), + DMA_FROM_DEVICE); for (i =3D 0; i < dspi->words_in_flight; i++) dspi_push_rx(dspi, dspi->dma->rx_dma_buf[i]); } @@ -513,6 +525,7 @@ static void dspi_rx_dma_callback(void *arg) =20 static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi) { + size_t size =3D dspi_dma_transfer_size(dspi); struct device *dev =3D &dspi->pdev->dev; struct fsl_dspi_dma *dma =3D dspi->dma; int time_left; @@ -521,10 +534,9 @@ static int dspi_next_xfer_dma_submit(struct fsl_dspi *= dspi) for (i =3D 0; i < dspi->words_in_flight; i++) dspi->dma->tx_dma_buf[i] =3D dspi_pop_tx_pushr(dspi); =20 + dma_sync_single_for_device(dev, dma->tx_dma_phys, size, DMA_TO_DEVICE); dma->tx_desc =3D dmaengine_prep_slave_single(dma->chan_tx, - dma->tx_dma_phys, - dspi->words_in_flight * - DMA_SLAVE_BUSWIDTH_4_BYTES, + dma->tx_dma_phys, size, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); if (!dma->tx_desc) { @@ -539,10 +551,10 @@ static int dspi_next_xfer_dma_submit(struct fsl_dspi = *dspi) return -EINVAL; } =20 + dma_sync_single_for_device(dev, dma->rx_dma_phys, size, + DMA_FROM_DEVICE); dma->rx_desc =3D dmaengine_prep_slave_single(dma->chan_rx, - dma->rx_dma_phys, - dspi->words_in_flight * - DMA_SLAVE_BUSWIDTH_4_BYTES, + dma->rx_dma_phys, size, DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); if (!dma->rx_desc) { @@ -644,17 +656,17 @@ static int dspi_request_dma(struct fsl_dspi *dspi, ph= ys_addr_t phy_addr) goto err_tx_channel; } =20 - dma->tx_dma_buf =3D dma_alloc_coherent(dma->chan_tx->device->dev, - dma_bufsize, &dma->tx_dma_phys, - GFP_KERNEL); + dma->tx_dma_buf =3D dma_alloc_noncoherent(dma->chan_tx->device->dev, + dma_bufsize, &dma->tx_dma_phys, + DMA_TO_DEVICE, GFP_KERNEL); if (!dma->tx_dma_buf) { ret =3D -ENOMEM; goto err_tx_dma_buf; } =20 - dma->rx_dma_buf =3D dma_alloc_coherent(dma->chan_rx->device->dev, - dma_bufsize, &dma->rx_dma_phys, - GFP_KERNEL); + dma->rx_dma_buf =3D dma_alloc_noncoherent(dma->chan_rx->device->dev, + dma_bufsize, &dma->rx_dma_phys, + DMA_FROM_DEVICE, GFP_KERNEL); if (!dma->rx_dma_buf) { ret =3D -ENOMEM; goto err_rx_dma_buf; @@ -689,11 +701,12 @@ static int dspi_request_dma(struct fsl_dspi *dspi, ph= ys_addr_t phy_addr) return 0; =20 err_slave_config: - dma_free_coherent(dma->chan_rx->device->dev, - dma_bufsize, dma->rx_dma_buf, dma->rx_dma_phys); + dma_free_noncoherent(dma->chan_rx->device->dev, dma_bufsize, + dma->rx_dma_buf, dma->rx_dma_phys, + DMA_FROM_DEVICE); err_rx_dma_buf: - dma_free_coherent(dma->chan_tx->device->dev, - dma_bufsize, dma->tx_dma_buf, dma->tx_dma_phys); + dma_free_noncoherent(dma->chan_tx->device->dev, dma_bufsize, + dma->tx_dma_buf, dma->tx_dma_phys, DMA_TO_DEVICE); err_tx_dma_buf: dma_release_channel(dma->chan_tx); err_tx_channel: @@ -714,14 +727,16 @@ static void dspi_release_dma(struct fsl_dspi *dspi) return; =20 if (dma->chan_tx) { - dma_free_coherent(dma->chan_tx->device->dev, dma_bufsize, - dma->tx_dma_buf, dma->tx_dma_phys); + dma_free_noncoherent(dma->chan_tx->device->dev, dma_bufsize, + dma->tx_dma_buf, dma->tx_dma_phys, + DMA_TO_DEVICE); dma_release_channel(dma->chan_tx); } =20 if (dma->chan_rx) { - dma_free_coherent(dma->chan_rx->device->dev, dma_bufsize, - dma->rx_dma_buf, dma->rx_dma_phys); + dma_free_noncoherent(dma->chan_rx->device->dev, dma_bufsize, + dma->rx_dma_buf, dma->rx_dma_phys, + DMA_FROM_DEVICE); dma_release_channel(dma->chan_rx); } } --=20 2.34.1 From nobody Fri Oct 10 13:32:46 2025 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (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 99E27291C34 for ; Fri, 13 Jun 2025 09:29:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749806969; cv=none; b=KHisdLrTVauBrwxpK0nZ0tYmvWNUFYrmdtfYMJrHMLKB8Klfk1Q6FHLQSWacYCMN6l/Ri2u76lyU0g7q1AhSgHC/7r3JpH+LYgNjxZSPoZx/xaM/j3AgtsFVxIMH6e6OWiGs5/67petc1nJWzdeE2mCaeQLbVztkD62SiTlU+3w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749806969; c=relaxed/simple; bh=CVwoRk0GER0LYrtRwOuy/VUCpYDZks7f+rhuuxXoALQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VxePJt1tUyj85kxFc7NnMsFhwSsCvtIywepFtCF3wTL49MzW7CI49GmW5T1+ItTG4Kp3kKZGVSCQPvXQASPQRA8a7uhJkcvgBB4GpCVhHlHmHZ2tXvbkBAsULqsYylDnB+jdy5ucF4FCqv5x+MDu7hi2GqT+5kf0EBtv6+F0vOY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=hD47HNMZ; arc=none smtp.client-ip=209.85.221.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="hD47HNMZ" Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-3a5123c1533so1229776f8f.2 for ; Fri, 13 Jun 2025 02:29:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1749806965; x=1750411765; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=6yB3v1heJMNnrfSWRvq2YsjasUe3fnLxNvvV0vI7w6w=; b=hD47HNMZMaNQ2f8OGo5ht2gCb1T3fnG76ZVYWxIYiS9BXZ4RpzTz4/U214W3EXQMQ4 FLE54/SH3hfbs+sckcucL8EUz//slSPPIZ23JaAg/kvBYpiz3xYnyjAb/2GrOOoiRQZZ 237febwrH0SGxPRLkBwwkzVLF4JL/VMFEOSZZhZxSibcTeSFzU4spwWHNM9ljQ2RKS3Y IDHBZUnLFKg0KKkTGnbaVcZ/h/mmFxSiS72zCMqbaeQYks/GgiFsGtIwuI+41OAlnH/g qUs2QsYSXuRDm9YAFLwTyzyqDwtADZB/23pvHQm3/N6Q2/KYTVkqJ3VLqVd+2IMuoWjE Wt1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749806965; x=1750411765; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6yB3v1heJMNnrfSWRvq2YsjasUe3fnLxNvvV0vI7w6w=; b=DJ6SMedLzSe2BRSyWSZyO26cEA1XoT8x42AqUtk/gq4dqgA1wYFC2z0XnUXMeKCTWm ddHf54LlxSzh3bFdoKabhwW4KYnnLhHD9UtGtvmkZXa4vkzxzzHf/ogvIJHhRW6MdX5A SeWdGnt7s2OJYCiCUHX/r/FVRGbLiNAsbOmNHYkBoBknsiyhZUSpbQ0AGIJPEqrv6Y36 pG2WhwNscFXVMqxcxL9u+EJl1s6ouTDwUzTV/yLaf48l/PKl5WSilPqRbfW2Hif0YYhr T3LUFKcXOLdDSCGoAHZhafQ7NcfnNuhTT0BZnZuEkJ5j98O12SQwScffZirNg1C89DLp ZLkw== X-Forwarded-Encrypted: i=1; AJvYcCXc7taAQ9rA9BE6qkZRxkddUi/vCIbBITqpDPmwjPh3QMXdFy3PDMO3fiplXO553NOEtOvIQizGPI7b1K8=@vger.kernel.org X-Gm-Message-State: AOJu0Yx4iMtYEBumWF+zRLUXcK7Jg5RmVhuA2JeJrINTc62KEe3woiTI HHddoyTVeGprVxkSyR4i3cVix45javcWCKVjKknIJB3v95dCXnO+fseUlbEKSjOn8l+zH60huRD z+qR1 X-Gm-Gg: ASbGnctPsGANWPf2KiFNLQlEfLnrOwUFuX0DREAjdICmulFLVD9YK6iUpjvFSAi85kN w5GI9lh8HXH1hNGCTk3jvHngcOeK5ajxLdU3g7hRXCwJFVzxKMXa3zYlE8doqKnRHrUQc4gWioq bW5WmupyemS0Ds4fZVTtM/YGqKEwKyzobSx4bKf2oH7KvDdvBfwGSIvO0xSyXnHRTFxqiqdp+Gz dFBD3bJWPP8F6mXEKdeIF4ZNEmGamo/dFxr1veNjNBA3lJLg0pbTT7UF0QGy7mnk3ufH68nOGO2 mo2hL3fF8H8AQxiIc9aPn2nWm/aesml0aOa1uY5Qr8rZAn/xwj5VvmWzacluyuETCm5hNAo= X-Google-Smtp-Source: AGHT+IHVoRas9OADbI1CexwPchRHvmKk79BDFD51FrhgWuPNDtOHfG14Xc4l+6ijC1aURbgK4Ow2vA== X-Received: by 2002:a05:6000:310d:b0:3a4:f6bc:d6f1 with SMTP id ffacd0b85a97d-3a56866f804mr2073879f8f.14.1749806964790; Fri, 13 Jun 2025 02:29:24 -0700 (PDT) Received: from ho-tower-lan.lan ([37.18.136.128]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a568b08a2bsm1805946f8f.62.2025.06.13.02.29.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jun 2025 02:29:24 -0700 (PDT) From: James Clark Date: Fri, 13 Jun 2025 10:28:58 +0100 Subject: [PATCH v2 3/5] spi: spi-fsl-dspi: Increase DMA buffer size Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250613-james-nxp-spi-dma-v2-3-017eecf24aab@linaro.org> References: <20250613-james-nxp-spi-dma-v2-0-017eecf24aab@linaro.org> In-Reply-To: <20250613-james-nxp-spi-dma-v2-0-017eecf24aab@linaro.org> To: Vladimir Oltean , Mark Brown Cc: Vladimir Oltean , Arnd Bergmann , Larisa Grigore , Frank Li , linux-spi@vger.kernel.org, imx@lists.linux.dev, linux-kernel@vger.kernel.org, James Clark X-Mailer: b4 0.14.0 From: Larisa Grigore When the device is configured as a target, the host won't stop sending data while we're draining the buffer which leads to FIFO underflows and corruption. Increase the DMA buffer size to the maximum words that edma can transfer once to reduce the chance of this happening. While we're here, also change the buffer size for host mode back to a page as it was before commit a957499bd437 ("spi: spi-fsl-dspi: Fix bits-per-word acceleration in DMA mode"). dma_alloc_noncoherent() allocations are backed by a full page anyway, so we might as well use it all. Signed-off-by: Larisa Grigore Signed-off-by: James Clark --- drivers/spi/spi-fsl-dspi.c | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/drivers/spi/spi-fsl-dspi.c b/drivers/spi/spi-fsl-dspi.c index f19404e10c92..48c2ebefcd4a 100644 --- a/drivers/spi/spi-fsl-dspi.c +++ b/drivers/spi/spi-fsl-dspi.c @@ -384,6 +384,39 @@ static int dspi_dma_transfer_size(struct fsl_dspi *dsp= i) return dspi->words_in_flight * DMA_SLAVE_BUSWIDTH_4_BYTES; } =20 +static int dspi_dma_bufsize(struct fsl_dspi *dspi) +{ + if (spi_controller_is_target(dspi->ctlr)) { + /* + * In target mode we have to be ready to receive the maximum + * that can possibly be transferred at once by EDMA without any + * FIFO underflows. This is CITER * SSIZE, where SSIZE is a max + * of 4 when transferring to a peripheral. + */ + return GENMASK(14, 0) * DMA_SLAVE_BUSWIDTH_4_BYTES; + } + + return PAGE_SIZE; +} + +static int dspi_dma_max_datawords(struct fsl_dspi *dspi) +{ + /* + * Transfers look like this so we always use a full DMA word regardless + * of SPI word size: + * + * 31 16 15 0 + * ----------------------------------------- + * | CONTROL WORD | 16-bit DATA | + * ----------------------------------------- + * or + * ----------------------------------------- + * | CONTROL WORD | UNUSED | 8-bit DATA | + * ----------------------------------------- + */ + return dspi_dma_bufsize(dspi) / DMA_SLAVE_BUSWIDTH_4_BYTES; +} + static void dspi_native_host_to_dev(struct fsl_dspi *dspi, u32 *txdata) { switch (dspi->oper_word_size) { @@ -606,6 +639,7 @@ static void dspi_setup_accel(struct fsl_dspi *dspi); static int dspi_dma_xfer(struct fsl_dspi *dspi) { struct spi_message *message =3D dspi->cur_msg; + int max_words =3D dspi_dma_max_datawords(dspi); struct device *dev =3D &dspi->pdev->dev; int ret =3D 0; =20 @@ -618,8 +652,8 @@ static int dspi_dma_xfer(struct fsl_dspi *dspi) dspi_setup_accel(dspi); =20 dspi->words_in_flight =3D dspi->len / dspi->oper_word_size; - if (dspi->words_in_flight > dspi->devtype_data->fifo_size) - dspi->words_in_flight =3D dspi->devtype_data->fifo_size; + if (dspi->words_in_flight > max_words) + dspi->words_in_flight =3D max_words; =20 message->actual_length +=3D dspi->words_in_flight * dspi->oper_word_size; @@ -636,7 +670,7 @@ static int dspi_dma_xfer(struct fsl_dspi *dspi) =20 static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr) { - int dma_bufsize =3D dspi->devtype_data->fifo_size * 2; + int dma_bufsize =3D dspi_dma_bufsize(dspi); struct device *dev =3D &dspi->pdev->dev; struct dma_slave_config cfg; struct fsl_dspi_dma *dma; @@ -720,7 +754,7 @@ static int dspi_request_dma(struct fsl_dspi *dspi, phys= _addr_t phy_addr) =20 static void dspi_release_dma(struct fsl_dspi *dspi) { - int dma_bufsize =3D dspi->devtype_data->fifo_size * 2; + int dma_bufsize =3D dspi_dma_bufsize(dspi); struct fsl_dspi_dma *dma =3D dspi->dma; =20 if (!dma) --=20 2.34.1 From nobody Fri Oct 10 13:32:46 2025 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.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 7842D2C158B for ; Fri, 13 Jun 2025 09:29:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749806969; cv=none; b=QIA5JrHfCfVgj/Lxxf0F9CpHWRHkCACqHzfIR35OwJWLDZzMMMaOgIW6eR8/Zd/D18gboGmaKVsH+j02majjlTKUlsjJ5ggjYpaU6/asQs6R5swsHBQoVZNoDvJs/J2NEufw4PPs0glr5ywr3h1FznLH5qHDe3m9FRvvIw3IUyA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749806969; c=relaxed/simple; bh=IpiugmKeISWFulxh2X5eQ8UZ9sRsTCudX6TcsNfAcSs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nb4KHe9fLfy257Vn3fG0ebwug3KbCyqW3GGQr5B3O+Fiz+ytmnYSZDJ/rrgjGMTbC7g+MELOMG+TKkaXiv5KD1ChVSMoFU0LU1XCy6jY7iLHKlHR4YsYB5HrlJaznBG3t6I/gKFK5qVJ8ZZXg24/CmVRa87qUkaj8IKfsHX3Jsg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=JU3mZq8L; arc=none smtp.client-ip=209.85.221.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="JU3mZq8L" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-3a4fea34e07so1150626f8f.1 for ; Fri, 13 Jun 2025 02:29:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1749806966; x=1750411766; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Fn5/JVKHTwCL3l+YfCzsIlgtnvvih6BSTZZfVhworAw=; b=JU3mZq8LAFl781Fsz93EhoT+SL+fKubYhBOgBE+ikmZV731+XuaBuCWGlOUfTYc2oF cx86O35Y5ufV45Y/m0aP94MHy5DHZB45zIJd15D8A4fciV4b/74FJ2FyEyAAVvPgj0xZ wcSVv8tUUh0BOpwW68ZhTqlvhUv8qFrXLCxL1JfR+Opxt7bHmg4964ZoZOm90NAGEw2y dovfC0xb+qxfwJit8B10n0bu1OU1nUwtesxd5eXbIonchZZ4HYae3dG9Jbk9qZfDeSkm dsSA2YWlenbWcSvMNo8q/JLeSL1U1KBzGJKTtsY2Mvpn+0r/GhYLQAMnTTzsVGq+9Qiq 26KQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749806966; x=1750411766; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Fn5/JVKHTwCL3l+YfCzsIlgtnvvih6BSTZZfVhworAw=; b=klCuxKXXGr34m6mAK8sCFkZmCYL/XHI+seBq13xCf/cV61pu10oAgdX3GeM8yccjob JLJ6n5Fdaj3d3KyJBZ4L/CSkCwZbH42NmhTzVF2XEiaZr/TPVR5Ath9FLP6stF72xvGq 6/uxFhGBw+SL9vcOExKWdm9SwnObSU8C0EbdhqdYFCtpxgT4O+xEAzFLzbbCHeD4+7kT vY7Gc+JHCsfHcwPihS8QJp99mDUPYxwq7pdi9WnSEV1lVFsAcnoI6TnPXVV4yX+17HHG QEfUnE7CspAamdMsA8uwBYWdnCrp5SxW7V0YdqOAgC1vaxIbyGsV43dMITTDtlGTuzv/ plEA== X-Forwarded-Encrypted: i=1; AJvYcCVjj6NSvYdVQg2QHY0RhZyHqQiUttlgW9bqh9AFUBKyou+poIRst0YqPH8RWsmn6bDTTMUHLWF3ATkUt5o=@vger.kernel.org X-Gm-Message-State: AOJu0YxvIwhd8QcikaCBoSFwTGAGTc/l9mfSPgFBYSorpazg68PghtmD qzWAbDjrDy8PoQw6nYLDbvb26Sqx3fj7Kvd47KkJ7aExN8CW8T4caiWAhAhlx9M6rHVwhDeCZlJ LyCfq X-Gm-Gg: ASbGncs1YQNxqFC0i7lSo2rMVMHrQL6Boq41Ark1K3KpBn2UHDd1cjOczL4aVLABjrd 95ewfh0fUGCaMO1kY74ZDFVWCJpAcX2CX9azeUX/kgYes0Y66ZcsNGhmYrhlZeYZfh9MEh5hXZd BTeiVb6Fs30TCq7714CyV6ui1LH0/EoJ3+3hSSkh3xcto3KKPRTsNjaZbIVr+MHHW/wzOLYBdfx b2BneKE5n0DdWi2c7WmujdpILgKME3o/VaIDozh8vqG5GyPHQGbzsPv49aVhIzycJrVpWtgn1cn OuSIBC1qaqXXsuewlkYJoDCPOq0K44BsjvCsw1N4ZsQNSAUPSAeiU1YoLX0b1hKPOk58s92Dw1M 2HvB/sw== X-Google-Smtp-Source: AGHT+IHTciO5qAtRfmmfRqhZoob18Wulxbnle++H7sYpmrlbCjBtBvPBK8JLtufPhbTAKQK+IlGULA== X-Received: by 2002:a05:6000:26d3:b0:3a4:e393:11e2 with SMTP id ffacd0b85a97d-3a56875776bmr2282133f8f.34.1749806965768; Fri, 13 Jun 2025 02:29:25 -0700 (PDT) Received: from ho-tower-lan.lan ([37.18.136.128]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a568b08a2bsm1805946f8f.62.2025.06.13.02.29.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jun 2025 02:29:25 -0700 (PDT) From: James Clark Date: Fri, 13 Jun 2025 10:28:59 +0100 Subject: [PATCH v2 4/5] spi: spi-fsl-dspi: Store status directly in cur_msg->status Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250613-james-nxp-spi-dma-v2-4-017eecf24aab@linaro.org> References: <20250613-james-nxp-spi-dma-v2-0-017eecf24aab@linaro.org> In-Reply-To: <20250613-james-nxp-spi-dma-v2-0-017eecf24aab@linaro.org> To: Vladimir Oltean , Mark Brown Cc: Vladimir Oltean , Arnd Bergmann , Larisa Grigore , Frank Li , linux-spi@vger.kernel.org, imx@lists.linux.dev, linux-kernel@vger.kernel.org, James Clark X-Mailer: b4 0.14.0 This will allow us to return a status from the interrupt handler in a later commit and avoids copying it at the end of dspi_transfer_one_message(). For consistency make polling and DMA modes use the same mechanism. Refactor dspi_rxtx() and dspi_poll() to not return -EINPROGRESS because this isn't actually a status that was ever returned to the core layer but some internal state. Wherever that was used we can look at dspi->len instead. No functional changes intended. Signed-off-by: James Clark --- drivers/spi/spi-fsl-dspi.c | 68 ++++++++++++++++++++++++------------------= ---- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/drivers/spi/spi-fsl-dspi.c b/drivers/spi/spi-fsl-dspi.c index 48c2ebefcd4a..31432d533dea 100644 --- a/drivers/spi/spi-fsl-dspi.c +++ b/drivers/spi/spi-fsl-dspi.c @@ -636,12 +636,11 @@ static int dspi_next_xfer_dma_submit(struct fsl_dspi = *dspi) =20 static void dspi_setup_accel(struct fsl_dspi *dspi); =20 -static int dspi_dma_xfer(struct fsl_dspi *dspi) +static void dspi_dma_xfer(struct fsl_dspi *dspi) { struct spi_message *message =3D dspi->cur_msg; int max_words =3D dspi_dma_max_datawords(dspi); struct device *dev =3D &dspi->pdev->dev; - int ret =3D 0; =20 /* * dspi->len gets decremented by dspi_pop_tx_pushr in @@ -658,14 +657,12 @@ static int dspi_dma_xfer(struct fsl_dspi *dspi) message->actual_length +=3D dspi->words_in_flight * dspi->oper_word_size; =20 - ret =3D dspi_next_xfer_dma_submit(dspi); - if (ret) { + message->status =3D dspi_next_xfer_dma_submit(dspi); + if (message->status) { dev_err(dev, "DMA transfer failed\n"); break; } } - - return ret; } =20 static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr) @@ -1035,36 +1032,40 @@ static void dspi_fifo_write(struct fsl_dspi *dspi) dspi->progress, !dspi->irq); } =20 -static int dspi_rxtx(struct fsl_dspi *dspi) +static void dspi_rxtx(struct fsl_dspi *dspi) { dspi_fifo_read(dspi); =20 if (!dspi->len) /* Success! */ - return 0; + return; =20 dspi_fifo_write(dspi); - - return -EINPROGRESS; } =20 -static int dspi_poll(struct fsl_dspi *dspi) +static void dspi_poll(struct fsl_dspi *dspi) { int tries =3D 1000; u32 spi_sr; =20 - do { - regmap_read(dspi->regmap, SPI_SR, &spi_sr); - regmap_write(dspi->regmap, SPI_SR, spi_sr); + while (dspi->len) { + do { + regmap_read(dspi->regmap, SPI_SR, &spi_sr); + regmap_write(dspi->regmap, SPI_SR, spi_sr); =20 - if (spi_sr & SPI_SR_CMDTCF) - break; - } while (--tries); + if (spi_sr & SPI_SR_CMDTCF) + break; + } while (--tries); =20 - if (!tries) - return -ETIMEDOUT; + if (!tries) { + dspi->cur_msg->status =3D -ETIMEDOUT; + return; + } =20 - return dspi_rxtx(dspi); + dspi_rxtx(dspi); + } + + dspi->cur_msg->status =3D 0; } =20 static irqreturn_t dspi_interrupt(int irq, void *dev_id) @@ -1078,8 +1079,13 @@ static irqreturn_t dspi_interrupt(int irq, void *dev= _id) if (!(spi_sr & SPI_SR_CMDTCF)) return IRQ_NONE; =20 - if (dspi_rxtx(dspi) =3D=3D 0) + dspi_rxtx(dspi); + + if (!dspi->len) { + if (dspi->cur_msg) + WRITE_ONCE(dspi->cur_msg->status, 0); complete(&dspi->xfer_done); + } =20 return IRQ_HANDLED; } @@ -1109,7 +1115,6 @@ static int dspi_transfer_one_message(struct spi_contr= oller *ctlr, struct spi_device *spi =3D message->spi; struct spi_transfer *transfer; bool cs =3D false; - int status =3D 0; u32 val =3D 0; bool cs_change =3D false; =20 @@ -1169,7 +1174,7 @@ static int dspi_transfer_one_message(struct spi_contr= oller *ctlr, dspi->progress, !dspi->irq); =20 if (dspi->devtype_data->trans_mode =3D=3D DSPI_DMA_MODE) { - status =3D dspi_dma_xfer(dspi); + dspi_dma_xfer(dspi); } else { /* * Reset completion counter to clear any extra @@ -1182,15 +1187,12 @@ static int dspi_transfer_one_message(struct spi_con= troller *ctlr, =20 dspi_fifo_write(dspi); =20 - if (dspi->irq) { + if (dspi->irq) wait_for_completion(&dspi->xfer_done); - } else { - do { - status =3D dspi_poll(dspi); - } while (status =3D=3D -EINPROGRESS); - } + else + dspi_poll(dspi); } - if (status) + if (READ_ONCE(message->status)) break; =20 spi_transfer_delay_exec(transfer); @@ -1199,7 +1201,8 @@ static int dspi_transfer_one_message(struct spi_contr= oller *ctlr, dspi_deassert_cs(spi, &cs); } =20 - if (status || !cs_change) { + dspi->cur_msg =3D NULL; + if (message->status || !cs_change) { /* Put DSPI in stop mode */ regmap_update_bits(dspi->regmap, SPI_MCR, SPI_MCR_HALT, SPI_MCR_HALT); @@ -1208,10 +1211,9 @@ static int dspi_transfer_one_message(struct spi_cont= roller *ctlr, ; } =20 - message->status =3D status; spi_finalize_current_message(ctlr); =20 - return status; + return message->status; } =20 static int dspi_set_mtf(struct fsl_dspi *dspi) --=20 2.34.1 From nobody Fri Oct 10 13:32:46 2025 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 559FC2C159F for ; Fri, 13 Jun 2025 09:29:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749806970; cv=none; b=azrT/OO8wv795qKoqN6VVAmjM0nVFPg/nPcPgwaRWRv+RWyt4kGN14BcuB9/EKZ7odtxJYqqAZLpCYh2dOTatjteXtwFCNVACcnp1cZSbquetwV6CMAK2bSyjXsCCo/9hBgqkoF0sr9ehm0Kl1M1UcSBO9ZO08z50yTCRkUs5o0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749806970; c=relaxed/simple; bh=olRfOaHDgzZ8i+4C3kFg3nNnJTByQvbKuBFGHGjLU/8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=G2CL8VCmnLMuWWFwmH00skxe3na7AxkyNXBnp4Vg4+oKD/hnpN62TGfxSjSSUOIFQcXre5CASFisScxf23HdT/D/QnQDWj/+En/Tvj0RbKdvn51Z1h2K2tq5HWEzJXus/Nu0rYavUv4Mews2JdUo+S49wIHxj+NKJua1ZLmUjpg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=kClpTe2R; arc=none smtp.client-ip=209.85.128.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="kClpTe2R" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-451d7b50815so15729265e9.2 for ; Fri, 13 Jun 2025 02:29:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1749806967; x=1750411767; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=VyHiR7U3e06QyZLuhEr1yr+WmILkiHQ+84NE+rNpfGI=; b=kClpTe2Rt8+YTYkvpNpK1xvPRimGIdDtdo+oZuTN9Iw+xKtDNxmjSwlFYTM2sOWPUc v5G5G91XehbCbDvd5FkFHvtHXLfzOtfpWZH12nP/qC8apgK7cd3xFc2BxadeLO1BMGm4 LxGGFcpRRxKYzA/KRdzJa9kwiJORiw48QKVBeUBlvNfLf739qZ81tmdodZ5wAESoucHK 6XapDMAvYCQ/0giQM/S/nJQ9cIIrOwpnZ1AFY4pcNxZuOLASBva96GfvqywEaN0qL2ow 5oWeGstQPLhC5dASsk5+YO32u48c7YDLYMSK1S1gnkQJ3KvVpXgSjAarA0zU5AdFd3DS xvVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749806967; x=1750411767; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VyHiR7U3e06QyZLuhEr1yr+WmILkiHQ+84NE+rNpfGI=; b=vlUza1myRsvGPM+V2Mo8shr2debiD/bqgg/Vbe6QvKY+xlDzWg01vsWq1x67whkZGY cx0K2FIQK3hBdZIWh4hgOB0QNPYYkhguu7QlkYJTpC4lpL1Ih9OANzDDXaAY3nP5Os2z mdgBG6uRh/2p2Ml9bgc6CupQeieBBnfqPF6NbjDMprjQSbz27dmgKOeLhuNnDXd36ey5 CKfULDcl2K43pZlJmKZJQGh5br+uSfrCjSQpJ7Lu4oRuuvKfV/n3JKz0UXjKfU24Nx8q 0sgm95WOUtFi6O+fOLdb52eBoBNFOXXpgrfFgiTbeQWmHGMg3DT065huAMuEFbsNPfj7 1lDA== X-Forwarded-Encrypted: i=1; AJvYcCVkpw10jWvf9UX3p3RasFmsp9bD4hMDvsiN5kx1aZXzjsJrkI8pydmAAogCGJ12r3741fPxwaX0lSRkjsE=@vger.kernel.org X-Gm-Message-State: AOJu0YwcfOGXgAJcr9cq++FHhN5opFWkOUXvYRNKQhdPfIeiI+06N+zp SLEYqIO8/Ou0OCtbU438oDJiFKImYaxpk+vrOM4yjemLYCKYXiBC9h3D8RuI5xahA711iGXD1NC lB7vJ X-Gm-Gg: ASbGncurJdTvGbaEoAM5273jovk1qeMZ9yCbicpDqL0jItVj1XEk7HVjlgGqX+liLmz 5R4l675iuFwjCMrWKCXcm6IdJtYSqaQ82vNQoc6/q7qmjX3IJ6Lw7ab6+NXg8bBfx8tl8RH6Zzk +e2mu3wXkxujKY/1a2WwXPai7W+CORzdV0Pma8kudae686KgJ4AZDuBkO4kAvcx3XFNXxtgW5u+ oBIpZqnsUgNGwL+Z/y1Tf44U+9jivgUmhN6paS4ufq/edu+tzlN+6lxLuvpXBEVfbNTOxwc7x73 CCoTfynF8Vi0YZt6Fb2vOikd/qMy2PVRG5C8KQTDUUm0Bh0+KkaoT3TYoOXOEn6rj5kQHaA= X-Google-Smtp-Source: AGHT+IFGL5ebfXnS6+SsaO2dpfkoeJy4mYNicoKfeWryjtieYLtsaUUUpvT+btcO05B9ZGq86PLAnw== X-Received: by 2002:a05:600c:8b08:b0:450:ceb2:67dd with SMTP id 5b1f17b1804b1-45334b80acemr20305265e9.33.1749806966724; Fri, 13 Jun 2025 02:29:26 -0700 (PDT) Received: from ho-tower-lan.lan ([37.18.136.128]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a568b08a2bsm1805946f8f.62.2025.06.13.02.29.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jun 2025 02:29:26 -0700 (PDT) From: James Clark Date: Fri, 13 Jun 2025 10:29:00 +0100 Subject: [PATCH v2 5/5] spi: spi-fsl-dspi: Report FIFO overflows as errors Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250613-james-nxp-spi-dma-v2-5-017eecf24aab@linaro.org> References: <20250613-james-nxp-spi-dma-v2-0-017eecf24aab@linaro.org> In-Reply-To: <20250613-james-nxp-spi-dma-v2-0-017eecf24aab@linaro.org> To: Vladimir Oltean , Mark Brown Cc: Vladimir Oltean , Arnd Bergmann , Larisa Grigore , Frank Li , linux-spi@vger.kernel.org, imx@lists.linux.dev, linux-kernel@vger.kernel.org, James Clark X-Mailer: b4 0.14.0 In target mode, the host sending more data than can be consumed would be a common problem for any message exceeding the FIFO or DMA buffer size. Cancel the whole message as soon as this condition is hit as the message will be corrupted. Only do this for target mode in a DMA transfer because we need to add a register read. In IRQ and polling modes always do it because SPI_SR was already read and it might catch some host mode programming/buffer management errors too. Signed-off-by: James Clark --- drivers/spi/spi-fsl-dspi.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/drivers/spi/spi-fsl-dspi.c b/drivers/spi/spi-fsl-dspi.c index 31432d533dea..f62f99f272b2 100644 --- a/drivers/spi/spi-fsl-dspi.c +++ b/drivers/spi/spi-fsl-dspi.c @@ -556,12 +556,24 @@ static void dspi_rx_dma_callback(void *arg) complete(&dma->cmd_rx_complete); } =20 +static int dspi_fifo_error(struct fsl_dspi *dspi, u32 spi_sr) +{ + if (spi_sr & (SPI_SR_TFUF | SPI_SR_RFOF)) { + dev_err_ratelimited(&dspi->pdev->dev, "FIFO errors:%s%s\n", + spi_sr & SPI_SR_TFUF ? " TX underflow," : "", + spi_sr & SPI_SR_RFOF ? " RX overflow," : ""); + return -EIO; + } + return 0; +} + static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi) { size_t size =3D dspi_dma_transfer_size(dspi); struct device *dev =3D &dspi->pdev->dev; struct fsl_dspi_dma *dma =3D dspi->dma; int time_left; + u32 spi_sr; int i; =20 for (i =3D 0; i < dspi->words_in_flight; i++) @@ -610,7 +622,8 @@ static int dspi_next_xfer_dma_submit(struct fsl_dspi *d= spi) =20 if (spi_controller_is_target(dspi->ctlr)) { wait_for_completion_interruptible(&dspi->dma->cmd_rx_complete); - return 0; + regmap_read(dspi->regmap, SPI_SR, &spi_sr); + return dspi_fifo_error(dspi, spi_sr); } =20 time_left =3D wait_for_completion_timeout(&dspi->dma->cmd_tx_complete, @@ -1055,6 +1068,10 @@ static void dspi_poll(struct fsl_dspi *dspi) =20 if (spi_sr & SPI_SR_CMDTCF) break; + + dspi->cur_msg->status =3D dspi_fifo_error(dspi, spi_sr); + if (dspi->cur_msg->status) + return; } while (--tries); =20 if (!tries) { @@ -1071,6 +1088,7 @@ static void dspi_poll(struct fsl_dspi *dspi) static irqreturn_t dspi_interrupt(int irq, void *dev_id) { struct fsl_dspi *dspi =3D (struct fsl_dspi *)dev_id; + int status; u32 spi_sr; =20 regmap_read(dspi->regmap, SPI_SR, &spi_sr); @@ -1079,6 +1097,14 @@ static irqreturn_t dspi_interrupt(int irq, void *dev= _id) if (!(spi_sr & SPI_SR_CMDTCF)) return IRQ_NONE; =20 + status =3D dspi_fifo_error(dspi, spi_sr); + if (status) { + if (dspi->cur_msg) + WRITE_ONCE(dspi->cur_msg->status, status); + complete(&dspi->xfer_done); + return IRQ_HANDLED; + } + dspi_rxtx(dspi); =20 if (!dspi->len) { --=20 2.34.1