qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/2] hw/dma: sifive_pdma: Fix Control.claim bit detection
@ 2021-09-27  7:21 Bin Meng
  2021-09-27  7:21 ` [PATCH v2 2/2] hw/dma: sifive_pdma: Don't run DMA when channel is disclaimed Bin Meng
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Bin Meng @ 2021-09-27  7:21 UTC (permalink / raw)
  To: Alistair Francis, qemu-devel, qemu-riscv
  Cc: Frank Chang, Philippe Mathieu-Daudé, Markus Armbruster

At present the codes detect whether the DMA channel is claimed by:

  claimed = !!s->chan[ch].control & CONTROL_CLAIM;

As ! has higher precedence over & (bitwise and), this is essentially

  claimed = (!!s->chan[ch].control) & CONTROL_CLAIM;

which is wrong, as any non-zero bit set in the control register will
produce a result of a claimed channel.

Fixes: de7c7988d25d ("hw/dma: sifive_pdma: reset Next* registers when Control.claim is set")
Signed-off-by: Bin Meng <bmeng.cn@gmail.com>

---

Changes in v2:
- reword the commit message

 hw/dma/sifive_pdma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/hw/dma/sifive_pdma.c b/hw/dma/sifive_pdma.c
index b4fd40573a..b8ec7621f3 100644
--- a/hw/dma/sifive_pdma.c
+++ b/hw/dma/sifive_pdma.c
@@ -243,7 +243,7 @@ static void sifive_pdma_write(void *opaque, hwaddr offset,
     offset &= 0xfff;
     switch (offset) {
     case DMA_CONTROL:
-        claimed = !!s->chan[ch].control & CONTROL_CLAIM;
+        claimed = !!(s->chan[ch].control & CONTROL_CLAIM);
 
         if (!claimed && (value & CONTROL_CLAIM)) {
             /* reset Next* registers */
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/2] hw/dma: sifive_pdma: Don't run DMA when channel is disclaimed
  2021-09-27  7:21 [PATCH v2 1/2] hw/dma: sifive_pdma: Fix Control.claim bit detection Bin Meng
@ 2021-09-27  7:21 ` Bin Meng
  2021-09-28 22:46   ` Alistair Francis
  2021-09-28 23:15   ` Alistair Francis
  2021-09-27 12:56 ` [PATCH v2 1/2] hw/dma: sifive_pdma: Fix Control.claim bit detection Philippe Mathieu-Daudé
  2021-09-28 22:45 ` Alistair Francis
  2 siblings, 2 replies; 7+ messages in thread
From: Bin Meng @ 2021-09-27  7:21 UTC (permalink / raw)
  To: Alistair Francis, qemu-devel, qemu-riscv
  Cc: Frank Chang, Philippe Mathieu-Daudé, Markus Armbruster

If Control.run bit is set while not preserving the Control.claim
bit, the DMA transfer shall not be started.

The following result is PDMA tested in U-Boot on Unleashed 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 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: 00000001 55000000 00000002 00000000    .......U........
03000010: 84000000 00000000 84001000 00000000    ................
=> mw.l 0x3000000 0x2                      <= Set channel 0 run bit only
=> md.l 0x3000000 8                        <= Dump PDMA status
03000000: 00000000 55000000 00000002 00000000    .......U........
03000010: 84000000 00000000 84001000 00000000    ................
=> md.l 0x84000000 1; md.l 0x84001000 1    <= Dump src/dst memory contents
84000000: 87654321                               !Ce.
84001000: 12345678                               xV4.

Signed-off-by: Bin Meng <bmeng.cn@gmail.com>

---

(no changes since v1)

 hw/dma/sifive_pdma.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/hw/dma/sifive_pdma.c b/hw/dma/sifive_pdma.c
