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