crc32le-vx.S 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Hardware-accelerated CRC-32 variants for Linux on z Systems
  3. *
  4. * Use the z/Architecture Vector Extension Facility to accelerate the
  5. * computing of bitreflected CRC-32 checksums for IEEE 802.3 Ethernet
  6. * and Castagnoli.
  7. *
  8. * This CRC-32 implementation algorithm is bitreflected and processes
  9. * the least-significant bit first (Little-Endian).
  10. *
  11. * Copyright IBM Corp. 2015
  12. * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
  13. */
  14. #include <linux/linkage.h>
  15. #include <asm/nospec-insn.h>
  16. #include <asm/vx-insn.h>
  17. /* Vector register range containing CRC-32 constants */
  18. #define CONST_PERM_LE2BE %v9
  19. #define CONST_R2R1 %v10
  20. #define CONST_R4R3 %v11
  21. #define CONST_R5 %v12
  22. #define CONST_RU_POLY %v13
  23. #define CONST_CRC_POLY %v14
  24. .data
  25. .align 8
  26. /*
  27. * The CRC-32 constant block contains reduction constants to fold and
  28. * process particular chunks of the input data stream in parallel.
  29. *
  30. * For the CRC-32 variants, the constants are precomputed according to
  31. * these definitions:
  32. *
  33. * R1 = [(x4*128+32 mod P'(x) << 32)]' << 1
  34. * R2 = [(x4*128-32 mod P'(x) << 32)]' << 1
  35. * R3 = [(x128+32 mod P'(x) << 32)]' << 1
  36. * R4 = [(x128-32 mod P'(x) << 32)]' << 1
  37. * R5 = [(x64 mod P'(x) << 32)]' << 1
  38. * R6 = [(x32 mod P'(x) << 32)]' << 1
  39. *
  40. * The bitreflected Barret reduction constant, u', is defined as
  41. * the bit reversal of floor(x**64 / P(x)).
  42. *
  43. * where P(x) is the polynomial in the normal domain and the P'(x) is the
  44. * polynomial in the reversed (bitreflected) domain.
  45. *
  46. * CRC-32 (IEEE 802.3 Ethernet, ...) polynomials:
  47. *
  48. * P(x) = 0x04C11DB7
  49. * P'(x) = 0xEDB88320
  50. *
  51. * CRC-32C (Castagnoli) polynomials:
  52. *
  53. * P(x) = 0x1EDC6F41
  54. * P'(x) = 0x82F63B78
  55. */
  56. .Lconstants_CRC_32_LE:
  57. .octa 0x0F0E0D0C0B0A09080706050403020100 # BE->LE mask
  58. .quad 0x1c6e41596, 0x154442bd4 # R2, R1
  59. .quad 0x0ccaa009e, 0x1751997d0 # R4, R3
  60. .octa 0x163cd6124 # R5
  61. .octa 0x1F7011641 # u'
  62. .octa 0x1DB710641 # P'(x) << 1
  63. .Lconstants_CRC_32C_LE:
  64. .octa 0x0F0E0D0C0B0A09080706050403020100 # BE->LE mask
  65. .quad 0x09e4addf8, 0x740eef02 # R2, R1
  66. .quad 0x14cd00bd6, 0xf20c0dfe # R4, R3
  67. .octa 0x0dd45aab8 # R5
  68. .octa 0x0dea713f1 # u'
  69. .octa 0x105ec76f0 # P'(x) << 1
  70. .previous
  71. GEN_BR_THUNK %r14
  72. .text
  73. /*
  74. * The CRC-32 functions use these calling conventions:
  75. *
  76. * Parameters:
  77. *
  78. * %r2: Initial CRC value, typically ~0; and final CRC (return) value.
  79. * %r3: Input buffer pointer, performance might be improved if the
  80. * buffer is on a doubleword boundary.
  81. * %r4: Length of the buffer, must be 64 bytes or greater.
  82. *
  83. * Register usage:
  84. *
  85. * %r5: CRC-32 constant pool base pointer.
  86. * V0: Initial CRC value and intermediate constants and results.
  87. * V1..V4: Data for CRC computation.
  88. * V5..V8: Next data chunks that are fetched from the input buffer.
  89. * V9: Constant for BE->LE conversion and shift operations
  90. *
  91. * V10..V14: CRC-32 constants.
  92. */
  93. ENTRY(crc32_le_vgfm_16)
  94. larl %r5,.Lconstants_CRC_32_LE
  95. j crc32_le_vgfm_generic
  96. ENTRY(crc32c_le_vgfm_16)
  97. larl %r5,.Lconstants_CRC_32C_LE
  98. j crc32_le_vgfm_generic
  99. crc32_le_vgfm_generic:
  100. /* Load CRC-32 constants */
  101. VLM CONST_PERM_LE2BE,CONST_CRC_POLY,0,%r5
  102. /*
  103. * Load the initial CRC value.
  104. *
  105. * The CRC value is loaded into the rightmost word of the
  106. * vector register and is later XORed with the LSB portion
  107. * of the loaded input data.
  108. */
  109. VZERO %v0 /* Clear V0 */
  110. VLVGF %v0,%r2,3 /* Load CRC into rightmost word */
  111. /* Load a 64-byte data chunk and XOR with CRC */
  112. VLM %v1,%v4,0,%r3 /* 64-bytes into V1..V4 */
  113. VPERM %v1,%v1,%v1,CONST_PERM_LE2BE
  114. VPERM %v2,%v2,%v2,CONST_PERM_LE2BE
  115. VPERM %v3,%v3,%v3,CONST_PERM_LE2BE
  116. VPERM %v4,%v4,%v4,CONST_PERM_LE2BE
  117. VX %v1,%v0,%v1 /* V1 ^= CRC */
  118. aghi %r3,64 /* BUF = BUF + 64 */
  119. aghi %r4,-64 /* LEN = LEN - 64 */
  120. cghi %r4,64
  121. jl .Lless_than_64bytes
  122. .Lfold_64bytes_loop:
  123. /* Load the next 64-byte data chunk into V5 to V8 */
  124. VLM %v5,%v8,0,%r3
  125. VPERM %v5,%v5,%v5,CONST_PERM_LE2BE
  126. VPERM %v6,%v6,%v6,CONST_PERM_LE2BE
  127. VPERM %v7,%v7,%v7,CONST_PERM_LE2BE
  128. VPERM %v8,%v8,%v8,CONST_PERM_LE2BE
  129. /*
  130. * Perform a GF(2) multiplication of the doublewords in V1 with
  131. * the R1 and R2 reduction constants in V0. The intermediate result
  132. * is then folded (accumulated) with the next data chunk in V5 and
  133. * stored in V1. Repeat this step for the register contents
  134. * in V2, V3, and V4 respectively.
  135. */
  136. VGFMAG %v1,CONST_R2R1,%v1,%v5
  137. VGFMAG %v2,CONST_R2R1,%v2,%v6
  138. VGFMAG %v3,CONST_R2R1,%v3,%v7
  139. VGFMAG %v4,CONST_R2R1,%v4,%v8
  140. aghi %r3,64 /* BUF = BUF + 64 */
  141. aghi %r4,-64 /* LEN = LEN - 64 */
  142. cghi %r4,64
  143. jnl .Lfold_64bytes_loop
  144. .Lless_than_64bytes:
  145. /*
  146. * Fold V1 to V4 into a single 128-bit value in V1. Multiply V1 with R3
  147. * and R4 and accumulating the next 128-bit chunk until a single 128-bit
  148. * value remains.
  149. */
  150. VGFMAG %v1,CONST_R4R3,%v1,%v2
  151. VGFMAG %v1,CONST_R4R3,%v1,%v3
  152. VGFMAG %v1,CONST_R4R3,%v1,%v4
  153. cghi %r4,16
  154. jl .Lfinal_fold
  155. .Lfold_16bytes_loop:
  156. VL %v2,0,,%r3 /* Load next data chunk */
  157. VPERM %v2,%v2,%v2,CONST_PERM_LE2BE
  158. VGFMAG %v1,CONST_R4R3,%v1,%v2 /* Fold next data chunk */
  159. aghi %r3,16
  160. aghi %r4,-16
  161. cghi %r4,16
  162. jnl .Lfold_16bytes_loop
  163. .Lfinal_fold:
  164. /*
  165. * Set up a vector register for byte shifts. The shift value must
  166. * be loaded in bits 1-4 in byte element 7 of a vector register.
  167. * Shift by 8 bytes: 0x40
  168. * Shift by 4 bytes: 0x20
  169. */
  170. VLEIB %v9,0x40,7
  171. /*
  172. * Prepare V0 for the next GF(2) multiplication: shift V0 by 8 bytes
  173. * to move R4 into the rightmost doubleword and set the leftmost
  174. * doubleword to 0x1.
  175. */
  176. VSRLB %v0,CONST_R4R3,%v9
  177. VLEIG %v0,1,0
  178. /*
  179. * Compute GF(2) product of V1 and V0. The rightmost doubleword
  180. * of V1 is multiplied with R4. The leftmost doubleword of V1 is
  181. * multiplied by 0x1 and is then XORed with rightmost product.
  182. * Implicitly, the intermediate leftmost product becomes padded
  183. */
  184. VGFMG %v1,%v0,%v1
  185. /*
  186. * Now do the final 32-bit fold by multiplying the rightmost word
  187. * in V1 with R5 and XOR the result with the remaining bits in V1.
  188. *
  189. * To achieve this by a single VGFMAG, right shift V1 by a word
  190. * and store the result in V2 which is then accumulated. Use the
  191. * vector unpack instruction to load the rightmost half of the
  192. * doubleword into the rightmost doubleword element of V1; the other
  193. * half is loaded in the leftmost doubleword.
  194. * The vector register with CONST_R5 contains the R5 constant in the
  195. * rightmost doubleword and the leftmost doubleword is zero to ignore
  196. * the leftmost product of V1.
  197. */
  198. VLEIB %v9,0x20,7 /* Shift by words */
  199. VSRLB %v2,%v1,%v9 /* Store remaining bits in V2 */
  200. VUPLLF %v1,%v1 /* Split rightmost doubleword */
  201. VGFMAG %v1,CONST_R5,%v1,%v2 /* V1 = (V1 * R5) XOR V2 */
  202. /*
  203. * Apply a Barret reduction to compute the final 32-bit CRC value.
  204. *
  205. * The input values to the Barret reduction are the degree-63 polynomial
  206. * in V1 (R(x)), degree-32 generator polynomial, and the reduction
  207. * constant u. The Barret reduction result is the CRC value of R(x) mod
  208. * P(x).
  209. *
  210. * The Barret reduction algorithm is defined as:
  211. *
  212. * 1. T1(x) = floor( R(x) / x^32 ) GF2MUL u
  213. * 2. T2(x) = floor( T1(x) / x^32 ) GF2MUL P(x)
  214. * 3. C(x) = R(x) XOR T2(x) mod x^32
  215. *
  216. * Note: The leftmost doubleword of vector register containing
  217. * CONST_RU_POLY is zero and, thus, the intermediate GF(2) product
  218. * is zero and does not contribute to the final result.
  219. */
  220. /* T1(x) = floor( R(x) / x^32 ) GF2MUL u */
  221. VUPLLF %v2,%v1
  222. VGFMG %v2,CONST_RU_POLY,%v2
  223. /*
  224. * Compute the GF(2) product of the CRC polynomial with T1(x) in
  225. * V2 and XOR the intermediate result, T2(x), with the value in V1.
  226. * The final result is stored in word element 2 of V2.
  227. */
  228. VUPLLF %v2,%v2
  229. VGFMAG %v2,CONST_CRC_POLY,%v2,%v1
  230. .Ldone:
  231. VLGVF %r2,%v2,2
  232. BR_EX %r14
  233. .previous