Skip to contentSkip to conversation

Add endianness conversion support

PATCH New Rahul Pathak 3 participants 9 messages Feb 2, 2023 – Feb 9, 2023

lore
Share & tools
lore
#0 lore raw

v2 cover Add endianness conversion support

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
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/3 include: Add support for byteorder/endianness conversion

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

---
 include/sbi/sbi_byteorder.h | 57 +++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
 create mode 100644 include/sbi/sbi_byteorder.h
include/sbi/sbi_byteorder.h new file +57 -0
new file mode 100644
index 000000000000..680710fe6d91
@@ -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__ */
Signature · 1 line
2.34.1
#5 lore raw

PATCH v2 2/3 lib: utils/fdt: Use byteorder conversion functions in libfdt_env.h

In reply to Rahul Pathak

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

---
 lib/utils/libfdt/libfdt_env.h | 29 ++++++++++-------------------
 1 file changed, 10 insertions(+), 19 deletions(-)
lib/utils/libfdt/libfdt_env.h +10 -19
index f9d9c6783c52..e5ad7698efca 100644
@@ -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>
Signature · 1 line
2.34.1
#7 lore raw

PATCH v2 3/3 include: types: Add typedefs for endianness

In reply to Rahul Pathak

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

---
 include/sbi/sbi_types.h       | 7 +++++++
 lib/utils/libfdt/libfdt_env.h | 6 +++---
 2 files changed, 10 insertions(+), 3 deletions(-)
include/sbi/sbi_types.h +7 -0
index 9c1fef3b8dc6..def88bbad2c3 100644
@@ -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
 
lib/utils/libfdt/libfdt_env.h +3 -3
index e5ad7698efca..6ebbae01b514 100644
@@ -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)
 {
Signature · 1 line
2.34.1
Applied
#2 lore raw

PATCH v2 1/3 include: Add support for byteorder/endianness conversion

In reply to Rahul Pathak
Quoted 8 lines · Rahul Pathak

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

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

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 76 lines
---
 include/sbi/sbi_byteorder.h | 57 +++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
 create mode 100644 include/sbi/sbi_byteorder.h
include/sbi/sbi_byteorder.h new file +57 -1
new file mode 100644
index 000000000000..680710fe6d91
@@ -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

Applied
#6 lore raw

PATCH v2 2/3 lib: utils/fdt: Use byteorder conversion functions in libfdt_env.h

In reply to Rahul Pathak
Quoted 6 lines · Rahul Pathak

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.

Quoted 6 lines

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

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 79 lines
---
 lib/utils/libfdt/libfdt_env.h | 29 ++++++++++-------------------
 1 file changed, 10 insertions(+), 19 deletions(-)
lib/utils/libfdt/libfdt_env.h +10 -20
index f9d9c6783c52..e5ad7698efca 100644
@@ -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

Applied
#8 lore raw

PATCH v2 3/3 include: types: Add typedefs for endianness

In reply to Rahul Pathak
Quoted 10 lines · Rahul Pathak

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

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 48 lines
---
 include/sbi/sbi_types.h       | 7 +++++++
 lib/utils/libfdt/libfdt_env.h | 6 +++---
 2 files changed, 10 insertions(+), 3 deletions(-)
include/sbi/sbi_types.h +7 -0
index 9c1fef3b8dc6..def88bbad2c3 100644
@@ -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
 
lib/utils/libfdt/libfdt_env.h +3 -4
index e5ad7698efca..6ebbae01b514 100644
@@ -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

#3 lore raw

PATCH v2 1/3 include: Add support for byteorder/endianness conversion

In reply to Rahul Pathak
Quoted 28 lines · Rahul Pathak

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

---
 include/sbi/sbi_byteorder.h | 57 +++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
 create mode 100644 include/sbi/sbi_byteorder.h
include/sbi/sbi_byteorder.h new file +10 -0
new file mode 100644
index 000000000000..680710fe6d91
@@ -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>

include/sbi/sbi_byteorder.h line 20

Quoted 10 lines
include/sbi/sbi_byteorder.h new file +10 -0
+
+#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

Quoted 35 lines
include/sbi/sbi_byteorder.h new file +35 -0
+#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__ */
#4 lore raw

PATCH v2 1/3 include: Add support for byteorder/endianness conversion

Quoted 45 lines · Samuel Holland
Quoted 28 lines · Rahul Pathak

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

---
 include/sbi/sbi_byteorder.h | 57 +++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
 create mode 100644 include/sbi/sbi_byteorder.h
include/sbi/sbi_byteorder.h new file +10 -0
new file mode 100644
index 000000000000..680710fe6d91
@@ -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>

include/sbi/sbi_byteorder.h line 20

Quoted 10 lines
include/sbi/sbi_byteorder.h new file +10 -0
+
+#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.

Quoted 1 line

(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

Quoted 45 lines

Regards,
Samuel

Quoted 25 lines
include/sbi/sbi_byteorder.h new file +25 -0
+#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))
+

include/sbi/sbi_byteorder.h line 57

Quoted 10 lines
include/sbi/sbi_byteorder.h new file +10 -0
+#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