Skip to contentSkip to conversation

hw/dma: sifive_pdma: Improve code readability for "!!foo & bar"

PATCH In review Bin Meng 3 participants 8 messages Sep 27, 2021 SiFive drivers

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH 1/2 hw/dma: sifive_pdma: Improve code readability for "!!foo & bar"

GCC seems to be strict about processing pattern like "!!for & bar". When 'bar' is not 0 or 1, it complains with -Werror=parentheses:

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

Add a () around "foo && bar", which also improves code readability.

---

 hw/dma/sifive_pdma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/dma/sifive_pdma.c +1 -1
index b4fd40573a..b8ec7621f3 100644
@@ -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 */
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 2/2 hw/dma: sifive_pdma: Don't run DMA when channel is disclaimed

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.

---

 hw/dma/sifive_pdma.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
hw/dma/sifive_pdma.c +9 -2
index b8ec7621f3..85fe34f5f3 100644
@@ -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;
         }
Signature · 1 line
2.25.1
#2 lore raw
In reply to Bin Meng
Quoted 18 lines · Bin Meng

GCC seems to be strict about processing pattern like "!!for & bar". When 'bar' is not 0 or 1, it complains with -Werror=parentheses:

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

Add a () around "foo && bar", which also improves code readability.

---

 hw/dma/sifive_pdma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/dma/sifive_pdma.c +0 -0
index b4fd40573a..b8ec7621f3 100644

hw/dma/sifive_pdma.c static void sifive_pdma_write(void *opaque, hwaddr offset, · line 246

Quoted 5 lines
hw/dma/sifive_pdma.c +1 -1
@@ -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);

AFAIK in C logical NOT has precedence over bitwise AND, so IIUC compilers should read the current code as:

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

meaning this patch is doing more than "improve code readability", this is a logical change and likely a bug fix...

BTW GCC suggestions are:

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

           claimed (!!s->chan[ch].control) && CONTROL_CLAIM;
