Skip to contentSkip to conversation

serial: samsung: use dma_ops of DMA if attached

PATCH Superseded Tamseel Shams 2 participants 3 messages Jun 26, 2021 – Jun 28, 2021 1 message not in the archive

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CGME20210625074751epcas5p125067e47c4ff1ad24a1e595d85f82540@epcas5p1.samsung.com

#1 lore raw

PATCH v4 1/1 serial: samsung: use dma_ops of DMA if attached

When DMA is used for TX and RX by serial driver, it should pass the DMA device pointer to DMA API instead of UART device pointer. DMA device should be used for DMA API because only the DMA device is aware of how the device connects to the memory. There might be an extra level of address translation due to a SMMU attached to the DMA device. When serial device is used for DMA API, the DMA API will have no clue of the SMMU attached to the DMA device.

This patch is necessary to fix the SMMU page faults which is observed when a DMA(with SMMU enabled) is attached to UART for transfer.

---
Changes since v1:
1. Rebased the patch on "tty-next" branch of TTY driver tree

Changes since v2:
1. Updated the commit message.
2. Changed the comment description

Changes since v3:
1. Removed the null pointer check for "dma", "dma->tx_chan" and "dma->rx_chan" and instead sending DMA device pointer while calling DMA API.

 drivers/tty/serial/samsung_tty.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)
drivers/tty/serial/samsung_tty.c +16 -16
index 9fbc61151c2e..fa30aa20a13f 100644
@@ -305,7 +305,7 @@ static void s3c24xx_serial_stop_tx(struct uart_port *port)
 		dmaengine_pause(dma->tx_chan);
 		dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state);
 		dmaengine_terminate_all(dma->tx_chan);
