xref: /openbmc/qemu/target/s390x/cpu_models.c (revision 7e6055c9)
1 /*
2  * CPU models for s390x
3  *
4  * Copyright 2016 IBM Corp.
5  *
6  * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
7  *
8  * This work is licensed under the terms of the GNU GPL, version 2 or (at
9  * your option) any later version. See the COPYING file in the top-level
10  * directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "cpu.h"
15 #include "s390x-internal.h"
16 #include "kvm/kvm_s390x.h"
17 #include "sysemu/kvm.h"
18 #include "sysemu/tcg.h"
19 #include "qapi/error.h"
20 #include "qapi/visitor.h"
21 #include "qemu/module.h"
22 #include "qemu/qemu-print.h"
23 #ifndef CONFIG_USER_ONLY
24 #include "sysemu/sysemu.h"
25 #endif
26 #include "hw/s390x/pv.h"
27 
28 #define CPUDEF_INIT(_type, _gen, _ec_ga, _mha_pow, _hmfai, _name, _desc) \
29     {                                                                    \
30         .name = _name,                                                   \
31         .type = _type,                                                   \
32         .gen = _gen,                                                     \
33         .ec_ga = _ec_ga,                                                 \
34         .mha_pow = _mha_pow,                                             \
35         .hmfai = _hmfai,                                                 \
36         .desc = _desc,                                                   \
37         .base_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _BASE },  \
38         .default_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _DEFAULT },  \
39         .full_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _FULL },  \
40     }
41 
42 /*
43  * CPU definition list in order of release. Up to generation 14 base features
44  * of a following release have been a superset of the previous release. With
45  * generation 15 one base feature and one optional feature have been deprecated.
46  */
47 static S390CPUDef s390_cpu_defs[] = {
48     CPUDEF_INIT(0x2064, 7, 1, 38, 0x00000000U, "z900", "IBM zSeries 900 GA1"),
49     CPUDEF_INIT(0x2064, 7, 2, 38, 0x00000000U, "z900.2", "IBM zSeries 900 GA2"),
50     CPUDEF_INIT(0x2064, 7, 3, 38, 0x00000000U, "z900.3", "IBM zSeries 900 GA3"),
51     CPUDEF_INIT(0x2066, 7, 3, 38, 0x00000000U, "z800", "IBM zSeries 800 GA1"),
52     CPUDEF_INIT(0x2084, 8, 1, 38, 0x00000000U, "z990", "IBM zSeries 990 GA1"),
53     CPUDEF_INIT(0x2084, 8, 2, 38, 0x00000000U, "z990.2", "IBM zSeries 990 GA2"),
54     CPUDEF_INIT(0x2084, 8, 3, 38, 0x00000000U, "z990.3", "IBM zSeries 990 GA3"),
55     CPUDEF_INIT(0x2086, 8, 3, 38, 0x00000000U, "z890", "IBM zSeries 880 GA1"),
56     CPUDEF_INIT(0x2084, 8, 4, 38, 0x00000000U, "z990.4", "IBM zSeries 990 GA4"),
57     CPUDEF_INIT(0x2086, 8, 4, 38, 0x00000000U, "z890.2", "IBM zSeries 880 GA2"),
58     CPUDEF_INIT(0x2084, 8, 5, 38, 0x00000000U, "z990.5", "IBM zSeries 990 GA5"),
59     CPUDEF_INIT(0x2086, 8, 5, 38, 0x00000000U, "z890.3", "IBM zSeries 880 GA3"),
60     CPUDEF_INIT(0x2094, 9, 1, 40, 0x00000000U, "z9EC", "IBM System z9 EC GA1"),
61     CPUDEF_INIT(0x2094, 9, 2, 40, 0x00000000U, "z9EC.2", "IBM System z9 EC GA2"),
62     CPUDEF_INIT(0x2096, 9, 2, 40, 0x00000000U, "z9BC", "IBM System z9 BC GA1"),
63     CPUDEF_INIT(0x2094, 9, 3, 40, 0x00000000U, "z9EC.3", "IBM System z9 EC GA3"),
64     CPUDEF_INIT(0x2096, 9, 3, 40, 0x00000000U, "z9BC.2", "IBM System z9 BC GA2"),
65     CPUDEF_INIT(0x2097, 10, 1, 43, 0x00000000U, "z10EC", "IBM System z10 EC GA1"),
66     CPUDEF_INIT(0x2097, 10, 2, 43, 0x00000000U, "z10EC.2", "IBM System z10 EC GA2"),
67     CPUDEF_INIT(0x2098, 10, 2, 43, 0x00000000U, "z10BC", "IBM System z10 BC GA1"),
68     CPUDEF_INIT(0x2097, 10, 3, 43, 0x00000000U, "z10EC.3", "IBM System z10 EC GA3"),
69     CPUDEF_INIT(0x2098, 10, 3, 43, 0x00000000U, "z10BC.2", "IBM System z10 BC GA2"),
70     CPUDEF_INIT(0x2817, 11, 1, 44, 0x08000000U, "z196", "IBM zEnterprise 196 GA1"),
71     CPUDEF_INIT(0x2817, 11, 2, 44, 0x08000000U, "z196.2", "IBM zEnterprise 196 GA2"),
72     CPUDEF_INIT(0x2818, 11, 2, 44, 0x08000000U, "z114", "IBM zEnterprise 114 GA1"),
73     CPUDEF_INIT(0x2827, 12, 1, 44, 0x08000000U, "zEC12", "IBM zEnterprise EC12 GA1"),
74     CPUDEF_INIT(0x2827, 12, 2, 44, 0x08000000U, "zEC12.2", "IBM zEnterprise EC12 GA2"),
75     CPUDEF_INIT(0x2828, 12, 2, 44, 0x08000000U, "zBC12", "IBM zEnterprise BC12 GA1"),
76     CPUDEF_INIT(0x2964, 13, 1, 47, 0x08000000U, "z13", "IBM z13 GA1"),
77     CPUDEF_INIT(0x2964, 13, 2, 47, 0x08000000U, "z13.2", "IBM z13 GA2"),
78     CPUDEF_INIT(0x2965, 13, 2, 47, 0x08000000U, "z13s", "IBM z13s GA1"),
79     CPUDEF_INIT(0x3906, 14, 1, 47, 0x08000000U, "z14", "IBM z14 GA1"),
80     CPUDEF_INIT(0x3906, 14, 2, 47, 0x08000000U, "z14.2", "IBM z14 GA2"),
81     CPUDEF_INIT(0x3907, 14, 1, 47, 0x08000000U, "z14ZR1", "IBM z14 Model ZR1 GA1"),
82     CPUDEF_INIT(0x8561, 15, 1, 47, 0x08000000U, "gen15a", "IBM z15 T01 GA1"),
83     CPUDEF_INIT(0x8562, 15, 1, 47, 0x08000000U, "gen15b", "IBM z15 T02 GA1"),
84     CPUDEF_INIT(0x3931, 16, 1, 47, 0x08000000U, "gen16a", "IBM 3931 GA1"),
85     CPUDEF_INIT(0x3932, 16, 1, 47, 0x08000000U, "gen16b", "IBM 3932 GA1"),
86 };
87 
88 #define QEMU_MAX_CPU_TYPE 0x3906
89 #define QEMU_MAX_CPU_GEN 14
90 #define QEMU_MAX_CPU_EC_GA 2
91 static const S390FeatInit qemu_max_cpu_feat_init = { S390_FEAT_LIST_QEMU_MAX };
92 static S390FeatBitmap qemu_max_cpu_feat;
93 
94 /* features part of a base model but not relevant for finding a base model */
95 S390FeatBitmap ignored_base_feat;
96 
97 void s390_cpudef_featoff(uint8_t gen, uint8_t ec_ga, S390Feat feat)
98 {
99     const S390CPUDef *def;
100 
101     def = s390_find_cpu_def(0, gen, ec_ga, NULL);
102     clear_bit(feat, (unsigned long *)&def->default_feat);
103 }
104 
105 void s390_cpudef_featoff_greater(uint8_t gen, uint8_t ec_ga, S390Feat feat)
106 {
107     int i;
108 
109     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
110         const S390CPUDef *def = &s390_cpu_defs[i];
111 
112         if (def->gen < gen) {
113             continue;
114         }
115         if (def->gen == gen && def->ec_ga < ec_ga) {
116             continue;
117         }
118 
119         clear_bit(feat, (unsigned long *)&def->default_feat);
120     }
121 }
122 
123 void s390_cpudef_group_featoff_greater(uint8_t gen, uint8_t ec_ga,
124                                        S390FeatGroup group)
125 {
126     const S390FeatGroupDef *group_def = s390_feat_group_def(group);
127     S390FeatBitmap group_def_off;
128     int i;
129 
130     bitmap_complement(group_def_off, group_def->feat, S390_FEAT_MAX);
131 
132     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
133         const S390CPUDef *cpu_def = &s390_cpu_defs[i];
134 
135         if (cpu_def->gen < gen) {
136             continue;
137         }
138         if (cpu_def->gen == gen && cpu_def->ec_ga < ec_ga) {
139             continue;
140         }
141 
142         bitmap_and((unsigned long *)&cpu_def->default_feat,
143                    cpu_def->default_feat, group_def_off, S390_FEAT_MAX);
144     }
145 }
146 
147 uint32_t s390_get_hmfai(void)
148 {
149     static S390CPU *cpu;
150 
151     if (!cpu) {
152         cpu = S390_CPU(qemu_get_cpu(0));
153     }
154 
155     if (!cpu || !cpu->model) {
156         return 0;
157     }
158     return cpu->model->def->hmfai;
159 }
160 
161 uint8_t s390_get_mha_pow(void)
162 {
163     static S390CPU *cpu;
164 
165     if (!cpu) {
166         cpu = S390_CPU(qemu_get_cpu(0));
167     }
168 
169     if (!cpu || !cpu->model) {
170         return 0;
171     }
172     return cpu->model->def->mha_pow;
173 }
174 
175 uint32_t s390_get_ibc_val(void)
176 {
177     uint16_t unblocked_ibc, lowest_ibc;
178     static S390CPU *cpu;
179 
180     if (!cpu) {
181         cpu = S390_CPU(qemu_get_cpu(0));
182     }
183 
184     if (!cpu || !cpu->model) {
185         return 0;
186     }
187     unblocked_ibc = s390_ibc_from_cpu_model(cpu->model);
188     lowest_ibc = cpu->model->lowest_ibc;
189     /* the lowest_ibc always has to be <= unblocked_ibc */
190     if (!lowest_ibc || lowest_ibc > unblocked_ibc) {
191         return 0;
192     }
193     return ((uint32_t) lowest_ibc << 16) | unblocked_ibc;
194 }
195 
196 void s390_get_feat_block(S390FeatType type, uint8_t *data)
197 {
198     static S390CPU *cpu;
199 
200     if (!cpu) {
201         cpu = S390_CPU(qemu_get_cpu(0));
202     }
203 
204     if (!cpu || !cpu->model) {
205         return;
206     }
207     s390_fill_feat_block(cpu->model->features, type, data);
208 }
209 
210 bool s390_has_feat(S390Feat feat)
211 {
212     static S390CPU *cpu;
213 
214     if (!cpu) {
215         cpu = S390_CPU(qemu_get_cpu(0));
216     }
217 
218     if (!cpu || !cpu->model) {
219 #ifdef CONFIG_KVM
220         if (kvm_enabled()) {
221             if (feat == S390_FEAT_VECTOR) {
222                 return kvm_check_extension(kvm_state,
223                                            KVM_CAP_S390_VECTOR_REGISTERS);
224             }
225             if (feat == S390_FEAT_RUNTIME_INSTRUMENTATION) {
226                 return kvm_s390_get_ri();
227             }
228             if (feat == S390_FEAT_MSA_EXT_3) {
229                 return true;
230             }
231         }
232 #endif
233         if (feat == S390_FEAT_ZPCI) {
234             return true;
235         }
236         return 0;
237     }
238 
239     if (s390_is_pv()) {
240         switch (feat) {
241         case S390_FEAT_DIAG_318:
242         case S390_FEAT_HPMA2:
243         case S390_FEAT_SIE_F2:
244         case S390_FEAT_SIE_SKEY:
245         case S390_FEAT_SIE_GPERE:
246         case S390_FEAT_SIE_SIIF:
247         case S390_FEAT_SIE_SIGPIF:
248         case S390_FEAT_SIE_IB:
249         case S390_FEAT_SIE_CEI:
250         case S390_FEAT_SIE_KSS:
251         case S390_FEAT_SIE_GSLS:
252         case S390_FEAT_SIE_64BSCAO:
253         case S390_FEAT_SIE_CMMA:
254         case S390_FEAT_SIE_PFMFI:
255         case S390_FEAT_SIE_IBS:
256             return false;
257             break;
258         default:
259             break;
260         }
261     }
262     return test_bit(feat, cpu->model->features);
263 }
264 
265 uint8_t s390_get_gen_for_cpu_type(uint16_t type)
266 {
267     int i;
268 
269     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
270         if (s390_cpu_defs[i].type == type) {
271             return s390_cpu_defs[i].gen;
272         }
273     }
274     return 0;
275 }
276 
277 const S390CPUDef *s390_find_cpu_def(uint16_t type, uint8_t gen, uint8_t ec_ga,
278                                     S390FeatBitmap features)
279 {
280     const S390CPUDef *last_compatible = NULL;
281     const S390CPUDef *matching_cpu_type = NULL;
282     int i;
283 
284     if (!gen) {
285         ec_ga = 0;
286     }
287     if (!gen && type) {
288         gen = s390_get_gen_for_cpu_type(type);
289     }
290 
291     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
292         const S390CPUDef *def = &s390_cpu_defs[i];
293         S390FeatBitmap missing;
294 
295         /* don't even try newer generations if we know the generation */
296         if (gen) {
297             if (def->gen > gen) {
298                 break;
299             } else if (def->gen == gen && ec_ga && def->ec_ga > ec_ga) {
300                 break;
301             }
302         }
303 
304         if (features) {
305             /* see if the model satisfies the minimum features */
306             bitmap_andnot(missing, def->base_feat, features, S390_FEAT_MAX);
307             /*
308              * Ignore certain features that are in the base model, but not
309              * relevant for the search (esp. MSA subfunctions).
310              */
311             bitmap_andnot(missing, missing, ignored_base_feat, S390_FEAT_MAX);
312             if (!bitmap_empty(missing, S390_FEAT_MAX)) {
313                 break;
314             }
315         }
316 
317         /* stop the search if we found the exact model */
318         if (def->type == type && def->ec_ga == ec_ga) {
319             return def;
320         }
321         /* remember if we've at least seen one with the same cpu type */
322         if (def->type == type) {
323             matching_cpu_type = def;
324         }
325         last_compatible = def;
326     }
327     /* prefer the model with the same cpu type, esp. don't take the BC for EC */
328     if (matching_cpu_type) {
329         return matching_cpu_type;
330     }
331     return last_compatible;
332 }
333 
334 static void s390_print_cpu_model_list_entry(gpointer data, gpointer user_data)
335 {
336     const S390CPUClass *scc = S390_CPU_CLASS((ObjectClass *)data);
337     char *name = g_strdup(object_class_get_name((ObjectClass *)data));
338     const char *details = "";
339 
340     if (scc->is_static) {
341         details = "(static, migration-safe)";
342     } else if (scc->is_migration_safe) {
343         details = "(migration-safe)";
344     }
345 
346     /* strip off the -s390x-cpu */
347     g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
348     qemu_printf("s390 %-15s %-35s %s\n", name, scc->desc, details);
349     g_free(name);
350 }
351 
352 static gint s390_cpu_list_compare(gconstpointer a, gconstpointer b)
353 {
354     const S390CPUClass *cc_a = S390_CPU_CLASS((ObjectClass *)a);
355     const S390CPUClass *cc_b = S390_CPU_CLASS((ObjectClass *)b);
356     const char *name_a = object_class_get_name((ObjectClass *)a);
357     const char *name_b = object_class_get_name((ObjectClass *)b);
358 
359     /*
360      * Move qemu, host and max to the top of the list, qemu first, host second,
361      * max third.
362      */
363     if (name_a[0] == 'q') {
364         return -1;
365     } else if (name_b[0] == 'q') {
366         return 1;
367     } else if (name_a[0] == 'h') {
368         return -1;
369     } else if (name_b[0] == 'h') {
370         return 1;
371     } else if (name_a[0] == 'm') {
372         return -1;
373     } else if (name_b[0] == 'm') {
374         return 1;
375     }
376 
377     /* keep the same order we have in our table (sorted by release date) */
378     if (cc_a->cpu_def != cc_b->cpu_def) {
379         return cc_a->cpu_def - cc_b->cpu_def;
380     }
381 
382     /* exact same definition - list base model first */
383     return cc_a->is_static ? -1 : 1;
384 }
385 
386 void s390_cpu_list(void)
387 {
388     S390FeatGroup group;
389     S390Feat feat;
390     GSList *list;
391 
392     list = object_class_get_list(TYPE_S390_CPU, false);
393     list = g_slist_sort(list, s390_cpu_list_compare);
394     g_slist_foreach(list, s390_print_cpu_model_list_entry, NULL);
395     g_slist_free(list);
396 
397     qemu_printf("\nRecognized feature flags:\n");
398     for (feat = 0; feat < S390_FEAT_MAX; feat++) {
399         const S390FeatDef *def = s390_feat_def(feat);
400 
401         qemu_printf("%-20s %s\n", def->name, def->desc);
402     }
403 
404     qemu_printf("\nRecognized feature groups:\n");
405     for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
406         const S390FeatGroupDef *def = s390_feat_group_def(group);
407 
408         qemu_printf("%-20s %s\n", def->name, def->desc);
409     }
410 }
411 
412 static void check_consistency(const S390CPUModel *model)
413 {
414     static int dep[][2] = {
415         { S390_FEAT_IPTE_RANGE, S390_FEAT_DAT_ENH },
416         { S390_FEAT_IDTE_SEGMENT, S390_FEAT_DAT_ENH },
417         { S390_FEAT_IDTE_REGION, S390_FEAT_DAT_ENH },
418         { S390_FEAT_IDTE_REGION, S390_FEAT_IDTE_SEGMENT },
419         { S390_FEAT_LOCAL_TLB_CLEARING, S390_FEAT_DAT_ENH},
420         { S390_FEAT_LONG_DISPLACEMENT_FAST, S390_FEAT_LONG_DISPLACEMENT },
421         { S390_FEAT_DFP_FAST, S390_FEAT_DFP },
422         { S390_FEAT_TRANSACTIONAL_EXE, S390_FEAT_STFLE_49 },
423         { S390_FEAT_EDAT_2, S390_FEAT_EDAT},
424         { S390_FEAT_MSA_EXT_5, S390_FEAT_KIMD_SHA_512 },
425         { S390_FEAT_MSA_EXT_5, S390_FEAT_KLMD_SHA_512 },
426         { S390_FEAT_MSA_EXT_4, S390_FEAT_MSA_EXT_3 },
427         { S390_FEAT_SIE_CMMA, S390_FEAT_CMM },
428         { S390_FEAT_SIE_CMMA, S390_FEAT_SIE_GSLS },
429         { S390_FEAT_SIE_PFMFI, S390_FEAT_EDAT },
430         { S390_FEAT_MSA_EXT_8, S390_FEAT_MSA_EXT_3 },
431         { S390_FEAT_MSA_EXT_9, S390_FEAT_MSA_EXT_3 },
432         { S390_FEAT_MSA_EXT_9, S390_FEAT_MSA_EXT_4 },
433         { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING },
434         { S390_FEAT_VECTOR_PACKED_DECIMAL, S390_FEAT_VECTOR },
435         { S390_FEAT_VECTOR_PACKED_DECIMAL_ENH, S390_FEAT_VECTOR_PACKED_DECIMAL },
436         { S390_FEAT_VECTOR_PACKED_DECIMAL_ENH2, S390_FEAT_VECTOR_PACKED_DECIMAL_ENH },
437         { S390_FEAT_VECTOR_ENH, S390_FEAT_VECTOR },
438         { S390_FEAT_INSTRUCTION_EXEC_PROT, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 },
439         { S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2, S390_FEAT_ESOP },
440         { S390_FEAT_CMM_NT, S390_FEAT_CMM },
441         { S390_FEAT_GUARDED_STORAGE, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 },
442         { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_STORE_CLOCK_FAST },
443         { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING },
444         { S390_FEAT_SEMAPHORE_ASSIST, S390_FEAT_STFLE_49 },
445         { S390_FEAT_KIMD_SHA3_224, S390_FEAT_MSA },
446         { S390_FEAT_KIMD_SHA3_256, S390_FEAT_MSA },
447         { S390_FEAT_KIMD_SHA3_384, S390_FEAT_MSA },
448         { S390_FEAT_KIMD_SHA3_512, S390_FEAT_MSA },
449         { S390_FEAT_KIMD_SHAKE_128, S390_FEAT_MSA },
450         { S390_FEAT_KIMD_SHAKE_256, S390_FEAT_MSA },
451         { S390_FEAT_KLMD_SHA3_224, S390_FEAT_MSA },
452         { S390_FEAT_KLMD_SHA3_256, S390_FEAT_MSA },
453         { S390_FEAT_KLMD_SHA3_384, S390_FEAT_MSA },
454         { S390_FEAT_KLMD_SHA3_512, S390_FEAT_MSA },
455         { S390_FEAT_KLMD_SHAKE_128, S390_FEAT_MSA },
456         { S390_FEAT_KLMD_SHAKE_256, S390_FEAT_MSA },
457         { S390_FEAT_PRNO_TRNG_QRTCR, S390_FEAT_MSA_EXT_5 },
458         { S390_FEAT_PRNO_TRNG, S390_FEAT_MSA_EXT_5 },
459         { S390_FEAT_SIE_KSS, S390_FEAT_SIE_F2 },
460         { S390_FEAT_AP_QUERY_CONFIG_INFO, S390_FEAT_AP },
461         { S390_FEAT_AP_FACILITIES_TEST, S390_FEAT_AP },
462         { S390_FEAT_PTFF_QSIE, S390_FEAT_MULTIPLE_EPOCH },
463         { S390_FEAT_PTFF_QTOUE, S390_FEAT_MULTIPLE_EPOCH },
464         { S390_FEAT_PTFF_STOE, S390_FEAT_MULTIPLE_EPOCH },
465         { S390_FEAT_PTFF_STOUE, S390_FEAT_MULTIPLE_EPOCH },
466         { S390_FEAT_AP_QUEUE_INTERRUPT_CONTROL, S390_FEAT_AP },
467         { S390_FEAT_DIAG_318, S390_FEAT_EXTENDED_LENGTH_SCCB },
468         { S390_FEAT_NNPA, S390_FEAT_VECTOR },
469         { S390_FEAT_RDP, S390_FEAT_LOCAL_TLB_CLEARING },
470     };
471     int i;
472 
473     for (i = 0; i < ARRAY_SIZE(dep); i++) {
474         if (test_bit(dep[i][0], model->features) &&
475             !test_bit(dep[i][1], model->features)) {
476             warn_report("\'%s\' requires \'%s\'.",
477                         s390_feat_def(dep[i][0])->name,
478                         s390_feat_def(dep[i][1])->name);
479         }
480     }
481 }
482 
483 static void error_prepend_missing_feat(const char *name, void *opaque)
484 {
485     error_prepend((Error **) opaque, "%s ", name);
486 }
487 
488 static void check_compatibility(const S390CPUModel *max_model,
489                                 const S390CPUModel *model, Error **errp)
490 {
491     S390FeatBitmap missing;
492 
493     if (model->def->gen > max_model->def->gen) {
494         error_setg(errp, "Selected CPU generation is too new. Maximum "
495                    "supported model in the configuration: \'%s\'",
496                    max_model->def->name);
497         return;
498     } else if (model->def->gen == max_model->def->gen &&
499                model->def->ec_ga > max_model->def->ec_ga) {
500         error_setg(errp, "Selected CPU GA level is too new. Maximum "
501                    "supported model in the configuration: \'%s\'",
502                    max_model->def->name);
503         return;
504     }
505 
506 #ifndef CONFIG_USER_ONLY
507     if (only_migratable && test_bit(S390_FEAT_UNPACK, model->features)) {
508         error_setg(errp, "The unpack facility is not compatible with "
509                    "the --only-migratable option. You must remove either "
510                    "the 'unpack' facility or the --only-migratable option");
511         return;
512     }
513 #endif
514 
515     /* detect the missing features to properly report them */
516     bitmap_andnot(missing, model->features, max_model->features, S390_FEAT_MAX);
517     if (bitmap_empty(missing, S390_FEAT_MAX)) {
518         return;
519     }
520 
521     error_setg(errp, " ");
522     s390_feat_bitmap_to_ascii(missing, errp, error_prepend_missing_feat);
523     error_prepend(errp, "Some features requested in the CPU model are not "
524                   "available in the configuration: ");
525 }
526 
527 S390CPUModel *get_max_cpu_model(Error **errp)
528 {
529     Error *err = NULL;
530     static S390CPUModel max_model;
531     static bool cached;
532 
533     if (cached) {
534         return &max_model;
535     }
536 
537     if (kvm_enabled()) {
538         kvm_s390_get_host_cpu_model(&max_model, &err);
539     } else {
540         max_model.def = s390_find_cpu_def(QEMU_MAX_CPU_TYPE, QEMU_MAX_CPU_GEN,
541                                           QEMU_MAX_CPU_EC_GA, NULL);
542         bitmap_copy(max_model.features, qemu_max_cpu_feat, S390_FEAT_MAX);
543     }
544     if (err) {
545         error_propagate(errp, err);
546         return NULL;
547     }
548     cached = true;
549     return &max_model;
550 }
551 
552 void s390_realize_cpu_model(CPUState *cs, Error **errp)
553 {
554     Error *err = NULL;
555     S390CPUClass *xcc = S390_CPU_GET_CLASS(cs);
556     S390CPU *cpu = S390_CPU(cs);
557     const S390CPUModel *max_model;
558 
559     if (xcc->kvm_required && !kvm_enabled()) {
560         error_setg(errp, "CPU definition requires KVM");
561         return;
562     }
563 
564     if (!cpu->model) {
565         /* no host model support -> perform compatibility stuff */
566         apply_cpu_model(NULL, errp);
567         return;
568     }
569 
570     max_model = get_max_cpu_model(errp);
571     if (!max_model) {
572         error_prepend(errp, "CPU models are not available: ");
573         return;
574     }
575 
576     /* copy over properties that can vary */
577     cpu->model->lowest_ibc = max_model->lowest_ibc;
578     cpu->model->cpu_id = max_model->cpu_id;
579     cpu->model->cpu_id_format = max_model->cpu_id_format;
580     cpu->model->cpu_ver = max_model->cpu_ver;
581 
582     check_consistency(cpu->model);
583     check_compatibility(max_model, cpu->model, &err);
584     if (err) {
585         error_propagate(errp, err);
586         return;
587     }
588 
589     apply_cpu_model(cpu->model, errp);
590 
591 #if !defined(CONFIG_USER_ONLY)
592     cpu->env.cpuid = s390_cpuid_from_cpu_model(cpu->model);
593     if (tcg_enabled()) {
594         /* basic mode, write the cpu address into the first 4 bit of the ID */
595         cpu->env.cpuid = deposit64(cpu->env.cpuid, 54, 4, cpu->env.core_id);
596     }
597 #endif
598 }
599 
600 static void get_feature(Object *obj, Visitor *v, const char *name,
601                         void *opaque, Error **errp)
602 {
603     S390Feat feat = (S390Feat) (uintptr_t) opaque;
604     S390CPU *cpu = S390_CPU(obj);
605     bool value;
606 
607     if (!cpu->model) {
608         error_setg(errp, "Details about the host CPU model are not available, "
609                          "features cannot be queried.");
610         return;
611     }
612 
613     value = test_bit(feat, cpu->model->features);
614     visit_type_bool(v, name, &value, errp);
615 }
616 
617 static void set_feature(Object *obj, Visitor *v, const char *name,
618                         void *opaque, Error **errp)
619 {
620     S390Feat feat = (S390Feat) (uintptr_t) opaque;
621     DeviceState *dev = DEVICE(obj);
622     S390CPU *cpu = S390_CPU(obj);
623     bool value;
624 
625     if (dev->realized) {
626         error_setg(errp, "Attempt to set property '%s' on '%s' after "
627                    "it was realized", name, object_get_typename(obj));
628         return;
629     } else if (!cpu->model) {
630         error_setg(errp, "Details about the host CPU model are not available, "
631                          "features cannot be changed.");
632         return;
633     }
634 
635     if (!visit_type_bool(v, name, &value, errp)) {
636         return;
637     }
638     if (value) {
639         if (!test_bit(feat, cpu->model->def->full_feat)) {
640             error_setg(errp, "Feature '%s' is not available for CPU model '%s',"
641                        " it was introduced with later models.",
642                        name, cpu->model->def->name);
643             return;
644         }
645         set_bit(feat, cpu->model->features);
646     } else {
647         clear_bit(feat, cpu->model->features);
648     }
649 }
650 
651 static void get_feature_group(Object *obj, Visitor *v, const char *name,
652                               void *opaque, Error **errp)
653 {
654     S390FeatGroup group = (S390FeatGroup) (uintptr_t) opaque;
655     const S390FeatGroupDef *def = s390_feat_group_def(group);
656     S390CPU *cpu = S390_CPU(obj);
657     S390FeatBitmap tmp;
658     bool value;
659 
660     if (!cpu->model) {
661         error_setg(errp, "Details about the host CPU model are not available, "
662                          "features cannot be queried.");
663         return;
664     }
665 
666     /* a group is enabled if all features are enabled */
667     bitmap_and(tmp, cpu->model->features, def->feat, S390_FEAT_MAX);
668     value = bitmap_equal(tmp, def->feat, S390_FEAT_MAX);
669     visit_type_bool(v, name, &value, errp);
670 }
671 
672 static void set_feature_group(Object *obj, Visitor *v, const char *name,
673                               void *opaque, Error **errp)
674 {
675     S390FeatGroup group = (S390FeatGroup) (uintptr_t) opaque;
676     const S390FeatGroupDef *def = s390_feat_group_def(group);
677     DeviceState *dev = DEVICE(obj);
678     S390CPU *cpu = S390_CPU(obj);
679     bool value;
680 
681     if (dev->realized) {
682         error_setg(errp, "Attempt to set property '%s' on '%s' after "
683                    "it was realized", name, object_get_typename(obj));
684         return;
685     } else if (!cpu->model) {
686         error_setg(errp, "Details about the host CPU model are not available, "
687                          "features cannot be changed.");
688         return;
689     }
690 
691     if (!visit_type_bool(v, name, &value, errp)) {
692         return;
693     }
694     if (value) {
695         /* groups are added in one shot, so an intersect is sufficient */
696         if (!bitmap_intersects(def->feat, cpu->model->def->full_feat,
697                                S390_FEAT_MAX)) {
698             error_setg(errp, "Group '%s' is not available for CPU model '%s',"
699                        " it was introduced with later models.",
700                        name, cpu->model->def->name);
701             return;
702         }
703         bitmap_or(cpu->model->features, cpu->model->features, def->feat,
704                   S390_FEAT_MAX);
705     } else {
706         bitmap_andnot(cpu->model->features, cpu->model->features, def->feat,
707                       S390_FEAT_MAX);
708     }
709 }
710 
711 static void s390_cpu_model_initfn(Object *obj)
712 {
713     S390CPU *cpu = S390_CPU(obj);
714     S390CPUClass *xcc = S390_CPU_GET_CLASS(cpu);
715 
716     cpu->model = g_malloc0(sizeof(*cpu->model));
717     /* copy the model, so we can modify it */
718     cpu->model->def = xcc->cpu_def;
719     if (xcc->is_static) {
720         /* base model - features will never change */
721         bitmap_copy(cpu->model->features, cpu->model->def->base_feat,
722                     S390_FEAT_MAX);
723     } else {
724         /* latest model - features can change */
725         bitmap_copy(cpu->model->features,
726                     cpu->model->def->default_feat, S390_FEAT_MAX);
727     }
728 }
729 
730 static S390CPUDef s390_qemu_cpu_def;
731 static S390CPUModel s390_qemu_cpu_model;
732 
733 /* Set the qemu CPU model (on machine initialization). Must not be called
734  * once CPUs have been created.
735  */
736 void s390_set_qemu_cpu_model(uint16_t type, uint8_t gen, uint8_t ec_ga,
737                              const S390FeatInit feat_init)
738 {
739     const S390CPUDef *def = s390_find_cpu_def(type, gen, ec_ga, NULL);
740 
741     g_assert(def);
742     g_assert(QTAILQ_EMPTY_RCU(&cpus));
743 
744     /* TCG emulates some features that can usually not be enabled with
745      * the emulated machine generation. Make sure they can be enabled
746      * when using the QEMU model by adding them to full_feat. We have
747      * to copy the definition to do that.
748      */
749     memcpy(&s390_qemu_cpu_def, def, sizeof(s390_qemu_cpu_def));
750     bitmap_or(s390_qemu_cpu_def.full_feat, s390_qemu_cpu_def.full_feat,
751               qemu_max_cpu_feat, S390_FEAT_MAX);
752 
753     /* build the CPU model */
754     s390_qemu_cpu_model.def = &s390_qemu_cpu_def;
755     bitmap_zero(s390_qemu_cpu_model.features, S390_FEAT_MAX);
756     s390_init_feat_bitmap(feat_init, s390_qemu_cpu_model.features);
757 }
758 
759 static void s390_qemu_cpu_model_initfn(Object *obj)
760 {
761     S390CPU *cpu = S390_CPU(obj);
762 
763     cpu->model = g_malloc0(sizeof(*cpu->model));
764     /* copy the CPU model so we can modify it */
765     memcpy(cpu->model, &s390_qemu_cpu_model, sizeof(*cpu->model));
766 }
767 
768 static void s390_max_cpu_model_initfn(Object *obj)
769 {
770     const S390CPUModel *max_model;
771     S390CPU *cpu = S390_CPU(obj);
772     Error *local_err = NULL;
773 
774     if (kvm_enabled() && !kvm_s390_cpu_models_supported()) {
775         /* "max" and "host" always work, even without CPU model support */
776         return;
777     }
778 
779     max_model = get_max_cpu_model(&local_err);
780     if (local_err) {
781         /* we expect errors only under KVM, when actually querying the kernel */
782         g_assert(kvm_enabled());
783         error_report_err(local_err);
784         /* fallback to unsupported CPU models */
785         return;
786     }
787 
788     cpu->model = g_new(S390CPUModel, 1);
789     /* copy the CPU model so we can modify it */
790     memcpy(cpu->model, max_model, sizeof(*cpu->model));
791 }
792 
793 static void s390_cpu_model_finalize(Object *obj)
794 {
795     S390CPU *cpu = S390_CPU(obj);
796 
797     g_free(cpu->model);
798     cpu->model = NULL;
799 }
800 
801 static bool get_is_migration_safe(Object *obj, Error **errp)
802 {
803     return S390_CPU_GET_CLASS(obj)->is_migration_safe;
804 }
805 
806 static bool get_is_static(Object *obj, Error **errp)
807 {
808     return S390_CPU_GET_CLASS(obj)->is_static;
809 }
810 
811 static char *get_description(Object *obj, Error **errp)
812 {
813     return g_strdup(S390_CPU_GET_CLASS(obj)->desc);
814 }
815 
816 void s390_cpu_model_class_register_props(ObjectClass *oc)
817 {
818     S390FeatGroup group;
819     S390Feat feat;
820 
821     object_class_property_add_bool(oc, "migration-safe", get_is_migration_safe,
822                                    NULL);
823     object_class_property_add_bool(oc, "static", get_is_static,
824                                    NULL);
825     object_class_property_add_str(oc, "description", get_description, NULL);
826 
827     for (feat = 0; feat < S390_FEAT_MAX; feat++) {
828         const S390FeatDef *def = s390_feat_def(feat);
829         object_class_property_add(oc, def->name, "bool", get_feature,
830                                   set_feature, NULL, (void *) feat);
831         object_class_property_set_description(oc, def->name, def->desc);
832     }
833     for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
834         const S390FeatGroupDef *def = s390_feat_group_def(group);
835         object_class_property_add(oc, def->name, "bool", get_feature_group,
836                                   set_feature_group, NULL, (void *) group);
837         object_class_property_set_description(oc, def->name, def->desc);
838     }
839 }
840 
841 #ifdef CONFIG_KVM
842 static void s390_host_cpu_model_class_init(ObjectClass *oc, void *data)
843 {
844     S390CPUClass *xcc = S390_CPU_CLASS(oc);
845 
846     xcc->kvm_required = true;
847     xcc->desc = "KVM only: All recognized features";
848 }
849 #endif
850 
851 static void s390_base_cpu_model_class_init(ObjectClass *oc, void *data)
852 {
853     S390CPUClass *xcc = S390_CPU_CLASS(oc);
854 
855     /* all base models are migration safe */
856     xcc->cpu_def = (const S390CPUDef *) data;
857     xcc->is_migration_safe = true;
858     xcc->is_static = true;
859     xcc->desc = xcc->cpu_def->desc;
860 }
861 
862 static void s390_cpu_model_class_init(ObjectClass *oc, void *data)
863 {
864     S390CPUClass *xcc = S390_CPU_CLASS(oc);
865 
866     /* model that can change between QEMU versions */
867     xcc->cpu_def = (const S390CPUDef *) data;
868     xcc->is_migration_safe = true;
869     xcc->desc = xcc->cpu_def->desc;
870 }
871 
872 static void s390_qemu_cpu_model_class_init(ObjectClass *oc, void *data)
873 {
874     S390CPUClass *xcc = S390_CPU_CLASS(oc);
875 
876     xcc->is_migration_safe = true;
877     xcc->desc = g_strdup_printf("QEMU Virtual CPU version %s",
878                                 qemu_hw_version());
879 }
880 
881 static void s390_max_cpu_model_class_init(ObjectClass *oc, void *data)
882 {
883     S390CPUClass *xcc = S390_CPU_CLASS(oc);
884 
885     /*
886      * The "max" model is neither static nor migration safe. Under KVM
887      * it represents the "host" model. Under TCG it represents some kind of
888      * "qemu" CPU model without compat handling and maybe with some additional
889      * CPU features that are not yet unlocked in the "qemu" model.
890      */
891     xcc->desc =
892         "Enables all features supported by the accelerator in the current host";
893 }
894 
895 /* Generate type name for a cpu model. Caller has to free the string. */
896 static char *s390_cpu_type_name(const char *model_name)
897 {
898     return g_strdup_printf(S390_CPU_TYPE_NAME("%s"), model_name);
899 }
900 
901 /* Generate type name for a base cpu model. Caller has to free the string. */
902 static char *s390_base_cpu_type_name(const char *model_name)
903 {
904     return g_strdup_printf(S390_CPU_TYPE_NAME("%s-base"), model_name);
905 }
906 
907 ObjectClass *s390_cpu_class_by_name(const char *name)
908 {
909     char *typename = s390_cpu_type_name(name);
910     ObjectClass *oc;
911 
912     oc = object_class_by_name(typename);
913     g_free(typename);
914     return oc;
915 }
916 
917 static const TypeInfo qemu_s390_cpu_type_info = {
918     .name = S390_CPU_TYPE_NAME("qemu"),
919     .parent = TYPE_S390_CPU,
920     .instance_init = s390_qemu_cpu_model_initfn,
921     .instance_finalize = s390_cpu_model_finalize,
922     .class_init = s390_qemu_cpu_model_class_init,
923 };
924 
925 static const TypeInfo max_s390_cpu_type_info = {
926     .name = S390_CPU_TYPE_NAME("max"),
927     .parent = TYPE_S390_CPU,
928     .instance_init = s390_max_cpu_model_initfn,
929     .instance_finalize = s390_cpu_model_finalize,
930     .class_init = s390_max_cpu_model_class_init,
931 };
932 
933 #ifdef CONFIG_KVM
934 static const TypeInfo host_s390_cpu_type_info = {
935     .name = S390_CPU_TYPE_NAME("host"),
936     .parent = S390_CPU_TYPE_NAME("max"),
937     .class_init = s390_host_cpu_model_class_init,
938 };
939 #endif
940 
941 static void init_ignored_base_feat(void)
942 {
943     static const int feats[] = {
944          /* MSA subfunctions that could not be available on certain machines */
945          S390_FEAT_KMAC_DEA,
946          S390_FEAT_KMAC_TDEA_128,
947          S390_FEAT_KMAC_TDEA_192,
948          S390_FEAT_KMC_DEA,
949          S390_FEAT_KMC_TDEA_128,
950          S390_FEAT_KMC_TDEA_192,
951          S390_FEAT_KM_DEA,
952          S390_FEAT_KM_TDEA_128,
953          S390_FEAT_KM_TDEA_192,
954          S390_FEAT_KIMD_SHA_1,
955          S390_FEAT_KLMD_SHA_1,
956          /* CSSKE is deprecated on newer generations */
957          S390_FEAT_CONDITIONAL_SSKE,
958     };
959     int i;
960 
961     for (i = 0; i < ARRAY_SIZE(feats); i++) {
962         set_bit(feats[i], ignored_base_feat);
963     }
964 }
965 
966 static void register_types(void)
967 {
968     static const S390FeatInit qemu_latest_init = { S390_FEAT_LIST_QEMU_LATEST };
969     int i;
970 
971     init_ignored_base_feat();
972 
973     /* init all bitmaps from gnerated data initially */
974     s390_init_feat_bitmap(qemu_max_cpu_feat_init, qemu_max_cpu_feat);
975     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
976         s390_init_feat_bitmap(s390_cpu_defs[i].base_init,
977                               s390_cpu_defs[i].base_feat);
978         s390_init_feat_bitmap(s390_cpu_defs[i].default_init,
979                               s390_cpu_defs[i].default_feat);
980         s390_init_feat_bitmap(s390_cpu_defs[i].full_init,
981                               s390_cpu_defs[i].full_feat);
982     }
983 
984     /* initialize the qemu model with latest definition */
985     s390_set_qemu_cpu_model(QEMU_MAX_CPU_TYPE, QEMU_MAX_CPU_GEN,
986                             QEMU_MAX_CPU_EC_GA, qemu_latest_init);
987 
988     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
989         char *base_name = s390_base_cpu_type_name(s390_cpu_defs[i].name);
990         TypeInfo ti_base = {
991             .name = base_name,
992             .parent = TYPE_S390_CPU,
993             .instance_init = s390_cpu_model_initfn,
994             .instance_finalize = s390_cpu_model_finalize,
995             .class_init = s390_base_cpu_model_class_init,
996             .class_data = (void *) &s390_cpu_defs[i],
997         };
998         char *name = s390_cpu_type_name(s390_cpu_defs[i].name);
999         TypeInfo ti = {
1000             .name = name,
1001             .parent = TYPE_S390_CPU,
1002             .instance_init = s390_cpu_model_initfn,
1003             .instance_finalize = s390_cpu_model_finalize,
1004             .class_init = s390_cpu_model_class_init,
1005             .class_data = (void *) &s390_cpu_defs[i],
1006         };
1007 
1008         type_register_static(&ti_base);
1009         type_register_static(&ti);
1010         g_free(base_name);
1011         g_free(name);
1012     }
1013 
1014     type_register_static(&qemu_s390_cpu_type_info);
1015     type_register_static(&max_s390_cpu_type_info);
1016 #ifdef CONFIG_KVM
1017     type_register_static(&host_s390_cpu_type_info);
1018 #endif
1019 }
1020 
1021 type_init(register_types)
1022