Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <ardb@kernel.org>
Link: https://lore.kernel.org/r/20241202010844.144356-11-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>

+97 -199
+1
arch/sparc/Kconfig
··· 110 110 select HAVE_SETUP_PER_CPU_AREA 111 111 select NEED_PER_CPU_EMBED_FIRST_CHUNK 112 112 select NEED_PER_CPU_PAGE_FIRST_CHUNK 113 + select ARCH_HAS_CRC32 113 114 114 115 config ARCH_PROC_KCORE_TEXT 115 116 def_bool y
-10
arch/sparc/crypto/Kconfig
··· 16 16 17 17 Architecture: sparc64 18 18 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 - 29 19 config CRYPTO_MD5_SPARC64 30 20 tristate "Digests: MD5" 31 21 depends on SPARC64
-4
arch/sparc/crypto/Makefile
··· 12 12 obj-$(CONFIG_CRYPTO_DES_SPARC64) += des-sparc64.o 13 13 obj-$(CONFIG_CRYPTO_CAMELLIA_SPARC64) += camellia-sparc64.o 14 14 15 - obj-$(CONFIG_CRYPTO_CRC32C_SPARC64) += crc32c-sparc64.o 16 - 17 15 sha1-sparc64-y := sha1_asm.o sha1_glue.o 18 16 sha256-sparc64-y := sha256_asm.o sha256_glue.o 19 17 sha512-sparc64-y := sha512_asm.o sha512_glue.o ··· 20 22 aes-sparc64-y := aes_asm.o aes_glue.o 21 23 des-sparc64-y := des_asm.o des_glue.o 22 24 camellia-sparc64-y := camellia_asm.o camellia_glue.o 23 - 24 - crc32c-sparc64-y := crc32c_asm.o crc32c_glue.o
+1 -1
arch/sparc/crypto/crc32c_asm.S arch/sparc/lib/crc32c_asm.S
··· 3 3 #include <asm/visasm.h> 4 4 #include <asm/asi.h> 5 5 6 - #include "opcodes.h" 6 + #include "../crypto/opcodes.h" 7 7 8 8 ENTRY(crc32c_sparc64) 9 9 /* %o0=crc32p, %o1=data_ptr, %o2=len */
-184
arch/sparc/crypto/crc32c_glue.c
··· 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 <austin_zhang@linux.intel.com> 8 - * Kent Liu <kent.liu@intel.com> 9 - */ 10 - 11 - #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 - 13 - #include <linux/init.h> 14 - #include <linux/module.h> 15 - #include <linux/string.h> 16 - #include <linux/kernel.h> 17 - #include <linux/crc32.h> 18 - 19 - #include <crypto/internal/hash.h> 20 - 21 - #include <asm/pstate.h> 22 - #include <asm/elf.h> 23 - #include <linux/unaligned.h> 24 - 25 - #include "opcodes.h" 26 - 27 - /* 28 - * Setting the seed allows arbitrary accumulators and flexible XOR policy 29 - * If your algorithm starts with ~0, then XOR with ~0 before you set 30 - * the seed. 31 - */ 32 - static int crc32c_sparc64_setkey(struct crypto_shash *hash, const u8 *key, 33 - unsigned int keylen) 34 - { 35 - u32 *mctx = crypto_shash_ctx(hash); 36 - 37 - if (keylen != sizeof(u32)) 38 - return -EINVAL; 39 - *mctx = get_unaligned_le32(key); 40 - return 0; 41 - } 42 - 43 - static int crc32c_sparc64_init(struct shash_desc *desc) 44 - { 45 - u32 *mctx = crypto_shash_ctx(desc->tfm); 46 - u32 *crcp = shash_desc_ctx(desc); 47 - 48 - *crcp = *mctx; 49 - 50 - return 0; 51 - } 52 - 53 - extern void crc32c_sparc64(u32 *crcp, const u64 *data, unsigned int len); 54 - 55 - static u32 crc32c_compute(u32 crc, const u8 *data, unsigned int len) 56 - { 57 - unsigned int n = -(uintptr_t)data & 7; 58 - 59 - if (n) { 60 - /* Data isn't 8-byte aligned. Align it. */ 61 - n = min(n, len); 62 - crc = __crc32c_le(crc, data, n); 63 - data += n; 64 - len -= n; 65 - } 66 - n = len & ~7U; 67 - if (n) { 68 - crc32c_sparc64(&crc, (const u64 *)data, n); 69 - data += n; 70 - len -= n; 71 - } 72 - if (len) 73 - crc = __crc32c_le(crc, data, len); 74 - return crc; 75 - } 76 - 77 - static int crc32c_sparc64_update(struct shash_desc *desc, const u8 *data, 78 - unsigned int len) 79 - { 80 - u32 *crcp = shash_desc_ctx(desc); 81 - 82 - *crcp = crc32c_compute(*crcp, data, len); 83 - return 0; 84 - } 85 - 86 - static int __crc32c_sparc64_finup(const u32 *crcp, const u8 *data, 87 - unsigned int len, u8 *out) 88 - { 89 - put_unaligned_le32(~crc32c_compute(*crcp, data, len), out); 90 - return 0; 91 - } 92 - 93 - static int crc32c_sparc64_finup(struct shash_desc *desc, const u8 *data, 94 - unsigned int len, u8 *out) 95 - { 96 - return __crc32c_sparc64_finup(shash_desc_ctx(desc), data, len, out); 97 - } 98 - 99 - static int crc32c_sparc64_final(struct shash_desc *desc, u8 *out) 100 - { 101 - u32 *crcp = shash_desc_ctx(desc); 102 - 103 - put_unaligned_le32(~*crcp, out); 104 - return 0; 105 - } 106 - 107 - static int crc32c_sparc64_digest(struct shash_desc *desc, const u8 *data, 108 - unsigned int len, u8 *out) 109 - { 110 - return __crc32c_sparc64_finup(crypto_shash_ctx(desc->tfm), data, len, 111 - out); 112 - } 113 - 114 - static int crc32c_sparc64_cra_init(struct crypto_tfm *tfm) 115 - { 116 - u32 *key = crypto_tfm_ctx(tfm); 117 - 118 - *key = ~0; 119 - 120 - return 0; 121 - } 122 - 123 - #define CHKSUM_BLOCK_SIZE 1 124 - #define CHKSUM_DIGEST_SIZE 4 125 - 126 - static struct shash_alg alg = { 127 - .setkey = crc32c_sparc64_setkey, 128 - .init = crc32c_sparc64_init, 129 - .update = crc32c_sparc64_update, 130 - .final = crc32c_sparc64_final, 131 - .finup = crc32c_sparc64_finup, 132 - .digest = crc32c_sparc64_digest, 133 - .descsize = sizeof(u32), 134 - .digestsize = CHKSUM_DIGEST_SIZE, 135 - .base = { 136 - .cra_name = "crc32c", 137 - .cra_driver_name = "crc32c-sparc64", 138 - .cra_priority = SPARC_CR_OPCODE_PRIORITY, 139 - .cra_flags = CRYPTO_ALG_OPTIONAL_KEY, 140 - .cra_blocksize = CHKSUM_BLOCK_SIZE, 141 - .cra_ctxsize = sizeof(u32), 142 - .cra_module = THIS_MODULE, 143 - .cra_init = crc32c_sparc64_cra_init, 144 - } 145 - }; 146 - 147 - static bool __init sparc64_has_crc32c_opcode(void) 148 - { 149 - unsigned long cfr; 150 - 151 - if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO)) 152 - return false; 153 - 154 - __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr)); 155 - if (!(cfr & CFR_CRC32C)) 156 - return false; 157 - 158 - return true; 159 - } 160 - 161 - static int __init crc32c_sparc64_mod_init(void) 162 - { 163 - if (sparc64_has_crc32c_opcode()) { 164 - pr_info("Using sparc64 crc32c opcode optimized CRC32C implementation\n"); 165 - return crypto_register_shash(&alg); 166 - } 167 - pr_info("sparc64 crc32c opcode not available.\n"); 168 - return -ENODEV; 169 - } 170 - 171 - static void __exit crc32c_sparc64_mod_fini(void) 172 - { 173 - crypto_unregister_shash(&alg); 174 - } 175 - 176 - module_init(crc32c_sparc64_mod_init); 177 - module_exit(crc32c_sparc64_mod_fini); 178 - 179 - MODULE_LICENSE("GPL"); 180 - MODULE_DESCRIPTION("CRC32c (Castagnoli), sparc64 crc32c opcode accelerated"); 181 - 182 - MODULE_ALIAS_CRYPTO("crc32c"); 183 - 184 - #include "crop_devid.c"
+2
arch/sparc/lib/Makefile
··· 53 53 obj-$(CONFIG_SPARC64) += iomap.o 54 54 obj-$(CONFIG_SPARC32) += atomic32.o 55 55 obj-$(CONFIG_SPARC64) += PeeCeeI.o 56 + obj-$(CONFIG_CRC32_ARCH) += crc32-sparc.o 57 + crc32-sparc-y := crc32_glue.o crc32c_asm.o
+93
arch/sparc/lib/crc32_glue.c
··· 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 <austin_zhang@linux.intel.com> 8 + * Kent Liu <kent.liu@intel.com> 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");