io_storage.c 8.76 KB
Newer Older
James Morrissey's avatar
James Morrissey committed
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
/*
 * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of ARM nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <assert.h>
32
#include <io_driver.h>
33
#include <io_storage.h>
34
#include <platform_def.h>
35
#include <stddef.h>
James Morrissey's avatar
James Morrissey committed
36
37
38


/* Storage for a fixed maximum number of IO entities, definable by platform */
39
static io_entity_t entity_pool[MAX_IO_HANDLES];
James Morrissey's avatar
James Morrissey committed
40
41
42

/* Simple way of tracking used storage - each entry is NULL or a pointer to an
 * entity */
43
static io_entity_t *entity_map[MAX_IO_HANDLES];
James Morrissey's avatar
James Morrissey committed
44
45
46
47

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

48
49
/* 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
50

51
52
/* Number of currently registered devices */
static unsigned int dev_count;
James Morrissey's avatar
James Morrissey committed
53
54
55
56
57


#if DEBUG	/* Extra validation functions only used in debug builds */

/* Return a boolean value indicating whether a device connector is valid */
58
static int is_valid_dev_connector(const io_dev_connector_t *dev_con)
James Morrissey's avatar
James Morrissey committed
59
60
61
62
63
64
65
{
	int result = (dev_con != NULL) && (dev_con->dev_open != NULL);
	return result;
}


/* Return a boolean value indicating whether a device handle is valid */
66
static int is_valid_dev(const uintptr_t dev_handle)
James Morrissey's avatar
James Morrissey committed
67
{
68
	const io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
James Morrissey's avatar
James Morrissey committed
69
70
71
72
73
74
75
76
	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 */
77
static int is_valid_entity(const uintptr_t handle)
James Morrissey's avatar
James Morrissey committed
78
{
79
80
81
	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
82
83
84
85
86
	return result;
}


/* Return a boolean value indicating whether a seek mode is valid */
87
static int is_valid_seek_mode(io_seek_mode_t mode)
James Morrissey's avatar
James Morrissey committed
88
89
90
91
92
93
94
95
{
	return ((mode != IO_SEEK_INVALID) && (mode < IO_SEEK_MAX));
}

#endif	/* End of debug-only validation functions */


/* Open a connection to a specific device */
96
static int dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec,
97
		io_dev_info_t **dev_info)
