4 * Copyright 2016 IBM Corp.
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
13 #include "qemu/osdep.h"
15 #include "gen-features.h"
16 #include "qapi/error.h"
17 #include "qapi/visitor.h"
18 #ifndef CONFIG_USER_ONLY
19 #include "sysemu/arch_init.h"
22 #define CPUDEF_INIT(_type, _gen, _ec_ga, _mha_pow, _hmfai, _name, _desc) \
28 .mha_pow = _mha_pow, \
31 .base_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _BASE }, \
32 .default_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _DEFAULT }, \
33 .full_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _FULL }, \
37 * CPU definiton list in order of release. For now, base features of a
38 * following release are always a subset of base features of the previous
39 * release. Same is correct for the other feature sets.
40 * A BC release always follows the corresponding EC release.
42 static S390CPUDef s390_cpu_defs[] = {
43 CPUDEF_INIT(0x2064, 7, 1, 38, 0x00000000U, "z900", "IBM zSeries 900 GA1"),
44 CPUDEF_INIT(0x2064, 7, 2, 38, 0x00000000U, "z900.2", "IBM zSeries 900 GA2"),
45 CPUDEF_INIT(0x2064, 7, 3, 38, 0x00000000U, "z900.3", "IBM zSeries 900 GA3"),
46 CPUDEF_INIT(0x2066, 7, 3, 38, 0x00000000U, "z800", "IBM zSeries 800 GA1"),
47 CPUDEF_INIT(0x2084, 8, 1, 38, 0x00000000U, "z990", "IBM zSeries 990 GA1"),
48 CPUDEF_INIT(0x2084, 8, 2, 38, 0x00000000U, "z990.2", "IBM zSeries 990 GA2"),
49 CPUDEF_INIT(0x2084, 8, 3, 38, 0x00000000U, "z990.3", "IBM zSeries 990 GA3"),
50 CPUDEF_INIT(0x2086, 8, 3, 38, 0x00000000U, "z890", "IBM zSeries 880 GA1"),
51 CPUDEF_INIT(0x2084, 8, 4, 38, 0x00000000U, "z990.4", "IBM zSeries 990 GA4"),
52 CPUDEF_INIT(0x2086, 8, 4, 38, 0x00000000U, "z890.2", "IBM zSeries 880 GA2"),
53 CPUDEF_INIT(0x2084, 8, 5, 38, 0x00000000U, "z990.5", "IBM zSeries 990 GA5"),
54 CPUDEF_INIT(0x2086, 8, 5, 38, 0x00000000U, "z890.3", "IBM zSeries 880 GA3"),
55 CPUDEF_INIT(0x2094, 9, 1, 40, 0x00000000U, "z9EC", "IBM System z9 EC GA1"),
56 CPUDEF_INIT(0x2094, 9, 2, 40, 0x00000000U, "z9EC.2", "IBM System z9 EC GA2"),
57 CPUDEF_INIT(0x2096, 9, 2, 40, 0x00000000U, "z9BC", "IBM System z9 BC GA1"),
58 CPUDEF_INIT(0x2094, 9, 3, 40, 0x00000000U, "z9EC.3", "IBM System z9 EC GA3"),
59 CPUDEF_INIT(0x2096, 9, 3, 40, 0x00000000U, "z9BC.2", "IBM System z9 BC GA2"),
60 CPUDEF_INIT(0x2097, 10, 1, 43, 0x00000000U, "z10EC", "IBM System z10 EC GA1"),
61 CPUDEF_INIT(0x2097, 10, 2, 43, 0x00000000U, "z10EC.2", "IBM System z10 EC GA2"),
62 CPUDEF_INIT(0x2098, 10, 2, 43, 0x00000000U, "z10BC", "IBM System z10 BC GA1"),
63 CPUDEF_INIT(0x2097, 10, 3, 43, 0x00000000U, "z10EC.3", "IBM System z10 EC GA3"),
64 CPUDEF_INIT(0x2098, 10, 3, 43, 0x00000000U, "z10BC.2", "IBM System z10 BC GA2"),
65 CPUDEF_INIT(0x2817, 11, 1, 44, 0x08000000U, "z196", "IBM zEnterprise 196 GA1"),
66 CPUDEF_INIT(0x2817, 11, 2, 44, 0x08000000U, "z196.2", "IBM zEnterprise 196 GA2"),
67 CPUDEF_INIT(0x2818, 11, 2, 44, 0x08000000U, "z114", "IBM zEnterprise 114 GA1"),
68 CPUDEF_INIT(0x2827, 12, 1, 44, 0x08000000U, "zEC12", "IBM zEnterprise EC12 GA1"),
69 CPUDEF_INIT(0x2827, 12, 2, 44, 0x08000000U, "zEC12.2", "IBM zEnterprise EC12 GA2"),
70 CPUDEF_INIT(0x2828, 12, 2, 44, 0x08000000U, "zBC12", "IBM zEnterprise BC12 GA1"),
71 CPUDEF_INIT(0x2964, 13, 1, 47, 0x08000000U, "z13", "IBM z13 GA1"),
72 CPUDEF_INIT(0x2964, 13, 2, 47, 0x08000000U, "z13.2", "IBM z13 GA2"),
73 CPUDEF_INIT(0x2965, 13, 2, 47, 0x08000000U, "z13s", "IBM z13s GA1"),
76 struct S390PrintCpuListInfo {
78 fprintf_function print;
81 static void print_cpu_model_list(ObjectClass *klass, void *opaque)
83 struct S390PrintCpuListInfo *info = opaque;
84 S390CPUClass *scc = S390_CPU_CLASS(klass);
85 char *name = g_strdup(object_class_get_name(klass));
86 const char *details = "";
89 details = "(static, migration-safe)";
90 } else if (scc->is_migration_safe) {
91 details = "(migration-safe)";
94 /* strip off the -s390-cpu */
95 g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
96 (*info->print)(info->f, "s390 %-15s %-35s %s\n", name, scc->desc,
101 void s390_cpu_list(FILE *f, fprintf_function print)
103 struct S390PrintCpuListInfo info = {
110 object_class_foreach(print_cpu_model_list, TYPE_S390_CPU, false, &info);
112 (*print)(f, "\nRecognized feature flags:\n");
113 for (feat = 0; feat < S390_FEAT_MAX; feat++) {
114 const S390FeatDef *def = s390_feat_def(feat);
116 (*print)(f, "%-20s %-50s\n", def->name, def->desc);
119 (*print)(f, "\nRecognized feature groups:\n");
120 for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
121 const S390FeatGroupDef *def = s390_feat_group_def(group);
123 (*print)(f, "%-20s %-50s\n", def->name, def->desc);
127 #ifndef CONFIG_USER_ONLY
128 static void create_cpu_model_list(ObjectClass *klass, void *opaque)
130 CpuDefinitionInfoList **cpu_list = opaque;
131 CpuDefinitionInfoList *entry;
132 CpuDefinitionInfo *info;
133 char *name = g_strdup(object_class_get_name(klass));
134 S390CPUClass *scc = S390_CPU_CLASS(klass);
136 /* strip off the -s390-cpu */
137 g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
138 info = g_malloc0(sizeof(*info));
140 info->has_migration_safe = true;
141 info->migration_safe = scc->is_migration_safe;
142 info->q_static = scc->is_static;
145 entry = g_malloc0(sizeof(*entry));
147 entry->next = *cpu_list;
151 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
153 CpuDefinitionInfoList *list = NULL;
155 object_class_foreach(create_cpu_model_list, TYPE_S390_CPU, false, &list);
161 void s390_realize_cpu_model(CPUState *cs, Error **errp)
163 S390CPUClass *xcc = S390_CPU_GET_CLASS(cs);
165 if (xcc->kvm_required && !kvm_enabled()) {
166 error_setg(errp, "CPU definition requires KVM");
171 static void get_feature(Object *obj, Visitor *v, const char *name,
172 void *opaque, Error **errp)
174 S390Feat feat = (S390Feat) opaque;
175 S390CPU *cpu = S390_CPU(obj);
179 error_setg(errp, "Details about the host CPU model are not available, "
180 "features cannot be queried.");
184 value = test_bit(feat, cpu->model->features);
185 visit_type_bool(v, name, &value, errp);
188 static void set_feature(Object *obj, Visitor *v, const char *name,
189 void *opaque, Error **errp)
191 S390Feat feat = (S390Feat) opaque;
192 DeviceState *dev = DEVICE(obj);
193 S390CPU *cpu = S390_CPU(obj);
197 error_setg(errp, "Attempt to set property '%s' on '%s' after "
198 "it was realized", name, object_get_typename(obj));
200 } else if (!cpu->model) {
201 error_setg(errp, "Details about the host CPU model are not available, "
202 "features cannot be changed.");
206 visit_type_bool(v, name, &value, errp);
211 if (!test_bit(feat, cpu->model->def->full_feat)) {
212 error_setg(errp, "Feature '%s' is not available for CPU model '%s',"
213 " it was introduced with later models.",
214 name, cpu->model->def->name);
217 set_bit(feat, cpu->model->features);
219 clear_bit(feat, cpu->model->features);
223 static void get_feature_group(Object *obj, Visitor *v, const char *name,
224 void *opaque, Error **errp)
226 S390FeatGroup group = (S390FeatGroup) opaque;
227 const S390FeatGroupDef *def = s390_feat_group_def(group);
228 S390CPU *cpu = S390_CPU(obj);
233 error_setg(errp, "Details about the host CPU model are not available, "
234 "features cannot be queried.");
238 /* a group is enabled if all features are enabled */
239 bitmap_and(tmp, cpu->model->features, def->feat, S390_FEAT_MAX);
240 value = bitmap_equal(tmp, def->feat, S390_FEAT_MAX);
241 visit_type_bool(v, name, &value, errp);
244 static void set_feature_group(Object *obj, Visitor *v, const char *name,
245 void *opaque, Error **errp)
247 S390FeatGroup group = (S390FeatGroup) opaque;
248 const S390FeatGroupDef *def = s390_feat_group_def(group);
249 DeviceState *dev = DEVICE(obj);
250 S390CPU *cpu = S390_CPU(obj);
254 error_setg(errp, "Attempt to set property '%s' on '%s' after "
255 "it was realized", name, object_get_typename(obj));
257 } else if (!cpu->model) {
258 error_setg(errp, "Details about the host CPU model are not available, "
259 "features cannot be changed.");
263 visit_type_bool(v, name, &value, errp);
268 /* groups are added in one shot, so an intersect is sufficient */
269 if (!bitmap_intersects(def->feat, cpu->model->def->full_feat,
271 error_setg(errp, "Group '%s' is not available for CPU model '%s',"
272 " it was introduced with later models.",
273 name, cpu->model->def->name);
276 bitmap_or(cpu->model->features, cpu->model->features, def->feat,
279 bitmap_andnot(cpu->model->features, cpu->model->features, def->feat,
284 void s390_cpu_model_register_props(Object *obj)
289 for (feat = 0; feat < S390_FEAT_MAX; feat++) {
290 const S390FeatDef *def = s390_feat_def(feat);
291 object_property_add(obj, def->name, "bool", get_feature,
292 set_feature, NULL, (void *) feat, NULL);
293 object_property_set_description(obj, def->name, def->desc , NULL);
295 for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
296 const S390FeatGroupDef *def = s390_feat_group_def(group);
297 object_property_add(obj, def->name, "bool", get_feature_group,
298 set_feature_group, NULL, (void *) group, NULL);
299 object_property_set_description(obj, def->name, def->desc , NULL);
303 static void s390_cpu_model_initfn(Object *obj)
305 S390CPU *cpu = S390_CPU(obj);
306 S390CPUClass *xcc = S390_CPU_GET_CLASS(cpu);
308 cpu->model = g_malloc0(sizeof(*cpu->model));
309 /* copy the model, so we can modify it */
310 cpu->model->def = xcc->cpu_def;
311 if (xcc->is_static) {
312 /* base model - features will never change */
313 bitmap_copy(cpu->model->features, cpu->model->def->base_feat,
316 /* latest model - features can change */
317 bitmap_copy(cpu->model->features,
318 cpu->model->def->default_feat, S390_FEAT_MAX);
323 static void s390_host_cpu_model_initfn(Object *obj)
328 static void s390_qemu_cpu_model_initfn(Object *obj)
330 S390CPU *cpu = S390_CPU(obj);
332 cpu->model = g_malloc0(sizeof(*cpu->model));
333 /* TCG emulates a z900 */
334 cpu->model->def = &s390_cpu_defs[0];
335 bitmap_copy(cpu->model->features, cpu->model->def->default_feat,
339 static void s390_cpu_model_finalize(Object *obj)
341 S390CPU *cpu = S390_CPU(obj);
347 static bool get_is_migration_safe(Object *obj, Error **errp)
349 return S390_CPU_GET_CLASS(obj)->is_migration_safe;
352 static bool get_is_static(Object *obj, Error **errp)
354 return S390_CPU_GET_CLASS(obj)->is_static;
357 static char *get_description(Object *obj, Error **errp)
359 return g_strdup(S390_CPU_GET_CLASS(obj)->desc);
362 void s390_cpu_model_class_register_props(ObjectClass *oc)
364 object_class_property_add_bool(oc, "migration-safe", get_is_migration_safe,
366 object_class_property_add_bool(oc, "static", get_is_static,
368 object_class_property_add_str(oc, "description", get_description, NULL,
373 static void s390_host_cpu_model_class_init(ObjectClass *oc, void *data)
375 S390CPUClass *xcc = S390_CPU_CLASS(oc);
377 xcc->kvm_required = true;
378 xcc->desc = "KVM only: All recognized features";
382 static void s390_base_cpu_model_class_init(ObjectClass *oc, void *data)
384 S390CPUClass *xcc = S390_CPU_CLASS(oc);
386 /* all base models are migration safe */
387 xcc->cpu_def = (const S390CPUDef *) data;
388 xcc->is_migration_safe = true;
389 xcc->is_static = true;
390 xcc->desc = xcc->cpu_def->desc;
393 static void s390_cpu_model_class_init(ObjectClass *oc, void *data)
395 S390CPUClass *xcc = S390_CPU_CLASS(oc);
397 /* model that can change between QEMU versions */
398 xcc->cpu_def = (const S390CPUDef *) data;
399 xcc->is_migration_safe = true;
400 xcc->desc = xcc->cpu_def->desc;
403 static void s390_qemu_cpu_model_class_init(ObjectClass *oc, void *data)
405 S390CPUClass *xcc = S390_CPU_CLASS(oc);
407 xcc->is_migration_safe = true;
408 xcc->desc = g_strdup_printf("QEMU Virtual CPU version %s",
412 #define S390_CPU_TYPE_SUFFIX "-" TYPE_S390_CPU
413 #define S390_CPU_TYPE_NAME(name) (name S390_CPU_TYPE_SUFFIX)
415 /* Generate type name for a cpu model. Caller has to free the string. */
416 static char *s390_cpu_type_name(const char *model_name)
418 return g_strdup_printf(S390_CPU_TYPE_NAME("%s"), model_name);
421 /* Generate type name for a base cpu model. Caller has to free the string. */
422 static char *s390_base_cpu_type_name(const char *model_name)
424 return g_strdup_printf(S390_CPU_TYPE_NAME("%s-base"), model_name);
427 ObjectClass *s390_cpu_class_by_name(const char *name)
429 char *typename = s390_cpu_type_name(name);
432 oc = object_class_by_name(typename);
437 static const TypeInfo qemu_s390_cpu_type_info = {
438 .name = S390_CPU_TYPE_NAME("qemu"),
439 .parent = TYPE_S390_CPU,
440 .instance_init = s390_qemu_cpu_model_initfn,
441 .instance_finalize = s390_cpu_model_finalize,
442 .class_init = s390_qemu_cpu_model_class_init,
446 static const TypeInfo host_s390_cpu_type_info = {
447 .name = S390_CPU_TYPE_NAME("host"),
448 .parent = TYPE_S390_CPU,
449 .instance_init = s390_host_cpu_model_initfn,
450 .instance_finalize = s390_cpu_model_finalize,
451 .class_init = s390_host_cpu_model_class_init,
455 static void register_types(void)
459 /* init all bitmaps from gnerated data initially */
460 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
461 s390_init_feat_bitmap(s390_cpu_defs[i].base_init,
462 s390_cpu_defs[i].base_feat);
463 s390_init_feat_bitmap(s390_cpu_defs[i].default_init,
464 s390_cpu_defs[i].default_feat);
465 s390_init_feat_bitmap(s390_cpu_defs[i].full_init,
466 s390_cpu_defs[i].full_feat);
469 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
470 char *base_name = s390_base_cpu_type_name(s390_cpu_defs[i].name);
473 .parent = TYPE_S390_CPU,
474 .instance_init = s390_cpu_model_initfn,
475 .instance_finalize = s390_cpu_model_finalize,
476 .class_init = s390_base_cpu_model_class_init,
477 .class_data = (void *) &s390_cpu_defs[i],
479 char *name = s390_cpu_type_name(s390_cpu_defs[i].name);
482 .parent = TYPE_S390_CPU,
483 .instance_init = s390_cpu_model_initfn,
484 .instance_finalize = s390_cpu_model_finalize,
485 .class_init = s390_cpu_model_class_init,
486 .class_data = (void *) &s390_cpu_defs[i],
489 type_register_static(&ti_base);
490 type_register_static(&ti);
495 type_register_static(&qemu_s390_cpu_type_info);
497 type_register_static(&host_s390_cpu_type_info);
501 type_init(register_types)