aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-keystone2/odp_buffer.c
blob: f4d0f136438774b3c8420504b773831a0c50b3c8 (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
/*
 * Copyright (c) 2014, Linaro Limited
 * Copyright (c) 2014, Texas Instruments Incorporated
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp/plat/pool_types.h>
#include <odp/plat/debug.h>

#include <odp/config.h>
#include <odp/buffer.h>

#include <odp_buffer_internal.h>

static inline
odp_buffer_t __odp_buffer_alloc(odp_pool_t pool)
{
	Cppi_HostDesc *desc;
	uintptr_t desc_phys;
	pool_entry_t *pool_entry = _odp_pool_entry(pool);
	uint32_t headroom = pool_entry->headroom;
	odp_pa_t buf_phys;

	desc_phys = QMSS_DESC_PTR(Qmss_queuePopRaw(pool_entry->free_queue));
	desc = _odp_mem_phys_to_virt(desc_phys);

	if (!desc)
		return ODP_BUFFER_INVALID;

	ODP_AS_STR(_cppi_desc_next(desc) == 0,
		   "Pool should not have linked buffers");

	buf_phys = _cppi_desc_orig_ptr(desc);
	if (buf_phys) {
		uint8_t *buf_addr = _odp_mem_phys_to_virt(buf_phys);
		ODP_AS_STR(buf_addr, "Failed phys to virt translation");

		_cppi_desc_orig_vptr_set(desc, buf_addr);
		/* Leave space for buffer metadata */
		_cppi_desc_buf_vptr_set(desc, buf_addr + headroom);
		_cppi_desc_buf_len_set(desc, _cppi_desc_orig_len(desc) -
				       headroom);
	}

	odp_pr_vdbg("pool_id: %p, pkt: %p, buf: %p\n", id, pkt, buf);
	return _cppi_desc_to_odp_buf(desc);
}

odp_buffer_t odp_buffer_alloc(odp_pool_t pool)
{
	return __odp_buffer_alloc(pool);
}

int odp_buffer_alloc_multi(odp_pool_t pool_hdl, odp_buffer_t buf[], int num)
{
	int count;

	for (count = 0; count < num; ++count) {
		buf[count] = __odp_buffer_alloc(pool_hdl);
		if (buf[count] == ODP_BUFFER_INVALID)
			break;
	}

	return count;
}

static inline
void __odp_buffer_free(odp_buffer_t buf)
{
	Cppi_HostDesc *desc;
	uint32_t pool_id;
	pool_entry_t *pool_entry;

	odp_pr_vdbg("buf: %p\n", buf);
	desc = _odp_buf_to_cppi_desc(buf);
	ODP_AS_STR(_cppi_desc_vnext(desc) == NULL,
		   "Pool should not have linked buffers");

	pool_id = _cppi_desc_pool_id(desc);
	pool_entry = _odp_pool_id_to_entry(pool_id);

	if (_cppi_desc_orig_vptr(desc)) {
		_cppi_desc_orig_ptr_set(desc, _odp_mem_virt_to_phys(
						_cppi_desc_orig_vptr(desc)));
	}

	Qmss_queuePushDescSizeRaw(pool_entry->free_queue,
				  (void *)_odp_mem_virt_to_phys(desc),
				  16);
}

void odp_buffer_free(odp_buffer_t buf)
{
	__odp_buffer_free(buf);
}

void odp_buffer_free_multi(const odp_buffer_t buf[], int len)
{
	int i;

	for (i = 0; i < len; ++i)
		__odp_buffer_free(buf[i]);
}

int odp_buffer_snprint(char *str, size_t n, odp_buffer_t buf)
{
	Cppi_HostDesc *desc;
	int len = 0;

	if (!odp_buffer_is_valid(buf)) {
		printf("Buffer is not valid.\n");
		return len;
	}

	desc = _odp_buf_to_cppi_desc(buf);

	len += snprintf(&str[len], n-len,
			"Buffer\n");
	len += snprintf(&str[len], n-len,
			"  desc_vaddr  %p\n",      desc);
	len += snprintf(&str[len], n-len,
			"  buf_paddr_o 0x%x\n",    desc->origBuffPtr);
	len += snprintf(&str[len], n-len,
			"  buf_paddr   0x%x\n",    desc->buffPtr);
	len += snprintf(&str[len], n-len,
			"  buf_len_o 0x%x\n",      desc->origBufferLen);
	len += snprintf(&str[len], n-len,
			"  buf_len   0x%x\n",      desc->buffLen);
	len += snprintf(&str[len], n-len,
			"  pool        %p\n",      odp_buffer_pool(buf));

	len += snprintf(&str[len], n-len, "\n");

	return len;
}

void odp_buffer_print(odp_buffer_t buf)
{
	int max_len = 512;
	char str[max_len];
	int len;
	Cppi_HostDesc *desc;

	len = odp_buffer_snprint(str, max_len-1, buf);
	if (!len)
		return;
	str[len] = 0;

	printf("\n%s\n", str);

	desc = _odp_buf_to_cppi_desc(buf);
	odp_print_mem(desc, TUNE_NETAPI_DESC_SIZE, "Descriptor dump");
	odp_print_mem((void *)desc->origBuffPtr,
		      desc->buffPtr - desc->origBuffPtr + 128,
		      "Buffer start");
}