4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (C) 2016 Gvozden Nešković. All rights reserved.
24 #include <sys/isa_defs.h>
26 #if defined(__x86_64) && defined(HAVE_AVX2)
28 #include <sys/types.h>
32 #define __asm __asm__ __volatile__
35 #define _REG_CNT(_0, _1, _2, _3, _4, _5, _6, _7, N, ...) N
36 #define REG_CNT(r...) _REG_CNT(r, 8, 7, 6, 5, 4, 3, 2, 1)
38 #define VR0_(REG, ...) "ymm"#REG
39 #define VR1_(_1, REG, ...) "ymm"#REG
40 #define VR2_(_1, _2, REG, ...) "ymm"#REG
41 #define VR3_(_1, _2, _3, REG, ...) "ymm"#REG
42 #define VR4_(_1, _2, _3, _4, REG, ...) "ymm"#REG
43 #define VR5_(_1, _2, _3, _4, _5, REG, ...) "ymm"#REG
44 #define VR6_(_1, _2, _3, _4, _5, _6, REG, ...) "ymm"#REG
45 #define VR7_(_1, _2, _3, _4, _5, _6, _7, REG, ...) "ymm"#REG
47 #define VR0(r...) VR0_(r)
48 #define VR1(r...) VR1_(r)
49 #define VR2(r...) VR2_(r, 1)
50 #define VR3(r...) VR3_(r, 1, 2)
51 #define VR4(r...) VR4_(r, 1, 2)
52 #define VR5(r...) VR5_(r, 1, 2, 3)
53 #define VR6(r...) VR6_(r, 1, 2, 3, 4)
54 #define VR7(r...) VR7_(r, 1, 2, 3, 4, 5)
56 #define R_01(REG1, REG2, ...) REG1, REG2
57 #define _R_23(_0, _1, REG2, REG3, ...) REG2, REG3
58 #define R_23(REG...) _R_23(REG, 1, 2, 3)
60 #define ZFS_ASM_BUG() ASSERT(0)
62 extern const uint8_t gf_clmul_mod_lt
[4*256][16];
67 uint8_t b
[ELEM_SIZE
] __attribute__((aligned(ELEM_SIZE
)));
71 #define XOR_ACC(src, r...) \
73 switch (REG_CNT(r)) { \
76 "vpxor 0x00(%[SRC]), %%" VR0(r)", %%" VR0(r) "\n" \
77 "vpxor 0x20(%[SRC]), %%" VR1(r)", %%" VR1(r) "\n" \
78 "vpxor 0x40(%[SRC]), %%" VR2(r)", %%" VR2(r) "\n" \
79 "vpxor 0x60(%[SRC]), %%" VR3(r)", %%" VR3(r) "\n" \
80 : : [SRC] "r" (src)); \
84 "vpxor 0x00(%[SRC]), %%" VR0(r)", %%" VR0(r) "\n" \
85 "vpxor 0x20(%[SRC]), %%" VR1(r)", %%" VR1(r) "\n" \
86 : : [SRC] "r" (src)); \
95 switch (REG_CNT(r)) { \
98 "vpxor %" VR0(r) ", %" VR4(r)", %" VR4(r) "\n" \
99 "vpxor %" VR1(r) ", %" VR5(r)", %" VR5(r) "\n" \
100 "vpxor %" VR2(r) ", %" VR6(r)", %" VR6(r) "\n" \
101 "vpxor %" VR3(r) ", %" VR7(r)", %" VR7(r)); \
105 "vpxor %" VR0(r) ", %" VR2(r)", %" VR2(r) "\n" \
106 "vpxor %" VR1(r) ", %" VR3(r)", %" VR3(r)); \
113 #define ZERO(r...) XOR(r, r)
117 switch (REG_CNT(r)) { \
120 "vmovdqa %" VR0(r) ", %" VR4(r) "\n" \
121 "vmovdqa %" VR1(r) ", %" VR5(r) "\n" \
122 "vmovdqa %" VR2(r) ", %" VR6(r) "\n" \
123 "vmovdqa %" VR3(r) ", %" VR7(r)); \
127 "vmovdqa %" VR0(r) ", %" VR2(r) "\n" \
128 "vmovdqa %" VR1(r) ", %" VR3(r)); \
135 #define LOAD(src, r...) \
137 switch (REG_CNT(r)) { \
140 "vmovdqa 0x00(%[SRC]), %%" VR0(r) "\n" \
141 "vmovdqa 0x20(%[SRC]), %%" VR1(r) "\n" \
142 "vmovdqa 0x40(%[SRC]), %%" VR2(r) "\n" \
143 "vmovdqa 0x60(%[SRC]), %%" VR3(r) "\n" \
144 : : [SRC] "r" (src)); \
148 "vmovdqa 0x00(%[SRC]), %%" VR0(r) "\n" \
149 "vmovdqa 0x20(%[SRC]), %%" VR1(r) "\n" \
150 : : [SRC] "r" (src)); \
157 #define STORE(dst, r...) \
159 switch (REG_CNT(r)) { \
162 "vmovdqa %%" VR0(r) ", 0x00(%[DST])\n" \
163 "vmovdqa %%" VR1(r) ", 0x20(%[DST])\n" \
164 "vmovdqa %%" VR2(r) ", 0x40(%[DST])\n" \
165 "vmovdqa %%" VR3(r) ", 0x60(%[DST])\n" \
166 : : [DST] "r" (dst)); \
170 "vmovdqa %%" VR0(r) ", 0x00(%[DST])\n" \
171 "vmovdqa %%" VR1(r) ", 0x20(%[DST])\n" \
172 : : [DST] "r" (dst)); \
181 __asm("vzeroupper"); \
184 #define MUL2_SETUP() \
186 __asm("vmovq %0, %%xmm14" :: "r"(0x1d1d1d1d1d1d1d1d)); \
187 __asm("vpbroadcastq %xmm14, %ymm14"); \
188 __asm("vpxor %ymm15, %ymm15 ,%ymm15"); \
191 #define _MUL2(r...) \
193 switch (REG_CNT(r)) { \
196 "vpcmpgtb %" VR0(r)", %ymm15, %ymm12\n" \
197 "vpcmpgtb %" VR1(r)", %ymm15, %ymm13\n" \
198 "vpaddb %" VR0(r)", %" VR0(r)", %" VR0(r) "\n" \
199 "vpaddb %" VR1(r)", %" VR1(r)", %" VR1(r) "\n" \
200 "vpand %ymm14, %ymm12, %ymm12\n" \
201 "vpand %ymm14, %ymm13, %ymm13\n" \
202 "vpxor %ymm12, %" VR0(r)", %" VR0(r) "\n" \
203 "vpxor %ymm13, %" VR1(r)", %" VR1(r)); \
212 switch (REG_CNT(r)) { \
234 #define _ltmod "ymm12"
235 #define _ltmul "ymm11"
239 static const uint8_t __attribute__((aligned(32))) _mul_mask
= 0x0F;
241 #define _MULx2(c, r...) \
243 switch (REG_CNT(r)) { \
246 "vpbroadcastb (%[mask]), %%" _0f "\n" \
248 "vbroadcasti128 0x00(%[lt]), %%" _ltmod "\n" \
249 "vbroadcasti128 0x10(%[lt]), %%" _ltmul "\n" \
251 "vpsraw $0x4, %%" VR0(r) ", %%"_as "\n" \
252 "vpsraw $0x4, %%" VR1(r) ", %%"_bs "\n" \
253 "vpand %%" _0f ", %%" VR0(r) ", %%" VR0(r) "\n" \
254 "vpand %%" _0f ", %%" VR1(r) ", %%" VR1(r) "\n" \
255 "vpand %%" _0f ", %%" _as ", %%" _as "\n" \
256 "vpand %%" _0f ", %%" _bs ", %%" _bs "\n" \
258 "vpshufb %%" _as ", %%" _ltmod ", %%" _ta "\n" \
259 "vpshufb %%" _bs ", %%" _ltmod ", %%" _tb "\n" \
260 "vpshufb %%" _as ", %%" _ltmul ", %%" _as "\n" \
261 "vpshufb %%" _bs ", %%" _ltmul ", %%" _bs "\n" \
263 "vbroadcasti128 0x20(%[lt]), %%" _ltmod "\n" \
264 "vbroadcasti128 0x30(%[lt]), %%" _ltmul "\n" \
266 "vpxor %%" _ta ", %%" _as ", %%" _as "\n" \
267 "vpxor %%" _tb ", %%" _bs ", %%" _bs "\n" \
269 "vpshufb %%" VR0(r) ", %%" _ltmod ", %%" _ta "\n" \
270 "vpshufb %%" VR1(r) ", %%" _ltmod ", %%" _tb "\n" \
271 "vpshufb %%" VR0(r) ", %%" _ltmul ", %%" VR0(r) "\n"\
272 "vpshufb %%" VR1(r) ", %%" _ltmul ", %%" VR1(r) "\n"\
274 "vpxor %%" _ta ", %%" VR0(r) ", %%" VR0(r) "\n" \
275 "vpxor %%" _as ", %%" VR0(r) ", %%" VR0(r) "\n" \
276 "vpxor %%" _tb ", %%" VR1(r) ", %%" VR1(r) "\n" \
277 "vpxor %%" _bs ", %%" VR1(r) ", %%" VR1(r) "\n" \
278 : : [mask] "r" (&_mul_mask), \
279 [lt] "r" (gf_clmul_mod_lt[4*(c)])); \
286 #define MUL(c, r...) \
288 switch (REG_CNT(r)) { \
290 _MULx2(c, R_01(r)); \
291 _MULx2(c, R_23(r)); \
294 _MULx2(c, R_01(r)); \
301 #define raidz_math_begin() kfpu_begin()
302 #define raidz_math_end() \
311 #define ZERO_STRIDE 4
312 #define ZERO_DEFINE() {}
313 #define ZERO_D 0, 1, 2, 3
315 #define COPY_STRIDE 4
316 #define COPY_DEFINE() {}
317 #define COPY_D 0, 1, 2, 3
320 #define ADD_DEFINE() {}
321 #define ADD_D 0, 1, 2, 3
324 #define MUL_DEFINE() {}
325 #define MUL_D 0, 1, 2, 3
327 #define GEN_P_STRIDE 4
328 #define GEN_P_DEFINE() {}
329 #define GEN_P_P 0, 1, 2, 3
331 #define GEN_PQ_STRIDE 4
332 #define GEN_PQ_DEFINE() {}
333 #define GEN_PQ_D 0, 1, 2, 3
334 #define GEN_PQ_C 4, 5, 6, 7
336 #define GEN_PQR_STRIDE 4
337 #define GEN_PQR_DEFINE() {}
338 #define GEN_PQR_D 0, 1, 2, 3
339 #define GEN_PQR_C 4, 5, 6, 7
341 #define SYN_Q_DEFINE() {}
342 #define SYN_Q_D 0, 1, 2, 3
343 #define SYN_Q_X 4, 5, 6, 7
345 #define SYN_R_DEFINE() {}
346 #define SYN_R_D 0, 1, 2, 3
347 #define SYN_R_X 4, 5, 6, 7
349 #define SYN_PQ_DEFINE() {}
350 #define SYN_PQ_D 0, 1, 2, 3
351 #define SYN_PQ_X 4, 5, 6, 7
353 #define REC_PQ_STRIDE 2
354 #define REC_PQ_DEFINE() {}
355 #define REC_PQ_X 0, 1
356 #define REC_PQ_Y 2, 3
357 #define REC_PQ_T 4, 5
359 #define SYN_PR_DEFINE() {}
360 #define SYN_PR_D 0, 1, 2, 3
361 #define SYN_PR_X 4, 5, 6, 7
363 #define REC_PR_STRIDE 2
364 #define REC_PR_DEFINE() {}
365 #define REC_PR_X 0, 1
366 #define REC_PR_Y 2, 3
367 #define REC_PR_T 4, 5
369 #define SYN_QR_DEFINE() {}
370 #define SYN_QR_D 0, 1, 2, 3
371 #define SYN_QR_X 4, 5, 6, 7
373 #define REC_QR_STRIDE 2
374 #define REC_QR_DEFINE() {}
375 #define REC_QR_X 0, 1
376 #define REC_QR_Y 2, 3
377 #define REC_QR_T 4, 5
379 #define SYN_PQR_DEFINE() {}
380 #define SYN_PQR_D 0, 1, 2, 3
381 #define SYN_PQR_X 4, 5, 6, 7
383 #define REC_PQR_STRIDE 2
384 #define REC_PQR_DEFINE() {}
385 #define REC_PQR_X 0, 1
386 #define REC_PQR_Y 2, 3
387 #define REC_PQR_Z 4, 5
388 #define REC_PQR_XS 6, 7
389 #define REC_PQR_YS 8, 9
392 #include <sys/vdev_raidz_impl.h>
393 #include "vdev_raidz_math_impl.h"
395 DEFINE_GEN_METHODS(avx2
);
396 DEFINE_REC_METHODS(avx2
);
399 raidz_will_avx2_work(void)
401 return (kfpu_allowed() && zfs_avx_available() && zfs_avx2_available());
404 const raidz_impl_ops_t vdev_raidz_avx2_impl
= {
407 .gen
= RAIDZ_GEN_METHODS(avx2
),
408 .rec
= RAIDZ_REC_METHODS(avx2
),
409 .is_supported
= &raidz_will_avx2_work
,
413 #endif /* defined(__x86_64) && defined(HAVE_AVX2) */