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