NGmemcpy.S 8.2 KB

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