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