target: Use generic cpu_model_from_type()
[qemu/ar7.git] / target / loongarch / loongarch-qmp-cmds.c
blobec33ce81f0eebe245838e5ccea9b9d12b6588135
1 /*
2 * QEMU LoongArch CPU (monitor definitions)
4 * SPDX-FileCopyrightText: 2021 Loongson Technology Corporation Limited
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
9 #include "qemu/osdep.h"
10 #include "qapi/error.h"
11 #include "qapi/qapi-commands-machine-target.h"
12 #include "cpu.h"
13 #include "qapi/qmp/qerror.h"
14 #include "qapi/qmp/qdict.h"
15 #include "qapi/qobject-input-visitor.h"
16 #include "qom/qom-qobject.h"
18 static void loongarch_cpu_add_definition(gpointer data, gpointer user_data)
20 ObjectClass *oc = data;
21 CpuDefinitionInfoList **cpu_list = user_data;
22 CpuDefinitionInfo *info = g_new0(CpuDefinitionInfo, 1);
23 const char *typename = object_class_get_name(oc);
25 info->name = cpu_model_from_type(typename);
26 info->q_typename = g_strdup(typename);
28 QAPI_LIST_PREPEND(*cpu_list, info);
31 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
33 CpuDefinitionInfoList *cpu_list = NULL;
34 GSList *list;
36 list = object_class_get_list(TYPE_LOONGARCH_CPU, false);
37 g_slist_foreach(list, loongarch_cpu_add_definition, &cpu_list);
38 g_slist_free(list);
40 return cpu_list;
43 static const char *cpu_model_advertised_features[] = {
44 "lsx", "lasx", NULL
47 CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
48 CpuModelInfo *model,
49 Error **errp)
51 CpuModelExpansionInfo *expansion_info;
52 QDict *qdict_out;
53 ObjectClass *oc;
54 Object *obj;
55 const char *name;
56 int i;
58 if (type != CPU_MODEL_EXPANSION_TYPE_STATIC) {
59 error_setg(errp, "The requested expansion type is not supported");
60 return NULL;
63 oc = cpu_class_by_name(TYPE_LOONGARCH_CPU, model->name);
64 if (!oc) {
65 error_setg(errp, "The CPU type '%s' is not a recognized LoongArch CPU type",
66 model->name);
67 return NULL;
70 obj = object_new(object_class_get_name(oc));
72 expansion_info = g_new0(CpuModelExpansionInfo, 1);
73 expansion_info->model = g_malloc0(sizeof(*expansion_info->model));
74 expansion_info->model->name = g_strdup(model->name);
76 qdict_out = qdict_new();
78 i = 0;
79 while ((name = cpu_model_advertised_features[i++]) != NULL) {
80 ObjectProperty *prop = object_property_find(obj, name);
81 if (prop) {
82 QObject *value;
84 assert(prop->get);
85 value = object_property_get_qobject(obj, name, &error_abort);
87 qdict_put_obj(qdict_out, name, value);
91 if (!qdict_size(qdict_out)) {
92 qobject_unref(qdict_out);
93 } else {
94 expansion_info->model->props = QOBJECT(qdict_out);
97 object_unref(obj);
99 return expansion_info;