1561cd33eSHarry Liebel /* 27fabe1a8SRoberto Vargas * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved. 3561cd33eSHarry Liebel * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5561cd33eSHarry Liebel */ 6561cd33eSHarry Liebel 7561cd33eSHarry Liebel #include <assert.h> 897043ac9SDan Handley #include <string.h> 909d40e0eSAntonio Nino Diaz 1009d40e0eSAntonio Nino Diaz #include <platform_def.h> 1109d40e0eSAntonio Nino Diaz 1209d40e0eSAntonio Nino Diaz #include <common/debug.h> 1309d40e0eSAntonio Nino Diaz #include <drivers/io/io_driver.h> 1409d40e0eSAntonio Nino Diaz #include <drivers/io/io_memmap.h> 1509d40e0eSAntonio Nino Diaz #include <drivers/io/io_storage.h> 1609d40e0eSAntonio Nino Diaz #include <lib/utils.h> 17561cd33eSHarry Liebel 18561cd33eSHarry Liebel /* As we need to be able to keep state for seek, only one file can be open 19561cd33eSHarry Liebel * at a time. Make this a structure and point to the entity->info. When we 20561cd33eSHarry Liebel * can malloc memory we can change this to support more open files. 21561cd33eSHarry Liebel */ 22561cd33eSHarry Liebel typedef struct { 23561cd33eSHarry Liebel /* Use the 'in_use' flag as any value for base and file_pos could be 24561cd33eSHarry Liebel * valid. 25561cd33eSHarry Liebel */ 26561cd33eSHarry Liebel int in_use; 27625de1d4SDan Handley uintptr_t base; 28*70cb0bffSYann Gautier unsigned long long file_pos; 29*70cb0bffSYann Gautier unsigned long long size; 30fb037bfbSDan Handley } file_state_t; 31561cd33eSHarry Liebel 32fb037bfbSDan Handley static file_state_t current_file = {0}; 33561cd33eSHarry Liebel 34561cd33eSHarry Liebel /* Identify the device type as memmap */ 357fabe1a8SRoberto Vargas static io_type_t device_type_memmap(void) 36561cd33eSHarry Liebel { 37561cd33eSHarry Liebel return IO_TYPE_MEMMAP; 38561cd33eSHarry Liebel } 39561cd33eSHarry Liebel 40561cd33eSHarry Liebel /* Memmap device functions */ 41625de1d4SDan Handley static int memmap_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info); 42625de1d4SDan Handley static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec, 43fb037bfbSDan Handley io_entity_t *entity); 44fb037bfbSDan Handley static int memmap_block_seek(io_entity_t *entity, int mode, 45*70cb0bffSYann Gautier signed long long offset); 466d70bfa1SGerald Lejeune static int memmap_block_len(io_entity_t *entity, size_t *length); 47625de1d4SDan Handley static int memmap_block_read(io_entity_t *entity, uintptr_t buffer, 48561cd33eSHarry Liebel size_t length, size_t *length_read); 49625de1d4SDan Handley static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer, 50561cd33eSHarry Liebel size_t length, size_t *length_written); 51fb037bfbSDan Handley static int memmap_block_close(io_entity_t *entity); 52fb037bfbSDan Handley static int memmap_dev_close(io_dev_info_t *dev_info); 53561cd33eSHarry Liebel 54561cd33eSHarry Liebel 55625de1d4SDan Handley static const io_dev_connector_t memmap_dev_connector = { 56561cd33eSHarry Liebel .dev_open = memmap_dev_open 57561cd33eSHarry Liebel }; 58561cd33eSHarry Liebel 59561cd33eSHarry Liebel 60625de1d4SDan Handley static const io_dev_funcs_t memmap_dev_funcs = { 61561cd33eSHarry Liebel .type = device_type_memmap, 62561cd33eSHarry Liebel .open = memmap_block_open, 63561cd33eSHarry Liebel .seek = memmap_block_seek, 646d70bfa1SGerald Lejeune .size = memmap_block_len, 65561cd33eSHarry Liebel .read = memmap_block_read, 66561cd33eSHarry Liebel .write = memmap_block_write, 67561cd33eSHarry Liebel .close = memmap_block_close, 68561cd33eSHarry Liebel .dev_init = NULL, 69561cd33eSHarry Liebel .dev_close = memmap_dev_close, 70561cd33eSHarry Liebel }; 71561cd33eSHarry Liebel 72561cd33eSHarry Liebel 73625de1d4SDan Handley /* No state associated with this device so structure can be const */ 74625de1d4SDan Handley static const io_dev_info_t memmap_dev_info = { 75561cd33eSHarry Liebel .funcs = &memmap_dev_funcs, 76561cd33eSHarry Liebel .info = (uintptr_t)NULL 77561cd33eSHarry Liebel }; 78561cd33eSHarry Liebel 79561cd33eSHarry Liebel 80561cd33eSHarry Liebel /* Open a connection to the memmap device */ 8165cd299fSSoren Brinkmann static int memmap_dev_open(const uintptr_t dev_spec __unused, 82fb037bfbSDan Handley io_dev_info_t **dev_info) 83561cd33eSHarry Liebel { 84561cd33eSHarry Liebel assert(dev_info != NULL); 85625de1d4SDan Handley *dev_info = (io_dev_info_t *)&memmap_dev_info; /* cast away const */ 86561cd33eSHarry Liebel 87e098e244SJuan Castillo return 0; 88561cd33eSHarry Liebel } 89561cd33eSHarry Liebel 90561cd33eSHarry Liebel 91561cd33eSHarry Liebel 92561cd33eSHarry Liebel /* Close a connection to the memmap device */ 93fb037bfbSDan Handley static int memmap_dev_close(io_dev_info_t *dev_info) 94561cd33eSHarry Liebel { 95561cd33eSHarry Liebel /* NOP */ 96561cd33eSHarry Liebel /* TODO: Consider tracking open files and cleaning them up here */ 97e098e244SJuan Castillo return 0; 98561cd33eSHarry Liebel } 99561cd33eSHarry Liebel 100561cd33eSHarry Liebel 101561cd33eSHarry Liebel /* Open a file on the memmap device */ 102625de1d4SDan Handley static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec, 103fb037bfbSDan Handley io_entity_t *entity) 104561cd33eSHarry Liebel { 105e098e244SJuan Castillo int result = -ENOMEM; 106fb037bfbSDan Handley const io_block_spec_t *block_spec = (io_block_spec_t *)spec; 107561cd33eSHarry Liebel 108561cd33eSHarry Liebel /* Since we need to track open state for seek() we only allow one open 109561cd33eSHarry Liebel * spec at a time. When we have dynamic memory we can malloc and set 110561cd33eSHarry Liebel * entity->info. 111561cd33eSHarry Liebel */ 112561cd33eSHarry Liebel if (current_file.in_use == 0) { 113561cd33eSHarry Liebel assert(block_spec != NULL); 114561cd33eSHarry Liebel assert(entity != NULL); 115561cd33eSHarry Liebel 116561cd33eSHarry Liebel current_file.in_use = 1; 117561cd33eSHarry Liebel current_file.base = block_spec->offset; 118561cd33eSHarry Liebel /* File cursor offset for seek and incremental reads etc. */ 119561cd33eSHarry Liebel current_file.file_pos = 0; 1206d70bfa1SGerald Lejeune current_file.size = block_spec->length; 121561cd33eSHarry Liebel entity->info = (uintptr_t)¤t_file; 122e098e244SJuan Castillo result = 0; 123561cd33eSHarry Liebel } else { 12408c28d53SJeenu Viswambharan WARN("A Memmap device is already active. Close first.\n"); 125561cd33eSHarry Liebel } 126561cd33eSHarry Liebel 127561cd33eSHarry Liebel return result; 128561cd33eSHarry Liebel } 129561cd33eSHarry Liebel 130561cd33eSHarry Liebel 131561cd33eSHarry Liebel /* Seek to a particular file offset on the memmap device */ 132*70cb0bffSYann Gautier static int memmap_block_seek(io_entity_t *entity, int mode, 133*70cb0bffSYann Gautier signed long long offset) 134561cd33eSHarry Liebel { 135e098e244SJuan Castillo int result = -ENOENT; 13669c043b2SJeenu Viswambharan file_state_t *fp; 137561cd33eSHarry Liebel 138561cd33eSHarry Liebel /* We only support IO_SEEK_SET for the moment. */ 139561cd33eSHarry Liebel if (mode == IO_SEEK_SET) { 140561cd33eSHarry Liebel assert(entity != NULL); 141561cd33eSHarry Liebel 14269c043b2SJeenu Viswambharan fp = (file_state_t *) entity->info; 14369c043b2SJeenu Viswambharan 14469c043b2SJeenu Viswambharan /* Assert that new file position is valid */ 145*70cb0bffSYann Gautier assert((offset >= 0) && 146*70cb0bffSYann Gautier ((unsigned long long)offset < fp->size)); 14769c043b2SJeenu Viswambharan 14869c043b2SJeenu Viswambharan /* Reset file position */ 149*70cb0bffSYann Gautier fp->file_pos = (unsigned long long)offset; 150e098e244SJuan Castillo result = 0; 151561cd33eSHarry Liebel } 152561cd33eSHarry Liebel 153561cd33eSHarry Liebel return result; 154561cd33eSHarry Liebel } 155561cd33eSHarry Liebel 156561cd33eSHarry Liebel 1576d70bfa1SGerald Lejeune /* Return the size of a file on the memmap device */ 1586d70bfa1SGerald Lejeune static int memmap_block_len(io_entity_t *entity, size_t *length) 1596d70bfa1SGerald Lejeune { 1606d70bfa1SGerald Lejeune assert(entity != NULL); 1616d70bfa1SGerald Lejeune assert(length != NULL); 1626d70bfa1SGerald Lejeune 163*70cb0bffSYann Gautier *length = (size_t)((file_state_t *)entity->info)->size; 1646d70bfa1SGerald Lejeune 1656d70bfa1SGerald Lejeune return 0; 1666d70bfa1SGerald Lejeune } 1676d70bfa1SGerald Lejeune 1686d70bfa1SGerald Lejeune 169561cd33eSHarry Liebel /* Read data from a file on the memmap device */ 170625de1d4SDan Handley static int memmap_block_read(io_entity_t *entity, uintptr_t buffer, 171561cd33eSHarry Liebel size_t length, size_t *length_read) 172561cd33eSHarry Liebel { 173fb037bfbSDan Handley file_state_t *fp; 174*70cb0bffSYann Gautier unsigned long long pos_after; 175561cd33eSHarry Liebel 176561cd33eSHarry Liebel assert(entity != NULL); 177561cd33eSHarry Liebel assert(length_read != NULL); 178561cd33eSHarry Liebel 179fb037bfbSDan Handley fp = (file_state_t *) entity->info; 180561cd33eSHarry Liebel 18169c043b2SJeenu Viswambharan /* Assert that file position is valid for this read operation */ 18269c043b2SJeenu Viswambharan pos_after = fp->file_pos + length; 18369c043b2SJeenu Viswambharan assert((pos_after >= fp->file_pos) && (pos_after <= fp->size)); 18469c043b2SJeenu Viswambharan 185*70cb0bffSYann Gautier memcpy((void *)buffer, 186*70cb0bffSYann Gautier (void *)((uintptr_t)(fp->base + fp->file_pos)), length); 187561cd33eSHarry Liebel 188561cd33eSHarry Liebel *length_read = length; 18969c043b2SJeenu Viswambharan 19069c043b2SJeenu Viswambharan /* Set file position after read */ 19169c043b2SJeenu Viswambharan fp->file_pos = pos_after; 192561cd33eSHarry Liebel 193e098e244SJuan Castillo return 0; 194561cd33eSHarry Liebel } 195561cd33eSHarry Liebel 196561cd33eSHarry Liebel 197561cd33eSHarry Liebel /* Write data to a file on the memmap device */ 198625de1d4SDan Handley static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer, 199561cd33eSHarry Liebel size_t length, size_t *length_written) 200561cd33eSHarry Liebel { 201fb037bfbSDan Handley file_state_t *fp; 202*70cb0bffSYann Gautier unsigned long long pos_after; 203561cd33eSHarry Liebel 204561cd33eSHarry Liebel assert(entity != NULL); 205561cd33eSHarry Liebel assert(length_written != NULL); 206561cd33eSHarry Liebel 207fb037bfbSDan Handley fp = (file_state_t *) entity->info; 208561cd33eSHarry Liebel 20969c043b2SJeenu Viswambharan /* Assert that file position is valid for this write operation */ 21069c043b2SJeenu Viswambharan pos_after = fp->file_pos + length; 21169c043b2SJeenu Viswambharan assert((pos_after >= fp->file_pos) && (pos_after <= fp->size)); 21269c043b2SJeenu Viswambharan 213*70cb0bffSYann Gautier memcpy((void *)((uintptr_t)(fp->base + fp->file_pos)), 214*70cb0bffSYann Gautier (void *)buffer, length); 215561cd33eSHarry Liebel 216561cd33eSHarry Liebel *length_written = length; 217561cd33eSHarry Liebel 21869c043b2SJeenu Viswambharan /* Set file position after write */ 21969c043b2SJeenu Viswambharan fp->file_pos = pos_after; 220561cd33eSHarry Liebel 221e098e244SJuan Castillo return 0; 222561cd33eSHarry Liebel } 223561cd33eSHarry Liebel 224561cd33eSHarry Liebel 225561cd33eSHarry Liebel /* Close a file on the memmap device */ 226fb037bfbSDan Handley static int memmap_block_close(io_entity_t *entity) 227561cd33eSHarry Liebel { 228561cd33eSHarry Liebel assert(entity != NULL); 229561cd33eSHarry Liebel 230561cd33eSHarry Liebel entity->info = 0; 231561cd33eSHarry Liebel 232561cd33eSHarry Liebel /* This would be a mem free() if we had malloc.*/ 23332f0d3c6SDouglas Raillard zeromem((void *)¤t_file, sizeof(current_file)); 234561cd33eSHarry Liebel 235e098e244SJuan Castillo return 0; 236561cd33eSHarry Liebel } 237561cd33eSHarry Liebel 238561cd33eSHarry Liebel 239561cd33eSHarry Liebel /* Exported functions */ 240561cd33eSHarry Liebel 241561cd33eSHarry Liebel /* Register the memmap driver with the IO abstraction */ 242625de1d4SDan Handley int register_io_dev_memmap(const io_dev_connector_t **dev_con) 243561cd33eSHarry Liebel { 244e098e244SJuan Castillo int result; 245561cd33eSHarry Liebel assert(dev_con != NULL); 246561cd33eSHarry Liebel 247561cd33eSHarry Liebel result = io_register_device(&memmap_dev_info); 248e098e244SJuan Castillo if (result == 0) 249561cd33eSHarry Liebel *dev_con = &memmap_dev_connector; 250561cd33eSHarry Liebel 251561cd33eSHarry Liebel return result; 252561cd33eSHarry Liebel } 253