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