OpenSBI Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/3] Add endianness conversion support
@ 2023-02-02  4:44 Rahul Pathak
  2023-02-02  4:44 ` [PATCH v2 1/3] include: Add support for byteorder/endianness conversion Rahul Pathak
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Rahul Pathak @ 2023-02-02  4:44 UTC (permalink / raw)
  To: opensbi

This series adds a new header which defines the generic
endianness conversion functions.
These new functions replaces the existing byte-swapping
functions defined in libfdt_env.h

New typedefs - leX_t/beX_t and use them in existing
libfdt_env.h

Changelog - v2:
- Added new patch for typedefs related to endianness

Rahul Pathak (3):
  include: Add support for byteorder/endianness conversion
  lib: utils/fdt: Use byteorder conversion functions in libfdt_env.h
  include: types: Add typedefs for endianness

 include/sbi/sbi_byteorder.h   | 57 +++++++++++++++++++++++++++++++++++
 include/sbi/sbi_types.h       |  7 +++++
 lib/utils/libfdt/libfdt_env.h | 35 ++++++++-------------
 3 files changed, 77 insertions(+), 22 deletions(-)
 create mode 100644 include/sbi/sbi_byteorder.h

-- 
2.34.1



^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 1/3] include: Add support for byteorder/endianness conversion
  2023-02-02  4:44 [PATCH v2 0/3] Add endianness conversion support Rahul Pathak
@ 2023-02-02  4:44 ` Rahul Pathak
  2023-02-08 12:46   ` Anup Patel
  2023-02-09  0:28   ` Samuel Holland
  2023-02-02  4:44 ` [PATCH v2 2/3] lib: utils/fdt: Use byteorder conversion functions in libfdt_env.h Rahul Pathak
  2023-02-02  4:44 ` [PATCH v2 3/3] include: types: Add typedefs for endianness Rahul Pathak
  2 siblings, 2 replies; 9+ messages in thread
From: Rahul Pathak @ 2023-02-02  4:44 UTC (permalink / raw)
  To: opensbi

Define macros general byteorder conversion
Define functions for endianness conversion
from general byteorder conversion macros

Signed-off-by: Rahul Pathak <rpathak@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Sergey Matyukevich <sergey.matyukevich@syntacore.com>
---
 include/sbi/sbi_byteorder.h | 57 +++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
 create mode 100644 include/sbi/sbi_byteorder.h

