1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2019 Marvell International Ltd.
5 #include <rte_mempool.h>
8 #include "otx2_mempool.h"
11 otx2_npa_enq(struct rte_mempool *mp, void * const *obj_table, unsigned int n)
13 unsigned int index; const uint64_t aura_handle = mp->pool_id;
14 const uint64_t reg = npa_lf_aura_handle_to_aura(aura_handle);
15 const uint64_t addr = npa_lf_aura_handle_to_base(aura_handle) +
18 for (index = 0; index < n; index++)
19 otx2_store_pair((uint64_t)obj_table[index], reg, addr);
24 static __rte_noinline int
25 npa_lf_aura_op_alloc_one(const int64_t wdata, int64_t * const addr,
26 void **obj_table, uint8_t i)
31 obj_table[i] = (void *)otx2_atomic64_add_nosync(wdata, addr);
32 if (obj_table[i] != NULL)
40 static inline int __hot
41 otx2_npa_deq(struct rte_mempool *mp, void **obj_table, unsigned int n)
43 const int64_t wdata = npa_lf_aura_handle_to_aura(mp->pool_id);
47 int64_t * const addr = (int64_t * const)
48 (npa_lf_aura_handle_to_base(mp->pool_id) +
49 NPA_LF_AURA_OP_ALLOCX(0));
50 for (index = 0; index < n; index++, obj_table++) {
51 obj = npa_lf_aura_op_alloc_one(wdata, addr, obj_table, 0);
53 for (; index > 0; index--) {
55 otx2_npa_enq(mp, obj_table, 1);
59 *obj_table = (void *)obj;
66 otx2_npa_get_count(const struct rte_mempool *mp)
68 return (unsigned int)npa_lf_aura_op_available(mp->pool_id);
72 npa_lf_aura_pool_init(struct otx2_mbox *mbox, uint32_t aura_id,
73 struct npa_aura_s *aura, struct npa_pool_s *pool)
75 struct npa_aq_enq_req *aura_init_req, *pool_init_req;
76 struct npa_aq_enq_rsp *aura_init_rsp, *pool_init_rsp;
77 struct otx2_mbox_dev *mdev = &mbox->dev[0];
80 aura_init_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
82 aura_init_req->aura_id = aura_id;
83 aura_init_req->ctype = NPA_AQ_CTYPE_AURA;
84 aura_init_req->op = NPA_AQ_INSTOP_INIT;
85 memcpy(&aura_init_req->aura, aura, sizeof(*aura));
87 pool_init_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
89 pool_init_req->aura_id = aura_id;
90 pool_init_req->ctype = NPA_AQ_CTYPE_POOL;
91 pool_init_req->op = NPA_AQ_INSTOP_INIT;
92 memcpy(&pool_init_req->pool, pool, sizeof(*pool));
94 otx2_mbox_msg_send(mbox, 0);
95 rc = otx2_mbox_wait_for_rsp(mbox, 0);
99 off = mbox->rx_start +
100 RTE_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
101 aura_init_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
102 off = mbox->rx_start + aura_init_rsp->hdr.next_msgoff;
103 pool_init_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
105 if (rc == 2 && aura_init_rsp->hdr.rc == 0 && pool_init_rsp->hdr.rc == 0)
108 return NPA_LF_ERR_AURA_POOL_INIT;
112 npa_lf_aura_pool_fini(struct otx2_mbox *mbox,
114 uint64_t aura_handle)
116 struct npa_aq_enq_req *aura_req, *pool_req;
117 struct npa_aq_enq_rsp *aura_rsp, *pool_rsp;
118 struct otx2_mbox_dev *mdev = &mbox->dev[0];
119 struct ndc_sync_op *ndc_req;
122 /* Procedure for disabling an aura/pool */
124 npa_lf_aura_op_alloc(aura_handle, 0);
126 pool_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
127 pool_req->aura_id = aura_id;
128 pool_req->ctype = NPA_AQ_CTYPE_POOL;
129 pool_req->op = NPA_AQ_INSTOP_WRITE;
130 pool_req->pool.ena = 0;
131 pool_req->pool_mask.ena = ~pool_req->pool_mask.ena;
133 aura_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
134 aura_req->aura_id = aura_id;
135 aura_req->ctype = NPA_AQ_CTYPE_AURA;
136 aura_req->op = NPA_AQ_INSTOP_WRITE;
137 aura_req->aura.ena = 0;
138 aura_req->aura_mask.ena = ~aura_req->aura_mask.ena;
140 otx2_mbox_msg_send(mbox, 0);
141 rc = otx2_mbox_wait_for_rsp(mbox, 0);
145 off = mbox->rx_start +
146 RTE_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
147 pool_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
149 off = mbox->rx_start + pool_rsp->hdr.next_msgoff;
150 aura_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
152 if (rc != 2 || aura_rsp->hdr.rc != 0 || pool_rsp->hdr.rc != 0)
153 return NPA_LF_ERR_AURA_POOL_FINI;
155 /* Sync NDC-NPA for LF */
156 ndc_req = otx2_mbox_alloc_msg_ndc_sync_op(mbox);
157 ndc_req->npa_lf_sync = 1;
159 rc = otx2_mbox_process(mbox);
161 otx2_err("Error on NDC-NPA LF sync, rc %d", rc);
162 return NPA_LF_ERR_AURA_POOL_FINI;
168 npa_lf_stack_memzone_name(struct otx2_npa_lf *lf, int pool_id, char *name)
170 snprintf(name, RTE_MEMZONE_NAMESIZE, "otx2_npa_stack_%x_%d",
171 lf->pf_func, pool_id);
176 static inline const struct rte_memzone *
177 npa_lf_stack_dma_alloc(struct otx2_npa_lf *lf, char *name,
178 int pool_id, size_t size)
180 return rte_memzone_reserve_aligned(
181 npa_lf_stack_memzone_name(lf, pool_id, name), size, 0,
182 RTE_MEMZONE_IOVA_CONTIG, OTX2_ALIGN);
186 npa_lf_stack_dma_free(struct otx2_npa_lf *lf, char *name, int pool_id)
188 const struct rte_memzone *mz;
190 mz = rte_memzone_lookup(npa_lf_stack_memzone_name(lf, pool_id, name));
194 return rte_memzone_free(mz);
198 bitmap_ctzll(uint64_t slab)
203 return __builtin_ctzll(slab);
207 npa_lf_aura_pool_pair_alloc(struct otx2_npa_lf *lf, const uint32_t block_size,
208 const uint32_t block_count, struct npa_aura_s *aura,
209 struct npa_pool_s *pool, uint64_t *aura_handle)
211 int rc, aura_id, pool_id, stack_size, alloc_size;
212 char name[RTE_MEMZONE_NAMESIZE];
213 const struct rte_memzone *mz;
218 if (!lf || !block_size || !block_count ||
219 !pool || !aura || !aura_handle)
220 return NPA_LF_ERR_PARAM;
222 /* Block size should be cache line aligned and in range of 128B-128KB */
223 if (block_size % OTX2_ALIGN || block_size < 128 ||
224 block_size > 128 * 1024)
225 return NPA_LF_ERR_INVALID_BLOCK_SZ;
228 /* Scan from the beginning */
229 __rte_bitmap_scan_init(lf->npa_bmp);
230 /* Scan bitmap to get the free pool */
231 rc = rte_bitmap_scan(lf->npa_bmp, &pos, &slab);
234 otx2_err("Mempools exhausted, 'max_pools' devargs to increase");
238 /* Get aura_id from resource bitmap */
239 aura_id = pos + bitmap_ctzll(slab);
240 /* Mark pool as reserved */
241 rte_bitmap_clear(lf->npa_bmp, aura_id);
243 /* Configuration based on each aura has separate pool(aura-pool pair) */
245 rc = (aura_id < 0 || pool_id >= (int)lf->nr_pools || aura_id >=
246 (int)BIT_ULL(6 + lf->aura_sz)) ? NPA_LF_ERR_AURA_ID_ALLOC : 0;
250 /* Allocate stack memory */
251 stack_size = (block_count + lf->stack_pg_ptrs - 1) / lf->stack_pg_ptrs;
252 alloc_size = stack_size * lf->stack_pg_bytes;
254 mz = npa_lf_stack_dma_alloc(lf, name, pool_id, alloc_size);
260 /* Update aura fields */
261 aura->pool_addr = pool_id;/* AF will translate to associated poolctx */
263 aura->shift = __builtin_clz(block_count) - 8;
264 aura->limit = block_count;
265 aura->pool_caching = 1;
266 aura->err_int_ena = BIT(NPA_AURA_ERR_INT_AURA_ADD_OVER);
267 aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_ADD_UNDER);
268 aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_FREE_UNDER);
269 aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_POOL_DIS);
270 /* Many to one reduction */
271 aura->err_qint_idx = aura_id % lf->qints;
273 /* Update pool fields */
274 pool->stack_base = mz->iova;
276 pool->buf_size = block_size / OTX2_ALIGN;
277 pool->stack_max_pages = stack_size;
278 pool->shift = __builtin_clz(block_count) - 8;
281 pool->stack_caching = 1;
282 pool->err_int_ena = BIT(NPA_POOL_ERR_INT_OVFLS);
283 pool->err_int_ena |= BIT(NPA_POOL_ERR_INT_RANGE);
284 pool->err_int_ena |= BIT(NPA_POOL_ERR_INT_PERR);
286 /* Many to one reduction */
287 pool->err_qint_idx = pool_id % lf->qints;
289 /* Issue AURA_INIT and POOL_INIT op */
290 rc = npa_lf_aura_pool_init(lf->mbox, aura_id, aura, pool);
294 *aura_handle = npa_lf_aura_handle_gen(aura_id, lf->base);
296 /* Update aura count */
297 npa_lf_aura_op_cnt_set(*aura_handle, 0, block_count);
298 /* Read it back to make sure aura count is updated */
299 npa_lf_aura_op_cnt_get(*aura_handle);
304 rte_memzone_free(mz);
306 rte_bitmap_set(lf->npa_bmp, aura_id);
312 npa_lf_aura_pool_pair_free(struct otx2_npa_lf *lf, uint64_t aura_handle)
314 char name[RTE_MEMZONE_NAMESIZE];
315 int aura_id, pool_id, rc;
317 if (!lf || !aura_handle)
318 return NPA_LF_ERR_PARAM;
320 aura_id = pool_id = npa_lf_aura_handle_to_aura(aura_handle);
321 rc = npa_lf_aura_pool_fini(lf->mbox, aura_id, aura_handle);
322 rc |= npa_lf_stack_dma_free(lf, name, pool_id);
324 rte_bitmap_set(lf->npa_bmp, aura_id);
330 otx2_npa_alloc(struct rte_mempool *mp)
332 uint32_t block_size, block_count;
333 struct otx2_npa_lf *lf;
334 struct npa_aura_s aura;
335 struct npa_pool_s pool;
336 uint64_t aura_handle;
339 lf = otx2_npa_lf_obj_get();
345 block_size = mp->elt_size + mp->header_size + mp->trailer_size;
346 block_count = mp->size;
348 if (block_size % OTX2_ALIGN != 0) {
349 otx2_err("Block size should be multiple of 128B");
354 memset(&aura, 0, sizeof(struct npa_aura_s));
355 memset(&pool, 0, sizeof(struct npa_pool_s));
359 if ((uint32_t)pool.buf_offset * OTX2_ALIGN != mp->header_size) {
360 otx2_err("Unsupported mp->header_size=%d", mp->header_size);
365 /* Use driver specific mp->pool_config to override aura config */
366 if (mp->pool_config != NULL)
367 memcpy(&aura, mp->pool_config, sizeof(struct npa_aura_s));
369 rc = npa_lf_aura_pool_pair_alloc(lf, block_size, block_count,
370 &aura, &pool, &aura_handle);
372 otx2_err("Failed to alloc pool or aura rc=%d", rc);
376 /* Store aura_handle for future queue operations */
377 mp->pool_id = aura_handle;
378 otx2_npa_dbg("lf=%p block_sz=%d block_count=%d aura_handle=0x%"PRIx64,
379 lf, block_size, block_count, aura_handle);
381 /* Just hold the reference of the object */
382 otx2_npa_lf_obj_ref();
389 otx2_npa_free(struct rte_mempool *mp)
391 struct otx2_npa_lf *lf = otx2_npa_lf_obj_get();
394 otx2_npa_dbg("lf=%p aura_handle=0x%"PRIx64, lf, mp->pool_id);
396 rc = npa_lf_aura_pool_pair_free(lf, mp->pool_id);
399 otx2_err("Failed to free pool or aura rc=%d", rc);
401 /* Release the reference of npalf */
406 otx2_npa_calc_mem_size(const struct rte_mempool *mp, uint32_t obj_num,
407 uint32_t pg_shift, size_t *min_chunk_size, size_t *align)
412 * Simply need space for one more object to be able to
413 * fulfill alignment requirements.
415 mem_size = rte_mempool_op_calc_mem_size_default(mp, obj_num + 1,
417 min_chunk_size, align);
420 * Memory area which contains objects must be physically
423 *min_chunk_size = mem_size;
430 otx2_npa_populate(struct rte_mempool *mp, unsigned int max_objs, void *vaddr,
431 rte_iova_t iova, size_t len,
432 rte_mempool_populate_obj_cb_t *obj_cb, void *obj_cb_arg)
437 if (iova == RTE_BAD_IOVA)
440 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
442 /* Align object start address to a multiple of total_elt_sz */
443 off = total_elt_sz - ((uintptr_t)vaddr % total_elt_sz);
448 vaddr = (char *)vaddr + off;
452 npa_lf_aura_op_range_set(mp->pool_id, iova, iova + len);
454 return rte_mempool_op_populate_default(mp, max_objs, vaddr, iova, len,
458 static struct rte_mempool_ops otx2_npa_ops = {
459 .name = "octeontx2_npa",
460 .alloc = otx2_npa_alloc,
461 .free = otx2_npa_free,
462 .enqueue = otx2_npa_enq,
463 .get_count = otx2_npa_get_count,
464 .calc_mem_size = otx2_npa_calc_mem_size,
465 .populate = otx2_npa_populate,
466 .dequeue = otx2_npa_deq,
469 MEMPOOL_REGISTER_OPS(otx2_npa_ops);