Branch data Line data Source code
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 : : #include <stdint.h>
9 : :
10 : : #include "hw/npa.h"
11 : :
12 : : #include "roc_bits.h"
13 : : #include "roc_constants.h"
14 : : #if defined(__aarch64__)
15 : : #include "roc_io.h"
16 : : #else
17 : : #include "roc_io_generic.h"
18 : : #endif
19 : : #include "roc_model.h"
20 : : #include "roc_npa_dp.h"
21 : :
22 : : #define ROC_AURA_OP_LIMIT_MASK (BIT_ULL(36) - 1)
23 : :
24 : : #define ROC_NPA_MAX_BLOCK_SZ_CN9K (128 * 1024)
25 : : #define ROC_NPA_MAX_BLOCK_SZ_CN10K (128 * 1024)
26 : : #define ROC_NPA_MAX_BLOCK_SZ_CN20K (256 * 1024)
27 : : #define ROC_CN10K_NPA_BATCH_ALLOC_MAX_PTRS 512
28 : : #define ROC_CN10K_NPA_BATCH_FREE_MAX_PTRS 15U
29 : : #define ROC_CN10K_NPA_BATCH_FREE_BURST_MAX 16U
30 : :
31 : : /* This value controls how much of the present average resource level is used to
32 : : * calculate the new resource level.
33 : : */
34 : : #define ROC_NPA_AVG_CONT 0xE0
35 : :
36 : : /* 16 CASP instructions can be outstanding in CN9k, but we use only 15
37 : : * outstanding CASPs as we run out of registers.
38 : : */
39 : : #define ROC_CN9K_NPA_BULK_ALLOC_MAX_PTRS 30
40 : :
41 : : static inline uint32_t
42 : : roc_npa_max_block_size(void)
43 : : {
44 [ # # ]: 0 : if (roc_model_is_cn20k())
45 : : return ROC_NPA_MAX_BLOCK_SZ_CN20K;
46 : : else if (roc_model_is_cn10k())
47 : : return ROC_NPA_MAX_BLOCK_SZ_CN10K;
48 : : else
49 : : return ROC_NPA_MAX_BLOCK_SZ_CN9K;
50 : : }
51 : :
52 : : /*
53 : : * Generate 64bit handle to have optimized alloc and free aura operation.
54 : : * 0 - ROC_AURA_ID_MASK for storing the aura_id.
55 : : * [ROC_AURA_ID_MASK+1, (2^64 - 1)] for storing the lf base address.
56 : : * This scheme is valid when OS can give ROC_AURA_ID_MASK
57 : : * aligned address for lf base address.
58 : : */
59 : : static inline uint64_t
60 : : roc_npa_aura_handle_gen(uint32_t aura_id, uintptr_t addr)
61 : : {
62 : : uint64_t val;
63 : :
64 : 0 : val = aura_id & ROC_AURA_ID_MASK;
65 [ # # # # : 0 : return (uint64_t)addr | val;
# # ]
66 : : }
67 : :
68 : : static inline uint64_t
69 : : roc_npa_aura_op_alloc(uint64_t aura_handle, const int drop)
70 : : {
71 : : uint64_t wdata = roc_npa_aura_handle_to_aura(aura_handle);
72 : : int64_t *addr;
73 : :
74 : : if (drop)
75 : : wdata |= BIT_ULL(63); /* DROP */
76 : :
77 : : addr = (int64_t *)(roc_npa_aura_handle_to_base(aura_handle) +
78 : : NPA_LF_AURA_OP_ALLOCX(0));
79 : : return roc_atomic64_add_nosync(wdata, addr);
80 : : }
81 : :
82 : : static inline uint64_t
83 : : roc_npa_aura_op_cnt_get(uint64_t aura_handle)
84 : : {
85 : : uint64_t wdata, shift;
86 : : int64_t *addr;
87 : : uint64_t reg;
88 : :
89 : : shift = roc_model_is_cn20k() ? 47 : 44;
90 : : wdata = roc_npa_aura_handle_to_aura(aura_handle) << shift;
91 : : addr = (int64_t *)(roc_npa_aura_handle_to_base(aura_handle) +
92 : : NPA_LF_AURA_OP_CNT);
93 : : reg = roc_atomic64_add_nosync(wdata, addr);
94 : :
95 : : if (reg & BIT_ULL(42) /* OP_ERR */)
96 : : return 0;
97 : : else
98 : : return reg & 0xFFFFFFFFF;
99 : : }
100 : :
101 : : static inline void
102 : : roc_npa_aura_op_cnt_set(uint64_t aura_handle, const int sign, uint64_t count)
103 : : {
104 : 0 : uint64_t reg = count & (BIT_ULL(36) - 1);
105 : : uint64_t shift;
106 : :
107 : : if (sign)
108 [ # # # # ]: 0 : reg |= BIT_ULL(43); /* CNT_ADD */
109 : :
110 [ # # # # : 0 : shift = roc_model_is_cn20k() ? 47 : 44;
# # ]
111 : 0 : reg |= (roc_npa_aura_handle_to_aura(aura_handle) << shift);
112 : :
113 : 0 : plt_write64(reg, roc_npa_aura_handle_to_base(aura_handle) +
114 : : NPA_LF_AURA_OP_CNT);
115 : 0 : }
116 : :
117 : : static inline uint64_t
118 : : roc_npa_aura_op_limit_get(uint64_t aura_handle)
119 : : {
120 : : uint64_t wdata, shift;
121 : : int64_t *addr;
122 : : uint64_t reg;
123 : :
124 : : shift = roc_model_is_cn20k() ? 47 : 44;
125 : : wdata = roc_npa_aura_handle_to_aura(aura_handle) << shift;
126 : : addr = (int64_t *)(roc_npa_aura_handle_to_base(aura_handle) +
127 : : NPA_LF_AURA_OP_LIMIT);
128 : : reg = roc_atomic64_add_nosync(wdata, addr);
129 : :
130 : : if (reg & BIT_ULL(42) /* OP_ERR */)
131 : : return 0;
132 : : else
133 : : return reg & ROC_AURA_OP_LIMIT_MASK;
134 : : }
135 : :
136 : : static inline void
137 : : roc_npa_aura_op_limit_set(uint64_t aura_handle, uint64_t limit)
138 : : {
139 : : uint64_t reg = limit & ROC_AURA_OP_LIMIT_MASK;
140 : : uint64_t shift;
141 : :
142 : : shift = roc_model_is_cn20k() ? 47 : 44;
143 : : reg |= (roc_npa_aura_handle_to_aura(aura_handle) << shift);
144 : :
145 : : plt_write64(reg, roc_npa_aura_handle_to_base(aura_handle) +
146 : : NPA_LF_AURA_OP_LIMIT);
147 : : }
148 : :
149 : : static inline uint64_t
150 : : roc_npa_aura_op_available(uint64_t aura_handle)
151 : : {
152 : : uint64_t wdata, shift;
153 : : uint64_t reg;
154 : : int64_t *addr;
155 : :
156 : : shift = roc_model_is_cn20k() ? 47 : 44;
157 : : wdata = roc_npa_aura_handle_to_aura(aura_handle) << shift;
158 : : addr = (int64_t *)(roc_npa_aura_handle_to_base(aura_handle) +
159 : : NPA_LF_POOL_OP_AVAILABLE);
160 : : reg = roc_atomic64_add_nosync(wdata, addr);
161 : :
162 : : if (reg & BIT_ULL(42) /* OP_ERR */)
163 : : return 0;
164 : : else
165 : : return reg & 0xFFFFFFFFF;
166 : : }
167 : :
168 : : /* Wait for a given timeout, repeatedly checking whether the available
169 : : * pointers has reached the given count. Returns the available pointer
170 : : * count if it has reached the given count or if timeout has expired
171 : : */
172 : : static inline uint32_t
173 : : roc_npa_aura_op_available_wait(uint64_t aura_handle, uint32_t count,
174 : : uint32_t tmo_ms)
175 : : {
176 : : #define OP_AVAIL_WAIT_MS_DEFAULT (100)
177 : : #define OP_AVAIL_CHECK_INTERVAL_MS (1)
178 : : uint32_t op_avail;
179 : : int retry;
180 : :
181 : : tmo_ms = tmo_ms ? tmo_ms : OP_AVAIL_WAIT_MS_DEFAULT;
182 : :
183 : : retry = tmo_ms / OP_AVAIL_CHECK_INTERVAL_MS;
184 : : op_avail = roc_npa_aura_op_available(aura_handle);
185 [ # # # # ]: 0 : while (retry && (op_avail < count)) {
186 : : plt_delay_ms(OP_AVAIL_CHECK_INTERVAL_MS);
187 : : op_avail = roc_npa_aura_op_available(aura_handle);
188 : 0 : retry--;
189 : : }
190 : :
191 : : return op_avail;
192 : : }
193 : :
194 : : static inline void
195 : : roc_npa_pool_op_dpc_reset(uint64_t aura_handle, uint8_t counter_id)
196 : : {
197 : : plt_write64(0, roc_npa_aura_handle_to_base(aura_handle) +
198 : : NPA_LF_DPCX_ALLOC_CNT(counter_id));
199 : : plt_write64(0, roc_npa_aura_handle_to_base(aura_handle) +
200 : : NPA_LF_DPCX_FREE_CNT(counter_id));
201 : : plt_write64(0, roc_npa_aura_handle_to_base(aura_handle) +
202 : : NPA_LF_DPCX_OUTST_BUF_CNT(counter_id));
203 : : plt_write64(0, roc_npa_aura_handle_to_base(aura_handle) +
204 : : NPA_LF_DPCX_CNT_HI_WM(counter_id));
205 : : }
206 : :
207 : : static inline uint64_t
208 : : roc_npa_pool_op_dpc_alloc_count(uint64_t aura_handle, uint8_t counter_id)
209 : : {
210 : : return plt_read64(roc_npa_aura_handle_to_base(aura_handle) +
211 : : NPA_LF_DPCX_ALLOC_CNT(counter_id));
212 : : }
213 : :
214 : : static inline uint64_t
215 : : roc_npa_pool_op_dpc_free_count(uint64_t aura_handle, uint8_t counter_id)
216 : : {
217 : : return plt_read64(roc_npa_aura_handle_to_base(aura_handle) +
218 : : NPA_LF_DPCX_FREE_CNT(counter_id));
219 : : }
220 : :
221 : : static inline uint64_t
222 : : roc_npa_pool_op_dpc_outst_buf_count(uint64_t aura_handle, uint8_t counter_id)
223 : : {
224 : : return plt_read64(roc_npa_aura_handle_to_base(aura_handle) +
225 : : NPA_LF_DPCX_OUTST_BUF_CNT(counter_id));
226 : : }
227 : :
228 : : static inline uint64_t
229 : : roc_npa_pool_op_dpc_high_wm_count(uint64_t aura_handle, uint8_t counter_id)
230 : : {
231 : : return plt_read64(roc_npa_aura_handle_to_base(aura_handle) +
232 : : NPA_LF_DPCX_CNT_HI_WM(counter_id));
233 : : }
234 : :
235 : : static inline uint64_t
236 : : roc_npa_pool_op_performance_counter(uint64_t aura_handle, const int drop)
237 : : {
238 : : union {
239 : : uint64_t u;
240 : : struct npa_aura_op_wdata_s s;
241 : : } op_wdata;
242 : : int64_t *addr;
243 : : uint64_t reg;
244 : :
245 : : op_wdata.u = 0;
246 : : op_wdata.s.aura = roc_npa_aura_handle_to_aura(aura_handle);
247 : : if (drop)
248 : : op_wdata.s.drop |= BIT_ULL(63); /* DROP */
249 : :
250 : : addr = (int64_t *)(roc_npa_aura_handle_to_base(aura_handle) +
251 : : NPA_LF_POOL_OP_PC);
252 : :
253 : : reg = roc_atomic64_add_nosync(op_wdata.u, addr);
254 : : /*
255 : : * NPA_LF_POOL_OP_PC Read Data
256 : : *
257 : : * 63 49 48 48 47 0
258 : : * -----------------------------
259 : : * | Reserved | OP_ERR | OP_PC |
260 : : * -----------------------------
261 : : */
262 : :
263 : : if (reg & BIT_ULL(48) /* OP_ERR */)
264 : : return 0;
265 : : else
266 : : return reg & 0xFFFFFFFFFFFF;
267 : : }
268 : :
269 : : static inline int
270 : : roc_npa_aura_batch_alloc_issue(uint64_t aura_handle, uint64_t *buf,
271 : : unsigned int num, const int dis_wait,
272 : : const int drop)
273 : : {
274 : : int64_t *addr;
275 : : uint64_t res;
276 : : union {
277 : : uint64_t u;
278 : : struct npa_batch_alloc_compare_s compare_s;
279 : : } cmp;
280 : :
281 [ # # # # : 0 : if (num > ROC_CN10K_NPA_BATCH_ALLOC_MAX_PTRS)
# # ]
282 : : return -1;
283 : :
284 : : addr = (int64_t *)(roc_npa_aura_handle_to_base(aura_handle) +
285 : : NPA_LF_AURA_BATCH_ALLOC);
286 [ # # # # ]: 0 : cmp.u = 0;
287 : 0 : cmp.compare_s.aura = roc_npa_aura_handle_to_aura(aura_handle);
288 : 0 : cmp.compare_s.drop = drop;
289 : : cmp.compare_s.stype = ALLOC_STYPE_STF;
290 : : cmp.compare_s.dis_wait = dis_wait;
291 : 0 : cmp.compare_s.count = num;
292 : :
293 : 0 : res = roc_atomic64_casl(cmp.u, (uint64_t)buf, addr);
294 [ # # # # : 0 : if (res != ALLOC_RESULT_ACCEPTED && res != ALLOC_RESULT_NOCORE)
# # ]
295 : 0 : return -1;
296 : :
297 : : return 0;
298 : : }
299 : :
300 : : /*
301 : : * Wait for a batch alloc operation on a cache line to complete.
302 : : */
303 : : static inline void
304 : 0 : roc_npa_batch_alloc_wait(uint64_t *cache_line, unsigned int wait_us)
305 : : {
306 : 0 : const uint64_t ticks = (uint64_t)wait_us * plt_tsc_hz() / (uint64_t)1E6;
307 : : const uint64_t start = plt_tsc_cycles();
308 : :
309 : : /* Batch alloc status code is updated in bits [5:6] of the first word
310 : : * of the 128 byte cache line.
311 : : */
312 [ # # ]: 0 : while (((__atomic_load_n(cache_line, __ATOMIC_ACQUIRE) >> 5) & 0x3) ==
313 : : ALLOC_CCODE_INVAL)
314 [ # # # # ]: 0 : if (wait_us && (plt_tsc_cycles() - start) >= ticks)
315 : : break;
316 : 0 : }
317 : :
318 : : /*
319 : : * Count the number of pointers in a single batch alloc cache line.
320 : : */
321 : : static inline unsigned int
322 : : roc_npa_aura_batch_alloc_count_line(uint64_t *line, unsigned int wait_us)
323 : : {
324 : : struct npa_batch_alloc_status_s *status;
325 : :
326 : : status = (struct npa_batch_alloc_status_s *)line;
327 : : roc_npa_batch_alloc_wait(line, wait_us);
328 : :
329 : : return status->count;
330 : : }
331 : :
332 : : /*
333 : : * Count the number of pointers in a sequence of batch alloc cache lines.
334 : : */
335 : : static inline unsigned int
336 : : roc_npa_aura_batch_alloc_count(uint64_t *aligned_buf, unsigned int num,
337 : : unsigned int wait_us)
338 : : {
339 : : unsigned int count, i;
340 : :
341 [ # # ]: 0 : if (num > ROC_CN10K_NPA_BATCH_ALLOC_MAX_PTRS)
342 : : return 0;
343 : :
344 : : count = 0;
345 : : /* Check each ROC cache line one by one */
346 [ # # ]: 0 : for (i = 0; i < num; i += (ROC_ALIGN >> 3)) {
347 : : struct npa_batch_alloc_status_s *status;
348 : :
349 : 0 : status = (struct npa_batch_alloc_status_s *)&aligned_buf[i];
350 : :
351 : 0 : roc_npa_batch_alloc_wait(&aligned_buf[i], wait_us);
352 : :
353 : 0 : count += status->count;
354 : : }
355 : :
356 : : return count;
357 : : }
358 : :
359 : : /*
360 : : * Extract allocated pointers from a single batch alloc cache line. This api
361 : : * only extracts the required number of pointers from the cache line and it
362 : : * adjusts the statsus->count so that a subsequent call to this api can
363 : : * extract the remaining pointers in the cache line appropriately.
364 : : */
365 : : static inline unsigned int
366 : : roc_npa_aura_batch_alloc_extract_line(uint64_t *buf, uint64_t *line,
367 : : unsigned int num, unsigned int *rem)
368 : : {
369 : : struct npa_batch_alloc_status_s *status;
370 : : unsigned int avail;
371 : :
372 : : status = (struct npa_batch_alloc_status_s *)line;
373 : : roc_npa_batch_alloc_wait(line, 0);
374 : : avail = status->count;
375 : : num = avail > num ? num : avail;
376 : : if (num)
377 : : memcpy(buf, &line[avail - num], num * sizeof(uint64_t));
378 : : avail -= num;
379 : : if (avail == 0) {
380 : : /* Clear the lowest 7 bits of the first pointer */
381 : : buf[0] &= ~0x7FUL;
382 : : status->ccode = 0;
383 : : }
384 : : status->count = avail;
385 : : *rem = avail;
386 : :
387 : : return num;
388 : : }
389 : :
390 : : /*
391 : : * Extract all allocated pointers from a sequence of batch alloc cache lines.
392 : : */
393 : : static inline unsigned int
394 : 0 : roc_npa_aura_batch_alloc_extract(uint64_t *buf, uint64_t *aligned_buf,
395 : : unsigned int num)
396 : : {
397 : : unsigned int count, i;
398 : :
399 [ # # ]: 0 : if (num > ROC_CN10K_NPA_BATCH_ALLOC_MAX_PTRS)
400 : : return 0;
401 : :
402 : : count = 0;
403 : : /* Check each ROC cache line one by one */
404 [ # # ]: 0 : for (i = 0; i < num; i += (ROC_ALIGN >> 3)) {
405 : : struct npa_batch_alloc_status_s *status;
406 : : int line_count;
407 : :
408 : 0 : status = (struct npa_batch_alloc_status_s *)&aligned_buf[i];
409 : :
410 : 0 : roc_npa_batch_alloc_wait(&aligned_buf[i], 0);
411 : :
412 : 0 : line_count = status->count;
413 : :
414 : : /* Clear the status from the cache line */
415 : 0 : status->ccode = 0;
416 : 0 : status->count = 0;
417 : :
418 : : /* 'Compress' the allocated buffers as there can
419 : : * be 'holes' at the end of the 128 byte cache
420 : : * lines.
421 : : */
422 : 0 : memmove(&buf[count], &aligned_buf[i],
423 : : line_count * sizeof(uint64_t));
424 : :
425 : 0 : count += line_count;
426 : : }
427 : :
428 : : return count;
429 : : }
430 : :
431 : : static inline void
432 : : roc_npa_aura_op_bulk_free(uint64_t aura_handle, uint64_t const *buf,
433 : : unsigned int num, const int fabs)
434 : : {
435 : : unsigned int i;
436 : :
437 [ # # ]: 0 : for (i = 0; i < num; i++) {
438 : 0 : const uint64_t inbuf = buf[i];
439 : :
440 : : roc_npa_aura_op_free(aura_handle, fabs, inbuf);
441 : : }
442 : : }
443 : :
444 : : /*
445 : : * Issue a batch alloc operation on a sequence of cache lines, wait for the
446 : : * batch alloc to complete and copy the pointers out into the user buffer.
447 : : */
448 : : static inline unsigned int
449 : : roc_npa_aura_op_batch_alloc(uint64_t aura_handle, uint64_t *buf,
450 : : unsigned int num, uint64_t *aligned_buf,
451 : : unsigned int aligned_buf_sz, const int dis_wait,
452 : : const int drop, const int partial)
453 : : {
454 : : unsigned int count, chunk, num_alloc;
455 : :
456 : : /* The buffer should be 128 byte cache line aligned */
457 : : if (((uint64_t)aligned_buf & (ROC_ALIGN - 1)) != 0)
458 : : return 0;
459 : :
460 : : count = 0;
461 : : while (num) {
462 : : /* Make sure that the pointers allocated fit into the cache
463 : : * lines reserved.
464 : : */
465 : : chunk = aligned_buf_sz / sizeof(uint64_t);
466 : : chunk = PLT_MIN(num, chunk);
467 : : chunk = PLT_MIN((int)chunk, ROC_CN10K_NPA_BATCH_ALLOC_MAX_PTRS);
468 : :
469 : : if (roc_npa_aura_batch_alloc_issue(aura_handle, aligned_buf,
470 : : chunk, dis_wait, drop))
471 : : break;
472 : :
473 : : num_alloc = roc_npa_aura_batch_alloc_extract(buf, aligned_buf,
474 : : chunk);
475 : :
476 : : count += num_alloc;
477 : : buf += num_alloc;
478 : : num -= num_alloc;
479 : :
480 : : if (num_alloc != chunk)
481 : : break;
482 : : }
483 : :
484 : : /* If the requested number of pointers was not allocated and if partial
485 : : * alloc is not desired, then free allocated pointers.
486 : : */
487 : : if (unlikely(num != 0 && !partial)) {
488 : : roc_npa_aura_op_bulk_free(aura_handle, buf - count, count, 1);
489 : : count = 0;
490 : : }
491 : :
492 : : return count;
493 : : }
494 : :
495 : : static inline void
496 : : roc_npa_aura_batch_free(uint64_t aura_handle, uint64_t const *buf,
497 : : unsigned int num, const int fabs, uint64_t lmt_addr,
498 : : uint64_t lmt_id)
499 : : {
500 : : uint64_t addr, tar_addr, free0;
501 : : volatile uint64_t *lmt_data;
502 : : unsigned int i;
503 : :
504 [ # # ]: 0 : lmt_data = (uint64_t *)lmt_addr;
505 : :
506 : : addr = roc_npa_aura_handle_to_base(aura_handle) +
507 : : NPA_LF_AURA_BATCH_FREE0;
508 : :
509 : : /*
510 : : * NPA_LF_AURA_BATCH_FREE0
511 : : *
512 : : * 63 63 62 34 33 32 31 20 19 0
513 : : * -----------------------------------------
514 : : * | FABS | Rsvd | COUNT_EOT | Rsvd | AURA |
515 : : * -----------------------------------------
516 : : */
517 : : free0 = roc_npa_aura_handle_to_aura(aura_handle);
518 [ # # ]: 0 : free0 |= ((uint64_t) !!fabs << 63);
519 : :
520 : : /*
521 : : * COUNT_EOT field is 1-bit wide in cn10k and is 2-bit wide in
522 : : * cn20k. However we can consider it as 2-bit wide for all cases
523 : : * as the upper bit is going to get ignored in cn10k.
524 : : */
525 : 0 : free0 |= ((uint64_t)(num & 0x3) << 32);
526 : :
527 : : /* tar_addr[4:6] is LMTST size-1 in units of 128b */
528 : : tar_addr = addr | ((num >> 1) << 4);
529 : :
530 : 0 : lmt_data[0] = free0;
531 [ # # ]: 0 : for (i = 0; i < num; i++)
532 : 0 : lmt_data[i + 1] = buf[i];
533 : :
534 : : roc_lmt_submit_steorl(lmt_id, tar_addr);
535 : 0 : plt_io_wmb();
536 : 0 : }
537 : :
538 : : static inline void
539 [ # # ]: 0 : roc_npa_aura_batch_free_burst(uint64_t aura_handle, uint64_t const *buf,
540 : : unsigned int num, const int fabs,
541 : : uint64_t lmt_addr, uint64_t lmt_id)
542 : : {
543 : : uint64_t addr, tar_addr, free0, send_data, lmtline;
544 : : uint64_t *lmt_data;
545 : :
546 : : /* 63 52 51 20 19 7 6 4 3 0
547 : : * ----------------------------------------
548 : : * | RSVD | ADDR | RSVD | LMTST SZ(0) | 0 |
549 : : * ----------------------------------------
550 : : */
551 : : addr = roc_npa_aura_handle_to_base(aura_handle) +
552 : : NPA_LF_AURA_BATCH_FREE0;
553 : : tar_addr = addr | (0x7 << 4);
554 : :
555 : : /* 63 63 62 34 33 32 31 20 19 0
556 : : * -----------------------------------------
557 : : * | FABS | Rsvd | COUNT_EOT | Rsvd | AURA |
558 : : * -----------------------------------------
559 : : */
560 : : free0 = roc_npa_aura_handle_to_aura(aura_handle);
561 [ # # ]: 0 : free0 |= ((uint64_t) !!fabs << 63);
562 : : /*
563 : : * COUNT_EOT field is 1-bit wide in cn10k and is 2-bit wide in
564 : : * cn20k. However we can consider it as 2-bit wide for all cases
565 : : * as the upper bit is going to get ignored in cn10k.
566 : : */
567 : 0 : free0 |= (0x3UL << 32);
568 : :
569 : : /* Fill the lmt lines */
570 : 0 : lmt_data = (uint64_t *)lmt_addr;
571 : : lmtline = 0;
572 [ # # ]: 0 : while (num) {
573 : 0 : lmt_data[lmtline * 16] = free0;
574 : 0 : memcpy(&lmt_data[(lmtline * 16) + 1], buf,
575 : : ROC_CN10K_NPA_BATCH_FREE_MAX_PTRS * sizeof(uint64_t));
576 : 0 : lmtline++;
577 : 0 : num -= ROC_CN10K_NPA_BATCH_FREE_MAX_PTRS;
578 : 0 : buf += ROC_CN10K_NPA_BATCH_FREE_MAX_PTRS;
579 : : }
580 : :
581 : : /* 63 19 18 16 15 12 11 11 10 0
582 : : * ---------------------------------------------------------------
583 : : * | LMTST SZ(15) ... LMTST SZ(1) | Rsvd | CNTM1 | Rsvd | LMT_ID |
584 : : * ---------------------------------------------------------------
585 : : */
586 : : send_data = lmt_id | ((lmtline - 1) << 12) | (0x1FFFFFFFFFFFUL << 19);
587 : : roc_lmt_submit_steorl(send_data, tar_addr);
588 : 0 : plt_io_wmb();
589 : 0 : }
590 : :
591 : : static inline void
592 : 0 : roc_npa_aura_op_batch_free(uint64_t aura_handle, uint64_t const *buf,
593 : : unsigned int num, const int fabs, uint64_t lmt_addr,
594 : : uint64_t lmt_id)
595 : : {
596 : : unsigned int max_burst, chunk, bnum;
597 : :
598 : : max_burst = ROC_CN10K_NPA_BATCH_FREE_MAX_PTRS *
599 : : ROC_CN10K_NPA_BATCH_FREE_BURST_MAX;
600 : 0 : bnum = num / ROC_CN10K_NPA_BATCH_FREE_MAX_PTRS;
601 : 0 : bnum *= ROC_CN10K_NPA_BATCH_FREE_MAX_PTRS;
602 : : num -= bnum;
603 : :
604 [ # # ]: 0 : while (bnum) {
605 : 0 : chunk = (bnum >= max_burst) ? max_burst : bnum;
606 : 0 : roc_npa_aura_batch_free_burst(aura_handle, buf, chunk, fabs,
607 : : lmt_addr, lmt_id);
608 : 0 : buf += chunk;
609 : 0 : bnum -= chunk;
610 : : }
611 : :
612 [ # # ]: 0 : if (num)
613 : : roc_npa_aura_batch_free(aura_handle, buf, num, fabs, lmt_addr,
614 : : lmt_id);
615 : 0 : }
616 : :
617 : : static inline unsigned int
618 : : roc_npa_aura_bulk_alloc(uint64_t aura_handle, uint64_t *buf, unsigned int num,
619 : : const int drop)
620 : : {
621 : : #if defined(__aarch64__)
622 : : uint64_t wdata = roc_npa_aura_handle_to_aura(aura_handle);
623 : : unsigned int i, count;
624 : : uint64_t addr;
625 : :
626 : : if (drop)
627 : : wdata |= BIT_ULL(63); /* DROP */
628 : :
629 : : addr = roc_npa_aura_handle_to_base(aura_handle) +
630 : : NPA_LF_AURA_OP_ALLOCX(0);
631 : :
632 : : switch (num) {
633 : : case 30:
634 : : asm volatile(
635 : : ".arch_extension lse\n"
636 : : "mov v18.d[0], %[dst]\n"
637 : : "mov v18.d[1], %[loc]\n"
638 : : "mov v19.d[0], %[wdata]\n"
639 : : "mov v19.d[1], x30\n"
640 : : "mov v20.d[0], x24\n"
641 : : "mov v20.d[1], x25\n"
642 : : "mov v21.d[0], x26\n"
643 : : "mov v21.d[1], x27\n"
644 : : "mov v22.d[0], x28\n"
645 : : "mov v22.d[1], x29\n"
646 : : "mov x28, v19.d[0]\n"
647 : : "mov x29, v19.d[0]\n"
648 : : "mov x30, v18.d[1]\n"
649 : : "casp x0, x1, x28, x29, [x30]\n"
650 : : "casp x2, x3, x28, x29, [x30]\n"
651 : : "casp x4, x5, x28, x29, [x30]\n"
652 : : "casp x6, x7, x28, x29, [x30]\n"
653 : : "casp x8, x9, x28, x29, [x30]\n"
654 : : "casp x10, x11, x28, x29, [x30]\n"
655 : : "casp x12, x13, x28, x29, [x30]\n"
656 : : "casp x14, x15, x28, x29, [x30]\n"
657 : : "casp x16, x17, x28, x29, [x30]\n"
658 : : "casp x18, x19, x28, x29, [x30]\n"
659 : : "casp x20, x21, x28, x29, [x30]\n"
660 : : "casp x22, x23, x28, x29, [x30]\n"
661 : : "casp x24, x25, x28, x29, [x30]\n"
662 : : "casp x26, x27, x28, x29, [x30]\n"
663 : : "casp x28, x29, x28, x29, [x30]\n"
664 : : "mov x30, v18.d[0]\n"
665 : : "stp x0, x1, [x30]\n"
666 : : "stp x2, x3, [x30, #16]\n"
667 : : "stp x4, x5, [x30, #32]\n"
668 : : "stp x6, x7, [x30, #48]\n"
669 : : "stp x8, x9, [x30, #64]\n"
670 : : "stp x10, x11, [x30, #80]\n"
671 : : "stp x12, x13, [x30, #96]\n"
672 : : "stp x14, x15, [x30, #112]\n"
673 : : "stp x16, x17, [x30, #128]\n"
674 : : "stp x18, x19, [x30, #144]\n"
675 : : "stp x20, x21, [x30, #160]\n"
676 : : "stp x22, x23, [x30, #176]\n"
677 : : "stp x24, x25, [x30, #192]\n"
678 : : "stp x26, x27, [x30, #208]\n"
679 : : "stp x28, x29, [x30, #224]\n"
680 : : "mov %[dst], v18.d[0]\n"
681 : : "mov %[loc], v18.d[1]\n"
682 : : "mov %[wdata], v19.d[0]\n"
683 : : "mov x30, v19.d[1]\n"
684 : : "mov x24, v20.d[0]\n"
685 : : "mov x25, v20.d[1]\n"
686 : : "mov x26, v21.d[0]\n"
687 : : "mov x27, v21.d[1]\n"
688 : : "mov x28, v22.d[0]\n"
689 : : "mov x29, v22.d[1]\n"
690 : : :
691 : : : [wdata] "r"(wdata), [loc] "r"(addr), [dst] "r"(buf)
692 : : : "memory", "x0", "x1", "x2", "x3", "x4", "x5", "x6",
693 : : "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14",
694 : : "x15", "x16", "x17", "x18", "x19", "x20", "x21",
695 : : "x22", "x23", "v18", "v19", "v20", "v21", "v22");
696 : : break;
697 : : case 16:
698 : : asm volatile(
699 : : ".arch_extension lse\n"
700 : : "mov x16, %[wdata]\n"
701 : : "mov x17, %[wdata]\n"
702 : : "casp x0, x1, x16, x17, [%[loc]]\n"
703 : : "casp x2, x3, x16, x17, [%[loc]]\n"
704 : : "casp x4, x5, x16, x17, [%[loc]]\n"
705 : : "casp x6, x7, x16, x17, [%[loc]]\n"
706 : : "casp x8, x9, x16, x17, [%[loc]]\n"
707 : : "casp x10, x11, x16, x17, [%[loc]]\n"
708 : : "casp x12, x13, x16, x17, [%[loc]]\n"
709 : : "casp x14, x15, x16, x17, [%[loc]]\n"
710 : : "stp x0, x1, [%[dst]]\n"
711 : : "stp x2, x3, [%[dst], #16]\n"
712 : : "stp x4, x5, [%[dst], #32]\n"
713 : : "stp x6, x7, [%[dst], #48]\n"
714 : : "stp x8, x9, [%[dst], #64]\n"
715 : : "stp x10, x11, [%[dst], #80]\n"
716 : : "stp x12, x13, [%[dst], #96]\n"
717 : : "stp x14, x15, [%[dst], #112]\n"
718 : : :
719 : : : [wdata] "r"(wdata), [dst] "r"(buf), [loc] "r"(addr)
720 : : : "memory", "x0", "x1", "x2", "x3", "x4", "x5", "x6",
721 : : "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14",
722 : : "x15", "x16", "x17");
723 : : break;
724 : : case 8:
725 : : asm volatile(
726 : : ".arch_extension lse\n"
727 : : "mov x16, %[wdata]\n"
728 : : "mov x17, %[wdata]\n"
729 : : "casp x0, x1, x16, x17, [%[loc]]\n"
730 : : "casp x2, x3, x16, x17, [%[loc]]\n"
731 : : "casp x4, x5, x16, x17, [%[loc]]\n"
732 : : "casp x6, x7, x16, x17, [%[loc]]\n"
733 : : "stp x0, x1, [%[dst]]\n"
734 : : "stp x2, x3, [%[dst], #16]\n"
735 : : "stp x4, x5, [%[dst], #32]\n"
736 : : "stp x6, x7, [%[dst], #48]\n"
737 : : :
738 : : : [wdata] "r"(wdata), [dst] "r"(buf), [loc] "r"(addr)
739 : : : "memory", "x0", "x1", "x2", "x3", "x4", "x5", "x6",
740 : : "x7", "x16", "x17");
741 : : break;
742 : : case 4:
743 : : asm volatile(
744 : : ".arch_extension lse\n"
745 : : "mov x16, %[wdata]\n"
746 : : "mov x17, %[wdata]\n"
747 : : "casp x0, x1, x16, x17, [%[loc]]\n"
748 : : "casp x2, x3, x16, x17, [%[loc]]\n"
749 : : "stp x0, x1, [%[dst]]\n"
750 : : "stp x2, x3, [%[dst], #16]\n"
751 : : :
752 : : : [wdata] "r"(wdata), [dst] "r"(buf), [loc] "r"(addr)
753 : : : "memory", "x0", "x1", "x2", "x3", "x16", "x17");
754 : : break;
755 : : case 2:
756 : : asm volatile(
757 : : ".arch_extension lse\n"
758 : : "mov x16, %[wdata]\n"
759 : : "mov x17, %[wdata]\n"
760 : : "casp x0, x1, x16, x17, [%[loc]]\n"
761 : : "stp x0, x1, [%[dst]]\n"
762 : : :
763 : : : [wdata] "r"(wdata), [dst] "r"(buf), [loc] "r"(addr)
764 : : : "memory", "x0", "x1", "x16", "x17");
765 : : break;
766 : : case 1:
767 : : buf[0] = roc_npa_aura_op_alloc(aura_handle, drop);
768 : : return !!buf[0];
769 : : }
770 : :
771 : : /* Pack the pointers */
772 : : for (i = 0, count = 0; i < num; i++)
773 : : if (buf[i])
774 : : buf[count++] = buf[i];
775 : :
776 : : return count;
777 : : #else
778 : : unsigned int i, count;
779 : :
780 [ # # ]: 0 : for (i = 0, count = 0; i < num; i++) {
781 : 0 : buf[count] = roc_npa_aura_op_alloc(aura_handle, drop);
782 : : if (buf[count])
783 : : count++;
784 : : }
785 : :
786 : : return count;
787 : : #endif
788 : : }
789 : :
790 : : static inline unsigned int
791 : : roc_npa_aura_op_bulk_alloc(uint64_t aura_handle, uint64_t *buf,
792 : : unsigned int num, const int drop, const int partial)
793 : : {
794 : : unsigned int chunk, count, num_alloc;
795 : :
796 : : count = 0;
797 [ # # ]: 0 : while (num) {
798 : : chunk = (num >= ROC_CN9K_NPA_BULK_ALLOC_MAX_PTRS) ?
799 [ # # ]: 0 : ROC_CN9K_NPA_BULK_ALLOC_MAX_PTRS :
800 : : plt_align32prevpow2(num);
801 : :
802 : : num_alloc =
803 : : roc_npa_aura_bulk_alloc(aura_handle, buf, chunk, drop);
804 : :
805 : : count += num_alloc;
806 : : buf += num_alloc;
807 : : num -= num_alloc;
808 : :
809 [ # # ]: 0 : if (unlikely(num_alloc != chunk))
810 : : break;
811 : : }
812 : :
813 : : /* If the requested number of pointers was not allocated and if partial
814 : : * alloc is not desired, then free allocated pointers.
815 : : */
816 : : if (unlikely(num != 0 && !partial)) {
817 : : roc_npa_aura_op_bulk_free(aura_handle, buf - count, count, 1);
818 : : count = 0;
819 : : }
820 : :
821 : : return count;
822 : : }
823 : :
824 : : struct roc_npa {
825 : : struct plt_pci_device *pci_dev;
826 : :
827 : : #define ROC_NPA_MEM_SZ (1 * 1024)
828 : : uint8_t reserved[ROC_NPA_MEM_SZ] __plt_cache_aligned;
829 : : } __plt_cache_aligned;
830 : :
831 : : int __roc_api roc_npa_dev_init(struct roc_npa *roc_npa);
832 : : int __roc_api roc_npa_dev_fini(struct roc_npa *roc_npa);
833 : :
834 : : /* Flags to pool create */
835 : : #define ROC_NPA_ZERO_AURA_F BIT(0)
836 : : #define ROC_NPA_FORCE_OPAQUE_MODE_F BIT(1)
837 : : #define ROC_NPA_HALO_F BIT(2)
838 : :
839 : : /* Enumerations */
840 : : enum roc_npa_buf_type {
841 : : /* Aura used for normal pkts */
842 : : ROC_NPA_BUF_TYPE_PACKET = 0,
843 : : /* Aura used for ipsec pkts */
844 : : ROC_NPA_BUF_TYPE_PACKET_IPSEC,
845 : : /* Aura used as vwqe for normal pkts */
846 : : ROC_NPA_BUF_TYPE_VWQE,
847 : : /* Aura used as vwqe for ipsec pkts */
848 : : ROC_NPA_BUF_TYPE_VWQE_IPSEC,
849 : : /* Aura used as SQB for SQ */
850 : : ROC_NPA_BUF_TYPE_SQB,
851 : : /* Aura used for general buffer */
852 : : ROC_NPA_BUF_TYPE_BUF,
853 : : /* Aura used for timeout pool */
854 : : ROC_NPA_BUF_TYPE_TIMEOUT,
855 : : ROC_NPA_BUF_TYPE_END,
856 : : };
857 : :
858 : : /* NPA pool */
859 : : int __roc_api roc_npa_pool_create(uint64_t *aura_handle, uint32_t block_size,
860 : : uint32_t block_count, struct npa_aura_s *aura,
861 : : struct npa_pool_s *pool, uint32_t flags);
862 : : int __roc_api roc_npa_aura_limit_modify(uint64_t aura_handle,
863 : : uint16_t aura_limit);
864 : : int __roc_api roc_npa_pool_destroy(uint64_t aura_handle);
865 : : int __roc_api roc_npa_pool_range_update_check(uint64_t aura_handle);
866 : : void __roc_api roc_npa_aura_op_range_set(uint64_t aura_handle,
867 : : uint64_t start_iova,
868 : : uint64_t end_iova);
869 : : void __roc_api roc_npa_aura_op_range_get(uint64_t aura_handle,
870 : : uint64_t *start_iova,
871 : : uint64_t *end_iova);
872 : : void __roc_api roc_npa_pool_op_range_set(uint64_t aura_handle,
873 : : uint64_t start_iova,
874 : : uint64_t end_iova);
875 : : int __roc_api roc_npa_aura_create(uint64_t *aura_handle, uint32_t block_count,
876 : : struct npa_aura_s *aura, int pool_id,
877 : : uint32_t flags);
878 : : int __roc_api roc_npa_aura_destroy(uint64_t aura_handle);
879 : : uint64_t __roc_api roc_npa_zero_aura_handle(void);
880 : : int __roc_api roc_npa_buf_type_update(uint64_t aura_handle, enum roc_npa_buf_type type, int cnt);
881 : : uint64_t __roc_api roc_npa_buf_type_mask(uint64_t aura_handle);
882 : : uint64_t __roc_api roc_npa_buf_type_limit_get(uint64_t type_mask);
883 : : int __roc_api roc_npa_aura_bp_configure(uint64_t aura_id, uint16_t bpid, uint8_t bp_intf,
884 : : uint8_t bp_thresh, bool enable);
885 : : int __roc_api roc_npa_pool_bp_configure(uint64_t pool_id, uint16_t bpid, uint8_t bp_thresh,
886 : : uint8_t bp_class, bool enable);
887 : :
888 : : /* Init callbacks */
889 : : typedef int (*roc_npa_lf_init_cb_t)(struct plt_pci_device *pci_dev);
890 : : int __roc_api roc_npa_lf_init_cb_register(roc_npa_lf_init_cb_t cb);
891 : :
892 : : /* Utility functions */
893 : : uint16_t __roc_api roc_npa_pf_func_get(void);
894 : :
895 : : /* Debug */
896 : : int __roc_api roc_npa_ctx_dump(void);
897 : : int __roc_api roc_npa_dump(void);
898 : :
899 : : /* Reset operation performance counter. */
900 : : int __roc_api roc_npa_pool_op_pc_reset(uint64_t aura_handle);
901 : :
902 : : int __roc_api roc_npa_aura_drop_set(uint64_t aura_handle, uint64_t limit,
903 : : bool ena);
904 : :
905 : : void __roc_api roc_npa_dev_lock(void);
906 : : void __roc_api roc_npa_dev_unlock(void);
907 : : int __roc_api roc_npa_dpc_alloc(uint8_t *counter_id, uint64_t conf);
908 : : int __roc_api roc_npa_dpc_free(uint8_t counter_id);
909 : : int __roc_api roc_npa_pool_dpc_enable(uint64_t aura_handle, uint8_t counter_id, uint32_t flags);
910 : : int __roc_api roc_npa_pool_dpc_disable(uint64_t aura_handle, uint32_t flags);
911 : :
912 : : #endif /* _ROC_NPA_H_ */
|