Skip to content

Commit bbf2cb1

Browse files
toromanoSTMherbertx
authored andcommitted
crypto: stm32/crc32 - Avoid lock if hardware is already used
If STM32 CRC device is already in use, calculate CRC by software. This will release CPU constraint for a concurrent access to the hardware, and avoid masking irqs during the whole block processing. Fixes: 7795c0b ("crypto: stm32/crc32 - protect from concurrent accesses") Signed-off-by: Nicolas Toromanoff <nicolas.toromanoff@st.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent c4c75fc commit bbf2cb1

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

‎drivers/crypto/stm32/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ config CRYPTO_DEV_STM32_CRC
33
tristate "Support for STM32 crc accelerators"
44
depends on ARCH_STM32
55
select CRYPTO_HASH
6+
select CRC32
67
help
78
This enables support for the CRC32 hw accelerator which can be found
89
on STMicroelectronics STM32 SOC.

‎drivers/crypto/stm32/stm32-crc32.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <linux/bitrev.h>
88
#include <linux/clk.h>
9+
#include <linux/crc32.h>
910
#include <linux/crc32poly.h>
1011
#include <linux/io.h>
1112
#include <linux/kernel.h>
@@ -149,15 +150,22 @@ static int burst_update(struct shash_desc *desc, const u8 *d8,
149150
struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
150151
struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
151152
struct stm32_crc *crc;
152-
unsigned long flags;
153153

154154
crc = stm32_crc_get_next_crc();
155155
if (!crc)
156156
return -ENODEV;
157157

158158
pm_runtime_get_sync(crc->dev);
159159

160-
spin_lock_irqsave(&crc->lock, flags);
160+
if (!spin_trylock(&crc->lock)) {
161+
/* Hardware is busy, calculate crc32 by software */
162+
if (mctx->poly == CRC32_POLY_LE)
163+
ctx->partial = crc32_le(ctx->partial, d8, length);
164+
else
165+
ctx->partial = __crc32c_le(ctx->partial, d8, length);
166+
167+
goto pm_out;
168+
}
161169

162170
/*
163171
* Restore previously calculated CRC for this context as init value
@@ -197,8 +205,9 @@ static int burst_update(struct shash_desc *desc, const u8 *d8,
197205
/* Store partial result */
198206
ctx->partial = readl_relaxed(crc->regs + CRC_DR);
199207

200-
spin_unlock_irqrestore(&crc->lock, flags);
208+
spin_unlock(&crc->lock);
201209

210+
pm_out:
202211
pm_runtime_mark_last_busy(crc->dev);
203212
pm_runtime_put_autosuspend(crc->dev);
204213

0 commit comments

Comments
 (0)