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