crc32le-vx.S 7.8 KB

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