1 /** @file
2 *
3 * Copyright (c) 2014-2017, 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/HobLib.h>
18 #include <Library/IoLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/PcdLib.h>
21
22 #include <Hi6220.h>
23
24 // The total number of descriptors, including the final "end-of-table" descriptor.
25 #define MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS 12
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 #define HIKEY_EXTRA_SYSTEM_MEMORY_BASE 0x40000000
32 #define HIKEY_EXTRA_SYSTEM_MEMORY_SIZE 0x40000000
33
34 STATIC struct HiKeyReservedMemory {
35 EFI_PHYSICAL_ADDRESS Offset;
36 EFI_PHYSICAL_ADDRESS Size;
37 } HiKeyReservedMemoryBuffer [] = {
38 { 0x05E00000, 0x00100000 }, // MCU
39 { 0x05F01000, 0x00001000 }, // ADB REBOOT "REASON"
40 { 0x06DFF000, 0x00001000 }, // MAILBOX
41 { 0x0740F000, 0x00001000 }, // MAILBOX
42 { 0x21F00000, 0x00100000 }, // PSTORE/RAMOOPS
43 { 0x3E000000, 0x02000000 } // TEE OS
44 };
45
46 STATIC
47 UINT64
48 EFIAPI
HiKeyInitMemorySize(IN VOID)49 HiKeyInitMemorySize (
50 IN VOID
51 )
52 {
53 UINT32 Data;
54 UINT64 MemorySize;
55
56 Data = MmioRead32 (MDDRC_AXI_BASE + AXI_REGION_MAP);
57 MemorySize = HIKEY_REGION_SIZE(Data);
58 return MemorySize;
59 }
60
61 /**
62 Return the Virtual Memory Map of your platform
63
64 This Virtual Memory Map is used by MemoryInitPei Module to initialize the MMU on your platform.
65
66 @param[out] VirtualMemoryMap Array of ARM_MEMORY_REGION_DESCRIPTOR describing a Physical-to-
67 Virtual Memory mapping. This array must be ended by a zero-filled
68 entry
69
70 **/
71 VOID
ArmPlatformGetVirtualMemoryMap(IN ARM_MEMORY_REGION_DESCRIPTOR ** VirtualMemoryMap)72 ArmPlatformGetVirtualMemoryMap (
73 IN ARM_MEMORY_REGION_DESCRIPTOR** VirtualMemoryMap
74 )
75 {
76 ARM_MEMORY_REGION_ATTRIBUTES CacheAttributes;
77 UINTN Index = 0, Count, ReservedTop;
78 ARM_MEMORY_REGION_DESCRIPTOR *VirtualMemoryTable;
79 EFI_PEI_HOB_POINTERS NextHob;
80 EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttributes;
81 UINT64 ResourceLength;
82 EFI_PHYSICAL_ADDRESS ResourceTop;
83 UINT64 MemorySize, AdditionalMemorySize;
84
85 MemorySize = HiKeyInitMemorySize ();
86 if (MemorySize == 0) {
87 MemorySize = PcdGet64 (PcdSystemMemorySize);
88 }
89
90 ResourceAttributes = (
91 EFI_RESOURCE_ATTRIBUTE_PRESENT |
92 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
93 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
94 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
95 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
96 EFI_RESOURCE_ATTRIBUTE_TESTED
97 );
98
99 // Create initial Base Hob for system memory.
100 BuildResourceDescriptorHob (
101 EFI_RESOURCE_SYSTEM_MEMORY,
102 ResourceAttributes,
103 PcdGet64 (PcdSystemMemoryBase),
104 PcdGet64 (PcdSystemMemorySize)
105 );
106
107 NextHob.Raw = GetHobList ();
108 Count = sizeof (HiKeyReservedMemoryBuffer) / sizeof (struct HiKeyReservedMemory);
109 while ((NextHob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, NextHob.Raw)) != NULL) {
110 if (Index >= Count) {
111 break;
112 }
113 if ((NextHob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) &&
114 (HiKeyReservedMemoryBuffer[Index].Offset >= NextHob.ResourceDescriptor->PhysicalStart) &&
115 ((HiKeyReservedMemoryBuffer[Index].Offset + HiKeyReservedMemoryBuffer[Index].Size) <=
116 NextHob.ResourceDescriptor->PhysicalStart + NextHob.ResourceDescriptor->ResourceLength)) {
117 ResourceAttributes = NextHob.ResourceDescriptor->ResourceAttribute;
118 ResourceLength = NextHob.ResourceDescriptor->ResourceLength;
119 ResourceTop = NextHob.ResourceDescriptor->PhysicalStart + ResourceLength;
120 ReservedTop = HiKeyReservedMemoryBuffer[Index].Offset + HiKeyReservedMemoryBuffer[Index].Size;
121
122 // Create the System Memory HOB for the reserved buffer
123 BuildResourceDescriptorHob (EFI_RESOURCE_MEMORY_RESERVED,
124 EFI_RESOURCE_ATTRIBUTE_PRESENT,
125 HiKeyReservedMemoryBuffer[Index].Offset,
126 HiKeyReservedMemoryBuffer[Index].Size);
127 // Update the HOB
128 NextHob.ResourceDescriptor->ResourceLength = HiKeyReservedMemoryBuffer[Index].Offset - NextHob.ResourceDescriptor->PhysicalStart;
129
130 // If there is some memory available on the top of the reserved memory then create a HOB
131 if (ReservedTop < ResourceTop) {
132 BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,
133 ResourceAttributes,
134 ReservedTop,
135 ResourceTop - ReservedTop);
136 }
137 Index++;
138 }
139 NextHob.Raw = GET_NEXT_HOB (NextHob);
140 }
141
142 AdditionalMemorySize = MemorySize - PcdGet64 (PcdSystemMemorySize);
143 if (AdditionalMemorySize >= SIZE_1GB) {
144 // Declared the additional memory
145 ResourceAttributes =
146 EFI_RESOURCE_ATTRIBUTE_PRESENT |
147 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
148 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
149 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
150 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
151 EFI_RESOURCE_ATTRIBUTE_TESTED;
152
153 BuildResourceDescriptorHob (
154 EFI_RESOURCE_SYSTEM_MEMORY,
155 ResourceAttributes,
156 HIKEY_EXTRA_SYSTEM_MEMORY_BASE,
157 HIKEY_EXTRA_SYSTEM_MEMORY_SIZE);
158 }
159
160 ASSERT (VirtualMemoryMap != NULL);
161
162 VirtualMemoryTable = (ARM_MEMORY_REGION_DESCRIPTOR*)AllocatePages(EFI_SIZE_TO_PAGES (sizeof(ARM_MEMORY_REGION_DESCRIPTOR) * MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS));
163 if (VirtualMemoryTable == NULL) {
164 return;
165 }
166
167 if (FeaturePcdGet(PcdCacheEnable) == TRUE) {
168 CacheAttributes = DDR_ATTRIBUTES_CACHED;
169 } else {
170 CacheAttributes = DDR_ATTRIBUTES_UNCACHED;
171 }
172
173 Index = 0;
174
175 // Hi6220 SOC peripherals
176 VirtualMemoryTable[Index].PhysicalBase = HI6220_PERIPH_BASE;
177 VirtualMemoryTable[Index].VirtualBase = HI6220_PERIPH_BASE;
178 VirtualMemoryTable[Index].Length = HI6220_PERIPH_SZ;
179 VirtualMemoryTable[Index].Attributes = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
180
181 // DDR - predefined 1GB size
182 VirtualMemoryTable[++Index].PhysicalBase = PcdGet64 (PcdSystemMemoryBase);
183 VirtualMemoryTable[Index].VirtualBase = PcdGet64 (PcdSystemMemoryBase);
184 VirtualMemoryTable[Index].Length = PcdGet64 (PcdSystemMemorySize);
185 VirtualMemoryTable[Index].Attributes = CacheAttributes;
186
187 // If DDR capacity is 2GB size, append a new entry to fill the gap.
188 if (AdditionalMemorySize >= SIZE_1GB) {
189 VirtualMemoryTable[++Index].PhysicalBase = HIKEY_EXTRA_SYSTEM_MEMORY_BASE;
190 VirtualMemoryTable[Index].VirtualBase = HIKEY_EXTRA_SYSTEM_MEMORY_BASE;
191 VirtualMemoryTable[Index].Length = HIKEY_EXTRA_SYSTEM_MEMORY_SIZE;
192 VirtualMemoryTable[Index].Attributes = CacheAttributes;
193 }
194
195 // End of Table
196 VirtualMemoryTable[++Index].PhysicalBase = 0;
197 VirtualMemoryTable[Index].VirtualBase = 0;
198 VirtualMemoryTable[Index].Length = 0;
199 VirtualMemoryTable[Index].Attributes = (ARM_MEMORY_REGION_ATTRIBUTES)0;
200
201 ASSERT((Index + 1) <= MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS);
202
203 *VirtualMemoryMap = VirtualMemoryTable;
204 }
205