]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Drivers/CpuDxe/CpuMmuCommon.c
ArmPkg: Replace BSD License with BSD+Patent License
[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 5*\r
4059386c 6* SPDX-License-Identifier: BSD-2-Clause-Patent\r
591fb378
OM
7*\r
8**/\r
9\r
10#include "CpuDxe.h"\r
11\r
12/**\r
13 Searches memory descriptors covered by given memory range.\r
14\r
15 This function searches into the Gcd Memory Space for descriptors\r
16 (from StartIndex to EndIndex) that contains the memory range\r
17 specified by BaseAddress and Length.\r
18\r
19 @param MemorySpaceMap Gcd Memory Space Map as array.\r
20 @param NumberOfDescriptors Number of descriptors in map.\r
21 @param BaseAddress BaseAddress for the requested range.\r
22 @param Length Length for the requested range.\r
23 @param StartIndex Start index into the Gcd Memory Space Map.\r
24 @param EndIndex End index into the Gcd Memory Space Map.\r
25\r
26 @retval EFI_SUCCESS Search successfully.\r
27 @retval EFI_NOT_FOUND The requested descriptors does not exist.\r
28\r
29**/\r
30EFI_STATUS\r
31SearchGcdMemorySpaces (\r
32 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap,\r
33 IN UINTN NumberOfDescriptors,\r
34 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
35 IN UINT64 Length,\r
36 OUT UINTN *StartIndex,\r
37 OUT UINTN *EndIndex\r
38 )\r
39{\r
40 UINTN Index;\r
41\r
42 *StartIndex = 0;\r
43 *EndIndex = 0;\r
44 for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
45 if ((BaseAddress >= MemorySpaceMap[Index].BaseAddress) &&\r
46 (BaseAddress < (MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length))) {\r
47 *StartIndex = Index;\r
48 }\r
49 if (((BaseAddress + Length - 1) >= MemorySpaceMap[Index].BaseAddress) &&\r
50 ((BaseAddress + Length - 1) < (MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length))) {\r
51 *EndIndex = Index;\r
52 return EFI_SUCCESS;\r
53 }\r
54 }\r
55 return EFI_NOT_FOUND;\r
56}\r
57\r
58\r
59/**\r
60 Sets the attributes for a specified range in Gcd Memory Space Map.\r
61\r
62 This function sets the attributes for a specified range in\r
63 Gcd Memory Space Map.\r
64\r
65 @param MemorySpaceMap Gcd Memory Space Map as array\r
66 @param NumberOfDescriptors Number of descriptors in map\r
67 @param BaseAddress BaseAddress for the range\r
68 @param Length Length for the range\r
69 @param Attributes Attributes to set\r
70\r
71 @retval EFI_SUCCESS Memory attributes set successfully\r
72 @retval EFI_NOT_FOUND The specified range does not exist in Gcd Memory Space\r
73\r
74**/\r
75EFI_STATUS\r
76SetGcdMemorySpaceAttributes (\r
77 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap,\r
78 IN UINTN NumberOfDescriptors,\r
79 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
80 IN UINT64 Length,\r
81 IN UINT64 Attributes\r
82 )\r
83{\r
84 EFI_STATUS Status;\r
85 UINTN Index;\r
86 UINTN StartIndex;\r
87 UINTN EndIndex;\r
88 EFI_PHYSICAL_ADDRESS RegionStart;\r
89 UINT64 RegionLength;\r
90\r
91 DEBUG ((DEBUG_GCD, "SetGcdMemorySpaceAttributes[0x%lX; 0x%lX] = 0x%lX\n",\r
92 BaseAddress, BaseAddress + Length, Attributes));\r
93\r
047c0cbb
OM
94 // We do not support a smaller granularity than 4KB on ARM Architecture\r
95 if ((Length & EFI_PAGE_MASK) != 0) {\r
96 DEBUG ((DEBUG_WARN,\r
97 "Warning: We do not support smaller granularity than 4KB on ARM Architecture (passed length: 0x%lX).\n",\r
98 Length));\r
99 }\r
100\r
591fb378
OM
101 //\r
102 // Get all memory descriptors covered by the memory range\r
103 //\r
104 Status = SearchGcdMemorySpaces (\r
105 MemorySpaceMap,\r
106 NumberOfDescriptors,\r
107 BaseAddress,\r
108 Length,\r
109 &StartIndex,\r
110 &EndIndex\r
111 );\r
112 if (EFI_ERROR (Status)) {\r
113 return Status;\r
114 }\r
115\r
116 //\r
117 // Go through all related descriptors and set attributes accordingly\r
118 //\r
119 for (Index = StartIndex; Index <= EndIndex; Index++) {\r
120 if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeNonExistent) {\r
121 continue;\r
122 }\r
123 //\r
124 // Calculate the start and end address of the overlapping range\r
125 //\r
126 if (BaseAddress >= MemorySpaceMap[Index].BaseAddress) {\r
127 RegionStart = BaseAddress;\r
128 } else {\r
129 RegionStart = MemorySpaceMap[Index].BaseAddress;\r
130 }\r
131 if ((BaseAddress + Length - 1) < (MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length)) {\r
132 RegionLength = BaseAddress + Length - RegionStart;\r
133 } else {\r
134 RegionLength = MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length - RegionStart;\r
135 }\r
136 //\r
137 // Set memory attributes according to MTRR attribute and the original attribute of descriptor\r
138 //\r
139 gDS->SetMemorySpaceAttributes (\r
140 RegionStart,\r
141 RegionLength,\r
142 (MemorySpaceMap[Index].Attributes & ~EFI_MEMORY_CACHETYPE_MASK) | (MemorySpaceMap[Index].Capabilities & Attributes)\r
143 );\r
144 }\r
145\r
146 return EFI_SUCCESS;\r
147}\r
148\r
149/**\r
150 This function modifies the attributes for the memory region specified by BaseAddress and\r
151 Length from their current attributes to the attributes specified by Attributes.\r
152\r
153 @param This The EFI_CPU_ARCH_PROTOCOL instance.\r
154 @param BaseAddress The physical address that is the start address of a memory region.\r
155 @param Length The size in bytes of the memory region.\r
156 @param Attributes The bit mask of attributes to set for the memory region.\r
157\r
158 @retval EFI_SUCCESS The attributes were set for the memory region.\r
159 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
160 BaseAddress and Length cannot be modified.\r
161 @retval EFI_INVALID_PARAMETER Length is zero.\r
162 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
163 the memory resource range.\r
164 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
165 resource range specified by BaseAddress and Length.\r
166 The bit mask of attributes is not support for the memory resource\r
167 range specified by BaseAddress and Length.\r
168\r
169**/\r
170EFI_STATUS\r
171EFIAPI\r
172CpuSetMemoryAttributes (\r
173 IN EFI_CPU_ARCH_PROTOCOL *This,\r
174 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
175 IN UINT64 Length,\r
2e969d2e 176 IN UINT64 EfiAttributes\r
591fb378
OM
177 )\r
178{\r
2e969d2e
OM
179 EFI_STATUS Status;\r
180 UINTN ArmAttributes;\r
181 UINTN RegionBaseAddress;\r
182 UINTN RegionLength;\r
183 UINTN RegionArmAttributes;\r
591fb378 184\r
3b44bb55
AB
185 if (mIsFlushingGCD) {\r
186 return EFI_SUCCESS;\r
187 }\r
188\r
591fb378
OM
189 if ((BaseAddress & (SIZE_4KB - 1)) != 0) {\r
190 // Minimum granularity is SIZE_4KB (4KB on ARM)\r
2e969d2e 191 DEBUG ((EFI_D_PAGE, "CpuSetMemoryAttributes(%lx, %lx, %lx): Minimum ganularity is SIZE_4KB\n", BaseAddress, Length, EfiAttributes));\r
591fb378
OM
192 return EFI_UNSUPPORTED;\r
193 }\r
194\r
2e969d2e
OM
195 // Convert the 'Attribute' into ARM Attribute\r
196 ArmAttributes = EfiAttributeToArmAttribute (EfiAttributes);\r
197\r
198 // Get the region starting from 'BaseAddress' and its 'Attribute'\r
199 RegionBaseAddress = BaseAddress;\r
200 Status = GetMemoryRegion (&RegionBaseAddress, &RegionLength, &RegionArmAttributes);\r
201\r
202 // Data & Instruction Caches are flushed when we set new memory attributes.\r
203 // So, we only set the attributes if the new region is different.\r
204 if (EFI_ERROR (Status) || (RegionArmAttributes != ArmAttributes) ||\r
205 ((BaseAddress + Length) > (RegionBaseAddress + RegionLength)))\r
206 {\r
d9c0d991 207 return ArmSetMemoryAttributes (BaseAddress, Length, EfiAttributes);\r
2e969d2e
OM
208 } else {\r
209 return EFI_SUCCESS;\r
210 }\r
591fb378 211}\r