crc32le-vx.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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/types.h>
  16. #include <asm/fpu.h>
  17. #include "crc32-vx.h"
  18. /* Vector register range containing CRC-32 constants */
  19. #define CONST_PERM_LE2BE 9
  20. #define CONST_R2R1 10
  21. #define CONST_R4R3 11
  22. #define CONST_R5 12
  23. #define CONST_RU_POLY 13
  24. #define CONST_CRC_POLY 14
  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. static unsigned long constants_CRC_32_LE[] = {
  56. 0x0f0e0d0c0b0a0908, 0x0706050403020100, /* BE->LE mask */
  57. 0x1c6e41596, 0x154442bd4, /* R2, R1 */
  58. 0x0ccaa009e, 0x1751997d0, /* R4, R3 */
  59. 0x0, 0x163cd6124, /* R5 */
  60. 0x0, 0x1f7011641, /* u' */
  61. 0x0, 0x1db710641 /* P'(x) << 1 */
  62. };
  63. static unsigned long constants_CRC_32C_LE[] = {
  64. 0x0f0e0d0c0b0a0908, 0x0706050403020100, /* BE->LE mask */
  65. 0x09e4addf8, 0x740eef02, /* R2, R1 */
  66. 0x14cd00bd6, 0xf20c0dfe, /* R4, R3 */
  67. 0x0, 0x0dd45aab8, /* R5 */
  68. 0x0, 0x0dea713f1, /* u' */
  69. 0x0, 0x105ec76f0 /* P'(x) << 1 */
  70. };
  71. /**
  72. * crc32_le_vgfm_generic - Compute CRC-32 (LE variant) with vector registers
  73. * @crc: Initial CRC value, typically ~0.
  74. * @buf: Input buffer pointer, performance might be improved if the
  75. * buffer is on a doubleword boundary.
  76. * @size: Size of the buffer, must be 64 bytes or greater.
  77. * @constants: CRC-32 constant pool base pointer.
  78. *
  79. * Register usage:
  80. * V0: Initial CRC value and intermediate constants and results.
  81. * V1..V4: Data for CRC computation.
  82. * V5..V8: Next data chunks that are fetched from the input buffer.
  83. * V9: Constant for BE->LE conversion and shift operations
  84. * V10..V14: CRC-32 constants.
  85. */
  86. static u32 crc32_le_vgfm_generic(u32 crc, unsigned char const *buf, size_t size, unsigned long *constants)
  87. {
  88. /* Load CRC-32 constants */
  89. fpu_vlm(CONST_PERM_LE2BE, CONST_CRC_POLY, constants);
  90. /*
  91. * Load the initial CRC value.
  92. *
  93. * The CRC value is loaded into the rightmost word of the
  94. * vector register and is later XORed with the LSB portion
  95. * of the loaded input data.
  96. */
  97. fpu_vzero(0); /* Clear V0 */
  98. fpu_vlvgf(0, crc, 3); /* Load CRC into rightmost word */
  99. /* Load a 64-byte data chunk and XOR with CRC */
  100. fpu_vlm(1, 4, buf);
  101. fpu_vperm(1, 1, 1, CONST_PERM_LE2BE);
  102. fpu_vperm(2, 2, 2, CONST_PERM_LE2BE);
  103. fpu_vperm(3, 3, 3, CONST_PERM_LE2BE);
  104. fpu_vperm(4, 4, 4, CONST_PERM_LE2BE);
  105. fpu_vx(1, 0, 1); /* V1 ^= CRC */
  106. buf += 64;
  107. size -= 64;
  108. while (size >= 64) {
  109. fpu_vlm(5, 8, buf);
  110. fpu_vperm(5, 5, 5, CONST_PERM_LE2BE);
  111. fpu_vperm(6, 6, 6, CONST_PERM_LE2BE);
  112. fpu_vperm(7, 7, 7, CONST_PERM_LE2BE);
  113. fpu_vperm(8, 8, 8, CONST_PERM_LE2BE);
  114. /*
  115. * Perform a GF(2) multiplication of the doublewords in V1 with
  116. * the R1 and R2 reduction constants in V0. The intermediate
  117. * result is then folded (accumulated) with the next data chunk
  118. * in V5 and stored in V1. Repeat this step for the register
  119. * contents in V2, V3, and V4 respectively.
  120. */
  121. fpu_vgfmag(1, CONST_R2R1, 1, 5);
  122. fpu_vgfmag(2, CONST_R2R1, 2, 6);
  123. fpu_vgfmag(3, CONST_R2R1, 3, 7);
  124. fpu_vgfmag(4, CONST_R2R1, 4, 8);
  125. buf += 64;
  126. size -= 64;
  127. }
  128. /*
  129. * Fold V1 to V4 into a single 128-bit value in V1. Multiply V1 with R3
  130. * and R4 and accumulating the next 128-bit chunk until a single 128-bit
  131. * value remains.
  132. */
  133. fpu_vgfmag(1, CONST_R4R3, 1, 2);
  134. fpu_vgfmag(1, CONST_R4R3, 1, 3);
  135. fpu_vgfmag(1, CONST_R4R3, 1, 4);
  136. while (size >= 16) {
  137. fpu_vl(2, buf);
  138. fpu_vperm(2, 2, 2, CONST_PERM_LE2BE);
  139. fpu_vgfmag(1, CONST_R4R3, 1, 2);
  140. buf += 16;
  141. size -= 16;
  142. }
  143. /*
  144. * Set up a vector register for byte shifts. The shift value must
  145. * be loaded in bits 1-4 in byte element 7 of a vector register.
  146. * Shift by 8 bytes: 0x40
  147. * Shift by 4 bytes: 0x20
  148. */
  149. fpu_vleib(9, 0x40, 7);
  150. /*
  151. * Prepare V0 for the next GF(2) multiplication: shift V0 by 8 bytes
  152. * to move R4 into the rightmost doubleword and set the leftmost
  153. * doubleword to 0x1.
  154. */
  155. fpu_vsrlb(0, CONST_R4R3, 9);
  156. fpu_vleig(0, 1, 0);
  157. /*
  158. * Compute GF(2) product of V1 and V0. The rightmost doubleword
  159. * of V1 is multiplied with R4. The leftmost doubleword of V1 is
  160. * multiplied by 0x1 and is then XORed with rightmost product.
  161. * Implicitly, the intermediate leftmost product becomes padded
  162. */
  163. fpu_vgfmg(1, 0, 1);
  164. /*
  165. * Now do the final 32-bit fold by multiplying the rightmost word
  166. * in V1 with R5 and XOR the result with the remaining bits in V1.
  167. *
  168. * To achieve this by a single VGFMAG, right shift V1 by a word
  169. * and store the result in V2 which is then accumulated. Use the
  170. * vector unpack instruction to load the rightmost half of the
  171. * doubleword into the rightmost doubleword element of V1; the other
  172. * half is loaded in the leftmost doubleword.
  173. * The vector register with CONST_R5 contains the R5 constant in the
  174. * rightmost doubleword and the leftmost doubleword is zero to ignore
  175. * the leftmost product of V1.
  176. */
  177. fpu_vleib(9, 0x20, 7); /* Shift by words */
  178. fpu_vsrlb(2, 1, 9); /* Store remaining bits in V2 */
  179. fpu_vupllf(1, 1); /* Split rightmost doubleword */
  180. fpu_vgfmag(1, CONST_R5, 1, 2); /* V1 = (V1 * R5) XOR V2 */
  181. /*
  182. * Apply a Barret reduction to compute the final 32-bit CRC value.
  183. *
  184. * The input values to the Barret reduction are the degree-63 polynomial
  185. * in V1 (R(x)), degree-32 generator polynomial, and the reduction
  186. * constant u. The Barret reduction result is the CRC value of R(x) mod
  187. * P(x).
  188. *
  189. * The Barret reduction algorithm is defined as:
  190. *
  191. * 1. T1(x) = floor( R(x) / x^32 ) GF2MUL u
  192. * 2. T2(x) = floor( T1(x) / x^32 ) GF2MUL P(x)
  193. * 3. C(x) = R(x) XOR T2(x) mod x^32
  194. *
  195. * Note: The leftmost doubleword of vector register containing
  196. * CONST_RU_POLY is zero and, thus, the intermediate GF(2) product
  197. * is zero and does not contribute to the final result.
  198. */
  199. /* T1(x) = floor( R(x) / x^32 ) GF2MUL u */
  200. fpu_vupllf(2, 1);
  201. fpu_vgfmg(2, CONST_RU_POLY, 2);
  202. /*
  203. * Compute the GF(2) product of the CRC polynomial with T1(x) in
  204. * V2 and XOR the intermediate result, T2(x), with the value in V1.
  205. * The final result is stored in word element 2 of V2.
  206. */
  207. fpu_vupllf(2, 2);
  208. fpu_vgfmag(2, CONST_CRC_POLY, 2, 1);
  209. return fpu_vlgvf(2, 2);
  210. }
  211. u32 crc32_le_vgfm_16(u32 crc, unsigned char const *buf, size_t size)
  212. {
  213. return crc32_le_vgfm_generic(crc, buf, size, &constants_CRC_32_LE[0]);
  214. }
  215. u32 crc32c_le_vgfm_16(u32 crc, unsigned char const *buf, size_t size)
  216. {
  217. return crc32_le_vgfm_generic(crc, buf, size, &constants_CRC_32C_LE[0]);
  218. }