1 /* NGmemcpy.S: Niagara optimized memcpy.
3 * Copyright (C) 2006, 2007 David S. Miller (davem@davemloft.net)
8 #include <asm/thread_info.h>
9 #define GLOBAL_SPARE %g7
10 #define RESTORE_ASI(TMP) \
11 ldub [%g6 + TI_CURRENT_DS], TMP; \
14 #define GLOBAL_SPARE %g5
15 #define RESTORE_ASI(TMP) \
20 #define SAVE_AMOUNT 128
22 #define SAVE_AMOUNT 64
26 #define STORE_ASI ASI_BLK_INIT_QUAD_LDD_P
38 #define EX_RETVAL(x) x
43 #define LOAD(type,addr,dest) type [addr], dest
45 #define LOAD(type,addr,dest) type##a [addr] 0x80, dest
50 #define LOAD_TWIN(addr_reg,dest0,dest1) \
51 ldda [addr_reg] ASI_BLK_INIT_QUAD_LDD_P, dest0
55 #define STORE(type,src,addr) type src, [addr]
59 #ifndef SIMULATE_NIAGARA_ON_NON_NIAGARA
60 #define STORE_INIT(src,addr) stxa src, [addr] %asi
62 #define STORE_INIT(src,addr) stx src, [addr + 0x00]
67 #define FUNC_NAME NGmemcpy
78 .register %g2,#scratch
79 .register %g3,#scratch
85 .type FUNC_NAME,#function
86 FUNC_NAME: /* %i0=dst, %i1=src, %i2=len */
88 save %sp, -SAVE_AMOUNT, %sp
100 /* 2 blocks (128 bytes) is the minimum we can do the block
101 * copy with. We need to ensure that we'll iterate at least
102 * once in the block copy loop. At worst we'll need to align
103 * the destination to a 64-byte boundary which can chew up
104 * to (64 - 1) bytes from the length before we perform the
113 * %i2: len (known to be >= 128)
115 * The block copy loops will use %i4/%i5,%g2/%g3 as
116 * temporaries while copying the data.
119 LOAD(prefetch, %i1, #one_read)
120 wr %g0, STORE_ASI, %asi
122 /* Align destination on 64-byte boundary. */
123 andcc %o0, (64 - 1), %i4
126 sub %g0, %i4, %i4 ! bytes to align dst
129 EX_LD(LOAD(ldub, %i1, %g1))
130 EX_ST(STORE(stb, %g1, %o0))
135 /* If the source is on a 16-byte boundary we can do
136 * the direct block copy loop. If it is 8-byte aligned
137 * we can do the 16-byte loads offset by -8 bytes and the
138 * init stores offset by one register.
140 * If the source is not even 8-byte aligned, we need to do
141 * shifting and masking (basically integer faligndata).
143 * The careful bit with init stores is that if we store
144 * to any part of the cache line we have to store the whole
145 * cacheline else we can end up with corrupt L2 cache line
146 * contents. Since the loop works on 64-bytes of 64-byte
147 * aligned store data at a time, this is easy to ensure.
150 andcc %i1, (16 - 1), %i4
151 andn %i2, (64 - 1), %g1 ! block copy loop iterator
153 sub %i2, %g1, %i2 ! final sub-block copy bytes
159 /* Neither 8-byte nor 16-byte aligned, shift and mask. */
160 and %i4, 0x7, GLOBAL_SPARE
161 sll GLOBAL_SPARE, 3, GLOBAL_SPARE
163 EX_LD(LOAD_TWIN(%i1, %g2, %g3))
164 sub %i5, GLOBAL_SPARE, %i5
173 #define MIX_THREE_WORDS(WORD1, WORD2, WORD3, PRE_SHIFT, POST_SHIFT, TMP) \
174 sllx WORD1, POST_SHIFT, WORD1; \
175 srlx WORD2, PRE_SHIFT, TMP; \
176 sllx WORD2, POST_SHIFT, WORD2; \
177 or WORD1, TMP, WORD1; \
178 srlx WORD3, PRE_SHIFT, TMP; \
179 or WORD2, TMP, WORD2;
181 8: EX_LD(LOAD_TWIN(%i1 + %o4, %o2, %o3))
182 MIX_THREE_WORDS(%g2, %g3, %o2, %i5, GLOBAL_SPARE, %o1)
183 LOAD(prefetch, %i1 + %i3, #one_read)
185 EX_ST(STORE_INIT(%g2, %o0 + 0x00))
186 EX_ST(STORE_INIT(%g3, %o0 + 0x08))
188 EX_LD(LOAD_TWIN(%i1 + %o5, %g2, %g3))
189 MIX_THREE_WORDS(%o2, %o3, %g2, %i5, GLOBAL_SPARE, %o1)
191 EX_ST(STORE_INIT(%o2, %o0 + 0x10))
192 EX_ST(STORE_INIT(%o3, %o0 + 0x18))
194 EX_LD(LOAD_TWIN(%i1 + %o7, %o2, %o3))
195 MIX_THREE_WORDS(%g2, %g3, %o2, %i5, GLOBAL_SPARE, %o1)
197 EX_ST(STORE_INIT(%g2, %o0 + 0x20))
198 EX_ST(STORE_INIT(%g3, %o0 + 0x28))
200 EX_LD(LOAD_TWIN(%i1 + %i3, %g2, %g3))
202 MIX_THREE_WORDS(%o2, %o3, %g2, %i5, GLOBAL_SPARE, %o1)
204 EX_ST(STORE_INIT(%o2, %o0 + 0x30))
205 EX_ST(STORE_INIT(%o3, %o0 + 0x38))
214 9: EX_LD(LOAD_TWIN(%i1 + %o4, %o2, %o3))
215 MIX_THREE_WORDS(%g3, %o2, %o3, %i5, GLOBAL_SPARE, %o1)
216 LOAD(prefetch, %i1 + %i3, #one_read)
218 EX_ST(STORE_INIT(%g3, %o0 + 0x00))
219 EX_ST(STORE_INIT(%o2, %o0 + 0x08))
221 EX_LD(LOAD_TWIN(%i1 + %o5, %g2, %g3))
222 MIX_THREE_WORDS(%o3, %g2, %g3, %i5, GLOBAL_SPARE, %o1)
224 EX_ST(STORE_INIT(%o3, %o0 + 0x10))
225 EX_ST(STORE_INIT(%g2, %o0 + 0x18))
227 EX_LD(LOAD_TWIN(%i1 + %o7, %o2, %o3))
228 MIX_THREE_WORDS(%g3, %o2, %o3, %i5, GLOBAL_SPARE, %o1)
230 EX_ST(STORE_INIT(%g3, %o0 + 0x20))
231 EX_ST(STORE_INIT(%o2, %o0 + 0x28))
233 EX_LD(LOAD_TWIN(%i1 + %i3, %g2, %g3))
235 MIX_THREE_WORDS(%o3, %g2, %g3, %i5, GLOBAL_SPARE, %o1)
237 EX_ST(STORE_INIT(%o3, %o0 + 0x30))
238 EX_ST(STORE_INIT(%g2, %o0 + 0x38))
247 10: /* Destination is 64-byte aligned, source was only 8-byte
248 * aligned but it has been subtracted by 8 and we perform
249 * one twin load ahead, then add 8 back into source when
250 * we finish the loop.
252 EX_LD(LOAD_TWIN(%i1, %o4, %o5))
257 1: EX_LD(LOAD_TWIN(%i1 + %o7, %o2, %o3))
258 LOAD(prefetch, %i1 + %o1, #one_read)
259 EX_ST(STORE_INIT(%o5, %o0 + 0x00)) ! initializes cache line
260 EX_ST(STORE_INIT(%o2, %o0 + 0x08))
261 EX_LD(LOAD_TWIN(%i1 + %g2, %o4, %o5))
262 EX_ST(STORE_INIT(%o3, %o0 + 0x10))
263 EX_ST(STORE_INIT(%o4, %o0 + 0x18))
264 EX_LD(LOAD_TWIN(%i1 + %g3, %o2, %o3))
265 EX_ST(STORE_INIT(%o5, %o0 + 0x20))
266 EX_ST(STORE_INIT(%o2, %o0 + 0x28))
267 EX_LD(LOAD_TWIN(%i1 + %o1, %o4, %o5))
269 EX_ST(STORE_INIT(%o3, %o0 + 0x30))
270 EX_ST(STORE_INIT(%o4, %o0 + 0x38))
278 50: /* Destination is 64-byte aligned, and source is 16-byte
285 1: EX_LD(LOAD_TWIN(%i1 + %g0, %o4, %o5))
286 EX_LD(LOAD_TWIN(%i1 + %o7, %o2, %o3))
287 LOAD(prefetch, %i1 + %o1, #one_read)
288 EX_ST(STORE_INIT(%o4, %o0 + 0x00)) ! initializes cache line
289 EX_ST(STORE_INIT(%o5, %o0 + 0x08))
290 EX_LD(LOAD_TWIN(%i1 + %g2, %o4, %o5))
291 EX_ST(STORE_INIT(%o2, %o0 + 0x10))
292 EX_ST(STORE_INIT(%o3, %o0 + 0x18))
293 EX_LD(LOAD_TWIN(%i1 + %g3, %o2, %o3))
295 EX_ST(STORE_INIT(%o4, %o0 + 0x20))
296 EX_ST(STORE_INIT(%o5, %o0 + 0x28))
297 EX_ST(STORE_INIT(%o2, %o0 + 0x30))
298 EX_ST(STORE_INIT(%o3, %o0 + 0x38))
307 /* %i2 contains any final bytes still needed to be copied
308 * over. If anything is left, we copy it one byte at a time.
316 70: /* 16 < len <= 64 */
323 1: subcc %i4, 0x10, %i4
324 EX_LD(LOAD(ldx, %i1, %o4))
326 EX_LD(LOAD(ldx, %i1, %g1))
328 EX_ST(STORE(stx, %o4, %i1 + %i3))
330 EX_ST(STORE(stx, %g1, %i1 + %i3))
333 73: andcc %i2, 0x8, %g0
337 EX_LD(LOAD(ldx, %i1, %o4))
338 EX_ST(STORE(stx, %o4, %i1 + %i3))
340 1: andcc %i2, 0x4, %g0
344 EX_LD(LOAD(lduw, %i1, %i5))
345 EX_ST(STORE(stw, %i5, %i1 + %i3))
361 EX_LD(LOAD(ldub, %i1, %i5))
362 EX_ST(STORE(stb, %i5, %i1 + %i3))
378 EX_LD(LOAD(ldx, %i1, %g2))
383 EX_LD(LOAD(ldx, %i1, %g3))
387 EX_ST(STORE(stx, %i5, %o0))
400 80: /* 0 < len <= 16 */
407 EX_LD(LOAD(lduw, %i1, %g1))
408 EX_ST(STORE(stw, %g1, %i1 + %i3))
413 restore EX_RETVAL(%i0), %g0, %o0
418 EX_LD(LOAD(ldub, %i1, %g1))
419 EX_ST(STORE(stb, %g1, %i1 + %i3))
423 restore EX_RETVAL(%i0), %g0, %o0
425 .size FUNC_NAME, .-FUNC_NAME