crc32c-intel_glue.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Using hardware provided CRC32 instruction to accelerate the CRC32 disposal.
  3. * CRC32C polynomial:0x1EDC6F41(BE)/0x82F63B78(LE)
  4. * CRC32 is a new instruction in Intel SSE4.2, the reference can be found at:
  5. * http://www.intel.com/products/processor/manuals/
  6. * Intel(R) 64 and IA-32 Architectures Software Developer's Manual
  7. * Volume 2A: Instruction Set Reference, A-M
  8. *
  9. * Copyright (C) 2008 Intel Corporation
  10. * Authors: Austin Zhang <austin_zhang@linux.intel.com>
  11. * Kent Liu <kent.liu@intel.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms and conditions of the GNU General Public License,
  15. * version 2, as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  20. * more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along with
  23. * this program; if not, write to the Free Software Foundation, Inc.,
  24. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  25. *
  26. */
  27. #include <linux/init.h>
  28. #include <linux/module.h>
  29. #include <linux/string.h>
  30. #include <linux/kernel.h>
  31. #include <crypto/internal/hash.h>
  32. #include <asm/cpufeature.h>
  33. #include <asm/cpu_device_id.h>
  34. #include <asm/i387.h>
  35. #include <asm/fpu-internal.h>
  36. #define CHKSUM_BLOCK_SIZE 1
  37. #define CHKSUM_DIGEST_SIZE 4
  38. #define SCALE_F sizeof(unsigned long)
  39. #ifdef CONFIG_X86_64
  40. #define REX_PRE "0x48, "
  41. #else
  42. #define REX_PRE
  43. #endif
  44. #ifdef CONFIG_X86_64
  45. /*
  46. * use carryless multiply version of crc32c when buffer
  47. * size is >= 512 (when eager fpu is enabled) or
  48. * >= 1024 (when eager fpu is disabled) to account
  49. * for fpu state save/restore overhead.
  50. */
  51. #define CRC32C_PCL_BREAKEVEN_EAGERFPU 512
  52. #define CRC32C_PCL_BREAKEVEN_NOEAGERFPU 1024
  53. asmlinkage unsigned int crc_pcl(const u8 *buffer, int len,
  54. unsigned int crc_init);
  55. static int crc32c_pcl_breakeven = CRC32C_PCL_BREAKEVEN_EAGERFPU;
  56. #if defined(X86_FEATURE_EAGER_FPU)
  57. #define set_pcl_breakeven_point() \
  58. do { \
  59. if (!use_eager_fpu()) \
  60. crc32c_pcl_breakeven = CRC32C_PCL_BREAKEVEN_NOEAGERFPU; \
  61. } while (0)
  62. #else
  63. #define set_pcl_breakeven_point() \
  64. (crc32c_pcl_breakeven = CRC32C_PCL_BREAKEVEN_NOEAGERFPU)
  65. #endif
  66. #endif /* CONFIG_X86_64 */
  67. static u32 crc32c_intel_le_hw_byte(u32 crc, unsigned char const *data, size_t length)
  68. {
  69. while (length--) {
  70. __asm__ __volatile__(
  71. ".byte 0xf2, 0xf, 0x38, 0xf0, 0xf1"
  72. :"=S"(crc)
  73. :"0"(crc), "c"(*data)
  74. );
  75. data++;
  76. }
  77. return crc;
  78. }
  79. static u32 __pure crc32c_intel_le_hw(u32 crc, unsigned char const *p, size_t len)
  80. {
  81. unsigned int iquotient = len / SCALE_F;
  82. unsigned int iremainder = len % SCALE_F;
  83. unsigned long *ptmp = (unsigned long *)p;
  84. while (iquotient--) {
  85. __asm__ __volatile__(
  86. ".byte 0xf2, " REX_PRE "0xf, 0x38, 0xf1, 0xf1;"
  87. :"=S"(crc)
  88. :"0"(crc), "c"(*ptmp)
  89. );
  90. ptmp++;
  91. }
  92. if (iremainder)
  93. crc = crc32c_intel_le_hw_byte(crc, (unsigned char *)ptmp,
  94. iremainder);
  95. return crc;
  96. }
  97. /*
  98. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  99. * If your algorithm starts with ~0, then XOR with ~0 before you set
  100. * the seed.
  101. */
  102. static int crc32c_intel_setkey(struct crypto_shash *hash, const u8 *key,
  103. unsigned int keylen)
  104. {
  105. u32 *mctx = crypto_shash_ctx(hash);
  106. if (keylen != sizeof(u32)) {
  107. crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
  108. return -EINVAL;
  109. }
  110. *mctx = le32_to_cpup((__le32 *)key);
  111. return 0;
  112. }
  113. static int crc32c_intel_init(struct shash_desc *desc)
  114. {
  115. u32 *mctx = crypto_shash_ctx(desc->tfm);
  116. u32 *crcp = shash_desc_ctx(desc);
  117. *crcp = *mctx;
  118. return 0;
  119. }
  120. static int crc32c_intel_update(struct shash_desc *desc, const u8 *data,
  121. unsigned int len)
  122. {
  123. u32 *crcp = shash_desc_ctx(desc);
  124. *crcp = crc32c_intel_le_hw(*crcp, data, len);
  125. return 0;
  126. }
  127. static int __crc32c_intel_finup(u32 *crcp, const u8 *data, unsigned int len,
  128. u8 *out)
  129. {
  130. *(__le32 *)out = ~cpu_to_le32(crc32c_intel_le_hw(*crcp, data, len));
  131. return 0;
  132. }
  133. static int crc32c_intel_finup(struct shash_desc *desc, const u8 *data,
  134. unsigned int len, u8 *out)
  135. {
  136. return __crc32c_intel_finup(shash_desc_ctx(desc), data, len, out);
  137. }
  138. static int crc32c_intel_final(struct shash_desc *desc, u8 *out)
  139. {
  140. u32 *crcp = shash_desc_ctx(desc);
  141. *(__le32 *)out = ~cpu_to_le32p(crcp);
  142. return 0;
  143. }
  144. static int crc32c_intel_digest(struct shash_desc *desc, const u8 *data,
  145. unsigned int len, u8 *out)
  146. {
  147. return __crc32c_intel_finup(crypto_shash_ctx(desc->tfm), data, len,
  148. out);
  149. }
  150. static int crc32c_intel_cra_init(struct crypto_tfm *tfm)
  151. {
  152. u32 *key = crypto_tfm_ctx(tfm);
  153. *key = ~0;
  154. return 0;
  155. }
  156. #ifdef CONFIG_X86_64
  157. static int crc32c_pcl_intel_update(struct shash_desc *desc, const u8 *data,
  158. unsigned int len)
  159. {
  160. u32 *crcp = shash_desc_ctx(desc);
  161. /*
  162. * use faster PCL version if datasize is large enough to
  163. * overcome kernel fpu state save/restore overhead
  164. */
  165. if (len >= crc32c_pcl_breakeven && irq_fpu_usable()) {
  166. kernel_fpu_begin();
  167. *crcp = crc_pcl(data, len, *crcp);
  168. kernel_fpu_end();
  169. } else
  170. *crcp = crc32c_intel_le_hw(*crcp, data, len);
  171. return 0;
  172. }
  173. static int __crc32c_pcl_intel_finup(u32 *crcp, const u8 *data, unsigned int len,
  174. u8 *out)
  175. {
  176. if (len >= crc32c_pcl_breakeven && irq_fpu_usable()) {
  177. kernel_fpu_begin();
  178. *(__le32 *)out = ~cpu_to_le32(crc_pcl(data, len, *crcp));
  179. kernel_fpu_end();
  180. } else
  181. *(__le32 *)out =
  182. ~cpu_to_le32(crc32c_intel_le_hw(*crcp, data, len));
  183. return 0;
  184. }
  185. static int crc32c_pcl_intel_finup(struct shash_desc *desc, const u8 *data,
  186. unsigned int len, u8 *out)
  187. {
  188. return __crc32c_pcl_intel_finup(shash_desc_ctx(desc), data, len, out);
  189. }
  190. static int crc32c_pcl_intel_digest(struct shash_desc *desc, const u8 *data,
  191. unsigned int len, u8 *out)
  192. {
  193. return __crc32c_pcl_intel_finup(crypto_shash_ctx(desc->tfm), data, len,
  194. out);
  195. }
  196. #endif /* CONFIG_X86_64 */
  197. static struct shash_alg alg = {
  198. .setkey = crc32c_intel_setkey,
  199. .init = crc32c_intel_init,
  200. .update = crc32c_intel_update,
  201. .final = crc32c_intel_final,
  202. .finup = crc32c_intel_finup,
  203. .digest = crc32c_intel_digest,
  204. .descsize = sizeof(u32),
  205. .digestsize = CHKSUM_DIGEST_SIZE,
  206. .base = {
  207. .cra_name = "crc32c",
  208. .cra_driver_name = "crc32c-intel",
  209. .cra_priority = 200,
  210. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  211. .cra_ctxsize = sizeof(u32),
  212. .cra_module = THIS_MODULE,
  213. .cra_init = crc32c_intel_cra_init,
  214. }
  215. };
  216. static const struct x86_cpu_id crc32c_cpu_id[] = {
  217. X86_FEATURE_MATCH(X86_FEATURE_XMM4_2),
  218. {}
  219. };
  220. MODULE_DEVICE_TABLE(x86cpu, crc32c_cpu_id);
  221. static int __init crc32c_intel_mod_init(void)
  222. {
  223. if (!x86_match_cpu(crc32c_cpu_id))
  224. return -ENODEV;
  225. #ifdef CONFIG_X86_64
  226. if (cpu_has_pclmulqdq) {
  227. alg.update = crc32c_pcl_intel_update;
  228. alg.finup = crc32c_pcl_intel_finup;
  229. alg.digest = crc32c_pcl_intel_digest;
  230. set_pcl_breakeven_point();
  231. }
  232. #endif
  233. return crypto_register_shash(&alg);
  234. }
  235. static void __exit crc32c_intel_mod_fini(void)
  236. {
  237. crypto_unregister_shash(&alg);
  238. }
  239. module_init(crc32c_intel_mod_init);
  240. module_exit(crc32c_intel_mod_fini);
  241. MODULE_AUTHOR("Austin Zhang <austin.zhang@intel.com>, Kent Liu <kent.liu@intel.com>");
  242. MODULE_DESCRIPTION("CRC32c (Castagnoli) optimization using Intel Hardware.");
  243. MODULE_LICENSE("GPL");
  244. MODULE_ALIAS("crc32c");
  245. MODULE_ALIAS("crc32c-intel");