From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f68.google.com (mail-pj1-f68.google.com [209.85.216.68]) by mx.groups.io with SMTP id smtpd.web10.9000.1598340263158673842 for ; Tue, 25 Aug 2020 00:24:23 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@linaro.org header.s=google header.b=sdYRw8ZF; spf=pass (domain: linaro.org, ip: 209.85.216.68, mailfrom: tanmay.jagdale@linaro.org) Received: by mail-pj1-f68.google.com with SMTP id z18so777928pjr.2 for ; Tue, 25 Aug 2020 00:24:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=L64jr1dlxkn+2A5r7kg8RKmy8Hv5QqQiHe3Ev8tdQeY=; b=sdYRw8ZF4g2YvTsT7870j5JXL7ySQOETTKbkRKKRnDV8VXyCOTqSzEGezBwau68Z0S HEFI3rmIuARaXpU95ZHWp3PBPnNtLl7LXWcEpsJf3VHxtIAcB8i96Hn76AnG//OFK1IN 0gmWryH3ybUer+YwTjooTZfMROown2imPlBNYnd8Ap/69sOUFGJ5RObx04TfsuTfAcRj d3Cs+Bju6R/uZWp10dFiK7vaf81P/FKLlM8u60ee5p1kFhC9JiHTChnmCLanUkobRfXy 2kOybvEBq+CbtvySUVHT5Q3HBDVcrPFg9ZQt4fTQHPbZwY2MWXp/L/XF74YqGzgi33Vg HD7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=L64jr1dlxkn+2A5r7kg8RKmy8Hv5QqQiHe3Ev8tdQeY=; b=mJW3QvdJwJDqsIE5K0p9NBgH9gMxMZmnDhmwITaantFT0zvuJzLP46g0OWpXJmWzcp SNn8GBer+ZhFDqNunhyj/cLNCC107+dqq5abwgCtaOe/tshAtEhC6bymqosOfWknk6h5 4Ks2k89qkOmeGcpwU2SDLCKyOQ42oRDs1P5o/1nUhmzu5YCM3o+d+11d87xJb0kUU+Jl TI06rEw7uW2PzDANdcLuCA/MA0P1ZA9Kz6dpAr/PszVQuF5x+pHqKN9JODtQdGp8mBrv DsY66cBeaz+kIKTCSy3K4MeGp6dhUxZ6+3/3A3/7QFeGC3ve4rdS8RIRn+ZBnPDV2diI 4+uA== X-Gm-Message-State: AOAM530NeLE8JxNwfu24Og11Ww0m71U92hUrYj3cXUraocv2TuA2ZjYz igWiECMB+h1ZzAcXHdOZTvpe+g== X-Google-Smtp-Source: ABdhPJx8o0HM+gWHUYev+MaD0IxB7Ti9BWPiaiemkTg4+NwBfsFYWQISYJfvq3s9XQFKQbFQk2E8/g== X-Received: by 2002:a17:90a:fb8b:: with SMTP id cp11mr522368pjb.56.1598340262657; Tue, 25 Aug 2020 00:24:22 -0700 (PDT) Return-Path: Received: from bean-canyon.localdomain ([106.51.140.68]) by smtp.googlemail.com with ESMTPSA id d5sm1722417pjw.18.2020.08.25.00.24.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Aug 2020 00:24:22 -0700 (PDT) From: "Tanmay Jagdale" To: leif@nuviainc.com, graeme@nuviainc.com, shashi.mallela@linaro.org, devel@edk2.groups.io Cc: paul.isaacs@linaro.org, tanmay@marvell.com, Tanmay Jagdale Subject: [PATCH v2 edk2-platforms 6/8] SbsaQemu: AcpiDxe: Create SSDT table at runtime Date: Tue, 25 Aug 2020 12:53:20 +0530 Message-Id: <20200825072322.10848-7-tanmay.jagdale@linaro.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200825072322.10848-1-tanmay.jagdale@linaro.org> References: <20200825072322.10848-1-tanmay.jagdale@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit - Add support to create SSDT table at runtime. Since SSDT table is a data table, added a few helper macros to create the AML entries. - Also added a function to calculate the length of Packages. Signed-off-by: Tanmay Jagdale --- Silicon/Qemu/SbsaQemu/Include/IndustryStandard/SbsaQemuAcpi.h | 29 ++++ Silicon/Qemu/SbsaQemu/Drivers/SbsaQemuAcpiDxe/SbsaQemuAcpiDxe.c | 144 ++++++++++++++++++++ 2 files changed, 173 insertions(+) diff --git a/Silicon/Qemu/SbsaQemu/Include/IndustryStandard/SbsaQemuAcpi.h b/Silicon/Qemu/SbsaQemu/Include/IndustryStandard/SbsaQemuAcpi.h index 7a9a0061675f..00c7c68256fd 100644 --- a/Silicon/Qemu/SbsaQemu/Include/IndustryStandard/SbsaQemuAcpi.h +++ b/Silicon/Qemu/SbsaQemu/Include/IndustryStandard/SbsaQemuAcpi.h @@ -43,4 +43,33 @@ #define SBSAQEMU_PCI_SEG0_BUSNUM_MIN 0x00 #define SBSAQEMU_PCI_SEG0_BUSNUM_MAX 0xFF +#define SBSAQEMU_ACPI_SCOPE_OP_MAX_LENGTH 5 + +#define SBSAQEMU_ACPI_SCOPE_NAME { '_', 'S', 'B', '_' } + +#define SBSAQEMU_ACPI_CPU_DEV_LEN 0x1C +#define SBSAQEMU_ACPI_CPU_DEV_NAME { 'C', '0', '0', '0' } + +// Macro to convert Integer to Character +#define SBSAQEMU_ACPI_ITOA(Byte) (0x30 + (Byte > 9 ? (Byte + 1) : Byte)) + +#define SBSAQEMU_ACPI_CPU_HID { \ + AML_NAME_OP, AML_NAME_CHAR__, 'H', 'I', 'D', \ + AML_STRING_PREFIX, 'A', 'C', 'P', 'I', '0', '0', '0', '7', \ + AML_ZERO_OP \ + } + +#define SBSAQEMU_ACPI_CPU_UID { \ + AML_NAME_OP, AML_NAME_CHAR__, 'U', 'I', 'D', AML_BYTE_PREFIX, \ + AML_ZERO_OP, AML_ZERO_OP \ + } + +typedef struct { + UINT8 device_header[2]; + UINT8 length; + UINT8 dev_name[4]; + UINT8 hid[15]; + UINT8 uid[8]; +} SBSAQEMU_ACPI_CPU_DEVICE; + #endif diff --git a/Silicon/Qemu/SbsaQemu/Drivers/SbsaQemuAcpiDxe/SbsaQemuAcpiDxe.c b/Silicon/Qemu/SbsaQemu/Drivers/SbsaQemuAcpiDxe/SbsaQemuAcpiDxe.c index 1a8cfb8b0a2e..ebffa1db4734 100644 --- a/Silicon/Qemu/SbsaQemu/Drivers/SbsaQemuAcpiDxe/SbsaQemuAcpiDxe.c +++ b/Silicon/Qemu/SbsaQemu/Drivers/SbsaQemuAcpiDxe/SbsaQemuAcpiDxe.c @@ -7,6 +7,7 @@ * **/ #include +#include #include #include #include @@ -199,6 +200,144 @@ AddMadtTable ( return Status; } +/* + * Function to calculate the PkgLength field in ACPI tables + */ +STATIC +UINT32 +SetPkgLength ( + IN UINT8 *TablePtr, + IN UINT32 Length +) +{ + UINT8 ByteCount; + UINT8 *PkgLeadByte = TablePtr; + + if (Length < 64) { + *TablePtr = Length; + return 1; + } + + // Set the LSB of Length in PkgLeadByte and advance Length + *PkgLeadByte = Length & 0xF; + Length = Length >> 4; + + while (Length) { + TablePtr++; + *TablePtr = (Length & 0xFF); + Length = (Length >> 8); + } + + // Calculate the number of bytes the Length field uses + // and set the ByteCount field in PkgLeadByte. + ByteCount = (TablePtr - PkgLeadByte) & 0xF; + *PkgLeadByte |= (ByteCount << 6); + + return ByteCount + 1; +} + +/* + * A function that adds SSDT ACPI table. + */ +EFI_STATUS +AddSsdtTable ( + IN EFI_ACPI_TABLE_PROTOCOL *AcpiTable + ) +{ + EFI_STATUS Status; + UINTN TableHandle; + UINT32 TableSize; + EFI_PHYSICAL_ADDRESS PageAddress; + UINT8 *New; + UINT32 CpuId; + UINT32 Offset; + UINT8 ScopeOpName[] = SBSAQEMU_ACPI_SCOPE_NAME; + UINT32 NumCores = PcdGet32 (PcdCoreCount); + + EFI_ACPI_DESCRIPTION_HEADER Header = + SBSAQEMU_ACPI_HEADER ( + EFI_ACPI_6_0_SECONDARY_SYSTEM_DESCRIPTION_TABLE_SIGNATURE, + EFI_ACPI_DESCRIPTION_HEADER, + EFI_ACPI_6_0_SECONDARY_SYSTEM_DESCRIPTION_TABLE_REVISION); + + SBSAQEMU_ACPI_CPU_DEVICE CpuDevice = { + { AML_EXT_OP, AML_EXT_DEVICE_OP }, /* Device () */ + SBSAQEMU_ACPI_CPU_DEV_LEN, /* Length */ + SBSAQEMU_ACPI_CPU_DEV_NAME, /* Device Name "C000" */ + SBSAQEMU_ACPI_CPU_HID, /* Name (HID, "ACPI0007") */ + SBSAQEMU_ACPI_CPU_UID, /* Name (UID, 0) */ + }; + + // Calculate the new table size based on the number of cores + TableSize = sizeof (EFI_ACPI_DESCRIPTION_HEADER) + + SBSAQEMU_ACPI_SCOPE_OP_MAX_LENGTH + sizeof (ScopeOpName) + + (sizeof (CpuDevice) * NumCores); + + Status = gBS->AllocatePages ( + AllocateAnyPages, + EfiACPIReclaimMemory, + EFI_SIZE_TO_PAGES (TableSize), + &PageAddress + ); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "Failed to allocate pages for SSDT table\n")); + return EFI_OUT_OF_RESOURCES; + } + + New = (UINT8 *)(UINTN) PageAddress; + ZeroMem (New, TableSize); + + // Add the ACPI Description table header + CopyMem (New, &Header, sizeof (EFI_ACPI_DESCRIPTION_HEADER)); + ((EFI_ACPI_DESCRIPTION_HEADER*) New)->Length = TableSize; + New += sizeof (EFI_ACPI_DESCRIPTION_HEADER); + + // Insert the top level ScopeOp + *New = AML_SCOPE_OP; + New++; + Offset = SetPkgLength (New, + (TableSize - sizeof (EFI_ACPI_DESCRIPTION_HEADER) - 1)); + New += Offset; + CopyMem (New, &ScopeOpName, sizeof (ScopeOpName)); + New += sizeof (ScopeOpName); + + // Add new Device structures for the Cores + for (CpuId = 0; CpuId < NumCores; CpuId++) { + SBSAQEMU_ACPI_CPU_DEVICE *CpuDevicePtr; + UINT8 CpuIdByte1, CpuIdByte2, CpuIdByte3; + + CopyMem (New, &CpuDevice, sizeof (SBSAQEMU_ACPI_CPU_DEVICE)); + CpuDevicePtr = (SBSAQEMU_ACPI_CPU_DEVICE *) New; + + CpuIdByte1 = CpuId & 0xF; + CpuIdByte2 = (CpuId >> 4) & 0xF; + CpuIdByte3 = (CpuId >> 8) & 0xF; + + CpuDevicePtr->dev_name[1] = SBSAQEMU_ACPI_ITOA(CpuIdByte3); + CpuDevicePtr->dev_name[2] = SBSAQEMU_ACPI_ITOA(CpuIdByte2); + CpuDevicePtr->dev_name[3] = SBSAQEMU_ACPI_ITOA(CpuIdByte1); + + CpuDevicePtr->uid[6] = CpuIdByte1 | CpuIdByte2; + CpuDevicePtr->uid[7] = CpuIdByte3; + New += sizeof (SBSAQEMU_ACPI_CPU_DEVICE); + } + + // Perform Checksum + AcpiPlatformChecksum ((UINT8*) PageAddress, TableSize); + + Status = AcpiTable->InstallAcpiTable ( + AcpiTable, + (EFI_ACPI_COMMON_HEADER *)PageAddress, + TableSize, + &TableHandle + ); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "Failed to install SSDT table\n")); + } + + return Status; +} + EFI_STATUS EFIAPI InitializeSbsaQemuAcpiDxe ( @@ -228,5 +367,10 @@ InitializeSbsaQemuAcpiDxe ( DEBUG((EFI_D_ERROR, "Failed to add MADT table\n")); } + Status = AddSsdtTable (AcpiTable); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "Failed to add SSDT table\n")); + } + return EFI_SUCCESS; } -- 2.28.0