Unverified Commit 36db0b03 authored by James Clark's avatar James Clark Committed by Mark Brown
Browse files

spi: spi-fsl-dspi: Use non-coherent memory for DMA



Using coherent memory here isn't functionally necessary, we're only
either sending data to the device or reading from it. This means
explicit synchronizations are only required around those points and the
change is fairly trivial.

This gives us around a 10% increase in throughput for large DMA
transfers and no loss for small transfers.

Suggested-by: default avatarArnd Bergmann <arnd@arndb.de>
Reviewed-by: default avatarFrank Li <Frank.Li@nxp.com>
Acked-by: default avatarArnd Bergmann <arnd@arndb.de>
Signed-off-by: default avatarJames Clark <james.clark@linaro.org>
Reviewed-by: default avatarVladimir Oltean <vladimir.oltean@nxp.com>
Message-ID: <20250902-james-nxp-spi-dma-v6-4-f7aa2c5e56e2@linaro.org>
Signed-off-by: default avatarMark Brown <broonie@kernel.org>
parent 4850f158
Loading
Loading
Loading
Loading
+41 −24
Original line number Diff line number Diff line
@@ -493,11 +493,19 @@ static u32 dspi_pop_tx_pushr(struct fsl_dspi *dspi)
	return cmd << 16 | data;
}

static size_t dspi_dma_transfer_size(struct fsl_dspi *dspi)
{
	return dspi->words_in_flight * DMA_SLAVE_BUSWIDTH_4_BYTES;
}

static void dspi_tx_dma_callback(void *arg)
{
	struct fsl_dspi *dspi = arg;
	struct fsl_dspi_dma *dma = dspi->dma;
	struct device *dev = &dspi->pdev->dev;

	dma_sync_single_for_cpu(dev, dma->tx_dma_phys,
				dspi_dma_transfer_size(dspi), DMA_TO_DEVICE);
	complete(&dma->cmd_tx_complete);
}

@@ -505,9 +513,13 @@ static void dspi_rx_dma_callback(void *arg)
{
	struct fsl_dspi *dspi = arg;
	struct fsl_dspi_dma *dma = dspi->dma;
	struct device *dev = &dspi->pdev->dev;
	int i;

	if (dspi->rx) {
		dma_sync_single_for_cpu(dev, dma->rx_dma_phys,
					dspi_dma_transfer_size(dspi),
					DMA_FROM_DEVICE);
		for (i = 0; i < dspi->words_in_flight; i++)
			dspi_push_rx(dspi, dspi->dma->rx_dma_buf[i]);
	}
@@ -517,6 +529,7 @@ static void dspi_rx_dma_callback(void *arg)

static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
{
	size_t size = dspi_dma_transfer_size(dspi);
	struct device *dev = &dspi->pdev->dev;
	struct fsl_dspi_dma *dma = dspi->dma;
	int time_left;
@@ -525,12 +538,12 @@ static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
	for (i = 0; i < dspi->words_in_flight; i++)
		dspi->dma->tx_dma_buf[i] = dspi_pop_tx_pushr(dspi);

	dma_sync_single_for_device(dev, dma->tx_dma_phys, size, DMA_TO_DEVICE);
	dma->tx_desc = 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);
						   DMA_PREP_INTERRUPT |
						   DMA_CTRL_ACK);
	if (!dma->tx_desc) {
		dev_err(dev, "Not able to get desc for DMA xfer\n");
		return -EIO;
@@ -543,12 +556,13 @@ static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
		return -EINVAL;
	}

	dma_sync_single_for_device(dev, dma->rx_dma_phys, size,
				   DMA_FROM_DEVICE);
	dma->rx_desc = 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);
						   DMA_PREP_INTERRUPT |
						   DMA_CTRL_ACK);
	if (!dma->rx_desc) {
		dev_err(dev, "Not able to get desc for DMA xfer\n");
		return -EIO;
@@ -643,17 +657,17 @@ static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr)
		goto err_tx_channel;
	}

	dma->tx_dma_buf = dma_alloc_coherent(dma->chan_tx->device->dev,
	dma->tx_dma_buf = dma_alloc_noncoherent(dma->chan_tx->device->dev,
						dma_bufsize, &dma->tx_dma_phys,
					     GFP_KERNEL);
						DMA_TO_DEVICE, GFP_KERNEL);
	if (!dma->tx_dma_buf) {
		ret = -ENOMEM;
		goto err_tx_dma_buf;
	}

	dma->rx_dma_buf = dma_alloc_coherent(dma->chan_rx->device->dev,
	dma->rx_dma_buf = dma_alloc_noncoherent(dma->chan_rx->device->dev,
						dma_bufsize, &dma->rx_dma_phys,
					     GFP_KERNEL);
						DMA_FROM_DEVICE, GFP_KERNEL);
	if (!dma->rx_dma_buf) {
		ret = -ENOMEM;
		goto err_rx_dma_buf;
@@ -688,11 +702,12 @@ static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr)
	return 0;

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:
@@ -713,14 +728,16 @@ static void dspi_release_dma(struct fsl_dspi *dspi)
		return;

	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);
	}

	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);
	}
}