4 * Copyright (c) 2015-2016 Amazon.com, Inc. or its affiliates.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef DPDK_ENA_COM_ENA_PLAT_DPDK_H_
35 #define DPDK_ENA_COM_ENA_PLAT_DPDK_H_
44 #include <rte_atomic.h>
45 #include <rte_branch_prediction.h>
46 #include <rte_cycles.h>
49 #include <rte_malloc.h>
50 #include <rte_memzone.h>
51 #include <rte_prefetch.h>
52 #include <rte_spinlock.h>
61 typedef uint64_t dma_addr_t;
63 #define ETIME ETIMEDOUT
66 #define ena_atomic32_t rte_atomic32_t
67 #define ena_mem_handle_t const struct rte_memzone *
73 #define ENA_COM_NO_MEM -ENOMEM
74 #define ENA_COM_INVAL -EINVAL
75 #define ENA_COM_NO_SPACE -ENOSPC
76 #define ENA_COM_NO_DEVICE -ENODEV
77 #define ENA_COM_TIMER_EXPIRED -ETIME
78 #define ENA_COM_FAULT -EFAULT
79 #define ENA_COM_TRY_AGAIN -EAGAIN
80 #define ENA_COM_UNSUPPORTED -EOPNOTSUPP
82 #define ____cacheline_aligned __rte_cache_aligned
84 #define ENA_ABORT() abort()
86 #define ENA_MSLEEP(x) rte_delay_ms(x)
87 #define ENA_UDELAY(x) rte_delay_us(x)
89 #define ENA_TOUCH(x) ((void)(x))
90 #define memcpy_toio memcpy
94 #define mmiowb rte_io_wmb
97 #define US_PER_S 1000000
98 #define ENA_GET_SYSTEM_USECS() \
99 (rte_get_timer_cycles() * US_PER_S / rte_get_timer_hz())
101 extern int ena_logtype_com;
102 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
103 #define ENA_ASSERT(cond, format, arg...) \
105 if (unlikely(!(cond))) { \
106 rte_log(RTE_LOGTYPE_ERR, ena_logtype_com, \
108 rte_panic("line %d\tassert \"" #cond "\"" \
109 "failed\n", __LINE__); \
113 #define ENA_ASSERT(cond, format, arg...) do {} while (0)
116 #define ENA_MAX32(x, y) RTE_MAX((x), (y))
117 #define ENA_MAX16(x, y) RTE_MAX((x), (y))
118 #define ENA_MAX8(x, y) RTE_MAX((x), (y))
119 #define ENA_MIN32(x, y) RTE_MIN((x), (y))
120 #define ENA_MIN16(x, y) RTE_MIN((x), (y))
121 #define ENA_MIN8(x, y) RTE_MIN((x), (y))
123 #define BITS_PER_LONG_LONG (__SIZEOF_LONG_LONG__ * 8)
124 #define U64_C(x) x ## ULL
125 #define BIT(nr) (1UL << (nr))
126 #define BITS_PER_LONG (__SIZEOF_LONG__ * 8)
127 #define GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
128 #define GENMASK_ULL(h, l) (((~0ULL) - (1ULL << (l)) + 1) & \
129 (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
131 #ifdef RTE_LIBRTE_ENA_COM_DEBUG
132 #define ena_trc_log(level, fmt, arg...) \
133 rte_log(RTE_LOG_ ## level, ena_logtype_com, \
134 "[ENA_COM: %s]" fmt, __func__, ##arg)
136 #define ena_trc_dbg(format, arg...) ena_trc_log(DEBUG, format, ##arg)
137 #define ena_trc_info(format, arg...) ena_trc_log(INFO, format, ##arg)
138 #define ena_trc_warn(format, arg...) ena_trc_log(WARNING, format, ##arg)
139 #define ena_trc_err(format, arg...) ena_trc_log(ERR, format, ##arg)
141 #define ena_trc_dbg(format, arg...) do { } while (0)
142 #define ena_trc_info(format, arg...) do { } while (0)
143 #define ena_trc_warn(format, arg...) do { } while (0)
144 #define ena_trc_err(format, arg...) do { } while (0)
145 #endif /* RTE_LIBRTE_ENA_COM_DEBUG */
147 #define ENA_WARN(cond, format, arg...) \
149 if (unlikely(cond)) { \
151 "Warn failed on %s:%s:%d:" format, \
152 __FILE__, __func__, __LINE__, ##arg); \
156 /* Spinlock related methods */
157 #define ena_spinlock_t rte_spinlock_t
158 #define ENA_SPINLOCK_INIT(spinlock) rte_spinlock_init(&spinlock)
159 #define ENA_SPINLOCK_LOCK(spinlock, flags) \
160 ({(void)flags; rte_spinlock_lock(&spinlock); })
161 #define ENA_SPINLOCK_UNLOCK(spinlock, flags) \
162 ({(void)flags; rte_spinlock_unlock(&(spinlock)); })
163 #define ENA_SPINLOCK_DESTROY(spinlock) ((void)spinlock)
165 #define q_waitqueue_t \
167 pthread_cond_t cond; \
168 pthread_mutex_t mutex; \
171 #define ena_wait_queue_t q_waitqueue_t
173 #define ENA_WAIT_EVENT_INIT(waitqueue) \
175 pthread_mutex_init(&(waitqueue).mutex, NULL); \
176 pthread_cond_init(&(waitqueue).cond, NULL); \
179 #define ENA_WAIT_EVENT_WAIT(waitevent, timeout) \
181 struct timespec wait; \
182 struct timeval now; \
183 unsigned long timeout_us; \
184 gettimeofday(&now, NULL); \
185 wait.tv_sec = now.tv_sec + timeout / 1000000UL; \
186 timeout_us = timeout % 1000000UL; \
187 wait.tv_nsec = (now.tv_usec + timeout_us) * 1000UL; \
188 pthread_mutex_lock(&waitevent.mutex); \
189 pthread_cond_timedwait(&waitevent.cond, \
190 &waitevent.mutex, &wait); \
191 pthread_mutex_unlock(&waitevent.mutex); \
193 #define ENA_WAIT_EVENT_SIGNAL(waitevent) pthread_cond_signal(&waitevent.cond)
194 /* pthread condition doesn't need to be rearmed after usage */
195 #define ENA_WAIT_EVENT_CLEAR(...)
196 #define ENA_WAIT_EVENT_DESTROY(waitqueue) ((void)(waitqueue))
198 #define ena_wait_event_t ena_wait_queue_t
199 #define ENA_MIGHT_SLEEP()
201 #define ENA_TIME_EXPIRE(timeout) (timeout < rte_get_timer_cycles())
202 #define ENA_GET_SYSTEM_TIMEOUT(timeout_us) \
203 (timeout_us * rte_get_timer_hz() / 1000000 + rte_get_timer_cycles())
206 * Each rte_memzone should have unique name.
207 * To satisfy it, count number of allocations and add it to name.
209 extern uint32_t ena_alloc_cnt;
211 #define ENA_MEM_ALLOC_COHERENT(dmadev, size, virt, phys, handle) \
213 const struct rte_memzone *mz; \
214 char z_name[RTE_MEMZONE_NAMESIZE]; \
215 ENA_TOUCH(dmadev); ENA_TOUCH(handle); \
216 snprintf(z_name, sizeof(z_name), \
217 "ena_alloc_%d", ena_alloc_cnt++); \
218 mz = rte_memzone_reserve(z_name, size, SOCKET_ID_ANY, \
219 RTE_MEMZONE_IOVA_CONTIG); \
225 memset(mz->addr, 0, size); \
230 #define ENA_MEM_FREE_COHERENT(dmadev, size, virt, phys, handle) \
231 ({ ENA_TOUCH(size); ENA_TOUCH(phys); \
233 rte_memzone_free(handle); })
235 #define ENA_MEM_ALLOC_COHERENT_NODE( \
236 dmadev, size, virt, phys, mem_handle, node, dev_node) \
238 const struct rte_memzone *mz; \
239 char z_name[RTE_MEMZONE_NAMESIZE]; \
240 ENA_TOUCH(dmadev); ENA_TOUCH(dev_node); \
241 snprintf(z_name, sizeof(z_name), \
242 "ena_alloc_%d", ena_alloc_cnt++); \
243 mz = rte_memzone_reserve(z_name, size, node, \
244 RTE_MEMZONE_IOVA_CONTIG); \
250 memset(mz->addr, 0, size); \
256 #define ENA_MEM_ALLOC_NODE(dmadev, size, virt, node, dev_node) \
258 ENA_TOUCH(dmadev); ENA_TOUCH(dev_node); \
259 virt = rte_zmalloc_socket(NULL, size, 0, node); \
262 #define ENA_MEM_ALLOC(dmadev, size) rte_zmalloc(NULL, size, 1)
263 #define ENA_MEM_FREE(dmadev, ptr) ({ENA_TOUCH(dmadev); rte_free(ptr); })
265 #define ENA_DB_SYNC(mem_handle) ((void)mem_handle)
267 #define ENA_REG_WRITE32(bus, value, reg) \
268 ({ (void)(bus); rte_write32((value), (reg)); })
269 #define ENA_REG_WRITE32_RELAXED(bus, value, reg) \
270 ({ (void)(bus); rte_write32_relaxed((value), (reg)); })
271 #define ENA_REG_READ32(bus, reg) \
272 ({ (void)(bus); rte_read32_relaxed((reg)); })
274 #define ATOMIC32_INC(i32_ptr) rte_atomic32_inc(i32_ptr)
275 #define ATOMIC32_DEC(i32_ptr) rte_atomic32_dec(i32_ptr)
276 #define ATOMIC32_SET(i32_ptr, val) rte_atomic32_set(i32_ptr, val)
277 #define ATOMIC32_READ(i32_ptr) rte_atomic32_read(i32_ptr)
279 #define msleep(x) rte_delay_us(x * 1000)
280 #define udelay(x) rte_delay_us(x)
282 #define dma_rmb() rmb()
284 #define MAX_ERRNO 4095
285 #define IS_ERR(x) (((unsigned long)x) >= (unsigned long)-MAX_ERRNO)
286 #define ERR_PTR(error) ((void *)(long)error)
287 #define PTR_ERR(error) ((long)(void *)error)
288 #define might_sleep()
290 #define prefetch(x) rte_prefetch0(x)
292 #define lower_32_bits(x) ((uint32_t)(x))
293 #define upper_32_bits(x) ((uint32_t)(((x) >> 16) >> 16))
295 #define ENA_TIME_EXPIRE(timeout) (timeout < rte_get_timer_cycles())
296 #define ENA_GET_SYSTEM_TIMEOUT(timeout_us) \
297 (timeout_us * rte_get_timer_hz() / 1000000 + rte_get_timer_cycles())
298 #define ENA_WAIT_EVENT_DESTROY(waitqueue) ((void)(waitqueue))
301 #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
304 #define READ_ONCE8(var) READ_ONCE(var)
305 #define READ_ONCE16(var) READ_ONCE(var)
306 #define READ_ONCE32(var) READ_ONCE(var)
308 /* The size must be 8 byte align */
309 #define ENA_MEMCPY_TO_DEVICE_64(dst, src, size) \
312 uint64_t *to = (uint64_t *)(dst); \
313 const uint64_t *from = (const uint64_t *)(src); \
314 count = (size) / 8; \
315 for (i = 0; i < count; i++, from++, to++) \
316 rte_write64_relaxed(*from, to); \
319 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
321 #endif /* DPDK_ENA_COM_ENA_PLAT_DPDK_H_ */