Skip to contentSkip to conversation

hw/i3c: Trace fixes and readability improvements from review feedback

PATCH Superseded Jamin Lin 2 participants 10 messages Mar 2 – Mar 3

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jamin Lin

author
#0 lore raw

cover hw/i3c: Trace fixes and readability improvements from review feedback

This small series applies a few follow-up cleanups and bug fixes to the I3C subsystem based on recent review feedback.

The series is based on the aspeed-next branch from:
https://github.com/legoater/qemu.git

v1:
1. Improve trace correctness/determinism by ensuring num_sent is always initialized and reported properly.
2. Replace a couple of less readable expressions with clearer helpers/logic, without changing behavior.

Jamin Lin (4):
hw/i3c/dw-i3c: Use ROUND_UP() for RX buffer allocation alignment hw/i3c/mock-i3c-target: Set num_sent in TX callback to fix trace
reporting
hw/i3c/core: Initialize num_sent in i3c_send_byte() hw/i3c/mock-i3c-target: Simplify GETMRL byte extraction logic

 hw/i3c/core.c            |  2 +-
 hw/i3c/dw-i3c.c          |  2 +-
 hw/i3c/mock-i3c-target.c | 10 +++++++---
 3 files changed, 9 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.0
author
#6 lore raw

PATCH 3/4 hw/i3c/core: Initialize num_sent in i3c_send_byte()

i3c_send_byte() declared num_sent without initializing it before passing its address to i3c_send().

Although i3c_send_byte() itself ignores num_sent after the call, i3c_send() forwards it to trace_i3c_send(). If the target send callback does not set *num_sent, the trace may print an uninitialized value, leading to misleading or garbage output.

Example concern from review:
trace_i3c_send(*num_sent, num_to_send, ret == 0);

If *num_sent is not written by the callback, this trace can report an incorrect number of transmitted bytes.

Initialize num_sent to 0 to ensure deterministic and predictable trace output, even if the callback fails to update it.

---
 hw/i3c/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i3c/core.c +1 -1
index 0a266e00a2..168526003d 100644
@@ -325,7 +325,7 @@ int i3c_send_byte(I3CBus *bus, uint8_t data)
      * Ignored, the caller can determine how many were sent based on if this was
      * ACKed/NACKed.
      */
-    uint32_t num_sent;
+    uint32_t num_sent = 0;
     return i3c_send(bus, &data, 1, &num_sent);
 }
 
Signature · 1 line
2.43.0
author
#8 lore raw

PATCH 4/4 hw/i3c/mock-i3c-target: Simplify GETMRL byte extraction logic

In reply to Jamin Lin

The GETMRL handling logic extracted MSB/LSB bytes from s->cfg.buf_size using a mask-and-shift expression:

(buf_size & (0xff00 >> (offset * 8))) >>
(8 - (offset * 8))

While functionally correct, the expression is difficult to read and obscures the intent, which is simply to return a 16-bit value in MSB-first order.

Replace the mask/shift formula with explicit MSB/LSB extraction:

offset == 0 -> buf_size >> 8
offset == 1 -> buf_size & 0xff

This makes the code clearer and easier to review without altering behavior or data ordering.

No functional change.

---
 hw/i3c/mock-i3c-target.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
hw/i3c/mock-i3c-target.c +6 -3
index 5c913ee49b..845c173427 100644
@@ -145,9 +145,12 @@ static int mock_i3c_target_handle_ccc_read(I3CTarget *i3c, uint8_t *data,
             if (s->ccc_byte_offset >= 2) {
                 break;
             }
-            data[s->ccc_byte_offset] = (s->cfg.buf_size &
-                                        (0xff00 >> (s->ccc_byte_offset * 8))) >>
-                                        (8 - (s->ccc_byte_offset * 8));
+            if (s->ccc_byte_offset == 0) {
+                data[s->ccc_byte_offset] = (uint8_t)(s->cfg.buf_size >> 8);
+            } else {
+                data[s->ccc_byte_offset] = (uint8_t)s->cfg.buf_size;
+            }
+
             s->ccc_byte_offset++;
             *num_read = num_to_read;
         }
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH 2/4 hw/i3c/mock-i3c-target: Set num_sent in TX callback to fix trace reporting

