forked from mirror/openwrt
generic: backport new field_prep()/get() helper for non-const bitmask
Backport new field_prep()/get() particularly useful to handle case where a bitmask is not const and FIELD_PREP can't be used. This permit to replace manual shift with these macro. (also needed to permit backport of some patch without modification) Backport reworked patch that drop the local field_prep()/get() macro in favor of the global one. Link: https://github.com/openwrt/openwrt/pull/22479 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
This commit is contained in:
parent
9b21478d96
commit
25f92aaae1
6 changed files with 350 additions and 0 deletions
|
|
@ -0,0 +1,103 @@
|
|||
From 2a6c045640c38a407a39cd40c3c4d8dd2fd89aa8 Mon Sep 17 00:00:00 2001
|
||||
From: Geert Uytterhoeven <geert+renesas@glider.be>
|
||||
Date: Thu, 6 Nov 2025 14:34:00 +0100
|
||||
Subject: [PATCH 1/2] bitfield: Add less-checking __FIELD_{GET,PREP}()
|
||||
|
||||
The BUILD_BUG_ON_MSG() check against "~0ull" works only with "unsigned
|
||||
(long) long" _mask types. For constant masks, that condition is usually
|
||||
met, as GENMASK() yields an UL value. The few places where the
|
||||
constant mask is stored in an intermediate variable were fixed by
|
||||
changing the variable type to u64 (see e.g. [1] and [2]).
|
||||
|
||||
However, for non-constant masks, smaller unsigned types should be valid,
|
||||
too, but currently lead to "result of comparison of constant
|
||||
18446744073709551615 with expression of type ... is always
|
||||
false"-warnings with clang and W=1.
|
||||
|
||||
Hence refactor the __BF_FIELD_CHECK() helper, and factor out
|
||||
__FIELD_{GET,PREP}(). The later lack the single problematic check, but
|
||||
are otherwise identical to FIELD_{GET,PREP}(), and are intended to be
|
||||
used in the fully non-const variants later.
|
||||
|
||||
[1] commit 5c667d5a5a3ec166 ("clk: sp7021: Adjust width of _m in
|
||||
HWM_FIELD_PREP()")
|
||||
[2] commit cfd6fb45cfaf46fa ("crypto: ccree - avoid out-of-range
|
||||
warnings from clang")
|
||||
|
||||
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
|
||||
Link: https://git.kernel.org/torvalds/c/5c667d5a5a3ec166 [1]
|
||||
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
|
||||
---
|
||||
include/linux/bitfield.h | 36 ++++++++++++++++++++++++++++--------
|
||||
1 file changed, 28 insertions(+), 8 deletions(-)
|
||||
|
||||
--- a/include/linux/bitfield.h
|
||||
+++ b/include/linux/bitfield.h
|
||||
@@ -60,7 +60,7 @@
|
||||
|
||||
#define __bf_cast_unsigned(type, x) ((__unsigned_scalar_typeof(type))(x))
|
||||
|
||||
-#define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \
|
||||
+#define __BF_FIELD_CHECK_MASK(_mask, _val, _pfx) \
|
||||
({ \
|
||||
BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \
|
||||
_pfx "mask is not constant"); \
|
||||
@@ -69,13 +69,33 @@
|
||||
~((_mask) >> __bf_shf(_mask)) & \
|
||||
(0 + (_val)) : 0, \
|
||||
_pfx "value too large for the field"); \
|
||||
- BUILD_BUG_ON_MSG(__bf_cast_unsigned(_mask, _mask) > \
|
||||
- __bf_cast_unsigned(_reg, ~0ull), \
|
||||
- _pfx "type of reg too small for mask"); \
|
||||
__BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \
|
||||
(1ULL << __bf_shf(_mask))); \
|
||||
})
|
||||
|
||||
+#define __BF_FIELD_CHECK_REG(mask, reg, pfx) \
|
||||
+ BUILD_BUG_ON_MSG(__bf_cast_unsigned(mask, mask) > \
|
||||
+ __bf_cast_unsigned(reg, ~0ull), \
|
||||
+ pfx "type of reg too small for mask")
|
||||
+
|
||||
+#define __BF_FIELD_CHECK(mask, reg, val, pfx) \
|
||||
+ ({ \
|
||||
+ __BF_FIELD_CHECK_MASK(mask, val, pfx); \
|
||||
+ __BF_FIELD_CHECK_REG(mask, reg, pfx); \
|
||||
+ })
|
||||
+
|
||||
+#define __FIELD_PREP(mask, val, pfx) \
|
||||
+ ({ \
|
||||
+ __BF_FIELD_CHECK_MASK(mask, val, pfx); \
|
||||
+ ((typeof(mask))(val) << __bf_shf(mask)) & (mask); \
|
||||
+ })
|
||||
+
|
||||
+#define __FIELD_GET(mask, reg, pfx) \
|
||||
+ ({ \
|
||||
+ __BF_FIELD_CHECK_MASK(mask, 0U, pfx); \
|
||||
+ (typeof(mask))(((reg) & (mask)) >> __bf_shf(mask)); \
|
||||
+ })
|
||||
+
|
||||
/**
|
||||
* FIELD_MAX() - produce the maximum value representable by a field
|
||||
* @_mask: shifted mask defining the field's length and position
|
||||
@@ -112,8 +132,8 @@
|
||||
*/
|
||||
#define FIELD_PREP(_mask, _val) \
|
||||
({ \
|
||||
- __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: "); \
|
||||
- ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \
|
||||
+ __BF_FIELD_CHECK_REG(_mask, 0ULL, "FIELD_PREP: "); \
|
||||
+ __FIELD_PREP(_mask, _val, "FIELD_PREP: "); \
|
||||
})
|
||||
|
||||
#define __BF_CHECK_POW2(n) BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0)
|
||||
@@ -152,8 +172,8 @@
|
||||
*/
|
||||
#define FIELD_GET(_mask, _reg) \
|
||||
({ \
|
||||
- __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
|
||||
- (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
|
||||
+ __BF_FIELD_CHECK_REG(_mask, _reg, "FIELD_GET: "); \
|
||||
+ __FIELD_GET(_mask, _reg, "FIELD_GET: "); \
|
||||
})
|
||||
|
||||
extern void __compiletime_error("value doesn't fit into mask")
|
||||
|
|
@ -0,0 +1,114 @@
|
|||
From c1c6ab80b25c8db1e2ef5ae3ac8075d2c242ae13 Mon Sep 17 00:00:00 2001
|
||||
From: Geert Uytterhoeven <geert+renesas@glider.be>
|
||||
Date: Thu, 6 Nov 2025 14:34:01 +0100
|
||||
Subject: [PATCH 2/2] bitfield: Add non-constant field_{prep,get}() helpers
|
||||
MIME-Version: 1.0
|
||||
Content-Type: text/plain; charset=UTF-8
|
||||
Content-Transfer-Encoding: 8bit
|
||||
|
||||
The existing FIELD_{GET,PREP}() macros are limited to compile-time
|
||||
constants. However, it is very common to prepare or extract bitfield
|
||||
elements where the bitfield mask is not a compile-time constant.
|
||||
|
||||
To avoid this limitation, the AT91 clock driver and several other
|
||||
drivers already have their own non-const field_{prep,get}() macros.
|
||||
Make them available for general use by adding them to
|
||||
<linux/bitfield.h>, and improve them slightly:
|
||||
1. Avoid evaluating macro parameters more than once,
|
||||
2. Replace "ffs() - 1" by "__ffs()",
|
||||
3. Support 64-bit use on 32-bit architectures,
|
||||
4. Wire field_{get,prep}() to FIELD_{GET,PREP}() when mask is
|
||||
actually constant.
|
||||
|
||||
This is deliberately not merged into the existing FIELD_{GET,PREP}()
|
||||
macros, as people expressed the desire to keep stricter variants for
|
||||
increased safety, or for performance critical paths.
|
||||
|
||||
Yury: use __mask withing new macros.
|
||||
|
||||
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
|
||||
Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
|
||||
Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
|
||||
Acked-by: Crt Mori <cmo@melexis.com>
|
||||
Acked-by: Nuno Sá <nuno.sa@analog.com>
|
||||
Acked-by: Richard Genoud <richard.genoud@bootlin.com>
|
||||
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
|
||||
Reviewed-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
|
||||
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
|
||||
---
|
||||
include/linux/bitfield.h | 59 ++++++++++++++++++++++++++++++++++++++++
|
||||
1 file changed, 59 insertions(+)
|
||||
|
||||
--- a/include/linux/bitfield.h
|
||||
+++ b/include/linux/bitfield.h
|
||||
@@ -16,6 +16,7 @@
|
||||
* FIELD_{GET,PREP} macros take as first parameter shifted mask
|
||||
* from which they extract the base mask and shift amount.
|
||||
* Mask must be a compilation time constant.
|
||||
+ * field_{get,prep} are variants that take a non-const mask.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
@@ -223,4 +224,62 @@ __MAKE_OP(64)
|
||||
#undef __MAKE_OP
|
||||
#undef ____MAKE_OP
|
||||
|
||||
+#define __field_prep(mask, val) \
|
||||
+ ({ \
|
||||
+ __auto_type __mask = (mask); \
|
||||
+ typeof(__mask) __val = (val); \
|
||||
+ unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \
|
||||
+ __ffs(__mask) : __ffs64(__mask); \
|
||||
+ (__val << __shift) & __mask; \
|
||||
+ })
|
||||
+
|
||||
+#define __field_get(mask, reg) \
|
||||
+ ({ \
|
||||
+ __auto_type __mask = (mask); \
|
||||
+ typeof(__mask) __reg = (reg); \
|
||||
+ unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \
|
||||
+ __ffs(__mask) : __ffs64(__mask); \
|
||||
+ (__reg & __mask) >> __shift; \
|
||||
+ })
|
||||
+
|
||||
+/**
|
||||
+ * field_prep() - prepare a bitfield element
|
||||
+ * @mask: shifted mask defining the field's length and position, must be
|
||||
+ * non-zero
|
||||
+ * @val: value to put in the field
|
||||
+ *
|
||||
+ * Return: field value masked and shifted to its final destination
|
||||
+ *
|
||||
+ * field_prep() masks and shifts up the value. The result should be
|
||||
+ * combined with other fields of the bitfield using logical OR.
|
||||
+ * Unlike FIELD_PREP(), @mask is not limited to a compile-time constant.
|
||||
+ * Typical usage patterns are a value stored in a table, or calculated by
|
||||
+ * shifting a constant by a variable number of bits.
|
||||
+ * If you want to ensure that @mask is a compile-time constant, please use
|
||||
+ * FIELD_PREP() directly instead.
|
||||
+ */
|
||||
+#define field_prep(mask, val) \
|
||||
+ (__builtin_constant_p(mask) ? __FIELD_PREP(mask, val, "field_prep: ") \
|
||||
+ : __field_prep(mask, val))
|
||||
+
|
||||
+/**
|
||||
+ * field_get() - extract a bitfield element
|
||||
+ * @mask: shifted mask defining the field's length and position, must be
|
||||
+ * non-zero
|
||||
+ * @reg: value of entire bitfield
|
||||
+ *
|
||||
+ * Return: extracted field value
|
||||
+ *
|
||||
+ * field_get() extracts the field specified by @mask from the
|
||||
+ * bitfield passed in as @reg by masking and shifting it down.
|
||||
+ * Unlike FIELD_GET(), @mask is not limited to a compile-time constant.
|
||||
+ * Typical usage patterns are a value stored in a table, or calculated by
|
||||
+ * shifting a constant by a variable number of bits.
|
||||
+ * If you want to ensure that @mask is a compile-time constant, please use
|
||||
+ * FIELD_GET() directly instead.
|
||||
+ */
|
||||
+#define field_get(mask, reg) \
|
||||
+ (__builtin_constant_p(mask) ? __FIELD_GET(mask, reg, "field_get: ") \
|
||||
+ : __field_get(mask, reg))
|
||||
+
|
||||
#endif
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
From b1cff2f4b2391a13bd3e9263502072df1ee5d035 Mon Sep 17 00:00:00 2001
|
||||
From: Geert Uytterhoeven <geert+renesas@glider.be>
|
||||
Date: Thu, 6 Nov 2025 14:34:10 +0100
|
||||
Subject: [PATCH] ALSA: usb-audio: Convert to common field_{get,prep}() helpers
|
||||
|
||||
Drop the driver-specific field_get() and field_prep() macros, in favor
|
||||
of the globally available variants from <linux/bitfield.h>.
|
||||
|
||||
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
|
||||
Acked-by: Takashi Iwai <tiwai@suse.de>
|
||||
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
|
||||
---
|
||||
sound/usb/mixer_quirks.c | 6 ------
|
||||
1 file changed, 6 deletions(-)
|
||||
|
||||
--- a/sound/usb/mixer_quirks.c
|
||||
+++ b/sound/usb/mixer_quirks.c
|
||||
@@ -3367,10 +3367,6 @@ static int snd_bbfpro_controls_create(st
|
||||
#define RME_DIGIFACE_REGISTER(reg, mask) (((reg) << 16) | (mask))
|
||||
#define RME_DIGIFACE_INVERT BIT(31)
|
||||
|
||||
-/* Nonconst helpers */
|
||||
-#define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
|
||||
-#define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
|
||||
-
|
||||
static int snd_rme_digiface_write_reg(struct snd_kcontrol *kcontrol, int item, u16 mask, u16 val)
|
||||
{
|
||||
struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
From 0f8407a1f1c795c417e4c7750654a6024a3ec68b Mon Sep 17 00:00:00 2001
|
||||
From: Geert Uytterhoeven <geert+renesas@glider.be>
|
||||
Date: Thu, 6 Nov 2025 14:34:02 +0100
|
||||
Subject: [PATCH] clk: at91: Convert to common field_{get,prep}() helpers
|
||||
|
||||
Drop the driver-specific field_get() and field_prep() macros, in favor
|
||||
of the globally available variants from <linux/bitfield.h>.
|
||||
|
||||
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
|
||||
Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
|
||||
Acked-by: Stephen Boyd <sboyd@kernel.org>
|
||||
Acked-by: Claudiu Beznea <claudiu.beznea@tuxon.dev>
|
||||
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
|
||||
---
|
||||
drivers/clk/at91/clk-peripheral.c | 1 +
|
||||
drivers/clk/at91/pmc.h | 5 -----
|
||||
2 files changed, 1 insertion(+), 5 deletions(-)
|
||||
|
||||
--- a/drivers/clk/at91/clk-peripheral.c
|
||||
+++ b/drivers/clk/at91/clk-peripheral.c
|
||||
@@ -3,6 +3,7 @@
|
||||
* Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
|
||||
*/
|
||||
|
||||
+#include <linux/bitfield.h>
|
||||
#include <linux/bitops.h>
|
||||
#include <linux/clk-provider.h>
|
||||
#include <linux/clkdev.h>
|
||||
--- a/drivers/clk/at91/pmc.h
|
||||
+++ b/drivers/clk/at91/pmc.h
|
||||
@@ -116,9 +116,6 @@ struct at91_clk_pms {
|
||||
unsigned int parent;
|
||||
};
|
||||
|
||||
-#define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
|
||||
-#define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
|
||||
-
|
||||
#define ndck(a, s) (a[s - 1].id + 1)
|
||||
#define nck(a) (a[ARRAY_SIZE(a) - 1].id + 1)
|
||||
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
From 1fe1c28a108e4953f083c0106575ee0eccc296ae Mon Sep 17 00:00:00 2001
|
||||
From: Geert Uytterhoeven <geert+renesas@glider.be>
|
||||
Date: Thu, 6 Nov 2025 14:34:07 +0100
|
||||
Subject: [PATCH] iio: mlx90614: Convert to common field_{get,prep}() helpers
|
||||
|
||||
Drop the driver-specific field_get() and field_prep() macros, in favor
|
||||
of the globally available variants from <linux/bitfield.h>.
|
||||
|
||||
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
|
||||
Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
|
||||
Acked-by: Crt Mori <cmo@melexis.com>
|
||||
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
|
||||
---
|
||||
drivers/iio/temperature/mlx90614.c | 7 +------
|
||||
1 file changed, 1 insertion(+), 6 deletions(-)
|
||||
|
||||
--- a/drivers/iio/temperature/mlx90614.c
|
||||
+++ b/drivers/iio/temperature/mlx90614.c
|
||||
@@ -22,6 +22,7 @@
|
||||
* the "wakeup" GPIO is not given, power management will be disabled.
|
||||
*/
|
||||
|
||||
+#include <linux/bitfield.h>
|
||||
#include <linux/delay.h>
|
||||
#include <linux/err.h>
|
||||
#include <linux/gpio/consumer.h>
|
||||
@@ -68,10 +69,6 @@
|
||||
#define MLX90614_CONST_SCALE 20 /* Scale in milliKelvin (0.02 * 1000) */
|
||||
#define MLX90614_CONST_FIR 0x7 /* Fixed value for FIR part of low pass filter */
|
||||
|
||||
-/* Non-constant mask variant of FIELD_GET() and FIELD_PREP() */
|
||||
-#define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
|
||||
-#define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
|
||||
-
|
||||
struct mlx_chip_info {
|
||||
/* EEPROM offsets with 16-bit data, MSB first */
|
||||
/* emissivity correction coefficient */
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
From bb0e7fda87753a973cb4a86c22905b1177f00d4e Mon Sep 17 00:00:00 2001
|
||||
From: Geert Uytterhoeven <geert+renesas@glider.be>
|
||||
Date: Thu, 6 Nov 2025 14:34:08 +0100
|
||||
Subject: [PATCH] pinctrl: ma35: Convert to common field_{get,prep}() helpers
|
||||
|
||||
Drop the driver-specific field_get() and field_prep() macros, in favor
|
||||
of the globally available variants from <linux/bitfield.h>.
|
||||
|
||||
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
|
||||
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
|
||||
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
|
||||
---
|
||||
drivers/pinctrl/nuvoton/pinctrl-ma35.c | 6 ------
|
||||
1 file changed, 6 deletions(-)
|
||||
|
||||
--- a/drivers/pinctrl/nuvoton/pinctrl-ma35.c
|
||||
+++ b/drivers/pinctrl/nuvoton/pinctrl-ma35.c
|
||||
@@ -81,10 +81,6 @@
|
||||
#define MVOLT_1800 0
|
||||
#define MVOLT_3300 1
|
||||
|
||||
-/* Non-constant mask variant of FIELD_GET() and FIELD_PREP() */
|
||||
-#define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
|
||||
-#define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
|
||||
-
|
||||
static const char * const gpio_group_name[] = {
|
||||
"gpioa", "gpiob", "gpioc", "gpiod", "gpioe", "gpiof", "gpiog",
|
||||
"gpioh", "gpioi", "gpioj", "gpiok", "gpiol", "gpiom", "gpion",
|
||||
Loading…
Add table
Reference in a new issue