Skip to contentSkip to conversation

linux/bitfield.h: sync <linux/bitfield.h> from Linux 6.15 + winbond

PATCH Superseded Christian Marangi 3 participants 9 messages Jun 6, 2025 – Aug 18, 2025

Subsystems (10)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover linux/bitfield.h: sync <linux/bitfield.h> from Linux 6.15 + winbond

This small series sync linux/bitfield.h from Linux 6.15 and fix all the compilation error due to a change in the header include.

The sync is needed to make it easier to support the winbond changes.

The changes are CI test with [1]

[1] https://github.com/u-boot/u-boot/pull/777

Changes v3:
- Fix compilation error for remaining drivers Changes v2:
- Copy the full file instead of cherry-pick Changes

Christian Marangi (5):
linux/bitfield.h: sync <linux/bitfield.h> from Linux 6.15
clk: imx: add missing linux/bug.h header for WARN
iommu: qcom-smmu: add missing linux/bug.h header for WARN_ON
arm: apple: rtkit: add missing header linux/bug.h and linux/bitops.h
mtd: spinand: winbond: add Winbond W25N04KV flash support

 arch/arm/mach-apple/rtkit.c       |   2 +
 drivers/clk/imx/clk-fracn-gppll.c |   1 +
 drivers/iommu/qcom-hyp-smmu.c     |   1 +
 drivers/mtd/nand/spi/winbond.c    |  13 ++++
 include/linux/bitfield.h          | 103 ++++++++++++++++++++++--------
 5 files changed, 92 insertions(+), 28 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH v3 1/5 linux/bitfield.h: sync <linux/bitfield.h> from Linux 6.15

Sync bitfield.h header with Linux 6.15 version. Mainly is to permit the introduction of FIELD_PREP_CONST. The bug.h header changed to build_bug.h doesn't cause any regression as we also ship split header similar to how it's done with in Linux.

---
 include/linux/bitfield.h | 103 ++++++++++++++++++++++++++++-----------
 1 file changed, 75 insertions(+), 28 deletions(-)
include/linux/bitfield.h +75 -28
index 7ad02f8cbb9..63928f17322 100644
@@ -1,21 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
 /*
  * Copyright (C) 2014 Felix Fietkau <[email protected]>
  * Copyright (C) 2004 - 2009 Ivo van Doorn <[email protected]>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2
- * as published by the Free Software Foundation
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
  */
 
 #ifndef _LINUX_BITFIELD_H
 #define _LINUX_BITFIELD_H
 
