]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseSynchronizationLib/Arm/Synchronization.S
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / BaseSynchronizationLib / Arm / Synchronization.S
1 // Implementation of synchronization functions for ARM architecture
2 //
3 // Copyright (c) 2012-2015, ARM Limited. All rights reserved.
4 // Copyright (c) 2015, Linaro Limited. All rights reserved.
5 //
6 // SPDX-License-Identifier: BSD-2-Clause-Patent
7 //
8 //
9
10 .text
11 .align 3
12
13 GCC_ASM_EXPORT(InternalSyncCompareExchange16)
14 GCC_ASM_EXPORT(InternalSyncCompareExchange32)
15 GCC_ASM_EXPORT(InternalSyncCompareExchange64)
16 GCC_ASM_EXPORT(InternalSyncIncrement)
17 GCC_ASM_EXPORT(InternalSyncDecrement)
18
19 /**
20 Performs an atomic compare exchange operation on a 16-bit unsigned integer.
21
22 Performs an atomic compare exchange operation on the 16-bit unsigned integer
23 specified by Value. If Value is equal to CompareValue, then Value is set to
24 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
25 then Value is returned. The compare exchange operation must be performed using
26 MP safe mechanisms.
27
28 @param Value A pointer to the 16-bit value for the compare exchange
29 operation.
30 @param CompareValue 16-bit value used in compare operation.
31 @param ExchangeValue 16-bit value used in exchange operation.
32
33 @return The original *Value before exchange.
34
35 **/
36 //UINT16
37 //EFIAPI
38 //InternalSyncCompareExchange16 (
39 // IN volatile UINT16 *Value,
40 // IN UINT16 CompareValue,
41 // IN UINT16 ExchangeValue
42 // )
43 ASM_PFX(InternalSyncCompareExchange16):
44 dmb
45
46 InternalSyncCompareExchange16Again:
47 ldrexh r3, [r0]
48 cmp r3, r1
49 bne InternalSyncCompareExchange16Fail
50
51 InternalSyncCompareExchange16Exchange:
52 strexh ip, r2, [r0]
53 cmp ip, #0
54 bne InternalSyncCompareExchange16Again
55
56 InternalSyncCompareExchange16Fail:
57 dmb
58 mov r0, r3
59 bx lr
60
61 /**
62 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
63
64 Performs an atomic compare exchange operation on the 32-bit unsigned integer
65 specified by Value. If Value is equal to CompareValue, then Value is set to
66 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
67 then Value is returned. The compare exchange operation must be performed using
68 MP safe mechanisms.
69
70 @param Value A pointer to the 32-bit value for the compare exchange
71 operation.
72 @param CompareValue 32-bit value used in compare operation.
73 @param ExchangeValue 32-bit value used in exchange operation.
74
75 @return The original *Value before exchange.
76
77 **/
78 //UINT32
79 //EFIAPI
80 //InternalSyncCompareExchange32 (
81 // IN volatile UINT32 *Value,
82 // IN UINT32 CompareValue,
83 // IN UINT32 ExchangeValue
84 // )
85 ASM_PFX(InternalSyncCompareExchange32):
86 dmb
87
88 InternalSyncCompareExchange32Again:
89 ldrex r3, [r0]
90 cmp r3, r1
91 bne InternalSyncCompareExchange32Fail
92
93 InternalSyncCompareExchange32Exchange:
94 strex ip, r2, [r0]
95 cmp ip, #0
96 bne InternalSyncCompareExchange32Again
97
98 InternalSyncCompareExchange32Fail:
99 dmb
100 mov r0, r3
101 bx lr
102
103 /**
104 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
105
106 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
107 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
108 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
109 The compare exchange operation must be performed using MP safe mechanisms.
110
111 @param Value A pointer to the 64-bit value for the compare exchange
112 operation.
113 @param CompareValue 64-bit value used in compare operation.
114 @param ExchangeValue 64-bit value used in exchange operation.
115
116 @return The original *Value before exchange.
117
118 **/
119 //UINT64
120 //EFIAPI
121 //InternalSyncCompareExchange64 (
122 // IN volatile UINT64 *Value, // r0
123 // IN UINT64 CompareValue, // r2-r3
124 // IN UINT64 ExchangeValue // stack
125 // )
126 ASM_PFX(InternalSyncCompareExchange64):
127 push { r4-r7 }
128 ldrd r4, r5, [sp, #16]
129 dmb
130
131 InternalSyncCompareExchange64Again:
132 ldrexd r6, r7, [r0]
133 cmp r6, r2
134 cmpeq r7, r3
135 bne InternalSyncCompareExchange64Fail
136
137 InternalSyncCompareExchange64Exchange:
138 strexd ip, r4, r5, [r0]
139 cmp ip, #0
140 bne InternalSyncCompareExchange64Again
141
142 InternalSyncCompareExchange64Fail:
143 dmb
144 mov r0, r6
145 mov r1, r7
146 pop { r4-r7 }
147 bx lr
148
149 /**
150 Performs an atomic increment of an 32-bit unsigned integer.
151
152 Performs an atomic increment of the 32-bit unsigned integer specified by
153 Value and returns the incremented value. The increment operation must be
154 performed using MP safe mechanisms. The state of the return value is not
155 guaranteed to be MP safe.
156
157 @param Value A pointer to the 32-bit value to increment.
158
159 @return The incremented value.
160
161 **/
162 //UINT32
163 //EFIAPI
164 //InternalSyncIncrement (
165 // IN volatile UINT32 *Value
166 // )
167 ASM_PFX(InternalSyncIncrement):
168 dmb
169 TryInternalSyncIncrement:
170 ldrex r1, [r0]
171 add r1, r1, #1
172 strex r2, r1, [r0]
173 cmp r2, #0
174 bne TryInternalSyncIncrement
175 dmb
176 mov r0, r1
177 bx lr
178
179 /**
180 Performs an atomic decrement of an 32-bit unsigned integer.
181
182 Performs an atomic decrement of the 32-bit unsigned integer specified by
183 Value and returns the decrement value. The decrement operation must be
184 performed using MP safe mechanisms. The state of the return value is not
185 guaranteed to be MP safe.
186
187 @param Value A pointer to the 32-bit value to decrement.
188
189 @return The decrement value.
190
191 **/
192 //UINT32
193 //EFIAPI
194 //InternalSyncDecrement (
195 // IN volatile UINT32 *Value
196 // )
197 ASM_PFX(InternalSyncDecrement):
198 dmb
199 TryInternalSyncDecrement:
200 ldrex r1, [r0]
201 sub r1, r1, #1
202 strex r2, r1, [r0]
203 cmp r2, #0
204 bne TryInternalSyncDecrement
205 dmb
206 mov r0, r1
207 bx lr