index b8ec7621f3..85fe34f5f3 100644
--- a/hw/dma/sifive_pdma.c
+++ b/hw/dma/sifive_pdma.c
@@ -232,7 +232,7 @@ static void sifive_pdma_write(void *opaque, hwaddr offset,
 {
     SiFivePDMAState *s = opaque;
     int ch = SIFIVE_PDMA_CHAN_NO(offset);
-    bool claimed;
+    bool claimed, run;
 
     if (ch >= SIFIVE_PDMA_CHANS) {
         qemu_log_mask(LOG_GUEST_ERROR, "%s: Invalid channel no %d\n",
@@ -244,6 +244,7 @@ static void sifive_pdma_write(void *opaque, hwaddr offset,
     switch (offset) {
     case DMA_CONTROL:
         claimed = !!(s->chan[ch].control & CONTROL_CLAIM);
+        run = !!(s->chan[ch].control & CONTROL_RUN);
 
         if (!claimed && (value & CONTROL_CLAIM)) {
             /* reset Next* registers */
@@ -254,13 +255,19 @@ static void sifive_pdma_write(void *opaque, hwaddr offset,
             s->chan[ch].next_src = 0;
         }
 
+        /* claim bit can only be cleared when run is low */
+        if (run && !(value & CONTROL_CLAIM)) {
+            value |= CONTROL_CLAIM;
+        }
+
         s->chan[ch].control = value;
 
         /*
          * If channel was not claimed before run bit is set,
+         * or if the channel is disclaimed when run was low,
          * DMA won't run.
          */
-        if (!claimed) {
+        if (!claimed || (!run && !(value & CONTROL_CLAIM))) {
             s->chan[ch].control &= ~CONTROL_RUN;
             return;
         }
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/2] hw/dma: sifive_pdma: Fix Control.claim bit detection
  2021-09-27  7:21 [PATCH v2 1/2] hw/dma: sifive_pdma: Fix Control.claim bit detection Bin Meng
  2021-09-27  7:21 ` [PATCH v2 2/2] hw/dma: sifive_pdma: Don't run DMA when channel is disclaimed Bin Meng
@ 2021-09-27 12:56 ` Philippe Mathieu-Daudé
  2021-09-27 13:14   ` Bin Meng
  2021-09-28 22:45 ` Alistair Francis
  2 siblings, 1 reply; 7+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-09-27 12:56 UTC (permalink / raw)
  To: Bin Meng, Alistair Francis, qemu-devel, qemu-riscv
  Cc: Frank Chang, Markus Armbruster

On 9/27/21 09:21, Bin Meng wrote:
> At present the codes detect whether the DMA channel is claimed by:
> 
>   claimed = !!s->chan[ch].control & CONTROL_CLAIM;
> 
> As ! has higher precedence over & (bitwise and), this is essentially
> 
>   claimed = (!!s->chan[ch].control) & CONTROL_CLAIM;
> 
> which is wrong, as any non-zero bit set in the control register will
> produce a result of a claimed channel.

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

Maybe worth adding:

Reported using GCC 9.3.0 on Ubuntu 20.04:

  suggest parentheses around operand of ‘!’ or change ‘&’ to ‘&&’ or ‘!’
to ‘~’ [-Werror=parentheses]

> Fixes: de7c7988d25d ("hw/dma: sifive_pdma: reset Next* registers when Control.claim is set")
> Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
> 
> ---
> 
> Changes in v2:
> - reword the commit message
> 
>  hw/dma/sifive_pdma.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/hw/dma/sifive_pdma.c b/hw/dma/sifive_pdma.c
> index b4fd40573a..b8ec7621f3 100644
> --- a/hw/dma/sifive_pdma.c
> +++ b/hw/dma/sifive_pdma.c
> @@ -243,7 +243,7 @@ static void sifive_pdma_write(void *opaque, hwaddr offset,
>      offset &= 0xfff;
>      switch (offset) {
>      case DMA_CONTROL:
> -        claimed = !!s->chan[ch].control & CONTROL_CLAIM;
> +        claimed = !!(s->chan[ch].control & CONTROL_CLAIM);
>  
>          if (!claimed && (value & CONTROL_CLAIM)) {
>              /* reset Next* registers */
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/2] hw/dma: sifive_pdma: Fix Control.claim bit detection
  2021-09-27 12:56 ` [PATCH v2 1/2] hw/dma: sifive_pdma: Fix Control.claim bit detection Philippe Mathieu-Daudé
@ 2021-09-27 13:14   ` Bin Meng
  0 siblings, 0 replies; 7+ messages in thread
From: Bin Meng @ 2021-09-27 13:14 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: Frank Chang, open list:RISC-V, Alistair Francis,
	qemu-devel@nongnu.org Developers, Markus Armbruster

Hi Philippe,

On Mon, Sep 27, 2021 at 8:56 PM Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:
>
> On 9/27/21 09:21, Bin Meng wrote:
> > At present the codes detect whether the DMA channel is claimed by:
> >
> >   claimed = !!s->chan[ch].control & CONTROL_CLAIM;
> >
> > As ! has higher precedence over & (bitwise and), this is essentially
> >
> >   claimed = (!!s->chan[ch].control) & CONTROL_CLAIM;
> >
> > which is wrong, as any non-zero bit set in the control register will
> > produce a result of a claimed channel.
>
> Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
>
> Maybe worth adding:
>
> Reported using GCC 9.3.0 on Ubuntu 20.04:
>
>   suggest parentheses around operand of ‘!’ or change ‘&’ to ‘&&’ or ‘!’
> to ‘~’ [-Werror=parentheses]

Actually GCC does not report this as CONTROL_CLAIM happens to be 1 in
this case, as I wrote in the v1 commit message. :)

Regards,
Bin


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/2] hw/dma: sifive_pdma: Fix Control.claim bit detection
  2021-09-27  7:21 [PATCH v2 1/2] hw/dma: sifive_pdma: Fix Control.claim bit detection Bin Meng
  2021-09-27  7:21 ` [PATCH v2 2/2] hw/dma: sifive_pdma: Don't run DMA when channel is disclaimed Bin Meng
  2021-09-27 12:56 ` [PATCH v2 1/2] hw/dma: sifive_pdma: Fix Control.claim bit detection Philippe Mathieu-Daudé
@ 2021-09-28 22:45 ` Alistair Francis
  2 siblings, 0 replies; 7+ messages in thread
From: Alistair Francis @ 2021-09-28 22:45 UTC (permalink / raw)
  To: Bin Meng
  Cc: open list:RISC-V, Frank Chang, qemu-devel@nongnu.org Developers,
	Markus Armbruster, Alistair Francis, Philippe Mathieu-Daudé

On Mon, Sep 27, 2021 at 5:21 PM Bin Meng <bmeng.cn@gmail.com> wrote:
>
> At present the codes detect whether the DMA channel is claimed by:
>
>   claimed = !!s->chan[ch].control & CONTROL_CLAIM;
>
> As ! has higher precedence over & (bitwise and), this is essentially
>
>   claimed = (!!s->chan[ch].control) & CONTROL_CLAIM;
>
> which is wrong, as any non-zero bit set in the control register will
> produce a result of a claimed channel.
>
> Fixes: de7c7988d25d ("hw/dma: sifive_pdma: reset Next* registers when Control.claim is set")
> Signed-off-by: Bin Meng <bmeng.cn@gmail.com>

Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

Alistair

>
> ---
>
> Changes in v2:
> - reword the commit message
>
>  hw/dma/sifive_pdma.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/hw/dma/sifive_pdma.c b/hw/dma/sifive_pdma.c
> index b4fd40573a..b8ec7621f3 100644
> --- a/hw/dma/sifive_pdma.c
> +++ b/hw/dma/sifive_pdma.c
> @@ -243,7 +243,7 @@ static void sifive_pdma_write(void *opaque, hwaddr offset,
>      offset &= 0xfff;
>      switch (offset) {
>      case DMA_CONTROL:
> -        claimed = !!s->chan[ch].control & CONTROL_CLAIM;
> +        claimed = !!(s->chan[ch].control & CONTROL_CLAIM);
>
>          if (!claimed && (value & CONTROL_CLAIM)) {
>              /* reset Next* registers */
> --
> 2.25.1
>
>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 2/2] hw/dma: sifive_pdma: Don't run DMA when channel is disclaimed
  2021-09-27  7:21 ` [PATCH v2 2/2] hw/dma: sifive_pdma: Don't run DMA when channel is disclaimed Bin Meng
@ 2021-09-28 22:46   ` Alistair Francis
  2021-09-28 23:15   ` Alistair Francis
  1 sibling, 0 replies; 7+ messages in thread
From: Alistair Francis @ 2021-09-28 22:46 UTC (permalink / raw)
  To: Bin Meng
  Cc: open list:RISC-V, Frank Chang, qemu-devel@nongnu.org Developers,
	Markus Armbruster, Alistair Francis, Philippe Mathieu-Daudé

On Mon, Sep 27, 2021 at 5:22 PM Bin Meng <bmeng.cn@gmail.com> wrote:
>
> If Control.run bit is set while not preserving the Control.claim
> bit, the DMA transfer shall not be started.
>
> The following result is PDMA tested in U-Boot on Unleashed 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 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: 00000001 55000000 00000002 00000000    .......U........
> 03000010: 84000000 00000000 84001000 00000000    ................
> => mw.l 0x3000000 0x2                      <= Set channel 0 run bit only
> => md.l 0x3000000 8                        <= Dump PDMA status
> 03000000: 00000000 55000000 00000002 00000000    .......U........
> 03000010: 84000000 00000000 84001000 00000000    ................
> => md.l 0x84000000 1; md.l 0x84001000 1    <= Dump src/dst memory contents
> 84000000: 87654321                               !Ce.
> 84001000: 12345678                               xV4.
>
> Signed-off-by: Bin Meng <bmeng.cn@gmail.com>

Acked-by: Alistair Francis <alistair.francis@wdc.com>

Alistair

>
> ---
>
> (no changes since v1)
>
>  hw/dma/sifive_pdma.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/hw/dma/sifive_pdma.c b/hw/dma/sifive_pdma.c
> index b8ec7621f3..85fe34f5f3 100644
> --- a/hw/dma/sifive_pdma.c
> +++ b/hw/dma/sifive_pdma.c
> @@ -232,7 +232,7 @@ static void sifive_pdma_write(void *opaque, hwaddr offset,
>  {
>      SiFivePDMAState *s = opaque;
>      int ch = SIFIVE_PDMA_CHAN_NO(offset);
> -    bool claimed;
> +    bool claimed, run;
>
>      if (ch >= SIFIVE_PDMA_CHANS) {
>          qemu_log_mask(LOG_GUEST_ERROR, "%s: Invalid channel no %d\n",
> @@ -244,6 +244,7 @@ static void sifive_pdma_write(void *opaque, hwaddr offset,
>      switch (offset) {
>      case DMA_CONTROL:
>          claimed = !!(s->chan[ch].control & CONTROL_CLAIM);
> +        run = !!(s->chan[ch].control & CONTROL_RUN);
>
>          if (!claimed && (value & CONTROL_CLAIM)) {
>              /* reset Next* registers */
> @@ -254,13 +255,19 @@ static void sifive_pdma_write(void *opaque, hwaddr offset,
>              s->chan[ch].next_src = 0;
>          }
>
> +        /* claim bit can only be cleared when run is low */
> +        if (run && !(value & CONTROL_CLAIM)) {
> +            value |= CONTROL_CLAIM;
> +        }
> +
>          s->chan[ch].control = value;
>
>          /*
>           * If channel was not claimed before run bit is set,
> +         * or if the channel is disclaimed when run was low,
>           * DMA won't run.
>           */
> -        if (!claimed) {
> +        if (!claimed || (!run && !(value & CONTROL_CLAIM))) {
>              s->chan[ch].control &= ~CONTROL_RUN;
>              return;
>          }
> --
> 2.25.1
>
>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 2/2] hw/dma: sifive_pdma: Don't run DMA when channel is disclaimed
  2021-09-27  7:21 ` [PATCH v2 2/2] hw/dma: sifive_pdma: Don't run DMA when channel is disclaimed Bin Meng
  2021-09-28 22:46   ` Alistair Francis
@ 2021-09-28 23:15   ` Alistair Francis
  1 sibling, 0 replies; 7+ messages in thread
From: Alistair Francis @ 2021-09-28 23:15 UTC (permalink / raw)
  To: Bin Meng
  Cc: open list:RISC-V, Frank Chang, qemu-devel@nongnu.org Developers,
	Markus Armbruster, Alistair Francis, Philippe Mathieu-Daudé

On Mon, Sep 27, 2021 at 5:22 PM Bin Meng <bmeng.cn@gmail.com> wrote:
>
> If Control.run bit is set while not preserving the Control.claim
> bit, the DMA transfer shall not be started.
>
> The following result is PDMA tested in U-Boot on Unleashed 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 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: 00000001 55000000 00000002 00000000    .......U........
> 03000010: 84000000 00000000 84001000 00000000    ................
> => mw.l 0x3000000 0x2                      <= Set channel 0 run bit only
> => md.l 0x3000000 8                        <= Dump PDMA status
> 03000000: 00000000 55000000 00000002 00000000    .......U........
> 03000010: 84000000 00000000 84001000 00000000    ................
> => md.l 0x84000000 1; md.l 0x84001000 1    <= Dump src/dst memory contents
> 84000000: 87654321                               !Ce.
> 84001000: 12345678                               xV4.
>
> Signed-off-by: Bin Meng <bmeng.cn@gmail.com>

Thanks!

Applied to riscv-to-apply.next

Alistair

>
> ---
>
> (no changes since v1)
>
>  hw/dma/sifive_pdma.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/hw/dma/sifive_pdma.c b/hw/dma/sifive_pdma.c
> index b8ec7621f3..85fe34f5f3 100644
> --- a/hw/dma/sifive_pdma.c
> +++ b/hw/dma/sifive_pdma.c
> @@ -232,7 +232,7 @@ static void sifive_pdma_write(void *opaque, hwaddr offset,
>  {
>      SiFivePDMAState *s = opaque;
>      int ch = SIFIVE_PDMA_CHAN_NO(offset);
> -    bool claimed;
> +    bool claimed, run;
>
>      if (ch >= SIFIVE_PDMA_CHANS) {
>          qemu_log_mask(LOG_GUEST_ERROR, "%s: Invalid channel no %d\n",
> @@ -244,6 +244,7 @@ static void sifive_pdma_write(void *opaque, hwaddr offset,
>      switch (offset) {
>      case DMA_CONTROL:
>          claimed = !!(s->chan[ch].control & CONTROL_CLAIM);
> +        run = !!(s->chan[ch].control & CONTROL_RUN);
>
>          if (!claimed && (value & CONTROL_CLAIM)) {
>              /* reset Next* registers */
> @@ -254,13 +255,19 @@ static void sifive_pdma_write(void *opaque, hwaddr offset,
>              s->chan[ch].next_src = 0;
>          }
>
> +        /* claim bit can only be cleared when run is low */
> +        if (run && !(value & CONTROL_CLAIM)) {
> +            value |= CONTROL_CLAIM;
> +        }
> +
>          s->chan[ch].control = value;
>
>          /*
>           * If channel was not claimed before run bit is set,
> +         * or if the channel is disclaimed when run was low,
>           * DMA won't run.
>           */
> -        if (!claimed) {
> +        if (!claimed || (!run && !(value & CONTROL_CLAIM))) {
>              s->chan[ch].control &= ~CONTROL_RUN;
>              return;
>          }
> --
> 2.25.1
>
>


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-09-28 23:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-27  7:21 [PATCH v2 1/2] hw/dma: sifive_pdma: Fix Control.claim bit detection Bin Meng
2021-09-27  7:21 ` [PATCH v2 2/2] hw/dma: sifive_pdma: Don't run DMA when channel is disclaimed Bin Meng
2021-09-28 22:46   ` Alistair Francis
2021-09-28 23:15   ` Alistair Francis
2021-09-27 12:56 ` [PATCH v2 1/2] hw/dma: sifive_pdma: Fix Control.claim bit detection Philippe Mathieu-Daudé
2021-09-27 13:14   ` Bin Meng
2021-09-28 22:45 ` Alistair Francis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).