/* SPDX-License-Identifier: GPL-2.0 */1/*2* Hardware-accelerated CRC-32 variants for Linux on z Systems3*4* Use the z/Architecture Vector Extension Facility to accelerate the5* computing of CRC-32 checksums.6*7* This CRC-32 implementation algorithm processes the most-significant8* bit first (BE).9*10* Copyright IBM Corp. 201511* Author(s): Hendrik Brueckner <[email protected]>12*/1314#include <linux/types.h>15#include <asm/fpu.h>16#include "crc32-vx.h"1718/* Vector register range containing CRC-32 constants */19#define CONST_R1R2 920#define CONST_R3R4 1021#define CONST_R5 1122#define CONST_R6 1223#define CONST_RU_POLY 1324#define CONST_CRC_POLY 142526/*27* The CRC-32 constant block contains reduction constants to fold and28* process particular chunks of the input data stream in parallel.29*30* For the CRC-32 variants, the constants are precomputed according to31* these definitions:32*33* R1 = x4*128+64 mod P(x)34* R2 = x4*128 mod P(x)35* R3 = x128+64 mod P(x)36* R4 = x128 mod P(x)37* R5 = x96 mod P(x)38* R6 = x64 mod P(x)39*40* Barret reduction constant, u, is defined as floor(x**64 / P(x)).41*42* where P(x) is the polynomial in the normal domain and the P'(x) is the43* polynomial in the reversed (bitreflected) domain.44*45* Note that the constant definitions below are extended in order to compute46* intermediate results with a single VECTOR GALOIS FIELD MULTIPLY instruction.47* The rightmost doubleword can be 0 to prevent contribution to the result or48* can be multiplied by 1 to perform an XOR without the need for a separate49* VECTOR EXCLUSIVE OR instruction.50*51* CRC-32 (IEEE 802.3 Ethernet, ...) polynomials:52*53* P(x) = 0x04C11DB754* P'(x) = 0xEDB8832055*/5657static unsigned long constants_CRC_32_BE[] = {580x08833794c, 0x0e6228b11, /* R1, R2 */590x0c5b9cd4c, 0x0e8a45605, /* R3, R4 */600x0f200aa66, 1UL << 32, /* R5, x32 */610x0490d678d, 1, /* R6, 1 */620x104d101df, 0, /* u */630x104C11DB7, 0, /* P(x) */64};6566/**67* crc32_be_vgfm_16 - Compute CRC-32 (BE variant) with vector registers68* @crc: Initial CRC value, typically ~0.69* @buf: Input buffer pointer, performance might be improved if the70* buffer is on a doubleword boundary.71* @size: Size of the buffer, must be 64 bytes or greater.72*73* Register usage:74* V0: Initial CRC value and intermediate constants and results.75* V1..V4: Data for CRC computation.76* V5..V8: Next data chunks that are fetched from the input buffer.77* V9..V14: CRC-32 constants.78*/79u32 crc32_be_vgfm_16(u32 crc, unsigned char const *buf, size_t size)80{81/* Load CRC-32 constants */82fpu_vlm(CONST_R1R2, CONST_CRC_POLY, &constants_CRC_32_BE);83fpu_vzero(0);8485/* Load the initial CRC value into the leftmost word of V0. */86fpu_vlvgf(0, crc, 0);8788/* Load a 64-byte data chunk and XOR with CRC */89fpu_vlm(1, 4, buf);90fpu_vx(1, 0, 1);91buf += 64;92size -= 64;9394while (size >= 64) {95/* Load the next 64-byte data chunk into V5 to V8 */96fpu_vlm(5, 8, buf);9798/*99* Perform a GF(2) multiplication of the doublewords in V1 with100* the reduction constants in V0. The intermediate result is101* then folded (accumulated) with the next data chunk in V5 and102* stored in V1. Repeat this step for the register contents103* in V2, V3, and V4 respectively.104*/105fpu_vgfmag(1, CONST_R1R2, 1, 5);106fpu_vgfmag(2, CONST_R1R2, 2, 6);107fpu_vgfmag(3, CONST_R1R2, 3, 7);108fpu_vgfmag(4, CONST_R1R2, 4, 8);109buf += 64;110size -= 64;111}112113/* Fold V1 to V4 into a single 128-bit value in V1 */114fpu_vgfmag(1, CONST_R3R4, 1, 2);115fpu_vgfmag(1, CONST_R3R4, 1, 3);116fpu_vgfmag(1, CONST_R3R4, 1, 4);117118while (size >= 16) {119fpu_vl(2, buf);120fpu_vgfmag(1, CONST_R3R4, 1, 2);121buf += 16;122size -= 16;123}124125/*126* The R5 constant is used to fold a 128-bit value into an 96-bit value127* that is XORed with the next 96-bit input data chunk. To use a single128* VGFMG instruction, multiply the rightmost 64-bit with x^32 (1<<32) to129* form an intermediate 96-bit value (with appended zeros) which is then130* XORed with the intermediate reduction result.131*/132fpu_vgfmg(1, CONST_R5, 1);133134/*135* Further reduce the remaining 96-bit value to a 64-bit value using a136* single VGFMG, the rightmost doubleword is multiplied with 0x1. The137* intermediate result is then XORed with the product of the leftmost138* doubleword with R6. The result is a 64-bit value and is subject to139* the Barret reduction.140*/141fpu_vgfmg(1, CONST_R6, 1);142143/*144* The input values to the Barret reduction are the degree-63 polynomial145* in V1 (R(x)), degree-32 generator polynomial, and the reduction146* constant u. The Barret reduction result is the CRC value of R(x) mod147* P(x).148*149* The Barret reduction algorithm is defined as:150*151* 1. T1(x) = floor( R(x) / x^32 ) GF2MUL u152* 2. T2(x) = floor( T1(x) / x^32 ) GF2MUL P(x)153* 3. C(x) = R(x) XOR T2(x) mod x^32154*155* Note: To compensate the division by x^32, use the vector unpack156* instruction to move the leftmost word into the leftmost doubleword157* of the vector register. The rightmost doubleword is multiplied158* with zero to not contribute to the intermediate results.159*/160161/* T1(x) = floor( R(x) / x^32 ) GF2MUL u */162fpu_vupllf(2, 1);163fpu_vgfmg(2, CONST_RU_POLY, 2);164165/*166* Compute the GF(2) product of the CRC polynomial in VO with T1(x) in167* V2 and XOR the intermediate result, T2(x), with the value in V1.168* The final result is in the rightmost word of V2.169*/170fpu_vupllf(2, 2);171fpu_vgfmag(2, CONST_CRC_POLY, 2, 1);172return fpu_vlgvf(2, 3);173}174175176