/*1* Copyright (C) 2008-2009 Michal Simek <[email protected]>2* Copyright (C) 2008-2009 PetaLogix3* Copyright (C) 2007 John Williams4*5* Reasonably optimised generic C-code for memcpy on Microblaze6* This is generic C code to do efficient, alignment-aware memmove.7*8* It is based on demo code originally Copyright 2001 by Intel Corp, taken from9* http://www.embedded.com/showArticle.jhtml?articleID=1920556710*11* Attempts were made, unsuccessfully, to contact the original12* author of this code (Michael Morrow, Intel). Below is the original13* copyright notice.14*15* This software has been developed by Intel Corporation.16* Intel specifically disclaims all warranties, express or17* implied, and all liability, including consequential and18* other indirect damages, for the use of this program, including19* liability for infringement of any proprietary rights,20* and including the warranties of merchantability and fitness21* for a particular purpose. Intel does not assume any22* responsibility for and errors which may appear in this program23* not any responsibility to update it.24*/2526#include <linux/types.h>27#include <linux/stddef.h>28#include <linux/compiler.h>29#include <linux/module.h>30#include <linux/string.h>3132#ifdef __HAVE_ARCH_MEMMOVE33#ifndef CONFIG_OPT_LIB_FUNCTION34void *memmove(void *v_dst, const void *v_src, __kernel_size_t c)35{36const char *src = v_src;37char *dst = v_dst;3839if (!c)40return v_dst;4142/* Use memcpy when source is higher than dest */43if (v_dst <= v_src)44return memcpy(v_dst, v_src, c);4546/* copy backwards, from end to beginning */47src += c;48dst += c;4950/* Simple, byte oriented memmove. */51while (c--)52*--dst = *--src;5354return v_dst;55}56#else /* CONFIG_OPT_LIB_FUNCTION */57void *memmove(void *v_dst, const void *v_src, __kernel_size_t c)58{59const char *src = v_src;60char *dst = v_dst;61const uint32_t *i_src;62uint32_t *i_dst;6364if (!c)65return v_dst;6667/* Use memcpy when source is higher than dest */68if (v_dst <= v_src)69return memcpy(v_dst, v_src, c);7071/* The following code tries to optimize the copy by using unsigned72* alignment. This will work fine if both source and destination are73* aligned on the same boundary. However, if they are aligned on74* different boundaries shifts will be necessary. This might result in75* bad performance on MicroBlaze systems without a barrel shifter.76*/77/* FIXME this part needs more test */78/* Do a descending copy - this is a bit trickier! */79dst += c;80src += c;8182if (c >= 4) {83unsigned value, buf_hold;8485/* Align the destination to a word boundary. */86/* This is done in an endian independent manner. */8788switch ((unsigned long)dst & 3) {89case 3:90*--dst = *--src;91--c;92case 2:93*--dst = *--src;94--c;95case 1:96*--dst = *--src;97--c;98}99100i_dst = (void *)dst;101/* Choose a copy scheme based on the source */102/* alignment relative to dstination. */103switch ((unsigned long)src & 3) {104case 0x0: /* Both byte offsets are aligned */105106i_src = (const void *)src;107108for (; c >= 4; c -= 4)109*--i_dst = *--i_src;110111src = (const void *)i_src;112break;113case 0x1: /* Unaligned - Off by 1 */114/* Word align the source */115i_src = (const void *) (((unsigned)src + 4) & ~3);116#ifndef __MICROBLAZEEL__117/* Load the holding buffer */118buf_hold = *--i_src >> 24;119120for (; c >= 4; c -= 4) {121value = *--i_src;122*--i_dst = buf_hold << 8 | value;123buf_hold = value >> 24;124}125#else126/* Load the holding buffer */127buf_hold = (*--i_src & 0xFF) << 24;128129for (; c >= 4; c -= 4) {130value = *--i_src;131*--i_dst = buf_hold | ((value & 0xFFFFFF00)>>8);132buf_hold = (value & 0xFF) << 24;133}134#endif135/* Realign the source */136src = (const void *)i_src;137src += 1;138break;139case 0x2: /* Unaligned - Off by 2 */140/* Word align the source */141i_src = (const void *) (((unsigned)src + 4) & ~3);142#ifndef __MICROBLAZEEL__143/* Load the holding buffer */144buf_hold = *--i_src >> 16;145146for (; c >= 4; c -= 4) {147value = *--i_src;148*--i_dst = buf_hold << 16 | value;149buf_hold = value >> 16;150}151#else152/* Load the holding buffer */153buf_hold = (*--i_src & 0xFFFF) << 16;154155for (; c >= 4; c -= 4) {156value = *--i_src;157*--i_dst = buf_hold | ((value & 0xFFFF0000)>>16);158buf_hold = (value & 0xFFFF) << 16;159}160#endif161/* Realign the source */162src = (const void *)i_src;163src += 2;164break;165case 0x3: /* Unaligned - Off by 3 */166/* Word align the source */167i_src = (const void *) (((unsigned)src + 4) & ~3);168#ifndef __MICROBLAZEEL__169/* Load the holding buffer */170buf_hold = *--i_src >> 8;171172for (; c >= 4; c -= 4) {173value = *--i_src;174*--i_dst = buf_hold << 24 | value;175buf_hold = value >> 8;176}177#else178/* Load the holding buffer */179buf_hold = (*--i_src & 0xFFFFFF) << 8;180181for (; c >= 4; c -= 4) {182value = *--i_src;183*--i_dst = buf_hold | ((value & 0xFF000000)>> 24);184buf_hold = (value & 0xFFFFFF) << 8;185}186#endif187/* Realign the source */188src = (const void *)i_src;189src += 3;190break;191}192dst = (void *)i_dst;193}194195/* simple fast copy, ... unless a cache boundary is crossed */196/* Finish off any remaining bytes */197switch (c) {198case 4:199*--dst = *--src;200case 3:201*--dst = *--src;202case 2:203*--dst = *--src;204case 1:205*--dst = *--src;206}207return v_dst;208}209#endif /* CONFIG_OPT_LIB_FUNCTION */210EXPORT_SYMBOL(memmove);211#endif /* __HAVE_ARCH_MEMMOVE */212213214