Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2017 Huawei Technologies Co., Ltd
3 : : */
4 : :
5 : : #ifndef _HINIC_COMPAT_H_
6 : : #define _HINIC_COMPAT_H_
7 : :
8 : : #include <stdint.h>
9 : : #include <sys/time.h>
10 : : #include <unistd.h>
11 : : #include <pthread.h>
12 : : #include <rte_common.h>
13 : : #include <rte_bitops.h>
14 : : #include <rte_byteorder.h>
15 : : #include <rte_memzone.h>
16 : : #include <rte_memcpy.h>
17 : : #include <rte_malloc.h>
18 : : #include <rte_stdatomic.h>
19 : : #include <rte_spinlock.h>
20 : : #include <rte_cycles.h>
21 : : #include <rte_log.h>
22 : : #include <rte_thread.h>
23 : :
24 : : typedef uint8_t u8;
25 : : typedef int8_t s8;
26 : : typedef uint16_t u16;
27 : : typedef uint32_t u32;
28 : : typedef int32_t s32;
29 : : typedef uint64_t u64;
30 : :
31 : : #ifndef dma_addr_t
32 : : typedef uint64_t dma_addr_t;
33 : : #endif
34 : :
35 : : #ifndef gfp_t
36 : : #define gfp_t unsigned
37 : : #endif
38 : :
39 : : #ifndef bool
40 : : #define bool int
41 : : #endif
42 : :
43 : : #ifndef FALSE
44 : : #define FALSE (0)
45 : : #endif
46 : :
47 : : #ifndef TRUE
48 : : #define TRUE (1)
49 : : #endif
50 : :
51 : : #ifndef false
52 : : #define false (0)
53 : : #endif
54 : :
55 : : #ifndef true
56 : : #define true (1)
57 : : #endif
58 : :
59 : : #ifndef NULL
60 : : #define NULL ((void *)0)
61 : : #endif
62 : :
63 : : #define HINIC_ERROR (-1)
64 : : #define HINIC_OK (0)
65 : :
66 : : #ifndef BIT
67 : : #define BIT(n) (1 << (n))
68 : : #endif
69 : :
70 : : #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
71 : : #define lower_32_bits(n) ((u32)(n))
72 : :
73 : : /* Returns X rounded up to the nearest multiple of Y. */
74 : : #define ROUND_UP(X, Y) (RTE_DIV_ROUND_UP(X, Y) * (Y))
75 : :
76 : : #undef ALIGN
77 : : #define ALIGN(x, a) RTE_ALIGN(x, a)
78 : :
79 : : #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
80 : :
81 : : extern int hinic_logtype;
82 : : #define RTE_LOGTYPE_NET_HINIC hinic_logtype
83 : :
84 : : #define PMD_DRV_LOG(level, ...) \
85 : : RTE_LOG_LINE(level, NET_HINIC, __VA_ARGS__)
86 : :
87 : : /* common definition */
88 : : #ifndef ETH_ALEN
89 : : #define ETH_ALEN 6
90 : : #endif
91 : : #define ETH_HLEN 14
92 : : #define ETH_CRC_LEN 4
93 : : #define VLAN_PRIO_SHIFT 13
94 : : #define VLAN_N_VID 4096
95 : :
96 : : /* bit order interface */
97 : : #define cpu_to_be16(o) rte_cpu_to_be_16(o)
98 : : #define cpu_to_be32(o) rte_cpu_to_be_32(o)
99 : : #define cpu_to_be64(o) rte_cpu_to_be_64(o)
100 : : #define cpu_to_le32(o) rte_cpu_to_le_32(o)
101 : : #define be16_to_cpu(o) rte_be_to_cpu_16(o)
102 : : #define be32_to_cpu(o) rte_be_to_cpu_32(o)
103 : : #define be64_to_cpu(o) rte_be_to_cpu_64(o)
104 : : #define le32_to_cpu(o) rte_le_to_cpu_32(o)
105 : :
106 : : /* virt memory and dma phy memory */
107 : : #define __iomem
108 : : #define GFP_KERNEL RTE_MEMZONE_IOVA_CONTIG
109 : : #define HINIC_PAGE_SHIFT 12
110 : : #define HINIC_PAGE_SIZE RTE_PGSIZE_4K
111 : : #define HINIC_MEM_ALLOC_ALIGN_MIN 8
112 : :
113 : : #define HINIC_PAGE_SIZE_DPDK 6
114 : :
115 : : void *dma_zalloc_coherent(void *dev, size_t size, dma_addr_t *dma_handle,
116 : : unsigned int socket_id);
117 : :
118 : : void *dma_zalloc_coherent_aligned(void *hwdev, size_t size,
119 : : dma_addr_t *dma_handle, unsigned int socket_id);
120 : :
121 : : void *dma_zalloc_coherent_aligned256k(void *hwdev, size_t size,
122 : : dma_addr_t *dma_handle, unsigned int socket_id);
123 : :
124 : : void dma_free_coherent(void *dev, size_t size, void *virt, dma_addr_t phys);
125 : :
126 : : /* dma pool alloc and free */
127 : : #define pci_pool dma_pool
128 : : #define pci_pool_alloc(pool, handle) dma_pool_alloc(pool, handle)
129 : : #define pci_pool_free(pool, vaddr, addr) dma_pool_free(pool, vaddr, addr)
130 : :
131 : : struct dma_pool *dma_pool_create(const char *name, void *dev, size_t size,
132 : : size_t align, size_t boundary);
133 : : void dma_pool_destroy(struct dma_pool *pool);
134 : : void *dma_pool_alloc(struct pci_pool *pool, dma_addr_t *dma_addr);
135 : : void dma_pool_free(struct pci_pool *pool, void *vaddr, dma_addr_t dma);
136 : :
137 : : #define kzalloc(size, flag) rte_zmalloc(NULL, size, HINIC_MEM_ALLOC_ALIGN_MIN)
138 : : #define kzalloc_aligned(size, flag) rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE)
139 : : #define kfree(ptr) rte_free(ptr)
140 : :
141 : : /* mmio interface */
142 : : static inline void writel(u32 value, volatile void *addr)
143 : : {
144 [ # # # # ]: 0 : *(volatile u32 *)addr = value;
145 : 0 : }
146 : :
147 : : static inline u32 readl(const volatile void *addr)
148 : : {
149 [ # # ]: 0 : return *(const volatile u32 *)addr;
150 : : }
151 : :
152 : : #define __raw_writel(value, reg) writel((value), (reg))
153 : : #define __raw_readl(reg) readl((reg))
154 : :
155 : : /* Spinlock related interface */
156 : : #define hinic_spinlock_t rte_spinlock_t
157 : :
158 : : #define spinlock_t rte_spinlock_t
159 : : #define spin_lock_init(spinlock_prt) rte_spinlock_init(spinlock_prt)
160 : : #define spin_lock_deinit(lock)
161 : : #define spin_lock(spinlock_prt) rte_spinlock_lock(spinlock_prt)
162 : : #define spin_unlock(spinlock_prt) rte_spinlock_unlock(spinlock_prt)
163 : :
164 : : #ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
165 : : #define CLOCK_TYPE CLOCK_MONOTONIC_RAW
166 : : #else
167 : : #define CLOCK_TYPE CLOCK_MONOTONIC
168 : : #endif
169 : : #define HINIC_MUTEX_TIMEOUT 10
170 : :
171 : : static inline unsigned long clock_gettime_ms(void)
172 : : {
173 : : struct timespec tv;
174 : :
175 : 0 : (void)clock_gettime(CLOCK_TYPE, &tv);
176 : :
177 : 0 : return (unsigned long)tv.tv_sec * 1000 +
178 [ # # # # : 0 : (unsigned long)tv.tv_nsec / 1000000;
# # ]
179 : : }
180 : :
181 : : #define jiffies clock_gettime_ms()
182 : : #define msecs_to_jiffies(ms) (ms)
183 : : #define time_before(now, end) ((now) < (end))
184 : :
185 : : /* misc kernel utils */
186 : : static inline u16 ilog2(u32 n)
187 : : {
188 : : u16 res = 0;
189 : :
190 [ # # # # ]: 0 : while (n > 1) {
191 : 0 : n >>= 1;
192 : 0 : res++;
193 : : }
194 : :
195 : : return res;
196 : : }
197 : :
198 : : static inline int hinic_mutex_init(pthread_mutex_t *pthreadmutex,
199 : : __rte_unused const pthread_mutexattr_t *mattr)
200 : : {
201 : 0 : rte_thread_mutex_init_shared(pthreadmutex);
202 : : return 0;
203 : : }
204 : :
205 : 0 : static inline int hinic_mutex_destroy(pthread_mutex_t *pthreadmutex)
206 : : {
207 : : int err;
208 : :
209 : 0 : err = pthread_mutex_destroy(pthreadmutex);
210 [ # # ]: 0 : if (unlikely(err))
211 : 0 : PMD_DRV_LOG(ERR, "Fail to destroy mutex, error: %d", err);
212 : :
213 : 0 : return err;
214 : : }
215 : :
216 : 0 : static inline int hinic_mutex_lock(pthread_mutex_t *pthreadmutex)
217 : : {
218 : : int err;
219 : : struct timespec tout;
220 : :
221 : 0 : (void)clock_gettime(CLOCK_TYPE, &tout);
222 : :
223 : 0 : tout.tv_sec += HINIC_MUTEX_TIMEOUT;
224 : 0 : err = pthread_mutex_timedlock(pthreadmutex, &tout);
225 [ # # ]: 0 : if (err)
226 : 0 : PMD_DRV_LOG(ERR, "Mutex lock failed. (ErrorNo=%d)", err);
227 : :
228 : 0 : return err;
229 : : }
230 : :
231 : : static inline int hinic_mutex_unlock(pthread_mutex_t *pthreadmutex)
232 : : {
233 : 0 : return pthread_mutex_unlock(pthreadmutex);
234 : : }
235 : :
236 : : #endif /* _HINIC_COMPAT_H_ */
|