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_config.h>
45 #include <rte_byteorder.h>
46 #include <rte_common.h>
48 #include <rte_debug.h>
49 #include <rte_memory.h>
50 #include <rte_memzone.h>
51 #include <rte_tailq.h>
53 #include <rte_malloc.h>
56 #include <dpaa_mempool.h>
58 struct dpaa_bp_info rte_dpaa_bpid_info[DPAA_MAX_BPOOLS];
61 dpaa_mbuf_create_pool(struct rte_mempool *mp)
64 struct bm_buffer bufs[8];
65 struct dpaa_bp_info *bp_info;
67 int num_bufs = 0, ret = 0;
68 struct bman_pool_params params = {
69 .flags = BMAN_POOL_FLAG_DYNAMIC_BPID
72 MEMPOOL_INIT_FUNC_TRACE();
74 bp = bman_new_pool(¶ms);
76 DPAA_MEMPOOL_ERR("bman_new_pool() failed");
79 bpid = bman_get_params(bp)->bpid;
81 /* Drain the pool of anything already in it. */
83 /* Acquire is all-or-nothing, so we drain in 8s,
84 * then in 1s for the remainder.
87 ret = bman_acquire(bp, bufs, 8, 0);
89 ret = bman_acquire(bp, bufs, 1, 0);
94 DPAA_MEMPOOL_WARN("drained %u bufs from BPID %d",
97 rte_dpaa_bpid_info[bpid].mp = mp;
98 rte_dpaa_bpid_info[bpid].bpid = bpid;
99 rte_dpaa_bpid_info[bpid].size = mp->elt_size;
100 rte_dpaa_bpid_info[bpid].bp = bp;
101 rte_dpaa_bpid_info[bpid].meta_data_size =
102 sizeof(struct rte_mbuf) + rte_pktmbuf_priv_size(mp);
103 rte_dpaa_bpid_info[bpid].dpaa_ops_index = mp->ops_index;
105 bp_info = rte_malloc(NULL,
106 sizeof(struct dpaa_bp_info),
107 RTE_CACHE_LINE_SIZE);
109 DPAA_MEMPOOL_WARN("Memory allocation failed for bp_info");
114 rte_memcpy(bp_info, (void *)&rte_dpaa_bpid_info[bpid],
115 sizeof(struct dpaa_bp_info));
116 mp->pool_data = (void *)bp_info;
118 DPAA_MEMPOOL_INFO("BMAN pool created for bpid =%d", bpid);
123 dpaa_mbuf_free_pool(struct rte_mempool *mp)
125 struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
127 MEMPOOL_INIT_FUNC_TRACE();
130 bman_free_pool(bp_info->bp);
131 DPAA_MEMPOOL_INFO("BMAN pool freed for bpid =%d",
133 rte_free(mp->pool_data);
134 mp->pool_data = NULL;
139 dpaa_buf_free(struct dpaa_bp_info *bp_info, uint64_t addr)
141 struct bm_buffer buf;
144 DPAA_MEMPOOL_DEBUG("Free 0x%lx to bpid: %d", addr, bp_info->bpid);
146 bm_buffer_set64(&buf, addr);
148 ret = bman_release(bp_info->bp, &buf, 1, 0);
150 DPAA_MEMPOOL_DEBUG("BMAN busy. Retrying...");
151 cpu_spin(CPU_SPIN_BACKOFF_CYCLES);
157 dpaa_mbuf_free_bulk(struct rte_mempool *pool,
158 void *const *obj_table,
161 struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
165 DPAA_MEMPOOL_DPDEBUG("Request to free %d buffers in bpid = %d",
168 ret = rte_dpaa_portal_init((void *)0);
170 DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
176 dpaa_buf_free(bp_info,
177 (uint64_t)rte_mempool_virt2phy(pool,
178 obj_table[i]) + bp_info->meta_data_size);
182 DPAA_MEMPOOL_DPDEBUG("freed %d buffers in bpid =%d",
189 dpaa_mbuf_alloc_bulk(struct rte_mempool *pool,
193 struct rte_mbuf **m = (struct rte_mbuf **)obj_table;
194 struct bm_buffer bufs[DPAA_MBUF_MAX_ACQ_REL];
195 struct dpaa_bp_info *bp_info;
200 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
202 DPAA_MEMPOOL_DPDEBUG("Request to alloc %d buffers in bpid = %d",
203 count, bp_info->bpid);
205 if (unlikely(count >= (RTE_MEMPOOL_CACHE_MAX_SIZE * 2))) {
206 DPAA_MEMPOOL_ERR("Unable to allocate requested (%u) buffers",
211 ret = rte_dpaa_portal_init((void *)0);
213 DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
219 /* Acquire is all-or-nothing, so we drain in 7s,
220 * then the remainder.
222 if ((count - n) > DPAA_MBUF_MAX_ACQ_REL) {
223 ret = bman_acquire(bp_info->bp, bufs,
224 DPAA_MBUF_MAX_ACQ_REL, 0);
226 ret = bman_acquire(bp_info->bp, bufs, count - n, 0);
228 /* In case of less than requested number of buffers available
229 * in pool, qbman_swp_acquire returns 0
232 DPAA_MEMPOOL_DPDEBUG("Buffer acquire failed (%d)",
234 /* The API expect the exact number of requested
235 * buffers. Releasing all buffers allocated
237 dpaa_mbuf_free_bulk(pool, obj_table, n);
240 /* assigning mbuf from the acquired objects */
241 for (i = 0; (i < ret) && bufs[i].addr; i++) {
242 /* TODO-errata - objerved that bufs may be null
243 * i.e. first buffer is valid, remaining 6 buffers
246 bufaddr = (void *)rte_dpaa_mem_ptov(bufs[i].addr);
247 m[n] = (struct rte_mbuf *)((char *)bufaddr
248 - bp_info->meta_data_size);
249 DPAA_MEMPOOL_DPDEBUG("Paddr (%p), FD (%p) from BMAN",
250 (void *)bufaddr, (void *)m[n]);
255 DPAA_MEMPOOL_DPDEBUG("Allocated %d buffers from bpid=%d",
261 dpaa_mbuf_get_count(const struct rte_mempool *mp)
263 struct dpaa_bp_info *bp_info;
265 MEMPOOL_INIT_FUNC_TRACE();
267 if (!mp || !mp->pool_data) {
268 DPAA_MEMPOOL_ERR("Invalid mempool provided\n");
272 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
274 return bman_query_free_buffers(bp_info->bp);
277 struct rte_mempool_ops dpaa_mpool_ops = {
279 .alloc = dpaa_mbuf_create_pool,
280 .free = dpaa_mbuf_free_pool,
281 .enqueue = dpaa_mbuf_free_bulk,
282 .dequeue = dpaa_mbuf_alloc_bulk,
283 .get_count = dpaa_mbuf_get_count,
286 MEMPOOL_REGISTER_OPS(dpaa_mpool_ops);