xref: /openbmc/linux/arch/s390/include/asm/facility.h (revision a84ae28d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright IBM Corp. 1999, 2009
4  *
5  * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
6  */
7 
8 #ifndef __ASM_FACILITY_H
9 #define __ASM_FACILITY_H
10 
11 #include <asm/facility-defs.h>
12 
13 #include <linux/minmax.h>
14 #include <linux/string.h>
15 #include <linux/types.h>
16 #include <linux/preempt.h>
17 
18 #include <asm/lowcore.h>
19 
20 #define MAX_FACILITY_BIT (sizeof(stfle_fac_list) * 8)
21 
22 extern u64 stfle_fac_list[16];
23 extern u64 alt_stfle_fac_list[16];
24 
25 static inline void __set_facility(unsigned long nr, void *facilities)
26 {
27 	unsigned char *ptr = (unsigned char *) facilities;
28 
29 	if (nr >= MAX_FACILITY_BIT)
30 		return;
31 	ptr[nr >> 3] |= 0x80 >> (nr & 7);
32 }
33 
34 static inline void __clear_facility(unsigned long nr, void *facilities)
35 {
36 	unsigned char *ptr = (unsigned char *) facilities;
37 
38 	if (nr >= MAX_FACILITY_BIT)
39 		return;
40 	ptr[nr >> 3] &= ~(0x80 >> (nr & 7));
41 }
42 
43 static inline int __test_facility(unsigned long nr, void *facilities)
44 {
45 	unsigned char *ptr;
46 
47 	if (nr >= MAX_FACILITY_BIT)
48 		return 0;
49 	ptr = (unsigned char *) facilities + (nr >> 3);
50 	return (*ptr & (0x80 >> (nr & 7))) != 0;
51 }
52 
53 /*
54  * The test_facility function uses the bit ordering where the MSB is bit 0.
55  * That makes it easier to query facility bits with the bit number as
56  * documented in the Principles of Operation.
57  */
58 static inline int test_facility(unsigned long nr)
59 {
60 	unsigned long facilities_als[] = { FACILITIES_ALS };
61 
62 	if (__builtin_constant_p(nr) && nr < sizeof(facilities_als) * 8) {
63 		if (__test_facility(nr, &facilities_als)) {
64 			if (!__is_defined(__DECOMPRESSOR))
65 				return 1;
66 		}
67 	}
68 	return __test_facility(nr, &stfle_fac_list);
69 }
70 
71 static inline unsigned long __stfle_asm(u64 *stfle_fac_list, int size)
72 {
73 	unsigned long reg0 = size - 1;
74 
75 	asm volatile(
76 		"	lgr	0,%[reg0]\n"
77 		"	.insn	s,0xb2b00000,%[list]\n" /* stfle */
78 		"	lgr	%[reg0],0\n"
79 		: [reg0] "+&d" (reg0), [list] "+Q" (*stfle_fac_list)
80 		:
81 		: "memory", "cc", "0");
82 	return reg0;
83 }
84 
85 /**
86  * stfle - Store facility list extended
87  * @stfle_fac_list: array where facility list can be stored
88  * @size: size of passed in array in double words
89  */
90 static inline void __stfle(u64 *stfle_fac_list, int size)
91 {
92 	unsigned long nr;
93 	u32 stfl_fac_list;
94 
95 	asm volatile(
96 		"	stfl	0(0)\n"
97 		: "=m" (S390_lowcore.stfl_fac_list));
98 	stfl_fac_list = S390_lowcore.stfl_fac_list;
99 	memcpy(stfle_fac_list, &stfl_fac_list, 4);
100 	nr = 4; /* bytes stored by stfl */
101 	if (stfl_fac_list & 0x01000000) {
102 		/* More facility bits available with stfle */
103 		nr = __stfle_asm(stfle_fac_list, size);
104 		nr = min_t(unsigned long, (nr + 1) * 8, size * 8);
105 	}
106 	memset((char *) stfle_fac_list + nr, 0, size * 8 - nr);
107 }
108 
109 static inline void stfle(u64 *stfle_fac_list, int size)
110 {
111 	preempt_disable();
112 	__stfle(stfle_fac_list, size);
113 	preempt_enable();
114 }
115 
116 #endif /* __ASM_FACILITY_H */
117