4 * Copyright 2017 6WIND S.A.
5 * Copyright 2017 Mellanox
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Memory management functions for mlx4 driver.
46 /* Verbs headers do not support -pedantic. */
48 #pragma GCC diagnostic ignored "-Wpedantic"
50 #include <infiniband/verbs.h>
52 #pragma GCC diagnostic error "-Wpedantic"
55 #include <rte_branch_prediction.h>
56 #include <rte_common.h>
57 #include <rte_errno.h>
58 #include <rte_malloc.h>
59 #include <rte_memory.h>
60 #include <rte_mempool.h>
61 #include <rte_spinlock.h>
63 #include "mlx4_glue.h"
64 #include "mlx4_rxtx.h"
65 #include "mlx4_utils.h"
67 struct mlx4_check_mempool_data {
74 * Called by mlx4_check_mempool() when iterating the memory chunks.
77 * Pointer to memory pool (unused).
78 * @param[in, out] data
79 * Pointer to shared buffer with mlx4_check_mempool().
81 * Pointer to mempool chunk header.
83 * Mempool element index (unused).
86 mlx4_check_mempool_cb(struct rte_mempool *mp, void *opaque,
87 struct rte_mempool_memhdr *memhdr,
90 struct mlx4_check_mempool_data *data = opaque;
94 /* It already failed, skip the next chunks. */
97 /* It is the first chunk. */
98 if (data->start == NULL && data->end == NULL) {
99 data->start = memhdr->addr;
100 data->end = data->start + memhdr->len;
103 if (data->end == memhdr->addr) {
104 data->end += memhdr->len;
107 if (data->start == (char *)memhdr->addr + memhdr->len) {
108 data->start -= memhdr->len;
111 /* Error, mempool is not virtually contiguous. */
116 * Check if a mempool can be used: it must be virtually contiguous.
119 * Pointer to memory pool.
121 * Pointer to the start address of the mempool virtual memory area.
123 * Pointer to the end address of the mempool virtual memory area.
126 * 0 on success (mempool is virtually contiguous), -1 on error.
129 mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start, uintptr_t *end)
131 struct mlx4_check_mempool_data data;
133 memset(&data, 0, sizeof(data));
134 rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
135 *start = (uintptr_t)data.start;
136 *end = (uintptr_t)data.end;
141 * Obtain a memory region from a memory pool.
143 * If a matching memory region already exists, it is returned with its
144 * reference count incremented, otherwise a new one is registered.
147 * Pointer to private structure.
149 * Pointer to memory pool.
152 * Memory region pointer, NULL in case of error and rte_errno is set.
155 mlx4_mr_get(struct priv *priv, struct rte_mempool *mp)
157 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
163 if (mlx4_check_mempool(mp, &start, &end) != 0) {
165 ERROR("mempool %p: not virtually contiguous",
169 DEBUG("mempool %p area start=%p end=%p size=%zu",
170 (void *)mp, (void *)start, (void *)end,
171 (size_t)(end - start));
172 /* Round start and end to page boundary if found in memory segments. */
173 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
174 uintptr_t addr = (uintptr_t)ms[i].addr;
175 size_t len = ms[i].len;
176 unsigned int align = ms[i].hugepage_sz;
178 if ((start > addr) && (start < addr + len))
179 start = RTE_ALIGN_FLOOR(start, align);
180 if ((end > addr) && (end < addr + len))
181 end = RTE_ALIGN_CEIL(end, align);
183 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
184 (void *)mp, (void *)start, (void *)end,
185 (size_t)(end - start));
186 rte_spinlock_lock(&priv->mr_lock);
187 LIST_FOREACH(mr, &priv->mr, next)
188 if (mp == mr->mp && start >= mr->start && end <= mr->end)
194 mr = rte_malloc(__func__, sizeof(*mr), 0);
199 *mr = (struct mlx4_mr){
204 .mr = mlx4_glue->reg_mr(priv->pd, (void *)start, end - start,
205 IBV_ACCESS_LOCAL_WRITE),
209 mr->lkey = mr->mr->lkey;
210 LIST_INSERT_HEAD(&priv->mr, mr, next);
214 rte_errno = errno ? errno : EINVAL;
217 rte_spinlock_unlock(&priv->mr_lock);
222 * Release a memory region.
224 * This function decrements its reference count and destroys it after
227 * Note to avoid race conditions given this function may be used from the
228 * data plane, it's extremely important that each user holds its own
232 * Memory region to release.
235 mlx4_mr_put(struct mlx4_mr *mr)
237 struct priv *priv = mr->priv;
239 rte_spinlock_lock(&priv->mr_lock);
243 LIST_REMOVE(mr, next);
244 claim_zero(mlx4_glue->dereg_mr(mr->mr));
247 rte_spinlock_unlock(&priv->mr_lock);
251 * Add memory region (MR) <-> memory pool (MP) association to txq->mp2mr[].
252 * If mp2mr[] is full, remove an entry first.
255 * Pointer to Tx queue structure.
257 * Memory pool for which a memory region lkey must be added.
259 * Index in memory pool (MP) where to add memory region (MR).
262 * Added mr->lkey on success, (uint32_t)-1 on failure.
265 mlx4_txq_add_mr(struct txq *txq, struct rte_mempool *mp, uint32_t i)
269 /* Add a new entry, register MR first. */
270 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
271 (void *)txq, mp->name, (void *)mp);
272 mr = mlx4_mr_get(txq->priv, mp);
273 if (unlikely(mr == NULL)) {
274 DEBUG("%p: unable to configure MR, mlx4_mr_get() failed",
278 if (unlikely(i == RTE_DIM(txq->mp2mr))) {
279 /* Table is full, remove oldest entry. */
280 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
283 mlx4_mr_put(txq->mp2mr[0].mr);
284 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
285 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
287 /* Store the new entry. */
288 txq->mp2mr[i].mp = mp;
289 txq->mp2mr[i].mr = mr;
290 txq->mp2mr[i].lkey = mr->lkey;
291 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
292 (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
293 return txq->mp2mr[i].lkey;