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>
15 #include <asm/export.h>
16 #include <asm/code-patching-asm.h>
17 #include <asm/kasan.h>
19 #define COPY_16_BYTES \
29 #define COPY_16_BYTES_WITHEX(n) \
47 #define COPY_16_BYTES_EXCODE(n) \
49 addi r5,r5,-(16 * n); \
52 addi r5,r5,-(16 * n); \
54 EX_TABLE(8 ## n ## 0b,9 ## n ## 0b); \
55 EX_TABLE(8 ## n ## 1b,9 ## n ## 0b); \
56 EX_TABLE(8 ## n ## 2b,9 ## n ## 0b); \
57 EX_TABLE(8 ## n ## 3b,9 ## n ## 0b); \
58 EX_TABLE(8 ## n ## 4b,9 ## n ## 1b); \
59 EX_TABLE(8 ## n ## 5b,9 ## n ## 1b); \
60 EX_TABLE(8 ## n ## 6b,9 ## n ## 1b); \
61 EX_TABLE(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)
74 rlwinm. r0 ,r5, 31, 1, 31
77 rlwimi r4 ,r4 ,16 ,0 ,15
85 EXPORT_SYMBOL(memset16)
89 * Use dcbz on the complete cache lines in the destination
90 * to set them to zero. This requires that the destination
91 * area is cacheable. -- paulus
93 * During early init, cache might not be active yet, so dcbz cannot be used.
94 * We therefore skip the optimised bloc that uses dcbz. This jump is
95 * replaced by a nop once cache is active. This is done in machine_init()
111 * Skip optimised bloc until cache is enabled. Will be replaced
112 * by 'bne' during boot to use normal procedure if r4 is not zero
115 patch_site 5b, patch__memset_nocache
117 clrlwi r7,r6,32-LG_CACHELINE_BYTES
119 srwi r9,r8,LG_CACHELINE_BYTES
120 addic. r9,r9,-1 /* total number of complete cachelines */
122 xori r0,r7,CACHELINE_MASK & ~3
131 addi r6,r6,CACHELINE_BYTES
133 clrlwi r5,r8,32-LG_CACHELINE_BYTES
156 EXPORT_SYMBOL(memset)
157 EXPORT_SYMBOL_KASAN(memset)
160 * This version uses dcbz on the complete cache lines in the
161 * destination area to reduce memory traffic. This requires that
162 * the destination area is cacheable.
163 * We only use this version if the source and dest don't overlap.
166 * During early init, cache might not be active yet, so dcbz cannot be used.
167 * We therefore jump to generic_memcpy which doesn't use dcbz. This jump is
168 * replaced by a nop once cache is active. This is done in machine_init()
170 _GLOBAL_KASAN(memmove)
175 _GLOBAL_KASAN(memcpy)
177 patch_site 1b, patch__memcpy_nocache
179 add r7,r3,r5 /* test if the src & dst overlap */
183 crand 0,0,4 /* cr0.lt &= cr1.lt */
184 blt generic_memcpy /* if regions overlap */
189 andi. r0,r0,CACHELINE_MASK /* # bytes to start of cache line */
192 cmplw 0,r5,r0 /* is this more than total to do? */
193 blt 63f /* if not much to do */
194 andi. r8,r0,3 /* get it word-aligned first */
198 70: lbz r9,4(r4) /* do some bytes */
206 72: lwzu r9,4(r4) /* do some words */
210 58: srwi. r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
211 clrlwi r5,r5,32-LG_CACHELINE_BYTES
218 #if L1_CACHE_BYTES >= 32
220 #if L1_CACHE_BYTES >= 64
223 #if L1_CACHE_BYTES >= 128
249 EXPORT_SYMBOL(memcpy)
250 EXPORT_SYMBOL(memmove)
251 EXPORT_SYMBOL_KASAN(memcpy)
252 EXPORT_SYMBOL_KASAN(memmove)
258 beq 2f /* if less than 8 bytes to do */
259 andi. r0,r6,3 /* get dest word aligned */
290 rlwinm. r7,r5,32-3,3,31
295 _GLOBAL(backwards_memcpy)
296 rlwinm. r7,r5,32-3,3,31 /* r0 = r5 >> 3 */
326 rlwinm. r7,r5,32-3,3,31
331 _GLOBAL(__copy_tofrom_user)
335 andi. r0,r0,CACHELINE_MASK /* # bytes to start of cache line */
338 cmplw 0,r5,r0 /* is this more than total to do? */
339 blt 63f /* if not much to do */
340 andi. r8,r0,3 /* get it word-aligned first */
343 70: lbz r9,4(r4) /* do some bytes */
352 72: lwzu r9,4(r4) /* do some words */
361 58: srwi. r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
362 clrlwi r5,r5,32-LG_CACHELINE_BYTES
366 /* Here we decide how far ahead to prefetch the source */
372 #if MAX_COPY_PREFETCH > 1
373 /* Heuristically, for large transfers we prefetch
374 MAX_COPY_PREFETCH cachelines ahead. For small transfers
375 we prefetch 1 cacheline ahead. */
376 cmpwi r0,MAX_COPY_PREFETCH
378 li r7,MAX_COPY_PREFETCH
381 addi r3,r3,CACHELINE_BYTES
385 addi r3,r3,CACHELINE_BYTES
386 #endif /* MAX_COPY_PREFETCH > 1 */
395 /* the main body of the cacheline loop */
396 COPY_16_BYTES_WITHEX(0)
397 #if L1_CACHE_BYTES >= 32
398 COPY_16_BYTES_WITHEX(1)
399 #if L1_CACHE_BYTES >= 64
400 COPY_16_BYTES_WITHEX(2)
401 COPY_16_BYTES_WITHEX(3)
402 #if L1_CACHE_BYTES >= 128
403 COPY_16_BYTES_WITHEX(4)
404 COPY_16_BYTES_WITHEX(5)
405 COPY_16_BYTES_WITHEX(6)
406 COPY_16_BYTES_WITHEX(7)
434 /* read fault, initial single-byte copy */
437 /* write fault, initial single-byte copy */
442 /* read fault, initial word copy */
445 /* write fault, initial word copy */
451 * this stuff handles faults in the cacheline loop and branches to either
452 * 104f (if in read part) or 105f (if in write part), after updating r5
454 COPY_16_BYTES_EXCODE(0)
455 #if L1_CACHE_BYTES >= 32
456 COPY_16_BYTES_EXCODE(1)
457 #if L1_CACHE_BYTES >= 64
458 COPY_16_BYTES_EXCODE(2)
459 COPY_16_BYTES_EXCODE(3)
460 #if L1_CACHE_BYTES >= 128
461 COPY_16_BYTES_EXCODE(4)
462 COPY_16_BYTES_EXCODE(5)
463 COPY_16_BYTES_EXCODE(6)
464 COPY_16_BYTES_EXCODE(7)
469 /* read fault in cacheline loop */
472 /* fault on dcbz (effectively a write fault) */
473 /* or write fault in cacheline loop */
475 92: li r3,LG_CACHELINE_BYTES
479 /* read fault in final word loop */
482 /* write fault in final word loop */
487 /* read fault in final byte loop */
490 /* write fault in final byte loop */
495 * At this stage the number of bytes not copied is
496 * r5 + (ctr << r3), and r9 is 0 for read or 1 for write.
501 beq 120f /* shouldn't happen */
504 /* for a read fault, first try to continue the copy one byte at a time */
511 /* then clear out the destination: r3 bytes starting at 4(r6) */
522 EXPORT_SYMBOL(__copy_tofrom_user)