6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of NXP nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/types.h>
42 #include <sys/syscall.h>
44 #include <rte_byteorder.h>
45 #include <rte_common.h>
47 #include <rte_debug.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_tailq.h>
52 #include <rte_malloc.h>
55 #include <dpaa_mempool.h>
57 struct dpaa_bp_info rte_dpaa_bpid_info[DPAA_MAX_BPOOLS];
60 dpaa_mbuf_create_pool(struct rte_mempool *mp)
63 struct bm_buffer bufs[8];
64 struct dpaa_bp_info *bp_info;
66 int num_bufs = 0, ret = 0;
67 struct bman_pool_params params = {
68 .flags = BMAN_POOL_FLAG_DYNAMIC_BPID
71 MEMPOOL_INIT_FUNC_TRACE();
73 bp = bman_new_pool(¶ms);
75 DPAA_MEMPOOL_ERR("bman_new_pool() failed");
78 bpid = bman_get_params(bp)->bpid;
80 /* Drain the pool of anything already in it. */
82 /* Acquire is all-or-nothing, so we drain in 8s,
83 * then in 1s for the remainder.
86 ret = bman_acquire(bp, bufs, 8, 0);
88 ret = bman_acquire(bp, bufs, 1, 0);
93 DPAA_MEMPOOL_WARN("drained %u bufs from BPID %d",
96 rte_dpaa_bpid_info[bpid].mp = mp;
97 rte_dpaa_bpid_info[bpid].bpid = bpid;
98 rte_dpaa_bpid_info[bpid].size = mp->elt_size;
99 rte_dpaa_bpid_info[bpid].bp = bp;
100 rte_dpaa_bpid_info[bpid].meta_data_size =
101 sizeof(struct rte_mbuf) + rte_pktmbuf_priv_size(mp);
102 rte_dpaa_bpid_info[bpid].dpaa_ops_index = mp->ops_index;
104 bp_info = rte_malloc(NULL,
105 sizeof(struct dpaa_bp_info),
106 RTE_CACHE_LINE_SIZE);
108 DPAA_MEMPOOL_WARN("Memory allocation failed for bp_info");
113 rte_memcpy(bp_info, (void *)&rte_dpaa_bpid_info[bpid],
114 sizeof(struct dpaa_bp_info));
115 mp->pool_data = (void *)bp_info;
117 DPAA_MEMPOOL_INFO("BMAN pool created for bpid =%d", bpid);
122 dpaa_mbuf_free_pool(struct rte_mempool *mp)
124 struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
126 MEMPOOL_INIT_FUNC_TRACE();
129 bman_free_pool(bp_info->bp);
130 DPAA_MEMPOOL_INFO("BMAN pool freed for bpid =%d",
132 rte_free(mp->pool_data);
133 mp->pool_data = NULL;
138 dpaa_buf_free(struct dpaa_bp_info *bp_info, uint64_t addr)
140 struct bm_buffer buf;
143 DPAA_MEMPOOL_DEBUG("Free 0x%lx to bpid: %d", addr, bp_info->bpid);
145 bm_buffer_set64(&buf, addr);
147 ret = bman_release(bp_info->bp, &buf, 1, 0);
149 DPAA_MEMPOOL_DEBUG("BMAN busy. Retrying...");
150 cpu_spin(CPU_SPIN_BACKOFF_CYCLES);
156 dpaa_mbuf_free_bulk(struct rte_mempool *pool,
157 void *const *obj_table,
160 struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
164 DPAA_MEMPOOL_DPDEBUG("Request to free %d buffers in bpid = %d",
167 ret = rte_dpaa_portal_init((void *)0);
169 DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
175 dpaa_buf_free(bp_info,
176 (uint64_t)rte_mempool_virt2phy(pool,
177 obj_table[i]) + bp_info->meta_data_size);
181 DPAA_MEMPOOL_DPDEBUG("freed %d buffers in bpid =%d",
188 dpaa_mbuf_alloc_bulk(struct rte_mempool *pool,
192 struct rte_mbuf **m = (struct rte_mbuf **)obj_table;
193 struct bm_buffer bufs[DPAA_MBUF_MAX_ACQ_REL];
194 struct dpaa_bp_info *bp_info;
199 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
201 DPAA_MEMPOOL_DPDEBUG("Request to alloc %d buffers in bpid = %d",
202 count, bp_info->bpid);
204 if (unlikely(count >= (RTE_MEMPOOL_CACHE_MAX_SIZE * 2))) {
205 DPAA_MEMPOOL_ERR("Unable to allocate requested (%u) buffers",
210 ret = rte_dpaa_portal_init((void *)0);
212 DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
218 /* Acquire is all-or-nothing, so we drain in 7s,
219 * then the remainder.
221 if ((count - n) > DPAA_MBUF_MAX_ACQ_REL) {
222 ret = bman_acquire(bp_info->bp, bufs,
223 DPAA_MBUF_MAX_ACQ_REL, 0);
225 ret = bman_acquire(bp_info->bp, bufs, count - n, 0);
227 /* In case of less than requested number of buffers available
228 * in pool, qbman_swp_acquire returns 0
231 DPAA_MEMPOOL_DPDEBUG("Buffer acquire failed (%d)",
233 /* The API expect the exact number of requested
234 * buffers. Releasing all buffers allocated
236 dpaa_mbuf_free_bulk(pool, obj_table, n);
239 /* assigning mbuf from the acquired objects */
240 for (i = 0; (i < ret) && bufs[i].addr; i++) {
241 /* TODO-errata - objerved that bufs may be null
242 * i.e. first buffer is valid, remaining 6 buffers
245 bufaddr = (void *)rte_dpaa_mem_ptov(bufs[i].addr);
246 m[n] = (struct rte_mbuf *)((char *)bufaddr
247 - bp_info->meta_data_size);
248 DPAA_MEMPOOL_DPDEBUG("Paddr (%p), FD (%p) from BMAN",
249 (void *)bufaddr, (void *)m[n]);
254 DPAA_MEMPOOL_DPDEBUG("Allocated %d buffers from bpid=%d",
260 dpaa_mbuf_get_count(const struct rte_mempool *mp)
262 struct dpaa_bp_info *bp_info;
264 MEMPOOL_INIT_FUNC_TRACE();
266 if (!mp || !mp->pool_data) {
267 DPAA_MEMPOOL_ERR("Invalid mempool provided\n");
271 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
273 return bman_query_free_buffers(bp_info->bp);
276 struct rte_mempool_ops dpaa_mpool_ops = {
278 .alloc = dpaa_mbuf_create_pool,
279 .free = dpaa_mbuf_free_pool,
280 .enqueue = dpaa_mbuf_free_bulk,
281 .dequeue = dpaa_mbuf_alloc_bulk,
282 .get_count = dpaa_mbuf_get_count,
285 MEMPOOL_REGISTER_OPS(dpaa_mpool_ops);