mempool/octeontx2: add NPA HW operations
[dpdk.git] / drivers / mempool / octeontx2 / otx2_mempool.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #ifndef __OTX2_MEMPOOL_H__
6 #define __OTX2_MEMPOOL_H__
7
8 #include <rte_bitmap.h>
9 #include <rte_bus_pci.h>
10 #include <rte_devargs.h>
11 #include <rte_mempool.h>
12
13 #include "otx2_common.h"
14 #include "otx2_mbox.h"
15
16 enum npa_lf_status {
17         NPA_LF_ERR_PARAM            = -512,
18         NPA_LF_ERR_ALLOC            = -513,
19         NPA_LF_ERR_INVALID_BLOCK_SZ = -514,
20         NPA_LF_ERR_AURA_ID_ALLOC    = -515,
21         NPA_LF_ERR_AURA_POOL_INIT   = -516,
22         NPA_LF_ERR_AURA_POOL_FINI   = -517,
23         NPA_LF_ERR_BASE_INVALID     = -518,
24 };
25
26 struct otx2_npa_lf;
27 struct otx2_npa_qint {
28         struct otx2_npa_lf *lf;
29         uint8_t qintx;
30 };
31
32 struct otx2_npa_lf {
33         uint16_t qints;
34         uintptr_t base;
35         uint8_t aura_sz;
36         uint16_t pf_func;
37         uint32_t nr_pools;
38         void *npa_bmp_mem;
39         void *npa_qint_mem;
40         uint16_t npa_msixoff;
41         struct otx2_mbox *mbox;
42         uint32_t stack_pg_ptrs;
43         uint32_t stack_pg_bytes;
44         struct rte_bitmap *npa_bmp;
45         struct rte_pci_device *pci_dev;
46         struct rte_intr_handle *intr_handle;
47 };
48
49 #define AURA_ID_MASK  (BIT_ULL(16) - 1)
50
51 /*
52  * Generate 64bit handle to have optimized alloc and free aura operation.
53  * 0 - AURA_ID_MASK for storing the aura_id.
54  * AURA_ID_MASK+1 - (2^64 - 1) for storing the lf base address.
55  * This scheme is valid when OS can give AURA_ID_MASK
56  * aligned address for lf base address.
57  */
58 static inline uint64_t
59 npa_lf_aura_handle_gen(uint32_t aura_id, uintptr_t addr)
60 {
61         uint64_t val;
62
63         val = aura_id & AURA_ID_MASK;
64         return (uint64_t)addr | val;
65 }
66
67 static inline uint64_t
68 npa_lf_aura_handle_to_aura(uint64_t aura_handle)
69 {
70         return aura_handle & AURA_ID_MASK;
71 }
72
73 static inline uintptr_t
74 npa_lf_aura_handle_to_base(uint64_t aura_handle)
75 {
76         return (uintptr_t)(aura_handle & ~AURA_ID_MASK);
77 }
78
79 static inline uint64_t
80 npa_lf_aura_op_alloc(uint64_t aura_handle, const int drop)
81 {
82         uint64_t wdata = npa_lf_aura_handle_to_aura(aura_handle);
83
84         if (drop)
85                 wdata |= BIT_ULL(63); /* DROP */
86
87         return otx2_atomic64_add_nosync(wdata,
88                 (int64_t *)(npa_lf_aura_handle_to_base(aura_handle) +
89                 NPA_LF_AURA_OP_ALLOCX(0)));
90 }
91
92 static inline void
93 npa_lf_aura_op_free(uint64_t aura_handle, const int fabs, uint64_t iova)
94 {
95         uint64_t reg = npa_lf_aura_handle_to_aura(aura_handle);
96
97         if (fabs)
98                 reg |= BIT_ULL(63); /* FABS */
99
100         otx2_store_pair(iova, reg,
101                 npa_lf_aura_handle_to_base(aura_handle) + NPA_LF_AURA_OP_FREE0);
102 }
103
104 static inline uint64_t
105 npa_lf_aura_op_cnt_get(uint64_t aura_handle)
106 {
107         uint64_t wdata;
108         uint64_t reg;
109
110         wdata = npa_lf_aura_handle_to_aura(aura_handle) << 44;
111
112         reg = otx2_atomic64_add_nosync(wdata,
113                         (int64_t *)(npa_lf_aura_handle_to_base(aura_handle) +
114                          NPA_LF_AURA_OP_CNT));
115
116         if (reg & BIT_ULL(42) /* OP_ERR */)
117                 return 0;
118         else
119                 return reg & 0xFFFFFFFFF;
120 }
121
122 static inline void
123 npa_lf_aura_op_cnt_set(uint64_t aura_handle, const int sign, uint64_t count)
124 {
125         uint64_t reg = count & (BIT_ULL(36) - 1);
126
127         if (sign)
128                 reg |= BIT_ULL(43); /* CNT_ADD */
129
130         reg |= (npa_lf_aura_handle_to_aura(aura_handle) << 44);
131
132         otx2_write64(reg,
133                 npa_lf_aura_handle_to_base(aura_handle) + NPA_LF_AURA_OP_CNT);
134 }
135
136 static inline uint64_t
137 npa_lf_aura_op_limit_get(uint64_t aura_handle)
138 {
139         uint64_t wdata;
140         uint64_t reg;
141
142         wdata = npa_lf_aura_handle_to_aura(aura_handle) << 44;
143
144         reg = otx2_atomic64_add_nosync(wdata,
145                         (int64_t *)(npa_lf_aura_handle_to_base(aura_handle) +
146                          NPA_LF_AURA_OP_LIMIT));
147
148         if (reg & BIT_ULL(42) /* OP_ERR */)
149                 return 0;
150         else
151                 return reg & 0xFFFFFFFFF;
152 }
153
154 static inline void
155 npa_lf_aura_op_limit_set(uint64_t aura_handle, uint64_t limit)
156 {
157         uint64_t reg = limit & (BIT_ULL(36) - 1);
158
159         reg |= (npa_lf_aura_handle_to_aura(aura_handle) << 44);
160
161         otx2_write64(reg,
162                 npa_lf_aura_handle_to_base(aura_handle) + NPA_LF_AURA_OP_LIMIT);
163 }
164
165 static inline uint64_t
166 npa_lf_aura_op_available(uint64_t aura_handle)
167 {
168         uint64_t wdata;
169         uint64_t reg;
170
171         wdata = npa_lf_aura_handle_to_aura(aura_handle) << 44;
172
173         reg = otx2_atomic64_add_nosync(wdata,
174                             (int64_t *)(npa_lf_aura_handle_to_base(
175                              aura_handle) + NPA_LF_POOL_OP_AVAILABLE));
176
177         if (reg & BIT_ULL(42) /* OP_ERR */)
178                 return 0;
179         else
180                 return reg & 0xFFFFFFFFF;
181 }
182
183 static inline void
184 npa_lf_aura_op_range_set(uint64_t aura_handle, uint64_t start_iova,
185                                 uint64_t end_iova)
186 {
187         uint64_t reg = npa_lf_aura_handle_to_aura(aura_handle);
188
189         otx2_store_pair(start_iova, reg,
190                         npa_lf_aura_handle_to_base(aura_handle) +
191                         NPA_LF_POOL_OP_PTR_START0);
192         otx2_store_pair(end_iova, reg,
193                         npa_lf_aura_handle_to_base(aura_handle) +
194                         NPA_LF_POOL_OP_PTR_END0);
195 }
196
197 /* NPA LF */
198 int otx2_npa_lf_init(struct rte_pci_device *pci_dev, void *otx2_dev);
199 int otx2_npa_lf_fini(void);
200
201 #endif /* __OTX2_MEMPOOL_H__ */