1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Simple program to generate defines out of facility lists that use the bit
4  * numbering scheme from the Princples of Operations: most significant bit
5  * has bit number 0.
6  *
7  *    Copyright IBM Corp. 2015, 2018
8  *
9  */
10 
11 #include <strings.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 
16 struct facility_def {
17 	char *name;
18 	int *bits;
19 };
20 
21 static struct facility_def facility_defs[] = {
22 	{
23 		/*
24 		 * FACILITIES_ALS contains the list of facilities that are
25 		 * required to run a kernel that is compiled e.g. with
26 		 * -march=<machine>.
27 		 */
28 		.name = "FACILITIES_ALS",
29 		.bits = (int[]){
30 			0,  /* N3 instructions */
31 			1,  /* z/Arch mode installed */
32 			18, /* long displacement facility */
33 			21, /* extended-immediate facility */
34 			25, /* store clock fast */
35 			27, /* mvcos */
36 			32, /* compare and swap and store */
37 			33, /* compare and swap and store 2 */
38 			34, /* general instructions extension */
39 			35, /* execute extensions */
40 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
41 			45, /* fast-BCR, etc. */
42 #endif
43 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
44 			49, /* misc-instruction-extensions */
45 			52, /* interlocked facility 2 */
46 #endif
47 #ifdef CONFIG_HAVE_MARCH_Z13_FEATURES
48 			53, /* load-and-zero-rightmost-byte, etc. */
49 			129, /* vector */
50 #endif
51 #ifdef CONFIG_HAVE_MARCH_Z14_FEATURES
52 			58, /* miscellaneous-instruction-extension 2 */
53 #endif
54 #ifdef CONFIG_HAVE_MARCH_Z15_FEATURES
55 			61, /* miscellaneous-instruction-extension 3 */
56 #endif
57 #ifdef CONFIG_HAVE_MARCH_Z17_FEATURES
58 			84, /* miscellaneous-instruction-extension 4 */
59 #endif
60 			-1 /* END */
61 		}
62 	},
63 	{
64 		/*
65 		 * FACILITIES_KVM contains the list of facilities that are part
66 		 * of the default facility mask and list that are passed to the
67 		 * initial CPU model. If no CPU model is used, this, together
68 		 * with the non-hypervisor managed bits, is the maximum list of
69 		 * guest facilities supported by KVM.
70 		 */
71 		.name = "FACILITIES_KVM",
72 		.bits = (int[]){
73 			0,  /* N3 instructions */
74 			1,  /* z/Arch mode installed */
75 			2,  /* z/Arch mode active */
76 			3,  /* DAT-enhancement */
77 			4,  /* idte segment table */
78 			5,  /* idte region table */
79 			6,  /* ASN-and-LX reuse */
80 			7,  /* stfle */
81 			8,  /* enhanced-DAT 1 */
82 			9,  /* sense-running-status */
83 			10, /* conditional sske */
84 			13, /* ipte-range */
85 			14, /* nonquiescing key-setting */
86 			73, /* transactional execution */
87 			75, /* access-exception-fetch/store indication */
88 			76, /* msa extension 3 */
89 			77, /* msa extension 4 */
90 			78, /* enhanced-DAT 2 */
91 			130, /* instruction-execution-protection */
92 			131, /* enhanced-SOP 2 and side-effect */
93 			139, /* multiple epoch facility */
94 			146, /* msa extension 8 */
95 			150, /* enhanced sort */
96 			151, /* deflate conversion */
97 			155, /* msa extension 9 */
98 			-1  /* END */
99 		}
100 	},
101 	{
102 		/*
103 		 * FACILITIES_KVM_CPUMODEL contains the list of facilities
104 		 * that can be enabled by CPU model code if the host supports
105 		 * it. These facilities are not passed to the guest without
106 		 * CPU model support.
107 		 */
108 
109 		.name = "FACILITIES_KVM_CPUMODEL",
110 		.bits = (int[]){
111 			12, /* AP Query Configuration Information */
112 			15, /* AP Facilities Test */
113 			156, /* etoken facility */
114 			165, /* nnpa facility */
115 			170, /* ineffective-nonconstrained-transaction facility */
116 			193, /* bear enhancement facility */
117 			194, /* rdp enhancement facility */
118 			196, /* processor activity instrumentation facility */
119 			197, /* processor activity instrumentation extension 1 */
120 			201, /* concurrent-functions facility */
121 			-1  /* END */
122 		}
123 	},
124 };
125 
126 static void print_facility_list(struct facility_def *def)
127 {
128 	unsigned int high, bit, dword, i;
129 	unsigned long long *array;
130 
131 	array = calloc(1, 8);
132 	if (!array)
133 		exit(EXIT_FAILURE);
134 	high = 0;
135 	for (i = 0; def->bits[i] != -1; i++) {
136 		bit = 63 - (def->bits[i] & 63);
137 		dword = def->bits[i] / 64;
138 		if (dword > high) {
139 			array = realloc(array, (dword + 1) * 8);
140 			if (!array)
141 				exit(EXIT_FAILURE);
142 			memset(array + high + 1, 0, (dword - high) * 8);
143 			high = dword;
144 		}
145 		array[dword] |= 1ULL << bit;
146 	}
147 	printf("#define %s ", def->name);
148 	for (i = 0; i <= high; i++)
149 		printf("_AC(0x%016llx,UL)%c", array[i], i < high ? ',' : '\n');
150 	free(array);
151 }
152 
153 static void print_facility_lists(void)
154 {
155 	unsigned int i;
156 
157 	for (i = 0; i < sizeof(facility_defs) / sizeof(facility_defs[0]); i++)
158 		print_facility_list(&facility_defs[i]);
159 }
160 
161 int main(int argc, char **argv)
162 {
163 	printf("#ifndef __ASM_S390_FACILITY_DEFS__\n");
164 	printf("#define __ASM_S390_FACILITY_DEFS__\n");
165 	printf("/*\n");
166 	printf(" * DO NOT MODIFY.\n");
167 	printf(" *\n");
168 	printf(" * This file was generated by %s\n", __FILE__);
169 	printf(" */\n\n");
170 	printf("#include <linux/const.h>\n\n");
171 	print_facility_lists();
172 	printf("\n#endif\n");
173 	return 0;
174 }
175