xref: /qemu/target/s390x/cpu_features.c (revision 0979ed017f093ead3c011513a3a79517ed75bd00)
178241744SMichael Mueller /*
278241744SMichael Mueller  * CPU features/facilities for s390x
378241744SMichael Mueller  *
427e84d4eSChristian Borntraeger  * Copyright IBM Corp. 2016, 2018
5220ae900SDavid Hildenbrand  * Copyright Red Hat, Inc. 2019
678241744SMichael Mueller  *
7220ae900SDavid Hildenbrand  * Author(s): David Hildenbrand <david@redhat.com>
878241744SMichael Mueller  *
978241744SMichael Mueller  * This work is licensed under the terms of the GNU GPL, version 2 or (at
1078241744SMichael Mueller  * your option) any later version. See the COPYING file in the top-level
1178241744SMichael Mueller  * directory.
1278241744SMichael Mueller  */
1378241744SMichael Mueller 
1478241744SMichael Mueller #include "qemu/osdep.h"
158b3d6cb1SDavid Hildenbrand #include "qemu/module.h"
1678241744SMichael Mueller #include "cpu_features.h"
1778241744SMichael Mueller 
18220ae900SDavid Hildenbrand #define DEF_FEAT(_FEAT, _NAME, _TYPE, _BIT, _DESC) \
19220ae900SDavid Hildenbrand     [S390_FEAT_##_FEAT] = {                        \
20220ae900SDavid Hildenbrand         .name = _NAME,                             \
21220ae900SDavid Hildenbrand         .type = S390_FEAT_TYPE_##_TYPE,            \
22220ae900SDavid Hildenbrand         .bit = _BIT,                               \
23220ae900SDavid Hildenbrand         .desc = _DESC,                             \
24220ae900SDavid Hildenbrand     },
25220ae900SDavid Hildenbrand static const S390FeatDef s390_features[S390_FEAT_MAX] = {
26*0979ed01SPaolo Bonzini     #include "cpu_features_def.h.inc"
2778241744SMichael Mueller };
28220ae900SDavid Hildenbrand #undef DEF_FEAT
2978241744SMichael Mueller 
3078241744SMichael Mueller const S390FeatDef *s390_feat_def(S390Feat feat)
3178241744SMichael Mueller {
3278241744SMichael Mueller     return &s390_features[feat];
3378241744SMichael Mueller }
3478241744SMichael Mueller 
3578241744SMichael Mueller S390Feat s390_feat_by_type_and_bit(S390FeatType type, int bit)
3678241744SMichael Mueller {
3778241744SMichael Mueller     S390Feat feat;
3878241744SMichael Mueller 
3978241744SMichael Mueller     for (feat = 0; feat < ARRAY_SIZE(s390_features); feat++) {
4078241744SMichael Mueller         if (s390_features[feat].type == type &&
4178241744SMichael Mueller             s390_features[feat].bit == bit) {
4278241744SMichael Mueller             return feat;
4378241744SMichael Mueller         }
4478241744SMichael Mueller     }
4578241744SMichael Mueller     return S390_FEAT_MAX;
4678241744SMichael Mueller }
4778241744SMichael Mueller 
4878241744SMichael Mueller void s390_init_feat_bitmap(const S390FeatInit init, S390FeatBitmap bitmap)
4978241744SMichael Mueller {
5078241744SMichael Mueller     int i, j;
5178241744SMichael Mueller 
5278241744SMichael Mueller     for (i = 0; i < (S390_FEAT_MAX / 64 + 1); i++) {
5378241744SMichael Mueller         if (init[i]) {
5478241744SMichael Mueller             for (j = 0; j < 64; j++) {
5578241744SMichael Mueller                 if (init[i] & 1ULL << j) {
5678241744SMichael Mueller                     set_bit(i * 64 + j, bitmap);
5778241744SMichael Mueller                 }
5878241744SMichael Mueller             }
5978241744SMichael Mueller         }
6078241744SMichael Mueller     }
6178241744SMichael Mueller }
6278241744SMichael Mueller 
6378241744SMichael Mueller void s390_fill_feat_block(const S390FeatBitmap features, S390FeatType type,
6478241744SMichael Mueller                           uint8_t *data)
6578241744SMichael Mueller {
6678241744SMichael Mueller     S390Feat feat;
6778241744SMichael Mueller     int bit_nr;
6878241744SMichael Mueller 
69cc18f907SDavid Hildenbrand     switch (type) {
70cc18f907SDavid Hildenbrand     case S390_FEAT_TYPE_STFL:
71cc18f907SDavid Hildenbrand         if (test_bit(S390_FEAT_ZARCH, features)) {
72075e52b8SJason J. Herne             /* Features that are always active */
733d1cfc3cSDavid Hildenbrand             set_be_bit(2, data);   /* z/Architecture */
743d1cfc3cSDavid Hildenbrand             set_be_bit(138, data); /* Configuration-z-architectural-mode */
7578241744SMichael Mueller         }
76cc18f907SDavid Hildenbrand         break;
77cc18f907SDavid Hildenbrand     case S390_FEAT_TYPE_PTFF:
78cc18f907SDavid Hildenbrand     case S390_FEAT_TYPE_KMAC:
79cc18f907SDavid Hildenbrand     case S390_FEAT_TYPE_KMC:
80cc18f907SDavid Hildenbrand     case S390_FEAT_TYPE_KM:
81cc18f907SDavid Hildenbrand     case S390_FEAT_TYPE_KIMD:
82cc18f907SDavid Hildenbrand     case S390_FEAT_TYPE_KLMD:
83cc18f907SDavid Hildenbrand     case S390_FEAT_TYPE_PCKMO:
84cc18f907SDavid Hildenbrand     case S390_FEAT_TYPE_KMCTR:
85cc18f907SDavid Hildenbrand     case S390_FEAT_TYPE_KMF:
86cc18f907SDavid Hildenbrand     case S390_FEAT_TYPE_KMO:
87cc18f907SDavid Hildenbrand     case S390_FEAT_TYPE_PCC:
88cc18f907SDavid Hildenbrand     case S390_FEAT_TYPE_PPNO:
89cc18f907SDavid Hildenbrand     case S390_FEAT_TYPE_KMA:
905dacbe23SChristian Borntraeger     case S390_FEAT_TYPE_KDSA:
91d220fabfSChristian Borntraeger     case S390_FEAT_TYPE_SORTL:
92afc7b866SChristian Borntraeger     case S390_FEAT_TYPE_DFLTCC:
933d1cfc3cSDavid Hildenbrand         set_be_bit(0, data); /* query is always available */
94cc18f907SDavid Hildenbrand         break;
95cc18f907SDavid Hildenbrand     default:
96cc18f907SDavid Hildenbrand         break;
97cc18f907SDavid Hildenbrand     };
9878241744SMichael Mueller 
9978241744SMichael Mueller     feat = find_first_bit(features, S390_FEAT_MAX);
10078241744SMichael Mueller     while (feat < S390_FEAT_MAX) {
10178241744SMichael Mueller         if (s390_features[feat].type == type) {
10278241744SMichael Mueller             bit_nr = s390_features[feat].bit;
10378241744SMichael Mueller             /* big endian on uint8_t array */
1043d1cfc3cSDavid Hildenbrand             set_be_bit(bit_nr, data);
10578241744SMichael Mueller         }
10678241744SMichael Mueller         feat = find_next_bit(features, S390_FEAT_MAX, feat + 1);
10778241744SMichael Mueller     }
10878241744SMichael Mueller }
10978241744SMichael Mueller 
11078241744SMichael Mueller void s390_add_from_feat_block(S390FeatBitmap features, S390FeatType type,
11178241744SMichael Mueller                               uint8_t *data)
11278241744SMichael Mueller {
11378241744SMichael Mueller     int nr_bits, le_bit;
11478241744SMichael Mueller 
11578241744SMichael Mueller     switch (type) {
11678241744SMichael Mueller     case S390_FEAT_TYPE_STFL:
117c547a757SDavid Hildenbrand        nr_bits = 16384;
11878241744SMichael Mueller        break;
11978241744SMichael Mueller     case S390_FEAT_TYPE_PLO:
120d220fabfSChristian Borntraeger     case S390_FEAT_TYPE_SORTL:
121afc7b866SChristian Borntraeger     case S390_FEAT_TYPE_DFLTCC:
12278241744SMichael Mueller        nr_bits = 256;
12378241744SMichael Mueller        break;
12478241744SMichael Mueller     default:
12578241744SMichael Mueller        /* all cpu subfunctions have 128 bit */
12678241744SMichael Mueller        nr_bits = 128;
12778241744SMichael Mueller     };
12878241744SMichael Mueller 
12978241744SMichael Mueller     le_bit = find_first_bit((unsigned long *) data, nr_bits);
13078241744SMichael Mueller     while (le_bit < nr_bits) {
13178241744SMichael Mueller         /* convert the bit number to a big endian bit nr */
13278241744SMichael Mueller         S390Feat feat = s390_feat_by_type_and_bit(type, BE_BIT_NR(le_bit));
13378241744SMichael Mueller         /* ignore unknown bits */
13478241744SMichael Mueller         if (feat < S390_FEAT_MAX) {
13578241744SMichael Mueller             set_bit(feat, features);
13678241744SMichael Mueller         }
13778241744SMichael Mueller         le_bit = find_next_bit((unsigned long *) data, nr_bits, le_bit + 1);
13878241744SMichael Mueller     }
13978241744SMichael Mueller }
14078241744SMichael Mueller 
1418b3d6cb1SDavid Hildenbrand void s390_feat_bitmap_to_ascii(const S390FeatBitmap features, void *opaque,
14278241744SMichael Mueller                                void (*fn)(const char *name, void *opaque))
14378241744SMichael Mueller {
1448b3d6cb1SDavid Hildenbrand     S390FeatBitmap bitmap, tmp;
1458b3d6cb1SDavid Hildenbrand     S390FeatGroup group;
14678241744SMichael Mueller     S390Feat feat;
14778241744SMichael Mueller 
1488b3d6cb1SDavid Hildenbrand     bitmap_copy(bitmap, features, S390_FEAT_MAX);
1498b3d6cb1SDavid Hildenbrand 
1508b3d6cb1SDavid Hildenbrand     /* process whole groups first */
1518b3d6cb1SDavid Hildenbrand     for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
1528b3d6cb1SDavid Hildenbrand         const S390FeatGroupDef *def = s390_feat_group_def(group);
1538b3d6cb1SDavid Hildenbrand 
1548b3d6cb1SDavid Hildenbrand         bitmap_and(tmp, bitmap, def->feat, S390_FEAT_MAX);
1558b3d6cb1SDavid Hildenbrand         if (bitmap_equal(tmp, def->feat, S390_FEAT_MAX)) {
1568b3d6cb1SDavid Hildenbrand             bitmap_andnot(bitmap, bitmap, def->feat, S390_FEAT_MAX);
1578b3d6cb1SDavid Hildenbrand             fn(def->name, opaque);
1588b3d6cb1SDavid Hildenbrand         }
1598b3d6cb1SDavid Hildenbrand     }
1608b3d6cb1SDavid Hildenbrand 
1618b3d6cb1SDavid Hildenbrand     /* report leftovers as separate features */
16278241744SMichael Mueller     feat = find_first_bit(bitmap, S390_FEAT_MAX);
16378241744SMichael Mueller     while (feat < S390_FEAT_MAX) {
16478241744SMichael Mueller         fn(s390_feat_def(feat)->name, opaque);
16578241744SMichael Mueller         feat = find_next_bit(bitmap, S390_FEAT_MAX, feat + 1);
16678241744SMichael Mueller     };
16778241744SMichael Mueller }
1688b3d6cb1SDavid Hildenbrand 
1698b3d6cb1SDavid Hildenbrand #define FEAT_GROUP_INIT(_name, _group, _desc)        \
1708b3d6cb1SDavid Hildenbrand     {                                                \
1718b3d6cb1SDavid Hildenbrand         .name = _name,                               \
1728b3d6cb1SDavid Hildenbrand         .desc = _desc,                               \
1738b3d6cb1SDavid Hildenbrand         .init = { S390_FEAT_GROUP_LIST_ ## _group }, \
1748b3d6cb1SDavid Hildenbrand     }
1758b3d6cb1SDavid Hildenbrand 
1768b3d6cb1SDavid Hildenbrand /* indexed by feature group number for easy lookup */
1778b3d6cb1SDavid Hildenbrand static S390FeatGroupDef s390_feature_groups[] = {
1788b3d6cb1SDavid Hildenbrand     FEAT_GROUP_INIT("plo", PLO, "Perform-locked-operation facility"),
1798b3d6cb1SDavid Hildenbrand     FEAT_GROUP_INIT("tods", TOD_CLOCK_STEERING, "Tod-clock-steering facility"),
1808b3d6cb1SDavid Hildenbrand     FEAT_GROUP_INIT("gen13ptff", GEN13_PTFF, "PTFF enhancements introduced with z13"),
1818b3d6cb1SDavid Hildenbrand     FEAT_GROUP_INIT("msa", MSA, "Message-security-assist facility"),
1828b3d6cb1SDavid Hildenbrand     FEAT_GROUP_INIT("msa1", MSA_EXT_1, "Message-security-assist-extension 1 facility"),
1838b3d6cb1SDavid Hildenbrand     FEAT_GROUP_INIT("msa2", MSA_EXT_2, "Message-security-assist-extension 2 facility"),
1848b3d6cb1SDavid Hildenbrand     FEAT_GROUP_INIT("msa3", MSA_EXT_3, "Message-security-assist-extension 3 facility"),
1858b3d6cb1SDavid Hildenbrand     FEAT_GROUP_INIT("msa4", MSA_EXT_4, "Message-security-assist-extension 4 facility"),
1868b3d6cb1SDavid Hildenbrand     FEAT_GROUP_INIT("msa5", MSA_EXT_5, "Message-security-assist-extension 5 facility"),
1876da5c593SJason J. Herne     FEAT_GROUP_INIT("msa6", MSA_EXT_6, "Message-security-assist-extension 6 facility"),
1886da5c593SJason J. Herne     FEAT_GROUP_INIT("msa7", MSA_EXT_7, "Message-security-assist-extension 7 facility"),
1896da5c593SJason J. Herne     FEAT_GROUP_INIT("msa8", MSA_EXT_8, "Message-security-assist-extension 8 facility"),
1905dacbe23SChristian Borntraeger     FEAT_GROUP_INIT("msa9", MSA_EXT_9, "Message-security-assist-extension 9 facility"),
1915dacbe23SChristian Borntraeger     FEAT_GROUP_INIT("msa9_pckmo", MSA_EXT_9_PCKMO, "Message-security-assist-extension 9 PCKMO subfunctions"),
192ddf5d18aSCollin Walling     FEAT_GROUP_INIT("mepochptff", MULTIPLE_EPOCH_PTFF, "PTFF enhancements introduced with Multiple-epoch facility"),
193d220fabfSChristian Borntraeger     FEAT_GROUP_INIT("esort", ENH_SORT, "Enhanced-sort facility"),
194afc7b866SChristian Borntraeger     FEAT_GROUP_INIT("deflate", DEFLATE_CONVERSION, "Deflate-conversion facility"),
1958b3d6cb1SDavid Hildenbrand };
1968b3d6cb1SDavid Hildenbrand 
1978b3d6cb1SDavid Hildenbrand const S390FeatGroupDef *s390_feat_group_def(S390FeatGroup group)
1988b3d6cb1SDavid Hildenbrand {
1998b3d6cb1SDavid Hildenbrand     return &s390_feature_groups[group];
2008b3d6cb1SDavid Hildenbrand }
2018b3d6cb1SDavid Hildenbrand 
2028b3d6cb1SDavid Hildenbrand static void init_groups(void)
2038b3d6cb1SDavid Hildenbrand {
2048b3d6cb1SDavid Hildenbrand     int i;
2058b3d6cb1SDavid Hildenbrand 
2068b3d6cb1SDavid Hildenbrand     /* init all bitmaps from gnerated data initially */
2078b3d6cb1SDavid Hildenbrand     for (i = 0; i < ARRAY_SIZE(s390_feature_groups); i++) {
2088b3d6cb1SDavid Hildenbrand         s390_init_feat_bitmap(s390_feature_groups[i].init,
2098b3d6cb1SDavid Hildenbrand                               s390_feature_groups[i].feat);
2108b3d6cb1SDavid Hildenbrand     }
2118b3d6cb1SDavid Hildenbrand }
2128b3d6cb1SDavid Hildenbrand 
2138b3d6cb1SDavid Hildenbrand type_init(init_groups)
214