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