LCOV - code coverage report
Current view: top level - drivers/mempool/dpaa - dpaa_mempool.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 2 129 1.6 %
Date: 2024-02-14 00:53:57 Functions: 2 9 22.2 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1 74 1.4 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *
       3                 :            :  *   Copyright 2017,2019 NXP
       4                 :            :  *
       5                 :            :  */
       6                 :            : 
       7                 :            : /* System headers */
       8                 :            : #include <stdio.h>
       9                 :            : #include <inttypes.h>
      10                 :            : #include <unistd.h>
      11                 :            : #include <limits.h>
      12                 :            : #include <sched.h>
      13                 :            : #include <signal.h>
      14                 :            : #include <pthread.h>
      15                 :            : #include <sys/types.h>
      16                 :            : #include <sys/syscall.h>
      17                 :            : 
      18                 :            : #include <rte_byteorder.h>
      19                 :            : #include <rte_common.h>
      20                 :            : #include <rte_log.h>
      21                 :            : #include <rte_debug.h>
      22                 :            : #include <rte_memory.h>
      23                 :            : #include <rte_tailq.h>
      24                 :            : #include <rte_eal.h>
      25                 :            : #include <rte_malloc.h>
      26                 :            : #include <rte_ring.h>
      27                 :            : 
      28                 :            : #include <dpaa_mempool.h>
      29                 :            : #include <dpaax_iova_table.h>
      30                 :            : 
      31                 :            : /* List of all the memseg information locally maintained in dpaa driver. This
      32                 :            :  * is to optimize the PA_to_VA searches until a better mechanism (algo) is
      33                 :            :  * available.
      34                 :            :  */
      35                 :            : struct dpaa_memseg_list rte_dpaa_memsegs
      36                 :            :         = TAILQ_HEAD_INITIALIZER(rte_dpaa_memsegs);
      37                 :            : 
      38                 :            : struct dpaa_bp_info *rte_dpaa_bpid_info;
      39                 :            : 
      40         [ -  + ]:        235 : RTE_LOG_REGISTER_DEFAULT(dpaa_logtype_mempool, NOTICE);
      41                 :            : #define RTE_LOGTYPE_DPAA_MEMPOOL dpaa_logtype_mempool
      42                 :            : 
      43                 :            : static int
      44                 :          0 : dpaa_mbuf_create_pool(struct rte_mempool *mp)
      45                 :            : {
      46                 :            :         struct bman_pool *bp;
      47                 :            :         struct bm_buffer bufs[8];
      48                 :            :         struct dpaa_bp_info *bp_info;
      49                 :            :         uint8_t bpid;
      50                 :            :         int num_bufs = 0, ret = 0;
      51                 :          0 :         struct bman_pool_params params = {
      52                 :            :                 .flags = BMAN_POOL_FLAG_DYNAMIC_BPID
      53                 :            :         };
      54                 :            : 
      55                 :          0 :         MEMPOOL_INIT_FUNC_TRACE();
      56                 :            : 
      57         [ #  # ]:          0 :         if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
      58                 :          0 :                 ret = rte_dpaa_portal_init((void *)0);
      59         [ #  # ]:          0 :                 if (ret) {
      60                 :          0 :                         DPAA_MEMPOOL_ERR(
      61                 :            :                                 "rte_dpaa_portal_init failed with ret: %d",
      62                 :            :                                  ret);
      63                 :          0 :                         return -1;
      64                 :            :                 }
      65                 :            :         }
      66                 :          0 :         bp = bman_new_pool(&params);
      67         [ #  # ]:          0 :         if (!bp) {
      68                 :          0 :                 DPAA_MEMPOOL_ERR("bman_new_pool() failed");
      69                 :          0 :                 return -ENODEV;
      70                 :            :         }
      71                 :          0 :         bpid = bman_get_params(bp)->bpid;
      72                 :            : 
      73                 :            :         /* Drain the pool of anything already in it. */
      74                 :            :         do {
      75                 :            :                 /* Acquire is all-or-nothing, so we drain in 8s,
      76                 :            :                  * then in 1s for the remainder.
      77                 :            :                  */
      78         [ #  # ]:          0 :                 if (ret != 1)
      79                 :          0 :                         ret = bman_acquire(bp, bufs, 8, 0);
      80         [ #  # ]:          0 :                 if (ret < 8)
      81                 :          0 :                         ret = bman_acquire(bp, bufs, 1, 0);
      82         [ #  # ]:          0 :                 if (ret > 0)
      83                 :          0 :                         num_bufs += ret;
      84         [ #  # ]:          0 :         } while (ret > 0);
      85         [ #  # ]:          0 :         if (num_bufs)
      86                 :          0 :                 DPAA_MEMPOOL_WARN("drained %u bufs from BPID %d",
      87                 :            :                                   num_bufs, bpid);
      88                 :            : 
      89         [ #  # ]:          0 :         if (rte_dpaa_bpid_info == NULL) {
      90                 :          0 :                 rte_dpaa_bpid_info = (struct dpaa_bp_info *)rte_zmalloc(NULL,
      91                 :            :                                 sizeof(struct dpaa_bp_info) * DPAA_MAX_BPOOLS,
      92                 :            :                                 RTE_CACHE_LINE_SIZE);
      93         [ #  # ]:          0 :                 if (rte_dpaa_bpid_info == NULL) {
      94                 :          0 :                         bman_free_pool(bp);
      95                 :          0 :                         return -ENOMEM;
      96                 :            :                 }
      97                 :            :         }
      98                 :            : 
      99                 :          0 :         rte_dpaa_bpid_info[bpid].mp = mp;
     100                 :          0 :         rte_dpaa_bpid_info[bpid].bpid = bpid;
     101                 :          0 :         rte_dpaa_bpid_info[bpid].size = mp->elt_size;
     102         [ #  # ]:          0 :         rte_dpaa_bpid_info[bpid].bp = bp;
     103                 :          0 :         rte_dpaa_bpid_info[bpid].meta_data_size =
     104                 :          0 :                 sizeof(struct rte_mbuf) + rte_pktmbuf_priv_size(mp);
     105                 :          0 :         rte_dpaa_bpid_info[bpid].dpaa_ops_index = mp->ops_index;
     106                 :          0 :         rte_dpaa_bpid_info[bpid].ptov_off = 0;
     107                 :          0 :         rte_dpaa_bpid_info[bpid].flags = 0;
     108                 :            : 
     109                 :          0 :         bp_info = rte_malloc(NULL,
     110                 :            :                              sizeof(struct dpaa_bp_info),
     111                 :            :                              RTE_CACHE_LINE_SIZE);
     112         [ #  # ]:          0 :         if (!bp_info) {
     113                 :          0 :                 DPAA_MEMPOOL_WARN("Memory allocation failed for bp_info");
     114                 :          0 :                 bman_free_pool(bp);
     115                 :          0 :                 return -ENOMEM;
     116                 :            :         }
     117                 :            : 
     118         [ #  # ]:          0 :         rte_memcpy(bp_info, (void *)&rte_dpaa_bpid_info[bpid],
     119                 :            :                    sizeof(struct dpaa_bp_info));
     120                 :          0 :         mp->pool_data = (void *)bp_info;
     121                 :            : 
     122                 :          0 :         DPAA_MEMPOOL_INFO("BMAN pool created for bpid =%d", bpid);
     123                 :          0 :         return 0;
     124                 :            : }
     125                 :            : 
     126                 :            : static void
     127                 :          0 : dpaa_mbuf_free_pool(struct rte_mempool *mp)
     128                 :            : {
     129                 :          0 :         struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
     130                 :            : 
     131                 :          0 :         MEMPOOL_INIT_FUNC_TRACE();
     132                 :            : 
     133         [ #  # ]:          0 :         if (bp_info) {
     134                 :          0 :                 bman_free_pool(bp_info->bp);
     135                 :          0 :                 DPAA_MEMPOOL_INFO("BMAN pool freed for bpid =%d",
     136                 :            :                                   bp_info->bpid);
     137                 :          0 :                 rte_free(mp->pool_data);
     138                 :          0 :                 bp_info->bp = NULL;
     139                 :          0 :                 mp->pool_data = NULL;
     140                 :            :         }
     141                 :          0 : }
     142                 :            : 
     143                 :            : static void
     144                 :          0 : dpaa_buf_free(struct dpaa_bp_info *bp_info, uint64_t addr)
     145                 :            : {
     146                 :            :         struct bm_buffer buf;
     147                 :            :         int ret;
     148                 :            : 
     149                 :            :         DPAA_MEMPOOL_DPDEBUG("Free 0x%" PRIx64 " to bpid: %d",
     150                 :            :                            addr, bp_info->bpid);
     151                 :            : 
     152                 :          0 :         bm_buffer_set64(&buf, addr);
     153                 :          0 : retry:
     154                 :          0 :         ret = bman_release(bp_info->bp, &buf, 1, 0);
     155         [ #  # ]:          0 :         if (ret) {
     156                 :          0 :                 DPAA_MEMPOOL_DEBUG("BMAN busy. Retrying...");
     157                 :            :                 cpu_spin(CPU_SPIN_BACKOFF_CYCLES);
     158                 :          0 :                 goto retry;
     159                 :            :         }
     160                 :          0 : }
     161                 :            : 
     162                 :            : static int
     163                 :          0 : dpaa_mbuf_free_bulk(struct rte_mempool *pool,
     164                 :            :                     void *const *obj_table,
     165                 :            :                     unsigned int n)
     166                 :            : {
     167                 :          0 :         struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
     168                 :            :         int ret;
     169                 :            :         unsigned int i = 0;
     170                 :            : 
     171                 :            :         DPAA_MEMPOOL_DPDEBUG("Request to free %d buffers in bpid = %d",
     172                 :            :                              n, bp_info->bpid);
     173                 :            : 
     174         [ #  # ]:          0 :         if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
     175                 :          0 :                 ret = rte_dpaa_portal_init((void *)0);
     176         [ #  # ]:          0 :                 if (ret) {
     177                 :          0 :                         DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
     178                 :            :                                          ret);
     179                 :          0 :                         return 0;
     180                 :            :                 }
     181                 :            :         }
     182                 :            : 
     183         [ #  # ]:          0 :         while (i < n) {
     184         [ #  # ]:          0 :                 uint64_t phy = rte_mempool_virt2iova(obj_table[i]);
     185                 :            : 
     186         [ #  # ]:          0 :                 if (unlikely(!bp_info->ptov_off)) {
     187                 :            :                         /* buffers are from single mem segment */
     188         [ #  # ]:          0 :                         if (bp_info->flags & DPAA_MPOOL_SINGLE_SEGMENT) {
     189                 :          0 :                                 bp_info->ptov_off = (size_t)obj_table[i] - phy;
     190                 :          0 :                                 rte_dpaa_bpid_info[bp_info->bpid].ptov_off
     191                 :          0 :                                                 = bp_info->ptov_off;
     192                 :            :                         }
     193                 :            :                 }
     194                 :            : 
     195                 :          0 :                 dpaa_buf_free(bp_info,
     196                 :          0 :                               (uint64_t)phy + bp_info->meta_data_size);
     197                 :          0 :                 i = i + 1;
     198                 :            :         }
     199                 :            : 
     200                 :            :         DPAA_MEMPOOL_DPDEBUG("freed %d buffers in bpid =%d",
     201                 :            :                              n, bp_info->bpid);
     202                 :            : 
     203                 :            :         return 0;
     204                 :            : }
     205                 :            : 
     206                 :            : static int
     207                 :          0 : dpaa_mbuf_alloc_bulk(struct rte_mempool *pool,
     208                 :            :                      void **obj_table,
     209                 :            :                      unsigned int count)
     210                 :            : {
     211                 :            :         struct rte_mbuf **m = (struct rte_mbuf **)obj_table;
     212                 :            :         struct bm_buffer bufs[DPAA_MBUF_MAX_ACQ_REL];
     213                 :            :         struct dpaa_bp_info *bp_info;
     214                 :            :         void *bufaddr;
     215                 :            :         int i, ret;
     216                 :            :         unsigned int n = 0;
     217                 :            : 
     218                 :          0 :         bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
     219                 :            : 
     220                 :            :         DPAA_MEMPOOL_DPDEBUG("Request to alloc %d buffers in bpid = %d",
     221                 :            :                              count, bp_info->bpid);
     222                 :            : 
     223         [ #  # ]:          0 :         if (unlikely(count >= (RTE_MEMPOOL_CACHE_MAX_SIZE * 2))) {
     224                 :          0 :                 DPAA_MEMPOOL_ERR("Unable to allocate requested (%u) buffers",
     225                 :            :                                  count);
     226                 :          0 :                 return -1;
     227                 :            :         }
     228                 :            : 
     229         [ #  # ]:          0 :         if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
     230                 :          0 :                 ret = rte_dpaa_portal_init((void *)0);
     231         [ #  # ]:          0 :                 if (ret) {
     232                 :          0 :                         DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
     233                 :            :                                          ret);
     234                 :          0 :                         return -1;
     235                 :            :                 }
     236                 :            :         }
     237                 :            : 
     238         [ #  # ]:          0 :         while (n < count) {
     239                 :            :                 /* Acquire is all-or-nothing, so we drain in 7s,
     240                 :            :                  * then the remainder.
     241                 :            :                  */
     242         [ #  # ]:          0 :                 if ((count - n) > DPAA_MBUF_MAX_ACQ_REL) {
     243                 :          0 :                         ret = bman_acquire(bp_info->bp, bufs,
     244                 :            :                                            DPAA_MBUF_MAX_ACQ_REL, 0);
     245                 :            :                 } else {
     246                 :          0 :                         ret = bman_acquire(bp_info->bp, bufs, count - n, 0);
     247                 :            :                 }
     248                 :            :                 /* In case of less than requested number of buffers available
     249                 :            :                  * in pool, qbman_swp_acquire returns 0
     250                 :            :                  */
     251         [ #  # ]:          0 :                 if (ret <= 0) {
     252                 :            :                         DPAA_MEMPOOL_DPDEBUG("Buffer acquire failed (%d)",
     253                 :            :                                              ret);
     254                 :            :                         /* The API expect the exact number of requested
     255                 :            :                          * buffers. Releasing all buffers allocated
     256                 :            :                          */
     257                 :          0 :                         dpaa_mbuf_free_bulk(pool, obj_table, n);
     258                 :          0 :                         return -ENOBUFS;
     259                 :            :                 }
     260                 :            :                 /* assigning mbuf from the acquired objects */
     261   [ #  #  #  # ]:          0 :                 for (i = 0; (i < ret) && bufs[i].addr; i++) {
     262                 :            :                         /* TODO-errata - observed that bufs may be null
     263                 :            :                          * i.e. first buffer is valid, remaining 6 buffers
     264                 :            :                          * may be null.
     265                 :            :                          */
     266                 :          0 :                         bufaddr = DPAA_MEMPOOL_PTOV(bp_info, bufs[i].addr);
     267                 :          0 :                         m[n] = (struct rte_mbuf *)((char *)bufaddr
     268                 :          0 :                                                 - bp_info->meta_data_size);
     269                 :            :                         DPAA_MEMPOOL_DPDEBUG("Paddr (%p), FD (%p) from BMAN",
     270                 :            :                                              (void *)bufaddr, (void *)m[n]);
     271                 :          0 :                         n++;
     272                 :            :                 }
     273                 :            :         }
     274                 :            : 
     275                 :            :         DPAA_MEMPOOL_DPDEBUG("Allocated %d buffers from bpid=%d",
     276                 :            :                              n, bp_info->bpid);
     277                 :            :         return 0;
     278                 :            : }
     279                 :            : 
     280                 :            : static unsigned int
     281                 :          0 : dpaa_mbuf_get_count(const struct rte_mempool *mp)
     282                 :            : {
     283                 :            :         struct dpaa_bp_info *bp_info;
     284                 :            : 
     285                 :          0 :         MEMPOOL_INIT_FUNC_TRACE();
     286                 :            : 
     287   [ #  #  #  # ]:          0 :         if (!mp || !mp->pool_data) {
     288                 :          0 :                 DPAA_MEMPOOL_ERR("Invalid mempool provided\n");
     289                 :          0 :                 return 0;
     290                 :            :         }
     291                 :            : 
     292                 :            :         bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
     293                 :            : 
     294                 :          0 :         return bman_query_free_buffers(bp_info->bp);
     295                 :            : }
     296                 :            : 
     297                 :            : static int
     298                 :          0 : dpaa_populate(struct rte_mempool *mp, unsigned int max_objs,
     299                 :            :               void *vaddr, rte_iova_t paddr, size_t len,
     300                 :            :               rte_mempool_populate_obj_cb_t *obj_cb, void *obj_cb_arg)
     301                 :            : {
     302                 :            :         struct dpaa_bp_info *bp_info;
     303                 :            :         unsigned int total_elt_sz;
     304                 :            : 
     305   [ #  #  #  # ]:          0 :         if (!mp || !mp->pool_data) {
     306                 :          0 :                 DPAA_MEMPOOL_ERR("Invalid mempool provided\n");
     307                 :          0 :                 return 0;
     308                 :            :         }
     309                 :            : 
     310                 :            :         /* Update the PA-VA Table */
     311                 :          0 :         dpaax_iova_table_update(paddr, vaddr, len);
     312                 :            : 
     313                 :          0 :         bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
     314                 :          0 :         total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
     315                 :            : 
     316                 :            :         DPAA_MEMPOOL_DPDEBUG("Req size %" PRIx64 " vs Available %u\n",
     317                 :            :                            (uint64_t)len, total_elt_sz * mp->size);
     318                 :            : 
     319                 :            :         /* Detect pool area has sufficient space for elements in this memzone */
     320         [ #  # ]:          0 :         if (len >= total_elt_sz * mp->size)
     321                 :          0 :                 bp_info->flags |= DPAA_MPOOL_SINGLE_SEGMENT;
     322                 :            :         struct dpaa_memseg *ms;
     323                 :            : 
     324                 :            :         /* For each memory chunk pinned to the Mempool, a linked list of the
     325                 :            :          * contained memsegs is created for searching when PA to VA
     326                 :            :          * conversion is required.
     327                 :            :          */
     328                 :          0 :         ms = rte_zmalloc(NULL, sizeof(struct dpaa_memseg), 0);
     329         [ #  # ]:          0 :         if (!ms) {
     330                 :          0 :                 DPAA_MEMPOOL_ERR("Unable to allocate internal memory.");
     331                 :          0 :                 DPAA_MEMPOOL_WARN("Fast Physical to Virtual Addr translation would not be available.");
     332                 :            :                 /* If the element is not added, it would only lead to failure
     333                 :            :                  * in searching for the element and the logic would Fallback
     334                 :            :                  * to traditional DPDK memseg traversal code. So, this is not
     335                 :            :                  * a blocking error - but, error would be printed on screen.
     336                 :            :                  */
     337                 :          0 :                 return 0;
     338                 :            :         }
     339                 :            : 
     340                 :          0 :         ms->vaddr = vaddr;
     341                 :          0 :         ms->iova = paddr;
     342                 :          0 :         ms->len = len;
     343                 :            :         /* Head insertions are generally faster than tail insertions as the
     344                 :            :          * buffers pinned are picked from rear end.
     345                 :            :          */
     346         [ #  # ]:          0 :         TAILQ_INSERT_HEAD(&rte_dpaa_memsegs, ms, next);
     347                 :            : 
     348                 :          0 :         return rte_mempool_op_populate_helper(mp, 0, max_objs, vaddr, paddr,
     349                 :            :                                                len, obj_cb, obj_cb_arg);
     350                 :            : }
     351                 :            : 
     352                 :            : static const struct rte_mempool_ops dpaa_mpool_ops = {
     353                 :            :         .name = DPAA_MEMPOOL_OPS_NAME,
     354                 :            :         .alloc = dpaa_mbuf_create_pool,
     355                 :            :         .free = dpaa_mbuf_free_pool,
     356                 :            :         .enqueue = dpaa_mbuf_free_bulk,
     357                 :            :         .dequeue = dpaa_mbuf_alloc_bulk,
     358                 :            :         .get_count = dpaa_mbuf_get_count,
     359                 :            :         .populate = dpaa_populate,
     360                 :            : };
     361                 :            : 
     362                 :        235 : RTE_MEMPOOL_REGISTER_OPS(dpaa_mpool_ops);

Generated by: LCOV version 1.14