1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * SHA-1 optimized for PowerPC
4  *
5  * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de>
6  */
7 
8 #include <asm/switch_to.h>
9 #include <linux/preempt.h>
10 
11 #ifdef CONFIG_SPE
12 /*
13  * MAX_BYTES defines the number of bytes that are allowed to be processed
14  * between preempt_disable() and preempt_enable(). SHA1 takes ~1000
15  * operations per 64 bytes. e500 cores can issue two arithmetic instructions
16  * per clock cycle using one 32/64 bit unit (SU1) and one 32 bit unit (SU2).
17  * Thus 2KB of input data will need an estimated maximum of 18,000 cycles.
18  * Headroom for cache misses included. Even with the low end model clocked
19  * at 667 MHz this equals to a critical time window of less than 27us.
20  *
21  */
22 #define MAX_BYTES 2048
23 
24 asmlinkage void ppc_spe_sha1_transform(struct sha1_block_state *state,
25 				       const u8 *data, u32 nblocks);
26 
spe_begin(void)27 static void spe_begin(void)
28 {
29 	/* We just start SPE operations and will save SPE registers later. */
30 	preempt_disable();
31 	enable_kernel_spe();
32 }
33 
spe_end(void)34 static void spe_end(void)
35 {
36 	disable_kernel_spe();
37 	/* reenable preemption */
38 	preempt_enable();
39 }
40 
sha1_blocks(struct sha1_block_state * state,const u8 * data,size_t nblocks)41 static void sha1_blocks(struct sha1_block_state *state,
42 			const u8 *data, size_t nblocks)
43 {
44 	do {
45 		u32 unit = min_t(size_t, nblocks, MAX_BYTES / SHA1_BLOCK_SIZE);
46 
47 		spe_begin();
48 		ppc_spe_sha1_transform(state, data, unit);
49 		spe_end();
50 
51 		data += unit * SHA1_BLOCK_SIZE;
52 		nblocks -= unit;
53 	} while (nblocks);
54 }
55 #else /* CONFIG_SPE */
56 asmlinkage void powerpc_sha_transform(struct sha1_block_state *state,
57 				      const u8 data[SHA1_BLOCK_SIZE]);
58 
sha1_blocks(struct sha1_block_state * state,const u8 * data,size_t nblocks)59 static void sha1_blocks(struct sha1_block_state *state,
60 			const u8 *data, size_t nblocks)
61 {
62 	do {
63 		powerpc_sha_transform(state, data);
64 		data += SHA1_BLOCK_SIZE;
65 	} while (--nblocks);
66 }
67 #endif /* !CONFIG_SPE */
68