Skip to content

Commit 0f60a8a

Browse files
committed
sparc/crc32: expose CRC32 functions through lib
Move the sparc CRC32C 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/sparc/crypto/crc32c_glue.c to arch/sparc/lib/crc32_glue.c, view this commit with 'git show -M10'. Reviewed-by: Ard Biesheuvel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]>
1 parent 0080719 commit 0f60a8a

File tree

7 files changed

+97
-199
lines changed

7 files changed

+97
-199
lines changed

arch/sparc/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ config SPARC64
110110
select HAVE_SETUP_PER_CPU_AREA
111111
select NEED_PER_CPU_EMBED_FIRST_CHUNK
112112
select NEED_PER_CPU_PAGE_FIRST_CHUNK
113+
select ARCH_HAS_CRC32
113114

114115
config ARCH_PROC_KCORE_TEXT
115116
def_bool y

arch/sparc/crypto/Kconfig

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ config CRYPTO_DES_SPARC64
1616

1717
Architecture: sparc64
1818

19-
config CRYPTO_CRC32C_SPARC64
20-
tristate "CRC32c"
21-
depends on SPARC64
22-
select CRYPTO_HASH
23-
select CRC32
24-
help
25-
CRC32c CRC algorithm with the iSCSI polynomial (RFC 3385 and RFC 3720)
26-
27-
Architecture: sparc64
28-
2919
config CRYPTO_MD5_SPARC64
3020
tristate "Digests: MD5"
3121
depends on SPARC64

arch/sparc/crypto/Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ obj-$(CONFIG_CRYPTO_AES_SPARC64) += aes-sparc64.o
1212
obj-$(CONFIG_CRYPTO_DES_SPARC64) += des-sparc64.o
1313
obj-$(CONFIG_CRYPTO_CAMELLIA_SPARC64) += camellia-sparc64.o
1414

15-
obj-$(CONFIG_CRYPTO_CRC32C_SPARC64) += crc32c-sparc64.o
16-
1715
sha1-sparc64-y := sha1_asm.o sha1_glue.o
1816
sha256-sparc64-y := sha256_asm.o sha256_glue.o
1917
sha512-sparc64-y := sha512_asm.o sha512_glue.o
@@ -22,5 +20,3 @@ md5-sparc64-y := md5_asm.o md5_glue.o
2220
aes-sparc64-y := aes_asm.o aes_glue.o
2321
des-sparc64-y := des_asm.o des_glue.o
2422
camellia-sparc64-y := camellia_asm.o camellia_glue.o
25-
26-
crc32c-sparc64-y := crc32c_asm.o crc32c_glue.o

arch/sparc/crypto/crc32c_glue.c

Lines changed: 0 additions & 184 deletions
This file was deleted.

arch/sparc/lib/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ lib-$(CONFIG_SPARC64) += mcount.o ipcsum.o xor.o hweight.o ffs.o
5353
obj-$(CONFIG_SPARC64) += iomap.o
5454
obj-$(CONFIG_SPARC32) += atomic32.o
5555
obj-$(CONFIG_SPARC64) += PeeCeeI.o
56+
obj-$(CONFIG_CRC32_ARCH) += crc32-sparc.o
57+
crc32-sparc-y := crc32_glue.o crc32c_asm.o

arch/sparc/lib/crc32_glue.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/* Glue code for CRC32C optimized for sparc64 crypto opcodes.
3+
*
4+
* This is based largely upon arch/x86/crypto/crc32c-intel.c
5+
*
6+
* Copyright (C) 2008 Intel Corporation
7+
* Authors: Austin Zhang <[email protected]>
8+
* Kent Liu <[email protected]>
9+
*/
10+
11+
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12+
13+
#include <linux/init.h>
14+
#include <linux/module.h>
15+
#include <linux/kernel.h>
16+
#include <linux/crc32.h>
17+
#include <asm/pstate.h>
18+
#include <asm/elf.h>
19+
20+
static DEFINE_STATIC_KEY_FALSE(have_crc32c_opcode);
21+
22+
u32 crc32_le_arch(u32 crc, const u8 *data, size_t len)
23+
{
24+
return crc32_le_base(crc, data, len);
25+
}
26+
EXPORT_SYMBOL(crc32_le_arch);
27+
28+
void crc32c_sparc64(u32 *crcp, const u64 *data, size_t len);
29+
30+
u32 crc32c_le_arch(u32 crc, const u8 *data, size_t len)
31+
{
32+
size_t n = -(uintptr_t)data & 7;
33+
34+
if (!static_branch_likely(&have_crc32c_opcode))
35+
return crc32c_le_base(crc, data, len);
36+
37+
if (n) {
38+
/* Data isn't 8-byte aligned. Align it. */
39+
n = min(n, len);
40+
crc = crc32c_le_base(crc, data, n);
41+
data += n;
42+
len -= n;
43+
}
44+
n = len & ~7U;
45+
if (n) {
46+
crc32c_sparc64(&crc, (const u64 *)data, n);
47+
data += n;
48+
len -= n;
49+
}
50+
if (len)
51+
crc = crc32c_le_base(crc, data, len);
52+
return crc;
53+
}
54+
EXPORT_SYMBOL(crc32c_le_arch);
55+
56+
u32 crc32_be_arch(u32 crc, const u8 *data, size_t len)
57+
{
58+
return crc32_be_base(crc, data, len);
59+
}
60+
EXPORT_SYMBOL(crc32_be_arch);
61+
62+
static int __init crc32_sparc_init(void)
63+
{
64+
unsigned long cfr;
65+
66+
if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
67+
return 0;
68+
69+
__asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
70+
if (!(cfr & CFR_CRC32C))
71+
return 0;
72+
73+
static_branch_enable(&have_crc32c_opcode);
74+
pr_info("Using sparc64 crc32c opcode optimized CRC32C implementation\n");
75+
return 0;
76+
}
77+
arch_initcall(crc32_sparc_init);
78+
79+
static void __exit crc32_sparc_exit(void)
80+
{
81+
}
82+
module_exit(crc32_sparc_exit);
83+
84+
u32 crc32_optimizations(void)
85+
{
86+
if (static_key_enabled(&have_crc32c_opcode))
87+
return CRC32C_OPTIMIZATION;
88+
return 0;
89+
}
90+
EXPORT_SYMBOL(crc32_optimizations);
91+
92+
MODULE_LICENSE("GPL");
93+
MODULE_DESCRIPTION("CRC32c (Castagnoli), sparc64 crc32c opcode accelerated");

arch/sparc/crypto/crc32c_asm.S renamed to arch/sparc/lib/crc32c_asm.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <asm/visasm.h>
44
#include <asm/asi.h>
55

6-
#include "opcodes.h"
6+
#include "../crypto/opcodes.h"
77

88
ENTRY(crc32c_sparc64)
99
/* %o0=crc32p, %o1=data_ptr, %o2=len */

0 commit comments

Comments
 (0)