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