memset_32.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright 2010 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 <arch/chip.h>
  15. #include <linux/types.h>
  16. #include <linux/string.h>
  17. #include <linux/module.h>
  18. #undef memset
  19. void *memset(void *s, int c, size_t n)
  20. {
  21. uint32_t *out32;
  22. int n32;
  23. uint32_t v16, v32;
  24. uint8_t *out8 = s;
  25. #if !CHIP_HAS_WH64()
  26. int ahead32;
  27. #else
  28. int to_align32;
  29. #endif
  30. /* Experimentation shows that a trivial tight loop is a win up until
  31. * around a size of 20, where writing a word at a time starts to win.
  32. */
  33. #define BYTE_CUTOFF 20
  34. #if BYTE_CUTOFF < 3
  35. /* This must be at least at least this big, or some code later
  36. * on doesn't work.
  37. */
  38. #error "BYTE_CUTOFF is too small"
  39. #endif
  40. if (n < BYTE_CUTOFF) {
  41. /* Strangely, this turns out to be the tightest way to
  42. * write this loop.
  43. */
  44. if (n != 0) {
  45. do {
  46. /* Strangely, combining these into one line
  47. * performs worse.
  48. */
  49. *out8 = c;
  50. out8++;
  51. } while (--n != 0);
  52. }
  53. return s;
  54. }
  55. #if !CHIP_HAS_WH64()
  56. /* Use a spare issue slot to start prefetching the first cache
  57. * line early. This instruction is free as the store can be buried
  58. * in otherwise idle issue slots doing ALU ops.
  59. */
  60. __insn_prefetch(out8);
  61. /* We prefetch the end so that a short memset that spans two cache
  62. * lines gets some prefetching benefit. Again we believe this is free
  63. * to issue.
  64. */
  65. __insn_prefetch(&out8[n - 1]);
  66. #endif /* !CHIP_HAS_WH64() */
  67. /* Align 'out8'. We know n >= 3 so this won't write past the end. */
  68. while (((uintptr_t) out8 & 3) != 0) {
  69. *out8++ = c;
  70. --n;
  71. }
  72. /* Align 'n'. */
  73. while (n & 3)
  74. out8[--n] = c;
  75. out32 = (uint32_t *) out8;
  76. n32 = n >> 2;
  77. /* Tile input byte out to 32 bits. */
  78. v16 = __insn_intlb(c, c);
  79. v32 = __insn_intlh(v16, v16);
  80. /* This must be at least 8 or the following loop doesn't work. */
  81. #define CACHE_LINE_SIZE_IN_WORDS (CHIP_L2_LINE_SIZE() / 4)
  82. #if !CHIP_HAS_WH64()
  83. ahead32 = CACHE_LINE_SIZE_IN_WORDS;
  84. /* We already prefetched the first and last cache lines, so
  85. * we only need to do more prefetching if we are storing
  86. * to more than two cache lines.
  87. */
  88. if (n32 > CACHE_LINE_SIZE_IN_WORDS * 2) {
  89. int i;
  90. /* Prefetch the next several cache lines.
  91. * This is the setup code for the software-pipelined
  92. * loop below.
  93. */
  94. #define MAX_PREFETCH 5
  95. ahead32 = n32 & -CACHE_LINE_SIZE_IN_WORDS;
  96. if (ahead32 > MAX_PREFETCH * CACHE_LINE_SIZE_IN_WORDS)
  97. ahead32 = MAX_PREFETCH * CACHE_LINE_SIZE_IN_WORDS;
  98. for (i = CACHE_LINE_SIZE_IN_WORDS;
  99. i < ahead32; i += CACHE_LINE_SIZE_IN_WORDS)
  100. __insn_prefetch(&out32[i]);
  101. }
  102. if (n32 > ahead32) {
  103. while (1) {
  104. int j;
  105. /* Prefetch by reading one word several cache lines
  106. * ahead. Since loads are non-blocking this will
  107. * cause the full cache line to be read while we are
  108. * finishing earlier cache lines. Using a store
  109. * here causes microarchitectural performance
  110. * problems where a victimizing store miss goes to
  111. * the head of the retry FIFO and locks the pipe for
  112. * a few cycles. So a few subsequent stores in this
  113. * loop go into the retry FIFO, and then later
  114. * stores see other stores to the same cache line
  115. * are already in the retry FIFO and themselves go
  116. * into the retry FIFO, filling it up and grinding
  117. * to a halt waiting for the original miss to be
  118. * satisfied.
  119. */
  120. __insn_prefetch(&out32[ahead32]);
  121. #if CACHE_LINE_SIZE_IN_WORDS % 4 != 0
  122. #error "Unhandled CACHE_LINE_SIZE_IN_WORDS"
  123. #endif
  124. n32 -= CACHE_LINE_SIZE_IN_WORDS;
  125. /* Save icache space by only partially unrolling
  126. * this loop.
  127. */
  128. for (j = CACHE_LINE_SIZE_IN_WORDS / 4; j > 0; j--) {
  129. *out32++ = v32;
  130. *out32++ = v32;
  131. *out32++ = v32;
  132. *out32++ = v32;
  133. }
  134. /* To save compiled code size, reuse this loop even
  135. * when we run out of prefetching to do by dropping
  136. * ahead32 down.
  137. */
  138. if (n32 <= ahead32) {
  139. /* Not even a full cache line left,
  140. * so stop now.
  141. */
  142. if (n32 < CACHE_LINE_SIZE_IN_WORDS)
  143. break;
  144. /* Choose a small enough value that we don't
  145. * prefetch past the end. There's no sense
  146. * in touching cache lines we don't have to.
  147. */
  148. ahead32 = CACHE_LINE_SIZE_IN_WORDS - 1;
  149. }
  150. }
  151. }
  152. #else /* CHIP_HAS_WH64() */
  153. /* Determine how many words we need to emit before the 'out32'
  154. * pointer becomes aligned modulo the cache line size.
  155. */
  156. to_align32 =
  157. (-((uintptr_t)out32 >> 2)) & (CACHE_LINE_SIZE_IN_WORDS - 1);
  158. /* Only bother aligning and using wh64 if there is at least
  159. * one full cache line to process. This check also prevents
  160. * overrunning the end of the buffer with alignment words.
  161. */
  162. if (to_align32 <= n32 - CACHE_LINE_SIZE_IN_WORDS) {
  163. int lines_left;
  164. /* Align out32 mod the cache line size so we can use wh64. */
  165. n32 -= to_align32;
  166. for (; to_align32 != 0; to_align32--) {
  167. *out32 = v32;
  168. out32++;
  169. }
  170. /* Use unsigned divide to turn this into a right shift. */
  171. lines_left = (unsigned)n32 / CACHE_LINE_SIZE_IN_WORDS;
  172. do {
  173. /* Only wh64 a few lines at a time, so we don't
  174. * exceed the maximum number of victim lines.
  175. */
  176. int x = ((lines_left < CHIP_MAX_OUTSTANDING_VICTIMS())
  177. ? lines_left
  178. : CHIP_MAX_OUTSTANDING_VICTIMS());
  179. uint32_t *wh = out32;
  180. int i = x;
  181. int j;
  182. lines_left -= x;
  183. do {
  184. __insn_wh64(wh);
  185. wh += CACHE_LINE_SIZE_IN_WORDS;
  186. } while (--i);
  187. for (j = x * (CACHE_LINE_SIZE_IN_WORDS / 4);
  188. j != 0; j--) {
  189. *out32++ = v32;
  190. *out32++ = v32;
  191. *out32++ = v32;
  192. *out32++ = v32;
  193. }
  194. } while (lines_left != 0);
  195. /* We processed all full lines above, so only this many
  196. * words remain to be processed.
  197. */
  198. n32 &= CACHE_LINE_SIZE_IN_WORDS - 1;
  199. }
  200. #endif /* CHIP_HAS_WH64() */
  201. /* Now handle any leftover values. */
  202. if (n32 != 0) {
  203. do {
  204. *out32 = v32;
  205. out32++;
  206. } while (--n32 != 0);
  207. }
  208. return s;
  209. }
  210. EXPORT_SYMBOL(memset);