-		dma_sync_single_for_cpu(ourport->port.dev,
+		dma_sync_single_for_cpu(dma->tx_chan->device->dev,
 			dma->tx_transfer_addr, dma->tx_size, DMA_TO_DEVICE);
 		async_tx_ack(dma->tx_desc);
 		count = dma->tx_bytes_requested - state.residue;
@@ -338,8 +338,8 @@ static void s3c24xx_serial_tx_dma_complete(void *args)
 	count = dma->tx_bytes_requested - state.residue;
 	async_tx_ack(dma->tx_desc);
 
-	dma_sync_single_for_cpu(ourport->port.dev, dma->tx_transfer_addr,
-				dma->tx_size, DMA_TO_DEVICE);
+	dma_sync_single_for_cpu(dma->tx_chan->device->dev,
+			dma->tx_transfer_addr, dma->tx_size, DMA_TO_DEVICE);
 
 	spin_lock_irqsave(&port->lock, flags);
 
@@ -443,8 +443,8 @@ static int s3c24xx_serial_start_tx_dma(struct s3c24xx_uart_port *ourport,
 	dma->tx_size = count & ~(dma_get_cache_alignment() - 1);
 	dma->tx_transfer_addr = dma->tx_addr + xmit->tail;
 
-	dma_sync_single_for_device(ourport->port.dev, dma->tx_transfer_addr,
-				dma->tx_size, DMA_TO_DEVICE);
+	dma_sync_single_for_device(dma->tx_chan->device->dev,
+			dma->tx_transfer_addr, dma->tx_size, DMA_TO_DEVICE);
 
 	dma->tx_desc = dmaengine_prep_slave_single(dma->tx_chan,
 				dma->tx_transfer_addr, dma->tx_size,
@@ -515,7 +515,7 @@ static void s3c24xx_uart_copy_rx_to_tty(struct s3c24xx_uart_port *ourport,
 	if (!count)
 		return;
 
-	dma_sync_single_for_cpu(ourport->port.dev, dma->rx_addr,
+	dma_sync_single_for_cpu(dma->rx_chan->device->dev, dma->rx_addr,
 				dma->rx_size, DMA_FROM_DEVICE);
 
 	ourport->port.icount.rx += count;
@@ -636,7 +636,7 @@ static void s3c64xx_start_rx_dma(struct s3c24xx_uart_port *ourport)
 {
 	struct s3c24xx_uart_dma *dma = ourport->dma;
 
-	dma_sync_single_for_device(ourport->port.dev, dma->rx_addr,
+	dma_sync_single_for_device(dma->rx_chan->device->dev, dma->rx_addr,
 				dma->rx_size, DMA_FROM_DEVICE);
 
 	dma->rx_desc = dmaengine_prep_slave_single(dma->rx_chan,
@@ -1102,18 +1102,18 @@ static int s3c24xx_serial_request_dma(struct s3c24xx_uart_port *p)
 		goto err_release_tx;
 	}
 
-	dma->rx_addr = dma_map_single(p->port.dev, dma->rx_buf,
+	dma->rx_addr = dma_map_single(dma->rx_chan->device->dev, dma->rx_buf,
 				dma->rx_size, DMA_FROM_DEVICE);
-	if (dma_mapping_error(p->port.dev, dma->rx_addr)) {
+	if (dma_mapping_error(dma->rx_chan->device->dev, dma->rx_addr)) {
 		reason = "DMA mapping error for RX buffer";
 		ret = -EIO;
 		goto err_free_rx;
 	}
 
 	/* TX buffer */
-	dma->tx_addr = dma_map_single(p->port.dev, p->port.state->xmit.buf,
-				UART_XMIT_SIZE, DMA_TO_DEVICE);
-	if (dma_mapping_error(p->port.dev, dma->tx_addr)) {
+	dma->tx_addr = dma_map_single(dma->tx_chan->device->dev,
+			p->port.state->xmit.buf, UART_XMIT_SIZE, DMA_TO_DEVICE);
+	if (dma_mapping_error(dma->tx_chan->device->dev, dma->tx_addr)) {
 		reason = "DMA mapping error for TX buffer";
 		ret = -EIO;
 		goto err_unmap_rx;
@@ -1122,8 +1122,8 @@ static int s3c24xx_serial_request_dma(struct s3c24xx_uart_port *p)
 	return 0;
 
 err_unmap_rx:
-	dma_unmap_single(p->port.dev, dma->rx_addr, dma->rx_size,
-			 DMA_FROM_DEVICE);
+	dma_unmap_single(dma->rx_chan->device->dev, dma->rx_addr,
+			dma->rx_size, DMA_FROM_DEVICE);
 err_free_rx:
 	kfree(dma->rx_buf);
 err_release_tx:
@@ -1142,7 +1142,7 @@ static void s3c24xx_serial_release_dma(struct s3c24xx_uart_port *p)
 
 	if (dma->rx_chan) {
 		dmaengine_terminate_all(dma->rx_chan);
-		dma_unmap_single(p->port.dev, dma->rx_addr,
+		dma_unmap_single(dma->rx_chan->device->dev, dma->rx_addr,
 				dma->rx_size, DMA_FROM_DEVICE);
 		kfree(dma->rx_buf);
 		dma_release_channel(dma->rx_chan);
@@ -1151,7 +1151,7 @@ static void s3c24xx_serial_release_dma(struct s3c24xx_uart_port *p)
 
 	if (dma->tx_chan) {
 		dmaengine_terminate_all(dma->tx_chan);
-		dma_unmap_single(p->port.dev, dma->tx_addr,
+		dma_unmap_single(dma->tx_chan->device->dev, dma->tx_addr,
 				UART_XMIT_SIZE, DMA_TO_DEVICE);
 		dma_release_channel(dma->tx_chan);
 		dma->tx_chan = NULL;
Signature · 1 line
2.17.1
#2 lore raw
Quoted 45 lines · Tamseel Shams

When DMA is used for TX and RX by serial driver, it should pass the DMA device pointer to DMA API instead of UART device pointer. DMA device should be used for DMA API because only the DMA device is aware of how the device connects to the memory. There might be an extra level of address translation due to a SMMU attached to the DMA device. When serial device is used for DMA API, the DMA API will have no clue of the SMMU attached to the DMA device.

This patch is necessary to fix the SMMU page faults which is observed when a DMA(with SMMU enabled) is attached to UART for transfer.

---
Changes since v1:
1. Rebased the patch on "tty-next" branch of TTY driver tree

Changes since v2:
1. Updated the commit message.
2. Changed the comment description

Changes since v3:
1. Removed the null pointer check for "dma", "dma->tx_chan" and "dma->rx_chan" and instead sending DMA device pointer while calling DMA API.

 drivers/tty/serial/samsung_tty.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)
drivers/tty/serial/samsung_tty.c +1 -1
index 9fbc61151c2e..fa30aa20a13f 100644
@@ -305,7 +305,7 @@ static void s3c24xx_serial_stop_tx(struct uart_port *port)
 		dmaengine_pause(dma->tx_chan);
 		dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state);
 		dmaengine_terminate_all(dma->tx_chan);
-		dma_sync_single_for_cpu(ourport->port.dev,
+		dma_sync_single_for_cpu(dma->tx_chan->device->dev,
 			dma->tx_transfer_addr, dma->tx_size, DMA_TO_DEVICE);
 		async_tx_ack(dma->tx_desc);
 		count = dma->tx_bytes_requested - state.residue;

drivers/tty/serial/samsung_tty.c static void s3c24xx_serial_tx_dma_complete(void *args) · line 342

Quoted 7 lines
drivers/tty/serial/samsung_tty.c +2 -2
@@ -338,8 +338,8 @@ static void s3c24xx_serial_tx_dma_complete(void *args)
 	count = dma->tx_bytes_requested - state.residue;
 	async_tx_ack(dma->tx_desc);
 
-	dma_sync_single_for_cpu(ourport->port.dev, dma->tx_transfer_addr,
-				dma->tx_size, DMA_TO_DEVICE);
+	dma_sync_single_for_cpu(dma->tx_chan->device->dev,
+			dma->tx_transfer_addr, dma->tx_size, DMA_TO_DEVICE);

Looks like alignment is broken here. However even if the line was not aligned before, please fix it up now - align the arguments like checkpatch suggests.

This applies to other places as well. Thanks.

Best regards,
Krzysztof

#3 lore raw
Quoted 57 lines

drivers/tty/serial/samsung_tty.c static void s3c24xx_serial_stop_tx(struct uart_port

Quoted 36 lines

When DMA is used for TX and RX by serial driver, it should pass the DMA device pointer to DMA API instead of UART device pointer. DMA device should be used for DMA API because only the DMA device is aware of how the device connects to the memory.
There might be an extra level of address translation due to a SMMU attached to the DMA device. When serial device is used for DMA API, the DMA API will have no clue of the SMMU attached to the DMA device.

This patch is necessary to fix the SMMU page faults which is observed when a DMA(with SMMU enabled) is attached to UART for transfer.

---
Changes since v1:
1. Rebased the patch on "tty-next" branch of TTY driver tree

Changes since v2:
1. Updated the commit message.
2. Changed the comment description

Changes since v3:
1. Removed the null pointer check for "dma", "dma->tx_chan" and "dma->rx_chan" and instead sending DMA device pointer while calling DMA API.

 drivers/tty/serial/samsung_tty.c | 32
++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)
drivers/tty/serial/samsung_tty.c +0 -0
index 9fbc61151c2e..fa30aa20a13f 100644
@@ -305,7 +305,7 @@ static void s3c24xx_serial_stop_tx(struct uart_port

*port)

drivers/tty/serial/samsung_tty.c static void s3c24xx_serial_stop_tx(struct uart_port *port)

Quoted 6 lines
drivers/tty/serial/samsung_tty.c +1 -1
 		dmaengine_pause(dma->tx_chan);
 		dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state);
 		dmaengine_terminate_all(dma->tx_chan);
-		dma_sync_single_for_cpu(ourport->port.dev,
+		dma_sync_single_for_cpu(dma->tx_chan->device->dev,
 			dma->tx_transfer_addr, dma->tx_size,

DMA_TO_DEVICE);

drivers/tty/serial/samsung_tty.c static void s3c24xx_serial_stop_tx(struct uart_port · line 311

Quoted 2 lines
drivers/tty/serial/samsung_tty.c +0 -0
 		async_tx_ack(dma->tx_desc);
 		count = dma->tx_bytes_requested - state.residue; @@ -338,8

+338,8

drivers/tty/serial/samsung_tty.c static void s3c24xx_serial_tx_dma_complete(void *args)

Quoted 8 lines

@@ static void s3c24xx_serial_tx_dma_complete(void *args)

drivers/tty/serial/samsung_tty.c +2 -2
 	count = dma->tx_bytes_requested - state.residue;
 	async_tx_ack(dma->tx_desc);
 
-	dma_sync_single_for_cpu(ourport->port.dev, dma->tx_transfer_addr,
-				dma->tx_size, DMA_TO_DEVICE);
+	dma_sync_single_for_cpu(dma->tx_chan->device->dev,
+			dma->tx_transfer_addr, dma->tx_size,

DMA_TO_DEVICE);

Quoted 7 lines

Looks like alignment is broken here. However even if the line was not aligned before, please fix it up now - align the arguments like checkpatch suggests.

This applies to other places as well. Thanks.

Best regards,
Krzysztof

Hi Krzysztof,
Will fix the alignments in the next version.

Thanks & Regards,
Tamseel Shams