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