1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
7 #include <asm/cpufeature.h>
8 #include <asm/processor.h>
9 #include <asm/intel-family.h>
11 #include <sys/simd-x86_64.h>
14 asmlinkage void poly1305_init_x86_64(void *ctx,
15 const u8 key[POLY1305_KEY_SIZE]);
16 asmlinkage void poly1305_blocks_x86_64(void *ctx, const u8 *inp,
17 const size_t len, const u32 padbit);
18 asmlinkage void poly1305_emit_x86_64(void *ctx, u8 mac[POLY1305_MAC_SIZE],
20 asmlinkage void poly1305_emit_avx(void *ctx, u8 mac[POLY1305_MAC_SIZE],
22 asmlinkage void poly1305_blocks_avx(void *ctx, const u8 *inp, const size_t len,
24 asmlinkage void poly1305_blocks_avx2(void *ctx, const u8 *inp, const size_t len,
26 asmlinkage void poly1305_blocks_avx512(void *ctx, const u8 *inp,
27 const size_t len, const u32 padbit);
29 static bool poly1305_use_avx __ro_after_init;
30 static bool poly1305_use_avx2 __ro_after_init;
31 static bool poly1305_use_avx512 __ro_after_init;
32 static bool *const poly1305_nobs[] __initconst = {
33 &poly1305_use_avx, &poly1305_use_avx2, &poly1305_use_avx512 };
35 static void __init poly1305_fpu_init(void)
39 boot_cpu_has(X86_FEATURE_AVX) &&
40 cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL);
42 boot_cpu_has(X86_FEATURE_AVX) &&
43 boot_cpu_has(X86_FEATURE_AVX2) &&
44 cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL);
45 #ifndef COMPAT_CANNOT_USE_AVX512
47 boot_cpu_has(X86_FEATURE_AVX) &&
48 boot_cpu_has(X86_FEATURE_AVX2) &&
49 boot_cpu_has(X86_FEATURE_AVX512F) &&
50 cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM |
51 XFEATURE_MASK_AVX512, NULL) &&
52 /* Skylake downclocks unacceptably much when using zmm. */
53 boot_cpu_data.x86_model != INTEL_FAM6_SKYLAKE_X;
57 poly1305_use_avx = !!(cpu_feature2 & CPUID2_AVX) &&
59 poly1305_use_avx2 = poly1305_use_avx &&
60 !!(cpu_stdext_feature & CPUID_STDEXT_AVX2);
61 poly1305_use_avx512 = poly1305_use_avx2 &&
62 !!(cpu_stdext_feature & CPUID_STDEXT_AVX512F) &&
67 static inline bool poly1305_init_arch(void *ctx,
68 const u8 key[POLY1305_KEY_SIZE])
70 poly1305_init_x86_64(ctx, key);
74 struct poly1305_arch_internal {
84 struct { u32 r2, r1, r4, r3; } rn[9];
87 /* The AVX code uses base 2^26, while the scalar code uses base 2^64. If we hit
88 * the unfortunate situation of using AVX and then having to go back to scalar
89 * -- because the user is silly and has called the update function from two
90 * separate contexts -- then we need to convert back to the original base before
91 * proceeding. It is possible to reason that the initial reduction below is
92 * sufficient given the implementation invariants. However, for an avoidance of
93 * doubt and because this is not performance critical, we do the full reduction
96 static void convert_to_base2_64(void *ctx)
98 struct poly1305_arch_internal *state = ctx;
101 if (!state->is_base2_26)
104 cy = state->h[0] >> 26; state->h[0] &= 0x3ffffff; state->h[1] += cy;
105 cy = state->h[1] >> 26; state->h[1] &= 0x3ffffff; state->h[2] += cy;
106 cy = state->h[2] >> 26; state->h[2] &= 0x3ffffff; state->h[3] += cy;
107 cy = state->h[3] >> 26; state->h[3] &= 0x3ffffff; state->h[4] += cy;
108 state->hs[0] = ((u64)state->h[2] << 52) | ((u64)state->h[1] << 26) | state->h[0];
109 state->hs[1] = ((u64)state->h[4] << 40) | ((u64)state->h[3] << 14) | (state->h[2] >> 12);
110 state->hs[2] = state->h[4] >> 24;
111 #define ULT(a, b) ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
112 cy = (state->hs[2] >> 2) + (state->hs[2] & ~3ULL);
115 state->hs[1] += (cy = ULT(state->hs[0], cy));
116 state->hs[2] += ULT(state->hs[1], cy);
118 state->is_base2_26 = 0;
121 static inline bool poly1305_blocks_arch(void *ctx, const u8 *inp,
122 size_t len, const u32 padbit,
123 simd_context_t *simd_context)
125 struct poly1305_arch_internal *state = ctx;
127 /* SIMD disables preemption, so relax after processing each page. */
128 BUILD_BUG_ON(PAGE_SIZE < POLY1305_BLOCK_SIZE ||
129 PAGE_SIZE % POLY1305_BLOCK_SIZE);
131 if (!poly1305_use_avx ||
132 (len < (POLY1305_BLOCK_SIZE * 18) && !state->is_base2_26) ||
133 !simd_use(simd_context)) {
134 convert_to_base2_64(ctx);
135 poly1305_blocks_x86_64(ctx, inp, len, padbit);
140 const size_t bytes = min_t(size_t, len, PAGE_SIZE);
142 if (poly1305_use_avx512)
143 poly1305_blocks_avx512(ctx, inp, bytes, padbit);
144 else if (poly1305_use_avx2)
145 poly1305_blocks_avx2(ctx, inp, bytes, padbit);
147 poly1305_blocks_avx(ctx, inp, bytes, padbit);
152 simd_relax(simd_context);
158 static inline bool poly1305_emit_arch(void *ctx, u8 mac[POLY1305_MAC_SIZE],
160 simd_context_t *simd_context)
162 struct poly1305_arch_internal *state = ctx;
164 if (!IS_ENABLED(CONFIG_AS_AVX) || !poly1305_use_avx ||
165 !state->is_base2_26 || !simd_use(simd_context)) {
166 convert_to_base2_64(ctx);
167 poly1305_emit_x86_64(ctx, mac, nonce);
169 poly1305_emit_avx(ctx, mac, nonce);