xref: /openbmc/qemu/target/s390x/cpu_features.c (revision f101c9fe)
1 /*
2  * CPU features/facilities for s390x
3  *
4  * Copyright IBM Corp. 2016, 2018
5  * Copyright Red Hat, Inc. 2019
6  *
7  * Author(s): David Hildenbrand <david@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or (at
10  * your option) any later version. See the COPYING file in the top-level
11  * directory.
12  */
13 
14 #include "qemu/osdep.h"
15 #include "qemu/module.h"
16 #include "cpu_features.h"
17 #include "hw/s390x/pv.h"
18 
19 #define DEF_FEAT(_FEAT, _NAME, _TYPE, _BIT, _DESC) \
20     [S390_FEAT_##_FEAT] = {                        \
21         .name = _NAME,                             \
22         .type = S390_FEAT_TYPE_##_TYPE,            \
23         .bit = _BIT,                               \
24         .desc = _DESC,                             \
25     },
26 static const S390FeatDef s390_features[S390_FEAT_MAX] = {
27     #include "cpu_features_def.h.inc"
28 };
29 #undef DEF_FEAT
30 
31 const S390FeatDef *s390_feat_def(S390Feat feat)
32 {
33     return &s390_features[feat];
34 }
35 
36 S390Feat s390_feat_by_type_and_bit(S390FeatType type, int bit)
37 {
38     S390Feat feat;
39 
40     for (feat = 0; feat < ARRAY_SIZE(s390_features); feat++) {
41         if (s390_features[feat].type == type &&
42             s390_features[feat].bit == bit) {
43             return feat;
44         }
45     }
46     return S390_FEAT_MAX;
47 }
48 
49 void s390_init_feat_bitmap(const S390FeatInit init, S390FeatBitmap bitmap)
50 {
51     int i, j;
52 
53     for (i = 0; i < (S390_FEAT_MAX / 64 + 1); i++) {
54         if (init[i]) {
55             for (j = 0; j < 64; j++) {
56                 if (init[i] & 1ULL << j) {
57                     set_bit(i * 64 + j, bitmap);
58                 }
59             }
60         }
61     }
62 }
63 
64 void s390_fill_feat_block(const S390FeatBitmap features, S390FeatType type,
65                           uint8_t *data)
66 {
67     S390Feat feat;
68     int bit_nr;
69 
70     switch (type) {
71     case S390_FEAT_TYPE_STFL:
72         if (test_bit(S390_FEAT_ZARCH, features)) {
73             /* Features that are always active */
74             set_be_bit(2, data);   /* z/Architecture */
75             set_be_bit(138, data); /* Configuration-z-architectural-mode */
76         }
77         break;
78     case S390_FEAT_TYPE_PTFF:
79     case S390_FEAT_TYPE_KMAC:
80     case S390_FEAT_TYPE_KMC:
81     case S390_FEAT_TYPE_KM:
82     case S390_FEAT_TYPE_KIMD:
83     case S390_FEAT_TYPE_KLMD:
84     case S390_FEAT_TYPE_PCKMO:
85     case S390_FEAT_TYPE_KMCTR:
86     case S390_FEAT_TYPE_KMF:
87     case S390_FEAT_TYPE_KMO:
88     case S390_FEAT_TYPE_PCC:
89     case S390_FEAT_TYPE_PPNO:
90     case S390_FEAT_TYPE_KMA:
91     case S390_FEAT_TYPE_KDSA:
92     case S390_FEAT_TYPE_SORTL:
93     case S390_FEAT_TYPE_DFLTCC:
94         set_be_bit(0, data); /* query is always available */
95         break;
96     default:
97         break;
98     };
99 
100     feat = find_first_bit(features, S390_FEAT_MAX);
101     while (feat < S390_FEAT_MAX) {
102         if (s390_features[feat].type == type) {
103             bit_nr = s390_features[feat].bit;
104             /* big endian on uint8_t array */
105             set_be_bit(bit_nr, data);
106         }
107         feat = find_next_bit(features, S390_FEAT_MAX, feat + 1);
108     }
109 
110     if (!s390_is_pv()) {
111         return;
112     }
113 
114     /*
115      * Some facilities are not available for CPUs in protected mode:
116      * - All SIE facilities because SIE is not available
117      * - DIAG318
118      *
119      * As VMs can move in and out of protected mode the CPU model
120      * doesn't protect us from that problem because it is only
121      * validated at the start of the VM.
122      */
123     switch (type) {
124     case S390_FEAT_TYPE_SCLP_CPU:
125         clear_be_bit(s390_feat_def(S390_FEAT_SIE_F2)->bit, data);
126         clear_be_bit(s390_feat_def(S390_FEAT_SIE_SKEY)->bit, data);
127         clear_be_bit(s390_feat_def(S390_FEAT_SIE_GPERE)->bit, data);
128         clear_be_bit(s390_feat_def(S390_FEAT_SIE_SIIF)->bit, data);
129         clear_be_bit(s390_feat_def(S390_FEAT_SIE_SIGPIF)->bit, data);
130         clear_be_bit(s390_feat_def(S390_FEAT_SIE_IB)->bit, data);
131         clear_be_bit(s390_feat_def(S390_FEAT_SIE_CEI)->bit, data);
132         break;
133     case S390_FEAT_TYPE_SCLP_CONF_CHAR:
134         clear_be_bit(s390_feat_def(S390_FEAT_SIE_GSLS)->bit, data);
135         clear_be_bit(s390_feat_def(S390_FEAT_HPMA2)->bit, data);
136         clear_be_bit(s390_feat_def(S390_FEAT_SIE_KSS)->bit, data);
137         break;
138     case S390_FEAT_TYPE_SCLP_CONF_CHAR_EXT:
139         clear_be_bit(s390_feat_def(S390_FEAT_SIE_64BSCAO)->bit, data);
140         clear_be_bit(s390_feat_def(S390_FEAT_SIE_CMMA)->bit, data);
141         clear_be_bit(s390_feat_def(S390_FEAT_SIE_PFMFI)->bit, data);
142         clear_be_bit(s390_feat_def(S390_FEAT_SIE_IBS)->bit, data);
143         break;
144     case S390_FEAT_TYPE_SCLP_FAC134:
145         clear_be_bit(s390_feat_def(S390_FEAT_DIAG_318)->bit, data);
146         break;
147     default:
148         return;
149     }
150 }
151 
152 void s390_add_from_feat_block(S390FeatBitmap features, S390FeatType type,
153                               uint8_t *data)
154 {
155     int nr_bits, le_bit;
156 
157     switch (type) {
158     case S390_FEAT_TYPE_STFL:
159        nr_bits = 16384;
160        break;
161     case S390_FEAT_TYPE_PLO:
162     case S390_FEAT_TYPE_SORTL:
163     case S390_FEAT_TYPE_DFLTCC:
164        nr_bits = 256;
165        break;
166     default:
167        /* all cpu subfunctions have 128 bit */
168        nr_bits = 128;
169     };
170 
171     le_bit = find_first_bit((unsigned long *) data, nr_bits);
172     while (le_bit < nr_bits) {
173         /* convert the bit number to a big endian bit nr */
174         S390Feat feat = s390_feat_by_type_and_bit(type, BE_BIT_NR(le_bit));
175         /* ignore unknown bits */
176         if (feat < S390_FEAT_MAX) {
177             set_bit(feat, features);
178         }
179         le_bit = find_next_bit((unsigned long *) data, nr_bits, le_bit + 1);
180     }
181 }
182 
183 void s390_feat_bitmap_to_ascii(const S390FeatBitmap features, void *opaque,
184                                void (*fn)(const char *name, void *opaque))
185 {
186     S390FeatBitmap bitmap, tmp;
187     S390FeatGroup group;
188     S390Feat feat;
189 
190     bitmap_copy(bitmap, features, S390_FEAT_MAX);
191 
192     /* process whole groups first */
193     for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
194         const S390FeatGroupDef *def = s390_feat_group_def(group);
195 
196         bitmap_and(tmp, bitmap, def->feat, S390_FEAT_MAX);
197         if (bitmap_equal(tmp, def->feat, S390_FEAT_MAX)) {
198             bitmap_andnot(bitmap, bitmap, def->feat, S390_FEAT_MAX);
199             fn(def->name, opaque);
200         }
201     }
202 
203     /* report leftovers as separate features */
204     feat = find_first_bit(bitmap, S390_FEAT_MAX);
205     while (feat < S390_FEAT_MAX) {
206         fn(s390_feat_def(feat)->name, opaque);
207         feat = find_next_bit(bitmap, S390_FEAT_MAX, feat + 1);
208     };
209 }
210 
211 #define FEAT_GROUP_INIT(_name, _group, _desc)        \
212     {                                                \
213         .name = _name,                               \
214         .desc = _desc,                               \
215         .init = { S390_FEAT_GROUP_LIST_ ## _group }, \
216     }
217 
218 /* indexed by feature group number for easy lookup */
219 static S390FeatGroupDef s390_feature_groups[] = {
220     FEAT_GROUP_INIT("plo", PLO, "Perform-locked-operation facility"),
221     FEAT_GROUP_INIT("tods", TOD_CLOCK_STEERING, "Tod-clock-steering facility"),
222     FEAT_GROUP_INIT("gen13ptff", GEN13_PTFF, "PTFF enhancements introduced with z13"),
223     FEAT_GROUP_INIT("msa", MSA, "Message-security-assist facility"),
224     FEAT_GROUP_INIT("msa1", MSA_EXT_1, "Message-security-assist-extension 1 facility"),
225     FEAT_GROUP_INIT("msa2", MSA_EXT_2, "Message-security-assist-extension 2 facility"),
226     FEAT_GROUP_INIT("msa3", MSA_EXT_3, "Message-security-assist-extension 3 facility"),
227     FEAT_GROUP_INIT("msa4", MSA_EXT_4, "Message-security-assist-extension 4 facility"),
228     FEAT_GROUP_INIT("msa5", MSA_EXT_5, "Message-security-assist-extension 5 facility"),
229     FEAT_GROUP_INIT("msa6", MSA_EXT_6, "Message-security-assist-extension 6 facility"),
230     FEAT_GROUP_INIT("msa7", MSA_EXT_7, "Message-security-assist-extension 7 facility"),
231     FEAT_GROUP_INIT("msa8", MSA_EXT_8, "Message-security-assist-extension 8 facility"),
232     FEAT_GROUP_INIT("msa9", MSA_EXT_9, "Message-security-assist-extension 9 facility"),
233     FEAT_GROUP_INIT("msa9_pckmo", MSA_EXT_9_PCKMO, "Message-security-assist-extension 9 PCKMO subfunctions"),
234     FEAT_GROUP_INIT("mepochptff", MULTIPLE_EPOCH_PTFF, "PTFF enhancements introduced with Multiple-epoch facility"),
235     FEAT_GROUP_INIT("esort", ENH_SORT, "Enhanced-sort facility"),
236     FEAT_GROUP_INIT("deflate", DEFLATE_CONVERSION, "Deflate-conversion facility"),
237 };
238 
239 const S390FeatGroupDef *s390_feat_group_def(S390FeatGroup group)
240 {
241     return &s390_feature_groups[group];
242 }
243 
244 static void init_groups(void)
245 {
246     int i;
247 
248     /* init all bitmaps from gnerated data initially */
249     for (i = 0; i < ARRAY_SIZE(s390_feature_groups); i++) {
250         s390_init_feat_bitmap(s390_feature_groups[i].init,
251                               s390_feature_groups[i].feat);
252     }
253 }
254 
255 type_init(init_groups)
256