1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4 
5 #include <rte_mbuf_pool_ops.h>
6 #include <rte_mempool.h>
7 
8 #include "roc_api.h"
9 #include "cnxk_mempool.h"
10 
11 int __rte_hot
cnxk_mempool_enq(struct rte_mempool * mp,void * const * obj_table,unsigned int n)12 cnxk_mempool_enq(struct rte_mempool *mp, void *const *obj_table, unsigned int n)
13 {
14 	unsigned int index;
15 
16 	/* Ensure mbuf init changes are written before the free pointers
17 	 * are enqueued to the stack.
18 	 */
19 	rte_io_wmb();
20 	for (index = 0; index < n; index++)
21 		roc_npa_aura_op_free(mp->pool_id, 0,
22 				     (uint64_t)obj_table[index]);
23 
24 	return 0;
25 }
26 
27 int __rte_hot
cnxk_mempool_deq(struct rte_mempool * mp,void ** obj_table,unsigned int n)28 cnxk_mempool_deq(struct rte_mempool *mp, void **obj_table, unsigned int n)
29 {
30 	unsigned int index;
31 	uint64_t obj;
32 
33 	for (index = 0; index < n; index++, obj_table++) {
34 		int retry = 4;
35 
36 		/* Retry few times before failing */
37 		do {
38 			obj = roc_npa_aura_op_alloc(mp->pool_id, 0);
39 		} while (retry-- && (obj == 0));
40 
41 		if (obj == 0) {
42 			cnxk_mempool_enq(mp, obj_table - index, index);
43 			return -ENOENT;
44 		}
45 		*obj_table = (void *)obj;
46 	}
47 
48 	return 0;
49 }
50 
51 unsigned int
cnxk_mempool_get_count(const struct rte_mempool * mp)52 cnxk_mempool_get_count(const struct rte_mempool *mp)
53 {
54 	return (unsigned int)roc_npa_aura_op_available(mp->pool_id);
55 }
56 
57 ssize_t
cnxk_mempool_calc_mem_size(const struct rte_mempool * mp,uint32_t obj_num,uint32_t pg_shift,size_t * min_chunk_size,size_t * align)58 cnxk_mempool_calc_mem_size(const struct rte_mempool *mp, uint32_t obj_num,
59 			   uint32_t pg_shift, size_t *min_chunk_size,
60 			   size_t *align)
61 {
62 	size_t total_elt_sz;
63 
64 	/* Need space for one more obj on each chunk to fulfill
65 	 * alignment requirements.
66 	 */
67 	total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
68 	return rte_mempool_op_calc_mem_size_helper(
69 		mp, obj_num, pg_shift, total_elt_sz, min_chunk_size, align);
70 }
71 
72 int
cnxk_mempool_alloc(struct rte_mempool * mp)73 cnxk_mempool_alloc(struct rte_mempool *mp)
74 {
75 	uint64_t aura_handle = 0;
76 	struct npa_aura_s aura;
77 	struct npa_pool_s pool;
78 	uint32_t block_count;
79 	size_t block_size;
80 	int rc = -ERANGE;
81 
82 	block_size = mp->elt_size + mp->header_size + mp->trailer_size;
83 	block_count = mp->size;
84 	if (mp->header_size % ROC_ALIGN != 0) {
85 		plt_err("Header size should be multiple of %dB", ROC_ALIGN);
86 		goto error;
87 	}
88 
89 	if (block_size % ROC_ALIGN != 0) {
90 		plt_err("Block size should be multiple of %dB", ROC_ALIGN);
91 		goto error;
92 	}
93 
94 	memset(&aura, 0, sizeof(struct npa_aura_s));
95 	memset(&pool, 0, sizeof(struct npa_pool_s));
96 	pool.nat_align = 1;
97 	pool.buf_offset = mp->header_size / ROC_ALIGN;
98 
99 	/* Use driver specific mp->pool_config to override aura config */
100 	if (mp->pool_config != NULL)
101 		memcpy(&aura, mp->pool_config, sizeof(struct npa_aura_s));
102 
103 	rc = roc_npa_pool_create(&aura_handle, block_size, block_count, &aura,
104 				 &pool);
105 	if (rc) {
106 		plt_err("Failed to alloc pool or aura rc=%d", rc);
107 		goto error;
108 	}
109 
110 	/* Store aura_handle for future queue operations */
111 	mp->pool_id = aura_handle;
112 	plt_npa_dbg("block_sz=%lu block_count=%d aura_handle=0x%" PRIx64,
113 		    block_size, block_count, aura_handle);
114 
115 	return 0;
116 error:
117 	return rc;
118 }
119 
120 void
cnxk_mempool_free(struct rte_mempool * mp)121 cnxk_mempool_free(struct rte_mempool *mp)
122 {
123 	int rc = 0;
124 
125 	plt_npa_dbg("aura_handle=0x%" PRIx64, mp->pool_id);
126 	rc = roc_npa_pool_destroy(mp->pool_id);
127 	if (rc)
128 		plt_err("Failed to free pool or aura rc=%d", rc);
129 }
130 
131 int
cnxk_mempool_populate(struct rte_mempool * mp,unsigned int max_objs,void * vaddr,rte_iova_t iova,size_t len,rte_mempool_populate_obj_cb_t * obj_cb,void * obj_cb_arg)132 cnxk_mempool_populate(struct rte_mempool *mp, unsigned int max_objs,
133 		      void *vaddr, rte_iova_t iova, size_t len,
134 		      rte_mempool_populate_obj_cb_t *obj_cb, void *obj_cb_arg)
135 {
136 	size_t total_elt_sz, off;
137 	int num_elts;
138 
139 	if (iova == RTE_BAD_IOVA)
140 		return -EINVAL;
141 
142 	total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
143 
144 	/* Align object start address to a multiple of total_elt_sz */
145 	off = total_elt_sz - ((((uintptr_t)vaddr - 1) % total_elt_sz) + 1);
146 
147 	if (len < off)
148 		return -EINVAL;
149 
150 	vaddr = (char *)vaddr + off;
151 	iova += off;
152 	len -= off;
153 	num_elts = len / total_elt_sz;
154 
155 	plt_npa_dbg("iova %" PRIx64 ", aligned iova %" PRIx64 "", iova - off,
156 		    iova);
157 	plt_npa_dbg("length %" PRIu64 ", aligned length %" PRIu64 "",
158 		    (uint64_t)(len + off), (uint64_t)len);
159 	plt_npa_dbg("element size %" PRIu64 "", (uint64_t)total_elt_sz);
160 	plt_npa_dbg("requested objects %" PRIu64 ", possible objects %" PRIu64
161 		    "", (uint64_t)max_objs, (uint64_t)num_elts);
162 
163 	roc_npa_aura_op_range_set(mp->pool_id, iova,
164 				  iova + num_elts * total_elt_sz);
165 
166 	if (roc_npa_pool_range_update_check(mp->pool_id) < 0)
167 		return -EBUSY;
168 
169 	return rte_mempool_op_populate_helper(
170 		mp, RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ, max_objs, vaddr, iova,
171 		len, obj_cb, obj_cb_arg);
172 }
173 
174 static int
cnxk_mempool_plt_init(void)175 cnxk_mempool_plt_init(void)
176 {
177 	int rc = 0;
178 
179 	if (roc_model_is_cn9k()) {
180 		rte_mbuf_set_platform_mempool_ops("cn9k_mempool_ops");
181 	} else if (roc_model_is_cn10k()) {
182 		rte_mbuf_set_platform_mempool_ops("cn10k_mempool_ops");
183 		rc = cn10k_mempool_plt_init();
184 	}
185 	return rc;
186 }
187 
RTE_INIT(cnxk_mempool_ops_init)188 RTE_INIT(cnxk_mempool_ops_init)
189 {
190 	roc_plt_init_cb_register(cnxk_mempool_plt_init);
191 }
192