2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/appleapiopts.h>
25 #include <ppc/asm.h> // EXT, LEXT
26 #include <machine/cpu_capabilities.h>
27 #include <machine/commpage.h>
30 /* OSAtomic.h library native implementations. */
35 atomic_add32: // int32_t OSAtomicAdd32( int32_t amt, int32_t *value );
44 COMMPAGE_DESCRIPTOR(atomic_add32,_COMM_PAGE_ATOMIC_ADD32,0,0,kCommPageBoth)
47 atomic_add64: // int64_t OSAtomicAdd64( int64_t amt, int64_t *value );
56 COMMPAGE_DESCRIPTOR(atomic_add64,_COMM_PAGE_ATOMIC_ADD64,k64Bit,0,kCommPage64)
58 /* WARNING: Libc clients assume compare-and-swap preserves r4, r5, and r9-r12! */
59 /* This is the no-barrier version */
60 compare_and_swap32_on32: // bool OSAtomicCompareAndSwap32( int32_t old, int32_t new, int32_t *value);
70 li r3,0 // return failure
73 COMMPAGE_DESCRIPTOR(compare_and_swap32_on32,_COMM_PAGE_COMPARE_AND_SWAP32,0,k64Bit,kCommPageBoth)
76 /* WARNING: Libc clients assume compare-and-swap preserves r4, r5, and r9-r12! */
77 /* This is the no-barrier version */
78 compare_and_swap32_on64: // bool OSAtomicCompareAndSwap32( int32_t old, int32_t new, int32_t *value);
88 li r8,-8 // on 970, must release reservation
89 li r3,0 // return failure
90 stwcx. r4,r8,r1 // store into red zone to release
93 COMMPAGE_DESCRIPTOR(compare_and_swap32_on64,_COMM_PAGE_COMPARE_AND_SWAP32,k64Bit,0,kCommPageBoth)
96 /* WARNING: Libc clients assume compare-and-swap preserves r4, r5, and r9-r12! */
97 /* This is the no-barrier version */
98 compare_and_swap64: // bool OSAtomicCompareAndSwap64( int64_t old, int64_t new, int64_t *value);
108 li r8,-8 // on 970, must release reservation
109 li r3,0 // return failure
110 stdcx. r4,r8,r1 // store into red zone to release
113 COMMPAGE_DESCRIPTOR(compare_and_swap64,_COMM_PAGE_COMPARE_AND_SWAP64,k64Bit,0,kCommPage64)
115 /* WARNING: Libc clients assume compare-and-swap preserves r4, r5, and r9-r12! */
116 /* This version of compare-and-swap incorporates a memory barrier. */
117 compare_and_swap32_on32b: // bool OSAtomicCompareAndSwapBarrier32( int32_t old, int32_t new, int32_t *value);
118 eieio // write barrier, NOP'd on a UP
125 isync // read barrier, NOP'd on a UP
129 li r3,0 // return failure
132 COMMPAGE_DESCRIPTOR(compare_and_swap32_on32b,_COMM_PAGE_COMPARE_AND_SWAP32B,0,k64Bit,kCommPageBoth+kCommPageSYNC+kCommPageISYNC)
135 /* WARNING: Libc clients assume compare-and-swap preserves r4, r5, and r9-r12! */
136 /* This version of compare-and-swap incorporates a memory barrier. */
137 compare_and_swap32_on64b: // bool OSAtomicCompareAndSwapBarrier32( int32_t old, int32_t new, int32_t *value);
138 lwsync // write barrier, NOP'd on a UP
145 isync // read barrier, NOP'd on a UP
149 li r8,-8 // on 970, must release reservation
150 li r3,0 // return failure
151 stwcx. r4,r8,r1 // store into red zone to release
154 COMMPAGE_DESCRIPTOR(compare_and_swap32_on64b,_COMM_PAGE_COMPARE_AND_SWAP32B,k64Bit,0,kCommPageBoth+kCommPageSYNC+kCommPageISYNC)
157 /* WARNING: Libc clients assume compare-and-swap preserves r4, r5, and r9-r12! */
158 /* This version of compare-and-swap incorporates a memory barrier. */
159 compare_and_swap64b: // bool OSAtomicCompareAndSwapBarrier64( int64_t old, int64_t new, int64_t *value);
160 lwsync // write barrier, NOP'd on a UP
167 isync // read barrier, NOP'd on a UP
171 li r8,-8 // on 970, must release reservation
172 li r3,0 // return failure
173 stdcx. r4,r8,r1 // store into red zone to release
176 COMMPAGE_DESCRIPTOR(compare_and_swap64b,_COMM_PAGE_COMPARE_AND_SWAP64B,k64Bit,0,kCommPage64+kCommPageSYNC+kCommPageISYNC)
179 atomic_enqueue32: // void OSAtomicEnqueue( void **list, void *new, size_t offset);
181 lwarx r6,0,r3 // get link to 1st on list
182 stwx r6,r4,r5 // hang list off new node
183 eieio // make sure the "stwx" comes before "stwcx." (nop'd on UP)
184 stwcx. r4,0,r3 // make new 1st on list
188 COMMPAGE_DESCRIPTOR(atomic_enqueue32,_COMM_PAGE_ENQUEUE,0,0,kCommPageSYNC+kCommPage32)
191 atomic_enqueue64: // void OSAtomicEnqueue( void **list, void *new, size_t offset);
193 ldarx r6,0,r3 // get link to 1st on list
194 stdx r6,r4,r5 // hang list off new node
195 lwsync // make sure the "stdx" comes before the "stdcx." (nop'd on UP)
196 stdcx. r4,0,r3 // make new 1st on list
200 COMMPAGE_DESCRIPTOR(atomic_enqueue64,_COMM_PAGE_ENQUEUE,k64Bit,0,kCommPageSYNC+kCommPage64)
203 atomic_dequeue32_on32: // void* OSAtomicDequeue( void **list, size_t offset);
206 lwarx r3,0,r5 // get 1st in list
208 beqlr // yes, list empty
209 lwzx r6,r3,r4 // get 2nd
210 stwcx. r6,0,r5 // make 2nd first
212 isync // cancel read-aheads (nop'd on UP)
215 COMMPAGE_DESCRIPTOR(atomic_dequeue32_on32,_COMM_PAGE_DEQUEUE,0,k64Bit,kCommPageISYNC+kCommPage32)
218 atomic_dequeue32_on64: // void* OSAtomicDequeue( void **list, size_t offset);
220 li r7,-8 // use red zone to release reservation if necessary
222 lwarx r3,0,r5 // get 1st in list
225 lwzx r6,r3,r4 // get 2nd
226 stwcx. r6,0,r5 // make 2nd first
227 isync // cancel read-aheads (nop'd on UP)
228 beqlr++ // return next element in r2
229 b 1b // retry (lost reservation)
231 stwcx. r0,r7,r1 // on 970, release reservation using red zone
234 COMMPAGE_DESCRIPTOR(atomic_dequeue32_on64,_COMM_PAGE_DEQUEUE,k64Bit,0,kCommPageISYNC+kCommPage32)
237 atomic_dequeue64: // void* OSAtomicDequeue( void **list, size_t offset);
239 li r7,-8 // use red zone to release reservation if necessary
241 ldarx r3,0,r5 // get 1st in list
244 ldx r6,r3,r4 // get 2nd
245 stdcx. r6,0,r5 // make 2nd first
246 isync // cancel read-aheads (nop'd on UP)
247 beqlr++ // return next element in r2
248 b 1b // retry (lost reservation)
250 stdcx. r0,r7,r1 // on 970, release reservation using red zone
253 COMMPAGE_DESCRIPTOR(atomic_dequeue64,_COMM_PAGE_DEQUEUE,k64Bit,0,kCommPageISYNC+kCommPage64)
256 memory_barrier_up: // void OSMemoryBarrier( void )
257 blr // nothing to do on UP
259 COMMPAGE_DESCRIPTOR(memory_barrier_up,_COMM_PAGE_MEMORY_BARRIER,kUP,0,kCommPageBoth)
262 memory_barrier_mp32: // void OSMemoryBarrier( void )
263 isync // we use eieio in preference to sync...
264 eieio // ...because it is faster
267 COMMPAGE_DESCRIPTOR(memory_barrier_mp32,_COMM_PAGE_MEMORY_BARRIER,0,kUP+k64Bit,kCommPage32)
270 memory_barrier_mp64: // void OSMemoryBarrier( void )
272 lwsync // on 970, lwsync is faster than eieio
275 COMMPAGE_DESCRIPTOR(memory_barrier_mp64,_COMM_PAGE_MEMORY_BARRIER,k64Bit,kUP,kCommPageBoth)