1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright 2017,2019 NXP
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;
40 RTE_LOG_REGISTER(dpaa_logtype_mempool, mempool.dpaa, NOTICE);
43 dpaa_mbuf_create_pool(struct rte_mempool *mp)
46 struct bm_buffer bufs[8];
47 struct dpaa_bp_info *bp_info;
49 int num_bufs = 0, ret = 0;
50 struct bman_pool_params params = {
51 .flags = BMAN_POOL_FLAG_DYNAMIC_BPID
54 MEMPOOL_INIT_FUNC_TRACE();
56 if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
57 ret = rte_dpaa_portal_init((void *)0);
60 "rte_dpaa_portal_init failed with ret: %d",
65 bp = bman_new_pool(¶ms);
67 DPAA_MEMPOOL_ERR("bman_new_pool() failed");
70 bpid = bman_get_params(bp)->bpid;
72 /* Drain the pool of anything already in it. */
74 /* Acquire is all-or-nothing, so we drain in 8s,
75 * then in 1s for the remainder.
78 ret = bman_acquire(bp, bufs, 8, 0);
80 ret = bman_acquire(bp, bufs, 1, 0);
85 DPAA_MEMPOOL_WARN("drained %u bufs from BPID %d",
88 if (rte_dpaa_bpid_info == NULL) {
89 rte_dpaa_bpid_info = (struct dpaa_bp_info *)rte_zmalloc(NULL,
90 sizeof(struct dpaa_bp_info) * DPAA_MAX_BPOOLS,
92 if (rte_dpaa_bpid_info == NULL) {
98 rte_dpaa_bpid_info[bpid].mp = mp;
99 rte_dpaa_bpid_info[bpid].bpid = bpid;
100 rte_dpaa_bpid_info[bpid].size = mp->elt_size;
101 rte_dpaa_bpid_info[bpid].bp = bp;
102 rte_dpaa_bpid_info[bpid].meta_data_size =
103 sizeof(struct rte_mbuf) + rte_pktmbuf_priv_size(mp);
104 rte_dpaa_bpid_info[bpid].dpaa_ops_index = mp->ops_index;
105 rte_dpaa_bpid_info[bpid].ptov_off = 0;
106 rte_dpaa_bpid_info[bpid].flags = 0;
108 bp_info = rte_malloc(NULL,
109 sizeof(struct dpaa_bp_info),
110 RTE_CACHE_LINE_SIZE);
112 DPAA_MEMPOOL_WARN("Memory allocation failed for bp_info");
117 rte_memcpy(bp_info, (void *)&rte_dpaa_bpid_info[bpid],
118 sizeof(struct dpaa_bp_info));
119 mp->pool_data = (void *)bp_info;
121 DPAA_MEMPOOL_INFO("BMAN pool created for bpid =%d", bpid);
126 dpaa_mbuf_free_pool(struct rte_mempool *mp)
128 struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
130 MEMPOOL_INIT_FUNC_TRACE();
133 bman_free_pool(bp_info->bp);
134 DPAA_MEMPOOL_INFO("BMAN pool freed for bpid =%d",
136 rte_free(mp->pool_data);
137 mp->pool_data = NULL;
142 dpaa_buf_free(struct dpaa_bp_info *bp_info, uint64_t addr)
144 struct bm_buffer buf;
147 DPAA_MEMPOOL_DPDEBUG("Free 0x%" PRIx64 " to bpid: %d",
148 addr, bp_info->bpid);
150 bm_buffer_set64(&buf, addr);
152 ret = bman_release(bp_info->bp, &buf, 1, 0);
154 DPAA_MEMPOOL_DEBUG("BMAN busy. Retrying...");
155 cpu_spin(CPU_SPIN_BACKOFF_CYCLES);
161 dpaa_mbuf_free_bulk(struct rte_mempool *pool,
162 void *const *obj_table,
165 struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
169 DPAA_MEMPOOL_DPDEBUG("Request to free %d buffers in bpid = %d",
172 if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
173 ret = rte_dpaa_portal_init((void *)0);
175 DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
182 uint64_t phy = rte_mempool_virt2iova(obj_table[i]);
184 if (unlikely(!bp_info->ptov_off)) {
185 /* buffers are from single mem segment */
186 if (bp_info->flags & DPAA_MPOOL_SINGLE_SEGMENT) {
187 bp_info->ptov_off = (size_t)obj_table[i] - phy;
188 rte_dpaa_bpid_info[bp_info->bpid].ptov_off
193 dpaa_buf_free(bp_info,
194 (uint64_t)phy + bp_info->meta_data_size);
198 DPAA_MEMPOOL_DPDEBUG("freed %d buffers in bpid =%d",
205 dpaa_mbuf_alloc_bulk(struct rte_mempool *pool,
209 struct rte_mbuf **m = (struct rte_mbuf **)obj_table;
210 struct bm_buffer bufs[DPAA_MBUF_MAX_ACQ_REL];
211 struct dpaa_bp_info *bp_info;
216 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
218 DPAA_MEMPOOL_DPDEBUG("Request to alloc %d buffers in bpid = %d",
219 count, bp_info->bpid);
221 if (unlikely(count >= (RTE_MEMPOOL_CACHE_MAX_SIZE * 2))) {
222 DPAA_MEMPOOL_ERR("Unable to allocate requested (%u) buffers",
227 if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
228 ret = rte_dpaa_portal_init((void *)0);
230 DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
237 /* Acquire is all-or-nothing, so we drain in 7s,
238 * then the remainder.
240 if ((count - n) > DPAA_MBUF_MAX_ACQ_REL) {
241 ret = bman_acquire(bp_info->bp, bufs,
242 DPAA_MBUF_MAX_ACQ_REL, 0);
244 ret = bman_acquire(bp_info->bp, bufs, count - n, 0);
246 /* In case of less than requested number of buffers available
247 * in pool, qbman_swp_acquire returns 0
250 DPAA_MEMPOOL_DPDEBUG("Buffer acquire failed (%d)",
252 /* The API expect the exact number of requested
253 * buffers. Releasing all buffers allocated
255 dpaa_mbuf_free_bulk(pool, obj_table, n);
258 /* assigning mbuf from the acquired objects */
259 for (i = 0; (i < ret) && bufs[i].addr; i++) {
260 /* TODO-errata - objerved that bufs may be null
261 * i.e. first buffer is valid, remaining 6 buffers
264 bufaddr = DPAA_MEMPOOL_PTOV(bp_info, bufs[i].addr);
265 m[n] = (struct rte_mbuf *)((char *)bufaddr
266 - bp_info->meta_data_size);
267 DPAA_MEMPOOL_DPDEBUG("Paddr (%p), FD (%p) from BMAN",
268 (void *)bufaddr, (void *)m[n]);
273 DPAA_MEMPOOL_DPDEBUG("Allocated %d buffers from bpid=%d",
279 dpaa_mbuf_get_count(const struct rte_mempool *mp)
281 struct dpaa_bp_info *bp_info;
283 MEMPOOL_INIT_FUNC_TRACE();
285 if (!mp || !mp->pool_data) {
286 DPAA_MEMPOOL_ERR("Invalid mempool provided\n");
290 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
292 return bman_query_free_buffers(bp_info->bp);
296 dpaa_populate(struct rte_mempool *mp, unsigned int max_objs,
297 void *vaddr, rte_iova_t paddr, size_t len,
298 rte_mempool_populate_obj_cb_t *obj_cb, void *obj_cb_arg)
300 struct dpaa_bp_info *bp_info;
301 unsigned int total_elt_sz;
303 if (!mp || !mp->pool_data) {
304 DPAA_MEMPOOL_ERR("Invalid mempool provided\n");
308 /* Update the PA-VA Table */
309 dpaax_iova_table_update(paddr, vaddr, len);
311 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
312 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
314 DPAA_MEMPOOL_DPDEBUG("Req size %" PRIx64 " vs Available %u\n",
315 (uint64_t)len, total_elt_sz * mp->size);
317 /* Detect pool area has sufficient space for elements in this memzone */
318 if (len >= total_elt_sz * mp->size)
319 bp_info->flags |= DPAA_MPOOL_SINGLE_SEGMENT;
320 struct dpaa_memseg *ms;
322 /* For each memory chunk pinned to the Mempool, a linked list of the
323 * contained memsegs is created for searching when PA to VA
324 * conversion is required.
326 ms = rte_zmalloc(NULL, sizeof(struct dpaa_memseg), 0);
328 DPAA_MEMPOOL_ERR("Unable to allocate internal memory.");
329 DPAA_MEMPOOL_WARN("Fast Physical to Virtual Addr translation would not be available.");
330 /* If the element is not added, it would only lead to failure
331 * in searching for the element and the logic would Fallback
332 * to traditional DPDK memseg traversal code. So, this is not
333 * a blocking error - but, error would be printed on screen.
341 /* Head insertions are generally faster than tail insertions as the
342 * buffers pinned are picked from rear end.
344 TAILQ_INSERT_HEAD(&rte_dpaa_memsegs, ms, next);
346 return rte_mempool_op_populate_helper(mp, 0, max_objs, vaddr, paddr,
347 len, obj_cb, obj_cb_arg);
350 static const struct rte_mempool_ops dpaa_mpool_ops = {
351 .name = DPAA_MEMPOOL_OPS_NAME,
352 .alloc = dpaa_mbuf_create_pool,
353 .free = dpaa_mbuf_free_pool,
354 .enqueue = dpaa_mbuf_free_bulk,
355 .dequeue = dpaa_mbuf_alloc_bulk,
356 .get_count = dpaa_mbuf_get_count,
357 .populate = dpaa_populate,
360 MEMPOOL_REGISTER_OPS(dpaa_mpool_ops);