net/ena: switch memcpy to optimized version
[dpdk.git] / drivers / net / ena / base / ena_plat_dpdk.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2015-2020 Amazon.com, Inc. or its affiliates.
3  * All rights reserved.
4  */
5
6 #ifndef DPDK_ENA_COM_ENA_PLAT_DPDK_H_
7 #define DPDK_ENA_COM_ENA_PLAT_DPDK_H_
8
9 #include <stdbool.h>
10 #include <stdlib.h>
11 #include <pthread.h>
12 #include <stdint.h>
13 #include <inttypes.h>
14 #include <string.h>
15 #include <errno.h>
16
17 #include <rte_atomic.h>
18 #include <rte_branch_prediction.h>
19 #include <rte_cycles.h>
20 #include <rte_io.h>
21 #include <rte_log.h>
22 #include <rte_malloc.h>
23 #include <rte_memzone.h>
24 #include <rte_prefetch.h>
25 #include <rte_spinlock.h>
26
27 #include <sys/time.h>
28 #include <rte_memcpy.h>
29
30 typedef uint64_t u64;
31 typedef uint32_t u32;
32 typedef uint16_t u16;
33 typedef uint8_t u8;
34
35 typedef uint64_t dma_addr_t;
36 #ifndef ETIME
37 #define ETIME ETIMEDOUT
38 #endif
39
40 #define ena_atomic32_t rte_atomic32_t
41 #define ena_mem_handle_t const struct rte_memzone *
42
43 #define SZ_256 (256U)
44 #define SZ_4K (4096U)
45
46 #define ENA_COM_OK      0
47 #define ENA_COM_NO_MEM  -ENOMEM
48 #define ENA_COM_INVAL   -EINVAL
49 #define ENA_COM_NO_SPACE        -ENOSPC
50 #define ENA_COM_NO_DEVICE       -ENODEV
51 #define ENA_COM_TIMER_EXPIRED   -ETIME
52 #define ENA_COM_FAULT   -EFAULT
53 #define ENA_COM_TRY_AGAIN       -EAGAIN
54 #define ENA_COM_UNSUPPORTED    -EOPNOTSUPP
55 #define ENA_COM_EIO    -EIO
56
57 #define ____cacheline_aligned __rte_cache_aligned
58
59 #define ENA_ABORT() abort()
60
61 #define ENA_MSLEEP(x) rte_delay_us_sleep(x * 1000)
62 #define ENA_USLEEP(x) rte_delay_us_sleep(x)
63 #define ENA_UDELAY(x) rte_delay_us_block(x)
64
65 #define ENA_TOUCH(x) ((void)(x))
66 /* Avoid nested declaration on arm64, as it may define rte_memcpy as memcpy. */
67 #if defined(RTE_ARCH_X86)
68 #undef memcpy
69 #define memcpy rte_memcpy
70 #endif
71 #define wmb rte_wmb
72 #define rmb rte_rmb
73 #define mb rte_mb
74 #define mmiowb rte_io_wmb
75 #define __iomem
76
77 #define US_PER_S 1000000
78 #define ENA_GET_SYSTEM_USECS()                                          \
79         (rte_get_timer_cycles() * US_PER_S / rte_get_timer_hz())
80
81 extern int ena_logtype_com;
82
83 #define ENA_MAX_T(type, x, y) RTE_MAX((type)(x), (type)(y))
84 #define ENA_MAX32(x, y) ENA_MAX_T(uint32_t, (x), (y))
85 #define ENA_MAX16(x, y) ENA_MAX_T(uint16_t, (x), (y))
86 #define ENA_MAX8(x, y) ENA_MAX_T(uint8_t, (x), (y))
87 #define ENA_MIN_T(type, x, y) RTE_MIN((type)(x), (type)(y))
88 #define ENA_MIN32(x, y) ENA_MIN_T(uint32_t, (x), (y))
89 #define ENA_MIN16(x, y) ENA_MIN_T(uint16_t, (x), (y))
90 #define ENA_MIN8(x, y) ENA_MIN_T(uint8_t, (x), (y))
91
92 #define BITS_PER_LONG_LONG (__SIZEOF_LONG_LONG__ * 8)
93 #define U64_C(x) x ## ULL
94 #define BIT(nr)         (1UL << (nr))
95 #define BITS_PER_LONG   (__SIZEOF_LONG__ * 8)
96 #define GENMASK(h, l)   (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
97 #define GENMASK_ULL(h, l) (((~0ULL) - (1ULL << (l)) + 1) & \
98                           (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
99
100 #ifdef RTE_LIBRTE_ENA_COM_DEBUG
101 #define ena_trc_log(level, fmt, arg...) \
102         rte_log(RTE_LOG_ ## level, ena_logtype_com, \
103                 "[ENA_COM: %s]" fmt, __func__, ##arg)
104
105 #define ena_trc_dbg(format, arg...)     ena_trc_log(DEBUG, format, ##arg)
106 #define ena_trc_info(format, arg...)    ena_trc_log(INFO, format, ##arg)
107 #define ena_trc_warn(format, arg...)    ena_trc_log(WARNING, format, ##arg)
108 #define ena_trc_err(format, arg...)     ena_trc_log(ERR, format, ##arg)
109 #else
110 #define ena_trc_dbg(format, arg...) do { } while (0)
111 #define ena_trc_info(format, arg...) do { } while (0)
112 #define ena_trc_warn(format, arg...) do { } while (0)
113 #define ena_trc_err(format, arg...) do { } while (0)
114 #endif /* RTE_LIBRTE_ENA_COM_DEBUG */
115
116 #define ENA_WARN(cond, format, arg...)                                 \
117 do {                                                                   \
118        if (unlikely(cond)) {                                           \
119                ena_trc_err(                                            \
120                        "Warn failed on %s:%s:%d:" format,              \
121                        __FILE__, __func__, __LINE__, ##arg);           \
122        }                                                               \
123 } while (0)
124
125 /* Spinlock related methods */
126 #define ena_spinlock_t rte_spinlock_t
127 #define ENA_SPINLOCK_INIT(spinlock) rte_spinlock_init(&spinlock)
128 #define ENA_SPINLOCK_LOCK(spinlock, flags)                              \
129         ({(void)flags; rte_spinlock_lock(&spinlock); })
130 #define ENA_SPINLOCK_UNLOCK(spinlock, flags)                            \
131         ({(void)flags; rte_spinlock_unlock(&(spinlock)); })
132 #define ENA_SPINLOCK_DESTROY(spinlock) ((void)spinlock)
133
134 #define q_waitqueue_t                   \
135         struct {                        \
136                 pthread_cond_t cond;    \
137                 pthread_mutex_t mutex;  \
138         }
139
140 #define ena_wait_queue_t q_waitqueue_t
141
142 #define ENA_WAIT_EVENT_INIT(waitqueue)                                  \
143         do {                                                            \
144                 pthread_mutex_init(&(waitqueue).mutex, NULL);           \
145                 pthread_cond_init(&(waitqueue).cond, NULL);             \
146         } while (0)
147
148 #define ENA_WAIT_EVENT_WAIT(waitevent, timeout)                         \
149         do {                                                            \
150                 struct timespec wait;                                   \
151                 struct timeval now;                                     \
152                 unsigned long timeout_us;                               \
153                 gettimeofday(&now, NULL);                               \
154                 wait.tv_sec = now.tv_sec + timeout / 1000000UL;         \
155                 timeout_us = timeout % 1000000UL;                       \
156                 wait.tv_nsec = (now.tv_usec + timeout_us) * 1000UL;     \
157                 pthread_mutex_lock(&waitevent.mutex);                   \
158                 pthread_cond_timedwait(&waitevent.cond,                 \
159                                 &waitevent.mutex, &wait);               \
160                 pthread_mutex_unlock(&waitevent.mutex);                 \
161         } while (0)
162 #define ENA_WAIT_EVENT_SIGNAL(waitevent) pthread_cond_signal(&waitevent.cond)
163 /* pthread condition doesn't need to be rearmed after usage */
164 #define ENA_WAIT_EVENT_CLEAR(...)
165 #define ENA_WAIT_EVENT_DESTROY(waitqueue) ((void)(waitqueue))
166
167 #define ena_wait_event_t ena_wait_queue_t
168 #define ENA_MIGHT_SLEEP()
169
170 #define ena_time_t uint64_t
171 #define ENA_TIME_EXPIRE(timeout)  (timeout < rte_get_timer_cycles())
172 #define ENA_GET_SYSTEM_TIMEOUT(timeout_us)                             \
173        (timeout_us * rte_get_timer_hz() / 1000000 + rte_get_timer_cycles())
174
175 /*
176  * Each rte_memzone should have unique name.
177  * To satisfy it, count number of allocations and add it to name.
178  */
179 extern rte_atomic32_t ena_alloc_cnt;
180
181 #define ENA_MEM_ALLOC_COHERENT_ALIGNED(                                 \
182         dmadev, size, virt, phys, handle, alignment)                    \
183         do {                                                            \
184                 const struct rte_memzone *mz = NULL;                    \
185                 ENA_TOUCH(dmadev); ENA_TOUCH(handle);                   \
186                 if (size > 0) {                                         \
187                         char z_name[RTE_MEMZONE_NAMESIZE];              \
188                         snprintf(z_name, sizeof(z_name),                \
189                          "ena_alloc_%d",                                \
190                          rte_atomic32_add_return(&ena_alloc_cnt, 1));   \
191                         mz = rte_memzone_reserve_aligned(z_name, size,  \
192                                         SOCKET_ID_ANY,                  \
193                                         RTE_MEMZONE_IOVA_CONTIG,        \
194                                         alignment);                     \
195                         handle = mz;                                    \
196                 }                                                       \
197                 if (mz == NULL) {                                       \
198                         virt = NULL;                                    \
199                         phys = 0;                                       \
200                 } else {                                                \
201                         memset(mz->addr, 0, size);                      \
202                         virt = mz->addr;                                \
203                         phys = mz->iova;                                \
204                 }                                                       \
205         } while (0)
206 #define ENA_MEM_ALLOC_COHERENT(dmadev, size, virt, phys, handle)        \
207                 ENA_MEM_ALLOC_COHERENT_ALIGNED(                         \
208                         dmadev,                                         \
209                         size,                                           \
210                         virt,                                           \
211                         phys,                                           \
212                         handle,                                         \
213                         RTE_CACHE_LINE_SIZE)
214 #define ENA_MEM_FREE_COHERENT(dmadev, size, virt, phys, handle)         \
215                 ({ ENA_TOUCH(size); ENA_TOUCH(phys);                    \
216                    ENA_TOUCH(dmadev);                                   \
217                    rte_memzone_free(handle); })
218
219 #define ENA_MEM_ALLOC_COHERENT_NODE_ALIGNED(                            \
220         dmadev, size, virt, phys, mem_handle, node, dev_node, alignment) \
221         do {                                                            \
222                 const struct rte_memzone *mz = NULL;                    \
223                 ENA_TOUCH(dmadev); ENA_TOUCH(dev_node);                 \
224                 if (size > 0) {                                         \
225                         char z_name[RTE_MEMZONE_NAMESIZE];              \
226                         snprintf(z_name, sizeof(z_name),                \
227                          "ena_alloc_%d",                                \
228                          rte_atomic32_add_return(&ena_alloc_cnt, 1));   \
229                         mz = rte_memzone_reserve_aligned(z_name, size, node, \
230                                 RTE_MEMZONE_IOVA_CONTIG, alignment);    \
231                         mem_handle = mz;                                \
232                 }                                                       \
233                 if (mz == NULL) {                                       \
234                         virt = NULL;                                    \
235                         phys = 0;                                       \
236                 } else {                                                \
237                         memset(mz->addr, 0, size);                      \
238                         virt = mz->addr;                                \
239                         phys = mz->iova;                                \
240                 }                                                       \
241         } while (0)
242 #define ENA_MEM_ALLOC_COHERENT_NODE(                                    \
243         dmadev, size, virt, phys, mem_handle, node, dev_node)           \
244                 ENA_MEM_ALLOC_COHERENT_NODE_ALIGNED(                    \
245                         dmadev,                                         \
246                         size,                                           \
247                         virt,                                           \
248                         phys,                                           \
249                         mem_handle,                                     \
250                         node,                                           \
251                         dev_node,                                       \
252                         RTE_CACHE_LINE_SIZE)
253 #define ENA_MEM_ALLOC_NODE(dmadev, size, virt, node, dev_node) \
254         do {                                                            \
255                 ENA_TOUCH(dmadev); ENA_TOUCH(dev_node);                 \
256                 virt = rte_zmalloc_socket(NULL, size, 0, node);         \
257         } while (0)
258
259 #define ENA_MEM_ALLOC(dmadev, size) rte_zmalloc(NULL, size, 1)
260 #define ENA_MEM_FREE(dmadev, ptr, size)                                 \
261         ({ ENA_TOUCH(dmadev); ENA_TOUCH(size); rte_free(ptr); })
262
263 #define ENA_DB_SYNC(mem_handle) ((void)mem_handle)
264
265 #define ENA_REG_WRITE32(bus, value, reg)                                \
266         ({ (void)(bus); rte_write32((value), (reg)); })
267 #define ENA_REG_WRITE32_RELAXED(bus, value, reg)                        \
268         ({ (void)(bus); rte_write32_relaxed((value), (reg)); })
269 #define ENA_REG_READ32(bus, reg)                                        \
270         ({ (void)(bus); rte_read32_relaxed((reg)); })
271
272 #define ATOMIC32_INC(i32_ptr) rte_atomic32_inc(i32_ptr)
273 #define ATOMIC32_DEC(i32_ptr) rte_atomic32_dec(i32_ptr)
274 #define ATOMIC32_SET(i32_ptr, val) rte_atomic32_set(i32_ptr, val)
275 #define ATOMIC32_READ(i32_ptr) rte_atomic32_read(i32_ptr)
276
277 #define msleep(x) rte_delay_us(x * 1000)
278 #define udelay(x) rte_delay_us(x)
279
280 #define dma_rmb() rmb()
281
282 #define MAX_ERRNO       4095
283 #define IS_ERR(x) (((unsigned long)x) >= (unsigned long)-MAX_ERRNO)
284 #define ERR_PTR(error) ((void *)(long)error)
285 #define PTR_ERR(error) ((long)(void *)error)
286 #define might_sleep()
287
288 #define prefetch(x) rte_prefetch0(x)
289 #define prefetchw(x) prefetch(x)
290
291 #define lower_32_bits(x) ((uint32_t)(x))
292 #define upper_32_bits(x) ((uint32_t)(((x) >> 16) >> 16))
293
294 #define ENA_TIME_EXPIRE(timeout)  (timeout < rte_get_timer_cycles())
295 #define ENA_GET_SYSTEM_TIMEOUT(timeout_us)                              \
296     (timeout_us * rte_get_timer_hz() / 1000000 + rte_get_timer_cycles())
297 #define ENA_WAIT_EVENT_DESTROY(waitqueue) ((void)(waitqueue))
298
299 #ifndef READ_ONCE
300 #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
301 #endif
302
303 #define READ_ONCE8(var) READ_ONCE(var)
304 #define READ_ONCE16(var) READ_ONCE(var)
305 #define READ_ONCE32(var) READ_ONCE(var)
306
307 /* The size must be 8 byte align */
308 #define ENA_MEMCPY_TO_DEVICE_64(dst, src, size)                         \
309         do {                                                            \
310                 int count, i;                                           \
311                 uint64_t *to = (uint64_t *)(dst);                       \
312                 const uint64_t *from = (const uint64_t *)(src);         \
313                 count = (size) / 8;                                     \
314                 for (i = 0; i < count; i++, from++, to++)               \
315                         rte_write64_relaxed(*from, to);                 \
316         } while(0)
317
318 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
319
320 #define ENA_FFS(x) ffs(x)
321
322 void ena_rss_key_fill(void *key, size_t size);
323
324 #define ENA_RSS_FILL_KEY(key, size) ena_rss_key_fill(key, size)
325
326 #define ENA_INTR_INITIAL_TX_INTERVAL_USECS_PLAT 0
327
328 #define ENA_PRIu64 PRIu64
329
330 #include "ena_includes.h"
331 #endif /* DPDK_ENA_COM_ENA_PLAT_DPDK_H_ */