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

#include <odp/shared_memory.h>
#include <odp/align.h>
#include <odp/plat/align.h>
#include <odp/plat/shared_resource.h>

static void push_entry(shres_head_t **top,
			      shres_head_t *head)
{
	head->next = *top;
	*top = head;
}

static shres_head_t *pop_entry(shres_head_t **top)
{
	shres_head_t *head;

	head = *top;
	if (!head)
		return NULL;
	*top = head->next;
	return head;
}

void shres_table_lock(shres_table_t *table)
{
	odp_ticketlock_lock(&table->lock);
}

void shres_table_unlock(shres_table_t *table)
{
	odp_ticketlock_unlock(&table->lock);
}

shres_head_t *_shres_alloc_nolock(shres_table_t *table)
{
	ODP_AS_STR(table, "Bad table");
	shres_head_t *head;
	head = pop_entry(&table->top_free);
	/* Locking is not mandatory here. Entry should not be used by others */
	ODP_AS_STR(!head->allocated, "Got already allocated entry");
	head->allocated = 1;
	head->ref_cnt = 1; /* One reference is created */
	table->entry_num_allocated++;
	return head;
}

shres_head_t *_shres_alloc(shres_table_t *table)
{
	ODP_AS_STR(table, "Bad table");
	shres_head_t *head;
	shres_table_lock(table);
	head = _shres_alloc_nolock(table);
	shres_table_unlock(table);
	ODP_DBG("Allocated entry head: %p\n", head);
	return head;
}

void _shres_free(shres_table_t *table, shres_head_t *head)
{
	ODP_AS_STR(head, "Bad head");
	ODP_AS_STR(head->allocated, "Freeing already freed entry");
	ODP_AS_STR(table, "Bad table");
	shres_table_lock(table);
	ODP_AS_STR(table->entry_num_allocated > 0, "Freeing extra entry");
	head->allocated = 0;
	push_entry(&table->top_free, head);
	table->entry_num_allocated--;
	shres_table_unlock(table);
}

int _shres_get(shres_table_t *table ODP_UNUSED, shres_head_t *head)
{
	_shres_lock(head);
	if (!head->allocated) {
		_shres_unlock(head);
		return -1;
	}
	head->ref_cnt++;
	_shres_unlock(head);
	return 0;
}

int _shres_put(shres_table_t *table, shres_head_t *head)
{
	int ret = 0;
	_shres_lock(head);
	ODP_AS_STR(head->ref_cnt > 0, "Extra resource put");
	head->ref_cnt--;
	if (!head->ref_cnt) {
		ret = table->free_callback((uint8_t *)head -
					   table->entry_head_offset);
		if (ret < 0) {
			head->ref_cnt++;
			_shres_unlock(head);
		} else {
			_shres_unlock(head);
			_shres_free(table, head);
		}
	} else {
		_shres_unlock(head);
	}

	return ret;
}

shres_head_t *_shres_next(shres_table_t *table, shres_head_t *head)
{
	ODP_AS_STR(head, "Bad head");
	ODP_AS_STR(table, "Bad table");
	uint16_t id = _shres_id(head);

	if (++id >= shres_table_num_entries(table))
		return NULL;
	return _shres_from_id(table, id);
}

#define _shres_table_for_each(table, head)			\
	for (head = _shres_from_id(table, 0);			\
	     head;						\
	     head = _shres_next(table, head))

#define _shres_table_for_each_continue(table, head)		\
	for (head = _shres_next(table, head);			\
	     head;						\
	     head = _shres_next(table, head))

shres_table_t *_shres_table_create(const char *name,
			   uint32_t entry_size,
			   uint32_t entry_num,
			   uint32_t entry_align,
			   uint16_t head_offset,
			   shres_free_cb_t free_func)
{
	odp_shm_t shm;
	void *addr;
	uint32_t size;
	uint32_t table_head_size;
	shres_table_t *table;
	shres_head_t *head;
	uint32_t i;

	if (entry_align == 0)
		entry_align = ODP_CACHE_LINE_SIZE;

	table_head_size = ODP_ALIGN_ROUNDUP(sizeof(shres_table_t),
					      entry_align);
	entry_size = ODP_ALIGN_ROUNDUP(entry_size, entry_align);
	size = table_head_size + entry_size * entry_num;

	shm = odp_shm_reserve(name, size, entry_align, ODP_SHM_SW_ONLY);
	if (shm == ODP_SHM_INVALID)
		return SHRES_TABLE_INVALID;

	addr = odp_shm_addr(shm);
	if (addr == NULL) {
		odp_shm_free(shm);
		return SHRES_TABLE_INVALID;
	}

	memset(addr, 0, size);

	table = addr;
	odp_ticketlock_init(&table->lock);
	table->top_free = NULL;
	table->entry_num = entry_num;
	table->entry_num_allocated = 0;
	table->entry_size = entry_size;
	table->entry_head_offset = head_offset;
	table->free_callback = free_func;

	addr = (uint8_t *)addr + table_head_size + head_offset;
	table->base = addr;

	for (i = 0; i < entry_num; i++) {
		ODP_AS_STR(ODP_ALIGNED_CHECK((uint8_t *)addr - head_offset,
					     entry_align),
			   "Wrong entry alignment");
		head = addr;
		head->id = i;
		odp_ticketlock_init(&head->lock);
		push_entry(&table->top_free, head);
		addr = (uint8_t *)addr + entry_size;
	}

	return table;
}

/**
 * shres_table_destroy - frees table by name
 * returns number of not freed entries, -1 if error
 */
int shres_table_destroy(const char *name)
{
	int i = 0;
	odp_shm_t shm;
	shres_head_t *head;
	shres_table_t *table;

	shm = odp_shm_lookup(name);
	if (shm == ODP_SHM_INVALID)
		return -1;

	table = odp_shm_addr(shm);
	_shres_table_for_each(table, head) {
		if (!head->allocated)
			continue;

		if (!i++) {
			ODP_DBG("| Table \"%s\"\n", name);
			ODP_DBG("|-----------------------------------\n");
			ODP_DBG("| entry id | refcnt |\n");
		}

		ODP_DBG("|  %06i  |  %04i  |\n", head->id, head->ref_cnt);
	}

	if (i) {
		ODP_DBG("|-----------------------------------\n");
		ODP_DBG("| %i entries were not freed\n", i);
	}

	if (odp_shm_free(shm) < 0) {
		ODP_DBG("shm free failed for table \"%s\"\n", name);
		return -1;
	}

	return i;
}

shres_head_t *_shres_first_allocated(shres_table_t *table)
{
	shres_head_t *head;
	_shres_table_for_each(table, head) {
		if (head->allocated)
			return head;
	}
	return NULL;
}

shres_head_t *_shres_next_allocated(shres_table_t *table,
					shres_head_t *head)
{
	ODP_AS_STR(head, "Bad head");
	ODP_AS_STR(table, "Bad table");

	_shres_table_for_each_continue(table, head) {
		if (head->allocated)
			return head;
	}
	return NULL;
}