ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / sparc64 / lib / U3memcpy.S
1 /* $Id: U3memcpy.S,v 1.2 2000/11/01 09:29:19 davem Exp $
2  * U3memcpy.S: UltraSparc-III optimized memcpy.
3  *
4  * Copyright (C) 1999, 2000 David S. Miller (davem@redhat.com)
5  */
6
7 #ifdef __KERNEL__
8 #include <asm/visasm.h>
9 #include <asm/asi.h>
10 #include <asm/dcu.h>
11 #include <asm/spitfire.h>
12 #undef SMALL_COPY_USES_FPU
13 #else
14 #define ASI_BLK_P 0xf0
15 #define FPRS_FEF  0x04
16 #define VISEntryHalf rd %fprs, %o5; wr %g0, FPRS_FEF, %fprs
17 #define VISExitHalf and %o5, FPRS_FEF, %o5; wr %o5, 0x0, %fprs
18 #define SMALL_COPY_USES_FPU
19 #endif
20
21         /* Special/non-trivial issues of this code:
22          *
23          * 1) %o5 is preserved from VISEntryHalf to VISExitHalf
24          * 2) Only low 32 FPU registers are used so that only the
25          *    lower half of the FPU register set is dirtied by this
26          *    code.  This is especially important in the kernel.
27          * 3) This code never prefetches cachelines past the end
28          *    of the source buffer.
29          */
30
31         .text
32         .align  32
33
34         /* The cheetah's flexible spine, oversized liver, enlarged heart,
35          * slender muscular body, and claws make it the swiftest hunter
36          * in Africa and the fastest animal on land.  Can reach speeds
37          * of up to 2.4GB per second.
38          */
39
40         .globl          U3memcpy
41 U3memcpy: /* %o0=dst, %o1=src, %o2=len */
42 #ifndef __KERNEL__
43         /* Save away original 'dst' for memcpy return value. */
44         mov             %o0, %g3                        ! A0    Group
45 #endif
46         /* Anything to copy at all? */
47         cmp             %o2, 0                          ! A1
48         ble,pn          %icc, U3memcpy_short_ret        ! BR
49
50         /* Extremely small copy? */
51          cmp            %o2, 31                         ! A0    Group
52         ble,pn          %icc, U3memcpy_short            ! BR
53
54         /* Large enough to use unrolled prefetch loops? */
55          cmp            %o2, 0x100                      ! A1
56         bge,a,pt        %icc, U3memcpy_enter            ! BR    Group
57          andcc          %o0, 0x3f, %g2                  ! A0
58
59         ba,pt           %xcc, U3memcpy_toosmall         ! BR    Group
60          andcc          %o0, 0x7, %g2                   ! A0
61
62         .align          32
63 U3memcpy_short:
64         /* Copy %o2 bytes from src to dst, one byte at a time. */
65         ldub            [%o1 + 0x00], %o3               ! MS    Group
66         add             %o1, 0x1, %o1                   ! A0
67         add             %o0, 0x1, %o0                   ! A1
68         subcc           %o2, 1, %o2                     ! A0    Group
69
70         bg,pt           %icc, U3memcpy_short            ! BR
71          stb            %o3, [%o0 + -1]                 ! MS    Group (1-cycle stall)
72
73 U3memcpy_short_ret:
74 #ifdef __KERNEL__
75         retl                                            ! BR    Group (0-4 cycle stall)
76          clr            %o0                             ! A0
77 #else
78         retl                                            ! BR    Group (0-4 cycle stall)
79          mov            %g3, %o0                        ! A0
80 #endif
81
82         /* Here len >= (6 * 64) and condition codes reflect execution
83          * of "andcc %o0, 0x7, %g2", done by caller.
84          */
85         .align          64
86 U3memcpy_enter:
87         /* Is 'dst' already aligned on an 64-byte boundary? */
88         be,pt           %xcc, 2f                        ! BR
89
90         /* Compute abs((dst & 0x3f) - 0x40) into %g2.  This is the number
91          * of bytes to copy to make 'dst' 64-byte aligned.  We pre-
92          * subtract this from 'len'.
93          */
94          sub            %g2, 0x40, %g2                  ! A0    Group
95         sub             %g0, %g2, %g2                   ! A0    Group
96         sub             %o2, %g2, %o2                   ! A0    Group
97
98         /* Copy %g2 bytes from src to dst, one byte at a time. */
99 1:      ldub            [%o1 + 0x00], %o3               ! MS    (Group)
100         add             %o1, 0x1, %o1                   ! A1
101         add             %o0, 0x1, %o0                   ! A0    Group
102         subcc           %g2, 0x1, %g2                   ! A1
103
104         bg,pt           %icc, 1b                        ! BR    Group
105          stb            %o3, [%o0 + -1]                 ! MS    Group
106
107 2:      VISEntryHalf                                    ! MS+MS
108         and             %o1, 0x7, %g1                   ! A1
109         ba,pt           %xcc, U3memcpy_begin            ! BR
110          alignaddr      %o1, %g0, %o1                   ! MS          (Break-after)
111
112         .align          64
113 U3memcpy_begin:
114 #ifdef __KERNEL__
115         .globl          U3memcpy_nop_1_6
116 U3memcpy_nop_1_6:
117         ldxa            [%g0] ASI_DCU_CONTROL_REG, %g3
118         sethi           %uhi(DCU_PE), %o3
119         sllx            %o3, 32, %o3
120         or              %g3, %o3, %o3
121         stxa            %o3, [%g0] ASI_DCU_CONTROL_REG  ! Enable P-cache
122         membar          #Sync
123 #endif
124         prefetch        [%o1 + 0x000], #one_read        ! MS    Group1
125         prefetch        [%o1 + 0x040], #one_read        ! MS    Group2
126         andn            %o2, (0x40 - 1), %o4            ! A0
127         prefetch        [%o1 + 0x080], #one_read        ! MS    Group3
128         cmp             %o4, 0x140                      ! A0
129         prefetch        [%o1 + 0x0c0], #one_read        ! MS    Group4
130         ldd             [%o1 + 0x000], %f0              ! MS    Group5 (%f0 results at G8)
131         bge,a,pt        %icc, 1f                        ! BR
132
133          prefetch       [%o1 + 0x100], #one_read        ! MS    Group6
134 1:      ldd             [%o1 + 0x008], %f2              ! AX           (%f2 results at G9)
135         cmp             %o4, 0x180                      ! A1
136         bge,a,pt        %icc, 1f                        ! BR
137          prefetch       [%o1 + 0x140], #one_read        ! MS    Group7
138 1:      ldd             [%o1 + 0x010], %f4              ! AX           (%f4 results at G10)
139         cmp             %o4, 0x1c0                      ! A1
140         bge,a,pt        %icc, 1f                        ! BR
141
142          prefetch       [%o1 + 0x180], #one_read        ! MS    Group8
143 1:      faligndata      %f0, %f2, %f16                  ! FGA   Group9 (%f16 at G12)
144         ldd             [%o1 + 0x018], %f6              ! AX           (%f6 results at G12)
145         faligndata      %f2, %f4, %f18                  ! FGA   Group10 (%f18 results at G13)
146         ldd             [%o1 + 0x020], %f8              ! MS            (%f8 results at G13)
147         faligndata      %f4, %f6, %f20                  ! FGA   Group12 (1-cycle stall,%f20 at G15)
148         ldd             [%o1 + 0x028], %f10             ! MS            (%f10 results at G15)
149         faligndata      %f6, %f8, %f22                  ! FGA   Group13 (%f22 results at G16)
150
151         ldd             [%o1 + 0x030], %f12             ! MS            (%f12 results at G16)
152         faligndata      %f8, %f10, %f24                 ! FGA   Group15 (1-cycle stall,%f24 at G18)
153         ldd             [%o1 + 0x038], %f14             ! MS            (%f14 results at G18)
154         faligndata      %f10, %f12, %f26                ! FGA   Group16 (%f26 results at G19)
155         ldd             [%o1 + 0x040], %f0              ! MS            (%f0 results at G19)
156
157         /* We only use the first loop if len > (7 * 64). */
158         subcc           %o4, 0x1c0, %o4                 ! A0    Group17
159         bg,pt           %icc, U3memcpy_loop1            ! BR
160          add            %o1, 0x40, %o1                  ! A1
161
162         add             %o4, 0x140, %o4                 ! A0    Group18
163         ba,pt           %xcc, U3memcpy_loop2            ! BR
164          srl            %o4, 6, %o3                     ! A0    Group19
165         nop
166         nop
167         nop
168         nop
169         nop
170
171         nop
172         nop
173
174         /* This loop performs the copy and queues new prefetches.
175          * We drop into the second loop when len <= (5 * 64).  Note
176          * that this (5 * 64) factor has been subtracted from len
177          * already.
178          */
179 U3memcpy_loop1:
180         ldd             [%o1 + 0x008], %f2              ! MS    Group2  (%f2 results at G5)
181         faligndata      %f12, %f14, %f28                ! FGA           (%f28 results at G5)
182         ldd             [%o1 + 0x010], %f4              ! MS    Group3  (%f4 results at G6)
183         faligndata      %f14, %f0, %f30                 ! FGA   Group4  (1-cycle stall, %f30 at G7)
184         stda            %f16, [%o0] ASI_BLK_P           ! MS
185         ldd             [%o1 + 0x018], %f6              ! AX            (%f6 results at G7)
186
187         faligndata      %f0, %f2, %f16                  ! FGA   Group12 (7-cycle stall)
188         ldd             [%o1 + 0x020], %f8              ! MS            (%f8 results at G15)
189         faligndata      %f2, %f4, %f18                  ! FGA   Group13 (%f18 results at G16)
190         ldd             [%o1 + 0x028], %f10             ! MS            (%f10 results at G16)
191         faligndata      %f4, %f6, %f20                  ! FGA   Group14 (%f20 results at G17)
192         ldd             [%o1 + 0x030], %f12             ! MS            (%f12 results at G17)
193         faligndata      %f6, %f8, %f22                  ! FGA   Group15 (%f22 results at G18)
194         ldd             [%o1 + 0x038], %f14             ! MS            (%f14 results at G18)
195
196         faligndata      %f8, %f10, %f24                 ! FGA   Group16 (%f24 results at G19)
197         ldd             [%o1 + 0x040], %f0              ! AX            (%f0 results at G19)
198         prefetch        [%o1 + 0x180], #one_read        ! MS
199         faligndata      %f10, %f12, %f26                ! FGA   Group17 (%f26 results at G20)
200         subcc           %o4, 0x40, %o4                  ! A0
201         add             %o1, 0x40, %o1                  ! A1
202         bg,pt           %xcc, U3memcpy_loop1            ! BR
203          add            %o0, 0x40, %o0                  ! A0    Group18
204
205 U3memcpy_loop2_enter:
206         mov             5, %o3                          ! A1
207
208         /* This loop performs on the copy, no new prefetches are
209          * queued.  We do things this way so that we do not perform
210          * any spurious prefetches past the end of the src buffer.
211          */
212 U3memcpy_loop2:
213         ldd             [%o1 + 0x008], %f2              ! MS
214         faligndata      %f12, %f14, %f28                ! FGA   Group2
215         ldd             [%o1 + 0x010], %f4              ! MS
216         faligndata      %f14, %f0, %f30                 ! FGA   Group4  (1-cycle stall)
217         stda            %f16, [%o0] ASI_BLK_P           ! MS
218         ldd             [%o1 + 0x018], %f6              ! AX
219         faligndata      %f0, %f2, %f16                  ! FGA   Group12 (7-cycle stall)
220
221         ldd             [%o1 + 0x020], %f8              ! MS
222         faligndata      %f2, %f4, %f18                  ! FGA   Group13
223         ldd             [%o1 + 0x028], %f10             ! MS
224         faligndata      %f4, %f6, %f20                  ! FGA   Group14
225         ldd             [%o1 + 0x030], %f12             ! MS
226         faligndata      %f6, %f8, %f22                  ! FGA   Group15
227         ldd             [%o1 + 0x038], %f14             ! MS
228         faligndata      %f8, %f10, %f24                 ! FGA   Group16
229
230         ldd             [%o1 + 0x040], %f0              ! AX
231         faligndata      %f10, %f12, %f26                ! FGA   Group17
232         subcc           %o3, 0x01, %o3                  ! A0
233         add             %o1, 0x40, %o1                  ! A1
234         bg,pt           %xcc, U3memcpy_loop2            ! BR
235          add            %o0, 0x40, %o0                  ! A0    Group18
236
237         /* Finally we copy the last full 64-byte block. */
238 U3memcpy_loopfini:
239         ldd             [%o1 + 0x008], %f2              ! MS
240         faligndata      %f12, %f14, %f28                ! FGA
241         ldd             [%o1 + 0x010], %f4              ! MS    Group19
242         faligndata      %f14, %f0, %f30                 ! FGA
243         stda            %f16, [%o0] ASI_BLK_P           ! MS    Group20
244         ldd             [%o1 + 0x018], %f6              ! AX
245         faligndata      %f0, %f2, %f16                  ! FGA   Group11 (7-cycle stall)
246         ldd             [%o1 + 0x020], %f8              ! MS
247         faligndata      %f2, %f4, %f18                  ! FGA   Group12
248         ldd             [%o1 + 0x028], %f10             ! MS
249         faligndata      %f4, %f6, %f20                  ! FGA   Group13
250         ldd             [%o1 + 0x030], %f12             ! MS
251         faligndata      %f6, %f8, %f22                  ! FGA   Group14
252         ldd             [%o1 + 0x038], %f14             ! MS
253         faligndata      %f8, %f10, %f24                 ! FGA   Group15
254         cmp             %g1, 0                          ! A0
255         be,pt           %icc, 1f                        ! BR
256          add            %o0, 0x40, %o0                  ! A1
257         ldd             [%o1 + 0x040], %f0              ! MS
258 1:      faligndata      %f10, %f12, %f26                ! FGA   Group16
259         faligndata      %f12, %f14, %f28                ! FGA   Group17
260         faligndata      %f14, %f0, %f30                 ! FGA   Group18
261         stda            %f16, [%o0] ASI_BLK_P           ! MS
262         add             %o0, 0x40, %o0                  ! A0
263         add             %o1, 0x40, %o1                  ! A1
264 #ifdef __KERNEL__
265         .globl          U3memcpy_nop_2_3
266 U3memcpy_nop_2_3:
267         mov             PRIMARY_CONTEXT, %o3
268         stxa            %g0, [%o3] ASI_DMMU             ! Flush P-cache
269         stxa            %g3, [%g0] ASI_DCU_CONTROL_REG  ! Disable P-cache
270 #endif
271         membar          #Sync                           ! MS    Group26 (7-cycle stall)
272
273         /* Now we copy the (len modulo 64) bytes at the end.
274          * Note how we borrow the %f0 loaded above.
275          *
276          * Also notice how this code is careful not to perform a
277          * load past the end of the src buffer just like similar
278          * code found in U3memcpy_toosmall processing.
279          */
280 U3memcpy_loopend:
281         and             %o2, 0x3f, %o2                  ! A0    Group
282         andcc           %o2, 0x38, %g2                  ! A0    Group
283         be,pn           %icc, U3memcpy_endcruft         ! BR
284          subcc          %g2, 0x8, %g2                   ! A1
285         be,pn           %icc, U3memcpy_endcruft         ! BR    Group
286          cmp            %g1, 0                          ! A0
287
288         be,a,pt         %icc, 1f                        ! BR    Group
289          ldd            [%o1 + 0x00], %f0               ! MS
290
291 1:      ldd             [%o1 + 0x08], %f2               ! MS    Group
292         add             %o1, 0x8, %o1                   ! A0
293         sub             %o2, 0x8, %o2                   ! A1
294         subcc           %g2, 0x8, %g2                   ! A0    Group
295         faligndata      %f0, %f2, %f8                   ! FGA   Group
296         std             %f8, [%o0 + 0x00]               ! MS    (XXX does it stall here? XXX)
297         be,pn           %icc, U3memcpy_endcruft         ! BR
298          add            %o0, 0x8, %o0                   ! A0
299         ldd             [%o1 + 0x08], %f0               ! MS    Group
300         add             %o1, 0x8, %o1                   ! A0
301         sub             %o2, 0x8, %o2                   ! A1
302         subcc           %g2, 0x8, %g2                   ! A0    Group
303         faligndata      %f2, %f0, %f8                   ! FGA
304         std             %f8, [%o0 + 0x00]               ! MS    (XXX does it stall here? XXX)
305         bne,pn          %icc, 1b                        ! BR
306          add            %o0, 0x8, %o0                   ! A0    Group
307
308         /* If anything is left, we copy it one byte at a time.
309          * Note that %g1 is (src & 0x3) saved above before the
310          * alignaddr was performed.
311          */
312 U3memcpy_endcruft:
313         cmp             %o2, 0
314         add             %o1, %g1, %o1
315         VISExitHalf
316         be,pn           %icc, U3memcpy_short_ret
317          nop
318         ba,a,pt         %xcc, U3memcpy_short
319
320         /* If we get here, then 32 <= len < (6 * 64) */
321 U3memcpy_toosmall:
322
323 #ifdef SMALL_COPY_USES_FPU
324
325         /* Is 'dst' already aligned on an 8-byte boundary? */
326         be,pt           %xcc, 2f                        ! BR    Group
327
328         /* Compute abs((dst & 7) - 8) into %g2.  This is the number
329          * of bytes to copy to make 'dst' 8-byte aligned.  We pre-
330          * subtract this from 'len'.
331          */
332          sub            %g2, 0x8, %g2                   ! A0
333         sub             %g0, %g2, %g2                   ! A0    Group (reg-dep)
334         sub             %o2, %g2, %o2                   ! A0    Group (reg-dep)
335
336         /* Copy %g2 bytes from src to dst, one byte at a time. */
337 1:      ldub            [%o1 + 0x00], %o3               ! MS    (Group) (%o3 in 3 cycles)
338         add             %o1, 0x1, %o1                   ! A1
339         add             %o0, 0x1, %o0                   ! A0    Group
340         subcc           %g2, 0x1, %g2                   ! A1
341
342         bg,pt           %icc, 1b                        ! BR    Group
343          stb            %o3, [%o0 + -1]                 ! MS    Group
344
345 2:      VISEntryHalf                                    ! MS+MS
346
347         /* Compute (len - (len % 8)) into %g2.  This is guaranteed
348          * to be nonzero.
349          */
350         andn            %o2, 0x7, %g2                   ! A0    Group
351
352         /* You may read this and believe that it allows reading
353          * one 8-byte longword past the end of src.  It actually
354          * does not, as %g2 is subtracted as loads are done from
355          * src, so we always stop before running off the end.
356          * Also, we are guaranteed to have at least 0x10 bytes
357          * to move here.
358          */
359         sub             %g2, 0x8, %g2                   ! A0    Group (reg-dep)
360         alignaddr       %o1, %g0, %g1                   ! MS          (Break-after)
361         ldd             [%g1 + 0x00], %f0               ! MS    Group (1-cycle stall)
362         add             %g1, 0x8, %g1                   ! A0
363
364 1:      ldd             [%g1 + 0x00], %f2               ! MS    Group
365         add             %g1, 0x8, %g1                   ! A0
366         sub             %o2, 0x8, %o2                   ! A1
367         subcc           %g2, 0x8, %g2                   ! A0    Group
368
369         faligndata      %f0, %f2, %f8                   ! FGA   Group (1-cycle stall)
370         std             %f8, [%o0 + 0x00]               ! MS    Group (2-cycle stall)
371         add             %o1, 0x8, %o1                   ! A0
372         be,pn           %icc, 2f                        ! BR
373
374          add            %o0, 0x8, %o0                   ! A1
375         ldd             [%g1 + 0x00], %f0               ! MS    Group
376         add             %g1, 0x8, %g1                   ! A0
377         sub             %o2, 0x8, %o2                   ! A1
378
379         subcc           %g2, 0x8, %g2                   ! A0    Group
380         faligndata      %f2, %f0, %f8                   ! FGA   Group (1-cycle stall)
381         std             %f8, [%o0 + 0x00]               ! MS    Group (2-cycle stall)
382         add             %o1, 0x8, %o1                   ! A0
383
384         bne,pn          %icc, 1b                        ! BR
385          add            %o0, 0x8, %o0                   ! A1
386
387         /* Nothing left to copy? */
388 2:      cmp             %o2, 0                          ! A0    Group
389         VISExitHalf                                     ! A0+MS
390         be,pn           %icc, U3memcpy_short_ret        ! BR    Group
391          nop                                            ! A0
392         ba,a,pt         %xcc, U3memcpy_short            ! BR    Group
393
394 #else /* !(SMALL_COPY_USES_FPU) */
395
396         xor             %o1, %o0, %g2
397         andcc           %g2, 0x7, %g0
398         bne,pn          %icc, U3memcpy_short
399          andcc          %o1, 0x7, %g2
400
401         be,pt           %xcc, 2f
402          sub            %g2, 0x8, %g2
403         sub             %g0, %g2, %g2
404         sub             %o2, %g2, %o2
405
406 1:      ldub            [%o1 + 0x00], %o3
407         add             %o1, 0x1, %o1
408         add             %o0, 0x1, %o0
409         subcc           %g2, 0x1, %g2
410         bg,pt           %icc, 1b
411          stb            %o3, [%o0 + -1]
412
413 2:      andn            %o2, 0x7, %g2
414         sub             %o2, %g2, %o2
415
416 3:      ldx             [%o1 + 0x00], %o3
417         add             %o1, 0x8, %o1
418         add             %o0, 0x8, %o0
419         subcc           %g2, 0x8, %g2
420         bg,pt           %icc, 3b
421          stx            %o3, [%o0 + -8]
422
423         cmp             %o2, 0
424         bne,pn          %icc, U3memcpy_short
425          nop
426         ba,a,pt         %xcc, U3memcpy_short_ret
427
428 #endif /* !(SMALL_COPY_USES_FPU) */