Add QEMU model for ASPEED OTP memory and integrate with SoC
Subsystems (2)
v6 cover Add QEMU model for ASPEED OTP memory and integrate with SoC
From: Kane-Chen-AS <[email protected]>
This patch series introduces a QEMU model for the ASPEED OTP (One-Time Programmable) memory, along with its integration into the Secure Boot Controller (SBC) and supported SoC (AST2600).
The OTP model emulates a simple fuse array used for secure boot or device configuration, implemented with internal buffers; external file/device support not included in this version. It exposes an AddressSpace to support transaction-based access from controllers like the SBC.
This series includes:
- OTP memory device implementation
- SBC integration with command decoding (READ/PROG)
- Direct integration in AST2600 SoC without requiring user parameters
**[RFC notice]**
This is an RFC submission. This version is primarily intended to gather feedback on how the OTP instance is created, connected, and integrated in the SoC structure.
A more finalized version with any requested changes, documentation, and test refinements will follow after review.
Any feedback or suggestions are appreciated!
---
ChangeLog
---------
v6:
- remove machine parameter 'otpmem' and BlockBackend
- remove alias relationship between machine parameter and BlockBackend
- simplify to static internal OTP instance with direct connection in SoC
- Submitted as [RFC] to gather feedback on QOM integration approach
v5:
- Switched to notifier-based late binding between SBC and OTP
- Exposed 'otpmem' as a machine parameter using QOM alias mechanism
- Added fallback support when no backend is provided
- Introduced functional test suite covering multiple init configurations
- Submitted as [RFC] to gather feedback on QOM integration approach
v4:
- Replaces blockdev backend with memory-mapped AddressSpace access
- Switches from -drive CLI option to otpmem machine parameter
- Restructures the OTP device under the SBC in the QOM tree
v3:
- Minor fixes and refinements on top of v2 content
v2:
- Separated the OTP memory into a standalone QEMU device (aspeed.otpmem)
- Replaced ad-hoc file handling with standard -drive backend integration
- Linked OTP to SBC via QOM property for cleaner abstraction
- Improved memory bounds checking and irreversible programming logic
v1:
- Embedded OTP logic in the SBC model and created the backing file internally.
---
Kane-Chen-AS (3):
hw/misc/aspeed_otp: Add ASPEED OTP memory device model hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC hw/arm: Integrate ASPEED OTP memory support into AST2600 SoCs
hw/arm/aspeed_ast2600.c | 2 +- hw/misc/aspeed_otpmem.c | 85 +++++++++++++++++++++++++++++ hw/misc/aspeed_sbc.c | 94 +++++++++++++++++++++++++++++++++ hw/misc/meson.build | 1 + hw/misc/trace-events | 7 +++ include/hw/misc/aspeed_otpmem.h | 33 ++++++++++++ include/hw/misc/aspeed_sbc.h | 5 ++ 7 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 hw/misc/aspeed_otpmem.c create mode 100644 include/hw/misc/aspeed_otpmem.h
Signature · 1 line
2.43.0
RFC v6 2/3 hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC
From: Kane-Chen-AS <[email protected]>
This patch connects the aspeed.otpmem device to the ASPEED Secure Boot Controller (SBC) model. It implements OTP memory access via the SBC's command interface and enables emulation of secure fuse programming flows.
The following OTP commands are supported:
- READ: reads a 32-bit word from OTP memory into internal registers
- PROG: programs a 32-bit word value to the specified OTP address
Trace events are added to observe read/program operations and command handling flow.
--- hw/misc/aspeed_sbc.c | 92 ++++++++++++++++++++++++++++++++++++ hw/misc/trace-events | 7 +++ include/hw/misc/aspeed_sbc.h | 3 ++ 3 files changed, 102 insertions(+)
hw/misc/aspeed_sbc.c +92 -0
@@ -15,9 +15,13 @@#include "hw/misc/aspeed_sbc.h" #include "qapi/error.h" #include "migration/vmstate.h" +#include "trace.h" #define R_PROT (0x000 / 4) +#define R_CMD (0x004 / 4) +#define R_ADDR (0x010 / 4)#define R_STATUS (0x014 / 4) +#define R_CAMP1 (0x020 / 4)#define R_QSR (0x040 / 4)/* R_STATUS */
@@ -41,6 +45,11 @@#define QSR_RSA_MASK (0x3 << 12)#define QSR_HASH_MASK (0x3 << 10) +typedef enum { +SBC_OTP_CMD_READ =0x23b1e361 , +SBC_OTP_CMD_PROG =0x23b1e364 , +}SBC_OTP_Command ; +static uint64_t aspeed_sbc_read (void *opaque,hwaddr addr,unsigned int size) {AspeedSBCState *s =ASPEED_SBC (opaque);
@@ -57,6 +66,78 @@ static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size)return s->regs[addr]; } +static bool aspeed_sbc_otpmem_read (AspeedSBCState *s, +uint32_t otp_addr,Error **errp) +{ +MemTxResult ret; +AspeedOTPMemState *otpmem =ASPEED_OTPMEM (s->otpmem); +uint32_t value; + +if (otpmem ==NULL ) { +return true; + } + ret =address_space_read (&otpmem->as, otp_addr,MEMTXATTRS_UNSPECIFIED , + &value,sizeof (value)); +if (ret !=0 ) { +qemu_log_mask (LOG_GUEST_ERROR ,"Failed to read OTP memory\n" ); +return false; + } + s->regs[R_CAMP1 ] = value; +trace_aspeed_sbc_otp_read (otp_addr, value); + +return true; +} + +static bool aspeed_sbc_otpmem_prog (AspeedSBCState *s, +uint32_t otp_addr,Error **errp) +{ +AspeedOTPMemState *otpmem =ASPEED_OTPMEM (s->otpmem); +uint32_t value =0x12345678 ; + +if (otpmem ==NULL ) { +return true; + } +address_space_write (&otpmem->as, otp_addr,MEMTXATTRS_UNSPECIFIED , + &value,sizeof (value)); +trace_aspeed_sbc_otp_prog (otp_addr, value); + +return true; +} + +static void aspeed_sbc_handle_command (void *opaque,uint32_t cmd) +{ +AspeedSBCState *s =ASPEED_SBC (opaque); +Error *local_err =NULL ; +bool ret = false; +uint32_t otp_addr; + + s->regs[R_STATUS ] &= ~(OTP_MEM_IDLE |OTP_IDLE ); + otp_addr = s->regs[R_ADDR ]; + +switch (cmd) { +case SBC_OTP_CMD_READ : + ret =aspeed_sbc_otpmem_read (s, otp_addr, &local_err); +break ; +case SBC_OTP_CMD_PROG : + ret =aspeed_sbc_otpmem_prog (s, otp_addr, &local_err); +break ; +default : +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Unknown command 0x%x\n" , + __func__, cmd); +break ; + } + +trace_aspeed_sbc_handle_cmd (cmd, otp_addr, ret); +if (ret == false && local_err) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: %s\n" , + __func__,error_get_pretty (local_err)); +error_free (local_err); + } + s->regs[R_STATUS ] |= (OTP_MEM_IDLE |OTP_IDLE ); +} +static void aspeed_sbc_write (void *opaque,hwaddr addr,uint64_t data,unsigned int size) {
@@ -78,6 +159,9 @@ static void aspeed_sbc_write(void *opaque, hwaddr addr, uint64_t data,"%s: write to read only register 0x%" HWADDR_PRIx"\n" , __func__, addr << 2);return ; +case R_CMD : +aspeed_sbc_handle_command (opaque, data); +return ;default :break ; }
@@ -119,11 +203,19 @@ static void aspeed_sbc_realize(DeviceState *dev, Error **errp) {AspeedSBCState *s =ASPEED_SBC (dev);SysBusDevice *sbd =SYS_BUS_DEVICE (dev); +AspeedSBCClass *sc =ASPEED_SBC_GET_CLASS (s);memory_region_init_io (&s->iomem,OBJECT (s), &aspeed_sbc_ops, s,TYPE_ASPEED_SBC ,0x1000 );sysbus_init_mmio (sbd, &s->iomem); + +if (sc->has_otpmem) { + s->otpmem =ASPEED_OTPMEM (object_new (TYPE_ASPEED_OTPMEM )); +object_initialize_child (OBJECT (s),"otp" , s->otpmem, +TYPE_ASPEED_OTPMEM ); +qdev_realize (DEVICE (s->otpmem),NULL , errp); + } }static const VMStateDescription vmstate_aspeed_sbc = {
hw/misc/trace-events +7 -0
@@ -90,6 +90,13 @@ slavio_sysctrl_mem_readl(uint32_t ret) "Read system control 0x%08x" slavio_led_mem_writew(uint32_t val) "Write diagnostic LED 0x%04x" slavio_led_mem_readw(uint32_t ret) "Read diagnostic LED 0x%04x" +# aspped_sbc.c +aspeed_sbc_otpmem_state(const char *enabled) "OTP Memory is %s" +aspeed_sbc_ignore_cmd(uint32_t cmd) "Ignoring command 0x%" PRIx32 +aspeed_sbc_handle_cmd(uint32_t cmd, uint32_t addr, bool ret) "Handling command 0x%" PRIx32 " for OTP addr 0x%" PRIx32 " Result: %d" +aspeed_sbc_otp_read(uint32_t addr, uint32_t value) "OTP Memory read: addr 0x%" PRIx32 " value 0x%" PRIx32 +aspeed_sbc_otp_prog(uint32_t addr, uint32_t value) "OTP Memory write: addr 0x%" PRIx32 " value 0x%" PRIx32 + # aspeed_scu.c aspeed_scu_write(uint64_t offset, unsigned size, uint32_t data) "To 0x%" PRIx64 " of size %u: 0x%" PRIx32 aspeed_scu_read(uint64_t offset, unsigned size, uint32_t data) "To 0x%" PRIx64 " of size %u: 0x%" PRIx32
include/hw/misc/aspeed_sbc.h +3 -0
@@ -10,6 +10,7 @@#define ASPEED_SBC_H #include "hw/sysbus.h" +#include "hw/misc/aspeed_otpmem.h" #define TYPE_ASPEED_SBC "aspeed.sbc"#define TYPE_ASPEED_AST2600_SBC TYPE_ASPEED_SBC "-ast2600"
@@ -36,6 +37,8 @@ struct AspeedSBCState {MemoryRegion iomem;uint32_t regs[ASPEED_SBC_NR_REGS ]; + +AspeedOTPMemState *otpmem; };struct AspeedSBCClass {
Signature · 1 line
2.43.0
Re: [RFC v6 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC
Quoted 39 lines · Kane Chen
From: Kane-Chen-AS <[email protected]>
This patch connects the aspeed.otpmem device to the ASPEED Secure Boot Controller (SBC) model. It implements OTP memory access via the SBC's command interface and enables emulation of secure fuse programming flows.
The following OTP commands are supported:
- READ: reads a 32-bit word from OTP memory into internal registers
- PROG: programs a 32-bit word value to the specified OTP addressTrace events are added to observe read/program operations and command handling flow.
Signed-off-by Kane-Chen-AS <[email protected]>--- hw/misc/aspeed_sbc.c | 92 ++++++++++++++++++++++++++++++++++++ hw/misc/trace-events | 7 +++ include/hw/misc/aspeed_sbc.h | 3 ++ 3 files changed, 102 insertions(+)hw/misc/aspeed_sbc.c +4
-0@@ -15,9 +15,13 @@#include "hw/misc/aspeed_sbc.h" #include "qapi/error.h" #include "migration/vmstate.h" +#include "trace.h" #define R_PROT (0x000 / 4) +#define R_CMD (0x004 / 4) +#define R_ADDR (0x010 / 4)#define R_STATUS (0x014 / 4) +#define R_CAMP1 (0x020 / 4)#define R_QSR (0x040 / 4)/* R_STATUS */
hw/misc/aspeed_sbc.c line 51
Quoted 7 lines
hw/misc/aspeed_sbc.c +4
-0@@ -41,6 +45,11 @@#define QSR_RSA_MASK (0x3 << 12)#define QSR_HASH_MASK (0x3 << 10) +typedef enum { +SBC_OTP_CMD_READ =0x23b1e361 , +SBC_OTP_CMD_PROG =0x23b1e364 , +}SBC_OTP_Command ;
Change these values to define
hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 70
Quoted 9 lines
hw/misc/aspeed_sbc.c +2
-0static uint64_t aspeed_sbc_read (void *opaque,hwaddr addr,unsigned int size) {AspeedSBCState *s =ASPEED_SBC (opaque);@@ -57,6 +66,78 @@ static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size)return s->regs[addr]; } +static bool aspeed_sbc_otpmem_read (AspeedSBCState *s, +uint32_t otp_addr,Error **errp)
errp looks unused.
hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 73
Quoted 3 lines
hw/misc/aspeed_sbc.c +3
-0+{ +MemTxResult ret; +AspeedOTPMemState *otpmem =ASPEED_OTPMEM (s->otpmem);
The QOM cast above is not needed.
hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 78
Quoted 5 lines
hw/misc/aspeed_sbc.c +5
-0+uint32_t value; + +if (otpmem ==NULL ) { +return true; + }
no need to test otpmem.
hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 81
Quoted 3 lines
hw/misc/aspeed_sbc.c +3
-0+ ret =address_space_read (&otpmem->as, otp_addr,MEMTXATTRS_UNSPECIFIED , + &value,sizeof (value)); +if (ret !=0 ) {
if (ret != MEMTX_OK) {
hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 82
Quoted 1 line
hw/misc/aspeed_sbc.c +1
-0+qemu_log_mask (LOG_GUEST_ERROR ,"Failed to read OTP memory\n" );
May be log the address too
hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 92
Quoted 10 lines
hw/misc/aspeed_sbc.c +10
-0+return false; + } + s->regs[R_CAMP1 ] = value; +trace_aspeed_sbc_otp_read (otp_addr, value); + +return true; +} + +static bool aspeed_sbc_otpmem_prog (AspeedSBCState *s, +uint32_t otp_addr,Error **errp)
errp looks unused.
hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 99
Quoted 7 lines
hw/misc/aspeed_sbc.c +7
-0+{ +AspeedOTPMemState *otpmem =ASPEED_OTPMEM (s->otpmem); +uint32_t value =0x12345678 ; + +if (otpmem ==NULL ) { +return true; + }
no need to test otpmem.
hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 101
Quoted 2 lines
hw/misc/aspeed_sbc.c +2
-0+address_space_write (&otpmem->as, otp_addr,MEMTXATTRS_UNSPECIFIED , + &value,sizeof (value));
Please test the return value.
Quoted 8 lines
hw/misc/aspeed_sbc.c +8
-0+trace_aspeed_sbc_otp_prog (otp_addr, value); + +return true; +} + +static void aspeed_sbc_handle_command (void *opaque,uint32_t cmd) +{ +AspeedSBCState *s =ASPEED_SBC (opaque);
hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 119
Quoted 10 lines
hw/misc/aspeed_sbc.c +10
-0+Error *local_err =NULL ; +bool ret = false; +uint32_t otp_addr; + + s->regs[R_STATUS ] &= ~(OTP_MEM_IDLE |OTP_IDLE ); + otp_addr = s->regs[R_ADDR ]; + +switch (cmd) { +case SBC_OTP_CMD_READ : + ret =aspeed_sbc_otpmem_read (s, otp_addr, &local_err);
&local_err is unused. please remove.
Quoted 25 lines
hw/misc/aspeed_sbc.c +21
-0+break ; +case SBC_OTP_CMD_PROG : + ret =aspeed_sbc_otpmem_prog (s, otp_addr, &local_err); +break ; +default : +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Unknown command 0x%x\n" , + __func__, cmd); +break ; + } + +trace_aspeed_sbc_handle_cmd (cmd, otp_addr, ret); +if (ret == false && local_err) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: %s\n" , + __func__,error_get_pretty (local_err)); +error_free (local_err); + } + s->regs[R_STATUS ] |= (OTP_MEM_IDLE |OTP_IDLE ); +} +static void aspeed_sbc_write (void *opaque,hwaddr addr,uint64_t data,unsigned int size) {
hw/misc/aspeed_sbc.c static void aspeed_sbc_write(void *opaque, hwaddr addr, uint64_t data, · line 163
Quoted 5 lines
hw/misc/aspeed_sbc.c +2
-0@@ -78,6 +159,9 @@ static void aspeed_sbc_write(void *opaque, hwaddr addr, uint64_t data,"%s: write to read only register 0x%" HWADDR_PRIx"\n" , __func__, addr << 2);return ; +case R_CMD : +aspeed_sbc_handle_command (opaque, data);
I would test sc->has_otpmem before calling aspeed_sbc_handle_command() and do a LOG_GUEST_ERROR if it shouldn't be called.
Quoted 7 lines
hw/misc/aspeed_sbc.c +1
-0+return ;default :break ; }@@ -119,11 +203,19 @@ static void aspeed_sbc_realize(DeviceState *dev, Error **errp) {AspeedSBCState *s =ASPEED_SBC (dev);
hw/misc/aspeed_sbc.c static void aspeed_sbc_realize(DeviceState *dev, Error **errp) · line 214
Quoted 10 lines
hw/misc/aspeed_sbc.c +4
-0SysBusDevice *sbd =SYS_BUS_DEVICE (dev); +AspeedSBCClass *sc =ASPEED_SBC_GET_CLASS (s);memory_region_init_io (&s->iomem,OBJECT (s), &aspeed_sbc_ops, s,TYPE_ASPEED_SBC ,0x1000 );sysbus_init_mmio (sbd, &s->iomem); + +if (sc->has_otpmem) { + s->otpmem =ASPEED_OTPMEM (object_new (TYPE_ASPEED_OTPMEM ));
you don't need to allocate s->otpmem.
hw/misc/aspeed_sbc.c static void aspeed_sbc_realize(DeviceState *dev, Error **errp) · line 216
Quoted 2 lines
hw/misc/aspeed_sbc.c +2
-0+object_initialize_child (OBJECT (s),"otp" , s->otpmem, +TYPE_ASPEED_OTPMEM );
Please move object_initialize_child() under a instance_init handler which should be called only if sc->has_otpmem
hw/misc/aspeed_sbc.c static void aspeed_sbc_realize(DeviceState *dev, Error **errp) · line 217
Quoted 1 line
hw/misc/aspeed_sbc.c +1
-0+qdev_realize (DEVICE (s->otpmem),NULL , errp);
Please test the return value.
Quoted 35 lines
hw/misc/aspeed_sbc.c +1
-0+ } }static const VMStateDescription vmstate_aspeed_sbc = {hw/misc/trace-events +7
-0@@ -90,6 +90,13 @@ slavio_sysctrl_mem_readl(uint32_t ret) "Read system control 0x%08x" slavio_led_mem_writew(uint32_t val) "Write diagnostic LED 0x%04x" slavio_led_mem_readw(uint32_t ret) "Read diagnostic LED 0x%04x" +# aspped_sbc.c +aspeed_sbc_otpmem_state(const char *enabled) "OTP Memory is %s" +aspeed_sbc_ignore_cmd(uint32_t cmd) "Ignoring command 0x%" PRIx32 +aspeed_sbc_handle_cmd(uint32_t cmd, uint32_t addr, bool ret) "Handling command 0x%" PRIx32 " for OTP addr 0x%" PRIx32 " Result: %d" +aspeed_sbc_otp_read(uint32_t addr, uint32_t value) "OTP Memory read: addr 0x%" PRIx32 " value 0x%" PRIx32 +aspeed_sbc_otp_prog(uint32_t addr, uint32_t value) "OTP Memory write: addr 0x%" PRIx32 " value 0x%" PRIx32 + # aspeed_scu.c aspeed_scu_write(uint64_t offset, unsigned size, uint32_t data) "To 0x%" PRIx64 " of size %u: 0x%" PRIx32 aspeed_scu_read(uint64_t offset, unsigned size, uint32_t data) "To 0x%" PRIx64 " of size %u: 0x%" PRIx32include/hw/misc/aspeed_sbc.h +1
-0@@ -10,6 +10,7 @@#define ASPEED_SBC_H #include "hw/sysbus.h" +#include "hw/misc/aspeed_otpmem.h" #define TYPE_ASPEED_SBC "aspeed.sbc"#define TYPE_ASPEED_AST2600_SBC TYPE_ASPEED_SBC "-ast2600"
include/hw/misc/aspeed_sbc.h struct AspeedSBCState { · line 41
Quoted 5 lines
include/hw/misc/aspeed_sbc.h +2
-0@@ -36,6 +37,8 @@ struct AspeedSBCState {MemoryRegion iomem;uint32_t regs[ASPEED_SBC_NR_REGS ]; + +AspeedOTPMemState *otpmem;
Please change to :
AspeedOTPMemState otpmem;
include/hw/misc/aspeed_sbc.h struct AspeedSBCState { · line 44
Quoted 3 lines
include/hw/misc/aspeed_sbc.h +0
-0};struct AspeedSBCClass {
what about has_otpmem definition ? Without it, this patch won't compile and this breaks bisect.
Thanks,
C.
RFC v6 1/3 hw/misc/aspeed_otp: Add ASPEED OTP memory device model
In reply to Kane Chen viaFrom: Kane-Chen-AS <[email protected]>
Introduce a QEMU device model for ASPEED's One-Time Programmable (OTP) memory.
This model simulates a word-addressable OTP region used for secure fuse storage. The OTP memory can operate with an internal memory buffer.
The OTP model provides a memory-like interface through a dedicated AddressSpace, allowing other device models (e.g., SBC) to issue transactions as if accessing a memory-mapped region.
--- hw/misc/aspeed_otpmem.c | 85 +++++++++++++++++++++++++++++++++ hw/misc/meson.build | 1 + include/hw/misc/aspeed_otpmem.h | 33 +++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 hw/misc/aspeed_otpmem.c create mode 100644 include/hw/misc/aspeed_otpmem.h
hw/misc/aspeed_otpmem.c new file +85 -0
@@ -0,0 +1,85 @@ +/* +* ASPEED OTP (One-Time Programmable) memory +* +* Copyright (C) 2025 Aspeed +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qapi/error.h" +#include "trace.h" +#include "system/block-backend-global-state.h" +#include "system/block-backend-io.h" +#include "hw/misc/aspeed_otpmem.h" + +static uint64_t aspeed_otpmem_read (void *opaque,hwaddr offset,unsigned size) +{ +AspeedOTPMemState *s = opaque; +uint64_t val =0 ; + +memcpy (&val, s->storage + offset, size); + +return val; +} + +static void aspeed_otpmem_write (void *opaque,hwaddr offset, +uint64_t val,unsigned size) +{ +AspeedOTPMemState *s = opaque; + +memcpy (s->storage + offset, &val, size); +} + +static const MemoryRegionOps aspeed_otpmem_ops = { + .read = aspeed_otpmem_read, + .write = aspeed_otpmem_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .valid.min_access_size =1 , + .valid.max_access_size =4 , +}; + +static void aspeed_otpmem_realize (DeviceState *dev,Error **errp) +{ +AspeedOTPMemState *s =ASPEED_OTPMEM (dev); +const size_t size =OTPMEM_SIZE ; +int i, num; +uint32_t *p; + + s->storage =g_malloc (size); +if (!s->storage) { +error_setg (errp,"Failed to allocate OTP memory storage buffer" ); +return ; + } + + num = size /sizeof (uint32_t ); + p = (uint32_t *)s->storage; +for (i =0 ; i < num; i++) { + p[i] = (i %2 ==0 ) ?0x00000000 :0xFFFFFFFF ; + } + +memory_region_init_io (&s->mmio,OBJECT (dev), &aspeed_otpmem_ops, + s,"aspeed.otpmem" , size); +address_space_init (&s->as, &s->mmio,NULL ); +} + +static void aspeed_otpmem_class_init (ObjectClass *klass,const void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + dc->realize = aspeed_otpmem_realize; +} + +static const TypeInfo aspeed_otpmem_info = { + .name =TYPE_ASPEED_OTPMEM , + .parent =TYPE_DEVICE , + .instance_size =sizeof (AspeedOTPMemState), + .class_init = aspeed_otpmem_class_init, +}; + +static void aspeed_otpmem_register_types (void ) +{ +type_register_static (&aspeed_otpmem_info); +} + +type_init (aspeed_otpmem_register_types)
hw/misc/meson.build +1 -0
@@ -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'))
include/hw/misc/aspeed_otpmem.h new file +33 -0
@@ -0,0 +1,33 @@ +/* +* ASPEED OTP (One-Time Programmable) memory +* +* Copyright (C) 2025 Aspeed +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef ASPEED_OTPMMEM_H +#define ASPEED_OTPMMEM_H + +#include "system/memory.h" +#include "hw/block/block.h" +#include "system/memory.h" +#include "system/address-spaces.h" + +#define OTPMEM_SIZE 0x4000 +#define TYPE_ASPEED_OTPMEM "aspeed.otpmem" +OBJECT_DECLARE_SIMPLE_TYPE(AspeedOTPMemState, ASPEED_OTPMEM) + +typedef struct AspeedOTPMemState { +DeviceState parent_obj; + +uint64_t size; + +AddressSpace as; + +MemoryRegion mmio; + +uint8_t *storage; +}AspeedOTPMemState ; + +#endif /* ASPEED_OTPMMEM_H */
Signature · 1 line
2.43.0
Re: [RFC v6 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device model
Quoted 64 lines · Kane Chen
From: Kane-Chen-AS <[email protected]>
Introduce a QEMU device model for ASPEED's One-Time Programmable (OTP) memory.
This model simulates a word-addressable OTP region used for secure fuse storage. The OTP memory can operate with an internal memory buffer.
The OTP model provides a memory-like interface through a dedicated AddressSpace, allowing other device models (e.g., SBC) to issue transactions as if accessing a memory-mapped region.
Signed-off-by Kane-Chen-AS <[email protected]>--- hw/misc/aspeed_otpmem.c | 85 +++++++++++++++++++++++++++++++++ hw/misc/meson.build | 1 + include/hw/misc/aspeed_otpmem.h | 33 +++++++++++++ 3 files changed, 119 insertions(+)create mode 100644
hw/misc/aspeed_otpmem.ccreate mode 100644include/hw/misc/aspeed_otpmem.hhw/misc/aspeed_otpmem.c new file +36
-0@@ -0,0 +1,85 @@ +/* +* ASPEED OTP (One-Time Programmable) memory +* +* Copyright (C) 2025 Aspeed +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qapi/error.h" +#include "trace.h" +#include "system/block-backend-global-state.h" +#include "system/block-backend-io.h" +#include "hw/misc/aspeed_otpmem.h" + +static uint64_t aspeed_otpmem_read (void *opaque,hwaddr offset,unsigned size) +{ +AspeedOTPMemState *s = opaque; +uint64_t val =0 ; + +memcpy (&val, s->storage + offset, size); + +return val; +} + +static void aspeed_otpmem_write (void *opaque,hwaddr offset, +uint64_t val,unsigned size) +{ +AspeedOTPMemState *s = opaque; + +memcpy (s->storage + offset, &val, size); +} + +static const MemoryRegionOps aspeed_otpmem_ops = { + .read = aspeed_otpmem_read,
hw/misc/aspeed_otpmem.c line 46
Quoted 10 lines
hw/misc/aspeed_otpmem.c new file +10
-0+ .write = aspeed_otpmem_write, + .endianness = DEVICE_LITTLE_ENDIAN, + .valid.min_access_size = 1, + .valid.max_access_size = 4, +}; + +static void aspeed_otpmem_realize (DeviceState *dev,Error **errp) +{ +AspeedOTPMemState *s =ASPEED_OTPMEM (dev); +const size_t size =OTPMEM_SIZE ;
Why not use s->size instead ? and a device property ?
hw/misc/aspeed_otpmem.c line 51
Quoted 5 lines
hw/misc/aspeed_otpmem.c new file +5
-0+int i, num; +uint32_t *p; + + s->storage =g_malloc (size); +if (!s->storage) {
if g_malloc() fails, the application will terminate. There is no need to test the returned pointer.
hw/misc/aspeed_otpmem.c line 60
Quoted 9 lines
hw/misc/aspeed_otpmem.c new file +9
-0+error_setg (errp,"Failed to allocate OTP memory storage buffer" ); +return ; + } + + num = size /sizeof (uint32_t ); + p = (uint32_t *)s->storage; +for (i =0 ; i < num; i++) { + p[i] = (i %2 ==0 ) ?0x00000000 :0xFFFFFFFF ; + }
The above initialization could be done in a aspeed_otpmem_init_storage()routine.
I understand that you want the values set at runtime to be kept after a machine/device reset.
include/hw/misc/aspeed_otpmem.h
Quoted 41 lines
hw/misc/aspeed_otpmem.c new file +24
-0+memory_region_init_io (&s->mmio,OBJECT (dev), &aspeed_otpmem_ops, + s,"aspeed.otpmem" , size); +address_space_init (&s->as, &s->mmio,NULL ); +} + +static void aspeed_otpmem_class_init (ObjectClass *klass,const void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + dc->realize = aspeed_otpmem_realize; +} + +static const TypeInfo aspeed_otpmem_info = { + .name =TYPE_ASPEED_OTPMEM , + .parent =TYPE_DEVICE , + .instance_size =sizeof (AspeedOTPMemState), + .class_init = aspeed_otpmem_class_init, +}; + +static void aspeed_otpmem_register_types (void ) +{ +type_register_static (&aspeed_otpmem_info); +} + +type_init (aspeed_otpmem_register_types)hw/misc/meson.build +1
-0@@ -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'))include/hw/misc/aspeed_otpmem.h new file +0
-0
Please add to your .git/config file :
[diff]
orderFile = /path/to/qemu/scripts/git.orderfile
Thanks,
C.
Quoted 34 lines
include/hw/misc/aspeed_otpmem.h new file +33
-0@@ -0,0 +1,33 @@ +/* +* ASPEED OTP (One-Time Programmable) memory +* +* Copyright (C) 2025 Aspeed +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef ASPEED_OTPMMEM_H +#define ASPEED_OTPMMEM_H + +#include "system/memory.h" +#include "hw/block/block.h" +#include "system/memory.h" +#include "system/address-spaces.h" + +#define OTPMEM_SIZE 0x4000 +#define TYPE_ASPEED_OTPMEM "aspeed.otpmem" +OBJECT_DECLARE_SIMPLE_TYPE(AspeedOTPMemState, ASPEED_OTPMEM) + +typedef struct AspeedOTPMemState { +DeviceState parent_obj; + +uint64_t size; + +AddressSpace as; + +MemoryRegion mmio; + +uint8_t *storage; +}AspeedOTPMemState ; + +#endif /* ASPEED_OTPMMEM_H */
RE: [RFC v6 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device model
Hi Cédric,
Thanks for your review and helpful comments. I will update the code accordingly.
I also have a question regarding the OTP memory content initialization. Since the OTP memory only needs to be initialized once, I plan to move the initialization code into the reset function and use a flag to ensure it runs only once. This way, the memory contents will be retained across device or machine resets.
Please let me know if you have any concerns or suggestions about this approach.
Best Regards,
Kane
Quoted 201 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, June 24, 2025 2:28 PM
To: Kane Chen <[email protected]>; Peter Maydell <[email protected]>; Steven Lee <[email protected]>; Troy Lee <[email protected]>; Jamin Lin <[email protected]>; Andrew Jeffery <[email protected]>; Joel Stanley <[email protected]>; open list:ASPEED BMCs <[email protected]>; open list:All patches CC here <[email protected]>
Cc: Troy Lee <[email protected]>
Subject: Re: [RFC v6 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device modelQuoted 3 lines · Kane Chen
From: Kane-Chen-AS <[email protected]>
Introduce a QEMU device model for ASPEED's One-Time Programmable
(OTP)
Quoted 6 lines
memory.
This model simulates a word-addressable OTP region used for secure fuse storage. The OTP memory can operate with an internal memory buffer.
Quoted 7 lines
The OTP model provides a memory-like interface through a dedicated AddressSpace, allowing other device models (e.g., SBC) to issue transactions as if accessing a memory-mapped region.
Signed-off-by Kane-Chen-AS <[email protected]>--- hw/misc/aspeed_otpmem.c | 85+++++++++++++++++++++++++++++++++
Quoted 43 lines
hw/misc/meson.build | 1 + include/hw/misc/aspeed_otpmem.h | 33 +++++++++++++ 3 files changed, 119 insertions(+)create mode 100644
hw/misc/aspeed_otpmem.ccreate mode 100644include/hw/misc/aspeed_otpmem.hhw/misc/aspeed_otpmem.c new renamed from hw/misc/aspeed_otpmem.c +0
-0file mode 100644 index 0000000000..b13b87fae8
hw/misc/aspeed_otpmem.c new file +32
-0@@ -0,0 +1,85 @@ +/* +* ASPEED OTP (One-Time Programmable) memory +* +* Copyright (C) 2025 Aspeed +* +* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qapi/error.h" +#include "trace.h" +#include "system/block-backend-global-state.h" +#include "system/block-backend-io.h" +#include "hw/misc/aspeed_otpmem.h" + +static uint64_t aspeed_otpmem_read (void *opaque,hwaddr offset, +unsigned size) { +AspeedOTPMemState *s = opaque; +uint64_t val =0 ; + +memcpy (&val, s->storage + offset, size); + +return val; +} + +static void aspeed_otpmem_write (void *opaque,hwaddr offset, +uint64_t val,unsigned size) { +AspeedOTPMemState *s = opaque; + +memcpy (s->storage + offset, &val, size); } + +static const MemoryRegionOps aspeed_otpmem_ops = {hw/misc/aspeed_otpmem.c line 42
Quoted 10 lines
hw/misc/aspeed_otpmem.c new file +10
-0+ .read = aspeed_otpmem_read, + .write = aspeed_otpmem_write, + .endianness = DEVICE_LITTLE_ENDIAN, + .valid.min_access_size = 1, + .valid.max_access_size = 4, +}; + +static void aspeed_otpmem_realize (DeviceState *dev,Error **errp) { +AspeedOTPMemState *s =ASPEED_OTPMEM (dev); +const size_t size =OTPMEM_SIZE ;Why not use
s->sizeinstead ? and a device property ?hw/misc/aspeed_otpmem.c line 51
Quoted 5 lines
hw/misc/aspeed_otpmem.c new file +5
-0+int i, num; +uint32_t *p; + + s->storage =g_malloc (size); +if (!s->storage) {if
g_malloc()fails, the application will terminate. There is no need to test the returned pointer.hw/misc/aspeed_otpmem.c line 60
Quoted 9 lines
hw/misc/aspeed_otpmem.c new file +9
-0+error_setg (errp,"Failed to allocate OTP memory storage buffer" ); +return ; + } + + num = size /sizeof (uint32_t ); + p = (uint32_t *)s->storage; +for (i =0 ; i < num; i++) { + p[i] = (i %2 ==0 ) ?0x00000000 :0xFFFFFFFF ; + }The above initialization could be done in a
aspeed_otpmem_init_storage()routine.I understand that you want the values set at runtime to be kept after a machine/device reset.
hw/misc/aspeed_otpmem.c line 57
Quoted 1 line
hw/misc/aspeed_otpmem.c new file +1
-0+ memory_region_init_io(&s->mmio, OBJECT(dev),
&aspeed_otpmem_ops,hw/misc/meson.build system_ss.add(when: 'CONFIG_ASPEED_SOC',
Quoted 26 lines
hw/misc/aspeed_otpmem.c new file +21
-0+ s, "aspeed.otpmem", size); +address_space_init (&s->as, &s->mmio,NULL ); } + +static void aspeed_otpmem_class_init (ObjectClass *klass,const void +*data) { +DeviceClass *dc =DEVICE_CLASS (klass); + dc->realize = aspeed_otpmem_realize; } + +static const TypeInfo aspeed_otpmem_info = { + .name =TYPE_ASPEED_OTPMEM , + .parent =TYPE_DEVICE , + .instance_size =sizeof (AspeedOTPMemState), + .class_init = aspeed_otpmem_class_init, +}; + +static void aspeed_otpmem_register_types (void ) +{ +type_register_static (&aspeed_otpmem_info); +} + +type_init (aspeed_otpmem_register_types)hw/misc/meson.build index renamed from hw/misc/meson.build +0
-0
6d47de482c..ed1eaaa2ad100644hw/misc/meson.build +0
-0@@ -136,6 +136,7 @@ system_ss.add(when: 'CONFIG_ASPEED_SOC',
if_true: files(include/hw/misc/aspeed_otpmem.h
Quoted 12 lines
hw/misc/meson.build +1
-0'aspeed_sbc.c', 'aspeed_sdmc.c', 'aspeed_xdma.c', + 'aspeed_otpmem.c', 'aspeed_peci.c', 'aspeed_sli.c'))(quoted diff) +0
-0
b/include/hw/misc/aspeed_otpmem.hnew file mode 100644 index 0000000000..a598e707a9include/hw/misc/aspeed_otpmem.h new file +0
-0Please add to your .git/config file :
[diff]
orderFile = /path/to/qemu/scripts/git.orderfileThanks,
C.
Quoted 9 lines
include/hw/misc/aspeed_otpmem.h new file +8
-0@@ -0,0 +1,33 @@ +/* +* ASPEED OTP (One-Time Programmable) memory +* +* Copyright (C) 2025 Aspeed +* +* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef ASPEED_OTPMMEM_H include/hw/misc/aspeed_otpmem.h line 18
Quoted 10 lines
include/hw/misc/aspeed_otpmem.h new file +10
-0+#define ASPEED_OTPMMEM_H + +#include "system/memory.h" +#include "hw/block/block.h" +#include "system/memory.h" +#include "system/address-spaces.h" + +#define OTPMEM_SIZE 0x4000 +#define TYPE_ASPEED_OTPMEM "aspeed.otpmem" +OBJECT_DECLARE_SIMPLE_TYPE(AspeedOTPMemState,
ASPEED_OTPMEM)Quoted 14 lines
include/hw/misc/aspeed_otpmem.h new file +14
-0+ +typedef struct AspeedOTPMemState { +DeviceState parent_obj; + +uint64_t size; + +AddressSpace as; + +MemoryRegion mmio; + +uint8_t *storage; +}AspeedOTPMemState ; + +#endif /* ASPEED_OTPMMEM_H */
Re: [RFC v6 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device model
Hello Kane,
Quoted 5 lines · Kane Chen
Hi Cédric,
Thanks for your review and helpful comments. I will update the code accordingly.
I also have a question regarding the OTP memory content initialization. Since the OTP memory only needs to be initialized once, I plan to move the initialization code into the reset function
It's not a reset function since the contents persist across resets.
Quoted 1 line
and use a flag to ensure it runs only once. This way, the memory contents will be retained across device or machine resets.
I would keep the contents initialization in the realize routine, same as m25p80_realize() does :
s->storage = blk_blockalign(NULL, s->size);
memset(s->storage, 0xFF, s->size);Thanks,
C.
Quoted 207 lines
Please let me know if you have any concerns or suggestions about this approach.
Best Regards,
KaneQuoted 201 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, June 24, 2025 2:28 PM
To: Kane Chen <[email protected]>; Peter Maydell <[email protected]>; Steven Lee <[email protected]>; Troy Lee <[email protected]>; Jamin Lin <[email protected]>; Andrew Jeffery <[email protected]>; Joel Stanley <[email protected]>; open list:ASPEED BMCs <[email protected]>; open list:All patches CC here <[email protected]>
Cc: Troy Lee <[email protected]>
Subject: Re: [RFC v6 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device modelQuoted 3 lines · Kane Chen
From: Kane-Chen-AS <[email protected]>
Introduce a QEMU device model for ASPEED's One-Time Programmable
(OTP)
Quoted 6 lines
memory.
This model simulates a word-addressable OTP region used for secure fuse storage. The OTP memory can operate with an internal memory buffer.
Quoted 7 lines
The OTP model provides a memory-like interface through a dedicated AddressSpace, allowing other device models (e.g., SBC) to issue transactions as if accessing a memory-mapped region.
Signed-off-by Kane-Chen-AS <[email protected]>--- hw/misc/aspeed_otpmem.c | 85+++++++++++++++++++++++++++++++++
Quoted 43 lines
hw/misc/meson.build | 1 + include/hw/misc/aspeed_otpmem.h | 33 +++++++++++++ 3 files changed, 119 insertions(+)create mode 100644
hw/misc/aspeed_otpmem.ccreate mode 100644include/hw/misc/aspeed_otpmem.hhw/misc/aspeed_otpmem.c new renamed from hw/misc/aspeed_otpmem.c +0
-0file mode 100644 index 0000000000..b13b87fae8
hw/misc/aspeed_otpmem.c new file +32
-0@@ -0,0 +1,85 @@ +/* +* ASPEED OTP (One-Time Programmable) memory +* +* Copyright (C) 2025 Aspeed +* +* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qapi/error.h" +#include "trace.h" +#include "system/block-backend-global-state.h" +#include "system/block-backend-io.h" +#include "hw/misc/aspeed_otpmem.h" + +static uint64_t aspeed_otpmem_read (void *opaque,hwaddr offset, +unsigned size) { +AspeedOTPMemState *s = opaque; +uint64_t val =0 ; + +memcpy (&val, s->storage + offset, size); + +return val; +} + +static void aspeed_otpmem_write (void *opaque,hwaddr offset, +uint64_t val,unsigned size) { +AspeedOTPMemState *s = opaque; + +memcpy (s->storage + offset, &val, size); } + +static const MemoryRegionOps aspeed_otpmem_ops = {hw/misc/aspeed_otpmem.c line 42
Quoted 10 lines
hw/misc/aspeed_otpmem.c new file +10
-0+ .read = aspeed_otpmem_read, + .write = aspeed_otpmem_write, + .endianness = DEVICE_LITTLE_ENDIAN, + .valid.min_access_size = 1, + .valid.max_access_size = 4, +}; + +static void aspeed_otpmem_realize (DeviceState *dev,Error **errp) { +AspeedOTPMemState *s =ASPEED_OTPMEM (dev); +const size_t size =OTPMEM_SIZE ;Why not use
s->sizeinstead ? and a device property ?hw/misc/aspeed_otpmem.c line 51
Quoted 5 lines
hw/misc/aspeed_otpmem.c new file +5
-0+int i, num; +uint32_t *p; + + s->storage =g_malloc (size); +if (!s->storage) {if
g_malloc()fails, the application will terminate. There is no need to test the returned pointer.hw/misc/aspeed_otpmem.c line 60
Quoted 9 lines
hw/misc/aspeed_otpmem.c new file +9
-0+error_setg (errp,"Failed to allocate OTP memory storage buffer" ); +return ; + } + + num = size /sizeof (uint32_t ); + p = (uint32_t *)s->storage; +for (i =0 ; i < num; i++) { + p[i] = (i %2 ==0 ) ?0x00000000 :0xFFFFFFFF ; + }The above initialization could be done in a
aspeed_otpmem_init_storage()routine.I understand that you want the values set at runtime to be kept after a machine/device reset.
hw/misc/aspeed_otpmem.c line 57
Quoted 1 line
hw/misc/aspeed_otpmem.c new file +1
-0+ memory_region_init_io(&s->mmio, OBJECT(dev),
&aspeed_otpmem_ops,hw/misc/meson.build system_ss.add(when: 'CONFIG_ASPEED_SOC',
Quoted 26 lines
hw/misc/aspeed_otpmem.c new file +21
-0+ s, "aspeed.otpmem", size); +address_space_init (&s->as, &s->mmio,NULL ); } + +static void aspeed_otpmem_class_init (ObjectClass *klass,const void +*data) { +DeviceClass *dc =DEVICE_CLASS (klass); + dc->realize = aspeed_otpmem_realize; } + +static const TypeInfo aspeed_otpmem_info = { + .name =TYPE_ASPEED_OTPMEM , + .parent =TYPE_DEVICE , + .instance_size =sizeof (AspeedOTPMemState), + .class_init = aspeed_otpmem_class_init, +}; + +static void aspeed_otpmem_register_types (void ) +{ +type_register_static (&aspeed_otpmem_info); +} + +type_init (aspeed_otpmem_register_types)hw/misc/meson.build index renamed from hw/misc/meson.build +0
-0
6d47de482c..ed1eaaa2ad100644hw/misc/meson.build +0
-0@@ -136,6 +136,7 @@ system_ss.add(when: 'CONFIG_ASPEED_SOC',
if_true: files(include/hw/misc/aspeed_otpmem.h
Quoted 12 lines
hw/misc/meson.build +1
-0'aspeed_sbc.c', 'aspeed_sdmc.c', 'aspeed_xdma.c', + 'aspeed_otpmem.c', 'aspeed_peci.c', 'aspeed_sli.c'))(quoted diff) +0
-0
b/include/hw/misc/aspeed_otpmem.hnew file mode 100644 index 0000000000..a598e707a9include/hw/misc/aspeed_otpmem.h new file +0
-0Please add to your .git/config file :
[diff]
orderFile = /path/to/qemu/scripts/git.orderfileThanks,
C.
Quoted 9 lines
include/hw/misc/aspeed_otpmem.h new file +8
-0@@ -0,0 +1,33 @@ +/* +* ASPEED OTP (One-Time Programmable) memory +* +* Copyright (C) 2025 Aspeed +* +* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef ASPEED_OTPMMEM_H include/hw/misc/aspeed_otpmem.h line 18
Quoted 10 lines
include/hw/misc/aspeed_otpmem.h new file +10
-0+#define ASPEED_OTPMMEM_H + +#include "system/memory.h" +#include "hw/block/block.h" +#include "system/memory.h" +#include "system/address-spaces.h" + +#define OTPMEM_SIZE 0x4000 +#define TYPE_ASPEED_OTPMEM "aspeed.otpmem" +OBJECT_DECLARE_SIMPLE_TYPE(AspeedOTPMemState,
ASPEED_OTPMEM)Quoted 14 lines
include/hw/misc/aspeed_otpmem.h new file +14
-0+ +typedef struct AspeedOTPMemState { +DeviceState parent_obj; + +uint64_t size; + +AddressSpace as; + +MemoryRegion mmio; + +uint8_t *storage; +}AspeedOTPMemState ; + +#endif /* ASPEED_OTPMMEM_H */
RE: [RFC v6 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device model
Hi Cédric,
Thanks for your comments.
I have one more question regarding the use of the "RFC" tag. Initially, I marked the series as "RFC" to gather feedback on the OTP memory and SBC initialization design. Now that we're moving forward with more concrete code review, I'm wondering whether I should still use the "RFC" tag for the next version of the patch.
Would you recommend keeping the "RFC" tag, or is it appropriate to drop it at this point?
Best Regards,
Kane
Quoted 252 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Wednesday, June 25, 2025 2:07 PM
To: Kane Chen <[email protected]>; Peter Maydell <[email protected]>; Steven Lee <[email protected]>; Troy Lee <[email protected]>; Jamin Lin <[email protected]>; Andrew Jeffery <[email protected]>; Joel Stanley <[email protected]>; open list:ASPEED BMCs <[email protected]>; open list:All patches CC here <[email protected]>
Cc: Troy Lee <[email protected]>
Subject: Re: [RFC v6 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device modelHello Kane,
Quoted 3 lines · Kane Chen
Hi Cédric,
Thanks for your review and helpful comments. I will update the code
accordingly.
Quoted 4 lines
I also have a question regarding the OTP memory content initialization. Since the OTP memory only needs to be initialized once, I plan to move the initialization code into the reset function
It's not a reset function since the contents persist across resets.
Quoted 1 line
and use a flag to ensure it runs only once. This way, the memory contents
will be retained across device or machine resets.
I would keep the contents initialization in the realize routine, same as
m25p80_realize()does :s->storage = blk_blockalign(NULL, s->size); memset(s->storage, 0xFF, s->size);Thanks,
C.
Quoted 2 lines
Please let me know if you have any concerns or suggestions about this
approach.
Quoted 34 lines
Best Regards,
KaneQuoted 31 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, June 24, 2025 2:28 PM
To: Kane Chen <[email protected]>; Peter Maydell <[email protected]>; Steven Lee <[email protected]>; Troy Lee <[email protected]>; Jamin Lin <[email protected]>; Andrew Jeffery <[email protected]>; Joel Stanley <[email protected]>; open list:ASPEED BMCs <[email protected]>; open list:All patches CC here <[email protected]>
Cc: Troy Lee <[email protected]>
Subject: Re: [RFC v6 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device modelQuoted 3 lines · Kane Chen
From: Kane-Chen-AS <[email protected]>
Introduce a QEMU device model for ASPEED's One-Time Programmable
(OTP)
Quoted 13 lines
memory.
This model simulates a word-addressable OTP region used for secure fuse storage. The OTP memory can operate with an internal memory buffer.
The OTP model provides a memory-like interface through a dedicated AddressSpace, allowing other device models (e.g., SBC) to issue transactions as if accessing a memory-mapped region.
Signed-off-by Kane-Chen-AS <[email protected]>--- hw/misc/aspeed_otpmem.c | 85Quoted 8 lines
Quoted 8 lines
+++++++++++++++++++++++++++++++++
hw/misc/aspeed_otpmem.c
Quoted 7 lines
hw/misc/meson.build | 1 + include/hw/misc/aspeed_otpmem.h | 33 +++++++++++++ 3 files changed, 119 insertions(+)create mode 100644
hw/misc/aspeed_otpmem.ccreate mode 100644include/hw/misc/aspeed_otpmem.hhw/misc/aspeed_otpmem.c +0
-0new
Quoted 55 lines
Quoted 55 lines
Quoted 36 lines
file mode 100644 index 0000000000..b13b87fae8
hw/misc/aspeed_otpmem.c new file +32
-0@@ -0,0 +1,85 @@ +/* +* ASPEED OTP (One-Time Programmable) memory +* +* Copyright (C) 2025 Aspeed +* +* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qapi/error.h" +#include "trace.h" +#include "system/block-backend-global-state.h" +#include "system/block-backend-io.h" +#include "hw/misc/aspeed_otpmem.h" + +static uint64_t aspeed_otpmem_read (void *opaque,hwaddr offset, +unsigned size) { +AspeedOTPMemState *s = opaque; +uint64_t val =0 ; + +memcpy (&val, s->storage + offset, size); + +return val; +} + +static void aspeed_otpmem_write (void *opaque,hwaddr offset, +uint64_t val,unsigned size) { +AspeedOTPMemState *s = opaque; + +memcpy (s->storage + offset, &val, size); } + +static const MemoryRegionOps aspeed_otpmem_ops = {hw/misc/aspeed_otpmem.c line 42
Quoted 10 lines
hw/misc/aspeed_otpmem.c new file +10
-0+ .read = aspeed_otpmem_read, + .write = aspeed_otpmem_write, + .endianness = DEVICE_LITTLE_ENDIAN, + .valid.min_access_size = 1, + .valid.max_access_size = 4, +}; + +static void aspeed_otpmem_realize (DeviceState *dev,Error **errp) { +AspeedOTPMemState *s =ASPEED_OTPMEM (dev); +const size_t size =OTPMEM_SIZE ;Why not use
s->sizeinstead ? and a device property ?Quoted 5 lines
hw/misc/aspeed_otpmem.c new file +5
-0+int i, num; +uint32_t *p; + + s->storage =g_malloc (size); +if (!s->storage) {Quoted 4 lines
Quoted 4 lines
if
g_malloc()fails, the application will terminate. There is no need to test the returned pointer.Quoted 1 line
hw/misc/aspeed_otpmem.c new file +1
-0+ error_setg(errp, "Failed to allocate OTP memory storagebuffer");
Quoted 104 lines
Quoted 103 lines
hw/misc/aspeed_otpmem.c line 60
Quoted 8 lines
hw/misc/aspeed_otpmem.c new file +8
-0+return ; + } + + num = size /sizeof (uint32_t ); + p = (uint32_t *)s->storage; +for (i =0 ; i < num; i++) { + p[i] = (i %2 ==0 ) ?0x00000000 :0xFFFFFFFF ; + }The above initialization could be done in a
aspeed_otpmem_init_storage()routine.I understand that you want the values set at runtime to be kept after a machine/device reset.
hw/misc/aspeed_otpmem.c line 57
Quoted 1 line
hw/misc/aspeed_otpmem.c new file +1
-0+ memory_region_init_io(&s->mmio, OBJECT(dev),
&aspeed_otpmem_ops,hw/misc/meson.build system_ss.add(when: 'CONFIG_ASPEED_SOC',
Quoted 26 lines
hw/misc/aspeed_otpmem.c new file +21
-0+ s, "aspeed.otpmem", size); +address_space_init (&s->as, &s->mmio,NULL ); } + +static void aspeed_otpmem_class_init (ObjectClass *klass,const void +*data) { +DeviceClass *dc =DEVICE_CLASS (klass); + dc->realize = aspeed_otpmem_realize; } + +static const TypeInfo aspeed_otpmem_info = { + .name =TYPE_ASPEED_OTPMEM , + .parent =TYPE_DEVICE , + .instance_size =sizeof (AspeedOTPMemState), + .class_init = aspeed_otpmem_class_init, +}; + +static void aspeed_otpmem_register_types (void ) +{ +type_register_static (&aspeed_otpmem_info); +} + +type_init (aspeed_otpmem_register_types)hw/misc/meson.build index renamed from hw/misc/meson.build +0
-0
6d47de482c..ed1eaaa2ad100644hw/misc/meson.build +0
-0@@ -136,6 +136,7 @@ system_ss.add(when: 'CONFIG_ASPEED_SOC',
if_true: files(include/hw/misc/aspeed_otpmem.h
Quoted 12 lines
hw/misc/meson.build +1
-0'aspeed_sbc.c', 'aspeed_sdmc.c', 'aspeed_xdma.c', + 'aspeed_otpmem.c', 'aspeed_peci.c', 'aspeed_sli.c'))(quoted diff) +0
-0
b/include/hw/misc/aspeed_otpmem.hnew file mode 100644 index 0000000000..a598e707a9include/hw/misc/aspeed_otpmem.h new file +0
-0Please add to your .git/config file :
[diff]
orderFile = /path/to/qemu/scripts/git.orderfileThanks,
C.
Quoted 9 lines
include/hw/misc/aspeed_otpmem.h new file +8
-0@@ -0,0 +1,33 @@ +/* +* ASPEED OTP (One-Time Programmable) memory +* +* Copyright (C) 2025 Aspeed +* +* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef ASPEED_OTPMMEM_H include/hw/misc/aspeed_otpmem.h line 18
Quoted 10 lines
include/hw/misc/aspeed_otpmem.h new file +10
-0+#define ASPEED_OTPMMEM_H + +#include "system/memory.h" +#include "hw/block/block.h" +#include "system/memory.h" +#include "system/address-spaces.h" + +#define OTPMEM_SIZE 0x4000 +#define TYPE_ASPEED_OTPMEM "aspeed.otpmem" +OBJECT_DECLARE_SIMPLE_TYPE(AspeedOTPMemState,
ASPEED_OTPMEM)Quoted 14 lines
include/hw/misc/aspeed_otpmem.h new file +14
-0+ +typedef struct AspeedOTPMemState { +DeviceState parent_obj; + +uint64_t size; + +AddressSpace as; + +MemoryRegion mmio; + +uint8_t *storage; +}AspeedOTPMemState ; + +#endif /* ASPEED_OTPMMEM_H */
Re: [RFC v6 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device model
Quoted 7 lines · Kane Chen
Hi Cédric,
Thanks for your comments.
I have one more question regarding the use of the "RFC" tag. Initially, I marked the series as "RFC" to gather feedback on the OTP memory and SBC initialization design. Now that we're moving forward with more concrete code review, I'm wondering whether I should still use the "RFC" tag for the next version of the patch.
Would you recommend keeping the "RFC" tag, or is it appropriate to drop it at this point?
It's fine to drop the RFC in the next version. Make it a v1 too.
Thanks,
C.
Re: [RFC v6 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device model
In reply to Kane Chen viaQuoted 14 lines · Kane Chen
From: Kane-Chen-AS <[email protected]>
Introduce a QEMU device model for ASPEED's One-Time Programmable (OTP) memory.
This model simulates a word-addressable OTP region used for secure fuse storage. The OTP memory can operate with an internal memory buffer.
The OTP model provides a memory-like interface through a dedicated AddressSpace, allowing other device models (e.g., SBC) to issue transactions as if accessing a memory-mapped region.
Quoted 5 lines
Signed-off-by Kane-Chen-AS <[email protected]>--- hw/misc/aspeed_otpmem.c | 85 +++++++++++++++++++++++++++++++++ hw/misc/meson.build | 1 + include/hw/misc/aspeed_otpmem.h | 33 +++++++++++++
Our other otp-type devices are in hw/nvram/.
You should probably look at whether there's any kind of already existing conventions for these types of devices that this one should follow. (There may be nothing appropriate, but it's worth a look, so we don't end up with half a dozen devices that are all fairly similar but all do things in arbitrarily different ways.)
thanks
-- PMM
RE: [RFC v6 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device model
Hi Peter,
Thanks for your suggestion.
Yes, I did review the existing devices under hw/nvram/, including bcm2835_otp.c, npcm7xx_otp.c, and others. However, I encountered two main challenges:
1. Lack of file backend support
2. Inability to support one-time programming behavior from 1 -> 0 as well as 0 -> 1
For point 1, it's manageable — some existing devices do support a file backend, or we could introduce a middleware layer to add this functionality if needed.
Point 2 is a limitation of the existing mode. Most existing implementations are designed for OTP models that only support 0 -> 1 transitions. Supporting 1 -> 0 one-time programming would require significant modification. Even with a middleware layer, the logic would become quite complex, and the changes might be more intrusive than introducing a new device model.
That’s why I decided to implement a new model specifically for the Aspeed OTP device. That said, I did refer to the designs in hw/nvram/ during development, and Cédric also pointed me to pnv_pnor.c and m25p80.c, which provided helpful reference points.
Please let me know if you have any concerns or further suggestions.
Best Regards,
Kane
Quoted 47 lines
-----Original Message-----
From: Peter Maydell <[email protected]>
Sent: Wednesday, June 25, 2025 6:26 PM
To: Kane Chen <[email protected]>
Cc: Cédric Le Goater <[email protected]>; Steven Lee <[email protected]>; Troy Lee <[email protected]>; Jamin Lin <[email protected]>; Andrew Jeffery <[email protected]>; Joel Stanley <[email protected]>; open list:ASPEED BMCs <[email protected]>; open list:All patches CC here <[email protected]>; Troy Lee <[email protected]>
Subject: Re: [RFC v6 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device modelQuoted 4 lines · Kane Chen
From: Kane-Chen-AS <[email protected]>
Introduce a QEMU device model for ASPEED's One-Time Programmable
(OTP)
Quoted 6 lines
memory.
This model simulates a word-addressable OTP region used for secure fuse storage. The OTP memory can operate with an internal memory buffer.
Quoted 7 lines
The OTP model provides a memory-like interface through a dedicated AddressSpace, allowing other device models (e.g., SBC) to issue transactions as if accessing a memory-mapped region.
Signed-off-by Kane-Chen-AS <[email protected]>--- hw/misc/aspeed_otpmem.c | 85+++++++++++++++++++++++++++++++++
Quoted 2 lines
hw/misc/meson.build | 1 + include/hw/misc/aspeed_otpmem.h | 33 +++++++++++++Our other otp-type devices are in hw/nvram/.
You should probably look at whether there's any kind of already existing conventions for these types of devices that this one should follow. (There may be nothing appropriate, but it's worth a look, so we don't end up with half a dozen devices that are all fairly similar but all do things in arbitrarily different ways.)
thanks
-- PMM
RFC v6 3/3 hw/arm: Integrate ASPEED OTP memory support into AST2600 SoCs
In reply to Kane Chen viaFrom: Kane-Chen-AS <[email protected]>
The has_otpmem attribute is enabled in the SBC subclasses for AST2600 to control the presence of OTP support per SoC type.
--- hw/arm/aspeed_ast2600.c | 2 +- hw/misc/aspeed_sbc.c | 2 ++ include/hw/misc/aspeed_sbc.h | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-)
hw/arm/aspeed_ast2600.c +1 -1
@@ -261,7 +261,7 @@ static void aspeed_soc_ast2600_init(Object *obj)object_initialize_child (obj,"i3c" , &s->i3c,TYPE_ASPEED_I3C ); -object_initialize_child (obj,"sbc" , &s->sbc,TYPE_ASPEED_SBC ); +object_initialize_child (obj,"sbc" , &s->sbc,TYPE_ASPEED_AST2600_SBC );object_initialize_child (obj,"iomem" , &s->iomem,TYPE_UNIMPLEMENTED_DEVICE );object_initialize_child (obj,"video" , &s->video,TYPE_UNIMPLEMENTED_DEVICE );
hw/misc/aspeed_sbc.c +2 -0
@@ -254,8 +254,10 @@ static const TypeInfo aspeed_sbc_info = {static void aspeed_ast2600_sbc_class_init (ObjectClass *klass,const void *data) {DeviceClass *dc =DEVICE_CLASS (klass); +AspeedSBCClass *sc =ASPEED_SBC_CLASS (klass); dc->desc ="AST2600 Secure Boot Controller" ; + sc->has_otpmem = true; }static const TypeInfo aspeed_ast2600_sbc_info = {
include/hw/misc/aspeed_sbc.h +2 -0
@@ -43,6 +43,8 @@ struct AspeedSBCState {struct AspeedSBCClass {SysBusDeviceClass parent_class; + +bool has_otpmem; };#endif /* ASPEED_SBC_H */
Signature · 1 line
2.43.0
Re: [RFC v6 3/3] hw/arm: Integrate ASPEED OTP memory support into AST2600 SoCs
Quoted 17 lines · Kane Chen
From: Kane-Chen-AS <[email protected]>
The
has_otpmemattribute is enabled in the SBC subclasses for AST2600 to control the presence of OTP support per SoC type.Signed-off-by Kane-Chen-AS <[email protected]>--- hw/arm/aspeed_ast2600.c | 2 +- hw/misc/aspeed_sbc.c | 2 ++ include/hw/misc/aspeed_sbc.h | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-)hw/arm/aspeed_ast2600.c +0
-0
hw/arm/aspeed_ast2600.c static void aspeed_soc_ast2600_init(Object *obj) · line 264
Quoted 5 lines
hw/arm/aspeed_ast2600.c +1
-1@@ -261,7 +261,7 @@ static void aspeed_soc_ast2600_init(Object *obj)object_initialize_child (obj,"i3c" , &s->i3c,TYPE_ASPEED_I3C ); -object_initialize_child (obj,"sbc" , &s->sbc,TYPE_ASPEED_SBC ); +object_initialize_child (obj,"sbc" , &s->sbc,TYPE_ASPEED_AST2600_SBC );
I see that TYPE_ASPEED_AST2600_SBC was defined but never used :/
Quoted 23 lines
hw/arm/aspeed_ast2600.c +0
-0object_initialize_child (obj,"iomem" , &s->iomem,TYPE_UNIMPLEMENTED_DEVICE );object_initialize_child (obj,"video" , &s->video,TYPE_UNIMPLEMENTED_DEVICE );hw/misc/aspeed_sbc.c +2
-0@@ -254,8 +254,10 @@ static const TypeInfo aspeed_sbc_info = {static void aspeed_ast2600_sbc_class_init (ObjectClass *klass,const void *data) {DeviceClass *dc =DEVICE_CLASS (klass); +AspeedSBCClass *sc =ASPEED_SBC_CLASS (klass); dc->desc ="AST2600 Secure Boot Controller" ; + sc->has_otpmem = true; }static const TypeInfo aspeed_ast2600_sbc_info = {include/hw/misc/aspeed_sbc.h +0
-0
include/hw/misc/aspeed_sbc.h struct AspeedSBCState { · line 47
Quoted 5 lines
include/hw/misc/aspeed_sbc.h +2
-0@@ -43,6 +43,8 @@ struct AspeedSBCState {struct AspeedSBCClass {SysBusDeviceClass parent_class; + +bool has_otpmem;
This change should be in the previous patch.
Thanks,
C.
Quoted 3 lines
include/hw/misc/aspeed_sbc.h +0
-0};#endif /* ASPEED_SBC_H */