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.
11 #include <linux/errno.h>
13 #include <asm/asm-offsets.h>
14 #include <asm/regdef.h>
18 * As we are sharing code base with the mips32 tree (which use the o32 ABI
19 * register definitions). We need to redefine the register definitions from
20 * the n64 ABI register naming to the o32 ABI register naming.
50 #endif /* USE_DOUBLE */
52 #define UNIT(unit) ((unit)*NBYTES)
54 #define ADDC(sum,reg) \
59 #define CSUM_BIGCHUNK1(src, offset, sum, _t0, _t1, _t2, _t3) \
60 LOAD _t0, (offset + UNIT(0))(src); \
61 LOAD _t1, (offset + UNIT(1))(src); \
62 LOAD _t2, (offset + UNIT(2))(src); \
63 LOAD _t3, (offset + UNIT(3))(src); \
70 #define CSUM_BIGCHUNK(src, offset, sum, _t0, _t1, _t2, _t3) \
71 CSUM_BIGCHUNK1(src, offset, sum, _t0, _t1, _t2, _t3)
73 #define CSUM_BIGCHUNK(src, offset, sum, _t0, _t1, _t2, _t3) \
74 CSUM_BIGCHUNK1(src, offset, sum, _t0, _t1, _t2, _t3); \
75 CSUM_BIGCHUNK1(src, offset + 0x10, sum, _t0, _t1, _t2, _t3)
80 * a1: length of the area to checksum
81 * a2: partial checksum
95 bnez t8, small_csumcpy /* < 8 bytes to copy */
98 andi t7, src, 0x1 /* odd buffer? */
105 LONG_SUBU a1, a1, 0x1
110 PTR_ADDU src, src, 0x1
118 LONG_SUBU a1, a1, 0x2
121 PTR_ADDU src, src, 0x2
124 bnez t8, do_end_words
132 LONG_SUBU a1, a1, 0x4
134 PTR_ADDU src, src, 0x4
143 LONG_SUBU a1, a1, 0x8
148 LONG_SUBU a1, a1, 0x8
152 PTR_ADDU src, src, 0x8
156 beqz t8, begin_movement
165 CSUM_BIGCHUNK1(src, 0x00, sum, t0, t1, t3, t4)
167 LONG_SUBU a1, a1, 0x10
168 PTR_ADDU src, src, 0x10
176 CSUM_BIGCHUNK(src, 0x00, sum, t0, t1, t3, t4)
177 CSUM_BIGCHUNK(src, 0x20, sum, t0, t1, t3, t4)
178 CSUM_BIGCHUNK(src, 0x40, sum, t0, t1, t3, t4)
179 CSUM_BIGCHUNK(src, 0x60, sum, t0, t1, t3, t4)
180 LONG_SUBU t8, t8, 0x01
181 bnez t8, move_128bytes
182 PTR_ADDU src, src, 0x80
189 CSUM_BIGCHUNK(src, 0x00, sum, t0, t1, t3, t4)
190 CSUM_BIGCHUNK(src, 0x20, sum, t0, t1, t3, t4)
191 PTR_ADDU src, src, 0x40
194 beqz t2, do_end_words
198 CSUM_BIGCHUNK(src, 0x00, sum, t0, t1, t3, t4)
200 PTR_ADDU src, src, 0x20
203 beqz t8, small_csumcpy
209 LONG_SUBU t8, t8, 0x1
212 PTR_ADDU src, src, 0x4
214 /* unknown src alignment and < 8 bytes to go */
222 /* Still a full word to go */
231 /* Still a halfword to go */
262 /* odd buffer alignment? */
271 /* Add the passed partial csum. */
279 * checksum and copy routines based on memcpy.S
281 * csum_partial_copy_nocheck(src, dst, len, sum)
282 * __csum_partial_copy_user(src, dst, len, sum, errp)
284 * See "Spec" in memcpy.S for details. Unlike __copy_user, all
285 * function in this file use the standard calling convention.
297 * The exception handler for loads requires that:
298 * 1- AT contain the address of the byte just past the end of the source
300 * 2- src_entry <= src < AT, and
301 * 3- (dst - src) == (dst_entry - src_entry),
302 * The _entry suffix denotes values when __copy_user was called.
304 * (1) is set up up by __csum_partial_copy_from_user and maintained by
305 * not writing AT in __csum_partial_copy
306 * (2) is met by incrementing src by the number of bytes copied
307 * (3) is met by not doing loads between a pair of increments of dst and src
309 * The exception handlers for stores stores -EFAULT to errptr and return.
310 * These handlers do not need to overwrite any data.
313 #define EXC(inst_reg,addr,handler) \
315 .section __ex_table,"a"; \
353 #endif /* USE_DOUBLE */
355 #ifdef CONFIG_CPU_LITTLE_ENDIAN
356 #define LDFIRST LOADR
358 #define STFIRST STORER
359 #define STREST STOREL
360 #define SHIFT_DISCARD SLLV
361 #define SHIFT_DISCARD_REVERT SRLV
363 #define LDFIRST LOADL
365 #define STFIRST STOREL
366 #define STREST STORER
367 #define SHIFT_DISCARD SRLV
368 #define SHIFT_DISCARD_REVERT SLLV
371 #define FIRST(unit) ((unit)*NBYTES)
372 #define REST(unit) (FIRST(unit)+NBYTES-1)
374 #define ADDRMASK (NBYTES-1)
378 LEAF(__csum_partial_copy_user)
379 PTR_ADDU AT, src, len /* See (1) above. */
385 FEXPORT(csum_partial_copy_nocheck)
389 * Note: dst & src may be unaligned, len may be 0
393 * The "issue break"s below are very approximate.
394 * Issue delays for dcache fills will perturb the schedule, as will
395 * load queue full replay traps, etc.
397 * If len < NBYTES use byte operations.
400 and t1, dst, ADDRMASK
401 bnez t2, copy_bytes_checklen
402 and t0, src, ADDRMASK
403 andi odd, dst, 0x1 /* odd buffer? */
404 bnez t1, dst_unaligned
406 bnez t0, src_unaligned_dst_aligned
408 * use delay slot for fall-through
409 * src and dst are aligned; need to compute rem
412 SRL t0, len, LOG_NBYTES+3 # +3 for 8 units/iter
413 beqz t0, cleanup_both_aligned # len < 8*NBYTES
415 SUB len, 8*NBYTES # subtract here for bgez loop
418 EXC( LOAD t0, UNIT(0)(src), l_exc)
419 EXC( LOAD t1, UNIT(1)(src), l_exc_copy)
420 EXC( LOAD t2, UNIT(2)(src), l_exc_copy)
421 EXC( LOAD t3, UNIT(3)(src), l_exc_copy)
422 EXC( LOAD t4, UNIT(4)(src), l_exc_copy)
423 EXC( LOAD t5, UNIT(5)(src), l_exc_copy)
424 EXC( LOAD t6, UNIT(6)(src), l_exc_copy)
425 EXC( LOAD t7, UNIT(7)(src), l_exc_copy)
426 SUB len, len, 8*NBYTES
427 ADD src, src, 8*NBYTES
428 EXC( STORE t0, UNIT(0)(dst), s_exc)
430 EXC( STORE t1, UNIT(1)(dst), s_exc)
432 EXC( STORE t2, UNIT(2)(dst), s_exc)
434 EXC( STORE t3, UNIT(3)(dst), s_exc)
436 EXC( STORE t4, UNIT(4)(dst), s_exc)
438 EXC( STORE t5, UNIT(5)(dst), s_exc)
440 EXC( STORE t6, UNIT(6)(dst), s_exc)
442 EXC( STORE t7, UNIT(7)(dst), s_exc)
445 ADD dst, dst, 8*NBYTES
446 ADD len, 8*NBYTES # revert len (see above)
449 * len == the number of bytes left to copy < 8*NBYTES
451 cleanup_both_aligned:
454 sltu t0, len, 4*NBYTES
455 bnez t0, less_than_4units
456 and rem, len, (NBYTES-1) # rem = len % NBYTES
460 EXC( LOAD t0, UNIT(0)(src), l_exc)
461 EXC( LOAD t1, UNIT(1)(src), l_exc_copy)
462 EXC( LOAD t2, UNIT(2)(src), l_exc_copy)
463 EXC( LOAD t3, UNIT(3)(src), l_exc_copy)
464 SUB len, len, 4*NBYTES
465 ADD src, src, 4*NBYTES
466 EXC( STORE t0, UNIT(0)(dst), s_exc)
468 EXC( STORE t1, UNIT(1)(dst), s_exc)
470 EXC( STORE t2, UNIT(2)(dst), s_exc)
472 EXC( STORE t3, UNIT(3)(dst), s_exc)
475 ADD dst, dst, 4*NBYTES
480 beq rem, len, copy_bytes
483 EXC( LOAD t0, 0(src), l_exc)
486 EXC( STORE t0, 0(dst), s_exc)
492 * src and dst are aligned, need to copy rem bytes (rem < NBYTES)
493 * A loop would do only a byte at a time with possible branch
494 * mispredicts. Can't do an explicit LOAD dst,mask,or,STORE
495 * because can't assume read-access to dst. Instead, use
496 * STREST dst, which doesn't require read access to dst.
498 * This code should perform better than a simple loop on modern,
499 * wide-issue mips processors because the code has fewer branches and
500 * more instruction-level parallelism.
504 ADD t1, dst, len # t1 is just past last byte of dst
506 SLL rem, len, 3 # rem = number of bits to keep
507 EXC( LOAD t0, 0(src), l_exc)
508 SUB bits, bits, rem # bits = number of bits to discard
509 SHIFT_DISCARD t0, t0, bits
510 EXC( STREST t0, -1(t1), s_exc)
511 SHIFT_DISCARD_REVERT t0, t0, bits
519 * t0 = src & ADDRMASK
520 * t1 = dst & ADDRMASK; T1 > 0
523 * Copy enough bytes to align dst
524 * Set match = (src and dst have same alignment)
527 EXC( LDFIRST t3, FIRST(0)(src), l_exc)
529 EXC( LDREST t3, REST(0)(src), l_exc_copy)
530 SUB t2, t2, t1 # t2 = number of bytes copied
532 EXC( STFIRST t3, FIRST(0)(dst), s_exc)
533 SLL t4, t1, 3 # t4 = number of bits to discard
534 SHIFT_DISCARD t3, t3, t4
535 /* no SHIFT_DISCARD_REVERT to handle odd buffer properly */
540 beqz match, both_aligned
543 src_unaligned_dst_aligned:
544 SRL t0, len, LOG_NBYTES+2 # +2 for 4 units/iter
545 beqz t0, cleanup_src_unaligned
546 and rem, len, (4*NBYTES-1) # rem = len % 4*NBYTES
549 * Avoid consecutive LD*'s to the same register since some mips
550 * implementations can't issue them in the same cycle.
551 * It's OK to load FIRST(N+1) before REST(N) because the two addresses
552 * are to the same unit (unless src is aligned, but it's not).
554 EXC( LDFIRST t0, FIRST(0)(src), l_exc)
555 EXC( LDFIRST t1, FIRST(1)(src), l_exc_copy)
556 SUB len, len, 4*NBYTES
557 EXC( LDREST t0, REST(0)(src), l_exc_copy)
558 EXC( LDREST t1, REST(1)(src), l_exc_copy)
559 EXC( LDFIRST t2, FIRST(2)(src), l_exc_copy)
560 EXC( LDFIRST t3, FIRST(3)(src), l_exc_copy)
561 EXC( LDREST t2, REST(2)(src), l_exc_copy)
562 EXC( LDREST t3, REST(3)(src), l_exc_copy)
563 ADD src, src, 4*NBYTES
564 #ifdef CONFIG_CPU_SB1
565 nop # improves slotting
567 EXC( STORE t0, UNIT(0)(dst), s_exc)
569 EXC( STORE t1, UNIT(1)(dst), s_exc)
571 EXC( STORE t2, UNIT(2)(dst), s_exc)
573 EXC( STORE t3, UNIT(3)(dst), s_exc)
576 ADD dst, dst, 4*NBYTES
578 cleanup_src_unaligned:
580 and rem, len, NBYTES-1 # rem = len % NBYTES
581 beq rem, len, copy_bytes
584 EXC( LDFIRST t0, FIRST(0)(src), l_exc)
585 EXC( LDREST t0, REST(0)(src), l_exc_copy)
588 EXC( STORE t0, 0(dst), s_exc)
597 /* 0 < len < NBYTES */
598 #ifdef CONFIG_CPU_LITTLE_ENDIAN
599 #define SHIFT_START 0
602 #define SHIFT_START 8*(NBYTES-1)
605 move t2, zero # partial word
606 li t3, SHIFT_START # shift
607 /* use l_exc_copy here to return correct sum on fault */
608 #define COPY_BYTE(N) \
609 EXC( lbu t0, N(src), l_exc_copy); \
611 EXC( sb t0, N(dst), s_exc); \
613 addu t3, SHIFT_INC; \
614 beqz len, copy_bytes_done; \
625 EXC( lbu t0, NBYTES-2(src), l_exc_copy)
627 EXC( sb t0, NBYTES-2(dst), s_exc)
647 /* odd buffer alignment? */
662 * Copy bytes from src until faulting load address (or until a
665 * When reached by a faulting LDFIRST/LDREST, THREAD_BUADDR($28)
666 * may be more than a byte beyond the last address.
667 * Hence, the lb below may get an exception.
669 * Assumes src < THREAD_BUADDR($28)
671 LOAD t0, TI_TASK($28)
673 LOAD t0, THREAD_BUADDR(t0)
675 EXC( lbu t1, 0(src), l_exc)
677 sb t1, 0(dst) # can't fault -- we're copy_from_user
684 LOAD t0, TI_TASK($28)
686 LOAD t0, THREAD_BUADDR(t0) # t0 is just past last good address
688 SUB len, AT, t0 # len number of uncopied bytes
690 * Here's where we rely on src and dst being incremented in tandem,
692 * dst += (fault addr - src) to put dst at first byte to clear
694 ADD dst, t0 # compute start address in a1
697 * Clear len bytes starting at dst. Can't call __bzero because it
698 * might modify len. An inefficient loop for these rare times...
711 li v0, -1 /* invalid checksum */
715 END(__csum_partial_copy_user)