hw/dma: Align SiFive PDMA behavior with real hardware
v2 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.
Changelog:
v2:
* Add comment to describe document errors.
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 | 54 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 11 deletions(-)
--
2.25.1
PATCH RESEND v2 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 | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
hw/dma/sifive_pdma.c +19 -0
@@ -54,6 +54,13 @@#define DMA_EXEC_DST 0x110#define DMA_EXEC_SRC 0x118 +/* +* FU540/FU740 docs are incorrect with NextConfig.wsize/rsize reset values. +* The reset values tested on Unleashed/Unmatched boards are 6 instead of 0. +*/ +#define CONFIG_WRSZ_DEFAULT 6 +#define CONFIG_RDSZ_DEFAULT 6 +enum dma_chan_state {DMA_CHAN_STATE_IDLE ,DMA_CHAN_STATE_STARTED ,
@@ -221,6 +228,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 +239,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
PATCH RESEND v2 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
@@ -252,6 +252,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
PATCH RESEND v2 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
@@ -74,7 +74,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;
@@ -106,11 +106,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 ;
@@ -131,6 +127,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
PATCH RESEND v2 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
@@ -80,7 +80,7 @@ static void sifive_pdma_run(SiFivePDMAState *s, int ch)/* do nothing if bytes to transfer is zero */ if (!bytes) { - goto error; + goto done; }/*
@@ -135,11 +135,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;
@@ -147,6 +142,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
Alistair Francis
AppliedRe: [PATCH RESEND v2 4/4] hw/dma: sifive_pdma: don't set Control.error if 0 bytes to transfer
Quoted 30 lines · On Sun, Sep 12, 2021 at 11:11 PM <[email protected]> wrote:
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 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 4 lines
Signed-off-by Frank Chang <[email protected]>Tested-by Max Hsu <[email protected]>Reviewed-by Bin Meng <[email protected]>Tested-by Bin Meng <[email protected]>
Thanks!
Applied to riscv-to-apply.next
Alistair
Quoted 45 lines
--- hw/dma/sifive_pdma.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)hw/dma/sifive_pdma.c +6
-7@@ -80,7 +80,7 @@ static void sifive_pdma_run(SiFivePDMAState *s, int ch)/* do nothing if bytes to transfer is zero */ if (!bytes) { - goto error; + goto done; }/* @@ -135,11 +135,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;@@ -147,6 +142,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: --2.25.1