1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
10 #include <rte_mempool.h>
11 #include <rte_malloc.h>
13 #include "mlx5_common.h"
14 #include "mlx5_common_os.h"
15 #include "mlx5_common_utils.h"
16 #include "mlx5_malloc.h"
18 int mlx5_common_logtype;
20 uint8_t haswell_broadwell_cpu;
23 mlx5_class_check_handler(__rte_unused const char *key, const char *value,
26 enum mlx5_class *ret = opaque;
28 if (strcmp(value, "vdpa") == 0) {
29 *ret = MLX5_CLASS_VDPA;
30 } else if (strcmp(value, "net") == 0) {
31 *ret = MLX5_CLASS_NET;
33 DRV_LOG(ERR, "Invalid mlx5 class %s. Maybe typo in device"
34 " class argument setting?", value);
35 *ret = MLX5_CLASS_INVALID;
41 mlx5_class_get(struct rte_devargs *devargs)
43 struct rte_kvargs *kvlist;
44 const char *key = MLX5_CLASS_ARG_NAME;
45 enum mlx5_class ret = MLX5_CLASS_NET;
49 kvlist = rte_kvargs_parse(devargs->args, NULL);
52 if (rte_kvargs_count(kvlist, key))
53 rte_kvargs_process(kvlist, key, mlx5_class_check_handler, &ret);
54 rte_kvargs_free(kvlist);
59 /* In case this is an x86_64 intel processor to check if
60 * we should use relaxed ordering.
62 #ifdef RTE_ARCH_X86_64
64 * This function returns processor identification and feature information
67 * @param eax, ebx, ecx, edx
68 * Pointers to the registers that will hold cpu information.
70 * The main category of information returned.
72 static inline void mlx5_cpu_id(unsigned int level,
73 unsigned int *eax, unsigned int *ebx,
74 unsigned int *ecx, unsigned int *edx)
77 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
82 RTE_INIT_PRIO(mlx5_log_init, LOG)
84 mlx5_common_logtype = rte_log_register("pmd.common.mlx5");
85 if (mlx5_common_logtype >= 0)
86 rte_log_set_level(mlx5_common_logtype, RTE_LOG_NOTICE);
89 static bool mlx5_common_initialized;
92 * One time innitialization routine for run-time dependency on glue library
93 * for multiple PMDs. Each mlx5 PMD that depends on mlx5_common module,
94 * must invoke in its constructor.
97 mlx5_common_init(void)
99 if (mlx5_common_initialized)
102 mlx5_glue_constructor();
103 mlx5_common_initialized = true;
107 * This function is responsible of initializing the variable
108 * haswell_broadwell_cpu by checking if the cpu is intel
109 * and reading the data returned from mlx5_cpu_id().
110 * since haswell and broadwell cpus don't have improved performance
111 * when using relaxed ordering we want to check the cpu type before
112 * before deciding whether to enable RO or not.
113 * if the cpu is haswell or broadwell the variable will be set to 1
114 * otherwise it will be 0.
116 RTE_INIT_PRIO(mlx5_is_haswell_broadwell_cpu, LOG)
118 #ifdef RTE_ARCH_X86_64
119 unsigned int broadwell_models[4] = {0x3d, 0x47, 0x4F, 0x56};
120 unsigned int haswell_models[4] = {0x3c, 0x3f, 0x45, 0x46};
121 unsigned int i, model, family, brand_id, vendor;
122 unsigned int signature_intel_ebx = 0x756e6547;
123 unsigned int extended_model;
124 unsigned int eax = 0;
125 unsigned int ebx = 0;
126 unsigned int ecx = 0;
127 unsigned int edx = 0;
130 mlx5_cpu_id(0, &eax, &ebx, &ecx, &edx);
134 haswell_broadwell_cpu = 0;
137 mlx5_cpu_id(1, &eax, &ebx, &ecx, &edx);
138 model = (eax >> 4) & 0x0f;
139 family = (eax >> 8) & 0x0f;
140 brand_id = ebx & 0xff;
141 extended_model = (eax >> 12) & 0xf0;
142 /* Check if the processor is Haswell or Broadwell */
143 if (vendor == signature_intel_ebx) {
145 model += extended_model;
146 if (brand_id == 0 && family == 0x6) {
147 for (i = 0; i < RTE_DIM(broadwell_models); i++)
148 if (model == broadwell_models[i]) {
149 haswell_broadwell_cpu = 1;
152 for (i = 0; i < RTE_DIM(haswell_models); i++)
153 if (model == haswell_models[i]) {
154 haswell_broadwell_cpu = 1;
160 haswell_broadwell_cpu = 0;
164 * Allocate page of door-bells and register it using DevX API.
167 * Pointer to the device context.
170 * Pointer to new page on success, NULL otherwise.
172 static struct mlx5_devx_dbr_page *
173 mlx5_alloc_dbr_page(void *ctx)
175 struct mlx5_devx_dbr_page *page;
177 /* Allocate space for door-bell page and management data. */
178 page = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
179 sizeof(struct mlx5_devx_dbr_page),
180 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
182 DRV_LOG(ERR, "cannot allocate dbr page");
185 /* Register allocated memory. */
186 page->umem = mlx5_glue->devx_umem_reg(ctx, page->dbrs,
187 MLX5_DBR_PAGE_SIZE, 0);
189 DRV_LOG(ERR, "cannot umem reg dbr page");
197 * Find the next available door-bell, allocate new page if needed.
200 * Pointer to device context.
202 * Pointer to the head of dbr pages list.
203 * @param [out] dbr_page
204 * Door-bell page containing the page data.
207 * Door-bell address offset on success, a negative error value otherwise.
210 mlx5_get_dbr(void *ctx, struct mlx5_dbr_page_list *head,
211 struct mlx5_devx_dbr_page **dbr_page)
213 struct mlx5_devx_dbr_page *page = NULL;
216 LIST_FOREACH(page, head, next)
217 if (page->dbr_count < MLX5_DBR_PER_PAGE)
219 if (!page) { /* No page with free door-bell exists. */
220 page = mlx5_alloc_dbr_page(ctx);
221 if (!page) /* Failed to allocate new page. */
223 LIST_INSERT_HEAD(head, page, next);
225 /* Loop to find bitmap part with clear bit. */
227 i < MLX5_DBR_BITMAP_SIZE && page->dbr_bitmap[i] == UINT64_MAX;
230 /* Find the first clear bit. */
231 MLX5_ASSERT(i < MLX5_DBR_BITMAP_SIZE);
232 j = rte_bsf64(~page->dbr_bitmap[i]);
233 page->dbr_bitmap[i] |= (UINT64_C(1) << j);
236 return (((i * 64) + j) * sizeof(uint64_t));
240 * Release a door-bell record.
243 * Pointer to the head of dbr pages list.
244 * @param [in] umem_id
245 * UMEM ID of page containing the door-bell record to release.
247 * Offset of door-bell record in page.
250 * 0 on success, a negative error value otherwise.
253 mlx5_release_dbr(struct mlx5_dbr_page_list *head, uint32_t umem_id,
256 struct mlx5_devx_dbr_page *page = NULL;
259 LIST_FOREACH(page, head, next)
260 /* Find the page this address belongs to. */
261 if (mlx5_os_get_umem_id(page->umem) == umem_id)
266 if (!page->dbr_count) {
267 /* Page not used, free it and remove from list. */
268 LIST_REMOVE(page, next);
270 ret = -mlx5_glue->devx_umem_dereg(page->umem);
273 /* Mark in bitmap that this door-bell is not in use. */
274 offset /= MLX5_DBR_SIZE;
278 page->dbr_bitmap[i] &= ~(UINT64_C(1) << j);