1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Cavium, Inc
14 #include <rte_atomic.h>
16 #include <rte_bus_pci.h>
17 #include <rte_errno.h>
18 #include <rte_memory.h>
19 #include <rte_malloc.h>
20 #include <rte_spinlock.h>
23 #include "octeontx_mbox.h"
24 #include "octeontx_fpavf.h"
26 /* FPA Mbox Message */
29 #define FPA_CONFIGSET 0x1
30 #define FPA_CONFIGGET 0x2
31 #define FPA_START_COUNT 0x3
32 #define FPA_STOP_COUNT 0x4
33 #define FPA_ATTACHAURA 0x5
34 #define FPA_DETACHAURA 0x6
35 #define FPA_SETAURALVL 0x7
36 #define FPA_GETAURALVL 0x8
38 #define FPA_COPROC 0x1
41 struct octeontx_mbox_fpa_cfg {
44 uint64_t pool_stack_base;
45 uint64_t pool_stack_end;
49 struct __attribute__((__packed__)) gen_req {
53 struct __attribute__((__packed__)) idn_req {
57 struct __attribute__((__packed__)) gen_resp {
62 struct __attribute__((__packed__)) dcfg_resp {
68 uint8_t net_port_count;
69 uint8_t virt_port_count;
72 #define FPA_MAX_POOL 32
73 #define FPA_PF_PAGE_SZ 4096
75 #define FPA_LN_SIZE 128
76 #define FPA_ROUND_UP(x, size) \
77 ((((unsigned long)(x)) + size-1) & (~(size-1)))
78 #define FPA_OBJSZ_2_CACHE_LINE(sz) (((sz) + RTE_CACHE_LINE_MASK) >> 7)
79 #define FPA_CACHE_LINE_2_OBJSZ(sz) ((sz) << 7)
81 #define POOL_ENA (0x1 << 0)
82 #define POOL_DIS (0x0 << 0)
83 #define POOL_SET_NAT_ALIGN (0x1 << 1)
84 #define POOL_DIS_NAT_ALIGN (0x0 << 1)
85 #define POOL_STYPE(x) (((x) & 0x1) << 2)
86 #define POOL_LTYPE(x) (((x) & 0x3) << 3)
87 #define POOL_BUF_OFFSET(x) (((x) & 0x7fffULL) << 16)
88 #define POOL_BUF_SIZE(x) (((x) & 0x7ffULL) << 32)
91 void *pool_stack_base;
93 uint64_t stack_ln_ptr;
95 uint16_t vf_id; /* gpool_id */
96 uint16_t sz128; /* Block size in cache lines */
100 struct octeontx_fpadev {
102 uint8_t total_gpool_cnt;
103 struct fpavf_res pool[FPA_VF_MAX];
106 static struct octeontx_fpadev fpadev;
108 int octeontx_logtype_fpavf;
109 int octeontx_logtype_fpavf_mbox;
111 RTE_INIT(otx_pool_init_log)
113 octeontx_logtype_fpavf = rte_log_register("pmd.mempool.octeontx");
114 if (octeontx_logtype_fpavf >= 0)
115 rte_log_set_level(octeontx_logtype_fpavf, RTE_LOG_NOTICE);
118 /* lock is taken by caller */
120 octeontx_fpa_gpool_alloc(unsigned int object_size)
122 struct fpavf_res *res = NULL;
126 sz128 = FPA_OBJSZ_2_CACHE_LINE(object_size);
128 for (gpool = 0; gpool < FPA_VF_MAX; gpool++) {
130 /* Skip VF that is not mapped Or _inuse */
131 if ((fpadev.pool[gpool].bar0 == NULL) ||
132 (fpadev.pool[gpool].is_inuse == true))
135 res = &fpadev.pool[gpool];
137 RTE_ASSERT(res->domain_id != (uint16_t)~0);
138 RTE_ASSERT(res->vf_id != (uint16_t)~0);
139 RTE_ASSERT(res->stack_ln_ptr != 0);
141 if (res->sz128 == 0) {
144 fpavf_log_dbg("gpool %d blk_sz %d\n", gpool, sz128);
152 /* lock is taken by caller */
153 static __rte_always_inline uintptr_t
154 octeontx_fpa_gpool2handle(uint16_t gpool)
156 struct fpavf_res *res = NULL;
158 RTE_ASSERT(gpool < FPA_VF_MAX);
160 res = &fpadev.pool[gpool];
161 return (uintptr_t)res->bar0 | gpool;
164 static __rte_always_inline bool
165 octeontx_fpa_handle_valid(uintptr_t handle)
167 struct fpavf_res *res = NULL;
172 if (unlikely(!handle))
176 gpool = octeontx_fpa_bufpool_gpool(handle);
178 /* get the bar address */
179 handle &= ~(uint64_t)FPA_GPOOL_MASK;
180 for (i = 0; i < FPA_VF_MAX; i++) {
181 if ((uintptr_t)fpadev.pool[i].bar0 != handle)
188 res = &fpadev.pool[i];
190 if (res->sz128 == 0 || res->domain_id == (uint16_t)~0 ||
191 res->stack_ln_ptr == 0)
202 octeontx_fpapf_pool_setup(unsigned int gpool, unsigned int buf_size,
203 signed short buf_offset, unsigned int max_buf_count)
206 rte_iova_t phys_addr;
208 struct fpavf_res *fpa = NULL;
210 struct octeontx_mbox_hdr hdr;
211 struct dcfg_resp resp;
212 struct octeontx_mbox_fpa_cfg cfg;
215 fpa = &fpadev.pool[gpool];
216 memsz = FPA_ROUND_UP(max_buf_count / fpa->stack_ln_ptr, FPA_LN_SIZE) *
219 /* Round-up to page size */
220 memsz = (memsz + FPA_PF_PAGE_SZ - 1) & ~(uintptr_t)(FPA_PF_PAGE_SZ-1);
221 memptr = rte_malloc(NULL, memsz, RTE_CACHE_LINE_SIZE);
222 if (memptr == NULL) {
227 /* Configure stack */
228 fpa->pool_stack_base = memptr;
229 phys_addr = rte_malloc_virt2iova(memptr);
231 buf_size /= FPA_LN_SIZE;
234 hdr.coproc = FPA_COPROC;
235 hdr.msg = FPA_CONFIGSET;
236 hdr.vfid = fpa->vf_id;
239 buf_offset /= FPA_LN_SIZE;
240 reg = POOL_BUF_SIZE(buf_size) | POOL_BUF_OFFSET(buf_offset) |
241 POOL_LTYPE(0x2) | POOL_STYPE(0) | POOL_SET_NAT_ALIGN |
246 cfg.pool_stack_base = phys_addr;
247 cfg.pool_stack_end = phys_addr + memsz;
248 cfg.aura_cfg = (1 << 9);
250 ret = octeontx_mbox_send(&hdr, &cfg,
251 sizeof(struct octeontx_mbox_fpa_cfg),
252 &resp, sizeof(resp));
258 fpavf_log_dbg(" vfid %d gpool %d aid %d pool_cfg 0x%x pool_stack_base %" PRIx64 " pool_stack_end %" PRIx64" aura_cfg %" PRIx64 "\n",
259 fpa->vf_id, gpool, cfg.aid, (unsigned int)cfg.pool_cfg,
260 cfg.pool_stack_base, cfg.pool_stack_end, cfg.aura_cfg);
262 /* Now pool is in_use */
263 fpa->is_inuse = true;
273 octeontx_fpapf_pool_destroy(unsigned int gpool_index)
275 struct octeontx_mbox_hdr hdr;
276 struct dcfg_resp resp;
277 struct octeontx_mbox_fpa_cfg cfg;
278 struct fpavf_res *fpa = NULL;
281 fpa = &fpadev.pool[gpool_index];
283 hdr.coproc = FPA_COPROC;
284 hdr.msg = FPA_CONFIGSET;
285 hdr.vfid = fpa->vf_id;
288 /* reset and free the pool */
291 cfg.pool_stack_base = 0;
292 cfg.pool_stack_end = 0;
295 ret = octeontx_mbox_send(&hdr, &cfg,
296 sizeof(struct octeontx_mbox_fpa_cfg),
297 &resp, sizeof(resp));
305 /* anycase free pool stack memory */
306 rte_free(fpa->pool_stack_base);
307 fpa->pool_stack_base = NULL;
312 octeontx_fpapf_aura_attach(unsigned int gpool_index)
314 struct octeontx_mbox_hdr hdr;
315 struct dcfg_resp resp;
316 struct octeontx_mbox_fpa_cfg cfg;
319 if (gpool_index >= FPA_MAX_POOL) {
323 hdr.coproc = FPA_COPROC;
324 hdr.msg = FPA_ATTACHAURA;
325 hdr.vfid = gpool_index;
327 memset(&cfg, 0x0, sizeof(struct octeontx_mbox_fpa_cfg));
328 cfg.aid = gpool_index; /* gpool is guara */
330 ret = octeontx_mbox_send(&hdr, &cfg,
331 sizeof(struct octeontx_mbox_fpa_cfg),
332 &resp, sizeof(resp));
334 fpavf_log_err("Could not attach fpa ");
335 fpavf_log_err("aura %d to pool %d. Err=%d. FuncErr=%d\n",
336 gpool_index, gpool_index, ret, hdr.res_code);
345 octeontx_fpapf_aura_detach(unsigned int gpool_index)
347 struct octeontx_mbox_fpa_cfg cfg = {0};
348 struct octeontx_mbox_hdr hdr = {0};
351 if (gpool_index >= FPA_MAX_POOL) {
356 cfg.aid = gpool_index; /* gpool is gaura */
357 hdr.coproc = FPA_COPROC;
358 hdr.msg = FPA_DETACHAURA;
359 hdr.vfid = gpool_index;
360 ret = octeontx_mbox_send(&hdr, &cfg, sizeof(cfg), NULL, 0);
362 fpavf_log_err("Couldn't detach FPA aura %d Err=%d FuncErr=%d\n",
363 gpool_index, ret, hdr.res_code);
372 octeontx_fpavf_pool_set_range(uintptr_t handle, unsigned long memsz,
373 void *memva, uint16_t gpool)
377 if (unlikely(!handle))
380 va_end = (uintptr_t)memva + memsz;
381 va_end &= ~RTE_CACHE_LINE_MASK;
384 fpavf_write64((uintptr_t)memva,
385 (void *)((uintptr_t)handle +
386 FPA_VF_VHPOOL_START_ADDR(gpool)));
387 fpavf_write64(va_end,
388 (void *)((uintptr_t)handle +
389 FPA_VF_VHPOOL_END_ADDR(gpool)));
394 octeontx_fpapf_start_count(uint16_t gpool_index)
397 struct octeontx_mbox_hdr hdr = {0};
399 if (gpool_index >= FPA_MAX_POOL) {
404 hdr.coproc = FPA_COPROC;
405 hdr.msg = FPA_START_COUNT;
406 hdr.vfid = gpool_index;
407 ret = octeontx_mbox_send(&hdr, NULL, 0, NULL, 0);
409 fpavf_log_err("Could not start buffer counting for ");
410 fpavf_log_err("FPA pool %d. Err=%d. FuncErr=%d\n",
411 gpool_index, ret, hdr.res_code);
420 static __rte_always_inline int
421 octeontx_fpavf_free(unsigned int gpool)
425 if (gpool >= FPA_MAX_POOL) {
431 fpadev.pool[gpool].is_inuse = false;
437 static __rte_always_inline int
438 octeontx_gpool_free(uint16_t gpool)
440 if (fpadev.pool[gpool].sz128 != 0) {
441 fpadev.pool[gpool].sz128 = 0;
448 * Return buffer size for a given pool
451 octeontx_fpa_bufpool_block_size(uintptr_t handle)
453 struct fpavf_res *res = NULL;
456 if (unlikely(!octeontx_fpa_handle_valid(handle)))
460 gpool = octeontx_fpa_bufpool_gpool(handle);
461 res = &fpadev.pool[gpool];
462 return FPA_CACHE_LINE_2_OBJSZ(res->sz128);
466 octeontx_fpa_bufpool_free_count(uintptr_t handle)
468 uint64_t cnt, limit, avail;
472 if (unlikely(!octeontx_fpa_handle_valid(handle)))
476 gpool = octeontx_fpa_bufpool_gpool(handle);
478 /* Get pool bar address from handle */
479 pool_bar = handle & ~(uint64_t)FPA_GPOOL_MASK;
481 cnt = fpavf_read64((void *)((uintptr_t)pool_bar +
482 FPA_VF_VHAURA_CNT(gpool)));
483 limit = fpavf_read64((void *)((uintptr_t)pool_bar +
484 FPA_VF_VHAURA_CNT_LIMIT(gpool)));
486 avail = fpavf_read64((void *)((uintptr_t)pool_bar +
487 FPA_VF_VHPOOL_AVAILABLE(gpool)));
489 return RTE_MIN(avail, (limit - cnt));
493 octeontx_fpa_bufpool_create(unsigned int object_size, unsigned int object_count,
494 unsigned int buf_offset, int node_id)
497 uintptr_t gpool_handle;
501 RTE_SET_USED(node_id);
502 RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) > OCTEONTX_FPAVF_BUF_OFFSET);
504 object_size = RTE_CACHE_LINE_ROUNDUP(object_size);
505 if (object_size > FPA_MAX_OBJ_SIZE) {
510 rte_spinlock_lock(&fpadev.lock);
511 res = octeontx_fpa_gpool_alloc(object_size);
514 if (unlikely(res < 0)) {
522 /* get pool handle */
523 gpool_handle = octeontx_fpa_gpool2handle(gpool);
524 if (!octeontx_fpa_handle_valid(gpool_handle)) {
526 goto error_gpool_free;
529 /* Get pool bar address from handle */
530 pool_bar = gpool_handle & ~(uint64_t)FPA_GPOOL_MASK;
532 res = octeontx_fpapf_pool_setup(gpool, object_size, buf_offset,
536 goto error_gpool_free;
539 /* populate AURA fields */
540 res = octeontx_fpapf_aura_attach(gpool);
543 goto error_pool_destroy;
547 rte_spinlock_unlock(&fpadev.lock);
549 /* populate AURA registers */
550 fpavf_write64(object_count, (void *)((uintptr_t)pool_bar +
551 FPA_VF_VHAURA_CNT(gpool)));
552 fpavf_write64(object_count, (void *)((uintptr_t)pool_bar +
553 FPA_VF_VHAURA_CNT_LIMIT(gpool)));
554 fpavf_write64(object_count + 1, (void *)((uintptr_t)pool_bar +
555 FPA_VF_VHAURA_CNT_THRESHOLD(gpool)));
557 octeontx_fpapf_start_count(gpool);
562 octeontx_fpavf_free(gpool);
563 octeontx_fpapf_pool_destroy(gpool);
565 octeontx_gpool_free(gpool);
567 rte_spinlock_unlock(&fpadev.lock);
569 return (uintptr_t)NULL;
573 * Destroy a buffer pool.
576 octeontx_fpa_bufpool_destroy(uintptr_t handle, int node_id)
578 void **node, **curr, *head = NULL;
585 RTE_SET_USED(node_id);
587 /* Wait for all outstanding writes to be committed */
590 if (unlikely(!octeontx_fpa_handle_valid(handle)))
594 gpool = octeontx_fpa_bufpool_gpool(handle);
596 /* Get pool bar address from handle */
597 pool_bar = handle & ~(uint64_t)FPA_GPOOL_MASK;
599 /* Check for no outstanding buffers */
600 cnt = fpavf_read64((void *)((uintptr_t)pool_bar +
601 FPA_VF_VHAURA_CNT(gpool)));
603 fpavf_log_dbg("buffer exist in pool cnt %" PRId64 "\n", cnt);
607 rte_spinlock_lock(&fpadev.lock);
609 avail = fpavf_read64((void *)((uintptr_t)pool_bar +
610 FPA_VF_VHPOOL_AVAILABLE(gpool)));
612 /* Prepare to empty the entire POOL */
613 fpavf_write64(avail, (void *)((uintptr_t)pool_bar +
614 FPA_VF_VHAURA_CNT_LIMIT(gpool)));
615 fpavf_write64(avail + 1, (void *)((uintptr_t)pool_bar +
616 FPA_VF_VHAURA_CNT_THRESHOLD(gpool)));
619 /* Invalidate the POOL */
620 octeontx_gpool_free(gpool);
622 /* Process all buffers in the pool */
625 /* Yank a buffer from the pool */
626 node = (void *)(uintptr_t)
627 fpavf_read64((void *)
628 (pool_bar + FPA_VF_VHAURA_OP_ALLOC(gpool)));
631 fpavf_log_err("GAURA[%u] missing %" PRIx64 " buf\n",
636 /* Imsert it into an ordered linked list */
637 for (curr = &head; curr[0] != NULL; curr = curr[0]) {
638 if ((uintptr_t)node <= (uintptr_t)curr[0])
645 /* Verify the linked list to be a perfect series */
646 sz = octeontx_fpa_bufpool_block_size(handle) << 7;
647 for (curr = head; curr != NULL && curr[0] != NULL;
649 if (curr == curr[0] ||
650 ((uintptr_t)curr != ((uintptr_t)curr[0] - sz))) {
651 fpavf_log_err("POOL# %u buf sequence err (%p vs. %p)\n",
652 gpool, curr, curr[0]);
656 /* Disable pool operation */
657 fpavf_write64(~0ul, (void *)((uintptr_t)pool_bar +
658 FPA_VF_VHPOOL_START_ADDR(gpool)));
659 fpavf_write64(~0ul, (void *)((uintptr_t)pool_bar +
660 FPA_VF_VHPOOL_END_ADDR(gpool)));
662 (void)octeontx_fpapf_pool_destroy(gpool);
664 /* Deactivate the AURA */
665 fpavf_write64(0, (void *)((uintptr_t)pool_bar +
666 FPA_VF_VHAURA_CNT_LIMIT(gpool)));
667 fpavf_write64(0, (void *)((uintptr_t)pool_bar +
668 FPA_VF_VHAURA_CNT_THRESHOLD(gpool)));
670 ret = octeontx_fpapf_aura_detach(gpool);
672 fpavf_log_err("Failed to dettach gaura %u. error code=%d\n",
677 (void)octeontx_fpavf_free(gpool);
679 rte_spinlock_unlock(&fpadev.lock);
684 octeontx_fpavf_setup(void)
687 static bool init_once;
690 rte_spinlock_init(&fpadev.lock);
691 fpadev.total_gpool_cnt = 0;
693 for (i = 0; i < FPA_VF_MAX; i++) {
695 fpadev.pool[i].domain_id = ~0;
696 fpadev.pool[i].stack_ln_ptr = 0;
697 fpadev.pool[i].sz128 = 0;
698 fpadev.pool[i].bar0 = NULL;
699 fpadev.pool[i].pool_stack_base = NULL;
700 fpadev.pool[i].is_inuse = false;
707 octeontx_fpavf_identify(void *bar0)
712 uint64_t stack_ln_ptr;
714 val = fpavf_read64((void *)((uintptr_t)bar0 +
715 FPA_VF_VHAURA_CNT_THRESHOLD(0)));
717 domain_id = (val >> 8) & 0xffff;
718 vf_id = (val >> 24) & 0xffff;
720 stack_ln_ptr = fpavf_read64((void *)((uintptr_t)bar0 +
721 FPA_VF_VHPOOL_THRESHOLD(0)));
722 if (vf_id >= FPA_VF_MAX) {
723 fpavf_log_err("vf_id(%d) greater than max vf (32)\n", vf_id);
727 if (fpadev.pool[vf_id].is_inuse) {
728 fpavf_log_err("vf_id %d is_inuse\n", vf_id);
732 fpadev.pool[vf_id].domain_id = domain_id;
733 fpadev.pool[vf_id].vf_id = vf_id;
734 fpadev.pool[vf_id].bar0 = bar0;
735 fpadev.pool[vf_id].stack_ln_ptr = stack_ln_ptr;
741 /* FPAVF pcie device aka mempool probe */
743 fpavf_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
747 struct fpavf_res *fpa = NULL;
749 RTE_SET_USED(pci_drv);
752 /* For secondary processes, the primary has done all the work */
753 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
756 if (pci_dev->mem_resource[0].addr == NULL) {
757 fpavf_log_err("Empty bars %p ", pci_dev->mem_resource[0].addr);
760 idreg = pci_dev->mem_resource[0].addr;
762 octeontx_fpavf_setup();
764 res = octeontx_fpavf_identify(idreg);
768 fpa = &fpadev.pool[res];
769 fpadev.total_gpool_cnt++;
772 fpavf_log_dbg("total_fpavfs %d bar0 %p domain %d vf %d stk_ln_ptr 0x%x",
773 fpadev.total_gpool_cnt, fpa->bar0, fpa->domain_id,
774 fpa->vf_id, (unsigned int)fpa->stack_ln_ptr);
779 static const struct rte_pci_id pci_fpavf_map[] = {
781 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
782 PCI_DEVICE_ID_OCTEONTX_FPA_VF)
789 static struct rte_pci_driver pci_fpavf = {
790 .id_table = pci_fpavf_map,
791 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_IOVA_AS_VA,
792 .probe = fpavf_probe,
795 RTE_PMD_REGISTER_PCI(octeontx_fpavf, pci_fpavf);