1561cd33eSHarry Liebel /*
2*d471bd9cSjohpow01 * Copyright (c) 2014-2020, 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;
2870cb0bffSYann Gautier unsigned long long file_pos;
2970cb0bffSYann Gautier unsigned long long size;
30*d471bd9cSjohpow01 } memmap_file_state_t;
31561cd33eSHarry Liebel
32*d471bd9cSjohpow01 static memmap_file_state_t current_memmap_file = {0};
33561cd33eSHarry Liebel
34561cd33eSHarry Liebel /* Identify the device type as memmap */
device_type_memmap(void)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,
4570cb0bffSYann 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 */
74*d471bd9cSjohpow01 static 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 */
memmap_dev_open(const uintptr_t dev_spec __unused,io_dev_info_t ** dev_info)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);
85*d471bd9cSjohpow01 *dev_info = &memmap_dev_info;
86e098e244SJuan Castillo return 0;
87561cd33eSHarry Liebel }
88561cd33eSHarry Liebel
89561cd33eSHarry Liebel
90561cd33eSHarry Liebel
91561cd33eSHarry Liebel /* Close a connection to the memmap device */
memmap_dev_close(io_dev_info_t * dev_info)92fb037bfbSDan Handley static int memmap_dev_close(io_dev_info_t *dev_info)
93561cd33eSHarry Liebel {
94561cd33eSHarry Liebel /* NOP */
95561cd33eSHarry Liebel /* TODO: Consider tracking open files and cleaning them up here */
96e098e244SJuan Castillo return 0;
97561cd33eSHarry Liebel }
98561cd33eSHarry Liebel
99561cd33eSHarry Liebel
100561cd33eSHarry Liebel /* Open a file on the memmap device */
memmap_block_open(io_dev_info_t * dev_info,const uintptr_t spec,io_entity_t * entity)101625de1d4SDan Handley static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
102fb037bfbSDan Handley io_entity_t *entity)
103561cd33eSHarry Liebel {
104e098e244SJuan Castillo int result = -ENOMEM;
105fb037bfbSDan Handley const io_block_spec_t *block_spec = (io_block_spec_t *)spec;
106561cd33eSHarry Liebel
107561cd33eSHarry Liebel /* Since we need to track open state for seek() we only allow one open
108561cd33eSHarry Liebel * spec at a time. When we have dynamic memory we can malloc and set
109561cd33eSHarry Liebel * entity->info.
110561cd33eSHarry Liebel */
111*d471bd9cSjohpow01 if (current_memmap_file.in_use == 0) {
112561cd33eSHarry Liebel assert(block_spec != NULL);
113561cd33eSHarry Liebel assert(entity != NULL);
114561cd33eSHarry Liebel
115*d471bd9cSjohpow01 current_memmap_file.in_use = 1;
116*d471bd9cSjohpow01 current_memmap_file.base = block_spec->offset;
117561cd33eSHarry Liebel /* File cursor offset for seek and incremental reads etc. */
118*d471bd9cSjohpow01 current_memmap_file.file_pos = 0;
119*d471bd9cSjohpow01 current_memmap_file.size = block_spec->length;
120*d471bd9cSjohpow01 entity->info = (uintptr_t)¤t_memmap_file;
121e098e244SJuan Castillo result = 0;
122561cd33eSHarry Liebel } else {
12308c28d53SJeenu Viswambharan WARN("A Memmap device is already active. Close first.\n");
124561cd33eSHarry Liebel }
125561cd33eSHarry Liebel
126561cd33eSHarry Liebel return result;
127561cd33eSHarry Liebel }
128561cd33eSHarry Liebel
129561cd33eSHarry Liebel
130561cd33eSHarry Liebel /* Seek to a particular file offset on the memmap device */
memmap_block_seek(io_entity_t * entity,int mode,signed long long offset)13170cb0bffSYann Gautier static int memmap_block_seek(io_entity_t *entity, int mode,
13270cb0bffSYann Gautier signed long long offset)
133561cd33eSHarry Liebel {
134e098e244SJuan Castillo int result = -ENOENT;
135*d471bd9cSjohpow01 memmap_file_state_t *fp;
136561cd33eSHarry Liebel
137561cd33eSHarry Liebel /* We only support IO_SEEK_SET for the moment. */
138561cd33eSHarry Liebel if (mode == IO_SEEK_SET) {
139561cd33eSHarry Liebel assert(entity != NULL);
140561cd33eSHarry Liebel
141*d471bd9cSjohpow01 fp = (memmap_file_state_t *) entity->info;
14269c043b2SJeenu Viswambharan
14369c043b2SJeenu Viswambharan /* Assert that new file position is valid */
14470cb0bffSYann Gautier assert((offset >= 0) &&
14570cb0bffSYann Gautier ((unsigned long long)offset < fp->size));
14669c043b2SJeenu Viswambharan
14769c043b2SJeenu Viswambharan /* Reset file position */
14870cb0bffSYann Gautier fp->file_pos = (unsigned long long)offset;
149e098e244SJuan Castillo result = 0;
150561cd33eSHarry Liebel }
151561cd33eSHarry Liebel
152561cd33eSHarry Liebel return result;
153561cd33eSHarry Liebel }
154561cd33eSHarry Liebel
155561cd33eSHarry Liebel
1566d70bfa1SGerald Lejeune /* Return the size of a file on the memmap device */
memmap_block_len(io_entity_t * entity,size_t * length)1576d70bfa1SGerald Lejeune static int memmap_block_len(io_entity_t *entity, size_t *length)
1586d70bfa1SGerald Lejeune {
1596d70bfa1SGerald Lejeune assert(entity != NULL);
1606d70bfa1SGerald Lejeune assert(length != NULL);
1616d70bfa1SGerald Lejeune
162*d471bd9cSjohpow01 *length = (size_t)((memmap_file_state_t *)entity->info)->size;
1636d70bfa1SGerald Lejeune
1646d70bfa1SGerald Lejeune return 0;
1656d70bfa1SGerald Lejeune }
1666d70bfa1SGerald Lejeune
1676d70bfa1SGerald Lejeune
168561cd33eSHarry Liebel /* Read data from a file on the memmap device */
memmap_block_read(io_entity_t * entity,uintptr_t buffer,size_t length,size_t * length_read)169625de1d4SDan Handley static int memmap_block_read(io_entity_t *entity, uintptr_t buffer,
170561cd33eSHarry Liebel size_t length, size_t *length_read)
171561cd33eSHarry Liebel {
172*d471bd9cSjohpow01 memmap_file_state_t *fp;
17370cb0bffSYann Gautier unsigned long long pos_after;
174561cd33eSHarry Liebel
175561cd33eSHarry Liebel assert(entity != NULL);
176561cd33eSHarry Liebel assert(length_read != NULL);
177561cd33eSHarry Liebel
178*d471bd9cSjohpow01 fp = (memmap_file_state_t *) entity->info;
179561cd33eSHarry Liebel
18069c043b2SJeenu Viswambharan /* Assert that file position is valid for this read operation */
18169c043b2SJeenu Viswambharan pos_after = fp->file_pos + length;
18269c043b2SJeenu Viswambharan assert((pos_after >= fp->file_pos) && (pos_after <= fp->size));
18369c043b2SJeenu Viswambharan
18470cb0bffSYann Gautier memcpy((void *)buffer,
18570cb0bffSYann Gautier (void *)((uintptr_t)(fp->base + fp->file_pos)), length);
186561cd33eSHarry Liebel
187561cd33eSHarry Liebel *length_read = length;
18869c043b2SJeenu Viswambharan
18969c043b2SJeenu Viswambharan /* Set file position after read */
19069c043b2SJeenu Viswambharan fp->file_pos = pos_after;
191561cd33eSHarry Liebel
192e098e244SJuan Castillo return 0;
193561cd33eSHarry Liebel }
194561cd33eSHarry Liebel
195561cd33eSHarry Liebel
196561cd33eSHarry Liebel /* Write data to a file on the memmap device */
memmap_block_write(io_entity_t * entity,const uintptr_t buffer,size_t length,size_t * length_written)197625de1d4SDan Handley static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer,
198561cd33eSHarry Liebel size_t length, size_t *length_written)
199561cd33eSHarry Liebel {
200*d471bd9cSjohpow01 memmap_file_state_t *fp;
20170cb0bffSYann Gautier unsigned long long pos_after;
202561cd33eSHarry Liebel
203561cd33eSHarry Liebel assert(entity != NULL);
204561cd33eSHarry Liebel assert(length_written != NULL);
205561cd33eSHarry Liebel
206*d471bd9cSjohpow01 fp = (memmap_file_state_t *) entity->info;
207561cd33eSHarry Liebel
20869c043b2SJeenu Viswambharan /* Assert that file position is valid for this write operation */
20969c043b2SJeenu Viswambharan pos_after = fp->file_pos + length;
21069c043b2SJeenu Viswambharan assert((pos_after >= fp->file_pos) && (pos_after <= fp->size));
21169c043b2SJeenu Viswambharan
21270cb0bffSYann Gautier memcpy((void *)((uintptr_t)(fp->base + fp->file_pos)),
21370cb0bffSYann Gautier (void *)buffer, length);
214561cd33eSHarry Liebel
215561cd33eSHarry Liebel *length_written = length;
216561cd33eSHarry Liebel
21769c043b2SJeenu Viswambharan /* Set file position after write */
21869c043b2SJeenu Viswambharan fp->file_pos = pos_after;
219561cd33eSHarry Liebel
220e098e244SJuan Castillo return 0;
221561cd33eSHarry Liebel }
222561cd33eSHarry Liebel
223561cd33eSHarry Liebel
224561cd33eSHarry Liebel /* Close a file on the memmap device */
memmap_block_close(io_entity_t * entity)225fb037bfbSDan Handley static int memmap_block_close(io_entity_t *entity)
226561cd33eSHarry Liebel {
227561cd33eSHarry Liebel assert(entity != NULL);
228561cd33eSHarry Liebel
229561cd33eSHarry Liebel entity->info = 0;
230561cd33eSHarry Liebel
231561cd33eSHarry Liebel /* This would be a mem free() if we had malloc.*/
232*d471bd9cSjohpow01 zeromem((void *)¤t_memmap_file, sizeof(current_memmap_file));
233561cd33eSHarry Liebel
234e098e244SJuan Castillo return 0;
235561cd33eSHarry Liebel }
236561cd33eSHarry Liebel
237561cd33eSHarry Liebel
238561cd33eSHarry Liebel /* Exported functions */
239561cd33eSHarry Liebel
240561cd33eSHarry Liebel /* Register the memmap driver with the IO abstraction */
register_io_dev_memmap(const io_dev_connector_t ** dev_con)241625de1d4SDan Handley int register_io_dev_memmap(const io_dev_connector_t **dev_con)
242561cd33eSHarry Liebel {
243e098e244SJuan Castillo int result;
244561cd33eSHarry Liebel assert(dev_con != NULL);
245561cd33eSHarry Liebel
246561cd33eSHarry Liebel result = io_register_device(&memmap_dev_info);
247e098e244SJuan Castillo if (result == 0)
248561cd33eSHarry Liebel *dev_con = &memmap_dev_connector;
249561cd33eSHarry Liebel
250561cd33eSHarry Liebel return result;
251561cd33eSHarry Liebel }
252