1 /* NGmemcpy.S: Niagara optimized memcpy.
3 * Copyright (C) 2006 David S. Miller (davem@davemloft.net)
8 #define GLOBAL_SPARE %g7
9 #define RESTORE_ASI wr %g0, ASI_AIUS, %asi
11 #define GLOBAL_SPARE %g5
16 #define STORE_ASI ASI_BLK_INIT_QUAD_LDD_P
28 #define EX_RETVAL(x) x
33 #define LOAD(type,addr,dest) type [addr], dest
35 #define LOAD(type,addr,dest) type##a [addr] 0x80, dest
40 #define LOAD_TWIN(addr_reg,dest0,dest1) \
41 ldda [addr_reg] ASI_BLK_INIT_QUAD_LDD_P, dest0
45 #define STORE(type,src,addr) type src, [addr]
49 #define STORE_INIT(src,addr) stxa src, [addr] %asi
53 #define FUNC_NAME NGmemcpy
64 .register %g2,#scratch
65 .register %g3,#scratch
71 .type FUNC_NAME,#function
72 FUNC_NAME: /* %o0=dst, %o1=src, %o2=len */
85 /* 2 blocks (128 bytes) is the minimum we can do the block
86 * copy with. We need to ensure that we'll iterate at least
87 * once in the block copy loop. At worst we'll need to align
88 * the destination to a 64-byte boundary which can chew up
89 * to (64 - 1) bytes from the length before we perform the
98 * %o2: len (known to be >= 128)
100 * The block copy loops will use %o4/%o5,%g2/%g3 as
101 * temporaries while copying the data.
104 LOAD(prefetch, %o1, #one_read)
105 wr %g0, STORE_ASI, %asi
107 /* Align destination on 64-byte boundary. */
108 andcc %o0, (64 - 1), %o4
111 sub %g0, %o4, %o4 ! bytes to align dst
114 EX_LD(LOAD(ldub, %o1, %g1))
115 EX_ST(STORE(stb, %g1, %o0))
120 /* If the source is on a 16-byte boundary we can do
121 * the direct block copy loop. If it is 8-byte aligned
122 * we can do the 16-byte loads offset by -8 bytes and the
123 * init stores offset by one register.
125 * If the source is not even 8-byte aligned, we need to do
126 * shifting and masking (basically integer faligndata).
128 * The careful bit with init stores is that if we store
129 * to any part of the cache line we have to store the whole
130 * cacheline else we can end up with corrupt L2 cache line
131 * contents. Since the loop works on 64-bytes of 64-byte
132 * aligned store data at a time, this is easy to ensure.
135 andcc %o1, (16 - 1), %o4
136 andn %o2, (64 - 1), %g1 ! block copy loop iterator
137 sub %o2, %g1, %o2 ! final sub-block copy bytes
143 /* Neither 8-byte nor 16-byte aligned, shift and mask. */
149 EX_LD(LOAD(ldx, %o1, %g2))
153 #define SWIVEL_ONE_DWORD(SRC, TMP1, TMP2, PRE_VAL, PRE_SHIFT, POST_SHIFT, DST)\
154 EX_LD(LOAD(ldx, SRC, TMP1)); \
155 srlx TMP1, PRE_SHIFT, TMP2; \
156 or TMP2, PRE_VAL, TMP2; \
157 EX_ST(STORE_INIT(TMP2, DST)); \
158 sllx TMP1, POST_SHIFT, PRE_VAL;
161 SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x00)
163 SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x08)
165 SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x10)
167 SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x18)
169 LOAD(prefetch, %o1, #one_read)
171 SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x20)
173 SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x28)
175 SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x30)
177 SWIVEL_ONE_DWORD(%o1, %g3, %o5, %g2, %o3, %g1, %o0 + 0x38)
182 #undef SWIVEL_ONE_DWORD
188 10: /* Destination is 64-byte aligned, source was only 8-byte
189 * aligned but it has been subtracted by 8 and we perform
190 * one twin load ahead, then add 8 back into source when
191 * we finish the loop.
193 EX_LD(LOAD_TWIN(%o1, %o4, %o5))
195 EX_LD(LOAD_TWIN(%o1, %g2, %g3))
196 add %o1, 16 + 32, %o1
197 LOAD(prefetch, %o1, #one_read)
199 EX_ST(STORE_INIT(%o5, %o0 + 0x00)) ! initializes cache line
200 EX_ST(STORE_INIT(%g2, %o0 + 0x08))
201 EX_LD(LOAD_TWIN(%o1, %o4, %o5))
203 EX_ST(STORE_INIT(%g3, %o0 + 0x10))
204 EX_ST(STORE_INIT(%o4, %o0 + 0x18))
205 EX_LD(LOAD_TWIN(%o1, %g2, %g3))
207 EX_ST(STORE_INIT(%o5, %o0 + 0x20))
208 EX_ST(STORE_INIT(%g2, %o0 + 0x28))
209 EX_LD(LOAD_TWIN(%o1, %o4, %o5))
210 EX_ST(STORE_INIT(%g3, %o0 + 0x30))
211 EX_ST(STORE_INIT(%o4, %o0 + 0x38))
219 50: /* Destination is 64-byte aligned, and source is 16-byte
222 1: EX_LD(LOAD_TWIN(%o1, %o4, %o5))
224 EX_LD(LOAD_TWIN(%o1, %g2, %g3))
225 add %o1, 16 + 32, %o1
226 LOAD(prefetch, %o1, #one_read)
228 EX_ST(STORE_INIT(%o4, %o0 + 0x00)) ! initializes cache line
229 EX_ST(STORE_INIT(%o5, %o0 + 0x08))
230 EX_LD(LOAD_TWIN(%o1, %o4, %o5))
232 EX_ST(STORE_INIT(%g2, %o0 + 0x10))
233 EX_ST(STORE_INIT(%g3, %o0 + 0x18))
234 EX_LD(LOAD_TWIN(%o1, %g2, %g3))
236 EX_ST(STORE_INIT(%o4, %o0 + 0x20))
237 EX_ST(STORE_INIT(%o5, %o0 + 0x28))
238 EX_ST(STORE_INIT(%g2, %o0 + 0x30))
239 EX_ST(STORE_INIT(%g3, %o0 + 0x38))
246 /* %o2 contains any final bytes still needed to be copied
247 * over. If anything is left, we copy it one byte at a time.
255 70: /* 16 < len <= 64 */
262 1: subcc %o4, 0x10, %o4
263 EX_LD(LOAD(ldx, %o1, %o5))
265 EX_LD(LOAD(ldx, %o1, %g1))
267 EX_ST(STORE(stx, %o5, %o1 + %o3))
269 EX_ST(STORE(stx, %g1, %o1 + %o3))
272 73: andcc %o2, 0x8, %g0
276 EX_LD(LOAD(ldx, %o1, %o5))
277 EX_ST(STORE(stx, %o5, %o1 + %o3))
279 1: andcc %o2, 0x4, %g0
283 EX_LD(LOAD(lduw, %o1, %o5))
284 EX_ST(STORE(stw, %o5, %o1 + %o3))
300 EX_LD(LOAD(ldub, %o1, %o5))
301 EX_ST(STORE(stb, %o5, %o1 + %o3))
317 EX_LD(LOAD(ldx, %o1, %g2))
322 EX_LD(LOAD(ldx, %o1, %g3))
326 EX_ST(STORE(stx, %o5, %o0))
339 80: /* 0 < len <= 16 */
346 EX_LD(LOAD(lduw, %o1, %g1))
347 EX_ST(STORE(stw, %g1, %o1 + %o3))
352 mov EX_RETVAL(GLOBAL_SPARE), %o0
357 EX_LD(LOAD(ldub, %o1, %g1))
358 EX_ST(STORE(stb, %g1, %o1 + %o3))
362 mov EX_RETVAL(GLOBAL_SPARE), %o0
364 .size FUNC_NAME, .-FUNC_NAME