mempool/octeontx2: add fast path mempool ops
[dpdk.git] / drivers / mempool / octeontx2 / otx2_mempool_ops.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #include <rte_mempool.h>
6 #include <rte_vect.h>
7
8 #include "otx2_mempool.h"
9
10 static int __hot
11 otx2_npa_enq(struct rte_mempool *mp, void * const *obj_table, unsigned int n)
12 {
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) +
16                                  NPA_LF_AURA_OP_FREE0;
17
18         for (index = 0; index < n; index++)
19                 otx2_store_pair((uint64_t)obj_table[index], reg, addr);
20
21         return 0;
22 }
23
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)
27 {
28         uint8_t retry = 4;
29
30         do {
31                 obj_table[i] = (void *)otx2_atomic64_add_nosync(wdata, addr);
32                 if (obj_table[i] != NULL)
33                         return 0;
34
35         } while (retry--);
36
37         return -ENOENT;
38 }
39
40 static inline int __hot
41 otx2_npa_deq(struct rte_mempool *mp, void **obj_table, unsigned int n)
42 {
43         const int64_t wdata = npa_lf_aura_handle_to_aura(mp->pool_id);
44         unsigned int index;
45         uint64_t obj;
46
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);
52                 if (obj == 0) {
53                         for (; index > 0; index--) {
54                                 obj_table--;
55                                 otx2_npa_enq(mp, obj_table, 1);
56                         }
57                         return -ENOENT;
58                 }
59                 *obj_table = (void *)obj;
60         }
61
62         return 0;
63 }
64
65 static unsigned int
66 otx2_npa_get_count(const struct rte_mempool *mp)
67 {
68         return (unsigned int)npa_lf_aura_op_available(mp->pool_id);
69 }
70
71 static int
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)
74 {
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];
78         int rc, off;
79
80         aura_init_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
81
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));
86
87         pool_init_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
88
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));
93
94         otx2_mbox_msg_send(mbox, 0);
95         rc = otx2_mbox_wait_for_rsp(mbox, 0);
96         if (rc < 0)
97                 return rc;
98
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);
104
105         if (rc == 2 && aura_init_rsp->hdr.rc == 0 && pool_init_rsp->hdr.rc == 0)
106                 return 0;
107         else
108                 return NPA_LF_ERR_AURA_POOL_INIT;
109 }
110
111 static int
112 npa_lf_aura_pool_fini(struct otx2_mbox *mbox,
113                       uint32_t aura_id,
114                       uint64_t aura_handle)
115 {
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;
120         int rc, off;
121
122         /* Procedure for disabling an aura/pool */
123         rte_delay_us(10);
124         npa_lf_aura_op_alloc(aura_handle, 0);
125
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;
132
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;
139
140         otx2_mbox_msg_send(mbox, 0);
141         rc = otx2_mbox_wait_for_rsp(mbox, 0);
142         if (rc < 0)
143                 return rc;
144
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);
148
149         off = mbox->rx_start + pool_rsp->hdr.next_msgoff;
150         aura_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
151
152         if (rc != 2 || aura_rsp->hdr.rc != 0 || pool_rsp->hdr.rc != 0)
153                 return NPA_LF_ERR_AURA_POOL_FINI;
154
155         /* Sync NDC-NPA for LF */
156         ndc_req = otx2_mbox_alloc_msg_ndc_sync_op(mbox);
157         ndc_req->npa_lf_sync = 1;
158
159         rc = otx2_mbox_process(mbox);
160         if (rc) {
161                 otx2_err("Error on NDC-NPA LF sync, rc %d", rc);
162                 return NPA_LF_ERR_AURA_POOL_FINI;
163         }
164         return 0;
165 }
166
167 static inline char*
168 npa_lf_stack_memzone_name(struct otx2_npa_lf *lf, int pool_id, char *name)
169 {
170         snprintf(name, RTE_MEMZONE_NAMESIZE, "otx2_npa_stack_%x_%d",
171                         lf->pf_func, pool_id);
172
173         return name;
174 }
175
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)
179 {
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);
183 }
184
185 static inline int
186 npa_lf_stack_dma_free(struct otx2_npa_lf *lf, char *name, int pool_id)
187 {
188         const struct rte_memzone *mz;
189
190         mz = rte_memzone_lookup(npa_lf_stack_memzone_name(lf, pool_id, name));
191         if (mz == NULL)
192                 return -EINVAL;
193
194         return rte_memzone_free(mz);
195 }
196
197 static inline int
198 bitmap_ctzll(uint64_t slab)
199 {
200         if (slab == 0)
201                 return 0;
202
203         return __builtin_ctzll(slab);
204 }
205
206 static int
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)
210 {
211         int rc, aura_id, pool_id, stack_size, alloc_size;
212         char name[RTE_MEMZONE_NAMESIZE];
213         const struct rte_memzone *mz;
214         uint64_t slab;
215         uint32_t pos;
216
217         /* Sanity check */
218         if (!lf || !block_size || !block_count ||
219             !pool || !aura || !aura_handle)
220                 return NPA_LF_ERR_PARAM;
221
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;
226
227         pos = slab = 0;
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);
232         /* Empty bitmap */
233         if (rc == 0) {
234                 otx2_err("Mempools exhausted, 'max_pools' devargs to increase");
235                 return -ERANGE;
236         }
237
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);
242
243         /* Configuration based on each aura has separate pool(aura-pool pair) */
244         pool_id = aura_id;
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;
247         if (rc)
248                 goto exit;
249
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;
253
254         mz = npa_lf_stack_dma_alloc(lf, name, pool_id, alloc_size);
255         if (mz == NULL) {
256                 rc = -ENOMEM;
257                 goto aura_res_put;
258         }
259
260         /* Update aura fields */
261         aura->pool_addr = pool_id;/* AF will translate to associated poolctx */
262         aura->ena = 1;
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;
272
273         /* Update pool fields */
274         pool->stack_base = mz->iova;
275         pool->ena = 1;
276         pool->buf_size = block_size / OTX2_ALIGN;
277         pool->stack_max_pages = stack_size;
278         pool->shift = __builtin_clz(block_count) - 8;
279         pool->ptr_start = 0;
280         pool->ptr_end = ~0;
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);
285
286         /* Many to one reduction */
287         pool->err_qint_idx = pool_id % lf->qints;
288
289         /* Issue AURA_INIT and POOL_INIT op */
290         rc = npa_lf_aura_pool_init(lf->mbox, aura_id, aura, pool);
291         if (rc)
292                 goto stack_mem_free;
293
294         *aura_handle = npa_lf_aura_handle_gen(aura_id, lf->base);
295
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);
300
301         return 0;
302
303 stack_mem_free:
304         rte_memzone_free(mz);
305 aura_res_put:
306         rte_bitmap_set(lf->npa_bmp, aura_id);
307 exit:
308         return rc;
309 }
310
311 static int
312 npa_lf_aura_pool_pair_free(struct otx2_npa_lf *lf, uint64_t aura_handle)
313 {
314         char name[RTE_MEMZONE_NAMESIZE];
315         int aura_id, pool_id, rc;
316
317         if (!lf || !aura_handle)
318                 return NPA_LF_ERR_PARAM;
319
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);
323
324         rte_bitmap_set(lf->npa_bmp, aura_id);
325
326         return rc;
327 }
328
329 static int
330 otx2_npa_alloc(struct rte_mempool *mp)
331 {
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;
337         int rc;
338
339         lf = otx2_npa_lf_obj_get();
340         if (lf == NULL) {
341                 rc = -EINVAL;
342                 goto error;
343         }
344
345         block_size = mp->elt_size + mp->header_size + mp->trailer_size;
346         block_count = mp->size;
347
348         if (block_size % OTX2_ALIGN != 0) {
349                 otx2_err("Block size should be multiple of 128B");
350                 rc = -ERANGE;
351                 goto error;
352         }
353
354         memset(&aura, 0, sizeof(struct npa_aura_s));
355         memset(&pool, 0, sizeof(struct npa_pool_s));
356         pool.nat_align = 1;
357         pool.buf_offset = 1;
358
359         if ((uint32_t)pool.buf_offset * OTX2_ALIGN != mp->header_size) {
360                 otx2_err("Unsupported mp->header_size=%d", mp->header_size);
361                 rc = -EINVAL;
362                 goto error;
363         }
364
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));
368
369         rc = npa_lf_aura_pool_pair_alloc(lf, block_size, block_count,
370                          &aura, &pool, &aura_handle);
371         if (rc) {
372                 otx2_err("Failed to alloc pool or aura rc=%d", rc);
373                 goto error;
374         }
375
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);
380
381         /* Just hold the reference of the object */
382         otx2_npa_lf_obj_ref();
383         return 0;
384 error:
385         return rc;
386 }
387
388 static void
389 otx2_npa_free(struct rte_mempool *mp)
390 {
391         struct otx2_npa_lf *lf = otx2_npa_lf_obj_get();
392         int rc = 0;
393
394         otx2_npa_dbg("lf=%p aura_handle=0x%"PRIx64, lf, mp->pool_id);
395         if (lf != NULL)
396                 rc = npa_lf_aura_pool_pair_free(lf, mp->pool_id);
397
398         if (rc)
399                 otx2_err("Failed to free pool or aura rc=%d", rc);
400
401         /* Release the reference of npalf */
402         otx2_npa_lf_fini();
403 }
404
405 static ssize_t
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)
408 {
409         ssize_t mem_size;
410
411         /*
412          * Simply need space for one more object to be able to
413          * fulfill alignment requirements.
414          */
415         mem_size = rte_mempool_op_calc_mem_size_default(mp, obj_num + 1,
416                                                         pg_shift,
417                                                         min_chunk_size, align);
418         if (mem_size >= 0) {
419                 /*
420                  * Memory area which contains objects must be physically
421                  * contiguous.
422                  */
423                 *min_chunk_size = mem_size;
424         }
425
426         return mem_size;
427 }
428
429 static int
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)
433 {
434         size_t total_elt_sz;
435         size_t off;
436
437         if (iova == RTE_BAD_IOVA)
438                 return -EINVAL;
439
440         total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
441
442         /* Align object start address to a multiple of total_elt_sz */
443         off = total_elt_sz - ((uintptr_t)vaddr % total_elt_sz);
444
445         if (len < off)
446                 return -EINVAL;
447
448         vaddr = (char *)vaddr + off;
449         iova += off;
450         len -= off;
451
452         npa_lf_aura_op_range_set(mp->pool_id, iova, iova + len);
453
454         return rte_mempool_op_populate_default(mp, max_objs, vaddr, iova, len,
455                                                obj_cb, obj_cb_arg);
456 }
457
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,
467 };
468
469 MEMPOOL_REGISTER_OPS(otx2_npa_ops);