hw/misc/aspeed_otp: Add ASPEED OTP memory model and SoC integration
Subsystems (2)
v4 cover hw/misc/aspeed_otp: Add ASPEED OTP memory model and SoC integration
From: Kane-Chen-AS <[email protected]>
Dear reviewers,
This series introduces the ASPEED OTP (One-Time Programmable) memory model and connects it to the SBC controller and AST10x0/AST2600 SoCs.
The OTP model supports irreversible bit programming, file-backed content, and tracepoints for program conflict debugging.
Patch 2 links the OTP device to the SBC controller and enables command handling. Patch 3 exposes the machine parameter and updates the SoC tree.
Any feedback or suggestions are appreciated!
Thanks,
Kane
---
ChangeLog
---------
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 controller hw/arm: Integrate ASPEED OTP memory into AST10x0 and AST2600 SoCs
hw/arm/aspeed.c | 20 ++++ hw/arm/aspeed_ast10x0.c | 2 +- hw/arm/aspeed_ast2600.c | 2 +- hw/misc/aspeed_otpmem.c | 198 ++++++++++++++++++++++++++++++++ hw/misc/aspeed_sbc.c | 197 +++++++++++++++++++++++++++++++ hw/misc/meson.build | 1 + hw/misc/trace-events | 10 ++ include/hw/misc/aspeed_otpmem.h | 39 +++++++ include/hw/misc/aspeed_sbc.h | 6 + 9 files changed, 473 insertions(+), 2 deletions(-) create mode 100644 hw/misc/aspeed_otpmem.c create mode 100644 include/hw/misc/aspeed_otpmem.h
Signature · 1 line
2.43.0
PATCH v4 2/3 hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC controller
From: Kane-Chen-AS <[email protected]>
Integrate the aspeed.otpmem backend with the ASPEED Secure Boot Controller (SBC).
This patch adds command handling support in the SBC to read and program the connected OTP memory using READ, WRITE, and PROG commands. It enables basic interaction with OTP content for secure boot or fuse modeling logic.
Tracepoints are used to monitor command activity and unsupported paths.
The following QOM hierarchy illustrates how OTP is connected:
/machine (ast1030-evb-machine)
/soc (ast1030-a1)
/sbc (aspeed.sbc-ast10X0)
/aspeed.sbc[0] (memory-region)
/otpmem (aspeed.otpmem)
/aspeed.otpmem.backend[0] (memory-region)--- hw/misc/aspeed_sbc.c | 179 +++++++++++++++++++++++++++++++++++ hw/misc/trace-events | 5 + include/hw/misc/aspeed_sbc.h | 5 + 3 files changed, 189 insertions(+)
hw/misc/aspeed_sbc.c +179 -0
@@ -15,9 +15,14 @@#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_CAMP2 (0x024 / 4)#define R_QSR (0x040 / 4)/* R_STATUS */
@@ -41,6 +46,19 @@#define QSR_RSA_MASK (0x3 << 12)#define QSR_HASH_MASK (0x3 << 10) +typedef enum { +SBC_OTP_CMD_READ =0x23b1e361 , +SBC_OTP_CMD_WRITE =0x23b1e362 , +SBC_OTP_CMD_PROG =0x23b1e364 , +}SBC_OTP_Command ; + +#define OTP_DATA_DWORD_COUNT (0x800) +#define OTP_TOTAL_DWORD_COUNT (0x1000) + +#define MODE_REGISTER (0x1000) +#define MODE_REGISTER_A (0x3000) +#define MODE_REGISTER_B (0x5000) +static uint64_t aspeed_sbc_read (void *opaque,hwaddr addr,unsigned int size) {AspeedSBCState *s =ASPEED_SBC (opaque);
@@ -57,6 +75,143 @@ 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) +{ +uint32_t data =0 , otp_offset; +bool is_data = false; +AspeedSBCClass *sc =ASPEED_SBC_GET_CLASS (s); +const AspeedOTPMemOps *otp_ops; + +if (sc->has_otpmem == false) { +trace_aspeed_sbc_otpmem_state ("disabled" ); +return true; + } + + otp_ops =aspeed_otpmem_get_ops (&s->otpmem); + +if (otp_addr <OTP_DATA_DWORD_COUNT ) { + is_data = true; + }else if (otp_addr >=OTP_TOTAL_DWORD_COUNT ) { +error_setg (errp,"Invalid OTP addr 0x%x" , otp_addr); +return false; + } + otp_offset = otp_addr <<2 ; + + data = otp_ops->read (&s->otpmem, otp_offset, errp); +if (*errp) { +return false; + } + s->regs[R_CAMP1 ] = data; + +if (is_data) { + data = otp_ops->read (&s->otpmem, otp_offset +4 , errp); +if (*errp) { +return false; + } + s->regs[R_CAMP2 ] = data; + } + +return true; +} + +static bool mr_handler (uint32_t otp_addr,Error **errp) +{ +switch (otp_addr) { +case MODE_REGISTER : +case MODE_REGISTER_A : +case MODE_REGISTER_B : +/* HW behavior, do nothing here */ +return true; +default : +error_setg (errp,"Unsupported address 0x%x" , otp_addr); +return false; + } +} + +static bool aspeed_sbc_otpmem_write (AspeedSBCState *s, +uint32_t otp_addr,Error **errp) +{ +if (otp_addr ==0 ) { +trace_aspeed_sbc_ignore_cmd (otp_addr); +return true; + }else if (otp_addr >=MODE_REGISTER ) { +if (mr_handler (otp_addr, errp) == false) { +return false; + } + }else { +error_setg (errp,"Unhandled OTP write address 0x%x" , otp_addr); +return false; + } + +return true; +} + +static bool aspeed_sbc_otpmem_prog (AspeedSBCState *s, +uint32_t otp_addr,Error **errp) +{ +uint32_t value; +AspeedSBCClass *sc =ASPEED_SBC_GET_CLASS (s); +const AspeedOTPMemOps *otp_ops; + +if (sc->has_otpmem == false) { +trace_aspeed_sbc_otpmem_state ("disabled" ); +return true; + } + + otp_ops =aspeed_otpmem_get_ops (&s->otpmem); + value = s->regs[R_CAMP1 ]; +if (otp_addr >=OTP_TOTAL_DWORD_COUNT ) { +error_setg (errp,"Invalid OTP addr 0x%x" , otp_addr); +return false; + } + + otp_ops->prog (&s->otpmem, otp_addr, value, errp); + +if (*errp) { +return false; + } + +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_WRITE : + ret =aspeed_sbc_otpmem_write (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 +233,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 +277,32 @@ 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 (dev); +char *otpmem;memory_region_init_io (&s->iomem,OBJECT (s), &aspeed_sbc_ops, s,TYPE_ASPEED_SBC ,0x1000 );sysbus_init_mmio (sbd, &s->iomem); + + otpmem =object_property_get_str (qdev_get_machine (),"otpmem" , errp); +/* +* If user doesn't specify the otpmem file location, +* to disable the OTP memory feature due to no backend data +*/ +if ((otpmem ==NULL ) || !strlen (otpmem)) { + sc->has_otpmem = false; + } + +if (sc->has_otpmem) { +object_initialize_child (OBJECT (s),"optmem" , + &s->otpmem,TYPE_ASPEED_OTPMEM ); +aspeed_otpmem_set_backend (&s->otpmem, otpmem); +qdev_realize (DEVICE (&s->otpmem),NULL , errp); +trace_aspeed_sbc_otpmem_state ("enabled" ); + }else { +trace_aspeed_sbc_otpmem_state ("disabled" ); + } }static const VMStateDescription vmstate_aspeed_sbc = {
hw/misc/trace-events +5 -0
@@ -95,6 +95,11 @@ aspeed_otpmem_prog(uint32_t addr, uint32_t prog_value, uint32_t value) "OTP Memo aspeed_otpmem_prog_conflict(uint32_t addr, uint32_t bits) "Conflict at addr=0x%x, bits=0x%08x" aspeed_otpmem_prog_bit(int bit) "Programmed bit %d" +# 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_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 +5 -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,10 +37,14 @@ struct AspeedSBCState {MemoryRegion iomem;uint32_t regs[ASPEED_SBC_NR_REGS ]; + + AspeedOTPMemState otpmem; };struct AspeedSBCClass {SysBusDeviceClass parent_class; + +bool has_otpmem; };#endif /* ASPEED_SBC_H */
Signature · 1 line
2.43.0
Re: [PATCH v4 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC controller
Quoted 84 lines · Kane Chen
From: Kane-Chen-AS <[email protected]>
Integrate the aspeed.otpmem backend with the ASPEED Secure Boot Controller (SBC).
This patch adds command handling support in the SBC to read and program the connected OTP memory using READ, WRITE, and PROG commands. It enables basic interaction with OTP content for secure boot or fuse modeling logic.
Tracepoints are used to monitor command activity and unsupported paths.
The following QOM hierarchy illustrates how OTP is connected:
/machine (ast1030-evb-machine) /soc (ast1030-a1) /sbc (aspeed.sbc-ast10X0) /aspeed.sbc[0] (memory-region) /otpmem (aspeed.otpmem) /aspeed.otpmem.backend[0] (memory-region)Signed-off-by Kane-Chen-AS <[email protected]>--- hw/misc/aspeed_sbc.c | 179 +++++++++++++++++++++++++++++++++++ hw/misc/trace-events | 5 + include/hw/misc/aspeed_sbc.h | 5 + 3 files changed, 189 insertions(+)hw/misc/aspeed_sbc.c +32
-0@@ -15,9 +15,14 @@#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_CAMP2 (0x024 / 4)#define R_QSR (0x040 / 4)/* R_STATUS */ @@ -41,6 +46,19 @@#define QSR_RSA_MASK (0x3 << 12)#define QSR_HASH_MASK (0x3 << 10) +typedef enum { +SBC_OTP_CMD_READ =0x23b1e361 , +SBC_OTP_CMD_WRITE =0x23b1e362 , +SBC_OTP_CMD_PROG =0x23b1e364 , +}SBC_OTP_Command ; + +#define OTP_DATA_DWORD_COUNT (0x800) +#define OTP_TOTAL_DWORD_COUNT (0x1000) + +#define MODE_REGISTER (0x1000) +#define MODE_REGISTER_A (0x3000) +#define MODE_REGISTER_B (0x5000) +static uint64_t aspeed_sbc_read (void *opaque,hwaddr addr,unsigned int size) {AspeedSBCState *s =ASPEED_SBC (opaque);@@ -57,6 +75,143 @@ 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) +{ +uint32_t data =0 , otp_offset; +bool is_data = false; +AspeedSBCClass *sc =ASPEED_SBC_GET_CLASS (s); +const AspeedOTPMemOps *otp_ops; + +if (sc->has_otpmem == false) { +trace_aspeed_sbc_otpmem_state ("disabled" ); +return true; + } + + otp_ops =aspeed_otpmem_get_ops (&s->otpmem);
hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 101
Quoted 10 lines
hw/misc/aspeed_sbc.c +10
-0+ +if (otp_addr <OTP_DATA_DWORD_COUNT ) { + is_data = true; + }else if (otp_addr >=OTP_TOTAL_DWORD_COUNT ) { +error_setg (errp,"Invalid OTP addr 0x%x" , otp_addr); +return false; + } + otp_offset = otp_addr <<2 ; + + data = otp_ops->read (&s->otpmem, otp_offset, errp);
why not call directly :
ret = address_space_read(&s->otpmem.as, addr, MEMTXATTRS_UNSPECIFIED,
(uint8_t *)&val, sizeof(val));
if (ret != MEMTX_OK) {
error_setg(errp, "Failed to read data from 0x%x", addr);
return OTPMEM_ERR_MAGIC;
}The otp_ops business looks useless to me. Same for the write and prog handlers.
Quoted 128 lines
hw/misc/aspeed_sbc.c +115
-0+if (*errp) { +return false; + } + s->regs[R_CAMP1 ] = data; + +if (is_data) { + data = otp_ops->read (&s->otpmem, otp_offset +4 , errp); +if (*errp) { +return false; + } + s->regs[R_CAMP2 ] = data; + } + +return true; +} + +static bool mr_handler (uint32_t otp_addr,Error **errp) +{ +switch (otp_addr) { +case MODE_REGISTER : +case MODE_REGISTER_A : +case MODE_REGISTER_B : +/* HW behavior, do nothing here */ +return true; +default : +error_setg (errp,"Unsupported address 0x%x" , otp_addr); +return false; + } +} + +static bool aspeed_sbc_otpmem_write (AspeedSBCState *s, +uint32_t otp_addr,Error **errp) +{ +if (otp_addr ==0 ) { +trace_aspeed_sbc_ignore_cmd (otp_addr); +return true; + }else if (otp_addr >=MODE_REGISTER ) { +if (mr_handler (otp_addr, errp) == false) { +return false; + } + }else { +error_setg (errp,"Unhandled OTP write address 0x%x" , otp_addr); +return false; + } + +return true; +} + +static bool aspeed_sbc_otpmem_prog (AspeedSBCState *s, +uint32_t otp_addr,Error **errp) +{ +uint32_t value; +AspeedSBCClass *sc =ASPEED_SBC_GET_CLASS (s); +const AspeedOTPMemOps *otp_ops; + +if (sc->has_otpmem == false) { +trace_aspeed_sbc_otpmem_state ("disabled" ); +return true; + } + + otp_ops =aspeed_otpmem_get_ops (&s->otpmem); + value = s->regs[R_CAMP1 ]; +if (otp_addr >=OTP_TOTAL_DWORD_COUNT ) { +error_setg (errp,"Invalid OTP addr 0x%x" , otp_addr); +return false; + } + + otp_ops->prog(&s->otpmem, otp_addr, value, errp);> + + if (*errp) { +return false; + } + +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_WRITE : + ret =aspeed_sbc_otpmem_write (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 +233,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 +277,32 @@ 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 288
Quoted 10 lines
hw/misc/aspeed_sbc.c +4
-0SysBusDevice *sbd =SYS_BUS_DEVICE (dev); +AspeedSBCClass *sc =ASPEED_SBC_GET_CLASS (dev); +char *otpmem;memory_region_init_io (&s->iomem,OBJECT (s), &aspeed_sbc_ops, s,TYPE_ASPEED_SBC ,0x1000 );sysbus_init_mmio (sbd, &s->iomem); + + otpmem =object_property_get_str (qdev_get_machine (),"otpmem" , errp);
nope. You should use property aliases instead.
The 'optmem' machine (drive) property would be an alias pointing to an 'optmem' property of AspeedSBCState. the 'optmem' property of AspeedSBCState would be an alias pointing to a drive property of AspeedOTPMemState.
hw/misc/aspeed_sbc.c static void aspeed_sbc_realize(DeviceState *dev, Error **errp) · line 295
Quoted 7 lines
hw/misc/aspeed_sbc.c +7
-0+/* +* If user doesn't specify the otpmem file location, +* to disable the OTP memory feature due to no backend data +*/ +if ((otpmem ==NULL ) || !strlen (otpmem)) { + sc->has_otpmem = false; + }
class attribute are usually compile time constants.
The 'otpmem' object will always be available. However, the drive backend below 'otpmem' might not be defined. So it's up to 'optmem' to implement a fallback. Generally, it's simply an empty buffer. See the m25p80 or pnv_pnor models.
hw/misc/aspeed_sbc.c static void aspeed_sbc_realize(DeviceState *dev, Error **errp) · line 299
Quoted 4 lines
hw/misc/aspeed_sbc.c +4
-0+ +if (sc->has_otpmem) { +object_initialize_child (OBJECT (s),"optmem" , + &s->otpmem,TYPE_ASPEED_OTPMEM );
The child should be initialized in an instance_init handler.
hw/misc/aspeed_sbc.c static void aspeed_sbc_realize(DeviceState *dev, Error **errp) · line 301
Quoted 2 lines
hw/misc/aspeed_sbc.c +2
-0+aspeed_otpmem_set_backend (&s->otpmem, otpmem); +qdev_realize (DEVICE (&s->otpmem),NULL , errp);
the return value should be tested too.
hw/misc/aspeed_sbc.c static void aspeed_sbc_realize(DeviceState *dev, Error **errp) · line 305
Quoted 4 lines
hw/misc/aspeed_sbc.c +4
-0+trace_aspeed_sbc_otpmem_state ("enabled" ); + }else { +trace_aspeed_sbc_otpmem_state ("disabled" ); + }
These trace events don't look useful to me.
Quoted 46 lines
hw/misc/aspeed_sbc.c +0
-0}static const VMStateDescription vmstate_aspeed_sbc = {hw/misc/trace-events +5
-0@@ -95,6 +95,11 @@ aspeed_otpmem_prog(uint32_t addr, uint32_t prog_value, uint32_t value) "OTP Memo aspeed_otpmem_prog_conflict(uint32_t addr, uint32_t bits) "Conflict at addr=0x%x, bits=0x%08x" aspeed_otpmem_prog_bit(int bit) "Programmed bit %d" +# 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_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 +5
-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,10 +37,14 @@ struct AspeedSBCState {MemoryRegion iomem;uint32_t regs[ASPEED_SBC_NR_REGS ]; + + AspeedOTPMemState otpmem; };struct AspeedSBCClass {SysBusDeviceClass parent_class; + +bool has_otpmem; };#endif /* ASPEED_SBC_H */
RE: [PATCH v4 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC controller
Hi Cédric,
Quoted 125 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, May 27, 2025 3:02 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: [PATCH v4 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC controllerQuoted 7 lines · Kane Chen
From: Kane-Chen-AS <[email protected]>
Integrate the aspeed.otpmem backend with the ASPEED Secure Boot Controller (SBC).
This patch adds command handling support in the SBC to read and program the connected OTP memory using READ, WRITE, and PROG
commands.
Quoted 1 line
It enables basic interaction with OTP content for secure boot or fuse
modeling logic.
Quoted 12 lines
Tracepoints are used to monitor command activity and unsupported paths.
The following QOM hierarchy illustrates how OTP is connected:
/machine (ast1030-evb-machine) /soc (ast1030-a1) /sbc (aspeed.sbc-ast10X0) /aspeed.sbc[0] (memory-region) /otpmem (aspeed.otpmem) /aspeed.otpmem.backend[0] (memory-region)Quoted 3 lines
Signed-off-by Kane-Chen-AS <[email protected]>--- hw/misc/aspeed_sbc.c | 179+++++++++++++++++++++++++++++++++++
Quoted 31 lines
hw/misc/trace-events | 5 + include/hw/misc/aspeed_sbc.h | 5 + 3 files changed, 189 insertions(+)hw/misc/aspeed_sbc.c index renamed from hw/misc/aspeed_sbc.c +0
-0
a7d101ba71..237a8499d9100644hw/misc/aspeed_sbc.c +9
-0@@ -15,9 +15,14 @@#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_CAMP2 (0x024 / 4)#define R_QSR (0x040 / 4)/* R_STATUS */ @@ -41,6 +46,19 @@#define QSR_RSA_MASK (0x3 << 12)#define QSR_HASH_MASK (0x3 << 10) +typedef enum { + SBC_OTP_CMD_READ = 0x23b1e361, + SBC_OTP_CMD_WRITE = 0x23b1e362, + SBC_OTP_CMD_PROG = 0x23b1e364,hw/misc/aspeed_sbc.c line 62
Quoted 10 lines
hw/misc/aspeed_sbc.c +9
-0+} SBC_OTP_Command; + +#define OTP_DATA_DWORD_COUNT (0x800) +#define OTP_TOTAL_DWORD_COUNT (0x1000) + +#define MODE_REGISTER (0x1000) +#define MODE_REGISTER_A (0x3000) +#define MODE_REGISTER_B (0x5000) + static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned intsize)
Quoted 3 lines
hw/misc/aspeed_sbc.c +0
-0{ AspeedSBCState *s = ASPEED_SBC(opaque); @@ -57,6 +75,143 @@static
uint64_taspeed_sbc_read(void *opaque, hwaddr addr, unsigned intsize)
hw/misc/aspeed_sbc.c
Quoted 5 lines
return s->regs[addr];}
hw/misc/aspeed_sbc.c +2
-0+static bool aspeed_sbc_otpmem_read (AspeedSBCState *s, +uint32_t otp_addr,Error **errp){
Quoted 11 lines
hw/misc/aspeed_sbc.c +11
-0+uint32_t data =0 , otp_offset; +bool is_data = false; +AspeedSBCClass *sc =ASPEED_SBC_GET_CLASS (s); +const AspeedOTPMemOps *otp_ops; + +if (sc->has_otpmem == false) { +trace_aspeed_sbc_otpmem_state ("disabled" ); +return true; + } + + otp_ops =aspeed_otpmem_get_ops (&s->otpmem);hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 101
Quoted 10 lines
hw/misc/aspeed_sbc.c +10
-0+ +if (otp_addr <OTP_DATA_DWORD_COUNT ) { + is_data = true; + }else if (otp_addr >=OTP_TOTAL_DWORD_COUNT ) { +error_setg (errp,"Invalid OTP addr 0x%x" , otp_addr); +return false; + } + otp_offset = otp_addr <<2 ; + + data = otp_ops->read (&s->otpmem, otp_offset, errp);why not call directly :
ret = address_space_read(&s->otpmem.as, addr, MEMTXATTRS_UNSPECIFIED, (uint8_t *)&val, sizeof(val)); if (ret != MEMTX_OK) { error_setg(errp, "Failed to read data from 0x%x", addr); return OTPMEM_ERR_MAGIC; }
Quoted 2 lines
The
otp_opsbusiness looks useless to me. Same for the write and prog handlers.
The OTP memory has some regions that are protected from guest access, so I use a custom read function to handle that behavior. The write path also has some special handling (see aspeed_otpmem_prog in patch 1), which is why I'm using AspeedOTPMemOps for both. If you have concerns or comments about this implementation, please let me know.
Quoted 160 lines
Quoted 23 lines
hw/misc/aspeed_sbc.c +21
-0+if (*errp) { +return false; + } + s->regs[R_CAMP1 ] = data; + +if (is_data) { + data = otp_ops->read (&s->otpmem, otp_offset +4 , errp); +if (*errp) { +return false; + } + s->regs[R_CAMP2 ] = data; + } + +return true; +} + +static bool mr_handler (uint32_t otp_addr,Error **errp) { +switch (otp_addr) { +case MODE_REGISTER : +case MODE_REGISTER_A : +case MODE_REGISTER_B :hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size)
Quoted 10 lines
hw/misc/aspeed_sbc.c +10
-0+/* HW behavior, do nothing here */ +return true; +default : +error_setg (errp,"Unsupported address 0x%x" , otp_addr); +return false; + } +} + +static bool aspeed_sbc_otpmem_write (AspeedSBCState *s, + uint32_t otp_addr, Error**errp)
Quoted 8 lines
hw/misc/aspeed_sbc.c +8
-0+{ +if (otp_addr ==0 ) { +trace_aspeed_sbc_ignore_cmd (otp_addr); +return true; + }else if (otp_addr >=MODE_REGISTER ) { +if (mr_handler (otp_addr, errp) == false) { +return false; + }hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 151
Quoted 10 lines
hw/misc/aspeed_sbc.c +10
-0+ }else { +error_setg (errp,"Unhandled OTP write address 0x%x" , otp_addr); +return false; + } + +return true; +} + +static bool aspeed_sbc_otpmem_prog (AspeedSBCState *s, +uint32_t otp_addr,Error **errp){
hw/misc/aspeed_sbc.c static void aspeed_sbc_write(void *opaque, hwaddr
Quoted 63 lines
hw/misc/aspeed_sbc.c +59
-0+uint32_t value; +AspeedSBCClass *sc =ASPEED_SBC_GET_CLASS (s); +const AspeedOTPMemOps *otp_ops; + +if (sc->has_otpmem == false) { +trace_aspeed_sbc_otpmem_state ("disabled" ); +return true; + } + + otp_ops =aspeed_otpmem_get_ops (&s->otpmem); + value = s->regs[R_CAMP1 ]; +if (otp_addr >=OTP_TOTAL_DWORD_COUNT ) { +error_setg (errp,"Invalid OTP addr 0x%x" , otp_addr); +return false; + } + + otp_ops->prog(&s->otpmem, otp_addr, value, errp);> + + if (*errp) { +return false; + } + +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_WRITE : + ret =aspeed_sbc_otpmem_write (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 +233,9 @@ static void aspeed_sbc_write(void *opaque, hwaddraddr,
uint64_tdata,hw/misc/aspeed_sbc.c static void aspeed_sbc_write(void *opaque, hwaddr · line 233
Quoted 1 line
hw/misc/aspeed_sbc.c +0
-0"%s: write to read only register 0x%"
HWADDR_PRIx"\n",hw/misc/aspeed_sbc.c static void aspeed_sbc_realize(DeviceState *dev,
Quoted 9 lines
hw/misc/aspeed_sbc.c +3
-0__func__, addr << 2);return ; +case R_CMD : +aspeed_sbc_handle_command (opaque, data); +return ;default :break ; }@@ -119,11 +277,32 @@ static void aspeed_sbc_realize(DeviceState *dev,Error **errp)
hw/misc/aspeed_sbc.c static void aspeed_sbc_realize(DeviceState *dev, Error **errp)
Quoted 7 lines
hw/misc/aspeed_sbc.c +2
-0{AspeedSBCState *s =ASPEED_SBC (dev);SysBusDevice *sbd =SYS_BUS_DEVICE (dev); +AspeedSBCClass *sc =ASPEED_SBC_GET_CLASS (dev); +char *otpmem; memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_sbc_ops,s,
hw/misc/aspeed_sbc.c static void aspeed_sbc_realize(DeviceState *dev, Error **errp)
Quoted 6 lines
hw/misc/aspeed_sbc.c +3
-0TYPE_ASPEED_SBC, 0x1000);sysbus_init_mmio (sbd, &s->iomem); + + otpmem =object_property_get_str (qdev_get_machine (),"otpmem" , + errp);nope. You should use property aliases instead.
The 'optmem' machine (drive) property would be an alias pointing to an 'optmem' property of AspeedSBCState. the 'optmem' property of AspeedSBCState would be an alias pointing to a drive property of AspeedOTPMemState.
hw/misc/aspeed_sbc.c static void aspeed_sbc_realize(DeviceState *dev, Error **errp) · line 295
Quoted 7 lines
hw/misc/aspeed_sbc.c +7
-0+/* +* If user doesn't specify the otpmem file location, +* to disable the OTP memory feature due to no backend data +*/ +if ((otpmem ==NULL ) || !strlen (otpmem)) { + sc->has_otpmem = false; + }
Quoted 8 lines
class attribute are usually compile time constants.
The 'otpmem' object will always be available. However, the drive backend below 'otpmem' might not be defined. So it's up to 'optmem' to implement a fallback. Generally, it's simply an empty buffer. See the m25p80 or
pnv_pnormodels.
Thanks for your suggestion. I will check m25p80 or pnv_pnor for further modification.
Quoted 4 lines
hw/misc/aspeed_sbc.c static void aspeed_sbc_realize(DeviceState *dev, Error **errp)
Quoted 4 lines
hw/misc/aspeed_sbc.c +4
-0+ +if (sc->has_otpmem) { + object_initialize_child(OBJECT(s), "optmem", + &s->otpmem,
Quoted 8 lines
TYPE_ASPEED_OTPMEM);The child should be initialized in an
instance_inithandler.hw/misc/aspeed_sbc.c static void aspeed_sbc_realize(DeviceState *dev, Error **errp) · line 301
Quoted 2 lines
hw/misc/aspeed_sbc.c +2
-0+aspeed_otpmem_set_backend (&s->otpmem, otpmem); +qdev_realize (DEVICE (&s->otpmem),NULL , errp);the return value should be tested too.
I will move the initial code to instance_init handler and check the return value.
Quoted 7 lines
hw/misc/aspeed_sbc.c static void aspeed_sbc_realize(DeviceState *dev, Error **errp) · line 305
Quoted 5 lines
hw/misc/aspeed_sbc.c +4
-0+trace_aspeed_sbc_otpmem_state ("enabled" ); + }else { +trace_aspeed_sbc_otpmem_state ("disabled" ); + }These trace events don't look useful to me.
I will remove these trace events.
Quoted 45 lines
hw/misc/trace-events aspeed_otpmem_prog(uint32_t addr, uint32_t
Quoted 9 lines
hw/misc/aspeed_sbc.c +0
-0} static const VMStateDescription vmstate_aspeed_sbc = { diff --gita/hw/misc/trace-events b/hw/misc/trace-events index
e9df349f6d..e67dcf6653100644hw/misc/trace-events +0
-0@@ -95,6 +95,11 @@ aspeed_otpmem_prog(uint32_t addr, uint32_t
prog_value,uint32_tvalue) "OTP Memohw/misc/trace-events aspeed_otpmem_prog(uint32_t addr, uint32_t · line 95
Quoted 1 line
hw/misc/trace-events +0
-0aspeed_otpmem_prog_conflict(uint32_t addr, uint32_t bits) "Conflict ataddr=0x%x, bits=0x%08x"
hw/misc/trace-events aspeed_otpmem_prog(uint32_t addr, uint32_t prog_value, uint32_t value) "OTP Memo
Quoted 6 lines
hw/misc/trace-events +4
-0aspeed_otpmem_prog_bit(int bit) "Programmed bit %d" +# 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) "Handlingcommand 0x%" PRIx32 " for OTP addr 0x%" PRIx32 " Result: %d"
hw/misc/trace-events aspeed_otpmem_prog(uint32_t addr, uint32_t prog_value, uint32_t value) "OTP Memo
Quoted 3 lines
hw/misc/trace-events +1
-0+ # aspeed_scu.c aspeed_scu_write(uint64_t offset, unsigned size, uint32_t data) "To 0x%"PRIx64 " of size %u: 0x%" PRIx32
Quoted 22 lines
hw/misc/trace-events +0
-0aspeed_scu_read(uint64_t offset, unsigned size, uint32_t data) "To0x%" PRIx64 " of size %u: 0x%" PRIx32 diff --git
a/include/hw/misc/aspeed_sbc.hb/include/hw/misc/aspeed_sbc.hindex405e6782b9..667d6088c0100644include/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,10 +37,14 @@ struct AspeedSBCState {MemoryRegion iomem;uint32_t regs[ASPEED_SBC_NR_REGS ]; + +AspeedOTPMemState otpmem; };
Quoted 7 lines
include/hw/misc/aspeed_sbc.h struct AspeedSBCState { · line 50
Quoted 7 lines
include/hw/misc/aspeed_sbc.h +2
-0struct AspeedSBCClass {SysBusDeviceClass parent_class; + +bool has_otpmem; };#endif /* ASPEED_SBC_H */
Best Regards,
Kane
Re: [PATCH v4 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC controller
Quoted 127 lines · Kane Chen
Hi Cédric,
Quoted 125 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, May 27, 2025 3:02 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: [PATCH v4 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC controllerQuoted 7 lines · Kane Chen
From: Kane-Chen-AS <[email protected]>
Integrate the aspeed.otpmem backend with the ASPEED Secure Boot Controller (SBC).
This patch adds command handling support in the SBC to read and program the connected OTP memory using READ, WRITE, and PROG
commands.
Quoted 1 line
It enables basic interaction with OTP content for secure boot or fuse
modeling logic.
Quoted 12 lines
Tracepoints are used to monitor command activity and unsupported paths.
The following QOM hierarchy illustrates how OTP is connected:
/machine (ast1030-evb-machine) /soc (ast1030-a1) /sbc (aspeed.sbc-ast10X0) /aspeed.sbc[0] (memory-region) /otpmem (aspeed.otpmem) /aspeed.otpmem.backend[0] (memory-region)Quoted 3 lines
Signed-off-by Kane-Chen-AS <[email protected]>--- hw/misc/aspeed_sbc.c | 179+++++++++++++++++++++++++++++++++++
Quoted 31 lines
hw/misc/trace-events | 5 + include/hw/misc/aspeed_sbc.h | 5 + 3 files changed, 189 insertions(+)hw/misc/aspeed_sbc.c index renamed from hw/misc/aspeed_sbc.c +0
-0
a7d101ba71..237a8499d9100644hw/misc/aspeed_sbc.c +9
-0@@ -15,9 +15,14 @@#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_CAMP2 (0x024 / 4)#define R_QSR (0x040 / 4)/* R_STATUS */ @@ -41,6 +46,19 @@#define QSR_RSA_MASK (0x3 << 12)#define QSR_HASH_MASK (0x3 << 10) +typedef enum { + SBC_OTP_CMD_READ = 0x23b1e361, + SBC_OTP_CMD_WRITE = 0x23b1e362, + SBC_OTP_CMD_PROG = 0x23b1e364,hw/misc/aspeed_sbc.c line 62
Quoted 10 lines
hw/misc/aspeed_sbc.c +9
-0+} SBC_OTP_Command; + +#define OTP_DATA_DWORD_COUNT (0x800) +#define OTP_TOTAL_DWORD_COUNT (0x1000) + +#define MODE_REGISTER (0x1000) +#define MODE_REGISTER_A (0x3000) +#define MODE_REGISTER_B (0x5000) + static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned intsize)
Quoted 3 lines
hw/misc/aspeed_sbc.c +0
-0{ AspeedSBCState *s = ASPEED_SBC(opaque); @@ -57,6 +75,143 @@static
uint64_taspeed_sbc_read(void *opaque, hwaddr addr, unsigned intsize)
hw/misc/aspeed_sbc.c
Quoted 5 lines
return s->regs[addr];}
hw/misc/aspeed_sbc.c +2
-0+static bool aspeed_sbc_otpmem_read (AspeedSBCState *s, +uint32_t otp_addr,Error **errp){
Quoted 11 lines
hw/misc/aspeed_sbc.c +11
-0+uint32_t data =0 , otp_offset; +bool is_data = false; +AspeedSBCClass *sc =ASPEED_SBC_GET_CLASS (s); +const AspeedOTPMemOps *otp_ops; + +if (sc->has_otpmem == false) { +trace_aspeed_sbc_otpmem_state ("disabled" ); +return true; + } + + otp_ops =aspeed_otpmem_get_ops (&s->otpmem);hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 101
Quoted 10 lines
hw/misc/aspeed_sbc.c +10
-0+ +if (otp_addr <OTP_DATA_DWORD_COUNT ) { + is_data = true; + }else if (otp_addr >=OTP_TOTAL_DWORD_COUNT ) { +error_setg (errp,"Invalid OTP addr 0x%x" , otp_addr); +return false; + } + otp_offset = otp_addr <<2 ; + + data = otp_ops->read (&s->otpmem, otp_offset, errp);why not call directly :
ret = address_space_read(&s->otpmem.as, addr, MEMTXATTRS_UNSPECIFIED, (uint8_t *)&val, sizeof(val)); if (ret != MEMTX_OK) { error_setg(errp, "Failed to read data from 0x%x", addr); return OTPMEM_ERR_MAGIC; }
Quoted 5 lines
Quoted 2 lines
The
otp_opsbusiness looks useless to me. Same for the write and prog handlers.The OTP memory has some regions that are protected from guest access, so I use a custom read function to handle that behavior.
Why not fail the transaction on the address_space then ?
Quoted 4 lines
The write path
also has some special handling (seeaspeed_otpmem_progin patch 1), which is why I'm using AspeedOTPMemOps for both. If you have concerns or comments about this implementation, please let me know.
If you want to keep routine program_otpmem_data at a low level, under the AspeedOTPMem model, it could be implemented using the drive backend directly instead of doing address_space transactions. You could also move all the code from aspeed_otpmem_prog() code in aspeed_sbc_otpmem_prog(). As you wish.
I don't see any reason to keep AspeedOTPMemOps.
Thanks,
C.
RE: [PATCH v4 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC controller
Hi Cédric,
Quoted 158 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, May 27, 2025 4:12 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: [PATCH v4 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC controllerQuoted 88 lines · Kane Chen
Hi Cédric,
Quoted 86 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, May 27, 2025 3:02 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: [PATCH v4 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC controllerQuoted 7 lines · Kane Chen
From: Kane-Chen-AS <[email protected]>
Integrate the aspeed.otpmem backend with the ASPEED Secure Boot Controller (SBC).
This patch adds command handling support in the SBC to read and program the connected OTP memory using READ, WRITE, and PROG
commands.
Quoted 2 lines
It enables basic interaction with OTP content for secure boot or fuse
modeling logic.
Quoted 12 lines
Tracepoints are used to monitor command activity and unsupported paths.
The following QOM hierarchy illustrates how OTP is connected:
/machine (ast1030-evb-machine) /soc (ast1030-a1) /sbc (aspeed.sbc-ast10X0) /aspeed.sbc[0] (memory-region) /otpmem (aspeed.otpmem) /aspeed.otpmem.backend[0] (memory-region)Quoted 3 lines
Signed-off-by Kane-Chen-AS <[email protected]>--- hw/misc/aspeed_sbc.c | 179+++++++++++++++++++++++++++++++++++
Quoted 41 lines
hw/misc/trace-events | 5 + include/hw/misc/aspeed_sbc.h | 5 + 3 files changed, 189 insertions(+)hw/misc/aspeed_sbc.c index renamed from hw/misc/aspeed_sbc.c +0
-0
a7d101ba71..237a8499d9100644hw/misc/aspeed_sbc.c +18
-0@@ -15,9 +15,14 @@#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_CAMP2 (0x024 / 4)#define R_QSR (0x040 / 4)/* R_STATUS */ @@ -41,6 +46,19 @@#define QSR_RSA_MASK (0x3 << 12)#define QSR_HASH_MASK (0x3 << 10) +typedef enum { +SBC_OTP_CMD_READ =0x23b1e361 , +SBC_OTP_CMD_WRITE =0x23b1e362 , +SBC_OTP_CMD_PROG =0x23b1e364 , +}SBC_OTP_Command ; + +#define OTP_DATA_DWORD_COUNT (0x800) +#define OTP_TOTAL_DWORD_COUNT (0x1000) + +#define MODE_REGISTER (0x1000) +#define MODE_REGISTER_A (0x3000) +#define MODE_REGISTER_B (0x5000) + static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr,Quoted 1 line
unsigned int
size)
Quoted 2 lines
{ AspeedSBCState *s = ASPEED_SBC(opaque); @@ -57,6 +75,143@@
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
static
uint64_taspeed_sbc_read(void *opaque, hwaddr addr, unsigned intsize)
Quoted 5 lines
return s->regs[addr];}
hw/misc/aspeed_sbc.c +2
-0+static bool aspeed_sbc_otpmem_read (AspeedSBCState *s, + uint32_t otp_addr, Error**errp)
Quoted 34 lines
Quoted 34 lines
{
Quoted 11 lines
hw/misc/aspeed_sbc.c +11
-0+uint32_t data =0 , otp_offset; +bool is_data = false; +AspeedSBCClass *sc =ASPEED_SBC_GET_CLASS (s); +const AspeedOTPMemOps *otp_ops; + +if (sc->has_otpmem == false) { +trace_aspeed_sbc_otpmem_state ("disabled" ); +return true; + } + + otp_ops =aspeed_otpmem_get_ops (&s->otpmem);hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 101
Quoted 10 lines
hw/misc/aspeed_sbc.c +10
-0+ +if (otp_addr <OTP_DATA_DWORD_COUNT ) { + is_data = true; + }else if (otp_addr >=OTP_TOTAL_DWORD_COUNT ) { +error_setg (errp,"Invalid OTP addr 0x%x" , otp_addr); +return false; + } + otp_offset = otp_addr <<2 ; + + data = otp_ops->read (&s->otpmem, otp_offset, errp);why not call directly :
ret = address_space_read(&s->otpmem.as, addr, MEMTXATTRS_UNSPECIFIED, (uint8_t *)&val, sizeof(val)); if (ret != MEMTX_OK) { error_setg(errp, "Failed to read data from 0x%x", addr); return OTPMEM_ERR_MAGIC; }Quoted 5 lines
Quoted 2 lines
The
otp_opsbusiness looks useless to me. Same for the write and prog handlers.The OTP memory has some regions that are protected from guest access, so I use a custom read function to handle that behavior.
Why not fail the transaction on the
address_spacethen ?Quoted 4 lines
The write path
also has some special handling (seeaspeed_otpmem_progin patch 1), which is why I'm using AspeedOTPMemOps for both. If you have concerns or comments about this implementation, please let me know.
Quoted 8 lines
If you want to keep routine
program_otpmem_dataat a low level, under the AspeedOTPMem model, it could be implemented using the drive backend directly instead of doingaddress_spacetransactions.
You could also move all the code fromaspeed_otpmem_prog()code inaspeed_sbc_otpmem_prog(). As you wish.I don't see any reason to keep AspeedOTPMemOps.
Got it. I think I misunderstood how address_space works. You're referring to something like pnv_pnor_ops, right? I'll update the code and remove AspeedOTPMemOps.
Thanks for the suggestion!
Quoted 3 lines
Thanks,
C.
Best regards,
Kane
Re: [PATCH v4 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC controller
Quoted 166 lines · Kane Chen
Hi Cédric,
Quoted 164 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, May 27, 2025 4:12 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: [PATCH v4 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC controllerQuoted 88 lines · Kane Chen
Hi Cédric,
Quoted 86 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, May 27, 2025 3:02 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: [PATCH v4 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC controllerQuoted 7 lines · Kane Chen
From: Kane-Chen-AS <[email protected]>
Integrate the aspeed.otpmem backend with the ASPEED Secure Boot Controller (SBC).
This patch adds command handling support in the SBC to read and program the connected OTP memory using READ, WRITE, and PROG
commands.
Quoted 2 lines
It enables basic interaction with OTP content for secure boot or fuse
modeling logic.
Quoted 12 lines
Tracepoints are used to monitor command activity and unsupported paths.
The following QOM hierarchy illustrates how OTP is connected:
/machine (ast1030-evb-machine) /soc (ast1030-a1) /sbc (aspeed.sbc-ast10X0) /aspeed.sbc[0] (memory-region) /otpmem (aspeed.otpmem) /aspeed.otpmem.backend[0] (memory-region)Quoted 3 lines
Signed-off-by Kane-Chen-AS <[email protected]>--- hw/misc/aspeed_sbc.c | 179+++++++++++++++++++++++++++++++++++
Quoted 41 lines
hw/misc/trace-events | 5 + include/hw/misc/aspeed_sbc.h | 5 + 3 files changed, 189 insertions(+)hw/misc/aspeed_sbc.c index renamed from hw/misc/aspeed_sbc.c +0
-0
a7d101ba71..237a8499d9100644hw/misc/aspeed_sbc.c +18
-0@@ -15,9 +15,14 @@#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_CAMP2 (0x024 / 4)#define R_QSR (0x040 / 4)/* R_STATUS */ @@ -41,6 +46,19 @@#define QSR_RSA_MASK (0x3 << 12)#define QSR_HASH_MASK (0x3 << 10) +typedef enum { +SBC_OTP_CMD_READ =0x23b1e361 , +SBC_OTP_CMD_WRITE =0x23b1e362 , +SBC_OTP_CMD_PROG =0x23b1e364 , +}SBC_OTP_Command ; + +#define OTP_DATA_DWORD_COUNT (0x800) +#define OTP_TOTAL_DWORD_COUNT (0x1000) + +#define MODE_REGISTER (0x1000) +#define MODE_REGISTER_A (0x3000) +#define MODE_REGISTER_B (0x5000) + static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr,Quoted 1 line
unsigned int
size)
Quoted 2 lines
{ AspeedSBCState *s = ASPEED_SBC(opaque); @@ -57,6 +75,143@@
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
static
uint64_taspeed_sbc_read(void *opaque, hwaddr addr, unsigned intsize)
Quoted 5 lines
return s->regs[addr]; }hw/misc/aspeed_sbc.c +2
-0+static bool aspeed_sbc_otpmem_read (AspeedSBCState *s, + uint32_t otp_addr, Error**errp)
Quoted 34 lines
Quoted 34 lines
{
Quoted 11 lines
hw/misc/aspeed_sbc.c +11
-0+uint32_t data =0 , otp_offset; +bool is_data = false; +AspeedSBCClass *sc =ASPEED_SBC_GET_CLASS (s); +const AspeedOTPMemOps *otp_ops; + +if (sc->has_otpmem == false) { +trace_aspeed_sbc_otpmem_state ("disabled" ); +return true; + } + + otp_ops =aspeed_otpmem_get_ops (&s->otpmem);hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 101
Quoted 10 lines
hw/misc/aspeed_sbc.c +10
-0+ +if (otp_addr <OTP_DATA_DWORD_COUNT ) { + is_data = true; + }else if (otp_addr >=OTP_TOTAL_DWORD_COUNT ) { +error_setg (errp,"Invalid OTP addr 0x%x" , otp_addr); +return false; + } + otp_offset = otp_addr <<2 ; + + data = otp_ops->read (&s->otpmem, otp_offset, errp);why not call directly :
ret = address_space_read(&s->otpmem.as, addr, MEMTXATTRS_UNSPECIFIED, (uint8_t *)&val, sizeof(val)); if (ret != MEMTX_OK) { error_setg(errp, "Failed to read data from 0x%x", addr); return OTPMEM_ERR_MAGIC; }Quoted 5 lines
Quoted 2 lines
The
otp_opsbusiness looks useless to me. Same for the write and prog handlers.The OTP memory has some regions that are protected from guest access, so I use a custom read function to handle that behavior.
Why not fail the transaction on the
address_spacethen ?Quoted 4 lines
The write path
also has some special handling (seeaspeed_otpmem_progin patch 1), which is why I'm using AspeedOTPMemOps for both. If you have concerns or comments about this implementation, please let me know.If you want to keep routine
program_otpmem_dataat a low level, under the AspeedOTPMem model, it could be implemented using the drive backend directly instead of doingaddress_spacetransactions.
You could also move all the code fromaspeed_otpmem_prog()code inaspeed_sbc_otpmem_prog(). As you wish.
Quoted 4 lines
Quoted 2 lines
I don't see any reason to keep AspeedOTPMemOps.
Got it. I think I misunderstood how
address_spaceworks. You're referring to something likepnv_pnor_ops, right?
yes. AspeedSBCState generates transactions on the AspeedOTPMem address space, which are forwarded to the AspeedOTPMem memory region memops, which generates transactions on the drive backend if present or on an allocated buffer if not.
Thanks,
C.
Quoted 1 line
I'll update the code and remove AspeedOTPMemOps.
Re: [PATCH v4 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC controller
Quoted 7 lines
Quoted 2 lines
Got it. I think I misunderstood how
address_spaceworks. You're referring to something likepnv_pnor_ops, right?yes. AspeedSBCState generates transactions on the AspeedOTPMem address space, which are forwarded to the AspeedOTPMem memory region memops, which generates transactions on the drive backend if present or on an allocated buffer if not.
That said, I think you should first propose an initial implementation with only an allocated buffer and then add a drive backend. It would clarify the design.
Thanks,
C.
RE: [PATCH v4 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC controller
Hi Cédric,
Sure, I’ll submit a version that removes the more specific operations and focuses on the initial implementation first.
Thanks again for the guidance!
Best Regards,
Kane
Quoted 28 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, May 27, 2025 5:36 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: [PATCH v4 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC controllerQuoted 7 lines
Quoted 2 lines
Got it. I think I misunderstood how
address_spaceworks. You're referring to something likepnv_pnor_ops, right?yes. AspeedSBCState generates transactions on the AspeedOTPMem address space, which are forwarded to the AspeedOTPMem memory region memops, which generates transactions on the drive backend if present or on an allocated buffer if not.
That said, I think you should first propose an initial implementation with only an allocated buffer and then add a drive backend. It would clarify the design.
Thanks,
C.
PATCH v4 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 model for ASPEED One-Time Programmable (OTP) memory.
This device provides:
- Bit-level irreversible programming (0→1 for even, 1→0 for odd)
- Read, program, and default-value initialization interfaces
- File-backed OTP content via machine parameter
- Trace support for bit conflict debugging
- Error propagation via QEMU's Error** interface
The OTP backend is designed to integrate with SoCs requiring secure fuse storage or secure boot features.
--- hw/misc/aspeed_otpmem.c | 198 ++++++++++++++++++++++++++++++++ hw/misc/meson.build | 1 + hw/misc/trace-events | 5 + include/hw/misc/aspeed_otpmem.h | 39 +++++++ 4 files changed, 243 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 +198 -0
@@ -0,0 +1,198 @@ +/* +* 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 "qemu/log.h" +#include "qapi/error.h" +#include "trace.h" +#include "hw/misc/aspeed_otpmem.h" + +void aspeed_otpmem_set_backend (AspeedOTPMemState *s,const char *path) +{ +g_free (s->otpmem_img_path); + s->otpmem_img_path =g_strdup (path); +} + +static void aspeed_otpmem_sync_region (AspeedOTPMemState *s, +hwaddr offset,hwaddr size) +{ +memory_region_msync (&s->iomem, offset, size); +} + +static uint32_t aspeed_otpmem_read (AspeedOtpmemState *s, +uint32_t addr,Error **errp) +{ +uint32_t val =0 ; +MemTxResult ret; + + ret =address_space_read (&s->as, addr,MEMTXATTRS_UNSPECIFIED , + (uint8_t *)&val,sizeof (val)); +if (ret !=MEMTX_OK ) { +error_setg (errp,"Failed to read data from 0x%x" , addr); +return OTPMEM_ERR_MAGIC ; + } +return val; +} + +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 = 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 = ~value & prog_bit; + }else { + programmed_bits = value & (~prog_bit); + } + +/* If any bit can be programmed, accept the request */ + has_programmable_bits = value ^ (~prog_bit); + +if (programmed_bits) { +trace_aspeed_otpmem_prog_conflict (otp_addr, programmed_bits); +for (int i =0 ; i <32 ; ++i) { +if (programmed_bits & (1U << i)) { +trace_aspeed_otpmem_prog_bit (i); + } + } + } + +return has_programmable_bits !=0 ; +} + +static bool program_otpmem_data (void *opaque,uint32_t otp_addr, +uint32_t prog_bit,uint32_t *value) +{ +AspeedOTPMemState *s =ASPEED_OTPMEM (opaque); +bool is_odd = otp_addr &1 ; +uint32_t otp_offset = otp_addr <<2 ; +MemTxResult ret; + + ret =address_space_read (&s->as, otp_offset,MEMTXATTRS_UNSPECIFIED , + value,sizeof (uint32_t )); +if (ret !=MEMTX_OK ) { +qemu_log_mask (LOG_GUEST_ERROR , +"Failed to read data 0x%x\n" , + otp_offset); +return false; + } + +if (!valid_program_data (otp_addr, *value, prog_bit)) { +return false; + } + +if (is_odd) { + *value &= ~prog_bit; + }else { + *value |= ~prog_bit; + } + +return true; +} + +static void aspeed_otpmem_prog (AspeedOtpmemState *s,uint32_t otp_addr, +uint32_t val,Error **errp) +{ +uint32_t otp_offset, value; +MemTxResult ret; + +if (!program_otpmem_data (s, otp_addr, val, &value)) { +error_setg (errp,"Failed to program data" ); +return ; + } + + otp_offset = otp_addr <<2 ; + ret =address_space_write (&s->as, otp_offset,MEMTXATTRS_UNSPECIFIED , + (uint8_t *)&value,sizeof (value)); +if (ret !=MEMTX_OK ) { +error_setg (errp,"Failed to write %x to OTP [%x]" , val, otp_addr); +return ; + } +trace_aspeed_otpmem_prog (otp_offset, value, val); +aspeed_otpmem_sync_region (s, otp_offset,sizeof (value)); +} + +static void aspeed_otpmem_set_default (AspeedOtpmemState *s,uint32_t otp_offset, +uint32_t val,Error **errp) +{ +MemTxResult ret; + + ret =address_space_write (&s->as, otp_offset,MEMTXATTRS_UNSPECIFIED , + (uint8_t *)&val,sizeof (val)); +if (ret !=MEMTX_OK ) { +error_setg (errp,"Failed to set value %x to OTP [%x]" , val, otp_offset); +return ; + } +aspeed_otpmem_sync_region (s, otp_offset,sizeof (val)); +} + +static const AspeedOTPMemOps aspeed_otpmem_default_ops = { + .read = aspeed_otpmem_read, + .prog = aspeed_otpmem_prog, + .set_default = aspeed_otpmem_set_default, +}; + +const AspeedOTPMemOps *aspeed_otpmem_get_ops (AspeedOTPMemState *s) +{ +return s->ops; +} + +static void aspeed_otpmem_realize (DeviceState *dev,Error **errp) +{ +AspeedOTPMemState *s =ASPEED_OTPMEM (dev); +struct stat st; + + s->size =OTPMEM_SIZE ; + s->ops = &aspeed_otpmem_default_ops; + +if (s->otpmem_img_path &&strlen (s->otpmem_img_path)) { +if (stat (s->otpmem_img_path, &st) <0 ) { +error_setg (errp,"Failed to open %s" , + s->otpmem_img_path); +return ; + } +if (st.st_size !=OTPMEM_SIZE ) { +error_setg (errp,"Invalid OTP size %ld" , + st.st_size); +return ; + } +memory_region_init_ram_from_file (&s->iomem,OBJECT (dev), +"aspeed.otpmem.backend" , s->size, s->size, +RAM_SHARED , s->otpmem_img_path,0 , errp); +address_space_init (&s->as, &s->iomem,NULL ); + } +} + +static void aspeed_otpmem_class_init (ObjectClass *klass,const void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + dc->realize = aspeed_otpmem_realize; + dc->user_creatable = false; +} + +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'))
hw/misc/trace-events +5 -0
@@ -90,6 +90,11 @@ 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" +# aspeed_otpmem.c +aspeed_otpmem_prog(uint32_t addr, uint32_t prog_value, uint32_t value) "OTP Memory program: addr 0x%" PRIx32 " prog_value 0x%" PRIx32 " value 0x%" PRIx32 +aspeed_otpmem_prog_conflict(uint32_t addr, uint32_t bits) "Conflict at addr=0x%x, bits=0x%08x" +aspeed_otpmem_prog_bit(int bit) "Programmed bit %d" + # 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_otpmem.h new file +39 -0
@@ -0,0 +1,39 @@ +/* +* 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 "system/memory.h" + +#define OTPMEM_SIZE 0x4000 +#define OTPMEM_ERR_MAGIC 0x45727200 +#define TYPE_ASPEED_OTPMEM "aspeed.otpmem" +OBJECT_DECLARE_SIMPLE_TYPE(AspeedOTPMemState, ASPEED_OTPMEM) + +typedef struct AspeedOTPMemOps { +uint32_t (*read)(AspeedOTPMemState *s,uint32_t addr,Error **errp); +void (*prog)(AspeedOTPMemState *s,uint32_t addr,uint32_t val,Error **errp); +void (*set_default)(AspeedOTPMemState *s,uint32_t addr,uint32_t val,Error **errp); +}AspeedOTPMemOps ; + +typedef struct AspeedOTPMemState { +DeviceState parent_obj; + +MemoryRegion iomem; +AddressSpace as; +size_t size; + +const AspeedOTPMemOps *ops; +char *otpmem_img_path; +}AspeedOtpmemState ; + +const AspeedOTPMemOps *aspeed_otpmem_get_ops (AspeedOTPMemState *s); +void aspeed_otpmem_set_backend (AspeedOTPMemState *s,const char *path); +#endif /* ASPEED_OTPMMEM_H */
Signature · 1 line
2.43.0
Re: [PATCH v4 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device model
Hello Kane
Quoted 14 lines · Kane Chen
From: Kane-Chen-AS <[email protected]>
Introduce a QEMU model for ASPEED One-Time Programmable (OTP) memory.
This device provides:
- Bit-level irreversible programming (0→1 for even, 1→0 for odd)
- Read, program, and default-value initialization interfaces
- File-backed OTP content via machine parameter
- Trace support for bit conflict debugging
- Error propagation via QEMU's Error** interfaceThe OTP backend is designed to integrate with SoCs requiring secure fuse storage or secure boot features.
Quoted 6 lines
Signed-off-by Kane-Chen-AS <[email protected]>--- hw/misc/aspeed_otpmem.c | 198 ++++++++++++++++++++++++++++++++ hw/misc/meson.build | 1 + hw/misc/trace-events | 5 + include/hw/misc/aspeed_otpmem.h | 39 +++++++
please include in your .git/config
[diff]
orderFile = /path/to/qemu/scripts/git.orderfile
Quoted 10 lines
4 files changed, 243 insertions(+)create mode 100644
hw/misc/aspeed_otpmem.ccreate mode 100644include/hw/misc/aspeed_otpmem.hhw/misc/aspeed_otpmem.c new file +0
-0
hw/misc/aspeed_otpmem.c line 7
Quoted 7 lines
hw/misc/aspeed_otpmem.c new file +7
-0@@ -0,0 +1,198 @@ +/* +* 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.
Replace the license boiler plate with :
SPDX-License-Identifier: GPL-2.0-or-later
Quoted 166 lines
hw/misc/aspeed_otpmem.c new file +166
-0+*/ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qapi/error.h" +#include "trace.h" +#include "hw/misc/aspeed_otpmem.h" + +void aspeed_otpmem_set_backend (AspeedOTPMemState *s,const char *path) +{ +g_free (s->otpmem_img_path); + s->otpmem_img_path =g_strdup (path); +} + +static void aspeed_otpmem_sync_region (AspeedOTPMemState *s, +hwaddr offset,hwaddr size) +{ +memory_region_msync (&s->iomem, offset, size); +} + +static uint32_t aspeed_otpmem_read (AspeedOtpmemState *s, +uint32_t addr,Error **errp) +{ +uint32_t val =0 ; +MemTxResult ret; + + ret =address_space_read (&s->as, addr,MEMTXATTRS_UNSPECIFIED , + (uint8_t *)&val,sizeof (val)); +if (ret !=MEMTX_OK ) { +error_setg (errp,"Failed to read data from 0x%x" , addr); +return OTPMEM_ERR_MAGIC ; + } +return val; +} + +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 = 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 = ~value & prog_bit; + }else { + programmed_bits = value & (~prog_bit); + } + +/* If any bit can be programmed, accept the request */ + has_programmable_bits = value ^ (~prog_bit); + +if (programmed_bits) { +trace_aspeed_otpmem_prog_conflict (otp_addr, programmed_bits); +for (int i =0 ; i <32 ; ++i) { +if (programmed_bits & (1U << i)) { +trace_aspeed_otpmem_prog_bit (i); + } + } + } + +return has_programmable_bits !=0 ; +} + +static bool program_otpmem_data (void *opaque,uint32_t otp_addr, +uint32_t prog_bit,uint32_t *value) +{ +AspeedOTPMemState *s =ASPEED_OTPMEM (opaque); +bool is_odd = otp_addr &1 ; +uint32_t otp_offset = otp_addr <<2 ; +MemTxResult ret; + + ret =address_space_read (&s->as, otp_offset,MEMTXATTRS_UNSPECIFIED , + value,sizeof (uint32_t )); +if (ret !=MEMTX_OK ) { +qemu_log_mask (LOG_GUEST_ERROR , +"Failed to read data 0x%x\n" , + otp_offset); +return false; + } + +if (!valid_program_data (otp_addr, *value, prog_bit)) { +return false; + } + +if (is_odd) { + *value &= ~prog_bit; + }else { + *value |= ~prog_bit; + } + +return true; +} + +static void aspeed_otpmem_prog (AspeedOtpmemState *s,uint32_t otp_addr, +uint32_t val,Error **errp) +{ +uint32_t otp_offset, value; +MemTxResult ret; + +if (!program_otpmem_data (s, otp_addr, val, &value)) { +error_setg (errp,"Failed to program data" ); +return ; + } + + otp_offset = otp_addr <<2 ; + ret =address_space_write (&s->as, otp_offset,MEMTXATTRS_UNSPECIFIED , + (uint8_t *)&value,sizeof (value)); +if (ret !=MEMTX_OK ) { +error_setg (errp,"Failed to write %x to OTP [%x]" , val, otp_addr); +return ; + } +trace_aspeed_otpmem_prog (otp_offset, value, val); +aspeed_otpmem_sync_region (s, otp_offset,sizeof (value)); +} + +static void aspeed_otpmem_set_default (AspeedOtpmemState *s,uint32_t otp_offset, +uint32_t val,Error **errp) +{ +MemTxResult ret; + + ret =address_space_write (&s->as, otp_offset,MEMTXATTRS_UNSPECIFIED , + (uint8_t *)&val,sizeof (val)); +if (ret !=MEMTX_OK ) { +error_setg (errp,"Failed to set value %x to OTP [%x]" , val, otp_offset); +return ; + } +aspeed_otpmem_sync_region (s, otp_offset,sizeof (val)); +} + +static const AspeedOTPMemOps aspeed_otpmem_default_ops = { + .read = aspeed_otpmem_read, + .prog = aspeed_otpmem_prog, + .set_default = aspeed_otpmem_set_default, +};> + +const AspeedOTPMemOps *aspeed_otpmem_get_ops(AspeedOTPMemState *s) +{ +return s->ops; +} + +static void aspeed_otpmem_realize (DeviceState *dev,Error **errp) +{ +AspeedOTPMemState *s =ASPEED_OTPMEM (dev); +struct stat st; + + s->size =OTPMEM_SIZE ; + s->ops = &aspeed_otpmem_default_ops; + +if (s->otpmem_img_path &&strlen (s->otpmem_img_path)) { +if (stat (s->otpmem_img_path, &st) <0 ) { +error_setg (errp,"Failed to open %s" , + s->otpmem_img_path); +return ; + } +if (st.st_size !=OTPMEM_SIZE ) { +error_setg (errp,"Invalid OTP size %ld" , + st.st_size); +return ; + } +memory_region_init_ram_from_file (&s->iomem,OBJECT (dev), +"aspeed.otpmem.backend" , s->size, s->size, +RAM_SHARED , s->otpmem_img_path,0 , errp);
hw/misc/aspeed_otpmem.c line 184
Quoted 10 lines
hw/misc/aspeed_otpmem.c new file +10
-0+address_space_init (&s->as, &s->iomem,NULL ); + } +} + +static void aspeed_otpmem_class_init (ObjectClass *klass,const void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + dc->realize = aspeed_otpmem_realize; + dc->user_creatable = false; +}
IIRC, I thought the AspeedOTPMemState model would have a drive property ? See pnv_pnor.c for a simple example.
static const Property aspeed_otpmem_info_properties[] = {
DEFINE_PROP_INT64("size", AspeedOTPMemState, size, 64 * KiB), DEFINE_PROP_DRIVE("drive", AspeedOTPMemState, blk),
};
Quoted 62 lines
hw/misc/aspeed_otpmem.c new file +14
-0+ +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'))hw/misc/trace-events +5
-0@@ -90,6 +90,11 @@ 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" +# aspeed_otpmem.c +aspeed_otpmem_prog(uint32_t addr, uint32_t prog_value, uint32_t value) "OTP Memory program: addr 0x%" PRIx32 " prog_value 0x%" PRIx32 " value 0x%" PRIx32 +aspeed_otpmem_prog_conflict(uint32_t addr, uint32_t bits) "Conflict at addr=0x%x, bits=0x%08x" +aspeed_otpmem_prog_bit(int bit) "Programmed bit %d" + # 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_otpmem.h new file +14
-0@@ -0,0 +1,39 @@ +/* +* 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 "system/memory.h" +
include/hw/misc/aspeed_otpmem.h line 24
Quoted 10 lines
include/hw/misc/aspeed_otpmem.h new file +10
-0+#define OTPMEM_SIZE 0x4000 +#define OTPMEM_ERR_MAGIC 0x45727200 +#define TYPE_ASPEED_OTPMEM "aspeed.otpmem" +OBJECT_DECLARE_SIMPLE_TYPE(AspeedOTPMemState, ASPEED_OTPMEM) + +typedef struct AspeedOTPMemOps { +uint32_t (*read)(AspeedOTPMemState *s,uint32_t addr,Error **errp); +void (*prog)(AspeedOTPMemState *s,uint32_t addr,uint32_t val,Error **errp); +void (*set_default)(AspeedOTPMemState *s,uint32_t addr,uint32_t val,Error **errp); +}AspeedOTPMemOps ;
I fail to understand how these ops are useful. See comments on patch 2.
include/hw/misc/aspeed_otpmem.h line 31
Quoted 7 lines
include/hw/misc/aspeed_otpmem.h new file +7
-0+ +typedef struct AspeedOTPMemState { + DeviceState parent_obj; + + MemoryRegion iomem; + AddressSpace as; + size_t size;
may be add a property for size too.
Thanks,
C.
Quoted 8 lines
include/hw/misc/aspeed_otpmem.h new file +8
-0+ +const AspeedOTPMemOps *ops; + char *otpmem_img_path; +} AspeedOtpmemState; + +const AspeedOTPMemOps *aspeed_otpmem_get_ops (AspeedOTPMemState *s); +void aspeed_otpmem_set_backend (AspeedOTPMemState *s,const char *path); +#endif /* ASPEED_OTPMMEM_H */
PATCH v4 3/3 hw/arm: Integrate ASPEED OTP memory into AST10x0 and AST2600 SoCs
In reply to Kane Chen viaFrom: Kane-Chen-AS <[email protected]>
Expose an "otpmem" machine parameter to load an external OTP memory image and enable OTP functionality in supported SoCs.
- Adds object property and backend connection logic for AST1030 and AST2600
- Allows disabling OTP feature via has_otpmem attribute
- Supports use cases involving preconfigured OTP content for boot-time behavior
To create a default OTP memory image with programmable and unprogrammable bit patterns, run the following:
```bash for i in $(seq 1 2048); do printf '\x00\x00\x00\x00\xff\xff\xff\xff' done > otpmem.img ```
Use the otpmem parameter to load this into the guest:
``` ./qemu-system-arm \ -machine ast2600-evb,otpmem=otpmem.img \ ... ```
--- hw/arm/aspeed.c | 20 ++++++++++++++++++++ hw/arm/aspeed_ast10x0.c | 2 +- hw/arm/aspeed_ast2600.c | 2 +- hw/misc/aspeed_sbc.c | 18 ++++++++++++++++++ include/hw/misc/aspeed_sbc.h | 1 + 5 files changed, 41 insertions(+), 2 deletions(-)
hw/arm/aspeed.c +20 -0
@@ -47,6 +47,7 @@ struct AspeedMachineState {uint32_t uart_chosen;char *fmc_model;char *spi_model; +char *otpmem;uint32_t hw_strap1; };
@@ -1199,6 +1200,21 @@ static void aspeed_set_bmc_console(Object *obj, const char *value, Error **errp) bmc->uart_chosen = val +ASPEED_DEV_UART0 ; } +static char *aspeed_get_otpmem (Object *obj,Error **errp) +{ +AspeedMachineState *bmc =ASPEED_MACHINE (obj); + +return g_strdup (bmc->otpmem); +} + +static void aspeed_set_otpmem (Object *obj,const char *value,Error **errp) +{ +AspeedMachineState *bmc =ASPEED_MACHINE (obj); + +g_free (bmc->otpmem); + bmc->otpmem =g_strdup (value); +} +static void aspeed_machine_class_props_init (ObjectClass *oc) { object_class_property_add_bool(oc, "execute-in-place",
@@ -1220,6 +1236,10 @@ static void aspeed_machine_class_props_init(ObjectClass *oc) aspeed_set_spi_model);object_class_property_set_description (oc,"spi-model" ,"Change the SPI Flash model" ); +object_class_property_add_str (oc,"otpmem" , aspeed_get_otpmem, + aspeed_set_otpmem); +object_class_property_set_description (oc,"otpmem" , +"Set OTP Memory type" ); }static void aspeed_machine_class_init_cpus_defaults (MachineClass *mc)
hw/arm/aspeed_ast10x0.c +1 -1
@@ -154,7 +154,7 @@ static void aspeed_soc_ast1030_init(Object *obj)object_initialize_child (obj,"peci" , &s->peci,TYPE_ASPEED_PECI ); -object_initialize_child (obj,"sbc" , &s->sbc,TYPE_ASPEED_SBC ); +object_initialize_child (obj,"sbc" , &s->sbc,TYPE_ASPEED_AST10X0_SBC );for (i =0 ; i < sc->wdts_num; i++) {snprintf (typename,sizeof (typename),"aspeed.wdt-%s" , socname);
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 +18 -0
@@ -341,8 +341,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 = {
@@ -351,9 +353,25 @@ static const TypeInfo aspeed_ast2600_sbc_info = { .class_init = aspeed_ast2600_sbc_class_init, }; +static void aspeed_ast10x0_sbc_class_init (ObjectClass *klass,const void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); +AspeedSBCClass *sc =ASPEED_SBC_CLASS (klass); + + dc->desc ="AST10X0 Secure Boot Controller" ; + sc->has_otpmem = true; +} + +static const TypeInfo aspeed_ast10x0_sbc_info = { + .name =TYPE_ASPEED_AST10X0_SBC , + .parent =TYPE_ASPEED_SBC , + .class_init = aspeed_ast10x0_sbc_class_init, +}; +static void aspeed_sbc_register_types (void ) {type_register_static (&aspeed_ast2600_sbc_info); +type_register_static (&aspeed_ast10x0_sbc_info);type_register_static (&aspeed_sbc_info); }
include/hw/misc/aspeed_sbc.h +1 -0
@@ -14,6 +14,7 @@#define TYPE_ASPEED_SBC "aspeed.sbc"#define TYPE_ASPEED_AST2600_SBC TYPE_ASPEED_SBC "-ast2600" +#define TYPE_ASPEED_AST10X0_SBC TYPE_ASPEED_SBC "-ast10X0" OBJECT_DECLARE_TYPE(AspeedSBCState, AspeedSBCClass, ASPEED_SBC)#define ASPEED_SBC_NR_REGS (0x93c >> 2)
Signature · 1 line
2.43.0
Quoted 7 lines · Kane Chen
From: Kane-Chen-AS <[email protected]>
Dear reviewers,
This series introduces the ASPEED OTP (One-Time Programmable) memory model and connects it to the SBC controller and AST10x0/AST2600 SoCs.
Quoted 7 lines
The OTP model supports irreversible bit programming, file-backed content, and tracepoints for program conflict debugging.
Patch 2 links the OTP device to the SBC controller and enables command handling. Patch 3 exposes the machine parameter and updates the SoC tree.
Any feedback or suggestions are appreciated!
We are making progress. Here are some suggestions to clarify the model.
First, implement an AspeedOTPMemState model with "drive" and "size" properties. Use a local address space and an associated memory region with mem ops to generate transactions on the drive backend. If the backend is not defined, use an empty buffer instead. See the m25p80 or pnv_pnor models for an example. Please get rid of AspeedOTPMemOps.
Then, add a AspeedOTPMemState child under AspeedSBCState depending on the value of AspeedSBCClass::has_optmem. Implement the HW interface to generate transactions on AspeedOTPMemState. Add an AspeedSBCState 'optmem' alias property pointing to the drive property of AspeedOTPMemState.
Last the machine, get the correct SBC model name. Add a machine 'optmem' alias property pointing to the 'optmem' property of AspeedSBCState.
Let's not forget a functional test to do :
qemu-system-arm -M ast2600-evb,otpmem=otpmem-drive \
-blockdev node-name=otpmem-drive,driver=file,filename=/path/to/otpmem.img \
...Thanks,
C.