8885d60ce069372cac4c2067346331a0b0ddc43b
[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 likely(x)       __builtin_expect(!!(x), 1)
69 #define unlikely(x)     __builtin_expect(!!(x), 0)
70 #define ____cacheline_aligned __attribute__((aligned(L1_CACHE_BYTES)))
71
72 #ifdef ARRAY_SIZE
73 #undef ARRAY_SIZE
74 #endif
75 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
76
77 /* Required types */
78 typedef uint8_t         u8;
79 typedef uint16_t        u16;
80 typedef uint32_t        u32;
81 typedef uint64_t        u64;
82 typedef uint64_t        dma_addr_t;
83 typedef cpu_set_t       cpumask_t;
84 typedef unsigned int    gfp_t;
85 typedef uint32_t        phandle;
86
87 /* I/O operations */
88 static inline u32 in_be32(volatile void *__p)
89 {
90         volatile u32 *p = __p;
91         return *p;
92 }
93
94 static inline void out_be32(volatile void *__p, u32 val)
95 {
96         volatile u32 *p = __p;
97         *p = val;
98 }
99
100 /* Debugging */
101 #define prflush(fmt, args...) \
102         do { \
103                 printf(fmt, ##args); \
104                 fflush(stdout); \
105         } while (0)
106 #define pr_crit(fmt, args...)    prflush("CRIT:" fmt, ##args)
107 #define pr_err(fmt, args...)     prflush("ERR:" fmt, ##args)
108 #define pr_warn(fmt, args...)    prflush("WARN:" fmt, ##args)
109 #define pr_info(fmt, args...)    prflush(fmt, ##args)
110
111 #ifdef RTE_LIBRTE_DPAA2_DEBUG_BUS
112 #ifdef pr_debug
113 #undef pr_debug
114 #endif
115 #define pr_debug(fmt, args...) {}
116 #define WARN_ON(c, str) \
117 do { \
118         static int warned_##__LINE__; \
119         if ((c) && !warned_##__LINE__) { \
120                 pr_warn("%s\n", str); \
121                 pr_warn("(%s:%d)\n", __FILE__, __LINE__); \
122                 warned_##__LINE__ = 1; \
123         } \
124 } while (0)
125 #ifdef CONFIG_BUGON
126 #define QBMAN_BUG_ON(c) WARN_ON(c, "BUG")
127 #else
128 #define QBMAN_BUG_ON(c) {}
129 #endif
130
131 #define ALIGN(x, a) (((x) + ((typeof(x))(a) - 1)) & ~((typeof(x))(a) - 1))
132
133 /* Other miscellaneous interfaces our APIs depend on; */
134 #define lower_32_bits(x) ((u32)(x))
135 #define upper_32_bits(x) ((u32)(((x) >> 16) >> 16))
136
137 /* Compiler/type stuff */
138
139 #define __iomem
140 #define GFP_KERNEL      0
141 #define __raw_readb(p)  (*(const volatile unsigned char *)(p))
142 #define __raw_readl(p)  (*(const volatile unsigned int *)(p))
143 #define __raw_writel(v, p) {*(volatile unsigned int *)(p) = (v); }
144
145 /* Allocator stuff */
146 #define kmalloc(sz, t)  malloc(sz)
147 #define kfree(p)        { if (p) free(p); }
148 static inline void *kzalloc(size_t sz, gfp_t __foo __rte_unused)
149 {
150         void *ptr = malloc(sz);
151
152         if (ptr)
153                 memset(ptr, 0, sz);
154         return ptr;
155 }
156
157 static inline unsigned long get_zeroed_page(gfp_t __foo __rte_unused)
158 {
159         void *p;
160
161         if (posix_memalign(&p, 4096, 4096))
162                 return 0;
163         memset(p, 0, 4096);
164         return (unsigned long)p;
165 }
166
167 static inline void free_page(unsigned long p)
168 {
169         free((void *)p);
170 }
171
172 #define atomic_t                rte_atomic32_t
173 #define atomic_read(v)          rte_atomic32_read(v)
174 #define atomic_set(v, i)        rte_atomic32_set(v, i)
175
176 #define atomic_inc(v)           rte_atomic32_add(v, 1)
177 #define atomic_dec(v)           rte_atomic32_sub(v, 1)
178
179 #define atomic_inc_and_test(v)  rte_atomic32_inc_and_test(v)
180 #define atomic_dec_and_test(v)  rte_atomic32_dec_and_test(v)
181
182 #define atomic_inc_return(v)    rte_atomic32_add_return(v, 1)
183 #define atomic_dec_return(v)    rte_atomic32_sub_return(v, 1)
184 #define atomic_sub_and_test(i, v) (rte_atomic32_sub_return(v, i) == 0)
185
186 #endif /* HEADER_COMPAT_H */