xref: /dpdk/drivers/common/mlx5/windows/mlx5_common_os.h (revision 72206323a5dd3182b13f61b25a64abdddfee595c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4 
5 #ifndef RTE_PMD_MLX5_COMMON_OS_H_
6 #define RTE_PMD_MLX5_COMMON_OS_H_
7 
8 #include <stdio.h>
9 #include <sys/types.h>
10 
11 #include <rte_errno.h>
12 #include <rte_interrupts.h>
13 
14 #include "mlx5_autoconf.h"
15 #include "mlx5_glue.h"
16 #include "mlx5_malloc.h"
17 #include "mlx5_common_mr.h"
18 #include "mlx5_win_ext.h"
19 
20 #define MLX5_BF_OFFSET 0x800
21 
22 /**
23  * This API allocates aligned or non-aligned memory.  The free can be on either
24  * aligned or nonaligned memory.  To be protected - even though there may be no
25  * alignment - in Windows this API will unconditionally call _aligned_malloc()
26  * with at least a minimal alignment size.
27  *
28  * @param[in] align
29  *    The alignment value, which must be an integer power of 2 (or 0 for
30  *    non-alignment)
31  * @param[in] size
32  *    Size in bytes to allocate
33  *
34  * @return
35  *    Valid pointer to allocated memory, NULL in case of failure
36  */
37 static inline void *
38 mlx5_os_malloc(size_t align, size_t size)
39 {
40 	if (align < MLX5_MALLOC_ALIGNMENT)
41 		align = MLX5_MALLOC_ALIGNMENT;
42 	return _aligned_malloc(size, align);
43 }
44 
45 /**
46  * This API de-allocates a memory that originally could have been allocated
47  * aligned or non-aligned. In Windows since the allocation was with
48  * _aligned_malloc() - it is safe to always call _aligned_free().
49  *
50  * @param[in] addr
51  *    Pointer to address to free
52  *
53  */
54 static inline void
55 mlx5_os_free(void *addr)
56 {
57 	_aligned_free(addr);
58 }
59 
60 /**
61  * Get fd. Given a pointer to DevX channel object of type
62  * 'struct mlx5dv_devx_event_channel*' - return its fd.
63  * Under Windows it is a stub.
64  *
65  * @param[in] channel
66  *    Pointer to channel object.
67  *
68  * @return
69  *    0
70  */
71 static inline int
72 mlx5_os_get_devx_channel_fd(void *channel)
73 {
74 	if (!channel)
75 		return 0;
76 	return 0;
77 }
78 
79 /**
80  * Get device name. Given a device pointer - return a
81  * pointer to the corresponding device name.
82  *
83  * @param[in] dev
84  *   Pointer to device.
85  *
86  * @return
87  *   Pointer to device name if dev is valid, NULL otherwise.
88  */
89 static inline const char *
90 mlx5_os_get_dev_device_name(void *dev)
91 {
92 	if (!dev)
93 		return NULL;
94 	return ((struct devx_device *)dev)->name;
95 }
96 
97 /**
98  * Get device name. Given a context pointer - return a
99  * pointer to the corresponding device name.
100  *
101  * @param[in] ctx
102  *   Pointer to context.
103  *
104  * @return
105  *   Pointer to device name if ctx is valid, NULL otherwise.
106  */
107 static inline const char *
108 mlx5_os_get_ctx_device_name(void *ctx)
109 {
110 	if (!ctx)
111 		return NULL;
112 	return ((mlx5_context_st *)ctx)->mlx5_dev.name;
113 }
114 
115 /**
116  * Get a device path name. Given acontext pointer - return a
117  * pointer to the corresponding device path name.
118  *
119  * @param[in] ctx
120  *   Pointer to context.
121  *
122  * @return
123  *   Pointer to device path name if ctx is valid, NULL otherwise.
124  */
125 
126 static inline const char *
127 mlx5_os_get_ctx_device_path(void *ctx)
128 {
129 	if (!ctx)
130 		return NULL;
131 	return ((mlx5_context_st *)ctx)->mlx5_dev.dev_pnp_id;
132 }
133 
134 /**
135  * Get umem id. Given a pointer to umem object of type return its id.
136  *
137  * @param[in] umem
138  *    Pointer to umem object.
139  *
140  * @return
141  *    The umem id if umem is valid, 0 otherwise.
142  */
143 static inline uint32_t
144 mlx5_os_get_umem_id(void *umem)
145 {
146 	if (!umem)
147 		return 0;
148 	return ((struct mlx5_devx_umem *)umem)->umem_id;
149 }
150 
151 /**
152  * Get mmap offset. Given a pointer to an DevX UAR object of type
153  * 'struct mlx5dv_devx_uar *' - return its mmap offset.
154  * In Windows, mmap_offset is unused.
155  *
156  * @param[in] uar
157  *    Pointer to UAR object.
158  *
159  * @return
160  *    0 as mmap_offset is unused
161  */
162 static inline off_t
163 mlx5_os_get_devx_uar_mmap_offset(void *uar)
164 {
165 	RTE_SET_USED(uar);
166 	return 0;
167 }
168 
169 /**
170  * Get base addr pointer. Given a pointer to an UAR object of type
171  * 'struct mlx5dv_devx_uar *' - return its base address.
172  *
173  * @param[in] uar
174  *    Pointer to an UAR object.
175  *
176  * @return
177  *    The base address if UAR is valid, NULL otherwise.
178  */
179 static inline void *
180 mlx5_os_get_devx_uar_base_addr(void *uar)
181 {
182 	if (!uar)
183 		return NULL;
184 	return ((devx_uar_handle *)uar)->uar_page;
185 }
186 
187 /**
188  * Get reg addr pointer. Given a pointer to an UAR object of type
189  * 'struct mlx5dv_devx_uar *' - return its reg address.
190  *
191  * @param[in] uar
192  *    Pointer to an UAR object.
193  *
194  * @return
195  *    The reg address if UAR is valid, NULL otherwise.
196  */
197 static inline void *
198 mlx5_os_get_devx_uar_reg_addr(void *uar)
199 {
200 	if (!uar)
201 		return NULL;
202 	return ((char *)((devx_uar_handle *)uar)->uar_page) + MLX5_BF_OFFSET;
203 }
204 
205 /**
206  * Get page id. Given a pointer to an UAR object of type
207  * 'struct mlx5dv_devx_uar *' - return its page id.
208  *
209  * @param[in] uar
210  *    Pointer to an UAR object.
211  *
212  * @return
213  *    The page id if UAR is valid, 0 otherwise.
214  */
215 static inline uint32_t
216 mlx5_os_get_devx_uar_page_id(void *uar)
217 {
218 	if (!uar)
219 		return 0;
220 	return ((devx_uar_handle *)uar)->uar_index;
221 }
222 
223 static inline void *
224 mlx5_os_devx_create_event_channel(void *ctx, int flags)
225 {
226 	(void)ctx;
227 	(void)flags;
228 	errno = ENOTSUP;
229 	return NULL;
230 }
231 
232 static inline void
233 mlx5_os_devx_destroy_event_channel(void *eventc)
234 {
235 	(void)eventc;
236 }
237 
238 static inline int
239 mlx5_os_devx_subscribe_devx_event(void *eventc,
240 				    void *obj,
241 				    uint16_t events_sz, uint16_t events_num[],
242 				    uint64_t cookie)
243 {
244 	(void)eventc;
245 	(void)obj;
246 	(void)events_sz;
247 	(void)events_num;
248 	(void)cookie;
249 	return -ENOTSUP;
250 }
251 
252 __rte_internal
253 void *mlx5_os_umem_reg(void *ctx, void *addr, size_t size, uint32_t access);
254 __rte_internal
255 int mlx5_os_umem_dereg(void *pumem);
256 
257 static inline struct rte_intr_handle *
258 mlx5_os_interrupt_handler_create(int mode, bool set_fd_nonblock, int fd,
259 				 rte_intr_callback_fn cb, void *cb_arg)
260 {
261 	(void)mode;
262 	(void)set_fd_nonblock;
263 	(void)fd;
264 	(void)cb;
265 	(void)cb_arg;
266 	rte_errno = ENOTSUP;
267 	return NULL;
268 }
269 
270 static inline void
271 mlx5_os_interrupt_handler_destroy(struct rte_intr_handle *intr_handle,
272 				  rte_intr_callback_fn cb, void *cb_arg)
273 {
274 	(void)intr_handle;
275 	(void)cb;
276 	(void)cb_arg;
277 }
278 
279 
280 #endif /* RTE_PMD_MLX5_COMMON_OS_H_ */
281