powerpc: Restore registers on error exit from csum_partial_copy_generic()
[pandora-kernel.git] / arch / powerpc / lib / checksum_64.S
1 /*
2  * This file contains assembly-language implementations
3  * of IP-style 1's complement checksum routines.
4  *      
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  *
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.
11  *
12  * Severely hacked about by Paul Mackerras (paulus@cs.anu.edu.au).
13  */
14
15 #include <linux/sys.h>
16 #include <asm/processor.h>
17 #include <asm/errno.h>
18 #include <asm/ppc_asm.h>
19
20 /*
21  * ip_fast_csum(r3=buf, r4=len) -- Optimized for IP header
22  * len is in words and is always >= 5.
23  *
24  * In practice len == 5, but this is not guaranteed.  So this code does not
25  * attempt to use doubleword instructions.
26  */
27 _GLOBAL(ip_fast_csum)
28         lwz     r0,0(r3)
29         lwzu    r5,4(r3)
30         addic.  r4,r4,-2
31         addc    r0,r0,r5
32         mtctr   r4
33         blelr-
34 1:      lwzu    r4,4(r3)
35         adde    r0,r0,r4
36         bdnz    1b
37         addze   r0,r0           /* add in final carry */
38         rldicl  r4,r0,32,0      /* fold two 32-bit halves together */
39         add     r0,r0,r4
40         srdi    r0,r0,32
41         rlwinm  r3,r0,16,0,31   /* fold two halves together */
42         add     r3,r0,r3
43         not     r3,r3
44         srwi    r3,r3,16
45         blr
46
47 /*
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.
53  */     
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 */
57         adde    r0,r0,r5
58         adde    r0,r0,r7
59         rldicl  r4,r0,32,0      /* fold 64 bit value */
60         add     r0,r4,r0
61         srdi    r0,r0,32
62         rlwinm  r3,r0,16,0,31   /* fold two halves together */
63         add     r3,r0,r3
64         not     r3,r3
65         srwi    r3,r3,16
66         blr
67
68 #define STACKFRAMESIZE 256
69 #define STK_REG(i)      (112 + ((i)-14)*8)
70
71 /*
72  * Computes the checksum of a memory block at buff, length len,
73  * and adds in "sum" (32-bit).
74  *
75  * csum_partial(r3=buff, r4=len, r5=sum)
76  */
77 _GLOBAL(csum_partial)
78         addic   r0,r5,0                 /* clear carry */
79
80         srdi.   r6,r4,3                 /* less than 8 bytes? */
81         beq     .Lcsum_tail_word
82
83         /*
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.
88          */
89         rldicl. r6,r3,64-1,64-2         /* r6 = (r3 & 0x3) >> 1 */
90         beq     .Lcsum_aligned
91
92         li      r7,4
93         sub     r6,r7,r6
94         mtctr   r6
95
96 1:
97         lhz     r6,0(r3)                /* align to doubleword */
98         subi    r4,r4,2
99         addi    r3,r3,2
100         adde    r0,r0,r6
101         bdnz    1b
102
103 .Lcsum_aligned:
104         /*
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
107          * 128 bytes.
108          */
109         srdi.   r6,r4,7
110         beq     .Lcsum_tail_doublewords         /* len < 128 */
111
112         srdi    r6,r4,6
113         subi    r6,r6,1
114         mtctr   r6
115
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)
120
121         ld      r6,0(r3)
122         ld      r9,8(r3)
123
124         ld      r10,16(r3)
125         ld      r11,24(r3)
126
127         /*
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.
132          */
133         .align 5
134 2:
135         adde    r0,r0,r6
136         ld      r12,32(r3)
137         ld      r14,40(r3)
138
139         adde    r0,r0,r9
140         ld      r15,48(r3)
141         ld      r16,56(r3)
142         addi    r3,r3,64
143
144         adde    r0,r0,r10
145
146         adde    r0,r0,r11
147
148         adde    r0,r0,r12
149
150         adde    r0,r0,r14
151
152         adde    r0,r0,r15
153         ld      r6,0(r3)
154         ld      r9,8(r3)
155
156         adde    r0,r0,r16
157         ld      r10,16(r3)
158         ld      r11,24(r3)
159         bdnz    2b
160
161
162         adde    r0,r0,r6
163         ld      r12,32(r3)
164         ld      r14,40(r3)
165
166         adde    r0,r0,r9
167         ld      r15,48(r3)
168         ld      r16,56(r3)
169         addi    r3,r3,64
170
171         adde    r0,r0,r10
172         adde    r0,r0,r11
173         adde    r0,r0,r12
174         adde    r0,r0,r14
175         adde    r0,r0,r15
176         adde    r0,r0,r16
177
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
182
183         andi.   r4,r4,63
184
185 .Lcsum_tail_doublewords:                /* Up to 127 bytes to go */
186         srdi.   r6,r4,3
187         beq     .Lcsum_tail_word
188
189         mtctr   r6
190 3:
191         ld      r6,0(r3)
192         addi    r3,r3,8
193         adde    r0,r0,r6
194         bdnz    3b
195
196         andi.   r4,r4,7
197
198 .Lcsum_tail_word:                       /* Up to 7 bytes to go */
199         srdi.   r6,r4,2
200         beq     .Lcsum_tail_halfword
201
202         lwz     r6,0(r3)
203         addi    r3,r3,4
204         adde    r0,r0,r6
205         subi    r4,r4,4
206
207 .Lcsum_tail_halfword:                   /* Up to 3 bytes to go */
208         srdi.   r6,r4,1
209         beq     .Lcsum_tail_byte
210
211         lhz     r6,0(r3)
212         addi    r3,r3,2
213         adde    r0,r0,r6
214         subi    r4,r4,2
215
216 .Lcsum_tail_byte:                       /* Up to 1 byte to go */
217         andi.   r6,r4,1
218         beq     .Lcsum_finish
219
220         lbz     r6,0(r3)
221         sldi    r9,r6,8                 /* Pad the byte out to 16 bits */
222         adde    r0,r0,r9
223
224 .Lcsum_finish:
225         addze   r0,r0                   /* add in final carry */
226         rldicl  r4,r0,32,0              /* fold two 32 bit halves together */
227         add     r3,r4,r0
228         srdi    r3,r3,32
229         blr
230
231
232         .macro srcnr
233 100:
234         .section __ex_table,"a"
235         .align 3
236         .llong 100b,.Lsrc_error_nr
237         .previous
238         .endm
239
240         .macro source
241 150:
242         .section __ex_table,"a"
243         .align 3
244         .llong 150b,.Lsrc_error
245         .previous
246         .endm
247
248         .macro dstnr
249 200:
250         .section __ex_table,"a"
251         .align 3
252         .llong 200b,.Ldest_error_nr
253         .previous
254         .endm
255
256         .macro dest
257 250:
258         .section __ex_table,"a"
259         .align 3
260         .llong 250b,.Ldest_error
261         .previous
262         .endm
263
264 /*
265  * Computes the checksum of a memory block at src, length len,
266  * and adds in "sum" (32-bit), while copying the block to dst.
267  * If an access exception occurs on src or dst, it stores -EFAULT
268  * to *src_err or *dst_err respectively. The caller must take any action
269  * required in this case (zeroing memory, recalculating partial checksum etc).
270  *
271  * csum_partial_copy_generic(r3=src, r4=dst, r5=len, r6=sum, r7=src_err, r8=dst_err)
272  */
273 _GLOBAL(csum_partial_copy_generic)
274         addic   r0,r6,0                 /* clear carry */
275
276         srdi.   r6,r5,3                 /* less than 8 bytes? */
277         beq     .Lcopy_tail_word
278
279         /*
280          * If only halfword aligned, align to a double word. Since odd
281          * aligned addresses should be rare and they would require more
282          * work to calculate the correct checksum, we ignore that case
283          * and take the potential slowdown of unaligned loads.
284          *
285          * If the source and destination are relatively unaligned we only
286          * align the source. This keeps things simple.
287          */
288         rldicl. r6,r3,64-1,64-2         /* r6 = (r3 & 0x3) >> 1 */
289         beq     .Lcopy_aligned
290
291         li      r9,4
292         sub     r6,r9,r6
293         mtctr   r6
294
295 1:
296 srcnr;  lhz     r6,0(r3)                /* align to doubleword */
297         subi    r5,r5,2
298         addi    r3,r3,2
299         adde    r0,r0,r6
300 dstnr;  sth     r6,0(r4)
301         addi    r4,r4,2
302         bdnz    1b
303
304 .Lcopy_aligned:
305         /*
306          * We unroll the loop such that each iteration is 64 bytes with an
307          * entry and exit limb of 64 bytes, meaning a minimum size of
308          * 128 bytes.
309          */
310         srdi.   r6,r5,7
311         beq     .Lcopy_tail_doublewords         /* len < 128 */
312
313         srdi    r6,r5,6
314         subi    r6,r6,1
315         mtctr   r6
316
317         stdu    r1,-STACKFRAMESIZE(r1)
318         std     r14,STK_REG(r14)(r1)
319         std     r15,STK_REG(r15)(r1)
320         std     r16,STK_REG(r16)(r1)
321
322 source; ld      r6,0(r3)
323 source; ld      r9,8(r3)
324
325 source; ld      r10,16(r3)
326 source; ld      r11,24(r3)
327
328         /*
329          * On POWER6 and POWER7 back to back addes take 2 cycles because of
330          * the XER dependency. This means the fastest this loop can go is
331          * 16 cycles per iteration. The scheduling of the loop below has
332          * been shown to hit this on both POWER6 and POWER7.
333          */
334         .align 5
335 2:
336         adde    r0,r0,r6
337 source; ld      r12,32(r3)
338 source; ld      r14,40(r3)
339
340         adde    r0,r0,r9
341 source; ld      r15,48(r3)
342 source; ld      r16,56(r3)
343         addi    r3,r3,64
344
345         adde    r0,r0,r10
346 dest;   std     r6,0(r4)
347 dest;   std     r9,8(r4)
348
349         adde    r0,r0,r11
350 dest;   std     r10,16(r4)
351 dest;   std     r11,24(r4)
352
353         adde    r0,r0,r12
354 dest;   std     r12,32(r4)
355 dest;   std     r14,40(r4)
356
357         adde    r0,r0,r14
358 dest;   std     r15,48(r4)
359 dest;   std     r16,56(r4)
360         addi    r4,r4,64
361
362         adde    r0,r0,r15
363 source; ld      r6,0(r3)
364 source; ld      r9,8(r3)
365
366         adde    r0,r0,r16
367 source; ld      r10,16(r3)
368 source; ld      r11,24(r3)
369         bdnz    2b
370
371
372         adde    r0,r0,r6
373 source; ld      r12,32(r3)
374 source; ld      r14,40(r3)
375
376         adde    r0,r0,r9
377 source; ld      r15,48(r3)
378 source; ld      r16,56(r3)
379         addi    r3,r3,64
380
381         adde    r0,r0,r10
382 dest;   std     r6,0(r4)
383 dest;   std     r9,8(r4)
384
385         adde    r0,r0,r11
386 dest;   std     r10,16(r4)
387 dest;   std     r11,24(r4)
388
389         adde    r0,r0,r12
390 dest;   std     r12,32(r4)
391 dest;   std     r14,40(r4)
392
393         adde    r0,r0,r14
394 dest;   std     r15,48(r4)
395 dest;   std     r16,56(r4)
396         addi    r4,r4,64
397
398         adde    r0,r0,r15
399         adde    r0,r0,r16
400
401         ld      r14,STK_REG(r14)(r1)
402         ld      r15,STK_REG(r15)(r1)
403         ld      r16,STK_REG(r16)(r1)
404         addi    r1,r1,STACKFRAMESIZE
405
406         andi.   r5,r5,63
407
408 .Lcopy_tail_doublewords:                /* Up to 127 bytes to go */
409         srdi.   r6,r5,3
410         beq     .Lcopy_tail_word
411
412         mtctr   r6
413 3:
414 srcnr;  ld      r6,0(r3)
415         addi    r3,r3,8
416         adde    r0,r0,r6
417 dstnr;  std     r6,0(r4)
418         addi    r4,r4,8
419         bdnz    3b
420
421         andi.   r5,r5,7
422
423 .Lcopy_tail_word:                       /* Up to 7 bytes to go */
424         srdi.   r6,r5,2
425         beq     .Lcopy_tail_halfword
426
427 srcnr;  lwz     r6,0(r3)
428         addi    r3,r3,4
429         adde    r0,r0,r6
430 dstnr;  stw     r6,0(r4)
431         addi    r4,r4,4
432         subi    r5,r5,4
433
434 .Lcopy_tail_halfword:                   /* Up to 3 bytes to go */
435         srdi.   r6,r5,1
436         beq     .Lcopy_tail_byte
437
438 srcnr;  lhz     r6,0(r3)
439         addi    r3,r3,2
440         adde    r0,r0,r6
441 dstnr;  sth     r6,0(r4)
442         addi    r4,r4,2
443         subi    r5,r5,2
444
445 .Lcopy_tail_byte:                       /* Up to 1 byte to go */
446         andi.   r6,r5,1
447         beq     .Lcopy_finish
448
449 srcnr;  lbz     r6,0(r3)
450         sldi    r9,r6,8                 /* Pad the byte out to 16 bits */
451         adde    r0,r0,r9
452 dstnr;  stb     r6,0(r4)
453
454 .Lcopy_finish:
455         addze   r0,r0                   /* add in final carry */
456         rldicl  r4,r0,32,0              /* fold two 32 bit halves together */
457         add     r3,r4,r0
458         srdi    r3,r3,32
459         blr
460
461 .Lsrc_error:
462         ld      r14,STK_REG(r14)(r1)
463         ld      r15,STK_REG(r15)(r1)
464         ld      r16,STK_REG(r16)(r1)
465         addi    r1,r1,STACKFRAMESIZE
466 .Lsrc_error_nr:
467         cmpdi   0,r7,0
468         beqlr
469         li      r6,-EFAULT
470         stw     r6,0(r7)
471         blr
472
473 .Ldest_error:
474         ld      r14,STK_REG(r14)(r1)
475         ld      r15,STK_REG(r15)(r1)
476         ld      r16,STK_REG(r16)(r1)
477         addi    r1,r1,STACKFRAMESIZE
478 .Ldest_error_nr:
479         cmpdi   0,r8,0
480         beqlr
481         li      r6,-EFAULT
482         stw     r6,0(r8)
483         blr