diff --git a/include/sbi/sbi_byteorder.h b/include/sbi/sbi_byteorder.h
new file mode 100644
index 000000000000..680710fe6d91
--- /dev/null
+++ b/include/sbi/sbi_byteorder.h
@@ -0,0 +1,57 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Ventana Micro Systems Inc.
+ */
+
+#ifndef __SBI_BYTEORDER_H__
+#define __SBI_BYTEORDER_H__
+
+#include <sbi/sbi_types.h>
+
+#define EXTRACT_BYTE(x, n)	((unsigned long long)((uint8_t *)&x)[n])
+
+#define BSWAP16(x)	((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
+#define BSWAP32(x)	((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
+			 (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
+#define BSWAP64(x)	((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
+			 (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
+			 (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
+			 (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
+
+
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__  /* CPU(little-endian) */
+#define cpu_to_be16(x)		((uint16_t)BSWAP16(x))
+#define cpu_to_be32(x)		((uint32_t)BSWAP32(x))
+#define cpu_to_be64(x)		((uint64_t)BSWAP64(x))
+
+#define be16_to_cpu(x)		((uint16_t)BSWAP16(x))
+#define be32_to_cpu(x)		((uint32_t)BSWAP32(x))
+#define be64_to_cpu(x)		((uint64_t)BSWAP64(x))
+
+#define cpu_to_le16(x)		((uint16_t)(x))
+#define cpu_to_le32(x)		((uint32_t)(x))
+#define cpu_to_le64(x)		((uint64_t)(x))
+
+#define le16_to_cpu(x)		((uint16_t)(x))
+#define le32_to_cpu(x)		((uint32_t)(x))
+#define le64_to_cpu(x)		((uint64_t)(x))
+#else /* CPU(big-endian) */
+#define cpu_to_be16(x)		((uint16_t)(x))
+#define cpu_to_be32(x)		((uint32_t)(x))
+#define cpu_to_be64(x)		((uint64_t)(x))
+
+#define be16_to_cpu(x)		((uint16_t)(x))
+#define be32_to_cpu(x)		((uint32_t)(x))
+#define be64_to_cpu(x)		((uint64_t)(x))
+
+#define cpu_to_le16(x)		((uint16_t)BSWAP16(x))
+#define cpu_to_le32(x)		((uint32_t)BSWAP32(x))
+#define cpu_to_le64(x)		((uint64_t)BSWAP64(x))
+
+#define le16_to_cpu(x)		((uint16_t)BSWAP16(x))
+#define le32_to_cpu(x)		((uint32_t)BSWAP32(x))
+#define le64_to_cpu(x)		((uint64_t)BSWAP64(x))
+#endif
+
+#endif /* __SBI_BYTEORDER_H__ */
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2 2/3] lib: utils/fdt: Use byteorder conversion functions in libfdt_env.h
  2023-02-02  4:44 [PATCH v2 0/3] Add endianness conversion support Rahul Pathak
  2023-02-02  4:44 ` [PATCH v2 1/3] include: Add support for byteorder/endianness conversion Rahul Pathak
@ 2023-02-02  4:44 ` Rahul Pathak
  2023-02-08 12:53   ` Anup Patel
  2023-02-02  4:44 ` [PATCH v2 3/3] include: types: Add typedefs for endianness Rahul Pathak
  2 siblings, 1 reply; 9+ messages in thread
From: Rahul Pathak @ 2023-02-02  4:44 UTC (permalink / raw)
  To: opensbi

FDT follows big-endian and CPU can be little or big
endian as per the implementation.
libfdt_env.h defines function for conversion between
fdt and cpu byteorder according to the endianness.

Currently, libfdt_env.h defines custom byte swapping
macros and then undefines them. Instead, use the generic
endianness conversion functions

Signed-off-by: Rahul Pathak <rpathak@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
---
 lib/utils/libfdt/libfdt_env.h | 29 ++++++++++-------------------
 1 file changed, 10 insertions(+), 19 deletions(-)

diff --git a/lib/utils/libfdt/libfdt_env.h b/lib/utils/libfdt/libfdt_env.h
index f9d9c6783c52..e5ad7698efca 100644
--- a/lib/utils/libfdt/libfdt_env.h
+++ b/lib/utils/libfdt/libfdt_env.h
@@ -9,6 +9,7 @@
 
 #include <sbi/sbi_string.h>
 #include <sbi/sbi_types.h>
+#include <sbi/sbi_byteorder.h>
 
 #define INT_MAX		((int)(~0U >> 1))
 #define UINT_MAX	((unsigned int)~0U)
@@ -41,45 +42,35 @@ typedef uint16_t FDT_BITWISE fdt16_t;
 typedef uint32_t FDT_BITWISE fdt32_t;
 typedef uint64_t FDT_BITWISE fdt64_t;
 
-#define EXTRACT_BYTE(x, n)	((unsigned long long)((uint8_t *)&x)[n])
-#define CPU_TO_FDT16(x) ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
-#define CPU_TO_FDT32(x) ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
-			 (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
-#define CPU_TO_FDT64(x) ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
-			 (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
-			 (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
-			 (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
-
 static inline uint16_t fdt16_to_cpu(fdt16_t x)
 {
-	return (FDT_FORCE uint16_t)CPU_TO_FDT16(x);
+	return (FDT_FORCE uint16_t)be16_to_cpu(x);
 }
+
 static inline fdt16_t cpu_to_fdt16(uint16_t x)
 {
-	return (FDT_FORCE fdt16_t)CPU_TO_FDT16(x);
+	return (FDT_FORCE fdt16_t)cpu_to_be16(x);
 }
 
 static inline uint32_t fdt32_to_cpu(fdt32_t x)
 {
-	return (FDT_FORCE uint32_t)CPU_TO_FDT32(x);
+	return (FDT_FORCE uint32_t)be32_to_cpu(x);
 }
+
 static inline fdt32_t cpu_to_fdt32(uint32_t x)
 {
-	return (FDT_FORCE fdt32_t)CPU_TO_FDT32(x);
+	return (FDT_FORCE fdt32_t)cpu_to_be32(x);
 }
 
 static inline uint64_t fdt64_to_cpu(fdt64_t x)
 {
-	return (FDT_FORCE uint64_t)CPU_TO_FDT64(x);
+	return (FDT_FORCE uint64_t)be64_to_cpu(x);
 }
+
 static inline fdt64_t cpu_to_fdt64(uint64_t x)
 {
-	return (FDT_FORCE fdt64_t)CPU_TO_FDT64(x);
+	return (FDT_FORCE fdt64_t)cpu_to_be64(x);
 }
-#undef CPU_TO_FDT64
-#undef CPU_TO_FDT32
-#undef CPU_TO_FDT16
-#undef EXTRACT_BYTE
 
 #ifdef __APPLE__
 #include <AvailabilityMacros.h>
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2 3/3] include: types: Add typedefs for endianness
  2023-02-02  4:44 [PATCH v2 0/3] Add endianness conversion support Rahul Pathak
  2023-02-02  4:44 ` [PATCH v2 1/3] include: Add support for byteorder/endianness conversion Rahul Pathak
  2023-02-02  4:44 ` [PATCH v2 2/3] lib: utils/fdt: Use byteorder conversion functions in libfdt_env.h Rahul Pathak
@ 2023-02-02  4:44 ` Rahul Pathak
  2023-02-08 12:56   ` Anup Patel
  2 siblings, 1 reply; 9+ messages in thread
From: Rahul Pathak @ 2023-02-02  4:44 UTC (permalink / raw)
  To: opensbi

If any variable/memory-location follows certain
endianness then its important to annotate it properly
so that proper conversion can be done before read/write
from that variable/memory.

Also, use these new typedefs in libfdt_env.h for deriving
its own custom fdtX_t types

Signed-off-by: Rahul Pathak <rpathak@ventanamicro.com>
---
 include/sbi/sbi_types.h       | 7 +++++++
 lib/utils/libfdt/libfdt_env.h | 6 +++---
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/include/sbi/sbi_types.h b/include/sbi/sbi_types.h
index 9c1fef3b8dc6..def88bbad2c3 100644
--- a/include/sbi/sbi_types.h
+++ b/include/sbi/sbi_types.h
@@ -54,6 +54,13 @@ typedef unsigned long		virtual_size_t;
 typedef unsigned long		physical_addr_t;
 typedef unsigned long		physical_size_t;
 
+typedef uint16_t		le16_t;
+typedef uint16_t		be16_t;
+typedef uint32_t		le32_t;
+typedef uint32_t		be32_t;
+typedef uint64_t		le64_t;
+typedef uint64_t		be64_t;
+
 #define true			1
 #define false			0
 
diff --git a/lib/utils/libfdt/libfdt_env.h b/lib/utils/libfdt/libfdt_env.h
index e5ad7698efca..6ebbae01b514 100644
--- a/lib/utils/libfdt/libfdt_env.h
+++ b/lib/utils/libfdt/libfdt_env.h
@@ -38,9 +38,9 @@
 #define strlen		sbi_strlen
 #define strnlen		sbi_strnlen
 
-typedef uint16_t FDT_BITWISE fdt16_t;
-typedef uint32_t FDT_BITWISE fdt32_t;
-typedef uint64_t FDT_BITWISE fdt64_t;
+typedef be16_t FDT_BITWISE fdt16_t;
+typedef be32_t FDT_BITWISE fdt32_t;
+typedef be64_t FDT_BITWISE fdt64_t;
 
 static inline uint16_t fdt16_to_cpu(fdt16_t x)
 {
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2 1/3] include: Add support for byteorder/endianness conversion
  2023-02-02  4:44 ` [PATCH v2 1/3] include: Add support for byteorder/endianness conversion Rahul Pathak
@ 2023-02-08 12:46   ` Anup Patel
  2023-02-09  0:28   ` Samuel Holland
  1 sibling, 0 replies; 9+ messages in thread
From: Anup Patel @ 2023-02-08 12:46 UTC (permalink / raw)
  To: opensbi

On Thu, Feb 2, 2023 at 10:15 AM Rahul Pathak <rpathak@ventanamicro.com> wrote:
>
> Define macros general byteorder conversion
> Define functions for endianness conversion
> from general byteorder conversion macros
>
> Signed-off-by: Rahul Pathak <rpathak@ventanamicro.com>
> Reviewed-by: Xiang W <wxjstz@126.com>
> Reviewed-by: Sergey Matyukevich <sergey.matyukevich@syntacore.com>

For now, this looks good. We can move to built-in compiler macros
as a separate patch.

Reviewed-by: Anup Patel <anup@brainfault.org>

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

> ---
>  include/sbi/sbi_byteorder.h | 57 +++++++++++++++++++++++++++++++++++++
>  1 file changed, 57 insertions(+)
>  create mode 100644 include/sbi/sbi_byteorder.h
>
> diff --git a/include/sbi/sbi_byteorder.h b/include/sbi/sbi_byteorder.h
> new file mode 100644
> index 000000000000..680710fe6d91
> --- /dev/null
> +++ b/include/sbi/sbi_byteorder.h
> @@ -0,0 +1,57 @@
> +/*
> + * SPDX-License-Identifier: BSD-2-Clause
> + *
> + * Copyright (c) 2023 Ventana Micro Systems Inc.
> + */
> +
> +#ifndef __SBI_BYTEORDER_H__
> +#define __SBI_BYTEORDER_H__
> +
> +#include <sbi/sbi_types.h>
> +
> +#define EXTRACT_BYTE(x, n)     ((unsigned long long)((uint8_t *)&x)[n])
> +
> +#define BSWAP16(x)     ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
> +#define BSWAP32(x)     ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
> +                        (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
> +#define BSWAP64(x)     ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
> +                        (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
> +                        (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
> +                        (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
> +
> +
> +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__  /* CPU(little-endian) */
> +#define cpu_to_be16(x)         ((uint16_t)BSWAP16(x))
> +#define cpu_to_be32(x)         ((uint32_t)BSWAP32(x))
> +#define cpu_to_be64(x)         ((uint64_t)BSWAP64(x))
> +
> +#define be16_to_cpu(x)         ((uint16_t)BSWAP16(x))
> +#define be32_to_cpu(x)         ((uint32_t)BSWAP32(x))
> +#define be64_to_cpu(x)         ((uint64_t)BSWAP64(x))
> +
> +#define cpu_to_le16(x)         ((uint16_t)(x))
> +#define cpu_to_le32(x)         ((uint32_t)(x))
> +#define cpu_to_le64(x)         ((uint64_t)(x))
> +
> +#define le16_to_cpu(x)         ((uint16_t)(x))
> +#define le32_to_cpu(x)         ((uint32_t)(x))
> +#define le64_to_cpu(x)         ((uint64_t)(x))
> +#else /* CPU(big-endian) */
> +#define cpu_to_be16(x)         ((uint16_t)(x))
> +#define cpu_to_be32(x)         ((uint32_t)(x))
> +#define cpu_to_be64(x)         ((uint64_t)(x))
> +
> +#define be16_to_cpu(x)         ((uint16_t)(x))
> +#define be32_to_cpu(x)         ((uint32_t)(x))
> +#define be64_to_cpu(x)         ((uint64_t)(x))
> +
> +#define cpu_to_le16(x)         ((uint16_t)BSWAP16(x))
> +#define cpu_to_le32(x)         ((uint32_t)BSWAP32(x))
> +#define cpu_to_le64(x)         ((uint64_t)BSWAP64(x))
> +
> +#define le16_to_cpu(x)         ((uint16_t)BSWAP16(x))
> +#define le32_to_cpu(x)         ((uint32_t)BSWAP32(x))
> +#define le64_to_cpu(x)         ((uint64_t)BSWAP64(x))
> +#endif
> +
> +#endif /* __SBI_BYTEORDER_H__ */
> --
> 2.34.1
>
>
> --
> opensbi mailing list
> opensbi at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 2/3] lib: utils/fdt: Use byteorder conversion functions in libfdt_env.h
  2023-02-02  4:44 ` [PATCH v2 2/3] lib: utils/fdt: Use byteorder conversion functions in libfdt_env.h Rahul Pathak
@ 2023-02-08 12:53   ` Anup Patel
  0 siblings, 0 replies; 9+ messages in thread
From: Anup Patel @ 2023-02-08 12:53 UTC (permalink / raw)
  To: opensbi

On Thu, Feb 2, 2023 at 10:15 AM Rahul Pathak <rpathak@ventanamicro.com> wrote:
>
> FDT follows big-endian and CPU can be little or big
> endian as per the implementation.
> libfdt_env.h defines function for conversion between
> fdt and cpu byteorder according to the endianness.
>
> Currently, libfdt_env.h defines custom byte swapping
> macros and then undefines them. Instead, use the generic
> endianness conversion functions
>
> Signed-off-by: Rahul Pathak <rpathak@ventanamicro.com>
> Reviewed-by: Xiang W <wxjstz@126.com>

Looks good to me.

Reviewed-by: Anup Patel <anup@brainfault.org>

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

> ---
>  lib/utils/libfdt/libfdt_env.h | 29 ++++++++++-------------------
>  1 file changed, 10 insertions(+), 19 deletions(-)
>
> diff --git a/lib/utils/libfdt/libfdt_env.h b/lib/utils/libfdt/libfdt_env.h
> index f9d9c6783c52..e5ad7698efca 100644
> --- a/lib/utils/libfdt/libfdt_env.h
> +++ b/lib/utils/libfdt/libfdt_env.h
> @@ -9,6 +9,7 @@
>
>  #include <sbi/sbi_string.h>
>  #include <sbi/sbi_types.h>
> +#include <sbi/sbi_byteorder.h>
>
>  #define INT_MAX                ((int)(~0U >> 1))
>  #define UINT_MAX       ((unsigned int)~0U)
> @@ -41,45 +42,35 @@ typedef uint16_t FDT_BITWISE fdt16_t;
>  typedef uint32_t FDT_BITWISE fdt32_t;
>  typedef uint64_t FDT_BITWISE fdt64_t;
>
> -#define EXTRACT_BYTE(x, n)     ((unsigned long long)((uint8_t *)&x)[n])
> -#define CPU_TO_FDT16(x) ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
> -#define CPU_TO_FDT32(x) ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
> -                        (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
> -#define CPU_TO_FDT64(x) ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
> -                        (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
> -                        (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
> -                        (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
> -
>  static inline uint16_t fdt16_to_cpu(fdt16_t x)
>  {
> -       return (FDT_FORCE uint16_t)CPU_TO_FDT16(x);
> +       return (FDT_FORCE uint16_t)be16_to_cpu(x);
>  }
> +
>  static inline fdt16_t cpu_to_fdt16(uint16_t x)
>  {
> -       return (FDT_FORCE fdt16_t)CPU_TO_FDT16(x);
> +       return (FDT_FORCE fdt16_t)cpu_to_be16(x);
>  }
>
>  static inline uint32_t fdt32_to_cpu(fdt32_t x)
>  {
> -       return (FDT_FORCE uint32_t)CPU_TO_FDT32(x);
> +       return (FDT_FORCE uint32_t)be32_to_cpu(x);
>  }
> +
>  static inline fdt32_t cpu_to_fdt32(uint32_t x)
>  {
> -       return (FDT_FORCE fdt32_t)CPU_TO_FDT32(x);
> +       return (FDT_FORCE fdt32_t)cpu_to_be32(x);
>  }
>
>  static inline uint64_t fdt64_to_cpu(fdt64_t x)
>  {
> -       return (FDT_FORCE uint64_t)CPU_TO_FDT64(x);
> +       return (FDT_FORCE uint64_t)be64_to_cpu(x);
>  }
> +
>  static inline fdt64_t cpu_to_fdt64(uint64_t x)
>  {
> -       return (FDT_FORCE fdt64_t)CPU_TO_FDT64(x);
> +       return (FDT_FORCE fdt64_t)cpu_to_be64(x);
>  }
> -#undef CPU_TO_FDT64
> -#undef CPU_TO_FDT32
> -#undef CPU_TO_FDT16
> -#undef EXTRACT_BYTE
>
>  #ifdef __APPLE__
>  #include <AvailabilityMacros.h>
> --
> 2.34.1
>
>
> --
> opensbi mailing list
> opensbi at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 3/3] include: types: Add typedefs for endianness
  2023-02-02  4:44 ` [PATCH v2 3/3] include: types: Add typedefs for endianness Rahul Pathak
@ 2023-02-08 12:56   ` Anup Patel
  0 siblings, 0 replies; 9+ messages in thread
From: Anup Patel @ 2023-02-08 12:56 UTC (permalink / raw)
  To: opensbi

On Thu, Feb 2, 2023 at 10:15 AM Rahul Pathak <rpathak@ventanamicro.com> wrote:
>
> If any variable/memory-location follows certain
> endianness then its important to annotate it properly
> so that proper conversion can be done before read/write
> from that variable/memory.
>
> Also, use these new typedefs in libfdt_env.h for deriving
> its own custom fdtX_t types
>
> Signed-off-by: Rahul Pathak <rpathak@ventanamicro.com>

Looks good to me.

Reviewed-by: Anup Patel <anup@brainfault.org>

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

> ---
>  include/sbi/sbi_types.h       | 7 +++++++
>  lib/utils/libfdt/libfdt_env.h | 6 +++---
>  2 files changed, 10 insertions(+), 3 deletions(-)
>
> diff --git a/include/sbi/sbi_types.h b/include/sbi/sbi_types.h
> index 9c1fef3b8dc6..def88bbad2c3 100644
> --- a/include/sbi/sbi_types.h
> +++ b/include/sbi/sbi_types.h
> @@ -54,6 +54,13 @@ typedef unsigned long                virtual_size_t;
>  typedef unsigned long          physical_addr_t;
>  typedef unsigned long          physical_size_t;
>
> +typedef uint16_t               le16_t;
> +typedef uint16_t               be16_t;
> +typedef uint32_t               le32_t;
> +typedef uint32_t               be32_t;
> +typedef uint64_t               le64_t;
> +typedef uint64_t               be64_t;
> +
>  #define true                   1
>  #define false                  0
>
> diff --git a/lib/utils/libfdt/libfdt_env.h b/lib/utils/libfdt/libfdt_env.h
> index e5ad7698efca..6ebbae01b514 100644
> --- a/lib/utils/libfdt/libfdt_env.h
> +++ b/lib/utils/libfdt/libfdt_env.h
> @@ -38,9 +38,9 @@
>  #define strlen         sbi_strlen
>  #define strnlen                sbi_strnlen
>
> -typedef uint16_t FDT_BITWISE fdt16_t;
> -typedef uint32_t FDT_BITWISE fdt32_t;
> -typedef uint64_t FDT_BITWISE fdt64_t;
> +typedef be16_t FDT_BITWISE fdt16_t;
> +typedef be32_t FDT_BITWISE fdt32_t;
> +typedef be64_t FDT_BITWISE fdt64_t;
>
>  static inline uint16_t fdt16_to_cpu(fdt16_t x)
>  {
> --
> 2.34.1
>
>
> --
> opensbi mailing list
> opensbi at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 1/3] include: Add support for byteorder/endianness conversion
  2023-02-02  4:44 ` [PATCH v2 1/3] include: Add support for byteorder/endianness conversion Rahul Pathak
  2023-02-08 12:46   ` Anup Patel
@ 2023-02-09  0:28   ` Samuel Holland
  2023-02-09  3:25     ` Anup Patel
  1 sibling, 1 reply; 9+ messages in thread
From: Samuel Holland @ 2023-02-09  0:28 UTC (permalink / raw)
  To: opensbi

On 2/1/23 22:44, Rahul Pathak wrote:
> Define macros general byteorder conversion
> Define functions for endianness conversion
> from general byteorder conversion macros
> 
> Signed-off-by: Rahul Pathak <rpathak@ventanamicro.com>
> Reviewed-by: Xiang W <wxjstz@126.com>
> Reviewed-by: Sergey Matyukevich <sergey.matyukevich@syntacore.com>
> ---
>  include/sbi/sbi_byteorder.h | 57 +++++++++++++++++++++++++++++++++++++
>  1 file changed, 57 insertions(+)
>  create mode 100644 include/sbi/sbi_byteorder.h
> 
> diff --git a/include/sbi/sbi_byteorder.h b/include/sbi/sbi_byteorder.h
> new file mode 100644
> index 000000000000..680710fe6d91
> --- /dev/null
> +++ b/include/sbi/sbi_byteorder.h
> @@ -0,0 +1,57 @@
> +/*
> + * SPDX-License-Identifier: BSD-2-Clause
> + *
> + * Copyright (c) 2023 Ventana Micro Systems Inc.
> + */
> +
> +#ifndef __SBI_BYTEORDER_H__
> +#define __SBI_BYTEORDER_H__
> +
> +#include <sbi/sbi_types.h>
> +
> +#define EXTRACT_BYTE(x, n)	((unsigned long long)((uint8_t *)&x)[n])
> +
> +#define BSWAP16(x)	((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
> +#define BSWAP32(x)	((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
> +			 (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
> +#define BSWAP64(x)	((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
> +			 (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
> +			 (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
> +			 (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))

This implementation is wrong. It only does any swapping on little-endian
systems. That was the desired behavior for CPU_TO_FDT*, but these macros
need to swap bytes regardless of the native endianness.

(Somehow, none of the other three reviewers noticed this?)

Regards,
Samuel

> +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__  /* CPU(little-endian) */
> +#define cpu_to_be16(x)		((uint16_t)BSWAP16(x))
> +#define cpu_to_be32(x)		((uint32_t)BSWAP32(x))
> +#define cpu_to_be64(x)		((uint64_t)BSWAP64(x))
> +
> +#define be16_to_cpu(x)		((uint16_t)BSWAP16(x))
> +#define be32_to_cpu(x)		((uint32_t)BSWAP32(x))
> +#define be64_to_cpu(x)		((uint64_t)BSWAP64(x))
> +
> +#define cpu_to_le16(x)		((uint16_t)(x))
> +#define cpu_to_le32(x)		((uint32_t)(x))
> +#define cpu_to_le64(x)		((uint64_t)(x))
> +
> +#define le16_to_cpu(x)		((uint16_t)(x))
> +#define le32_to_cpu(x)		((uint32_t)(x))
> +#define le64_to_cpu(x)		((uint64_t)(x))
> +#else /* CPU(big-endian) */
> +#define cpu_to_be16(x)		((uint16_t)(x))
> +#define cpu_to_be32(x)		((uint32_t)(x))
> +#define cpu_to_be64(x)		((uint64_t)(x))
> +
> +#define be16_to_cpu(x)		((uint16_t)(x))
> +#define be32_to_cpu(x)		((uint32_t)(x))
> +#define be64_to_cpu(x)		((uint64_t)(x))
> +
> +#define cpu_to_le16(x)		((uint16_t)BSWAP16(x))
> +#define cpu_to_le32(x)		((uint32_t)BSWAP32(x))
> +#define cpu_to_le64(x)		((uint64_t)BSWAP64(x))
> +
> +#define le16_to_cpu(x)		((uint16_t)BSWAP16(x))
> +#define le32_to_cpu(x)		((uint32_t)BSWAP32(x))
> +#define le64_to_cpu(x)		((uint64_t)BSWAP64(x))
> +#endif
> +
> +#endif /* __SBI_BYTEORDER_H__ */



^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 1/3] include: Add support for byteorder/endianness conversion
  2023-02-09  0:28   ` Samuel Holland
@ 2023-02-09  3:25     ` Anup Patel
  0 siblings, 0 replies; 9+ messages in thread
From: Anup Patel @ 2023-02-09  3:25 UTC (permalink / raw)
  To: opensbi

On Thu, Feb 9, 2023 at 5:58 AM Samuel Holland <samuel@sholland.org> wrote:
>
> On 2/1/23 22:44, Rahul Pathak wrote:
> > Define macros general byteorder conversion
> > Define functions for endianness conversion
> > from general byteorder conversion macros
> >
> > Signed-off-by: Rahul Pathak <rpathak@ventanamicro.com>
> > Reviewed-by: Xiang W <wxjstz@126.com>
> > Reviewed-by: Sergey Matyukevich <sergey.matyukevich@syntacore.com>
> > ---
> >  include/sbi/sbi_byteorder.h | 57 +++++++++++++++++++++++++++++++++++++
> >  1 file changed, 57 insertions(+)
> >  create mode 100644 include/sbi/sbi_byteorder.h
> >
> > diff --git a/include/sbi/sbi_byteorder.h b/include/sbi/sbi_byteorder.h
> > new file mode 100644
> > index 000000000000..680710fe6d91
> > --- /dev/null
> > +++ b/include/sbi/sbi_byteorder.h
> > @@ -0,0 +1,57 @@
> > +/*
> > + * SPDX-License-Identifier: BSD-2-Clause
> > + *
> > + * Copyright (c) 2023 Ventana Micro Systems Inc.
> > + */
> > +
> > +#ifndef __SBI_BYTEORDER_H__
> > +#define __SBI_BYTEORDER_H__
> > +
> > +#include <sbi/sbi_types.h>
> > +
> > +#define EXTRACT_BYTE(x, n)   ((unsigned long long)((uint8_t *)&x)[n])
> > +
> > +#define BSWAP16(x)   ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
> > +#define BSWAP32(x)   ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
> > +                      (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
> > +#define BSWAP64(x)   ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
> > +                      (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
> > +                      (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
> > +                      (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
>
> This implementation is wrong. It only does any swapping on little-endian
> systems. That was the desired behavior for CPU_TO_FDT*, but these macros
> need to swap bytes regardless of the native endianness.
>
> (Somehow, none of the other three reviewers noticed this?)

Argh, this was totally missed. Thanks for catching.

The EXTRACT_BYTE() macro is the culprit and does not do the
right thing for a big-endian host. Also, this macro will fail if "x" is
a constant.

I will send a fix ASAP.

Regards,
Anup

>
> Regards,
> Samuel
>
> > +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__  /* CPU(little-endian) */
> > +#define cpu_to_be16(x)               ((uint16_t)BSWAP16(x))
> > +#define cpu_to_be32(x)               ((uint32_t)BSWAP32(x))
> > +#define cpu_to_be64(x)               ((uint64_t)BSWAP64(x))
> > +
> > +#define be16_to_cpu(x)               ((uint16_t)BSWAP16(x))
> > +#define be32_to_cpu(x)               ((uint32_t)BSWAP32(x))
> > +#define be64_to_cpu(x)               ((uint64_t)BSWAP64(x))
> > +
> > +#define cpu_to_le16(x)               ((uint16_t)(x))
> > +#define cpu_to_le32(x)               ((uint32_t)(x))
> > +#define cpu_to_le64(x)               ((uint64_t)(x))
> > +
> > +#define le16_to_cpu(x)               ((uint16_t)(x))
> > +#define le32_to_cpu(x)               ((uint32_t)(x))
> > +#define le64_to_cpu(x)               ((uint64_t)(x))
> > +#else /* CPU(big-endian) */
> > +#define cpu_to_be16(x)               ((uint16_t)(x))
> > +#define cpu_to_be32(x)               ((uint32_t)(x))
> > +#define cpu_to_be64(x)               ((uint64_t)(x))
> > +
> > +#define be16_to_cpu(x)               ((uint16_t)(x))
> > +#define be32_to_cpu(x)               ((uint32_t)(x))
> > +#define be64_to_cpu(x)               ((uint64_t)(x))
> > +
> > +#define cpu_to_le16(x)               ((uint16_t)BSWAP16(x))
> > +#define cpu_to_le32(x)               ((uint32_t)BSWAP32(x))
> > +#define cpu_to_le64(x)               ((uint64_t)BSWAP64(x))
> > +
> > +#define le16_to_cpu(x)               ((uint16_t)BSWAP16(x))
> > +#define le32_to_cpu(x)               ((uint32_t)BSWAP32(x))
> > +#define le64_to_cpu(x)               ((uint64_t)BSWAP64(x))
> > +#endif
> > +
> > +#endif /* __SBI_BYTEORDER_H__ */
>
>
> --
> opensbi mailing list
> opensbi at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-02-09  3:25 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-02  4:44 [PATCH v2 0/3] Add endianness conversion support Rahul Pathak
2023-02-02  4:44 ` [PATCH v2 1/3] include: Add support for byteorder/endianness conversion Rahul Pathak
2023-02-08 12:46   ` Anup Patel
2023-02-09  0:28   ` Samuel Holland
2023-02-09  3:25     ` Anup Patel
2023-02-02  4:44 ` [PATCH v2 2/3] lib: utils/fdt: Use byteorder conversion functions in libfdt_env.h Rahul Pathak
2023-02-08 12:53   ` Anup Patel
2023-02-02  4:44 ` [PATCH v2 3/3] include: types: Add typedefs for endianness Rahul Pathak
2023-02-08 12:56   ` Anup Patel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox