crc32c.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef __BTRFS_CRC32C__
  2. #define __BTRFS_CRC32C__
  3. #include <asm/byteorder.h>
  4. #include <linux/crc32c.h>
  5. #include <linux/version.h>
  6. /* #define CONFIG_BTRFS_HW_SUM 1 */
  7. #ifdef CONFIG_BTRFS_HW_SUM
  8. #ifdef CONFIG_X86
  9. /*
  10. * Using hardware provided CRC32 instruction to accelerate the CRC32 disposal.
  11. * CRC32C polynomial:0x1EDC6F41(BE)/0x82F63B78(LE)
  12. * CRC32 is a new instruction in Intel SSE4.2, the reference can be found at:
  13. * http://www.intel.com/products/processor/manuals/
  14. * Intel(R) 64 and IA-32 Architectures Software Developer's Manual
  15. * Volume 2A: Instruction Set Reference, A-M
  16. */
  17. #include <asm/cpufeature.h>
  18. #include <asm/processor.h>
  19. #define X86_FEATURE_XMM4_2 (4*32+20) /* Streaming SIMD Extensions-4.2 */
  20. #define cpu_has_xmm4_2 boot_cpu_has(X86_FEATURE_XMM4_2)
  21. #ifdef CONFIG_X86_64
  22. #define REX_PRE "0x48, "
  23. #define SCALE_F 8
  24. #else
  25. #define REX_PRE
  26. #define SCALE_F 4
  27. #endif
  28. static inline u32 btrfs_crc32c_le_hw_byte(u32 crc, unsigned char const *data,
  29. size_t length)
  30. {
  31. while (length--) {
  32. __asm__ __volatile__(
  33. ".byte 0xf2, 0xf, 0x38, 0xf0, 0xf1"
  34. :"=S"(crc)
  35. :"0"(crc), "c"(*data)
  36. );
  37. data++;
  38. }
  39. return crc;
  40. }
  41. static inline u32 __pure btrfs_crc32c_le_hw(u32 crc, unsigned char const *p,
  42. size_t len)
  43. {
  44. unsigned int iquotient = len / SCALE_F;
  45. unsigned int iremainder = len % SCALE_F;
  46. #ifdef CONFIG_X86_64
  47. u64 *ptmp = (u64 *)p;
  48. #else
  49. u32 *ptmp = (u32 *)p;
  50. #endif
  51. while (iquotient--) {
  52. __asm__ __volatile__(
  53. ".byte 0xf2, " REX_PRE "0xf, 0x38, 0xf1, 0xf1;"
  54. :"=S"(crc)
  55. :"0"(crc), "c"(*ptmp)
  56. );
  57. ptmp++;
  58. }
  59. if (iremainder)
  60. crc = btrfs_crc32c_le_hw_byte(crc, (unsigned char *)ptmp,
  61. iremainder);
  62. return crc;
  63. }
  64. #endif /* CONFIG_BTRFS_HW_SUM */
  65. static inline u32 __btrfs_crc32c(u32 crc, unsigned char const *address,
  66. size_t len)
  67. {
  68. #ifdef CONFIG_BTRFS_HW_SUM
  69. if (cpu_has_xmm4_2)
  70. return btrfs_crc32c_le_hw(crc, address, len);
  71. #endif
  72. return crc32c_le(crc, address, len);
  73. }
  74. #else
  75. #define __btrfs_crc32c(seed, data, length) crc32c(seed, data, length)
  76. #endif /* CONFIG_X86 */
  77. /**
  78. * implementation of crc32c_le() changed in linux-2.6.23,
  79. * has of v0.13 btrfs-progs is using the latest version.
  80. * We must workaround older implementations of crc32c_le()
  81. * found on older kernel versions.
  82. */
  83. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  84. #define btrfs_crc32c(seed, data, length) \
  85. __cpu_to_le32( __btrfs_crc32c( __le32_to_cpu(seed), \
  86. (unsigned char const *)data, length) )
  87. #else
  88. #define btrfs_crc32c(seed, data, length) \
  89. __btrfs_crc32c(seed, (unsigned char const *)data, length)
  90. #endif
  91. #endif