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