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.
44 /* Verbs headers do not support -pedantic. */
46 #pragma GCC diagnostic ignored "-Wpedantic"
48 #include <infiniband/verbs.h>
50 #pragma GCC diagnostic error "-Wpedantic"
53 #include <rte_common.h>
54 #include <rte_errno.h>
55 #include <rte_memory.h>
56 #include <rte_mempool.h>
58 #include "mlx4_utils.h"
60 struct mlx4_check_mempool_data {
67 * Called by mlx4_check_mempool() when iterating the memory chunks.
70 * Pointer to memory pool (unused).
71 * @param[in, out] data
72 * Pointer to shared buffer with mlx4_check_mempool().
74 * Pointer to mempool chunk header.
76 * Mempool element index (unused).
79 mlx4_check_mempool_cb(struct rte_mempool *mp, void *opaque,
80 struct rte_mempool_memhdr *memhdr,
83 struct mlx4_check_mempool_data *data = opaque;
87 /* It already failed, skip the next chunks. */
90 /* It is the first chunk. */
91 if (data->start == NULL && data->end == NULL) {
92 data->start = memhdr->addr;
93 data->end = data->start + memhdr->len;
96 if (data->end == memhdr->addr) {
97 data->end += memhdr->len;
100 if (data->start == (char *)memhdr->addr + memhdr->len) {
101 data->start -= memhdr->len;
104 /* Error, mempool is not virtually contiguous. */
109 * Check if a mempool can be used: it must be virtually contiguous.
112 * Pointer to memory pool.
114 * Pointer to the start address of the mempool virtual memory area.
116 * Pointer to the end address of the mempool virtual memory area.
119 * 0 on success (mempool is virtually contiguous), -1 on error.
122 mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start, uintptr_t *end)
124 struct mlx4_check_mempool_data data;
126 memset(&data, 0, sizeof(data));
127 rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
128 *start = (uintptr_t)data.start;
129 *end = (uintptr_t)data.end;
134 * Register mempool as a memory region.
137 * Pointer to protection domain.
139 * Pointer to memory pool.
142 * Memory region pointer, NULL in case of error and rte_errno is set.
145 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
147 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
153 if (mlx4_check_mempool(mp, &start, &end) != 0) {
155 ERROR("mempool %p: not virtually contiguous",
159 DEBUG("mempool %p area start=%p end=%p size=%zu",
160 (void *)mp, (void *)start, (void *)end,
161 (size_t)(end - start));
162 /* Round start and end to page boundary if found in memory segments. */
163 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
164 uintptr_t addr = (uintptr_t)ms[i].addr;
165 size_t len = ms[i].len;
166 unsigned int align = ms[i].hugepage_sz;
168 if ((start > addr) && (start < addr + len))
169 start = RTE_ALIGN_FLOOR(start, align);
170 if ((end > addr) && (end < addr + len))
171 end = RTE_ALIGN_CEIL(end, align);
173 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
174 (void *)mp, (void *)start, (void *)end,
175 (size_t)(end - start));
179 IBV_ACCESS_LOCAL_WRITE);
181 rte_errno = errno ? errno : EINVAL;