1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Copyright (C) 2023 Rivos Inc.
7 #ifndef __ASM_RISCV_CHECKSUM_H
8 #define __ASM_RISCV_CHECKSUM_H
10 #include <linux/in6.h>
11 #include <linux/uaccess.h>
13 #define ip_fast_csum ip_fast_csum
15 extern unsigned int do_csum(const unsigned char *buff
, int len
);
16 #define do_csum do_csum
18 /* Default version is sufficient for 32 bit */
20 #define _HAVE_ARCH_IPV6_CSUM
21 __sum16
csum_ipv6_magic(const struct in6_addr
*saddr
,
22 const struct in6_addr
*daddr
,
23 __u32 len
, __u8 proto
, __wsum sum
);
26 /* Define riscv versions of functions before importing asm-generic/checksum.h */
27 #include <asm-generic/checksum.h>
30 * Quickly compute an IP checksum with the assumption that IPv4 headers will
31 * always be in multiples of 32-bits, and have an ihl of at least 5.
33 * @ihl: the number of 32 bit segments and must be greater than or equal to 5.
34 * @iph: assumed to be word aligned given that NET_IP_ALIGN is set to 2 on
35 * riscv, defining IP headers to be aligned.
37 static inline __sum16
ip_fast_csum(const void *iph
, unsigned int ihl
)
39 unsigned long csum
= 0;
43 csum
+= ((const unsigned int *)iph
)[pos
];
44 if (IS_ENABLED(CONFIG_32BIT
))
45 csum
+= csum
< ((const unsigned int *)iph
)[pos
];
46 } while (++pos
< ihl
);
49 * ZBB only saves three instructions on 32-bit and five on 64-bit so not
50 * worth checking if supported without Alternatives.
52 if (IS_ENABLED(CONFIG_RISCV_ISA_ZBB
) &&
53 IS_ENABLED(CONFIG_RISCV_ALTERNATIVE
)) {
54 unsigned long fold_temp
;
56 asm goto(ALTERNATIVE("j %l[no_zbb]", "nop", 0,
63 if (IS_ENABLED(CONFIG_32BIT
)) {
66 not %[fold_temp], %[csum] \n\
67 rori %[csum], %[csum], 16 \n\
68 sub %[csum], %[fold_temp], %[csum] \n\
70 : [csum
] "+r" (csum
), [fold_temp
] "=&r" (fold_temp
));
74 rori %[fold_temp], %[csum], 32 \n\
75 add %[csum], %[fold_temp], %[csum] \n\
76 srli %[csum], %[csum], 32 \n\
77 not %[fold_temp], %[csum] \n\
78 roriw %[csum], %[csum], 16 \n\
79 subw %[csum], %[fold_temp], %[csum] \n\
81 : [csum
] "+r" (csum
), [fold_temp
] "=&r" (fold_temp
));
83 return (__force __sum16
)(csum
>> 16);
87 csum
+= ror64(csum
, 32);
90 return csum_fold((__force __wsum
)csum
);
93 #endif /* __ASM_RISCV_CHECKSUM_H */