From nobody Thu Nov 14 18:02:36 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1708305948; cv=none; d=zohomail.com; s=zohoarc; b=QuUlKg4MzLoidsbQAYnN/e8tGSZOkX1MJ4gF+ivzSDI6Y5m+mK47IWy6Gju80qAUEB61biR2+9LGMLPFIGAdJujKw8L5nl87fhxOS9COMzmttTXSZsh/ix+/9KLCGV3ytQ3tTwHc+qfTC7bUdRKOcEUCTvUwMoZ0cKHXPoIMvXc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1708305948; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=eYmWgHZv1i9ycEb8Rk2NOoEFjXjhR9gUFRUNKobjtVA=; b=R534EbPIJWu9aKU4CJ1FIU4K6vKhuMOdE4ovaWvL1BoncSPq9bb1+JMto3+zQL9mw6M9PEOyBJKpPla9Jr2Xzd+3G3wdBPUnEPCOYdyucUhMYYHXlDYn9r+XUbHvuzCO7wCu5Lew9OfRsMChuWTsqYgYmptY/huc0/Tzpuwa5yo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1708305948346927.2240901486268; Sun, 18 Feb 2024 17:25:48 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rbsJT-0005EQ-OD; Sun, 18 Feb 2024 20:19:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rbsIe-0003YA-J9; Sun, 18 Feb 2024 20:18:29 -0500 Received: from mail-yw1-x1131.google.com ([2607:f8b0:4864:20::1131]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rbsIT-0004J3-K5; Sun, 18 Feb 2024 20:18:23 -0500 Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-6080beb19ddso14102007b3.1; Sun, 18 Feb 2024 17:18:15 -0800 (PST) Received: from localhost.localdomain ([201.203.117.224]) by smtp.gmail.com with ESMTPSA id m12-20020a819e0c000000b00607ff905ed3sm1204704ywj.58.2024.02.18.17.18.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 17:18:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708305494; x=1708910294; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eYmWgHZv1i9ycEb8Rk2NOoEFjXjhR9gUFRUNKobjtVA=; b=kObyLVuqA00IqR178DxEnZOMjszznv5NOUpNlgVyidfaEWOxzWp2Bsrhlomah00cYK yurHfq1uDfqRbnzFcsquXW4W7LFgcqIvYmE8GoA/V7kM1CXSrfkJkb7ZhasR1rPKXmg9 8xW3CwClYqXTh6dGdKL3lrDE8dVdO88LxV42pOyNBrwKRc8urBUst7q+CcQY+66YiLuQ NJRzkk3jkLxJW4i6VPx65qD7Wp4UVenouG3l4fO19PqOQAQw/KwDHDRHQOu6UiVM6LBn zyyFvuCI0Y9lTKjEZxMq/pJ/Ra8DVx+q9pWYwx/mrLg4arpKns/PdQtltdwqqQ8qc6bX 4fmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708305494; x=1708910294; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eYmWgHZv1i9ycEb8Rk2NOoEFjXjhR9gUFRUNKobjtVA=; b=Mv8lPPPXfRfODtUF5g37P9C6Ca3BV/Coipt2NT109XPCb44YERlt8bGKkxCqIxyf9D y9GXHeC+zW3a/U44bXJKeZXeZEnpDnildl/+Bu8MbQSBN3bHKtHrAhoq1FMvYg/h2/uy JHhIy5jUQqpOsm3PjvlFgU6KpMTTtm0GV68d1MyHvMt7v5sTrb3IcUeLjzgb3+PwpJij tk5kZsJTbfSAfuafe9reYDkW8GZ+TBhpKJyTXQi+VR+NBgl06p84Ii/5SNo3ECY4iws5 N03n4LtJYmdaci21tVnEsq5XoQhxuNUT86KAOl95xTyCawLaXTWx5vDIp80FfJSZQ0jT WiPg== X-Gm-Message-State: AOJu0YwT1eyTihmZSmyY0RN9LMtf7rkhcJ2EJk2YgLJbbqEMvZGBfOon MVkAGel4PpxqwRqiq7M53kkg8x4QTHeic3rR6xmoX2PfBgjNWezwj8xDIuN3e34= X-Google-Smtp-Source: AGHT+IGjHzqaWvNW7BH7JCb/u3QKgjcPMsIOBhQM+wvddmUmUHdrV/piJTE2fGqWrDXicTqB7n5XYg== X-Received: by 2002:a05:690c:3682:b0:608:2d21:7193 with SMTP id fu2-20020a05690c368200b006082d217193mr1741114ywb.44.1708305494589; Sun, 18 Feb 2024 17:18:14 -0800 (PST) From: Sergey Kambalin X-Google-Original-From: Sergey Kambalin To: qemu-arm@nongnu.org Cc: qemu-devel@nongnu.org, Sergey Kambalin Subject: [PATCH v5 25/41] Implement GENET register ops Date: Sun, 18 Feb 2024 19:17:23 -0600 Message-Id: <20240219011739.2316619-26-sergey.kambalin@auriga.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240219011739.2316619-1-sergey.kambalin@auriga.com> References: <20240219011739.2316619-1-sergey.kambalin@auriga.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1131; envelope-from=serg.oker@gmail.com; helo=mail-yw1-x1131.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1708305949892100005 Content-Type: text/plain; charset="utf-8" Signed-off-by: Sergey Kambalin --- hw/net/bcm2838_genet.c | 189 +++++++++++++++++++++++++++++++-- include/hw/net/bcm2838_genet.h | 2 + 2 files changed, 182 insertions(+), 9 deletions(-) diff --git a/hw/net/bcm2838_genet.c b/hw/net/bcm2838_genet.c index be899b68f8..56ff6a6f39 100644 --- a/hw/net/bcm2838_genet.c +++ b/hw/net/bcm2838_genet.c @@ -233,25 +233,144 @@ REG16(GENET_PHY_EXP_SEL, 0) FIELD(GENET_PHY_EXP_SEL, REG_ID, 0, 8) FIELD(GENET_PHY_EXP_SEL, BLOCK_ID, 8, 8) =20 +static void bcm2838_genet_set_qemu_mac(BCM2838GenetState *s) +{ + const MACAddr *addr =3D &s->nic_conf.macaddr; + + s->regs.umac.mac0 =3D FIELD_DP32(s->regs.umac.mac0, GENET_UMAC_MAC_0, + ADDR_0, addr->a[0]); + s->regs.umac.mac0 =3D FIELD_DP32(s->regs.umac.mac0, GENET_UMAC_MAC_0, + ADDR_1, addr->a[1]); + s->regs.umac.mac0 =3D FIELD_DP32(s->regs.umac.mac0, GENET_UMAC_MAC_0, + ADDR_2, addr->a[2]); + s->regs.umac.mac0 =3D FIELD_DP32(s->regs.umac.mac0, GENET_UMAC_MAC_0, + ADDR_3, addr->a[3]); + s->regs.umac.mac1 =3D FIELD_DP32(s->regs.umac.mac1, GENET_UMAC_MAC_1, + ADDR_4, addr->a[4]); + s->regs.umac.mac1 =3D 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 =3D s->regs.intrl0.stat; + uint32_t intrl_0_mask =3D s->regs.intrl0.mask_status; + int level =3D (intrl_0_status & ~intrl_0_mask) =3D=3D 0 ? 0 : 1; + + qemu_set_irq(s->irq_default, level); +} + +static void bcm2838_genet_set_irq_prio(BCM2838GenetState *s) +{ + uint32_t intrl_1_status =3D s->regs.intrl1.stat; + uint32_t intrl_1_mask =3D s->regs.intrl1.mask_status; + int level =3D (intrl_1_status & ~intrl_1_mask) =3D=3D 0 ? 0 : 1; + + qemu_set_irq(s->irq_prio, level); +} + static uint64_t bcm2838_genet_read(void *opaque, hwaddr offset, unsigned s= ize) { uint64_t value =3D ~0; + BCM2838GenetState *s =3D opaque; =20 - 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)) { + memcpy(&value, (uint8_t *)&s->regs + offset, size); + } else { + qemu_log_mask( + LOG_GUEST_ERROR, + "%s: out-of-range access, %u bytes @ offset 0x%04" PRIx64 "\n", + __func__, size, offset); + } =20 trace_bcm2838_genet_read(size, offset, value); return value; } =20 static void bcm2838_genet_write(void *opaque, hwaddr offset, uint64_t valu= e, - 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 =3D opaque; + MACAddr *mac =3D &s->nic_conf.macaddr; + NetClientState *ncs =3D qemu_get_queue(s->nic); + + trace_bcm2838_genet_write(size, offset, value); + + if (offset + size < sizeof(s->regs)) { + switch (offset) { + case BCM2838_GENET_INTRL0_SET: + s->regs.intrl0.stat |=3D value; + break; + case BCM2838_GENET_INTRL0_CLEAR: + s->regs.intrl0.stat &=3D ~value; + break; + case BCM2838_GENET_INTRL0_MASK_SET: + s->regs.intrl0.mask_status |=3D value; + break; + case BCM2838_GENET_INTRL0_MASK_CLEAR: + s->regs.intrl0.mask_status &=3D ~value; + break; + case BCM2838_GENET_INTRL1_SET: + s->regs.intrl1.stat |=3D value; + break; + case BCM2838_GENET_INTRL1_CLEAR: + s->regs.intrl1.stat &=3D ~value; + break; + case BCM2838_GENET_INTRL1_MASK_SET: + s->regs.intrl1.mask_status |=3D value; + break; + case BCM2838_GENET_INTRL1_MASK_CLEAR: + s->regs.intrl1.mask_status &=3D ~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) =3D=3D 1) { + device_cold_reset(DEVICE(s)); + value =3D 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] =3D FIELD_EX32(value, GENET_UMAC_MAC_0, ADDR_0); + mac->a[1] =3D FIELD_EX32(value, GENET_UMAC_MAC_0, ADDR_1); + mac->a[2] =3D FIELD_EX32(value, GENET_UMAC_MAC_0, ADDR_2); + mac->a[3] =3D FIELD_EX32(value, GENET_UMAC_MAC_0, ADDR_3); + qemu_macaddr_default_if_unset(mac); + 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] =3D FIELD_EX32(value, GENET_UMAC_MAC_1, ADDR_4); + mac->a[5] =3D FIELD_EX32(value, GENET_UMAC_MAC_1, ADDR_5); + qemu_macaddr_default_if_unset(mac); + qemu_format_nic_info_str(ncs, mac->a); + trace_bcm2838_genet_mac_address(ncs->info_str); + 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; + } + + memcpy((uint8_t *)&s->regs + offset, &value, size); + bcm2838_genet_set_irq_default(s); + bcm2838_genet_set_irq_prio(s); + } else { + qemu_log_mask( + LOG_GUEST_ERROR, + "%s: out-of-range access, %u bytes @ offset 0x%04" PRIx64 "\n", + __func__, size, offset); + } } =20 static const MemoryRegionOps bcm2838_genet_ops =3D { @@ -262,9 +381,14 @@ static const MemoryRegionOps bcm2838_genet_ops =3D { .valid =3D {.min_access_size =3D 4}, }; =20 +static NetClientInfo bcm2838_genet_client_info =3D { + .type =3D NET_CLIENT_DRIVER_NIC, + .size =3D sizeof(NICState) +}; =20 static void bcm2838_genet_realize(DeviceState *dev, Error **errp) { + NetClientState *ncs; BCM2838GenetState *s =3D BCM2838_GENET(dev); SysBusDevice *sbd =3D SYS_BUS_DEVICE(dev); =20 @@ -272,10 +396,46 @@ static void bcm2838_genet_realize(DeviceState *dev, E= rror **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 =3D 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 =3D 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= "); } =20 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 =3D 0x1140; + s->phy_regs.bmsr =3D 0x7949; + s->phy_regs.sid1 =3D 0x600D; + s->phy_regs.sid2 =3D 0x84A2; + s->phy_regs.advertise =3D 0x01E1; + s->phy_regs.ctrl1000 =3D 0x0200; + s->phy_regs.estatus =3D 0x3000; + + s->phy_shd_regs.clk_ctl =3D 0x0200; + s->phy_shd_regs.scr3 =3D 0x001F; + s->phy_shd_regs.apd =3D 0x0001; + + s->phy_aux_ctl_shd_regs.misc =3D 0x1E; + trace_bcm2838_genet_phy_reset("done"); } =20 @@ -285,17 +445,28 @@ static void bcm2838_genet_reset(DeviceState *d) =20 memset(&s->regs, 0x00, sizeof(s->regs)); =20 + s->regs.sys.rev_ctrl =3D FIELD_DP32(s->regs.sys.rev_ctrl, GENET_SYS_RE= V_CTRL, + MAJOR_REV, BCM2838_GENET_REV_MAJOR); + s->regs.sys.rev_ctrl =3D FIELD_DP32(s->regs.sys.rev_ctrl, GENET_SYS_RE= V_CTRL, + MINOR_REV, BCM2838_GENET_REV_MINOR); + trace_bcm2838_genet_reset("done"); =20 bcm2838_genet_phy_reset(s); } =20 +static Property genet_properties[] =3D { + DEFINE_NIC_PROPERTIES(BCM2838GenetState, nic_conf), + DEFINE_PROP_END_OF_LIST(), +}; + static void bcm2838_genet_class_init(ObjectClass *class, void *data) { DeviceClass *dc =3D DEVICE_CLASS(class); =20 dc->realize =3D bcm2838_genet_realize; dc->reset =3D bcm2838_genet_reset; + device_class_set_props(dc, genet_properties); } =20 static const TypeInfo bcm2838_genet_info =3D { diff --git a/include/hw/net/bcm2838_genet.h b/include/hw/net/bcm2838_genet.h index e7a76cda81..b9d6d35cce 100644 --- a/include/hw/net/bcm2838_genet.h +++ b/include/hw/net/bcm2838_genet.h @@ -388,6 +388,8 @@ struct BCM2838GenetState { SysBusDevice parent_obj; =20 /*< public >*/ + NICState *nic; + NICConf nic_conf; =20 MemoryRegion regs_mr; AddressSpace dma_as; --=20 2.34.1