1 /*
2  * Copyright (c) 2022 Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the License); you may
7  * not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <assert.h>
20 #include <errno.h>
21 #include <stdbool.h>
22 
23 #include "io_defs.h"
24 #include "io_driver.h"
25 
26 /* Storage for a fixed maximum number of IO entities, definable by platform */
27 static io_entity_t entity_pool[MAX_IO_HANDLES];
28 
29 /* Simple way of tracking used storage - each entry is NULL or a pointer to an
30  * entity */
31 static io_entity_t *entity_map[MAX_IO_HANDLES];
32 
33 /* Track number of allocated entities */
34 static unsigned int entity_count;
35 
36 /* Array of fixed maximum of registered devices, definable by platform */
37 static const io_dev_info_t *devices[MAX_IO_DEVICES];
38 
39 /* Number of currently registered devices */
40 static unsigned int dev_count;
41 
42 /* Return a boolean value indicating whether a device connector is valid */
is_valid_dev_connector(const io_dev_connector_t * dev_con)43 static bool is_valid_dev_connector(const io_dev_connector_t *dev_con) {
44     return (dev_con != NULL) && (dev_con->dev_open != NULL);
45 }
46 
47 /* Return a boolean value indicating whether a device handle is valid */
is_valid_dev(const uintptr_t dev_handle)48 static bool is_valid_dev(const uintptr_t dev_handle) {
49     const io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
50 
51     return (dev != NULL) && (dev->funcs != NULL) &&
52            (dev->funcs->type != NULL) && (dev->funcs->type() < IO_TYPE_MAX);
53 }
54 
55 /* Return a boolean value indicating whether an IO entity is valid */
is_valid_entity(const uintptr_t handle)56 static bool is_valid_entity(const uintptr_t handle) {
57     const io_entity_t *entity = (io_entity_t *)handle;
58 
59     return (entity != NULL) && (is_valid_dev((uintptr_t)entity->dev_handle));
60 }
61 
62 /* Return a boolean value indicating whether a seek mode is valid */
is_valid_seek_mode(io_seek_mode_t mode)63 static bool is_valid_seek_mode(io_seek_mode_t mode) {
64     return ((mode != IO_SEEK_INVALID) && (mode < IO_SEEK_MAX));
65 }
66 
67 /* Open a connection to a specific device */
io_storage_dev_open(const io_dev_connector_t * dev_con,const uintptr_t dev_spec,io_dev_info_t ** dev_info)68 static int io_storage_dev_open(const io_dev_connector_t *dev_con,
69                                const uintptr_t dev_spec,
70                                io_dev_info_t **dev_info) {
71     assert(dev_info != NULL);
72     assert(is_valid_dev_connector(dev_con));
73 
74     return dev_con->dev_open(dev_spec, dev_info);
75 }
76 
77 /* Set a handle to track an entity */
set_handle(uintptr_t * handle,io_entity_t * entity)78 static void set_handle(uintptr_t *handle, io_entity_t *entity) {
79     assert(handle != NULL);
80     *handle = (uintptr_t)entity;
81 }
82 
83 /* Locate an entity in the pool, specified by address */
find_first_entity(const io_entity_t * entity,unsigned int * index_out)84 static int find_first_entity(const io_entity_t *entity,
85                              unsigned int *index_out) {
86     int result = -ENOENT;
87     for (unsigned int index = 0; index < MAX_IO_HANDLES; ++index) {
88         if (entity_map[index] == entity) {
89             result = 0;
90             *index_out = index;
91             break;
92         }
93     }
94     return result;
95 }
96 
97 /* Allocate an entity from the pool and return a pointer to it */
allocate_entity(io_entity_t ** entity)98 static int allocate_entity(io_entity_t **entity) {
99     int result = -ENOMEM;
100     assert(entity != NULL);
101 
102     if (entity_count < MAX_IO_HANDLES) {
103         unsigned int index = 0;
104         result = find_first_entity(NULL, &index);
105         assert(result == 0);
106         *entity = &entity_pool[index];
107         entity_map[index] = &entity_pool[index];
108         ++entity_count;
109     }
110 
111     return result;
112 }
113 
114 /* Release an entity back to the pool */
free_entity(const io_entity_t * entity)115 static int free_entity(const io_entity_t *entity) {
116     int result;
117     unsigned int index = 0;
118     assert(entity != NULL);
119 
120     result = find_first_entity(entity, &index);
121     if (result == 0) {
122         entity_map[index] = NULL;
123         --entity_count;
124     }
125 
126     return result;
127 }
128 
129 /* Exported API */
130 
131 /* Register an io device */
io_register_device(const io_dev_info_t * dev_info)132 int io_register_device(const io_dev_info_t *dev_info) {
133     int result = -ENOMEM;
134     assert(dev_info != NULL);
135 
136     if (dev_count < MAX_IO_DEVICES) {
137         devices[dev_count] = dev_info;
138         dev_count++;
139         result = 0;
140     }
141 
142     return result;
143 }
144 
145 /* Open a connection to an IO device */
io_dev_open(const io_dev_connector_t * dev_con,const uintptr_t dev_spec,uintptr_t * handle)146 int io_dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec,
147                 uintptr_t *handle) {
148     assert(handle != NULL);
149     return io_storage_dev_open(dev_con, dev_spec, (io_dev_info_t **)handle);
150 }
151 
152 /* Initialise an IO device explicitly - to permit lazy initialisation or
153  * re-initialisation */
io_dev_init(uintptr_t dev_handle,const uintptr_t init_params)154 int io_dev_init(uintptr_t dev_handle, const uintptr_t init_params) {
155     int result = 0;
156     assert(dev_handle != (uintptr_t)NULL);
157     assert(is_valid_dev(dev_handle));
158 
159     io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
160 
161     /* Absence of registered function implies NOP here */
162     if (dev->funcs->dev_init != NULL) {
163         result = dev->funcs->dev_init(dev, init_params);
164     }
165 
166     return result;
167 }
168 
169 /* Close a connection to a device */
io_dev_close(uintptr_t dev_handle)170 int io_dev_close(uintptr_t dev_handle) {
171     int result = 0;
172     assert(dev_handle != (uintptr_t)NULL);
173     assert(is_valid_dev(dev_handle));
174 
175     io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
176 
177     /* Absence of registered function implies NOP here */
178     if (dev->funcs->dev_close != NULL) {
179         result = dev->funcs->dev_close(dev);
180     }
181 
182     return result;
183 }
184 
185 /* Synchronous operations */
186 
187 /* Open an IO entity */
io_open(uintptr_t dev_handle,const uintptr_t spec,uintptr_t * handle)188 int io_open(uintptr_t dev_handle, const uintptr_t spec, uintptr_t *handle) {
189     int result;
190     assert((spec != (uintptr_t)NULL) && (handle != NULL));
191     assert(is_valid_dev(dev_handle));
192 
193     io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
194     io_entity_t *entity;
195 
196     result = allocate_entity(&entity);
197 
198     if (result == 0) {
199         assert(dev->funcs->open != NULL);
200         result = dev->funcs->open(dev, spec, entity);
201 
202         if (result == 0) {
203             entity->dev_handle = dev;
204             set_handle(handle, entity);
205         } else
206             free_entity(entity);
207     }
208     return result;
209 }
210 
211 /* Seek to a specific position in an IO entity */
io_seek(uintptr_t handle,io_seek_mode_t mode,int32_t offset)212 int io_seek(uintptr_t handle, io_seek_mode_t mode, int32_t offset) {
213     int result = -ENODEV;
214     assert(is_valid_entity(handle) && is_valid_seek_mode(mode));
215 
216     io_entity_t *entity = (io_entity_t *)handle;
217 
218     io_dev_info_t *dev = entity->dev_handle;
219 
220     if (dev->funcs->seek != NULL)
221         result = dev->funcs->seek(entity, mode, offset);
222 
223     return result;
224 }
225 
226 /* Determine the length of an IO entity */
io_size(uintptr_t handle,size_t * length)227 int io_size(uintptr_t handle, size_t *length) {
228     int result = -ENODEV;
229     assert(is_valid_entity(handle) && (length != NULL));
230 
231     io_entity_t *entity = (io_entity_t *)handle;
232 
233     io_dev_info_t *dev = entity->dev_handle;
234 
235     if (dev->funcs->size != NULL) result = dev->funcs->size(entity, length);
236 
237     return result;
238 }
239 
240 /* Read data from an IO entity */
io_read(uintptr_t handle,uintptr_t buffer,size_t length,size_t * length_read)241 int io_read(uintptr_t handle, uintptr_t buffer, size_t length,
242             size_t *length_read) {
243     int result = -ENODEV;
244     assert(is_valid_entity(handle));
245 
246     io_entity_t *entity = (io_entity_t *)handle;
247 
248     io_dev_info_t *dev = entity->dev_handle;
249 
250     if (dev->funcs->read != NULL)
251         result = dev->funcs->read(entity, buffer, length, length_read);
252 
253     return result;
254 }
255 
256 /* Write data to an IO entity */
io_write(uintptr_t handle,const uintptr_t buffer,size_t length,size_t * length_written)257 int io_write(uintptr_t handle, const uintptr_t buffer, size_t length,
258              size_t *length_written) {
259     int result = -ENODEV;
260     assert(is_valid_entity(handle));
261 
262     io_entity_t *entity = (io_entity_t *)handle;
263 
264     io_dev_info_t *dev = entity->dev_handle;
265 
266     if (dev->funcs->write != NULL) {
267         result = dev->funcs->write(entity, buffer, length, length_written);
268     }
269 
270     return result;
271 }
272 
273 /* Close an IO entity */
io_close(uintptr_t handle)274 int io_close(uintptr_t handle) {
275     int result = 0;
276     assert(is_valid_entity(handle));
277 
278     io_entity_t *entity = (io_entity_t *)handle;
279 
280     io_dev_info_t *dev = entity->dev_handle;
281 
282     /* Absence of registered function implies NOP here */
283     if (dev->funcs->close != NULL) result = dev->funcs->close(entity);
284 
285     /* Ignore improbable free_entity failure */
286     (void)free_entity(entity);
287 
288     return result;
289 }
290