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