1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Cavium, Inc
6 #include <rte_mempool.h>
7 #include <rte_malloc.h>
10 #include "octeontx_fpavf.h"
13 * Per-pool descriptor.
14 * Links mempool with the corresponding memzone,
15 * that provides memory under the pool's elements.
17 struct octeontx_pool_info {
18 const struct rte_mempool *mp;
21 SLIST_ENTRY(octeontx_pool_info) link;
24 SLIST_HEAD(octeontx_pool_list, octeontx_pool_info);
26 /* List of the allocated pools */
27 static struct octeontx_pool_list octeontx_pool_head =
28 SLIST_HEAD_INITIALIZER(octeontx_pool_head);
29 /* Spinlock to protect pool list */
30 static rte_spinlock_t pool_list_lock = RTE_SPINLOCK_INITIALIZER;
33 octeontx_fpavf_alloc(struct rte_mempool *mp)
36 struct octeontx_pool_info *pool_info;
37 uint32_t memseg_count = mp->size;
42 rte_spinlock_lock(&pool_list_lock);
43 SLIST_FOREACH(pool_info, &octeontx_pool_head, link) {
44 if (pool_info->mp == mp)
47 if (pool_info == NULL) {
48 rte_spinlock_unlock(&pool_list_lock);
52 /* virtual hugepage mapped addr */
53 va_start = pool_info->mz_addr;
54 rte_spinlock_unlock(&pool_list_lock);
56 object_size = mp->elt_size + mp->header_size + mp->trailer_size;
58 pool = octeontx_fpa_bufpool_create(object_size, memseg_count,
59 OCTEONTX_FPAVF_BUF_OFFSET,
62 rc = octeontx_fpa_bufpool_block_size(pool);
66 if ((uint32_t)rc != object_size)
67 fpavf_log_err("buffer size mismatch: %d instead of %u\n",
70 fpavf_log_info("Pool created %p with .. ", (void *)pool);
71 fpavf_log_info("obj_sz %d, cnt %d\n", object_size, memseg_count);
73 /* assign pool handle to mempool */
74 mp->pool_id = (uint64_t)pool;
83 octeontx_fpavf_free(struct rte_mempool *mp)
85 struct octeontx_pool_info *pool_info;
88 pool = (uintptr_t)mp->pool_id;
90 rte_spinlock_lock(&pool_list_lock);
91 SLIST_FOREACH(pool_info, &octeontx_pool_head, link) {
92 if (pool_info->mp == mp)
96 if (pool_info == NULL) {
97 rte_spinlock_unlock(&pool_list_lock);
98 rte_panic("%s: trying to free pool with no valid metadata",
102 SLIST_REMOVE(&octeontx_pool_head, pool_info, octeontx_pool_info, link);
103 rte_spinlock_unlock(&pool_list_lock);
106 octeontx_fpa_bufpool_destroy(pool, mp->socket_id);
109 static __rte_always_inline void *
110 octeontx_fpa_bufpool_alloc(uintptr_t handle)
112 return (void *)(uintptr_t)fpavf_read64((void *)(handle +
113 FPA_VF_VHAURA_OP_ALLOC(0)));
116 static __rte_always_inline void
117 octeontx_fpa_bufpool_free(uintptr_t handle, void *buf)
119 uint64_t free_addr = FPA_VF_FREE_ADDRS_S(FPA_VF_VHAURA_OP_FREE(0),
120 0 /* DWB */, 1 /* FABS */);
122 fpavf_write64((uintptr_t)buf, (void *)(uintptr_t)(handle + free_addr));
126 octeontx_fpavf_enqueue(struct rte_mempool *mp, void * const *obj_table,
132 pool = (uintptr_t)mp->pool_id;
133 /* Get pool bar address from handle */
134 pool &= ~(uint64_t)FPA_GPOOL_MASK;
135 for (index = 0; index < n; index++, obj_table++)
136 octeontx_fpa_bufpool_free(pool, *obj_table);
142 octeontx_fpavf_dequeue(struct rte_mempool *mp, void **obj_table,
149 pool = (uintptr_t)mp->pool_id;
150 /* Get pool bar address from handle */
151 pool &= ~(uint64_t)FPA_GPOOL_MASK;
152 for (index = 0; index < n; index++, obj_table++) {
153 obj = octeontx_fpa_bufpool_alloc(pool);
156 * Failed to allocate the requested number of objects
157 * from the pool. Current pool implementation requires
158 * completing the entire request or returning error
160 * Free already allocated buffers to the pool.
162 for (; index > 0; index--) {
164 octeontx_fpa_bufpool_free(pool, *obj_table);
175 octeontx_fpavf_get_count(const struct rte_mempool *mp)
179 pool = (uintptr_t)mp->pool_id;
181 return octeontx_fpa_bufpool_free_count(pool);
185 octeontx_fpavf_get_capabilities(const struct rte_mempool *mp,
189 *flags |= (MEMPOOL_F_CAPA_PHYS_CONTIG |
190 MEMPOOL_F_CAPA_BLK_ALIGNED_OBJECTS);
195 octeontx_fpavf_register_memory_area(const struct rte_mempool *mp,
196 char *vaddr, rte_iova_t paddr, size_t len)
198 struct octeontx_pool_info *pool_info;
203 pool_info = rte_malloc("octeontx_pool_info", sizeof(*pool_info), 0);
204 if (pool_info == NULL)
208 pool_info->mz_addr = (uintptr_t)vaddr;
209 rte_spinlock_lock(&pool_list_lock);
210 SLIST_INSERT_HEAD(&octeontx_pool_head, pool_info, link);
211 rte_spinlock_unlock(&pool_list_lock);
215 static struct rte_mempool_ops octeontx_fpavf_ops = {
216 .name = "octeontx_fpavf",
217 .alloc = octeontx_fpavf_alloc,
218 .free = octeontx_fpavf_free,
219 .enqueue = octeontx_fpavf_enqueue,
220 .dequeue = octeontx_fpavf_dequeue,
221 .get_count = octeontx_fpavf_get_count,
222 .get_capabilities = octeontx_fpavf_get_capabilities,
223 .register_memory_area = octeontx_fpavf_register_memory_area,
226 MEMPOOL_REGISTER_OPS(octeontx_fpavf_ops);