WIP FPC-III support
[linux/fpc-iii.git] / arch / powerpc / crypto / crct10dif-vpmsum_glue.c
blob1dc8b691517811503fb91528c692f16f3dd6000c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Calculate a CRC T10-DIF with vpmsum acceleration
5 * Copyright 2017, Daniel Axtens, IBM Corporation.
6 * [based on crc32c-vpmsum_glue.c]
7 */
9 #include <linux/crc-t10dif.h>
10 #include <crypto/internal/hash.h>
11 #include <crypto/internal/simd.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/string.h>
15 #include <linux/kernel.h>
16 #include <linux/cpufeature.h>
17 #include <asm/simd.h>
18 #include <asm/switch_to.h>
20 #define VMX_ALIGN 16
21 #define VMX_ALIGN_MASK (VMX_ALIGN-1)
23 #define VECTOR_BREAKPOINT 64
25 u32 __crct10dif_vpmsum(u32 crc, unsigned char const *p, size_t len);
27 static u16 crct10dif_vpmsum(u16 crci, unsigned char const *p, size_t len)
29 unsigned int prealign;
30 unsigned int tail;
31 u32 crc = crci;
33 if (len < (VECTOR_BREAKPOINT + VMX_ALIGN) || !crypto_simd_usable())
34 return crc_t10dif_generic(crc, p, len);
36 if ((unsigned long)p & VMX_ALIGN_MASK) {
37 prealign = VMX_ALIGN - ((unsigned long)p & VMX_ALIGN_MASK);
38 crc = crc_t10dif_generic(crc, p, prealign);
39 len -= prealign;
40 p += prealign;
43 if (len & ~VMX_ALIGN_MASK) {
44 crc <<= 16;
45 preempt_disable();
46 pagefault_disable();
47 enable_kernel_altivec();
48 crc = __crct10dif_vpmsum(crc, p, len & ~VMX_ALIGN_MASK);
49 disable_kernel_altivec();
50 pagefault_enable();
51 preempt_enable();
52 crc >>= 16;
55 tail = len & VMX_ALIGN_MASK;
56 if (tail) {
57 p += len & ~VMX_ALIGN_MASK;
58 crc = crc_t10dif_generic(crc, p, tail);
61 return crc & 0xffff;
64 static int crct10dif_vpmsum_init(struct shash_desc *desc)
66 u16 *crc = shash_desc_ctx(desc);
68 *crc = 0;
69 return 0;
72 static int crct10dif_vpmsum_update(struct shash_desc *desc, const u8 *data,
73 unsigned int length)
75 u16 *crc = shash_desc_ctx(desc);
77 *crc = crct10dif_vpmsum(*crc, data, length);
79 return 0;
83 static int crct10dif_vpmsum_final(struct shash_desc *desc, u8 *out)
85 u16 *crcp = shash_desc_ctx(desc);
87 *(u16 *)out = *crcp;
88 return 0;
91 static struct shash_alg alg = {
92 .init = crct10dif_vpmsum_init,
93 .update = crct10dif_vpmsum_update,
94 .final = crct10dif_vpmsum_final,
95 .descsize = CRC_T10DIF_DIGEST_SIZE,
96 .digestsize = CRC_T10DIF_DIGEST_SIZE,
97 .base = {
98 .cra_name = "crct10dif",
99 .cra_driver_name = "crct10dif-vpmsum",
100 .cra_priority = 200,
101 .cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
102 .cra_module = THIS_MODULE,
106 static int __init crct10dif_vpmsum_mod_init(void)
108 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
109 return -ENODEV;
111 return crypto_register_shash(&alg);
114 static void __exit crct10dif_vpmsum_mod_fini(void)
116 crypto_unregister_shash(&alg);
119 module_cpu_feature_match(PPC_MODULE_FEATURE_VEC_CRYPTO, crct10dif_vpmsum_mod_init);
120 module_exit(crct10dif_vpmsum_mod_fini);
122 MODULE_AUTHOR("Daniel Axtens <dja@axtens.net>");
123 MODULE_DESCRIPTION("CRCT10DIF using vector polynomial multiply-sum instructions");
124 MODULE_LICENSE("GPL");
125 MODULE_ALIAS_CRYPTO("crct10dif");
126 MODULE_ALIAS_CRYPTO("crct10dif-vpmsum");