hw/dma: Align SiFive PDMA behavior with real hardware
cover hw/dma: Align SiFive PDMA behavior with real hardware
From: Frank Chang <[email protected]>
Current QEMU PDMA doesn't align with real PDMA's behavior. This would result in Linux dmatest failed. This patchest aligns with real PDMA's behavior we tested on the real board. The golden results are performed in U-boot on the Unmatched board with PDMA supported.
Frank Chang (3):
hw/dma: sifive_pdma: reset Next* registers when Control.claim is set hw/dma: sifive_pdma: claim bit must be set before DMA transactions hw/dma: sifive_pdma: don't set Control.error if 0 bytes to transfer
Green Wan (1):
hw/dma: sifive_pdma: allow non-multiple transaction size transactions
hw/dma/sifive_pdma.c | 50 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 11 deletions(-)
--
2.25.1
PATCH 1/4 hw/dma: sifive_pdma: reset Next* registers when Control.claim is set
From: Frank Chang <[email protected]>
Setting Control.claim clears all of the chanel's Next registers. This is effective only when Control.claim is set from 0 to 1.
--- hw/dma/sifive_pdma.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
hw/dma/sifive_pdma.c +15 -0
@@ -54,6 +54,9 @@#define DMA_EXEC_DST 0x110#define DMA_EXEC_SRC 0x118 +#define CONFIG_WRSZ_DEFAULT 6 +#define CONFIG_RDSZ_DEFAULT 6 +enum dma_chan_state {DMA_CHAN_STATE_IDLE ,DMA_CHAN_STATE_STARTED ,
@@ -221,6 +224,7 @@ static void sifive_pdma_write(void *opaque, hwaddr offset, {SiFivePDMAState *s = opaque;int ch =SIFIVE_PDMA_CHAN_NO (offset); +bool claimed;if (ch >=SIFIVE_PDMA_CHANS ) { qemu_log_mask(LOG_GUEST_ERROR, "%s: Invalid channel no %d\n",
@@ -231,6 +235,17 @@ static void sifive_pdma_write(void *opaque, hwaddr offset, offset &=0xfff ;switch (offset) {case DMA_CONTROL : + claimed = !!s->chan[ch].control &CONTROL_CLAIM ; + +if (!claimed && (value &CONTROL_CLAIM )) { +/* reset Next* registers */ + s->chan[ch].next_config = (CONFIG_RDSZ_DEFAULT <<CONFIG_RDSZ_SHIFT ) | + (CONFIG_WRSZ_DEFAULT <<CONFIG_WRSZ_SHIFT ); + s->chan[ch].next_bytes =0 ; + s->chan[ch].next_dst =0 ; + s->chan[ch].next_src =0 ; + } + s->chan[ch].control = value;if (value &CONTROL_RUN ) {
Signature · 1 line
2.25.1
Re: [PATCH 1/4] hw/dma: sifive_pdma: reset Next* registers when Control.claim is set
Quoted 17 lines · On Fri, Sep 10, 2021 at 1:56 PM <[email protected]> wrote:
From: Frank Chang <[email protected]>
Setting Control.claim clears all of the chanel's Next registers. This is effective only when Control.claim is set from 0 to 1.
Signed-off-by Frank Chang <[email protected]>Tested-by Max Hsu <[email protected]>--- hw/dma/sifive_pdma.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)hw/dma/sifive_pdma.c +0
-0
hw/dma/sifive_pdma.c line 58
Quoted 5 lines
hw/dma/sifive_pdma.c +2
-0@@ -54,6 +54,9 @@#define DMA_EXEC_DST 0x110#define DMA_EXEC_SRC 0x118 +#define CONFIG_WRSZ_DEFAULT 6 +#define CONFIG_RDSZ_DEFAULT 6
The FU540 manual says the next config reset value is 0, not 6.
Quoted 20 lines
hw/dma/sifive_pdma.c +6
-0+enum dma_chan_state {DMA_CHAN_STATE_IDLE ,DMA_CHAN_STATE_STARTED ,@@ -221,6 +224,7 @@ static void sifive_pdma_write(void *opaque, hwaddr offset, {SiFivePDMAState *s = opaque;int ch =SIFIVE_PDMA_CHAN_NO (offset); +bool claimed;if (ch >=SIFIVE_PDMA_CHANS ) { qemu_log_mask(LOG_GUEST_ERROR, "%s: Invalid channel no %d\n",@@ -231,6 +235,17 @@ static void sifive_pdma_write(void *opaque, hwaddr offset, offset &=0xfff ;switch (offset) {case DMA_CONTROL : + claimed = !!s->chan[ch].control &CONTROL_CLAIM ; + +if (!claimed && (value &CONTROL_CLAIM )) { +/* reset Next* registers */
hw/dma/sifive_pdma.c static void sifive_pdma_write(void *opaque, hwaddr offset, · line 251
Quoted 10 lines
hw/dma/sifive_pdma.c +7
-0+ s->chan[ch].next_config = (CONFIG_RDSZ_DEFAULT <<CONFIG_RDSZ_SHIFT ) | + (CONFIG_WRSZ_DEFAULT <<CONFIG_WRSZ_SHIFT ); + s->chan[ch].next_bytes =0 ; + s->chan[ch].next_dst =0 ; + s->chan[ch].next_src =0 ; + } + s->chan[ch].control = value;if (value &CONTROL_RUN ) {
Regards,
Bin
Bin Meng
Reviewed-byRe: [PATCH 1/4] hw/dma: sifive_pdma: reset Next* registers when Control.claim is set
Quoted 22 lines · Bin Meng
Quoted 20 lines · On Fri, Sep 10, 2021 at 1:56 PM <[email protected]> wrote:
From: Frank Chang <[email protected]>
Setting Control.claim clears all of the chanel's Next registers. This is effective only when Control.claim is set from 0 to 1.
Signed-off-by Frank Chang <[email protected]>Tested-by Max Hsu <[email protected]>--- hw/dma/sifive_pdma.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)hw/dma/sifive_pdma.c +0
-0@@ -54,6 +54,9 @@#define DMA_EXEC_DST 0x110#define DMA_EXEC_SRC 0x118
Quoted 5 lines
hw/dma/sifive_pdma.c line 58
Quoted 2 lines
hw/dma/sifive_pdma.c +2
-0+#define CONFIG_WRSZ_DEFAULT 6 +#define CONFIG_RDSZ_DEFAULT 6The FU540 manual says the next config reset value is 0, not 6.
From patch#2 's log on Unmatched, I see where the number 6 is coming. I also validated on Unleashed and observed the same. So there is a documentation error.
Please add a comment to explain that.
Otherwise,
Bin Meng
Tested-byRe: [PATCH 1/4] hw/dma: sifive_pdma: reset Next* registers when Control.claim is set
Quoted 30 lines · Bin Meng
Quoted 22 lines · Bin Meng
Quoted 20 lines · On Fri, Sep 10, 2021 at 1:56 PM <[email protected]> wrote:
From: Frank Chang <[email protected]>
Setting Control.claim clears all of the chanel's Next registers. This is effective only when Control.claim is set from 0 to 1.
Signed-off-by Frank Chang <[email protected]>Tested-by Max Hsu <[email protected]>--- hw/dma/sifive_pdma.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)hw/dma/sifive_pdma.c +0
-0@@ -54,6 +54,9 @@#define DMA_EXEC_DST 0x110#define DMA_EXEC_SRC 0x118Quoted 5 lines
hw/dma/sifive_pdma.c line 58
Quoted 2 lines
hw/dma/sifive_pdma.c +2
-0+#define CONFIG_WRSZ_DEFAULT 6 +#define CONFIG_RDSZ_DEFAULT 6The FU540 manual says the next config reset value is 0, not 6.
Quoted 8 lines
From patch#2 's log on Unmatched, I see where the number 6 is coming. I also validated on Unleashed and observed the same. So there is a documentation error.
Please add a comment to explain that.
Otherwise,
Reviewed-by Bin Meng <[email protected]>
Re: [PATCH 1/4] hw/dma: sifive_pdma: reset Next* registers when Control.claim is set
In reply to Bin MengQuoted 29 lines · Bin Meng
Quoted 22 lines · Bin Meng
Quoted 20 lines · On Fri, Sep 10, 2021 at 1:56 PM <[email protected]> wrote:
From: Frank Chang <[email protected]>
Setting Control.claim clears all of the chanel's Next registers. This is effective only when Control.claim is set from 0 to 1.
Signed-off-by Frank Chang <[email protected]>Tested-by Max Hsu <[email protected]>--- hw/dma/sifive_pdma.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)hw/dma/sifive_pdma.c +0
-0@@ -54,6 +54,9 @@#define DMA_EXEC_DST 0x110#define DMA_EXEC_SRC 0x118Quoted 5 lines
hw/dma/sifive_pdma.c line 58
Quoted 2 lines
hw/dma/sifive_pdma.c +2
-0+#define CONFIG_WRSZ_DEFAULT 6 +#define CONFIG_RDSZ_DEFAULT 6The FU540 manual says the next config reset value is 0, not 6.
Quoted 6 lines
From patch#2 's log on Unmatched, I see where the number 6 is coming. I also validated on Unleashed and observed the same. So there is a documentation error.
Please add a comment to explain that.
Thanks for the review.
Will update it.
Regards,
Frank Chang
Quoted 4 lines
Otherwise,
Reviewed-by Bin Meng <[email protected]>
PATCH 2/4 hw/dma: sifive_pdma: claim bit must be set before DMA transactions
In reply to [email protected]From: Frank Chang <[email protected]>
Real PDMA must have Control.claim bit to be set before Control.run bit is set to start any DMA transactions. Otherwise nothing will be transferred.
The following result is PDMA tested in U-boot on Unmatched board:
=> mw.l 0x3000000 0x0 <= Disclaim channel 0
(Channel 0 is not claimed)
=> mw.l 0x3000004 0x55000000 <= wsize = rsize = 5 (2^5 = 32 bytes) => mw.q 0x3000008 0x2 <= NextBytes = 2 => mw.q 0x3000010 0x84000000 <= NextDestination = 0x84000000 => mw.q 0x3000018 0x84001000 <= NextSource = 0x84001000 => mw.l 0x84000000 0x87654321 <= Fill test data to dst => mw.l 0x84001000 0x12345678 <= Fill test data to src => md.l 0x84000000 1; md.l 0x84001000 1 <= Dump src/dst memory contents
84000000: 87654321 !Ce.
84001000: 12345678 xV4.
=> md.l 0x3000000 8 <= Dump PDMA status
03000000: 00000000 55000000 00000002 00000000 .......U........
03000010: 84000000 00000000 84001000 00000000 ................
=> mw.l 0x3000000 0x3 <= Set channel 0 run and claim bits
=> md.l 0x3000000 8 <= Dump PDMA status
03000000: 00000001 66000000 00000000 00000000 .......f........
03000010: 00000000 00000000 00000000 00000000 ................
=> md.l 0x84000000 1; md.l 0x84001000 1 <= Dump src/dst memory contents
84000000: 87654321 !Ce.
84001000: 12345678 xV4.
--- hw/dma/sifive_pdma.c | 9 +++++++++ 1 file changed, 9 insertions(+)
hw/dma/sifive_pdma.c +9 -0
@@ -248,6 +248,15 @@ static void sifive_pdma_write(void *opaque, hwaddr offset, s->chan[ch].control = value; +/* +* If channel was not claimed before run bit is set, +* DMA won't run. +*/ +if (!claimed) { + s->chan[ch].control &= ~CONTROL_RUN ; +return ; + } +if (value &CONTROL_RUN ) {sifive_pdma_run (s, ch); }
Signature · 1 line
2.25.1
Re: [PATCH 2/4] hw/dma: sifive_pdma: claim bit must be set before DMA transactions
Quoted 8 lines · On Fri, Sep 10, 2021 at 1:58 PM <[email protected]> wrote:
From: Frank Chang <[email protected]>
Real PDMA must have Control.claim bit to be set before Control.run bit is set to start any DMA transactions. Otherwise nothing will be transferred.
The following result is PDMA tested in U-boot on Unmatched board:
%s/U-boot/U-Boot
Quoted 23 lines
=> mw.l
0x30000000x0<= Disclaim channel 0
(Channel 0 is not claimed)=> mw.l 0x3000004 0x55000000 <= wsize = rsize = 5 (2^5 = 32 bytes) => mw.q 0x3000008 0x2 <= NextBytes = 2 => mw.q 0x3000010 0x84000000 <= NextDestination = 0x84000000 => mw.q 0x3000018 0x84001000 <= NextSource = 0x84001000 => mw.l 0x84000000 0x87654321 <= Fill test data to dst => mw.l 0x84001000 0x12345678 <= Fill test data to src => md.l 0x84000000 1; md.l 0x84001000 1 <= Dump src/dst memory contents84000000: 87654321 !Ce.
84001000: 12345678 xV4.
=> md.l0x30000008 <= Dump PDMA status
03000000: 00000000 55000000 00000002 00000000 .......U........
03000010: 84000000 00000000 84001000 00000000 ................
=> mw.l0x30000000x3<= Set channel 0 run and claim bits
=> md.l0x30000008 <= Dump PDMA status
03000000: 00000001 66000000 00000000 00000000 .......f........
03000010: 00000000 00000000 00000000 00000000 ................
=> md.l0x840000001; md.l0x840010001 <= Dump src/dst memory contents
84000000: 87654321 !Ce.
84001000: 12345678 xV4.
Quoted 6 lines
Signed-off-by Frank Chang <[email protected]>Tested-by Max Hsu <[email protected]>--- hw/dma/sifive_pdma.c | 9 +++++++++ 1 file changed, 9 insertions(+)
Otherwise,
PATCH 3/4 hw/dma: sifive_pdma: allow non-multiple transaction size transactions
In reply to [email protected]From: Green Wan <[email protected]>
Real PDMA is able to deal with non-multiple transaction size transactions.
The following result is PDMA tested in U-boot on Unmatched board:
=> mw.l 0x3000000 0x0 <= Disclaim channel 0 => mw.l 0x3000000 0x1 <= Claim channel 0 => mw.l 0x3000004 0x11000000 <= wsize = rsize = 1 (2^1 = 2 bytes) => mw.q 0x3000008 0x3 <= NextBytes = 3 => mw.q 0x3000010 0x84000000 <= NextDestination = 0x84000000 => mw.q 0x3000018 0x84001000 <= NextSource = 0x84001000 => mw.l 0x84000000 0x87654321 <= Fill test data to dst => mw.l 0x84001000 0x12345678 <= Fill test data to src => md.l 0x84000000 1; md.l 0x84001000 1 <= Dump src/dst memory contents
84000000: 87654321 !Ce.
84001000: 12345678 xV4.
=> md.l 0x3000000 8 <= Dump PDMA status
03000000: 00000001 11000000 00000003 00000000 ................
03000010: 84000000 00000000 84001000 00000000 ................
=> mw.l 0x3000000 0x3 <= Set channel 0 run and claim bits
=> md.l 0x3000000 8 <= Dump PDMA status
03000000: 40000001 11000000 00000003 00000000 ...@............
03000010: 84000000 00000000 84001000 00000000 ................
=> md.l 0x84000000 1; md.l 0x84001000 1 <= Dump src/dst memory contents
84000000: 87345678 xV4.
84001000: 12345678 xV4.
--- hw/dma/sifive_pdma.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
hw/dma/sifive_pdma.c +10 -6
@@ -70,7 +70,7 @@ static void sifive_pdma_run(SiFivePDMAState *s, int ch)uint64_t dst = s->chan[ch].next_dst;uint64_t src = s->chan[ch].next_src;uint32_t config = s->chan[ch].next_config; -int wsize, rsize, size; +int wsize, rsize, size, remainder;uint8_t buf[64 ];int n;
@@ -102,11 +102,7 @@ static void sifive_pdma_run(SiFivePDMAState *s, int ch) size =6 ; } size =1 << size; - -/* the bytes to transfer should be multiple of transaction size */ -if (bytes % size) { - goto error; - } + remainder = bytes % size;/* indicate a DMA transfer is started */ s->chan[ch].state =DMA_CHAN_STATE_STARTED ;
@@ -127,6 +123,14 @@ static void sifive_pdma_run(SiFivePDMAState *s, int ch) s->chan[ch].exec_bytes -= size; } +if (remainder) { +cpu_physical_memory_read (s->chan[ch].exec_src, buf, remainder); +cpu_physical_memory_write (s->chan[ch].exec_dst, buf, remainder); + s->chan[ch].exec_src += remainder; + s->chan[ch].exec_dst += remainder; + s->chan[ch].exec_bytes -= remainder; + } +/* indicate a DMA transfer is done */ s->chan[ch].state =DMA_CHAN_STATE_DONE ; s->chan[ch].control &= ~CONTROL_RUN ;
Signature · 1 line
2.25.1
Re: [PATCH 3/4] hw/dma: sifive_pdma: allow non-multiple transaction size transactions
Quoted 6 lines · On Fri, Sep 10, 2021 at 1:56 PM <[email protected]> wrote:
From: Green Wan <[email protected]>
Real PDMA is able to deal with non-multiple transaction size transactions.
The following result is PDMA tested in U-boot on Unmatched board:
%s/U-boot/U-Boot
Quoted 23 lines
=> mw.l 0x3000000 0x0 <= Disclaim channel 0 => mw.l 0x3000000 0x1 <= Claim channel 0 => mw.l 0x3000004 0x11000000 <= wsize = rsize = 1 (2^1 = 2 bytes) => mw.q 0x3000008 0x3 <= NextBytes = 3 => mw.q 0x3000010 0x84000000 <= NextDestination = 0x84000000 => mw.q 0x3000018 0x84001000 <= NextSource = 0x84001000 => mw.l 0x84000000 0x87654321 <= Fill test data to dst => mw.l 0x84001000 0x12345678 <= Fill test data to src => md.l 0x84000000 1; md.l 0x84001000 1 <= Dump src/dst memory contents84000000: 87654321 !Ce.
84001000: 12345678 xV4.
=> md.l0x30000008 <= Dump PDMA status
03000000: 00000001 11000000 00000003 00000000 ................
03000010: 84000000 00000000 84001000 00000000 ................
=> mw.l0x30000000x3<= Set channel 0 run and claim bits
=> md.l0x30000008 <= Dump PDMA status
03000000: 40000001 11000000 00000003 00000000 ...@............
03000010: 84000000 00000000 84001000 00000000 ................
=> md.l0x840000001; md.l0x840010001 <= Dump src/dst memory contents
84000000: 87345678 xV4.
84001000: 12345678 xV4.
Quoted 8 lines
Signed-off-by Green Wan <[email protected]>Reviewed-by Frank Chang <[email protected]>Tested-by Max Hsu <[email protected]>Signed-off-by Frank Chang <[email protected]>--- hw/dma/sifive_pdma.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
Otherwise,
PATCH 4/4 hw/dma: sifive_pdma: don't set Control.error if 0 bytes to transfer
In reply to [email protected]From: Frank Chang <[email protected]>
Real PDMA doesn't set Control.error if there are 0 bytes to be transferred. The DMA transfer is still success.
The following result is PDMA tested in U-boot on Unmatched board:
=> mw.l 0x3000000 0x0 <= Disclaim channel 0 => mw.l 0x3000000 0x1 <= Claim channel 0 => mw.l 0x3000004 0x55000000 <= wsize = rsize = 5 (2^5 = 32 bytes) => mw.q 0x3000008 0x0 <= NextBytes = 0 => mw.q 0x3000010 0x84000000 <= NextDestination = 0x84000000 => mw.q 0x3000018 0x84001000 <= NextSource = 0x84001000 => mw.l 0x84000000 0x87654321 <= Fill test data to dst => mw.l 0x84001000 0x12345678 <= Fill test data to src => md.l 0x84000000 1; md.l 0x84001000 1 <= Dump src/dst memory contents
84000000: 87654321 !Ce.
84001000: 12345678 xV4.
=> md.l 0x3000000 8 <= Dump PDMA status
03000000: 00000001 55000000 00000000 00000000 .......U........
03000010: 84000000 00000000 84001000 00000000 ................
=> mw.l 0x3000000 0x3 <= Set channel 0 run and claim bits
=> md.l 0x3000000 8 <= Dump PDMA status
03000000: 40000001 55000000 00000000 00000000 [email protected]........
03000010: 84000000 00000000 84001000 00000000 ................
=> md.l 0x84000000 1; md.l 0x84001000 1 <= Dump src/dst memory contents
84000000: 87654321 !Ce.
84001000: 12345678 xV4.
--- hw/dma/sifive_pdma.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
hw/dma/sifive_pdma.c +6 -6
@@ -76,7 +76,7 @@ static void sifive_pdma_run(SiFivePDMAState *s, int ch)/* do nothing if bytes to transfer is zero */ if (!bytes) { - goto error; + goto done; }/*
@@ -131,11 +131,6 @@ static void sifive_pdma_run(SiFivePDMAState *s, int ch) s->chan[ch].exec_bytes -= remainder; } -/* indicate a DMA transfer is done */ - s->chan[ch].state =DMA_CHAN_STATE_DONE ; - s->chan[ch].control &= ~CONTROL_RUN ; - s->chan[ch].control |=CONTROL_DONE ; -/* reload exec_ registers if repeat is required */ if (s->chan[ch].next_config &CONFIG_REPEAT ) { s->chan[ch].exec_bytes = bytes;
@@ -143,6 +138,11 @@ static void sifive_pdma_run(SiFivePDMAState *s, int ch) s->chan[ch].exec_src = src; } +done: +/* indicate a DMA transfer is done */ + s->chan[ch].state =DMA_CHAN_STATE_DONE ; + s->chan[ch].control &= ~CONTROL_RUN ; + s->chan[ch].control |=CONTROL_DONE ;return ; error:
Signature · 1 line
2.25.1
Re: [PATCH 4/4] hw/dma: sifive_pdma: don't set Control.error if 0 bytes to transfer
Quoted 4 lines · On Fri, Sep 10, 2021 at 1:59 PM <[email protected]> wrote:
From: Frank Chang <[email protected]>
Real PDMA doesn't set Control.error if there are 0 bytes to be
nits: 0 byte
Please fix the commit title as well
Quoted 3 lines
transferred. The DMA transfer is still success.
The following result is PDMA tested in U-boot on Unmatched board:
%s/U-boot/U-Boot
Quoted 23 lines
=> mw.l 0x3000000 0x0 <= Disclaim channel 0 => mw.l 0x3000000 0x1 <= Claim channel 0 => mw.l 0x3000004 0x55000000 <= wsize = rsize = 5 (2^5 = 32 bytes) => mw.q 0x3000008 0x0 <= NextBytes = 0 => mw.q 0x3000010 0x84000000 <= NextDestination = 0x84000000 => mw.q 0x3000018 0x84001000 <= NextSource = 0x84001000 => mw.l 0x84000000 0x87654321 <= Fill test data to dst => mw.l 0x84001000 0x12345678 <= Fill test data to src => md.l 0x84000000 1; md.l 0x84001000 1 <= Dump src/dst memory contents84000000: 87654321 !Ce.
84001000: 12345678 xV4.
=> md.l0x30000008 <= Dump PDMA status
03000000: 00000001 55000000 00000000 00000000 .......U........
03000010: 84000000 00000000 84001000 00000000 ................
=> mw.l0x30000000x3<= Set channel 0 run and claim bits
=> md.l0x30000008 <= Dump PDMA status
03000000: 40000001 55000000 00000000 00000000 [email protected]........
03000010: 84000000 00000000 84001000 00000000 ................
=> md.l0x840000001; md.l0x840010001 <= Dump src/dst memory contents
84000000: 87654321 !Ce.
84001000: 12345678 xV4.
Quoted 6 lines
Signed-off-by Frank Chang <[email protected]>Tested-by Max Hsu <[email protected]>--- hw/dma/sifive_pdma.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
Otherwise,