[PATCH v3] hw/net: cadence: Honor subword lanes in the GEM registers

Bin Meng posted 1 patch 1 week, 6 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261109143410.3279305-1-bin.meng@processmission.com
Maintainers: "Edgar E. Iglesias" <edgar.iglesias@gmail.com>, Alistair Francis <alistair@alistair23.me>, Peter Maydell <peter.maydell@linaro.org>, Jason Wang <jasowangio@gmail.com>
hw/net/cadence_gem.c | 55 +++++++++++++++++++++++++++-----------------
1 file changed, 34 insertions(+), 21 deletions(-)
[PATCH v3] hw/net: cadence: Honor subword lanes in the GEM registers
Posted by Bin Meng 1 week, 6 days ago
The GEM MMIO callbacks ignored the low address bits and access size.
Byte reads therefore always returned lane zero, causing guest driver
that reads the station address byte by byte to turn four unique MAC
addresses into the same value. Partial writes had the corresponding
lane-selection problem.

Extract the requested little-endian read lane and merge partial writes
with the current register value. Preserve the existing read-clear and
write-clear behavior and dynamic queue pointer readback.

With this change, reset the TX queue pointers only when a write covers
TX-enable and leaves it clear, and only start a PHY operation when a
write covers the OP/ST fields in the PHY maintenance register.

Fixes: e9f186e514a7 ("cadence_gem: initial version of device model")
Signed-off-by: Bin Meng <bin.meng@processmission.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>

---

Changes in v3:
- Describe the TX-enable check and PHY operation trigger

Changes in v2:
- drop the gem_get_register() helper

 hw/net/cadence_gem.c | 55 +++++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 21 deletions(-)

diff --git a/hw/net/cadence_gem.c b/hw/net/cadence_gem.c
index 9a94b3b8ca..7456f0e18e 100644
--- a/hw/net/cadence_gem.c
+++ b/hw/net/cadence_gem.c
@@ -1603,8 +1603,11 @@ static uint64_t gem_read(void *opaque, hwaddr offset, unsigned size)
 {
     CadenceGEMState *s;
     uint32_t retval;
+    unsigned shift;
+
     s = opaque;
 
+    shift = (offset & 3) * 8;
     offset >>= 2;
     retval = s->regs[offset];
 
@@ -1638,7 +1641,7 @@ static uint64_t gem_read(void *opaque, hwaddr offset, unsigned size)
 
     DB_PRINT("0x%08x\n", retval);
     gem_update_int_status(s);
-    return retval;
+    return extract32(retval, shift, size * 8);
 }
 
 /*
@@ -1649,35 +1652,43 @@ static void gem_write(void *opaque, hwaddr offset, uint64_t val,
         unsigned size)
 {
     CadenceGEMState *s = (CadenceGEMState *)opaque;
-    uint32_t readonly;
+    uint32_t access_mask;
+    uint32_t writable_mask;
+    uint32_t write_val;
+    unsigned shift;
     int i;
 
     DB_PRINT("offset: 0x%04x write: 0x%08x ", (unsigned)offset, (unsigned)val);
+    shift = (offset & 3) * 8;
+    access_mask = MAKE_64BIT_MASK(shift, size * 8);
+    write_val = (uint32_t)val << shift;
     offset >>= 2;
 
     /* Squash bits which are read only in write value */
-    val &= ~(s->regs_ro[offset]);
-    /* Preserve (only) bits which are read only and wtc in register */
-    readonly = s->regs[offset] & (s->regs_ro[offset] | s->regs_w1c[offset]);
+    write_val &= ~(s->regs_ro[offset]);
 
-    /* Copy register write to backing store */
-    s->regs[offset] = (val & ~s->regs_w1c[offset]) | readonly;
+    /* Copy writable bits in the accessed lanes to the backing store */
+    writable_mask = access_mask &
+                    ~(s->regs_ro[offset] | s->regs_w1c[offset]);
+    s->regs[offset] = (s->regs[offset] & ~writable_mask) |
+                      (write_val & writable_mask);
 
     /* do w1c */
