Skip to contentSkip to conversation

Add npcm7xx emc model

PATCH Superseded dje--- via 3 participants 9 messages Feb 2, 2021 – Feb 9, 2021 Arm/Amlogic Meson SoC support

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

dje--- via

author
#0 lore raw

v2 cover Add npcm7xx emc model

This is a 10/100 ethernet device that has several features. Only the ones needed by the Linux driver have been implemented. See npcm7xx_emc.c for a list of unimplemented features.

Doug Evans (3):
hw/net: Add npcm7xx emc model
hw/arm: Add npcm7xx emc model
tests/qtests: Add npcm7xx emc model test

 docs/system/arm/nuvoton.rst    |   3 +-
 hw/arm/npcm7xx.c               |  50 +-
 hw/net/meson.build             |   1 +
 hw/net/npcm7xx_emc.c           | 852 +++++++++++++++++++++++++++++++++
 hw/net/trace-events            |  17 +
 include/hw/arm/npcm7xx.h       |   2 +
 include/hw/net/npcm7xx_emc.h   | 286 +++++++++++
 tests/qtest/meson.build        |   1 +
 tests/qtest/npcm7xx_emc-test.c | 793 ++++++++++++++++++++++++++++++
 9 files changed, 2002 insertions(+), 3 deletions(-)
 create mode 100644 hw/net/npcm7xx_emc.c
 create mode 100644 include/hw/net/npcm7xx_emc.h
 create mode 100644 tests/qtest/npcm7xx_emc-test.c
Signature · 1 line
2.30.0.365.g02bc693789-goog
author
#1 lore raw

PATCH v2 1/3 hw/net: Add npcm7xx emc model

This is a 10/100 ethernet device that has several features. Only the ones needed by the Linux driver have been implemented. See npcm7xx_emc.c for a list of unimplemented features.

---
 hw/net/meson.build           |   1 +
 hw/net/npcm7xx_emc.c         | 852 +++++++++++++++++++++++++++++++++++
 hw/net/trace-events          |  17 +
 include/hw/net/npcm7xx_emc.h | 286 ++++++++++++
 4 files changed, 1156 insertions(+)
 create mode 100644 hw/net/npcm7xx_emc.c
 create mode 100644 include/hw/net/npcm7xx_emc.h
hw/net/meson.build +1 -0
index 4a7051b54a..af0749c42b 100644
@@ -35,6 +35,7 @@ softmmu_ss.add(when: 'CONFIG_I82596_COMMON', if_true: files('i82596.c'))
 softmmu_ss.add(when: 'CONFIG_SUNHME', if_true: files('sunhme.c'))
 softmmu_ss.add(when: 'CONFIG_FTGMAC100', if_true: files('ftgmac100.c'))
 softmmu_ss.add(when: 'CONFIG_SUNGEM', if_true: files('sungem.c'))
+softmmu_ss.add(when: 'CONFIG_NPCM7XX', if_true: files('npcm7xx_emc.c'))
 
 softmmu_ss.add(when: 'CONFIG_ETRAXFS', if_true: files('etraxfs_eth.c'))
 softmmu_ss.add(when: 'CONFIG_COLDFIRE', if_true: files('mcf_fec.c'))
hw/net/npcm7xx_emc.c new file +852 -0

852 lines not on this page · raw message

hw/net/trace-events +17 -0
index 5db45456d9..baf25ffa7e 100644
@@ -429,3 +429,20 @@ imx_fec_receive_last(int last) "rx frame flags 0x%04x"
 imx_enet_receive(size_t size) "len %zu"
 imx_enet_receive_len(uint64_t addr, int len) "rx_bd 0x%"PRIx64" length %d"
 imx_enet_receive_last(int last) "rx frame flags 0x%04x"
