atmel_spi: Pass correct DMA address to controller
Subsystems (2)
PATCH 1/1 atmel_spi: Pass correct DMA address to controller
When either rx_buf or tx_buf is not being used, i.e. for plain read- or write operations, the atmel_spi uses a fixed-size DMA buffer instead. If the transfer is longer than the size of this buffer, it is split into multiple DMA transfers.
When the transfer is split like this, the atmel_spi driver ends up using the same DMA address again and again even for the buffer that came from the user, which is of course wrong. Fix this by adding the number of bytes already transferred to the DMA address so that the data ends up in the right place.
Thanks to Wu Xuan for discovering this bug.
--- drivers/spi/atmel_spi.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
drivers/spi/atmel_spi.c +4 -4
@@ -113,16 +113,16 @@ static void atmel_spi_next_xfer(struct spi_master *master, len = as->remaining_bytes; - tx_dma = xfer->tx_dma; - rx_dma = xfer->rx_dma; + tx_dma = xfer->tx_dma + xfer->len - len; + rx_dma = xfer->rx_dma + xfer->len - len;/* use scratch buffer only when rx or tx data is unspecified */ -if (rx_dma ==INVALID_DMA_ADDRESS ) { +if (!xfer->rx_buf) { rx_dma = as->buffer_dma;if (len >BUFFER_SIZE ) len =BUFFER_SIZE ; } -if (tx_dma ==INVALID_DMA_ADDRESS ) { +if (!xfer->tx_buf) { tx_dma = as->buffer_dma;if (len >BUFFER_SIZE ) len =BUFFER_SIZE ;
Signature · 1 line
1.4.4.4
Quoted 8 lines · Haavard Skinnemoen
When either
rx_bufortx_bufis not being used, i.e. for plain read- or write operations, theatmel_spiuses a fixed-size DMA buffer instead. If the transfer is longer than the size of this buffer, it is split into multiple DMA transfers.When the transfer is split like this, the
atmel_spidriver ends up using the same DMA address again and again even for the buffer that came from the user, which is of course wrong.
Yes, and needs fixing. This is almost right, see below.
Quoted 17 lines
Fix this by adding the
number of bytes already transferred to the DMA address so that the data ends up in the right place.Thanks to Wu Xuan for discovering this bug.
Signed-off-by Haavard Skinnemoen <[email protected]>--- drivers/spi/atmel_spi.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)drivers/spi/atmel_spi.c +0
-0@@ -113,16 +113,16 @@ static void atmel_spi_next_xfer(struct spi_master *master,
drivers/spi/atmel_spi.c static void atmel_spi_next_xfer(struct spi_master *master, · line 120
Quoted 10 lines
drivers/spi/atmel_spi.c +3
-3len = as->remaining_bytes; - tx_dma = xfer->tx_dma; - rx_dma = xfer->rx_dma; + tx_dma = xfer->tx_dma + xfer->len - len; + rx_dma = xfer->rx_dma + xfer->len - len;/* use scratch buffer only when rx or tx data is unspecified */ -if (rx_dma ==INVALID_DMA_ADDRESS ) { +if (!xfer->rx_buf) {
This test ...
drivers/spi/atmel_spi.c static void atmel_spi_next_xfer(struct spi_master *master, · line 125
Quoted 6 lines
drivers/spi/atmel_spi.c +1
-1rx_dma = as->buffer_dma;if (len >BUFFER_SIZE ) len =BUFFER_SIZE ; } -if (tx_dma ==INVALID_DMA_ADDRESS ) { +if (!xfer->tx_buf) {
... and this one must not assume that the relevant buffer is always NULL when the tx_dma has been set up. Keep the test against INVALID_DMA_ADDRESS, but fetch the address from the spi_transfer.
It's legit to set up cpu-virtual (for PIO) and dma addresses for each buffer, since the upper layer driver has no way to know if the underlying controller driver is DMA-capable, or for that matter PIO-capable.
Quoted 6 lines
drivers/spi/atmel_spi.c +0
-0tx_dma = as->buffer_dma;if (len >BUFFER_SIZE ) len =BUFFER_SIZE ;Signature · 1 line
1.4.4.4
Quoted 19 lines · David Brownell
drivers/spi/atmel_spi.c static void atmel_spi_next_xfer(struct spi_master *master, · line 120
Quoted 8 lines · Haavard Skinnemoen
drivers/spi/atmel_spi.c +3
-3- tx_dma = xfer->tx_dma; - rx_dma = xfer->rx_dma; + tx_dma = xfer->tx_dma + xfer->len - len; + rx_dma = xfer->rx_dma + xfer->len - len;/* use scratch buffer only when rx or tx data is unspecified */ -if (rx_dma ==INVALID_DMA_ADDRESS ) { +if (!xfer->rx_buf) {This test ...
drivers/spi/atmel_spi.c static void atmel_spi_next_xfer(struct spi_master *master, · line 125
Quoted 6 lines
rx_dma = as->buffer_dma; if (len > BUFFER_SIZE) len = BUFFER_SIZE; }drivers/spi/atmel_spi.c +1
-1-if (tx_dma ==INVALID_DMA_ADDRESS ) { +if (!xfer->tx_buf) {
Quoted 4 lines
... and this one must not assume that the relevant buffer is always NULL when the
tx_dmahas been set up. Keep the test againstINVALID_DMA_ADDRESS, but fetch the address from thespi_transfer.
No it doesn't -- it's the other way around. It assumes that if xfer->tx_buf is NULL, the user didn't provide a buffer so it has to use the scratch buffer instead. The driver never does PIO transfers, so if tx_buf is valid, tx_dma ought to be valid too. This is taken care of elsewhere by calling dma_map_single() if the user provided a virtual pointer but not a dma address.
Checking for an invalid cpu-virtual pointer really should be equivalent to checking for an invalid DMA address at this point.
I could perhaps add else blocks to those two ifs and add the offset to the respective dma addresses there instead if it makes it clearer. The tests are supposed to be the same as before, but I had to change them one way or another because of the added offset (which would make an invalid dma address non-invalid.)
Quoted 4 lines
It's legit to set up cpu-virtual (for PIO) and dma addresses for each buffer, since the upper layer driver has no way to know if the underlying controller driver is DMA-capable, or for that matter PIO-capable.
Yes, but are there any drivers that will provide a valid dma address and a NULL cpu-virtual pointer? That would indeed break my assumptions, but it would also break any PIO-only drivers, wouldn't it?
Haavard
I think I'll sign off on this as-is.
Quoted 9 lines · Håvard Skinnemoen
Quoted 4 lines · David Brownell
It's legit to set up cpu-virtual (for PIO) and dma addresses for each buffer, since the upper layer driver has no way to know if the underlying controller driver is DMA-capable, or for that matter PIO-capable.
Yes, but are there any drivers that will provide a valid dma address and a NULL cpu-virtual pointer?
Potentially. One scenario would be a block driver, which needs to work with scatterlists. dma_map_sg() is allowed to coalesce the scatterlist entries, as with an IOMMU. If it does that, there can no longer be a one-to-one linkage between addreses provided to that driver, and the dma addresess. (Likewise, addresses in HIGHMEM are not normally going to have kernel virtual addresses.) So providing both types of address is no longer practical with scatterlists.
Quoted 3 lines
That would indeed break my
assumptions, but it would also break any PIO-only drivers, wouldn't it?
Which is exactly why the current "mmc_spi" code doesn't use the dma_map_sg() interface. Instead, it goes in more byte-size chunks, taking care to provide both dma and pio addresses. It's a PITA, but at least it's coded now.
- Dave
Quoted 2 lines
Haavard