aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWendy Liang2016-08-08 18:46:48 -0500
committerWendy Liang2016-08-09 11:54:46 -0500
commit93b5a366f90a195338207ec56276fe04cbc12c94 (patch)
treee89f3262547510fc0749b661480859bc06485318
parented325d2a38dccd6fdb7710cbb0d19a8112a4451f (diff)
downloadlibmetal-93b5a366f90a195338207ec56276fe04cbc12c94.tar.gz
libmetal-93b5a366f90a195338207ec56276fe04cbc12c94.tar.xz
libmetal-93b5a366f90a195338207ec56276fe04cbc12c94.zip
Add libmetal memset_io and memcpy_io
The memcpy/memset may not work on device memory on some architecture, as some architecture assumes the memcpy/memset will only access aligned device memory address. We add memset_io and memcpy_io to allow memset/memcpy unaligned device memory access. Signed-off-by: Wendy Liang <jliang@xilinx.com>
-rw-r--r--lib/CMakeLists.txt1
-rw-r--r--lib/io.c80
-rw-r--r--lib/io.h26
3 files changed, 107 insertions, 0 deletions
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 516afdc..48248f8 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -30,6 +30,7 @@ collect (PROJECT_LIB_SOURCES log.c)
30collect (PROJECT_LIB_SOURCES shmem.c) 30collect (PROJECT_LIB_SOURCES shmem.c)
31collect (PROJECT_LIB_SOURCES version.c) 31collect (PROJECT_LIB_SOURCES version.c)
32collect (PROJECT_LIB_SOURCES dma.c) 32collect (PROJECT_LIB_SOURCES dma.c)
33collect (PROJECT_LIB_SOURCES io.c)
33 34
34add_subdirectory (compiler) 35add_subdirectory (compiler)
35add_subdirectory (processor) 36add_subdirectory (processor)
diff --git a/lib/io.c b/lib/io.c
new file mode 100644
index 0000000..817b302
--- /dev/null
+++ b/lib/io.c
@@ -0,0 +1,80 @@
1/*
2 * Copyright (c) 2015, Xilinx Inc. 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 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. 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 * 3. Neither the name of Xilinx nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without
16 * specific 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 "metal/io.h"
32
33void *metal_generic_memset_io(void *dst, int c, size_t size)
34{
35 void *retdst = dst;
36 unsigned int cint = (unsigned char)c;
37 unsigned int i;
38 for (i = 1; i < sizeof(int); i++)
39 cint |= (c << (8 * i));
40
41 for (; size && ((uintptr_t)dst % sizeof(int)); dst++, size--)
42 *(unsigned char volatile *)dst = (unsigned char) c;
43 for(; size >= sizeof(int); dst += sizeof(int), size -= sizeof(int))
44 *(unsigned int volatile *)dst = cint;
45 for(; size != 0; dst++, size--)
46 *(unsigned char volatile*)dst = (unsigned char) c;
47 return retdst;
48}
49
50void *metal_generic_memcpy_io(void *dst, const void *src, size_t size)
51{
52 void *retdst = dst;
53 while (size && (
54 ((uintptr_t)dst % sizeof(int)) ||
55 ((uintptr_t)src % sizeof(int)))) {
56 *(unsigned char volatile *)dst =
57 *(const unsigned char volatile *)src;
58 dst++;
59 src++;
60 size--;
61 }
62 for (; size >= sizeof(int);
63 dst += sizeof(int), src += sizeof(int), size -= sizeof(int))
64 *(unsigned int volatile *)dst =
65 *(const unsigned int volatile *)src;
66 for (; size != 0; dst++, src++, size--)
67 *(unsigned char volatile *)dst =
68 *(const unsigned char volatile *)src;
69 return retdst;
70}
71
72void *metal_memset_io(void *dst, int c, size_t size)
73{
74 return metal_generic_memset_io(dst, c, size);
75}
76
77void *metal_memcpy_io(void *dst, const void *src, size_t size)
78{
79 return metal_generic_memcpy_io(dst, src, size);
80}
diff --git a/lib/io.h b/lib/io.h
index badf84e..a0f8fcf 100644
--- a/lib/io.h
+++ b/lib/io.h
@@ -347,6 +347,32 @@ metal_io_write(struct metal_io_region *io, unsigned long offset,
347void *metal_io_mem_map(metal_phys_addr_t pa, 347void *metal_io_mem_map(metal_phys_addr_t pa,
348 struct metal_io_region *io, 348 struct metal_io_region *io,
349 size_t size); 349 size_t size);
350
351/**
352 * @brief libmetal set device memory
353 *
354 * This function is to fill the device memory with the specified value.
355 *
356 * @param[in] dst target memory
357 * @param[in] c val to fill
358 * @param[in] size size of memory to fill.
359 * @return pointer to the target memory
360 */
361void *metal_memset_io(void *dst, int c, size_t size);
362
363/**
364 * @brief libmetal copy to target memory
365 *
366 * This function is to copy specified memory area.
367 * The source memory or the destination memory can be device memory.
368 *
369 * @param[in] dst target memory
370 * @param[in] src source memory
371 * @param[in] size size of memory to copy.
372 * @return pointer to the target memory
373 */
374void *metal_memcpy_io(void *dst, const void *src, size_t size);
375
350#ifdef __cplusplus 376#ifdef __cplusplus
351} 377}
352#endif 378#endif