summaryrefslogtreecommitdiff
path: root/drivers/mempool/octeontx2/otx2_mempool.c
blob: 3a4a9425f6788f8e47bf6117afaaa9768cd9ed60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(C) 2019 Marvell International Ltd.
 */

#include <rte_atomic.h>
#include <rte_bus_pci.h>
#include <rte_common.h>
#include <rte_eal.h>
#include <rte_io.h>
#include <rte_kvargs.h>
#include <rte_malloc.h>
#include <rte_mbuf_pool_ops.h>
#include <rte_pci.h>

#include "otx2_common.h"
#include "otx2_dev.h"
#include "otx2_mempool.h"

#define OTX2_NPA_DEV_NAME	RTE_STR(otx2_npa_dev_)
#define OTX2_NPA_DEV_NAME_LEN	(sizeof(OTX2_NPA_DEV_NAME) + PCI_PRI_STR_SIZE)

static inline int
npa_lf_alloc(struct otx2_npa_lf *lf)
{
	struct otx2_mbox *mbox = lf->mbox;
	struct npa_lf_alloc_req *req;
	struct npa_lf_alloc_rsp *rsp;
	int rc;

	req = otx2_mbox_alloc_msg_npa_lf_alloc(mbox);
	req->aura_sz = lf->aura_sz;
	req->nr_pools = lf->nr_pools;

	rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
	if (rc)
		return NPA_LF_ERR_ALLOC;

	lf->stack_pg_ptrs = rsp->stack_pg_ptrs;
	lf->stack_pg_bytes = rsp->stack_pg_bytes;
	lf->qints = rsp->qints;

	return 0;
}

static int
npa_lf_free(struct otx2_mbox *mbox)
{
	otx2_mbox_alloc_msg_npa_lf_free(mbox);

	return otx2_mbox_process(mbox);
}

static int
npa_lf_init(struct otx2_npa_lf *lf, uintptr_t base, uint8_t aura_sz,
	    uint32_t nr_pools, struct otx2_mbox *mbox)
{
	uint32_t i, bmp_sz;
	int rc;

	/* Sanity checks */
	if (!lf || !base || !mbox || !nr_pools)
		return NPA_LF_ERR_PARAM;

	if (base & AURA_ID_MASK)
		return NPA_LF_ERR_BASE_INVALID;

	if (aura_sz == NPA_AURA_SZ_0 || aura_sz >= NPA_AURA_SZ_MAX)
		return NPA_LF_ERR_PARAM;

	memset(lf, 0x0, sizeof(*lf));
	lf->base = base;
	lf->aura_sz = aura_sz;
	lf->nr_pools = nr_pools;
	lf->mbox = mbox;

	rc = npa_lf_alloc(lf);
	if (rc)
		goto exit;

	bmp_sz = rte_bitmap_get_memory_footprint(nr_pools);

	/* Allocate memory for bitmap */
	lf->npa_bmp_mem = rte_zmalloc("npa_bmp_mem", bmp_sz,
					RTE_CACHE_LINE_SIZE);
	if (lf->npa_bmp_mem == NULL) {
		rc = -ENOMEM;
		goto lf_free;
	}

	/* Initialize pool resource bitmap array */
	lf->npa_bmp = rte_bitmap_init(nr_pools, lf->npa_bmp_mem, bmp_sz);
	if (lf->npa_bmp == NULL) {
		rc = -EINVAL;
		goto bmap_mem_free;
	}

	/* Mark all pools available */
	for (i = 0; i < nr_pools; i++)
		rte_bitmap_set(lf->npa_bmp, i);

	/* Allocate memory for qint context */
	lf->npa_qint_mem = rte_zmalloc("npa_qint_mem",
			sizeof(struct otx2_npa_qint) * nr_pools, 0);
	if (lf->npa_qint_mem == NULL) {
		rc = -ENOMEM;
		goto bmap_free;
	}

	/* Allocate memory for nap_aura_lim memory */
	lf->aura_lim = rte_zmalloc("npa_aura_lim_mem",
			sizeof(struct npa_aura_lim) * nr_pools, 0);
	if (lf->aura_lim == NULL) {
		rc = -ENOMEM;
		goto qint_free;
	}

	/* Init aura start & end limits */
	for (i = 0; i < nr_pools; i++) {
		lf->aura_lim[i].ptr_start = UINT64_MAX;
		lf->aura_lim[i].ptr_end = 0x0ull;
	}

	return 0;

qint_free:
	rte_free(lf->npa_qint_mem);
bmap_free:
	rte_bitmap_free(lf->npa_bmp);
bmap_mem_free:
	rte_free(lf->npa_bmp_mem);
lf_free:
	npa_lf_free(lf->mbox);
exit:
	return rc;
}

