3b2e5a9b08a045d66678e2d1636692cf88270079
[dpdk.git] / drivers / mempool / dpaa2 / dpaa2_hw_mempool.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
5  *   Copyright 2016 NXP.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <fcntl.h>
40 #include <errno.h>
41
42 #include <rte_mbuf.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>
49 #include <rte_dev.h>
50
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"
56
57 struct dpaa2_bp_info rte_dpaa2_bpid_info[MAX_BPID];
58 static struct dpaa2_bp_list *h_bp_list;
59
60 static int
61 rte_hw_mbuf_create_pool(struct rte_mempool *mp)
62 {
63         struct dpaa2_bp_list *bp_list;
64         struct dpaa2_dpbp_dev *avail_dpbp;
65         struct dpbp_attr dpbp_attr;
66         uint32_t bpid;
67         int ret, p_ret;
68
69         avail_dpbp = dpaa2_alloc_dpbp_dev();
70
71         if (!avail_dpbp) {
72                 PMD_DRV_LOG(ERR, "DPAA2 resources not available");
73                 return -ENOENT;
74         }
75
76         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
77                 ret = dpaa2_affine_qbman_swp();
78                 if (ret) {
79                         RTE_LOG(ERR, PMD, "Failure in affining portal\n");
80                         return ret;
81                 }
82         }
83
84         ret = dpbp_enable(&avail_dpbp->dpbp, CMD_PRI_LOW, avail_dpbp->token);
85         if (ret != 0) {
86                 PMD_INIT_LOG(ERR, "Resource enable failure with"
87                         " err code: %d\n", ret);
88                 return ret;
89         }
90
91         ret = dpbp_get_attributes(&avail_dpbp->dpbp, CMD_PRI_LOW,
92                                   avail_dpbp->token, &dpbp_attr);
93         if (ret != 0) {
94                 PMD_INIT_LOG(ERR, "Resource read failure with"
95                              " err code: %d\n", ret);
96                 p_ret = ret;
97                 ret = dpbp_disable(&avail_dpbp->dpbp, CMD_PRI_LOW,
98                                    avail_dpbp->token);
99                 return p_ret;
100         }
101
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);
105         if (!bp_list) {
106                 PMD_INIT_LOG(ERR, "No heap memory available");
107                 return -ENOMEM;
108         }
109
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;
120         bp_list->mp = mp;
121
122         bpid = dpbp_attr.bpid;
123
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;
128
129         mp->pool_data = (void *)&rte_dpaa2_bpid_info[bpid];
130
131         PMD_INIT_LOG(DEBUG, "BP List created for bpid =%d", dpbp_attr.bpid);
132
133         h_bp_list = bp_list;
134         return 0;
135 }
136
137 static void
138 rte_hw_mbuf_free_pool(struct rte_mempool *mp)
139 {
140         struct dpaa2_bp_info *bpinfo;
141         struct dpaa2_bp_list *bp;
142         struct dpaa2_dpbp_dev *dpbp_node;
143
144         if (!mp->pool_data) {
145                 PMD_DRV_LOG(ERR, "Not a valid dpaa22 pool");
146                 return;
147         }
148
149         bpinfo = (struct dpaa2_bp_info *)mp->pool_data;
150         bp = bpinfo->bp_list;
151         dpbp_node = bp->buf_pool.dpbp_node;
152
153         dpbp_disable(&(dpbp_node->dpbp), CMD_PRI_LOW, dpbp_node->token);
154
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;
160                 while (temp) {
161                         if (temp == bp) {
162                                 prev->next = temp->next;
163                                 free(bp);
164                                 break;
165                         }
166                         prev = temp;
167                         temp = temp->next;
168                 }
169         }
170
171         dpaa2_free_dpbp_dev(dpbp_node);
172 }
173
174 static void
175 rte_dpaa2_mbuf_release(struct rte_mempool *pool __rte_unused,
176                         void * const *obj_table,
177                         uint32_t bpid,
178                         uint32_t meta_data_size,
179                         int count)
180 {
181         struct qbman_release_desc releasedesc;
182         struct qbman_swp *swp;
183         int ret;
184         int i, n;
185         uint64_t bufs[DPAA2_MBUF_MAX_ACQ_REL];
186
187         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
188                 ret = dpaa2_affine_qbman_swp();
189                 if (ret != 0) {
190                         RTE_LOG(ERR, PMD, "Failed to allocate IO portal\n");
191                         return;
192                 }
193         }
194         swp = DPAA2_PER_LCORE_PORTAL;
195
196         /* Create a release descriptor required for releasing
197          * buffers into QBMAN
198          */
199         qbman_release_desc_clear(&releasedesc);
200         qbman_release_desc_set_bpid(&releasedesc, bpid);
201
202         n = count % DPAA2_MBUF_MAX_ACQ_REL;
203         if (unlikely(!n))
204                 goto aligned;
205
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])
210                                 + meta_data_size;
211 #else
212                 bufs[i] = (uint64_t)obj_table[i] + meta_data_size;
213 #endif
214         }
215
216         /* feed them to bman */
217         do {
218                 ret = qbman_swp_release(swp, &releasedesc, bufs, n);
219         } while (ret == -EBUSY);
220
221 aligned:
222         /* if there are more buffers to free */
223         while (n < count) {
224                 /* convert mbuf to buffers */
225                 for (i = 0; i < DPAA2_MBUF_MAX_ACQ_REL; i++) {
226 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
227                         bufs[i] = (uint64_t)
228                                   rte_mempool_virt2phy(pool, obj_table[n + i])
229                                   + meta_data_size;
230 #else
231                         bufs[i] = (uint64_t)obj_table[n + i] + meta_data_size;
232 #endif
233                 }
234
235                 do {
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;
240         }
241 }
242
243 int
244 rte_dpaa2_mbuf_alloc_bulk(struct rte_mempool *pool,
245                           void **obj_table, unsigned int count)
246 {
247 #ifdef RTE_LIBRTE_DPAA2_DEBUG_DRIVER
248         static int alloc;
249 #endif
250         struct qbman_swp *swp;
251         uint16_t bpid;
252         uint64_t bufs[DPAA2_MBUF_MAX_ACQ_REL];
253         int i, ret;
254         unsigned int n = 0;
255         struct dpaa2_bp_info *bp_info;
256
257         bp_info = mempool_to_bpinfo(pool);
258
259         if (!(bp_info->bp_list)) {
260                 RTE_LOG(ERR, PMD, "DPAA2 buffer pool not configured\n");
261                 return -ENOENT;
262         }
263
264         bpid = bp_info->bpid;
265
266         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
267                 ret = dpaa2_affine_qbman_swp();
268                 if (ret != 0) {
269                         RTE_LOG(ERR, PMD, "Failed to allocate IO portal\n");
270                         return ret;
271                 }
272         }
273         swp = DPAA2_PER_LCORE_PORTAL;
274
275         while (n < count) {
276                 /* Acquire is all-or-nothing, so we drain in 7s,
277                  * then the remainder.
278                  */
279                 if ((count - n) > DPAA2_MBUF_MAX_ACQ_REL) {
280                         ret = qbman_swp_acquire(swp, bpid, bufs,
281                                                 DPAA2_MBUF_MAX_ACQ_REL);
282                 } else {
283                         ret = qbman_swp_acquire(swp, bpid, bufs,
284                                                 count - n);
285                 }
286                 /* In case of less than requested number of buffers available
287                  * in pool, qbman_swp_acquire returns 0
288                  */
289                 if (ret <= 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);
296                         return ret;
297                 }
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]);
305                         n++;
306                 }
307         }
308
309 #ifdef RTE_LIBRTE_DPAA2_DEBUG_DRIVER
310         alloc += n;
311         PMD_TX_LOG(DEBUG, "Total = %d , req = %d done = %d",
312                    alloc, count, n);
313 #endif
314         return 0;
315 }
316
317 static int
318 rte_hw_mbuf_free_bulk(struct rte_mempool *pool,
319                   void * const *obj_table, unsigned int n)
320 {
321         struct dpaa2_bp_info *bp_info;
322
323         bp_info = mempool_to_bpinfo(pool);
324         if (!(bp_info->bp_list)) {
325                 RTE_LOG(ERR, PMD, "DPAA2 buffer pool not configured\n");
326                 return -ENOENT;
327         }
328         rte_dpaa2_mbuf_release(pool, obj_table, bp_info->bpid,
329                            bp_info->meta_data_size, n);
330
331         return 0;
332 }
333
334 static unsigned int
335 rte_hw_mbuf_get_count(const struct rte_mempool *mp)
336 {
337         int ret;
338         unsigned int num_of_bufs = 0;
339         struct dpaa2_bp_info *bp_info;
340         struct dpaa2_dpbp_dev *dpbp_node;
341
342         if (!mp || !mp->pool_data) {
343                 RTE_LOG(ERR, PMD, "Invalid mempool provided\n");
344                 return 0;
345         }
346
347         bp_info = (struct dpaa2_bp_info *)mp->pool_data;
348         dpbp_node = bp_info->bp_list->buf_pool.dpbp_node;
349
350         ret = dpbp_get_num_free_bufs(&dpbp_node->dpbp, CMD_PRI_LOW,
351                                      dpbp_node->token, &num_of_bufs);
352         if (ret) {
353                 RTE_LOG(ERR, PMD, "Unable to obtain free buf count (err=%d)\n",
354                         ret);
355                 return 0;
356         }
357
358         RTE_LOG(DEBUG, PMD, "Free bufs = %u\n", num_of_bufs);
359
360         return num_of_bufs;
361 }
362
363 struct rte_mempool_ops dpaa2_mpool_ops = {
364         .name = "dpaa2",
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,
370 };
371
372 MEMPOOL_REGISTER_OPS(dpaa2_mpool_ops);