Skip to contentSkip to conversation

dmaengine: dw-edma: fix endianess confusion

PATCH In review Arnd Bergmann 3 participants 6 messages Jun 17, 2019 – Jun 21, 2019

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

PATCH 1/1 dmaengine: dw-edma: fix endianess confusion

When building with 'make C=1', sparse reports an endianess bug:

drivers/dma/dw-edma/dw-edma-v0-debugfs.c:60:30: warning: cast removes address space of expression drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: warning: incorrect type in argument 1 (different address spaces)
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: expected void const volatile [noderef] <asn:2>*addr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: got void *[assigned] ptr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: warning: incorrect type in argument 1 (different address spaces)
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: expected void const volatile [noderef] <asn:2>*addr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: got void *[assigned] ptr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: warning: incorrect type in argument 1 (different address spaces)
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: expected void const volatile [noderef] <asn:2>*addr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: got void *[assigned] ptr

The current code is clearly wrong, as it passes an endian-swapped word into a register function where it gets swapped again. I assume that this was simply ported from a non-Linux OS, and the swap was done incorrectly. Replace it with a cast to uintptr_t.

Fixes 7e4b8a4fbe2c ("dmaengine: Add Synopsys eDMA IP version 0 support")
---
 drivers/dma/dw-edma/dw-edma-v0-core.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/dma/dw-edma/dw-edma-v0-core.c +5 -5
index 97e3fd41c8a8..d670ebcc37b3 100644
@@ -195,7 +195,7 @@ static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk)
 	struct dw_edma_v0_lli __iomem *lli;
 	struct dw_edma_v0_llp __iomem *llp;
 	u32 control = 0, i = 0;
-	u64 sar, dar, addr;
+	uintptr_t sar, dar, addr;
 	int j;
 
 	lli = chunk->ll_region.vaddr;
@@ -214,11 +214,11 @@ static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk)
 		/* Transfer size */
 		SET_LL(&lli[i].transfer_size, child->sz);
 		/* SAR - low, high */
-		sar = cpu_to_le64(child->sar);
+		sar = (uintptr_t)child->sar;
 		SET_LL(&lli[i].sar_low, lower_32_bits(sar));
 		SET_LL(&lli[i].sar_high, upper_32_bits(sar));
 		/* DAR - low, high */
-		dar = cpu_to_le64(child->dar);
+		dar = (uintptr_t)child->dar;
 		SET_LL(&lli[i].dar_low, lower_32_bits(dar));
 		SET_LL(&lli[i].dar_high, upper_32_bits(dar));
 		i++;
@@ -232,7 +232,7 @@ static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk)
 	/* Channel control */
 	SET_LL(&llp->control, control);
 	/* Linked list  - low, high */
-	addr = cpu_to_le64(chunk->ll_region.paddr);
+	addr = (uintptr_t)chunk->ll_region.paddr;
 	SET_LL(&llp->llp_low, lower_32_bits(addr));
 	SET_LL(&llp->llp_high, upper_32_bits(addr));
 }
@@ -262,7 +262,7 @@ void dw_edma_v0_core_start(struct dw_edma_chunk *chunk, bool first)
 		SET_CH(dw, chan->dir, chan->id, ch_control1,
 		       (DW_EDMA_V0_CCS | DW_EDMA_V0_LLE));
 		/* Linked list - low, high */
-		llp = cpu_to_le64(chunk->ll_region.paddr);
+		llp = (uintptr_t)chunk->ll_region.paddr;
 		SET_CH(dw, chan->dir, chan->id, llp_low, lower_32_bits(llp));
 		SET_CH(dw, chan->dir, chan->id, llp_high, upper_32_bits(llp));
 	}
Signature · 1 line
2.20.0
#1 lore raw

Hi,

Quoted 1 line · Arnd Bergmann

When building with 'make C=1', sparse reports an endianess bug:

I didn't know that option.

Quoted 12 lines

