2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/appleapiopts.h>
26 #include <machine/cpu_capabilities.h>
27 #include <machine/commpage.h>
32 * WARNING: this code is written for 32-bit mode, and ported by the kernel if necessary
33 * to 64-bit mode for use in the 64-bit commpage. This "port" consists of the following
34 * simple transformations:
35 * - all word compares are changed to doubleword
36 * - all "srwi[.]" opcodes are changed to "srdi[.]"
37 * Nothing else is done. For this to work, the following rules must be
39 * - do not use carry or overflow
40 * - only use record mode if you are sure the results are mode-invariant
41 * for example, all "andi." and almost all "rlwinm." are fine
42 * - do not use "slwi", "slw", or "srw"
43 * An imaginative programmer could break the porting model in other ways, but the above
44 * are the most likely problem areas. It is perhaps surprising how well in practice
45 * this simple method works.
48 /* *********************
49 * * M E M S E T _ G 5 *
50 * *********************
52 * This is a subroutine called by Libc memset and memset_pattern for large nonzero
53 * operands (zero operands are funneled into bzero.) This version is for
54 * 64-bit processors with a 128-byte cache line and Altivec.
57 * r4 = count of bytes to store (must be >= 32)
58 * r8 = ptr to the 1st byte to store (16-byte aligned)
59 * r9 = ptr to 16-byte pattern to store (16-byte aligned)
61 * r3 = not changed, since memset returns it
62 * r4 = bytes remaining to store (will be <32)
64 * r8 = ptr to next byte to store (still 16-byte aligned)
65 * r12 = not changed (holds return value for memset)
68 #define kBig (3*128) // big enough to warrant using dcbz (NB: must be >= 3*128)
72 cmplwi cr1,r4,kBig // big enough to warrant using dcbz?
73 neg r10,r8 // start to align ptr
74 mfspr r2,vrsave // we'll be using VRs
75 andi. r10,r10,0x70 // get #bytes to cache line align
76 oris r0,r2,0x8000 // we use vr0
78 li r5,16 // get offsets for "stvx"
79 lvx v0,0,r9 // load the pattern into v0
81 blt cr1,LShort // not big enough to bother with dcbz
86 beq 2f // already aligned
88 subic. r10,r10,16 // more to go?
94 // Loop over cache lines. This code uses a private protocol with the kernel:
95 // when the kernel emulates an alignment exception on a DCBZ that occurs in the
96 // commpage, it zeroes CR7. We use this to detect the case where we are operating on
97 // uncached memory, and do not use DCBZ again in this code. We assume that either
98 // all the operand is cacheable or none of it is, so we only check the first DCBZ.
100 cmpw cr7,r3,r3 // set cr7_eq (kernel will clear if DCBZ faults)
101 dcbzl 0,r8 // zero first cache line (clearing cr7 if alignment exception)
102 srwi r0,r4,7 // get #cache lines (>=2)
103 rlwinm r4,r4,0,0x7F // mask down to residual count (0..127)
104 bne-- cr7,LNoDcbz // exit if we took alignment exception on the first DCBZ
105 subic r0,r0,1 // loop 1-too-few times
106 li r11,128 // set DCBZ look-ahead
108 b 3f // use loop that DCBZs
110 // Loop over cache lines. We DCBZ one line ahead, which is a little faster.
114 dcbzl r11,r8 // zero one line ahead
127 li r0,1 // we've already DCBZ'd the last line
128 LNoDcbz: // r0: loop count
131 // Loop which does not DCBZ. Normally this is only used for last cache line,
132 // because we've already zeroed it.
144 bdnz-- 4b // optimize for the cacheable case
146 // loop over 32-byte chunks
148 srwi. r0,r4,5 // get count of 32-byte chunks
149 rlwinm r4,r4,0,0x1F // mask down to residual count (0..31)
150 beq 7f // no chunks so done
158 mtspr vrsave,r2 // restore caller's vrsave
162 COMMPAGE_DESCRIPTOR(memset_g5,_COMM_PAGE_MEMSET_PATTERN,kCache128+k64Bit+kHasAltivec,0, \
163 kCommPageBoth+kPort32to64)