csum_partial.S 19 KB

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