drivers/dma/dw-edma/dw-edma-v0-debugfs.c:60:30: warning: cast removes address space of expression drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: warning: incorrect type in argument 1 (different address spaces)
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: expected void const volatile [noderef] <asn:2>*addr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: got void *[assigned] ptr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: warning: incorrect type in argument 1 (different address spaces)
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: expected void const volatile [noderef] <asn:2>*addr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: got void *[assigned] ptr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: warning: incorrect type in argument 1 (different address spaces)
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: expected void const volatile [noderef] <asn:2>*addr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: got void *[assigned] ptr

Quoted 2 lines

The current code is clearly wrong, as it passes an endian-swapped word into a register function where it gets swapped again. I assume that this

Sorry I didn't catch this, endianness-swapped word into a register function where it gets swapped again?
Where?

Quoted 14 lines

was simply ported from a non-Linux OS, and the swap was done incorrectly. Replace it with a cast to uintptr_t.

Fixes 7e4b8a4fbe2c ("dmaengine: Add Synopsys eDMA IP version 0 support")
---
 drivers/dma/dw-edma/dw-edma-v0-core.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/dma/dw-edma/dw-edma-v0-core.c +0 -0
index 97e3fd41c8a8..d670ebcc37b3 100644

drivers/dma/dw-edma/dw-edma-v0-core.c static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk) · line 198

Quoted 5 lines
drivers/dma/dw-edma/dw-edma-v0-core.c +1 -1
@@ -195,7 +195,7 @@ static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk)
 	struct dw_edma_v0_lli __iomem *lli;
 	struct dw_edma_v0_llp __iomem *llp;
 	u32 control = 0, i = 0;
-	u64 sar, dar, addr;
+	uintptr_t sar, dar, addr;

Will this type assure variables sar, dar and addr are 64 bits?

drivers/dma/dw-edma/dw-edma-v0-core.c static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk) · line 217

Quoted 9 lines
drivers/dma/dw-edma/dw-edma-v0-core.c +1 -1
 	int j;
 
 	lli = chunk->ll_region.vaddr;
@@ -214,11 +214,11 @@ static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk)
 		/* Transfer size */
 		SET_LL(&lli[i].transfer_size, child->sz);
 		/* SAR - low, high */
-		sar = cpu_to_le64(child->sar);
+		sar = (uintptr_t)child->sar;

Assuming the host is a big-endian machine and the eDMA on the endpoint strictly requires the address to be little endian.
By not using cpu_to_le64(), the address to be written on the eDMA will be in big-endian format, right? If so, that will break the driver.

drivers/dma/dw-edma/dw-edma-v0-core.c static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk) · line 221

Quoted 5 lines
drivers/dma/dw-edma/dw-edma-v0-core.c +1 -1
 		SET_LL(&lli[i].sar_low, lower_32_bits(sar));
 		SET_LL(&lli[i].sar_high, upper_32_bits(sar));
 		/* DAR - low, high */
-		dar = cpu_to_le64(child->dar);
+		dar = (uintptr_t)child->dar;

Ditto.

drivers/dma/dw-edma/dw-edma-v0-core.c static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk) · line 235

Quoted 9 lines
drivers/dma/dw-edma/dw-edma-v0-core.c +1 -1
 		SET_LL(&lli[i].dar_low, lower_32_bits(dar));
 		SET_LL(&lli[i].dar_high, upper_32_bits(dar));
 		i++;
@@ -232,7 +232,7 @@ static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk)
 	/* Channel control */
 	SET_LL(&llp->control, control);
 	/* Linked list  - low, high */
-	addr = cpu_to_le64(chunk->ll_region.paddr);
+	addr = (uintptr_t)chunk->ll_region.paddr;

Ditto.

drivers/dma/dw-edma/dw-edma-v0-core.c void dw_edma_v0_core_start(struct dw_edma_chunk *chunk, bool first) · line 265

Quoted 9 lines
drivers/dma/dw-edma/dw-edma-v0-core.c +1 -1
 	SET_LL(&llp->llp_low, lower_32_bits(addr));
 	SET_LL(&llp->llp_high, upper_32_bits(addr));
 }
@@ -262,7 +262,7 @@ void dw_edma_v0_core_start(struct dw_edma_chunk *chunk, bool first)
 		SET_CH(dw, chan->dir, chan->id, ch_control1,
 		       (DW_EDMA_V0_CCS | DW_EDMA_V0_LLE));
 		/* Linked list - low, high */
