checksum_64.S 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. #include <linux/sys.h>
  15. #include <asm/processor.h>
  16. #include <asm/errno.h>
  17. #include <asm/ppc_asm.h>
  18. /*
  19. * ip_fast_csum(r3=buf, r4=len) -- Optimized for IP header
  20. * len is in words and is always >= 5.
  21. *
  22. * In practice len == 5, but this is not guaranteed. So this code does not
  23. * attempt to use doubleword instructions.
  24. */
  25. _GLOBAL(ip_fast_csum)
  26. lwz r0,0(r3)
  27. lwzu r5,4(r3)
  28. addic. r4,r4,-2
  29. addc r0,r0,r5
  30. mtctr r4
  31. blelr-
  32. 1: lwzu r4,4(r3)
  33. adde r0,r0,r4
  34. bdnz 1b
  35. addze r0,r0 /* add in final carry */
  36. rldicl r4,r0,32,0 /* fold two 32-bit halves together */
  37. add r0,r0,r4
  38. srdi r0,r0,32
  39. rlwinm r3,r0,16,0,31 /* fold two halves together */
  40. add r3,r0,r3
  41. not r3,r3
  42. srwi r3,r3,16
  43. blr
  44. /*
  45. * Compute checksum of TCP or UDP pseudo-header:
  46. * csum_tcpudp_magic(r3=saddr, r4=daddr, r5=len, r6=proto, r7=sum)
  47. * No real gain trying to do this specially for 64 bit, but
  48. * the 32 bit addition may spill into the upper bits of
  49. * the doubleword so we still must fold it down from 64.
  50. */
  51. _GLOBAL(csum_tcpudp_magic)
  52. rlwimi r5,r6,16,0,15 /* put proto in upper half of len */
  53. addc r0,r3,r4 /* add 4 32-bit words together */
  54. adde r0,r0,r5
  55. adde r0,r0,r7
  56. rldicl r4,r0,32,0 /* fold 64 bit value */
  57. add r0,r4,r0
  58. srdi r0,r0,32
  59. rlwinm r3,r0,16,0,31 /* fold two halves together */
  60. add r3,r0,r3
  61. not r3,r3
  62. srwi r3,r3,16
  63. blr
  64. /*
  65. * Computes the checksum of a memory block at buff, length len,
  66. * and adds in "sum" (32-bit).
  67. *
  68. * csum_partial(r3=buff, r4=len, r5=sum)
  69. */
  70. _GLOBAL(csum_partial)
  71. addic r0,r5,0 /* clear carry */
  72. srdi. r6,r4,3 /* less than 8 bytes? */
  73. beq .Lcsum_tail_word
  74. /*
  75. * If only halfword aligned, align to a double word. Since odd
  76. * aligned addresses should be rare and they would require more
  77. * work to calculate the correct checksum, we ignore that case
  78. * and take the potential slowdown of unaligned loads.
  79. */
  80. rldicl. r6,r3,64-1,64-2 /* r6 = (r3 & 0x3) >> 1 */
  81. beq .Lcsum_aligned
  82. li r7,4
  83. sub r6,r7,r6
  84. mtctr r6
  85. 1:
  86. lhz r6,0(r3) /* align to doubleword */
  87. subi r4,r4,2
  88. addi r3,r3,2
  89. adde r0,r0,r6
  90. bdnz 1b
  91. .Lcsum_aligned:
  92. /*
  93. * We unroll the loop such that each iteration is 64 bytes with an
  94. * entry and exit limb of 64 bytes, meaning a minimum size of
  95. * 128 bytes.
  96. */
  97. srdi. r6,r4,7
  98. beq .Lcsum_tail_doublewords /* len < 128 */
  99. srdi r6,r4,6
  100. subi r6,r6,1
  101. mtctr r6
  102. stdu r1,-STACKFRAMESIZE(r1)
  103. std r14,STK_REG(R14)(r1)
  104. std r15,STK_REG(R15)(r1)
  105. std r16,STK_REG(R16)(r1)
  106. ld r6,0(r3)
  107. ld r9,8(r3)
  108. ld r10,16(r3)
  109. ld r11,24(r3)
  110. /*
  111. * On POWER6 and POWER7 back to back addes take 2 cycles because of
  112. * the XER dependency. This means the fastest this loop can go is
  113. * 16 cycles per iteration. The scheduling of the loop below has
  114. * been shown to hit this on both POWER6 and POWER7.
  115. */
  116. .align 5
  117. 2:
  118. adde r0,r0,r6
  119. ld r12,32(r3)
  120. ld r14,40(r3)
  121. adde r0,r0,r9
  122. ld r15,48(r3)
  123. ld r16,56(r3)
  124. addi r3,r3,64
  125. adde r0,r0,r10
  126. adde r0,r0,r11
  127. adde r0,r0,r12
  128. adde r0,r0,r14
  129. adde r0,r0,r15
  130. ld r6,0(r3)
  131. ld r9,8(r3)
  132. adde r0,r0,r16
  133. ld r10,16(r3)
  134. ld r11,24(r3)
  135. bdnz 2b
  136. adde r0,r0,r6
  137. ld r12,32(r3)
  138. ld r14,40(r3)
  139. adde r0,r0,r9
  140. ld r15,48(r3)
  141. ld r16,56(r3)
  142. addi r3,r3,64
  143. adde r0,r0,r10
  144. adde r0,r0,r11
  145. adde r0,r0,r12
  146. adde r0,r0,r14
  147. adde r0,r0,r15
  148. adde r0,r0,r16
  149. ld r14,STK_REG(R14)(r1)
  150. ld r15,STK_REG(R15)(r1)
  151. ld r16,STK_REG(R16)(r1)
  152. addi r1,r1,STACKFRAMESIZE
  153. andi. r4,r4,63
  154. .Lcsum_tail_doublewords: /* Up to 127 bytes to go */
  155. srdi. r6,r4,3
  156. beq .Lcsum_tail_word
  157. mtctr r6
  158. 3:
  159. ld r6,0(r3)
  160. addi r3,r3,8
  161. adde r0,r0,r6
  162. bdnz 3b
  163. andi. r4,r4,7
  164. .Lcsum_tail_word: /* Up to 7 bytes to go */
  165. srdi. r6,r4,2
  166. beq .Lcsum_tail_halfword
  167. lwz r6,0(r3)
  168. addi r3,r3,4
  169. adde r0,r0,r6
  170. subi r4,r4,4
  171. .Lcsum_tail_halfword: /* Up to 3 bytes to go */
  172. srdi. r6,r4,1
  173. beq .Lcsum_tail_byte
  174. lhz r6,0(r3)
  175. addi r3,r3,2
  176. adde r0,r0,r6
  177. subi r4,r4,2
  178. .Lcsum_tail_byte: /* Up to 1 byte to go */
  179. andi. r6,r4,1
  180. beq .Lcsum_finish
  181. lbz r6,0(r3)
  182. sldi r9,r6,8 /* Pad the byte out to 16 bits */
  183. adde r0,r0,r9
  184. .Lcsum_finish:
  185. addze r0,r0 /* add in final carry */
  186. rldicl r4,r0,32,0 /* fold two 32 bit halves together */
  187. add r3,r4,r0
  188. srdi r3,r3,32
  189. blr
  190. .macro source
  191. 100:
  192. .section __ex_table,"a"
  193. .align 3
  194. .llong 100b,.Lsrc_error
  195. .previous
  196. .endm
  197. .macro dest
  198. 200:
  199. .section __ex_table,"a"
  200. .align 3
  201. .llong 200b,.Ldest_error
  202. .previous
  203. .endm
  204. /*
  205. * Computes the checksum of a memory block at src, length len,
  206. * and adds in "sum" (32-bit), while copying the block to dst.
  207. * If an access exception occurs on src or dst, it stores -EFAULT
  208. * to *src_err or *dst_err respectively. The caller must take any action
  209. * required in this case (zeroing memory, recalculating partial checksum etc).
  210. *
  211. * csum_partial_copy_generic(r3=src, r4=dst, r5=len, r6=sum, r7=src_err, r8=dst_err)
  212. */
  213. _GLOBAL(csum_partial_copy_generic)
  214. addic r0,r6,0 /* clear carry */
  215. srdi. r6,r5,3 /* less than 8 bytes? */
  216. beq .Lcopy_tail_word
  217. /*
  218. * If only halfword aligned, align to a double word. Since odd
  219. * aligned addresses should be rare and they would require more
  220. * work to calculate the correct checksum, we ignore that case
  221. * and take the potential slowdown of unaligned loads.
  222. *
  223. * If the source and destination are relatively unaligned we only
  224. * align the source. This keeps things simple.
  225. */
  226. rldicl. r6,r3,64-1,64-2 /* r6 = (r3 & 0x3) >> 1 */
  227. beq .Lcopy_aligned
  228. li r7,4
  229. sub r6,r7,r6
  230. mtctr r6
  231. 1:
  232. source; lhz r6,0(r3) /* align to doubleword */
  233. subi r5,r5,2
  234. addi r3,r3,2
  235. adde r0,r0,r6
  236. dest; sth r6,0(r4)
  237. addi r4,r4,2
  238. bdnz 1b
  239. .Lcopy_aligned:
  240. /*
  241. * We unroll the loop such that each iteration is 64 bytes with an
  242. * entry and exit limb of 64 bytes, meaning a minimum size of
  243. * 128 bytes.
  244. */
  245. srdi. r6,r5,7
  246. beq .Lcopy_tail_doublewords /* len < 128 */
  247. srdi r6,r5,6
  248. subi r6,r6,1
  249. mtctr r6
  250. stdu r1,-STACKFRAMESIZE(r1)
  251. std r14,STK_REG(R14)(r1)
  252. std r15,STK_REG(R15)(r1)
  253. std r16,STK_REG(R16)(r1)
  254. source; ld r6,0(r3)
  255. source; ld r9,8(r3)
  256. source; ld r10,16(r3)
  257. source; ld r11,24(r3)
  258. /*
  259. * On POWER6 and POWER7 back to back addes take 2 cycles because of
  260. * the XER dependency. This means the fastest this loop can go is
  261. * 16 cycles per iteration. The scheduling of the loop below has
  262. * been shown to hit this on both POWER6 and POWER7.
  263. */
  264. .align 5
  265. 2:
  266. adde r0,r0,r6
  267. source; ld r12,32(r3)
  268. source; ld r14,40(r3)
  269. adde r0,r0,r9
  270. source; ld r15,48(r3)
  271. source; ld r16,56(r3)
  272. addi r3,r3,64
  273. adde r0,r0,r10
  274. dest; std r6,0(r4)
  275. dest; std r9,8(r4)
  276. adde r0,r0,r11
  277. dest; std r10,16(r4)
  278. dest; std r11,24(r4)
  279. adde r0,r0,r12
  280. dest; std r12,32(r4)
  281. dest; std r14,40(r4)
  282. adde r0,r0,r14
  283. dest; std r15,48(r4)
  284. dest; std r16,56(r4)
  285. addi r4,r4,64
  286. adde r0,r0,r15
  287. source; ld r6,0(r3)
  288. source; ld r9,8(r3)
  289. adde r0,r0,r16
  290. source; ld r10,16(r3)
  291. source; ld r11,24(r3)
  292. bdnz 2b
  293. adde r0,r0,r6
  294. source; ld r12,32(r3)
  295. source; ld r14,40(r3)
  296. adde r0,r0,r9
  297. source; ld r15,48(r3)
  298. source; ld r16,56(r3)
  299. addi r3,r3,64
  300. adde r0,r0,r10
  301. dest; std r6,0(r4)
  302. dest; std r9,8(r4)
  303. adde r0,r0,r11
  304. dest; std r10,16(r4)
  305. dest; std r11,24(r4)
  306. adde r0,r0,r12
  307. dest; std r12,32(r4)
  308. dest; std r14,40(r4)
  309. adde r0,r0,r14
  310. dest; std r15,48(r4)
  311. dest; std r16,56(r4)
  312. addi r4,r4,64
  313. adde r0,r0,r15
  314. adde r0,r0,r16
  315. ld r14,STK_REG(R14)(r1)
  316. ld r15,STK_REG(R15)(r1)
  317. ld r16,STK_REG(R16)(r1)
  318. addi r1,r1,STACKFRAMESIZE
  319. andi. r5,r5,63
  320. .Lcopy_tail_doublewords: /* Up to 127 bytes to go */
  321. srdi. r6,r5,3
  322. beq .Lcopy_tail_word
  323. mtctr r6
  324. 3:
  325. source; ld r6,0(r3)
  326. addi r3,r3,8
  327. adde r0,r0,r6
  328. dest; std r6,0(r4)
  329. addi r4,r4,8
  330. bdnz 3b
  331. andi. r5,r5,7
  332. .Lcopy_tail_word: /* Up to 7 bytes to go */
  333. srdi. r6,r5,2
  334. beq .Lcopy_tail_halfword
  335. source; lwz r6,0(r3)
  336. addi r3,r3,4
  337. adde r0,r0,r6
  338. dest; stw r6,0(r4)
  339. addi r4,r4,4
  340. subi r5,r5,4
  341. .Lcopy_tail_halfword: /* Up to 3 bytes to go */
  342. srdi. r6,r5,1
  343. beq .Lcopy_tail_byte
  344. source; lhz r6,0(r3)
  345. addi r3,r3,2
  346. adde r0,r0,r6
  347. dest; sth r6,0(r4)
  348. addi r4,r4,2
  349. subi r5,r5,2
  350. .Lcopy_tail_byte: /* Up to 1 byte to go */
  351. andi. r6,r5,1
  352. beq .Lcopy_finish
  353. source; lbz r6,0(r3)
  354. sldi r9,r6,8 /* Pad the byte out to 16 bits */
  355. adde r0,r0,r9
  356. dest; stb r6,0(r4)
  357. .Lcopy_finish:
  358. addze r0,r0 /* add in final carry */
  359. rldicl r4,r0,32,0 /* fold two 32 bit halves together */
  360. add r3,r4,r0
  361. srdi r3,r3,32
  362. blr
  363. .Lsrc_error:
  364. cmpdi 0,r7,0
  365. beqlr
  366. li r6,-EFAULT
  367. stw r6,0(r7)
  368. blr
  369. .Ldest_error:
  370. cmpdi 0,r8,0
  371. beqlr
  372. li r6,-EFAULT
  373. stw r6,0(r8)
  374. blr