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