2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Quick'n'dirty IP checksum ...
8 * Copyright (C) 1998, 1999 Ralf Baechle
9 * Copyright (C) 1999 Silicon Graphics, Inc.
10 * Copyright (C) 2007 Maciej W. Rozycki
12 #include <linux/errno.h>
14 #include <asm/asm-offsets.h>
15 #include <asm/regdef.h>
19 * As we are sharing code base with the mips32 tree (which use the o32 ABI
20 * register definitions). We need to redefine the register definitions from
21 * the n64 ABI register naming to the o32 ABI register naming.
51 #endif /* USE_DOUBLE */
53 #define UNIT(unit) ((unit)*NBYTES)
55 #define ADDC(sum,reg) \
63 #define CSUM_BIGCHUNK1(src, offset, sum, _t0, _t1, _t2, _t3) \
64 LOAD _t0, (offset + UNIT(0))(src); \
65 LOAD _t1, (offset + UNIT(1))(src); \
66 LOAD _t2, (offset + UNIT(2))(src); \
67 LOAD _t3, (offset + UNIT(3))(src); \
74 #define CSUM_BIGCHUNK(src, offset, sum, _t0, _t1, _t2, _t3) \
75 CSUM_BIGCHUNK1(src, offset, sum, _t0, _t1, _t2, _t3)
77 #define CSUM_BIGCHUNK(src, offset, sum, _t0, _t1, _t2, _t3) \
78 CSUM_BIGCHUNK1(src, offset, sum, _t0, _t1, _t2, _t3); \
79 CSUM_BIGCHUNK1(src, offset + 0x10, sum, _t0, _t1, _t2, _t3)
84 * a1: length of the area to checksum
85 * a2: partial checksum
99 bnez t8, .Lsmall_csumcpy /* < 8 bytes to copy */
102 andi t7, src, 0x1 /* odd buffer? */
105 beqz t7, .Lword_align
109 LONG_SUBU a1, a1, 0x1
114 PTR_ADDU src, src, 0x1
118 beqz t8, .Ldword_align
122 LONG_SUBU a1, a1, 0x2
125 PTR_ADDU src, src, 0x2
128 bnez t8, .Ldo_end_words
132 beqz t8, .Lqword_align
136 LONG_SUBU a1, a1, 0x4
138 PTR_ADDU src, src, 0x4
142 beqz t8, .Loword_align
147 LONG_SUBU a1, a1, 0x8
152 LONG_SUBU a1, a1, 0x8
156 PTR_ADDU src, src, 0x8
160 beqz t8, .Lbegin_movement
169 CSUM_BIGCHUNK1(src, 0x00, sum, t0, t1, t3, t4)
171 LONG_SUBU a1, a1, 0x10
172 PTR_ADDU src, src, 0x10
180 CSUM_BIGCHUNK(src, 0x00, sum, t0, t1, t3, t4)
181 CSUM_BIGCHUNK(src, 0x20, sum, t0, t1, t3, t4)
182 CSUM_BIGCHUNK(src, 0x40, sum, t0, t1, t3, t4)
183 CSUM_BIGCHUNK(src, 0x60, sum, t0, t1, t3, t4)
184 LONG_SUBU t8, t8, 0x01
185 .set reorder /* DADDI_WAR */
186 PTR_ADDU src, src, 0x80
187 bnez t8, .Lmove_128bytes
195 CSUM_BIGCHUNK(src, 0x00, sum, t0, t1, t3, t4)
196 CSUM_BIGCHUNK(src, 0x20, sum, t0, t1, t3, t4)
197 PTR_ADDU src, src, 0x40
200 beqz t2, .Ldo_end_words
204 CSUM_BIGCHUNK(src, 0x00, sum, t0, t1, t3, t4)
206 PTR_ADDU src, src, 0x20
209 beqz t8, .Lsmall_csumcpy
215 LONG_SUBU t8, t8, 0x1
217 .set reorder /* DADDI_WAR */
218 PTR_ADDU src, src, 0x4
222 /* unknown src alignment and < 8 bytes to go */
230 /* Still a full word to go */
239 /* Still a halfword to go */
272 /* odd buffer alignment? */
282 /* Add the passed partial csum. */
290 * checksum and copy routines based on memcpy.S
292 * csum_partial_copy_nocheck(src, dst, len, sum)
293 * __csum_partial_copy_user(src, dst, len, sum, errp)
295 * See "Spec" in memcpy.S for details. Unlike __copy_user, all
296 * function in this file use the standard calling convention.
308 * The exception handler for loads requires that:
309 * 1- AT contain the address of the byte just past the end of the source
311 * 2- src_entry <= src < AT, and
312 * 3- (dst - src) == (dst_entry - src_entry),
313 * The _entry suffix denotes values when __copy_user was called.
315 * (1) is set up up by __csum_partial_copy_from_user and maintained by
316 * not writing AT in __csum_partial_copy
317 * (2) is met by incrementing src by the number of bytes copied
318 * (3) is met by not doing loads between a pair of increments of dst and src
320 * The exception handlers for stores stores -EFAULT to errptr and return.
321 * These handlers do not need to overwrite any data.
324 #define EXC(inst_reg,addr,handler) \
326 .section __ex_table,"a"; \
364 #endif /* USE_DOUBLE */
366 #ifdef CONFIG_CPU_LITTLE_ENDIAN
367 #define LDFIRST LOADR
369 #define STFIRST STORER
370 #define STREST STOREL
371 #define SHIFT_DISCARD SLLV
372 #define SHIFT_DISCARD_REVERT SRLV
374 #define LDFIRST LOADL
376 #define STFIRST STOREL
377 #define STREST STORER
378 #define SHIFT_DISCARD SRLV
379 #define SHIFT_DISCARD_REVERT SLLV
382 #define FIRST(unit) ((unit)*NBYTES)
383 #define REST(unit) (FIRST(unit)+NBYTES-1)
385 #define ADDRMASK (NBYTES-1)
387 #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
393 LEAF(__csum_partial_copy_user)
394 PTR_ADDU AT, src, len /* See (1) above. */
400 FEXPORT(csum_partial_copy_nocheck)
404 * Note: dst & src may be unaligned, len may be 0
408 * The "issue break"s below are very approximate.
409 * Issue delays for dcache fills will perturb the schedule, as will
410 * load queue full replay traps, etc.
412 * If len < NBYTES use byte operations.
415 and t1, dst, ADDRMASK
416 bnez t2, .Lcopy_bytes_checklen
417 and t0, src, ADDRMASK
418 andi odd, dst, 0x1 /* odd buffer? */
419 bnez t1, .Ldst_unaligned
421 bnez t0, .Lsrc_unaligned_dst_aligned
423 * use delay slot for fall-through
424 * src and dst are aligned; need to compute rem
427 SRL t0, len, LOG_NBYTES+3 # +3 for 8 units/iter
428 beqz t0, .Lcleanup_both_aligned # len < 8*NBYTES
430 SUB len, 8*NBYTES # subtract here for bgez loop
433 EXC( LOAD t0, UNIT(0)(src), .Ll_exc)
434 EXC( LOAD t1, UNIT(1)(src), .Ll_exc_copy)
435 EXC( LOAD t2, UNIT(2)(src), .Ll_exc_copy)
436 EXC( LOAD t3, UNIT(3)(src), .Ll_exc_copy)
437 EXC( LOAD t4, UNIT(4)(src), .Ll_exc_copy)
438 EXC( LOAD t5, UNIT(5)(src), .Ll_exc_copy)
439 EXC( LOAD t6, UNIT(6)(src), .Ll_exc_copy)
440 EXC( LOAD t7, UNIT(7)(src), .Ll_exc_copy)
441 SUB len, len, 8*NBYTES
442 ADD src, src, 8*NBYTES
443 EXC( STORE t0, UNIT(0)(dst), .Ls_exc)
445 EXC( STORE t1, UNIT(1)(dst), .Ls_exc)
447 EXC( STORE t2, UNIT(2)(dst), .Ls_exc)
449 EXC( STORE t3, UNIT(3)(dst), .Ls_exc)
451 EXC( STORE t4, UNIT(4)(dst), .Ls_exc)
453 EXC( STORE t5, UNIT(5)(dst), .Ls_exc)
455 EXC( STORE t6, UNIT(6)(dst), .Ls_exc)
457 EXC( STORE t7, UNIT(7)(dst), .Ls_exc)
459 .set reorder /* DADDI_WAR */
460 ADD dst, dst, 8*NBYTES
463 ADD len, 8*NBYTES # revert len (see above)
466 * len == the number of bytes left to copy < 8*NBYTES
468 .Lcleanup_both_aligned:
471 sltu t0, len, 4*NBYTES
472 bnez t0, .Lless_than_4units
473 and rem, len, (NBYTES-1) # rem = len % NBYTES
477 EXC( LOAD t0, UNIT(0)(src), .Ll_exc)
478 EXC( LOAD t1, UNIT(1)(src), .Ll_exc_copy)
479 EXC( LOAD t2, UNIT(2)(src), .Ll_exc_copy)
480 EXC( LOAD t3, UNIT(3)(src), .Ll_exc_copy)
481 SUB len, len, 4*NBYTES
482 ADD src, src, 4*NBYTES
483 EXC( STORE t0, UNIT(0)(dst), .Ls_exc)
485 EXC( STORE t1, UNIT(1)(dst), .Ls_exc)
487 EXC( STORE t2, UNIT(2)(dst), .Ls_exc)
489 EXC( STORE t3, UNIT(3)(dst), .Ls_exc)
491 .set reorder /* DADDI_WAR */
492 ADD dst, dst, 4*NBYTES
499 beq rem, len, .Lcopy_bytes
502 EXC( LOAD t0, 0(src), .Ll_exc)
505 EXC( STORE t0, 0(dst), .Ls_exc)
507 .set reorder /* DADDI_WAR */
513 * src and dst are aligned, need to copy rem bytes (rem < NBYTES)
514 * A loop would do only a byte at a time with possible branch
515 * mispredicts. Can't do an explicit LOAD dst,mask,or,STORE
516 * because can't assume read-access to dst. Instead, use
517 * STREST dst, which doesn't require read access to dst.
519 * This code should perform better than a simple loop on modern,
520 * wide-issue mips processors because the code has fewer branches and
521 * more instruction-level parallelism.
525 ADD t1, dst, len # t1 is just past last byte of dst
527 SLL rem, len, 3 # rem = number of bits to keep
528 EXC( LOAD t0, 0(src), .Ll_exc)
529 SUB bits, bits, rem # bits = number of bits to discard
530 SHIFT_DISCARD t0, t0, bits
531 EXC( STREST t0, -1(t1), .Ls_exc)
532 SHIFT_DISCARD_REVERT t0, t0, bits
540 * t0 = src & ADDRMASK
541 * t1 = dst & ADDRMASK; T1 > 0
544 * Copy enough bytes to align dst
545 * Set match = (src and dst have same alignment)
548 EXC( LDFIRST t3, FIRST(0)(src), .Ll_exc)
550 EXC( LDREST t3, REST(0)(src), .Ll_exc_copy)
551 SUB t2, t2, t1 # t2 = number of bytes copied
553 EXC( STFIRST t3, FIRST(0)(dst), .Ls_exc)
554 SLL t4, t1, 3 # t4 = number of bits to discard
555 SHIFT_DISCARD t3, t3, t4
556 /* no SHIFT_DISCARD_REVERT to handle odd buffer properly */
561 beqz match, .Lboth_aligned
564 .Lsrc_unaligned_dst_aligned:
565 SRL t0, len, LOG_NBYTES+2 # +2 for 4 units/iter
566 beqz t0, .Lcleanup_src_unaligned
567 and rem, len, (4*NBYTES-1) # rem = len % 4*NBYTES
570 * Avoid consecutive LD*'s to the same register since some mips
571 * implementations can't issue them in the same cycle.
572 * It's OK to load FIRST(N+1) before REST(N) because the two addresses
573 * are to the same unit (unless src is aligned, but it's not).
575 EXC( LDFIRST t0, FIRST(0)(src), .Ll_exc)
576 EXC( LDFIRST t1, FIRST(1)(src), .Ll_exc_copy)
577 SUB len, len, 4*NBYTES
578 EXC( LDREST t0, REST(0)(src), .Ll_exc_copy)
579 EXC( LDREST t1, REST(1)(src), .Ll_exc_copy)
580 EXC( LDFIRST t2, FIRST(2)(src), .Ll_exc_copy)
581 EXC( LDFIRST t3, FIRST(3)(src), .Ll_exc_copy)
582 EXC( LDREST t2, REST(2)(src), .Ll_exc_copy)
583 EXC( LDREST t3, REST(3)(src), .Ll_exc_copy)
584 ADD src, src, 4*NBYTES
585 #ifdef CONFIG_CPU_SB1
586 nop # improves slotting
588 EXC( STORE t0, UNIT(0)(dst), .Ls_exc)
590 EXC( STORE t1, UNIT(1)(dst), .Ls_exc)
592 EXC( STORE t2, UNIT(2)(dst), .Ls_exc)
594 EXC( STORE t3, UNIT(3)(dst), .Ls_exc)
596 .set reorder /* DADDI_WAR */
597 ADD dst, dst, 4*NBYTES
601 .Lcleanup_src_unaligned:
603 and rem, len, NBYTES-1 # rem = len % NBYTES
604 beq rem, len, .Lcopy_bytes
607 EXC( LDFIRST t0, FIRST(0)(src), .Ll_exc)
608 EXC( LDREST t0, REST(0)(src), .Ll_exc_copy)
611 EXC( STORE t0, 0(dst), .Ls_exc)
613 .set reorder /* DADDI_WAR */
618 .Lcopy_bytes_checklen:
622 /* 0 < len < NBYTES */
623 #ifdef CONFIG_CPU_LITTLE_ENDIAN
624 #define SHIFT_START 0
627 #define SHIFT_START 8*(NBYTES-1)
630 move t2, zero # partial word
631 li t3, SHIFT_START # shift
632 /* use .Ll_exc_copy here to return correct sum on fault */
633 #define COPY_BYTE(N) \
634 EXC( lbu t0, N(src), .Ll_exc_copy); \
636 EXC( sb t0, N(dst), .Ls_exc); \
638 addu t3, SHIFT_INC; \
639 beqz len, .Lcopy_bytes_done; \
650 EXC( lbu t0, NBYTES-2(src), .Ll_exc_copy)
652 EXC( sb t0, NBYTES-2(dst), .Ls_exc)
674 /* odd buffer alignment? */
690 * Copy bytes from src until faulting load address (or until a
693 * When reached by a faulting LDFIRST/LDREST, THREAD_BUADDR($28)
694 * may be more than a byte beyond the last address.
695 * Hence, the lb below may get an exception.
697 * Assumes src < THREAD_BUADDR($28)
699 LOAD t0, TI_TASK($28)
701 LOAD t0, THREAD_BUADDR(t0)
703 EXC( lbu t1, 0(src), .Ll_exc)
705 sb t1, 0(dst) # can't fault -- we're copy_from_user
709 .set reorder /* DADDI_WAR */
714 LOAD t0, TI_TASK($28)
716 LOAD t0, THREAD_BUADDR(t0) # t0 is just past last good address
718 SUB len, AT, t0 # len number of uncopied bytes
720 * Here's where we rely on src and dst being incremented in tandem,
722 * dst += (fault addr - src) to put dst at first byte to clear
724 ADD dst, t0 # compute start address in a1
727 * Clear len bytes starting at dst. Can't call __bzero because it
728 * might modify len. An inefficient loop for these rare times...
730 .set reorder /* DADDI_WAR */
738 #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
751 li v0, -1 /* invalid checksum */
756 END(__csum_partial_copy_user)