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