memcpy_64.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright 2011 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/string.h>
  16. #include <linux/module.h>
  17. /* EXPORT_SYMBOL() is in arch/tile/lib/exports.c since this should be asm. */
  18. /* Must be 8 bytes in size. */
  19. #define word_t uint64_t
  20. #if CHIP_L2_LINE_SIZE() != 64 && CHIP_L2_LINE_SIZE() != 128
  21. #error "Assumes 64 or 128 byte line size"
  22. #endif
  23. /* How many cache lines ahead should we prefetch? */
  24. #define PREFETCH_LINES_AHEAD 3
  25. /*
  26. * Provide "base versions" of load and store for the normal code path.
  27. * The kernel provides other versions for userspace copies.
  28. */
  29. #define ST(p, v) (*(p) = (v))
  30. #define LD(p) (*(p))
  31. #ifndef USERCOPY_FUNC
  32. #define ST1 ST
  33. #define ST2 ST
  34. #define ST4 ST
  35. #define ST8 ST
  36. #define LD1 LD
  37. #define LD2 LD
  38. #define LD4 LD
  39. #define LD8 LD
  40. #define RETVAL dstv
  41. void *memcpy(void *__restrict dstv, const void *__restrict srcv, size_t n)
  42. #else
  43. /*
  44. * Special kernel version will provide implementation of the LDn/STn
  45. * macros to return a count of uncopied bytes due to mm fault.
  46. */
  47. #define RETVAL 0
  48. int USERCOPY_FUNC(void *__restrict dstv, const void *__restrict srcv, size_t n)
  49. #endif
  50. {
  51. char *__restrict dst1 = (char *)dstv;
  52. const char *__restrict src1 = (const char *)srcv;
  53. const char *__restrict src1_end;
  54. const char *__restrict prefetch;
  55. word_t *__restrict dst8; /* 8-byte pointer to destination memory. */
  56. word_t final; /* Final bytes to write to trailing word, if any */
  57. long i;
  58. if (n < 16) {
  59. for (; n; n--)
  60. ST1(dst1++, LD1(src1++));
  61. return RETVAL;
  62. }
  63. /*
  64. * Locate the end of source memory we will copy. Don't
  65. * prefetch past this.
  66. */
  67. src1_end = src1 + n - 1;
  68. /* Prefetch ahead a few cache lines, but not past the end. */
  69. prefetch = src1;
  70. for (i = 0; i < PREFETCH_LINES_AHEAD; i++) {
  71. __insn_prefetch(prefetch);
  72. prefetch += CHIP_L2_LINE_SIZE();
  73. prefetch = (prefetch > src1_end) ? prefetch : src1;
  74. }
  75. /* Copy bytes until dst is word-aligned. */
  76. for (; (uintptr_t)dst1 & (sizeof(word_t) - 1); n--)
  77. ST1(dst1++, LD1(src1++));
  78. /* 8-byte pointer to destination memory. */
  79. dst8 = (word_t *)dst1;
  80. if (__builtin_expect((uintptr_t)src1 & (sizeof(word_t) - 1), 0)) {
  81. /*
  82. * Misaligned copy. Copy 8 bytes at a time, but don't
  83. * bother with other fanciness.
  84. *
  85. * TODO: Consider prefetching and using wh64 as well.
  86. */
  87. /* Create an aligned src8. */
  88. const word_t *__restrict src8 =
  89. (const word_t *)((uintptr_t)src1 & -sizeof(word_t));
  90. word_t b;
  91. word_t a = LD8(src8++);
  92. for (; n >= sizeof(word_t); n -= sizeof(word_t)) {
  93. b = LD8(src8++);
  94. a = __insn_dblalign(a, b, src1);
  95. ST8(dst8++, a);
  96. a = b;
  97. }
  98. if (n == 0)
  99. return RETVAL;
  100. b = ((const char *)src8 <= src1_end) ? *src8 : 0;
  101. /*
  102. * Final source bytes to write to trailing partial
  103. * word, if any.
  104. */
  105. final = __insn_dblalign(a, b, src1);
  106. } else {
  107. /* Aligned copy. */
  108. const word_t* __restrict src8 = (const word_t *)src1;
  109. /* src8 and dst8 are both word-aligned. */
  110. if (n >= CHIP_L2_LINE_SIZE()) {
  111. /* Copy until 'dst' is cache-line-aligned. */
  112. for (; (uintptr_t)dst8 & (CHIP_L2_LINE_SIZE() - 1);
  113. n -= sizeof(word_t))
  114. ST8(dst8++, LD8(src8++));
  115. for (; n >= CHIP_L2_LINE_SIZE(); ) {
  116. __insn_wh64(dst8);
  117. /*
  118. * Prefetch and advance to next line
  119. * to prefetch, but don't go past the end
  120. */
  121. __insn_prefetch(prefetch);
  122. prefetch += CHIP_L2_LINE_SIZE();
  123. prefetch = (prefetch > src1_end) ? prefetch :
  124. (const char *)src8;
  125. /*
  126. * Copy an entire cache line. Manually
  127. * unrolled to avoid idiosyncracies of
  128. * compiler unrolling.
  129. */
  130. #define COPY_WORD(offset) ({ ST8(dst8+offset, LD8(src8+offset)); n -= 8; })
  131. COPY_WORD(0);
  132. COPY_WORD(1);
  133. COPY_WORD(2);
  134. COPY_WORD(3);
  135. COPY_WORD(4);
  136. COPY_WORD(5);
  137. COPY_WORD(6);
  138. COPY_WORD(7);
  139. #if CHIP_L2_LINE_SIZE() == 128
  140. COPY_WORD(8);
  141. COPY_WORD(9);
  142. COPY_WORD(10);
  143. COPY_WORD(11);
  144. COPY_WORD(12);
  145. COPY_WORD(13);
  146. COPY_WORD(14);
  147. COPY_WORD(15);
  148. #elif CHIP_L2_LINE_SIZE() != 64
  149. # error Fix code that assumes particular L2 cache line sizes
  150. #endif
  151. dst8 += CHIP_L2_LINE_SIZE() / sizeof(word_t);
  152. src8 += CHIP_L2_LINE_SIZE() / sizeof(word_t);
  153. }
  154. }
  155. for (; n >= sizeof(word_t); n -= sizeof(word_t))
  156. ST8(dst8++, LD8(src8++));
  157. if (__builtin_expect(n == 0, 1))
  158. return RETVAL;
  159. final = LD8(src8);
  160. }
  161. /* n != 0 if we get here. Write out any trailing bytes. */
  162. dst1 = (char *)dst8;
  163. #ifndef __BIG_ENDIAN__
  164. if (n & 4) {
  165. ST4((uint32_t *)dst1, final);
  166. dst1 += 4;
  167. final >>= 32;
  168. n &= 3;
  169. }
  170. if (n & 2) {
  171. ST2((uint16_t *)dst1, final);
  172. dst1 += 2;
  173. final >>= 16;
  174. n &= 1;
  175. }
  176. if (n)
  177. ST1((uint8_t *)dst1, final);
  178. #else
  179. if (n & 4) {
  180. ST4((uint32_t *)dst1, final >> 32);
  181. dst1 += 4;
  182. }
  183. else
  184. {
  185. final >>= 32;
  186. }
  187. if (n & 2) {
  188. ST2((uint16_t *)dst1, final >> 16);
  189. dst1 += 2;
  190. }
  191. else
  192. {
  193. final >>= 16;
  194. }
  195. if (n & 1)
  196. ST1((uint8_t *)dst1, final >> 8);
  197. #endif
  198. return RETVAL;
  199. }
  200. #ifdef USERCOPY_FUNC
  201. #undef ST1
  202. #undef ST2
  203. #undef ST4
  204. #undef ST8
  205. #undef LD1
  206. #undef LD2
  207. #undef LD4
  208. #undef LD8
  209. #undef USERCOPY_FUNC
  210. #endif