memcpy_64.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #define __memcpy memcpy
  18. /* EXPORT_SYMBOL() is in arch/tile/lib/exports.c since this should be asm. */
  19. /* Must be 8 bytes in size. */
  20. #define word_t uint64_t
  21. #if CHIP_L2_LINE_SIZE() != 64 && CHIP_L2_LINE_SIZE() != 128
  22. #error "Assumes 64 or 128 byte line size"
  23. #endif
  24. /* How many cache lines ahead should we prefetch? */
  25. #define PREFETCH_LINES_AHEAD 3
  26. /*
  27. * Provide "base versions" of load and store for the normal code path.
  28. * The kernel provides other versions for userspace copies.
  29. */
  30. #define ST(p, v) (*(p) = (v))
  31. #define LD(p) (*(p))
  32. #ifndef USERCOPY_FUNC
  33. #define ST1 ST
  34. #define ST2 ST
  35. #define ST4 ST
  36. #define ST8 ST
  37. #define LD1 LD
  38. #define LD2 LD
  39. #define LD4 LD
  40. #define LD8 LD
  41. #define RETVAL dstv
  42. void *memcpy(void *__restrict dstv, const void *__restrict srcv, size_t n)
  43. #else
  44. /*
  45. * Special kernel version will provide implementation of the LDn/STn
  46. * macros to return a count of uncopied bytes due to mm fault.
  47. */
  48. #define RETVAL 0
  49. int USERCOPY_FUNC(void *__restrict dstv, const void *__restrict srcv, size_t n)
  50. #endif
  51. {
  52. char *__restrict dst1 = (char *)dstv;
  53. const char *__restrict src1 = (const char *)srcv;
  54. const char *__restrict src1_end;
  55. const char *__restrict prefetch;
  56. word_t *__restrict dst8; /* 8-byte pointer to destination memory. */
  57. word_t final; /* Final bytes to write to trailing word, if any */
  58. long i;
  59. if (n < 16) {
  60. for (; n; n--)
  61. ST1(dst1++, LD1(src1++));
  62. return RETVAL;
  63. }
  64. /*
  65. * Locate the end of source memory we will copy. Don't
  66. * prefetch past this.
  67. */
  68. src1_end = src1 + n - 1;
  69. /* Prefetch ahead a few cache lines, but not past the end. */
  70. prefetch = src1;
  71. for (i = 0; i < PREFETCH_LINES_AHEAD; i++) {
  72. __insn_prefetch(prefetch);
  73. prefetch += CHIP_L2_LINE_SIZE();
  74. prefetch = (prefetch > src1_end) ? prefetch : src1;
  75. }
  76. /* Copy bytes until dst is word-aligned. */
  77. for (; (uintptr_t)dst1 & (sizeof(word_t) - 1); n--)
  78. ST1(dst1++, LD1(src1++));
  79. /* 8-byte pointer to destination memory. */
  80. dst8 = (word_t *)dst1;
  81. if (__builtin_expect((uintptr_t)src1 & (sizeof(word_t) - 1), 0)) {
  82. /*
  83. * Misaligned copy. Copy 8 bytes at a time, but don't
  84. * bother with other fanciness.
  85. *
  86. * TODO: Consider prefetching and using wh64 as well.
  87. */
  88. /* Create an aligned src8. */
  89. const word_t *__restrict src8 =
  90. (const word_t *)((uintptr_t)src1 & -sizeof(word_t));
  91. word_t b;
  92. word_t a = LD8(src8++);
  93. for (; n >= sizeof(word_t); n -= sizeof(word_t)) {
  94. b = LD8(src8++);
  95. a = __insn_dblalign(a, b, src1);
  96. ST8(dst8++, a);
  97. a = b;
  98. }
  99. if (n == 0)
  100. return RETVAL;
  101. b = ((const char *)src8 <= src1_end) ? *src8 : 0;
  102. /*
  103. * Final source bytes to write to trailing partial
  104. * word, if any.
  105. */
  106. final = __insn_dblalign(a, b, src1);
  107. } else {
  108. /* Aligned copy. */
  109. const word_t* __restrict src8 = (const word_t *)src1;
  110. /* src8 and dst8 are both word-aligned. */
  111. if (n >= CHIP_L2_LINE_SIZE()) {
  112. /* Copy until 'dst' is cache-line-aligned. */
  113. for (; (uintptr_t)dst8 & (CHIP_L2_LINE_SIZE() - 1);
  114. n -= sizeof(word_t))
  115. ST8(dst8++, LD8(src8++));
  116. for (; n >= CHIP_L2_LINE_SIZE(); ) {
  117. __insn_wh64(dst8);
  118. /*
  119. * Prefetch and advance to next line
  120. * to prefetch, but don't go past the end
  121. */
  122. __insn_prefetch(prefetch);
  123. prefetch += CHIP_L2_LINE_SIZE();
  124. prefetch = (prefetch > src1_end) ? prefetch :
  125. (const char *)src8;
  126. /*
  127. * Copy an entire cache line. Manually
  128. * unrolled to avoid idiosyncracies of
  129. * compiler unrolling.
  130. */
  131. #define COPY_WORD(offset) ({ ST8(dst8+offset, LD8(src8+offset)); n -= 8; })
  132. COPY_WORD(0);
  133. COPY_WORD(1);
  134. COPY_WORD(2);
  135. COPY_WORD(3);
  136. COPY_WORD(4);
  137. COPY_WORD(5);
  138. COPY_WORD(6);
  139. COPY_WORD(7);
  140. #if CHIP_L2_LINE_SIZE() == 128
  141. COPY_WORD(8);
  142. COPY_WORD(9);
  143. COPY_WORD(10);
  144. COPY_WORD(11);
  145. COPY_WORD(12);
  146. COPY_WORD(13);
  147. COPY_WORD(14);
  148. COPY_WORD(15);
  149. #elif CHIP_L2_LINE_SIZE() != 64
  150. # error Fix code that assumes particular L2 cache line sizes
  151. #endif
  152. dst8 += CHIP_L2_LINE_SIZE() / sizeof(word_t);
  153. src8 += CHIP_L2_LINE_SIZE() / sizeof(word_t);
  154. }
  155. }
  156. for (; n >= sizeof(word_t); n -= sizeof(word_t))
  157. ST8(dst8++, LD8(src8++));
  158. if (__builtin_expect(n == 0, 1))
  159. return RETVAL;
  160. final = LD8(src8);
  161. }
  162. /* n != 0 if we get here. Write out any trailing bytes. */
  163. dst1 = (char *)dst8;
  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. return RETVAL;
  179. }
  180. #ifdef USERCOPY_FUNC
  181. #undef ST1
  182. #undef ST2
  183. #undef ST4
  184. #undef ST8
  185. #undef LD1
  186. #undef LD2
  187. #undef LD4
  188. #undef LD8
  189. #undef USERCOPY_FUNC
  190. #endif