static int
npa_lf_fini(struct otx2_npa_lf *lf)
{
	if (!lf)
		return NPA_LF_ERR_PARAM;

	rte_free(lf->aura_lim);
	rte_free(lf->npa_qint_mem);
	rte_bitmap_free(lf->npa_bmp);
	rte_free(lf->npa_bmp_mem);

	return npa_lf_free(lf->mbox);

}

static inline uint32_t
otx2_aura_size_to_u32(uint8_t val)
{
	if (val == NPA_AURA_SZ_0)
		return 128;
	if (val >= NPA_AURA_SZ_MAX)
		return BIT_ULL(20);

	return 1 << (val + 6);
}

static int
parse_max_pools(const char *key, const char *value, void *extra_args)
{
	RTE_SET_USED(key);
	uint32_t val;

	val = atoi(value);
	if (val < otx2_aura_size_to_u32(NPA_AURA_SZ_128))
		val = 128;
	if (val > otx2_aura_size_to_u32(NPA_AURA_SZ_1M))
		val = BIT_ULL(20);

	*(uint8_t *)extra_args = rte_log2_u32(val) - 6;
	return 0;
}

#define OTX2_MAX_POOLS "max_pools"

static uint8_t
otx2_parse_aura_size(struct rte_devargs *devargs)
{
	uint8_t aura_sz = NPA_AURA_SZ_128;
	struct rte_kvargs *kvlist;

	if (devargs == NULL)
		goto exit;
	kvlist = rte_kvargs_parse(devargs->args, NULL);
	if (kvlist == NULL)
		goto exit;

	rte_kvargs_process(kvlist, OTX2_MAX_POOLS, &parse_max_pools, &aura_sz);
	rte_kvargs_free(kvlist);
exit:
	return aura_sz;
}

static inline int
npa_lf_attach(struct otx2_mbox *mbox)
{
	struct rsrc_attach_req *req;

	req = otx2_mbox_alloc_msg_attach_resources(mbox);
	req->npalf = true;

	return otx2_mbox_process(mbox);
}

static inline int
npa_lf_detach(struct otx2_mbox *mbox)
{
	struct rsrc_detach_req *req;

	req = otx2_mbox_alloc_msg_detach_resources(mbox);
	req->npalf = true;

	return otx2_mbox_process(mbox);
}

static inline int
npa_lf_get_msix_offset(struct otx2_mbox *mbox, uint16_t *npa_msixoff)
{
	struct msix_offset_rsp *msix_rsp;
	int rc;

	/* Get NPA and NIX MSIX vector offsets */
	otx2_mbox_alloc_msg_msix_offset(mbox);

	rc = otx2_mbox_process_msg(mbox, (void *)&msix_rsp);

	*npa_msixoff = msix_rsp->npa_msixoff;

	return rc;
}

/**
 * @internal
 * Finalize NPA LF.
 */
int
otx2_npa_lf_fini(void)
{
	struct otx2_idev_cfg *idev;
	int rc = 0;

	idev = otx2_intra_dev_get_cfg();
	if (idev == NULL)
		return -ENOMEM;

	if (rte_atomic16_add_return(&idev->npa_refcnt, -1) == 0) {
		otx2_npa_unregister_irqs(idev->npa_lf);
		rc |= npa_lf_fini(idev->npa_lf);
		rc |= npa_lf_detach(idev->npa_lf->mbox);
		otx2_npa_set_defaults(idev);
	}

	return rc;
}

/**
 * @internal
 * Initialize NPA LF.
 */
int
otx2_npa_lf_init(struct rte_pci_device *pci_dev, void *otx2_dev)
{
	struct otx2_dev *dev = otx2_dev;
	struct otx2_idev_cfg *idev;
	struct otx2_npa_lf *lf;
	uint16_t npa_msixoff;
	uint32_t nr_pools;
	uint8_t aura_sz;
	int rc;

	idev = otx2_intra_dev_get_cfg();
	if (idev == NULL)
		return -ENOMEM;

	/* Is NPA LF initialized by any another driver? */
	if (rte_atomic16_add_return(&idev->npa_refcnt, 1) == 1) {

		rc = npa_lf_attach(dev->mbox);
		if (rc)
			goto fail;

		rc = npa_lf_get_msix_offset(dev->mbox, &npa_msixoff);
		if (rc)
			goto npa_detach;

		aura_sz = otx2_parse_aura_size(pci_dev->device.devargs);
		nr_pools = otx2_aura_size_to_u32(aura_sz);

		lf = &dev->npalf;
		rc = npa_lf_init(lf, dev->bar2 + (RVU_BLOCK_ADDR_NPA << 20),
					aura_sz, nr_pools, dev->mbox);

		if (rc)
			goto npa_detach;

		lf->pf_func = dev->pf_func;
		lf->npa_msixoff = npa_msixoff;
		lf->intr_handle = &pci_dev->intr_handle;
		lf->pci_dev = pci_dev;

		idev->npa_pf_func = dev->pf_func;
		idev->npa_lf = lf;
		rte_smp_wmb();
		rc = otx2_npa_register_irqs(lf);
		if (rc)
			goto npa_fini;

		rte_mbuf_set_platform_mempool_ops("octeontx2_npa");
		otx2_npa_dbg("npa_lf=%p pools=%d sz=%d pf_func=0x%x msix=0x%x",
			     lf, nr_pools, aura_sz, lf->pf_func, npa_msixoff);
	}

	return 0;

npa_fini:
	npa_lf_fini(idev->npa_lf);
npa_detach:
	npa_lf_detach(dev->mbox);
fail:
	rte_atomic16_dec(&idev->npa_refcnt);
	return rc;
}

