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 dpaa2_bp_info *bp_info;
66 struct dpbp_attr dpbp_attr;
70 avail_dpbp = dpaa2_alloc_dpbp_dev();
73 PMD_DRV_LOG(ERR, "DPAA2 resources not available");
77 if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
78 ret = dpaa2_affine_qbman_swp();
80 RTE_LOG(ERR, PMD, "Failure in affining portal\n");
85 ret = dpbp_enable(&avail_dpbp->dpbp, CMD_PRI_LOW, avail_dpbp->token);
87 PMD_INIT_LOG(ERR, "Resource enable failure with"
88 " err code: %d\n", ret);
92 ret = dpbp_get_attributes(&avail_dpbp->dpbp, CMD_PRI_LOW,
93 avail_dpbp->token, &dpbp_attr);
95 PMD_INIT_LOG(ERR, "Resource read failure with"
96 " err code: %d\n", ret);
98 ret = dpbp_disable(&avail_dpbp->dpbp, CMD_PRI_LOW,
103 /* Allocate the bp_list which will be added into global_bp_list */
104 bp_list = rte_malloc(NULL, sizeof(struct dpaa2_bp_list),
105 RTE_CACHE_LINE_SIZE);
107 PMD_INIT_LOG(ERR, "No heap memory available");
111 /* Set parameters of buffer pool list */
112 bp_list->buf_pool.num_bufs = mp->size;
113 bp_list->buf_pool.size = mp->elt_size
114 - sizeof(struct rte_mbuf) - rte_pktmbuf_priv_size(mp);
115 bp_list->buf_pool.bpid = dpbp_attr.bpid;
116 bp_list->buf_pool.h_bpool_mem = NULL;
117 bp_list->buf_pool.dpbp_node = avail_dpbp;
118 /* Identification for our offloaded pool_data structure */
119 bp_list->dpaa2_ops_index = mp->ops_index;
120 bp_list->next = h_bp_list;
123 bpid = dpbp_attr.bpid;
125 rte_dpaa2_bpid_info[bpid].meta_data_size = sizeof(struct rte_mbuf)
126 + rte_pktmbuf_priv_size(mp);
127 rte_dpaa2_bpid_info[bpid].bp_list = bp_list;
128 rte_dpaa2_bpid_info[bpid].bpid = bpid;
130 bp_info = rte_malloc(NULL,
131 sizeof(struct dpaa2_bp_info),
132 RTE_CACHE_LINE_SIZE);
133 rte_memcpy(bp_info, (void *)&rte_dpaa2_bpid_info[bpid],
134 sizeof(struct dpaa2_bp_info));
135 mp->pool_data = (void *)bp_info;
137 PMD_INIT_LOG(DEBUG, "BP List created for bpid =%d", dpbp_attr.bpid);
144 rte_hw_mbuf_free_pool(struct rte_mempool *mp)
146 struct dpaa2_bp_info *bpinfo;
147 struct dpaa2_bp_list *bp;
148 struct dpaa2_dpbp_dev *dpbp_node;
150 if (!mp->pool_data) {
151 PMD_DRV_LOG(ERR, "Not a valid dpaa22 pool");
155 bpinfo = (struct dpaa2_bp_info *)mp->pool_data;
156 bp = bpinfo->bp_list;
157 dpbp_node = bp->buf_pool.dpbp_node;
159 dpbp_disable(&(dpbp_node->dpbp), CMD_PRI_LOW, dpbp_node->token);
161 if (h_bp_list == bp) {
162 h_bp_list = h_bp_list->next;
163 } else { /* if it is not the first node */
164 struct dpaa2_bp_list *prev = h_bp_list, *temp;
165 temp = h_bp_list->next;
168 prev->next = temp->next;
177 rte_free(mp->pool_data);
178 dpaa2_free_dpbp_dev(dpbp_node);
182 rte_dpaa2_mbuf_release(struct rte_mempool *pool __rte_unused,
183 void * const *obj_table,
185 uint32_t meta_data_size,
188 struct qbman_release_desc releasedesc;
189 struct qbman_swp *swp;
192 uint64_t bufs[DPAA2_MBUF_MAX_ACQ_REL];
194 if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
195 ret = dpaa2_affine_qbman_swp();
197 RTE_LOG(ERR, PMD, "Failed to allocate IO portal\n");
201 swp = DPAA2_PER_LCORE_PORTAL;
203 /* Create a release descriptor required for releasing
206 qbman_release_desc_clear(&releasedesc);
207 qbman_release_desc_set_bpid(&releasedesc, bpid);
209 n = count % DPAA2_MBUF_MAX_ACQ_REL;
213 /* convert mbuf to buffers for the remainder */
214 for (i = 0; i < n ; i++) {
215 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
216 bufs[i] = (uint64_t)rte_mempool_virt2phy(pool, obj_table[i])
219 bufs[i] = (uint64_t)obj_table[i] + meta_data_size;
223 /* feed them to bman */
225 ret = qbman_swp_release(swp, &releasedesc, bufs, n);
226 } while (ret == -EBUSY);
229 /* if there are more buffers to free */
231 /* convert mbuf to buffers */
232 for (i = 0; i < DPAA2_MBUF_MAX_ACQ_REL; i++) {
233 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
235 rte_mempool_virt2phy(pool, obj_table[n + i])
238 bufs[i] = (uint64_t)obj_table[n + i] + meta_data_size;
243 ret = qbman_swp_release(swp, &releasedesc, bufs,
244 DPAA2_MBUF_MAX_ACQ_REL);
245 } while (ret == -EBUSY);
246 n += DPAA2_MBUF_MAX_ACQ_REL;
251 rte_dpaa2_mbuf_alloc_bulk(struct rte_mempool *pool,
252 void **obj_table, unsigned int count)
254 #ifdef RTE_LIBRTE_DPAA2_DEBUG_DRIVER
257 struct qbman_swp *swp;
259 uint64_t bufs[DPAA2_MBUF_MAX_ACQ_REL];
262 struct dpaa2_bp_info *bp_info;
264 bp_info = mempool_to_bpinfo(pool);
266 if (!(bp_info->bp_list)) {
267 RTE_LOG(ERR, PMD, "DPAA2 buffer pool not configured\n");
271 bpid = bp_info->bpid;
273 if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
274 ret = dpaa2_affine_qbman_swp();
276 RTE_LOG(ERR, PMD, "Failed to allocate IO portal\n");
280 swp = DPAA2_PER_LCORE_PORTAL;
283 /* Acquire is all-or-nothing, so we drain in 7s,
284 * then the remainder.
286 if ((count - n) > DPAA2_MBUF_MAX_ACQ_REL) {
287 ret = qbman_swp_acquire(swp, bpid, bufs,
288 DPAA2_MBUF_MAX_ACQ_REL);
290 ret = qbman_swp_acquire(swp, bpid, bufs,
293 /* In case of less than requested number of buffers available
294 * in pool, qbman_swp_acquire returns 0
297 PMD_TX_LOG(ERR, "Buffer acquire failed with"
298 " err code: %d", ret);
299 /* The API expect the exact number of requested bufs */
300 /* Releasing all buffers allocated */
301 rte_dpaa2_mbuf_release(pool, obj_table, bpid,
302 bp_info->meta_data_size, n);
305 /* assigning mbuf from the acquired objects */
306 for (i = 0; (i < ret) && bufs[i]; i++) {
307 DPAA2_MODIFY_IOVA_TO_VADDR(bufs[i], uint64_t);
308 obj_table[n] = (struct rte_mbuf *)
309 (bufs[i] - bp_info->meta_data_size);
310 PMD_TX_LOG(DEBUG, "Acquired %p address %p from BMAN",
311 (void *)bufs[i], (void *)obj_table[n]);
316 #ifdef RTE_LIBRTE_DPAA2_DEBUG_DRIVER
318 PMD_TX_LOG(DEBUG, "Total = %d , req = %d done = %d",
325 rte_hw_mbuf_free_bulk(struct rte_mempool *pool,
326 void * const *obj_table, unsigned int n)
328 struct dpaa2_bp_info *bp_info;
330 bp_info = mempool_to_bpinfo(pool);
331 if (!(bp_info->bp_list)) {
332 RTE_LOG(ERR, PMD, "DPAA2 buffer pool not configured\n");
335 rte_dpaa2_mbuf_release(pool, obj_table, bp_info->bpid,
336 bp_info->meta_data_size, n);
342 rte_hw_mbuf_get_count(const struct rte_mempool *mp)
345 unsigned int num_of_bufs = 0;
346 struct dpaa2_bp_info *bp_info;
347 struct dpaa2_dpbp_dev *dpbp_node;
349 if (!mp || !mp->pool_data) {
350 RTE_LOG(ERR, PMD, "Invalid mempool provided\n");
354 bp_info = (struct dpaa2_bp_info *)mp->pool_data;
355 dpbp_node = bp_info->bp_list->buf_pool.dpbp_node;
357 ret = dpbp_get_num_free_bufs(&dpbp_node->dpbp, CMD_PRI_LOW,
358 dpbp_node->token, &num_of_bufs);
360 RTE_LOG(ERR, PMD, "Unable to obtain free buf count (err=%d)\n",
365 RTE_LOG(DEBUG, PMD, "Free bufs = %u\n", num_of_bufs);
370 struct rte_mempool_ops dpaa2_mpool_ops = {
372 .alloc = rte_hw_mbuf_create_pool,
373 .free = rte_hw_mbuf_free_pool,
374 .enqueue = rte_hw_mbuf_free_bulk,
375 .dequeue = rte_dpaa2_mbuf_alloc_bulk,
376 .get_count = rte_hw_mbuf_get_count,
379 MEMPOOL_REGISTER_OPS(dpaa2_mpool_ops);