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/export.h>
12 #include <linux/sys.h>
13 #include <asm/processor.h>
14 #include <asm/cache.h>
15 #include <asm/errno.h>
16 #include <asm/ppc_asm.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 0xffffffff, while copying the block to dst.
82 * If an access exception occurs it returns zero.
84 * csum_partial_copy_generic(src, dst, len)
86 #define CSUM_COPY_16_BYTES_WITHEX(n) \
108 #define CSUM_COPY_16_BYTES_EXCODE(n) \
109 EX_TABLE(8 ## n ## 0b, fault); \
110 EX_TABLE(8 ## n ## 1b, fault); \
111 EX_TABLE(8 ## n ## 2b, fault); \
112 EX_TABLE(8 ## n ## 3b, fault); \
113 EX_TABLE(8 ## n ## 4b, fault); \
114 EX_TABLE(8 ## n ## 5b, fault); \
115 EX_TABLE(8 ## n ## 6b, fault); \
116 EX_TABLE(8 ## n ## 7b, fault);
120 CACHELINE_BYTES = L1_CACHE_BYTES
121 LG_CACHELINE_BYTES = L1_CACHE_SHIFT
122 CACHELINE_MASK = (L1_CACHE_BYTES-1)
124 _GLOBAL(csum_partial_copy_generic)
126 addic r0,r0,0 /* clear carry */
130 andi. r0,r0,CACHELINE_MASK /* # bytes to start of cache line */
134 cmplw 0,r5,r0 /* is this more than total to do? */
135 blt 63f /* if not much to do */
137 rlwnm r12,r12,r7,0,31 /* odd destination address: rotate one byte */
138 cmplwi cr7,r7,0 /* is destination address even ? */
139 andi. r8,r0,3 /* get it word-aligned first */
143 70: lbz r9,4(r4) /* do some bytes */
155 72: lwzu r9,4(r4) /* do some words */
160 58: srwi. r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
161 clrlwi r5,r5,32-LG_CACHELINE_BYTES
165 /* Here we decide how far ahead to prefetch the source */
171 #if MAX_COPY_PREFETCH > 1
172 /* Heuristically, for large transfers we prefetch
173 MAX_COPY_PREFETCH cachelines ahead. For small transfers
174 we prefetch 1 cacheline ahead. */
175 cmpwi r0,MAX_COPY_PREFETCH
177 li r7,MAX_COPY_PREFETCH
180 addi r3,r3,CACHELINE_BYTES
184 addi r3,r3,CACHELINE_BYTES
185 #endif /* MAX_COPY_PREFETCH > 1 */
193 /* the main body of the cacheline loop */
194 CSUM_COPY_16_BYTES_WITHEX(0)
195 #if L1_CACHE_BYTES >= 32
196 CSUM_COPY_16_BYTES_WITHEX(1)
197 #if L1_CACHE_BYTES >= 64
198 CSUM_COPY_16_BYTES_WITHEX(2)
199 CSUM_COPY_16_BYTES_WITHEX(3)
200 #if L1_CACHE_BYTES >= 128
201 CSUM_COPY_16_BYTES_WITHEX(4)
202 CSUM_COPY_16_BYTES_WITHEX(5)
203 CSUM_COPY_16_BYTES_WITHEX(6)
204 CSUM_COPY_16_BYTES_WITHEX(7)
237 rlwinm r3,r3,8,0,31 /* odd destination address: rotate one byte */
244 EX_TABLE(70b, fault);
245 EX_TABLE(71b, fault);
246 EX_TABLE(72b, fault);
247 EX_TABLE(73b, fault);
248 EX_TABLE(54b, fault);
251 * this stuff handles faults in the cacheline loop and branches to either
252 * fault (if in read part) or fault (if in write part)
254 CSUM_COPY_16_BYTES_EXCODE(0)
255 #if L1_CACHE_BYTES >= 32
256 CSUM_COPY_16_BYTES_EXCODE(1)
257 #if L1_CACHE_BYTES >= 64
258 CSUM_COPY_16_BYTES_EXCODE(2)
259 CSUM_COPY_16_BYTES_EXCODE(3)
260 #if L1_CACHE_BYTES >= 128
261 CSUM_COPY_16_BYTES_EXCODE(4)
262 CSUM_COPY_16_BYTES_EXCODE(5)
263 CSUM_COPY_16_BYTES_EXCODE(6)
264 CSUM_COPY_16_BYTES_EXCODE(7)
269 EX_TABLE(30b, fault);
270 EX_TABLE(31b, fault);
271 EX_TABLE(40b, fault);
272 EX_TABLE(41b, fault);
273 EX_TABLE(50b, fault);
274 EX_TABLE(51b, fault);
276 EXPORT_SYMBOL(csum_partial_copy_generic)
279 * __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
280 * const struct in6_addr *daddr,
281 * __u32 len, __u8 proto, __wsum sum)
284 _GLOBAL(csum_ipv6_magic)
300 add r5, r5, r6 /* assumption: len + proto doesn't carry */
307 rlwinm r3, r3, 16, 16, 31
309 EXPORT_SYMBOL(csum_ipv6_magic)