memset_32.c 6.2 KB

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