1//
2//  Copyright (c) 2011-2013, ARM Limited. All rights reserved.
3//  Copyright (c) 2015-2016, Linaro Limited. All rights reserved.
4//
5//  SPDX-License-Identifier: BSD-2-Clause-Patent
6//
7//
8
9#include <AsmMacroIoLibV8.h>
10
11ASM_FUNC(_ModuleEntryPoint)
12  bl    ASM_PFX(DiscoverDramFromDt)
13
14  // Get ID of this CPU in Multicore system
15  bl    ASM_PFX(ArmReadMpidr)
16  // Keep a copy of the MpId register value
17  mov   x20, x0
18
19// Check if we can install the stack at the top of the System Memory or if we need
20// to install the stacks at the bottom of the Firmware Device (case the FD is located
21// at the top of the DRAM)
22_SetupStackPosition:
23  // Compute Top of System Memory
24  ldr   x1, PcdGet64 (PcdSystemMemoryBase)
25  ldr   x2, PcdGet64 (PcdSystemMemorySize)
26  sub   x2, x2, #1
27  add   x1, x1, x2      // x1 = SystemMemoryTop = PcdSystemMemoryBase + PcdSystemMemorySize
28
29  // Calculate Top of the Firmware Device
30  ldr   x2, PcdGet64 (PcdFdBaseAddress)
31  MOV32 (w3, FixedPcdGet32 (PcdFdSize) - 1)
32  add   x3, x3, x2      // x3 = FdTop = PcdFdBaseAddress + PcdFdSize
33
34  // UEFI Memory Size (stacks are allocated in this region)
35  MOV32 (x4, FixedPcdGet32(PcdSystemMemoryUefiRegionSize))
36
37  //
38  // Reserve the memory for the UEFI region (contain stacks on its top)
39  //
40
41  // Calculate how much space there is between the top of the Firmware and the Top of the System Memory
42  subs  x0, x1, x3   // x0 = SystemMemoryTop - FdTop
43  b.mi  _SetupStack  // Jump if negative (FdTop > SystemMemoryTop). Case when the PrePi is in XIP memory outside of the DRAM
44  cmp   x0, x4
45  b.ge  _SetupStack
46
47  // Case the top of stacks is the FdBaseAddress
48  mov   x1, x2
49
50_SetupStack:
51  // x1 contains the top of the stack (and the UEFI Memory)
52
53  // Because the 'push' instruction is equivalent to 'stmdb' (decrement before), we need to increment
54  // one to the top of the stack. We check if incrementing one does not overflow (case of DRAM at the
55  // top of the memory space)
56  adds  x21, x1, #1
57  b.cs  _SetupOverflowStack
58
59_SetupAlignedStack:
60  mov   x1, x21
61  b     _GetBaseUefiMemory
62
63_SetupOverflowStack:
64  // Case memory at the top of the address space. Ensure the top of the stack is EFI_PAGE_SIZE
65  // aligned (4KB)
66  and   x1, x1, ~EFI_PAGE_MASK
67
68_GetBaseUefiMemory:
69  // Calculate the Base of the UEFI Memory
70  sub   x21, x1, x4
71
72_GetStackBase:
73  // r1 = The top of the Mpcore Stacks
74  mov   sp, x1
75
76  // Stack for the primary core = PrimaryCoreStack
77  MOV32 (x2, FixedPcdGet32(PcdCPUCorePrimaryStackSize))
78  sub   x22, x1, x2
79
80  mov   x0, x20
81  mov   x1, x21
82  mov   x2, x22
83
84  // Set the frame pointer to NULL so any backtraces terminate here
85  mov   x29, xzr
86
87  // Jump to PrePiCore C code
88  //    x0 = MpId
89  //    x1 = UefiMemoryBase
90  //    x2 = StacksBase
91  bl    ASM_PFX(CEntryPoint)
92
93_NeverReturn:
94  b _NeverReturn
95
96// VOID
97// DiscoverDramFromDt (
98//   VOID   *DeviceTreeBaseAddress,   // passed by loader in x0
99//   VOID   *ImageBase                // passed by FDF trampoline in x1
100//   );
101ASM_PFX(DiscoverDramFromDt):
102  //
103  // If we are booting from RAM using the Linux kernel boot protocol, x0 will
104  // point to the DTB image in memory. Otherwise, use the default value defined
105  // by the platform.
106  //
107  cbnz  x0, 0f
108  ldr   x0, PcdGet64 (PcdDeviceTreeInitialBaseAddress)
109
1100:mov   x29, x30            // preserve LR
111  mov   x28, x0             // preserve DTB pointer
112  mov   x27, x1             // preserve base of image pointer
113
114  //
115  // The base of the runtime image has been preserved in x1. Check whether
116  // the expected magic number can be found in the header.
117  //
118  ldr   w8, .LArm64LinuxMagic
119  ldr   w9, [x1, #0x38]
120  cmp   w8, w9
121  bne   .Lout
122
123  //
124  //
125  // OK, so far so good. We have confirmed that we likely have a DTB and are
126  // booting via the arm64 Linux boot protocol. Update the base-of-image PCD
127  // to the actual relocated value, and add the shift of PcdFdBaseAddress to
128  // PcdFvBaseAddress as well
129  //
130  adr   x8, PcdGet64 (PcdFdBaseAddress)
131  adr   x9, PcdGet64 (PcdFvBaseAddress)
132  ldr   x6, [x8]
133  ldr   x7, [x9]
134  sub   x7, x7, x6
135  add   x7, x7, x1
136  str   x1, [x8]
137  str   x7, [x9]
138
139  //
140  // The runtime address may be different from the link time address so fix
141  // up the PE/COFF relocations. Since we are calling a C function, use the
142  // window at the beginning of the FD image as a temp stack.
143  //
144  mov   x0, x7
145  adr   x1, PeCoffLoaderImageReadFromMemory
146  mov   sp, x7
147  bl    RelocatePeCoffImage
148
149  //
150  // Discover the memory size and offset from the DTB, and record in the
151  // respective PCDs. This will also return false if a corrupt DTB is
152  // encountered.
153  //
154  mov   x0, x28
155  adr   x1, PcdGet64 (PcdSystemMemoryBase)
156  adr   x2, PcdGet64 (PcdSystemMemorySize)
157  bl    FindMemnode
158  cbz   x0, .Lout
159
160  //
161  // Copy the DTB to the slack space right after the 64 byte arm64/Linux style
162  // image header at the base of this image (defined in the FDF), and record the
163  // pointer in PcdDeviceTreeInitialBaseAddress.
164  //
165  adr   x8, PcdGet64 (PcdDeviceTreeInitialBaseAddress)
166  add   x27, x27, #0x40
167  str   x27, [x8]
168
169  mov   x0, x27
170  mov   x1, x28
171  bl    CopyFdt
172
173.Lout:
174  ret    x29
175
176.LArm64LinuxMagic:
177  .byte   0x41, 0x52, 0x4d, 0x64
178