+
+# npcm7xx_emc.c
+npcm7xx_emc_reset(int emc_num) "Resetting emc%d"
+npcm7xx_emc_update_tx_irq(int level) "Setting tx irq to %d"
+npcm7xx_emc_update_rx_irq(int level) "Setting rx irq to %d"
+npcm7xx_emc_set_mista(uint32_t flags) "ORing 0x%x into MISTA"
+npcm7xx_emc_cpu_owned_desc(uint32_t addr) "Can't process cpu-owned descriptor @0x%x"
+npcm7xx_emc_sent_packet(uint32_t len) "Sent %u byte packet"
+npcm7xx_emc_tx_done(uint32_t ctxdsa) "TX done, CTXDSA=0x%x"
+npcm7xx_emc_can_receive(int can_receive) "Can receive: %d"
+npcm7xx_emc_packet_filtered_out(const char* fail_reason) "Packet filtered out: %s"
+npcm7xx_emc_packet_dropped(uint32_t len) "%u byte packet dropped"
+npcm7xx_emc_receiving_packet(uint32_t len) "Receiving %u byte packet"
+npcm7xx_emc_received_packet(uint32_t len) "Received %u byte packet"
+npcm7xx_emc_rx_done(uint32_t crxdsa) "RX done, CRXDSA=0x%x"
+npcm7xx_emc_reg_read(int emc_num, uint32_t result, const char *name, int regno) "emc%d: 0x%x = reg[%s/%d]"
+npcm7xx_emc_reg_write(int emc_num, const char *name, int regno, uint32_t value) "emc%d: reg[%s/%d] = 0x%x"
include/hw/net/npcm7xx_emc.h new file +286 -0

286 lines not on this page · raw message

Signature · 1 line
2.30.0.365.g02bc693789-goog
#2 lore raw

Re: [PATCH v2 1/3] hw/net: Add npcm7xx emc model

Quoted 33 lines · Doug Evans

This is a 10/100 ethernet device that has several features. Only the ones needed by the Linux driver have been implemented. See npcm7xx_emc.c for a list of unimplemented features.

---
 hw/net/meson.build           |   1 +
 hw/net/npcm7xx_emc.c         | 852 +++++++++++++++++++++++++++++++++++
 hw/net/trace-events          |  17 +
 include/hw/net/npcm7xx_emc.h | 286 ++++++++++++
 4 files changed, 1156 insertions(+)
 create mode 100644 hw/net/npcm7xx_emc.c
 create mode 100644 include/hw/net/npcm7xx_emc.h
hw/net/npcm7xx_emc.c new file +16 -0
+static void emc_reset(NPCM7xxEMCState *emc)
+{
+    trace_npcm7xx_emc_reset(emc->emc_num);
+
+    memset(&emc->regs[0], 0, sizeof(emc->regs));
+
+    /* These regs have non-zero reset values. */
+    emc->regs[REG_TXDLSA] = 0xfffffffc;
+    emc->regs[REG_RXDLSA] = 0xfffffffc;
+    emc->regs[REG_MIIDA] = 0x00900000;
+    emc->regs[REG_FFTCR] = 0x0101;
+    emc->regs[REG_DMARFC] = 0x0800;
+    emc->regs[REG_MPCNT] = 0x7fff;
+
+    emc->tx_active = false;
+    emc->rx_active = false;

hw/net/npcm7xx_emc.c line 125

Quoted 10 lines
hw/net/npcm7xx_emc.c new file +10 -0
+
+    qemu_set_irq(emc->tx_irq, 0);
+    qemu_set_irq(emc->rx_irq, 0);
+}
+
+static void npcm7xx_emc_reset(DeviceState *dev)
+{
+    NPCM7xxEMCState *emc = NPCM7XX_EMC(dev);
+    emc_reset(emc);
+}

You can't call qemu_set_irq() from a DeviceState::reset method. Usually it's OK just not to try to set the outbound IRQs and to assume that the device you're connected to has reset to the state where its inbound IRQ line is not asserted. If you really need to set the irq line then you need to switch to 3-phase reset (some of the other npcm7xx devices do this). But I suspect that just moving the qemu_set_irq() calls to emc_soft_reset() would be enough.

