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