xref: /openbmc/qemu/target/riscv/riscv-qmp-cmds.c (revision 7b571ef3)
1 /*
2  * QEMU CPU QMP commands for RISC-V
3  *
4  * Copyright (c) 2023 Ventana Micro Systems Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 
27 #include "qapi/error.h"
28 #include "qapi/qapi-commands-machine-target.h"
29 #include "qapi/qmp/qdict.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qapi/qobject-input-visitor.h"
32 #include "qapi/visitor.h"
33 #include "qom/qom-qobject.h"
34 #include "sysemu/kvm.h"
35 #include "sysemu/tcg.h"
36 #include "cpu-qom.h"
37 #include "cpu.h"
38 
39 static void riscv_cpu_add_definition(gpointer data, gpointer user_data)
40 {
41     ObjectClass *oc = data;
42     CpuDefinitionInfoList **cpu_list = user_data;
43     CpuDefinitionInfo *info = g_malloc0(sizeof(*info));
44     const char *typename = object_class_get_name(oc);
45     ObjectClass *dyn_class;
46 
47     info->name = cpu_model_from_type(typename);
48     info->q_typename = g_strdup(typename);
49 
50     dyn_class = object_class_dynamic_cast(oc, TYPE_RISCV_DYNAMIC_CPU);
51     info->q_static = dyn_class == NULL;
52 
53     QAPI_LIST_PREPEND(*cpu_list, info);
54 }
55 
56 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
57 {
58     CpuDefinitionInfoList *cpu_list = NULL;
59     GSList *list = object_class_get_list(TYPE_RISCV_CPU, false);
60 
61     g_slist_foreach(list, riscv_cpu_add_definition, &cpu_list);
62     g_slist_free(list);
63 
64     return cpu_list;
65 }
66 
67 static void riscv_check_if_cpu_available(RISCVCPU *cpu, Error **errp)
68 {
69     if (!riscv_cpu_accelerator_compatible(cpu)) {
70         g_autofree char *name = riscv_cpu_get_name(cpu);
71         const char *accel = kvm_enabled() ? "kvm" : "tcg";
72 
73         error_setg(errp, "'%s' CPU not available with %s", name, accel);
74         return;
75     }
76 }
77 
78 static void riscv_obj_add_qdict_prop(Object *obj, QDict *qdict_out,
79                                      const char *name)
80 {
81     ObjectProperty *prop = object_property_find(obj, name);
82 
83     if (prop) {
84         QObject *value;
85 
86         assert(prop->get);
87         value = object_property_get_qobject(obj, name, &error_abort);
88 
89         qdict_put_obj(qdict_out, name, value);
90     }
91 }
92 
93 static void riscv_obj_add_multiext_props(Object *obj, QDict *qdict_out,
94                                          const RISCVCPUMultiExtConfig *arr)
95 {
96     for (int i = 0; arr[i].name != NULL; i++) {
97         riscv_obj_add_qdict_prop(obj, qdict_out, arr[i].name);
98     }
99 }
100 
101 static void riscv_cpuobj_validate_qdict_in(Object *obj, QObject *props,
102                                            const QDict *qdict_in,
103                                            Error **errp)
104 {
105     const QDictEntry *qe;
106     Visitor *visitor;
107     Error *local_err = NULL;
108 
109     visitor = qobject_input_visitor_new(props);
110     if (!visit_start_struct(visitor, NULL, NULL, 0, &local_err)) {
111         goto err;
112     }
113 
114     for (qe = qdict_first(qdict_in); qe; qe = qdict_next(qdict_in, qe)) {
115         object_property_find_err(obj, qe->key, &local_err);
116         if (local_err) {
117             goto err;
118         }
119 
120         object_property_set(obj, qe->key, visitor, &local_err);
121         if (local_err) {
122             goto err;
123         }
124     }
125 
126     visit_check_struct(visitor, &local_err);
127     if (local_err) {
128         goto err;
129     }
130 
131     riscv_cpu_finalize_features(RISCV_CPU(obj), &local_err);
132     if (local_err) {
133         goto err;
134     }
135 
136     visit_end_struct(visitor, NULL);
137 
138 err:
139     error_propagate(errp, local_err);
140     visit_free(visitor);
141 }
142 
143 CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
144                                                      CpuModelInfo *model,
145                                                      Error **errp)
146 {
147     CpuModelExpansionInfo *expansion_info;
148     const QDict *qdict_in = NULL;
149     QDict *qdict_out;
150     ObjectClass *oc;
151     Object *obj;
152     Error *local_err = NULL;
153 
154     if (type != CPU_MODEL_EXPANSION_TYPE_FULL) {
155         error_setg(errp, "The requested expansion type is not supported");
156         return NULL;
157     }
158 
159     oc = cpu_class_by_name(TYPE_RISCV_CPU, model->name);
160     if (!oc) {
161         error_setg(errp, "The CPU type '%s' is not a known RISC-V CPU type",
162                    model->name);
163         return NULL;
164     }
165 
166     if (model->props) {
167         qdict_in = qobject_to(QDict, model->props);
168         if (!qdict_in) {
169             error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
170             return NULL;
171         }
172     }
173 
174     obj = object_new(object_class_get_name(oc));
175 
176     riscv_check_if_cpu_available(RISCV_CPU(obj), &local_err);
177     if (local_err != NULL) {
178         error_propagate(errp, local_err);
179         object_unref(obj);
180         return NULL;
181     }
182 
183     if (qdict_in) {
184         riscv_cpuobj_validate_qdict_in(obj, model->props, qdict_in,
185                                        &local_err);
186         if (local_err) {
187             error_propagate(errp, local_err);
188             object_unref(obj);
189             return NULL;
190         }
191     }
192 
193     expansion_info = g_new0(CpuModelExpansionInfo, 1);
194     expansion_info->model = g_malloc0(sizeof(*expansion_info->model));
195     expansion_info->model->name = g_strdup(model->name);
196 
197     qdict_out = qdict_new();
198 
199     riscv_obj_add_multiext_props(obj, qdict_out, riscv_cpu_extensions);
200     riscv_obj_add_multiext_props(obj, qdict_out, riscv_cpu_experimental_exts);
201     riscv_obj_add_multiext_props(obj, qdict_out, riscv_cpu_vendor_exts);
202 
203     /* Add our CPU boolean options too */
204     riscv_obj_add_qdict_prop(obj, qdict_out, "mmu");
205     riscv_obj_add_qdict_prop(obj, qdict_out, "pmp");
206 
207     if (!qdict_size(qdict_out)) {
208         qobject_unref(qdict_out);
209     } else {
210         expansion_info->model->props = QOBJECT(qdict_out);
211     }
212 
213     object_unref(obj);
214 
215     return expansion_info;
216 }
217