build: remove redundant _GNU_SOURCE definitions
[dpdk.git] / drivers / common / dpaax / compat.h
1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2  *
3  * Copyright 2011 Freescale Semiconductor, Inc.
4  * All rights reserved.
5  * Copyright 2019-2020 NXP
6  *
7  */
8
9 #ifndef __COMPAT_H
10 #define __COMPAT_H
11
12 #include <sched.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <stddef.h>
16 #include <stdio.h>
17 #include <errno.h>
18 #include <string.h>
19 #include <pthread.h>
20 #include <linux/types.h>
21 #include <stdbool.h>
22 #include <ctype.h>
23 #include <malloc.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <sys/mman.h>
29 #include <limits.h>
30 #include <assert.h>
31 #include <dirent.h>
32 #include <inttypes.h>
33 #include <error.h>
34 #include <rte_byteorder.h>
35 #include <rte_atomic.h>
36 #include <rte_spinlock.h>
37 #include <rte_common.h>
38 #include <rte_debug.h>
39 #include <rte_cycles.h>
40 #include <rte_malloc.h>
41
42 /* The following definitions are primarily to allow the single-source driver
43  * interfaces to be included by arbitrary program code. Ie. for interfaces that
44  * are also available in kernel-space, these definitions provide compatibility
45  * with certain attributes and types used in those interfaces.
46  */
47
48 /* Required compiler attributes */
49 #ifndef __maybe_unused
50 #define __maybe_unused  __rte_unused
51 #endif
52 #ifndef __always_unused
53 #define __always_unused __rte_unused
54 #endif
55 #ifndef __packed
56 #define __packed        __rte_packed
57 #endif
58 #ifndef noinline
59 #define noinline        __rte_noinline
60 #endif
61 #define L1_CACHE_BYTES 64
62 #define ____cacheline_aligned __rte_aligned(L1_CACHE_BYTES)
63 #define __stringify_1(x) #x
64 #define __stringify(x)  __stringify_1(x)
65
66 #ifdef ARRAY_SIZE
67 #undef ARRAY_SIZE
68 #endif
69 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
70
71 /* Debugging */
72 #define prflush(fmt, args...) \
73         do { \
74                 printf(fmt, ##args); \
75                 fflush(stdout); \
76         } while (0)
77 #ifndef pr_crit
78 #define pr_crit(fmt, args...)    prflush("CRIT:" fmt, ##args)
79 #endif
80 #ifndef pr_err
81 #define pr_err(fmt, args...)     prflush("ERR:" fmt, ##args)
82 #endif
83 #ifndef pr_warn
84 #define pr_warn(fmt, args...)    prflush("WARN:" fmt, ##args)
85 #endif
86 #ifndef pr_info
87 #define pr_info(fmt, args...)    prflush(fmt, ##args)
88 #endif
89 #ifndef pr_debug
90 #ifdef RTE_LIBRTE_DPAA_DEBUG_BUS
91 #define pr_debug(fmt, args...)  printf(fmt, ##args)
92 #else
93 #define pr_debug(fmt, args...) {}
94 #endif
95 #endif
96
97 #define DPAA_BUG_ON(x) RTE_ASSERT(x)
98
99 /* Required types */
100 typedef uint8_t         u8;
101 typedef uint16_t        u16;
102 typedef uint32_t        u32;
103 typedef uint64_t        u64;
104 typedef uint64_t        dma_addr_t;
105 typedef cpu_set_t       cpumask_t;
106 typedef uint32_t        phandle;
107 typedef uint32_t        gfp_t;
108 typedef uint32_t        irqreturn_t;
109
110 #define ETHER_ADDR_LEN 6
111
112 #define IRQ_HANDLED     0
113 #define request_irq     qbman_request_irq
114 #define free_irq        qbman_free_irq
115
116 #define __iomem
117 #define GFP_KERNEL      0
118 #define __raw_readb(p)  (*(const volatile unsigned char *)(p))
119 #define __raw_readl(p)  (*(const volatile unsigned int *)(p))
120 #define __raw_writel(v, p) {*(volatile unsigned int *)(p) = (v); }
121
122 /* to be used as an upper-limit only */
123 #define NR_CPUS                 64
124
125 /* Waitqueue stuff */
126 typedef struct { }              wait_queue_head_t;
127 #define DECLARE_WAIT_QUEUE_HEAD(x) int dummy_##x __always_unused
128 #define wake_up(x)              do { } while (0)
129
130 /* I/O operations */
131 static inline u32 in_be32(volatile void *__p)
132 {
133         volatile u32 *p = __p;
134         return rte_be_to_cpu_32(*p);
135 }
136
137 static inline void out_be32(volatile void *__p, u32 val)
138 {
139         volatile u32 *p = __p;
140         *p = rte_cpu_to_be_32(val);
141 }
142
143 #define hwsync() rte_rmb()
144 #define lwsync() rte_wmb()
145
146 #define dcbt_ro(p) __builtin_prefetch(p, 0)
147 #define dcbt_rw(p) __builtin_prefetch(p, 1)
148
149 #if defined(RTE_ARCH_ARM)
150 #if defined(RTE_ARCH_64)
151 #define dcbz(p) { asm volatile("dc zva, %0" : : "r" (p) : "memory"); }
152 #define dcbz_64(p) dcbz(p)
153 #define dcbf(p) { asm volatile("dc cvac, %0" : : "r"(p) : "memory"); }
154 #define dcbf_64(p) dcbf(p)
155 #define dccivac(p) { asm volatile("dc civac, %0" : : "r"(p) : "memory"); }
156
157 #define dcbit_ro(p) \
158         do { \
159                 dccivac(p);                                             \
160                 asm volatile("prfm pldl1keep, [%0, #64]" : : "r" (p));  \
161         } while (0)
162
163 #else /* RTE_ARCH_32 */
164 #define dcbz(p) memset((p), 0, 32)
165 #define dcbz_64(p) memset((p), 0, 64)
166 #define dcbf(p) RTE_SET_USED(p)
167 #define dcbf_64(p) dcbf(p)
168 #define dccivac(p)      RTE_SET_USED(p)
169 #define dcbit_ro(p)     RTE_SET_USED(p)
170 #endif
171
172 #else
173 #define dcbz(p) RTE_SET_USED(p)
174 #define dcbz_64(p) dcbz(p)
175 #define dcbf(p) RTE_SET_USED(p)
176 #define dcbf_64(p) dcbf(p)
177 #define dccivac(p)      RTE_SET_USED(p)
178 #define dcbit_ro(p)     RTE_SET_USED(p)
179 #endif
180
181 #define barrier() { asm volatile ("" : : : "memory"); }
182 #define cpu_relax barrier
183
184 #if defined(RTE_ARCH_ARM64)
185 static inline uint64_t mfatb(void)
186 {
187         uint64_t ret, ret_new, timeout = 200;
188
189         asm volatile ("mrs %0, cntvct_el0" : "=r" (ret));
190         asm volatile ("mrs %0, cntvct_el0" : "=r" (ret_new));
191         while (ret != ret_new && timeout--) {
192                 ret = ret_new;
193                 asm volatile ("mrs %0, cntvct_el0" : "=r" (ret_new));
194         }
195         DPAA_BUG_ON(!timeout && (ret != ret_new));
196         return ret * 64;
197 }
198 #else
199
200 #define mfatb rte_rdtsc
201
202 #endif
203
204 /* Spin for a few cycles without bothering the bus */
205 static inline void cpu_spin(int cycles)
206 {
207         uint64_t now = mfatb();
208
209         while (mfatb() < (now + cycles))
210                 ;
211 }
212
213 /* Qman/Bman API inlines and macros; */
214 #ifdef lower_32_bits
215 #undef lower_32_bits
216 #endif
217 #define lower_32_bits(x) ((u32)(x))
218
219 #ifdef upper_32_bits
220 #undef upper_32_bits
221 #endif
222 #define upper_32_bits(x) ((u32)(((x) >> 16) >> 16))
223
224 /*
225  * Swap bytes of a 48-bit value.
226  */
227 static inline uint64_t
228 __bswap_48(uint64_t x)
229 {
230         return  ((x & 0x0000000000ffULL) << 40) |
231                 ((x & 0x00000000ff00ULL) << 24) |
232                 ((x & 0x000000ff0000ULL) <<  8) |
233                 ((x & 0x0000ff000000ULL) >>  8) |
234                 ((x & 0x00ff00000000ULL) >> 24) |
235                 ((x & 0xff0000000000ULL) >> 40);
236 }
237
238 /*
239  * Swap bytes of a 40-bit value.
240  */
241 static inline uint64_t
242 __bswap_40(uint64_t x)
243 {
244         return  ((x & 0x00000000ffULL) << 32) |
245                 ((x & 0x000000ff00ULL) << 16) |
246                 ((x & 0x0000ff0000ULL)) |
247                 ((x & 0x00ff000000ULL) >> 16) |
248                 ((x & 0xff00000000ULL) >> 32);
249 }
250
251 /*
252  * Swap bytes of a 24-bit value.
253  */
254 static inline uint32_t
255 __bswap_24(uint32_t x)
256 {
257         return  ((x & 0x0000ffULL) << 16) |
258                 ((x & 0x00ff00ULL)) |
259                 ((x & 0xff0000ULL) >> 16);
260 }
261
262 #define be64_to_cpu(x) rte_be_to_cpu_64(x)
263 #define be32_to_cpu(x) rte_be_to_cpu_32(x)
264 #define be16_to_cpu(x) rte_be_to_cpu_16(x)
265
266 #define cpu_to_be64(x) rte_cpu_to_be_64(x)
267 #if !defined(cpu_to_be32)
268 #define cpu_to_be32(x) rte_cpu_to_be_32(x)
269 #endif
270 #define cpu_to_be16(x) rte_cpu_to_be_16(x)
271
272 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
273
274 #define cpu_to_be48(x) __bswap_48(x)
275 #define be48_to_cpu(x) __bswap_48(x)
276
277 #define cpu_to_be40(x) __bswap_40(x)
278 #define be40_to_cpu(x) __bswap_40(x)
279
280 #define cpu_to_be24(x) __bswap_24(x)
281 #define be24_to_cpu(x) __bswap_24(x)
282
283 #else /* RTE_BIG_ENDIAN */
284
285 #define cpu_to_be48(x) (x)
286 #define be48_to_cpu(x) (x)
287
288 #define cpu_to_be40(x) (x)
289 #define be40_to_cpu(x) (x)
290
291 #define cpu_to_be24(x) (x)
292 #define be24_to_cpu(x) (x)
293
294 #endif /* RTE_BIG_ENDIAN */
295
296 /* When copying aligned words or shorts, try to avoid memcpy() */
297 /* memcpy() stuff - when you know alignments in advance */
298 #define CONFIG_TRY_BETTER_MEMCPY
299
300 #ifdef CONFIG_TRY_BETTER_MEMCPY
301 static inline void copy_words(void *dest, const void *src, size_t sz)
302 {
303         u32 *__dest = dest;
304         const u32 *__src = src;
305         size_t __sz = sz >> 2;
306
307         DPAA_BUG_ON((unsigned long)dest & 0x3);
308         DPAA_BUG_ON((unsigned long)src & 0x3);
309         DPAA_BUG_ON(sz & 0x3);
310         while (__sz--)
311                 *(__dest++) = *(__src++);
312 }
313
314 static inline void copy_shorts(void *dest, const void *src, size_t sz)
315 {
316         u16 *__dest = dest;
317         const u16 *__src = src;
318         size_t __sz = sz >> 1;
319
320         DPAA_BUG_ON((unsigned long)dest & 0x1);
321         DPAA_BUG_ON((unsigned long)src & 0x1);
322         DPAA_BUG_ON(sz & 0x1);
323         while (__sz--)
324                 *(__dest++) = *(__src++);
325 }
326
327 static inline void copy_bytes(void *dest, const void *src, size_t sz)
328 {
329         u8 *__dest = dest;
330         const u8 *__src = src;
331
332         while (sz--)
333                 *(__dest++) = *(__src++);
334 }
335 #else
336 #define copy_words memcpy
337 #define copy_shorts memcpy
338 #define copy_bytes memcpy
339 #endif
340
341 /* Allocator stuff */
342 #define kmalloc(sz, t)  rte_malloc(NULL, sz, 0)
343 #define vmalloc(sz)     rte_malloc(NULL, sz, 0)
344 #define kfree(p)        { if (p) rte_free(p); }
345 static inline void *kzalloc(size_t sz, gfp_t __foo __rte_unused)
346 {
347         void *ptr = rte_malloc(NULL, sz, 0);
348
349         if (ptr)
350                 memset(ptr, 0, sz);
351         return ptr;
352 }
353
354 static inline unsigned long get_zeroed_page(gfp_t __foo __rte_unused)
355 {
356         void *p;
357
358         if (posix_memalign(&p, 4096, 4096))
359                 return 0;
360         memset(p, 0, 4096);
361         return (unsigned long)p;
362 }
363
364 /* Spinlock stuff */
365 #define spinlock_t              rte_spinlock_t
366 #define __SPIN_LOCK_UNLOCKED(x) RTE_SPINLOCK_INITIALIZER
367 #define DEFINE_SPINLOCK(x)      spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
368 #define spin_lock_init(x)       rte_spinlock_init(x)
369 #define spin_lock_destroy(x)
370 #define spin_lock(x)            rte_spinlock_lock(x)
371 #define spin_unlock(x)          rte_spinlock_unlock(x)
372 #define spin_lock_irq(x)        spin_lock(x)
373 #define spin_unlock_irq(x)      spin_unlock(x)
374 #define spin_lock_irqsave(x, f) spin_lock_irq(x)
375 #define spin_unlock_irqrestore(x, f) spin_unlock_irq(x)
376
377 #define atomic_t                rte_atomic32_t
378 #define atomic_read(v)          rte_atomic32_read(v)
379 #define atomic_set(v, i)        rte_atomic32_set(v, i)
380
381 #define atomic_inc(v)           rte_atomic32_add(v, 1)
382 #define atomic_dec(v)           rte_atomic32_sub(v, 1)
383
384 #define atomic_inc_and_test(v)  rte_atomic32_inc_and_test(v)
385 #define atomic_dec_and_test(v)  rte_atomic32_dec_and_test(v)
386
387 #define atomic_inc_return(v)    rte_atomic32_add_return(v, 1)
388 #define atomic_dec_return(v)    rte_atomic32_sub_return(v, 1)
389 #define atomic_sub_and_test(i, v) (rte_atomic32_sub_return(v, i) == 0)
390
391 /* Interface name len*/
392 #define IF_NAME_MAX_LEN 16
393
394 #endif /* __COMPAT_H */