Non-const bitfield helpers
Subsystems (14)
Geert Uytterhoeven
maintainerv3 cover Non-const bitfield helpers
Hi all,
This is an updated subset of a patch series I sent more than 3 years ago[2].
<linux/bitfield.h> contains various helpers for accessing bitfields, as typically used in hardware registers for memory-mapped I/O blocks. These helpers ensure type safety, and deduce automatically shift values from mask values, avoiding mistakes due to inconsistent shifts and masks, and leading to a reduction in source code size.
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 introduced its own field_{prep,get}() macros. Hence my v1 series aimed to make them available for general use, and convert several drivers to the existing FIELD_{GET,PREP}() and the new field_{get,prep}() helpers.
Due to some pushback (mostly centered around using the typed {u*,be*,le*,...}_get_bits() macros instead, which of course would require making them work with non-constant masks first, too), this series was never applied, and became buried deep in my TODO haystack... However, several people still liked the idea: since v1, multiple copies of the field_{prep,get}() macros appeared upstream, and one more is queued for v6.15.
Hence I think it's time to revive and consolidate...
Changes compared to v2[1]:
- New patch "[PATCH v3 1/4] bitfield: Drop underscores from macro parameters",
- Add Acked-by,
- Drop underscores from macro parameters,
- Use __auto_type where possible,
- Correctly cast reg to the mask type,
- Introduces __val and __reg intermediates to simplify the actual operation,
- Drop unneeded parentheses,
- Clarify having both FIELD_{GET,PREP}() and field_{get,prep}(),
Changes compared to v1[2]:
- Cast val resp. reg to the mask type,
- Fix 64-bit use on 32-bit architectures,
- Convert new upstream users:
- drivers/crypto/intel/qat/qat_common/adf_gen4_pm_debugfs.c
- drivers/gpio/gpio-aspeed.c
- drivers/iio/temperature/mlx90614.c
- drivers/pinctrl/nuvoton/pinctrl-ma35.c
- sound/usb/mixer_quirks.c
- Convert new user queued in renesas-devel for v6.15:
- drivers/soc/renesas/rz-sysc.c
- Drop the last 14 RFC patches.
They can be updated/resubmitted/applied later.
I can take all four patches through the Renesas tree, and provide an immutable branch with the first two patches for the interested parties.
Thanks for your comments!
[1] "[PATCH v2 0/3] Non-const bitfield helpers"
https://lore.kernel.org/all/[email protected] [2] "[PATCH 00/17] Non-const bitfield helper conversions" https://lore.kernel.org/all/[email protected]
Geert Uytterhoeven (4):
bitfield: Drop underscores from macro parameters
bitfield: Add non-constant field_{prep,get}() helpers
clk: renesas: Use bitfield helpers
soc: renesas: Use bitfield helpers
drivers/clk/at91/clk-peripheral.c | 1 + drivers/clk/at91/pmc.h | 3 - drivers/clk/renesas/clk-div6.c | 6 +- drivers/clk/renesas/rcar-gen3-cpg.c | 15 +-- drivers/clk/renesas/rcar-gen4-cpg.c | 9 +- .../qat/qat_common/adf_gen4_pm_debugfs.c | 8 +- drivers/gpio/gpio-aspeed.c | 5 +- drivers/iio/temperature/mlx90614.c | 5 +- drivers/pinctrl/nuvoton/pinctrl-ma35.c | 4 - drivers/soc/renesas/renesas-soc.c | 4 +- drivers/soc/renesas/rz-sysc.c | 3 +- include/linux/bitfield.h | 122 ++++++++++++------ sound/usb/mixer_quirks.c | 4 - 13 files changed, 97 insertions(+), 92 deletions(-)
Signature · 12 lines
2.43.0
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus TorvaldsGeert Uytterhoeven
maintainerPATCH v3 1/4 bitfield: Drop underscores from macro parameters
There is no need to prefix macro parameters with underscores. Remove the underscores.
---
v3:
- New.
--- include/linux/bitfield.h | 86 ++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 43 deletions(-)
include/linux/bitfield.h +43 -43
@@ -60,68 +60,68 @@#define __bf_cast_unsigned (type, x) ((__unsigned_scalar_typeof(type))(x)) -#define __BF_FIELD_CHECK (_mask, _reg, _val, _pfx) \ +#define __BF_FIELD_CHECK (mask, reg, val, pfx) \ ({ \ - BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \ - _pfx "mask is not constant"); \ - BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero"); \ - BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \ - ~((_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))); \ + BUILD_BUG_ON_MSG(!__builtin_constant_p(mask), \ + pfx "mask is not constant"); \ + BUILD_BUG_ON_MSG((mask) == 0, pfx "mask is zero"); \ + BUILD_BUG_ON_MSG(__builtin_constant_p(val) ? \ + ~((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))); \ })/** * FIELD_MAX() - produce the maximum value representable by a field -* @_mask: shifted mask defining the field's length and position +* @mask: shifted mask defining the field's length and position * * FIELD_MAX() returns the maximum value that can be held in the field -* specified by @_mask. +* specified by @mask. */ -#define FIELD_MAX (_mask) \ +#define FIELD_MAX (mask) \ ({ \ - __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: "); \ - (typeof(_mask))((_mask) >> __bf_shf(_mask)); \ + __BF_FIELD_CHECK(mask, 0ULL, 0ULL, "FIELD_MAX: "); \ + (typeof(mask))((mask) >> __bf_shf(mask)); \ })/** * FIELD_FIT() - check if value fits in the field -* @_mask: shifted mask defining the field's length and position -* @_val: value to test against the field +* @mask: shifted mask defining the field's length and position +* @val: value to test against the field * -* Return: true if @_val can fit inside @_mask, false if @_val is too big. +* Return: true if @val can fit inside @mask, false if @val is too big. */ -#define FIELD_FIT (_mask, _val) \ +#define FIELD_FIT (mask, val) \ ({ \ - __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: "); \ - !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \ + __BF_FIELD_CHECK(mask, 0ULL, 0ULL, "FIELD_FIT: "); \ + !((((typeof(mask))val) << __bf_shf(mask)) & ~(mask)); \ })/** * FIELD_PREP() - prepare a bitfield element -* @_mask: shifted mask defining the field's length and position -* @_val: value to put in the field +* @mask: shifted mask defining the field's length and position +* @val: value to put in the field * * FIELD_PREP() masks and shifts up the value. The result should * be combined with other fields of the bitfield using logical OR. */ -#define FIELD_PREP (_mask, _val) \ +#define FIELD_PREP (mask, val) \ ({ \ - __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: "); \ - ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \ + __BF_FIELD_CHECK(mask, 0ULL, val, "FIELD_PREP: "); \ + ((typeof(mask))(val) << __bf_shf(mask)) & (mask); \ })#define __BF_CHECK_POW2 (n) BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0)/** * FIELD_PREP_CONST() - prepare a constant bitfield element -* @_mask: shifted mask defining the field's length and position -* @_val: value to put in the field +* @mask: shifted mask defining the field's length and position +* @val: value to put in the field * * FIELD_PREP_CONST() masks and shifts up the value. The result should * be combined with other fields of the bitfield using logical OR.
@@ -130,30 +130,30 @@* be used in initializers. Error checking is less comfortable for this * version, and non-constant masks cannot be used. */ -#define FIELD_PREP_CONST (_mask, _val) \ +#define FIELD_PREP_CONST (mask, val) \ ( \/* mask must be non-zero */ \ - BUILD_BUG_ON_ZERO((_mask) == 0) + \ + BUILD_BUG_ON_ZERO((mask) == 0) + \ /* check if value fits */ \ - BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \ + BUILD_BUG_ON_ZERO(~((mask) >> __bf_shf(mask)) & (val)) + \ /* check if mask is contiguous */ \ - __BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask))) + \ + __BF_CHECK_POW2((mask) + (1ULL << __bf_shf(mask))) + \ /* and create the value */ \ - (((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask)) \ + (((typeof(mask))(val) << __bf_shf(mask)) & (mask)) \ )/** * FIELD_GET() - extract a bitfield element -* @_mask: shifted mask defining the field's length and position -* @_reg: value of entire bitfield +* @mask: shifted mask defining the field's length and position +* @reg: value of entire bitfield * -* FIELD_GET() extracts the field specified by @_mask from the -* bitfield passed in as @_reg by masking and shifting it down. +* FIELD_GET() extracts the field specified by @mask from the +* bitfield passed in as @reg by masking and shifting it down. */ -#define FIELD_GET (_mask, _reg) \ +#define FIELD_GET (mask, reg) \ ({ \ - __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \ - (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \ + __BF_FIELD_CHECK(mask, reg, 0U, "FIELD_GET: "); \ + (typeof(mask))(((reg) & (mask)) >> __bf_shf(mask)); \ }) extern void __compiletime_error("value doesn't fit into mask")
Signature · 1 line
2.43.0
Geert Uytterhoeven
maintainerPATCH v3 2/4 bitfield: Add non-constant field_{prep,get}() helpers
In reply to Geert UytterhoevenThe 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 consolidating them in <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.
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.
---
v3:
- Add Acked-by,
- Drop underscores from macro parameters,
- Use __auto_type where possible,
- Correctly cast reg to the mask type,
- Introduces __val and __reg intermediates to simplify the actual operation,
- Drop unneeded parentheses,
- Clarify having both FIELD_{GET,PREP}() and field_{get,prep}(),
v2:
- Cast val resp. reg to the mask type,
- Fix 64-bit use on 32-bit architectures,
- Convert new upstream users:
- drivers/crypto/intel/qat/qat_common/adf_gen4_pm_debugfs.c
- drivers/gpio/gpio-aspeed.c
- drivers/iio/temperature/mlx90614.c
- drivers/pinctrl/nuvoton/pinctrl-ma35.c
- sound/usb/mixer_quirks.c
- Convert new user queued in renesas-devel for v6.15:
- drivers/soc/renesas/rz-sysc.c
--- drivers/clk/at91/clk-peripheral.c | 1 + drivers/clk/at91/pmc.h | 3 -- .../qat/qat_common/adf_gen4_pm_debugfs.c | 8 +---- drivers/gpio/gpio-aspeed.c | 5 +-- drivers/iio/temperature/mlx90614.c | 5 +-- drivers/pinctrl/nuvoton/pinctrl-ma35.c | 4 --- drivers/soc/renesas/rz-sysc.c | 3 +- include/linux/bitfield.h | 36 +++++++++++++++++++ sound/usb/mixer_quirks.c | 4 --- 9 files changed, 41 insertions(+), 28 deletions(-)
drivers/clk/at91/clk-peripheral.c +1 -0
@@ -3,6 +3,7 @@* Copyright (C) 2013 Boris BREZILLON <[email protected]> */ +#include <linux/bitfield.h> #include <linux/bitops.h> #include <linux/clk-provider.h> #include <linux/clkdev.h>
drivers/clk/at91/pmc.h +0 -3
@@ -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)
drivers/crypto/intel/qat/qat_common/adf_gen4_pm_debugfs.c +1 -7
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only /* Copyright(c) 2023 Intel Corporation */ +#include <linux/bitfield.h> #include <linux/dma-mapping.h> #include <linux/kernel.h> #include <linux/string_helpers.h>
@@ -11,13 +12,6 @@#include "adf_gen4_pm.h" #include "icp_qat_fw_init_admin.h" -/* -* This is needed because a variable is used to index the mask at -* pm_scnprint_table(), making it not compile time constant, so the compile -* asserts from FIELD_GET() or u32_get_bits() won't be fulfilled. -*/ -#define field_get (_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1)) -#define PM_INFO_MEMBER_OFF (member) \ (offsetof(struct icp_qat_fw_init_admin_pm_info, member) / sizeof(u32))
drivers/gpio/gpio-aspeed.c +1 -4
@@ -5,6 +5,7 @@* Joel Stanley <[email protected]> */ +#include <linux/bitfield.h> #include <linux/clk.h> #include <linux/gpio/aspeed.h> #include <linux/gpio/driver.h>
@@ -30,10 +31,6 @@#include <linux/gpio/consumer.h> #include "gpiolib.h" -/* 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)) -#define GPIO_G7_IRQ_STS_BASE 0x100#define GPIO_G7_IRQ_STS_OFFSET (x) (GPIO_G7_IRQ_STS_BASE + (x) * 0x4)#define GPIO_G7_CTRL_REG_BASE 0x180
drivers/iio/temperature/mlx90614.c +1 -4
@@ -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 */
drivers/pinctrl/nuvoton/pinctrl-ma35.c +0 -4
@@ -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" ,
drivers/soc/renesas/rz-sysc.c +1 -2
@@ -5,6 +5,7 @@* Copyright (C) 2024 Renesas Electronics Corp. */ +#include <linux/bitfield.h> #include <linux/io.h> #include <linux/of.h> #include <linux/platform_device.h>
@@ -12,8 +13,6 @@#include "rz-sysc.h" -#define field_get (_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1)) -/** * struct rz_sysc - RZ SYSC private data structure * @base: SYSC base address
include/linux/bitfield.h +36 -0
@@ -203,4 +203,40 @@ __MAKE_OP(64)#undef __MAKE_OP#undef ____MAKE_OP +/** +* field_prep() - prepare a bitfield element +* @mask: shifted mask defining the field's length and position +* @val: value to put in the field +* +* 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. +*/ +#define field_prep (mask, val) \ + ({ \ + __auto_type __mask = (mask); \ + typeof(mask) __val = (val); \ + unsigned int __shift = sizeof(mask) <= 4 ? \ + __ffs(__mask) : __ffs64(__mask); \ + (__val << __shift) & __mask; \ + }) + +/** +* field_get() - extract a bitfield element +* @mask: shifted mask defining the field's length and position +* @reg: value of entire bitfield +* +* 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. +*/ +#define field_get (mask, reg) \ + ({ \ + __auto_type __mask = (mask); \ + typeof(mask) __reg = (reg); \ + unsigned int __shift = sizeof(mask) <= 4 ? \ + __ffs(__mask) : __ffs64(__mask); \ + (__reg & __mask) >> __shift; \ + }) +#endif
sound/usb/mixer_quirks.c +0 -4
@@ -3110,10 +3110,6 @@ static int snd_bbfpro_controls_create(struct usb_mixer_interface *mixer)#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);
Signature · 1 line
2.43.0
Re: [PATCH treewide v3 2/4] bitfield: Add non-constant field_{prep,get}() helpers
It seems equivalent change, but I do not have chip nearby to do a quick test for mlx90614 so for that part:
Thanks for your contribution.
Quoted 250 lines · Geert Uytterhoeven
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 consolidating them in <
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.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.
Signed-off-by Geert Uytterhoeven <[email protected]>Acked-by Jonathan Cameron <[email protected]>---
v3:
- Add Acked-by,
- Drop underscores from macro parameters,
- Use__auto_typewhere possible,
- Correctly cast reg to the mask type,
- Introduces__valand__regintermediates to simplify the actual operation,
- Drop unneeded parentheses,
- Clarify having both FIELD_{GET,PREP}() and field_{get,prep}(),v2:
- Cast val resp. reg to the mask type,
- Fix 64-bit use on 32-bit architectures,
- Convert new upstream users:
-drivers/crypto/intel/qat/qat_common/adf_gen4_pm_debugfs.c
-drivers/gpio/gpio-aspeed.c
-drivers/iio/temperature/mlx90614.c
-drivers/pinctrl/nuvoton/pinctrl-ma35.c
-sound/usb/mixer_quirks.c
- Convert new user queued in renesas-devel for v6.15:
-drivers/soc/renesas/rz-sysc.c--- drivers/clk/at91/clk-peripheral.c | 1 + drivers/clk/at91/pmc.h | 3 -- .../qat/qat_common/adf_gen4_pm_debugfs.c | 8 +---- drivers/gpio/gpio-aspeed.c | 5 +-- drivers/iio/temperature/mlx90614.c | 5 +-- drivers/pinctrl/nuvoton/pinctrl-ma35.c | 4 --- drivers/soc/renesas/rz-sysc.c | 3 +- include/linux/bitfield.h | 36 +++++++++++++++++++ sound/usb/mixer_quirks.c | 4 --- 9 files changed, 41 insertions(+), 28 deletions(-)drivers/clk/at91/clk-peripheral.c +1
-0@@ -3,6 +3,7 @@* Copyright (C) 2013 Boris BREZILLON <[email protected]> */ +#include <linux/bitfield.h> #include <linux/bitops.h> #include <linux/clk-provider.h> #include <linux/clkdev.h> drivers/clk/at91/pmc.h +0
-3@@ -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)drivers/crypto/intel/qat/qat_common/adf_gen4_pm_debugfs.c +1
-7@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only /* Copyright(c) 2023 Intel Corporation */ +#include <linux/bitfield.h> #include <linux/dma-mapping.h> #include <linux/kernel.h> #include <linux/string_helpers.h> @@ -11,13 +12,6 @@#include "adf_gen4_pm.h" #include "icp_qat_fw_init_admin.h" -/* -* This is needed because a variable is used to index the mask at -* pm_scnprint_table(), making it not compile time constant, so the compile -* asserts from FIELD_GET() or u32_get_bits() won't be fulfilled. -*/ -#define field_get (_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1)) -#define PM_INFO_MEMBER_OFF (member) \ (offsetof(struct icp_qat_fw_init_admin_pm_info, member) / sizeof(u32))drivers/gpio/gpio-aspeed.c +1
-4@@ -5,6 +5,7 @@* Joel Stanley <[email protected]> */ +#include <linux/bitfield.h> #include <linux/clk.h> #include <linux/gpio/aspeed.h> #include <linux/gpio/driver.h> @@ -30,10 +31,6 @@#include <linux/gpio/consumer.h> #include "gpiolib.h" -/* 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)) -#define GPIO_G7_IRQ_STS_BASE 0x100#define GPIO_G7_IRQ_STS_OFFSET (x) (GPIO_G7_IRQ_STS_BASE + (x) * 0x4)#define GPIO_G7_CTRL_REG_BASE 0x180drivers/iio/temperature/mlx90614.c +1
-4@@ -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 */ drivers/pinctrl/nuvoton/pinctrl-ma35.c +0
-4@@ -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" ,drivers/soc/renesas/rz-sysc.c +1
-2@@ -5,6 +5,7 @@* Copyright (C) 2024 Renesas Electronics Corp. */ +#include <linux/bitfield.h> #include <linux/io.h> #include <linux/of.h> #include <linux/platform_device.h> @@ -12,8 +13,6 @@#include "rz-sysc.h" -#define field_get (_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1)) -/** * struct rz_sysc - RZ SYSC private data structure * @base: SYSC base address include/linux/bitfield.h +36
-0@@ -203,4 +203,40 @@ __MAKE_OP(64)#undef __MAKE_OP#undef ____MAKE_OP +/** +* field_prep() - prepare a bitfield element +* @mask: shifted mask defining the field's length and position +* @val: value to put in the field +* +* 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. +*/ +#define field_prep (mask, val) \ + ({ \ + __auto_type __mask = (mask); \ + typeof(mask) __val = (val); \ + unsigned int __shift = sizeof(mask) <= 4 ? \ + __ffs(__mask) : __ffs64(__mask); \ + (__val << __shift) & __mask; \ + }) + +/** +* field_get() - extract a bitfield element +* @mask: shifted mask defining the field's length and position +* @reg: value of entire bitfield +* +* 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. +*/ +#define field_get (mask, reg) \ + ({ \ + __auto_type __mask = (mask); \ + typeof(mask) __reg = (reg); \ + unsigned int __shift = sizeof(mask) <= 4 ? \ + __ffs(__mask) : __ffs64(__mask); \ + (__reg & __mask) >> __shift; \ + }) +#endif sound/usb/mixer_quirks.c +0
-5@@ -3110,10 +3110,6 @@ static int snd_bbfpro_controls_create(struct usb_mixer_interface *mixer)#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); --2.43.0
Re: [PATCH treewide v3 2/4] bitfield: Add non-constant field_{prep,get}() helpers
In reply to Geert UytterhoevenQuoted 12 lines · On Fri, 14 Feb 2025 14:55:51 +0100 Geert Uytterhoeven wrote:
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 consolidating them in <
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.
Quoted 3 lines
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.
I really really think that people should just use the static inline helpers if the field is not constant. And we should do something like below so that people can actually find them.
include/linux/bitfield.h +75 -1
@@ -156,6 +156,80 @@ (typeof (_mask))(((_reg) & (_mask)) >>__bf_shf (_mask)); \ }) +/** +* u32_encode_bits() - prepare a u32 bitfield element (non-const) +* @v: value to put in the field +* @field: shifted mask defining the field's length and position +* +* Equivalent of FIELD_PREP() for u32, field does not have to be constant. +* +* Note that the helper is available for other field widths (generated below). +*/ +static __always_inline __u32 u32_encode_bits(u32 v, u32 field) +{ +if (__builtin_constant_p (v) && (v & ~field_mask (field))) +__field_overflow (); +return ((v &field_mask (field)) *field_multiplier (field)); +} + +/** +* u32_replace_bits() - change a u32 bitfield element (non-const) +* @old: old u32 value to modify +* @val: value to put in the field +* @field: shifted mask defining the field's length and position +* +* Remove the current contents of the @field in @old and set it to @new. +* +* Note that the helper is available for other field widths (generated below). +*/ +static __always_inline __u32 u32_replace_bits(__u32 old, u32 val, u32 field) +{ +return (old & ~(field)) |u32_encode_bits (val, field); +} + +/** +* u32_get_bits() - get u32 bitfield element (non-const) +* @v: value to extract the field from +* @field: shifted mask defining the field's length and position +* +* Extract the value of the field and shift it down. +* +* Note that the helper is available for other field widths (generated below). +*/ +static __always_inline u32 u32_get_bits(__u32 v, u32 field) +{ +return ((v) & field) /field_multiplier (field); +} + +static __always_inline void u32p_replace_bits(__u32 * p, u32 val, u32 field) +{ + * p = ( * p & ~(field)) |u32_encode_bits (val, field); +} + +static __always_inline __le32 le32_encode_bits(u32 v, u32 field) +{ +if (__builtin_constant_p (v) && (v & ~field_mask (field))) +__field_overflow (); +return cpu_to_le32 ((v &field_mask (field)) *field_multiplier (field)); +} + +static __always_inline __le32 le32_replace_bits(__le32 old, u32 val, u32 field) +{ +return (old & ~cpu_to_le32 (field)) |le32_encode_bits (val, field); +} + +static __always_inline void le32p_replace_bits(__le32 * p, u32 val, u32 field) +{ + * p = ( * p & ~cpu_to_le32 (field)) |le32_encode_bits (val, field); +} + +static __always_inline u32 le32_get_bits(__le32 v, u32 field) +{ +return (le32_to_cpu (v) & field) /field_multiplier (field); +} + +/* Auto-generate bit ops for other field width and endian combination */ + extern void __compiletime_error("value doesn't fit into mask")__field_overflow (void); extern void __compiletime_error("bad bitfield mask")
@@ -198,7 +272,7 @@ static __always_inline base type##_get_bits(__##type v, base field) \ ____MAKE_OP(u##size,u##size,,) ____MAKE_OP(u8,u8,,) __MAKE_OP(16) -__MAKE_OP(32) +____MAKE_OP(be32,u32,cpu_to_be32,be32_to_cpu) /* Other 32b types open coded */ __MAKE_OP(64) #undef __MAKE_OP #undef ____MAKE_OP
Re: [PATCH treewide v3 2/4] bitfield: Add non-constant field_{prep,get}() helpers
Quoted 13 lines · On Fri, 14 Feb 2025 07:34:02 -0800 Jakub Kicinski <[email protected]> wrote:
Quoted 12 lines · On Fri, 14 Feb 2025 14:55:51 +0100 Geert Uytterhoeven wrote:
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 consolidating them in <
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.
Quoted 7 lines
Quoted 3 lines
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.
I really really think that people should just use the static inline helpers if the field is not constant. And we should do something like below so that people can actually find them.
Especially since you really don't want to be calling ffs() on variables.
Much better to have saved the low bit and field width/mask.
David
Re: [PATCH treewide v3 2/4] bitfield: Add non-constant field_{prep,get}() helpers
Hi David,
Quoted 19 lines · David Laight
Quoted 17 lines · On Fri, 14 Feb 2025 07:34:02 -0800 Jakub Kicinski <[email protected]> wrote:
Quoted 15 lines · On Fri, 14 Feb 2025 14:55:51 +0100 Geert Uytterhoeven wrote:
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 consolidating them in <
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.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.
Quoted 5 lines
Quoted 3 lines
I really really think that people should just use the static inline helpers if the field is not constant. And we should do something like below so that people can actually find them.
Especially since you really don't want to be calling
ffs()on variables.
It is not that bad, as most temporary architectures have an instruction for that.
Quoted 1 line
Much better to have saved the low bit and field width/mask.
While that would allow some space saving (only 10 or 12 bits needed to store low + width), gcc would generate quite some code to create the mask (even on PowerPC, where I expected a single instruction would do ;-).
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Re: [PATCH treewide v3 2/4] bitfield: Add non-constant field_{prep,get}() helpers
In reply to Jakub KicinskiHi Jakub,
Quoted 34 lines · Jakub Kicinski
Quoted 12 lines · On Fri, 14 Feb 2025 14:55:51 +0100 Geert Uytterhoeven wrote:
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 consolidating them in <
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.Quoted 3 lines
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.
I really really think that people should just use the static inline helpers if the field is not constant. And we should do something like below so that people can actually find them.
include/linux/bitfield.h +5
-0@@ -156,6 +156,80 @@ (typeof (_mask))(((_reg) & (_mask)) >>__bf_shf (_mask)); \ }) +/** +* u32_encode_bits() - prepare a u32 bitfield element (non-const) +* @v: value to put in the field +* @field: shifted mask defining the field's length and position +*
include/linux/bitfield.h line 173
Quoted 10 lines
include/linux/bitfield.h +10
-0+* Equivalent of FIELD_PREP() for u32, field does not have to be constant. +* +* Note that the helper is available for other field widths (generated below). +*/ +static __always_inline __u32 u32_encode_bits(u32 v, u32 field) +{ +if (__builtin_constant_p (v) && (v & ~field_mask (field))) +__field_overflow (); +return ((v &field_mask (field)) *field_multiplier (field)); +}
Unfortunately gcc emits actual divisions or __*div*() calls, and multiplications in the non-constant case.
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Re: [PATCH treewide v3 2/4] bitfield: Add non-constant field_{prep,get}() helpers
In reply to Geert UytterhoevenQuoted 12 lines · On Fri, 14 Feb 2025 14:55:51 +0100 Geert Uytterhoeven wrote:
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 consolidating them in <
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.
Quoted 3 lines
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.
We already have helpers for this, please just don't know they exist :/
The "const" version of the helpers are specifically defined to work on masks generated with BIT() and GENMASK(). If the mask is not constant we should expect it to have a well defined width.
I strongly prefer that we do this instead and convert the users to the fixed-width version:
---->8----------------
Subject: bitfield: open code the fixed-width non-const helpers so that people see them
There is a number of useful helpers defined in bitfield.h but they are mostly invisible to the reader because they are all generated by macros. Open code the 32b versions (which are most commonly used) to give developers a chance to discover them.
--- include/linux/bitfield.h | 82 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-)
include/linux/bitfield.h +81 -1
@@ -173,6 +173,11 @@ *(_reg_p) |= (((typeof (_mask))(_val) <<__bf_shf (_mask)) & (_mask)); \ }) +/* Non-constant, fixed-width helpers follow +* Open code u32 and le32 versions for documentation / visibility, +* be32 and other widths exist but are generated using macroes. +*/ + extern void __compiletime_error("value doesn't fit into mask")__field_overflow (void); extern void __compiletime_error("bad bitfield mask")
@@ -188,6 +193,81 @@ static __always_inline u64 field_mask(u64 field)return field /field_multiplier (field); }#define field_max (field) ((typeof(field))field_mask(field)) + +/** +* u32_encode_bits() - prepare a u32 bitfield element (non-const) +* @v: value to put in the field +* @field: shifted mask defining the field's length and position +* +* Equivalent of FIELD_PREP() for u32, field does not have to be constant. +* +* Note that the helper is available for other field widths (generated below). +*/ +static __always_inline __u32 u32_encode_bits(u32 v, u32 field) +{ +if (__builtin_constant_p (v) && (v & ~field_mask (field))) +__field_overflow (); +return ((v &field_mask (field)) *field_multiplier (field)); +} + +/** +* u32_replace_bits() - change a u32 bitfield element (non-const) +* @old: old u32 value to modify +* @val: value to put in the field +* @field: shifted mask defining the field's length and position +* +* Remove the current contents of the @field in @old and set it to @new. +* +* Note that the helper is available for other field widths (generated below). +*/ +static __always_inline __u32 u32_replace_bits(__u32 old, u32 val, u32 field) +{ +return (old & ~(field)) |u32_encode_bits (val, field); +} + +/** +* u32_get_bits() - get u32 bitfield element (non-const) +* @v: value to extract the field from +* @field: shifted mask defining the field's length and position +* +* Extract the value of the field and shift it down. +* +* Note that the helper is available for other field widths (generated below). +*/ +static __always_inline u32 u32_get_bits(__u32 v, u32 field) +{ +return ((v) & field) /field_multiplier (field); +} + +static __always_inline void u32p_replace_bits(__u32 *p, u32 val, u32 field) +{ + *p = (*p & ~(field)) |u32_encode_bits (val, field); +} + +static __always_inline __le32 le32_encode_bits(u32 v, u32 field) +{ +if (__builtin_constant_p (v) && (v & ~field_mask (field))) +__field_overflow (); +return cpu_to_le32 ((v &field_mask (field)) *field_multiplier (field)); +} + +static __always_inline __le32 le32_replace_bits(__le32 old, u32 val, u32 field) +{ +return (old & ~cpu_to_le32 (field)) |le32_encode_bits (val, field); +} + +static __always_inline void le32p_replace_bits(__le32 *p, u32 val, u32 field) +{ + *p = (*p & ~cpu_to_le32 (field)) |le32_encode_bits (val, field); +} + +static __always_inline u32 le32_get_bits(__le32 v, u32 field) +{ +return (le32_to_cpu (v) & field) /field_multiplier (field); +} + +/* Auto-generate bit ops for other field width and endian combination */ +#define ____MAKE_OP (type,base,to,from) \ static __always_inline __##type __must_check type##_encode_bits(base v, base field) \ { \
@@ -215,7 +295,7 @@ static __always_inline base __must_check type##_get_bits(__##type v, base field) ____MAKE_OP(u##size,u##size,,) ____MAKE_OP(u8,u8,,) __MAKE_OP(16) -__MAKE_OP(32) +____MAKE_OP(be32,u32,cpu_to_be32,be32_to_cpu) /* Other 32b types open coded */ __MAKE_OP(64) #undef __MAKE_OP #undef ____MAKE_OP
Signature · 1 line
2.51.0
Re: [PATCH treewide v3 2/4] bitfield: Add non-constant field_{prep,get}() helpers
Hi Jakub,
Quoted 28 lines · Jakub Kicinski
Quoted 12 lines · On Fri, 14 Feb 2025 14:55:51 +0100 Geert Uytterhoeven wrote:
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 consolidating them in <
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.Quoted 3 lines
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.
We already have helpers for this, please just don't know they exist :/
The "const" version of the helpers are specifically defined to work on masks generated with
BIT()andGENMASK(). If the mask is not constant we should expect it to have a well defined width.I strongly prefer that we do this instead and convert the users to the fixed-width version:
---->8----------------
Quoted 8 lines
Subject: bitfield: open code the fixed-width non-const helpers so that people see them
There is a number of useful helpers defined in
bitfield.hbut they are mostly invisible to the reader because they are all generated by macros. Open code the 32b versions (which are most commonly used) to give developers a chance to discover them.Signed-off-by Jakub Kicinski <[email protected]>
Thanks, but this is more or less the same code which you suggested before [1], and to which I just replied[2] after looking at the generated assembler output on various architectures.
Quoted 9 lines
include/linux/bitfield.h +5
-0@@ -188,6 +193,81 @@ static __always_inline u64 field_mask(u64 field)return field /field_multiplier (field); }#define field_max (field) ((typeof(field))field_mask(field)) + +/** +* u32_encode_bits() - prepare a u32 bitfield element (non-const) +* @v: value to put in the field +* @field: shifted mask defining the field's length and position
include/linux/bitfield.h static __always_inline u64 field_mask(u64 field) · line 210
Quoted 10 lines
include/linux/bitfield.h +10
-0+* +* Equivalent of FIELD_PREP() for u32, field does not have to be constant. +* +* Note that the helper is available for other field widths (generated below). +*/ +static __always_inline __u32 u32_encode_bits(u32 v, u32 field) +{ +if (__builtin_constant_p (v) && (v & ~field_mask (field))) +__field_overflow (); +return ((v &field_mask (field)) *field_multiplier (field));
Unfortunately gcc emits actual divisions or __*div*() calls, and multiplications in the non-constant case.
So I don't think this is suitable as-is.
Review static __always_inline u64 field_mask(u64 field) · line 211
Quoted 1 line
(quoted diff) +1
-0+}
[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/CAMuHMdU+0HGG22FbO3wNmXtbUm9RhTopYrGghF6UrkFu-iww2A@mail.gmail.com
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH treewide v3 2/4] bitfield: Add non-constant field_{prep,get}() helpers
Quoted 18 lines · On Fri, 17 Oct 2025 18:00:41 +0200 Geert Uytterhoeven wrote:
Quoted 4 lines
include/linux/bitfield.h +4
-0+/** +* u32_encode_bits() - prepare a u32 bitfield element (non-const) +* @v: value to put in the field +* @field: shifted mask defining the field's length and position include/linux/bitfield.h static __always_inline u64 field_mask(u64 field) · line 210
Quoted 10 lines
include/linux/bitfield.h +10
-0+* +* Equivalent of FIELD_PREP() for u32, field does not have to be constant. +* +* Note that the helper is available for other field widths (generated below). +*/ +static __always_inline __u32 u32_encode_bits(u32 v, u32 field) +{ +if (__builtin_constant_p (v) && (v & ~field_mask (field))) +__field_overflow (); +return ((v &field_mask (field)) *field_multiplier (field));Unfortunately gcc emits actual divisions or __*div*() calls, and multiplications in the non-constant case.
Quoted 1 line
So I don't think this is suitable as-is.
Sorry I missed or forgot that you replied :(
The inline helpers exist already have have a lot of uses. If __ffs is more optimal then why not make existing helpers use it as well? It'd be far more beneficial:
$ git grep u32_encode_bits | wc -l 391
Sorry if I'm being slow..
Geert Uytterhoeven
maintainerPATCH v3 3/4 clk: renesas: Use bitfield helpers
In reply to Geert UytterhoevenUse the FIELD_{GET,PREP}() and field_{get,prep}() helpers for const respective non-const bitfields, instead of open-coding the same operations.
---
v3:
- No changes,
v2:
- Rebase on top of commit 470e3f0d0b1529ab ("clk: renesas: rcar-gen4:
Introduce R-Car Gen4 CPG driver").
--- drivers/clk/renesas/clk-div6.c | 6 +++--- drivers/clk/renesas/rcar-gen3-cpg.c | 15 +++++---------- drivers/clk/renesas/rcar-gen4-cpg.c | 9 +++------ 3 files changed, 11 insertions(+), 19 deletions(-)
drivers/clk/renesas/clk-div6.c +3 -3
@@ -7,6 +7,7 @@* Contact: Laurent Pinchart <[email protected]> */ +#include <linux/bitfield.h> #include <linux/clk-provider.h> #include <linux/init.h> #include <linux/io.h>
@@ -171,8 +172,7 @@ static u8 cpg_div6_clock_get_parent(struct clk_hw *hw)if (clock->src_mask ==0 )return 0 ; - hw_index = (readl (clock->reg) & clock->src_mask) >> -__ffs (clock->src_mask); + hw_index =field_get (clock->src_mask,readl (clock->reg));for (i =0 ; i <clk_hw_get_num_parents (hw); i++) {if (clock->parents[i] == hw_index)return i;
@@ -191,7 +191,7 @@ static int cpg_div6_clock_set_parent(struct clk_hw *hw, u8 index)if (index >=clk_hw_get_num_parents (hw))return -EINVAL ; - src = clock->parents[index] <<__ffs (clock->src_mask); + src =field_prep (clock->src_mask, clock->parents[index]);writel ((readl (clock->reg) & ~clock->src_mask) | src, clock->reg);return 0 ; }
drivers/clk/renesas/rcar-gen3-cpg.c +5 -10
@@ -54,10 +54,8 @@ static unsigned long cpg_pll_clk_recalc_rate(struct clk_hw *hw, {struct cpg_pll_clk *pll_clk =to_pll_clk (hw);unsigned int mult; -u32 val; - val =readl (pll_clk->pllcr_reg) & CPG_PLLnCR_STC_MASK; - mult = (val >>__ffs (CPG_PLLnCR_STC_MASK)) +1 ; + mult =FIELD_GET (CPG_PLLnCR_STC_MASK,readl (pll_clk->pllcr_reg)) +1 ;return parent_rate * mult * pll_clk->fixed_mult; }
@@ -94,7 +92,7 @@ static int cpg_pll_clk_set_rate(struct clk_hw *hw, unsigned long rate, val =readl (pll_clk->pllcr_reg); val &= ~CPG_PLLnCR_STC_MASK; - val |= (mult -1 ) <<__ffs (CPG_PLLnCR_STC_MASK); + val |=FIELD_PREP (CPG_PLLnCR_STC_MASK, mult -1 );writel (val, pll_clk->pllcr_reg);for (i =1000 ; i; i--) {
@@ -176,11 +174,7 @@ static unsigned long cpg_z_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) {struct cpg_z_clk *zclk =to_z_clk (hw); -unsigned int mult; -u32 val; - - val =readl (zclk->reg) & zclk->mask; - mult =32 - (val >>__ffs (zclk->mask)); +unsigned int mult =32 -field_get (zclk->mask,readl (zclk->reg));return DIV_ROUND_CLOSEST_ULL ((u64 )parent_rate * mult,32 * zclk->fixed_div);
@@ -231,7 +225,8 @@ static int cpg_z_clk_set_rate(struct clk_hw *hw, unsigned long rate,if (readl (zclk->kick_reg) &CPG_FRQCRB_KICK )return -EBUSY ; -cpg_reg_modify (zclk->reg, zclk->mask, (32 - mult) <<__ffs (zclk->mask)); +cpg_reg_modify (zclk->reg, zclk->mask, +field_prep (zclk->mask,32 - mult));/* * Set KICK bit in FRQCRB to update hardware setting and wait for
drivers/clk/renesas/rcar-gen4-cpg.c +3 -6
@@ -279,11 +279,7 @@ static unsigned long cpg_z_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) {struct cpg_z_clk *zclk =to_z_clk (hw); -unsigned int mult; -u32 val; - - val =readl (zclk->reg) & zclk->mask; - mult =32 - (val >>__ffs (zclk->mask)); +unsigned int mult =32 -field_get (zclk->mask,readl (zclk->reg));return DIV_ROUND_CLOSEST_ULL ((u64 )parent_rate * mult,32 * zclk->fixed_div);
@@ -334,7 +330,8 @@ static int cpg_z_clk_set_rate(struct clk_hw *hw, unsigned long rate,if (readl (zclk->kick_reg) &CPG_FRQCRB_KICK )return -EBUSY ; -cpg_reg_modify (zclk->reg, zclk->mask, (32 - mult) <<__ffs (zclk->mask)); +cpg_reg_modify (zclk->reg, zclk->mask, +field_prep (zclk->mask,32 - mult));/* * Set KICK bit in FRQCRB to update hardware setting and wait for
Signature · 1 line
2.43.0
Geert Uytterhoeven
maintainerPATCH v3 4/4 soc: renesas: Use bitfield helpers
In reply to Geert UytterhoevenUse the field_get() helper, instead of open-coding the same operation.
---
v3:
- No changes,
v2:
- Drop RFC, as a dependency was applied.
--- drivers/soc/renesas/renesas-soc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/soc/renesas/renesas-soc.c +2 -2
@@ -5,6 +5,7 @@* Copyright (C) 2014-2016 Glider bvba */ +#include <linux/bitfield.h> #include <linux/io.h> #include <linux/of.h> #include <linux/of_address.h>
@@ -512,8 +513,7 @@ static int __init renesas_soc_init(void) eshi, eslo); } -if (soc->id && - ((product & id->mask) >>__ffs (id->mask)) != soc->id) { +if (soc->id &&field_get (id->mask, product) != soc->id) {pr_warn ("SoC mismatch (product = 0x%x)\n" , product); ret = -ENODEV ; goto free_soc_dev_attr;
Signature · 1 line
2.43.0