1 /* $NetBSD: bzero.S,v 1.6 2003/03/15 14:10:21 hannken Exp $ */
4 * Copyright (C) 2001 Martin J. Laubach <mjl@NetBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /*----------------------------------------------------------------------*/
31 #include <machine/asm.h>
36 #define USE_STSWX 0 /* don't. slower than trivial copy loop */
38 /*----------------------------------------------------------------------*/
40 void bzero(void *b %r3, size_t len %r4);
41 void * memset(void *b %r3, int c %r4, size_t len %r5);
43 /*----------------------------------------------------------------------*/
52 li r_val, 0 /* Value to stuff in */
59 beqlr- cr1 /* Nothing to do */
61 rlwimi %r0, %r4, 8, 16, 23 /* word extend fill value */
62 rlwimi %r0, %r0, 16, 0, 15
64 bne- simple_fill /* =! 0, use trivial fill */
67 /*----------------------------------------------------------------------*/
69 /* First find out cache line size */
72 bl _GLOBAL_OFFSET_TABLE_@local-4
75 lwz %r5,cache_info@got(%r10)
78 ori %r5,%r5,cache_info@l
82 bne+ cb_cacheline_known
84 /*----------------------------------------------------------------------*/
86 #define CPU_CACHELINE 1
87 #define CPU_CACHEINFO 5
89 #define STKFRAME_SZ 48
99 stwu %r1, -STKFRAME_SZ(%r1)
101 stw %r8, R8_SAVE(%r1)
102 stw %r3, R3_SAVE(%r1)
103 stw %r4, R4_SAVE(%r1)
104 stw %r0, R0_SAVE(%r1)
108 li %r0, CTL_MACHDEP /* Construct MIB */
110 li %r0, CPU_CACHEINFO
113 li %r0, 4*4 /* Oldlenp := 4*4 */
114 stw %r0, OLDPLEN(%r1)
117 li %r4, 2 /* namelen */
118 /* %r5 already contains &cache_info */
119 addi %r6, %r1, OLDPLEN
122 bl PIC_PLT(_C_LABEL(sysctl))
124 cmpwi %r3, 0 /* Check result */
127 /* Failure, try older sysctl */
129 li %r0, CTL_MACHDEP /* Construct MIB */
131 li %r0, CPU_CACHELINE
134 li %r0, 4 /* Oldlenp := 4 */
135 stw %r0, OLDPLEN(%r1)
138 li %r4, 2 /* namelen */
141 bl _GLOBAL_OFFSET_TABLE_@local-4
144 lwz %r5,cache_info@got(%r10)
147 lis %r5,cache_info+4@h
148 ori %r5,%r5,cache_info+4@l
150 addi %r6, %r1, OLDPLEN
153 bl PIC_PLT(_C_LABEL(sysctl))
155 lwz %r8, R8_SAVE(%r1)
156 lwz %r3, R3_SAVE(%r1)
157 lwz %r4, R4_SAVE(%r1)
158 lwz %r0, R0_SAVE(%r1)
161 bl _GLOBAL_OFFSET_TABLE_@local-4
163 lwz %r9, cache_info@got(%r10)
166 lis %r5, cache_info+4@ha
167 lwz %r9, cache_info+4@l(%r5)
169 la %r1, STKFRAME_SZ(%r1)
173 cntlzw %r6, %r9 /* compute shift value */
178 lwz %r6, cache_sh@got(%r10)
182 stw %r5, cache_sh@l(%r6)
184 /*----------------------------------------------------------------------*/
185 /* Okay, we know the cache line size (%r9) and shift value (%r10) */
188 lwz %r5, cache_info@got(%r10)
190 lwz %r5, cache_sh@got(%r10)
193 lis %r9, cache_info+4@ha
194 lwz %r9, cache_info+4@l(%r9)
195 lis %r10, cache_sh@ha
196 lwz %r10, cache_sh@l(%r10)
200 #ifdef MULTIPROCESSOR
201 mfsprg %r10, 0 /* Get cpu_info pointer */
203 lis %r10, cpu_info_store@ha
204 addi %r10, %r10, cpu_info_store@l
206 lwz %r9, CPU_CI+4(%r10) /* Load D$ line size */
207 cntlzw %r10, %r9 /* Calculate shift.. */
211 /* Back in memory filling business */
213 cmplwi cr1, r_len, 0 /* Nothing to do? */
215 cmplw r_len, %r5 /* <= 2*CL bytes to move? */
216 beqlr- cr1 /* then do nothing */
218 blt+ simple_fill /* a trivial fill routine */
220 /* Word align the block, fill bytewise until dst even*/
222 andi. %r5, r_dst, 0x03
224 beq+ cb_aligned_w /* already aligned to word? */
226 subf %r5, %r5, %r6 /* bytes to fill to align4 */
230 add r_dst, %r5, r_dst
235 1: stbu r_val, 1(r_dst) /* Fill bytewise */
240 subf r_len, %r5, r_len
242 cb_aligned_w: /* Cache block align, fill wordwise until dst aligned */
244 /* I know I have something to do since we had > 2*CL initially */
245 /* so no need to check for r_len = 0 */
247 subi %r6, %r9, 1 /* CL mask */
251 beq cb_aligned_cb /* already on CL boundary? */
253 subf %r5, %r5, %r6 /* words to fill to alignment */
256 subf r_len, %r5, r_len
259 1: stwu r_val, 4(r_dst) /* Fill wordwise */
263 cb_aligned_cb: /* no need to check r_len, see above */
265 srw. %r5, r_len, %r10 /* Number of cache blocks */
270 subf r_len, %r5, r_len
272 1: dcbz 0, r_dst /* Clear blockwise */
273 add r_dst, r_dst, %r9
276 cblocks_done: /* still CL aligned, but less than CL bytes left */
281 blt- sf_bytewise /* <8 remaining? */
284 /*----------------------------------------------------------------------*/
288 beqlr- /* Nothing to do */
292 cmplwi cr1, r_len, 12 /* < 12 bytes to move? */
294 cmplwi cr1, r_len, 8 /* < 8 bytes to move? */
296 andi. %r5, r_dst, 0x03 /* bytes to fill to align4 */
297 blt cr1, sf_bytewise /* trivial byte mover */
301 beq+ sf_aligned_w /* dest is word aligned */
306 add r_dst, %r5, r_dst
308 mtctr %r5 /* nope, then fill bytewise */
309 subi r_dst, r_dst, 1 /* until it is */
310 1: stbu r_val, 1(r_dst)
315 subf r_len, %r5, r_len
317 sf_aligned_w: /* no need to check r_len since it were >= 8 bytes initially */
325 slwi %r5, %r5, 3 /* adjust len */
326 subf. r_len, %r5, r_len
328 1: stswi %r6, r_dst, 8
332 srwi %r5, r_len, 2 /* words to fill */
336 subf. r_len, %r5, r_len /* adjust len for fill */
339 1: stwu r_val, 4(r_dst)
344 sf_word_done: bne- sf_bytewise
346 sf_return: mr %r3, %r8 /* restore orig ptr */
347 blr /* for memset functionality */
361 1: stbu r_val, 1(r_dst)
364 mr %r3, %r8 /* restore orig ptr */
365 blr /* for memset functionality */
367 /*----------------------------------------------------------------------*/
370 cache_info: .long -1, -1, -1, -1
374 /*----------------------------------------------------------------------*/