Skip to contentSkip to conversation

spi: atmel: limit transfers to the bounce buffer size

PATCH In review Angelo Compagnucci 2 participants 3 messages Nov 11 – Nov 13

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 spi: atmel: limit transfers to the bounce buffer size

On SAM9 SoCs (CONFIG_SOC_SAM_V4_V5) a DMA transfer from or to a vmalloc buffer goes through bounce buffers of SPI_MAX_DMA_XFER bytes. The whole transfer is copied in them though, whatever its length, and nothing limits the length of a transfer: a longer one is copied past the end of the buffer.

The MIPI DBI panel drivers send a whole frame from the framebuffer, a vmalloc buffer, in one transfer: 153600 bytes for a 320x240 ILI9341 panel. On an Arietta G25 (SAM9G25), once the driver accepts their 16 bit transfers, the first frame oopses:

  Unable to handle kernel paging request at virtual address c8915000 when write
  PC is at mmiocpy+0x34/0x2a0
   mmiocpy from atmel_spi_one_transfer+0x4f0/0x77c

When the bounce buffers are in use, report SPI_MAX_DMA_XFER as the maximum transfer size, so that SPI device drivers split longer transfers.

Fixes a9889ed62d06 ("spi: atmel: Implements transfers with bounce buffer")
---
 drivers/spi/spi-atmel.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/spi/spi-atmel.c +8 -0
index ffc1292b0..fb8d4c7c9 100644
@@ -591,6 +591,12 @@ static void atmel_spi_release_dma(void *data)
 	}
 }
 
+/* Transfers from vmalloc buffers are copied in the bounce buffers */
+static size_t atmel_spi_max_transfer_size(struct spi_device *spi)
+{
+	return SPI_MAX_DMA_XFER;
+}
+
 static int atmel_spi_configure_dma(struct spi_controller *host,
 				   struct atmel_spi *as)
 {
@@ -637,6 +643,8 @@ static int atmel_spi_configure_dma(struct spi_controller *host,
 			err = -ENOMEM;
 			goto err_release_dma;
 		}
+
+		host->max_transfer_size = atmel_spi_max_transfer_size;
 	}
 
 	err = devm_add_action_or_reset(dev, atmel_spi_release_dma, host);
Signature · 1 line
2.53.0
#1 lore raw

PATCH 2/2 spi: atmel: allow changing bits_per_word between transfers

The controller supports 8 to 16 bits per word, as its bits_per_word_mask says, but the word size is written in the chip select register only by setup(): a transfer with another word size fails with -ENOPROTOOPT ("you can't yet change bits_per_word in transfers").

The MIPI DBI panel drivers send commands in 8 bit words and pixels in 16 bit words when the controller supports them, so no frame reaches an ILI9341 panel on an Arietta G25 (SAM9G25):

ili9341 spi0.0: SPI transfer failed: -92

The DMA and PIO paths already follow the word size of each transfer:
when it differs from the configured one, update the BITS field of the chip select register instead of failing.

---
 drivers/spi/spi-atmel.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/spi/spi-atmel.c +12 -3
index fb8d4c7c9..c02a364fa 100644
@@ -1401,9 +1401,18 @@ static int atmel_spi_one_transfer(struct spi_controller *host,
 	asd = spi->controller_state;
 	bits = (asd->csr >> 4) & 0xf;
 	if (bits != xfer->bits_per_word - 8) {
-		dev_dbg(&spi->dev,
-			"you can't yet change bits_per_word in transfers\n");
-		return -ENOPROTOOPT;
+		int chip_select;
+
+		if (spi_get_csgpiod(spi, 0))
+			chip_select = as->native_cs_for_gpio;
+		else
+			chip_select = spi_get_chipselect(spi, 0);
+
+		/* The chip select is active: only the word size changes */
+		asd->csr = SPI_BFINS(BITS, xfer->bits_per_word - 8, asd->csr);
+		spi_writel(as, CSR0 + 4 * chip_select, asd->csr);
+		if (atmel_spi_is_v2(as))
+			spi_writel(as, CSR0, asd->csr);
 	}
 
 	/*
Signature · 1 line
2.53.0
maintainer
#2 lore raw
In reply to Angelo Compagnucci

drivers/spi/spi-atmel.c static void atmel_spi_release_dma(void *data) · line 597

Quoted 10 lines

On SAM9 SoCs (CONFIG_SOC_SAM_V4_V5) a DMA transfer from or to a vmalloc buffer goes through bounce buffers of SPI_MAX_DMA_XFER bytes. The whole transfer is copied in them though, whatever its length, and nothing limits the length of a transfer: a longer one is copied past the end of the buffer.

drivers/spi/spi-atmel.c +4 -0
+/* Transfers from vmalloc buffers are copied in the bounce buffers */
+static size_t atmel_spi_max_transfer_size(struct spi_device *spi)
+{
+	return SPI_MAX_DMA_XFER;

This poorly namespaced driver specific constant is 65535 which is going to cause problems with 16 bit words, __spi_validate() will end up trying to split transfers into 65535 byte chunks which isn't going to go well. Picking an even number would avoid the issue.

Signature · 1 line
Mark Brown <[email protected]>