crc32c.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C) 2008 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #ifndef __BTRFS_CRC32C__
  19. #define __BTRFS_CRC32C__
  20. #include <asm/byteorder.h>
  21. #include <linux/crc32c.h>
  22. #include <linux/version.h>
  23. /* #define CONFIG_BTRFS_HW_SUM 1 */
  24. #ifdef CONFIG_BTRFS_HW_SUM
  25. #ifdef CONFIG_X86
  26. /*
  27. * Using hardware provided CRC32 instruction to accelerate the CRC32 disposal.
  28. * CRC32C polynomial:0x1EDC6F41(BE)/0x82F63B78(LE)
  29. * CRC32 is a new instruction in Intel SSE4.2, the reference can be found at:
  30. * http://www.intel.com/products/processor/manuals/
  31. * Intel(R) 64 and IA-32 Architectures Software Developer's Manual
  32. * Volume 2A: Instruction Set Reference, A-M
  33. */
  34. #include <asm/cpufeature.h>
  35. #include <asm/processor.h>
  36. #define X86_FEATURE_XMM4_2 (4*32+20) /* Streaming SIMD Extensions-4.2 */
  37. #define cpu_has_xmm4_2 boot_cpu_has(X86_FEATURE_XMM4_2)
  38. #ifdef CONFIG_X86_64
  39. #define REX_PRE "0x48, "
  40. #define SCALE_F 8
  41. #else
  42. #define REX_PRE
  43. #define SCALE_F 4
  44. #endif
  45. static inline u32 btrfs_crc32c_le_hw_byte(u32 crc, unsigned char const *data,
  46. size_t length)
  47. {
  48. while (length--) {
  49. __asm__ __volatile__(
  50. ".byte 0xf2, 0xf, 0x38, 0xf0, 0xf1"
  51. :"=S"(crc)
  52. :"0"(crc), "c"(*data)
  53. );
  54. data++;
  55. }
  56. return crc;
  57. }
  58. static inline u32 __pure btrfs_crc32c_le_hw(u32 crc, unsigned char const *p,
  59. size_t len)
  60. {
  61. unsigned int iquotient = len / SCALE_F;
  62. unsigned int iremainder = len % SCALE_F;
  63. #ifdef CONFIG_X86_64
  64. u64 *ptmp = (u64 *)p;
  65. #else
  66. u32 *ptmp = (u32 *)p;
  67. #endif
  68. while (iquotient--) {
  69. __asm__ __volatile__(
  70. ".byte 0xf2, " REX_PRE "0xf, 0x38, 0xf1, 0xf1;"
  71. :"=S"(crc)
  72. :"0"(crc), "c"(*ptmp)
  73. );
  74. ptmp++;
  75. }
  76. if (iremainder)
  77. crc = btrfs_crc32c_le_hw_byte(crc, (unsigned char *)ptmp,
  78. iremainder);
  79. return crc;
  80. }
  81. #endif /* CONFIG_BTRFS_HW_SUM */
  82. static inline u32 __btrfs_crc32c(u32 crc, unsigned char const *address,
  83. size_t len)
  84. {
  85. #ifdef CONFIG_BTRFS_HW_SUM
  86. if (cpu_has_xmm4_2)
  87. return btrfs_crc32c_le_hw(crc, address, len);
  88. #endif
  89. return crc32c_le(crc, address, len);
  90. }
  91. #else
  92. #define __btrfs_crc32c(seed, data, length) crc32c(seed, data, length)
  93. #endif /* CONFIG_X86 */
  94. /**
  95. * implementation of crc32c_le() changed in linux-2.6.23,
  96. * has of v0.13 btrfs-progs is using the latest version.
  97. * We must workaround older implementations of crc32c_le()
  98. * found on older kernel versions.
  99. */
  100. #define btrfs_crc32c(seed, data, length) \
  101. __btrfs_crc32c(seed, (unsigned char const *)data, length)
  102. #endif