New upstream version 17.11-rc3
[deb_dpdk.git] / drivers / bus / dpaa / include / compat.h
1 /*-
2  * This file is provided under a dual BSD/GPLv2 license. When using or
3  * redistributing this file, you may do so under either license.
4  *
5  *   BSD LICENSE
6  *
7  * Copyright 2011 Freescale Semiconductor, Inc.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * * Neither the name of the above-listed copyright holders nor the
18  * names of any contributors may be used to endorse or promote products
19  * derived from this software without specific prior written permission.
20  *
21  *   GPL LICENSE SUMMARY
22  *
23  * ALTERNATIVELY, this software may be distributed under the terms of the
24  * GNU General Public License ("GPL") as published by the Free Software
25  * Foundation, either version 2 of that License or (at your option) any
26  * later version.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 #ifndef __COMPAT_H
42 #define __COMPAT_H
43
44 #include <sched.h>
45
46 #ifndef _GNU_SOURCE
47 #define _GNU_SOURCE
48 #endif
49 #include <stdint.h>
50 #include <stdlib.h>
51 #include <stddef.h>
52 #include <stdio.h>
53 #include <errno.h>
54 #include <string.h>
55 #include <pthread.h>
56 #include <linux/types.h>
57 #include <stdbool.h>
58 #include <ctype.h>
59 #include <malloc.h>
60 #include <sys/types.h>
61 #include <sys/stat.h>
62 #include <fcntl.h>
63 #include <unistd.h>
64 #include <sys/mman.h>
65 #include <limits.h>
66 #include <assert.h>
67 #include <dirent.h>
68 #include <inttypes.h>
69 #include <error.h>
70 #include <rte_byteorder.h>
71 #include <rte_atomic.h>
72 #include <rte_spinlock.h>
73 #include <rte_common.h>
74 #include <rte_debug.h>
75
76 /* The following definitions are primarily to allow the single-source driver
77  * interfaces to be included by arbitrary program code. Ie. for interfaces that
78  * are also available in kernel-space, these definitions provide compatibility
79  * with certain attributes and types used in those interfaces.
80  */
81
82 /* Required compiler attributes */
83 #define __maybe_unused  __rte_unused
84 #define __always_unused __rte_unused
85 #define __packed        __rte_packed
86 #define noinline        __attribute__((noinline))
87
88 #define L1_CACHE_BYTES 64
89 #define ____cacheline_aligned __attribute__((aligned(L1_CACHE_BYTES)))
90 #define __stringify_1(x) #x
91 #define __stringify(x)  __stringify_1(x)
92
93 #ifdef ARRAY_SIZE
94 #undef ARRAY_SIZE
95 #endif
96 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
97
98 /* Debugging */
99 #define prflush(fmt, args...) \
100         do { \
101                 printf(fmt, ##args); \
102                 fflush(stdout); \
103         } while (0)
104
105 #define pr_crit(fmt, args...)    prflush("CRIT:" fmt, ##args)
106 #define pr_err(fmt, args...)     prflush("ERR:" fmt, ##args)
107 #define pr_warn(fmt, args...)    prflush("WARN:" fmt, ##args)
108 #define pr_info(fmt, args...)    prflush(fmt, ##args)
109
110 #ifdef RTE_LIBRTE_DPAA_DEBUG_BUS
111 #ifdef pr_debug
112 #undef pr_debug
113 #endif
114 #define pr_debug(fmt, args...)  printf(fmt, ##args)
115 #else
116 #define pr_debug(fmt, args...) {}
117 #endif
118
119 #define DPAA_BUG_ON(x) RTE_ASSERT(x)
120
121 /* Required types */
122 typedef uint8_t         u8;
123 typedef uint16_t        u16;
124 typedef uint32_t        u32;
125 typedef uint64_t        u64;
126 typedef uint64_t        dma_addr_t;
127 typedef cpu_set_t       cpumask_t;
128 typedef uint32_t        phandle;
129 typedef uint32_t        gfp_t;
130 typedef uint32_t        irqreturn_t;
131
132 #define IRQ_HANDLED     0
133 #define request_irq     qbman_request_irq
134 #define free_irq        qbman_free_irq
135
136 #define __iomem
137 #define GFP_KERNEL      0
138 #define __raw_readb(p)  (*(const volatile unsigned char *)(p))
139 #define __raw_readl(p)  (*(const volatile unsigned int *)(p))
140 #define __raw_writel(v, p) {*(volatile unsigned int *)(p) = (v); }
141
142 /* to be used as an upper-limit only */
143 #define NR_CPUS                 64
144
145 /* Waitqueue stuff */
146 typedef struct { }              wait_queue_head_t;
147 #define DECLARE_WAIT_QUEUE_HEAD(x) int dummy_##x __always_unused
148 #define wake_up(x)              do { } while (0)
149
150 /* I/O operations */
151 static inline u32 in_be32(volatile void *__p)
152 {
153         volatile u32 *p = __p;
154         return rte_be_to_cpu_32(*p);
155 }
156
157 static inline void out_be32(volatile void *__p, u32 val)
158 {
159         volatile u32 *p = __p;
160         *p = rte_cpu_to_be_32(val);
161 }
162
163 #define dcbt_ro(p) __builtin_prefetch(p, 0)
164 #define dcbt_rw(p) __builtin_prefetch(p, 1)
165
166 #define dcbz(p) { asm volatile("dc zva, %0" : : "r" (p) : "memory"); }
167 #define dcbz_64(p) dcbz(p)
168 #define hwsync() rte_rmb()
169 #define lwsync() rte_wmb()
170 #define dcbf(p) { asm volatile("dc cvac, %0" : : "r"(p) : "memory"); }
171 #define dcbf_64(p) dcbf(p)
172 #define dccivac(p) { asm volatile("dc civac, %0" : : "r"(p) : "memory"); }
173
174 #define dcbit_ro(p) \
175         do { \
176                 dccivac(p);                                             \
177                 asm volatile("prfm pldl1keep, [%0, #64]" : : "r" (p));  \
178         } while (0)
179
180 #define barrier() { asm volatile ("" : : : "memory"); }
181 #define cpu_relax barrier
182
183 static inline uint64_t mfatb(void)
184 {
185         uint64_t ret, ret_new, timeout = 200;
186
187         asm volatile ("mrs %0, cntvct_el0" : "=r" (ret));
188         asm volatile ("mrs %0, cntvct_el0" : "=r" (ret_new));
189         while (ret != ret_new && timeout--) {
190                 ret = ret_new;
191                 asm volatile ("mrs %0, cntvct_el0" : "=r" (ret_new));
192         }
193         DPAA_BUG_ON(!timeout && (ret != ret_new));
194         return ret * 64;
195 }
196
197 /* Spin for a few cycles without bothering the bus */
198 static inline void cpu_spin(int cycles)
199 {
200         uint64_t now = mfatb();
201
202         while (mfatb() < (now + cycles))
203                 ;
204 }
205
206 /* Qman/Bman API inlines and macros; */
207 #ifdef lower_32_bits
208 #undef lower_32_bits
209 #endif
210 #define lower_32_bits(x) ((u32)(x))
211
212 #ifdef upper_32_bits
213 #undef upper_32_bits
214 #endif
215 #define upper_32_bits(x) ((u32)(((x) >> 16) >> 16))
216
217 /*
218  * Swap bytes of a 48-bit value.
219  */
220 static inline uint64_t
221 __bswap_48(uint64_t x)
222 {
223         return  ((x & 0x0000000000ffULL) << 40) |
224                 ((x & 0x00000000ff00ULL) << 24) |
225                 ((x & 0x000000ff0000ULL) <<  8) |
226                 ((x & 0x0000ff000000ULL) >>  8) |
227                 ((x & 0x00ff00000000ULL) >> 24) |
228                 ((x & 0xff0000000000ULL) >> 40);
229 }
230
231 /*
232  * Swap bytes of a 40-bit value.
233  */
234 static inline uint64_t
235 __bswap_40(uint64_t x)
236 {
237         return  ((x & 0x00000000ffULL) << 32) |
238                 ((x & 0x000000ff00ULL) << 16) |
239                 ((x & 0x0000ff0000ULL)) |
240                 ((x & 0x00ff000000ULL) >> 16) |
241                 ((x & 0xff00000000ULL) >> 32);
242 }
243
244 /*
245  * Swap bytes of a 24-bit value.
246  */
247 static inline uint32_t
248 __bswap_24(uint32_t x)
249 {
250         return  ((x & 0x0000ffULL) << 16) |
251                 ((x & 0x00ff00ULL)) |
252                 ((x & 0xff0000ULL) >> 16);
253 }
254
255 #define be64_to_cpu(x) rte_be_to_cpu_64(x)
256 #define be32_to_cpu(x) rte_be_to_cpu_32(x)
257 #define be16_to_cpu(x) rte_be_to_cpu_16(x)
258
259 #define cpu_to_be64(x) rte_cpu_to_be_64(x)
260 #define cpu_to_be32(x) rte_cpu_to_be_32(x)
261 #define cpu_to_be16(x) rte_cpu_to_be_16(x)
262
263 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
264
265 #define cpu_to_be48(x) __bswap_48(x)
266 #define be48_to_cpu(x) __bswap_48(x)
267
268 #define cpu_to_be40(x) __bswap_40(x)
269 #define be40_to_cpu(x) __bswap_40(x)
270
271 #define cpu_to_be24(x) __bswap_24(x)
272 #define be24_to_cpu(x) __bswap_24(x)
273
274 #else /* RTE_BIG_ENDIAN */
275
276 #define cpu_to_be48(x) (x)
277 #define be48_to_cpu(x) (x)
278
279 #define cpu_to_be40(x) (x)
280 #define be40_to_cpu(x) (x)
281
282 #define cpu_to_be24(x) (x)
283 #define be24_to_cpu(x) (x)
284
285 #endif /* RTE_BIG_ENDIAN */
286
287 /* When copying aligned words or shorts, try to avoid memcpy() */
288 /* memcpy() stuff - when you know alignments in advance */
289 #define CONFIG_TRY_BETTER_MEMCPY
290
291 #ifdef CONFIG_TRY_BETTER_MEMCPY
292 static inline void copy_words(void *dest, const void *src, size_t sz)
293 {
294         u32 *__dest = dest;
295         const u32 *__src = src;
296         size_t __sz = sz >> 2;
297
298         DPAA_BUG_ON((unsigned long)dest & 0x3);
299         DPAA_BUG_ON((unsigned long)src & 0x3);
300         DPAA_BUG_ON(sz & 0x3);
301         while (__sz--)
302                 *(__dest++) = *(__src++);
303 }
304
305 static inline void copy_shorts(void *dest, const void *src, size_t sz)
306 {
307         u16 *__dest = dest;
308         const u16 *__src = src;
309         size_t __sz = sz >> 1;
310
311         DPAA_BUG_ON((unsigned long)dest & 0x1);
312         DPAA_BUG_ON((unsigned long)src & 0x1);
313         DPAA_BUG_ON(sz & 0x1);
314         while (__sz--)
315                 *(__dest++) = *(__src++);
316 }
317
318 static inline void copy_bytes(void *dest, const void *src, size_t sz)
319 {
320         u8 *__dest = dest;
321         const u8 *__src = src;
322
323         while (sz--)
324                 *(__dest++) = *(__src++);
325 }
326 #else
327 #define copy_words memcpy
328 #define copy_shorts memcpy
329 #define copy_bytes memcpy
330 #endif
331
332 /* Allocator stuff */
333 #define kmalloc(sz, t)  malloc(sz)
334 #define vmalloc(sz)     malloc(sz)
335 #define kfree(p)        { if (p) free(p); }
336 static inline void *kzalloc(size_t sz, gfp_t __foo __rte_unused)
337 {
338         void *ptr = malloc(sz);
339
340         if (ptr)
341                 memset(ptr, 0, sz);
342         return ptr;
343 }
344
345 static inline unsigned long get_zeroed_page(gfp_t __foo __rte_unused)
346 {
347         void *p;
348
349         if (posix_memalign(&p, 4096, 4096))
350                 return 0;
351         memset(p, 0, 4096);
352         return (unsigned long)p;
353 }
354
355 /* Spinlock stuff */
356 #define spinlock_t              rte_spinlock_t
357 #define __SPIN_LOCK_UNLOCKED(x) RTE_SPINLOCK_INITIALIZER
358 #define DEFINE_SPINLOCK(x)      spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
359 #define spin_lock_init(x)       rte_spinlock_init(x)
360 #define spin_lock_destroy(x)
361 #define spin_lock(x)            rte_spinlock_lock(x)
362 #define spin_unlock(x)          rte_spinlock_unlock(x)
363 #define spin_lock_irq(x)        spin_lock(x)
364 #define spin_unlock_irq(x)      spin_unlock(x)
365 #define spin_lock_irqsave(x, f) spin_lock_irq(x)
366 #define spin_unlock_irqrestore(x, f) spin_unlock_irq(x)
367
368 #define atomic_t                rte_atomic32_t
369 #define atomic_read(v)          rte_atomic32_read(v)
370 #define atomic_set(v, i)        rte_atomic32_set(v, i)
371
372 #define atomic_inc(v)           rte_atomic32_add(v, 1)
373 #define atomic_dec(v)           rte_atomic32_sub(v, 1)
374
375 #define atomic_inc_and_test(v)  rte_atomic32_inc_and_test(v)
376 #define atomic_dec_and_test(v)  rte_atomic32_dec_and_test(v)
377
378 #define atomic_inc_return(v)    rte_atomic32_add_return(v, 1)
379 #define atomic_dec_return(v)    rte_atomic32_sub_return(v, 1)
380 #define atomic_sub_and_test(i, v) (rte_atomic32_sub_return(v, i) == 0)
381
382 #include <dpaa_list.h>
383 #include <dpaa_bits.h>
384
385 #endif /* __COMPAT_H */