xref: /openbmc/qemu/target/s390x/cpu_models.c (revision 8779fccb)
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 };
79 
80 uint32_t s390_get_hmfai(void)
81 {
82     static S390CPU *cpu;
83 
84     if (!cpu) {
85         cpu = S390_CPU(qemu_get_cpu(0));
86     }
87 
88     if (!cpu || !cpu->model) {
89         return 0;
90     }
91     return cpu->model->def->hmfai;
92 }
93 
94 uint8_t s390_get_mha_pow(void)
95 {
96     static S390CPU *cpu;
97 
98     if (!cpu) {
99         cpu = S390_CPU(qemu_get_cpu(0));
100     }
101 
102     if (!cpu || !cpu->model) {
103         return 0;
104     }
105     return cpu->model->def->mha_pow;
106 }
107 
108 uint32_t s390_get_ibc_val(void)
109 {
110     uint16_t unblocked_ibc, lowest_ibc;
111     static S390CPU *cpu;
112 
113     if (!cpu) {
114         cpu = S390_CPU(qemu_get_cpu(0));
115     }
116 
117     if (!cpu || !cpu->model) {
118         return 0;
119     }
120     unblocked_ibc = s390_ibc_from_cpu_model(cpu->model);
121     lowest_ibc = cpu->model->lowest_ibc;
122     /* the lowest_ibc always has to be <= unblocked_ibc */
123     if (!lowest_ibc || lowest_ibc > unblocked_ibc) {
124         return 0;
125     }
126     return ((uint32_t) lowest_ibc << 16) | unblocked_ibc;
127 }
128 
129 void s390_get_feat_block(S390FeatType type, uint8_t *data)
130 {
131     static S390CPU *cpu;
132 
133     if (!cpu) {
134         cpu = S390_CPU(qemu_get_cpu(0));
135     }
136 
137     if (!cpu || !cpu->model) {
138         return;
139     }
140     s390_fill_feat_block(cpu->model->features, type, data);
141 }
142 
143 bool s390_has_feat(S390Feat feat)
144 {
145     static S390CPU *cpu;
146 
147     if (!cpu) {
148         cpu = S390_CPU(qemu_get_cpu(0));
149     }
150 
151     if (!cpu || !cpu->model) {
152 #ifdef CONFIG_KVM
153         if (kvm_enabled()) {
154             if (feat == S390_FEAT_VECTOR) {
155                 return kvm_check_extension(kvm_state,
156                                            KVM_CAP_S390_VECTOR_REGISTERS);
157             }
158             if (feat == S390_FEAT_RUNTIME_INSTRUMENTATION) {
159                 return kvm_s390_get_ri();
160             }
161             if (feat == S390_FEAT_MSA_EXT_3) {
162                 return true;
163             }
164         }
165 #endif
166         return 0;
167     }
168     return test_bit(feat, cpu->model->features);
169 }
170 
171 uint8_t s390_get_gen_for_cpu_type(uint16_t type)
172 {
173     int i;
174 
175     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
176         if (s390_cpu_defs[i].type == type) {
177             return s390_cpu_defs[i].gen;
178         }
179     }
180     return 0;
181 }
182 
183 const S390CPUDef *s390_find_cpu_def(uint16_t type, uint8_t gen, uint8_t ec_ga,
184                                     S390FeatBitmap features)
185 {
186     const S390CPUDef *last_compatible = NULL;
187     int i;
188 
189     if (!gen) {
190         ec_ga = 0;
191     }
192     if (!gen && type) {
193         gen = s390_get_gen_for_cpu_type(type);
194     }
195 
196     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
197         const S390CPUDef *def = &s390_cpu_defs[i];
198         S390FeatBitmap missing;
199 
200         /* don't even try newer generations if we know the generation */
201         if (gen) {
202             if (def->gen > gen) {
203                 break;
204             } else if (def->gen == gen && ec_ga && def->ec_ga > ec_ga) {
205                 break;
206             }
207         }
208 
209         if (features) {
210             /* see if the model satisfies the minimum features */
211             bitmap_andnot(missing, def->base_feat, features, S390_FEAT_MAX);
212             if (!bitmap_empty(missing, S390_FEAT_MAX)) {
213                 break;
214             }
215         }
216 
217         /* stop the search if we found the exact model */
218         if (def->type == type && def->ec_ga == ec_ga) {
219             return def;
220         }
221         last_compatible = def;
222     }
223     return last_compatible;
224 }
225 
226 struct S390PrintCpuListInfo {
227     FILE *f;
228     fprintf_function print;
229 };
230 
231 static void print_cpu_model_list(ObjectClass *klass, void *opaque)
232 {
233     struct S390PrintCpuListInfo *info = opaque;
234     S390CPUClass *scc = S390_CPU_CLASS(klass);
235     char *name = g_strdup(object_class_get_name(klass));
236     const char *details = "";
237 
238     if (scc->is_static) {
239         details = "(static, migration-safe)";
240     } else if (scc->is_migration_safe) {
241         details = "(migration-safe)";
242     }
243 
244     /* strip off the -s390-cpu */
245     g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
246     (*info->print)(info->f, "s390 %-15s %-35s %s\n", name, scc->desc,
247                    details);
248     g_free(name);
249 }
250 
251 void s390_cpu_list(FILE *f, fprintf_function print)
252 {
253     struct S390PrintCpuListInfo info = {
254         .f = f,
255         .print = print,
256     };
257     S390FeatGroup group;
258     S390Feat feat;
259 
260     object_class_foreach(print_cpu_model_list, TYPE_S390_CPU, false, &info);
261 
262     (*print)(f, "\nRecognized feature flags:\n");
263     for (feat = 0; feat < S390_FEAT_MAX; feat++) {
264         const S390FeatDef *def = s390_feat_def(feat);
265 
266         (*print)(f, "%-20s %-50s\n", def->name, def->desc);
267     }
268 
269     (*print)(f, "\nRecognized feature groups:\n");
270     for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
271         const S390FeatGroupDef *def = s390_feat_group_def(group);
272 
273         (*print)(f, "%-20s %-50s\n", def->name, def->desc);
274     }
275 }
276 
277 #ifndef CONFIG_USER_ONLY
278 static void create_cpu_model_list(ObjectClass *klass, void *opaque)
279 {
280     CpuDefinitionInfoList **cpu_list = opaque;
281     CpuDefinitionInfoList *entry;
282     CpuDefinitionInfo *info;
283     char *name = g_strdup(object_class_get_name(klass));
284     S390CPUClass *scc = S390_CPU_CLASS(klass);
285 
286     /* strip off the -s390-cpu */
287     g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
288     info = g_malloc0(sizeof(*info));
289     info->name = name;
290     info->has_migration_safe = true;
291     info->migration_safe = scc->is_migration_safe;
292     info->q_static = scc->is_static;
293     info->q_typename = g_strdup(object_class_get_name(klass));
294 
295 
296     entry = g_malloc0(sizeof(*entry));
297     entry->value = info;
298     entry->next = *cpu_list;
299     *cpu_list = entry;
300 }
301 
302 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
303 {
304     CpuDefinitionInfoList *list = NULL;
305 
306     object_class_foreach(create_cpu_model_list, TYPE_S390_CPU, false, &list);
307 
308     return list;
309 }
310 
311 static void cpu_model_from_info(S390CPUModel *model, const CpuModelInfo *info,
312                                 Error **errp)
313 {
314     const QDict *qdict = NULL;
315     const QDictEntry *e;
316     Visitor *visitor;
317     ObjectClass *oc;
318     S390CPU *cpu;
319     Object *obj;
320 
321     if (info->props) {
322         qdict = qobject_to_qdict(info->props);
323         if (!qdict) {
324             error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
325             return;
326         }
327     }
328 
329     oc = cpu_class_by_name(TYPE_S390_CPU, info->name);
330     if (!oc) {
331         error_setg(errp, "The CPU definition \'%s\' is unknown.", info->name);
332         return;
333     }
334     if (S390_CPU_CLASS(oc)->kvm_required && !kvm_enabled()) {
335         error_setg(errp, "The CPU definition '%s' requires KVM", info->name);
336         return;
337     }
338     obj = object_new(object_class_get_name(oc));
339     cpu = S390_CPU(obj);
340 
341     if (!cpu->model) {
342         error_setg(errp, "Details about the host CPU model are not available, "
343                          "it cannot be used.");
344         object_unref(obj);
345         return;
346     }
347 
348     if (qdict) {
349         visitor = qobject_input_visitor_new(info->props, true);
350         visit_start_struct(visitor, NULL, NULL, 0, errp);
351         if (*errp) {
352             object_unref(obj);
353             return;
354         }
355         for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
356             object_property_set(obj, visitor, e->key, errp);
357             if (*errp) {
358                 break;
359             }
360         }
361         if (!*errp) {
362             visit_check_struct(visitor, errp);
363         }
364         visit_end_struct(visitor, NULL);
365         visit_free(visitor);
366         if (*errp) {
367             object_unref(obj);
368             return;
369         }
370     }
371 
372     /* copy the model and throw the cpu away */
373     memcpy(model, cpu->model, sizeof(*model));
374     object_unref(obj);
375 }
376 
377 static void qdict_add_disabled_feat(const char *name, void *opaque)
378 {
379     qdict_put((QDict *) opaque, name, qbool_from_bool(false));
380 }
381 
382 static void qdict_add_enabled_feat(const char *name, void *opaque)
383 {
384     qdict_put((QDict *) opaque, name, qbool_from_bool(true));
385 }
386 
387 /* convert S390CPUDef into a static CpuModelInfo */
388 static void cpu_info_from_model(CpuModelInfo *info, const S390CPUModel *model,
389                                 bool delta_changes)
390 {
391     QDict *qdict = qdict_new();
392     S390FeatBitmap bitmap;
393 
394     /* always fallback to the static base model */
395     info->name = g_strdup_printf("%s-base", model->def->name);
396 
397     if (delta_changes) {
398         /* features deleted from the base feature set */
399         bitmap_andnot(bitmap, model->def->base_feat, model->features,
400                       S390_FEAT_MAX);
401         if (!bitmap_empty(bitmap, S390_FEAT_MAX)) {
402             s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_disabled_feat);
403         }
404 
405         /* features added to the base feature set */
406         bitmap_andnot(bitmap, model->features, model->def->base_feat,
407                       S390_FEAT_MAX);
408         if (!bitmap_empty(bitmap, S390_FEAT_MAX)) {
409             s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_enabled_feat);
410         }
411     } else {
412         /* expand all features */
413         s390_feat_bitmap_to_ascii(model->features, qdict,
414                                   qdict_add_enabled_feat);
415         bitmap_complement(bitmap, model->features, S390_FEAT_MAX);
416         s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_disabled_feat);
417     }
418 
419     if (!qdict_size(qdict)) {
420         QDECREF(qdict);
421     } else {
422         info->props = QOBJECT(qdict);
423         info->has_props = true;
424     }
425 }
426 
427 CpuModelExpansionInfo *arch_query_cpu_model_expansion(CpuModelExpansionType type,
428                                                       CpuModelInfo *model,
429                                                       Error **errp)
430 {
431     CpuModelExpansionInfo *expansion_info = NULL;
432     S390CPUModel s390_model;
433     bool delta_changes = false;
434 
435     /* convert it to our internal representation */
436     cpu_model_from_info(&s390_model, model, errp);
437     if (*errp) {
438         return NULL;
439     }
440 
441     if (type == CPU_MODEL_EXPANSION_TYPE_STATIC) {
442         delta_changes = true;
443     } else if (type != CPU_MODEL_EXPANSION_TYPE_FULL) {
444         error_setg(errp, "The requested expansion type is not supported.");
445         return NULL;
446     }
447 
448     /* convert it back to a static representation */
449     expansion_info = g_malloc0(sizeof(*expansion_info));
450     expansion_info->model = g_malloc0(sizeof(*expansion_info->model));
451     cpu_info_from_model(expansion_info->model, &s390_model, delta_changes);
452     return expansion_info;
453 }
454 
455 static void list_add_feat(const char *name, void *opaque)
456 {
457     strList **last = (strList **) opaque;
458     strList *entry;
459 
460     entry = g_malloc0(sizeof(*entry));
461     entry->value = g_strdup(name);
462     entry->next = *last;
463     *last = entry;
464 }
465 
466 CpuModelCompareInfo *arch_query_cpu_model_comparison(CpuModelInfo *infoa,
467                                                      CpuModelInfo *infob,
468                                                      Error **errp)
469 {
470     CpuModelCompareResult feat_result, gen_result;
471     CpuModelCompareInfo *compare_info;
472     S390FeatBitmap missing, added;
473     S390CPUModel modela, modelb;
474 
475     /* convert both models to our internal representation */
476     cpu_model_from_info(&modela, infoa, errp);
477     if (*errp) {
478         return NULL;
479     }
480     cpu_model_from_info(&modelb, infob, errp);
481     if (*errp) {
482         return NULL;
483     }
484     compare_info = g_malloc0(sizeof(*compare_info));
485 
486     /* check the cpu generation and ga level */
487     if (modela.def->gen == modelb.def->gen) {
488         if (modela.def->ec_ga == modelb.def->ec_ga) {
489             /* ec and corresponding bc are identical */
490             gen_result = CPU_MODEL_COMPARE_RESULT_IDENTICAL;
491         } else if (modela.def->ec_ga < modelb.def->ec_ga) {
492             gen_result = CPU_MODEL_COMPARE_RESULT_SUBSET;
493         } else {
494             gen_result = CPU_MODEL_COMPARE_RESULT_SUPERSET;
495         }
496     } else if (modela.def->gen < modelb.def->gen) {
497         gen_result = CPU_MODEL_COMPARE_RESULT_SUBSET;
498     } else {
499         gen_result = CPU_MODEL_COMPARE_RESULT_SUPERSET;
500     }
501     if (gen_result != CPU_MODEL_COMPARE_RESULT_IDENTICAL) {
502         /* both models cannot be made identical */
503         list_add_feat("type", &compare_info->responsible_properties);
504     }
505 
506     /* check the feature set */
507     if (bitmap_equal(modela.features, modelb.features, S390_FEAT_MAX)) {
508         feat_result = CPU_MODEL_COMPARE_RESULT_IDENTICAL;
509     } else {
510         bitmap_andnot(missing, modela.features, modelb.features, S390_FEAT_MAX);
511         s390_feat_bitmap_to_ascii(missing,
512                                   &compare_info->responsible_properties,
513                                   list_add_feat);
514         bitmap_andnot(added, modelb.features, modela.features, S390_FEAT_MAX);
515         s390_feat_bitmap_to_ascii(added, &compare_info->responsible_properties,
516                                   list_add_feat);
517         if (bitmap_empty(missing, S390_FEAT_MAX)) {
518             feat_result = CPU_MODEL_COMPARE_RESULT_SUBSET;
519         } else if (bitmap_empty(added, S390_FEAT_MAX)) {
520             feat_result = CPU_MODEL_COMPARE_RESULT_SUPERSET;
521         } else {
522             feat_result = CPU_MODEL_COMPARE_RESULT_INCOMPATIBLE;
523         }
524     }
525 
526     /* combine the results */
527     if (gen_result == feat_result) {
528         compare_info->result = gen_result;
529     } else if (feat_result == CPU_MODEL_COMPARE_RESULT_IDENTICAL) {
530         compare_info->result = gen_result;
531     } else if (gen_result == CPU_MODEL_COMPARE_RESULT_IDENTICAL) {
532         compare_info->result = feat_result;
533     } else {
534         compare_info->result = CPU_MODEL_COMPARE_RESULT_INCOMPATIBLE;
535     }
536     return compare_info;
537 }
538 
539 CpuModelBaselineInfo *arch_query_cpu_model_baseline(CpuModelInfo *infoa,
540                                                     CpuModelInfo *infob,
541                                                     Error **errp)
542 {
543     CpuModelBaselineInfo *baseline_info;
544     S390CPUModel modela, modelb, model;
545     uint16_t cpu_type;
546     uint8_t max_gen_ga;
547     uint8_t max_gen;
548 
549     /* convert both models to our internal representation */
550     cpu_model_from_info(&modela, infoa, errp);
551     if (*errp) {
552         return NULL;
553     }
554 
555     cpu_model_from_info(&modelb, infob, errp);
556     if (*errp) {
557         return NULL;
558     }
559 
560     /* features both models support */
561     bitmap_and(model.features, modela.features, modelb.features, S390_FEAT_MAX);
562 
563     /* detect the maximum model not regarding features */
564     if (modela.def->gen == modelb.def->gen) {
565         if (modela.def->type == modelb.def->type) {
566             cpu_type = modela.def->type;
567         } else {
568             cpu_type = 0;
569         }
570         max_gen = modela.def->gen;
571         max_gen_ga = MIN(modela.def->ec_ga, modelb.def->ec_ga);
572     } else if (modela.def->gen > modelb.def->gen) {
573         cpu_type = modelb.def->type;
574         max_gen = modelb.def->gen;
575         max_gen_ga = modelb.def->ec_ga;
576     } else {
577         cpu_type = modela.def->type;
578         max_gen = modela.def->gen;
579         max_gen_ga = modela.def->ec_ga;
580     }
581 
582     model.def = s390_find_cpu_def(cpu_type, max_gen, max_gen_ga,
583                                   model.features);
584     /* strip off features not part of the max model */
585     bitmap_and(model.features, model.features, model.def->full_feat,
586                S390_FEAT_MAX);
587 
588     baseline_info = g_malloc0(sizeof(*baseline_info));
589     baseline_info->model = g_malloc0(sizeof(*baseline_info->model));
590     cpu_info_from_model(baseline_info->model, &model, true);
591     return baseline_info;
592 }
593 #endif
594 
595 static void check_consistency(const S390CPUModel *model)
596 {
597     static int dep[][2] = {
598         { S390_FEAT_IPTE_RANGE, S390_FEAT_DAT_ENH },
599         { S390_FEAT_IDTE_SEGMENT, S390_FEAT_DAT_ENH },
600         { S390_FEAT_IDTE_REGION, S390_FEAT_DAT_ENH },
601         { S390_FEAT_IDTE_REGION, S390_FEAT_IDTE_SEGMENT },
602         { S390_FEAT_LOCAL_TLB_CLEARING, S390_FEAT_DAT_ENH},
603         { S390_FEAT_LONG_DISPLACEMENT_FAST, S390_FEAT_LONG_DISPLACEMENT },
604         { S390_FEAT_DFP_FAST, S390_FEAT_DFP },
605         { S390_FEAT_TRANSACTIONAL_EXE, S390_FEAT_STFLE_49 },
606         { S390_FEAT_EDAT_2, S390_FEAT_EDAT},
607         { S390_FEAT_MSA_EXT_5, S390_FEAT_KIMD_SHA_512 },
608         { S390_FEAT_MSA_EXT_5, S390_FEAT_KLMD_SHA_512 },
609         { S390_FEAT_MSA_EXT_4, S390_FEAT_MSA_EXT_3 },
610         { S390_FEAT_SIE_CMMA, S390_FEAT_CMM },
611         { S390_FEAT_SIE_CMMA, S390_FEAT_SIE_GSLS },
612         { S390_FEAT_SIE_PFMFI, S390_FEAT_EDAT },
613     };
614     int i;
615 
616     for (i = 0; i < ARRAY_SIZE(dep); i++) {
617         if (test_bit(dep[i][0], model->features) &&
618             !test_bit(dep[i][1], model->features)) {
619             error_report("Warning: \'%s\' requires \'%s\'.",
620                          s390_feat_def(dep[i][0])->name,
621                          s390_feat_def(dep[i][1])->name);
622         }
623     }
624 }
625 
626 static void error_prepend_missing_feat(const char *name, void *opaque)
627 {
628     error_prepend((Error **) opaque, "%s ", name);
629 }
630 
631 static void check_compatibility(const S390CPUModel *max_model,
632                                 const S390CPUModel *model, Error **errp)
633 {
634     S390FeatBitmap missing;
635 
636     if (model->def->gen > max_model->def->gen) {
637         error_setg(errp, "Selected CPU generation is too new. Maximum "
638                    "supported model in the configuration: \'%s\'",
639                    max_model->def->name);
640         return;
641     } else if (model->def->gen == max_model->def->gen &&
642                model->def->ec_ga > max_model->def->ec_ga) {
643         error_setg(errp, "Selected CPU GA level is too new. Maximum "
644                    "supported model in the configuration: \'%s\'",
645                    max_model->def->name);
646         return;
647     }
648 
649     /* detect the missing features to properly report them */
650     bitmap_andnot(missing, model->features, max_model->features, S390_FEAT_MAX);
651     if (bitmap_empty(missing, S390_FEAT_MAX)) {
652         return;
653     }
654 
655     error_setg(errp, " ");
656     s390_feat_bitmap_to_ascii(missing, errp, error_prepend_missing_feat);
657     error_prepend(errp, "Some features requested in the CPU model are not "
658                   "available in the configuration: ");
659 }
660 
661 static S390CPUModel *get_max_cpu_model(Error **errp)
662 {
663 #ifndef CONFIG_USER_ONLY
664     static S390CPUModel max_model;
665     static bool cached;
666 
667     if (cached) {
668         return &max_model;
669     }
670 
671     if (kvm_enabled()) {
672         kvm_s390_get_host_cpu_model(&max_model, errp);
673     } else {
674         /* TCG enulates a z900 */
675         max_model.def = &s390_cpu_defs[0];
676         bitmap_copy(max_model.features, max_model.def->default_feat,
677                     S390_FEAT_MAX);
678     }
679     if (!*errp) {
680         cached = true;
681         return &max_model;
682     }
683 #endif
684     return NULL;
685 }
686 
687 static inline void apply_cpu_model(const S390CPUModel *model, Error **errp)
688 {
689 #ifndef CONFIG_USER_ONLY
690     static S390CPUModel applied_model;
691     static bool applied;
692 
693     /*
694      * We have the same model for all VCPUs. KVM can only be configured before
695      * any VCPUs are defined in KVM.
696      */
697     if (applied) {
698         if (model && memcmp(&applied_model, model, sizeof(S390CPUModel))) {
699             error_setg(errp, "Mixed CPU models are not supported on s390x.");
700         }
701         return;
702     }
703 
704     if (kvm_enabled()) {
705         kvm_s390_apply_cpu_model(model, errp);
706     } else if (model) {
707         /* FIXME TCG - use data for stdip/stfl */
708     }
709 
710     if (!*errp) {
711         applied = true;
712         if (model) {
713             applied_model = *model;
714         }
715     }
716 #endif
717 }
718 
719 void s390_realize_cpu_model(CPUState *cs, Error **errp)
720 {
721     S390CPUClass *xcc = S390_CPU_GET_CLASS(cs);
722     S390CPU *cpu = S390_CPU(cs);
723     const S390CPUModel *max_model;
724 
725     if (xcc->kvm_required && !kvm_enabled()) {
726         error_setg(errp, "CPU definition requires KVM");
727         return;
728     }
729 
730     if (!cpu->model) {
731         /* no host model support -> perform compatibility stuff */
732         apply_cpu_model(NULL, errp);
733         return;
734     }
735 
736     max_model = get_max_cpu_model(errp);
737     if (*errp) {
738         error_prepend(errp, "CPU models are not available: ");
739         return;
740     }
741 
742     /* copy over properties that can vary */
743     cpu->model->lowest_ibc = max_model->lowest_ibc;
744     cpu->model->cpu_id = max_model->cpu_id;
745     cpu->model->cpu_ver = max_model->cpu_ver;
746 
747     check_consistency(cpu->model);
748     check_compatibility(max_model, cpu->model, errp);
749     if (*errp) {
750         return;
751     }
752 
753     apply_cpu_model(cpu->model, errp);
754 }
755 
756 static void get_feature(Object *obj, Visitor *v, const char *name,
757                         void *opaque, Error **errp)
758 {
759     S390Feat feat = (S390Feat) opaque;
760     S390CPU *cpu = S390_CPU(obj);
761     bool value;
762 
763     if (!cpu->model) {
764         error_setg(errp, "Details about the host CPU model are not available, "
765                          "features cannot be queried.");
766         return;
767     }
768 
769     value = test_bit(feat, cpu->model->features);
770     visit_type_bool(v, name, &value, errp);
771 }
772 
773 static void set_feature(Object *obj, Visitor *v, const char *name,
774                         void *opaque, Error **errp)
775 {
776     S390Feat feat = (S390Feat) opaque;
777     DeviceState *dev = DEVICE(obj);
778     S390CPU *cpu = S390_CPU(obj);
779     bool value;
780 
781     if (dev->realized) {
782         error_setg(errp, "Attempt to set property '%s' on '%s' after "
783                    "it was realized", name, object_get_typename(obj));
784         return;
785     } else if (!cpu->model) {
786         error_setg(errp, "Details about the host CPU model are not available, "
787                          "features cannot be changed.");
788         return;
789     }
790 
791     visit_type_bool(v, name, &value, errp);
792     if (*errp) {
793         return;
794     }
795     if (value) {
796         if (!test_bit(feat, cpu->model->def->full_feat)) {
797             error_setg(errp, "Feature '%s' is not available for CPU model '%s',"
798                        " it was introduced with later models.",
799                        name, cpu->model->def->name);
800             return;
801         }
802         set_bit(feat, cpu->model->features);
803     } else {
804         clear_bit(feat, cpu->model->features);
805     }
806 }
807 
808 static void get_feature_group(Object *obj, Visitor *v, const char *name,
809                               void *opaque, Error **errp)
810 {
811     S390FeatGroup group = (S390FeatGroup) opaque;
812     const S390FeatGroupDef *def = s390_feat_group_def(group);
813     S390CPU *cpu = S390_CPU(obj);
814     S390FeatBitmap tmp;
815     bool value;
816 
817     if (!cpu->model) {
818         error_setg(errp, "Details about the host CPU model are not available, "
819                          "features cannot be queried.");
820         return;
821     }
822 
823     /* a group is enabled if all features are enabled */
824     bitmap_and(tmp, cpu->model->features, def->feat, S390_FEAT_MAX);
825     value = bitmap_equal(tmp, def->feat, S390_FEAT_MAX);
826     visit_type_bool(v, name, &value, errp);
827 }
828 
829 static void set_feature_group(Object *obj, Visitor *v, const char *name,
830                               void *opaque, Error **errp)
831 {
832     S390FeatGroup group = (S390FeatGroup) opaque;
833     const S390FeatGroupDef *def = s390_feat_group_def(group);
834     DeviceState *dev = DEVICE(obj);
835     S390CPU *cpu = S390_CPU(obj);
836     bool value;
837 
838     if (dev->realized) {
839         error_setg(errp, "Attempt to set property '%s' on '%s' after "
840                    "it was realized", name, object_get_typename(obj));
841         return;
842     } else if (!cpu->model) {
843         error_setg(errp, "Details about the host CPU model are not available, "
844                          "features cannot be changed.");
845         return;
846     }
847 
848     visit_type_bool(v, name, &value, errp);
849     if (*errp) {
850         return;
851     }
852     if (value) {
853         /* groups are added in one shot, so an intersect is sufficient */
854         if (!bitmap_intersects(def->feat, cpu->model->def->full_feat,
855                                S390_FEAT_MAX)) {
856             error_setg(errp, "Group '%s' is not available for CPU model '%s',"
857                        " it was introduced with later models.",
858                        name, cpu->model->def->name);
859             return;
860         }
861         bitmap_or(cpu->model->features, cpu->model->features, def->feat,
862                   S390_FEAT_MAX);
863     } else {
864         bitmap_andnot(cpu->model->features, cpu->model->features, def->feat,
865                       S390_FEAT_MAX);
866     }
867 }
868 
869 void s390_cpu_model_register_props(Object *obj)
870 {
871     S390FeatGroup group;
872     S390Feat feat;
873 
874     for (feat = 0; feat < S390_FEAT_MAX; feat++) {
875         const S390FeatDef *def = s390_feat_def(feat);
876         object_property_add(obj, def->name, "bool", get_feature,
877                             set_feature, NULL, (void *) feat, NULL);
878         object_property_set_description(obj, def->name, def->desc , NULL);
879     }
880     for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
881         const S390FeatGroupDef *def = s390_feat_group_def(group);
882         object_property_add(obj, def->name, "bool", get_feature_group,
883                             set_feature_group, NULL, (void *) group, NULL);
884         object_property_set_description(obj, def->name, def->desc , NULL);
885     }
886 }
887 
888 static void s390_cpu_model_initfn(Object *obj)
889 {
890     S390CPU *cpu = S390_CPU(obj);
891     S390CPUClass *xcc = S390_CPU_GET_CLASS(cpu);
892 
893     cpu->model = g_malloc0(sizeof(*cpu->model));
894     /* copy the model, so we can modify it */
895     cpu->model->def = xcc->cpu_def;
896     if (xcc->is_static) {
897         /* base model - features will never change */
898         bitmap_copy(cpu->model->features, cpu->model->def->base_feat,
899                     S390_FEAT_MAX);
900     } else {
901         /* latest model - features can change */
902         bitmap_copy(cpu->model->features,
903                     cpu->model->def->default_feat, S390_FEAT_MAX);
904     }
905 }
906 
907 #ifdef CONFIG_KVM
908 static void s390_host_cpu_model_initfn(Object *obj)
909 {
910     S390CPU *cpu = S390_CPU(obj);
911     Error *err = NULL;
912 
913     if (!kvm_enabled() || !kvm_s390_cpu_models_supported()) {
914         return;
915     }
916 
917     cpu->model = g_malloc0(sizeof(*cpu->model));
918     kvm_s390_get_host_cpu_model(cpu->model, &err);
919     if (err) {
920         error_report_err(err);
921         g_free(cpu->model);
922         /* fallback to unsupported cpu models */
923         cpu->model = NULL;
924     }
925 }
926 #endif
927 
928 static void s390_qemu_cpu_model_initfn(Object *obj)
929 {
930     S390CPU *cpu = S390_CPU(obj);
931 
932     cpu->model = g_malloc0(sizeof(*cpu->model));
933     /* TCG emulates a z900 */
934     cpu->model->def = &s390_cpu_defs[0];
935     bitmap_copy(cpu->model->features, cpu->model->def->default_feat,
936                 S390_FEAT_MAX);
937 }
938 
939 static void s390_cpu_model_finalize(Object *obj)
940 {
941     S390CPU *cpu = S390_CPU(obj);
942 
943     g_free(cpu->model);
944     cpu->model = NULL;
945 }
946 
947 static bool get_is_migration_safe(Object *obj, Error **errp)
948 {
949     return S390_CPU_GET_CLASS(obj)->is_migration_safe;
950 }
951 
952 static bool get_is_static(Object *obj, Error **errp)
953 {
954     return S390_CPU_GET_CLASS(obj)->is_static;
955 }
956 
957 static char *get_description(Object *obj, Error **errp)
958 {
959     return g_strdup(S390_CPU_GET_CLASS(obj)->desc);
960 }
961 
962 void s390_cpu_model_class_register_props(ObjectClass *oc)
963 {
964     object_class_property_add_bool(oc, "migration-safe", get_is_migration_safe,
965                                    NULL, NULL);
966     object_class_property_add_bool(oc, "static", get_is_static,
967                                    NULL, NULL);
968     object_class_property_add_str(oc, "description", get_description, NULL,
969                                   NULL);
970 }
971 
972 #ifdef CONFIG_KVM
973 static void s390_host_cpu_model_class_init(ObjectClass *oc, void *data)
974 {
975     S390CPUClass *xcc = S390_CPU_CLASS(oc);
976 
977     xcc->kvm_required = true;
978     xcc->desc = "KVM only: All recognized features";
979 }
980 #endif
981 
982 static void s390_base_cpu_model_class_init(ObjectClass *oc, void *data)
983 {
984     S390CPUClass *xcc = S390_CPU_CLASS(oc);
985 
986     /* all base models are migration safe */
987     xcc->cpu_def = (const S390CPUDef *) data;
988     xcc->is_migration_safe = true;
989     xcc->is_static = true;
990     xcc->desc = xcc->cpu_def->desc;
991 }
992 
993 static void s390_cpu_model_class_init(ObjectClass *oc, void *data)
994 {
995     S390CPUClass *xcc = S390_CPU_CLASS(oc);
996 
997     /* model that can change between QEMU versions */
998     xcc->cpu_def = (const S390CPUDef *) data;
999     xcc->is_migration_safe = true;
1000     xcc->desc = xcc->cpu_def->desc;
1001 }
1002 
1003 static void s390_qemu_cpu_model_class_init(ObjectClass *oc, void *data)
1004 {
1005     S390CPUClass *xcc = S390_CPU_CLASS(oc);
1006 
1007     xcc->is_migration_safe = true;
1008     xcc->desc = g_strdup_printf("QEMU Virtual CPU version %s",
1009                                 qemu_hw_version());
1010 }
1011 
1012 #define S390_CPU_TYPE_SUFFIX "-" TYPE_S390_CPU
1013 #define S390_CPU_TYPE_NAME(name) (name S390_CPU_TYPE_SUFFIX)
1014 
1015 /* Generate type name for a cpu model. Caller has to free the string. */
1016 static char *s390_cpu_type_name(const char *model_name)
1017 {
1018     return g_strdup_printf(S390_CPU_TYPE_NAME("%s"), model_name);
1019 }
1020 
1021 /* Generate type name for a base cpu model. Caller has to free the string. */
1022 static char *s390_base_cpu_type_name(const char *model_name)
1023 {
1024     return g_strdup_printf(S390_CPU_TYPE_NAME("%s-base"), model_name);
1025 }
1026 
1027 ObjectClass *s390_cpu_class_by_name(const char *name)
1028 {
1029     char *typename = s390_cpu_type_name(name);
1030     ObjectClass *oc;
1031 
1032     oc = object_class_by_name(typename);
1033     g_free(typename);
1034     return oc;
1035 }
1036 
1037 static const TypeInfo qemu_s390_cpu_type_info = {
1038     .name = S390_CPU_TYPE_NAME("qemu"),
1039     .parent = TYPE_S390_CPU,
1040     .instance_init = s390_qemu_cpu_model_initfn,
1041     .instance_finalize = s390_cpu_model_finalize,
1042     .class_init = s390_qemu_cpu_model_class_init,
1043 };
1044 
1045 #ifdef CONFIG_KVM
1046 static const TypeInfo host_s390_cpu_type_info = {
1047     .name = S390_CPU_TYPE_NAME("host"),
1048     .parent = TYPE_S390_CPU,
1049     .instance_init = s390_host_cpu_model_initfn,
1050     .instance_finalize = s390_cpu_model_finalize,
1051     .class_init = s390_host_cpu_model_class_init,
1052 };
1053 #endif
1054 
1055 static void register_types(void)
1056 {
1057     int i;
1058 
1059     /* init all bitmaps from gnerated data initially */
1060     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
1061         s390_init_feat_bitmap(s390_cpu_defs[i].base_init,
1062                               s390_cpu_defs[i].base_feat);
1063         s390_init_feat_bitmap(s390_cpu_defs[i].default_init,
1064                               s390_cpu_defs[i].default_feat);
1065         s390_init_feat_bitmap(s390_cpu_defs[i].full_init,
1066                               s390_cpu_defs[i].full_feat);
1067     }
1068 
1069     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
1070         char *base_name = s390_base_cpu_type_name(s390_cpu_defs[i].name);
1071         TypeInfo ti_base = {
1072             .name = base_name,
1073             .parent = TYPE_S390_CPU,
1074             .instance_init = s390_cpu_model_initfn,
1075             .instance_finalize = s390_cpu_model_finalize,
1076             .class_init = s390_base_cpu_model_class_init,
1077             .class_data = (void *) &s390_cpu_defs[i],
1078         };
1079         char *name = s390_cpu_type_name(s390_cpu_defs[i].name);
1080         TypeInfo ti = {
1081             .name = name,
1082             .parent = TYPE_S390_CPU,
1083             .instance_init = s390_cpu_model_initfn,
1084             .instance_finalize = s390_cpu_model_finalize,
1085             .class_init = s390_cpu_model_class_init,
1086             .class_data = (void *) &s390_cpu_defs[i],
1087         };
1088 
1089         type_register_static(&ti_base);
1090         type_register_static(&ti);
1091         g_free(base_name);
1092         g_free(name);
1093     }
1094 
1095     type_register_static(&qemu_s390_cpu_type_info);
1096 #ifdef CONFIG_KVM
1097     type_register_static(&host_s390_cpu_type_info);
1098 #endif
1099 }
1100 
1101 type_init(register_types)
1102