NGmemcpy.S 9.4 KB

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