io_storage.c 7.22 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 <stddef.h>
James Morrissey's avatar
James Morrissey committed
9

10
11
12
13
#include <platform_def.h>

#include <drivers/io/io_driver.h>
#include <drivers/io/io_storage.h>
James Morrissey's avatar
James Morrissey committed
14
15

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

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

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

25
26
/* 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
27

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

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

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


/* 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;
45
46

	return (dev != NULL) && (dev->funcs != NULL) &&
James Morrissey's avatar
James Morrissey committed
47
48
49
50
51
52
			(dev->funcs->type != NULL) &&
			(dev->funcs->type() < IO_TYPE_MAX);
}


/* 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
	const io_entity_t *entity = (io_entity_t *)handle;
56
57

	return (entity != NULL) &&
58
			(is_valid_dev((uintptr_t)entity->dev_handle));
James Morrissey's avatar
James Morrissey committed
59
60
61
62
}


/* 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
77
78
{
	assert(dev_info != NULL);
	assert(is_valid_dev_connector(dev_con));

79
	return dev_con->dev_open(dev_spec, dev_info);
James Morrissey's avatar
James Morrissey committed
80
81
82
83
}


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


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


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

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

	return result;
}


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

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

	return result;
}


/* Exported API */

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

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

	return result;
}


/* Open a connection to an IO device */
160
161
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
162
163
164
{
	assert(handle != NULL);

165
	return dev_open(dev_con, dev_spec, (io_dev_info_t **)handle);
James Morrissey's avatar
James Morrissey committed
166
167
168
169
170
}


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

177
	io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
James Morrissey's avatar
James Morrissey committed
178

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

James Morrissey's avatar
James Morrissey committed
184
185
186
187
	return result;
}

/* Close a connection to a device */
188
int io_dev_close(uintptr_t dev_handle)
James Morrissey's avatar
James Morrissey committed
189
{
190
	int result = 0;
191
	assert(dev_handle != (uintptr_t)NULL);
James Morrissey's avatar
James Morrissey committed
192
193
	assert(is_valid_dev(dev_handle));

194
	io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
James Morrissey's avatar
James Morrissey committed
195

196
	/* Absence of registered function implies NOP here */
James Morrissey's avatar
James Morrissey committed
197
198
199
200
201
202
203
204
205
206
207
208
	if (dev->funcs->dev_close != NULL) {
		result = dev->funcs->dev_close(dev);
	}

	return result;
}


/* Synchronous operations */


/* Open an IO entity */
209
int io_open(uintptr_t dev_handle, const uintptr_t spec, uintptr_t *handle)
James Morrissey's avatar
James Morrissey committed
210
{
211
	int result;
212
	assert((spec != (uintptr_t)NULL) && (handle != NULL));
James Morrissey's avatar
James Morrissey committed
213
214
	assert(is_valid_dev(dev_handle));

215
	io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
216
	io_entity_t *entity;
James Morrissey's avatar
James Morrissey committed
217
218
219

	result = allocate_entity(&entity);

220
	if (result == 0) {
James Morrissey's avatar
James Morrissey committed
221
222
223
		assert(dev->funcs->open != NULL);
		result = dev->funcs->open(dev, spec, entity);

224
		if (result == 0) {
225
			entity->dev_handle = dev;
James Morrissey's avatar
James Morrissey committed
226
227
228
229
230
231
232
233
234
			set_handle(handle, entity);
		} else
			free_entity(entity);
	}
	return result;
}


/* Seek to a specific position in an IO entity */
235
int io_seek(uintptr_t handle, io_seek_mode_t mode, signed long long offset)
James Morrissey's avatar
James Morrissey committed
236
{
237
	int result = -ENODEV;
James Morrissey's avatar
James Morrissey committed
238
239
	assert(is_valid_entity(handle) && is_valid_seek_mode(mode));

240
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
241

242
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
243
244
245
246
247
248
249
250
251

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

	return result;
}


/* Determine the length of an IO entity */
252
int io_size(uintptr_t handle, size_t *length)
James Morrissey's avatar
James Morrissey committed
253
{
254
	int result = -ENODEV;
James Morrissey's avatar
James Morrissey committed
255
256
	assert(is_valid_entity(handle) && (length != NULL));

257
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
258

259
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
260
261
262
263
264
265
266
267
268

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

	return result;
}


/* Read data from an IO entity */
269
270
271
272
int io_read(uintptr_t handle,
		uintptr_t buffer,
		size_t length,
		size_t *length_read)
James Morrissey's avatar
James Morrissey committed
273
{
274
	int result = -ENODEV;
275
	assert(is_valid_entity(handle));
James Morrissey's avatar
James Morrissey committed
276

277
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
278

279
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
280
281
282
283
284
285
286
287
288

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

	return result;
}


/* Write data to an IO entity */
289
290
291
int io_write(uintptr_t handle,
		const uintptr_t buffer,
		size_t length,
James Morrissey's avatar
James Morrissey committed
292
293
		size_t *length_written)
{
294
	int result = -ENODEV;
295
	assert(is_valid_entity(handle));
James Morrissey's avatar
James Morrissey committed
296

297
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
298

299
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
300
301
302
303

	if (dev->funcs->write != NULL) {
		result = dev->funcs->write(entity, buffer, length,
				length_written);
304
	}
James Morrissey's avatar
James Morrissey committed
305
306
307
308
309
310

	return result;
}


/* Close an IO entity */
311
int io_close(uintptr_t handle)
James Morrissey's avatar
James Morrissey committed
312
{
313
	int result = 0;
James Morrissey's avatar
James Morrissey committed
314
315
	assert(is_valid_entity(handle));

316
	io_entity_t *entity = (io_entity_t *)handle;
James Morrissey's avatar
James Morrissey committed
317

318
	io_dev_info_t *dev = entity->dev_handle;
James Morrissey's avatar
James Morrissey committed
319

320
	/* Absence of registered function implies NOP here */
James Morrissey's avatar
James Morrissey committed
321
322
	if (dev->funcs->close != NULL)
		result = dev->funcs->close(entity);
323

James Morrissey's avatar
James Morrissey committed
324
325
326
327
328
	/* Ignore improbable free_entity failure */
	(void)free_entity(entity);

	return result;
}