• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <string.h>
9 
10 #include <platform_def.h>
11 
12 #include <common/debug.h>
13 #include <drivers/io/io_driver.h>
14 #include <drivers/io/io_memmap.h>
15 #include <drivers/io/io_storage.h>
16 #include <lib/utils.h>
17 
18 #include "qspi/cadence_qspi.h"
19 
20 /* As we need to be able to keep state for seek, only one file can be open
21  * at a time. Make this a structure and point to the entity->info. When we
22  * can malloc memory we can change this to support more open files.
23  */
24 typedef struct {
25 	/* Use the 'in_use' flag as any value for base and file_pos could be
26 	 * valid.
27 	 */
28 	int		in_use;
29 	uintptr_t		base;
30 	unsigned long long	file_pos;
31 	unsigned long long	size;
32 } file_state_t;
33 
34 static file_state_t current_file = {0};
35 
36 /* Identify the device type as memmap */
device_type_memmap(void)37 static io_type_t device_type_memmap(void)
38 {
39 	return IO_TYPE_MEMMAP;
40 }
41 
42 /* Memmap device functions */
43 static int memmap_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
44 static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
45 			     io_entity_t *entity);
46 static int memmap_block_seek(io_entity_t *entity, int mode,
47 			     signed long long offset);
48 static int memmap_block_len(io_entity_t *entity, size_t *length);
49 static int memmap_block_read(io_entity_t *entity, uintptr_t buffer,
50 			     size_t length, size_t *length_read);
51 static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer,
52 			      size_t length, size_t *length_written);
53 static int memmap_block_close(io_entity_t *entity);
54 static int memmap_dev_close(io_dev_info_t *dev_info);
55 
56 
57 static const io_dev_connector_t memmap_dev_connector = {
58 	.dev_open = memmap_dev_open
59 };
60 
61 
62 static const io_dev_funcs_t memmap_dev_funcs = {
63 	.type = device_type_memmap,
64 	.open = memmap_block_open,
65 	.seek = memmap_block_seek,
66 	.size = memmap_block_len,
67 	.read = memmap_block_read,
68 	.write = memmap_block_write,
69 	.close = memmap_block_close,
70 	.dev_init = NULL,
71 	.dev_close = memmap_dev_close,
72 };
73 
74 
75 /* No state associated with this device so structure can be const */
76 static const io_dev_info_t memmap_dev_info = {
77 	.funcs = &memmap_dev_funcs,
78 	.info = (uintptr_t)NULL
79 };
80 
81 
82 /* Open a connection to the memmap device */
memmap_dev_open(const uintptr_t dev_spec __unused,io_dev_info_t ** dev_info)83 static int memmap_dev_open(const uintptr_t dev_spec __unused,
84 			   io_dev_info_t **dev_info)
85 {
86 	assert(dev_info != NULL);
87 	*dev_info = (io_dev_info_t *)&memmap_dev_info; /* cast away const */
88 
89 	return 0;
90 }
91 
92 
93 
94 /* Close a connection to the memmap device */
memmap_dev_close(io_dev_info_t * dev_info)95 static int memmap_dev_close(io_dev_info_t *dev_info)
96 {
97 	/* NOP */
98 	/* TODO: Consider tracking open files and cleaning them up here */
99 	return 0;
100 }
101 
102 
103 /* Open a file on the memmap device */
memmap_block_open(io_dev_info_t * dev_info,const uintptr_t spec,io_entity_t * entity)104 static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
105 			     io_entity_t *entity)
106 {
107 	int result = -ENOMEM;
108 	const io_block_spec_t *block_spec = (io_block_spec_t *)spec;
109 
110 	/* Since we need to track open state for seek() we only allow one open
111 	 * spec at a time. When we have dynamic memory we can malloc and set
112 	 * entity->info.
113 	 */
114 	if (current_file.in_use == 0) {
115 		assert(block_spec != NULL);
116 		assert(entity != NULL);
117 
118 		current_file.in_use = 1;
119 		current_file.base = block_spec->offset;
120 		/* File cursor offset for seek and incremental reads etc. */
121 		current_file.file_pos = 0;
122 		current_file.size = block_spec->length;
123 		entity->info = (uintptr_t)&current_file;
124 		result = 0;
125 	} else {
126 		WARN("A Memmap device is already active. Close first.\n");
127 	}
128 
129 	return result;
130 }
131 
132 
133 /* Seek to a particular file offset on the memmap device */
memmap_block_seek(io_entity_t * entity,int mode,signed long long offset)134 static int memmap_block_seek(io_entity_t *entity, int mode,
135 			     signed long long offset)
136 {
137 	int result = -ENOENT;
138 	file_state_t *fp;
139 
140 	/* We only support IO_SEEK_SET for the moment. */
141 	if (mode == IO_SEEK_SET) {
142 		assert(entity != NULL);
143 
144 		fp = (file_state_t *) entity->info;
145 
146 		/* Assert that new file position is valid */
147 		assert((offset >= 0) &&
148 		       ((unsigned long long)offset < fp->size));
149 
150 		/* Reset file position */
151 		fp->file_pos = offset;
152 		result = 0;
153 	}
154 
155 	return result;
156 }
157 
158 
159 /* Return the size of a file on the memmap device */
memmap_block_len(io_entity_t * entity,size_t * length)160 static int memmap_block_len(io_entity_t *entity, size_t *length)
161 {
162 	assert(entity != NULL);
163 	assert(length != NULL);
164 
165 	*length = ((file_state_t *)entity->info)->size;
166 
167 	return 0;
168 }
169 
170 
171 /* 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)172 static int memmap_block_read(io_entity_t *entity, uintptr_t buffer,
173 			     size_t length, size_t *length_read)
174 {
175 	file_state_t *fp;
176 	unsigned long long pos_after;
177 
178 	assert(entity != NULL);
179 	assert(length_read != NULL);
180 
181 	fp = (file_state_t *) entity->info;
182 
183 	/* Assert that file position is valid for this read operation */
184 	pos_after = fp->file_pos + length;
185 	assert((pos_after >= fp->file_pos) && (pos_after <= fp->size));
186 
187 	//memcpy((void *)buffer, (void *)(fp->base + fp->file_pos), length);
188 	cad_qspi_read((void *)buffer, fp->base + fp->file_pos, length);
189 	*length_read = length;
190 
191 	/* Set file position after read */
192 	fp->file_pos = pos_after;
193 
194 	return 0;
195 }
196 
197 
198 /* 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)199 static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer,
200 			      size_t length, size_t *length_written)
201 {
202 	file_state_t *fp;
203 	unsigned long long pos_after;
204 
205 	assert(entity != NULL);
206 	assert(length_written != NULL);
207 
208 	fp = (file_state_t *) entity->info;
209 
210 	/* Assert that file position is valid for this write operation */
211 	pos_after = fp->file_pos + length;
212 	assert((pos_after >= fp->file_pos) && (pos_after <= fp->size));
213 
214 	memcpy((void *)(fp->base + fp->file_pos), (void *)buffer, length);
215 
216 	*length_written = length;
217 
218 	/* Set file position after write */
219 	fp->file_pos = pos_after;
220 
221 	return 0;
222 }
223 
224 
225 /* Close a file on the memmap device */
memmap_block_close(io_entity_t * entity)226 static int memmap_block_close(io_entity_t *entity)
227 {
228 	assert(entity != NULL);
229 
230 	entity->info = 0;
231 
232 	/* This would be a mem free() if we had malloc.*/
233 	zeromem((void *)&current_file, sizeof(current_file));
234 
235 	return 0;
236 }
237 
238 
239 /* Exported functions */
240 
241 /* Register the memmap driver with the IO abstraction */
register_io_dev_memmap(const io_dev_connector_t ** dev_con)242 int register_io_dev_memmap(const io_dev_connector_t **dev_con)
243 {
244 	int result;
245 
246 	assert(dev_con != NULL);
247 
248 	result = io_register_device(&memmap_dev_info);
249 	if (result == 0)
250 		*dev_con = &memmap_dev_connector;
251 
252 	return result;
253 }
254