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