1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2015-2020 Amazon.com, Inc. or its affiliates.
6 #ifndef DPDK_ENA_COM_ENA_PLAT_DPDK_H_
7 #define DPDK_ENA_COM_ENA_PLAT_DPDK_H_
17 #include <rte_atomic.h>
18 #include <rte_branch_prediction.h>
19 #include <rte_cycles.h>
22 #include <rte_malloc.h>
23 #include <rte_memzone.h>
24 #include <rte_prefetch.h>
25 #include <rte_spinlock.h>
34 typedef uint64_t dma_addr_t;
36 #define ETIME ETIMEDOUT
39 #define ena_atomic32_t rte_atomic32_t
40 #define ena_mem_handle_t const struct rte_memzone *
46 #define ENA_COM_NO_MEM -ENOMEM
47 #define ENA_COM_INVAL -EINVAL
48 #define ENA_COM_NO_SPACE -ENOSPC
49 #define ENA_COM_NO_DEVICE -ENODEV
50 #define ENA_COM_TIMER_EXPIRED -ETIME
51 #define ENA_COM_FAULT -EFAULT
52 #define ENA_COM_TRY_AGAIN -EAGAIN
53 #define ENA_COM_UNSUPPORTED -EOPNOTSUPP
55 #define ____cacheline_aligned __rte_cache_aligned
57 #define ENA_ABORT() abort()
59 #define ENA_MSLEEP(x) rte_delay_ms(x)
60 #define ENA_UDELAY(x) rte_delay_us(x)
62 #define ENA_TOUCH(x) ((void)(x))
63 #define memcpy_toio memcpy
67 #define mmiowb rte_io_wmb
70 #define US_PER_S 1000000
71 #define ENA_GET_SYSTEM_USECS() \
72 (rte_get_timer_cycles() * US_PER_S / rte_get_timer_hz())
74 extern int ena_logtype_com;
75 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
76 #define ENA_ASSERT(cond, format, arg...) \
78 if (unlikely(!(cond))) { \
79 rte_log(RTE_LOGTYPE_ERR, ena_logtype_com, \
81 rte_panic("line %d\tassert \"" #cond "\"" \
82 "failed\n", __LINE__); \
86 #define ENA_ASSERT(cond, format, arg...) do {} while (0)
89 #define ENA_MAX32(x, y) RTE_MAX((x), (y))
90 #define ENA_MAX16(x, y) RTE_MAX((x), (y))
91 #define ENA_MAX8(x, y) RTE_MAX((x), (y))
92 #define ENA_MIN32(x, y) RTE_MIN((x), (y))
93 #define ENA_MIN16(x, y) RTE_MIN((x), (y))
94 #define ENA_MIN8(x, y) RTE_MIN((x), (y))
96 #define BITS_PER_LONG_LONG (__SIZEOF_LONG_LONG__ * 8)
97 #define U64_C(x) x ## ULL
98 #define BIT(nr) (1UL << (nr))
99 #define BITS_PER_LONG (__SIZEOF_LONG__ * 8)
100 #define GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
101 #define GENMASK_ULL(h, l) (((~0ULL) - (1ULL << (l)) + 1) & \
102 (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
104 #ifdef RTE_LIBRTE_ENA_COM_DEBUG
105 #define ena_trc_log(level, fmt, arg...) \
106 rte_log(RTE_LOG_ ## level, ena_logtype_com, \
107 "[ENA_COM: %s]" fmt, __func__, ##arg)
109 #define ena_trc_dbg(format, arg...) ena_trc_log(DEBUG, format, ##arg)
110 #define ena_trc_info(format, arg...) ena_trc_log(INFO, format, ##arg)
111 #define ena_trc_warn(format, arg...) ena_trc_log(WARNING, format, ##arg)
112 #define ena_trc_err(format, arg...) ena_trc_log(ERR, format, ##arg)
114 #define ena_trc_dbg(format, arg...) do { } while (0)
115 #define ena_trc_info(format, arg...) do { } while (0)
116 #define ena_trc_warn(format, arg...) do { } while (0)
117 #define ena_trc_err(format, arg...) do { } while (0)
118 #endif /* RTE_LIBRTE_ENA_COM_DEBUG */
120 #define ENA_WARN(cond, format, arg...) \
122 if (unlikely(cond)) { \
124 "Warn failed on %s:%s:%d:" format, \
125 __FILE__, __func__, __LINE__, ##arg); \
129 /* Spinlock related methods */
130 #define ena_spinlock_t rte_spinlock_t
131 #define ENA_SPINLOCK_INIT(spinlock) rte_spinlock_init(&spinlock)
132 #define ENA_SPINLOCK_LOCK(spinlock, flags) \
133 ({(void)flags; rte_spinlock_lock(&spinlock); })
134 #define ENA_SPINLOCK_UNLOCK(spinlock, flags) \
135 ({(void)flags; rte_spinlock_unlock(&(spinlock)); })
136 #define ENA_SPINLOCK_DESTROY(spinlock) ((void)spinlock)
138 #define q_waitqueue_t \
140 pthread_cond_t cond; \
141 pthread_mutex_t mutex; \
144 #define ena_wait_queue_t q_waitqueue_t
146 #define ENA_WAIT_EVENT_INIT(waitqueue) \
148 pthread_mutex_init(&(waitqueue).mutex, NULL); \
149 pthread_cond_init(&(waitqueue).cond, NULL); \
152 #define ENA_WAIT_EVENT_WAIT(waitevent, timeout) \
154 struct timespec wait; \
155 struct timeval now; \
156 unsigned long timeout_us; \
157 gettimeofday(&now, NULL); \
158 wait.tv_sec = now.tv_sec + timeout / 1000000UL; \
159 timeout_us = timeout % 1000000UL; \
160 wait.tv_nsec = (now.tv_usec + timeout_us) * 1000UL; \
161 pthread_mutex_lock(&waitevent.mutex); \
162 pthread_cond_timedwait(&waitevent.cond, \
163 &waitevent.mutex, &wait); \
164 pthread_mutex_unlock(&waitevent.mutex); \
166 #define ENA_WAIT_EVENT_SIGNAL(waitevent) pthread_cond_signal(&waitevent.cond)
167 /* pthread condition doesn't need to be rearmed after usage */
168 #define ENA_WAIT_EVENT_CLEAR(...)
169 #define ENA_WAIT_EVENT_DESTROY(waitqueue) ((void)(waitqueue))
171 #define ena_wait_event_t ena_wait_queue_t
172 #define ENA_MIGHT_SLEEP()
174 #define ena_time_t uint64_t
175 #define ENA_TIME_EXPIRE(timeout) (timeout < rte_get_timer_cycles())
176 #define ENA_GET_SYSTEM_TIMEOUT(timeout_us) \
177 (timeout_us * rte_get_timer_hz() / 1000000 + rte_get_timer_cycles())
180 * Each rte_memzone should have unique name.
181 * To satisfy it, count number of allocations and add it to name.
183 extern rte_atomic32_t ena_alloc_cnt;
185 #define ENA_MEM_ALLOC_COHERENT(dmadev, size, virt, phys, handle) \
187 const struct rte_memzone *mz = NULL; \
188 ENA_TOUCH(dmadev); ENA_TOUCH(handle); \
190 char z_name[RTE_MEMZONE_NAMESIZE]; \
191 snprintf(z_name, sizeof(z_name), \
193 rte_atomic32_add_return(&ena_alloc_cnt, 1)); \
194 mz = rte_memzone_reserve(z_name, size, \
196 RTE_MEMZONE_IOVA_CONTIG); \
203 memset(mz->addr, 0, size); \
208 #define ENA_MEM_FREE_COHERENT(dmadev, size, virt, phys, handle) \
209 ({ ENA_TOUCH(size); ENA_TOUCH(phys); \
211 rte_memzone_free(handle); })
213 #define ENA_MEM_ALLOC_COHERENT_NODE( \
214 dmadev, size, virt, phys, mem_handle, node, dev_node) \
216 const struct rte_memzone *mz = NULL; \
217 ENA_TOUCH(dmadev); ENA_TOUCH(dev_node); \
219 char z_name[RTE_MEMZONE_NAMESIZE]; \
220 snprintf(z_name, sizeof(z_name), \
222 rte_atomic32_add_return(&ena_alloc_cnt, 1)); \
223 mz = rte_memzone_reserve(z_name, size, node, \
224 RTE_MEMZONE_IOVA_CONTIG); \
231 memset(mz->addr, 0, size); \
237 #define ENA_MEM_ALLOC_NODE(dmadev, size, virt, node, dev_node) \
239 ENA_TOUCH(dmadev); ENA_TOUCH(dev_node); \
240 virt = rte_zmalloc_socket(NULL, size, 0, node); \
243 #define ENA_MEM_ALLOC(dmadev, size) rte_zmalloc(NULL, size, 1)
244 #define ENA_MEM_FREE(dmadev, ptr, size) \
245 ({ ENA_TOUCH(dmadev); ENA_TOUCH(size); rte_free(ptr); })
247 #define ENA_DB_SYNC(mem_handle) ((void)mem_handle)
249 #define ENA_REG_WRITE32(bus, value, reg) \
250 ({ (void)(bus); rte_write32((value), (reg)); })
251 #define ENA_REG_WRITE32_RELAXED(bus, value, reg) \
252 ({ (void)(bus); rte_write32_relaxed((value), (reg)); })
253 #define ENA_REG_READ32(bus, reg) \
254 ({ (void)(bus); rte_read32_relaxed((reg)); })
256 #define ATOMIC32_INC(i32_ptr) rte_atomic32_inc(i32_ptr)
257 #define ATOMIC32_DEC(i32_ptr) rte_atomic32_dec(i32_ptr)
258 #define ATOMIC32_SET(i32_ptr, val) rte_atomic32_set(i32_ptr, val)
259 #define ATOMIC32_READ(i32_ptr) rte_atomic32_read(i32_ptr)
261 #define msleep(x) rte_delay_us(x * 1000)
262 #define udelay(x) rte_delay_us(x)
264 #define dma_rmb() rmb()
266 #define MAX_ERRNO 4095
267 #define IS_ERR(x) (((unsigned long)x) >= (unsigned long)-MAX_ERRNO)
268 #define ERR_PTR(error) ((void *)(long)error)
269 #define PTR_ERR(error) ((long)(void *)error)
270 #define might_sleep()
272 #define prefetch(x) rte_prefetch0(x)
273 #define prefetchw(x) prefetch(x)
275 #define lower_32_bits(x) ((uint32_t)(x))
276 #define upper_32_bits(x) ((uint32_t)(((x) >> 16) >> 16))
278 #define ENA_TIME_EXPIRE(timeout) (timeout < rte_get_timer_cycles())
279 #define ENA_GET_SYSTEM_TIMEOUT(timeout_us) \
280 (timeout_us * rte_get_timer_hz() / 1000000 + rte_get_timer_cycles())
281 #define ENA_WAIT_EVENT_DESTROY(waitqueue) ((void)(waitqueue))
284 #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
287 #define READ_ONCE8(var) READ_ONCE(var)
288 #define READ_ONCE16(var) READ_ONCE(var)
289 #define READ_ONCE32(var) READ_ONCE(var)
291 /* The size must be 8 byte align */
292 #define ENA_MEMCPY_TO_DEVICE_64(dst, src, size) \
295 uint64_t *to = (uint64_t *)(dst); \
296 const uint64_t *from = (const uint64_t *)(src); \
297 count = (size) / 8; \
298 for (i = 0; i < count; i++, from++, to++) \
299 rte_write64_relaxed(*from, to); \
302 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
304 #define ENA_FFS(x) ffs(x)
306 void ena_rss_key_fill(void *key, size_t size);
308 #define ENA_RSS_FILL_KEY(key, size) ena_rss_key_fill(key, size)
310 #define ENA_INTR_INITIAL_TX_INTERVAL_USECS_PLAT 0
312 #define ENA_PRIu64 PRIu64
314 #include "ena_includes.h"
315 #endif /* DPDK_ENA_COM_ENA_PLAT_DPDK_H_ */