Merge tag 'qemu-macppc-20230206' of https://github.com/mcayland/qemu into staging
[qemu.git] / target / s390x / cpu_models.c
blob065ec6d66cdbe3c8e3a08b0752f585667e931b0c
1 /*
2 * CPU models for s390x
4 * Copyright 2016 IBM Corp.
6 * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
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.
13 #include "qemu/osdep.h"
14 #include "cpu.h"
15 #include "s390x-internal.h"
16 #include "kvm/kvm_s390x.h"
17 #include "sysemu/kvm.h"
18 #include "sysemu/tcg.h"
19 #include "qapi/error.h"
20 #include "qapi/visitor.h"
21 #include "qemu/module.h"
22 #include "qemu/hw-version.h"
23 #include "qemu/qemu-print.h"
24 #ifndef CONFIG_USER_ONLY
25 #include "sysemu/sysemu.h"
26 #include "hw/s390x/pv.h"
27 #endif
29 #define CPUDEF_INIT(_type, _gen, _ec_ga, _mha_pow, _hmfai, _name, _desc) \
30 { \
31 .name = _name, \
32 .type = _type, \
33 .gen = _gen, \
34 .ec_ga = _ec_ga, \
35 .mha_pow = _mha_pow, \
36 .hmfai = _hmfai, \
37 .desc = _desc, \
38 .base_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _BASE }, \
39 .default_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _DEFAULT }, \
40 .full_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _FULL }, \
44 * CPU definition list in order of release. Up to generation 14 base features
45 * of a following release have been a superset of the previous release. With
46 * generation 15 one base feature and one optional feature have been deprecated.
48 static S390CPUDef s390_cpu_defs[] = {
49 CPUDEF_INIT(0x2064, 7, 1, 38, 0x00000000U, "z900", "IBM zSeries 900 GA1"),
50 CPUDEF_INIT(0x2064, 7, 2, 38, 0x00000000U, "z900.2", "IBM zSeries 900 GA2"),
51 CPUDEF_INIT(0x2064, 7, 3, 38, 0x00000000U, "z900.3", "IBM zSeries 900 GA3"),
52 CPUDEF_INIT(0x2066, 7, 3, 38, 0x00000000U, "z800", "IBM zSeries 800 GA1"),
53 CPUDEF_INIT(0x2084, 8, 1, 38, 0x00000000U, "z990", "IBM zSeries 990 GA1"),
54 CPUDEF_INIT(0x2084, 8, 2, 38, 0x00000000U, "z990.2", "IBM zSeries 990 GA2"),
55 CPUDEF_INIT(0x2084, 8, 3, 38, 0x00000000U, "z990.3", "IBM zSeries 990 GA3"),
56 CPUDEF_INIT(0x2086, 8, 3, 38, 0x00000000U, "z890", "IBM zSeries 880 GA1"),
57 CPUDEF_INIT(0x2084, 8, 4, 38, 0x00000000U, "z990.4", "IBM zSeries 990 GA4"),
58 CPUDEF_INIT(0x2086, 8, 4, 38, 0x00000000U, "z890.2", "IBM zSeries 880 GA2"),
59 CPUDEF_INIT(0x2084, 8, 5, 38, 0x00000000U, "z990.5", "IBM zSeries 990 GA5"),
60 CPUDEF_INIT(0x2086, 8, 5, 38, 0x00000000U, "z890.3", "IBM zSeries 880 GA3"),
61 CPUDEF_INIT(0x2094, 9, 1, 40, 0x00000000U, "z9EC", "IBM System z9 EC GA1"),
62 CPUDEF_INIT(0x2094, 9, 2, 40, 0x00000000U, "z9EC.2", "IBM System z9 EC GA2"),
63 CPUDEF_INIT(0x2096, 9, 2, 40, 0x00000000U, "z9BC", "IBM System z9 BC GA1"),
64 CPUDEF_INIT(0x2094, 9, 3, 40, 0x00000000U, "z9EC.3", "IBM System z9 EC GA3"),
65 CPUDEF_INIT(0x2096, 9, 3, 40, 0x00000000U, "z9BC.2", "IBM System z9 BC GA2"),
66 CPUDEF_INIT(0x2097, 10, 1, 43, 0x00000000U, "z10EC", "IBM System z10 EC GA1"),
67 CPUDEF_INIT(0x2097, 10, 2, 43, 0x00000000U, "z10EC.2", "IBM System z10 EC GA2"),
68 CPUDEF_INIT(0x2098, 10, 2, 43, 0x00000000U, "z10BC", "IBM System z10 BC GA1"),
69 CPUDEF_INIT(0x2097, 10, 3, 43, 0x00000000U, "z10EC.3", "IBM System z10 EC GA3"),
70 CPUDEF_INIT(0x2098, 10, 3, 43, 0x00000000U, "z10BC.2", "IBM System z10 BC GA2"),
71 CPUDEF_INIT(0x2817, 11, 1, 44, 0x08000000U, "z196", "IBM zEnterprise 196 GA1"),
72 CPUDEF_INIT(0x2817, 11, 2, 44, 0x08000000U, "z196.2", "IBM zEnterprise 196 GA2"),
73 CPUDEF_INIT(0x2818, 11, 2, 44, 0x08000000U, "z114", "IBM zEnterprise 114 GA1"),
74 CPUDEF_INIT(0x2827, 12, 1, 44, 0x08000000U, "zEC12", "IBM zEnterprise EC12 GA1"),
75 CPUDEF_INIT(0x2827, 12, 2, 44, 0x08000000U, "zEC12.2", "IBM zEnterprise EC12 GA2"),
76 CPUDEF_INIT(0x2828, 12, 2, 44, 0x08000000U, "zBC12", "IBM zEnterprise BC12 GA1"),
77 CPUDEF_INIT(0x2964, 13, 1, 47, 0x08000000U, "z13", "IBM z13 GA1"),
78 CPUDEF_INIT(0x2964, 13, 2, 47, 0x08000000U, "z13.2", "IBM z13 GA2"),
79 CPUDEF_INIT(0x2965, 13, 2, 47, 0x08000000U, "z13s", "IBM z13s GA1"),
80 CPUDEF_INIT(0x3906, 14, 1, 47, 0x08000000U, "z14", "IBM z14 GA1"),
81 CPUDEF_INIT(0x3906, 14, 2, 47, 0x08000000U, "z14.2", "IBM z14 GA2"),
82 CPUDEF_INIT(0x3907, 14, 1, 47, 0x08000000U, "z14ZR1", "IBM z14 Model ZR1 GA1"),
83 CPUDEF_INIT(0x8561, 15, 1, 47, 0x08000000U, "gen15a", "IBM z15 T01 GA1"),
84 CPUDEF_INIT(0x8562, 15, 1, 47, 0x08000000U, "gen15b", "IBM z15 T02 GA1"),
85 CPUDEF_INIT(0x3931, 16, 1, 47, 0x08000000U, "gen16a", "IBM 3931 GA1"),
86 CPUDEF_INIT(0x3932, 16, 1, 47, 0x08000000U, "gen16b", "IBM 3932 GA1"),
89 #define QEMU_MAX_CPU_TYPE 0x8561
90 #define QEMU_MAX_CPU_GEN 15
91 #define QEMU_MAX_CPU_EC_GA 1
92 static S390FeatBitmap qemu_max_cpu_feat;
94 /* features part of a base model but not relevant for finding a base model */
95 S390FeatBitmap ignored_base_feat;
97 void s390_cpudef_featoff(uint8_t gen, uint8_t ec_ga, S390Feat feat)
99 const S390CPUDef *def;
101 def = s390_find_cpu_def(0, gen, ec_ga, NULL);
102 clear_bit(feat, (unsigned long *)&def->default_feat);
105 void s390_cpudef_featoff_greater(uint8_t gen, uint8_t ec_ga, S390Feat feat)
107 int i;
109 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
110 const S390CPUDef *def = &s390_cpu_defs[i];
112 if (def->gen < gen) {
113 continue;
115 if (def->gen == gen && def->ec_ga < ec_ga) {
116 continue;
119 clear_bit(feat, (unsigned long *)&def->default_feat);
123 void s390_cpudef_group_featoff_greater(uint8_t gen, uint8_t ec_ga,
124 S390FeatGroup group)
126 const S390FeatGroupDef *group_def = s390_feat_group_def(group);
127 S390FeatBitmap group_def_off;
128 int i;
130 bitmap_complement(group_def_off, group_def->feat, S390_FEAT_MAX);
132 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
133 const S390CPUDef *cpu_def = &s390_cpu_defs[i];
135 if (cpu_def->gen < gen) {
136 continue;
138 if (cpu_def->gen == gen && cpu_def->ec_ga < ec_ga) {
139 continue;
142 bitmap_and((unsigned long *)&cpu_def->default_feat,
143 cpu_def->default_feat, group_def_off, S390_FEAT_MAX);
147 uint32_t s390_get_hmfai(void)
149 static S390CPU *cpu;
151 if (!cpu) {
152 cpu = S390_CPU(qemu_get_cpu(0));
155 if (!cpu || !cpu->model) {
156 return 0;
158 return cpu->model->def->hmfai;
161 uint8_t s390_get_mha_pow(void)
163 static S390CPU *cpu;
165 if (!cpu) {
166 cpu = S390_CPU(qemu_get_cpu(0));
169 if (!cpu || !cpu->model) {
170 return 0;
172 return cpu->model->def->mha_pow;
175 uint32_t s390_get_ibc_val(void)
177 uint16_t unblocked_ibc, lowest_ibc;
178 static S390CPU *cpu;
180 if (!cpu) {
181 cpu = S390_CPU(qemu_get_cpu(0));
184 if (!cpu || !cpu->model) {
185 return 0;
187 unblocked_ibc = s390_ibc_from_cpu_model(cpu->model);
188 lowest_ibc = cpu->model->lowest_ibc;
189 /* the lowest_ibc always has to be <= unblocked_ibc */
190 if (!lowest_ibc || lowest_ibc > unblocked_ibc) {
191 return 0;
193 return ((uint32_t) lowest_ibc << 16) | unblocked_ibc;
196 void s390_get_feat_block(S390FeatType type, uint8_t *data)
198 static S390CPU *cpu;
200 if (!cpu) {
201 cpu = S390_CPU(qemu_get_cpu(0));
204 if (!cpu || !cpu->model) {
205 return;
207 s390_fill_feat_block(cpu->model->features, type, data);
210 bool s390_has_feat(S390Feat feat)
212 static S390CPU *cpu;
214 if (!cpu) {
215 cpu = S390_CPU(qemu_get_cpu(0));
218 if (!cpu || !cpu->model) {
219 #ifdef CONFIG_KVM
220 if (kvm_enabled()) {
221 if (feat == S390_FEAT_VECTOR) {
222 return kvm_check_extension(kvm_state,
223 KVM_CAP_S390_VECTOR_REGISTERS);
225 if (feat == S390_FEAT_RUNTIME_INSTRUMENTATION) {
226 return kvm_s390_get_ri();
228 if (feat == S390_FEAT_MSA_EXT_3) {
229 return true;
232 #endif
233 if (feat == S390_FEAT_ZPCI) {
234 return true;
236 return 0;
239 #ifndef CONFIG_USER_ONLY
240 if (s390_is_pv()) {
241 switch (feat) {
242 case S390_FEAT_DIAG_318:
243 case S390_FEAT_HPMA2:
244 case S390_FEAT_SIE_F2:
245 case S390_FEAT_SIE_SKEY:
246 case S390_FEAT_SIE_GPERE:
247 case S390_FEAT_SIE_SIIF:
248 case S390_FEAT_SIE_SIGPIF:
249 case S390_FEAT_SIE_IB:
250 case S390_FEAT_SIE_CEI:
251 case S390_FEAT_SIE_KSS:
252 case S390_FEAT_SIE_GSLS:
253 case S390_FEAT_SIE_64BSCAO:
254 case S390_FEAT_SIE_CMMA:
255 case S390_FEAT_SIE_PFMFI:
256 case S390_FEAT_SIE_IBS:
257 return false;
258 break;
259 default:
260 break;
263 #endif
264 return test_bit(feat, cpu->model->features);
267 uint8_t s390_get_gen_for_cpu_type(uint16_t type)
269 int i;
271 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
272 if (s390_cpu_defs[i].type == type) {
273 return s390_cpu_defs[i].gen;
276 return 0;
279 const S390CPUDef *s390_find_cpu_def(uint16_t type, uint8_t gen, uint8_t ec_ga,
280 S390FeatBitmap features)
282 const S390CPUDef *last_compatible = NULL;
283 const S390CPUDef *matching_cpu_type = NULL;
284 int i;
286 if (!gen) {
287 ec_ga = 0;
289 if (!gen && type) {
290 gen = s390_get_gen_for_cpu_type(type);
293 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
294 const S390CPUDef *def = &s390_cpu_defs[i];
295 S390FeatBitmap missing;
297 /* don't even try newer generations if we know the generation */
298 if (gen) {
299 if (def->gen > gen) {
300 break;
301 } else if (def->gen == gen && ec_ga && def->ec_ga > ec_ga) {
302 break;
306 if (features) {
307 /* see if the model satisfies the minimum features */
308 bitmap_andnot(missing, def->base_feat, features, S390_FEAT_MAX);
310 * Ignore certain features that are in the base model, but not
311 * relevant for the search (esp. MSA subfunctions).
313 bitmap_andnot(missing, missing, ignored_base_feat, S390_FEAT_MAX);
314 if (!bitmap_empty(missing, S390_FEAT_MAX)) {
315 break;
319 /* stop the search if we found the exact model */
320 if (def->type == type && def->ec_ga == ec_ga) {
321 return def;
323 /* remember if we've at least seen one with the same cpu type */
324 if (def->type == type) {
325 matching_cpu_type = def;
327 last_compatible = def;
329 /* prefer the model with the same cpu type, esp. don't take the BC for EC */
330 if (matching_cpu_type) {
331 return matching_cpu_type;
333 return last_compatible;
336 static void s390_print_cpu_model_list_entry(gpointer data, gpointer user_data)
338 const S390CPUClass *scc = S390_CPU_CLASS((ObjectClass *)data);
339 CPUClass *cc = CPU_CLASS(scc);
340 char *name = g_strdup(object_class_get_name((ObjectClass *)data));
341 g_autoptr(GString) details = g_string_new("");
343 if (scc->is_static) {
344 g_string_append(details, "static, ");
346 if (scc->is_migration_safe) {
347 g_string_append(details, "migration-safe, ");
349 if (cc->deprecation_note) {
350 g_string_append(details, "deprecated, ");
352 if (details->len) {
353 /* cull trailing ', ' */
354 g_string_truncate(details, details->len - 2);
357 /* strip off the -s390x-cpu */
358 g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
359 if (details->len) {
360 qemu_printf("s390 %-15s %-35s (%s)\n", name, scc->desc, details->str);
361 } else {
362 qemu_printf("s390 %-15s %-35s\n", name, scc->desc);
364 g_free(name);
367 static gint s390_cpu_list_compare(gconstpointer a, gconstpointer b)
369 const S390CPUClass *cc_a = S390_CPU_CLASS((ObjectClass *)a);
370 const S390CPUClass *cc_b = S390_CPU_CLASS((ObjectClass *)b);
371 const char *name_a = object_class_get_name((ObjectClass *)a);
372 const char *name_b = object_class_get_name((ObjectClass *)b);
375 * Move qemu, host and max to the top of the list, qemu first, host second,
376 * max third.
378 if (name_a[0] == 'q') {
379 return -1;
380 } else if (name_b[0] == 'q') {
381 return 1;
382 } else if (name_a[0] == 'h') {
383 return -1;
384 } else if (name_b[0] == 'h') {
385 return 1;
386 } else if (name_a[0] == 'm') {
387 return -1;
388 } else if (name_b[0] == 'm') {
389 return 1;
392 /* keep the same order we have in our table (sorted by release date) */
393 if (cc_a->cpu_def != cc_b->cpu_def) {
394 return cc_a->cpu_def - cc_b->cpu_def;
397 /* exact same definition - list base model first */
398 return cc_a->is_static ? -1 : 1;
401 void s390_cpu_list(void)
403 S390FeatGroup group;
404 S390Feat feat;
405 GSList *list;
407 list = object_class_get_list(TYPE_S390_CPU, false);
408 list = g_slist_sort(list, s390_cpu_list_compare);
409 g_slist_foreach(list, s390_print_cpu_model_list_entry, NULL);
410 g_slist_free(list);
412 qemu_printf("\nRecognized feature flags:\n");
413 for (feat = 0; feat < S390_FEAT_MAX; feat++) {
414 const S390FeatDef *def = s390_feat_def(feat);
416 qemu_printf("%-20s %s\n", def->name, def->desc);
419 qemu_printf("\nRecognized feature groups:\n");
420 for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
421 const S390FeatGroupDef *def = s390_feat_group_def(group);
423 qemu_printf("%-20s %s\n", def->name, def->desc);
427 static void check_consistency(const S390CPUModel *model)
429 static int dep[][2] = {
430 { S390_FEAT_IPTE_RANGE, S390_FEAT_DAT_ENH },
431 { S390_FEAT_IDTE_SEGMENT, S390_FEAT_DAT_ENH },
432 { S390_FEAT_IDTE_REGION, S390_FEAT_DAT_ENH },
433 { S390_FEAT_IDTE_REGION, S390_FEAT_IDTE_SEGMENT },
434 { S390_FEAT_LOCAL_TLB_CLEARING, S390_FEAT_DAT_ENH},
435 { S390_FEAT_LONG_DISPLACEMENT_FAST, S390_FEAT_LONG_DISPLACEMENT },
436 { S390_FEAT_DFP_FAST, S390_FEAT_DFP },
437 { S390_FEAT_TRANSACTIONAL_EXE, S390_FEAT_STFLE_49 },
438 { S390_FEAT_EDAT_2, S390_FEAT_EDAT},
439 { S390_FEAT_MSA_EXT_5, S390_FEAT_KIMD_SHA_512 },
440 { S390_FEAT_MSA_EXT_5, S390_FEAT_KLMD_SHA_512 },
441 { S390_FEAT_MSA_EXT_4, S390_FEAT_MSA_EXT_3 },
442 { S390_FEAT_SIE_CMMA, S390_FEAT_CMM },
443 { S390_FEAT_SIE_CMMA, S390_FEAT_SIE_GSLS },
444 { S390_FEAT_SIE_PFMFI, S390_FEAT_EDAT },
445 { S390_FEAT_MSA_EXT_8, S390_FEAT_MSA_EXT_3 },
446 { S390_FEAT_MSA_EXT_9, S390_FEAT_MSA_EXT_3 },
447 { S390_FEAT_MSA_EXT_9, S390_FEAT_MSA_EXT_4 },
448 { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING },
449 { S390_FEAT_VECTOR_PACKED_DECIMAL, S390_FEAT_VECTOR },
450 { S390_FEAT_VECTOR_PACKED_DECIMAL_ENH, S390_FEAT_VECTOR_PACKED_DECIMAL },
451 { S390_FEAT_VECTOR_PACKED_DECIMAL_ENH2, S390_FEAT_VECTOR_PACKED_DECIMAL_ENH },
452 { S390_FEAT_VECTOR_ENH, S390_FEAT_VECTOR },
453 { S390_FEAT_INSTRUCTION_EXEC_PROT, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 },
454 { S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2, S390_FEAT_ESOP },
455 { S390_FEAT_CMM_NT, S390_FEAT_CMM },
456 { S390_FEAT_GUARDED_STORAGE, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 },
457 { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_STORE_CLOCK_FAST },
458 { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING },
459 { S390_FEAT_SEMAPHORE_ASSIST, S390_FEAT_STFLE_49 },
460 { S390_FEAT_KIMD_SHA3_224, S390_FEAT_MSA },
461 { S390_FEAT_KIMD_SHA3_256, S390_FEAT_MSA },
462 { S390_FEAT_KIMD_SHA3_384, S390_FEAT_MSA },
463 { S390_FEAT_KIMD_SHA3_512, S390_FEAT_MSA },
464 { S390_FEAT_KIMD_SHAKE_128, S390_FEAT_MSA },
465 { S390_FEAT_KIMD_SHAKE_256, S390_FEAT_MSA },
466 { S390_FEAT_KLMD_SHA3_224, S390_FEAT_MSA },
467 { S390_FEAT_KLMD_SHA3_256, S390_FEAT_MSA },
468 { S390_FEAT_KLMD_SHA3_384, S390_FEAT_MSA },
469 { S390_FEAT_KLMD_SHA3_512, S390_FEAT_MSA },
470 { S390_FEAT_KLMD_SHAKE_128, S390_FEAT_MSA },
471 { S390_FEAT_KLMD_SHAKE_256, S390_FEAT_MSA },
472 { S390_FEAT_PRNO_TRNG_QRTCR, S390_FEAT_MSA_EXT_5 },
473 { S390_FEAT_PRNO_TRNG, S390_FEAT_MSA_EXT_5 },
474 { S390_FEAT_SIE_KSS, S390_FEAT_SIE_F2 },
475 { S390_FEAT_AP_QUERY_CONFIG_INFO, S390_FEAT_AP },
476 { S390_FEAT_AP_FACILITIES_TEST, S390_FEAT_AP },
477 { S390_FEAT_PTFF_QSIE, S390_FEAT_MULTIPLE_EPOCH },
478 { S390_FEAT_PTFF_QTOUE, S390_FEAT_MULTIPLE_EPOCH },
479 { S390_FEAT_PTFF_STOE, S390_FEAT_MULTIPLE_EPOCH },
480 { S390_FEAT_PTFF_STOUE, S390_FEAT_MULTIPLE_EPOCH },
481 { S390_FEAT_AP_QUEUE_INTERRUPT_CONTROL, S390_FEAT_AP },
482 { S390_FEAT_DIAG_318, S390_FEAT_EXTENDED_LENGTH_SCCB },
483 { S390_FEAT_NNPA, S390_FEAT_VECTOR },
484 { S390_FEAT_RDP, S390_FEAT_LOCAL_TLB_CLEARING },
486 int i;
488 for (i = 0; i < ARRAY_SIZE(dep); i++) {
489 if (test_bit(dep[i][0], model->features) &&
490 !test_bit(dep[i][1], model->features)) {
491 warn_report("\'%s\' requires \'%s\'.",
492 s390_feat_def(dep[i][0])->name,
493 s390_feat_def(dep[i][1])->name);
498 static void error_prepend_missing_feat(const char *name, void *opaque)
500 error_prepend((Error **) opaque, "%s ", name);
503 static void check_compatibility(const S390CPUModel *max_model,
504 const S390CPUModel *model, Error **errp)
506 S390FeatBitmap missing;
508 if (model->def->gen > max_model->def->gen) {
509 error_setg(errp, "Selected CPU generation is too new. Maximum "
510 "supported model in the configuration: \'%s\'",
511 max_model->def->name);
512 return;
513 } else if (model->def->gen == max_model->def->gen &&
514 model->def->ec_ga > max_model->def->ec_ga) {
515 error_setg(errp, "Selected CPU GA level is too new. Maximum "
516 "supported model in the configuration: \'%s\'",
517 max_model->def->name);
518 return;
521 #ifndef CONFIG_USER_ONLY
522 if (only_migratable && test_bit(S390_FEAT_UNPACK, model->features)) {
523 error_setg(errp, "The unpack facility is not compatible with "
524 "the --only-migratable option. You must remove either "
525 "the 'unpack' facility or the --only-migratable option");
526 return;
528 #endif
530 /* detect the missing features to properly report them */
531 bitmap_andnot(missing, model->features, max_model->features, S390_FEAT_MAX);
532 if (bitmap_empty(missing, S390_FEAT_MAX)) {
533 return;
536 error_setg(errp, " ");
537 s390_feat_bitmap_to_ascii(missing, errp, error_prepend_missing_feat);
538 error_prepend(errp, "Some features requested in the CPU model are not "
539 "available in the configuration: ");
542 S390CPUModel *get_max_cpu_model(Error **errp)
544 Error *err = NULL;
545 static S390CPUModel max_model;
546 static bool cached;
548 if (cached) {
549 return &max_model;
552 if (kvm_enabled()) {
553 kvm_s390_get_host_cpu_model(&max_model, &err);
554 } else {
555 max_model.def = s390_find_cpu_def(QEMU_MAX_CPU_TYPE, QEMU_MAX_CPU_GEN,
556 QEMU_MAX_CPU_EC_GA, NULL);
557 bitmap_copy(max_model.features, qemu_max_cpu_feat, S390_FEAT_MAX);
559 if (err) {
560 error_propagate(errp, err);
561 return NULL;
563 cached = true;
564 return &max_model;
567 void s390_realize_cpu_model(CPUState *cs, Error **errp)
569 Error *err = NULL;
570 S390CPUClass *xcc = S390_CPU_GET_CLASS(cs);
571 S390CPU *cpu = S390_CPU(cs);
572 const S390CPUModel *max_model;
574 if (xcc->kvm_required && !kvm_enabled()) {
575 error_setg(errp, "CPU definition requires KVM");
576 return;
579 if (!cpu->model) {
580 /* no host model support -> perform compatibility stuff */
581 apply_cpu_model(NULL, errp);
582 return;
585 max_model = get_max_cpu_model(errp);
586 if (!max_model) {
587 error_prepend(errp, "CPU models are not available: ");
588 return;
591 /* copy over properties that can vary */
592 cpu->model->lowest_ibc = max_model->lowest_ibc;
593 cpu->model->cpu_id = max_model->cpu_id;
594 cpu->model->cpu_id_format = max_model->cpu_id_format;
595 cpu->model->cpu_ver = max_model->cpu_ver;
597 check_consistency(cpu->model);
598 check_compatibility(max_model, cpu->model, &err);
599 if (err) {
600 error_propagate(errp, err);
601 return;
604 apply_cpu_model(cpu->model, errp);
606 #if !defined(CONFIG_USER_ONLY)
607 cpu->env.cpuid = s390_cpuid_from_cpu_model(cpu->model);
608 if (tcg_enabled()) {
609 /* basic mode, write the cpu address into the first 4 bit of the ID */
610 cpu->env.cpuid = deposit64(cpu->env.cpuid, 54, 4, cpu->env.core_id);
612 #endif
615 static void get_feature(Object *obj, Visitor *v, const char *name,
616 void *opaque, Error **errp)
618 S390Feat feat = (S390Feat) (uintptr_t) opaque;
619 S390CPU *cpu = S390_CPU(obj);
620 bool value;
622 if (!cpu->model) {
623 error_setg(errp, "Details about the host CPU model are not available, "
624 "features cannot be queried.");
625 return;
628 value = test_bit(feat, cpu->model->features);
629 visit_type_bool(v, name, &value, errp);
632 static void set_feature(Object *obj, Visitor *v, const char *name,
633 void *opaque, Error **errp)
635 S390Feat feat = (S390Feat) (uintptr_t) opaque;
636 DeviceState *dev = DEVICE(obj);
637 S390CPU *cpu = S390_CPU(obj);
638 bool value;
640 if (dev->realized) {
641 error_setg(errp, "Attempt to set property '%s' on '%s' after "
642 "it was realized", name, object_get_typename(obj));
643 return;
644 } else if (!cpu->model) {
645 error_setg(errp, "Details about the host CPU model are not available, "
646 "features cannot be changed.");
647 return;
650 if (!visit_type_bool(v, name, &value, errp)) {
651 return;
653 if (value) {
654 if (!test_bit(feat, cpu->model->def->full_feat)) {
655 error_setg(errp, "Feature '%s' is not available for CPU model '%s',"
656 " it was introduced with later models.",
657 name, cpu->model->def->name);
658 return;
660 set_bit(feat, cpu->model->features);
661 } else {
662 clear_bit(feat, cpu->model->features);
666 static void get_feature_group(Object *obj, Visitor *v, const char *name,
667 void *opaque, Error **errp)
669 S390FeatGroup group = (S390FeatGroup) (uintptr_t) opaque;
670 const S390FeatGroupDef *def = s390_feat_group_def(group);
671 S390CPU *cpu = S390_CPU(obj);
672 S390FeatBitmap tmp;
673 bool value;
675 if (!cpu->model) {
676 error_setg(errp, "Details about the host CPU model are not available, "
677 "features cannot be queried.");
678 return;
681 /* a group is enabled if all features are enabled */
682 bitmap_and(tmp, cpu->model->features, def->feat, S390_FEAT_MAX);
683 value = bitmap_equal(tmp, def->feat, S390_FEAT_MAX);
684 visit_type_bool(v, name, &value, errp);
687 static void set_feature_group(Object *obj, Visitor *v, const char *name,
688 void *opaque, Error **errp)
690 S390FeatGroup group = (S390FeatGroup) (uintptr_t) opaque;
691 const S390FeatGroupDef *def = s390_feat_group_def(group);
692 DeviceState *dev = DEVICE(obj);
693 S390CPU *cpu = S390_CPU(obj);
694 bool value;
696 if (dev->realized) {
697 error_setg(errp, "Attempt to set property '%s' on '%s' after "
698 "it was realized", name, object_get_typename(obj));
699 return;
700 } else if (!cpu->model) {
701 error_setg(errp, "Details about the host CPU model are not available, "
702 "features cannot be changed.");
703 return;
706 if (!visit_type_bool(v, name, &value, errp)) {
707 return;
709 if (value) {
710 /* groups are added in one shot, so an intersect is sufficient */
711 if (!bitmap_intersects(def->feat, cpu->model->def->full_feat,
712 S390_FEAT_MAX)) {
713 error_setg(errp, "Group '%s' is not available for CPU model '%s',"
714 " it was introduced with later models.",
715 name, cpu->model->def->name);
716 return;
718 bitmap_or(cpu->model->features, cpu->model->features, def->feat,
719 S390_FEAT_MAX);
720 } else {
721 bitmap_andnot(cpu->model->features, cpu->model->features, def->feat,
722 S390_FEAT_MAX);
726 static void s390_cpu_model_initfn(Object *obj)
728 S390CPU *cpu = S390_CPU(obj);
729 S390CPUClass *xcc = S390_CPU_GET_CLASS(cpu);
731 cpu->model = g_malloc0(sizeof(*cpu->model));
732 /* copy the model, so we can modify it */
733 cpu->model->def = xcc->cpu_def;
734 if (xcc->is_static) {
735 /* base model - features will never change */
736 bitmap_copy(cpu->model->features, cpu->model->def->base_feat,
737 S390_FEAT_MAX);
738 } else {
739 /* latest model - features can change */
740 bitmap_copy(cpu->model->features,
741 cpu->model->def->default_feat, S390_FEAT_MAX);
745 static S390CPUModel s390_qemu_cpu_model;
747 /* Set the qemu CPU model (on machine initialization). Must not be called
748 * once CPUs have been created.
750 void s390_set_qemu_cpu_model(uint16_t type, uint8_t gen, uint8_t ec_ga,
751 const S390FeatInit feat_init)
753 const S390CPUDef *def = s390_find_cpu_def(type, gen, ec_ga, NULL);
755 g_assert(def);
756 g_assert(QTAILQ_EMPTY_RCU(&cpus));
758 /* build the CPU model */
759 s390_qemu_cpu_model.def = def;
760 bitmap_zero(s390_qemu_cpu_model.features, S390_FEAT_MAX);
761 s390_init_feat_bitmap(feat_init, s390_qemu_cpu_model.features);
764 static void s390_qemu_cpu_model_initfn(Object *obj)
766 S390CPU *cpu = S390_CPU(obj);
768 cpu->model = g_malloc0(sizeof(*cpu->model));
769 /* copy the CPU model so we can modify it */
770 memcpy(cpu->model, &s390_qemu_cpu_model, sizeof(*cpu->model));
773 static void s390_max_cpu_model_initfn(Object *obj)
775 const S390CPUModel *max_model;
776 S390CPU *cpu = S390_CPU(obj);
777 Error *local_err = NULL;
779 if (kvm_enabled() && !kvm_s390_cpu_models_supported()) {
780 /* "max" and "host" always work, even without CPU model support */
781 return;
784 max_model = get_max_cpu_model(&local_err);
785 if (local_err) {
786 /* we expect errors only under KVM, when actually querying the kernel */
787 g_assert(kvm_enabled());
788 error_report_err(local_err);
789 /* fallback to unsupported CPU models */
790 return;
793 cpu->model = g_new(S390CPUModel, 1);
794 /* copy the CPU model so we can modify it */
795 memcpy(cpu->model, max_model, sizeof(*cpu->model));
798 static void s390_cpu_model_finalize(Object *obj)
800 S390CPU *cpu = S390_CPU(obj);
802 g_free(cpu->model);
803 cpu->model = NULL;
806 static bool get_is_migration_safe(Object *obj, Error **errp)
808 return S390_CPU_GET_CLASS(obj)->is_migration_safe;
811 static bool get_is_static(Object *obj, Error **errp)
813 return S390_CPU_GET_CLASS(obj)->is_static;
816 static char *get_description(Object *obj, Error **errp)
818 return g_strdup(S390_CPU_GET_CLASS(obj)->desc);
821 void s390_cpu_model_class_register_props(ObjectClass *oc)
823 S390FeatGroup group;
824 S390Feat feat;
826 object_class_property_add_bool(oc, "migration-safe", get_is_migration_safe,
827 NULL);
828 object_class_property_add_bool(oc, "static", get_is_static,
829 NULL);
830 object_class_property_add_str(oc, "description", get_description, NULL);
832 for (feat = 0; feat < S390_FEAT_MAX; feat++) {
833 const S390FeatDef *def = s390_feat_def(feat);
834 object_class_property_add(oc, def->name, "bool", get_feature,
835 set_feature, NULL, (void *) feat);
836 object_class_property_set_description(oc, def->name, def->desc);
838 for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
839 const S390FeatGroupDef *def = s390_feat_group_def(group);
840 object_class_property_add(oc, def->name, "bool", get_feature_group,
841 set_feature_group, NULL, (void *) group);
842 object_class_property_set_description(oc, def->name, def->desc);
846 #ifdef CONFIG_KVM
847 static void s390_host_cpu_model_class_init(ObjectClass *oc, void *data)
849 S390CPUClass *xcc = S390_CPU_CLASS(oc);
851 xcc->kvm_required = true;
852 xcc->desc = "KVM only: All recognized features";
854 #endif
856 static void s390_base_cpu_model_class_init(ObjectClass *oc, void *data)
858 S390CPUClass *xcc = S390_CPU_CLASS(oc);
860 /* all base models are migration safe */
861 xcc->cpu_def = (const S390CPUDef *) data;
862 xcc->is_migration_safe = true;
863 xcc->is_static = true;
864 xcc->desc = xcc->cpu_def->desc;
867 static void s390_cpu_model_class_init(ObjectClass *oc, void *data)
869 S390CPUClass *xcc = S390_CPU_CLASS(oc);
871 /* model that can change between QEMU versions */
872 xcc->cpu_def = (const S390CPUDef *) data;
873 xcc->is_migration_safe = true;
874 xcc->desc = xcc->cpu_def->desc;
877 static void s390_qemu_cpu_model_class_init(ObjectClass *oc, void *data)
879 S390CPUClass *xcc = S390_CPU_CLASS(oc);
881 xcc->is_migration_safe = true;
882 xcc->desc = g_strdup_printf("QEMU Virtual CPU version %s",
883 qemu_hw_version());
886 static void s390_max_cpu_model_class_init(ObjectClass *oc, void *data)
888 S390CPUClass *xcc = S390_CPU_CLASS(oc);
891 * The "max" model is neither static nor migration safe. Under KVM
892 * it represents the "host" model. Under TCG it represents the "qemu" CPU
893 * model of the latest QEMU machine.
895 xcc->desc =
896 "Enables all features supported by the accelerator in the current host";
899 /* Generate type name for a cpu model. Caller has to free the string. */
900 static char *s390_cpu_type_name(const char *model_name)
902 return g_strdup_printf(S390_CPU_TYPE_NAME("%s"), model_name);
905 /* Generate type name for a base cpu model. Caller has to free the string. */
906 static char *s390_base_cpu_type_name(const char *model_name)
908 return g_strdup_printf(S390_CPU_TYPE_NAME("%s-base"), model_name);
911 ObjectClass *s390_cpu_class_by_name(const char *name)
913 char *typename = s390_cpu_type_name(name);
914 ObjectClass *oc;
916 oc = object_class_by_name(typename);
917 g_free(typename);
918 return oc;
921 static const TypeInfo qemu_s390_cpu_type_info = {
922 .name = S390_CPU_TYPE_NAME("qemu"),
923 .parent = TYPE_S390_CPU,
924 .instance_init = s390_qemu_cpu_model_initfn,
925 .instance_finalize = s390_cpu_model_finalize,
926 .class_init = s390_qemu_cpu_model_class_init,
929 static const TypeInfo max_s390_cpu_type_info = {
930 .name = S390_CPU_TYPE_NAME("max"),
931 .parent = TYPE_S390_CPU,
932 .instance_init = s390_max_cpu_model_initfn,
933 .instance_finalize = s390_cpu_model_finalize,
934 .class_init = s390_max_cpu_model_class_init,
937 #ifdef CONFIG_KVM
938 static const TypeInfo host_s390_cpu_type_info = {
939 .name = S390_CPU_TYPE_NAME("host"),
940 .parent = S390_CPU_TYPE_NAME("max"),
941 .class_init = s390_host_cpu_model_class_init,
943 #endif
945 static void init_ignored_base_feat(void)
947 static const int feats[] = {
948 /* MSA subfunctions that could not be available on certain machines */
949 S390_FEAT_KMAC_DEA,
950 S390_FEAT_KMAC_TDEA_128,
951 S390_FEAT_KMAC_TDEA_192,
952 S390_FEAT_KMC_DEA,
953 S390_FEAT_KMC_TDEA_128,
954 S390_FEAT_KMC_TDEA_192,
955 S390_FEAT_KM_DEA,
956 S390_FEAT_KM_TDEA_128,
957 S390_FEAT_KM_TDEA_192,
958 S390_FEAT_KIMD_SHA_1,
959 S390_FEAT_KLMD_SHA_1,
960 /* CSSKE is deprecated on newer generations */
961 S390_FEAT_CONDITIONAL_SSKE,
963 int i;
965 for (i = 0; i < ARRAY_SIZE(feats); i++) {
966 set_bit(feats[i], ignored_base_feat);
970 static void register_types(void)
972 static const S390FeatInit qemu_max_init = { S390_FEAT_LIST_QEMU_MAX };
973 int i;
975 init_ignored_base_feat();
977 /* init all bitmaps from gnerated data initially */
978 s390_init_feat_bitmap(qemu_max_init, qemu_max_cpu_feat);
979 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
980 s390_init_feat_bitmap(s390_cpu_defs[i].base_init,
981 s390_cpu_defs[i].base_feat);
982 s390_init_feat_bitmap(s390_cpu_defs[i].default_init,
983 s390_cpu_defs[i].default_feat);
984 s390_init_feat_bitmap(s390_cpu_defs[i].full_init,
985 s390_cpu_defs[i].full_feat);
988 /* initialize the qemu model with the maximum definition ("max" model) */
989 s390_set_qemu_cpu_model(QEMU_MAX_CPU_TYPE, QEMU_MAX_CPU_GEN,
990 QEMU_MAX_CPU_EC_GA, qemu_max_init);
992 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
993 char *base_name = s390_base_cpu_type_name(s390_cpu_defs[i].name);
994 TypeInfo ti_base = {
995 .name = base_name,
996 .parent = TYPE_S390_CPU,
997 .instance_init = s390_cpu_model_initfn,
998 .instance_finalize = s390_cpu_model_finalize,
999 .class_init = s390_base_cpu_model_class_init,
1000 .class_data = (void *) &s390_cpu_defs[i],
1002 char *name = s390_cpu_type_name(s390_cpu_defs[i].name);
1003 TypeInfo ti = {
1004 .name = name,
1005 .parent = TYPE_S390_CPU,
1006 .instance_init = s390_cpu_model_initfn,
1007 .instance_finalize = s390_cpu_model_finalize,
1008 .class_init = s390_cpu_model_class_init,
1009 .class_data = (void *) &s390_cpu_defs[i],
1012 type_register_static(&ti_base);
1013 type_register_static(&ti);
1014 g_free(base_name);
1015 g_free(name);
1018 type_register_static(&qemu_s390_cpu_type_info);
1019 type_register_static(&max_s390_cpu_type_info);
1020 #ifdef CONFIG_KVM
1021 type_register_static(&host_s390_cpu_type_info);
1022 #endif
1025 type_init(register_types)