xref: /rk3399_ARM-atf/drivers/io/io_memmap.c (revision 09d40e0e08283a249e7dce0e106c07c5141f9b7e)
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>
9*09d40e0eSAntonio Nino Diaz 
10*09d40e0eSAntonio Nino Diaz #include <platform_def.h>
11*09d40e0eSAntonio Nino Diaz 
12*09d40e0eSAntonio Nino Diaz #include <common/debug.h>
13*09d40e0eSAntonio Nino Diaz #include <drivers/io/io_driver.h>
14*09d40e0eSAntonio Nino Diaz #include <drivers/io/io_memmap.h>
15*09d40e0eSAntonio Nino Diaz #include <drivers/io/io_storage.h>
16*09d40e0eSAntonio 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;
28561cd33eSHarry Liebel 	size_t		file_pos;
296d70bfa1SGerald Lejeune 	size_t		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,
45561cd33eSHarry Liebel 			     ssize_t 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)&current_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 */
132fb037bfbSDan Handley static int memmap_block_seek(io_entity_t *entity, int mode, ssize_t offset)
133561cd33eSHarry Liebel {
134e098e244SJuan Castillo 	int result = -ENOENT;
13569c043b2SJeenu Viswambharan 	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 
14169c043b2SJeenu Viswambharan 		fp = (file_state_t *) entity->info;
14269c043b2SJeenu Viswambharan 
14369c043b2SJeenu Viswambharan 		/* Assert that new file position is valid */
14469c043b2SJeenu Viswambharan 		assert((offset >= 0) && (offset < fp->size));
14569c043b2SJeenu Viswambharan 
14669c043b2SJeenu Viswambharan 		/* Reset file position */
14769c043b2SJeenu Viswambharan 		fp->file_pos = offset;
148e098e244SJuan Castillo 		result = 0;
149561cd33eSHarry Liebel 	}
150561cd33eSHarry Liebel 
151561cd33eSHarry Liebel 	return result;
152561cd33eSHarry Liebel }
153561cd33eSHarry Liebel 
154561cd33eSHarry Liebel 
1556d70bfa1SGerald Lejeune /* Return the size of a file on the memmap device */
1566d70bfa1SGerald Lejeune static int memmap_block_len(io_entity_t *entity, size_t *length)
1576d70bfa1SGerald Lejeune {
1586d70bfa1SGerald Lejeune 	assert(entity != NULL);
1596d70bfa1SGerald Lejeune 	assert(length != NULL);
1606d70bfa1SGerald Lejeune 
1616d70bfa1SGerald Lejeune 	*length = ((file_state_t *)entity->info)->size;
1626d70bfa1SGerald Lejeune 
1636d70bfa1SGerald Lejeune 	return 0;
1646d70bfa1SGerald Lejeune }
1656d70bfa1SGerald Lejeune 
1666d70bfa1SGerald Lejeune 
167561cd33eSHarry Liebel /* Read data from a file on the memmap device */
168625de1d4SDan Handley static int memmap_block_read(io_entity_t *entity, uintptr_t buffer,
169561cd33eSHarry Liebel 			     size_t length, size_t *length_read)
170561cd33eSHarry Liebel {
171fb037bfbSDan Handley 	file_state_t *fp;
17269c043b2SJeenu Viswambharan 	size_t pos_after;
173561cd33eSHarry Liebel 
174561cd33eSHarry Liebel 	assert(entity != NULL);
175561cd33eSHarry Liebel 	assert(length_read != NULL);
176561cd33eSHarry Liebel 
177fb037bfbSDan Handley 	fp = (file_state_t *) entity->info;
178561cd33eSHarry Liebel 
17969c043b2SJeenu Viswambharan 	/* Assert that file position is valid for this read operation */
18069c043b2SJeenu Viswambharan 	pos_after = fp->file_pos + length;
18169c043b2SJeenu Viswambharan 	assert((pos_after >= fp->file_pos) && (pos_after <= fp->size));
18269c043b2SJeenu Viswambharan 
183625de1d4SDan Handley 	memcpy((void *)buffer, (void *)(fp->base + fp->file_pos), length);
184561cd33eSHarry Liebel 
185561cd33eSHarry Liebel 	*length_read = length;
18669c043b2SJeenu Viswambharan 
18769c043b2SJeenu Viswambharan 	/* Set file position after read */
18869c043b2SJeenu Viswambharan 	fp->file_pos = pos_after;
189561cd33eSHarry Liebel 
190e098e244SJuan Castillo 	return 0;
191561cd33eSHarry Liebel }
192561cd33eSHarry Liebel 
193561cd33eSHarry Liebel 
194561cd33eSHarry Liebel /* Write data to a file on the memmap device */
195625de1d4SDan Handley static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer,
196561cd33eSHarry Liebel 			      size_t length, size_t *length_written)
197561cd33eSHarry Liebel {
198fb037bfbSDan Handley 	file_state_t *fp;
19969c043b2SJeenu Viswambharan 	size_t pos_after;
200561cd33eSHarry Liebel 
201561cd33eSHarry Liebel 	assert(entity != NULL);
202561cd33eSHarry Liebel 	assert(length_written != NULL);
203561cd33eSHarry Liebel 
204fb037bfbSDan Handley 	fp = (file_state_t *) entity->info;
205561cd33eSHarry Liebel 
20669c043b2SJeenu Viswambharan 	/* Assert that file position is valid for this write operation */
20769c043b2SJeenu Viswambharan 	pos_after = fp->file_pos + length;
20869c043b2SJeenu Viswambharan 	assert((pos_after >= fp->file_pos) && (pos_after <= fp->size));
20969c043b2SJeenu Viswambharan 
210625de1d4SDan Handley 	memcpy((void *)(fp->base + fp->file_pos), (void *)buffer, length);
211561cd33eSHarry Liebel 
212561cd33eSHarry Liebel 	*length_written = length;
213561cd33eSHarry Liebel 
21469c043b2SJeenu Viswambharan 	/* Set file position after write */
21569c043b2SJeenu Viswambharan 	fp->file_pos = pos_after;
216561cd33eSHarry Liebel 
217e098e244SJuan Castillo 	return 0;
218561cd33eSHarry Liebel }
219561cd33eSHarry Liebel 
220561cd33eSHarry Liebel 
221561cd33eSHarry Liebel /* Close a file on the memmap device */
222fb037bfbSDan Handley static int memmap_block_close(io_entity_t *entity)
223561cd33eSHarry Liebel {
224561cd33eSHarry Liebel 	assert(entity != NULL);
225561cd33eSHarry Liebel 
226561cd33eSHarry Liebel 	entity->info = 0;
227561cd33eSHarry Liebel 
228561cd33eSHarry Liebel 	/* This would be a mem free() if we had malloc.*/
22932f0d3c6SDouglas Raillard 	zeromem((void *)&current_file, sizeof(current_file));
230561cd33eSHarry Liebel 
231e098e244SJuan Castillo 	return 0;
232561cd33eSHarry Liebel }
233561cd33eSHarry Liebel 
234561cd33eSHarry Liebel 
235561cd33eSHarry Liebel /* Exported functions */
236561cd33eSHarry Liebel 
237561cd33eSHarry Liebel /* Register the memmap driver with the IO abstraction */
238625de1d4SDan Handley int register_io_dev_memmap(const io_dev_connector_t **dev_con)
239561cd33eSHarry Liebel {
240e098e244SJuan Castillo 	int result;
241561cd33eSHarry Liebel 	assert(dev_con != NULL);
242561cd33eSHarry Liebel 
243561cd33eSHarry Liebel 	result = io_register_device(&memmap_dev_info);
244e098e244SJuan Castillo 	if (result == 0)
245561cd33eSHarry Liebel 		*dev_con = &memmap_dev_connector;
246561cd33eSHarry Liebel 
247561cd33eSHarry Liebel 	return result;
248561cd33eSHarry Liebel }
249