blob: 9a45da2dacacb2b607d7cd0b96a5c73ebb5892d1 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
2 * Copyright (c) 2018, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
Antonio Nino Diaz09a00ef2019-01-11 13:12:58 +00008#include <drivers/io/io_driver.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02009#include <io_storage.h>
10#include <platform_def.h>
11#include <stddef.h>
12
13
14/* Storage for a fixed maximum number of IO entities, definable by platform */
15static io_entity_t entity_pool[MAX_IO_HANDLES];
16
17/* Simple way of tracking used storage - each entry is NULL or a pointer to an
18 * entity */
19static io_entity_t *entity_map[MAX_IO_HANDLES];
20
21/* Track number of allocated entities */
22static unsigned int entity_count;
23
24/* Array of fixed maximum of registered devices, definable by platform */
25static const io_dev_info_t *devices[MAX_IO_DEVICES];
26
27/* Number of currently registered devices */
28static unsigned int dev_count;
29
30
31#if DEBUG /* Extra validation functions only used in debug builds */
32
33/* Return a boolean value indicating whether a device connector is valid */
34static int is_valid_dev_connector(const io_dev_connector_t *dev_con)
35{
36 int result = (dev_con != NULL) && (dev_con->dev_open != NULL);
37 return result;
38}
39
40
41/* Return a boolean value indicating whether a device handle is valid */
42static int is_valid_dev(const uintptr_t dev_handle)
43{
44 const io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
45 int result = (dev != NULL) && (dev->funcs != NULL) &&
46 (dev->funcs->type != NULL) &&
47 (dev->funcs->type() < IO_TYPE_MAX);
48 return result;
49}
50
51
52/* Return a boolean value indicating whether an IO entity is valid */
53static int is_valid_entity(const uintptr_t handle)
54{
55 const io_entity_t *entity = (io_entity_t *)handle;
56 int result = (entity != NULL) &&
57 (is_valid_dev((uintptr_t)entity->dev_handle));
58 return result;
59}
60
61
62/* Return a boolean value indicating whether a seek mode is valid */
63static int is_valid_seek_mode(io_seek_mode_t mode)
64{
65 return ((mode != IO_SEEK_INVALID) && (mode < IO_SEEK_MAX));
66}
67
68#endif /* End of debug-only validation functions */
69
70
71/* Open a connection to a specific device */
72static int dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec,
73 io_dev_info_t **dev_info)
74{
75 int result = IO_FAIL;
76 assert(dev_info != NULL);
77 assert(is_valid_dev_connector(dev_con));
78
79 result = dev_con->dev_open(dev_spec, dev_info);
80 return result;
81}
82
83
84/* Set a handle to track an entity */
85static void set_handle(uintptr_t *handle, io_entity_t *entity)
86{
87 assert(handle != NULL);
88 *handle = (uintptr_t)entity;
89}
90
91
92/* Locate an entity in the pool, specified by address */
93static int find_first_entity(const io_entity_t *entity, unsigned int *index_out)
94{
95 int result = IO_FAIL;
96 for (int index = 0; index < MAX_IO_HANDLES; ++index) {
97 if (entity_map[index] == entity) {
98 result = IO_SUCCESS;
99 *index_out = index;
100 break;
101 }
102 }
103 return result;
104}
105
106
107/* Allocate an entity from the pool and return a pointer to it */
108static int allocate_entity(io_entity_t **entity)
109{
110 int result = IO_FAIL;
111 assert(entity != NULL);
112
113 if (entity_count < MAX_IO_HANDLES) {
114 unsigned int index = 0;
115 result = find_first_entity(NULL, &index);
116 assert(result == IO_SUCCESS);
117 *entity = entity_map[index] = &entity_pool[index];
118 ++entity_count;
119 } else
120 result = IO_RESOURCES_EXHAUSTED;
121
122 return result;
123}
124
125
126/* Release an entity back to the pool */
127static int free_entity(const io_entity_t *entity)
128{
129 int result = IO_FAIL;
130 unsigned int index = 0;
131 assert(entity != NULL);
132
133 result = find_first_entity(entity, &index);
134 if (result == IO_SUCCESS) {
135 entity_map[index] = NULL;
136 --entity_count;
137 }
138
139 return result;
140}
141
142
143/* Exported API */
144
145/* Register a device driver */
146int io_register_device(const io_dev_info_t *dev_info)
147{
148 int result = IO_FAIL;
149 assert(dev_info != NULL);
150
151 if (dev_count < MAX_IO_DEVICES) {
152 devices[dev_count] = dev_info;
153 dev_count++;
154 result = IO_SUCCESS;
155 } else {
156 result = IO_RESOURCES_EXHAUSTED;
157 }
158
159 return result;
160}
161
162
163/* Open a connection to an IO device */
164int io_dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec,
165 uintptr_t *handle)
166{
167 int result = IO_FAIL;
168 assert(handle != NULL);
169
170 result = dev_open(dev_con, dev_spec, (io_dev_info_t **)handle);
171 return result;
172}
173
174
175/* Initialise an IO device explicitly - to permit lazy initialisation or
176 * re-initialisation */
177int io_dev_init(uintptr_t dev_handle, const uintptr_t init_params)
178{
179 int result = IO_FAIL;
180 assert(dev_handle != (uintptr_t)NULL);
181 assert(is_valid_dev(dev_handle));
182
183 io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
184
185 if (dev->funcs->dev_init != NULL) {
186 result = dev->funcs->dev_init(dev, init_params);
187 } else {
188 /* Absence of registered function implies NOP here */
189 result = IO_SUCCESS;
190 }
191 return result;
192}
193
194
195/* TODO: Consider whether an explicit "shutdown" API should be included */
196
197/* Close a connection to a device */
198int io_dev_close(uintptr_t dev_handle)
199{
200 int result = IO_FAIL;
201 assert(dev_handle != (uintptr_t)NULL);
202 assert(is_valid_dev(dev_handle));
203
204 io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
205
206 if (dev->funcs->dev_close != NULL) {
207 result = dev->funcs->dev_close(dev);
208 } else {
209 /* Absence of registered function implies NOP here */
210 result = IO_SUCCESS;
211 }
212
213 return result;
214}
215
216
217/* Synchronous operations */
218
219
220/* Open an IO entity */
221int io_open(uintptr_t dev_handle, const uintptr_t spec, uintptr_t *handle)
222{
223 int result = IO_FAIL;
224 assert((spec != (uintptr_t)NULL) && (handle != NULL));
225 assert(is_valid_dev(dev_handle));
226
227 io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
228 io_entity_t *entity;
229
230 result = allocate_entity(&entity);
231
232 if (result == IO_SUCCESS) {
233 assert(dev->funcs->open != NULL);
234 result = dev->funcs->open(dev, spec, entity);
235
236 if (result == IO_SUCCESS) {
237 entity->dev_handle = dev;
238 set_handle(handle, entity);
239 } else
240 free_entity(entity);
241 }
242 return result;
243}
244
245
246/* Seek to a specific position in an IO entity */
247int io_seek(uintptr_t handle, io_seek_mode_t mode, ssize_t offset)
248{
249 int result = IO_FAIL;
250 assert(is_valid_entity(handle) && is_valid_seek_mode(mode));
251
252 io_entity_t *entity = (io_entity_t *)handle;
253
254 io_dev_info_t *dev = entity->dev_handle;
255
256 if (dev->funcs->seek != NULL)
257 result = dev->funcs->seek(entity, mode, offset);
258 else
259 result = IO_NOT_SUPPORTED;
260
261 return result;
262}
263
264
265/* Determine the length of an IO entity */
266int io_size(uintptr_t handle, size_t *length)
267{
268 int result = IO_FAIL;
269 assert(is_valid_entity(handle) && (length != NULL));
270
271 io_entity_t *entity = (io_entity_t *)handle;
272
273 io_dev_info_t *dev = entity->dev_handle;
274
275 if (dev->funcs->size != NULL)
276 result = dev->funcs->size(entity, length);
277 else
278 result = IO_NOT_SUPPORTED;
279
280 return result;
281}
282
283
284/* Read data from an IO entity */
285int io_read(uintptr_t handle,
286 uintptr_t buffer,
287 size_t length,
288 size_t *length_read)
289{
290 int result = IO_FAIL;
291 assert(is_valid_entity(handle) && (buffer != (uintptr_t)NULL));
292
293 io_entity_t *entity = (io_entity_t *)handle;
294
295 io_dev_info_t *dev = entity->dev_handle;
296
297 if (dev->funcs->read != NULL)
298 result = dev->funcs->read(entity, buffer, length, length_read);
299 else
300 result = IO_NOT_SUPPORTED;
301
302 return result;
303}
304
305
306/* Write data to an IO entity */
307int io_write(uintptr_t handle,
308 const uintptr_t buffer,
309 size_t length,
310 size_t *length_written)
311{
312 int result = IO_FAIL;
313 assert(is_valid_entity(handle) && (buffer != (uintptr_t)NULL));
314
315 io_entity_t *entity = (io_entity_t *)handle;
316
317 io_dev_info_t *dev = entity->dev_handle;
318
319 if (dev->funcs->write != NULL) {
320 result = dev->funcs->write(entity, buffer, length,
321 length_written);
322 } else
323 result = IO_NOT_SUPPORTED;
324
325 return result;
326}
327
328
329/* Close an IO entity */
330int io_close(uintptr_t handle)
331{
332 int result = IO_FAIL;
333 assert(is_valid_entity(handle));
334
335 io_entity_t *entity = (io_entity_t *)handle;
336
337 io_dev_info_t *dev = entity->dev_handle;
338
339 if (dev->funcs->close != NULL)
340 result = dev->funcs->close(entity);
341 else {
342 /* Absence of registered function implies NOP here */
343 result = IO_SUCCESS;
344 }
345 /* Ignore improbable free_entity failure */
346 (void)free_entity(entity);
347
348 return result;
349}