~funderscore blog cgit wiki get in touch
aboutsummaryrefslogtreecommitdiff
blob: 4d8173f5ba7c7de24ea7979de26123c70494a792 (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
// SPDX-License-Identifier:    GPL-2.0
/*
 * Copyright (C) 2018 Marvell International Ltd.
 */

#include <dm.h>
#include <errno.h>
#include <malloc.h>
#include <misc.h>
#include <net.h>
#include <asm/io.h>

#include "rvu.h"

int qmem_alloc(struct qmem *q, u32 qsize, size_t entry_sz)
{
	q->base = memalign(CONFIG_SYS_CACHELINE_SIZE, qsize * entry_sz);
	if (!q->base)
		return -ENOMEM;
	q->entry_sz = entry_sz;
	q->qsize = qsize;
	q->alloc_sz = (size_t)qsize * entry_sz;
	q->iova = (dma_addr_t)(q->base);
	debug("NIX: qmem alloc for (%d * %d = %ld bytes) at %p\n",
	      q->qsize, q->entry_sz, q->alloc_sz, q->base);
	return 0;
}

void qmem_free(struct qmem *q)
{
	if (q->base)
		free(q->base);
	memset(q, 0, sizeof(*q));
}

/**
 * Allocates an admin queue for instructions and results
 *
 * @param	aq	admin queue to allocate for
 * @param	qsize	Number of entries in the queue
 * @param	inst_size	Size of each instruction
 * @param	res_size	Size of each result
 *
 * Return:	-ENOMEM on error, 0 on success
 */
int rvu_aq_alloc(struct admin_queue *aq, unsigned int qsize,
		 size_t inst_size, size_t res_size)
{
	int err;

	err = qmem_alloc(&aq->inst, qsize, inst_size);
	if (err)
		return err;
	err = qmem_alloc(&aq->res, qsize, res_size);
	if (err)
		qmem_free(&aq->inst);

	return err;
}

/**
 * Frees an admin queue
 *
 * @param	aq	Admin queue to free
 */
void rvu_aq_free(struct admin_queue *aq)
{
	qmem_free(&aq->inst);
	qmem_free(&aq->res);
	memset(aq, 0, sizeof(*aq));
}