1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2014 Felix Fietkau <nbd@nbd.name>
4  * Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
5  */
6 
7 #ifndef _LINUX_BITFIELD_H
8 #define _LINUX_BITFIELD_H
9 
10 #include <linux/build_bug.h>
11 #include <linux/typecheck.h>
12 #include <asm/byteorder.h>
13 
14 /*
15  * Bitfield access macros
16  *
17  * FIELD_{GET,PREP} macros take as first parameter shifted mask
18  * from which they extract the base mask and shift amount.
19  * Mask must be a compilation time constant.
20  *
21  * Example:
22  *
23  *  #include <linux/bitfield.h>
24  *  #include <linux/bits.h>
25  *
26  *  #define REG_FIELD_A  GENMASK(6, 0)
27  *  #define REG_FIELD_B  BIT(7)
28  *  #define REG_FIELD_C  GENMASK(15, 8)
29  *  #define REG_FIELD_D  GENMASK(31, 16)
30  *
31  * Get:
32  *  a = FIELD_GET(REG_FIELD_A, reg);
33  *  b = FIELD_GET(REG_FIELD_B, reg);
34  *
35  * Set:
36  *  reg = FIELD_PREP(REG_FIELD_A, 1) |
37  *	  FIELD_PREP(REG_FIELD_B, 0) |
38  *	  FIELD_PREP(REG_FIELD_C, c) |
39  *	  FIELD_PREP(REG_FIELD_D, 0x40);
40  *
41  * Modify:
42  *  FIELD_MODIFY(REG_FIELD_C, &reg, c);
43  */
44 
45 #define __bf_shf(x) (__builtin_ffsll(x) - 1)
46 
47 #define __scalar_type_to_unsigned_cases(type)				\
48 		unsigned type:	(unsigned type)0,			\
49 		signed type:	(unsigned type)0
50 
51 #define __unsigned_scalar_typeof(x) typeof(				\
52 		_Generic((x),						\
53 			char:	(unsigned char)0,			\
54 			__scalar_type_to_unsigned_cases(char),		\
55 			__scalar_type_to_unsigned_cases(short),		\
56 			__scalar_type_to_unsigned_cases(int),		\
57 			__scalar_type_to_unsigned_cases(long),		\
58 			__scalar_type_to_unsigned_cases(long long),	\
59 			default: (x)))
60 
61 #define __bf_cast_unsigned(type, x)	((__unsigned_scalar_typeof(type))(x))
62 
63 #define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx)			\
64 	({								\
65 		BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask),		\
66 				 _pfx "mask is not constant");		\
67 		BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero");	\
68 		BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ?		\
69 				 ~((_mask) >> __bf_shf(_mask)) &	\
70 					(0 + (_val)) : 0,		\
71 				 _pfx "value too large for the field"); \
72 		BUILD_BUG_ON_MSG(__bf_cast_unsigned(_mask, _mask) >	\
73 				 __bf_cast_unsigned(_reg, ~0ull),	\
74 				 _pfx "type of reg too small for mask"); \
75 		__BUILD_BUG_ON_NOT_POWER_OF_2((_mask) +			\
76 					      (1ULL << __bf_shf(_mask))); \
77 	})
78 
79 /**
80  * FIELD_MAX() - produce the maximum value representable by a field
81  * @_mask: shifted mask defining the field's length and position
82  *
83  * FIELD_MAX() returns the maximum value that can be held in the field
84  * specified by @_mask.
85  */
86 #define FIELD_MAX(_mask)						\
87 	({								\
88 		__BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: ");	\
89 		(typeof(_mask))((_mask) >> __bf_shf(_mask));		\
90 	})
91 
92 /**
93  * FIELD_FIT() - check if value fits in the field
94  * @_mask: shifted mask defining the field's length and position
95  * @_val:  value to test against the field
96  *
97  * Return: true if @_val can fit inside @_mask, false if @_val is too big.
98  */
99 #define FIELD_FIT(_mask, _val)						\
100 	({								\
101 		__BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: ");	\
102 		!((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
103 	})
104 
105 /**
106  * FIELD_PREP() - prepare a bitfield element
107  * @_mask: shifted mask defining the field's length and position
108  * @_val:  value to put in the field
109  *
110  * FIELD_PREP() masks and shifts up the value.  The result should
111  * be combined with other fields of the bitfield using logical OR.
112  */
113 #define FIELD_PREP(_mask, _val)						\
114 	({								\
115 		__BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: ");	\
116 		((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask);	\
117 	})
118 
119 #define __BF_CHECK_POW2(n)	BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0)
120 
121 /**
122  * FIELD_PREP_CONST() - prepare a constant bitfield element
123  * @_mask: shifted mask defining the field's length and position
124  * @_val:  value to put in the field
125  *
126  * FIELD_PREP_CONST() masks and shifts up the value.  The result should
127  * be combined with other fields of the bitfield using logical OR.
128  *
129  * Unlike FIELD_PREP() this is a constant expression and can therefore
130  * be used in initializers. Error checking is less comfortable for this
131  * version, and non-constant masks cannot be used.
132  */
133 #define FIELD_PREP_CONST(_mask, _val)					\
134 	(								\
135 		/* mask must be non-zero */				\
136 		BUILD_BUG_ON_ZERO((_mask) == 0) +			\
137 		/* check if value fits */				\
138 		BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \
139 		/* check if mask is contiguous */			\
140 		__BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask))) +	\
141 		/* and create the value */				\
142 		(((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask))	\
143 	)
144 
145 /**
146  * FIELD_GET() - extract a bitfield element
147  * @_mask: shifted mask defining the field's length and position
148  * @_reg:  value of entire bitfield
149  *
150  * FIELD_GET() extracts the field specified by @_mask from the
151  * bitfield passed in as @_reg by masking and shifting it down.
152  */
153 #define FIELD_GET(_mask, _reg)						\
154 	({								\
155 		__BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: ");	\
156 		(typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask));	\
157 	})
158 
159 /**
160  * FIELD_MODIFY() - modify a bitfield element
161  * @_mask: shifted mask defining the field's length and position
162  * @_reg_p: pointer to the memory that should be updated
163  * @_val: value to store in the bitfield
164  *
165  * FIELD_MODIFY() modifies the set of bits in @_reg_p specified by @_mask,
166  * by replacing them with the bitfield value passed in as @_val.
167  */
168 #define FIELD_MODIFY(_mask, _reg_p, _val)						\
169 	({										\
170 		typecheck_pointer(_reg_p);						\
171 		__BF_FIELD_CHECK(_mask, *(_reg_p), _val, "FIELD_MODIFY: ");		\
172 		*(_reg_p) &= ~(_mask);							\
173 		*(_reg_p) |= (((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask));	\
174 	})
175 
176 extern void __compiletime_error("value doesn't fit into mask")
177 __field_overflow(void);
178 extern void __compiletime_error("bad bitfield mask")
179 __bad_mask(void);
field_multiplier(u64 field)180 static __always_inline u64 field_multiplier(u64 field)
181 {
182 	if ((field | (field - 1)) & ((field | (field - 1)) + 1))
183 		__bad_mask();
184 	return field & -field;
185 }
field_mask(u64 field)186 static __always_inline u64 field_mask(u64 field)
187 {
188 	return field / field_multiplier(field);
189 }
190 #define field_max(field)	((typeof(field))field_mask(field))
191 #define ____MAKE_OP(type,base,to,from)					\
192 static __always_inline __##type __must_check type##_encode_bits(base v, base field)	\
193 {									\
194 	if (__builtin_constant_p(v) && (v & ~field_mask(field)))	\
195 		__field_overflow();					\
196 	return to((v & field_mask(field)) * field_multiplier(field));	\
197 }									\
198 static __always_inline __##type __must_check type##_replace_bits(__##type old,	\
199 							base val, base field)	\
200 {									\
201 	return (old & ~to(field)) | type##_encode_bits(val, field);	\
202 }									\
203 static __always_inline void type##p_replace_bits(__##type *p,		\
204 					base val, base field)		\
205 {									\
206 	*p = (*p & ~to(field)) | type##_encode_bits(val, field);	\
207 }									\
208 static __always_inline base __must_check type##_get_bits(__##type v, base field)	\
209 {									\
210 	return (from(v) & field)/field_multiplier(field);		\
211 }
212 #define __MAKE_OP(size)							\
213 	____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu)	\
214 	____MAKE_OP(be##size,u##size,cpu_to_be##size,be##size##_to_cpu)	\
215 	____MAKE_OP(u##size,u##size,,)
216 ____MAKE_OP(u8,u8,,)
217 __MAKE_OP(16)
218 __MAKE_OP(32)
219 __MAKE_OP(64)
220 #undef __MAKE_OP
221 #undef ____MAKE_OP
222 
223 #endif
224