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/qdict.h"
14 #include "qapi/qobject-input-visitor.h"
15 #include "qom/qom-qobject.h"
16 
17 static void loongarch_cpu_add_definition(gpointer data, gpointer user_data)
18 {
19     ObjectClass *oc = data;
20     CpuDefinitionInfoList **cpu_list = user_data;
21     CpuDefinitionInfo *info = g_new0(CpuDefinitionInfo, 1);
22     const char *typename = object_class_get_name(oc);
23 
24     info->name = cpu_model_from_type(typename);
25     info->q_typename = g_strdup(typename);
26 
27     QAPI_LIST_PREPEND(*cpu_list, info);
28 }
29 
30 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
31 {
32     CpuDefinitionInfoList *cpu_list = NULL;
33     GSList *list;
34 
35     list = object_class_get_list(TYPE_LOONGARCH_CPU, false);
36     g_slist_foreach(list, loongarch_cpu_add_definition, &cpu_list);
37     g_slist_free(list);
38 
39     return cpu_list;
40 }
41 
42 static const char *cpu_model_advertised_features[] = {
43     "lsx", "lasx", "lbt", "pmu", NULL
44 };
45 
46 CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
47                                                      CpuModelInfo *model,
48                                                      Error **errp)
49 {
50     Visitor *visitor;
51     bool ok;
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     if (model->props) {
65         visitor = qobject_input_visitor_new(model->props);
66         if (!visit_start_struct(visitor, "model.props", NULL, 0, errp)) {
67             visit_free(visitor);
68             return NULL;
69         }
70 
71         ok = visit_check_struct(visitor, errp);
72         visit_end_struct(visitor, NULL);
73         visit_free(visitor);
74         if (!ok) {
75             return NULL;
76         }
77     }
78 
79     oc = cpu_class_by_name(TYPE_LOONGARCH_CPU, model->name);
80     if (!oc) {
81         error_setg(errp, "The CPU type '%s' is not a recognized LoongArch CPU type",
82                    model->name);
83         return NULL;
84     }
85 
86     obj = object_new(object_class_get_name(oc));
87 
88     expansion_info = g_new0(CpuModelExpansionInfo, 1);
89     expansion_info->model = g_malloc0(sizeof(*expansion_info->model));
90     expansion_info->model->name = g_strdup(model->name);
91 
92     qdict_out = qdict_new();
93 
94     i = 0;
95     while ((name = cpu_model_advertised_features[i++]) != NULL) {
96         ObjectProperty *prop = object_property_find(obj, name);
97         if (prop) {
98             QObject *value;
99 
100             assert(prop->get);
101             value = object_property_get_qobject(obj, name, &error_abort);
102 
103             qdict_put_obj(qdict_out, name, value);
104         }
105     }
106 
107     if (!qdict_size(qdict_out)) {
108         qobject_unref(qdict_out);
109     } else {
110         expansion_info->model->props = QOBJECT(qdict_out);
111     }
112 
113     object_unref(obj);
114 
115     return expansion_info;
116 }
117