io_storage.c 7.45 KB
Newer Older
James Morrissey's avatar
James Morrissey committed
1
/*
2
 * Copyright (c) 2014-2017, ARM Limited and Contributors. All rights reserved.
James Morrissey's avatar
James Morrissey committed
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
James Morrissey's avatar
James Morrissey committed
5
6
7
 */

#include <assert.h>
8
#include <io_driver.h>
9
#include <io_storage.h>
10
#include <platform_def.h>
11
#include <stddef.h>
James Morrissey's avatar
James Morrissey committed
12
13
14


/* Storage for a fixed maximum number of IO entities, definable by platform */
15
static io_entity_t entity_pool[MAX_IO_HANDLES];
James Morrissey's avatar
James Morrissey committed
16
17
18

/* Simple way of tracking used storage - each entry is NULL or a pointer to an
 * entity */
19
static io_entity_t *entity_map[MAX_IO_HANDLES];
James Morrissey's avatar
James Morrissey committed
20
21
22
23

/* Track number of allocated entities */
static unsigned int entity_count;

24
25
/* Array of fixed maximum of registered devices, definable by platform */
static const io_dev_info_t *devices[MAX_IO_DEVICES];
James Morrissey's avatar
James Morrissey committed
26

27
28
/* Number of currently registered devices */
static unsigned int dev_count;
James Morrissey's avatar
James Morrissey committed
29

30
31
/* Extra validation functions only used when asserts are enabled */
#if ENABLE_ASSERTIONS
James Morrissey's avatar
James Morrissey committed
32
33

/* Return a boolean value indicating whether a device connector is valid */
34
static int is_valid_dev_connector(const io_dev_connector_t *dev_con)
James Morrissey's avatar
James Morrissey committed
35
36
37
38
39
40
41
{
	int result = (dev_con != NULL) && (dev_con->dev_open != NULL);
	return result;
}


/* Return a boolean value indicating whether a device handle is valid */
42
static int is_valid_dev(const uintptr_t dev_handle)
James Morrissey's avatar
James Morrissey committed
43
{
44
	const io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
James Morrissey's avatar
James Morrissey committed
45
46
47
48
49
50
51
52
	int result = (dev != NULL) && (dev->funcs != NULL) &&
			(dev->funcs->type != NULL) &&
			(dev->funcs->type() < IO_TYPE_MAX);
	return result;
}


/* Return a boolean value indicating whether an IO entity is valid */
53
static int is_valid_entity(const uintptr_t handle)
James Morrissey's avatar
James Morrissey committed
54
{
55
56
57
	const io_entity_t *entity = (io_entity_t *)handle;
	int result = (entity != NULL) &&
			(is_valid_dev((uintptr_t)entity->dev_handle));
James Morrissey's avatar
James Morrissey committed
58
59
60
61
62
	return result;
}


/* Return a boolean value indicating whether a seek mode is valid */
63
static int is_valid_seek_mode(io_seek_mode_t mode)
James Morrissey's avatar
James Morrissey committed
64
65
66
67
{
	return ((mode != IO_SEEK_INVALID) && (mode < IO_SEEK_MAX));
}

68
69
#endif /* ENABLE_ASSERTIONS */
/* End of extra validation functions only used when asserts are enabled */
James Morrissey's avatar
James Morrissey committed
70
71
72


/* Open a connection to a specific device */
73
static int dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec,
74
		io_dev_info_t **dev_info)
James Morrissey's avatar
James Morrissey committed
75
{
76
	int result;
James Morrissey's avatar
James Morrissey committed
77
78
79
80
81
82
83
84
85
	assert(dev_info != NULL);
	assert(is_valid_dev_connector(dev_con));

	result = dev_con->dev_open(dev_spec, dev_info);
	return result;
}


/* Set a handle to track an entity */
86
static void set_handle(uintptr_t *handle, io_entity_t *entity)
James Morrissey's avatar
James Morrissey committed
87
88
{
	assert(handle != NULL);
89
	*handle = (uintptr_t)entity;
James Morrissey's avatar
James Morrissey committed
90
91
92
93
}


