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