fa74b753237c1c4c02ca10fef1f98f470b84f6b0
[dpdk.git] / drivers / mempool / octeontx2 / otx2_mempool.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #include <rte_atomic.h>
6 #include <rte_bus_pci.h>
7 #include <rte_common.h>
8 #include <rte_eal.h>
9 #include <rte_io.h>
10 #include <rte_malloc.h>
11 #include <rte_mbuf_pool_ops.h>
12 #include <rte_pci.h>
13
14 #include "otx2_common.h"
15 #include "otx2_dev.h"
16 #include "otx2_mempool.h"
17
18 #define OTX2_NPA_DEV_NAME       RTE_STR(otx2_npa_dev_)
19 #define OTX2_NPA_DEV_NAME_LEN   (sizeof(OTX2_NPA_DEV_NAME) + PCI_PRI_STR_SIZE)
20
21 static inline int
22 npa_lf_alloc(struct otx2_npa_lf *lf)
23 {
24         struct otx2_mbox *mbox = lf->mbox;
25         struct npa_lf_alloc_req *req;
26         struct npa_lf_alloc_rsp *rsp;
27         int rc;
28
29         req = otx2_mbox_alloc_msg_npa_lf_alloc(mbox);
30         req->aura_sz = lf->aura_sz;
31         req->nr_pools = lf->nr_pools;
32
33         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
34         if (rc)
35                 return NPA_LF_ERR_ALLOC;
36
37         lf->stack_pg_ptrs = rsp->stack_pg_ptrs;
38         lf->stack_pg_bytes = rsp->stack_pg_bytes;
39         lf->qints = rsp->qints;
40
41         return 0;
42 }
43
44 static int
45 npa_lf_free(struct otx2_mbox *mbox)
46 {
47         otx2_mbox_alloc_msg_npa_lf_free(mbox);
48
49         return otx2_mbox_process(mbox);
50 }
51
52 static int
53 npa_lf_init(struct otx2_npa_lf *lf, uintptr_t base, uint8_t aura_sz,
54             uint32_t nr_pools, struct otx2_mbox *mbox)
55 {
56         uint32_t i, bmp_sz;
57         int rc;
58
59         /* Sanity checks */
60         if (!lf || !base || !mbox || !nr_pools)
61                 return NPA_LF_ERR_PARAM;
62
63         if (base & AURA_ID_MASK)
64                 return NPA_LF_ERR_BASE_INVALID;
65
66         if (aura_sz == NPA_AURA_SZ_0 || aura_sz >= NPA_AURA_SZ_MAX)
67                 return NPA_LF_ERR_PARAM;
68
69         memset(lf, 0x0, sizeof(*lf));
70         lf->base = base;
71         lf->aura_sz = aura_sz;
72         lf->nr_pools = nr_pools;
73         lf->mbox = mbox;
74
75         rc = npa_lf_alloc(lf);
76         if (rc)
77                 goto exit;
78
79         bmp_sz = rte_bitmap_get_memory_footprint(nr_pools);
80
81         /* Allocate memory for bitmap */
82         lf->npa_bmp_mem = rte_zmalloc("npa_bmp_mem", bmp_sz,
83                                         RTE_CACHE_LINE_SIZE);
84         if (lf->npa_bmp_mem == NULL) {
85                 rc = -ENOMEM;
86                 goto lf_free;
87         }
88
89         /* Initialize pool resource bitmap array */
90         lf->npa_bmp = rte_bitmap_init(nr_pools, lf->npa_bmp_mem, bmp_sz);
91         if (lf->npa_bmp == NULL) {
92                 rc = -EINVAL;
93                 goto bmap_mem_free;
94         }
95
96         /* Mark all pools available */
97         for (i = 0; i < nr_pools; i++)
98                 rte_bitmap_set(lf->npa_bmp, i);
99
100         /* Allocate memory for qint context */
101         lf->npa_qint_mem = rte_zmalloc("npa_qint_mem",
102                         sizeof(struct otx2_npa_qint) * nr_pools, 0);
103         if (lf->npa_qint_mem == NULL) {
104                 rc = -ENOMEM;
105                 goto bmap_free;
106         }
107
108         return 0;
109
110 bmap_free:
111         rte_bitmap_free(lf->npa_bmp);
112 bmap_mem_free:
113         rte_free(lf->npa_bmp_mem);
114 lf_free:
115         npa_lf_free(lf->mbox);
116 exit:
117         return rc;
118 }
119
120 static int
121 npa_lf_fini(struct otx2_npa_lf *lf)
122 {
123         if (!lf)
124                 return NPA_LF_ERR_PARAM;
125
126         rte_free(lf->npa_qint_mem);
127         rte_bitmap_free(lf->npa_bmp);
128         rte_free(lf->npa_bmp_mem);
129
130         return npa_lf_free(lf->mbox);
131
132 }
133
134 static inline uint32_t
135 otx2_aura_size_to_u32(uint8_t val)
136 {
137         if (val == NPA_AURA_SZ_0)
138                 return 128;
139         if (val >= NPA_AURA_SZ_MAX)
140                 return BIT_ULL(20);
141
142         return 1 << (val + 6);
143 }
144
145 static inline int
146 npa_lf_attach(struct otx2_mbox *mbox)
147 {
148         struct rsrc_attach_req *req;
149
150         req = otx2_mbox_alloc_msg_attach_resources(mbox);
151         req->npalf = true;
152
153         return otx2_mbox_process(mbox);
154 }
155
156 static inline int
157 npa_lf_detach(struct otx2_mbox *mbox)
158 {
159         struct rsrc_detach_req *req;
160
161         req = otx2_mbox_alloc_msg_detach_resources(mbox);
162         req->npalf = true;
163
164         return otx2_mbox_process(mbox);
165 }
166
167 static inline int
168 npa_lf_get_msix_offset(struct otx2_mbox *mbox, uint16_t *npa_msixoff)
169 {
170         struct msix_offset_rsp *msix_rsp;
171         int rc;
172
173         /* Get NPA and NIX MSIX vector offsets */
174         otx2_mbox_alloc_msg_msix_offset(mbox);
175
176         rc = otx2_mbox_process_msg(mbox, (void *)&msix_rsp);
177
178         *npa_msixoff = msix_rsp->npa_msixoff;
179
180         return rc;
181 }
182
183 /**
184  * @internal
185  * Finalize NPA LF.
186  */
187 int
188 otx2_npa_lf_fini(void)
189 {
190         struct otx2_idev_cfg *idev;
191         int rc = 0;
192
193         idev = otx2_intra_dev_get_cfg();
194         if (idev == NULL)
195                 return -ENOMEM;
196
197         if (rte_atomic16_add_return(&idev->npa_refcnt, -1) == 0) {
198                 rc |= npa_lf_fini(idev->npa_lf);
199                 rc |= npa_lf_detach(idev->npa_lf->mbox);
200                 otx2_npa_set_defaults(idev);
201         }
202
203         return rc;
204 }
205
206 /**
207  * @internal
208  * Initialize NPA LF.
209  */
210 int
211 otx2_npa_lf_init(struct rte_pci_device *pci_dev, void *otx2_dev)
212 {
213         struct otx2_dev *dev = otx2_dev;
214         struct otx2_idev_cfg *idev;
215         struct otx2_npa_lf *lf;
216         uint16_t npa_msixoff;
217         uint32_t nr_pools;
218         uint8_t aura_sz;
219         int rc;
220
221         idev = otx2_intra_dev_get_cfg();
222         if (idev == NULL)
223                 return -ENOMEM;
224
225         /* Is NPA LF initialized by any another driver? */
226         if (rte_atomic16_add_return(&idev->npa_refcnt, 1) == 1) {
227
228                 rc = npa_lf_attach(dev->mbox);
229                 if (rc)
230                         goto fail;
231
232                 rc = npa_lf_get_msix_offset(dev->mbox, &npa_msixoff);
233                 if (rc)
234                         goto npa_detach;
235
236                 aura_sz = NPA_AURA_SZ_128;
237                 nr_pools = otx2_aura_size_to_u32(aura_sz);
238
239                 lf = &dev->npalf;
240                 rc = npa_lf_init(lf, dev->bar2 + (RVU_BLOCK_ADDR_NPA << 20),
241                                         aura_sz, nr_pools, dev->mbox);
242
243                 if (rc)
244                         goto npa_detach;
245
246                 lf->pf_func = dev->pf_func;
247                 lf->npa_msixoff = npa_msixoff;
248                 lf->intr_handle = &pci_dev->intr_handle;
249                 lf->pci_dev = pci_dev;
250
251                 idev->npa_pf_func = dev->pf_func;
252                 idev->npa_lf = lf;
253                 rte_smp_wmb();
254
255                 rte_mbuf_set_platform_mempool_ops("octeontx2_npa");
256                 otx2_npa_dbg("npa_lf=%p pools=%d sz=%d pf_func=0x%x msix=0x%x",
257                              lf, nr_pools, aura_sz, lf->pf_func, npa_msixoff);
258         }
259
260         return 0;
261
262 npa_detach:
263         npa_lf_detach(dev->mbox);
264 fail:
265         rte_atomic16_dec(&idev->npa_refcnt);
266         return rc;
267 }
268
269 static inline char*
270 otx2_npa_dev_to_name(struct rte_pci_device *pci_dev, char *name)
271 {
272         snprintf(name, OTX2_NPA_DEV_NAME_LEN,
273                  OTX2_NPA_DEV_NAME  PCI_PRI_FMT,
274                  pci_dev->addr.domain, pci_dev->addr.bus,
275                  pci_dev->addr.devid, pci_dev->addr.function);
276
277         return name;
278 }
279
280 static int
281 otx2_npa_init(struct rte_pci_device *pci_dev)
282 {
283         char name[OTX2_NPA_DEV_NAME_LEN];
284         const struct rte_memzone *mz;
285         struct otx2_dev *dev;
286         int rc = -ENOMEM;
287
288         mz = rte_memzone_reserve_aligned(otx2_npa_dev_to_name(pci_dev, name),
289                                          sizeof(*dev), SOCKET_ID_ANY,
290                                          0, OTX2_ALIGN);
291         if (mz == NULL)
292                 goto error;
293
294         dev = mz->addr;
295
296         /* Initialize the base otx2_dev object */
297         rc = otx2_dev_init(pci_dev, dev);
298         if (rc)
299                 goto malloc_fail;
300
301         /* Grab the NPA LF if required */
302         rc = otx2_npa_lf_init(pci_dev, dev);
303         if (rc)
304                 goto dev_uninit;
305
306         dev->drv_inited = true;
307         return 0;
308
309 dev_uninit:
310         otx2_npa_lf_fini();
311         otx2_dev_fini(pci_dev, dev);
312 malloc_fail:
313         rte_memzone_free(mz);
314 error:
315         otx2_err("Failed to initialize npa device rc=%d", rc);
316         return rc;
317 }
318
319 static int
320 otx2_npa_fini(struct rte_pci_device *pci_dev)
321 {
322         char name[OTX2_NPA_DEV_NAME_LEN];
323         const struct rte_memzone *mz;
324         struct otx2_dev *dev;
325
326         mz = rte_memzone_lookup(otx2_npa_dev_to_name(pci_dev, name));
327         if (mz == NULL)
328                 return -EINVAL;
329
330         dev = mz->addr;
331         if (!dev->drv_inited)
332                 goto dev_fini;
333
334         dev->drv_inited = false;
335         otx2_npa_lf_fini();
336
337 dev_fini:
338         if (otx2_npa_lf_active(dev)) {
339                 otx2_info("%s: common resource in use by other devices",
340                           pci_dev->name);
341                 return -EAGAIN;
342         }
343
344         otx2_dev_fini(pci_dev, dev);
345         rte_memzone_free(mz);
346
347         return 0;
348 }
349
350 static int
351 npa_remove(struct rte_pci_device *pci_dev)
352 {
353         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
354                 return 0;
355
356         return otx2_npa_fini(pci_dev);
357 }
358
359 static int
360 npa_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
361 {
362         RTE_SET_USED(pci_drv);
363
364         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
365                 return 0;
366
367         return otx2_npa_init(pci_dev);
368 }
369
370 static const struct rte_pci_id pci_npa_map[] = {
371         {
372                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
373                                         PCI_DEVID_OCTEONTX2_RVU_NPA_PF)
374         },
375         {
376                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
377                                         PCI_DEVID_OCTEONTX2_RVU_NPA_VF)
378         },
379         {
380                 .vendor_id = 0,
381         },
382 };
383
384 static struct rte_pci_driver pci_npa = {
385         .id_table = pci_npa_map,
386         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_IOVA_AS_VA,
387         .probe = npa_probe,
388         .remove = npa_remove,
389 };
390
391 RTE_PMD_REGISTER_PCI(mempool_octeontx2, pci_npa);
392 RTE_PMD_REGISTER_PCI_TABLE(mempool_octeontx2, pci_npa_map);
393 RTE_PMD_REGISTER_KMOD_DEP(mempool_octeontx2, "vfio-pci");