s390x/cpumodel: Set up CPU model for AQIC interception
[qemu/ar7.git] / target / s390x / cpu_models.c
blob1d16d7d5e7945217cd782937f8582c8066316d7c
1 /*
2 * CPU models for s390x
4 * Copyright 2016 IBM Corp.
6 * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
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.
13 #include "qemu/osdep.h"
14 #include "cpu.h"
15 #include "internal.h"
16 #include "kvm_s390x.h"
17 #include "sysemu/kvm.h"
18 #include "sysemu/tcg.h"
19 #include "qapi/error.h"
20 #include "qapi/visitor.h"
21 #include "qemu/error-report.h"
22 #include "qemu/module.h"
23 #include "qemu/qemu-print.h"
24 #include "qapi/qmp/qerror.h"
25 #include "qapi/qobject-input-visitor.h"
26 #include "qapi/qmp/qdict.h"
27 #ifndef CONFIG_USER_ONLY
28 #include "sysemu/arch_init.h"
29 #include "hw/pci/pci.h"
30 #endif
31 #include "qapi/qapi-commands-machine-target.h"
33 #define CPUDEF_INIT(_type, _gen, _ec_ga, _mha_pow, _hmfai, _name, _desc) \
34 { \
35 .name = _name, \
36 .type = _type, \
37 .gen = _gen, \
38 .ec_ga = _ec_ga, \
39 .mha_pow = _mha_pow, \
40 .hmfai = _hmfai, \
41 .desc = _desc, \
42 .base_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _BASE }, \
43 .default_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _DEFAULT }, \
44 .full_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _FULL }, \
48 * CPU definition list in order of release. Up to generation 14 base features
49 * of a following release have been a superset of the previous release. With
50 * generation 15 one base feature and one optional feature have been deprecated.
52 static S390CPUDef s390_cpu_defs[] = {
53 CPUDEF_INIT(0x2064, 7, 1, 38, 0x00000000U, "z900", "IBM zSeries 900 GA1"),
54 CPUDEF_INIT(0x2064, 7, 2, 38, 0x00000000U, "z900.2", "IBM zSeries 900 GA2"),
55 CPUDEF_INIT(0x2064, 7, 3, 38, 0x00000000U, "z900.3", "IBM zSeries 900 GA3"),
56 CPUDEF_INIT(0x2066, 7, 3, 38, 0x00000000U, "z800", "IBM zSeries 800 GA1"),
57 CPUDEF_INIT(0x2084, 8, 1, 38, 0x00000000U, "z990", "IBM zSeries 990 GA1"),
58 CPUDEF_INIT(0x2084, 8, 2, 38, 0x00000000U, "z990.2", "IBM zSeries 990 GA2"),
59 CPUDEF_INIT(0x2084, 8, 3, 38, 0x00000000U, "z990.3", "IBM zSeries 990 GA3"),
60 CPUDEF_INIT(0x2086, 8, 3, 38, 0x00000000U, "z890", "IBM zSeries 880 GA1"),
61 CPUDEF_INIT(0x2084, 8, 4, 38, 0x00000000U, "z990.4", "IBM zSeries 990 GA4"),
62 CPUDEF_INIT(0x2086, 8, 4, 38, 0x00000000U, "z890.2", "IBM zSeries 880 GA2"),
63 CPUDEF_INIT(0x2084, 8, 5, 38, 0x00000000U, "z990.5", "IBM zSeries 990 GA5"),
64 CPUDEF_INIT(0x2086, 8, 5, 38, 0x00000000U, "z890.3", "IBM zSeries 880 GA3"),
65 CPUDEF_INIT(0x2094, 9, 1, 40, 0x00000000U, "z9EC", "IBM System z9 EC GA1"),
66 CPUDEF_INIT(0x2094, 9, 2, 40, 0x00000000U, "z9EC.2", "IBM System z9 EC GA2"),
67 CPUDEF_INIT(0x2096, 9, 2, 40, 0x00000000U, "z9BC", "IBM System z9 BC GA1"),
68 CPUDEF_INIT(0x2094, 9, 3, 40, 0x00000000U, "z9EC.3", "IBM System z9 EC GA3"),
69 CPUDEF_INIT(0x2096, 9, 3, 40, 0x00000000U, "z9BC.2", "IBM System z9 BC GA2"),
70 CPUDEF_INIT(0x2097, 10, 1, 43, 0x00000000U, "z10EC", "IBM System z10 EC GA1"),
71 CPUDEF_INIT(0x2097, 10, 2, 43, 0x00000000U, "z10EC.2", "IBM System z10 EC GA2"),
72 CPUDEF_INIT(0x2098, 10, 2, 43, 0x00000000U, "z10BC", "IBM System z10 BC GA1"),
73 CPUDEF_INIT(0x2097, 10, 3, 43, 0x00000000U, "z10EC.3", "IBM System z10 EC GA3"),
74 CPUDEF_INIT(0x2098, 10, 3, 43, 0x00000000U, "z10BC.2", "IBM System z10 BC GA2"),
75 CPUDEF_INIT(0x2817, 11, 1, 44, 0x08000000U, "z196", "IBM zEnterprise 196 GA1"),
76 CPUDEF_INIT(0x2817, 11, 2, 44, 0x08000000U, "z196.2", "IBM zEnterprise 196 GA2"),
77 CPUDEF_INIT(0x2818, 11, 2, 44, 0x08000000U, "z114", "IBM zEnterprise 114 GA1"),
78 CPUDEF_INIT(0x2827, 12, 1, 44, 0x08000000U, "zEC12", "IBM zEnterprise EC12 GA1"),
79 CPUDEF_INIT(0x2827, 12, 2, 44, 0x08000000U, "zEC12.2", "IBM zEnterprise EC12 GA2"),
80 CPUDEF_INIT(0x2828, 12, 2, 44, 0x08000000U, "zBC12", "IBM zEnterprise BC12 GA1"),
81 CPUDEF_INIT(0x2964, 13, 1, 47, 0x08000000U, "z13", "IBM z13 GA1"),
82 CPUDEF_INIT(0x2964, 13, 2, 47, 0x08000000U, "z13.2", "IBM z13 GA2"),
83 CPUDEF_INIT(0x2965, 13, 2, 47, 0x08000000U, "z13s", "IBM z13s GA1"),
84 CPUDEF_INIT(0x3906, 14, 1, 47, 0x08000000U, "z14", "IBM z14 GA1"),
85 CPUDEF_INIT(0x3906, 14, 2, 47, 0x08000000U, "z14.2", "IBM z14 GA2"),
86 CPUDEF_INIT(0x3907, 14, 1, 47, 0x08000000U, "z14ZR1", "IBM z14 Model ZR1 GA1"),
87 CPUDEF_INIT(0x8561, 15, 1, 47, 0x08000000U, "gen15a", "IBM 8561 GA1"),
88 CPUDEF_INIT(0x8562, 15, 1, 47, 0x08000000U, "gen15b", "IBM 8562 GA1"),
91 #define QEMU_MAX_CPU_TYPE 0x2964
92 #define QEMU_MAX_CPU_GEN 13
93 #define QEMU_MAX_CPU_EC_GA 2
94 static const S390FeatInit qemu_max_cpu_feat_init = { S390_FEAT_LIST_QEMU_MAX };
95 static S390FeatBitmap qemu_max_cpu_feat;
97 /* features part of a base model but not relevant for finding a base model */
98 S390FeatBitmap ignored_base_feat;
100 void s390_cpudef_featoff(uint8_t gen, uint8_t ec_ga, S390Feat feat)
102 const S390CPUDef *def;
104 def = s390_find_cpu_def(0, gen, ec_ga, NULL);
105 clear_bit(feat, (unsigned long *)&def->default_feat);
108 void s390_cpudef_featoff_greater(uint8_t gen, uint8_t ec_ga, S390Feat feat)
110 int i;
112 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
113 const S390CPUDef *def = &s390_cpu_defs[i];
115 if (def->gen < gen) {
116 continue;
118 if (def->gen == gen && def->ec_ga < ec_ga) {
119 continue;
122 clear_bit(feat, (unsigned long *)&def->default_feat);
126 void s390_cpudef_group_featoff_greater(uint8_t gen, uint8_t ec_ga,
127 S390FeatGroup group)
129 const S390FeatGroupDef *group_def = s390_feat_group_def(group);
130 S390FeatBitmap group_def_off;
131 int i;
133 bitmap_complement(group_def_off, group_def->feat, S390_FEAT_MAX);
135 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
136 const S390CPUDef *cpu_def = &s390_cpu_defs[i];
138 if (cpu_def->gen < gen) {
139 continue;
141 if (cpu_def->gen == gen && cpu_def->ec_ga < ec_ga) {
142 continue;
145 bitmap_and((unsigned long *)&cpu_def->default_feat,
146 cpu_def->default_feat, group_def_off, S390_FEAT_MAX);
150 uint32_t s390_get_hmfai(void)
152 static S390CPU *cpu;
154 if (!cpu) {
155 cpu = S390_CPU(qemu_get_cpu(0));
158 if (!cpu || !cpu->model) {
159 return 0;
161 return cpu->model->def->hmfai;
164 uint8_t s390_get_mha_pow(void)
166 static S390CPU *cpu;
168 if (!cpu) {
169 cpu = S390_CPU(qemu_get_cpu(0));
172 if (!cpu || !cpu->model) {
173 return 0;
175 return cpu->model->def->mha_pow;
178 uint32_t s390_get_ibc_val(void)
180 uint16_t unblocked_ibc, lowest_ibc;
181 static S390CPU *cpu;
183 if (!cpu) {
184 cpu = S390_CPU(qemu_get_cpu(0));
187 if (!cpu || !cpu->model) {
188 return 0;
190 unblocked_ibc = s390_ibc_from_cpu_model(cpu->model);
191 lowest_ibc = cpu->model->lowest_ibc;
192 /* the lowest_ibc always has to be <= unblocked_ibc */
193 if (!lowest_ibc || lowest_ibc > unblocked_ibc) {
194 return 0;
196 return ((uint32_t) lowest_ibc << 16) | unblocked_ibc;
199 void s390_get_feat_block(S390FeatType type, uint8_t *data)
201 static S390CPU *cpu;
203 if (!cpu) {
204 cpu = S390_CPU(qemu_get_cpu(0));
207 if (!cpu || !cpu->model) {
208 return;
210 s390_fill_feat_block(cpu->model->features, type, data);
213 bool s390_has_feat(S390Feat feat)
215 static S390CPU *cpu;
217 if (!cpu) {
218 cpu = S390_CPU(qemu_get_cpu(0));
221 if (!cpu || !cpu->model) {
222 #ifdef CONFIG_KVM
223 if (kvm_enabled()) {
224 if (feat == S390_FEAT_VECTOR) {
225 return kvm_check_extension(kvm_state,
226 KVM_CAP_S390_VECTOR_REGISTERS);
228 if (feat == S390_FEAT_RUNTIME_INSTRUMENTATION) {
229 return kvm_s390_get_ri();
231 if (feat == S390_FEAT_MSA_EXT_3) {
232 return true;
235 #endif
236 if (feat == S390_FEAT_ZPCI) {
237 return true;
239 return 0;
241 return test_bit(feat, cpu->model->features);
244 uint8_t s390_get_gen_for_cpu_type(uint16_t type)
246 int i;
248 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
249 if (s390_cpu_defs[i].type == type) {
250 return s390_cpu_defs[i].gen;
253 return 0;
256 const S390CPUDef *s390_find_cpu_def(uint16_t type, uint8_t gen, uint8_t ec_ga,
257 S390FeatBitmap features)
259 const S390CPUDef *last_compatible = NULL;
260 const S390CPUDef *matching_cpu_type = NULL;
261 int i;
263 if (!gen) {
264 ec_ga = 0;
266 if (!gen && type) {
267 gen = s390_get_gen_for_cpu_type(type);
270 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
271 const S390CPUDef *def = &s390_cpu_defs[i];
272 S390FeatBitmap missing;
274 /* don't even try newer generations if we know the generation */
275 if (gen) {
276 if (def->gen > gen) {
277 break;
278 } else if (def->gen == gen && ec_ga && def->ec_ga > ec_ga) {
279 break;
283 if (features) {
284 /* see if the model satisfies the minimum features */
285 bitmap_andnot(missing, def->base_feat, features, S390_FEAT_MAX);
287 * Ignore certain features that are in the base model, but not
288 * relevant for the search (esp. MSA subfunctions).
290 bitmap_andnot(missing, missing, ignored_base_feat, S390_FEAT_MAX);
291 if (!bitmap_empty(missing, S390_FEAT_MAX)) {
292 break;
296 /* stop the search if we found the exact model */
297 if (def->type == type && def->ec_ga == ec_ga) {
298 return def;
300 /* remember if we've at least seen one with the same cpu type */
301 if (def->type == type) {
302 matching_cpu_type = def;
304 last_compatible = def;
306 /* prefer the model with the same cpu type, esp. don't take the BC for EC */
307 if (matching_cpu_type) {
308 return matching_cpu_type;
310 return last_compatible;
313 static void s390_print_cpu_model_list_entry(gpointer data, gpointer user_data)
315 const S390CPUClass *scc = S390_CPU_CLASS((ObjectClass *)data);
316 char *name = g_strdup(object_class_get_name((ObjectClass *)data));
317 const char *details = "";
319 if (scc->is_static) {
320 details = "(static, migration-safe)";
321 } else if (scc->is_migration_safe) {
322 details = "(migration-safe)";
325 /* strip off the -s390x-cpu */
326 g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
327 qemu_printf("s390 %-15s %-35s %s\n", name, scc->desc, details);
328 g_free(name);
331 static gint s390_cpu_list_compare(gconstpointer a, gconstpointer b)
333 const S390CPUClass *cc_a = S390_CPU_CLASS((ObjectClass *)a);
334 const S390CPUClass *cc_b = S390_CPU_CLASS((ObjectClass *)b);
335 const char *name_a = object_class_get_name((ObjectClass *)a);
336 const char *name_b = object_class_get_name((ObjectClass *)b);
339 * Move qemu, host and max to the top of the list, qemu first, host second,
340 * max third.
342 if (name_a[0] == 'q') {
343 return -1;
344 } else if (name_b[0] == 'q') {
345 return 1;
346 } else if (name_a[0] == 'h') {
347 return -1;
348 } else if (name_b[0] == 'h') {
349 return 1;
350 } else if (name_a[0] == 'm') {
351 return -1;
352 } else if (name_b[0] == 'm') {
353 return 1;
356 /* keep the same order we have in our table (sorted by release date) */
357 if (cc_a->cpu_def != cc_b->cpu_def) {
358 return cc_a->cpu_def - cc_b->cpu_def;
361 /* exact same definition - list base model first */
362 return cc_a->is_static ? -1 : 1;
365 void s390_cpu_list(void)
367 S390FeatGroup group;
368 S390Feat feat;
369 GSList *list;
371 list = object_class_get_list(TYPE_S390_CPU, false);
372 list = g_slist_sort(list, s390_cpu_list_compare);
373 g_slist_foreach(list, s390_print_cpu_model_list_entry, NULL);
374 g_slist_free(list);
376 qemu_printf("\nRecognized feature flags:\n");
377 for (feat = 0; feat < S390_FEAT_MAX; feat++) {
378 const S390FeatDef *def = s390_feat_def(feat);
380 qemu_printf("%-20s %-50s\n", def->name, def->desc);
383 qemu_printf("\nRecognized feature groups:\n");
384 for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
385 const S390FeatGroupDef *def = s390_feat_group_def(group);
387 qemu_printf("%-20s %-50s\n", def->name, def->desc);
391 static S390CPUModel *get_max_cpu_model(Error **errp);
393 #ifndef CONFIG_USER_ONLY
394 static void list_add_feat(const char *name, void *opaque);
396 static void check_unavailable_features(const S390CPUModel *max_model,
397 const S390CPUModel *model,
398 strList **unavailable)
400 S390FeatBitmap missing;
402 /* check general model compatibility */
403 if (max_model->def->gen < model->def->gen ||
404 (max_model->def->gen == model->def->gen &&
405 max_model->def->ec_ga < model->def->ec_ga)) {
406 list_add_feat("type", unavailable);
409 /* detect missing features if any to properly report them */
410 bitmap_andnot(missing, model->features, max_model->features,
411 S390_FEAT_MAX);
412 if (!bitmap_empty(missing, S390_FEAT_MAX)) {
413 s390_feat_bitmap_to_ascii(missing, unavailable, list_add_feat);
417 struct CpuDefinitionInfoListData {
418 CpuDefinitionInfoList *list;
419 S390CPUModel *model;
422 static void create_cpu_model_list(ObjectClass *klass, void *opaque)
424 struct CpuDefinitionInfoListData *cpu_list_data = opaque;
425 CpuDefinitionInfoList **cpu_list = &cpu_list_data->list;
426 CpuDefinitionInfoList *entry;
427 CpuDefinitionInfo *info;
428 char *name = g_strdup(object_class_get_name(klass));
429 S390CPUClass *scc = S390_CPU_CLASS(klass);
431 /* strip off the -s390x-cpu */
432 g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
433 info = g_new0(CpuDefinitionInfo, 1);
434 info->name = name;
435 info->has_migration_safe = true;
436 info->migration_safe = scc->is_migration_safe;
437 info->q_static = scc->is_static;
438 info->q_typename = g_strdup(object_class_get_name(klass));
439 /* check for unavailable features */
440 if (cpu_list_data->model) {
441 Object *obj;
442 S390CPU *sc;
443 obj = object_new(object_class_get_name(klass));
444 sc = S390_CPU(obj);
445 if (sc->model) {
446 info->has_unavailable_features = true;
447 check_unavailable_features(cpu_list_data->model, sc->model,
448 &info->unavailable_features);
450 object_unref(obj);
453 entry = g_new0(CpuDefinitionInfoList, 1);
454 entry->value = info;
455 entry->next = *cpu_list;
456 *cpu_list = entry;
459 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
461 struct CpuDefinitionInfoListData list_data = {
462 .list = NULL,
465 list_data.model = get_max_cpu_model(errp);
466 if (*errp) {
467 error_free(*errp);
468 *errp = NULL;
471 object_class_foreach(create_cpu_model_list, TYPE_S390_CPU, false,
472 &list_data);
474 return list_data.list;
477 static void cpu_model_from_info(S390CPUModel *model, const CpuModelInfo *info,
478 Error **errp)
480 const QDict *qdict = NULL;
481 const QDictEntry *e;
482 Visitor *visitor;
483 ObjectClass *oc;
484 S390CPU *cpu;
485 Object *obj;
487 if (info->props) {
488 qdict = qobject_to(QDict, info->props);
489 if (!qdict) {
490 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
491 return;
495 oc = cpu_class_by_name(TYPE_S390_CPU, info->name);
496 if (!oc) {
497 error_setg(errp, "The CPU definition \'%s\' is unknown.", info->name);
498 return;
500 if (S390_CPU_CLASS(oc)->kvm_required && !kvm_enabled()) {
501 error_setg(errp, "The CPU definition '%s' requires KVM", info->name);
502 return;
504 obj = object_new(object_class_get_name(oc));
505 cpu = S390_CPU(obj);
507 if (!cpu->model) {
508 error_setg(errp, "Details about the host CPU model are not available, "
509 "it cannot be used.");
510 object_unref(obj);
511 return;
514 if (qdict) {
515 visitor = qobject_input_visitor_new(info->props);
516 visit_start_struct(visitor, NULL, NULL, 0, errp);
517 if (*errp) {
518 object_unref(obj);
519 return;
521 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
522 object_property_set(obj, visitor, e->key, errp);
523 if (*errp) {
524 break;
527 if (!*errp) {
528 visit_check_struct(visitor, errp);
530 visit_end_struct(visitor, NULL);
531 visit_free(visitor);
532 if (*errp) {
533 object_unref(obj);
534 return;
538 /* copy the model and throw the cpu away */
539 memcpy(model, cpu->model, sizeof(*model));
540 object_unref(obj);
543 static void qdict_add_disabled_feat(const char *name, void *opaque)
545 qdict_put_bool(opaque, name, false);
548 static void qdict_add_enabled_feat(const char *name, void *opaque)
550 qdict_put_bool(opaque, name, true);
553 /* convert S390CPUDef into a static CpuModelInfo */
554 static void cpu_info_from_model(CpuModelInfo *info, const S390CPUModel *model,
555 bool delta_changes)
557 QDict *qdict = qdict_new();
558 S390FeatBitmap bitmap;
560 /* always fallback to the static base model */
561 info->name = g_strdup_printf("%s-base", model->def->name);
563 if (delta_changes) {
564 /* features deleted from the base feature set */
565 bitmap_andnot(bitmap, model->def->base_feat, model->features,
566 S390_FEAT_MAX);
567 if (!bitmap_empty(bitmap, S390_FEAT_MAX)) {
568 s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_disabled_feat);
571 /* features added to the base feature set */
572 bitmap_andnot(bitmap, model->features, model->def->base_feat,
573 S390_FEAT_MAX);
574 if (!bitmap_empty(bitmap, S390_FEAT_MAX)) {
575 s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_enabled_feat);
577 } else {
578 /* expand all features */
579 s390_feat_bitmap_to_ascii(model->features, qdict,
580 qdict_add_enabled_feat);
581 bitmap_complement(bitmap, model->features, S390_FEAT_MAX);
582 s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_disabled_feat);
585 if (!qdict_size(qdict)) {
586 qobject_unref(qdict);
587 } else {
588 info->props = QOBJECT(qdict);
589 info->has_props = true;
593 CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
594 CpuModelInfo *model,
595 Error **errp)
597 CpuModelExpansionInfo *expansion_info = NULL;
598 S390CPUModel s390_model;
599 bool delta_changes = false;
601 /* convert it to our internal representation */
602 cpu_model_from_info(&s390_model, model, errp);
603 if (*errp) {
604 return NULL;
607 if (type == CPU_MODEL_EXPANSION_TYPE_STATIC) {
608 delta_changes = true;
609 } else if (type != CPU_MODEL_EXPANSION_TYPE_FULL) {
610 error_setg(errp, "The requested expansion type is not supported.");
611 return NULL;
614 /* convert it back to a static representation */
615 expansion_info = g_new0(CpuModelExpansionInfo, 1);
616 expansion_info->model = g_malloc0(sizeof(*expansion_info->model));
617 cpu_info_from_model(expansion_info->model, &s390_model, delta_changes);
618 return expansion_info;
621 static void list_add_feat(const char *name, void *opaque)
623 strList **last = (strList **) opaque;
624 strList *entry;
626 entry = g_new0(strList, 1);
627 entry->value = g_strdup(name);
628 entry->next = *last;
629 *last = entry;
632 CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *infoa,
633 CpuModelInfo *infob,
634 Error **errp)
636 CpuModelCompareResult feat_result, gen_result;
637 CpuModelCompareInfo *compare_info;
638 S390FeatBitmap missing, added;
639 S390CPUModel modela, modelb;
641 /* convert both models to our internal representation */
642 cpu_model_from_info(&modela, infoa, errp);
643 if (*errp) {
644 return NULL;
646 cpu_model_from_info(&modelb, infob, errp);
647 if (*errp) {
648 return NULL;
650 compare_info = g_new0(CpuModelCompareInfo, 1);
652 /* check the cpu generation and ga level */
653 if (modela.def->gen == modelb.def->gen) {
654 if (modela.def->ec_ga == modelb.def->ec_ga) {
655 /* ec and corresponding bc are identical */
656 gen_result = CPU_MODEL_COMPARE_RESULT_IDENTICAL;
657 } else if (modela.def->ec_ga < modelb.def->ec_ga) {
658 gen_result = CPU_MODEL_COMPARE_RESULT_SUBSET;
659 } else {
660 gen_result = CPU_MODEL_COMPARE_RESULT_SUPERSET;
662 } else if (modela.def->gen < modelb.def->gen) {
663 gen_result = CPU_MODEL_COMPARE_RESULT_SUBSET;
664 } else {
665 gen_result = CPU_MODEL_COMPARE_RESULT_SUPERSET;
667 if (gen_result != CPU_MODEL_COMPARE_RESULT_IDENTICAL) {
668 /* both models cannot be made identical */
669 list_add_feat("type", &compare_info->responsible_properties);
672 /* check the feature set */
673 if (bitmap_equal(modela.features, modelb.features, S390_FEAT_MAX)) {
674 feat_result = CPU_MODEL_COMPARE_RESULT_IDENTICAL;
675 } else {
676 bitmap_andnot(missing, modela.features, modelb.features, S390_FEAT_MAX);
677 s390_feat_bitmap_to_ascii(missing,
678 &compare_info->responsible_properties,
679 list_add_feat);
680 bitmap_andnot(added, modelb.features, modela.features, S390_FEAT_MAX);
681 s390_feat_bitmap_to_ascii(added, &compare_info->responsible_properties,
682 list_add_feat);
683 if (bitmap_empty(missing, S390_FEAT_MAX)) {
684 feat_result = CPU_MODEL_COMPARE_RESULT_SUBSET;
685 } else if (bitmap_empty(added, S390_FEAT_MAX)) {
686 feat_result = CPU_MODEL_COMPARE_RESULT_SUPERSET;
687 } else {
688 feat_result = CPU_MODEL_COMPARE_RESULT_INCOMPATIBLE;
692 /* combine the results */
693 if (gen_result == feat_result) {
694 compare_info->result = gen_result;
695 } else if (feat_result == CPU_MODEL_COMPARE_RESULT_IDENTICAL) {
696 compare_info->result = gen_result;
697 } else if (gen_result == CPU_MODEL_COMPARE_RESULT_IDENTICAL) {
698 compare_info->result = feat_result;
699 } else {
700 compare_info->result = CPU_MODEL_COMPARE_RESULT_INCOMPATIBLE;
702 return compare_info;
705 CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *infoa,
706 CpuModelInfo *infob,
707 Error **errp)
709 CpuModelBaselineInfo *baseline_info;
710 S390CPUModel modela, modelb, model;
711 uint16_t cpu_type;
712 uint8_t max_gen_ga;
713 uint8_t max_gen;
715 /* convert both models to our internal representation */
716 cpu_model_from_info(&modela, infoa, errp);
717 if (*errp) {
718 return NULL;
721 cpu_model_from_info(&modelb, infob, errp);
722 if (*errp) {
723 return NULL;
726 /* features both models support */
727 bitmap_and(model.features, modela.features, modelb.features, S390_FEAT_MAX);
729 /* detect the maximum model not regarding features */
730 if (modela.def->gen == modelb.def->gen) {
731 if (modela.def->type == modelb.def->type) {
732 cpu_type = modela.def->type;
733 } else {
734 cpu_type = 0;
736 max_gen = modela.def->gen;
737 max_gen_ga = MIN(modela.def->ec_ga, modelb.def->ec_ga);
738 } else if (modela.def->gen > modelb.def->gen) {
739 cpu_type = modelb.def->type;
740 max_gen = modelb.def->gen;
741 max_gen_ga = modelb.def->ec_ga;
742 } else {
743 cpu_type = modela.def->type;
744 max_gen = modela.def->gen;
745 max_gen_ga = modela.def->ec_ga;
748 model.def = s390_find_cpu_def(cpu_type, max_gen, max_gen_ga,
749 model.features);
751 /* models without early base features (esan3) are bad */
752 if (!model.def) {
753 error_setg(errp, "No compatible CPU model could be created as"
754 " important base features are disabled");
755 return NULL;
758 /* strip off features not part of the max model */
759 bitmap_and(model.features, model.features, model.def->full_feat,
760 S390_FEAT_MAX);
762 baseline_info = g_new0(CpuModelBaselineInfo, 1);
763 baseline_info->model = g_malloc0(sizeof(*baseline_info->model));
764 cpu_info_from_model(baseline_info->model, &model, true);
765 return baseline_info;
767 #endif
769 static void check_consistency(const S390CPUModel *model)
771 static int dep[][2] = {
772 { S390_FEAT_IPTE_RANGE, S390_FEAT_DAT_ENH },
773 { S390_FEAT_IDTE_SEGMENT, S390_FEAT_DAT_ENH },
774 { S390_FEAT_IDTE_REGION, S390_FEAT_DAT_ENH },
775 { S390_FEAT_IDTE_REGION, S390_FEAT_IDTE_SEGMENT },
776 { S390_FEAT_LOCAL_TLB_CLEARING, S390_FEAT_DAT_ENH},
777 { S390_FEAT_LONG_DISPLACEMENT_FAST, S390_FEAT_LONG_DISPLACEMENT },
778 { S390_FEAT_DFP_FAST, S390_FEAT_DFP },
779 { S390_FEAT_TRANSACTIONAL_EXE, S390_FEAT_STFLE_49 },
780 { S390_FEAT_EDAT_2, S390_FEAT_EDAT},
781 { S390_FEAT_MSA_EXT_5, S390_FEAT_KIMD_SHA_512 },
782 { S390_FEAT_MSA_EXT_5, S390_FEAT_KLMD_SHA_512 },
783 { S390_FEAT_MSA_EXT_4, S390_FEAT_MSA_EXT_3 },
784 { S390_FEAT_SIE_CMMA, S390_FEAT_CMM },
785 { S390_FEAT_SIE_CMMA, S390_FEAT_SIE_GSLS },
786 { S390_FEAT_SIE_PFMFI, S390_FEAT_EDAT },
787 { S390_FEAT_MSA_EXT_8, S390_FEAT_MSA_EXT_3 },
788 { S390_FEAT_MSA_EXT_9, S390_FEAT_MSA_EXT_3 },
789 { S390_FEAT_MSA_EXT_9, S390_FEAT_MSA_EXT_4 },
790 { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING },
791 { S390_FEAT_VECTOR_PACKED_DECIMAL, S390_FEAT_VECTOR },
792 { S390_FEAT_VECTOR_ENH, S390_FEAT_VECTOR },
793 { S390_FEAT_INSTRUCTION_EXEC_PROT, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 },
794 { S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2, S390_FEAT_ESOP },
795 { S390_FEAT_CMM_NT, S390_FEAT_CMM },
796 { S390_FEAT_GUARDED_STORAGE, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 },
797 { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_STORE_CLOCK_FAST },
798 { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING },
799 { S390_FEAT_SEMAPHORE_ASSIST, S390_FEAT_STFLE_49 },
800 { S390_FEAT_KIMD_SHA3_224, S390_FEAT_MSA },
801 { S390_FEAT_KIMD_SHA3_256, S390_FEAT_MSA },
802 { S390_FEAT_KIMD_SHA3_384, S390_FEAT_MSA },
803 { S390_FEAT_KIMD_SHA3_512, S390_FEAT_MSA },
804 { S390_FEAT_KIMD_SHAKE_128, S390_FEAT_MSA },
805 { S390_FEAT_KIMD_SHAKE_256, S390_FEAT_MSA },
806 { S390_FEAT_KLMD_SHA3_224, S390_FEAT_MSA },
807 { S390_FEAT_KLMD_SHA3_256, S390_FEAT_MSA },
808 { S390_FEAT_KLMD_SHA3_384, S390_FEAT_MSA },
809 { S390_FEAT_KLMD_SHA3_512, S390_FEAT_MSA },
810 { S390_FEAT_KLMD_SHAKE_128, S390_FEAT_MSA },
811 { S390_FEAT_KLMD_SHAKE_256, S390_FEAT_MSA },
812 { S390_FEAT_PRNO_TRNG_QRTCR, S390_FEAT_MSA_EXT_5 },
813 { S390_FEAT_PRNO_TRNG, S390_FEAT_MSA_EXT_5 },
814 { S390_FEAT_SIE_KSS, S390_FEAT_SIE_F2 },
815 { S390_FEAT_AP_QUERY_CONFIG_INFO, S390_FEAT_AP },
816 { S390_FEAT_AP_FACILITIES_TEST, S390_FEAT_AP },
817 { S390_FEAT_PTFF_QSIE, S390_FEAT_MULTIPLE_EPOCH },
818 { S390_FEAT_PTFF_QTOUE, S390_FEAT_MULTIPLE_EPOCH },
819 { S390_FEAT_PTFF_STOE, S390_FEAT_MULTIPLE_EPOCH },
820 { S390_FEAT_PTFF_STOUE, S390_FEAT_MULTIPLE_EPOCH },
821 { S390_FEAT_AP_QUEUE_INTERRUPT_CONTROL, S390_FEAT_AP },
823 int i;
825 for (i = 0; i < ARRAY_SIZE(dep); i++) {
826 if (test_bit(dep[i][0], model->features) &&
827 !test_bit(dep[i][1], model->features)) {
828 warn_report("\'%s\' requires \'%s\'.",
829 s390_feat_def(dep[i][0])->name,
830 s390_feat_def(dep[i][1])->name);
835 static void error_prepend_missing_feat(const char *name, void *opaque)
837 error_prepend((Error **) opaque, "%s ", name);
840 static void check_compatibility(const S390CPUModel *max_model,
841 const S390CPUModel *model, Error **errp)
843 S390FeatBitmap missing;
845 if (model->def->gen > max_model->def->gen) {
846 error_setg(errp, "Selected CPU generation is too new. Maximum "
847 "supported model in the configuration: \'%s\'",
848 max_model->def->name);
849 return;
850 } else if (model->def->gen == max_model->def->gen &&
851 model->def->ec_ga > max_model->def->ec_ga) {
852 error_setg(errp, "Selected CPU GA level is too new. Maximum "
853 "supported model in the configuration: \'%s\'",
854 max_model->def->name);
855 return;
858 /* detect the missing features to properly report them */
859 bitmap_andnot(missing, model->features, max_model->features, S390_FEAT_MAX);
860 if (bitmap_empty(missing, S390_FEAT_MAX)) {
861 return;
864 error_setg(errp, " ");
865 s390_feat_bitmap_to_ascii(missing, errp, error_prepend_missing_feat);
866 error_prepend(errp, "Some features requested in the CPU model are not "
867 "available in the configuration: ");
870 static S390CPUModel *get_max_cpu_model(Error **errp)
872 static S390CPUModel max_model;
873 static bool cached;
875 if (cached) {
876 return &max_model;
879 if (kvm_enabled()) {
880 kvm_s390_get_host_cpu_model(&max_model, errp);
881 } else {
882 max_model.def = s390_find_cpu_def(QEMU_MAX_CPU_TYPE, QEMU_MAX_CPU_GEN,
883 QEMU_MAX_CPU_EC_GA, NULL);
884 bitmap_copy(max_model.features, qemu_max_cpu_feat, S390_FEAT_MAX);
886 if (!*errp) {
887 cached = true;
888 return &max_model;
890 return NULL;
893 static inline void apply_cpu_model(const S390CPUModel *model, Error **errp)
895 #ifndef CONFIG_USER_ONLY
896 static S390CPUModel applied_model;
897 static bool applied;
900 * We have the same model for all VCPUs. KVM can only be configured before
901 * any VCPUs are defined in KVM.
903 if (applied) {
904 if (model && memcmp(&applied_model, model, sizeof(S390CPUModel))) {
905 error_setg(errp, "Mixed CPU models are not supported on s390x.");
907 return;
910 if (kvm_enabled()) {
911 kvm_s390_apply_cpu_model(model, errp);
914 if (!*errp) {
915 applied = true;
916 if (model) {
917 applied_model = *model;
920 #endif
923 void s390_realize_cpu_model(CPUState *cs, Error **errp)
925 S390CPUClass *xcc = S390_CPU_GET_CLASS(cs);
926 S390CPU *cpu = S390_CPU(cs);
927 const S390CPUModel *max_model;
929 if (xcc->kvm_required && !kvm_enabled()) {
930 error_setg(errp, "CPU definition requires KVM");
931 return;
934 if (!cpu->model) {
935 /* no host model support -> perform compatibility stuff */
936 apply_cpu_model(NULL, errp);
937 return;
940 max_model = get_max_cpu_model(errp);
941 if (*errp) {
942 error_prepend(errp, "CPU models are not available: ");
943 return;
946 /* copy over properties that can vary */
947 cpu->model->lowest_ibc = max_model->lowest_ibc;
948 cpu->model->cpu_id = max_model->cpu_id;
949 cpu->model->cpu_id_format = max_model->cpu_id_format;
950 cpu->model->cpu_ver = max_model->cpu_ver;
952 check_consistency(cpu->model);
953 check_compatibility(max_model, cpu->model, errp);
954 if (*errp) {
955 return;
958 apply_cpu_model(cpu->model, errp);
960 #if !defined(CONFIG_USER_ONLY)
961 cpu->env.cpuid = s390_cpuid_from_cpu_model(cpu->model);
962 if (tcg_enabled()) {
963 /* basic mode, write the cpu address into the first 4 bit of the ID */
964 cpu->env.cpuid = deposit64(cpu->env.cpuid, 54, 4, cpu->env.core_id);
966 #endif
969 static void get_feature(Object *obj, Visitor *v, const char *name,
970 void *opaque, Error **errp)
972 S390Feat feat = (S390Feat) opaque;
973 S390CPU *cpu = S390_CPU(obj);
974 bool value;
976 if (!cpu->model) {
977 error_setg(errp, "Details about the host CPU model are not available, "
978 "features cannot be queried.");
979 return;
982 value = test_bit(feat, cpu->model->features);
983 visit_type_bool(v, name, &value, errp);
986 static void set_feature(Object *obj, Visitor *v, const char *name,
987 void *opaque, Error **errp)
989 S390Feat feat = (S390Feat) opaque;
990 DeviceState *dev = DEVICE(obj);
991 S390CPU *cpu = S390_CPU(obj);
992 bool value;
994 if (dev->realized) {
995 error_setg(errp, "Attempt to set property '%s' on '%s' after "
996 "it was realized", name, object_get_typename(obj));
997 return;
998 } else if (!cpu->model) {
999 error_setg(errp, "Details about the host CPU model are not available, "
1000 "features cannot be changed.");
1001 return;
1004 visit_type_bool(v, name, &value, errp);
1005 if (*errp) {
1006 return;
1008 if (value) {
1009 if (!test_bit(feat, cpu->model->def->full_feat)) {
1010 error_setg(errp, "Feature '%s' is not available for CPU model '%s',"
1011 " it was introduced with later models.",
1012 name, cpu->model->def->name);
1013 return;
1015 set_bit(feat, cpu->model->features);
1016 } else {
1017 clear_bit(feat, cpu->model->features);
1021 static void get_feature_group(Object *obj, Visitor *v, const char *name,
1022 void *opaque, Error **errp)
1024 S390FeatGroup group = (S390FeatGroup) opaque;
1025 const S390FeatGroupDef *def = s390_feat_group_def(group);
1026 S390CPU *cpu = S390_CPU(obj);
1027 S390FeatBitmap tmp;
1028 bool value;
1030 if (!cpu->model) {
1031 error_setg(errp, "Details about the host CPU model are not available, "
1032 "features cannot be queried.");
1033 return;
1036 /* a group is enabled if all features are enabled */
1037 bitmap_and(tmp, cpu->model->features, def->feat, S390_FEAT_MAX);
1038 value = bitmap_equal(tmp, def->feat, S390_FEAT_MAX);
1039 visit_type_bool(v, name, &value, errp);
1042 static void set_feature_group(Object *obj, Visitor *v, const char *name,
1043 void *opaque, Error **errp)
1045 S390FeatGroup group = (S390FeatGroup) opaque;
1046 const S390FeatGroupDef *def = s390_feat_group_def(group);
1047 DeviceState *dev = DEVICE(obj);
1048 S390CPU *cpu = S390_CPU(obj);
1049 bool value;
1051 if (dev->realized) {
1052 error_setg(errp, "Attempt to set property '%s' on '%s' after "
1053 "it was realized", name, object_get_typename(obj));
1054 return;
1055 } else if (!cpu->model) {
1056 error_setg(errp, "Details about the host CPU model are not available, "
1057 "features cannot be changed.");
1058 return;
1061 visit_type_bool(v, name, &value, errp);
1062 if (*errp) {
1063 return;
1065 if (value) {
1066 /* groups are added in one shot, so an intersect is sufficient */
1067 if (!bitmap_intersects(def->feat, cpu->model->def->full_feat,
1068 S390_FEAT_MAX)) {
1069 error_setg(errp, "Group '%s' is not available for CPU model '%s',"
1070 " it was introduced with later models.",
1071 name, cpu->model->def->name);
1072 return;
1074 bitmap_or(cpu->model->features, cpu->model->features, def->feat,
1075 S390_FEAT_MAX);
1076 } else {
1077 bitmap_andnot(cpu->model->features, cpu->model->features, def->feat,
1078 S390_FEAT_MAX);
1082 void s390_cpu_model_register_props(Object *obj)
1084 S390FeatGroup group;
1085 S390Feat feat;
1087 for (feat = 0; feat < S390_FEAT_MAX; feat++) {
1088 const S390FeatDef *def = s390_feat_def(feat);
1089 object_property_add(obj, def->name, "bool", get_feature,
1090 set_feature, NULL, (void *) feat, NULL);
1091 object_property_set_description(obj, def->name, def->desc , NULL);
1093 for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
1094 const S390FeatGroupDef *def = s390_feat_group_def(group);
1095 object_property_add(obj, def->name, "bool", get_feature_group,
1096 set_feature_group, NULL, (void *) group, NULL);
1097 object_property_set_description(obj, def->name, def->desc , NULL);
1101 static void s390_cpu_model_initfn(Object *obj)
1103 S390CPU *cpu = S390_CPU(obj);
1104 S390CPUClass *xcc = S390_CPU_GET_CLASS(cpu);
1106 cpu->model = g_malloc0(sizeof(*cpu->model));
1107 /* copy the model, so we can modify it */
1108 cpu->model->def = xcc->cpu_def;
1109 if (xcc->is_static) {
1110 /* base model - features will never change */
1111 bitmap_copy(cpu->model->features, cpu->model->def->base_feat,
1112 S390_FEAT_MAX);
1113 } else {
1114 /* latest model - features can change */
1115 bitmap_copy(cpu->model->features,
1116 cpu->model->def->default_feat, S390_FEAT_MAX);
1120 static S390CPUDef s390_qemu_cpu_def;
1121 static S390CPUModel s390_qemu_cpu_model;
1123 /* Set the qemu CPU model (on machine initialization). Must not be called
1124 * once CPUs have been created.
1126 void s390_set_qemu_cpu_model(uint16_t type, uint8_t gen, uint8_t ec_ga,
1127 const S390FeatInit feat_init)
1129 const S390CPUDef *def = s390_find_cpu_def(type, gen, ec_ga, NULL);
1131 g_assert(def);
1132 g_assert(QTAILQ_EMPTY_RCU(&cpus));
1134 /* TCG emulates some features that can usually not be enabled with
1135 * the emulated machine generation. Make sure they can be enabled
1136 * when using the QEMU model by adding them to full_feat. We have
1137 * to copy the definition to do that.
1139 memcpy(&s390_qemu_cpu_def, def, sizeof(s390_qemu_cpu_def));
1140 bitmap_or(s390_qemu_cpu_def.full_feat, s390_qemu_cpu_def.full_feat,
1141 qemu_max_cpu_feat, S390_FEAT_MAX);
1143 /* build the CPU model */
1144 s390_qemu_cpu_model.def = &s390_qemu_cpu_def;
1145 bitmap_zero(s390_qemu_cpu_model.features, S390_FEAT_MAX);
1146 s390_init_feat_bitmap(feat_init, s390_qemu_cpu_model.features);
1149 static void s390_qemu_cpu_model_initfn(Object *obj)
1151 S390CPU *cpu = S390_CPU(obj);
1153 cpu->model = g_malloc0(sizeof(*cpu->model));
1154 /* copy the CPU model so we can modify it */
1155 memcpy(cpu->model, &s390_qemu_cpu_model, sizeof(*cpu->model));
1158 static void s390_max_cpu_model_initfn(Object *obj)
1160 const S390CPUModel *max_model;
1161 S390CPU *cpu = S390_CPU(obj);
1162 Error *local_err = NULL;
1164 if (kvm_enabled() && !kvm_s390_cpu_models_supported()) {
1165 /* "max" and "host" always work, even without CPU model support */
1166 return;
1169 max_model = get_max_cpu_model(&local_err);
1170 if (local_err) {
1171 /* we expect errors only under KVM, when actually querying the kernel */
1172 g_assert(kvm_enabled());
1173 error_report_err(local_err);
1174 /* fallback to unsupported CPU models */
1175 return;
1178 cpu->model = g_new(S390CPUModel, 1);
1179 /* copy the CPU model so we can modify it */
1180 memcpy(cpu->model, max_model, sizeof(*cpu->model));
1183 static void s390_cpu_model_finalize(Object *obj)
1185 S390CPU *cpu = S390_CPU(obj);
1187 g_free(cpu->model);
1188 cpu->model = NULL;
1191 static bool get_is_migration_safe(Object *obj, Error **errp)
1193 return S390_CPU_GET_CLASS(obj)->is_migration_safe;
1196 static bool get_is_static(Object *obj, Error **errp)
1198 return S390_CPU_GET_CLASS(obj)->is_static;
1201 static char *get_description(Object *obj, Error **errp)
1203 return g_strdup(S390_CPU_GET_CLASS(obj)->desc);
1206 void s390_cpu_model_class_register_props(ObjectClass *oc)
1208 object_class_property_add_bool(oc, "migration-safe", get_is_migration_safe,
1209 NULL, NULL);
1210 object_class_property_add_bool(oc, "static", get_is_static,
1211 NULL, NULL);
1212 object_class_property_add_str(oc, "description", get_description, NULL,
1213 NULL);
1216 #ifdef CONFIG_KVM
1217 static void s390_host_cpu_model_class_init(ObjectClass *oc, void *data)
1219 S390CPUClass *xcc = S390_CPU_CLASS(oc);
1221 xcc->kvm_required = true;
1222 xcc->desc = "KVM only: All recognized features";
1224 #endif
1226 static void s390_base_cpu_model_class_init(ObjectClass *oc, void *data)
1228 S390CPUClass *xcc = S390_CPU_CLASS(oc);
1230 /* all base models are migration safe */
1231 xcc->cpu_def = (const S390CPUDef *) data;
1232 xcc->is_migration_safe = true;
1233 xcc->is_static = true;
1234 xcc->desc = xcc->cpu_def->desc;
1237 static void s390_cpu_model_class_init(ObjectClass *oc, void *data)
1239 S390CPUClass *xcc = S390_CPU_CLASS(oc);
1241 /* model that can change between QEMU versions */
1242 xcc->cpu_def = (const S390CPUDef *) data;
1243 xcc->is_migration_safe = true;
1244 xcc->desc = xcc->cpu_def->desc;
1247 static void s390_qemu_cpu_model_class_init(ObjectClass *oc, void *data)
1249 S390CPUClass *xcc = S390_CPU_CLASS(oc);
1251 xcc->is_migration_safe = true;
1252 xcc->desc = g_strdup_printf("QEMU Virtual CPU version %s",
1253 qemu_hw_version());
1256 static void s390_max_cpu_model_class_init(ObjectClass *oc, void *data)
1258 S390CPUClass *xcc = S390_CPU_CLASS(oc);
1261 * The "max" model is neither static nor migration safe. Under KVM
1262 * it represents the "host" model. Under TCG it represents some kind of
1263 * "qemu" CPU model without compat handling and maybe with some additional
1264 * CPU features that are not yet unlocked in the "qemu" model.
1266 xcc->desc =
1267 "Enables all features supported by the accelerator in the current host";
1270 /* Generate type name for a cpu model. Caller has to free the string. */
1271 static char *s390_cpu_type_name(const char *model_name)
1273 return g_strdup_printf(S390_CPU_TYPE_NAME("%s"), model_name);
1276 /* Generate type name for a base cpu model. Caller has to free the string. */
1277 static char *s390_base_cpu_type_name(const char *model_name)
1279 return g_strdup_printf(S390_CPU_TYPE_NAME("%s-base"), model_name);
1282 ObjectClass *s390_cpu_class_by_name(const char *name)
1284 char *typename = s390_cpu_type_name(name);
1285 ObjectClass *oc;
1287 oc = object_class_by_name(typename);
1288 g_free(typename);
1289 return oc;
1292 static const TypeInfo qemu_s390_cpu_type_info = {
1293 .name = S390_CPU_TYPE_NAME("qemu"),
1294 .parent = TYPE_S390_CPU,
1295 .instance_init = s390_qemu_cpu_model_initfn,
1296 .instance_finalize = s390_cpu_model_finalize,
1297 .class_init = s390_qemu_cpu_model_class_init,
1300 static const TypeInfo max_s390_cpu_type_info = {
1301 .name = S390_CPU_TYPE_NAME("max"),
1302 .parent = TYPE_S390_CPU,
1303 .instance_init = s390_max_cpu_model_initfn,
1304 .instance_finalize = s390_cpu_model_finalize,
1305 .class_init = s390_max_cpu_model_class_init,
1308 #ifdef CONFIG_KVM
1309 static const TypeInfo host_s390_cpu_type_info = {
1310 .name = S390_CPU_TYPE_NAME("host"),
1311 .parent = S390_CPU_TYPE_NAME("max"),
1312 .class_init = s390_host_cpu_model_class_init,
1314 #endif
1316 static void init_ignored_base_feat(void)
1318 static const int feats[] = {
1319 /* MSA subfunctions that could not be available on certain machines */
1320 S390_FEAT_KMAC_DEA,
1321 S390_FEAT_KMAC_TDEA_128,
1322 S390_FEAT_KMAC_TDEA_192,
1323 S390_FEAT_KMC_DEA,
1324 S390_FEAT_KMC_TDEA_128,
1325 S390_FEAT_KMC_TDEA_192,
1326 S390_FEAT_KM_DEA,
1327 S390_FEAT_KM_TDEA_128,
1328 S390_FEAT_KM_TDEA_192,
1329 S390_FEAT_KIMD_SHA_1,
1330 S390_FEAT_KLMD_SHA_1,
1331 /* CSSKE is deprecated on newer generations */
1332 S390_FEAT_CONDITIONAL_SSKE,
1334 int i;
1336 for (i = 0; i < ARRAY_SIZE(feats); i++) {
1337 set_bit(feats[i], ignored_base_feat);
1341 static void register_types(void)
1343 static const S390FeatInit qemu_latest_init = { S390_FEAT_LIST_QEMU_LATEST };
1344 int i;
1346 init_ignored_base_feat();
1348 /* init all bitmaps from gnerated data initially */
1349 s390_init_feat_bitmap(qemu_max_cpu_feat_init, qemu_max_cpu_feat);
1350 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
1351 s390_init_feat_bitmap(s390_cpu_defs[i].base_init,
1352 s390_cpu_defs[i].base_feat);
1353 s390_init_feat_bitmap(s390_cpu_defs[i].default_init,
1354 s390_cpu_defs[i].default_feat);
1355 s390_init_feat_bitmap(s390_cpu_defs[i].full_init,
1356 s390_cpu_defs[i].full_feat);
1359 /* initialize the qemu model with latest definition */
1360 s390_set_qemu_cpu_model(QEMU_MAX_CPU_TYPE, QEMU_MAX_CPU_GEN,
1361 QEMU_MAX_CPU_EC_GA, qemu_latest_init);
1363 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
1364 char *base_name = s390_base_cpu_type_name(s390_cpu_defs[i].name);
1365 TypeInfo ti_base = {
1366 .name = base_name,
1367 .parent = TYPE_S390_CPU,
1368 .instance_init = s390_cpu_model_initfn,
1369 .instance_finalize = s390_cpu_model_finalize,
1370 .class_init = s390_base_cpu_model_class_init,
1371 .class_data = (void *) &s390_cpu_defs[i],
1373 char *name = s390_cpu_type_name(s390_cpu_defs[i].name);
1374 TypeInfo ti = {
1375 .name = name,
1376 .parent = TYPE_S390_CPU,
1377 .instance_init = s390_cpu_model_initfn,
1378 .instance_finalize = s390_cpu_model_finalize,
1379 .class_init = s390_cpu_model_class_init,
1380 .class_data = (void *) &s390_cpu_defs[i],
1383 type_register_static(&ti_base);
1384 type_register_static(&ti);
1385 g_free(base_name);
1386 g_free(name);
1389 type_register_static(&qemu_s390_cpu_type_info);
1390 type_register_static(&max_s390_cpu_type_info);
1391 #ifdef CONFIG_KVM
1392 type_register_static(&host_s390_cpu_type_info);
1393 #endif
1396 type_init(register_types)