-		llp = cpu_to_le64(chunk->ll_region.paddr);
+		llp = (uintptr_t)chunk->ll_region.paddr;

Ditto.

Quoted 5 lines
drivers/dma/dw-edma/dw-edma-v0-core.c +0 -0
 		SET_CH(dw, chan->dir, chan->id, llp_low, lower_32_bits(llp));
 		SET_CH(dw, chan->dir, chan->id, llp_high, upper_32_bits(llp));
 	}
Signature · 1 line
2.20.0
#2 lore raw
Quoted 8 lines · Gustavo Pimentel

Hi,

Quoted 1 line · Arnd Bergmann

When building with 'make C=1', sparse reports an endianess bug:

I didn't know that option.

And CF="-D__CHECK_ENDIAN__" is useful.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Gustavo Pimentel

Hi,

Quoted 7 lines
Quoted 5 lines · Arnd Bergmann
Quoted 2 lines

When building with 'make C=1', sparse reports an endianess bug:

I didn't know that option.

And CF="-D__CHECK_ENDIAN__" is useful.

Cool, I will add it to my personal notes.
Thanks.

Quoted 5 lines
Signature · 3 lines
-- 
With Best Regards,
Andy Shevchenko
#4 lore raw
In reply to Gustavo Pimentel
Quoted 18 lines · Gustavo Pimentel
Quoted 1 line · Arnd Bergmann

When building with 'make C=1', sparse reports an endianess bug:

I didn't know that option.

Quoted 12 lines

drivers/dma/dw-edma/dw-edma-v0-debugfs.c:60:30: warning: cast removes address space of expression drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: warning: incorrect type in argument 1 (different address spaces)
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: expected void const volatile [noderef] <asn:2>*addr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: got void *[assigned] ptr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: warning: incorrect type in argument 1 (different address spaces)
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: expected void const volatile [noderef] <asn:2>*addr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: got void *[assigned] ptr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: warning: incorrect type in argument 1 (different address spaces)
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: expected void const volatile [noderef] <asn:2>*addr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: got void *[assigned] ptr

Quoted 6 lines
Quoted 2 lines

The current code is clearly wrong, as it passes an endian-swapped word into a register function where it gets swapped again. I assume that this

Sorry I didn't catch this, endianness-swapped word into a register function where it gets swapped again?
Where?

See dw_edma_v0_core_write_chunk()

                sar = cpu_to_le64(child->sar);
                SET_LL(&lli[i].sar_low, lower_32_bits(sar));
                SET_LL(&lli[i].sar_high, upper_32_bits(sar));

SET_LL() expands to writel(), which does a cpu_to_le32() swap. (the swap gets left out on architectures that are little-endian only)

Quoted 4 lines
Quoted 4 lines
drivers/dma/dw-edma/dw-edma-v0-core.c +0 -0
index 97e3fd41c8a8..d670ebcc37b3 100644
Quoted 8 lines

drivers/dma/dw-edma/dw-edma-v0-core.c static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk) · line 198

Quoted 6 lines
drivers/dma/dw-edma/dw-edma-v0-core.c +1 -1
@@ -195,7 +195,7 @@ static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk)
      struct dw_edma_v0_lli __iomem *lli;
      struct dw_edma_v0_llp __iomem *llp;
      u32 control = 0, i = 0;
-     u64 sar, dar, addr;
+     uintptr_t sar, dar, addr;

Will this type assure variables sar, dar and addr are 64 bits?

No, just as long as a pointer. I somehow misread these as referring to a kernel pointer, but got that part wrong. The local variables can just be dropped then, just use lower_32_bits(child->sar)) etc.

Quoted 14 lines

drivers/dma/dw-edma/dw-edma-v0-core.c static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk) · line 217

Quoted 9 lines
drivers/dma/dw-edma/dw-edma-v0-core.c +1 -1
      int j;
 
      lli = chunk->ll_region.vaddr;
