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