Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2022 Intel Corporation
3 : : */
4 : :
5 : : #ifndef _GVE_OSDEP_H_
6 : : #define _GVE_OSDEP_H_
7 : :
8 : : #include <string.h>
9 : : #include <stdint.h>
10 : : #include <stdio.h>
11 : : #include <stdarg.h>
12 : : #include <inttypes.h>
13 : : #include <stdbool.h>
14 : :
15 : : #include <rte_bitops.h>
16 : : #include <rte_byteorder.h>
17 : : #include <rte_common.h>
18 : : #include <rte_ether.h>
19 : : #include <rte_io.h>
20 : : #include <rte_log.h>
21 : : #include <rte_malloc.h>
22 : : #include <rte_memcpy.h>
23 : : #include <rte_memzone.h>
24 : : #include <rte_version.h>
25 : :
26 : : #include "../gve_logs.h"
27 : :
28 : : #include <sys/utsname.h>
29 : :
30 : : #ifndef u8
31 : : #define u8 uint8_t
32 : : #endif
33 : : #ifndef u16
34 : : #define u16 uint16_t
35 : : #endif
36 : : #ifndef u32
37 : : #define u32 uint32_t
38 : : #endif
39 : : #ifndef u64
40 : : #define u64 uint64_t
41 : : #endif
42 : :
43 : : #ifndef __sum16
44 : : #define __sum16 rte_be16_t
45 : : #endif
46 : :
47 : : #ifndef __be16
48 : : #define __be16 rte_be16_t
49 : : #endif
50 : : #ifndef __be32
51 : : #define __be32 rte_be32_t
52 : : #endif
53 : : #ifndef __be64
54 : : #define __be64 rte_be64_t
55 : : #endif
56 : :
57 : : #ifndef __le16
58 : : #define __le16 rte_le16_t
59 : : #endif
60 : : #ifndef __le32
61 : : #define __le32 rte_le32_t
62 : : #endif
63 : : #ifndef __le64
64 : : #define __le64 rte_le64_t
65 : : #endif
66 : :
67 : : #ifndef dma_addr_t
68 : : #define dma_addr_t rte_iova_t
69 : : #endif
70 : :
71 : : #define ETH_MIN_MTU RTE_ETHER_MIN_MTU
72 : : #define ETH_ALEN RTE_ETHER_ADDR_LEN
73 : :
74 : : #ifndef PAGE_SHIFT
75 : : #define PAGE_SHIFT 12
76 : : #endif
77 : : #ifndef PAGE_SIZE
78 : : #define PAGE_SIZE (1UL << PAGE_SHIFT)
79 : : #endif
80 : :
81 : : #define BIT(nr) RTE_BIT32(nr)
82 : :
83 : : #define be16_to_cpu(x) rte_be_to_cpu_16(x)
84 : : #define be32_to_cpu(x) rte_be_to_cpu_32(x)
85 : : #define be64_to_cpu(x) rte_be_to_cpu_64(x)
86 : :
87 : : #define cpu_to_be16(x) rte_cpu_to_be_16(x)
88 : : #define cpu_to_be32(x) rte_cpu_to_be_32(x)
89 : : #define cpu_to_be64(x) rte_cpu_to_be_64(x)
90 : :
91 : : #define READ_ONCE32(x) rte_read32(&(x))
92 : :
93 : : #ifndef ____cacheline_aligned
94 : : #define ____cacheline_aligned __rte_cache_aligned
95 : : #endif
96 : : #ifndef __packed
97 : : #define __packed __attribute__((__packed__))
98 : : #endif
99 : : #define __iomem
100 : :
101 : : #define msleep(ms) rte_delay_ms(ms)
102 : :
103 : : #define OS_VERSION_STRLEN 128
104 : : struct os_version_string {
105 : : char os_version_str1[OS_VERSION_STRLEN];
106 : : char os_version_str2[OS_VERSION_STRLEN];
107 : : };
108 : :
109 : : /* These macros are used to generate compilation errors if a struct/union
110 : : * is not exactly the correct length. It gives a divide by zero error if
111 : : * the struct/union is not of the correct size, otherwise it creates an
112 : : * enum that is never used.
113 : : */
114 : : #define GVE_CHECK_STRUCT_LEN(n, X) enum gve_static_assert_enum_##X \
115 : : { gve_static_assert_##X = (n) / ((sizeof(struct X) == (n)) ? 1 : 0) }
116 : : #define GVE_CHECK_UNION_LEN(n, X) enum gve_static_asset_enum_##X \
117 : : { gve_static_assert_##X = (n) / ((sizeof(union X) == (n)) ? 1 : 0) }
118 : :
119 : : static __rte_always_inline u8
120 : : readb(volatile void *addr)
121 : : {
122 : : return rte_read8(addr);
123 : : }
124 : :
125 : : static __rte_always_inline void
126 : : writeb(u8 value, volatile void *addr)
127 : : {
128 : : rte_write8(value, addr);
129 : : }
130 : :
131 : : static __rte_always_inline void
132 : : writew(u16 value, volatile void *addr)
133 : : {
134 : : rte_write16(value, addr);
135 : : }
136 : :
137 : : static __rte_always_inline void
138 : : writel(u32 value, volatile void *addr)
139 : : {
140 : : rte_write32(value, addr);
141 : : }
142 : :
143 : : static __rte_always_inline u16
144 : : ioread16be(const volatile void *addr)
145 : : {
146 : : return rte_be_to_cpu_16(rte_read16(addr));
147 : : }
148 : :
149 : : static __rte_always_inline u32
150 : : ioread32be(const volatile void *addr)
151 : : {
152 : : return rte_be_to_cpu_32(rte_read32(addr));
153 : : }
154 : :
155 : : static __rte_always_inline void
156 : : iowrite16be(u16 value, volatile void *addr)
157 : : {
158 : : writew(rte_cpu_to_be_16(value), addr);
159 : : }
160 : :
161 : : static __rte_always_inline void
162 : : iowrite32be(u32 value, volatile void *addr)
163 : : {
164 [ # # # # : 0 : writel(rte_cpu_to_be_32(value), addr);
# # # # ]
165 : 0 : }
166 : :
167 : : /* DMA memory allocation tracking */
168 : : struct gve_dma_mem {
169 : : void *va;
170 : : rte_iova_t pa;
171 : : uint32_t size;
172 : : const void *zone;
173 : : };
174 : :
175 : : static inline void *
176 : 0 : gve_alloc_dma_mem(struct gve_dma_mem *mem, u64 size)
177 : : {
178 : : static RTE_ATOMIC(uint16_t) gve_dma_memzone_id;
179 : : const struct rte_memzone *mz = NULL;
180 : : char z_name[RTE_MEMZONE_NAMESIZE];
181 : :
182 [ # # ]: 0 : if (!mem)
183 : : return NULL;
184 : :
185 : 0 : snprintf(z_name, sizeof(z_name), "gve_dma_%u",
186 : 0 : rte_atomic_fetch_add_explicit(&gve_dma_memzone_id, 1, rte_memory_order_relaxed));
187 : 0 : mz = rte_memzone_reserve_aligned(z_name, size, SOCKET_ID_ANY,
188 : : RTE_MEMZONE_IOVA_CONTIG,
189 : : PAGE_SIZE);
190 [ # # ]: 0 : if (!mz)
191 : : return NULL;
192 : :
193 : 0 : mem->size = size;
194 : 0 : mem->va = mz->addr;
195 : 0 : mem->pa = mz->iova;
196 : 0 : mem->zone = mz;
197 : 0 : PMD_DRV_LOG(DEBUG, "memzone %s is allocated", mz->name);
198 : :
199 : 0 : return mem->va;
200 : : }
201 : :
202 : : static inline void
203 : 0 : gve_free_dma_mem(struct gve_dma_mem *mem)
204 : : {
205 : 0 : PMD_DRV_LOG(DEBUG, "memzone %s to be freed",
206 : : ((const struct rte_memzone *)mem->zone)->name);
207 : :
208 : 0 : rte_memzone_free(mem->zone);
209 : 0 : mem->zone = NULL;
210 : 0 : mem->va = NULL;
211 : 0 : mem->pa = 0;
212 : 0 : }
213 : :
214 : : static inline void
215 : 0 : populate_driver_version_strings(char *str1, char *str2)
216 : : {
217 : : struct utsname uts;
218 [ # # ]: 0 : if (uname(&uts) >= 0) {
219 : : /* release */
220 : 0 : rte_strscpy(str1, uts.release,
221 : : OS_VERSION_STRLEN);
222 : : /* version */
223 : 0 : rte_strscpy(str2, uts.version,
224 : : OS_VERSION_STRLEN);
225 : : }
226 : 0 : }
227 : : #endif /* _GVE_OSDEP_H_ */
|