#include <Library/MemoryAllocationLib.h>\r
#include <Library/QemuFwCfgLib.h>\r
#include <Library/DxeServicesTableLib.h>\r
-\r
+#include <Library/PcdLib.h>\r
+#include <IndustryStandard/Acpi.h>\r
\r
BOOLEAN\r
QemuDetected (\r
}\r
\r
\r
+STATIC\r
+UINTN\r
+CountBits16 (\r
+ UINT16 Mask\r
+ )\r
+{\r
+ //\r
+ // For all N >= 1, N bits are enough to represent the number of bits set\r
+ // among N bits. It's true for N == 1. When adding a new bit (N := N+1),\r
+ // the maximum number of possibly set bits increases by one, while the\r
+ // representable maximum doubles.\r
+ //\r
+ Mask = ((Mask & 0xAAAA) >> 1) + (Mask & 0x5555);\r
+ Mask = ((Mask & 0xCCCC) >> 2) + (Mask & 0x3333);\r
+ Mask = ((Mask & 0xF0F0) >> 4) + (Mask & 0x0F0F);\r
+ Mask = ((Mask & 0xFF00) >> 8) + (Mask & 0x00FF);\r
+\r
+ return Mask;\r
+}\r
+\r
+\r
STATIC\r
EFI_STATUS\r
EFIAPI\r
OUT UINTN *TableKey\r
)\r
{\r
- EFI_STATUS Status;\r
- UINTN Count;\r
- UINTN Loop;\r
- EFI_ACPI_DESCRIPTION_HEADER *Hdr;\r
- UINTN NewBufferSize;\r
- EFI_ACPI_1_0_PROCESSOR_LOCAL_APIC_STRUCTURE *LocalApic;\r
+ UINTN CpuCount;\r
+ UINTN PciLinkIsoCount;\r
+ UINTN NewBufferSize;\r
+ EFI_ACPI_1_0_MULTIPLE_APIC_DESCRIPTION_TABLE_HEADER *Madt;\r
+ EFI_ACPI_1_0_PROCESSOR_LOCAL_APIC_STRUCTURE *LocalApic;\r
+ EFI_ACPI_1_0_IO_APIC_STRUCTURE *IoApic;\r
+ EFI_ACPI_1_0_INTERRUPT_SOURCE_OVERRIDE_STRUCTURE *Iso;\r
+ EFI_ACPI_1_0_LOCAL_APIC_NMI_STRUCTURE *LocalApicNmi;\r
+ VOID *Ptr;\r
+ UINTN Loop;\r
+ EFI_STATUS Status;\r
+\r
+ ASSERT (AcpiTableBufferSize >= sizeof (EFI_ACPI_DESCRIPTION_HEADER));\r
\r
QemuFwCfgSelectItem (QemuFwCfgItemSmpCpuCount);\r
- Count = (UINTN) QemuFwCfgRead16 ();\r
- ASSERT (Count >= 1);\r
-\r
- if (Count == 1) {\r
- //\r
- // The pre-built MADT table covers the single CPU case\r
- //\r
- return InstallAcpiTable (\r
- AcpiProtocol,\r
- AcpiTableBuffer,\r
- AcpiTableBufferSize,\r
- TableKey\r
- );\r
- }\r
+ CpuCount = QemuFwCfgRead16 ();\r
+ ASSERT (CpuCount >= 1);\r
\r
//\r
- // We need to add additional Local APIC entries to the MADT\r
+ // Set Level-tiggered, Active High for these identity mapped IRQs. The bitset\r
+ // corresponds to the union of all possible interrupt assignments for the LNKA,\r
+ // LNKB, LNKC, LNKD PCI interrupt lines. See the DSDT.\r
//\r
- NewBufferSize = AcpiTableBufferSize + ((Count - 1) * sizeof (*LocalApic));\r
- Hdr = (EFI_ACPI_DESCRIPTION_HEADER*) AllocatePool (NewBufferSize);\r
- ASSERT (Hdr != NULL);\r
+ PciLinkIsoCount = CountBits16 (PcdGet16 (Pcd8259LegacyModeEdgeLevel));\r
+\r
+ NewBufferSize = 1 * sizeof (*Madt) +\r
+ CpuCount * sizeof (*LocalApic) +\r
+ 1 * sizeof (*IoApic) +\r
+ (1 + PciLinkIsoCount) * sizeof (*Iso) +\r
+ 1 * sizeof (*LocalApicNmi);\r
\r
- CopyMem (Hdr, AcpiTableBuffer, AcpiTableBufferSize);\r
+ Madt = AllocatePool (NewBufferSize);\r
+ if (Madt == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ Madt->Header = *(EFI_ACPI_DESCRIPTION_HEADER *) AcpiTableBuffer;\r
+ Madt->Header.Length = NewBufferSize;\r
+ Madt->LocalApicAddress = PcdGet32 (PcdCpuLocalApicBaseAddress);\r
+ Madt->Flags = EFI_ACPI_1_0_PCAT_COMPAT;\r
+ Ptr = Madt + 1;\r
+\r
+ LocalApic = Ptr;\r
+ for (Loop = 0; Loop < CpuCount; ++Loop) {\r
+ LocalApic->Type = EFI_ACPI_1_0_PROCESSOR_LOCAL_APIC;\r
+ LocalApic->Length = sizeof (*LocalApic);\r
+ LocalApic->AcpiProcessorId = Loop;\r
+ LocalApic->ApicId = Loop;\r
+ LocalApic->Flags = 1; // enabled\r
+ ++LocalApic;\r
+ }\r
+ Ptr = LocalApic;\r
\r
- LocalApic = (EFI_ACPI_1_0_PROCESSOR_LOCAL_APIC_STRUCTURE*)\r
- (((UINT8*) Hdr) + AcpiTableBufferSize);\r
+ IoApic = Ptr;\r
+ IoApic->Type = EFI_ACPI_1_0_IO_APIC;\r
+ IoApic->Length = sizeof (*IoApic);\r
+ IoApic->IoApicId = CpuCount;\r
+ IoApic->Reserved = EFI_ACPI_RESERVED_BYTE;\r
+ IoApic->IoApicAddress = 0xFEC00000;\r
+ IoApic->SystemVectorBase = 0x00000000;\r
+ Ptr = IoApic + 1;\r
\r
//\r
- // Add Local APIC entries for the APs to the MADT\r
+ // IRQ0 (8254 Timer) => IRQ2 (PIC) Interrupt Source Override Structure\r
//\r
- for (Loop = 1; Loop < Count; Loop++) {\r
- LocalApic->Type = EFI_ACPI_1_0_PROCESSOR_LOCAL_APIC;\r
- LocalApic->Length = sizeof (*LocalApic);\r
- LocalApic->AcpiProcessorId = (UINT8) Loop;\r
- LocalApic->ApicId = (UINT8) Loop;\r
- LocalApic->Flags = 1;\r
- LocalApic++;\r
- }\r
+ Iso = Ptr;\r
+ Iso->Type = EFI_ACPI_1_0_INTERRUPT_SOURCE_OVERRIDE;\r
+ Iso->Length = sizeof (*Iso);\r
+ Iso->Bus = 0x00; // ISA\r
+ Iso->Source = 0x00; // IRQ0\r
+ Iso->GlobalSystemInterruptVector = 0x00000002;\r
+ Iso->Flags = 0x0000; // Conforms to specs of the bus\r
+ ++Iso;\r
\r
- Hdr->Length = (UINT32) NewBufferSize;\r
+ //\r
+ // Set Level-tiggered, Active High for all possible PCI link targets.\r
+ //\r
+ for (Loop = 0; Loop < 16; ++Loop) {\r
+ if ((PcdGet16 (Pcd8259LegacyModeEdgeLevel) & (1 << Loop)) == 0) {\r
+ continue;\r
+ }\r
+ Iso->Type = EFI_ACPI_1_0_INTERRUPT_SOURCE_OVERRIDE;\r
+ Iso->Length = sizeof (*Iso);\r
+ Iso->Bus = 0x00; // ISA\r
+ Iso->Source = Loop;\r
+ Iso->GlobalSystemInterruptVector = Loop;\r
+ Iso->Flags = 0x000D; // Level-tiggered, Active High\r
+ ++Iso;\r
+ }\r
+ ASSERT (\r
+ Iso - (EFI_ACPI_1_0_INTERRUPT_SOURCE_OVERRIDE_STRUCTURE *)Ptr ==\r
+ 1 + PciLinkIsoCount\r
+ );\r
+ Ptr = Iso;\r
+\r
+ LocalApicNmi = Ptr;\r
+ LocalApicNmi->Type = EFI_ACPI_1_0_LOCAL_APIC_NMI;\r
+ LocalApicNmi->Length = sizeof (*LocalApicNmi);\r
+ LocalApicNmi->AcpiProcessorId = 0xFF; // applies to all processors\r
+ //\r
+ // polarity and trigger mode of the APIC I/O input signals conform to the\r
+ // specifications of the bus\r
+ //\r
+ LocalApicNmi->Flags = 0x0000;\r
+ //\r
+ // Local APIC interrupt input LINTn to which NMI is connected.\r
+ //\r
+ LocalApicNmi->LocalApicInti = 0x01;\r
+ Ptr = LocalApicNmi + 1;\r
\r
- Status = InstallAcpiTable (AcpiProtocol, Hdr, NewBufferSize, TableKey);\r
+ ASSERT ((UINT8 *)Ptr - (UINT8 *)Madt == NewBufferSize);\r
+ Status = InstallAcpiTable (AcpiProtocol, Madt, NewBufferSize, TableKey);\r
\r
- FreePool (Hdr);\r
+ FreePool (Madt);\r
\r
return Status;\r
}\r