3 * Copyright (c) 2013, ARM Limited. All rights reserved.
4 * Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
6 * This program and the accompanying materials
7 * are licensed and made available under the terms and conditions of the BSD License
8 * which accompanies this distribution. The full text of the license may be found at
9 * http://opensource.org/licenses/bsd-license.php
11 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Searches memory descriptors covered by given memory range.
21 This function searches into the Gcd Memory Space for descriptors
22 (from StartIndex to EndIndex) that contains the memory range
23 specified by BaseAddress and Length.
25 @param MemorySpaceMap Gcd Memory Space Map as array.
26 @param NumberOfDescriptors Number of descriptors in map.
27 @param BaseAddress BaseAddress for the requested range.
28 @param Length Length for the requested range.
29 @param StartIndex Start index into the Gcd Memory Space Map.
30 @param EndIndex End index into the Gcd Memory Space Map.
32 @retval EFI_SUCCESS Search successfully.
33 @retval EFI_NOT_FOUND The requested descriptors does not exist.
37 SearchGcdMemorySpaces (
38 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
,
39 IN UINTN NumberOfDescriptors
,
40 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
42 OUT UINTN
*StartIndex
,
50 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
51 if ((BaseAddress
>= MemorySpaceMap
[Index
].BaseAddress
) &&
52 (BaseAddress
< (MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
))) {
55 if (((BaseAddress
+ Length
- 1) >= MemorySpaceMap
[Index
].BaseAddress
) &&
56 ((BaseAddress
+ Length
- 1) < (MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
))) {
66 Sets the attributes for a specified range in Gcd Memory Space Map.
68 This function sets the attributes for a specified range in
71 @param MemorySpaceMap Gcd Memory Space Map as array
72 @param NumberOfDescriptors Number of descriptors in map
73 @param BaseAddress BaseAddress for the range
74 @param Length Length for the range
75 @param Attributes Attributes to set
77 @retval EFI_SUCCESS Memory attributes set successfully
78 @retval EFI_NOT_FOUND The specified range does not exist in Gcd Memory Space
82 SetGcdMemorySpaceAttributes (
83 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
,
84 IN UINTN NumberOfDescriptors
,
85 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
94 EFI_PHYSICAL_ADDRESS RegionStart
;
97 DEBUG ((DEBUG_GCD
, "SetGcdMemorySpaceAttributes[0x%lX; 0x%lX] = 0x%lX\n",
98 BaseAddress
, BaseAddress
+ Length
, Attributes
));
100 // We do not support a smaller granularity than 4KB on ARM Architecture
101 if ((Length
& EFI_PAGE_MASK
) != 0) {
103 "Warning: We do not support smaller granularity than 4KB on ARM Architecture (passed length: 0x%lX).\n",
108 // Get all memory descriptors covered by the memory range
110 Status
= SearchGcdMemorySpaces (
118 if (EFI_ERROR (Status
)) {
123 // Go through all related descriptors and set attributes accordingly
125 for (Index
= StartIndex
; Index
<= EndIndex
; Index
++) {
126 if (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeNonExistent
) {
130 // Calculate the start and end address of the overlapping range
132 if (BaseAddress
>= MemorySpaceMap
[Index
].BaseAddress
) {
133 RegionStart
= BaseAddress
;
135 RegionStart
= MemorySpaceMap
[Index
].BaseAddress
;
137 if ((BaseAddress
+ Length
- 1) < (MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
)) {
138 RegionLength
= BaseAddress
+ Length
- RegionStart
;
140 RegionLength
= MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
- RegionStart
;
143 // Set memory attributes according to MTRR attribute and the original attribute of descriptor
145 gDS
->SetMemorySpaceAttributes (
148 (MemorySpaceMap
[Index
].Attributes
& ~EFI_MEMORY_CACHETYPE_MASK
) | (MemorySpaceMap
[Index
].Capabilities
& Attributes
)
156 This function modifies the attributes for the memory region specified by BaseAddress and
157 Length from their current attributes to the attributes specified by Attributes.
159 @param This The EFI_CPU_ARCH_PROTOCOL instance.
160 @param BaseAddress The physical address that is the start address of a memory region.
161 @param Length The size in bytes of the memory region.
162 @param Attributes The bit mask of attributes to set for the memory region.
164 @retval EFI_SUCCESS The attributes were set for the memory region.
165 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by
166 BaseAddress and Length cannot be modified.
167 @retval EFI_INVALID_PARAMETER Length is zero.
168 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
169 the memory resource range.
170 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
171 resource range specified by BaseAddress and Length.
172 The bit mask of attributes is not support for the memory resource
173 range specified by BaseAddress and Length.
178 CpuSetMemoryAttributes (
179 IN EFI_CPU_ARCH_PROTOCOL
*This
,
180 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
182 IN UINT64 EfiAttributes
187 UINTN RegionBaseAddress
;
189 UINTN RegionArmAttributes
;
191 if (mIsFlushingGCD
) {
195 if ((BaseAddress
& (SIZE_4KB
- 1)) != 0) {
196 // Minimum granularity is SIZE_4KB (4KB on ARM)
197 DEBUG ((EFI_D_PAGE
, "CpuSetMemoryAttributes(%lx, %lx, %lx): Minimum ganularity is SIZE_4KB\n", BaseAddress
, Length
, EfiAttributes
));
198 return EFI_UNSUPPORTED
;
201 // Convert the 'Attribute' into ARM Attribute
202 ArmAttributes
= EfiAttributeToArmAttribute (EfiAttributes
);
204 // Get the region starting from 'BaseAddress' and its 'Attribute'
205 RegionBaseAddress
= BaseAddress
;
206 Status
= GetMemoryRegion (&RegionBaseAddress
, &RegionLength
, &RegionArmAttributes
);
208 // Data & Instruction Caches are flushed when we set new memory attributes.
209 // So, we only set the attributes if the new region is different.
210 if (EFI_ERROR (Status
) || (RegionArmAttributes
!= ArmAttributes
) ||
211 ((BaseAddress
+ Length
) > (RegionBaseAddress
+ RegionLength
)))
213 return ArmSetMemoryAttributes (BaseAddress
, Length
, EfiAttributes
);