]> Git Repo - qemu.git/blob - target-s390x/cpu_models.c
s390x/cpumodel: expose features and feature groups as properties
[qemu.git] / target-s390x / cpu_models.c
1 /*
2  * CPU models for s390x
3  *
4  * Copyright 2016 IBM Corp.
5  *
6  * Author(s): David Hildenbrand <[email protected]>
7  *
8  * This work is licensed under the terms of the GNU GPL, version 2 or (at
9  * your option) any later version. See the COPYING file in the top-level
10  * directory.
11  */
12
13 #include "qemu/osdep.h"
14 #include "cpu.h"
15 #include "gen-features.h"
16 #include "qapi/error.h"
17 #include "qapi/visitor.h"
18 #ifndef CONFIG_USER_ONLY
19 #include "sysemu/arch_init.h"
20 #endif
21
22 #define CPUDEF_INIT(_type, _gen, _ec_ga, _mha_pow, _hmfai, _name, _desc) \
23     {                                                                    \
24         .name = _name,                                                   \
25         .type = _type,                                                   \
26         .gen = _gen,                                                     \
27         .ec_ga = _ec_ga,                                                 \
28         .mha_pow = _mha_pow,                                             \
29         .hmfai = _hmfai,                                                 \
30         .desc = _desc,                                                   \
31         .base_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _BASE },  \
32         .default_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _DEFAULT },  \
33         .full_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _FULL },  \
34     }
35
36 /*
37  * CPU definiton list in order of release. For now, base features of a
38  * following release are always a subset of base features of the previous
39  * release. Same is correct for the other feature sets.
40  * A BC release always follows the corresponding EC release.
41  */
42 static S390CPUDef s390_cpu_defs[] = {
43     CPUDEF_INIT(0x2064, 7, 1, 38, 0x00000000U, "z900", "IBM zSeries 900 GA1"),
44     CPUDEF_INIT(0x2064, 7, 2, 38, 0x00000000U, "z900.2", "IBM zSeries 900 GA2"),
45     CPUDEF_INIT(0x2064, 7, 3, 38, 0x00000000U, "z900.3", "IBM zSeries 900 GA3"),
46     CPUDEF_INIT(0x2066, 7, 3, 38, 0x00000000U, "z800", "IBM zSeries 800 GA1"),
47     CPUDEF_INIT(0x2084, 8, 1, 38, 0x00000000U, "z990", "IBM zSeries 990 GA1"),
48     CPUDEF_INIT(0x2084, 8, 2, 38, 0x00000000U, "z990.2", "IBM zSeries 990 GA2"),
49     CPUDEF_INIT(0x2084, 8, 3, 38, 0x00000000U, "z990.3", "IBM zSeries 990 GA3"),
50     CPUDEF_INIT(0x2086, 8, 3, 38, 0x00000000U, "z890", "IBM zSeries 880 GA1"),
51     CPUDEF_INIT(0x2084, 8, 4, 38, 0x00000000U, "z990.4", "IBM zSeries 990 GA4"),
52     CPUDEF_INIT(0x2086, 8, 4, 38, 0x00000000U, "z890.2", "IBM zSeries 880 GA2"),
53     CPUDEF_INIT(0x2084, 8, 5, 38, 0x00000000U, "z990.5", "IBM zSeries 990 GA5"),
54     CPUDEF_INIT(0x2086, 8, 5, 38, 0x00000000U, "z890.3", "IBM zSeries 880 GA3"),
55     CPUDEF_INIT(0x2094, 9, 1, 40, 0x00000000U, "z9EC", "IBM System z9 EC GA1"),
56     CPUDEF_INIT(0x2094, 9, 2, 40, 0x00000000U, "z9EC.2", "IBM System z9 EC GA2"),
57     CPUDEF_INIT(0x2096, 9, 2, 40, 0x00000000U, "z9BC", "IBM System z9 BC GA1"),
58     CPUDEF_INIT(0x2094, 9, 3, 40, 0x00000000U, "z9EC.3", "IBM System z9 EC GA3"),
59     CPUDEF_INIT(0x2096, 9, 3, 40, 0x00000000U, "z9BC.2", "IBM System z9 BC GA2"),
60     CPUDEF_INIT(0x2097, 10, 1, 43, 0x00000000U, "z10EC", "IBM System z10 EC GA1"),
61     CPUDEF_INIT(0x2097, 10, 2, 43, 0x00000000U, "z10EC.2", "IBM System z10 EC GA2"),
62     CPUDEF_INIT(0x2098, 10, 2, 43, 0x00000000U, "z10BC", "IBM System z10 BC GA1"),
63     CPUDEF_INIT(0x2097, 10, 3, 43, 0x00000000U, "z10EC.3", "IBM System z10 EC GA3"),
64     CPUDEF_INIT(0x2098, 10, 3, 43, 0x00000000U, "z10BC.2", "IBM System z10 BC GA2"),
65     CPUDEF_INIT(0x2817, 11, 1, 44, 0x08000000U, "z196", "IBM zEnterprise 196 GA1"),
66     CPUDEF_INIT(0x2817, 11, 2, 44, 0x08000000U, "z196.2", "IBM zEnterprise 196 GA2"),
67     CPUDEF_INIT(0x2818, 11, 2, 44, 0x08000000U, "z114", "IBM zEnterprise 114 GA1"),
68     CPUDEF_INIT(0x2827, 12, 1, 44, 0x08000000U, "zEC12", "IBM zEnterprise EC12 GA1"),
69     CPUDEF_INIT(0x2827, 12, 2, 44, 0x08000000U, "zEC12.2", "IBM zEnterprise EC12 GA2"),
70     CPUDEF_INIT(0x2828, 12, 2, 44, 0x08000000U, "zBC12", "IBM zEnterprise BC12 GA1"),
71     CPUDEF_INIT(0x2964, 13, 1, 47, 0x08000000U, "z13", "IBM z13 GA1"),
72     CPUDEF_INIT(0x2964, 13, 2, 47, 0x08000000U, "z13.2", "IBM z13 GA2"),
73     CPUDEF_INIT(0x2965, 13, 2, 47, 0x08000000U, "z13s", "IBM z13s GA1"),
74 };
75
76 struct S390PrintCpuListInfo {
77     FILE *f;
78     fprintf_function print;
79 };
80
81 static void print_cpu_model_list(ObjectClass *klass, void *opaque)
82 {
83     struct S390PrintCpuListInfo *info = opaque;
84     S390CPUClass *scc = S390_CPU_CLASS(klass);
85     char *name = g_strdup(object_class_get_name(klass));
86     const char *details = "";
87
88     if (scc->is_static) {
89         details = "(static, migration-safe)";
90     } else if (scc->is_migration_safe) {
91         details = "(migration-safe)";
92     }
93
94     /* strip off the -s390-cpu */
95     g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
96     (*info->print)(info->f, "s390 %-15s %-35s %s\n", name, scc->desc,
97                    details);
98     g_free(name);
99 }
100
101 void s390_cpu_list(FILE *f, fprintf_function print)
102 {
103     struct S390PrintCpuListInfo info = {
104         .f = f,
105         .print = print,
106     };
107     S390FeatGroup group;
108     S390Feat feat;
109
110     object_class_foreach(print_cpu_model_list, TYPE_S390_CPU, false, &info);
111
112     (*print)(f, "\nRecognized feature flags:\n");
113     for (feat = 0; feat < S390_FEAT_MAX; feat++) {
114         const S390FeatDef *def = s390_feat_def(feat);
115
116         (*print)(f, "%-20s %-50s\n", def->name, def->desc);
117     }
118
119     (*print)(f, "\nRecognized feature groups:\n");
120     for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
121         const S390FeatGroupDef *def = s390_feat_group_def(group);
122
123         (*print)(f, "%-20s %-50s\n", def->name, def->desc);
124     }
125 }
126
127 #ifndef CONFIG_USER_ONLY
128 static void create_cpu_model_list(ObjectClass *klass, void *opaque)
129 {
130     CpuDefinitionInfoList **cpu_list = opaque;
131     CpuDefinitionInfoList *entry;
132     CpuDefinitionInfo *info;
133     char *name = g_strdup(object_class_get_name(klass));
134     S390CPUClass *scc = S390_CPU_CLASS(klass);
135
136     /* strip off the -s390-cpu */
137     g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
138     info = g_malloc0(sizeof(*info));
139     info->name = name;
140     info->has_migration_safe = true;
141     info->migration_safe = scc->is_migration_safe;
142     info->q_static = scc->is_static;
143
144
145     entry = g_malloc0(sizeof(*entry));
146     entry->value = info;
147     entry->next = *cpu_list;
148     *cpu_list = entry;
149 }
150
151 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
152 {
153     CpuDefinitionInfoList *list = NULL;
154
155     object_class_foreach(create_cpu_model_list, TYPE_S390_CPU, false, &list);
156
157     return list;
158 }
159 #endif
160
161 void s390_realize_cpu_model(CPUState *cs, Error **errp)
162 {
163     S390CPUClass *xcc = S390_CPU_GET_CLASS(cs);
164
165     if (xcc->kvm_required && !kvm_enabled()) {
166         error_setg(errp, "CPU definition requires KVM");
167         return;
168     }
169 }
170
171 static void get_feature(Object *obj, Visitor *v, const char *name,
172                         void *opaque, Error **errp)
173 {
174     S390Feat feat = (S390Feat) opaque;
175     S390CPU *cpu = S390_CPU(obj);
176     bool value;
177
178     if (!cpu->model) {
179         error_setg(errp, "Details about the host CPU model are not available, "
180                          "features cannot be queried.");
181         return;
182     }
183
184     value = test_bit(feat, cpu->model->features);
185     visit_type_bool(v, name, &value, errp);
186 }
187
188 static void set_feature(Object *obj, Visitor *v, const char *name,
189                         void *opaque, Error **errp)
190 {
191     S390Feat feat = (S390Feat) opaque;
192     DeviceState *dev = DEVICE(obj);
193     S390CPU *cpu = S390_CPU(obj);
194     bool value;
195
196     if (dev->realized) {
197         error_setg(errp, "Attempt to set property '%s' on '%s' after "
198                    "it was realized", name, object_get_typename(obj));
199         return;
200     } else if (!cpu->model) {
201         error_setg(errp, "Details about the host CPU model are not available, "
202                          "features cannot be changed.");
203         return;
204     }
205
206     visit_type_bool(v, name, &value, errp);
207     if (*errp) {
208         return;
209     }
210     if (value) {
211         if (!test_bit(feat, cpu->model->def->full_feat)) {
212             error_setg(errp, "Feature '%s' is not available for CPU model '%s',"
213                        " it was introduced with later models.",
214                        name, cpu->model->def->name);
215             return;
216         }
217         set_bit(feat, cpu->model->features);
218     } else {
219         clear_bit(feat, cpu->model->features);
220     }
221 }
222
223 static void get_feature_group(Object *obj, Visitor *v, const char *name,
224                               void *opaque, Error **errp)
225 {
226     S390FeatGroup group = (S390FeatGroup) opaque;
227     const S390FeatGroupDef *def = s390_feat_group_def(group);
228     S390CPU *cpu = S390_CPU(obj);
229     S390FeatBitmap tmp;
230     bool value;
231
232     if (!cpu->model) {
233         error_setg(errp, "Details about the host CPU model are not available, "
234                          "features cannot be queried.");
235         return;
236     }
237
238     /* a group is enabled if all features are enabled */
239     bitmap_and(tmp, cpu->model->features, def->feat, S390_FEAT_MAX);
240     value = bitmap_equal(tmp, def->feat, S390_FEAT_MAX);
241     visit_type_bool(v, name, &value, errp);
242 }
243
244 static void set_feature_group(Object *obj, Visitor *v, const char *name,
245                               void *opaque, Error **errp)
246 {
247     S390FeatGroup group = (S390FeatGroup) opaque;
248     const S390FeatGroupDef *def = s390_feat_group_def(group);
249     DeviceState *dev = DEVICE(obj);
250     S390CPU *cpu = S390_CPU(obj);
251     bool value;
252
253     if (dev->realized) {
254         error_setg(errp, "Attempt to set property '%s' on '%s' after "
255                    "it was realized", name, object_get_typename(obj));
256         return;
257     } else if (!cpu->model) {
258         error_setg(errp, "Details about the host CPU model are not available, "
259                          "features cannot be changed.");
260         return;
261     }
262
263     visit_type_bool(v, name, &value, errp);
264     if (*errp) {
265         return;
266     }
267     if (value) {
268         /* groups are added in one shot, so an intersect is sufficient */
269         if (!bitmap_intersects(def->feat, cpu->model->def->full_feat,
270                                S390_FEAT_MAX)) {
271             error_setg(errp, "Group '%s' is not available for CPU model '%s',"
272                        " it was introduced with later models.",
273                        name, cpu->model->def->name);
274             return;
275         }
276         bitmap_or(cpu->model->features, cpu->model->features, def->feat,
277                   S390_FEAT_MAX);
278     } else {
279         bitmap_andnot(cpu->model->features, cpu->model->features, def->feat,
280                       S390_FEAT_MAX);
281     }
282 }
283
284 void s390_cpu_model_register_props(Object *obj)
285 {
286     S390FeatGroup group;
287     S390Feat feat;
288
289     for (feat = 0; feat < S390_FEAT_MAX; feat++) {
290         const S390FeatDef *def = s390_feat_def(feat);
291         object_property_add(obj, def->name, "bool", get_feature,
292                             set_feature, NULL, (void *) feat, NULL);
293         object_property_set_description(obj, def->name, def->desc , NULL);
294     }
295     for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
296         const S390FeatGroupDef *def = s390_feat_group_def(group);
297         object_property_add(obj, def->name, "bool", get_feature_group,
298                             set_feature_group, NULL, (void *) group, NULL);
299         object_property_set_description(obj, def->name, def->desc , NULL);
300     }
301 }
302
303 static void s390_cpu_model_initfn(Object *obj)
304 {
305     S390CPU *cpu = S390_CPU(obj);
306     S390CPUClass *xcc = S390_CPU_GET_CLASS(cpu);
307
308     cpu->model = g_malloc0(sizeof(*cpu->model));
309     /* copy the model, so we can modify it */
310     cpu->model->def = xcc->cpu_def;
311     if (xcc->is_static) {
312         /* base model - features will never change */
313         bitmap_copy(cpu->model->features, cpu->model->def->base_feat,
314                     S390_FEAT_MAX);
315     } else {
316         /* latest model - features can change */
317         bitmap_copy(cpu->model->features,
318                     cpu->model->def->default_feat, S390_FEAT_MAX);
319     }
320 }
321
322 #ifdef CONFIG_KVM
323 static void s390_host_cpu_model_initfn(Object *obj)
324 {
325 }
326 #endif
327
328 static void s390_qemu_cpu_model_initfn(Object *obj)
329 {
330     S390CPU *cpu = S390_CPU(obj);
331
332     cpu->model = g_malloc0(sizeof(*cpu->model));
333     /* TCG emulates a z900 */
334     cpu->model->def = &s390_cpu_defs[0];
335     bitmap_copy(cpu->model->features, cpu->model->def->default_feat,
336                 S390_FEAT_MAX);
337 }
338
339 static void s390_cpu_model_finalize(Object *obj)
340 {
341     S390CPU *cpu = S390_CPU(obj);
342
343     g_free(cpu->model);
344     cpu->model = NULL;
345 }
346
347 static bool get_is_migration_safe(Object *obj, Error **errp)
348 {
349     return S390_CPU_GET_CLASS(obj)->is_migration_safe;
350 }
351
352 static bool get_is_static(Object *obj, Error **errp)
353 {
354     return S390_CPU_GET_CLASS(obj)->is_static;
355 }
356
357 static char *get_description(Object *obj, Error **errp)
358 {
359     return g_strdup(S390_CPU_GET_CLASS(obj)->desc);
360 }
361
362 void s390_cpu_model_class_register_props(ObjectClass *oc)
363 {
364     object_class_property_add_bool(oc, "migration-safe", get_is_migration_safe,
365                                    NULL, NULL);
366     object_class_property_add_bool(oc, "static", get_is_static,
367                                    NULL, NULL);
368     object_class_property_add_str(oc, "description", get_description, NULL,
369                                   NULL);
370 }
371
372 #ifdef CONFIG_KVM
373 static void s390_host_cpu_model_class_init(ObjectClass *oc, void *data)
374 {
375     S390CPUClass *xcc = S390_CPU_CLASS(oc);
376
377     xcc->kvm_required = true;
378     xcc->desc = "KVM only: All recognized features";
379 }
380 #endif
381
382 static void s390_base_cpu_model_class_init(ObjectClass *oc, void *data)
383 {
384     S390CPUClass *xcc = S390_CPU_CLASS(oc);
385
386     /* all base models are migration safe */
387     xcc->cpu_def = (const S390CPUDef *) data;
388     xcc->is_migration_safe = true;
389     xcc->is_static = true;
390     xcc->desc = xcc->cpu_def->desc;
391 }
392
393 static void s390_cpu_model_class_init(ObjectClass *oc, void *data)
394 {
395     S390CPUClass *xcc = S390_CPU_CLASS(oc);
396
397     /* model that can change between QEMU versions */
398     xcc->cpu_def = (const S390CPUDef *) data;
399     xcc->is_migration_safe = true;
400     xcc->desc = xcc->cpu_def->desc;
401 }
402
403 static void s390_qemu_cpu_model_class_init(ObjectClass *oc, void *data)
404 {
405     S390CPUClass *xcc = S390_CPU_CLASS(oc);
406
407     xcc->is_migration_safe = true;
408     xcc->desc = g_strdup_printf("QEMU Virtual CPU version %s",
409                                 qemu_hw_version());
410 }
411
412 #define S390_CPU_TYPE_SUFFIX "-" TYPE_S390_CPU
413 #define S390_CPU_TYPE_NAME(name) (name S390_CPU_TYPE_SUFFIX)
414
415 /* Generate type name for a cpu model. Caller has to free the string. */
416 static char *s390_cpu_type_name(const char *model_name)
417 {
418     return g_strdup_printf(S390_CPU_TYPE_NAME("%s"), model_name);
419 }
420
421 /* Generate type name for a base cpu model. Caller has to free the string. */
422 static char *s390_base_cpu_type_name(const char *model_name)
423 {
424     return g_strdup_printf(S390_CPU_TYPE_NAME("%s-base"), model_name);
425 }
426
427 ObjectClass *s390_cpu_class_by_name(const char *name)
428 {
429     char *typename = s390_cpu_type_name(name);
430     ObjectClass *oc;
431
432     oc = object_class_by_name(typename);
433     g_free(typename);
434     return oc;
435 }
436
437 static const TypeInfo qemu_s390_cpu_type_info = {
438     .name = S390_CPU_TYPE_NAME("qemu"),
439     .parent = TYPE_S390_CPU,
440     .instance_init = s390_qemu_cpu_model_initfn,
441     .instance_finalize = s390_cpu_model_finalize,
442     .class_init = s390_qemu_cpu_model_class_init,
443 };
444
445 #ifdef CONFIG_KVM
446 static const TypeInfo host_s390_cpu_type_info = {
447     .name = S390_CPU_TYPE_NAME("host"),
448     .parent = TYPE_S390_CPU,
449     .instance_init = s390_host_cpu_model_initfn,
450     .instance_finalize = s390_cpu_model_finalize,
451     .class_init = s390_host_cpu_model_class_init,
452 };
453 #endif
454
455 static void register_types(void)
456 {
457     int i;
458
459     /* init all bitmaps from gnerated data initially */
460     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
461         s390_init_feat_bitmap(s390_cpu_defs[i].base_init,
462                               s390_cpu_defs[i].base_feat);
463         s390_init_feat_bitmap(s390_cpu_defs[i].default_init,
464                               s390_cpu_defs[i].default_feat);
465         s390_init_feat_bitmap(s390_cpu_defs[i].full_init,
466                               s390_cpu_defs[i].full_feat);
467     }
468
469     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
470         char *base_name = s390_base_cpu_type_name(s390_cpu_defs[i].name);
471         TypeInfo ti_base = {
472             .name = base_name,
473             .parent = TYPE_S390_CPU,
474             .instance_init = s390_cpu_model_initfn,
475             .instance_finalize = s390_cpu_model_finalize,
476             .class_init = s390_base_cpu_model_class_init,
477             .class_data = (void *) &s390_cpu_defs[i],
478         };
479         char *name = s390_cpu_type_name(s390_cpu_defs[i].name);
480         TypeInfo ti = {
481             .name = name,
482             .parent = TYPE_S390_CPU,
483             .instance_init = s390_cpu_model_initfn,
484             .instance_finalize = s390_cpu_model_finalize,
485             .class_init = s390_cpu_model_class_init,
486             .class_data = (void *) &s390_cpu_defs[i],
487         };
488
489         type_register_static(&ti_base);
490         type_register_static(&ti);
491         g_free(base_name);
492         g_free(name);
493     }
494
495     type_register_static(&qemu_s390_cpu_type_info);
496 #ifdef CONFIG_KVM
497     type_register_static(&host_s390_cpu_type_info);
498 #endif
499 }
500
501 type_init(register_types)
This page took 0.052794 seconds and 4 git commands to generate.