1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright IBM Corp. 1999, 2009
8 #ifndef __ASM_FACILITY_H
9 #define __ASM_FACILITY_H
11 #include <asm/facility-defs.h>
13 #include <linux/minmax.h>
14 #include <linux/string.h>
15 #include <linux/types.h>
16 #include <linux/preempt.h>
17 #include <asm/alternative.h>
18 #include <asm/lowcore.h>
20 #define MAX_FACILITY_BIT (sizeof(stfle_fac_list) * 8)
22 extern u64 stfle_fac_list[16];
24 static inline void __set_facility(unsigned long nr, void *facilities)
26 unsigned char *ptr = (unsigned char *) facilities;
28 if (nr >= MAX_FACILITY_BIT)
30 ptr[nr >> 3] |= 0x80 >> (nr & 7);
33 static inline void __clear_facility(unsigned long nr, void *facilities)
35 unsigned char *ptr = (unsigned char *) facilities;
37 if (nr >= MAX_FACILITY_BIT)
39 ptr[nr >> 3] &= ~(0x80 >> (nr & 7));
42 static __always_inline bool __test_facility(unsigned long nr, void *facilities)
46 if (nr >= MAX_FACILITY_BIT)
48 ptr = (unsigned char *) facilities + (nr >> 3);
49 return (*ptr & (0x80 >> (nr & 7))) != 0;
53 * __test_facility_constant() generates a single instruction branch. If the
54 * tested facility is available (likely) the branch is patched into a nop.
56 * Do not use this function unless you know what you are doing. All users are
57 * supposed to use test_facility() which will do the right thing.
59 static __always_inline bool __test_facility_constant(unsigned long nr)
62 ALTERNATIVE("brcl 15,%l[l_no]", "brcl 0,0", ALT_FACILITY(%[nr]))
73 * The test_facility function uses the bit ordering where the MSB is bit 0.
74 * That makes it easier to query facility bits with the bit number as
75 * documented in the Principles of Operation.
77 static __always_inline bool test_facility(unsigned long nr)
79 unsigned long facilities_als[] = { FACILITIES_ALS };
81 if (!__is_defined(__DECOMPRESSOR) && __builtin_constant_p(nr)) {
82 if (nr < sizeof(facilities_als) * 8) {
83 if (__test_facility(nr, &facilities_als))
86 return __test_facility_constant(nr);
88 return __test_facility(nr, &stfle_fac_list);
91 static inline unsigned long __stfle_asm(u64 *fac_list, int size)
93 unsigned long reg0 = size - 1;
97 " .insn s,0xb2b00000,%[list]\n" /* stfle */
99 : [reg0] "+&d" (reg0), [list] "+Q" (*fac_list)
101 : "memory", "cc", "0");
106 * stfle - Store facility list extended
107 * @fac_list: array where facility list can be stored
108 * @size: size of passed in array in double words
110 static inline void __stfle(u64 *fac_list, int size)
117 : "=m" (get_lowcore()->stfl_fac_list));
118 stfl_fac_list = get_lowcore()->stfl_fac_list;
119 memcpy(fac_list, &stfl_fac_list, 4);
120 nr = 4; /* bytes stored by stfl */
121 if (stfl_fac_list & 0x01000000) {
122 /* More facility bits available with stfle */
123 nr = __stfle_asm(fac_list, size);
124 nr = min_t(unsigned long, (nr + 1) * 8, size * 8);
126 memset((char *)fac_list + nr, 0, size * 8 - nr);
129 static inline void stfle(u64 *fac_list, int size)
132 __stfle(fac_list, size);
137 * stfle_size - Actual size of the facility list as specified by stfle
138 * (number of double words)
140 unsigned int stfle_size(void);
142 #endif /* __ASM_FACILITY_H */