Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : *
3 : : * Copyright 2017,2019,2023 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 : : /* List of all the memseg information locally maintained in dpaa driver. This
33 : : * is to optimize the PA_to_VA searches until a better mechanism (algo) is
34 : : * available.
35 : : */
36 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_dpaa_memsegs)
37 : : struct dpaa_memseg_list rte_dpaa_memsegs
38 : : = TAILQ_HEAD_INITIALIZER(rte_dpaa_memsegs);
39 : :
40 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_dpaa_bpid_info)
41 : : struct dpaa_bp_info *rte_dpaa_bpid_info;
42 : :
43 [ - + ]: 252 : RTE_LOG_REGISTER_DEFAULT(dpaa_logtype_mempool, NOTICE);
44 : : #define RTE_LOGTYPE_DPAA_MEMPOOL dpaa_logtype_mempool
45 : :
46 : : static int
47 : 0 : dpaa_mbuf_create_pool(struct rte_mempool *mp)
48 : : {
49 : : struct bman_pool *bp;
50 : : struct bm_buffer bufs[8];
51 : : struct dpaa_bp_info *bp_info;
52 : : uint8_t bpid;
53 : : int num_bufs = 0, ret = 0;
54 : 0 : struct bman_pool_params params = {
55 : : .flags = BMAN_POOL_FLAG_DYNAMIC_BPID
56 : : };
57 : : unsigned int lcore_id;
58 : : struct rte_mempool_cache *cache;
59 : :
60 : 0 : MEMPOOL_INIT_FUNC_TRACE();
61 : :
62 [ # # ]: 0 : if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
63 : 0 : ret = rte_dpaa_portal_init((void *)0);
64 [ # # ]: 0 : if (ret) {
65 : 0 : DPAA_MEMPOOL_ERR(
66 : : "rte_dpaa_portal_init failed with ret: %d",
67 : : ret);
68 : 0 : return -1;
69 : : }
70 : : }
71 : 0 : bp = bman_new_pool(¶ms);
72 [ # # ]: 0 : if (!bp) {
73 : 0 : DPAA_MEMPOOL_ERR("bman_new_pool() failed");
74 : 0 : return -ENODEV;
75 : : }
76 : 0 : bpid = bman_get_params(bp)->bpid;
77 : :
78 : : /* Drain the pool of anything already in it. */
79 : : do {
80 : : /* Acquire is all-or-nothing, so we drain in 8s,
81 : : * then in 1s for the remainder.
82 : : */
83 [ # # ]: 0 : if (ret != 1)
84 : 0 : ret = bman_acquire(bp, bufs, 8, 0);
85 [ # # ]: 0 : if (ret < 8)
86 : 0 : ret = bman_acquire(bp, bufs, 1, 0);
87 [ # # ]: 0 : if (ret > 0)
88 : 0 : num_bufs += ret;
89 [ # # ]: 0 : } while (ret > 0);
90 [ # # ]: 0 : if (num_bufs)
91 : 0 : DPAA_MEMPOOL_WARN("drained %u bufs from BPID %d",
92 : : num_bufs, bpid);
93 : :
94 [ # # ]: 0 : if (rte_dpaa_bpid_info == NULL) {
95 : 0 : rte_dpaa_bpid_info = (struct dpaa_bp_info *)rte_zmalloc(NULL,
96 : : sizeof(struct dpaa_bp_info) * DPAA_MAX_BPOOLS,
97 : : RTE_CACHE_LINE_SIZE);
98 [ # # ]: 0 : if (rte_dpaa_bpid_info == NULL) {
99 : 0 : bman_free_pool(bp);
100 : 0 : return -ENOMEM;
101 : : }
102 : : }
103 : :
104 : 0 : rte_dpaa_bpid_info[bpid].mp = mp;
105 : 0 : rte_dpaa_bpid_info[bpid].bpid = bpid;
106 : 0 : rte_dpaa_bpid_info[bpid].size = mp->elt_size;
107 [ # # ]: 0 : rte_dpaa_bpid_info[bpid].bp = bp;
108 : 0 : rte_dpaa_bpid_info[bpid].meta_data_size =
109 : 0 : sizeof(struct rte_mbuf) + rte_pktmbuf_priv_size(mp);
110 : 0 : rte_dpaa_bpid_info[bpid].dpaa_ops_index = mp->ops_index;
111 : 0 : rte_dpaa_bpid_info[bpid].ptov_off = 0;
112 : 0 : rte_dpaa_bpid_info[bpid].flags = 0;
113 : :
114 : 0 : bp_info = rte_malloc(NULL,
115 : : sizeof(struct dpaa_bp_info),
116 : : RTE_CACHE_LINE_SIZE);
117 [ # # ]: 0 : if (!bp_info) {
118 : 0 : DPAA_MEMPOOL_WARN("Memory allocation failed for bp_info");
119 : 0 : bman_free_pool(bp);
120 : 0 : return -ENOMEM;
121 : : }
122 : :
123 [ # # ]: 0 : rte_memcpy(bp_info, (void *)&rte_dpaa_bpid_info[bpid],
124 : : sizeof(struct dpaa_bp_info));
125 : 0 : mp->pool_data = (void *)bp_info;
126 : : /* Update per core mempool cache threshold to optimal value which is
127 : : * number of buffers that can be released to HW buffer pool in
128 : : * a single API call.
129 : : */
130 [ # # ]: 0 : for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
131 : 0 : cache = &mp->local_cache[lcore_id];
132 : 0 : DPAA_MEMPOOL_DEBUG("lCore %d: cache->flushthresh %d -> %d",
133 : : lcore_id, cache->flushthresh,
134 : : (uint32_t)(cache->size + DPAA_MBUF_MAX_ACQ_REL));
135 [ # # ]: 0 : if (cache->flushthresh)
136 : 0 : cache->flushthresh = cache->size + DPAA_MBUF_MAX_ACQ_REL;
137 : : }
138 : :
139 : 0 : DPAA_MEMPOOL_INFO("BMAN pool created for bpid =%d", bpid);
140 : 0 : return 0;
141 : : }
142 : :
143 : : static void
144 : 0 : dpaa_mbuf_free_pool(struct rte_mempool *mp)
145 : : {
146 : 0 : struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
147 : :
148 : 0 : MEMPOOL_INIT_FUNC_TRACE();
149 : :
150 [ # # ]: 0 : if (bp_info) {
151 : 0 : bman_free_pool(bp_info->bp);
152 : 0 : DPAA_MEMPOOL_INFO("BMAN pool freed for bpid =%d",
153 : : bp_info->bpid);
154 : 0 : rte_free(mp->pool_data);
155 : 0 : bp_info->bp = NULL;
156 : 0 : mp->pool_data = NULL;
157 : : }
158 : 0 : }
159 : :
160 : : static void
161 : 0 : dpaa_buf_free(struct dpaa_bp_info *bp_info, uint64_t addr)
162 : : {
163 : : struct bm_buffer buf;
164 : : int ret;
165 : :
166 : : DPAA_MEMPOOL_DPDEBUG("Free 0x%" PRIx64 " to bpid: %d",
167 : : addr, bp_info->bpid);
168 : :
169 : 0 : bm_buffer_set64(&buf, addr);
170 : 0 : retry:
171 : 0 : ret = bman_release(bp_info->bp, &buf, 1, 0);
172 [ # # ]: 0 : if (ret) {
173 : 0 : DPAA_MEMPOOL_DEBUG("BMAN busy. Retrying...");
174 : : cpu_spin(CPU_SPIN_BACKOFF_CYCLES);
175 : 0 : goto retry;
176 : : }
177 : 0 : }
178 : :
179 : : static int
180 : 0 : dpaa_mbuf_free_bulk(struct rte_mempool *pool,
181 : : void *const *obj_table,
182 : : unsigned int n)
183 : : {
184 : 0 : struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
185 : : int ret;
186 : : unsigned int i = 0;
187 : :
188 : : DPAA_MEMPOOL_DPDEBUG("Request to free %d buffers in bpid = %d",
189 : : n, bp_info->bpid);
190 : :
191 [ # # ]: 0 : if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
192 : 0 : ret = rte_dpaa_portal_init((void *)0);
193 [ # # ]: 0 : if (ret) {
194 : 0 : DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
195 : : ret);
196 : 0 : return 0;
197 : : }
198 : : }
199 : :
200 [ # # ]: 0 : while (i < n) {
201 [ # # ]: 0 : uint64_t phy = rte_mempool_virt2iova(obj_table[i]);
202 : :
203 [ # # ]: 0 : if (unlikely(!bp_info->ptov_off)) {
204 : : /* buffers are from single mem segment */
205 [ # # ]: 0 : if (bp_info->flags & DPAA_MPOOL_SINGLE_SEGMENT) {
206 : 0 : bp_info->ptov_off = (size_t)obj_table[i] - phy;
207 : 0 : rte_dpaa_bpid_info[bp_info->bpid].ptov_off
208 : 0 : = bp_info->ptov_off;
209 : : }
210 : : }
211 : :
212 : 0 : dpaa_buf_free(bp_info,
213 : 0 : (uint64_t)phy + bp_info->meta_data_size);
214 : 0 : i = i + 1;
215 : : }
216 : :
217 : : DPAA_MEMPOOL_DPDEBUG("freed %d buffers in bpid =%d",
218 : : n, bp_info->bpid);
219 : :
220 : : return 0;
221 : : }
222 : :
223 : : static int
224 : 0 : dpaa_mbuf_alloc_bulk(struct rte_mempool *pool,
225 : : void **obj_table,
226 : : unsigned int count)
227 : : {
228 : : struct rte_mbuf **m = (struct rte_mbuf **)obj_table;
229 : : struct bm_buffer bufs[DPAA_MBUF_MAX_ACQ_REL];
230 : : struct dpaa_bp_info *bp_info;
231 : : void *bufaddr;
232 : : int i, ret;
233 : : unsigned int n = 0;
234 : :
235 : 0 : bp_info = DPAA_MEMPOOL_TO_POOL_INFO(pool);
236 : :
237 : : DPAA_MEMPOOL_DPDEBUG("Request to alloc %d buffers in bpid = %d",
238 : : count, bp_info->bpid);
239 : :
240 [ # # ]: 0 : if (unlikely(count >= (RTE_MEMPOOL_CACHE_MAX_SIZE * 2))) {
241 : 0 : DPAA_MEMPOOL_ERR("Unable to allocate requested (%u) buffers",
242 : : count);
243 : 0 : return -1;
244 : : }
245 : :
246 [ # # ]: 0 : if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
247 : 0 : ret = rte_dpaa_portal_init((void *)0);
248 [ # # ]: 0 : if (ret) {
249 : 0 : DPAA_MEMPOOL_ERR("rte_dpaa_portal_init failed with ret: %d",
250 : : ret);
251 : 0 : return -1;
252 : : }
253 : : }
254 : :
255 [ # # ]: 0 : while (n < count) {
256 : : /* Acquire is all-or-nothing, so we drain in 7s,
257 : : * then the remainder.
258 : : */
259 [ # # ]: 0 : if ((count - n) > DPAA_MBUF_MAX_ACQ_REL) {
260 : 0 : ret = bman_acquire(bp_info->bp, bufs,
261 : : DPAA_MBUF_MAX_ACQ_REL, 0);
262 : : } else {
263 : 0 : ret = bman_acquire(bp_info->bp, bufs, count - n, 0);
264 : : }
265 : : /* In case of less than requested number of buffers available
266 : : * in pool, qbman_swp_acquire returns 0
267 : : */
268 [ # # ]: 0 : if (ret <= 0) {
269 : : DPAA_MEMPOOL_DPDEBUG("Buffer acquire failed (%d)",
270 : : ret);
271 : : /* The API expect the exact number of requested
272 : : * buffers. Releasing all buffers allocated
273 : : */
274 : 0 : dpaa_mbuf_free_bulk(pool, obj_table, n);
275 : 0 : return -ENOBUFS;
276 : : }
277 : : /* assigning mbuf from the acquired objects */
278 [ # # # # ]: 0 : for (i = 0; (i < ret) && bufs[i].addr; i++) {
279 : : /* TODO-errata - observed that bufs may be null
280 : : * i.e. first buffer is valid, remaining 6 buffers
281 : : * may be null.
282 : : */
283 : 0 : bufaddr = DPAA_MEMPOOL_PTOV(bp_info, bufs[i].addr);
284 : 0 : m[n] = (struct rte_mbuf *)((char *)bufaddr
285 : 0 : - bp_info->meta_data_size);
286 : : DPAA_MEMPOOL_DPDEBUG("Paddr (%p), FD (%p) from BMAN",
287 : : (void *)bufaddr, (void *)m[n]);
288 : 0 : n++;
289 : : }
290 : : }
291 : :
292 : : DPAA_MEMPOOL_DPDEBUG("Allocated %d buffers from bpid=%d",
293 : : n, bp_info->bpid);
294 : : return 0;
295 : : }
296 : :
297 : : static unsigned int
298 : 0 : dpaa_mbuf_get_count(const struct rte_mempool *mp)
299 : : {
300 : : struct dpaa_bp_info *bp_info;
301 : :
302 : 0 : MEMPOOL_INIT_FUNC_TRACE();
303 : :
304 [ # # # # ]: 0 : if (!mp || !mp->pool_data) {
305 : 0 : DPAA_MEMPOOL_ERR("Invalid mempool provided");
306 : 0 : return 0;
307 : : }
308 : :
309 : : bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
310 : :
311 : 0 : return bman_query_free_buffers(bp_info->bp);
312 : : }
313 : :
314 : : static int
315 : 0 : dpaa_populate(struct rte_mempool *mp, unsigned int max_objs,
316 : : void *vaddr, rte_iova_t paddr, size_t len,
317 : : rte_mempool_populate_obj_cb_t *obj_cb, void *obj_cb_arg)
318 : : {
319 : : struct dpaa_bp_info *bp_info;
320 : : unsigned int total_elt_sz;
321 : :
322 [ # # # # ]: 0 : if (!mp || !mp->pool_data) {
323 : 0 : DPAA_MEMPOOL_ERR("Invalid mempool provided");
324 : 0 : return 0;
325 : : }
326 : :
327 : : /* Update the PA-VA Table */
328 : 0 : dpaax_iova_table_update(paddr, vaddr, len);
329 : :
330 : 0 : bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
331 : 0 : total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
332 : :
333 : : DPAA_MEMPOOL_DPDEBUG("Req size %" PRIx64 " vs Available %u\n",
334 : : (uint64_t)len, total_elt_sz * mp->size);
335 : :
336 : : /* Detect pool area has sufficient space for elements in this memzone */
337 [ # # ]: 0 : if (len >= total_elt_sz * mp->size)
338 : 0 : bp_info->flags |= DPAA_MPOOL_SINGLE_SEGMENT;
339 : : struct dpaa_memseg *ms;
340 : :
341 : : /* For each memory chunk pinned to the Mempool, a linked list of the
342 : : * contained memsegs is created for searching when PA to VA
343 : : * conversion is required.
344 : : */
345 : 0 : ms = rte_zmalloc(NULL, sizeof(struct dpaa_memseg), 0);
346 [ # # ]: 0 : if (!ms) {
347 : 0 : DPAA_MEMPOOL_ERR("Unable to allocate internal memory.");
348 : 0 : DPAA_MEMPOOL_WARN("Fast Physical to Virtual Addr translation would not be available.");
349 : : /* If the element is not added, it would only lead to failure
350 : : * in searching for the element and the logic would Fallback
351 : : * to traditional DPDK memseg traversal code. So, this is not
352 : : * a blocking error - but, error would be printed on screen.
353 : : */
354 : 0 : return 0;
355 : : }
356 : :
357 : 0 : ms->vaddr = vaddr;
358 : 0 : ms->iova = paddr;
359 : 0 : ms->len = len;
360 : : /* Head insertions are generally faster than tail insertions as the
361 : : * buffers pinned are picked from rear end.
362 : : */
363 [ # # ]: 0 : TAILQ_INSERT_HEAD(&rte_dpaa_memsegs, ms, next);
364 : :
365 : 0 : return rte_mempool_op_populate_helper(mp, 0, max_objs, vaddr, paddr,
366 : : len, obj_cb, obj_cb_arg);
367 : : }
368 : :
369 : : static const struct rte_mempool_ops dpaa_mpool_ops = {
370 : : .name = DPAA_MEMPOOL_OPS_NAME,
371 : : .alloc = dpaa_mbuf_create_pool,
372 : : .free = dpaa_mbuf_free_pool,
373 : : .enqueue = dpaa_mbuf_free_bulk,
374 : : .dequeue = dpaa_mbuf_alloc_bulk,
375 : : .get_count = dpaa_mbuf_get_count,
376 : : .populate = dpaa_populate,
377 : : };
378 : :
379 : 252 : RTE_MEMPOOL_REGISTER_OPS(dpaa_mpool_ops);
|