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