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 CRC-32 checksums.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * This CRC-32 implementation algorithm processes the most-significant
962306a36Sopenharmony_ci * bit first (BE).
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * Copyright IBM Corp. 2015
1262306a36Sopenharmony_ci * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
1362306a36Sopenharmony_ci */
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include <linux/linkage.h>
1662306a36Sopenharmony_ci#include <asm/nospec-insn.h>
1762306a36Sopenharmony_ci#include <asm/vx-insn.h>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/* Vector register range containing CRC-32 constants */
2062306a36Sopenharmony_ci#define CONST_R1R2		%v9
2162306a36Sopenharmony_ci#define CONST_R3R4		%v10
2262306a36Sopenharmony_ci#define CONST_R5		%v11
2362306a36Sopenharmony_ci#define CONST_R6		%v12
2462306a36Sopenharmony_ci#define CONST_RU_POLY		%v13
2562306a36Sopenharmony_ci#define CONST_CRC_POLY		%v14
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci	.data
2862306a36Sopenharmony_ci	.balign	8
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/*
3162306a36Sopenharmony_ci * The CRC-32 constant block contains reduction constants to fold and
3262306a36Sopenharmony_ci * process particular chunks of the input data stream in parallel.
3362306a36Sopenharmony_ci *
3462306a36Sopenharmony_ci * For the CRC-32 variants, the constants are precomputed according to
3562306a36Sopenharmony_ci * these definitions:
3662306a36Sopenharmony_ci *
3762306a36Sopenharmony_ci *	R1 = x4*128+64 mod P(x)
3862306a36Sopenharmony_ci *	R2 = x4*128    mod P(x)
3962306a36Sopenharmony_ci *	R3 = x128+64   mod P(x)
4062306a36Sopenharmony_ci *	R4 = x128      mod P(x)
4162306a36Sopenharmony_ci *	R5 = x96       mod P(x)
4262306a36Sopenharmony_ci *	R6 = x64       mod P(x)
4362306a36Sopenharmony_ci *
4462306a36Sopenharmony_ci *	Barret reduction constant, u, is defined as floor(x**64 / P(x)).
4562306a36Sopenharmony_ci *
4662306a36Sopenharmony_ci *	where P(x) is the polynomial in the normal domain and the P'(x) is the
4762306a36Sopenharmony_ci *	polynomial in the reversed (bitreflected) domain.
4862306a36Sopenharmony_ci *
4962306a36Sopenharmony_ci * Note that the constant definitions below are extended in order to compute
5062306a36Sopenharmony_ci * intermediate results with a single VECTOR GALOIS FIELD MULTIPLY instruction.
5162306a36Sopenharmony_ci * The rightmost doubleword can be 0 to prevent contribution to the result or
5262306a36Sopenharmony_ci * can be multiplied by 1 to perform an XOR without the need for a separate
5362306a36Sopenharmony_ci * VECTOR EXCLUSIVE OR instruction.
5462306a36Sopenharmony_ci *
5562306a36Sopenharmony_ci * CRC-32 (IEEE 802.3 Ethernet, ...) polynomials:
5662306a36Sopenharmony_ci *
5762306a36Sopenharmony_ci *	P(x)  = 0x04C11DB7
5862306a36Sopenharmony_ci *	P'(x) = 0xEDB88320
5962306a36Sopenharmony_ci */
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ciSYM_DATA_START_LOCAL(constants_CRC_32_BE)
6262306a36Sopenharmony_ci	.quad		0x08833794c, 0x0e6228b11	# R1, R2
6362306a36Sopenharmony_ci	.quad		0x0c5b9cd4c, 0x0e8a45605	# R3, R4
6462306a36Sopenharmony_ci	.quad		0x0f200aa66, 1 << 32		# R5, x32
6562306a36Sopenharmony_ci	.quad		0x0490d678d, 1			# R6, 1
6662306a36Sopenharmony_ci	.quad		0x104d101df, 0			# u
6762306a36Sopenharmony_ci	.quad		0x104C11DB7, 0			# P(x)
6862306a36Sopenharmony_ciSYM_DATA_END(constants_CRC_32_BE)
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	.previous
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	GEN_BR_THUNK %r14
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	.text
7562306a36Sopenharmony_ci/*
7662306a36Sopenharmony_ci * The CRC-32 function(s) use these calling conventions:
7762306a36Sopenharmony_ci *
7862306a36Sopenharmony_ci * Parameters:
7962306a36Sopenharmony_ci *
8062306a36Sopenharmony_ci *	%r2:	Initial CRC value, typically ~0; and final CRC (return) value.
8162306a36Sopenharmony_ci *	%r3:	Input buffer pointer, performance might be improved if the
8262306a36Sopenharmony_ci *		buffer is on a doubleword boundary.
8362306a36Sopenharmony_ci *	%r4:	Length of the buffer, must be 64 bytes or greater.
8462306a36Sopenharmony_ci *
8562306a36Sopenharmony_ci * Register usage:
8662306a36Sopenharmony_ci *
8762306a36Sopenharmony_ci *	%r5:	CRC-32 constant pool base pointer.
8862306a36Sopenharmony_ci *	V0:	Initial CRC value and intermediate constants and results.
8962306a36Sopenharmony_ci *	V1..V4:	Data for CRC computation.
9062306a36Sopenharmony_ci *	V5..V8:	Next data chunks that are fetched from the input buffer.
9162306a36Sopenharmony_ci *
9262306a36Sopenharmony_ci *	V9..V14: CRC-32 constants.
9362306a36Sopenharmony_ci */
9462306a36Sopenharmony_ciSYM_FUNC_START(crc32_be_vgfm_16)
9562306a36Sopenharmony_ci	/* Load CRC-32 constants */
9662306a36Sopenharmony_ci	larl	%r5,constants_CRC_32_BE
9762306a36Sopenharmony_ci	VLM	CONST_R1R2,CONST_CRC_POLY,0,%r5
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	/* Load the initial CRC value into the leftmost word of V0. */
10062306a36Sopenharmony_ci	VZERO	%v0
10162306a36Sopenharmony_ci	VLVGF	%v0,%r2,0
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	/* Load a 64-byte data chunk and XOR with CRC */
10462306a36Sopenharmony_ci	VLM	%v1,%v4,0,%r3		/* 64-bytes into V1..V4 */
10562306a36Sopenharmony_ci	VX	%v1,%v0,%v1		/* V1 ^= CRC */
10662306a36Sopenharmony_ci	aghi	%r3,64			/* BUF = BUF + 64 */
10762306a36Sopenharmony_ci	aghi	%r4,-64			/* LEN = LEN - 64 */
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	/* Check remaining buffer size and jump to proper folding method */
11062306a36Sopenharmony_ci	cghi	%r4,64
11162306a36Sopenharmony_ci	jl	.Lless_than_64bytes
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci.Lfold_64bytes_loop:
11462306a36Sopenharmony_ci	/* Load the next 64-byte data chunk into V5 to V8 */
11562306a36Sopenharmony_ci	VLM	%v5,%v8,0,%r3
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	/*
11862306a36Sopenharmony_ci	 * Perform a GF(2) multiplication of the doublewords in V1 with
11962306a36Sopenharmony_ci	 * the reduction constants in V0.  The intermediate result is
12062306a36Sopenharmony_ci	 * then folded (accumulated) with the next data chunk in V5 and
12162306a36Sopenharmony_ci	 * stored in V1.  Repeat this step for the register contents
12262306a36Sopenharmony_ci	 * in V2, V3, and V4 respectively.
12362306a36Sopenharmony_ci	 */
12462306a36Sopenharmony_ci	VGFMAG	%v1,CONST_R1R2,%v1,%v5
12562306a36Sopenharmony_ci	VGFMAG	%v2,CONST_R1R2,%v2,%v6
12662306a36Sopenharmony_ci	VGFMAG	%v3,CONST_R1R2,%v3,%v7
12762306a36Sopenharmony_ci	VGFMAG	%v4,CONST_R1R2,%v4,%v8
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	/* Adjust buffer pointer and length for next loop */
13062306a36Sopenharmony_ci	aghi	%r3,64			/* BUF = BUF + 64 */
13162306a36Sopenharmony_ci	aghi	%r4,-64			/* LEN = LEN - 64 */
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	cghi	%r4,64
13462306a36Sopenharmony_ci	jnl	.Lfold_64bytes_loop
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci.Lless_than_64bytes:
13762306a36Sopenharmony_ci	/* Fold V1 to V4 into a single 128-bit value in V1 */
13862306a36Sopenharmony_ci	VGFMAG	%v1,CONST_R3R4,%v1,%v2
13962306a36Sopenharmony_ci	VGFMAG	%v1,CONST_R3R4,%v1,%v3
14062306a36Sopenharmony_ci	VGFMAG	%v1,CONST_R3R4,%v1,%v4
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	/* Check whether to continue with 64-bit folding */
14362306a36Sopenharmony_ci	cghi	%r4,16
14462306a36Sopenharmony_ci	jl	.Lfinal_fold
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci.Lfold_16bytes_loop:
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	VL	%v2,0,,%r3		/* Load next data chunk */
14962306a36Sopenharmony_ci	VGFMAG	%v1,CONST_R3R4,%v1,%v2	/* Fold next data chunk */
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci	/* Adjust buffer pointer and size for folding next data chunk */
15262306a36Sopenharmony_ci	aghi	%r3,16
15362306a36Sopenharmony_ci	aghi	%r4,-16
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	/* Process remaining data chunks */
15662306a36Sopenharmony_ci	cghi	%r4,16
15762306a36Sopenharmony_ci	jnl	.Lfold_16bytes_loop
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci.Lfinal_fold:
16062306a36Sopenharmony_ci	/*
16162306a36Sopenharmony_ci	 * The R5 constant is used to fold a 128-bit value into an 96-bit value
16262306a36Sopenharmony_ci	 * that is XORed with the next 96-bit input data chunk.  To use a single
16362306a36Sopenharmony_ci	 * VGFMG instruction, multiply the rightmost 64-bit with x^32 (1<<32) to
16462306a36Sopenharmony_ci	 * form an intermediate 96-bit value (with appended zeros) which is then
16562306a36Sopenharmony_ci	 * XORed with the intermediate reduction result.
16662306a36Sopenharmony_ci	 */
16762306a36Sopenharmony_ci	VGFMG	%v1,CONST_R5,%v1
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	/*
17062306a36Sopenharmony_ci	 * Further reduce the remaining 96-bit value to a 64-bit value using a
17162306a36Sopenharmony_ci	 * single VGFMG, the rightmost doubleword is multiplied with 0x1. The
17262306a36Sopenharmony_ci	 * intermediate result is then XORed with the product of the leftmost
17362306a36Sopenharmony_ci	 * doubleword with R6.	The result is a 64-bit value and is subject to
17462306a36Sopenharmony_ci	 * the Barret reduction.
17562306a36Sopenharmony_ci	 */
17662306a36Sopenharmony_ci	VGFMG	%v1,CONST_R6,%v1
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	/*
17962306a36Sopenharmony_ci	 * The input values to the Barret reduction are the degree-63 polynomial
18062306a36Sopenharmony_ci	 * in V1 (R(x)), degree-32 generator polynomial, and the reduction
18162306a36Sopenharmony_ci	 * constant u.	The Barret reduction result is the CRC value of R(x) mod
18262306a36Sopenharmony_ci	 * P(x).
18362306a36Sopenharmony_ci	 *
18462306a36Sopenharmony_ci	 * The Barret reduction algorithm is defined as:
18562306a36Sopenharmony_ci	 *
18662306a36Sopenharmony_ci	 *    1. T1(x) = floor( R(x) / x^32 ) GF2MUL u
18762306a36Sopenharmony_ci	 *    2. T2(x) = floor( T1(x) / x^32 ) GF2MUL P(x)
18862306a36Sopenharmony_ci	 *    3. C(x)  = R(x) XOR T2(x) mod x^32
18962306a36Sopenharmony_ci	 *
19062306a36Sopenharmony_ci	 * Note: To compensate the division by x^32, use the vector unpack
19162306a36Sopenharmony_ci	 * instruction to move the leftmost word into the leftmost doubleword
19262306a36Sopenharmony_ci	 * of the vector register.  The rightmost doubleword is multiplied
19362306a36Sopenharmony_ci	 * with zero to not contribute to the intermediate results.
19462306a36Sopenharmony_ci	 */
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	/* T1(x) = floor( R(x) / x^32 ) GF2MUL u */
19762306a36Sopenharmony_ci	VUPLLF	%v2,%v1
19862306a36Sopenharmony_ci	VGFMG	%v2,CONST_RU_POLY,%v2
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	/*
20162306a36Sopenharmony_ci	 * Compute the GF(2) product of the CRC polynomial in VO with T1(x) in
20262306a36Sopenharmony_ci	 * V2 and XOR the intermediate result, T2(x),  with the value in V1.
20362306a36Sopenharmony_ci	 * The final result is in the rightmost word of V2.
20462306a36Sopenharmony_ci	 */
20562306a36Sopenharmony_ci	VUPLLF	%v2,%v2
20662306a36Sopenharmony_ci	VGFMAG	%v2,CONST_CRC_POLY,%v2,%v1
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci.Ldone:
20962306a36Sopenharmony_ci	VLGVF	%r2,%v2,3
21062306a36Sopenharmony_ci	BR_EX	%r14
21162306a36Sopenharmony_ciSYM_FUNC_END(crc32_be_vgfm_16)
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci.previous
214