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