18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Hardware-accelerated CRC-32 variants for Linux on z Systems
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Use the z/Architecture Vector Extension Facility to accelerate the
68c2ecf20Sopenharmony_ci * computing of bitreflected CRC-32 checksums for IEEE 802.3 Ethernet
78c2ecf20Sopenharmony_ci * and Castagnoli.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This CRC-32 implementation algorithm is bitreflected and processes
108c2ecf20Sopenharmony_ci * the least-significant bit first (Little-Endian).
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Copyright IBM Corp. 2015
138c2ecf20Sopenharmony_ci * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/linkage.h>
178c2ecf20Sopenharmony_ci#include <asm/nospec-insn.h>
188c2ecf20Sopenharmony_ci#include <asm/vx-insn.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* Vector register range containing CRC-32 constants */
218c2ecf20Sopenharmony_ci#define CONST_PERM_LE2BE	%v9
228c2ecf20Sopenharmony_ci#define CONST_R2R1		%v10
238c2ecf20Sopenharmony_ci#define CONST_R4R3		%v11
248c2ecf20Sopenharmony_ci#define CONST_R5		%v12
258c2ecf20Sopenharmony_ci#define CONST_RU_POLY		%v13
268c2ecf20Sopenharmony_ci#define CONST_CRC_POLY		%v14
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci.data
298c2ecf20Sopenharmony_ci.align 8
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/*
328c2ecf20Sopenharmony_ci * The CRC-32 constant block contains reduction constants to fold and
338c2ecf20Sopenharmony_ci * process particular chunks of the input data stream in parallel.
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * For the CRC-32 variants, the constants are precomputed according to
368c2ecf20Sopenharmony_ci * these definitions:
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci *	R1 = [(x4*128+32 mod P'(x) << 32)]' << 1
398c2ecf20Sopenharmony_ci *	R2 = [(x4*128-32 mod P'(x) << 32)]' << 1
408c2ecf20Sopenharmony_ci *	R3 = [(x128+32 mod P'(x) << 32)]'   << 1
418c2ecf20Sopenharmony_ci *	R4 = [(x128-32 mod P'(x) << 32)]'   << 1
428c2ecf20Sopenharmony_ci *	R5 = [(x64 mod P'(x) << 32)]'	    << 1
438c2ecf20Sopenharmony_ci *	R6 = [(x32 mod P'(x) << 32)]'	    << 1
448c2ecf20Sopenharmony_ci *
458c2ecf20Sopenharmony_ci *	The bitreflected Barret reduction constant, u', is defined as
468c2ecf20Sopenharmony_ci *	the bit reversal of floor(x**64 / P(x)).
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci *	where P(x) is the polynomial in the normal domain and the P'(x) is the
498c2ecf20Sopenharmony_ci *	polynomial in the reversed (bitreflected) domain.
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * CRC-32 (IEEE 802.3 Ethernet, ...) polynomials:
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci *	P(x)  = 0x04C11DB7
548c2ecf20Sopenharmony_ci *	P'(x) = 0xEDB88320
558c2ecf20Sopenharmony_ci *
568c2ecf20Sopenharmony_ci * CRC-32C (Castagnoli) polynomials:
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci *	P(x)  = 0x1EDC6F41
598c2ecf20Sopenharmony_ci *	P'(x) = 0x82F63B78
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci.Lconstants_CRC_32_LE:
638c2ecf20Sopenharmony_ci	.octa		0x0F0E0D0C0B0A09080706050403020100	# BE->LE mask
648c2ecf20Sopenharmony_ci	.quad		0x1c6e41596, 0x154442bd4		# R2, R1
658c2ecf20Sopenharmony_ci	.quad		0x0ccaa009e, 0x1751997d0		# R4, R3
668c2ecf20Sopenharmony_ci	.octa		0x163cd6124				# R5
678c2ecf20Sopenharmony_ci	.octa		0x1F7011641				# u'
688c2ecf20Sopenharmony_ci	.octa		0x1DB710641				# P'(x) << 1
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci.Lconstants_CRC_32C_LE:
718c2ecf20Sopenharmony_ci	.octa		0x0F0E0D0C0B0A09080706050403020100	# BE->LE mask
728c2ecf20Sopenharmony_ci	.quad		0x09e4addf8, 0x740eef02			# R2, R1
738c2ecf20Sopenharmony_ci	.quad		0x14cd00bd6, 0xf20c0dfe			# R4, R3
748c2ecf20Sopenharmony_ci	.octa		0x0dd45aab8				# R5
758c2ecf20Sopenharmony_ci	.octa		0x0dea713f1				# u'
768c2ecf20Sopenharmony_ci	.octa		0x105ec76f0				# P'(x) << 1
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci.previous
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	GEN_BR_THUNK %r14
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci.text
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/*
858c2ecf20Sopenharmony_ci * The CRC-32 functions use these calling conventions:
868c2ecf20Sopenharmony_ci *
878c2ecf20Sopenharmony_ci * Parameters:
888c2ecf20Sopenharmony_ci *
898c2ecf20Sopenharmony_ci *	%r2:	Initial CRC value, typically ~0; and final CRC (return) value.
908c2ecf20Sopenharmony_ci *	%r3:	Input buffer pointer, performance might be improved if the
918c2ecf20Sopenharmony_ci *		buffer is on a doubleword boundary.
928c2ecf20Sopenharmony_ci *	%r4:	Length of the buffer, must be 64 bytes or greater.
938c2ecf20Sopenharmony_ci *
948c2ecf20Sopenharmony_ci * Register usage:
958c2ecf20Sopenharmony_ci *
968c2ecf20Sopenharmony_ci *	%r5:	CRC-32 constant pool base pointer.
978c2ecf20Sopenharmony_ci *	V0:	Initial CRC value and intermediate constants and results.
988c2ecf20Sopenharmony_ci *	V1..V4:	Data for CRC computation.
998c2ecf20Sopenharmony_ci *	V5..V8:	Next data chunks that are fetched from the input buffer.
1008c2ecf20Sopenharmony_ci *	V9:	Constant for BE->LE conversion and shift operations
1018c2ecf20Sopenharmony_ci *
1028c2ecf20Sopenharmony_ci *	V10..V14: CRC-32 constants.
1038c2ecf20Sopenharmony_ci */
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ciENTRY(crc32_le_vgfm_16)
1068c2ecf20Sopenharmony_ci	larl	%r5,.Lconstants_CRC_32_LE
1078c2ecf20Sopenharmony_ci	j	crc32_le_vgfm_generic
1088c2ecf20Sopenharmony_ciENDPROC(crc32_le_vgfm_16)
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ciENTRY(crc32c_le_vgfm_16)
1118c2ecf20Sopenharmony_ci	larl	%r5,.Lconstants_CRC_32C_LE
1128c2ecf20Sopenharmony_ci	j	crc32_le_vgfm_generic
1138c2ecf20Sopenharmony_ciENDPROC(crc32c_le_vgfm_16)
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciENTRY(crc32_le_vgfm_generic)
1168c2ecf20Sopenharmony_ci	/* Load CRC-32 constants */
1178c2ecf20Sopenharmony_ci	VLM	CONST_PERM_LE2BE,CONST_CRC_POLY,0,%r5
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	/*
1208c2ecf20Sopenharmony_ci	 * Load the initial CRC value.
1218c2ecf20Sopenharmony_ci	 *
1228c2ecf20Sopenharmony_ci	 * The CRC value is loaded into the rightmost word of the
1238c2ecf20Sopenharmony_ci	 * vector register and is later XORed with the LSB portion
1248c2ecf20Sopenharmony_ci	 * of the loaded input data.
1258c2ecf20Sopenharmony_ci	 */
1268c2ecf20Sopenharmony_ci	VZERO	%v0			/* Clear V0 */
1278c2ecf20Sopenharmony_ci	VLVGF	%v0,%r2,3		/* Load CRC into rightmost word */
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	/* Load a 64-byte data chunk and XOR with CRC */
1308c2ecf20Sopenharmony_ci	VLM	%v1,%v4,0,%r3		/* 64-bytes into V1..V4 */
1318c2ecf20Sopenharmony_ci	VPERM	%v1,%v1,%v1,CONST_PERM_LE2BE
1328c2ecf20Sopenharmony_ci	VPERM	%v2,%v2,%v2,CONST_PERM_LE2BE
1338c2ecf20Sopenharmony_ci	VPERM	%v3,%v3,%v3,CONST_PERM_LE2BE
1348c2ecf20Sopenharmony_ci	VPERM	%v4,%v4,%v4,CONST_PERM_LE2BE
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	VX	%v1,%v0,%v1		/* V1 ^= CRC */
1378c2ecf20Sopenharmony_ci	aghi	%r3,64			/* BUF = BUF + 64 */
1388c2ecf20Sopenharmony_ci	aghi	%r4,-64			/* LEN = LEN - 64 */
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	cghi	%r4,64
1418c2ecf20Sopenharmony_ci	jl	.Lless_than_64bytes
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci.Lfold_64bytes_loop:
1448c2ecf20Sopenharmony_ci	/* Load the next 64-byte data chunk into V5 to V8 */
1458c2ecf20Sopenharmony_ci	VLM	%v5,%v8,0,%r3
1468c2ecf20Sopenharmony_ci	VPERM	%v5,%v5,%v5,CONST_PERM_LE2BE
1478c2ecf20Sopenharmony_ci	VPERM	%v6,%v6,%v6,CONST_PERM_LE2BE
1488c2ecf20Sopenharmony_ci	VPERM	%v7,%v7,%v7,CONST_PERM_LE2BE
1498c2ecf20Sopenharmony_ci	VPERM	%v8,%v8,%v8,CONST_PERM_LE2BE
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	/*
1528c2ecf20Sopenharmony_ci	 * Perform a GF(2) multiplication of the doublewords in V1 with
1538c2ecf20Sopenharmony_ci	 * the R1 and R2 reduction constants in V0.  The intermediate result
1548c2ecf20Sopenharmony_ci	 * is then folded (accumulated) with the next data chunk in V5 and
1558c2ecf20Sopenharmony_ci	 * stored in V1. Repeat this step for the register contents
1568c2ecf20Sopenharmony_ci	 * in V2, V3, and V4 respectively.
1578c2ecf20Sopenharmony_ci	 */
1588c2ecf20Sopenharmony_ci	VGFMAG	%v1,CONST_R2R1,%v1,%v5
1598c2ecf20Sopenharmony_ci	VGFMAG	%v2,CONST_R2R1,%v2,%v6
1608c2ecf20Sopenharmony_ci	VGFMAG	%v3,CONST_R2R1,%v3,%v7
1618c2ecf20Sopenharmony_ci	VGFMAG	%v4,CONST_R2R1,%v4,%v8
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	aghi	%r3,64			/* BUF = BUF + 64 */
1648c2ecf20Sopenharmony_ci	aghi	%r4,-64			/* LEN = LEN - 64 */
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	cghi	%r4,64
1678c2ecf20Sopenharmony_ci	jnl	.Lfold_64bytes_loop
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci.Lless_than_64bytes:
1708c2ecf20Sopenharmony_ci	/*
1718c2ecf20Sopenharmony_ci	 * Fold V1 to V4 into a single 128-bit value in V1.  Multiply V1 with R3
1728c2ecf20Sopenharmony_ci	 * and R4 and accumulating the next 128-bit chunk until a single 128-bit
1738c2ecf20Sopenharmony_ci	 * value remains.
1748c2ecf20Sopenharmony_ci	 */
1758c2ecf20Sopenharmony_ci	VGFMAG	%v1,CONST_R4R3,%v1,%v2
1768c2ecf20Sopenharmony_ci	VGFMAG	%v1,CONST_R4R3,%v1,%v3
1778c2ecf20Sopenharmony_ci	VGFMAG	%v1,CONST_R4R3,%v1,%v4
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	cghi	%r4,16
1808c2ecf20Sopenharmony_ci	jl	.Lfinal_fold
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci.Lfold_16bytes_loop:
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	VL	%v2,0,,%r3		/* Load next data chunk */
1858c2ecf20Sopenharmony_ci	VPERM	%v2,%v2,%v2,CONST_PERM_LE2BE
1868c2ecf20Sopenharmony_ci	VGFMAG	%v1,CONST_R4R3,%v1,%v2	/* Fold next data chunk */
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	aghi	%r3,16
1898c2ecf20Sopenharmony_ci	aghi	%r4,-16
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	cghi	%r4,16
1928c2ecf20Sopenharmony_ci	jnl	.Lfold_16bytes_loop
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci.Lfinal_fold:
1958c2ecf20Sopenharmony_ci	/*
1968c2ecf20Sopenharmony_ci	 * Set up a vector register for byte shifts.  The shift value must
1978c2ecf20Sopenharmony_ci	 * be loaded in bits 1-4 in byte element 7 of a vector register.
1988c2ecf20Sopenharmony_ci	 * Shift by 8 bytes: 0x40
1998c2ecf20Sopenharmony_ci	 * Shift by 4 bytes: 0x20
2008c2ecf20Sopenharmony_ci	 */
2018c2ecf20Sopenharmony_ci	VLEIB	%v9,0x40,7
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	/*
2048c2ecf20Sopenharmony_ci	 * Prepare V0 for the next GF(2) multiplication: shift V0 by 8 bytes
2058c2ecf20Sopenharmony_ci	 * to move R4 into the rightmost doubleword and set the leftmost
2068c2ecf20Sopenharmony_ci	 * doubleword to 0x1.
2078c2ecf20Sopenharmony_ci	 */
2088c2ecf20Sopenharmony_ci	VSRLB	%v0,CONST_R4R3,%v9
2098c2ecf20Sopenharmony_ci	VLEIG	%v0,1,0
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	/*
2128c2ecf20Sopenharmony_ci	 * Compute GF(2) product of V1 and V0.	The rightmost doubleword
2138c2ecf20Sopenharmony_ci	 * of V1 is multiplied with R4.  The leftmost doubleword of V1 is
2148c2ecf20Sopenharmony_ci	 * multiplied by 0x1 and is then XORed with rightmost product.
2158c2ecf20Sopenharmony_ci	 * Implicitly, the intermediate leftmost product becomes padded
2168c2ecf20Sopenharmony_ci	 */
2178c2ecf20Sopenharmony_ci	VGFMG	%v1,%v0,%v1
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	/*
2208c2ecf20Sopenharmony_ci	 * Now do the final 32-bit fold by multiplying the rightmost word
2218c2ecf20Sopenharmony_ci	 * in V1 with R5 and XOR the result with the remaining bits in V1.
2228c2ecf20Sopenharmony_ci	 *
2238c2ecf20Sopenharmony_ci	 * To achieve this by a single VGFMAG, right shift V1 by a word
2248c2ecf20Sopenharmony_ci	 * and store the result in V2 which is then accumulated.  Use the
2258c2ecf20Sopenharmony_ci	 * vector unpack instruction to load the rightmost half of the
2268c2ecf20Sopenharmony_ci	 * doubleword into the rightmost doubleword element of V1; the other
2278c2ecf20Sopenharmony_ci	 * half is loaded in the leftmost doubleword.
2288c2ecf20Sopenharmony_ci	 * The vector register with CONST_R5 contains the R5 constant in the
2298c2ecf20Sopenharmony_ci	 * rightmost doubleword and the leftmost doubleword is zero to ignore
2308c2ecf20Sopenharmony_ci	 * the leftmost product of V1.
2318c2ecf20Sopenharmony_ci	 */
2328c2ecf20Sopenharmony_ci	VLEIB	%v9,0x20,7		  /* Shift by words */
2338c2ecf20Sopenharmony_ci	VSRLB	%v2,%v1,%v9		  /* Store remaining bits in V2 */
2348c2ecf20Sopenharmony_ci	VUPLLF	%v1,%v1			  /* Split rightmost doubleword */
2358c2ecf20Sopenharmony_ci	VGFMAG	%v1,CONST_R5,%v1,%v2	  /* V1 = (V1 * R5) XOR V2 */
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	/*
2388c2ecf20Sopenharmony_ci	 * Apply a Barret reduction to compute the final 32-bit CRC value.
2398c2ecf20Sopenharmony_ci	 *
2408c2ecf20Sopenharmony_ci	 * The input values to the Barret reduction are the degree-63 polynomial
2418c2ecf20Sopenharmony_ci	 * in V1 (R(x)), degree-32 generator polynomial, and the reduction
2428c2ecf20Sopenharmony_ci	 * constant u.	The Barret reduction result is the CRC value of R(x) mod
2438c2ecf20Sopenharmony_ci	 * P(x).
2448c2ecf20Sopenharmony_ci	 *
2458c2ecf20Sopenharmony_ci	 * The Barret reduction algorithm is defined as:
2468c2ecf20Sopenharmony_ci	 *
2478c2ecf20Sopenharmony_ci	 *    1. T1(x) = floor( R(x) / x^32 ) GF2MUL u
2488c2ecf20Sopenharmony_ci	 *    2. T2(x) = floor( T1(x) / x^32 ) GF2MUL P(x)
2498c2ecf20Sopenharmony_ci	 *    3. C(x)  = R(x) XOR T2(x) mod x^32
2508c2ecf20Sopenharmony_ci	 *
2518c2ecf20Sopenharmony_ci	 *  Note: The leftmost doubleword of vector register containing
2528c2ecf20Sopenharmony_ci	 *  CONST_RU_POLY is zero and, thus, the intermediate GF(2) product
2538c2ecf20Sopenharmony_ci	 *  is zero and does not contribute to the final result.
2548c2ecf20Sopenharmony_ci	 */
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	/* T1(x) = floor( R(x) / x^32 ) GF2MUL u */
2578c2ecf20Sopenharmony_ci	VUPLLF	%v2,%v1
2588c2ecf20Sopenharmony_ci	VGFMG	%v2,CONST_RU_POLY,%v2
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	/*
2618c2ecf20Sopenharmony_ci	 * Compute the GF(2) product of the CRC polynomial with T1(x) in
2628c2ecf20Sopenharmony_ci	 * V2 and XOR the intermediate result, T2(x), with the value in V1.
2638c2ecf20Sopenharmony_ci	 * The final result is stored in word element 2 of V2.
2648c2ecf20Sopenharmony_ci	 */
2658c2ecf20Sopenharmony_ci	VUPLLF	%v2,%v2
2668c2ecf20Sopenharmony_ci	VGFMAG	%v2,CONST_CRC_POLY,%v2,%v1
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci.Ldone:
2698c2ecf20Sopenharmony_ci	VLGVF	%r2,%v2,2
2708c2ecf20Sopenharmony_ci	BR_EX	%r14
2718c2ecf20Sopenharmony_ciENDPROC(crc32_le_vgfm_generic)
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci.previous
274