2 SMM Memory pool management functions.
4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "PiSmmCore.h"
18 // MIN_POOL_SHIFT must not be less than 5
20 #define MIN_POOL_SHIFT 6
21 #define MIN_POOL_SIZE (1 << MIN_POOL_SHIFT)
24 // MAX_POOL_SHIFT must not be less than EFI_PAGE_SHIFT - 1
26 #define MAX_POOL_SHIFT (EFI_PAGE_SHIFT - 1)
27 #define MAX_POOL_SIZE (1 << MAX_POOL_SHIFT)
30 // MAX_POOL_INDEX are calculated by maximum and minimum pool sizes
32 #define MAX_POOL_INDEX (MAX_POOL_SHIFT - MIN_POOL_SHIFT + 1)
44 LIST_ENTRY mSmmPoolLists
[MAX_POOL_INDEX
];
46 // To cache the SMRAM base since when Loading modules At fixed address feature is enabled,
47 // all module is assigned an offset relative the SMRAM base in build time.
49 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PHYSICAL_ADDRESS gLoadModuleAtFixAddressSmramBase
= 0;
52 Called to initialize the memory service.
54 @param SmramRangeCount Number of SMRAM Regions
55 @param SmramRanges Pointer to SMRAM Descriptors
59 SmmInitializeMemoryServices (
60 IN UINTN SmramRangeCount
,
61 IN EFI_SMRAM_DESCRIPTOR
*SmramRanges
66 UINTN CurrentSmramRangesIndex
;
70 // Initialize Pool list
72 for (Index
= sizeof (mSmmPoolLists
) / sizeof (*mSmmPoolLists
); Index
> 0;) {
73 InitializeListHead (&mSmmPoolLists
[--Index
]);
75 CurrentSmramRangesIndex
= 0;
77 // If Loadding Module At fixed Address feature is enabled, cache the SMRAM base here
79 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
81 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
83 SmmCodeSize
= LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
), EFI_PAGE_SHIFT
);
86 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
88 for (Index
= 0, MaxSize
= SIZE_256KB
- EFI_PAGE_SIZE
; Index
< SmramRangeCount
; Index
++) {
90 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization
92 if ((SmramRanges
[Index
].RegionState
& (EFI_ALLOCATED
| EFI_NEEDS_TESTING
| EFI_NEEDS_ECC_INITIALIZATION
)) != 0) {
96 if (SmramRanges
[Index
].CpuStart
>= BASE_1MB
) {
97 if ((SmramRanges
[Index
].CpuStart
+ SmramRanges
[Index
].PhysicalSize
) <= BASE_4GB
) {
98 if (SmramRanges
[Index
].PhysicalSize
>= MaxSize
) {
99 MaxSize
= SmramRanges
[Index
].PhysicalSize
;
100 CurrentSmramRangesIndex
= Index
;
105 gLoadModuleAtFixAddressSmramBase
= SmramRanges
[CurrentSmramRangesIndex
].CpuStart
;
108 // cut out a memory range from this SMRAM range with the size SmmCodeSize to hold SMM driver code
109 // A notable thing is that SMM core is already loaded into this range.
111 SmramRanges
[CurrentSmramRangesIndex
].CpuStart
= SmramRanges
[CurrentSmramRangesIndex
].CpuStart
+ SmmCodeSize
;
112 SmramRanges
[CurrentSmramRangesIndex
].PhysicalSize
= SmramRanges
[CurrentSmramRangesIndex
].PhysicalSize
- SmmCodeSize
;
115 // Initialize free SMRAM regions
117 for (Index
= 0; Index
< SmramRangeCount
; Index
++) {
119 SmramRanges
[Index
].CpuStart
,
120 SmramRanges
[Index
].PhysicalSize
,
121 EfiConventionalMemory
,
122 SmramRanges
[Index
].RegionState
129 Internal Function. Allocate a pool by specified PoolIndex.
131 @param PoolIndex Index which indicate the Pool size.
132 @param FreePoolHdr The returned Free pool.
134 @retval EFI_OUT_OF_RESOURCES Allocation failed.
135 @retval EFI_SUCCESS Pool successfully allocated.
139 InternalAllocPoolByIndex (
141 OUT FREE_POOL_HEADER
**FreePoolHdr
145 FREE_POOL_HEADER
*Hdr
;
147 ASSERT (PoolIndex
<= MAX_POOL_INDEX
);
148 Status
= EFI_SUCCESS
;
149 if (PoolIndex
== MAX_POOL_INDEX
) {
150 Hdr
= (FREE_POOL_HEADER
*)AllocatePages (EFI_SIZE_TO_PAGES (MAX_POOL_SIZE
<< 1));
152 return EFI_OUT_OF_RESOURCES
;
154 } else if (!IsListEmpty (&mSmmPoolLists
[PoolIndex
])) {
155 Hdr
= BASE_CR (GetFirstNode (&mSmmPoolLists
[PoolIndex
]), FREE_POOL_HEADER
, Link
);
156 RemoveEntryList (&Hdr
->Link
);
158 Status
= InternalAllocPoolByIndex (PoolIndex
+ 1, &Hdr
);
159 if (!EFI_ERROR (Status
)) {
160 Hdr
->Header
.Size
>>= 1;
161 Hdr
->Header
.Available
= TRUE
;
162 InsertHeadList (&mSmmPoolLists
[PoolIndex
], &Hdr
->Link
);
163 Hdr
= (FREE_POOL_HEADER
*)((UINT8
*)Hdr
+ Hdr
->Header
.Size
);
167 if (!EFI_ERROR (Status
)) {
168 Hdr
->Header
.Size
= MIN_POOL_SIZE
<< PoolIndex
;
169 Hdr
->Header
.Available
= FALSE
;
177 Internal Function. Free a pool by specified PoolIndex.
179 @param FreePoolHdr The pool to free.
181 @retval EFI_SUCCESS Pool successfully freed.
185 InternalFreePoolByIndex (
186 IN FREE_POOL_HEADER
*FreePoolHdr
191 ASSERT ((FreePoolHdr
->Header
.Size
& (FreePoolHdr
->Header
.Size
- 1)) == 0);
192 ASSERT (((UINTN
)FreePoolHdr
& (FreePoolHdr
->Header
.Size
- 1)) == 0);
193 ASSERT (FreePoolHdr
->Header
.Size
>= MIN_POOL_SIZE
);
195 PoolIndex
= (UINTN
) (HighBitSet32 ((UINT32
)FreePoolHdr
->Header
.Size
) - MIN_POOL_SHIFT
);
196 FreePoolHdr
->Header
.Available
= TRUE
;
197 ASSERT (PoolIndex
< MAX_POOL_INDEX
);
198 InsertHeadList (&mSmmPoolLists
[PoolIndex
], &FreePoolHdr
->Link
);
203 Allocate pool of a particular type.
205 @param PoolType Type of pool to allocate.
206 @param Size The amount of pool to allocate.
207 @param Buffer The address to return a pointer to the allocated
210 @retval EFI_INVALID_PARAMETER PoolType not valid.
211 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
212 @retval EFI_SUCCESS Pool successfully allocated.
218 IN EFI_MEMORY_TYPE PoolType
,
223 POOL_HEADER
*PoolHdr
;
224 FREE_POOL_HEADER
*FreePoolHdr
;
226 EFI_PHYSICAL_ADDRESS Address
;
229 if (PoolType
!= EfiRuntimeServicesCode
&&
230 PoolType
!= EfiRuntimeServicesData
) {
231 return EFI_INVALID_PARAMETER
;
234 Size
+= sizeof (*PoolHdr
);
235 if (Size
> MAX_POOL_SIZE
) {
236 Size
= EFI_SIZE_TO_PAGES (Size
);
237 Status
= SmmAllocatePages (AllocateAnyPages
, PoolType
, Size
, &Address
);
238 if (EFI_ERROR (Status
)) {
242 PoolHdr
= (POOL_HEADER
*)(UINTN
)Address
;
243 PoolHdr
->Size
= EFI_PAGES_TO_SIZE (Size
);
244 PoolHdr
->Available
= FALSE
;
245 *Buffer
= PoolHdr
+ 1;
249 Size
= (Size
+ MIN_POOL_SIZE
- 1) >> MIN_POOL_SHIFT
;
250 PoolIndex
= (UINTN
) HighBitSet32 ((UINT32
)Size
);
251 if ((Size
& (Size
- 1)) != 0) {
255 Status
= InternalAllocPoolByIndex (PoolIndex
, &FreePoolHdr
);
256 *Buffer
= &FreePoolHdr
->Header
+ 1;
263 @param Buffer The allocated pool entry to free.
265 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
266 @retval EFI_SUCCESS Pool successfully freed.
275 FREE_POOL_HEADER
*FreePoolHdr
;
277 if (Buffer
== NULL
) {
278 return EFI_INVALID_PARAMETER
;
281 FreePoolHdr
= (FREE_POOL_HEADER
*)((POOL_HEADER
*)Buffer
- 1);
282 ASSERT (!FreePoolHdr
->Header
.Available
);
284 if (FreePoolHdr
->Header
.Size
> MAX_POOL_SIZE
) {
285 ASSERT (((UINTN
)FreePoolHdr
& EFI_PAGE_MASK
) == 0);
286 ASSERT ((FreePoolHdr
->Header
.Size
& EFI_PAGE_MASK
) == 0);
287 return SmmFreePages (
288 (EFI_PHYSICAL_ADDRESS
)(UINTN
)FreePoolHdr
,
289 EFI_SIZE_TO_PAGES (FreePoolHdr
->Header
.Size
)
292 return InternalFreePoolByIndex (FreePoolHdr
);