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