mempool/octeontx2: add devargs to lock context in cache
[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 __rte_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 #if defined(RTE_ARCH_ARM64)
41 static __rte_noinline int
42 npa_lf_aura_op_search_alloc(const int64_t wdata, int64_t * const addr,
43                 void **obj_table, unsigned int n)
44 {
45         uint8_t i;
46
47         for (i = 0; i < n; i++) {
48                 if (obj_table[i] != NULL)
49                         continue;
50                 if (npa_lf_aura_op_alloc_one(wdata, addr, obj_table, i))
51                         return -ENOENT;
52         }
53
54         return 0;
55 }
56
57 static __rte_noinline int
58 npa_lf_aura_op_alloc_bulk(const int64_t wdata, int64_t * const addr,
59                           unsigned int n, void **obj_table)
60 {
61         register const uint64_t wdata64 __asm("x26") = wdata;
62         register const uint64_t wdata128 __asm("x27") = wdata;
63         uint64x2_t failed = vdupq_n_u64(~0);
64
65         switch (n) {
66         case 32:
67         {
68                 asm volatile (
69                 ".cpu  generic+lse\n"
70                 "casp x0, x1, %[wdata64], %[wdata128], [%[loc]]\n"
71                 "casp x2, x3, %[wdata64], %[wdata128], [%[loc]]\n"
72                 "casp x4, x5, %[wdata64], %[wdata128], [%[loc]]\n"
73                 "casp x6, x7, %[wdata64], %[wdata128], [%[loc]]\n"
74                 "casp x8, x9, %[wdata64], %[wdata128], [%[loc]]\n"
75                 "casp x10, x11, %[wdata64], %[wdata128], [%[loc]]\n"
76                 "casp x12, x13, %[wdata64], %[wdata128], [%[loc]]\n"
77                 "casp x14, x15, %[wdata64], %[wdata128], [%[loc]]\n"
78                 "casp x16, x17, %[wdata64], %[wdata128], [%[loc]]\n"
79                 "casp x18, x19, %[wdata64], %[wdata128], [%[loc]]\n"
80                 "casp x20, x21, %[wdata64], %[wdata128], [%[loc]]\n"
81                 "casp x22, x23, %[wdata64], %[wdata128], [%[loc]]\n"
82                 "fmov d16, x0\n"
83                 "fmov v16.D[1], x1\n"
84                 "casp x0, x1, %[wdata64], %[wdata128], [%[loc]]\n"
85                 "fmov d17, x2\n"
86                 "fmov v17.D[1], x3\n"
87                 "casp x2, x3, %[wdata64], %[wdata128], [%[loc]]\n"
88                 "fmov d18, x4\n"
89                 "fmov v18.D[1], x5\n"
90                 "casp x4, x5, %[wdata64], %[wdata128], [%[loc]]\n"
91                 "fmov d19, x6\n"
92                 "fmov v19.D[1], x7\n"
93                 "casp x6, x7, %[wdata64], %[wdata128], [%[loc]]\n"
94                 "and %[failed].16B, %[failed].16B, v16.16B\n"
95                 "and %[failed].16B, %[failed].16B, v17.16B\n"
96                 "and %[failed].16B, %[failed].16B, v18.16B\n"
97                 "and %[failed].16B, %[failed].16B, v19.16B\n"
98                 "fmov d20, x8\n"
99                 "fmov v20.D[1], x9\n"
100                 "fmov d21, x10\n"
101                 "fmov v21.D[1], x11\n"
102                 "fmov d22, x12\n"
103                 "fmov v22.D[1], x13\n"
104                 "fmov d23, x14\n"
105                 "fmov v23.D[1], x15\n"
106                 "and %[failed].16B, %[failed].16B, v20.16B\n"
107                 "and %[failed].16B, %[failed].16B, v21.16B\n"
108                 "and %[failed].16B, %[failed].16B, v22.16B\n"
109                 "and %[failed].16B, %[failed].16B, v23.16B\n"
110                 "st1 { v16.2d, v17.2d, v18.2d, v19.2d}, [%[dst]], 64\n"
111                 "st1 { v20.2d, v21.2d, v22.2d, v23.2d}, [%[dst]], 64\n"
112                 "fmov d16, x16\n"
113                 "fmov v16.D[1], x17\n"
114                 "fmov d17, x18\n"
115                 "fmov v17.D[1], x19\n"
116                 "fmov d18, x20\n"
117                 "fmov v18.D[1], x21\n"
118                 "fmov d19, x22\n"
119                 "fmov v19.D[1], x23\n"
120                 "and %[failed].16B, %[failed].16B, v16.16B\n"
121                 "and %[failed].16B, %[failed].16B, v17.16B\n"
122                 "and %[failed].16B, %[failed].16B, v18.16B\n"
123                 "and %[failed].16B, %[failed].16B, v19.16B\n"
124                 "fmov d20, x0\n"
125                 "fmov v20.D[1], x1\n"
126                 "fmov d21, x2\n"
127                 "fmov v21.D[1], x3\n"
128                 "fmov d22, x4\n"
129                 "fmov v22.D[1], x5\n"
130                 "fmov d23, x6\n"
131                 "fmov v23.D[1], x7\n"
132                 "and %[failed].16B, %[failed].16B, v20.16B\n"
133                 "and %[failed].16B, %[failed].16B, v21.16B\n"
134                 "and %[failed].16B, %[failed].16B, v22.16B\n"
135                 "and %[failed].16B, %[failed].16B, v23.16B\n"
136                 "st1 { v16.2d, v17.2d, v18.2d, v19.2d}, [%[dst]], 64\n"
137                 "st1 { v20.2d, v21.2d, v22.2d, v23.2d}, [%[dst]], 64\n"
138                 : "+Q" (*addr), [failed] "=&w" (failed)
139                 : [wdata64] "r" (wdata64), [wdata128] "r" (wdata128),
140                 [dst] "r" (obj_table), [loc] "r" (addr)
141                 : "memory", "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
142                 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16",
143                 "x17", "x18", "x19", "x20", "x21", "x22", "x23", "v16", "v17",
144                 "v18", "v19", "v20", "v21", "v22", "v23"
145                 );
146                 break;
147         }
148         case 16:
149         {
150                 asm volatile (
151                 ".cpu  generic+lse\n"
152                 "casp x0, x1, %[wdata64], %[wdata128], [%[loc]]\n"
153                 "casp x2, x3, %[wdata64], %[wdata128], [%[loc]]\n"
154                 "casp x4, x5, %[wdata64], %[wdata128], [%[loc]]\n"
155                 "casp x6, x7, %[wdata64], %[wdata128], [%[loc]]\n"
156                 "casp x8, x9, %[wdata64], %[wdata128], [%[loc]]\n"
157                 "casp x10, x11, %[wdata64], %[wdata128], [%[loc]]\n"
158                 "casp x12, x13, %[wdata64], %[wdata128], [%[loc]]\n"
159                 "casp x14, x15, %[wdata64], %[wdata128], [%[loc]]\n"
160                 "fmov d16, x0\n"
161                 "fmov v16.D[1], x1\n"
162                 "fmov d17, x2\n"
163                 "fmov v17.D[1], x3\n"
164                 "fmov d18, x4\n"
165                 "fmov v18.D[1], x5\n"
166                 "fmov d19, x6\n"
167                 "fmov v19.D[1], x7\n"
168                 "and %[failed].16B, %[failed].16B, v16.16B\n"
169                 "and %[failed].16B, %[failed].16B, v17.16B\n"
170                 "and %[failed].16B, %[failed].16B, v18.16B\n"
171                 "and %[failed].16B, %[failed].16B, v19.16B\n"
172                 "fmov d20, x8\n"
173                 "fmov v20.D[1], x9\n"
174                 "fmov d21, x10\n"
175                 "fmov v21.D[1], x11\n"
176                 "fmov d22, x12\n"
177                 "fmov v22.D[1], x13\n"
178                 "fmov d23, x14\n"
179                 "fmov v23.D[1], x15\n"
180                 "and %[failed].16B, %[failed].16B, v20.16B\n"
181                 "and %[failed].16B, %[failed].16B, v21.16B\n"
182                 "and %[failed].16B, %[failed].16B, v22.16B\n"
183                 "and %[failed].16B, %[failed].16B, v23.16B\n"
184                 "st1 { v16.2d, v17.2d, v18.2d, v19.2d}, [%[dst]], 64\n"
185                 "st1 { v20.2d, v21.2d, v22.2d, v23.2d}, [%[dst]], 64\n"
186                 : "+Q" (*addr), [failed] "=&w" (failed)
187                 : [wdata64] "r" (wdata64), [wdata128] "r" (wdata128),
188                 [dst] "r" (obj_table), [loc] "r" (addr)
189                 : "memory", "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
190                 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "v16",
191                 "v17", "v18", "v19", "v20", "v21", "v22", "v23"
192                 );
193                 break;
194         }
195         case 8:
196         {
197                 asm volatile (
198                 ".cpu  generic+lse\n"
199                 "casp x0, x1, %[wdata64], %[wdata128], [%[loc]]\n"
200                 "casp x2, x3, %[wdata64], %[wdata128], [%[loc]]\n"
201                 "casp x4, x5, %[wdata64], %[wdata128], [%[loc]]\n"
202                 "casp x6, x7, %[wdata64], %[wdata128], [%[loc]]\n"
203                 "fmov d16, x0\n"
204                 "fmov v16.D[1], x1\n"
205                 "fmov d17, x2\n"
206                 "fmov v17.D[1], x3\n"
207                 "fmov d18, x4\n"
208                 "fmov v18.D[1], x5\n"
209                 "fmov d19, x6\n"
210                 "fmov v19.D[1], x7\n"
211                 "and %[failed].16B, %[failed].16B, v16.16B\n"
212                 "and %[failed].16B, %[failed].16B, v17.16B\n"
213                 "and %[failed].16B, %[failed].16B, v18.16B\n"
214                 "and %[failed].16B, %[failed].16B, v19.16B\n"
215                 "st1 { v16.2d, v17.2d, v18.2d, v19.2d}, [%[dst]], 64\n"
216                 : "+Q" (*addr), [failed] "=&w" (failed)
217                 : [wdata64] "r" (wdata64), [wdata128] "r" (wdata128),
218                 [dst] "r" (obj_table), [loc] "r" (addr)
219                 : "memory", "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
220                 "v16", "v17", "v18", "v19"
221                 );
222                 break;
223         }
224         case 4:
225         {
226                 asm volatile (
227                 ".cpu  generic+lse\n"
228                 "casp x0, x1, %[wdata64], %[wdata128], [%[loc]]\n"
229                 "casp x2, x3, %[wdata64], %[wdata128], [%[loc]]\n"
230                 "fmov d16, x0\n"
231                 "fmov v16.D[1], x1\n"
232                 "fmov d17, x2\n"
233                 "fmov v17.D[1], x3\n"
234                 "and %[failed].16B, %[failed].16B, v16.16B\n"
235                 "and %[failed].16B, %[failed].16B, v17.16B\n"
236                 "st1 { v16.2d, v17.2d}, [%[dst]], 32\n"
237                 : "+Q" (*addr), [failed] "=&w" (failed)
238                 : [wdata64] "r" (wdata64), [wdata128] "r" (wdata128),
239                 [dst] "r" (obj_table), [loc] "r" (addr)
240                 : "memory", "x0", "x1", "x2", "x3", "v16", "v17"
241                 );
242                 break;
243         }
244         case 2:
245         {
246                 asm volatile (
247                 ".cpu  generic+lse\n"
248                 "casp x0, x1, %[wdata64], %[wdata128], [%[loc]]\n"
249                 "fmov d16, x0\n"
250                 "fmov v16.D[1], x1\n"
251                 "and %[failed].16B, %[failed].16B, v16.16B\n"
252                 "st1 { v16.2d}, [%[dst]], 16\n"
253                 : "+Q" (*addr), [failed] "=&w" (failed)
254                 : [wdata64] "r" (wdata64), [wdata128] "r" (wdata128),
255                 [dst] "r" (obj_table), [loc] "r" (addr)
256                 : "memory", "x0", "x1", "v16"
257                 );
258                 break;
259         }
260         case 1:
261                 return npa_lf_aura_op_alloc_one(wdata, addr, obj_table, 0);
262         }
263
264         if (unlikely(!(vgetq_lane_u64(failed, 0) & vgetq_lane_u64(failed, 1))))
265                 return npa_lf_aura_op_search_alloc(wdata, addr, (void **)
266                         ((char *)obj_table - (sizeof(uint64_t) * n)), n);
267
268         return 0;
269 }
270
271 static __rte_noinline void
272 otx2_npa_clear_alloc(struct rte_mempool *mp, void **obj_table, unsigned int n)
273 {
274         unsigned int i;
275
276         for (i = 0; i < n; i++) {
277                 if (obj_table[i] != NULL) {
278                         otx2_npa_enq(mp, &obj_table[i], 1);
279                         obj_table[i] = NULL;
280                 }
281         }
282 }
283
284 static __rte_noinline int __rte_hot
285 otx2_npa_deq_arm64(struct rte_mempool *mp, void **obj_table, unsigned int n)
286 {
287         const int64_t wdata = npa_lf_aura_handle_to_aura(mp->pool_id);
288         void **obj_table_bak = obj_table;
289         const unsigned int nfree = n;
290         unsigned int parts;
291
292         int64_t * const addr = (int64_t * const)
293                         (npa_lf_aura_handle_to_base(mp->pool_id) +
294                                 NPA_LF_AURA_OP_ALLOCX(0));
295         while (n) {
296                 parts = n > 31 ? 32 : rte_align32prevpow2(n);
297                 n -= parts;
298                 if (unlikely(npa_lf_aura_op_alloc_bulk(wdata, addr,
299                                 parts, obj_table))) {
300                         otx2_npa_clear_alloc(mp, obj_table_bak, nfree - n);
301                         return -ENOENT;
302                 }
303                 obj_table += parts;
304         }
305
306         return 0;
307 }
308
309 #else
310
311 static inline int __rte_hot
312 otx2_npa_deq(struct rte_mempool *mp, void **obj_table, unsigned int n)
313 {
314         const int64_t wdata = npa_lf_aura_handle_to_aura(mp->pool_id);
315         unsigned int index;
316         uint64_t obj;
317
318         int64_t * const addr = (int64_t *)
319                         (npa_lf_aura_handle_to_base(mp->pool_id) +
320                                 NPA_LF_AURA_OP_ALLOCX(0));
321         for (index = 0; index < n; index++, obj_table++) {
322                 obj = npa_lf_aura_op_alloc_one(wdata, addr, obj_table, 0);
323                 if (obj == 0) {
324                         for (; index > 0; index--) {
325                                 obj_table--;
326                                 otx2_npa_enq(mp, obj_table, 1);
327                         }
328                         return -ENOENT;
329                 }
330                 *obj_table = (void *)obj;
331         }
332
333         return 0;
334 }
335
336 #endif
337
338 static unsigned int
339 otx2_npa_get_count(const struct rte_mempool *mp)
340 {
341         return (unsigned int)npa_lf_aura_op_available(mp->pool_id);
342 }
343
344 static int
345 npa_lf_aura_pool_init(struct otx2_mbox *mbox, uint32_t aura_id,
346                       struct npa_aura_s *aura, struct npa_pool_s *pool)
347 {
348         struct npa_aq_enq_req *aura_init_req, *pool_init_req;
349         struct npa_aq_enq_rsp *aura_init_rsp, *pool_init_rsp;
350         struct otx2_mbox_dev *mdev = &mbox->dev[0];
351         struct otx2_idev_cfg *idev;
352         int rc, off;
353
354         idev = otx2_intra_dev_get_cfg();
355         if (idev == NULL)
356                 return -ENOMEM;
357
358         aura_init_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
359
360         aura_init_req->aura_id = aura_id;
361         aura_init_req->ctype = NPA_AQ_CTYPE_AURA;
362         aura_init_req->op = NPA_AQ_INSTOP_INIT;
363         otx2_mbox_memcpy(&aura_init_req->aura, aura, sizeof(*aura));
364
365         pool_init_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
366
367         pool_init_req->aura_id = aura_id;
368         pool_init_req->ctype = NPA_AQ_CTYPE_POOL;
369         pool_init_req->op = NPA_AQ_INSTOP_INIT;
370         otx2_mbox_memcpy(&pool_init_req->pool, pool, sizeof(*pool));
371
372         otx2_mbox_msg_send(mbox, 0);
373         rc = otx2_mbox_wait_for_rsp(mbox, 0);
374         if (rc < 0)
375                 return rc;
376
377         off = mbox->rx_start +
378                         RTE_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
379         aura_init_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
380         off = mbox->rx_start + aura_init_rsp->hdr.next_msgoff;
381         pool_init_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
382
383         if (rc == 2 && aura_init_rsp->hdr.rc == 0 && pool_init_rsp->hdr.rc == 0)
384                 return 0;
385         else
386                 return NPA_LF_ERR_AURA_POOL_INIT;
387
388         if (!(idev->npa_lock_mask & BIT_ULL(aura_id)))
389                 return 0;
390
391         aura_init_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
392         aura_init_req->aura_id = aura_id;
393         aura_init_req->ctype = NPA_AQ_CTYPE_AURA;
394         aura_init_req->op = NPA_AQ_INSTOP_LOCK;
395
396         pool_init_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
397         if (!pool_init_req) {
398                 /* The shared memory buffer can be full.
399                  * Flush it and retry
400                  */
401                 otx2_mbox_msg_send(mbox, 0);
402                 rc = otx2_mbox_wait_for_rsp(mbox, 0);
403                 if (rc < 0) {
404                         otx2_err("Failed to LOCK AURA context");
405                         return -ENOMEM;
406                 }
407
408                 pool_init_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
409                 if (!pool_init_req) {
410                         otx2_err("Failed to LOCK POOL context");
411                         return -ENOMEM;
412                 }
413         }
414         pool_init_req->aura_id = aura_id;
415         pool_init_req->ctype = NPA_AQ_CTYPE_POOL;
416         pool_init_req->op = NPA_AQ_INSTOP_LOCK;
417
418         rc = otx2_mbox_process(mbox);
419         if (rc < 0) {
420                 otx2_err("Failed to lock POOL ctx to NDC");
421                 return -ENOMEM;
422         }
423
424         return 0;
425 }
426
427 static int
428 npa_lf_aura_pool_fini(struct otx2_mbox *mbox,
429                       uint32_t aura_id,
430                       uint64_t aura_handle)
431 {
432         struct npa_aq_enq_req *aura_req, *pool_req;
433         struct npa_aq_enq_rsp *aura_rsp, *pool_rsp;
434         struct otx2_mbox_dev *mdev = &mbox->dev[0];
435         struct ndc_sync_op *ndc_req;
436         struct otx2_idev_cfg *idev;
437         int rc, off;
438
439         idev = otx2_intra_dev_get_cfg();
440         if (idev == NULL)
441                 return -EINVAL;
442
443         /* Procedure for disabling an aura/pool */
444         rte_delay_us(10);
445         npa_lf_aura_op_alloc(aura_handle, 0);
446
447         pool_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
448         pool_req->aura_id = aura_id;
449         pool_req->ctype = NPA_AQ_CTYPE_POOL;
450         pool_req->op = NPA_AQ_INSTOP_WRITE;
451         pool_req->pool.ena = 0;
452         pool_req->pool_mask.ena = ~pool_req->pool_mask.ena;
453
454         aura_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
455         aura_req->aura_id = aura_id;
456         aura_req->ctype = NPA_AQ_CTYPE_AURA;
457         aura_req->op = NPA_AQ_INSTOP_WRITE;
458         aura_req->aura.ena = 0;
459         aura_req->aura_mask.ena = ~aura_req->aura_mask.ena;
460
461         otx2_mbox_msg_send(mbox, 0);
462         rc = otx2_mbox_wait_for_rsp(mbox, 0);
463         if (rc < 0)
464                 return rc;
465
466         off = mbox->rx_start +
467                         RTE_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
468         pool_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
469
470         off = mbox->rx_start + pool_rsp->hdr.next_msgoff;
471         aura_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
472
473         if (rc != 2 || aura_rsp->hdr.rc != 0 || pool_rsp->hdr.rc != 0)
474                 return NPA_LF_ERR_AURA_POOL_FINI;
475
476         /* Sync NDC-NPA for LF */
477         ndc_req = otx2_mbox_alloc_msg_ndc_sync_op(mbox);
478         ndc_req->npa_lf_sync = 1;
479
480         rc = otx2_mbox_process(mbox);
481         if (rc) {
482                 otx2_err("Error on NDC-NPA LF sync, rc %d", rc);
483                 return NPA_LF_ERR_AURA_POOL_FINI;
484         }
485
486         if (!(idev->npa_lock_mask & BIT_ULL(aura_id)))
487                 return 0;
488
489         aura_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
490         aura_req->aura_id = aura_id;
491         aura_req->ctype = NPA_AQ_CTYPE_AURA;
492         aura_req->op = NPA_AQ_INSTOP_UNLOCK;
493
494         rc = otx2_mbox_process(mbox);
495         if (rc < 0) {
496                 otx2_err("Failed to unlock AURA ctx to NDC");
497                 return -EINVAL;
498         }
499
500         pool_req = otx2_mbox_alloc_msg_npa_aq_enq(mbox);
501         pool_req->aura_id = aura_id;
502         pool_req->ctype = NPA_AQ_CTYPE_POOL;
503         pool_req->op = NPA_AQ_INSTOP_UNLOCK;
504
505         rc = otx2_mbox_process(mbox);
506         if (rc < 0) {
507                 otx2_err("Failed to unlock POOL ctx to NDC");
508                 return -EINVAL;
509         }
510
511         return 0;
512 }
513
514 static inline char*
515 npa_lf_stack_memzone_name(struct otx2_npa_lf *lf, int pool_id, char *name)
516 {
517         snprintf(name, RTE_MEMZONE_NAMESIZE, "otx2_npa_stack_%x_%d",
518                         lf->pf_func, pool_id);
519
520         return name;
521 }
522
523 static inline const struct rte_memzone *
524 npa_lf_stack_dma_alloc(struct otx2_npa_lf *lf, char *name,
525                        int pool_id, size_t size)
526 {
527         return rte_memzone_reserve_aligned(
528                 npa_lf_stack_memzone_name(lf, pool_id, name), size, 0,
529                         RTE_MEMZONE_IOVA_CONTIG, OTX2_ALIGN);
530 }
531
532 static inline int
533 npa_lf_stack_dma_free(struct otx2_npa_lf *lf, char *name, int pool_id)
534 {
535         const struct rte_memzone *mz;
536
537         mz = rte_memzone_lookup(npa_lf_stack_memzone_name(lf, pool_id, name));
538         if (mz == NULL)
539                 return -EINVAL;
540
541         return rte_memzone_free(mz);
542 }
543
544 static inline int
545 bitmap_ctzll(uint64_t slab)
546 {
547         if (slab == 0)
548                 return 0;
549
550         return __builtin_ctzll(slab);
551 }
552
553 static int
554 npa_lf_aura_pool_pair_alloc(struct otx2_npa_lf *lf, const uint32_t block_size,
555                             const uint32_t block_count, struct npa_aura_s *aura,
556                             struct npa_pool_s *pool, uint64_t *aura_handle)
557 {
558         int rc, aura_id, pool_id, stack_size, alloc_size;
559         char name[RTE_MEMZONE_NAMESIZE];
560         const struct rte_memzone *mz;
561         uint64_t slab;
562         uint32_t pos;
563
564         /* Sanity check */
565         if (!lf || !block_size || !block_count ||
566             !pool || !aura || !aura_handle)
567                 return NPA_LF_ERR_PARAM;
568
569         /* Block size should be cache line aligned and in range of 128B-128KB */
570         if (block_size % OTX2_ALIGN || block_size < 128 ||
571             block_size > 128 * 1024)
572                 return NPA_LF_ERR_INVALID_BLOCK_SZ;
573
574         pos = slab = 0;
575         /* Scan from the beginning */
576         __rte_bitmap_scan_init(lf->npa_bmp);
577         /* Scan bitmap to get the free pool */
578         rc = rte_bitmap_scan(lf->npa_bmp, &pos, &slab);
579         /* Empty bitmap */
580         if (rc == 0) {
581                 otx2_err("Mempools exhausted, 'max_pools' devargs to increase");
582                 return -ERANGE;
583         }
584
585         /* Get aura_id from resource bitmap */
586         aura_id = pos + bitmap_ctzll(slab);
587         /* Mark pool as reserved */
588         rte_bitmap_clear(lf->npa_bmp, aura_id);
589
590         /* Configuration based on each aura has separate pool(aura-pool pair) */
591         pool_id = aura_id;
592         rc = (aura_id < 0 || pool_id >= (int)lf->nr_pools || aura_id >=
593               (int)BIT_ULL(6 + lf->aura_sz)) ? NPA_LF_ERR_AURA_ID_ALLOC : 0;
594         if (rc)
595                 goto exit;
596
597         /* Allocate stack memory */
598         stack_size = (block_count + lf->stack_pg_ptrs - 1) / lf->stack_pg_ptrs;
599         alloc_size = stack_size * lf->stack_pg_bytes;
600
601         mz = npa_lf_stack_dma_alloc(lf, name, pool_id, alloc_size);
602         if (mz == NULL) {
603                 rc = -ENOMEM;
604                 goto aura_res_put;
605         }
606
607         /* Update aura fields */
608         aura->pool_addr = pool_id;/* AF will translate to associated poolctx */
609         aura->ena = 1;
610         aura->shift = __builtin_clz(block_count) - 8;
611         aura->limit = block_count;
612         aura->pool_caching = 1;
613         aura->err_int_ena = BIT(NPA_AURA_ERR_INT_AURA_ADD_OVER);
614         aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_ADD_UNDER);
615         aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_FREE_UNDER);
616         aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_POOL_DIS);
617         /* Many to one reduction */
618         aura->err_qint_idx = aura_id % lf->qints;
619
620         /* Update pool fields */
621         pool->stack_base = mz->iova;
622         pool->ena = 1;
623         pool->buf_size = block_size / OTX2_ALIGN;
624         pool->stack_max_pages = stack_size;
625         pool->shift = __builtin_clz(block_count) - 8;
626         pool->ptr_start = 0;
627         pool->ptr_end = ~0;
628         pool->stack_caching = 1;
629         pool->err_int_ena = BIT(NPA_POOL_ERR_INT_OVFLS);
630         pool->err_int_ena |= BIT(NPA_POOL_ERR_INT_RANGE);
631         pool->err_int_ena |= BIT(NPA_POOL_ERR_INT_PERR);
632
633         /* Many to one reduction */
634         pool->err_qint_idx = pool_id % lf->qints;
635
636         /* Issue AURA_INIT and POOL_INIT op */
637         rc = npa_lf_aura_pool_init(lf->mbox, aura_id, aura, pool);
638         if (rc)
639                 goto stack_mem_free;
640
641         *aura_handle = npa_lf_aura_handle_gen(aura_id, lf->base);
642
643         /* Update aura count */
644         npa_lf_aura_op_cnt_set(*aura_handle, 0, block_count);
645         /* Read it back to make sure aura count is updated */
646         npa_lf_aura_op_cnt_get(*aura_handle);
647
648         return 0;
649
650 stack_mem_free:
651         rte_memzone_free(mz);
652 aura_res_put:
653         rte_bitmap_set(lf->npa_bmp, aura_id);
654 exit:
655         return rc;
656 }
657
658 static int
659 npa_lf_aura_pool_pair_free(struct otx2_npa_lf *lf, uint64_t aura_handle)
660 {
661         char name[RTE_MEMZONE_NAMESIZE];
662         int aura_id, pool_id, rc;
663
664         if (!lf || !aura_handle)
665                 return NPA_LF_ERR_PARAM;
666
667         aura_id = pool_id = npa_lf_aura_handle_to_aura(aura_handle);
668         rc = npa_lf_aura_pool_fini(lf->mbox, aura_id, aura_handle);
669         rc |= npa_lf_stack_dma_free(lf, name, pool_id);
670
671         rte_bitmap_set(lf->npa_bmp, aura_id);
672
673         return rc;
674 }
675
676 static int
677 npa_lf_aura_range_update_check(uint64_t aura_handle)
678 {
679         uint64_t aura_id = npa_lf_aura_handle_to_aura(aura_handle);
680         struct otx2_npa_lf *lf = otx2_npa_lf_obj_get();
681         struct npa_aura_lim *lim = lf->aura_lim;
682         __otx2_io struct npa_pool_s *pool;
683         struct npa_aq_enq_req *req;
684         struct npa_aq_enq_rsp *rsp;
685         int rc;
686
687         req  = otx2_mbox_alloc_msg_npa_aq_enq(lf->mbox);
688
689         req->aura_id = aura_id;
690         req->ctype = NPA_AQ_CTYPE_POOL;
691         req->op = NPA_AQ_INSTOP_READ;
692
693         rc = otx2_mbox_process_msg(lf->mbox, (void *)&rsp);
694         if (rc) {
695                 otx2_err("Failed to get pool(0x%"PRIx64") context", aura_id);
696                 return rc;
697         }
698
699         pool = &rsp->pool;
700
701         if (lim[aura_id].ptr_start != pool->ptr_start ||
702                 lim[aura_id].ptr_end != pool->ptr_end) {
703                 otx2_err("Range update failed on pool(0x%"PRIx64")", aura_id);
704                 return -ERANGE;
705         }
706
707         return 0;
708 }
709
710 static int
711 otx2_npa_alloc(struct rte_mempool *mp)
712 {
713         uint32_t block_size, block_count;
714         uint64_t aura_handle = 0;
715         struct otx2_npa_lf *lf;
716         struct npa_aura_s aura;
717         struct npa_pool_s pool;
718         size_t padding;
719         int rc;
720
721         lf = otx2_npa_lf_obj_get();
722         if (lf == NULL) {
723                 rc = -EINVAL;
724                 goto error;
725         }
726
727         block_size = mp->elt_size + mp->header_size + mp->trailer_size;
728         /*
729          * OCTEON TX2 has 8 sets, 41 ways L1D cache, VA<9:7> bits dictate
730          * the set selection.
731          * Add additional padding to ensure that the element size always
732          * occupies odd number of cachelines to ensure even distribution
733          * of elements among L1D cache sets.
734          */
735         padding = ((block_size / RTE_CACHE_LINE_SIZE) % 2) ? 0 :
736                                 RTE_CACHE_LINE_SIZE;
737         mp->trailer_size += padding;
738         block_size += padding;
739
740         block_count = mp->size;
741
742         if (block_size % OTX2_ALIGN != 0) {
743                 otx2_err("Block size should be multiple of 128B");
744                 rc = -ERANGE;
745                 goto error;
746         }
747
748         memset(&aura, 0, sizeof(struct npa_aura_s));
749         memset(&pool, 0, sizeof(struct npa_pool_s));
750         pool.nat_align = 1;
751         pool.buf_offset = 1;
752
753         if ((uint32_t)pool.buf_offset * OTX2_ALIGN != mp->header_size) {
754                 otx2_err("Unsupported mp->header_size=%d", mp->header_size);
755                 rc = -EINVAL;
756                 goto error;
757         }
758
759         /* Use driver specific mp->pool_config to override aura config */
760         if (mp->pool_config != NULL)
761                 memcpy(&aura, mp->pool_config, sizeof(struct npa_aura_s));
762
763         rc = npa_lf_aura_pool_pair_alloc(lf, block_size, block_count,
764                          &aura, &pool, &aura_handle);
765         if (rc) {
766                 otx2_err("Failed to alloc pool or aura rc=%d", rc);
767                 goto error;
768         }
769
770         /* Store aura_handle for future queue operations */
771         mp->pool_id = aura_handle;
772         otx2_npa_dbg("lf=%p block_sz=%d block_count=%d aura_handle=0x%"PRIx64,
773                      lf, block_size, block_count, aura_handle);
774
775         /* Just hold the reference of the object */
776         otx2_npa_lf_obj_ref();
777         return 0;
778 error:
779         return rc;
780 }
781
782 static void
783 otx2_npa_free(struct rte_mempool *mp)
784 {
785         struct otx2_npa_lf *lf = otx2_npa_lf_obj_get();
786         int rc = 0;
787
788         otx2_npa_dbg("lf=%p aura_handle=0x%"PRIx64, lf, mp->pool_id);
789         if (lf != NULL)
790                 rc = npa_lf_aura_pool_pair_free(lf, mp->pool_id);
791
792         if (rc)
793                 otx2_err("Failed to free pool or aura rc=%d", rc);
794
795         /* Release the reference of npalf */
796         otx2_npa_lf_fini();
797 }
798
799 static ssize_t
800 otx2_npa_calc_mem_size(const struct rte_mempool *mp, uint32_t obj_num,
801                        uint32_t pg_shift, size_t *min_chunk_size, size_t *align)
802 {
803         size_t total_elt_sz;
804
805         /* Need space for one more obj on each chunk to fulfill
806          * alignment requirements.
807          */
808         total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
809         return rte_mempool_op_calc_mem_size_helper(mp, obj_num, pg_shift,
810                                                 total_elt_sz, min_chunk_size,
811                                                 align);
812 }
813
814 static uint8_t
815 otx2_npa_l1d_way_set_get(uint64_t iova)
816 {
817         return (iova >> rte_log2_u32(RTE_CACHE_LINE_SIZE)) & 0x7;
818 }
819
820 static int
821 otx2_npa_populate(struct rte_mempool *mp, unsigned int max_objs, void *vaddr,
822                   rte_iova_t iova, size_t len,
823                   rte_mempool_populate_obj_cb_t *obj_cb, void *obj_cb_arg)
824 {
825 #define OTX2_L1D_NB_SETS        8
826         uint64_t distribution[OTX2_L1D_NB_SETS];
827         rte_iova_t start_iova;
828         size_t total_elt_sz;
829         uint8_t set;
830         size_t off;
831         int i;
832
833         if (iova == RTE_BAD_IOVA)
834                 return -EINVAL;
835
836         total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
837
838         /* Align object start address to a multiple of total_elt_sz */
839         off = total_elt_sz - ((((uintptr_t)vaddr - 1) % total_elt_sz) + 1);
840
841         if (len < off)
842                 return -EINVAL;
843
844
845         vaddr = (char *)vaddr + off;
846         iova += off;
847         len -= off;
848
849         memset(distribution, 0, sizeof(uint64_t) * OTX2_L1D_NB_SETS);
850         start_iova = iova;
851         while (start_iova < iova + len) {
852                 set = otx2_npa_l1d_way_set_get(start_iova + mp->header_size);
853                 distribution[set]++;
854                 start_iova += total_elt_sz;
855         }
856
857         otx2_npa_dbg("iova %"PRIx64", aligned iova %"PRIx64"", iova - off,
858                      iova);
859         otx2_npa_dbg("length %"PRIu64", aligned length %"PRIu64"",
860                      (uint64_t)(len + off), (uint64_t)len);
861         otx2_npa_dbg("element size %"PRIu64"", (uint64_t)total_elt_sz);
862         otx2_npa_dbg("requested objects %"PRIu64", possible objects %"PRIu64"",
863                      (uint64_t)max_objs, (uint64_t)(len / total_elt_sz));
864         otx2_npa_dbg("L1D set distribution :");
865         for (i = 0; i < OTX2_L1D_NB_SETS; i++)
866                 otx2_npa_dbg("set[%d] : objects : %"PRIu64"", i,
867                              distribution[i]);
868
869         npa_lf_aura_op_range_set(mp->pool_id, iova, iova + len);
870
871         if (npa_lf_aura_range_update_check(mp->pool_id) < 0)
872                 return -EBUSY;
873
874         return rte_mempool_op_populate_helper(mp,
875                                         RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ,
876                                         max_objs, vaddr, iova, len,
877                                         obj_cb, obj_cb_arg);
878 }
879
880 static struct rte_mempool_ops otx2_npa_ops = {
881         .name = "octeontx2_npa",
882         .alloc = otx2_npa_alloc,
883         .free = otx2_npa_free,
884         .enqueue = otx2_npa_enq,
885         .get_count = otx2_npa_get_count,
886         .calc_mem_size = otx2_npa_calc_mem_size,
887         .populate = otx2_npa_populate,
888 #if defined(RTE_ARCH_ARM64)
889         .dequeue = otx2_npa_deq_arm64,
890 #else
891         .dequeue = otx2_npa_deq,
892 #endif
893 };
894
895 MEMPOOL_REGISTER_OPS(otx2_npa_ops);