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