From nobody Thu Nov 14 17:50:05 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=1708906291; cv=none; d=zohomail.com; s=zohoarc; b=JLDDoWwJ7oPNlc0QPwfVeEFBQfiOOaCH+WswDdvjkYeh6h8HxN23HFWqR5r+34FvkD6rLIYNYXVzsdjsBzKv4yey9iWqCiuNVlcrTmcLr9G6lM2W3te60DXlgbYl9OpjtlyJeWJ1+y6XMNu5tW0zPB9if8Vnfk8BWvwUl3aBqqM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1708906291; 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=K6MO4yi+QHk/hVHG1IV9lxkY916xA1sTegA4CtSfs2gAe1EfijCSb0wC8WzXEtnB4X1WE30URb3CxBVz5ZlLSMWSWRtC0YRV0O43vOfKVQkA4ylBKQxAGXdc8iuRztWJjaKiVaiBMVPH6le8uVSmbYymuctNCjCYWA/8KfZw8zc= 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 1708906291311420.6026019324745; Sun, 25 Feb 2024 16:11:31 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1reOTz-0007t4-Qi; Sun, 25 Feb 2024 19:04:35 -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 1reOT2-0006nT-9r; Sun, 25 Feb 2024 19:03:36 -0500 Received: from mail-yw1-x112d.google.com ([2607:f8b0:4864:20::112d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1reOT0-0000TD-8H; Sun, 25 Feb 2024 19:03:36 -0500 Received: by mail-yw1-x112d.google.com with SMTP id 00721157ae682-6087192b092so27905157b3.0; Sun, 25 Feb 2024 16:03:33 -0800 (PST) Received: from localhost.localdomain ([201.203.117.224]) by smtp.gmail.com with ESMTPSA id t18-20020a818312000000b00607bfa1913csm938171ywf.114.2024.02.25.16.03.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 16:03:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708905813; x=1709510613; 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=Rr/m6vhtw+lrTc7zfLLGAzzkE1NJlZUtxAwVV+NwMGAXEtrjQA87f0Kb12njU6OwIu wzrMNKBUu17qPkOHdhluC2VxuPlWRdh3FlCtuKihSeR4ZS7aGeENX/t0K0BFq1tiYtmN 11HDNz911M7mceqo5KmvJdHrF0kwVt4htYdqlXnh+oT8VNAtVtU05b2i3yyJYzy0upOz nlfPrbfgp1vuGbpI/xo9HFQlKytacGmGrdGXJvQQzDSQyW+ZNkQYjFr1RCsGyOdYKp+O tnZMGlZ4S/GpgENjDXRFhrtfPhc632NVqMdhzanbd2srXRU1rN59W4kI7wFp+oEZXZxs NW9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708905813; x=1709510613; 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=IzzH9ufoqoGbb0GYAEHGXK1kh1QD3E7nGrMqh0G6ZXFneL/NlOQZiEC+5mb1kN5Hec MadBb5AeHXuLOILXmDQ5liO2SGv2Fuh7UhZ9AQHUdl6sVbjOdtxsyHnKyX0KDXWBoZ/v PknEZQW4BojRxjLBZ37hPDdImu2IZ9YcWNCJ9li8ZCZhFoNXg9ZgjuZ9b2E5VEotsRm7 IioGuUgMCn2QW29jrf0todw9diap+1/Mkgw34MUPr+6elcTyCUUnmXCt7DIyWThfUdaC bBemgws17k2VDlXut0Zfg2ClYbVWMOaO66uqxljtQ8vWgCWCkEmrh6QRV4v5uO0QNhkr OuIQ== X-Gm-Message-State: AOJu0Yw6+oGXyWl0izFOcIa04u5WIPcDh2ADwDqDW1kokPalyUZbdBFH Ipfs4TkrLy707rBv8RjNMfQotBTxso87dQPaCLlTu1XjNpKqGxjz9JThA84yhOcNsw== X-Google-Smtp-Source: AGHT+IG/0wqLkAyy/+3hR3pWQqmq4NN+VnzaXAPYaNSo3n3fSmh36r8Qd7Q4+oKrpq6bpk/r8J1M5Q== X-Received: by 2002:a0d:e8c1:0:b0:607:9e4f:7274 with SMTP id r184-20020a0de8c1000000b006079e4f7274mr5770323ywe.7.1708905812727; Sun, 25 Feb 2024 16:03:32 -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 v6 25/41] Implement GENET register ops Date: Sun, 25 Feb 2024 18:02:43 -0600 Message-Id: <20240226000259.2752893-26-sergey.kambalin@auriga.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240226000259.2752893-1-sergey.kambalin@auriga.com> References: <20240226000259.2752893-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::112d; envelope-from=serg.oker@gmail.com; helo=mail-yw1-x112d.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: 1708906291731100005 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