11769Sjkh// SPDX-License-Identifier: GPL-2.0
21769Sjkh/*
31769Sjkh * Simple program to generate defines out of facility lists that use the bit
41769Sjkh * numbering scheme from the Princples of Operations: most significant bit
51769Sjkh * has bit number 0.
61769Sjkh *
71769Sjkh *    Copyright IBM Corp. 2015, 2018
81769Sjkh *
91769Sjkh */
101769Sjkh
111769Sjkh#include <strings.h>
121769Sjkh#include <string.h>
131769Sjkh#include <stdlib.h>
141769Sjkh#include <stdio.h>
151769Sjkh
161769Sjkhstruct facility_def {
171769Sjkh	char *name;
181769Sjkh	int *bits;
191769Sjkh};
201769Sjkh
211769Sjkhstatic struct facility_def facility_defs[] = {
221769Sjkh	{
231769Sjkh		/*
241769Sjkh		 * FACILITIES_ALS contains the list of facilities that are
251769Sjkh		 * required to run a kernel that is compiled e.g. with
261769Sjkh		 * -march=<machine>.
271769Sjkh		 */
281769Sjkh		.name = "FACILITIES_ALS",
291769Sjkh		.bits = (int[]){
301769Sjkh			0,  /* N3 instructions */
311769Sjkh			1,  /* z/Arch mode installed */
321769Sjkh			18, /* long displacement facility */
3351237Speter			21, /* extended-immediate facility */
34130151Sschweikh			25, /* store clock fast */
351769Sjkh			27, /* mvcos */
363876Sache			32, /* compare and swap and store */
373876Sache			33, /* compare and swap and store 2 */
3875786Sache			34, /* general instructions extension */
3975786Sache			35, /* execute extensions */
4075786Sache#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
4175786Sache			45, /* fast-BCR, etc. */
4275786Sache#endif
4375786Sache#ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
443120Sache			49, /* misc-instruction-extensions */
455559Sache			52, /* interlocked facility 2 */
4675786Sache#endif
4762284Sache#ifdef CONFIG_HAVE_MARCH_Z13_FEATURES
481769Sjkh			53, /* load-and-zero-rightmost-byte, etc. */
4975786Sache			129, /* vector */
5081669Sache#endif
5181669Sache#ifdef CONFIG_HAVE_MARCH_Z14_FEATURES
5286793Sache			58, /* miscellaneous-instruction-extension 2 */
533876Sache#endif
5486793Sache#ifdef CONFIG_HAVE_MARCH_Z15_FEATURES
553876Sache			61, /* miscellaneous-instruction-extension 3 */
563623Sache#endif
5763728Sache			-1 /* END */
5862284Sache		}
5962284Sache	},
6062284Sache	{
6162284Sache		/*
6262284Sache		 * FACILITIES_KVM contains the list of facilities that are part
6362284Sache		 * of the default facility mask and list that are passed to the
6462284Sache		 * initial CPU model. If no CPU model is used, this, together
6562284Sache		 * with the non-hypervisor managed bits, is the maximum list of
661769Sjkh		 * guest facilities supported by KVM.
677708Srgrimes		 */
683623Sache		.name = "FACILITIES_KVM",
693623Sache		.bits = (int[]){
7062284Sache			0,  /* N3 instructions */
7162284Sache			1,  /* z/Arch mode installed */
7262284Sache			2,  /* z/Arch mode active */
7362284Sache			3,  /* DAT-enhancement */
74194107Sedwin			4,  /* idte segment table */
75194107Sedwin			5,  /* idte region table */
76194107Sedwin			6,  /* ASN-and-LX reuse */
77194107Sedwin			7,  /* stfle */
78194107Sedwin			8,  /* enhanced-DAT 1 */
79194107Sedwin			9,  /* sense-running-status */
80194107Sedwin			10, /* conditional sske */
81194107Sedwin			13, /* ipte-range */
82194107Sedwin			14, /* nonquiescing key-setting */
83194107Sedwin			73, /* transactional execution */
84194107Sedwin			75, /* access-exception-fetch/store indication */
8575717Sache			76, /* msa extension 3 */
8675717Sache			77, /* msa extension 4 */
8786791Sache			78, /* enhanced-DAT 2 */
8865073Sache			130, /* instruction-execution-protection */
8975717Sache			131, /* enhanced-SOP 2 and side-effect */
9063728Sache			139, /* multiple epoch facility */
9175717Sache			146, /* msa extension 8 */
9262284Sache			150, /* enhanced sort */
9375717Sache			151, /* deflate conversion */
9462284Sache			155, /* msa extension 9 */
9575717Sache			-1  /* END */
9662284Sache		}
9775717Sache	},
9862284Sache	{
9975717Sache		/*
1003132Sache		 * FACILITIES_KVM_CPUMODEL contains the list of facilities
10175717Sache		 * that can be enabled by CPU model code if the host supports
1023623Sache		 * it. These facilities are not passed to the guest without
10375717Sache		 * CPU model support.
10462284Sache		 */
10575717Sache
10662284Sache		.name = "FACILITIES_KVM_CPUMODEL",
107194107Sedwin		.bits = (int[]){
108194107Sedwin			12, /* AP Query Configuration Information */
109194107Sedwin			15, /* AP Facilities Test */
110194107Sedwin			156, /* etoken facility */
111194107Sedwin			165, /* nnpa facility */
112194107Sedwin			193, /* bear enhancement facility */
113194107Sedwin			194, /* rdp enhancement facility */
114194107Sedwin			196, /* processor activity instrumentation facility */
115194107Sedwin			197, /* processor activity instrumentation extension 1 */
116194107Sedwin			-1  /* END */
117194107Sedwin		}
118194107Sedwin	},
119194107Sedwin};
120194107Sedwin
121194107Sedwinstatic void print_facility_list(struct facility_def *def)
122194107Sedwin{
123194107Sedwin	unsigned int high, bit, dword, i;
124194107Sedwin	unsigned long long *array;
125194107Sedwin
126194107Sedwin	array = calloc(1, 8);
127194107Sedwin	if (!array)
128194107Sedwin		exit(EXIT_FAILURE);
12962284Sache	high = 0;
13062284Sache	for (i = 0; def->bits[i] != -1; i++) {
13186791Sache		bit = 63 - (def->bits[i] & 63);
13262284Sache		dword = def->bits[i] / 64;
13362284Sache		if (dword > high) {
13463728Sache			array = realloc(array, (dword + 1) * 8);
13562284Sache			if (!array)
13662284Sache				exit(EXIT_FAILURE);
13762284Sache			memset(array + high + 1, 0, (dword - high) * 8);
13862284Sache			high = dword;
13962284Sache		}
14062284Sache		array[dword] |= 1ULL << bit;
14162284Sache	}
14262284Sache	printf("#define %s ", def->name);
14362284Sache	for (i = 0; i <= high; i++)
14462284Sache		printf("_AC(0x%016llx,UL)%c", array[i], i < high ? ',' : '\n');
14562284Sache	free(array);
14662284Sache}
14762284Sache
14862284Sachestatic void print_facility_lists(void)
14962284Sache{
15062284Sache	unsigned int i;
15162284Sache
1523623Sache	for (i = 0; i < sizeof(facility_defs) / sizeof(facility_defs[0]); i++)
15386791Sache		print_facility_list(&facility_defs[i]);
1543876Sache}
1553623Sache
15663728Sacheint main(int argc, char **argv)
15762284Sache{
15862284Sache	printf("#ifndef __ASM_S390_FACILITY_DEFS__\n");
15962284Sache	printf("#define __ASM_S390_FACILITY_DEFS__\n");
16062284Sache	printf("/*\n");
16162284Sache	printf(" * DO NOT MODIFY.\n");
16262284Sache	printf(" *\n");
16362284Sache	printf(" * This file was generated by %s\n", __FILE__);
16462284Sache	printf(" */\n\n");
1653623Sache	printf("#include <linux/const.h>\n\n");
1663132Sache	print_facility_lists();
1673623Sache	printf("\n#endif\n");
1683623Sache	return 0;
16962284Sache}
17062284Sache