[PATCH v1 3/3] target/loongarch: Implement query-cpu-model-expansion

Song Gao posted 3 patches 1 year, 1 month ago
Maintainers: Eric Blake <eblake@redhat.com>, Markus Armbruster <armbru@redhat.com>, Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Yanan Wang <wangyanan55@huawei.com>, Song Gao <gaosong@loongson.cn>
[PATCH v1 3/3] target/loongarch: Implement query-cpu-model-expansion
Posted by Song Gao 1 year, 1 month ago
Add support for the query-cpu-model-expansion QMP command to LoongArch.
We only support query the 'max' cpu features.

  e.g
    start with '-cpu max,lasx=off'

    (QEMU) query-cpu-model-expansion type=static  model={"name":"max"}
    {"return": {"model": {"name": "max", "props": {"lasx": false, "lsx": true}}}}

Signed-off-by: Song Gao <gaosong@loongson.cn>
---
 qapi/machine-target.json              |  6 ++-
 target/loongarch/loongarch-qmp-cmds.c | 64 +++++++++++++++++++++++++++
 2 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/qapi/machine-target.json b/qapi/machine-target.json
index f0a6b72414..752b18cced 100644
--- a/qapi/machine-target.json
+++ b/qapi/machine-target.json
@@ -228,7 +228,8 @@
   'data': { 'model': 'CpuModelInfo' },
   'if': { 'any': [ 'TARGET_S390X',
                    'TARGET_I386',
-                   'TARGET_ARM' ] } }
+                   'TARGET_ARM',
+                   'TARGET_LOONGARCH64' ] } }
 
 ##
 # @query-cpu-model-expansion:
@@ -273,7 +274,8 @@
   'returns': 'CpuModelExpansionInfo',
   'if': { 'any': [ 'TARGET_S390X',
                    'TARGET_I386',
-                   'TARGET_ARM' ] } }
+                   'TARGET_ARM',
+                   'TARGET_LOONGARCH64' ] } }
 
 ##
 # @CpuDefinitionInfo:
diff --git a/target/loongarch/loongarch-qmp-cmds.c b/target/loongarch/loongarch-qmp-cmds.c
index 6c25957881..645672ff59 100644
--- a/target/loongarch/loongarch-qmp-cmds.c
+++ b/target/loongarch/loongarch-qmp-cmds.c
@@ -7,8 +7,13 @@
  */
 
 #include "qemu/osdep.h"
+#include "qapi/error.h"
 #include "qapi/qapi-commands-machine-target.h"
 #include "cpu.h"
+#include "qapi/qmp/qerror.h"
+#include "qapi/qmp/qdict.h"
+#include "qapi/qobject-input-visitor.h"
+#include "qom/qom-qobject.h"
 
 static void loongarch_cpu_add_definition(gpointer data, gpointer user_data)
 {
@@ -35,3 +40,62 @@ CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
 
     return cpu_list;
 }
+
+static const char *cpu_model_advertised_features[] = {
+    "lsx", "lasx", NULL
+};
+
+CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
+                                                     CpuModelInfo *model,
+                                                     Error **errp)
+{
+    CpuModelExpansionInfo *expansion_info;
+    QDict *qdict_out;
+    ObjectClass *oc;
+    Object *obj;
+    const char *name;
+    int i;
+
+    if (type != CPU_MODEL_EXPANSION_TYPE_STATIC) {
+        error_setg(errp, "The requested expansion type is not supported");
+        return NULL;
+    }
+
+    oc = cpu_class_by_name(TYPE_LOONGARCH_CPU, model->name);
+    if (!oc) {
+        error_setg(errp, "The CPU type '%s' is not a recognized LoongArch CPU type",
+                   model->name);
+        return NULL;
+    }
+
+    obj = object_new(object_class_get_name(oc));
+
+    expansion_info = g_new0(CpuModelExpansionInfo, 1);
+    expansion_info->model = g_malloc0(sizeof(*expansion_info->model));
+    expansion_info->model->name = g_strdup(model->name);
+
+    qdict_out = qdict_new();
+
+    i = 0;
+    while ((name = cpu_model_advertised_features[i++]) != NULL) {
+        ObjectProperty *prop = object_property_find(obj, name);
+        if (prop) {
+            QObject *value;
+
+            assert(prop->get);
+            value = object_property_get_qobject(obj, name, &error_abort);
+
+            qdict_put_obj(qdict_out, name, value);
+        }
+    }
+
+    if (!qdict_size(qdict_out)) {
+        qobject_unref(qdict_out);
+    } else {
+        expansion_info->model->props = QOBJECT(qdict_out);
+    }
+
+    object_unref(obj);
+
+    return expansion_info;
+}
-- 
2.25.1
Re: [PATCH v1 3/3] target/loongarch: Implement query-cpu-model-expansion
Posted by Markus Armbruster 1 year, 1 month ago
Song Gao <gaosong@loongson.cn> writes:

> Add support for the query-cpu-model-expansion QMP command to LoongArch.
> We only support query the 'max' cpu features.
>
>   e.g
>     start with '-cpu max,lasx=off'
>
>     (QEMU) query-cpu-model-expansion type=static  model={"name":"max"}
>     {"return": {"model": {"name": "max", "props": {"lasx": false, "lsx": true}}}}

Suggest to show what happens when you try to query something else.

> Signed-off-by: Song Gao <gaosong@loongson.cn>
> ---
>  qapi/machine-target.json              |  6 ++-
>  target/loongarch/loongarch-qmp-cmds.c | 64 +++++++++++++++++++++++++++
>  2 files changed, 68 insertions(+), 2 deletions(-)
>
> diff --git a/qapi/machine-target.json b/qapi/machine-target.json
> index f0a6b72414..752b18cced 100644
> --- a/qapi/machine-target.json
> +++ b/qapi/machine-target.json
> @@ -228,7 +228,8 @@
>    'data': { 'model': 'CpuModelInfo' },
>    'if': { 'any': [ 'TARGET_S390X',
>                     'TARGET_I386',
> -                   'TARGET_ARM' ] } }
> +                   'TARGET_ARM',
> +                   'TARGET_LOONGARCH64' ] } }
>  
>  ##
>  # @query-cpu-model-expansion:
> @@ -273,7 +274,8 @@
>    'returns': 'CpuModelExpansionInfo',
>    'if': { 'any': [ 'TARGET_S390X',
>                     'TARGET_I386',
> -                   'TARGET_ARM' ] } }
> +                   'TARGET_ARM',
> +                   'TARGET_LOONGARCH64' ] } }
>  
>  ##
>  # @CpuDefinitionInfo:

QAPI schema
Acked-by: Markus Armbruster <armbru@redhat.com>

[...]