Re: [PATCH 5/9] include: Import & Update bitops.h

2022-03-16 Thread Simon Glass
On Wed, 16 Mar 2022 at 09:40, Pierre-Clément Tosi  wrote:
>
> Import the header from version 5.16 of the kernel:
>
> commit df0cc57e057f18e44dac8e6c18aba47ab53202f9
>
> Inline the included  and prevent U-Boot from including
>  as BITS_PER_LONG is defined in .
>
> Remove now-duplicate definitions from .
>
> Note: This brings extra compile-time checks through GENMASK_INPUT_CHECK.
>
> Signed-off-by: Pierre-Clément Tosi 
> Cc: Simon Glass 
> Cc: Tom Rini 
> ---
>  include/linux/bitops.h | 27 ++---
>  include/linux/bits.h   | 55 ++
>  2 files changed, 62 insertions(+), 20 deletions(-)
>  create mode 100644 include/linux/bits.h
>

Reviewed-by: Simon Glass 


[PATCH 5/9] include: Import & Update bitops.h

2022-03-16 Thread Pierre-Clément Tosi
Import the header from version 5.16 of the kernel:

commit df0cc57e057f18e44dac8e6c18aba47ab53202f9

Inline the included  and prevent U-Boot from including
 as BITS_PER_LONG is defined in .

Remove now-duplicate definitions from .

Note: This brings extra compile-time checks through GENMASK_INPUT_CHECK.

Signed-off-by: Pierre-Clément Tosi 
Cc: Simon Glass 
Cc: Tom Rini 
---
 include/linux/bitops.h | 27 ++---
 include/linux/bits.h   | 55 ++
 2 files changed, 62 insertions(+), 20 deletions(-)
 create mode 100644 include/linux/bits.h

diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index d2e5ca026e..6d465077d6 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -5,37 +5,24 @@
 
 #include 
 #include 
+#include 
 #include 
 
 #ifdef __KERNEL__
-#define BIT(nr)(1UL << (nr))
-#define BIT_ULL(nr)(1ULL << (nr))
-#define BIT_MASK(nr)   (1UL << ((nr) % BITS_PER_LONG))
-#define BIT_WORD(nr)   ((nr) / BITS_PER_LONG)
-#define BIT_ULL_MASK(nr)   (1ULL << ((nr) % BITS_PER_LONG_LONG))
-#define BIT_ULL_WORD(nr)   ((nr) / BITS_PER_LONG_LONG)
-#define BITS_PER_BYTE  8
 #define BITS_TO_LONGS(nr)  DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
 #endif
 
 /* kernel.h includes log.h which include bitops.h */
 #include 
 
-/*
- * Create a contiguous bitmask starting at bit position @l and ending at
- * position @h. For example
- * GENMASK_ULL(39, 21) gives us the 64bit vector 0x00e0.
- */
 #ifdef CONFIG_SANDBOX
-#define GENMASK(h, l) \
-   (((~0UL) << (l)) & (~0UL >> (CONFIG_SANDBOX_BITS_PER_LONG - 1 - (h
-#else
-#define GENMASK(h, l) \
-   (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h
+#ifdef __GENMASK
+#undef __GENMASK
+#endif
+#define __GENMASK(h, l) \
+   (((~UL(0)) - (UL(1) << (l)) + 1) & \
+(~UL(0) >> (CONFIG_SANDBOX_BITS_PER_LONG  - 1 - (h
 #endif
-
-#define GENMASK_ULL(h, l) \
-   (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h
 
 /*
  * ffs: find first bit set. This is defined the same way as
diff --git a/include/linux/bits.h b/include/linux/bits.h
new file mode 100644
index 00..04e7dae7f9
--- /dev/null
+++ b/include/linux/bits.h
@@ -0,0 +1,55 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __LINUX_BITS_H
+#define __LINUX_BITS_H
+
+#include 
+#ifndef __UBOOT__
+#include 
+#else
+#define BIT(nr) (UL(1) << (nr))
+#endif
+#ifndef __UBOOT__
+#include 
+#else
+/* U-Boot defines BITS_PER_LONG in . */
+#include 
+#endif
+
+#define BIT_ULL(nr)(ULL(1) << (nr))
+#define BIT_MASK(nr)   (UL(1) << ((nr) % BITS_PER_LONG))
+#define BIT_WORD(nr)   ((nr) / BITS_PER_LONG)
+#define BIT_ULL_MASK(nr)   (ULL(1) << ((nr) % BITS_PER_LONG_LONG))
+#define BIT_ULL_WORD(nr)   ((nr) / BITS_PER_LONG_LONG)
+#define BITS_PER_BYTE  8
+
+/*
+ * Create a contiguous bitmask starting at bit position @l and ending at
+ * position @h. For example
+ * GENMASK_ULL(39, 21) gives us the 64bit vector 0x00e0.
+ */
+#if !defined(__ASSEMBLY__)
+#include 
+#define GENMASK_INPUT_CHECK(h, l) \
+   (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
+   __is_constexpr((l) > (h)), (l) > (h), 0)))
+#else
+/*
+ * BUILD_BUG_ON_ZERO is not available in h files included from asm files,
+ * disable the input check if that is the case.
+ */
+#define GENMASK_INPUT_CHECK(h, l) 0
+#endif
+
+#define __GENMASK(h, l) \
+   (((~UL(0)) - (UL(1) << (l)) + 1) & \
+(~UL(0) >> (BITS_PER_LONG - 1 - (h
+#define GENMASK(h, l) \
+   (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
+
+#define __GENMASK_ULL(h, l) \
+   (((~ULL(0)) - (ULL(1) << (l)) + 1) & \
+(~ULL(0) >> (BITS_PER_LONG_LONG - 1 - (h
+#define GENMASK_ULL(h, l) \
+   (GENMASK_INPUT_CHECK(h, l) + __GENMASK_ULL(h, l))
+
+#endif /* __LINUX_BITS_H */
-- 
2.35.1.723.g4982287a31-goog