xref: /openbmc/linux/arch/s390/tools/gen_facilities.c (revision 4ed91d48259d9ddd378424d008f2e6559f7e78f8)
1 /*
2  * Simple program to generate defines out of facility lists that use the bit
3  * numbering scheme from the Princples of Operations: most significant bit
4  * has bit number 0.
5  *
6  *    Copyright IBM Corp. 2015
7  *
8  */
9 
10 #include <strings.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 
15 struct facility_def {
16 	char *name;
17 	int *bits;
18 };
19 
20 static struct facility_def facility_defs[] = {
21 	{
22 		/*
23 		 * FACILITIES_ALS contains the list of facilities that are
24 		 * required to run a kernel that is compiled e.g. with
25 		 * -march=<machine>.
26 		 */
27 		.name = "FACILITIES_ALS",
28 		.bits = (int[]){
29 #ifdef CONFIG_HAVE_MARCH_Z900_FEATURES
30 			0,  /* N3 instructions */
31 			1,  /* z/Arch mode installed */
32 #endif
33 #ifdef CONFIG_HAVE_MARCH_Z990_FEATURES
34 			18, /* long displacement facility */
35 #endif
36 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
37 			7,  /* stfle */
38 			17, /* message security assist */
39 			21, /* extended-immediate facility */
40 			25, /* store clock fast */
41 #endif
42 #ifdef CONFIG_HAVE_MARCH_Z10_FEATURES
43 			27, /* mvcos */
44 			32, /* compare and swap and store */
45 			33, /* compare and swap and store 2 */
46 			34, /* general extension facility */
47 			35, /* execute extensions */
48 #endif
49 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
50 			45, /* fast-BCR, etc. */
51 #endif
52 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
53 			49, /* misc-instruction-extensions */
54 			52, /* interlocked facility 2 */
55 #endif
56 #ifdef CONFIG_HAVE_MARCH_Z13_FEATURES
57 			53, /* load-and-zero-rightmost-byte, etc. */
58 #endif
59 			-1 /* END */
60 		}
61 	},
62 	{
63 		.name = "FACILITIES_KVM",
64 		.bits = (int[]){
65 			0,  /* N3 instructions */
66 			1,  /* z/Arch mode installed */
67 			2,  /* z/Arch mode active */
68 			3,  /* DAT-enhancement */
69 			4,  /* idte segment table */
70 			5,  /* idte region table */
71 			6,  /* ASN-and-LX reuse */
72 			7,  /* stfle */
73 			8,  /* enhanced-DAT 1 */
74 			9,  /* sense-running-status */
75 			10, /* conditional sske */
76 			13, /* ipte-range */
77 			14, /* nonquiescing key-setting */
78 			73, /* transactional execution */
79 			75, /* access-exception-fetch/store indication */
80 			76, /* msa extension 3 */
81 			77, /* msa extension 4 */
82 			78, /* enhanced-DAT 2 */
83 			130, /* instruction-execution-protection */
84 			131, /* enhanced-SOP 2 and side-effect */
85 			-1  /* END */
86 		}
87 	},
88 };
89 
90 static void print_facility_list(struct facility_def *def)
91 {
92 	unsigned int high, bit, dword, i;
93 	unsigned long long *array;
94 
95 	array = calloc(1, 8);
96 	if (!array)
97 		exit(EXIT_FAILURE);
98 	high = 0;
99 	for (i = 0; def->bits[i] != -1; i++) {
100 		bit = 63 - (def->bits[i] & 63);
101 		dword = def->bits[i] / 64;
102 		if (dword > high) {
103 			array = realloc(array, (dword + 1) * 8);
104 			if (!array)
105 				exit(EXIT_FAILURE);
106 			memset(array + high + 1, 0, (dword - high) * 8);
107 			high = dword;
108 		}
109 		array[dword] |= 1ULL << bit;
110 	}
111 	printf("#define %s ", def->name);
112 	for (i = 0; i <= high; i++)
113 		printf("_AC(0x%016llx,UL)%c", array[i], i < high ? ',' : '\n');
114 	free(array);
115 }
116 
117 static void print_facility_lists(void)
118 {
119 	unsigned int i;
120 
121 	for (i = 0; i < sizeof(facility_defs) / sizeof(facility_defs[0]); i++)
122 		print_facility_list(&facility_defs[i]);
123 }
124 
125 int main(int argc, char **argv)
126 {
127 	printf("#ifndef __ASM_S390_FACILITIES__\n");
128 	printf("#define __ASM_S390_FACILITIES__\n");
129 	printf("/*\n");
130 	printf(" * DO NOT MODIFY.\n");
131 	printf(" *\n");
132 	printf(" * This file was generated by %s\n", __FILE__);
133 	printf(" */\n\n");
134 	printf("#include <linux/const.h>\n\n");
135 	print_facility_lists();
136 	printf("\n#endif\n");
137 	return 0;
138 }
139