1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
8 static roc_npa_lf_init_cb_t lf_init_cb;
11 roc_npa_lf_init_cb_register(roc_npa_lf_init_cb_t cb)
13 if (lf_init_cb != NULL)
21 roc_npa_aura_op_range_set(uint64_t aura_handle, uint64_t start_iova,
24 const uint64_t start = roc_npa_aura_handle_to_base(aura_handle) +
25 NPA_LF_POOL_OP_PTR_START0;
26 const uint64_t end = roc_npa_aura_handle_to_base(aura_handle) +
27 NPA_LF_POOL_OP_PTR_END0;
28 uint64_t reg = roc_npa_aura_handle_to_aura(aura_handle);
29 struct npa_lf *lf = idev_npa_obj_get();
30 struct npa_aura_lim *lim;
35 lim[reg].ptr_start = PLT_MIN(lim[reg].ptr_start, start_iova);
36 lim[reg].ptr_end = PLT_MAX(lim[reg].ptr_end, end_iova);
38 roc_store_pair(lim[reg].ptr_start, reg, start);
39 roc_store_pair(lim[reg].ptr_end, reg, end);
43 npa_aura_pool_init(struct mbox *mbox, uint32_t aura_id, struct npa_aura_s *aura,
44 struct npa_pool_s *pool)
46 struct npa_aq_enq_req *aura_init_req, *pool_init_req;
47 struct npa_aq_enq_rsp *aura_init_rsp, *pool_init_rsp;
48 struct mbox_dev *mdev = &mbox->dev[0];
49 int rc = -ENOSPC, off;
51 aura_init_req = mbox_alloc_msg_npa_aq_enq(mbox);
52 if (aura_init_req == NULL)
54 aura_init_req->aura_id = aura_id;
55 aura_init_req->ctype = NPA_AQ_CTYPE_AURA;
56 aura_init_req->op = NPA_AQ_INSTOP_INIT;
57 mbox_memcpy(&aura_init_req->aura, aura, sizeof(*aura));
59 pool_init_req = mbox_alloc_msg_npa_aq_enq(mbox);
60 if (pool_init_req == NULL)
62 pool_init_req->aura_id = aura_id;
63 pool_init_req->ctype = NPA_AQ_CTYPE_POOL;
64 pool_init_req->op = NPA_AQ_INSTOP_INIT;
65 mbox_memcpy(&pool_init_req->pool, pool, sizeof(*pool));
67 rc = mbox_process(mbox);
71 off = mbox->rx_start +
72 PLT_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
73 aura_init_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
74 off = mbox->rx_start + aura_init_rsp->hdr.next_msgoff;
75 pool_init_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
77 if (aura_init_rsp->hdr.rc == 0 && pool_init_rsp->hdr.rc == 0)
80 return NPA_ERR_AURA_POOL_INIT;
84 npa_aura_pool_fini(struct mbox *mbox, uint32_t aura_id, uint64_t aura_handle)
86 struct npa_aq_enq_req *aura_req, *pool_req;
87 struct npa_aq_enq_rsp *aura_rsp, *pool_rsp;
88 struct mbox_dev *mdev = &mbox->dev[0];
89 struct ndc_sync_op *ndc_req;
90 int rc = -ENOSPC, off;
93 /* Procedure for disabling an aura/pool */
96 /* Clear all the pointers from the aura */
98 ptr = roc_npa_aura_op_alloc(aura_handle, 0);
101 pool_req = mbox_alloc_msg_npa_aq_enq(mbox);
102 if (pool_req == NULL)
104 pool_req->aura_id = aura_id;
105 pool_req->ctype = NPA_AQ_CTYPE_POOL;
106 pool_req->op = NPA_AQ_INSTOP_WRITE;
107 pool_req->pool.ena = 0;
108 pool_req->pool_mask.ena = ~pool_req->pool_mask.ena;
110 aura_req = mbox_alloc_msg_npa_aq_enq(mbox);
111 if (aura_req == NULL)
113 aura_req->aura_id = aura_id;
114 aura_req->ctype = NPA_AQ_CTYPE_AURA;
115 aura_req->op = NPA_AQ_INSTOP_WRITE;
116 aura_req->aura.ena = 0;
117 aura_req->aura_mask.ena = ~aura_req->aura_mask.ena;
119 rc = mbox_process(mbox);
123 off = mbox->rx_start +
124 PLT_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
125 pool_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
127 off = mbox->rx_start + pool_rsp->hdr.next_msgoff;
128 aura_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
130 if (aura_rsp->hdr.rc != 0 || pool_rsp->hdr.rc != 0)
131 return NPA_ERR_AURA_POOL_FINI;
133 /* Sync NDC-NPA for LF */
134 ndc_req = mbox_alloc_msg_ndc_sync_op(mbox);
137 ndc_req->npa_lf_sync = 1;
138 rc = mbox_process(mbox);
140 plt_err("Error on NDC-NPA LF sync, rc %d", rc);
141 return NPA_ERR_AURA_POOL_FINI;
147 roc_npa_pool_op_pc_reset(uint64_t aura_handle)
149 struct npa_lf *lf = idev_npa_obj_get();
150 struct npa_aq_enq_req *pool_req;
151 struct npa_aq_enq_rsp *pool_rsp;
152 struct ndc_sync_op *ndc_req;
153 struct mbox_dev *mdev;
154 int rc = -ENOSPC, off;
158 return NPA_ERR_PARAM;
161 mdev = &mbox->dev[0];
162 plt_npa_dbg("lf=%p aura_handle=0x%" PRIx64, lf, aura_handle);
164 pool_req = mbox_alloc_msg_npa_aq_enq(mbox);
165 if (pool_req == NULL)
167 pool_req->aura_id = roc_npa_aura_handle_to_aura(aura_handle);
168 pool_req->ctype = NPA_AQ_CTYPE_POOL;
169 pool_req->op = NPA_AQ_INSTOP_WRITE;
170 pool_req->pool.op_pc = 0;
171 pool_req->pool_mask.op_pc = ~pool_req->pool_mask.op_pc;
173 rc = mbox_process(mbox);
177 off = mbox->rx_start +
178 PLT_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
179 pool_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
181 if (pool_rsp->hdr.rc != 0)
182 return NPA_ERR_AURA_POOL_FINI;
184 /* Sync NDC-NPA for LF */
185 ndc_req = mbox_alloc_msg_ndc_sync_op(mbox);
188 ndc_req->npa_lf_sync = 1;
189 rc = mbox_process(mbox);
191 plt_err("Error on NDC-NPA LF sync, rc %d", rc);
192 return NPA_ERR_AURA_POOL_FINI;
197 npa_stack_memzone_name(struct npa_lf *lf, int pool_id, char *name)
199 snprintf(name, PLT_MEMZONE_NAMESIZE, "roc_npa_stack_%x_%d", lf->pf_func,
204 static inline const struct plt_memzone *
205 npa_stack_dma_alloc(struct npa_lf *lf, char *name, int pool_id, size_t size)
207 const char *mz_name = npa_stack_memzone_name(lf, pool_id, name);
209 return plt_memzone_reserve_aligned(mz_name, size, 0, ROC_ALIGN);
213 npa_stack_dma_free(struct npa_lf *lf, char *name, int pool_id)
215 const struct plt_memzone *mz;
217 mz = plt_memzone_lookup(npa_stack_memzone_name(lf, pool_id, name));
219 return NPA_ERR_PARAM;
221 return plt_memzone_free(mz);
225 bitmap_ctzll(uint64_t slab)
230 return __builtin_ctzll(slab);
234 npa_aura_pool_pair_alloc(struct npa_lf *lf, const uint32_t block_size,
235 const uint32_t block_count, struct npa_aura_s *aura,
236 struct npa_pool_s *pool, uint64_t *aura_handle)
238 int rc, aura_id, pool_id, stack_size, alloc_size;
239 char name[PLT_MEMZONE_NAMESIZE];
240 const struct plt_memzone *mz;
245 if (!lf || !block_size || !block_count || !pool || !aura ||
247 return NPA_ERR_PARAM;
249 /* Block size should be cache line aligned and in range of 128B-128KB */
250 if (block_size % ROC_ALIGN || block_size < 128 ||
251 block_size > ROC_NPA_MAX_BLOCK_SZ)
252 return NPA_ERR_INVALID_BLOCK_SZ;
256 /* Scan from the beginning */
257 plt_bitmap_scan_init(lf->npa_bmp);
258 /* Scan bitmap to get the free pool */
259 rc = plt_bitmap_scan(lf->npa_bmp, &pos, &slab);
262 plt_err("Mempools exhausted");
263 return NPA_ERR_AURA_ID_ALLOC;
266 /* Get aura_id from resource bitmap */
267 aura_id = pos + bitmap_ctzll(slab);
268 /* Mark pool as reserved */
269 plt_bitmap_clear(lf->npa_bmp, aura_id);
271 /* Configuration based on each aura has separate pool(aura-pool pair) */
273 rc = (aura_id < 0 || pool_id >= (int)lf->nr_pools ||
274 aura_id >= (int)BIT_ULL(6 + lf->aura_sz)) ?
275 NPA_ERR_AURA_ID_ALLOC :
280 /* Allocate stack memory */
281 stack_size = (block_count + lf->stack_pg_ptrs - 1) / lf->stack_pg_ptrs;
282 alloc_size = stack_size * lf->stack_pg_bytes;
284 mz = npa_stack_dma_alloc(lf, name, pool_id, alloc_size);
290 /* Update aura fields */
291 aura->pool_addr = pool_id; /* AF will translate to associated poolctx */
293 aura->shift = plt_log2_u32(block_count);
294 aura->shift = aura->shift < 8 ? 0 : aura->shift - 8;
295 aura->limit = block_count;
296 aura->pool_caching = 1;
297 aura->err_int_ena = BIT(NPA_AURA_ERR_INT_AURA_ADD_OVER);
298 aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_ADD_UNDER);
299 aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_FREE_UNDER);
300 aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_POOL_DIS);
301 aura->avg_con = ROC_NPA_AVG_CONT;
302 /* Many to one reduction */
303 aura->err_qint_idx = aura_id % lf->qints;
305 /* Update pool fields */
306 pool->stack_base = mz->iova;
308 pool->buf_size = block_size / ROC_ALIGN;
309 pool->stack_max_pages = stack_size;
310 pool->shift = plt_log2_u32(block_count);
311 pool->shift = pool->shift < 8 ? 0 : pool->shift - 8;
314 pool->stack_caching = 1;
315 pool->err_int_ena = BIT(NPA_POOL_ERR_INT_OVFLS);
316 pool->err_int_ena |= BIT(NPA_POOL_ERR_INT_RANGE);
317 pool->err_int_ena |= BIT(NPA_POOL_ERR_INT_PERR);
318 pool->avg_con = ROC_NPA_AVG_CONT;
320 /* Many to one reduction */
321 pool->err_qint_idx = pool_id % lf->qints;
323 /* Issue AURA_INIT and POOL_INIT op */
324 rc = npa_aura_pool_init(lf->mbox, aura_id, aura, pool);
328 *aura_handle = roc_npa_aura_handle_gen(aura_id, lf->base);
329 /* Update aura count */
330 roc_npa_aura_op_cnt_set(*aura_handle, 0, block_count);
331 /* Read it back to make sure aura count is updated */
332 roc_npa_aura_op_cnt_get(*aura_handle);
337 plt_memzone_free(mz);
339 plt_bitmap_set(lf->npa_bmp, aura_id);
345 roc_npa_pool_create(uint64_t *aura_handle, uint32_t block_size,
346 uint32_t block_count, struct npa_aura_s *aura,
347 struct npa_pool_s *pool)
349 struct npa_aura_s defaura;
350 struct npa_pool_s defpool;
351 struct idev_cfg *idev;
355 lf = idev_npa_obj_get();
357 rc = NPA_ERR_DEVICE_NOT_BOUNDED;
361 idev = idev_get_cfg();
368 memset(&defaura, 0, sizeof(struct npa_aura_s));
372 memset(&defpool, 0, sizeof(struct npa_pool_s));
373 defpool.nat_align = 1;
374 defpool.buf_offset = 1;
378 rc = npa_aura_pool_pair_alloc(lf, block_size, block_count, aura, pool,
381 plt_err("Failed to alloc pool or aura rc=%d", rc);
385 plt_npa_dbg("lf=%p block_sz=%d block_count=%d aura_handle=0x%" PRIx64,
386 lf, block_size, block_count, *aura_handle);
388 /* Just hold the reference of the object */
389 __atomic_fetch_add(&idev->npa_refcnt, 1, __ATOMIC_SEQ_CST);
395 roc_npa_aura_limit_modify(uint64_t aura_handle, uint16_t aura_limit)
397 struct npa_aq_enq_req *aura_req;
401 lf = idev_npa_obj_get();
403 return NPA_ERR_DEVICE_NOT_BOUNDED;
405 aura_req = mbox_alloc_msg_npa_aq_enq(lf->mbox);
406 if (aura_req == NULL)
408 aura_req->aura_id = roc_npa_aura_handle_to_aura(aura_handle);
409 aura_req->ctype = NPA_AQ_CTYPE_AURA;
410 aura_req->op = NPA_AQ_INSTOP_WRITE;
412 aura_req->aura.limit = aura_limit;
413 aura_req->aura_mask.limit = ~(aura_req->aura_mask.limit);
414 rc = mbox_process(lf->mbox);
420 npa_aura_pool_pair_free(struct npa_lf *lf, uint64_t aura_handle)
422 char name[PLT_MEMZONE_NAMESIZE];
423 int aura_id, pool_id, rc;
425 if (!lf || !aura_handle)
426 return NPA_ERR_PARAM;
428 aura_id = roc_npa_aura_handle_to_aura(aura_handle);
430 rc = npa_aura_pool_fini(lf->mbox, aura_id, aura_handle);
431 rc |= npa_stack_dma_free(lf, name, pool_id);
433 plt_bitmap_set(lf->npa_bmp, aura_id);
439 roc_npa_pool_destroy(uint64_t aura_handle)
441 struct npa_lf *lf = idev_npa_obj_get();
444 plt_npa_dbg("lf=%p aura_handle=0x%" PRIx64, lf, aura_handle);
445 rc = npa_aura_pool_pair_free(lf, aura_handle);
447 plt_err("Failed to destroy pool or aura rc=%d", rc);
449 /* Release the reference of npa */
455 roc_npa_pool_range_update_check(uint64_t aura_handle)
457 uint64_t aura_id = roc_npa_aura_handle_to_aura(aura_handle);
459 struct npa_aura_lim *lim;
460 __io struct npa_pool_s *pool;
461 struct npa_aq_enq_req *req;
462 struct npa_aq_enq_rsp *rsp;
465 lf = idev_npa_obj_get();
467 return NPA_ERR_PARAM;
471 req = mbox_alloc_msg_npa_aq_enq(lf->mbox);
475 req->aura_id = aura_id;
476 req->ctype = NPA_AQ_CTYPE_POOL;
477 req->op = NPA_AQ_INSTOP_READ;
479 rc = mbox_process_msg(lf->mbox, (void *)&rsp);
481 plt_err("Failed to get pool(0x%" PRIx64 ") context", aura_id);
486 if (lim[aura_id].ptr_start != pool->ptr_start ||
487 lim[aura_id].ptr_end != pool->ptr_end) {
488 plt_err("Range update failed on pool(0x%" PRIx64 ")", aura_id);
489 return NPA_ERR_PARAM;
496 npa_attach(struct mbox *mbox)
498 struct rsrc_attach_req *req;
500 req = mbox_alloc_msg_attach_resources(mbox);
506 return mbox_process(mbox);
510 npa_detach(struct mbox *mbox)
512 struct rsrc_detach_req *req;
514 req = mbox_alloc_msg_detach_resources(mbox);
520 return mbox_process(mbox);
524 npa_get_msix_offset(struct mbox *mbox, uint16_t *npa_msixoff)
526 struct msix_offset_rsp *msix_rsp;
529 /* Get NPA MSIX vector offsets */
530 mbox_alloc_msg_msix_offset(mbox);
531 rc = mbox_process_msg(mbox, (void *)&msix_rsp);
533 *npa_msixoff = msix_rsp->npa_msixoff;
539 npa_lf_alloc(struct npa_lf *lf)
541 struct mbox *mbox = lf->mbox;
542 struct npa_lf_alloc_req *req;
543 struct npa_lf_alloc_rsp *rsp;
546 req = mbox_alloc_msg_npa_lf_alloc(mbox);
549 req->aura_sz = lf->aura_sz;
550 req->nr_pools = lf->nr_pools;
552 rc = mbox_process_msg(mbox, (void *)&rsp);
554 return NPA_ERR_ALLOC;
556 lf->stack_pg_ptrs = rsp->stack_pg_ptrs;
557 lf->stack_pg_bytes = rsp->stack_pg_bytes;
558 lf->qints = rsp->qints;
564 npa_lf_free(struct mbox *mbox)
566 mbox_alloc_msg_npa_lf_free(mbox);
567 return mbox_process(mbox);
570 static inline uint32_t
571 aura_size_to_u32(uint8_t val)
573 if (val == NPA_AURA_SZ_0)
575 if (val >= NPA_AURA_SZ_MAX)
578 return 1 << (val + 6);
582 pool_count_aura_sz_get(uint32_t *nr_pools, uint8_t *aura_sz)
586 val = roc_idev_npa_maxpools_get();
587 if (val < aura_size_to_u32(NPA_AURA_SZ_128))
589 if (val > aura_size_to_u32(NPA_AURA_SZ_1M))
592 roc_idev_npa_maxpools_set(val);
594 *aura_sz = plt_log2_u32(val) - 6;
598 npa_dev_init(struct npa_lf *lf, uintptr_t base, struct mbox *mbox)
600 uint32_t i, bmp_sz, nr_pools;
605 if (!lf || !base || !mbox)
606 return NPA_ERR_PARAM;
608 if (base & ROC_AURA_ID_MASK)
609 return NPA_ERR_BASE_INVALID;
611 pool_count_aura_sz_get(&nr_pools, &aura_sz);
612 if (aura_sz == NPA_AURA_SZ_0 || aura_sz >= NPA_AURA_SZ_MAX)
613 return NPA_ERR_PARAM;
615 memset(lf, 0x0, sizeof(*lf));
617 lf->aura_sz = aura_sz;
618 lf->nr_pools = nr_pools;
621 rc = npa_lf_alloc(lf);
625 bmp_sz = plt_bitmap_get_memory_footprint(nr_pools);
627 /* Allocate memory for bitmap */
628 lf->npa_bmp_mem = plt_zmalloc(bmp_sz, ROC_ALIGN);
629 if (lf->npa_bmp_mem == NULL) {
634 /* Initialize pool resource bitmap array */
635 lf->npa_bmp = plt_bitmap_init(nr_pools, lf->npa_bmp_mem, bmp_sz);
636 if (lf->npa_bmp == NULL) {
641 /* Mark all pools available */
642 for (i = 0; i < nr_pools; i++)
643 plt_bitmap_set(lf->npa_bmp, i);
645 /* Allocate memory for qint context */
646 lf->npa_qint_mem = plt_zmalloc(sizeof(struct npa_qint) * nr_pools, 0);
647 if (lf->npa_qint_mem == NULL) {
652 /* Allocate memory for nap_aura_lim memory */
653 lf->aura_lim = plt_zmalloc(sizeof(struct npa_aura_lim) * nr_pools, 0);
654 if (lf->aura_lim == NULL) {
659 /* Init aura start & end limits */
660 for (i = 0; i < nr_pools; i++) {
661 lf->aura_lim[i].ptr_start = UINT64_MAX;
662 lf->aura_lim[i].ptr_end = 0x0ull;
668 plt_free(lf->npa_qint_mem);
670 plt_bitmap_free(lf->npa_bmp);
672 plt_free(lf->npa_bmp_mem);
674 npa_lf_free(lf->mbox);
680 npa_dev_fini(struct npa_lf *lf)
683 return NPA_ERR_PARAM;
685 plt_free(lf->aura_lim);
686 plt_free(lf->npa_qint_mem);
687 plt_bitmap_free(lf->npa_bmp);
688 plt_free(lf->npa_bmp_mem);
690 return npa_lf_free(lf->mbox);
694 npa_lf_init(struct dev *dev, struct plt_pci_device *pci_dev)
696 struct idev_cfg *idev;
697 uint16_t npa_msixoff;
701 idev = idev_get_cfg();
703 return NPA_ERR_ALLOC;
705 /* Not the first PCI device */
706 if (__atomic_fetch_add(&idev->npa_refcnt, 1, __ATOMIC_SEQ_CST) != 0)
710 rc = (*lf_init_cb)(pci_dev);
715 rc = npa_attach(dev->mbox);
719 rc = npa_get_msix_offset(dev->mbox, &npa_msixoff);
724 rc = npa_dev_init(lf, dev->bar2 + (RVU_BLOCK_ADDR_NPA << 20),
729 lf->pf_func = dev->pf_func;
730 lf->npa_msixoff = npa_msixoff;
731 lf->intr_handle = pci_dev->intr_handle;
732 lf->pci_dev = pci_dev;
734 idev->npa_pf_func = dev->pf_func;
738 rc = npa_register_irqs(lf);
742 plt_npa_dbg("npa=%p max_pools=%d pf_func=0x%x msix=0x%x", lf,
743 roc_idev_npa_maxpools_get(), lf->pf_func, npa_msixoff);
748 npa_dev_fini(idev->npa);
750 npa_detach(dev->mbox);
752 __atomic_fetch_sub(&idev->npa_refcnt, 1, __ATOMIC_SEQ_CST);
759 struct idev_cfg *idev;
762 idev = idev_get_cfg();
764 return NPA_ERR_ALLOC;
766 /* Not the last PCI device */
767 if (__atomic_sub_fetch(&idev->npa_refcnt, 1, __ATOMIC_SEQ_CST) != 0)
770 npa_unregister_irqs(idev->npa);
771 rc |= npa_dev_fini(idev->npa);
772 rc |= npa_detach(idev->npa->mbox);
773 idev_set_defaults(idev);
779 roc_npa_dev_init(struct roc_npa *roc_npa)
781 struct plt_pci_device *pci_dev;
786 if (roc_npa == NULL || roc_npa->pci_dev == NULL)
787 return NPA_ERR_PARAM;
789 PLT_STATIC_ASSERT(sizeof(struct npa) <= ROC_NPA_MEM_SZ);
790 npa = roc_npa_to_npa_priv(roc_npa);
791 memset(npa, 0, sizeof(*npa));
792 pci_dev = roc_npa->pci_dev;
795 /* Initialize device */
796 rc = dev_init(dev, pci_dev);
798 plt_err("Failed to init roc device");
802 npa->pci_dev = pci_dev;
803 dev->drv_inited = true;
809 roc_npa_dev_fini(struct roc_npa *roc_npa)
811 struct npa *npa = roc_npa_to_npa_priv(roc_npa);
814 return NPA_ERR_PARAM;
816 npa->dev.drv_inited = false;
817 return dev_fini(&npa->dev, npa->pci_dev);