Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 1 | /* |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 2 | * Copyright (c) 2014-2020, ARM Limited and Contributors. All rights reserved. |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 8 | #include <string.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | |
| 10 | #include <platform_def.h> |
| 11 | |
| 12 | #include <common/debug.h> |
| 13 | #include <drivers/io/io_driver.h> |
| 14 | #include <drivers/io/io_memmap.h> |
| 15 | #include <drivers/io/io_storage.h> |
| 16 | #include <lib/utils.h> |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 17 | |
| 18 | /* As we need to be able to keep state for seek, only one file can be open |
| 19 | * at a time. Make this a structure and point to the entity->info. When we |
| 20 | * can malloc memory we can change this to support more open files. |
| 21 | */ |
| 22 | typedef struct { |
| 23 | /* Use the 'in_use' flag as any value for base and file_pos could be |
| 24 | * valid. |
| 25 | */ |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 26 | int in_use; |
| 27 | uintptr_t base; |
| 28 | unsigned long long file_pos; |
| 29 | unsigned long long size; |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 30 | } memmap_file_state_t; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 31 | |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 32 | static memmap_file_state_t current_memmap_file = {0}; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 33 | |
| 34 | /* Identify the device type as memmap */ |
Roberto Vargas | 0571270 | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 35 | static io_type_t device_type_memmap(void) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 36 | { |
| 37 | return IO_TYPE_MEMMAP; |
| 38 | } |
| 39 | |
| 40 | /* Memmap device functions */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 41 | static int memmap_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info); |
| 42 | static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 43 | io_entity_t *entity); |
| 44 | static int memmap_block_seek(io_entity_t *entity, int mode, |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 45 | signed long long offset); |
Gerald Lejeune | 2a1f2d1 | 2015-03-18 14:41:42 +0100 | [diff] [blame] | 46 | static int memmap_block_len(io_entity_t *entity, size_t *length); |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 47 | static int memmap_block_read(io_entity_t *entity, uintptr_t buffer, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 48 | size_t length, size_t *length_read); |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 49 | static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 50 | size_t length, size_t *length_written); |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 51 | static int memmap_block_close(io_entity_t *entity); |
| 52 | static int memmap_dev_close(io_dev_info_t *dev_info); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 53 | |
| 54 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 55 | static const io_dev_connector_t memmap_dev_connector = { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 56 | .dev_open = memmap_dev_open |
| 57 | }; |
| 58 | |
| 59 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 60 | static const io_dev_funcs_t memmap_dev_funcs = { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 61 | .type = device_type_memmap, |
| 62 | .open = memmap_block_open, |
| 63 | .seek = memmap_block_seek, |
Gerald Lejeune | 2a1f2d1 | 2015-03-18 14:41:42 +0100 | [diff] [blame] | 64 | .size = memmap_block_len, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 65 | .read = memmap_block_read, |
| 66 | .write = memmap_block_write, |
| 67 | .close = memmap_block_close, |
| 68 | .dev_init = NULL, |
| 69 | .dev_close = memmap_dev_close, |
| 70 | }; |
| 71 | |
| 72 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 73 | /* No state associated with this device so structure can be const */ |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 74 | static io_dev_info_t memmap_dev_info = { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 75 | .funcs = &memmap_dev_funcs, |
| 76 | .info = (uintptr_t)NULL |
| 77 | }; |
| 78 | |
| 79 | |
| 80 | /* Open a connection to the memmap device */ |
Soren Brinkmann | 46dd170 | 2016-01-14 10:11:05 -0800 | [diff] [blame] | 81 | static int memmap_dev_open(const uintptr_t dev_spec __unused, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 82 | io_dev_info_t **dev_info) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 83 | { |
| 84 | assert(dev_info != NULL); |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 85 | *dev_info = &memmap_dev_info; |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 86 | return 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | |
| 90 | |
| 91 | /* Close a connection to the memmap device */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 92 | static int memmap_dev_close(io_dev_info_t *dev_info) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 93 | { |
| 94 | /* NOP */ |
| 95 | /* TODO: Consider tracking open files and cleaning them up here */ |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 96 | return 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | |
| 100 | /* Open a file on the memmap device */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 101 | static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 102 | io_entity_t *entity) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 103 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 104 | int result = -ENOMEM; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 105 | const io_block_spec_t *block_spec = (io_block_spec_t *)spec; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 106 | |
| 107 | /* Since we need to track open state for seek() we only allow one open |
| 108 | * spec at a time. When we have dynamic memory we can malloc and set |
| 109 | * entity->info. |
| 110 | */ |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 111 | if (current_memmap_file.in_use == 0) { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 112 | assert(block_spec != NULL); |
| 113 | assert(entity != NULL); |
| 114 | |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 115 | current_memmap_file.in_use = 1; |
| 116 | current_memmap_file.base = block_spec->offset; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 117 | /* File cursor offset for seek and incremental reads etc. */ |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 118 | current_memmap_file.file_pos = 0; |
| 119 | current_memmap_file.size = block_spec->length; |
| 120 | entity->info = (uintptr_t)¤t_memmap_file; |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 121 | result = 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 122 | } else { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 123 | WARN("A Memmap device is already active. Close first.\n"); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | return result; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /* Seek to a particular file offset on the memmap device */ |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 131 | static int memmap_block_seek(io_entity_t *entity, int mode, |
| 132 | signed long long offset) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 133 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 134 | int result = -ENOENT; |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 135 | memmap_file_state_t *fp; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 136 | |
| 137 | /* We only support IO_SEEK_SET for the moment. */ |
| 138 | if (mode == IO_SEEK_SET) { |
| 139 | assert(entity != NULL); |
| 140 | |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 141 | fp = (memmap_file_state_t *) entity->info; |
Jeenu Viswambharan | e6bae8c | 2017-02-13 13:06:18 +0000 | [diff] [blame] | 142 | |
| 143 | /* Assert that new file position is valid */ |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 144 | assert((offset >= 0) && |
| 145 | ((unsigned long long)offset < fp->size)); |
Jeenu Viswambharan | e6bae8c | 2017-02-13 13:06:18 +0000 | [diff] [blame] | 146 | |
| 147 | /* Reset file position */ |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 148 | fp->file_pos = (unsigned long long)offset; |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 149 | result = 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | return result; |
| 153 | } |
| 154 | |
| 155 | |
Gerald Lejeune | 2a1f2d1 | 2015-03-18 14:41:42 +0100 | [diff] [blame] | 156 | /* Return the size of a file on the memmap device */ |
| 157 | static int memmap_block_len(io_entity_t *entity, size_t *length) |
| 158 | { |
| 159 | assert(entity != NULL); |
| 160 | assert(length != NULL); |
| 161 | |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 162 | *length = (size_t)((memmap_file_state_t *)entity->info)->size; |
Gerald Lejeune | 2a1f2d1 | 2015-03-18 14:41:42 +0100 | [diff] [blame] | 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 168 | /* Read data from a file on the memmap device */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 169 | static int memmap_block_read(io_entity_t *entity, uintptr_t buffer, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 170 | size_t length, size_t *length_read) |
| 171 | { |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 172 | memmap_file_state_t *fp; |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 173 | unsigned long long pos_after; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 174 | |
| 175 | assert(entity != NULL); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 176 | assert(length_read != NULL); |
| 177 | |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 178 | fp = (memmap_file_state_t *) entity->info; |
Jeenu Viswambharan | e6bae8c | 2017-02-13 13:06:18 +0000 | [diff] [blame] | 179 | |
| 180 | /* Assert that file position is valid for this read operation */ |
| 181 | pos_after = fp->file_pos + length; |
| 182 | assert((pos_after >= fp->file_pos) && (pos_after <= fp->size)); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 183 | |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 184 | memcpy((void *)buffer, |
| 185 | (void *)((uintptr_t)(fp->base + fp->file_pos)), length); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 186 | |
| 187 | *length_read = length; |
Jeenu Viswambharan | e6bae8c | 2017-02-13 13:06:18 +0000 | [diff] [blame] | 188 | |
| 189 | /* Set file position after read */ |
| 190 | fp->file_pos = pos_after; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 191 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 192 | return 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | |
| 196 | /* Write data to a file on the memmap device */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 197 | static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 198 | size_t length, size_t *length_written) |
| 199 | { |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 200 | memmap_file_state_t *fp; |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 201 | unsigned long long pos_after; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 202 | |
| 203 | assert(entity != NULL); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 204 | assert(length_written != NULL); |
| 205 | |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 206 | fp = (memmap_file_state_t *) entity->info; |
Jeenu Viswambharan | e6bae8c | 2017-02-13 13:06:18 +0000 | [diff] [blame] | 207 | |
| 208 | /* Assert that file position is valid for this write operation */ |
| 209 | pos_after = fp->file_pos + length; |
| 210 | assert((pos_after >= fp->file_pos) && (pos_after <= fp->size)); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 211 | |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 212 | memcpy((void *)((uintptr_t)(fp->base + fp->file_pos)), |
| 213 | (void *)buffer, length); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 214 | |
| 215 | *length_written = length; |
| 216 | |
Jeenu Viswambharan | e6bae8c | 2017-02-13 13:06:18 +0000 | [diff] [blame] | 217 | /* Set file position after write */ |
| 218 | fp->file_pos = pos_after; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 219 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 220 | return 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | |
| 224 | /* Close a file on the memmap device */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 225 | static int memmap_block_close(io_entity_t *entity) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 226 | { |
| 227 | assert(entity != NULL); |
| 228 | |
| 229 | entity->info = 0; |
| 230 | |
| 231 | /* This would be a mem free() if we had malloc.*/ |
johpow01 | 2103128 | 2020-07-01 17:09:57 -0500 | [diff] [blame] | 232 | zeromem((void *)¤t_memmap_file, sizeof(current_memmap_file)); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 233 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 234 | return 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | |
| 238 | /* Exported functions */ |
| 239 | |
| 240 | /* Register the memmap driver with the IO abstraction */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 241 | int register_io_dev_memmap(const io_dev_connector_t **dev_con) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 242 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 243 | int result; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 244 | assert(dev_con != NULL); |
| 245 | |
| 246 | result = io_register_device(&memmap_dev_info); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 247 | if (result == 0) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 248 | *dev_con = &memmap_dev_connector; |
| 249 | |
| 250 | return result; |
| 251 | } |