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