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/qbool.h"
30 #include "qapi/qmp/qdict.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/qobject-input-visitor.h"
33 #include "qapi/visitor.h"
34 #include "qom/qom-qobject.h"
35 #include "sysemu/kvm.h"
36 #include "sysemu/tcg.h"
40 static void riscv_cpu_add_definition(gpointer data
, gpointer user_data
)
42 ObjectClass
*oc
= data
;
43 CpuDefinitionInfoList
**cpu_list
= user_data
;
44 CpuDefinitionInfo
*info
= g_malloc0(sizeof(*info
));
45 const char *typename
= object_class_get_name(oc
);
46 ObjectClass
*dyn_class
;
48 info
->name
= cpu_model_from_type(typename
);
49 info
->q_typename
= g_strdup(typename
);
51 dyn_class
= object_class_dynamic_cast(oc
, TYPE_RISCV_DYNAMIC_CPU
);
52 info
->q_static
= dyn_class
== NULL
;
54 QAPI_LIST_PREPEND(*cpu_list
, info
);
57 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
59 CpuDefinitionInfoList
*cpu_list
= NULL
;
60 GSList
*list
= object_class_get_list(TYPE_RISCV_CPU
, false);
62 g_slist_foreach(list
, riscv_cpu_add_definition
, &cpu_list
);
68 static void riscv_check_if_cpu_available(RISCVCPU
*cpu
, Error
**errp
)
70 if (!riscv_cpu_accelerator_compatible(cpu
)) {
71 g_autofree
char *name
= riscv_cpu_get_name(cpu
);
72 const char *accel
= kvm_enabled() ? "kvm" : "tcg";
74 error_setg(errp
, "'%s' CPU not available with %s", name
, accel
);
79 static void riscv_obj_add_qdict_prop(Object
*obj
, QDict
*qdict_out
,
82 ObjectProperty
*prop
= object_property_find(obj
, name
);
88 value
= object_property_get_qobject(obj
, name
, &error_abort
);
90 qdict_put_obj(qdict_out
, name
, value
);
94 static void riscv_obj_add_multiext_props(Object
*obj
, QDict
*qdict_out
,
95 const RISCVCPUMultiExtConfig
*arr
)
97 for (int i
= 0; arr
[i
].name
!= NULL
; i
++) {
98 riscv_obj_add_qdict_prop(obj
, qdict_out
, arr
[i
].name
);
102 static void riscv_obj_add_named_feats_qdict(Object
*obj
, QDict
*qdict_out
)
104 const RISCVCPUMultiExtConfig
*named_cfg
;
105 RISCVCPU
*cpu
= RISCV_CPU(obj
);
109 for (int i
= 0; riscv_cpu_named_features
[i
].name
!= NULL
; i
++) {
110 named_cfg
= &riscv_cpu_named_features
[i
];
111 flag_val
= isa_ext_is_enabled(cpu
, named_cfg
->offset
);
112 value
= QOBJECT(qbool_from_bool(flag_val
));
114 qdict_put_obj(qdict_out
, named_cfg
->name
, value
);
118 static void riscv_obj_add_profiles_qdict(Object
*obj
, QDict
*qdict_out
)
120 RISCVCPUProfile
*profile
;
123 for (int i
= 0; riscv_profiles
[i
] != NULL
; i
++) {
124 profile
= riscv_profiles
[i
];
125 value
= QOBJECT(qbool_from_bool(profile
->enabled
));
127 qdict_put_obj(qdict_out
, profile
->name
, value
);
131 static void riscv_cpuobj_validate_qdict_in(Object
*obj
, QObject
*props
,
132 const QDict
*qdict_in
,
135 const QDictEntry
*qe
;
137 Error
*local_err
= NULL
;
139 visitor
= qobject_input_visitor_new(props
);
140 if (!visit_start_struct(visitor
, NULL
, NULL
, 0, &local_err
)) {
144 for (qe
= qdict_first(qdict_in
); qe
; qe
= qdict_next(qdict_in
, qe
)) {
145 object_property_find_err(obj
, qe
->key
, &local_err
);
150 object_property_set(obj
, qe
->key
, visitor
, &local_err
);
156 visit_check_struct(visitor
, &local_err
);
161 visit_end_struct(visitor
, NULL
);
164 error_propagate(errp
, local_err
);
168 CpuModelExpansionInfo
*qmp_query_cpu_model_expansion(CpuModelExpansionType type
,
172 CpuModelExpansionInfo
*expansion_info
;
173 const QDict
*qdict_in
= NULL
;
177 Error
*local_err
= NULL
;
179 if (type
!= CPU_MODEL_EXPANSION_TYPE_FULL
) {
180 error_setg(errp
, "The requested expansion type is not supported");
184 oc
= cpu_class_by_name(TYPE_RISCV_CPU
, model
->name
);
186 error_setg(errp
, "The CPU type '%s' is not a known RISC-V CPU type",
192 qdict_in
= qobject_to(QDict
, model
->props
);
194 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
199 obj
= object_new(object_class_get_name(oc
));
201 riscv_check_if_cpu_available(RISCV_CPU(obj
), &local_err
);
202 if (local_err
!= NULL
) {
203 error_propagate(errp
, local_err
);
209 riscv_cpuobj_validate_qdict_in(obj
, model
->props
, qdict_in
,
212 error_propagate(errp
, local_err
);
218 riscv_cpu_finalize_features(RISCV_CPU(obj
), &local_err
);
220 error_propagate(errp
, local_err
);
225 expansion_info
= g_new0(CpuModelExpansionInfo
, 1);
226 expansion_info
->model
= g_malloc0(sizeof(*expansion_info
->model
));
227 expansion_info
->model
->name
= g_strdup(model
->name
);
229 qdict_out
= qdict_new();
231 riscv_obj_add_multiext_props(obj
, qdict_out
, riscv_cpu_extensions
);
232 riscv_obj_add_multiext_props(obj
, qdict_out
, riscv_cpu_experimental_exts
);
233 riscv_obj_add_multiext_props(obj
, qdict_out
, riscv_cpu_vendor_exts
);
234 riscv_obj_add_named_feats_qdict(obj
, qdict_out
);
235 riscv_obj_add_profiles_qdict(obj
, qdict_out
);
237 /* Add our CPU boolean options too */
238 riscv_obj_add_qdict_prop(obj
, qdict_out
, "mmu");
239 riscv_obj_add_qdict_prop(obj
, qdict_out
, "pmp");
241 if (!qdict_size(qdict_out
)) {
242 qobject_unref(qdict_out
);
244 expansion_info
->model
->props
= QOBJECT(qdict_out
);
249 return expansion_info
;