7b69fd1034268cc3888f9763799efd9713106207
[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 /* Completion stuff */
233 #define DECLARE_COMPLETION(n) int n = 0
234 #define complete(n) { *n = 1; }
235 #define wait_for_completion(n) \
236 do { \
237         while (!*n) { \
238                 bman_poll(); \
239                 qman_poll(); \
240         } \
241         *n = 0; \
242 } while (0)
243
244 /* Allocator stuff */
245 #define kmalloc(sz, t)  malloc(sz)
246 #define vmalloc(sz)     malloc(sz)
247 #define kfree(p)        { if (p) free(p); }
248 static inline void *kzalloc(size_t sz, gfp_t __foo __rte_unused)
249 {
250         void *ptr = malloc(sz);
251
252         if (ptr)
253                 memset(ptr, 0, sz);
254         return ptr;
255 }
256
257 static inline unsigned long get_zeroed_page(gfp_t __foo __rte_unused)
258 {
259         void *p;
260
261         if (posix_memalign(&p, 4096, 4096))
262                 return 0;
263         memset(p, 0, 4096);
264         return (unsigned long)p;
265 }
266
267 static inline void free_page(unsigned long p)
268 {
269         free((void *)p);
270 }
271
272 /* Bitfield stuff. */
273 #define BITS_PER_ULONG  (sizeof(unsigned long) << 3)
274 #define SHIFT_PER_ULONG (((1 << 5) == BITS_PER_ULONG) ? 5 : 6)
275 #define BITS_MASK(idx)  ((unsigned long)1 << ((idx) & (BITS_PER_ULONG - 1)))
276 #define BITS_IDX(idx)   ((idx) >> SHIFT_PER_ULONG)
277 static inline unsigned long test_bits(unsigned long mask,
278                                       volatile unsigned long *p)
279 {
280         return *p & mask;
281 }
282
283 static inline int test_bit(int idx, volatile unsigned long *bits)
284 {
285         return test_bits(BITS_MASK(idx), bits + BITS_IDX(idx));
286 }
287
288 static inline void set_bits(unsigned long mask, volatile unsigned long *p)
289 {
290         *p |= mask;
291 }
292
293 static inline void set_bit(int idx, volatile unsigned long *bits)
294 {
295         set_bits(BITS_MASK(idx), bits + BITS_IDX(idx));
296 }
297
298 static inline void clear_bits(unsigned long mask, volatile unsigned long *p)
299 {
300         *p &= ~mask;
301 }
302
303 static inline void clear_bit(int idx, volatile unsigned long *bits)
304 {
305         clear_bits(BITS_MASK(idx), bits + BITS_IDX(idx));
306 }
307
308 static inline unsigned long test_and_set_bits(unsigned long mask,
309                                               volatile unsigned long *p)
310 {
311         unsigned long ret = test_bits(mask, p);
312
313         set_bits(mask, p);
314         return ret;
315 }
316
317 static inline int test_and_set_bit(int idx, volatile unsigned long *bits)
318 {
319         int ret = test_bit(idx, bits);
320
321         set_bit(idx, bits);
322         return ret;
323 }
324
325 static inline int test_and_clear_bit(int idx, volatile unsigned long *bits)
326 {
327         int ret = test_bit(idx, bits);
328
329         clear_bit(idx, bits);
330         return ret;
331 }
332
333 static inline int find_next_zero_bit(unsigned long *bits, int limit, int idx)
334 {
335         while ((++idx < limit) && test_bit(idx, bits))
336                 ;
337         return idx;
338 }
339
340 static inline int find_first_zero_bit(unsigned long *bits, int limit)
341 {
342         int idx = 0;
343
344         while (test_bit(idx, bits) && (++idx < limit))
345                 ;
346         return idx;
347 }
348
349 static inline u64 div64_u64(u64 n, u64 d)
350 {
351         return n / d;
352 }
353
354 #define atomic_t                rte_atomic32_t
355 #define atomic_read(v)          rte_atomic32_read(v)
356 #define atomic_set(v, i)        rte_atomic32_set(v, i)
357
358 #define atomic_inc(v)           rte_atomic32_add(v, 1)
359 #define atomic_dec(v)           rte_atomic32_sub(v, 1)
360
361 #define atomic_inc_and_test(v)  rte_atomic32_inc_and_test(v)
362 #define atomic_dec_and_test(v)  rte_atomic32_dec_and_test(v)
363
364 #define atomic_inc_return(v)    rte_atomic32_add_return(v, 1)
365 #define atomic_dec_return(v)    rte_atomic32_sub_return(v, 1)
366 #define atomic_sub_and_test(i, v) (rte_atomic32_sub_return(v, i) == 0)
367
368 #endif /* HEADER_COMPAT_H */