]> git.proxmox.com Git - mirror_edk2.git/blame - ArmVirtPkg/Library/ArmVirtMemoryInitPeiLib/ArmVirtMemoryInitPeiLib.c
ArmPkg ArmVirtPkg MdeModulePkg: switch to separate ArmMmuLib
[mirror_edk2.git] / ArmVirtPkg / Library / ArmVirtMemoryInitPeiLib / ArmVirtMemoryInitPeiLib.c
CommitLineData
a1bcf5c3
AB
1/** @file\r
2*\r
3* Copyright (c) 2011-2014, ARM Limited. All rights reserved.\r
4* Copyright (c) 2014, Linaro Limited. All rights reserved.\r
5*\r
6* This program and the accompanying materials\r
7* are licensed and made available under the terms and conditions of the BSD License\r
8* which accompanies this distribution. The full text of the license may be found at\r
9* http://opensource.org/licenses/bsd-license.php\r
10*\r
11* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13*\r
14**/\r
15\r
16#include <PiPei.h>\r
17\r
5db1cce1 18#include <Library/ArmMmuLib.h>\r
a1bcf5c3
AB
19#include <Library/ArmPlatformLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/HobLib.h>\r
22#include <Library/MemoryAllocationLib.h>\r
23#include <Library/PcdLib.h>\r
e1d52990 24#include <Library/CacheMaintenanceLib.h>\r
a1bcf5c3
AB
25\r
26VOID\r
27BuildMemoryTypeInformationHob (\r
28 VOID\r
29 );\r
30\r
31VOID\r
32InitMmu (\r
33 VOID\r
34 )\r
35{\r
36 ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable;\r
37 VOID *TranslationTableBase;\r
38 UINTN TranslationTableSize;\r
39 RETURN_STATUS Status;\r
40\r
41 // Get Virtual Memory Map from the Platform Library\r
42 ArmPlatformGetVirtualMemoryMap (&MemoryTable);\r
43\r
44 //Note: Because we called PeiServicesInstallPeiMemory() before to call InitMmu() the MMU Page Table resides in\r
45 // DRAM (even at the top of DRAM as it is the first permanent memory allocation)\r
46 Status = ArmConfigureMmu (MemoryTable, &TranslationTableBase, &TranslationTableSize);\r
47 if (EFI_ERROR (Status)) {\r
48 DEBUG ((EFI_D_ERROR, "Error: Failed to enable MMU\n"));\r
49 }\r
50}\r
51\r
52EFI_STATUS\r
53EFIAPI\r
54MemoryPeim (\r
55 IN EFI_PHYSICAL_ADDRESS UefiMemoryBase,\r
56 IN UINT64 UefiMemorySize\r
57 )\r
58{\r
59 EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttributes;\r
72daeed8 60 UINT64 SystemMemoryTop;\r
a1bcf5c3
AB
61\r
62 // Ensure PcdSystemMemorySize has been set\r
63 ASSERT (PcdGet64 (PcdSystemMemorySize) != 0);\r
64\r
65 //\r
66 // Now, the permanent memory has been installed, we can call AllocatePages()\r
67 //\r
68 ResourceAttributes = (\r
69 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
70 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
71 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
72 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
73 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
74 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |\r
75 EFI_RESOURCE_ATTRIBUTE_TESTED\r
76 );\r
77\r
72daeed8
AB
78 SystemMemoryTop = PcdGet64 (PcdSystemMemoryBase) +\r
79 PcdGet64 (PcdSystemMemorySize);\r
80\r
81 if (SystemMemoryTop - 1 > MAX_ADDRESS) {\r
82 BuildResourceDescriptorHob (\r
83 EFI_RESOURCE_SYSTEM_MEMORY,\r
84 ResourceAttributes,\r
85 PcdGet64 (PcdSystemMemoryBase),\r
86 (UINT64)MAX_ADDRESS - PcdGet64 (PcdSystemMemoryBase) + 1\r
87 );\r
88 BuildResourceDescriptorHob (\r
89 EFI_RESOURCE_SYSTEM_MEMORY,\r
90 ResourceAttributes,\r
91 (UINT64)MAX_ADDRESS + 1,\r
92 SystemMemoryTop - MAX_ADDRESS - 1\r
93 );\r
94 } else {\r
95 BuildResourceDescriptorHob (\r
96 EFI_RESOURCE_SYSTEM_MEMORY,\r
97 ResourceAttributes,\r
98 PcdGet64 (PcdSystemMemoryBase),\r
99 PcdGet64 (PcdSystemMemorySize)\r
100 );\r
101 }\r
a1bcf5c3 102\r
e1d52990
AB
103 //\r
104 // When running under virtualization, the PI/UEFI memory region may be\r
105 // clean but not invalidated in system caches or in lower level caches\r
106 // on other CPUs. So invalidate the region by virtual address, to ensure\r
107 // that the contents we put there with the caches and MMU off will still\r
108 // be visible after turning them on.\r
109 //\r
110 InvalidateDataCacheRange ((VOID*)(UINTN)UefiMemoryBase, UefiMemorySize);\r
111\r
a1bcf5c3
AB
112 // Build Memory Allocation Hob\r
113 InitMmu ();\r
114\r
115 if (FeaturePcdGet (PcdPrePiProduceMemoryTypeInformationHob)) {\r
116 // Optional feature that helps prevent EFI memory map fragmentation.\r
117 BuildMemoryTypeInformationHob ();\r
118 }\r
119\r
120 return EFI_SUCCESS;\r
121}\r