In reply to Jamin Lin

mock_i3c_target_tx() did not update *num_sent before returning.

Although some callers may not directly use this value, i3c_send() passes num_sent to trace_i3c_send(). If the target TX callback does not initialize *num_sent, the trace output may report an incorrect or uninitialized value, leading to confusing debugging information.

For example, the following trace was observed:

mock_i3c_target_tx I3C mock target write 0x12
i3c_send I3C send 0/1 bytes, ack=1 (expected 1/1 bytes)

This happens because *num_sent was never set by the TX callback.

Fix this by setting:

  *num_sent = to_write;

so that the actual number of transmitted bytes is correctly propagated back to i3c_send() and reflected in trace output.

---
 hw/i3c/mock-i3c-target.c | 1 +
 1 file changed, 1 insertion(+)
hw/i3c/mock-i3c-target.c +1 -0
index 875cd7c7d0..5c913ee49b 100644
@@ -86,6 +86,7 @@ static int mock_i3c_target_tx(I3CTarget *i3c, const uint8_t *data,
         s->buf[s->p_buf] = data[i];
         s->p_buf++;
     }
+    *num_sent = to_write;
     return ret;
 }
 
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/4 hw/i3c/dw-i3c: Use ROUND_UP() for RX buffer allocation alignment

In reply to Jamin Lin

The RX temporary buffer allocation manually aligned the size using:

    num + (4 - (num & 0x03))

Replace this with ROUND_UP(num, 4) for better readability and consistency with common QEMU coding style.

No functional change.

---
 hw/i3c/dw-i3c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i3c/dw-i3c.c +1 -1
index d742458129..3d8b95a14c 100644
@@ -1282,7 +1282,7 @@ static uint16_t dw_i3c_rx(DWI3C *s, uint16_t num, bool is_i2c)
      * Allocate a temporary buffer to read data from the target.
      * Zero it and word-align it as well in case we're reading unaligned data.
      */
-    g_autofree uint8_t *data = g_new0(uint8_t, num + (4 - (num & 0x03)));
+    g_autofree uint8_t *data = g_new0(uint8_t, ROUND_UP(num, 4));
     uint32_t *data32 = (uint32_t *)data;
     /*
      * 32-bits since the I3C API wants a 32-bit number, even though the
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v1 2/4] hw/i3c/mock-i3c-target: Set num_sent in TX callback to fix trace reporting

In reply to Jamin Lin
Quoted 31 lines · Jamin Lin

mock_i3c_target_tx() did not update *num_sent before returning.

Although some callers may not directly use this value, i3c_send() passes num_sent to trace_i3c_send(). If the target TX callback does not initialize *num_sent, the trace output may report an incorrect or uninitialized value, leading to confusing debugging information.

For example, the following trace was observed:

mock_i3c_target_tx I3C mock target write 0x12
i3c_send I3C send 0/1 bytes, ack=1 (expected 1/1 bytes)

This happens because *num_sent was never set by the TX callback.

Fix this by setting:

  *num_sent = to_write;

so that the actual number of transmitted bytes is correctly propagated back to i3c_send() and reflected in trace output.

---
 hw/i3c/mock-i3c-target.c | 1 +
 1 file changed, 1 insertion(+)
hw/i3c/mock-i3c-target.c +0 -0
index 875cd7c7d0..5c913ee49b 100644

hw/i3c/mock-i3c-target.c static int mock_i3c_target_tx(I3CTarget *i3c, const uint8_t *data, · line 89

Quoted 4 lines
hw/i3c/mock-i3c-target.c +1 -0
@@ -86,6 +86,7 @@ static int mock_i3c_target_tx(I3CTarget *i3c, const uint8_t *data,
         s->buf[s->p_buf] = data[i];
         s->p_buf++;
     }
+    *num_sent = to_write;

Thanks for fixing this... could you also set *num_sent to 1 in the ibi return path earlier in the function (snippet below), where it seems to consume exactly 1 byte of data
...

<snip>

    if (s->cfg.ibi_magic && num_to_send == 1 && s->cfg.ibi_magic == *data) {
        mock_i3c_target_ibi_timer_start(s);
+	*num_sent = 1;
        return 0;
    }

</snip>

hw/i3c/mock-i3c-target.c static int mock_i3c_target_tx(I3CTarget *i3c, const uint8_t *data, · line 92

Quoted 3 lines
hw/i3c/mock-i3c-target.c +0 -0
     return ret;
 }
 

Jithu

Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH v1 1/4] hw/i3c/dw-i3c: Use ROUND_UP() for RX buffer allocation alignment

In reply to Jamin Lin
Quoted 19 lines · Jamin Lin

The RX temporary buffer allocation manually aligned the size using:

    num + (4 - (num & 0x03))

Replace this with ROUND_UP(num, 4) for better readability and consistency with common QEMU coding style.

No functional change.

---
 hw/i3c/dw-i3c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i3c/dw-i3c.c +0 -0
index d742458129..3d8b95a14c 100644

hw/i3c/dw-i3c.c static uint16_t dw_i3c_rx(DWI3C *s, uint16_t num, bool is_i2c) · line 1288

Quoted 8 lines
hw/i3c/dw-i3c.c +1 -1
@@ -1282,7 +1282,7 @@ static uint16_t dw_i3c_rx(DWI3C *s, uint16_t num, bool is_i2c)
      * Allocate a temporary buffer to read data from the target.
      * Zero it and word-align it as well in case we're reading unaligned data.
      */
