/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* SHA-256 Secure Hash Algorithm, SPE optimized3*4* Based on generic implementation. The assembler module takes care5* about the SPE registers so it can run from interrupt context.6*7* Copyright (c) 2015 Markus Stockhausen <[email protected]>8*/910#include <asm/switch_to.h>11#include <linux/preempt.h>1213/*14* MAX_BYTES defines the number of bytes that are allowed to be processed15* between preempt_disable() and preempt_enable(). SHA256 takes ~2,00016* operations per 64 bytes. e500 cores can issue two arithmetic instructions17* per clock cycle using one 32/64 bit unit (SU1) and one 32 bit unit (SU2).18* Thus 1KB of input data will need an estimated maximum of 18,000 cycles.19* Headroom for cache misses included. Even with the low end model clocked20* at 667 MHz this equals to a critical time window of less than 27us.21*22*/23#define MAX_BYTES 10242425extern void ppc_spe_sha256_transform(struct sha256_block_state *state,26const u8 *src, u32 blocks);2728static void spe_begin(void)29{30/* We just start SPE operations and will save SPE registers later. */31preempt_disable();32enable_kernel_spe();33}3435static void spe_end(void)36{37disable_kernel_spe();38/* reenable preemption */39preempt_enable();40}4142static void sha256_blocks(struct sha256_block_state *state,43const u8 *data, size_t nblocks)44{45do {46/* cut input data into smaller blocks */47u32 unit = min_t(size_t, nblocks,48MAX_BYTES / SHA256_BLOCK_SIZE);4950spe_begin();51ppc_spe_sha256_transform(state, data, unit);52spe_end();5354data += unit * SHA256_BLOCK_SIZE;55nblocks -= unit;56} while (nblocks);57}585960