memset_32.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 1
  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. #else
  135. /* Unfortunately, due to a code generator flaw this
  136. * allocates a separate register for each of these
  137. * stores, which requires a large number of spills,
  138. * which makes this procedure enormously bigger
  139. * (something like 70%)
  140. */
  141. *out32++ = v32;
  142. *out32++ = v32;
  143. *out32++ = v32;
  144. *out32++ = v32;
  145. *out32++ = v32;
  146. *out32++ = v32;
  147. *out32++ = v32;
  148. *out32++ = v32;
  149. *out32++ = v32;
  150. *out32++ = v32;
  151. *out32++ = v32;
  152. *out32++ = v32;
  153. *out32++ = v32;
  154. *out32++ = v32;
  155. *out32++ = v32;
  156. n32 -= 16;
  157. #endif
  158. /* To save compiled code size, reuse this loop even
  159. * when we run out of prefetching to do by dropping
  160. * ahead32 down.
  161. */
  162. if (n32 <= ahead32) {
  163. /* Not even a full cache line left,
  164. * so stop now.
  165. */
  166. if (n32 < CACHE_LINE_SIZE_IN_WORDS)
  167. break;
  168. /* Choose a small enough value that we don't
  169. * prefetch past the end. There's no sense
  170. * in touching cache lines we don't have to.
  171. */
  172. ahead32 = CACHE_LINE_SIZE_IN_WORDS - 1;
  173. }
  174. }
  175. }
  176. #else /* CHIP_HAS_WH64() */
  177. /* Determine how many words we need to emit before the 'out32'
  178. * pointer becomes aligned modulo the cache line size.
  179. */
  180. to_align32 =
  181. (-((uintptr_t)out32 >> 2)) & (CACHE_LINE_SIZE_IN_WORDS - 1);
  182. /* Only bother aligning and using wh64 if there is at least
  183. * one full cache line to process. This check also prevents
  184. * overrunning the end of the buffer with alignment words.
  185. */
  186. if (to_align32 <= n32 - CACHE_LINE_SIZE_IN_WORDS) {
  187. int lines_left;
  188. /* Align out32 mod the cache line size so we can use wh64. */
  189. n32 -= to_align32;
  190. for (; to_align32 != 0; to_align32--) {
  191. *out32 = v32;
  192. out32++;
  193. }
  194. /* Use unsigned divide to turn this into a right shift. */
  195. lines_left = (unsigned)n32 / CACHE_LINE_SIZE_IN_WORDS;
  196. do {
  197. /* Only wh64 a few lines at a time, so we don't
  198. * exceed the maximum number of victim lines.
  199. */
  200. int x = ((lines_left < CHIP_MAX_OUTSTANDING_VICTIMS())
  201. ? lines_left
  202. : CHIP_MAX_OUTSTANDING_VICTIMS());
  203. uint32_t *wh = out32;
  204. int i = x;
  205. int j;
  206. lines_left -= x;
  207. do {
  208. __insn_wh64(wh);
  209. wh += CACHE_LINE_SIZE_IN_WORDS;
  210. } while (--i);
  211. for (j = x * (CACHE_LINE_SIZE_IN_WORDS / 4); j != 0; j--) {
  212. *out32++ = v32;
  213. *out32++ = v32;
  214. *out32++ = v32;
  215. *out32++ = v32;
  216. }
  217. } while (lines_left != 0);
  218. /* We processed all full lines above, so only this many
  219. * words remain to be processed.
  220. */
  221. n32 &= CACHE_LINE_SIZE_IN_WORDS - 1;
  222. }
  223. #endif /* CHIP_HAS_WH64() */
  224. /* Now handle any leftover values. */
  225. if (n32 != 0) {
  226. do {
  227. *out32 = v32;
  228. out32++;
  229. } while (--n32 != 0);
  230. }
  231. return s;
  232. }
  233. EXPORT_SYMBOL(memset);