Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : *
3 : : * Copyright 2017,2019,2023-2025 NXP
4 : : *
5 : : */
6 : :
7 : : /* System headers */
8 : : #include <stdio.h>
9 : : #include <inttypes.h>
10 : : #include <unistd.h>
11 : : #include <limits.h>
12 : : #include <sched.h>
13 : : #include <signal.h>
14 : : #include <pthread.h>
15 : : #include <sys/types.h>
16 : : #include <sys/syscall.h>
17 : :
18 : : #include <eal_export.h>
19 : : #include <rte_byteorder.h>
20 : : #include <rte_common.h>
21 : : #include <rte_log.h>
22 : : #include <rte_debug.h>
23 : : #include <rte_memory.h>
24 : : #include <rte_tailq.h>
25 : : #include <rte_eal.h>
26 : : #include <rte_malloc.h>
27 : : #include <rte_ring.h>
28 : :
29 : : #include <dpaa_mempool.h>
30 : : #include <dpaax_iova_table.h>
31 : :
32 : : #define FMAN_ERRATA_BOUNDARY ((uint64_t)4096)
33 : : #define FMAN_ERRATA_BOUNDARY_MASK (~(FMAN_ERRATA_BOUNDARY - 1))
34 : :
35 : : /* List of all the memseg information locally maintained in dpaa driver. This
36 : : * is to optimize the PA_to_VA searches until a better mechanism (algo) is
37 : : * available.
38 : : */
39 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_dpaa_memsegs)
40 : : struct dpaa_memseg_list rte_dpaa_memsegs
41 : : = TAILQ_HEAD_INITIALIZER(rte_dpaa_memsegs);
42 : :
43 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_dpaa_bpid_info)
44 : : struct dpaa_bp_info *rte_dpaa_bpid_info;
45 : :
46 [ - + ]: 301 : RTE_LOG_REGISTER_DEFAULT(dpaa_logtype_mempool, NOTICE);
47 : : #define RTE_LOGTYPE_DPAA_MEMPOOL dpaa_logtype_mempool
48 : :
49 : : static int
50 : 0 : dpaa_mbuf_create_pool(struct rte_mempool *mp)
51 : : {
52 : : struct bman_pool *bp;
53 : : struct bm_buffer bufs[8];
54 : : struct dpaa_bp_info *bp_info;
55 : : uint8_t bpid;
56 : : int num_bufs = 0, ret = 0;
57 : : uint16_t elem_max_size;
58 : 0 : struct bman_pool_params params = {
59 : : .flags = BMAN_POOL_FLAG_DYNAMIC_BPID
60 : : };
61 : :
62 : 0 : MEMPOOL_INIT_FUNC_TRACE();
63 : :
64 [ # # ]: 0 : if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
65 : 0 : ret = rte_dpaa_portal_init((void *)0);
66 [ # # ]: 0 : if (ret) {
67 : 0 : DPAA_MEMPOOL_ERR(
68 : : "rte_dpaa_portal_init failed with ret: %d",
69 : : ret);
70 : 0 : return -1;
71 : : }
72 : : }
73 : 0 : bp = bman_new_pool(¶ms);
74 [ # # ]: 0 : if (!bp) {
75 : 0 : DPAA_MEMPOOL_ERR("bman_new_pool() failed");
76 : 0 : return -ENODEV;
77 : : }
78 : 0 : bpid = bman_get_params(bp)->bpid;
79 : :
80 : : /* Drain the pool of anything already in it. */
81 : : do {
82 : : /* Acquire is all-or-nothing, so we drain in 8s,
83 : : * then in 1s for the remainder.
84 : : */
85 [ # # ]: 0 : if (ret != 1)
86 : 0 : ret = bman_acquire(bp, bufs, 8, 0);
87 [ # # ]: 0 : if (ret < 8)
88 : 0 : ret = bman_acquire(bp, bufs, 1, 0);
89 [ # # ]: 0 : if (ret > 0)
90 : 0 : num_bufs += ret;
91 [ # # ]: 0 : } while (ret > 0);
92 [ # # ]: 0 : if (num_bufs)
93 : 0 : DPAA_MEMPOOL_WARN("drained %u bufs from BPID %d",
94 : : num_bufs, bpid);
95 : :
96 [ # # ]: 0 : if (rte_dpaa_bpid_info == NULL) {
97 : 0 : rte_dpaa_bpid_info = (struct dpaa_bp_info *)rte_zmalloc(NULL,
98 : : sizeof(struct dpaa_bp_info) * DPAA_MAX_BPOOLS,
99 : : RTE_CACHE_LINE_SIZE);
100 [ # # ]: 0 : if (rte_dpaa_bpid_info == NULL) {
101 : 0 : bman_free_pool(bp);
102 : 0 : return -ENOMEM;
103 : : }
104 : : }
105 : :
106 : : elem_max_size = rte_pktmbuf_data_room_size(mp);
107 : :
108 : 0 : rte_dpaa_bpid_info[bpid].mp = mp;
109 : 0 : rte_dpaa_bpid_info[bpid].bpid = bpid;
110 : 0 : rte_dpaa_bpid_info[bpid].size = elem_max_size;
111 : 0 : rte_dpaa_bpid_info[bpid].bp = bp;
112 : 0 : rte_dpaa_bpid_info[bpid].meta_data_size =
113 : 0 : sizeof(struct rte_mbuf) + rte_pktmbuf_priv_size(mp);
114 : 0 : rte_dpaa_bpid_info[bpid].dpaa_ops_index = mp->ops_index;
115 : 0 : rte_dpaa_bpid_info[bpid].ptov_off = 0;
116 : 0 : rte_dpaa_bpid_info[bpid].flags = 0;
117 : :
118 : 0 : bp_info = rte_malloc(NULL,
119 : : sizeof(struct dpaa_bp_info),
120 : : RTE_CACHE_LINE_SIZE);
121 [ # # ]: 0 : if (!bp_info) {
122 : 0 : DPAA_MEMPOOL_WARN("Memory allocation failed for bp_info");
123 : 0 : bman_free_pool(bp);
124 : 0 : return -ENOMEM;
125 : : }
126 : :
127 : 0 : rte_memcpy(bp_info, (void *)&rte_dpaa_bpid_info[bpid],
128 : : sizeof(struct dpaa_bp_info));
129 : 0 : mp->pool_data = (void *)bp_info;
130 : :
131 : 0 : DPAA_MEMPOOL_INFO("BMAN pool created for bpid =%d", bpid);
132 : 0 : return 0;
133 : : }
134 : :
135 : : static void
136 : 0 : dpaa_mbuf_free_pool(struct rte_mempool *mp)
137 : : {
138 : 0 : struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
139 : :
140 : 0 : MEMPOOL_INIT_FUNC_TRACE();
141 : :
142 [ # # ]: 0 : if (bp_info) {
143 : 0 : bman_free_pool(bp_info->bp);
144 : 0 : DPAA_MEMPOOL_INFO("BMAN pool freed for bpid =%d",
145 : : bp_info->bpid);
146 : 0 : rte_free(mp->pool_data);
147 : 0 : bp_info->bp = NULL;
148 : 0 : mp->pool_data = NULL;
149 : : }
150 : 0 : }
151 : :
152 : : static int
153 : 0 : dpaa_mbuf_free_bulk(struct rte_mempool *pool,
154 : : void *const *obj_table,
155 : : unsigned int count)
156 : : {
157 : 0 : struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
158 : : int ret;
159 : : uint32_t n = 0, i, left;
160 : : uint64_t phys[DPAA_MBUF_MAX_ACQ_REL];
161 : :
162 : : DPAA_MEMPOOL_DPDEBUG("Request to free %d buffers in bpid = %d",
163 : : count, bp_info->bpid);
164 : :
165 [ # # ]: 0 : if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
166 : 0 : ret = rte_dpaa_portal_init((void *)0);
167 [ # # ]: 0 : if (ret) {
168 : 0 : DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
169 : : ret);
170 : 0 : return ret;
171 : : }
172 : : }
173 : :
174 [ # # ]: 0 : while (n < count) {
175 : : /* Acquire is all-or-nothing, so we drain in 7s,
176 : : * then the remainder.
177 : : */
178 : 0 : if ((count - n) > DPAA_MBUF_MAX_ACQ_REL)
179 : : left = DPAA_MBUF_MAX_ACQ_REL;
180 : : else
181 : : left = count - n;
182 : :
183 [ # # ]: 0 : for (i = 0; i < left; i++) {
184 : 0 : phys[i] = rte_mempool_virt2iova(obj_table[n]);
185 : 0 : phys[i] += bp_info->meta_data_size;
186 : 0 : n++;
187 : : }
188 : 0 : release_again:
189 : 0 : ret = bman_release_fast(bp_info->bp, phys, left);
190 [ # # ]: 0 : if (unlikely(ret))
191 : 0 : goto release_again;
192 : : }
193 : :
194 : : DPAA_MEMPOOL_DPDEBUG("freed %d buffers in bpid =%d",
195 : : n, bp_info->bpid);
196 : :
197 : : return 0;
198 : : }
199 : :
200 : : static int
201 : 0 : dpaa_mbuf_alloc_bulk(struct rte_mempool *pool,
202 : : void **obj_table,
203 : : unsigned int count)
204 : : {
205 : : struct rte_mbuf **m = (struct rte_mbuf **)obj_table;
206 : : uint64_t bufs[DPAA_MBUF_MAX_ACQ_REL];
207 : : struct dpaa_bp_info *bp_info;
208 : : uint8_t *bufaddr;
209 : : int i, ret;
210 : : unsigned int n = 0;
211 : :
212 : 0 : bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
213 : :
214 : : DPAA_MEMPOOL_DPDEBUG("Request to alloc %d buffers in bpid = %d",
215 : : count, bp_info->bpid);
216 : :
217 [ # # ]: 0 : if (unlikely(count >= (RTE_MEMPOOL_CACHE_MAX_SIZE * 2))) {
218 : 0 : DPAA_MEMPOOL_ERR("Unable to allocate requested (%u) buffers",
219 : : count);
220 : 0 : return -EINVAL;
221 : : }
222 : :
223 [ # # ]: 0 : if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
224 : 0 : ret = rte_dpaa_portal_init((void *)0);
225 [ # # ]: 0 : if (ret) {
226 : 0 : DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
227 : : ret);
228 : 0 : return ret;
229 : : }
230 : : }
231 : :
232 [ # # ]: 0 : while (n < count) {
233 : : /* Acquire is all-or-nothing, so we drain in 7s,
234 : : * then the remainder.
235 : : */
236 [ # # ]: 0 : if ((count - n) > DPAA_MBUF_MAX_ACQ_REL) {
237 : 0 : ret = bman_acquire_fast(bp_info->bp, bufs,
238 : : DPAA_MBUF_MAX_ACQ_REL);
239 : : } else {
240 : 0 : ret = bman_acquire_fast(bp_info->bp, bufs,
241 : 0 : count - n);
242 : : }
243 : : /* In case of less than requested number of buffers available
244 : : * in pool, qbman_swp_acquire returns 0
245 : : */
246 [ # # ]: 0 : if (ret <= 0) {
247 : : DPAA_MEMPOOL_DPDEBUG("Buffer acquire failed (%d)",
248 : : ret);
249 : : /* The API expect the exact number of requested
250 : : * buffers. Releasing all buffers allocated
251 : : */
252 : 0 : dpaa_mbuf_free_bulk(pool, obj_table, n);
253 : 0 : return -ENOBUFS;
254 : : }
255 : : /* assigning mbuf from the acquired objects */
256 [ # # # # ]: 0 : for (i = 0; (i < ret) && bufs[i]; i++) {
257 : : /* TODO-errata - observed that bufs may be null
258 : : * i.e. first buffer is valid, remaining 6 buffers
259 : : * may be null.
260 : : */
261 : : bufaddr = DPAA_MEMPOOL_PTOV(bp_info, bufs[i]);
262 : 0 : m[n] = (void *)(bufaddr - bp_info->meta_data_size);
263 : : DPAA_MEMPOOL_DPDEBUG("Vaddr(%p), mbuf(%p) from BMAN",
264 : : bufaddr, m[n]);
265 : 0 : n++;
266 : : }
267 : : }
268 : :
269 : : DPAA_MEMPOOL_DPDEBUG("Allocated %d buffers from bpid=%d",
270 : : n, bp_info->bpid);
271 : : return 0;
272 : : }
273 : :
274 : : static unsigned int
275 : 0 : dpaa_mbuf_get_count(const struct rte_mempool *mp)
276 : : {
277 : : struct dpaa_bp_info *bp_info;
278 : :
279 : 0 : MEMPOOL_INIT_FUNC_TRACE();
280 : :
281 [ # # # # ]: 0 : if (!mp || !mp->pool_data) {
282 : 0 : DPAA_MEMPOOL_ERR("Invalid mempool provided");
283 : 0 : return 0;
284 : : }
285 : :
286 : : bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
287 : :
288 : 0 : return bman_query_free_buffers(bp_info->bp);
289 : : }
290 : :
291 : : static int
292 : : dpaa_check_obj_bounds(char *obj, size_t pg_sz, size_t elt_sz)
293 : : {
294 : 0 : if (!pg_sz || elt_sz > pg_sz)
295 : : return true;
296 : :
297 : 0 : if (RTE_PTR_ALIGN(obj, pg_sz) !=
298 [ # # ]: 0 : RTE_PTR_ALIGN(obj + elt_sz - 1, pg_sz))
299 : : return false;
300 : : return true;
301 : : }
302 : :
303 : : static void
304 : 0 : dpaa_adjust_obj_bounds(char *va, size_t *offset,
305 : : size_t pg_sz, size_t total, uint32_t flags)
306 : : {
307 : 0 : size_t off = *offset;
308 : :
309 [ # # ]: 0 : if (dpaa_check_obj_bounds(va + off, pg_sz, total) == false) {
310 : 0 : off += RTE_PTR_ALIGN_CEIL(va + off, pg_sz) - (va + off);
311 [ # # ]: 0 : if (flags & RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ)
312 : 0 : off += total - ((((size_t)va + off - 1) % total) + 1);
313 : : }
314 : :
315 : 0 : *offset = off;
316 : 0 : }
317 : :
318 : : static int
319 : 0 : dpaa_mbuf_ls1043a_errata_obj_adjust(uint8_t **pobj,
320 : : uint32_t header_size, size_t *poff, size_t data_room)
321 : : {
322 : 0 : uint8_t *obj = *pobj;
323 : 0 : size_t off = *poff, buf_addr, end;
324 : :
325 : : if (RTE_PKTMBUF_HEADROOM % FMAN_ERRATA_BUF_START_ALIGN) {
326 : : DPAA_MEMPOOL_ERR("RTE_PKTMBUF_HEADROOM(%d) NOT aligned to %d",
327 : : RTE_PKTMBUF_HEADROOM,
328 : : FMAN_ERRATA_BUF_START_ALIGN);
329 : : return -1;
330 : : }
331 [ # # ]: 0 : if (header_size % FMAN_ERRATA_BUF_START_ALIGN) {
332 : 0 : DPAA_MEMPOOL_ERR("Header size(%d) NOT aligned to %d",
333 : : header_size,
334 : : FMAN_ERRATA_BUF_START_ALIGN);
335 : 0 : return -1;
336 : : }
337 : :
338 : : /** All FMAN DMA start addresses (for example, BMAN buffer
339 : : * address, FD[address] + FD[offset]) are 16B aligned.
340 : : */
341 : 0 : buf_addr = (size_t)obj + header_size;
342 [ # # ]: 0 : while (!rte_is_aligned((void *)buf_addr,
343 : : FMAN_ERRATA_BUF_START_ALIGN)) {
344 : 0 : off++;
345 : 0 : obj++;
346 : 0 : buf_addr = (size_t)obj + header_size;
347 : : }
348 : :
349 : : /** Frame buffers must not span a 4KB address boundary,
350 : : * unless the frame start address is 256 byte aligned.
351 : : */
352 : 0 : end = buf_addr + data_room;
353 : 0 : if (((buf_addr + RTE_PKTMBUF_HEADROOM) &
354 [ # # ]: 0 : FMAN_ERRATA_BOUNDARY_MASK) ==
355 : : (end & FMAN_ERRATA_BOUNDARY_MASK))
356 : 0 : goto quit;
357 : :
358 [ # # ]: 0 : while (!rte_is_aligned((void *)(buf_addr + RTE_PKTMBUF_HEADROOM),
359 : : FMAN_ERRATA_4K_SPAN_ADDR_ALIGN)) {
360 : 0 : off++;
361 : 0 : obj++;
362 : 0 : buf_addr = (size_t)obj + header_size;
363 : : }
364 : 0 : quit:
365 : 0 : *pobj = obj;
366 : 0 : *poff = off;
367 : :
368 : 0 : return 0;
369 : : }
370 : :
371 : : static int
372 [ # # ]: 0 : dpaa_mbuf_op_pop_helper(struct rte_mempool *mp, uint32_t flags,
373 : : uint32_t max_objs, void *vaddr, rte_iova_t iova,
374 : : size_t len, struct dpaa_bp_info *bp_info,
375 : : rte_mempool_populate_obj_cb_t *obj_cb, void *obj_cb_arg)
376 : : {
377 : : char *va = vaddr;
378 : : size_t total_elt_sz, pg_sz, off;
379 : : uint32_t i;
380 : : void *obj;
381 : : int ret;
382 : : uint16_t data_room = rte_pktmbuf_data_room_size(mp);
383 : :
384 : 0 : ret = rte_mempool_get_page_size(mp, &pg_sz);
385 [ # # ]: 0 : if (ret < 0)
386 : : return ret;
387 : :
388 : 0 : total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
389 : :
390 [ # # ]: 0 : if (flags & RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ)
391 : 0 : off = total_elt_sz - (((uintptr_t)(va - 1) % total_elt_sz) + 1);
392 : : else
393 : 0 : off = 0;
394 [ # # ]: 0 : for (i = 0; i < max_objs; i++) {
395 : : /* avoid objects to cross page boundaries */
396 : 0 : dpaa_adjust_obj_bounds(va, &off, pg_sz, total_elt_sz, flags);
397 [ # # ]: 0 : if (off + total_elt_sz > len)
398 : : break;
399 : :
400 : 0 : off += mp->header_size;
401 : 0 : obj = va + off;
402 [ # # ]: 0 : if (dpaa_soc_ver() == SVR_LS1043A_FAMILY) {
403 : 0 : dpaa_mbuf_ls1043a_errata_obj_adjust((uint8_t **)&obj,
404 : : bp_info->meta_data_size, &off, data_room);
405 : : }
406 [ # # ]: 0 : obj_cb(mp, obj_cb_arg, obj,
407 : 0 : (iova == RTE_BAD_IOVA) ? RTE_BAD_IOVA : (iova + off));
408 : : rte_mempool_ops_enqueue_bulk(mp, &obj, 1);
409 : 0 : off += mp->elt_size + mp->trailer_size;
410 : : }
411 : :
412 : 0 : return i;
413 : : }
414 : :
415 : : static int
416 : 0 : dpaa_populate(struct rte_mempool *mp, unsigned int max_objs,
417 : : void *vaddr, rte_iova_t paddr, size_t len,
418 : : rte_mempool_populate_obj_cb_t *obj_cb, void *obj_cb_arg)
419 : : {
420 : : struct dpaa_bp_info *bp_info;
421 : : unsigned int total_elt_sz;
422 : : struct dpaa_memseg *ms;
423 : :
424 [ # # # # ]: 0 : if (!mp || !mp->pool_data) {
425 : 0 : DPAA_MEMPOOL_ERR("Invalid mempool provided");
426 [ # # ]: 0 : if (dpaa_soc_ver() == SVR_LS1043A_FAMILY) {
427 : : /** populate must be successful for LS1043A*/
428 : : return -EINVAL;
429 : : }
430 : 0 : return 0;
431 : : }
432 : :
433 : : /* Update the PA-VA Table */
434 : 0 : dpaax_iova_table_update(paddr, vaddr, len);
435 : :
436 : 0 : bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
437 : 0 : total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
438 : :
439 : : DPAA_MEMPOOL_DPDEBUG("Req size %" PRIx64 " vs Available %u\n",
440 : : (uint64_t)len, total_elt_sz * mp->size);
441 : :
442 : : /* Detect pool area has sufficient space for elements in this memzone */
443 [ # # ]: 0 : if (len >= total_elt_sz * mp->size)
444 : 0 : bp_info->flags |= DPAA_MPOOL_SINGLE_SEGMENT;
445 : :
446 : : /* For each memory chunk pinned to the Mempool, a linked list of the
447 : : * contained memsegs is created for searching when PA to VA
448 : : * conversion is required.
449 : : */
450 : 0 : ms = rte_zmalloc(NULL, sizeof(struct dpaa_memseg), 0);
451 [ # # ]: 0 : if (!ms) {
452 : 0 : DPAA_MEMPOOL_ERR("Unable to allocate internal memory.");
453 : 0 : DPAA_MEMPOOL_WARN("Fast Physical to Virtual Addr translation would not be available.");
454 : : /* If the element is not added, it would only lead to failure
455 : : * in searching for the element and the logic would Fallback
456 : : * to traditional DPDK memseg traversal code. So, this is not
457 : : * a blocking error - but, error would be printed on screen.
458 : : */
459 : 0 : return 0;
460 : : }
461 : :
462 : 0 : ms->vaddr = vaddr;
463 : 0 : ms->iova = paddr;
464 : 0 : ms->len = len;
465 : : /* Head insertions are generally faster than tail insertions as the
466 : : * buffers pinned are picked from rear end.
467 : : */
468 [ # # ]: 0 : TAILQ_INSERT_HEAD(&rte_dpaa_memsegs, ms, next);
469 : :
470 : 0 : return dpaa_mbuf_op_pop_helper(mp, 0, max_objs, vaddr, paddr,
471 : : len, bp_info, obj_cb, obj_cb_arg);
472 : : }
473 : :
474 : : static const struct rte_mempool_ops dpaa_mpool_ops = {
475 : : .name = DPAA_MEMPOOL_OPS_NAME,
476 : : .alloc = dpaa_mbuf_create_pool,
477 : : .free = dpaa_mbuf_free_pool,
478 : : .enqueue = dpaa_mbuf_free_bulk,
479 : : .dequeue = dpaa_mbuf_alloc_bulk,
480 : : .get_count = dpaa_mbuf_get_count,
481 : : .populate = dpaa_populate,
482 : : };
483 : :
484 : 301 : RTE_MEMPOOL_REGISTER_OPS(dpaa_mpool_ops);
|