LCOV - code coverage report
Current view: top level - drivers/mempool/cnxk - cn10k_hwpool_ops.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 1 78 1.3 %
Date: 2025-05-01 17:49:45 Functions: 1 11 9.1 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 30 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(C) 2023 Marvell.
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <eal_export.h>
       6                 :            : #include <rte_mempool.h>
       7                 :            : #include <rte_pmd_cnxk_mempool.h>
       8                 :            : 
       9                 :            : #include "roc_api.h"
      10                 :            : #include "cnxk_mempool.h"
      11                 :            : 
      12                 :            : #define CN10K_HWPOOL_MEM_SIZE    128
      13                 :            : #define CN10K_NPA_IOVA_RANGE_MIN 0x0
      14                 :            : #define CN10K_NPA_IOVA_RANGE_MAX 0x1fffffffffff80
      15                 :            : 
      16                 :            : static int __rte_hot
      17                 :          0 : cn10k_hwpool_enq(struct rte_mempool *hp, void *const *obj_table, unsigned int n)
      18                 :            : {
      19                 :            :         struct rte_mempool *mp;
      20                 :            :         unsigned int index;
      21                 :            : 
      22                 :          0 :         mp = CNXK_MEMPOOL_CONFIG(hp);
      23                 :            :         /* Ensure mbuf init changes are written before the free pointers
      24                 :            :          * are enqueued to the stack.
      25                 :            :          */
      26                 :          0 :         rte_io_wmb();
      27         [ #  # ]:          0 :         for (index = 0; index < n; index++) {
      28                 :            :                 struct rte_mempool_objhdr *hdr;
      29                 :            :                 struct rte_mbuf *m;
      30                 :            : 
      31                 :          0 :                 m = PLT_PTR_CAST(obj_table[index]);
      32                 :            :                 /* Update mempool information in the mbuf */
      33                 :            :                 hdr = rte_mempool_get_header(obj_table[index]);
      34                 :            : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
      35                 :            :                 if (hdr->mp != m->pool || hdr->mp != hp)
      36                 :            :                         plt_err("Pool Header Mismatch");
      37                 :            : #endif
      38                 :          0 :                 m->pool = mp;
      39                 :          0 :                 hdr->mp = mp;
      40                 :          0 :                 roc_npa_aura_op_free(hp->pool_id, 0,
      41                 :          0 :                                      (uint64_t)obj_table[index]);
      42                 :            :         }
      43                 :            : 
      44                 :          0 :         return 0;
      45                 :            : }
      46                 :            : 
      47                 :            : static int __rte_hot
      48                 :          0 : cn10k_hwpool_deq(struct rte_mempool *hp, void **obj_table, unsigned int n)
      49                 :            : {
      50                 :            :         unsigned int index;
      51                 :            :         uint64_t obj;
      52                 :            : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
      53                 :            :         struct rte_mempool *mp;
      54                 :            : 
      55                 :            :         mp = CNXK_MEMPOOL_CONFIG(hp);
      56                 :            : #endif
      57                 :            : 
      58         [ #  # ]:          0 :         for (index = 0; index < n; index++, obj_table++) {
      59                 :            :                 struct rte_mempool_objhdr *hdr;
      60                 :            :                 struct rte_mbuf *m;
      61                 :            :                 int retry = 4;
      62                 :            : 
      63                 :            :                 /* Retry few times before failing */
      64                 :            :                 do {
      65                 :            :                         obj = roc_npa_aura_op_alloc(hp->pool_id, 0);
      66                 :            :                 } while (retry-- && (obj == 0));
      67                 :            : 
      68                 :            :                 if (obj == 0) {
      69                 :          0 :                         cn10k_hwpool_enq(hp, obj_table - index, index);
      70                 :          0 :                         return -ENOENT;
      71                 :            :                 }
      72                 :            :                 /* Update mempool information in the mbuf */
      73                 :            :                 hdr = rte_mempool_get_header(PLT_PTR_CAST(obj));
      74                 :            :                 m = PLT_PTR_CAST(obj);
      75                 :            : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
      76                 :            :                 if (hdr->mp != m->pool || hdr->mp != mp)
      77                 :            :                         plt_err("Pool Header Mismatch");
      78                 :            : #endif
      79                 :            :                 m->pool = hp;
      80                 :            :                 hdr->mp = hp;
      81                 :            :                 *obj_table = (void *)obj;
      82                 :            :         }
      83                 :            : 
      84                 :            :         return 0;
      85                 :            : }
      86                 :            : 
      87                 :            : static unsigned int
      88                 :          0 : cn10k_hwpool_get_count(const struct rte_mempool *hp)
      89                 :            : {
      90                 :          0 :         return (unsigned int)roc_npa_aura_op_available(hp->pool_id);
      91                 :            : }
      92                 :            : 
      93                 :            : static int
      94                 :          0 : cn10k_hwpool_alloc(struct rte_mempool *hp)
      95                 :            : {
      96                 :          0 :         uint64_t aura_handle = 0;
      97                 :            :         struct rte_mempool *mp;
      98                 :            :         uint32_t pool_id;
      99                 :            :         int rc;
     100                 :            : 
     101         [ #  # ]:          0 :         if (hp->cache_size) {
     102                 :          0 :                 plt_err("Hwpool does not support cache");
     103                 :          0 :                 return -EINVAL;
     104                 :            :         }
     105                 :            : 
     106         [ #  # ]:          0 :         if (CNXK_MEMPOOL_FLAGS(hp)) {
     107                 :          0 :                 plt_err("Flags must not be passed to hwpool ops");
     108                 :          0 :                 return -EINVAL;
     109                 :            :         }
     110                 :            : 
     111                 :          0 :         mp = CNXK_MEMPOOL_CONFIG(hp);
     112         [ #  # ]:          0 :         if (!mp) {
     113                 :          0 :                 plt_err("Invalid rte_mempool passed as pool_config");
     114                 :          0 :                 return -EINVAL;
     115                 :            :         }
     116         [ #  # ]:          0 :         if (mp->cache_size) {
     117                 :          0 :                 plt_err("Hwpool does not support attaching to pool with cache");
     118                 :          0 :                 return -EINVAL;
     119                 :            :         }
     120                 :            : 
     121         [ #  # ]:          0 :         if (hp->elt_size != mp->elt_size ||
     122                 :          0 :             hp->header_size != mp->header_size ||
     123   [ #  #  #  # ]:          0 :             hp->trailer_size != mp->trailer_size || hp->size != mp->size) {
     124                 :          0 :                 plt_err("Hwpool parameters matching with master pool");
     125                 :          0 :                 return -EINVAL;
     126                 :            :         }
     127                 :            : 
     128                 :            :         /* Create the NPA aura */
     129                 :          0 :         pool_id = roc_npa_aura_handle_to_aura(mp->pool_id);
     130                 :          0 :         rc = roc_npa_aura_create(&aura_handle, hp->size, NULL, (int)pool_id, 0);
     131         [ #  # ]:          0 :         if (rc) {
     132                 :          0 :                 plt_err("Failed to create aura rc=%d", rc);
     133                 :          0 :                 return rc;
     134                 :            :         }
     135                 :            : 
     136                 :            :         /* Set the flags for the hardware pool */
     137                 :          0 :         CNXK_MEMPOOL_SET_FLAGS(hp, CNXK_MEMPOOL_F_IS_HWPOOL);
     138                 :          0 :         hp->pool_id = aura_handle;
     139                 :          0 :         plt_npa_dbg("aura_handle=0x%" PRIx64, aura_handle);
     140                 :            : 
     141                 :          0 :         return 0;
     142                 :            : }
     143                 :            : 
     144                 :            : static void
     145                 :          0 : cn10k_hwpool_free(struct rte_mempool *hp)
     146                 :            : {
     147                 :            :         int rc = 0;
     148                 :            : 
     149                 :          0 :         plt_npa_dbg("aura_handle=0x%" PRIx64, hp->pool_id);
     150                 :            :         /* It can happen that rte_mempool_free() is called immediately after
     151                 :            :          * rte_mempool_create_empty(). In such cases the NPA pool will not be
     152                 :            :          * allocated.
     153                 :            :          */
     154         [ #  # ]:          0 :         if (roc_npa_aura_handle_to_base(hp->pool_id) == 0)
     155                 :            :                 return;
     156                 :            : 
     157                 :          0 :         rc = roc_npa_aura_destroy(hp->pool_id);
     158         [ #  # ]:          0 :         if (rc)
     159                 :          0 :                 plt_err("Failed to destroy aura rc=%d", rc);
     160                 :            : }
     161                 :            : 
     162                 :            : static ssize_t
     163                 :          0 : cn10k_hwpool_calc_mem_size(const struct rte_mempool *hp, uint32_t obj_num,
     164                 :            :                            uint32_t pg_shift, size_t *min_chunk_size,
     165                 :            :                            size_t *align)
     166                 :            : {
     167                 :            :         RTE_SET_USED(hp);
     168                 :            :         RTE_SET_USED(obj_num);
     169                 :            :         RTE_SET_USED(pg_shift);
     170                 :          0 :         *min_chunk_size = CN10K_HWPOOL_MEM_SIZE;
     171                 :          0 :         *align = CN10K_HWPOOL_MEM_SIZE;
     172                 :            :         /* Return a minimum mem size so that hwpool can also be initialized just
     173                 :            :          * like a regular pool. This memzone will not be used anywhere.
     174                 :            :          */
     175                 :          0 :         return CN10K_HWPOOL_MEM_SIZE;
     176                 :            : }
     177                 :            : 
     178                 :            : static int
     179                 :          0 : cn10k_hwpool_populate(struct rte_mempool *hp, unsigned int max_objs,
     180                 :            :                       void *vaddr, rte_iova_t iova, size_t len,
     181                 :            :                       rte_mempool_populate_obj_cb_t *obj_cb, void *obj_cb_arg)
     182                 :            : {
     183                 :            :         uint64_t start_iova, end_iova;
     184                 :            :         struct rte_mempool *mp;
     185                 :            : 
     186                 :            :         RTE_SET_USED(max_objs);
     187                 :            :         RTE_SET_USED(vaddr);
     188                 :            :         RTE_SET_USED(iova);
     189                 :            :         RTE_SET_USED(len);
     190                 :            :         RTE_SET_USED(obj_cb);
     191                 :            :         RTE_SET_USED(obj_cb_arg);
     192                 :            :         /* HW pools does not require populating anything as these pools are
     193                 :            :          * only associated with NPA aura. The NPA pool being used is that of
     194                 :            :          * another rte_mempool. Only copy the iova range from the aura of
     195                 :            :          * the other rte_mempool to this pool's aura.
     196                 :            :          */
     197                 :          0 :         mp = CNXK_MEMPOOL_CONFIG(hp);
     198                 :          0 :         roc_npa_aura_op_range_get(mp->pool_id, &start_iova, &end_iova);
     199                 :          0 :         roc_npa_aura_op_range_set(hp->pool_id, start_iova, end_iova);
     200                 :            : 
     201                 :          0 :         return hp->size;
     202                 :            : }
     203                 :            : 
     204                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pmd_cnxk_mempool_mbuf_exchange, 23.07)
     205                 :            : int
     206                 :          0 : rte_pmd_cnxk_mempool_mbuf_exchange(struct rte_mbuf *m1, struct rte_mbuf *m2)
     207                 :            : {
     208                 :            :         struct rte_mempool_objhdr *hdr;
     209                 :            : 
     210                 :            : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
     211                 :            :         if (!(CNXK_MEMPOOL_FLAGS(m1->pool) & CNXK_MEMPOOL_F_NO_RANGE_CHECK) ||
     212                 :            :             !(CNXK_MEMPOOL_FLAGS(m2->pool) & CNXK_MEMPOOL_F_NO_RANGE_CHECK)) {
     213                 :            :                 plt_err("Pools must have range check disabled");
     214                 :            :                 return -EINVAL;
     215                 :            :         }
     216                 :            :         if (m1->pool->elt_size != m2->pool->elt_size ||
     217                 :            :             m1->pool->header_size != m2->pool->header_size ||
     218                 :            :             m1->pool->trailer_size != m2->pool->trailer_size ||
     219                 :            :             m1->pool->size != m2->pool->size) {
     220                 :            :                 plt_err("Parameters of pools involved in exchange does not match");
     221                 :            :                 return -EINVAL;
     222                 :            :         }
     223                 :            : #endif
     224                 :          0 :         RTE_SWAP(m1->pool, m2->pool);
     225                 :            :         hdr = rte_mempool_get_header(m1);
     226                 :          0 :         hdr->mp = m1->pool;
     227                 :            :         hdr = rte_mempool_get_header(m2);
     228                 :          0 :         hdr->mp = m2->pool;
     229                 :          0 :         return 0;
     230                 :            : }
     231                 :            : 
     232                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pmd_cnxk_mempool_is_hwpool, 23.07)
     233                 :            : int
     234                 :          0 : rte_pmd_cnxk_mempool_is_hwpool(struct rte_mempool *mp)
     235                 :            : {
     236                 :          0 :         return !!(CNXK_MEMPOOL_FLAGS(mp) & CNXK_MEMPOOL_F_IS_HWPOOL);
     237                 :            : }
     238                 :            : 
     239                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pmd_cnxk_mempool_range_check_disable, 23.07)
     240                 :            : int
     241                 :          0 : rte_pmd_cnxk_mempool_range_check_disable(struct rte_mempool *mp)
     242                 :            : {
     243         [ #  # ]:          0 :         if (rte_pmd_cnxk_mempool_is_hwpool(mp)) {
     244                 :            :                 /* Disable only aura range check for hardware pools */
     245                 :          0 :                 roc_npa_aura_op_range_set(mp->pool_id, CN10K_NPA_IOVA_RANGE_MIN,
     246                 :            :                                           CN10K_NPA_IOVA_RANGE_MAX);
     247                 :          0 :                 CNXK_MEMPOOL_SET_FLAGS(mp, CNXK_MEMPOOL_F_NO_RANGE_CHECK);
     248                 :          0 :                 mp = CNXK_MEMPOOL_CONFIG(mp);
     249                 :            :         }
     250                 :            : 
     251                 :            :         /* No need to disable again if already disabled */
     252         [ #  # ]:          0 :         if (CNXK_MEMPOOL_FLAGS(mp) & CNXK_MEMPOOL_F_NO_RANGE_CHECK)
     253                 :            :                 return 0;
     254                 :            : 
     255                 :            :         /* Disable aura/pool range check */
     256                 :          0 :         roc_npa_pool_op_range_set(mp->pool_id, CN10K_NPA_IOVA_RANGE_MIN,
     257                 :            :                                   CN10K_NPA_IOVA_RANGE_MAX);
     258         [ #  # ]:          0 :         if (roc_npa_pool_range_update_check(mp->pool_id) < 0)
     259                 :            :                 return -EBUSY;
     260                 :            : 
     261                 :          0 :         CNXK_MEMPOOL_SET_FLAGS(mp, CNXK_MEMPOOL_F_NO_RANGE_CHECK);
     262                 :          0 :         return 0;
     263                 :            : }
     264                 :            : 
     265                 :            : static struct rte_mempool_ops cn10k_hwpool_ops = {
     266                 :            :         .name = "cn10k_hwpool_ops",
     267                 :            :         .alloc = cn10k_hwpool_alloc,
     268                 :            :         .free = cn10k_hwpool_free,
     269                 :            :         .enqueue = cn10k_hwpool_enq,
     270                 :            :         .dequeue = cn10k_hwpool_deq,
     271                 :            :         .get_count = cn10k_hwpool_get_count,
     272                 :            :         .calc_mem_size = cn10k_hwpool_calc_mem_size,
     273                 :            :         .populate = cn10k_hwpool_populate,
     274                 :            : };
     275                 :            : 
     276                 :        252 : RTE_MEMPOOL_REGISTER_OPS(cn10k_hwpool_ops);

Generated by: LCOV version 1.14