crc32le-vx.S 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 <brueckner@linux.vnet.ibm.com>
  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. ENTRY(crc32c_le_vgfm_16)
  98. larl %r5,.Lconstants_CRC_32C_LE
  99. j crc32_le_vgfm_generic
  100. crc32_le_vgfm_generic:
  101. /* Load CRC-32 constants */
  102. VLM CONST_PERM_LE2BE,CONST_CRC_POLY,0,%r5
  103. /*
  104. * Load the initial CRC value.
  105. *
  106. * The CRC value is loaded into the rightmost word of the
  107. * vector register and is later XORed with the LSB portion
  108. * of the loaded input data.
  109. */
  110. VZERO %v0 /* Clear V0 */
  111. VLVGF %v0,%r2,3 /* Load CRC into rightmost word */
  112. /* Load a 64-byte data chunk and XOR with CRC */
  113. VLM %v1,%v4,0,%r3 /* 64-bytes into V1..V4 */
  114. VPERM %v1,%v1,%v1,CONST_PERM_LE2BE
  115. VPERM %v2,%v2,%v2,CONST_PERM_LE2BE
  116. VPERM %v3,%v3,%v3,CONST_PERM_LE2BE
  117. VPERM %v4,%v4,%v4,CONST_PERM_LE2BE
  118. VX %v1,%v0,%v1 /* V1 ^= CRC */
  119. aghi %r3,64 /* BUF = BUF + 64 */
  120. aghi %r4,-64 /* LEN = LEN - 64 */
  121. cghi %r4,64
  122. jl .Lless_than_64bytes
  123. .Lfold_64bytes_loop:
  124. /* Load the next 64-byte data chunk into V5 to V8 */
  125. VLM %v5,%v8,0,%r3
  126. VPERM %v5,%v5,%v5,CONST_PERM_LE2BE
  127. VPERM %v6,%v6,%v6,CONST_PERM_LE2BE
  128. VPERM %v7,%v7,%v7,CONST_PERM_LE2BE
  129. VPERM %v8,%v8,%v8,CONST_PERM_LE2BE
  130. /*
  131. * Perform a GF(2) multiplication of the doublewords in V1 with
  132. * the R1 and R2 reduction constants in V0. The intermediate result
  133. * is then folded (accumulated) with the next data chunk in V5 and
  134. * stored in V1. Repeat this step for the register contents
  135. * in V2, V3, and V4 respectively.
  136. */
  137. VGFMAG %v1,CONST_R2R1,%v1,%v5
  138. VGFMAG %v2,CONST_R2R1,%v2,%v6
  139. VGFMAG %v3,CONST_R2R1,%v3,%v7
  140. VGFMAG %v4,CONST_R2R1,%v4,%v8
  141. aghi %r3,64 /* BUF = BUF + 64 */
  142. aghi %r4,-64 /* LEN = LEN - 64 */
  143. cghi %r4,64
  144. jnl .Lfold_64bytes_loop
  145. .Lless_than_64bytes:
  146. /*
  147. * Fold V1 to V4 into a single 128-bit value in V1. Multiply V1 with R3
  148. * and R4 and accumulating the next 128-bit chunk until a single 128-bit
  149. * value remains.
  150. */
  151. VGFMAG %v1,CONST_R4R3,%v1,%v2
  152. VGFMAG %v1,CONST_R4R3,%v1,%v3
  153. VGFMAG %v1,CONST_R4R3,%v1,%v4
  154. cghi %r4,16
  155. jl .Lfinal_fold
  156. .Lfold_16bytes_loop:
  157. VL %v2,0,,%r3 /* Load next data chunk */
  158. VPERM %v2,%v2,%v2,CONST_PERM_LE2BE
  159. VGFMAG %v1,CONST_R4R3,%v1,%v2 /* Fold next data chunk */
  160. aghi %r3,16
  161. aghi %r4,-16
  162. cghi %r4,16
  163. jnl .Lfold_16bytes_loop
  164. .Lfinal_fold:
  165. /*
  166. * Set up a vector register for byte shifts. The shift value must
  167. * be loaded in bits 1-4 in byte element 7 of a vector register.
  168. * Shift by 8 bytes: 0x40
  169. * Shift by 4 bytes: 0x20
  170. */
  171. VLEIB %v9,0x40,7
  172. /*
  173. * Prepare V0 for the next GF(2) multiplication: shift V0 by 8 bytes
  174. * to move R4 into the rightmost doubleword and set the leftmost
  175. * doubleword to 0x1.
  176. */
  177. VSRLB %v0,CONST_R4R3,%v9
  178. VLEIG %v0,1,0
  179. /*
  180. * Compute GF(2) product of V1 and V0. The rightmost doubleword
  181. * of V1 is multiplied with R4. The leftmost doubleword of V1 is
  182. * multiplied by 0x1 and is then XORed with rightmost product.
  183. * Implicitly, the intermediate leftmost product becomes padded
  184. */
  185. VGFMG %v1,%v0,%v1
  186. /*
  187. * Now do the final 32-bit fold by multiplying the rightmost word
  188. * in V1 with R5 and XOR the result with the remaining bits in V1.
  189. *
  190. * To achieve this by a single VGFMAG, right shift V1 by a word
  191. * and store the result in V2 which is then accumulated. Use the
  192. * vector unpack instruction to load the rightmost half of the
  193. * doubleword into the rightmost doubleword element of V1; the other
  194. * half is loaded in the leftmost doubleword.
  195. * The vector register with CONST_R5 contains the R5 constant in the
  196. * rightmost doubleword and the leftmost doubleword is zero to ignore
  197. * the leftmost product of V1.
  198. */
  199. VLEIB %v9,0x20,7 /* Shift by words */
  200. VSRLB %v2,%v1,%v9 /* Store remaining bits in V2 */
  201. VUPLLF %v1,%v1 /* Split rightmost doubleword */
  202. VGFMAG %v1,CONST_R5,%v1,%v2 /* V1 = (V1 * R5) XOR V2 */
  203. /*
  204. * Apply a Barret reduction to compute the final 32-bit CRC value.
  205. *
  206. * The input values to the Barret reduction are the degree-63 polynomial
  207. * in V1 (R(x)), degree-32 generator polynomial, and the reduction
  208. * constant u. The Barret reduction result is the CRC value of R(x) mod
  209. * P(x).
  210. *
  211. * The Barret reduction algorithm is defined as:
  212. *
  213. * 1. T1(x) = floor( R(x) / x^32 ) GF2MUL u
  214. * 2. T2(x) = floor( T1(x) / x^32 ) GF2MUL P(x)
  215. * 3. C(x) = R(x) XOR T2(x) mod x^32
  216. *
  217. * Note: The leftmost doubleword of vector register containing
  218. * CONST_RU_POLY is zero and, thus, the intermediate GF(2) product
  219. * is zero and does not contribute to the final result.
  220. */
  221. /* T1(x) = floor( R(x) / x^32 ) GF2MUL u */
  222. VUPLLF %v2,%v1
  223. VGFMG %v2,CONST_RU_POLY,%v2
  224. /*
  225. * Compute the GF(2) product of the CRC polynomial with T1(x) in
  226. * V2 and XOR the intermediate result, T2(x), with the value in V1.
  227. * The final result is stored in word element 2 of V2.
  228. */
  229. VUPLLF %v2,%v2
  230. VGFMAG %v2,CONST_CRC_POLY,%v2,%v1
  231. .Ldone:
  232. VLGVF %r2,%v2,2
  233. BR_EX %r14
  234. .previous