]> git.proxmox.com Git - mirror_edk2.git/blame - ArmVirtPkg/PrePi/AArch64/ModuleEntryPoint.S
ArmVirtPkg/PrePi: move DRAM discovery code into PrePi
[mirror_edk2.git] / ArmVirtPkg / PrePi / AArch64 / ModuleEntryPoint.S
CommitLineData
f9a9d2dc
AB
1//\r
2// Copyright (c) 2011-2013, ARM Limited. All rights reserved.\r
dfc28388 3// Copyright (c) 2015-2016, Linaro Limited. All rights reserved.\r
f9a9d2dc
AB
4//\r
5// This program and the accompanying materials\r
6// are licensed and made available under the terms and conditions of the BSD License\r
7// which accompanies this distribution. The full text of the license may be found at\r
8// http://opensource.org/licenses/bsd-license.php\r
9//\r
10// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12//\r
13//\r
14\r
15#include <AsmMacroIoLibV8.h>\r
f9a9d2dc 16\r
dfc28388 17ASM_FUNC(_ModuleEntryPoint)\r
f9a9d2dc
AB
18 //\r
19 // We are built as a ET_DYN PIE executable, so we need to process all\r
20 // relative relocations regardless of whether or not we are executing from\r
21 // the same offset we were linked at. This is only possible if we are\r
22 // running from RAM.\r
23 //\r
24 adr x8, __reloc_base\r
25 adr x9, __reloc_start\r
26 adr x10, __reloc_end\r
27\r
28.Lreloc_loop:\r
29 cmp x9, x10\r
30 bhs .Lreloc_done\r
31\r
32 //\r
33 // AArch64 uses the ELF64 RELA format, which means each entry in the\r
34 // relocation table consists of\r
35 //\r
36 // UINT64 offset : the relative offset of the value that needs to\r
37 // be relocated\r
38 // UINT64 info : relocation type and symbol index (the latter is\r
39 // not used for R_AARCH64_RELATIVE relocations)\r
40 // UINT64 addend : value to be added to the value being relocated\r
41 //\r
42 ldp x11, x12, [x9], #24 // read offset into x11 and info into x12\r
43 cmp x12, #0x403 // check info == R_AARCH64_RELATIVE?\r
44 bne .Lreloc_loop // not a relative relocation? then skip\r
45\r
46 ldr x12, [x9, #-8] // read addend into x12\r
47 add x12, x12, x8 // add reloc base to addend to get relocated value\r
48 str x12, [x11, x8] // write relocated value at offset\r
49 b .Lreloc_loop\r
50.Lreloc_done:\r
51\r
83270956 52 bl ASM_PFX(DiscoverDramFromDt)\r
f9a9d2dc
AB
53\r
54 // Get ID of this CPU in Multicore system\r
55 bl ASM_PFX(ArmReadMpidr)\r
56 // Keep a copy of the MpId register value\r
298f8361 57 mov x20, x0\r
f9a9d2dc
AB
58\r
59// Check if we can install the stack at the top of the System Memory or if we need\r
60// to install the stacks at the bottom of the Firmware Device (case the FD is located\r
61// at the top of the DRAM)\r
62_SetupStackPosition:\r
63 // Compute Top of System Memory\r
64 ldr x1, PcdGet64 (PcdSystemMemoryBase)\r
65 ldr x2, PcdGet64 (PcdSystemMemorySize)\r
66 sub x2, x2, #1\r
67 add x1, x1, x2 // x1 = SystemMemoryTop = PcdSystemMemoryBase + PcdSystemMemorySize\r
68\r
69 // Calculate Top of the Firmware Device\r
70 ldr x2, PcdGet64 (PcdFdBaseAddress)\r
dfc28388 71 MOV32 (w3, FixedPcdGet32 (PcdFdSize) - 1)\r
f9a9d2dc
AB
72 add x3, x3, x2 // x3 = FdTop = PcdFdBaseAddress + PcdFdSize\r
73\r
74 // UEFI Memory Size (stacks are allocated in this region)\r
dfc28388 75 MOV32 (x4, FixedPcdGet32(PcdSystemMemoryUefiRegionSize))\r
f9a9d2dc
AB
76\r
77 //\r
78 // Reserve the memory for the UEFI region (contain stacks on its top)\r
79 //\r
80\r
81 // Calculate how much space there is between the top of the Firmware and the Top of the System Memory\r
82 subs x0, x1, x3 // x0 = SystemMemoryTop - FdTop\r
83 b.mi _SetupStack // Jump if negative (FdTop > SystemMemoryTop). Case when the PrePi is in XIP memory outside of the DRAM\r
84 cmp x0, x4\r
85 b.ge _SetupStack\r
86\r
87 // Case the top of stacks is the FdBaseAddress\r
88 mov x1, x2\r
89\r
90_SetupStack:\r
91 // x1 contains the top of the stack (and the UEFI Memory)\r
92\r
93 // Because the 'push' instruction is equivalent to 'stmdb' (decrement before), we need to increment\r
94 // one to the top of the stack. We check if incrementing one does not overflow (case of DRAM at the\r
95 // top of the memory space)\r
298f8361 96 adds x21, x1, #1\r
f9a9d2dc
AB
97 b.cs _SetupOverflowStack\r
98\r
99_SetupAlignedStack:\r
298f8361 100 mov x1, x21\r
f9a9d2dc
AB
101 b _GetBaseUefiMemory\r
102\r
103_SetupOverflowStack:\r
104 // Case memory at the top of the address space. Ensure the top of the stack is EFI_PAGE_SIZE\r
105 // aligned (4KB)\r
dfc28388 106 and x1, x1, ~EFI_PAGE_MASK\r
f9a9d2dc
AB
107\r
108_GetBaseUefiMemory:\r
109 // Calculate the Base of the UEFI Memory\r
298f8361 110 sub x21, x1, x4\r
f9a9d2dc
AB
111\r
112_GetStackBase:\r
113 // r1 = The top of the Mpcore Stacks\r
114 // Stack for the primary core = PrimaryCoreStack\r
dfc28388 115 MOV32 (x2, FixedPcdGet32(PcdCPUCorePrimaryStackSize))\r
298f8361 116 sub x22, x1, x2\r
f9a9d2dc
AB
117\r
118 // Stack for the secondary core = Number of Cores - 1\r
dfc28388 119 MOV32 (x1, (FixedPcdGet32(PcdCoreCount) - 1) * FixedPcdGet32(PcdCPUCoreSecondaryStackSize))\r
298f8361 120 sub x22, x22, x1\r
f9a9d2dc 121\r
298f8361
AB
122 // x22 = The base of the MpCore Stacks (primary stack & secondary stacks)\r
123 mov x0, x22\r
124 mov x1, x20\r
f9a9d2dc 125 //ArmPlatformStackSet(StackBase, MpId, PrimaryStackSize, SecondaryStackSize)\r
dfc28388
AB
126 MOV32 (x2, FixedPcdGet32(PcdCPUCorePrimaryStackSize))\r
127 MOV32 (x3, FixedPcdGet32(PcdCPUCoreSecondaryStackSize))\r
f9a9d2dc
AB
128 bl ASM_PFX(ArmPlatformStackSet)\r
129\r
298f8361
AB
130 mov x0, x20\r
131 mov x1, x21\r
132 mov x2, x22\r
f9a9d2dc 133\r
f9a9d2dc
AB
134 // Jump to PrePiCore C code\r
135 // x0 = MpId\r
136 // x1 = UefiMemoryBase\r
137 // x2 = StacksBase\r
16a9fe2c 138 bl ASM_PFX(CEntryPoint)\r
f9a9d2dc
AB
139\r
140_NeverReturn:\r
141 b _NeverReturn\r
83270956
AB
142\r
143// VOID\r
144// DiscoverDramFromDt (\r
145// VOID *DeviceTreeBaseAddress, // passed by loader in x0\r
146// VOID *ImageBase // passed by FDF trampoline in x1\r
147// );\r
148ASM_PFX(DiscoverDramFromDt):\r
149 //\r
150 // If we are booting from RAM using the Linux kernel boot protocol, x0 will\r
151 // point to the DTB image in memory. Otherwise, use the default value defined\r
152 // by the platform.\r
153 //\r
154 cbnz x0, 0f\r
155 ldr x0, PcdGet64 (PcdDeviceTreeInitialBaseAddress)\r
156\r
1570:mov x29, x30 // preserve LR\r
158 mov x28, x0 // preserve DTB pointer\r
159 mov x27, x1 // preserve base of image pointer\r
160\r
161 //\r
162 // The base of the runtime image has been preserved in x1. Check whether\r
163 // the expected magic number can be found in the header.\r
164 //\r
165 ldr w8, .LArm64LinuxMagic\r
166 ldr w9, [x1, #0x38]\r
167 cmp w8, w9\r
168 bne .Lout\r
169\r
170 //\r
171 //\r
172 // OK, so far so good. We have confirmed that we likely have a DTB and are\r
173 // booting via the arm64 Linux boot protocol. Update the base-of-image PCD\r
174 // to the actual relocated value, and add the shift of PcdFdBaseAddress to\r
175 // PcdFvBaseAddress as well\r
176 //\r
177 adr x8, PcdGet64 (PcdFdBaseAddress)\r
178 adr x9, PcdGet64 (PcdFvBaseAddress)\r
179 ldr x6, [x8]\r
180 ldr x7, [x9]\r
181 sub x7, x7, x6\r
182 add x7, x7, x1\r
183 str x1, [x8]\r
184 str x7, [x9]\r
185\r
186 //\r
187 // Discover the memory size and offset from the DTB, and record in the\r
188 // respective PCDs. This will also return false if a corrupt DTB is\r
189 // encountered. Since we are calling a C function, use the window at the\r
190 // beginning of the FD image as a temp stack.\r
191 //\r
192 adr x1, PcdGet64 (PcdSystemMemoryBase)\r
193 adr x2, PcdGet64 (PcdSystemMemorySize)\r
194 mov sp, x7\r
195 bl FindMemnode\r
196 cbz x0, .Lout\r
197\r
198 //\r
199 // Copy the DTB to the slack space right after the 64 byte arm64/Linux style\r
200 // image header at the base of this image (defined in the FDF), and record the\r
201 // pointer in PcdDeviceTreeInitialBaseAddress.\r
202 //\r
203 adr x8, PcdGet64 (PcdDeviceTreeInitialBaseAddress)\r
204 add x27, x27, #0x40\r
205 str x27, [x8]\r
206\r
207 mov x0, x27\r
208 mov x1, x28\r
209 bl CopyFdt\r
210\r
211.Lout:\r
212 ret x29\r
213\r
214.LArm64LinuxMagic:\r
215 .byte 0x41, 0x52, 0x4d, 0x64\r