master
c 135 lines 3.74 KB
Raw
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 "qemu/target-info.h"
11 #include "qapi/error.h"
12 #include "qapi/qapi-commands-machine.h"
13 #include "cpu.h"
14 #include "qobject/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(target_cpu_type(), 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", "lbt", "pmu", "kvm-pv-ipi", "kvm-steal-time", "msgint",
45 "ptw", NULL
46 };
47
48 CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
49 CpuModelInfo *model,
50 Error **errp)
51 {
52 Visitor *visitor;
53 CpuModelExpansionInfo *expansion_info;
54 QDict *qdict_out;
55 ObjectClass *oc;
56 Object *obj;
57 const char *name;
58 int i;
59
60 if ((type != CPU_MODEL_EXPANSION_TYPE_STATIC) &&
61 (type != CPU_MODEL_EXPANSION_TYPE_FULL)) {
62 error_setg(errp, "The requested expansion type is not supported");
63 return NULL;
64 }
65
66 oc = cpu_class_by_name(TYPE_LOONGARCH_CPU, model->name);
67 if (!oc) {
68 error_setg(errp, "The CPU type '%s' is not a recognized LoongArch "
69 "CPU type", model->name);
70 return NULL;
71 }
72
73 obj = object_new(object_class_get_name(oc));
74 if (model->props) {
75 Error *err = NULL;
76 const QDict *qdict_in;
77
78 visitor = qobject_input_visitor_new(model->props);
79 if (!visit_start_struct(visitor, "model.props", NULL, 0, errp)) {
80 visit_free(visitor);
81 object_unref(obj);
82 return NULL;
83 }
84
85 qdict_in = qobject_to(QDict, model->props);
86 i = 0;
87 while ((name = cpu_model_advertised_features[i++]) != NULL) {
88 if (qdict_get(qdict_in, name)) {
89 if (!object_property_set(obj, name, visitor, &err)) {
90 break;
91 }
92 }
93 }
94
95 if (!err) {
96 visit_check_struct(visitor, &err);
97 }
98 visit_end_struct(visitor, NULL);
99 visit_free(visitor);
100 if (err) {
101 error_propagate(errp, err);
102 object_unref(obj);
103 return NULL;
104 }
105 }
106
107 expansion_info = g_new0(CpuModelExpansionInfo, 1);
108 expansion_info->model = g_malloc0(sizeof(*expansion_info->model));
109 expansion_info->model->name = g_strdup(model->name);
110
111 qdict_out = qdict_new();
112
113 i = 0;
114 while ((name = cpu_model_advertised_features[i++]) != NULL) {
115 ObjectProperty *prop = object_property_find(obj, name);
116 if (prop) {
117 QObject *value;
118
119 assert(prop->get);
120 value = object_property_get_qobject(obj, name, &error_abort);
121
122 qdict_put_obj(qdict_out, name, value);
123 }
124 }
125
126 if (!qdict_size(qdict_out)) {
127 qobject_unref(qdict_out);
128 } else {
129 expansion_info->model->props = QOBJECT(qdict_out);
130 }
131
132 object_unref(obj);
133
134 return expansion_info;
135 }