summaryrefslogtreecommitdiffstats
path: root/include/linux/bitfield.h
diff options
context:
space:
mode:
authorJakub Kicinski <jakub.kicinski@netronome.com>2016-08-31 07:46:44 -0400
committerKalle Valo <kvalo@codeaurora.org>2016-09-09 05:09:24 -0400
commit3e9b3112ec74f192eaab976c3889e34255cae940 (patch)
tree8c0b1eb296017b9f33a234d615d3e0370ed80a37 /include/linux/bitfield.h
parent3935ccc14d2c68488bd96448fc073da48eaeebf0 (diff)
add basic register-field manipulation macros
Common approach to accessing register fields is to define structures or sets of macros containing mask and shift pair. Operations on the register are then performed as follows: field = (reg >> shift) & mask; reg &= ~(mask << shift); reg |= (field & mask) << shift; Defining shift and mask separately is tedious. Ivo van Doorn came up with an idea of computing them at compilation time based on a single shifted mask (later refined by Felix) which can be used like this: #define REG_FIELD 0x000ff000 field = FIELD_GET(REG_FIELD, reg); reg &= ~REG_FIELD; reg |= FIELD_PREP(REG_FIELD, field); FIELD_{GET,PREP} macros take care of finding out what the appropriate shift is based on compilation time ffs operation. GENMASK can be used to define registers (which is usually less error-prone and easier to match with datasheets). This approach is the most convenient I've seen so to limit code multiplication let's move the macros to a global header file. Attempts to use static inlines instead of macros failed due to false positive triggering of BUILD_BUG_ON()s, especially with GCC < 6.0. Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com> Reviewed-by: Dinan Gunawardena <dinan.gunawardena@netronome.com> Signed-off-by: Kalle Valo <kvalo@codeaurora.org>
Diffstat (limited to 'include/linux/bitfield.h')
-rw-r--r--include/linux/bitfield.h93
1 files changed, 93 insertions, 0 deletions
diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
new file mode 100644
index 000000000000..f6505d83069d
--- /dev/null
+++ b/include/linux/bitfield.h
@@ -0,0 +1,93 @@
1/*
2 * Copyright (C) 2014 Felix Fietkau <nbd@nbd.name>
3 * Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#ifndef _LINUX_BITFIELD_H
16#define _LINUX_BITFIELD_H
17
18#include <linux/bug.h>
19
20/*
21 * Bitfield access macros
22 *
23 * FIELD_{GET,PREP} macros take as first parameter shifted mask
24 * from which they extract the base mask and shift amount.
25 * Mask must be a compilation time constant.
26 *
27 * Example:
28 *
29 * #define REG_FIELD_A GENMASK(6, 0)
30 * #define REG_FIELD_B BIT(7)
31 * #define REG_FIELD_C GENMASK(15, 8)
32 * #define REG_FIELD_D GENMASK(31, 16)
33 *
34 * Get:
35 * a = FIELD_GET(REG_FIELD_A, reg);
36 * b = FIELD_GET(REG_FIELD_B, reg);
37 *
38 * Set:
39 * reg = FIELD_PREP(REG_FIELD_A, 1) |
40 * FIELD_PREP(REG_FIELD_B, 0) |
41 * FIELD_PREP(REG_FIELD_C, c) |
42 * FIELD_PREP(REG_FIELD_D, 0x40);
43 *
44 * Modify:
45 * reg &= ~REG_FIELD_C;
46 * reg |= FIELD_PREP(REG_FIELD_C, c);
47 */
48
49#define __bf_shf(x) (__builtin_ffsll(x) - 1)
50
51#define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \
52 ({ \
53 BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \
54 _pfx "mask is not constant"); \
55 BUILD_BUG_ON_MSG(!(_mask), _pfx "mask is zero"); \
56 BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \
57 ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
58 _pfx "value too large for the field"); \
59 BUILD_BUG_ON_MSG((_mask) > (typeof(_reg))~0ull, \
60 _pfx "type of reg too small for mask"); \
61 __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \
62 (1ULL << __bf_shf(_mask))); \
63 })
64
65/**
66 * FIELD_PREP() - prepare a bitfield element
67 * @_mask: shifted mask defining the field's length and position
68 * @_val: value to put in the field
69 *
70 * FIELD_PREP() masks and shifts up the value. The result should
71 * be combined with other fields of the bitfield using logical OR.
72 */
73#define FIELD_PREP(_mask, _val) \
74 ({ \
75 __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: "); \
76 ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \
77 })
78
79/**
80 * FIELD_GET() - extract a bitfield element
81 * @_mask: shifted mask defining the field's length and position
82 * @_reg: 32bit value of entire bitfield
83 *
84 * FIELD_GET() extracts the field specified by @_mask from the
85 * bitfield passed in as @_reg by masking and shifting it down.
86 */
87#define FIELD_GET(_mask, _reg) \
88 ({ \
89 __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
90 (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
91 })
92
93#endif