diff options
Diffstat (limited to 'fs/btrfs/crc32c.h')
-rw-r--r-- | fs/btrfs/crc32c.h | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/fs/btrfs/crc32c.h b/fs/btrfs/crc32c.h new file mode 100644 index 000000000000..1eaf11d334fd --- /dev/null +++ b/fs/btrfs/crc32c.h | |||
@@ -0,0 +1,120 @@ | |||
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 | |||
19 | #ifndef __BTRFS_CRC32C__ | ||
20 | #define __BTRFS_CRC32C__ | ||
21 | #include <asm/byteorder.h> | ||
22 | #include <linux/crc32c.h> | ||
23 | #include <linux/version.h> | ||
24 | |||
25 | /* #define CONFIG_BTRFS_HW_SUM 1 */ | ||
26 | |||
27 | #ifdef CONFIG_BTRFS_HW_SUM | ||
28 | #ifdef CONFIG_X86 | ||
29 | /* | ||
30 | * Using hardware provided CRC32 instruction to accelerate the CRC32 disposal. | ||
31 | * CRC32C polynomial:0x1EDC6F41(BE)/0x82F63B78(LE) | ||
32 | * CRC32 is a new instruction in Intel SSE4.2, the reference can be found at: | ||
33 | * http://www.intel.com/products/processor/manuals/ | ||
34 | * Intel(R) 64 and IA-32 Architectures Software Developer's Manual | ||
35 | * Volume 2A: Instruction Set Reference, A-M | ||
36 | */ | ||
37 | |||
38 | #include <asm/cpufeature.h> | ||
39 | #include <asm/processor.h> | ||
40 | |||
41 | #define X86_FEATURE_XMM4_2 (4*32+20) /* Streaming SIMD Extensions-4.2 */ | ||
42 | #define cpu_has_xmm4_2 boot_cpu_has(X86_FEATURE_XMM4_2) | ||
43 | |||
44 | #ifdef CONFIG_X86_64 | ||
45 | #define REX_PRE "0x48, " | ||
46 | #define SCALE_F 8 | ||
47 | #else | ||
48 | #define REX_PRE | ||
49 | #define SCALE_F 4 | ||
50 | #endif | ||
51 | |||
52 | static inline u32 btrfs_crc32c_le_hw_byte(u32 crc, unsigned char const *data, | ||
53 | 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 | |||
64 | return crc; | ||
65 | } | ||
66 | |||
67 | static inline u32 __pure btrfs_crc32c_le_hw(u32 crc, unsigned char const *p, | ||
68 | size_t len) | ||
69 | { | ||
70 | unsigned int iquotient = len / SCALE_F; | ||
71 | unsigned int iremainder = len % SCALE_F; | ||
72 | #ifdef CONFIG_X86_64 | ||
73 | u64 *ptmp = (u64 *)p; | ||
74 | #else | ||
75 | u32 *ptmp = (u32 *)p; | ||
76 | #endif | ||
77 | |||
78 | while (iquotient--) { | ||
79 | __asm__ __volatile__( | ||
80 | ".byte 0xf2, " REX_PRE "0xf, 0x38, 0xf1, 0xf1;" | ||
81 | :"=S"(crc) | ||
82 | :"0"(crc), "c"(*ptmp) | ||
83 | ); | ||
84 | ptmp++; | ||
85 | } | ||
86 | |||
87 | if (iremainder) | ||
88 | crc = btrfs_crc32c_le_hw_byte(crc, (unsigned char *)ptmp, | ||
89 | iremainder); | ||
90 | |||
91 | return crc; | ||
92 | } | ||
93 | #endif /* CONFIG_BTRFS_HW_SUM */ | ||
94 | |||
95 | static inline u32 __btrfs_crc32c(u32 crc, unsigned char const *address, | ||
96 | size_t len) | ||
97 | { | ||
98 | #ifdef CONFIG_BTRFS_HW_SUM | ||
99 | if (cpu_has_xmm4_2) | ||
100 | return btrfs_crc32c_le_hw(crc, address, len); | ||
101 | #endif | ||
102 | return crc32c_le(crc, address, len); | ||
103 | } | ||
104 | |||
105 | #else | ||
106 | |||
107 | #define __btrfs_crc32c(seed, data, length) crc32c(seed, data, length) | ||
108 | |||
109 | #endif /* CONFIG_X86 */ | ||
110 | |||
111 | /** | ||
112 | * implementation of crc32c_le() changed in linux-2.6.23, | ||
113 | * has of v0.13 btrfs-progs is using the latest version. | ||
114 | * We must workaround older implementations of crc32c_le() | ||
115 | * found on older kernel versions. | ||
116 | */ | ||
117 | #define btrfs_crc32c(seed, data, length) \ | ||
118 | __btrfs_crc32c(seed, (unsigned char const *)data, length) | ||
119 | #endif | ||
120 | |||