162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Hardware-accelerated CRC-32 variants for Linux on z Systems
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Use the z/Architecture Vector Extension Facility to accelerate the
662306a36Sopenharmony_ci * computing of bitreflected CRC-32 checksums for IEEE 802.3 Ethernet
762306a36Sopenharmony_ci * and Castagnoli.
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * This CRC-32 implementation algorithm is bitreflected and processes
1062306a36Sopenharmony_ci * the least-significant bit first (Little-Endian).
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * Copyright IBM Corp. 2015
1362306a36Sopenharmony_ci * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
1462306a36Sopenharmony_ci */
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#include <linux/linkage.h>
1762306a36Sopenharmony_ci#include <asm/nospec-insn.h>
1862306a36Sopenharmony_ci#include <asm/vx-insn.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/* Vector register range containing CRC-32 constants */
2162306a36Sopenharmony_ci#define CONST_PERM_LE2BE	%v9
2262306a36Sopenharmony_ci#define CONST_R2R1		%v10
2362306a36Sopenharmony_ci#define CONST_R4R3		%v11
2462306a36Sopenharmony_ci#define CONST_R5		%v12
2562306a36Sopenharmony_ci#define CONST_RU_POLY		%v13
2662306a36Sopenharmony_ci#define CONST_CRC_POLY		%v14
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci	.data
2962306a36Sopenharmony_ci	.balign	8
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/*
3262306a36Sopenharmony_ci * The CRC-32 constant block contains reduction constants to fold and
3362306a36Sopenharmony_ci * process particular chunks of the input data stream in parallel.
3462306a36Sopenharmony_ci *
3562306a36Sopenharmony_ci * For the CRC-32 variants, the constants are precomputed according to
3662306a36Sopenharmony_ci * these definitions:
3762306a36Sopenharmony_ci *
3862306a36Sopenharmony_ci *	R1 = [(x4*128+32 mod P'(x) << 32)]' << 1
3962306a36Sopenharmony_ci *	R2 = [(x4*128-32 mod P'(x) << 32)]' << 1
4062306a36Sopenharmony_ci *	R3 = [(x128+32 mod P'(x) << 32)]'   << 1
4162306a36Sopenharmony_ci *	R4 = [(x128-32 mod P'(x) << 32)]'   << 1
4262306a36Sopenharmony_ci *	R5 = [(x64 mod P'(x) << 32)]'	    << 1
4362306a36Sopenharmony_ci *	R6 = [(x32 mod P'(x) << 32)]'	    << 1
4462306a36Sopenharmony_ci *
4562306a36Sopenharmony_ci *	The bitreflected Barret reduction constant, u', is defined as
4662306a36Sopenharmony_ci *	the bit reversal of floor(x**64 / P(x)).
4762306a36Sopenharmony_ci *
4862306a36Sopenharmony_ci *	where P(x) is the polynomial in the normal domain and the P'(x) is the
4962306a36Sopenharmony_ci *	polynomial in the reversed (bitreflected) domain.
5062306a36Sopenharmony_ci *
5162306a36Sopenharmony_ci * CRC-32 (IEEE 802.3 Ethernet, ...) polynomials:
5262306a36Sopenharmony_ci *
5362306a36Sopenharmony_ci *	P(x)  = 0x04C11DB7
5462306a36Sopenharmony_ci *	P'(x) = 0xEDB88320
5562306a36Sopenharmony_ci *
5662306a36Sopenharmony_ci * CRC-32C (Castagnoli) polynomials:
5762306a36Sopenharmony_ci *
5862306a36Sopenharmony_ci *	P(x)  = 0x1EDC6F41
5962306a36Sopenharmony_ci *	P'(x) = 0x82F63B78
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ciSYM_DATA_START_LOCAL(constants_CRC_32_LE)
6362306a36Sopenharmony_ci	.octa		0x0F0E0D0C0B0A09080706050403020100	# BE->LE mask
6462306a36Sopenharmony_ci	.quad		0x1c6e41596, 0x154442bd4		# R2, R1
6562306a36Sopenharmony_ci	.quad		0x0ccaa009e, 0x1751997d0		# R4, R3
6662306a36Sopenharmony_ci	.octa		0x163cd6124				# R5
6762306a36Sopenharmony_ci	.octa		0x1F7011641				# u'
6862306a36Sopenharmony_ci	.octa		0x1DB710641				# P'(x) << 1
6962306a36Sopenharmony_ciSYM_DATA_END(constants_CRC_32_LE)
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ciSYM_DATA_START_LOCAL(constants_CRC_32C_LE)
7262306a36Sopenharmony_ci	.octa		0x0F0E0D0C0B0A09080706050403020100	# BE->LE mask
7362306a36Sopenharmony_ci	.quad		0x09e4addf8, 0x740eef02			# R2, R1
7462306a36Sopenharmony_ci	.quad		0x14cd00bd6, 0xf20c0dfe			# R4, R3
7562306a36Sopenharmony_ci	.octa		0x0dd45aab8				# R5
7662306a36Sopenharmony_ci	.octa		0x0dea713f1				# u'
7762306a36Sopenharmony_ci	.octa		0x105ec76f0				# P'(x) << 1
7862306a36Sopenharmony_ciSYM_DATA_END(constants_CRC_32C_LE)
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	.previous
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	GEN_BR_THUNK %r14
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	.text
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci/*
8762306a36Sopenharmony_ci * The CRC-32 functions use these calling conventions:
8862306a36Sopenharmony_ci *
8962306a36Sopenharmony_ci * Parameters:
9062306a36Sopenharmony_ci *
9162306a36Sopenharmony_ci *	%r2:	Initial CRC value, typically ~0; and final CRC (return) value.
9262306a36Sopenharmony_ci *	%r3:	Input buffer pointer, performance might be improved if the
9362306a36Sopenharmony_ci *		buffer is on a doubleword boundary.
9462306a36Sopenharmony_ci *	%r4:	Length of the buffer, must be 64 bytes or greater.
9562306a36Sopenharmony_ci *
9662306a36Sopenharmony_ci * Register usage:
9762306a36Sopenharmony_ci *
9862306a36Sopenharmony_ci *	%r5:	CRC-32 constant pool base pointer.
9962306a36Sopenharmony_ci *	V0:	Initial CRC value and intermediate constants and results.
10062306a36Sopenharmony_ci *	V1..V4:	Data for CRC computation.
10162306a36Sopenharmony_ci *	V5..V8:	Next data chunks that are fetched from the input buffer.
10262306a36Sopenharmony_ci *	V9:	Constant for BE->LE conversion and shift operations
10362306a36Sopenharmony_ci *
10462306a36Sopenharmony_ci *	V10..V14: CRC-32 constants.
10562306a36Sopenharmony_ci */
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ciSYM_FUNC_START(crc32_le_vgfm_16)
10862306a36Sopenharmony_ci	larl	%r5,constants_CRC_32_LE
10962306a36Sopenharmony_ci	j	crc32_le_vgfm_generic
11062306a36Sopenharmony_ciSYM_FUNC_END(crc32_le_vgfm_16)
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ciSYM_FUNC_START(crc32c_le_vgfm_16)
11362306a36Sopenharmony_ci	larl	%r5,constants_CRC_32C_LE
11462306a36Sopenharmony_ci	j	crc32_le_vgfm_generic
11562306a36Sopenharmony_ciSYM_FUNC_END(crc32c_le_vgfm_16)
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ciSYM_FUNC_START(crc32_le_vgfm_generic)
11862306a36Sopenharmony_ci	/* Load CRC-32 constants */
11962306a36Sopenharmony_ci	VLM	CONST_PERM_LE2BE,CONST_CRC_POLY,0,%r5
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	/*
12262306a36Sopenharmony_ci	 * Load the initial CRC value.
12362306a36Sopenharmony_ci	 *
12462306a36Sopenharmony_ci	 * The CRC value is loaded into the rightmost word of the
12562306a36Sopenharmony_ci	 * vector register and is later XORed with the LSB portion
12662306a36Sopenharmony_ci	 * of the loaded input data.
12762306a36Sopenharmony_ci	 */
12862306a36Sopenharmony_ci	VZERO	%v0			/* Clear V0 */
12962306a36Sopenharmony_ci	VLVGF	%v0,%r2,3		/* Load CRC into rightmost word */
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	/* Load a 64-byte data chunk and XOR with CRC */
13262306a36Sopenharmony_ci	VLM	%v1,%v4,0,%r3		/* 64-bytes into V1..V4 */
13362306a36Sopenharmony_ci	VPERM	%v1,%v1,%v1,CONST_PERM_LE2BE
13462306a36Sopenharmony_ci	VPERM	%v2,%v2,%v2,CONST_PERM_LE2BE
13562306a36Sopenharmony_ci	VPERM	%v3,%v3,%v3,CONST_PERM_LE2BE
13662306a36Sopenharmony_ci	VPERM	%v4,%v4,%v4,CONST_PERM_LE2BE
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	VX	%v1,%v0,%v1		/* V1 ^= CRC */
13962306a36Sopenharmony_ci	aghi	%r3,64			/* BUF = BUF + 64 */
14062306a36Sopenharmony_ci	aghi	%r4,-64			/* LEN = LEN - 64 */
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	cghi	%r4,64
14362306a36Sopenharmony_ci	jl	.Lless_than_64bytes
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci.Lfold_64bytes_loop:
14662306a36Sopenharmony_ci	/* Load the next 64-byte data chunk into V5 to V8 */
14762306a36Sopenharmony_ci	VLM	%v5,%v8,0,%r3
14862306a36Sopenharmony_ci	VPERM	%v5,%v5,%v5,CONST_PERM_LE2BE
14962306a36Sopenharmony_ci	VPERM	%v6,%v6,%v6,CONST_PERM_LE2BE
15062306a36Sopenharmony_ci	VPERM	%v7,%v7,%v7,CONST_PERM_LE2BE
15162306a36Sopenharmony_ci	VPERM	%v8,%v8,%v8,CONST_PERM_LE2BE
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	/*
15462306a36Sopenharmony_ci	 * Perform a GF(2) multiplication of the doublewords in V1 with
15562306a36Sopenharmony_ci	 * the R1 and R2 reduction constants in V0.  The intermediate result
15662306a36Sopenharmony_ci	 * is then folded (accumulated) with the next data chunk in V5 and
15762306a36Sopenharmony_ci	 * stored in V1. Repeat this step for the register contents
15862306a36Sopenharmony_ci	 * in V2, V3, and V4 respectively.
15962306a36Sopenharmony_ci	 */
16062306a36Sopenharmony_ci	VGFMAG	%v1,CONST_R2R1,%v1,%v5
16162306a36Sopenharmony_ci	VGFMAG	%v2,CONST_R2R1,%v2,%v6
16262306a36Sopenharmony_ci	VGFMAG	%v3,CONST_R2R1,%v3,%v7
16362306a36Sopenharmony_ci	VGFMAG	%v4,CONST_R2R1,%v4,%v8
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	aghi	%r3,64			/* BUF = BUF + 64 */
16662306a36Sopenharmony_ci	aghi	%r4,-64			/* LEN = LEN - 64 */
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	cghi	%r4,64
16962306a36Sopenharmony_ci	jnl	.Lfold_64bytes_loop
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci.Lless_than_64bytes:
17262306a36Sopenharmony_ci	/*
17362306a36Sopenharmony_ci	 * Fold V1 to V4 into a single 128-bit value in V1.  Multiply V1 with R3
17462306a36Sopenharmony_ci	 * and R4 and accumulating the next 128-bit chunk until a single 128-bit
17562306a36Sopenharmony_ci	 * value remains.
17662306a36Sopenharmony_ci	 */
17762306a36Sopenharmony_ci	VGFMAG	%v1,CONST_R4R3,%v1,%v2
17862306a36Sopenharmony_ci	VGFMAG	%v1,CONST_R4R3,%v1,%v3
17962306a36Sopenharmony_ci	VGFMAG	%v1,CONST_R4R3,%v1,%v4
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	cghi	%r4,16
18262306a36Sopenharmony_ci	jl	.Lfinal_fold
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci.Lfold_16bytes_loop:
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	VL	%v2,0,,%r3		/* Load next data chunk */
18762306a36Sopenharmony_ci	VPERM	%v2,%v2,%v2,CONST_PERM_LE2BE
18862306a36Sopenharmony_ci	VGFMAG	%v1,CONST_R4R3,%v1,%v2	/* Fold next data chunk */
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci	aghi	%r3,16
19162306a36Sopenharmony_ci	aghi	%r4,-16
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	cghi	%r4,16
19462306a36Sopenharmony_ci	jnl	.Lfold_16bytes_loop
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci.Lfinal_fold:
19762306a36Sopenharmony_ci	/*
19862306a36Sopenharmony_ci	 * Set up a vector register for byte shifts.  The shift value must
19962306a36Sopenharmony_ci	 * be loaded in bits 1-4 in byte element 7 of a vector register.
20062306a36Sopenharmony_ci	 * Shift by 8 bytes: 0x40
20162306a36Sopenharmony_ci	 * Shift by 4 bytes: 0x20
20262306a36Sopenharmony_ci	 */
20362306a36Sopenharmony_ci	VLEIB	%v9,0x40,7
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	/*
20662306a36Sopenharmony_ci	 * Prepare V0 for the next GF(2) multiplication: shift V0 by 8 bytes
20762306a36Sopenharmony_ci	 * to move R4 into the rightmost doubleword and set the leftmost
20862306a36Sopenharmony_ci	 * doubleword to 0x1.
20962306a36Sopenharmony_ci	 */
21062306a36Sopenharmony_ci	VSRLB	%v0,CONST_R4R3,%v9
21162306a36Sopenharmony_ci	VLEIG	%v0,1,0
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	/*
21462306a36Sopenharmony_ci	 * Compute GF(2) product of V1 and V0.	The rightmost doubleword
21562306a36Sopenharmony_ci	 * of V1 is multiplied with R4.  The leftmost doubleword of V1 is
21662306a36Sopenharmony_ci	 * multiplied by 0x1 and is then XORed with rightmost product.
21762306a36Sopenharmony_ci	 * Implicitly, the intermediate leftmost product becomes padded
21862306a36Sopenharmony_ci	 */
21962306a36Sopenharmony_ci	VGFMG	%v1,%v0,%v1
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	/*
22262306a36Sopenharmony_ci	 * Now do the final 32-bit fold by multiplying the rightmost word
22362306a36Sopenharmony_ci	 * in V1 with R5 and XOR the result with the remaining bits in V1.
22462306a36Sopenharmony_ci	 *
22562306a36Sopenharmony_ci	 * To achieve this by a single VGFMAG, right shift V1 by a word
22662306a36Sopenharmony_ci	 * and store the result in V2 which is then accumulated.  Use the
22762306a36Sopenharmony_ci	 * vector unpack instruction to load the rightmost half of the
22862306a36Sopenharmony_ci	 * doubleword into the rightmost doubleword element of V1; the other
22962306a36Sopenharmony_ci	 * half is loaded in the leftmost doubleword.
23062306a36Sopenharmony_ci	 * The vector register with CONST_R5 contains the R5 constant in the
23162306a36Sopenharmony_ci	 * rightmost doubleword and the leftmost doubleword is zero to ignore
23262306a36Sopenharmony_ci	 * the leftmost product of V1.
23362306a36Sopenharmony_ci	 */
23462306a36Sopenharmony_ci	VLEIB	%v9,0x20,7		  /* Shift by words */
23562306a36Sopenharmony_ci	VSRLB	%v2,%v1,%v9		  /* Store remaining bits in V2 */
23662306a36Sopenharmony_ci	VUPLLF	%v1,%v1			  /* Split rightmost doubleword */
23762306a36Sopenharmony_ci	VGFMAG	%v1,CONST_R5,%v1,%v2	  /* V1 = (V1 * R5) XOR V2 */
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	/*
24062306a36Sopenharmony_ci	 * Apply a Barret reduction to compute the final 32-bit CRC value.
24162306a36Sopenharmony_ci	 *
24262306a36Sopenharmony_ci	 * The input values to the Barret reduction are the degree-63 polynomial
24362306a36Sopenharmony_ci	 * in V1 (R(x)), degree-32 generator polynomial, and the reduction
24462306a36Sopenharmony_ci	 * constant u.	The Barret reduction result is the CRC value of R(x) mod
24562306a36Sopenharmony_ci	 * P(x).
24662306a36Sopenharmony_ci	 *
24762306a36Sopenharmony_ci	 * The Barret reduction algorithm is defined as:
24862306a36Sopenharmony_ci	 *
24962306a36Sopenharmony_ci	 *    1. T1(x) = floor( R(x) / x^32 ) GF2MUL u
25062306a36Sopenharmony_ci	 *    2. T2(x) = floor( T1(x) / x^32 ) GF2MUL P(x)
25162306a36Sopenharmony_ci	 *    3. C(x)  = R(x) XOR T2(x) mod x^32
25262306a36Sopenharmony_ci	 *
25362306a36Sopenharmony_ci	 *  Note: The leftmost doubleword of vector register containing
25462306a36Sopenharmony_ci	 *  CONST_RU_POLY is zero and, thus, the intermediate GF(2) product
25562306a36Sopenharmony_ci	 *  is zero and does not contribute to the final result.
25662306a36Sopenharmony_ci	 */
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci	/* T1(x) = floor( R(x) / x^32 ) GF2MUL u */
25962306a36Sopenharmony_ci	VUPLLF	%v2,%v1
26062306a36Sopenharmony_ci	VGFMG	%v2,CONST_RU_POLY,%v2
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci	/*
26362306a36Sopenharmony_ci	 * Compute the GF(2) product of the CRC polynomial with T1(x) in
26462306a36Sopenharmony_ci	 * V2 and XOR the intermediate result, T2(x), with the value in V1.
26562306a36Sopenharmony_ci	 * The final result is stored in word element 2 of V2.
26662306a36Sopenharmony_ci	 */
26762306a36Sopenharmony_ci	VUPLLF	%v2,%v2
26862306a36Sopenharmony_ci	VGFMAG	%v2,CONST_CRC_POLY,%v2,%v1
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci.Ldone:
27162306a36Sopenharmony_ci	VLGVF	%r2,%v2,2
27262306a36Sopenharmony_ci	BR_EX	%r14
27362306a36Sopenharmony_ciSYM_FUNC_END(crc32_le_vgfm_generic)
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci.previous
276