[PATCH v4 5/5] hw/misc/aspeed_sbc: Add machine parameter to alias OTP drive property

Kane Chen via posted 5 patches 1 year, 2 months ago
Only 3 patches received!
[PATCH v4 5/5] hw/misc/aspeed_sbc: Add machine parameter to alias OTP drive property
Posted by Kane Chen via 1 year, 2 months ago
From: Kane-Chen-AS <kane_chen@aspeedtech.com>

This patch adds a new machine parameter `otpmem` which creates a QOM
property alias on the aspeed_sbc device for the OTP drive.

Example usage:

  ./qemu-system-arm \
    -machine ast2600-evb,otpmem=otp-drive \
    -blockdev driver=file,filename=otpmem.img,node-name=otp \
    -global aspeed-otp.drive=otp \
    ...

With this change, the specified alias name (e.g. "otp-drive") becomes
available on the QOM path `/machine/soc/sbc/otp-drive`.

Signed-off-by: Kane-Chen-AS <kane_chen@aspeedtech.com>
---
 hw/arm/aspeed.c      | 20 ++++++++++++++++++++
 hw/misc/aspeed_sbc.c |  8 ++++++++
 2 files changed, 28 insertions(+)

diff --git a/hw/arm/aspeed.c b/hw/arm/aspeed.c
index c31bbe7701..8ec32369a6 100644
--- a/hw/arm/aspeed.c
+++ b/hw/arm/aspeed.c
@@ -48,6 +48,7 @@ struct AspeedMachineState {
     uint32_t uart_chosen;
     char *fmc_model;
     char *spi_model;
+    char *otpmem;
     uint32_t hw_strap1;
 };
 
@@ -1424,6 +1425,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",
@@ -1445,6 +1461,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)
diff --git a/hw/misc/aspeed_sbc.c b/hw/misc/aspeed_sbc.c
index b56a8b7678..b82c5e37cc 100644
--- a/hw/misc/aspeed_sbc.c
+++ b/hw/misc/aspeed_sbc.c
@@ -209,10 +209,18 @@ static void aspeed_sbc_instance_init(Object *obj)
 {
     AspeedSBCClass *sc = ASPEED_SBC_GET_CLASS(obj);
     AspeedSBCState *s = ASPEED_SBC(obj);
+    char *otpname;
 
     if (sc->has_otp) {
         object_initialize_child(OBJECT(s), "otp", &s->otp,
                                 TYPE_ASPEED_OTP);
+        otpname = object_property_get_str(qdev_get_machine(),
+                                          "otpmem",
+                                          &error_abort);
+        if (strlen(otpname)) {
+            object_property_add_alias(obj, otpname,
+                                      OBJECT(&s->otp), "drive");
+        }
     }
 }
 
-- 
2.43.0
Re: [SPAM] [PATCH v4 5/5] hw/misc/aspeed_sbc: Add machine parameter to alias OTP drive property
Posted by Cédric Le Goater 1 year, 1 month ago
On 7/8/25 07:57, Kane Chen wrote:
> From: Kane-Chen-AS <kane_chen@aspeedtech.com>
> 
> This patch adds a new machine parameter `otpmem` which creates a QOM
> property alias on the aspeed_sbc device for the OTP drive.
> 
> Example usage:
> 
>    ./qemu-system-arm \
>      -machine ast2600-evb,otpmem=otp-drive \
>      -blockdev driver=file,filename=otpmem.img,node-name=otp \
>      -global aspeed-otp.drive=otp \
>      ...
> 
> With this change, the specified alias name (e.g. "otp-drive") becomes
> available on the QOM path `/machine/soc/sbc/otp-drive`.
> 
> Signed-off-by: Kane-Chen-AS <kane_chen@aspeedtech.com>
> ---
>   hw/arm/aspeed.c      | 20 ++++++++++++++++++++
>   hw/misc/aspeed_sbc.c |  8 ++++++++
>   2 files changed, 28 insertions(+)
> 
> diff --git a/hw/arm/aspeed.c b/hw/arm/aspeed.c
> index c31bbe7701..8ec32369a6 100644
> --- a/hw/arm/aspeed.c
> +++ b/hw/arm/aspeed.c
> @@ -48,6 +48,7 @@ struct AspeedMachineState {
>       uint32_t uart_chosen;
>       char *fmc_model;
>       char *spi_model;
> +    char *otpmem;
>       uint32_t hw_strap1;
>   };
>   
> @@ -1424,6 +1425,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",
> @@ -1445,6 +1461,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)
> diff --git a/hw/misc/aspeed_sbc.c b/hw/misc/aspeed_sbc.c
> index b56a8b7678..b82c5e37cc 100644
> --- a/hw/misc/aspeed_sbc.c
> +++ b/hw/misc/aspeed_sbc.c
> @@ -209,10 +209,18 @@ static void aspeed_sbc_instance_init(Object *obj)
>   {
>       AspeedSBCClass *sc = ASPEED_SBC_GET_CLASS(obj);
>       AspeedSBCState *s = ASPEED_SBC(obj);
> +    char *otpname;
>   
>       if (sc->has_otp) {
>           object_initialize_child(OBJECT(s), "otp", &s->otp,
>                                   TYPE_ASPEED_OTP);
> +        otpname = object_property_get_str(qdev_get_machine(),
> +                                          "otpmem",> +                                          &error_abort);

This is a hack and I would prefer to prevent device models from
accessing directly the machine.

It would have been nice to have a machine option, but since the
user can specify a file for the OTP backend from the command line,
let's leave it as is.


Thanks,

C.



> +        if (strlen(otpname)) {
> +            object_property_add_alias(obj, otpname,
> +                                      OBJECT(&s->otp), "drive");
> +        }
>       }
>   }
>