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