xref: /openbmc/linux/arch/s390/tools/gen_facilities.c (revision 5927145e)
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
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 #ifdef CONFIG_HAVE_MARCH_Z900_FEATURES
31 			0,  /* N3 instructions */
32 			1,  /* z/Arch mode installed */
33 #endif
34 #ifdef CONFIG_HAVE_MARCH_Z990_FEATURES
35 			18, /* long displacement facility */
36 #endif
37 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
38 			21, /* extended-immediate facility */
39 			25, /* store clock fast */
40 #endif
41 #ifdef CONFIG_HAVE_MARCH_Z10_FEATURES
42 			27, /* mvcos */
43 			32, /* compare and swap and store */
44 			33, /* compare and swap and store 2 */
45 			34, /* general instructions extension */
46 			35, /* execute extensions */
47 #endif
48 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
49 			45, /* fast-BCR, etc. */
50 #endif
51 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
52 			49, /* misc-instruction-extensions */
53 			52, /* interlocked facility 2 */
54 #endif
55 #ifdef CONFIG_HAVE_MARCH_Z13_FEATURES
56 			53, /* load-and-zero-rightmost-byte, etc. */
57 #endif
58 #ifdef CONFIG_HAVE_MARCH_Z14_FEATURES
59 			58, /* miscellaneous-instruction-extension 2 */
60 #endif
61 			-1 /* END */
62 		}
63 	},
64 	{
65 		.name = "FACILITIES_KVM",
66 		.bits = (int[]){
67 			0,  /* N3 instructions */
68 			1,  /* z/Arch mode installed */
69 			2,  /* z/Arch mode active */
70 			3,  /* DAT-enhancement */
71 			4,  /* idte segment table */
72 			5,  /* idte region table */
73 			6,  /* ASN-and-LX reuse */
74 			7,  /* stfle */
75 			8,  /* enhanced-DAT 1 */
76 			9,  /* sense-running-status */
77 			10, /* conditional sske */
78 			13, /* ipte-range */
79 			14, /* nonquiescing key-setting */
80 			73, /* transactional execution */
81 			75, /* access-exception-fetch/store indication */
82 			76, /* msa extension 3 */
83 			77, /* msa extension 4 */
84 			78, /* enhanced-DAT 2 */
85 			130, /* instruction-execution-protection */
86 			131, /* enhanced-SOP 2 and side-effect */
87 			139, /* multiple epoch facility */
88 			146, /* msa extension 8 */
89 			-1  /* END */
90 		}
91 	},
92 };
93 
94 static void print_facility_list(struct facility_def *def)
95 {
96 	unsigned int high, bit, dword, i;
97 	unsigned long long *array;
98 
99 	array = calloc(1, 8);
100 	if (!array)
101 		exit(EXIT_FAILURE);
102 	high = 0;
103 	for (i = 0; def->bits[i] != -1; i++) {
104 		bit = 63 - (def->bits[i] & 63);
105 		dword = def->bits[i] / 64;
106 		if (dword > high) {
107 			array = realloc(array, (dword + 1) * 8);
108 			if (!array)
109 				exit(EXIT_FAILURE);
110 			memset(array + high + 1, 0, (dword - high) * 8);
111 			high = dword;
112 		}
113 		array[dword] |= 1ULL << bit;
114 	}
115 	printf("#define %s ", def->name);
116 	for (i = 0; i <= high; i++)
117 		printf("_AC(0x%016llx,UL)%c", array[i], i < high ? ',' : '\n');
118 	free(array);
119 }
120 
121 static void print_facility_lists(void)
122 {
123 	unsigned int i;
124 
125 	for (i = 0; i < sizeof(facility_defs) / sizeof(facility_defs[0]); i++)
126 		print_facility_list(&facility_defs[i]);
127 }
128 
129 int main(int argc, char **argv)
130 {
131 	printf("#ifndef __ASM_S390_FACILITY_DEFS__\n");
132 	printf("#define __ASM_S390_FACILITY_DEFS__\n");
133 	printf("/*\n");
134 	printf(" * DO NOT MODIFY.\n");
135 	printf(" *\n");
136 	printf(" * This file was generated by %s\n", __FILE__);
137 	printf(" */\n\n");
138 	printf("#include <linux/const.h>\n\n");
139 	print_facility_lists();
140 	printf("\n#endif\n");
141 	return 0;
142 }
143