1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
3 */
4
5 #include <rte_mempool.h>
6
7 #include "roc_api.h"
8 #include "cnxk_mempool.h"
9
10 #define BATCH_ALLOC_SZ ROC_CN10K_NPA_BATCH_ALLOC_MAX_PTRS
11 #define BATCH_OP_DATA_TABLE_MZ_NAME "batch_op_data_table_mz"
12
13 enum batch_op_status {
14 BATCH_ALLOC_OP_NOT_ISSUED = 0,
15 BATCH_ALLOC_OP_ISSUED = 1,
16 BATCH_ALLOC_OP_DONE
17 };
18
19 struct batch_op_mem {
20 unsigned int sz;
21 enum batch_op_status status;
22 uint64_t objs[BATCH_ALLOC_SZ] __rte_aligned(ROC_ALIGN);
23 };
24
25 struct batch_op_data {
26 uint64_t lmt_addr;
27 struct batch_op_mem mem[RTE_MAX_LCORE] __rte_aligned(ROC_ALIGN);
28 };
29
30 static struct batch_op_data **batch_op_data_tbl;
31
32 static int
batch_op_data_table_create(void)33 batch_op_data_table_create(void)
34 {
35 const struct rte_memzone *mz;
36
37 /* If table is already set, nothing to do */
38 if (batch_op_data_tbl)
39 return 0;
40
41 mz = rte_memzone_lookup(BATCH_OP_DATA_TABLE_MZ_NAME);
42 if (mz == NULL) {
43 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
44 unsigned int maxpools, sz;
45
46 maxpools = roc_idev_npa_maxpools_get();
47 sz = maxpools * sizeof(struct batch_op_data *);
48
49 mz = rte_memzone_reserve_aligned(
50 BATCH_OP_DATA_TABLE_MZ_NAME, sz, SOCKET_ID_ANY,
51 0, ROC_ALIGN);
52 }
53 if (mz == NULL) {
54 plt_err("Failed to reserve batch op data table");
55 return -ENOMEM;
56 }
57 }
58 batch_op_data_tbl = mz->addr;
59 rte_wmb();
60 return 0;
61 }
62
63 static inline struct batch_op_data *
batch_op_data_get(uint64_t pool_id)64 batch_op_data_get(uint64_t pool_id)
65 {
66 uint64_t aura = roc_npa_aura_handle_to_aura(pool_id);
67
68 return batch_op_data_tbl[aura];
69 }
70
71 static inline void
batch_op_data_set(uint64_t pool_id,struct batch_op_data * op_data)72 batch_op_data_set(uint64_t pool_id, struct batch_op_data *op_data)
73 {
74 uint64_t aura = roc_npa_aura_handle_to_aura(pool_id);
75
76 batch_op_data_tbl[aura] = op_data;
77 }
78
79 static int
batch_op_init(struct rte_mempool * mp)80 batch_op_init(struct rte_mempool *mp)
81 {
82 struct batch_op_data *op_data;
83 int i;
84
85 op_data = batch_op_data_get(mp->pool_id);
86 /* The data should not have been allocated previously */
87 RTE_ASSERT(op_data == NULL);
88
89 op_data = rte_zmalloc(NULL, sizeof(struct batch_op_data), ROC_ALIGN);
90 if (op_data == NULL)
91 return -ENOMEM;
92
93 for (i = 0; i < RTE_MAX_LCORE; i++) {
94 op_data->mem[i].sz = 0;
95 op_data->mem[i].status = BATCH_ALLOC_OP_NOT_ISSUED;
96 }
97
98 op_data->lmt_addr = roc_idev_lmt_base_addr_get();
99 batch_op_data_set(mp->pool_id, op_data);
100 rte_wmb();
101
102 return 0;
103 }
104
105 static void
batch_op_fini(struct rte_mempool * mp)106 batch_op_fini(struct rte_mempool *mp)
107 {
108 struct batch_op_data *op_data;
109 int i;
110
111 op_data = batch_op_data_get(mp->pool_id);
112
113 rte_wmb();
114 for (i = 0; i < RTE_MAX_LCORE; i++) {
115 struct batch_op_mem *mem = &op_data->mem[i];
116
117 if (mem->status == BATCH_ALLOC_OP_ISSUED) {
118 mem->sz = roc_npa_aura_batch_alloc_extract(
119 mem->objs, mem->objs, BATCH_ALLOC_SZ);
120 mem->status = BATCH_ALLOC_OP_DONE;
121 }
122 if (mem->status == BATCH_ALLOC_OP_DONE) {
123 roc_npa_aura_op_bulk_free(mp->pool_id, mem->objs,
124 mem->sz, 1);
125 mem->status = BATCH_ALLOC_OP_NOT_ISSUED;
126 }
127 }
128
129 rte_free(op_data);
130 batch_op_data_set(mp->pool_id, NULL);
131 rte_wmb();
132 }
133
134 static int __rte_hot
cn10k_mempool_enq(struct rte_mempool * mp,void * const * obj_table,unsigned int n)135 cn10k_mempool_enq(struct rte_mempool *mp, void *const *obj_table,
136 unsigned int n)
137 {
138 const uint64_t *ptr = (const uint64_t *)obj_table;
139 uint64_t lmt_addr = 0, lmt_id = 0;
140 struct batch_op_data *op_data;
141
142 /* Ensure mbuf init changes are written before the free pointers are
143 * enqueued to the stack.
144 */
145 rte_io_wmb();
146
147 if (n == 1) {
148 roc_npa_aura_op_free(mp->pool_id, 1, ptr[0]);
149 return 0;
150 }
151
152 op_data = batch_op_data_get(mp->pool_id);
153 lmt_addr = op_data->lmt_addr;
154 ROC_LMT_BASE_ID_GET(lmt_addr, lmt_id);
155 roc_npa_aura_op_batch_free(mp->pool_id, ptr, n, 1, lmt_addr, lmt_id);
156
157 return 0;
158 }
159
160 static unsigned int
cn10k_mempool_get_count(const struct rte_mempool * mp)161 cn10k_mempool_get_count(const struct rte_mempool *mp)
162 {
163 struct batch_op_data *op_data;
164 unsigned int count = 0;
165 int i;
166
167 op_data = batch_op_data_get(mp->pool_id);
168
169 rte_wmb();
170 for (i = 0; i < RTE_MAX_LCORE; i++) {
171 struct batch_op_mem *mem = &op_data->mem[i];
172
173 if (mem->status == BATCH_ALLOC_OP_ISSUED)
174 count += roc_npa_aura_batch_alloc_count(mem->objs,
175 BATCH_ALLOC_SZ);
176
177 if (mem->status == BATCH_ALLOC_OP_DONE)
178 count += mem->sz;
179 }
180
181 count += cnxk_mempool_get_count(mp);
182
183 return count;
184 }
185
186 static int __rte_hot
cn10k_mempool_deq(struct rte_mempool * mp,void ** obj_table,unsigned int n)187 cn10k_mempool_deq(struct rte_mempool *mp, void **obj_table, unsigned int n)
188 {
189 struct batch_op_data *op_data;
190 struct batch_op_mem *mem;
191 unsigned int count = 0;
192 int tid, rc, retry;
193 bool loop = true;
194
195 op_data = batch_op_data_get(mp->pool_id);
196 tid = rte_lcore_id();
197 mem = &op_data->mem[tid];
198
199 /* Issue batch alloc */
200 if (mem->status == BATCH_ALLOC_OP_NOT_ISSUED) {
201 rc = roc_npa_aura_batch_alloc_issue(mp->pool_id, mem->objs,
202 BATCH_ALLOC_SZ, 0, 1);
203 /* If issue fails, try falling back to default alloc */
204 if (unlikely(rc))
205 return cnxk_mempool_deq(mp, obj_table, n);
206 mem->status = BATCH_ALLOC_OP_ISSUED;
207 }
208
209 retry = 4;
210 while (loop) {
211 unsigned int cur_sz;
212
213 if (mem->status == BATCH_ALLOC_OP_ISSUED) {
214 mem->sz = roc_npa_aura_batch_alloc_extract(
215 mem->objs, mem->objs, BATCH_ALLOC_SZ);
216
217 /* If partial alloc reduce the retry count */
218 retry -= (mem->sz != BATCH_ALLOC_SZ);
219 /* Break the loop if retry count exhausted */
220 loop = !!retry;
221 mem->status = BATCH_ALLOC_OP_DONE;
222 }
223
224 cur_sz = n - count;
225 if (cur_sz > mem->sz)
226 cur_sz = mem->sz;
227
228 /* Dequeue the pointers */
229 memcpy(&obj_table[count], &mem->objs[mem->sz - cur_sz],
230 cur_sz * sizeof(uintptr_t));
231 mem->sz -= cur_sz;
232 count += cur_sz;
233
234 /* Break loop if the required pointers has been dequeued */
235 loop &= (count != n);
236
237 /* Issue next batch alloc if pointers are exhausted */
238 if (mem->sz == 0) {
239 rc = roc_npa_aura_batch_alloc_issue(
240 mp->pool_id, mem->objs, BATCH_ALLOC_SZ, 0, 1);
241 /* Break loop if issue failed and set status */
242 loop &= !rc;
243 mem->status = !rc;
244 }
245 }
246
247 if (unlikely(count != n)) {
248 /* No partial alloc allowed. Free up allocated pointers */
249 cn10k_mempool_enq(mp, obj_table, count);
250 return -ENOENT;
251 }
252
253 return 0;
254 }
255
256 static int
cn10k_mempool_alloc(struct rte_mempool * mp)257 cn10k_mempool_alloc(struct rte_mempool *mp)
258 {
259 uint32_t block_size;
260 size_t padding;
261 int rc;
262
263 block_size = mp->elt_size + mp->header_size + mp->trailer_size;
264 /* Align header size to ROC_ALIGN */
265 if (mp->header_size % ROC_ALIGN != 0) {
266 padding = RTE_ALIGN_CEIL(mp->header_size, ROC_ALIGN) -
267 mp->header_size;
268 mp->header_size += padding;
269 block_size += padding;
270 }
271
272 /* Align block size to ROC_ALIGN */
273 if (block_size % ROC_ALIGN != 0) {
274 padding = RTE_ALIGN_CEIL(block_size, ROC_ALIGN) - block_size;
275 mp->trailer_size += padding;
276 block_size += padding;
277 }
278
279 rc = cnxk_mempool_alloc(mp);
280 if (rc)
281 return rc;
282
283 rc = batch_op_init(mp);
284 if (rc) {
285 plt_err("Failed to init batch alloc mem rc=%d", rc);
286 goto error;
287 }
288
289 return 0;
290 error:
291 cnxk_mempool_free(mp);
292 return rc;
293 }
294
295 static void
cn10k_mempool_free(struct rte_mempool * mp)296 cn10k_mempool_free(struct rte_mempool *mp)
297 {
298 batch_op_fini(mp);
299 cnxk_mempool_free(mp);
300 }
301
302 int
cn10k_mempool_plt_init(void)303 cn10k_mempool_plt_init(void)
304 {
305 return batch_op_data_table_create();
306 }
307
308 static struct rte_mempool_ops cn10k_mempool_ops = {
309 .name = "cn10k_mempool_ops",
310 .alloc = cn10k_mempool_alloc,
311 .free = cn10k_mempool_free,
312 .enqueue = cn10k_mempool_enq,
313 .dequeue = cn10k_mempool_deq,
314 .get_count = cn10k_mempool_get_count,
315 .calc_mem_size = cnxk_mempool_calc_mem_size,
316 .populate = cnxk_mempool_populate,
317 };
318
319 RTE_MEMPOOL_REGISTER_OPS(cn10k_mempool_ops);
320