]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Drivers/CpuDxe/CpuMmuCommon.c
ArmPkg: delete references to unused guids/Pcds from CpuDxe
[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 190\r
3b44bb55
AB
191 if (mIsFlushingGCD) {\r
192 return EFI_SUCCESS;\r
193 }\r
194\r
591fb378
OM
195 if ((BaseAddress & (SIZE_4KB - 1)) != 0) {\r
196 // Minimum granularity is SIZE_4KB (4KB on ARM)\r
2e969d2e 197 DEBUG ((EFI_D_PAGE, "CpuSetMemoryAttributes(%lx, %lx, %lx): Minimum ganularity is SIZE_4KB\n", BaseAddress, Length, EfiAttributes));\r
591fb378
OM
198 return EFI_UNSUPPORTED;\r
199 }\r
200\r
2e969d2e
OM
201 // Convert the 'Attribute' into ARM Attribute\r
202 ArmAttributes = EfiAttributeToArmAttribute (EfiAttributes);\r
203\r
204 // Get the region starting from 'BaseAddress' and its 'Attribute'\r
205 RegionBaseAddress = BaseAddress;\r
206 Status = GetMemoryRegion (&RegionBaseAddress, &RegionLength, &RegionArmAttributes);\r
207\r
208 // Data & Instruction Caches are flushed when we set new memory attributes.\r
209 // So, we only set the attributes if the new region is different.\r
210 if (EFI_ERROR (Status) || (RegionArmAttributes != ArmAttributes) ||\r
211 ((BaseAddress + Length) > (RegionBaseAddress + RegionLength)))\r
212 {\r
d9c0d991 213 return ArmSetMemoryAttributes (BaseAddress, Length, EfiAttributes);\r
2e969d2e
OM
214 } else {\r
215 return EFI_SUCCESS;\r
216 }\r
591fb378 217}\r