Quoted 30 lines
(quoted diff) +28 -0
+
+static void emc_soft_reset(NPCM7xxEMCState *emc)
+{
+    /*
+     * The docs say at least MCMDR.{LBK,OPMOD} bits are not changed during a
+     * soft reset, but does not go into further detail. For now, KISS.
+     */
+    uint32_t mcmdr = emc->regs[REG_MCMDR];
+    emc_reset(emc);
+    emc->regs[REG_MCMDR] =
+        mcmdr & (REG_MCMDR_LBK | REG_MCMDR_OPMOD);
+ }
 
 
+static void emc_try_send_next_packet(NPCM7xxEMCState *emc)
+{
+    uint32_t desc_addr = TX_DESC_NTXDSA(emc->regs[REG_CTXDSA]);
+    NPCM7xxEMCTxDesc tx_desc;
+    if (emc_read_tx_desc(desc_addr, &tx_desc)) {
+        /* Error reading descriptor, already reported. */
+        emc_halt_tx(emc, REG_MISTA_TXBERR);
+        emc_update_tx_irq(emc);
+        return;
+    }
+
+    /* Nothing we can do if we don't own the descriptor. */
+    if (!(tx_desc.flags & TX_DESC_FLAG_OWNER_MASK)) {
+        trace_npcm7xx_emc_cpu_owned_desc(desc_addr);
+        emc_halt_tx(emc, REG_MISTA_TDU);
+        emc_update_tx_irq(emc);

Review

Quoted 10 lines
(quoted diff) +10 -0
+        return;
+     }
+
+    /* Give the descriptor back regardless of what happens. */
+    tx_desc.flags &= ~TX_DESC_FLAG_OWNER_MASK;
+    tx_desc.status_and_length &= 0xffff;
+
+    /* Working buffer for sending out packets. Most packets fit in this. */
+#define TX_BUFFER_SIZE 2048
+    uint8_t tx_send_buffer[TX_BUFFER_SIZE];

Don't put local variable declarations in the middle of functions, please. Coding style says they should be at the start of a block (so, here, the start of the function). It looks like you've got middle-of-function declarations in several places in other functions too, so could you fix them all up please?

hw/net/npcm7xx_emc.c line 355

Quoted 12 lines
hw/net/npcm7xx_emc.c new file +12 -0
+
+    /*
+     * Despite the h/w documentation saying the tx buffer is word aligned,
+     * the linux driver does not word align the buffer. There is value in not
+     * aligning the buffer: See the description of NET_IP_ALIGN in linux
+     * kernel sources.
+     */
+    uint32_t next_buf_addr = tx_desc.txbsa;
+    emc->regs[REG_CTXBSA] = next_buf_addr;
+    uint32_t length = TX_DESC_PKT_LEN(tx_desc.status_and_length);
+    uint8_t *buf = &tx_send_buffer[0];
+    uint8_t *malloced_buf = NULL;

Optional, but you might consider using g_autofree for malloced_buf, which would let the compiler deal with g_free()ing it for you on all the function exit paths.

hw/net/npcm7xx_emc.c line 360

Quoted 5 lines
hw/net/npcm7xx_emc.c new file +5 -0
+
+    if (length > sizeof(tx_send_buffer)) {
+        malloced_buf = g_malloc(length);
+        buf = malloced_buf;
+    }

Otherwise the patch looks OK I think.

thanks
-- PMM

#3 lore raw

Re: [PATCH v2 1/3] hw/net: Add npcm7xx emc model

Quoted 45 lines · Peter Maydell
Quoted 33 lines · Doug Evans

This is a 10/100 ethernet device that has several features. Only the ones needed by the Linux driver have been implemented. See npcm7xx_emc.c for a list of unimplemented features.

---
 hw/net/meson.build           |   1 +
 hw/net/npcm7xx_emc.c         | 852 +++++++++++++++++++++++++++++++++++
 hw/net/trace-events          |  17 +
 include/hw/net/npcm7xx_emc.h | 286 ++++++++++++
 4 files changed, 1156 insertions(+)
 create mode 100644 hw/net/npcm7xx_emc.c
 create mode 100644 include/hw/net/npcm7xx_emc.h
hw/net/npcm7xx_emc.c new file +16 -0
+static void emc_reset(NPCM7xxEMCState *emc)
+{
+    trace_npcm7xx_emc_reset(emc->emc_num);
+
+    memset(&emc->regs[0], 0, sizeof(emc->regs));
+
+    /* These regs have non-zero reset values. */
+    emc->regs[REG_TXDLSA] = 0xfffffffc;
+    emc->regs[REG_RXDLSA] = 0xfffffffc;
+    emc->regs[REG_MIIDA] = 0x00900000;
+    emc->regs[REG_FFTCR] = 0x0101;
+    emc->regs[REG_DMARFC] = 0x0800;
+    emc->regs[REG_MPCNT] = 0x7fff;
+
+    emc->tx_active = false;
+    emc->rx_active = false;

hw/net/npcm7xx_emc.c line 125

Quoted 10 lines
hw/net/npcm7xx_emc.c new file +10 -0
+
+    qemu_set_irq(emc->tx_irq, 0);
+    qemu_set_irq(emc->rx_irq, 0);
+}
+
+static void npcm7xx_emc_reset(DeviceState *dev)
+{
+    NPCM7xxEMCState *emc = NPCM7XX_EMC(dev);
+    emc_reset(emc);
+}
Quoted 9 lines

You can't call qemu_set_irq() from a DeviceState::reset method. Usually it's OK just not to try to set the outbound IRQs and to assume that the device you're connected to has reset to the state where its inbound IRQ line is not asserted. If you really need to set the irq line then you need to switch to 3-phase reset (some of the other npcm7xx devices do this). But I suspect that just moving the qemu_set_irq() calls to emc_soft_reset() would be enough.

Ah. Fixed in v3.

Don't put local variable declarations in the middle of functions,

Quoted 5 lines

please. Coding style says they should be at the start of a block (so, here, the start of the function). It looks like you've got middle-of-function declarations in several places in other functions too, so could you fix them all up please?

Fixed in v3.
Maybe now's a good time though to revisit this rule.
QEMU uses C99, and mixed decls/statements is an easy improvement to the coding standards.
I'm guessing this is an uncontroversial request. Is there just inertia behind not making the change thus far?

Quoted 4 lines

Optional, but you might consider using g_autofree for malloced_buf, which would let the compiler deal with g_free()ing it for you on all the function exit paths.

Done in v3.

Thanks.

#4 lore raw

Re: [PATCH v2 1/3] hw/net: Add npcm7xx emc model

Quoted 8 lines · Doug Evans
Quoted 5 lines · Peter Maydell

Don't put local variable declarations in the middle of functions, please. Coding style says they should be at the start of a block (so, here, the start of the function). It looks like you've got middle-of-function declarations in several places in other functions too, so could you fix them all up please?

Quoted 4 lines

Fixed in v3.
Maybe now's a good time though to revisit this rule.
QEMU uses C99, and mixed decls/statements is an easy improvement to the coding standards. I'm guessing this is an uncontroversial request. Is there just inertia behind not making the change thus far?

My personal opinion is that having declarations in the middle of functions is uglier. CODING_STYLE.rst already notes that we have an occasional-exception if there are #ifdefs involved, so we don't have the style rule because we're trying to work with compilers that don't accept middle-of-block declarations.

thanks
-- PMM

author
#5 lore raw

PATCH v2 2/3 hw/arm: Add npcm7xx emc model

In reply to dje--- via

This is a 10/100 ethernet device that has several features. Only the ones needed by the Linux driver have been implemented. See npcm7xx_emc.c for a list of unimplemented features.

---
 docs/system/arm/nuvoton.rst |  3 ++-
 hw/arm/npcm7xx.c            | 50 +++++++++++++++++++++++++++++++++++--
 include/hw/arm/npcm7xx.h    |  2 ++
 3 files changed, 52 insertions(+), 3 deletions(-)
docs/system/arm/nuvoton.rst +2 -1
index a1786342e2..c6e9a4c17e 100644
@@ -43,6 +43,7 @@ Supported devices
  * GPIO controller
  * Analog to Digital Converter (ADC)
  * Pulse Width Modulation (PWM)
+ * Ethernet controller (EMC)
 
 Missing devices
 ---------------
@@ -56,7 +57,7 @@ Missing devices
    * Shared memory (SHM)
    * eSPI slave interface
 
- * Ethernet controllers (GMAC and EMC)
+ * Ethernet controller (GMAC)
  * USB device (USBD)
  * SMBus controller (SMBF)
  * Peripheral SPI controller (PSPI)
hw/arm/npcm7xx.c +48 -2

86 lines not on this page · raw message

include/hw/arm/npcm7xx.h +2 -0
index f6227aa8aa..3bfc75aafe 100644
@@ -25,6 +25,7 @@
 #include "hw/misc/npcm7xx_gcr.h"
 #include "hw/misc/npcm7xx_pwm.h"
 #include "hw/misc/npcm7xx_rng.h"
+#include "hw/net/npcm7xx_emc.h"
 #include "hw/nvram/npcm7xx_otp.h"
 #include "hw/timer/npcm7xx_timer.h"
 #include "hw/ssi/npcm7xx_fiu.h"
@@ -88,6 +89,7 @@ typedef struct NPCM7xxState {
     EHCISysBusState     ehci;
     OHCISysBusState     ohci;
     NPCM7xxFIUState     fiu[2];
+    NPCM7xxEMCState     emc[2];
 } NPCM7xxState;
 
 #define TYPE_NPCM7XX    "npcm7xx"
Signature · 1 line
2.30.0.365.g02bc693789-goog
Reviewed-by on v2 2/3
#6 lore raw

Re: [PATCH v2 2/3] hw/arm: Add npcm7xx emc model

Quoted 5 lines · Doug Evans

This is a 10/100 ethernet device that has several features. Only the ones needed by the Linux driver have been implemented. See npcm7xx_emc.c for a list of unimplemented features.

Quoted 8 lines
---
 docs/system/arm/nuvoton.rst |  3 ++-
 hw/arm/npcm7xx.c            | 50 +++++++++++++++++++++++++++++++++++--
 include/hw/arm/npcm7xx.h    |  2 ++
 3 files changed, 52 insertions(+), 3 deletions(-)

thanks
-- PMM

author
#7 lore raw

PATCH v2 3/3 tests/qtests: Add npcm7xx emc model test

In reply to dje--- via
---
 tests/qtest/meson.build        |   1 +
 tests/qtest/npcm7xx_emc-test.c | 793 +++++++++++++++++++++++++++++++++
 2 files changed, 794 insertions(+)
 create mode 100644 tests/qtest/npcm7xx_emc-test.c
tests/qtest/meson.build +1 -0
index c83bc211b6..f7c369f3d5 100644
@@ -136,6 +136,7 @@ qtests_sparc64 = \
 
 qtests_npcm7xx = \
   ['npcm7xx_adc-test',
+   'npcm7xx_emc-test',
    'npcm7xx_gpio-test',
    'npcm7xx_pwm-test',
    'npcm7xx_rng-test',
tests/qtest/npcm7xx_emc-test.c new file +793 -0

793 lines not on this page · raw message

Signature · 1 line
2.30.0.365.g02bc693789-goog
Reviewed-by on v2 3/3
#8 lore raw

Re: [PATCH v2 3/3] tests/qtests: Add npcm7xx emc model test

Quoted 9 lines · Doug Evans
---
 tests/qtest/meson.build        |   1 +
 tests/qtest/npcm7xx_emc-test.c | 793 +++++++++++++++++++++++++++++++++
 2 files changed, 794 insertions(+)
 create mode 100644 tests/qtest/npcm7xx_emc-test.c

I see some more middle-of-function variable declarations here, but otherwise

thanks
-- PMM