1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
3 * Copyright 2016 Mellanox Technologies, Ltd
6 #include <rte_eal_memconfig.h>
7 #include <rte_mempool.h>
8 #include <rte_malloc.h>
9 #include <rte_rwlock.h>
10 #include <rte_bus_pci.h>
12 #include <mlx5_common_mp.h>
13 #include <mlx5_common_mr.h>
17 #include "mlx5_rxtx.h"
19 struct mr_find_contig_memsegs_data {
23 const struct rte_memseg_list *msl;
26 struct mr_update_mp_data {
27 struct rte_eth_dev *dev;
28 struct mlx5_mr_ctrl *mr_ctrl;
33 * Callback for memory free event. Iterate freed memsegs and check whether it
34 * belongs to an existing MR. If found, clear the bit from bitmap of MR. As a
35 * result, the MR would be fragmented. If it becomes empty, the MR will be freed
36 * later by mlx5_mr_garbage_collect(). Even if this callback is called from a
37 * secondary process, the garbage collector will be called in primary process
38 * as the secondary process can't call mlx5_mr_create().
40 * The global cache must be rebuilt if there's any change and this event has to
41 * be propagated to dataplane threads to flush the local caches.
44 * Pointer to the Ethernet device shared context.
46 * Address of freed memory.
48 * Size of freed memory.
51 mlx5_mr_mem_event_free_cb(struct mlx5_dev_ctx_shared *sh,
52 const void *addr, size_t len)
54 const struct rte_memseg_list *msl;
60 DEBUG("device %s free callback: addr=%p, len=%zu",
61 sh->ibdev_name, addr, len);
62 msl = rte_mem_virt2memseg_list(addr);
63 /* addr and len must be page-aligned. */
64 MLX5_ASSERT((uintptr_t)addr ==
65 RTE_ALIGN((uintptr_t)addr, msl->page_sz));
66 MLX5_ASSERT(len == RTE_ALIGN(len, msl->page_sz));
67 ms_n = len / msl->page_sz;
68 rte_rwlock_write_lock(&sh->share_cache.rwlock);
69 /* Clear bits of freed memsegs from MR. */
70 for (i = 0; i < ms_n; ++i) {
71 const struct rte_memseg *ms;
72 struct mr_cache_entry entry;
77 /* Find MR having this memseg. */
78 start = (uintptr_t)addr + i * msl->page_sz;
79 mr = mlx5_mr_lookup_list(&sh->share_cache, &entry, start);
82 MLX5_ASSERT(mr->msl); /* Can't be external memory. */
83 ms = rte_mem_virt2memseg((void *)start, msl);
84 MLX5_ASSERT(ms != NULL);
85 MLX5_ASSERT(msl->page_sz == ms->hugepage_sz);
86 ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
87 pos = ms_idx - mr->ms_base_idx;
88 MLX5_ASSERT(rte_bitmap_get(mr->ms_bmp, pos));
89 MLX5_ASSERT(pos < mr->ms_bmp_n);
90 DEBUG("device %s MR(%p): clear bitmap[%u] for addr %p",
91 sh->ibdev_name, (void *)mr, pos, (void *)start);
92 rte_bitmap_clear(mr->ms_bmp, pos);
93 if (--mr->ms_n == 0) {
95 LIST_INSERT_HEAD(&sh->share_cache.mr_free_list, mr, mr);
96 DEBUG("device %s remove MR(%p) from list",
97 sh->ibdev_name, (void *)mr);
100 * MR is fragmented or will be freed. the global cache must be
106 mlx5_mr_rebuild_cache(&sh->share_cache);
108 * Flush local caches by propagating invalidation across cores.
109 * rte_smp_wmb() is enough to synchronize this event. If one of
110 * freed memsegs is seen by other core, that means the memseg
111 * has been allocated by allocator, which will come after this
112 * free call. Therefore, this store instruction (incrementing
113 * generation below) will be guaranteed to be seen by other core
114 * before the core sees the newly allocated memory.
116 ++sh->share_cache.dev_gen;
117 DEBUG("broadcasting local cache flush, gen=%d",
118 sh->share_cache.dev_gen);
121 rte_rwlock_write_unlock(&sh->share_cache.rwlock);
125 * Callback for memory event. This can be called from both primary and secondary
136 mlx5_mr_mem_event_cb(enum rte_mem_event event_type, const void *addr,
137 size_t len, void *arg __rte_unused)
139 struct mlx5_dev_ctx_shared *sh;
140 struct mlx5_dev_list *dev_list = &mlx5_shared_data->mem_event_cb_list;
142 /* Must be called from the primary process. */
143 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
144 switch (event_type) {
145 case RTE_MEM_EVENT_FREE:
146 rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock);
147 /* Iterate all the existing mlx5 devices. */
148 LIST_FOREACH(sh, dev_list, mem_event_cb)
149 mlx5_mr_mem_event_free_cb(sh, addr, len);
150 rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock);
152 case RTE_MEM_EVENT_ALLOC:
159 * Bottom-half of LKey search on Rx.
162 * Pointer to Rx queue structure.
167 * Searched LKey on success, UINT32_MAX on no match.
170 mlx5_rx_addr2mr_bh(struct mlx5_rxq_data *rxq, uintptr_t addr)
172 struct mlx5_rxq_ctrl *rxq_ctrl =
173 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
174 struct mlx5_mr_ctrl *mr_ctrl = &rxq->mr_ctrl;
175 struct mlx5_priv *priv = rxq_ctrl->priv;
177 return mlx5_mr_addr2mr_bh(priv->sh->pd, &priv->mp_id,
178 &priv->sh->share_cache, mr_ctrl, addr,
179 priv->config.mr_ext_memseg_en);
183 * Bottom-half of LKey search on Tx.
186 * Pointer to Tx queue structure.
191 * Searched LKey on success, UINT32_MAX on no match.
194 mlx5_tx_addr2mr_bh(struct mlx5_txq_data *txq, uintptr_t addr)
196 struct mlx5_txq_ctrl *txq_ctrl =
197 container_of(txq, struct mlx5_txq_ctrl, txq);
198 struct mlx5_mr_ctrl *mr_ctrl = &txq->mr_ctrl;
199 struct mlx5_priv *priv = txq_ctrl->priv;
201 return mlx5_mr_addr2mr_bh(priv->sh->pd, &priv->mp_id,
202 &priv->sh->share_cache, mr_ctrl, addr,
203 priv->config.mr_ext_memseg_en);
207 * Bottom-half of LKey search on Tx. If it can't be searched in the memseg
208 * list, register the mempool of the mbuf as externally allocated memory.
211 * Pointer to Tx queue structure.
216 * Searched LKey on success, UINT32_MAX on no match.
219 mlx5_tx_mb2mr_bh(struct mlx5_txq_data *txq, struct rte_mbuf *mb)
221 uintptr_t addr = (uintptr_t)mb->buf_addr;
224 lkey = mlx5_tx_addr2mr_bh(txq, addr);
225 if (lkey == UINT32_MAX && rte_errno == ENXIO) {
226 /* Mempool may have externally allocated memory. */
227 return mlx5_tx_update_ext_mp(txq, addr, mlx5_mb2mp(mb));
233 * Called during rte_mempool_mem_iter() by mlx5_mr_update_ext_mp().
235 * Externally allocated chunk is registered and a MR is created for the chunk.
236 * The MR object is added to the global list. If memseg list of a MR object
237 * (mr->msl) is null, the MR object can be regarded as externally allocated
240 * Once external memory is registered, it should be static. If the memory is
241 * freed and the virtual address range has different physical memory mapped
242 * again, it may cause crash on device due to the wrong translation entry. PMD
243 * can't track the free event of the external memory for now.
246 mlx5_mr_update_ext_mp_cb(struct rte_mempool *mp, void *opaque,
247 struct rte_mempool_memhdr *memhdr,
248 unsigned mem_idx __rte_unused)
250 struct mr_update_mp_data *data = opaque;
251 struct rte_eth_dev *dev = data->dev;
252 struct mlx5_priv *priv = dev->data->dev_private;
253 struct mlx5_dev_ctx_shared *sh = priv->sh;
254 struct mlx5_mr_ctrl *mr_ctrl = data->mr_ctrl;
255 struct mlx5_mr *mr = NULL;
256 uintptr_t addr = (uintptr_t)memhdr->addr;
257 size_t len = memhdr->len;
258 struct mr_cache_entry entry;
261 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
262 /* If already registered, it should return. */
263 rte_rwlock_read_lock(&sh->share_cache.rwlock);
264 lkey = mlx5_mr_lookup_cache(&sh->share_cache, &entry, addr);
265 rte_rwlock_read_unlock(&sh->share_cache.rwlock);
266 if (lkey != UINT32_MAX)
268 DRV_LOG(DEBUG, "port %u register MR for chunk #%d of mempool (%s)",
269 dev->data->port_id, mem_idx, mp->name);
270 mr = mlx5_create_mr_ext(sh->pd, addr, len, mp->socket_id,
271 sh->share_cache.reg_mr_cb);
274 "port %u unable to allocate a new MR of"
276 dev->data->port_id, mp->name);
280 rte_rwlock_write_lock(&sh->share_cache.rwlock);
281 LIST_INSERT_HEAD(&sh->share_cache.mr_list, mr, mr);
282 /* Insert to the global cache table. */
283 mlx5_mr_insert_cache(&sh->share_cache, mr);
284 rte_rwlock_write_unlock(&sh->share_cache.rwlock);
285 /* Insert to the local cache table */
286 mlx5_mr_addr2mr_bh(sh->pd, &priv->mp_id, &sh->share_cache,
287 mr_ctrl, addr, priv->config.mr_ext_memseg_en);
291 * Finds the first ethdev that match the pci device.
292 * The existence of multiple ethdev per pci device is only with representors.
293 * On such case, it is enough to get only one of the ports as they all share
294 * the same ibv context.
297 * Pointer to the PCI device.
300 * Pointer to the ethdev if found, NULL otherwise.
302 static struct rte_eth_dev *
303 pci_dev_to_eth_dev(struct rte_pci_device *pdev)
307 port_id = rte_eth_find_next_of(0, &pdev->device);
308 if (port_id == RTE_MAX_ETHPORTS)
310 return &rte_eth_devices[port_id];
314 * DPDK callback to DMA map external memory to a PCI device.
317 * Pointer to the PCI device.
319 * Starting virtual address of memory to be mapped.
321 * Starting IOVA address of memory to be mapped.
323 * Length of memory segment being mapped.
326 * 0 on success, negative value on error.
329 mlx5_dma_map(struct rte_pci_device *pdev, void *addr,
330 uint64_t iova __rte_unused, size_t len)
332 struct rte_eth_dev *dev;
334 struct mlx5_priv *priv;
335 struct mlx5_dev_ctx_shared *sh;
337 dev = pci_dev_to_eth_dev(pdev);
339 DRV_LOG(WARNING, "unable to find matching ethdev "
340 "to PCI device %p", (void *)pdev);
344 priv = dev->data->dev_private;
346 mr = mlx5_create_mr_ext(sh->pd, (uintptr_t)addr, len, SOCKET_ID_ANY,
347 sh->share_cache.reg_mr_cb);
350 "port %u unable to dma map", dev->data->port_id);
354 rte_rwlock_write_lock(&sh->share_cache.rwlock);
355 LIST_INSERT_HEAD(&sh->share_cache.mr_list, mr, mr);
356 /* Insert to the global cache table. */
357 mlx5_mr_insert_cache(&sh->share_cache, mr);
358 rte_rwlock_write_unlock(&sh->share_cache.rwlock);
363 * DPDK callback to DMA unmap external memory to a PCI device.
366 * Pointer to the PCI device.
368 * Starting virtual address of memory to be unmapped.
370 * Starting IOVA address of memory to be unmapped.
372 * Length of memory segment being unmapped.
375 * 0 on success, negative value on error.
378 mlx5_dma_unmap(struct rte_pci_device *pdev, void *addr,
379 uint64_t iova __rte_unused, size_t len __rte_unused)
381 struct rte_eth_dev *dev;
382 struct mlx5_priv *priv;
383 struct mlx5_dev_ctx_shared *sh;
385 struct mr_cache_entry entry;
387 dev = pci_dev_to_eth_dev(pdev);
389 DRV_LOG(WARNING, "unable to find matching ethdev "
390 "to PCI device %p", (void *)pdev);
394 priv = dev->data->dev_private;
396 rte_rwlock_read_lock(&sh->share_cache.rwlock);
397 mr = mlx5_mr_lookup_list(&sh->share_cache, &entry, (uintptr_t)addr);
399 rte_rwlock_read_unlock(&sh->share_cache.rwlock);
400 DRV_LOG(WARNING, "address 0x%" PRIxPTR " wasn't registered "
401 "to PCI device %p", (uintptr_t)addr,
407 LIST_INSERT_HEAD(&sh->share_cache.mr_free_list, mr, mr);
408 DEBUG("port %u remove MR(%p) from list", dev->data->port_id,
410 mlx5_mr_rebuild_cache(&sh->share_cache);
412 * Flush local caches by propagating invalidation across cores.
413 * rte_smp_wmb() is enough to synchronize this event. If one of
414 * freed memsegs is seen by other core, that means the memseg
415 * has been allocated by allocator, which will come after this
416 * free call. Therefore, this store instruction (incrementing
417 * generation below) will be guaranteed to be seen by other core
418 * before the core sees the newly allocated memory.
420 ++sh->share_cache.dev_gen;
421 DEBUG("broadcasting local cache flush, gen=%d",
422 sh->share_cache.dev_gen);
424 rte_rwlock_read_unlock(&sh->share_cache.rwlock);
429 * Register MR for entire memory chunks in a Mempool having externally allocated
430 * memory and fill in local cache.
433 * Pointer to Ethernet device.
435 * Pointer to per-queue MR control structure.
437 * Pointer to registering Mempool.
440 * 0 on success, -1 on failure.
443 mlx5_mr_update_ext_mp(struct rte_eth_dev *dev, struct mlx5_mr_ctrl *mr_ctrl,
444 struct rte_mempool *mp)
446 struct mr_update_mp_data data = {
452 rte_mempool_mem_iter(mp, mlx5_mr_update_ext_mp_cb, &data);
457 * Register MR entire memory chunks in a Mempool having externally allocated
458 * memory and search LKey of the address to return.
461 * Pointer to Ethernet device.
465 * Pointer to registering Mempool where addr belongs.
468 * LKey for address on success, UINT32_MAX on failure.
471 mlx5_tx_update_ext_mp(struct mlx5_txq_data *txq, uintptr_t addr,
472 struct rte_mempool *mp)
474 struct mlx5_txq_ctrl *txq_ctrl =
475 container_of(txq, struct mlx5_txq_ctrl, txq);
476 struct mlx5_mr_ctrl *mr_ctrl = &txq->mr_ctrl;
477 struct mlx5_priv *priv = txq_ctrl->priv;
479 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
481 "port %u using address (%p) from unregistered mempool"
482 " having externally allocated memory"
483 " in secondary process, please create mempool"
484 " prior to rte_eth_dev_start()",
485 PORT_ID(priv), (void *)addr);
488 mlx5_mr_update_ext_mp(ETH_DEV(priv), mr_ctrl, mp);
489 return mlx5_tx_addr2mr_bh(txq, addr);
492 /* Called during rte_mempool_mem_iter() by mlx5_mr_update_mp(). */
494 mlx5_mr_update_mp_cb(struct rte_mempool *mp __rte_unused, void *opaque,
495 struct rte_mempool_memhdr *memhdr,
496 unsigned mem_idx __rte_unused)
498 struct mr_update_mp_data *data = opaque;
499 struct rte_eth_dev *dev = data->dev;
500 struct mlx5_priv *priv = dev->data->dev_private;
504 /* Stop iteration if failed in the previous walk. */
507 /* Register address of the chunk and update local caches. */
508 lkey = mlx5_mr_addr2mr_bh(priv->sh->pd, &priv->mp_id,
509 &priv->sh->share_cache, data->mr_ctrl,
510 (uintptr_t)memhdr->addr,
511 priv->config.mr_ext_memseg_en);
512 if (lkey == UINT32_MAX)
517 * Register entire memory chunks in a Mempool.
520 * Pointer to Ethernet device.
522 * Pointer to per-queue MR control structure.
524 * Pointer to registering Mempool.
527 * 0 on success, -1 on failure.
530 mlx5_mr_update_mp(struct rte_eth_dev *dev, struct mlx5_mr_ctrl *mr_ctrl,
531 struct rte_mempool *mp)
533 struct mr_update_mp_data data = {
539 rte_mempool_mem_iter(mp, mlx5_mr_update_mp_cb, &data);
540 if (data.ret < 0 && rte_errno == ENXIO) {
541 /* Mempool may have externally allocated memory. */
542 return mlx5_mr_update_ext_mp(dev, mr_ctrl, mp);