1*561cd33eSHarry Liebel /* 2*561cd33eSHarry Liebel * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved. 3*561cd33eSHarry Liebel * 4*561cd33eSHarry Liebel * Redistribution and use in source and binary forms, with or without 5*561cd33eSHarry Liebel * modification, are permitted provided that the following conditions are met: 6*561cd33eSHarry Liebel * 7*561cd33eSHarry Liebel * Redistributions of source code must retain the above copyright notice, this 8*561cd33eSHarry Liebel * list of conditions and the following disclaimer. 9*561cd33eSHarry Liebel * 10*561cd33eSHarry Liebel * Redistributions in binary form must reproduce the above copyright notice, 11*561cd33eSHarry Liebel * this list of conditions and the following disclaimer in the documentation 12*561cd33eSHarry Liebel * and/or other materials provided with the distribution. 13*561cd33eSHarry Liebel * 14*561cd33eSHarry Liebel * Neither the name of ARM nor the names of its contributors may be used 15*561cd33eSHarry Liebel * to endorse or promote products derived from this software without specific 16*561cd33eSHarry Liebel * prior written permission. 17*561cd33eSHarry Liebel * 18*561cd33eSHarry Liebel * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19*561cd33eSHarry Liebel * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*561cd33eSHarry Liebel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*561cd33eSHarry Liebel * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22*561cd33eSHarry Liebel * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*561cd33eSHarry Liebel * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*561cd33eSHarry Liebel * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*561cd33eSHarry Liebel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*561cd33eSHarry Liebel * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*561cd33eSHarry Liebel * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*561cd33eSHarry Liebel * POSSIBILITY OF SUCH DAMAGE. 29*561cd33eSHarry Liebel */ 30*561cd33eSHarry Liebel 31*561cd33eSHarry Liebel #include <assert.h> 32*561cd33eSHarry Liebel #include <string.h> 33*561cd33eSHarry Liebel #include "io_storage.h" 34*561cd33eSHarry Liebel #include "io_driver.h" 35*561cd33eSHarry Liebel #include "debug.h" 36*561cd33eSHarry Liebel 37*561cd33eSHarry Liebel /* As we need to be able to keep state for seek, only one file can be open 38*561cd33eSHarry Liebel * at a time. Make this a structure and point to the entity->info. When we 39*561cd33eSHarry Liebel * can malloc memory we can change this to support more open files. 40*561cd33eSHarry Liebel */ 41*561cd33eSHarry Liebel typedef struct { 42*561cd33eSHarry Liebel /* Use the 'in_use' flag as any value for base and file_pos could be 43*561cd33eSHarry Liebel * valid. 44*561cd33eSHarry Liebel */ 45*561cd33eSHarry Liebel int in_use; 46*561cd33eSHarry Liebel size_t base; 47*561cd33eSHarry Liebel size_t file_pos; 48*561cd33eSHarry Liebel } file_state; 49*561cd33eSHarry Liebel 50*561cd33eSHarry Liebel static file_state current_file = {0}; 51*561cd33eSHarry Liebel 52*561cd33eSHarry Liebel /* Identify the device type as memmap */ 53*561cd33eSHarry Liebel io_type device_type_memmap(void) 54*561cd33eSHarry Liebel { 55*561cd33eSHarry Liebel return IO_TYPE_MEMMAP; 56*561cd33eSHarry Liebel } 57*561cd33eSHarry Liebel 58*561cd33eSHarry Liebel /* Memmap device functions */ 59*561cd33eSHarry Liebel static int memmap_dev_open(void *spec, struct io_dev_info **dev_info); 60*561cd33eSHarry Liebel static int memmap_block_open(struct io_dev_info *dev_info, const void *spec, 61*561cd33eSHarry Liebel struct io_entity *entity); 62*561cd33eSHarry Liebel static int memmap_block_seek(struct io_entity *entity, int mode, 63*561cd33eSHarry Liebel ssize_t offset); 64*561cd33eSHarry Liebel static int memmap_block_read(struct io_entity *entity, void *buffer, 65*561cd33eSHarry Liebel size_t length, size_t *length_read); 66*561cd33eSHarry Liebel static int memmap_block_write(struct io_entity *entity, const void *buffer, 67*561cd33eSHarry Liebel size_t length, size_t *length_written); 68*561cd33eSHarry Liebel static int memmap_block_close(struct io_entity *entity); 69*561cd33eSHarry Liebel static int memmap_dev_close(struct io_dev_info *dev_info); 70*561cd33eSHarry Liebel 71*561cd33eSHarry Liebel 72*561cd33eSHarry Liebel static struct io_dev_connector memmap_dev_connector = { 73*561cd33eSHarry Liebel .dev_open = memmap_dev_open 74*561cd33eSHarry Liebel }; 75*561cd33eSHarry Liebel 76*561cd33eSHarry Liebel 77*561cd33eSHarry Liebel static struct io_dev_funcs memmap_dev_funcs = { 78*561cd33eSHarry Liebel .type = device_type_memmap, 79*561cd33eSHarry Liebel .open = memmap_block_open, 80*561cd33eSHarry Liebel .seek = memmap_block_seek, 81*561cd33eSHarry Liebel .size = NULL, 82*561cd33eSHarry Liebel .read = memmap_block_read, 83*561cd33eSHarry Liebel .write = memmap_block_write, 84*561cd33eSHarry Liebel .close = memmap_block_close, 85*561cd33eSHarry Liebel .dev_init = NULL, 86*561cd33eSHarry Liebel .dev_close = memmap_dev_close, 87*561cd33eSHarry Liebel }; 88*561cd33eSHarry Liebel 89*561cd33eSHarry Liebel 90*561cd33eSHarry Liebel static struct io_dev_info memmap_dev_info = { 91*561cd33eSHarry Liebel .funcs = &memmap_dev_funcs, 92*561cd33eSHarry Liebel .info = (uintptr_t)NULL 93*561cd33eSHarry Liebel }; 94*561cd33eSHarry Liebel 95*561cd33eSHarry Liebel 96*561cd33eSHarry Liebel /* Open a connection to the memmap device */ 97*561cd33eSHarry Liebel static int memmap_dev_open(void *spec __attribute__((unused)), 98*561cd33eSHarry Liebel struct io_dev_info **dev_info) 99*561cd33eSHarry Liebel { 100*561cd33eSHarry Liebel assert(dev_info != NULL); 101*561cd33eSHarry Liebel *dev_info = &memmap_dev_info; 102*561cd33eSHarry Liebel 103*561cd33eSHarry Liebel return IO_SUCCESS; 104*561cd33eSHarry Liebel } 105*561cd33eSHarry Liebel 106*561cd33eSHarry Liebel 107*561cd33eSHarry Liebel 108*561cd33eSHarry Liebel /* Close a connection to the memmap device */ 109*561cd33eSHarry Liebel static int memmap_dev_close(struct io_dev_info *dev_info) 110*561cd33eSHarry Liebel { 111*561cd33eSHarry Liebel /* NOP */ 112*561cd33eSHarry Liebel /* TODO: Consider tracking open files and cleaning them up here */ 113*561cd33eSHarry Liebel return IO_SUCCESS; 114*561cd33eSHarry Liebel } 115*561cd33eSHarry Liebel 116*561cd33eSHarry Liebel 117*561cd33eSHarry Liebel /* Open a file on the memmap device */ 118*561cd33eSHarry Liebel /* TODO: Can we do any sensible limit checks on requested memory */ 119*561cd33eSHarry Liebel static int memmap_block_open(struct io_dev_info *dev_info, const void *spec, 120*561cd33eSHarry Liebel struct io_entity *entity) 121*561cd33eSHarry Liebel { 122*561cd33eSHarry Liebel int result = IO_FAIL; 123*561cd33eSHarry Liebel const io_block_spec *block_spec = (io_block_spec *)spec; 124*561cd33eSHarry Liebel 125*561cd33eSHarry Liebel /* Since we need to track open state for seek() we only allow one open 126*561cd33eSHarry Liebel * spec at a time. When we have dynamic memory we can malloc and set 127*561cd33eSHarry Liebel * entity->info. 128*561cd33eSHarry Liebel */ 129*561cd33eSHarry Liebel if (current_file.in_use == 0) { 130*561cd33eSHarry Liebel assert(block_spec != NULL); 131*561cd33eSHarry Liebel assert(entity != NULL); 132*561cd33eSHarry Liebel 133*561cd33eSHarry Liebel current_file.in_use = 1; 134*561cd33eSHarry Liebel current_file.base = block_spec->offset; 135*561cd33eSHarry Liebel /* File cursor offset for seek and incremental reads etc. */ 136*561cd33eSHarry Liebel current_file.file_pos = 0; 137*561cd33eSHarry Liebel entity->info = (uintptr_t)¤t_file; 138*561cd33eSHarry Liebel result = IO_SUCCESS; 139*561cd33eSHarry Liebel } else { 140*561cd33eSHarry Liebel ERROR("A Memmap device is already active. Close first.\n"); 141*561cd33eSHarry Liebel result = IO_RESOURCES_EXHAUSTED; 142*561cd33eSHarry Liebel } 143*561cd33eSHarry Liebel 144*561cd33eSHarry Liebel return result; 145*561cd33eSHarry Liebel } 146*561cd33eSHarry Liebel 147*561cd33eSHarry Liebel 148*561cd33eSHarry Liebel /* Seek to a particular file offset on the memmap device */ 149*561cd33eSHarry Liebel static int memmap_block_seek(struct io_entity *entity, int mode, ssize_t offset) 150*561cd33eSHarry Liebel { 151*561cd33eSHarry Liebel int result = IO_FAIL; 152*561cd33eSHarry Liebel 153*561cd33eSHarry Liebel /* We only support IO_SEEK_SET for the moment. */ 154*561cd33eSHarry Liebel if (mode == IO_SEEK_SET) { 155*561cd33eSHarry Liebel assert(entity != NULL); 156*561cd33eSHarry Liebel 157*561cd33eSHarry Liebel /* TODO: can we do some basic limit checks on seek? */ 158*561cd33eSHarry Liebel ((file_state *)entity->info)->file_pos = offset; 159*561cd33eSHarry Liebel result = IO_SUCCESS; 160*561cd33eSHarry Liebel } else { 161*561cd33eSHarry Liebel result = IO_FAIL; 162*561cd33eSHarry Liebel } 163*561cd33eSHarry Liebel 164*561cd33eSHarry Liebel return result; 165*561cd33eSHarry Liebel } 166*561cd33eSHarry Liebel 167*561cd33eSHarry Liebel 168*561cd33eSHarry Liebel /* Read data from a file on the memmap device */ 169*561cd33eSHarry Liebel static int memmap_block_read(struct io_entity *entity, void *buffer, 170*561cd33eSHarry Liebel size_t length, size_t *length_read) 171*561cd33eSHarry Liebel { 172*561cd33eSHarry Liebel file_state *fp; 173*561cd33eSHarry Liebel 174*561cd33eSHarry Liebel assert(entity != NULL); 175*561cd33eSHarry Liebel assert(buffer != NULL); 176*561cd33eSHarry Liebel assert(length_read != NULL); 177*561cd33eSHarry Liebel 178*561cd33eSHarry Liebel fp = (file_state *)entity->info; 179*561cd33eSHarry Liebel 180*561cd33eSHarry Liebel memcpy(buffer, (void *)(fp->base + fp->file_pos), length); 181*561cd33eSHarry Liebel 182*561cd33eSHarry Liebel *length_read = length; 183*561cd33eSHarry Liebel /* advance the file 'cursor' for incremental reads */ 184*561cd33eSHarry Liebel fp->file_pos += length; 185*561cd33eSHarry Liebel 186*561cd33eSHarry Liebel return IO_SUCCESS; 187*561cd33eSHarry Liebel } 188*561cd33eSHarry Liebel 189*561cd33eSHarry Liebel 190*561cd33eSHarry Liebel /* Write data to a file on the memmap device */ 191*561cd33eSHarry Liebel static int memmap_block_write(struct io_entity *entity, const void *buffer, 192*561cd33eSHarry Liebel size_t length, size_t *length_written) 193*561cd33eSHarry Liebel { 194*561cd33eSHarry Liebel file_state *fp; 195*561cd33eSHarry Liebel 196*561cd33eSHarry Liebel assert(entity != NULL); 197*561cd33eSHarry Liebel assert(buffer != NULL); 198*561cd33eSHarry Liebel assert(length_written != NULL); 199*561cd33eSHarry Liebel 200*561cd33eSHarry Liebel fp = (file_state *)entity->info; 201*561cd33eSHarry Liebel 202*561cd33eSHarry Liebel memcpy((void *)(fp->base + fp->file_pos), buffer, length); 203*561cd33eSHarry Liebel 204*561cd33eSHarry Liebel *length_written = length; 205*561cd33eSHarry Liebel 206*561cd33eSHarry Liebel /* advance the file 'cursor' for incremental writes */ 207*561cd33eSHarry Liebel fp->file_pos += length; 208*561cd33eSHarry Liebel 209*561cd33eSHarry Liebel return IO_SUCCESS; 210*561cd33eSHarry Liebel } 211*561cd33eSHarry Liebel 212*561cd33eSHarry Liebel 213*561cd33eSHarry Liebel /* Close a file on the memmap device */ 214*561cd33eSHarry Liebel static int memmap_block_close(struct io_entity *entity) 215*561cd33eSHarry Liebel { 216*561cd33eSHarry Liebel assert(entity != NULL); 217*561cd33eSHarry Liebel 218*561cd33eSHarry Liebel entity->info = 0; 219*561cd33eSHarry Liebel 220*561cd33eSHarry Liebel /* This would be a mem free() if we had malloc.*/ 221*561cd33eSHarry Liebel memset((void *)¤t_file, 0, sizeof(current_file)); 222*561cd33eSHarry Liebel 223*561cd33eSHarry Liebel return IO_SUCCESS; 224*561cd33eSHarry Liebel } 225*561cd33eSHarry Liebel 226*561cd33eSHarry Liebel 227*561cd33eSHarry Liebel /* Exported functions */ 228*561cd33eSHarry Liebel 229*561cd33eSHarry Liebel /* Register the memmap driver with the IO abstraction */ 230*561cd33eSHarry Liebel int register_io_dev_memmap(struct io_dev_connector **dev_con) 231*561cd33eSHarry Liebel { 232*561cd33eSHarry Liebel int result = IO_FAIL; 233*561cd33eSHarry Liebel assert(dev_con != NULL); 234*561cd33eSHarry Liebel 235*561cd33eSHarry Liebel result = io_register_device(&memmap_dev_info); 236*561cd33eSHarry Liebel if (result == IO_SUCCESS) 237*561cd33eSHarry Liebel *dev_con = &memmap_dev_connector; 238*561cd33eSHarry Liebel 239*561cd33eSHarry Liebel return result; 240*561cd33eSHarry Liebel } 241