2 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
3 * Copyright (C) 2008-2009 PetaLogix
4 * Copyright (C) 2007 John Williams
6 * Reasonably optimised generic C-code for memcpy on Microblaze
7 * This is generic C code to do efficient, alignment-aware memcpy.
9 * It is based on demo code originally Copyright 2001 by Intel Corp, taken from
10 * http://www.embedded.com/showArticle.jhtml?articleID=19205567
12 * Attempts were made, unsuccessfully, to contact the original
13 * author of this code (Michael Morrow, Intel). Below is the original
16 * This software has been developed by Intel Corporation.
17 * Intel specifically disclaims all warranties, express or
18 * implied, and all liability, including consequential and
19 * other indirect damages, for the use of this program, including
20 * liability for infringement of any proprietary rights,
21 * and including the warranties of merchantability and fitness
22 * for a particular purpose. Intel does not assume any
23 * responsibility for and errors which may appear in this program
24 * not any responsibility to update it.
27 #include <linux/export.h>
28 #include <linux/types.h>
29 #include <linux/stddef.h>
30 #include <linux/compiler.h>
32 #include <linux/string.h>
34 #ifdef __HAVE_ARCH_MEMCPY
35 #ifndef CONFIG_OPT_LIB_FUNCTION
36 void *memcpy(void *v_dst, const void *v_src, __kernel_size_t c)
38 const char *src = v_src;
41 /* Simple, byte oriented memcpy. */
47 #else /* CONFIG_OPT_LIB_FUNCTION */
48 void *memcpy(void *v_dst, const void *v_src, __kernel_size_t c)
50 const char *src = v_src;
53 /* The following code tries to optimize the copy by using unsigned
54 * alignment. This will work fine if both source and destination are
55 * aligned on the same boundary. However, if they are aligned on
56 * different boundaries shifts will be necessary. This might result in
57 * bad performance on MicroBlaze systems without a barrel shifter.
59 const uint32_t *i_src;
63 unsigned value, buf_hold;
65 /* Align the destination to a word boundary. */
66 /* This is done in an endian independent manner. */
67 switch ((unsigned long)dst & 3) {
81 /* Choose a copy scheme based on the source */
82 /* alignment relative to destination. */
83 switch ((unsigned long)src & 3) {
84 case 0x0: /* Both byte offsets are aligned */
85 i_src = (const void *)src;
87 for (; c >= 4; c -= 4)
90 src = (const void *)i_src;
92 case 0x1: /* Unaligned - Off by 1 */
93 /* Word align the source */
94 i_src = (const void *) ((unsigned)src & ~3);
95 #ifndef __MICROBLAZEEL__
96 /* Load the holding buffer */
97 buf_hold = *i_src++ << 8;
99 for (; c >= 4; c -= 4) {
101 *i_dst++ = buf_hold | value >> 24;
102 buf_hold = value << 8;
105 /* Load the holding buffer */
106 buf_hold = (*i_src++ & 0xFFFFFF00) >> 8;
108 for (; c >= 4; c -= 4) {
110 *i_dst++ = buf_hold | ((value & 0xFF) << 24);
111 buf_hold = (value & 0xFFFFFF00) >> 8;
114 /* Realign the source */
115 src = (const void *)i_src;
118 case 0x2: /* Unaligned - Off by 2 */
119 /* Word align the source */
120 i_src = (const void *) ((unsigned)src & ~3);
121 #ifndef __MICROBLAZEEL__
122 /* Load the holding buffer */
123 buf_hold = *i_src++ << 16;
125 for (; c >= 4; c -= 4) {
127 *i_dst++ = buf_hold | value >> 16;
128 buf_hold = value << 16;
131 /* Load the holding buffer */
132 buf_hold = (*i_src++ & 0xFFFF0000) >> 16;
134 for (; c >= 4; c -= 4) {
136 *i_dst++ = buf_hold | ((value & 0xFFFF) << 16);
137 buf_hold = (value & 0xFFFF0000) >> 16;
140 /* Realign the source */
141 src = (const void *)i_src;
144 case 0x3: /* Unaligned - Off by 3 */
145 /* Word align the source */
146 i_src = (const void *) ((unsigned)src & ~3);
147 #ifndef __MICROBLAZEEL__
148 /* Load the holding buffer */
149 buf_hold = *i_src++ << 24;
151 for (; c >= 4; c -= 4) {
153 *i_dst++ = buf_hold | value >> 8;
154 buf_hold = value << 24;
157 /* Load the holding buffer */
158 buf_hold = (*i_src++ & 0xFF000000) >> 24;
160 for (; c >= 4; c -= 4) {
162 *i_dst++ = buf_hold | ((value & 0xFFFFFF) << 8);
163 buf_hold = (value & 0xFF000000) >> 24;
166 /* Realign the source */
167 src = (const void *)i_src;
174 /* Finish off any remaining bytes */
175 /* simple fast copy, ... unless a cache boundary is crossed */
187 #endif /* CONFIG_OPT_LIB_FUNCTION */
188 EXPORT_SYMBOL(memcpy);
189 #endif /* __HAVE_ARCH_MEMCPY */