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