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>
29 #include <dpaax_iova_table.h>
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
35 struct dpaa_memseg_list rte_dpaa_memsegs
36 = TAILQ_HEAD_INITIALIZER(rte_dpaa_memsegs);
38 struct dpaa_bp_info *rte_dpaa_bpid_info;
41 dpaa_mbuf_create_pool(struct rte_mempool *mp)
44 struct bm_buffer bufs[8];
45 struct dpaa_bp_info *bp_info;
47 int num_bufs = 0, ret = 0;
48 struct bman_pool_params params = {
49 .flags = BMAN_POOL_FLAG_DYNAMIC_BPID
52 MEMPOOL_INIT_FUNC_TRACE();
54 bp = bman_new_pool(¶ms);
56 DPAA_MEMPOOL_ERR("bman_new_pool() failed");
59 bpid = bman_get_params(bp)->bpid;
61 /* Drain the pool of anything already in it. */
63 /* Acquire is all-or-nothing, so we drain in 8s,
64 * then in 1s for the remainder.
67 ret = bman_acquire(bp, bufs, 8, 0);
69 ret = bman_acquire(bp, bufs, 1, 0);
74 DPAA_MEMPOOL_WARN("drained %u bufs from BPID %d",
77 if (rte_dpaa_bpid_info == NULL) {
78 rte_dpaa_bpid_info = (struct dpaa_bp_info *)rte_zmalloc(NULL,
79 sizeof(struct dpaa_bp_info) * DPAA_MAX_BPOOLS,
81 if (rte_dpaa_bpid_info == NULL) {
87 rte_dpaa_bpid_info[bpid].mp = mp;
88 rte_dpaa_bpid_info[bpid].bpid = bpid;
89 rte_dpaa_bpid_info[bpid].size = mp->elt_size;
90 rte_dpaa_bpid_info[bpid].bp = bp;
91 rte_dpaa_bpid_info[bpid].meta_data_size =
92 sizeof(struct rte_mbuf) + rte_pktmbuf_priv_size(mp);
93 rte_dpaa_bpid_info[bpid].dpaa_ops_index = mp->ops_index;
94 rte_dpaa_bpid_info[bpid].ptov_off = 0;
95 rte_dpaa_bpid_info[bpid].flags = 0;
97 bp_info = rte_malloc(NULL,
98 sizeof(struct dpaa_bp_info),
101 DPAA_MEMPOOL_WARN("Memory allocation failed for bp_info");
106 rte_memcpy(bp_info, (void *)&rte_dpaa_bpid_info[bpid],
107 sizeof(struct dpaa_bp_info));
108 mp->pool_data = (void *)bp_info;
110 DPAA_MEMPOOL_INFO("BMAN pool created for bpid =%d", bpid);
115 dpaa_mbuf_free_pool(struct rte_mempool *mp)
117 struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
119 MEMPOOL_INIT_FUNC_TRACE();
122 bman_free_pool(bp_info->bp);
123 DPAA_MEMPOOL_INFO("BMAN pool freed for bpid =%d",
125 rte_free(mp->pool_data);
126 mp->pool_data = NULL;
131 dpaa_buf_free(struct dpaa_bp_info *bp_info, uint64_t addr)
133 struct bm_buffer buf;
136 DPAA_MEMPOOL_DPDEBUG("Free 0x%" PRIx64 " to bpid: %d",
137 addr, bp_info->bpid);
139 bm_buffer_set64(&buf, addr);
141 ret = bman_release(bp_info->bp, &buf, 1, 0);
143 DPAA_MEMPOOL_DEBUG("BMAN busy. Retrying...");
144 cpu_spin(CPU_SPIN_BACKOFF_CYCLES);
150 dpaa_mbuf_free_bulk(struct rte_mempool *pool,
151 void *const *obj_table,
154 struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
158 DPAA_MEMPOOL_DPDEBUG("Request to free %d buffers in bpid = %d",
161 if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
162 ret = rte_dpaa_portal_init((void *)0);
164 DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
171 uint64_t phy = rte_mempool_virt2iova(obj_table[i]);
173 if (unlikely(!bp_info->ptov_off)) {
174 /* buffers are from single mem segment */
175 if (bp_info->flags & DPAA_MPOOL_SINGLE_SEGMENT) {
176 bp_info->ptov_off = (size_t)obj_table[i] - phy;
177 rte_dpaa_bpid_info[bp_info->bpid].ptov_off
182 dpaa_buf_free(bp_info,
183 (uint64_t)phy + bp_info->meta_data_size);
187 DPAA_MEMPOOL_DPDEBUG("freed %d buffers in bpid =%d",
194 dpaa_mbuf_alloc_bulk(struct rte_mempool *pool,
198 struct rte_mbuf **m = (struct rte_mbuf **)obj_table;
199 struct bm_buffer bufs[DPAA_MBUF_MAX_ACQ_REL];
200 struct dpaa_bp_info *bp_info;
205 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
207 DPAA_MEMPOOL_DPDEBUG("Request to alloc %d buffers in bpid = %d",
208 count, bp_info->bpid);
210 if (unlikely(count >= (RTE_MEMPOOL_CACHE_MAX_SIZE * 2))) {
211 DPAA_MEMPOOL_ERR("Unable to allocate requested (%u) buffers",
216 if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
217 ret = rte_dpaa_portal_init((void *)0);
219 DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
226 /* Acquire is all-or-nothing, so we drain in 7s,
227 * then the remainder.
229 if ((count - n) > DPAA_MBUF_MAX_ACQ_REL) {
230 ret = bman_acquire(bp_info->bp, bufs,
231 DPAA_MBUF_MAX_ACQ_REL, 0);
233 ret = bman_acquire(bp_info->bp, bufs, count - n, 0);
235 /* In case of less than requested number of buffers available
236 * in pool, qbman_swp_acquire returns 0
239 DPAA_MEMPOOL_DPDEBUG("Buffer acquire failed (%d)",
241 /* The API expect the exact number of requested
242 * buffers. Releasing all buffers allocated
244 dpaa_mbuf_free_bulk(pool, obj_table, n);
247 /* assigning mbuf from the acquired objects */
248 for (i = 0; (i < ret) && bufs[i].addr; i++) {
249 /* TODO-errata - objerved that bufs may be null
250 * i.e. first buffer is valid, remaining 6 buffers
253 bufaddr = DPAA_MEMPOOL_PTOV(bp_info, bufs[i].addr);
254 m[n] = (struct rte_mbuf *)((char *)bufaddr
255 - bp_info->meta_data_size);
256 DPAA_MEMPOOL_DPDEBUG("Paddr (%p), FD (%p) from BMAN",
257 (void *)bufaddr, (void *)m[n]);
262 DPAA_MEMPOOL_DPDEBUG("Allocated %d buffers from bpid=%d",
268 dpaa_mbuf_get_count(const struct rte_mempool *mp)
270 struct dpaa_bp_info *bp_info;
272 MEMPOOL_INIT_FUNC_TRACE();
274 if (!mp || !mp->pool_data) {
275 DPAA_MEMPOOL_ERR("Invalid mempool provided\n");
279 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
281 return bman_query_free_buffers(bp_info->bp);
285 dpaa_populate(struct rte_mempool *mp, unsigned int max_objs,
286 void *vaddr, rte_iova_t paddr, size_t len,
287 rte_mempool_populate_obj_cb_t *obj_cb, void *obj_cb_arg)
289 struct dpaa_bp_info *bp_info;
290 unsigned int total_elt_sz;
292 MEMPOOL_INIT_FUNC_TRACE();
294 if (!mp || !mp->pool_data) {
295 DPAA_MEMPOOL_ERR("Invalid mempool provided\n");
299 /* Update the PA-VA Table */
300 dpaax_iova_table_update(paddr, vaddr, len);
302 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
303 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
305 DPAA_MEMPOOL_DEBUG("Req size %" PRIx64 " vs Available %u\n",
306 (uint64_t)len, total_elt_sz * mp->size);
308 /* Detect pool area has sufficient space for elements in this memzone */
309 if (len >= total_elt_sz * mp->size)
310 bp_info->flags |= DPAA_MPOOL_SINGLE_SEGMENT;
311 struct dpaa_memseg *ms;
313 /* For each memory chunk pinned to the Mempool, a linked list of the
314 * contained memsegs is created for searching when PA to VA
315 * conversion is required.
317 ms = rte_zmalloc(NULL, sizeof(struct dpaa_memseg), 0);
319 DPAA_MEMPOOL_ERR("Unable to allocate internal memory.");
320 DPAA_MEMPOOL_WARN("Fast Physical to Virtual Addr translation would not be available.");
321 /* If the element is not added, it would only lead to failure
322 * in searching for the element and the logic would Fallback
323 * to traditional DPDK memseg traversal code. So, this is not
324 * a blocking error - but, error would be printed on screen.
332 /* Head insertions are generally faster than tail insertions as the
333 * buffers pinned are picked from rear end.
335 TAILQ_INSERT_HEAD(&rte_dpaa_memsegs, ms, next);
337 return rte_mempool_op_populate_default(mp, max_objs, vaddr, paddr, len,
341 static const struct rte_mempool_ops dpaa_mpool_ops = {
342 .name = DPAA_MEMPOOL_OPS_NAME,
343 .alloc = dpaa_mbuf_create_pool,
344 .free = dpaa_mbuf_free_pool,
345 .enqueue = dpaa_mbuf_free_bulk,
346 .dequeue = dpaa_mbuf_alloc_bulk,
347 .get_count = dpaa_mbuf_get_count,
348 .populate = dpaa_populate,
351 MEMPOOL_REGISTER_OPS(dpaa_mpool_ops);