Quoted 4 lines
hw/dma/sifive_pdma.c +0 -0
         if (!claimed && (value & CONTROL_CLAIM)) {
             /* reset Next* registers */
 
#3 lore raw
Quoted 2 lines · Philippe Mathieu-Daudé
Quoted 1 line · Bin Meng

GCC seems to be strict about processing pattern like "!!for & bar".

What GCC version btw?

Quoted 42 lines
Quoted 17 lines

When 'bar' is not 0 or 1, it complains with -Werror=parentheses:

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

Add a () around "foo && bar", which also improves code readability.

---

 hw/dma/sifive_pdma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/dma/sifive_pdma.c +0 -0
index b4fd40573a..b8ec7621f3 100644

hw/dma/sifive_pdma.c static void sifive_pdma_write(void *opaque, hwaddr offset, · line 246

Quoted 5 lines
hw/dma/sifive_pdma.c +1 -1
@@ -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);

AFAIK in C logical NOT has precedence over bitwise AND, so IIUC compilers should read the current code as:

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

meaning this patch is doing more than "improve code readability", this is a logical change and likely a bug fix...

BTW GCC suggestions are:

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

           claimed (!!s->chan[ch].control) && CONTROL_CLAIM;
Quoted 4 lines
hw/dma/sifive_pdma.c +0 -0
         if (!claimed && (value & CONTROL_CLAIM)) {
             /* reset Next* registers */
 
author
#4 lore raw
In reply to Philippe Mathieu-Daudé

Hi Philippe,

Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 18 lines · Bin Meng

GCC seems to be strict about processing pattern like "!!for & bar". When 'bar' is not 0 or 1, it complains with -Werror=parentheses:

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

Add a () around "foo && bar", which also improves code readability.

---

 hw/dma/sifive_pdma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/dma/sifive_pdma.c +0 -0
index b4fd40573a..b8ec7621f3 100644

hw/dma/sifive_pdma.c static void sifive_pdma_write(void *opaque, hwaddr offset, · line 246

Quoted 5 lines
hw/dma/sifive_pdma.c +1 -1
@@ -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);

AFAIK in C logical NOT has precedence over bitwise AND, so IIUC compilers should read the current code as:

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

meaning this patch is doing more than "improve code readability", this is a logical change and likely a bug fix...

Yes, you are correct. Indeed this is a bug fix. I did not dig into the operator precedence in detail. I will reword this in v2.

Quoted 10 lines

BTW GCC suggestions are:

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

           claimed (!!s->chan[ch].control) && CONTROL_CLAIM;
Quoted 4 lines
hw/dma/sifive_pdma.c +0 -0
         if (!claimed && (value & CONTROL_CLAIM)) {
             /* reset Next* registers */
 

I was using GCC 9.3.0 on Ubuntu 20.04.

Regards,
Bin

#5 lore raw
In reply to Bin Meng
Quoted 18 lines · Bin Meng

GCC seems to be strict about processing pattern like "!!for & bar". When 'bar' is not 0 or 1, it complains with -Werror=parentheses:

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

Add a () around "foo && bar", which also improves code readability.

---

 hw/dma/sifive_pdma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/dma/sifive_pdma.c +0 -0
index b4fd40573a..b8ec7621f3 100644

hw/dma/sifive_pdma.c static void sifive_pdma_write(void *opaque, hwaddr offset, · line 249

Quoted 8 lines
hw/dma/sifive_pdma.c +1 -1
@@ -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 */

Old code

first double-negate, mapping zero to zero, non-zero to one then mask, which does nothing, because CONTROL_CLAIM is 1

New code:

first mask, yielding 0 or 1
then double-negate, which does nothing

Looks like a bug fix to me. If I'm right, the commit message is wrong, and the double negate is redundant.

author
#6 lore raw

Hi Markus,

Quoted 35 lines · Markus Armbruster
Quoted 18 lines · Bin Meng

GCC seems to be strict about processing pattern like "!!for & bar". When 'bar' is not 0 or 1, it complains with -Werror=parentheses:

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

Add a () around "foo && bar", which also improves code readability.

---

 hw/dma/sifive_pdma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/dma/sifive_pdma.c +0 -0
index b4fd40573a..b8ec7621f3 100644

hw/dma/sifive_pdma.c static void sifive_pdma_write(void *opaque, hwaddr offset, · line 249

Quoted 8 lines
hw/dma/sifive_pdma.c +1 -1
@@ -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 */

Old code

first double-negate, mapping zero to zero, non-zero to one then mask, which does nothing, because CONTROL_CLAIM is 1

Quoted 8 lines

New code:

first mask, yielding 0 or 1
then double-negate, which does nothing

Looks like a bug fix to me. If I'm right, the commit message is wrong, and the double negate is redundant.

Thanks for the review. The double negate is not needed with CONTROL_CLAIM which is 1, but is needed if the bit is in another position.

Regards,
Bin

#7 lore raw
Quoted 43 lines · Bin Meng

Hi Markus,

Quoted 40 lines · Markus Armbruster
Quoted 18 lines · Bin Meng

GCC seems to be strict about processing pattern like "!!for & bar". When 'bar' is not 0 or 1, it complains with -Werror=parentheses:

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

Add a () around "foo && bar", which also improves code readability.

---

 hw/dma/sifive_pdma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/dma/sifive_pdma.c +0 -0
index b4fd40573a..b8ec7621f3 100644

hw/dma/sifive_pdma.c static void sifive_pdma_write(void *opaque, hwaddr offset, · line 249

Quoted 8 lines
hw/dma/sifive_pdma.c +1 -1
@@ -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 */

Old code

first double-negate, mapping zero to zero, non-zero to one then mask, which does nothing, because CONTROL_CLAIM is 1

New code:

first mask, yielding 0 or 1
then double-negate, which does nothing

Quoted 7 lines
Quoted 3 lines

Looks like a bug fix to me. If I'm right, the commit message is wrong, and the double negate is redundant.

Thanks for the review. The double negate is not needed with CONTROL_CLAIM which is 1, but is needed if the bit is in another position.

It's not needed even then: conversion from integer type to bool takes care of it. It's not wrong, though.

However, the commit message does look wrong to me.