xref: /qemu/target/s390x/cpu_models.c (revision d6a7c3f4)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  * CPU models for s390x
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  * Copyright 2016 IBM Corp.
5fcf5ef2aSThomas Huth  *
6fcf5ef2aSThomas Huth  * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
7fcf5ef2aSThomas Huth  *
8fcf5ef2aSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or (at
9fcf5ef2aSThomas Huth  * your option) any later version. See the COPYING file in the top-level
10fcf5ef2aSThomas Huth  * directory.
11fcf5ef2aSThomas Huth  */
12fcf5ef2aSThomas Huth 
13fcf5ef2aSThomas Huth #include "qemu/osdep.h"
14fcf5ef2aSThomas Huth #include "cpu.h"
15b6b47223SCho, Yu-Chen #include "s390x-internal.h"
1667043607SCho, Yu-Chen #include "kvm/kvm_s390x.h"
17f16bbb9bSDavid Hildenbrand #include "sysemu/kvm.h"
1814a48c1dSMarkus Armbruster #include "sysemu/tcg.h"
19fcf5ef2aSThomas Huth #include "qapi/error.h"
20cc37d98bSRichard Henderson #include "qemu/error-report.h"
21fcf5ef2aSThomas Huth #include "qapi/visitor.h"
220b8fa32fSMarkus Armbruster #include "qemu/module.h"
2315e09912SPeter Maydell #include "qemu/hw-version.h"
240442428aSMarkus Armbruster #include "qemu/qemu-print.h"
25fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
260110253eSChristian Borntraeger #include "sysemu/sysemu.h"
27f5f9c6eaSPhilippe Mathieu-Daudé #include "target/s390x/kvm/pv.h"
28*d6a7c3f4SThomas Huth #include CONFIG_DEVICES
293ea7e312SPhilippe Mathieu-Daudé #endif
30fcf5ef2aSThomas Huth 
31fcf5ef2aSThomas Huth #define CPUDEF_INIT(_type, _gen, _ec_ga, _mha_pow, _hmfai, _name, _desc) \
32fcf5ef2aSThomas Huth     {                                                                    \
33fcf5ef2aSThomas Huth         .name = _name,                                                   \
34fcf5ef2aSThomas Huth         .type = _type,                                                   \
35fcf5ef2aSThomas Huth         .gen = _gen,                                                     \
36fcf5ef2aSThomas Huth         .ec_ga = _ec_ga,                                                 \
37fcf5ef2aSThomas Huth         .mha_pow = _mha_pow,                                             \
38fcf5ef2aSThomas Huth         .hmfai = _hmfai,                                                 \
39fcf5ef2aSThomas Huth         .desc = _desc,                                                   \
40fcf5ef2aSThomas Huth         .base_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _BASE },  \
41fcf5ef2aSThomas Huth         .default_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _DEFAULT },  \
42fcf5ef2aSThomas Huth         .full_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _FULL },  \
43fcf5ef2aSThomas Huth     }
44fcf5ef2aSThomas Huth 
45fcf5ef2aSThomas Huth /*
46c657e84fSChristian Borntraeger  * CPU definition list in order of release. Up to generation 14 base features
47c657e84fSChristian Borntraeger  * of a following release have been a superset of the previous release. With
48c657e84fSChristian Borntraeger  * generation 15 one base feature and one optional feature have been deprecated.
49fcf5ef2aSThomas Huth  */
50fcf5ef2aSThomas Huth static S390CPUDef s390_cpu_defs[] = {
51*d6a7c3f4SThomas Huth     /*
52*d6a7c3f4SThomas Huth      * Linux requires at least z10 nowadays, and IBM only supports recent CPUs
53*d6a7c3f4SThomas Huth      * (see https://www.ibm.com/support/pages/ibm-mainframe-life-cycle-history),
54*d6a7c3f4SThomas Huth      * so we consider older CPUs as legacy that can optionally be disabled via
55*d6a7c3f4SThomas Huth      * the CONFIG_S390X_LEGACY_CPUS config switch.
56*d6a7c3f4SThomas Huth      */
57*d6a7c3f4SThomas Huth #if defined(CONFIG_S390X_LEGACY_CPUS) || defined(CONFIG_USER_ONLY)
58fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2064, 7, 1, 38, 0x00000000U, "z900", "IBM zSeries 900 GA1"),
59fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2064, 7, 2, 38, 0x00000000U, "z900.2", "IBM zSeries 900 GA2"),
60fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2064, 7, 3, 38, 0x00000000U, "z900.3", "IBM zSeries 900 GA3"),
61fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2066, 7, 3, 38, 0x00000000U, "z800", "IBM zSeries 800 GA1"),
62fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2084, 8, 1, 38, 0x00000000U, "z990", "IBM zSeries 990 GA1"),
63fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2084, 8, 2, 38, 0x00000000U, "z990.2", "IBM zSeries 990 GA2"),
64fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2084, 8, 3, 38, 0x00000000U, "z990.3", "IBM zSeries 990 GA3"),
65fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2086, 8, 3, 38, 0x00000000U, "z890", "IBM zSeries 880 GA1"),
66fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2084, 8, 4, 38, 0x00000000U, "z990.4", "IBM zSeries 990 GA4"),
67fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2086, 8, 4, 38, 0x00000000U, "z890.2", "IBM zSeries 880 GA2"),
68fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2084, 8, 5, 38, 0x00000000U, "z990.5", "IBM zSeries 990 GA5"),
69fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2086, 8, 5, 38, 0x00000000U, "z890.3", "IBM zSeries 880 GA3"),
70fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2094, 9, 1, 40, 0x00000000U, "z9EC", "IBM System z9 EC GA1"),
71fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2094, 9, 2, 40, 0x00000000U, "z9EC.2", "IBM System z9 EC GA2"),
72fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2096, 9, 2, 40, 0x00000000U, "z9BC", "IBM System z9 BC GA1"),
73fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2094, 9, 3, 40, 0x00000000U, "z9EC.3", "IBM System z9 EC GA3"),
74fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2096, 9, 3, 40, 0x00000000U, "z9BC.2", "IBM System z9 BC GA2"),
75*d6a7c3f4SThomas Huth #endif
76fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2097, 10, 1, 43, 0x00000000U, "z10EC", "IBM System z10 EC GA1"),
77fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2097, 10, 2, 43, 0x00000000U, "z10EC.2", "IBM System z10 EC GA2"),
78fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2098, 10, 2, 43, 0x00000000U, "z10BC", "IBM System z10 BC GA1"),
79fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2097, 10, 3, 43, 0x00000000U, "z10EC.3", "IBM System z10 EC GA3"),
80fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2098, 10, 3, 43, 0x00000000U, "z10BC.2", "IBM System z10 BC GA2"),
81fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2817, 11, 1, 44, 0x08000000U, "z196", "IBM zEnterprise 196 GA1"),
82fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2817, 11, 2, 44, 0x08000000U, "z196.2", "IBM zEnterprise 196 GA2"),
83fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2818, 11, 2, 44, 0x08000000U, "z114", "IBM zEnterprise 114 GA1"),
84fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2827, 12, 1, 44, 0x08000000U, "zEC12", "IBM zEnterprise EC12 GA1"),
85fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2827, 12, 2, 44, 0x08000000U, "zEC12.2", "IBM zEnterprise EC12 GA2"),
86fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2828, 12, 2, 44, 0x08000000U, "zBC12", "IBM zEnterprise BC12 GA1"),
87fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2964, 13, 1, 47, 0x08000000U, "z13", "IBM z13 GA1"),
88fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2964, 13, 2, 47, 0x08000000U, "z13.2", "IBM z13 GA2"),
89fcf5ef2aSThomas Huth     CPUDEF_INIT(0x2965, 13, 2, 47, 0x08000000U, "z13s", "IBM z13s GA1"),
90cc28a594SJason J. Herne     CPUDEF_INIT(0x3906, 14, 1, 47, 0x08000000U, "z14", "IBM z14 GA1"),
91f2a7d157SCollin Walling     CPUDEF_INIT(0x3906, 14, 2, 47, 0x08000000U, "z14.2", "IBM z14 GA2"),
9223ad956bSChristian Borntraeger     CPUDEF_INIT(0x3907, 14, 1, 47, 0x08000000U, "z14ZR1", "IBM z14 Model ZR1 GA1"),
93d66a52b5SCornelia Huck     CPUDEF_INIT(0x8561, 15, 1, 47, 0x08000000U, "gen15a", "IBM z15 T01 GA1"),
94d66a52b5SCornelia Huck     CPUDEF_INIT(0x8562, 15, 1, 47, 0x08000000U, "gen15b", "IBM z15 T02 GA1"),
95fb4a0812SChristian Borntraeger     CPUDEF_INIT(0x3931, 16, 1, 47, 0x08000000U, "gen16a", "IBM 3931 GA1"),
96fb4a0812SChristian Borntraeger     CPUDEF_INIT(0x3932, 16, 1, 47, 0x08000000U, "gen16b", "IBM 3932 GA1"),
97fcf5ef2aSThomas Huth };
98fcf5ef2aSThomas Huth 
998a4eafb6SDavid Miller #define QEMU_MAX_CPU_TYPE 0x8561
1008a4eafb6SDavid Miller #define QEMU_MAX_CPU_GEN 15
1018a4eafb6SDavid Miller #define QEMU_MAX_CPU_EC_GA 1
10235b4df64SDavid Hildenbrand static S390FeatBitmap qemu_max_cpu_feat;
10335b4df64SDavid Hildenbrand 
10430e82de7SDavid Hildenbrand /* features part of a base model but not relevant for finding a base model */
10530e82de7SDavid Hildenbrand S390FeatBitmap ignored_base_feat;
10630e82de7SDavid Hildenbrand 
s390_cpudef_featoff(uint8_t gen,uint8_t ec_ga,S390Feat feat)107c9ad8a7aSJason J. Herne void s390_cpudef_featoff(uint8_t gen, uint8_t ec_ga, S390Feat feat)
108c9ad8a7aSJason J. Herne {
109c9ad8a7aSJason J. Herne     const S390CPUDef *def;
110c9ad8a7aSJason J. Herne 
111c9ad8a7aSJason J. Herne     def = s390_find_cpu_def(0, gen, ec_ga, NULL);
112c9ad8a7aSJason J. Herne     clear_bit(feat, (unsigned long *)&def->default_feat);
113c9ad8a7aSJason J. Herne }
114c9ad8a7aSJason J. Herne 
s390_cpudef_featoff_greater(uint8_t gen,uint8_t ec_ga,S390Feat feat)115c9ad8a7aSJason J. Herne void s390_cpudef_featoff_greater(uint8_t gen, uint8_t ec_ga, S390Feat feat)
116c9ad8a7aSJason J. Herne {
117c9ad8a7aSJason J. Herne     int i;
118c9ad8a7aSJason J. Herne 
119c9ad8a7aSJason J. Herne     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
120c9ad8a7aSJason J. Herne         const S390CPUDef *def = &s390_cpu_defs[i];
121c9ad8a7aSJason J. Herne 
122c9ad8a7aSJason J. Herne         if (def->gen < gen) {
123c9ad8a7aSJason J. Herne             continue;
124c9ad8a7aSJason J. Herne         }
125c9ad8a7aSJason J. Herne         if (def->gen == gen && def->ec_ga < ec_ga) {
126c9ad8a7aSJason J. Herne             continue;
127c9ad8a7aSJason J. Herne         }
128c9ad8a7aSJason J. Herne 
129c9ad8a7aSJason J. Herne         clear_bit(feat, (unsigned long *)&def->default_feat);
130c9ad8a7aSJason J. Herne     }
131c9ad8a7aSJason J. Herne }
132c9ad8a7aSJason J. Herne 
s390_cpudef_group_featoff_greater(uint8_t gen,uint8_t ec_ga,S390FeatGroup group)13384176c79SCollin Walling void s390_cpudef_group_featoff_greater(uint8_t gen, uint8_t ec_ga,
13484176c79SCollin Walling                                        S390FeatGroup group)
13584176c79SCollin Walling {
13684176c79SCollin Walling     const S390FeatGroupDef *group_def = s390_feat_group_def(group);
13784176c79SCollin Walling     S390FeatBitmap group_def_off;
13884176c79SCollin Walling     int i;
13984176c79SCollin Walling 
14084176c79SCollin Walling     bitmap_complement(group_def_off, group_def->feat, S390_FEAT_MAX);
14184176c79SCollin Walling 
14284176c79SCollin Walling     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
14384176c79SCollin Walling         const S390CPUDef *cpu_def = &s390_cpu_defs[i];
14484176c79SCollin Walling 
14584176c79SCollin Walling         if (cpu_def->gen < gen) {
14684176c79SCollin Walling             continue;
14784176c79SCollin Walling         }
14884176c79SCollin Walling         if (cpu_def->gen == gen && cpu_def->ec_ga < ec_ga) {
14984176c79SCollin Walling             continue;
15084176c79SCollin Walling         }
15184176c79SCollin Walling 
15284176c79SCollin Walling         bitmap_and((unsigned long *)&cpu_def->default_feat,
15384176c79SCollin Walling                    cpu_def->default_feat, group_def_off, S390_FEAT_MAX);
15484176c79SCollin Walling     }
15584176c79SCollin Walling }
15684176c79SCollin Walling 
s390_get_hmfai(void)157fcf5ef2aSThomas Huth uint32_t s390_get_hmfai(void)
158fcf5ef2aSThomas Huth {
159fcf5ef2aSThomas Huth     static S390CPU *cpu;
160fcf5ef2aSThomas Huth 
161fcf5ef2aSThomas Huth     if (!cpu) {
162fcf5ef2aSThomas Huth         cpu = S390_CPU(qemu_get_cpu(0));
163fcf5ef2aSThomas Huth     }
164fcf5ef2aSThomas Huth 
165fcf5ef2aSThomas Huth     if (!cpu || !cpu->model) {
166fcf5ef2aSThomas Huth         return 0;
167fcf5ef2aSThomas Huth     }
168fcf5ef2aSThomas Huth     return cpu->model->def->hmfai;
169fcf5ef2aSThomas Huth }
170fcf5ef2aSThomas Huth 
s390_get_mha_pow(void)171fcf5ef2aSThomas Huth uint8_t s390_get_mha_pow(void)
172fcf5ef2aSThomas Huth {
173fcf5ef2aSThomas Huth     static S390CPU *cpu;
174fcf5ef2aSThomas Huth 
175fcf5ef2aSThomas Huth     if (!cpu) {
176fcf5ef2aSThomas Huth         cpu = S390_CPU(qemu_get_cpu(0));
177fcf5ef2aSThomas Huth     }
178fcf5ef2aSThomas Huth 
179fcf5ef2aSThomas Huth     if (!cpu || !cpu->model) {
180fcf5ef2aSThomas Huth         return 0;
181fcf5ef2aSThomas Huth     }
182fcf5ef2aSThomas Huth     return cpu->model->def->mha_pow;
183fcf5ef2aSThomas Huth }
184fcf5ef2aSThomas Huth 
s390_get_ibc_val(void)185fcf5ef2aSThomas Huth uint32_t s390_get_ibc_val(void)
186fcf5ef2aSThomas Huth {
187fcf5ef2aSThomas Huth     uint16_t unblocked_ibc, lowest_ibc;
188fcf5ef2aSThomas Huth     static S390CPU *cpu;
189fcf5ef2aSThomas Huth 
190fcf5ef2aSThomas Huth     if (!cpu) {
191fcf5ef2aSThomas Huth         cpu = S390_CPU(qemu_get_cpu(0));
192fcf5ef2aSThomas Huth     }
193fcf5ef2aSThomas Huth 
194fcf5ef2aSThomas Huth     if (!cpu || !cpu->model) {
195fcf5ef2aSThomas Huth         return 0;
196fcf5ef2aSThomas Huth     }
197fcf5ef2aSThomas Huth     unblocked_ibc = s390_ibc_from_cpu_model(cpu->model);
198fcf5ef2aSThomas Huth     lowest_ibc = cpu->model->lowest_ibc;
199fcf5ef2aSThomas Huth     /* the lowest_ibc always has to be <= unblocked_ibc */
200fcf5ef2aSThomas Huth     if (!lowest_ibc || lowest_ibc > unblocked_ibc) {
201fcf5ef2aSThomas Huth         return 0;
202fcf5ef2aSThomas Huth     }
203fcf5ef2aSThomas Huth     return ((uint32_t) lowest_ibc << 16) | unblocked_ibc;
204fcf5ef2aSThomas Huth }
205fcf5ef2aSThomas Huth 
s390_get_feat_block(S390FeatType type,uint8_t * data)206fcf5ef2aSThomas Huth void s390_get_feat_block(S390FeatType type, uint8_t *data)
207fcf5ef2aSThomas Huth {
208ad63e6d6SPhilippe Mathieu-Daudé     S390CPU *cpu = S390_CPU(first_cpu);
209fcf5ef2aSThomas Huth 
210fcf5ef2aSThomas Huth     if (!cpu || !cpu->model) {
211fcf5ef2aSThomas Huth         return;
212fcf5ef2aSThomas Huth     }
213fcf5ef2aSThomas Huth     s390_fill_feat_block(cpu->model->features, type, data);
214fcf5ef2aSThomas Huth }
215fcf5ef2aSThomas Huth 
s390_has_feat(S390Feat feat)216fcf5ef2aSThomas Huth bool s390_has_feat(S390Feat feat)
217fcf5ef2aSThomas Huth {
218fcf5ef2aSThomas Huth     static S390CPU *cpu;
219fcf5ef2aSThomas Huth 
220fcf5ef2aSThomas Huth     if (!cpu) {
221fcf5ef2aSThomas Huth         cpu = S390_CPU(qemu_get_cpu(0));
222fcf5ef2aSThomas Huth     }
223fcf5ef2aSThomas Huth 
224fcf5ef2aSThomas Huth     if (!cpu || !cpu->model) {
225fcf5ef2aSThomas Huth #ifdef CONFIG_KVM
226fcf5ef2aSThomas Huth         if (kvm_enabled()) {
227fcf5ef2aSThomas Huth             if (feat == S390_FEAT_VECTOR) {
228fcf5ef2aSThomas Huth                 return kvm_check_extension(kvm_state,
229fcf5ef2aSThomas Huth                                            KVM_CAP_S390_VECTOR_REGISTERS);
230fcf5ef2aSThomas Huth             }
231fcf5ef2aSThomas Huth             if (feat == S390_FEAT_RUNTIME_INSTRUMENTATION) {
232fcf5ef2aSThomas Huth                 return kvm_s390_get_ri();
233fcf5ef2aSThomas Huth             }
234fcf5ef2aSThomas Huth             if (feat == S390_FEAT_MSA_EXT_3) {
235fcf5ef2aSThomas Huth                 return true;
236fcf5ef2aSThomas Huth             }
237fcf5ef2aSThomas Huth         }
238fcf5ef2aSThomas Huth #endif
2398ad9087cSCornelia Huck         if (feat == S390_FEAT_ZPCI) {
2408ad9087cSCornelia Huck             return true;
2418ad9087cSCornelia Huck         }
242fcf5ef2aSThomas Huth         return 0;
243fcf5ef2aSThomas Huth     }
24410248418SJanosch Frank 
2453ea7e312SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY
24610248418SJanosch Frank     if (s390_is_pv()) {
24710248418SJanosch Frank         switch (feat) {
24810248418SJanosch Frank         case S390_FEAT_DIAG_318:
24910248418SJanosch Frank         case S390_FEAT_HPMA2:
25010248418SJanosch Frank         case S390_FEAT_SIE_F2:
25110248418SJanosch Frank         case S390_FEAT_SIE_SKEY:
25210248418SJanosch Frank         case S390_FEAT_SIE_GPERE:
25310248418SJanosch Frank         case S390_FEAT_SIE_SIIF:
25410248418SJanosch Frank         case S390_FEAT_SIE_SIGPIF:
25510248418SJanosch Frank         case S390_FEAT_SIE_IB:
25610248418SJanosch Frank         case S390_FEAT_SIE_CEI:
25710248418SJanosch Frank         case S390_FEAT_SIE_KSS:
25810248418SJanosch Frank         case S390_FEAT_SIE_GSLS:
25910248418SJanosch Frank         case S390_FEAT_SIE_64BSCAO:
26010248418SJanosch Frank         case S390_FEAT_SIE_CMMA:
26110248418SJanosch Frank         case S390_FEAT_SIE_PFMFI:
26210248418SJanosch Frank         case S390_FEAT_SIE_IBS:
263f530b9e7SPierre Morel         case S390_FEAT_CONFIGURATION_TOPOLOGY:
2643ded270aSJanosch Frank             return false;
26510248418SJanosch Frank             break;
26610248418SJanosch Frank         default:
26710248418SJanosch Frank             break;
26810248418SJanosch Frank         }
2693ded270aSJanosch Frank     }
2703ea7e312SPhilippe Mathieu-Daudé #endif
271fcf5ef2aSThomas Huth     return test_bit(feat, cpu->model->features);
272fcf5ef2aSThomas Huth }
273fcf5ef2aSThomas Huth 
s390_get_gen_for_cpu_type(uint16_t type)274fcf5ef2aSThomas Huth uint8_t s390_get_gen_for_cpu_type(uint16_t type)
275fcf5ef2aSThomas Huth {
276fcf5ef2aSThomas Huth     int i;
277fcf5ef2aSThomas Huth 
278fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
279fcf5ef2aSThomas Huth         if (s390_cpu_defs[i].type == type) {
280fcf5ef2aSThomas Huth             return s390_cpu_defs[i].gen;
281fcf5ef2aSThomas Huth         }
282fcf5ef2aSThomas Huth     }
283fcf5ef2aSThomas Huth     return 0;
284fcf5ef2aSThomas Huth }
285fcf5ef2aSThomas Huth 
s390_find_cpu_def(uint16_t type,uint8_t gen,uint8_t ec_ga,S390FeatBitmap features)286fcf5ef2aSThomas Huth const S390CPUDef *s390_find_cpu_def(uint16_t type, uint8_t gen, uint8_t ec_ga,
287fcf5ef2aSThomas Huth                                     S390FeatBitmap features)
288fcf5ef2aSThomas Huth {
289fcf5ef2aSThomas Huth     const S390CPUDef *last_compatible = NULL;
290fbe8202eSDavid Hildenbrand     const S390CPUDef *matching_cpu_type = NULL;
291fcf5ef2aSThomas Huth     int i;
292fcf5ef2aSThomas Huth 
293fcf5ef2aSThomas Huth     if (!gen) {
294fcf5ef2aSThomas Huth         ec_ga = 0;
295fcf5ef2aSThomas Huth     }
296fcf5ef2aSThomas Huth     if (!gen && type) {
297fcf5ef2aSThomas Huth         gen = s390_get_gen_for_cpu_type(type);
298fcf5ef2aSThomas Huth     }
299fcf5ef2aSThomas Huth 
300fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
301fcf5ef2aSThomas Huth         const S390CPUDef *def = &s390_cpu_defs[i];
302fcf5ef2aSThomas Huth         S390FeatBitmap missing;
303fcf5ef2aSThomas Huth 
304fcf5ef2aSThomas Huth         /* don't even try newer generations if we know the generation */
305fcf5ef2aSThomas Huth         if (gen) {
306fcf5ef2aSThomas Huth             if (def->gen > gen) {
307fcf5ef2aSThomas Huth                 break;
308fcf5ef2aSThomas Huth             } else if (def->gen == gen && ec_ga && def->ec_ga > ec_ga) {
309fcf5ef2aSThomas Huth                 break;
310fcf5ef2aSThomas Huth             }
311fcf5ef2aSThomas Huth         }
312fcf5ef2aSThomas Huth 
313fcf5ef2aSThomas Huth         if (features) {
314fcf5ef2aSThomas Huth             /* see if the model satisfies the minimum features */
315fcf5ef2aSThomas Huth             bitmap_andnot(missing, def->base_feat, features, S390_FEAT_MAX);
31630e82de7SDavid Hildenbrand             /*
31730e82de7SDavid Hildenbrand              * Ignore certain features that are in the base model, but not
31830e82de7SDavid Hildenbrand              * relevant for the search (esp. MSA subfunctions).
31930e82de7SDavid Hildenbrand              */
32030e82de7SDavid Hildenbrand             bitmap_andnot(missing, missing, ignored_base_feat, S390_FEAT_MAX);
321fcf5ef2aSThomas Huth             if (!bitmap_empty(missing, S390_FEAT_MAX)) {
322fcf5ef2aSThomas Huth                 break;
323fcf5ef2aSThomas Huth             }
324fcf5ef2aSThomas Huth         }
325fcf5ef2aSThomas Huth 
326fcf5ef2aSThomas Huth         /* stop the search if we found the exact model */
327fcf5ef2aSThomas Huth         if (def->type == type && def->ec_ga == ec_ga) {
328fcf5ef2aSThomas Huth             return def;
329fcf5ef2aSThomas Huth         }
330fbe8202eSDavid Hildenbrand         /* remember if we've at least seen one with the same cpu type */
331fbe8202eSDavid Hildenbrand         if (def->type == type) {
332fbe8202eSDavid Hildenbrand             matching_cpu_type = def;
333fbe8202eSDavid Hildenbrand         }
334fcf5ef2aSThomas Huth         last_compatible = def;
335fcf5ef2aSThomas Huth     }
336fbe8202eSDavid Hildenbrand     /* prefer the model with the same cpu type, esp. don't take the BC for EC */
337fbe8202eSDavid Hildenbrand     if (matching_cpu_type) {
338fbe8202eSDavid Hildenbrand         return matching_cpu_type;
339fbe8202eSDavid Hildenbrand     }
340fcf5ef2aSThomas Huth     return last_compatible;
341fcf5ef2aSThomas Huth }
342fcf5ef2aSThomas Huth 
s390_print_cpu_model_list_entry(gpointer data,gpointer user_data)34399aa6bf2SDavid Hildenbrand static void s390_print_cpu_model_list_entry(gpointer data, gpointer user_data)
344fcf5ef2aSThomas Huth {
34599aa6bf2SDavid Hildenbrand     const S390CPUClass *scc = S390_CPU_CLASS((ObjectClass *)data);
346738cdc2fSDaniel P. Berrangé     CPUClass *cc = CPU_CLASS(scc);
34799aa6bf2SDavid Hildenbrand     char *name = g_strdup(object_class_get_name((ObjectClass *)data));
348738cdc2fSDaniel P. Berrangé     g_autoptr(GString) details = g_string_new("");
349fcf5ef2aSThomas Huth 
350fcf5ef2aSThomas Huth     if (scc->is_static) {
351738cdc2fSDaniel P. Berrangé         g_string_append(details, "static, ");
352738cdc2fSDaniel P. Berrangé     }
353738cdc2fSDaniel P. Berrangé     if (scc->is_migration_safe) {
354738cdc2fSDaniel P. Berrangé         g_string_append(details, "migration-safe, ");
355738cdc2fSDaniel P. Berrangé     }
356738cdc2fSDaniel P. Berrangé     if (cc->deprecation_note) {
357738cdc2fSDaniel P. Berrangé         g_string_append(details, "deprecated, ");
358738cdc2fSDaniel P. Berrangé     }
359738cdc2fSDaniel P. Berrangé     if (details->len) {
360738cdc2fSDaniel P. Berrangé         /* cull trailing ', ' */
361738cdc2fSDaniel P. Berrangé         g_string_truncate(details, details->len - 2);
362fcf5ef2aSThomas Huth     }
363fcf5ef2aSThomas Huth 
364e555cbe7SDavid Hildenbrand     /* strip off the -s390x-cpu */
365fcf5ef2aSThomas Huth     g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
366738cdc2fSDaniel P. Berrangé     if (details->len) {
3677febce36SThomas Huth         qemu_printf("  %-15s %-35s (%s)\n", name, scc->desc, details->str);
368738cdc2fSDaniel P. Berrangé     } else {
3697febce36SThomas Huth         qemu_printf("  %-15s %-35s\n", name, scc->desc);
370738cdc2fSDaniel P. Berrangé     }
371fcf5ef2aSThomas Huth     g_free(name);
372fcf5ef2aSThomas Huth }
373fcf5ef2aSThomas Huth 
s390_cpu_list_compare(gconstpointer a,gconstpointer b)37499aa6bf2SDavid Hildenbrand static gint s390_cpu_list_compare(gconstpointer a, gconstpointer b)
37599aa6bf2SDavid Hildenbrand {
37699aa6bf2SDavid Hildenbrand     const S390CPUClass *cc_a = S390_CPU_CLASS((ObjectClass *)a);
37799aa6bf2SDavid Hildenbrand     const S390CPUClass *cc_b = S390_CPU_CLASS((ObjectClass *)b);
37899aa6bf2SDavid Hildenbrand     const char *name_a = object_class_get_name((ObjectClass *)a);
37999aa6bf2SDavid Hildenbrand     const char *name_b = object_class_get_name((ObjectClass *)b);
38099aa6bf2SDavid Hildenbrand 
381c6117788SDavid Hildenbrand     /*
382c6117788SDavid Hildenbrand      * Move qemu, host and max to the top of the list, qemu first, host second,
383c6117788SDavid Hildenbrand      * max third.
384c6117788SDavid Hildenbrand      */
38599aa6bf2SDavid Hildenbrand     if (name_a[0] == 'q') {
38699aa6bf2SDavid Hildenbrand         return -1;
38799aa6bf2SDavid Hildenbrand     } else if (name_b[0] == 'q') {
38899aa6bf2SDavid Hildenbrand         return 1;
38999aa6bf2SDavid Hildenbrand     } else if (name_a[0] == 'h') {
39099aa6bf2SDavid Hildenbrand         return -1;
39199aa6bf2SDavid Hildenbrand     } else if (name_b[0] == 'h') {
39299aa6bf2SDavid Hildenbrand         return 1;
393c6117788SDavid Hildenbrand     } else if (name_a[0] == 'm') {
394c6117788SDavid Hildenbrand         return -1;
395c6117788SDavid Hildenbrand     } else if (name_b[0] == 'm') {
396c6117788SDavid Hildenbrand         return 1;
39799aa6bf2SDavid Hildenbrand     }
39899aa6bf2SDavid Hildenbrand 
39999aa6bf2SDavid Hildenbrand     /* keep the same order we have in our table (sorted by release date) */
40099aa6bf2SDavid Hildenbrand     if (cc_a->cpu_def != cc_b->cpu_def) {
40199aa6bf2SDavid Hildenbrand         return cc_a->cpu_def - cc_b->cpu_def;
40299aa6bf2SDavid Hildenbrand     }
40399aa6bf2SDavid Hildenbrand 
40499aa6bf2SDavid Hildenbrand     /* exact same definition - list base model first */
40599aa6bf2SDavid Hildenbrand     return cc_a->is_static ? -1 : 1;
40699aa6bf2SDavid Hildenbrand }
40799aa6bf2SDavid Hildenbrand 
s390_cpu_list(void)4080442428aSMarkus Armbruster void s390_cpu_list(void)
409fcf5ef2aSThomas Huth {
410fcf5ef2aSThomas Huth     S390FeatGroup group;
411fcf5ef2aSThomas Huth     S390Feat feat;
41299aa6bf2SDavid Hildenbrand     GSList *list;
413fcf5ef2aSThomas Huth 
4147febce36SThomas Huth     qemu_printf("Available CPUs:\n");
41599aa6bf2SDavid Hildenbrand     list = object_class_get_list(TYPE_S390_CPU, false);
41699aa6bf2SDavid Hildenbrand     list = g_slist_sort(list, s390_cpu_list_compare);
4170442428aSMarkus Armbruster     g_slist_foreach(list, s390_print_cpu_model_list_entry, NULL);
41899aa6bf2SDavid Hildenbrand     g_slist_free(list);
419fcf5ef2aSThomas Huth 
4200442428aSMarkus Armbruster     qemu_printf("\nRecognized feature flags:\n");
421fcf5ef2aSThomas Huth     for (feat = 0; feat < S390_FEAT_MAX; feat++) {
422fcf5ef2aSThomas Huth         const S390FeatDef *def = s390_feat_def(feat);
423fcf5ef2aSThomas Huth 
42461848717SMarkus Armbruster         qemu_printf("  %-20s %s\n", def->name, def->desc);
425fcf5ef2aSThomas Huth     }
426fcf5ef2aSThomas Huth 
4270442428aSMarkus Armbruster     qemu_printf("\nRecognized feature groups:\n");
428fcf5ef2aSThomas Huth     for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
429fcf5ef2aSThomas Huth         const S390FeatGroupDef *def = s390_feat_group_def(group);
430fcf5ef2aSThomas Huth 
43161848717SMarkus Armbruster         qemu_printf("  %-20s %s\n", def->name, def->desc);
432fcf5ef2aSThomas Huth     }
433fcf5ef2aSThomas Huth }
434fcf5ef2aSThomas Huth 
check_consistency(const S390CPUModel * model)435fcf5ef2aSThomas Huth static void check_consistency(const S390CPUModel *model)
436fcf5ef2aSThomas Huth {
437fcf5ef2aSThomas Huth     static int dep[][2] = {
438fcf5ef2aSThomas Huth         { S390_FEAT_IPTE_RANGE, S390_FEAT_DAT_ENH },
439fcf5ef2aSThomas Huth         { S390_FEAT_IDTE_SEGMENT, S390_FEAT_DAT_ENH },
440fcf5ef2aSThomas Huth         { S390_FEAT_IDTE_REGION, S390_FEAT_DAT_ENH },
441fcf5ef2aSThomas Huth         { S390_FEAT_IDTE_REGION, S390_FEAT_IDTE_SEGMENT },
442fcf5ef2aSThomas Huth         { S390_FEAT_LOCAL_TLB_CLEARING, S390_FEAT_DAT_ENH},
443fcf5ef2aSThomas Huth         { S390_FEAT_LONG_DISPLACEMENT_FAST, S390_FEAT_LONG_DISPLACEMENT },
444fcf5ef2aSThomas Huth         { S390_FEAT_DFP_FAST, S390_FEAT_DFP },
445fcf5ef2aSThomas Huth         { S390_FEAT_TRANSACTIONAL_EXE, S390_FEAT_STFLE_49 },
446fcf5ef2aSThomas Huth         { S390_FEAT_EDAT_2, S390_FEAT_EDAT},
447fcf5ef2aSThomas Huth         { S390_FEAT_MSA_EXT_5, S390_FEAT_KIMD_SHA_512 },
448fcf5ef2aSThomas Huth         { S390_FEAT_MSA_EXT_5, S390_FEAT_KLMD_SHA_512 },
449fcf5ef2aSThomas Huth         { S390_FEAT_MSA_EXT_4, S390_FEAT_MSA_EXT_3 },
450fcf5ef2aSThomas Huth         { S390_FEAT_SIE_CMMA, S390_FEAT_CMM },
451fcf5ef2aSThomas Huth         { S390_FEAT_SIE_CMMA, S390_FEAT_SIE_GSLS },
452fcf5ef2aSThomas Huth         { S390_FEAT_SIE_PFMFI, S390_FEAT_EDAT },
4536da5c593SJason J. Herne         { S390_FEAT_MSA_EXT_8, S390_FEAT_MSA_EXT_3 },
4545dacbe23SChristian Borntraeger         { S390_FEAT_MSA_EXT_9, S390_FEAT_MSA_EXT_3 },
4555dacbe23SChristian Borntraeger         { S390_FEAT_MSA_EXT_9, S390_FEAT_MSA_EXT_4 },
4566da5c593SJason J. Herne         { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING },
4576da5c593SJason J. Herne         { S390_FEAT_VECTOR_PACKED_DECIMAL, S390_FEAT_VECTOR },
458fb4a0812SChristian Borntraeger         { S390_FEAT_VECTOR_PACKED_DECIMAL_ENH, S390_FEAT_VECTOR_PACKED_DECIMAL },
459fb4a0812SChristian Borntraeger         { S390_FEAT_VECTOR_PACKED_DECIMAL_ENH2, S390_FEAT_VECTOR_PACKED_DECIMAL_ENH },
4606da5c593SJason J. Herne         { S390_FEAT_VECTOR_ENH, S390_FEAT_VECTOR },
4616da5c593SJason J. Herne         { S390_FEAT_INSTRUCTION_EXEC_PROT, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 },
4626da5c593SJason J. Herne         { S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2, S390_FEAT_ESOP },
4636da5c593SJason J. Herne         { S390_FEAT_CMM_NT, S390_FEAT_CMM },
4646da5c593SJason J. Herne         { S390_FEAT_GUARDED_STORAGE, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 },
4656da5c593SJason J. Herne         { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_STORE_CLOCK_FAST },
4666da5c593SJason J. Herne         { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING },
4676da5c593SJason J. Herne         { S390_FEAT_SEMAPHORE_ASSIST, S390_FEAT_STFLE_49 },
4686da5c593SJason J. Herne         { S390_FEAT_KIMD_SHA3_224, S390_FEAT_MSA },
4696da5c593SJason J. Herne         { S390_FEAT_KIMD_SHA3_256, S390_FEAT_MSA },
4706da5c593SJason J. Herne         { S390_FEAT_KIMD_SHA3_384, S390_FEAT_MSA },
4716da5c593SJason J. Herne         { S390_FEAT_KIMD_SHA3_512, S390_FEAT_MSA },
4726da5c593SJason J. Herne         { S390_FEAT_KIMD_SHAKE_128, S390_FEAT_MSA },
4736da5c593SJason J. Herne         { S390_FEAT_KIMD_SHAKE_256, S390_FEAT_MSA },
4746da5c593SJason J. Herne         { S390_FEAT_KLMD_SHA3_224, S390_FEAT_MSA },
4756da5c593SJason J. Herne         { S390_FEAT_KLMD_SHA3_256, S390_FEAT_MSA },
4766da5c593SJason J. Herne         { S390_FEAT_KLMD_SHA3_384, S390_FEAT_MSA },
4776da5c593SJason J. Herne         { S390_FEAT_KLMD_SHA3_512, S390_FEAT_MSA },
4786da5c593SJason J. Herne         { S390_FEAT_KLMD_SHAKE_128, S390_FEAT_MSA },
4796da5c593SJason J. Herne         { S390_FEAT_KLMD_SHAKE_256, S390_FEAT_MSA },
4806da5c593SJason J. Herne         { S390_FEAT_PRNO_TRNG_QRTCR, S390_FEAT_MSA_EXT_5 },
4816da5c593SJason J. Herne         { S390_FEAT_PRNO_TRNG, S390_FEAT_MSA_EXT_5 },
482c1b364ffSChristian Borntraeger         { S390_FEAT_SIE_KSS, S390_FEAT_SIE_F2 },
483c5cd17afSTony Krowiak         { S390_FEAT_AP_QUERY_CONFIG_INFO, S390_FEAT_AP },
484c5cd17afSTony Krowiak         { S390_FEAT_AP_FACILITIES_TEST, S390_FEAT_AP },
485ddf5d18aSCollin Walling         { S390_FEAT_PTFF_QSIE, S390_FEAT_MULTIPLE_EPOCH },
486ddf5d18aSCollin Walling         { S390_FEAT_PTFF_QTOUE, S390_FEAT_MULTIPLE_EPOCH },
487ddf5d18aSCollin Walling         { S390_FEAT_PTFF_STOE, S390_FEAT_MULTIPLE_EPOCH },
488ddf5d18aSCollin Walling         { S390_FEAT_PTFF_STOUE, S390_FEAT_MULTIPLE_EPOCH },
4899ef2d19eSPierre Morel         { S390_FEAT_AP_QUEUE_INTERRUPT_CONTROL, S390_FEAT_AP },
490fabdada9SCollin Walling         { S390_FEAT_DIAG_318, S390_FEAT_EXTENDED_LENGTH_SCCB },
491fb4a0812SChristian Borntraeger         { S390_FEAT_NNPA, S390_FEAT_VECTOR },
492fb4a0812SChristian Borntraeger         { S390_FEAT_RDP, S390_FEAT_LOCAL_TLB_CLEARING },
4935ac95151SSteffen Eiden         { S390_FEAT_UV_FEAT_AP, S390_FEAT_AP },
4945ac95151SSteffen Eiden         { S390_FEAT_UV_FEAT_AP_INTR, S390_FEAT_UV_FEAT_AP },
495fcf5ef2aSThomas Huth     };
496fcf5ef2aSThomas Huth     int i;
497fcf5ef2aSThomas Huth 
498fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(dep); i++) {
499fcf5ef2aSThomas Huth         if (test_bit(dep[i][0], model->features) &&
500fcf5ef2aSThomas Huth             !test_bit(dep[i][1], model->features)) {
5013dc6f869SAlistair Francis             warn_report("\'%s\' requires \'%s\'.",
502fcf5ef2aSThomas Huth                         s390_feat_def(dep[i][0])->name,
503fcf5ef2aSThomas Huth                         s390_feat_def(dep[i][1])->name);
504fcf5ef2aSThomas Huth         }
505fcf5ef2aSThomas Huth     }
506fcf5ef2aSThomas Huth }
507fcf5ef2aSThomas Huth 
error_prepend_missing_feat(const char * name,void * opaque)508fcf5ef2aSThomas Huth static void error_prepend_missing_feat(const char *name, void *opaque)
509fcf5ef2aSThomas Huth {
510fcf5ef2aSThomas Huth     error_prepend((Error **) opaque, "%s ", name);
511fcf5ef2aSThomas Huth }
512fcf5ef2aSThomas Huth 
check_compat_model_failed(Error ** errp,const S390CPUModel * max_model,const char * msg)513ff7c98a9SClaudio Fontana static void check_compat_model_failed(Error **errp,
514ff7c98a9SClaudio Fontana                                       const S390CPUModel *max_model,
515ff7c98a9SClaudio Fontana                                       const char *msg)
516ff7c98a9SClaudio Fontana {
517ff7c98a9SClaudio Fontana     error_setg(errp, "%s. Maximum supported model in the current configuration: \'%s\'",
518ff7c98a9SClaudio Fontana                msg, max_model->def->name);
519ff7c98a9SClaudio Fontana     error_append_hint(errp, "Consider a different accelerator, try \"-accel help\"\n");
520ff7c98a9SClaudio Fontana     return;
521ff7c98a9SClaudio Fontana }
522ff7c98a9SClaudio Fontana 
check_compatibility(const S390CPUModel * max_model,const S390CPUModel * model,Error ** errp)5237c0c099aSZhao Liu static bool check_compatibility(const S390CPUModel *max_model,
524fcf5ef2aSThomas Huth                                 const S390CPUModel *model, Error **errp)
525fcf5ef2aSThomas Huth {
52695e9053aSZhao Liu     ERRP_GUARD();
527fcf5ef2aSThomas Huth     S390FeatBitmap missing;
528fcf5ef2aSThomas Huth 
529fcf5ef2aSThomas Huth     if (model->def->gen > max_model->def->gen) {
530ff7c98a9SClaudio Fontana         check_compat_model_failed(errp, max_model, "Selected CPU generation is too new");
5317c0c099aSZhao Liu         return false;
532fcf5ef2aSThomas Huth     } else if (model->def->gen == max_model->def->gen &&
533fcf5ef2aSThomas Huth                model->def->ec_ga > max_model->def->ec_ga) {
534ff7c98a9SClaudio Fontana         check_compat_model_failed(errp, max_model, "Selected CPU GA level is too new");
5357c0c099aSZhao Liu         return false;
536fcf5ef2aSThomas Huth     }
537fcf5ef2aSThomas Huth 
5380110253eSChristian Borntraeger #ifndef CONFIG_USER_ONLY
5390110253eSChristian Borntraeger     if (only_migratable && test_bit(S390_FEAT_UNPACK, model->features)) {
5400110253eSChristian Borntraeger         error_setg(errp, "The unpack facility is not compatible with "
5410110253eSChristian Borntraeger                    "the --only-migratable option. You must remove either "
5420110253eSChristian Borntraeger                    "the 'unpack' facility or the --only-migratable option");
5437c0c099aSZhao Liu         return false;
5440110253eSChristian Borntraeger     }
5450110253eSChristian Borntraeger #endif
5460110253eSChristian Borntraeger 
547fcf5ef2aSThomas Huth     /* detect the missing features to properly report them */
548fcf5ef2aSThomas Huth     bitmap_andnot(missing, model->features, max_model->features, S390_FEAT_MAX);
549fcf5ef2aSThomas Huth     if (bitmap_empty(missing, S390_FEAT_MAX)) {
5507c0c099aSZhao Liu         return true;
551fcf5ef2aSThomas Huth     }
552fcf5ef2aSThomas Huth 
553fcf5ef2aSThomas Huth     error_setg(errp, " ");
554fcf5ef2aSThomas Huth     s390_feat_bitmap_to_ascii(missing, errp, error_prepend_missing_feat);
555fcf5ef2aSThomas Huth     error_prepend(errp, "Some features requested in the CPU model are not "
556ff7c98a9SClaudio Fontana                   "available in the current configuration: ");
557ff7c98a9SClaudio Fontana     error_append_hint(errp,
558ff7c98a9SClaudio Fontana                       "Consider a different accelerator, QEMU, or kernel version\n");
5597c0c099aSZhao Liu     return false;
560fcf5ef2aSThomas Huth }
561fcf5ef2aSThomas Huth 
get_max_cpu_model(Error ** errp)5627ab3eb42SCho, Yu-Chen S390CPUModel *get_max_cpu_model(Error **errp)
563fcf5ef2aSThomas Huth {
564fcf5ef2aSThomas Huth     static S390CPUModel max_model;
565fcf5ef2aSThomas Huth     static bool cached;
566fcf5ef2aSThomas Huth 
567fcf5ef2aSThomas Huth     if (cached) {
568fcf5ef2aSThomas Huth         return &max_model;
569fcf5ef2aSThomas Huth     }
570fcf5ef2aSThomas Huth 
571fcf5ef2aSThomas Huth     if (kvm_enabled()) {
572c6f1baf2SZhao Liu         if (!kvm_s390_get_host_cpu_model(&max_model, errp)) {
57347ab3b21SZhao Liu             return NULL;
57447ab3b21SZhao Liu         }
575fcf5ef2aSThomas Huth     } else {
57635b4df64SDavid Hildenbrand         max_model.def = s390_find_cpu_def(QEMU_MAX_CPU_TYPE, QEMU_MAX_CPU_GEN,
57735b4df64SDavid Hildenbrand                                           QEMU_MAX_CPU_EC_GA, NULL);
57835b4df64SDavid Hildenbrand         bitmap_copy(max_model.features, qemu_max_cpu_feat, S390_FEAT_MAX);
579fcf5ef2aSThomas Huth     }
580fcf5ef2aSThomas Huth     cached = true;
581fcf5ef2aSThomas Huth     return &max_model;
582fcf5ef2aSThomas Huth }
583fcf5ef2aSThomas Huth 
s390_realize_cpu_model(CPUState * cs,Error ** errp)584fcf5ef2aSThomas Huth void s390_realize_cpu_model(CPUState *cs, Error **errp)
585fcf5ef2aSThomas Huth {
58695e9053aSZhao Liu     ERRP_GUARD();
587fcf5ef2aSThomas Huth     S390CPUClass *xcc = S390_CPU_GET_CLASS(cs);
588fcf5ef2aSThomas Huth     S390CPU *cpu = S390_CPU(cs);
589fcf5ef2aSThomas Huth     const S390CPUModel *max_model;
590fcf5ef2aSThomas Huth 
591fcf5ef2aSThomas Huth     if (xcc->kvm_required && !kvm_enabled()) {
592fcf5ef2aSThomas Huth         error_setg(errp, "CPU definition requires KVM");
593fcf5ef2aSThomas Huth         return;
594fcf5ef2aSThomas Huth     }
595fcf5ef2aSThomas Huth 
596fcf5ef2aSThomas Huth     if (!cpu->model) {
597fcf5ef2aSThomas Huth         /* no host model support -> perform compatibility stuff */
598fcf5ef2aSThomas Huth         apply_cpu_model(NULL, errp);
599fcf5ef2aSThomas Huth         return;
600fcf5ef2aSThomas Huth     }
601fcf5ef2aSThomas Huth 
602fcf5ef2aSThomas Huth     max_model = get_max_cpu_model(errp);
603d687ae1aSMarkus Armbruster     if (!max_model) {
604fcf5ef2aSThomas Huth         error_prepend(errp, "CPU models are not available: ");
605fcf5ef2aSThomas Huth         return;
606fcf5ef2aSThomas Huth     }
607fcf5ef2aSThomas Huth 
608fcf5ef2aSThomas Huth     /* copy over properties that can vary */
609fcf5ef2aSThomas Huth     cpu->model->lowest_ibc = max_model->lowest_ibc;
610fcf5ef2aSThomas Huth     cpu->model->cpu_id = max_model->cpu_id;
61164bc98f4SDavid Hildenbrand     cpu->model->cpu_id_format = max_model->cpu_id_format;
612fcf5ef2aSThomas Huth     cpu->model->cpu_ver = max_model->cpu_ver;
613fcf5ef2aSThomas Huth 
614fcf5ef2aSThomas Huth     check_consistency(cpu->model);
6159c2df9c5SZhao Liu     if (!check_compatibility(max_model, cpu->model, errp)) {
616fcf5ef2aSThomas Huth         return;
617fcf5ef2aSThomas Huth     }
618fcf5ef2aSThomas Huth 
619fcf5ef2aSThomas Huth     apply_cpu_model(cpu->model, errp);
620076d4d39SDavid Hildenbrand 
6211e70ba24SDavid Hildenbrand #if !defined(CONFIG_USER_ONLY)
622076d4d39SDavid Hildenbrand     cpu->env.cpuid = s390_cpuid_from_cpu_model(cpu->model);
623076d4d39SDavid Hildenbrand     if (tcg_enabled()) {
62471b11cbeSIlya Leoshkevich         cpu->env.cpuid = deposit64(cpu->env.cpuid, CPU_PHYS_ADDR_SHIFT,
62571b11cbeSIlya Leoshkevich                                    CPU_PHYS_ADDR_BITS, cpu->env.core_id);
626076d4d39SDavid Hildenbrand     }
6271e70ba24SDavid Hildenbrand #endif
628fcf5ef2aSThomas Huth }
629fcf5ef2aSThomas Huth 
get_feature(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)630fcf5ef2aSThomas Huth static void get_feature(Object *obj, Visitor *v, const char *name,
631fcf5ef2aSThomas Huth                         void *opaque, Error **errp)
632fcf5ef2aSThomas Huth {
633074df27fSDaniele Buono     S390Feat feat = (S390Feat) (uintptr_t) opaque;
634fcf5ef2aSThomas Huth     S390CPU *cpu = S390_CPU(obj);
635fcf5ef2aSThomas Huth     bool value;
636fcf5ef2aSThomas Huth 
637fcf5ef2aSThomas Huth     if (!cpu->model) {
638fcf5ef2aSThomas Huth         error_setg(errp, "Details about the host CPU model are not available, "
639fcf5ef2aSThomas Huth                          "features cannot be queried.");
640fcf5ef2aSThomas Huth         return;
641fcf5ef2aSThomas Huth     }
642fcf5ef2aSThomas Huth 
643fcf5ef2aSThomas Huth     value = test_bit(feat, cpu->model->features);
644fcf5ef2aSThomas Huth     visit_type_bool(v, name, &value, errp);
645fcf5ef2aSThomas Huth }
646fcf5ef2aSThomas Huth 
set_feature(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)647fcf5ef2aSThomas Huth static void set_feature(Object *obj, Visitor *v, const char *name,
648fcf5ef2aSThomas Huth                         void *opaque, Error **errp)
649fcf5ef2aSThomas Huth {
650074df27fSDaniele Buono     S390Feat feat = (S390Feat) (uintptr_t) opaque;
651fcf5ef2aSThomas Huth     DeviceState *dev = DEVICE(obj);
652fcf5ef2aSThomas Huth     S390CPU *cpu = S390_CPU(obj);
653fcf5ef2aSThomas Huth     bool value;
654fcf5ef2aSThomas Huth 
655fcf5ef2aSThomas Huth     if (dev->realized) {
656fcf5ef2aSThomas Huth         error_setg(errp, "Attempt to set property '%s' on '%s' after "
657fcf5ef2aSThomas Huth                    "it was realized", name, object_get_typename(obj));
658fcf5ef2aSThomas Huth         return;
659fcf5ef2aSThomas Huth     } else if (!cpu->model) {
660fcf5ef2aSThomas Huth         error_setg(errp, "Details about the host CPU model are not available, "
661fcf5ef2aSThomas Huth                          "features cannot be changed.");
662fcf5ef2aSThomas Huth         return;
663fcf5ef2aSThomas Huth     }
664fcf5ef2aSThomas Huth 
665668f62ecSMarkus Armbruster     if (!visit_type_bool(v, name, &value, errp)) {
666fcf5ef2aSThomas Huth         return;
667fcf5ef2aSThomas Huth     }
668fcf5ef2aSThomas Huth     if (value) {
669fcf5ef2aSThomas Huth         if (!test_bit(feat, cpu->model->def->full_feat)) {
670fcf5ef2aSThomas Huth             error_setg(errp, "Feature '%s' is not available for CPU model '%s',"
671fcf5ef2aSThomas Huth                        " it was introduced with later models.",
672fcf5ef2aSThomas Huth                        name, cpu->model->def->name);
673fcf5ef2aSThomas Huth             return;
674fcf5ef2aSThomas Huth         }
675fcf5ef2aSThomas Huth         set_bit(feat, cpu->model->features);
676fcf5ef2aSThomas Huth     } else {
677fcf5ef2aSThomas Huth         clear_bit(feat, cpu->model->features);
678fcf5ef2aSThomas Huth     }
679fcf5ef2aSThomas Huth }
680fcf5ef2aSThomas Huth 
get_feature_group(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)681fcf5ef2aSThomas Huth static void get_feature_group(Object *obj, Visitor *v, const char *name,
682fcf5ef2aSThomas Huth                               void *opaque, Error **errp)
683fcf5ef2aSThomas Huth {
684074df27fSDaniele Buono     S390FeatGroup group = (S390FeatGroup) (uintptr_t) opaque;
685fcf5ef2aSThomas Huth     const S390FeatGroupDef *def = s390_feat_group_def(group);
686fcf5ef2aSThomas Huth     S390CPU *cpu = S390_CPU(obj);
687fcf5ef2aSThomas Huth     S390FeatBitmap tmp;
688fcf5ef2aSThomas Huth     bool value;
689fcf5ef2aSThomas Huth 
690fcf5ef2aSThomas Huth     if (!cpu->model) {
691fcf5ef2aSThomas Huth         error_setg(errp, "Details about the host CPU model are not available, "
692fcf5ef2aSThomas Huth                          "features cannot be queried.");
693fcf5ef2aSThomas Huth         return;
694fcf5ef2aSThomas Huth     }
695fcf5ef2aSThomas Huth 
696fcf5ef2aSThomas Huth     /* a group is enabled if all features are enabled */
697fcf5ef2aSThomas Huth     bitmap_and(tmp, cpu->model->features, def->feat, S390_FEAT_MAX);
698fcf5ef2aSThomas Huth     value = bitmap_equal(tmp, def->feat, S390_FEAT_MAX);
699fcf5ef2aSThomas Huth     visit_type_bool(v, name, &value, errp);
700fcf5ef2aSThomas Huth }
701fcf5ef2aSThomas Huth 
set_feature_group(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)702fcf5ef2aSThomas Huth static void set_feature_group(Object *obj, Visitor *v, const char *name,
703fcf5ef2aSThomas Huth                               void *opaque, Error **errp)
704fcf5ef2aSThomas Huth {
705074df27fSDaniele Buono     S390FeatGroup group = (S390FeatGroup) (uintptr_t) opaque;
706fcf5ef2aSThomas Huth     const S390FeatGroupDef *def = s390_feat_group_def(group);
707fcf5ef2aSThomas Huth     DeviceState *dev = DEVICE(obj);
708fcf5ef2aSThomas Huth     S390CPU *cpu = S390_CPU(obj);
709fcf5ef2aSThomas Huth     bool value;
710fcf5ef2aSThomas Huth 
711fcf5ef2aSThomas Huth     if (dev->realized) {
712fcf5ef2aSThomas Huth         error_setg(errp, "Attempt to set property '%s' on '%s' after "
713fcf5ef2aSThomas Huth                    "it was realized", name, object_get_typename(obj));
714fcf5ef2aSThomas Huth         return;
715fcf5ef2aSThomas Huth     } else if (!cpu->model) {
716fcf5ef2aSThomas Huth         error_setg(errp, "Details about the host CPU model are not available, "
717fcf5ef2aSThomas Huth                          "features cannot be changed.");
718fcf5ef2aSThomas Huth         return;
719fcf5ef2aSThomas Huth     }
720fcf5ef2aSThomas Huth 
721668f62ecSMarkus Armbruster     if (!visit_type_bool(v, name, &value, errp)) {
722fcf5ef2aSThomas Huth         return;
723fcf5ef2aSThomas Huth     }
724fcf5ef2aSThomas Huth     if (value) {
725fcf5ef2aSThomas Huth         /* groups are added in one shot, so an intersect is sufficient */
726fcf5ef2aSThomas Huth         if (!bitmap_intersects(def->feat, cpu->model->def->full_feat,
727fcf5ef2aSThomas Huth                                S390_FEAT_MAX)) {
728fcf5ef2aSThomas Huth             error_setg(errp, "Group '%s' is not available for CPU model '%s',"
729fcf5ef2aSThomas Huth                        " it was introduced with later models.",
730fcf5ef2aSThomas Huth                        name, cpu->model->def->name);
731fcf5ef2aSThomas Huth             return;
732fcf5ef2aSThomas Huth         }
733fcf5ef2aSThomas Huth         bitmap_or(cpu->model->features, cpu->model->features, def->feat,
734fcf5ef2aSThomas Huth                   S390_FEAT_MAX);
735fcf5ef2aSThomas Huth     } else {
736fcf5ef2aSThomas Huth         bitmap_andnot(cpu->model->features, cpu->model->features, def->feat,
737fcf5ef2aSThomas Huth                       S390_FEAT_MAX);
738fcf5ef2aSThomas Huth     }
739fcf5ef2aSThomas Huth }
740fcf5ef2aSThomas Huth 
s390_cpu_model_initfn(Object * obj)741fcf5ef2aSThomas Huth static void s390_cpu_model_initfn(Object *obj)
742fcf5ef2aSThomas Huth {
743fcf5ef2aSThomas Huth     S390CPU *cpu = S390_CPU(obj);
744fcf5ef2aSThomas Huth     S390CPUClass *xcc = S390_CPU_GET_CLASS(cpu);
745fcf5ef2aSThomas Huth 
746fcf5ef2aSThomas Huth     cpu->model = g_malloc0(sizeof(*cpu->model));
747fcf5ef2aSThomas Huth     /* copy the model, so we can modify it */
748fcf5ef2aSThomas Huth     cpu->model->def = xcc->cpu_def;
749fcf5ef2aSThomas Huth     if (xcc->is_static) {
750fcf5ef2aSThomas Huth         /* base model - features will never change */
751fcf5ef2aSThomas Huth         bitmap_copy(cpu->model->features, cpu->model->def->base_feat,
752fcf5ef2aSThomas Huth                     S390_FEAT_MAX);
753fcf5ef2aSThomas Huth     } else {
754fcf5ef2aSThomas Huth         /* latest model - features can change */
755fcf5ef2aSThomas Huth         bitmap_copy(cpu->model->features,
756fcf5ef2aSThomas Huth                     cpu->model->def->default_feat, S390_FEAT_MAX);
757fcf5ef2aSThomas Huth     }
758fcf5ef2aSThomas Huth }
759fcf5ef2aSThomas Huth 
76035b4df64SDavid Hildenbrand static S390CPUModel s390_qemu_cpu_model;
76135b4df64SDavid Hildenbrand 
76235b4df64SDavid Hildenbrand /* Set the qemu CPU model (on machine initialization). Must not be called
76335b4df64SDavid Hildenbrand  * once CPUs have been created.
76435b4df64SDavid Hildenbrand  */
s390_set_qemu_cpu_model(uint16_t type,uint8_t gen,uint8_t ec_ga,const S390FeatInit feat_init)76535b4df64SDavid Hildenbrand void s390_set_qemu_cpu_model(uint16_t type, uint8_t gen, uint8_t ec_ga,
76635b4df64SDavid Hildenbrand                              const S390FeatInit feat_init)
76735b4df64SDavid Hildenbrand {
76835b4df64SDavid Hildenbrand     const S390CPUDef *def = s390_find_cpu_def(type, gen, ec_ga, NULL);
76935b4df64SDavid Hildenbrand 
77035b4df64SDavid Hildenbrand     g_assert(def);
7713c55dd58SPhilippe Mathieu-Daudé     g_assert(QTAILQ_EMPTY_RCU(&cpus_queue));
77235b4df64SDavid Hildenbrand 
77335b4df64SDavid Hildenbrand     /* build the CPU model */
774d98ed7d9SDavid Hildenbrand     s390_qemu_cpu_model.def = def;
77535b4df64SDavid Hildenbrand     bitmap_zero(s390_qemu_cpu_model.features, S390_FEAT_MAX);
77635b4df64SDavid Hildenbrand     s390_init_feat_bitmap(feat_init, s390_qemu_cpu_model.features);
77735b4df64SDavid Hildenbrand }
77835b4df64SDavid Hildenbrand 
s390_qemu_cpu_model_initfn(Object * obj)779fcf5ef2aSThomas Huth static void s390_qemu_cpu_model_initfn(Object *obj)
780fcf5ef2aSThomas Huth {
781fcf5ef2aSThomas Huth     S390CPU *cpu = S390_CPU(obj);
782fcf5ef2aSThomas Huth 
783fcf5ef2aSThomas Huth     cpu->model = g_malloc0(sizeof(*cpu->model));
78435b4df64SDavid Hildenbrand     /* copy the CPU model so we can modify it */
78535b4df64SDavid Hildenbrand     memcpy(cpu->model, &s390_qemu_cpu_model, sizeof(*cpu->model));
786fcf5ef2aSThomas Huth }
787fcf5ef2aSThomas Huth 
s390_max_cpu_model_initfn(Object * obj)788c6117788SDavid Hildenbrand static void s390_max_cpu_model_initfn(Object *obj)
789c6117788SDavid Hildenbrand {
790c6117788SDavid Hildenbrand     const S390CPUModel *max_model;
791c6117788SDavid Hildenbrand     S390CPU *cpu = S390_CPU(obj);
792c6117788SDavid Hildenbrand     Error *local_err = NULL;
793c6117788SDavid Hildenbrand 
794c6117788SDavid Hildenbrand     if (kvm_enabled() && !kvm_s390_cpu_models_supported()) {
795c6117788SDavid Hildenbrand         /* "max" and "host" always work, even without CPU model support */
796c6117788SDavid Hildenbrand         return;
797c6117788SDavid Hildenbrand     }
798c6117788SDavid Hildenbrand 
799c6117788SDavid Hildenbrand     max_model = get_max_cpu_model(&local_err);
800c6117788SDavid Hildenbrand     if (local_err) {
801c6117788SDavid Hildenbrand         /* we expect errors only under KVM, when actually querying the kernel */
802c6117788SDavid Hildenbrand         g_assert(kvm_enabled());
803c6117788SDavid Hildenbrand         error_report_err(local_err);
804c6117788SDavid Hildenbrand         /* fallback to unsupported CPU models */
805c6117788SDavid Hildenbrand         return;
806c6117788SDavid Hildenbrand     }
807c6117788SDavid Hildenbrand 
808c6117788SDavid Hildenbrand     cpu->model = g_new(S390CPUModel, 1);
809c6117788SDavid Hildenbrand     /* copy the CPU model so we can modify it */
810c6117788SDavid Hildenbrand     memcpy(cpu->model, max_model, sizeof(*cpu->model));
811c6117788SDavid Hildenbrand }
812c6117788SDavid Hildenbrand 
s390_cpu_model_finalize(Object * obj)813fcf5ef2aSThomas Huth static void s390_cpu_model_finalize(Object *obj)
814fcf5ef2aSThomas Huth {
815fcf5ef2aSThomas Huth     S390CPU *cpu = S390_CPU(obj);
816fcf5ef2aSThomas Huth 
817fcf5ef2aSThomas Huth     g_free(cpu->model);
818fcf5ef2aSThomas Huth     cpu->model = NULL;
819fcf5ef2aSThomas Huth }
820fcf5ef2aSThomas Huth 
get_is_migration_safe(Object * obj,Error ** errp)821fcf5ef2aSThomas Huth static bool get_is_migration_safe(Object *obj, Error **errp)
822fcf5ef2aSThomas Huth {
823fcf5ef2aSThomas Huth     return S390_CPU_GET_CLASS(obj)->is_migration_safe;
824fcf5ef2aSThomas Huth }
825fcf5ef2aSThomas Huth 
get_is_static(Object * obj,Error ** errp)826fcf5ef2aSThomas Huth static bool get_is_static(Object *obj, Error **errp)
827fcf5ef2aSThomas Huth {
828fcf5ef2aSThomas Huth     return S390_CPU_GET_CLASS(obj)->is_static;
829fcf5ef2aSThomas Huth }
830fcf5ef2aSThomas Huth 
get_description(Object * obj,Error ** errp)831fcf5ef2aSThomas Huth static char *get_description(Object *obj, Error **errp)
832fcf5ef2aSThomas Huth {
833fcf5ef2aSThomas Huth     return g_strdup(S390_CPU_GET_CLASS(obj)->desc);
834fcf5ef2aSThomas Huth }
835fcf5ef2aSThomas Huth 
s390_cpu_model_class_register_props(ObjectClass * oc)836fcf5ef2aSThomas Huth void s390_cpu_model_class_register_props(ObjectClass *oc)
837fcf5ef2aSThomas Huth {
838ab76d63aSEduardo Habkost     S390FeatGroup group;
839ab76d63aSEduardo Habkost     S390Feat feat;
840ab76d63aSEduardo Habkost 
841fcf5ef2aSThomas Huth     object_class_property_add_bool(oc, "migration-safe", get_is_migration_safe,
842fcf5ef2aSThomas Huth                                    NULL);
843d2623129SMarkus Armbruster     object_class_property_add_bool(oc, "static", get_is_static,
844d2623129SMarkus Armbruster                                    NULL);
845d2623129SMarkus Armbruster     object_class_property_add_str(oc, "description", get_description, NULL);
846ab76d63aSEduardo Habkost 
847ab76d63aSEduardo Habkost     for (feat = 0; feat < S390_FEAT_MAX; feat++) {
848ab76d63aSEduardo Habkost         const S390FeatDef *def = s390_feat_def(feat);
849ab76d63aSEduardo Habkost         object_class_property_add(oc, def->name, "bool", get_feature,
850ab76d63aSEduardo Habkost                                   set_feature, NULL, (void *) feat);
851ab76d63aSEduardo Habkost         object_class_property_set_description(oc, def->name, def->desc);
852ab76d63aSEduardo Habkost     }
853ab76d63aSEduardo Habkost     for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
854ab76d63aSEduardo Habkost         const S390FeatGroupDef *def = s390_feat_group_def(group);
855ab76d63aSEduardo Habkost         object_class_property_add(oc, def->name, "bool", get_feature_group,
856ab76d63aSEduardo Habkost                                   set_feature_group, NULL, (void *) group);
857ab76d63aSEduardo Habkost         object_class_property_set_description(oc, def->name, def->desc);
858ab76d63aSEduardo Habkost     }
859fcf5ef2aSThomas Huth }
860fcf5ef2aSThomas Huth 
861fcf5ef2aSThomas Huth #ifdef CONFIG_KVM
s390_host_cpu_model_class_init(ObjectClass * oc,void * data)862fcf5ef2aSThomas Huth static void s390_host_cpu_model_class_init(ObjectClass *oc, void *data)
863fcf5ef2aSThomas Huth {
864fcf5ef2aSThomas Huth     S390CPUClass *xcc = S390_CPU_CLASS(oc);
865fcf5ef2aSThomas Huth 
866fcf5ef2aSThomas Huth     xcc->kvm_required = true;
867fcf5ef2aSThomas Huth     xcc->desc = "KVM only: All recognized features";
868fcf5ef2aSThomas Huth }
869fcf5ef2aSThomas Huth #endif
870fcf5ef2aSThomas Huth 
s390_base_cpu_model_class_init(ObjectClass * oc,void * data)871fcf5ef2aSThomas Huth static void s390_base_cpu_model_class_init(ObjectClass *oc, void *data)
872fcf5ef2aSThomas Huth {
873fcf5ef2aSThomas Huth     S390CPUClass *xcc = S390_CPU_CLASS(oc);
874fcf5ef2aSThomas Huth 
875fcf5ef2aSThomas Huth     /* all base models are migration safe */
876fcf5ef2aSThomas Huth     xcc->cpu_def = (const S390CPUDef *) data;
877fcf5ef2aSThomas Huth     xcc->is_migration_safe = true;
878fcf5ef2aSThomas Huth     xcc->is_static = true;
879fcf5ef2aSThomas Huth     xcc->desc = xcc->cpu_def->desc;
880fcf5ef2aSThomas Huth }
881fcf5ef2aSThomas Huth 
s390_cpu_model_class_init(ObjectClass * oc,void * data)882fcf5ef2aSThomas Huth static void s390_cpu_model_class_init(ObjectClass *oc, void *data)
883fcf5ef2aSThomas Huth {
884fcf5ef2aSThomas Huth     S390CPUClass *xcc = S390_CPU_CLASS(oc);
885fcf5ef2aSThomas Huth 
886fcf5ef2aSThomas Huth     /* model that can change between QEMU versions */
887fcf5ef2aSThomas Huth     xcc->cpu_def = (const S390CPUDef *) data;
888fcf5ef2aSThomas Huth     xcc->is_migration_safe = true;
889fcf5ef2aSThomas Huth     xcc->desc = xcc->cpu_def->desc;
890fcf5ef2aSThomas Huth }
891fcf5ef2aSThomas Huth 
s390_qemu_cpu_model_class_init(ObjectClass * oc,void * data)892fcf5ef2aSThomas Huth static void s390_qemu_cpu_model_class_init(ObjectClass *oc, void *data)
893fcf5ef2aSThomas Huth {
894fcf5ef2aSThomas Huth     S390CPUClass *xcc = S390_CPU_CLASS(oc);
895fcf5ef2aSThomas Huth 
896fcf5ef2aSThomas Huth     xcc->is_migration_safe = true;
897fcf5ef2aSThomas Huth     xcc->desc = g_strdup_printf("QEMU Virtual CPU version %s",
898fcf5ef2aSThomas Huth                                 qemu_hw_version());
899fcf5ef2aSThomas Huth }
900fcf5ef2aSThomas Huth 
s390_max_cpu_model_class_init(ObjectClass * oc,void * data)901c6117788SDavid Hildenbrand static void s390_max_cpu_model_class_init(ObjectClass *oc, void *data)
902c6117788SDavid Hildenbrand {
903c6117788SDavid Hildenbrand     S390CPUClass *xcc = S390_CPU_CLASS(oc);
904c6117788SDavid Hildenbrand 
905c6117788SDavid Hildenbrand     /*
906c6117788SDavid Hildenbrand      * The "max" model is neither static nor migration safe. Under KVM
907d98ed7d9SDavid Hildenbrand      * it represents the "host" model. Under TCG it represents the "qemu" CPU
908d98ed7d9SDavid Hildenbrand      * model of the latest QEMU machine.
909c6117788SDavid Hildenbrand      */
910c6117788SDavid Hildenbrand     xcc->desc =
911c6117788SDavid Hildenbrand         "Enables all features supported by the accelerator in the current host";
912c6117788SDavid Hildenbrand }
913c6117788SDavid Hildenbrand 
914fcf5ef2aSThomas Huth /* Generate type name for a cpu model. Caller has to free the string. */
s390_cpu_type_name(const char * model_name)915fcf5ef2aSThomas Huth static char *s390_cpu_type_name(const char *model_name)
916fcf5ef2aSThomas Huth {
917fcf5ef2aSThomas Huth     return g_strdup_printf(S390_CPU_TYPE_NAME("%s"), model_name);
918fcf5ef2aSThomas Huth }
919fcf5ef2aSThomas Huth 
920fcf5ef2aSThomas Huth /* Generate type name for a base cpu model. Caller has to free the string. */
s390_base_cpu_type_name(const char * model_name)921fcf5ef2aSThomas Huth static char *s390_base_cpu_type_name(const char *model_name)
922fcf5ef2aSThomas Huth {
923fcf5ef2aSThomas Huth     return g_strdup_printf(S390_CPU_TYPE_NAME("%s-base"), model_name);
924fcf5ef2aSThomas Huth }
925fcf5ef2aSThomas Huth 
s390_cpu_class_by_name(const char * name)926fcf5ef2aSThomas Huth ObjectClass *s390_cpu_class_by_name(const char *name)
927fcf5ef2aSThomas Huth {
928fcf5ef2aSThomas Huth     char *typename = s390_cpu_type_name(name);
929fcf5ef2aSThomas Huth     ObjectClass *oc;
930fcf5ef2aSThomas Huth 
931fcf5ef2aSThomas Huth     oc = object_class_by_name(typename);
932fcf5ef2aSThomas Huth     g_free(typename);
933fcf5ef2aSThomas Huth     return oc;
934fcf5ef2aSThomas Huth }
935fcf5ef2aSThomas Huth 
936fcf5ef2aSThomas Huth static const TypeInfo qemu_s390_cpu_type_info = {
937fcf5ef2aSThomas Huth     .name = S390_CPU_TYPE_NAME("qemu"),
938fcf5ef2aSThomas Huth     .parent = TYPE_S390_CPU,
939fcf5ef2aSThomas Huth     .instance_init = s390_qemu_cpu_model_initfn,
940fcf5ef2aSThomas Huth     .instance_finalize = s390_cpu_model_finalize,
941fcf5ef2aSThomas Huth     .class_init = s390_qemu_cpu_model_class_init,
942fcf5ef2aSThomas Huth };
943fcf5ef2aSThomas Huth 
944c6117788SDavid Hildenbrand static const TypeInfo max_s390_cpu_type_info = {
945c6117788SDavid Hildenbrand     .name = S390_CPU_TYPE_NAME("max"),
946c6117788SDavid Hildenbrand     .parent = TYPE_S390_CPU,
947c6117788SDavid Hildenbrand     .instance_init = s390_max_cpu_model_initfn,
948c6117788SDavid Hildenbrand     .instance_finalize = s390_cpu_model_finalize,
949c6117788SDavid Hildenbrand     .class_init = s390_max_cpu_model_class_init,
950c6117788SDavid Hildenbrand };
951c6117788SDavid Hildenbrand 
952fcf5ef2aSThomas Huth #ifdef CONFIG_KVM
953fcf5ef2aSThomas Huth static const TypeInfo host_s390_cpu_type_info = {
954fcf5ef2aSThomas Huth     .name = S390_CPU_TYPE_NAME("host"),
955c6117788SDavid Hildenbrand     .parent = S390_CPU_TYPE_NAME("max"),
956fcf5ef2aSThomas Huth     .class_init = s390_host_cpu_model_class_init,
957fcf5ef2aSThomas Huth };
958fcf5ef2aSThomas Huth #endif
959fcf5ef2aSThomas Huth 
init_ignored_base_feat(void)96030e82de7SDavid Hildenbrand static void init_ignored_base_feat(void)
96130e82de7SDavid Hildenbrand {
96230e82de7SDavid Hildenbrand     static const int feats[] = {
96330e82de7SDavid Hildenbrand          /* MSA subfunctions that could not be available on certain machines */
96430e82de7SDavid Hildenbrand          S390_FEAT_KMAC_DEA,
96530e82de7SDavid Hildenbrand          S390_FEAT_KMAC_TDEA_128,
96630e82de7SDavid Hildenbrand          S390_FEAT_KMAC_TDEA_192,
96730e82de7SDavid Hildenbrand          S390_FEAT_KMC_DEA,
96830e82de7SDavid Hildenbrand          S390_FEAT_KMC_TDEA_128,
96930e82de7SDavid Hildenbrand          S390_FEAT_KMC_TDEA_192,
97030e82de7SDavid Hildenbrand          S390_FEAT_KM_DEA,
97130e82de7SDavid Hildenbrand          S390_FEAT_KM_TDEA_128,
97230e82de7SDavid Hildenbrand          S390_FEAT_KM_TDEA_192,
97330e82de7SDavid Hildenbrand          S390_FEAT_KIMD_SHA_1,
97430e82de7SDavid Hildenbrand          S390_FEAT_KLMD_SHA_1,
975eaf6f642SChristian Borntraeger          /* CSSKE is deprecated on newer generations */
976eaf6f642SChristian Borntraeger          S390_FEAT_CONDITIONAL_SSKE,
97730e82de7SDavid Hildenbrand     };
97830e82de7SDavid Hildenbrand     int i;
97930e82de7SDavid Hildenbrand 
98030e82de7SDavid Hildenbrand     for (i = 0; i < ARRAY_SIZE(feats); i++) {
98130e82de7SDavid Hildenbrand         set_bit(feats[i], ignored_base_feat);
98230e82de7SDavid Hildenbrand     }
98330e82de7SDavid Hildenbrand }
98430e82de7SDavid Hildenbrand 
register_types(void)985fcf5ef2aSThomas Huth static void register_types(void)
986fcf5ef2aSThomas Huth {
987d98ed7d9SDavid Hildenbrand     static const S390FeatInit qemu_max_init = { S390_FEAT_LIST_QEMU_MAX };
988fcf5ef2aSThomas Huth     int i;
989fcf5ef2aSThomas Huth 
99030e82de7SDavid Hildenbrand     init_ignored_base_feat();
99130e82de7SDavid Hildenbrand 
992cced0d65SMichael Tokarev     /* init all bitmaps from generated data initially */
993d98ed7d9SDavid Hildenbrand     s390_init_feat_bitmap(qemu_max_init, qemu_max_cpu_feat);
994fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
995fcf5ef2aSThomas Huth         s390_init_feat_bitmap(s390_cpu_defs[i].base_init,
996fcf5ef2aSThomas Huth                               s390_cpu_defs[i].base_feat);
997fcf5ef2aSThomas Huth         s390_init_feat_bitmap(s390_cpu_defs[i].default_init,
998fcf5ef2aSThomas Huth                               s390_cpu_defs[i].default_feat);
999fcf5ef2aSThomas Huth         s390_init_feat_bitmap(s390_cpu_defs[i].full_init,
1000fcf5ef2aSThomas Huth                               s390_cpu_defs[i].full_feat);
1001fcf5ef2aSThomas Huth     }
1002fcf5ef2aSThomas Huth 
1003d98ed7d9SDavid Hildenbrand     /* initialize the qemu model with the maximum definition ("max" model) */
100435b4df64SDavid Hildenbrand     s390_set_qemu_cpu_model(QEMU_MAX_CPU_TYPE, QEMU_MAX_CPU_GEN,
1005d98ed7d9SDavid Hildenbrand                             QEMU_MAX_CPU_EC_GA, qemu_max_init);
100635b4df64SDavid Hildenbrand 
1007fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
1008fcf5ef2aSThomas Huth         char *base_name = s390_base_cpu_type_name(s390_cpu_defs[i].name);
1009fcf5ef2aSThomas Huth         TypeInfo ti_base = {
1010fcf5ef2aSThomas Huth             .name = base_name,
1011fcf5ef2aSThomas Huth             .parent = TYPE_S390_CPU,
1012fcf5ef2aSThomas Huth             .instance_init = s390_cpu_model_initfn,
1013fcf5ef2aSThomas Huth             .instance_finalize = s390_cpu_model_finalize,
1014fcf5ef2aSThomas Huth             .class_init = s390_base_cpu_model_class_init,
1015fcf5ef2aSThomas Huth             .class_data = (void *) &s390_cpu_defs[i],
1016fcf5ef2aSThomas Huth         };
1017fcf5ef2aSThomas Huth         char *name = s390_cpu_type_name(s390_cpu_defs[i].name);
1018fcf5ef2aSThomas Huth         TypeInfo ti = {
1019fcf5ef2aSThomas Huth             .name = name,
1020fcf5ef2aSThomas Huth             .parent = TYPE_S390_CPU,
1021fcf5ef2aSThomas Huth             .instance_init = s390_cpu_model_initfn,
1022fcf5ef2aSThomas Huth             .instance_finalize = s390_cpu_model_finalize,
1023fcf5ef2aSThomas Huth             .class_init = s390_cpu_model_class_init,
1024fcf5ef2aSThomas Huth             .class_data = (void *) &s390_cpu_defs[i],
1025fcf5ef2aSThomas Huth         };
1026fcf5ef2aSThomas Huth 
1027fcf5ef2aSThomas Huth         type_register_static(&ti_base);
1028fcf5ef2aSThomas Huth         type_register_static(&ti);
1029fcf5ef2aSThomas Huth         g_free(base_name);
1030fcf5ef2aSThomas Huth         g_free(name);
1031fcf5ef2aSThomas Huth     }
1032fcf5ef2aSThomas Huth 
1033fcf5ef2aSThomas Huth     type_register_static(&qemu_s390_cpu_type_info);
1034c6117788SDavid Hildenbrand     type_register_static(&max_s390_cpu_type_info);
1035fcf5ef2aSThomas Huth #ifdef CONFIG_KVM
1036fcf5ef2aSThomas Huth     type_register_static(&host_s390_cpu_type_info);
1037fcf5ef2aSThomas Huth #endif
1038fcf5ef2aSThomas Huth }
1039fcf5ef2aSThomas Huth 
1040fcf5ef2aSThomas Huth type_init(register_types)
1041