xref: /dpdk/drivers/common/mlx5/windows/mlx5_common_os.h (revision 1094dd940ec0cc4e3ce2c5cd94807350855a17f9)
19b7d7440SOphir Munk /* SPDX-License-Identifier: BSD-3-Clause
29b7d7440SOphir Munk  * Copyright 2020 Mellanox Technologies, Ltd
39b7d7440SOphir Munk  */
49b7d7440SOphir Munk 
59b7d7440SOphir Munk #ifndef RTE_PMD_MLX5_COMMON_OS_H_
69b7d7440SOphir Munk #define RTE_PMD_MLX5_COMMON_OS_H_
79b7d7440SOphir Munk 
89b7d7440SOphir Munk #include <stdio.h>
9d02ef361SOphir Munk #include <sys/types.h>
109b7d7440SOphir Munk 
11*1094dd94SDavid Marchand #include <rte_compat.h>
12ba420719SOphir Munk #include <rte_errno.h>
1372d7efe4SSpike Du #include <rte_interrupts.h>
14ba420719SOphir Munk 
159b7d7440SOphir Munk #include "mlx5_autoconf.h"
169b7d7440SOphir Munk #include "mlx5_glue.h"
179b7d7440SOphir Munk #include "mlx5_malloc.h"
18ba420719SOphir Munk #include "mlx5_common_mr.h"
19ba420719SOphir Munk #include "mlx5_win_ext.h"
209b7d7440SOphir Munk 
21d02ef361SOphir Munk #define MLX5_BF_OFFSET 0x800
22d02ef361SOphir Munk 
239b7d7440SOphir Munk /**
249b7d7440SOphir Munk  * This API allocates aligned or non-aligned memory.  The free can be on either
259b7d7440SOphir Munk  * aligned or nonaligned memory.  To be protected - even though there may be no
267be78d02SJosh Soref  * alignment - in Windows this API will unconditionally call _aligned_malloc()
279b7d7440SOphir Munk  * with at least a minimal alignment size.
289b7d7440SOphir Munk  *
299b7d7440SOphir Munk  * @param[in] align
309b7d7440SOphir Munk  *    The alignment value, which must be an integer power of 2 (or 0 for
319b7d7440SOphir Munk  *    non-alignment)
329b7d7440SOphir Munk  * @param[in] size
339b7d7440SOphir Munk  *    Size in bytes to allocate
349b7d7440SOphir Munk  *
359b7d7440SOphir Munk  * @return
369b7d7440SOphir Munk  *    Valid pointer to allocated memory, NULL in case of failure
379b7d7440SOphir Munk  */
389b7d7440SOphir Munk static inline void *
mlx5_os_malloc(size_t align,size_t size)399b7d7440SOphir Munk mlx5_os_malloc(size_t align, size_t size)
409b7d7440SOphir Munk {
419b7d7440SOphir Munk 	if (align < MLX5_MALLOC_ALIGNMENT)
429b7d7440SOphir Munk 		align = MLX5_MALLOC_ALIGNMENT;
439b7d7440SOphir Munk 	return _aligned_malloc(size, align);
449b7d7440SOphir Munk }
459b7d7440SOphir Munk 
469b7d7440SOphir Munk /**
479b7d7440SOphir Munk  * This API de-allocates a memory that originally could have been allocated
489b7d7440SOphir Munk  * aligned or non-aligned. In Windows since the allocation was with
499b7d7440SOphir Munk  * _aligned_malloc() - it is safe to always call _aligned_free().
509b7d7440SOphir Munk  *
519b7d7440SOphir Munk  * @param[in] addr
529b7d7440SOphir Munk  *    Pointer to address to free
539b7d7440SOphir Munk  *
549b7d7440SOphir Munk  */
559b7d7440SOphir Munk static inline void
mlx5_os_free(void * addr)569b7d7440SOphir Munk mlx5_os_free(void *addr)
579b7d7440SOphir Munk {
589b7d7440SOphir Munk 	_aligned_free(addr);
599b7d7440SOphir Munk }
6086576a64SOphir Munk 
6186576a64SOphir Munk /**
6286576a64SOphir Munk  * Get fd. Given a pointer to DevX channel object of type
6386576a64SOphir Munk  * 'struct mlx5dv_devx_event_channel*' - return its fd.
6486576a64SOphir Munk  * Under Windows it is a stub.
6586576a64SOphir Munk  *
6686576a64SOphir Munk  * @param[in] channel
6786576a64SOphir Munk  *    Pointer to channel object.
6886576a64SOphir Munk  *
6986576a64SOphir Munk  * @return
7086576a64SOphir Munk  *    0
7186576a64SOphir Munk  */
7286576a64SOphir Munk static inline int
mlx5_os_get_devx_channel_fd(void * channel)7386576a64SOphir Munk mlx5_os_get_devx_channel_fd(void *channel)
7486576a64SOphir Munk {
7586576a64SOphir Munk 	if (!channel)
7686576a64SOphir Munk 		return 0;
7786576a64SOphir Munk 	return 0;
7886576a64SOphir Munk }
7986576a64SOphir Munk 
8086576a64SOphir Munk /**
8186576a64SOphir Munk  * Get device name. Given a device pointer - return a
8286576a64SOphir Munk  * pointer to the corresponding device name.
8386576a64SOphir Munk  *
8486576a64SOphir Munk  * @param[in] dev
8586576a64SOphir Munk  *   Pointer to device.
8686576a64SOphir Munk  *
8786576a64SOphir Munk  * @return
8886576a64SOphir Munk  *   Pointer to device name if dev is valid, NULL otherwise.
8986576a64SOphir Munk  */
9086576a64SOphir Munk static inline const char *
mlx5_os_get_dev_device_name(void * dev)9186576a64SOphir Munk mlx5_os_get_dev_device_name(void *dev)
9286576a64SOphir Munk {
9386576a64SOphir Munk 	if (!dev)
9486576a64SOphir Munk 		return NULL;
9586576a64SOphir Munk 	return ((struct devx_device *)dev)->name;
9686576a64SOphir Munk }
9786576a64SOphir Munk 
9886576a64SOphir Munk /**
9986576a64SOphir Munk  * Get device name. Given a context pointer - return a
10086576a64SOphir Munk  * pointer to the corresponding device name.
10186576a64SOphir Munk  *
10286576a64SOphir Munk  * @param[in] ctx
10386576a64SOphir Munk  *   Pointer to context.
10486576a64SOphir Munk  *
10586576a64SOphir Munk  * @return
10686576a64SOphir Munk  *   Pointer to device name if ctx is valid, NULL otherwise.
10786576a64SOphir Munk  */
10886576a64SOphir Munk static inline const char *
mlx5_os_get_ctx_device_name(void * ctx)10986576a64SOphir Munk mlx5_os_get_ctx_device_name(void *ctx)
11086576a64SOphir Munk {
11186576a64SOphir Munk 	if (!ctx)
11286576a64SOphir Munk 		return NULL;
11386576a64SOphir Munk 	return ((mlx5_context_st *)ctx)->mlx5_dev.name;
11486576a64SOphir Munk }
11586576a64SOphir Munk 
11686576a64SOphir Munk /**
11786576a64SOphir Munk  * Get a device path name. Given acontext pointer - return a
11886576a64SOphir Munk  * pointer to the corresponding device path name.
11986576a64SOphir Munk  *
12086576a64SOphir Munk  * @param[in] ctx
12186576a64SOphir Munk  *   Pointer to context.
12286576a64SOphir Munk  *
12386576a64SOphir Munk  * @return
12486576a64SOphir Munk  *   Pointer to device path name if ctx is valid, NULL otherwise.
12586576a64SOphir Munk  */
12686576a64SOphir Munk 
12786576a64SOphir Munk static inline const char *
mlx5_os_get_ctx_device_path(void * ctx)12886576a64SOphir Munk mlx5_os_get_ctx_device_path(void *ctx)
12986576a64SOphir Munk {
13086576a64SOphir Munk 	if (!ctx)
13186576a64SOphir Munk 		return NULL;
13286576a64SOphir Munk 	return ((mlx5_context_st *)ctx)->mlx5_dev.dev_pnp_id;
13386576a64SOphir Munk }
13486576a64SOphir Munk 
13586576a64SOphir Munk /**
13686576a64SOphir Munk  * Get umem id. Given a pointer to umem object of type return its id.
13786576a64SOphir Munk  *
13886576a64SOphir Munk  * @param[in] umem
13986576a64SOphir Munk  *    Pointer to umem object.
14086576a64SOphir Munk  *
14186576a64SOphir Munk  * @return
14286576a64SOphir Munk  *    The umem id if umem is valid, 0 otherwise.
14386576a64SOphir Munk  */
14486576a64SOphir Munk static inline uint32_t
mlx5_os_get_umem_id(void * umem)14586576a64SOphir Munk mlx5_os_get_umem_id(void *umem)
14686576a64SOphir Munk {
14786576a64SOphir Munk 	if (!umem)
14886576a64SOphir Munk 		return 0;
14986576a64SOphir Munk 	return ((struct mlx5_devx_umem *)umem)->umem_id;
15086576a64SOphir Munk }
1511552fb28STal Shnaiderman 
152d02ef361SOphir Munk /**
153d02ef361SOphir Munk  * Get mmap offset. Given a pointer to an DevX UAR object of type
154d02ef361SOphir Munk  * 'struct mlx5dv_devx_uar *' - return its mmap offset.
155d02ef361SOphir Munk  * In Windows, mmap_offset is unused.
156d02ef361SOphir Munk  *
157d02ef361SOphir Munk  * @param[in] uar
158d02ef361SOphir Munk  *    Pointer to UAR object.
159d02ef361SOphir Munk  *
160d02ef361SOphir Munk  * @return
161d02ef361SOphir Munk  *    0 as mmap_offset is unused
162d02ef361SOphir Munk  */
163d02ef361SOphir Munk static inline off_t
mlx5_os_get_devx_uar_mmap_offset(void * uar)164d02ef361SOphir Munk mlx5_os_get_devx_uar_mmap_offset(void *uar)
165d02ef361SOphir Munk {
166d02ef361SOphir Munk 	RTE_SET_USED(uar);
167d02ef361SOphir Munk 	return 0;
168d02ef361SOphir Munk }
169d02ef361SOphir Munk 
170d02ef361SOphir Munk /**
171d02ef361SOphir Munk  * Get base addr pointer. Given a pointer to an UAR object of type
172d02ef361SOphir Munk  * 'struct mlx5dv_devx_uar *' - return its base address.
173d02ef361SOphir Munk  *
174d02ef361SOphir Munk  * @param[in] uar
175d02ef361SOphir Munk  *    Pointer to an UAR object.
176d02ef361SOphir Munk  *
177d02ef361SOphir Munk  * @return
178d02ef361SOphir Munk  *    The base address if UAR is valid, NULL otherwise.
179d02ef361SOphir Munk  */
180d02ef361SOphir Munk static inline void *
mlx5_os_get_devx_uar_base_addr(void * uar)181d02ef361SOphir Munk mlx5_os_get_devx_uar_base_addr(void *uar)
182d02ef361SOphir Munk {
183d02ef361SOphir Munk 	if (!uar)
184d02ef361SOphir Munk 		return NULL;
185d02ef361SOphir Munk 	return ((devx_uar_handle *)uar)->uar_page;
186d02ef361SOphir Munk }
187d02ef361SOphir Munk 
188d02ef361SOphir Munk /**
189d02ef361SOphir Munk  * Get reg addr pointer. Given a pointer to an UAR object of type
190d02ef361SOphir Munk  * 'struct mlx5dv_devx_uar *' - return its reg address.
191d02ef361SOphir Munk  *
192d02ef361SOphir Munk  * @param[in] uar
193d02ef361SOphir Munk  *    Pointer to an UAR object.
194d02ef361SOphir Munk  *
195d02ef361SOphir Munk  * @return
196d02ef361SOphir Munk  *    The reg address if UAR is valid, NULL otherwise.
197d02ef361SOphir Munk  */
198d02ef361SOphir Munk static inline void *
mlx5_os_get_devx_uar_reg_addr(void * uar)199d02ef361SOphir Munk mlx5_os_get_devx_uar_reg_addr(void *uar)
200d02ef361SOphir Munk {
201d02ef361SOphir Munk 	if (!uar)
202d02ef361SOphir Munk 		return NULL;
203d02ef361SOphir Munk 	return ((char *)((devx_uar_handle *)uar)->uar_page) + MLX5_BF_OFFSET;
204d02ef361SOphir Munk }
205d02ef361SOphir Munk 
206d02ef361SOphir Munk /**
207d02ef361SOphir Munk  * Get page id. Given a pointer to an UAR object of type
208d02ef361SOphir Munk  * 'struct mlx5dv_devx_uar *' - return its page id.
209d02ef361SOphir Munk  *
210d02ef361SOphir Munk  * @param[in] uar
211d02ef361SOphir Munk  *    Pointer to an UAR object.
212d02ef361SOphir Munk  *
213d02ef361SOphir Munk  * @return
214d02ef361SOphir Munk  *    The page id if UAR is valid, 0 otherwise.
215d02ef361SOphir Munk  */
216d02ef361SOphir Munk static inline uint32_t
mlx5_os_get_devx_uar_page_id(void * uar)217d02ef361SOphir Munk mlx5_os_get_devx_uar_page_id(void *uar)
218d02ef361SOphir Munk {
219d02ef361SOphir Munk 	if (!uar)
220d02ef361SOphir Munk 		return 0;
221d02ef361SOphir Munk 	return ((devx_uar_handle *)uar)->uar_index;
222d02ef361SOphir Munk }
223d02ef361SOphir Munk 
2241e78f26dSTal Shnaiderman static inline void *
mlx5_os_devx_create_event_channel(void * ctx,int flags)2251e78f26dSTal Shnaiderman mlx5_os_devx_create_event_channel(void *ctx, int flags)
2261e78f26dSTal Shnaiderman {
2271e78f26dSTal Shnaiderman 	(void)ctx;
2281e78f26dSTal Shnaiderman 	(void)flags;
2291e78f26dSTal Shnaiderman 	errno = ENOTSUP;
2301e78f26dSTal Shnaiderman 	return NULL;
2311e78f26dSTal Shnaiderman }
2321e78f26dSTal Shnaiderman 
2331e78f26dSTal Shnaiderman static inline void
mlx5_os_devx_destroy_event_channel(void * eventc)2341e78f26dSTal Shnaiderman mlx5_os_devx_destroy_event_channel(void *eventc)
2351e78f26dSTal Shnaiderman {
2361e78f26dSTal Shnaiderman 	(void)eventc;
2371e78f26dSTal Shnaiderman }
2381e78f26dSTal Shnaiderman 
2391e78f26dSTal Shnaiderman static inline int
mlx5_os_devx_subscribe_devx_event(void * eventc,void * obj,uint16_t events_sz,uint16_t events_num[],uint64_t cookie)2401e78f26dSTal Shnaiderman mlx5_os_devx_subscribe_devx_event(void *eventc,
2411e78f26dSTal Shnaiderman 				    void *obj,
2421e78f26dSTal Shnaiderman 				    uint16_t events_sz, uint16_t events_num[],
2431e78f26dSTal Shnaiderman 				    uint64_t cookie)
2441e78f26dSTal Shnaiderman {
2451e78f26dSTal Shnaiderman 	(void)eventc;
2461e78f26dSTal Shnaiderman 	(void)obj;
2471e78f26dSTal Shnaiderman 	(void)events_sz;
2481e78f26dSTal Shnaiderman 	(void)events_num;
2491e78f26dSTal Shnaiderman 	(void)cookie;
2501e78f26dSTal Shnaiderman 	return -ENOTSUP;
2511e78f26dSTal Shnaiderman }
2521e78f26dSTal Shnaiderman 
2538ec36fb1STal Shnaiderman __rte_internal
2541969ee42STal Shnaiderman void *mlx5_os_umem_reg(void *ctx, void *addr, size_t size, uint32_t access);
2558ec36fb1STal Shnaiderman __rte_internal
2561969ee42STal Shnaiderman int mlx5_os_umem_dereg(void *pumem);
2575fbc75acSMichael Baum 
25872d7efe4SSpike Du static inline struct rte_intr_handle *
mlx5_os_interrupt_handler_create(int mode,bool set_fd_nonblock,int fd,rte_intr_callback_fn cb,void * cb_arg)25972d7efe4SSpike Du mlx5_os_interrupt_handler_create(int mode, bool set_fd_nonblock, int fd,
26072d7efe4SSpike Du 				 rte_intr_callback_fn cb, void *cb_arg)
26172d7efe4SSpike Du {
26272d7efe4SSpike Du 	(void)mode;
26372d7efe4SSpike Du 	(void)set_fd_nonblock;
26472d7efe4SSpike Du 	(void)fd;
26572d7efe4SSpike Du 	(void)cb;
26672d7efe4SSpike Du 	(void)cb_arg;
26772d7efe4SSpike Du 	rte_errno = ENOTSUP;
26872d7efe4SSpike Du 	return NULL;
26972d7efe4SSpike Du }
27072d7efe4SSpike Du 
27172d7efe4SSpike Du static inline void
mlx5_os_interrupt_handler_destroy(struct rte_intr_handle * intr_handle,rte_intr_callback_fn cb,void * cb_arg)27272d7efe4SSpike Du mlx5_os_interrupt_handler_destroy(struct rte_intr_handle *intr_handle,
27372d7efe4SSpike Du 				  rte_intr_callback_fn cb, void *cb_arg)
27472d7efe4SSpike Du {
27572d7efe4SSpike Du 	(void)intr_handle;
27672d7efe4SSpike Du 	(void)cb;
27772d7efe4SSpike Du 	(void)cb_arg;
27872d7efe4SSpike Du }
27972d7efe4SSpike Du 
28072d7efe4SSpike Du 
2819b7d7440SOphir Munk #endif /* RTE_PMD_MLX5_COMMON_OS_H_ */
282