static inline char*
otx2_npa_dev_to_name(struct rte_pci_device *pci_dev, char *name)
{
	snprintf(name, OTX2_NPA_DEV_NAME_LEN,
		 OTX2_NPA_DEV_NAME  PCI_PRI_FMT,
		 pci_dev->addr.domain, pci_dev->addr.bus,
		 pci_dev->addr.devid, pci_dev->addr.function);

	return name;
}

static int
otx2_npa_init(struct rte_pci_device *pci_dev)
{
	char name[OTX2_NPA_DEV_NAME_LEN];
	const struct rte_memzone *mz;
	struct otx2_dev *dev;
	int rc = -ENOMEM;

	mz = rte_memzone_reserve_aligned(otx2_npa_dev_to_name(pci_dev, name),
					 sizeof(*dev), SOCKET_ID_ANY,
					 0, OTX2_ALIGN);
	if (mz == NULL)
		goto error;

	dev = mz->addr;

	/* Initialize the base otx2_dev object */
	rc = otx2_dev_init(pci_dev, dev);
	if (rc)
		goto malloc_fail;

	/* Grab the NPA LF if required */
	rc = otx2_npa_lf_init(pci_dev, dev);
	if (rc)
		goto dev_uninit;

	dev->drv_inited = true;
	return 0;

dev_uninit:
	otx2_npa_lf_fini();
	otx2_dev_fini(pci_dev, dev);
malloc_fail:
	rte_memzone_free(mz);
error:
	otx2_err("Failed to initialize npa device rc=%d", rc);
	return rc;
}

static int
otx2_npa_fini(struct rte_pci_device *pci_dev)
{
	char name[OTX2_NPA_DEV_NAME_LEN];
	const struct rte_memzone *mz;
	struct otx2_dev *dev;

	mz = rte_memzone_lookup(otx2_npa_dev_to_name(pci_dev, name));
	if (mz == NULL)
		return -EINVAL;

	dev = mz->addr;
	if (!dev->drv_inited)
		goto dev_fini;

	dev->drv_inited = false;
	otx2_npa_lf_fini();

dev_fini:
	if (otx2_npa_lf_active(dev)) {
		otx2_info("%s: common resource in use by other devices",
			  pci_dev->name);
		return -EAGAIN;
	}

	otx2_dev_fini(pci_dev, dev);
	rte_memzone_free(mz);

	return 0;
}

static int
npa_remove(struct rte_pci_device *pci_dev)
{
	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
		return 0;

	return otx2_npa_fini(pci_dev);
}

static int
npa_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
{
	RTE_SET_USED(pci_drv);

	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
		return 0;

	return otx2_npa_init(pci_dev);
}

static const struct rte_pci_id pci_npa_map[] = {
	{
		RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
					PCI_DEVID_OCTEONTX2_RVU_NPA_PF)
	},
	{
		RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
					PCI_DEVID_OCTEONTX2_RVU_NPA_VF)
	},
	{
		.vendor_id = 0,
	},
};

static struct rte_pci_driver pci_npa = {
	.id_table = pci_npa_map,
	.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_NEED_IOVA_AS_VA,
	.probe = npa_probe,
	.remove = npa_remove,
};

RTE_PMD_REGISTER_PCI(mempool_octeontx2, pci_npa);
RTE_PMD_REGISTER_PCI_TABLE(mempool_octeontx2, pci_npa_map);
RTE_PMD_REGISTER_KMOD_DEP(mempool_octeontx2, "vfio-pci");
RTE_PMD_REGISTER_PARAM_STRING(mempool_octeontx2,
			      OTX2_MAX_POOLS "=<128-1048576>");