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 */
69 * Computes the checksum of a memory block at buff, length len,
70 * and adds in "sum" (32-bit).
72 * This code assumes at least halfword alignment, though the length
73 * can be any number of bytes. The sum is accumulated in r5.
75 * csum_partial(r3=buff, r4=len, r5=sum)
78 subi r3,r3,8 /* we'll offset by 8 for the loads */
79 srdi. r6,r4,3 /* divide by 8 for doubleword count */
80 addic r5,r5,0 /* clear carry */
81 beq 3f /* if we're doing < 8 bytes */
82 andi. r0,r3,2 /* aligned on a word boundary already? */
84 lhz r6,8(r3) /* do 2 bytes to get aligned */
88 srdi. r6,r4,3 /* recompute number of doublewords */
89 beq 3f /* any left? */
91 2: ldu r6,8(r3) /* main sum loop */
94 andi. r4,r4,7 /* compute bytes left to sum after doublewords */
95 3: cmpwi 0,r4,4 /* is at least a full word left? */
97 lwz r6,8(r3) /* sum this word */
101 4: cmpwi 0,r4,2 /* is at least a halfword left? */
103 lhz r6,8(r3) /* sum this halfword */
107 5: cmpwi 0,r4,1 /* is at least a byte left? */
109 lbz r6,8(r3) /* sum this byte */
110 slwi r6,r6,8 /* this byte is assumed to be the upper byte of a halfword */
112 6: addze r5,r5 /* add in final carry */
113 rldicl r4,r5,32,0 /* fold two 32-bit halves together */
119 * Computes the checksum of a memory block at src, length len,
120 * and adds in "sum" (32-bit), while copying the block to dst.
121 * If an access exception occurs on src or dst, it stores -EFAULT
122 * to *src_err or *dst_err respectively, and (for an error on
123 * src) zeroes the rest of dst.
125 * This code needs to be reworked to take advantage of 64 bit sum+copy.
126 * However, due to tokenring halfword alignment problems this will be very
127 * tricky. For now we'll leave it until we instrument it somehow.
129 * csum_partial_copy_generic(r3=src, r4=dst, r5=len, r6=sum, r7=src_err, r8=dst_err)
131 _GLOBAL(csum_partial_copy_generic)
136 beq 3f /* if we're doing < 4 bytes */
137 andi. r9,r4,2 /* Align dst to longword boundary */
139 81: lhz r6,4(r3) /* do 2 bytes to get aligned */
145 srwi. r6,r5,2 /* # words to do */
148 82: lwzu r6,4(r3) /* the bdnz has zero overhead, so it should */
149 92: stwu r6,4(r4) /* be unnecessary to unroll this loop */
165 slwi r6,r6,8 /* Upper byte of word */
167 5: addze r3,r0 /* add in final carry (unlikely with 64-bit regs) */
168 rldicl r4,r3,32,0 /* fold 64 bit value */
173 /* These shouldn't go in the fixup section, since that would
174 cause the ex_table addresses to get out of order. */
217 .section __ex_table,"a"
219 .llong 81b,src_error_1
221 .llong 82b,src_error_2
223 .llong 83b,src_error_3
225 .llong 84b,src_error_3