1 /** @file
2 *
3 *  Copyright (c) 2013-2015, ARM 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/HobLib.h>
18 #include <Library/PcdLib.h>
19 #include <Library/IoLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 
22 #include <ArmPlatform.h>
23 
24 // The total number of descriptors, including the final "end-of-table" descriptor.
25 #define MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS 16
26 
27 // DDR attributes
28 #define DDR_ATTRIBUTES_CACHED           ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
29 #define DDR_ATTRIBUTES_UNCACHED         ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
30 
31 /**
32   Return the Virtual Memory Map of your platform
33 
34   This Virtual Memory Map is used by MemoryInitPei Module to initialize the MMU on your platform.
35 
36   @param[out]   VirtualMemoryMap    Array of ARM_MEMORY_REGION_DESCRIPTOR describing a Physical-to-
37                                     Virtual Memory mapping. This array must be ended by a zero-filled
38                                     entry
39 
40 **/
41 VOID
ArmPlatformGetVirtualMemoryMap(IN ARM_MEMORY_REGION_DESCRIPTOR ** VirtualMemoryMap)42 ArmPlatformGetVirtualMemoryMap (
43   IN ARM_MEMORY_REGION_DESCRIPTOR** VirtualMemoryMap
44   )
45 {
46   ARM_MEMORY_REGION_ATTRIBUTES  CacheAttributes;
47   UINTN                         Index = 0;
48   ARM_MEMORY_REGION_DESCRIPTOR  *VirtualMemoryTable;
49   EFI_RESOURCE_ATTRIBUTE_TYPE   ResourceAttributes;
50 
51   ASSERT (VirtualMemoryMap != NULL);
52 
53   //
54   // Declared the additional 6GB of memory
55   //
56   ResourceAttributes =
57       EFI_RESOURCE_ATTRIBUTE_PRESENT |
58       EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
59       EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
60       EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
61       EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
62       EFI_RESOURCE_ATTRIBUTE_TESTED;
63 
64   BuildResourceDescriptorHob (
65     EFI_RESOURCE_SYSTEM_MEMORY,
66     ResourceAttributes,
67     ARM_JUNO_EXTRA_SYSTEM_MEMORY_BASE,
68     ARM_JUNO_EXTRA_SYSTEM_MEMORY_SZ);
69 
70   VirtualMemoryTable = (ARM_MEMORY_REGION_DESCRIPTOR*)AllocatePages(EFI_SIZE_TO_PAGES (sizeof(ARM_MEMORY_REGION_DESCRIPTOR) * MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS));
71   if (VirtualMemoryTable == NULL) {
72       return;
73   }
74 
75   if (FeaturePcdGet(PcdCacheEnable) == TRUE) {
76       CacheAttributes = DDR_ATTRIBUTES_CACHED;
77   } else {
78       CacheAttributes = DDR_ATTRIBUTES_UNCACHED;
79   }
80 
81   // SMB CS0 - NOR0 Flash
82   VirtualMemoryTable[Index].PhysicalBase    = ARM_VE_SMB_NOR0_BASE;
83   VirtualMemoryTable[Index].VirtualBase     = ARM_VE_SMB_NOR0_BASE;
84   VirtualMemoryTable[Index].Length          = SIZE_256KB * 255;
85   VirtualMemoryTable[Index].Attributes      = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
86   // Environment Variables region
87   VirtualMemoryTable[++Index].PhysicalBase  = ARM_VE_SMB_NOR0_BASE + (SIZE_256KB * 255);
88   VirtualMemoryTable[Index].VirtualBase     = ARM_VE_SMB_NOR0_BASE + (SIZE_256KB * 255);
89   VirtualMemoryTable[Index].Length          = SIZE_64KB * 4;
90   VirtualMemoryTable[Index].Attributes      = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
91 
92   // SMB CS2 & CS3 - Off-chip (motherboard) peripherals
93   VirtualMemoryTable[++Index].PhysicalBase  = ARM_VE_SMB_PERIPH_BASE;
94   VirtualMemoryTable[Index].VirtualBase     = ARM_VE_SMB_PERIPH_BASE;
95   VirtualMemoryTable[Index].Length          = ARM_VE_SMB_PERIPH_SZ;
96   VirtualMemoryTable[Index].Attributes      = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
97 
98   // Juno OnChip non-secure ROM
99   VirtualMemoryTable[++Index].PhysicalBase  = ARM_JUNO_NON_SECURE_ROM_BASE;
100   VirtualMemoryTable[Index].VirtualBase     = ARM_JUNO_NON_SECURE_ROM_BASE;
101   VirtualMemoryTable[Index].Length          = ARM_JUNO_NON_SECURE_ROM_SZ;
102   VirtualMemoryTable[Index].Attributes      = CacheAttributes;
103 
104   // Juno OnChip peripherals
105   VirtualMemoryTable[++Index].PhysicalBase  = ARM_JUNO_PERIPHERALS_BASE;
106   VirtualMemoryTable[Index].VirtualBase     = ARM_JUNO_PERIPHERALS_BASE;
107   VirtualMemoryTable[Index].Length          = ARM_JUNO_PERIPHERALS_SZ;
108   VirtualMemoryTable[Index].Attributes      = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
109 
110   // Juno OnChip non-secure SRAM
111   VirtualMemoryTable[++Index].PhysicalBase  = ARM_JUNO_NON_SECURE_SRAM_BASE;
112   VirtualMemoryTable[Index].VirtualBase     = ARM_JUNO_NON_SECURE_SRAM_BASE;
113   VirtualMemoryTable[Index].Length          = ARM_JUNO_NON_SECURE_SRAM_SZ;
114   VirtualMemoryTable[Index].Attributes      = CacheAttributes;
115 
116   // PCI Root Complex
117   VirtualMemoryTable[++Index].PhysicalBase  = PcdGet64 (PcdPcieControlBaseAddress);
118   VirtualMemoryTable[Index].VirtualBase     = PcdGet64 (PcdPcieControlBaseAddress);
119   VirtualMemoryTable[Index].Length          = SIZE_128KB;
120   VirtualMemoryTable[Index].Attributes      = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
121 
122   //
123   // PCI Configuration Space
124   //
125   VirtualMemoryTable[++Index].PhysicalBase  = PcdGet64 (PcdPciConfigurationSpaceBaseAddress);
126   VirtualMemoryTable[Index].VirtualBase     = PcdGet64 (PcdPciConfigurationSpaceBaseAddress);
127   VirtualMemoryTable[Index].Length          = PcdGet64 (PcdPciConfigurationSpaceSize);
128   VirtualMemoryTable[Index].Attributes      = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
129 
130   //
131   // PCI Memory Space
132   //
133   VirtualMemoryTable[++Index].PhysicalBase  = PcdGet32 (PcdPciMmio32Base);
134   VirtualMemoryTable[Index].VirtualBase     = PcdGet32 (PcdPciMmio32Base);
135   VirtualMemoryTable[Index].Length          = PcdGet32 (PcdPciMmio32Size);
136   VirtualMemoryTable[Index].Attributes      = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
137 
138   //
139   // 64-bit PCI Memory Space
140   //
141   VirtualMemoryTable[++Index].PhysicalBase  = PcdGet64 (PcdPciMmio64Base);
142   VirtualMemoryTable[Index].VirtualBase     = PcdGet64 (PcdPciMmio64Base);
143   VirtualMemoryTable[Index].Length          = PcdGet64 (PcdPciMmio64Size);
144   VirtualMemoryTable[Index].Attributes      = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
145 
146   // Juno SOC peripherals
147   VirtualMemoryTable[++Index].PhysicalBase  = ARM_JUNO_SOC_PERIPHERALS_BASE;
148   VirtualMemoryTable[Index].VirtualBase     = ARM_JUNO_SOC_PERIPHERALS_BASE;
149   VirtualMemoryTable[Index].Length          = ARM_JUNO_SOC_PERIPHERALS_SZ;
150   VirtualMemoryTable[Index].Attributes      = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
151 
152   // DDR - 2GB
153   VirtualMemoryTable[++Index].PhysicalBase  = PcdGet64 (PcdSystemMemoryBase);
154   VirtualMemoryTable[Index].VirtualBase     = PcdGet64 (PcdSystemMemoryBase);
155   VirtualMemoryTable[Index].Length          = PcdGet64 (PcdSystemMemorySize);
156   VirtualMemoryTable[Index].Attributes      = CacheAttributes;
157 
158   // DDR - 6GB
159   VirtualMemoryTable[++Index].PhysicalBase  = ARM_JUNO_EXTRA_SYSTEM_MEMORY_BASE;
160   VirtualMemoryTable[Index].VirtualBase     = ARM_JUNO_EXTRA_SYSTEM_MEMORY_BASE;
161   VirtualMemoryTable[Index].Length          = ARM_JUNO_EXTRA_SYSTEM_MEMORY_SZ;
162   VirtualMemoryTable[Index].Attributes      = CacheAttributes;
163 
164   // End of Table
165   VirtualMemoryTable[++Index].PhysicalBase  = 0;
166   VirtualMemoryTable[Index].VirtualBase     = 0;
167   VirtualMemoryTable[Index].Length          = 0;
168   VirtualMemoryTable[Index].Attributes      = (ARM_MEMORY_REGION_ATTRIBUTES)0;
169 
170   ASSERT((Index + 1) <= MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS);
171 
172   *VirtualMemoryMap = VirtualMemoryTable;
173 }
174