2 * Memory copy functions for 32-bit PowerPC.
4 * Copyright (C) 1996-2005 Paul Mackerras.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 #include <asm/processor.h>
12 #include <asm/cache.h>
13 #include <asm/errno.h>
14 #include <asm/ppc_asm.h>
16 #define COPY_16_BYTES \
26 #define COPY_16_BYTES_WITHEX(n) \
44 #define COPY_16_BYTES_EXCODE(n) \
46 addi r5,r5,-(16 * n); \
49 addi r5,r5,-(16 * n); \
51 .section __ex_table,"a"; \
53 .long 8 ## n ## 0b,9 ## n ## 0b; \
54 .long 8 ## n ## 1b,9 ## n ## 0b; \
55 .long 8 ## n ## 2b,9 ## n ## 0b; \
56 .long 8 ## n ## 3b,9 ## n ## 0b; \
57 .long 8 ## n ## 4b,9 ## n ## 1b; \
58 .long 8 ## n ## 5b,9 ## n ## 1b; \
59 .long 8 ## n ## 6b,9 ## n ## 1b; \
60 .long 8 ## n ## 7b,9 ## n ## 1b; \
64 .stabs "arch/powerpc/lib/",N_SO,0,0,0f
65 .stabs "copy_32.S",N_SO,0,0,0f
68 CACHELINE_BYTES = L1_CACHE_BYTES
69 LG_CACHELINE_BYTES = L1_CACHE_SHIFT
70 CACHELINE_MASK = (L1_CACHE_BYTES-1)
73 * Use dcbz on the complete cache lines in the destination
74 * to set them to zero. This requires that the destination
75 * area is cacheable. -- paulus
77 _GLOBAL(cacheable_memzero)
88 clrlwi r7,r6,32-LG_CACHELINE_BYTES
90 srwi r9,r8,LG_CACHELINE_BYTES
91 addic. r9,r9,-1 /* total number of complete cachelines */
93 xori r0,r7,CACHELINE_MASK & ~3
102 addi r6,r6,CACHELINE_BYTES
104 clrlwi r5,r8,32-LG_CACHELINE_BYTES
146 * This version uses dcbz on the complete cache lines in the
147 * destination area to reduce memory traffic. This requires that
148 * the destination area is cacheable.
149 * We only use this version if the source and dest don't overlap.
152 _GLOBAL(cacheable_memcpy)
153 add r7,r3,r5 /* test if the src & dst overlap */
157 crand 0,0,4 /* cr0.lt &= cr1.lt */
158 blt memcpy /* if regions overlap */
163 andi. r0,r0,CACHELINE_MASK /* # bytes to start of cache line */
166 cmplw 0,r5,r0 /* is this more than total to do? */
167 blt 63f /* if not much to do */
168 andi. r8,r0,3 /* get it word-aligned first */
172 70: lbz r9,4(r4) /* do some bytes */
180 72: lwzu r9,4(r4) /* do some words */
184 58: srwi. r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
185 clrlwi r5,r5,32-LG_CACHELINE_BYTES
192 #if L1_CACHE_BYTES >= 32
194 #if L1_CACHE_BYTES >= 64
197 #if L1_CACHE_BYTES >= 128
233 beq 2f /* if less than 8 bytes to do */
234 andi. r0,r6,3 /* get dest word aligned */
265 rlwinm. r7,r5,32-3,3,31
270 _GLOBAL(backwards_memcpy)
271 rlwinm. r7,r5,32-3,3,31 /* r0 = r5 >> 3 */
301 rlwinm. r7,r5,32-3,3,31
306 _GLOBAL(__copy_tofrom_user)
310 andi. r0,r0,CACHELINE_MASK /* # bytes to start of cache line */
313 cmplw 0,r5,r0 /* is this more than total to do? */
314 blt 63f /* if not much to do */
315 andi. r8,r0,3 /* get it word-aligned first */
318 70: lbz r9,4(r4) /* do some bytes */
327 72: lwzu r9,4(r4) /* do some words */
331 .section __ex_table,"a"
339 58: srwi. r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
340 clrlwi r5,r5,32-LG_CACHELINE_BYTES
344 /* Here we decide how far ahead to prefetch the source */
350 #if MAX_COPY_PREFETCH > 1
351 /* Heuristically, for large transfers we prefetch
352 MAX_COPY_PREFETCH cachelines ahead. For small transfers
353 we prefetch 1 cacheline ahead. */
354 cmpwi r0,MAX_COPY_PREFETCH
356 li r7,MAX_COPY_PREFETCH
359 addi r3,r3,CACHELINE_BYTES
363 addi r3,r3,CACHELINE_BYTES
364 #endif /* MAX_COPY_PREFETCH > 1 */
372 .section __ex_table,"a"
376 /* the main body of the cacheline loop */
377 COPY_16_BYTES_WITHEX(0)
378 #if L1_CACHE_BYTES >= 32
379 COPY_16_BYTES_WITHEX(1)
380 #if L1_CACHE_BYTES >= 64
381 COPY_16_BYTES_WITHEX(2)
382 COPY_16_BYTES_WITHEX(3)
383 #if L1_CACHE_BYTES >= 128
384 COPY_16_BYTES_WITHEX(4)
385 COPY_16_BYTES_WITHEX(5)
386 COPY_16_BYTES_WITHEX(6)
387 COPY_16_BYTES_WITHEX(7)
415 /* read fault, initial single-byte copy */
418 /* write fault, initial single-byte copy */
423 /* read fault, initial word copy */
426 /* write fault, initial word copy */
432 * this stuff handles faults in the cacheline loop and branches to either
433 * 104f (if in read part) or 105f (if in write part), after updating r5
435 COPY_16_BYTES_EXCODE(0)
436 #if L1_CACHE_BYTES >= 32
437 COPY_16_BYTES_EXCODE(1)
438 #if L1_CACHE_BYTES >= 64
439 COPY_16_BYTES_EXCODE(2)
440 COPY_16_BYTES_EXCODE(3)
441 #if L1_CACHE_BYTES >= 128
442 COPY_16_BYTES_EXCODE(4)
443 COPY_16_BYTES_EXCODE(5)
444 COPY_16_BYTES_EXCODE(6)
445 COPY_16_BYTES_EXCODE(7)
450 /* read fault in cacheline loop */
453 /* fault on dcbz (effectively a write fault) */
454 /* or write fault in cacheline loop */
456 92: li r3,LG_CACHELINE_BYTES
460 /* read fault in final word loop */
463 /* write fault in final word loop */
468 /* read fault in final byte loop */
471 /* write fault in final byte loop */
476 * At this stage the number of bytes not copied is
477 * r5 + (ctr << r3), and r9 is 0 for read or 1 for write.
482 beq 120f /* shouldn't happen */
485 /* for a read fault, first try to continue the copy one byte at a time */
492 /* then clear out the destination: r3 bytes starting at 4(r6) */
508 .section __ex_table,"a"