hw/arm/raspi4b: working PCIe and GENET (real networking)
Subsystems (3)
v6 cover hw/arm/raspi4b: working PCIe and GENET (real networking)
This series gets raspi4b's PCIe root complex and GENET Ethernet controller working end to end: the PCIe link comes up and devices on it enumerate, and GENET passes real network traffic (DHCP, SSH, bulk downloads) under both older (Debian 11, 5.15) and newer (Debian 13, 6.18) guest kernels. Most of the GENET code is Sergey Kambalin's 2024 series (patchwork series 829638), carried forward with his authorship and Signed-off-by kept.
v6: thanks to Peter for the review of v5.
- Patch 14 (v4's migration patch, which v5 had dropped): add a VMStateDescription for the GENET device, as Peter asked. The MAC and PHY register blocks contain only uint32_t or uint16_t fields, so each is overlaid with an array of that element type in an anonymous union (the rest of the code keeps its s->regs.foo accesses) and is migrated with VMSTATE_UINT32_ARRAY() or VMSTATE_UINT16_ARRAY(), which is correct across hosts of different endianness. There is no post_load function: the interrupt outputs are derived from the registers and the device at the other end of each line migrates its own state.
- Patch 10: add the trailing comma to the .size initializer of bcm2838_genet_client_info, so that the RX patch does not have to edit that line.
- Added Peter's Reviewed-by to patches 1, 2 and 13.
v5: thanks to Peter for the review of v4.
- Patch 1: the vmstate describes the PCI device and the AER log in the same way as the other PCIe root port models that add state of their own (gen_pcie_root_port.c, ioh3420.c, xio3130_downstream.c): through the PCIESlot that the root port embeds.
- Patch 2: pass accesses to the EXT_CFG_DATA window to pci_host_config_read_common()/pci_host_config_write_common() for the function that EXT_CFG_INDEX selects, instead of calling the ECAM region's read/write ops; this also means that pcie_host_mmcfg_init() is no longer needed, so the call is gone and the commit message no longer lists it as a fixed defect (it also said "four" for a list of three). Set .impl.max_access_size to 8, and replace the comment on the unassigned-address windows, which had been copied from gpex.c, with one that only says what the code does. On map_irq: only slot 0 exists behind the root port, so there is nothing to swizzle; I added a comment saying so.
- Patch 10: return early on an out-of-range access in the write handler, and assert that the register offset is a multiple of 4 in bcm2838_genet_reg().
- Patch 13: clamp the length of each copy so that it cannot exceed what is left of rx_packet[] after the status block, rather than relying on the caller's size check.
- Dropped v4 patch 14 (GENET migration state); restored in v6.
- Added Peter's Reviewed-by to patches 10, 11 and 12 (patch 10 now includes the two changes he asked for).
v4: thanks to Peter for the review of v3.
- Patch 1: move the dc->user_creatable rationale comment next to the line it explains; add a VMStateDescription for the root port (PCI device, AER log and the regs[] array).
- Patch 2: treat root_port.regs[] as little-endian bytes and access it with ldn_le_p()/stn_le_p()/ldl_le_p() instead of memcpy() and a uint32_t * overlay. Drop pci_bus_set_route_irq_fn(), its callback and the irq_num[] storage that only fed it: INTx routing queries come only from VFIO (with KVM irqfds), some x86 code and hw/remote, none of which applies to raspi4b.
- Patch 3: drop the call to the removed set_irq_num() helper.
- Patch 4: set both min and max access size on impl and valid.
- Patch 8: BCM2838GenetPhyAuxShdRegs becomes an array of uint16_t rather than a struct, as suggested on patch 11.
- Patch 10: access the register block as host-order uint32_t at the register's offset instead of memcpy() of an arbitrary size (the HFB filter array becomes uint32_t too, so every register in the block is one); do not call qemu_macaddr_default_if_unset() when the guest programs a MAC address, just update the NIC info string; accept an access that ends exactly at the end of the register block.
- Patch 11: do not call bcm2838_genet_phy_update_link() from the MDIO patch; it is only defined by the RX patch, so the series did not build at this point. The prototype and the call move to the RX patch. Reword the "8 bits" comment and assert that the expansion shadow register indexes are in range.
- Patch 12: check that a TX frame is at least a status block plus an Ethernet header, and that a frame's descriptors fit in tx_packet[]; check that reading the frame from guest memory succeeded; use lduw_be_p()/stw_be_p(); lower-case local names; add a helper for building 64-bit values from register halves; drop redundant "!= 0" on bools; split the producer-index handling out of bcm2838_genet_tdma() to reduce nesting.
- Patch 13: the same clean-ups as the TX path where they apply; replace the comment that described an old bug with one noting that we do not implement the hardware's partial RX checksum and rely on the guest checking in software.
- Patch 16: call require_netdev('user') so the test is skipped on builds without the user-mode network backend.
Based on master (7e8d3c7fc4). It conflicts trivially (raspi4b.c and bcm2838_peripherals.[ch]) with the RNG200/thermal series, which is in target-arm.next; I will rebase once that lands.
Tested: checkpatch.pl reports no errors; every patch builds on its own; tests/functional/aarch64/test_raspi4.py passes; and unmodified Raspberry Pi OS Bullseye and Trixie images boot under raspi4b with a working network (DHCP, ping) and SSH login; and a booted Debian 13 guest keeps its state and its network across savevm/loadvm.
Marcelo Manzo (8):
hw/arm/bcm2838_pcie: add BCM2838 PCIe Root Complex hw/arm/bcm2838_pcie: add BCM2838 PCIe host hw/arm/bcm2838: enable BCM2838 PCIe host bridge hw/net/bcm2838_genet: add GENET stub device hw/net/bcm2838_genet: add migration state hw/arm/bcm2838: enable BCM2838 GENET controller docs/system/arm/raspi: move PCIe/GENET from missing to implemented tests/functional/aarch64: add raspi4b GENET networking test
Sergey Kambalin (9):
hw/net/bcm2838_genet: add GENET register structs, part 1/4 hw/net/bcm2838_genet: add GENET register structs, part 2/4 hw/net/bcm2838_genet: add GENET register structs, part 3/4 hw/net/bcm2838_genet: add GENET register structs, part 4/4 hw/net/bcm2838_genet: add GENET register access macros hw/net/bcm2838_genet: implement GENET register ops hw/net/bcm2838_genet: implement GENET MDIO hw/net/bcm2838_genet: implement GENET TX path hw/net/bcm2838_genet: implement GENET RX path
docs/system/arm/raspi.rst | 6 +- hw/arm/bcm2838.c | 13 + hw/arm/bcm2838_pcie.c | 313 ++++++ hw/arm/bcm2838_peripherals.c | 46 + hw/arm/meson.build | 4 +- hw/arm/raspi4b.c | 2 - hw/arm/trace-events | 4 + hw/net/bcm2838_genet.c | 1185 +++++++++++++++++++++++ hw/net/meson.build | 2 + hw/net/trace-events | 16 + include/hw/arm/bcm2838_pcie.h | 73 ++ include/hw/arm/bcm2838_peripherals.h | 7 + include/hw/net/bcm2838_genet.h | 455 +++++++++ tests/functional/aarch64/test_raspi4.py | 31 + 14 files changed, 2152 insertions(+), 5 deletions(-) create mode 100644 hw/arm/bcm2838_pcie.c create mode 100644 hw/net/bcm2838_genet.c create mode 100644 include/hw/arm/bcm2838_pcie.h create mode 100644 include/hw/net/bcm2838_genet.h
Signature · 1 line
2.55.0
PATCH v6 1/17 hw/arm/bcm2838_pcie: add BCM2838 PCIe Root Complex
Add the PCI-facing "root" part (D0:F0) of the BCM2838 (Raspberry Pi 4B / BCM2711) PCIe Root Complex: a PCIe root port with the vendor's device/vendor IDs and capability offsets, not usable on its own without the host-facing part added in the next patch.
Adapted from patch 13/41 of Sergey Kambalin's "Raspberry Pi 4B machine" series (patchwork series 829638, posted to qemu-devel in 2024 but never merged) against current QEMU master; the surrounding PCIe/GENET networking support for raspi4b was left unmerged upstream (base scaffolding aside), so this and the following patches complete and debug that series to get real networking working under raspi4b.
--- hw/arm/bcm2838_pcie.c | 101 ++++++++++++++++++++++++++++++++++ hw/arm/meson.build | 4 +- include/hw/arm/bcm2838_pcie.h | 54 ++++++++++++++++++ 3 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 hw/arm/bcm2838_pcie.c create mode 100644 include/hw/arm/bcm2838_pcie.h
hw/arm/bcm2838_pcie.c new file +101 -0
@@ -0,0 +1,101 @@ +/* +* BCM2838 PCIe Root Complex emulation +* +* Copyright (C) 2022 Ovchinnikov Vitalii <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qapi/error.h" +#include "hw/core/irq.h" +#include "hw/pci-host/gpex.h" +#include "hw/core/qdev-properties.h" +#include "migration/vmstate.h" +#include "qemu/module.h" +#include "hw/pci/pcie.h" +#include "hw/pci/pcie_aer.h" +#include "hw/arm/bcm2838_pcie.h" +#include "trace.h" + +/* +* RC root part (D0:F0) +*/ + +static void bcm2838_pcie_root_port_reset_hold (Object *obj,ResetType type) +{ +PCIERootPortClass *rpc =PCIE_ROOT_PORT_GET_CLASS (obj); +PCIDevice *dev =PCI_DEVICE (obj); +BCM2838PcieRootState *s =BCM2838_PCIE_ROOT (dev); + +if (rpc->parent_phases.hold) { + rpc->parent_phases.hold (obj, type); + } + +memset (s->regs,0xFF ,sizeof (s->regs)); +} + +static void bcm2838_pcie_root_init (Object *obj) +{ +PCIBridge *br =PCI_BRIDGE (obj); + br->bus_name ="pcie.1" ; +} + +static const VMStateDescription vmstate_bcm2838_pcie_root = { + .name ="bcm2838-pcie-root" , + .priority =MIG_PRI_PCI_BUS , + .version_id =1 , + .minimum_version_id =1 , + .post_load = pcie_cap_slot_post_load, + .fields = (const VMStateField []) { +VMSTATE_PCI_DEVICE (parent_obj.parent_obj.parent_obj, PCIESlot), +VMSTATE_STRUCT (parent_obj.parent_obj.parent_obj.exp.aer_log, + PCIESlot,0 , vmstate_pcie_aer_log, PCIEAERLog), +VMSTATE_UINT8_ARRAY (regs, BCM2838PcieRootState, +BCM2838_PCIE_REGS_SIZE -PCIE_CONFIG_SPACE_SIZE ), +VMSTATE_END_OF_LIST () + } +}; + +static void bcm2838_pcie_root_class_init (ObjectClass *class,const void *data) +{ +DeviceClass *dc =DEVICE_CLASS (class); +PCIDeviceClass *k =PCI_DEVICE_CLASS (class); +ResettableClass *rc =RESETTABLE_CLASS (class); +PCIERootPortClass *rpc =PCIE_ROOT_PORT_CLASS (class); + + dc->desc ="BCM2711 PCIe Bridge" ; + dc->vmsd = &vmstate_bcm2838_pcie_root; + +resettable_class_set_parent_phases (rc,NULL , + bcm2838_pcie_root_port_reset_hold, +NULL , &rpc->parent_phases); + +/* +* PCI-facing part of the host bridge, not usable without the host-facing +* part, which can't be device_add'ed. +*/ + dc->user_creatable = false; + k->vendor_id =BCM2838_PCIE_VENDOR_ID ; + k->device_id =BCM2838_PCIE_DEVICE_ID ; + k->revision =BCM2838_PCIE_REVISION ; + + rpc->exp_offset =BCM2838_PCIE_EXP_CAP_OFFSET ; + rpc->aer_offset =BCM2838_PCIE_AER_CAP_OFFSET ; +} + +static const TypeInfo bcm2838_pcie_root_info = { + .name =TYPE_BCM2838_PCIE_ROOT , + .parent =TYPE_PCIE_ROOT_PORT , + .instance_size =sizeof (BCM2838PcieRootState), + .instance_init = bcm2838_pcie_root_init, + .class_init = bcm2838_pcie_root_class_init, +}; + +static void bcm2838_pcie_register (void ) +{ +type_register_static (&bcm2838_pcie_root_info); +} + +type_init (bcm2838_pcie_register)
hw/arm/meson.build +3 -1
@@ -104,7 +104,9 @@ arm_common_ss.add(when: 'CONFIG_EXYNOS4', if_true: files('exynos4_boards.c')) arm_common_ss.add(when: 'CONFIG_MAX78000FTHR', if_true: files('max78000fthr.c')) arm_common_ss.add(when: 'CONFIG_NETDUINO2', if_true: files('netduino2.c')) arm_common_ss.add(when: 'CONFIG_RASPI', if_true: files('bcm2835_peripherals.c')) -arm_common_ss.add(when: 'CONFIG_RASPI', if_true: files('bcm2838_peripherals.c')) +arm_common_ss.add(when: 'CONFIG_RASPI', if_true: files( + 'bcm2838_peripherals.c', + 'bcm2838_pcie.c')) arm_common_ss.add(when: 'CONFIG_STRONGARM', if_true: files('strongarm.c')) arm_common_ss.add(when: 'CONFIG_SX1', if_true: files('omap_sx1.c')) arm_common_ss.add(when: 'CONFIG_VERSATILE', if_true: files('versatilepb.c'))
include/hw/arm/bcm2838_pcie.h new file +54 -0
54 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v6 3/17 hw/arm/bcm2838: enable BCM2838 PCIe host bridge
In reply to Marcelo ManzoWire the BCM2838 PCIe host bridge into the SoC's peripheral block:
instantiate it, map its RC registers and MMIO window.
Per Peter Maydell's review of a later follow-up series, fold in the remaining defect from what was originally a separate bugfix here, since this device isn't in upstream git yet: the PCI MMIO window was mapped at the ARM base address with no address translation, even though PCIE_MMIO_OFFSET was defined for exactly that purpose (and otherwise unused). The DTB declares CPU 0x600000000 as mapping to PCI 0xc0000000, so a guest would read PCI address 0 where it expected a device BAR. Map an alias at the correct PCI offset instead.
--- hw/arm/bcm2838.c | 7 ++++++ hw/arm/bcm2838_peripherals.c | 35 ++++++++++++++++++++++++++++ hw/arm/raspi4b.c | 1 - include/hw/arm/bcm2838_peripherals.h | 3 +++ 4 files changed, 45 insertions(+), 1 deletion(-)
hw/arm/bcm2838.c +7 -0
@@ -230,6 +230,13 @@ static void bcm2838_realize(DeviceState *dev, Error **errp)qdev_connect_gpio_out (dma_9_10_irq_orgate,0 ,qdev_get_gpio_in (gicdev,GIC_SPI_INTERRUPT_DMA_9_10 )); +/* Connect PCIe host bridge to the interrupt controller */ +for (int n =0 ; n <BCM2838_PCIE_NUM_IRQS ; n++) { +int int_n =GIC_SPI_INTERRUPT_PCI_INT_A + n; +sysbus_connect_irq (SYS_BUS_DEVICE (&ps->pcie_host), n, +qdev_get_gpio_in (gicdev, int_n)); + } +/* Pass through inbound GPIO lines to the GIC */ qdev_init_gpio_in (dev, bcm2838_gic_set_irq,GIC_NUM_IRQS );
hw/arm/bcm2838_peripherals.c +35 -0
@@ -15,6 +15,11 @@#define CLOCK_ISP_OFFSET 0xc11000#define CLOCK_ISP_SIZE 0x100 +#define PCIE_RC_OFFSET 0x1500000 +#define PCIE_MMIO_OFFSET 0xc0000000 +#define PCIE_MMIO_ARM_OFFSET 0x600000000 +#define PCIE_MMIO_SIZE 0x40000000 +/* Lower peripheral base address on the VC (GPU) system bus */ #define BCM2838_VC_PERI_LOW_BASE 0x7c000000
@@ -35,6 +40,10 @@ static void bcm2838_peripherals_init(Object *obj)/* Extended Mass Media Controller 2 */ object_initialize_child (obj,"emmc2" , &s->emmc2,TYPE_SYSBUS_SDHCI ); +/* PCIe Host Bridge */ +object_initialize_child (obj,"pcie-host" , &s->pcie_host, +TYPE_BCM2838_PCIE_HOST ); +/* GPIO */ object_initialize_child (obj,"gpio" , &s->gpio,TYPE_BCM2838_GPIO );
@@ -67,6 +76,8 @@ static void bcm2838_peripherals_realize(DeviceState *dev, Error **errp)MemoryRegion *mphi_mr;BCM2838PeripheralState *s =BCM2838_PERIPHERALS (dev);BCMSocPeripheralBaseState *s_base =BCM_SOC_PERIPHERALS_BASE (dev); +MemoryRegion *regs_mr; +MemoryRegion *mmio_mr;int n;bcm_soc_peripherals_common_realize (dev, errp);
@@ -182,6 +193,30 @@ static void bcm2838_peripherals_realize(DeviceState *dev, Error **errp)create_unimp (s_base, &s->clkisp,"bcm2835-clkisp" ,CLOCK_ISP_OFFSET ,CLOCK_ISP_SIZE ); +/* PCIe Root Complex */ +if (!sysbus_realize (SYS_BUS_DEVICE (&s->pcie_host), errp)) { +return ; + } +/* RC registers region */ + regs_mr =sysbus_mmio_get_region (SYS_BUS_DEVICE (&s->pcie_host),0 ); +memory_region_add_subregion (&s->peri_low_mr,PCIE_RC_OFFSET , regs_mr); +/* +* MMIO region. +* +* The BCM2711 PCIe controller translates addresses between the ARM and +* PCI address spaces: the DTB declares CPU 0x600000000 as mapping to PCI +* 0xc0000000. Map an alias of the PCI window starting at that PCI offset +* so accesses land on the right addresses; mapping the window directly +* would expose PCI address 0 at the ARM base instead, and every BAR +* behind the root port would be read at the wrong address. +*/ + mmio_mr =sysbus_mmio_get_region (SYS_BUS_DEVICE (&s->pcie_host),1 ); +memory_region_init_alias (&s->pcie_mmio_alias,OBJECT (s), +"bcm2838_pcie_mmio_alias" , mmio_mr, +PCIE_MMIO_OFFSET ,PCIE_MMIO_SIZE ); +memory_region_add_subregion (get_system_memory (),PCIE_MMIO_ARM_OFFSET , + &s->pcie_mmio_alias); +/* GPIO */ if (!sysbus_realize (SYS_BUS_DEVICE (&s->gpio), errp)) {return ;
hw/arm/raspi4b.c +0 -1
@@ -63,7 +63,6 @@ static void raspi4_modify_dtb(const struct arm_boot_info *info, void *fdt)/* Temporarily disable following devices until they are implemented */ const char *nodes_to_remove[] = { -"brcm,bcm2711-pcie" ,"brcm,bcm2711-rng200" ,"brcm,bcm2711-thermal" ,"brcm,bcm2711-genet-v5" ,
include/hw/arm/bcm2838_peripherals.h +3 -0
@@ -10,6 +10,7 @@#define BCM2838_PERIPHERALS_H #include "hw/arm/bcm2835_peripherals.h" +#include "hw/arm/bcm2838_pcie.h" #include "hw/sd/sdhci.h" #include "hw/gpio/bcm2838_gpio.h"
@@ -65,6 +66,8 @@ struct BCM2838PeripheralState {MemoryRegion mphi_mr_alias;SDHCIState emmc2; +MemoryRegion pcie_mmio_alias; +BCM2838PcieHostState pcie_host;BCM2838GpioState gpio;OrIRQState mmc_irq_orgate;
Signature · 1 line
2.55.0
PATCH v6 10/17 hw/net/bcm2838_genet: implement GENET register ops
In reply to Marcelo ManzoFrom: Sergey Kambalin <[email protected]>
Replace the stub MMIO read/write handlers with real dispatch over the register offsets from the previous patches: SYS revision control, INTRL0/1 status/mask registers, UMAC command/MAC-address/ MDIO-command registers, and TDMA/RDMA ring configuration, wired up to the NIC's realize()/reset() and qdev NIC properties (DEFINE_NIC_PROPERTIES) so the device gets a MAC address and peer netdev from the command line.
Part of Sergey Kambalin's original Raspberry Pi 4B PCIe/GENET networking series (patchwork series 829638, posted to qemu-devel in 2024, never merged). Carried forward and completed by Marcelo Manzo:
adapted to the current Property/class_init/Resettable APIs.
While carrying this forward, make these changes to the original code:
- Access registers as a host-order uint32_t at their byte offset instead of memcpy()ing an arbitrary number of bytes in and out of the register block, which gives the wrong result on big-endian hosts for anything that is not a uint32_t. The HFB filter array becomes a uint32_t array too, so that every register in the block is one.
- Do not call qemu_macaddr_default_if_unset() when the guest programs a MAC address: it is for realize() to pick a default when the user gave none. Only update the NIC info string.
- Accept an access that ends exactly at the end of the register block.
--- hw/net/bcm2838_genet.c | 196 +++++++++++++++++++++++++++++++-- include/hw/net/bcm2838_genet.h | 19 +++- 2 files changed, 204 insertions(+), 11 deletions(-)
hw/net/bcm2838_genet.c +187 -9
@@ -236,25 +236,150 @@ REG16(GENET_PHY_EXP_SEL, 0) FIELD(GENET_PHY_EXP_SEL, REG_ID, 0, 8) FIELD(GENET_PHY_EXP_SEL, BLOCK_ID, 8, 8) +static void bcm2838_genet_set_qemu_mac (BCM2838GenetState *s) +{ +const MACAddr *addr = &s->nic_conf.macaddr; + + s->regs.umac.mac0 =FIELD_DP32 (s->regs.umac.mac0,GENET_UMAC_MAC_0 , +ADDR_0 , addr->a[0 ]); + s->regs.umac.mac0 =FIELD_DP32 (s->regs.umac.mac0,GENET_UMAC_MAC_0 , +ADDR_1 , addr->a[1 ]); + s->regs.umac.mac0 =FIELD_DP32 (s->regs.umac.mac0,GENET_UMAC_MAC_0 , +ADDR_2 , addr->a[2 ]); + s->regs.umac.mac0 =FIELD_DP32 (s->regs.umac.mac0,GENET_UMAC_MAC_0 , +ADDR_3 , addr->a[3 ]); + s->regs.umac.mac1 =FIELD_DP32 (s->regs.umac.mac1,GENET_UMAC_MAC_1 , +ADDR_4 , addr->a[4 ]); + s->regs.umac.mac1 =FIELD_DP32 (s->regs.umac.mac1,GENET_UMAC_MAC_1 , +ADDR_5 , addr->a[5 ]); +} + +static void bcm2838_genet_set_irq_default (BCM2838GenetState *s) +{ +uint32_t intrl_0_status = s->regs.intrl0.stat; +uint32_t intrl_0_mask = s->regs.intrl0.mask_status; +int level = (intrl_0_status & ~intrl_0_mask) ==0 ?0 :1 ; + +qemu_set_irq (s->irq_default, level); +} + +static void bcm2838_genet_set_irq_prio (BCM2838GenetState *s) +{ +uint32_t intrl_1_status = s->regs.intrl1.stat; +uint32_t intrl_1_mask = s->regs.intrl1.mask_status; +int level = (intrl_1_status & ~intrl_1_mask) ==0 ?0 :1 ; + +qemu_set_irq (s->irq_prio, level); +} +static uint64_t bcm2838_genet_read (void *opaque,hwaddr offset,unsigned size) {uint64_t value = ~0 ; +BCM2838GenetState *s = opaque; -qemu_log_mask ( -LOG_GUEST_ERROR , -"%s: out-of-range access, %u bytes @ offset 0x%04" PRIx64"\n" , - __func__, size, offset); +if (offset + size <=sizeof (s->regs)) { + value = *bcm2838_genet_reg (s, offset); + }else { +qemu_log_mask ( +LOG_GUEST_ERROR , +"%s: out-of-range access, %u bytes @ offset 0x%04" PRIx64"\n" , + __func__, size, offset); + }trace_bcm2838_genet_read (size, offset, value);return value; }static void bcm2838_genet_write (void *opaque,hwaddr offset,uint64_t value, -unsigned size) { -qemu_log_mask ( -LOG_GUEST_ERROR , -"%s: out-of-range access, %u bytes @ offset 0x%04" PRIx64"\n" , - __func__, size, offset); +unsigned size) +{ +BCM2838GenetState *s = opaque; +MACAddr *mac = &s->nic_conf.macaddr; +NetClientState *ncs =qemu_get_queue (s->nic); + +trace_bcm2838_genet_write (size, offset, value); + +if (offset + size >sizeof (s->regs)) { +qemu_log_mask ( +LOG_GUEST_ERROR , +"%s: out-of-range access, %u bytes @ offset 0x%04" PRIx64"\n" , + __func__, size, offset); +return ; + } + +switch (offset) { +case BCM2838_GENET_INTRL0_SET : + s->regs.intrl0.stat |= value; +break ; +case BCM2838_GENET_INTRL0_CLEAR : + s->regs.intrl0.stat &= ~value; +break ; +case BCM2838_GENET_INTRL0_MASK_SET : + s->regs.intrl0.mask_status |= value; +break ; +case BCM2838_GENET_INTRL0_MASK_CLEAR : + s->regs.intrl0.mask_status &= ~value; +break ; +case BCM2838_GENET_INTRL1_SET : + s->regs.intrl1.stat |= value; +break ; +case BCM2838_GENET_INTRL1_CLEAR : + s->regs.intrl1.stat &= ~value; +break ; +case BCM2838_GENET_INTRL1_MASK_SET : + s->regs.intrl1.mask_status |= value; +break ; +case BCM2838_GENET_INTRL1_MASK_CLEAR : + s->regs.intrl1.mask_status &= ~value; +break ; +case BCM2838_GENET_UMAC_CMD : +/* Complete SW reset as soon as it has been requested */ +if (FIELD_EX32 (value,GENET_UMAC_CMD ,SW_RESET ) ==1 ) { +device_cold_reset (DEVICE (s)); + value =FIELD_DP32 (value,GENET_UMAC_CMD ,SW_RESET ,0 ); + } +break ; +/* +* TODO: before changing MAC address we'd better inform QEMU +* network subsystem about freeing previously used one, but +* qemu_macaddr_set_free function isn't accessible for us (marked +* as static in net/net.c), see also https://lists.nongnu.org/ +* archive/html/qemu-devel/2022-07/msg02123.html +*/ +case BCM2838_GENET_UMAC_MAC0 : + mac->a[0 ] =FIELD_EX32 (value,GENET_UMAC_MAC_0 ,ADDR_0 ); + mac->a[1 ] =FIELD_EX32 (value,GENET_UMAC_MAC_0 ,ADDR_1 ); + mac->a[2 ] =FIELD_EX32 (value,GENET_UMAC_MAC_0 ,ADDR_2 ); + mac->a[3 ] =FIELD_EX32 (value,GENET_UMAC_MAC_0 ,ADDR_3 ); +qemu_format_nic_info_str (ncs, mac->a); +trace_bcm2838_genet_mac_address (ncs->info_str); +break ; +case BCM2838_GENET_UMAC_MAC1 : + mac->a[4 ] =FIELD_EX32 (value,GENET_UMAC_MAC_1 ,ADDR_4 ); + mac->a[5 ] =FIELD_EX32 (value,GENET_UMAC_MAC_1 ,ADDR_5 ); +qemu_format_nic_info_str (ncs, mac->a); +trace_bcm2838_genet_mac_address (ncs->info_str); +break ; +case BCM2838_GENET_RDMA_CTRL : + s->regs.rdma.status = (~value) &GENET_DMA_ENABLE_MASK ; +break ; +case BCM2838_GENET_UMAC_MDIO_CMD : +case BCM2838_GENET_TDMA_REGS + ... BCM2838_GENET_TDMA_REGS + sizeof(BCM2838GenetRegsTdma) - 1: +qemu_log_mask (LOG_UNIMP , +"UMAC MDIO and TDMA aren't implemented yet" ); +break ; +default : +break ; + } + + *bcm2838_genet_reg (s, offset) = value; +if (offset ==BCM2838_GENET_RDMA_CTRL && +FIELD_EX32 (value,GENET_DMA_CTRL ,EN )) { +qemu_flush_queued_packets (ncs); + } +bcm2838_genet_set_irq_default (s); +bcm2838_genet_set_irq_prio (s); }static const MemoryRegionOps bcm2838_genet_ops = {
@@ -265,9 +390,14 @@ static const MemoryRegionOps bcm2838_genet_ops = { .valid = {.min_access_size = 4, .max_access_size = 4}, }; +static NetClientInfo bcm2838_genet_client_info = { + .type =NET_CLIENT_DRIVER_NIC , + .size =sizeof (NICState), +};static void bcm2838_genet_realize (DeviceState *dev,Error **errp) { +NetClientState *ncs;BCM2838GenetState *s =BCM2838_GENET (dev);SysBusDevice *sbd =SYS_BUS_DEVICE (dev);
@@ -275,10 +405,46 @@ static void bcm2838_genet_realize(DeviceState *dev, Error **errp)memory_region_init_io (&s->regs_mr,OBJECT (s), &bcm2838_genet_ops, s,"bcm2838_genet_regs" ,sizeof (s->regs));sysbus_init_mmio (sbd, &s->regs_mr); + +/* QEMU-managed NIC (host network back-end connection) */ +qemu_macaddr_default_if_unset (&s->nic_conf.macaddr); + s->nic =qemu_new_nic (&bcm2838_genet_client_info, &s->nic_conf, +object_get_typename (OBJECT (dev)), dev->id, + &dev->mem_reentrancy_guard, s); +bcm2838_genet_set_qemu_mac (s); + ncs =qemu_get_queue (s->nic); +qemu_format_nic_info_str (ncs, s->nic_conf.macaddr.a); +trace_bcm2838_genet_mac_address (ncs->info_str); + +/* Interrupts */ +sysbus_init_irq (sbd, &s->irq_default); +sysbus_init_irq (sbd, &s->irq_prio); + +/* DMA space */ +address_space_init (&s->dma_as,get_system_memory (),"bcm2838_genet_dma" ); }static void bcm2838_genet_phy_reset (BCM2838GenetState *s) { +memset (&s->phy_regs,0x00 ,sizeof (s->phy_regs)); +memset (&s->phy_shd_regs,0x00 ,sizeof (s->phy_shd_regs)); +memset (&s->phy_aux_ctl_shd_regs,0x00 ,sizeof (s->phy_aux_ctl_shd_regs)); + +/* All values below were taken from real HW trace and logs */ + s->phy_regs.bmcr =0x1140 ; + s->phy_regs.bmsr =0x7949 ; + s->phy_regs.sid1 =0x600D ; + s->phy_regs.sid2 =0x84A2 ; + s->phy_regs.advertise =0x01E1 ; + s->phy_regs.ctrl1000 =0x0200 ; + s->phy_regs.estatus =0x3000 ; + + s->phy_shd_regs.clk_ctl =0x0200 ; + s->phy_shd_regs.scr3 =0x001F ; + s->phy_shd_regs.apd =0x0001 ; + + s->phy_aux_ctl_shd_regs.regs[BCM2838_GENET_PHY_AUX_CTL_MISC ] =0x1E ; +trace_bcm2838_genet_phy_reset ("done" ); }
@@ -288,11 +454,22 @@ static void bcm2838_genet_reset(Object *obj, ResetType type)memset (&s->regs,0x00 ,sizeof (s->regs)); + s->regs.sys.rev_ctrl =FIELD_DP32 (s->regs.sys.rev_ctrl,GENET_SYS_REV_CTRL , +MAJOR_REV ,BCM2838_GENET_REV_MAJOR ); + s->regs.sys.rev_ctrl =FIELD_DP32 (s->regs.sys.rev_ctrl,GENET_SYS_REV_CTRL , +MINOR_REV ,BCM2838_GENET_REV_MINOR ); + s->regs.rdma.status =GENET_DMA_ENABLE_MASK ; + s->regs.tdma.status =GENET_DMA_ENABLE_MASK ; +trace_bcm2838_genet_reset ("done" );bcm2838_genet_phy_reset (s); } +static const Property genet_properties[] = { +DEFINE_NIC_PROPERTIES (BCM2838GenetState, nic_conf), +}; +static void bcm2838_genet_class_init (ObjectClass *class,const void *data) {static ResettablePhases unused_parent_phases;
@@ -302,6 +479,7 @@ static void bcm2838_genet_class_init(ObjectClass *class, const void *data) dc->realize = bcm2838_genet_realize;resettable_class_set_parent_phases (rc,NULL , bcm2838_genet_reset,NULL , &unused_parent_phases); +device_class_set_props (dc, genet_properties); }static const TypeInfo bcm2838_genet_info = {
include/hw/net/bcm2838_genet.h +17 -2
@@ -293,8 +293,7 @@ typedef struct { } BCM2838GenetRegsTdma; typedef struct { - uint8_t flt[BCM2838_GENET_HFB_FILTER_CNT * BCM2838_GENET_HFB_FILTER_SIZE - * sizeof(uint32_t)]; + uint32_t flt[BCM2838_GENET_HFB_FILTER_CNT * BCM2838_GENET_HFB_FILTER_SIZE]; uint32_t reserved_0x6000[1792]; uint32_t ctrl; uint32_t flt_enable[2];
@@ -388,6 +387,8 @@ struct BCM2838GenetState {SysBusDevice parent_obj;/*< public >*/ +NICState *nic; +NICConf nic_conf;MemoryRegion regs_mr;AddressSpace dma_as;
@@ -402,4 +403,18 @@ struct BCM2838GenetState {qemu_irq irq_prio; }; +/* +* Every field of BCM2838GenetRegs is a uint32_t and the MMIO ops only allow +* naturally aligned 4-byte accesses, so a register can be accessed as a +* host-order uint32_t at its byte offset; the memory API takes care of the +* guest/host byte order difference. +*/ +static inline uint32_t *bcm2838_genet_reg (BCM2838GenetState *s,hwaddr offset) +{ +QEMU_BUILD_BUG_ON (sizeof (BCM2838GenetRegs) %sizeof (uint32_t ) !=0 ); +assert (offset %sizeof (uint32_t ) ==0 ); + +return (uint32_t *)((uint8_t *)&s->regs + offset); +} +#endif /* BCM2838_GENET_H */
Signature · 1 line
2.55.0
PATCH v6 5/17 hw/net/bcm2838_genet: add GENET register structs, part 1/4
In reply to Marcelo ManzoFrom: Sergey Kambalin <[email protected]>
Add the SYS, GR_BRIDGE, EXT, INTRL0, and INTRL1 register block layouts and the top-level BCM2838GenetRegs/BCM2838GenetState structs, plus REG32/FIELD accessors for the revision-control and interrupt-status registers.
Part of Sergey Kambalin's original Raspberry Pi 4B PCIe/GENET networking series (patchwork series 829638, posted to qemu-devel in 2024, never merged). Carried forward and completed by Marcelo Manzo.
--- hw/net/bcm2838_genet.c | 37 ++++++++++++++++ include/hw/net/bcm2838_genet.h | 77 +++++++++++++++++++++++++++++++++- 2 files changed, 113 insertions(+), 1 deletion(-)
hw/net/bcm2838_genet.c +37 -0
@@ -15,10 +15,47 @@#include "hw/core/irq.h" #include "net/checksum.h" #include "system/dma.h" +#include "hw/core/registerfields.h" #include "hw/core/resettable.h" #include "hw/net/bcm2838_genet.h" #include "trace.h" +REG32(GENET_SYS_REV_CTRL, 0) +FIELD(GENET_SYS_REV_CTRL, GPHY_REV, 0, 16) +FIELD(GENET_SYS_REV_CTRL, MINOR_REV, 16, 4) +FIELD(GENET_SYS_REV_CTRL, RSVD_20_23, 20, 4) +FIELD(GENET_SYS_REV_CTRL, MAJOR_REV, 24, 4) +FIELD(GENET_SYS_REV_CTRL, RSVD_28_31, 28, 4) + +REG32(GENET_INTRL_0, 0) +FIELD(GENET_INTRL_0, SCB, 0, 1) +FIELD(GENET_INTRL_0, EPHY, 1, 1) +FIELD(GENET_INTRL_0, PHY_DET_R, 2, 1) +FIELD(GENET_INTRL_0, PHY_DET_F, 3, 1) +FIELD(GENET_INTRL_0, LINK_UP, 4, 1) +FIELD(GENET_INTRL_0, LINK_DOWN, 5, 1) +FIELD(GENET_INTRL_0, UMAC, 6, 1) +FIELD(GENET_INTRL_0, UMAC_TSV, 7, 1) +FIELD(GENET_INTRL_0, TBUF_UNDERRUN, 8, 1) +FIELD(GENET_INTRL_0, RBUF_OVERFLOW, 9, 1) +FIELD(GENET_INTRL_0, HFB_SM, 10, 1) +FIELD(GENET_INTRL_0, HFB_MM, 11, 1) +FIELD(GENET_INTRL_0, MPD_R, 12, 1) +FIELD(GENET_INTRL_0, RXDMA_MBDONE, 13, 1) +FIELD(GENET_INTRL_0, RXDMA_PDONE, 14, 1) +FIELD(GENET_INTRL_0, RXDMA_BDONE, 15, 1) +FIELD(GENET_INTRL_0, TXDMA_MBDONE, 16, 1) +FIELD(GENET_INTRL_0, TXDMA_PDONE, 17, 1) +FIELD(GENET_INTRL_0, TXDMA_BDONE, 18, 1) +FIELD(GENET_INTRL_0, RSVD_19_22, 19, 4) +FIELD(GENET_INTRL_0, MDIO_DONE, 23, 1) +FIELD(GENET_INTRL_0, MDIO_ERROR, 24, 1) +FIELD(GENET_INTRL_0, RSVD_25_31, 25, 4) + +REG32(GENET_INTRL_1, 0) +FIELD(GENET_INTRL_1, TX_INTRS, 0, 16) +FIELD(GENET_INTRL_1, RX_INTRS, 16, 16) +static uint64_t bcm2838_genet_read (void *opaque,hwaddr offset,unsigned size) {
include/hw/net/bcm2838_genet.h +76 -1
83 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v6 11/17 hw/net/bcm2838_genet: implement GENET MDIO
In reply to Marcelo ManzoFrom: Sergey Kambalin <[email protected]>
Implement the MDIO command register handling used to read/write the BCM54213PE PHY's standard MII registers (BMCR, BMSR, LPA, ...) plus its vendor "shadow" register blocks reached through the AUX_CTL/ EXP_SHD indirection scheme, and a simple link-state model (PHY starts down, comes up once BMCR is programmed and autonegotiation is (re)started).
Part of Sergey Kambalin's original Raspberry Pi 4B PCIe/GENET networking series (patchwork series 829638, posted to qemu-devel in 2024, never merged). Carried forward and completed by Marcelo Manzo.
While carrying this forward, fix the autonegotiation-restart handling in bcm2838_genet_mdio_cmd(): the guest PHY driver sets BMCR.ANRESTART and polls BMCR until it reads back as cleared, but the result of FIELD_DP16() (which returns a new value rather than modifying its argument) was discarded, so the bit never cleared and the guest spun forever at boot.
--- hw/net/bcm2838_genet.c | 138 ++++++++++++++++++++++++++++++++- include/hw/net/bcm2838_genet.h | 3 +- 2 files changed, 138 insertions(+), 3 deletions(-)
hw/net/bcm2838_genet.c +136 -2
@@ -216,6 +216,7 @@ FIELD(GENET_PHY_STAT_1000, LOCALRXOK, 13, 1) FIELD(GENET_PHY_STAT_1000, MSRES, 14, 1)FIELD (GENET_PHY_STAT_1000 ,MSFAIL ,15 ,1 ) +/* There are two data representations for PHY_AUX_CTRL register */ REG16(GENET_PHY_AUX_CTRL_0, 0) FIELD(GENET_PHY_AUX_CTRL_0, REG_ID_MASK, 0, 3)FIELD (GENET_PHY_AUX_CTRL_0 ,RSVD_3 ,3 ,1 )
@@ -272,6 +273,135 @@ static void bcm2838_genet_set_irq_prio(BCM2838GenetState *s)qemu_set_irq (s->irq_prio, level); } +static void bcm2838_genet_phy_aux_ctl_write (BCM2838GenetState *s, +uint16_t value) +{ +uint16_t reg_id =FIELD_EX16 (value,GENET_PHY_AUX_CTRL_0 ,REG_ID ); +uint16_t reg_id_mask =FIELD_EX16 (value,GENET_PHY_AUX_CTRL_0 ,REG_ID_MASK ); +uint16_t misc_wren =FIELD_EX16 (value,GENET_PHY_AUX_CTRL_0 ,MISC_WREN ); +uint16_t reg_data =FIELD_EX16 (value,GENET_PHY_AUX_CTRL_0 ,REG_DATA ); +uint16_t reg_data12 =FIELD_EX16 (value,GENET_PHY_AUX_CTRL_1 ,REG_DATA ); + +uint16_t *phy_aux_ctl_shd_reg_id = + &s->phy_aux_ctl_shd_regs.regs[reg_id]; +uint16_t *phy_aux_ctl_shd_reg_id_mask = + &s->phy_aux_ctl_shd_regs.regs[reg_id_mask]; + +if (reg_id_mask ==BCM2838_GENET_PHY_AUX_CTL_MISC ) { +if (reg_id ==BCM2838_GENET_PHY_AUX_CTL_MISC ) { +if (misc_wren ==0 ) { +/* write for subsequent read (8-bit from AUX_CTL_MISC) */ +FIELD_DP16 (value,GENET_PHY_AUX_CTRL_0 ,REG_DATA , + *phy_aux_ctl_shd_reg_id); + }else { +/* write the 8-bit REG_DATA field to AUX_CTL_MISC */ + *phy_aux_ctl_shd_reg_id_mask = reg_data; + } + }else { +/* write for subsequent read (12-bit) */ +FIELD_DP16 (value,GENET_PHY_AUX_CTRL_1 ,REG_DATA , + *phy_aux_ctl_shd_reg_id); + } + }else { +/* write 12 bits */ + *phy_aux_ctl_shd_reg_id_mask = reg_data12; + } + + s->phy_regs.aux_ctl = value; +} + +static void bcm2838_genet_phy_shadow_write (BCM2838GenetState *s, +uint16_t value) +{ +uint16_t reg_id =FIELD_EX16 (value,GENET_PHY_SHADOW ,REG_ID ); +uint16_t wr =FIELD_EX16 (value,GENET_PHY_SHADOW ,WR ); +uint16_t reg_data =FIELD_EX16 (value,GENET_PHY_SHADOW ,REG_DATA ); + +uint16_t *phy_shd_reg = (uint16_t *)&s->phy_shd_regs + reg_id; + +if (wr ==0 ) { +FIELD_DP16 (value,GENET_PHY_SHADOW ,REG_DATA , *phy_shd_reg); + }else { + *phy_shd_reg = reg_data; + } + + s->phy_regs.shd = value; +} + +static void bcm2838_genet_phy_exp_shadow_write (BCM2838GenetState *s, +uint16_t value) +{ +/* +* TODO Stub implementation without side effect, +* just storing registers values +*/ +uint16_t reg_id =FIELD_EX16 (s->phy_regs.exp_ctrl, +GENET_PHY_EXP_SEL ,REG_ID ); +uint16_t block_id =FIELD_EX16 (s->phy_regs.exp_ctrl, +GENET_PHY_EXP_SEL ,BLOCK_ID ); + +assert (block_id <ARRAY_SIZE (s->phy_exp_shd_regs.regs)); +assert (reg_id <ARRAY_SIZE (s->phy_exp_shd_regs.regs[0 ])); + s->phy_exp_shd_regs.regs[block_id][reg_id] = value; +} + +static uint16_t bcm2838_genet_phy_exp_shadow_read (BCM2838GenetState *s) +{ +uint16_t reg_id =FIELD_EX16 (s->phy_regs.exp_ctrl, +GENET_PHY_EXP_SEL ,REG_ID ); +uint16_t block_id =FIELD_EX16 (s->phy_regs.exp_ctrl, +GENET_PHY_EXP_SEL ,BLOCK_ID ); + +assert (block_id <ARRAY_SIZE (s->phy_exp_shd_regs.regs)); +assert (reg_id <ARRAY_SIZE (s->phy_exp_shd_regs.regs[0 ])); +return s->phy_exp_shd_regs.regs[block_id][reg_id]; +} + +static uint64_t bcm2838_genet_mdio_cmd (BCM2838GenetState *s,uint64_t cmd) +{ +uint32_t phy_reg_id =FIELD_EX32 (cmd,GENET_UMAC_MDIO_CMD ,REG_ID ); +uint32_t phy_reg_data =FIELD_EX32 (cmd,GENET_UMAC_MDIO_CMD ,REG_DATA ); +uint32_t start_busy =FIELD_EX32 (cmd,GENET_UMAC_MDIO_CMD ,START_BUSY ); +uint32_t rd =FIELD_EX32 (cmd,GENET_UMAC_MDIO_CMD ,RD ); +uint32_t wr =FIELD_EX32 (cmd,GENET_UMAC_MDIO_CMD ,WR ); +uint16_t *phy_reg = (uint16_t *)&s->phy_regs + phy_reg_id; + +uint16_t anrestart =FIELD_EX16 (phy_reg_data,GENET_PHY_BMCR ,ANRESTART ); + +if (start_busy !=0 ) { + cmd =FIELD_DP32 (cmd,GENET_UMAC_MDIO_CMD ,START_BUSY ,0 ); + +if (rd !=0 ) { +if (phy_reg_id ==BCM2838_GENET_EXP_DATA ) { + cmd =FIELD_DP32 (cmd,GENET_UMAC_MDIO_CMD ,REG_DATA , +bcm2838_genet_phy_exp_shadow_read (s)); + }else { + cmd =FIELD_DP32 (cmd,GENET_UMAC_MDIO_CMD ,REG_DATA , *phy_reg); + } + }else if (wr !=0 ) { +if (phy_reg_id ==BCM2838_GENET_PHY_AUX_CTL ) { +bcm2838_genet_phy_aux_ctl_write (s, phy_reg_data); + }else if (phy_reg_id ==BCM2838_GENET_PHY_SHD ) { +bcm2838_genet_phy_shadow_write (s, phy_reg_data); + }else if (phy_reg_id ==BCM2838_GENET_EXP_DATA ) { +bcm2838_genet_phy_exp_shadow_write (s, phy_reg_data); + }else { +if (phy_reg_id ==BCM2838_GENET_PHY_BMCR ) { +/* Initiate auto-negotiation once it has been restarted */ +if (anrestart ==1 ) { + phy_reg_data =FIELD_DP16 (phy_reg_data, +GENET_PHY_BMCR , +ANRESTART ,0 ); + } + } + *phy_reg = phy_reg_data; + } + } + } + +return cmd; +} +static uint64_t bcm2838_genet_read (void *opaque,hwaddr offset,unsigned size) {uint64_t value = ~0 ;
@@ -364,10 +494,13 @@ static void bcm2838_genet_write(void *opaque, hwaddr offset, uint64_t value, s->regs.rdma.status = (~value) &GENET_DMA_ENABLE_MASK ;break ;case BCM2838_GENET_UMAC_MDIO_CMD : + value =bcm2838_genet_mdio_cmd (s, value); + s->regs.intrl0.stat =FIELD_DP32 (s->regs.intrl0.stat, +GENET_INTRL_0 ,MDIO_DONE ,1 ); +break ;case BCM2838_GENET_TDMA_REGS ... BCM2838_GENET_TDMA_REGS + sizeof(BCM2838GenetRegsTdma) - 1: -qemu_log_mask (LOG_UNIMP , -"UMAC MDIO and TDMA aren't implemented yet" ); +qemu_log_mask (LOG_UNIMP ,"TDMA isn't implemented yet" );break ;default :break ;
@@ -463,6 +596,7 @@ static void bcm2838_genet_reset(Object *obj, ResetType type)trace_bcm2838_genet_reset ("done" ); +bcm2838_genet_set_qemu_mac (s);bcm2838_genet_phy_reset (s); }
include/hw/net/bcm2838_genet.h +2 -1
@@ -103,7 +103,8 @@ OBJECT_DECLARE_SIMPLE_TYPE(BCM2838GenetState, BCM2838_GENET)#define BCM2838_GENET_EXP_DATA BCM2838_GENET_PHY_REG(exp_data)#define BCM2838_GENET_EXP_SEL BCM2838_GENET_PHY_REG(exp_ctrl) -#define BCM2838_GENET_PHY_AUX_CTL_MISC 0x7 +#define BCM2838_GENET_PHY_AUX_CTL_AUXCTL 0x0 +#define BCM2838_GENET_PHY_AUX_CTL_MISC 0x7#define BCM2838_GENET_PHY_AUX_CTL_REGS_SIZE 8#define BCM2838_GENET_PHY_EXP_SHD_BLOCKS_CNT 256
Signature · 1 line
2.55.0
PATCH v6 15/17 hw/arm/bcm2838: enable BCM2838 GENET controller
In reply to Marcelo ManzoWire the BCM2838 GENET device (added in the previous patches) into the SoC: instantiate it in the peripherals block with qemu_configure_nic_device() so it picks up a -nic/-netdev from the command line, map its register region at the real BCM2711 physical offset, and route its two interrupt lines through the GIC. Also stop raspi4b from masking out the "brcm,bcm2711-genet-v5" device-tree node, now that the controller it describes is actually implemented.
Adapted from patches 19-29/41 of Sergey Kambalin's Raspberry Pi 4B series (patchwork series 829638) against current QEMU master.
--- hw/arm/bcm2838.c | 6 ++++++ hw/arm/bcm2838_peripherals.c | 11 +++++++++++ hw/arm/raspi4b.c | 1 - include/hw/arm/bcm2838_peripherals.h | 2 ++ 4 files changed, 19 insertions(+), 1 deletion(-)
hw/arm/bcm2838.c +6 -0
@@ -237,6 +237,12 @@ static void bcm2838_realize(DeviceState *dev, Error **errp) qdev_get_gpio_in(gicdev, int_n)); } +/* Connect Gigabit Ethernet controller to the interrupt controller */ +sysbus_connect_irq (SYS_BUS_DEVICE (&ps->genet),0 , +qdev_get_gpio_in (gicdev,GIC_SPI_INTERRUPT_GENET_A )); +sysbus_connect_irq (SYS_BUS_DEVICE (&ps->genet),1 , +qdev_get_gpio_in (gicdev,GIC_SPI_INTERRUPT_GENET_B )); +/* Pass through inbound GPIO lines to the GIC */ qdev_init_gpio_in (dev, bcm2838_gic_set_irq,GIC_NUM_IRQS );
hw/arm/bcm2838_peripherals.c +11 -0
@@ -44,6 +44,10 @@ static void bcm2838_peripherals_init(Object *obj)object_initialize_child (obj,"pcie-host" , &s->pcie_host,TYPE_BCM2838_PCIE_HOST ); +/* Gigabit Ethernet */ +object_initialize_child (obj,"genet" , &s->genet,TYPE_BCM2838_GENET ); +qemu_configure_nic_device (DEVICE (&s->genet), true,NULL ); +/* GPIO */ object_initialize_child (obj,"gpio" , &s->gpio,TYPE_BCM2838_GPIO );
@@ -217,6 +221,13 @@ static void bcm2838_peripherals_realize(DeviceState *dev, Error **errp)memory_region_add_subregion (get_system_memory (),PCIE_MMIO_ARM_OFFSET , &s->pcie_mmio_alias); +/* Gigabit Ethernet */ +if (!sysbus_realize (SYS_BUS_DEVICE (&s->genet), errp)) { +return ; + } + regs_mr =sysbus_mmio_get_region (SYS_BUS_DEVICE (&s->genet),0 ); +memory_region_add_subregion (&s->peri_low_mr,GENET_OFFSET , regs_mr); +/* GPIO */ if (!sysbus_realize (SYS_BUS_DEVICE (&s->gpio), errp)) {return ;
hw/arm/raspi4b.c +0 -1
@@ -65,7 +65,6 @@ static void raspi4_modify_dtb(const struct arm_boot_info *info, void *fdt)const char *nodes_to_remove[] = {"brcm,bcm2711-rng200" ,"brcm,bcm2711-thermal" , -"brcm,bcm2711-genet-v5" , };for (int i =0 ; i <ARRAY_SIZE (nodes_to_remove); i++) {
include/hw/arm/bcm2838_peripherals.h +2 -0
@@ -11,6 +11,7 @@#include "hw/arm/bcm2835_peripherals.h" #include "hw/arm/bcm2838_pcie.h" +#include "hw/net/bcm2838_genet.h" #include "hw/sd/sdhci.h" #include "hw/gpio/bcm2838_gpio.h"
@@ -70,6 +71,7 @@ struct BCM2838PeripheralState {SDHCIState emmc2;MemoryRegion pcie_mmio_alias;BCM2838PcieHostState pcie_host; +BCM2838GenetState genet;BCM2838GpioState gpio;OrIRQState mmc_irq_orgate;
Signature · 1 line
2.55.0
PATCH v6 12/17 hw/net/bcm2838_genet: implement GENET TX path
In reply to Marcelo ManzoFrom: Sergey Kambalin <[email protected]>
Implement the transmit DMA ring handling: walk TX descriptors between the consumer and producer index for a ring, pull frame data out of guest memory, and hand completed frames to the netdev peer via qemu_send_packet(), advancing the consumer index and raising the TXDMA_MBDONE interrupt as descriptors complete.
Part of Sergey Kambalin's original Raspberry Pi 4B PCIe/GENET networking series (patchwork series 829638, posted to qemu-devel in 2024, never merged). Carried forward and completed by Marcelo Manzo.
While carrying this forward, fix bcm2838_genet_tdma_ring_active():
it extracted the per-ring enable bit using the DMA_CTRL register's 1-bit EN field definition instead of DMA_RING_CFG's 17-bit EN field (one bit per ring), so only ring 0 ever appeared active and TX traffic hashed onto any other queue was silently dropped, eventually tripping the guest's qdisc watchdog ("NETDEV WATCHDOG: transmit queue N timed out") and breaking DHCP.
While carrying this forward, also:
- Check that a frame is at least a status block plus an Ethernet header before looking at it, and that the descriptors of a frame don't add up to more than the TX buffer, so a guest can't make us read or write outside it.
- Check that reading the frame data from guest memory succeeded.
- Use lduw_be_p()/stw_be_p() for the length/type field.
--- hw/net/bcm2838_genet.c | 244 ++++++++++++++++++++++++++++++++- include/hw/net/bcm2838_genet.h | 17 +++ 2 files changed, 260 insertions(+), 1 deletion(-)
hw/net/bcm2838_genet.c +243 -1
@@ -237,6 +237,13 @@ REG16(GENET_PHY_EXP_SEL, 0) FIELD(GENET_PHY_EXP_SEL, REG_ID, 0, 8) FIELD(GENET_PHY_EXP_SEL, BLOCK_ID, 8, 8) +REG32(GENET_TX_CSUM_INFO, 0) +FIELD(GENET_TX_CSUM_INFO, OFFSET, 0, 15) +FIELD(GENET_TX_CSUM_INFO, PROTO_UDP, 15, 1) +FIELD(GENET_TX_CSUM_INFO, START, 16, 15) +FIELD(GENET_TX_CSUM_INFO, LV, 30, 1) + +static void bcm2838_genet_set_qemu_mac (BCM2838GenetState *s) {const MACAddr *addr = &s->nic_conf.macaddr;
@@ -402,6 +409,241 @@ static uint64_t bcm2838_genet_mdio_cmd(BCM2838GenetState *s, uint64_t cmd)return cmd; } +/* Assemble a 64-bit value from its high and low 32-bit register halves */ +static uint64_t bcm2838_genet_make_u64 (uint32_t hi,uint32_t lo) +{ +return ((uint64_t )hi <<32 ) | lo; +} + +static void bcm2838_genet_xmit_packet (NetClientState *s,void *packet, +size_t size) +{ +uint8_t *buf = packet +sizeof (BCM2838GenetXmitStatus); +size_t len; +uint16_t len_type; + +/* We need the status block and a whole Ethernet header (len/type at 12) */ +if (size <sizeof (BCM2838GenetXmitStatus) +ETH_HLEN ) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: TX frame too short (%zu bytes)\n" , __func__, size); +return ; + } + + len = size -sizeof (BCM2838GenetXmitStatus); +net_checksum_calculate (buf, len,CSUM_ALL ); + + len_type =lduw_be_p (buf +12 ); +if (len_type <MAX_PAYLOAD_SIZE ) { +stw_be_p (buf +12 , len); + } + +qemu_send_packet (s, buf, len); +} + +static uint64_t bcm2838_genet_tx (BCM2838GenetState *s,unsigned int ring_index, +uint32_t prod_index, +uint32_t cons_index) +{ +BCM2838GenetTdmaRing *ring = &s->regs.tdma.rings[ring_index]; +const unsigned int desc_size_words + =sizeof (BCM2838GenetTdmaDesc) /sizeof (uint32_t ); +const uint64_t ring_start_addr + =bcm2838_genet_make_u64 (ring->start_addr_hi, ring->start_addr); +const uint64_t ring_end_addr + =bcm2838_genet_make_u64 (ring->end_addr_hi, ring->end_addr); + +hwaddr data_addr; +uint64_t desc_index; +uint32_t desc_status =0 ; +uint32_t buflength =0 ; +uint64_t num_descs =0 ; +uint64_t read_ptr + =bcm2838_genet_make_u64 (ring->read_ptr_hi, ring->read_ptr); +size_t packet_off =0 ; + +uint32_t prod_index_fld =FIELD_EX32 (prod_index, +GENET_DMA_PROD_INDEX ,INDEX ); +uint32_t cons_index_fld =FIELD_EX32 (cons_index, +GENET_DMA_CONS_INDEX ,INDEX ); + +while (cons_index_fld != prod_index_fld) { + desc_index = read_ptr / desc_size_words; +if (desc_index >=BCM2838_GENET_DMA_DESC_CNT ) { +qemu_log_mask ( +LOG_GUEST_ERROR , +"%s: invalid TX descriptor index %" PRIu64" (exceeds %u)\n" , + __func__, desc_index,BCM2838_GENET_DMA_DESC_CNT -1 ); +break ; + } + desc_status = s->regs.tdma.descs[desc_index].length_status; + data_addr =bcm2838_genet_make_u64 ( + s->regs.tdma.descs[desc_index].address_hi, + s->regs.tdma.descs[desc_index].address_lo); +trace_bcm2838_genet_tx (ring_index, desc_index, desc_status, + data_addr); + +if (FIELD_EX32 (desc_status,GENET_RDMA_LENGTH_STATUS ,SOP ) !=0 ) { + packet_off =0 ; + } + + buflength =FIELD_EX32 (desc_status, +GENET_RDMA_LENGTH_STATUS ,BUFLENGTH ); + +if (packet_off + buflength >sizeof (s->tx_packet)) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: TX frame too big (%zu + %u bytes, max %zu)\n" , + __func__, packet_off, buflength, +sizeof (s->tx_packet)); +break ; + } + +if (address_space_read (&s->dma_as, data_addr, +MEMTXATTRS_UNSPECIFIED , + s->tx_packet + packet_off, + buflength) !=MEMTX_OK ) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: failed to read TX buffer at 0x%" HWADDR_PRIx +"\n" , __func__, data_addr); +break ; + } + packet_off += buflength; + +if (FIELD_EX32 (desc_status,GENET_RDMA_LENGTH_STATUS ,EOP ) !=0 ) { +bcm2838_genet_xmit_packet (qemu_get_queue (s->nic), s->tx_packet, + packet_off); + packet_off =0 ; + } + + num_descs++; + cons_index_fld++; + s->regs.tdma.descs[desc_index].length_status = +FIELD_DP32 (s->regs.tdma.descs[desc_index].length_status, +GENET_RDMA_LENGTH_STATUS ,OWN ,1 ); + read_ptr = read_ptr == ring_end_addr +1 - desc_size_words + ? ring_start_addr : read_ptr + desc_size_words; + } + + ring->read_ptr = read_ptr; + ring->read_ptr_hi = read_ptr >>32 ; + +return num_descs; +} + +static bool bcm2838_genet_tdma_ring_active (BCM2838GenetState *s, +unsigned int ring_index) +{ +uint32_t ctrl_reg = s->regs.tdma.ctrl; +uint32_t ring_cfg_reg = s->regs.tdma.ring_cfg; +uint32_t ring_mask =1 << ring_index; +bool dma_en =FIELD_EX32 (ctrl_reg,GENET_DMA_CTRL ,EN ); +bool ring_en = +FIELD_EX32 (ring_cfg_reg,GENET_DMA_RING_CFG ,EN ) & ring_mask; +bool ring_buf_en = +FIELD_EX32 (ctrl_reg,GENET_DMA_CTRL ,RING_BUF_EN ) & ring_mask; +bool active = dma_en && ring_en && ring_buf_en; + +trace_bcm2838_genet_tx_dma_ring_active (ring_index, + active ?"active" :"halted" ); +return active; +} + +/* The guest wrote a ring's producer index: send what it has queued */ +static void bcm2838_genet_tdma_prod_index_write (BCM2838GenetState *s, +unsigned int ring_index, +uint32_t prod_index) +{ +BCM2838GenetTdmaRing *ring = &s->regs.tdma.rings[ring_index]; +uint32_t cons_index = ring->cons_index; +uint32_t cons_index_fld =FIELD_EX32 (cons_index, +GENET_DMA_CONS_INDEX ,INDEX ); +uint32_t prod_index_fld =FIELD_EX32 (prod_index, +GENET_DMA_PROD_INDEX ,INDEX ); +uint64_t num_descs_tx; +uint32_t tx_intrs; + +if (cons_index_fld == prod_index_fld) { +return ; + } + +trace_bcm2838_genet_tx_request (ring_index, prod_index_fld, + cons_index_fld); + num_descs_tx =bcm2838_genet_tx (s, ring_index, prod_index, cons_index); +if (num_descs_tx ==0 ) { +return ; + } + + ring->cons_index =FIELD_DP32 (ring->cons_index,GENET_DMA_CONS_INDEX , +INDEX , cons_index + num_descs_tx); + +if (ring_index ==BCM2838_GENET_DMA_RING_DEFAULT ) { + s->regs.intrl0.stat =FIELD_DP32 (s->regs.intrl0.stat,GENET_INTRL_0 , +TXDMA_MBDONE ,1 ); + }else { + tx_intrs =FIELD_EX32 (s->regs.intrl1.stat,GENET_INTRL_1 ,TX_INTRS ); + s->regs.intrl1.stat =FIELD_DP32 (s->regs.intrl1.stat,GENET_INTRL_1 , +TX_INTRS , tx_intrs |1 << ring_index); + } +} + +static void bcm2838_genet_tdma (BCM2838GenetState *s,hwaddr offset, +uint64_t value) +{ +hwaddr ring_offset; +unsigned int ring_index; +uint32_t prod_index = value; +uint32_t ring_cfg = value; +uint32_t dma_ctrl = value; + +uint32_t exst_tdma_en = +FIELD_EX32 (s->regs.tdma.ctrl,GENET_DMA_CTRL ,EN ); +uint32_t exst_ring_en = +FIELD_EX32 (s->regs.tdma.ring_cfg,GENET_DMA_RING_CFG ,EN ); +uint32_t incm_tdma_en = +FIELD_EX32 (dma_ctrl,GENET_DMA_CTRL ,EN ); +uint32_t incm_ring_en = +FIELD_EX32 (ring_cfg,GENET_DMA_RING_CFG ,EN ); +uint32_t incm_ring_buf_en = +FIELD_EX32 (dma_ctrl,GENET_DMA_CTRL ,RING_BUF_EN ); + +switch (offset) { +case BCM2838_GENET_TDMA_RINGS + ... BCM2838_GENET_TDMA_RINGS + sizeof(s->regs.tdma.rings) - 1: + ring_index = (offset -BCM2838_GENET_TDMA_RINGS ) + /sizeof (BCM2838GenetTdmaRing); +if (bcm2838_genet_tdma_ring_active (s, ring_index)) { + ring_offset = offset -BCM2838_GENET_TDMA_RINGS + - ring_index *sizeof (BCM2838GenetTdmaRing); +switch (ring_offset) { +case BCM2838_GENET_TRING_PROD_INDEX : +bcm2838_genet_tdma_prod_index_write (s, ring_index, + prod_index); +break ; +default : +break ; + } + } +break ; +case BCM2838_GENET_TDMA_RING_CFG : +if (exst_ring_en != incm_ring_en) { +trace_bcm2838_genet_tx_dma_ring (incm_ring_en); + } +break ; +case BCM2838_GENET_TDMA_CTRL : + s->regs.tdma.status = (~dma_ctrl) &GENET_DMA_ENABLE_MASK ; +if (exst_tdma_en != incm_tdma_en) { +trace_bcm2838_genet_tx_dma (incm_tdma_en ==1 + ?"enabled" + :"disabled" ); + } +if (exst_ring_en != incm_ring_buf_en) { +trace_bcm2838_genet_tx_dma_ring_buf (incm_ring_buf_en); + } +break ; +default : +break ; + } +} +static uint64_t bcm2838_genet_read (void *opaque,hwaddr offset,unsigned size) {uint64_t value = ~0 ;
@@ -500,7 +742,7 @@ static void bcm2838_genet_write(void *opaque, hwaddr offset, uint64_t value,break ;case BCM2838_GENET_TDMA_REGS ... BCM2838_GENET_TDMA_REGS + sizeof(BCM2838GenetRegsTdma) - 1: -qemu_log_mask (LOG_UNIMP ,"TDMA isn't implemented yet" ); +bcm2838_genet_tdma (s, offset, value);break ;default :break ;
include/hw/net/bcm2838_genet.h +17 -0
@@ -110,6 +110,21 @@ OBJECT_DECLARE_SIMPLE_TYPE(BCM2838GenetState, BCM2838_GENET)#define BCM2838_GENET_PHY_EXP_SHD_BLOCKS_CNT 256#define BCM2838_GENET_PHY_EXP_SHD_REGS_CNT 256 +#define MAX_FRAME_SIZE 0xFFF +#define MAX_PACKET_SIZE 1518 +#define MAX_PAYLOAD_SIZE 1500 +#define TX_MIN_PKT_SIZE 60 + + +typedef struct BCM2838GenetXmitStatus { +uint32_t length_status;/* length and peripheral status */ +uint32_t ext_status;/* Extended status */ +uint32_t rx_csum;/* partial rx checksum */ +uint32_t unused1[9 ];/* unused */ +uint32_t tx_csum_info;/* Tx checksum info. */ +uint32_t unused2[3 ];/* unused */ +}BCM2838GenetXmitStatus ; + typedef struct { uint32_t rev_ctrl; uint32_t port_ctrl;
@@ -402,6 +417,8 @@ struct BCM2838GenetState {qemu_irq irq_default;qemu_irq irq_prio; + +uint8_t tx_packet[MAX_FRAME_SIZE ]; };/*
Signature · 1 line
2.55.0
PATCH v6 17/17 tests/functional/aarch64: add raspi4b GENET networking test
In reply to Marcelo ManzoAdd test_arm_raspi4_genet, exercising the raspi4b GENET controller end to end: boot with a -nic bcm2838-genet device attached, confirm the guest driver probes it ("GENET 5.0 EPHY" in the kernel log), reach a shell, and confirm the link actually comes up ("ip link show eth0" reports LOWER_UP) rather than just that the device exists.
Verified locally against this series with the exact kernel/initrd assets this file already pins (raspberrypi-kernel_1.20230106-1 / groeck linux-build-test rootfs). The link only comes up because the MDIO patch handles the PHY autonegotiation-restart request (BMCR ANRESTART) correctly; without that the link never comes up and this test would hang at the "ip link show eth0" step, so it also serves as a regression test for it.
--- tests/functional/aarch64/test_raspi4.py | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
tests/functional/aarch64/test_raspi4.py +31 -0
@@ -109,5 +109,36 @@ def test_arm_raspi4_initrd(self): #self.vm.wait() + def test_arm_raspi4_genet(self): + self.require_netdev('user') + kernel_path = self.archive_extract(self.ASSET_KERNEL_20190215, + member='boot/kernel8.img') + dtb_path = self.archive_extract(self.ASSET_KERNEL_20190215, + member='boot/bcm2711-rpi-4-b.dtb') + initrd_path = self.uncompress(self.ASSET_INITRD) + + self.set_machine('raspi4b') + self.vm.set_console() + kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + + 'earlycon=pl011,mmio32,0xfe201000 ' + + 'console=ttyAMA0,115200 ' + + 'panic=-1 noreboot ' + + 'dwc_otg.fiq_fsm_enable=0') + self.vm.add_args('-kernel', kernel_path, + '-dtb', dtb_path, + '-initrd', initrd_path, + '-append', kernel_command_line, + '-no-reboot', + '-nic', 'user,model=bcm2838-genet') + self.vm.launch() + self.wait_for_console_pattern( + 'bcmgenet fd580000.ethernet: GENET 5.0 EPHY') + self.wait_for_console_pattern('Boot successful.') + + exec_command_and_wait_for_pattern(self, 'ip link show eth0', + 'LOWER_UP') + exec_command_and_wait_for_pattern(self, 'halt', 'reboot: System halted') + + if __name__ == '__main__': LinuxKernelTest.main()
Signature · 1 line
2.55.0
PATCH v6 2/17 hw/arm/bcm2838_pcie: add BCM2838 PCIe host
In reply to Marcelo ManzoAdd the BCM2838 PCIe host bridge model: config space, MMIO window, and register access for the root port.
Per Peter Maydell's review of a later follow-up series, fold in what was originally a separate bugfix here, since this device isn't in upstream git yet and the bug was in code this patch itself introduces. Two defects fixed, both on the enumeration path (none of this was exercised until a later patch actually attaches a PCI device):
1. bcm2838_pcie_host_read/write computed
"offset - PCIE_CONFIG_SPACE_SIZE" on an unsigned hwaddr, which underflows for every offset below 4KB. The root port's own config space -- including vendor/device ID at offset 0 -- therefore always fell into the out-of-range branch and read back as all-ones, i.e. "no device present", so the guest stopped scanning immediately.
2. The root port overrode PCIDeviceClass::config_read/config_write with plain pci_default_*_config(). That bypasses rp_write_config() -> pci_bridge_write_config(), so programming the bridge's memory window never updated the bridge's address space and BARs behind the root port stayed unreachable. Drop the overrides and dispatch through pci_host_config_{read,write}_common() so the device's real handlers run.
Accesses to the EXT_CFG_DATA window are passed on to pci_host_config_{read,write}_common() for the function that EXT_CFG_INDEX selects.
--- hw/arm/bcm2838_pcie.c | 212 ++++++++++++++++++++++++++++++++++ hw/arm/trace-events | 4 + include/hw/arm/bcm2838_pcie.h | 19 +++ 3 files changed, 235 insertions(+)
hw/arm/bcm2838_pcie.c +212 -0
@@ -11,6 +11,7 @@#include "qapi/error.h" #include "hw/core/irq.h" #include "hw/pci-host/gpex.h" +#include "hw/pci/pci_host.h" #include "hw/core/qdev-properties.h" #include "migration/vmstate.h" #include "qemu/module.h"
@@ -19,6 +20,216 @@#include "hw/arm/bcm2838_pcie.h" #include "trace.h" +/* +* The register bytes in root_port.regs[] are little-endian whatever the host +* is, so access them with the ld*_le_p()/st*_le_p() helpers. +*/ +static uint32_t bcm2838_pcie_ext_cfg_index (const uint8_t *root_regs) +{ +return ldl_le_p (root_regs +BCM2838_PCIE_EXT_CFG_INDEX + -PCIE_CONFIG_SPACE_SIZE ); +} + +/* +* EXT_CFG_INDEX selects the bus/device/function (in ECAM address layout) that +* the EXT_CFG_DATA window accesses; the offset within the window selects the +* register in that function's config space. +*/ +static PCIDevice *bcm2838_pcie_ext_cfg_dev (BCM2838PcieHostState *s, +hwaddr data_offset, +uint32_t *conf_offset) +{ +uint32_t mmcfg_addr =bcm2838_pcie_ext_cfg_index (s->root_port.regs) + |PCIE_MMCFG_CONFOFFSET (data_offset); + + *conf_offset =PCIE_MMCFG_CONFOFFSET (mmcfg_addr); +return pci_find_device (PCI_HOST_BRIDGE (s)->bus, +PCIE_MMCFG_BUS (mmcfg_addr), +PCIE_MMCFG_DEVFN (mmcfg_addr)); +} + +static uint64_t bcm2838_pcie_host_read (void *opaque,hwaddr offset, +unsigned size) { +uint64_t value = ~0 ; +BCM2838PcieHostState *s = opaque; +uint8_t *root_regs = s->root_port.regs; +PCIDevice *pci_dev; +uint32_t conf_offset; + +if (offset <PCIE_CONFIG_SPACE_SIZE ) { +/* +* The first 4KB of the window is the root port's own PCI config +* space (vendor/device ID, BARs, capabilities). Serve it from the +* real PCIDevice, not from the raw regs[] shadow buffer, which is +* only backing store for the controller registers above 4KB. +*/ + value =pci_host_config_read_common (PCI_DEVICE (&s->root_port), + offset,PCIE_CONFIG_SPACE_SIZE , + size); + }else if (offset -PCIE_CONFIG_SPACE_SIZE + size + <=sizeof (s->root_port.regs)) { +switch (offset) { +case BCM2838_PCIE_EXT_CFG_DATA + ... BCM2838_PCIE_EXT_CFG_DATA + PCIE_CONFIG_SPACE_SIZE - 1: + pci_dev =bcm2838_pcie_ext_cfg_dev ( + s, offset -BCM2838_PCIE_EXT_CFG_DATA , &conf_offset); +if (pci_dev) { + value =pci_host_config_read_common ( + pci_dev, conf_offset,pci_config_size (pci_dev), size); + } +break ; +default : + value =ldn_le_p (root_regs + offset -PCIE_CONFIG_SPACE_SIZE , size); + } + }else { +qemu_log_mask ( +LOG_GUEST_ERROR , +"%s: out-of-range access, %u bytes @ offset 0x%04" PRIx64"\n" , + __func__, size, offset); + } + +trace_bcm2838_pcie_host_read (size, offset, value); +return value; +} + +static void bcm2838_pcie_host_write (void *opaque,hwaddr offset, +uint64_t value,unsigned size) { +BCM2838PcieHostState *s = opaque; +uint8_t *root_regs = s->root_port.regs; +PCIDevice *pci_dev; +uint32_t conf_offset; + +trace_bcm2838_pcie_host_write (size, offset, value); + +if (offset <PCIE_CONFIG_SPACE_SIZE ) { +/* Root port's own PCI config space -- see read path above */ +pci_host_config_write_common (PCI_DEVICE (&s->root_port), offset, +PCIE_CONFIG_SPACE_SIZE , value, size); + }else if (offset -PCIE_CONFIG_SPACE_SIZE + size + <=sizeof (s->root_port.regs)) { +switch (offset) { +case BCM2838_PCIE_EXT_CFG_DATA + ... BCM2838_PCIE_EXT_CFG_DATA + PCIE_CONFIG_SPACE_SIZE - 1: + pci_dev =bcm2838_pcie_ext_cfg_dev ( + s, offset -BCM2838_PCIE_EXT_CFG_DATA , &conf_offset); +if (pci_dev) { +pci_host_config_write_common ( + pci_dev, conf_offset,pci_config_size (pci_dev), + value, size); + } +break ; +default : +stn_le_p (root_regs + offset -PCIE_CONFIG_SPACE_SIZE , size, value); + } + }else { +qemu_log_mask ( +LOG_GUEST_ERROR , +"%s: out-of-range access, %u bytes @ offset 0x%04" PRIx64"\n" , + __func__, size, offset); + } +} + +static const MemoryRegionOps bcm2838_pcie_host_ops = { + .read = bcm2838_pcie_host_read, + .write = bcm2838_pcie_host_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .impl = {.max_access_size =8 }, +}; + +static void bcm2838_pcie_host_set_irq (void *opaque,int irq_num,int level) +{ +BCM2838PcieHostState *s = opaque; + +qemu_set_irq (s->irq[irq_num], level); +} + +static int bcm2838_pcie_host_map_irq (PCIDevice *pci_dev,int pin) +{ +/* Only slot 0 exists behind the root port: nothing to swizzle */ +return pin; +} + +static void bcm2838_pcie_host_realize (DeviceState *dev,Error **errp) +{ +PCIHostState *pci =PCI_HOST_BRIDGE (dev); +BCM2838PcieHostState *s =BCM2838_PCIE_HOST (dev); +SysBusDevice *sbd =SYS_BUS_DEVICE (dev); + +int i; + +memory_region_init_io (&s->cfg_regs,OBJECT (s), &bcm2838_pcie_host_ops, s, +"bcm2838_pcie_cfg_regs" ,BCM2838_PCIE_REGS_SIZE ); +sysbus_init_mmio (sbd, &s->cfg_regs); + +/* +* io_mmio and io_ioport are the PCI view of the PCI address space that we +* pass to pci_register_root_bus(). The io_mmio_window and io_ioport_window +* containers hold them as subregions and are what we expose as SysBus +* MRs: accesses to addresses where no PCI device has been mapped read as +* -1 and ignore writes. +*/ +memory_region_init (&s->io_mmio,OBJECT (s),"bcm2838_pcie_mmio" ,UINT64_MAX ); +memory_region_init (&s->io_ioport,OBJECT (s),"bcm2838_pcie_ioport" , +64 *1024 ); + +memory_region_init_io (&s->io_mmio_window,OBJECT (s), + &unassigned_io_ops,OBJECT (s), +"bcm2838_pcie_mmio_window" ,UINT64_MAX ); +memory_region_init_io (&s->io_ioport_window,OBJECT (s), + &unassigned_io_ops,OBJECT (s), +"bcm2838_pcie_ioport_window" ,64 *1024 ); + +memory_region_add_subregion (&s->io_mmio_window,0 , &s->io_mmio); +memory_region_add_subregion (&s->io_ioport_window,0 , &s->io_ioport); +sysbus_init_mmio (sbd, &s->io_mmio_window); +sysbus_init_mmio (sbd, &s->io_ioport_window); + +for (i =0 ; i <BCM2838_PCIE_NUM_IRQS ; i++) { +sysbus_init_irq (sbd, &s->irq[i]); + } + + pci->bus =pci_register_root_bus (dev,"pcie.0" , bcm2838_pcie_host_set_irq, + bcm2838_pcie_host_map_irq, s, &s->io_mmio, + &s->io_ioport,0 ,BCM2838_PCIE_NUM_IRQS , +TYPE_PCIE_BUS ); +qdev_realize (DEVICE (&s->root_port),BUS (pci->bus), &error_fatal); +} + +static const char *bcm2838_pcie_host_root_bus_path (PCIHostState *host_bridge, +PCIBus *rootbus) +{ +return "0000:00" ; +} + +static void bcm2838_pcie_host_class_init (ObjectClass *class,const void *data) +{ +DeviceClass *dc =DEVICE_CLASS (class); +PCIHostBridgeClass *hc =PCI_HOST_BRIDGE_CLASS (class); + + hc->root_bus_path = bcm2838_pcie_host_root_bus_path; + dc->realize = bcm2838_pcie_host_realize; +set_bit (DEVICE_CATEGORY_BRIDGE , dc->categories); + dc->fw_name ="pci" ; +} + +static void bcm2838_pcie_host_initfn (Object *obj) +{ +BCM2838PcieHostState *s =BCM2838_PCIE_HOST (obj); +BCM2838PcieRootState *root = &s->root_port; + +object_initialize_child (obj,"root_port" , root,TYPE_BCM2838_PCIE_ROOT ); +qdev_prop_set_int32 (DEVICE (root),"addr" ,PCI_DEVFN (0 ,0 )); +qdev_prop_set_bit (DEVICE (root),"multifunction" , false); +} + +static const TypeInfo bcm2838_pcie_host_info = { + .name =TYPE_BCM2838_PCIE_HOST , + .parent =TYPE_PCIE_HOST_BRIDGE , + .instance_size =sizeof (BCM2838PcieHostState), + .instance_init = bcm2838_pcie_host_initfn, + .class_init = bcm2838_pcie_host_class_init, +}; +/* * RC root part (D0:F0) */
@@ -96,6 +307,7 @@ static const TypeInfo bcm2838_pcie_root_info = {static void bcm2838_pcie_register (void ) {type_register_static (&bcm2838_pcie_root_info); +type_register_static (&bcm2838_pcie_host_info); }type_init (bcm2838_pcie_register)
hw/arm/trace-events +4 -0
@@ -96,3 +96,7 @@ z2_aer915_event(int8_t event, int8_t len) "i2c event =0x%x len=%d bytes" # bcm2838.c bcm2838_gic_set_irq(int irq, int level) "gic irq:%d lvl:%d" + +# bcm2838_pcie.c +bcm2838_pcie_host_read(unsigned int size, uint64_t offset, uint64_t value) "%u bytes @ 0x%04"PRIx64": 0x%016"PRIx64 +bcm2838_pcie_host_write(unsigned int size, uint64_t offset, uint64_t value) "%u bytes @ 0x%04"PRIx64": 0x%016"PRIx64
include/hw/arm/bcm2838_pcie.h +19 -0
@@ -16,6 +16,9 @@#include "hw/pci/pcie_port.h" #include "qom/object.h" +#define TYPE_BCM2838_PCIE_HOST "bcm2838-pcie-host" +OBJECT_DECLARE_SIMPLE_TYPE(BCM2838PcieHostState, BCM2838_PCIE_HOST) +#define TYPE_BCM2838_PCIE_ROOT "bcm2838-pcie-root"OBJECT_DECLARE_TYPE (BCM2838PcieRootState, BCM2838PcieRootClass,BCM2838_PCIE_ROOT )
@@ -51,4 +54,20 @@ struct BCM2838PcieRootClass { }; +struct BCM2838PcieHostState { +/*< private >*/ +PCIExpressHost parent_obj; + +/*< public >*/ +BCM2838PcieRootState root_port; + +MemoryRegion cfg_regs; +MemoryRegion io_ioport; +MemoryRegion io_mmio; +MemoryRegion io_ioport_window; +MemoryRegion io_mmio_window; + +qemu_irq irq[BCM2838_PCIE_NUM_IRQS ]; +}; +#endif /* BCM2838_PCIE_H */
Signature · 1 line
2.55.0
PATCH v6 8/17 hw/net/bcm2838_genet: add GENET register structs, part 4/4
In reply to Marcelo ManzoFrom: Sergey Kambalin <[email protected]>
Add the PHY "shadow" register structs (BCM54213PE PHY-specific clock control, RGMII mode, LED, and expansion-block registers accessed via the AUX_CTL/EXP_SHD indirection scheme) used by the MDIO emulation added in a later patch.
Part of Sergey Kambalin's original Raspberry Pi 4B PCIe/GENET networking series (patchwork series 829638, posted to qemu-devel in 2024, never merged). Carried forward and completed by Marcelo Manzo.
--- include/hw/net/bcm2838_genet.h | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
include/hw/net/bcm2838_genet.h +35 -0
@@ -25,6 +25,12 @@ OBJECT_DECLARE_SIMPLE_TYPE(BCM2838GenetState, BCM2838_GENET)#define BCM2838_GENET_HFB_FILTER_CNT 48#define BCM2838_GENET_HFB_FILTER_SIZE 128 +#define BCM2838_GENET_PHY_AUX_CTL_MISC 0x7 +#define BCM2838_GENET_PHY_AUX_CTL_REGS_SIZE 8 + +#define BCM2838_GENET_PHY_EXP_SHD_BLOCKS_CNT 256 +#define BCM2838_GENET_PHY_EXP_SHD_REGS_CNT 256 + typedef struct { uint32_t rev_ctrl; uint32_t port_ctrl;
@@ -273,6 +279,32 @@ typedef struct {uint16_t rdb_data; } BCM2838GenetPhyRegs; +typedef struct { +uint16_t reserved_0_2[3 ]; +uint16_t clk_ctl; +uint16_t scr2; +uint16_t scr3; +uint16_t reserved_6_9[4 ]; +uint16_t apd; +uint16_t rgmii_mode; +uint16_t reserved_12; +uint16_t leds1; +uint16_t reserved_14_18[5 ]; +uint16_t _100fx_ctrl; +uint16_t ssd; +uint16_t reserved_21_30[10 ]; +uint16_t mode; +}BCM2838GenetPhyShdRegs ; + +typedef struct { +uint16_t regs[BCM2838_GENET_PHY_AUX_CTL_REGS_SIZE ]; +}BCM2838GenetPhyAuxShdRegs ; + +typedef struct { +uint16_t regs[BCM2838_GENET_PHY_EXP_SHD_BLOCKS_CNT ] + [BCM2838_GENET_PHY_EXP_SHD_REGS_CNT ]; +}BCM2838GenetPhyExpShdRegs ; +struct BCM2838GenetState {/*< private >*/ SysBusDevice parent_obj;
@@ -284,6 +316,9 @@ struct BCM2838GenetState {BCM2838GenetRegs regs;BCM2838GenetPhyRegs phy_regs; +BCM2838GenetPhyShdRegs phy_shd_regs; +BCM2838GenetPhyAuxShdRegs phy_aux_ctl_shd_regs; +BCM2838GenetPhyExpShdRegs phy_exp_shd_regs;qemu_irq irq_default;qemu_irq irq_prio;
Signature · 1 line
2.55.0
PATCH v6 14/17 hw/net/bcm2838_genet: add migration state
In reply to Marcelo ManzoThe device had no VMStateDescription, so none of its register or PHY state was saved or migrated.
The MAC register block and the PHY registers are structs that contain nothing but uint32_t (MAC) or uint16_t (PHY) fields. Overlay each with an array of the same element type, in an anonymous union, so that the rest of the code keeps its s->regs.foo style access, and migrate each block as an array of that type. Unlike describing them as byte buffers, this is correct when migrating between hosts of different endianness.
The interrupt outputs are derived from the registers and need no handling after load: the device at the other end of each line migrates its own state.
--- hw/net/bcm2838_genet.c | 23 +++++++++++++++++++++++ include/hw/net/bcm2838_genet.h | 22 +++++++++++++++++++--- 2 files changed, 42 insertions(+), 3 deletions(-)
hw/net/bcm2838_genet.c +23 -0
@@ -17,6 +17,7 @@#include "system/dma.h" #include "hw/core/registerfields.h" #include "hw/core/resettable.h" +#include "migration/vmstate.h" #include "hw/net/bcm2838_genet.h" #include "trace.h"
@@ -1135,6 +1136,27 @@ static const Property genet_properties[] = { DEFINE_NIC_PROPERTIES(BCM2838GenetState, nic_conf), }; +static const VMStateDescription vmstate_bcm2838_genet = { + .name ="bcm2838-genet" , + .version_id =1 , + .minimum_version_id =1 , + .fields = (const VMStateField []) { +VMSTATE_UINT32_ARRAY (regs_u32, BCM2838GenetState, +sizeof (BCM2838GenetRegs) /sizeof (uint32_t )), +VMSTATE_UINT16_ARRAY (phy_regs_u16, BCM2838GenetState, +sizeof (BCM2838GenetPhyRegs) /sizeof (uint16_t )), +VMSTATE_UINT16_ARRAY (phy_shd_regs_u16, BCM2838GenetState, +sizeof (BCM2838GenetPhyShdRegs) + /sizeof (uint16_t )), +VMSTATE_UINT16_ARRAY (phy_aux_ctl_shd_regs.regs, BCM2838GenetState, +BCM2838_GENET_PHY_AUX_CTL_REGS_SIZE ), +VMSTATE_UINT16_2DARRAY (phy_exp_shd_regs.regs, BCM2838GenetState, +BCM2838_GENET_PHY_EXP_SHD_BLOCKS_CNT , +BCM2838_GENET_PHY_EXP_SHD_REGS_CNT ), +VMSTATE_END_OF_LIST () + } +}; +static void bcm2838_genet_class_init (ObjectClass *class,const void *data) {static ResettablePhases unused_parent_phases;
@@ -1142,6 +1164,7 @@ static void bcm2838_genet_class_init(ObjectClass *class, const void *data)ResettableClass *rc =RESETTABLE_CLASS (class); dc->realize = bcm2838_genet_realize; + dc->vmsd = &vmstate_bcm2838_genet;resettable_class_set_parent_phases (rc,NULL , bcm2838_genet_reset,NULL , &unused_parent_phases);device_class_set_props (dc, genet_properties);
include/hw/net/bcm2838_genet.h +19 -3
@@ -409,9 +409,25 @@ struct BCM2838GenetState {MemoryRegion regs_mr;AddressSpace dma_as; -BCM2838GenetRegs regs; -BCM2838GenetPhyRegs phy_regs; -BCM2838GenetPhyShdRegs phy_shd_regs; +/* +* The register blocks are named structs that contain nothing but +* uint32_t (MAC) or uint16_t (PHY) fields. Overlay each with an array +* of the same element type so that the whole block can be migrated +* with a single VMSTATE_UINT32_ARRAY()/VMSTATE_UINT16_ARRAY(). +*/ +union { +BCM2838GenetRegs regs; +uint32_t regs_u32[sizeof (BCM2838GenetRegs) /sizeof (uint32_t )]; + }; +union { +BCM2838GenetPhyRegs phy_regs; +uint16_t phy_regs_u16[sizeof (BCM2838GenetPhyRegs) /sizeof (uint16_t )]; + }; +union { +BCM2838GenetPhyShdRegs phy_shd_regs; +uint16_t phy_shd_regs_u16[sizeof (BCM2838GenetPhyShdRegs) + /sizeof (uint16_t )]; + };BCM2838GenetPhyAuxShdRegs phy_aux_ctl_shd_regs;BCM2838GenetPhyExpShdRegs phy_exp_shd_regs;
Signature · 1 line
2.55.0
PATCH v6 4/17 hw/net/bcm2838_genet: add GENET stub device
In reply to Marcelo ManzoAdd a bare-bones BCM2838 GENET (Gigabit Ethernet) sysbus device:
MMIO region registration, an out-of-range read/write handler that just logs, and a reset callback. No register-level behavior yet; that comes in the following patches.
Adapted from patch 19/41 of Sergey Kambalin's Raspberry Pi 4B series (patchwork series 829638) against current QEMU master. The reset callback and class_init() use the current Resettable-phases API (ResettableClass / resettable_class_set_parent_phases()) rather than the plain DeviceClass::reset hook the original patch used, and headers moved under hw/core/, both API changes since the series was originally posted.
--- hw/net/bcm2838_genet.c | 103 +++++++++++++++++++++++++++ hw/net/meson.build | 2 + hw/net/trace-events | 16 +++++ include/hw/arm/bcm2838_peripherals.h | 2 + include/hw/net/bcm2838_genet.h | 40 +++++++++++ 5 files changed, 163 insertions(+) create mode 100644 hw/net/bcm2838_genet.c create mode 100644 include/hw/net/bcm2838_genet.h
hw/net/bcm2838_genet.c new file +103 -0
@@ -0,0 +1,103 @@ +/* +* BCM2838 Gigabit Ethernet emulation +* +* Copyright (C) 2022 Ovchinnikov Vitalii <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "qemu/module.h" +#include "qemu/log.h" +#include "qemu/error-report.h" +#include "net/eth.h" +#include "qapi/error.h" +#include "hw/core/irq.h" +#include "net/checksum.h" +#include "system/dma.h" +#include "hw/core/resettable.h" +#include "hw/net/bcm2838_genet.h" +#include "trace.h" + + +static uint64_t bcm2838_genet_read (void *opaque,hwaddr offset,unsigned size) +{ +uint64_t value = ~0 ; + +qemu_log_mask ( +LOG_GUEST_ERROR , +"%s: out-of-range access, %u bytes @ offset 0x%04" PRIx64"\n" , + __func__, size, offset); + +trace_bcm2838_genet_read (size, offset, value); +return value; +} + +static void bcm2838_genet_write (void *opaque,hwaddr offset,uint64_t value, +unsigned size) { +qemu_log_mask ( +LOG_GUEST_ERROR , +"%s: out-of-range access, %u bytes @ offset 0x%04" PRIx64"\n" , + __func__, size, offset); +} + +static const MemoryRegionOps bcm2838_genet_ops = { + .read = bcm2838_genet_read, + .write = bcm2838_genet_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .impl = {.min_access_size =4 , .max_access_size =4 }, + .valid = {.min_access_size =4 , .max_access_size =4 }, +}; + + +static void bcm2838_genet_realize (DeviceState *dev,Error **errp) +{ +BCM2838GenetState *s =BCM2838_GENET (dev); +SysBusDevice *sbd =SYS_BUS_DEVICE (dev); + +/* Controller registers */ +memory_region_init_io (&s->regs_mr,OBJECT (s), &bcm2838_genet_ops, s, +"bcm2838_genet_regs" ,sizeof (s->regs)); +sysbus_init_mmio (sbd, &s->regs_mr); +} + +static void bcm2838_genet_phy_reset (BCM2838GenetState *s) +{ +trace_bcm2838_genet_phy_reset ("done" ); +} + +static void bcm2838_genet_reset (Object *obj,ResetType type) +{ +BCM2838GenetState *s =BCM2838_GENET (obj); + +memset (&s->regs,0x00 ,sizeof (s->regs)); + +trace_bcm2838_genet_reset ("done" ); + +bcm2838_genet_phy_reset (s); +} + +static void bcm2838_genet_class_init (ObjectClass *class,const void *data) +{ +static ResettablePhases unused_parent_phases; +DeviceClass *dc =DEVICE_CLASS (class); +ResettableClass *rc =RESETTABLE_CLASS (class); + + dc->realize = bcm2838_genet_realize; +resettable_class_set_parent_phases (rc,NULL , bcm2838_genet_reset,NULL , + &unused_parent_phases); +} + +static const TypeInfo bcm2838_genet_info = { + .name =TYPE_BCM2838_GENET , + .parent =TYPE_SYS_BUS_DEVICE , + .instance_size =sizeof (BCM2838GenetState), + .class_init = bcm2838_genet_class_init, +}; + +static void bcm2838_genet_register (void ) +{ +type_register_static (&bcm2838_genet_info); +} + +type_init (bcm2838_genet_register)
hw/net/meson.build +2 -0
@@ -70,4 +70,6 @@ if have_hmp system_ss.add(files('rocker/rocker-hmp-cmds.c')) endif +system_ss.add(when: 'CONFIG_RASPI', if_true: files('bcm2838_genet.c')) + subdir('can')
hw/net/trace-events +16 -0
@@ -528,3 +528,19 @@ xen_netdev_rx(int dev, int idx, int status, int flags) "vif%u idx %d status %d f ethlite_pkt_lost(uint32_t rx_ctrl) "rx_ctrl:0x%" PRIx32 ethlite_pkt_size_too_big(uint64_t size) "size:0x%" PRIx64 ethlite_pkt_tx_size_too_big(uint64_t size) "size:0x%" PRIx64 +# bcm2838_genet.c +bcm2838_genet_read(unsigned int size, uint64_t offset, uint64_t value) "%u bytes @ 0x%04" PRIx64 ": 0x%016" PRIx64 +bcm2838_genet_write(unsigned int size, uint64_t offset, uint64_t value) "%u bytes @ 0x%04" PRIx64 ": 0x%016" PRIx64 +bcm2838_genet_can_receive(const char *state) "receive is %s" +bcm2838_genet_receive(ssize_t bytes_received) "%zd bytes received" +bcm2838_genet_phy_update_link(const char *link_state) "link is %s" +bcm2838_genet_phy_reset(const char *status) "PHY reset %s" +bcm2838_genet_reset(const char *status) "MAC reset %s" +bcm2838_genet_mac_address(const char *info) "%s" +bcm2838_genet_tx_dma(const char *dma_state) "TX DMA %s" +bcm2838_genet_tx_dma_ring(uint32_t ring_en) "TX DMA enabled rings: 0x%05x" +bcm2838_genet_tx_dma_ring_buf(uint32_t ring_buf_en) "TX DMA enabled ring buffers: 0x%05x" +bcm2838_genet_tx_dma_ring_active(unsigned int ring, const char *ring_state) "ring %u is %s" +bcm2838_genet_tx_request(unsigned int ring_idx, uint32_t prod_idx, uint32_t cons_idx) "ring %u, PROD_INDEX %u, CONS_INDEX %u" +bcm2838_genet_tx(unsigned int ring_idx, uint64_t desc_idx, uint32_t desc_status, uint64_t data_addr) "ring %u, descriptor %" PRIu64 ": 0x%08x, data @ 0x%08" PRIx64 +bcm2838_genet_rx_dma_ring_active(unsigned int ring, const char *ring_state) "ring %u is %s"
include/hw/arm/bcm2838_peripherals.h +2 -0
@@ -52,6 +52,8 @@#define BCM2838_MPHI_OFFSET 0xb200#define BCM2838_MPHI_SIZE 0x200 +#define GENET_OFFSET 0x1580000 +#define TYPE_BCM2838_PERIPHERALS "bcm2838-peripherals"OBJECT_DECLARE_TYPE (BCM2838PeripheralState, BCM2838PeripheralClass,BCM2838_PERIPHERALS )
include/hw/net/bcm2838_genet.h new file +40 -0
@@ -0,0 +1,40 @@ +/* +* BCM2838 Gigabit Ethernet emulation +* +* Copyright (C) 2022 Ovchinnikov Vitalii <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef BCM2838_GENET_H +#define BCM2838_GENET_H + +#include "net/net.h" +#include "hw/core/sysbus.h" + +#define TYPE_BCM2838_GENET "bcm2838-genet" +OBJECT_DECLARE_SIMPLE_TYPE(BCM2838GenetState, BCM2838_GENET) + +#define BCM2838_GENET_REV_MAJOR 6 +#define BCM2838_GENET_REV_MINOR 0 + +typedef struct { +uint8_t stub_area[0x10000 ];/* temporary stub */ +}BCM2838GenetRegs ; + +struct BCM2838GenetState { +/*< private >*/ +SysBusDevice parent_obj; + +/*< public >*/ + +MemoryRegion regs_mr; +AddressSpace dma_as; + +BCM2838GenetRegs regs; + +qemu_irq irq_default; +qemu_irq irq_prio; +}; + +#endif /* BCM2838_GENET_H */
Signature · 1 line
2.55.0
PATCH v6 6/17 hw/net/bcm2838_genet: add GENET register structs, part 2/4
In reply to Marcelo ManzoFrom: Sergey Kambalin <[email protected]>
Add the UMAC (CMD, MAC address, MDIO command) and DMA (ring config, control, producer/consumer index, status, RX descriptor length/status) register layouts and their field accessors.
Part of Sergey Kambalin's original Raspberry Pi 4B PCIe/GENET networking series (patchwork series 829638, posted to qemu-devel in 2024, never merged). Carried forward and completed by Marcelo Manzo.
--- hw/net/bcm2838_genet.c | 91 ++++++++++++++++++++++++++++++++++ include/hw/net/bcm2838_genet.h | 89 +++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+)
hw/net/bcm2838_genet.c +91 -0
@@ -20,6 +20,7 @@#include "hw/net/bcm2838_genet.h" #include "trace.h" +/* GENET layouts */ REG32(GENET_SYS_REV_CTRL, 0) FIELD(GENET_SYS_REV_CTRL, GPHY_REV, 0, 16)FIELD (GENET_SYS_REV_CTRL ,MINOR_REV ,16 ,4 )
@@ -56,6 +57,96 @@ REG32(GENET_INTRL_1, 0) FIELD(GENET_INTRL_1, TX_INTRS, 0, 16) FIELD(GENET_INTRL_1, RX_INTRS, 16, 16) +REG32(GENET_UMAC_CMD, 0) +FIELD(GENET_UMAC_CMD, TX_EN, 0, 1) +FIELD(GENET_UMAC_CMD, RX_EN, 1, 1) +FIELD(GENET_UMAC_CMD, SPEED, 2, 2) +FIELD(GENET_UMAC_CMD, PROMISC, 4, 1) +FIELD(GENET_UMAC_CMD, PAD_EN, 5, 1) +FIELD(GENET_UMAC_CMD, CRC_FWD, 6, 1) +FIELD(GENET_UMAC_CMD, PAUSE_FWD, 7, 1) +FIELD(GENET_UMAC_CMD, RX_PAUSE_IGNORE, 8, 1) +FIELD(GENET_UMAC_CMD, TX_ADDR_INS, 9, 1) +FIELD(GENET_UMAC_CMD, HD_EN, 10, 1) +FIELD(GENET_UMAC_CMD, SW_RESET_OLD, 11, 1) +FIELD(GENET_UMAC_CMD, RSVD_12, 12, 1) +FIELD(GENET_UMAC_CMD, SW_RESET, 13, 1) +FIELD(GENET_UMAC_CMD, RSVD_14, 14, 1) +FIELD(GENET_UMAC_CMD, LCL_LOOP_EN, 15, 1) +FIELD(GENET_UMAC_CMD, RSVD_16_21, 16, 6) +FIELD(GENET_UMAC_CMD, AUTO_CONFIG, 22, 1) +FIELD(GENET_UMAC_CMD, CNTL_FRM_EN, 23, 1) +FIELD(GENET_UMAC_CMD, NO_LEN_CHK, 24, 1) +FIELD(GENET_UMAC_CMD, RMT_LOOP_EN, 25, 1) +FIELD(GENET_UMAC_CMD, RX_ERR_DISC, 26, 1) +FIELD(GENET_UMAC_CMD, PRBL_EN, 27, 1) +FIELD(GENET_UMAC_CMD, TX_PAUSE_IGNORE, 28, 1) +FIELD(GENET_UMAC_CMD, TX_RX_EN, 29, 1) +FIELD(GENET_UMAC_CMD, RUNT_FILTER_DIS, 30, 1) +FIELD(GENET_UMAC_CMD, RSVD_31, 31, 1) + +REG32(GENET_UMAC_MAC_0, 0) +FIELD(GENET_UMAC_MAC_0, ADDR_3, 0, 8) +FIELD(GENET_UMAC_MAC_0, ADDR_2, 8, 8) +FIELD(GENET_UMAC_MAC_0, ADDR_1, 16, 8) +FIELD(GENET_UMAC_MAC_0, ADDR_0, 24, 8) + +REG32(GENET_UMAC_MAC_1, 0) +FIELD(GENET_UMAC_MAC_1, ADDR_5, 0, 8) +FIELD(GENET_UMAC_MAC_1, ADDR_4, 8, 8) +FIELD(GENET_UMAC_MAC_1, RSVD_16_31, 16, 8) + +REG32(GENET_UMAC_MDIO_CMD, 0) +FIELD(GENET_UMAC_MDIO_CMD, REG_DATA, 0, 16) +FIELD(GENET_UMAC_MDIO_CMD, REG_ID, 16, 5) +FIELD(GENET_UMAC_MDIO_CMD, PHY_ID, 21, 5) +FIELD(GENET_UMAC_MDIO_CMD, WR, 26, 1) +FIELD(GENET_UMAC_MDIO_CMD, RD, 27, 1) +FIELD(GENET_UMAC_MDIO_CMD, RD_FAIL, 28, 1) +FIELD(GENET_UMAC_MDIO_CMD, START_BUSY, 29, 1) +FIELD(GENET_UMAC_MDIO_CMD, RSVD_30_31, 30, 2) + +REG32(GENET_DMA_RING_CFG, 0) +FIELD(GENET_DMA_RING_CFG, EN, 0, 17) +FIELD(GENET_DMA_RING_CFG, RSVD_17_31, 17, 14) + +REG32(GENET_DMA_CTRL, 0) +FIELD(GENET_DMA_CTRL, EN, 0, 1) +FIELD(GENET_DMA_CTRL, RING_BUF_EN, 1, 17) +FIELD(GENET_DMA_CTRL, RSVD_18_19, 18, 2) +FIELD(GENET_DMA_CTRL, TSB_SWAP_EN, 20, 1) +FIELD(GENET_DMA_CTRL, RSVD_21_31, 21, 11) + +REG32(GENET_DMA_PROD_INDEX, 0) +FIELD(GENET_DMA_PROD_INDEX, INDEX, 0, 16) +FIELD(GENET_DMA_PROD_INDEX, DISCARD_CNT, 16, 16) + +REG32(GENET_DMA_CONS_INDEX, 0) +FIELD(GENET_DMA_CONS_INDEX, INDEX, 0, 16) +FIELD(GENET_DMA_CONS_INDEX, RSVD_16_31, 16, 16) + +REG32(GENET_DMA_STATUS, 0) +FIELD(GENET_DMA_STATUS, DISABLED, 0, 1) +FIELD(GENET_DMA_STATUS, DESC_RAM_INIT_BUSY, 1, 1) +FIELD(GENET_DMA_STATUS, RSVD_2_31, 2, 30) + +#define GENET_DMA_ENABLE_MASK ((1U << 18) - 1) + +REG32(GENET_RDMA_LENGTH_STATUS, 0) +FIELD(GENET_RDMA_LENGTH_STATUS, OVERRUN, 0, 1) +FIELD(GENET_RDMA_LENGTH_STATUS, CRC_ERROR, 1, 1) +FIELD(GENET_RDMA_LENGTH_STATUS, RXERR, 2, 1) +FIELD(GENET_RDMA_LENGTH_STATUS, NO, 3, 1) +FIELD(GENET_RDMA_LENGTH_STATUS, LG, 4, 1) +FIELD(GENET_RDMA_LENGTH_STATUS, MULTICAST, 5, 1) +FIELD(GENET_RDMA_LENGTH_STATUS, BROADCAST, 6, 1) +FIELD(GENET_RDMA_LENGTH_STATUS, RSVD_7_11, 7, 5) +FIELD(GENET_RDMA_LENGTH_STATUS, WRAP, 12, 1) +FIELD(GENET_RDMA_LENGTH_STATUS, SOP, 13, 1) +FIELD(GENET_RDMA_LENGTH_STATUS, EOP, 14, 1) +FIELD(GENET_RDMA_LENGTH_STATUS, OWN, 15, 1) +FIELD(GENET_RDMA_LENGTH_STATUS, BUFLENGTH, 16, 12) +FIELD(GENET_RDMA_LENGTH_STATUS, RSVD_28_31, 29, 4)static uint64_t bcm2838_genet_read (void *opaque,hwaddr offset,unsigned size) {
include/hw/net/bcm2838_genet.h +89 -0
107 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v6 16/17 docs/system/arm/raspi: move PCIe/GENET from missing to implemented
In reply to Marcelo Manzoraspi4b now has a working PCIe Root Complex and GENET Ethernet controller (added in the preceding patches); move them out of the "Missing devices" list and into "Implemented devices".
--- docs/system/arm/raspi.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
docs/system/arm/raspi.rst +4 -2
@@ -36,10 +36,12 @@ Implemented devices * VideoCore firmware (property) * Peripheral SPI controller (SPI) * Broadcom Serial Controller (I2C) + * PCIe Root Complex (raspi4b) + * GENET Ethernet Controller (raspi4b) Missing devices --------------- * Pulse Width Modulation (PWM) - * PCIE Root Port (raspi4b) - * GENET Ethernet Controller (raspi4b) + * PCIe MSI/MSI-X interrupt delivery (raspi4b) -- INTx works; a PCIe + device that only signals via MSI/MSI-X needs ``msi=off,msix=off``
Signature · 1 line
2.55.0
PATCH v6 13/17 hw/net/bcm2838_genet: implement GENET RX path
In reply to Marcelo ManzoFrom: Sergey Kambalin <[email protected]>
Implement the receive DMA ring handling: qemu_can_receive()/ receive() callbacks for the NIC's netdev peer, copying incoming frame data into descriptor buffers on the active RX ring, classifying broadcast/multicast, and raising the RXDMA_MBDONE interrupt as descriptors complete.
Part of Sergey Kambalin's original Raspberry Pi 4B PCIe/GENET networking series (patchwork series 829638, posted to qemu-devel in 2024, never merged). Carried forward and completed by Marcelo Manzo.
While carrying this forward, make these changes to the original code:
- Report no hardware-computed RX checksum (rx_csum = 0). The original stored the IPv4 header checksum in host byte order, but the guest's CHECKSUM_COMPLETE path expects a running checksum over the L4 payload, which made the guest reject otherwise-valid inbound packets ("hw csum failure" in bcmgenet_rx_poll()) and broke DHCP/connectivity intermittently. With 0 the guest verifies checksums in software instead.
- Fill in the per-packet status block with stl_le_p() rather than by casting the packet buffer to a C struct, which is undefined behaviour for a misaligned buffer and wrong on big-endian hosts.
- Refresh the link state once a BMCR autonegotiation restart has been handled by the MDIO code (moved here from the MDIO patch, where the function it calls does not exist yet).
- Validate the guest-programmed descriptor indexes against the size of the descriptor array, and limit the DMA write to the size of our packet buffer, so that a guest-controlled register can't make us access memory outside them.
--- hw/net/bcm2838_genet.c | 289 +++++++++++++++++++++++++++++++++ include/hw/net/bcm2838_genet.h | 1 + 2 files changed, 290 insertions(+)
hw/net/bcm2838_genet.c +289 -0
@@ -20,6 +20,8 @@#include "hw/net/bcm2838_genet.h" #include "trace.h" +static void bcm2838_genet_phy_update_link (BCM2838GenetState *s); +/* GENET layouts */ REG32(GENET_SYS_REV_CTRL, 0)FIELD (GENET_SYS_REV_CTRL ,GPHY_REV ,0 ,16 )
@@ -402,6 +404,9 @@ static uint64_t bcm2838_genet_mdio_cmd(BCM2838GenetState *s, uint64_t cmd) } } *phy_reg = phy_reg_data; +if (phy_reg_id ==BCM2838_GENET_PHY_BMCR && anrestart ==1 ) { +bcm2838_genet_phy_update_link (s); + } } } }
@@ -585,6 +590,24 @@ static void bcm2838_genet_tdma_prod_index_write(BCM2838GenetState *s, } } +static bool bcm2838_genet_rdma_ring_active (BCM2838GenetState *s, +unsigned int ring_index) +{ +uint32_t ring_mask =1 << ring_index; + +bool dma_en =FIELD_EX32 (s->regs.rdma.ctrl,GENET_DMA_CTRL ,EN ); +bool ring_en =FIELD_EX32 (s->regs.rdma.ring_cfg,GENET_DMA_RING_CFG ,EN ) + & ring_mask; +bool ring_buf_en =FIELD_EX32 (s->regs.rdma.ctrl, +GENET_DMA_CTRL ,RING_BUF_EN ) & ring_mask; +bool active = dma_en && ring_en && ring_buf_en; + +trace_bcm2838_genet_rx_dma_ring_active (ring_index, + active ?"active" :"halted" ); + +return active; +} +static void bcm2838_genet_tdma (BCM2838GenetState *s,hwaddr offset,uint64_t value) {
@@ -765,9 +788,273 @@ static const MemoryRegionOps bcm2838_genet_ops = { .valid = {.min_access_size = 4, .max_access_size = 4}, }; +static int32_t bcm2838_genet_filter (BCM2838GenetState *s,const void *buf, +size_t size) +{ +qemu_log_mask (LOG_UNIMP , +"Packet filtration with HFB isn't implemented yet" ); +return -1 ; +} + +static int32_t bcm2838_genet_filter2ring (BCM2838GenetState *s, +uint32_t filter_idx) +{ +qemu_log_mask (LOG_UNIMP , +"Packet filtration with HFB isn't implemented yet" ); +return -1 ; +} + +static bool is_packet_broadcast (const uint8_t *buf,size_t size) +{ +static const uint8_t bcst_addr[] = {0xff ,0xff ,0xff ,0xff ,0xff ,0xff }; + +if (size <sizeof (bcst_addr)) { +return false; + } + +return !memcmp (buf, bcst_addr,sizeof (bcst_addr)); +} + +static bool is_packet_multicast (const uint8_t *buf,size_t size) +{ +return !!(buf[0 ] &0x01 ); +} + +static ssize_t bcm2838_genet_rdma (BCM2838GenetState *s,uint32_t ring_idx, +const void *buf,size_t size) +{ +const size_t desc_word_size = +sizeof (BCM2838GenetRdmaDesc) /sizeof (uint32_t ); + +ssize_t len =0 ; +BCM2838GenetRegsRdma *rdma = &s->regs.rdma; +BCM2838GenetRdmaRing *ring = &rdma->rings[ring_idx]; +hwaddr write_index = +bcm2838_genet_make_u64 (ring->write_ptr_hi, ring->write_ptr) + / desc_word_size; +BCM2838GenetRdmaDesc *desc; + +const hwaddr start_index = +bcm2838_genet_make_u64 (ring->start_addr_hi, ring->start_addr) + / desc_word_size; +const hwaddr end_index = +bcm2838_genet_make_u64 (ring->end_addr_hi, ring->end_addr) + / desc_word_size; + +if (!bcm2838_genet_rdma_ring_active (s, ring_idx)) { +return -1 ; + } + +if (write_index >=BCM2838_GENET_DMA_DESC_CNT ) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: invalid RX descriptor index %" PRIu64 +" (exceeds %u)\n" , __func__, (uint64_t )write_index, +BCM2838_GENET_DMA_DESC_CNT -1 ); +return -1 ; + } + desc = &rdma->descs[write_index]; + + desc->length_status =FIELD_DP32 (desc->length_status, +GENET_RDMA_LENGTH_STATUS ,SOP ,1 ); + +while (len < size) { +size_t l = size - len; +size_t buf_size =MIN (ring->ring_buf_size &0xffff , +sizeof (s->rx_packet)); +uint8_t *dma_buf = s->rx_packet; +hwaddr dma_buf_addr = +bcm2838_genet_make_u64 (desc->address_hi, desc->address_lo); +MemTxResult mem_tx_result =MEMTX_OK ; +const size_t frame_off =sizeof (BCM2838GenetXmitStatus) +2 ; +uint8_t *frame_buf = dma_buf + frame_off; + +bool crc_fwd =FIELD_EX32 (s->regs.umac.cmd,GENET_UMAC_CMD ,CRC_FWD ); +size_t buflength; +uint32_t prod_index; + +if (l > ring->ring_buf_size) { + l = ring->ring_buf_size; + } +/* Never copy more than fits in rx_packet after the status block */ +if (l >sizeof (s->rx_packet) - frame_off) { + l =sizeof (s->rx_packet) - frame_off; + } + +memcpy (frame_buf, buf + len, l); + + len += l; + + desc->length_status =FIELD_DP32 (desc->length_status, +GENET_RDMA_LENGTH_STATUS , +EOP , !!(len >= size)); + + buflength = l +sizeof (BCM2838GenetXmitStatus) +2 ; +if (crc_fwd) { + buflength +=4 ; + } + + desc->length_status =FIELD_DP32 (desc->length_status, +GENET_RDMA_LENGTH_STATUS , +BUFLENGTH , buflength); + + desc->length_status =FIELD_DP32 (desc->length_status, +GENET_RDMA_LENGTH_STATUS , +BROADCAST , + !!is_packet_broadcast (frame_buf, l)); + desc->length_status =FIELD_DP32 (desc->length_status, +GENET_RDMA_LENGTH_STATUS , +MULTICAST , + !!is_packet_multicast (frame_buf, l)); + +/* +* We don't implement the hardware's partial RX checksum: report 0 and +* rely on the guest verifying checksums in software. +*/ +stl_le_p (dma_buf + offsetof(BCM2838GenetXmitStatus , rx_csum),0 ); +stl_le_p (dma_buf + offsetof(BCM2838GenetXmitStatus , length_status), + desc->length_status); + + mem_tx_result =address_space_write (&s->dma_as, dma_buf_addr, +MEMTXATTRS_UNSPECIFIED , + dma_buf, buf_size); +if (mem_tx_result !=MEMTX_OK ) { + desc->length_status =FIELD_DP32 (desc->length_status, +GENET_RDMA_LENGTH_STATUS , +RXERR ,1 ); + } + +if (FIELD_EX32 (desc->length_status, +GENET_RDMA_LENGTH_STATUS ,RXERR ) !=0 ) { +break ; + } + + prod_index =FIELD_EX32 (ring->prod_index,GENET_DMA_PROD_INDEX ,INDEX ); + ring->prod_index =FIELD_DP32 (ring->prod_index, +GENET_DMA_PROD_INDEX , +INDEX , ++prod_index); +if (++write_index > end_index) { + write_index = start_index; + } +if (write_index >=BCM2838_GENET_DMA_DESC_CNT ) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: invalid RX descriptor index %" PRIu64 +" (exceeds %u)\n" , __func__, (uint64_t )write_index, +BCM2838_GENET_DMA_DESC_CNT -1 ); +break ; + } + desc = &rdma->descs[write_index]; + ring->write_ptr = write_index * desc_word_size; + ring->write_ptr_hi = ((hwaddr )write_index * desc_word_size) >>32 ; + } + +if (ring_idx ==BCM2838_GENET_DMA_RING_DEFAULT ) { + s->regs.intrl0.stat =FIELD_DP32 (s->regs.intrl0.stat, +GENET_INTRL_0 ,RXDMA_MBDONE ,1 ); + }else { +uint32_t rx_intrs = +FIELD_EX32 (s->regs.intrl1.stat,GENET_INTRL_1 ,RX_INTRS ); + rx_intrs |=1 << ring_idx; + + s->regs.intrl1.stat =FIELD_DP32 (s->regs.intrl1.stat, +GENET_INTRL_1 ,RX_INTRS , rx_intrs); + } + +return len; +} + +static ssize_t bcm2838_genet_receive (NetClientState *nc,const uint8_t *buf, +size_t size) +{ +BCM2838GenetState *s = (BCM2838GenetState *)qemu_get_nic_opaque (nc); +ssize_t bytes_received =-1 ; +int32_t filter_index =-1 ; +int32_t ring_index =-1 ; + +if (FIELD_EX32 (s->regs.rdma.ctrl,GENET_DMA_CTRL ,EN ) !=0 ) { + filter_index =bcm2838_genet_filter (s, buf, size); + +if (filter_index >=0 ) { + ring_index =bcm2838_genet_filter2ring (s, filter_index); + }else { + ring_index =BCM2838_GENET_DMA_RING_CNT -1 ; +if (!bcm2838_genet_rdma_ring_active (s, ring_index)) { +for (ring_index =0 ; + ring_index <BCM2838_GENET_DMA_RING_CNT -1 ; + ring_index++) { +if (bcm2838_genet_rdma_ring_active (s, ring_index)) { +break ; + } + } + } + } + +if (size <=MAX_PACKET_SIZE ) { + bytes_received =bcm2838_genet_rdma (s, ring_index, buf, size); + } + } + +bcm2838_genet_set_irq_default (s); +bcm2838_genet_set_irq_prio (s); + +return bytes_received; +} + +static void bcm2838_genet_phy_update_link (BCM2838GenetState *s) +{ +bool qemu_link_down =qemu_get_queue (s->nic)->link_down; + +bool lstatus =FIELD_EX32 (s->phy_regs.bmsr,GENET_PHY_BMSR ,LSTATUS ); + +if (qemu_link_down && lstatus) { +trace_bcm2838_genet_phy_update_link ("down" ); + + s->phy_regs.bmsr =FIELD_DP32 (s->phy_regs.bmsr, +GENET_PHY_BMSR ,ANEGCOMPLETE ,0 ); + s->phy_regs.bmsr =FIELD_DP32 (s->phy_regs.bmsr, +GENET_PHY_BMSR ,LSTATUS ,0 ); + s->regs.intrl0.stat =FIELD_DP32 (s->regs.intrl0.stat, +GENET_INTRL_0 ,LINK_DOWN ,1 ); + }else if (!qemu_link_down && !lstatus) { +trace_bcm2838_genet_phy_update_link ("up" ); + +/* +* Complete auto-negotiation (fixed link partner's abilities for now: +* 1Gbps with flow control) +*/ + s->phy_regs.stat1000 =FIELD_DP32 (s->phy_regs.stat1000, +GENET_PHY_STAT_1000 ,HALF ,1 ); + s->phy_regs.stat1000 =FIELD_DP32 (s->phy_regs.stat1000, +GENET_PHY_STAT_1000 ,FULL ,1 ); + + s->phy_regs.lpa =FIELD_DP32 (s->phy_regs.lpa, +GENET_PHY_LPA ,PAUSE_CAP ,1 ); + s->phy_regs.lpa =FIELD_DP32 (s->phy_regs.lpa, +GENET_PHY_LPA ,PAUSE_ASYM ,1 ); + s->phy_regs.lpa =FIELD_DP32 (s->phy_regs.lpa,GENET_PHY_LPA ,LPACK ,1 ); + + s->phy_regs.bmsr =FIELD_DP32 (s->phy_regs.bmsr, +GENET_PHY_BMSR ,ANEGCOMPLETE ,1 ); + s->phy_regs.bmsr =FIELD_DP32 (s->phy_regs.bmsr, +GENET_PHY_BMSR ,LSTATUS ,1 ); + + s->regs.intrl0.stat =FIELD_DP32 (s->regs.intrl0.stat, +GENET_INTRL_0 ,LINK_UP ,1 ); + } + +bcm2838_genet_set_irq_default (s); +} +static void bcm2838_genet_set_link (NetClientState *nc) +{ +BCM2838GenetState *s =qemu_get_nic_opaque (nc); + +bcm2838_genet_phy_update_link (s); +} +static NetClientInfo bcm2838_genet_client_info = { .type =NET_CLIENT_DRIVER_NIC , .size =sizeof (NICState), + .receive = bcm2838_genet_receive, + .link_status_changed = bcm2838_genet_set_link, };static void bcm2838_genet_realize (DeviceState *dev,Error **errp)
@@ -821,6 +1108,8 @@ static void bcm2838_genet_phy_reset(BCM2838GenetState *s) s->phy_aux_ctl_shd_regs.regs[BCM2838_GENET_PHY_AUX_CTL_MISC ] =0x1E ;trace_bcm2838_genet_phy_reset ("done" ); + +bcm2838_genet_phy_update_link (s); }static void bcm2838_genet_reset (Object *obj,ResetType type)
include/hw/net/bcm2838_genet.h +1 -0
@@ -419,6 +419,7 @@ struct BCM2838GenetState {qemu_irq irq_prio;uint8_t tx_packet[MAX_FRAME_SIZE ]; +uint8_t rx_packet[MAX_FRAME_SIZE ]; };/*
Signature · 1 line
2.55.0
PATCH v6 9/17 hw/net/bcm2838_genet: add GENET register access macros
In reply to Marcelo ManzoFrom: Sergey Kambalin <[email protected]>
Add offsetof()-based byte-offset macros for the register block layouts added in the previous patches (INTRL0/1, UMAC, TDMA/RDMA and their per-ring registers, HFB filters), used by the MMIO read/write dispatch implemented in the following patches.
Part of Sergey Kambalin's original Raspberry Pi 4B PCIe/GENET networking series (patchwork series 829638, posted to qemu-devel in 2024, never merged). Carried forward and completed by Marcelo Manzo.
--- include/hw/net/bcm2838_genet.h | 78 ++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+)
include/hw/net/bcm2838_genet.h +78 -0
@@ -22,9 +22,87 @@ OBJECT_DECLARE_SIMPLE_TYPE(BCM2838GenetState, BCM2838_GENET)#define BCM2838_GENET_DMA_RING_CNT 17#define BCM2838_GENET_DMA_RING_DEFAULT (BCM2838_GENET_DMA_RING_CNT - 1) +#define BCM2838_GENET_HFB_FILTER_REGS offsetof(BCM2838GenetRegs, hfb) +#define BCM2838_GENET_HFB_FILTER_REG (reg) (BCM2838_GENET_HFB_FILTER_REGS \ + + offsetof(BCM2838GenetRegsHfb, reg))#define BCM2838_GENET_HFB_FILTER_CNT 48#define BCM2838_GENET_HFB_FILTER_SIZE 128 +#define BCM2838_GENET_INTRL0_REG (reg) (offsetof(BCM2838GenetRegs, intrl0) \ + + offsetof(BCM2838GenetRegsIntrl0, reg)) +#define BCM2838_GENET_INTRL0_SET BCM2838_GENET_INTRL0_REG(set) +#define BCM2838_GENET_INTRL0_CLEAR BCM2838_GENET_INTRL0_REG(clear) +#define BCM2838_GENET_INTRL0_MASK_SET BCM2838_GENET_INTRL0_REG(mask_set) +#define BCM2838_GENET_INTRL0_MASK_CLEAR BCM2838_GENET_INTRL0_REG(mask_clear) + +#define BCM2838_GENET_INTRL1_REG (reg) (offsetof(BCM2838GenetRegs, intrl1) \ + + offsetof(BCM2838GenetRegsIntrl1, reg)) +#define BCM2838_GENET_INTRL1_SET BCM2838_GENET_INTRL1_REG(set) +#define BCM2838_GENET_INTRL1_CLEAR BCM2838_GENET_INTRL1_REG(clear) +#define BCM2838_GENET_INTRL1_MASK_SET BCM2838_GENET_INTRL1_REG(mask_set) +#define BCM2838_GENET_INTRL1_MASK_CLEAR BCM2838_GENET_INTRL1_REG(mask_clear) + +#define BCM2838_GENET_UMAC_REG (reg) (offsetof(BCM2838GenetRegs, umac) \ + + offsetof(BCM2838GenetRegsUmac, reg)) +#define BCM2838_GENET_UMAC_CMD BCM2838_GENET_UMAC_REG(cmd) +#define BCM2838_GENET_UMAC_MAC0 BCM2838_GENET_UMAC_REG(mac0) +#define BCM2838_GENET_UMAC_MAC1 BCM2838_GENET_UMAC_REG(mac1) +#define BCM2838_GENET_UMAC_MDIO_CMD BCM2838_GENET_UMAC_REG(mdio_cmd) + +#define BCM2838_GENET_TDMA_REGS offsetof(BCM2838GenetRegs, tdma) +#define BCM2838_GENET_TDMA_REG (reg) (BCM2838_GENET_TDMA_REGS \ + + offsetof(BCM2838GenetRegsTdma, reg)) +#define BCM2838_GENET_TDMA_RINGS BCM2838_GENET_TDMA_REG(rings) +#define BCM2838_GENET_TDMA_RING_CFG BCM2838_GENET_TDMA_REG(ring_cfg) +#define BCM2838_GENET_TDMA_CTRL BCM2838_GENET_TDMA_REG(ctrl) +#define BCM2838_GENET_TDMA_STATUS BCM2838_GENET_TDMA_REG(status) + +#define BCM2838_GENET_RDMA_REGS offsetof(BCM2838GenetRegs, rdma) +#define BCM2838_GENET_RDMA_REG (reg) (BCM2838_GENET_RDMA_REGS \ + + offsetof(BCM2838GenetRegsRdma, reg)) +#define BCM2838_GENET_RDMA_RINGS BCM2838_GENET_RDMA_REG(rings) +#define BCM2838_GENET_RDMA_RING_CFG BCM2838_GENET_RDMA_REG(ring_cfg) +#define BCM2838_GENET_RDMA_CTRL BCM2838_GENET_RDMA_REG(ctrl) +#define BCM2838_GENET_RDMA_STATUS BCM2838_GENET_RDMA_REG(status) + +#define BCM2838_GENET_TRING_REG (reg) offsetof(BCM2838GenetTdmaRing, reg) +#define BCM2838_GENET_TRING_WRITE_PTR BCM2838_GENET_TRING_REG(write_ptr) +#define BCM2838_GENET_TRING_WRITE_PTR_HI BCM2838_GENET_TRING_REG(write_ptr_hi) +#define BCM2838_GENET_TRING_PROD_INDEX BCM2838_GENET_TRING_REG(prod_index) +#define BCM2838_GENET_TRING_CONS_INDEX BCM2838_GENET_TRING_REG(cons_index) +#define BCM2838_GENET_TRING_RING_BUF_SIZE BCM2838_GENET_TRING_REG(ring_buf_size) +#define BCM2838_GENET_TRING_RING_START_ADDR BCM2838_GENET_TRING_REG(start_addr) +#define BCM2838_GENET_TRING_RING_START_ADDR_HI BCM2838_GENET_TRING_REG(start_addr_hi) +#define BCM2838_GENET_TRING_RING_END_ADDR BCM2838_GENET_TRING_REG(end_addr) +#define BCM2838_GENET_TRING_RING_END_ADDR_HI BCM2838_GENET_TRING_REG(end_addr_hi) +#define BCM2838_GENET_TRING_RING_MBUF_DONE_TRESH BCM2838_GENET_TRING_REG(mbuf_done_tresh) +#define BCM2838_GENET_TRING_RING_FLOW_PERIOD BCM2838_GENET_TRING_REG(flow_period) +#define BCM2838_GENET_TRING_RING_READ_PTR BCM2838_GENET_TRING_REG(read_ptr) +#define BCM2838_GENET_TRING_RING_READ_PTR_HI BCM2838_GENET_TRING_REG(read_ptr_hi) + +#define BCM2838_GENET_RRING_REG (reg) offsetof(BCM2838GenetRdmaRing, reg) +#define BCM2838_GENET_RRING_WRITE_PTR BCM2838_GENET_RRING_REG(write_ptr) +#define BCM2838_GENET_RRING_WRITE_PTR_HI BCM2838_GENET_RRING_REG(write_ptr_hi) +#define BCM2838_GENET_RRING_PROD_INDEX BCM2838_GENET_RRING_REG(prod_index) +#define BCM2838_GENET_RRING_CONS_INDEX BCM2838_GENET_RRING_REG(cons_index) +#define BCM2838_GENET_RRING_RING_BUF_SIZE BCM2838_GENET_RRING_REG(ring_buf_size) +#define BCM2838_GENET_RRING_RING_START_ADDR BCM2838_GENET_RRING_REG(start_addr) +#define BCM2838_GENET_RRING_RING_START_ADDR_HI BCM2838_GENET_RRING_REG(start_addr_hi) +#define BCM2838_GENET_RRING_RING_END_ADDR BCM2838_GENET_RRING_REG(end_addr) +#define BCM2838_GENET_RRING_RING_END_ADDR_HI BCM2838_GENET_RRING_REG(end_addr_hi) +#define BCM2838_GENET_RRING_RING_MBUF_DONE_TRESH BCM2838_GENET_RRING_REG(mbuf_done_tresh) +#define BCM2838_GENET_RRING_RING_XON_XOFF_TRESH BCM2838_GENET_RRING_REG(xon_xoff_tresh) +#define BCM2838_GENET_RRING_RING_READ_PTR BCM2838_GENET_RRING_REG(read_ptr) +#define BCM2838_GENET_RRING_RING_READ_PTR_HI BCM2838_GENET_RRING_REG(read_ptr_hi) + + +#define BCM2838_GENET_PHY_REG (reg) (offsetof(BCM2838GenetPhyRegs, reg) / 2) +#define BCM2838_GENET_PHY_BMCR BCM2838_GENET_PHY_REG(bmcr) +#define BCM2838_GENET_PHY_AUX_CTL BCM2838_GENET_PHY_REG(aux_ctl) +#define BCM2838_GENET_PHY_SHD BCM2838_GENET_PHY_REG(shd) +#define BCM2838_GENET_EXP_DATA BCM2838_GENET_PHY_REG(exp_data) +#define BCM2838_GENET_EXP_SEL BCM2838_GENET_PHY_REG(exp_ctrl) +#define BCM2838_GENET_PHY_AUX_CTL_MISC 0x7#define BCM2838_GENET_PHY_AUX_CTL_REGS_SIZE 8
Signature · 1 line
2.55.0
PATCH v6 7/17 hw/net/bcm2838_genet: add GENET register structs, part 3/4
In reply to Marcelo ManzoFrom: Sergey Kambalin <[email protected]>
Add the TDMA (transmit DMA ring/length-status) register layout and field accessors, and the PHY BMCR/BMSR MII register field definitions.
Part of Sergey Kambalin's original Raspberry Pi 4B PCIe/GENET networking series (patchwork series 829638, posted to qemu-devel in 2024, never merged). Carried forward and completed by Marcelo Manzo.
--- hw/net/bcm2838_genet.c | 88 ++++++++++++++++++++++++++++++++++ include/hw/net/bcm2838_genet.h | 88 ++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+)
hw/net/bcm2838_genet.c +88 -0
@@ -148,6 +148,94 @@ FIELD(GENET_RDMA_LENGTH_STATUS, OWN, 15, 1) FIELD(GENET_RDMA_LENGTH_STATUS, BUFLENGTH, 16, 12) FIELD(GENET_RDMA_LENGTH_STATUS, RSVD_28_31, 29, 4) +REG32(GENET_TDMA_LENGTH_STATUS, 0) +FIELD(GENET_TDMA_LENGTH_STATUS, RSVD_0_3, 0, 4) +FIELD(GENET_TDMA_LENGTH_STATUS, DO_CSUM, 4, 1) +FIELD(GENET_TDMA_LENGTH_STATUS, OW_CRC, 5, 1) +FIELD(GENET_TDMA_LENGTH_STATUS, APPEND_CRC, 6, 1) +FIELD(GENET_TDMA_LENGTH_STATUS, RSVD_7_8, 7, 2) +FIELD(GENET_TDMA_LENGTH_STATUS, UNDERRUN, 9, 1) +FIELD(GENET_TDMA_LENGTH_STATUS, RSVD_10_11, 10, 2) +FIELD(GENET_TDMA_LENGTH_STATUS, WRAP, 12, 1) +FIELD(GENET_TDMA_LENGTH_STATUS, SOP, 13, 1) +FIELD(GENET_TDMA_LENGTH_STATUS, EOP, 14, 1) +FIELD(GENET_TDMA_LENGTH_STATUS, OWN, 15, 1) +FIELD(GENET_TDMA_LENGTH_STATUS, BUFLENGTH, 16, 12) +FIELD(GENET_TDMA_LENGTH_STATUS, RSVD_28_31, 29, 4) + +REG16(GENET_PHY_BMCR, 0) +FIELD(GENET_PHY_BMCR, RSVD_0_5, 0, 6) +FIELD(GENET_PHY_BMCR, SPEED1000, 6, 1) +FIELD(GENET_PHY_BMCR, CTST, 7, 1) +FIELD(GENET_PHY_BMCR, FULLDPLX, 8, 1) +FIELD(GENET_PHY_BMCR, ANRESTART, 9, 1) +FIELD(GENET_PHY_BMCR, ISOLATE, 10, 1) +FIELD(GENET_PHY_BMCR, PDOWN, 11, 1) +FIELD(GENET_PHY_BMCR, AENABLE, 12, 1) +FIELD(GENET_PHY_BMCR, SPEED100, 13, 1) +FIELD(GENET_PHY_BMCR, LOOPBACK, 14, 1) +FIELD(GENET_PHY_BMCR, RESET, 15, 1) + +REG16(GENET_PHY_BMSR, 0) +FIELD(GENET_PHY_BMSR, ERCAP, 0, 1) +FIELD(GENET_PHY_BMSR, JCD, 1, 1) +FIELD(GENET_PHY_BMSR, LSTATUS, 2, 1) +FIELD(GENET_PHY_BMSR, ANEGCAPABLE, 3, 1) +FIELD(GENET_PHY_BMSR, RFAULT, 4, 1) +FIELD(GENET_PHY_BMSR, ANEGCOMPLETE, 5, 1) +FIELD(GENET_PHY_BMSR, RSVD_6_7, 6, 2) +FIELD(GENET_PHY_BMSR, ESTATEN, 8, 1) +FIELD(GENET_PHY_BMSR, _100HALF2, 9, 1) +FIELD(GENET_PHY_BMSR, _100FULL2, 10, 1) +FIELD(GENET_PHY_BMSR, _10HALF, 11, 1) +FIELD(GENET_PHY_BMSR, _10FULL, 12, 1) +FIELD(GENET_PHY_BMSR, _100HALF, 13, 1) +FIELD(GENET_PHY_BMSR, _100FULL, 14, 1) +FIELD(GENET_PHY_BMSR, _10BASE4, 15, 1) + +REG16(GENET_PHY_LPA, 0) +FIELD(GENET_PHY_LPA, SLCT, 0, 5) +FIELD(GENET_PHY_LPA, _10HALF_1000XFULL, 5, 1) +FIELD(GENET_PHY_LPA, _10FULL_1000XHALF, 6, 1) +FIELD(GENET_PHY_LPA, _100HALF_1000XPAUSE, 7, 1) +FIELD(GENET_PHY_LPA, _100FULL_1000XPAUSE_ASYM, 8, 1) +FIELD(GENET_PHY_LPA, _100BASE4, 9, 1) +FIELD(GENET_PHY_LPA, PAUSE_CAP, 10, 1) +FIELD(GENET_PHY_LPA, PAUSE_ASYM, 11, 1) +FIELD(GENET_PHY_LPA, RSVD_12, 12, 1) +FIELD(GENET_PHY_LPA, RFAULT, 13, 1) +FIELD(GENET_PHY_LPA, LPACK, 14, 1) +FIELD(GENET_PHY_LPA, NPAGE, 15, 1) + +REG16(GENET_PHY_STAT_1000, 0) +FIELD(GENET_PHY_STAT_1000, RSVD_0_9, 0, 10) +FIELD(GENET_PHY_STAT_1000, HALF, 10, 1) +FIELD(GENET_PHY_STAT_1000, FULL, 11, 1) +FIELD(GENET_PHY_STAT_1000, REMRXOK, 12, 1) +FIELD(GENET_PHY_STAT_1000, LOCALRXOK, 13, 1) +FIELD(GENET_PHY_STAT_1000, MSRES, 14, 1) +FIELD(GENET_PHY_STAT_1000, MSFAIL, 15, 1) + +REG16(GENET_PHY_AUX_CTRL_0, 0) +FIELD(GENET_PHY_AUX_CTRL_0, REG_ID_MASK, 0, 3) +FIELD(GENET_PHY_AUX_CTRL_0, RSVD_3, 3, 1) +FIELD(GENET_PHY_AUX_CTRL_0, REG_DATA, 4, 8) +FIELD(GENET_PHY_AUX_CTRL_0, REG_ID, 12, 3) +FIELD(GENET_PHY_AUX_CTRL_0, MISC_WREN, 15, 1) + +REG16(GENET_PHY_AUX_CTRL_1, 0) +FIELD(GENET_PHY_AUX_CTRL_1, RSVD_0_3, 0, 4) +FIELD(GENET_PHY_AUX_CTRL_1, REG_DATA, 4, 12) + +REG16(GENET_PHY_SHADOW, 0) +FIELD(GENET_PHY_SHADOW, REG_DATA, 0, 10) +FIELD(GENET_PHY_SHADOW, REG_ID, 10, 5) +FIELD(GENET_PHY_SHADOW, WR, 15, 1) + +REG16(GENET_PHY_EXP_SEL, 0) +FIELD(GENET_PHY_EXP_SEL, REG_ID, 0, 8) +FIELD(GENET_PHY_EXP_SEL, BLOCK_ID, 8, 8) +static uint64_t bcm2838_genet_read (void *opaque,hwaddr offset,unsigned size) {uint64_t value = ~0 ;
include/hw/net/bcm2838_genet.h +88 -0
114 lines not on this page · raw message
Signature · 1 line
2.55.0
Peter Maydell
AppliedQuoted 9 lines · Marcelo Manzo
This series gets raspi4b's PCIe root complex and GENET Ethernet controller working end to end: the PCIe link comes up and devices on it enumerate, and GENET passes real network traffic (DHCP, SSH, bulk downloads) under both older (Debian 11, 5.15) and newer (Debian 13, 6.18) guest kernels. Most of the GENET code is Sergey Kambalin's 2024 series (patchwork series 829638), carried forward with his authorship and Signed-off-by kept.
Applied to target-arm.next, thanks.
-- PMM