4 * Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Freescale Semiconductor, Inc nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/types.h>
43 #include <rte_ethdev.h>
44 #include <rte_malloc.h>
45 #include <rte_memcpy.h>
46 #include <rte_string_fns.h>
47 #include <rte_cycles.h>
48 #include <rte_kvargs.h>
51 #include <fslmc_logs.h>
52 #include <mc/fsl_dpbp.h>
53 #include <portal/dpaa2_hw_pvt.h>
54 #include <portal/dpaa2_hw_dpio.h>
55 #include "dpaa2_hw_mempool.h"
57 struct dpaa2_bp_info rte_dpaa2_bpid_info[MAX_BPID];
58 static struct dpaa2_bp_list *h_bp_list;
61 rte_hw_mbuf_create_pool(struct rte_mempool *mp)
63 struct dpaa2_bp_list *bp_list;
64 struct dpaa2_dpbp_dev *avail_dpbp;
65 struct dpbp_attr dpbp_attr;
69 avail_dpbp = dpaa2_alloc_dpbp_dev();
72 PMD_DRV_LOG(ERR, "DPAA2 resources not available");
76 if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
77 ret = dpaa2_affine_qbman_swp();
79 RTE_LOG(ERR, PMD, "Failure in affining portal\n");
84 ret = dpbp_enable(&avail_dpbp->dpbp, CMD_PRI_LOW, avail_dpbp->token);
86 PMD_INIT_LOG(ERR, "Resource enable failure with"
87 " err code: %d\n", ret);
91 ret = dpbp_get_attributes(&avail_dpbp->dpbp, CMD_PRI_LOW,
92 avail_dpbp->token, &dpbp_attr);
94 PMD_INIT_LOG(ERR, "Resource read failure with"
95 " err code: %d\n", ret);
97 ret = dpbp_disable(&avail_dpbp->dpbp, CMD_PRI_LOW,
102 /* Allocate the bp_list which will be added into global_bp_list */
103 bp_list = rte_malloc(NULL, sizeof(struct dpaa2_bp_list),
104 RTE_CACHE_LINE_SIZE);
106 PMD_INIT_LOG(ERR, "No heap memory available");
110 /* Set parameters of buffer pool list */
111 bp_list->buf_pool.num_bufs = mp->size;
112 bp_list->buf_pool.size = mp->elt_size
113 - sizeof(struct rte_mbuf) - rte_pktmbuf_priv_size(mp);
114 bp_list->buf_pool.bpid = dpbp_attr.bpid;
115 bp_list->buf_pool.h_bpool_mem = NULL;
116 bp_list->buf_pool.dpbp_node = avail_dpbp;
117 /* Identification for our offloaded pool_data structure */
118 bp_list->dpaa2_ops_index = mp->ops_index;
119 bp_list->next = h_bp_list;
122 bpid = dpbp_attr.bpid;
124 rte_dpaa2_bpid_info[bpid].meta_data_size = sizeof(struct rte_mbuf)
125 + rte_pktmbuf_priv_size(mp);
126 rte_dpaa2_bpid_info[bpid].bp_list = bp_list;
127 rte_dpaa2_bpid_info[bpid].bpid = bpid;
129 mp->pool_data = (void *)&rte_dpaa2_bpid_info[bpid];
131 PMD_INIT_LOG(DEBUG, "BP List created for bpid =%d", dpbp_attr.bpid);
138 rte_hw_mbuf_free_pool(struct rte_mempool *mp)
140 struct dpaa2_bp_info *bpinfo;
141 struct dpaa2_bp_list *bp;
142 struct dpaa2_dpbp_dev *dpbp_node;
144 if (!mp->pool_data) {
145 PMD_DRV_LOG(ERR, "Not a valid dpaa22 pool");
149 bpinfo = (struct dpaa2_bp_info *)mp->pool_data;
150 bp = bpinfo->bp_list;
151 dpbp_node = bp->buf_pool.dpbp_node;
153 dpbp_disable(&(dpbp_node->dpbp), CMD_PRI_LOW, dpbp_node->token);
155 if (h_bp_list == bp) {
156 h_bp_list = h_bp_list->next;
157 } else { /* if it is not the first node */
158 struct dpaa2_bp_list *prev = h_bp_list, *temp;
159 temp = h_bp_list->next;
162 prev->next = temp->next;
171 dpaa2_free_dpbp_dev(dpbp_node);
175 rte_dpaa2_mbuf_release(struct rte_mempool *pool __rte_unused,
176 void * const *obj_table,
178 uint32_t meta_data_size,
181 struct qbman_release_desc releasedesc;
182 struct qbman_swp *swp;
185 uint64_t bufs[DPAA2_MBUF_MAX_ACQ_REL];
187 if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
188 ret = dpaa2_affine_qbman_swp();
190 RTE_LOG(ERR, PMD, "Failed to allocate IO portal\n");
194 swp = DPAA2_PER_LCORE_PORTAL;
196 /* Create a release descriptor required for releasing
199 qbman_release_desc_clear(&releasedesc);
200 qbman_release_desc_set_bpid(&releasedesc, bpid);
202 n = count % DPAA2_MBUF_MAX_ACQ_REL;
206 /* convert mbuf to buffers for the remainder */
207 for (i = 0; i < n ; i++) {
208 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
209 bufs[i] = (uint64_t)rte_mempool_virt2phy(pool, obj_table[i])
212 bufs[i] = (uint64_t)obj_table[i] + meta_data_size;
216 /* feed them to bman */
218 ret = qbman_swp_release(swp, &releasedesc, bufs, n);
219 } while (ret == -EBUSY);
222 /* if there are more buffers to free */
224 /* convert mbuf to buffers */
225 for (i = 0; i < DPAA2_MBUF_MAX_ACQ_REL; i++) {
226 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
228 rte_mempool_virt2phy(pool, obj_table[n + i])
231 bufs[i] = (uint64_t)obj_table[n + i] + meta_data_size;
236 ret = qbman_swp_release(swp, &releasedesc, bufs,
237 DPAA2_MBUF_MAX_ACQ_REL);
238 } while (ret == -EBUSY);
239 n += DPAA2_MBUF_MAX_ACQ_REL;
244 rte_dpaa2_mbuf_alloc_bulk(struct rte_mempool *pool,
245 void **obj_table, unsigned int count)
247 #ifdef RTE_LIBRTE_DPAA2_DEBUG_DRIVER
250 struct qbman_swp *swp;
252 uint64_t bufs[DPAA2_MBUF_MAX_ACQ_REL];
255 struct dpaa2_bp_info *bp_info;
257 bp_info = mempool_to_bpinfo(pool);
259 if (!(bp_info->bp_list)) {
260 RTE_LOG(ERR, PMD, "DPAA2 buffer pool not configured\n");
264 bpid = bp_info->bpid;
266 if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
267 ret = dpaa2_affine_qbman_swp();
269 RTE_LOG(ERR, PMD, "Failed to allocate IO portal\n");
273 swp = DPAA2_PER_LCORE_PORTAL;
276 /* Acquire is all-or-nothing, so we drain in 7s,
277 * then the remainder.
279 if ((count - n) > DPAA2_MBUF_MAX_ACQ_REL) {
280 ret = qbman_swp_acquire(swp, bpid, bufs,
281 DPAA2_MBUF_MAX_ACQ_REL);
283 ret = qbman_swp_acquire(swp, bpid, bufs,
286 /* In case of less than requested number of buffers available
287 * in pool, qbman_swp_acquire returns 0
290 PMD_TX_LOG(ERR, "Buffer acquire failed with"
291 " err code: %d", ret);
292 /* The API expect the exact number of requested bufs */
293 /* Releasing all buffers allocated */
294 rte_dpaa2_mbuf_release(pool, obj_table, bpid,
295 bp_info->meta_data_size, n);
298 /* assigning mbuf from the acquired objects */
299 for (i = 0; (i < ret) && bufs[i]; i++) {
300 DPAA2_MODIFY_IOVA_TO_VADDR(bufs[i], uint64_t);
301 obj_table[n] = (struct rte_mbuf *)
302 (bufs[i] - bp_info->meta_data_size);
303 PMD_TX_LOG(DEBUG, "Acquired %p address %p from BMAN",
304 (void *)bufs[i], (void *)obj_table[n]);
309 #ifdef RTE_LIBRTE_DPAA2_DEBUG_DRIVER
311 PMD_TX_LOG(DEBUG, "Total = %d , req = %d done = %d",
318 rte_hw_mbuf_free_bulk(struct rte_mempool *pool,
319 void * const *obj_table, unsigned int n)
321 struct dpaa2_bp_info *bp_info;
323 bp_info = mempool_to_bpinfo(pool);
324 if (!(bp_info->bp_list)) {
325 RTE_LOG(ERR, PMD, "DPAA2 buffer pool not configured\n");
328 rte_dpaa2_mbuf_release(pool, obj_table, bp_info->bpid,
329 bp_info->meta_data_size, n);
335 rte_hw_mbuf_get_count(const struct rte_mempool *mp)
338 unsigned int num_of_bufs = 0;
339 struct dpaa2_bp_info *bp_info;
340 struct dpaa2_dpbp_dev *dpbp_node;
342 if (!mp || !mp->pool_data) {
343 RTE_LOG(ERR, PMD, "Invalid mempool provided\n");
347 bp_info = (struct dpaa2_bp_info *)mp->pool_data;
348 dpbp_node = bp_info->bp_list->buf_pool.dpbp_node;
350 ret = dpbp_get_num_free_bufs(&dpbp_node->dpbp, CMD_PRI_LOW,
351 dpbp_node->token, &num_of_bufs);
353 RTE_LOG(ERR, PMD, "Unable to obtain free buf count (err=%d)\n",
358 RTE_LOG(DEBUG, PMD, "Free bufs = %u\n", num_of_bufs);
363 struct rte_mempool_ops dpaa2_mpool_ops = {
365 .alloc = rte_hw_mbuf_create_pool,
366 .free = rte_hw_mbuf_free_pool,
367 .enqueue = rte_hw_mbuf_free_bulk,
368 .dequeue = rte_dpaa2_mbuf_alloc_bulk,
369 .get_count = rte_hw_mbuf_get_count,
372 MEMPOOL_REGISTER_OPS(dpaa2_mpool_ops);