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