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