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 * During early init, cache might not be active yet, so dcbz cannot be used.
78 * We therefore skip the optimised bloc that uses dcbz. This jump is
79 * replaced by a nop once cache is active. This is done in machine_init()
94 bne 2f /* Use normal procedure if r4 is not zero */
95 _GLOBAL(memset_nocache_branch)
96 b 2f /* Skip optimised bloc until cache is enabled */
98 clrlwi r7,r6,32-LG_CACHELINE_BYTES
100 srwi r9,r8,LG_CACHELINE_BYTES
101 addic. r9,r9,-1 /* total number of complete cachelines */
103 xori r0,r7,CACHELINE_MASK & ~3
112 addi r6,r6,CACHELINE_BYTES
114 clrlwi r5,r8,32-LG_CACHELINE_BYTES
132 * This version uses dcbz on the complete cache lines in the
133 * destination area to reduce memory traffic. This requires that
134 * the destination area is cacheable.
135 * We only use this version if the source and dest don't overlap.
138 * During early init, cache might not be active yet, so dcbz cannot be used.
139 * We therefore jump to generic_memcpy which doesn't use dcbz. This jump is
140 * replaced by a nop once cache is active. This is done in machine_init()
149 add r7,r3,r5 /* test if the src & dst overlap */
153 crand 0,0,4 /* cr0.lt &= cr1.lt */
154 blt generic_memcpy /* if regions overlap */
159 andi. r0,r0,CACHELINE_MASK /* # bytes to start of cache line */
162 cmplw 0,r5,r0 /* is this more than total to do? */
163 blt 63f /* if not much to do */
164 andi. r8,r0,3 /* get it word-aligned first */
168 70: lbz r9,4(r4) /* do some bytes */
176 72: lwzu r9,4(r4) /* do some words */
180 58: srwi. r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
181 clrlwi r5,r5,32-LG_CACHELINE_BYTES
188 #if L1_CACHE_BYTES >= 32
190 #if L1_CACHE_BYTES >= 64
193 #if L1_CACHE_BYTES >= 128
220 _GLOBAL(generic_memcpy)
224 beq 2f /* if less than 8 bytes to do */
225 andi. r0,r6,3 /* get dest word aligned */
256 rlwinm. r7,r5,32-3,3,31
261 _GLOBAL(backwards_memcpy)
262 rlwinm. r7,r5,32-3,3,31 /* r0 = r5 >> 3 */
292 rlwinm. r7,r5,32-3,3,31
297 _GLOBAL(__copy_tofrom_user)
301 andi. r0,r0,CACHELINE_MASK /* # bytes to start of cache line */
304 cmplw 0,r5,r0 /* is this more than total to do? */
305 blt 63f /* if not much to do */
306 andi. r8,r0,3 /* get it word-aligned first */
309 70: lbz r9,4(r4) /* do some bytes */
318 72: lwzu r9,4(r4) /* do some words */
322 .section __ex_table,"a"
330 58: srwi. r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
331 clrlwi r5,r5,32-LG_CACHELINE_BYTES
335 /* Here we decide how far ahead to prefetch the source */
341 #if MAX_COPY_PREFETCH > 1
342 /* Heuristically, for large transfers we prefetch
343 MAX_COPY_PREFETCH cachelines ahead. For small transfers
344 we prefetch 1 cacheline ahead. */
345 cmpwi r0,MAX_COPY_PREFETCH
347 li r7,MAX_COPY_PREFETCH
350 addi r3,r3,CACHELINE_BYTES
354 addi r3,r3,CACHELINE_BYTES
355 #endif /* MAX_COPY_PREFETCH > 1 */
363 .section __ex_table,"a"
367 /* the main body of the cacheline loop */
368 COPY_16_BYTES_WITHEX(0)
369 #if L1_CACHE_BYTES >= 32
370 COPY_16_BYTES_WITHEX(1)
371 #if L1_CACHE_BYTES >= 64
372 COPY_16_BYTES_WITHEX(2)
373 COPY_16_BYTES_WITHEX(3)
374 #if L1_CACHE_BYTES >= 128
375 COPY_16_BYTES_WITHEX(4)
376 COPY_16_BYTES_WITHEX(5)
377 COPY_16_BYTES_WITHEX(6)
378 COPY_16_BYTES_WITHEX(7)
406 /* read fault, initial single-byte copy */
409 /* write fault, initial single-byte copy */
414 /* read fault, initial word copy */
417 /* write fault, initial word copy */
423 * this stuff handles faults in the cacheline loop and branches to either
424 * 104f (if in read part) or 105f (if in write part), after updating r5
426 COPY_16_BYTES_EXCODE(0)
427 #if L1_CACHE_BYTES >= 32
428 COPY_16_BYTES_EXCODE(1)
429 #if L1_CACHE_BYTES >= 64
430 COPY_16_BYTES_EXCODE(2)
431 COPY_16_BYTES_EXCODE(3)
432 #if L1_CACHE_BYTES >= 128
433 COPY_16_BYTES_EXCODE(4)
434 COPY_16_BYTES_EXCODE(5)
435 COPY_16_BYTES_EXCODE(6)
436 COPY_16_BYTES_EXCODE(7)
441 /* read fault in cacheline loop */
444 /* fault on dcbz (effectively a write fault) */
445 /* or write fault in cacheline loop */
447 92: li r3,LG_CACHELINE_BYTES
451 /* read fault in final word loop */
454 /* write fault in final word loop */
459 /* read fault in final byte loop */
462 /* write fault in final byte loop */
467 * At this stage the number of bytes not copied is
468 * r5 + (ctr << r3), and r9 is 0 for read or 1 for write.
473 beq 120f /* shouldn't happen */
476 /* for a read fault, first try to continue the copy one byte at a time */
483 /* then clear out the destination: r3 bytes starting at 4(r6) */
499 .section __ex_table,"a"