xref: /dpdk/drivers/common/dpaax/caamflib/compat.h (revision 3178e37c65a676366f33f0bc56f49d9b26a06448)
1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2  *
3  * Copyright 2013-2016 Freescale Semiconductor Inc.
4  * Copyright 2016 NXP
5  *
6  */
7 
8 #ifndef __RTA_COMPAT_H__
9 #define __RTA_COMPAT_H__
10 
11 #include <stdint.h>
12 #include <errno.h>
13 
14 #ifdef RTE_EXEC_ENV_LINUX
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <stdbool.h>
19 
20 #include <rte_byteorder.h>
21 #include <rte_common.h>
22 
23 #include "dpaax_logs.h"
24 
25 #ifndef __BYTE_ORDER__
26 #error "Undefined endianness"
27 #endif
28 
29 #else /* !RTE_EXEC_ENV_LINUX */
30 #error Environment not supported!
31 #endif
32 
33 #ifndef __always_inline
34 #define __always_inline __rte_always_inline
35 #endif
36 
37 #ifndef __always_unused
38 #define __always_unused __rte_unused
39 #endif
40 
41 #ifndef __maybe_unused
42 #define __maybe_unused __rte_unused
43 #endif
44 
45 #if defined(SUPPRESS_PRINTS)
46 #define pr_msg(l, fmt, ...) do { } while (0)
47 #else
48 #define pr_msg(l, fmt, ...) \
49 	RTE_LOG(l, DPAAX_LOGGER, "%s(): " fmt "\n", __func__, ##__VA_ARGS__)
50 #endif
51 
52 #if !defined(pr_debug)
53 #if defined(RTA_DEBUG)
54 #define pr_debug(fmt, ...) pr_msg(DEBUG, fmt, ##__VA_ARGS__)
55 #else
56 #define pr_debug(fmt, ...) do { } while (0)
57 #endif
58 #endif /* pr_debug */
59 
60 #if !defined(pr_err)
61 #define pr_err(fmt, ...) pr_msg(ERR, fmt, ##__VA_ARGS__)
62 #endif /* pr_err */
63 
64 #if !defined(pr_warn)
65 #define pr_warn(fmt, ...) pr_msg(WARNING, fmt, ##__VA_ARGS__)
66 #endif /* pr_warn */
67 
68 /**
69  * ARRAY_SIZE - returns the number of elements in an array
70  * @x: array
71  */
72 #ifndef ARRAY_SIZE
73 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
74 #endif
75 
76 #ifndef ALIGN
77 #define ALIGN(x, a) (((x) + ((__typeof__(x))(a) - 1)) & \
78 			~((__typeof__(x))(a) - 1))
79 #endif
80 
81 #ifndef BIT
82 #define BIT(nr)		(1UL << (nr))
83 #endif
84 
85 #ifndef upper_32_bits
86 /**
87  * upper_32_bits - return bits 32-63 of a number
88  * @n: the number we're accessing
89  */
90 #define upper_32_bits(n) ((uint32_t)(((n) >> 16) >> 16))
91 #endif
92 
93 #ifndef lower_32_bits
94 /**
95  * lower_32_bits - return bits 0-31 of a number
96  * @n: the number we're accessing
97  */
98 #define lower_32_bits(n) ((uint32_t)(n))
99 #endif
100 
101 /* Use Linux naming convention */
102 #if defined(RTE_EXEC_ENV_LINUX) || defined(__GLIBC__)
103 	#define swab16(x) rte_bswap16(x)
104 	#define swab32(x) rte_bswap32(x)
105 	#define swab64(x) rte_bswap64(x)
106 	/* Define cpu_to_be32 macro if not defined in the build environment */
107 	#if !defined(cpu_to_be32)
108 		#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
109 			#define cpu_to_be32(x)	(x)
110 		#else
111 			#define cpu_to_be32(x)	swab32(x)
112 		#endif
113 	#endif
114 	/* Define cpu_to_le32 macro if not defined in the build environment */
115 	#if !defined(cpu_to_le32)
116 		#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
117 			#define cpu_to_le32(x)	swab32(x)
118 		#else
119 			#define cpu_to_le32(x)	(x)
120 		#endif
121 	#endif
122 #endif
123 
124 #endif /* __RTA_COMPAT_H__ */
125