-    g_autofree uint8_t *data = g_new0(uint8_t, num + (4 - (num & 0x03)));
+    g_autofree uint8_t *data = g_new0(uint8_t, ROUND_UP(num, 4));
     uint32_t *data32 = (uint32_t *)data;
     /*
      * 32-bits since the I3C API wants a 32-bit number, even though the

Looks good to me

Thanks
Jithu

Reviewed-by on v1 3/4
#7 lore raw

Re: [PATCH v1 3/4] hw/i3c/core: Initialize num_sent in i3c_send_byte()

In reply to Jamin Lin
Quoted 27 lines · Jamin Lin

i3c_send_byte() declared num_sent without initializing it before passing its address to i3c_send().

Although i3c_send_byte() itself ignores num_sent after the call, i3c_send() forwards it to trace_i3c_send(). If the target send callback does not set *num_sent, the trace may print an uninitialized value, leading to misleading or garbage output.

Example concern from review:
trace_i3c_send(*num_sent, num_to_send, ret == 0);

If *num_sent is not written by the callback, this trace can report an incorrect number of transmitted bytes.

Initialize num_sent to 0 to ensure deterministic and predictable trace output, even if the callback fails to update it.

---
 hw/i3c/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i3c/core.c +0 -0
index 0a266e00a2..168526003d 100644

hw/i3c/core.c int i3c_send_byte(I3CBus *bus, uint8_t data) · line 331

Quoted 8 lines
hw/i3c/core.c +1 -1
@@ -325,7 +325,7 @@ int i3c_send_byte(I3CBus *bus, uint8_t data)
      * Ignored, the caller can determine how many were sent based on if this was
      * ACKed/NACKed.
      */
-    uint32_t num_sent;
+    uint32_t num_sent = 0;
     return i3c_send(bus, &data, 1, &num_sent);
 }
 

Looks good to me

Thanks
Jithu

Reviewed-by on v1 4/4
#9 lore raw

Re: [PATCH v1 4/4] hw/i3c/mock-i3c-target: Simplify GETMRL byte extraction logic

In reply to Jamin Lin
Quoted 35 lines · Jamin Lin

The GETMRL handling logic extracted MSB/LSB bytes from s->cfg.buf_size using a mask-and-shift expression:

(buf_size & (0xff00 >> (offset * 8))) >>
(8 - (offset * 8))

While functionally correct, the expression is difficult to read and obscures the intent, which is simply to return a 16-bit value in MSB-first order.

Replace the mask/shift formula with explicit MSB/LSB extraction:

offset == 0 -> buf_size >> 8
offset == 1 -> buf_size & 0xff

