mempool/octeontx2: add mempool alloc op
[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
11 npa_lf_aura_pool_init(struct otx2_mbox *mbox, uint32_t aura_id,
12                       struct npa_aura_s *aura, struct npa_pool_s *pool)
13 {
14         struct npa_aq_enq_req *aura_init_req, *pool_init_req;
15         struct npa_aq_enq_rsp *aura_init_rsp, *pool_init_rsp;
16         struct otx2_mbox_dev *mdev = &mbox->dev[0];
17         int rc, off;
18
19         aura_init_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
20
21         aura_init_req->aura_id = aura_id;
22         aura_init_req->ctype = NPA_AQ_CTYPE_AURA;
23         aura_init_req->op = NPA_AQ_INSTOP_INIT;
24         memcpy(&aura_init_req->aura, aura, sizeof(*aura));
25
26         pool_init_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
27
28         pool_init_req->aura_id = aura_id;
29         pool_init_req->ctype = NPA_AQ_CTYPE_POOL;
30         pool_init_req->op = NPA_AQ_INSTOP_INIT;
31         memcpy(&pool_init_req->pool, pool, sizeof(*pool));
32
33         otx2_mbox_msg_send(mbox, 0);
34         rc = otx2_mbox_wait_for_rsp(mbox, 0);
35         if (rc < 0)
36                 return rc;
37
38         off = mbox->rx_start +
39                         RTE_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
40         aura_init_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
41         off = mbox->rx_start + aura_init_rsp->hdr.next_msgoff;
42         pool_init_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
43
44         if (rc == 2 && aura_init_rsp->hdr.rc == 0 && pool_init_rsp->hdr.rc == 0)
45                 return 0;
46         else
47                 return NPA_LF_ERR_AURA_POOL_INIT;
48 }
49
50 static inline char*
51 npa_lf_stack_memzone_name(struct otx2_npa_lf *lf, int pool_id, char *name)
52 {
53         snprintf(name, RTE_MEMZONE_NAMESIZE, "otx2_npa_stack_%x_%d",
54                         lf->pf_func, pool_id);
55
56         return name;
57 }
58
59 static inline const struct rte_memzone *
60 npa_lf_stack_dma_alloc(struct otx2_npa_lf *lf, char *name,
61                        int pool_id, size_t size)
62 {
63         return rte_memzone_reserve_aligned(
64                 npa_lf_stack_memzone_name(lf, pool_id, name), size, 0,
65                         RTE_MEMZONE_IOVA_CONTIG, OTX2_ALIGN);
66 }
67
68 static inline int
69 bitmap_ctzll(uint64_t slab)
70 {
71         if (slab == 0)
72                 return 0;
73
74         return __builtin_ctzll(slab);
75 }
76
77 static int
78 npa_lf_aura_pool_pair_alloc(struct otx2_npa_lf *lf, const uint32_t block_size,
79                             const uint32_t block_count, struct npa_aura_s *aura,
80                             struct npa_pool_s *pool, uint64_t *aura_handle)
81 {
82         int rc, aura_id, pool_id, stack_size, alloc_size;
83         char name[RTE_MEMZONE_NAMESIZE];
84         const struct rte_memzone *mz;
85         uint64_t slab;
86         uint32_t pos;
87
88         /* Sanity check */
89         if (!lf || !block_size || !block_count ||
90             !pool || !aura || !aura_handle)
91                 return NPA_LF_ERR_PARAM;
92
93         /* Block size should be cache line aligned and in range of 128B-128KB */
94         if (block_size % OTX2_ALIGN || block_size < 128 ||
95             block_size > 128 * 1024)
96                 return NPA_LF_ERR_INVALID_BLOCK_SZ;
97
98         pos = slab = 0;
99         /* Scan from the beginning */
100         __rte_bitmap_scan_init(lf->npa_bmp);
101         /* Scan bitmap to get the free pool */
102         rc = rte_bitmap_scan(lf->npa_bmp, &pos, &slab);
103         /* Empty bitmap */
104         if (rc == 0) {
105                 otx2_err("Mempools exhausted, 'max_pools' devargs to increase");
106                 return -ERANGE;
107         }
108
109         /* Get aura_id from resource bitmap */
110         aura_id = pos + bitmap_ctzll(slab);
111         /* Mark pool as reserved */
112         rte_bitmap_clear(lf->npa_bmp, aura_id);
113
114         /* Configuration based on each aura has separate pool(aura-pool pair) */
115         pool_id = aura_id;
116         rc = (aura_id < 0 || pool_id >= (int)lf->nr_pools || aura_id >=
117               (int)BIT_ULL(6 + lf->aura_sz)) ? NPA_LF_ERR_AURA_ID_ALLOC : 0;
118         if (rc)
119                 goto exit;
120
121         /* Allocate stack memory */
122         stack_size = (block_count + lf->stack_pg_ptrs - 1) / lf->stack_pg_ptrs;
123         alloc_size = stack_size * lf->stack_pg_bytes;
124
125         mz = npa_lf_stack_dma_alloc(lf, name, pool_id, alloc_size);
126         if (mz == NULL) {
127                 rc = -ENOMEM;
128                 goto aura_res_put;
129         }
130
131         /* Update aura fields */
132         aura->pool_addr = pool_id;/* AF will translate to associated poolctx */
133         aura->ena = 1;
134         aura->shift = __builtin_clz(block_count) - 8;
135         aura->limit = block_count;
136         aura->pool_caching = 1;
137         aura->err_int_ena = BIT(NPA_AURA_ERR_INT_AURA_ADD_OVER);
138         aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_ADD_UNDER);
139         aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_FREE_UNDER);
140         aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_POOL_DIS);
141         /* Many to one reduction */
142         aura->err_qint_idx = aura_id % lf->qints;
143
144         /* Update pool fields */
145         pool->stack_base = mz->iova;
146         pool->ena = 1;
147         pool->buf_size = block_size / OTX2_ALIGN;
148         pool->stack_max_pages = stack_size;
149         pool->shift = __builtin_clz(block_count) - 8;
150         pool->ptr_start = 0;
151         pool->ptr_end = ~0;
152         pool->stack_caching = 1;
153         pool->err_int_ena = BIT(NPA_POOL_ERR_INT_OVFLS);
154         pool->err_int_ena |= BIT(NPA_POOL_ERR_INT_RANGE);
155         pool->err_int_ena |= BIT(NPA_POOL_ERR_INT_PERR);
156
157         /* Many to one reduction */
158         pool->err_qint_idx = pool_id % lf->qints;
159
160         /* Issue AURA_INIT and POOL_INIT op */
161         rc = npa_lf_aura_pool_init(lf->mbox, aura_id, aura, pool);
162         if (rc)
163                 goto stack_mem_free;
164
165         *aura_handle = npa_lf_aura_handle_gen(aura_id, lf->base);
166
167         /* Update aura count */
168         npa_lf_aura_op_cnt_set(*aura_handle, 0, block_count);
169         /* Read it back to make sure aura count is updated */
170         npa_lf_aura_op_cnt_get(*aura_handle);
171
172         return 0;
173
174 stack_mem_free:
175         rte_memzone_free(mz);
176 aura_res_put:
177         rte_bitmap_set(lf->npa_bmp, aura_id);
178 exit:
179         return rc;
180 }
181
182 static int
183 otx2_npa_alloc(struct rte_mempool *mp)
184 {
185         uint32_t block_size, block_count;
186         struct otx2_npa_lf *lf;
187         struct npa_aura_s aura;
188         struct npa_pool_s pool;
189         uint64_t aura_handle;
190         int rc;
191
192         lf = otx2_npa_lf_obj_get();
193         if (lf == NULL) {
194                 rc = -EINVAL;
195                 goto error;
196         }
197
198         block_size = mp->elt_size + mp->header_size + mp->trailer_size;
199         block_count = mp->size;
200
201         if (block_size % OTX2_ALIGN != 0) {
202                 otx2_err("Block size should be multiple of 128B");
203                 rc = -ERANGE;
204                 goto error;
205         }
206
207         memset(&aura, 0, sizeof(struct npa_aura_s));
208         memset(&pool, 0, sizeof(struct npa_pool_s));
209         pool.nat_align = 1;
210         pool.buf_offset = 1;
211
212         if ((uint32_t)pool.buf_offset * OTX2_ALIGN != mp->header_size) {
213                 otx2_err("Unsupported mp->header_size=%d", mp->header_size);
214                 rc = -EINVAL;
215                 goto error;
216         }
217
218         /* Use driver specific mp->pool_config to override aura config */
219         if (mp->pool_config != NULL)
220                 memcpy(&aura, mp->pool_config, sizeof(struct npa_aura_s));
221
222         rc = npa_lf_aura_pool_pair_alloc(lf, block_size, block_count,
223                          &aura, &pool, &aura_handle);
224         if (rc) {
225                 otx2_err("Failed to alloc pool or aura rc=%d", rc);
226                 goto error;
227         }
228
229         /* Store aura_handle for future queue operations */
230         mp->pool_id = aura_handle;
231         otx2_npa_dbg("lf=%p block_sz=%d block_count=%d aura_handle=0x%"PRIx64,
232                      lf, block_size, block_count, aura_handle);
233
234         /* Just hold the reference of the object */
235         otx2_npa_lf_obj_ref();
236         return 0;
237 error:
238         return rc;
239 }
240
241 static struct rte_mempool_ops otx2_npa_ops = {
242         .name = "octeontx2_npa",
243         .alloc = otx2_npa_alloc,
244 };
245
246 MEMPOOL_REGISTER_OPS(otx2_npa_ops);