]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseSynchronizationLib/SynchronizationMsc.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdePkg / Library / BaseSynchronizationLib / SynchronizationMsc.c
1 /** @file
2 Implementation of synchronization functions.
3
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "BaseSynchronizationLibInternals.h"
10
11 /**
12 Microsoft Visual Studio 7.1 Function Prototypes for read write barrier Intrinsics.
13 **/
14
15 void
16 _ReadWriteBarrier (
17 void
18 );
19
20 #pragma intrinsic(_ReadWriteBarrier)
21
22 #define SPIN_LOCK_RELEASED ((UINTN) 1)
23 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
24
25 /**
26 Retrieves the architecture specific spin lock alignment requirements for
27 optimal spin lock performance.
28
29 This function retrieves the spin lock alignment requirements for optimal
30 performance on a given CPU architecture. The spin lock alignment is byte alignment.
31 It must be a power of two and is returned by this function. If there are no alignment
32 requirements, then 1 must be returned. The spin lock synchronization
33 functions must function correctly if the spin lock size and alignment values
34 returned by this function are not used at all. These values are hints to the
35 consumers of the spin lock synchronization functions to obtain optimal spin
36 lock performance.
37
38 @return The architecture specific spin lock alignment.
39
40 **/
41 UINTN
42 EFIAPI
43 GetSpinLockProperties (
44 VOID
45 )
46 {
47 return InternalGetSpinLockProperties ();
48 }
49
50 /**
51 Initializes a spin lock to the released state and returns the spin lock.
52
53 This function initializes the spin lock specified by SpinLock to the released
54 state, and returns SpinLock. Optimal performance can be achieved by calling
55 GetSpinLockProperties() to determine the size and alignment requirements for
56 SpinLock.
57
58 If SpinLock is NULL, then ASSERT().
59
60 @param SpinLock A pointer to the spin lock to initialize to the released
61 state.
62
63 @return SpinLock is in release state.
64
65 **/
66 SPIN_LOCK *
67 EFIAPI
68 InitializeSpinLock (
69 OUT SPIN_LOCK *SpinLock
70 )
71 {
72 ASSERT (SpinLock != NULL);
73
74 _ReadWriteBarrier ();
75 *SpinLock = SPIN_LOCK_RELEASED;
76 _ReadWriteBarrier ();
77
78 return SpinLock;
79 }
80
81 /**
82 Waits until a spin lock can be placed in the acquired state.
83
84 This function checks the state of the spin lock specified by SpinLock. If
85 SpinLock is in the released state, then this function places SpinLock in the
86 acquired state and returns SpinLock. Otherwise, this function waits
87 indefinitely for the spin lock to be released, and then places it in the
88 acquired state and returns SpinLock. All state transitions of SpinLock must
89 be performed using MP safe mechanisms.
90
91 If SpinLock is NULL, then ASSERT().
92 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
93 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
94 PcdSpinLockTimeout microseconds, then ASSERT().
95
96 @param SpinLock A pointer to the spin lock to place in the acquired state.
97
98 @return SpinLock acquired the lock.
99
100 **/
101 SPIN_LOCK *
102 EFIAPI
103 AcquireSpinLock (
104 IN OUT SPIN_LOCK *SpinLock
105 )
106 {
107 UINT64 Current;
108 UINT64 Previous;
109 UINT64 Total;
110 UINT64 Start;
111 UINT64 End;
112 UINT64 Timeout;
113 INT64 Cycle;
114 INT64 Delta;
115
116 if (PcdGet32 (PcdSpinLockTimeout) == 0) {
117 while (!AcquireSpinLockOrFail (SpinLock)) {
118 CpuPause ();
119 }
120 } else if (!AcquireSpinLockOrFail (SpinLock)) {
121 //
122 // Get the current timer value
123 //
124 Current = GetPerformanceCounter ();
125
126 //
127 // Initialize local variables
128 //
129 Start = 0;
130 End = 0;
131 Total = 0;
132
133 //
134 // Retrieve the performance counter properties and compute the number of performance
135 // counter ticks required to reach the timeout
136 //
137 Timeout = DivU64x32 (
138 MultU64x32 (
139 GetPerformanceCounterProperties (&Start, &End),
140 PcdGet32 (PcdSpinLockTimeout)
141 ),
142 1000000
143 );
144 Cycle = End - Start;
145 if (Cycle < 0) {
146 Cycle = -Cycle;
147 }
148
149 Cycle++;
150
151 while (!AcquireSpinLockOrFail (SpinLock)) {
152 CpuPause ();
153 Previous = Current;
154 Current = GetPerformanceCounter ();
155 Delta = (INT64)(Current - Previous);
156 if (Start > End) {
157 Delta = -Delta;
158 }
159
160 if (Delta < 0) {
161 Delta += Cycle;
162 }
163
164 Total += Delta;
165 ASSERT (Total < Timeout);
166 }
167 }
168
169 return SpinLock;
170 }
171
172 /**
173 Attempts to place a spin lock in the acquired state.
174
175 This function checks the state of the spin lock specified by SpinLock. If
176 SpinLock is in the released state, then this function places SpinLock in the
177 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
178 transitions of SpinLock must be performed using MP safe mechanisms.
179
180 If SpinLock is NULL, then ASSERT().
181 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
182
183 @param SpinLock A pointer to the spin lock to place in the acquired state.
184
185 @retval TRUE SpinLock was placed in the acquired state.
186 @retval FALSE SpinLock could not be acquired.
187
188 **/
189 BOOLEAN
190 EFIAPI
191 AcquireSpinLockOrFail (
192 IN OUT SPIN_LOCK *SpinLock
193 )
194 {
195 SPIN_LOCK LockValue;
196 VOID *Result;
197
198 ASSERT (SpinLock != NULL);
199
200 LockValue = *SpinLock;
201 ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);
202
203 _ReadWriteBarrier ();
204 Result = InterlockedCompareExchangePointer (
205 (VOID **)SpinLock,
206 (VOID *)SPIN_LOCK_RELEASED,
207 (VOID *)SPIN_LOCK_ACQUIRED
208 );
209
210 _ReadWriteBarrier ();
211 return (BOOLEAN)(Result == (VOID *)SPIN_LOCK_RELEASED);
212 }
213
214 /**
215 Releases a spin lock.
216
217 This function places the spin lock specified by SpinLock in the release state
218 and returns SpinLock.
219
220 If SpinLock is NULL, then ASSERT().
221 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
222
223 @param SpinLock A pointer to the spin lock to release.
224
225 @return SpinLock released the lock.
226
227 **/
228 SPIN_LOCK *
229 EFIAPI
230 ReleaseSpinLock (
231 IN OUT SPIN_LOCK *SpinLock
232 )
233 {
234 SPIN_LOCK LockValue;
235
236 ASSERT (SpinLock != NULL);
237
238 LockValue = *SpinLock;
239 ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);
240
241 _ReadWriteBarrier ();
242 *SpinLock = SPIN_LOCK_RELEASED;
243 _ReadWriteBarrier ();
244
245 return SpinLock;
246 }
247
248 /**
249 Performs an atomic increment of an 32-bit unsigned integer.
250
251 Performs an atomic increment of the 32-bit unsigned integer specified by
252 Value and returns the incremented value. The increment operation must be
253 performed using MP safe mechanisms.
254
255 If Value is NULL, then ASSERT().
256
257 @param Value A pointer to the 32-bit value to increment.
258
259 @return The incremented value.
260
261 **/
262 UINT32
263 EFIAPI
264 InterlockedIncrement (
265 IN volatile UINT32 *Value
266 )
267 {
268 ASSERT (Value != NULL);
269 return InternalSyncIncrement (Value);
270 }
271
272 /**
273 Performs an atomic decrement of an 32-bit unsigned integer.
274
275 Performs an atomic decrement of the 32-bit unsigned integer specified by
276 Value and returns the decremented value. The decrement operation must be
277 performed using MP safe mechanisms.
278
279 If Value is NULL, then ASSERT().
280
281 @param Value A pointer to the 32-bit value to decrement.
282
283 @return The decremented value.
284
285 **/
286 UINT32
287 EFIAPI
288 InterlockedDecrement (
289 IN volatile UINT32 *Value
290 )
291 {
292 ASSERT (Value != NULL);
293 return InternalSyncDecrement (Value);
294 }
295
296 /**
297 Performs an atomic compare exchange operation on a 16-bit unsigned integer.
298
299 Performs an atomic compare exchange operation on the 16-bit unsigned integer
300 specified by Value. If Value is equal to CompareValue, then Value is set to
301 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
302 then Value is returned. The compare exchange operation must be performed using
303 MP safe mechanisms.
304
305 If Value is NULL, then ASSERT().
306
307 @param Value A pointer to the 16-bit value for the compare exchange
308 operation.
309 @param CompareValue A 16-bit value used in a compare operation.
310 @param ExchangeValue A 16-bit value used in an exchange operation.
311
312 @return The original *Value before exchange.
313
314 **/
315 UINT16
316 EFIAPI
317 InterlockedCompareExchange16 (
318 IN OUT volatile UINT16 *Value,
319 IN UINT16 CompareValue,
320 IN UINT16 ExchangeValue
321 )
322 {
323 ASSERT (Value != NULL);
324 return InternalSyncCompareExchange16 (Value, CompareValue, ExchangeValue);
325 }
326
327 /**
328 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
329
330 Performs an atomic compare exchange operation on the 32-bit unsigned integer
331 specified 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 CompareValue,
333 then Value is returned. The compare exchange operation must be performed using
334 MP safe mechanisms.
335
336 If Value is NULL, then ASSERT().
337
338 @param Value A pointer to the 32-bit value for the compare exchange
339 operation.
340 @param CompareValue A 32-bit value used in a compare operation.
341 @param ExchangeValue A 32-bit value used in an exchange operation.
342
343 @return The original *Value before exchange.
344
345 **/
346 UINT32
347 EFIAPI
348 InterlockedCompareExchange32 (
349 IN OUT volatile UINT32 *Value,
350 IN UINT32 CompareValue,
351 IN UINT32 ExchangeValue
352 )
353 {
354 ASSERT (Value != NULL);
355 return InternalSyncCompareExchange32 (Value, CompareValue, ExchangeValue);
356 }
357
358 /**
359 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
360
361 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
362 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
363 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
364 The compare exchange operation must be performed using MP safe mechanisms.
365
366 If Value is NULL, then ASSERT().
367
368 @param Value A pointer to the 64-bit value for the compare exchange
369 operation.
370 @param CompareValue A 64-bit value used in a compare operation.
371 @param ExchangeValue A 64-bit value used in an exchange operation.
372
373 @return The original *Value before exchange.
374
375 **/
376 UINT64
377 EFIAPI
378 InterlockedCompareExchange64 (
379 IN OUT volatile UINT64 *Value,
380 IN UINT64 CompareValue,
381 IN UINT64 ExchangeValue
382 )
383 {
384 ASSERT (Value != NULL);
385 return InternalSyncCompareExchange64 (Value, CompareValue, ExchangeValue);
386 }
387
388 /**
389 Performs an atomic compare exchange operation on a pointer value.
390
391 Performs an atomic compare exchange operation on the pointer value specified
392 by Value. If Value is equal to CompareValue, then Value is set to
393 ExchangeValue and CompareValue is returned. If Value is not equal to
394 CompareValue, then Value is returned. The compare exchange operation must be
395 performed using MP safe mechanisms.
396
397 If Value is NULL, then ASSERT().
398
399 @param Value A pointer to the pointer value for the compare exchange
400 operation.
401 @param CompareValue A pointer value used in a compare operation.
402 @param ExchangeValue A pointer value used in an exchange operation.
403
404 @return The original *Value before exchange.
405 **/
406 VOID *
407 EFIAPI
408 InterlockedCompareExchangePointer (
409 IN OUT VOID *volatile *Value,
410 IN VOID *CompareValue,
411 IN VOID *ExchangeValue
412 )
413 {
414 UINT8 SizeOfValue;
415
416 SizeOfValue = (UINT8)sizeof (*Value);
417
418 switch (SizeOfValue) {
419 case sizeof (UINT32):
420 return (VOID *)(UINTN)InterlockedCompareExchange32 (
421 (volatile UINT32 *)Value,
422 (UINT32)(UINTN)CompareValue,
423 (UINT32)(UINTN)ExchangeValue
424 );
425 case sizeof (UINT64):
426 return (VOID *)(UINTN)InterlockedCompareExchange64 (
427 (volatile UINT64 *)Value,
428 (UINT64)(UINTN)CompareValue,
429 (UINT64)(UINTN)ExchangeValue
430 );
431 default:
432 ASSERT (FALSE);
433 return NULL;
434 }
435 }