James Morrissey's avatar
James Morrissey committed
98
{
99
	int result;
James Morrissey's avatar
James Morrissey committed
100
101
102
103
104
105
106
107
108
	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 */
109
static void set_handle(uintptr_t *handle, io_entity_t *entity)
James Morrissey's avatar
James Morrissey committed
110
111
{
	assert(handle != NULL);
112
	*handle = (uintptr_t)entity;
James Morrissey's avatar
James Morrissey committed
113
114
115
116
}


/* Locate an entity in the pool, specified by address */
117
static int find_first_entity(const io_entity_t *entity, unsigned int *index_out)
James Morrissey's avatar
James Morrissey committed
118
{
119
	int result = -ENOENT;
James Morrissey's avatar
James Morrissey committed
120
121
	for (int index = 0; index < MAX_IO_HANDLES; ++index) {
		if (entity_map[index] == entity) {
122
			result = 0;
James Morrissey's avatar
James Morrissey committed
123
124
125
126
127
128
129
130
131
			*index_out = index;
			break;
		}
	}
	return result;
}


/* Allocate an entity from the pool and return a pointer to it */
132
static int allocate_entity(io_entity_t **entity)
James Morrissey's avatar
James Morrissey committed
133
{
134
	int result = -ENOMEM;
James Morrissey's avatar
James Morrissey committed
135
136
137
138
139
	assert(entity != NULL);

	if (entity_count < MAX_IO_HANDLES) {
		unsigned int index = 0;
		result = find_first_entity(NULL, &index);
140
		assert(result == 0);
James Morrissey's avatar
James Morrissey committed
141
142
		*entity = entity_map[index] = &entity_pool[index];
		++entity_count;
143
	}
James Morrissey's avatar
James Morrissey committed
144
145
146
147
148
149

	return result;
}


/* Release an entity back to the pool */
150
static int free_entity(const io_entity_t *entity)
James Morrissey's avatar
James Morrissey committed
151
{
152
	int result;
James Morrissey's avatar
James Morrissey committed
153
154
155
156
	unsigned int index = 0;
	assert(entity != NULL);

	result = find_first_entity(entity, &index);
157
	if (result ==  0) {
James Morrissey's avatar
James Morrissey committed
158
159
160
161
162
163
164
165
166
167
168
		entity_map[index] = NULL;
		--entity_count;
	}

	return result;
}


/* Exported API */

/* Register a device driver */
169
int io_register_device(const io_dev_info_t *dev_info)
James Morrissey's avatar
James Morrissey committed
170
{
171
	int result = -ENOMEM;
James Morrissey's avatar
James Morrissey committed
172
173
	assert(dev_info != NULL);

174
175
176
	if (dev_count < MAX_IO_DEVICES) {
		devices[dev_count] = dev_info;
		dev_count++;
177
		result = 0;
James Morrissey's avatar
James Morrissey committed
178
179
180
181
182
183
184
	}

	return result;
}


/* Open a connection to an IO device */
185
186
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
187
{
188
	int result;
James Morrissey's avatar
James Morrissey committed
189
190
	assert(handle != NULL);

191
	result = dev_open(dev_con, dev_spec, (io_dev_info_t **)handle);
James Morrissey's avatar
James Morrissey committed
192
193
194
195
196
197
	return result;
}


/* Initialise an IO device explicitly - to permit lazy initialisation or
 * re-initialisation */
198
int io_dev_init(uintptr_t dev_handle, const uintptr_t init_params)
James Morrissey's avatar
James Morrissey committed
199
{
200
	int result = 0;
201
	assert(dev_handle != (uintptr_t)NULL);
James Morrissey's avatar
James Morrissey committed
202
203
	assert(is_valid_dev(dev_handle));

204
	io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
James Morrissey's avatar
James Morrissey committed
205

206
	/* Absence of registered function implies NOP here */
James Morrissey's avatar
James Morrissey committed
207
208
209
	if (dev->funcs->dev_init != NULL) {
		result = dev->funcs->dev_init(dev, init_params);
	}
210

James Morrissey's avatar
James Morrissey committed
211
212
213
214
215
216
217
	return result;
}


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

/* Close a connection to a device */
218
int io_dev_close(uintptr_t dev_handle)
James Morrissey's avatar
James Morrissey committed
219
{
220
	int result = 0;
221
	assert(dev_handle != (uintptr_t)NULL);
James Morrissey's avatar
James Morrissey committed
222
223
	assert(is_valid_dev(dev_handle));

224
	io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
James Morrissey's avatar
James Morrissey committed
225

226
	/* Absence of registered function implies NOP here */
James Morrissey's avatar
James Morrissey committed
227
228
229
230
231
232
233
234
235
236
237
238
	if (dev->funcs->dev_close != NULL) {
		result = dev->funcs->dev_close(dev);
	}

	return result;
}


/* Synchronous operations */


/* Open an IO entity */
239
int io_open(uintptr_t dev_handle, const uintptr_t spec, uintptr_t *handle)
James Morrissey's avatar
James Morrissey committed
240
{
241
	int result;
242
	assert((spec != (uintptr_t)NULL) && (handle != NULL));
James Morrissey's avatar
James Morrissey committed
243
244
	assert(is_valid_dev(dev_handle));

245
	io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
246
	io_entity_t *entity;
James Morrissey's avatar
James Morrissey committed
247
248
249

	result = allocate_entity(&entity);

250
	if (result == 0) {
James Morrissey's avatar
James Morrissey committed
251
252
253
		assert(dev->funcs->open != NULL);
		result = dev->funcs->open(dev, spec, entity);

254
		if (result == 0) {
255
			entity->dev_handle = dev;
James Morrissey's avatar
James Morrissey committed
256
257
258
259
260
261
262
263
264
			set_handle(handle, entity);
		} else
			free_entity(entity);
	}
	return result;
}


/* Seek to a specific position in an IO entity */
265
int io_seek(uintptr_t handle, io_seek_mode_t mode, ssize_t offset)
James Morrissey's avatar
James Morrissey committed
266
{
267
	int result = -ENODEV;
James Morrissey's avatar
James Morrissey committed
268
269
	assert(is_valid_entity(handle) && is_valid_seek_mode(mode));

270
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
271

272
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
273
274
275
276
277
278
279
280
281

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

	return result;
}


/* Determine the length of an IO entity */
282
int io_size(uintptr_t handle, size_t *length)
James Morrissey's avatar
James Morrissey committed
283
{
284
	int result = -ENODEV;
James Morrissey's avatar
James Morrissey committed
285
286
	assert(is_valid_entity(handle) && (length != NULL));

287
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
288

289
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
290
291
292
293
294
295
296
297
298

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

	return result;
}


/* Read data from an IO entity */
299
300
301
302
int io_read(uintptr_t handle,
		uintptr_t buffer,
		size_t length,
		size_t *length_read)
James Morrissey's avatar
James Morrissey committed
303
{
304
	int result = -ENODEV;
305
	assert(is_valid_entity(handle) && (buffer != (uintptr_t)NULL));
James Morrissey's avatar
James Morrissey committed
306

307
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
308

309
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
310
311
312
313
314
315
316
317
318

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

	return result;
}


/* Write data to an IO entity */
319
320
321
int io_write(uintptr_t handle,
		const uintptr_t buffer,
		size_t length,
James Morrissey's avatar
James Morrissey committed
322
323
		size_t *length_written)
{
324
	int result = -ENODEV;
325
	assert(is_valid_entity(handle) && (buffer != (uintptr_t)NULL));
James Morrissey's avatar
James Morrissey committed
326

327
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
328

329
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
330
331
332
333

	if (dev->funcs->write != NULL) {
		result = dev->funcs->write(entity, buffer, length,
				length_written);
334
	}
James Morrissey's avatar
James Morrissey committed
335
336
337
338
339
340

	return result;
}


/* Close an IO entity */
341
int io_close(uintptr_t handle)
James Morrissey's avatar
James Morrissey committed
342
{
343
	int result = 0;
James Morrissey's avatar
James Morrissey committed
344
345
	assert(is_valid_entity(handle));

346
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
347

348
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
349

350
	/* Absence of registered function implies NOP here */
James Morrissey's avatar
James Morrissey committed
351
352
	if (dev->funcs->close != NULL)
		result = dev->funcs->close(entity);
353

James Morrissey's avatar
James Morrissey committed
354
355
356
357
358
	/* Ignore improbable free_entity failure */
	(void)free_entity(entity);

	return result;
}