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