crc32le-vx.S 7.6 KB

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