io_storage.c 9.39 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
34
#include <io_storage.h>
#include <stddef.h>
James Morrissey's avatar
James Morrissey committed
35
36
37
38
39
40
41


#define MAX_DEVICES(plat_data)						\
	(sizeof((plat_data)->devices)/sizeof((plat_data)->devices[0]))


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

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

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


/* Used to keep a reference to platform-specific data */
53
static io_plat_data_t *platform_data;
James Morrissey's avatar
James Morrissey committed
54
55
56
57
58


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

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


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


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

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


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


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


/* Allocate an entity from the pool and return a pointer to it */
133
static int allocate_entity(io_entity_t **entity)
James Morrissey's avatar
James Morrissey committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
{
	int result = IO_FAIL;
	assert(entity != NULL);

	if (entity_count < MAX_IO_HANDLES) {
		unsigned int index = 0;
		result = find_first_entity(NULL, &index);
		assert(result == IO_SUCCESS);
		*entity = entity_map[index] = &entity_pool[index];
		++entity_count;
	} else
		result = IO_RESOURCES_EXHAUSTED;

	return result;
}


/* Release an entity back to the pool */
152
static int free_entity(const io_entity_t *entity)
James Morrissey's avatar
James Morrissey committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
{
	int result = IO_FAIL;
	unsigned int index = 0;
	assert(entity != NULL);

	result = find_first_entity(entity, &index);
	if (result ==  IO_SUCCESS) {
		entity_map[index] = NULL;
		--entity_count;
	}

	return result;
}


/* Exported API */


/* Initialise the IO layer */
172
void io_init(io_plat_data_t *data)
James Morrissey's avatar
James Morrissey committed
173
174
175
176
177
178
179
{
	assert(data != NULL);
	platform_data = data;
}


/* Register a device driver */
180
int io_register_device(const io_dev_info_t *dev_info)
James Morrissey's avatar
James Morrissey committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
{
	int result = IO_FAIL;
	assert(dev_info != NULL);
	assert(platform_data != NULL);

	unsigned int dev_count = platform_data->dev_count;

	if (dev_count < MAX_DEVICES(platform_data)) {
		platform_data->devices[dev_count] = dev_info;
		platform_data->dev_count++;
		result = IO_SUCCESS;
	} else {
		result = IO_RESOURCES_EXHAUSTED;
	}

	return result;
}


/* Open a connection to an IO device */
201
202
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
203
204
205
206
{
	int result = IO_FAIL;
	assert(handle != NULL);

207
	result = dev_open(dev_con, dev_spec, (io_dev_info_t **)handle);
James Morrissey's avatar
James Morrissey committed
208
209
210
211
212
213
	return result;
}


/* Initialise an IO device explicitly - to permit lazy initialisation or
 * re-initialisation */
214
int io_dev_init(uintptr_t dev_handle, const uintptr_t init_params)
James Morrissey's avatar
James Morrissey committed
215
216
{
	int result = IO_FAIL;
217
	assert(dev_handle != (uintptr_t)NULL);
James Morrissey's avatar
James Morrissey committed
218
219
	assert(is_valid_dev(dev_handle));

220
	io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
James Morrissey's avatar
James Morrissey committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234

	if (dev->funcs->dev_init != NULL) {
		result = dev->funcs->dev_init(dev, init_params);
	} else {
		/* Absence of registered function implies NOP here */
		result = IO_SUCCESS;
	}
	return result;
}


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

/* Close a connection to a device */
235
int io_dev_close(uintptr_t dev_handle)
James Morrissey's avatar
James Morrissey committed
236
237
{
	int result = IO_FAIL;
238
	assert(dev_handle != (uintptr_t)NULL);
James Morrissey's avatar
James Morrissey committed
239
240
	assert(is_valid_dev(dev_handle));

241
	io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
James Morrissey's avatar
James Morrissey committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

	if (dev->funcs->dev_close != NULL) {
		result = dev->funcs->dev_close(dev);
	} else {
		/* Absence of registered function implies NOP here */
		result = IO_SUCCESS;
	}

	return result;
}


/* Synchronous operations */


/* Open an IO entity */
258
int io_open(uintptr_t dev_handle, const uintptr_t spec, uintptr_t *handle)
James Morrissey's avatar
James Morrissey committed
259
260
{
	int result = IO_FAIL;
261
	assert((spec != (uintptr_t)NULL) && (handle != NULL));
James Morrissey's avatar
James Morrissey committed
262
263
	assert(is_valid_dev(dev_handle));

264
	io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
265
	io_entity_t *entity;
James Morrissey's avatar
James Morrissey committed
266
267
268
269
270
271
272
273

	result = allocate_entity(&entity);

	if (result == IO_SUCCESS) {
		assert(dev->funcs->open != NULL);
		result = dev->funcs->open(dev, spec, entity);

		if (result == IO_SUCCESS) {
274
			entity->dev_handle = dev;
James Morrissey's avatar
James Morrissey committed
275
276
277
278
279
280
281
282
283
			set_handle(handle, entity);
		} else
			free_entity(entity);
	}
	return result;
}


/* Seek to a specific position in an IO entity */
284
int io_seek(uintptr_t handle, io_seek_mode_t mode, ssize_t offset)
James Morrissey's avatar
James Morrissey committed
285
286
287
288
{
	int result = IO_FAIL;
	assert(is_valid_entity(handle) && is_valid_seek_mode(mode));

289
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
290

291
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
292
293
294
295
296
297
298
299
300
301
302

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

	return result;
}


/* Determine the length of an IO entity */
303
int io_size(uintptr_t handle, size_t *length)
James Morrissey's avatar
James Morrissey committed
304
305
306
307
{
	int result = IO_FAIL;
	assert(is_valid_entity(handle) && (length != NULL));

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

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

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

	return result;
}


/* Read data from an IO entity */
322
323
324
325
int io_read(uintptr_t handle,
		uintptr_t buffer,
		size_t length,
		size_t *length_read)
James Morrissey's avatar
James Morrissey committed
326
327
{
	int result = IO_FAIL;
328
	assert(is_valid_entity(handle) && (buffer != (uintptr_t)NULL));
James Morrissey's avatar
James Morrissey committed
329

330
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
331

332
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
333
334
335
336
337
338
339
340
341
342
343

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

	return result;
}


/* Write data to an IO entity */
344
345
346
int io_write(uintptr_t handle,
		const uintptr_t buffer,
		size_t length,
James Morrissey's avatar
James Morrissey committed
347
348
349
		size_t *length_written)
{
	int result = IO_FAIL;
350
	assert(is_valid_entity(handle) && (buffer != (uintptr_t)NULL));
James Morrissey's avatar
James Morrissey committed
351

352
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
353

354
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
355
356
357
358
359
360
361
362
363
364
365
366

	if (dev->funcs->write != NULL) {
		result = dev->funcs->write(entity, buffer, length,
				length_written);
	} else
		result = IO_NOT_SUPPORTED;

	return result;
}


/* Close an IO entity */
367
int io_close(uintptr_t handle)
James Morrissey's avatar
James Morrissey committed
368
369
370
371
{
	int result = IO_FAIL;
	assert(is_valid_entity(handle));

372
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
373

374
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
375
376
377
378
379
380
381
382
383
384
385
386

	if (dev->funcs->close != NULL)
		result = dev->funcs->close(entity);
	else {
		/* Absence of registered function implies NOP here */
		result = IO_SUCCESS;
	}
	/* Ignore improbable free_entity failure */
	(void)free_entity(entity);

	return result;
}