1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
3 * Copyright 2011 Freescale Semiconductor, Inc.
23 #include <linux/types.h>
27 #include <sys/types.h>
37 #include <rte_byteorder.h>
38 #include <rte_atomic.h>
39 #include <rte_spinlock.h>
40 #include <rte_common.h>
41 #include <rte_debug.h>
42 #include <rte_cycles.h>
44 /* The following definitions are primarily to allow the single-source driver
45 * interfaces to be included by arbitrary program code. Ie. for interfaces that
46 * are also available in kernel-space, these definitions provide compatibility
47 * with certain attributes and types used in those interfaces.
50 /* Required compiler attributes */
51 #ifndef __maybe_unused
52 #define __maybe_unused __rte_unused
54 #ifndef __always_unused
55 #define __always_unused __rte_unused
58 #define __packed __rte_packed
61 #define noinline __attribute__((noinline))
63 #define L1_CACHE_BYTES 64
64 #define ____cacheline_aligned __attribute__((aligned(L1_CACHE_BYTES)))
65 #define __stringify_1(x) #x
66 #define __stringify(x) __stringify_1(x)
71 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
74 #define prflush(fmt, args...) \
76 printf(fmt, ##args); \
80 #define pr_crit(fmt, args...) prflush("CRIT:" fmt, ##args)
83 #define pr_err(fmt, args...) prflush("ERR:" fmt, ##args)
86 #define pr_warn(fmt, args...) prflush("WARN:" fmt, ##args)
89 #define pr_info(fmt, args...) prflush(fmt, ##args)
92 #ifdef RTE_LIBRTE_DPAA_DEBUG_BUS
93 #define pr_debug(fmt, args...) printf(fmt, ##args)
95 #define pr_debug(fmt, args...) {}
99 #define DPAA_BUG_ON(x) RTE_ASSERT(x)
103 typedef uint16_t u16;
104 typedef uint32_t u32;
105 typedef uint64_t u64;
106 typedef uint64_t dma_addr_t;
107 typedef cpu_set_t cpumask_t;
108 typedef uint32_t phandle;
109 typedef uint32_t gfp_t;
110 typedef uint32_t irqreturn_t;
112 #define IRQ_HANDLED 0
113 #define request_irq qbman_request_irq
114 #define free_irq qbman_free_irq
118 #define __raw_readb(p) (*(const volatile unsigned char *)(p))
119 #define __raw_readl(p) (*(const volatile unsigned int *)(p))
120 #define __raw_writel(v, p) {*(volatile unsigned int *)(p) = (v); }
122 /* to be used as an upper-limit only */
125 /* Waitqueue stuff */
126 typedef struct { } wait_queue_head_t;
127 #define DECLARE_WAIT_QUEUE_HEAD(x) int dummy_##x __always_unused
128 #define wake_up(x) do { } while (0)
131 static inline u32 in_be32(volatile void *__p)
133 volatile u32 *p = __p;
134 return rte_be_to_cpu_32(*p);
137 static inline void out_be32(volatile void *__p, u32 val)
139 volatile u32 *p = __p;
140 *p = rte_cpu_to_be_32(val);
143 #define hwsync() rte_rmb()
144 #define lwsync() rte_wmb()
146 #define dcbt_ro(p) __builtin_prefetch(p, 0)
147 #define dcbt_rw(p) __builtin_prefetch(p, 1)
149 #if defined(RTE_ARCH_ARM64)
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 #elif defined(RTE_ARCH_ARM)
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)
171 #define dcbz(p) RTE_SET_USED(p)
172 #define dcbz_64(p) dcbz(p)
173 #define dcbf(p) RTE_SET_USED(p)
174 #define dcbf_64(p) dcbf(p)
175 #define dccivac(p) RTE_SET_USED(p)
176 #define dcbit_ro(p) RTE_SET_USED(p)
179 #define barrier() { asm volatile ("" : : : "memory"); }
180 #define cpu_relax barrier
182 #if defined(RTE_ARCH_ARM64)
183 static inline uint64_t mfatb(void)
185 uint64_t ret, ret_new, timeout = 200;
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--) {
191 asm volatile ("mrs %0, cntvct_el0" : "=r" (ret_new));
193 DPAA_BUG_ON(!timeout && (ret != ret_new));
198 #define mfatb rte_rdtsc
202 /* Spin for a few cycles without bothering the bus */
203 static inline void cpu_spin(int cycles)
205 uint64_t now = mfatb();
207 while (mfatb() < (now + cycles))
211 /* Qman/Bman API inlines and macros; */
215 #define lower_32_bits(x) ((u32)(x))
220 #define upper_32_bits(x) ((u32)(((x) >> 16) >> 16))
223 * Swap bytes of a 48-bit value.
225 static inline uint64_t
226 __bswap_48(uint64_t x)
228 return ((x & 0x0000000000ffULL) << 40) |
229 ((x & 0x00000000ff00ULL) << 24) |
230 ((x & 0x000000ff0000ULL) << 8) |
231 ((x & 0x0000ff000000ULL) >> 8) |
232 ((x & 0x00ff00000000ULL) >> 24) |
233 ((x & 0xff0000000000ULL) >> 40);
237 * Swap bytes of a 40-bit value.
239 static inline uint64_t
240 __bswap_40(uint64_t x)
242 return ((x & 0x00000000ffULL) << 32) |
243 ((x & 0x000000ff00ULL) << 16) |
244 ((x & 0x0000ff0000ULL)) |
245 ((x & 0x00ff000000ULL) >> 16) |
246 ((x & 0xff00000000ULL) >> 32);
250 * Swap bytes of a 24-bit value.
252 static inline uint32_t
253 __bswap_24(uint32_t x)
255 return ((x & 0x0000ffULL) << 16) |
256 ((x & 0x00ff00ULL)) |
257 ((x & 0xff0000ULL) >> 16);
260 #define be64_to_cpu(x) rte_be_to_cpu_64(x)
261 #define be32_to_cpu(x) rte_be_to_cpu_32(x)
262 #define be16_to_cpu(x) rte_be_to_cpu_16(x)
264 #define cpu_to_be64(x) rte_cpu_to_be_64(x)
265 #if !defined(cpu_to_be32)
266 #define cpu_to_be32(x) rte_cpu_to_be_32(x)
268 #define cpu_to_be16(x) rte_cpu_to_be_16(x)
270 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
272 #define cpu_to_be48(x) __bswap_48(x)
273 #define be48_to_cpu(x) __bswap_48(x)
275 #define cpu_to_be40(x) __bswap_40(x)
276 #define be40_to_cpu(x) __bswap_40(x)
278 #define cpu_to_be24(x) __bswap_24(x)
279 #define be24_to_cpu(x) __bswap_24(x)
281 #else /* RTE_BIG_ENDIAN */
283 #define cpu_to_be48(x) (x)
284 #define be48_to_cpu(x) (x)
286 #define cpu_to_be40(x) (x)
287 #define be40_to_cpu(x) (x)
289 #define cpu_to_be24(x) (x)
290 #define be24_to_cpu(x) (x)
292 #endif /* RTE_BIG_ENDIAN */
294 /* When copying aligned words or shorts, try to avoid memcpy() */
295 /* memcpy() stuff - when you know alignments in advance */
296 #define CONFIG_TRY_BETTER_MEMCPY
298 #ifdef CONFIG_TRY_BETTER_MEMCPY
299 static inline void copy_words(void *dest, const void *src, size_t sz)
302 const u32 *__src = src;
303 size_t __sz = sz >> 2;
305 DPAA_BUG_ON((unsigned long)dest & 0x3);
306 DPAA_BUG_ON((unsigned long)src & 0x3);
307 DPAA_BUG_ON(sz & 0x3);
309 *(__dest++) = *(__src++);
312 static inline void copy_shorts(void *dest, const void *src, size_t sz)
315 const u16 *__src = src;
316 size_t __sz = sz >> 1;
318 DPAA_BUG_ON((unsigned long)dest & 0x1);
319 DPAA_BUG_ON((unsigned long)src & 0x1);
320 DPAA_BUG_ON(sz & 0x1);
322 *(__dest++) = *(__src++);
325 static inline void copy_bytes(void *dest, const void *src, size_t sz)
328 const u8 *__src = src;
331 *(__dest++) = *(__src++);
334 #define copy_words memcpy
335 #define copy_shorts memcpy
336 #define copy_bytes memcpy
339 /* Allocator stuff */
340 #define kmalloc(sz, t) malloc(sz)
341 #define vmalloc(sz) malloc(sz)
342 #define kfree(p) { if (p) free(p); }
343 static inline void *kzalloc(size_t sz, gfp_t __foo __rte_unused)
345 void *ptr = malloc(sz);
352 static inline unsigned long get_zeroed_page(gfp_t __foo __rte_unused)
356 if (posix_memalign(&p, 4096, 4096))
359 return (unsigned long)p;
363 #define spinlock_t rte_spinlock_t
364 #define __SPIN_LOCK_UNLOCKED(x) RTE_SPINLOCK_INITIALIZER
365 #define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
366 #define spin_lock_init(x) rte_spinlock_init(x)
367 #define spin_lock_destroy(x)
368 #define spin_lock(x) rte_spinlock_lock(x)
369 #define spin_unlock(x) rte_spinlock_unlock(x)
370 #define spin_lock_irq(x) spin_lock(x)
371 #define spin_unlock_irq(x) spin_unlock(x)
372 #define spin_lock_irqsave(x, f) spin_lock_irq(x)
373 #define spin_unlock_irqrestore(x, f) spin_unlock_irq(x)
375 #define atomic_t rte_atomic32_t
376 #define atomic_read(v) rte_atomic32_read(v)
377 #define atomic_set(v, i) rte_atomic32_set(v, i)
379 #define atomic_inc(v) rte_atomic32_add(v, 1)
380 #define atomic_dec(v) rte_atomic32_sub(v, 1)
382 #define atomic_inc_and_test(v) rte_atomic32_inc_and_test(v)
383 #define atomic_dec_and_test(v) rte_atomic32_dec_and_test(v)
385 #define atomic_inc_return(v) rte_atomic32_add_return(v, 1)
386 #define atomic_dec_return(v) rte_atomic32_sub_return(v, 1)
387 #define atomic_sub_and_test(i, v) (rte_atomic32_sub_return(v, i) == 0)
389 #include <dpaa_list.h>
390 #include <dpaa_bits.h>
392 #endif /* __COMPAT_H */