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 = cpu_model_from_type(typename);
26     info->q_typename = g_strdup(typename);
27 
28     QAPI_LIST_PREPEND(*cpu_list, info);
29 }
30 
31 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
32 {
33     CpuDefinitionInfoList *cpu_list = NULL;
34     GSList *list;
35 
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);
39 
40     return cpu_list;
41 }
42 
43 static const char *cpu_model_advertised_features[] = {
44     "lsx", "lasx", NULL
45 };
46 
47 CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
48                                                      CpuModelInfo *model,
49                                                      Error **errp)
50 {
51     CpuModelExpansionInfo *expansion_info;
52     QDict *qdict_out;
53     ObjectClass *oc;
54     Object *obj;
55     const char *name;
56     int i;
57 
58     if (type != CPU_MODEL_EXPANSION_TYPE_STATIC) {
59         error_setg(errp, "The requested expansion type is not supported");
60         return NULL;
61     }
62 
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;
68     }
69 
70     obj = object_new(object_class_get_name(oc));
71 
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);
75 
76     qdict_out = qdict_new();
77 
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;
83 
84             assert(prop->get);
85             value = object_property_get_qobject(obj, name, &error_abort);
86 
87             qdict_put_obj(qdict_out, name, value);
88         }
89     }
90 
91     if (!qdict_size(qdict_out)) {
92         qobject_unref(qdict_out);
93     } else {
94         expansion_info->model->props = QOBJECT(qdict_out);
95     }
96 
97     object_unref(obj);
98 
99     return expansion_info;
100 }
101