1 // 2 // Copyright (c) 2011-2015, ARM Limited. All rights reserved. 3 // 4 // This program and the accompanying materials 5 // are licensed and made available under the terms and conditions of the BSD License 6 // which accompanies this distribution. The full text of the license may be found at 7 // http://opensource.org/licenses/bsd-license.php 8 // 9 // THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 11 // 12 // 13 14 #include <AsmMacroIoLibV8.h> 15 #include <Base.h> 16 #include <Library/PcdLib.h> 17 #include <AutoGen.h> 18 19 .text 20 .align 3 21 22 GCC_ASM_IMPORT(ArmPlatformIsPrimaryCore) 23 GCC_ASM_IMPORT(ArmReadMpidr) 24 GCC_ASM_IMPORT(ArmPlatformPeiBootAction) 25 GCC_ASM_IMPORT(ArmPlatformStackSet) 26 GCC_ASM_EXPORT(_ModuleEntryPoint) 27 ASM_GLOBAL ASM_PFX(mSystemMemoryEnd) 28 29 StartupAddr: .8byte ASM_PFX(CEntryPoint) 30 ASM_PFX(mSystemMemoryEnd): .8byte 0 31 32 ASM_PFX(_ModuleEntryPoint): 33 // Do early platform specific actions 34 bl ASM_PFX(ArmPlatformPeiBootAction) 35 36 // Get ID of this CPU in Multicore system 37 bl ASM_PFX(ArmReadMpidr) 38 // Keep a copy of the MpId register value 39 mov x10, x0 40 41 _SetSVCMode: 42 // Check if we can install the stack at the top of the System Memory or if we need 43 // to install the stacks at the bottom of the Firmware Device (case the FD is located 44 // at the top of the DRAM) 45 _SystemMemoryEndInit: 46 ldr x1, mSystemMemoryEnd 47 48 // Is mSystemMemoryEnd initialized? 49 cmp x1, #0 50 bne _SetupStackPosition 51 52 LoadConstantToReg (FixedPcdGet32(PcdSystemMemoryBase), x1) 53 LoadConstantToReg (FixedPcdGet32(PcdSystemMemorySize), x2) 54 sub x2, x2, #1 55 add x1, x1, x2 56 // Update the global variable 57 adr x2, mSystemMemoryEnd 58 str x1, [x2] 59 60 _SetupStackPosition: 61 // r1 = SystemMemoryTop 62 63 // Calculate Top of the Firmware Device 64 LoadConstantToReg (FixedPcdGet32(PcdFdBaseAddress), x2) 65 LoadConstantToReg (FixedPcdGet32(PcdFdSize), x3) 66 sub x3, x3, #1 67 add x3, x3, x2 // x3 = FdTop = PcdFdBaseAddress + PcdFdSize 68 69 // UEFI Memory Size (stacks are allocated in this region) 70 LoadConstantToReg (FixedPcdGet32(PcdSystemMemoryUefiRegionSize), x4) 71 72 // 73 // Reserve the memory for the UEFI region (contain stacks on its top) 74 // 75 76 // Calculate how much space there is between the top of the Firmware and the Top of the System Memory 77 subs x0, x1, x3 // x0 = SystemMemoryTop - FdTop 78 b.mi _SetupStack // Jump if negative (FdTop > SystemMemoryTop). Case when the PrePi is in XIP memory outside of the DRAM 79 cmp x0, x4 80 b.ge _SetupStack 81 82 // Case the top of stacks is the FdBaseAddress 83 mov x1, x2 84 85 _SetupStack: 86 // x1 contains the top of the stack (and the UEFI Memory) 87 88 // Because the 'push' instruction is equivalent to 'stmdb' (decrement before), we need to increment 89 // one to the top of the stack. We check if incrementing one does not overflow (case of DRAM at the 90 // top of the memory space) 91 adds x11, x1, #1 92 b.cs _SetupOverflowStack 93 94 _SetupAlignedStack: 95 mov x1, x11 96 b _GetBaseUefiMemory 97 98 _SetupOverflowStack: 99 // Case memory at the top of the address space. Ensure the top of the stack is EFI_PAGE_SIZE 100 // aligned (4KB) 101 LoadConstantToReg (EFI_PAGE_MASK, x11) 102 and x11, x11, x1 103 sub x1, x1, x11 104 105 _GetBaseUefiMemory: 106 // Calculate the Base of the UEFI Memory 107 sub x11, x1, x4 108 109 _GetStackBase: 110 // r1 = The top of the Mpcore Stacks 111 // Stack for the primary core = PrimaryCoreStack 112 LoadConstantToReg (FixedPcdGet32(PcdCPUCorePrimaryStackSize), x2) 113 sub x12, x1, x2 114 115 // Stack for the secondary core = Number of Cores - 1 116 LoadConstantToReg (FixedPcdGet32(PcdCoreCount), x0) 117 sub x0, x0, #1 118 LoadConstantToReg (FixedPcdGet32(PcdCPUCoreSecondaryStackSize), x1) 119 mul x1, x1, x0 120 sub x12, x12, x1 121 122 // x12 = The base of the MpCore Stacks (primary stack & secondary stacks) 123 mov x0, x12 124 mov x1, x10 125 //ArmPlatformStackSet(StackBase, MpId, PrimaryStackSize, SecondaryStackSize) 126 LoadConstantToReg (FixedPcdGet32(PcdCPUCorePrimaryStackSize), x2) 127 LoadConstantToReg (FixedPcdGet32(PcdCPUCoreSecondaryStackSize), x3) 128 bl ASM_PFX(ArmPlatformStackSet) 129 130 // Is it the Primary Core ? 131 mov x0, x10 132 bl ASM_PFX(ArmPlatformIsPrimaryCore) 133 cmp x0, #1 134 bne _PrepareArguments 135 136 _PrepareArguments: 137 mov x0, x10 138 mov x1, x11 139 mov x2, x12 140 141 // Move sec startup address into a data register 142 // Ensure we're jumping to FV version of the code (not boot remapped alias) 143 ldr x4, StartupAddr 144 145 // Jump to PrePiCore C code 146 // x0 = MpId 147 // x1 = UefiMemoryBase 148 // x2 = StacksBase 149 blr x4 150 151 _NeverReturn: 152 b _NeverReturn 153