NGmemcpy.S 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* NGmemcpy.S: Niagara optimized memcpy.
  2. *
  3. * Copyright (C) 2006 David S. Miller (davem@davemloft.net)
  4. */
  5. #ifdef __KERNEL__
  6. #include <asm/asi.h>
  7. #define GLOBAL_SPARE %g7
  8. #define RESTORE_ASI wr %g0, ASI_AIUS, %asi
  9. #else
  10. #define GLOBAL_SPARE %g5
  11. #define RESTORE_ASI
  12. #endif
  13. #ifndef STORE_ASI
  14. #define STORE_ASI ASI_BLK_INIT_QUAD_LDD_P
  15. #endif
  16. #ifndef EX_LD
  17. #define EX_LD(x) x
  18. #endif
  19. #ifndef EX_ST
  20. #define EX_ST(x) x
  21. #endif
  22. #ifndef EX_RETVAL
  23. #define EX_RETVAL(x) x
  24. #endif
  25. #ifndef LOAD
  26. #ifndef MEMCPY_DEBUG
  27. #define LOAD(type,addr,dest) type [addr], dest
  28. #else
  29. #define LOAD(type,addr,dest) type##a [addr] 0x80, dest
  30. #endif
  31. #endif
  32. #ifndef LOAD_TWIN
  33. #define LOAD_TWIN(addr_reg,dest0,dest1) \
  34. ldda [addr_reg] ASI_BLK_INIT_QUAD_LDD_P, dest0
  35. #endif
  36. #ifndef STORE
  37. #define STORE(type,src,addr) type src, [addr]
  38. #endif
  39. #ifndef STORE_INIT
  40. #define STORE_INIT(src,addr) stxa src, [addr] %asi
  41. #endif
  42. #ifndef FUNC_NAME
  43. #define FUNC_NAME NGmemcpy
  44. #endif
  45. #ifndef PREAMBLE
  46. #define PREAMBLE
  47. #endif
  48. #ifndef XCC
  49. #define XCC xcc
  50. #endif
  51. .register %g2,#scratch
  52. .register %g3,#scratch
  53. .text
  54. .align 64
  55. .globl FUNC_NAME
  56. .type FUNC_NAME,#function
  57. FUNC_NAME: /* %o0=dst, %o1=src, %o2=len */
  58. srlx %o2, 31, %g2
  59. cmp %g2, 0
  60. tne %xcc, 5
  61. PREAMBLE
  62. mov %o0, GLOBAL_SPARE
  63. cmp %o2, 0
  64. be,pn %XCC, 85f
  65. or %o0, %o1, %o3
  66. cmp %o2, 16
  67. blu,a,pn %XCC, 80f
  68. or %o3, %o2, %o3
  69. /* 2 blocks (128 bytes) is the minimum we can do the block
  70. * copy with. We need to ensure that we'll iterate at least
  71. * once in the block copy loop. At worst we'll need to align
  72. * the destination to a 64-byte boundary which can chew up
  73. * to (64 - 1) bytes from the length before we perform the
  74. * block copy loop.
  75. */
  76. cmp %o2, (2 * 64)
  77. blu,pt %XCC, 70f
  78. andcc %o3, 0x7, %g0
  79. /* %o0: dst
  80. * %o1: src
  81. * %o2: len (known to be >= 128)
  82. *
  83. * The block copy loops will use %o4/%o5,%g2/%g3 as
  84. * temporaries while copying the data.
  85. */
  86. LOAD(prefetch, %o1, #one_read)
  87. wr %g0, STORE_ASI, %asi
  88. /* Align destination on 64-byte boundary. */
  89. andcc %o0, (64 - 1), %o4
  90. be,pt %XCC, 2f
  91. sub %o4, 64, %o4
  92. sub %g0, %o4, %o4 ! bytes to align dst
  93. sub %o2, %o4, %o2
  94. 1: subcc %o4, 1, %o4
  95. EX_LD(LOAD(ldub, %o1, %g1))
  96. EX_ST(STORE(stb, %g1, %o0))
  97. add %o1, 1, %o1
  98. bne,pt %XCC, 1b
  99. add %o0, 1, %o0
  100. /* If the source is on a 16-byte boundary we can do
  101. * the direct block copy loop. If it is 8-byte aligned
  102. * we can do the 16-byte loads offset by -8 bytes and the
  103. * init stores offset by one register.
  104. *
  105. * If the source is not even 8-byte aligned, we need to do
  106. * shifting and masking (basically integer faligndata).
  107. *
  108. * The careful bit with init stores is that if we store
  109. * to any part of the cache line we have to store the whole
  110. * cacheline else we can end up with corrupt L2 cache line
  111. * contents. Since the loop works on 64-bytes of 64-byte
  112. * aligned store data at a time, this is easy to ensure.
  113. */
  114. 2:
  115. andcc %o1, (16 - 1), %o4
  116. andn %o2, (64 - 1), %g1 ! block copy loop iterator
  117. sub %o2, %g1, %o2 ! final sub-block copy bytes
  118. be,pt %XCC, 50f
  119. cmp %o4, 8
  120. be,a,pt %XCC, 10f
  121. sub %o1, 0x8, %o1
  122. /* Neither 8-byte nor 16-byte aligned, shift and mask. */
  123. mov %g1, %o4
  124. and %o1, 0x7, %g1
  125. sll %g1, 3, %g1
  126. mov 64, %o3
  127. andn %o1, 0x7, %o1
  128. EX_LD(LOAD(ldx, %o1, %g2))
  129. sub %o3, %g1, %o3
  130. sllx %g2, %g1, %g2
  131. #define SWIVEL_ONE_DWORD(SRC, TMP1, TMP2, PRE_VAL, PRE_SHIFT, POST_SHIFT, DST)\
  132. EX_LD(LOAD(ldx, SRC, TMP1)); \
  133. srlx TMP1, PRE_SHIFT, TMP2; \
  134. or TMP2, PRE_VAL, TMP2; \
  135. EX_ST(STORE_INIT(TMP2, DST)); \
  136. sllx TMP1, POST_SHIFT, PRE_VAL;
  137. 1: add %o1, 0x8, %o1
  138. SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x00)
  139. add %o1, 0x8, %o1
  140. SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x08)
  141. add %o1, 0x8, %o1
  142. SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x10)
  143. add %o1, 0x8, %o1
  144. SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x18)
  145. add %o1, 32, %o1
  146. LOAD(prefetch, %o1, #one_read)
  147. sub %o1, 32 - 8, %o1
  148. SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x20)
  149. add %o1, 8, %o1
  150. SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x28)
  151. add %o1, 8, %o1
  152. SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x30)
  153. add %o1, 8, %o1
  154. SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x38)
  155. subcc %o4, 64, %o4
  156. bne,pt %XCC, 1b
  157. add %o0, 64, %o0
  158. #undef SWIVEL_ONE_DWORD
  159. srl %g1, 3, %g1
  160. ba,pt %XCC, 60f
  161. add %o1, %g1, %o1
  162. 10: /* Destination is 64-byte aligned, source was only 8-byte
  163. * aligned but it has been subtracted by 8 and we perform
  164. * one twin load ahead, then add 8 back into source when
  165. * we finish the loop.
  166. */
  167. EX_LD(LOAD_TWIN(%o1, %o4, %o5))
  168. 1: add %o1, 16, %o1
  169. EX_LD(LOAD_TWIN(%o1, %g2, %g3))
  170. add %o1, 16 + 32, %o1
  171. LOAD(prefetch, %o1, #one_read)
  172. sub %o1, 32, %o1
  173. EX_ST(STORE_INIT(%o5, %o0 + 0x00)) ! initializes cache line
  174. EX_ST(STORE_INIT(%g2, %o0 + 0x08))
  175. EX_LD(LOAD_TWIN(%o1, %o4, %o5))
  176. add %o1, 16, %o1
  177. EX_ST(STORE_INIT(%g3, %o0 + 0x10))
  178. EX_ST(STORE_INIT(%o4, %o0 + 0x18))
  179. EX_LD(LOAD_TWIN(%o1, %g2, %g3))
  180. add %o1, 16, %o1
  181. EX_ST(STORE_INIT(%o5, %o0 + 0x20))
  182. EX_ST(STORE_INIT(%g2, %o0 + 0x28))
  183. EX_LD(LOAD_TWIN(%o1, %o4, %o5))
  184. EX_ST(STORE_INIT(%g3, %o0 + 0x30))
  185. EX_ST(STORE_INIT(%o4, %o0 + 0x38))
  186. subcc %g1, 64, %g1
  187. bne,pt %XCC, 1b
  188. add %o0, 64, %o0
  189. ba,pt %XCC, 60f
  190. add %o1, 0x8, %o1
  191. 50: /* Destination is 64-byte aligned, and source is 16-byte
  192. * aligned.
  193. */
  194. 1: EX_LD(LOAD_TWIN(%o1, %o4, %o5))
  195. add %o1, 16, %o1
  196. EX_LD(LOAD_TWIN(%o1, %g2, %g3))
  197. add %o1, 16 + 32, %o1
  198. LOAD(prefetch, %o1, #one_read)
  199. sub %o1, 32, %o1
  200. EX_ST(STORE_INIT(%o4, %o0 + 0x00)) ! initializes cache line
  201. EX_ST(STORE_INIT(%o5, %o0 + 0x08))
  202. EX_LD(LOAD_TWIN(%o1, %o4, %o5))
  203. add %o1, 16, %o1
  204. EX_ST(STORE_INIT(%g2, %o0 + 0x10))
  205. EX_ST(STORE_INIT(%g3, %o0 + 0x18))
  206. EX_LD(LOAD_TWIN(%o1, %g2, %g3))
  207. add %o1, 16, %o1
  208. EX_ST(STORE_INIT(%o4, %o0 + 0x20))
  209. EX_ST(STORE_INIT(%o5, %o0 + 0x28))
  210. EX_ST(STORE_INIT(%g2, %o0 + 0x30))
  211. EX_ST(STORE_INIT(%g3, %o0 + 0x38))
  212. subcc %g1, 64, %g1
  213. bne,pt %XCC, 1b
  214. add %o0, 64, %o0
  215. /* fall through */
  216. 60:
  217. /* %o2 contains any final bytes still needed to be copied
  218. * over. If anything is left, we copy it one byte at a time.
  219. */
  220. RESTORE_ASI
  221. brz,pt %o2, 85f
  222. sub %o0, %o1, %o3
  223. ba,a,pt %XCC, 90f
  224. .align 64
  225. 70: /* 16 < len <= 64 */
  226. bne,pn %XCC, 75f
  227. sub %o0, %o1, %o3
  228. 72:
  229. andn %o2, 0xf, %o4
  230. and %o2, 0xf, %o2
  231. 1: subcc %o4, 0x10, %o4
  232. EX_LD(LOAD(ldx, %o1, %o5))
  233. add %o1, 0x08, %o1
  234. EX_LD(LOAD(ldx, %o1, %g1))
  235. sub %o1, 0x08, %o1
  236. EX_ST(STORE(stx, %o5, %o1 + %o3))
  237. add %o1, 0x8, %o1
  238. EX_ST(STORE(stx, %g1, %o1 + %o3))
  239. bgu,pt %XCC, 1b
  240. add %o1, 0x8, %o1
  241. 73: andcc %o2, 0x8, %g0
  242. be,pt %XCC, 1f
  243. nop
  244. sub %o2, 0x8, %o2
  245. EX_LD(LOAD(ldx, %o1, %o5))
  246. EX_ST(STORE(stx, %o5, %o1 + %o3))
  247. add %o1, 0x8, %o1
  248. 1: andcc %o2, 0x4, %g0
  249. be,pt %XCC, 1f
  250. nop
  251. sub %o2, 0x4, %o2
  252. EX_LD(LOAD(lduw, %o1, %o5))
  253. EX_ST(STORE(stw, %o5, %o1 + %o3))
  254. add %o1, 0x4, %o1
  255. 1: cmp %o2, 0
  256. be,pt %XCC, 85f
  257. nop
  258. ba,pt %xcc, 90f
  259. nop
  260. 75:
  261. andcc %o0, 0x7, %g1
  262. sub %g1, 0x8, %g1
  263. be,pn %icc, 2f
  264. sub %g0, %g1, %g1
  265. sub %o2, %g1, %o2
  266. 1: subcc %g1, 1, %g1
  267. EX_LD(LOAD(ldub, %o1, %o5))
  268. EX_ST(STORE(stb, %o5, %o1 + %o3))
  269. bgu,pt %icc, 1b
  270. add %o1, 1, %o1
  271. 2: add %o1, %o3, %o0
  272. andcc %o1, 0x7, %g1
  273. bne,pt %icc, 8f
  274. sll %g1, 3, %g1
  275. cmp %o2, 16
  276. bgeu,pt %icc, 72b
  277. nop
  278. ba,a,pt %xcc, 73b
  279. 8: mov 64, %o3
  280. andn %o1, 0x7, %o1
  281. EX_LD(LOAD(ldx, %o1, %g2))
  282. sub %o3, %g1, %o3
  283. andn %o2, 0x7, %o4
  284. sllx %g2, %g1, %g2
  285. 1: add %o1, 0x8, %o1
  286. EX_LD(LOAD(ldx, %o1, %g3))
  287. subcc %o4, 0x8, %o4
  288. srlx %g3, %o3, %o5
  289. or %o5, %g2, %o5
  290. EX_ST(STORE(stx, %o5, %o0))
  291. add %o0, 0x8, %o0
  292. bgu,pt %icc, 1b
  293. sllx %g3, %g1, %g2
  294. srl %g1, 3, %g1
  295. andcc %o2, 0x7, %o2
  296. be,pn %icc, 85f
  297. add %o1, %g1, %o1
  298. ba,pt %xcc, 90f
  299. sub %o0, %o1, %o3
  300. .align 64
  301. 80: /* 0 < len <= 16 */
  302. andcc %o3, 0x3, %g0
  303. bne,pn %XCC, 90f
  304. sub %o0, %o1, %o3
  305. 1:
  306. subcc %o2, 4, %o2
  307. EX_LD(LOAD(lduw, %o1, %g1))
  308. EX_ST(STORE(stw, %g1, %o1 + %o3))
  309. bgu,pt %XCC, 1b
  310. add %o1, 4, %o1
  311. 85: retl
  312. mov EX_RETVAL(GLOBAL_SPARE), %o0
  313. .align 32
  314. 90:
  315. subcc %o2, 1, %o2
  316. EX_LD(LOAD(ldub, %o1, %g1))
  317. EX_ST(STORE(stb, %g1, %o1 + %o3))
  318. bgu,pt %XCC, 90b
  319. add %o1, 1, %o1
  320. retl
  321. mov EX_RETVAL(GLOBAL_SPARE), %o0
  322. .size FUNC_NAME, .-FUNC_NAME