/* Locate an entity in the pool, specified by address */
94
static int find_first_entity(const io_entity_t *entity, unsigned int *index_out)
James Morrissey's avatar
James Morrissey committed
95
{
96
	int result = -ENOENT;
97
	for (unsigned int index = 0; index < MAX_IO_HANDLES; ++index) {
James Morrissey's avatar
James Morrissey committed
98
		if (entity_map[index] == entity) {
99
			result = 0;
James Morrissey's avatar
James Morrissey committed
100
101
102
103
104
105
106
107
108
			*index_out = index;
			break;
		}
	}
	return result;
}


/* Allocate an entity from the pool and return a pointer to it */
109
static int allocate_entity(io_entity_t **entity)
James Morrissey's avatar
James Morrissey committed
110
{
111
	int result = -ENOMEM;
James Morrissey's avatar
James Morrissey committed
112
113
114
115
116
	assert(entity != NULL);

	if (entity_count < MAX_IO_HANDLES) {
		unsigned int index = 0;
		result = find_first_entity(NULL, &index);
117
		assert(result == 0);
James Morrissey's avatar
James Morrissey committed
118
119
		*entity = entity_map[index] = &entity_pool[index];
		++entity_count;
120
	}
James Morrissey's avatar
James Morrissey committed
121
122
123
124
125
126

	return result;
}


/* Release an entity back to the pool */
127
static int free_entity(const io_entity_t *entity)
James Morrissey's avatar
James Morrissey committed
128
{
129
	int result;
James Morrissey's avatar
James Morrissey committed
130
131
132
133
	unsigned int index = 0;
	assert(entity != NULL);

	result = find_first_entity(entity, &index);
134
	if (result ==  0) {
James Morrissey's avatar
James Morrissey committed
135
136
137
138
139
140
141
142
143
144
145
		entity_map[index] = NULL;
		--entity_count;
	}

	return result;
}


/* Exported API */

/* Register a device driver */
146
int io_register_device(const io_dev_info_t *dev_info)
James Morrissey's avatar
James Morrissey committed
147
{
148
	int result = -ENOMEM;
James Morrissey's avatar
James Morrissey committed
149
150
	assert(dev_info != NULL);

151
152
153
	if (dev_count < MAX_IO_DEVICES) {
		devices[dev_count] = dev_info;
		dev_count++;
154
		result = 0;
James Morrissey's avatar
James Morrissey committed
155
156
157
158
159
160
161
	}

	return result;
}


/* Open a connection to an IO device */
162
163
int io_dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec,
		uintptr_t *handle)
James Morrissey's avatar
James Morrissey committed
164
{
165
	int result;
James Morrissey's avatar
James Morrissey committed
166
167
	assert(handle != NULL);

168
	result = dev_open(dev_con, dev_spec, (io_dev_info_t **)handle);
James Morrissey's avatar
James Morrissey committed
169
170
171
172
173
174
	return result;
}


/* Initialise an IO device explicitly - to permit lazy initialisation or
 * re-initialisation */
175
int io_dev_init(uintptr_t dev_handle, const uintptr_t init_params)
James Morrissey's avatar
James Morrissey committed
176
{
177
	int result = 0;
178
	assert(dev_handle != (uintptr_t)NULL);
James Morrissey's avatar
James Morrissey committed
179
180
	assert(is_valid_dev(dev_handle));

181
	io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
James Morrissey's avatar
James Morrissey committed
182

183
	/* Absence of registered function implies NOP here */
James Morrissey's avatar
James Morrissey committed
184
185
186
	if (dev->funcs->dev_init != NULL) {
		result = dev->funcs->dev_init(dev, init_params);
	}
187

James Morrissey's avatar
James Morrissey committed
188
189
190
191
192
193
194
	return result;
}


/* TODO: Consider whether an explicit "shutdown" API should be included */

/* Close a connection to a device */
195
int io_dev_close(uintptr_t dev_handle)
James Morrissey's avatar
James Morrissey committed
196
{
197
	int result = 0;
198
	assert(dev_handle != (uintptr_t)NULL);
James Morrissey's avatar
James Morrissey committed
199
200
	assert(is_valid_dev(dev_handle));

201
	io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
James Morrissey's avatar
James Morrissey committed
202

203
	/* Absence of registered function implies NOP here */
James Morrissey's avatar
James Morrissey committed
204
205
206
207
208
209
210
211
212
213
214
215
	if (dev->funcs->dev_close != NULL) {
		result = dev->funcs->dev_close(dev);
	}

	return result;
}


