1 /*
2  * QEMU LoongArch CPU (monitor definitions)
3  *
4  * SPDX-FileCopyrightText: 2021 Loongson Technology Corporation Limited
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
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"
17 
18 static void loongarch_cpu_add_definition(gpointer data, gpointer user_data)
19 {
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);
24 
25     info->name = g_strndup(typename,
26                            strlen(typename) - strlen("-" TYPE_LOONGARCH_CPU));
27     info->q_typename = g_strdup(typename);
28 
29     QAPI_LIST_PREPEND(*cpu_list, info);
30 }
31 
32 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
33 {
34     CpuDefinitionInfoList *cpu_list = NULL;
35     GSList *list;
36 
37     list = object_class_get_list(TYPE_LOONGARCH_CPU, false);
38     g_slist_foreach(list, loongarch_cpu_add_definition, &cpu_list);
39     g_slist_free(list);
40 
41     return cpu_list;
42 }
43 
44 static const char *cpu_model_advertised_features[] = {
45     "lsx", "lasx", NULL
46 };
47 
48 CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
49                                                      CpuModelInfo *model,
50                                                      Error **errp)
51 {
52     CpuModelExpansionInfo *expansion_info;
53     QDict *qdict_out;
54     ObjectClass *oc;
55     Object *obj;
56     const char *name;
57     int i;
58 
59     if (type != CPU_MODEL_EXPANSION_TYPE_STATIC) {
60         error_setg(errp, "The requested expansion type is not supported");
61         return NULL;
62     }
63 
64     oc = cpu_class_by_name(TYPE_LOONGARCH_CPU, model->name);
65     if (!oc) {
66         error_setg(errp, "The CPU type '%s' is not a recognized LoongArch CPU type",
67                    model->name);
68         return NULL;
69     }
70 
71     obj = object_new(object_class_get_name(oc));
72 
73     expansion_info = g_new0(CpuModelExpansionInfo, 1);
74     expansion_info->model = g_malloc0(sizeof(*expansion_info->model));
75     expansion_info->model->name = g_strdup(model->name);
76 
77     qdict_out = qdict_new();
78 
79     i = 0;
80     while ((name = cpu_model_advertised_features[i++]) != NULL) {
81         ObjectProperty *prop = object_property_find(obj, name);
82         if (prop) {
83             QObject *value;
84 
85             assert(prop->get);
86             value = object_property_get_qobject(obj, name, &error_abort);
87 
88             qdict_put_obj(qdict_out, name, value);
89         }
90     }
91 
92     if (!qdict_size(qdict_out)) {
93         qobject_unref(qdict_out);
94     } else {
95         expansion_info->model->props = QOBJECT(qdict_out);
96     }
97 
98     object_unref(obj);
99 
100     return expansion_info;
101 }
102