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/cache.h>
14 #include <asm/errno.h>
15 #include <asm/ppc_asm.h>
16 #include <asm/export.h>
21 * computes the checksum of a memory block at buff, length len,
22 * and adds in "sum" (32-bit)
24 * __csum_partial(buff, len, sum)
26 _GLOBAL(__csum_partial)
28 srawi. r6,r4,2 /* Divide len by 4 and also clear carry */
29 beq 3f /* if we're doing < 4 bytes */
30 andi. r0,r3,2 /* Align buffer to longword boundary */
32 lhz r0,4(r3) /* do 2 bytes to get aligned */
35 srwi. r6,r4,2 /* # words to do */
38 1: andi. r6,r6,3 /* Prepare to handle words 4 by 4 */
44 21: srwi. r6,r4,4 /* # blocks of 4 words to do */
73 slwi r0,r0,8 /* Upper byte of word */
75 5: addze r3,r5 /* add in final carry */
77 EXPORT_SYMBOL(__csum_partial)
80 * Computes the checksum of a memory block at src, length len,
81 * and adds in "sum" (32-bit), while copying the block to dst.
82 * If an access exception occurs on src or dst, it stores -EFAULT
83 * to *src_err or *dst_err respectively, and (for an error on
84 * src) zeroes the rest of dst.
86 * csum_partial_copy_generic(src, dst, len, sum, src_err, dst_err)
88 #define CSUM_COPY_16_BYTES_WITHEX(n) \
110 #define CSUM_COPY_16_BYTES_EXCODE(n) \
111 EX_TABLE(8 ## n ## 0b, src_error); \
112 EX_TABLE(8 ## n ## 1b, src_error); \
113 EX_TABLE(8 ## n ## 2b, src_error); \
114 EX_TABLE(8 ## n ## 3b, src_error); \
115 EX_TABLE(8 ## n ## 4b, dst_error); \
116 EX_TABLE(8 ## n ## 5b, dst_error); \
117 EX_TABLE(8 ## n ## 6b, dst_error); \
118 EX_TABLE(8 ## n ## 7b, dst_error);
121 .stabs "arch/powerpc/lib/",N_SO,0,0,0f
122 .stabs "checksum_32.S",N_SO,0,0,0f
125 CACHELINE_BYTES = L1_CACHE_BYTES
126 LG_CACHELINE_BYTES = L1_CACHE_SHIFT
127 CACHELINE_MASK = (L1_CACHE_BYTES-1)
129 _GLOBAL(csum_partial_copy_generic)
138 andi. r0,r0,CACHELINE_MASK /* # bytes to start of cache line */
142 cmplw 0,r5,r0 /* is this more than total to do? */
143 blt 63f /* if not much to do */
145 rlwnm r12,r12,r7,0,31 /* odd destination address: rotate one byte */
146 cmplwi cr7,r7,0 /* is destination address even ? */
147 andi. r8,r0,3 /* get it word-aligned first */
151 70: lbz r9,4(r4) /* do some bytes */
163 72: lwzu r9,4(r4) /* do some words */
168 58: srwi. r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
169 clrlwi r5,r5,32-LG_CACHELINE_BYTES
173 /* Here we decide how far ahead to prefetch the source */
179 #if MAX_COPY_PREFETCH > 1
180 /* Heuristically, for large transfers we prefetch
181 MAX_COPY_PREFETCH cachelines ahead. For small transfers
182 we prefetch 1 cacheline ahead. */
183 cmpwi r0,MAX_COPY_PREFETCH
185 li r7,MAX_COPY_PREFETCH
188 addi r3,r3,CACHELINE_BYTES
192 addi r3,r3,CACHELINE_BYTES
193 #endif /* MAX_COPY_PREFETCH > 1 */
201 /* the main body of the cacheline loop */
202 CSUM_COPY_16_BYTES_WITHEX(0)
203 #if L1_CACHE_BYTES >= 32
204 CSUM_COPY_16_BYTES_WITHEX(1)
205 #if L1_CACHE_BYTES >= 64
206 CSUM_COPY_16_BYTES_WITHEX(2)
207 CSUM_COPY_16_BYTES_WITHEX(3)
208 #if L1_CACHE_BYTES >= 128
209 CSUM_COPY_16_BYTES_WITHEX(4)
210 CSUM_COPY_16_BYTES_WITHEX(5)
211 CSUM_COPY_16_BYTES_WITHEX(6)
212 CSUM_COPY_16_BYTES_WITHEX(7)
246 rlwinm r3,r3,8,0,31 /* odd destination address: rotate one byte */
268 EX_TABLE(70b, src_error);
269 EX_TABLE(71b, dst_error);
270 EX_TABLE(72b, src_error);
271 EX_TABLE(73b, dst_error);
272 EX_TABLE(54b, dst_error);
275 * this stuff handles faults in the cacheline loop and branches to either
276 * src_error (if in read part) or dst_error (if in write part)
278 CSUM_COPY_16_BYTES_EXCODE(0)
279 #if L1_CACHE_BYTES >= 32
280 CSUM_COPY_16_BYTES_EXCODE(1)
281 #if L1_CACHE_BYTES >= 64
282 CSUM_COPY_16_BYTES_EXCODE(2)
283 CSUM_COPY_16_BYTES_EXCODE(3)
284 #if L1_CACHE_BYTES >= 128
285 CSUM_COPY_16_BYTES_EXCODE(4)
286 CSUM_COPY_16_BYTES_EXCODE(5)
287 CSUM_COPY_16_BYTES_EXCODE(6)
288 CSUM_COPY_16_BYTES_EXCODE(7)
293 EX_TABLE(30b, src_error);
294 EX_TABLE(31b, dst_error);
295 EX_TABLE(40b, src_error);
296 EX_TABLE(41b, dst_error);
297 EX_TABLE(50b, src_error);
298 EX_TABLE(51b, dst_error);
300 EXPORT_SYMBOL(csum_partial_copy_generic)
303 * __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
304 * const struct in6_addr *daddr,
305 * __u32 len, __u8 proto, __wsum sum)
308 _GLOBAL(csum_ipv6_magic)
324 add r5, r5, r6 /* assumption: len + proto doesn't carry */
331 rlwinm r3, r3, 16, 16, 31
333 EXPORT_SYMBOL(csum_ipv6_magic)