2 Emu driver to produce CPU Architectural Protocol.
4 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2011 - 2012, Apple Inc. All rights reserved.
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.
16 #include "CpuDriver.h"
20 CPU_ARCH_PROTOCOL_PRIVATE mCpuTemplate
= {
21 CPU_ARCH_PROT_PRIVATE_SIGNATURE
,
29 EmuRegisterInterruptHandler
,
31 EmuSetMemoryAttributes
,
48 #define EFI_CPU_DATA_MAXIMUM_LENGTH 0x100
50 SMBIOS_TABLE_TYPE4 mCpuSmbiosType4
= {
51 { EFI_SMBIOS_TYPE_PROCESSOR_INFORMATION
, sizeof (SMBIOS_TABLE_TYPE4
), 0},
53 ProcessorOther
, // ProcessorType; ///< The enumeration value from PROCESSOR_TYPE_DATA.
54 ProcessorFamilyOther
, // ProcessorFamily; ///< The enumeration value from PROCESSOR_FAMILY_DATA.
55 2, // ProcessorManufacture String;
57 { // PROCESSOR_SIGNATURE
58 0, // ProcessorSteppingId:4;
59 0, // ProcessorModel: 4;
60 0, // ProcessorFamily: 4;
61 0, // ProcessorType: 2;
62 0, // ProcessorReserved1: 2;
63 0, // ProcessorXModel: 4;
64 0, // ProcessorXFamily: 8;
65 0, // ProcessorReserved2: 4;
67 { // PROCESSOR_FEATURE_FLAGS
68 0, // ProcessorFpu :1;
69 0, // ProcessorVme :1;
71 0, // ProcessorPse :1;
72 0, // ProcessorTsc :1;
73 0, // ProcessorMsr :1;
74 0, // ProcessorPae :1;
75 0, // ProcessorMce :1;
76 0, // ProcessorCx8 :1;
77 0, // ProcessorApic :1;
78 0, // ProcessorReserved1 :1;
79 0, // ProcessorSep :1;
80 0, // ProcessorMtrr :1;
81 0, // ProcessorPge :1;
82 0, // ProcessorMca :1;
83 0, // ProcessorCmov :1;
84 0, // ProcessorPat :1;
85 0, // ProcessorPse36 :1;
86 0, // ProcessorPsn :1;
87 0, // ProcessorClfsh :1;
88 0, // ProcessorReserved2 :1;
90 0, // ProcessorAcpi :1;
91 0, // ProcessorMmx :1;
92 0, // ProcessorFxsr :1;
93 0, // ProcessorSse :1;
94 0, // ProcessorSse2 :1;
96 0, // ProcessorReserved3 :1;
98 0, // ProcessorReserved4 :2;
101 3, // ProcessorVersion String;
103 1, // ProcessorVoltageCapability5V :1;
104 1, // ProcessorVoltageCapability3_3V :1;
105 1, // ProcessorVoltageCapability2_9V :1;
106 0, // ProcessorVoltageCapabilityReserved :1; ///< Bit 3, must be zero.
107 0, // ProcessorVoltageReserved :3; ///< Bits 4-6, must be zero.
108 0 // ProcessorVoltageIndicateLegacy :1;
114 ProcessorUpgradeOther
, // ProcessorUpgrade; ///< The enumeration value from PROCESSOR_UPGRADE.
122 0, // EnabledCoreCount;
124 0, // ProcessorCharacteristics;
125 0, // ProcessorFamily2;
128 CHAR8
*mCpuSmbiosType4Strings
[] = {
130 "http://www.tianocore.org/edk2/",
131 "Emulated Processor",
140 Create SMBIOS record.
142 Converts a fixed SMBIOS structure and an array of pointers to strings into
143 an SMBIOS record where the strings are cat'ed on the end of the fixed record
144 and terminated via a double NULL and add to SMBIOS table.
146 SMBIOS_TABLE_TYPE32 gSmbiosType12 = {
147 { EFI_SMBIOS_TYPE_SYSTEM_CONFIGURATION_OPTIONS, sizeof (SMBIOS_TABLE_TYPE12), 0 },
150 CHAR8 *gSmbiosType12Strings[] = {
157 (EFI_SMBIOS_TABLE_HEADER*)&gSmbiosType12,
161 @param Template Fixed SMBIOS structure, required.
162 @param StringArray Array of strings to convert to an SMBIOS string pack.
168 IN EFI_SMBIOS_TABLE_HEADER
*Template
,
169 IN CHAR8
**StringPack
173 EFI_SMBIOS_PROTOCOL
*Smbios
;
174 EFI_SMBIOS_HANDLE SmbiosHandle
;
175 EFI_SMBIOS_TABLE_HEADER
*Record
;
182 // Locate Smbios protocol.
184 Status
= gBS
->LocateProtocol (&gEfiSmbiosProtocolGuid
, NULL
, (VOID
**)&Smbios
);
185 if (EFI_ERROR (Status
)) {
189 // Calculate the size of the fixed record and optional string pack
190 Size
= Template
->Length
;
191 if (StringPack
== NULL
) {
192 // At least a double null is required
195 for (Index
= 0; StringPack
[Index
] != NULL
; Index
++) {
196 StringSize
= AsciiStrSize (StringPack
[Index
]);
199 if (StringPack
[0] == NULL
) {
200 // At least a double null is required
203 // Don't forget the terminating double null
207 // Copy over Template
208 Record
= (EFI_SMBIOS_TABLE_HEADER
*)AllocateZeroPool (Size
);
209 if (Record
== NULL
) {
210 return EFI_OUT_OF_RESOURCES
;
212 CopyMem (Record
, Template
, Template
->Length
);
214 // Append string pack
215 Str
= ((CHAR8
*)Record
) + Record
->Length
;
216 for (Index
= 0; StringPack
[Index
] != NULL
; Index
++) {
217 StringSize
= AsciiStrSize (StringPack
[Index
]);
218 CopyMem (Str
, StringPack
[Index
], StringSize
);
223 SmbiosHandle
= SMBIOS_HANDLE_PI_RESERVED
;
224 Status
= Smbios
->Add (
230 ASSERT_EFI_ERROR (Status
);
244 mCpuSmbiosType4
.CoreCount
= (UINT8
) MaxCpus
;
245 mCpuSmbiosType4
.EnabledCoreCount
= (UINT8
) MaxCpus
;
246 mCpuSmbiosType4
.ThreadCount
= (UINT8
) MaxCpus
;
248 LogSmbiosData ((EFI_SMBIOS_TABLE_HEADER
*)&mCpuSmbiosType4
, mCpuSmbiosType4Strings
);
253 // Service routines for the driver
257 EmuFlushCpuDataCache (
258 IN EFI_CPU_ARCH_PROTOCOL
*This
,
259 IN EFI_PHYSICAL_ADDRESS Start
,
261 IN EFI_CPU_FLUSH_TYPE FlushType
264 if (FlushType
== EfiCpuFlushTypeWriteBackInvalidate
) {
266 // Only WB flush is supported. We actually need do nothing on Emu emulator
267 // environment. Classify this to follow EFI spec
272 // Other flush types are not supported by Emu emulator
274 return EFI_UNSUPPORTED
;
280 IN EFI_CPU_ARCH_PROTOCOL
*This
283 CPU_ARCH_PROTOCOL_PRIVATE
*Private
;
285 Private
= CPU_ARCH_PROTOCOL_PRIVATE_DATA_FROM_THIS (This
);
286 Private
->InterruptState
= TRUE
;
287 gEmuThunk
->EnableInterrupt ();
293 EmuDisableInterrupt (
294 IN EFI_CPU_ARCH_PROTOCOL
*This
297 CPU_ARCH_PROTOCOL_PRIVATE
*Private
;
299 Private
= CPU_ARCH_PROTOCOL_PRIVATE_DATA_FROM_THIS (This
);
300 Private
->InterruptState
= FALSE
;
301 gEmuThunk
->DisableInterrupt ();
307 EmuGetInterruptState (
308 IN EFI_CPU_ARCH_PROTOCOL
*This
,
312 CPU_ARCH_PROTOCOL_PRIVATE
*Private
;
315 return EFI_INVALID_PARAMETER
;
318 Private
= CPU_ARCH_PROTOCOL_PRIVATE_DATA_FROM_THIS (This
);
319 *State
= Private
->InterruptState
;
326 IN EFI_CPU_ARCH_PROTOCOL
*This
,
327 IN EFI_CPU_INIT_TYPE InitType
330 return EFI_UNSUPPORTED
;
335 EmuRegisterInterruptHandler (
336 IN EFI_CPU_ARCH_PROTOCOL
*This
,
337 IN EFI_EXCEPTION_TYPE InterruptType
,
338 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
342 // Do parameter checking for EFI spec conformance
344 if (InterruptType
< 0 || InterruptType
> 0xff) {
345 return EFI_UNSUPPORTED
;
348 // Do nothing for Emu emulation
350 return EFI_UNSUPPORTED
;
356 IN EFI_CPU_ARCH_PROTOCOL
*This
,
357 IN UINT32 TimerIndex
,
358 OUT UINT64
*TimerValue
,
359 OUT UINT64
*TimerPeriod OPTIONAL
362 if (TimerValue
== NULL
) {
363 return EFI_INVALID_PARAMETER
;
366 if (TimerIndex
!= 0) {
367 return EFI_INVALID_PARAMETER
;
370 *TimerValue
= gEmuThunk
->QueryPerformanceCounter ();
372 if (TimerPeriod
!= NULL
) {
373 *TimerPeriod
= mTimerPeriod
;
382 EmuSetMemoryAttributes (
383 IN EFI_CPU_ARCH_PROTOCOL
*This
,
384 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
390 // Check for invalid parameter for Spec conformance
393 return EFI_INVALID_PARAMETER
;
397 // Do nothing for Nt32 emulation
399 return EFI_UNSUPPORTED
;
406 Callback function for idle events.
408 @param Event Event whose notification function is being invoked.
409 @param Context The pointer to the notification function's context,
410 which is implementation-dependent.
415 IdleLoopEventCallback (
420 gEmuThunk
->CpuSleep ();
427 IN EFI_HANDLE ImageHandle
,
428 IN EFI_SYSTEM_TABLE
*SystemTable
433 EFI_EVENT IdleLoopEvent
;
437 // Retrieve the frequency of the performance counter in Hz.
439 Frequency
= gEmuThunk
->QueryPerformanceFrequency ();
442 // Convert frequency in Hz to a clock period in femtoseconds.
444 mTimerPeriod
= DivU64x64Remainder (1000000000000000ULL, Frequency
, NULL
);
446 CpuMpServicesInit (&MaxCpu
);
448 CpuUpdateSmbios (MaxCpu
);
451 Status
= gBS
->CreateEventEx (
454 IdleLoopEventCallback
,
459 ASSERT_EFI_ERROR (Status
);
462 Status
= gBS
->InstallMultipleProtocolInterfaces (
463 &mCpuTemplate
.Handle
,
464 &gEfiCpuArchProtocolGuid
, &mCpuTemplate
.Cpu
,
465 &gEfiCpuIo2ProtocolGuid
, &mCpuTemplate
.CpuIo
,
468 ASSERT_EFI_ERROR (Status
);