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