xref: /rk3399_ARM-atf/drivers/io/io_memmap.c (revision 7fabe1a89903cab4919304faa085ee6eaaec5c9d)
1561cd33eSHarry Liebel /*
2*7fabe1a8SRoberto 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>
835e98e55SDan Handley #include <debug.h>
997043ac9SDan Handley #include <io_driver.h>
10*7fabe1a8SRoberto Vargas #include <io_memmap.h>
1197043ac9SDan Handley #include <io_storage.h>
1297043ac9SDan Handley #include <string.h>
1332f0d3c6SDouglas Raillard #include <utils.h>
14561cd33eSHarry Liebel 
15561cd33eSHarry Liebel /* As we need to be able to keep state for seek, only one file can be open
16561cd33eSHarry Liebel  * at a time. Make this a structure and point to the entity->info. When we
17561cd33eSHarry Liebel  * can malloc memory we can change this to support more open files.
18561cd33eSHarry Liebel  */
19561cd33eSHarry Liebel typedef struct {
20561cd33eSHarry Liebel 	/* Use the 'in_use' flag as any value for base and file_pos could be
21561cd33eSHarry Liebel 	 * valid.
22561cd33eSHarry Liebel 	 */
23561cd33eSHarry Liebel 	int		in_use;
24625de1d4SDan Handley 	uintptr_t	base;
25561cd33eSHarry Liebel 	size_t		file_pos;
266d70bfa1SGerald Lejeune 	size_t		size;
27fb037bfbSDan Handley } file_state_t;
28561cd33eSHarry Liebel 
29fb037bfbSDan Handley static file_state_t current_file = {0};
30561cd33eSHarry Liebel 
31561cd33eSHarry Liebel /* Identify the device type as memmap */
32*7fabe1a8SRoberto Vargas static io_type_t device_type_memmap(void)
33561cd33eSHarry Liebel {
34561cd33eSHarry Liebel 	return IO_TYPE_MEMMAP;
35561cd33eSHarry Liebel }
36561cd33eSHarry Liebel 
37561cd33eSHarry Liebel /* Memmap device functions */
38625de1d4SDan Handley static int memmap_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
39625de1d4SDan Handley static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
40fb037bfbSDan Handley 			     io_entity_t *entity);
41fb037bfbSDan Handley static int memmap_block_seek(io_entity_t *entity, int mode,
42561cd33eSHarry Liebel 			     ssize_t offset);
436d70bfa1SGerald Lejeune static int memmap_block_len(io_entity_t *entity, size_t *length);
44625de1d4SDan Handley static int memmap_block_read(io_entity_t *entity, uintptr_t buffer,
45561cd33eSHarry Liebel 			     size_t length, size_t *length_read);
46625de1d4SDan Handley static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer,
47561cd33eSHarry Liebel 			      size_t length, size_t *length_written);
48fb037bfbSDan Handley static int memmap_block_close(io_entity_t *entity);
49fb037bfbSDan Handley static int memmap_dev_close(io_dev_info_t *dev_info);
50561cd33eSHarry Liebel 
51561cd33eSHarry Liebel 
52625de1d4SDan Handley static const io_dev_connector_t memmap_dev_connector = {
53561cd33eSHarry Liebel 	.dev_open = memmap_dev_open
54561cd33eSHarry Liebel };
55561cd33eSHarry Liebel 
56561cd33eSHarry Liebel 
57625de1d4SDan Handley static const io_dev_funcs_t memmap_dev_funcs = {
58561cd33eSHarry Liebel 	.type = device_type_memmap,
59561cd33eSHarry Liebel 	.open = memmap_block_open,
60561cd33eSHarry Liebel 	.seek = memmap_block_seek,
616d70bfa1SGerald Lejeune 	.size = memmap_block_len,
62561cd33eSHarry Liebel 	.read = memmap_block_read,
63561cd33eSHarry Liebel 	.write = memmap_block_write,
64561cd33eSHarry Liebel 	.close = memmap_block_close,
65561cd33eSHarry Liebel 	.dev_init = NULL,
66561cd33eSHarry Liebel 	.dev_close = memmap_dev_close,
67561cd33eSHarry Liebel };
68561cd33eSHarry Liebel 
69561cd33eSHarry Liebel 
70625de1d4SDan Handley /* No state associated with this device so structure can be const */
71625de1d4SDan Handley static const io_dev_info_t memmap_dev_info = {
72561cd33eSHarry Liebel 	.funcs = &memmap_dev_funcs,
73561cd33eSHarry Liebel 	.info = (uintptr_t)NULL
74561cd33eSHarry Liebel };
75561cd33eSHarry Liebel 
76561cd33eSHarry Liebel 
77561cd33eSHarry Liebel /* Open a connection to the memmap device */
7865cd299fSSoren Brinkmann static int memmap_dev_open(const uintptr_t dev_spec __unused,
79fb037bfbSDan Handley 			   io_dev_info_t **dev_info)
80561cd33eSHarry Liebel {
81561cd33eSHarry Liebel 	assert(dev_info != NULL);
82625de1d4SDan Handley 	*dev_info = (io_dev_info_t *)&memmap_dev_info; /* cast away const */
83561cd33eSHarry Liebel 
84e098e244SJuan Castillo 	return 0;
85561cd33eSHarry Liebel }
86561cd33eSHarry Liebel 
87561cd33eSHarry Liebel 
88561cd33eSHarry Liebel 
89561cd33eSHarry Liebel /* Close a connection to the memmap device */
90fb037bfbSDan Handley static int memmap_dev_close(io_dev_info_t *dev_info)
91561cd33eSHarry Liebel {
92561cd33eSHarry Liebel 	/* NOP */
93561cd33eSHarry Liebel 	/* TODO: Consider tracking open files and cleaning them up here */
94e098e244SJuan Castillo 	return 0;
95561cd33eSHarry Liebel }
96561cd33eSHarry Liebel 
97561cd33eSHarry Liebel 
98561cd33eSHarry Liebel /* Open a file on the memmap device */
99625de1d4SDan Handley static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
100fb037bfbSDan Handley 			     io_entity_t *entity)
101561cd33eSHarry Liebel {
102e098e244SJuan Castillo 	int result = -ENOMEM;
103fb037bfbSDan Handley 	const io_block_spec_t *block_spec = (io_block_spec_t *)spec;
104561cd33eSHarry Liebel 
105561cd33eSHarry Liebel 	/* Since we need to track open state for seek() we only allow one open
106561cd33eSHarry Liebel 	 * spec at a time. When we have dynamic memory we can malloc and set
107561cd33eSHarry Liebel 	 * entity->info.
108561cd33eSHarry Liebel 	 */
109561cd33eSHarry Liebel 	if (current_file.in_use == 0) {
110561cd33eSHarry Liebel 		assert(block_spec != NULL);
111561cd33eSHarry Liebel 		assert(entity != NULL);
112561cd33eSHarry Liebel 
113561cd33eSHarry Liebel 		current_file.in_use = 1;
114561cd33eSHarry Liebel 		current_file.base = block_spec->offset;
115561cd33eSHarry Liebel 		/* File cursor offset for seek and incremental reads etc. */
116561cd33eSHarry Liebel 		current_file.file_pos = 0;
1176d70bfa1SGerald Lejeune 		current_file.size = block_spec->length;
118561cd33eSHarry Liebel 		entity->info = (uintptr_t)&current_file;
119e098e244SJuan Castillo 		result = 0;
120561cd33eSHarry Liebel 	} else {
12108c28d53SJeenu Viswambharan 		WARN("A Memmap device is already active. Close first.\n");
122561cd33eSHarry Liebel 	}
123561cd33eSHarry Liebel 
124561cd33eSHarry Liebel 	return result;
125561cd33eSHarry Liebel }
126561cd33eSHarry Liebel 
127561cd33eSHarry Liebel 
128561cd33eSHarry Liebel /* Seek to a particular file offset on the memmap device */
129fb037bfbSDan Handley static int memmap_block_seek(io_entity_t *entity, int mode, ssize_t offset)
130561cd33eSHarry Liebel {
131e098e244SJuan Castillo 	int result = -ENOENT;
13269c043b2SJeenu Viswambharan 	file_state_t *fp;
133561cd33eSHarry Liebel 
134561cd33eSHarry Liebel 	/* We only support IO_SEEK_SET for the moment. */
135561cd33eSHarry Liebel 	if (mode == IO_SEEK_SET) {
136561cd33eSHarry Liebel 		assert(entity != NULL);
137561cd33eSHarry Liebel 
13869c043b2SJeenu Viswambharan 		fp = (file_state_t *) entity->info;
13969c043b2SJeenu Viswambharan 
14069c043b2SJeenu Viswambharan 		/* Assert that new file position is valid */
14169c043b2SJeenu Viswambharan 		assert((offset >= 0) && (offset < fp->size));
14269c043b2SJeenu Viswambharan 
14369c043b2SJeenu Viswambharan 		/* Reset file position */
14469c043b2SJeenu Viswambharan 		fp->file_pos = offset;
145e098e244SJuan Castillo 		result = 0;
146561cd33eSHarry Liebel 	}
147561cd33eSHarry Liebel 
148561cd33eSHarry Liebel 	return result;
149561cd33eSHarry Liebel }
150561cd33eSHarry Liebel 
151561cd33eSHarry Liebel 
1526d70bfa1SGerald Lejeune /* Return the size of a file on the memmap device */
1536d70bfa1SGerald Lejeune static int memmap_block_len(io_entity_t *entity, size_t *length)
1546d70bfa1SGerald Lejeune {
1556d70bfa1SGerald Lejeune 	assert(entity != NULL);
1566d70bfa1SGerald Lejeune 	assert(length != NULL);
1576d70bfa1SGerald Lejeune 
1586d70bfa1SGerald Lejeune 	*length = ((file_state_t *)entity->info)->size;
1596d70bfa1SGerald Lejeune 
1606d70bfa1SGerald Lejeune 	return 0;
1616d70bfa1SGerald Lejeune }
1626d70bfa1SGerald Lejeune 
1636d70bfa1SGerald Lejeune 
164561cd33eSHarry Liebel /* Read data from a file on the memmap device */
165625de1d4SDan Handley static int memmap_block_read(io_entity_t *entity, uintptr_t buffer,
166561cd33eSHarry Liebel 			     size_t length, size_t *length_read)
167561cd33eSHarry Liebel {
168fb037bfbSDan Handley 	file_state_t *fp;
16969c043b2SJeenu Viswambharan 	size_t pos_after;
170561cd33eSHarry Liebel 
171561cd33eSHarry Liebel 	assert(entity != NULL);
172625de1d4SDan Handley 	assert(buffer != (uintptr_t)NULL);
173561cd33eSHarry Liebel 	assert(length_read != NULL);
174561cd33eSHarry Liebel 
175fb037bfbSDan Handley 	fp = (file_state_t *) entity->info;
176561cd33eSHarry Liebel 
17769c043b2SJeenu Viswambharan 	/* Assert that file position is valid for this read operation */
17869c043b2SJeenu Viswambharan 	pos_after = fp->file_pos + length;
17969c043b2SJeenu Viswambharan 	assert((pos_after >= fp->file_pos) && (pos_after <= fp->size));
18069c043b2SJeenu Viswambharan 
181625de1d4SDan Handley 	memcpy((void *)buffer, (void *)(fp->base + fp->file_pos), length);
182561cd33eSHarry Liebel 
183561cd33eSHarry Liebel 	*length_read = length;
18469c043b2SJeenu Viswambharan 
18569c043b2SJeenu Viswambharan 	/* Set file position after read */
18669c043b2SJeenu Viswambharan 	fp->file_pos = pos_after;
187561cd33eSHarry Liebel 
188e098e244SJuan Castillo 	return 0;
189561cd33eSHarry Liebel }
190561cd33eSHarry Liebel 
191561cd33eSHarry Liebel 
192561cd33eSHarry Liebel /* Write data to a file on the memmap device */
193625de1d4SDan Handley static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer,
194561cd33eSHarry Liebel 			      size_t length, size_t *length_written)
195561cd33eSHarry Liebel {
196fb037bfbSDan Handley 	file_state_t *fp;
19769c043b2SJeenu Viswambharan 	size_t pos_after;
198561cd33eSHarry Liebel 
199561cd33eSHarry Liebel 	assert(entity != NULL);
200625de1d4SDan Handley 	assert(buffer != (uintptr_t)NULL);
201561cd33eSHarry Liebel 	assert(length_written != NULL);
202561cd33eSHarry Liebel 
203fb037bfbSDan Handley 	fp = (file_state_t *) entity->info;
204561cd33eSHarry Liebel 
20569c043b2SJeenu Viswambharan 	/* Assert that file position is valid for this write operation */
20669c043b2SJeenu Viswambharan 	pos_after = fp->file_pos + length;
20769c043b2SJeenu Viswambharan 	assert((pos_after >= fp->file_pos) && (pos_after <= fp->size));
20869c043b2SJeenu Viswambharan 
209625de1d4SDan Handley 	memcpy((void *)(fp->base + fp->file_pos), (void *)buffer, length);
210561cd33eSHarry Liebel 
211561cd33eSHarry Liebel 	*length_written = length;
212561cd33eSHarry Liebel 
21369c043b2SJeenu Viswambharan 	/* Set file position after write */
21469c043b2SJeenu Viswambharan 	fp->file_pos = pos_after;
215561cd33eSHarry Liebel 
216e098e244SJuan Castillo 	return 0;
217561cd33eSHarry Liebel }
218561cd33eSHarry Liebel 
219561cd33eSHarry Liebel 
220561cd33eSHarry Liebel /* Close a file on the memmap device */
221fb037bfbSDan Handley static int memmap_block_close(io_entity_t *entity)
222561cd33eSHarry Liebel {
223561cd33eSHarry Liebel 	assert(entity != NULL);
224561cd33eSHarry Liebel 
225561cd33eSHarry Liebel 	entity->info = 0;
226561cd33eSHarry Liebel 
227561cd33eSHarry Liebel 	/* This would be a mem free() if we had malloc.*/
22832f0d3c6SDouglas Raillard 	zeromem((void *)&current_file, sizeof(current_file));
229561cd33eSHarry Liebel 
230e098e244SJuan Castillo 	return 0;
231561cd33eSHarry Liebel }
232561cd33eSHarry Liebel 
233561cd33eSHarry Liebel 
234561cd33eSHarry Liebel /* Exported functions */
235561cd33eSHarry Liebel 
236561cd33eSHarry Liebel /* Register the memmap driver with the IO abstraction */
237625de1d4SDan Handley int register_io_dev_memmap(const io_dev_connector_t **dev_con)
238561cd33eSHarry Liebel {
239e098e244SJuan Castillo 	int result;
240561cd33eSHarry Liebel 	assert(dev_con != NULL);
241561cd33eSHarry Liebel 
242561cd33eSHarry Liebel 	result = io_register_device(&memmap_dev_info);
243e098e244SJuan Castillo 	if (result == 0)
244561cd33eSHarry Liebel 		*dev_con = &memmap_dev_connector;
245561cd33eSHarry Liebel 
246561cd33eSHarry Liebel 	return result;
247561cd33eSHarry Liebel }
248