blob: 4e035aca6f7e60003ce8235ea11e0b158c650f6e [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Jakub Kicinski3e9b3112016-08-31 12:46:44 +01002/*
3 * Copyright (C) 2014 Felix Fietkau <nbd@nbd.name>
4 * Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
Jakub Kicinski3e9b3112016-08-31 12:46:44 +01005 */
6
7#ifndef _LINUX_BITFIELD_H
8#define _LINUX_BITFIELD_H
9
Masahiro Yamada80015412017-11-17 15:27:49 -080010#include <linux/build_bug.h>
Al Viro00b0c9b2017-12-14 21:27:55 -050011#include <asm/byteorder.h>
Jakub Kicinski3e9b3112016-08-31 12:46:44 +010012
13/*
14 * Bitfield access macros
15 *
16 * FIELD_{GET,PREP} macros take as first parameter shifted mask
17 * from which they extract the base mask and shift amount.
18 * Mask must be a compilation time constant.
19 *
20 * Example:
21 *
22 * #define REG_FIELD_A GENMASK(6, 0)
23 * #define REG_FIELD_B BIT(7)
24 * #define REG_FIELD_C GENMASK(15, 8)
25 * #define REG_FIELD_D GENMASK(31, 16)
26 *
27 * Get:
28 * a = FIELD_GET(REG_FIELD_A, reg);
29 * b = FIELD_GET(REG_FIELD_B, reg);
30 *
31 * Set:
32 * reg = FIELD_PREP(REG_FIELD_A, 1) |
33 * FIELD_PREP(REG_FIELD_B, 0) |
34 * FIELD_PREP(REG_FIELD_C, c) |
35 * FIELD_PREP(REG_FIELD_D, 0x40);
36 *
37 * Modify:
38 * reg &= ~REG_FIELD_C;
39 * reg |= FIELD_PREP(REG_FIELD_C, c);
40 */
41
42#define __bf_shf(x) (__builtin_ffsll(x) - 1)
43
44#define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \
45 ({ \
46 BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \
47 _pfx "mask is not constant"); \
Arnd Bergmanne36488c2018-08-17 15:43:36 -070048 BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero"); \
Jakub Kicinski3e9b3112016-08-31 12:46:44 +010049 BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \
50 ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
51 _pfx "value too large for the field"); \
52 BUILD_BUG_ON_MSG((_mask) > (typeof(_reg))~0ull, \
53 _pfx "type of reg too small for mask"); \
54 __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \
55 (1ULL << __bf_shf(_mask))); \
56 })
57
58/**
Alex Eldere31a5012020-03-12 11:44:27 -050059 * FIELD_MAX() - produce the maximum value representable by a field
60 * @_mask: shifted mask defining the field's length and position
61 *
62 * FIELD_MAX() returns the maximum value that can be held in the field
63 * specified by @_mask.
64 */
65#define FIELD_MAX(_mask) \
66 ({ \
67 __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: "); \
68 (typeof(_mask))((_mask) >> __bf_shf(_mask)); \
69 })
70
71/**
Jakub Kicinski16975992017-02-09 09:17:27 -080072 * FIELD_FIT() - check if value fits in the field
73 * @_mask: shifted mask defining the field's length and position
74 * @_val: value to test against the field
75 *
76 * Return: true if @_val can fit inside @_mask, false if @_val is too big.
77 */
78#define FIELD_FIT(_mask, _val) \
79 ({ \
Jakub Kicinski444da3f2020-08-10 11:21:11 -070080 __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: "); \
Jakub Kicinski16975992017-02-09 09:17:27 -080081 !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
82 })
83
84/**
Jakub Kicinski3e9b3112016-08-31 12:46:44 +010085 * FIELD_PREP() - prepare a bitfield element
86 * @_mask: shifted mask defining the field's length and position
87 * @_val: value to put in the field
88 *
89 * FIELD_PREP() masks and shifts up the value. The result should
90 * be combined with other fields of the bitfield using logical OR.
91 */
92#define FIELD_PREP(_mask, _val) \
93 ({ \
94 __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: "); \
95 ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \
96 })
97
98/**
99 * FIELD_GET() - extract a bitfield element
100 * @_mask: shifted mask defining the field's length and position
Masahiro Yamada72407672017-10-03 16:16:04 -0700101 * @_reg: value of entire bitfield
Jakub Kicinski3e9b3112016-08-31 12:46:44 +0100102 *
103 * FIELD_GET() extracts the field specified by @_mask from the
104 * bitfield passed in as @_reg by masking and shifting it down.
105 */
106#define FIELD_GET(_mask, _reg) \
107 ({ \
108 __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
109 (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
110 })
111
Johannes Berge7d4a952018-06-20 08:58:28 +0200112extern void __compiletime_error("value doesn't fit into mask")
Al Viro00b0c9b2017-12-14 21:27:55 -0500113__field_overflow(void);
114extern void __compiletime_error("bad bitfield mask")
115__bad_mask(void);
116static __always_inline u64 field_multiplier(u64 field)
117{
118 if ((field | (field - 1)) & ((field | (field - 1)) + 1))
119 __bad_mask();
120 return field & -field;
121}
122static __always_inline u64 field_mask(u64 field)
123{
124 return field / field_multiplier(field);
125}
Alex Eldere31a5012020-03-12 11:44:27 -0500126#define field_max(field) ((typeof(field))field_mask(field))
Al Viro00b0c9b2017-12-14 21:27:55 -0500127#define ____MAKE_OP(type,base,to,from) \
128static __always_inline __##type type##_encode_bits(base v, base field) \
129{ \
Johannes Berge7d4a952018-06-20 08:58:28 +0200130 if (__builtin_constant_p(v) && (v & ~field_mask(field))) \
131 __field_overflow(); \
Al Viro00b0c9b2017-12-14 21:27:55 -0500132 return to((v & field_mask(field)) * field_multiplier(field)); \
133} \
134static __always_inline __##type type##_replace_bits(__##type old, \
135 base val, base field) \
136{ \
137 return (old & ~to(field)) | type##_encode_bits(val, field); \
138} \
139static __always_inline void type##p_replace_bits(__##type *p, \
140 base val, base field) \
141{ \
142 *p = (*p & ~to(field)) | type##_encode_bits(val, field); \
143} \
144static __always_inline base type##_get_bits(__##type v, base field) \
145{ \
146 return (from(v) & field)/field_multiplier(field); \
147}
148#define __MAKE_OP(size) \
149 ____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu) \
150 ____MAKE_OP(be##size,u##size,cpu_to_be##size,be##size##_to_cpu) \
151 ____MAKE_OP(u##size,u##size,,)
Johannes Berg37a38622018-06-20 08:58:29 +0200152____MAKE_OP(u8,u8,,)
Al Viro00b0c9b2017-12-14 21:27:55 -0500153__MAKE_OP(16)
154__MAKE_OP(32)
155__MAKE_OP(64)
156#undef __MAKE_OP
157#undef ____MAKE_OP
158
Jakub Kicinski3e9b3112016-08-31 12:46:44 +0100159#endif