xref: /dpdk/drivers/common/mlx5/linux/mlx5_common_os.h (revision 7e7af4e99a9d34e3d8510fe9c57b683ea762cfcc)
1391b8bccSOphir Munk /* SPDX-License-Identifier: BSD-3-Clause
2391b8bccSOphir Munk  * Copyright 2020 Mellanox Technologies, Ltd
3391b8bccSOphir Munk  */
4391b8bccSOphir Munk 
5391b8bccSOphir Munk #ifndef RTE_PMD_MLX5_COMMON_OS_H_
6391b8bccSOphir Munk #define RTE_PMD_MLX5_COMMON_OS_H_
7391b8bccSOphir Munk 
8391b8bccSOphir Munk #include <stdio.h>
9*7e7af4e9SOphir Munk #include <malloc.h>
10391b8bccSOphir Munk 
11391b8bccSOphir Munk #include <rte_pci.h>
12391b8bccSOphir Munk #include <rte_debug.h>
13391b8bccSOphir Munk #include <rte_atomic.h>
14391b8bccSOphir Munk #include <rte_log.h>
15391b8bccSOphir Munk #include <rte_kvargs.h>
16391b8bccSOphir Munk #include <rte_devargs.h>
17391b8bccSOphir Munk 
18391b8bccSOphir Munk #include "mlx5_autoconf.h"
199d60f545SOphir Munk #include "mlx5_glue.h"
20*7e7af4e9SOphir Munk #include "mlx5_malloc.h"
21391b8bccSOphir Munk 
22391b8bccSOphir Munk /**
23391b8bccSOphir Munk  * Get device name. Given an ibv_device pointer - return a
24391b8bccSOphir Munk  * pointer to the corresponding device name.
25391b8bccSOphir Munk  *
26391b8bccSOphir Munk  * @param[in] dev
27391b8bccSOphir Munk  *   Pointer to ibv device.
28391b8bccSOphir Munk  *
29391b8bccSOphir Munk  * @return
30391b8bccSOphir Munk  *   Pointer to device name if dev is valid, NULL otherwise.
31391b8bccSOphir Munk  */
32391b8bccSOphir Munk static inline const char *
33391b8bccSOphir Munk mlx5_os_get_dev_device_name(void *dev)
34391b8bccSOphir Munk {
35391b8bccSOphir Munk 	if (!dev)
36391b8bccSOphir Munk 		return NULL;
37391b8bccSOphir Munk 	return ((struct ibv_device *)dev)->name;
38391b8bccSOphir Munk }
39391b8bccSOphir Munk 
40391b8bccSOphir Munk /**
41391b8bccSOphir Munk  * Get ibv device name. Given an ibv_context pointer - return a
42391b8bccSOphir Munk  * pointer to the corresponding device name.
43391b8bccSOphir Munk  *
44391b8bccSOphir Munk  * @param[in] ctx
45391b8bccSOphir Munk  *   Pointer to ibv context.
46391b8bccSOphir Munk  *
47391b8bccSOphir Munk  * @return
48391b8bccSOphir Munk  *   Pointer to device name if ctx is valid, NULL otherwise.
49391b8bccSOphir Munk  */
50391b8bccSOphir Munk static inline const char *
51391b8bccSOphir Munk mlx5_os_get_ctx_device_name(void *ctx)
52391b8bccSOphir Munk {
53391b8bccSOphir Munk 	if (!ctx)
54391b8bccSOphir Munk 		return NULL;
55391b8bccSOphir Munk 	return ((struct ibv_context *)ctx)->device->name;
56391b8bccSOphir Munk }
57391b8bccSOphir Munk 
58391b8bccSOphir Munk /**
59391b8bccSOphir Munk  * Get ibv device path name. Given an ibv_context pointer - return a
60391b8bccSOphir Munk  * pointer to the corresponding device path name.
61391b8bccSOphir Munk  *
62391b8bccSOphir Munk  * @param[in] ctx
63391b8bccSOphir Munk  *   Pointer to ibv context.
64391b8bccSOphir Munk  *
65391b8bccSOphir Munk  * @return
66391b8bccSOphir Munk  *   Pointer to device path name if ctx is valid, NULL otherwise.
67391b8bccSOphir Munk  */
68391b8bccSOphir Munk 
69391b8bccSOphir Munk static inline const char *
70391b8bccSOphir Munk mlx5_os_get_ctx_device_path(void *ctx)
71391b8bccSOphir Munk {
72391b8bccSOphir Munk 	if (!ctx)
73391b8bccSOphir Munk 		return NULL;
74391b8bccSOphir Munk 
75391b8bccSOphir Munk 	return ((struct ibv_context *)ctx)->device->ibdev_path;
76391b8bccSOphir Munk }
77391b8bccSOphir Munk 
78391b8bccSOphir Munk /**
79391b8bccSOphir Munk  * Get umem id. Given a pointer to umem object of type
80391b8bccSOphir Munk  * 'struct mlx5dv_devx_umem *' - return its id.
81391b8bccSOphir Munk  *
82391b8bccSOphir Munk  * @param[in] umem
83391b8bccSOphir Munk  *    Pointer to umem object.
84391b8bccSOphir Munk  *
85391b8bccSOphir Munk  * @return
86391b8bccSOphir Munk  *    The umem id if umem is valid, 0 otherwise.
87391b8bccSOphir Munk  */
88391b8bccSOphir Munk static inline uint32_t
89391b8bccSOphir Munk mlx5_os_get_umem_id(void *umem)
90391b8bccSOphir Munk {
91391b8bccSOphir Munk 	if (!umem)
92391b8bccSOphir Munk 		return 0;
93391b8bccSOphir Munk 	return ((struct mlx5dv_devx_umem *)umem)->umem_id;
94391b8bccSOphir Munk }
951f66ac5bSOphir Munk 
961f66ac5bSOphir Munk /**
971f66ac5bSOphir Munk  * Get fd. Given a pointer to DevX channel object of type
981f66ac5bSOphir Munk  * 'struct mlx5dv_devx_event_channel*' - return its fd.
991f66ac5bSOphir Munk  *
1001f66ac5bSOphir Munk  * @param[in] channel
1011f66ac5bSOphir Munk  *    Pointer to channel object.
1021f66ac5bSOphir Munk  *
1031f66ac5bSOphir Munk  * @return
1041f66ac5bSOphir Munk  *    The fd if channel is valid, 0 otherwise.
1051f66ac5bSOphir Munk  */
1061f66ac5bSOphir Munk static inline int
1071f66ac5bSOphir Munk mlx5_os_get_devx_channel_fd(void *channel)
1081f66ac5bSOphir Munk {
1091f66ac5bSOphir Munk 	if (!channel)
1101f66ac5bSOphir Munk 		return 0;
1111f66ac5bSOphir Munk 	return ((struct mlx5dv_devx_event_channel *)channel)->fd;
1121f66ac5bSOphir Munk }
1131f66ac5bSOphir Munk 
1141f66ac5bSOphir Munk /**
1151f66ac5bSOphir Munk  * Get mmap offset. Given a pointer to an DevX UAR object of type
1161f66ac5bSOphir Munk  * 'struct mlx5dv_devx_uar *' - return its mmap offset.
1171f66ac5bSOphir Munk  *
1181f66ac5bSOphir Munk  * @param[in] uar
1191f66ac5bSOphir Munk  *    Pointer to UAR object.
1201f66ac5bSOphir Munk  *
1211f66ac5bSOphir Munk  * @return
1221f66ac5bSOphir Munk  *    The mmap offset if uar is valid, 0 otherwise.
1231f66ac5bSOphir Munk  */
1241f66ac5bSOphir Munk static inline off_t
1251f66ac5bSOphir Munk mlx5_os_get_devx_uar_mmap_offset(void *uar)
1261f66ac5bSOphir Munk {
1271f66ac5bSOphir Munk #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
1281f66ac5bSOphir Munk 	if (!uar)
1291f66ac5bSOphir Munk 		return 0;
1301f66ac5bSOphir Munk 	return ((struct mlx5dv_devx_uar *)uar)->mmap_off;
1311f66ac5bSOphir Munk #else
1321f66ac5bSOphir Munk 	RTE_SET_USED(uar);
1331f66ac5bSOphir Munk 	return 0;
1341f66ac5bSOphir Munk #endif
1351f66ac5bSOphir Munk }
1361f66ac5bSOphir Munk 
1371f66ac5bSOphir Munk /**
1381f66ac5bSOphir Munk  * Get base addr pointer. Given a pointer to an UAR object of type
1391f66ac5bSOphir Munk  * 'struct mlx5dv_devx_uar *' - return its base address.
1401f66ac5bSOphir Munk  *
1411f66ac5bSOphir Munk  * @param[in] uar
1421f66ac5bSOphir Munk  *    Pointer to an UAR object.
1431f66ac5bSOphir Munk  *
1441f66ac5bSOphir Munk  * @return
1451f66ac5bSOphir Munk  *    The base address if UAR is valid, 0 otherwise.
1461f66ac5bSOphir Munk  */
1471f66ac5bSOphir Munk static inline void *
1481f66ac5bSOphir Munk mlx5_os_get_devx_uar_base_addr(void *uar)
1491f66ac5bSOphir Munk {
1501f66ac5bSOphir Munk #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
1511f66ac5bSOphir Munk 	if (!uar)
1521f66ac5bSOphir Munk 		return NULL;
1531f66ac5bSOphir Munk 	return ((struct mlx5dv_devx_uar *)uar)->base_addr;
1541f66ac5bSOphir Munk #else
1551f66ac5bSOphir Munk 	RTE_SET_USED(uar);
1561f66ac5bSOphir Munk 	return NULL;
1571f66ac5bSOphir Munk #endif
1581f66ac5bSOphir Munk }
1591f66ac5bSOphir Munk 
1601f66ac5bSOphir Munk /**
1611f66ac5bSOphir Munk  * Get reg addr pointer. Given a pointer to an UAR object of type
1621f66ac5bSOphir Munk  * 'struct mlx5dv_devx_uar *' - return its reg address.
1631f66ac5bSOphir Munk  *
1641f66ac5bSOphir Munk  * @param[in] uar
1651f66ac5bSOphir Munk  *    Pointer to an UAR object.
1661f66ac5bSOphir Munk  *
1671f66ac5bSOphir Munk  * @return
1681f66ac5bSOphir Munk  *    The reg address if UAR is valid, 0 otherwise.
1691f66ac5bSOphir Munk  */
1701f66ac5bSOphir Munk static inline void *
1711f66ac5bSOphir Munk mlx5_os_get_devx_uar_reg_addr(void *uar)
1721f66ac5bSOphir Munk {
1731f66ac5bSOphir Munk #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
1741f66ac5bSOphir Munk 	if (!uar)
1751f66ac5bSOphir Munk 		return NULL;
1761f66ac5bSOphir Munk 	return ((struct mlx5dv_devx_uar *)uar)->reg_addr;
1771f66ac5bSOphir Munk #else
1781f66ac5bSOphir Munk 	RTE_SET_USED(uar);
1791f66ac5bSOphir Munk 	return NULL;
1801f66ac5bSOphir Munk #endif
1811f66ac5bSOphir Munk }
1821f66ac5bSOphir Munk 
1831f66ac5bSOphir Munk /**
1841f66ac5bSOphir Munk  * Get page id. Given a pointer to an UAR object of type
1851f66ac5bSOphir Munk  * 'struct mlx5dv_devx_uar *' - return its page id.
1861f66ac5bSOphir Munk  *
1871f66ac5bSOphir Munk  * @param[in] uar
1881f66ac5bSOphir Munk  *    Pointer to an UAR object.
1891f66ac5bSOphir Munk  *
1901f66ac5bSOphir Munk  * @return
1911f66ac5bSOphir Munk  *    The page id if UAR is valid, 0 otherwise.
1921f66ac5bSOphir Munk  */
1931f66ac5bSOphir Munk static inline uint32_t
1941f66ac5bSOphir Munk mlx5_os_get_devx_uar_page_id(void *uar)
1951f66ac5bSOphir Munk {
1961f66ac5bSOphir Munk #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
1971f66ac5bSOphir Munk 	if (!uar)
1981f66ac5bSOphir Munk 		return 0;
1991f66ac5bSOphir Munk 	return ((struct mlx5dv_devx_uar *)uar)->page_id;
2001f66ac5bSOphir Munk #else
2011f66ac5bSOphir Munk 	RTE_SET_USED(uar);
2021f66ac5bSOphir Munk 	return 0;
2031f66ac5bSOphir Munk #endif
2041f66ac5bSOphir Munk }
2051f66ac5bSOphir Munk 
2061cb210abSOphir Munk static inline void *
2071cb210abSOphir Munk mlx5_os_alloc_pd(void *ctx)
2081cb210abSOphir Munk {
2091cb210abSOphir Munk 	return mlx5_glue->alloc_pd(ctx);
2101cb210abSOphir Munk }
2111cb210abSOphir Munk 
2121cb210abSOphir Munk static inline int
2131cb210abSOphir Munk mlx5_os_dealloc_pd(void *pd)
2141cb210abSOphir Munk {
2151cb210abSOphir Munk 	return mlx5_glue->dealloc_pd(pd);
2161cb210abSOphir Munk }
21707a99de8STal Shnaiderman 
21807a99de8STal Shnaiderman static inline void *
21907a99de8STal Shnaiderman mlx5_os_umem_reg(void *ctx, void *addr, size_t size, uint32_t access)
22007a99de8STal Shnaiderman {
22107a99de8STal Shnaiderman 	return mlx5_glue->devx_umem_reg(ctx, addr, size, access);
22207a99de8STal Shnaiderman }
22307a99de8STal Shnaiderman 
22407a99de8STal Shnaiderman static inline int
22507a99de8STal Shnaiderman mlx5_os_umem_dereg(void *pumem)
22607a99de8STal Shnaiderman {
22707a99de8STal Shnaiderman 	return mlx5_glue->devx_umem_dereg(pumem);
22807a99de8STal Shnaiderman }
229*7e7af4e9SOphir Munk 
230*7e7af4e9SOphir Munk /**
231*7e7af4e9SOphir Munk  * Memory allocation optionally with alignment.
232*7e7af4e9SOphir Munk  *
233*7e7af4e9SOphir Munk  * @param[in] align
234*7e7af4e9SOphir Munk  *    Alignment size (may be zero)
235*7e7af4e9SOphir Munk  * @param[in] size
236*7e7af4e9SOphir Munk  *    Size in bytes to allocate
237*7e7af4e9SOphir Munk  *
238*7e7af4e9SOphir Munk  * @return
239*7e7af4e9SOphir Munk  *    Valid pointer to allocated memory, NULL in case of failure
240*7e7af4e9SOphir Munk  */
241*7e7af4e9SOphir Munk static inline void *
242*7e7af4e9SOphir Munk mlx5_os_malloc(size_t align, size_t size)
243*7e7af4e9SOphir Munk {
244*7e7af4e9SOphir Munk 	void *buf;
245*7e7af4e9SOphir Munk 
246*7e7af4e9SOphir Munk 	if (posix_memalign(&buf, align, size))
247*7e7af4e9SOphir Munk 		return NULL;
248*7e7af4e9SOphir Munk 	return buf;
249*7e7af4e9SOphir Munk }
250*7e7af4e9SOphir Munk 
251*7e7af4e9SOphir Munk /**
252*7e7af4e9SOphir Munk  * This API de-allocates a memory that originally could have been
253*7e7af4e9SOphir Munk  * allocated aligned or non-aligned. In Linux it is a wrapper
254*7e7af4e9SOphir Munk  * around free().
255*7e7af4e9SOphir Munk  *
256*7e7af4e9SOphir Munk  * @param[in] addr
257*7e7af4e9SOphir Munk  *    Pointer to address to free
258*7e7af4e9SOphir Munk  *
259*7e7af4e9SOphir Munk  */
260*7e7af4e9SOphir Munk static inline void
261*7e7af4e9SOphir Munk mlx5_os_free(void *addr)
262*7e7af4e9SOphir Munk {
263*7e7af4e9SOphir Munk 	free(addr);
264*7e7af4e9SOphir Munk }
265391b8bccSOphir Munk #endif /* RTE_PMD_MLX5_COMMON_OS_H_ */
266