2 * QEMU CPU QMP commands for RISC-V
4 * Copyright (c) 2023 Ventana Micro Systems Inc.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
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"
39 static void riscv_cpu_add_definition(gpointer data
, gpointer user_data
)
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
;
47 info
->name
= cpu_model_from_type(typename
);
48 info
->q_typename
= g_strdup(typename
);
50 dyn_class
= object_class_dynamic_cast(oc
, TYPE_RISCV_DYNAMIC_CPU
);
51 info
->q_static
= dyn_class
== NULL
;
53 QAPI_LIST_PREPEND(*cpu_list
, info
);
56 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
58 CpuDefinitionInfoList
*cpu_list
= NULL
;
59 GSList
*list
= object_class_get_list(TYPE_RISCV_CPU
, false);
61 g_slist_foreach(list
, riscv_cpu_add_definition
, &cpu_list
);
67 static void riscv_check_if_cpu_available(RISCVCPU
*cpu
, Error
**errp
)
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";
73 error_setg(errp
, "'%s' CPU not available with %s", name
, accel
);
78 static void riscv_obj_add_qdict_prop(Object
*obj
, QDict
*qdict_out
,
81 ObjectProperty
*prop
= object_property_find(obj
, name
);
87 value
= object_property_get_qobject(obj
, name
, &error_abort
);
89 qdict_put_obj(qdict_out
, name
, value
);
93 static void riscv_obj_add_multiext_props(Object
*obj
, QDict
*qdict_out
,
94 const RISCVCPUMultiExtConfig
*arr
)
96 for (int i
= 0; arr
[i
].name
!= NULL
; i
++) {
97 riscv_obj_add_qdict_prop(obj
, qdict_out
, arr
[i
].name
);
101 static void riscv_cpuobj_validate_qdict_in(Object
*obj
, QObject
*props
,
102 const QDict
*qdict_in
,
105 const QDictEntry
*qe
;
107 Error
*local_err
= NULL
;
109 visitor
= qobject_input_visitor_new(props
);
110 if (!visit_start_struct(visitor
, NULL
, NULL
, 0, &local_err
)) {
114 for (qe
= qdict_first(qdict_in
); qe
; qe
= qdict_next(qdict_in
, qe
)) {
115 object_property_find_err(obj
, qe
->key
, &local_err
);
120 object_property_set(obj
, qe
->key
, visitor
, &local_err
);
126 visit_check_struct(visitor
, &local_err
);
131 riscv_cpu_finalize_features(RISCV_CPU(obj
), &local_err
);
136 visit_end_struct(visitor
, NULL
);
139 error_propagate(errp
, local_err
);
143 CpuModelExpansionInfo
*qmp_query_cpu_model_expansion(CpuModelExpansionType type
,
147 CpuModelExpansionInfo
*expansion_info
;
148 const QDict
*qdict_in
= NULL
;
152 Error
*local_err
= NULL
;
154 if (type
!= CPU_MODEL_EXPANSION_TYPE_FULL
) {
155 error_setg(errp
, "The requested expansion type is not supported");
159 oc
= cpu_class_by_name(TYPE_RISCV_CPU
, model
->name
);
161 error_setg(errp
, "The CPU type '%s' is not a known RISC-V CPU type",
167 qdict_in
= qobject_to(QDict
, model
->props
);
169 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
174 obj
= object_new(object_class_get_name(oc
));
176 riscv_check_if_cpu_available(RISCV_CPU(obj
), &local_err
);
177 if (local_err
!= NULL
) {
178 error_propagate(errp
, local_err
);
184 riscv_cpuobj_validate_qdict_in(obj
, model
->props
, qdict_in
,
187 error_propagate(errp
, local_err
);
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
);
197 qdict_out
= qdict_new();
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
);
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");
207 if (!qdict_size(qdict_out
)) {
208 qobject_unref(qdict_out
);
210 expansion_info
->model
->props
= QOBJECT(qdict_out
);
215 return expansion_info
;