]> Git Repo - qemu.git/blame - target/s390x/cpu_models.c
Merge remote-tracking branch 'remotes/philmd-gitlab/tags/led-api-20201026' into staging
[qemu.git] / target / s390x / cpu_models.c
CommitLineData
41868f84
DH
1/*
2 * CPU models for s390x
3 *
4 * Copyright 2016 IBM Corp.
5 *
6 * Author(s): David Hildenbrand <[email protected]>
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"
4e58b838 15#include "internal.h"
f16bbb9b
DH
16#include "kvm_s390x.h"
17#include "sysemu/kvm.h"
14a48c1d 18#include "sysemu/tcg.h"
41868f84 19#include "qapi/error.h"
0754f604 20#include "qapi/visitor.h"
80560137 21#include "qemu/error-report.h"
0b8fa32f 22#include "qemu/module.h"
0442428a 23#include "qemu/qemu-print.h"
137974ce 24#include "qapi/qmp/qerror.h"
b3db211f 25#include "qapi/qobject-input-visitor.h"
452fcdbc 26#include "qapi/qmp/qdict.h"
41868f84
DH
27#ifndef CONFIG_USER_ONLY
28#include "sysemu/arch_init.h"
e3fd586d 29#include "hw/pci/pci.h"
41868f84 30#endif
7f7b4e7a 31#include "qapi/qapi-commands-machine-target.h"
3ded270a 32#include "hw/s390x/pv.h"
41868f84 33
6c064de1
DH
34#define CPUDEF_INIT(_type, _gen, _ec_ga, _mha_pow, _hmfai, _name, _desc) \
35 { \
36 .name = _name, \
37 .type = _type, \
38 .gen = _gen, \
39 .ec_ga = _ec_ga, \
40 .mha_pow = _mha_pow, \
41 .hmfai = _hmfai, \
42 .desc = _desc, \
43 .base_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _BASE }, \
44 .default_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _DEFAULT }, \
45 .full_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _FULL }, \
46 }
47
48/*
c657e84f
CB
49 * CPU definition list in order of release. Up to generation 14 base features
50 * of a following release have been a superset of the previous release. With
51 * generation 15 one base feature and one optional feature have been deprecated.
6c064de1
DH
52 */
53static S390CPUDef s390_cpu_defs[] = {
54 CPUDEF_INIT(0x2064, 7, 1, 38, 0x00000000U, "z900", "IBM zSeries 900 GA1"),
55 CPUDEF_INIT(0x2064, 7, 2, 38, 0x00000000U, "z900.2", "IBM zSeries 900 GA2"),
56 CPUDEF_INIT(0x2064, 7, 3, 38, 0x00000000U, "z900.3", "IBM zSeries 900 GA3"),
57 CPUDEF_INIT(0x2066, 7, 3, 38, 0x00000000U, "z800", "IBM zSeries 800 GA1"),
58 CPUDEF_INIT(0x2084, 8, 1, 38, 0x00000000U, "z990", "IBM zSeries 990 GA1"),
59 CPUDEF_INIT(0x2084, 8, 2, 38, 0x00000000U, "z990.2", "IBM zSeries 990 GA2"),
60 CPUDEF_INIT(0x2084, 8, 3, 38, 0x00000000U, "z990.3", "IBM zSeries 990 GA3"),
61 CPUDEF_INIT(0x2086, 8, 3, 38, 0x00000000U, "z890", "IBM zSeries 880 GA1"),
62 CPUDEF_INIT(0x2084, 8, 4, 38, 0x00000000U, "z990.4", "IBM zSeries 990 GA4"),
63 CPUDEF_INIT(0x2086, 8, 4, 38, 0x00000000U, "z890.2", "IBM zSeries 880 GA2"),
64 CPUDEF_INIT(0x2084, 8, 5, 38, 0x00000000U, "z990.5", "IBM zSeries 990 GA5"),
65 CPUDEF_INIT(0x2086, 8, 5, 38, 0x00000000U, "z890.3", "IBM zSeries 880 GA3"),
66 CPUDEF_INIT(0x2094, 9, 1, 40, 0x00000000U, "z9EC", "IBM System z9 EC GA1"),
67 CPUDEF_INIT(0x2094, 9, 2, 40, 0x00000000U, "z9EC.2", "IBM System z9 EC GA2"),
68 CPUDEF_INIT(0x2096, 9, 2, 40, 0x00000000U, "z9BC", "IBM System z9 BC GA1"),
69 CPUDEF_INIT(0x2094, 9, 3, 40, 0x00000000U, "z9EC.3", "IBM System z9 EC GA3"),
70 CPUDEF_INIT(0x2096, 9, 3, 40, 0x00000000U, "z9BC.2", "IBM System z9 BC GA2"),
71 CPUDEF_INIT(0x2097, 10, 1, 43, 0x00000000U, "z10EC", "IBM System z10 EC GA1"),
72 CPUDEF_INIT(0x2097, 10, 2, 43, 0x00000000U, "z10EC.2", "IBM System z10 EC GA2"),
73 CPUDEF_INIT(0x2098, 10, 2, 43, 0x00000000U, "z10BC", "IBM System z10 BC GA1"),
74 CPUDEF_INIT(0x2097, 10, 3, 43, 0x00000000U, "z10EC.3", "IBM System z10 EC GA3"),
75 CPUDEF_INIT(0x2098, 10, 3, 43, 0x00000000U, "z10BC.2", "IBM System z10 BC GA2"),
76 CPUDEF_INIT(0x2817, 11, 1, 44, 0x08000000U, "z196", "IBM zEnterprise 196 GA1"),
77 CPUDEF_INIT(0x2817, 11, 2, 44, 0x08000000U, "z196.2", "IBM zEnterprise 196 GA2"),
78 CPUDEF_INIT(0x2818, 11, 2, 44, 0x08000000U, "z114", "IBM zEnterprise 114 GA1"),
79 CPUDEF_INIT(0x2827, 12, 1, 44, 0x08000000U, "zEC12", "IBM zEnterprise EC12 GA1"),
80 CPUDEF_INIT(0x2827, 12, 2, 44, 0x08000000U, "zEC12.2", "IBM zEnterprise EC12 GA2"),
81 CPUDEF_INIT(0x2828, 12, 2, 44, 0x08000000U, "zBC12", "IBM zEnterprise BC12 GA1"),
82 CPUDEF_INIT(0x2964, 13, 1, 47, 0x08000000U, "z13", "IBM z13 GA1"),
83 CPUDEF_INIT(0x2964, 13, 2, 47, 0x08000000U, "z13.2", "IBM z13 GA2"),
84 CPUDEF_INIT(0x2965, 13, 2, 47, 0x08000000U, "z13s", "IBM z13s GA1"),
cc28a594 85 CPUDEF_INIT(0x3906, 14, 1, 47, 0x08000000U, "z14", "IBM z14 GA1"),
f2a7d157 86 CPUDEF_INIT(0x3906, 14, 2, 47, 0x08000000U, "z14.2", "IBM z14 GA2"),
23ad956b 87 CPUDEF_INIT(0x3907, 14, 1, 47, 0x08000000U, "z14ZR1", "IBM z14 Model ZR1 GA1"),
7505deca 88 CPUDEF_INIT(0x8561, 15, 1, 47, 0x08000000U, "gen15a", "IBM z15 GA1"),
c657e84f 89 CPUDEF_INIT(0x8562, 15, 1, 47, 0x08000000U, "gen15b", "IBM 8562 GA1"),
6c064de1
DH
90};
91
08ef92d5
DH
92#define QEMU_MAX_CPU_TYPE 0x2964
93#define QEMU_MAX_CPU_GEN 13
35b4df64
DH
94#define QEMU_MAX_CPU_EC_GA 2
95static const S390FeatInit qemu_max_cpu_feat_init = { S390_FEAT_LIST_QEMU_MAX };
96static S390FeatBitmap qemu_max_cpu_feat;
97
30e82de7
DH
98/* features part of a base model but not relevant for finding a base model */
99S390FeatBitmap ignored_base_feat;
100
c9ad8a7a
JH
101void s390_cpudef_featoff(uint8_t gen, uint8_t ec_ga, S390Feat feat)
102{
103 const S390CPUDef *def;
104
105 def = s390_find_cpu_def(0, gen, ec_ga, NULL);
106 clear_bit(feat, (unsigned long *)&def->default_feat);
107}
108
109void s390_cpudef_featoff_greater(uint8_t gen, uint8_t ec_ga, S390Feat feat)
110{
111 int i;
112
113 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
114 const S390CPUDef *def = &s390_cpu_defs[i];
115
116 if (def->gen < gen) {
117 continue;
118 }
119 if (def->gen == gen && def->ec_ga < ec_ga) {
120 continue;
121 }
122
123 clear_bit(feat, (unsigned long *)&def->default_feat);
124 }
125}
126
84176c79
CW
127void s390_cpudef_group_featoff_greater(uint8_t gen, uint8_t ec_ga,
128 S390FeatGroup group)
129{
130 const S390FeatGroupDef *group_def = s390_feat_group_def(group);
131 S390FeatBitmap group_def_off;
132 int i;
133
134 bitmap_complement(group_def_off, group_def->feat, S390_FEAT_MAX);
135
136 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
137 const S390CPUDef *cpu_def = &s390_cpu_defs[i];
138
139 if (cpu_def->gen < gen) {
140 continue;
141 }
142 if (cpu_def->gen == gen && cpu_def->ec_ga < ec_ga) {
143 continue;
144 }
145
146 bitmap_and((unsigned long *)&cpu_def->default_feat,
147 cpu_def->default_feat, group_def_off, S390_FEAT_MAX);
148 }
149}
150
a3669307
DH
151uint32_t s390_get_hmfai(void)
152{
153 static S390CPU *cpu;
154
155 if (!cpu) {
156 cpu = S390_CPU(qemu_get_cpu(0));
157 }
158
159 if (!cpu || !cpu->model) {
160 return 0;
161 }
162 return cpu->model->def->hmfai;
163}
164
3fad3252
DH
165uint8_t s390_get_mha_pow(void)
166{
167 static S390CPU *cpu;
168
169 if (!cpu) {
170 cpu = S390_CPU(qemu_get_cpu(0));
171 }
172
173 if (!cpu || !cpu->model) {
174 return 0;
175 }
176 return cpu->model->def->mha_pow;
177}
178
059be520
DH
179uint32_t s390_get_ibc_val(void)
180{
181 uint16_t unblocked_ibc, lowest_ibc;
182 static S390CPU *cpu;
183
184 if (!cpu) {
185 cpu = S390_CPU(qemu_get_cpu(0));
186 }
187
188 if (!cpu || !cpu->model) {
189 return 0;
190 }
191 unblocked_ibc = s390_ibc_from_cpu_model(cpu->model);
192 lowest_ibc = cpu->model->lowest_ibc;
193 /* the lowest_ibc always has to be <= unblocked_ibc */
194 if (!lowest_ibc || lowest_ibc > unblocked_ibc) {
195 return 0;
196 }
197 return ((uint32_t) lowest_ibc << 16) | unblocked_ibc;
198}
199
4dd4200e
DH
200void s390_get_feat_block(S390FeatType type, uint8_t *data)
201{
202 static S390CPU *cpu;
203
204 if (!cpu) {
205 cpu = S390_CPU(qemu_get_cpu(0));
206 }
207
208 if (!cpu || !cpu->model) {
209 return;
210 }
211 s390_fill_feat_block(cpu->model->features, type, data);
212}
213
7c72ac49
DH
214bool s390_has_feat(S390Feat feat)
215{
216 static S390CPU *cpu;
217
218 if (!cpu) {
219 cpu = S390_CPU(qemu_get_cpu(0));
220 }
221
222 if (!cpu || !cpu->model) {
223#ifdef CONFIG_KVM
224 if (kvm_enabled()) {
225 if (feat == S390_FEAT_VECTOR) {
226 return kvm_check_extension(kvm_state,
227 KVM_CAP_S390_VECTOR_REGISTERS);
228 }
229 if (feat == S390_FEAT_RUNTIME_INSTRUMENTATION) {
230 return kvm_s390_get_ri();
231 }
c85d21c7
DH
232 if (feat == S390_FEAT_MSA_EXT_3) {
233 return true;
234 }
7c72ac49
DH
235 }
236#endif
8ad9087c
CH
237 if (feat == S390_FEAT_ZPCI) {
238 return true;
239 }
7c72ac49
DH
240 return 0;
241 }
3ded270a
JF
242 if (feat == S390_FEAT_DIAG_318 && s390_is_pv()) {
243 return false;
244 }
7c72ac49
DH
245 return test_bit(feat, cpu->model->features);
246}
247
3b84c25c
DH
248uint8_t s390_get_gen_for_cpu_type(uint16_t type)
249{
250 int i;
251
252 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
253 if (s390_cpu_defs[i].type == type) {
254 return s390_cpu_defs[i].gen;
255 }
256 }
257 return 0;
258}
259
260const S390CPUDef *s390_find_cpu_def(uint16_t type, uint8_t gen, uint8_t ec_ga,
261 S390FeatBitmap features)
262{
263 const S390CPUDef *last_compatible = NULL;
fbe8202e 264 const S390CPUDef *matching_cpu_type = NULL;
3b84c25c
DH
265 int i;
266
267 if (!gen) {
268 ec_ga = 0;
269 }
270 if (!gen && type) {
271 gen = s390_get_gen_for_cpu_type(type);
272 }
273
274 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
275 const S390CPUDef *def = &s390_cpu_defs[i];
276 S390FeatBitmap missing;
277
278 /* don't even try newer generations if we know the generation */
279 if (gen) {
280 if (def->gen > gen) {
281 break;
282 } else if (def->gen == gen && ec_ga && def->ec_ga > ec_ga) {
283 break;
284 }
285 }
286
287 if (features) {
288 /* see if the model satisfies the minimum features */
289 bitmap_andnot(missing, def->base_feat, features, S390_FEAT_MAX);
30e82de7
DH
290 /*
291 * Ignore certain features that are in the base model, but not
292 * relevant for the search (esp. MSA subfunctions).
293 */
294 bitmap_andnot(missing, missing, ignored_base_feat, S390_FEAT_MAX);
3b84c25c
DH
295 if (!bitmap_empty(missing, S390_FEAT_MAX)) {
296 break;
297 }
298 }
299
300 /* stop the search if we found the exact model */
301 if (def->type == type && def->ec_ga == ec_ga) {
302 return def;
303 }
fbe8202e
DH
304 /* remember if we've at least seen one with the same cpu type */
305 if (def->type == type) {
306 matching_cpu_type = def;
307 }
3b84c25c
DH
308 last_compatible = def;
309 }
fbe8202e
DH
310 /* prefer the model with the same cpu type, esp. don't take the BC for EC */
311 if (matching_cpu_type) {
312 return matching_cpu_type;
313 }
3b84c25c
DH
314 return last_compatible;
315}
316
99aa6bf2 317static void s390_print_cpu_model_list_entry(gpointer data, gpointer user_data)
41868f84 318{
99aa6bf2
DH
319 const S390CPUClass *scc = S390_CPU_CLASS((ObjectClass *)data);
320 char *name = g_strdup(object_class_get_name((ObjectClass *)data));
41868f84
DH
321 const char *details = "";
322
323 if (scc->is_static) {
324 details = "(static, migration-safe)";
325 } else if (scc->is_migration_safe) {
326 details = "(migration-safe)";
327 }
328
e555cbe7 329 /* strip off the -s390x-cpu */
41868f84 330 g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
0442428a 331 qemu_printf("s390 %-15s %-35s %s\n", name, scc->desc, details);
41868f84
DH
332 g_free(name);
333}
334
99aa6bf2
DH
335static gint s390_cpu_list_compare(gconstpointer a, gconstpointer b)
336{
337 const S390CPUClass *cc_a = S390_CPU_CLASS((ObjectClass *)a);
338 const S390CPUClass *cc_b = S390_CPU_CLASS((ObjectClass *)b);
339 const char *name_a = object_class_get_name((ObjectClass *)a);
340 const char *name_b = object_class_get_name((ObjectClass *)b);
341
c6117788
DH
342 /*
343 * Move qemu, host and max to the top of the list, qemu first, host second,
344 * max third.
345 */
99aa6bf2
DH
346 if (name_a[0] == 'q') {
347 return -1;
348 } else if (name_b[0] == 'q') {
349 return 1;
350 } else if (name_a[0] == 'h') {
351 return -1;
352 } else if (name_b[0] == 'h') {
353 return 1;
c6117788
DH
354 } else if (name_a[0] == 'm') {
355 return -1;
356 } else if (name_b[0] == 'm') {
357 return 1;
99aa6bf2
DH
358 }
359
360 /* keep the same order we have in our table (sorted by release date) */
361 if (cc_a->cpu_def != cc_b->cpu_def) {
362 return cc_a->cpu_def - cc_b->cpu_def;
363 }
364
365 /* exact same definition - list base model first */
366 return cc_a->is_static ? -1 : 1;
367}
368
0442428a 369void s390_cpu_list(void)
41868f84 370{
0754f604
DH
371 S390FeatGroup group;
372 S390Feat feat;
99aa6bf2 373 GSList *list;
41868f84 374
99aa6bf2
DH
375 list = object_class_get_list(TYPE_S390_CPU, false);
376 list = g_slist_sort(list, s390_cpu_list_compare);
0442428a 377 g_slist_foreach(list, s390_print_cpu_model_list_entry, NULL);
99aa6bf2 378 g_slist_free(list);
0754f604 379
0442428a 380 qemu_printf("\nRecognized feature flags:\n");
0754f604
DH
381 for (feat = 0; feat < S390_FEAT_MAX; feat++) {
382 const S390FeatDef *def = s390_feat_def(feat);
383
0442428a 384 qemu_printf("%-20s %-50s\n", def->name, def->desc);
0754f604
DH
385 }
386
0442428a 387 qemu_printf("\nRecognized feature groups:\n");
0754f604
DH
388 for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
389 const S390FeatGroupDef *def = s390_feat_group_def(group);
390
0442428a 391 qemu_printf("%-20s %-50s\n", def->name, def->desc);
0754f604 392 }
41868f84
DH
393}
394
38cba1f4
VM
395static S390CPUModel *get_max_cpu_model(Error **errp);
396
41868f84 397#ifndef CONFIG_USER_ONLY
38cba1f4
VM
398static void list_add_feat(const char *name, void *opaque);
399
400static void check_unavailable_features(const S390CPUModel *max_model,
401 const S390CPUModel *model,
402 strList **unavailable)
403{
404 S390FeatBitmap missing;
405
406 /* check general model compatibility */
407 if (max_model->def->gen < model->def->gen ||
408 (max_model->def->gen == model->def->gen &&
409 max_model->def->ec_ga < model->def->ec_ga)) {
410 list_add_feat("type", unavailable);
411 }
412
413 /* detect missing features if any to properly report them */
414 bitmap_andnot(missing, model->features, max_model->features,
415 S390_FEAT_MAX);
416 if (!bitmap_empty(missing, S390_FEAT_MAX)) {
417 s390_feat_bitmap_to_ascii(missing, unavailable, list_add_feat);
418 }
419}
420
421struct CpuDefinitionInfoListData {
422 CpuDefinitionInfoList *list;
423 S390CPUModel *model;
424};
425
41868f84
DH
426static void create_cpu_model_list(ObjectClass *klass, void *opaque)
427{
38cba1f4
VM
428 struct CpuDefinitionInfoListData *cpu_list_data = opaque;
429 CpuDefinitionInfoList **cpu_list = &cpu_list_data->list;
41868f84
DH
430 CpuDefinitionInfoList *entry;
431 CpuDefinitionInfo *info;
432 char *name = g_strdup(object_class_get_name(klass));
433 S390CPUClass *scc = S390_CPU_CLASS(klass);
434
e555cbe7 435 /* strip off the -s390x-cpu */
41868f84 436 g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
96f64aa8 437 info = g_new0(CpuDefinitionInfo, 1);
41868f84
DH
438 info->name = name;
439 info->has_migration_safe = true;
440 info->migration_safe = scc->is_migration_safe;
441 info->q_static = scc->is_static;
8ed877b7 442 info->q_typename = g_strdup(object_class_get_name(klass));
38cba1f4
VM
443 /* check for unavailable features */
444 if (cpu_list_data->model) {
445 Object *obj;
446 S390CPU *sc;
3c75e12e 447 obj = object_new_with_class(klass);
38cba1f4
VM
448 sc = S390_CPU(obj);
449 if (sc->model) {
450 info->has_unavailable_features = true;
451 check_unavailable_features(cpu_list_data->model, sc->model,
452 &info->unavailable_features);
453 }
454 object_unref(obj);
455 }
41868f84 456
96f64aa8 457 entry = g_new0(CpuDefinitionInfoList, 1);
41868f84
DH
458 entry->value = info;
459 entry->next = *cpu_list;
460 *cpu_list = entry;
461}
462
25a9d6ca 463CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
41868f84 464{
38cba1f4
VM
465 struct CpuDefinitionInfoListData list_data = {
466 .list = NULL,
467 };
468
7dcb1bae 469 list_data.model = get_max_cpu_model(NULL);
41868f84 470
38cba1f4
VM
471 object_class_foreach(create_cpu_model_list, TYPE_S390_CPU, false,
472 &list_data);
41868f84 473
38cba1f4 474 return list_data.list;
41868f84 475}
137974ce
DH
476
477static void cpu_model_from_info(S390CPUModel *model, const CpuModelInfo *info,
478 Error **errp)
479{
e47970f5 480 Error *err = NULL;
137974ce
DH
481 const QDict *qdict = NULL;
482 const QDictEntry *e;
483 Visitor *visitor;
484 ObjectClass *oc;
485 S390CPU *cpu;
486 Object *obj;
487
488 if (info->props) {
7dc847eb 489 qdict = qobject_to(QDict, info->props);
137974ce
DH
490 if (!qdict) {
491 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
492 return;
493 }
494 }
495
496 oc = cpu_class_by_name(TYPE_S390_CPU, info->name);
497 if (!oc) {
498 error_setg(errp, "The CPU definition \'%s\' is unknown.", info->name);
499 return;
500 }
501 if (S390_CPU_CLASS(oc)->kvm_required && !kvm_enabled()) {
502 error_setg(errp, "The CPU definition '%s' requires KVM", info->name);
503 return;
504 }
3c75e12e 505 obj = object_new_with_class(oc);
137974ce
DH
506 cpu = S390_CPU(obj);
507
508 if (!cpu->model) {
509 error_setg(errp, "Details about the host CPU model are not available, "
510 "it cannot be used.");
511 object_unref(obj);
512 return;
513 }
514
515 if (qdict) {
048abb7b 516 visitor = qobject_input_visitor_new(info->props);
668f62ec 517 if (!visit_start_struct(visitor, NULL, NULL, 0, errp)) {
be39110d 518 visit_free(visitor);
137974ce
DH
519 object_unref(obj);
520 return;
521 }
522 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
778a2dc5 523 if (!object_property_set(obj, e->key, visitor, &err)) {
137974ce
DH
524 break;
525 }
526 }
e47970f5 527 if (!err) {
5e959d2e 528 visit_check_struct(visitor, &err);
137974ce
DH
529 }
530 visit_end_struct(visitor, NULL);
531 visit_free(visitor);
e47970f5
MA
532 if (err) {
533 error_propagate(errp, err);
137974ce
DH
534 object_unref(obj);
535 return;
536 }
537 }
538
539 /* copy the model and throw the cpu away */
540 memcpy(model, cpu->model, sizeof(*model));
541 object_unref(obj);
542}
543
544static void qdict_add_disabled_feat(const char *name, void *opaque)
545{
46f5ac20 546 qdict_put_bool(opaque, name, false);
137974ce
DH
547}
548
549static void qdict_add_enabled_feat(const char *name, void *opaque)
550{
46f5ac20 551 qdict_put_bool(opaque, name, true);
137974ce
DH
552}
553
554/* convert S390CPUDef into a static CpuModelInfo */
555static void cpu_info_from_model(CpuModelInfo *info, const S390CPUModel *model,
556 bool delta_changes)
557{
558 QDict *qdict = qdict_new();
559 S390FeatBitmap bitmap;
560
561 /* always fallback to the static base model */
562 info->name = g_strdup_printf("%s-base", model->def->name);
563
564 if (delta_changes) {
565 /* features deleted from the base feature set */
566 bitmap_andnot(bitmap, model->def->base_feat, model->features,
567 S390_FEAT_MAX);
568 if (!bitmap_empty(bitmap, S390_FEAT_MAX)) {
569 s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_disabled_feat);
570 }
571
572 /* features added to the base feature set */
573 bitmap_andnot(bitmap, model->features, model->def->base_feat,
574 S390_FEAT_MAX);
575 if (!bitmap_empty(bitmap, S390_FEAT_MAX)) {
576 s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_enabled_feat);
577 }
578 } else {
579 /* expand all features */
580 s390_feat_bitmap_to_ascii(model->features, qdict,
581 qdict_add_enabled_feat);
582 bitmap_complement(bitmap, model->features, S390_FEAT_MAX);
583 s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_disabled_feat);
584 }
585
586 if (!qdict_size(qdict)) {
cb3e7f08 587 qobject_unref(qdict);
137974ce
DH
588 } else {
589 info->props = QOBJECT(qdict);
590 info->has_props = true;
591 }
592}
593
96f75b59 594CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
137974ce
DH
595 CpuModelInfo *model,
596 Error **errp)
597{
e47970f5 598 Error *err = NULL;
137974ce
DH
599 CpuModelExpansionInfo *expansion_info = NULL;
600 S390CPUModel s390_model;
601 bool delta_changes = false;
602
603 /* convert it to our internal representation */
e47970f5
MA
604 cpu_model_from_info(&s390_model, model, &err);
605 if (err) {
606 error_propagate(errp, err);
137974ce
DH
607 return NULL;
608 }
609
610 if (type == CPU_MODEL_EXPANSION_TYPE_STATIC) {
611 delta_changes = true;
612 } else if (type != CPU_MODEL_EXPANSION_TYPE_FULL) {
613 error_setg(errp, "The requested expansion type is not supported.");
614 return NULL;
615 }
616
617 /* convert it back to a static representation */
96f64aa8 618 expansion_info = g_new0(CpuModelExpansionInfo, 1);
137974ce
DH
619 expansion_info->model = g_malloc0(sizeof(*expansion_info->model));
620 cpu_info_from_model(expansion_info->model, &s390_model, delta_changes);
621 return expansion_info;
622}
4e82ef05
DH
623
624static void list_add_feat(const char *name, void *opaque)
625{
626 strList **last = (strList **) opaque;
627 strList *entry;
628
96f64aa8 629 entry = g_new0(strList, 1);
4e82ef05
DH
630 entry->value = g_strdup(name);
631 entry->next = *last;
632 *last = entry;
633}
634
0e2f4530 635CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *infoa,
4e82ef05
DH
636 CpuModelInfo *infob,
637 Error **errp)
638{
e47970f5 639 Error *err = NULL;
4e82ef05
DH
640 CpuModelCompareResult feat_result, gen_result;
641 CpuModelCompareInfo *compare_info;
642 S390FeatBitmap missing, added;
643 S390CPUModel modela, modelb;
644
645 /* convert both models to our internal representation */
e47970f5
MA
646 cpu_model_from_info(&modela, infoa, &err);
647 if (err) {
648 error_propagate(errp, err);
4e82ef05
DH
649 return NULL;
650 }
e47970f5
MA
651 cpu_model_from_info(&modelb, infob, &err);
652 if (err) {
653 error_propagate(errp, err);
4e82ef05
DH
654 return NULL;
655 }
96f64aa8 656 compare_info = g_new0(CpuModelCompareInfo, 1);
4e82ef05
DH
657
658 /* check the cpu generation and ga level */
659 if (modela.def->gen == modelb.def->gen) {
660 if (modela.def->ec_ga == modelb.def->ec_ga) {
661 /* ec and corresponding bc are identical */
662 gen_result = CPU_MODEL_COMPARE_RESULT_IDENTICAL;
663 } else if (modela.def->ec_ga < modelb.def->ec_ga) {
664 gen_result = CPU_MODEL_COMPARE_RESULT_SUBSET;
665 } else {
666 gen_result = CPU_MODEL_COMPARE_RESULT_SUPERSET;
667 }
668 } else if (modela.def->gen < modelb.def->gen) {
669 gen_result = CPU_MODEL_COMPARE_RESULT_SUBSET;
670 } else {
671 gen_result = CPU_MODEL_COMPARE_RESULT_SUPERSET;
672 }
673 if (gen_result != CPU_MODEL_COMPARE_RESULT_IDENTICAL) {
674 /* both models cannot be made identical */
675 list_add_feat("type", &compare_info->responsible_properties);
676 }
677
678 /* check the feature set */
679 if (bitmap_equal(modela.features, modelb.features, S390_FEAT_MAX)) {
680 feat_result = CPU_MODEL_COMPARE_RESULT_IDENTICAL;
681 } else {
682 bitmap_andnot(missing, modela.features, modelb.features, S390_FEAT_MAX);
683 s390_feat_bitmap_to_ascii(missing,
684 &compare_info->responsible_properties,
685 list_add_feat);
686 bitmap_andnot(added, modelb.features, modela.features, S390_FEAT_MAX);
687 s390_feat_bitmap_to_ascii(added, &compare_info->responsible_properties,
688 list_add_feat);
689 if (bitmap_empty(missing, S390_FEAT_MAX)) {
690 feat_result = CPU_MODEL_COMPARE_RESULT_SUBSET;
691 } else if (bitmap_empty(added, S390_FEAT_MAX)) {
692 feat_result = CPU_MODEL_COMPARE_RESULT_SUPERSET;
693 } else {
694 feat_result = CPU_MODEL_COMPARE_RESULT_INCOMPATIBLE;
695 }
696 }
697
698 /* combine the results */
699 if (gen_result == feat_result) {
700 compare_info->result = gen_result;
701 } else if (feat_result == CPU_MODEL_COMPARE_RESULT_IDENTICAL) {
702 compare_info->result = gen_result;
703 } else if (gen_result == CPU_MODEL_COMPARE_RESULT_IDENTICAL) {
704 compare_info->result = feat_result;
705 } else {
706 compare_info->result = CPU_MODEL_COMPARE_RESULT_INCOMPATIBLE;
707 }
708 return compare_info;
709}
f1a47d08 710
0e2f4530 711CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *infoa,
f1a47d08
DH
712 CpuModelInfo *infob,
713 Error **errp)
714{
e47970f5 715 Error *err = NULL;
f1a47d08
DH
716 CpuModelBaselineInfo *baseline_info;
717 S390CPUModel modela, modelb, model;
718 uint16_t cpu_type;
719 uint8_t max_gen_ga;
720 uint8_t max_gen;
721
722 /* convert both models to our internal representation */
e47970f5
MA
723 cpu_model_from_info(&modela, infoa, &err);
724 if (err) {
725 error_propagate(errp, err);
f1a47d08
DH
726 return NULL;
727 }
728
e47970f5
MA
729 cpu_model_from_info(&modelb, infob, &err);
730 if (err) {
731 error_propagate(errp, err);
f1a47d08
DH
732 return NULL;
733 }
734
735 /* features both models support */
736 bitmap_and(model.features, modela.features, modelb.features, S390_FEAT_MAX);
737
738 /* detect the maximum model not regarding features */
739 if (modela.def->gen == modelb.def->gen) {
740 if (modela.def->type == modelb.def->type) {
741 cpu_type = modela.def->type;
742 } else {
743 cpu_type = 0;
744 }
745 max_gen = modela.def->gen;
746 max_gen_ga = MIN(modela.def->ec_ga, modelb.def->ec_ga);
747 } else if (modela.def->gen > modelb.def->gen) {
748 cpu_type = modelb.def->type;
749 max_gen = modelb.def->gen;
750 max_gen_ga = modelb.def->ec_ga;
751 } else {
752 cpu_type = modela.def->type;
753 max_gen = modela.def->gen;
754 max_gen_ga = modela.def->ec_ga;
755 }
756
757 model.def = s390_find_cpu_def(cpu_type, max_gen, max_gen_ga,
758 model.features);
677ff32d
DH
759
760 /* models without early base features (esan3) are bad */
761 if (!model.def) {
762 error_setg(errp, "No compatible CPU model could be created as"
763 " important base features are disabled");
764 return NULL;
765 }
766
f1a47d08
DH
767 /* strip off features not part of the max model */
768 bitmap_and(model.features, model.features, model.def->full_feat,
769 S390_FEAT_MAX);
770
96f64aa8 771 baseline_info = g_new0(CpuModelBaselineInfo, 1);
f1a47d08
DH
772 baseline_info->model = g_malloc0(sizeof(*baseline_info->model));
773 cpu_info_from_model(baseline_info->model, &model, true);
774 return baseline_info;
775}
41868f84
DH
776#endif
777
80560137
DH
778static void check_consistency(const S390CPUModel *model)
779{
780 static int dep[][2] = {
781 { S390_FEAT_IPTE_RANGE, S390_FEAT_DAT_ENH },
782 { S390_FEAT_IDTE_SEGMENT, S390_FEAT_DAT_ENH },
783 { S390_FEAT_IDTE_REGION, S390_FEAT_DAT_ENH },
784 { S390_FEAT_IDTE_REGION, S390_FEAT_IDTE_SEGMENT },
785 { S390_FEAT_LOCAL_TLB_CLEARING, S390_FEAT_DAT_ENH},
786 { S390_FEAT_LONG_DISPLACEMENT_FAST, S390_FEAT_LONG_DISPLACEMENT },
787 { S390_FEAT_DFP_FAST, S390_FEAT_DFP },
788 { S390_FEAT_TRANSACTIONAL_EXE, S390_FEAT_STFLE_49 },
789 { S390_FEAT_EDAT_2, S390_FEAT_EDAT},
790 { S390_FEAT_MSA_EXT_5, S390_FEAT_KIMD_SHA_512 },
791 { S390_FEAT_MSA_EXT_5, S390_FEAT_KLMD_SHA_512 },
792 { S390_FEAT_MSA_EXT_4, S390_FEAT_MSA_EXT_3 },
793 { S390_FEAT_SIE_CMMA, S390_FEAT_CMM },
794 { S390_FEAT_SIE_CMMA, S390_FEAT_SIE_GSLS },
795 { S390_FEAT_SIE_PFMFI, S390_FEAT_EDAT },
6da5c593 796 { S390_FEAT_MSA_EXT_8, S390_FEAT_MSA_EXT_3 },
5dacbe23
CB
797 { S390_FEAT_MSA_EXT_9, S390_FEAT_MSA_EXT_3 },
798 { S390_FEAT_MSA_EXT_9, S390_FEAT_MSA_EXT_4 },
6da5c593
JH
799 { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING },
800 { S390_FEAT_VECTOR_PACKED_DECIMAL, S390_FEAT_VECTOR },
801 { S390_FEAT_VECTOR_ENH, S390_FEAT_VECTOR },
802 { S390_FEAT_INSTRUCTION_EXEC_PROT, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 },
803 { S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2, S390_FEAT_ESOP },
804 { S390_FEAT_CMM_NT, S390_FEAT_CMM },
805 { S390_FEAT_GUARDED_STORAGE, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 },
806 { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_STORE_CLOCK_FAST },
807 { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING },
808 { S390_FEAT_SEMAPHORE_ASSIST, S390_FEAT_STFLE_49 },
809 { S390_FEAT_KIMD_SHA3_224, S390_FEAT_MSA },
810 { S390_FEAT_KIMD_SHA3_256, S390_FEAT_MSA },
811 { S390_FEAT_KIMD_SHA3_384, S390_FEAT_MSA },
812 { S390_FEAT_KIMD_SHA3_512, S390_FEAT_MSA },
813 { S390_FEAT_KIMD_SHAKE_128, S390_FEAT_MSA },
814 { S390_FEAT_KIMD_SHAKE_256, S390_FEAT_MSA },
815 { S390_FEAT_KLMD_SHA3_224, S390_FEAT_MSA },
816 { S390_FEAT_KLMD_SHA3_256, S390_FEAT_MSA },
817 { S390_FEAT_KLMD_SHA3_384, S390_FEAT_MSA },
818 { S390_FEAT_KLMD_SHA3_512, S390_FEAT_MSA },
819 { S390_FEAT_KLMD_SHAKE_128, S390_FEAT_MSA },
820 { S390_FEAT_KLMD_SHAKE_256, S390_FEAT_MSA },
821 { S390_FEAT_PRNO_TRNG_QRTCR, S390_FEAT_MSA_EXT_5 },
822 { S390_FEAT_PRNO_TRNG, S390_FEAT_MSA_EXT_5 },
c1b364ff 823 { S390_FEAT_SIE_KSS, S390_FEAT_SIE_F2 },
c5cd17af
TK
824 { S390_FEAT_AP_QUERY_CONFIG_INFO, S390_FEAT_AP },
825 { S390_FEAT_AP_FACILITIES_TEST, S390_FEAT_AP },
ddf5d18a
CW
826 { S390_FEAT_PTFF_QSIE, S390_FEAT_MULTIPLE_EPOCH },
827 { S390_FEAT_PTFF_QTOUE, S390_FEAT_MULTIPLE_EPOCH },
828 { S390_FEAT_PTFF_STOE, S390_FEAT_MULTIPLE_EPOCH },
829 { S390_FEAT_PTFF_STOUE, S390_FEAT_MULTIPLE_EPOCH },
9ef2d19e 830 { S390_FEAT_AP_QUEUE_INTERRUPT_CONTROL, S390_FEAT_AP },
fabdada9 831 { S390_FEAT_DIAG_318, S390_FEAT_EXTENDED_LENGTH_SCCB },
80560137
DH
832 };
833 int i;
834
835 for (i = 0; i < ARRAY_SIZE(dep); i++) {
836 if (test_bit(dep[i][0], model->features) &&
837 !test_bit(dep[i][1], model->features)) {
3dc6f869
AF
838 warn_report("\'%s\' requires \'%s\'.",
839 s390_feat_def(dep[i][0])->name,
840 s390_feat_def(dep[i][1])->name);
80560137
DH
841 }
842 }
843}
844
845static void error_prepend_missing_feat(const char *name, void *opaque)
846{
847 error_prepend((Error **) opaque, "%s ", name);
848}
849
850static void check_compatibility(const S390CPUModel *max_model,
851 const S390CPUModel *model, Error **errp)
852{
853 S390FeatBitmap missing;
854
855 if (model->def->gen > max_model->def->gen) {
856 error_setg(errp, "Selected CPU generation is too new. Maximum "
857 "supported model in the configuration: \'%s\'",
858 max_model->def->name);
859 return;
860 } else if (model->def->gen == max_model->def->gen &&
861 model->def->ec_ga > max_model->def->ec_ga) {
862 error_setg(errp, "Selected CPU GA level is too new. Maximum "
863 "supported model in the configuration: \'%s\'",
864 max_model->def->name);
865 return;
866 }
867
868 /* detect the missing features to properly report them */
869 bitmap_andnot(missing, model->features, max_model->features, S390_FEAT_MAX);
870 if (bitmap_empty(missing, S390_FEAT_MAX)) {
871 return;
872 }
873
874 error_setg(errp, " ");
875 s390_feat_bitmap_to_ascii(missing, errp, error_prepend_missing_feat);
876 error_prepend(errp, "Some features requested in the CPU model are not "
877 "available in the configuration: ");
878}
879
880static S390CPUModel *get_max_cpu_model(Error **errp)
881{
d687ae1a 882 Error *err = NULL;
80560137
DH
883 static S390CPUModel max_model;
884 static bool cached;
885
886 if (cached) {
887 return &max_model;
888 }
889
890 if (kvm_enabled()) {
d687ae1a 891 kvm_s390_get_host_cpu_model(&max_model, &err);
80560137 892 } else {
35b4df64
DH
893 max_model.def = s390_find_cpu_def(QEMU_MAX_CPU_TYPE, QEMU_MAX_CPU_GEN,
894 QEMU_MAX_CPU_EC_GA, NULL);
895 bitmap_copy(max_model.features, qemu_max_cpu_feat, S390_FEAT_MAX);
80560137 896 }
d687ae1a
MA
897 if (err) {
898 error_propagate(errp, err);
899 return NULL;
900 }
901 cached = true;
902 return &max_model;
80560137
DH
903}
904
905static inline void apply_cpu_model(const S390CPUModel *model, Error **errp)
906{
907#ifndef CONFIG_USER_ONLY
d687ae1a 908 Error *err = NULL;
80560137
DH
909 static S390CPUModel applied_model;
910 static bool applied;
911
912 /*
913 * We have the same model for all VCPUs. KVM can only be configured before
914 * any VCPUs are defined in KVM.
915 */
916 if (applied) {
917 if (model && memcmp(&applied_model, model, sizeof(S390CPUModel))) {
918 error_setg(errp, "Mixed CPU models are not supported on s390x.");
919 }
920 return;
921 }
922
923 if (kvm_enabled()) {
d687ae1a
MA
924 kvm_s390_apply_cpu_model(model, &err);
925 if (err) {
926 error_propagate(errp, err);
927 return;
928 }
80560137
DH
929 }
930
d687ae1a
MA
931 applied = true;
932 if (model) {
933 applied_model = *model;
80560137
DH
934 }
935#endif
936}
937
41868f84
DH
938void s390_realize_cpu_model(CPUState *cs, Error **errp)
939{
d687ae1a 940 Error *err = NULL;
41868f84 941 S390CPUClass *xcc = S390_CPU_GET_CLASS(cs);
80560137
DH
942 S390CPU *cpu = S390_CPU(cs);
943 const S390CPUModel *max_model;
41868f84
DH
944
945 if (xcc->kvm_required && !kvm_enabled()) {
946 error_setg(errp, "CPU definition requires KVM");
947 return;
948 }
80560137
DH
949
950 if (!cpu->model) {
951 /* no host model support -> perform compatibility stuff */
952 apply_cpu_model(NULL, errp);
953 return;
954 }
955
956 max_model = get_max_cpu_model(errp);
d687ae1a 957 if (!max_model) {
80560137
DH
958 error_prepend(errp, "CPU models are not available: ");
959 return;
960 }
961
962 /* copy over properties that can vary */
963 cpu->model->lowest_ibc = max_model->lowest_ibc;
964 cpu->model->cpu_id = max_model->cpu_id;
64bc98f4 965 cpu->model->cpu_id_format = max_model->cpu_id_format;
80560137
DH
966 cpu->model->cpu_ver = max_model->cpu_ver;
967
968 check_consistency(cpu->model);
d687ae1a
MA
969 check_compatibility(max_model, cpu->model, &err);
970 if (err) {
971 error_propagate(errp, err);
80560137
DH
972 return;
973 }
974
975 apply_cpu_model(cpu->model, errp);
076d4d39 976
1e70ba24 977#if !defined(CONFIG_USER_ONLY)
076d4d39
DH
978 cpu->env.cpuid = s390_cpuid_from_cpu_model(cpu->model);
979 if (tcg_enabled()) {
980 /* basic mode, write the cpu address into the first 4 bit of the ID */
ca5c1457 981 cpu->env.cpuid = deposit64(cpu->env.cpuid, 54, 4, cpu->env.core_id);
076d4d39 982 }
1e70ba24 983#endif
41868f84
DH
984}
985
0754f604
DH
986static void get_feature(Object *obj, Visitor *v, const char *name,
987 void *opaque, Error **errp)
988{
989 S390Feat feat = (S390Feat) opaque;
990 S390CPU *cpu = S390_CPU(obj);
991 bool value;
992
993 if (!cpu->model) {
994 error_setg(errp, "Details about the host CPU model are not available, "
995 "features cannot be queried.");
996 return;
997 }
998
999 value = test_bit(feat, cpu->model->features);
1000 visit_type_bool(v, name, &value, errp);
1001}
1002
1003static void set_feature(Object *obj, Visitor *v, const char *name,
1004 void *opaque, Error **errp)
1005{
1006 S390Feat feat = (S390Feat) opaque;
1007 DeviceState *dev = DEVICE(obj);
1008 S390CPU *cpu = S390_CPU(obj);
1009 bool value;
1010
1011 if (dev->realized) {
1012 error_setg(errp, "Attempt to set property '%s' on '%s' after "
1013 "it was realized", name, object_get_typename(obj));
1014 return;
1015 } else if (!cpu->model) {
1016 error_setg(errp, "Details about the host CPU model are not available, "
1017 "features cannot be changed.");
1018 return;
1019 }
1020
668f62ec 1021 if (!visit_type_bool(v, name, &value, errp)) {
0754f604
DH
1022 return;
1023 }
1024 if (value) {
1025 if (!test_bit(feat, cpu->model->def->full_feat)) {
1026 error_setg(errp, "Feature '%s' is not available for CPU model '%s',"
1027 " it was introduced with later models.",
1028 name, cpu->model->def->name);
1029 return;
1030 }
1031 set_bit(feat, cpu->model->features);
1032 } else {
1033 clear_bit(feat, cpu->model->features);
1034 }
1035}
1036
1037static void get_feature_group(Object *obj, Visitor *v, const char *name,
1038 void *opaque, Error **errp)
1039{
1040 S390FeatGroup group = (S390FeatGroup) opaque;
1041 const S390FeatGroupDef *def = s390_feat_group_def(group);
1042 S390CPU *cpu = S390_CPU(obj);
1043 S390FeatBitmap tmp;
1044 bool value;
1045
1046 if (!cpu->model) {
1047 error_setg(errp, "Details about the host CPU model are not available, "
1048 "features cannot be queried.");
1049 return;
1050 }
1051
1052 /* a group is enabled if all features are enabled */
1053 bitmap_and(tmp, cpu->model->features, def->feat, S390_FEAT_MAX);
1054 value = bitmap_equal(tmp, def->feat, S390_FEAT_MAX);
1055 visit_type_bool(v, name, &value, errp);
1056}
1057
1058static void set_feature_group(Object *obj, Visitor *v, const char *name,
1059 void *opaque, Error **errp)
1060{
1061 S390FeatGroup group = (S390FeatGroup) opaque;
1062 const S390FeatGroupDef *def = s390_feat_group_def(group);
1063 DeviceState *dev = DEVICE(obj);
1064 S390CPU *cpu = S390_CPU(obj);
1065 bool value;
1066
1067 if (dev->realized) {
1068 error_setg(errp, "Attempt to set property '%s' on '%s' after "
1069 "it was realized", name, object_get_typename(obj));
1070 return;
1071 } else if (!cpu->model) {
1072 error_setg(errp, "Details about the host CPU model are not available, "
1073 "features cannot be changed.");
1074 return;
1075 }
1076
668f62ec 1077 if (!visit_type_bool(v, name, &value, errp)) {
0754f604
DH
1078 return;
1079 }
1080 if (value) {
1081 /* groups are added in one shot, so an intersect is sufficient */
1082 if (!bitmap_intersects(def->feat, cpu->model->def->full_feat,
1083 S390_FEAT_MAX)) {
1084 error_setg(errp, "Group '%s' is not available for CPU model '%s',"
1085 " it was introduced with later models.",
1086 name, cpu->model->def->name);
1087 return;
1088 }
1089 bitmap_or(cpu->model->features, cpu->model->features, def->feat,
1090 S390_FEAT_MAX);
1091 } else {
1092 bitmap_andnot(cpu->model->features, cpu->model->features, def->feat,
1093 S390_FEAT_MAX);
1094 }
1095}
1096
6c064de1
DH
1097static void s390_cpu_model_initfn(Object *obj)
1098{
ad5afd07
DH
1099 S390CPU *cpu = S390_CPU(obj);
1100 S390CPUClass *xcc = S390_CPU_GET_CLASS(cpu);
1101
1102 cpu->model = g_malloc0(sizeof(*cpu->model));
1103 /* copy the model, so we can modify it */
1104 cpu->model->def = xcc->cpu_def;
1105 if (xcc->is_static) {
1106 /* base model - features will never change */
1107 bitmap_copy(cpu->model->features, cpu->model->def->base_feat,
1108 S390_FEAT_MAX);
1109 } else {
1110 /* latest model - features can change */
1111 bitmap_copy(cpu->model->features,
1112 cpu->model->def->default_feat, S390_FEAT_MAX);
1113 }
6c064de1
DH
1114}
1115
35b4df64
DH
1116static S390CPUDef s390_qemu_cpu_def;
1117static S390CPUModel s390_qemu_cpu_model;
1118
1119/* Set the qemu CPU model (on machine initialization). Must not be called
1120 * once CPUs have been created.
1121 */
1122void s390_set_qemu_cpu_model(uint16_t type, uint8_t gen, uint8_t ec_ga,
1123 const S390FeatInit feat_init)
1124{
1125 const S390CPUDef *def = s390_find_cpu_def(type, gen, ec_ga, NULL);
1126
1127 g_assert(def);
068a5ea0 1128 g_assert(QTAILQ_EMPTY_RCU(&cpus));
35b4df64
DH
1129
1130 /* TCG emulates some features that can usually not be enabled with
1131 * the emulated machine generation. Make sure they can be enabled
1132 * when using the QEMU model by adding them to full_feat. We have
1133 * to copy the definition to do that.
1134 */
1135 memcpy(&s390_qemu_cpu_def, def, sizeof(s390_qemu_cpu_def));
1136 bitmap_or(s390_qemu_cpu_def.full_feat, s390_qemu_cpu_def.full_feat,
1137 qemu_max_cpu_feat, S390_FEAT_MAX);
1138
1139 /* build the CPU model */
1140 s390_qemu_cpu_model.def = &s390_qemu_cpu_def;
1141 bitmap_zero(s390_qemu_cpu_model.features, S390_FEAT_MAX);
1142 s390_init_feat_bitmap(feat_init, s390_qemu_cpu_model.features);
1143}
1144
41868f84
DH
1145static void s390_qemu_cpu_model_initfn(Object *obj)
1146{
ad5afd07
DH
1147 S390CPU *cpu = S390_CPU(obj);
1148
1149 cpu->model = g_malloc0(sizeof(*cpu->model));
35b4df64
DH
1150 /* copy the CPU model so we can modify it */
1151 memcpy(cpu->model, &s390_qemu_cpu_model, sizeof(*cpu->model));
41868f84
DH
1152}
1153
c6117788
DH
1154static void s390_max_cpu_model_initfn(Object *obj)
1155{
1156 const S390CPUModel *max_model;
1157 S390CPU *cpu = S390_CPU(obj);
1158 Error *local_err = NULL;
1159
1160 if (kvm_enabled() && !kvm_s390_cpu_models_supported()) {
1161 /* "max" and "host" always work, even without CPU model support */
1162 return;
1163 }
1164
1165 max_model = get_max_cpu_model(&local_err);
1166 if (local_err) {
1167 /* we expect errors only under KVM, when actually querying the kernel */
1168 g_assert(kvm_enabled());
1169 error_report_err(local_err);
1170 /* fallback to unsupported CPU models */
1171 return;
1172 }
1173
1174 cpu->model = g_new(S390CPUModel, 1);
1175 /* copy the CPU model so we can modify it */
1176 memcpy(cpu->model, max_model, sizeof(*cpu->model));
1177}
1178
41868f84
DH
1179static void s390_cpu_model_finalize(Object *obj)
1180{
ad5afd07
DH
1181 S390CPU *cpu = S390_CPU(obj);
1182
1183 g_free(cpu->model);
1184 cpu->model = NULL;
41868f84
DH
1185}
1186
6efadc90
DH
1187static bool get_is_migration_safe(Object *obj, Error **errp)
1188{
1189 return S390_CPU_GET_CLASS(obj)->is_migration_safe;
1190}
1191
1192static bool get_is_static(Object *obj, Error **errp)
1193{
1194 return S390_CPU_GET_CLASS(obj)->is_static;
1195}
1196
1197static char *get_description(Object *obj, Error **errp)
1198{
1199 return g_strdup(S390_CPU_GET_CLASS(obj)->desc);
1200}
1201
1202void s390_cpu_model_class_register_props(ObjectClass *oc)
1203{
ab76d63a
EH
1204 S390FeatGroup group;
1205 S390Feat feat;
1206
6efadc90 1207 object_class_property_add_bool(oc, "migration-safe", get_is_migration_safe,
d2623129 1208 NULL);
6efadc90 1209 object_class_property_add_bool(oc, "static", get_is_static,
d2623129
MA
1210 NULL);
1211 object_class_property_add_str(oc, "description", get_description, NULL);
ab76d63a
EH
1212
1213 for (feat = 0; feat < S390_FEAT_MAX; feat++) {
1214 const S390FeatDef *def = s390_feat_def(feat);
1215 object_class_property_add(oc, def->name, "bool", get_feature,
1216 set_feature, NULL, (void *) feat);
1217 object_class_property_set_description(oc, def->name, def->desc);
1218 }
1219 for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
1220 const S390FeatGroupDef *def = s390_feat_group_def(group);
1221 object_class_property_add(oc, def->name, "bool", get_feature_group,
1222 set_feature_group, NULL, (void *) group);
1223 object_class_property_set_description(oc, def->name, def->desc);
1224 }
6efadc90
DH
1225}
1226
41868f84
DH
1227#ifdef CONFIG_KVM
1228static void s390_host_cpu_model_class_init(ObjectClass *oc, void *data)
1229{
1230 S390CPUClass *xcc = S390_CPU_CLASS(oc);
1231
1232 xcc->kvm_required = true;
1233 xcc->desc = "KVM only: All recognized features";
1234}
1235#endif
1236
6c064de1
DH
1237static void s390_base_cpu_model_class_init(ObjectClass *oc, void *data)
1238{
1239 S390CPUClass *xcc = S390_CPU_CLASS(oc);
1240
1241 /* all base models are migration safe */
1242 xcc->cpu_def = (const S390CPUDef *) data;
1243 xcc->is_migration_safe = true;
1244 xcc->is_static = true;
1245 xcc->desc = xcc->cpu_def->desc;
1246}
1247
1248static void s390_cpu_model_class_init(ObjectClass *oc, void *data)
1249{
1250 S390CPUClass *xcc = S390_CPU_CLASS(oc);
1251
1252 /* model that can change between QEMU versions */
1253 xcc->cpu_def = (const S390CPUDef *) data;
1254 xcc->is_migration_safe = true;
1255 xcc->desc = xcc->cpu_def->desc;
1256}
1257
41868f84
DH
1258static void s390_qemu_cpu_model_class_init(ObjectClass *oc, void *data)
1259{
1260 S390CPUClass *xcc = S390_CPU_CLASS(oc);
1261
1262 xcc->is_migration_safe = true;
1263 xcc->desc = g_strdup_printf("QEMU Virtual CPU version %s",
1264 qemu_hw_version());
1265}
1266
c6117788
DH
1267static void s390_max_cpu_model_class_init(ObjectClass *oc, void *data)
1268{
1269 S390CPUClass *xcc = S390_CPU_CLASS(oc);
1270
1271 /*
1272 * The "max" model is neither static nor migration safe. Under KVM
1273 * it represents the "host" model. Under TCG it represents some kind of
1274 * "qemu" CPU model without compat handling and maybe with some additional
1275 * CPU features that are not yet unlocked in the "qemu" model.
1276 */
1277 xcc->desc =
1278 "Enables all features supported by the accelerator in the current host";
1279}
1280
41868f84
DH
1281/* Generate type name for a cpu model. Caller has to free the string. */
1282static char *s390_cpu_type_name(const char *model_name)
1283{
1284 return g_strdup_printf(S390_CPU_TYPE_NAME("%s"), model_name);
1285}
1286
6c064de1
DH
1287/* Generate type name for a base cpu model. Caller has to free the string. */
1288static char *s390_base_cpu_type_name(const char *model_name)
1289{
1290 return g_strdup_printf(S390_CPU_TYPE_NAME("%s-base"), model_name);
1291}
1292
41868f84
DH
1293ObjectClass *s390_cpu_class_by_name(const char *name)
1294{
1295 char *typename = s390_cpu_type_name(name);
1296 ObjectClass *oc;
1297
1298 oc = object_class_by_name(typename);
1299 g_free(typename);
1300 return oc;
1301}
1302
1303static const TypeInfo qemu_s390_cpu_type_info = {
1304 .name = S390_CPU_TYPE_NAME("qemu"),
1305 .parent = TYPE_S390_CPU,
1306 .instance_init = s390_qemu_cpu_model_initfn,
1307 .instance_finalize = s390_cpu_model_finalize,
1308 .class_init = s390_qemu_cpu_model_class_init,
1309};
1310
c6117788
DH
1311static const TypeInfo max_s390_cpu_type_info = {
1312 .name = S390_CPU_TYPE_NAME("max"),
1313 .parent = TYPE_S390_CPU,
1314 .instance_init = s390_max_cpu_model_initfn,
1315 .instance_finalize = s390_cpu_model_finalize,
1316 .class_init = s390_max_cpu_model_class_init,
1317};
1318
41868f84
DH
1319#ifdef CONFIG_KVM
1320static const TypeInfo host_s390_cpu_type_info = {
1321 .name = S390_CPU_TYPE_NAME("host"),
c6117788 1322 .parent = S390_CPU_TYPE_NAME("max"),
41868f84
DH
1323 .class_init = s390_host_cpu_model_class_init,
1324};
1325#endif
1326
30e82de7
DH
1327static void init_ignored_base_feat(void)
1328{
1329 static const int feats[] = {
1330 /* MSA subfunctions that could not be available on certain machines */
1331 S390_FEAT_KMAC_DEA,
1332 S390_FEAT_KMAC_TDEA_128,
1333 S390_FEAT_KMAC_TDEA_192,
1334 S390_FEAT_KMC_DEA,
1335 S390_FEAT_KMC_TDEA_128,
1336 S390_FEAT_KMC_TDEA_192,
1337 S390_FEAT_KM_DEA,
1338 S390_FEAT_KM_TDEA_128,
1339 S390_FEAT_KM_TDEA_192,
1340 S390_FEAT_KIMD_SHA_1,
1341 S390_FEAT_KLMD_SHA_1,
eaf6f642
CB
1342 /* CSSKE is deprecated on newer generations */
1343 S390_FEAT_CONDITIONAL_SSKE,
30e82de7
DH
1344 };
1345 int i;
1346
1347 for (i = 0; i < ARRAY_SIZE(feats); i++) {
1348 set_bit(feats[i], ignored_base_feat);
1349 }
1350}
1351
41868f84
DH
1352static void register_types(void)
1353{
35b4df64 1354 static const S390FeatInit qemu_latest_init = { S390_FEAT_LIST_QEMU_LATEST };
6c064de1
DH
1355 int i;
1356
30e82de7
DH
1357 init_ignored_base_feat();
1358
6c064de1 1359 /* init all bitmaps from gnerated data initially */
35b4df64 1360 s390_init_feat_bitmap(qemu_max_cpu_feat_init, qemu_max_cpu_feat);
6c064de1
DH
1361 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
1362 s390_init_feat_bitmap(s390_cpu_defs[i].base_init,
1363 s390_cpu_defs[i].base_feat);
1364 s390_init_feat_bitmap(s390_cpu_defs[i].default_init,
1365 s390_cpu_defs[i].default_feat);
1366 s390_init_feat_bitmap(s390_cpu_defs[i].full_init,
1367 s390_cpu_defs[i].full_feat);
1368 }
1369
35b4df64
DH
1370 /* initialize the qemu model with latest definition */
1371 s390_set_qemu_cpu_model(QEMU_MAX_CPU_TYPE, QEMU_MAX_CPU_GEN,
1372 QEMU_MAX_CPU_EC_GA, qemu_latest_init);
1373
6c064de1
DH
1374 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
1375 char *base_name = s390_base_cpu_type_name(s390_cpu_defs[i].name);
1376 TypeInfo ti_base = {
1377 .name = base_name,
1378 .parent = TYPE_S390_CPU,
1379 .instance_init = s390_cpu_model_initfn,
1380 .instance_finalize = s390_cpu_model_finalize,
1381 .class_init = s390_base_cpu_model_class_init,
1382 .class_data = (void *) &s390_cpu_defs[i],
1383 };
1384 char *name = s390_cpu_type_name(s390_cpu_defs[i].name);
1385 TypeInfo ti = {
1386 .name = name,
1387 .parent = TYPE_S390_CPU,
1388 .instance_init = s390_cpu_model_initfn,
1389 .instance_finalize = s390_cpu_model_finalize,
1390 .class_init = s390_cpu_model_class_init,
1391 .class_data = (void *) &s390_cpu_defs[i],
1392 };
1393
1394 type_register_static(&ti_base);
1395 type_register_static(&ti);
1396 g_free(base_name);
1397 g_free(name);
1398 }
1399
41868f84 1400 type_register_static(&qemu_s390_cpu_type_info);
c6117788 1401 type_register_static(&max_s390_cpu_type_info);
41868f84
DH
1402#ifdef CONFIG_KVM
1403 type_register_static(&host_s390_cpu_type_info);
1404#endif
1405}
1406
1407type_init(register_types)
This page took 0.471967 seconds and 4 git commands to generate.