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