common/cnxk: add NPA pool HW operations
[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 /*
12  * Generate 64bit handle to have optimized alloc and free aura operation.
13  * 0 - ROC_AURA_ID_MASK for storing the aura_id.
14  * [ROC_AURA_ID_MASK+1, (2^64 - 1)] for storing the lf base address.
15  * This scheme is valid when OS can give ROC_AURA_ID_MASK
16  * aligned address for lf base address.
17  */
18 static inline uint64_t
19 roc_npa_aura_handle_gen(uint32_t aura_id, uintptr_t addr)
20 {
21         uint64_t val;
22
23         val = aura_id & ROC_AURA_ID_MASK;
24         return (uint64_t)addr | val;
25 }
26
27 static inline uint64_t
28 roc_npa_aura_handle_to_aura(uint64_t aura_handle)
29 {
30         return aura_handle & ROC_AURA_ID_MASK;
31 }
32
33 static inline uintptr_t
34 roc_npa_aura_handle_to_base(uint64_t aura_handle)
35 {
36         return (uintptr_t)(aura_handle & ~ROC_AURA_ID_MASK);
37 }
38
39 static inline uint64_t
40 roc_npa_aura_op_alloc(uint64_t aura_handle, const int drop)
41 {
42         uint64_t wdata = roc_npa_aura_handle_to_aura(aura_handle);
43         int64_t *addr;
44
45         if (drop)
46                 wdata |= BIT_ULL(63); /* DROP */
47
48         addr = (int64_t *)(roc_npa_aura_handle_to_base(aura_handle) +
49                            NPA_LF_AURA_OP_ALLOCX(0));
50         return roc_atomic64_add_nosync(wdata, addr);
51 }
52
53 static inline void
54 roc_npa_aura_op_free(uint64_t aura_handle, const int fabs, uint64_t iova)
55 {
56         uint64_t reg = roc_npa_aura_handle_to_aura(aura_handle);
57         const uint64_t addr =
58                 roc_npa_aura_handle_to_base(aura_handle) + NPA_LF_AURA_OP_FREE0;
59         if (fabs)
60                 reg |= BIT_ULL(63); /* FABS */
61
62         roc_store_pair(iova, reg, addr);
63 }
64
65 static inline uint64_t
66 roc_npa_aura_op_cnt_get(uint64_t aura_handle)
67 {
68         uint64_t wdata;
69         int64_t *addr;
70         uint64_t reg;
71
72         wdata = roc_npa_aura_handle_to_aura(aura_handle) << 44;
73         addr = (int64_t *)(roc_npa_aura_handle_to_base(aura_handle) +
74                            NPA_LF_AURA_OP_CNT);
75         reg = roc_atomic64_add_nosync(wdata, addr);
76
77         if (reg & BIT_ULL(42) /* OP_ERR */)
78                 return 0;
79         else
80                 return reg & 0xFFFFFFFFF;
81 }
82
83 static inline void
84 roc_npa_aura_op_cnt_set(uint64_t aura_handle, const int sign, uint64_t count)
85 {
86         uint64_t reg = count & (BIT_ULL(36) - 1);
87
88         if (sign)
89                 reg |= BIT_ULL(43); /* CNT_ADD */
90
91         reg |= (roc_npa_aura_handle_to_aura(aura_handle) << 44);
92
93         plt_write64(reg, roc_npa_aura_handle_to_base(aura_handle) +
94                                  NPA_LF_AURA_OP_CNT);
95 }
96
97 static inline uint64_t
98 roc_npa_aura_op_limit_get(uint64_t aura_handle)
99 {
100         uint64_t wdata;
101         int64_t *addr;
102         uint64_t reg;
103
104         wdata = roc_npa_aura_handle_to_aura(aura_handle) << 44;
105         addr = (int64_t *)(roc_npa_aura_handle_to_base(aura_handle) +
106                            NPA_LF_AURA_OP_LIMIT);
107         reg = roc_atomic64_add_nosync(wdata, addr);
108
109         if (reg & BIT_ULL(42) /* OP_ERR */)
110                 return 0;
111         else
112                 return reg & ROC_AURA_OP_LIMIT_MASK;
113 }
114
115 static inline void
116 roc_npa_aura_op_limit_set(uint64_t aura_handle, uint64_t limit)
117 {
118         uint64_t reg = limit & ROC_AURA_OP_LIMIT_MASK;
119
120         reg |= (roc_npa_aura_handle_to_aura(aura_handle) << 44);
121
122         plt_write64(reg, roc_npa_aura_handle_to_base(aura_handle) +
123                                  NPA_LF_AURA_OP_LIMIT);
124 }
125
126 static inline uint64_t
127 roc_npa_aura_op_available(uint64_t aura_handle)
128 {
129         uint64_t wdata;
130         uint64_t reg;
131         int64_t *addr;
132
133         wdata = roc_npa_aura_handle_to_aura(aura_handle) << 44;
134         addr = (int64_t *)(roc_npa_aura_handle_to_base(aura_handle) +
135                            NPA_LF_POOL_OP_AVAILABLE);
136         reg = roc_atomic64_add_nosync(wdata, addr);
137
138         if (reg & BIT_ULL(42) /* OP_ERR */)
139                 return 0;
140         else
141                 return reg & 0xFFFFFFFFF;
142 }
143
144 struct roc_npa {
145         struct plt_pci_device *pci_dev;
146
147 #define ROC_NPA_MEM_SZ (1 * 1024)
148         uint8_t reserved[ROC_NPA_MEM_SZ] __plt_cache_aligned;
149 } __plt_cache_aligned;
150
151 int __roc_api roc_npa_dev_init(struct roc_npa *roc_npa);
152 int __roc_api roc_npa_dev_fini(struct roc_npa *roc_npa);
153
154 /* NPA pool */
155 int __roc_api roc_npa_pool_create(uint64_t *aura_handle, uint32_t block_size,
156                                   uint32_t block_count, struct npa_aura_s *aura,
157                                   struct npa_pool_s *pool);
158 int __roc_api roc_npa_aura_limit_modify(uint64_t aura_handle,
159                                         uint16_t aura_limit);
160 int __roc_api roc_npa_pool_destroy(uint64_t aura_handle);
161 int __roc_api roc_npa_pool_range_update_check(uint64_t aura_handle);
162 void __roc_api roc_npa_aura_op_range_set(uint64_t aura_handle,
163                                          uint64_t start_iova,
164                                          uint64_t end_iova);
165
166 /* Debug */
167 int __roc_api roc_npa_ctx_dump(void);
168 int __roc_api roc_npa_dump(void);
169
170 #endif /* _ROC_NPA_H_ */