3 * MIPS Technologies, Inc., California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the MIPS Technologies, Inc., nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #ifdef ANDROID_CHANGES
31 # include "machine/asm.h"
32 # include "machine/regdef.h"
33 # define PREFETCH_STORE_HINT PREFETCH_HINT_PREPAREFORSTORE
35 # include "machine/asm.h"
36 # include "machine/regdef.h"
37 # define PREFETCH_STORE_HINT PREFETCH_HINT_PREPAREFORSTORE
43 /* Check to see if the MIPS architecture we are compiling for supports
46 #if (__mips == 4) || (__mips == 5) || (__mips == 32) || (__mips == 64)
47 # ifndef DISABLE_PREFETCH
52 #if defined(_MIPS_SIM) && ((_MIPS_SIM == _ABI64) || (_MIPS_SIM == _ABIN32))
53 # ifndef DISABLE_DOUBLE
59 # ifndef DISABLE_DOUBLE_ALIGN
64 /* Some asm.h files do not have the L macro definition. */
66 # if _MIPS_SIM == _ABIO32
67 # define L(label) $L ## label
69 # define L(label) .L ## label
73 /* Some asm.h files do not have the PTR_ADDIU macro definition. */
76 # define PTR_ADDIU daddiu
78 # define PTR_ADDIU addiu
82 /* New R6 instructions that may not be in asm.h. */
84 # if _MIPS_SIM == _ABI64
91 /* Using PREFETCH_HINT_PREPAREFORSTORE instead of PREFETCH_STORE
92 or PREFETCH_STORE_STREAMED offers a large performance advantage
93 but PREPAREFORSTORE has some special restrictions to consider.
95 Prefetch with the 'prepare for store' hint does not copy a memory
96 location into the cache, it just allocates a cache line and zeros
97 it out. This means that if you do not write to the entire cache
98 line before writing it out to memory some data will get zero'ed out
99 when the cache line is written back to memory and data will be lost.
101 There are ifdef'ed sections of this memcpy to make sure that it does not
102 do prefetches on cache lines that are not going to be completely written.
103 This code is only needed and only used when PREFETCH_STORE_HINT is set to
104 PREFETCH_HINT_PREPAREFORSTORE. This code assumes that cache lines are
105 less than MAX_PREFETCH_SIZE bytes and if the cache line is larger it will
106 not work correctly. */
109 # define PREFETCH_HINT_STORE 1
110 # define PREFETCH_HINT_STORE_STREAMED 5
111 # define PREFETCH_HINT_STORE_RETAINED 7
112 # define PREFETCH_HINT_PREPAREFORSTORE 30
114 /* If we have not picked out what hints to use at this point use the
115 standard load and store prefetch hints. */
116 # ifndef PREFETCH_STORE_HINT
117 # define PREFETCH_STORE_HINT PREFETCH_HINT_STORE
120 /* We double everything when USE_DOUBLE is true so we do 2 prefetches to
121 get 64 bytes in that case. The assumption is that each individual
122 prefetch brings in 32 bytes. */
124 # define PREFETCH_CHUNK 64
125 # define PREFETCH_FOR_STORE(chunk, reg) \
126 pref PREFETCH_STORE_HINT, (chunk)*64(reg); \
127 pref PREFETCH_STORE_HINT, ((chunk)*64)+32(reg)
129 # define PREFETCH_CHUNK 32
130 # define PREFETCH_FOR_STORE(chunk, reg) \
131 pref PREFETCH_STORE_HINT, (chunk)*32(reg)
134 /* MAX_PREFETCH_SIZE is the maximum size of a prefetch, it must not be less
135 than PREFETCH_CHUNK, the assumed size of each prefetch. If the real size
136 of a prefetch is greater than MAX_PREFETCH_SIZE and the PREPAREFORSTORE
137 hint is used, the code will not work correctly. If PREPAREFORSTORE is not
138 used than MAX_PREFETCH_SIZE does not matter. */
139 # define MAX_PREFETCH_SIZE 128
140 /* PREFETCH_LIMIT is set based on the fact that we never use an offset greater
141 than 5 on a STORE prefetch and that a single prefetch can never be larger
142 than MAX_PREFETCH_SIZE. We add the extra 32 when USE_DOUBLE is set because
143 we actually do two prefetches in that case, one 32 bytes after the other. */
145 # define PREFETCH_LIMIT (5 * PREFETCH_CHUNK) + 32 + MAX_PREFETCH_SIZE
147 # define PREFETCH_LIMIT (5 * PREFETCH_CHUNK) + MAX_PREFETCH_SIZE
150 # if (PREFETCH_STORE_HINT == PREFETCH_HINT_PREPAREFORSTORE) \
151 && ((PREFETCH_CHUNK * 4) < MAX_PREFETCH_SIZE)
152 /* We cannot handle this because the initial prefetches may fetch bytes that
153 are before the buffer being copied. We start copies with an offset
154 of 4 so avoid this situation when using PREPAREFORSTORE. */
155 # error "PREFETCH_CHUNK is too large and/or MAX_PREFETCH_SIZE is too small."
157 #else /* USE_PREFETCH not defined */
158 # define PREFETCH_FOR_STORE(offset, reg)
161 #if __mips_isa_rev > 5
162 # if (PREFETCH_STORE_HINT == PREFETCH_HINT_PREPAREFORSTORE)
163 # undef PREFETCH_STORE_HINT
164 # define PREFETCH_STORE_HINT PREFETCH_HINT_STORE_STREAMED
169 /* Allow the routine to be named something else if desired. */
171 # define MEMSET_NAME memset
174 /* We load/store 64 bits at a time when USE_DOUBLE is true.
175 The C_ prefix stands for CHUNK and is used to avoid macro name
176 conflicts with system header files. */
181 # define C_STHI sdl /* high part is left in big-endian */
183 # define C_STHI sdr /* high part is right in little-endian */
188 # define C_STHI swl /* high part is left in big-endian */
190 # define C_STHI swr /* high part is right in little-endian */
194 /* Bookkeeping values for 32 vs. 64 bit mode. */
197 # define NSIZEMASK 0x3f
198 # define NSIZEDMASK 0x7f
201 # define NSIZEMASK 0x1f
202 # define NSIZEDMASK 0x3f
204 #define UNIT(unit) ((unit)*NSIZE)
205 #define UNITM1(unit) (((unit)*NSIZE)-1)
207 #ifdef ANDROID_CHANGES
215 /* If the size is less than 2*NSIZE (8 or 16), go to L(lastb). Regardless of
216 size, copy dst pointer to v0 for the return value. */
217 slti t2,a2,(2 * NSIZE)
221 /* If memset value is not zero, we copy it to all the bytes in a 32 or 64
223 beq a1,zero,L(set0) /* If memset value is zero no smear */
227 /* smear byte into 32 or 64 bit word */
228 #if ((__mips == 64) || (__mips == 32)) && (__mips_isa_rev >= 2)
230 dins a1, a1, 8, 8 /* Replicate fill byte into half-word. */
231 dins a1, a1, 16, 16 /* Replicate fill byte into word. */
232 dins a1, a1, 32, 32 /* Replicate fill byte into dbl word. */
234 ins a1, a1, 8, 8 /* Replicate fill byte into half-word. */
235 ins a1, a1, 16, 16 /* Replicate fill byte into word. */
255 /* If the destination address is not aligned do a partial store to get it
256 aligned. If it is already aligned just jump to L(aligned). */
259 andi t2,a3,(NSIZE-1) /* word-unaligned address? */
260 beq t2,zero,L(aligned) /* t2 is the unalignment count */
302 /* If USE_DOUBLE is not set we may still want to align the data on a 16
303 byte boundry instead of an 8 byte boundry to maximize the opportunity
304 of proAptiv chips to do memory bonding (combining two sequential 4
305 byte stores into one 8 byte store). We know there are at least 4 bytes
306 left to store or we would have jumped to L(lastb) earlier in the code. */
309 beq t2,zero,L(double_aligned)
316 /* Now the destination is aligned to (word or double word) aligned address
317 Set a2 to count how many bytes we have to copy after all the 64/128 byte
318 chunks are copied and a3 to the dest pointer after all the 64/128 byte
319 chunks have been copied. We will loop, incrementing a0 until it equals
321 andi t8,a2,NSIZEDMASK /* any whole 64-byte/128-byte chunks? */
322 beq a2,t8,L(chkw) /* if a2==t8, no 64-byte/128-byte chunks */
323 PTR_SUBU a3,a2,t8 /* subtract from a2 the reminder */
324 PTR_ADDU a3,a0,a3 /* Now a3 is the final dst after loop */
326 /* When in the loop we may prefetch with the 'prepare to store' hint,
327 in this case the a0+x should not be past the "t0-32" address. This
328 means: for x=128 the last "safe" a0 address is "t0-160". Alternatively,
329 for x=64 the last "safe" a0 address is "t0-96" In the current version we
330 will use "prefetch hint,128(a0)", so "t0-160" is the limit. */
331 #if defined(USE_PREFETCH) \
332 && (PREFETCH_STORE_HINT == PREFETCH_HINT_PREPAREFORSTORE)
333 PTR_ADDU t0,a0,a2 /* t0 is the "past the end" address */
334 PTR_SUBU t9,t0,PREFETCH_LIMIT /* t9 is the "last safe pref" address */
336 #if defined(USE_PREFETCH) \
337 && (PREFETCH_STORE_HINT != PREFETCH_HINT_PREPAREFORSTORE)
338 PREFETCH_FOR_STORE (1, a0)
339 PREFETCH_FOR_STORE (2, a0)
340 PREFETCH_FOR_STORE (3, a0)
344 #if defined(USE_PREFETCH) \
345 && (PREFETCH_STORE_HINT == PREFETCH_HINT_PREPAREFORSTORE)
346 sltu v1,t9,a0 /* If a0 > t9 don't use next prefetch */
351 PREFETCH_FOR_STORE (4, a0)
352 PREFETCH_FOR_STORE (5, a0)
354 PREFETCH_FOR_STORE (2, a0)
373 PTR_ADDIU a0,a0,UNIT(16) /* adding 64/128 to dest */
378 /* Here we have dest word-aligned but less than 64-bytes or 128 bytes to go.
379 Check for a 32(64) byte chunk and copy if if there is one. Otherwise
380 jump down to L(chk1w) to handle the tail end of the copy. */
382 andi t8,a2,NSIZEMASK /* is there a 32-byte/64-byte chunk. */
383 /* the t8 is the reminder count past 32-bytes */
384 beq a2,t8,L(chk1w)/* when a2==t8, no 32-byte chunk */
394 PTR_ADDIU a0,a0,UNIT(8)
396 /* Here we have less than 32(64) bytes to set. Set up for a loop to
397 copy one word (or double word) at a time. Set a2 to count how many
398 bytes we have to copy after all the word (or double word) chunks are
399 copied and a3 to the dest pointer after all the (d)word chunks have
400 been copied. We will loop, incrementing a0 until a0 equals a3. */
402 andi a2,t8,(NSIZE-1) /* a2 is the reminder past one (d)word chunks */
404 PTR_SUBU a3,t8,a2 /* a3 is count of bytes in one (d)word chunks */
405 PTR_ADDU a3,a0,a3 /* a3 is the dst address after loop */
407 /* copying in words (4-byte or 8 byte chunks) */
409 PTR_ADDIU a0,a0,UNIT(1)
410 bne a0,a3,L(wordCopy_loop)
413 /* Copy the last 8 (or 16) bytes */
416 PTR_ADDU a3,a0,a2 /* a3 is the last dst address */
419 bne a0,a3,L(lastbloop)