xref: /rk3399_ARM-atf/drivers/io/io_storage.c (revision 51faada71a219a8b94cd8d8e423f0f22e9da4d8f)
1 /*
2  * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <assert.h>
32 #include <io_driver.h>
33 #include <io_storage.h>
34 #include <platform_def.h>
35 #include <stddef.h>
36 
37 
38 /* Storage for a fixed maximum number of IO entities, definable by platform */
39 static io_entity_t entity_pool[MAX_IO_HANDLES];
40 
41 /* Simple way of tracking used storage - each entry is NULL or a pointer to an
42  * entity */
43 static io_entity_t *entity_map[MAX_IO_HANDLES];
44 
45 /* Track number of allocated entities */
46 static unsigned int entity_count;
47 
48 /* Array of fixed maximum of registered devices, definable by platform */
49 static const io_dev_info_t *devices[MAX_IO_DEVICES];
50 
51 /* Number of currently registered devices */
52 static unsigned int dev_count;
53 
54 
55 #if DEBUG	/* Extra validation functions only used in debug builds */
56 
57 /* Return a boolean value indicating whether a device connector is valid */
58 static int is_valid_dev_connector(const io_dev_connector_t *dev_con)
59 {
60 	int result = (dev_con != NULL) && (dev_con->dev_open != NULL);
61 	return result;
62 }
63 
64 
65 /* Return a boolean value indicating whether a device handle is valid */
66 static int is_valid_dev(const uintptr_t dev_handle)
67 {
68 	const io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
69 	int result = (dev != NULL) && (dev->funcs != NULL) &&
70 			(dev->funcs->type != NULL) &&
71 			(dev->funcs->type() < IO_TYPE_MAX);
72 	return result;
73 }
74 
75 
76 /* Return a boolean value indicating whether an IO entity is valid */
77 static int is_valid_entity(const uintptr_t handle)
78 {
79 	const io_entity_t *entity = (io_entity_t *)handle;
80 	int result = (entity != NULL) &&
81 			(is_valid_dev((uintptr_t)entity->dev_handle));
82 	return result;
83 }
84 
85 
86 /* Return a boolean value indicating whether a seek mode is valid */
87 static int is_valid_seek_mode(io_seek_mode_t mode)
88 {
89 	return ((mode != IO_SEEK_INVALID) && (mode < IO_SEEK_MAX));
90 }
91 
92 #endif	/* End of debug-only validation functions */
93 
94 
95 /* Open a connection to a specific device */
96 static int dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec,
97 		io_dev_info_t **dev_info)
98 {
99 	int result;
100 	assert(dev_info != NULL);
101 	assert(is_valid_dev_connector(dev_con));
102 
103 	result = dev_con->dev_open(dev_spec, dev_info);
104 	return result;
105 }
106 
107 
108 /* Set a handle to track an entity */
109 static void set_handle(uintptr_t *handle, io_entity_t *entity)
110 {
111 	assert(handle != NULL);
112 	*handle = (uintptr_t)entity;
113 }
114 
115 
116 /* Locate an entity in the pool, specified by address */
117 static int find_first_entity(const io_entity_t *entity, unsigned int *index_out)
118 {
119 	int result = -ENOENT;
120 	for (int index = 0; index < MAX_IO_HANDLES; ++index) {
121 		if (entity_map[index] == entity) {
122 			result = 0;
123 			*index_out = index;
124 			break;
125 		}
126 	}
127 	return result;
128 }
129 
130 
131 /* Allocate an entity from the pool and return a pointer to it */
132 static int allocate_entity(io_entity_t **entity)
133 {
134 	int result = -ENOMEM;
135 	assert(entity != NULL);
136 
137 	if (entity_count < MAX_IO_HANDLES) {
138 		unsigned int index = 0;
139 		result = find_first_entity(NULL, &index);
140 		assert(result == 0);
141 		*entity = entity_map[index] = &entity_pool[index];
142 		++entity_count;
143 	}
144 
145 	return result;
146 }
147 
148 
149 /* Release an entity back to the pool */
150 static int free_entity(const io_entity_t *entity)
151 {
152 	int result;
153 	unsigned int index = 0;
154 	assert(entity != NULL);
155 
156 	result = find_first_entity(entity, &index);
157 	if (result ==  0) {
158 		entity_map[index] = NULL;
159 		--entity_count;
160 	}
161 
162 	return result;
163 }
164 
165 
166 /* Exported API */
167 
168 /* Register a device driver */
169 int io_register_device(const io_dev_info_t *dev_info)
170 {
171 	int result = -ENOMEM;
172 	assert(dev_info != NULL);
173 
174 	if (dev_count < MAX_IO_DEVICES) {
175 		devices[dev_count] = dev_info;
176 		dev_count++;
177 		result = 0;
178 	}
179 
180 	return result;
181 }
182 
183 
184 /* Open a connection to an IO device */
185 int io_dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec,
186 		uintptr_t *handle)
187 {
188 	int result;
189 	assert(handle != NULL);
190 
191 	result = dev_open(dev_con, dev_spec, (io_dev_info_t **)handle);
192 	return result;
193 }
194 
195 
196 /* Initialise an IO device explicitly - to permit lazy initialisation or
197  * re-initialisation */
198 int io_dev_init(uintptr_t dev_handle, const uintptr_t init_params)
199 {
200 	int result = 0;
201 	assert(dev_handle != (uintptr_t)NULL);
202 	assert(is_valid_dev(dev_handle));
203 
204 	io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
205 
206 	/* Absence of registered function implies NOP here */
207 	if (dev->funcs->dev_init != NULL) {
208 		result = dev->funcs->dev_init(dev, init_params);
209 	}
210 
211 	return result;
212 }
213 
214 
215 /* TODO: Consider whether an explicit "shutdown" API should be included */
216 
217 /* Close a connection to a device */
218 int io_dev_close(uintptr_t dev_handle)
219 {
220 	int result = 0;
221 	assert(dev_handle != (uintptr_t)NULL);
222 	assert(is_valid_dev(dev_handle));
223 
224 	io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
225 
226 	/* Absence of registered function implies NOP here */
227 	if (dev->funcs->dev_close != NULL) {
228 		result = dev->funcs->dev_close(dev);
229 	}
230 
231 	return result;
232 }
233 
234 
235 /* Synchronous operations */
236 
237 
238 /* Open an IO entity */
239 int io_open(uintptr_t dev_handle, const uintptr_t spec, uintptr_t *handle)
240 {
241 	int result;
242 	assert((spec != (uintptr_t)NULL) && (handle != NULL));
243 	assert(is_valid_dev(dev_handle));
244 
245 	io_dev_info_t *dev = (io_dev_info_t *)dev_handle;
246 	io_entity_t *entity;
247 
248 	result = allocate_entity(&entity);
249 
250 	if (result == 0) {
251 		assert(dev->funcs->open != NULL);
252 		result = dev->funcs->open(dev, spec, entity);
253 
254 		if (result == 0) {
255 			entity->dev_handle = dev;
256 			set_handle(handle, entity);
257 		} else
258 			free_entity(entity);
259 	}
260 	return result;
261 }
262 
263 
264 /* Seek to a specific position in an IO entity */
265 int io_seek(uintptr_t handle, io_seek_mode_t mode, ssize_t offset)
266 {
267 	int result = -ENODEV;
268 	assert(is_valid_entity(handle) && is_valid_seek_mode(mode));
269 
270 	io_entity_t *entity = (io_entity_t *)handle;
271 
272 	io_dev_info_t *dev = entity->dev_handle;
273 
274 	if (dev->funcs->seek != NULL)
275 		result = dev->funcs->seek(entity, mode, offset);
276 
277 	return result;
278 }
279 
280 
281 /* Determine the length of an IO entity */
282 int io_size(uintptr_t handle, size_t *length)
283 {
284 	int result = -ENODEV;
285 	assert(is_valid_entity(handle) && (length != NULL));
286 
287 	io_entity_t *entity = (io_entity_t *)handle;
288 
289 	io_dev_info_t *dev = entity->dev_handle;
290 
291 	if (dev->funcs->size != NULL)
292 		result = dev->funcs->size(entity, length);
293 
294 	return result;
295 }
296 
297 
298 /* Read data from an IO entity */
299 int io_read(uintptr_t handle,
300 		uintptr_t buffer,
301 		size_t length,
302 		size_t *length_read)
303 {
304 	int result = -ENODEV;
305 	assert(is_valid_entity(handle) && (buffer != (uintptr_t)NULL));
306 
307 	io_entity_t *entity = (io_entity_t *)handle;
308 
309 	io_dev_info_t *dev = entity->dev_handle;
310 
311 	if (dev->funcs->read != NULL)
312 		result = dev->funcs->read(entity, buffer, length, length_read);
313 
314 	return result;
315 }
316 
317 
318 /* Write data to an IO entity */
319 int io_write(uintptr_t handle,
320 		const uintptr_t buffer,
321 		size_t length,
322 		size_t *length_written)
323 {
324 	int result = -ENODEV;
325 	assert(is_valid_entity(handle) && (buffer != (uintptr_t)NULL));
326 
327 	io_entity_t *entity = (io_entity_t *)handle;
328 
329 	io_dev_info_t *dev = entity->dev_handle;
330 
331 	if (dev->funcs->write != NULL) {
332 		result = dev->funcs->write(entity, buffer, length,
333 				length_written);
334 	}
335 
336 	return result;
337 }
338 
339 
340 /* Close an IO entity */
341 int io_close(uintptr_t handle)
342 {
343 	int result = 0;
344 	assert(is_valid_entity(handle));
345 
346 	io_entity_t *entity = (io_entity_t *)handle;
347 
348 	io_dev_info_t *dev = entity->dev_handle;
349 
350 	/* Absence of registered function implies NOP here */
351 	if (dev->funcs->close != NULL)
352 		result = dev->funcs->close(entity);
353 
354 	/* Ignore improbable free_entity failure */
355 	(void)free_entity(entity);
356 
357 	return result;
358 }
359