1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
3 * Copyright 2011 Freescale Semiconductor, Inc.
5 * Copyright 2019-2020 NXP
20 #include <linux/types.h>
24 #include <sys/types.h>
33 #include <rte_byteorder.h>
34 #include <rte_atomic.h>
35 #include <rte_spinlock.h>
36 #include <rte_common.h>
37 #include <rte_debug.h>
38 #include <rte_cycles.h>
39 #include <rte_malloc.h>
41 /* The following definitions are primarily to allow the single-source driver
42 * interfaces to be included by arbitrary program code. Ie. for interfaces that
43 * are also available in kernel-space, these definitions provide compatibility
44 * with certain attributes and types used in those interfaces.
47 /* Required compiler attributes */
48 #ifndef __maybe_unused
49 #define __maybe_unused __rte_unused
51 #ifndef __always_unused
52 #define __always_unused __rte_unused
55 #define __packed __rte_packed
58 #define noinline __rte_noinline
60 #define L1_CACHE_BYTES 64
61 #define ____cacheline_aligned __rte_aligned(L1_CACHE_BYTES)
62 #define __stringify_1(x) #x
63 #define __stringify(x) __stringify_1(x)
68 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
71 #define prflush(fmt, args...) \
73 printf(fmt, ##args); \
77 #define pr_crit(fmt, args...) prflush("CRIT:" fmt, ##args)
80 #define pr_err(fmt, args...) prflush("ERR:" fmt, ##args)
83 #define pr_warn(fmt, args...) prflush("WARN:" fmt, ##args)
86 #define pr_info(fmt, args...) prflush(fmt, ##args)
89 #ifdef RTE_LIBRTE_DPAA_DEBUG_BUS
90 #define pr_debug(fmt, args...) printf(fmt, ##args)
92 #define pr_debug(fmt, args...) {}
96 #define DPAA_BUG_ON(x) RTE_ASSERT(x)
100 typedef uint16_t u16;
101 typedef uint32_t u32;
102 typedef uint64_t u64;
103 typedef uint64_t dma_addr_t;
104 typedef cpu_set_t cpumask_t;
105 typedef uint32_t phandle;
106 typedef uint32_t gfp_t;
107 typedef uint32_t irqreturn_t;
109 #define ETHER_ADDR_LEN 6
111 #define IRQ_HANDLED 0
112 #define request_irq qbman_request_irq
113 #define free_irq qbman_free_irq
117 #define __raw_readb(p) (*(const volatile unsigned char *)(p))
118 #define __raw_readl(p) (*(const volatile unsigned int *)(p))
119 #define __raw_writel(v, p) {*(volatile unsigned int *)(p) = (v); }
121 /* to be used as an upper-limit only */
124 /* Waitqueue stuff */
125 typedef struct { } wait_queue_head_t;
126 #define DECLARE_WAIT_QUEUE_HEAD(x) int dummy_##x __always_unused
127 #define wake_up(x) do { } while (0)
130 static inline u32 in_be32(volatile void *__p)
132 volatile u32 *p = __p;
133 return rte_be_to_cpu_32(*p);
136 static inline void out_be32(volatile void *__p, u32 val)
138 volatile u32 *p = __p;
139 *p = rte_cpu_to_be_32(val);
142 #define hwsync() rte_rmb()
143 #define lwsync() rte_wmb()
145 #define dcbt_ro(p) __builtin_prefetch(p, 0)
146 #define dcbt_rw(p) __builtin_prefetch(p, 1)
148 #if defined(RTE_ARCH_ARM)
149 #if defined(RTE_ARCH_64)
150 #define dcbz(p) { asm volatile("dc zva, %0" : : "r" (p) : "memory"); }
151 #define dcbz_64(p) dcbz(p)
152 #define dcbf(p) { asm volatile("dc cvac, %0" : : "r"(p) : "memory"); }
153 #define dcbf_64(p) dcbf(p)
154 #define dccivac(p) { asm volatile("dc civac, %0" : : "r"(p) : "memory"); }
156 #define dcbit_ro(p) \
159 asm volatile("prfm pldl1keep, [%0, #64]" : : "r" (p)); \
162 #else /* RTE_ARCH_32 */
163 #define dcbz(p) memset((p), 0, 32)
164 #define dcbz_64(p) memset((p), 0, 64)
165 #define dcbf(p) RTE_SET_USED(p)
166 #define dcbf_64(p) dcbf(p)
167 #define dccivac(p) RTE_SET_USED(p)
168 #define dcbit_ro(p) RTE_SET_USED(p)
172 #define dcbz(p) RTE_SET_USED(p)
173 #define dcbz_64(p) dcbz(p)
174 #define dcbf(p) RTE_SET_USED(p)
175 #define dcbf_64(p) dcbf(p)
176 #define dccivac(p) RTE_SET_USED(p)
177 #define dcbit_ro(p) RTE_SET_USED(p)
180 #define barrier() { asm volatile ("" : : : "memory"); }
181 #define cpu_relax barrier
183 #if defined(RTE_ARCH_ARM64)
184 static inline uint64_t mfatb(void)
186 uint64_t ret, ret_new, timeout = 200;
188 asm volatile ("mrs %0, cntvct_el0" : "=r" (ret));
189 asm volatile ("mrs %0, cntvct_el0" : "=r" (ret_new));
190 while (ret != ret_new && timeout--) {
192 asm volatile ("mrs %0, cntvct_el0" : "=r" (ret_new));
194 DPAA_BUG_ON(!timeout && (ret != ret_new));
199 #define mfatb rte_rdtsc
203 /* Spin for a few cycles without bothering the bus */
204 static inline void cpu_spin(int cycles)
206 uint64_t now = mfatb();
208 while (mfatb() < (now + cycles))
212 /* Qman/Bman API inlines and macros; */
216 #define lower_32_bits(x) ((u32)(x))
221 #define upper_32_bits(x) ((u32)(((x) >> 16) >> 16))
224 * Swap bytes of a 48-bit value.
226 static inline uint64_t
227 __bswap_48(uint64_t x)
229 return ((x & 0x0000000000ffULL) << 40) |
230 ((x & 0x00000000ff00ULL) << 24) |
231 ((x & 0x000000ff0000ULL) << 8) |
232 ((x & 0x0000ff000000ULL) >> 8) |
233 ((x & 0x00ff00000000ULL) >> 24) |
234 ((x & 0xff0000000000ULL) >> 40);
238 * Swap bytes of a 40-bit value.
240 static inline uint64_t
241 __bswap_40(uint64_t x)
243 return ((x & 0x00000000ffULL) << 32) |
244 ((x & 0x000000ff00ULL) << 16) |
245 ((x & 0x0000ff0000ULL)) |
246 ((x & 0x00ff000000ULL) >> 16) |
247 ((x & 0xff00000000ULL) >> 32);
251 * Swap bytes of a 24-bit value.
253 static inline uint32_t
254 __bswap_24(uint32_t x)
256 return ((x & 0x0000ffULL) << 16) |
257 ((x & 0x00ff00ULL)) |
258 ((x & 0xff0000ULL) >> 16);
261 #define be64_to_cpu(x) rte_be_to_cpu_64(x)
262 #define be32_to_cpu(x) rte_be_to_cpu_32(x)
263 #define be16_to_cpu(x) rte_be_to_cpu_16(x)
265 #define cpu_to_be64(x) rte_cpu_to_be_64(x)
266 #if !defined(cpu_to_be32)
267 #define cpu_to_be32(x) rte_cpu_to_be_32(x)
269 #define cpu_to_be16(x) rte_cpu_to_be_16(x)
271 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
273 #define cpu_to_be48(x) __bswap_48(x)
274 #define be48_to_cpu(x) __bswap_48(x)
276 #define cpu_to_be40(x) __bswap_40(x)
277 #define be40_to_cpu(x) __bswap_40(x)
279 #define cpu_to_be24(x) __bswap_24(x)
280 #define be24_to_cpu(x) __bswap_24(x)
282 #else /* RTE_BIG_ENDIAN */
284 #define cpu_to_be48(x) (x)
285 #define be48_to_cpu(x) (x)
287 #define cpu_to_be40(x) (x)
288 #define be40_to_cpu(x) (x)
290 #define cpu_to_be24(x) (x)
291 #define be24_to_cpu(x) (x)
293 #endif /* RTE_BIG_ENDIAN */
295 /* When copying aligned words or shorts, try to avoid memcpy() */
296 /* memcpy() stuff - when you know alignments in advance */
297 #define CONFIG_TRY_BETTER_MEMCPY
299 #ifdef CONFIG_TRY_BETTER_MEMCPY
300 static inline void copy_words(void *dest, const void *src, size_t sz)
303 const u32 *__src = src;
304 size_t __sz = sz >> 2;
306 DPAA_BUG_ON((unsigned long)dest & 0x3);
307 DPAA_BUG_ON((unsigned long)src & 0x3);
308 DPAA_BUG_ON(sz & 0x3);
310 *(__dest++) = *(__src++);
313 static inline void copy_shorts(void *dest, const void *src, size_t sz)
316 const u16 *__src = src;
317 size_t __sz = sz >> 1;
319 DPAA_BUG_ON((unsigned long)dest & 0x1);
320 DPAA_BUG_ON((unsigned long)src & 0x1);
321 DPAA_BUG_ON(sz & 0x1);
323 *(__dest++) = *(__src++);
326 static inline void copy_bytes(void *dest, const void *src, size_t sz)
329 const u8 *__src = src;
332 *(__dest++) = *(__src++);
335 #define copy_words memcpy
336 #define copy_shorts memcpy
337 #define copy_bytes memcpy
340 /* Allocator stuff */
341 #define kmalloc(sz, t) rte_malloc(NULL, sz, 0)
342 #define vmalloc(sz) rte_malloc(NULL, sz, 0)
343 #define kfree(p) { if (p) rte_free(p); }
344 static inline void *kzalloc(size_t sz, gfp_t __foo __rte_unused)
346 void *ptr = rte_malloc(NULL, sz, 0);
353 static inline unsigned long get_zeroed_page(gfp_t __foo __rte_unused)
357 if (posix_memalign(&p, 4096, 4096))
360 return (unsigned long)p;
364 #define spinlock_t rte_spinlock_t
365 #define __SPIN_LOCK_UNLOCKED(x) RTE_SPINLOCK_INITIALIZER
366 #define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
367 #define spin_lock_init(x) rte_spinlock_init(x)
368 #define spin_lock_destroy(x)
369 #define spin_lock(x) rte_spinlock_lock(x)
370 #define spin_unlock(x) rte_spinlock_unlock(x)
371 #define spin_lock_irq(x) spin_lock(x)
372 #define spin_unlock_irq(x) spin_unlock(x)
373 #define spin_lock_irqsave(x, f) spin_lock_irq(x)
374 #define spin_unlock_irqrestore(x, f) spin_unlock_irq(x)
376 #define atomic_t rte_atomic32_t
377 #define atomic_read(v) rte_atomic32_read(v)
378 #define atomic_set(v, i) rte_atomic32_set(v, i)
380 #define atomic_inc(v) rte_atomic32_add(v, 1)
381 #define atomic_dec(v) rte_atomic32_sub(v, 1)
383 #define atomic_inc_and_test(v) rte_atomic32_inc_and_test(v)
384 #define atomic_dec_and_test(v) rte_atomic32_dec_and_test(v)
386 #define atomic_inc_return(v) rte_atomic32_add_return(v, 1)
387 #define atomic_dec_return(v) rte_atomic32_sub_return(v, 1)
388 #define atomic_sub_and_test(i, v) (rte_atomic32_sub_return(v, i) == 0)
390 /* Interface name len*/
391 #define IF_NAME_MAX_LEN 16
393 #endif /* __COMPAT_H */