audk/OvmfPkg/AcpiPlatformDxe/Qemu.c

320 lines
8.0 KiB
C

/** @file
OVMF ACPI QEMU support
Copyright (c) 2008 - 2012, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "AcpiPlatform.h"
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/QemuFwCfgLib.h>
#include <Library/DxeServicesTableLib.h>
BOOLEAN
QemuDetected (
VOID
)
{
if (!QemuFwCfgIsAvailable ()) {
return FALSE;
}
return TRUE;
}
STATIC
EFI_STATUS
EFIAPI
QemuInstallAcpiMadtTable (
IN EFI_ACPI_TABLE_PROTOCOL *AcpiProtocol,
IN VOID *AcpiTableBuffer,
IN UINTN AcpiTableBufferSize,
OUT UINTN *TableKey
)
{
EFI_STATUS Status;
UINTN Count;
UINTN Loop;
EFI_ACPI_DESCRIPTION_HEADER *Hdr;
UINTN NewBufferSize;
EFI_ACPI_1_0_PROCESSOR_LOCAL_APIC_STRUCTURE *LocalApic;
QemuFwCfgSelectItem (QemuFwCfgItemSmpCpuCount);
Count = (UINTN) QemuFwCfgRead16 ();
ASSERT (Count >= 1);
if (Count == 1) {
//
// The pre-built MADT table covers the single CPU case
//
return InstallAcpiTable (
AcpiProtocol,
AcpiTableBuffer,
AcpiTableBufferSize,
TableKey
);
}
//
// We need to add additional Local APIC entries to the MADT
//
NewBufferSize = AcpiTableBufferSize + ((Count - 1) * sizeof (*LocalApic));
Hdr = (EFI_ACPI_DESCRIPTION_HEADER*) AllocatePool (NewBufferSize);
ASSERT (Hdr != NULL);
CopyMem (Hdr, AcpiTableBuffer, AcpiTableBufferSize);
LocalApic = (EFI_ACPI_1_0_PROCESSOR_LOCAL_APIC_STRUCTURE*)
(((UINT8*) Hdr) + AcpiTableBufferSize);
//
// Add Local APIC entries for the APs to the MADT
//
for (Loop = 1; Loop < Count; Loop++) {
LocalApic->Type = EFI_ACPI_1_0_PROCESSOR_LOCAL_APIC;
LocalApic->Length = sizeof (*LocalApic);
LocalApic->AcpiProcessorId = (UINT8) Loop;
LocalApic->ApicId = (UINT8) Loop;
LocalApic->Flags = 1;
LocalApic++;
}
Hdr->Length = (UINT32) NewBufferSize;
Status = InstallAcpiTable (AcpiProtocol, Hdr, NewBufferSize, TableKey);
FreePool (Hdr);
return Status;
}
#pragma pack(1)
typedef struct {
UINT64 Base;
UINT64 End;
UINT64 Length;
} PCI_WINDOW;
typedef struct {
PCI_WINDOW PciWindow32;
PCI_WINDOW PciWindow64;
} FIRMWARE_DATA;
#pragma pack()
STATIC
EFI_STATUS
EFIAPI
PopulateFwData(
OUT FIRMWARE_DATA *FwData
)
{
EFI_STATUS Status;
UINTN NumDesc;
EFI_GCD_MEMORY_SPACE_DESCRIPTOR *AllDesc;
Status = gDS->GetMemorySpaceMap (&NumDesc, &AllDesc);
if (Status == EFI_SUCCESS) {
UINT64 NonMmio32MaxExclTop;
UINT64 Mmio32MinBase;
UINT64 Mmio32MaxExclTop;
UINTN CurDesc;
Status = EFI_UNSUPPORTED;
NonMmio32MaxExclTop = 0;
Mmio32MinBase = BASE_4GB;
Mmio32MaxExclTop = 0;
for (CurDesc = 0; CurDesc < NumDesc; ++CurDesc) {
CONST EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Desc;
UINT64 ExclTop;
Desc = &AllDesc[CurDesc];
ExclTop = Desc->BaseAddress + Desc->Length;
if (ExclTop <= BASE_4GB) {
switch (Desc->GcdMemoryType) {
case EfiGcdMemoryTypeNonExistent:
break;
case EfiGcdMemoryTypeReserved:
case EfiGcdMemoryTypeSystemMemory:
if (NonMmio32MaxExclTop < ExclTop) {
NonMmio32MaxExclTop = ExclTop;
}
break;
case EfiGcdMemoryTypeMemoryMappedIo:
if (Mmio32MinBase > Desc->BaseAddress) {
Mmio32MinBase = Desc->BaseAddress;
}
if (Mmio32MaxExclTop < ExclTop) {
Mmio32MaxExclTop = ExclTop;
}
break;
default:
ASSERT(0);
}
}
}
if (Mmio32MinBase < NonMmio32MaxExclTop) {
Mmio32MinBase = NonMmio32MaxExclTop;
}
if (Mmio32MinBase < Mmio32MaxExclTop) {
FwData->PciWindow32.Base = Mmio32MinBase;
FwData->PciWindow32.End = Mmio32MaxExclTop - 1;
FwData->PciWindow32.Length = Mmio32MaxExclTop - Mmio32MinBase;
FwData->PciWindow64.Base = 0;
FwData->PciWindow64.End = 0;
FwData->PciWindow64.Length = 0;
Status = EFI_SUCCESS;
}
FreePool (AllDesc);
}
DEBUG ((
DEBUG_INFO,
"ACPI PciWindow32: Base=0x%08lx End=0x%08lx Length=0x%08lx\n",
FwData->PciWindow32.Base,
FwData->PciWindow32.End,
FwData->PciWindow32.Length
));
DEBUG ((
DEBUG_INFO,
"ACPI PciWindow64: Base=0x%08lx End=0x%08lx Length=0x%08lx\n",
FwData->PciWindow64.Base,
FwData->PciWindow64.End,
FwData->PciWindow64.Length
));
return Status;
}
STATIC
EFI_STATUS
EFIAPI
QemuInstallAcpiSsdtTable (
IN EFI_ACPI_TABLE_PROTOCOL *AcpiProtocol,
IN VOID *AcpiTableBuffer,
IN UINTN AcpiTableBufferSize,
OUT UINTN *TableKey
)
{
EFI_STATUS Status;
FIRMWARE_DATA *FwData;
Status = EFI_OUT_OF_RESOURCES;
FwData = AllocateReservedPool (sizeof (*FwData));
if (FwData != NULL) {
UINTN SsdtSize;
UINT8 *Ssdt;
SsdtSize = AcpiTableBufferSize + 17;
Ssdt = AllocatePool (SsdtSize);
if (Ssdt != NULL) {
Status = PopulateFwData (FwData);
if (Status == EFI_SUCCESS) {
UINT8 *SsdtPtr;
SsdtPtr = Ssdt;
CopyMem (SsdtPtr, AcpiTableBuffer, AcpiTableBufferSize);
SsdtPtr += AcpiTableBufferSize;
//
// build "OperationRegion(FWDT, SystemMemory, 0x12345678, 0x87654321)"
//
*(SsdtPtr++) = 0x5B; // ExtOpPrefix
*(SsdtPtr++) = 0x80; // OpRegionOp
*(SsdtPtr++) = 'F';
*(SsdtPtr++) = 'W';
*(SsdtPtr++) = 'D';
*(SsdtPtr++) = 'T';
*(SsdtPtr++) = 0x00; // SystemMemory
*(SsdtPtr++) = 0x0C; // DWordPrefix
//
// no virtual addressing yet, take the four least significant bytes
//
CopyMem(SsdtPtr, &FwData, 4);
SsdtPtr += 4;
*(SsdtPtr++) = 0x0C; // DWordPrefix
*(UINT32*) SsdtPtr = sizeof (*FwData);
SsdtPtr += 4;
ASSERT(SsdtPtr - Ssdt == SsdtSize);
((EFI_ACPI_DESCRIPTION_HEADER *) Ssdt)->Length = SsdtSize;
Status = InstallAcpiTable (AcpiProtocol, Ssdt, SsdtSize, TableKey);
}
FreePool(Ssdt);
}
if (Status != EFI_SUCCESS) {
FreePool(FwData);
}
}
return Status;
}
EFI_STATUS
EFIAPI
QemuInstallAcpiTable (
IN EFI_ACPI_TABLE_PROTOCOL *AcpiProtocol,
IN VOID *AcpiTableBuffer,
IN UINTN AcpiTableBufferSize,
OUT UINTN *TableKey
)
{
EFI_ACPI_DESCRIPTION_HEADER *Hdr;
EFI_ACPI_TABLE_INSTALL_ACPI_TABLE TableInstallFunction;
Hdr = (EFI_ACPI_DESCRIPTION_HEADER*) AcpiTableBuffer;
switch (Hdr->Signature) {
case EFI_ACPI_1_0_APIC_SIGNATURE:
TableInstallFunction = QemuInstallAcpiMadtTable;
break;
case EFI_ACPI_1_0_SECONDARY_SYSTEM_DESCRIPTION_TABLE_SIGNATURE:
TableInstallFunction = QemuInstallAcpiSsdtTable;
break;
default:
TableInstallFunction = InstallAcpiTable;
}
return TableInstallFunction (
AcpiProtocol,
AcpiTableBuffer,
AcpiTableBufferSize,
TableKey
);
}