summaryrefslogtreecommitdiff
path: root/lib/crypto/x86/nh.h
blob: 83361c2e97838d0b5840161753db1905377b4edc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * x86_64 accelerated implementation of NH
 *
 * Copyright 2018 Google LLC
 */

#include <asm/fpu/api.h>
#include <linux/static_call.h>

static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_sse2);
static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_avx2);

asmlinkage void nh_sse2(const u32 *key, const u8 *message, size_t message_len,
			__le64 hash[NH_NUM_PASSES]);
asmlinkage void nh_avx2(const u32 *key, const u8 *message, size_t message_len,
			__le64 hash[NH_NUM_PASSES]);

static bool nh_arch(const u32 *key, const u8 *message, size_t message_len,
		    __le64 hash[NH_NUM_PASSES])
{
	if (message_len >= 64 && static_branch_likely(&have_sse2) &&
	    irq_fpu_usable()) {
		kernel_fpu_begin();
		if (static_branch_likely(&have_avx2))
			nh_avx2(key, message, message_len, hash);
		else
			nh_sse2(key, message, message_len, hash);
		kernel_fpu_end();
		return true;
	}
	return false;
}

#define nh_mod_init_arch nh_mod_init_arch
static void nh_mod_init_arch(void)
{
	if (boot_cpu_has(X86_FEATURE_XMM2)) {
		static_branch_enable(&have_sse2);
		if (boot_cpu_has(X86_FEATURE_AVX2) &&
		    cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
				      NULL))
			static_branch_enable(&have_avx2);
	}
}