summaryrefslogtreecommitdiff
path: root/ArmPkg/Include/AsmMacroIoLib.h
blob: 0276275a41f09e8318c711d7fccd080333f2ab83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/** @file
  Macros to work around lack of Apple support for LDR register, =expr

  Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
  Copyright (c) 2011-2012, ARM Ltd. All rights reserved.<BR>

  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/


#ifndef __MACRO_IO_LIB_H__
#define __MACRO_IO_LIB_H__

#if defined(__APPLE__)

//
//  ldr reg, =expr does not work with current Apple tool chain. So do the work our selves
//

// returns _Data in R0 and _Address in R1
#define MmioWrite32(_Address, _Data) \
  ldr  r1, [pc, #8]     ;            \
  ldr  r0, [pc, #8]     ;            \
  str  r0, [r1]         ;            \
  b    1f               ;            \
  .long (_Address)      ;            \
  .long (_Data) ;                    \
1:

// returns _Data in R0 and _Address in R1, and _OrData in r2
#define MmioOr32(_Address, _OrData) \
  ldr  r1, [pc, #16]    ;           \
  ldr  r2, [pc, #16]    ;           \
  ldr  r0, [r1]         ;           \
  orr  r0, r0, r2       ;           \
  str  r0, [r1]         ;           \
  b    1f               ;           \
  .long (_Address)      ;           \
  .long (_OrData)       ;           \
1:

// returns _Data in R0 and _Address in R1, and _OrData in r2
#define MmioAnd32(_Address, _AndData) \
  ldr  r1, [pc, #16]    ;             \
  ldr  r2, [pc, #16]    ;             \
  ldr  r0, [r1]         ;             \
  and  r0, r0, r2       ;             \
  str  r0, [r1]         ;             \
  b    1f               ;             \
  .long (_Address)      ;             \
  .long (_AndData)       ;             \
1:

// returns result in R0, _Address in R1, and _OrData in r2
#define MmioAndThenOr32(_Address, _AndData, _OrData)  \
  ldr  r1, [pc, #24]    ;                             \
  ldr  r0, [r1]         ;                             \
  ldr  r2, [pc, #20]    ;                             \
  and  r0, r0, r2       ;                             \
  ldr  r2, [pc, #16]    ;                             \
  orr  r0, r0, r2       ;                             \
  str  r0, [r1]         ;                             \
  b    1f               ;                             \
  .long (_Address)      ;                             \
  .long (_AndData)      ;                             \
  .long (_OrData)       ;                             \
1:

// returns _Data in _Reg and _Address in R1
#define MmioWriteFromReg32(_Address, _Reg) \
  ldr  r1, [pc, #4]     ;                  \
  str  _Reg, [r1]       ;                  \
  b    1f               ;                  \
  .long (_Address)      ;                  \
1:


// returns _Data in R0 and _Address in R1
#define MmioRead32(_Address)   \
  ldr  r1, [pc, #4]     ;      \
  ldr  r0, [r1]         ;      \
  b    1f               ;      \
  .long (_Address)      ;      \
1:

// returns _Data in Reg and _Address in R1
#define MmioReadToReg32(_Address, _Reg) \
  ldr  r1, [pc, #4]     ;               \
  ldr  _Reg, [r1]       ;               \
  b    1f               ;               \
  .long (_Address)      ;               \
1:


// load R0 with _Data
#define LoadConstant(_Data)  \
  ldr  r0, [pc, #0]     ;    \
  b    1f               ;    \
  .long (_Data)         ;    \
1:

// load _Reg with _Data
#define LoadConstantToReg(_Data, _Reg)  \
  ldr  _Reg, [pc, #0]   ;               \
  b    1f               ;               \
  .long (_Data)         ;               \
1:

// load _Reg with _Data if eq
#define LoadConstantToRegIfEq(_Data, _Reg)  \
  ldreq  _Reg, [pc, #0]   ;                 \
  b    1f                 ;                 \
  .long (_Data)           ;                 \
1:

// Convert the (ClusterId,CoreId) into a Core Position
// We assume there are 4 cores per cluster
#define GetCorePositionFromMpId(Pos, MpId, Tmp) \
  lsr   Pos, MpId, #6 ;                        \
  and   Tmp, MpId, #3 ;                        \
  add   Pos, Pos, Tmp

// Reserve a region at the top of the Primary Core stack
// for Global variables for the XIP phase
#define SetPrimaryStack(StackTop, GlobalSize, Tmp)  \
  and     Tmp, GlobalSize, #7         ;             \
  rsbne   Tmp, Tmp, #8                ;             \
  add     GlobalSize, GlobalSize, Tmp ;             \
  sub     sp, StackTop, GlobalSize    ;             \
                                      ;             \
  mov     Tmp, sp                     ;             \
  mov     GlobalSize, #0x0            ;             \
_SetPrimaryStackInitGlobals:          ;             \
  cmp     Tmp, StackTop               ;             \
  beq     _SetPrimaryStackEnd         ;             \
  str     GlobalSize, [Tmp], #4       ;             \
  b       _SetPrimaryStackInitGlobals ;             \
_SetPrimaryStackEnd:


#elif defined (__GNUC__)

#define MmioWrite32(Address, Data) \
  ldr  r1, =Address ;              \
  ldr  r0, =Data    ;              \
  str  r0, [r1]
    
#define MmioOr32(Address, OrData) \
  ldr  r1, =Address ;             \
  ldr  r2, =OrData  ;             \
  ldr  r0, [r1]     ;             \
  orr  r0, r0, r2   ;             \
  str  r0, [r1]

#define MmioAnd32(Address, AndData) \
  ldr  r1, =Address ;               \
  ldr  r2, =AndData ;               \
  ldr  r0, [r1]     ;               \
  and  r0, r0, r2   ;               \
  str  r0, [r1]

#define MmioAndThenOr32(Address, AndData, OrData) \
  ldr  r1, =Address ;                             \
  ldr  r0, [r1]     ;                             \
  ldr  r2, =AndData ;                             \
  and  r0, r0, r2   ;                             \
  ldr  r2, =OrData  ;                             \
  orr  r0, r0, r2   ;                             \
  str  r0, [r1]         

#define MmioWriteFromReg32(Address, Reg) \
  ldr  r1, =Address ;                    \
  str  Reg, [r1]

#define MmioRead32(Address) \
  ldr  r1, =Address ;       \
  ldr  r0, [r1]

#define MmioReadToReg32(Address, Reg) \
  ldr  r1, =Address ;                 \
  ldr  Reg, [r1]

#define LoadConstant(Data) \
  ldr  r0, =Data

#define LoadConstantToReg(Data, Reg) \
  ldr  Reg, =Data
  
#define GetCorePositionFromMpId(Pos, MpId, Tmp) \
  lsr   Pos, MpId, #6 ;                        \
  and   Tmp, MpId, #3 ;                        \
  add   Pos, Pos, Tmp

#define SetPrimaryStack(StackTop, GlobalSize, Tmp)  \
  and     Tmp, GlobalSize, #7         ;             \
  rsbne   Tmp, Tmp, #8                ;             \
  add     GlobalSize, GlobalSize, Tmp ;             \
  sub     sp, StackTop, GlobalSize    ;             \
                                      ;             \
  mov     Tmp, sp                     ;             \
  mov     GlobalSize, #0x0            ;             \
_SetPrimaryStackInitGlobals:          ;             \
  cmp     Tmp, StackTop               ;             \
  beq     _SetPrimaryStackEnd         ;             \
  str     GlobalSize, [Tmp], #4       ;             \
  b       _SetPrimaryStackInitGlobals ;             \
_SetPrimaryStackEnd:

#else

//
// Use ARM assembly macros, form armasam 
//
//  Less magic in the macros if ldr reg, =expr works
//

// returns _Data in R0 and _Address in R1



#define MmioWrite32(Address, Data) MmioWrite32Macro Address, Data




// returns Data in R0 and Address in R1, and OrData in r2
#define MmioOr32(Address, OrData) MmioOr32Macro Address, OrData
  

// returns _Data in R0 and _Address in R1, and _OrData in r2


#define MmioAnd32(Address, AndData)  MmioAnd32Macro Address, AndData

// returns result in R0, _Address in R1, and _OrData in r2


#define MmioAndThenOr32(Address, AndData, OrData) MmioAndThenOr32Macro Address, AndData, OrData


// returns _Data in _Reg and _Address in R1


#define MmioWriteFromReg32(Address, Reg) MmioWriteFromReg32Macro Address, Reg

// returns _Data in R0 and _Address in R1


#define MmioRead32(Address)  MmioRead32Macro Address

// returns _Data in Reg and _Address in R1


#define MmioReadToReg32(Address, Reg) MmioReadToReg32Macro Address, Reg


// load R0 with _Data


#define LoadConstant(Data)  LoadConstantMacro Data

// load _Reg with _Data


#define LoadConstantToReg(Data, Reg)  LoadConstantToRegMacro Data, Reg

// conditional load testing eq flag
#define LoadConstantToRegIfEq(Data, Reg)  LoadConstantToRegIfEqMacro Data, Reg

#define GetCorePositionFromMpId(Pos, MpId, Tmp)  GetCorePositionFromMpId Pos, MpId, Tmp

#define SetPrimaryStack(StackTop,GlobalSize,Tmp) SetPrimaryStack StackTop, GlobalSize, Tmp

#endif

#endif