4 Copyright (c) 2008 - 2013, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this 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.
21 BOOLEAN InterruptState
= FALSE
;
22 EFI_HANDLE mCpuHandle
= NULL
;
23 BOOLEAN mIsFlushingGCD
;
24 UINT64 mValidMtrrAddressMask
= MTRR_LIB_CACHE_VALID_ADDRESS
;
25 UINT64 mValidMtrrBitsMask
= MTRR_LIB_MSR_VALID_MASK
;
27 FIXED_MTRR mFixedMtrrTable
[] = {
29 MTRR_LIB_IA32_MTRR_FIX64K_00000
,
34 MTRR_LIB_IA32_MTRR_FIX16K_80000
,
39 MTRR_LIB_IA32_MTRR_FIX16K_A0000
,
44 MTRR_LIB_IA32_MTRR_FIX4K_C0000
,
49 MTRR_LIB_IA32_MTRR_FIX4K_C8000
,
54 MTRR_LIB_IA32_MTRR_FIX4K_D0000
,
59 MTRR_LIB_IA32_MTRR_FIX4K_D8000
,
64 MTRR_LIB_IA32_MTRR_FIX4K_E0000
,
69 MTRR_LIB_IA32_MTRR_FIX4K_E8000
,
74 MTRR_LIB_IA32_MTRR_FIX4K_F0000
,
79 MTRR_LIB_IA32_MTRR_FIX4K_F8000
,
86 EFI_CPU_ARCH_PROTOCOL gCpu
= {
92 CpuRegisterInterruptHandler
,
94 CpuSetMemoryAttributes
,
96 4 // DmaBufferAlignment
100 // CPU Arch Protocol Functions
104 Flush CPU data cache. If the instruction cache is fully coherent
105 with all DMA operations then function can just return EFI_SUCCESS.
107 @param This Protocol instance structure
108 @param Start Physical address to start flushing from.
109 @param Length Number of bytes to flush. Round up to chipset
111 @param FlushType Specifies the type of flush operation to perform.
113 @retval EFI_SUCCESS If cache was flushed
114 @retval EFI_UNSUPPORTED If flush type is not supported.
115 @retval EFI_DEVICE_ERROR If requested range could not be flushed.
120 CpuFlushCpuDataCache (
121 IN EFI_CPU_ARCH_PROTOCOL
*This
,
122 IN EFI_PHYSICAL_ADDRESS Start
,
124 IN EFI_CPU_FLUSH_TYPE FlushType
127 if (FlushType
== EfiCpuFlushTypeWriteBackInvalidate
) {
130 } else if (FlushType
== EfiCpuFlushTypeInvalidate
) {
134 return EFI_UNSUPPORTED
;
140 Enables CPU interrupts.
142 @param This Protocol instance structure
144 @retval EFI_SUCCESS If interrupts were enabled in the CPU
145 @retval EFI_DEVICE_ERROR If interrupts could not be enabled on the CPU.
151 IN EFI_CPU_ARCH_PROTOCOL
*This
156 InterruptState
= TRUE
;
162 Disables CPU interrupts.
164 @param This Protocol instance structure
166 @retval EFI_SUCCESS If interrupts were disabled in the CPU.
167 @retval EFI_DEVICE_ERROR If interrupts could not be disabled on the CPU.
172 CpuDisableInterrupt (
173 IN EFI_CPU_ARCH_PROTOCOL
*This
176 DisableInterrupts ();
178 InterruptState
= FALSE
;
184 Return the state of interrupts.
186 @param This Protocol instance structure
187 @param State Pointer to the CPU's current interrupt state
189 @retval EFI_SUCCESS If interrupts were disabled in the CPU.
190 @retval EFI_INVALID_PARAMETER State is NULL.
195 CpuGetInterruptState (
196 IN EFI_CPU_ARCH_PROTOCOL
*This
,
201 return EFI_INVALID_PARAMETER
;
204 *State
= InterruptState
;
210 Generates an INIT to the CPU.
212 @param This Protocol instance structure
213 @param InitType Type of CPU INIT to perform
215 @retval EFI_SUCCESS If CPU INIT occurred. This value should never be
217 @retval EFI_DEVICE_ERROR If CPU INIT failed.
218 @retval EFI_UNSUPPORTED Requested type of CPU INIT not supported.
224 IN EFI_CPU_ARCH_PROTOCOL
*This
,
225 IN EFI_CPU_INIT_TYPE InitType
228 return EFI_UNSUPPORTED
;
233 Registers a function to be called from the CPU interrupt handler.
235 @param This Protocol instance structure
236 @param InterruptType Defines which interrupt to hook. IA-32
237 valid range is 0x00 through 0xFF
238 @param InterruptHandler A pointer to a function of type
239 EFI_CPU_INTERRUPT_HANDLER that is called
240 when a processor interrupt occurs. A null
241 pointer is an error condition.
243 @retval EFI_SUCCESS If handler installed or uninstalled.
244 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler
245 for InterruptType was previously installed.
246 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for
247 InterruptType was not previously installed.
248 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType
254 CpuRegisterInterruptHandler (
255 IN EFI_CPU_ARCH_PROTOCOL
*This
,
256 IN EFI_EXCEPTION_TYPE InterruptType
,
257 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
260 return RegisterCpuInterruptHandler (InterruptType
, InterruptHandler
);
265 Returns a timer value from one of the CPU's internal timers. There is no
266 inherent time interval between ticks but is a function of the CPU frequency.
268 @param This - Protocol instance structure.
269 @param TimerIndex - Specifies which CPU timer is requested.
270 @param TimerValue - Pointer to the returned timer value.
271 @param TimerPeriod - A pointer to the amount of time that passes
272 in femtoseconds (10-15) for each increment
273 of TimerValue. If TimerValue does not
274 increment at a predictable rate, then 0 is
275 returned. The amount of time that has
276 passed between two calls to GetTimerValue()
277 can be calculated with the formula
278 (TimerValue2 - TimerValue1) * TimerPeriod.
279 This parameter is optional and may be NULL.
281 @retval EFI_SUCCESS - If the CPU timer count was returned.
282 @retval EFI_UNSUPPORTED - If the CPU does not have any readable timers.
283 @retval EFI_DEVICE_ERROR - If an error occurred while reading the timer.
284 @retval EFI_INVALID_PARAMETER - TimerIndex is not valid or TimerValue is NULL.
290 IN EFI_CPU_ARCH_PROTOCOL
*This
,
291 IN UINT32 TimerIndex
,
292 OUT UINT64
*TimerValue
,
293 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
) {
308 // BugBug: Hard coded. Don't know how to do this generically
310 *TimerPeriod
= 1000000000;
318 Implementation of SetMemoryAttributes() service of CPU Architecture Protocol.
320 This function modifies the attributes for the memory region specified by BaseAddress and
321 Length from their current attributes to the attributes specified by Attributes.
323 @param This The EFI_CPU_ARCH_PROTOCOL instance.
324 @param BaseAddress The physical address that is the start address of a memory region.
325 @param Length The size in bytes of the memory region.
326 @param Attributes The bit mask of attributes to set for the memory region.
328 @retval EFI_SUCCESS The attributes were set for the memory region.
329 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by
330 BaseAddress and Length cannot be modified.
331 @retval EFI_INVALID_PARAMETER Length is zero.
332 Attributes specified an illegal combination of attributes that
333 cannot be set together.
334 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
335 the memory resource range.
336 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
337 resource range specified by BaseAddress and Length.
338 The bit mask of attributes is not support for the memory resource
339 range specified by BaseAddress and Length.
344 CpuSetMemoryAttributes (
345 IN EFI_CPU_ARCH_PROTOCOL
*This
,
346 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
351 RETURN_STATUS Status
;
352 MTRR_MEMORY_CACHE_TYPE CacheType
;
354 if (!IsMtrrSupported ()) {
355 return EFI_UNSUPPORTED
;
359 // If this function is called because GCD SetMemorySpaceAttributes () is called
360 // by RefreshGcdMemoryAttributes (), then we are just synchronzing GCD memory
361 // map with MTRR values. So there is no need to modify MTRRs, just return immediately
362 // to avoid unnecessary computing.
364 if (mIsFlushingGCD
) {
365 DEBUG((EFI_D_INFO
, " Flushing GCD\n"));
369 switch (Attributes
) {
371 CacheType
= CacheUncacheable
;
375 CacheType
= CacheWriteCombining
;
379 CacheType
= CacheWriteThrough
;
383 CacheType
= CacheWriteProtected
;
387 CacheType
= CacheWriteBack
;
393 case EFI_MEMORY_RUNTIME
:
394 return EFI_UNSUPPORTED
;
397 return EFI_INVALID_PARAMETER
;
400 // call MTRR libary function
402 Status
= MtrrSetMemoryAttribute (
408 return (EFI_STATUS
) Status
;
412 Initializes the valid bits mask and valid address mask for MTRRs.
414 This function initializes the valid bits mask and valid address mask for MTRRs.
423 UINT8 PhysicalAddressBits
;
425 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
427 if (RegEax
>= 0x80000008) {
428 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
430 PhysicalAddressBits
= (UINT8
) RegEax
;
432 mValidMtrrBitsMask
= LShiftU64 (1, PhysicalAddressBits
) - 1;
433 mValidMtrrAddressMask
= mValidMtrrBitsMask
& 0xfffffffffffff000ULL
;
435 mValidMtrrBitsMask
= MTRR_LIB_MSR_VALID_MASK
;
436 mValidMtrrAddressMask
= MTRR_LIB_CACHE_VALID_ADDRESS
;
441 Gets GCD Mem Space type from MTRR Type.
443 This function gets GCD Mem Space type from MTRR Type.
445 @param MtrrAttributes MTRR memory type
447 @return GCD Mem Space type
451 GetMemorySpaceAttributeFromMtrrType (
452 IN UINT8 MtrrAttributes
455 switch (MtrrAttributes
) {
456 case MTRR_CACHE_UNCACHEABLE
:
457 return EFI_MEMORY_UC
;
458 case MTRR_CACHE_WRITE_COMBINING
:
459 return EFI_MEMORY_WC
;
460 case MTRR_CACHE_WRITE_THROUGH
:
461 return EFI_MEMORY_WT
;
462 case MTRR_CACHE_WRITE_PROTECTED
:
463 return EFI_MEMORY_WP
;
464 case MTRR_CACHE_WRITE_BACK
:
465 return EFI_MEMORY_WB
;
472 Searches memory descriptors covered by given memory range.
474 This function searches into the Gcd Memory Space for descriptors
475 (from StartIndex to EndIndex) that contains the memory range
476 specified by BaseAddress and Length.
478 @param MemorySpaceMap Gcd Memory Space Map as array.
479 @param NumberOfDescriptors Number of descriptors in map.
480 @param BaseAddress BaseAddress for the requested range.
481 @param Length Length for the requested range.
482 @param StartIndex Start index into the Gcd Memory Space Map.
483 @param EndIndex End index into the Gcd Memory Space Map.
485 @retval EFI_SUCCESS Search successfully.
486 @retval EFI_NOT_FOUND The requested descriptors does not exist.
490 SearchGcdMemorySpaces (
491 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
,
492 IN UINTN NumberOfDescriptors
,
493 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
495 OUT UINTN
*StartIndex
,
503 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
504 if (BaseAddress
>= MemorySpaceMap
[Index
].BaseAddress
&&
505 BaseAddress
< MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) {
508 if (BaseAddress
+ Length
- 1 >= MemorySpaceMap
[Index
].BaseAddress
&&
509 BaseAddress
+ Length
- 1 < MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) {
514 return EFI_NOT_FOUND
;
518 Sets the attributes for a specified range in Gcd Memory Space Map.
520 This function sets the attributes for a specified range in
521 Gcd Memory Space Map.
523 @param MemorySpaceMap Gcd Memory Space Map as array
524 @param NumberOfDescriptors Number of descriptors in map
525 @param BaseAddress BaseAddress for the range
526 @param Length Length for the range
527 @param Attributes Attributes to set
529 @retval EFI_SUCCESS Memory attributes set successfully
530 @retval EFI_NOT_FOUND The specified range does not exist in Gcd Memory Space
534 SetGcdMemorySpaceAttributes (
535 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
,
536 IN UINTN NumberOfDescriptors
,
537 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
546 EFI_PHYSICAL_ADDRESS RegionStart
;
550 // Get all memory descriptors covered by the memory range
552 Status
= SearchGcdMemorySpaces (
560 if (EFI_ERROR (Status
)) {
565 // Go through all related descriptors and set attributes accordingly
567 for (Index
= StartIndex
; Index
<= EndIndex
; Index
++) {
568 if (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeNonExistent
) {
572 // Calculate the start and end address of the overlapping range
574 if (BaseAddress
>= MemorySpaceMap
[Index
].BaseAddress
) {
575 RegionStart
= BaseAddress
;
577 RegionStart
= MemorySpaceMap
[Index
].BaseAddress
;
579 if (BaseAddress
+ Length
- 1 < MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) {
580 RegionLength
= BaseAddress
+ Length
- RegionStart
;
582 RegionLength
= MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
- RegionStart
;
585 // Set memory attributes according to MTRR attribute and the original attribute of descriptor
587 gDS
->SetMemorySpaceAttributes (
590 (MemorySpaceMap
[Index
].Attributes
& ~EFI_MEMORY_CACHETYPE_MASK
) | (MemorySpaceMap
[Index
].Capabilities
& Attributes
)
599 Refreshes the GCD Memory Space attributes according to MTRRs.
601 This function refreshes the GCD Memory Space attributes according to MTRRs.
605 RefreshGcdMemoryAttributes (
613 EFI_PHYSICAL_ADDRESS BaseAddress
;
616 UINT64 CurrentAttributes
;
618 UINTN NumberOfDescriptors
;
619 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
620 UINT64 DefaultAttributes
;
621 VARIABLE_MTRR VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
622 MTRR_FIXED_SETTINGS MtrrFixedSettings
;
623 UINT32 FirmwareVariableMtrrCount
;
624 UINT8 DefaultMemoryType
;
626 if (!IsMtrrSupported ()) {
630 FirmwareVariableMtrrCount
= GetFirmwareVariableMtrrCount ();
631 ASSERT (FirmwareVariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
633 mIsFlushingGCD
= TRUE
;
634 MemorySpaceMap
= NULL
;
637 // Initialize the valid bits mask and valid address mask for MTRRs
639 InitializeMtrrMask ();
642 // Get the memory attribute of variable MTRRs
644 MtrrGetMemoryAttributeInVariableMtrr (
646 mValidMtrrAddressMask
,
651 // Get the memory space map from GCD
653 Status
= gDS
->GetMemorySpaceMap (
654 &NumberOfDescriptors
,
657 ASSERT_EFI_ERROR (Status
);
659 DefaultMemoryType
= (UINT8
) MtrrGetDefaultMemoryType ();
660 DefaultAttributes
= GetMemorySpaceAttributeFromMtrrType (DefaultMemoryType
);
663 // Set default attributes to all spaces.
665 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
666 if (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeNonExistent
) {
669 gDS
->SetMemorySpaceAttributes (
670 MemorySpaceMap
[Index
].BaseAddress
,
671 MemorySpaceMap
[Index
].Length
,
672 (MemorySpaceMap
[Index
].Attributes
& ~EFI_MEMORY_CACHETYPE_MASK
) |
673 (MemorySpaceMap
[Index
].Capabilities
& DefaultAttributes
)
678 // Go for variable MTRRs with WB attribute
680 for (Index
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
681 if (VariableMtrr
[Index
].Valid
&&
682 VariableMtrr
[Index
].Type
== MTRR_CACHE_WRITE_BACK
) {
683 SetGcdMemorySpaceAttributes (
686 VariableMtrr
[Index
].BaseAddress
,
687 VariableMtrr
[Index
].Length
,
694 // Go for variable MTRRs with the attribute except for WB and UC attributes
696 for (Index
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
697 if (VariableMtrr
[Index
].Valid
&&
698 VariableMtrr
[Index
].Type
!= MTRR_CACHE_WRITE_BACK
&&
699 VariableMtrr
[Index
].Type
!= MTRR_CACHE_UNCACHEABLE
) {
700 Attributes
= GetMemorySpaceAttributeFromMtrrType ((UINT8
) VariableMtrr
[Index
].Type
);
701 SetGcdMemorySpaceAttributes (
704 VariableMtrr
[Index
].BaseAddress
,
705 VariableMtrr
[Index
].Length
,
712 // Go for variable MTRRs with UC attribute
714 for (Index
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
715 if (VariableMtrr
[Index
].Valid
&&
716 VariableMtrr
[Index
].Type
== MTRR_CACHE_UNCACHEABLE
) {
717 SetGcdMemorySpaceAttributes (
720 VariableMtrr
[Index
].BaseAddress
,
721 VariableMtrr
[Index
].Length
,
728 // Go for fixed MTRRs
733 MtrrGetFixedMtrr (&MtrrFixedSettings
);
734 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
735 RegValue
= MtrrFixedSettings
.Mtrr
[Index
];
737 // Check for continuous fixed MTRR sections
739 for (SubIndex
= 0; SubIndex
< 8; SubIndex
++) {
740 MtrrType
= (UINT8
) RShiftU64 (RegValue
, SubIndex
* 8);
741 CurrentAttributes
= GetMemorySpaceAttributeFromMtrrType (MtrrType
);
744 // A new MTRR attribute begins
746 Attributes
= CurrentAttributes
;
749 // If fixed MTRR attribute changed, then set memory attribute for previous atrribute
751 if (CurrentAttributes
!= Attributes
) {
752 SetGcdMemorySpaceAttributes (
759 BaseAddress
= mFixedMtrrTable
[Index
].BaseAddress
+ mFixedMtrrTable
[Index
].Length
* SubIndex
;
761 Attributes
= CurrentAttributes
;
764 Length
+= mFixedMtrrTable
[Index
].Length
;
768 // Handle the last fixed MTRR region
770 SetGcdMemorySpaceAttributes (
779 // Free memory space map allocated by GCD service GetMemorySpaceMap ()
781 if (MemorySpaceMap
!= NULL
) {
782 FreePool (MemorySpaceMap
);
785 mIsFlushingGCD
= FALSE
;
789 Initialize Interrupt Descriptor Table for interrupt handling.
793 InitInterruptDescriptorTable (
798 EFI_VECTOR_HANDOFF_INFO
*VectorInfoList
;
799 EFI_VECTOR_HANDOFF_INFO
*VectorInfo
;
802 Status
= EfiGetSystemConfigurationTable (&gEfiVectorHandoffTableGuid
, (VOID
**) &VectorInfoList
);
803 if (Status
== EFI_SUCCESS
&& VectorInfoList
!= NULL
) {
804 VectorInfo
= VectorInfoList
;
806 Status
= InitializeCpuInterruptHandlers (VectorInfo
);
807 ASSERT_EFI_ERROR (Status
);
812 Callback function for idle events.
814 @param Event Event whose notification function is being invoked.
815 @param Context The pointer to the notification function's context,
816 which is implementation-dependent.
821 IdleLoopEventCallback (
831 Initialize the state information for the CPU Architectural Protocol.
833 @param ImageHandle Image handle this driver.
834 @param SystemTable Pointer to the System Table.
836 @retval EFI_SUCCESS Thread can be successfully created
837 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
838 @retval EFI_DEVICE_ERROR Cannot create the thread
844 IN EFI_HANDLE ImageHandle
,
845 IN EFI_SYSTEM_TABLE
*SystemTable
849 EFI_EVENT IdleLoopEvent
;
851 InitializeFloatingPointUnits ();
854 // Make sure interrupts are disabled
856 DisableInterrupts ();
861 InitGlobalDescriptorTable ();
864 // Setup IDT pointer, IDT and interrupt entry points
866 InitInterruptDescriptorTable ();
869 // Enable the local APIC for Virtual Wire Mode.
871 ProgramVirtualWireMode ();
874 // Install CPU Architectural Protocol
876 Status
= gBS
->InstallMultipleProtocolInterfaces (
878 &gEfiCpuArchProtocolGuid
, &gCpu
,
881 ASSERT_EFI_ERROR (Status
);
884 // Refresh GCD memory space map according to MTRR value.
886 RefreshGcdMemoryAttributes ();
889 // Setup a callback for idle events
891 Status
= gBS
->CreateEventEx (
894 IdleLoopEventCallback
,
899 ASSERT_EFI_ERROR (Status
);
901 InitializeMpSupport ();