do_csum.S 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. *
  4. * Optmized version of the standard do_csum() function
  5. *
  6. * Return: a 64bit quantity containing the 16bit Internet checksum
  7. *
  8. * Inputs:
  9. * in0: address of buffer to checksum (char *)
  10. * in1: length of the buffer (int)
  11. *
  12. * Copyright (C) 1999, 2001-2002 Hewlett-Packard Co
  13. * Stephane Eranian <eranian@hpl.hp.com>
  14. *
  15. * 02/04/22 Ken Chen <kenneth.w.chen@intel.com>
  16. * Data locality study on the checksum buffer.
  17. * More optimization cleanup - remove excessive stop bits.
  18. * 02/04/08 David Mosberger <davidm@hpl.hp.com>
  19. * More cleanup and tuning.
  20. * 01/04/18 Jun Nakajima <jun.nakajima@intel.com>
  21. * Clean up and optimize and the software pipeline, loading two
  22. * back-to-back 8-byte words per loop. Clean up the initialization
  23. * for the loop. Support the cases where load latency = 1 or 2.
  24. * Set CONFIG_IA64_LOAD_LATENCY to 1 or 2 (default).
  25. */
  26. #include <asm/asmmacro.h>
  27. //
  28. // Theory of operations:
  29. // The goal is to go as quickly as possible to the point where
  30. // we can checksum 16 bytes/loop. Before reaching that point we must
  31. // take care of incorrect alignment of first byte.
  32. //
  33. // The code hereafter also takes care of the "tail" part of the buffer
  34. // before entering the core loop, if any. The checksum is a sum so it
  35. // allows us to commute operations. So we do the "head" and "tail"
  36. // first to finish at full speed in the body. Once we get the head and
  37. // tail values, we feed them into the pipeline, very handy initialization.
  38. //
  39. // Of course we deal with the special case where the whole buffer fits
  40. // into one 8 byte word. In this case we have only one entry in the pipeline.
  41. //
  42. // We use a (LOAD_LATENCY+2)-stage pipeline in the loop to account for
  43. // possible load latency and also to accommodate for head and tail.
  44. //
  45. // The end of the function deals with folding the checksum from 64bits
  46. // down to 16bits taking care of the carry.
  47. //
  48. // This version avoids synchronization in the core loop by also using a
  49. // pipeline for the accumulation of the checksum in resultx[] (x=1,2).
  50. //
  51. // wordx[] (x=1,2)
  52. // |---|
  53. // | | 0 : new value loaded in pipeline
  54. // |---|
  55. // | | - : in transit data
  56. // |---|
  57. // | | LOAD_LATENCY : current value to add to checksum
  58. // |---|
  59. // | | LOAD_LATENCY+1 : previous value added to checksum
  60. // |---| (previous iteration)
  61. //
  62. // resultx[] (x=1,2)
  63. // |---|
  64. // | | 0 : initial value
  65. // |---|
  66. // | | LOAD_LATENCY-1 : new checksum
  67. // |---|
  68. // | | LOAD_LATENCY : previous value of checksum
  69. // |---|
  70. // | | LOAD_LATENCY+1 : final checksum when out of the loop
  71. // |---|
  72. //
  73. //
  74. // See RFC1071 "Computing the Internet Checksum" for various techniques for
  75. // calculating the Internet checksum.
  76. //
  77. // NOT YET DONE:
  78. // - Maybe another algorithm which would take care of the folding at the
  79. // end in a different manner
  80. // - Work with people more knowledgeable than me on the network stack
  81. // to figure out if we could not split the function depending on the
  82. // type of packet or alignment we get. Like the ip_fast_csum() routine
  83. // where we know we have at least 20bytes worth of data to checksum.
  84. // - Do a better job of handling small packets.
  85. // - Note on prefetching: it was found that under various load, i.e. ftp read/write,
  86. // nfs read/write, the L1 cache hit rate is at 60% and L2 cache hit rate is at 99.8%
  87. // on the data that buffer points to (partly because the checksum is often preceded by
  88. // a copy_from_user()). This finding indiate that lfetch will not be beneficial since
  89. // the data is already in the cache.
  90. //
  91. #define saved_pfs r11
  92. #define hmask r16
  93. #define tmask r17
  94. #define first1 r18
  95. #define firstval r19
  96. #define firstoff r20
  97. #define last r21
  98. #define lastval r22
  99. #define lastoff r23
  100. #define saved_lc r24
  101. #define saved_pr r25
  102. #define tmp1 r26
  103. #define tmp2 r27
  104. #define tmp3 r28
  105. #define carry1 r29
  106. #define carry2 r30
  107. #define first2 r31
  108. #define buf in0
  109. #define len in1
  110. #define LOAD_LATENCY 2 // XXX fix me
  111. #if (LOAD_LATENCY != 1) && (LOAD_LATENCY != 2)
  112. # error "Only 1 or 2 is supported/tested for LOAD_LATENCY."
  113. #endif
  114. #define PIPE_DEPTH (LOAD_LATENCY+2)
  115. #define ELD p[LOAD_LATENCY] // end of load
  116. #define ELD_1 p[LOAD_LATENCY+1] // and next stage
  117. // unsigned long do_csum(unsigned char *buf,long len)
  118. GLOBAL_ENTRY(do_csum)
  119. .prologue
  120. .save ar.pfs, saved_pfs
  121. alloc saved_pfs=ar.pfs,2,16,0,16
  122. .rotr word1[4], word2[4],result1[LOAD_LATENCY+2],result2[LOAD_LATENCY+2]
  123. .rotp p[PIPE_DEPTH], pC1[2], pC2[2]
  124. mov ret0=r0 // in case we have zero length
  125. cmp.lt p0,p6=r0,len // check for zero length or negative (32bit len)
  126. ;;
  127. add tmp1=buf,len // last byte's address
  128. .save pr, saved_pr
  129. mov saved_pr=pr // preserve predicates (rotation)
  130. (p6) br.ret.spnt.many rp // return if zero or negative length
  131. mov hmask=-1 // initialize head mask
  132. tbit.nz p15,p0=buf,0 // is buf an odd address?
  133. and first1=-8,buf // 8-byte align down address of first1 element
  134. and firstoff=7,buf // how many bytes off for first1 element
  135. mov tmask=-1 // initialize tail mask
  136. ;;
  137. adds tmp2=-1,tmp1 // last-1
  138. and lastoff=7,tmp1 // how many bytes off for last element
  139. ;;
  140. sub tmp1=8,lastoff // complement to lastoff
  141. and last=-8,tmp2 // address of word containing last byte
  142. ;;
  143. sub tmp3=last,first1 // tmp3=distance from first1 to last
  144. .save ar.lc, saved_lc
  145. mov saved_lc=ar.lc // save lc
  146. cmp.eq p8,p9=last,first1 // everything fits in one word ?
  147. ld8 firstval=[first1],8 // load, ahead of time, "first1" word
  148. and tmp1=7, tmp1 // make sure that if tmp1==8 -> tmp1=0
  149. shl tmp2=firstoff,3 // number of bits
  150. ;;
  151. (p9) ld8 lastval=[last] // load, ahead of time, "last" word, if needed
  152. shl tmp1=tmp1,3 // number of bits
  153. (p9) adds tmp3=-8,tmp3 // effectively loaded
  154. ;;
  155. (p8) mov lastval=r0 // we don't need lastval if first1==last
  156. shl hmask=hmask,tmp2 // build head mask, mask off [0,first1off[
  157. shr.u tmask=tmask,tmp1 // build tail mask, mask off ]8,lastoff]
  158. ;;
  159. .body
  160. #define count tmp3
  161. (p8) and hmask=hmask,tmask // apply tail mask to head mask if 1 word only
  162. (p9) and word2[0]=lastval,tmask // mask last it as appropriate
  163. shr.u count=count,3 // how many 8-byte?
  164. ;;
  165. // If count is odd, finish this 8-byte word so that we can
  166. // load two back-to-back 8-byte words per loop thereafter.
  167. and word1[0]=firstval,hmask // and mask it as appropriate
  168. tbit.nz p10,p11=count,0 // if (count is odd)
  169. ;;
  170. (p8) mov result1[0]=word1[0]
  171. (p9) add result1[0]=word1[0],word2[0]
  172. ;;
  173. cmp.ltu p6,p0=result1[0],word1[0] // check the carry
  174. cmp.eq.or.andcm p8,p0=0,count // exit if zero 8-byte
  175. ;;
  176. (p6) adds result1[0]=1,result1[0]
  177. (p8) br.cond.dptk .do_csum_exit // if (within an 8-byte word)
  178. (p11) br.cond.dptk .do_csum16 // if (count is even)
  179. // Here count is odd.
  180. ld8 word1[1]=[first1],8 // load an 8-byte word
  181. cmp.eq p9,p10=1,count // if (count == 1)
  182. adds count=-1,count // loaded an 8-byte word
  183. ;;
  184. add result1[0]=result1[0],word1[1]
  185. ;;
  186. cmp.ltu p6,p0=result1[0],word1[1]
  187. ;;
  188. (p6) adds result1[0]=1,result1[0]
  189. (p9) br.cond.sptk .do_csum_exit // if (count == 1) exit
  190. // Fall through to calculate the checksum, feeding result1[0] as
  191. // the initial value in result1[0].
  192. //
  193. // Calculate the checksum loading two 8-byte words per loop.
  194. //
  195. .do_csum16:
  196. add first2=8,first1
  197. shr.u count=count,1 // we do 16 bytes per loop
  198. ;;
  199. adds count=-1,count
  200. mov carry1=r0
  201. mov carry2=r0
  202. brp.loop.imp 1f,2f
  203. ;;
  204. mov ar.ec=PIPE_DEPTH
  205. mov ar.lc=count // set lc
  206. mov pr.rot=1<<16
  207. // result1[0] must be initialized in advance.
  208. mov result2[0]=r0
  209. ;;
  210. .align 32
  211. 1:
  212. (ELD_1) cmp.ltu pC1[0],p0=result1[LOAD_LATENCY],word1[LOAD_LATENCY+1]
  213. (pC1[1])adds carry1=1,carry1
  214. (ELD_1) cmp.ltu pC2[0],p0=result2[LOAD_LATENCY],word2[LOAD_LATENCY+1]
  215. (pC2[1])adds carry2=1,carry2
  216. (ELD) add result1[LOAD_LATENCY-1]=result1[LOAD_LATENCY],word1[LOAD_LATENCY]
  217. (ELD) add result2[LOAD_LATENCY-1]=result2[LOAD_LATENCY],word2[LOAD_LATENCY]
  218. 2:
  219. (p[0]) ld8 word1[0]=[first1],16
  220. (p[0]) ld8 word2[0]=[first2],16
  221. br.ctop.sptk 1b
  222. ;;
  223. // Since len is a 32-bit value, carry cannot be larger than a 64-bit value.
  224. (pC1[1])adds carry1=1,carry1 // since we miss the last one
  225. (pC2[1])adds carry2=1,carry2
  226. ;;
  227. add result1[LOAD_LATENCY+1]=result1[LOAD_LATENCY+1],carry1
  228. add result2[LOAD_LATENCY+1]=result2[LOAD_LATENCY+1],carry2
  229. ;;
  230. cmp.ltu p6,p0=result1[LOAD_LATENCY+1],carry1
  231. cmp.ltu p7,p0=result2[LOAD_LATENCY+1],carry2
  232. ;;
  233. (p6) adds result1[LOAD_LATENCY+1]=1,result1[LOAD_LATENCY+1]
  234. (p7) adds result2[LOAD_LATENCY+1]=1,result2[LOAD_LATENCY+1]
  235. ;;
  236. add result1[0]=result1[LOAD_LATENCY+1],result2[LOAD_LATENCY+1]
  237. ;;
  238. cmp.ltu p6,p0=result1[0],result2[LOAD_LATENCY+1]
  239. ;;
  240. (p6) adds result1[0]=1,result1[0]
  241. ;;
  242. .do_csum_exit:
  243. //
  244. // now fold 64 into 16 bits taking care of carry
  245. // that's not very good because it has lots of sequentiality
  246. //
  247. mov tmp3=0xffff
  248. zxt4 tmp1=result1[0]
  249. shr.u tmp2=result1[0],32
  250. ;;
  251. add result1[0]=tmp1,tmp2
  252. ;;
  253. and tmp1=result1[0],tmp3
  254. shr.u tmp2=result1[0],16
  255. ;;
  256. add result1[0]=tmp1,tmp2
  257. ;;
  258. and tmp1=result1[0],tmp3
  259. shr.u tmp2=result1[0],16
  260. ;;
  261. add result1[0]=tmp1,tmp2
  262. ;;
  263. and tmp1=result1[0],tmp3
  264. shr.u tmp2=result1[0],16
  265. ;;
  266. add ret0=tmp1,tmp2
  267. mov pr=saved_pr,0xffffffffffff0000
  268. ;;
  269. // if buf was odd then swap bytes
  270. mov ar.pfs=saved_pfs // restore ar.ec
  271. (p15) mux1 ret0=ret0,@rev // reverse word
  272. ;;
  273. mov ar.lc=saved_lc
  274. (p15) shr.u ret0=ret0,64-16 // + shift back to position = swap bytes
  275. br.ret.sptk.many rp
  276. // I (Jun Nakajima) wrote an equivalent code (see below), but it was
  277. // not much better than the original. So keep the original there so that
  278. // someone else can challenge.
  279. //
  280. // shr.u word1[0]=result1[0],32
  281. // zxt4 result1[0]=result1[0]
  282. // ;;
  283. // add result1[0]=result1[0],word1[0]
  284. // ;;
  285. // zxt2 result2[0]=result1[0]
  286. // extr.u word1[0]=result1[0],16,16
  287. // shr.u carry1=result1[0],32
  288. // ;;
  289. // add result2[0]=result2[0],word1[0]
  290. // ;;
  291. // add result2[0]=result2[0],carry1
  292. // ;;
  293. // extr.u ret0=result2[0],16,16
  294. // ;;
  295. // add ret0=ret0,result2[0]
  296. // ;;
  297. // zxt2 ret0=ret0
  298. // mov ar.pfs=saved_pfs // restore ar.ec
  299. // mov pr=saved_pr,0xffffffffffff0000
  300. // ;;
  301. // // if buf was odd then swap bytes
  302. // mov ar.lc=saved_lc
  303. //(p15) mux1 ret0=ret0,@rev // reverse word
  304. // ;;
  305. //(p15) shr.u ret0=ret0,64-16 // + shift back to position = swap bytes
  306. // br.ret.sptk.many rp
  307. END(do_csum)