xor_64.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #ifndef _ASM_X86_XOR_64_H
  2. #define _ASM_X86_XOR_64_H
  3. /*
  4. * Optimized RAID-5 checksumming functions for MMX and SSE.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * (for example /usr/src/linux/COPYING); if not, write to the Free
  13. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. /*
  16. * Cache avoiding checksumming functions utilizing KNI instructions
  17. * Copyright (C) 1999 Zach Brown (with obvious credit due Ingo)
  18. */
  19. /*
  20. * Based on
  21. * High-speed RAID5 checksumming functions utilizing SSE instructions.
  22. * Copyright (C) 1998 Ingo Molnar.
  23. */
  24. /*
  25. * x86-64 changes / gcc fixes from Andi Kleen.
  26. * Copyright 2002 Andi Kleen, SuSE Labs.
  27. *
  28. * This hasn't been optimized for the hammer yet, but there are likely
  29. * no advantages to be gotten from x86-64 here anyways.
  30. */
  31. typedef struct {
  32. unsigned long a, b;
  33. } __attribute__((aligned(16))) xmm_store_t;
  34. /* Doesn't use gcc to save the XMM registers, because there is no easy way to
  35. tell it to do a clts before the register saving. */
  36. #define XMMS_SAVE \
  37. do { \
  38. preempt_disable(); \
  39. asm volatile( \
  40. "movq %%cr0,%0 ;\n\t" \
  41. "clts ;\n\t" \
  42. "movups %%xmm0,(%1) ;\n\t" \
  43. "movups %%xmm1,0x10(%1) ;\n\t" \
  44. "movups %%xmm2,0x20(%1) ;\n\t" \
  45. "movups %%xmm3,0x30(%1) ;\n\t" \
  46. : "=&r" (cr0) \
  47. : "r" (xmm_save) \
  48. : "memory"); \
  49. } while (0)
  50. #define XMMS_RESTORE \
  51. do { \
  52. asm volatile( \
  53. "sfence ;\n\t" \
  54. "movups (%1),%%xmm0 ;\n\t" \
  55. "movups 0x10(%1),%%xmm1 ;\n\t" \
  56. "movups 0x20(%1),%%xmm2 ;\n\t" \
  57. "movups 0x30(%1),%%xmm3 ;\n\t" \
  58. "movq %0,%%cr0 ;\n\t" \
  59. : \
  60. : "r" (cr0), "r" (xmm_save) \
  61. : "memory"); \
  62. preempt_enable(); \
  63. } while (0)
  64. #define OFFS(x) "16*("#x")"
  65. #define PF_OFFS(x) "256+16*("#x")"
  66. #define PF0(x) " prefetchnta "PF_OFFS(x)"(%[p1]) ;\n"
  67. #define LD(x, y) " movaps "OFFS(x)"(%[p1]), %%xmm"#y" ;\n"
  68. #define ST(x, y) " movaps %%xmm"#y", "OFFS(x)"(%[p1]) ;\n"
  69. #define PF1(x) " prefetchnta "PF_OFFS(x)"(%[p2]) ;\n"
  70. #define PF2(x) " prefetchnta "PF_OFFS(x)"(%[p3]) ;\n"
  71. #define PF3(x) " prefetchnta "PF_OFFS(x)"(%[p4]) ;\n"
  72. #define PF4(x) " prefetchnta "PF_OFFS(x)"(%[p5]) ;\n"
  73. #define PF5(x) " prefetchnta "PF_OFFS(x)"(%[p6]) ;\n"
  74. #define XO1(x, y) " xorps "OFFS(x)"(%[p2]), %%xmm"#y" ;\n"
  75. #define XO2(x, y) " xorps "OFFS(x)"(%[p3]), %%xmm"#y" ;\n"
  76. #define XO3(x, y) " xorps "OFFS(x)"(%[p4]), %%xmm"#y" ;\n"
  77. #define XO4(x, y) " xorps "OFFS(x)"(%[p5]), %%xmm"#y" ;\n"
  78. #define XO5(x, y) " xorps "OFFS(x)"(%[p6]), %%xmm"#y" ;\n"
  79. static void
  80. xor_sse_2(unsigned long bytes, unsigned long *p1, unsigned long *p2)
  81. {
  82. unsigned int lines = bytes >> 8;
  83. unsigned long cr0;
  84. xmm_store_t xmm_save[4];
  85. XMMS_SAVE;
  86. asm volatile(
  87. #undef BLOCK
  88. #define BLOCK(i) \
  89. LD(i, 0) \
  90. LD(i + 1, 1) \
  91. PF1(i) \
  92. PF1(i + 2) \
  93. LD(i + 2, 2) \
  94. LD(i + 3, 3) \
  95. PF0(i + 4) \
  96. PF0(i + 6) \
  97. XO1(i, 0) \
  98. XO1(i + 1, 1) \
  99. XO1(i + 2, 2) \
  100. XO1(i + 3, 3) \
  101. ST(i, 0) \
  102. ST(i + 1, 1) \
  103. ST(i + 2, 2) \
  104. ST(i + 3, 3) \
  105. PF0(0)
  106. PF0(2)
  107. " .align 32 ;\n"
  108. " 1: ;\n"
  109. BLOCK(0)
  110. BLOCK(4)
  111. BLOCK(8)
  112. BLOCK(12)
  113. " addq %[inc], %[p1] ;\n"
  114. " addq %[inc], %[p2] ;\n"
  115. " decl %[cnt] ; jnz 1b"
  116. : [p1] "+r" (p1), [p2] "+r" (p2), [cnt] "+r" (lines)
  117. : [inc] "r" (256UL)
  118. : "memory");
  119. XMMS_RESTORE;
  120. }
  121. static void
  122. xor_sse_3(unsigned long bytes, unsigned long *p1, unsigned long *p2,
  123. unsigned long *p3)
  124. {
  125. unsigned int lines = bytes >> 8;
  126. xmm_store_t xmm_save[4];
  127. unsigned long cr0;
  128. XMMS_SAVE;
  129. asm volatile(
  130. #undef BLOCK
  131. #define BLOCK(i) \
  132. PF1(i) \
  133. PF1(i + 2) \
  134. LD(i, 0) \
  135. LD(i + 1, 1) \
  136. LD(i + 2, 2) \
  137. LD(i + 3, 3) \
  138. PF2(i) \
  139. PF2(i + 2) \
  140. PF0(i + 4) \
  141. PF0(i + 6) \
  142. XO1(i, 0) \
  143. XO1(i + 1, 1) \
  144. XO1(i + 2, 2) \
  145. XO1(i + 3, 3) \
  146. XO2(i, 0) \
  147. XO2(i + 1, 1) \
  148. XO2(i + 2, 2) \
  149. XO2(i + 3, 3) \
  150. ST(i, 0) \
  151. ST(i + 1, 1) \
  152. ST(i + 2, 2) \
  153. ST(i + 3, 3) \
  154. PF0(0)
  155. PF0(2)
  156. " .align 32 ;\n"
  157. " 1: ;\n"
  158. BLOCK(0)
  159. BLOCK(4)
  160. BLOCK(8)
  161. BLOCK(12)
  162. " addq %[inc], %[p1] ;\n"
  163. " addq %[inc], %[p2] ;\n"
  164. " addq %[inc], %[p3] ;\n"
  165. " decl %[cnt] ; jnz 1b"
  166. : [cnt] "+r" (lines),
  167. [p1] "+r" (p1), [p2] "+r" (p2), [p3] "+r" (p3)
  168. : [inc] "r" (256UL)
  169. : "memory");
  170. XMMS_RESTORE;
  171. }
  172. static void
  173. xor_sse_4(unsigned long bytes, unsigned long *p1, unsigned long *p2,
  174. unsigned long *p3, unsigned long *p4)
  175. {
  176. unsigned int lines = bytes >> 8;
  177. xmm_store_t xmm_save[4];
  178. unsigned long cr0;
  179. XMMS_SAVE;
  180. asm volatile(
  181. #undef BLOCK
  182. #define BLOCK(i) \
  183. PF1(i) \
  184. PF1(i + 2) \
  185. LD(i, 0) \
  186. LD(i + 1, 1) \
  187. LD(i + 2, 2) \
  188. LD(i + 3, 3) \
  189. PF2(i) \
  190. PF2(i + 2) \
  191. XO1(i, 0) \
  192. XO1(i + 1, 1) \
  193. XO1(i + 2, 2) \
  194. XO1(i + 3, 3) \
  195. PF3(i) \
  196. PF3(i + 2) \
  197. PF0(i + 4) \
  198. PF0(i + 6) \
  199. XO2(i, 0) \
  200. XO2(i + 1, 1) \
  201. XO2(i + 2, 2) \
  202. XO2(i + 3, 3) \
  203. XO3(i, 0) \
  204. XO3(i + 1, 1) \
  205. XO3(i + 2, 2) \
  206. XO3(i + 3, 3) \
  207. ST(i, 0) \
  208. ST(i + 1, 1) \
  209. ST(i + 2, 2) \
  210. ST(i + 3, 3) \
  211. PF0(0)
  212. PF0(2)
  213. " .align 32 ;\n"
  214. " 1: ;\n"
  215. BLOCK(0)
  216. BLOCK(4)
  217. BLOCK(8)
  218. BLOCK(12)
  219. " addq %[inc], %[p1] ;\n"
  220. " addq %[inc], %[p2] ;\n"
  221. " addq %[inc], %[p3] ;\n"
  222. " addq %[inc], %[p4] ;\n"
  223. " decl %[cnt] ; jnz 1b"
  224. : [cnt] "+c" (lines),
  225. [p1] "+r" (p1), [p2] "+r" (p2), [p3] "+r" (p3), [p4] "+r" (p4)
  226. : [inc] "r" (256UL)
  227. : "memory" );
  228. XMMS_RESTORE;
  229. }
  230. static void
  231. xor_sse_5(unsigned long bytes, unsigned long *p1, unsigned long *p2,
  232. unsigned long *p3, unsigned long *p4, unsigned long *p5)
  233. {
  234. unsigned int lines = bytes >> 8;
  235. xmm_store_t xmm_save[4];
  236. unsigned long cr0;
  237. XMMS_SAVE;
  238. asm volatile(
  239. #undef BLOCK
  240. #define BLOCK(i) \
  241. PF1(i) \
  242. PF1(i + 2) \
  243. LD(i, 0) \
  244. LD(i + 1, 1) \
  245. LD(i + 2, 2) \
  246. LD(i + 3, 3) \
  247. PF2(i) \
  248. PF2(i + 2) \
  249. XO1(i, 0) \
  250. XO1(i + 1, 1) \
  251. XO1(i + 2, 2) \
  252. XO1(i + 3, 3) \
  253. PF3(i) \
  254. PF3(i + 2) \
  255. XO2(i, 0) \
  256. XO2(i + 1, 1) \
  257. XO2(i + 2, 2) \
  258. XO2(i + 3, 3) \
  259. PF4(i) \
  260. PF4(i + 2) \
  261. PF0(i + 4) \
  262. PF0(i + 6) \
  263. XO3(i, 0) \
  264. XO3(i + 1, 1) \
  265. XO3(i + 2, 2) \
  266. XO3(i + 3, 3) \
  267. XO4(i, 0) \
  268. XO4(i + 1, 1) \
  269. XO4(i + 2, 2) \
  270. XO4(i + 3, 3) \
  271. ST(i, 0) \
  272. ST(i + 1, 1) \
  273. ST(i + 2, 2) \
  274. ST(i + 3, 3) \
  275. PF0(0)
  276. PF0(2)
  277. " .align 32 ;\n"
  278. " 1: ;\n"
  279. BLOCK(0)
  280. BLOCK(4)
  281. BLOCK(8)
  282. BLOCK(12)
  283. " addq %[inc], %[p1] ;\n"
  284. " addq %[inc], %[p2] ;\n"
  285. " addq %[inc], %[p3] ;\n"
  286. " addq %[inc], %[p4] ;\n"
  287. " addq %[inc], %[p5] ;\n"
  288. " decl %[cnt] ; jnz 1b"
  289. : [cnt] "+c" (lines),
  290. [p1] "+r" (p1), [p2] "+r" (p2), [p3] "+r" (p3), [p4] "+r" (p4),
  291. [p5] "+r" (p5)
  292. : [inc] "r" (256UL)
  293. : "memory");
  294. XMMS_RESTORE;
  295. }
  296. static struct xor_block_template xor_block_sse = {
  297. .name = "generic_sse",
  298. .do_2 = xor_sse_2,
  299. .do_3 = xor_sse_3,
  300. .do_4 = xor_sse_4,
  301. .do_5 = xor_sse_5,
  302. };
  303. #undef XOR_TRY_TEMPLATES
  304. #define XOR_TRY_TEMPLATES \
  305. do { \
  306. xor_speed(&xor_block_sse); \
  307. } while (0)
  308. /* We force the use of the SSE xor block because it can write around L2.
  309. We may also be able to load into the L1 only depending on how the cpu
  310. deals with a load to a line that is being prefetched. */
  311. #define XOR_SELECT_TEMPLATE(FASTEST) (&xor_block_sse)
  312. #endif /* _ASM_X86_XOR_64_H */