From nobody Sun Nov 16 00:58:33 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1745377132; cv=none; d=zohomail.com; s=zohoarc; b=iSEKklvOkBLWejSEPR55F4XE6+CY2263YEkaNdOntqugESj1yNXdKCrhtxEiB1mhWPbJUY3aWSrRtcDRu/QFkxqyRwQA7VmVPB3ov1K0iDG2ZmaNHiU7PK2twvwVYBBK73DbRZ+/WBQxdAsAYTdYRwXwn+qKfw9yHsYh5midS20= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1745377132; h=Content-Type: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:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=r33nPRQHCna6qxJYb12rS42rb9sSGAOT+oN+FJNY0IA=; b=F3eVy9XxkALU1llnwgNXldxFz/8Huwt9TVeqNwVMSgWmnK9KGjlAOxfO0Y6ezCfv1Jl3yI7lmBAvHq5nnWHU1txH3A6pFQxC2U2GYDNk0jWh6naJy/Mm+bpVhkzX0mMhg4EEKmXGd+fwYAAjQx3rg73mBiOVH1Z+DqVe4ehrASI= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 17453771320591010.1498961180499; Tue, 22 Apr 2025 19:58:52 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u7QIa-0007GN-PD; Tue, 22 Apr 2025 22:57:20 -0400 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 1u7QIO-0007Bk-J7; Tue, 22 Apr 2025 22:57:11 -0400 Received: from mail.aspeedtech.com ([211.20.114.72] helo=TWMBX01.aspeed.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u7QIM-00043I-Cm; Tue, 22 Apr 2025 22:57:08 -0400 Received: from TWMBX01.aspeed.com (192.168.0.62) by TWMBX01.aspeed.com (192.168.0.62) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.12; Wed, 23 Apr 2025 10:56:52 +0800 Received: from mail.aspeedtech.com (192.168.10.10) by TWMBX01.aspeed.com (192.168.0.62) with Microsoft SMTP Server id 15.2.1258.12 via Frontend Transport; Wed, 23 Apr 2025 10:56:52 +0800 To: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , Peter Maydell , Steven Lee , Troy Lee , Jamin Lin , Andrew Jeffery , Joel Stanley , "open list:ASPEED BMCs" , "open list:All patches CC here" CC: , Kane-Chen-AS Subject: [PATCH v3 1/3] hw/misc/aspeed_otp: Add Aspeed OTP memory device model Date: Wed, 23 Apr 2025 10:56:49 +0800 Message-ID: <20250423025651.189702-2-kane_chen@aspeedtech.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250423025651.189702-1-kane_chen@aspeedtech.com> References: <20250423025651.189702-1-kane_chen@aspeedtech.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=211.20.114.72; envelope-from=kane_chen@aspeedtech.com; helo=TWMBX01.aspeed.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_FAIL=0.001, SPF_PASS=-0.001 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: , Reply-to: Kane Chen From: Kane Chen via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1745377134527019100 From: Kane-Chen-AS This introduces a new model for the ASPEED OTP (One-Time Programmable) memory. The device is implemented as a `SysBusDevice` and provides an abstracted interface for OTP read, write (program), and default value initialization. OTP content is backed by a block device and supports QEMU=E2=80=99s drive infrastructure via the "drive" property. Features: - Enforces irreversible bit programming logic (0->1 or 1->0) - Provides interface for SoC/secure controller integration - Validates bounds and bit-level constraints - Uses QEMU error handling conventions and logging Signed-off-by: Kane-Chen-AS --- hw/misc/aspeed_otpmem.c | 211 ++++++++++++++++++++++++++++++++ hw/misc/meson.build | 1 + include/hw/misc/aspeed_otpmem.h | 40 ++++++ 3 files changed, 252 insertions(+) create mode 100644 hw/misc/aspeed_otpmem.c create mode 100644 include/hw/misc/aspeed_otpmem.h diff --git a/hw/misc/aspeed_otpmem.c b/hw/misc/aspeed_otpmem.c new file mode 100644 index 0000000000..4f8f2827f7 --- /dev/null +++ b/hw/misc/aspeed_otpmem.c @@ -0,0 +1,211 @@ +/* + * ASPEED OTP (One-Time Programmable) memory + * + * Copyright (C) 2025 Aspeed + * + * This code is licensed under the GPL version 2 or later. See + * the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "hw/block/block.h" +#include "hw/block/flash.h" +#include "hw/qdev-properties.h" +#include "hw/qdev-properties-system.h" +#include "system/block-backend.h" +#include "qemu/log.h" +#include "qemu/option.h" +#include "hw/sysbus.h" +#include "qemu/error-report.h" +#include "hw/misc/aspeed_otpmem.h" + +static const Property aspeed_otpmem_properties[] =3D { + DEFINE_PROP_DRIVE("drive", AspeedOTPMemState, blk), +}; + +static void aspeed_otpmem_read(void *opaque, uint32_t addr, + uint32_t *out, Error **errp) +{ + AspeedOTPMemState *otp =3D ASPEED_OTPMEM(opaque); + + assert(otp->blk); + + if (out =3D=3D NULL) { + error_setg(errp, "out is NULL"); + return; + } + + if (addr > (otp->max_size - 4)) { + error_setg(errp, "OTP memory 0x%x is exceeded", addr); + return; + } + + if (blk_pread(otp->blk, (int64_t)addr, sizeof(uint32_t), out, 0) < 0) { + error_setg(errp, "Failed to read data 0x%x", addr); + return; + } + return; +} + +static bool valid_program_data(uint32_t otp_addr, + uint32_t value, uint32_t prog_bit) +{ + uint32_t programmed_bits, has_programmable_bits; + bool is_odd =3D otp_addr & 1; + + /* + * prog_bit uses 0s to indicate target bits to program: + * - if OTP word is even-indexed, programmed bits flip 0->1 + * - if odd, bits flip 1->0 + * Bit programming is one-way only and irreversible. + */ + if (is_odd) { + programmed_bits =3D ~value & prog_bit; + } else { + programmed_bits =3D value & (~prog_bit); + } + + /* If there is some bit can be programed, to accept the request */ + has_programmable_bits =3D value ^ (~prog_bit); + + if (programmed_bits) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Found programmed bits in addr %x\n", + __func__, otp_addr); + for (int i =3D 0; i < 32; ++i) { + if (programmed_bits & (1U << i)) { + qemu_log_mask(LOG_GUEST_ERROR, + " Programmed bit %d\n", + i); + } + } + } + + return has_programmable_bits !=3D 0; +} + +static bool program_otpmem_data(void *opaque, uint32_t otp_addr, + uint32_t prog_bit, uint32_t *value) +{ + AspeedOTPMemState *s =3D ASPEED_OTPMEM(opaque); + bool is_odd =3D otp_addr & 1; + uint32_t otp_offset =3D otp_addr << 2; + + if (blk_pread(s->blk, (int64_t)otp_offset, + sizeof(uint32_t), value, 0) < 0) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Failed to read data 0x%x\n", + __func__, otp_offset); + return false; + } + + if (!valid_program_data(otp_addr, *value, prog_bit)) { + return false; + } + + if (is_odd) { + *value &=3D ~prog_bit; + } else { + *value |=3D ~prog_bit; + } + + return true; +} + +static void aspeed_otpmem_prog(void *s, uint32_t otp_addr, + uint32_t data, Error **errp) +{ + AspeedOTPMemState *otp =3D ASPEED_OTPMEM(s); + uint32_t otp_offset, value; + + assert(otp->blk); + + if (otp_addr > (otp->max_size >> 2)) { + error_setg(errp, "OTP memory 0x%x is exceeded", otp_addr); + return; + } + + otp_offset =3D otp_addr << 2; + if (!program_otpmem_data(s, otp_addr, data, &value)) { + error_setg(errp, "Failed to program data"); + return; + } + + if (blk_pwrite(otp->blk, (int64_t)otp_offset, + sizeof(value), &value, 0) < 0) { + error_setg(errp, "Failed to write data"); + } + + return; +} + +static void aspeed_otpmem_set_default(void *s, uint32_t otp_offset, + uint32_t data, Error **errp) +{ + AspeedOTPMemState *otp =3D ASPEED_OTPMEM(s); + + if ((otp_offset + 4) > otp->max_size) { + error_setg(errp, "OTP memory 0x%x is exceeded", otp_offset); + return; + } + + if (blk_pwrite(otp->blk, (int64_t)otp_offset, + sizeof(data), &data, 0) < 0) { + error_setg(errp, "Failed to write data"); + } + return; +} + +static AspeedOTPMemOps aspeed_otpmem_ops =3D { + .read =3D aspeed_otpmem_read, + .prog =3D aspeed_otpmem_prog, + .set_default_value =3D aspeed_otpmem_set_default +}; + +static void aspeed_otpmem_realize(DeviceState *dev, Error **errp) +{ + AspeedOTPMemState *s =3D ASPEED_OTPMEM(dev); + + if (!s->blk) { + error_setg(&error_fatal, "OTP memory is not initialized"); + return; + } + + s->max_size =3D blk_getlength(s->blk); + if (s->max_size < 0 || (s->max_size % 4)) { + error_setg(&error_fatal, + "Unexpected OTP memory size: %" PRId64 "", + s->max_size); + return; + } + + s->ops =3D &aspeed_otpmem_ops; + + return; +} + +static void aspeed_otpmem_system_reset(DeviceState *dev) +{ + return; +} + +static void aspeed_otpmem_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + + device_class_set_legacy_reset(dc, aspeed_otpmem_system_reset); + dc->realize =3D aspeed_otpmem_realize; + device_class_set_props(dc, aspeed_otpmem_properties); + +} + +static const TypeInfo aspeed_otpmem_types[] =3D { + { + .name =3D TYPE_ASPEED_OTPMEM, + .parent =3D TYPE_SYS_BUS_DEVICE, + .instance_size =3D sizeof(AspeedOTPMemState), + .class_init =3D aspeed_otpmem_class_init, + }, +}; + +DEFINE_TYPES(aspeed_otpmem_types) diff --git a/hw/misc/meson.build b/hw/misc/meson.build index 6d47de482c..ed1eaaa2ad 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -136,6 +136,7 @@ system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files( 'aspeed_sbc.c', 'aspeed_sdmc.c', 'aspeed_xdma.c', + 'aspeed_otpmem.c', 'aspeed_peci.c', 'aspeed_sli.c')) =20 diff --git a/include/hw/misc/aspeed_otpmem.h b/include/hw/misc/aspeed_otpme= m.h new file mode 100644 index 0000000000..11e2de70b6 --- /dev/null +++ b/include/hw/misc/aspeed_otpmem.h @@ -0,0 +1,40 @@ +/* + * ASPEED OTP (One-Time Programmable) memory + * + * Copyright (C) 2025 Aspeed + * + * This code is licensed under the GPL version 2 or later. See + * the COPYING file in the top-level directory. + */ + +#ifndef ASPEED_OTPMMEM_H +#define ASPEED_OTPMMEM_H + +#include "hw/sysbus.h" +#include "qapi/error.h" + +#define TYPE_ASPEED_OTPMEM "aspeed.otpmem" +#define ASPEED_OTPMEM_DRIVE "otpmem" + +#define ASPEED_OTPMEM(obj) OBJECT_CHECK(AspeedOTPMemState, (obj), \ + TYPE_ASPEED_OTPMEM) + +typedef struct AspeedOTPMemOps { + void (*read)(void *s, uint32_t addr, uint32_t *out, Error **errp); + void (*prog)(void *s, uint32_t addr, uint32_t data, Error **errp); + void (*set_default_value)(void *s, uint32_t otp_offset, + uint32_t data, Error **errp); +} AspeedOTPMemOps; + +typedef struct AspeedOTPMemState { + SysBusDevice parent_obj; + + MemoryRegion mmio; + BlockBackend *blk; + int64_t max_size; + + AspeedOTPMemOps *ops; +} AspeedOTPMemState; + +#endif /* ASPEED_OTPMMEM_H */ + --=20 2.43.0