xref: /openbmc/qemu/target/s390x/cpu_models.c (revision 44b1ff31)
1 /*
2  * CPU models for s390x
3  *
4  * Copyright 2016 IBM Corp.
5  *
6  * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
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 "internal.h"
16 #include "kvm_s390x.h"
17 #include "sysemu/kvm.h"
18 #include "gen-features.h"
19 #include "qapi/error.h"
20 #include "qapi/visitor.h"
21 #include "qemu/error-report.h"
22 #include "qapi/qmp/qerror.h"
23 #include "qapi/qobject-input-visitor.h"
24 #include "qapi/qmp/qbool.h"
25 #ifndef CONFIG_USER_ONLY
26 #include "sysemu/arch_init.h"
27 #endif
28 
29 #define CPUDEF_INIT(_type, _gen, _ec_ga, _mha_pow, _hmfai, _name, _desc) \
30     {                                                                    \
31         .name = _name,                                                   \
32         .type = _type,                                                   \
33         .gen = _gen,                                                     \
34         .ec_ga = _ec_ga,                                                 \
35         .mha_pow = _mha_pow,                                             \
36         .hmfai = _hmfai,                                                 \
37         .desc = _desc,                                                   \
38         .base_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _BASE },  \
39         .default_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _DEFAULT },  \
40         .full_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _FULL },  \
41     }
42 
43 /*
44  * CPU definiton list in order of release. For now, base features of a
45  * following release are always a subset of base features of the previous
46  * release. Same is correct for the other feature sets.
47  * A BC release always follows the corresponding EC release.
48  */
49 static S390CPUDef s390_cpu_defs[] = {
50     CPUDEF_INIT(0x2064, 7, 1, 38, 0x00000000U, "z900", "IBM zSeries 900 GA1"),
51     CPUDEF_INIT(0x2064, 7, 2, 38, 0x00000000U, "z900.2", "IBM zSeries 900 GA2"),
52     CPUDEF_INIT(0x2064, 7, 3, 38, 0x00000000U, "z900.3", "IBM zSeries 900 GA3"),
53     CPUDEF_INIT(0x2066, 7, 3, 38, 0x00000000U, "z800", "IBM zSeries 800 GA1"),
54     CPUDEF_INIT(0x2084, 8, 1, 38, 0x00000000U, "z990", "IBM zSeries 990 GA1"),
55     CPUDEF_INIT(0x2084, 8, 2, 38, 0x00000000U, "z990.2", "IBM zSeries 990 GA2"),
56     CPUDEF_INIT(0x2084, 8, 3, 38, 0x00000000U, "z990.3", "IBM zSeries 990 GA3"),
57     CPUDEF_INIT(0x2086, 8, 3, 38, 0x00000000U, "z890", "IBM zSeries 880 GA1"),
58     CPUDEF_INIT(0x2084, 8, 4, 38, 0x00000000U, "z990.4", "IBM zSeries 990 GA4"),
59     CPUDEF_INIT(0x2086, 8, 4, 38, 0x00000000U, "z890.2", "IBM zSeries 880 GA2"),
60     CPUDEF_INIT(0x2084, 8, 5, 38, 0x00000000U, "z990.5", "IBM zSeries 990 GA5"),
61     CPUDEF_INIT(0x2086, 8, 5, 38, 0x00000000U, "z890.3", "IBM zSeries 880 GA3"),
62     CPUDEF_INIT(0x2094, 9, 1, 40, 0x00000000U, "z9EC", "IBM System z9 EC GA1"),
63     CPUDEF_INIT(0x2094, 9, 2, 40, 0x00000000U, "z9EC.2", "IBM System z9 EC GA2"),
64     CPUDEF_INIT(0x2096, 9, 2, 40, 0x00000000U, "z9BC", "IBM System z9 BC GA1"),
65     CPUDEF_INIT(0x2094, 9, 3, 40, 0x00000000U, "z9EC.3", "IBM System z9 EC GA3"),
66     CPUDEF_INIT(0x2096, 9, 3, 40, 0x00000000U, "z9BC.2", "IBM System z9 BC GA2"),
67     CPUDEF_INIT(0x2097, 10, 1, 43, 0x00000000U, "z10EC", "IBM System z10 EC GA1"),
68     CPUDEF_INIT(0x2097, 10, 2, 43, 0x00000000U, "z10EC.2", "IBM System z10 EC GA2"),
69     CPUDEF_INIT(0x2098, 10, 2, 43, 0x00000000U, "z10BC", "IBM System z10 BC GA1"),
70     CPUDEF_INIT(0x2097, 10, 3, 43, 0x00000000U, "z10EC.3", "IBM System z10 EC GA3"),
71     CPUDEF_INIT(0x2098, 10, 3, 43, 0x00000000U, "z10BC.2", "IBM System z10 BC GA2"),
72     CPUDEF_INIT(0x2817, 11, 1, 44, 0x08000000U, "z196", "IBM zEnterprise 196 GA1"),
73     CPUDEF_INIT(0x2817, 11, 2, 44, 0x08000000U, "z196.2", "IBM zEnterprise 196 GA2"),
74     CPUDEF_INIT(0x2818, 11, 2, 44, 0x08000000U, "z114", "IBM zEnterprise 114 GA1"),
75     CPUDEF_INIT(0x2827, 12, 1, 44, 0x08000000U, "zEC12", "IBM zEnterprise EC12 GA1"),
76     CPUDEF_INIT(0x2827, 12, 2, 44, 0x08000000U, "zEC12.2", "IBM zEnterprise EC12 GA2"),
77     CPUDEF_INIT(0x2828, 12, 2, 44, 0x08000000U, "zBC12", "IBM zEnterprise BC12 GA1"),
78     CPUDEF_INIT(0x2964, 13, 1, 47, 0x08000000U, "z13", "IBM z13 GA1"),
79     CPUDEF_INIT(0x2964, 13, 2, 47, 0x08000000U, "z13.2", "IBM z13 GA2"),
80     CPUDEF_INIT(0x2965, 13, 2, 47, 0x08000000U, "z13s", "IBM z13s GA1"),
81     CPUDEF_INIT(0x3906, 14, 1, 47, 0x08000000U, "z14", "IBM z14 GA1"),
82 };
83 
84 /* features part of a base model but not relevant for finding a base model */
85 S390FeatBitmap ignored_base_feat;
86 
87 void s390_cpudef_featoff(uint8_t gen, uint8_t ec_ga, S390Feat feat)
88 {
89     const S390CPUDef *def;
90 
91     def = s390_find_cpu_def(0, gen, ec_ga, NULL);
92     clear_bit(feat, (unsigned long *)&def->default_feat);
93 }
94 
95 void s390_cpudef_featoff_greater(uint8_t gen, uint8_t ec_ga, S390Feat feat)
96 {
97     int i;
98 
99     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
100         const S390CPUDef *def = &s390_cpu_defs[i];
101 
102         if (def->gen < gen) {
103             continue;
104         }
105         if (def->gen == gen && def->ec_ga < ec_ga) {
106             continue;
107         }
108 
109         clear_bit(feat, (unsigned long *)&def->default_feat);
110     }
111 }
112 
113 uint32_t s390_get_hmfai(void)
114 {
115     static S390CPU *cpu;
116 
117     if (!cpu) {
118         cpu = S390_CPU(qemu_get_cpu(0));
119     }
120 
121     if (!cpu || !cpu->model) {
122         return 0;
123     }
124     return cpu->model->def->hmfai;
125 }
126 
127 uint8_t s390_get_mha_pow(void)
128 {
129     static S390CPU *cpu;
130 
131     if (!cpu) {
132         cpu = S390_CPU(qemu_get_cpu(0));
133     }
134 
135     if (!cpu || !cpu->model) {
136         return 0;
137     }
138     return cpu->model->def->mha_pow;
139 }
140 
141 uint32_t s390_get_ibc_val(void)
142 {
143     uint16_t unblocked_ibc, lowest_ibc;
144     static S390CPU *cpu;
145 
146     if (!cpu) {
147         cpu = S390_CPU(qemu_get_cpu(0));
148     }
149 
150     if (!cpu || !cpu->model) {
151         return 0;
152     }
153     unblocked_ibc = s390_ibc_from_cpu_model(cpu->model);
154     lowest_ibc = cpu->model->lowest_ibc;
155     /* the lowest_ibc always has to be <= unblocked_ibc */
156     if (!lowest_ibc || lowest_ibc > unblocked_ibc) {
157         return 0;
158     }
159     return ((uint32_t) lowest_ibc << 16) | unblocked_ibc;
160 }
161 
162 void s390_get_feat_block(S390FeatType type, uint8_t *data)
163 {
164     static S390CPU *cpu;
165 
166     if (!cpu) {
167         cpu = S390_CPU(qemu_get_cpu(0));
168     }
169 
170     if (!cpu || !cpu->model) {
171         return;
172     }
173     s390_fill_feat_block(cpu->model->features, type, data);
174 }
175 
176 bool s390_has_feat(S390Feat feat)
177 {
178     static S390CPU *cpu;
179 
180     if (!cpu) {
181         cpu = S390_CPU(qemu_get_cpu(0));
182     }
183 
184     if (!cpu || !cpu->model) {
185 #ifdef CONFIG_KVM
186         if (kvm_enabled()) {
187             if (feat == S390_FEAT_VECTOR) {
188                 return kvm_check_extension(kvm_state,
189                                            KVM_CAP_S390_VECTOR_REGISTERS);
190             }
191             if (feat == S390_FEAT_RUNTIME_INSTRUMENTATION) {
192                 return kvm_s390_get_ri();
193             }
194             if (feat == S390_FEAT_MSA_EXT_3) {
195                 return true;
196             }
197         }
198 #endif
199         if (feat == S390_FEAT_ZPCI) {
200             return true;
201         }
202         return 0;
203     }
204     return test_bit(feat, cpu->model->features);
205 }
206 
207 uint8_t s390_get_gen_for_cpu_type(uint16_t type)
208 {
209     int i;
210 
211     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
212         if (s390_cpu_defs[i].type == type) {
213             return s390_cpu_defs[i].gen;
214         }
215     }
216     return 0;
217 }
218 
219 const S390CPUDef *s390_find_cpu_def(uint16_t type, uint8_t gen, uint8_t ec_ga,
220                                     S390FeatBitmap features)
221 {
222     const S390CPUDef *last_compatible = NULL;
223     const S390CPUDef *matching_cpu_type = NULL;
224     int i;
225 
226     if (!gen) {
227         ec_ga = 0;
228     }
229     if (!gen && type) {
230         gen = s390_get_gen_for_cpu_type(type);
231     }
232 
233     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
234         const S390CPUDef *def = &s390_cpu_defs[i];
235         S390FeatBitmap missing;
236 
237         /* don't even try newer generations if we know the generation */
238         if (gen) {
239             if (def->gen > gen) {
240                 break;
241             } else if (def->gen == gen && ec_ga && def->ec_ga > ec_ga) {
242                 break;
243             }
244         }
245 
246         if (features) {
247             /* see if the model satisfies the minimum features */
248             bitmap_andnot(missing, def->base_feat, features, S390_FEAT_MAX);
249             /*
250              * Ignore certain features that are in the base model, but not
251              * relevant for the search (esp. MSA subfunctions).
252              */
253             bitmap_andnot(missing, missing, ignored_base_feat, S390_FEAT_MAX);
254             if (!bitmap_empty(missing, S390_FEAT_MAX)) {
255                 break;
256             }
257         }
258 
259         /* stop the search if we found the exact model */
260         if (def->type == type && def->ec_ga == ec_ga) {
261             return def;
262         }
263         /* remember if we've at least seen one with the same cpu type */
264         if (def->type == type) {
265             matching_cpu_type = def;
266         }
267         last_compatible = def;
268     }
269     /* prefer the model with the same cpu type, esp. don't take the BC for EC */
270     if (matching_cpu_type) {
271         return matching_cpu_type;
272     }
273     return last_compatible;
274 }
275 
276 static void s390_print_cpu_model_list_entry(gpointer data, gpointer user_data)
277 {
278     CPUListState *s = user_data;
279     const S390CPUClass *scc = S390_CPU_CLASS((ObjectClass *)data);
280     char *name = g_strdup(object_class_get_name((ObjectClass *)data));
281     const char *details = "";
282 
283     if (scc->is_static) {
284         details = "(static, migration-safe)";
285     } else if (scc->is_migration_safe) {
286         details = "(migration-safe)";
287     }
288 
289     /* strip off the -s390-cpu */
290     g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
291     (*s->cpu_fprintf)(s->file, "s390 %-15s %-35s %s\n", name, scc->desc,
292                       details);
293     g_free(name);
294 }
295 
296 static gint s390_cpu_list_compare(gconstpointer a, gconstpointer b)
297 {
298     const S390CPUClass *cc_a = S390_CPU_CLASS((ObjectClass *)a);
299     const S390CPUClass *cc_b = S390_CPU_CLASS((ObjectClass *)b);
300     const char *name_a = object_class_get_name((ObjectClass *)a);
301     const char *name_b = object_class_get_name((ObjectClass *)b);
302 
303     /* move qemu and host to the top of the list, qemu first, host second */
304     if (name_a[0] == 'q') {
305         return -1;
306     } else if (name_b[0] == 'q') {
307         return 1;
308     } else if (name_a[0] == 'h') {
309         return -1;
310     } else if (name_b[0] == 'h') {
311         return 1;
312     }
313 
314     /* keep the same order we have in our table (sorted by release date) */
315     if (cc_a->cpu_def != cc_b->cpu_def) {
316         return cc_a->cpu_def - cc_b->cpu_def;
317     }
318 
319     /* exact same definition - list base model first */
320     return cc_a->is_static ? -1 : 1;
321 }
322 
323 void s390_cpu_list(FILE *f, fprintf_function print)
324 {
325     CPUListState s = {
326         .file = f,
327         .cpu_fprintf = print,
328     };
329     S390FeatGroup group;
330     S390Feat feat;
331     GSList *list;
332 
333     list = object_class_get_list(TYPE_S390_CPU, false);
334     list = g_slist_sort(list, s390_cpu_list_compare);
335     g_slist_foreach(list, s390_print_cpu_model_list_entry, &s);
336     g_slist_free(list);
337 
338     (*print)(f, "\nRecognized feature flags:\n");
339     for (feat = 0; feat < S390_FEAT_MAX; feat++) {
340         const S390FeatDef *def = s390_feat_def(feat);
341 
342         (*print)(f, "%-20s %-50s\n", def->name, def->desc);
343     }
344 
345     (*print)(f, "\nRecognized feature groups:\n");
346     for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
347         const S390FeatGroupDef *def = s390_feat_group_def(group);
348 
349         (*print)(f, "%-20s %-50s\n", def->name, def->desc);
350     }
351 }
352 
353 static S390CPUModel *get_max_cpu_model(Error **errp);
354 
355 #ifndef CONFIG_USER_ONLY
356 static void list_add_feat(const char *name, void *opaque);
357 
358 static void check_unavailable_features(const S390CPUModel *max_model,
359                                        const S390CPUModel *model,
360                                        strList **unavailable)
361 {
362     S390FeatBitmap missing;
363 
364     /* check general model compatibility */
365     if (max_model->def->gen < model->def->gen ||
366         (max_model->def->gen == model->def->gen &&
367          max_model->def->ec_ga < model->def->ec_ga)) {
368         list_add_feat("type", unavailable);
369     }
370 
371     /* detect missing features if any to properly report them */
372     bitmap_andnot(missing, model->features, max_model->features,
373                   S390_FEAT_MAX);
374     if (!bitmap_empty(missing, S390_FEAT_MAX)) {
375         s390_feat_bitmap_to_ascii(missing, unavailable, list_add_feat);
376     }
377 }
378 
379 struct CpuDefinitionInfoListData {
380     CpuDefinitionInfoList *list;
381     S390CPUModel *model;
382 };
383 
384 static void create_cpu_model_list(ObjectClass *klass, void *opaque)
385 {
386     struct CpuDefinitionInfoListData *cpu_list_data = opaque;
387     CpuDefinitionInfoList **cpu_list = &cpu_list_data->list;
388     CpuDefinitionInfoList *entry;
389     CpuDefinitionInfo *info;
390     char *name = g_strdup(object_class_get_name(klass));
391     S390CPUClass *scc = S390_CPU_CLASS(klass);
392 
393     /* strip off the -s390-cpu */
394     g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
395     info = g_malloc0(sizeof(*info));
396     info->name = name;
397     info->has_migration_safe = true;
398     info->migration_safe = scc->is_migration_safe;
399     info->q_static = scc->is_static;
400     info->q_typename = g_strdup(object_class_get_name(klass));
401     /* check for unavailable features */
402     if (cpu_list_data->model) {
403         Object *obj;
404         S390CPU *sc;
405         obj = object_new(object_class_get_name(klass));
406         sc = S390_CPU(obj);
407         if (sc->model) {
408             info->has_unavailable_features = true;
409             check_unavailable_features(cpu_list_data->model, sc->model,
410                                        &info->unavailable_features);
411         }
412         object_unref(obj);
413     }
414 
415     entry = g_malloc0(sizeof(*entry));
416     entry->value = info;
417     entry->next = *cpu_list;
418     *cpu_list = entry;
419 }
420 
421 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
422 {
423     struct CpuDefinitionInfoListData list_data = {
424         .list = NULL,
425     };
426 
427     list_data.model = get_max_cpu_model(errp);
428     if (*errp) {
429         error_free(*errp);
430         *errp = NULL;
431     }
432 
433     object_class_foreach(create_cpu_model_list, TYPE_S390_CPU, false,
434                          &list_data);
435 
436     return list_data.list;
437 }
438 
439 static void cpu_model_from_info(S390CPUModel *model, const CpuModelInfo *info,
440                                 Error **errp)
441 {
442     const QDict *qdict = NULL;
443     const QDictEntry *e;
444     Visitor *visitor;
445     ObjectClass *oc;
446     S390CPU *cpu;
447     Object *obj;
448 
449     if (info->props) {
450         qdict = qobject_to_qdict(info->props);
451         if (!qdict) {
452             error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
453             return;
454         }
455     }
456 
457     oc = cpu_class_by_name(TYPE_S390_CPU, info->name);
458     if (!oc) {
459         error_setg(errp, "The CPU definition \'%s\' is unknown.", info->name);
460         return;
461     }
462     if (S390_CPU_CLASS(oc)->kvm_required && !kvm_enabled()) {
463         error_setg(errp, "The CPU definition '%s' requires KVM", info->name);
464         return;
465     }
466     obj = object_new(object_class_get_name(oc));
467     cpu = S390_CPU(obj);
468 
469     if (!cpu->model) {
470         error_setg(errp, "Details about the host CPU model are not available, "
471                          "it cannot be used.");
472         object_unref(obj);
473         return;
474     }
475 
476     if (qdict) {
477         visitor = qobject_input_visitor_new(info->props);
478         visit_start_struct(visitor, NULL, NULL, 0, errp);
479         if (*errp) {
480             object_unref(obj);
481             return;
482         }
483         for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
484             object_property_set(obj, visitor, e->key, errp);
485             if (*errp) {
486                 break;
487             }
488         }
489         if (!*errp) {
490             visit_check_struct(visitor, errp);
491         }
492         visit_end_struct(visitor, NULL);
493         visit_free(visitor);
494         if (*errp) {
495             object_unref(obj);
496             return;
497         }
498     }
499 
500     /* copy the model and throw the cpu away */
501     memcpy(model, cpu->model, sizeof(*model));
502     object_unref(obj);
503 }
504 
505 static void qdict_add_disabled_feat(const char *name, void *opaque)
506 {
507     qdict_put_bool(opaque, name, false);
508 }
509 
510 static void qdict_add_enabled_feat(const char *name, void *opaque)
511 {
512     qdict_put_bool(opaque, name, true);
513 }
514 
515 /* convert S390CPUDef into a static CpuModelInfo */
516 static void cpu_info_from_model(CpuModelInfo *info, const S390CPUModel *model,
517                                 bool delta_changes)
518 {
519     QDict *qdict = qdict_new();
520     S390FeatBitmap bitmap;
521 
522     /* always fallback to the static base model */
523     info->name = g_strdup_printf("%s-base", model->def->name);
524 
525     if (delta_changes) {
526         /* features deleted from the base feature set */
527         bitmap_andnot(bitmap, model->def->base_feat, model->features,
528                       S390_FEAT_MAX);
529         if (!bitmap_empty(bitmap, S390_FEAT_MAX)) {
530             s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_disabled_feat);
531         }
532 
533         /* features added to the base feature set */
534         bitmap_andnot(bitmap, model->features, model->def->base_feat,
535                       S390_FEAT_MAX);
536         if (!bitmap_empty(bitmap, S390_FEAT_MAX)) {
537             s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_enabled_feat);
538         }
539     } else {
540         /* expand all features */
541         s390_feat_bitmap_to_ascii(model->features, qdict,
542                                   qdict_add_enabled_feat);
543         bitmap_complement(bitmap, model->features, S390_FEAT_MAX);
544         s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_disabled_feat);
545     }
546 
547     if (!qdict_size(qdict)) {
548         QDECREF(qdict);
549     } else {
550         info->props = QOBJECT(qdict);
551         info->has_props = true;
552     }
553 }
554 
555 CpuModelExpansionInfo *arch_query_cpu_model_expansion(CpuModelExpansionType type,
556                                                       CpuModelInfo *model,
557                                                       Error **errp)
558 {
559     CpuModelExpansionInfo *expansion_info = NULL;
560     S390CPUModel s390_model;
561     bool delta_changes = false;
562 
563     /* convert it to our internal representation */
564     cpu_model_from_info(&s390_model, model, errp);
565     if (*errp) {
566         return NULL;
567     }
568 
569     if (type == CPU_MODEL_EXPANSION_TYPE_STATIC) {
570         delta_changes = true;
571     } else if (type != CPU_MODEL_EXPANSION_TYPE_FULL) {
572         error_setg(errp, "The requested expansion type is not supported.");
573         return NULL;
574     }
575 
576     /* convert it back to a static representation */
577     expansion_info = g_malloc0(sizeof(*expansion_info));
578     expansion_info->model = g_malloc0(sizeof(*expansion_info->model));
579     cpu_info_from_model(expansion_info->model, &s390_model, delta_changes);
580     return expansion_info;
581 }
582 
583 static void list_add_feat(const char *name, void *opaque)
584 {
585     strList **last = (strList **) opaque;
586     strList *entry;
587 
588     entry = g_malloc0(sizeof(*entry));
589     entry->value = g_strdup(name);
590     entry->next = *last;
591     *last = entry;
592 }
593 
594 CpuModelCompareInfo *arch_query_cpu_model_comparison(CpuModelInfo *infoa,
595                                                      CpuModelInfo *infob,
596                                                      Error **errp)
597 {
598     CpuModelCompareResult feat_result, gen_result;
599     CpuModelCompareInfo *compare_info;
600     S390FeatBitmap missing, added;
601     S390CPUModel modela, modelb;
602 
603     /* convert both models to our internal representation */
604     cpu_model_from_info(&modela, infoa, errp);
605     if (*errp) {
606         return NULL;
607     }
608     cpu_model_from_info(&modelb, infob, errp);
609     if (*errp) {
610         return NULL;
611     }
612     compare_info = g_malloc0(sizeof(*compare_info));
613 
614     /* check the cpu generation and ga level */
615     if (modela.def->gen == modelb.def->gen) {
616         if (modela.def->ec_ga == modelb.def->ec_ga) {
617             /* ec and corresponding bc are identical */
618             gen_result = CPU_MODEL_COMPARE_RESULT_IDENTICAL;
619         } else if (modela.def->ec_ga < modelb.def->ec_ga) {
620             gen_result = CPU_MODEL_COMPARE_RESULT_SUBSET;
621         } else {
622             gen_result = CPU_MODEL_COMPARE_RESULT_SUPERSET;
623         }
624     } else if (modela.def->gen < modelb.def->gen) {
625         gen_result = CPU_MODEL_COMPARE_RESULT_SUBSET;
626     } else {
627         gen_result = CPU_MODEL_COMPARE_RESULT_SUPERSET;
628     }
629     if (gen_result != CPU_MODEL_COMPARE_RESULT_IDENTICAL) {
630         /* both models cannot be made identical */
631         list_add_feat("type", &compare_info->responsible_properties);
632     }
633 
634     /* check the feature set */
635     if (bitmap_equal(modela.features, modelb.features, S390_FEAT_MAX)) {
636         feat_result = CPU_MODEL_COMPARE_RESULT_IDENTICAL;
637     } else {
638         bitmap_andnot(missing, modela.features, modelb.features, S390_FEAT_MAX);
639         s390_feat_bitmap_to_ascii(missing,
640                                   &compare_info->responsible_properties,
641                                   list_add_feat);
642         bitmap_andnot(added, modelb.features, modela.features, S390_FEAT_MAX);
643         s390_feat_bitmap_to_ascii(added, &compare_info->responsible_properties,
644                                   list_add_feat);
645         if (bitmap_empty(missing, S390_FEAT_MAX)) {
646             feat_result = CPU_MODEL_COMPARE_RESULT_SUBSET;
647         } else if (bitmap_empty(added, S390_FEAT_MAX)) {
648             feat_result = CPU_MODEL_COMPARE_RESULT_SUPERSET;
649         } else {
650             feat_result = CPU_MODEL_COMPARE_RESULT_INCOMPATIBLE;
651         }
652     }
653 
654     /* combine the results */
655     if (gen_result == feat_result) {
656         compare_info->result = gen_result;
657     } else if (feat_result == CPU_MODEL_COMPARE_RESULT_IDENTICAL) {
658         compare_info->result = gen_result;
659     } else if (gen_result == CPU_MODEL_COMPARE_RESULT_IDENTICAL) {
660         compare_info->result = feat_result;
661     } else {
662         compare_info->result = CPU_MODEL_COMPARE_RESULT_INCOMPATIBLE;
663     }
664     return compare_info;
665 }
666 
667 CpuModelBaselineInfo *arch_query_cpu_model_baseline(CpuModelInfo *infoa,
668                                                     CpuModelInfo *infob,
669                                                     Error **errp)
670 {
671     CpuModelBaselineInfo *baseline_info;
672     S390CPUModel modela, modelb, model;
673     uint16_t cpu_type;
674     uint8_t max_gen_ga;
675     uint8_t max_gen;
676 
677     /* convert both models to our internal representation */
678     cpu_model_from_info(&modela, infoa, errp);
679     if (*errp) {
680         return NULL;
681     }
682 
683     cpu_model_from_info(&modelb, infob, errp);
684     if (*errp) {
685         return NULL;
686     }
687 
688     /* features both models support */
689     bitmap_and(model.features, modela.features, modelb.features, S390_FEAT_MAX);
690 
691     /* detect the maximum model not regarding features */
692     if (modela.def->gen == modelb.def->gen) {
693         if (modela.def->type == modelb.def->type) {
694             cpu_type = modela.def->type;
695         } else {
696             cpu_type = 0;
697         }
698         max_gen = modela.def->gen;
699         max_gen_ga = MIN(modela.def->ec_ga, modelb.def->ec_ga);
700     } else if (modela.def->gen > modelb.def->gen) {
701         cpu_type = modelb.def->type;
702         max_gen = modelb.def->gen;
703         max_gen_ga = modelb.def->ec_ga;
704     } else {
705         cpu_type = modela.def->type;
706         max_gen = modela.def->gen;
707         max_gen_ga = modela.def->ec_ga;
708     }
709 
710     model.def = s390_find_cpu_def(cpu_type, max_gen, max_gen_ga,
711                                   model.features);
712     /* strip off features not part of the max model */
713     bitmap_and(model.features, model.features, model.def->full_feat,
714                S390_FEAT_MAX);
715 
716     baseline_info = g_malloc0(sizeof(*baseline_info));
717     baseline_info->model = g_malloc0(sizeof(*baseline_info->model));
718     cpu_info_from_model(baseline_info->model, &model, true);
719     return baseline_info;
720 }
721 #endif
722 
723 static void check_consistency(const S390CPUModel *model)
724 {
725     static int dep[][2] = {
726         { S390_FEAT_IPTE_RANGE, S390_FEAT_DAT_ENH },
727         { S390_FEAT_IDTE_SEGMENT, S390_FEAT_DAT_ENH },
728         { S390_FEAT_IDTE_REGION, S390_FEAT_DAT_ENH },
729         { S390_FEAT_IDTE_REGION, S390_FEAT_IDTE_SEGMENT },
730         { S390_FEAT_LOCAL_TLB_CLEARING, S390_FEAT_DAT_ENH},
731         { S390_FEAT_LONG_DISPLACEMENT_FAST, S390_FEAT_LONG_DISPLACEMENT },
732         { S390_FEAT_DFP_FAST, S390_FEAT_DFP },
733         { S390_FEAT_TRANSACTIONAL_EXE, S390_FEAT_STFLE_49 },
734         { S390_FEAT_EDAT_2, S390_FEAT_EDAT},
735         { S390_FEAT_MSA_EXT_5, S390_FEAT_KIMD_SHA_512 },
736         { S390_FEAT_MSA_EXT_5, S390_FEAT_KLMD_SHA_512 },
737         { S390_FEAT_MSA_EXT_4, S390_FEAT_MSA_EXT_3 },
738         { S390_FEAT_SIE_CMMA, S390_FEAT_CMM },
739         { S390_FEAT_SIE_CMMA, S390_FEAT_SIE_GSLS },
740         { S390_FEAT_SIE_PFMFI, S390_FEAT_EDAT },
741         { S390_FEAT_MSA_EXT_8, S390_FEAT_MSA_EXT_3 },
742         { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING },
743         { S390_FEAT_VECTOR_PACKED_DECIMAL, S390_FEAT_VECTOR },
744         { S390_FEAT_VECTOR_ENH, S390_FEAT_VECTOR },
745         { S390_FEAT_INSTRUCTION_EXEC_PROT, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 },
746         { S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2, S390_FEAT_ESOP },
747         { S390_FEAT_CMM_NT, S390_FEAT_CMM },
748         { S390_FEAT_GUARDED_STORAGE, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 },
749         { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_STORE_CLOCK_FAST },
750         { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING },
751         { S390_FEAT_SEMAPHORE_ASSIST, S390_FEAT_STFLE_49 },
752         { S390_FEAT_KIMD_SHA3_224, S390_FEAT_MSA },
753         { S390_FEAT_KIMD_SHA3_256, S390_FEAT_MSA },
754         { S390_FEAT_KIMD_SHA3_384, S390_FEAT_MSA },
755         { S390_FEAT_KIMD_SHA3_512, S390_FEAT_MSA },
756         { S390_FEAT_KIMD_SHAKE_128, S390_FEAT_MSA },
757         { S390_FEAT_KIMD_SHAKE_256, S390_FEAT_MSA },
758         { S390_FEAT_KLMD_SHA3_224, S390_FEAT_MSA },
759         { S390_FEAT_KLMD_SHA3_256, S390_FEAT_MSA },
760         { S390_FEAT_KLMD_SHA3_384, S390_FEAT_MSA },
761         { S390_FEAT_KLMD_SHA3_512, S390_FEAT_MSA },
762         { S390_FEAT_KLMD_SHAKE_128, S390_FEAT_MSA },
763         { S390_FEAT_KLMD_SHAKE_256, S390_FEAT_MSA },
764         { S390_FEAT_PRNO_TRNG_QRTCR, S390_FEAT_MSA_EXT_5 },
765         { S390_FEAT_PRNO_TRNG, S390_FEAT_MSA_EXT_5 },
766         { S390_FEAT_SIE_KSS, S390_FEAT_SIE_F2 },
767     };
768     int i;
769 
770     for (i = 0; i < ARRAY_SIZE(dep); i++) {
771         if (test_bit(dep[i][0], model->features) &&
772             !test_bit(dep[i][1], model->features)) {
773             warn_report("\'%s\' requires \'%s\'.",
774                         s390_feat_def(dep[i][0])->name,
775                         s390_feat_def(dep[i][1])->name);
776         }
777     }
778 }
779 
780 static void error_prepend_missing_feat(const char *name, void *opaque)
781 {
782     error_prepend((Error **) opaque, "%s ", name);
783 }
784 
785 static void check_compatibility(const S390CPUModel *max_model,
786                                 const S390CPUModel *model, Error **errp)
787 {
788     S390FeatBitmap missing;
789 
790     if (model->def->gen > max_model->def->gen) {
791         error_setg(errp, "Selected CPU generation is too new. Maximum "
792                    "supported model in the configuration: \'%s\'",
793                    max_model->def->name);
794         return;
795     } else if (model->def->gen == max_model->def->gen &&
796                model->def->ec_ga > max_model->def->ec_ga) {
797         error_setg(errp, "Selected CPU GA level is too new. Maximum "
798                    "supported model in the configuration: \'%s\'",
799                    max_model->def->name);
800         return;
801     }
802 
803     /* detect the missing features to properly report them */
804     bitmap_andnot(missing, model->features, max_model->features, S390_FEAT_MAX);
805     if (bitmap_empty(missing, S390_FEAT_MAX)) {
806         return;
807     }
808 
809     error_setg(errp, " ");
810     s390_feat_bitmap_to_ascii(missing, errp, error_prepend_missing_feat);
811     error_prepend(errp, "Some features requested in the CPU model are not "
812                   "available in the configuration: ");
813 }
814 
815 /**
816  * The base TCG CPU model "qemu" is based on the z900. However, we already
817  * can also emulate some additional features of later CPU generations, so
818  * we add these additional feature bits here.
819  */
820 static void add_qemu_cpu_model_features(S390FeatBitmap fbm)
821 {
822     static const int feats[] = {
823         S390_FEAT_DAT_ENH,
824         S390_FEAT_IDTE_SEGMENT,
825         S390_FEAT_STFLE,
826         S390_FEAT_EXTENDED_IMMEDIATE,
827         S390_FEAT_EXTENDED_TRANSLATION_2,
828         S390_FEAT_EXTENDED_TRANSLATION_3,
829         S390_FEAT_LONG_DISPLACEMENT,
830         S390_FEAT_LONG_DISPLACEMENT_FAST,
831         S390_FEAT_ETF2_ENH,
832         S390_FEAT_STORE_CLOCK_FAST,
833         S390_FEAT_MOVE_WITH_OPTIONAL_SPEC,
834         S390_FEAT_ETF3_ENH,
835         S390_FEAT_COMPARE_AND_SWAP_AND_STORE,
836         S390_FEAT_COMPARE_AND_SWAP_AND_STORE_2,
837         S390_FEAT_GENERAL_INSTRUCTIONS_EXT,
838         S390_FEAT_EXECUTE_EXT,
839         S390_FEAT_FLOATING_POINT_SUPPPORT_ENH,
840         S390_FEAT_STFLE_45,
841         S390_FEAT_STFLE_49,
842         S390_FEAT_LOCAL_TLB_CLEARING,
843         S390_FEAT_STFLE_53,
844     };
845     int i;
846 
847     for (i = 0; i < ARRAY_SIZE(feats); i++) {
848         set_bit(feats[i], fbm);
849     }
850 }
851 
852 static S390CPUModel *get_max_cpu_model(Error **errp)
853 {
854     static S390CPUModel max_model;
855     static bool cached;
856 
857     if (cached) {
858         return &max_model;
859     }
860 
861     if (kvm_enabled()) {
862         kvm_s390_get_host_cpu_model(&max_model, errp);
863     } else {
864         /* TCG emulates a z900 (with some optional additional features) */
865         max_model.def = &s390_cpu_defs[0];
866         bitmap_copy(max_model.features, max_model.def->default_feat,
867                     S390_FEAT_MAX);
868         add_qemu_cpu_model_features(max_model.features);
869     }
870     if (!*errp) {
871         cached = true;
872         return &max_model;
873     }
874     return NULL;
875 }
876 
877 static inline void apply_cpu_model(const S390CPUModel *model, Error **errp)
878 {
879 #ifndef CONFIG_USER_ONLY
880     static S390CPUModel applied_model;
881     static bool applied;
882 
883     /*
884      * We have the same model for all VCPUs. KVM can only be configured before
885      * any VCPUs are defined in KVM.
886      */
887     if (applied) {
888         if (model && memcmp(&applied_model, model, sizeof(S390CPUModel))) {
889             error_setg(errp, "Mixed CPU models are not supported on s390x.");
890         }
891         return;
892     }
893 
894     if (kvm_enabled()) {
895         kvm_s390_apply_cpu_model(model, errp);
896     }
897 
898     if (!*errp) {
899         applied = true;
900         if (model) {
901             applied_model = *model;
902         }
903     }
904 #endif
905 }
906 
907 void s390_realize_cpu_model(CPUState *cs, Error **errp)
908 {
909     S390CPUClass *xcc = S390_CPU_GET_CLASS(cs);
910     S390CPU *cpu = S390_CPU(cs);
911     const S390CPUModel *max_model;
912 
913     if (xcc->kvm_required && !kvm_enabled()) {
914         error_setg(errp, "CPU definition requires KVM");
915         return;
916     }
917 
918     if (!cpu->model) {
919         /* no host model support -> perform compatibility stuff */
920         apply_cpu_model(NULL, errp);
921         return;
922     }
923 
924     max_model = get_max_cpu_model(errp);
925     if (*errp) {
926         error_prepend(errp, "CPU models are not available: ");
927         return;
928     }
929 
930     /* copy over properties that can vary */
931     cpu->model->lowest_ibc = max_model->lowest_ibc;
932     cpu->model->cpu_id = max_model->cpu_id;
933     cpu->model->cpu_id_format = max_model->cpu_id_format;
934     cpu->model->cpu_ver = max_model->cpu_ver;
935 
936     check_consistency(cpu->model);
937     check_compatibility(max_model, cpu->model, errp);
938     if (*errp) {
939         return;
940     }
941 
942     apply_cpu_model(cpu->model, errp);
943 
944     cpu->env.cpuid = s390_cpuid_from_cpu_model(cpu->model);
945     if (tcg_enabled()) {
946         /* basic mode, write the cpu address into the first 4 bit of the ID */
947         cpu->env.cpuid = deposit64(cpu->env.cpuid, 54, 4, cpu->env.core_id);
948     }
949 }
950 
951 static void get_feature(Object *obj, Visitor *v, const char *name,
952                         void *opaque, Error **errp)
953 {
954     S390Feat feat = (S390Feat) opaque;
955     S390CPU *cpu = S390_CPU(obj);
956     bool value;
957 
958     if (!cpu->model) {
959         error_setg(errp, "Details about the host CPU model are not available, "
960                          "features cannot be queried.");
961         return;
962     }
963 
964     value = test_bit(feat, cpu->model->features);
965     visit_type_bool(v, name, &value, errp);
966 }
967 
968 static void set_feature(Object *obj, Visitor *v, const char *name,
969                         void *opaque, Error **errp)
970 {
971     S390Feat feat = (S390Feat) opaque;
972     DeviceState *dev = DEVICE(obj);
973     S390CPU *cpu = S390_CPU(obj);
974     bool value;
975 
976     if (dev->realized) {
977         error_setg(errp, "Attempt to set property '%s' on '%s' after "
978                    "it was realized", name, object_get_typename(obj));
979         return;
980     } else if (!cpu->model) {
981         error_setg(errp, "Details about the host CPU model are not available, "
982                          "features cannot be changed.");
983         return;
984     }
985 
986     visit_type_bool(v, name, &value, errp);
987     if (*errp) {
988         return;
989     }
990     if (value) {
991         if (!test_bit(feat, cpu->model->def->full_feat)) {
992             error_setg(errp, "Feature '%s' is not available for CPU model '%s',"
993                        " it was introduced with later models.",
994                        name, cpu->model->def->name);
995             return;
996         }
997         set_bit(feat, cpu->model->features);
998     } else {
999         clear_bit(feat, cpu->model->features);
1000     }
1001 }
1002 
1003 static void get_feature_group(Object *obj, Visitor *v, const char *name,
1004                               void *opaque, Error **errp)
1005 {
1006     S390FeatGroup group = (S390FeatGroup) opaque;
1007     const S390FeatGroupDef *def = s390_feat_group_def(group);
1008     S390CPU *cpu = S390_CPU(obj);
1009     S390FeatBitmap tmp;
1010     bool value;
1011 
1012     if (!cpu->model) {
1013         error_setg(errp, "Details about the host CPU model are not available, "
1014                          "features cannot be queried.");
1015         return;
1016     }
1017 
1018     /* a group is enabled if all features are enabled */
1019     bitmap_and(tmp, cpu->model->features, def->feat, S390_FEAT_MAX);
1020     value = bitmap_equal(tmp, def->feat, S390_FEAT_MAX);
1021     visit_type_bool(v, name, &value, errp);
1022 }
1023 
1024 static void set_feature_group(Object *obj, Visitor *v, const char *name,
1025                               void *opaque, Error **errp)
1026 {
1027     S390FeatGroup group = (S390FeatGroup) opaque;
1028     const S390FeatGroupDef *def = s390_feat_group_def(group);
1029     DeviceState *dev = DEVICE(obj);
1030     S390CPU *cpu = S390_CPU(obj);
1031     bool value;
1032 
1033     if (dev->realized) {
1034         error_setg(errp, "Attempt to set property '%s' on '%s' after "
1035                    "it was realized", name, object_get_typename(obj));
1036         return;
1037     } else if (!cpu->model) {
1038         error_setg(errp, "Details about the host CPU model are not available, "
1039                          "features cannot be changed.");
1040         return;
1041     }
1042 
1043     visit_type_bool(v, name, &value, errp);
1044     if (*errp) {
1045         return;
1046     }
1047     if (value) {
1048         /* groups are added in one shot, so an intersect is sufficient */
1049         if (!bitmap_intersects(def->feat, cpu->model->def->full_feat,
1050                                S390_FEAT_MAX)) {
1051             error_setg(errp, "Group '%s' is not available for CPU model '%s',"
1052                        " it was introduced with later models.",
1053                        name, cpu->model->def->name);
1054             return;
1055         }
1056         bitmap_or(cpu->model->features, cpu->model->features, def->feat,
1057                   S390_FEAT_MAX);
1058     } else {
1059         bitmap_andnot(cpu->model->features, cpu->model->features, def->feat,
1060                       S390_FEAT_MAX);
1061     }
1062 }
1063 
1064 void s390_cpu_model_register_props(Object *obj)
1065 {
1066     S390FeatGroup group;
1067     S390Feat feat;
1068 
1069     for (feat = 0; feat < S390_FEAT_MAX; feat++) {
1070         const S390FeatDef *def = s390_feat_def(feat);
1071         object_property_add(obj, def->name, "bool", get_feature,
1072                             set_feature, NULL, (void *) feat, NULL);
1073         object_property_set_description(obj, def->name, def->desc , NULL);
1074     }
1075     for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
1076         const S390FeatGroupDef *def = s390_feat_group_def(group);
1077         object_property_add(obj, def->name, "bool", get_feature_group,
1078                             set_feature_group, NULL, (void *) group, NULL);
1079         object_property_set_description(obj, def->name, def->desc , NULL);
1080     }
1081 }
1082 
1083 static void s390_cpu_model_initfn(Object *obj)
1084 {
1085     S390CPU *cpu = S390_CPU(obj);
1086     S390CPUClass *xcc = S390_CPU_GET_CLASS(cpu);
1087 
1088     cpu->model = g_malloc0(sizeof(*cpu->model));
1089     /* copy the model, so we can modify it */
1090     cpu->model->def = xcc->cpu_def;
1091     if (xcc->is_static) {
1092         /* base model - features will never change */
1093         bitmap_copy(cpu->model->features, cpu->model->def->base_feat,
1094                     S390_FEAT_MAX);
1095     } else {
1096         /* latest model - features can change */
1097         bitmap_copy(cpu->model->features,
1098                     cpu->model->def->default_feat, S390_FEAT_MAX);
1099     }
1100 }
1101 
1102 #ifdef CONFIG_KVM
1103 static void s390_host_cpu_model_initfn(Object *obj)
1104 {
1105     S390CPU *cpu = S390_CPU(obj);
1106     Error *err = NULL;
1107 
1108     if (!kvm_enabled() || !kvm_s390_cpu_models_supported()) {
1109         return;
1110     }
1111 
1112     cpu->model = g_malloc0(sizeof(*cpu->model));
1113     kvm_s390_get_host_cpu_model(cpu->model, &err);
1114     if (err) {
1115         error_report_err(err);
1116         g_free(cpu->model);
1117         /* fallback to unsupported cpu models */
1118         cpu->model = NULL;
1119     }
1120 }
1121 #endif
1122 
1123 static void s390_qemu_cpu_model_initfn(Object *obj)
1124 {
1125     static S390CPUDef s390_qemu_cpu_defs;
1126     S390CPU *cpu = S390_CPU(obj);
1127 
1128     cpu->model = g_malloc0(sizeof(*cpu->model));
1129     /* TCG emulates a z900 (with some optional additional features) */
1130     memcpy(&s390_qemu_cpu_defs, &s390_cpu_defs[0], sizeof(s390_qemu_cpu_defs));
1131     add_qemu_cpu_model_features(s390_qemu_cpu_defs.full_feat);
1132     cpu->model->def = &s390_qemu_cpu_defs;
1133     bitmap_copy(cpu->model->features, cpu->model->def->default_feat,
1134                 S390_FEAT_MAX);
1135 }
1136 
1137 static void s390_cpu_model_finalize(Object *obj)
1138 {
1139     S390CPU *cpu = S390_CPU(obj);
1140 
1141     g_free(cpu->model);
1142     cpu->model = NULL;
1143 }
1144 
1145 static bool get_is_migration_safe(Object *obj, Error **errp)
1146 {
1147     return S390_CPU_GET_CLASS(obj)->is_migration_safe;
1148 }
1149 
1150 static bool get_is_static(Object *obj, Error **errp)
1151 {
1152     return S390_CPU_GET_CLASS(obj)->is_static;
1153 }
1154 
1155 static char *get_description(Object *obj, Error **errp)
1156 {
1157     return g_strdup(S390_CPU_GET_CLASS(obj)->desc);
1158 }
1159 
1160 void s390_cpu_model_class_register_props(ObjectClass *oc)
1161 {
1162     object_class_property_add_bool(oc, "migration-safe", get_is_migration_safe,
1163                                    NULL, NULL);
1164     object_class_property_add_bool(oc, "static", get_is_static,
1165                                    NULL, NULL);
1166     object_class_property_add_str(oc, "description", get_description, NULL,
1167                                   NULL);
1168 }
1169 
1170 #ifdef CONFIG_KVM
1171 static void s390_host_cpu_model_class_init(ObjectClass *oc, void *data)
1172 {
1173     S390CPUClass *xcc = S390_CPU_CLASS(oc);
1174 
1175     xcc->kvm_required = true;
1176     xcc->desc = "KVM only: All recognized features";
1177 }
1178 #endif
1179 
1180 static void s390_base_cpu_model_class_init(ObjectClass *oc, void *data)
1181 {
1182     S390CPUClass *xcc = S390_CPU_CLASS(oc);
1183 
1184     /* all base models are migration safe */
1185     xcc->cpu_def = (const S390CPUDef *) data;
1186     xcc->is_migration_safe = true;
1187     xcc->is_static = true;
1188     xcc->desc = xcc->cpu_def->desc;
1189 }
1190 
1191 static void s390_cpu_model_class_init(ObjectClass *oc, void *data)
1192 {
1193     S390CPUClass *xcc = S390_CPU_CLASS(oc);
1194 
1195     /* model that can change between QEMU versions */
1196     xcc->cpu_def = (const S390CPUDef *) data;
1197     xcc->is_migration_safe = true;
1198     xcc->desc = xcc->cpu_def->desc;
1199 }
1200 
1201 static void s390_qemu_cpu_model_class_init(ObjectClass *oc, void *data)
1202 {
1203     S390CPUClass *xcc = S390_CPU_CLASS(oc);
1204 
1205     xcc->is_migration_safe = true;
1206     xcc->desc = g_strdup_printf("QEMU Virtual CPU version %s",
1207                                 qemu_hw_version());
1208 }
1209 
1210 #define S390_CPU_TYPE_SUFFIX "-" TYPE_S390_CPU
1211 #define S390_CPU_TYPE_NAME(name) (name S390_CPU_TYPE_SUFFIX)
1212 
1213 /* Generate type name for a cpu model. Caller has to free the string. */
1214 static char *s390_cpu_type_name(const char *model_name)
1215 {
1216     return g_strdup_printf(S390_CPU_TYPE_NAME("%s"), model_name);
1217 }
1218 
1219 /* Generate type name for a base cpu model. Caller has to free the string. */
1220 static char *s390_base_cpu_type_name(const char *model_name)
1221 {
1222     return g_strdup_printf(S390_CPU_TYPE_NAME("%s-base"), model_name);
1223 }
1224 
1225 ObjectClass *s390_cpu_class_by_name(const char *name)
1226 {
1227     char *typename = s390_cpu_type_name(name);
1228     ObjectClass *oc;
1229 
1230     oc = object_class_by_name(typename);
1231     g_free(typename);
1232     return oc;
1233 }
1234 
1235 const char *s390_default_cpu_model_name(void)
1236 {
1237      if (kvm_enabled()) {
1238         return "host";
1239      }
1240      return "qemu";
1241 }
1242 
1243 static const TypeInfo qemu_s390_cpu_type_info = {
1244     .name = S390_CPU_TYPE_NAME("qemu"),
1245     .parent = TYPE_S390_CPU,
1246     .instance_init = s390_qemu_cpu_model_initfn,
1247     .instance_finalize = s390_cpu_model_finalize,
1248     .class_init = s390_qemu_cpu_model_class_init,
1249 };
1250 
1251 #ifdef CONFIG_KVM
1252 static const TypeInfo host_s390_cpu_type_info = {
1253     .name = S390_CPU_TYPE_NAME("host"),
1254     .parent = TYPE_S390_CPU,
1255     .instance_init = s390_host_cpu_model_initfn,
1256     .instance_finalize = s390_cpu_model_finalize,
1257     .class_init = s390_host_cpu_model_class_init,
1258 };
1259 #endif
1260 
1261 static void init_ignored_base_feat(void)
1262 {
1263     static const int feats[] = {
1264          /* MSA subfunctions that could not be available on certain machines */
1265          S390_FEAT_KMAC_DEA,
1266          S390_FEAT_KMAC_TDEA_128,
1267          S390_FEAT_KMAC_TDEA_192,
1268          S390_FEAT_KMC_DEA,
1269          S390_FEAT_KMC_TDEA_128,
1270          S390_FEAT_KMC_TDEA_192,
1271          S390_FEAT_KM_DEA,
1272          S390_FEAT_KM_TDEA_128,
1273          S390_FEAT_KM_TDEA_192,
1274          S390_FEAT_KIMD_SHA_1,
1275          S390_FEAT_KLMD_SHA_1,
1276     };
1277     int i;
1278 
1279     for (i = 0; i < ARRAY_SIZE(feats); i++) {
1280         set_bit(feats[i], ignored_base_feat);
1281     }
1282 }
1283 
1284 static void register_types(void)
1285 {
1286     int i;
1287 
1288     init_ignored_base_feat();
1289 
1290     /* init all bitmaps from gnerated data initially */
1291     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
1292         s390_init_feat_bitmap(s390_cpu_defs[i].base_init,
1293                               s390_cpu_defs[i].base_feat);
1294         s390_init_feat_bitmap(s390_cpu_defs[i].default_init,
1295                               s390_cpu_defs[i].default_feat);
1296         s390_init_feat_bitmap(s390_cpu_defs[i].full_init,
1297                               s390_cpu_defs[i].full_feat);
1298     }
1299 
1300     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
1301         char *base_name = s390_base_cpu_type_name(s390_cpu_defs[i].name);
1302         TypeInfo ti_base = {
1303             .name = base_name,
1304             .parent = TYPE_S390_CPU,
1305             .instance_init = s390_cpu_model_initfn,
1306             .instance_finalize = s390_cpu_model_finalize,
1307             .class_init = s390_base_cpu_model_class_init,
1308             .class_data = (void *) &s390_cpu_defs[i],
1309         };
1310         char *name = s390_cpu_type_name(s390_cpu_defs[i].name);
1311         TypeInfo ti = {
1312             .name = name,
1313             .parent = TYPE_S390_CPU,
1314             .instance_init = s390_cpu_model_initfn,
1315             .instance_finalize = s390_cpu_model_finalize,
1316             .class_init = s390_cpu_model_class_init,
1317             .class_data = (void *) &s390_cpu_defs[i],
1318         };
1319 
1320         type_register_static(&ti_base);
1321         type_register_static(&ti);
1322         g_free(base_name);
1323         g_free(name);
1324     }
1325 
1326     type_register_static(&qemu_s390_cpu_type_info);
1327 #ifdef CONFIG_KVM
1328     type_register_static(&host_s390_cpu_type_info);
1329 #endif
1330 }
1331 
1332 type_init(register_types)
1333