]> git.proxmox.com Git - mirror_edk2.git/blob - ArmVirtPkg/Library/ArmVirtMemoryInitPeiLib/ArmVirtMemoryInitPeiLib.c
ArmVirtPkg: Apply uncrustify changes
[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 * SPDX-License-Identifier: BSD-2-Clause-Patent
7 *
8 **/
9
10 #include <PiPei.h>
11
12 #include <Library/ArmMmuLib.h>
13 #include <Library/ArmVirtMemInfoLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/HobLib.h>
16 #include <Library/MemoryAllocationLib.h>
17 #include <Library/PcdLib.h>
18 #include <Library/CacheMaintenanceLib.h>
19
20 VOID
21 BuildMemoryTypeInformationHob (
22 VOID
23 );
24
25 VOID
26 InitMmu (
27 VOID
28 )
29 {
30 ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable;
31 VOID *TranslationTableBase;
32 UINTN TranslationTableSize;
33 RETURN_STATUS Status;
34
35 // Get Virtual Memory Map from the Platform Library
36 ArmVirtGetMemoryMap (&MemoryTable);
37
38 // Note: Because we called PeiServicesInstallPeiMemory() before to call InitMmu() the MMU Page Table resides in
39 // DRAM (even at the top of DRAM as it is the first permanent memory allocation)
40 Status = ArmConfigureMmu (MemoryTable, &TranslationTableBase, &TranslationTableSize);
41 if (EFI_ERROR (Status)) {
42 DEBUG ((DEBUG_ERROR, "Error: Failed to enable MMU\n"));
43 }
44 }
45
46 EFI_STATUS
47 EFIAPI
48 MemoryPeim (
49 IN EFI_PHYSICAL_ADDRESS UefiMemoryBase,
50 IN UINT64 UefiMemorySize
51 )
52 {
53 EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttributes;
54 UINT64 SystemMemoryTop;
55
56 // Ensure PcdSystemMemorySize has been set
57 ASSERT (PcdGet64 (PcdSystemMemorySize) != 0);
58
59 //
60 // Now, the permanent memory has been installed, we can call AllocatePages()
61 //
62 ResourceAttributes = (
63 EFI_RESOURCE_ATTRIBUTE_PRESENT |
64 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
65 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
66 EFI_RESOURCE_ATTRIBUTE_TESTED
67 );
68
69 SystemMemoryTop = PcdGet64 (PcdSystemMemoryBase) +
70 PcdGet64 (PcdSystemMemorySize);
71
72 if (SystemMemoryTop - 1 > MAX_ALLOC_ADDRESS) {
73 BuildResourceDescriptorHob (
74 EFI_RESOURCE_SYSTEM_MEMORY,
75 ResourceAttributes,
76 PcdGet64 (PcdSystemMemoryBase),
77 (UINT64)MAX_ALLOC_ADDRESS - PcdGet64 (PcdSystemMemoryBase) + 1
78 );
79 BuildResourceDescriptorHob (
80 EFI_RESOURCE_SYSTEM_MEMORY,
81 ResourceAttributes,
82 (UINT64)MAX_ALLOC_ADDRESS + 1,
83 SystemMemoryTop - MAX_ALLOC_ADDRESS - 1
84 );
85 } else {
86 BuildResourceDescriptorHob (
87 EFI_RESOURCE_SYSTEM_MEMORY,
88 ResourceAttributes,
89 PcdGet64 (PcdSystemMemoryBase),
90 PcdGet64 (PcdSystemMemorySize)
91 );
92 }
93
94 //
95 // When running under virtualization, the PI/UEFI memory region may be
96 // clean but not invalidated in system caches or in lower level caches
97 // on other CPUs. So invalidate the region by virtual address, to ensure
98 // that the contents we put there with the caches and MMU off will still
99 // be visible after turning them on.
100 //
101 InvalidateDataCacheRange ((VOID *)(UINTN)UefiMemoryBase, UefiMemorySize);
102
103 // Build Memory Allocation Hob
104 InitMmu ();
105
106 if (FeaturePcdGet (PcdPrePiProduceMemoryTypeInformationHob)) {
107 // Optional feature that helps prevent EFI memory map fragmentation.
108 BuildMemoryTypeInformationHob ();
109 }
110
111 return EFI_SUCCESS;
112 }