This makes the code clearer and easier to review without altering behavior or data ordering.

No functional change.

---
 hw/i3c/mock-i3c-target.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
hw/i3c/mock-i3c-target.c +0 -2
index 5c913ee49b..845c173427 100644
@@ -145,9 +145,12 @@ static int mock_i3c_target_handle_ccc_read(I3CTarget *i3c, uint8_t *data,
             if (s->ccc_byte_offset >= 2) {
                 break;
             }
-            data[s->ccc_byte_offset] = (s->cfg.buf_size &
-                                        (0xff00 >> (s->ccc_byte_offset * 8))) >>

hw/i3c/mock-i3c-target.c static int mock_i3c_target_handle_ccc_read(I3CTarget *i3c, uint8_t *data, · line 156

Quoted 10 lines
hw/i3c/mock-i3c-target.c +6 -1
-                                        (8 - (s->ccc_byte_offset * 8));
+            if (s->ccc_byte_offset == 0) {
+                data[s->ccc_byte_offset] = (uint8_t)(s->cfg.buf_size >> 8);
+            } else {
+                data[s->ccc_byte_offset] = (uint8_t)s->cfg.buf_size;
+            }
+
             s->ccc_byte_offset++;
             *num_read = num_to_read;
         }

Looks way more readable

Thanks
Jithu

author
#5 lore raw

RE: [PATCH v1 2/4] hw/i3c/mock-i3c-target: Set num_sent in TX callback to fix trace reporting

In reply to Jithu Joseph

Hi Jithu Joseph

Quoted 54 lines

Subject: Re: [PATCH v1 2/4] hw/i3c/mock-i3c-target: Set num_sent in TX callback to fix trace reporting

hw/i3c/mock-i3c-target.c static int mock_i3c_target_tx(I3CTarget *i3c, const

Quoted 31 lines · Jamin Lin

mock_i3c_target_tx() did not update *num_sent before returning.

Although some callers may not directly use this value, i3c_send() passes num_sent to trace_i3c_send(). If the target TX callback does not initialize *num_sent, the trace output may report an incorrect or uninitialized value, leading to confusing debugging information.

For example, the following trace was observed:

mock_i3c_target_tx I3C mock target write 0x12
i3c_send I3C send 0/1 bytes, ack=1 (expected 1/1 bytes)

This happens because *num_sent was never set by the TX callback.

Fix this by setting:

  *num_sent = to_write;

so that the actual number of transmitted bytes is correctly propagated back to i3c_send() and reflected in trace output.

---
 hw/i3c/mock-i3c-target.c | 1 +
 1 file changed, 1 insertion(+)
hw/i3c/mock-i3c-target.c index renamed from hw/i3c/mock-i3c-target.c +0 -0

875cd7c7d0..5c913ee49b 100644

hw/i3c/mock-i3c-target.c +0 -0
@@ -86,6 +86,7 @@ static int mock_i3c_target_tx(I3CTarget *i3c, const

uint8_t *data,

hw/i3c/mock-i3c-target.c static int mock_i3c_target_tx(I3CTarget *i3c, const uint8_t *data, · line 89

Quoted 4 lines
hw/i3c/mock-i3c-target.c +1 -0
         s->buf[s->p_buf] = data[i];
         s->p_buf++;
     }
+    *num_sent = to_write;

Thanks for fixing this... could you also set *num_sent to 1 in the ibi return path earlier in the function (snippet below), where it seems to consume exactly 1 byte of data ...

<snip>

    if (s->cfg.ibi_magic && num_to_send == 1 && s->cfg.ibi_magic == *data)
{
        mock_i3c_target_ibi_timer_start(s);
(quoted diff) +1 -0
+	*num_sent = 1;
        return 0;
    }
Quoted 2 lines

</snip>

Thanks for the review and suggestion.
Will resend in v2.

Thanks,
Jamin

Quoted 5 lines

hw/i3c/mock-i3c-target.c static int mock_i3c_target_tx(I3CTarget *i3c, const uint8_t *data, · line 92

Quoted 3 lines
hw/i3c/mock-i3c-target.c +0 -0
     return ret;
 }
 

Jithu