1*00807191SEric Biggers /* SPDX-License-Identifier: GPL-2.0 */
2*00807191SEric Biggers /*
3*00807191SEric Biggers * Hardware-accelerated CRC-32 variants for Linux on z Systems
4*00807191SEric Biggers *
5*00807191SEric Biggers * Use the z/Architecture Vector Extension Facility to accelerate the
6*00807191SEric Biggers * computing of bitreflected CRC-32 checksums for IEEE 802.3 Ethernet
7*00807191SEric Biggers * and Castagnoli.
8*00807191SEric Biggers *
9*00807191SEric Biggers * This CRC-32 implementation algorithm is bitreflected and processes
10*00807191SEric Biggers * the least-significant bit first (Little-Endian).
11*00807191SEric Biggers *
12*00807191SEric Biggers * Copyright IBM Corp. 2015
13*00807191SEric Biggers * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
14*00807191SEric Biggers */
15*00807191SEric Biggers
16*00807191SEric Biggers #include <linux/types.h>
17*00807191SEric Biggers #include <asm/fpu.h>
18*00807191SEric Biggers #include "crc32-vx.h"
19*00807191SEric Biggers
20*00807191SEric Biggers /* Vector register range containing CRC-32 constants */
21*00807191SEric Biggers #define CONST_PERM_LE2BE 9
22*00807191SEric Biggers #define CONST_R2R1 10
23*00807191SEric Biggers #define CONST_R4R3 11
24*00807191SEric Biggers #define CONST_R5 12
25*00807191SEric Biggers #define CONST_RU_POLY 13
26*00807191SEric Biggers #define CONST_CRC_POLY 14
27*00807191SEric Biggers
28*00807191SEric Biggers /*
29*00807191SEric Biggers * The CRC-32 constant block contains reduction constants to fold and
30*00807191SEric Biggers * process particular chunks of the input data stream in parallel.
31*00807191SEric Biggers *
32*00807191SEric Biggers * For the CRC-32 variants, the constants are precomputed according to
33*00807191SEric Biggers * these definitions:
34*00807191SEric Biggers *
35*00807191SEric Biggers * R1 = [(x4*128+32 mod P'(x) << 32)]' << 1
36*00807191SEric Biggers * R2 = [(x4*128-32 mod P'(x) << 32)]' << 1
37*00807191SEric Biggers * R3 = [(x128+32 mod P'(x) << 32)]' << 1
38*00807191SEric Biggers * R4 = [(x128-32 mod P'(x) << 32)]' << 1
39*00807191SEric Biggers * R5 = [(x64 mod P'(x) << 32)]' << 1
40*00807191SEric Biggers * R6 = [(x32 mod P'(x) << 32)]' << 1
41*00807191SEric Biggers *
42*00807191SEric Biggers * The bitreflected Barret reduction constant, u', is defined as
43*00807191SEric Biggers * the bit reversal of floor(x**64 / P(x)).
44*00807191SEric Biggers *
45*00807191SEric Biggers * where P(x) is the polynomial in the normal domain and the P'(x) is the
46*00807191SEric Biggers * polynomial in the reversed (bitreflected) domain.
47*00807191SEric Biggers *
48*00807191SEric Biggers * CRC-32 (IEEE 802.3 Ethernet, ...) polynomials:
49*00807191SEric Biggers *
50*00807191SEric Biggers * P(x) = 0x04C11DB7
51*00807191SEric Biggers * P'(x) = 0xEDB88320
52*00807191SEric Biggers *
53*00807191SEric Biggers * CRC-32C (Castagnoli) polynomials:
54*00807191SEric Biggers *
55*00807191SEric Biggers * P(x) = 0x1EDC6F41
56*00807191SEric Biggers * P'(x) = 0x82F63B78
57*00807191SEric Biggers */
58*00807191SEric Biggers
59*00807191SEric Biggers static unsigned long constants_CRC_32_LE[] = {
60*00807191SEric Biggers 0x0f0e0d0c0b0a0908, 0x0706050403020100, /* BE->LE mask */
61*00807191SEric Biggers 0x1c6e41596, 0x154442bd4, /* R2, R1 */
62*00807191SEric Biggers 0x0ccaa009e, 0x1751997d0, /* R4, R3 */
63*00807191SEric Biggers 0x0, 0x163cd6124, /* R5 */
64*00807191SEric Biggers 0x0, 0x1f7011641, /* u' */
65*00807191SEric Biggers 0x0, 0x1db710641 /* P'(x) << 1 */
66*00807191SEric Biggers };
67*00807191SEric Biggers
68*00807191SEric Biggers static unsigned long constants_CRC_32C_LE[] = {
69*00807191SEric Biggers 0x0f0e0d0c0b0a0908, 0x0706050403020100, /* BE->LE mask */
70*00807191SEric Biggers 0x09e4addf8, 0x740eef02, /* R2, R1 */
71*00807191SEric Biggers 0x14cd00bd6, 0xf20c0dfe, /* R4, R3 */
72*00807191SEric Biggers 0x0, 0x0dd45aab8, /* R5 */
73*00807191SEric Biggers 0x0, 0x0dea713f1, /* u' */
74*00807191SEric Biggers 0x0, 0x105ec76f0 /* P'(x) << 1 */
75*00807191SEric Biggers };
76*00807191SEric Biggers
77*00807191SEric Biggers /**
78*00807191SEric Biggers * crc32_le_vgfm_generic - Compute CRC-32 (LE variant) with vector registers
79*00807191SEric Biggers * @crc: Initial CRC value, typically ~0.
80*00807191SEric Biggers * @buf: Input buffer pointer, performance might be improved if the
81*00807191SEric Biggers * buffer is on a doubleword boundary.
82*00807191SEric Biggers * @size: Size of the buffer, must be 64 bytes or greater.
83*00807191SEric Biggers * @constants: CRC-32 constant pool base pointer.
84*00807191SEric Biggers *
85*00807191SEric Biggers * Register usage:
86*00807191SEric Biggers * V0: Initial CRC value and intermediate constants and results.
87*00807191SEric Biggers * V1..V4: Data for CRC computation.
88*00807191SEric Biggers * V5..V8: Next data chunks that are fetched from the input buffer.
89*00807191SEric Biggers * V9: Constant for BE->LE conversion and shift operations
90*00807191SEric Biggers * V10..V14: CRC-32 constants.
91*00807191SEric Biggers */
crc32_le_vgfm_generic(u32 crc,unsigned char const * buf,size_t size,unsigned long * constants)92*00807191SEric Biggers static u32 crc32_le_vgfm_generic(u32 crc, unsigned char const *buf, size_t size, unsigned long *constants)
93*00807191SEric Biggers {
94*00807191SEric Biggers /* Load CRC-32 constants */
95*00807191SEric Biggers fpu_vlm(CONST_PERM_LE2BE, CONST_CRC_POLY, constants);
96*00807191SEric Biggers
97*00807191SEric Biggers /*
98*00807191SEric Biggers * Load the initial CRC value.
99*00807191SEric Biggers *
100*00807191SEric Biggers * The CRC value is loaded into the rightmost word of the
101*00807191SEric Biggers * vector register and is later XORed with the LSB portion
102*00807191SEric Biggers * of the loaded input data.
103*00807191SEric Biggers */
104*00807191SEric Biggers fpu_vzero(0); /* Clear V0 */
105*00807191SEric Biggers fpu_vlvgf(0, crc, 3); /* Load CRC into rightmost word */
106*00807191SEric Biggers
107*00807191SEric Biggers /* Load a 64-byte data chunk and XOR with CRC */
108*00807191SEric Biggers fpu_vlm(1, 4, buf);
109*00807191SEric Biggers fpu_vperm(1, 1, 1, CONST_PERM_LE2BE);
110*00807191SEric Biggers fpu_vperm(2, 2, 2, CONST_PERM_LE2BE);
111*00807191SEric Biggers fpu_vperm(3, 3, 3, CONST_PERM_LE2BE);
112*00807191SEric Biggers fpu_vperm(4, 4, 4, CONST_PERM_LE2BE);
113*00807191SEric Biggers
114*00807191SEric Biggers fpu_vx(1, 0, 1); /* V1 ^= CRC */
115*00807191SEric Biggers buf += 64;
116*00807191SEric Biggers size -= 64;
117*00807191SEric Biggers
118*00807191SEric Biggers while (size >= 64) {
119*00807191SEric Biggers fpu_vlm(5, 8, buf);
120*00807191SEric Biggers fpu_vperm(5, 5, 5, CONST_PERM_LE2BE);
121*00807191SEric Biggers fpu_vperm(6, 6, 6, CONST_PERM_LE2BE);
122*00807191SEric Biggers fpu_vperm(7, 7, 7, CONST_PERM_LE2BE);
123*00807191SEric Biggers fpu_vperm(8, 8, 8, CONST_PERM_LE2BE);
124*00807191SEric Biggers /*
125*00807191SEric Biggers * Perform a GF(2) multiplication of the doublewords in V1 with
126*00807191SEric Biggers * the R1 and R2 reduction constants in V0. The intermediate
127*00807191SEric Biggers * result is then folded (accumulated) with the next data chunk
128*00807191SEric Biggers * in V5 and stored in V1. Repeat this step for the register
129*00807191SEric Biggers * contents in V2, V3, and V4 respectively.
130*00807191SEric Biggers */
131*00807191SEric Biggers fpu_vgfmag(1, CONST_R2R1, 1, 5);
132*00807191SEric Biggers fpu_vgfmag(2, CONST_R2R1, 2, 6);
133*00807191SEric Biggers fpu_vgfmag(3, CONST_R2R1, 3, 7);
134*00807191SEric Biggers fpu_vgfmag(4, CONST_R2R1, 4, 8);
135*00807191SEric Biggers buf += 64;
136*00807191SEric Biggers size -= 64;
137*00807191SEric Biggers }
138*00807191SEric Biggers
139*00807191SEric Biggers /*
140*00807191SEric Biggers * Fold V1 to V4 into a single 128-bit value in V1. Multiply V1 with R3
141*00807191SEric Biggers * and R4 and accumulating the next 128-bit chunk until a single 128-bit
142*00807191SEric Biggers * value remains.
143*00807191SEric Biggers */
144*00807191SEric Biggers fpu_vgfmag(1, CONST_R4R3, 1, 2);
145*00807191SEric Biggers fpu_vgfmag(1, CONST_R4R3, 1, 3);
146*00807191SEric Biggers fpu_vgfmag(1, CONST_R4R3, 1, 4);
147*00807191SEric Biggers
148*00807191SEric Biggers while (size >= 16) {
149*00807191SEric Biggers fpu_vl(2, buf);
150*00807191SEric Biggers fpu_vperm(2, 2, 2, CONST_PERM_LE2BE);
151*00807191SEric Biggers fpu_vgfmag(1, CONST_R4R3, 1, 2);
152*00807191SEric Biggers buf += 16;
153*00807191SEric Biggers size -= 16;
154*00807191SEric Biggers }
155*00807191SEric Biggers
156*00807191SEric Biggers /*
157*00807191SEric Biggers * Set up a vector register for byte shifts. The shift value must
158*00807191SEric Biggers * be loaded in bits 1-4 in byte element 7 of a vector register.
159*00807191SEric Biggers * Shift by 8 bytes: 0x40
160*00807191SEric Biggers * Shift by 4 bytes: 0x20
161*00807191SEric Biggers */
162*00807191SEric Biggers fpu_vleib(9, 0x40, 7);
163*00807191SEric Biggers
164*00807191SEric Biggers /*
165*00807191SEric Biggers * Prepare V0 for the next GF(2) multiplication: shift V0 by 8 bytes
166*00807191SEric Biggers * to move R4 into the rightmost doubleword and set the leftmost
167*00807191SEric Biggers * doubleword to 0x1.
168*00807191SEric Biggers */
169*00807191SEric Biggers fpu_vsrlb(0, CONST_R4R3, 9);
170*00807191SEric Biggers fpu_vleig(0, 1, 0);
171*00807191SEric Biggers
172*00807191SEric Biggers /*
173*00807191SEric Biggers * Compute GF(2) product of V1 and V0. The rightmost doubleword
174*00807191SEric Biggers * of V1 is multiplied with R4. The leftmost doubleword of V1 is
175*00807191SEric Biggers * multiplied by 0x1 and is then XORed with rightmost product.
176*00807191SEric Biggers * Implicitly, the intermediate leftmost product becomes padded
177*00807191SEric Biggers */
178*00807191SEric Biggers fpu_vgfmg(1, 0, 1);
179*00807191SEric Biggers
180*00807191SEric Biggers /*
181*00807191SEric Biggers * Now do the final 32-bit fold by multiplying the rightmost word
182*00807191SEric Biggers * in V1 with R5 and XOR the result with the remaining bits in V1.
183*00807191SEric Biggers *
184*00807191SEric Biggers * To achieve this by a single VGFMAG, right shift V1 by a word
185*00807191SEric Biggers * and store the result in V2 which is then accumulated. Use the
186*00807191SEric Biggers * vector unpack instruction to load the rightmost half of the
187*00807191SEric Biggers * doubleword into the rightmost doubleword element of V1; the other
188*00807191SEric Biggers * half is loaded in the leftmost doubleword.
189*00807191SEric Biggers * The vector register with CONST_R5 contains the R5 constant in the
190*00807191SEric Biggers * rightmost doubleword and the leftmost doubleword is zero to ignore
191*00807191SEric Biggers * the leftmost product of V1.
192*00807191SEric Biggers */
193*00807191SEric Biggers fpu_vleib(9, 0x20, 7); /* Shift by words */
194*00807191SEric Biggers fpu_vsrlb(2, 1, 9); /* Store remaining bits in V2 */
195*00807191SEric Biggers fpu_vupllf(1, 1); /* Split rightmost doubleword */
196*00807191SEric Biggers fpu_vgfmag(1, CONST_R5, 1, 2); /* V1 = (V1 * R5) XOR V2 */
197*00807191SEric Biggers
198*00807191SEric Biggers /*
199*00807191SEric Biggers * Apply a Barret reduction to compute the final 32-bit CRC value.
200*00807191SEric Biggers *
201*00807191SEric Biggers * The input values to the Barret reduction are the degree-63 polynomial
202*00807191SEric Biggers * in V1 (R(x)), degree-32 generator polynomial, and the reduction
203*00807191SEric Biggers * constant u. The Barret reduction result is the CRC value of R(x) mod
204*00807191SEric Biggers * P(x).
205*00807191SEric Biggers *
206*00807191SEric Biggers * The Barret reduction algorithm is defined as:
207*00807191SEric Biggers *
208*00807191SEric Biggers * 1. T1(x) = floor( R(x) / x^32 ) GF2MUL u
209*00807191SEric Biggers * 2. T2(x) = floor( T1(x) / x^32 ) GF2MUL P(x)
210*00807191SEric Biggers * 3. C(x) = R(x) XOR T2(x) mod x^32
211*00807191SEric Biggers *
212*00807191SEric Biggers * Note: The leftmost doubleword of vector register containing
213*00807191SEric Biggers * CONST_RU_POLY is zero and, thus, the intermediate GF(2) product
214*00807191SEric Biggers * is zero and does not contribute to the final result.
215*00807191SEric Biggers */
216*00807191SEric Biggers
217*00807191SEric Biggers /* T1(x) = floor( R(x) / x^32 ) GF2MUL u */
218*00807191SEric Biggers fpu_vupllf(2, 1);
219*00807191SEric Biggers fpu_vgfmg(2, CONST_RU_POLY, 2);
220*00807191SEric Biggers
221*00807191SEric Biggers /*
222*00807191SEric Biggers * Compute the GF(2) product of the CRC polynomial with T1(x) in
223*00807191SEric Biggers * V2 and XOR the intermediate result, T2(x), with the value in V1.
224*00807191SEric Biggers * The final result is stored in word element 2 of V2.
225*00807191SEric Biggers */
226*00807191SEric Biggers fpu_vupllf(2, 2);
227*00807191SEric Biggers fpu_vgfmag(2, CONST_CRC_POLY, 2, 1);
228*00807191SEric Biggers
229*00807191SEric Biggers return fpu_vlgvf(2, 2);
230*00807191SEric Biggers }
231*00807191SEric Biggers
crc32_le_vgfm_16(u32 crc,unsigned char const * buf,size_t size)232*00807191SEric Biggers u32 crc32_le_vgfm_16(u32 crc, unsigned char const *buf, size_t size)
233*00807191SEric Biggers {
234*00807191SEric Biggers return crc32_le_vgfm_generic(crc, buf, size, &constants_CRC_32_LE[0]);
235*00807191SEric Biggers }
236*00807191SEric Biggers
crc32c_le_vgfm_16(u32 crc,unsigned char const * buf,size_t size)237*00807191SEric Biggers u32 crc32c_le_vgfm_16(u32 crc, unsigned char const *buf, size_t size)
238*00807191SEric Biggers {
239*00807191SEric Biggers return crc32_le_vgfm_generic(crc, buf, size, &constants_CRC_32C_LE[0]);
240*00807191SEric Biggers }
241