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