xref: /rk3399_ARM-atf/drivers/io/io_memmap.c (revision 97043ac98e13a726dbf8b3b41654dca759e3da2c)
1561cd33eSHarry Liebel /*
2561cd33eSHarry Liebel  * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved.
3561cd33eSHarry Liebel  *
4561cd33eSHarry Liebel  * Redistribution and use in source and binary forms, with or without
5561cd33eSHarry Liebel  * modification, are permitted provided that the following conditions are met:
6561cd33eSHarry Liebel  *
7561cd33eSHarry Liebel  * Redistributions of source code must retain the above copyright notice, this
8561cd33eSHarry Liebel  * list of conditions and the following disclaimer.
9561cd33eSHarry Liebel  *
10561cd33eSHarry Liebel  * Redistributions in binary form must reproduce the above copyright notice,
11561cd33eSHarry Liebel  * this list of conditions and the following disclaimer in the documentation
12561cd33eSHarry Liebel  * and/or other materials provided with the distribution.
13561cd33eSHarry Liebel  *
14561cd33eSHarry Liebel  * Neither the name of ARM nor the names of its contributors may be used
15561cd33eSHarry Liebel  * to endorse or promote products derived from this software without specific
16561cd33eSHarry Liebel  * prior written permission.
17561cd33eSHarry Liebel  *
18561cd33eSHarry Liebel  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19561cd33eSHarry Liebel  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20561cd33eSHarry Liebel  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21561cd33eSHarry Liebel  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22561cd33eSHarry Liebel  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23561cd33eSHarry Liebel  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24561cd33eSHarry Liebel  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25561cd33eSHarry Liebel  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26561cd33eSHarry Liebel  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27561cd33eSHarry Liebel  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28561cd33eSHarry Liebel  * POSSIBILITY OF SUCH DAMAGE.
29561cd33eSHarry Liebel  */
30561cd33eSHarry Liebel 
31561cd33eSHarry Liebel #include <assert.h>
3235e98e55SDan Handley #include <debug.h>
33*97043ac9SDan Handley #include <io_driver.h>
34*97043ac9SDan Handley #include <io_storage.h>
35*97043ac9SDan Handley #include <string.h>
36561cd33eSHarry Liebel 
37561cd33eSHarry Liebel /* As we need to be able to keep state for seek, only one file can be open
38561cd33eSHarry Liebel  * at a time. Make this a structure and point to the entity->info. When we
39561cd33eSHarry Liebel  * can malloc memory we can change this to support more open files.
40561cd33eSHarry Liebel  */
41561cd33eSHarry Liebel typedef struct {
42561cd33eSHarry Liebel 	/* Use the 'in_use' flag as any value for base and file_pos could be
43561cd33eSHarry Liebel 	 * valid.
44561cd33eSHarry Liebel 	 */
45561cd33eSHarry Liebel 	int	in_use;
46561cd33eSHarry Liebel 	size_t	base;
47561cd33eSHarry Liebel 	size_t  file_pos;
48fb037bfbSDan Handley } file_state_t;
49561cd33eSHarry Liebel 
50fb037bfbSDan Handley static file_state_t current_file = {0};
51561cd33eSHarry Liebel 
52561cd33eSHarry Liebel /* Identify the device type as memmap */
53fb037bfbSDan Handley io_type_t device_type_memmap(void)
54561cd33eSHarry Liebel {
55561cd33eSHarry Liebel 	return IO_TYPE_MEMMAP;
56561cd33eSHarry Liebel }
57561cd33eSHarry Liebel 
58561cd33eSHarry Liebel /* Memmap device functions */
59fb037bfbSDan Handley static int memmap_dev_open(void *spec, io_dev_info_t **dev_info);
60fb037bfbSDan Handley static int memmap_block_open(io_dev_info_t *dev_info, const void *spec,
61fb037bfbSDan Handley 			     io_entity_t *entity);
62fb037bfbSDan Handley static int memmap_block_seek(io_entity_t *entity, int mode,
63561cd33eSHarry Liebel 			     ssize_t offset);
64fb037bfbSDan Handley static int memmap_block_read(io_entity_t *entity, void *buffer,
65561cd33eSHarry Liebel 			     size_t length, size_t *length_read);
66fb037bfbSDan Handley static int memmap_block_write(io_entity_t *entity, const void *buffer,
67561cd33eSHarry Liebel 			      size_t length, size_t *length_written);
68fb037bfbSDan Handley static int memmap_block_close(io_entity_t *entity);
69fb037bfbSDan Handley static int memmap_dev_close(io_dev_info_t *dev_info);
70561cd33eSHarry Liebel 
71561cd33eSHarry Liebel 
72561cd33eSHarry Liebel static struct io_dev_connector memmap_dev_connector = {
73561cd33eSHarry Liebel 	.dev_open = memmap_dev_open
74561cd33eSHarry Liebel };
75561cd33eSHarry Liebel 
76561cd33eSHarry Liebel 
77561cd33eSHarry Liebel static struct io_dev_funcs memmap_dev_funcs = {
78561cd33eSHarry Liebel 	.type = device_type_memmap,
79561cd33eSHarry Liebel 	.open = memmap_block_open,
80561cd33eSHarry Liebel 	.seek = memmap_block_seek,
81561cd33eSHarry Liebel 	.size = NULL,
82561cd33eSHarry Liebel 	.read = memmap_block_read,
83561cd33eSHarry Liebel 	.write = memmap_block_write,
84561cd33eSHarry Liebel 	.close = memmap_block_close,
85561cd33eSHarry Liebel 	.dev_init = NULL,
86561cd33eSHarry Liebel 	.dev_close = memmap_dev_close,
87561cd33eSHarry Liebel };
88561cd33eSHarry Liebel 
89561cd33eSHarry Liebel 
90561cd33eSHarry Liebel static struct io_dev_info memmap_dev_info = {
91561cd33eSHarry Liebel 	.funcs = &memmap_dev_funcs,
92561cd33eSHarry Liebel 	.info = (uintptr_t)NULL
93561cd33eSHarry Liebel };
94561cd33eSHarry Liebel 
95561cd33eSHarry Liebel 
96561cd33eSHarry Liebel /* Open a connection to the memmap device */
97561cd33eSHarry Liebel static int memmap_dev_open(void *spec __attribute__((unused)),
98fb037bfbSDan Handley 			   io_dev_info_t **dev_info)
99561cd33eSHarry Liebel {
100561cd33eSHarry Liebel 	assert(dev_info != NULL);
101561cd33eSHarry Liebel 	*dev_info = &memmap_dev_info;
102561cd33eSHarry Liebel 
103561cd33eSHarry Liebel 	return IO_SUCCESS;
104561cd33eSHarry Liebel }
105561cd33eSHarry Liebel 
106561cd33eSHarry Liebel 
107561cd33eSHarry Liebel 
108561cd33eSHarry Liebel /* Close a connection to the memmap device */
109fb037bfbSDan Handley static int memmap_dev_close(io_dev_info_t *dev_info)
110561cd33eSHarry Liebel {
111561cd33eSHarry Liebel 	/* NOP */
112561cd33eSHarry Liebel 	/* TODO: Consider tracking open files and cleaning them up here */
113561cd33eSHarry Liebel 	return IO_SUCCESS;
114561cd33eSHarry Liebel }
115561cd33eSHarry Liebel 
116561cd33eSHarry Liebel 
117561cd33eSHarry Liebel /* Open a file on the memmap device */
118561cd33eSHarry Liebel /* TODO: Can we do any sensible limit checks on requested memory */
119fb037bfbSDan Handley static int memmap_block_open(io_dev_info_t *dev_info, const void *spec,
120fb037bfbSDan Handley 			     io_entity_t *entity)
121561cd33eSHarry Liebel {
122561cd33eSHarry Liebel 	int result = IO_FAIL;
123fb037bfbSDan Handley 	const io_block_spec_t *block_spec = (io_block_spec_t *)spec;
124561cd33eSHarry Liebel 
125561cd33eSHarry Liebel 	/* Since we need to track open state for seek() we only allow one open
126561cd33eSHarry Liebel 	 * spec at a time. When we have dynamic memory we can malloc and set
127561cd33eSHarry Liebel 	 * entity->info.
128561cd33eSHarry Liebel 	 */
129561cd33eSHarry Liebel 	if (current_file.in_use == 0) {
130561cd33eSHarry Liebel 		assert(block_spec != NULL);
131561cd33eSHarry Liebel 		assert(entity != NULL);
132561cd33eSHarry Liebel 
133561cd33eSHarry Liebel 		current_file.in_use = 1;
134561cd33eSHarry Liebel 		current_file.base = block_spec->offset;
135561cd33eSHarry Liebel 		/* File cursor offset for seek and incremental reads etc. */
136561cd33eSHarry Liebel 		current_file.file_pos = 0;
137561cd33eSHarry Liebel 		entity->info = (uintptr_t)&current_file;
138561cd33eSHarry Liebel 		result = IO_SUCCESS;
139561cd33eSHarry Liebel 	} else {
14008c28d53SJeenu Viswambharan 		WARN("A Memmap device is already active. Close first.\n");
141561cd33eSHarry Liebel 		result = IO_RESOURCES_EXHAUSTED;
142561cd33eSHarry Liebel 	}
143561cd33eSHarry Liebel 
144561cd33eSHarry Liebel 	return result;
145561cd33eSHarry Liebel }
146561cd33eSHarry Liebel 
147561cd33eSHarry Liebel 
148561cd33eSHarry Liebel /* Seek to a particular file offset on the memmap device */
149fb037bfbSDan Handley static int memmap_block_seek(io_entity_t *entity, int mode, ssize_t offset)
150561cd33eSHarry Liebel {
151561cd33eSHarry Liebel 	int result = IO_FAIL;
152561cd33eSHarry Liebel 
153561cd33eSHarry Liebel 	/* We only support IO_SEEK_SET for the moment. */
154561cd33eSHarry Liebel 	if (mode == IO_SEEK_SET) {
155561cd33eSHarry Liebel 		assert(entity != NULL);
156561cd33eSHarry Liebel 
157561cd33eSHarry Liebel 		/* TODO: can we do some basic limit checks on seek? */
158fb037bfbSDan Handley 		((file_state_t *)entity->info)->file_pos = offset;
159561cd33eSHarry Liebel 		result = IO_SUCCESS;
160561cd33eSHarry Liebel 	} else {
161561cd33eSHarry Liebel 		result = IO_FAIL;
162561cd33eSHarry Liebel 	}
163561cd33eSHarry Liebel 
164561cd33eSHarry Liebel 	return result;
165561cd33eSHarry Liebel }
166561cd33eSHarry Liebel 
167561cd33eSHarry Liebel 
168561cd33eSHarry Liebel /* Read data from a file on the memmap device */
169fb037bfbSDan Handley static int memmap_block_read(io_entity_t *entity, void *buffer,
170561cd33eSHarry Liebel 			     size_t length, size_t *length_read)
171561cd33eSHarry Liebel {
172fb037bfbSDan Handley 	file_state_t *fp;
173561cd33eSHarry Liebel 
174561cd33eSHarry Liebel 	assert(entity != NULL);
175561cd33eSHarry Liebel 	assert(buffer != NULL);
176561cd33eSHarry Liebel 	assert(length_read != NULL);
177561cd33eSHarry Liebel 
178fb037bfbSDan Handley 	fp = (file_state_t *)entity->info;
179561cd33eSHarry Liebel 
180561cd33eSHarry Liebel 	memcpy(buffer, (void *)(fp->base + fp->file_pos), length);
181561cd33eSHarry Liebel 
182561cd33eSHarry Liebel 	*length_read = length;
183561cd33eSHarry Liebel 	/* advance the file 'cursor' for incremental reads */
184561cd33eSHarry Liebel 	fp->file_pos += length;
185561cd33eSHarry Liebel 
186561cd33eSHarry Liebel 	return IO_SUCCESS;
187561cd33eSHarry Liebel }
188561cd33eSHarry Liebel 
189561cd33eSHarry Liebel 
190561cd33eSHarry Liebel /* Write data to a file on the memmap device */
191fb037bfbSDan Handley static int memmap_block_write(io_entity_t *entity, const void *buffer,
192561cd33eSHarry Liebel 			      size_t length, size_t *length_written)
193561cd33eSHarry Liebel {
194fb037bfbSDan Handley 	file_state_t *fp;
195561cd33eSHarry Liebel 
196561cd33eSHarry Liebel 	assert(entity != NULL);
197561cd33eSHarry Liebel 	assert(buffer != NULL);
198561cd33eSHarry Liebel 	assert(length_written != NULL);
199561cd33eSHarry Liebel 
200fb037bfbSDan Handley 	fp = (file_state_t *)entity->info;
201561cd33eSHarry Liebel 
202561cd33eSHarry Liebel 	memcpy((void *)(fp->base + fp->file_pos), buffer, length);
203561cd33eSHarry Liebel 
204561cd33eSHarry Liebel 	*length_written = length;
205561cd33eSHarry Liebel 
206561cd33eSHarry Liebel 	/* advance the file 'cursor' for incremental writes */
207561cd33eSHarry Liebel 	fp->file_pos += length;
208561cd33eSHarry Liebel 
209561cd33eSHarry Liebel 	return IO_SUCCESS;
210561cd33eSHarry Liebel }
211561cd33eSHarry Liebel 
212561cd33eSHarry Liebel 
213561cd33eSHarry Liebel /* Close a file on the memmap device */
214fb037bfbSDan Handley static int memmap_block_close(io_entity_t *entity)
215561cd33eSHarry Liebel {
216561cd33eSHarry Liebel 	assert(entity != NULL);
217561cd33eSHarry Liebel 
218561cd33eSHarry Liebel 	entity->info = 0;
219561cd33eSHarry Liebel 
220561cd33eSHarry Liebel 	/* This would be a mem free() if we had malloc.*/
221561cd33eSHarry Liebel 	memset((void *)&current_file, 0, sizeof(current_file));
222561cd33eSHarry Liebel 
223561cd33eSHarry Liebel 	return IO_SUCCESS;
224561cd33eSHarry Liebel }
225561cd33eSHarry Liebel 
226561cd33eSHarry Liebel 
227561cd33eSHarry Liebel /* Exported functions */
228561cd33eSHarry Liebel 
229561cd33eSHarry Liebel /* Register the memmap driver with the IO abstraction */
230fb037bfbSDan Handley int register_io_dev_memmap(io_dev_connector_t **dev_con)
231561cd33eSHarry Liebel {
232561cd33eSHarry Liebel 	int result = IO_FAIL;
233561cd33eSHarry Liebel 	assert(dev_con != NULL);
234561cd33eSHarry Liebel 
235561cd33eSHarry Liebel 	result = io_register_device(&memmap_dev_info);
236561cd33eSHarry Liebel 	if (result == IO_SUCCESS)
237561cd33eSHarry Liebel 		*dev_con = &memmap_dev_connector;
238561cd33eSHarry Liebel 
239561cd33eSHarry Liebel 	return result;
240561cd33eSHarry Liebel }
241