/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* SHA-1 optimized for PowerPC3*4* Copyright (c) 2015 Markus Stockhausen <[email protected]>5*/67#include <asm/switch_to.h>8#include <linux/preempt.h>910#ifdef CONFIG_SPE11/*12* MAX_BYTES defines the number of bytes that are allowed to be processed13* between preempt_disable() and preempt_enable(). SHA1 takes ~100014* operations per 64 bytes. e500 cores can issue two arithmetic instructions15* per clock cycle using one 32/64 bit unit (SU1) and one 32 bit unit (SU2).16* Thus 2KB of input data will need an estimated maximum of 18,000 cycles.17* Headroom for cache misses included. Even with the low end model clocked18* at 667 MHz this equals to a critical time window of less than 27us.19*20*/21#define MAX_BYTES 20482223asmlinkage void ppc_spe_sha1_transform(struct sha1_block_state *state,24const u8 *data, u32 nblocks);2526static void spe_begin(void)27{28/* We just start SPE operations and will save SPE registers later. */29preempt_disable();30enable_kernel_spe();31}3233static void spe_end(void)34{35disable_kernel_spe();36/* reenable preemption */37preempt_enable();38}3940static void sha1_blocks(struct sha1_block_state *state,41const u8 *data, size_t nblocks)42{43do {44u32 unit = min_t(size_t, nblocks, MAX_BYTES / SHA1_BLOCK_SIZE);4546spe_begin();47ppc_spe_sha1_transform(state, data, unit);48spe_end();4950data += unit * SHA1_BLOCK_SIZE;51nblocks -= unit;52} while (nblocks);53}54#else /* CONFIG_SPE */55asmlinkage void powerpc_sha_transform(struct sha1_block_state *state,56const u8 data[SHA1_BLOCK_SIZE]);5758static void sha1_blocks(struct sha1_block_state *state,59const u8 *data, size_t nblocks)60{61do {62powerpc_sha_transform(state, data);63data += SHA1_BLOCK_SIZE;64} while (--nblocks);65}66#endif /* !CONFIG_SPE */676869