xref: /openbmc/qemu/target/arm/arm-qmp-cmds.c (revision e452053097371880910c744a5d42ae2df058a4a7)
1 /*
2  * QEMU monitor.c for ARM.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "qemu/osdep.h"
24 #include "qemu/target-info.h"
25 #include "hw/boards.h"
26 #include "kvm_arm.h"
27 #include "qapi/error.h"
28 #include "qapi/visitor.h"
29 #include "qapi/qobject-input-visitor.h"
30 #include "qapi/qapi-commands-machine.h"
31 #include "qapi/qapi-commands-misc-arm.h"
32 #include "qobject/qdict.h"
33 #include "qom/qom-qobject.h"
34 #include "cpu.h"
35 
gic_cap_new(int version)36 static GICCapability *gic_cap_new(int version)
37 {
38     GICCapability *cap = g_new0(GICCapability, 1);
39     cap->version = version;
40     /* by default, support none */
41     cap->emulated = false;
42     cap->kernel = false;
43     return cap;
44 }
45 
gic_cap_kvm_probe(GICCapability * v2,GICCapability * v3)46 static inline void gic_cap_kvm_probe(GICCapability *v2, GICCapability *v3)
47 {
48 #ifdef CONFIG_KVM
49     int fdarray[3];
50 
51     if (!kvm_arm_create_scratch_host_vcpu(fdarray, NULL)) {
52         return;
53     }
54 
55     /* Test KVM GICv2 */
56     if (kvm_device_supported(fdarray[1], KVM_DEV_TYPE_ARM_VGIC_V2)) {
57         v2->kernel = true;
58     }
59 
60     /* Test KVM GICv3 */
61     if (kvm_device_supported(fdarray[1], KVM_DEV_TYPE_ARM_VGIC_V3)) {
62         v3->kernel = true;
63     }
64 
65     kvm_arm_destroy_scratch_host_vcpu(fdarray);
66 #endif
67 }
68 
qmp_query_gic_capabilities(Error ** errp)69 GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
70 {
71     GICCapabilityList *head = NULL;
72     GICCapability *v2 = gic_cap_new(2), *v3 = gic_cap_new(3);
73 
74     v2->emulated = true;
75     v3->emulated = true;
76 
77     gic_cap_kvm_probe(v2, v3);
78 
79     QAPI_LIST_PREPEND(head, v2);
80     QAPI_LIST_PREPEND(head, v3);
81 
82     return head;
83 }
84 
85 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
86 
87 /*
88  * These are cpu model features we want to advertise. The order here
89  * matters as this is the order in which qmp_query_cpu_model_expansion
90  * will attempt to set them. If there are dependencies between features,
91  * then the order that considers those dependencies must be used.
92  */
93 static const char *cpu_model_advertised_features[] = {
94     "aarch64", "pmu", "sve",
95     "sve128", "sve256", "sve384", "sve512",
96     "sve640", "sve768", "sve896", "sve1024", "sve1152", "sve1280",
97     "sve1408", "sve1536", "sve1664", "sve1792", "sve1920", "sve2048",
98     "kvm-no-adjvtime", "kvm-steal-time",
99     "pauth", "pauth-impdef", "pauth-qarma3", "pauth-qarma5",
100     NULL
101 };
102 
qmp_query_cpu_model_expansion(CpuModelExpansionType type,CpuModelInfo * model,Error ** errp)103 CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
104                                                      CpuModelInfo *model,
105                                                      Error **errp)
106 {
107     CpuModelExpansionInfo *expansion_info;
108     const QDict *qdict_in;
109     QDict *qdict_out;
110     ObjectClass *oc;
111     Object *obj;
112     const char *name;
113     int i;
114 
115     if (type != CPU_MODEL_EXPANSION_TYPE_FULL) {
116         error_setg(errp, "The requested expansion type is not supported");
117         return NULL;
118     }
119 
120     if (!kvm_enabled() && !strcmp(model->name, "host")) {
121         error_setg(errp, "The CPU type '%s' requires KVM", model->name);
122         return NULL;
123     }
124 
125     oc = cpu_class_by_name(TYPE_ARM_CPU, model->name);
126     if (!oc) {
127         error_setg(errp, "The CPU type '%s' is not a recognized ARM CPU type",
128                    model->name);
129         return NULL;
130     }
131 
132     if (kvm_enabled()) {
133         bool supported = false;
134 
135         if (!strcmp(model->name, "host") || !strcmp(model->name, "max")) {
136             /* These are kvmarm's recommended cpu types */
137             supported = true;
138         } else if (current_machine->cpu_type) {
139             const char *cpu_type = current_machine->cpu_type;
140             int len = strlen(cpu_type) - strlen(ARM_CPU_TYPE_SUFFIX);
141 
142             if (strlen(model->name) == len &&
143                 !strncmp(model->name, cpu_type, len)) {
144                 /* KVM is enabled and we're using this type, so it works. */
145                 supported = true;
146             }
147         }
148         if (!supported) {
149             error_setg(errp, "We cannot guarantee the CPU type '%s' works "
150                              "with KVM on this host", model->name);
151             return NULL;
152         }
153     }
154 
155     obj = object_new(object_class_get_name(oc));
156 
157     if (model->props) {
158         Visitor *visitor;
159         Error *err = NULL;
160 
161         visitor = qobject_input_visitor_new(model->props);
162         if (!visit_start_struct(visitor, "model.props", NULL, 0, errp)) {
163             visit_free(visitor);
164             object_unref(obj);
165             return NULL;
166         }
167 
168         qdict_in = qobject_to(QDict, model->props);
169         i = 0;
170         while ((name = cpu_model_advertised_features[i++]) != NULL) {
171             if (qdict_get(qdict_in, name)) {
172                 if (!object_property_set(obj, name, visitor, &err)) {
173                     break;
174                 }
175             }
176         }
177 
178         if (!err) {
179             visit_check_struct(visitor, &err);
180         }
181         if (!err) {
182             arm_cpu_finalize_features(ARM_CPU(obj), &err);
183         }
184         visit_end_struct(visitor, NULL);
185         visit_free(visitor);
186         if (err) {
187             object_unref(obj);
188             error_propagate(errp, err);
189             return NULL;
190         }
191     } else {
192         arm_cpu_finalize_features(ARM_CPU(obj), &error_abort);
193     }
194 
195     expansion_info = g_new0(CpuModelExpansionInfo, 1);
196     expansion_info->model = g_malloc0(sizeof(*expansion_info->model));
197     expansion_info->model->name = g_strdup(model->name);
198 
199     qdict_out = qdict_new();
200 
201     i = 0;
202     while ((name = cpu_model_advertised_features[i++]) != NULL) {
203         ObjectProperty *prop = object_property_find(obj, name);
204         if (prop) {
205             QObject *value;
206 
207             assert(prop->get);
208             value = object_property_get_qobject(obj, name, &error_abort);
209 
210             qdict_put_obj(qdict_out, name, value);
211         }
212     }
213 
214     if (!qdict_size(qdict_out)) {
215         qobject_unref(qdict_out);
216     } else {
217         expansion_info->model->props = QOBJECT(qdict_out);
218     }
219 
220     object_unref(obj);
221 
222     return expansion_info;
223 }
224 
arm_cpu_add_definition(gpointer data,gpointer user_data)225 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
226 {
227     ObjectClass *oc = data;
228     CpuDefinitionInfoList **cpu_list = user_data;
229     CpuDefinitionInfo *info;
230     const char *typename;
231 
232     typename = object_class_get_name(oc);
233     info = g_malloc0(sizeof(*info));
234     info->name = cpu_model_from_type(typename);
235     info->q_typename = g_strdup(typename);
236 
237     QAPI_LIST_PREPEND(*cpu_list, info);
238 }
239 
qmp_query_cpu_definitions(Error ** errp)240 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
241 {
242     CpuDefinitionInfoList *cpu_list = NULL;
243     GSList *list;
244 
245     list = object_class_get_list(target_cpu_type(), false);
246     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
247     g_slist_free(list);
248 
249     return cpu_list;
250 }
251