1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * This file contains assembly-language implementations
4 * of IP-style 1's complement checksum routines.
6 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
8 * Severely hacked about by Paul Mackerras (paulus@cs.anu.edu.au).
11 #include <linux/sys.h>
12 #include <asm/processor.h>
13 #include <asm/errno.h>
14 #include <asm/ppc_asm.h>
15 #include <asm/export.h>
18 * Computes the checksum of a memory block at buff, length len,
19 * and adds in "sum" (32-bit).
21 * __csum_partial(r3=buff, r4=len, r5=sum)
23 _GLOBAL(__csum_partial)
24 addic r0,r5,0 /* clear carry */
26 srdi. r6,r4,3 /* less than 8 bytes? */
30 * If only halfword aligned, align to a double word. Since odd
31 * aligned addresses should be rare and they would require more
32 * work to calculate the correct checksum, we ignore that case
33 * and take the potential slowdown of unaligned loads.
35 rldicl. r6,r3,64-1,64-2 /* r6 = (r3 >> 1) & 0x3 */
43 lhz r6,0(r3) /* align to doubleword */
51 * We unroll the loop such that each iteration is 64 bytes with an
52 * entry and exit limb of 64 bytes, meaning a minimum size of
56 beq .Lcsum_tail_doublewords /* len < 128 */
62 stdu r1,-STACKFRAMESIZE(r1)
63 std r14,STK_REG(R14)(r1)
64 std r15,STK_REG(R15)(r1)
65 std r16,STK_REG(R16)(r1)
74 * On POWER6 and POWER7 back to back adde instructions take 2 cycles
75 * because of the XER dependency. This means the fastest this loop can
76 * go is 16 cycles per iteration. The scheduling of the loop below has
77 * been shown to hit this on both POWER6 and POWER7.
124 ld r14,STK_REG(R14)(r1)
125 ld r15,STK_REG(R15)(r1)
126 ld r16,STK_REG(R16)(r1)
127 addi r1,r1,STACKFRAMESIZE
131 .Lcsum_tail_doublewords: /* Up to 127 bytes to go */
144 .Lcsum_tail_word: /* Up to 7 bytes to go */
146 beq .Lcsum_tail_halfword
153 .Lcsum_tail_halfword: /* Up to 3 bytes to go */
162 .Lcsum_tail_byte: /* Up to 1 byte to go */
167 #ifdef __BIG_ENDIAN__
168 sldi r9,r6,8 /* Pad the byte out to 16 bits */
175 addze r0,r0 /* add in final carry */
176 rldicl r4,r0,32,0 /* fold two 32 bit halves together */
180 EXPORT_SYMBOL(__csum_partial)
185 EX_TABLE(100b,.Lsrc_error_nr)
190 EX_TABLE(150b,.Lsrc_error)
195 EX_TABLE(200b,.Ldest_error_nr)
200 EX_TABLE(250b,.Ldest_error)
204 * Computes the checksum of a memory block at src, length len,
205 * and adds in "sum" (32-bit), while copying the block to dst.
206 * If an access exception occurs on src or dst, it stores -EFAULT
207 * to *src_err or *dst_err respectively. The caller must take any action
208 * required in this case (zeroing memory, recalculating partial checksum etc).
210 * csum_partial_copy_generic(r3=src, r4=dst, r5=len, r6=sum, r7=src_err, r8=dst_err)
212 _GLOBAL(csum_partial_copy_generic)
213 addic r0,r6,0 /* clear carry */
215 srdi. r6,r5,3 /* less than 8 bytes? */
219 * If only halfword aligned, align to a double word. Since odd
220 * aligned addresses should be rare and they would require more
221 * work to calculate the correct checksum, we ignore that case
222 * and take the potential slowdown of unaligned loads.
224 * If the source and destination are relatively unaligned we only
225 * align the source. This keeps things simple.
227 rldicl. r6,r3,64-1,64-2 /* r6 = (r3 >> 1) & 0x3 */
235 srcnr; lhz r6,0(r3) /* align to doubleword */
245 * We unroll the loop such that each iteration is 64 bytes with an
246 * entry and exit limb of 64 bytes, meaning a minimum size of
250 beq .Lcopy_tail_doublewords /* len < 128 */
256 stdu r1,-STACKFRAMESIZE(r1)
257 std r14,STK_REG(R14)(r1)
258 std r15,STK_REG(R15)(r1)
259 std r16,STK_REG(R16)(r1)
264 source; ld r10,16(r3)
265 source; ld r11,24(r3)
268 * On POWER6 and POWER7 back to back adde instructions take 2 cycles
269 * because of the XER dependency. This means the fastest this loop can
270 * go is 16 cycles per iteration. The scheduling of the loop below has
271 * been shown to hit this on both POWER6 and POWER7.
276 source; ld r12,32(r3)
277 source; ld r14,40(r3)
280 source; ld r15,48(r3)
281 source; ld r16,56(r3)
306 source; ld r10,16(r3)
307 source; ld r11,24(r3)
312 source; ld r12,32(r3)
313 source; ld r14,40(r3)
316 source; ld r15,48(r3)
317 source; ld r16,56(r3)
340 ld r14,STK_REG(R14)(r1)
341 ld r15,STK_REG(R15)(r1)
342 ld r16,STK_REG(R16)(r1)
343 addi r1,r1,STACKFRAMESIZE
347 .Lcopy_tail_doublewords: /* Up to 127 bytes to go */
362 .Lcopy_tail_word: /* Up to 7 bytes to go */
364 beq .Lcopy_tail_halfword
373 .Lcopy_tail_halfword: /* Up to 3 bytes to go */
384 .Lcopy_tail_byte: /* Up to 1 byte to go */
389 #ifdef __BIG_ENDIAN__
390 sldi r9,r6,8 /* Pad the byte out to 16 bits */
398 addze r0,r0 /* add in final carry */
399 rldicl r4,r0,32,0 /* fold two 32 bit halves together */
405 ld r14,STK_REG(R14)(r1)
406 ld r15,STK_REG(R15)(r1)
407 ld r16,STK_REG(R16)(r1)
408 addi r1,r1,STACKFRAMESIZE
417 ld r14,STK_REG(R14)(r1)
418 ld r15,STK_REG(R15)(r1)
419 ld r16,STK_REG(R16)(r1)
420 addi r1,r1,STACKFRAMESIZE
427 EXPORT_SYMBOL(csum_partial_copy_generic)
430 * __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
431 * const struct in6_addr *daddr,
432 * __u32 len, __u8 proto, __wsum sum)
435 _GLOBAL(csum_ipv6_magic)
442 #ifdef CONFIG_CPU_LITTLE_ENDIAN
450 rotldi r3, r0, 32 /* fold two 32 bit halves together */
453 rotlwi r3, r0, 16 /* fold two 16 bit halves together */
456 rlwinm r3, r3, 16, 16, 31
458 EXPORT_SYMBOL(csum_ipv6_magic)