common/cnxk: support NPA bulk alloc/free
[dpdk.git] / drivers / common / cnxk / roc_npa.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #ifndef _ROC_NPA_H_
6 #define _ROC_NPA_H_
7
8 #define ROC_AURA_ID_MASK       (BIT_ULL(16) - 1)
9 #define ROC_AURA_OP_LIMIT_MASK (BIT_ULL(36) - 1)
10
11 /* 16 CASP instructions can be outstanding in CN9k, but we use only 15
12  * outstanding CASPs as we run out of registers.
13  */
14 #define ROC_CN9K_NPA_BULK_ALLOC_MAX_PTRS 30
15
16 /*
17  * Generate 64bit handle to have optimized alloc and free aura operation.
18  * 0 - ROC_AURA_ID_MASK for storing the aura_id.
19  * [ROC_AURA_ID_MASK+1, (2^64 - 1)] for storing the lf base address.
20  * This scheme is valid when OS can give ROC_AURA_ID_MASK
21  * aligned address for lf base address.
22  */
23 static inline uint64_t
24 roc_npa_aura_handle_gen(uint32_t aura_id, uintptr_t addr)
25 {
26         uint64_t val;
27
28         val = aura_id & ROC_AURA_ID_MASK;
29         return (uint64_t)addr | val;
30 }
31
32 static inline uint64_t
33 roc_npa_aura_handle_to_aura(uint64_t aura_handle)
34 {
35         return aura_handle & ROC_AURA_ID_MASK;
36 }
37
38 static inline uintptr_t
39 roc_npa_aura_handle_to_base(uint64_t aura_handle)
40 {
41         return (uintptr_t)(aura_handle & ~ROC_AURA_ID_MASK);
42 }
43
44 static inline uint64_t
45 roc_npa_aura_op_alloc(uint64_t aura_handle, const int drop)
46 {
47         uint64_t wdata = roc_npa_aura_handle_to_aura(aura_handle);
48         int64_t *addr;
49
50         if (drop)
51                 wdata |= BIT_ULL(63); /* DROP */
52
53         addr = (int64_t *)(roc_npa_aura_handle_to_base(aura_handle) +
54                            NPA_LF_AURA_OP_ALLOCX(0));
55         return roc_atomic64_add_nosync(wdata, addr);
56 }
57
58 static inline void
59 roc_npa_aura_op_free(uint64_t aura_handle, const int fabs, uint64_t iova)
60 {
61         uint64_t reg = roc_npa_aura_handle_to_aura(aura_handle);
62         const uint64_t addr =
63                 roc_npa_aura_handle_to_base(aura_handle) + NPA_LF_AURA_OP_FREE0;
64         if (fabs)
65                 reg |= BIT_ULL(63); /* FABS */
66
67         roc_store_pair(iova, reg, addr);
68 }
69
70 static inline uint64_t
71 roc_npa_aura_op_cnt_get(uint64_t aura_handle)
72 {
73         uint64_t wdata;
74         int64_t *addr;
75         uint64_t reg;
76
77         wdata = roc_npa_aura_handle_to_aura(aura_handle) << 44;
78         addr = (int64_t *)(roc_npa_aura_handle_to_base(aura_handle) +
79                            NPA_LF_AURA_OP_CNT);
80         reg = roc_atomic64_add_nosync(wdata, addr);
81
82         if (reg & BIT_ULL(42) /* OP_ERR */)
83                 return 0;
84         else
85                 return reg & 0xFFFFFFFFF;
86 }
87
88 static inline void
89 roc_npa_aura_op_cnt_set(uint64_t aura_handle, const int sign, uint64_t count)
90 {
91         uint64_t reg = count & (BIT_ULL(36) - 1);
92
93         if (sign)
94                 reg |= BIT_ULL(43); /* CNT_ADD */
95
96         reg |= (roc_npa_aura_handle_to_aura(aura_handle) << 44);
97
98         plt_write64(reg, roc_npa_aura_handle_to_base(aura_handle) +
99                                  NPA_LF_AURA_OP_CNT);
100 }
101
102 static inline uint64_t
103 roc_npa_aura_op_limit_get(uint64_t aura_handle)
104 {
105         uint64_t wdata;
106         int64_t *addr;
107         uint64_t reg;
108
109         wdata = roc_npa_aura_handle_to_aura(aura_handle) << 44;
110         addr = (int64_t *)(roc_npa_aura_handle_to_base(aura_handle) +
111                            NPA_LF_AURA_OP_LIMIT);
112         reg = roc_atomic64_add_nosync(wdata, addr);
113
114         if (reg & BIT_ULL(42) /* OP_ERR */)
115                 return 0;
116         else
117                 return reg & ROC_AURA_OP_LIMIT_MASK;
118 }
119
120 static inline void
121 roc_npa_aura_op_limit_set(uint64_t aura_handle, uint64_t limit)
122 {
123         uint64_t reg = limit & ROC_AURA_OP_LIMIT_MASK;
124
125         reg |= (roc_npa_aura_handle_to_aura(aura_handle) << 44);
126
127         plt_write64(reg, roc_npa_aura_handle_to_base(aura_handle) +
128                                  NPA_LF_AURA_OP_LIMIT);
129 }
130
131 static inline uint64_t
132 roc_npa_aura_op_available(uint64_t aura_handle)
133 {
134         uint64_t wdata;
135         uint64_t reg;
136         int64_t *addr;
137
138         wdata = roc_npa_aura_handle_to_aura(aura_handle) << 44;
139         addr = (int64_t *)(roc_npa_aura_handle_to_base(aura_handle) +
140                            NPA_LF_POOL_OP_AVAILABLE);
141         reg = roc_atomic64_add_nosync(wdata, addr);
142
143         if (reg & BIT_ULL(42) /* OP_ERR */)
144                 return 0;
145         else
146                 return reg & 0xFFFFFFFFF;
147 }
148
149 static inline void
150 roc_npa_aura_op_bulk_free(uint64_t aura_handle, uint64_t const *buf,
151                           unsigned int num, const int fabs)
152 {
153         unsigned int i;
154
155         for (i = 0; i < num; i++) {
156                 const uint64_t inbuf = buf[i];
157
158                 roc_npa_aura_op_free(aura_handle, fabs, inbuf);
159         }
160 }
161
162 static inline unsigned int
163 roc_npa_aura_bulk_alloc(uint64_t aura_handle, uint64_t *buf, unsigned int num,
164                         const int drop)
165 {
166 #if defined(__aarch64__)
167         uint64_t wdata = roc_npa_aura_handle_to_aura(aura_handle);
168         unsigned int i, count;
169         uint64_t addr;
170
171         if (drop)
172                 wdata |= BIT_ULL(63); /* DROP */
173
174         addr = roc_npa_aura_handle_to_base(aura_handle) +
175                NPA_LF_AURA_OP_ALLOCX(0);
176
177         switch (num) {
178         case 30:
179                 asm volatile(
180                         ".cpu  generic+lse\n"
181                         "mov v18.d[0], %[dst]\n"
182                         "mov v18.d[1], %[loc]\n"
183                         "mov v19.d[0], %[wdata]\n"
184                         "mov v19.d[1], x30\n"
185                         "mov v20.d[0], x24\n"
186                         "mov v20.d[1], x25\n"
187                         "mov v21.d[0], x26\n"
188                         "mov v21.d[1], x27\n"
189                         "mov v22.d[0], x28\n"
190                         "mov v22.d[1], x29\n"
191                         "mov x28, v19.d[0]\n"
192                         "mov x29, v19.d[0]\n"
193                         "mov x30, v18.d[1]\n"
194                         "casp x0, x1, x28, x29, [x30]\n"
195                         "casp x2, x3, x28, x29, [x30]\n"
196                         "casp x4, x5, x28, x29, [x30]\n"
197                         "casp x6, x7, x28, x29, [x30]\n"
198                         "casp x8, x9, x28, x29, [x30]\n"
199                         "casp x10, x11, x28, x29, [x30]\n"
200                         "casp x12, x13, x28, x29, [x30]\n"
201                         "casp x14, x15, x28, x29, [x30]\n"
202                         "casp x16, x17, x28, x29, [x30]\n"
203                         "casp x18, x19, x28, x29, [x30]\n"
204                         "casp x20, x21, x28, x29, [x30]\n"
205                         "casp x22, x23, x28, x29, [x30]\n"
206                         "casp x24, x25, x28, x29, [x30]\n"
207                         "casp x26, x27, x28, x29, [x30]\n"
208                         "casp x28, x29, x28, x29, [x30]\n"
209                         "mov x30, v18.d[0]\n"
210                         "stp x0, x1, [x30]\n"
211                         "stp x2, x3, [x30, #16]\n"
212                         "stp x4, x5, [x30, #32]\n"
213                         "stp x6, x7, [x30, #48]\n"
214                         "stp x8, x9, [x30, #64]\n"
215                         "stp x10, x11, [x30, #80]\n"
216                         "stp x12, x13, [x30, #96]\n"
217                         "stp x14, x15, [x30, #112]\n"
218                         "stp x16, x17, [x30, #128]\n"
219                         "stp x18, x19, [x30, #144]\n"
220                         "stp x20, x21, [x30, #160]\n"
221                         "stp x22, x23, [x30, #176]\n"
222                         "stp x24, x25, [x30, #192]\n"
223                         "stp x26, x27, [x30, #208]\n"
224                         "stp x28, x29, [x30, #224]\n"
225                         "mov %[dst], v18.d[0]\n"
226                         "mov %[loc], v18.d[1]\n"
227                         "mov %[wdata], v19.d[0]\n"
228                         "mov x30, v19.d[1]\n"
229                         "mov x24, v20.d[0]\n"
230                         "mov x25, v20.d[1]\n"
231                         "mov x26, v21.d[0]\n"
232                         "mov x27, v21.d[1]\n"
233                         "mov x28, v22.d[0]\n"
234                         "mov x29, v22.d[1]\n"
235                         :
236                         : [wdata] "r"(wdata), [loc] "r"(addr), [dst] "r"(buf)
237                         : "memory", "x0", "x1", "x2", "x3", "x4", "x5", "x6",
238                           "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14",
239                           "x15", "x16", "x17", "x18", "x19", "x20", "x21",
240                           "x22", "x23", "v18", "v19", "v20", "v21", "v22");
241                 break;
242         case 16:
243                 asm volatile(
244                         ".cpu  generic+lse\n"
245                         "mov x16, %[wdata]\n"
246                         "mov x17, %[wdata]\n"
247                         "casp x0, x1, x16, x17, [%[loc]]\n"
248                         "casp x2, x3, x16, x17, [%[loc]]\n"
249                         "casp x4, x5, x16, x17, [%[loc]]\n"
250                         "casp x6, x7, x16, x17, [%[loc]]\n"
251                         "casp x8, x9, x16, x17, [%[loc]]\n"
252                         "casp x10, x11, x16, x17, [%[loc]]\n"
253                         "casp x12, x13, x16, x17, [%[loc]]\n"
254                         "casp x14, x15, x16, x17, [%[loc]]\n"
255                         "stp x0, x1, [%[dst]]\n"
256                         "stp x2, x3, [%[dst], #16]\n"
257                         "stp x4, x5, [%[dst], #32]\n"
258                         "stp x6, x7, [%[dst], #48]\n"
259                         "stp x8, x9, [%[dst], #64]\n"
260                         "stp x10, x11, [%[dst], #80]\n"
261                         "stp x12, x13, [%[dst], #96]\n"
262                         "stp x14, x15, [%[dst], #112]\n"
263                         :
264                         : [wdata] "r" (wdata), [dst] "r" (buf), [loc] "r" (addr)
265                         : "memory", "x0", "x1", "x2", "x3", "x4", "x5", "x6",
266                           "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14",
267                           "x15", "x16", "x17"
268                 );
269                 break;
270         case 8:
271                 asm volatile(
272                         ".cpu  generic+lse\n"
273                         "mov x16, %[wdata]\n"
274                         "mov x17, %[wdata]\n"
275                         "casp x0, x1, x16, x17, [%[loc]]\n"
276                         "casp x2, x3, x16, x17, [%[loc]]\n"
277                         "casp x4, x5, x16, x17, [%[loc]]\n"
278                         "casp x6, x7, x16, x17, [%[loc]]\n"
279                         "stp x0, x1, [%[dst]]\n"
280                         "stp x2, x3, [%[dst], #16]\n"
281                         "stp x4, x5, [%[dst], #32]\n"
282                         "stp x6, x7, [%[dst], #48]\n"
283                         :
284                         : [wdata] "r" (wdata), [dst] "r" (buf), [loc] "r" (addr)
285                         : "memory", "x0", "x1", "x2", "x3", "x4", "x5", "x6",
286                           "x7", "x16", "x17"
287                 );
288                 break;
289         case 4:
290                 asm volatile(
291                         ".cpu  generic+lse\n"
292                         "mov x16, %[wdata]\n"
293                         "mov x17, %[wdata]\n"
294                         "casp x0, x1, x16, x17, [%[loc]]\n"
295                         "casp x2, x3, x16, x17, [%[loc]]\n"
296                         "stp x0, x1, [%[dst]]\n"
297                         "stp x2, x3, [%[dst], #16]\n"
298                         :
299                         : [wdata] "r" (wdata), [dst] "r" (buf), [loc] "r" (addr)
300                         : "memory", "x0", "x1", "x2", "x3", "x16", "x17"
301                 );
302                 break;
303         case 2:
304                 asm volatile(
305                         ".cpu  generic+lse\n"
306                         "mov x16, %[wdata]\n"
307                         "mov x17, %[wdata]\n"
308                         "casp x0, x1, x16, x17, [%[loc]]\n"
309                         "stp x0, x1, [%[dst]]\n"
310                         :
311                         : [wdata] "r" (wdata), [dst] "r" (buf), [loc] "r" (addr)
312                         : "memory", "x0", "x1", "x16", "x17"
313                 );
314                 break;
315         case 1:
316                 buf[0] = roc_npa_aura_op_alloc(aura_handle, drop);
317                 return !!buf[0];
318         }
319
320         /* Pack the pointers */
321         for (i = 0, count = 0; i < num; i++)
322                 if (buf[i])
323                         buf[count++] = buf[i];
324
325         return count;
326 #else
327         unsigned int i, count;
328
329         for (i = 0, count = 0; i < num; i++) {
330                 buf[count] = roc_npa_aura_op_alloc(aura_handle, drop);
331                 if (buf[count])
332                         count++;
333         }
334
335         return count;
336 #endif
337 }
338
339 static inline unsigned int
340 roc_npa_aura_op_bulk_alloc(uint64_t aura_handle, uint64_t *buf,
341                            unsigned int num, const int drop, const int partial)
342 {
343         unsigned int chunk, count, num_alloc;
344
345         count = 0;
346         while (num) {
347                 chunk = (num >= ROC_CN9K_NPA_BULK_ALLOC_MAX_PTRS) ?
348                                       ROC_CN9K_NPA_BULK_ALLOC_MAX_PTRS :
349                                       plt_align32prevpow2(num);
350
351                 num_alloc =
352                         roc_npa_aura_bulk_alloc(aura_handle, buf, chunk, drop);
353
354                 count += num_alloc;
355                 buf += num_alloc;
356                 num -= num_alloc;
357
358                 if (unlikely(num_alloc != chunk))
359                         break;
360         }
361
362         /* If the requested number of pointers was not allocated and if partial
363          * alloc is not desired, then free allocated pointers.
364          */
365         if (unlikely(num != 0 && !partial)) {
366                 roc_npa_aura_op_bulk_free(aura_handle, buf - count, count, 1);
367                 count = 0;
368         }
369
370         return count;
371 }
372
373 struct roc_npa {
374         struct plt_pci_device *pci_dev;
375
376 #define ROC_NPA_MEM_SZ (1 * 1024)
377         uint8_t reserved[ROC_NPA_MEM_SZ] __plt_cache_aligned;
378 } __plt_cache_aligned;
379
380 int __roc_api roc_npa_dev_init(struct roc_npa *roc_npa);
381 int __roc_api roc_npa_dev_fini(struct roc_npa *roc_npa);
382
383 /* NPA pool */
384 int __roc_api roc_npa_pool_create(uint64_t *aura_handle, uint32_t block_size,
385                                   uint32_t block_count, struct npa_aura_s *aura,
386                                   struct npa_pool_s *pool);
387 int __roc_api roc_npa_aura_limit_modify(uint64_t aura_handle,
388                                         uint16_t aura_limit);
389 int __roc_api roc_npa_pool_destroy(uint64_t aura_handle);
390 int __roc_api roc_npa_pool_range_update_check(uint64_t aura_handle);
391 void __roc_api roc_npa_aura_op_range_set(uint64_t aura_handle,
392                                          uint64_t start_iova,
393                                          uint64_t end_iova);
394
395 /* Debug */
396 int __roc_api roc_npa_ctx_dump(void);
397 int __roc_api roc_npa_dump(void);
398
399 #endif /* _ROC_NPA_H_ */