io_storage.c 7.41 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
38
39
40
41
42
{
	int result = (dev_con != NULL) && (dev_con->dev_open != NULL);
	return result;
}


/* Return a boolean value indicating whether a device handle is valid */
43
static int is_valid_dev(const uintptr_t dev_handle)
James Morrissey's avatar
James Morrissey committed
44
{
45
	const io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
James Morrissey's avatar
James Morrissey committed
46
47
48
49
50
51
52
53
	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 */
54
static int is_valid_entity(const uintptr_t handle)
James Morrissey's avatar
James Morrissey committed
55
{
56
57
58
	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
59
60
61
62
63
	return result;
}


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

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


/* Open a connection to a specific device */
74
static int dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec,
75
		io_dev_info_t **dev_info)
James Morrissey's avatar
James Morrissey committed
76
{
77
	int result;
James Morrissey's avatar
James Morrissey committed
78
79
80
81
82
83
84
85
86
	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 */
87
static void set_handle(uintptr_t *handle, io_entity_t *entity)
James Morrissey's avatar
James Morrissey committed
88
89
{
	assert(handle != NULL);
90
	*handle = (uintptr_t)entity;
James Morrissey's avatar
James Morrissey committed
91
92
93
94
}


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


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

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

	return result;
}


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

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

	return result;
}


/* Exported API */

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

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

	return result;
}


/* Open a connection to an IO device */
163
164
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
165
{
166
	int result;
James Morrissey's avatar
James Morrissey committed
167
168
	assert(handle != NULL);

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


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

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

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

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


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

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

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

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

	return result;
}


/* Synchronous operations */


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

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

	result = allocate_entity(&entity);

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

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


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

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

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

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

	return result;
}


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

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

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

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

	return result;
}


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

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

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

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

	return result;
}


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

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

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

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

	return result;
}


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

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

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

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

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

	return result;
}