-    s->regs[offset] &= ~(s->regs_w1c[offset] & val);
+    s->regs[offset] &= ~(s->regs_w1c[offset] & write_val);
 
     /* Handle register write side effects */
     switch (offset) {
     case R_NWCTRL:
-        if (FIELD_EX32(val, NWCTRL, ENABLE_RECEIVE)) {
+        if (FIELD_EX32(write_val, NWCTRL, ENABLE_RECEIVE)) {
             for (i = 0; i < s->num_priority_queues; ++i) {
                 gem_get_rx_desc(s, i);
             }
         }
-        if (FIELD_EX32(val, NWCTRL, TRANSMIT_START)) {
+        if (FIELD_EX32(write_val, NWCTRL, TRANSMIT_START)) {
             gem_transmit(s);
         }
-        if (!(FIELD_EX32(val, NWCTRL, ENABLE_TRANSMIT))) {
+        if ((access_mask & R_NWCTRL_ENABLE_TRANSMIT_MASK) &&
+            !(FIELD_EX32(s->regs[offset], NWCTRL, ENABLE_TRANSMIT))) {
             /* Reset to start of Q when transmit disabled. */
             for (i = 0; i < s->num_priority_queues; i++) {
                 s->tx_desc_addr[i] = gem_get_tx_queue_base_addr(s, i);
@@ -1692,37 +1703,37 @@ static void gem_write(void *opaque, hwaddr offset, uint64_t val,
         gem_update_int_status(s);
         break;
     case R_RXQBASE:
-        s->rx_desc_addr[0] = val;
+        s->rx_desc_addr[0] = s->regs[offset];
         break;
     case R_RECEIVE_Q1_PTR ... R_RECEIVE_Q7_PTR:
-        s->rx_desc_addr[offset - R_RECEIVE_Q1_PTR + 1] = val;
+        s->rx_desc_addr[offset - R_RECEIVE_Q1_PTR + 1] = s->regs[offset];
         break;
     case R_TXQBASE:
-        s->tx_desc_addr[0] = val;
+        s->tx_desc_addr[0] = s->regs[offset];
         break;
     case R_TRANSMIT_Q1_PTR ... R_TRANSMIT_Q7_PTR:
-        s->tx_desc_addr[offset - R_TRANSMIT_Q1_PTR + 1] = val;
+        s->tx_desc_addr[offset - R_TRANSMIT_Q1_PTR + 1] = s->regs[offset];
         break;
     case R_RXSTATUS:
         gem_update_int_status(s);
         break;
     case R_IER:
-        s->regs[R_IMR] &= ~val;
+        s->regs[R_IMR] &= ~write_val;
         gem_update_int_status(s);
         break;
     case R_JUMBO_MAX_LEN:
-        s->regs[R_JUMBO_MAX_LEN] = val & MAX_JUMBO_FRAME_SIZE_MASK;
+        s->regs[R_JUMBO_MAX_LEN] &= MAX_JUMBO_FRAME_SIZE_MASK;
         break;
     case R_INT_Q1_ENABLE ... R_INT_Q7_ENABLE:
-        s->regs[R_INT_Q1_MASK + offset - R_INT_Q1_ENABLE] &= ~val;
+        s->regs[R_INT_Q1_MASK + offset - R_INT_Q1_ENABLE] &= ~write_val;
         gem_update_int_status(s);
         break;
     case R_IDR:
-        s->regs[R_IMR] |= val;
+        s->regs[R_IMR] |= write_val;
         gem_update_int_status(s);
         break;
     case R_INT_Q1_DISABLE ... R_INT_Q7_DISABLE:
-        s->regs[R_INT_Q1_MASK + offset - R_INT_Q1_DISABLE] |= val;
+        s->regs[R_INT_Q1_MASK + offset - R_INT_Q1_DISABLE] |= write_val;
         gem_update_int_status(s);
         break;
     case R_SPADDR1LO:
@@ -1738,7 +1749,9 @@ static void gem_write(void *opaque, hwaddr offset, uint64_t val,
         s->sar_active[(offset - R_SPADDR1HI) / 2] = true;
         break;
     case R_PHYMNTNC:
-        gem_handle_phy_access(s);
+        if (access_mask & (R_PHYMNTNC_OP_MASK | R_PHYMNTNC_ST_MASK)) {
+            gem_handle_phy_access(s);
+        }
         break;
     }
 
---
base-commit: 71f25c75417d31c214ba2cd26b273c114fc99987
branch: cadence

-- 
2.53.0


Re: [PATCH v3] hw/net: cadence: Honor subword lanes in the GEM registers
Posted by Peter Maydell 1 week, 2 days ago
On Mon, 9 Nov 2026 at 14:34, Bin Meng <bin.meng@processmission.com> wrote:
>
> The GEM MMIO callbacks ignored the low address bits and access size.
> Byte reads therefore always returned lane zero, causing guest driver
> that reads the station address byte by byte to turn four unique MAC
> addresses into the same value. Partial writes had the corresponding
> lane-selection problem.
>
> Extract the requested little-endian read lane and merge partial writes
> with the current register value. Preserve the existing read-clear and
> write-clear behavior and dynamic queue pointer readback.
>
> With this change, reset the TX queue pointers only when a write covers
> TX-enable and leaves it clear, and only start a PHY operation when a
> write covers the OP/ST fields in the PHY maintenance register.
>
> Fixes: e9f186e514a7 ("cadence_gem: initial version of device model")
> Signed-off-by: Bin Meng <bin.meng@processmission.com>
> Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>

This device is used by both arm and riscv machines. Alistair,
do you plan to take it via the riscv queue?

thanks
-- PMM
Re: [PATCH v3] hw/net: cadence: Honor subword lanes in the GEM registers
Posted by Alistair 1 week ago
On Fri, 2026-11-13 at 10:00 +0000, Peter Maydell wrote:
> On Mon, 9 Nov 2026 at 14:34, Bin Meng <bin.meng@processmission.com>
> wrote:
> > 
> > The GEM MMIO callbacks ignored the low address bits and access
> > size.
> > Byte reads therefore always returned lane zero, causing guest
> > driver
> > that reads the station address byte by byte to turn four unique MAC
> > addresses into the same value. Partial writes had the corresponding
> > lane-selection problem.
> > 
> > Extract the requested little-endian read lane and merge partial
> > writes
> > with the current register value. Preserve the existing read-clear
> > and
> > write-clear behavior and dynamic queue pointer readback.
> > 
> > With this change, reset the TX queue pointers only when a write
> > covers
> > TX-enable and leaves it clear, and only start a PHY operation when
> > a
> > write covers the OP/ST fields in the PHY maintenance register.
> > 
> > Fixes: e9f186e514a7 ("cadence_gem: initial version of device
> > model")
> > Signed-off-by: Bin Meng <bin.meng@processmission.com>
> > Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
> 
> This device is used by both arm and riscv machines. Alistair,
> do you plan to take it via the riscv queue?

I just sent a PR for 11.2, so I'm happy for this to go via the ARM tree

Alistair

> 
> thanks
> -- PMM
Re: [PATCH v3] hw/net: cadence: Honor subword lanes in the GEM registers
Posted by Peter Maydell 6 days, 18 hours ago
On Mon, 16 Nov 2026 at 04:11, Alistair <alistair@alistair23.me> wrote:
>
> On Fri, 2026-11-13 at 10:00 +0000, Peter Maydell wrote:
> > On Mon, 9 Nov 2026 at 14:34, Bin Meng <bin.meng@processmission.com>
> > wrote:
> > >
> > > The GEM MMIO callbacks ignored the low address bits and access
> > > size.
> > > Byte reads therefore always returned lane zero, causing guest
> > > driver
> > > that reads the station address byte by byte to turn four unique MAC
> > > addresses into the same value. Partial writes had the corresponding
> > > lane-selection problem.
> > >
> > > Extract the requested little-endian read lane and merge partial
> > > writes
> > > with the current register value. Preserve the existing read-clear
> > > and
> > > write-clear behavior and dynamic queue pointer readback.
> > >
> > > With this change, reset the TX queue pointers only when a write
> > > covers
> > > TX-enable and leaves it clear, and only start a PHY operation when
> > > a
> > > write covers the OP/ST fields in the PHY maintenance register.
> > >
> > > Fixes: e9f186e514a7 ("cadence_gem: initial version of device
> > > model")
> > > Signed-off-by: Bin Meng <bin.meng@processmission.com>
> > > Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
> >
> > This device is used by both arm and riscv machines. Alistair,
> > do you plan to take it via the riscv queue?
>
> I just sent a PR for 11.2, so I'm happy for this to go via the ARM tree

OK, I've applied it to target-arm.next.

-- PMM