1 /* SPDX-License-Identifier: BSD-3-Clause
15 #include <sys/types.h>
16 #include <sys/syscall.h>
18 #include <rte_byteorder.h>
19 #include <rte_common.h>
21 #include <rte_debug.h>
22 #include <rte_memory.h>
23 #include <rte_tailq.h>
25 #include <rte_malloc.h>
28 #include <dpaa_mempool.h>
30 struct dpaa_bp_info rte_dpaa_bpid_info[DPAA_MAX_BPOOLS];
33 dpaa_mbuf_create_pool(struct rte_mempool *mp)
36 struct bm_buffer bufs[8];
37 struct dpaa_bp_info *bp_info;
39 int num_bufs = 0, ret = 0;
40 struct bman_pool_params params = {
41 .flags = BMAN_POOL_FLAG_DYNAMIC_BPID
44 MEMPOOL_INIT_FUNC_TRACE();
46 bp = bman_new_pool(¶ms);
48 DPAA_MEMPOOL_ERR("bman_new_pool() failed");
51 bpid = bman_get_params(bp)->bpid;
53 /* Drain the pool of anything already in it. */
55 /* Acquire is all-or-nothing, so we drain in 8s,
56 * then in 1s for the remainder.
59 ret = bman_acquire(bp, bufs, 8, 0);
61 ret = bman_acquire(bp, bufs, 1, 0);
66 DPAA_MEMPOOL_WARN("drained %u bufs from BPID %d",
69 rte_dpaa_bpid_info[bpid].mp = mp;
70 rte_dpaa_bpid_info[bpid].bpid = bpid;
71 rte_dpaa_bpid_info[bpid].size = mp->elt_size;
72 rte_dpaa_bpid_info[bpid].bp = bp;
73 rte_dpaa_bpid_info[bpid].meta_data_size =
74 sizeof(struct rte_mbuf) + rte_pktmbuf_priv_size(mp);
75 rte_dpaa_bpid_info[bpid].dpaa_ops_index = mp->ops_index;
77 bp_info = rte_malloc(NULL,
78 sizeof(struct dpaa_bp_info),
81 DPAA_MEMPOOL_WARN("Memory allocation failed for bp_info");
86 rte_memcpy(bp_info, (void *)&rte_dpaa_bpid_info[bpid],
87 sizeof(struct dpaa_bp_info));
88 mp->pool_data = (void *)bp_info;
90 DPAA_MEMPOOL_INFO("BMAN pool created for bpid =%d", bpid);
95 dpaa_mbuf_free_pool(struct rte_mempool *mp)
97 struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
99 MEMPOOL_INIT_FUNC_TRACE();
102 bman_free_pool(bp_info->bp);
103 DPAA_MEMPOOL_INFO("BMAN pool freed for bpid =%d",
105 rte_free(mp->pool_data);
106 mp->pool_data = NULL;
111 dpaa_buf_free(struct dpaa_bp_info *bp_info, uint64_t addr)
113 struct bm_buffer buf;
116 DPAA_MEMPOOL_DEBUG("Free 0x%lx to bpid: %d", addr, bp_info->bpid);
118 bm_buffer_set64(&buf, addr);
120 ret = bman_release(bp_info->bp, &buf, 1, 0);
122 DPAA_MEMPOOL_DEBUG("BMAN busy. Retrying...");
123 cpu_spin(CPU_SPIN_BACKOFF_CYCLES);
129 dpaa_mbuf_free_bulk(struct rte_mempool *pool,
130 void *const *obj_table,
133 struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
137 DPAA_MEMPOOL_DPDEBUG("Request to free %d buffers in bpid = %d",
140 ret = rte_dpaa_portal_init((void *)0);
142 DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
148 dpaa_buf_free(bp_info,
149 (uint64_t)rte_mempool_virt2iova(obj_table[i]) +
150 bp_info->meta_data_size);
154 DPAA_MEMPOOL_DPDEBUG("freed %d buffers in bpid =%d",
161 dpaa_mbuf_alloc_bulk(struct rte_mempool *pool,
165 struct rte_mbuf **m = (struct rte_mbuf **)obj_table;
166 struct bm_buffer bufs[DPAA_MBUF_MAX_ACQ_REL];
167 struct dpaa_bp_info *bp_info;
172 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
174 DPAA_MEMPOOL_DPDEBUG("Request to alloc %d buffers in bpid = %d",
175 count, bp_info->bpid);
177 if (unlikely(count >= (RTE_MEMPOOL_CACHE_MAX_SIZE * 2))) {
178 DPAA_MEMPOOL_ERR("Unable to allocate requested (%u) buffers",
183 ret = rte_dpaa_portal_init((void *)0);
185 DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
191 /* Acquire is all-or-nothing, so we drain in 7s,
192 * then the remainder.
194 if ((count - n) > DPAA_MBUF_MAX_ACQ_REL) {
195 ret = bman_acquire(bp_info->bp, bufs,
196 DPAA_MBUF_MAX_ACQ_REL, 0);
198 ret = bman_acquire(bp_info->bp, bufs, count - n, 0);
200 /* In case of less than requested number of buffers available
201 * in pool, qbman_swp_acquire returns 0
204 DPAA_MEMPOOL_DPDEBUG("Buffer acquire failed (%d)",
206 /* The API expect the exact number of requested
207 * buffers. Releasing all buffers allocated
209 dpaa_mbuf_free_bulk(pool, obj_table, n);
212 /* assigning mbuf from the acquired objects */
213 for (i = 0; (i < ret) && bufs[i].addr; i++) {
214 /* TODO-errata - objerved that bufs may be null
215 * i.e. first buffer is valid, remaining 6 buffers
218 bufaddr = (void *)rte_dpaa_mem_ptov(bufs[i].addr);
219 m[n] = (struct rte_mbuf *)((char *)bufaddr
220 - bp_info->meta_data_size);
221 DPAA_MEMPOOL_DPDEBUG("Paddr (%p), FD (%p) from BMAN",
222 (void *)bufaddr, (void *)m[n]);
227 DPAA_MEMPOOL_DPDEBUG("Allocated %d buffers from bpid=%d",
233 dpaa_mbuf_get_count(const struct rte_mempool *mp)
235 struct dpaa_bp_info *bp_info;
237 MEMPOOL_INIT_FUNC_TRACE();
239 if (!mp || !mp->pool_data) {
240 DPAA_MEMPOOL_ERR("Invalid mempool provided\n");
244 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
246 return bman_query_free_buffers(bp_info->bp);
249 struct rte_mempool_ops dpaa_mpool_ops = {
251 .alloc = dpaa_mbuf_create_pool,
252 .free = dpaa_mbuf_free_pool,
253 .enqueue = dpaa_mbuf_free_bulk,
254 .dequeue = dpaa_mbuf_alloc_bulk,
255 .get_count = dpaa_mbuf_get_count,
258 MEMPOOL_REGISTER_OPS(dpaa_mpool_ops);