Updated MSA file for EBC.
[mirror_edk2.git] / MdePkg / Library / BaseLib / SynchronizationGcc.c
1 /** @file
2 Implementation of synchronization functions.
3
4 Copyright (c) 2006 - 2007, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 Module Name: SynchronizationGcc.c
14
15 **/
16
17 #include "BaseLibInternals.h"
18
19 //
20 // GCC inline assembly for Read Write Barrier
21 //
22 #define _ReadWriteBarrier() do { asm volatile ("": : : "memory"); } while(0)
23
24 #define SPIN_LOCK_RELEASED ((UINTN) 1)
25 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
26
27 /**
28 Retrieves the architecture specific spin lock alignment requirements for
29 optimal spin lock performance.
30
31 This function retrieves the spin lock alignment requirements for optimal
32 performance on a given CPU architecture. The spin lock alignment must be a
33 power of two and is returned by this function. If there are no alignment
34 requirements, then 1 must be returned. The spin lock synchronization
35 functions must function correctly if the spin lock size and alignment values
36 returned by this function are not used at all. These values are hints to the
37 consumers of the spin lock synchronization functions to obtain optimal spin
38 lock performance.
39
40 @return The architecture specific spin lock alignment.
41
42 **/
43 UINTN
44 EFIAPI
45 GetSpinLockProperties (
46 VOID
47 )
48 {
49 // @bug May use a PCD entry to determine this alignment.
50 return 32;
51 }
52
53 /**
54 Initializes a spin lock to the released state and returns the spin lock.
55
56 This function initializes the spin lock specified by SpinLock to the released
57 state, and returns SpinLock. Optimal performance can be achieved by calling
58 GetSpinLockProperties() to determine the size and alignment requirements for
59 SpinLock.
60
61 If SpinLock is NULL, then ASSERT().
62
63 @param SpinLock A pointer to the spin lock to initialize to the released
64 state.
65
66 @return SpinLock
67
68 **/
69 SPIN_LOCK *
70 EFIAPI
71 InitializeSpinLock (
72 OUT SPIN_LOCK *SpinLock
73 )
74 {
75 ASSERT (SpinLock != NULL);
76
77 _ReadWriteBarrier();
78 *SpinLock = SPIN_LOCK_RELEASED;
79 _ReadWriteBarrier();
80
81 return SpinLock;
82 }
83
84 /**
85 Waits until a spin lock can be placed in the acquired state.
86
87 This function checks the state of the spin lock specified by SpinLock. If
88 SpinLock is in the released state, then this function places SpinLock in the
89 acquired state and returns SpinLock. Otherwise, this function waits
90 indefinitely for the spin lock to be released, and then places it in the
91 acquired state and returns SpinLock. All state transitions of SpinLock must
92 be performed using MP safe mechanisms.
93
94 If SpinLock is NULL, then ASSERT().
95 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
96 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
97 PcdSpinLockTimeout microseconds, then ASSERT().
98
99 @param SpinLock A pointer to the spin lock to place in the acquired state.
100
101 @return SpinLock
102
103 **/
104 SPIN_LOCK *
105 EFIAPI
106 AcquireSpinLock (
107 IN OUT SPIN_LOCK *SpinLock
108 )
109 {
110 UINT64 Tick;
111 UINT64 Start, End;
112 UINT64 Timeout;
113
114 Tick = 0;
115 Start = 0;
116 End = 0;
117 if (PcdGet32 (PcdSpinLockTimeout) > 0) {
118 Tick = GetPerformanceCounter ();
119 Timeout = DivU64x32 (
120 MultU64x32 (
121 GetPerformanceCounterProperties (&Start, &End),
122 PcdGet32 (PcdSpinLockTimeout)
123 ),
124 1000000
125 );
126 if (Start < End) {
127 Tick += Timeout;
128 } else {
129 Tick -= Timeout;
130 }
131 }
132
133 while (!AcquireSpinLockOrFail (SpinLock)) {
134 CpuPause ();
135 ASSERT ((Start < End) ^ (Tick <= GetPerformanceCounter ()));
136 }
137 return SpinLock;
138 }
139
140 /**
141 Attempts to place a spin lock in the acquired state.
142
143 This function checks the state of the spin lock specified by SpinLock. If
144 SpinLock is in the released state, then this function places SpinLock in the
145 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
146 transitions of SpinLock must be performed using MP safe mechanisms.
147
148 If SpinLock is NULL, then ASSERT().
149 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
150
151 @param SpinLock A pointer to the spin lock to place in the acquired state.
152
153 @retval TRUE SpinLock was placed in the acquired state.
154 @retval FALSE SpinLock could not be acquired.
155
156 **/
157 BOOLEAN
158 EFIAPI
159 AcquireSpinLockOrFail (
160 IN OUT SPIN_LOCK *SpinLock
161 )
162 {
163 SPIN_LOCK LockValue;
164 VOID *Result;
165
166 ASSERT (SpinLock != NULL);
167
168 LockValue = *SpinLock;
169 ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);
170
171 _ReadWriteBarrier ();
172 Result = InterlockedCompareExchangePointer (
173 (VOID**)SpinLock,
174 (VOID*)SPIN_LOCK_RELEASED,
175 (VOID*)SPIN_LOCK_ACQUIRED
176 );
177
178 _ReadWriteBarrier ();
179 return (BOOLEAN) (Result == (VOID*) SPIN_LOCK_RELEASED);
180 }
181
182 /**
183 Releases a spin lock.
184
185 This function places the spin lock specified by SpinLock in the release state
186 and returns SpinLock.
187
188 If SpinLock is NULL, then ASSERT().
189 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
190
191 @param SpinLock A pointer to the spin lock to release.
192
193 @return SpinLock
194
195 **/
196 SPIN_LOCK *
197 EFIAPI
198 ReleaseSpinLock (
199 IN OUT SPIN_LOCK *SpinLock
200 )
201 {
202 SPIN_LOCK LockValue;
203
204 ASSERT (SpinLock != NULL);
205
206 LockValue = *SpinLock;
207 ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);
208
209 _ReadWriteBarrier ();
210 *SpinLock = SPIN_LOCK_RELEASED;
211 _ReadWriteBarrier ();
212
213 return SpinLock;
214 }
215
216 /**
217 Performs an atomic increment of an 32-bit unsigned integer.
218
219 Performs an atomic increment of the 32-bit unsigned integer specified by
220 Value and returns the incremented value. The increment operation must be
221 performed using MP safe mechanisms. The state of the return value is not
222 guaranteed to be MP safe.
223
224 If Value is NULL, then ASSERT().
225
226 @param Value A pointer to the 32-bit value to increment.
227
228 @return The incremented value.
229
230 **/
231 UINT32
232 EFIAPI
233 InterlockedIncrement (
234 IN UINT32 *Value
235 )
236 {
237 ASSERT (Value != NULL);
238 return InternalSyncIncrement (Value);
239 }
240
241 /**
242 Performs an atomic decrement of an 32-bit unsigned integer.
243
244 Performs an atomic decrement of the 32-bit unsigned integer specified by
245 Value and returns the decremented value. The decrement operation must be
246 performed using MP safe mechanisms. The state of the return value is not
247 guaranteed to be MP safe.
248
249 If Value is NULL, then ASSERT().
250
251 @param Value A pointer to the 32-bit value to decrement.
252
253 @return The decremented value.
254
255 **/
256 UINT32
257 EFIAPI
258 InterlockedDecrement (
259 IN UINT32 *Value
260 )
261 {
262 ASSERT (Value != NULL);
263 return InternalSyncDecrement (Value);
264 }
265
266 /**
267 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
268
269 Performs an atomic compare exchange operation on the 32-bit unsigned integer
270 specified by Value. If Value is equal to CompareValue, then Value is set to
271 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
272 then Value is returned. The compare exchange operation must be performed using
273 MP safe mechanisms.
274
275 If Value is NULL, then ASSERT().
276
277 @param Value A pointer to the 32-bit value for the compare exchange
278 operation.
279 @param CompareValue 32-bit value used in compare operation.
280 @param ExchangeValue 32-bit value used in exchange operation.
281
282 @return The original *Value before exchange.
283
284 **/
285 UINT32
286 EFIAPI
287 InterlockedCompareExchange32 (
288 IN OUT UINT32 *Value,
289 IN UINT32 CompareValue,
290 IN UINT32 ExchangeValue
291 )
292 {
293 ASSERT (Value != NULL);
294 return InternalSyncCompareExchange32 (Value, CompareValue, ExchangeValue);
295 }
296
297 /**
298 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
299
300 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
301 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
302 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
303 The compare exchange operation must be performed using MP safe mechanisms.
304
305 If Value is NULL, then ASSERT().
306
307 @param Value A pointer to the 64-bit value for the compare exchange
308 operation.
309 @param CompareValue 64-bit value used in compare operation.
310 @param ExchangeValue 64-bit value used in exchange operation.
311
312 @return The original *Value before exchange.
313
314 **/
315 UINT64
316 EFIAPI
317 InterlockedCompareExchange64 (
318 IN OUT UINT64 *Value,
319 IN UINT64 CompareValue,
320 IN UINT64 ExchangeValue
321 )
322 {
323 ASSERT (Value != NULL);
324 return InternalSyncCompareExchange64 (Value, CompareValue, ExchangeValue);
325 }
326
327 /**
328 Performs an atomic compare exchange operation on a pointer value.
329
330 Performs an atomic compare exchange operation on the pointer value specified
331 by Value. If Value is equal to CompareValue, then Value is set to
332 ExchangeValue and CompareValue is returned. If Value is not equal to
333 CompareValue, then Value is returned. The compare exchange operation must be
334 performed using MP safe mechanisms.
335
336 If Value is NULL, then ASSERT().
337
338 @param Value A pointer to the pointer value for the compare exchange
339 operation.
340 @param CompareValue Pointer value used in compare operation.
341 @param ExchangeValue Pointer value used in exchange operation.
342
343 **/
344 VOID *
345 EFIAPI
346 InterlockedCompareExchangePointer (
347 IN OUT VOID **Value,
348 IN VOID *CompareValue,
349 IN VOID *ExchangeValue
350 )
351 {
352 UINT8 SizeOfValue;
353
354 SizeOfValue = sizeof (*Value);
355
356 switch (SizeOfValue) {
357 case sizeof (UINT32):
358 return (VOID*)(UINTN)InterlockedCompareExchange32 (
359 (UINT32*)Value,
360 (UINT32)(UINTN)CompareValue,
361 (UINT32)(UINTN)ExchangeValue
362 );
363 case sizeof (UINT64):
364 return (VOID*)(UINTN)InterlockedCompareExchange64 (
365 (UINT64*)Value,
366 (UINT64)(UINTN)CompareValue,
367 (UINT64)(UINTN)ExchangeValue
368 );
369 default:
370 ASSERT (FALSE);
371 return NULL;
372 }
373 }