GNU Linux-libre 4.9.337-gnu1
[releases.git] / arch / s390 / crypto / crc32le-vx.S
1 /*
2  * Hardware-accelerated CRC-32 variants for Linux on z Systems
3  *
4  * Use the z/Architecture Vector Extension Facility to accelerate the
5  * computing of bitreflected CRC-32 checksums for IEEE 802.3 Ethernet
6  * and Castagnoli.
7  *
8  * This CRC-32 implementation algorithm is bitreflected and processes
9  * the least-significant bit first (Little-Endian).
10  *
11  * Copyright IBM Corp. 2015
12  * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
13  */
14
15 #include <linux/linkage.h>
16 #include <asm/nospec-insn.h>
17 #include <asm/vx-insn.h>
18
19 /* Vector register range containing CRC-32 constants */
20 #define CONST_PERM_LE2BE        %v9
21 #define CONST_R2R1              %v10
22 #define CONST_R4R3              %v11
23 #define CONST_R5                %v12
24 #define CONST_RU_POLY           %v13
25 #define CONST_CRC_POLY          %v14
26
27 .data
28 .align 8
29
30 /*
31  * The CRC-32 constant block contains reduction constants to fold and
32  * process particular chunks of the input data stream in parallel.
33  *
34  * For the CRC-32 variants, the constants are precomputed according to
35  * these definitions:
36  *
37  *      R1 = [(x4*128+32 mod P'(x) << 32)]' << 1
38  *      R2 = [(x4*128-32 mod P'(x) << 32)]' << 1
39  *      R3 = [(x128+32 mod P'(x) << 32)]'   << 1
40  *      R4 = [(x128-32 mod P'(x) << 32)]'   << 1
41  *      R5 = [(x64 mod P'(x) << 32)]'       << 1
42  *      R6 = [(x32 mod P'(x) << 32)]'       << 1
43  *
44  *      The bitreflected Barret reduction constant, u', is defined as
45  *      the bit reversal of floor(x**64 / P(x)).
46  *
47  *      where P(x) is the polynomial in the normal domain and the P'(x) is the
48  *      polynomial in the reversed (bitreflected) domain.
49  *
50  * CRC-32 (IEEE 802.3 Ethernet, ...) polynomials:
51  *
52  *      P(x)  = 0x04C11DB7
53  *      P'(x) = 0xEDB88320
54  *
55  * CRC-32C (Castagnoli) polynomials:
56  *
57  *      P(x)  = 0x1EDC6F41
58  *      P'(x) = 0x82F63B78
59  */
60
61 .Lconstants_CRC_32_LE:
62         .octa           0x0F0E0D0C0B0A09080706050403020100      # BE->LE mask
63         .quad           0x1c6e41596, 0x154442bd4                # R2, R1
64         .quad           0x0ccaa009e, 0x1751997d0                # R4, R3
65         .octa           0x163cd6124                             # R5
66         .octa           0x1F7011641                             # u'
67         .octa           0x1DB710641                             # P'(x) << 1
68
69 .Lconstants_CRC_32C_LE:
70         .octa           0x0F0E0D0C0B0A09080706050403020100      # BE->LE mask
71         .quad           0x09e4addf8, 0x740eef02                 # R2, R1
72         .quad           0x14cd00bd6, 0xf20c0dfe                 # R4, R3
73         .octa           0x0dd45aab8                             # R5
74         .octa           0x0dea713f1                             # u'
75         .octa           0x105ec76f0                             # P'(x) << 1
76
77 .previous
78
79         GEN_BR_THUNK %r14
80
81 .text
82
83 /*
84  * The CRC-32 functions use these calling conventions:
85  *
86  * Parameters:
87  *
88  *      %r2:    Initial CRC value, typically ~0; and final CRC (return) value.
89  *      %r3:    Input buffer pointer, performance might be improved if the
90  *              buffer is on a doubleword boundary.
91  *      %r4:    Length of the buffer, must be 64 bytes or greater.
92  *
93  * Register usage:
94  *
95  *      %r5:    CRC-32 constant pool base pointer.
96  *      V0:     Initial CRC value and intermediate constants and results.
97  *      V1..V4: Data for CRC computation.
98  *      V5..V8: Next data chunks that are fetched from the input buffer.
99  *      V9:     Constant for BE->LE conversion and shift operations
100  *
101  *      V10..V14: CRC-32 constants.
102  */
103
104 ENTRY(crc32_le_vgfm_16)
105         larl    %r5,.Lconstants_CRC_32_LE
106         j       crc32_le_vgfm_generic
107
108 ENTRY(crc32c_le_vgfm_16)
109         larl    %r5,.Lconstants_CRC_32C_LE
110         j       crc32_le_vgfm_generic
111
112
113 crc32_le_vgfm_generic:
114         /* Load CRC-32 constants */
115         VLM     CONST_PERM_LE2BE,CONST_CRC_POLY,0,%r5
116
117         /*
118          * Load the initial CRC value.
119          *
120          * The CRC value is loaded into the rightmost word of the
121          * vector register and is later XORed with the LSB portion
122          * of the loaded input data.
123          */
124         VZERO   %v0                     /* Clear V0 */
125         VLVGF   %v0,%r2,3               /* Load CRC into rightmost word */
126
127         /* Load a 64-byte data chunk and XOR with CRC */
128         VLM     %v1,%v4,0,%r3           /* 64-bytes into V1..V4 */
129         VPERM   %v1,%v1,%v1,CONST_PERM_LE2BE
130         VPERM   %v2,%v2,%v2,CONST_PERM_LE2BE
131         VPERM   %v3,%v3,%v3,CONST_PERM_LE2BE
132         VPERM   %v4,%v4,%v4,CONST_PERM_LE2BE
133
134         VX      %v1,%v0,%v1             /* V1 ^= CRC */
135         aghi    %r3,64                  /* BUF = BUF + 64 */
136         aghi    %r4,-64                 /* LEN = LEN - 64 */
137
138         cghi    %r4,64
139         jl      .Lless_than_64bytes
140
141 .Lfold_64bytes_loop:
142         /* Load the next 64-byte data chunk into V5 to V8 */
143         VLM     %v5,%v8,0,%r3
144         VPERM   %v5,%v5,%v5,CONST_PERM_LE2BE
145         VPERM   %v6,%v6,%v6,CONST_PERM_LE2BE
146         VPERM   %v7,%v7,%v7,CONST_PERM_LE2BE
147         VPERM   %v8,%v8,%v8,CONST_PERM_LE2BE
148
149         /*
150          * Perform a GF(2) multiplication of the doublewords in V1 with
151          * the R1 and R2 reduction constants in V0.  The intermediate result
152          * is then folded (accumulated) with the next data chunk in V5 and
153          * stored in V1. Repeat this step for the register contents
154          * in V2, V3, and V4 respectively.
155          */
156         VGFMAG  %v1,CONST_R2R1,%v1,%v5
157         VGFMAG  %v2,CONST_R2R1,%v2,%v6
158         VGFMAG  %v3,CONST_R2R1,%v3,%v7
159         VGFMAG  %v4,CONST_R2R1,%v4,%v8
160
161         aghi    %r3,64                  /* BUF = BUF + 64 */
162         aghi    %r4,-64                 /* LEN = LEN - 64 */
163
164         cghi    %r4,64
165         jnl     .Lfold_64bytes_loop
166
167 .Lless_than_64bytes:
168         /*
169          * Fold V1 to V4 into a single 128-bit value in V1.  Multiply V1 with R3
170          * and R4 and accumulating the next 128-bit chunk until a single 128-bit
171          * value remains.
172          */
173         VGFMAG  %v1,CONST_R4R3,%v1,%v2
174         VGFMAG  %v1,CONST_R4R3,%v1,%v3
175         VGFMAG  %v1,CONST_R4R3,%v1,%v4
176
177         cghi    %r4,16
178         jl      .Lfinal_fold
179
180 .Lfold_16bytes_loop:
181
182         VL      %v2,0,,%r3              /* Load next data chunk */
183         VPERM   %v2,%v2,%v2,CONST_PERM_LE2BE
184         VGFMAG  %v1,CONST_R4R3,%v1,%v2  /* Fold next data chunk */
185
186         aghi    %r3,16
187         aghi    %r4,-16
188
189         cghi    %r4,16
190         jnl     .Lfold_16bytes_loop
191
192 .Lfinal_fold:
193         /*
194          * Set up a vector register for byte shifts.  The shift value must
195          * be loaded in bits 1-4 in byte element 7 of a vector register.
196          * Shift by 8 bytes: 0x40
197          * Shift by 4 bytes: 0x20
198          */
199         VLEIB   %v9,0x40,7
200
201         /*
202          * Prepare V0 for the next GF(2) multiplication: shift V0 by 8 bytes
203          * to move R4 into the rightmost doubleword and set the leftmost
204          * doubleword to 0x1.
205          */
206         VSRLB   %v0,CONST_R4R3,%v9
207         VLEIG   %v0,1,0
208
209         /*
210          * Compute GF(2) product of V1 and V0.  The rightmost doubleword
211          * of V1 is multiplied with R4.  The leftmost doubleword of V1 is
212          * multiplied by 0x1 and is then XORed with rightmost product.
213          * Implicitly, the intermediate leftmost product becomes padded
214          */
215         VGFMG   %v1,%v0,%v1
216
217         /*
218          * Now do the final 32-bit fold by multiplying the rightmost word
219          * in V1 with R5 and XOR the result with the remaining bits in V1.
220          *
221          * To achieve this by a single VGFMAG, right shift V1 by a word
222          * and store the result in V2 which is then accumulated.  Use the
223          * vector unpack instruction to load the rightmost half of the
224          * doubleword into the rightmost doubleword element of V1; the other
225          * half is loaded in the leftmost doubleword.
226          * The vector register with CONST_R5 contains the R5 constant in the
227          * rightmost doubleword and the leftmost doubleword is zero to ignore
228          * the leftmost product of V1.
229          */
230         VLEIB   %v9,0x20,7                /* Shift by words */
231         VSRLB   %v2,%v1,%v9               /* Store remaining bits in V2 */
232         VUPLLF  %v1,%v1                   /* Split rightmost doubleword */
233         VGFMAG  %v1,CONST_R5,%v1,%v2      /* V1 = (V1 * R5) XOR V2 */
234
235         /*
236          * Apply a Barret reduction to compute the final 32-bit CRC value.
237          *
238          * The input values to the Barret reduction are the degree-63 polynomial
239          * in V1 (R(x)), degree-32 generator polynomial, and the reduction
240          * constant u.  The Barret reduction result is the CRC value of R(x) mod
241          * P(x).
242          *
243          * The Barret reduction algorithm is defined as:
244          *
245          *    1. T1(x) = floor( R(x) / x^32 ) GF2MUL u
246          *    2. T2(x) = floor( T1(x) / x^32 ) GF2MUL P(x)
247          *    3. C(x)  = R(x) XOR T2(x) mod x^32
248          *
249          *  Note: The leftmost doubleword of vector register containing
250          *  CONST_RU_POLY is zero and, thus, the intermediate GF(2) product
251          *  is zero and does not contribute to the final result.
252          */
253
254         /* T1(x) = floor( R(x) / x^32 ) GF2MUL u */
255         VUPLLF  %v2,%v1
256         VGFMG   %v2,CONST_RU_POLY,%v2
257
258         /*
259          * Compute the GF(2) product of the CRC polynomial with T1(x) in
260          * V2 and XOR the intermediate result, T2(x), with the value in V1.
261          * The final result is stored in word element 2 of V2.
262          */
263         VUPLLF  %v2,%v2
264         VGFMAG  %v2,CONST_CRC_POLY,%v2,%v1
265
266 .Ldone:
267         VLGVF   %r2,%v2,2
268         BR_EX   %r14
269
270 .previous