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.
20 BOOLEAN InterruptState
= FALSE
;
21 EFI_HANDLE mCpuHandle
= NULL
;
22 BOOLEAN mIsFlushingGCD
;
23 UINT64 mValidMtrrAddressMask
= MTRR_LIB_CACHE_VALID_ADDRESS
;
24 UINT64 mValidMtrrBitsMask
= MTRR_LIB_MSR_VALID_MASK
;
26 FIXED_MTRR mFixedMtrrTable
[] = {
28 MTRR_LIB_IA32_MTRR_FIX64K_00000
,
33 MTRR_LIB_IA32_MTRR_FIX16K_80000
,
38 MTRR_LIB_IA32_MTRR_FIX16K_A0000
,
43 MTRR_LIB_IA32_MTRR_FIX4K_C0000
,
48 MTRR_LIB_IA32_MTRR_FIX4K_C8000
,
53 MTRR_LIB_IA32_MTRR_FIX4K_D0000
,
58 MTRR_LIB_IA32_MTRR_FIX4K_D8000
,
63 MTRR_LIB_IA32_MTRR_FIX4K_E0000
,
68 MTRR_LIB_IA32_MTRR_FIX4K_E8000
,
73 MTRR_LIB_IA32_MTRR_FIX4K_F0000
,
78 MTRR_LIB_IA32_MTRR_FIX4K_F8000
,
85 EFI_CPU_ARCH_PROTOCOL gCpu
= {
91 CpuRegisterInterruptHandler
,
93 CpuSetMemoryAttributes
,
95 4 // DmaBufferAlignment
99 // CPU Arch Protocol Functions
103 Flush CPU data cache. If the instruction cache is fully coherent
104 with all DMA operations then function can just return EFI_SUCCESS.
106 @param This Protocol instance structure
107 @param Start Physical address to start flushing from.
108 @param Length Number of bytes to flush. Round up to chipset
110 @param FlushType Specifies the type of flush operation to perform.
112 @retval EFI_SUCCESS If cache was flushed
113 @retval EFI_UNSUPPORTED If flush type is not supported.
114 @retval EFI_DEVICE_ERROR If requested range could not be flushed.
119 CpuFlushCpuDataCache (
120 IN EFI_CPU_ARCH_PROTOCOL
*This
,
121 IN EFI_PHYSICAL_ADDRESS Start
,
123 IN EFI_CPU_FLUSH_TYPE FlushType
126 if (FlushType
== EfiCpuFlushTypeWriteBackInvalidate
) {
129 } else if (FlushType
== EfiCpuFlushTypeInvalidate
) {
133 return EFI_UNSUPPORTED
;
139 Enables CPU interrupts.
141 @param This Protocol instance structure
143 @retval EFI_SUCCESS If interrupts were enabled in the CPU
144 @retval EFI_DEVICE_ERROR If interrupts could not be enabled on the CPU.
150 IN EFI_CPU_ARCH_PROTOCOL
*This
155 InterruptState
= TRUE
;
161 Disables CPU interrupts.
163 @param This Protocol instance structure
165 @retval EFI_SUCCESS If interrupts were disabled in the CPU.
166 @retval EFI_DEVICE_ERROR If interrupts could not be disabled on the CPU.
171 CpuDisableInterrupt (
172 IN EFI_CPU_ARCH_PROTOCOL
*This
175 DisableInterrupts ();
177 InterruptState
= FALSE
;
183 Return the state of interrupts.
185 @param This Protocol instance structure
186 @param State Pointer to the CPU's current interrupt state
188 @retval EFI_SUCCESS If interrupts were disabled in the CPU.
189 @retval EFI_INVALID_PARAMETER State is NULL.
194 CpuGetInterruptState (
195 IN EFI_CPU_ARCH_PROTOCOL
*This
,
200 return EFI_INVALID_PARAMETER
;
203 *State
= InterruptState
;
209 Generates an INIT to the CPU.
211 @param This Protocol instance structure
212 @param InitType Type of CPU INIT to perform
214 @retval EFI_SUCCESS If CPU INIT occurred. This value should never be
216 @retval EFI_DEVICE_ERROR If CPU INIT failed.
217 @retval EFI_UNSUPPORTED Requested type of CPU INIT not supported.
223 IN EFI_CPU_ARCH_PROTOCOL
*This
,
224 IN EFI_CPU_INIT_TYPE InitType
227 return EFI_UNSUPPORTED
;
232 Registers a function to be called from the CPU interrupt handler.
234 @param This Protocol instance structure
235 @param InterruptType Defines which interrupt to hook. IA-32
236 valid range is 0x00 through 0xFF
237 @param InterruptHandler A pointer to a function of type
238 EFI_CPU_INTERRUPT_HANDLER that is called
239 when a processor interrupt occurs. A null
240 pointer is an error condition.
242 @retval EFI_SUCCESS If handler installed or uninstalled.
243 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler
244 for InterruptType was previously installed.
245 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for
246 InterruptType was not previously installed.
247 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType
253 CpuRegisterInterruptHandler (
254 IN EFI_CPU_ARCH_PROTOCOL
*This
,
255 IN EFI_EXCEPTION_TYPE InterruptType
,
256 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
259 return RegisterCpuInterruptHandler (InterruptType
, InterruptHandler
);
264 Returns a timer value from one of the CPU's internal timers. There is no
265 inherent time interval between ticks but is a function of the CPU frequency.
267 @param This - Protocol instance structure.
268 @param TimerIndex - Specifies which CPU timer is requested.
269 @param TimerValue - Pointer to the returned timer value.
270 @param TimerPeriod - A pointer to the amount of time that passes
271 in femtoseconds (10-15) for each increment
272 of TimerValue. If TimerValue does not
273 increment at a predictable rate, then 0 is
274 returned. The amount of time that has
275 passed between two calls to GetTimerValue()
276 can be calculated with the formula
277 (TimerValue2 - TimerValue1) * TimerPeriod.
278 This parameter is optional and may be NULL.
280 @retval EFI_SUCCESS - If the CPU timer count was returned.
281 @retval EFI_UNSUPPORTED - If the CPU does not have any readable timers.
282 @retval EFI_DEVICE_ERROR - If an error occurred while reading the timer.
283 @retval EFI_INVALID_PARAMETER - TimerIndex is not valid or TimerValue is NULL.
289 IN EFI_CPU_ARCH_PROTOCOL
*This
,
290 IN UINT32 TimerIndex
,
291 OUT UINT64
*TimerValue
,
292 OUT UINT64
*TimerPeriod OPTIONAL
295 if (TimerValue
== NULL
) {
296 return EFI_INVALID_PARAMETER
;
299 if (TimerIndex
!= 0) {
300 return EFI_INVALID_PARAMETER
;
303 *TimerValue
= AsmReadTsc ();
305 if (TimerPeriod
!= NULL
) {
307 // BugBug: Hard coded. Don't know how to do this generically
309 *TimerPeriod
= 1000000000;
317 Implementation of SetMemoryAttributes() service of CPU Architecture Protocol.
319 This function modifies the attributes for the memory region specified by BaseAddress and
320 Length from their current attributes to the attributes specified by Attributes.
322 @param This The EFI_CPU_ARCH_PROTOCOL instance.
323 @param BaseAddress The physical address that is the start address of a memory region.
324 @param Length The size in bytes of the memory region.
325 @param Attributes The bit mask of attributes to set for the memory region.
327 @retval EFI_SUCCESS The attributes were set for the memory region.
328 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by
329 BaseAddress and Length cannot be modified.
330 @retval EFI_INVALID_PARAMETER Length is zero.
331 Attributes specified an illegal combination of attributes that
332 cannot be set together.
333 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
334 the memory resource range.
335 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
336 resource range specified by BaseAddress and Length.
337 The bit mask of attributes is not support for the memory resource
338 range specified by BaseAddress and Length.
343 CpuSetMemoryAttributes (
344 IN EFI_CPU_ARCH_PROTOCOL
*This
,
345 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
350 RETURN_STATUS Status
;
351 MTRR_MEMORY_CACHE_TYPE CacheType
;
353 if (!IsMtrrSupported ()) {
354 return EFI_UNSUPPORTED
;
358 // If this function is called because GCD SetMemorySpaceAttributes () is called
359 // by RefreshGcdMemoryAttributes (), then we are just synchronzing GCD memory
360 // map with MTRR values. So there is no need to modify MTRRs, just return immediately
361 // to avoid unnecessary computing.
363 if (mIsFlushingGCD
) {
364 DEBUG((EFI_D_INFO
, " Flushing GCD\n"));
368 switch (Attributes
) {
370 CacheType
= CacheUncacheable
;
374 CacheType
= CacheWriteCombining
;
378 CacheType
= CacheWriteThrough
;
382 CacheType
= CacheWriteProtected
;
386 CacheType
= CacheWriteBack
;
392 case EFI_MEMORY_RUNTIME
:
393 return EFI_UNSUPPORTED
;
396 return EFI_INVALID_PARAMETER
;
399 // call MTRR libary function
401 Status
= MtrrSetMemoryAttribute (
407 return (EFI_STATUS
) Status
;
411 Initializes the valid bits mask and valid address mask for MTRRs.
413 This function initializes the valid bits mask and valid address mask for MTRRs.
422 UINT8 PhysicalAddressBits
;
424 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
426 if (RegEax
>= 0x80000008) {
427 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
429 PhysicalAddressBits
= (UINT8
) RegEax
;
431 mValidMtrrBitsMask
= LShiftU64 (1, PhysicalAddressBits
) - 1;
432 mValidMtrrAddressMask
= mValidMtrrBitsMask
& 0xfffffffffffff000ULL
;
434 mValidMtrrBitsMask
= MTRR_LIB_MSR_VALID_MASK
;
435 mValidMtrrAddressMask
= MTRR_LIB_CACHE_VALID_ADDRESS
;
440 Gets GCD Mem Space type from MTRR Type.
442 This function gets GCD Mem Space type from MTRR Type.
444 @param MtrrAttributes MTRR memory type
446 @return GCD Mem Space type
450 GetMemorySpaceAttributeFromMtrrType (
451 IN UINT8 MtrrAttributes
454 switch (MtrrAttributes
) {
455 case MTRR_CACHE_UNCACHEABLE
:
456 return EFI_MEMORY_UC
;
457 case MTRR_CACHE_WRITE_COMBINING
:
458 return EFI_MEMORY_WC
;
459 case MTRR_CACHE_WRITE_THROUGH
:
460 return EFI_MEMORY_WT
;
461 case MTRR_CACHE_WRITE_PROTECTED
:
462 return EFI_MEMORY_WP
;
463 case MTRR_CACHE_WRITE_BACK
:
464 return EFI_MEMORY_WB
;
471 Searches memory descriptors covered by given memory range.
473 This function searches into the Gcd Memory Space for descriptors
474 (from StartIndex to EndIndex) that contains the memory range
475 specified by BaseAddress and Length.
477 @param MemorySpaceMap Gcd Memory Space Map as array.
478 @param NumberOfDescriptors Number of descriptors in map.
479 @param BaseAddress BaseAddress for the requested range.
480 @param Length Length for the requested range.
481 @param StartIndex Start index into the Gcd Memory Space Map.
482 @param EndIndex End index into the Gcd Memory Space Map.
484 @retval EFI_SUCCESS Search successfully.
485 @retval EFI_NOT_FOUND The requested descriptors does not exist.
489 SearchGcdMemorySpaces (
490 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
,
491 IN UINTN NumberOfDescriptors
,
492 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
494 OUT UINTN
*StartIndex
,
502 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
503 if (BaseAddress
>= MemorySpaceMap
[Index
].BaseAddress
&&
504 BaseAddress
< MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) {
507 if (BaseAddress
+ Length
- 1 >= MemorySpaceMap
[Index
].BaseAddress
&&
508 BaseAddress
+ Length
- 1 < MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) {
513 return EFI_NOT_FOUND
;
517 Sets the attributes for a specified range in Gcd Memory Space Map.
519 This function sets the attributes for a specified range in
520 Gcd Memory Space Map.
522 @param MemorySpaceMap Gcd Memory Space Map as array
523 @param NumberOfDescriptors Number of descriptors in map
524 @param BaseAddress BaseAddress for the range
525 @param Length Length for the range
526 @param Attributes Attributes to set
528 @retval EFI_SUCCESS Memory attributes set successfully
529 @retval EFI_NOT_FOUND The specified range does not exist in Gcd Memory Space
533 SetGcdMemorySpaceAttributes (
534 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
,
535 IN UINTN NumberOfDescriptors
,
536 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
545 EFI_PHYSICAL_ADDRESS RegionStart
;
549 // Get all memory descriptors covered by the memory range
551 Status
= SearchGcdMemorySpaces (
559 if (EFI_ERROR (Status
)) {
564 // Go through all related descriptors and set attributes accordingly
566 for (Index
= StartIndex
; Index
<= EndIndex
; Index
++) {
567 if (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeNonExistent
) {
571 // Calculate the start and end address of the overlapping range
573 if (BaseAddress
>= MemorySpaceMap
[Index
].BaseAddress
) {
574 RegionStart
= BaseAddress
;
576 RegionStart
= MemorySpaceMap
[Index
].BaseAddress
;
578 if (BaseAddress
+ Length
- 1 < MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) {
579 RegionLength
= BaseAddress
+ Length
- RegionStart
;
581 RegionLength
= MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
- RegionStart
;
584 // Set memory attributes according to MTRR attribute and the original attribute of descriptor
586 gDS
->SetMemorySpaceAttributes (
589 (MemorySpaceMap
[Index
].Attributes
& ~EFI_MEMORY_CACHETYPE_MASK
) | (MemorySpaceMap
[Index
].Capabilities
& Attributes
)
598 Refreshes the GCD Memory Space attributes according to MTRRs.
600 This function refreshes the GCD Memory Space attributes according to MTRRs.
604 RefreshGcdMemoryAttributes (
612 EFI_PHYSICAL_ADDRESS BaseAddress
;
615 UINT64 CurrentAttributes
;
617 UINTN NumberOfDescriptors
;
618 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
619 UINT64 DefaultAttributes
;
620 VARIABLE_MTRR VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
621 MTRR_FIXED_SETTINGS MtrrFixedSettings
;
622 UINT32 FirmwareVariableMtrrCount
;
623 UINT8 DefaultMemoryType
;
625 if (!IsMtrrSupported ()) {
629 FirmwareVariableMtrrCount
= GetFirmwareVariableMtrrCount ();
630 ASSERT (FirmwareVariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
632 mIsFlushingGCD
= TRUE
;
633 MemorySpaceMap
= NULL
;
636 // Initialize the valid bits mask and valid address mask for MTRRs
638 InitializeMtrrMask ();
641 // Get the memory attribute of variable MTRRs
643 MtrrGetMemoryAttributeInVariableMtrr (
645 mValidMtrrAddressMask
,
650 // Get the memory space map from GCD
652 Status
= gDS
->GetMemorySpaceMap (
653 &NumberOfDescriptors
,
656 ASSERT_EFI_ERROR (Status
);
658 DefaultMemoryType
= (UINT8
) MtrrGetDefaultMemoryType ();
659 DefaultAttributes
= GetMemorySpaceAttributeFromMtrrType (DefaultMemoryType
);
662 // Set default attributes to all spaces.
664 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
665 if (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeNonExistent
) {
668 gDS
->SetMemorySpaceAttributes (
669 MemorySpaceMap
[Index
].BaseAddress
,
670 MemorySpaceMap
[Index
].Length
,
671 (MemorySpaceMap
[Index
].Attributes
& ~EFI_MEMORY_CACHETYPE_MASK
) |
672 (MemorySpaceMap
[Index
].Capabilities
& DefaultAttributes
)
677 // Go for variable MTRRs with WB attribute
679 for (Index
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
680 if (VariableMtrr
[Index
].Valid
&&
681 VariableMtrr
[Index
].Type
== MTRR_CACHE_WRITE_BACK
) {
682 SetGcdMemorySpaceAttributes (
685 VariableMtrr
[Index
].BaseAddress
,
686 VariableMtrr
[Index
].Length
,
693 // Go for variable MTRRs with the attribute except for WB and UC attributes
695 for (Index
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
696 if (VariableMtrr
[Index
].Valid
&&
697 VariableMtrr
[Index
].Type
!= MTRR_CACHE_WRITE_BACK
&&
698 VariableMtrr
[Index
].Type
!= MTRR_CACHE_UNCACHEABLE
) {
699 Attributes
= GetMemorySpaceAttributeFromMtrrType ((UINT8
) VariableMtrr
[Index
].Type
);
700 SetGcdMemorySpaceAttributes (
703 VariableMtrr
[Index
].BaseAddress
,
704 VariableMtrr
[Index
].Length
,
711 // Go for variable MTRRs with UC attribute
713 for (Index
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
714 if (VariableMtrr
[Index
].Valid
&&
715 VariableMtrr
[Index
].Type
== MTRR_CACHE_UNCACHEABLE
) {
716 SetGcdMemorySpaceAttributes (
719 VariableMtrr
[Index
].BaseAddress
,
720 VariableMtrr
[Index
].Length
,
727 // Go for fixed MTRRs
732 MtrrGetFixedMtrr (&MtrrFixedSettings
);
733 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
734 RegValue
= MtrrFixedSettings
.Mtrr
[Index
];
736 // Check for continuous fixed MTRR sections
738 for (SubIndex
= 0; SubIndex
< 8; SubIndex
++) {
739 MtrrType
= (UINT8
) RShiftU64 (RegValue
, SubIndex
* 8);
740 CurrentAttributes
= GetMemorySpaceAttributeFromMtrrType (MtrrType
);
743 // A new MTRR attribute begins
745 Attributes
= CurrentAttributes
;
748 // If fixed MTRR attribute changed, then set memory attribute for previous atrribute
750 if (CurrentAttributes
!= Attributes
) {
751 SetGcdMemorySpaceAttributes (
758 BaseAddress
= mFixedMtrrTable
[Index
].BaseAddress
+ mFixedMtrrTable
[Index
].Length
* SubIndex
;
760 Attributes
= CurrentAttributes
;
763 Length
+= mFixedMtrrTable
[Index
].Length
;
767 // Handle the last fixed MTRR region
769 SetGcdMemorySpaceAttributes (
778 // Free memory space map allocated by GCD service GetMemorySpaceMap ()
780 if (MemorySpaceMap
!= NULL
) {
781 FreePool (MemorySpaceMap
);
784 mIsFlushingGCD
= FALSE
;
788 Initialize Interrupt Descriptor Table for interrupt handling.
792 InitInterruptDescriptorTable (
797 EFI_VECTOR_HANDOFF_INFO
*VectorInfoList
;
798 EFI_VECTOR_HANDOFF_INFO
*VectorInfo
;
801 Status
= EfiGetSystemConfigurationTable (&gEfiVectorHandoffTableGuid
, (VOID
**) &VectorInfoList
);
802 if (Status
== EFI_SUCCESS
&& VectorInfoList
!= NULL
) {
803 VectorInfo
= VectorInfoList
;
805 Status
= InitializeCpuInterruptHandlers (VectorInfo
);
806 ASSERT_EFI_ERROR (Status
);
811 Callback function for idle events.
813 @param Event Event whose notification function is being invoked.
814 @param Context The pointer to the notification function's context,
815 which is implementation-dependent.
820 IdleLoopEventCallback (
830 Initialize the state information for the CPU Architectural Protocol.
832 @param ImageHandle Image handle this driver.
833 @param SystemTable Pointer to the System Table.
835 @retval EFI_SUCCESS Thread can be successfully created
836 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
837 @retval EFI_DEVICE_ERROR Cannot create the thread
843 IN EFI_HANDLE ImageHandle
,
844 IN EFI_SYSTEM_TABLE
*SystemTable
848 EFI_EVENT IdleLoopEvent
;
850 InitializeFloatingPointUnits ();
853 // Make sure interrupts are disabled
855 DisableInterrupts ();
860 InitGlobalDescriptorTable ();
863 // Setup IDT pointer, IDT and interrupt entry points
865 InitInterruptDescriptorTable ();
868 // Enable the local APIC for Virtual Wire Mode.
870 ProgramVirtualWireMode ();
873 // Install CPU Architectural Protocol
875 Status
= gBS
->InstallMultipleProtocolInterfaces (
877 &gEfiCpuArchProtocolGuid
, &gCpu
,
880 ASSERT_EFI_ERROR (Status
);
883 // Refresh GCD memory space map according to MTRR value.
885 RefreshGcdMemoryAttributes ();
888 // Setup a callback for idle events
890 Status
= gBS
->CreateEventEx (
893 IdleLoopEventCallback
,
898 ASSERT_EFI_ERROR (Status
);