xor_avx.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #ifndef _ASM_X86_XOR_AVX_H
  2. #define _ASM_X86_XOR_AVX_H
  3. /*
  4. * Optimized RAID-5 checksumming functions for AVX
  5. *
  6. * Copyright (C) 2012 Intel Corporation
  7. * Author: Jim Kukunas <james.t.kukunas@linux.intel.com>
  8. *
  9. * Based on Ingo Molnar and Zach Brown's respective MMX and SSE routines
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; version 2
  14. * of the License.
  15. */
  16. #ifdef CONFIG_AS_AVX
  17. #include <linux/compiler.h>
  18. #include <asm/i387.h>
  19. #define BLOCK4(i) \
  20. BLOCK(32 * i, 0) \
  21. BLOCK(32 * (i + 1), 1) \
  22. BLOCK(32 * (i + 2), 2) \
  23. BLOCK(32 * (i + 3), 3)
  24. #define BLOCK16() \
  25. BLOCK4(0) \
  26. BLOCK4(4) \
  27. BLOCK4(8) \
  28. BLOCK4(12)
  29. static void xor_avx_2(unsigned long bytes, unsigned long *p0, unsigned long *p1)
  30. {
  31. unsigned long lines = bytes >> 9;
  32. kernel_fpu_begin();
  33. while (lines--) {
  34. #undef BLOCK
  35. #define BLOCK(i, reg) \
  36. do { \
  37. asm volatile("vmovdqa %0, %%ymm" #reg : : "m" (p1[i / sizeof(*p1)])); \
  38. asm volatile("vxorps %0, %%ymm" #reg ", %%ymm" #reg : : \
  39. "m" (p0[i / sizeof(*p0)])); \
  40. asm volatile("vmovdqa %%ymm" #reg ", %0" : \
  41. "=m" (p0[i / sizeof(*p0)])); \
  42. } while (0);
  43. BLOCK16()
  44. p0 = (unsigned long *)((uintptr_t)p0 + 512);
  45. p1 = (unsigned long *)((uintptr_t)p1 + 512);
  46. }
  47. kernel_fpu_end();
  48. }
  49. static void xor_avx_3(unsigned long bytes, unsigned long *p0, unsigned long *p1,
  50. unsigned long *p2)
  51. {
  52. unsigned long lines = bytes >> 9;
  53. kernel_fpu_begin();
  54. while (lines--) {
  55. #undef BLOCK
  56. #define BLOCK(i, reg) \
  57. do { \
  58. asm volatile("vmovdqa %0, %%ymm" #reg : : "m" (p2[i / sizeof(*p2)])); \
  59. asm volatile("vxorps %0, %%ymm" #reg ", %%ymm" #reg : : \
  60. "m" (p1[i / sizeof(*p1)])); \
  61. asm volatile("vxorps %0, %%ymm" #reg ", %%ymm" #reg : : \
  62. "m" (p0[i / sizeof(*p0)])); \
  63. asm volatile("vmovdqa %%ymm" #reg ", %0" : \
  64. "=m" (p0[i / sizeof(*p0)])); \
  65. } while (0);
  66. BLOCK16()
  67. p0 = (unsigned long *)((uintptr_t)p0 + 512);
  68. p1 = (unsigned long *)((uintptr_t)p1 + 512);
  69. p2 = (unsigned long *)((uintptr_t)p2 + 512);
  70. }
  71. kernel_fpu_end();
  72. }
  73. static void xor_avx_4(unsigned long bytes, unsigned long *p0, unsigned long *p1,
  74. unsigned long *p2, unsigned long *p3)
  75. {
  76. unsigned long lines = bytes >> 9;
  77. kernel_fpu_begin();
  78. while (lines--) {
  79. #undef BLOCK
  80. #define BLOCK(i, reg) \
  81. do { \
  82. asm volatile("vmovdqa %0, %%ymm" #reg : : "m" (p3[i / sizeof(*p3)])); \
  83. asm volatile("vxorps %0, %%ymm" #reg ", %%ymm" #reg : : \
  84. "m" (p2[i / sizeof(*p2)])); \
  85. asm volatile("vxorps %0, %%ymm" #reg ", %%ymm" #reg : : \
  86. "m" (p1[i / sizeof(*p1)])); \
  87. asm volatile("vxorps %0, %%ymm" #reg ", %%ymm" #reg : : \
  88. "m" (p0[i / sizeof(*p0)])); \
  89. asm volatile("vmovdqa %%ymm" #reg ", %0" : \
  90. "=m" (p0[i / sizeof(*p0)])); \
  91. } while (0);
  92. BLOCK16();
  93. p0 = (unsigned long *)((uintptr_t)p0 + 512);
  94. p1 = (unsigned long *)((uintptr_t)p1 + 512);
  95. p2 = (unsigned long *)((uintptr_t)p2 + 512);
  96. p3 = (unsigned long *)((uintptr_t)p3 + 512);
  97. }
  98. kernel_fpu_end();
  99. }
  100. static void xor_avx_5(unsigned long bytes, unsigned long *p0, unsigned long *p1,
  101. unsigned long *p2, unsigned long *p3, unsigned long *p4)
  102. {
  103. unsigned long lines = bytes >> 9;
  104. kernel_fpu_begin();
  105. while (lines--) {
  106. #undef BLOCK
  107. #define BLOCK(i, reg) \
  108. do { \
  109. asm volatile("vmovdqa %0, %%ymm" #reg : : "m" (p4[i / sizeof(*p4)])); \
  110. asm volatile("vxorps %0, %%ymm" #reg ", %%ymm" #reg : : \
  111. "m" (p3[i / sizeof(*p3)])); \
  112. asm volatile("vxorps %0, %%ymm" #reg ", %%ymm" #reg : : \
  113. "m" (p2[i / sizeof(*p2)])); \
  114. asm volatile("vxorps %0, %%ymm" #reg ", %%ymm" #reg : : \
  115. "m" (p1[i / sizeof(*p1)])); \
  116. asm volatile("vxorps %0, %%ymm" #reg ", %%ymm" #reg : : \
  117. "m" (p0[i / sizeof(*p0)])); \
  118. asm volatile("vmovdqa %%ymm" #reg ", %0" : \
  119. "=m" (p0[i / sizeof(*p0)])); \
  120. } while (0);
  121. BLOCK16()
  122. p0 = (unsigned long *)((uintptr_t)p0 + 512);
  123. p1 = (unsigned long *)((uintptr_t)p1 + 512);
  124. p2 = (unsigned long *)((uintptr_t)p2 + 512);
  125. p3 = (unsigned long *)((uintptr_t)p3 + 512);
  126. p4 = (unsigned long *)((uintptr_t)p4 + 512);
  127. }
  128. kernel_fpu_end();
  129. }
  130. static struct xor_block_template xor_block_avx = {
  131. .name = "avx",
  132. .do_2 = xor_avx_2,
  133. .do_3 = xor_avx_3,
  134. .do_4 = xor_avx_4,
  135. .do_5 = xor_avx_5,
  136. };
  137. #define AVX_XOR_SPEED \
  138. do { \
  139. if (cpu_has_avx) \
  140. xor_speed(&xor_block_avx); \
  141. } while (0)
  142. #define AVX_SELECT(FASTEST) \
  143. (cpu_has_avx ? &xor_block_avx : FASTEST)
  144. #else
  145. #define AVX_XOR_SPEED {}
  146. #define AVX_SELECT(FASTEST) (FASTEST)
  147. #endif
  148. #endif