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;
76 rte_dpaa_bpid_info[bpid].ptov_off = 0;
77 rte_dpaa_bpid_info[bpid].flags = 0;
79 bp_info = rte_malloc(NULL,
80 sizeof(struct dpaa_bp_info),
83 DPAA_MEMPOOL_WARN("Memory allocation failed for bp_info");
88 rte_memcpy(bp_info, (void *)&rte_dpaa_bpid_info[bpid],
89 sizeof(struct dpaa_bp_info));
90 mp->pool_data = (void *)bp_info;
92 DPAA_MEMPOOL_INFO("BMAN pool created for bpid =%d", bpid);
97 dpaa_mbuf_free_pool(struct rte_mempool *mp)
99 struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
101 MEMPOOL_INIT_FUNC_TRACE();
104 bman_free_pool(bp_info->bp);
105 DPAA_MEMPOOL_INFO("BMAN pool freed for bpid =%d",
107 rte_free(mp->pool_data);
108 mp->pool_data = NULL;
113 dpaa_buf_free(struct dpaa_bp_info *bp_info, uint64_t addr)
115 struct bm_buffer buf;
118 DPAA_MEMPOOL_DEBUG("Free 0x%" PRIx64 " to bpid: %d",
119 addr, bp_info->bpid);
121 bm_buffer_set64(&buf, addr);
123 ret = bman_release(bp_info->bp, &buf, 1, 0);
125 DPAA_MEMPOOL_DEBUG("BMAN busy. Retrying...");
126 cpu_spin(CPU_SPIN_BACKOFF_CYCLES);
132 dpaa_mbuf_free_bulk(struct rte_mempool *pool,
133 void *const *obj_table,
136 struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
140 DPAA_MEMPOOL_DPDEBUG("Request to free %d buffers in bpid = %d",
143 if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
144 ret = rte_dpaa_portal_init((void *)0);
146 DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
153 uint64_t phy = rte_mempool_virt2iova(obj_table[i]);
155 if (unlikely(!bp_info->ptov_off)) {
156 /* buffers are from single mem segment */
157 if (bp_info->flags & DPAA_MPOOL_SINGLE_SEGMENT) {
158 bp_info->ptov_off = (size_t)obj_table[i] - phy;
159 rte_dpaa_bpid_info[bp_info->bpid].ptov_off
164 dpaa_buf_free(bp_info,
165 (uint64_t)phy + bp_info->meta_data_size);
169 DPAA_MEMPOOL_DPDEBUG("freed %d buffers in bpid =%d",
176 dpaa_mbuf_alloc_bulk(struct rte_mempool *pool,
180 struct rte_mbuf **m = (struct rte_mbuf **)obj_table;
181 struct bm_buffer bufs[DPAA_MBUF_MAX_ACQ_REL];
182 struct dpaa_bp_info *bp_info;
187 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
189 DPAA_MEMPOOL_DPDEBUG("Request to alloc %d buffers in bpid = %d",
190 count, bp_info->bpid);
192 if (unlikely(count >= (RTE_MEMPOOL_CACHE_MAX_SIZE * 2))) {
193 DPAA_MEMPOOL_ERR("Unable to allocate requested (%u) buffers",
198 if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
199 ret = rte_dpaa_portal_init((void *)0);
201 DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
208 /* Acquire is all-or-nothing, so we drain in 7s,
209 * then the remainder.
211 if ((count - n) > DPAA_MBUF_MAX_ACQ_REL) {
212 ret = bman_acquire(bp_info->bp, bufs,
213 DPAA_MBUF_MAX_ACQ_REL, 0);
215 ret = bman_acquire(bp_info->bp, bufs, count - n, 0);
217 /* In case of less than requested number of buffers available
218 * in pool, qbman_swp_acquire returns 0
221 DPAA_MEMPOOL_DPDEBUG("Buffer acquire failed (%d)",
223 /* The API expect the exact number of requested
224 * buffers. Releasing all buffers allocated
226 dpaa_mbuf_free_bulk(pool, obj_table, n);
229 /* assigning mbuf from the acquired objects */
230 for (i = 0; (i < ret) && bufs[i].addr; i++) {
231 /* TODO-errata - objerved that bufs may be null
232 * i.e. first buffer is valid, remaining 6 buffers
235 bufaddr = DPAA_MEMPOOL_PTOV(bp_info, bufs[i].addr);
236 m[n] = (struct rte_mbuf *)((char *)bufaddr
237 - bp_info->meta_data_size);
238 DPAA_MEMPOOL_DPDEBUG("Paddr (%p), FD (%p) from BMAN",
239 (void *)bufaddr, (void *)m[n]);
244 DPAA_MEMPOOL_DPDEBUG("Allocated %d buffers from bpid=%d",
250 dpaa_mbuf_get_count(const struct rte_mempool *mp)
252 struct dpaa_bp_info *bp_info;
254 MEMPOOL_INIT_FUNC_TRACE();
256 if (!mp || !mp->pool_data) {
257 DPAA_MEMPOOL_ERR("Invalid mempool provided\n");
261 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
263 return bman_query_free_buffers(bp_info->bp);
267 dpaa_populate(struct rte_mempool *mp, unsigned int max_objs,
268 void *vaddr, rte_iova_t paddr, size_t len,
269 rte_mempool_populate_obj_cb_t *obj_cb, void *obj_cb_arg)
271 struct dpaa_bp_info *bp_info;
272 unsigned int total_elt_sz;
274 MEMPOOL_INIT_FUNC_TRACE();
276 if (!mp || !mp->pool_data) {
277 DPAA_MEMPOOL_ERR("Invalid mempool provided\n");
281 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
282 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
284 DPAA_MEMPOOL_DEBUG("Req size %" PRIx64 " vs Available %u\n",
285 (uint64_t)len, total_elt_sz * mp->size);
287 /* Detect pool area has sufficient space for elements in this memzone */
288 if (len >= total_elt_sz * mp->size)
289 bp_info->flags |= DPAA_MPOOL_SINGLE_SEGMENT;
291 return rte_mempool_op_populate_default(mp, max_objs, vaddr, paddr, len,
296 struct rte_mempool_ops dpaa_mpool_ops = {
297 .name = DPAA_MEMPOOL_OPS_NAME,
298 .alloc = dpaa_mbuf_create_pool,
299 .free = dpaa_mbuf_free_pool,
300 .enqueue = dpaa_mbuf_free_bulk,
301 .dequeue = dpaa_mbuf_alloc_bulk,
302 .get_count = dpaa_mbuf_get_count,
303 .populate = dpaa_populate,
306 MEMPOOL_REGISTER_OPS(dpaa_mpool_ops);