6df203fce2820b7701affc2db1a6adb5b728723d
[deb_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 dpaa2_bp_info *bp_info;
66         struct dpbp_attr dpbp_attr;
67         uint32_t bpid;
68         int ret, p_ret;
69
70         avail_dpbp = dpaa2_alloc_dpbp_dev();
71
72         if (!avail_dpbp) {
73                 PMD_DRV_LOG(ERR, "DPAA2 resources not available");
74                 return -ENOENT;
75         }
76
77         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
78                 ret = dpaa2_affine_qbman_swp();
79                 if (ret) {
80                         RTE_LOG(ERR, PMD, "Failure in affining portal\n");
81                         return ret;
82                 }
83         }
84
85         ret = dpbp_enable(&avail_dpbp->dpbp, CMD_PRI_LOW, avail_dpbp->token);
86         if (ret != 0) {
87                 PMD_INIT_LOG(ERR, "Resource enable failure with"
88                         " err code: %d\n", ret);
89                 return ret;
90         }
91
92         ret = dpbp_get_attributes(&avail_dpbp->dpbp, CMD_PRI_LOW,
93                                   avail_dpbp->token, &dpbp_attr);
94         if (ret != 0) {
95                 PMD_INIT_LOG(ERR, "Resource read failure with"
96                              " err code: %d\n", ret);
97                 p_ret = ret;
98                 ret = dpbp_disable(&avail_dpbp->dpbp, CMD_PRI_LOW,
99                                    avail_dpbp->token);
100                 return p_ret;
101         }
102
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);
106         if (!bp_list) {
107                 PMD_INIT_LOG(ERR, "No heap memory available");
108                 return -ENOMEM;
109         }
110
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;
121         bp_list->mp = mp;
122
123         bpid = dpbp_attr.bpid;
124
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;
129
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;
136
137         PMD_INIT_LOG(DEBUG, "BP List created for bpid =%d", dpbp_attr.bpid);
138
139         h_bp_list = bp_list;
140         return 0;
141 }
142
143 static void
144 rte_hw_mbuf_free_pool(struct rte_mempool *mp)
145 {
146         struct dpaa2_bp_info *bpinfo;
147         struct dpaa2_bp_list *bp;
148         struct dpaa2_dpbp_dev *dpbp_node;
149
150         if (!mp->pool_data) {
151                 PMD_DRV_LOG(ERR, "Not a valid dpaa22 pool");
152                 return;
153         }
154
155         bpinfo = (struct dpaa2_bp_info *)mp->pool_data;
156         bp = bpinfo->bp_list;
157         dpbp_node = bp->buf_pool.dpbp_node;
158
159         dpbp_disable(&(dpbp_node->dpbp), CMD_PRI_LOW, dpbp_node->token);
160
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;
166                 while (temp) {
167                         if (temp == bp) {
168                                 prev->next = temp->next;
169                                 rte_free(bp);
170                                 break;
171                         }
172                         prev = temp;
173                         temp = temp->next;
174                 }
175         }
176
177         rte_free(mp->pool_data);
178         dpaa2_free_dpbp_dev(dpbp_node);
179 }
180
181 static void
182 rte_dpaa2_mbuf_release(struct rte_mempool *pool __rte_unused,
183                         void * const *obj_table,
184                         uint32_t bpid,
185                         uint32_t meta_data_size,
186                         int count)
187 {
188         struct qbman_release_desc releasedesc;
189         struct qbman_swp *swp;
190         int ret;
191         int i, n;
192         uint64_t bufs[DPAA2_MBUF_MAX_ACQ_REL];
193
194         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
195                 ret = dpaa2_affine_qbman_swp();
196                 if (ret != 0) {
197                         RTE_LOG(ERR, PMD, "Failed to allocate IO portal\n");
198                         return;
199                 }
200         }
201         swp = DPAA2_PER_LCORE_PORTAL;
202
203         /* Create a release descriptor required for releasing
204          * buffers into QBMAN
205          */
206         qbman_release_desc_clear(&releasedesc);
207         qbman_release_desc_set_bpid(&releasedesc, bpid);
208
209         n = count % DPAA2_MBUF_MAX_ACQ_REL;
210         if (unlikely(!n))
211                 goto aligned;
212
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])
217                                 + meta_data_size;
218 #else
219                 bufs[i] = (uint64_t)obj_table[i] + meta_data_size;
220 #endif
221         }
222
223         /* feed them to bman */
224         do {
225                 ret = qbman_swp_release(swp, &releasedesc, bufs, n);
226         } while (ret == -EBUSY);
227
228 aligned:
229         /* if there are more buffers to free */
230         while (n < count) {
231                 /* convert mbuf to buffers */
232                 for (i = 0; i < DPAA2_MBUF_MAX_ACQ_REL; i++) {
233 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
234                         bufs[i] = (uint64_t)
235                                   rte_mempool_virt2phy(pool, obj_table[n + i])
236                                   + meta_data_size;
237 #else
238                         bufs[i] = (uint64_t)obj_table[n + i] + meta_data_size;
239 #endif
240                 }
241
242                 do {
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;
247         }
248 }
249
250 int
251 rte_dpaa2_mbuf_alloc_bulk(struct rte_mempool *pool,
252                           void **obj_table, unsigned int count)
253 {
254 #ifdef RTE_LIBRTE_DPAA2_DEBUG_DRIVER
255         static int alloc;
256 #endif
257         struct qbman_swp *swp;
258         uint16_t bpid;
259         uint64_t bufs[DPAA2_MBUF_MAX_ACQ_REL];
260         int i, ret;
261         unsigned int n = 0;
262         struct dpaa2_bp_info *bp_info;
263
264         bp_info = mempool_to_bpinfo(pool);
265
266         if (!(bp_info->bp_list)) {
267                 RTE_LOG(ERR, PMD, "DPAA2 buffer pool not configured\n");
268                 return -ENOENT;
269         }
270
271         bpid = bp_info->bpid;
272
273         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
274                 ret = dpaa2_affine_qbman_swp();
275                 if (ret != 0) {
276                         RTE_LOG(ERR, PMD, "Failed to allocate IO portal\n");
277                         return ret;
278                 }
279         }
280         swp = DPAA2_PER_LCORE_PORTAL;
281
282         while (n < count) {
283                 /* Acquire is all-or-nothing, so we drain in 7s,
284                  * then the remainder.
285                  */
286                 if ((count - n) > DPAA2_MBUF_MAX_ACQ_REL) {
287                         ret = qbman_swp_acquire(swp, bpid, bufs,
288                                                 DPAA2_MBUF_MAX_ACQ_REL);
289                 } else {
290                         ret = qbman_swp_acquire(swp, bpid, bufs,
291                                                 count - n);
292                 }
293                 /* In case of less than requested number of buffers available
294                  * in pool, qbman_swp_acquire returns 0
295                  */
296                 if (ret <= 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);
303                         return -ENOBUFS;
304                 }
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]);
312                         n++;
313                 }
314         }
315
316 #ifdef RTE_LIBRTE_DPAA2_DEBUG_DRIVER
317         alloc += n;
318         PMD_TX_LOG(DEBUG, "Total = %d , req = %d done = %d",
319                    alloc, count, n);
320 #endif
321         return 0;
322 }
323
324 static int
325 rte_hw_mbuf_free_bulk(struct rte_mempool *pool,
326                   void * const *obj_table, unsigned int n)
327 {
328         struct dpaa2_bp_info *bp_info;
329
330         bp_info = mempool_to_bpinfo(pool);
331         if (!(bp_info->bp_list)) {
332                 RTE_LOG(ERR, PMD, "DPAA2 buffer pool not configured\n");
333                 return -ENOENT;
334         }
335         rte_dpaa2_mbuf_release(pool, obj_table, bp_info->bpid,
336                            bp_info->meta_data_size, n);
337
338         return 0;
339 }
340
341 static unsigned int
342 rte_hw_mbuf_get_count(const struct rte_mempool *mp)
343 {
344         int ret;
345         unsigned int num_of_bufs = 0;
346         struct dpaa2_bp_info *bp_info;
347         struct dpaa2_dpbp_dev *dpbp_node;
348
349         if (!mp || !mp->pool_data) {
350                 RTE_LOG(ERR, PMD, "Invalid mempool provided\n");
351                 return 0;
352         }
353
354         bp_info = (struct dpaa2_bp_info *)mp->pool_data;
355         dpbp_node = bp_info->bp_list->buf_pool.dpbp_node;
356
357         ret = dpbp_get_num_free_bufs(&dpbp_node->dpbp, CMD_PRI_LOW,
358                                      dpbp_node->token, &num_of_bufs);
359         if (ret) {
360                 RTE_LOG(ERR, PMD, "Unable to obtain free buf count (err=%d)\n",
361                         ret);
362                 return 0;
363         }
364
365         RTE_LOG(DEBUG, PMD, "Free bufs = %u\n", num_of_bufs);
366
367         return num_of_bufs;
368 }
369
370 struct rte_mempool_ops dpaa2_mpool_ops = {
371         .name = "dpaa2",
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,
377 };
378
379 MEMPOOL_REGISTER_OPS(dpaa2_mpool_ops);