2 * Accelerated CRC32(C) using ARM CRC, NEON and Crypto Extensions instructions
4 * Copyright (C) 2016 Linaro Ltd <ard.biesheuvel@linaro.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
13 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2 only,
17 * as published by the Free Software Foundation.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License version 2 for more details (a copy is included
23 * in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU General Public License
26 * version 2 along with this program; If not, see http://www.gnu.org/licenses
28 * Please visit http://www.xyratex.com/contact if you need additional
29 * information or have any questions.
35 * Copyright 2012 Xyratex Technology Limited
37 * Using hardware provided PCLMULQDQ instruction to accelerate the CRC32
39 * CRC32 polynomial:0x04c11db7(BE)/0xEDB88320(LE)
40 * PCLMULQDQ is a new instruction in Intel SSE4.2, the reference can be found
42 * https://www.intel.com/products/processor/manuals/
43 * Intel(R) 64 and IA-32 Architectures Software Developer's Manual
44 * Volume 2B: Instruction Set Reference, N-Z
46 * Authors: Gregory Prestas <Gregory_Prestas@us.xyratex.com>
47 * Alexander Boyko <Alexander_Boyko@xyratex.com>
50 #include <linux/linkage.h>
51 #include <linux/cfi_types.h>
52 #include <asm/assembler.h>
58 .fpu crypto-neon-fp-armv8
62 * [x4*128+32 mod P(x) << 32)]' << 1 = 0x154442bd4
63 * #define CONSTANT_R1 0x154442bd4LL
65 * [(x4*128-32 mod P(x) << 32)]' << 1 = 0x1c6e41596
66 * #define CONSTANT_R2 0x1c6e41596LL
68 .quad 0x0000000154442bd4
69 .quad 0x00000001c6e41596
72 * [(x128+32 mod P(x) << 32)]' << 1 = 0x1751997d0
73 * #define CONSTANT_R3 0x1751997d0LL
75 * [(x128-32 mod P(x) << 32)]' << 1 = 0x0ccaa009e
76 * #define CONSTANT_R4 0x0ccaa009eLL
78 .quad 0x00000001751997d0
79 .quad 0x00000000ccaa009e
82 * [(x64 mod P(x) << 32)]' << 1 = 0x163cd6124
83 * #define CONSTANT_R5 0x163cd6124LL
85 .quad 0x0000000163cd6124
86 .quad 0x00000000FFFFFFFF
89 * #define CRCPOLY_TRUE_LE_FULL 0x1DB710641LL
91 * Barrett Reduction constant (u64`) = u` = (x**64 / P(x))`
93 * #define CONSTANT_RU 0x1F7011641LL
95 .quad 0x00000001DB710641
96 .quad 0x00000001F7011641
99 .quad 0x00000000740eef02
100 .quad 0x000000009e4addf8
101 .quad 0x00000000f20c0dfe
102 .quad 0x000000014cd00bd6
103 .quad 0x00000000dd45aab8
104 .quad 0x00000000FFFFFFFF
105 .quad 0x0000000105ec76f0
106 .quad 0x00000000dea713f1
121 * LEN - sizeof buffer (multiple of 16 bytes), LEN should be > 63
122 * CRC - initial crc32
124 * uint crc32_pmull_le(unsigned char const *buffer,
125 * size_t len, uint crc32)
127 SYM_FUNC_START(crc32_pmull_le)
128 adr r3, .Lcrc32_constants
130 SYM_FUNC_END(crc32_pmull_le)
132 SYM_FUNC_START(crc32c_pmull_le)
133 adr r3, .Lcrc32c_constants
136 vld1.8 {q1-q2}, [BUF, :128]!
137 vld1.8 {q3-q4}, [BUF, :128]!
139 vmov.i8 qCONSTANT, #0
140 vmov.32 dCONSTANTl[0], CRC
141 veor.8 d2, d2, dCONSTANTl
146 vld1.64 {qCONSTANT}, [r3]
148 loop_64: /* 64 bytes Full cache line folding */
151 vmull.p64 q5, d3, dCONSTANTh
152 vmull.p64 q6, d5, dCONSTANTh
153 vmull.p64 q7, d7, dCONSTANTh
154 vmull.p64 q8, d9, dCONSTANTh
156 vmull.p64 q1, d2, dCONSTANTl
157 vmull.p64 q2, d4, dCONSTANTl
158 vmull.p64 q3, d6, dCONSTANTl
159 vmull.p64 q4, d8, dCONSTANTl
162 vld1.8 {q5}, [BUF, :128]!
164 vld1.8 {q6}, [BUF, :128]!
166 vld1.8 {q7}, [BUF, :128]!
168 vld1.8 {q8}, [BUF, :128]!
178 less_64: /* Folding cache line into 128bit */
179 vldr dCONSTANTl, [r3, #16]
180 vldr dCONSTANTh, [r3, #24]
182 vmull.p64 q5, d3, dCONSTANTh
183 vmull.p64 q1, d2, dCONSTANTl
187 vmull.p64 q5, d3, dCONSTANTh
188 vmull.p64 q1, d2, dCONSTANTl
192 vmull.p64 q5, d3, dCONSTANTh
193 vmull.p64 q1, d2, dCONSTANTl
200 loop_16: /* Folding rest buffer into 128bit */
203 vld1.8 {q2}, [BUF, :128]!
204 vmull.p64 q5, d3, dCONSTANTh
205 vmull.p64 q1, d2, dCONSTANTl
212 /* perform the last 64 bit fold, also adds 32 zeroes
213 * to the input stream */
214 vmull.p64 q2, d2, dCONSTANTh
215 vext.8 q1, q1, qzr, #8
218 /* final 32-bit fold */
219 vldr dCONSTANTl, [r3, #32]
223 vext.8 q2, q1, qzr, #4
225 vmull.p64 q1, d2, dCONSTANTl
228 /* Finish up with the bit-reversed barrett reduction 64 ==> 32 bits */
229 vldr dCONSTANTl, [r3, #48]
230 vldr dCONSTANTh, [r3, #56]
233 vext.8 q2, qzr, q2, #8
234 vmull.p64 q2, d5, dCONSTANTh
236 vmull.p64 q2, d4, dCONSTANTl
241 SYM_FUNC_END(crc32c_pmull_le)
252 ldrd r2, r3, [r1], #8
255 crc32\c\()w r0, r0, r2
256 crc32\c\()w r0, r0, r3
266 crc32\c\()w r0, r0, r3
271 ARM_BE8(rev16 r3, r3 )
272 crc32\c\()h r0, r0, r3
277 crc32\c\()b r0, r0, r3
285 crc32\c\()b r0, r0, r3
291 ARM_BE8(rev16 r3, r3 )
292 crc32\c\()h r0, r0, r3
300 SYM_TYPED_FUNC_START(crc32_armv8_le)
302 SYM_FUNC_END(crc32_armv8_le)
305 SYM_TYPED_FUNC_START(crc32c_armv8_le)
307 SYM_FUNC_END(crc32c_armv8_le)