2 * This file contains assembly-language implementations
3 * of IP-style 1's complement checksum routines.
5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Severely hacked about by Paul Mackerras (paulus@cs.anu.edu.au).
15 #include <linux/sys.h>
16 #include <asm/processor.h>
17 #include <asm/errno.h>
18 #include <asm/ppc_asm.h>
21 * ip_fast_csum(r3=buf, r4=len) -- Optimized for IP header
22 * len is in words and is always >= 5.
24 * In practice len == 5, but this is not guaranteed. So this code does not
25 * attempt to use doubleword instructions.
37 addze r0,r0 /* add in final carry */
38 rldicl r4,r0,32,0 /* fold two 32-bit halves together */
41 rlwinm r3,r0,16,0,31 /* fold two halves together */
48 * Compute checksum of TCP or UDP pseudo-header:
49 * csum_tcpudp_magic(r3=saddr, r4=daddr, r5=len, r6=proto, r7=sum)
50 * No real gain trying to do this specially for 64 bit, but
51 * the 32 bit addition may spill into the upper bits of
52 * the doubleword so we still must fold it down from 64.
54 _GLOBAL(csum_tcpudp_magic)
55 rlwimi r5,r6,16,0,15 /* put proto in upper half of len */
56 addc r0,r3,r4 /* add 4 32-bit words together */
59 rldicl r4,r0,32,0 /* fold 64 bit value */
62 rlwinm r3,r0,16,0,31 /* fold two halves together */
68 #define STACKFRAMESIZE 256
69 #define STK_REG(i) (112 + ((i)-14)*8)
72 * Computes the checksum of a memory block at buff, length len,
73 * and adds in "sum" (32-bit).
75 * csum_partial(r3=buff, r4=len, r5=sum)
78 addic r0,r5,0 /* clear carry */
80 srdi. r6,r4,3 /* less than 8 bytes? */
84 * If only halfword aligned, align to a double word. Since odd
85 * aligned addresses should be rare and they would require more
86 * work to calculate the correct checksum, we ignore that case
87 * and take the potential slowdown of unaligned loads.
89 rldicl. r6,r3,64-1,64-2 /* r6 = (r3 & 0x3) >> 1 */
97 lhz r6,0(r3) /* align to doubleword */
105 * We unroll the loop such that each iteration is 64 bytes with an
106 * entry and exit limb of 64 bytes, meaning a minimum size of
110 beq .Lcsum_tail_doublewords /* len < 128 */
116 stdu r1,-STACKFRAMESIZE(r1)
117 std r14,STK_REG(r14)(r1)
118 std r15,STK_REG(r15)(r1)
119 std r16,STK_REG(r16)(r1)
128 * On POWER6 and POWER7 back to back addes take 2 cycles because of
129 * the XER dependency. This means the fastest this loop can go is
130 * 16 cycles per iteration. The scheduling of the loop below has
131 * been shown to hit this on both POWER6 and POWER7.
178 ld r14,STK_REG(r14)(r1)
179 ld r15,STK_REG(r15)(r1)
180 ld r16,STK_REG(r16)(r1)
181 addi r1,r1,STACKFRAMESIZE
185 .Lcsum_tail_doublewords: /* Up to 127 bytes to go */
198 .Lcsum_tail_word: /* Up to 7 bytes to go */
200 beq .Lcsum_tail_halfword
207 .Lcsum_tail_halfword: /* Up to 3 bytes to go */
216 .Lcsum_tail_byte: /* Up to 1 byte to go */
221 sldi r9,r6,8 /* Pad the byte out to 16 bits */
225 addze r0,r0 /* add in final carry */
226 rldicl r4,r0,32,0 /* fold two 32 bit halves together */
232 * Computes the checksum of a memory block at src, length len,
233 * and adds in "sum" (32-bit), while copying the block to dst.
234 * If an access exception occurs on src or dst, it stores -EFAULT
235 * to *src_err or *dst_err respectively, and (for an error on
236 * src) zeroes the rest of dst.
238 * This code needs to be reworked to take advantage of 64 bit sum+copy.
239 * However, due to tokenring halfword alignment problems this will be very
240 * tricky. For now we'll leave it until we instrument it somehow.
242 * csum_partial_copy_generic(r3=src, r4=dst, r5=len, r6=sum, r7=src_err, r8=dst_err)
244 _GLOBAL(csum_partial_copy_generic)
249 beq 3f /* if we're doing < 4 bytes */
250 andi. r9,r4,2 /* Align dst to longword boundary */
252 81: lhz r6,4(r3) /* do 2 bytes to get aligned */
258 srwi. r6,r5,2 /* # words to do */
261 82: lwzu r6,4(r3) /* the bdnz has zero overhead, so it should */
262 92: stwu r6,4(r4) /* be unnecessary to unroll this loop */
278 slwi r6,r6,8 /* Upper byte of word */
280 5: addze r3,r0 /* add in final carry (unlikely with 64-bit regs) */
281 rldicl r4,r3,32,0 /* fold 64 bit value */
286 /* These shouldn't go in the fixup section, since that would
287 cause the ex_table addresses to get out of order. */
330 .section __ex_table,"a"
332 .llong 81b,src_error_1
334 .llong 82b,src_error_2
336 .llong 83b,src_error_3
338 .llong 84b,src_error_3