Skip to content

Commit

Permalink
loongarch/crc32: expose CRC32 functions through lib
Browse files Browse the repository at this point in the history
Move the loongarch CRC32 assembly code into the lib directory and wire
it up to the library interface.  This allows it to be used without going
through the crypto API.  It remains usable via the crypto API too via
the shash algorithms that use the library interface.  Thus all the
arch-specific "shash" code becomes unnecessary and is removed.

Note: to see the diff from arch/loongarch/crypto/crc32-loongarch.c to
arch/loongarch/lib/crc32-loongarch.c, view this commit with
'git show -M10'.

Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: WangYuli <wangyuli@uniontech.com>
Link: https://lore.kernel.org/r/20241202010844.144356-7-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
Eric Biggers committed Dec 2, 2024

Unverified

No user is associated with the committer email.
1 parent 1e1b6db commit 72f51a4
Showing 7 changed files with 138 additions and 312 deletions.
1 change: 1 addition & 0 deletions arch/loongarch/Kconfig
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ config LOONGARCH
select ARCH_ENABLE_THP_MIGRATION if TRANSPARENT_HUGEPAGE
select ARCH_HAS_ACPI_TABLE_UPGRADE if ACPI
select ARCH_HAS_CPU_FINALIZE_INIT
select ARCH_HAS_CRC32
select ARCH_HAS_CURRENT_STACK_POINTER
select ARCH_HAS_DEBUG_VM_PGTABLE
select ARCH_HAS_FAST_MULTIPLIER
1 change: 0 additions & 1 deletion arch/loongarch/configs/loongson3_defconfig
Original file line number Diff line number Diff line change
@@ -1040,7 +1040,6 @@ CONFIG_CRYPTO_USER_API_HASH=m
CONFIG_CRYPTO_USER_API_SKCIPHER=m
CONFIG_CRYPTO_USER_API_RNG=m
CONFIG_CRYPTO_USER_API_AEAD=m
CONFIG_CRYPTO_CRC32_LOONGARCH=m
CONFIG_CRYPTO_DEV_VIRTIO=m
CONFIG_DMA_CMA=y
CONFIG_DMA_NUMA_CMA=y
9 changes: 0 additions & 9 deletions arch/loongarch/crypto/Kconfig
Original file line number Diff line number Diff line change
@@ -2,13 +2,4 @@

menu "Accelerated Cryptographic Algorithms for CPU (loongarch)"

config CRYPTO_CRC32_LOONGARCH
tristate "CRC32c and CRC32"
select CRC32
select CRYPTO_HASH
help
CRC32c and CRC32 CRC algorithms

Architecture: LoongArch with CRC32 instructions

endmenu
2 changes: 0 additions & 2 deletions arch/loongarch/crypto/Makefile
Original file line number Diff line number Diff line change
@@ -2,5 +2,3 @@
#
# Makefile for LoongArch crypto files..
#

obj-$(CONFIG_CRYPTO_CRC32_LOONGARCH) += crc32-loongarch.o
300 changes: 0 additions & 300 deletions arch/loongarch/crypto/crc32-loongarch.c

This file was deleted.

2 changes: 2 additions & 0 deletions arch/loongarch/lib/Makefile
Original file line number Diff line number Diff line change
@@ -11,3 +11,5 @@ obj-$(CONFIG_ARCH_SUPPORTS_INT128) += tishift.o
obj-$(CONFIG_CPU_HAS_LSX) += xor_simd.o xor_simd_glue.o

obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o

obj-$(CONFIG_CRC32_ARCH) += crc32-loongarch.o
135 changes: 135 additions & 0 deletions arch/loongarch/lib/crc32-loongarch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// SPDX-License-Identifier: GPL-2.0
/*
* CRC32 and CRC32C using LoongArch crc* instructions
*
* Module based on mips/crypto/crc32-mips.c
*
* Copyright (C) 2014 Linaro Ltd <yazen.ghannam@linaro.org>
* Copyright (C) 2018 MIPS Tech, LLC
* Copyright (C) 2020-2023 Loongson Technology Corporation Limited
*/

#include <asm/cpu-features.h>
#include <linux/crc32.h>
#include <linux/module.h>
#include <linux/unaligned.h>

#define _CRC32(crc, value, size, type) \
do { \
__asm__ __volatile__( \
#type ".w." #size ".w" " %0, %1, %0\n\t"\
: "+r" (crc) \
: "r" (value) \
: "memory"); \
} while (0)

#define CRC32(crc, value, size) _CRC32(crc, value, size, crc)
#define CRC32C(crc, value, size) _CRC32(crc, value, size, crcc)

static DEFINE_STATIC_KEY_FALSE(have_crc32);

u32 crc32_le_arch(u32 crc, const u8 *p, size_t len)
{
if (!static_branch_likely(&have_crc32))
return crc32_le_base(crc, p, len);

while (len >= sizeof(u64)) {
u64 value = get_unaligned_le64(p);

CRC32(crc, value, d);
p += sizeof(u64);
len -= sizeof(u64);
}

if (len & sizeof(u32)) {
u32 value = get_unaligned_le32(p);

CRC32(crc, value, w);
p += sizeof(u32);
}

if (len & sizeof(u16)) {
u16 value = get_unaligned_le16(p);

CRC32(crc, value, h);
p += sizeof(u16);
}

if (len & sizeof(u8)) {
u8 value = *p++;

CRC32(crc, value, b);
}

return crc;
}
EXPORT_SYMBOL(crc32_le_arch);

u32 crc32c_le_arch(u32 crc, const u8 *p, size_t len)
{
if (!static_branch_likely(&have_crc32))
return crc32c_le_base(crc, p, len);

while (len >= sizeof(u64)) {
u64 value = get_unaligned_le64(p);

CRC32C(crc, value, d);
p += sizeof(u64);
len -= sizeof(u64);
}

if (len & sizeof(u32)) {
u32 value = get_unaligned_le32(p);

CRC32C(crc, value, w);
p += sizeof(u32);
}

if (len & sizeof(u16)) {
u16 value = get_unaligned_le16(p);

CRC32C(crc, value, h);
p += sizeof(u16);
}

if (len & sizeof(u8)) {
u8 value = *p++;

CRC32C(crc, value, b);
}

return crc;
}
EXPORT_SYMBOL(crc32c_le_arch);

u32 crc32_be_arch(u32 crc, const u8 *p, size_t len)
{
return crc32_be_base(crc, p, len);
}
EXPORT_SYMBOL(crc32_be_arch);

static int __init crc32_loongarch_init(void)
{
if (cpu_has_crc32)
static_branch_enable(&have_crc32);
return 0;
}
arch_initcall(crc32_loongarch_init);

static void __exit crc32_loongarch_exit(void)
{
}
module_exit(crc32_loongarch_exit);

u32 crc32_optimizations(void)
{
if (static_key_enabled(&have_crc32))
return CRC32_LE_OPTIMIZATION | CRC32C_OPTIMIZATION;
return 0;
}
EXPORT_SYMBOL(crc32_optimizations);

MODULE_AUTHOR("Min Zhou <zhoumin@loongson.cn>");
MODULE_AUTHOR("Huacai Chen <chenhuacai@loongson.cn>");
MODULE_DESCRIPTION("CRC32 and CRC32C using LoongArch crc* instructions");
MODULE_LICENSE("GPL v2");

0 comments on commit 72f51a4

Please sign in to comment.