529f1ea3320d41e7e47d2baecb7a220bad1ee289
[deb_dpdk.git] / drivers / bus / fslmc / qbman / include / compat.h
1 /*-
2  *   BSD LICENSE
3  *
4  * Copyright (c) 2008-2016 Freescale Semiconductor, Inc.
5  * Copyright 2017 NXP.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above copyright
12  *       notice, this list of conditions and the following disclaimer in the
13  *       documentation and/or other materials provided with the distribution.
14  *     * Neither the name of Freescale Semiconductor nor the
15  *       names of its contributors may be used to endorse or promote products
16  *       derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef HEADER_COMPAT_H
31 #define HEADER_COMPAT_H
32
33 #include <sched.h>
34
35 #ifndef _GNU_SOURCE
36 #define _GNU_SOURCE
37 #endif
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <stddef.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <pthread.h>
44 #include <net/ethernet.h>
45 #include <stdio.h>
46 #include <stdbool.h>
47 #include <ctype.h>
48 #include <malloc.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <fcntl.h>
52 #include <unistd.h>
53 #include <sys/mman.h>
54 #include <limits.h>
55 #include <assert.h>
56 #include <dirent.h>
57 #include <inttypes.h>
58 #include <error.h>
59 #include <rte_atomic.h>
60
61 /* The following definitions are primarily to allow the single-source driver
62  * interfaces to be included by arbitrary program code. Ie. for interfaces that
63  * are also available in kernel-space, these definitions provide compatibility
64  * with certain attributes and types used in those interfaces.
65  */
66
67 /* Required compiler attributes */
68 #define __user
69 #define likely(x)       __builtin_expect(!!(x), 1)
70 #define unlikely(x)     __builtin_expect(!!(x), 0)
71 #define ____cacheline_aligned __attribute__((aligned(L1_CACHE_BYTES)))
72 #undef container_of
73 #define container_of(ptr, type, member) ({ \
74                 typeof(((type *)0)->member)(*__mptr) = (ptr); \
75                 (type *)((char *)__mptr - offsetof(type, member)); })
76 #define __stringify_1(x) #x
77 #define __stringify(x)  __stringify_1(x)
78
79 #ifdef ARRAY_SIZE
80 #undef ARRAY_SIZE
81 #endif
82 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
83
84 /* Required types */
85 typedef uint8_t         u8;
86 typedef uint16_t        u16;
87 typedef uint32_t        u32;
88 typedef uint64_t        u64;
89 typedef uint64_t        dma_addr_t;
90 typedef cpu_set_t       cpumask_t;
91 typedef u32             compat_uptr_t;
92
93 static inline void __user *compat_ptr(compat_uptr_t uptr)
94 {
95         return (void __user *)(unsigned long)uptr;
96 }
97
98 static inline compat_uptr_t ptr_to_compat(void __user *uptr)
99 {
100         return (u32)(unsigned long)uptr;
101 }
102
103 /* I/O operations */
104 static inline u32 in_be32(volatile void *__p)
105 {
106         volatile u32 *p = __p;
107         return *p;
108 }
109
110 static inline void out_be32(volatile void *__p, u32 val)
111 {
112         volatile u32 *p = __p;
113         *p = val;
114 }
115
116 /* Debugging */
117 #define prflush(fmt, args...) \
118         do { \
119                 printf(fmt, ##args); \
120                 fflush(stdout); \
121         } while (0)
122 #define pr_crit(fmt, args...)    prflush("CRIT:" fmt, ##args)
123 #define pr_err(fmt, args...)     prflush("ERR:" fmt, ##args)
124 #define pr_warn(fmt, args...)    prflush("WARN:" fmt, ##args)
125 #define pr_info(fmt, args...)    prflush(fmt, ##args)
126
127 #ifdef pr_debug
128 #undef pr_debug
129 #endif
130 #define pr_debug(fmt, args...) {}
131 #define might_sleep_if(c) {}
132 #define msleep(x) {}
133 #define WARN_ON(c, str) \
134 do { \
135         static int warned_##__LINE__; \
136         if ((c) && !warned_##__LINE__) { \
137                 pr_warn("%s\n", str); \
138                 pr_warn("(%s:%d)\n", __FILE__, __LINE__); \
139                 warned_##__LINE__ = 1; \
140         } \
141 } while (0)
142 #ifdef CONFIG_BUGON
143 #define QBMAN_BUG_ON(c) WARN_ON(c, "BUG")
144 #else
145 #define QBMAN_BUG_ON(c) {}
146 #endif
147
148 #define ALIGN(x, a) (((x) + ((typeof(x))(a) - 1)) & ~((typeof(x))(a) - 1))
149
150 /****************/
151 /* Linked-lists */
152 /****************/
153
154 struct list_head {
155         struct list_head *prev;
156         struct list_head *next;
157 };
158
159 #define LIST_HEAD(n) \
160 struct list_head n = { \
161         .prev = &n, \
162         .next = &n \
163 }
164
165 #define INIT_LIST_HEAD(p) \
166 do { \
167         struct list_head *__p298 = (p); \
168         __p298->next = __p298; \
169         __p298->prev = __p298->next; \
170 } while (0)
171 #define list_entry(node, type, member) \
172         (type *)((void *)node - offsetof(type, member))
173 #define list_empty(p) \
174 ({ \
175         const struct list_head *__p298 = (p); \
176         ((__p298->next == __p298) && (__p298->prev == __p298)); \
177 })
178 #define list_add(p, l) \
179 do { \
180         struct list_head *__p298 = (p); \
181         struct list_head *__l298 = (l); \
182         __p298->next = __l298->next; \
183         __p298->prev = __l298; \
184         __l298->next->prev = __p298; \
185         __l298->next = __p298; \
186 } while (0)
187 #define list_add_tail(p, l) \
188 do { \
189         struct list_head *__p298 = (p); \
190         struct list_head *__l298 = (l); \
191         __p298->prev = __l298->prev; \
192         __p298->next = __l298; \
193         __l298->prev->next = __p298; \
194         __l298->prev = __p298; \
195 } while (0)
196 #define list_for_each(i, l)                             \
197         for (i = (l)->next; i != (l); i = i->next)
198 #define list_for_each_safe(i, j, l)                     \
199         for (i = (l)->next, j = i->next; i != (l);      \
200              i = j, j = i->next)
201 #define list_for_each_entry(i, l, name) \
202         for (i = list_entry((l)->next, typeof(*i), name); &i->name != (l); \
203                 i = list_entry(i->name.next, typeof(*i), name))
204 #define list_for_each_entry_safe(i, j, l, name) \
205         for (i = list_entry((l)->next, typeof(*i), name), \
206                 j = list_entry(i->name.next, typeof(*j), name); \
207                 &i->name != (l); \
208                 i = j, j = list_entry(j->name.next, typeof(*j), name))
209 #define list_del(i) \
210 do { \
211         (i)->next->prev = (i)->prev; \
212         (i)->prev->next = (i)->next; \
213 } while (0)
214
215 /* Other miscellaneous interfaces our APIs depend on; */
216
217 #define lower_32_bits(x) ((u32)(x))
218 #define upper_32_bits(x) ((u32)(((x) >> 16) >> 16))
219
220 /* Compiler/type stuff */
221 typedef unsigned int    gfp_t;
222 typedef uint32_t        phandle;
223
224 #define __iomem
225 #define EINTR           4
226 #define ENODEV          19
227 #define GFP_KERNEL      0
228 #define __raw_readb(p)  (*(const volatile unsigned char *)(p))
229 #define __raw_readl(p)  (*(const volatile unsigned int *)(p))
230 #define __raw_writel(v, p) {*(volatile unsigned int *)(p) = (v); }
231
232 /* memcpy() stuff - when you know alignments in advance */
233 #ifdef CONFIG_TRY_BETTER_MEMCPY
234 static inline void copy_words(void *dest, const void *src, size_t sz)
235 {
236         u32 *__dest = dest;
237         const u32 *__src = src;
238         size_t __sz = sz >> 2;
239
240         QBMAN_BUG_ON((unsigned long)dest & 0x3);
241         QBMAN_BUG_ON((unsigned long)src & 0x3);
242         QBMAN_BUG_ON(sz & 0x3);
243         while (__sz--)
244                 *(__dest++) = *(__src++);
245 }
246
247 static inline void copy_shorts(void *dest, const void *src, size_t sz)
248 {
249         u16 *__dest = dest;
250         const u16 *__src = src;
251         size_t __sz = sz >> 1;
252
253         QBMAN_BUG_ON((unsigned long)dest & 0x1);
254         QBMAN_BUG_ON((unsigned long)src & 0x1);
255         QBMAN_BUG_ON(sz & 0x1);
256         while (__sz--)
257                 *(__dest++) = *(__src++);
258 }
259
260 static inline void copy_bytes(void *dest, const void *src, size_t sz)
261 {
262         u8 *__dest = dest;
263         const u8 *__src = src;
264
265         while (sz--)
266                 *(__dest++) = *(__src++);
267 }
268 #else
269 #define copy_words memcpy
270 #define copy_shorts memcpy
271 #define copy_bytes memcpy
272 #endif
273
274 /* Completion stuff */
275 #define DECLARE_COMPLETION(n) int n = 0
276 #define complete(n) { *n = 1; }
277 #define wait_for_completion(n) \
278 do { \
279         while (!*n) { \
280                 bman_poll(); \
281                 qman_poll(); \
282         } \
283         *n = 0; \
284 } while (0)
285
286 /* Allocator stuff */
287 #define kmalloc(sz, t)  malloc(sz)
288 #define vmalloc(sz)     malloc(sz)
289 #define kfree(p)        { if (p) free(p); }
290 static inline void *kzalloc(size_t sz, gfp_t __foo __rte_unused)
291 {
292         void *ptr = malloc(sz);
293
294         if (ptr)
295                 memset(ptr, 0, sz);
296         return ptr;
297 }
298
299 static inline unsigned long get_zeroed_page(gfp_t __foo __rte_unused)
300 {
301         void *p;
302
303         if (posix_memalign(&p, 4096, 4096))
304                 return 0;
305         memset(p, 0, 4096);
306         return (unsigned long)p;
307 }
308
309 static inline void free_page(unsigned long p)
310 {
311         free((void *)p);
312 }
313
314 /* Bitfield stuff. */
315 #define BITS_PER_ULONG  (sizeof(unsigned long) << 3)
316 #define SHIFT_PER_ULONG (((1 << 5) == BITS_PER_ULONG) ? 5 : 6)
317 #define BITS_MASK(idx)  ((unsigned long)1 << ((idx) & (BITS_PER_ULONG - 1)))
318 #define BITS_IDX(idx)   ((idx) >> SHIFT_PER_ULONG)
319 static inline unsigned long test_bits(unsigned long mask,
320                                       volatile unsigned long *p)
321 {
322         return *p & mask;
323 }
324
325 static inline int test_bit(int idx, volatile unsigned long *bits)
326 {
327         return test_bits(BITS_MASK(idx), bits + BITS_IDX(idx));
328 }
329
330 static inline void set_bits(unsigned long mask, volatile unsigned long *p)
331 {
332         *p |= mask;
333 }
334
335 static inline void set_bit(int idx, volatile unsigned long *bits)
336 {
337         set_bits(BITS_MASK(idx), bits + BITS_IDX(idx));
338 }
339
340 static inline void clear_bits(unsigned long mask, volatile unsigned long *p)
341 {
342         *p &= ~mask;
343 }
344
345 static inline void clear_bit(int idx, volatile unsigned long *bits)
346 {
347         clear_bits(BITS_MASK(idx), bits + BITS_IDX(idx));
348 }
349
350 static inline unsigned long test_and_set_bits(unsigned long mask,
351                                               volatile unsigned long *p)
352 {
353         unsigned long ret = test_bits(mask, p);
354
355         set_bits(mask, p);
356         return ret;
357 }
358
359 static inline int test_and_set_bit(int idx, volatile unsigned long *bits)
360 {
361         int ret = test_bit(idx, bits);
362
363         set_bit(idx, bits);
364         return ret;
365 }
366
367 static inline int test_and_clear_bit(int idx, volatile unsigned long *bits)
368 {
369         int ret = test_bit(idx, bits);
370
371         clear_bit(idx, bits);
372         return ret;
373 }
374
375 static inline int find_next_zero_bit(unsigned long *bits, int limit, int idx)
376 {
377         while ((++idx < limit) && test_bit(idx, bits))
378                 ;
379         return idx;
380 }
381
382 static inline int find_first_zero_bit(unsigned long *bits, int limit)
383 {
384         int idx = 0;
385
386         while (test_bit(idx, bits) && (++idx < limit))
387                 ;
388         return idx;
389 }
390
391 static inline u64 div64_u64(u64 n, u64 d)
392 {
393         return n / d;
394 }
395
396 #define atomic_t                rte_atomic32_t
397 #define atomic_read(v)          rte_atomic32_read(v)
398 #define atomic_set(v, i)        rte_atomic32_set(v, i)
399
400 #define atomic_inc(v)           rte_atomic32_add(v, 1)
401 #define atomic_dec(v)           rte_atomic32_sub(v, 1)
402
403 #define atomic_inc_and_test(v)  rte_atomic32_inc_and_test(v)
404 #define atomic_dec_and_test(v)  rte_atomic32_dec_and_test(v)
405
406 #define atomic_inc_return(v)    rte_atomic32_add_return(v, 1)
407 #define atomic_dec_return(v)    rte_atomic32_sub_return(v, 1)
408 #define atomic_sub_and_test(i, v) (rte_atomic32_sub_return(v, i) == 0)
409
410 #endif /* HEADER_COMPAT_H */