@@ -214,11 +214,11 @@ static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk)
              /* Transfer size */
              SET_LL(&lli[i].transfer_size, child->sz);
              /* SAR - low, high */
-             sar = cpu_to_le64(child->sar);
+             sar = (uintptr_t)child->sar;

Assuming the host is a big-endian machine and the eDMA on the endpoint strictly requires the address to be little endian.
By not using cpu_to_le64(), the address to be written on the eDMA will be in big-endian format, right? If so, that will break the driver.

No, because of the double-swap you are writing a big-endian address here, which is the bug I am referring to.

Arnd

#5 lore raw
Quoted 27 lines · Arnd Bergmann
Quoted 18 lines · Gustavo Pimentel
Quoted 1 line · Arnd Bergmann

When building with 'make C=1', sparse reports an endianess bug:

I didn't know that option.

Quoted 12 lines

drivers/dma/dw-edma/dw-edma-v0-debugfs.c:60:30: warning: cast removes address space of expression drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: warning: incorrect type in argument 1 (different address spaces)
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: expected void const volatile [noderef] <asn:2>*addr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: got void *[assigned] ptr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: warning: incorrect type in argument 1 (different address spaces)
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: expected void const volatile [noderef] <asn:2>*addr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: got void *[assigned] ptr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: warning: incorrect type in argument 1 (different address spaces)
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: expected void const volatile [noderef] <asn:2>*addr
drivers/dma/dw-edma/dw-edma-v0-debugfs.c:86:24: got void *[assigned] ptr

Quoted 6 lines
Quoted 2 lines

The current code is clearly wrong, as it passes an endian-swapped word into a register function where it gets swapped again. I assume that this

Sorry I didn't catch this, endianness-swapped word into a register function where it gets swapped again?
Where?

Quoted 8 lines

See dw_edma_v0_core_write_chunk()

                sar = cpu_to_le64(child->sar);
                SET_LL(&lli[i].sar_low, lower_32_bits(sar));
                SET_LL(&lli[i].sar_high, upper_32_bits(sar));

SET_LL() expands to writel(), which does a cpu_to_le32() swap. (the swap gets left out on architectures that are little-endian only)

That's right! I didn't look inside of writel().

Quoted 34 lines
Quoted 13 lines

drivers/dma/dw-edma/dw-edma-v0-core.c static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk) · line 198

Quoted 11 lines
drivers/dma/dw-edma/dw-edma-v0-core.c +1 -1
index 97e3fd41c8a8..d670ebcc37b3 100644
@@ -195,7 +195,7 @@ static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk)
      struct dw_edma_v0_lli __iomem *lli;
      struct dw_edma_v0_llp __iomem *llp;
      u32 control = 0, i = 0;
-     u64 sar, dar, addr;
+     uintptr_t sar, dar, addr;

Will this type assure variables sar, dar and addr are 64 bits?

No, just as long as a pointer. I somehow misread these as referring to a kernel pointer, but got that part wrong. The local variables can just be dropped then, just use lower_32_bits(child->sar)) etc.

Quoted 14 lines

drivers/dma/dw-edma/dw-edma-v0-core.c static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk) · line 217

Quoted 9 lines
drivers/dma/dw-edma/dw-edma-v0-core.c +1 -1
      int j;
 
      lli = chunk->ll_region.vaddr;
@@ -214,11 +214,11 @@ static void dw_edma_v0_core_write_chunk(struct dw_edma_chunk *chunk)
              /* Transfer size */
              SET_LL(&lli[i].transfer_size, child->sz);
              /* SAR - low, high */
-             sar = cpu_to_le64(child->sar);
+             sar = (uintptr_t)child->sar;

Assuming the host is a big-endian machine and the eDMA on the endpoint strictly requires the address to be little endian.
By not using cpu_to_le64(), the address to be written on the eDMA will be in big-endian format, right? If so, that will break the driver.

Quoted 2 lines

No, because of the double-swap you are writing a big-endian address here, which is the bug I am referring to.

After doing a quick exercise I realize that cpu_to_le64() shouldn't be there at all like you said. Since I only tested this driver on little-endian architecture system this issue wasn't visible at all.

Quoted 2 lines

Arnd