f70978f6414fc80c0325feb7afa42339ae4699f6
[mirror_edk2.git] / ArmVirtPkg / HighMemDxe / HighMemDxe.c
1 /** @file
2 * High memory node enumeration DXE driver for ARM Virtual Machines
3 *
4 * Copyright (c) 2015-2016, Linaro Ltd. All rights reserved.
5 *
6 * This program and the accompanying materials are licensed and made available
7 * under the terms and conditions of the BSD License which accompanies this
8 * 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
13 * IMPLIED.
14 *
15 **/
16
17 #include <Library/BaseLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/DxeServicesTableLib.h>
20 #include <Library/PcdLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22
23 #include <Protocol/FdtClient.h>
24
25 EFI_STATUS
26 EFIAPI
27 InitializeHighMemDxe (
28 IN EFI_HANDLE ImageHandle,
29 IN EFI_SYSTEM_TABLE *SystemTable
30 )
31 {
32 FDT_CLIENT_PROTOCOL *FdtClient;
33 EFI_STATUS Status, FindNodeStatus;
34 INT32 Node;
35 CONST UINT32 *Reg;
36 UINT32 RegSize;
37 UINTN AddressCells, SizeCells;
38 UINT64 CurBase;
39 UINT64 CurSize;
40 UINT64 Attributes;
41
42 Status = gBS->LocateProtocol (&gFdtClientProtocolGuid, NULL,
43 (VOID **)&FdtClient);
44 ASSERT_EFI_ERROR (Status);
45
46 //
47 // Check for memory node and add the memory spaces except the lowest one
48 //
49 for (FindNodeStatus = FdtClient->FindMemoryNodeReg (FdtClient, &Node,
50 (CONST VOID **) &Reg, &AddressCells,
51 &SizeCells, &RegSize);
52 !EFI_ERROR (FindNodeStatus);
53 FindNodeStatus = FdtClient->FindNextMemoryNodeReg (FdtClient, Node,
54 &Node, (CONST VOID **) &Reg, &AddressCells,
55 &SizeCells, &RegSize)) {
56 ASSERT (AddressCells <= 2);
57 ASSERT (SizeCells <= 2);
58
59 while (RegSize > 0) {
60 CurBase = SwapBytes32 (*Reg++);
61 if (AddressCells > 1) {
62 CurBase = (CurBase << 32) | SwapBytes32 (*Reg++);
63 }
64 CurSize = SwapBytes32 (*Reg++);
65 if (SizeCells > 1) {
66 CurSize = (CurSize << 32) | SwapBytes32 (*Reg++);
67 }
68 RegSize -= (AddressCells + SizeCells) * sizeof (UINT32);
69
70 if (PcdGet64 (PcdSystemMemoryBase) != CurBase) {
71 Status = gDS->AddMemorySpace (EfiGcdMemoryTypeSystemMemory, CurBase,
72 CurSize, EFI_MEMORY_WB);
73
74 if (EFI_ERROR (Status)) {
75 DEBUG ((EFI_D_ERROR,
76 "%a: Failed to add System RAM @ 0x%lx - 0x%lx (%r)\n",
77 __FUNCTION__, CurBase, CurBase + CurSize - 1, Status));
78 continue;
79 }
80
81 //
82 // Take care not to strip any permission attributes that will have been
83 // set by DxeCore on the region we just added if a strict permission
84 // policy is in effect for EfiConventionalMemory regions.
85 // Unfortunately, we cannot interrogate the GCD memory space map for
86 // those permissions, since they are not recorded there (for historical
87 // reasons), so check the policy directly.
88 //
89 Attributes = EFI_MEMORY_WB;
90 if ((PcdGet64 (PcdDxeNxMemoryProtectionPolicy) &
91 (1U << (UINT32)EfiConventionalMemory)) != 0) {
92 Attributes |= EFI_MEMORY_XP;
93 }
94
95 Status = gDS->SetMemorySpaceAttributes (CurBase, CurSize, Attributes);
96
97 if (EFI_ERROR (Status)) {
98 DEBUG ((EFI_D_ERROR,
99 "%a: Failed to set System RAM @ 0x%lx - 0x%lx attribute (%r)\n",
100 __FUNCTION__, CurBase, CurBase + CurSize - 1, Status));
101 } else {
102 DEBUG ((EFI_D_INFO, "%a: Add System RAM @ 0x%lx - 0x%lx\n",
103 __FUNCTION__, CurBase, CurBase + CurSize - 1));
104 }
105 }
106 }
107 }
108
109 return EFI_SUCCESS;
110 }