-#include <linux/bug.h>
+#include <linux/build_bug.h>
 #include <asm/byteorder.h>
 
 /*
@@ -27,6 +19,9 @@
  *
  * Example:
  *
+ *  #include <linux/bitfield.h>
+ *  #include <linux/bits.h>
+ *
  *  #define REG_FIELD_A  GENMASK(6, 0)
  *  #define REG_FIELD_B  BIT(7)
  *  #define REG_FIELD_C  GENMASK(15, 8)
@@ -49,20 +44,51 @@
 
 #define __bf_shf(x) (__builtin_ffsll(x) - 1)
 
+#define __scalar_type_to_unsigned_cases(type)				\
+		unsigned type:	(unsigned type)0,			\
+		signed type:	(unsigned type)0
+
+#define __unsigned_scalar_typeof(x) typeof(				\
+		_Generic((x),						\
+			char:	(unsigned char)0,			\
+			__scalar_type_to_unsigned_cases(char),		\
+			__scalar_type_to_unsigned_cases(short),		\
+			__scalar_type_to_unsigned_cases(int),		\
+			__scalar_type_to_unsigned_cases(long),		\
+			__scalar_type_to_unsigned_cases(long long),	\
+			default: (x)))
+
+#define __bf_cast_unsigned(type, x)	((__unsigned_scalar_typeof(type))(x))
+
 #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), _pfx "mask is zero");	\
+		BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero");	\
 		BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ?		\
-				 ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
+				 ~((_mask) >> __bf_shf(_mask)) &	\
+					(0 + (_val)) : 0,		\
 				 _pfx "value too large for the field"); \
-		BUILD_BUG_ON_MSG((_mask) > (typeof(_reg))~0ull,		\
+		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
+ *
+ * FIELD_MAX() returns the maximum value that can be held in the field
+ * specified by @_mask.
+ */
+#define FIELD_MAX(_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
@@ -72,7 +98,7 @@
  */
 #define FIELD_FIT(_mask, _val)						\
 	({								\
-		__BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_FIT: ");	\
+		__BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: ");	\
 		!((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
 	})
 
@@ -90,10 +116,36 @@
 		((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
+ *
+ * FIELD_PREP_CONST() masks and shifts up the value.  The result should
+ * be combined with other fields of the bitfield using logical OR.
+ *
+ * Unlike FIELD_PREP() this is a constant expression and can therefore
+ * 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)					\
+	(								\
+		/* mask must be non-zero */				\
+		BUILD_BUG_ON_ZERO((_mask) == 0) +			\
+		/* check if value fits */				\
+		BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \
+		/* check if mask is contiguous */			\
+		__BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask))) +	\
+		/* and create the value */				\
+		(((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask))	\
+	)
+
 /**
  * FIELD_GET() - extract a bitfield element
  * @_mask: shifted mask defining the field's length and position
- * @_reg:  32bit value of entire bitfield
+ * @_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.
@@ -108,20 +160,18 @@ extern void __compiletime_error("value doesn't fit into mask")
 __field_overflow(void);
 extern void __compiletime_error("bad bitfield mask")
 __bad_mask(void);
-
 static __always_inline u64 field_multiplier(u64 field)
 {
 	if ((field | (field - 1)) & ((field | (field - 1)) + 1))
 		__bad_mask();
 	return field & -field;
 }
-
 static __always_inline u64 field_mask(u64 field)
 {
 	return field / field_multiplier(field);
 }
-
-#define ____MAKE_OP(type, base, to, from)				\
+#define field_max(field)	((typeof(field))field_mask(field))
+#define ____MAKE_OP(type,base,to,from)					\
 static __always_inline __##type type##_encode_bits(base v, base field)	\
 {									\
 	if (__builtin_constant_p(v) && (v & ~field_mask(field)))	\
@@ -133,26 +183,23 @@ static __always_inline __##type type##_replace_bits(__##type old,	\
 {									\
 	return (old & ~to(field)) | type##_encode_bits(val, field);	\
 }									\
-static __always_inline void type##p_replace_bits(__##type * p,		\
+static __always_inline void type##p_replace_bits(__##type *p,		\
 					base val, base field)		\
 {									\
 	*p = (*p & ~to(field)) | type##_encode_bits(val, field);	\
 }									\
 static __always_inline base type##_get_bits(__##type v, base field)	\
 {									\
-	return (from(v) & field) / field_multiplier(field);		\
+	return (from(v) & field)/field_multiplier(field);		\
 }
-
 #define __MAKE_OP(size)							\
-	____MAKE_OP(le##size, u##size, cpu_to_le##size, le##size##_to_cpu) \
-	____MAKE_OP(be##size, u##size, cpu_to_be##size, be##size##_to_cpu) \
-	____MAKE_OP(u##size, u##size, ,)
-
-____MAKE_OP(u8, u8, ,)
+	____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu)	\
+	____MAKE_OP(be##size,u##size,cpu_to_be##size,be##size##_to_cpu)	\
+	____MAKE_OP(u##size,u##size,,)
+____MAKE_OP(u8,u8,,)
 __MAKE_OP(16)
 __MAKE_OP(32)
 __MAKE_OP(64)
-
 #undef __MAKE_OP
 #undef ____MAKE_OP
 
Signature · 1 line
2.48.1
#2 lore raw

PATCH v3 2/5 clk: imx: add missing linux/bug.h header for WARN

In reply to Christian Marangi

The WARN macro requires inclusion of linux/bug.h header. It did previously work as bitfield.h included it indirectly but now this has changed.

Explicitly include the header to fix compilation error.

---
 drivers/clk/imx/clk-fracn-gppll.c | 1 +
 1 file changed, 1 insertion(+)
drivers/clk/imx/clk-fracn-gppll.c +1 -0
index 81e19d393cf..b3926564a22 100644
@@ -10,6 +10,7 @@
 #include <dm/devres.h>
 #include <linux/bitfield.h>
 #include <linux/bitops.h>
+#include <linux/bug.h>
 #include <linux/clk-provider.h>
 #include <linux/delay.h>
 #include <linux/err.h>
Signature · 1 line
2.48.1
#3 lore raw

PATCH v3 3/5 iommu: qcom-smmu: add missing linux/bug.h header for WARN_ON

In reply to Christian Marangi

The WARN_ON macro requires inclusion of linux/bug.h header. It did previously work as bitfield.h included it indirectly but now this has changed.

Explicitly include the header to fix compilation error.

---
 drivers/iommu/qcom-hyp-smmu.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iommu/qcom-hyp-smmu.c +1 -0
index c1b95bc8b8c..2e51ce4f242 100644
@@ -11,6 +11,7 @@
 #include <dm.h>
 #include <iommu.h>
 #include <linux/bitfield.h>
+#include <linux/bug.h>
 #include <linux/list.h>
 #include <linux/err.h>
 #include <lmb.h>
Signature · 1 line
2.48.1
#4 lore raw

PATCH v3 4/5 arm: apple: rtkit: add missing header linux/bug.h and linux/bitops.h

In reply to Christian Marangi

The GENMASK/GENMASK_ULL macro requires inclusion of linux/bitops.h header. It did previously work as bitfield.h included it indirectly but now this has changed.

Also raw printf require linux/bug.h header (also previously included indirectly by bitfield.h)

Explicitly include the headers to fix compilation error.

---
 arch/arm/mach-apple/rtkit.c | 2 ++
 1 file changed, 2 insertions(+)
arch/arm/mach-apple/rtkit.c +2 -0
index f3561543a35..251c6056cbd 100644
@@ -9,7 +9,9 @@
 
 #include <asm/arch/rtkit.h>
 #include <linux/apple-mailbox.h>
+#include <linux/bug.h>
 #include <linux/bitfield.h>
+#include <linux/bitops.h>
 #include <linux/errno.h>
 #include <linux/sizes.h>
 #include <linux/types.h>
Signature · 1 line
2.48.1
#5 lore raw

PATCH v3 5/5 mtd: spinand: winbond: add Winbond W25N04KV flash support

In reply to Christian Marangi

Add Winbond W25N04KV flash support that use a different value to detect ECC bitflip.

---
 drivers/mtd/nand/spi/winbond.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/mtd/nand/spi/winbond.c +13 -0
index dd4ed257a83..16abf89dbbf 100644
@@ -11,6 +11,7 @@
 #include <linux/device.h>
 #include <linux/kernel.h>
 #endif
+#include <linux/bitfield.h>
 #include <linux/bug.h>
 #include <linux/mtd/spinand.h>
 
@@ -18,6 +19,8 @@
 
 #define WINBOND_CFG_BUF_READ		BIT(3)
 
+#define W25N04KV_STATUS_ECC_5_8_BITFLIPS FIELD_PREP_CONST(STATUS_ECC_MASK, 0x3)
+
 static SPINAND_OP_VARIANTS(read_cache_variants,
 		SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
 		SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
@@ -121,6 +124,7 @@ static int w25n02kv_ecc_get_status(struct spinand_device *spinand,
 		return -EBADMSG;
 
 	case STATUS_ECC_HAS_BITFLIPS:
+	case W25N04KV_STATUS_ECC_5_8_BITFLIPS:
 		/*
 		 * Let's try to retrieve the real maximum number of bitflips
 		 * in order to avoid forcing the wear-leveling layer to move
@@ -172,6 +176,15 @@ static const struct spinand_info winbond_spinand_table[] = {
 					      &update_cache_variants),
 		     0,
 		     SPINAND_ECCINFO(&w25n02kv_ooblayout, w25n02kv_ecc_get_status)),
+	SPINAND_INFO("W25N04KV",
+		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xaa, 0x23),
+		     NAND_MEMORG(1, 2048, 128, 64, 4096, 40, 2, 1, 1),
+		     NAND_ECCREQ(8, 512),
+		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
+					      &write_cache_variants,
+					      &update_cache_variants),
+		     0,
+		     SPINAND_ECCINFO(&w25n02kv_ooblayout, w25n02kv_ecc_get_status)),
 };
 
 static int winbond_spinand_init(struct spinand_device *spinand)
Signature · 1 line
2.48.1
#6 lore raw

Re: [PATCH v3 5/5] mtd: spinand: winbond: add Winbond W25N04KV flash support

Am 06.06.25 um 09:21 schrieb Christian Marangi:

Quoted 4 lines

Add Winbond W25N04KV flash support that use a different value to detect ECC bitflip.

For information: this will be covered by Mikhail's patchset here:

https://patchwork.ozlabs.org/project/uboot/cover/[email protected]/

And for the future, please provide a changelog when you send mulitple versions of the same patch. Thanks!

Quoted 49 lines
---
 drivers/mtd/nand/spi/winbond.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/mtd/nand/spi/winbond.c +13 -0
index dd4ed257a83..16abf89dbbf 100644
@@ -11,6 +11,7 @@
 #include <linux/device.h>
 #include <linux/kernel.h>
 #endif
+#include <linux/bitfield.h>
 #include <linux/bug.h>
 #include <linux/mtd/spinand.h>
 
@@ -18,6 +19,8 @@
 
 #define WINBOND_CFG_BUF_READ		BIT(3)
 
+#define W25N04KV_STATUS_ECC_5_8_BITFLIPS FIELD_PREP_CONST(STATUS_ECC_MASK, 0x3)
+
 static SPINAND_OP_VARIANTS(read_cache_variants,
 		SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
 		SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
@@ -121,6 +124,7 @@ static int w25n02kv_ecc_get_status(struct spinand_device *spinand,
 		return -EBADMSG;
 
 	case STATUS_ECC_HAS_BITFLIPS:
+	case W25N04KV_STATUS_ECC_5_8_BITFLIPS:
 		/*
 		 * Let's try to retrieve the real maximum number of bitflips
 		 * in order to avoid forcing the wear-leveling layer to move
@@ -172,6 +176,15 @@ static const struct spinand_info winbond_spinand_table[] = {
 					      &update_cache_variants),
 		     0,
 		     SPINAND_ECCINFO(&w25n02kv_ooblayout, w25n02kv_ecc_get_status)),
+	SPINAND_INFO("W25N04KV",
+		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xaa, 0x23),
+		     NAND_MEMORG(1, 2048, 128, 64, 4096, 40, 2, 1, 1),
+		     NAND_ECCREQ(8, 512),
+		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
+					      &write_cache_variants,
+					      &update_cache_variants),
+		     0,
+		     SPINAND_ECCINFO(&w25n02kv_ooblayout, w25n02kv_ecc_get_status)),
 };
 
 static int winbond_spinand_init(struct spinand_device *spinand)
#7 lore raw

Re: [PATCH v3 5/5] mtd: spinand: winbond: add Winbond W25N04KV flash support

Am 18.08.25 um 12:21 schrieb Frieder Schrempf:

Quoted 4 lines

Am 06.06.25 um 09:21 schrieb Christian Marangi:

Quoted 3 lines

Add Winbond W25N04KV flash support that use a different value to detect ECC bitflip.

Quoted 8 lines
Quoted 1 line

For information: this will be covered by Mikhail's patchset here:

https://patchwork.ozlabs.org/project/uboot/cover/[email protected]/

And for the future, please provide a changelog when you send mulitple versions of the same patch. Thanks!

Sorry, I missed the rest of the series and the changelog in the cover letter. And I missed the fact that this has already been merged. So forget about my message and sorry for the noise.

#8 lore raw
In reply to Christian Marangi

Hi Christian,

Quoted 14 lines · Christian Marangi

This small series sync linux/bitfield.h from Linux 6.15 and fix all the compilation error due to a change in the header include.

The sync is needed to make it easier to support the winbond changes.

The changes are CI test with [1]

[1] https://github.com/u-boot/u-boot/pull/777

Changes v3:
- Fix compilation error for remaining drivers Changes v2:
- Copy the full file instead of cherry-pick Changes

Quoted 5 lines

Christian Marangi (5):
linux/bitfield.h: sync <linux/bitfield.h> from Linux 6.15
clk: imx: add missing linux/bug.h header for WARN
iommu: qcom-smmu: add missing linux/bug.h header for WARN_ON
arm: apple: rtkit: add missing header linux/bug.h and linux/bitops.h

We are generally aiming for bisectability, so we should avoid having commits that are broken. Therefore, patches 2 and 3 should be before the bitfield sync (patch 1). Because they will "fix" a "bug" that will never happen, their commit log should explain why we are doing this change: in anticipation of the sync commit that will remove the dependency on bug.h for build_bug.h.

Cheers,
Quentin