crc32c-intel_glue.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/cpufeatures.h>
  33. #include <asm/cpu_device_id.h>
  34. #include <asm/fpu/internal.h>
  35. #define CHKSUM_BLOCK_SIZE 1
  36. #define CHKSUM_DIGEST_SIZE 4
  37. #define SCALE_F sizeof(unsigned long)
  38. #ifdef CONFIG_X86_64
  39. #define REX_PRE "0x48, "
  40. #else
  41. #define REX_PRE
  42. #endif
  43. #ifdef CONFIG_X86_64
  44. /*
  45. * use carryless multiply version of crc32c when buffer
  46. * size is >= 512 to account
  47. * for fpu state save/restore overhead.
  48. */
  49. #define CRC32C_PCL_BREAKEVEN 512
  50. asmlinkage unsigned int crc_pcl(const u8 *buffer, int len,
  51. unsigned int crc_init);
  52. #endif /* CONFIG_X86_64 */
  53. static u32 crc32c_intel_le_hw_byte(u32 crc, unsigned char const *data, size_t length)
  54. {
  55. while (length--) {
  56. __asm__ __volatile__(
  57. ".byte 0xf2, 0xf, 0x38, 0xf0, 0xf1"
  58. :"=S"(crc)
  59. :"0"(crc), "c"(*data)
  60. );
  61. data++;
  62. }
  63. return crc;
  64. }
  65. static u32 __pure crc32c_intel_le_hw(u32 crc, unsigned char const *p, size_t len)
  66. {
  67. unsigned int iquotient = len / SCALE_F;
  68. unsigned int iremainder = len % SCALE_F;
  69. unsigned long *ptmp = (unsigned long *)p;
  70. while (iquotient--) {
  71. __asm__ __volatile__(
  72. ".byte 0xf2, " REX_PRE "0xf, 0x38, 0xf1, 0xf1;"
  73. :"=S"(crc)
  74. :"0"(crc), "c"(*ptmp)
  75. );
  76. ptmp++;
  77. }
  78. if (iremainder)
  79. crc = crc32c_intel_le_hw_byte(crc, (unsigned char *)ptmp,
  80. iremainder);
  81. return crc;
  82. }
  83. /*
  84. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  85. * If your algorithm starts with ~0, then XOR with ~0 before you set
  86. * the seed.
  87. */
  88. static int crc32c_intel_setkey(struct crypto_shash *hash, const u8 *key,
  89. unsigned int keylen)
  90. {
  91. u32 *mctx = crypto_shash_ctx(hash);
  92. if (keylen != sizeof(u32)) {
  93. crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
  94. return -EINVAL;
  95. }
  96. *mctx = le32_to_cpup((__le32 *)key);
  97. return 0;
  98. }
  99. static int crc32c_intel_init(struct shash_desc *desc)
  100. {
  101. u32 *mctx = crypto_shash_ctx(desc->tfm);
  102. u32 *crcp = shash_desc_ctx(desc);
  103. *crcp = *mctx;
  104. return 0;
  105. }
  106. static int crc32c_intel_update(struct shash_desc *desc, const u8 *data,
  107. unsigned int len)
  108. {
  109. u32 *crcp = shash_desc_ctx(desc);
  110. *crcp = crc32c_intel_le_hw(*crcp, data, len);
  111. return 0;
  112. }
  113. static int __crc32c_intel_finup(u32 *crcp, const u8 *data, unsigned int len,
  114. u8 *out)
  115. {
  116. *(__le32 *)out = ~cpu_to_le32(crc32c_intel_le_hw(*crcp, data, len));
  117. return 0;
  118. }
  119. static int crc32c_intel_finup(struct shash_desc *desc, const u8 *data,
  120. unsigned int len, u8 *out)
  121. {
  122. return __crc32c_intel_finup(shash_desc_ctx(desc), data, len, out);
  123. }
  124. static int crc32c_intel_final(struct shash_desc *desc, u8 *out)
  125. {
  126. u32 *crcp = shash_desc_ctx(desc);
  127. *(__le32 *)out = ~cpu_to_le32p(crcp);
  128. return 0;
  129. }
  130. static int crc32c_intel_digest(struct shash_desc *desc, const u8 *data,
  131. unsigned int len, u8 *out)
  132. {
  133. return __crc32c_intel_finup(crypto_shash_ctx(desc->tfm), data, len,
  134. out);
  135. }
  136. static int crc32c_intel_cra_init(struct crypto_tfm *tfm)
  137. {
  138. u32 *key = crypto_tfm_ctx(tfm);
  139. *key = ~0;
  140. return 0;
  141. }
  142. #ifdef CONFIG_X86_64
  143. static int crc32c_pcl_intel_update(struct shash_desc *desc, const u8 *data,
  144. unsigned int len)
  145. {
  146. u32 *crcp = shash_desc_ctx(desc);
  147. /*
  148. * use faster PCL version if datasize is large enough to
  149. * overcome kernel fpu state save/restore overhead
  150. */
  151. if (len >= CRC32C_PCL_BREAKEVEN && irq_fpu_usable()) {
  152. kernel_fpu_begin();
  153. *crcp = crc_pcl(data, len, *crcp);
  154. kernel_fpu_end();
  155. } else
  156. *crcp = crc32c_intel_le_hw(*crcp, data, len);
  157. return 0;
  158. }
  159. static int __crc32c_pcl_intel_finup(u32 *crcp, const u8 *data, unsigned int len,
  160. u8 *out)
  161. {
  162. if (len >= CRC32C_PCL_BREAKEVEN && irq_fpu_usable()) {
  163. kernel_fpu_begin();
  164. *(__le32 *)out = ~cpu_to_le32(crc_pcl(data, len, *crcp));
  165. kernel_fpu_end();
  166. } else
  167. *(__le32 *)out =
  168. ~cpu_to_le32(crc32c_intel_le_hw(*crcp, data, len));
  169. return 0;
  170. }
  171. static int crc32c_pcl_intel_finup(struct shash_desc *desc, const u8 *data,
  172. unsigned int len, u8 *out)
  173. {
  174. return __crc32c_pcl_intel_finup(shash_desc_ctx(desc), data, len, out);
  175. }
  176. static int crc32c_pcl_intel_digest(struct shash_desc *desc, const u8 *data,
  177. unsigned int len, u8 *out)
  178. {
  179. return __crc32c_pcl_intel_finup(crypto_shash_ctx(desc->tfm), data, len,
  180. out);
  181. }
  182. #endif /* CONFIG_X86_64 */
  183. static struct shash_alg alg = {
  184. .setkey = crc32c_intel_setkey,
  185. .init = crc32c_intel_init,
  186. .update = crc32c_intel_update,
  187. .final = crc32c_intel_final,
  188. .finup = crc32c_intel_finup,
  189. .digest = crc32c_intel_digest,
  190. .descsize = sizeof(u32),
  191. .digestsize = CHKSUM_DIGEST_SIZE,
  192. .base = {
  193. .cra_name = "crc32c",
  194. .cra_driver_name = "crc32c-intel",
  195. .cra_priority = 200,
  196. .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  197. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  198. .cra_ctxsize = sizeof(u32),
  199. .cra_module = THIS_MODULE,
  200. .cra_init = crc32c_intel_cra_init,
  201. }
  202. };
  203. static const struct x86_cpu_id crc32c_cpu_id[] = {
  204. X86_FEATURE_MATCH(X86_FEATURE_XMM4_2),
  205. {}
  206. };
  207. MODULE_DEVICE_TABLE(x86cpu, crc32c_cpu_id);
  208. static int __init crc32c_intel_mod_init(void)
  209. {
  210. if (!x86_match_cpu(crc32c_cpu_id))
  211. return -ENODEV;
  212. #ifdef CONFIG_X86_64
  213. if (boot_cpu_has(X86_FEATURE_PCLMULQDQ)) {
  214. alg.update = crc32c_pcl_intel_update;
  215. alg.finup = crc32c_pcl_intel_finup;
  216. alg.digest = crc32c_pcl_intel_digest;
  217. }
  218. #endif
  219. return crypto_register_shash(&alg);
  220. }
  221. static void __exit crc32c_intel_mod_fini(void)
  222. {
  223. crypto_unregister_shash(&alg);
  224. }
  225. module_init(crc32c_intel_mod_init);
  226. module_exit(crc32c_intel_mod_fini);
  227. MODULE_AUTHOR("Austin Zhang <austin.zhang@intel.com>, Kent Liu <kent.liu@intel.com>");
  228. MODULE_DESCRIPTION("CRC32c (Castagnoli) optimization using Intel Hardware.");
  229. MODULE_LICENSE("GPL");
  230. MODULE_ALIAS_CRYPTO("crc32c");
  231. MODULE_ALIAS_CRYPTO("crc32c-intel");