crc32.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Accelerated CRC32 implementation with Zbc extension.
  4. *
  5. * Copyright (C) 2024 Intel Corporation
  6. */
  7. #include <asm/hwcap.h>
  8. #include <asm/alternative-macros.h>
  9. #include <asm/byteorder.h>
  10. #include <linux/types.h>
  11. #include <linux/minmax.h>
  12. #include <linux/crc32poly.h>
  13. #include <linux/crc32.h>
  14. #include <linux/byteorder/generic.h>
  15. /*
  16. * Refer to https://www.corsix.org/content/barrett-reduction-polynomials for
  17. * better understanding of how this math works.
  18. *
  19. * let "+" denotes polynomial add (XOR)
  20. * let "-" denotes polynomial sub (XOR)
  21. * let "*" denotes polynomial multiplication
  22. * let "/" denotes polynomial floor division
  23. * let "S" denotes source data, XLEN bit wide
  24. * let "P" denotes CRC32 polynomial
  25. * let "T" denotes 2^(XLEN+32)
  26. * let "QT" denotes quotient of T/P, with the bit for 2^XLEN being implicit
  27. *
  28. * crc32(S, P)
  29. * => S * (2^32) - S * (2^32) / P * P
  30. * => lowest 32 bits of: S * (2^32) / P * P
  31. * => lowest 32 bits of: S * (2^32) * (T / P) / T * P
  32. * => lowest 32 bits of: S * (2^32) * quotient / T * P
  33. * => lowest 32 bits of: S * quotient / 2^XLEN * P
  34. * => lowest 32 bits of: (clmul_high_part(S, QT) + S) * P
  35. * => clmul_low_part(clmul_high_part(S, QT) + S, P)
  36. *
  37. * In terms of below implementations, the BE case is more intuitive, since the
  38. * higher order bit sits at more significant position.
  39. */
  40. #if __riscv_xlen == 64
  41. /* Slide by XLEN bits per iteration */
  42. # define STEP_ORDER 3
  43. /* Each below polynomial quotient has an implicit bit for 2^XLEN */
  44. /* Polynomial quotient of (2^(XLEN+32))/CRC32_POLY, in LE format */
  45. # define CRC32_POLY_QT_LE 0x5a72d812fb808b20
  46. /* Polynomial quotient of (2^(XLEN+32))/CRC32C_POLY, in LE format */
  47. # define CRC32C_POLY_QT_LE 0xa434f61c6f5389f8
  48. /* Polynomial quotient of (2^(XLEN+32))/CRC32_POLY, in BE format, it should be
  49. * the same as the bit-reversed version of CRC32_POLY_QT_LE
  50. */
  51. # define CRC32_POLY_QT_BE 0x04d101df481b4e5a
  52. static inline u64 crc32_le_prep(u32 crc, unsigned long const *ptr)
  53. {
  54. return (u64)crc ^ (__force u64)__cpu_to_le64(*ptr);
  55. }
  56. static inline u32 crc32_le_zbc(unsigned long s, u32 poly, unsigned long poly_qt)
  57. {
  58. u32 crc;
  59. /* We don't have a "clmulrh" insn, so use clmul + slli instead. */
  60. asm volatile (".option push\n"
  61. ".option arch,+zbc\n"
  62. "clmul %0, %1, %2\n"
  63. "slli %0, %0, 1\n"
  64. "xor %0, %0, %1\n"
  65. "clmulr %0, %0, %3\n"
  66. "srli %0, %0, 32\n"
  67. ".option pop\n"
  68. : "=&r" (crc)
  69. : "r" (s),
  70. "r" (poly_qt),
  71. "r" ((u64)poly << 32)
  72. :);
  73. return crc;
  74. }
  75. static inline u64 crc32_be_prep(u32 crc, unsigned long const *ptr)
  76. {
  77. return ((u64)crc << 32) ^ (__force u64)__cpu_to_be64(*ptr);
  78. }
  79. #elif __riscv_xlen == 32
  80. # define STEP_ORDER 2
  81. /* Each quotient should match the upper half of its analog in RV64 */
  82. # define CRC32_POLY_QT_LE 0xfb808b20
  83. # define CRC32C_POLY_QT_LE 0x6f5389f8
  84. # define CRC32_POLY_QT_BE 0x04d101df
  85. static inline u32 crc32_le_prep(u32 crc, unsigned long const *ptr)
  86. {
  87. return crc ^ (__force u32)__cpu_to_le32(*ptr);
  88. }
  89. static inline u32 crc32_le_zbc(unsigned long s, u32 poly, unsigned long poly_qt)
  90. {
  91. u32 crc;
  92. /* We don't have a "clmulrh" insn, so use clmul + slli instead. */
  93. asm volatile (".option push\n"
  94. ".option arch,+zbc\n"
  95. "clmul %0, %1, %2\n"
  96. "slli %0, %0, 1\n"
  97. "xor %0, %0, %1\n"
  98. "clmulr %0, %0, %3\n"
  99. ".option pop\n"
  100. : "=&r" (crc)
  101. : "r" (s),
  102. "r" (poly_qt),
  103. "r" (poly)
  104. :);
  105. return crc;
  106. }
  107. static inline u32 crc32_be_prep(u32 crc, unsigned long const *ptr)
  108. {
  109. return crc ^ (__force u32)__cpu_to_be32(*ptr);
  110. }
  111. #else
  112. # error "Unexpected __riscv_xlen"
  113. #endif
  114. static inline u32 crc32_be_zbc(unsigned long s)
  115. {
  116. u32 crc;
  117. asm volatile (".option push\n"
  118. ".option arch,+zbc\n"
  119. "clmulh %0, %1, %2\n"
  120. "xor %0, %0, %1\n"
  121. "clmul %0, %0, %3\n"
  122. ".option pop\n"
  123. : "=&r" (crc)
  124. : "r" (s),
  125. "r" (CRC32_POLY_QT_BE),
  126. "r" (CRC32_POLY_BE)
  127. :);
  128. return crc;
  129. }
  130. #define STEP (1 << STEP_ORDER)
  131. #define OFFSET_MASK (STEP - 1)
  132. typedef u32 (*fallback)(u32 crc, unsigned char const *p, size_t len);
  133. static inline u32 crc32_le_unaligned(u32 crc, unsigned char const *p,
  134. size_t len, u32 poly,
  135. unsigned long poly_qt)
  136. {
  137. size_t bits = len * 8;
  138. unsigned long s = 0;
  139. u32 crc_low = 0;
  140. for (int i = 0; i < len; i++)
  141. s = ((unsigned long)*p++ << (__riscv_xlen - 8)) | (s >> 8);
  142. s ^= (unsigned long)crc << (__riscv_xlen - bits);
  143. if (__riscv_xlen == 32 || len < sizeof(u32))
  144. crc_low = crc >> bits;
  145. crc = crc32_le_zbc(s, poly, poly_qt);
  146. crc ^= crc_low;
  147. return crc;
  148. }
  149. static inline u32 __pure crc32_le_generic(u32 crc, unsigned char const *p,
  150. size_t len, u32 poly,
  151. unsigned long poly_qt,
  152. fallback crc_fb)
  153. {
  154. size_t offset, head_len, tail_len;
  155. unsigned long const *p_ul;
  156. unsigned long s;
  157. asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
  158. RISCV_ISA_EXT_ZBC, 1)
  159. : : : : legacy);
  160. /* Handle the unaligned head. */
  161. offset = (unsigned long)p & OFFSET_MASK;
  162. if (offset && len) {
  163. head_len = min(STEP - offset, len);
  164. crc = crc32_le_unaligned(crc, p, head_len, poly, poly_qt);
  165. p += head_len;
  166. len -= head_len;
  167. }
  168. tail_len = len & OFFSET_MASK;
  169. len = len >> STEP_ORDER;
  170. p_ul = (unsigned long const *)p;
  171. for (int i = 0; i < len; i++) {
  172. s = crc32_le_prep(crc, p_ul);
  173. crc = crc32_le_zbc(s, poly, poly_qt);
  174. p_ul++;
  175. }
  176. /* Handle the tail bytes. */
  177. p = (unsigned char const *)p_ul;
  178. if (tail_len)
  179. crc = crc32_le_unaligned(crc, p, tail_len, poly, poly_qt);
  180. return crc;
  181. legacy:
  182. return crc_fb(crc, p, len);
  183. }
  184. u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
  185. {
  186. return crc32_le_generic(crc, p, len, CRC32_POLY_LE, CRC32_POLY_QT_LE,
  187. crc32_le_base);
  188. }
  189. u32 __pure __crc32c_le(u32 crc, unsigned char const *p, size_t len)
  190. {
  191. return crc32_le_generic(crc, p, len, CRC32C_POLY_LE,
  192. CRC32C_POLY_QT_LE, __crc32c_le_base);
  193. }
  194. static inline u32 crc32_be_unaligned(u32 crc, unsigned char const *p,
  195. size_t len)
  196. {
  197. size_t bits = len * 8;
  198. unsigned long s = 0;
  199. u32 crc_low = 0;
  200. s = 0;
  201. for (int i = 0; i < len; i++)
  202. s = *p++ | (s << 8);
  203. if (__riscv_xlen == 32 || len < sizeof(u32)) {
  204. s ^= crc >> (32 - bits);
  205. crc_low = crc << bits;
  206. } else {
  207. s ^= (unsigned long)crc << (bits - 32);
  208. }
  209. crc = crc32_be_zbc(s);
  210. crc ^= crc_low;
  211. return crc;
  212. }
  213. u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
  214. {
  215. size_t offset, head_len, tail_len;
  216. unsigned long const *p_ul;
  217. unsigned long s;
  218. asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
  219. RISCV_ISA_EXT_ZBC, 1)
  220. : : : : legacy);
  221. /* Handle the unaligned head. */
  222. offset = (unsigned long)p & OFFSET_MASK;
  223. if (offset && len) {
  224. head_len = min(STEP - offset, len);
  225. crc = crc32_be_unaligned(crc, p, head_len);
  226. p += head_len;
  227. len -= head_len;
  228. }
  229. tail_len = len & OFFSET_MASK;
  230. len = len >> STEP_ORDER;
  231. p_ul = (unsigned long const *)p;
  232. for (int i = 0; i < len; i++) {
  233. s = crc32_be_prep(crc, p_ul);
  234. crc = crc32_be_zbc(s);
  235. p_ul++;
  236. }
  237. /* Handle the tail bytes. */
  238. p = (unsigned char const *)p_ul;
  239. if (tail_len)
  240. crc = crc32_be_unaligned(crc, p, tail_len);
  241. return crc;
  242. legacy:
  243. return crc32_be_base(crc, p, len);
  244. }