2 CPU DXE Module to produce CPU ARCH Protocol.
4 Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "CpuPageTable.h"
16 BOOLEAN InterruptState
= FALSE
;
17 EFI_HANDLE mCpuHandle
= NULL
;
18 BOOLEAN mIsFlushingGCD
;
19 BOOLEAN mIsAllocatingPageTable
= FALSE
;
20 UINT64 mValidMtrrAddressMask
;
21 UINT64 mValidMtrrBitsMask
;
22 UINT64 mTimerPeriod
= 0;
24 FIXED_MTRR mFixedMtrrTable
[] = {
26 MSR_IA32_MTRR_FIX64K_00000
,
31 MSR_IA32_MTRR_FIX16K_80000
,
36 MSR_IA32_MTRR_FIX16K_A0000
,
41 MSR_IA32_MTRR_FIX4K_C0000
,
46 MSR_IA32_MTRR_FIX4K_C8000
,
51 MSR_IA32_MTRR_FIX4K_D0000
,
56 MSR_IA32_MTRR_FIX4K_D8000
,
61 MSR_IA32_MTRR_FIX4K_E0000
,
66 MSR_IA32_MTRR_FIX4K_E8000
,
71 MSR_IA32_MTRR_FIX4K_F0000
,
76 MSR_IA32_MTRR_FIX4K_F8000
,
83 EFI_CPU_ARCH_PROTOCOL gCpu
= {
89 CpuRegisterInterruptHandler
,
91 CpuSetMemoryAttributes
,
93 4 // DmaBufferAlignment
97 // CPU Arch Protocol Functions
101 Flush CPU data cache. If the instruction cache is fully coherent
102 with all DMA operations then function can just return EFI_SUCCESS.
104 @param This Protocol instance structure
105 @param Start Physical address to start flushing from.
106 @param Length Number of bytes to flush. Round up to chipset
108 @param FlushType Specifies the type of flush operation to perform.
110 @retval EFI_SUCCESS If cache was flushed
111 @retval EFI_UNSUPPORTED If flush type is not supported.
112 @retval EFI_DEVICE_ERROR If requested range could not be flushed.
117 CpuFlushCpuDataCache (
118 IN EFI_CPU_ARCH_PROTOCOL
*This
,
119 IN EFI_PHYSICAL_ADDRESS Start
,
121 IN EFI_CPU_FLUSH_TYPE FlushType
124 if (FlushType
== EfiCpuFlushTypeWriteBackInvalidate
) {
127 } else if (FlushType
== EfiCpuFlushTypeInvalidate
) {
131 return EFI_UNSUPPORTED
;
137 Enables CPU interrupts.
139 @param This Protocol instance structure
141 @retval EFI_SUCCESS If interrupts were enabled in the CPU
142 @retval EFI_DEVICE_ERROR If interrupts could not be enabled on the CPU.
148 IN EFI_CPU_ARCH_PROTOCOL
*This
153 InterruptState
= TRUE
;
159 Disables CPU interrupts.
161 @param This Protocol instance structure
163 @retval EFI_SUCCESS If interrupts were disabled in the CPU.
164 @retval EFI_DEVICE_ERROR If interrupts could not be disabled on the CPU.
169 CpuDisableInterrupt (
170 IN EFI_CPU_ARCH_PROTOCOL
*This
173 DisableInterrupts ();
175 InterruptState
= FALSE
;
181 Return the state of interrupts.
183 @param This Protocol instance structure
184 @param State Pointer to the CPU's current interrupt state
186 @retval EFI_SUCCESS If interrupts were disabled in the CPU.
187 @retval EFI_INVALID_PARAMETER State is NULL.
192 CpuGetInterruptState (
193 IN EFI_CPU_ARCH_PROTOCOL
*This
,
198 return EFI_INVALID_PARAMETER
;
201 *State
= InterruptState
;
207 Generates an INIT to the CPU.
209 @param This Protocol instance structure
210 @param InitType Type of CPU INIT to perform
212 @retval EFI_SUCCESS If CPU INIT occurred. This value should never be
214 @retval EFI_DEVICE_ERROR If CPU INIT failed.
215 @retval EFI_UNSUPPORTED Requested type of CPU INIT not supported.
221 IN EFI_CPU_ARCH_PROTOCOL
*This
,
222 IN EFI_CPU_INIT_TYPE InitType
225 return EFI_UNSUPPORTED
;
230 Registers a function to be called from the CPU interrupt handler.
232 @param This Protocol instance structure
233 @param InterruptType Defines which interrupt to hook. IA-32
234 valid range is 0x00 through 0xFF
235 @param InterruptHandler A pointer to a function of type
236 EFI_CPU_INTERRUPT_HANDLER that is called
237 when a processor interrupt occurs. A null
238 pointer is an error condition.
240 @retval EFI_SUCCESS If handler installed or uninstalled.
241 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler
242 for InterruptType was previously installed.
243 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for
244 InterruptType was not previously installed.
245 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType
251 CpuRegisterInterruptHandler (
252 IN EFI_CPU_ARCH_PROTOCOL
*This
,
253 IN EFI_EXCEPTION_TYPE InterruptType
,
254 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
257 return RegisterCpuInterruptHandler (InterruptType
, InterruptHandler
);
262 Returns a timer value from one of the CPU's internal timers. There is no
263 inherent time interval between ticks but is a function of the CPU frequency.
265 @param This - Protocol instance structure.
266 @param TimerIndex - Specifies which CPU timer is requested.
267 @param TimerValue - Pointer to the returned timer value.
268 @param TimerPeriod - A pointer to the amount of time that passes
269 in femtoseconds (10-15) for each increment
270 of TimerValue. If TimerValue does not
271 increment at a predictable rate, then 0 is
272 returned. The amount of time that has
273 passed between two calls to GetTimerValue()
274 can be calculated with the formula
275 (TimerValue2 - TimerValue1) * TimerPeriod.
276 This parameter is optional and may be NULL.
278 @retval EFI_SUCCESS - If the CPU timer count was returned.
279 @retval EFI_UNSUPPORTED - If the CPU does not have any readable timers.
280 @retval EFI_DEVICE_ERROR - If an error occurred while reading the timer.
281 @retval EFI_INVALID_PARAMETER - TimerIndex is not valid or TimerValue is NULL.
287 IN EFI_CPU_ARCH_PROTOCOL
*This
,
288 IN UINT32 TimerIndex
,
289 OUT UINT64
*TimerValue
,
290 OUT UINT64
*TimerPeriod OPTIONAL
296 if (TimerValue
== NULL
) {
297 return EFI_INVALID_PARAMETER
;
300 if (TimerIndex
!= 0) {
301 return EFI_INVALID_PARAMETER
;
304 *TimerValue
= AsmReadTsc ();
306 if (TimerPeriod
!= NULL
) {
307 if (mTimerPeriod
== 0) {
309 // Read time stamp counter before and after delay of 100 microseconds
311 BeginValue
= AsmReadTsc ();
312 MicroSecondDelay (100);
313 EndValue
= AsmReadTsc ();
315 // Calculate the actual frequency
317 mTimerPeriod
= DivU64x64Remainder (
322 EndValue
- BeginValue
,
326 *TimerPeriod
= mTimerPeriod
;
333 A minimal wrapper function that allows MtrrSetAllMtrrs() to be passed to
334 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() as Procedure.
336 @param[in] Buffer Pointer to an MTRR_SETTINGS object, to be passed to
345 MtrrSetAllMtrrs (Buffer
);
349 Implementation of SetMemoryAttributes() service of CPU Architecture Protocol.
351 This function modifies the attributes for the memory region specified by BaseAddress and
352 Length from their current attributes to the attributes specified by Attributes.
354 @param This The EFI_CPU_ARCH_PROTOCOL instance.
355 @param BaseAddress The physical address that is the start address of a memory region.
356 @param Length The size in bytes of the memory region.
357 @param Attributes The bit mask of attributes to set for the memory region.
359 @retval EFI_SUCCESS The attributes were set for the memory region.
360 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by
361 BaseAddress and Length cannot be modified.
362 @retval EFI_INVALID_PARAMETER Length is zero.
363 Attributes specified an illegal combination of attributes that
364 cannot be set together.
365 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
366 the memory resource range.
367 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
368 resource range specified by BaseAddress and Length.
369 The bit mask of attributes is not support for the memory resource
370 range specified by BaseAddress and Length.
375 CpuSetMemoryAttributes (
376 IN EFI_CPU_ARCH_PROTOCOL
*This
,
377 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
382 RETURN_STATUS Status
;
383 MTRR_MEMORY_CACHE_TYPE CacheType
;
385 EFI_MP_SERVICES_PROTOCOL
*MpService
;
386 MTRR_SETTINGS MtrrSettings
;
387 UINT64 CacheAttributes
;
388 UINT64 MemoryAttributes
;
389 MTRR_MEMORY_CACHE_TYPE CurrentCacheType
;
392 // If this function is called because GCD SetMemorySpaceAttributes () is called
393 // by RefreshGcdMemoryAttributes (), then we are just synchronizing GCD memory
394 // map with MTRR values. So there is no need to modify MTRRs, just return immediately
395 // to avoid unnecessary computing.
397 if (mIsFlushingGCD
) {
398 DEBUG((DEBUG_VERBOSE
, " Flushing GCD\n"));
403 // During memory attributes updating, new pages may be allocated to setup
404 // smaller granularity of page table. Page allocation action might then cause
405 // another calling of CpuSetMemoryAttributes() recursively, due to memory
406 // protection policy configured (such as PcdDxeNxMemoryProtectionPolicy).
407 // Since this driver will always protect memory used as page table by itself,
408 // there's no need to apply protection policy requested from memory service.
409 // So it's safe to just return EFI_SUCCESS if this time of calling is caused
410 // by page table memory allocation.
412 if (mIsAllocatingPageTable
) {
413 DEBUG((DEBUG_VERBOSE
, " Allocating page table memory\n"));
417 CacheAttributes
= Attributes
& EFI_CACHE_ATTRIBUTE_MASK
;
418 MemoryAttributes
= Attributes
& EFI_MEMORY_ATTRIBUTE_MASK
;
420 if (Attributes
!= (CacheAttributes
| MemoryAttributes
)) {
421 return EFI_INVALID_PARAMETER
;
424 if (CacheAttributes
!= 0) {
425 if (!IsMtrrSupported ()) {
426 return EFI_UNSUPPORTED
;
429 switch (CacheAttributes
) {
431 CacheType
= CacheUncacheable
;
435 CacheType
= CacheWriteCombining
;
439 CacheType
= CacheWriteThrough
;
443 CacheType
= CacheWriteProtected
;
447 CacheType
= CacheWriteBack
;
451 return EFI_INVALID_PARAMETER
;
453 CurrentCacheType
= MtrrGetMemoryAttribute(BaseAddress
);
454 if (CurrentCacheType
!= CacheType
) {
456 // call MTRR library function
458 Status
= MtrrSetMemoryAttribute (
464 if (!RETURN_ERROR (Status
)) {
465 MpStatus
= gBS
->LocateProtocol (
466 &gEfiMpServiceProtocolGuid
,
471 // Synchronize the update with all APs
473 if (!EFI_ERROR (MpStatus
)) {
474 MtrrGetAllMtrrs (&MtrrSettings
);
475 MpStatus
= MpService
->StartupAllAPs (
477 SetMtrrsFromBuffer
, // Procedure
478 FALSE
, // SingleThread
480 0, // TimeoutInMicrosecsond
481 &MtrrSettings
, // ProcedureArgument
482 NULL
// FailedCpuList
484 ASSERT (MpStatus
== EFI_SUCCESS
|| MpStatus
== EFI_NOT_STARTED
);
487 if (EFI_ERROR(Status
)) {
494 // Set memory attribute by page table
496 return AssignMemoryPageAttributes (NULL
, BaseAddress
, Length
, MemoryAttributes
, NULL
);
500 Initializes the valid bits mask and valid address mask for MTRRs.
502 This function initializes the valid bits mask and valid address mask for MTRRs.
511 UINT8 PhysicalAddressBits
;
513 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
515 if (RegEax
>= 0x80000008) {
516 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
518 PhysicalAddressBits
= (UINT8
) RegEax
;
520 PhysicalAddressBits
= 36;
523 mValidMtrrBitsMask
= LShiftU64 (1, PhysicalAddressBits
) - 1;
524 mValidMtrrAddressMask
= mValidMtrrBitsMask
& 0xfffffffffffff000ULL
;
528 Gets GCD Mem Space type from MTRR Type.
530 This function gets GCD Mem Space type from MTRR Type.
532 @param MtrrAttributes MTRR memory type
534 @return GCD Mem Space type
538 GetMemorySpaceAttributeFromMtrrType (
539 IN UINT8 MtrrAttributes
542 switch (MtrrAttributes
) {
543 case MTRR_CACHE_UNCACHEABLE
:
544 return EFI_MEMORY_UC
;
545 case MTRR_CACHE_WRITE_COMBINING
:
546 return EFI_MEMORY_WC
;
547 case MTRR_CACHE_WRITE_THROUGH
:
548 return EFI_MEMORY_WT
;
549 case MTRR_CACHE_WRITE_PROTECTED
:
550 return EFI_MEMORY_WP
;
551 case MTRR_CACHE_WRITE_BACK
:
552 return EFI_MEMORY_WB
;
559 Searches memory descriptors covered by given memory range.
561 This function searches into the Gcd Memory Space for descriptors
562 (from StartIndex to EndIndex) that contains the memory range
563 specified by BaseAddress and Length.
565 @param MemorySpaceMap Gcd Memory Space Map as array.
566 @param NumberOfDescriptors Number of descriptors in map.
567 @param BaseAddress BaseAddress for the requested range.
568 @param Length Length for the requested range.
569 @param StartIndex Start index into the Gcd Memory Space Map.
570 @param EndIndex End index into the Gcd Memory Space Map.
572 @retval EFI_SUCCESS Search successfully.
573 @retval EFI_NOT_FOUND The requested descriptors does not exist.
577 SearchGcdMemorySpaces (
578 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
,
579 IN UINTN NumberOfDescriptors
,
580 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
582 OUT UINTN
*StartIndex
,
590 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
591 if (BaseAddress
>= MemorySpaceMap
[Index
].BaseAddress
&&
592 BaseAddress
< MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) {
595 if (BaseAddress
+ Length
- 1 >= MemorySpaceMap
[Index
].BaseAddress
&&
596 BaseAddress
+ Length
- 1 < MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) {
601 return EFI_NOT_FOUND
;
605 Sets the attributes for a specified range in Gcd Memory Space Map.
607 This function sets the attributes for a specified range in
608 Gcd Memory Space Map.
610 @param MemorySpaceMap Gcd Memory Space Map as array
611 @param NumberOfDescriptors Number of descriptors in map
612 @param BaseAddress BaseAddress for the range
613 @param Length Length for the range
614 @param Attributes Attributes to set
616 @retval EFI_SUCCESS Memory attributes set successfully
617 @retval EFI_NOT_FOUND The specified range does not exist in Gcd Memory Space
621 SetGcdMemorySpaceAttributes (
622 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
,
623 IN UINTN NumberOfDescriptors
,
624 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
633 EFI_PHYSICAL_ADDRESS RegionStart
;
637 // Get all memory descriptors covered by the memory range
639 Status
= SearchGcdMemorySpaces (
647 if (EFI_ERROR (Status
)) {
652 // Go through all related descriptors and set attributes accordingly
654 for (Index
= StartIndex
; Index
<= EndIndex
; Index
++) {
655 if (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeNonExistent
) {
659 // Calculate the start and end address of the overlapping range
661 if (BaseAddress
>= MemorySpaceMap
[Index
].BaseAddress
) {
662 RegionStart
= BaseAddress
;
664 RegionStart
= MemorySpaceMap
[Index
].BaseAddress
;
666 if (BaseAddress
+ Length
- 1 < MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) {
667 RegionLength
= BaseAddress
+ Length
- RegionStart
;
669 RegionLength
= MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
- RegionStart
;
672 // Set memory attributes according to MTRR attribute and the original attribute of descriptor
674 gDS
->SetMemorySpaceAttributes (
677 (MemorySpaceMap
[Index
].Attributes
& ~EFI_CACHE_ATTRIBUTE_MASK
) | (MemorySpaceMap
[Index
].Capabilities
& Attributes
)
686 Refreshes the GCD Memory Space attributes according to MTRRs.
688 This function refreshes the GCD Memory Space attributes according to MTRRs.
692 RefreshMemoryAttributesFromMtrr (
700 EFI_PHYSICAL_ADDRESS BaseAddress
;
703 UINT64 CurrentAttributes
;
705 UINTN NumberOfDescriptors
;
706 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
707 UINT64 DefaultAttributes
;
708 VARIABLE_MTRR VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
709 MTRR_FIXED_SETTINGS MtrrFixedSettings
;
710 UINT32 FirmwareVariableMtrrCount
;
711 UINT8 DefaultMemoryType
;
713 FirmwareVariableMtrrCount
= GetFirmwareVariableMtrrCount ();
714 ASSERT (FirmwareVariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
716 MemorySpaceMap
= NULL
;
719 // Initialize the valid bits mask and valid address mask for MTRRs
721 InitializeMtrrMask ();
724 // Get the memory attribute of variable MTRRs
726 MtrrGetMemoryAttributeInVariableMtrr (
728 mValidMtrrAddressMask
,
733 // Get the memory space map from GCD
735 Status
= gDS
->GetMemorySpaceMap (
736 &NumberOfDescriptors
,
739 ASSERT_EFI_ERROR (Status
);
741 DefaultMemoryType
= (UINT8
) MtrrGetDefaultMemoryType ();
742 DefaultAttributes
= GetMemorySpaceAttributeFromMtrrType (DefaultMemoryType
);
745 // Set default attributes to all spaces.
747 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
748 if (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeNonExistent
) {
751 gDS
->SetMemorySpaceAttributes (
752 MemorySpaceMap
[Index
].BaseAddress
,
753 MemorySpaceMap
[Index
].Length
,
754 (MemorySpaceMap
[Index
].Attributes
& ~EFI_CACHE_ATTRIBUTE_MASK
) |
755 (MemorySpaceMap
[Index
].Capabilities
& DefaultAttributes
)
760 // Go for variable MTRRs with WB attribute
762 for (Index
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
763 if (VariableMtrr
[Index
].Valid
&&
764 VariableMtrr
[Index
].Type
== MTRR_CACHE_WRITE_BACK
) {
765 SetGcdMemorySpaceAttributes (
768 VariableMtrr
[Index
].BaseAddress
,
769 VariableMtrr
[Index
].Length
,
776 // Go for variable MTRRs with the attribute except for WB and UC attributes
778 for (Index
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
779 if (VariableMtrr
[Index
].Valid
&&
780 VariableMtrr
[Index
].Type
!= MTRR_CACHE_WRITE_BACK
&&
781 VariableMtrr
[Index
].Type
!= MTRR_CACHE_UNCACHEABLE
) {
782 Attributes
= GetMemorySpaceAttributeFromMtrrType ((UINT8
) VariableMtrr
[Index
].Type
);
783 SetGcdMemorySpaceAttributes (
786 VariableMtrr
[Index
].BaseAddress
,
787 VariableMtrr
[Index
].Length
,
794 // Go for variable MTRRs with UC attribute
796 for (Index
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
797 if (VariableMtrr
[Index
].Valid
&&
798 VariableMtrr
[Index
].Type
== MTRR_CACHE_UNCACHEABLE
) {
799 SetGcdMemorySpaceAttributes (
802 VariableMtrr
[Index
].BaseAddress
,
803 VariableMtrr
[Index
].Length
,
810 // Go for fixed MTRRs
815 MtrrGetFixedMtrr (&MtrrFixedSettings
);
816 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
817 RegValue
= MtrrFixedSettings
.Mtrr
[Index
];
819 // Check for continuous fixed MTRR sections
821 for (SubIndex
= 0; SubIndex
< 8; SubIndex
++) {
822 MtrrType
= (UINT8
) RShiftU64 (RegValue
, SubIndex
* 8);
823 CurrentAttributes
= GetMemorySpaceAttributeFromMtrrType (MtrrType
);
826 // A new MTRR attribute begins
828 Attributes
= CurrentAttributes
;
831 // If fixed MTRR attribute changed, then set memory attribute for previous attribute
833 if (CurrentAttributes
!= Attributes
) {
834 SetGcdMemorySpaceAttributes (
841 BaseAddress
= mFixedMtrrTable
[Index
].BaseAddress
+ mFixedMtrrTable
[Index
].Length
* SubIndex
;
843 Attributes
= CurrentAttributes
;
846 Length
+= mFixedMtrrTable
[Index
].Length
;
850 // Handle the last fixed MTRR region
852 SetGcdMemorySpaceAttributes (
861 // Free memory space map allocated by GCD service GetMemorySpaceMap ()
863 if (MemorySpaceMap
!= NULL
) {
864 FreePool (MemorySpaceMap
);
869 Check if paging is enabled or not.
872 IsPagingAndPageAddressExtensionsEnabled (
879 Cr0
.UintN
= AsmReadCr0 ();
880 Cr4
.UintN
= AsmReadCr4 ();
882 return ((Cr0
.Bits
.PG
!= 0) && (Cr4
.Bits
.PAE
!= 0));
886 Refreshes the GCD Memory Space attributes according to MTRRs and Paging.
888 This function refreshes the GCD Memory Space attributes according to MTRRs
893 RefreshGcdMemoryAttributes (
897 mIsFlushingGCD
= TRUE
;
899 if (IsMtrrSupported ()) {
900 RefreshMemoryAttributesFromMtrr ();
903 if (IsPagingAndPageAddressExtensionsEnabled ()) {
904 RefreshGcdMemoryAttributesFromPaging ();
907 mIsFlushingGCD
= FALSE
;
911 Initialize Interrupt Descriptor Table for interrupt handling.
915 InitInterruptDescriptorTable (
920 EFI_VECTOR_HANDOFF_INFO
*VectorInfoList
;
921 EFI_VECTOR_HANDOFF_INFO
*VectorInfo
;
924 Status
= EfiGetSystemConfigurationTable (&gEfiVectorHandoffTableGuid
, (VOID
**) &VectorInfoList
);
925 if (Status
== EFI_SUCCESS
&& VectorInfoList
!= NULL
) {
926 VectorInfo
= VectorInfoList
;
928 Status
= InitializeCpuInterruptHandlers (VectorInfo
);
929 ASSERT_EFI_ERROR (Status
);
934 Callback function for idle events.
936 @param Event Event whose notification function is being invoked.
937 @param Context The pointer to the notification function's context,
938 which is implementation-dependent.
943 IdleLoopEventCallback (
952 Ensure the compatibility of a memory space descriptor with the MMIO aperture.
954 The memory space descriptor can come from the GCD memory space map, or it can
955 represent a gap between two neighboring memory space descriptors. In the
956 latter case, the GcdMemoryType field is expected to be
957 EfiGcdMemoryTypeNonExistent.
959 If the memory space descriptor already has type
960 EfiGcdMemoryTypeMemoryMappedIo, and its capabilities are a superset of the
961 required capabilities, then no action is taken -- it is by definition
962 compatible with the aperture.
964 Otherwise, the intersection of the memory space descriptor is calculated with
965 the aperture. If the intersection is the empty set (no overlap), no action is
966 taken; the memory space descriptor is compatible with the aperture.
968 Otherwise, the type of the descriptor is investigated again. If the type is
969 EfiGcdMemoryTypeNonExistent (representing a gap, or a genuine descriptor with
970 such a type), then an attempt is made to add the intersection as MMIO space
971 to the GCD memory space map, with the specified capabilities. This ensures
972 continuity for the aperture, and the descriptor is deemed compatible with the
975 Otherwise, the memory space descriptor is incompatible with the MMIO
978 @param[in] Base Base address of the aperture.
979 @param[in] Length Length of the aperture.
980 @param[in] Capabilities Capabilities required by the aperture.
981 @param[in] Descriptor The descriptor to ensure compatibility with the
984 @retval EFI_SUCCESS The descriptor is compatible. The GCD memory
985 space map may have been updated, for
986 continuity within the aperture.
987 @retval EFI_INVALID_PARAMETER The descriptor is incompatible.
988 @return Error codes from gDS->AddMemorySpace().
991 IntersectMemoryDescriptor (
994 IN UINT64 Capabilities
,
995 IN CONST EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
998 UINT64 IntersectionBase
;
999 UINT64 IntersectionEnd
;
1002 if (Descriptor
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
&&
1003 (Descriptor
->Capabilities
& Capabilities
) == Capabilities
) {
1007 IntersectionBase
= MAX (Base
, Descriptor
->BaseAddress
);
1008 IntersectionEnd
= MIN (Base
+ Length
,
1009 Descriptor
->BaseAddress
+ Descriptor
->Length
);
1010 if (IntersectionBase
>= IntersectionEnd
) {
1012 // The descriptor and the aperture don't overlap.
1017 if (Descriptor
->GcdMemoryType
== EfiGcdMemoryTypeNonExistent
) {
1018 Status
= gDS
->AddMemorySpace (EfiGcdMemoryTypeMemoryMappedIo
,
1019 IntersectionBase
, IntersectionEnd
- IntersectionBase
,
1022 DEBUG ((EFI_ERROR (Status
) ? DEBUG_ERROR
: DEBUG_VERBOSE
,
1023 "%a: %a: add [%Lx, %Lx): %r\n", gEfiCallerBaseName
, __FUNCTION__
,
1024 IntersectionBase
, IntersectionEnd
, Status
));
1028 DEBUG ((DEBUG_ERROR
, "%a: %a: desc [%Lx, %Lx) type %u cap %Lx conflicts "
1029 "with aperture [%Lx, %Lx) cap %Lx\n", gEfiCallerBaseName
, __FUNCTION__
,
1030 Descriptor
->BaseAddress
, Descriptor
->BaseAddress
+ Descriptor
->Length
,
1031 (UINT32
)Descriptor
->GcdMemoryType
, Descriptor
->Capabilities
,
1032 Base
, Base
+ Length
, Capabilities
));
1033 return EFI_INVALID_PARAMETER
;
1037 Add MMIO space to GCD.
1038 The routine checks the GCD database and only adds those which are
1039 not added in the specified range to GCD.
1041 @param Base Base address of the MMIO space.
1042 @param Length Length of the MMIO space.
1043 @param Capabilities Capabilities of the MMIO space.
1045 @retval EFI_SUCCESS The MMIO space was added successfully.
1048 AddMemoryMappedIoSpace (
1051 IN UINT64 Capabilities
1056 UINTN NumberOfDescriptors
;
1057 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
1059 Status
= gDS
->GetMemorySpaceMap (&NumberOfDescriptors
, &MemorySpaceMap
);
1060 if (EFI_ERROR (Status
)) {
1061 DEBUG ((DEBUG_ERROR
, "%a: %a: GetMemorySpaceMap(): %r\n",
1062 gEfiCallerBaseName
, __FUNCTION__
, Status
));
1066 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
1067 Status
= IntersectMemoryDescriptor (Base
, Length
, Capabilities
,
1068 &MemorySpaceMap
[Index
]);
1069 if (EFI_ERROR (Status
)) {
1070 goto FreeMemorySpaceMap
;
1074 DEBUG_CODE_BEGIN ();
1076 // Make sure there are adjacent descriptors covering [Base, Base + Length).
1077 // It is possible that they have not been merged; merging can be prevented
1078 // by allocation and different capabilities.
1081 EFI_STATUS CheckStatus
;
1082 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor
;
1084 for (CheckBase
= Base
;
1085 CheckBase
< Base
+ Length
;
1086 CheckBase
= Descriptor
.BaseAddress
+ Descriptor
.Length
) {
1087 CheckStatus
= gDS
->GetMemorySpaceDescriptor (CheckBase
, &Descriptor
);
1088 ASSERT_EFI_ERROR (CheckStatus
);
1089 ASSERT (Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
);
1090 ASSERT ((Descriptor
.Capabilities
& Capabilities
) == Capabilities
);
1095 FreePool (MemorySpaceMap
);
1101 Add and allocate CPU local APIC memory mapped space.
1103 @param[in]ImageHandle Image handle this driver.
1107 AddLocalApicMemorySpace (
1108 IN EFI_HANDLE ImageHandle
1112 EFI_PHYSICAL_ADDRESS BaseAddress
;
1114 BaseAddress
= (EFI_PHYSICAL_ADDRESS
) GetLocalApicBaseAddress();
1115 Status
= AddMemoryMappedIoSpace (BaseAddress
, SIZE_4KB
, EFI_MEMORY_UC
);
1116 ASSERT_EFI_ERROR (Status
);
1119 // Try to allocate APIC memory mapped space, does not check return
1120 // status because it may be allocated by other driver, or DXE Core if
1121 // this range is built into Memory Allocation HOB.
1123 Status
= gDS
->AllocateMemorySpace (
1124 EfiGcdAllocateAddress
,
1125 EfiGcdMemoryTypeMemoryMappedIo
,
1132 if (EFI_ERROR (Status
)) {
1133 DEBUG ((DEBUG_INFO
, "%a: %a: AllocateMemorySpace() Status - %r\n",
1134 gEfiCallerBaseName
, __FUNCTION__
, Status
));
1139 Initialize the state information for the CPU Architectural Protocol.
1141 @param ImageHandle Image handle this driver.
1142 @param SystemTable Pointer to the System Table.
1144 @retval EFI_SUCCESS Thread can be successfully created
1145 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
1146 @retval EFI_DEVICE_ERROR Cannot create the thread
1152 IN EFI_HANDLE ImageHandle
,
1153 IN EFI_SYSTEM_TABLE
*SystemTable
1157 EFI_EVENT IdleLoopEvent
;
1159 InitializePageTableLib();
1161 InitializeFloatingPointUnits ();
1164 // Make sure interrupts are disabled
1166 DisableInterrupts ();
1171 InitGlobalDescriptorTable ();
1174 // Setup IDT pointer, IDT and interrupt entry points
1176 InitInterruptDescriptorTable ();
1179 // Install CPU Architectural Protocol
1181 Status
= gBS
->InstallMultipleProtocolInterfaces (
1183 &gEfiCpuArchProtocolGuid
, &gCpu
,
1186 ASSERT_EFI_ERROR (Status
);
1189 // Refresh GCD memory space map according to MTRR value.
1191 RefreshGcdMemoryAttributes ();
1194 // Add and allocate local APIC memory mapped space
1196 AddLocalApicMemorySpace (ImageHandle
);
1199 // Setup a callback for idle events
1201 Status
= gBS
->CreateEventEx (
1204 IdleLoopEventCallback
,
1206 &gIdleLoopEventGuid
,
1209 ASSERT_EFI_ERROR (Status
);
1211 InitializeMpSupport ();