]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Drivers/CpuDxe/CpuMmuCommon.c
OvmfPkg/AcpiPlatformDxe: replay QEMU_LOADER_WRITE_POINTER commands at S3
[mirror_edk2.git] / ArmPkg / Drivers / CpuDxe / CpuMmuCommon.c
CommitLineData
591fb378
OM
1/** @file\r
2*\r
3* Copyright (c) 2013, ARM Limited. All rights reserved.\r
4*\r
5* This program and the accompanying materials\r
6* are licensed and made available under the terms and conditions of the BSD License\r
7* which accompanies this distribution. The full text of the license may be found at\r
8* http://opensource.org/licenses/bsd-license.php\r
9*\r
10* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12*\r
13**/\r
14\r
15#include "CpuDxe.h"\r
16\r
17/**\r
18 Searches memory descriptors covered by given memory range.\r
19\r
20 This function searches into the Gcd Memory Space for descriptors\r
21 (from StartIndex to EndIndex) that contains the memory range\r
22 specified by BaseAddress and Length.\r
23\r
24 @param MemorySpaceMap Gcd Memory Space Map as array.\r
25 @param NumberOfDescriptors Number of descriptors in map.\r
26 @param BaseAddress BaseAddress for the requested range.\r
27 @param Length Length for the requested range.\r
28 @param StartIndex Start index into the Gcd Memory Space Map.\r
29 @param EndIndex End index into the Gcd Memory Space Map.\r
30\r
31 @retval EFI_SUCCESS Search successfully.\r
32 @retval EFI_NOT_FOUND The requested descriptors does not exist.\r
33\r
34**/\r
35EFI_STATUS\r
36SearchGcdMemorySpaces (\r
37 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap,\r
38 IN UINTN NumberOfDescriptors,\r
39 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
40 IN UINT64 Length,\r
41 OUT UINTN *StartIndex,\r
42 OUT UINTN *EndIndex\r
43 )\r
44{\r
45 UINTN Index;\r
46\r
47 *StartIndex = 0;\r
48 *EndIndex = 0;\r
49 for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
50 if ((BaseAddress >= MemorySpaceMap[Index].BaseAddress) &&\r
51 (BaseAddress < (MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length))) {\r
52 *StartIndex = Index;\r
53 }\r
54 if (((BaseAddress + Length - 1) >= MemorySpaceMap[Index].BaseAddress) &&\r
55 ((BaseAddress + Length - 1) < (MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length))) {\r
56 *EndIndex = Index;\r
57 return EFI_SUCCESS;\r
58 }\r
59 }\r
60 return EFI_NOT_FOUND;\r
61}\r
62\r
63\r
64/**\r
65 Sets the attributes for a specified range in Gcd Memory Space Map.\r
66\r
67 This function sets the attributes for a specified range in\r
68 Gcd Memory Space Map.\r
69\r
70 @param MemorySpaceMap Gcd Memory Space Map as array\r
71 @param NumberOfDescriptors Number of descriptors in map\r
72 @param BaseAddress BaseAddress for the range\r
73 @param Length Length for the range\r
74 @param Attributes Attributes to set\r
75\r
76 @retval EFI_SUCCESS Memory attributes set successfully\r
77 @retval EFI_NOT_FOUND The specified range does not exist in Gcd Memory Space\r
78\r
79**/\r
80EFI_STATUS\r
81SetGcdMemorySpaceAttributes (\r
82 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap,\r
83 IN UINTN NumberOfDescriptors,\r
84 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
85 IN UINT64 Length,\r
86 IN UINT64 Attributes\r
87 )\r
88{\r
89 EFI_STATUS Status;\r
90 UINTN Index;\r
91 UINTN StartIndex;\r
92 UINTN EndIndex;\r
93 EFI_PHYSICAL_ADDRESS RegionStart;\r
94 UINT64 RegionLength;\r
95\r
96 DEBUG ((DEBUG_GCD, "SetGcdMemorySpaceAttributes[0x%lX; 0x%lX] = 0x%lX\n",\r
97 BaseAddress, BaseAddress + Length, Attributes));\r
98\r
047c0cbb
OM
99 // We do not support a smaller granularity than 4KB on ARM Architecture\r
100 if ((Length & EFI_PAGE_MASK) != 0) {\r
101 DEBUG ((DEBUG_WARN,\r
102 "Warning: We do not support smaller granularity than 4KB on ARM Architecture (passed length: 0x%lX).\n",\r
103 Length));\r
104 }\r
105\r
591fb378
OM
106 //\r
107 // Get all memory descriptors covered by the memory range\r
108 //\r
109 Status = SearchGcdMemorySpaces (\r
110 MemorySpaceMap,\r
111 NumberOfDescriptors,\r
112 BaseAddress,\r
113 Length,\r
114 &StartIndex,\r
115 &EndIndex\r
116 );\r
117 if (EFI_ERROR (Status)) {\r
118 return Status;\r
119 }\r
120\r
121 //\r
122 // Go through all related descriptors and set attributes accordingly\r
123 //\r
124 for (Index = StartIndex; Index <= EndIndex; Index++) {\r
125 if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeNonExistent) {\r
126 continue;\r
127 }\r
128 //\r
129 // Calculate the start and end address of the overlapping range\r
130 //\r
131 if (BaseAddress >= MemorySpaceMap[Index].BaseAddress) {\r
132 RegionStart = BaseAddress;\r
133 } else {\r
134 RegionStart = MemorySpaceMap[Index].BaseAddress;\r
135 }\r
136 if ((BaseAddress + Length - 1) < (MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length)) {\r
137 RegionLength = BaseAddress + Length - RegionStart;\r
138 } else {\r
139 RegionLength = MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length - RegionStart;\r
140 }\r
141 //\r
142 // Set memory attributes according to MTRR attribute and the original attribute of descriptor\r
143 //\r
144 gDS->SetMemorySpaceAttributes (\r
145 RegionStart,\r
146 RegionLength,\r
147 (MemorySpaceMap[Index].Attributes & ~EFI_MEMORY_CACHETYPE_MASK) | (MemorySpaceMap[Index].Capabilities & Attributes)\r
148 );\r
149 }\r
150\r
151 return EFI_SUCCESS;\r
152}\r
153\r
154/**\r
155 This function modifies the attributes for the memory region specified by BaseAddress and\r
156 Length from their current attributes to the attributes specified by Attributes.\r
157\r
158 @param This The EFI_CPU_ARCH_PROTOCOL instance.\r
159 @param BaseAddress The physical address that is the start address of a memory region.\r
160 @param Length The size in bytes of the memory region.\r
161 @param Attributes The bit mask of attributes to set for the memory region.\r
162\r
163 @retval EFI_SUCCESS The attributes were set for the memory region.\r
164 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
165 BaseAddress and Length cannot be modified.\r
166 @retval EFI_INVALID_PARAMETER Length is zero.\r
167 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
168 the memory resource range.\r
169 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
170 resource range specified by BaseAddress and Length.\r
171 The bit mask of attributes is not support for the memory resource\r
172 range specified by BaseAddress and Length.\r
173\r
174**/\r
175EFI_STATUS\r
176EFIAPI\r
177CpuSetMemoryAttributes (\r
178 IN EFI_CPU_ARCH_PROTOCOL *This,\r
179 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
180 IN UINT64 Length,\r
2e969d2e 181 IN UINT64 EfiAttributes\r
591fb378
OM
182 )\r
183{\r
2e969d2e
OM
184 EFI_STATUS Status;\r
185 UINTN ArmAttributes;\r
186 UINTN RegionBaseAddress;\r
187 UINTN RegionLength;\r
188 UINTN RegionArmAttributes;\r
591fb378
OM
189\r
190 if ((BaseAddress & (SIZE_4KB - 1)) != 0) {\r
191 // Minimum granularity is SIZE_4KB (4KB on ARM)\r
2e969d2e 192 DEBUG ((EFI_D_PAGE, "CpuSetMemoryAttributes(%lx, %lx, %lx): Minimum ganularity is SIZE_4KB\n", BaseAddress, Length, EfiAttributes));\r
591fb378
OM
193 return EFI_UNSUPPORTED;\r
194 }\r
195\r
2e969d2e
OM
196 // Convert the 'Attribute' into ARM Attribute\r
197 ArmAttributes = EfiAttributeToArmAttribute (EfiAttributes);\r
198\r
199 // Get the region starting from 'BaseAddress' and its 'Attribute'\r
200 RegionBaseAddress = BaseAddress;\r
201 Status = GetMemoryRegion (&RegionBaseAddress, &RegionLength, &RegionArmAttributes);\r
202\r
203 // Data & Instruction Caches are flushed when we set new memory attributes.\r
204 // So, we only set the attributes if the new region is different.\r
205 if (EFI_ERROR (Status) || (RegionArmAttributes != ArmAttributes) ||\r
206 ((BaseAddress + Length) > (RegionBaseAddress + RegionLength)))\r
207 {\r
208 return SetMemoryAttributes (BaseAddress, Length, EfiAttributes, 0);\r
209 } else {\r
210 return EFI_SUCCESS;\r
211 }\r
591fb378
OM
212}\r
213\r
214EFI_STATUS\r
215EFIAPI\r
216CpuConvertPagesToUncachedVirtualAddress (\r
217 IN VIRTUAL_UNCACHED_PAGES_PROTOCOL *This,\r
218 IN EFI_PHYSICAL_ADDRESS Address,\r
219 IN UINTN Length,\r
220 IN EFI_PHYSICAL_ADDRESS VirtualMask,\r
221 OUT UINT64 *Attributes OPTIONAL\r
222 )\r
223{\r
224 EFI_STATUS Status;\r
225 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
226\r
227 if (Attributes != NULL) {\r
228 Status = gDS->GetMemorySpaceDescriptor (Address, &GcdDescriptor);\r
229 if (!EFI_ERROR (Status)) {\r
230 *Attributes = GcdDescriptor.Attributes;\r
231 }\r
232 }\r
233\r
234 //\r
235 // Make this address range page fault if accessed. If it is a DMA buffer than this would\r
236 // be the PCI address. Code should always use the CPU address, and we will or in VirtualMask\r
237 // to that address.\r
238 //\r
239 Status = SetMemoryAttributes (Address, Length, EFI_MEMORY_WP, 0);\r
240 if (!EFI_ERROR (Status)) {\r
241 Status = SetMemoryAttributes (Address | VirtualMask, Length, EFI_MEMORY_UC, VirtualMask);\r
242 }\r
243\r
244 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "CpuConvertPagesToUncachedVirtualAddress()\n Unmapped 0x%08lx Mapped 0x%08lx 0x%x bytes\n", Address, Address | VirtualMask, Length));\r
245\r
246 return Status;\r
247}\r
248\r
249\r
250EFI_STATUS\r
251EFIAPI\r
252CpuReconvertPages (\r
253 IN VIRTUAL_UNCACHED_PAGES_PROTOCOL *This,\r
254 IN EFI_PHYSICAL_ADDRESS Address,\r
255 IN UINTN Length,\r
256 IN EFI_PHYSICAL_ADDRESS VirtualMask,\r
257 IN UINT64 Attributes\r
258 )\r
259{\r
260 EFI_STATUS Status;\r
261\r
262 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "CpuReconvertPages(%lx, %x, %lx, %lx)\n", Address, Length, VirtualMask, Attributes));\r
263\r
264 //\r
265 // Unmap the aliased Address\r
266 //\r
267 Status = SetMemoryAttributes (Address | VirtualMask, Length, EFI_MEMORY_WP, 0);\r
268 if (!EFI_ERROR (Status)) {\r
269 //\r
270 // Restore atttributes\r
271 //\r
272 Status = SetMemoryAttributes (Address, Length, Attributes, 0);\r
273 }\r
274\r
275 return Status;\r
276}\r
277\r
278\r
279VIRTUAL_UNCACHED_PAGES_PROTOCOL gVirtualUncachedPages = {\r
280 CpuConvertPagesToUncachedVirtualAddress,\r
281 CpuReconvertPages\r
282};\r