csum_partial.S 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  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
  4. * for more details.
  5. *
  6. * Quick'n'dirty IP checksum ...
  7. *
  8. * Copyright (C) 1998, 1999 Ralf Baechle
  9. * Copyright (C) 1999 Silicon Graphics, Inc.
  10. * Copyright (C) 2007 Maciej W. Rozycki
  11. * Copyright (C) 2014 Imagination Technologies Ltd.
  12. */
  13. #include <linux/errno.h>
  14. #include <asm/asm.h>
  15. #include <asm/asm-offsets.h>
  16. #include <asm/regdef.h>
  17. #ifdef CONFIG_64BIT
  18. /*
  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.
  22. */
  23. #undef t0
  24. #undef t1
  25. #undef t2
  26. #undef t3
  27. #define t0 $8
  28. #define t1 $9
  29. #define t2 $10
  30. #define t3 $11
  31. #define t4 $12
  32. #define t5 $13
  33. #define t6 $14
  34. #define t7 $15
  35. #define USE_DOUBLE
  36. #endif
  37. #ifdef USE_DOUBLE
  38. #define LOAD ld
  39. #define LOAD32 lwu
  40. #define ADD daddu
  41. #define NBYTES 8
  42. #else
  43. #define LOAD lw
  44. #define LOAD32 lw
  45. #define ADD addu
  46. #define NBYTES 4
  47. #endif /* USE_DOUBLE */
  48. #define UNIT(unit) ((unit)*NBYTES)
  49. #define ADDC(sum,reg) \
  50. .set push; \
  51. .set noat; \
  52. ADD sum, reg; \
  53. sltu v1, sum, reg; \
  54. ADD sum, v1; \
  55. .set pop
  56. #define ADDC32(sum,reg) \
  57. .set push; \
  58. .set noat; \
  59. addu sum, reg; \
  60. sltu v1, sum, reg; \
  61. addu sum, v1; \
  62. .set pop
  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); \
  68. ADDC(_t0, _t1); \
  69. ADDC(_t2, _t3); \
  70. ADDC(sum, _t0); \
  71. ADDC(sum, _t2)
  72. #ifdef USE_DOUBLE
  73. #define CSUM_BIGCHUNK(src, offset, sum, _t0, _t1, _t2, _t3) \
  74. CSUM_BIGCHUNK1(src, offset, sum, _t0, _t1, _t2, _t3)
  75. #else
  76. #define CSUM_BIGCHUNK(src, offset, sum, _t0, _t1, _t2, _t3) \
  77. CSUM_BIGCHUNK1(src, offset, sum, _t0, _t1, _t2, _t3); \
  78. CSUM_BIGCHUNK1(src, offset + 0x10, sum, _t0, _t1, _t2, _t3)
  79. #endif
  80. /*
  81. * a0: source address
  82. * a1: length of the area to checksum
  83. * a2: partial checksum
  84. */
  85. #define src a0
  86. #define sum v0
  87. .text
  88. .set noreorder
  89. .align 5
  90. LEAF(csum_partial)
  91. move sum, zero
  92. move t7, zero
  93. sltiu t8, a1, 0x8
  94. bnez t8, .Lsmall_csumcpy /* < 8 bytes to copy */
  95. move t2, a1
  96. andi t7, src, 0x1 /* odd buffer? */
  97. .Lhword_align:
  98. beqz t7, .Lword_align
  99. andi t8, src, 0x2
  100. lbu t0, (src)
  101. LONG_SUBU a1, a1, 0x1
  102. #ifdef __MIPSEL__
  103. sll t0, t0, 8
  104. #endif
  105. ADDC(sum, t0)
  106. PTR_ADDU src, src, 0x1
  107. andi t8, src, 0x2
  108. .Lword_align:
  109. beqz t8, .Ldword_align
  110. sltiu t8, a1, 56
  111. lhu t0, (src)
  112. LONG_SUBU a1, a1, 0x2
  113. ADDC(sum, t0)
  114. sltiu t8, a1, 56
  115. PTR_ADDU src, src, 0x2
  116. .Ldword_align:
  117. bnez t8, .Ldo_end_words
  118. move t8, a1
  119. andi t8, src, 0x4
  120. beqz t8, .Lqword_align
  121. andi t8, src, 0x8
  122. LOAD32 t0, 0x00(src)
  123. LONG_SUBU a1, a1, 0x4
  124. ADDC(sum, t0)
  125. PTR_ADDU src, src, 0x4
  126. andi t8, src, 0x8
  127. .Lqword_align:
  128. beqz t8, .Loword_align
  129. andi t8, src, 0x10
  130. #ifdef USE_DOUBLE
  131. ld t0, 0x00(src)
  132. LONG_SUBU a1, a1, 0x8
  133. ADDC(sum, t0)
  134. #else
  135. lw t0, 0x00(src)
  136. lw t1, 0x04(src)
  137. LONG_SUBU a1, a1, 0x8
  138. ADDC(sum, t0)
  139. ADDC(sum, t1)
  140. #endif
  141. PTR_ADDU src, src, 0x8
  142. andi t8, src, 0x10
  143. .Loword_align:
  144. beqz t8, .Lbegin_movement
  145. LONG_SRL t8, a1, 0x7
  146. #ifdef USE_DOUBLE
  147. ld t0, 0x00(src)
  148. ld t1, 0x08(src)
  149. ADDC(sum, t0)
  150. ADDC(sum, t1)
  151. #else
  152. CSUM_BIGCHUNK1(src, 0x00, sum, t0, t1, t3, t4)
  153. #endif
  154. LONG_SUBU a1, a1, 0x10
  155. PTR_ADDU src, src, 0x10
  156. LONG_SRL t8, a1, 0x7
  157. .Lbegin_movement:
  158. beqz t8, 1f
  159. andi t2, a1, 0x40
  160. .Lmove_128bytes:
  161. CSUM_BIGCHUNK(src, 0x00, sum, t0, t1, t3, t4)
  162. CSUM_BIGCHUNK(src, 0x20, sum, t0, t1, t3, t4)
  163. CSUM_BIGCHUNK(src, 0x40, sum, t0, t1, t3, t4)
  164. CSUM_BIGCHUNK(src, 0x60, sum, t0, t1, t3, t4)
  165. LONG_SUBU t8, t8, 0x01
  166. .set reorder /* DADDI_WAR */
  167. PTR_ADDU src, src, 0x80
  168. bnez t8, .Lmove_128bytes
  169. .set noreorder
  170. 1:
  171. beqz t2, 1f
  172. andi t2, a1, 0x20
  173. .Lmove_64bytes:
  174. CSUM_BIGCHUNK(src, 0x00, sum, t0, t1, t3, t4)
  175. CSUM_BIGCHUNK(src, 0x20, sum, t0, t1, t3, t4)
  176. PTR_ADDU src, src, 0x40
  177. 1:
  178. beqz t2, .Ldo_end_words
  179. andi t8, a1, 0x1c
  180. .Lmove_32bytes:
  181. CSUM_BIGCHUNK(src, 0x00, sum, t0, t1, t3, t4)
  182. andi t8, a1, 0x1c
  183. PTR_ADDU src, src, 0x20
  184. .Ldo_end_words:
  185. beqz t8, .Lsmall_csumcpy
  186. andi t2, a1, 0x3
  187. LONG_SRL t8, t8, 0x2
  188. .Lend_words:
  189. LOAD32 t0, (src)
  190. LONG_SUBU t8, t8, 0x1
  191. ADDC(sum, t0)
  192. .set reorder /* DADDI_WAR */
  193. PTR_ADDU src, src, 0x4
  194. bnez t8, .Lend_words
  195. .set noreorder
  196. /* unknown src alignment and < 8 bytes to go */
  197. .Lsmall_csumcpy:
  198. move a1, t2
  199. andi t0, a1, 4
  200. beqz t0, 1f
  201. andi t0, a1, 2
  202. /* Still a full word to go */
  203. ulw t1, (src)
  204. PTR_ADDIU src, 4
  205. #ifdef USE_DOUBLE
  206. dsll t1, t1, 32 /* clear lower 32bit */
  207. #endif
  208. ADDC(sum, t1)
  209. 1: move t1, zero
  210. beqz t0, 1f
  211. andi t0, a1, 1
  212. /* Still a halfword to go */
  213. ulhu t1, (src)
  214. PTR_ADDIU src, 2
  215. 1: beqz t0, 1f
  216. sll t1, t1, 16
  217. lbu t2, (src)
  218. nop
  219. #ifdef __MIPSEB__
  220. sll t2, t2, 8
  221. #endif
  222. or t1, t2
  223. 1: ADDC(sum, t1)
  224. /* fold checksum */
  225. #ifdef USE_DOUBLE
  226. dsll32 v1, sum, 0
  227. daddu sum, v1
  228. sltu v1, sum, v1
  229. dsra32 sum, sum, 0
  230. addu sum, v1
  231. #endif
  232. /* odd buffer alignment? */
  233. #if defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_LOONGSON3)
  234. .set push
  235. .set arch=mips32r2
  236. wsbh v1, sum
  237. movn sum, v1, t7
  238. .set pop
  239. #else
  240. beqz t7, 1f /* odd buffer alignment? */
  241. lui v1, 0x00ff
  242. addu v1, 0x00ff
  243. and t0, sum, v1
  244. sll t0, t0, 8
  245. srl sum, sum, 8
  246. and sum, sum, v1
  247. or sum, sum, t0
  248. 1:
  249. #endif
  250. .set reorder
  251. /* Add the passed partial csum. */
  252. ADDC32(sum, a2)
  253. jr ra
  254. .set noreorder
  255. END(csum_partial)
  256. /*
  257. * checksum and copy routines based on memcpy.S
  258. *
  259. * csum_partial_copy_nocheck(src, dst, len, sum)
  260. * __csum_partial_copy_kernel(src, dst, len, sum, errp)
  261. *
  262. * See "Spec" in memcpy.S for details. Unlike __copy_user, all
  263. * function in this file use the standard calling convention.
  264. */
  265. #define src a0
  266. #define dst a1
  267. #define len a2
  268. #define psum a3
  269. #define sum v0
  270. #define odd t8
  271. #define errptr t9
  272. /*
  273. * The exception handler for loads requires that:
  274. * 1- AT contain the address of the byte just past the end of the source
  275. * of the copy,
  276. * 2- src_entry <= src < AT, and
  277. * 3- (dst - src) == (dst_entry - src_entry),
  278. * The _entry suffix denotes values when __copy_user was called.
  279. *
  280. * (1) is set up up by __csum_partial_copy_from_user and maintained by
  281. * not writing AT in __csum_partial_copy
  282. * (2) is met by incrementing src by the number of bytes copied
  283. * (3) is met by not doing loads between a pair of increments of dst and src
  284. *
  285. * The exception handlers for stores stores -EFAULT to errptr and return.
  286. * These handlers do not need to overwrite any data.
  287. */
  288. /* Instruction type */
  289. #define LD_INSN 1
  290. #define ST_INSN 2
  291. #define LEGACY_MODE 1
  292. #define EVA_MODE 2
  293. #define USEROP 1
  294. #define KERNELOP 2
  295. /*
  296. * Wrapper to add an entry in the exception table
  297. * in case the insn causes a memory exception.
  298. * Arguments:
  299. * insn : Load/store instruction
  300. * type : Instruction type
  301. * reg : Register
  302. * addr : Address
  303. * handler : Exception handler
  304. */
  305. #define EXC(insn, type, reg, addr, handler) \
  306. .if \mode == LEGACY_MODE; \
  307. 9: insn reg, addr; \
  308. .section __ex_table,"a"; \
  309. PTR 9b, handler; \
  310. .previous; \
  311. /* This is enabled in EVA mode */ \
  312. .else; \
  313. /* If loading from user or storing to user */ \
  314. .if ((\from == USEROP) && (type == LD_INSN)) || \
  315. ((\to == USEROP) && (type == ST_INSN)); \
  316. 9: __BUILD_EVA_INSN(insn##e, reg, addr); \
  317. .section __ex_table,"a"; \
  318. PTR 9b, handler; \
  319. .previous; \
  320. .else; \
  321. /* EVA without exception */ \
  322. insn reg, addr; \
  323. .endif; \
  324. .endif
  325. #undef LOAD
  326. #ifdef USE_DOUBLE
  327. #define LOADK ld /* No exception */
  328. #define LOAD(reg, addr, handler) EXC(ld, LD_INSN, reg, addr, handler)
  329. #define LOADBU(reg, addr, handler) EXC(lbu, LD_INSN, reg, addr, handler)
  330. #define LOADL(reg, addr, handler) EXC(ldl, LD_INSN, reg, addr, handler)
  331. #define LOADR(reg, addr, handler) EXC(ldr, LD_INSN, reg, addr, handler)
  332. #define STOREB(reg, addr, handler) EXC(sb, ST_INSN, reg, addr, handler)
  333. #define STOREL(reg, addr, handler) EXC(sdl, ST_INSN, reg, addr, handler)
  334. #define STORER(reg, addr, handler) EXC(sdr, ST_INSN, reg, addr, handler)
  335. #define STORE(reg, addr, handler) EXC(sd, ST_INSN, reg, addr, handler)
  336. #define ADD daddu
  337. #define SUB dsubu
  338. #define SRL dsrl
  339. #define SLL dsll
  340. #define SLLV dsllv
  341. #define SRLV dsrlv
  342. #define NBYTES 8
  343. #define LOG_NBYTES 3
  344. #else
  345. #define LOADK lw /* No exception */
  346. #define LOAD(reg, addr, handler) EXC(lw, LD_INSN, reg, addr, handler)
  347. #define LOADBU(reg, addr, handler) EXC(lbu, LD_INSN, reg, addr, handler)
  348. #define LOADL(reg, addr, handler) EXC(lwl, LD_INSN, reg, addr, handler)
  349. #define LOADR(reg, addr, handler) EXC(lwr, LD_INSN, reg, addr, handler)
  350. #define STOREB(reg, addr, handler) EXC(sb, ST_INSN, reg, addr, handler)
  351. #define STOREL(reg, addr, handler) EXC(swl, ST_INSN, reg, addr, handler)
  352. #define STORER(reg, addr, handler) EXC(swr, ST_INSN, reg, addr, handler)
  353. #define STORE(reg, addr, handler) EXC(sw, ST_INSN, reg, addr, handler)
  354. #define ADD addu
  355. #define SUB subu
  356. #define SRL srl
  357. #define SLL sll
  358. #define SLLV sllv
  359. #define SRLV srlv
  360. #define NBYTES 4
  361. #define LOG_NBYTES 2
  362. #endif /* USE_DOUBLE */
  363. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  364. #define LDFIRST LOADR
  365. #define LDREST LOADL
  366. #define STFIRST STORER
  367. #define STREST STOREL
  368. #define SHIFT_DISCARD SLLV
  369. #define SHIFT_DISCARD_REVERT SRLV
  370. #else
  371. #define LDFIRST LOADL
  372. #define LDREST LOADR
  373. #define STFIRST STOREL
  374. #define STREST STORER
  375. #define SHIFT_DISCARD SRLV
  376. #define SHIFT_DISCARD_REVERT SLLV
  377. #endif
  378. #define FIRST(unit) ((unit)*NBYTES)
  379. #define REST(unit) (FIRST(unit)+NBYTES-1)
  380. #define ADDRMASK (NBYTES-1)
  381. #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
  382. .set noat
  383. #else
  384. .set at=v1
  385. #endif
  386. .macro __BUILD_CSUM_PARTIAL_COPY_USER mode, from, to, __nocheck
  387. PTR_ADDU AT, src, len /* See (1) above. */
  388. /* initialize __nocheck if this the first time we execute this
  389. * macro
  390. */
  391. #ifdef CONFIG_64BIT
  392. move errptr, a4
  393. #else
  394. lw errptr, 16(sp)
  395. #endif
  396. .if \__nocheck == 1
  397. FEXPORT(csum_partial_copy_nocheck)
  398. .endif
  399. move sum, zero
  400. move odd, zero
  401. /*
  402. * Note: dst & src may be unaligned, len may be 0
  403. * Temps
  404. */
  405. /*
  406. * The "issue break"s below are very approximate.
  407. * Issue delays for dcache fills will perturb the schedule, as will
  408. * load queue full replay traps, etc.
  409. *
  410. * If len < NBYTES use byte operations.
  411. */
  412. sltu t2, len, NBYTES
  413. and t1, dst, ADDRMASK
  414. bnez t2, .Lcopy_bytes_checklen\@
  415. and t0, src, ADDRMASK
  416. andi odd, dst, 0x1 /* odd buffer? */
  417. bnez t1, .Ldst_unaligned\@
  418. nop
  419. bnez t0, .Lsrc_unaligned_dst_aligned\@
  420. /*
  421. * use delay slot for fall-through
  422. * src and dst are aligned; need to compute rem
  423. */
  424. .Lboth_aligned\@:
  425. SRL t0, len, LOG_NBYTES+3 # +3 for 8 units/iter
  426. beqz t0, .Lcleanup_both_aligned\@ # len < 8*NBYTES
  427. nop
  428. SUB len, 8*NBYTES # subtract here for bgez loop
  429. .align 4
  430. 1:
  431. LOAD(t0, UNIT(0)(src), .Ll_exc\@)
  432. LOAD(t1, UNIT(1)(src), .Ll_exc_copy\@)
  433. LOAD(t2, UNIT(2)(src), .Ll_exc_copy\@)
  434. LOAD(t3, UNIT(3)(src), .Ll_exc_copy\@)
  435. LOAD(t4, UNIT(4)(src), .Ll_exc_copy\@)
  436. LOAD(t5, UNIT(5)(src), .Ll_exc_copy\@)
  437. LOAD(t6, UNIT(6)(src), .Ll_exc_copy\@)
  438. LOAD(t7, UNIT(7)(src), .Ll_exc_copy\@)
  439. SUB len, len, 8*NBYTES
  440. ADD src, src, 8*NBYTES
  441. STORE(t0, UNIT(0)(dst), .Ls_exc\@)
  442. ADDC(t0, t1)
  443. STORE(t1, UNIT(1)(dst), .Ls_exc\@)
  444. ADDC(sum, t0)
  445. STORE(t2, UNIT(2)(dst), .Ls_exc\@)
  446. ADDC(t2, t3)
  447. STORE(t3, UNIT(3)(dst), .Ls_exc\@)
  448. ADDC(sum, t2)
  449. STORE(t4, UNIT(4)(dst), .Ls_exc\@)
  450. ADDC(t4, t5)
  451. STORE(t5, UNIT(5)(dst), .Ls_exc\@)
  452. ADDC(sum, t4)
  453. STORE(t6, UNIT(6)(dst), .Ls_exc\@)
  454. ADDC(t6, t7)
  455. STORE(t7, UNIT(7)(dst), .Ls_exc\@)
  456. ADDC(sum, t6)
  457. .set reorder /* DADDI_WAR */
  458. ADD dst, dst, 8*NBYTES
  459. bgez len, 1b
  460. .set noreorder
  461. ADD len, 8*NBYTES # revert len (see above)
  462. /*
  463. * len == the number of bytes left to copy < 8*NBYTES
  464. */
  465. .Lcleanup_both_aligned\@:
  466. #define rem t7
  467. beqz len, .Ldone\@
  468. sltu t0, len, 4*NBYTES
  469. bnez t0, .Lless_than_4units\@
  470. and rem, len, (NBYTES-1) # rem = len % NBYTES
  471. /*
  472. * len >= 4*NBYTES
  473. */
  474. LOAD(t0, UNIT(0)(src), .Ll_exc\@)
  475. LOAD(t1, UNIT(1)(src), .Ll_exc_copy\@)
  476. LOAD(t2, UNIT(2)(src), .Ll_exc_copy\@)
  477. LOAD(t3, UNIT(3)(src), .Ll_exc_copy\@)
  478. SUB len, len, 4*NBYTES
  479. ADD src, src, 4*NBYTES
  480. STORE(t0, UNIT(0)(dst), .Ls_exc\@)
  481. ADDC(t0, t1)
  482. STORE(t1, UNIT(1)(dst), .Ls_exc\@)
  483. ADDC(sum, t0)
  484. STORE(t2, UNIT(2)(dst), .Ls_exc\@)
  485. ADDC(t2, t3)
  486. STORE(t3, UNIT(3)(dst), .Ls_exc\@)
  487. ADDC(sum, t2)
  488. .set reorder /* DADDI_WAR */
  489. ADD dst, dst, 4*NBYTES
  490. beqz len, .Ldone\@
  491. .set noreorder
  492. .Lless_than_4units\@:
  493. /*
  494. * rem = len % NBYTES
  495. */
  496. beq rem, len, .Lcopy_bytes\@
  497. nop
  498. 1:
  499. LOAD(t0, 0(src), .Ll_exc\@)
  500. ADD src, src, NBYTES
  501. SUB len, len, NBYTES
  502. STORE(t0, 0(dst), .Ls_exc\@)
  503. ADDC(sum, t0)
  504. .set reorder /* DADDI_WAR */
  505. ADD dst, dst, NBYTES
  506. bne rem, len, 1b
  507. .set noreorder
  508. /*
  509. * src and dst are aligned, need to copy rem bytes (rem < NBYTES)
  510. * A loop would do only a byte at a time with possible branch
  511. * mispredicts. Can't do an explicit LOAD dst,mask,or,STORE
  512. * because can't assume read-access to dst. Instead, use
  513. * STREST dst, which doesn't require read access to dst.
  514. *
  515. * This code should perform better than a simple loop on modern,
  516. * wide-issue mips processors because the code has fewer branches and
  517. * more instruction-level parallelism.
  518. */
  519. #define bits t2
  520. beqz len, .Ldone\@
  521. ADD t1, dst, len # t1 is just past last byte of dst
  522. li bits, 8*NBYTES
  523. SLL rem, len, 3 # rem = number of bits to keep
  524. LOAD(t0, 0(src), .Ll_exc\@)
  525. SUB bits, bits, rem # bits = number of bits to discard
  526. SHIFT_DISCARD t0, t0, bits
  527. STREST(t0, -1(t1), .Ls_exc\@)
  528. SHIFT_DISCARD_REVERT t0, t0, bits
  529. .set reorder
  530. ADDC(sum, t0)
  531. b .Ldone\@
  532. .set noreorder
  533. .Ldst_unaligned\@:
  534. /*
  535. * dst is unaligned
  536. * t0 = src & ADDRMASK
  537. * t1 = dst & ADDRMASK; T1 > 0
  538. * len >= NBYTES
  539. *
  540. * Copy enough bytes to align dst
  541. * Set match = (src and dst have same alignment)
  542. */
  543. #define match rem
  544. LDFIRST(t3, FIRST(0)(src), .Ll_exc\@)
  545. ADD t2, zero, NBYTES
  546. LDREST(t3, REST(0)(src), .Ll_exc_copy\@)
  547. SUB t2, t2, t1 # t2 = number of bytes copied
  548. xor match, t0, t1
  549. STFIRST(t3, FIRST(0)(dst), .Ls_exc\@)
  550. SLL t4, t1, 3 # t4 = number of bits to discard
  551. SHIFT_DISCARD t3, t3, t4
  552. /* no SHIFT_DISCARD_REVERT to handle odd buffer properly */
  553. ADDC(sum, t3)
  554. beq len, t2, .Ldone\@
  555. SUB len, len, t2
  556. ADD dst, dst, t2
  557. beqz match, .Lboth_aligned\@
  558. ADD src, src, t2
  559. .Lsrc_unaligned_dst_aligned\@:
  560. SRL t0, len, LOG_NBYTES+2 # +2 for 4 units/iter
  561. beqz t0, .Lcleanup_src_unaligned\@
  562. and rem, len, (4*NBYTES-1) # rem = len % 4*NBYTES
  563. 1:
  564. /*
  565. * Avoid consecutive LD*'s to the same register since some mips
  566. * implementations can't issue them in the same cycle.
  567. * It's OK to load FIRST(N+1) before REST(N) because the two addresses
  568. * are to the same unit (unless src is aligned, but it's not).
  569. */
  570. LDFIRST(t0, FIRST(0)(src), .Ll_exc\@)
  571. LDFIRST(t1, FIRST(1)(src), .Ll_exc_copy\@)
  572. SUB len, len, 4*NBYTES
  573. LDREST(t0, REST(0)(src), .Ll_exc_copy\@)
  574. LDREST(t1, REST(1)(src), .Ll_exc_copy\@)
  575. LDFIRST(t2, FIRST(2)(src), .Ll_exc_copy\@)
  576. LDFIRST(t3, FIRST(3)(src), .Ll_exc_copy\@)
  577. LDREST(t2, REST(2)(src), .Ll_exc_copy\@)
  578. LDREST(t3, REST(3)(src), .Ll_exc_copy\@)
  579. ADD src, src, 4*NBYTES
  580. #ifdef CONFIG_CPU_SB1
  581. nop # improves slotting
  582. #endif
  583. STORE(t0, UNIT(0)(dst), .Ls_exc\@)
  584. ADDC(t0, t1)
  585. STORE(t1, UNIT(1)(dst), .Ls_exc\@)
  586. ADDC(sum, t0)
  587. STORE(t2, UNIT(2)(dst), .Ls_exc\@)
  588. ADDC(t2, t3)
  589. STORE(t3, UNIT(3)(dst), .Ls_exc\@)
  590. ADDC(sum, t2)
  591. .set reorder /* DADDI_WAR */
  592. ADD dst, dst, 4*NBYTES
  593. bne len, rem, 1b
  594. .set noreorder
  595. .Lcleanup_src_unaligned\@:
  596. beqz len, .Ldone\@
  597. and rem, len, NBYTES-1 # rem = len % NBYTES
  598. beq rem, len, .Lcopy_bytes\@
  599. nop
  600. 1:
  601. LDFIRST(t0, FIRST(0)(src), .Ll_exc\@)
  602. LDREST(t0, REST(0)(src), .Ll_exc_copy\@)
  603. ADD src, src, NBYTES
  604. SUB len, len, NBYTES
  605. STORE(t0, 0(dst), .Ls_exc\@)
  606. ADDC(sum, t0)
  607. .set reorder /* DADDI_WAR */
  608. ADD dst, dst, NBYTES
  609. bne len, rem, 1b
  610. .set noreorder
  611. .Lcopy_bytes_checklen\@:
  612. beqz len, .Ldone\@
  613. nop
  614. .Lcopy_bytes\@:
  615. /* 0 < len < NBYTES */
  616. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  617. #define SHIFT_START 0
  618. #define SHIFT_INC 8
  619. #else
  620. #define SHIFT_START 8*(NBYTES-1)
  621. #define SHIFT_INC -8
  622. #endif
  623. move t2, zero # partial word
  624. li t3, SHIFT_START # shift
  625. /* use .Ll_exc_copy here to return correct sum on fault */
  626. #define COPY_BYTE(N) \
  627. LOADBU(t0, N(src), .Ll_exc_copy\@); \
  628. SUB len, len, 1; \
  629. STOREB(t0, N(dst), .Ls_exc\@); \
  630. SLLV t0, t0, t3; \
  631. addu t3, SHIFT_INC; \
  632. beqz len, .Lcopy_bytes_done\@; \
  633. or t2, t0
  634. COPY_BYTE(0)
  635. COPY_BYTE(1)
  636. #ifdef USE_DOUBLE
  637. COPY_BYTE(2)
  638. COPY_BYTE(3)
  639. COPY_BYTE(4)
  640. COPY_BYTE(5)
  641. #endif
  642. LOADBU(t0, NBYTES-2(src), .Ll_exc_copy\@)
  643. SUB len, len, 1
  644. STOREB(t0, NBYTES-2(dst), .Ls_exc\@)
  645. SLLV t0, t0, t3
  646. or t2, t0
  647. .Lcopy_bytes_done\@:
  648. ADDC(sum, t2)
  649. .Ldone\@:
  650. /* fold checksum */
  651. .set push
  652. .set noat
  653. #ifdef USE_DOUBLE
  654. dsll32 v1, sum, 0
  655. daddu sum, v1
  656. sltu v1, sum, v1
  657. dsra32 sum, sum, 0
  658. addu sum, v1
  659. #endif
  660. #if defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_LOONGSON3)
  661. .set push
  662. .set arch=mips32r2
  663. wsbh v1, sum
  664. movn sum, v1, odd
  665. .set pop
  666. #else
  667. beqz odd, 1f /* odd buffer alignment? */
  668. lui v1, 0x00ff
  669. addu v1, 0x00ff
  670. and t0, sum, v1
  671. sll t0, t0, 8
  672. srl sum, sum, 8
  673. and sum, sum, v1
  674. or sum, sum, t0
  675. 1:
  676. #endif
  677. .set pop
  678. .set reorder
  679. ADDC32(sum, psum)
  680. jr ra
  681. .set noreorder
  682. .Ll_exc_copy\@:
  683. /*
  684. * Copy bytes from src until faulting load address (or until a
  685. * lb faults)
  686. *
  687. * When reached by a faulting LDFIRST/LDREST, THREAD_BUADDR($28)
  688. * may be more than a byte beyond the last address.
  689. * Hence, the lb below may get an exception.
  690. *
  691. * Assumes src < THREAD_BUADDR($28)
  692. */
  693. LOADK t0, TI_TASK($28)
  694. li t2, SHIFT_START
  695. LOADK t0, THREAD_BUADDR(t0)
  696. 1:
  697. LOADBU(t1, 0(src), .Ll_exc\@)
  698. ADD src, src, 1
  699. sb t1, 0(dst) # can't fault -- we're copy_from_user
  700. SLLV t1, t1, t2
  701. addu t2, SHIFT_INC
  702. ADDC(sum, t1)
  703. .set reorder /* DADDI_WAR */
  704. ADD dst, dst, 1
  705. bne src, t0, 1b
  706. .set noreorder
  707. .Ll_exc\@:
  708. LOADK t0, TI_TASK($28)
  709. nop
  710. LOADK t0, THREAD_BUADDR(t0) # t0 is just past last good address
  711. nop
  712. SUB len, AT, t0 # len number of uncopied bytes
  713. /*
  714. * Here's where we rely on src and dst being incremented in tandem,
  715. * See (3) above.
  716. * dst += (fault addr - src) to put dst at first byte to clear
  717. */
  718. ADD dst, t0 # compute start address in a1
  719. SUB dst, src
  720. /*
  721. * Clear len bytes starting at dst. Can't call __bzero because it
  722. * might modify len. An inefficient loop for these rare times...
  723. */
  724. .set reorder /* DADDI_WAR */
  725. SUB src, len, 1
  726. beqz len, .Ldone\@
  727. .set noreorder
  728. 1: sb zero, 0(dst)
  729. ADD dst, dst, 1
  730. .set push
  731. .set noat
  732. #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
  733. bnez src, 1b
  734. SUB src, src, 1
  735. #else
  736. li v1, 1
  737. bnez src, 1b
  738. SUB src, src, v1
  739. #endif
  740. li v1, -EFAULT
  741. b .Ldone\@
  742. sw v1, (errptr)
  743. .Ls_exc\@:
  744. li v0, -1 /* invalid checksum */
  745. li v1, -EFAULT
  746. jr ra
  747. sw v1, (errptr)
  748. .set pop
  749. .endm
  750. LEAF(__csum_partial_copy_kernel)
  751. #ifndef CONFIG_EVA
  752. FEXPORT(__csum_partial_copy_to_user)
  753. FEXPORT(__csum_partial_copy_from_user)
  754. #endif
  755. __BUILD_CSUM_PARTIAL_COPY_USER LEGACY_MODE USEROP USEROP 1
  756. END(__csum_partial_copy_kernel)
  757. #ifdef CONFIG_EVA
  758. LEAF(__csum_partial_copy_to_user)
  759. __BUILD_CSUM_PARTIAL_COPY_USER EVA_MODE KERNELOP USEROP 0
  760. END(__csum_partial_copy_to_user)
  761. LEAF(__csum_partial_copy_from_user)
  762. __BUILD_CSUM_PARTIAL_COPY_USER EVA_MODE USEROP KERNELOP 0
  763. END(__csum_partial_copy_from_user)
  764. #endif