1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */ 2 /* 3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. 4 */ 5 6 #include <asm/cpufeature.h> 7 #include <asm/fpu/api.h> 8 #include <asm/processor.h> 9 #include <asm/simd.h> 10 #include <linux/jump_label.h> 11 #include <linux/kernel.h> 12 #include <linux/sizes.h> 13 14 asmlinkage void blake2s_compress_ssse3(struct blake2s_state *state, 15 const u8 *block, const size_t nblocks, 16 const u32 inc); 17 asmlinkage void blake2s_compress_avx512(struct blake2s_state *state, 18 const u8 *block, const size_t nblocks, 19 const u32 inc); 20 21 static __ro_after_init DEFINE_STATIC_KEY_FALSE(blake2s_use_ssse3); 22 static __ro_after_init DEFINE_STATIC_KEY_FALSE(blake2s_use_avx512); 23 24 static void blake2s_compress(struct blake2s_state *state, const u8 *block, 25 size_t nblocks, const u32 inc) 26 { 27 /* SIMD disables preemption, so relax after processing each page. */ 28 BUILD_BUG_ON(SZ_4K / BLAKE2S_BLOCK_SIZE < 8); 29 30 if (!static_branch_likely(&blake2s_use_ssse3) || !may_use_simd()) { 31 blake2s_compress_generic(state, block, nblocks, inc); 32 return; 33 } 34 35 do { 36 const size_t blocks = min_t(size_t, nblocks, 37 SZ_4K / BLAKE2S_BLOCK_SIZE); 38 39 kernel_fpu_begin(); 40 if (static_branch_likely(&blake2s_use_avx512)) 41 blake2s_compress_avx512(state, block, blocks, inc); 42 else 43 blake2s_compress_ssse3(state, block, blocks, inc); 44 kernel_fpu_end(); 45 46 nblocks -= blocks; 47 block += blocks * BLAKE2S_BLOCK_SIZE; 48 } while (nblocks); 49 } 50 51 #define blake2s_mod_init_arch blake2s_mod_init_arch 52 static void blake2s_mod_init_arch(void) 53 { 54 if (boot_cpu_has(X86_FEATURE_SSSE3)) 55 static_branch_enable(&blake2s_use_ssse3); 56 57 if (boot_cpu_has(X86_FEATURE_AVX) && 58 boot_cpu_has(X86_FEATURE_AVX2) && 59 boot_cpu_has(X86_FEATURE_AVX512F) && 60 boot_cpu_has(X86_FEATURE_AVX512VL) && 61 cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM | 62 XFEATURE_MASK_AVX512, NULL)) 63 static_branch_enable(&blake2s_use_avx512); 64 } 65