/* Synchronous operations */


/* Open an IO entity */
216
int io_open(uintptr_t dev_handle, const uintptr_t spec, uintptr_t *handle)
James Morrissey's avatar
James Morrissey committed
217
{
218
	int result;
219
	assert((spec != (uintptr_t)NULL) && (handle != NULL));
James Morrissey's avatar
James Morrissey committed
220
221
	assert(is_valid_dev(dev_handle));

222
	io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
223
	io_entity_t *entity;
James Morrissey's avatar
James Morrissey committed
224
225
226

	result = allocate_entity(&entity);

227
	if (result == 0) {
James Morrissey's avatar
James Morrissey committed
228
229
230
		assert(dev->funcs->open != NULL);
		result = dev->funcs->open(dev, spec, entity);

231
		if (result == 0) {
232
			entity->dev_handle = dev;
James Morrissey's avatar
James Morrissey committed
233
234
235
236
237
238
239
240
241
			set_handle(handle, entity);
		} else
			free_entity(entity);
	}
	return result;
}


/* Seek to a specific position in an IO entity */
242
int io_seek(uintptr_t handle, io_seek_mode_t mode, ssize_t offset)
James Morrissey's avatar
James Morrissey committed
243
{
244
	int result = -ENODEV;
James Morrissey's avatar
James Morrissey committed
245
246
	assert(is_valid_entity(handle) && is_valid_seek_mode(mode));

247
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
248

249
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
250
251
252
253
254
255
256
257
258

	if (dev->funcs->seek != NULL)
		result = dev->funcs->seek(entity, mode, offset);

	return result;
}


/* Determine the length of an IO entity */
259
int io_size(uintptr_t handle, size_t *length)
James Morrissey's avatar
James Morrissey committed
260
{
261
	int result = -ENODEV;
James Morrissey's avatar
James Morrissey committed
262
263
	assert(is_valid_entity(handle) && (length != NULL));

264
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
265

266
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
267
268
269
270
271
272
273
274
275

	if (dev->funcs->size != NULL)
		result = dev->funcs->size(entity, length);

	return result;
}


/* Read data from an IO entity */
276
277
278
279
int io_read(uintptr_t handle,
		uintptr_t buffer,
		size_t length,
		size_t *length_read)
James Morrissey's avatar
James Morrissey committed
280
{
281
	int result = -ENODEV;
282
	assert(is_valid_entity(handle) && (buffer != (uintptr_t)NULL));
James Morrissey's avatar
James Morrissey committed
283

284
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
285

286
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
287
288
289
290
291
292
293
294
295

	if (dev->funcs->read != NULL)
		result = dev->funcs->read(entity, buffer, length, length_read);

	return result;
}


/* Write data to an IO entity */
296
297
298
int io_write(uintptr_t handle,
		const uintptr_t buffer,
		size_t length,
James Morrissey's avatar
James Morrissey committed
299
300
		size_t *length_written)
{
301
	int result = -ENODEV;
302
	assert(is_valid_entity(handle) && (buffer != (uintptr_t)NULL));
James Morrissey's avatar
James Morrissey committed
303

304
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
305

306
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
307
308
309
310

	if (dev->funcs->write != NULL) {
		result = dev->funcs->write(entity, buffer, length,
				length_written);
311
	}
James Morrissey's avatar
James Morrissey committed
312
313
314
315
316
317

	return result;
}


/* Close an IO entity */
318
int io_close(uintptr_t handle)
James Morrissey's avatar
James Morrissey committed
319
{
320
	int result = 0;
James Morrissey's avatar
James Morrissey committed
321
322
	assert(is_valid_entity(handle));

323
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
324

325
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
326

327
	/* Absence of registered function implies NOP here */
James Morrissey's avatar
James Morrissey committed
328
329
	if (dev->funcs->close != NULL)
		result = dev->funcs->close(entity);
330

James Morrissey's avatar
James Morrissey committed
331
332
333
334
335
	/* Ignore improbable free_entity failure */
	(void)free_entity(entity);

	return result;
}