mempool: prevent objects from being across pages
[dpdk.git] / drivers / mempool / octeontx / rte_mempool_octeontx.c
index 42d93b8..bd00700 100644 (file)
@@ -1,34 +1,7 @@
-/*
- *   BSD LICENSE
- *
- *   Copyright (C) 2017 Cavium Inc. All rights reserved.
- *
- *   Redistribution and use in source and binary forms, with or without
- *   modification, are permitted provided that the following conditions
- *   are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in
- *       the documentation and/or other materials provided with the
- *       distribution.
- *     * Neither the name of Intel Corporation nor the names of its
- *       contributors may be used to endorse or promote products derived
- *       from this software without specific prior written permission.
- *
- *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2017 Cavium, Inc
  */
+
 #include <stdio.h>
 #include <rte_mempool.h>
 #include <rte_malloc.h>
@@ -42,17 +15,12 @@ octeontx_fpavf_alloc(struct rte_mempool *mp)
        uintptr_t pool;
        uint32_t memseg_count = mp->size;
        uint32_t object_size;
-       uintptr_t va_start;
        int rc = 0;
 
-       /* virtual hugepage mapped addr */
-       va_start = ~(uint64_t)0;
-
        object_size = mp->elt_size + mp->header_size + mp->trailer_size;
 
        pool = octeontx_fpa_bufpool_create(object_size, memseg_count,
                                                OCTEONTX_FPAVF_BUF_OFFSET,
-                                               (char **)&va_start,
                                                mp->socket_id);
        rc = octeontx_fpa_bufpool_block_size(pool);
        if (rc < 0)
@@ -78,7 +46,6 @@ static void
 octeontx_fpavf_free(struct rte_mempool *mp)
 {
        uintptr_t pool;
-
        pool = (uintptr_t)mp->pool_id;
 
        octeontx_fpa_bufpool_destroy(pool, mp->socket_id);
@@ -159,6 +126,71 @@ octeontx_fpavf_get_count(const struct rte_mempool *mp)
        return octeontx_fpa_bufpool_free_count(pool);
 }
 
+static ssize_t
+octeontx_fpavf_calc_mem_size(const struct rte_mempool *mp,
+                            uint32_t obj_num, uint32_t pg_shift,
+                            size_t *min_chunk_size, size_t *align)
+{
+       ssize_t mem_size;
+       size_t total_elt_sz;
+
+       /* Need space for one more obj on each chunk to fulfill
+        * alignment requirements.
+        */
+       total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
+       mem_size = rte_mempool_op_calc_mem_size_helper(mp, obj_num, pg_shift,
+                                               total_elt_sz, min_chunk_size,
+                                               align);
+       if (mem_size >= 0) {
+               /*
+                * Memory area which contains objects must be physically
+                * contiguous.
+                */
+               *min_chunk_size = mem_size;
+       }
+
+       return mem_size;
+}
+
+static int
+octeontx_fpavf_populate(struct rte_mempool *mp, unsigned int max_objs,
+                       void *vaddr, rte_iova_t iova, size_t len,
+                       rte_mempool_populate_obj_cb_t *obj_cb, void *obj_cb_arg)
+{
+       size_t total_elt_sz;
+       size_t off;
+       uint8_t gpool;
+       uintptr_t pool_bar;
+       int ret;
+
+       if (iova == RTE_BAD_IOVA)
+               return -EINVAL;
+
+       total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
+
+       /* align object start address to a multiple of total_elt_sz */
+       off = total_elt_sz - ((((uintptr_t)vaddr - 1) % total_elt_sz) + 1);
+
+       if (len < off)
+               return -EINVAL;
+
+       vaddr = (char *)vaddr + off;
+       iova += off;
+       len -= off;
+
+       gpool = octeontx_fpa_bufpool_gpool(mp->pool_id);
+       pool_bar = mp->pool_id & ~(uint64_t)FPA_GPOOL_MASK;
+
+       ret = octeontx_fpavf_pool_set_range(pool_bar, len, vaddr, gpool);
+       if (ret < 0)
+               return ret;
+
+       return rte_mempool_op_populate_helper(mp,
+                                       RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ,
+                                       max_objs, vaddr, iova, len,
+                                       obj_cb, obj_cb_arg);
+}
+
 static struct rte_mempool_ops octeontx_fpavf_ops = {
        .name = "octeontx_fpavf",
        .alloc = octeontx_fpavf_alloc,
@@ -166,8 +198,8 @@ static struct rte_mempool_ops octeontx_fpavf_ops = {
        .enqueue = octeontx_fpavf_enqueue,
        .dequeue = octeontx_fpavf_dequeue,
        .get_count = octeontx_fpavf_get_count,
-       .get_capabilities = NULL,
-       .register_memory_area = NULL,
+       .calc_mem_size = octeontx_fpavf_calc_mem_size,
+       .populate = octeontx_fpavf_populate,
 };
 
 MEMPOOL_REGISTER_OPS(octeontx_fpavf_ops);