Skip to contentSkip to conversation

ibex_uart: fix XOR-as-pow

PATCH In review Paolo Bonzini 5 participants 6 messages Jun 23, 2020 – Jun 24, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ibex_uart: fix XOR-as-pow

The xor-as-pow warning in clang actually detected a genuine bug. Fix it.

---
 hw/char/ibex_uart.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/char/ibex_uart.c +1 -1
index 3e0dd9968e..45cd724998 100644
@@ -331,7 +331,7 @@ static void ibex_uart_write(void *opaque, hwaddr addr,
         if (value & UART_CTRL_NCO) {
             uint64_t baud = ((value & UART_CTRL_NCO) >> 16);
             baud *= 1000;
-            baud /= 2 ^ 20;
+            baud >>= 20;
 
             s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
         }
Signature · 1 line
2.26.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Paolo Bonzini

The xor-as-pow warning in clang actually detected a genuine bug. Fix it.

---
  hw/char/ibex_uart.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/char/ibex_uart.c +0 -0
index 3e0dd9968e..45cd724998 100644

hw/char/ibex_uart.c static void ibex_uart_write(void *opaque, hwaddr addr, · line 334

Quoted 5 lines
hw/char/ibex_uart.c +1 -1
@@ -331,7 +331,7 @@ static void ibex_uart_write(void *opaque, hwaddr addr,
          if (value & UART_CTRL_NCO) {
              uint64_t baud = ((value & UART_CTRL_NCO) >> 16);
              baud *= 1000;
-            baud /= 2 ^ 20;
+            baud >>= 20;

Dividing by 1M instead of 22 seems much more logical, indeed :) It's odd that we are scaling up by 1000, down by 1024*1024, then

hw/char/ibex_uart.c static void ibex_uart_write(void *opaque, hwaddr addr, · line 336

Quoted 2 lines
hw/char/ibex_uart.c +0 -0
              s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;

inverting and scaling by 10. I don't know if there is a more succinct way to write the intended transformation.

hw/char/ibex_uart.c static void ibex_uart_write(void *opaque, hwaddr addr, · line 336

Quoted 1 line
hw/char/ibex_uart.c +0 -0
          }

But even without reading the hardware manual to see if the resulting scaling is correct, the typo fix is obvious enough to give:

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#2 lore raw
Quoted 7 lines · Eric Blake
Quoted 5 lines
             uint64_t baud = ((value & UART_CTRL_NCO) >> 16);

              baud *= 1000;
-            baud /= 2 ^ 20;
+            baud >>= 20;

Dividing by 1M instead of 22 seems much more logical, indeed :)

Based on the spec, the "* 1000" is the clock, in other words this is a fixed point value relative to the clock:

f_baud = NCO * f_clock / 2^20

The example in the spec (https://docs.opentitan.org/hw/ip/uart/doc/) has f_clock = 50 MHz, while here it's only 1 kHz. And the register is only 16 bit, so the above would only allow a baud rate up to 62 (65535 * 1000 / 2^20).

Should the clock be a property of the device instead?

Thanks,

Paolo

Quoted 3 lines

It's odd that we are scaling up by 1000, down by 1024*1024, then

Quoted 1 line

                s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;

#3 lore raw
Quoted 12 lines · Paolo Bonzini
Quoted 7 lines · Eric Blake
Quoted 5 lines
             uint64_t baud = ((value & UART_CTRL_NCO) >> 16);

              baud *= 1000;
-            baud /= 2 ^ 20;
+            baud >>= 20;

Dividing by 1M instead of 22 seems much more logical, indeed :)

Based on the spec, the "* 1000" is the clock, in other words this is a fixed point value relative to the clock:

Quoted 8 lines

f_baud = NCO * f_clock / 2^20

The example in the spec (https://docs.opentitan.org/hw/ip/uart/doc/) has f_clock = 50 MHz, while here it's only 1 kHz. And the register is only 16 bit, so the above would only allow a baud rate up to 62 (65535 * 1000 / 2^20).

Should the clock be a property of the device instead?

Ideally the device should use qdev_get_clock_in(), (see docs/devel/clocks.rst):

  static void ibex_uart_init(Object *obj)
  {
     ...
     s->f_clk = qdev_get_clock_in(DEVICE(obj), "f_clock");
     ...

Then in ibex_uart_write():

     uint64_t baud = ((value & UART_CTRL_NCO) >> 16);
     baud *= clock_get_hz(dev->f_clk));
     baud >>= 20;

Devices not using the QDEV_CLOCK API use QDEV properties.

Quoted 10 lines

Thanks,

Paolo

Quoted 3 lines

It's odd that we are scaling up by 1000, down by 1024*1024, then

Quoted 1 line

                s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;

#4 lore raw
In reply to Paolo Bonzini

cc'ing Alistair as listed maintainer for the device...

Quoted 24 lines · Paolo Bonzini

The xor-as-pow warning in clang actually detected a genuine bug. Fix it.

---
 hw/char/ibex_uart.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/char/ibex_uart.c +1 -2
index 3e0dd9968e..45cd724998 100644
@@ -331,7 +331,7 @@ static void ibex_uart_write(void *opaque, hwaddr addr,
         if (value & UART_CTRL_NCO) {
             uint64_t baud = ((value & UART_CTRL_NCO) >> 16);
             baud *= 1000;
-            baud /= 2 ^ 20;
+            baud >>= 20;
 
             s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
         }
--

2.26.2

Reviewed-by on v1 1/1
#5 lore raw
In reply to Paolo Bonzini
Quoted 14 lines · Paolo Bonzini

The xor-as-pow warning in clang actually detected a genuine bug. Fix it.

---
 hw/char/ibex_uart.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/char/ibex_uart.c +0 -0
index 3e0dd9968e..45cd724998 100644

hw/char/ibex_uart.c static void ibex_uart_write(void *opaque, hwaddr addr, · line 334

Quoted 5 lines
hw/char/ibex_uart.c +1 -1
@@ -331,7 +331,7 @@ static void ibex_uart_write(void *opaque, hwaddr addr,
         if (value & UART_CTRL_NCO) {
             uint64_t baud = ((value & UART_CTRL_NCO) >> 16);
             baud *= 1000;
-            baud /= 2 ^ 20;
+            baud >>= 20;

Whoops, that is clearly wrong. Thanks for catching this.

Alistair

Quoted 7 lines
hw/char/ibex_uart.c +0 -1
             s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
         }
--

2.26.2