1 /** @file
2 *
3 *  Copyright (c) 2014, Linaro Limited. All rights reserved.
4 *
5 *  This program and the accompanying materials
6 *  are licensed and made available under the terms and conditions of the BSD License
7 *  which accompanies this distribution.  The full text of the license may be found at
8 *  http://opensource.org/licenses/bsd-license.php
9 *
10 *  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 *  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 *
13 **/
14 
15 #include <Library/ArmPlatformLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/PcdLib.h>
19 #include <Library/IoLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <ArmPlatform.h>
22 
23 // Number of Virtual Memory Map Descriptors
24 #define MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS          5
25 
26 // DDR attributes
27 #define DDR_ATTRIBUTES_CACHED    ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
28 #define DDR_ATTRIBUTES_UNCACHED  ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
29 
30 EFI_PHYSICAL_ADDRESS
31 ArmGetPhysAddrTop (
32   VOID
33   );
34 
35 /**
36   Return the Virtual Memory Map of your platform
37 
38   This Virtual Memory Map is used by MemoryInitPei Module to initialize the MMU
39   on your platform.
40 
41   @param[out]   VirtualMemoryMap    Array of ARM_MEMORY_REGION_DESCRIPTOR
42                                     describing a Physical-to-Virtual Memory
43                                     mapping. This array must be ended by a
44                                     zero-filled entry
45 
46 **/
47 VOID
ArmPlatformGetVirtualMemoryMap(IN ARM_MEMORY_REGION_DESCRIPTOR ** VirtualMemoryMap)48 ArmPlatformGetVirtualMemoryMap (
49   IN ARM_MEMORY_REGION_DESCRIPTOR** VirtualMemoryMap
50   )
51 {
52   ARM_MEMORY_REGION_ATTRIBUTES  CacheAttributes;
53   ARM_MEMORY_REGION_DESCRIPTOR  *VirtualMemoryTable;
54 
55   ASSERT (VirtualMemoryMap != NULL);
56 
57   VirtualMemoryTable = AllocatePages (
58                          EFI_SIZE_TO_PAGES (
59                            sizeof (ARM_MEMORY_REGION_DESCRIPTOR)
60                            * MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS
61                            )
62                          );
63 
64   if (VirtualMemoryTable == NULL) {
65     DEBUG ((EFI_D_ERROR, "%a: Error: Failed AllocatePages()\n", __FUNCTION__));
66     return;
67   }
68 
69   if (FeaturePcdGet (PcdCacheEnable) == TRUE) {
70     CacheAttributes = DDR_ATTRIBUTES_CACHED;
71   } else {
72     CacheAttributes = DDR_ATTRIBUTES_UNCACHED;
73   }
74 
75   // System DRAM
76   VirtualMemoryTable[0].PhysicalBase = PcdGet64 (PcdSystemMemoryBase);
77   VirtualMemoryTable[0].VirtualBase  = VirtualMemoryTable[0].PhysicalBase;
78   VirtualMemoryTable[0].Length       = PcdGet64 (PcdSystemMemorySize);
79   VirtualMemoryTable[0].Attributes   = CacheAttributes;
80 
81   DEBUG ((EFI_D_INFO, "%a: Dumping System DRAM Memory Map:\n"
82       "\tPhysicalBase: 0x%lX\n"
83       "\tVirtualBase: 0x%lX\n"
84       "\tLength: 0x%lX\n",
85       __FUNCTION__,
86       VirtualMemoryTable[0].PhysicalBase,
87       VirtualMemoryTable[0].VirtualBase,
88       VirtualMemoryTable[0].Length));
89 
90   // Peripheral space before DRAM
91   VirtualMemoryTable[1].PhysicalBase = 0x0;
92   VirtualMemoryTable[1].VirtualBase  = 0x0;
93   VirtualMemoryTable[1].Length       = VirtualMemoryTable[0].PhysicalBase;
94   VirtualMemoryTable[1].Attributes   = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
95 
96   // Peripheral space after DRAM
97   VirtualMemoryTable[2].PhysicalBase = VirtualMemoryTable[0].Length + VirtualMemoryTable[1].Length;
98   VirtualMemoryTable[2].VirtualBase  = VirtualMemoryTable[2].PhysicalBase;
99   VirtualMemoryTable[2].Length       = MIN (1ULL << FixedPcdGet8 (PcdPrePiCpuMemorySize),
100                                          ArmGetPhysAddrTop ()) -
101                                        VirtualMemoryTable[2].PhysicalBase;
102   VirtualMemoryTable[2].Attributes   = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
103 
104   // Remap the FD region as normal executable memory
105   VirtualMemoryTable[3].PhysicalBase = FixedPcdGet64 (PcdFdBaseAddress);
106   VirtualMemoryTable[3].VirtualBase  = VirtualMemoryTable[3].PhysicalBase;
107   VirtualMemoryTable[3].Length       = FixedPcdGet32 (PcdFdSize);
108   VirtualMemoryTable[3].Attributes   = CacheAttributes;
109 
110   // End of Table
111   ZeroMem (&VirtualMemoryTable[4], sizeof (ARM_MEMORY_REGION_DESCRIPTOR));
112 
113   *VirtualMemoryMap = VirtualMemoryTable;
114 }
115