Skip to contentSkip to conversation

spi: atmel: keep DMA chunks to whole 16 bit words

PATCH In review Angelo Compagnucci 2 participants 4 messages Nov 13

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

PATCH v2 1/3 spi: atmel: keep DMA chunks to whole 16 bit words

The PDC path cuts a long transfer into chunks of max_dma_len bytes, that is SPI_MAX_DMA_XFER: 65535, an odd size. With more than 8 bits per word, a chunk takes 65535 bytes off the remaining length, but programs the PDC counters with 65535 / 2 = 32767 words, that is 65534 bytes. One byte is lost at each chunk, and the next chunk starts in the middle of a word.

The SPI core also cuts the DMA scatterlists into segments of max_dma_len bytes, so a segment can end in the middle of a word too.

The chunks were PAGE_SIZE, an even size, before SPI_MAX_DMA_XFER. Make it 65534: it is even, and still within the limits of the PDC and of the DMA controller.

Fixes 7910d9af000a ("spi: atmel: Use core SPI_MASTER_MUST_[RT]X handling")

---
Changes v1 -> v2:
- New patch: make SPI_MAX_DMA_XFER even, so that the transfers and DMA chunks cut at that size keep whole 16 bit words [Mark Brown]

 drivers/spi/spi-atmel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/spi-atmel.c +1 -1
index ffc896e4621a..c762e28407d8 100644
@@ -288,7 +288,7 @@ struct atmel_spi_device {
 	u32			csr;
 };
 
-#define SPI_MAX_DMA_XFER	65535 /* true for both PDC and DMA */
+#define SPI_MAX_DMA_XFER	65534 /* even, within the PDC and DMA limits */
 #define INVALID_DMA_ADDRESS	0xffffffff
 
 /*
Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 2/3 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 longer transfers are split. SPI_MAX_DMA_XFER is even, so the split transfers keep whole 16 bit words.

Fixes a9889ed62d06 ("spi: atmel: Implements transfers with bounce buffer")

---
Changes v1 -> v2:
- SPI_MAX_DMA_XFER is now even, see the previous patch [Mark Brown]
- Rebased on spi/for-next

 drivers/spi/spi-atmel.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/spi/spi-atmel.c +8 -0
index c762e28407d8..08075c9cc07b 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
#2 lore raw

PATCH v2 3/3 spi: atmel: allow changing bits_per_word between transfers

In reply to Angelo Compagnucci

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.

---
Changes v1 -> v2:
- Rebased on spi/for-next

 drivers/spi/spi-atmel.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/spi/spi-atmel.c +12 -3
index 08075c9cc07b..0d28c08a85c3 100644
@@ -1402,9 +1402,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
#3 lore raw
In reply to Angelo Compagnucci
Quoted 5 lines · Angelo Compagnucci

The PDC path cuts a long transfer into chunks of max_dma_len bytes, that is SPI_MAX_DMA_XFER: 65535, an odd size. With more than 8 bits per word, a chunk takes 65535 bytes off the remaining length, but programs the PDC counters with 65535 / 2 = 32767 words, that is 65534 bytes. One byte is lost at each chunk, and the next chunk starts in the middle of a word.

Please supply cover letters for multi-patch serieses, it makes it much easier to keep track of what's going on for both people and tooling.